2017-05-12 Paolo Carlini <paolo.carlini@oracle.com>
[official-gcc.git] / gcc / cp / parser.c
blobf82a90c43d2f58127507e69022d4662c8c667a1d
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (C_IS_RESERVED_WORD (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value, 0);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2779 if (!cp_parser_simulate_error (parser))
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2786 if (token->type == CPP_PRAGMA)
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2821 static void
2822 cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2847 else if (parser->scope && parser->scope != global_namespace)
2849 switch (desired)
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2869 else if (parser->scope == global_namespace)
2871 switch (desired)
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2888 else
2890 switch (desired)
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2912 static bool
2913 cp_parser_simulate_error (cp_parser* parser)
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2920 return false;
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2927 static bool
2928 cp_parser_check_type_definition (cp_parser* parser)
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2938 return true;
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2947 static void
2948 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2976 static void
2977 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2982 cp_token_position start = 0;
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2986 if (TYPE_P (type))
2987 error_at (location, "%qT is not a template", type);
2988 else if (identifier_p (type))
2990 if (tag_type != none_type)
2991 error_at (location, "%qE is not a class template", type);
2992 else
2993 error_at (location, "%qE is not a template", type);
2995 else
2996 error_at (location, "invalid template-id");
2997 /* Remember the location of the invalid "<". */
2998 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2999 start = cp_lexer_token_position (parser->lexer, true);
3000 /* Consume the "<". */
3001 cp_lexer_consume_token (parser->lexer);
3002 /* Parse the template arguments. */
3003 cp_parser_enclosed_template_argument_list (parser);
3004 /* Permanently remove the invalid template arguments so that
3005 this error message is not issued again. */
3006 if (start)
3007 cp_lexer_purge_tokens_after (parser->lexer, start);
3011 /* If parsing an integral constant-expression, issue an error message
3012 about the fact that THING appeared and return true. Otherwise,
3013 return false. In either case, set
3014 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3016 static bool
3017 cp_parser_non_integral_constant_expression (cp_parser *parser,
3018 non_integral_constant thing)
3020 parser->non_integral_constant_expression_p = true;
3021 if (parser->integral_constant_expression_p)
3023 if (!parser->allow_non_integral_constant_expression_p)
3025 const char *msg = NULL;
3026 switch (thing)
3028 case NIC_FLOAT:
3029 pedwarn (input_location, OPT_Wpedantic,
3030 "ISO C++ forbids using a floating-point literal "
3031 "in a constant-expression");
3032 return true;
3033 case NIC_CAST:
3034 error ("a cast to a type other than an integral or "
3035 "enumeration type cannot appear in a "
3036 "constant-expression");
3037 return true;
3038 case NIC_TYPEID:
3039 error ("%<typeid%> operator "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_NCC:
3043 error ("non-constant compound literals "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_FUNC_CALL:
3047 error ("a function call "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_INC:
3051 error ("an increment "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_DEC:
3055 error ("an decrement "
3056 "cannot appear in a constant-expression");
3057 return true;
3058 case NIC_ARRAY_REF:
3059 error ("an array reference "
3060 "cannot appear in a constant-expression");
3061 return true;
3062 case NIC_ADDR_LABEL:
3063 error ("the address of a label "
3064 "cannot appear in a constant-expression");
3065 return true;
3066 case NIC_OVERLOADED:
3067 error ("calls to overloaded operators "
3068 "cannot appear in a constant-expression");
3069 return true;
3070 case NIC_ASSIGNMENT:
3071 error ("an assignment cannot appear in a constant-expression");
3072 return true;
3073 case NIC_COMMA:
3074 error ("a comma operator "
3075 "cannot appear in a constant-expression");
3076 return true;
3077 case NIC_CONSTRUCTOR:
3078 error ("a call to a constructor "
3079 "cannot appear in a constant-expression");
3080 return true;
3081 case NIC_TRANSACTION:
3082 error ("a transaction expression "
3083 "cannot appear in a constant-expression");
3084 return true;
3085 case NIC_THIS:
3086 msg = "this";
3087 break;
3088 case NIC_FUNC_NAME:
3089 msg = "__FUNCTION__";
3090 break;
3091 case NIC_PRETTY_FUNC:
3092 msg = "__PRETTY_FUNCTION__";
3093 break;
3094 case NIC_C99_FUNC:
3095 msg = "__func__";
3096 break;
3097 case NIC_VA_ARG:
3098 msg = "va_arg";
3099 break;
3100 case NIC_ARROW:
3101 msg = "->";
3102 break;
3103 case NIC_POINT:
3104 msg = ".";
3105 break;
3106 case NIC_STAR:
3107 msg = "*";
3108 break;
3109 case NIC_ADDR:
3110 msg = "&";
3111 break;
3112 case NIC_PREINCREMENT:
3113 msg = "++";
3114 break;
3115 case NIC_PREDECREMENT:
3116 msg = "--";
3117 break;
3118 case NIC_NEW:
3119 msg = "new";
3120 break;
3121 case NIC_DEL:
3122 msg = "delete";
3123 break;
3124 default:
3125 gcc_unreachable ();
3127 if (msg)
3128 error ("%qs cannot appear in a constant-expression", msg);
3129 return true;
3132 return false;
3135 /* Emit a diagnostic for an invalid type name. This function commits
3136 to the current active tentative parse, if any. (Otherwise, the
3137 problematic construct might be encountered again later, resulting
3138 in duplicate error messages.) LOCATION is the location of ID. */
3140 static void
3141 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3142 location_t location)
3144 tree decl, ambiguous_decls;
3145 cp_parser_commit_to_tentative_parse (parser);
3146 /* Try to lookup the identifier. */
3147 decl = cp_parser_lookup_name (parser, id, none_type,
3148 /*is_template=*/false,
3149 /*is_namespace=*/false,
3150 /*check_dependency=*/true,
3151 &ambiguous_decls, location);
3152 if (ambiguous_decls)
3153 /* If the lookup was ambiguous, an error will already have
3154 been issued. */
3155 return;
3156 /* If the lookup found a template-name, it means that the user forgot
3157 to specify an argument list. Emit a useful error message. */
3158 if (DECL_TYPE_TEMPLATE_P (decl))
3160 error_at (location,
3161 "invalid use of template-name %qE without an argument list",
3162 decl);
3163 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3164 inform (location, "class template argument deduction is only available "
3165 "with -std=c++1z or -std=gnu++1z");
3166 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3168 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3169 error_at (location, "invalid use of destructor %qD as a type", id);
3170 else if (TREE_CODE (decl) == TYPE_DECL)
3171 /* Something like 'unsigned A a;' */
3172 error_at (location, "invalid combination of multiple type-specifiers");
3173 else if (!parser->scope)
3175 /* Issue an error message. */
3176 const char *suggestion = NULL;
3177 if (TREE_CODE (id) == IDENTIFIER_NODE)
3178 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3179 if (suggestion)
3181 gcc_rich_location richloc (location);
3182 richloc.add_fixit_replace (suggestion);
3183 error_at_rich_loc (&richloc,
3184 "%qE does not name a type; did you mean %qs?",
3185 id, suggestion);
3187 else
3188 error_at (location, "%qE does not name a type", id);
3189 /* If we're in a template class, it's possible that the user was
3190 referring to a type from a base class. For example:
3192 template <typename T> struct A { typedef T X; };
3193 template <typename T> struct B : public A<T> { X x; };
3195 The user should have said "typename A<T>::X". */
3196 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3197 inform (location, "C++11 %<constexpr%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3200 inform (location, "C++11 %<noexcept%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11
3203 && TREE_CODE (id) == IDENTIFIER_NODE
3204 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3205 inform (location, "C++11 %<thread_local%> only available with "
3206 "-std=c++11 or -std=gnu++11");
3207 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3208 inform (location, "%<concept%> only available with -fconcepts");
3209 else if (processing_template_decl && current_class_type
3210 && TYPE_BINFO (current_class_type))
3212 tree b;
3214 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3216 b = TREE_CHAIN (b))
3218 tree base_type = BINFO_TYPE (b);
3219 if (CLASS_TYPE_P (base_type)
3220 && dependent_type_p (base_type))
3222 tree field;
3223 /* Go from a particular instantiation of the
3224 template (which will have an empty TYPE_FIELDs),
3225 to the main version. */
3226 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3227 for (field = TYPE_FIELDS (base_type);
3228 field;
3229 field = DECL_CHAIN (field))
3230 if (TREE_CODE (field) == TYPE_DECL
3231 && DECL_NAME (field) == id)
3233 inform (location,
3234 "(perhaps %<typename %T::%E%> was intended)",
3235 BINFO_TYPE (b), id);
3236 break;
3238 if (field)
3239 break;
3244 /* Here we diagnose qualified-ids where the scope is actually correct,
3245 but the identifier does not resolve to a valid type name. */
3246 else if (parser->scope != error_mark_node)
3248 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3250 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3251 error_at (location_of (id),
3252 "%qE in namespace %qE does not name a template type",
3253 id, parser->scope);
3254 else
3255 error_at (location_of (id),
3256 "%qE in namespace %qE does not name a type",
3257 id, parser->scope);
3258 if (DECL_P (decl))
3259 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3261 else if (CLASS_TYPE_P (parser->scope)
3262 && constructor_name_p (id, parser->scope))
3264 /* A<T>::A<T>() */
3265 error_at (location, "%<%T::%E%> names the constructor, not"
3266 " the type", parser->scope, id);
3267 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3268 error_at (location, "and %qT has no template constructors",
3269 parser->scope);
3271 else if (TYPE_P (parser->scope)
3272 && dependent_scope_p (parser->scope))
3273 error_at (location, "need %<typename%> before %<%T::%E%> because "
3274 "%qT is a dependent scope",
3275 parser->scope, id, parser->scope);
3276 else if (TYPE_P (parser->scope))
3278 if (!COMPLETE_TYPE_P (parser->scope))
3279 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3280 parser->scope);
3281 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3282 error_at (location_of (id),
3283 "%qE in %q#T does not name a template type",
3284 id, parser->scope);
3285 else
3286 error_at (location_of (id),
3287 "%qE in %q#T does not name a type",
3288 id, parser->scope);
3289 if (DECL_P (decl))
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 else
3293 gcc_unreachable ();
3297 /* Check for a common situation where a type-name should be present,
3298 but is not, and issue a sensible error message. Returns true if an
3299 invalid type-name was detected.
3301 The situation handled by this function are variable declarations of the
3302 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3303 Usually, `ID' should name a type, but if we got here it means that it
3304 does not. We try to emit the best possible error message depending on
3305 how exactly the id-expression looks like. */
3307 static bool
3308 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3310 tree id;
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 /* Avoid duplicate error about ambiguous lookup. */
3314 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3316 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3317 if (next->type == CPP_NAME && next->error_reported)
3318 goto out;
3321 cp_parser_parse_tentatively (parser);
3322 id = cp_parser_id_expression (parser,
3323 /*template_keyword_p=*/false,
3324 /*check_dependency_p=*/true,
3325 /*template_p=*/NULL,
3326 /*declarator_p=*/true,
3327 /*optional_p=*/false);
3328 /* If the next token is a (, this is a function with no explicit return
3329 type, i.e. constructor, destructor or conversion op. */
3330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3331 || TREE_CODE (id) == TYPE_DECL)
3333 cp_parser_abort_tentative_parse (parser);
3334 return false;
3336 if (!cp_parser_parse_definitely (parser))
3337 return false;
3339 /* Emit a diagnostic for the invalid type. */
3340 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3341 out:
3342 /* If we aren't in the middle of a declarator (i.e. in a
3343 parameter-declaration-clause), skip to the end of the declaration;
3344 there's no point in trying to process it. */
3345 if (!parser->in_declarator_p)
3346 cp_parser_skip_to_end_of_block_or_statement (parser);
3347 return true;
3350 /* Consume tokens up to, and including, the next non-nested closing `)'.
3351 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3352 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3353 found an unnested token of that type. */
3355 static int
3356 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3357 bool recovering,
3358 cpp_ttype or_ttype,
3359 bool consume_paren)
3361 unsigned paren_depth = 0;
3362 unsigned brace_depth = 0;
3363 unsigned square_depth = 0;
3365 if (recovering && or_ttype == CPP_EOF
3366 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3367 return 0;
3369 while (true)
3371 cp_token * token = cp_lexer_peek_token (parser->lexer);
3373 /* Have we found what we're looking for before the closing paren? */
3374 if (token->type == or_ttype && or_ttype != CPP_EOF
3375 && !brace_depth && !paren_depth && !square_depth)
3376 return -1;
3378 switch (token->type)
3380 case CPP_EOF:
3381 case CPP_PRAGMA_EOL:
3382 /* If we've run out of tokens, then there is no closing `)'. */
3383 return 0;
3385 /* This is good for lambda expression capture-lists. */
3386 case CPP_OPEN_SQUARE:
3387 ++square_depth;
3388 break;
3389 case CPP_CLOSE_SQUARE:
3390 if (!square_depth--)
3391 return 0;
3392 break;
3394 case CPP_SEMICOLON:
3395 /* This matches the processing in skip_to_end_of_statement. */
3396 if (!brace_depth)
3397 return 0;
3398 break;
3400 case CPP_OPEN_BRACE:
3401 ++brace_depth;
3402 break;
3403 case CPP_CLOSE_BRACE:
3404 if (!brace_depth--)
3405 return 0;
3406 break;
3408 case CPP_OPEN_PAREN:
3409 if (!brace_depth)
3410 ++paren_depth;
3411 break;
3413 case CPP_CLOSE_PAREN:
3414 if (!brace_depth && !paren_depth--)
3416 if (consume_paren)
3417 cp_lexer_consume_token (parser->lexer);
3418 return 1;
3420 break;
3422 default:
3423 break;
3426 /* Consume the token. */
3427 cp_lexer_consume_token (parser->lexer);
3431 /* Consume tokens up to, and including, the next non-nested closing `)'.
3432 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3433 are doing error recovery. Returns -1 if OR_COMMA is true and we
3434 found an unnested token of that type. */
3436 static int
3437 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3438 bool recovering,
3439 bool or_comma,
3440 bool consume_paren)
3442 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3443 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3444 ttype, consume_paren);
3447 /* Consume tokens until we reach the end of the current statement.
3448 Normally, that will be just before consuming a `;'. However, if a
3449 non-nested `}' comes first, then we stop before consuming that. */
3451 static void
3452 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3454 unsigned nesting_depth = 0;
3456 /* Unwind generic function template scope if necessary. */
3457 if (parser->fully_implicit_function_template_p)
3458 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3460 while (true)
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3464 switch (token->type)
3466 case CPP_EOF:
3467 case CPP_PRAGMA_EOL:
3468 /* If we've run out of tokens, stop. */
3469 return;
3471 case CPP_SEMICOLON:
3472 /* If the next token is a `;', we have reached the end of the
3473 statement. */
3474 if (!nesting_depth)
3475 return;
3476 break;
3478 case CPP_CLOSE_BRACE:
3479 /* If this is a non-nested '}', stop before consuming it.
3480 That way, when confronted with something like:
3482 { 3 + }
3484 we stop before consuming the closing '}', even though we
3485 have not yet reached a `;'. */
3486 if (nesting_depth == 0)
3487 return;
3489 /* If it is the closing '}' for a block that we have
3490 scanned, stop -- but only after consuming the token.
3491 That way given:
3493 void f g () { ... }
3494 typedef int I;
3496 we will stop after the body of the erroneously declared
3497 function, but before consuming the following `typedef'
3498 declaration. */
3499 if (--nesting_depth == 0)
3501 cp_lexer_consume_token (parser->lexer);
3502 return;
3504 break;
3506 case CPP_OPEN_BRACE:
3507 ++nesting_depth;
3508 break;
3510 default:
3511 break;
3514 /* Consume the token. */
3515 cp_lexer_consume_token (parser->lexer);
3519 /* This function is called at the end of a statement or declaration.
3520 If the next token is a semicolon, it is consumed; otherwise, error
3521 recovery is attempted. */
3523 static void
3524 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3526 /* Look for the trailing `;'. */
3527 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3529 /* If there is additional (erroneous) input, skip to the end of
3530 the statement. */
3531 cp_parser_skip_to_end_of_statement (parser);
3532 /* If the next token is now a `;', consume it. */
3533 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3534 cp_lexer_consume_token (parser->lexer);
3538 /* Skip tokens until we have consumed an entire block, or until we
3539 have consumed a non-nested `;'. */
3541 static void
3542 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3544 int nesting_depth = 0;
3546 /* Unwind generic function template scope if necessary. */
3547 if (parser->fully_implicit_function_template_p)
3548 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3550 while (nesting_depth >= 0)
3552 cp_token *token = cp_lexer_peek_token (parser->lexer);
3554 switch (token->type)
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, stop. */
3559 return;
3561 case CPP_SEMICOLON:
3562 /* Stop if this is an unnested ';'. */
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3567 case CPP_CLOSE_BRACE:
3568 /* Stop if this is an unnested '}', or closes the outermost
3569 nesting level. */
3570 nesting_depth--;
3571 if (nesting_depth < 0)
3572 return;
3573 if (!nesting_depth)
3574 nesting_depth = -1;
3575 break;
3577 case CPP_OPEN_BRACE:
3578 /* Nest. */
3579 nesting_depth++;
3580 break;
3582 default:
3583 break;
3586 /* Consume the token. */
3587 cp_lexer_consume_token (parser->lexer);
3591 /* Skip tokens until a non-nested closing curly brace is the next
3592 token, or there are no more tokens. Return true in the first case,
3593 false otherwise. */
3595 static bool
3596 cp_parser_skip_to_closing_brace (cp_parser *parser)
3598 unsigned nesting_depth = 0;
3600 while (true)
3602 cp_token *token = cp_lexer_peek_token (parser->lexer);
3604 switch (token->type)
3606 case CPP_EOF:
3607 case CPP_PRAGMA_EOL:
3608 /* If we've run out of tokens, stop. */
3609 return false;
3611 case CPP_CLOSE_BRACE:
3612 /* If the next token is a non-nested `}', then we have reached
3613 the end of the current block. */
3614 if (nesting_depth-- == 0)
3615 return true;
3616 break;
3618 case CPP_OPEN_BRACE:
3619 /* If it the next token is a `{', then we are entering a new
3620 block. Consume the entire block. */
3621 ++nesting_depth;
3622 break;
3624 default:
3625 break;
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3633 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3634 parameter is the PRAGMA token, allowing us to purge the entire pragma
3635 sequence. */
3637 static void
3638 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3640 cp_token *token;
3642 parser->lexer->in_pragma = false;
3645 token = cp_lexer_consume_token (parser->lexer);
3646 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3648 /* Ensure that the pragma is not parsed again. */
3649 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3652 /* Require pragma end of line, resyncing with it as necessary. The
3653 arguments are as for cp_parser_skip_to_pragma_eol. */
3655 static void
3656 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3658 parser->lexer->in_pragma = false;
3659 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3663 /* This is a simple wrapper around make_typename_type. When the id is
3664 an unresolved identifier node, we can provide a superior diagnostic
3665 using cp_parser_diagnose_invalid_type_name. */
3667 static tree
3668 cp_parser_make_typename_type (cp_parser *parser, tree id,
3669 location_t id_location)
3671 tree result;
3672 if (identifier_p (id))
3674 result = make_typename_type (parser->scope, id, typename_type,
3675 /*complain=*/tf_none);
3676 if (result == error_mark_node)
3677 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3678 return result;
3680 return make_typename_type (parser->scope, id, typename_type, tf_error);
3683 /* This is a wrapper around the
3684 make_{pointer,ptrmem,reference}_declarator functions that decides
3685 which one to call based on the CODE and CLASS_TYPE arguments. The
3686 CODE argument should be one of the values returned by
3687 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3688 appertain to the pointer or reference. */
3690 static cp_declarator *
3691 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3692 cp_cv_quals cv_qualifiers,
3693 cp_declarator *target,
3694 tree attributes)
3696 if (code == ERROR_MARK)
3697 return cp_error_declarator;
3699 if (code == INDIRECT_REF)
3700 if (class_type == NULL_TREE)
3701 return make_pointer_declarator (cv_qualifiers, target, attributes);
3702 else
3703 return make_ptrmem_declarator (cv_qualifiers, class_type,
3704 target, attributes);
3705 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3706 return make_reference_declarator (cv_qualifiers, target,
3707 false, attributes);
3708 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 true, attributes);
3711 gcc_unreachable ();
3714 /* Create a new C++ parser. */
3716 static cp_parser *
3717 cp_parser_new (void)
3719 cp_parser *parser;
3720 cp_lexer *lexer;
3721 unsigned i;
3723 /* cp_lexer_new_main is called before doing GC allocation because
3724 cp_lexer_new_main might load a PCH file. */
3725 lexer = cp_lexer_new_main ();
3727 /* Initialize the binops_by_token so that we can get the tree
3728 directly from the token. */
3729 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3730 binops_by_token[binops[i].token_type] = binops[i];
3732 parser = ggc_cleared_alloc<cp_parser> ();
3733 parser->lexer = lexer;
3734 parser->context = cp_parser_context_new (NULL);
3736 /* For now, we always accept GNU extensions. */
3737 parser->allow_gnu_extensions_p = 1;
3739 /* The `>' token is a greater-than operator, not the end of a
3740 template-id. */
3741 parser->greater_than_is_operator_p = true;
3743 parser->default_arg_ok_p = true;
3745 /* We are not parsing a constant-expression. */
3746 parser->integral_constant_expression_p = false;
3747 parser->allow_non_integral_constant_expression_p = false;
3748 parser->non_integral_constant_expression_p = false;
3750 /* Local variable names are not forbidden. */
3751 parser->local_variables_forbidden_p = false;
3753 /* We are not processing an `extern "C"' declaration. */
3754 parser->in_unbraced_linkage_specification_p = false;
3756 /* We are not processing a declarator. */
3757 parser->in_declarator_p = false;
3759 /* We are not processing a template-argument-list. */
3760 parser->in_template_argument_list_p = false;
3762 /* We are not in an iteration statement. */
3763 parser->in_statement = 0;
3765 /* We are not in a switch statement. */
3766 parser->in_switch_statement_p = false;
3768 /* We are not parsing a type-id inside an expression. */
3769 parser->in_type_id_in_expr_p = false;
3771 /* Declarations aren't implicitly extern "C". */
3772 parser->implicit_extern_c = false;
3774 /* String literals should be translated to the execution character set. */
3775 parser->translate_strings_p = true;
3777 /* We are not parsing a function body. */
3778 parser->in_function_body = false;
3780 /* We can correct until told otherwise. */
3781 parser->colon_corrects_to_scope_p = true;
3783 /* The unparsed function queue is empty. */
3784 push_unparsed_function_queues (parser);
3786 /* There are no classes being defined. */
3787 parser->num_classes_being_defined = 0;
3789 /* No template parameters apply. */
3790 parser->num_template_parameter_lists = 0;
3792 /* Special parsing data structures. */
3793 parser->omp_declare_simd = NULL;
3794 parser->cilk_simd_fn_info = NULL;
3795 parser->oacc_routine = NULL;
3797 /* Not declaring an implicit function template. */
3798 parser->auto_is_implicit_function_template_parm_p = false;
3799 parser->fully_implicit_function_template_p = false;
3800 parser->implicit_template_parms = 0;
3801 parser->implicit_template_scope = 0;
3803 /* Allow constrained-type-specifiers. */
3804 parser->prevent_constrained_type_specifiers = 0;
3806 return parser;
3809 /* Create a cp_lexer structure which will emit the tokens in CACHE
3810 and push it onto the parser's lexer stack. This is used for delayed
3811 parsing of in-class method bodies and default arguments, and should
3812 not be confused with tentative parsing. */
3813 static void
3814 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3816 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3817 lexer->next = parser->lexer;
3818 parser->lexer = lexer;
3820 /* Move the current source position to that of the first token in the
3821 new lexer. */
3822 cp_lexer_set_source_position_from_token (lexer->next_token);
3825 /* Pop the top lexer off the parser stack. This is never used for the
3826 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3827 static void
3828 cp_parser_pop_lexer (cp_parser *parser)
3830 cp_lexer *lexer = parser->lexer;
3831 parser->lexer = lexer->next;
3832 cp_lexer_destroy (lexer);
3834 /* Put the current source position back where it was before this
3835 lexer was pushed. */
3836 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3839 /* Lexical conventions [gram.lex] */
3841 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3842 identifier. */
3844 static cp_expr
3845 cp_parser_identifier (cp_parser* parser)
3847 cp_token *token;
3849 /* Look for the identifier. */
3850 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3851 /* Return the value. */
3852 if (token)
3853 return cp_expr (token->u.value, token->location);
3854 else
3855 return error_mark_node;
3858 /* Parse a sequence of adjacent string constants. Returns a
3859 TREE_STRING representing the combined, nul-terminated string
3860 constant. If TRANSLATE is true, translate the string to the
3861 execution character set. If WIDE_OK is true, a wide string is
3862 invalid here.
3864 C++98 [lex.string] says that if a narrow string literal token is
3865 adjacent to a wide string literal token, the behavior is undefined.
3866 However, C99 6.4.5p4 says that this results in a wide string literal.
3867 We follow C99 here, for consistency with the C front end.
3869 This code is largely lifted from lex_string() in c-lex.c.
3871 FUTURE: ObjC++ will need to handle @-strings here. */
3872 static cp_expr
3873 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3874 bool lookup_udlit = true)
3876 tree value;
3877 size_t count;
3878 struct obstack str_ob;
3879 cpp_string str, istr, *strs;
3880 cp_token *tok;
3881 enum cpp_ttype type, curr_type;
3882 int have_suffix_p = 0;
3883 tree string_tree;
3884 tree suffix_id = NULL_TREE;
3885 bool curr_tok_is_userdef_p = false;
3887 tok = cp_lexer_peek_token (parser->lexer);
3888 if (!cp_parser_is_string_literal (tok))
3890 cp_parser_error (parser, "expected string-literal");
3891 return error_mark_node;
3894 location_t loc = tok->location;
3896 if (cpp_userdef_string_p (tok->type))
3898 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3899 curr_type = cpp_userdef_string_remove_type (tok->type);
3900 curr_tok_is_userdef_p = true;
3902 else
3904 string_tree = tok->u.value;
3905 curr_type = tok->type;
3907 type = curr_type;
3909 /* Try to avoid the overhead of creating and destroying an obstack
3910 for the common case of just one string. */
3911 if (!cp_parser_is_string_literal
3912 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3914 cp_lexer_consume_token (parser->lexer);
3916 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3917 str.len = TREE_STRING_LENGTH (string_tree);
3918 count = 1;
3920 if (curr_tok_is_userdef_p)
3922 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3923 have_suffix_p = 1;
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3926 else
3927 curr_type = tok->type;
3929 strs = &str;
3931 else
3933 location_t last_tok_loc = tok->location;
3934 gcc_obstack_init (&str_ob);
3935 count = 0;
3939 cp_lexer_consume_token (parser->lexer);
3940 count++;
3941 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3942 str.len = TREE_STRING_LENGTH (string_tree);
3944 if (curr_tok_is_userdef_p)
3946 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3947 if (have_suffix_p == 0)
3949 suffix_id = curr_suffix_id;
3950 have_suffix_p = 1;
3952 else if (have_suffix_p == 1
3953 && curr_suffix_id != suffix_id)
3955 error ("inconsistent user-defined literal suffixes"
3956 " %qD and %qD in string literal",
3957 suffix_id, curr_suffix_id);
3958 have_suffix_p = -1;
3960 curr_type = cpp_userdef_string_remove_type (tok->type);
3962 else
3963 curr_type = tok->type;
3965 if (type != curr_type)
3967 if (type == CPP_STRING)
3968 type = curr_type;
3969 else if (curr_type != CPP_STRING)
3971 rich_location rich_loc (line_table, tok->location);
3972 rich_loc.add_range (last_tok_loc, false);
3973 error_at_rich_loc (&rich_loc,
3974 "unsupported non-standard concatenation "
3975 "of string literals");
3979 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3981 last_tok_loc = tok->location;
3983 tok = cp_lexer_peek_token (parser->lexer);
3984 if (cpp_userdef_string_p (tok->type))
3986 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3987 curr_type = cpp_userdef_string_remove_type (tok->type);
3988 curr_tok_is_userdef_p = true;
3990 else
3992 string_tree = tok->u.value;
3993 curr_type = tok->type;
3994 curr_tok_is_userdef_p = false;
3997 while (cp_parser_is_string_literal (tok));
3999 /* A string literal built by concatenation has its caret=start at
4000 the start of the initial string, and its finish at the finish of
4001 the final string literal. */
4002 loc = make_location (loc, loc, get_finish (last_tok_loc));
4004 strs = (cpp_string *) obstack_finish (&str_ob);
4007 if (type != CPP_STRING && !wide_ok)
4009 cp_parser_error (parser, "a wide string is invalid in this context");
4010 type = CPP_STRING;
4013 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4014 (parse_in, strs, count, &istr, type))
4016 value = build_string (istr.len, (const char *)istr.text);
4017 free (CONST_CAST (unsigned char *, istr.text));
4019 switch (type)
4021 default:
4022 case CPP_STRING:
4023 case CPP_UTF8STRING:
4024 TREE_TYPE (value) = char_array_type_node;
4025 break;
4026 case CPP_STRING16:
4027 TREE_TYPE (value) = char16_array_type_node;
4028 break;
4029 case CPP_STRING32:
4030 TREE_TYPE (value) = char32_array_type_node;
4031 break;
4032 case CPP_WSTRING:
4033 TREE_TYPE (value) = wchar_array_type_node;
4034 break;
4037 value = fix_string_type (value);
4039 if (have_suffix_p)
4041 tree literal = build_userdef_literal (suffix_id, value,
4042 OT_NONE, NULL_TREE);
4043 if (lookup_udlit)
4044 value = cp_parser_userdef_string_literal (literal);
4045 else
4046 value = literal;
4049 else
4050 /* cpp_interpret_string has issued an error. */
4051 value = error_mark_node;
4053 if (count > 1)
4054 obstack_free (&str_ob, 0);
4056 return cp_expr (value, loc);
4059 /* Look up a literal operator with the name and the exact arguments. */
4061 static tree
4062 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4064 tree decl, fns;
4065 decl = lookup_name (name);
4066 if (!decl || !is_overloaded_fn (decl))
4067 return error_mark_node;
4069 for (fns = decl; fns; fns = OVL_NEXT (fns))
4071 unsigned int ix;
4072 bool found = true;
4073 tree fn = OVL_CURRENT (fns);
4074 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4075 if (parmtypes != NULL_TREE)
4077 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4078 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4080 tree tparm = TREE_VALUE (parmtypes);
4081 tree targ = TREE_TYPE ((*args)[ix]);
4082 bool ptr = TYPE_PTR_P (tparm);
4083 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4084 if ((ptr || arr || !same_type_p (tparm, targ))
4085 && (!ptr || !arr
4086 || !same_type_p (TREE_TYPE (tparm),
4087 TREE_TYPE (targ))))
4088 found = false;
4090 if (found
4091 && ix == vec_safe_length (args)
4092 /* May be this should be sufficient_parms_p instead,
4093 depending on how exactly should user-defined literals
4094 work in presence of default arguments on the literal
4095 operator parameters. */
4096 && parmtypes == void_list_node)
4097 return decl;
4101 return error_mark_node;
4104 /* Parse a user-defined char constant. Returns a call to a user-defined
4105 literal operator taking the character as an argument. */
4107 static cp_expr
4108 cp_parser_userdef_char_literal (cp_parser *parser)
4110 cp_token *token = cp_lexer_consume_token (parser->lexer);
4111 tree literal = token->u.value;
4112 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4113 tree value = USERDEF_LITERAL_VALUE (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree decl, result;
4117 /* Build up a call to the user-defined operator */
4118 /* Lookup the name we got back from the id-expression. */
4119 vec<tree, va_gc> *args = make_tree_vector ();
4120 vec_safe_push (args, value);
4121 decl = lookup_literal_operator (name, args);
4122 if (!decl || decl == error_mark_node)
4124 error ("unable to find character literal operator %qD with %qT argument",
4125 name, TREE_TYPE (value));
4126 release_tree_vector (args);
4127 return error_mark_node;
4129 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4130 release_tree_vector (args);
4131 return result;
4134 /* A subroutine of cp_parser_userdef_numeric_literal to
4135 create a char... template parameter pack from a string node. */
4137 static tree
4138 make_char_string_pack (tree value)
4140 tree charvec;
4141 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4142 const char *str = TREE_STRING_POINTER (value);
4143 int i, len = TREE_STRING_LENGTH (value) - 1;
4144 tree argvec = make_tree_vec (1);
4146 /* Fill in CHARVEC with all of the parameters. */
4147 charvec = make_tree_vec (len);
4148 for (i = 0; i < len; ++i)
4149 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4151 /* Build the argument packs. */
4152 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
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 return cp_parser_trait_expr (parser, token->keyword);
5140 // C++ concepts
5141 case RID_REQUIRES:
5142 return cp_parser_requires_expression (parser);
5144 /* Objective-C++ expressions. */
5145 case RID_AT_ENCODE:
5146 case RID_AT_PROTOCOL:
5147 case RID_AT_SELECTOR:
5148 return cp_parser_objc_expression (parser);
5150 case RID_TEMPLATE:
5151 if (parser->in_function_body
5152 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5153 == CPP_LESS))
5155 error_at (token->location,
5156 "a template declaration cannot appear at block scope");
5157 cp_parser_skip_to_end_of_block_or_statement (parser);
5158 return error_mark_node;
5160 /* FALLTHRU */
5161 default:
5162 cp_parser_error (parser, "expected primary-expression");
5163 return error_mark_node;
5166 /* An id-expression can start with either an identifier, a
5167 `::' as the beginning of a qualified-id, or the "operator"
5168 keyword. */
5169 case CPP_NAME:
5170 case CPP_SCOPE:
5171 case CPP_TEMPLATE_ID:
5172 case CPP_NESTED_NAME_SPECIFIER:
5174 id_expression:
5175 cp_expr id_expression;
5176 cp_expr decl;
5177 const char *error_msg;
5178 bool template_p;
5179 bool done;
5180 cp_token *id_expr_token;
5182 /* Parse the id-expression. */
5183 id_expression
5184 = cp_parser_id_expression (parser,
5185 /*template_keyword_p=*/false,
5186 /*check_dependency_p=*/true,
5187 &template_p,
5188 /*declarator_p=*/false,
5189 /*optional_p=*/false);
5190 if (id_expression == error_mark_node)
5191 return error_mark_node;
5192 id_expr_token = token;
5193 token = cp_lexer_peek_token (parser->lexer);
5194 done = (token->type != CPP_OPEN_SQUARE
5195 && token->type != CPP_OPEN_PAREN
5196 && token->type != CPP_DOT
5197 && token->type != CPP_DEREF
5198 && token->type != CPP_PLUS_PLUS
5199 && token->type != CPP_MINUS_MINUS);
5200 /* If we have a template-id, then no further lookup is
5201 required. If the template-id was for a template-class, we
5202 will sometimes have a TYPE_DECL at this point. */
5203 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5204 || TREE_CODE (id_expression) == TYPE_DECL)
5205 decl = id_expression;
5206 /* Look up the name. */
5207 else
5209 tree ambiguous_decls;
5211 /* If we already know that this lookup is ambiguous, then
5212 we've already issued an error message; there's no reason
5213 to check again. */
5214 if (id_expr_token->type == CPP_NAME
5215 && id_expr_token->error_reported)
5217 cp_parser_simulate_error (parser);
5218 return error_mark_node;
5221 decl = cp_parser_lookup_name (parser, id_expression,
5222 none_type,
5223 template_p,
5224 /*is_namespace=*/false,
5225 /*check_dependency=*/true,
5226 &ambiguous_decls,
5227 id_expr_token->location);
5228 /* If the lookup was ambiguous, an error will already have
5229 been issued. */
5230 if (ambiguous_decls)
5231 return error_mark_node;
5233 /* In Objective-C++, we may have an Objective-C 2.0
5234 dot-syntax for classes here. */
5235 if (c_dialect_objc ()
5236 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5237 && TREE_CODE (decl) == TYPE_DECL
5238 && objc_is_class_name (decl))
5240 tree component;
5241 cp_lexer_consume_token (parser->lexer);
5242 component = cp_parser_identifier (parser);
5243 if (component == error_mark_node)
5244 return error_mark_node;
5246 tree result = objc_build_class_component_ref (id_expression,
5247 component);
5248 /* Build a location of the form:
5249 expr.component
5250 ~~~~~^~~~~~~~~
5251 with caret at the start of the component name (at
5252 input_location), ranging from the start of the id_expression
5253 to the end of the component name. */
5254 location_t combined_loc
5255 = make_location (input_location, id_expression.get_start (),
5256 get_finish (input_location));
5257 protected_set_expr_location (result, combined_loc);
5258 return result;
5261 /* In Objective-C++, an instance variable (ivar) may be preferred
5262 to whatever cp_parser_lookup_name() found.
5263 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5264 rest of c-family, we have to do a little extra work to preserve
5265 any location information in cp_expr "decl". Given that
5266 objc_lookup_ivar is implemented in "c-family" and "objc", we
5267 have a trip through the pure "tree" type, rather than cp_expr.
5268 Naively copying it back to "decl" would implicitly give the
5269 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5270 store an EXPR_LOCATION. Hence we only update "decl" (and
5271 hence its location_t) if we get back a different tree node. */
5272 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5273 id_expression);
5274 if (decl_tree != decl.get_value ())
5275 decl = cp_expr (decl_tree);
5277 /* If name lookup gives us a SCOPE_REF, then the
5278 qualifying scope was dependent. */
5279 if (TREE_CODE (decl) == SCOPE_REF)
5281 /* At this point, we do not know if DECL is a valid
5282 integral constant expression. We assume that it is
5283 in fact such an expression, so that code like:
5285 template <int N> struct A {
5286 int a[B<N>::i];
5289 is accepted. At template-instantiation time, we
5290 will check that B<N>::i is actually a constant. */
5291 return decl;
5293 /* Check to see if DECL is a local variable in a context
5294 where that is forbidden. */
5295 if (parser->local_variables_forbidden_p
5296 && local_variable_p (decl))
5298 /* It might be that we only found DECL because we are
5299 trying to be generous with pre-ISO scoping rules.
5300 For example, consider:
5302 int i;
5303 void g() {
5304 for (int i = 0; i < 10; ++i) {}
5305 extern void f(int j = i);
5308 Here, name look up will originally find the out
5309 of scope `i'. We need to issue a warning message,
5310 but then use the global `i'. */
5311 decl = check_for_out_of_scope_variable (decl);
5312 if (local_variable_p (decl))
5314 error_at (id_expr_token->location,
5315 "local variable %qD may not appear in this context",
5316 decl.get_value ());
5317 return error_mark_node;
5322 decl = (finish_id_expression
5323 (id_expression, decl, parser->scope,
5324 idk,
5325 parser->integral_constant_expression_p,
5326 parser->allow_non_integral_constant_expression_p,
5327 &parser->non_integral_constant_expression_p,
5328 template_p, done, address_p,
5329 template_arg_p,
5330 &error_msg,
5331 id_expression.get_location ()));
5332 if (error_msg)
5333 cp_parser_error (parser, error_msg);
5334 decl.set_location (id_expr_token->location);
5335 return decl;
5338 /* Anything else is an error. */
5339 default:
5340 cp_parser_error (parser, "expected primary-expression");
5341 return error_mark_node;
5345 static inline cp_expr
5346 cp_parser_primary_expression (cp_parser *parser,
5347 bool address_p,
5348 bool cast_p,
5349 bool template_arg_p,
5350 cp_id_kind *idk)
5352 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5353 /*decltype*/false, idk);
5356 /* Parse an id-expression.
5358 id-expression:
5359 unqualified-id
5360 qualified-id
5362 qualified-id:
5363 :: [opt] nested-name-specifier template [opt] unqualified-id
5364 :: identifier
5365 :: operator-function-id
5366 :: template-id
5368 Return a representation of the unqualified portion of the
5369 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5370 a `::' or nested-name-specifier.
5372 Often, if the id-expression was a qualified-id, the caller will
5373 want to make a SCOPE_REF to represent the qualified-id. This
5374 function does not do this in order to avoid wastefully creating
5375 SCOPE_REFs when they are not required.
5377 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5378 `template' keyword.
5380 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5381 uninstantiated templates.
5383 If *TEMPLATE_P is non-NULL, it is set to true iff the
5384 `template' keyword is used to explicitly indicate that the entity
5385 named is a template.
5387 If DECLARATOR_P is true, the id-expression is appearing as part of
5388 a declarator, rather than as part of an expression. */
5390 static cp_expr
5391 cp_parser_id_expression (cp_parser *parser,
5392 bool template_keyword_p,
5393 bool check_dependency_p,
5394 bool *template_p,
5395 bool declarator_p,
5396 bool optional_p)
5398 bool global_scope_p;
5399 bool nested_name_specifier_p;
5401 /* Assume the `template' keyword was not used. */
5402 if (template_p)
5403 *template_p = template_keyword_p;
5405 /* Look for the optional `::' operator. */
5406 global_scope_p
5407 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5408 != NULL_TREE);
5409 /* Look for the optional nested-name-specifier. */
5410 nested_name_specifier_p
5411 = (cp_parser_nested_name_specifier_opt (parser,
5412 /*typename_keyword_p=*/false,
5413 check_dependency_p,
5414 /*type_p=*/false,
5415 declarator_p)
5416 != NULL_TREE);
5417 /* If there is a nested-name-specifier, then we are looking at
5418 the first qualified-id production. */
5419 if (nested_name_specifier_p)
5421 tree saved_scope;
5422 tree saved_object_scope;
5423 tree saved_qualifying_scope;
5424 cp_expr unqualified_id;
5425 bool is_template;
5427 /* See if the next token is the `template' keyword. */
5428 if (!template_p)
5429 template_p = &is_template;
5430 *template_p = cp_parser_optional_template_keyword (parser);
5431 /* Name lookup we do during the processing of the
5432 unqualified-id might obliterate SCOPE. */
5433 saved_scope = parser->scope;
5434 saved_object_scope = parser->object_scope;
5435 saved_qualifying_scope = parser->qualifying_scope;
5436 /* Process the final unqualified-id. */
5437 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5438 check_dependency_p,
5439 declarator_p,
5440 /*optional_p=*/false);
5441 /* Restore the SAVED_SCOPE for our caller. */
5442 parser->scope = saved_scope;
5443 parser->object_scope = saved_object_scope;
5444 parser->qualifying_scope = saved_qualifying_scope;
5446 return unqualified_id;
5448 /* Otherwise, if we are in global scope, then we are looking at one
5449 of the other qualified-id productions. */
5450 else if (global_scope_p)
5452 cp_token *token;
5453 tree id;
5455 /* Peek at the next token. */
5456 token = cp_lexer_peek_token (parser->lexer);
5458 /* If it's an identifier, and the next token is not a "<", then
5459 we can avoid the template-id case. This is an optimization
5460 for this common case. */
5461 if (token->type == CPP_NAME
5462 && !cp_parser_nth_token_starts_template_argument_list_p
5463 (parser, 2))
5464 return cp_parser_identifier (parser);
5466 cp_parser_parse_tentatively (parser);
5467 /* Try a template-id. */
5468 id = cp_parser_template_id (parser,
5469 /*template_keyword_p=*/false,
5470 /*check_dependency_p=*/true,
5471 none_type,
5472 declarator_p);
5473 /* If that worked, we're done. */
5474 if (cp_parser_parse_definitely (parser))
5475 return id;
5477 /* Peek at the next token. (Changes in the token buffer may
5478 have invalidated the pointer obtained above.) */
5479 token = cp_lexer_peek_token (parser->lexer);
5481 switch (token->type)
5483 case CPP_NAME:
5484 return cp_parser_identifier (parser);
5486 case CPP_KEYWORD:
5487 if (token->keyword == RID_OPERATOR)
5488 return cp_parser_operator_function_id (parser);
5489 /* Fall through. */
5491 default:
5492 cp_parser_error (parser, "expected id-expression");
5493 return error_mark_node;
5496 else
5497 return cp_parser_unqualified_id (parser, template_keyword_p,
5498 /*check_dependency_p=*/true,
5499 declarator_p,
5500 optional_p);
5503 /* Parse an unqualified-id.
5505 unqualified-id:
5506 identifier
5507 operator-function-id
5508 conversion-function-id
5509 ~ class-name
5510 template-id
5512 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5513 keyword, in a construct like `A::template ...'.
5515 Returns a representation of unqualified-id. For the `identifier'
5516 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5517 production a BIT_NOT_EXPR is returned; the operand of the
5518 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5519 other productions, see the documentation accompanying the
5520 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5521 names are looked up in uninstantiated templates. If DECLARATOR_P
5522 is true, the unqualified-id is appearing as part of a declarator,
5523 rather than as part of an expression. */
5525 static cp_expr
5526 cp_parser_unqualified_id (cp_parser* parser,
5527 bool template_keyword_p,
5528 bool check_dependency_p,
5529 bool declarator_p,
5530 bool optional_p)
5532 cp_token *token;
5534 /* Peek at the next token. */
5535 token = cp_lexer_peek_token (parser->lexer);
5537 switch ((int) token->type)
5539 case CPP_NAME:
5541 tree id;
5543 /* We don't know yet whether or not this will be a
5544 template-id. */
5545 cp_parser_parse_tentatively (parser);
5546 /* Try a template-id. */
5547 id = cp_parser_template_id (parser, template_keyword_p,
5548 check_dependency_p,
5549 none_type,
5550 declarator_p);
5551 /* If it worked, we're done. */
5552 if (cp_parser_parse_definitely (parser))
5553 return id;
5554 /* Otherwise, it's an ordinary identifier. */
5555 return cp_parser_identifier (parser);
5558 case CPP_TEMPLATE_ID:
5559 return cp_parser_template_id (parser, template_keyword_p,
5560 check_dependency_p,
5561 none_type,
5562 declarator_p);
5564 case CPP_COMPL:
5566 tree type_decl;
5567 tree qualifying_scope;
5568 tree object_scope;
5569 tree scope;
5570 bool done;
5572 /* Consume the `~' token. */
5573 cp_lexer_consume_token (parser->lexer);
5574 /* Parse the class-name. The standard, as written, seems to
5575 say that:
5577 template <typename T> struct S { ~S (); };
5578 template <typename T> S<T>::~S() {}
5580 is invalid, since `~' must be followed by a class-name, but
5581 `S<T>' is dependent, and so not known to be a class.
5582 That's not right; we need to look in uninstantiated
5583 templates. A further complication arises from:
5585 template <typename T> void f(T t) {
5586 t.T::~T();
5589 Here, it is not possible to look up `T' in the scope of `T'
5590 itself. We must look in both the current scope, and the
5591 scope of the containing complete expression.
5593 Yet another issue is:
5595 struct S {
5596 int S;
5597 ~S();
5600 S::~S() {}
5602 The standard does not seem to say that the `S' in `~S'
5603 should refer to the type `S' and not the data member
5604 `S::S'. */
5606 /* DR 244 says that we look up the name after the "~" in the
5607 same scope as we looked up the qualifying name. That idea
5608 isn't fully worked out; it's more complicated than that. */
5609 scope = parser->scope;
5610 object_scope = parser->object_scope;
5611 qualifying_scope = parser->qualifying_scope;
5613 /* Check for invalid scopes. */
5614 if (scope == error_mark_node)
5616 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5617 cp_lexer_consume_token (parser->lexer);
5618 return error_mark_node;
5620 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5622 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5623 error_at (token->location,
5624 "scope %qT before %<~%> is not a class-name",
5625 scope);
5626 cp_parser_simulate_error (parser);
5627 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5628 cp_lexer_consume_token (parser->lexer);
5629 return error_mark_node;
5631 gcc_assert (!scope || TYPE_P (scope));
5633 /* If the name is of the form "X::~X" it's OK even if X is a
5634 typedef. */
5635 token = cp_lexer_peek_token (parser->lexer);
5636 if (scope
5637 && token->type == CPP_NAME
5638 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5639 != CPP_LESS)
5640 && (token->u.value == TYPE_IDENTIFIER (scope)
5641 || (CLASS_TYPE_P (scope)
5642 && constructor_name_p (token->u.value, scope))))
5644 cp_lexer_consume_token (parser->lexer);
5645 return build_nt (BIT_NOT_EXPR, scope);
5648 /* ~auto means the destructor of whatever the object is. */
5649 if (cp_parser_is_keyword (token, RID_AUTO))
5651 if (cxx_dialect < cxx14)
5652 pedwarn (input_location, 0,
5653 "%<~auto%> only available with "
5654 "-std=c++14 or -std=gnu++14");
5655 cp_lexer_consume_token (parser->lexer);
5656 return build_nt (BIT_NOT_EXPR, make_auto ());
5659 /* If there was an explicit qualification (S::~T), first look
5660 in the scope given by the qualification (i.e., S).
5662 Note: in the calls to cp_parser_class_name below we pass
5663 typename_type so that lookup finds the injected-class-name
5664 rather than the constructor. */
5665 done = false;
5666 type_decl = NULL_TREE;
5667 if (scope)
5669 cp_parser_parse_tentatively (parser);
5670 type_decl = cp_parser_class_name (parser,
5671 /*typename_keyword_p=*/false,
5672 /*template_keyword_p=*/false,
5673 typename_type,
5674 /*check_dependency=*/false,
5675 /*class_head_p=*/false,
5676 declarator_p);
5677 if (cp_parser_parse_definitely (parser))
5678 done = true;
5680 /* In "N::S::~S", look in "N" as well. */
5681 if (!done && scope && qualifying_scope)
5683 cp_parser_parse_tentatively (parser);
5684 parser->scope = qualifying_scope;
5685 parser->object_scope = NULL_TREE;
5686 parser->qualifying_scope = NULL_TREE;
5687 type_decl
5688 = cp_parser_class_name (parser,
5689 /*typename_keyword_p=*/false,
5690 /*template_keyword_p=*/false,
5691 typename_type,
5692 /*check_dependency=*/false,
5693 /*class_head_p=*/false,
5694 declarator_p);
5695 if (cp_parser_parse_definitely (parser))
5696 done = true;
5698 /* In "p->S::~T", look in the scope given by "*p" as well. */
5699 else if (!done && object_scope)
5701 cp_parser_parse_tentatively (parser);
5702 parser->scope = object_scope;
5703 parser->object_scope = NULL_TREE;
5704 parser->qualifying_scope = NULL_TREE;
5705 type_decl
5706 = cp_parser_class_name (parser,
5707 /*typename_keyword_p=*/false,
5708 /*template_keyword_p=*/false,
5709 typename_type,
5710 /*check_dependency=*/false,
5711 /*class_head_p=*/false,
5712 declarator_p);
5713 if (cp_parser_parse_definitely (parser))
5714 done = true;
5716 /* Look in the surrounding context. */
5717 if (!done)
5719 parser->scope = NULL_TREE;
5720 parser->object_scope = NULL_TREE;
5721 parser->qualifying_scope = NULL_TREE;
5722 if (processing_template_decl)
5723 cp_parser_parse_tentatively (parser);
5724 type_decl
5725 = cp_parser_class_name (parser,
5726 /*typename_keyword_p=*/false,
5727 /*template_keyword_p=*/false,
5728 typename_type,
5729 /*check_dependency=*/false,
5730 /*class_head_p=*/false,
5731 declarator_p);
5732 if (processing_template_decl
5733 && ! cp_parser_parse_definitely (parser))
5735 /* We couldn't find a type with this name. If we're parsing
5736 tentatively, fail and try something else. */
5737 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5739 cp_parser_simulate_error (parser);
5740 return error_mark_node;
5742 /* Otherwise, accept it and check for a match at instantiation
5743 time. */
5744 type_decl = cp_parser_identifier (parser);
5745 if (type_decl != error_mark_node)
5746 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5747 return type_decl;
5750 /* If an error occurred, assume that the name of the
5751 destructor is the same as the name of the qualifying
5752 class. That allows us to keep parsing after running
5753 into ill-formed destructor names. */
5754 if (type_decl == error_mark_node && scope)
5755 return build_nt (BIT_NOT_EXPR, scope);
5756 else if (type_decl == error_mark_node)
5757 return error_mark_node;
5759 /* Check that destructor name and scope match. */
5760 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5762 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5763 error_at (token->location,
5764 "declaration of %<~%T%> as member of %qT",
5765 type_decl, scope);
5766 cp_parser_simulate_error (parser);
5767 return error_mark_node;
5770 /* [class.dtor]
5772 A typedef-name that names a class shall not be used as the
5773 identifier in the declarator for a destructor declaration. */
5774 if (declarator_p
5775 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5776 && !DECL_SELF_REFERENCE_P (type_decl)
5777 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5778 error_at (token->location,
5779 "typedef-name %qD used as destructor declarator",
5780 type_decl);
5782 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5785 case CPP_KEYWORD:
5786 if (token->keyword == RID_OPERATOR)
5788 cp_expr id;
5790 /* This could be a template-id, so we try that first. */
5791 cp_parser_parse_tentatively (parser);
5792 /* Try a template-id. */
5793 id = cp_parser_template_id (parser, template_keyword_p,
5794 /*check_dependency_p=*/true,
5795 none_type,
5796 declarator_p);
5797 /* If that worked, we're done. */
5798 if (cp_parser_parse_definitely (parser))
5799 return id;
5800 /* We still don't know whether we're looking at an
5801 operator-function-id or a conversion-function-id. */
5802 cp_parser_parse_tentatively (parser);
5803 /* Try an operator-function-id. */
5804 id = cp_parser_operator_function_id (parser);
5805 /* If that didn't work, try a conversion-function-id. */
5806 if (!cp_parser_parse_definitely (parser))
5807 id = cp_parser_conversion_function_id (parser);
5808 else if (UDLIT_OPER_P (id))
5810 /* 17.6.3.3.5 */
5811 const char *name = UDLIT_OP_SUFFIX (id);
5812 if (name[0] != '_' && !in_system_header_at (input_location)
5813 && declarator_p)
5814 warning (OPT_Wliteral_suffix,
5815 "literal operator suffixes not preceded by %<_%>"
5816 " are reserved for future standardization");
5819 return id;
5821 /* Fall through. */
5823 default:
5824 if (optional_p)
5825 return NULL_TREE;
5826 cp_parser_error (parser, "expected unqualified-id");
5827 return error_mark_node;
5831 /* Parse an (optional) nested-name-specifier.
5833 nested-name-specifier: [C++98]
5834 class-or-namespace-name :: nested-name-specifier [opt]
5835 class-or-namespace-name :: template nested-name-specifier [opt]
5837 nested-name-specifier: [C++0x]
5838 type-name ::
5839 namespace-name ::
5840 nested-name-specifier identifier ::
5841 nested-name-specifier template [opt] simple-template-id ::
5843 PARSER->SCOPE should be set appropriately before this function is
5844 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5845 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5846 in name lookups.
5848 Sets PARSER->SCOPE to the class (TYPE) or namespace
5849 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5850 it unchanged if there is no nested-name-specifier. Returns the new
5851 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5853 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5854 part of a declaration and/or decl-specifier. */
5856 static tree
5857 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5858 bool typename_keyword_p,
5859 bool check_dependency_p,
5860 bool type_p,
5861 bool is_declaration)
5863 bool success = false;
5864 cp_token_position start = 0;
5865 cp_token *token;
5867 /* Remember where the nested-name-specifier starts. */
5868 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5870 start = cp_lexer_token_position (parser->lexer, false);
5871 push_deferring_access_checks (dk_deferred);
5874 while (true)
5876 tree new_scope;
5877 tree old_scope;
5878 tree saved_qualifying_scope;
5879 bool template_keyword_p;
5881 /* Spot cases that cannot be the beginning of a
5882 nested-name-specifier. */
5883 token = cp_lexer_peek_token (parser->lexer);
5885 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5886 the already parsed nested-name-specifier. */
5887 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5889 /* Grab the nested-name-specifier and continue the loop. */
5890 cp_parser_pre_parsed_nested_name_specifier (parser);
5891 /* If we originally encountered this nested-name-specifier
5892 with IS_DECLARATION set to false, we will not have
5893 resolved TYPENAME_TYPEs, so we must do so here. */
5894 if (is_declaration
5895 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5897 new_scope = resolve_typename_type (parser->scope,
5898 /*only_current_p=*/false);
5899 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5900 parser->scope = new_scope;
5902 success = true;
5903 continue;
5906 /* Spot cases that cannot be the beginning of a
5907 nested-name-specifier. On the second and subsequent times
5908 through the loop, we look for the `template' keyword. */
5909 if (success && token->keyword == RID_TEMPLATE)
5911 /* A template-id can start a nested-name-specifier. */
5912 else if (token->type == CPP_TEMPLATE_ID)
5914 /* DR 743: decltype can be used in a nested-name-specifier. */
5915 else if (token_is_decltype (token))
5917 else
5919 /* If the next token is not an identifier, then it is
5920 definitely not a type-name or namespace-name. */
5921 if (token->type != CPP_NAME)
5922 break;
5923 /* If the following token is neither a `<' (to begin a
5924 template-id), nor a `::', then we are not looking at a
5925 nested-name-specifier. */
5926 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5928 if (token->type == CPP_COLON
5929 && parser->colon_corrects_to_scope_p
5930 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5932 gcc_rich_location richloc (token->location);
5933 richloc.add_fixit_replace ("::");
5934 error_at_rich_loc (&richloc,
5935 "found %<:%> in nested-name-specifier, "
5936 "expected %<::%>");
5937 token->type = CPP_SCOPE;
5940 if (token->type != CPP_SCOPE
5941 && !cp_parser_nth_token_starts_template_argument_list_p
5942 (parser, 2))
5943 break;
5946 /* The nested-name-specifier is optional, so we parse
5947 tentatively. */
5948 cp_parser_parse_tentatively (parser);
5950 /* Look for the optional `template' keyword, if this isn't the
5951 first time through the loop. */
5952 if (success)
5953 template_keyword_p = cp_parser_optional_template_keyword (parser);
5954 else
5955 template_keyword_p = false;
5957 /* Save the old scope since the name lookup we are about to do
5958 might destroy it. */
5959 old_scope = parser->scope;
5960 saved_qualifying_scope = parser->qualifying_scope;
5961 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5962 look up names in "X<T>::I" in order to determine that "Y" is
5963 a template. So, if we have a typename at this point, we make
5964 an effort to look through it. */
5965 if (is_declaration
5966 && !typename_keyword_p
5967 && parser->scope
5968 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5969 parser->scope = resolve_typename_type (parser->scope,
5970 /*only_current_p=*/false);
5971 /* Parse the qualifying entity. */
5972 new_scope
5973 = cp_parser_qualifying_entity (parser,
5974 typename_keyword_p,
5975 template_keyword_p,
5976 check_dependency_p,
5977 type_p,
5978 is_declaration);
5979 /* Look for the `::' token. */
5980 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5982 /* If we found what we wanted, we keep going; otherwise, we're
5983 done. */
5984 if (!cp_parser_parse_definitely (parser))
5986 bool error_p = false;
5988 /* Restore the OLD_SCOPE since it was valid before the
5989 failed attempt at finding the last
5990 class-or-namespace-name. */
5991 parser->scope = old_scope;
5992 parser->qualifying_scope = saved_qualifying_scope;
5994 /* If the next token is a decltype, and the one after that is a
5995 `::', then the decltype has failed to resolve to a class or
5996 enumeration type. Give this error even when parsing
5997 tentatively since it can't possibly be valid--and we're going
5998 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5999 won't get another chance.*/
6000 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6001 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6002 == CPP_SCOPE))
6004 token = cp_lexer_consume_token (parser->lexer);
6005 error_at (token->location, "decltype evaluates to %qT, "
6006 "which is not a class or enumeration type",
6007 token->u.tree_check_value->value);
6008 parser->scope = error_mark_node;
6009 error_p = true;
6010 /* As below. */
6011 success = true;
6012 cp_lexer_consume_token (parser->lexer);
6015 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6016 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6018 /* If we have a non-type template-id followed by ::, it can't
6019 possibly be valid. */
6020 token = cp_lexer_peek_token (parser->lexer);
6021 tree tid = token->u.tree_check_value->value;
6022 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6023 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6025 tree tmpl = NULL_TREE;
6026 if (is_overloaded_fn (tid))
6028 tree fns = get_fns (tid);
6029 if (!OVL_CHAIN (fns))
6030 tmpl = OVL_CURRENT (fns);
6031 error_at (token->location, "function template-id %qD "
6032 "in nested-name-specifier", tid);
6034 else
6036 /* Variable template. */
6037 tmpl = TREE_OPERAND (tid, 0);
6038 gcc_assert (variable_template_p (tmpl));
6039 error_at (token->location, "variable template-id %qD "
6040 "in nested-name-specifier", tid);
6042 if (tmpl)
6043 inform (DECL_SOURCE_LOCATION (tmpl),
6044 "%qD declared here", tmpl);
6046 parser->scope = error_mark_node;
6047 error_p = true;
6048 /* As below. */
6049 success = true;
6050 cp_lexer_consume_token (parser->lexer);
6051 cp_lexer_consume_token (parser->lexer);
6055 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6056 break;
6057 /* If the next token is an identifier, and the one after
6058 that is a `::', then any valid interpretation would have
6059 found a class-or-namespace-name. */
6060 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6061 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6062 == CPP_SCOPE)
6063 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6064 != CPP_COMPL))
6066 token = cp_lexer_consume_token (parser->lexer);
6067 if (!error_p)
6069 if (!token->error_reported)
6071 tree decl;
6072 tree ambiguous_decls;
6074 decl = cp_parser_lookup_name (parser, token->u.value,
6075 none_type,
6076 /*is_template=*/false,
6077 /*is_namespace=*/false,
6078 /*check_dependency=*/true,
6079 &ambiguous_decls,
6080 token->location);
6081 if (TREE_CODE (decl) == TEMPLATE_DECL)
6082 error_at (token->location,
6083 "%qD used without template parameters",
6084 decl);
6085 else if (ambiguous_decls)
6087 // cp_parser_lookup_name has the same diagnostic,
6088 // thus make sure to emit it at most once.
6089 if (cp_parser_uncommitted_to_tentative_parse_p
6090 (parser))
6092 error_at (token->location,
6093 "reference to %qD is ambiguous",
6094 token->u.value);
6095 print_candidates (ambiguous_decls);
6097 decl = error_mark_node;
6099 else
6101 if (cxx_dialect != cxx98)
6102 cp_parser_name_lookup_error
6103 (parser, token->u.value, decl, NLE_NOT_CXX98,
6104 token->location);
6105 else
6106 cp_parser_name_lookup_error
6107 (parser, token->u.value, decl, NLE_CXX98,
6108 token->location);
6111 parser->scope = error_mark_node;
6112 error_p = true;
6113 /* Treat this as a successful nested-name-specifier
6114 due to:
6116 [basic.lookup.qual]
6118 If the name found is not a class-name (clause
6119 _class_) or namespace-name (_namespace.def_), the
6120 program is ill-formed. */
6121 success = true;
6123 cp_lexer_consume_token (parser->lexer);
6125 break;
6127 /* We've found one valid nested-name-specifier. */
6128 success = true;
6129 /* Name lookup always gives us a DECL. */
6130 if (TREE_CODE (new_scope) == TYPE_DECL)
6131 new_scope = TREE_TYPE (new_scope);
6132 /* Uses of "template" must be followed by actual templates. */
6133 if (template_keyword_p
6134 && !(CLASS_TYPE_P (new_scope)
6135 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6136 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6137 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6138 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6139 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6140 == TEMPLATE_ID_EXPR)))
6141 permerror (input_location, TYPE_P (new_scope)
6142 ? G_("%qT is not a template")
6143 : G_("%qD is not a template"),
6144 new_scope);
6145 /* If it is a class scope, try to complete it; we are about to
6146 be looking up names inside the class. */
6147 if (TYPE_P (new_scope)
6148 /* Since checking types for dependency can be expensive,
6149 avoid doing it if the type is already complete. */
6150 && !COMPLETE_TYPE_P (new_scope)
6151 /* Do not try to complete dependent types. */
6152 && !dependent_type_p (new_scope))
6154 new_scope = complete_type (new_scope);
6155 /* If it is a typedef to current class, use the current
6156 class instead, as the typedef won't have any names inside
6157 it yet. */
6158 if (!COMPLETE_TYPE_P (new_scope)
6159 && currently_open_class (new_scope))
6160 new_scope = TYPE_MAIN_VARIANT (new_scope);
6162 /* Make sure we look in the right scope the next time through
6163 the loop. */
6164 parser->scope = new_scope;
6167 /* If parsing tentatively, replace the sequence of tokens that makes
6168 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6169 token. That way, should we re-parse the token stream, we will
6170 not have to repeat the effort required to do the parse, nor will
6171 we issue duplicate error messages. */
6172 if (success && start)
6174 cp_token *token;
6176 token = cp_lexer_token_at (parser->lexer, start);
6177 /* Reset the contents of the START token. */
6178 token->type = CPP_NESTED_NAME_SPECIFIER;
6179 /* Retrieve any deferred checks. Do not pop this access checks yet
6180 so the memory will not be reclaimed during token replacing below. */
6181 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6182 token->u.tree_check_value->value = parser->scope;
6183 token->u.tree_check_value->checks = get_deferred_access_checks ();
6184 token->u.tree_check_value->qualifying_scope =
6185 parser->qualifying_scope;
6186 token->keyword = RID_MAX;
6188 /* Purge all subsequent tokens. */
6189 cp_lexer_purge_tokens_after (parser->lexer, start);
6192 if (start)
6193 pop_to_parent_deferring_access_checks ();
6195 return success ? parser->scope : NULL_TREE;
6198 /* Parse a nested-name-specifier. See
6199 cp_parser_nested_name_specifier_opt for details. This function
6200 behaves identically, except that it will an issue an error if no
6201 nested-name-specifier is present. */
6203 static tree
6204 cp_parser_nested_name_specifier (cp_parser *parser,
6205 bool typename_keyword_p,
6206 bool check_dependency_p,
6207 bool type_p,
6208 bool is_declaration)
6210 tree scope;
6212 /* Look for the nested-name-specifier. */
6213 scope = cp_parser_nested_name_specifier_opt (parser,
6214 typename_keyword_p,
6215 check_dependency_p,
6216 type_p,
6217 is_declaration);
6218 /* If it was not present, issue an error message. */
6219 if (!scope)
6221 cp_parser_error (parser, "expected nested-name-specifier");
6222 parser->scope = NULL_TREE;
6225 return scope;
6228 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6229 this is either a class-name or a namespace-name (which corresponds
6230 to the class-or-namespace-name production in the grammar). For
6231 C++0x, it can also be a type-name that refers to an enumeration
6232 type or a simple-template-id.
6234 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6235 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6236 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6237 TYPE_P is TRUE iff the next name should be taken as a class-name,
6238 even the same name is declared to be another entity in the same
6239 scope.
6241 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6242 specified by the class-or-namespace-name. If neither is found the
6243 ERROR_MARK_NODE is returned. */
6245 static tree
6246 cp_parser_qualifying_entity (cp_parser *parser,
6247 bool typename_keyword_p,
6248 bool template_keyword_p,
6249 bool check_dependency_p,
6250 bool type_p,
6251 bool is_declaration)
6253 tree saved_scope;
6254 tree saved_qualifying_scope;
6255 tree saved_object_scope;
6256 tree scope;
6257 bool only_class_p;
6258 bool successful_parse_p;
6260 /* DR 743: decltype can appear in a nested-name-specifier. */
6261 if (cp_lexer_next_token_is_decltype (parser->lexer))
6263 scope = cp_parser_decltype (parser);
6264 if (TREE_CODE (scope) != ENUMERAL_TYPE
6265 && !MAYBE_CLASS_TYPE_P (scope))
6267 cp_parser_simulate_error (parser);
6268 return error_mark_node;
6270 if (TYPE_NAME (scope))
6271 scope = TYPE_NAME (scope);
6272 return scope;
6275 /* Before we try to parse the class-name, we must save away the
6276 current PARSER->SCOPE since cp_parser_class_name will destroy
6277 it. */
6278 saved_scope = parser->scope;
6279 saved_qualifying_scope = parser->qualifying_scope;
6280 saved_object_scope = parser->object_scope;
6281 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6282 there is no need to look for a namespace-name. */
6283 only_class_p = template_keyword_p
6284 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6285 if (!only_class_p)
6286 cp_parser_parse_tentatively (parser);
6287 scope = cp_parser_class_name (parser,
6288 typename_keyword_p,
6289 template_keyword_p,
6290 type_p ? class_type : none_type,
6291 check_dependency_p,
6292 /*class_head_p=*/false,
6293 is_declaration,
6294 /*enum_ok=*/cxx_dialect > cxx98);
6295 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6296 /* If that didn't work, try for a namespace-name. */
6297 if (!only_class_p && !successful_parse_p)
6299 /* Restore the saved scope. */
6300 parser->scope = saved_scope;
6301 parser->qualifying_scope = saved_qualifying_scope;
6302 parser->object_scope = saved_object_scope;
6303 /* If we are not looking at an identifier followed by the scope
6304 resolution operator, then this is not part of a
6305 nested-name-specifier. (Note that this function is only used
6306 to parse the components of a nested-name-specifier.) */
6307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6308 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6309 return error_mark_node;
6310 scope = cp_parser_namespace_name (parser);
6313 return scope;
6316 /* Return true if we are looking at a compound-literal, false otherwise. */
6318 static bool
6319 cp_parser_compound_literal_p (cp_parser *parser)
6321 /* Consume the `('. */
6322 cp_lexer_consume_token (parser->lexer);
6324 cp_lexer_save_tokens (parser->lexer);
6326 /* Skip tokens until the next token is a closing parenthesis.
6327 If we find the closing `)', and the next token is a `{', then
6328 we are looking at a compound-literal. */
6329 bool compound_literal_p
6330 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6331 /*consume_paren=*/true)
6332 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6334 /* Roll back the tokens we skipped. */
6335 cp_lexer_rollback_tokens (parser->lexer);
6337 return compound_literal_p;
6340 /* Parse a postfix-expression.
6342 postfix-expression:
6343 primary-expression
6344 postfix-expression [ expression ]
6345 postfix-expression ( expression-list [opt] )
6346 simple-type-specifier ( expression-list [opt] )
6347 typename :: [opt] nested-name-specifier identifier
6348 ( expression-list [opt] )
6349 typename :: [opt] nested-name-specifier template [opt] template-id
6350 ( expression-list [opt] )
6351 postfix-expression . template [opt] id-expression
6352 postfix-expression -> template [opt] id-expression
6353 postfix-expression . pseudo-destructor-name
6354 postfix-expression -> pseudo-destructor-name
6355 postfix-expression ++
6356 postfix-expression --
6357 dynamic_cast < type-id > ( expression )
6358 static_cast < type-id > ( expression )
6359 reinterpret_cast < type-id > ( expression )
6360 const_cast < type-id > ( expression )
6361 typeid ( expression )
6362 typeid ( type-id )
6364 GNU Extension:
6366 postfix-expression:
6367 ( type-id ) { initializer-list , [opt] }
6369 This extension is a GNU version of the C99 compound-literal
6370 construct. (The C99 grammar uses `type-name' instead of `type-id',
6371 but they are essentially the same concept.)
6373 If ADDRESS_P is true, the postfix expression is the operand of the
6374 `&' operator. CAST_P is true if this expression is the target of a
6375 cast.
6377 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6378 class member access expressions [expr.ref].
6380 Returns a representation of the expression. */
6382 static cp_expr
6383 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6384 bool member_access_only_p, bool decltype_p,
6385 cp_id_kind * pidk_return)
6387 cp_token *token;
6388 location_t loc;
6389 enum rid keyword;
6390 cp_id_kind idk = CP_ID_KIND_NONE;
6391 cp_expr postfix_expression = NULL_TREE;
6392 bool is_member_access = false;
6393 int saved_in_statement = -1;
6395 /* Peek at the next token. */
6396 token = cp_lexer_peek_token (parser->lexer);
6397 loc = token->location;
6398 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6400 /* Some of the productions are determined by keywords. */
6401 keyword = token->keyword;
6402 switch (keyword)
6404 case RID_DYNCAST:
6405 case RID_STATCAST:
6406 case RID_REINTCAST:
6407 case RID_CONSTCAST:
6409 tree type;
6410 cp_expr expression;
6411 const char *saved_message;
6412 bool saved_in_type_id_in_expr_p;
6414 /* All of these can be handled in the same way from the point
6415 of view of parsing. Begin by consuming the token
6416 identifying the cast. */
6417 cp_lexer_consume_token (parser->lexer);
6419 /* New types cannot be defined in the cast. */
6420 saved_message = parser->type_definition_forbidden_message;
6421 parser->type_definition_forbidden_message
6422 = G_("types may not be defined in casts");
6424 /* Look for the opening `<'. */
6425 cp_parser_require (parser, CPP_LESS, RT_LESS);
6426 /* Parse the type to which we are casting. */
6427 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6428 parser->in_type_id_in_expr_p = true;
6429 type = cp_parser_type_id (parser);
6430 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6431 /* Look for the closing `>'. */
6432 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6433 /* Restore the old message. */
6434 parser->type_definition_forbidden_message = saved_message;
6436 bool saved_greater_than_is_operator_p
6437 = parser->greater_than_is_operator_p;
6438 parser->greater_than_is_operator_p = true;
6440 /* And the expression which is being cast. */
6441 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6442 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6443 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6444 RT_CLOSE_PAREN);
6445 location_t end_loc = close_paren ?
6446 close_paren->location : UNKNOWN_LOCATION;
6448 parser->greater_than_is_operator_p
6449 = saved_greater_than_is_operator_p;
6451 /* Only type conversions to integral or enumeration types
6452 can be used in constant-expressions. */
6453 if (!cast_valid_in_integral_constant_expression_p (type)
6454 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6456 postfix_expression = error_mark_node;
6457 break;
6460 switch (keyword)
6462 case RID_DYNCAST:
6463 postfix_expression
6464 = build_dynamic_cast (type, expression, tf_warning_or_error);
6465 break;
6466 case RID_STATCAST:
6467 postfix_expression
6468 = build_static_cast (type, expression, tf_warning_or_error);
6469 break;
6470 case RID_REINTCAST:
6471 postfix_expression
6472 = build_reinterpret_cast (type, expression,
6473 tf_warning_or_error);
6474 break;
6475 case RID_CONSTCAST:
6476 postfix_expression
6477 = build_const_cast (type, expression, tf_warning_or_error);
6478 break;
6479 default:
6480 gcc_unreachable ();
6483 /* Construct a location e.g. :
6484 reinterpret_cast <int *> (expr)
6485 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6486 ranging from the start of the "*_cast" token to the final closing
6487 paren, with the caret at the start. */
6488 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6489 postfix_expression.set_location (cp_cast_loc);
6491 break;
6493 case RID_TYPEID:
6495 tree type;
6496 const char *saved_message;
6497 bool saved_in_type_id_in_expr_p;
6499 /* Consume the `typeid' token. */
6500 cp_lexer_consume_token (parser->lexer);
6501 /* Look for the `(' token. */
6502 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6503 /* Types cannot be defined in a `typeid' expression. */
6504 saved_message = parser->type_definition_forbidden_message;
6505 parser->type_definition_forbidden_message
6506 = G_("types may not be defined in a %<typeid%> expression");
6507 /* We can't be sure yet whether we're looking at a type-id or an
6508 expression. */
6509 cp_parser_parse_tentatively (parser);
6510 /* Try a type-id first. */
6511 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6512 parser->in_type_id_in_expr_p = true;
6513 type = cp_parser_type_id (parser);
6514 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6515 /* Look for the `)' token. Otherwise, we can't be sure that
6516 we're not looking at an expression: consider `typeid (int
6517 (3))', for example. */
6518 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6519 /* If all went well, simply lookup the type-id. */
6520 if (cp_parser_parse_definitely (parser))
6521 postfix_expression = get_typeid (type, tf_warning_or_error);
6522 /* Otherwise, fall back to the expression variant. */
6523 else
6525 tree expression;
6527 /* Look for an expression. */
6528 expression = cp_parser_expression (parser, & idk);
6529 /* Compute its typeid. */
6530 postfix_expression = build_typeid (expression, tf_warning_or_error);
6531 /* Look for the `)' token. */
6532 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6534 /* Restore the saved message. */
6535 parser->type_definition_forbidden_message = saved_message;
6536 /* `typeid' may not appear in an integral constant expression. */
6537 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6538 postfix_expression = error_mark_node;
6540 break;
6542 case RID_TYPENAME:
6544 tree type;
6545 /* The syntax permitted here is the same permitted for an
6546 elaborated-type-specifier. */
6547 ++parser->prevent_constrained_type_specifiers;
6548 type = cp_parser_elaborated_type_specifier (parser,
6549 /*is_friend=*/false,
6550 /*is_declaration=*/false);
6551 --parser->prevent_constrained_type_specifiers;
6552 postfix_expression = cp_parser_functional_cast (parser, type);
6554 break;
6556 case RID_CILK_SPAWN:
6558 location_t cilk_spawn_loc
6559 = cp_lexer_peek_token (parser->lexer)->location;
6560 cp_lexer_consume_token (parser->lexer);
6561 token = cp_lexer_peek_token (parser->lexer);
6562 if (token->type == CPP_SEMICOLON)
6564 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6565 "an expression");
6566 postfix_expression = error_mark_node;
6567 break;
6569 else if (!current_function_decl)
6571 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6572 "inside a function");
6573 postfix_expression = error_mark_node;
6574 break;
6576 else
6578 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6579 saved_in_statement = parser->in_statement;
6580 parser->in_statement |= IN_CILK_SPAWN;
6582 cfun->calls_cilk_spawn = 1;
6583 postfix_expression =
6584 cp_parser_postfix_expression (parser, false, false,
6585 false, false, &idk);
6586 if (!flag_cilkplus)
6588 error_at (token->location, "-fcilkplus must be enabled to use"
6589 " %<_Cilk_spawn%>");
6590 cfun->calls_cilk_spawn = 0;
6592 else if (saved_in_statement & IN_CILK_SPAWN)
6594 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6595 "are not permitted");
6596 postfix_expression = error_mark_node;
6597 cfun->calls_cilk_spawn = 0;
6599 else
6601 location_t loc = postfix_expression.get_location ();
6602 postfix_expression = build_cilk_spawn (token->location,
6603 postfix_expression);
6604 /* Build a location of the form:
6605 _Cilk_spawn expr
6606 ~~~~~~~~~~~~^~~~
6607 with caret at the expr, ranging from the start of the
6608 _Cilk_spawn token to the end of the expression. */
6609 location_t combined_loc =
6610 make_location (loc, cilk_spawn_loc, get_finish (loc));
6611 postfix_expression.set_location (combined_loc);
6612 if (postfix_expression != error_mark_node)
6613 SET_EXPR_LOCATION (postfix_expression, input_location);
6614 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6616 break;
6619 case RID_ADDRESSOF:
6620 case RID_BUILTIN_SHUFFLE:
6621 case RID_BUILTIN_LAUNDER:
6623 vec<tree, va_gc> *vec;
6624 unsigned int i;
6625 tree p;
6627 cp_lexer_consume_token (parser->lexer);
6628 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6629 /*cast_p=*/false, /*allow_expansion_p=*/true,
6630 /*non_constant_p=*/NULL);
6631 if (vec == NULL)
6633 postfix_expression = error_mark_node;
6634 break;
6637 FOR_EACH_VEC_ELT (*vec, i, p)
6638 mark_exp_read (p);
6640 switch (keyword)
6642 case RID_ADDRESSOF:
6643 if (vec->length () == 1)
6644 postfix_expression
6645 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6646 else
6648 error_at (loc, "wrong number of arguments to "
6649 "%<__builtin_addressof%>");
6650 postfix_expression = error_mark_node;
6652 break;
6654 case RID_BUILTIN_LAUNDER:
6655 if (vec->length () == 1)
6656 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6657 tf_warning_or_error);
6658 else
6660 error_at (loc, "wrong number of arguments to "
6661 "%<__builtin_launder%>");
6662 postfix_expression = error_mark_node;
6664 break;
6666 case RID_BUILTIN_SHUFFLE:
6667 if (vec->length () == 2)
6668 postfix_expression
6669 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6670 (*vec)[1], tf_warning_or_error);
6671 else if (vec->length () == 3)
6672 postfix_expression
6673 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6674 (*vec)[2], tf_warning_or_error);
6675 else
6677 error_at (loc, "wrong number of arguments to "
6678 "%<__builtin_shuffle%>");
6679 postfix_expression = error_mark_node;
6681 break;
6683 default:
6684 gcc_unreachable ();
6686 break;
6689 default:
6691 tree type;
6693 /* If the next thing is a simple-type-specifier, we may be
6694 looking at a functional cast. We could also be looking at
6695 an id-expression. So, we try the functional cast, and if
6696 that doesn't work we fall back to the primary-expression. */
6697 cp_parser_parse_tentatively (parser);
6698 /* Look for the simple-type-specifier. */
6699 ++parser->prevent_constrained_type_specifiers;
6700 type = cp_parser_simple_type_specifier (parser,
6701 /*decl_specs=*/NULL,
6702 CP_PARSER_FLAGS_NONE);
6703 --parser->prevent_constrained_type_specifiers;
6704 /* Parse the cast itself. */
6705 if (!cp_parser_error_occurred (parser))
6706 postfix_expression
6707 = cp_parser_functional_cast (parser, type);
6708 /* If that worked, we're done. */
6709 if (cp_parser_parse_definitely (parser))
6710 break;
6712 /* If the functional-cast didn't work out, try a
6713 compound-literal. */
6714 if (cp_parser_allow_gnu_extensions_p (parser)
6715 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6717 cp_expr initializer = NULL_TREE;
6719 cp_parser_parse_tentatively (parser);
6721 /* Avoid calling cp_parser_type_id pointlessly, see comment
6722 in cp_parser_cast_expression about c++/29234. */
6723 if (!cp_parser_compound_literal_p (parser))
6724 cp_parser_simulate_error (parser);
6725 else
6727 /* Parse the type. */
6728 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6729 parser->in_type_id_in_expr_p = true;
6730 type = cp_parser_type_id (parser);
6731 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6732 /* Look for the `)'. */
6733 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6736 /* If things aren't going well, there's no need to
6737 keep going. */
6738 if (!cp_parser_error_occurred (parser))
6740 bool non_constant_p;
6741 /* Parse the brace-enclosed initializer list. */
6742 initializer = cp_parser_braced_list (parser,
6743 &non_constant_p);
6745 /* If that worked, we're definitely looking at a
6746 compound-literal expression. */
6747 if (cp_parser_parse_definitely (parser))
6749 /* Warn the user that a compound literal is not
6750 allowed in standard C++. */
6751 pedwarn (input_location, OPT_Wpedantic,
6752 "ISO C++ forbids compound-literals");
6753 /* For simplicity, we disallow compound literals in
6754 constant-expressions. We could
6755 allow compound literals of integer type, whose
6756 initializer was a constant, in constant
6757 expressions. Permitting that usage, as a further
6758 extension, would not change the meaning of any
6759 currently accepted programs. (Of course, as
6760 compound literals are not part of ISO C++, the
6761 standard has nothing to say.) */
6762 if (cp_parser_non_integral_constant_expression (parser,
6763 NIC_NCC))
6765 postfix_expression = error_mark_node;
6766 break;
6768 /* Form the representation of the compound-literal. */
6769 postfix_expression
6770 = finish_compound_literal (type, initializer,
6771 tf_warning_or_error, fcl_c99);
6772 postfix_expression.set_location (initializer.get_location ());
6773 break;
6777 /* It must be a primary-expression. */
6778 postfix_expression
6779 = cp_parser_primary_expression (parser, address_p, cast_p,
6780 /*template_arg_p=*/false,
6781 decltype_p,
6782 &idk);
6784 break;
6787 /* Note that we don't need to worry about calling build_cplus_new on a
6788 class-valued CALL_EXPR in decltype when it isn't the end of the
6789 postfix-expression; unary_complex_lvalue will take care of that for
6790 all these cases. */
6792 /* Keep looping until the postfix-expression is complete. */
6793 while (true)
6795 if (idk == CP_ID_KIND_UNQUALIFIED
6796 && identifier_p (postfix_expression)
6797 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6798 /* It is not a Koenig lookup function call. */
6799 postfix_expression
6800 = unqualified_name_lookup_error (postfix_expression);
6802 /* Peek at the next token. */
6803 token = cp_lexer_peek_token (parser->lexer);
6805 switch (token->type)
6807 case CPP_OPEN_SQUARE:
6808 if (cp_next_tokens_can_be_std_attribute_p (parser))
6810 cp_parser_error (parser,
6811 "two consecutive %<[%> shall "
6812 "only introduce an attribute");
6813 return error_mark_node;
6815 postfix_expression
6816 = cp_parser_postfix_open_square_expression (parser,
6817 postfix_expression,
6818 false,
6819 decltype_p);
6820 postfix_expression.set_range (start_loc,
6821 postfix_expression.get_location ());
6823 idk = CP_ID_KIND_NONE;
6824 is_member_access = false;
6825 break;
6827 case CPP_OPEN_PAREN:
6828 /* postfix-expression ( expression-list [opt] ) */
6830 bool koenig_p;
6831 bool is_builtin_constant_p;
6832 bool saved_integral_constant_expression_p = false;
6833 bool saved_non_integral_constant_expression_p = false;
6834 tsubst_flags_t complain = complain_flags (decltype_p);
6835 vec<tree, va_gc> *args;
6836 location_t close_paren_loc = UNKNOWN_LOCATION;
6838 is_member_access = false;
6840 is_builtin_constant_p
6841 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6842 if (is_builtin_constant_p)
6844 /* The whole point of __builtin_constant_p is to allow
6845 non-constant expressions to appear as arguments. */
6846 saved_integral_constant_expression_p
6847 = parser->integral_constant_expression_p;
6848 saved_non_integral_constant_expression_p
6849 = parser->non_integral_constant_expression_p;
6850 parser->integral_constant_expression_p = false;
6852 args = (cp_parser_parenthesized_expression_list
6853 (parser, non_attr,
6854 /*cast_p=*/false, /*allow_expansion_p=*/true,
6855 /*non_constant_p=*/NULL,
6856 /*close_paren_loc=*/&close_paren_loc));
6857 if (is_builtin_constant_p)
6859 parser->integral_constant_expression_p
6860 = saved_integral_constant_expression_p;
6861 parser->non_integral_constant_expression_p
6862 = saved_non_integral_constant_expression_p;
6865 if (args == NULL)
6867 postfix_expression = error_mark_node;
6868 break;
6871 /* Function calls are not permitted in
6872 constant-expressions. */
6873 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6874 && cp_parser_non_integral_constant_expression (parser,
6875 NIC_FUNC_CALL))
6877 postfix_expression = error_mark_node;
6878 release_tree_vector (args);
6879 break;
6882 koenig_p = false;
6883 if (idk == CP_ID_KIND_UNQUALIFIED
6884 || idk == CP_ID_KIND_TEMPLATE_ID)
6886 if (identifier_p (postfix_expression))
6888 if (!args->is_empty ())
6890 koenig_p = true;
6891 if (!any_type_dependent_arguments_p (args))
6892 postfix_expression
6893 = perform_koenig_lookup (postfix_expression, args,
6894 complain);
6896 else
6897 postfix_expression
6898 = unqualified_fn_lookup_error (postfix_expression);
6900 /* We do not perform argument-dependent lookup if
6901 normal lookup finds a non-function, in accordance
6902 with the expected resolution of DR 218. */
6903 else if (!args->is_empty ()
6904 && is_overloaded_fn (postfix_expression))
6906 tree fn = get_first_fn (postfix_expression);
6907 fn = STRIP_TEMPLATE (fn);
6909 /* Do not do argument dependent lookup if regular
6910 lookup finds a member function or a block-scope
6911 function declaration. [basic.lookup.argdep]/3 */
6912 if (!DECL_FUNCTION_MEMBER_P (fn)
6913 && !DECL_LOCAL_FUNCTION_P (fn))
6915 koenig_p = true;
6916 if (!any_type_dependent_arguments_p (args))
6917 postfix_expression
6918 = perform_koenig_lookup (postfix_expression, args,
6919 complain);
6924 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6925 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6926 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6927 && vec_safe_length (args) == 3)
6929 tree arg0 = (*args)[0];
6930 tree arg1 = (*args)[1];
6931 tree arg2 = (*args)[2];
6932 int literal_mask = ((!!integer_zerop (arg1) << 1)
6933 | (!!integer_zerop (arg2) << 2));
6934 if (TREE_CODE (arg2) == CONST_DECL)
6935 arg2 = DECL_INITIAL (arg2);
6936 warn_for_memset (input_location, arg0, arg2, literal_mask);
6939 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6941 tree instance = TREE_OPERAND (postfix_expression, 0);
6942 tree fn = TREE_OPERAND (postfix_expression, 1);
6944 if (processing_template_decl
6945 && (type_dependent_object_expression_p (instance)
6946 || (!BASELINK_P (fn)
6947 && TREE_CODE (fn) != FIELD_DECL)
6948 || type_dependent_expression_p (fn)
6949 || any_type_dependent_arguments_p (args)))
6951 maybe_generic_this_capture (instance, fn);
6952 postfix_expression
6953 = build_nt_call_vec (postfix_expression, args);
6954 release_tree_vector (args);
6955 break;
6958 if (BASELINK_P (fn))
6960 postfix_expression
6961 = (build_new_method_call
6962 (instance, fn, &args, NULL_TREE,
6963 (idk == CP_ID_KIND_QUALIFIED
6964 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6965 : LOOKUP_NORMAL),
6966 /*fn_p=*/NULL,
6967 complain));
6969 else
6970 postfix_expression
6971 = finish_call_expr (postfix_expression, &args,
6972 /*disallow_virtual=*/false,
6973 /*koenig_p=*/false,
6974 complain);
6976 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6977 || TREE_CODE (postfix_expression) == MEMBER_REF
6978 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6979 postfix_expression = (build_offset_ref_call_from_tree
6980 (postfix_expression, &args,
6981 complain));
6982 else if (idk == CP_ID_KIND_QUALIFIED)
6983 /* A call to a static class member, or a namespace-scope
6984 function. */
6985 postfix_expression
6986 = finish_call_expr (postfix_expression, &args,
6987 /*disallow_virtual=*/true,
6988 koenig_p,
6989 complain);
6990 else
6991 /* All other function calls. */
6992 postfix_expression
6993 = finish_call_expr (postfix_expression, &args,
6994 /*disallow_virtual=*/false,
6995 koenig_p,
6996 complain);
6998 if (close_paren_loc != UNKNOWN_LOCATION)
7000 location_t combined_loc = make_location (token->location,
7001 start_loc,
7002 close_paren_loc);
7003 postfix_expression.set_location (combined_loc);
7006 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7007 idk = CP_ID_KIND_NONE;
7009 release_tree_vector (args);
7011 break;
7013 case CPP_DOT:
7014 case CPP_DEREF:
7015 /* postfix-expression . template [opt] id-expression
7016 postfix-expression . pseudo-destructor-name
7017 postfix-expression -> template [opt] id-expression
7018 postfix-expression -> pseudo-destructor-name */
7020 /* Consume the `.' or `->' operator. */
7021 cp_lexer_consume_token (parser->lexer);
7023 postfix_expression
7024 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7025 postfix_expression,
7026 false, &idk, loc);
7028 is_member_access = true;
7029 break;
7031 case CPP_PLUS_PLUS:
7032 /* postfix-expression ++ */
7033 /* Consume the `++' token. */
7034 cp_lexer_consume_token (parser->lexer);
7035 /* Generate a representation for the complete expression. */
7036 postfix_expression
7037 = finish_increment_expr (postfix_expression,
7038 POSTINCREMENT_EXPR);
7039 /* Increments may not appear in constant-expressions. */
7040 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7041 postfix_expression = error_mark_node;
7042 idk = CP_ID_KIND_NONE;
7043 is_member_access = false;
7044 break;
7046 case CPP_MINUS_MINUS:
7047 /* postfix-expression -- */
7048 /* Consume the `--' token. */
7049 cp_lexer_consume_token (parser->lexer);
7050 /* Generate a representation for the complete expression. */
7051 postfix_expression
7052 = finish_increment_expr (postfix_expression,
7053 POSTDECREMENT_EXPR);
7054 /* Decrements may not appear in constant-expressions. */
7055 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7056 postfix_expression = error_mark_node;
7057 idk = CP_ID_KIND_NONE;
7058 is_member_access = false;
7059 break;
7061 default:
7062 if (pidk_return != NULL)
7063 * pidk_return = idk;
7064 if (member_access_only_p)
7065 return is_member_access
7066 ? postfix_expression
7067 : cp_expr (error_mark_node);
7068 else
7069 return postfix_expression;
7073 /* We should never get here. */
7074 gcc_unreachable ();
7075 return error_mark_node;
7078 /* This function parses Cilk Plus array notations. If a normal array expr. is
7079 parsed then the array index is passed back to the caller through *INIT_INDEX
7080 and the function returns a NULL_TREE. If array notation expr. is parsed,
7081 then *INIT_INDEX is ignored by the caller and the function returns
7082 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7083 error_mark_node. */
7085 static tree
7086 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7087 tree array_value)
7089 cp_token *token = NULL;
7090 tree length_index, stride = NULL_TREE, value_tree, array_type;
7091 if (!array_value || array_value == error_mark_node)
7093 cp_parser_skip_to_end_of_statement (parser);
7094 return error_mark_node;
7097 array_type = TREE_TYPE (array_value);
7099 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7100 parser->colon_corrects_to_scope_p = false;
7101 token = cp_lexer_peek_token (parser->lexer);
7103 if (!token)
7105 cp_parser_error (parser, "expected %<:%> or numeral");
7106 return error_mark_node;
7108 else if (token->type == CPP_COLON)
7110 /* Consume the ':'. */
7111 cp_lexer_consume_token (parser->lexer);
7113 /* If we are here, then we have a case like this A[:]. */
7114 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7116 cp_parser_error (parser, "expected %<]%>");
7117 cp_parser_skip_to_end_of_statement (parser);
7118 return error_mark_node;
7120 *init_index = NULL_TREE;
7121 stride = NULL_TREE;
7122 length_index = NULL_TREE;
7124 else
7126 /* If we are here, then there are three valid possibilities:
7127 1. ARRAY [ EXP ]
7128 2. ARRAY [ EXP : EXP ]
7129 3. ARRAY [ EXP : EXP : EXP ] */
7131 *init_index = cp_parser_expression (parser);
7132 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7134 /* This indicates that we have a normal array expression. */
7135 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7136 return NULL_TREE;
7139 /* Consume the ':'. */
7140 cp_lexer_consume_token (parser->lexer);
7141 length_index = cp_parser_expression (parser);
7142 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7144 cp_lexer_consume_token (parser->lexer);
7145 stride = cp_parser_expression (parser);
7148 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7150 if (*init_index == error_mark_node || length_index == error_mark_node
7151 || stride == error_mark_node || array_type == error_mark_node)
7153 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7154 cp_lexer_consume_token (parser->lexer);
7155 return error_mark_node;
7157 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7159 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7160 length_index, stride, array_type);
7161 return value_tree;
7164 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7165 by cp_parser_builtin_offsetof. We're looking for
7167 postfix-expression [ expression ]
7168 postfix-expression [ braced-init-list ] (C++11)
7170 FOR_OFFSETOF is set if we're being called in that context, which
7171 changes how we deal with integer constant expressions. */
7173 static tree
7174 cp_parser_postfix_open_square_expression (cp_parser *parser,
7175 tree postfix_expression,
7176 bool for_offsetof,
7177 bool decltype_p)
7179 tree index = NULL_TREE;
7180 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7181 bool saved_greater_than_is_operator_p;
7183 /* Consume the `[' token. */
7184 cp_lexer_consume_token (parser->lexer);
7186 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7187 parser->greater_than_is_operator_p = true;
7189 /* Parse the index expression. */
7190 /* ??? For offsetof, there is a question of what to allow here. If
7191 offsetof is not being used in an integral constant expression context,
7192 then we *could* get the right answer by computing the value at runtime.
7193 If we are in an integral constant expression context, then we might
7194 could accept any constant expression; hard to say without analysis.
7195 Rather than open the barn door too wide right away, allow only integer
7196 constant expressions here. */
7197 if (for_offsetof)
7198 index = cp_parser_constant_expression (parser);
7199 else
7201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7203 bool expr_nonconst_p;
7204 cp_lexer_set_source_position (parser->lexer);
7205 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7206 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7207 if (flag_cilkplus
7208 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7210 error_at (cp_lexer_peek_token (parser->lexer)->location,
7211 "braced list index is not allowed with array "
7212 "notation");
7213 cp_parser_skip_to_end_of_statement (parser);
7214 return error_mark_node;
7217 else if (flag_cilkplus)
7219 /* Here are have these two options:
7220 ARRAY[EXP : EXP] - Array notation expr with default
7221 stride of 1.
7222 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7223 stride. */
7224 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7225 postfix_expression);
7226 if (an_exp)
7227 return an_exp;
7229 else
7230 index = cp_parser_expression (parser);
7233 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7235 /* Look for the closing `]'. */
7236 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7238 /* Build the ARRAY_REF. */
7239 postfix_expression = grok_array_decl (loc, postfix_expression,
7240 index, decltype_p);
7242 /* When not doing offsetof, array references are not permitted in
7243 constant-expressions. */
7244 if (!for_offsetof
7245 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7246 postfix_expression = error_mark_node;
7248 return postfix_expression;
7251 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7252 by cp_parser_builtin_offsetof. We're looking for
7254 postfix-expression . template [opt] id-expression
7255 postfix-expression . pseudo-destructor-name
7256 postfix-expression -> template [opt] id-expression
7257 postfix-expression -> pseudo-destructor-name
7259 FOR_OFFSETOF is set if we're being called in that context. That sorta
7260 limits what of the above we'll actually accept, but nevermind.
7261 TOKEN_TYPE is the "." or "->" token, which will already have been
7262 removed from the stream. */
7264 static tree
7265 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7266 enum cpp_ttype token_type,
7267 cp_expr postfix_expression,
7268 bool for_offsetof, cp_id_kind *idk,
7269 location_t location)
7271 tree name;
7272 bool dependent_p;
7273 bool pseudo_destructor_p;
7274 tree scope = NULL_TREE;
7275 location_t start_loc = postfix_expression.get_start ();
7277 /* If this is a `->' operator, dereference the pointer. */
7278 if (token_type == CPP_DEREF)
7279 postfix_expression = build_x_arrow (location, postfix_expression,
7280 tf_warning_or_error);
7281 /* Check to see whether or not the expression is type-dependent and
7282 not the current instantiation. */
7283 dependent_p = type_dependent_object_expression_p (postfix_expression);
7284 /* The identifier following the `->' or `.' is not qualified. */
7285 parser->scope = NULL_TREE;
7286 parser->qualifying_scope = NULL_TREE;
7287 parser->object_scope = NULL_TREE;
7288 *idk = CP_ID_KIND_NONE;
7290 /* Enter the scope corresponding to the type of the object
7291 given by the POSTFIX_EXPRESSION. */
7292 if (!dependent_p)
7294 scope = TREE_TYPE (postfix_expression);
7295 /* According to the standard, no expression should ever have
7296 reference type. Unfortunately, we do not currently match
7297 the standard in this respect in that our internal representation
7298 of an expression may have reference type even when the standard
7299 says it does not. Therefore, we have to manually obtain the
7300 underlying type here. */
7301 scope = non_reference (scope);
7302 /* The type of the POSTFIX_EXPRESSION must be complete. */
7303 /* Unlike the object expression in other contexts, *this is not
7304 required to be of complete type for purposes of class member
7305 access (5.2.5) outside the member function body. */
7306 if (postfix_expression != current_class_ref
7307 && scope != error_mark_node
7308 && !(processing_template_decl
7309 && current_class_type
7310 && (same_type_ignoring_top_level_qualifiers_p
7311 (scope, current_class_type))))
7313 scope = complete_type (scope);
7314 if (!COMPLETE_TYPE_P (scope)
7315 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7316 && EXPR_P (postfix_expression))
7318 /* In a template, be permissive by treating an object expression
7319 of incomplete type as dependent (after a pedwarn). */
7320 diagnostic_t kind = (processing_template_decl
7321 ? DK_PEDWARN
7322 : DK_ERROR);
7323 cxx_incomplete_type_diagnostic
7324 (location_of (postfix_expression),
7325 postfix_expression, scope, kind);
7326 if (processing_template_decl)
7328 dependent_p = true;
7329 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7334 if (!dependent_p)
7336 /* Let the name lookup machinery know that we are processing a
7337 class member access expression. */
7338 parser->context->object_type = scope;
7339 /* If something went wrong, we want to be able to discern that case,
7340 as opposed to the case where there was no SCOPE due to the type
7341 of expression being dependent. */
7342 if (!scope)
7343 scope = error_mark_node;
7344 /* If the SCOPE was erroneous, make the various semantic analysis
7345 functions exit quickly -- and without issuing additional error
7346 messages. */
7347 if (scope == error_mark_node)
7348 postfix_expression = error_mark_node;
7352 if (dependent_p)
7353 /* Tell cp_parser_lookup_name that there was an object, even though it's
7354 type-dependent. */
7355 parser->context->object_type = unknown_type_node;
7357 /* Assume this expression is not a pseudo-destructor access. */
7358 pseudo_destructor_p = false;
7360 /* If the SCOPE is a scalar type, then, if this is a valid program,
7361 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7362 is type dependent, it can be pseudo-destructor-name or something else.
7363 Try to parse it as pseudo-destructor-name first. */
7364 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7366 tree s;
7367 tree type;
7369 cp_parser_parse_tentatively (parser);
7370 /* Parse the pseudo-destructor-name. */
7371 s = NULL_TREE;
7372 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7373 &s, &type);
7374 if (dependent_p
7375 && (cp_parser_error_occurred (parser)
7376 || !SCALAR_TYPE_P (type)))
7377 cp_parser_abort_tentative_parse (parser);
7378 else if (cp_parser_parse_definitely (parser))
7380 pseudo_destructor_p = true;
7381 postfix_expression
7382 = finish_pseudo_destructor_expr (postfix_expression,
7383 s, type, location);
7387 if (!pseudo_destructor_p)
7389 /* If the SCOPE is not a scalar type, we are looking at an
7390 ordinary class member access expression, rather than a
7391 pseudo-destructor-name. */
7392 bool template_p;
7393 cp_token *token = cp_lexer_peek_token (parser->lexer);
7394 /* Parse the id-expression. */
7395 name = (cp_parser_id_expression
7396 (parser,
7397 cp_parser_optional_template_keyword (parser),
7398 /*check_dependency_p=*/true,
7399 &template_p,
7400 /*declarator_p=*/false,
7401 /*optional_p=*/false));
7402 /* In general, build a SCOPE_REF if the member name is qualified.
7403 However, if the name was not dependent and has already been
7404 resolved; there is no need to build the SCOPE_REF. For example;
7406 struct X { void f(); };
7407 template <typename T> void f(T* t) { t->X::f(); }
7409 Even though "t" is dependent, "X::f" is not and has been resolved
7410 to a BASELINK; there is no need to include scope information. */
7412 /* But we do need to remember that there was an explicit scope for
7413 virtual function calls. */
7414 if (parser->scope)
7415 *idk = CP_ID_KIND_QUALIFIED;
7417 /* If the name is a template-id that names a type, we will get a
7418 TYPE_DECL here. That is invalid code. */
7419 if (TREE_CODE (name) == TYPE_DECL)
7421 error_at (token->location, "invalid use of %qD", name);
7422 postfix_expression = error_mark_node;
7424 else
7426 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7428 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7430 error_at (token->location, "%<%D::%D%> is not a class member",
7431 parser->scope, name);
7432 postfix_expression = error_mark_node;
7434 else
7435 name = build_qualified_name (/*type=*/NULL_TREE,
7436 parser->scope,
7437 name,
7438 template_p);
7439 parser->scope = NULL_TREE;
7440 parser->qualifying_scope = NULL_TREE;
7441 parser->object_scope = NULL_TREE;
7443 if (parser->scope && name && BASELINK_P (name))
7444 adjust_result_of_qualified_name_lookup
7445 (name, parser->scope, scope);
7446 postfix_expression
7447 = finish_class_member_access_expr (postfix_expression, name,
7448 template_p,
7449 tf_warning_or_error);
7450 /* Build a location e.g.:
7451 ptr->access_expr
7452 ~~~^~~~~~~~~~~~~
7453 where the caret is at the deref token, ranging from
7454 the start of postfix_expression to the end of the access expr. */
7455 location_t end_loc
7456 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7457 location_t combined_loc
7458 = make_location (input_location, start_loc, end_loc);
7459 protected_set_expr_location (postfix_expression, combined_loc);
7463 /* We no longer need to look up names in the scope of the object on
7464 the left-hand side of the `.' or `->' operator. */
7465 parser->context->object_type = NULL_TREE;
7467 /* Outside of offsetof, these operators may not appear in
7468 constant-expressions. */
7469 if (!for_offsetof
7470 && (cp_parser_non_integral_constant_expression
7471 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7472 postfix_expression = error_mark_node;
7474 return postfix_expression;
7477 /* Parse a parenthesized expression-list.
7479 expression-list:
7480 assignment-expression
7481 expression-list, assignment-expression
7483 attribute-list:
7484 expression-list
7485 identifier
7486 identifier, expression-list
7488 CAST_P is true if this expression is the target of a cast.
7490 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7491 argument pack.
7493 Returns a vector of trees. Each element is a representation of an
7494 assignment-expression. NULL is returned if the ( and or ) are
7495 missing. An empty, but allocated, vector is returned on no
7496 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7497 if we are parsing an attribute list for an attribute that wants a
7498 plain identifier argument, normal_attr for an attribute that wants
7499 an expression, or non_attr if we aren't parsing an attribute list. If
7500 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7501 not all of the expressions in the list were constant.
7502 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7503 will be written to with the location of the closing parenthesis. If
7504 an error occurs, it may or may not be written to. */
7506 static vec<tree, va_gc> *
7507 cp_parser_parenthesized_expression_list (cp_parser* parser,
7508 int is_attribute_list,
7509 bool cast_p,
7510 bool allow_expansion_p,
7511 bool *non_constant_p,
7512 location_t *close_paren_loc)
7514 vec<tree, va_gc> *expression_list;
7515 bool fold_expr_p = is_attribute_list != non_attr;
7516 tree identifier = NULL_TREE;
7517 bool saved_greater_than_is_operator_p;
7519 /* Assume all the expressions will be constant. */
7520 if (non_constant_p)
7521 *non_constant_p = false;
7523 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7524 return NULL;
7526 expression_list = make_tree_vector ();
7528 /* Within a parenthesized expression, a `>' token is always
7529 the greater-than operator. */
7530 saved_greater_than_is_operator_p
7531 = parser->greater_than_is_operator_p;
7532 parser->greater_than_is_operator_p = true;
7534 /* Consume expressions until there are no more. */
7535 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7536 while (true)
7538 tree expr;
7540 /* At the beginning of attribute lists, check to see if the
7541 next token is an identifier. */
7542 if (is_attribute_list == id_attr
7543 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7545 cp_token *token;
7547 /* Consume the identifier. */
7548 token = cp_lexer_consume_token (parser->lexer);
7549 /* Save the identifier. */
7550 identifier = token->u.value;
7552 else
7554 bool expr_non_constant_p;
7556 /* Parse the next assignment-expression. */
7557 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7559 /* A braced-init-list. */
7560 cp_lexer_set_source_position (parser->lexer);
7561 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7562 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7563 if (non_constant_p && expr_non_constant_p)
7564 *non_constant_p = true;
7566 else if (non_constant_p)
7568 expr = (cp_parser_constant_expression
7569 (parser, /*allow_non_constant_p=*/true,
7570 &expr_non_constant_p));
7571 if (expr_non_constant_p)
7572 *non_constant_p = true;
7574 else
7575 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7576 cast_p);
7578 if (fold_expr_p)
7579 expr = instantiate_non_dependent_expr (expr);
7581 /* If we have an ellipsis, then this is an expression
7582 expansion. */
7583 if (allow_expansion_p
7584 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7586 /* Consume the `...'. */
7587 cp_lexer_consume_token (parser->lexer);
7589 /* Build the argument pack. */
7590 expr = make_pack_expansion (expr);
7593 /* Add it to the list. We add error_mark_node
7594 expressions to the list, so that we can still tell if
7595 the correct form for a parenthesized expression-list
7596 is found. That gives better errors. */
7597 vec_safe_push (expression_list, expr);
7599 if (expr == error_mark_node)
7600 goto skip_comma;
7603 /* After the first item, attribute lists look the same as
7604 expression lists. */
7605 is_attribute_list = non_attr;
7607 get_comma:;
7608 /* If the next token isn't a `,', then we are done. */
7609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7610 break;
7612 /* Otherwise, consume the `,' and keep going. */
7613 cp_lexer_consume_token (parser->lexer);
7616 if (close_paren_loc)
7617 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7619 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7621 int ending;
7623 skip_comma:;
7624 /* We try and resync to an unnested comma, as that will give the
7625 user better diagnostics. */
7626 ending = cp_parser_skip_to_closing_parenthesis (parser,
7627 /*recovering=*/true,
7628 /*or_comma=*/true,
7629 /*consume_paren=*/true);
7630 if (ending < 0)
7631 goto get_comma;
7632 if (!ending)
7634 parser->greater_than_is_operator_p
7635 = saved_greater_than_is_operator_p;
7636 return NULL;
7640 parser->greater_than_is_operator_p
7641 = saved_greater_than_is_operator_p;
7643 if (identifier)
7644 vec_safe_insert (expression_list, 0, identifier);
7646 return expression_list;
7649 /* Parse a pseudo-destructor-name.
7651 pseudo-destructor-name:
7652 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7653 :: [opt] nested-name-specifier template template-id :: ~ type-name
7654 :: [opt] nested-name-specifier [opt] ~ type-name
7656 If either of the first two productions is used, sets *SCOPE to the
7657 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7658 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7659 or ERROR_MARK_NODE if the parse fails. */
7661 static void
7662 cp_parser_pseudo_destructor_name (cp_parser* parser,
7663 tree object,
7664 tree* scope,
7665 tree* type)
7667 bool nested_name_specifier_p;
7669 /* Handle ~auto. */
7670 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7671 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7672 && !type_dependent_expression_p (object))
7674 if (cxx_dialect < cxx14)
7675 pedwarn (input_location, 0,
7676 "%<~auto%> only available with "
7677 "-std=c++14 or -std=gnu++14");
7678 cp_lexer_consume_token (parser->lexer);
7679 cp_lexer_consume_token (parser->lexer);
7680 *scope = NULL_TREE;
7681 *type = TREE_TYPE (object);
7682 return;
7685 /* Assume that things will not work out. */
7686 *type = error_mark_node;
7688 /* Look for the optional `::' operator. */
7689 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7690 /* Look for the optional nested-name-specifier. */
7691 nested_name_specifier_p
7692 = (cp_parser_nested_name_specifier_opt (parser,
7693 /*typename_keyword_p=*/false,
7694 /*check_dependency_p=*/true,
7695 /*type_p=*/false,
7696 /*is_declaration=*/false)
7697 != NULL_TREE);
7698 /* Now, if we saw a nested-name-specifier, we might be doing the
7699 second production. */
7700 if (nested_name_specifier_p
7701 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7703 /* Consume the `template' keyword. */
7704 cp_lexer_consume_token (parser->lexer);
7705 /* Parse the template-id. */
7706 cp_parser_template_id (parser,
7707 /*template_keyword_p=*/true,
7708 /*check_dependency_p=*/false,
7709 class_type,
7710 /*is_declaration=*/true);
7711 /* Look for the `::' token. */
7712 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7714 /* If the next token is not a `~', then there might be some
7715 additional qualification. */
7716 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7718 /* At this point, we're looking for "type-name :: ~". The type-name
7719 must not be a class-name, since this is a pseudo-destructor. So,
7720 it must be either an enum-name, or a typedef-name -- both of which
7721 are just identifiers. So, we peek ahead to check that the "::"
7722 and "~" tokens are present; if they are not, then we can avoid
7723 calling type_name. */
7724 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7725 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7726 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7728 cp_parser_error (parser, "non-scalar type");
7729 return;
7732 /* Look for the type-name. */
7733 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7734 if (*scope == error_mark_node)
7735 return;
7737 /* Look for the `::' token. */
7738 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7740 else
7741 *scope = NULL_TREE;
7743 /* Look for the `~'. */
7744 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7746 /* Once we see the ~, this has to be a pseudo-destructor. */
7747 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7748 cp_parser_commit_to_topmost_tentative_parse (parser);
7750 /* Look for the type-name again. We are not responsible for
7751 checking that it matches the first type-name. */
7752 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7755 /* Parse a unary-expression.
7757 unary-expression:
7758 postfix-expression
7759 ++ cast-expression
7760 -- cast-expression
7761 unary-operator cast-expression
7762 sizeof unary-expression
7763 sizeof ( type-id )
7764 alignof ( type-id ) [C++0x]
7765 new-expression
7766 delete-expression
7768 GNU Extensions:
7770 unary-expression:
7771 __extension__ cast-expression
7772 __alignof__ unary-expression
7773 __alignof__ ( type-id )
7774 alignof unary-expression [C++0x]
7775 __real__ cast-expression
7776 __imag__ cast-expression
7777 && identifier
7778 sizeof ( type-id ) { initializer-list , [opt] }
7779 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7780 __alignof__ ( type-id ) { initializer-list , [opt] }
7782 ADDRESS_P is true iff the unary-expression is appearing as the
7783 operand of the `&' operator. CAST_P is true if this expression is
7784 the target of a cast.
7786 Returns a representation of the expression. */
7788 static cp_expr
7789 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7790 bool address_p, bool cast_p, bool decltype_p)
7792 cp_token *token;
7793 enum tree_code unary_operator;
7795 /* Peek at the next token. */
7796 token = cp_lexer_peek_token (parser->lexer);
7797 /* Some keywords give away the kind of expression. */
7798 if (token->type == CPP_KEYWORD)
7800 enum rid keyword = token->keyword;
7802 switch (keyword)
7804 case RID_ALIGNOF:
7805 case RID_SIZEOF:
7807 tree operand, ret;
7808 enum tree_code op;
7809 location_t start_loc = token->location;
7811 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7812 /* Consume the token. */
7813 cp_lexer_consume_token (parser->lexer);
7814 /* Parse the operand. */
7815 operand = cp_parser_sizeof_operand (parser, keyword);
7817 if (TYPE_P (operand))
7818 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7819 else
7821 /* ISO C++ defines alignof only with types, not with
7822 expressions. So pedwarn if alignof is used with a non-
7823 type expression. However, __alignof__ is ok. */
7824 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7825 pedwarn (token->location, OPT_Wpedantic,
7826 "ISO C++ does not allow %<alignof%> "
7827 "with a non-type");
7829 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7831 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7832 SIZEOF_EXPR with the original operand. */
7833 if (op == SIZEOF_EXPR && ret != error_mark_node)
7835 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7837 if (!processing_template_decl && TYPE_P (operand))
7839 ret = build_min (SIZEOF_EXPR, size_type_node,
7840 build1 (NOP_EXPR, operand,
7841 error_mark_node));
7842 SIZEOF_EXPR_TYPE_P (ret) = 1;
7844 else
7845 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7846 TREE_SIDE_EFFECTS (ret) = 0;
7847 TREE_READONLY (ret) = 1;
7851 /* Construct a location e.g. :
7852 alignof (expr)
7853 ^~~~~~~~~~~~~~
7854 with start == caret at the start of the "alignof"/"sizeof"
7855 token, with the endpoint at the final closing paren. */
7856 location_t finish_loc
7857 = cp_lexer_previous_token (parser->lexer)->location;
7858 location_t compound_loc
7859 = make_location (start_loc, start_loc, finish_loc);
7861 cp_expr ret_expr (ret);
7862 ret_expr.set_location (compound_loc);
7863 return ret_expr;
7866 case RID_NEW:
7867 return cp_parser_new_expression (parser);
7869 case RID_DELETE:
7870 return cp_parser_delete_expression (parser);
7872 case RID_EXTENSION:
7874 /* The saved value of the PEDANTIC flag. */
7875 int saved_pedantic;
7876 tree expr;
7878 /* Save away the PEDANTIC flag. */
7879 cp_parser_extension_opt (parser, &saved_pedantic);
7880 /* Parse the cast-expression. */
7881 expr = cp_parser_simple_cast_expression (parser);
7882 /* Restore the PEDANTIC flag. */
7883 pedantic = saved_pedantic;
7885 return expr;
7888 case RID_REALPART:
7889 case RID_IMAGPART:
7891 tree expression;
7893 /* Consume the `__real__' or `__imag__' token. */
7894 cp_lexer_consume_token (parser->lexer);
7895 /* Parse the cast-expression. */
7896 expression = cp_parser_simple_cast_expression (parser);
7897 /* Create the complete representation. */
7898 return build_x_unary_op (token->location,
7899 (keyword == RID_REALPART
7900 ? REALPART_EXPR : IMAGPART_EXPR),
7901 expression,
7902 tf_warning_or_error);
7904 break;
7906 case RID_TRANSACTION_ATOMIC:
7907 case RID_TRANSACTION_RELAXED:
7908 return cp_parser_transaction_expression (parser, keyword);
7910 case RID_NOEXCEPT:
7912 tree expr;
7913 const char *saved_message;
7914 bool saved_integral_constant_expression_p;
7915 bool saved_non_integral_constant_expression_p;
7916 bool saved_greater_than_is_operator_p;
7918 cp_lexer_consume_token (parser->lexer);
7919 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7921 saved_message = parser->type_definition_forbidden_message;
7922 parser->type_definition_forbidden_message
7923 = G_("types may not be defined in %<noexcept%> expressions");
7925 saved_integral_constant_expression_p
7926 = parser->integral_constant_expression_p;
7927 saved_non_integral_constant_expression_p
7928 = parser->non_integral_constant_expression_p;
7929 parser->integral_constant_expression_p = false;
7931 saved_greater_than_is_operator_p
7932 = parser->greater_than_is_operator_p;
7933 parser->greater_than_is_operator_p = true;
7935 ++cp_unevaluated_operand;
7936 ++c_inhibit_evaluation_warnings;
7937 ++cp_noexcept_operand;
7938 expr = cp_parser_expression (parser);
7939 --cp_noexcept_operand;
7940 --c_inhibit_evaluation_warnings;
7941 --cp_unevaluated_operand;
7943 parser->greater_than_is_operator_p
7944 = saved_greater_than_is_operator_p;
7946 parser->integral_constant_expression_p
7947 = saved_integral_constant_expression_p;
7948 parser->non_integral_constant_expression_p
7949 = saved_non_integral_constant_expression_p;
7951 parser->type_definition_forbidden_message = saved_message;
7953 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7954 return finish_noexcept_expr (expr, tf_warning_or_error);
7957 default:
7958 break;
7962 /* Look for the `:: new' and `:: delete', which also signal the
7963 beginning of a new-expression, or delete-expression,
7964 respectively. If the next token is `::', then it might be one of
7965 these. */
7966 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7968 enum rid keyword;
7970 /* See if the token after the `::' is one of the keywords in
7971 which we're interested. */
7972 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7973 /* If it's `new', we have a new-expression. */
7974 if (keyword == RID_NEW)
7975 return cp_parser_new_expression (parser);
7976 /* Similarly, for `delete'. */
7977 else if (keyword == RID_DELETE)
7978 return cp_parser_delete_expression (parser);
7981 /* Look for a unary operator. */
7982 unary_operator = cp_parser_unary_operator (token);
7983 /* The `++' and `--' operators can be handled similarly, even though
7984 they are not technically unary-operators in the grammar. */
7985 if (unary_operator == ERROR_MARK)
7987 if (token->type == CPP_PLUS_PLUS)
7988 unary_operator = PREINCREMENT_EXPR;
7989 else if (token->type == CPP_MINUS_MINUS)
7990 unary_operator = PREDECREMENT_EXPR;
7991 /* Handle the GNU address-of-label extension. */
7992 else if (cp_parser_allow_gnu_extensions_p (parser)
7993 && token->type == CPP_AND_AND)
7995 tree identifier;
7996 tree expression;
7997 location_t start_loc = token->location;
7999 /* Consume the '&&' token. */
8000 cp_lexer_consume_token (parser->lexer);
8001 /* Look for the identifier. */
8002 location_t finish_loc
8003 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8004 identifier = cp_parser_identifier (parser);
8005 /* Construct a location of the form:
8006 &&label
8007 ^~~~~~~
8008 with caret==start at the "&&", finish at the end of the label. */
8009 location_t combined_loc
8010 = make_location (start_loc, start_loc, finish_loc);
8011 /* Create an expression representing the address. */
8012 expression = finish_label_address_expr (identifier, combined_loc);
8013 if (cp_parser_non_integral_constant_expression (parser,
8014 NIC_ADDR_LABEL))
8015 expression = error_mark_node;
8016 return expression;
8019 if (unary_operator != ERROR_MARK)
8021 cp_expr cast_expression;
8022 cp_expr expression = error_mark_node;
8023 non_integral_constant non_constant_p = NIC_NONE;
8024 location_t loc = token->location;
8025 tsubst_flags_t complain = complain_flags (decltype_p);
8027 /* Consume the operator token. */
8028 token = cp_lexer_consume_token (parser->lexer);
8029 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8031 /* Parse the cast-expression. */
8032 cast_expression
8033 = cp_parser_cast_expression (parser,
8034 unary_operator == ADDR_EXPR,
8035 /*cast_p=*/false,
8036 /*decltype*/false,
8037 pidk);
8039 /* Make a location:
8040 OP_TOKEN CAST_EXPRESSION
8041 ^~~~~~~~~~~~~~~~~~~~~~~~~
8042 with start==caret at the operator token, and
8043 extending to the end of the cast_expression. */
8044 loc = make_location (loc, loc, cast_expression.get_finish ());
8046 /* Now, build an appropriate representation. */
8047 switch (unary_operator)
8049 case INDIRECT_REF:
8050 non_constant_p = NIC_STAR;
8051 expression = build_x_indirect_ref (loc, cast_expression,
8052 RO_UNARY_STAR,
8053 complain);
8054 /* TODO: build_x_indirect_ref does not always honor the
8055 location, so ensure it is set. */
8056 expression.set_location (loc);
8057 break;
8059 case ADDR_EXPR:
8060 non_constant_p = NIC_ADDR;
8061 /* Fall through. */
8062 case BIT_NOT_EXPR:
8063 expression = build_x_unary_op (loc, unary_operator,
8064 cast_expression,
8065 complain);
8066 /* TODO: build_x_unary_op does not always honor the location,
8067 so ensure it is set. */
8068 expression.set_location (loc);
8069 break;
8071 case PREINCREMENT_EXPR:
8072 case PREDECREMENT_EXPR:
8073 non_constant_p = unary_operator == PREINCREMENT_EXPR
8074 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8075 /* Fall through. */
8076 case NEGATE_EXPR:
8077 /* Immediately fold negation of a constant, unless the constant is 0
8078 (since -0 == 0) or it would overflow. */
8079 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8080 && CONSTANT_CLASS_P (cast_expression)
8081 && !integer_zerop (cast_expression)
8082 && !TREE_OVERFLOW (cast_expression))
8084 tree folded = fold_build1 (unary_operator,
8085 TREE_TYPE (cast_expression),
8086 cast_expression);
8087 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8089 expression = cp_expr (folded, loc);
8090 break;
8093 /* Fall through. */
8094 case UNARY_PLUS_EXPR:
8095 case TRUTH_NOT_EXPR:
8096 expression = finish_unary_op_expr (loc, unary_operator,
8097 cast_expression, complain);
8098 break;
8100 default:
8101 gcc_unreachable ();
8104 if (non_constant_p != NIC_NONE
8105 && cp_parser_non_integral_constant_expression (parser,
8106 non_constant_p))
8107 expression = error_mark_node;
8109 return expression;
8112 return cp_parser_postfix_expression (parser, address_p, cast_p,
8113 /*member_access_only_p=*/false,
8114 decltype_p,
8115 pidk);
8118 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8119 unary-operator, the corresponding tree code is returned. */
8121 static enum tree_code
8122 cp_parser_unary_operator (cp_token* token)
8124 switch (token->type)
8126 case CPP_MULT:
8127 return INDIRECT_REF;
8129 case CPP_AND:
8130 return ADDR_EXPR;
8132 case CPP_PLUS:
8133 return UNARY_PLUS_EXPR;
8135 case CPP_MINUS:
8136 return NEGATE_EXPR;
8138 case CPP_NOT:
8139 return TRUTH_NOT_EXPR;
8141 case CPP_COMPL:
8142 return BIT_NOT_EXPR;
8144 default:
8145 return ERROR_MARK;
8149 /* Parse a new-expression.
8151 new-expression:
8152 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8153 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8155 Returns a representation of the expression. */
8157 static tree
8158 cp_parser_new_expression (cp_parser* parser)
8160 bool global_scope_p;
8161 vec<tree, va_gc> *placement;
8162 tree type;
8163 vec<tree, va_gc> *initializer;
8164 tree nelts = NULL_TREE;
8165 tree ret;
8167 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8169 /* Look for the optional `::' operator. */
8170 global_scope_p
8171 = (cp_parser_global_scope_opt (parser,
8172 /*current_scope_valid_p=*/false)
8173 != NULL_TREE);
8174 /* Look for the `new' operator. */
8175 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8176 /* There's no easy way to tell a new-placement from the
8177 `( type-id )' construct. */
8178 cp_parser_parse_tentatively (parser);
8179 /* Look for a new-placement. */
8180 placement = cp_parser_new_placement (parser);
8181 /* If that didn't work out, there's no new-placement. */
8182 if (!cp_parser_parse_definitely (parser))
8184 if (placement != NULL)
8185 release_tree_vector (placement);
8186 placement = NULL;
8189 /* If the next token is a `(', then we have a parenthesized
8190 type-id. */
8191 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8193 cp_token *token;
8194 const char *saved_message = parser->type_definition_forbidden_message;
8196 /* Consume the `('. */
8197 cp_lexer_consume_token (parser->lexer);
8199 /* Parse the type-id. */
8200 parser->type_definition_forbidden_message
8201 = G_("types may not be defined in a new-expression");
8203 type_id_in_expr_sentinel s (parser);
8204 type = cp_parser_type_id (parser);
8206 parser->type_definition_forbidden_message = saved_message;
8208 /* Look for the closing `)'. */
8209 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8210 token = cp_lexer_peek_token (parser->lexer);
8211 /* There should not be a direct-new-declarator in this production,
8212 but GCC used to allowed this, so we check and emit a sensible error
8213 message for this case. */
8214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8216 error_at (token->location,
8217 "array bound forbidden after parenthesized type-id");
8218 inform (token->location,
8219 "try removing the parentheses around the type-id");
8220 cp_parser_direct_new_declarator (parser);
8223 /* Otherwise, there must be a new-type-id. */
8224 else
8225 type = cp_parser_new_type_id (parser, &nelts);
8227 /* If the next token is a `(' or '{', then we have a new-initializer. */
8228 cp_token *token = cp_lexer_peek_token (parser->lexer);
8229 if (token->type == CPP_OPEN_PAREN
8230 || token->type == CPP_OPEN_BRACE)
8231 initializer = cp_parser_new_initializer (parser);
8232 else
8233 initializer = NULL;
8235 /* A new-expression may not appear in an integral constant
8236 expression. */
8237 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8238 ret = error_mark_node;
8239 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8240 of a new-type-id or type-id of a new-expression, the new-expression shall
8241 contain a new-initializer of the form ( assignment-expression )".
8242 Additionally, consistently with the spirit of DR 1467, we want to accept
8243 'new auto { 2 }' too. */
8244 else if ((ret = type_uses_auto (type))
8245 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8246 && (vec_safe_length (initializer) != 1
8247 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8248 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8250 error_at (token->location,
8251 "initialization of new-expression for type %<auto%> "
8252 "requires exactly one element");
8253 ret = error_mark_node;
8255 else
8257 /* Construct a location e.g.:
8258 ptr = new int[100]
8259 ^~~~~~~~~~~~
8260 with caret == start at the start of the "new" token, and the end
8261 at the end of the final token we consumed. */
8262 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8263 location_t end_loc = get_finish (end_tok->location);
8264 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8266 /* Create a representation of the new-expression. */
8267 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8268 tf_warning_or_error);
8269 protected_set_expr_location (ret, combined_loc);
8272 if (placement != NULL)
8273 release_tree_vector (placement);
8274 if (initializer != NULL)
8275 release_tree_vector (initializer);
8277 return ret;
8280 /* Parse a new-placement.
8282 new-placement:
8283 ( expression-list )
8285 Returns the same representation as for an expression-list. */
8287 static vec<tree, va_gc> *
8288 cp_parser_new_placement (cp_parser* parser)
8290 vec<tree, va_gc> *expression_list;
8292 /* Parse the expression-list. */
8293 expression_list = (cp_parser_parenthesized_expression_list
8294 (parser, non_attr, /*cast_p=*/false,
8295 /*allow_expansion_p=*/true,
8296 /*non_constant_p=*/NULL));
8298 if (expression_list && expression_list->is_empty ())
8299 error ("expected expression-list or type-id");
8301 return expression_list;
8304 /* Parse a new-type-id.
8306 new-type-id:
8307 type-specifier-seq new-declarator [opt]
8309 Returns the TYPE allocated. If the new-type-id indicates an array
8310 type, *NELTS is set to the number of elements in the last array
8311 bound; the TYPE will not include the last array bound. */
8313 static tree
8314 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8316 cp_decl_specifier_seq type_specifier_seq;
8317 cp_declarator *new_declarator;
8318 cp_declarator *declarator;
8319 cp_declarator *outer_declarator;
8320 const char *saved_message;
8322 /* The type-specifier sequence must not contain type definitions.
8323 (It cannot contain declarations of new types either, but if they
8324 are not definitions we will catch that because they are not
8325 complete.) */
8326 saved_message = parser->type_definition_forbidden_message;
8327 parser->type_definition_forbidden_message
8328 = G_("types may not be defined in a new-type-id");
8329 /* Parse the type-specifier-seq. */
8330 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8331 /*is_trailing_return=*/false,
8332 &type_specifier_seq);
8333 /* Restore the old message. */
8334 parser->type_definition_forbidden_message = saved_message;
8336 if (type_specifier_seq.type == error_mark_node)
8337 return error_mark_node;
8339 /* Parse the new-declarator. */
8340 new_declarator = cp_parser_new_declarator_opt (parser);
8342 /* Determine the number of elements in the last array dimension, if
8343 any. */
8344 *nelts = NULL_TREE;
8345 /* Skip down to the last array dimension. */
8346 declarator = new_declarator;
8347 outer_declarator = NULL;
8348 while (declarator && (declarator->kind == cdk_pointer
8349 || declarator->kind == cdk_ptrmem))
8351 outer_declarator = declarator;
8352 declarator = declarator->declarator;
8354 while (declarator
8355 && declarator->kind == cdk_array
8356 && declarator->declarator
8357 && declarator->declarator->kind == cdk_array)
8359 outer_declarator = declarator;
8360 declarator = declarator->declarator;
8363 if (declarator && declarator->kind == cdk_array)
8365 *nelts = declarator->u.array.bounds;
8366 if (*nelts == error_mark_node)
8367 *nelts = integer_one_node;
8369 if (outer_declarator)
8370 outer_declarator->declarator = declarator->declarator;
8371 else
8372 new_declarator = NULL;
8375 return groktypename (&type_specifier_seq, new_declarator, false);
8378 /* Parse an (optional) new-declarator.
8380 new-declarator:
8381 ptr-operator new-declarator [opt]
8382 direct-new-declarator
8384 Returns the declarator. */
8386 static cp_declarator *
8387 cp_parser_new_declarator_opt (cp_parser* parser)
8389 enum tree_code code;
8390 tree type, std_attributes = NULL_TREE;
8391 cp_cv_quals cv_quals;
8393 /* We don't know if there's a ptr-operator next, or not. */
8394 cp_parser_parse_tentatively (parser);
8395 /* Look for a ptr-operator. */
8396 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8397 /* If that worked, look for more new-declarators. */
8398 if (cp_parser_parse_definitely (parser))
8400 cp_declarator *declarator;
8402 /* Parse another optional declarator. */
8403 declarator = cp_parser_new_declarator_opt (parser);
8405 declarator = cp_parser_make_indirect_declarator
8406 (code, type, cv_quals, declarator, std_attributes);
8408 return declarator;
8411 /* If the next token is a `[', there is a direct-new-declarator. */
8412 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8413 return cp_parser_direct_new_declarator (parser);
8415 return NULL;
8418 /* Parse a direct-new-declarator.
8420 direct-new-declarator:
8421 [ expression ]
8422 direct-new-declarator [constant-expression]
8426 static cp_declarator *
8427 cp_parser_direct_new_declarator (cp_parser* parser)
8429 cp_declarator *declarator = NULL;
8431 while (true)
8433 tree expression;
8434 cp_token *token;
8436 /* Look for the opening `['. */
8437 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8439 token = cp_lexer_peek_token (parser->lexer);
8440 expression = cp_parser_expression (parser);
8441 /* The standard requires that the expression have integral
8442 type. DR 74 adds enumeration types. We believe that the
8443 real intent is that these expressions be handled like the
8444 expression in a `switch' condition, which also allows
8445 classes with a single conversion to integral or
8446 enumeration type. */
8447 if (!processing_template_decl)
8449 expression
8450 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8451 expression,
8452 /*complain=*/true);
8453 if (!expression)
8455 error_at (token->location,
8456 "expression in new-declarator must have integral "
8457 "or enumeration type");
8458 expression = error_mark_node;
8462 /* Look for the closing `]'. */
8463 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8465 /* Add this bound to the declarator. */
8466 declarator = make_array_declarator (declarator, expression);
8468 /* If the next token is not a `[', then there are no more
8469 bounds. */
8470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8471 break;
8474 return declarator;
8477 /* Parse a new-initializer.
8479 new-initializer:
8480 ( expression-list [opt] )
8481 braced-init-list
8483 Returns a representation of the expression-list. */
8485 static vec<tree, va_gc> *
8486 cp_parser_new_initializer (cp_parser* parser)
8488 vec<tree, va_gc> *expression_list;
8490 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8492 tree t;
8493 bool expr_non_constant_p;
8494 cp_lexer_set_source_position (parser->lexer);
8495 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8496 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8497 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8498 expression_list = make_tree_vector_single (t);
8500 else
8501 expression_list = (cp_parser_parenthesized_expression_list
8502 (parser, non_attr, /*cast_p=*/false,
8503 /*allow_expansion_p=*/true,
8504 /*non_constant_p=*/NULL));
8506 return expression_list;
8509 /* Parse a delete-expression.
8511 delete-expression:
8512 :: [opt] delete cast-expression
8513 :: [opt] delete [ ] cast-expression
8515 Returns a representation of the expression. */
8517 static tree
8518 cp_parser_delete_expression (cp_parser* parser)
8520 bool global_scope_p;
8521 bool array_p;
8522 tree expression;
8524 /* Look for the optional `::' operator. */
8525 global_scope_p
8526 = (cp_parser_global_scope_opt (parser,
8527 /*current_scope_valid_p=*/false)
8528 != NULL_TREE);
8529 /* Look for the `delete' keyword. */
8530 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8531 /* See if the array syntax is in use. */
8532 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8534 /* Consume the `[' token. */
8535 cp_lexer_consume_token (parser->lexer);
8536 /* Look for the `]' token. */
8537 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8538 /* Remember that this is the `[]' construct. */
8539 array_p = true;
8541 else
8542 array_p = false;
8544 /* Parse the cast-expression. */
8545 expression = cp_parser_simple_cast_expression (parser);
8547 /* A delete-expression may not appear in an integral constant
8548 expression. */
8549 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8550 return error_mark_node;
8552 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8553 tf_warning_or_error);
8556 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8557 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8558 0 otherwise. */
8560 static int
8561 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8563 cp_token *token = cp_lexer_peek_token (parser->lexer);
8564 switch (token->type)
8566 case CPP_COMMA:
8567 case CPP_SEMICOLON:
8568 case CPP_QUERY:
8569 case CPP_COLON:
8570 case CPP_CLOSE_SQUARE:
8571 case CPP_CLOSE_PAREN:
8572 case CPP_CLOSE_BRACE:
8573 case CPP_OPEN_BRACE:
8574 case CPP_DOT:
8575 case CPP_DOT_STAR:
8576 case CPP_DEREF:
8577 case CPP_DEREF_STAR:
8578 case CPP_DIV:
8579 case CPP_MOD:
8580 case CPP_LSHIFT:
8581 case CPP_RSHIFT:
8582 case CPP_LESS:
8583 case CPP_GREATER:
8584 case CPP_LESS_EQ:
8585 case CPP_GREATER_EQ:
8586 case CPP_EQ_EQ:
8587 case CPP_NOT_EQ:
8588 case CPP_EQ:
8589 case CPP_MULT_EQ:
8590 case CPP_DIV_EQ:
8591 case CPP_MOD_EQ:
8592 case CPP_PLUS_EQ:
8593 case CPP_MINUS_EQ:
8594 case CPP_RSHIFT_EQ:
8595 case CPP_LSHIFT_EQ:
8596 case CPP_AND_EQ:
8597 case CPP_XOR_EQ:
8598 case CPP_OR_EQ:
8599 case CPP_XOR:
8600 case CPP_OR:
8601 case CPP_OR_OR:
8602 case CPP_EOF:
8603 case CPP_ELLIPSIS:
8604 return 0;
8606 case CPP_OPEN_PAREN:
8607 /* In ((type ()) () the last () isn't a valid cast-expression,
8608 so the whole must be parsed as postfix-expression. */
8609 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8610 != CPP_CLOSE_PAREN;
8612 case CPP_OPEN_SQUARE:
8613 /* '[' may start a primary-expression in obj-c++ and in C++11,
8614 as a lambda-expression, eg, '(void)[]{}'. */
8615 if (cxx_dialect >= cxx11)
8616 return -1;
8617 return c_dialect_objc ();
8619 case CPP_PLUS_PLUS:
8620 case CPP_MINUS_MINUS:
8621 /* '++' and '--' may or may not start a cast-expression:
8623 struct T { void operator++(int); };
8624 void f() { (T())++; }
8628 int a;
8629 (int)++a; */
8630 return -1;
8632 default:
8633 return 1;
8637 /* Parse a cast-expression.
8639 cast-expression:
8640 unary-expression
8641 ( type-id ) cast-expression
8643 ADDRESS_P is true iff the unary-expression is appearing as the
8644 operand of the `&' operator. CAST_P is true if this expression is
8645 the target of a cast.
8647 Returns a representation of the expression. */
8649 static cp_expr
8650 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8651 bool decltype_p, cp_id_kind * pidk)
8653 /* If it's a `(', then we might be looking at a cast. */
8654 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8656 tree type = NULL_TREE;
8657 cp_expr expr (NULL_TREE);
8658 int cast_expression = 0;
8659 const char *saved_message;
8661 /* There's no way to know yet whether or not this is a cast.
8662 For example, `(int (3))' is a unary-expression, while `(int)
8663 3' is a cast. So, we resort to parsing tentatively. */
8664 cp_parser_parse_tentatively (parser);
8665 /* Types may not be defined in a cast. */
8666 saved_message = parser->type_definition_forbidden_message;
8667 parser->type_definition_forbidden_message
8668 = G_("types may not be defined in casts");
8669 /* Consume the `('. */
8670 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8671 location_t open_paren_loc = open_paren->location;
8673 /* A very tricky bit is that `(struct S) { 3 }' is a
8674 compound-literal (which we permit in C++ as an extension).
8675 But, that construct is not a cast-expression -- it is a
8676 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8677 is legal; if the compound-literal were a cast-expression,
8678 you'd need an extra set of parentheses.) But, if we parse
8679 the type-id, and it happens to be a class-specifier, then we
8680 will commit to the parse at that point, because we cannot
8681 undo the action that is done when creating a new class. So,
8682 then we cannot back up and do a postfix-expression.
8684 Another tricky case is the following (c++/29234):
8686 struct S { void operator () (); };
8688 void foo ()
8690 ( S()() );
8693 As a type-id we parse the parenthesized S()() as a function
8694 returning a function, groktypename complains and we cannot
8695 back up in this case either.
8697 Therefore, we scan ahead to the closing `)', and check to see
8698 if the tokens after the `)' can start a cast-expression. Otherwise
8699 we are dealing with an unary-expression, a postfix-expression
8700 or something else.
8702 Yet another tricky case, in C++11, is the following (c++/54891):
8704 (void)[]{};
8706 The issue is that usually, besides the case of lambda-expressions,
8707 the parenthesized type-id cannot be followed by '[', and, eg, we
8708 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8709 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8710 we don't commit, we try a cast-expression, then an unary-expression.
8712 Save tokens so that we can put them back. */
8713 cp_lexer_save_tokens (parser->lexer);
8715 /* We may be looking at a cast-expression. */
8716 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8717 /*consume_paren=*/true))
8718 cast_expression
8719 = cp_parser_tokens_start_cast_expression (parser);
8721 /* Roll back the tokens we skipped. */
8722 cp_lexer_rollback_tokens (parser->lexer);
8723 /* If we aren't looking at a cast-expression, simulate an error so
8724 that the call to cp_parser_error_occurred below returns true. */
8725 if (!cast_expression)
8726 cp_parser_simulate_error (parser);
8727 else
8729 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8730 parser->in_type_id_in_expr_p = true;
8731 /* Look for the type-id. */
8732 type = cp_parser_type_id (parser);
8733 /* Look for the closing `)'. */
8734 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8735 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8738 /* Restore the saved message. */
8739 parser->type_definition_forbidden_message = saved_message;
8741 /* At this point this can only be either a cast or a
8742 parenthesized ctor such as `(T ())' that looks like a cast to
8743 function returning T. */
8744 if (!cp_parser_error_occurred (parser))
8746 /* Only commit if the cast-expression doesn't start with
8747 '++', '--', or '[' in C++11. */
8748 if (cast_expression > 0)
8749 cp_parser_commit_to_topmost_tentative_parse (parser);
8751 expr = cp_parser_cast_expression (parser,
8752 /*address_p=*/false,
8753 /*cast_p=*/true,
8754 /*decltype_p=*/false,
8755 pidk);
8757 if (cp_parser_parse_definitely (parser))
8759 /* Warn about old-style casts, if so requested. */
8760 if (warn_old_style_cast
8761 && !in_system_header_at (input_location)
8762 && !VOID_TYPE_P (type)
8763 && current_lang_name != lang_name_c)
8764 warning (OPT_Wold_style_cast,
8765 "use of old-style cast to %qT", type);
8767 /* Only type conversions to integral or enumeration types
8768 can be used in constant-expressions. */
8769 if (!cast_valid_in_integral_constant_expression_p (type)
8770 && cp_parser_non_integral_constant_expression (parser,
8771 NIC_CAST))
8772 return error_mark_node;
8774 /* Perform the cast. */
8775 /* Make a location:
8776 (TYPE) EXPR
8777 ^~~~~~~~~~~
8778 with start==caret at the open paren, extending to the
8779 end of "expr". */
8780 location_t cast_loc = make_location (open_paren_loc,
8781 open_paren_loc,
8782 expr.get_finish ());
8783 expr = build_c_cast (cast_loc, type, expr);
8784 return expr;
8787 else
8788 cp_parser_abort_tentative_parse (parser);
8791 /* If we get here, then it's not a cast, so it must be a
8792 unary-expression. */
8793 return cp_parser_unary_expression (parser, pidk, address_p,
8794 cast_p, decltype_p);
8797 /* Parse a binary expression of the general form:
8799 pm-expression:
8800 cast-expression
8801 pm-expression .* cast-expression
8802 pm-expression ->* cast-expression
8804 multiplicative-expression:
8805 pm-expression
8806 multiplicative-expression * pm-expression
8807 multiplicative-expression / pm-expression
8808 multiplicative-expression % pm-expression
8810 additive-expression:
8811 multiplicative-expression
8812 additive-expression + multiplicative-expression
8813 additive-expression - multiplicative-expression
8815 shift-expression:
8816 additive-expression
8817 shift-expression << additive-expression
8818 shift-expression >> additive-expression
8820 relational-expression:
8821 shift-expression
8822 relational-expression < shift-expression
8823 relational-expression > shift-expression
8824 relational-expression <= shift-expression
8825 relational-expression >= shift-expression
8827 GNU Extension:
8829 relational-expression:
8830 relational-expression <? shift-expression
8831 relational-expression >? shift-expression
8833 equality-expression:
8834 relational-expression
8835 equality-expression == relational-expression
8836 equality-expression != relational-expression
8838 and-expression:
8839 equality-expression
8840 and-expression & equality-expression
8842 exclusive-or-expression:
8843 and-expression
8844 exclusive-or-expression ^ and-expression
8846 inclusive-or-expression:
8847 exclusive-or-expression
8848 inclusive-or-expression | exclusive-or-expression
8850 logical-and-expression:
8851 inclusive-or-expression
8852 logical-and-expression && inclusive-or-expression
8854 logical-or-expression:
8855 logical-and-expression
8856 logical-or-expression || logical-and-expression
8858 All these are implemented with a single function like:
8860 binary-expression:
8861 simple-cast-expression
8862 binary-expression <token> binary-expression
8864 CAST_P is true if this expression is the target of a cast.
8866 The binops_by_token map is used to get the tree codes for each <token> type.
8867 binary-expressions are associated according to a precedence table. */
8869 #define TOKEN_PRECEDENCE(token) \
8870 (((token->type == CPP_GREATER \
8871 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8872 && !parser->greater_than_is_operator_p) \
8873 ? PREC_NOT_OPERATOR \
8874 : binops_by_token[token->type].prec)
8876 static cp_expr
8877 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8878 bool no_toplevel_fold_p,
8879 bool decltype_p,
8880 enum cp_parser_prec prec,
8881 cp_id_kind * pidk)
8883 cp_parser_expression_stack stack;
8884 cp_parser_expression_stack_entry *sp = &stack[0];
8885 cp_parser_expression_stack_entry current;
8886 cp_expr rhs;
8887 cp_token *token;
8888 enum tree_code rhs_type;
8889 enum cp_parser_prec new_prec, lookahead_prec;
8890 tree overload;
8892 /* Parse the first expression. */
8893 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8894 ? TRUTH_NOT_EXPR : ERROR_MARK);
8895 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8896 cast_p, decltype_p, pidk);
8897 current.prec = prec;
8899 if (cp_parser_error_occurred (parser))
8900 return error_mark_node;
8902 for (;;)
8904 /* Get an operator token. */
8905 token = cp_lexer_peek_token (parser->lexer);
8907 if (warn_cxx11_compat
8908 && token->type == CPP_RSHIFT
8909 && !parser->greater_than_is_operator_p)
8911 if (warning_at (token->location, OPT_Wc__11_compat,
8912 "%<>>%> operator is treated"
8913 " as two right angle brackets in C++11"))
8914 inform (token->location,
8915 "suggest parentheses around %<>>%> expression");
8918 new_prec = TOKEN_PRECEDENCE (token);
8919 if (new_prec != PREC_NOT_OPERATOR
8920 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8921 /* This is a fold-expression; handle it later. */
8922 new_prec = PREC_NOT_OPERATOR;
8924 /* Popping an entry off the stack means we completed a subexpression:
8925 - either we found a token which is not an operator (`>' where it is not
8926 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8927 will happen repeatedly;
8928 - or, we found an operator which has lower priority. This is the case
8929 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8930 parsing `3 * 4'. */
8931 if (new_prec <= current.prec)
8933 if (sp == stack)
8934 break;
8935 else
8936 goto pop;
8939 get_rhs:
8940 current.tree_type = binops_by_token[token->type].tree_type;
8941 current.loc = token->location;
8943 /* We used the operator token. */
8944 cp_lexer_consume_token (parser->lexer);
8946 /* For "false && x" or "true || x", x will never be executed;
8947 disable warnings while evaluating it. */
8948 if (current.tree_type == TRUTH_ANDIF_EXPR)
8949 c_inhibit_evaluation_warnings +=
8950 cp_fully_fold (current.lhs) == truthvalue_false_node;
8951 else if (current.tree_type == TRUTH_ORIF_EXPR)
8952 c_inhibit_evaluation_warnings +=
8953 cp_fully_fold (current.lhs) == truthvalue_true_node;
8955 /* Extract another operand. It may be the RHS of this expression
8956 or the LHS of a new, higher priority expression. */
8957 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8958 ? TRUTH_NOT_EXPR : ERROR_MARK);
8959 rhs = cp_parser_simple_cast_expression (parser);
8961 /* Get another operator token. Look up its precedence to avoid
8962 building a useless (immediately popped) stack entry for common
8963 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8964 token = cp_lexer_peek_token (parser->lexer);
8965 lookahead_prec = TOKEN_PRECEDENCE (token);
8966 if (lookahead_prec != PREC_NOT_OPERATOR
8967 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8968 lookahead_prec = PREC_NOT_OPERATOR;
8969 if (lookahead_prec > new_prec)
8971 /* ... and prepare to parse the RHS of the new, higher priority
8972 expression. Since precedence levels on the stack are
8973 monotonically increasing, we do not have to care about
8974 stack overflows. */
8975 *sp = current;
8976 ++sp;
8977 current.lhs = rhs;
8978 current.lhs_type = rhs_type;
8979 current.prec = new_prec;
8980 new_prec = lookahead_prec;
8981 goto get_rhs;
8983 pop:
8984 lookahead_prec = new_prec;
8985 /* If the stack is not empty, we have parsed into LHS the right side
8986 (`4' in the example above) of an expression we had suspended.
8987 We can use the information on the stack to recover the LHS (`3')
8988 from the stack together with the tree code (`MULT_EXPR'), and
8989 the precedence of the higher level subexpression
8990 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8991 which will be used to actually build the additive expression. */
8992 rhs = current.lhs;
8993 rhs_type = current.lhs_type;
8994 --sp;
8995 current = *sp;
8998 /* Undo the disabling of warnings done above. */
8999 if (current.tree_type == TRUTH_ANDIF_EXPR)
9000 c_inhibit_evaluation_warnings -=
9001 cp_fully_fold (current.lhs) == truthvalue_false_node;
9002 else if (current.tree_type == TRUTH_ORIF_EXPR)
9003 c_inhibit_evaluation_warnings -=
9004 cp_fully_fold (current.lhs) == truthvalue_true_node;
9006 if (warn_logical_not_paren
9007 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9008 && current.lhs_type == TRUTH_NOT_EXPR
9009 /* Avoid warning for !!x == y. */
9010 && (TREE_CODE (current.lhs) != NE_EXPR
9011 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9012 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9013 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9014 /* Avoid warning for !b == y where b is boolean. */
9015 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9016 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9017 != BOOLEAN_TYPE))))
9018 /* Avoid warning for !!b == y where b is boolean. */
9019 && (!DECL_P (current.lhs)
9020 || TREE_TYPE (current.lhs) == NULL_TREE
9021 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9022 warn_logical_not_parentheses (current.loc, current.tree_type,
9023 current.lhs, maybe_constant_value (rhs));
9025 overload = NULL;
9027 location_t combined_loc = make_location (current.loc,
9028 current.lhs.get_start (),
9029 rhs.get_finish ());
9031 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9032 ERROR_MARK for everything that is not a binary expression.
9033 This makes warn_about_parentheses miss some warnings that
9034 involve unary operators. For unary expressions we should
9035 pass the correct tree_code unless the unary expression was
9036 surrounded by parentheses.
9038 if (no_toplevel_fold_p
9039 && lookahead_prec <= current.prec
9040 && sp == stack)
9041 current.lhs = build2_loc (combined_loc,
9042 current.tree_type,
9043 TREE_CODE_CLASS (current.tree_type)
9044 == tcc_comparison
9045 ? boolean_type_node : TREE_TYPE (current.lhs),
9046 current.lhs, rhs);
9047 else
9049 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9050 current.lhs, current.lhs_type,
9051 rhs, rhs_type, &overload,
9052 complain_flags (decltype_p));
9053 /* TODO: build_x_binary_op doesn't always honor the location. */
9054 current.lhs.set_location (combined_loc);
9056 current.lhs_type = current.tree_type;
9058 /* If the binary operator required the use of an overloaded operator,
9059 then this expression cannot be an integral constant-expression.
9060 An overloaded operator can be used even if both operands are
9061 otherwise permissible in an integral constant-expression if at
9062 least one of the operands is of enumeration type. */
9064 if (overload
9065 && cp_parser_non_integral_constant_expression (parser,
9066 NIC_OVERLOADED))
9067 return error_mark_node;
9070 return current.lhs;
9073 static cp_expr
9074 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9075 bool no_toplevel_fold_p,
9076 enum cp_parser_prec prec,
9077 cp_id_kind * pidk)
9079 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9080 /*decltype*/false, prec, pidk);
9083 /* Parse the `? expression : assignment-expression' part of a
9084 conditional-expression. The LOGICAL_OR_EXPR is the
9085 logical-or-expression that started the conditional-expression.
9086 Returns a representation of the entire conditional-expression.
9088 This routine is used by cp_parser_assignment_expression.
9090 ? expression : assignment-expression
9092 GNU Extensions:
9094 ? : assignment-expression */
9096 static tree
9097 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9099 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9100 cp_expr assignment_expr;
9101 struct cp_token *token;
9102 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9104 /* Consume the `?' token. */
9105 cp_lexer_consume_token (parser->lexer);
9106 token = cp_lexer_peek_token (parser->lexer);
9107 if (cp_parser_allow_gnu_extensions_p (parser)
9108 && token->type == CPP_COLON)
9110 pedwarn (token->location, OPT_Wpedantic,
9111 "ISO C++ does not allow ?: with omitted middle operand");
9112 /* Implicit true clause. */
9113 expr = NULL_TREE;
9114 c_inhibit_evaluation_warnings +=
9115 folded_logical_or_expr == truthvalue_true_node;
9116 warn_for_omitted_condop (token->location, logical_or_expr);
9118 else
9120 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9121 parser->colon_corrects_to_scope_p = false;
9122 /* Parse the expression. */
9123 c_inhibit_evaluation_warnings +=
9124 folded_logical_or_expr == truthvalue_false_node;
9125 expr = cp_parser_expression (parser);
9126 c_inhibit_evaluation_warnings +=
9127 ((folded_logical_or_expr == truthvalue_true_node)
9128 - (folded_logical_or_expr == truthvalue_false_node));
9129 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9132 /* The next token should be a `:'. */
9133 cp_parser_require (parser, CPP_COLON, RT_COLON);
9134 /* Parse the assignment-expression. */
9135 assignment_expr = cp_parser_assignment_expression (parser);
9136 c_inhibit_evaluation_warnings -=
9137 folded_logical_or_expr == truthvalue_true_node;
9139 /* Make a location:
9140 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9141 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9142 with the caret at the "?", ranging from the start of
9143 the logical_or_expr to the end of the assignment_expr. */
9144 loc = make_location (loc,
9145 logical_or_expr.get_start (),
9146 assignment_expr.get_finish ());
9148 /* Build the conditional-expression. */
9149 return build_x_conditional_expr (loc, logical_or_expr,
9150 expr,
9151 assignment_expr,
9152 tf_warning_or_error);
9155 /* Parse an assignment-expression.
9157 assignment-expression:
9158 conditional-expression
9159 logical-or-expression assignment-operator assignment_expression
9160 throw-expression
9162 CAST_P is true if this expression is the target of a cast.
9163 DECLTYPE_P is true if this expression is the operand of decltype.
9165 Returns a representation for the expression. */
9167 static cp_expr
9168 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9169 bool cast_p, bool decltype_p)
9171 cp_expr expr;
9173 /* If the next token is the `throw' keyword, then we're looking at
9174 a throw-expression. */
9175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9176 expr = cp_parser_throw_expression (parser);
9177 /* Otherwise, it must be that we are looking at a
9178 logical-or-expression. */
9179 else
9181 /* Parse the binary expressions (logical-or-expression). */
9182 expr = cp_parser_binary_expression (parser, cast_p, false,
9183 decltype_p,
9184 PREC_NOT_OPERATOR, pidk);
9185 /* If the next token is a `?' then we're actually looking at a
9186 conditional-expression. */
9187 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9188 return cp_parser_question_colon_clause (parser, expr);
9189 else
9191 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9193 /* If it's an assignment-operator, we're using the second
9194 production. */
9195 enum tree_code assignment_operator
9196 = cp_parser_assignment_operator_opt (parser);
9197 if (assignment_operator != ERROR_MARK)
9199 bool non_constant_p;
9201 /* Parse the right-hand side of the assignment. */
9202 cp_expr rhs = cp_parser_initializer_clause (parser,
9203 &non_constant_p);
9205 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9206 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9208 /* An assignment may not appear in a
9209 constant-expression. */
9210 if (cp_parser_non_integral_constant_expression (parser,
9211 NIC_ASSIGNMENT))
9212 return error_mark_node;
9213 /* Build the assignment expression. Its default
9214 location:
9215 LHS = RHS
9216 ~~~~^~~~~
9217 is the location of the '=' token as the
9218 caret, ranging from the start of the lhs to the
9219 end of the rhs. */
9220 loc = make_location (loc,
9221 expr.get_start (),
9222 rhs.get_finish ());
9223 expr = build_x_modify_expr (loc, expr,
9224 assignment_operator,
9225 rhs,
9226 complain_flags (decltype_p));
9227 /* TODO: build_x_modify_expr doesn't honor the location,
9228 so we must set it here. */
9229 expr.set_location (loc);
9234 return expr;
9237 /* Parse an (optional) assignment-operator.
9239 assignment-operator: one of
9240 = *= /= %= += -= >>= <<= &= ^= |=
9242 GNU Extension:
9244 assignment-operator: one of
9245 <?= >?=
9247 If the next token is an assignment operator, the corresponding tree
9248 code is returned, and the token is consumed. For example, for
9249 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9250 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9251 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9252 operator, ERROR_MARK is returned. */
9254 static enum tree_code
9255 cp_parser_assignment_operator_opt (cp_parser* parser)
9257 enum tree_code op;
9258 cp_token *token;
9260 /* Peek at the next token. */
9261 token = cp_lexer_peek_token (parser->lexer);
9263 switch (token->type)
9265 case CPP_EQ:
9266 op = NOP_EXPR;
9267 break;
9269 case CPP_MULT_EQ:
9270 op = MULT_EXPR;
9271 break;
9273 case CPP_DIV_EQ:
9274 op = TRUNC_DIV_EXPR;
9275 break;
9277 case CPP_MOD_EQ:
9278 op = TRUNC_MOD_EXPR;
9279 break;
9281 case CPP_PLUS_EQ:
9282 op = PLUS_EXPR;
9283 break;
9285 case CPP_MINUS_EQ:
9286 op = MINUS_EXPR;
9287 break;
9289 case CPP_RSHIFT_EQ:
9290 op = RSHIFT_EXPR;
9291 break;
9293 case CPP_LSHIFT_EQ:
9294 op = LSHIFT_EXPR;
9295 break;
9297 case CPP_AND_EQ:
9298 op = BIT_AND_EXPR;
9299 break;
9301 case CPP_XOR_EQ:
9302 op = BIT_XOR_EXPR;
9303 break;
9305 case CPP_OR_EQ:
9306 op = BIT_IOR_EXPR;
9307 break;
9309 default:
9310 /* Nothing else is an assignment operator. */
9311 op = ERROR_MARK;
9314 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9315 if (op != ERROR_MARK
9316 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9317 op = ERROR_MARK;
9319 /* If it was an assignment operator, consume it. */
9320 if (op != ERROR_MARK)
9321 cp_lexer_consume_token (parser->lexer);
9323 return op;
9326 /* Parse an expression.
9328 expression:
9329 assignment-expression
9330 expression , assignment-expression
9332 CAST_P is true if this expression is the target of a cast.
9333 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9334 except possibly parenthesized or on the RHS of a comma (N3276).
9336 Returns a representation of the expression. */
9338 static cp_expr
9339 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9340 bool cast_p, bool decltype_p)
9342 cp_expr expression = NULL_TREE;
9343 location_t loc = UNKNOWN_LOCATION;
9345 while (true)
9347 cp_expr assignment_expression;
9349 /* Parse the next assignment-expression. */
9350 assignment_expression
9351 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9353 /* We don't create a temporary for a call that is the immediate operand
9354 of decltype or on the RHS of a comma. But when we see a comma, we
9355 need to create a temporary for a call on the LHS. */
9356 if (decltype_p && !processing_template_decl
9357 && TREE_CODE (assignment_expression) == CALL_EXPR
9358 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9359 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9360 assignment_expression
9361 = build_cplus_new (TREE_TYPE (assignment_expression),
9362 assignment_expression, tf_warning_or_error);
9364 /* If this is the first assignment-expression, we can just
9365 save it away. */
9366 if (!expression)
9367 expression = assignment_expression;
9368 else
9370 /* Create a location with caret at the comma, ranging
9371 from the start of the LHS to the end of the RHS. */
9372 loc = make_location (loc,
9373 expression.get_start (),
9374 assignment_expression.get_finish ());
9375 expression = build_x_compound_expr (loc, expression,
9376 assignment_expression,
9377 complain_flags (decltype_p));
9378 expression.set_location (loc);
9380 /* If the next token is not a comma, or we're in a fold-expression, then
9381 we are done with the expression. */
9382 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9383 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9384 break;
9385 /* Consume the `,'. */
9386 loc = cp_lexer_peek_token (parser->lexer)->location;
9387 cp_lexer_consume_token (parser->lexer);
9388 /* A comma operator cannot appear in a constant-expression. */
9389 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9390 expression = error_mark_node;
9393 return expression;
9396 /* Parse a constant-expression.
9398 constant-expression:
9399 conditional-expression
9401 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9402 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9403 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9404 is false, NON_CONSTANT_P should be NULL. */
9406 static cp_expr
9407 cp_parser_constant_expression (cp_parser* parser,
9408 bool allow_non_constant_p,
9409 bool *non_constant_p)
9411 bool saved_integral_constant_expression_p;
9412 bool saved_allow_non_integral_constant_expression_p;
9413 bool saved_non_integral_constant_expression_p;
9414 cp_expr expression;
9416 /* It might seem that we could simply parse the
9417 conditional-expression, and then check to see if it were
9418 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9419 one that the compiler can figure out is constant, possibly after
9420 doing some simplifications or optimizations. The standard has a
9421 precise definition of constant-expression, and we must honor
9422 that, even though it is somewhat more restrictive.
9424 For example:
9426 int i[(2, 3)];
9428 is not a legal declaration, because `(2, 3)' is not a
9429 constant-expression. The `,' operator is forbidden in a
9430 constant-expression. However, GCC's constant-folding machinery
9431 will fold this operation to an INTEGER_CST for `3'. */
9433 /* Save the old settings. */
9434 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9435 saved_allow_non_integral_constant_expression_p
9436 = parser->allow_non_integral_constant_expression_p;
9437 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9438 /* We are now parsing a constant-expression. */
9439 parser->integral_constant_expression_p = true;
9440 parser->allow_non_integral_constant_expression_p
9441 = (allow_non_constant_p || cxx_dialect >= cxx11);
9442 parser->non_integral_constant_expression_p = false;
9443 /* Although the grammar says "conditional-expression", we parse an
9444 "assignment-expression", which also permits "throw-expression"
9445 and the use of assignment operators. In the case that
9446 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9447 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9448 actually essential that we look for an assignment-expression.
9449 For example, cp_parser_initializer_clauses uses this function to
9450 determine whether a particular assignment-expression is in fact
9451 constant. */
9452 expression = cp_parser_assignment_expression (parser);
9453 /* Restore the old settings. */
9454 parser->integral_constant_expression_p
9455 = saved_integral_constant_expression_p;
9456 parser->allow_non_integral_constant_expression_p
9457 = saved_allow_non_integral_constant_expression_p;
9458 if (cxx_dialect >= cxx11)
9460 /* Require an rvalue constant expression here; that's what our
9461 callers expect. Reference constant expressions are handled
9462 separately in e.g. cp_parser_template_argument. */
9463 bool is_const = potential_rvalue_constant_expression (expression);
9464 parser->non_integral_constant_expression_p = !is_const;
9465 if (!is_const && !allow_non_constant_p)
9466 require_potential_rvalue_constant_expression (expression);
9468 if (allow_non_constant_p)
9469 *non_constant_p = parser->non_integral_constant_expression_p;
9470 parser->non_integral_constant_expression_p
9471 = saved_non_integral_constant_expression_p;
9473 return expression;
9476 /* Parse __builtin_offsetof.
9478 offsetof-expression:
9479 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9481 offsetof-member-designator:
9482 id-expression
9483 | offsetof-member-designator "." id-expression
9484 | offsetof-member-designator "[" expression "]"
9485 | offsetof-member-designator "->" id-expression */
9487 static cp_expr
9488 cp_parser_builtin_offsetof (cp_parser *parser)
9490 int save_ice_p, save_non_ice_p;
9491 tree type;
9492 cp_expr expr;
9493 cp_id_kind dummy;
9494 cp_token *token;
9495 location_t finish_loc;
9497 /* We're about to accept non-integral-constant things, but will
9498 definitely yield an integral constant expression. Save and
9499 restore these values around our local parsing. */
9500 save_ice_p = parser->integral_constant_expression_p;
9501 save_non_ice_p = parser->non_integral_constant_expression_p;
9503 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9505 /* Consume the "__builtin_offsetof" token. */
9506 cp_lexer_consume_token (parser->lexer);
9507 /* Consume the opening `('. */
9508 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9509 /* Parse the type-id. */
9510 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9511 type = cp_parser_type_id (parser);
9512 /* Look for the `,'. */
9513 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9514 token = cp_lexer_peek_token (parser->lexer);
9516 /* Build the (type *)null that begins the traditional offsetof macro. */
9517 tree object_ptr
9518 = build_static_cast (build_pointer_type (type), null_pointer_node,
9519 tf_warning_or_error);
9521 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9522 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9523 true, &dummy, token->location);
9524 while (true)
9526 token = cp_lexer_peek_token (parser->lexer);
9527 switch (token->type)
9529 case CPP_OPEN_SQUARE:
9530 /* offsetof-member-designator "[" expression "]" */
9531 expr = cp_parser_postfix_open_square_expression (parser, expr,
9532 true, false);
9533 break;
9535 case CPP_DEREF:
9536 /* offsetof-member-designator "->" identifier */
9537 expr = grok_array_decl (token->location, expr,
9538 integer_zero_node, false);
9539 /* FALLTHRU */
9541 case CPP_DOT:
9542 /* offsetof-member-designator "." identifier */
9543 cp_lexer_consume_token (parser->lexer);
9544 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9545 expr, true, &dummy,
9546 token->location);
9547 break;
9549 case CPP_CLOSE_PAREN:
9550 /* Consume the ")" token. */
9551 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9552 cp_lexer_consume_token (parser->lexer);
9553 goto success;
9555 default:
9556 /* Error. We know the following require will fail, but
9557 that gives the proper error message. */
9558 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9559 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9560 expr = error_mark_node;
9561 goto failure;
9565 success:
9566 /* Make a location of the form:
9567 __builtin_offsetof (struct s, f)
9568 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9569 with caret at the type-id, ranging from the start of the
9570 "_builtin_offsetof" token to the close paren. */
9571 loc = make_location (loc, start_loc, finish_loc);
9572 /* The result will be an INTEGER_CST, so we need to explicitly
9573 preserve the location. */
9574 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9576 failure:
9577 parser->integral_constant_expression_p = save_ice_p;
9578 parser->non_integral_constant_expression_p = save_non_ice_p;
9580 return expr;
9583 /* Parse a trait expression.
9585 Returns a representation of the expression, the underlying type
9586 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9588 static tree
9589 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9591 cp_trait_kind kind;
9592 tree type1, type2 = NULL_TREE;
9593 bool binary = false;
9594 bool variadic = false;
9596 switch (keyword)
9598 case RID_HAS_NOTHROW_ASSIGN:
9599 kind = CPTK_HAS_NOTHROW_ASSIGN;
9600 break;
9601 case RID_HAS_NOTHROW_CONSTRUCTOR:
9602 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9603 break;
9604 case RID_HAS_NOTHROW_COPY:
9605 kind = CPTK_HAS_NOTHROW_COPY;
9606 break;
9607 case RID_HAS_TRIVIAL_ASSIGN:
9608 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9609 break;
9610 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9611 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9612 break;
9613 case RID_HAS_TRIVIAL_COPY:
9614 kind = CPTK_HAS_TRIVIAL_COPY;
9615 break;
9616 case RID_HAS_TRIVIAL_DESTRUCTOR:
9617 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9618 break;
9619 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9620 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9621 break;
9622 case RID_HAS_VIRTUAL_DESTRUCTOR:
9623 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9624 break;
9625 case RID_IS_ABSTRACT:
9626 kind = CPTK_IS_ABSTRACT;
9627 break;
9628 case RID_IS_AGGREGATE:
9629 kind = CPTK_IS_AGGREGATE;
9630 break;
9631 case RID_IS_BASE_OF:
9632 kind = CPTK_IS_BASE_OF;
9633 binary = true;
9634 break;
9635 case RID_IS_CLASS:
9636 kind = CPTK_IS_CLASS;
9637 break;
9638 case RID_IS_EMPTY:
9639 kind = CPTK_IS_EMPTY;
9640 break;
9641 case RID_IS_ENUM:
9642 kind = CPTK_IS_ENUM;
9643 break;
9644 case RID_IS_FINAL:
9645 kind = CPTK_IS_FINAL;
9646 break;
9647 case RID_IS_LITERAL_TYPE:
9648 kind = CPTK_IS_LITERAL_TYPE;
9649 break;
9650 case RID_IS_POD:
9651 kind = CPTK_IS_POD;
9652 break;
9653 case RID_IS_POLYMORPHIC:
9654 kind = CPTK_IS_POLYMORPHIC;
9655 break;
9656 case RID_IS_SAME_AS:
9657 kind = CPTK_IS_SAME_AS;
9658 binary = true;
9659 break;
9660 case RID_IS_STD_LAYOUT:
9661 kind = CPTK_IS_STD_LAYOUT;
9662 break;
9663 case RID_IS_TRIVIAL:
9664 kind = CPTK_IS_TRIVIAL;
9665 break;
9666 case RID_IS_TRIVIALLY_ASSIGNABLE:
9667 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9668 binary = true;
9669 break;
9670 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9671 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9672 variadic = true;
9673 break;
9674 case RID_IS_TRIVIALLY_COPYABLE:
9675 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9676 break;
9677 case RID_IS_UNION:
9678 kind = CPTK_IS_UNION;
9679 break;
9680 case RID_UNDERLYING_TYPE:
9681 kind = CPTK_UNDERLYING_TYPE;
9682 break;
9683 case RID_BASES:
9684 kind = CPTK_BASES;
9685 break;
9686 case RID_DIRECT_BASES:
9687 kind = CPTK_DIRECT_BASES;
9688 break;
9689 default:
9690 gcc_unreachable ();
9693 /* Consume the token. */
9694 cp_lexer_consume_token (parser->lexer);
9696 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9699 type_id_in_expr_sentinel s (parser);
9700 type1 = cp_parser_type_id (parser);
9703 if (type1 == error_mark_node)
9704 return error_mark_node;
9706 if (binary)
9708 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9711 type_id_in_expr_sentinel s (parser);
9712 type2 = cp_parser_type_id (parser);
9715 if (type2 == error_mark_node)
9716 return error_mark_node;
9718 else if (variadic)
9720 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9722 cp_lexer_consume_token (parser->lexer);
9723 tree elt = cp_parser_type_id (parser);
9724 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9726 cp_lexer_consume_token (parser->lexer);
9727 elt = make_pack_expansion (elt);
9729 if (elt == error_mark_node)
9730 return error_mark_node;
9731 type2 = tree_cons (NULL_TREE, elt, type2);
9735 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9737 /* Complete the trait expression, which may mean either processing
9738 the trait expr now or saving it for template instantiation. */
9739 switch (kind)
9741 case CPTK_UNDERLYING_TYPE:
9742 return finish_underlying_type (type1);
9743 case CPTK_BASES:
9744 return finish_bases (type1, false);
9745 case CPTK_DIRECT_BASES:
9746 return finish_bases (type1, true);
9747 default:
9748 return finish_trait_expr (kind, type1, type2);
9752 /* Lambdas that appear in variable initializer or default argument scope
9753 get that in their mangling, so we need to record it. We might as well
9754 use the count for function and namespace scopes as well. */
9755 static GTY(()) tree lambda_scope;
9756 static GTY(()) int lambda_count;
9757 struct GTY(()) tree_int
9759 tree t;
9760 int i;
9762 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9764 static void
9765 start_lambda_scope (tree decl)
9767 tree_int ti;
9768 gcc_assert (decl);
9769 /* Once we're inside a function, we ignore other scopes and just push
9770 the function again so that popping works properly. */
9771 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9772 decl = current_function_decl;
9773 ti.t = lambda_scope;
9774 ti.i = lambda_count;
9775 vec_safe_push (lambda_scope_stack, ti);
9776 if (lambda_scope != decl)
9778 /* Don't reset the count if we're still in the same function. */
9779 lambda_scope = decl;
9780 lambda_count = 0;
9784 static void
9785 record_lambda_scope (tree lambda)
9787 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9788 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9791 static void
9792 finish_lambda_scope (void)
9794 tree_int *p = &lambda_scope_stack->last ();
9795 if (lambda_scope != p->t)
9797 lambda_scope = p->t;
9798 lambda_count = p->i;
9800 lambda_scope_stack->pop ();
9803 /* Parse a lambda expression.
9805 lambda-expression:
9806 lambda-introducer lambda-declarator [opt] compound-statement
9808 Returns a representation of the expression. */
9810 static cp_expr
9811 cp_parser_lambda_expression (cp_parser* parser)
9813 tree lambda_expr = build_lambda_expr ();
9814 tree type;
9815 bool ok = true;
9816 cp_token *token = cp_lexer_peek_token (parser->lexer);
9817 cp_token_position start = 0;
9819 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9821 if (cp_unevaluated_operand)
9823 if (!token->error_reported)
9825 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9826 "lambda-expression in unevaluated context");
9827 token->error_reported = true;
9829 ok = false;
9831 else if (parser->in_template_argument_list_p)
9833 if (!token->error_reported)
9835 error_at (token->location, "lambda-expression in template-argument");
9836 token->error_reported = true;
9838 ok = false;
9841 /* We may be in the middle of deferred access check. Disable
9842 it now. */
9843 push_deferring_access_checks (dk_no_deferred);
9845 cp_parser_lambda_introducer (parser, lambda_expr);
9847 type = begin_lambda_type (lambda_expr);
9848 if (type == error_mark_node)
9849 return error_mark_node;
9851 record_lambda_scope (lambda_expr);
9853 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9854 determine_visibility (TYPE_NAME (type));
9856 /* Now that we've started the type, add the capture fields for any
9857 explicit captures. */
9858 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9861 /* Inside the class, surrounding template-parameter-lists do not apply. */
9862 unsigned int saved_num_template_parameter_lists
9863 = parser->num_template_parameter_lists;
9864 unsigned char in_statement = parser->in_statement;
9865 bool in_switch_statement_p = parser->in_switch_statement_p;
9866 bool fully_implicit_function_template_p
9867 = parser->fully_implicit_function_template_p;
9868 tree implicit_template_parms = parser->implicit_template_parms;
9869 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9870 bool auto_is_implicit_function_template_parm_p
9871 = parser->auto_is_implicit_function_template_parm_p;
9873 parser->num_template_parameter_lists = 0;
9874 parser->in_statement = 0;
9875 parser->in_switch_statement_p = false;
9876 parser->fully_implicit_function_template_p = false;
9877 parser->implicit_template_parms = 0;
9878 parser->implicit_template_scope = 0;
9879 parser->auto_is_implicit_function_template_parm_p = false;
9881 /* By virtue of defining a local class, a lambda expression has access to
9882 the private variables of enclosing classes. */
9884 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9886 if (ok && cp_parser_error_occurred (parser))
9887 ok = false;
9889 if (ok)
9891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9892 && cp_parser_start_tentative_firewall (parser))
9893 start = token;
9894 cp_parser_lambda_body (parser, lambda_expr);
9896 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9898 if (cp_parser_skip_to_closing_brace (parser))
9899 cp_lexer_consume_token (parser->lexer);
9902 /* The capture list was built up in reverse order; fix that now. */
9903 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9904 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9906 if (ok)
9907 maybe_add_lambda_conv_op (type);
9909 type = finish_struct (type, /*attributes=*/NULL_TREE);
9911 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9912 parser->in_statement = in_statement;
9913 parser->in_switch_statement_p = in_switch_statement_p;
9914 parser->fully_implicit_function_template_p
9915 = fully_implicit_function_template_p;
9916 parser->implicit_template_parms = implicit_template_parms;
9917 parser->implicit_template_scope = implicit_template_scope;
9918 parser->auto_is_implicit_function_template_parm_p
9919 = auto_is_implicit_function_template_parm_p;
9922 /* This field is only used during parsing of the lambda. */
9923 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9925 /* This lambda shouldn't have any proxies left at this point. */
9926 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9927 /* And now that we're done, push proxies for an enclosing lambda. */
9928 insert_pending_capture_proxies ();
9930 if (ok)
9931 lambda_expr = build_lambda_object (lambda_expr);
9932 else
9933 lambda_expr = error_mark_node;
9935 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9937 pop_deferring_access_checks ();
9939 return lambda_expr;
9942 /* Parse the beginning of a lambda expression.
9944 lambda-introducer:
9945 [ lambda-capture [opt] ]
9947 LAMBDA_EXPR is the current representation of the lambda expression. */
9949 static void
9950 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9952 /* Need commas after the first capture. */
9953 bool first = true;
9955 /* Eat the leading `['. */
9956 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9958 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9959 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9960 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9961 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9962 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9963 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9965 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9967 cp_lexer_consume_token (parser->lexer);
9968 first = false;
9971 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9973 cp_token* capture_token;
9974 tree capture_id;
9975 tree capture_init_expr;
9976 cp_id_kind idk = CP_ID_KIND_NONE;
9977 bool explicit_init_p = false;
9979 enum capture_kind_type
9981 BY_COPY,
9982 BY_REFERENCE
9984 enum capture_kind_type capture_kind = BY_COPY;
9986 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9988 error ("expected end of capture-list");
9989 return;
9992 if (first)
9993 first = false;
9994 else
9995 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9997 /* Possibly capture `this'. */
9998 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10000 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10001 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10002 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10003 "with by-copy capture default");
10004 cp_lexer_consume_token (parser->lexer);
10005 add_capture (lambda_expr,
10006 /*id=*/this_identifier,
10007 /*initializer=*/finish_this_expr (),
10008 /*by_reference_p=*/true,
10009 explicit_init_p);
10010 continue;
10013 /* Possibly capture `*this'. */
10014 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10015 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10017 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10018 if (cxx_dialect < cxx1z)
10019 pedwarn (loc, 0, "%<*this%> capture only available with "
10020 "-std=c++1z or -std=gnu++1z");
10021 cp_lexer_consume_token (parser->lexer);
10022 cp_lexer_consume_token (parser->lexer);
10023 add_capture (lambda_expr,
10024 /*id=*/this_identifier,
10025 /*initializer=*/finish_this_expr (),
10026 /*by_reference_p=*/false,
10027 explicit_init_p);
10028 continue;
10031 /* Remember whether we want to capture as a reference or not. */
10032 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10034 capture_kind = BY_REFERENCE;
10035 cp_lexer_consume_token (parser->lexer);
10038 /* Get the identifier. */
10039 capture_token = cp_lexer_peek_token (parser->lexer);
10040 capture_id = cp_parser_identifier (parser);
10042 if (capture_id == error_mark_node)
10043 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10044 delimiters, but I modified this to stop on unnested ']' as well. It
10045 was already changed to stop on unnested '}', so the
10046 "closing_parenthesis" name is no more misleading with my change. */
10048 cp_parser_skip_to_closing_parenthesis (parser,
10049 /*recovering=*/true,
10050 /*or_comma=*/true,
10051 /*consume_paren=*/true);
10052 break;
10055 /* Find the initializer for this capture. */
10056 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10057 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10058 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10060 bool direct, non_constant;
10061 /* An explicit initializer exists. */
10062 if (cxx_dialect < cxx14)
10063 pedwarn (input_location, 0,
10064 "lambda capture initializers "
10065 "only available with -std=c++14 or -std=gnu++14");
10066 capture_init_expr = cp_parser_initializer (parser, &direct,
10067 &non_constant);
10068 explicit_init_p = true;
10069 if (capture_init_expr == NULL_TREE)
10071 error ("empty initializer for lambda init-capture");
10072 capture_init_expr = error_mark_node;
10075 else
10077 const char* error_msg;
10079 /* Turn the identifier into an id-expression. */
10080 capture_init_expr
10081 = cp_parser_lookup_name_simple (parser, capture_id,
10082 capture_token->location);
10084 if (capture_init_expr == error_mark_node)
10086 unqualified_name_lookup_error (capture_id);
10087 continue;
10089 else if (DECL_P (capture_init_expr)
10090 && (!VAR_P (capture_init_expr)
10091 && TREE_CODE (capture_init_expr) != PARM_DECL))
10093 error_at (capture_token->location,
10094 "capture of non-variable %qD ",
10095 capture_init_expr);
10096 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10097 "%q#D declared here", capture_init_expr);
10098 continue;
10100 if (VAR_P (capture_init_expr)
10101 && decl_storage_duration (capture_init_expr) != dk_auto)
10103 if (pedwarn (capture_token->location, 0, "capture of variable "
10104 "%qD with non-automatic storage duration",
10105 capture_init_expr))
10106 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10107 "%q#D declared here", capture_init_expr);
10108 continue;
10111 capture_init_expr
10112 = finish_id_expression
10113 (capture_id,
10114 capture_init_expr,
10115 parser->scope,
10116 &idk,
10117 /*integral_constant_expression_p=*/false,
10118 /*allow_non_integral_constant_expression_p=*/false,
10119 /*non_integral_constant_expression_p=*/NULL,
10120 /*template_p=*/false,
10121 /*done=*/true,
10122 /*address_p=*/false,
10123 /*template_arg_p=*/false,
10124 &error_msg,
10125 capture_token->location);
10127 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10129 cp_lexer_consume_token (parser->lexer);
10130 capture_init_expr = make_pack_expansion (capture_init_expr);
10132 else
10133 check_for_bare_parameter_packs (capture_init_expr);
10136 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10137 && !explicit_init_p)
10139 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10140 && capture_kind == BY_COPY)
10141 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10142 "of %qD redundant with by-copy capture default",
10143 capture_id);
10144 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10145 && capture_kind == BY_REFERENCE)
10146 pedwarn (capture_token->location, 0, "explicit by-reference "
10147 "capture of %qD redundant with by-reference capture "
10148 "default", capture_id);
10151 add_capture (lambda_expr,
10152 capture_id,
10153 capture_init_expr,
10154 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10155 explicit_init_p);
10158 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10161 /* Parse the (optional) middle of a lambda expression.
10163 lambda-declarator:
10164 < template-parameter-list [opt] >
10165 ( parameter-declaration-clause [opt] )
10166 attribute-specifier [opt]
10167 decl-specifier-seq [opt]
10168 exception-specification [opt]
10169 lambda-return-type-clause [opt]
10171 LAMBDA_EXPR is the current representation of the lambda expression. */
10173 static bool
10174 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10176 /* 5.1.1.4 of the standard says:
10177 If a lambda-expression does not include a lambda-declarator, it is as if
10178 the lambda-declarator were ().
10179 This means an empty parameter list, no attributes, and no exception
10180 specification. */
10181 tree param_list = void_list_node;
10182 tree attributes = NULL_TREE;
10183 tree exception_spec = NULL_TREE;
10184 tree template_param_list = NULL_TREE;
10185 tree tx_qual = NULL_TREE;
10186 cp_decl_specifier_seq lambda_specs;
10187 clear_decl_specs (&lambda_specs);
10189 /* The template-parameter-list is optional, but must begin with
10190 an opening angle if present. */
10191 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10193 if (cxx_dialect < cxx14)
10194 pedwarn (parser->lexer->next_token->location, 0,
10195 "lambda templates are only available with "
10196 "-std=c++14 or -std=gnu++14");
10197 else
10198 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10199 "ISO C++ does not support lambda templates");
10201 cp_lexer_consume_token (parser->lexer);
10203 template_param_list = cp_parser_template_parameter_list (parser);
10205 cp_parser_skip_to_end_of_template_parameter_list (parser);
10207 /* We just processed one more parameter list. */
10208 ++parser->num_template_parameter_lists;
10211 /* The parameter-declaration-clause is optional (unless
10212 template-parameter-list was given), but must begin with an
10213 opening parenthesis if present. */
10214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10216 cp_lexer_consume_token (parser->lexer);
10218 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10220 /* Parse parameters. */
10221 param_list = cp_parser_parameter_declaration_clause (parser);
10223 /* Default arguments shall not be specified in the
10224 parameter-declaration-clause of a lambda-declarator. */
10225 if (cxx_dialect < cxx14)
10226 for (tree t = param_list; t; t = TREE_CHAIN (t))
10227 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10228 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10229 "default argument specified for lambda parameter");
10231 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10233 attributes = cp_parser_attributes_opt (parser);
10235 /* In the decl-specifier-seq of the lambda-declarator, each
10236 decl-specifier shall either be mutable or constexpr. */
10237 int declares_class_or_enum;
10238 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10239 cp_parser_decl_specifier_seq (parser,
10240 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10241 &lambda_specs, &declares_class_or_enum);
10242 if (lambda_specs.storage_class == sc_mutable)
10244 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10245 if (lambda_specs.conflicting_specifiers_p)
10246 error_at (lambda_specs.locations[ds_storage_class],
10247 "duplicate %<mutable%>");
10250 tx_qual = cp_parser_tx_qualifier_opt (parser);
10252 /* Parse optional exception specification. */
10253 exception_spec = cp_parser_exception_specification_opt (parser);
10255 /* Parse optional trailing return type. */
10256 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10258 cp_lexer_consume_token (parser->lexer);
10259 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10260 = cp_parser_trailing_type_id (parser);
10263 /* The function parameters must be in scope all the way until after the
10264 trailing-return-type in case of decltype. */
10265 pop_bindings_and_leave_scope ();
10267 else if (template_param_list != NULL_TREE) // generate diagnostic
10268 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10270 /* Create the function call operator.
10272 Messing with declarators like this is no uglier than building up the
10273 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10274 other code. */
10276 cp_decl_specifier_seq return_type_specs;
10277 cp_declarator* declarator;
10278 tree fco;
10279 int quals;
10280 void *p;
10282 clear_decl_specs (&return_type_specs);
10283 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10284 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10285 else
10286 /* Maybe we will deduce the return type later. */
10287 return_type_specs.type = make_auto ();
10289 if (lambda_specs.locations[ds_constexpr])
10291 if (cxx_dialect >= cxx1z)
10292 return_type_specs.locations[ds_constexpr]
10293 = lambda_specs.locations[ds_constexpr];
10294 else
10295 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10296 "lambda only available with -std=c++1z or -std=gnu++1z");
10299 p = obstack_alloc (&declarator_obstack, 0);
10301 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10302 sfk_none);
10304 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10305 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10306 declarator = make_call_declarator (declarator, param_list, quals,
10307 VIRT_SPEC_UNSPECIFIED,
10308 REF_QUAL_NONE,
10309 tx_qual,
10310 exception_spec,
10311 /*late_return_type=*/NULL_TREE,
10312 /*requires_clause*/NULL_TREE);
10313 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10315 fco = grokmethod (&return_type_specs,
10316 declarator,
10317 attributes);
10318 if (fco != error_mark_node)
10320 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10321 DECL_ARTIFICIAL (fco) = 1;
10322 /* Give the object parameter a different name. */
10323 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10324 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10325 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10327 if (template_param_list)
10329 fco = finish_member_template_decl (fco);
10330 finish_template_decl (template_param_list);
10331 --parser->num_template_parameter_lists;
10333 else if (parser->fully_implicit_function_template_p)
10334 fco = finish_fully_implicit_template (parser, fco);
10336 finish_member_declaration (fco);
10338 obstack_free (&declarator_obstack, p);
10340 return (fco != error_mark_node);
10344 /* Parse the body of a lambda expression, which is simply
10346 compound-statement
10348 but which requires special handling.
10349 LAMBDA_EXPR is the current representation of the lambda expression. */
10351 static void
10352 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10354 bool nested = (current_function_decl != NULL_TREE);
10355 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10356 if (nested)
10357 push_function_context ();
10358 else
10359 /* Still increment function_depth so that we don't GC in the
10360 middle of an expression. */
10361 ++function_depth;
10362 vec<tree> omp_privatization_save;
10363 save_omp_privatization_clauses (omp_privatization_save);
10364 /* Clear this in case we're in the middle of a default argument. */
10365 parser->local_variables_forbidden_p = false;
10367 /* Finish the function call operator
10368 - class_specifier
10369 + late_parsing_for_member
10370 + function_definition_after_declarator
10371 + ctor_initializer_opt_and_function_body */
10373 tree fco = lambda_function (lambda_expr);
10374 tree body;
10375 bool done = false;
10376 tree compound_stmt;
10377 tree cap;
10379 /* Let the front end know that we are going to be defining this
10380 function. */
10381 start_preparsed_function (fco,
10382 NULL_TREE,
10383 SF_PRE_PARSED | SF_INCLASS_INLINE);
10385 start_lambda_scope (fco);
10386 body = begin_function_body ();
10388 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10389 goto out;
10391 /* Push the proxies for any explicit captures. */
10392 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10393 cap = TREE_CHAIN (cap))
10394 build_capture_proxy (TREE_PURPOSE (cap));
10396 compound_stmt = begin_compound_stmt (0);
10398 /* 5.1.1.4 of the standard says:
10399 If a lambda-expression does not include a trailing-return-type, it
10400 is as if the trailing-return-type denotes the following type:
10401 * if the compound-statement is of the form
10402 { return attribute-specifier [opt] expression ; }
10403 the type of the returned expression after lvalue-to-rvalue
10404 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10405 (_conv.array_ 4.2), and function-to-pointer conversion
10406 (_conv.func_ 4.3);
10407 * otherwise, void. */
10409 /* In a lambda that has neither a lambda-return-type-clause
10410 nor a deducible form, errors should be reported for return statements
10411 in the body. Since we used void as the placeholder return type, parsing
10412 the body as usual will give such desired behavior. */
10413 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10414 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10415 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10417 tree expr = NULL_TREE;
10418 cp_id_kind idk = CP_ID_KIND_NONE;
10420 /* Parse tentatively in case there's more after the initial return
10421 statement. */
10422 cp_parser_parse_tentatively (parser);
10424 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10426 expr = cp_parser_expression (parser, &idk);
10428 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10429 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10431 if (cp_parser_parse_definitely (parser))
10433 if (!processing_template_decl)
10435 tree type = lambda_return_type (expr);
10436 apply_deduced_return_type (fco, type);
10437 if (type == error_mark_node)
10438 expr = error_mark_node;
10441 /* Will get error here if type not deduced yet. */
10442 finish_return_stmt (expr);
10444 done = true;
10448 if (!done)
10450 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10451 cp_parser_label_declaration (parser);
10452 cp_parser_statement_seq_opt (parser, NULL_TREE);
10453 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10456 finish_compound_stmt (compound_stmt);
10458 out:
10459 finish_function_body (body);
10460 finish_lambda_scope ();
10462 /* Finish the function and generate code for it if necessary. */
10463 tree fn = finish_function (/*inline*/2);
10465 /* Only expand if the call op is not a template. */
10466 if (!DECL_TEMPLATE_INFO (fco))
10467 expand_or_defer_fn (fn);
10470 restore_omp_privatization_clauses (omp_privatization_save);
10471 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10472 if (nested)
10473 pop_function_context();
10474 else
10475 --function_depth;
10478 /* Statements [gram.stmt.stmt] */
10480 /* Parse a statement.
10482 statement:
10483 labeled-statement
10484 expression-statement
10485 compound-statement
10486 selection-statement
10487 iteration-statement
10488 jump-statement
10489 declaration-statement
10490 try-block
10492 C++11:
10494 statement:
10495 labeled-statement
10496 attribute-specifier-seq (opt) expression-statement
10497 attribute-specifier-seq (opt) compound-statement
10498 attribute-specifier-seq (opt) selection-statement
10499 attribute-specifier-seq (opt) iteration-statement
10500 attribute-specifier-seq (opt) jump-statement
10501 declaration-statement
10502 attribute-specifier-seq (opt) try-block
10504 init-statement:
10505 expression-statement
10506 simple-declaration
10508 TM Extension:
10510 statement:
10511 atomic-statement
10513 IN_COMPOUND is true when the statement is nested inside a
10514 cp_parser_compound_statement; this matters for certain pragmas.
10516 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10517 is a (possibly labeled) if statement which is not enclosed in braces
10518 and has an else clause. This is used to implement -Wparentheses.
10520 CHAIN is a vector of if-else-if conditions. */
10522 static void
10523 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10524 bool in_compound, bool *if_p, vec<tree> *chain)
10526 tree statement, std_attrs = NULL_TREE;
10527 cp_token *token;
10528 location_t statement_location, attrs_location;
10530 restart:
10531 if (if_p != NULL)
10532 *if_p = false;
10533 /* There is no statement yet. */
10534 statement = NULL_TREE;
10536 saved_token_sentinel saved_tokens (parser->lexer);
10537 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10538 if (c_dialect_objc ())
10539 /* In obj-c++, seeing '[[' might be the either the beginning of
10540 c++11 attributes, or a nested objc-message-expression. So
10541 let's parse the c++11 attributes tentatively. */
10542 cp_parser_parse_tentatively (parser);
10543 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10544 if (c_dialect_objc ())
10546 if (!cp_parser_parse_definitely (parser))
10547 std_attrs = NULL_TREE;
10550 /* Peek at the next token. */
10551 token = cp_lexer_peek_token (parser->lexer);
10552 /* Remember the location of the first token in the statement. */
10553 statement_location = token->location;
10554 /* If this is a keyword, then that will often determine what kind of
10555 statement we have. */
10556 if (token->type == CPP_KEYWORD)
10558 enum rid keyword = token->keyword;
10560 switch (keyword)
10562 case RID_CASE:
10563 case RID_DEFAULT:
10564 /* Looks like a labeled-statement with a case label.
10565 Parse the label, and then use tail recursion to parse
10566 the statement. */
10567 cp_parser_label_for_labeled_statement (parser, std_attrs);
10568 in_compound = false;
10569 goto restart;
10571 case RID_IF:
10572 case RID_SWITCH:
10573 statement = cp_parser_selection_statement (parser, if_p, chain);
10574 break;
10576 case RID_WHILE:
10577 case RID_DO:
10578 case RID_FOR:
10579 statement = cp_parser_iteration_statement (parser, if_p, false);
10580 break;
10582 case RID_CILK_FOR:
10583 if (!flag_cilkplus)
10585 error_at (cp_lexer_peek_token (parser->lexer)->location,
10586 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10587 cp_lexer_consume_token (parser->lexer);
10588 statement = error_mark_node;
10590 else
10591 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10592 break;
10594 case RID_BREAK:
10595 case RID_CONTINUE:
10596 case RID_RETURN:
10597 case RID_GOTO:
10598 statement = cp_parser_jump_statement (parser);
10599 break;
10601 case RID_CILK_SYNC:
10602 cp_lexer_consume_token (parser->lexer);
10603 if (flag_cilkplus)
10605 tree sync_expr = build_cilk_sync ();
10606 SET_EXPR_LOCATION (sync_expr,
10607 token->location);
10608 statement = finish_expr_stmt (sync_expr);
10610 else
10612 error_at (token->location, "-fcilkplus must be enabled to use"
10613 " %<_Cilk_sync%>");
10614 statement = error_mark_node;
10616 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10617 break;
10619 /* Objective-C++ exception-handling constructs. */
10620 case RID_AT_TRY:
10621 case RID_AT_CATCH:
10622 case RID_AT_FINALLY:
10623 case RID_AT_SYNCHRONIZED:
10624 case RID_AT_THROW:
10625 statement = cp_parser_objc_statement (parser);
10626 break;
10628 case RID_TRY:
10629 statement = cp_parser_try_block (parser);
10630 break;
10632 case RID_NAMESPACE:
10633 /* This must be a namespace alias definition. */
10634 cp_parser_declaration_statement (parser);
10635 return;
10637 case RID_TRANSACTION_ATOMIC:
10638 case RID_TRANSACTION_RELAXED:
10639 case RID_SYNCHRONIZED:
10640 case RID_ATOMIC_NOEXCEPT:
10641 case RID_ATOMIC_CANCEL:
10642 statement = cp_parser_transaction (parser, token);
10643 break;
10644 case RID_TRANSACTION_CANCEL:
10645 statement = cp_parser_transaction_cancel (parser);
10646 break;
10648 default:
10649 /* It might be a keyword like `int' that can start a
10650 declaration-statement. */
10651 break;
10654 else if (token->type == CPP_NAME)
10656 /* If the next token is a `:', then we are looking at a
10657 labeled-statement. */
10658 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10659 if (token->type == CPP_COLON)
10661 /* Looks like a labeled-statement with an ordinary label.
10662 Parse the label, and then use tail recursion to parse
10663 the statement. */
10665 cp_parser_label_for_labeled_statement (parser, std_attrs);
10666 in_compound = false;
10667 goto restart;
10670 /* Anything that starts with a `{' must be a compound-statement. */
10671 else if (token->type == CPP_OPEN_BRACE)
10672 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10673 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10674 a statement all its own. */
10675 else if (token->type == CPP_PRAGMA)
10677 /* Only certain OpenMP pragmas are attached to statements, and thus
10678 are considered statements themselves. All others are not. In
10679 the context of a compound, accept the pragma as a "statement" and
10680 return so that we can check for a close brace. Otherwise we
10681 require a real statement and must go back and read one. */
10682 if (in_compound)
10683 cp_parser_pragma (parser, pragma_compound, if_p);
10684 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10685 goto restart;
10686 return;
10688 else if (token->type == CPP_EOF)
10690 cp_parser_error (parser, "expected statement");
10691 return;
10694 /* Everything else must be a declaration-statement or an
10695 expression-statement. Try for the declaration-statement
10696 first, unless we are looking at a `;', in which case we know that
10697 we have an expression-statement. */
10698 if (!statement)
10700 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10702 if (std_attrs != NULL_TREE)
10704 /* Attributes should be parsed as part of the the
10705 declaration, so let's un-parse them. */
10706 saved_tokens.rollback();
10707 std_attrs = NULL_TREE;
10710 cp_parser_parse_tentatively (parser);
10711 /* Try to parse the declaration-statement. */
10712 cp_parser_declaration_statement (parser);
10713 /* If that worked, we're done. */
10714 if (cp_parser_parse_definitely (parser))
10715 return;
10717 /* Look for an expression-statement instead. */
10718 statement = cp_parser_expression_statement (parser, in_statement_expr);
10720 /* Handle [[fallthrough]];. */
10721 if (attribute_fallthrough_p (std_attrs))
10723 /* The next token after the fallthrough attribute is ';'. */
10724 if (statement == NULL_TREE)
10726 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10727 statement = build_call_expr_internal_loc (statement_location,
10728 IFN_FALLTHROUGH,
10729 void_type_node, 0);
10730 finish_expr_stmt (statement);
10732 else
10733 warning_at (statement_location, OPT_Wattributes,
10734 "%<fallthrough%> attribute not followed by %<;%>");
10735 std_attrs = NULL_TREE;
10739 /* Set the line number for the statement. */
10740 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10741 SET_EXPR_LOCATION (statement, statement_location);
10743 /* Allow "[[fallthrough]];", but warn otherwise. */
10744 if (std_attrs != NULL_TREE)
10745 warning_at (attrs_location,
10746 OPT_Wattributes,
10747 "attributes at the beginning of statement are ignored");
10750 /* Parse the label for a labeled-statement, i.e.
10752 identifier :
10753 case constant-expression :
10754 default :
10756 GNU Extension:
10757 case constant-expression ... constant-expression : statement
10759 When a label is parsed without errors, the label is added to the
10760 parse tree by the finish_* functions, so this function doesn't
10761 have to return the label. */
10763 static void
10764 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10766 cp_token *token;
10767 tree label = NULL_TREE;
10768 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10770 /* The next token should be an identifier. */
10771 token = cp_lexer_peek_token (parser->lexer);
10772 if (token->type != CPP_NAME
10773 && token->type != CPP_KEYWORD)
10775 cp_parser_error (parser, "expected labeled-statement");
10776 return;
10779 /* Remember whether this case or a user-defined label is allowed to fall
10780 through to. */
10781 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10783 parser->colon_corrects_to_scope_p = false;
10784 switch (token->keyword)
10786 case RID_CASE:
10788 tree expr, expr_hi;
10789 cp_token *ellipsis;
10791 /* Consume the `case' token. */
10792 cp_lexer_consume_token (parser->lexer);
10793 /* Parse the constant-expression. */
10794 expr = cp_parser_constant_expression (parser);
10795 if (check_for_bare_parameter_packs (expr))
10796 expr = error_mark_node;
10798 ellipsis = cp_lexer_peek_token (parser->lexer);
10799 if (ellipsis->type == CPP_ELLIPSIS)
10801 /* Consume the `...' token. */
10802 cp_lexer_consume_token (parser->lexer);
10803 expr_hi = cp_parser_constant_expression (parser);
10804 if (check_for_bare_parameter_packs (expr_hi))
10805 expr_hi = error_mark_node;
10807 /* We don't need to emit warnings here, as the common code
10808 will do this for us. */
10810 else
10811 expr_hi = NULL_TREE;
10813 if (parser->in_switch_statement_p)
10815 tree l = finish_case_label (token->location, expr, expr_hi);
10816 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10817 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10819 else
10820 error_at (token->location,
10821 "case label %qE not within a switch statement",
10822 expr);
10824 break;
10826 case RID_DEFAULT:
10827 /* Consume the `default' token. */
10828 cp_lexer_consume_token (parser->lexer);
10830 if (parser->in_switch_statement_p)
10832 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10833 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10834 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10836 else
10837 error_at (token->location, "case label not within a switch statement");
10838 break;
10840 default:
10841 /* Anything else must be an ordinary label. */
10842 label = finish_label_stmt (cp_parser_identifier (parser));
10843 if (label && TREE_CODE (label) == LABEL_DECL)
10844 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10845 break;
10848 /* Require the `:' token. */
10849 cp_parser_require (parser, CPP_COLON, RT_COLON);
10851 /* An ordinary label may optionally be followed by attributes.
10852 However, this is only permitted if the attributes are then
10853 followed by a semicolon. This is because, for backward
10854 compatibility, when parsing
10855 lab: __attribute__ ((unused)) int i;
10856 we want the attribute to attach to "i", not "lab". */
10857 if (label != NULL_TREE
10858 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10860 tree attrs;
10861 cp_parser_parse_tentatively (parser);
10862 attrs = cp_parser_gnu_attributes_opt (parser);
10863 if (attrs == NULL_TREE
10864 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10865 cp_parser_abort_tentative_parse (parser);
10866 else if (!cp_parser_parse_definitely (parser))
10868 else
10869 attributes = chainon (attributes, attrs);
10872 if (attributes != NULL_TREE)
10873 cplus_decl_attributes (&label, attributes, 0);
10875 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10878 /* Parse an expression-statement.
10880 expression-statement:
10881 expression [opt] ;
10883 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10884 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10885 indicates whether this expression-statement is part of an
10886 expression statement. */
10888 static tree
10889 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10891 tree statement = NULL_TREE;
10892 cp_token *token = cp_lexer_peek_token (parser->lexer);
10893 location_t loc = token->location;
10895 /* There might be attribute fallthrough. */
10896 tree attr = cp_parser_gnu_attributes_opt (parser);
10898 /* If the next token is a ';', then there is no expression
10899 statement. */
10900 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10902 statement = cp_parser_expression (parser);
10903 if (statement == error_mark_node
10904 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10906 cp_parser_skip_to_end_of_block_or_statement (parser);
10907 return error_mark_node;
10911 /* Handle [[fallthrough]];. */
10912 if (attribute_fallthrough_p (attr))
10914 /* The next token after the fallthrough attribute is ';'. */
10915 if (statement == NULL_TREE)
10916 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10917 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10918 void_type_node, 0);
10919 else
10920 warning_at (loc, OPT_Wattributes,
10921 "%<fallthrough%> attribute not followed by %<;%>");
10922 attr = NULL_TREE;
10925 /* Allow "[[fallthrough]];", but warn otherwise. */
10926 if (attr != NULL_TREE)
10927 warning_at (loc, OPT_Wattributes,
10928 "attributes at the beginning of statement are ignored");
10930 /* Give a helpful message for "A<T>::type t;" and the like. */
10931 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10932 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10934 if (TREE_CODE (statement) == SCOPE_REF)
10935 error_at (token->location, "need %<typename%> before %qE because "
10936 "%qT is a dependent scope",
10937 statement, TREE_OPERAND (statement, 0));
10938 else if (is_overloaded_fn (statement)
10939 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10941 /* A::A a; */
10942 tree fn = get_first_fn (statement);
10943 error_at (token->location,
10944 "%<%T::%D%> names the constructor, not the type",
10945 DECL_CONTEXT (fn), DECL_NAME (fn));
10949 /* Consume the final `;'. */
10950 cp_parser_consume_semicolon_at_end_of_statement (parser);
10952 if (in_statement_expr
10953 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10954 /* This is the final expression statement of a statement
10955 expression. */
10956 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10957 else if (statement)
10958 statement = finish_expr_stmt (statement);
10960 return statement;
10963 /* Parse a compound-statement.
10965 compound-statement:
10966 { statement-seq [opt] }
10968 GNU extension:
10970 compound-statement:
10971 { label-declaration-seq [opt] statement-seq [opt] }
10973 label-declaration-seq:
10974 label-declaration
10975 label-declaration-seq label-declaration
10977 Returns a tree representing the statement. */
10979 static tree
10980 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10981 int bcs_flags, bool function_body)
10983 tree compound_stmt;
10985 /* Consume the `{'. */
10986 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10987 return error_mark_node;
10988 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10989 && !function_body && cxx_dialect < cxx14)
10990 pedwarn (input_location, OPT_Wpedantic,
10991 "compound-statement in constexpr function");
10992 /* Begin the compound-statement. */
10993 compound_stmt = begin_compound_stmt (bcs_flags);
10994 /* If the next keyword is `__label__' we have a label declaration. */
10995 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10996 cp_parser_label_declaration (parser);
10997 /* Parse an (optional) statement-seq. */
10998 cp_parser_statement_seq_opt (parser, in_statement_expr);
10999 /* Finish the compound-statement. */
11000 finish_compound_stmt (compound_stmt);
11001 /* Consume the `}'. */
11002 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11004 return compound_stmt;
11007 /* Parse an (optional) statement-seq.
11009 statement-seq:
11010 statement
11011 statement-seq [opt] statement */
11013 static void
11014 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11016 /* Scan statements until there aren't any more. */
11017 while (true)
11019 cp_token *token = cp_lexer_peek_token (parser->lexer);
11021 /* If we are looking at a `}', then we have run out of
11022 statements; the same is true if we have reached the end
11023 of file, or have stumbled upon a stray '@end'. */
11024 if (token->type == CPP_CLOSE_BRACE
11025 || token->type == CPP_EOF
11026 || token->type == CPP_PRAGMA_EOL
11027 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11028 break;
11030 /* If we are in a compound statement and find 'else' then
11031 something went wrong. */
11032 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11034 if (parser->in_statement & IN_IF_STMT)
11035 break;
11036 else
11038 token = cp_lexer_consume_token (parser->lexer);
11039 error_at (token->location, "%<else%> without a previous %<if%>");
11043 /* Parse the statement. */
11044 cp_parser_statement (parser, in_statement_expr, true, NULL);
11048 /* Return true if we're looking at (init; cond), false otherwise. */
11050 static bool
11051 cp_parser_init_statement_p (cp_parser *parser)
11053 /* Save tokens so that we can put them back. */
11054 cp_lexer_save_tokens (parser->lexer);
11056 /* Look for ';' that is not nested in () or {}. */
11057 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11058 /*recovering=*/false,
11059 CPP_SEMICOLON,
11060 /*consume_paren=*/false);
11062 /* Roll back the tokens we skipped. */
11063 cp_lexer_rollback_tokens (parser->lexer);
11065 return ret == -1;
11068 /* Parse a selection-statement.
11070 selection-statement:
11071 if ( init-statement [opt] condition ) statement
11072 if ( init-statement [opt] condition ) statement else statement
11073 switch ( init-statement [opt] condition ) statement
11075 Returns the new IF_STMT or SWITCH_STMT.
11077 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11078 is a (possibly labeled) if statement which is not enclosed in
11079 braces and has an else clause. This is used to implement
11080 -Wparentheses.
11082 CHAIN is a vector of if-else-if conditions. This is used to implement
11083 -Wduplicated-cond. */
11085 static tree
11086 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11087 vec<tree> *chain)
11089 cp_token *token;
11090 enum rid keyword;
11091 token_indent_info guard_tinfo;
11093 if (if_p != NULL)
11094 *if_p = false;
11096 /* Peek at the next token. */
11097 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11098 guard_tinfo = get_token_indent_info (token);
11100 /* See what kind of keyword it is. */
11101 keyword = token->keyword;
11102 switch (keyword)
11104 case RID_IF:
11105 case RID_SWITCH:
11107 tree statement;
11108 tree condition;
11110 bool cx = false;
11111 if (keyword == RID_IF
11112 && cp_lexer_next_token_is_keyword (parser->lexer,
11113 RID_CONSTEXPR))
11115 cx = true;
11116 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11117 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11118 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11119 "with -std=c++1z or -std=gnu++1z");
11122 /* Look for the `('. */
11123 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11125 cp_parser_skip_to_end_of_statement (parser);
11126 return error_mark_node;
11129 /* Begin the selection-statement. */
11130 if (keyword == RID_IF)
11132 statement = begin_if_stmt ();
11133 IF_STMT_CONSTEXPR_P (statement) = cx;
11135 else
11136 statement = begin_switch_stmt ();
11138 /* Parse the optional init-statement. */
11139 if (cp_parser_init_statement_p (parser))
11141 tree decl;
11142 if (cxx_dialect < cxx1z)
11143 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11144 "init-statement in selection statements only available "
11145 "with -std=c++1z or -std=gnu++1z");
11146 cp_parser_init_statement (parser, &decl);
11149 /* Parse the condition. */
11150 condition = cp_parser_condition (parser);
11151 /* Look for the `)'. */
11152 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11153 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11154 /*consume_paren=*/true);
11156 if (keyword == RID_IF)
11158 bool nested_if;
11159 unsigned char in_statement;
11161 /* Add the condition. */
11162 condition = finish_if_stmt_cond (condition, statement);
11164 if (warn_duplicated_cond)
11165 warn_duplicated_cond_add_or_warn (token->location, condition,
11166 &chain);
11168 /* Parse the then-clause. */
11169 in_statement = parser->in_statement;
11170 parser->in_statement |= IN_IF_STMT;
11172 /* Outside a template, the non-selected branch of a constexpr
11173 if is a 'discarded statement', i.e. unevaluated. */
11174 bool was_discarded = in_discarded_stmt;
11175 bool discard_then = (cx && !processing_template_decl
11176 && integer_zerop (condition));
11177 if (discard_then)
11179 in_discarded_stmt = true;
11180 ++c_inhibit_evaluation_warnings;
11183 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11184 guard_tinfo);
11186 parser->in_statement = in_statement;
11188 finish_then_clause (statement);
11190 if (discard_then)
11192 THEN_CLAUSE (statement) = NULL_TREE;
11193 in_discarded_stmt = was_discarded;
11194 --c_inhibit_evaluation_warnings;
11197 /* If the next token is `else', parse the else-clause. */
11198 if (cp_lexer_next_token_is_keyword (parser->lexer,
11199 RID_ELSE))
11201 bool discard_else = (cx && !processing_template_decl
11202 && integer_nonzerop (condition));
11203 if (discard_else)
11205 in_discarded_stmt = true;
11206 ++c_inhibit_evaluation_warnings;
11209 guard_tinfo
11210 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11211 /* Consume the `else' keyword. */
11212 cp_lexer_consume_token (parser->lexer);
11213 if (warn_duplicated_cond)
11215 if (cp_lexer_next_token_is_keyword (parser->lexer,
11216 RID_IF)
11217 && chain == NULL)
11219 /* We've got "if (COND) else if (COND2)". Start
11220 the condition chain and add COND as the first
11221 element. */
11222 chain = new vec<tree> ();
11223 if (!CONSTANT_CLASS_P (condition)
11224 && !TREE_SIDE_EFFECTS (condition))
11226 /* Wrap it in a NOP_EXPR so that we can set the
11227 location of the condition. */
11228 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11229 condition);
11230 SET_EXPR_LOCATION (e, token->location);
11231 chain->safe_push (e);
11234 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11235 RID_IF))
11237 /* This is if-else without subsequent if. Zap the
11238 condition chain; we would have already warned at
11239 this point. */
11240 delete chain;
11241 chain = NULL;
11244 begin_else_clause (statement);
11245 /* Parse the else-clause. */
11246 cp_parser_implicitly_scoped_statement (parser, NULL,
11247 guard_tinfo, chain);
11249 finish_else_clause (statement);
11251 /* If we are currently parsing a then-clause, then
11252 IF_P will not be NULL. We set it to true to
11253 indicate that this if statement has an else clause.
11254 This may trigger the Wparentheses warning below
11255 when we get back up to the parent if statement. */
11256 if (if_p != NULL)
11257 *if_p = true;
11259 if (discard_else)
11261 ELSE_CLAUSE (statement) = NULL_TREE;
11262 in_discarded_stmt = was_discarded;
11263 --c_inhibit_evaluation_warnings;
11266 else
11268 /* This if statement does not have an else clause. If
11269 NESTED_IF is true, then the then-clause has an if
11270 statement which does have an else clause. We warn
11271 about the potential ambiguity. */
11272 if (nested_if)
11273 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11274 "suggest explicit braces to avoid ambiguous"
11275 " %<else%>");
11276 if (warn_duplicated_cond)
11278 /* We don't need the condition chain anymore. */
11279 delete chain;
11280 chain = NULL;
11284 /* Now we're all done with the if-statement. */
11285 finish_if_stmt (statement);
11287 else
11289 bool in_switch_statement_p;
11290 unsigned char in_statement;
11292 /* Add the condition. */
11293 finish_switch_cond (condition, statement);
11295 /* Parse the body of the switch-statement. */
11296 in_switch_statement_p = parser->in_switch_statement_p;
11297 in_statement = parser->in_statement;
11298 parser->in_switch_statement_p = true;
11299 parser->in_statement |= IN_SWITCH_STMT;
11300 cp_parser_implicitly_scoped_statement (parser, if_p,
11301 guard_tinfo);
11302 parser->in_switch_statement_p = in_switch_statement_p;
11303 parser->in_statement = in_statement;
11305 /* Now we're all done with the switch-statement. */
11306 finish_switch_stmt (statement);
11309 return statement;
11311 break;
11313 default:
11314 cp_parser_error (parser, "expected selection-statement");
11315 return error_mark_node;
11319 /* Parse a condition.
11321 condition:
11322 expression
11323 type-specifier-seq declarator = initializer-clause
11324 type-specifier-seq declarator braced-init-list
11326 GNU Extension:
11328 condition:
11329 type-specifier-seq declarator asm-specification [opt]
11330 attributes [opt] = assignment-expression
11332 Returns the expression that should be tested. */
11334 static tree
11335 cp_parser_condition (cp_parser* parser)
11337 cp_decl_specifier_seq type_specifiers;
11338 const char *saved_message;
11339 int declares_class_or_enum;
11341 /* Try the declaration first. */
11342 cp_parser_parse_tentatively (parser);
11343 /* New types are not allowed in the type-specifier-seq for a
11344 condition. */
11345 saved_message = parser->type_definition_forbidden_message;
11346 parser->type_definition_forbidden_message
11347 = G_("types may not be defined in conditions");
11348 /* Parse the type-specifier-seq. */
11349 cp_parser_decl_specifier_seq (parser,
11350 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11351 &type_specifiers,
11352 &declares_class_or_enum);
11353 /* Restore the saved message. */
11354 parser->type_definition_forbidden_message = saved_message;
11355 /* If all is well, we might be looking at a declaration. */
11356 if (!cp_parser_error_occurred (parser))
11358 tree decl;
11359 tree asm_specification;
11360 tree attributes;
11361 cp_declarator *declarator;
11362 tree initializer = NULL_TREE;
11364 /* Parse the declarator. */
11365 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11366 /*ctor_dtor_or_conv_p=*/NULL,
11367 /*parenthesized_p=*/NULL,
11368 /*member_p=*/false,
11369 /*friend_p=*/false);
11370 /* Parse the attributes. */
11371 attributes = cp_parser_attributes_opt (parser);
11372 /* Parse the asm-specification. */
11373 asm_specification = cp_parser_asm_specification_opt (parser);
11374 /* If the next token is not an `=' or '{', then we might still be
11375 looking at an expression. For example:
11377 if (A(a).x)
11379 looks like a decl-specifier-seq and a declarator -- but then
11380 there is no `=', so this is an expression. */
11381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11382 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11383 cp_parser_simulate_error (parser);
11385 /* If we did see an `=' or '{', then we are looking at a declaration
11386 for sure. */
11387 if (cp_parser_parse_definitely (parser))
11389 tree pushed_scope;
11390 bool non_constant_p;
11391 int flags = LOOKUP_ONLYCONVERTING;
11393 /* Create the declaration. */
11394 decl = start_decl (declarator, &type_specifiers,
11395 /*initialized_p=*/true,
11396 attributes, /*prefix_attributes=*/NULL_TREE,
11397 &pushed_scope);
11399 /* Parse the initializer. */
11400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11402 initializer = cp_parser_braced_list (parser, &non_constant_p);
11403 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11404 flags = 0;
11406 else
11408 /* Consume the `='. */
11409 cp_parser_require (parser, CPP_EQ, RT_EQ);
11410 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11412 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11413 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11415 /* Process the initializer. */
11416 cp_finish_decl (decl,
11417 initializer, !non_constant_p,
11418 asm_specification,
11419 flags);
11421 if (pushed_scope)
11422 pop_scope (pushed_scope);
11424 return convert_from_reference (decl);
11427 /* If we didn't even get past the declarator successfully, we are
11428 definitely not looking at a declaration. */
11429 else
11430 cp_parser_abort_tentative_parse (parser);
11432 /* Otherwise, we are looking at an expression. */
11433 return cp_parser_expression (parser);
11436 /* Parses a for-statement or range-for-statement until the closing ')',
11437 not included. */
11439 static tree
11440 cp_parser_for (cp_parser *parser, bool ivdep)
11442 tree init, scope, decl;
11443 bool is_range_for;
11445 /* Begin the for-statement. */
11446 scope = begin_for_scope (&init);
11448 /* Parse the initialization. */
11449 is_range_for = cp_parser_init_statement (parser, &decl);
11451 if (is_range_for)
11452 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11453 else
11454 return cp_parser_c_for (parser, scope, init, ivdep);
11457 static tree
11458 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11460 /* Normal for loop */
11461 tree condition = NULL_TREE;
11462 tree expression = NULL_TREE;
11463 tree stmt;
11465 stmt = begin_for_stmt (scope, init);
11466 /* The init-statement has already been parsed in
11467 cp_parser_init_statement, so no work is needed here. */
11468 finish_init_stmt (stmt);
11470 /* If there's a condition, process it. */
11471 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11472 condition = cp_parser_condition (parser);
11473 else if (ivdep)
11475 cp_parser_error (parser, "missing loop condition in loop with "
11476 "%<GCC ivdep%> pragma");
11477 condition = error_mark_node;
11479 finish_for_cond (condition, stmt, ivdep);
11480 /* Look for the `;'. */
11481 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11483 /* If there's an expression, process it. */
11484 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11485 expression = cp_parser_expression (parser);
11486 finish_for_expr (expression, stmt);
11488 return stmt;
11491 /* Tries to parse a range-based for-statement:
11493 range-based-for:
11494 decl-specifier-seq declarator : expression
11496 The decl-specifier-seq declarator and the `:' are already parsed by
11497 cp_parser_init_statement. If processing_template_decl it returns a
11498 newly created RANGE_FOR_STMT; if not, it is converted to a
11499 regular FOR_STMT. */
11501 static tree
11502 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11503 bool ivdep)
11505 tree stmt, range_expr;
11506 auto_vec <cxx_binding *, 16> bindings;
11507 auto_vec <tree, 16> names;
11508 tree decomp_first_name = NULL_TREE;
11509 unsigned int decomp_cnt = 0;
11511 /* Get the range declaration momentarily out of the way so that
11512 the range expression doesn't clash with it. */
11513 if (range_decl != error_mark_node)
11515 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11517 tree v = DECL_VALUE_EXPR (range_decl);
11518 /* For decomposition declaration get all of the corresponding
11519 declarations out of the way. */
11520 if (TREE_CODE (v) == ARRAY_REF
11521 && VAR_P (TREE_OPERAND (v, 0))
11522 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11524 tree d = range_decl;
11525 range_decl = TREE_OPERAND (v, 0);
11526 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11527 decomp_first_name = d;
11528 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11530 tree name = DECL_NAME (d);
11531 names.safe_push (name);
11532 bindings.safe_push (IDENTIFIER_BINDING (name));
11533 IDENTIFIER_BINDING (name)
11534 = IDENTIFIER_BINDING (name)->previous;
11538 if (names.is_empty ())
11540 tree name = DECL_NAME (range_decl);
11541 names.safe_push (name);
11542 bindings.safe_push (IDENTIFIER_BINDING (name));
11543 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11547 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11549 bool expr_non_constant_p;
11550 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11552 else
11553 range_expr = cp_parser_expression (parser);
11555 /* Put the range declaration(s) back into scope. */
11556 for (unsigned int i = 0; i < names.length (); i++)
11558 cxx_binding *binding = bindings[i];
11559 binding->previous = IDENTIFIER_BINDING (names[i]);
11560 IDENTIFIER_BINDING (names[i]) = binding;
11563 /* If in template, STMT is converted to a normal for-statement
11564 at instantiation. If not, it is done just ahead. */
11565 if (processing_template_decl)
11567 if (check_for_bare_parameter_packs (range_expr))
11568 range_expr = error_mark_node;
11569 stmt = begin_range_for_stmt (scope, init);
11570 if (ivdep)
11571 RANGE_FOR_IVDEP (stmt) = 1;
11572 finish_range_for_decl (stmt, range_decl, range_expr);
11573 if (!type_dependent_expression_p (range_expr)
11574 /* do_auto_deduction doesn't mess with template init-lists. */
11575 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11576 do_range_for_auto_deduction (range_decl, range_expr);
11578 else
11580 stmt = begin_for_stmt (scope, init);
11581 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11582 decomp_first_name, decomp_cnt, ivdep);
11584 return stmt;
11587 /* Subroutine of cp_convert_range_for: given the initializer expression,
11588 builds up the range temporary. */
11590 static tree
11591 build_range_temp (tree range_expr)
11593 tree range_type, range_temp;
11595 /* Find out the type deduced by the declaration
11596 `auto &&__range = range_expr'. */
11597 range_type = cp_build_reference_type (make_auto (), true);
11598 range_type = do_auto_deduction (range_type, range_expr,
11599 type_uses_auto (range_type));
11601 /* Create the __range variable. */
11602 range_temp = build_decl (input_location, VAR_DECL,
11603 get_identifier ("__for_range"), range_type);
11604 TREE_USED (range_temp) = 1;
11605 DECL_ARTIFICIAL (range_temp) = 1;
11607 return range_temp;
11610 /* Used by cp_parser_range_for in template context: we aren't going to
11611 do a full conversion yet, but we still need to resolve auto in the
11612 type of the for-range-declaration if present. This is basically
11613 a shortcut version of cp_convert_range_for. */
11615 static void
11616 do_range_for_auto_deduction (tree decl, tree range_expr)
11618 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11619 if (auto_node)
11621 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11622 range_temp = convert_from_reference (build_range_temp (range_expr));
11623 iter_type = (cp_parser_perform_range_for_lookup
11624 (range_temp, &begin_dummy, &end_dummy));
11625 if (iter_type)
11627 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11628 iter_type);
11629 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11630 tf_warning_or_error);
11631 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11632 iter_decl, auto_node);
11637 /* Converts a range-based for-statement into a normal
11638 for-statement, as per the definition.
11640 for (RANGE_DECL : RANGE_EXPR)
11641 BLOCK
11643 should be equivalent to:
11646 auto &&__range = RANGE_EXPR;
11647 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11648 __begin != __end;
11649 ++__begin)
11651 RANGE_DECL = *__begin;
11652 BLOCK
11656 If RANGE_EXPR is an array:
11657 BEGIN_EXPR = __range
11658 END_EXPR = __range + ARRAY_SIZE(__range)
11659 Else if RANGE_EXPR has a member 'begin' or 'end':
11660 BEGIN_EXPR = __range.begin()
11661 END_EXPR = __range.end()
11662 Else:
11663 BEGIN_EXPR = begin(__range)
11664 END_EXPR = end(__range);
11666 If __range has a member 'begin' but not 'end', or vice versa, we must
11667 still use the second alternative (it will surely fail, however).
11668 When calling begin()/end() in the third alternative we must use
11669 argument dependent lookup, but always considering 'std' as an associated
11670 namespace. */
11672 tree
11673 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11674 tree decomp_first_name, unsigned int decomp_cnt,
11675 bool ivdep)
11677 tree begin, end;
11678 tree iter_type, begin_expr, end_expr;
11679 tree condition, expression;
11681 if (range_decl == error_mark_node || range_expr == error_mark_node)
11682 /* If an error happened previously do nothing or else a lot of
11683 unhelpful errors would be issued. */
11684 begin_expr = end_expr = iter_type = error_mark_node;
11685 else
11687 tree range_temp;
11689 if (VAR_P (range_expr)
11690 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11691 /* Can't bind a reference to an array of runtime bound. */
11692 range_temp = range_expr;
11693 else
11695 range_temp = build_range_temp (range_expr);
11696 pushdecl (range_temp);
11697 cp_finish_decl (range_temp, range_expr,
11698 /*is_constant_init*/false, NULL_TREE,
11699 LOOKUP_ONLYCONVERTING);
11700 range_temp = convert_from_reference (range_temp);
11702 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11703 &begin_expr, &end_expr);
11706 /* The new for initialization statement. */
11707 begin = build_decl (input_location, VAR_DECL,
11708 get_identifier ("__for_begin"), iter_type);
11709 TREE_USED (begin) = 1;
11710 DECL_ARTIFICIAL (begin) = 1;
11711 pushdecl (begin);
11712 cp_finish_decl (begin, begin_expr,
11713 /*is_constant_init*/false, NULL_TREE,
11714 LOOKUP_ONLYCONVERTING);
11716 if (cxx_dialect >= cxx1z)
11717 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11718 end = build_decl (input_location, VAR_DECL,
11719 get_identifier ("__for_end"), iter_type);
11720 TREE_USED (end) = 1;
11721 DECL_ARTIFICIAL (end) = 1;
11722 pushdecl (end);
11723 cp_finish_decl (end, end_expr,
11724 /*is_constant_init*/false, NULL_TREE,
11725 LOOKUP_ONLYCONVERTING);
11727 finish_init_stmt (statement);
11729 /* The new for condition. */
11730 condition = build_x_binary_op (input_location, NE_EXPR,
11731 begin, ERROR_MARK,
11732 end, ERROR_MARK,
11733 NULL, tf_warning_or_error);
11734 finish_for_cond (condition, statement, ivdep);
11736 /* The new increment expression. */
11737 expression = finish_unary_op_expr (input_location,
11738 PREINCREMENT_EXPR, begin,
11739 tf_warning_or_error);
11740 finish_for_expr (expression, statement);
11742 /* The declaration is initialized with *__begin inside the loop body. */
11743 cp_finish_decl (range_decl,
11744 build_x_indirect_ref (input_location, begin, RO_NULL,
11745 tf_warning_or_error),
11746 /*is_constant_init*/false, NULL_TREE,
11747 LOOKUP_ONLYCONVERTING);
11748 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11749 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11751 return statement;
11754 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11755 We need to solve both at the same time because the method used
11756 depends on the existence of members begin or end.
11757 Returns the type deduced for the iterator expression. */
11759 static tree
11760 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11762 if (error_operand_p (range))
11764 *begin = *end = error_mark_node;
11765 return error_mark_node;
11768 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11770 error ("range-based %<for%> expression of type %qT "
11771 "has incomplete type", TREE_TYPE (range));
11772 *begin = *end = error_mark_node;
11773 return error_mark_node;
11775 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11777 /* If RANGE is an array, we will use pointer arithmetic. */
11778 *begin = decay_conversion (range, tf_warning_or_error);
11779 *end = build_binary_op (input_location, PLUS_EXPR,
11780 range,
11781 array_type_nelts_top (TREE_TYPE (range)),
11783 return TREE_TYPE (*begin);
11785 else
11787 /* If it is not an array, we must do a bit of magic. */
11788 tree id_begin, id_end;
11789 tree member_begin, member_end;
11791 *begin = *end = error_mark_node;
11793 id_begin = get_identifier ("begin");
11794 id_end = get_identifier ("end");
11795 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11796 /*protect=*/2, /*want_type=*/false,
11797 tf_warning_or_error);
11798 member_end = lookup_member (TREE_TYPE (range), id_end,
11799 /*protect=*/2, /*want_type=*/false,
11800 tf_warning_or_error);
11802 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11804 /* Use the member functions. */
11805 if (member_begin != NULL_TREE)
11806 *begin = cp_parser_range_for_member_function (range, id_begin);
11807 else
11808 error ("range-based %<for%> expression of type %qT has an "
11809 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11811 if (member_end != NULL_TREE)
11812 *end = cp_parser_range_for_member_function (range, id_end);
11813 else
11814 error ("range-based %<for%> expression of type %qT has a "
11815 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11817 else
11819 /* Use global functions with ADL. */
11820 vec<tree, va_gc> *vec;
11821 vec = make_tree_vector ();
11823 vec_safe_push (vec, range);
11825 member_begin = perform_koenig_lookup (id_begin, vec,
11826 tf_warning_or_error);
11827 *begin = finish_call_expr (member_begin, &vec, false, true,
11828 tf_warning_or_error);
11829 member_end = perform_koenig_lookup (id_end, vec,
11830 tf_warning_or_error);
11831 *end = finish_call_expr (member_end, &vec, false, true,
11832 tf_warning_or_error);
11834 release_tree_vector (vec);
11837 /* Last common checks. */
11838 if (*begin == error_mark_node || *end == error_mark_node)
11840 /* If one of the expressions is an error do no more checks. */
11841 *begin = *end = error_mark_node;
11842 return error_mark_node;
11844 else if (type_dependent_expression_p (*begin)
11845 || type_dependent_expression_p (*end))
11846 /* Can happen, when, eg, in a template context, Koenig lookup
11847 can't resolve begin/end (c++/58503). */
11848 return NULL_TREE;
11849 else
11851 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11852 /* The unqualified type of the __begin and __end temporaries should
11853 be the same, as required by the multiple auto declaration. */
11854 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11856 if (cxx_dialect >= cxx1z
11857 && (build_x_binary_op (input_location, NE_EXPR,
11858 *begin, ERROR_MARK,
11859 *end, ERROR_MARK,
11860 NULL, tf_none)
11861 != error_mark_node))
11862 /* P0184R0 allows __begin and __end to have different types,
11863 but make sure they are comparable so we can give a better
11864 diagnostic. */;
11865 else
11866 error ("inconsistent begin/end types in range-based %<for%> "
11867 "statement: %qT and %qT",
11868 TREE_TYPE (*begin), TREE_TYPE (*end));
11870 return iter_type;
11875 /* Helper function for cp_parser_perform_range_for_lookup.
11876 Builds a tree for RANGE.IDENTIFIER(). */
11878 static tree
11879 cp_parser_range_for_member_function (tree range, tree identifier)
11881 tree member, res;
11882 vec<tree, va_gc> *vec;
11884 member = finish_class_member_access_expr (range, identifier,
11885 false, tf_warning_or_error);
11886 if (member == error_mark_node)
11887 return error_mark_node;
11889 vec = make_tree_vector ();
11890 res = finish_call_expr (member, &vec,
11891 /*disallow_virtual=*/false,
11892 /*koenig_p=*/false,
11893 tf_warning_or_error);
11894 release_tree_vector (vec);
11895 return res;
11898 /* Parse an iteration-statement.
11900 iteration-statement:
11901 while ( condition ) statement
11902 do statement while ( expression ) ;
11903 for ( init-statement condition [opt] ; expression [opt] )
11904 statement
11906 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11908 static tree
11909 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11911 cp_token *token;
11912 enum rid keyword;
11913 tree statement;
11914 unsigned char in_statement;
11915 token_indent_info guard_tinfo;
11917 /* Peek at the next token. */
11918 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11919 if (!token)
11920 return error_mark_node;
11922 guard_tinfo = get_token_indent_info (token);
11924 /* Remember whether or not we are already within an iteration
11925 statement. */
11926 in_statement = parser->in_statement;
11928 /* See what kind of keyword it is. */
11929 keyword = token->keyword;
11930 switch (keyword)
11932 case RID_WHILE:
11934 tree condition;
11936 /* Begin the while-statement. */
11937 statement = begin_while_stmt ();
11938 /* Look for the `('. */
11939 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11940 /* Parse the condition. */
11941 condition = cp_parser_condition (parser);
11942 finish_while_stmt_cond (condition, statement, ivdep);
11943 /* Look for the `)'. */
11944 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11945 /* Parse the dependent statement. */
11946 parser->in_statement = IN_ITERATION_STMT;
11947 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11948 parser->in_statement = in_statement;
11949 /* We're done with the while-statement. */
11950 finish_while_stmt (statement);
11952 break;
11954 case RID_DO:
11956 tree expression;
11958 /* Begin the do-statement. */
11959 statement = begin_do_stmt ();
11960 /* Parse the body of the do-statement. */
11961 parser->in_statement = IN_ITERATION_STMT;
11962 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11963 parser->in_statement = in_statement;
11964 finish_do_body (statement);
11965 /* Look for the `while' keyword. */
11966 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11967 /* Look for the `('. */
11968 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11969 /* Parse the expression. */
11970 expression = cp_parser_expression (parser);
11971 /* We're done with the do-statement. */
11972 finish_do_stmt (expression, statement, ivdep);
11973 /* Look for the `)'. */
11974 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11975 /* Look for the `;'. */
11976 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11978 break;
11980 case RID_FOR:
11982 /* Look for the `('. */
11983 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11985 statement = cp_parser_for (parser, ivdep);
11987 /* Look for the `)'. */
11988 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11990 /* Parse the body of the for-statement. */
11991 parser->in_statement = IN_ITERATION_STMT;
11992 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11993 parser->in_statement = in_statement;
11995 /* We're done with the for-statement. */
11996 finish_for_stmt (statement);
11998 break;
12000 default:
12001 cp_parser_error (parser, "expected iteration-statement");
12002 statement = error_mark_node;
12003 break;
12006 return statement;
12009 /* Parse a init-statement or the declarator of a range-based-for.
12010 Returns true if a range-based-for declaration is seen.
12012 init-statement:
12013 expression-statement
12014 simple-declaration */
12016 static bool
12017 cp_parser_init_statement (cp_parser* parser, tree *decl)
12019 /* If the next token is a `;', then we have an empty
12020 expression-statement. Grammatically, this is also a
12021 simple-declaration, but an invalid one, because it does not
12022 declare anything. Therefore, if we did not handle this case
12023 specially, we would issue an error message about an invalid
12024 declaration. */
12025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12027 bool is_range_for = false;
12028 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12030 /* A colon is used in range-based for. */
12031 parser->colon_corrects_to_scope_p = false;
12033 /* We're going to speculatively look for a declaration, falling back
12034 to an expression, if necessary. */
12035 cp_parser_parse_tentatively (parser);
12036 /* Parse the declaration. */
12037 cp_parser_simple_declaration (parser,
12038 /*function_definition_allowed_p=*/false,
12039 decl);
12040 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12041 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12043 /* It is a range-for, consume the ':' */
12044 cp_lexer_consume_token (parser->lexer);
12045 is_range_for = true;
12046 if (cxx_dialect < cxx11)
12048 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12049 "range-based %<for%> loops only available with "
12050 "-std=c++11 or -std=gnu++11");
12051 *decl = error_mark_node;
12054 else
12055 /* The ';' is not consumed yet because we told
12056 cp_parser_simple_declaration not to. */
12057 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12059 if (cp_parser_parse_definitely (parser))
12060 return is_range_for;
12061 /* If the tentative parse failed, then we shall need to look for an
12062 expression-statement. */
12064 /* If we are here, it is an expression-statement. */
12065 cp_parser_expression_statement (parser, NULL_TREE);
12066 return false;
12069 /* Parse a jump-statement.
12071 jump-statement:
12072 break ;
12073 continue ;
12074 return expression [opt] ;
12075 return braced-init-list ;
12076 goto identifier ;
12078 GNU extension:
12080 jump-statement:
12081 goto * expression ;
12083 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12085 static tree
12086 cp_parser_jump_statement (cp_parser* parser)
12088 tree statement = error_mark_node;
12089 cp_token *token;
12090 enum rid keyword;
12091 unsigned char in_statement;
12093 /* Peek at the next token. */
12094 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12095 if (!token)
12096 return error_mark_node;
12098 /* See what kind of keyword it is. */
12099 keyword = token->keyword;
12100 switch (keyword)
12102 case RID_BREAK:
12103 in_statement = parser->in_statement & ~IN_IF_STMT;
12104 switch (in_statement)
12106 case 0:
12107 error_at (token->location, "break statement not within loop or switch");
12108 break;
12109 default:
12110 gcc_assert ((in_statement & IN_SWITCH_STMT)
12111 || in_statement == IN_ITERATION_STMT);
12112 statement = finish_break_stmt ();
12113 if (in_statement == IN_ITERATION_STMT)
12114 break_maybe_infinite_loop ();
12115 break;
12116 case IN_OMP_BLOCK:
12117 error_at (token->location, "invalid exit from OpenMP structured block");
12118 break;
12119 case IN_OMP_FOR:
12120 error_at (token->location, "break statement used with OpenMP for loop");
12121 break;
12122 case IN_CILK_SIMD_FOR:
12123 error_at (token->location, "break statement used with Cilk Plus for loop");
12124 break;
12126 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12127 break;
12129 case RID_CONTINUE:
12130 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12132 case 0:
12133 error_at (token->location, "continue statement not within a loop");
12134 break;
12135 case IN_CILK_SIMD_FOR:
12136 error_at (token->location,
12137 "continue statement within %<#pragma simd%> loop body");
12138 /* Fall through. */
12139 case IN_ITERATION_STMT:
12140 case IN_OMP_FOR:
12141 statement = finish_continue_stmt ();
12142 break;
12143 case IN_OMP_BLOCK:
12144 error_at (token->location, "invalid exit from OpenMP structured block");
12145 break;
12146 default:
12147 gcc_unreachable ();
12149 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12150 break;
12152 case RID_RETURN:
12154 tree expr;
12155 bool expr_non_constant_p;
12157 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12159 cp_lexer_set_source_position (parser->lexer);
12160 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12161 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12163 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12164 expr = cp_parser_expression (parser);
12165 else
12166 /* If the next token is a `;', then there is no
12167 expression. */
12168 expr = NULL_TREE;
12169 /* Build the return-statement. */
12170 if (current_function_auto_return_pattern && in_discarded_stmt)
12171 /* Don't deduce from a discarded return statement. */;
12172 else
12173 statement = finish_return_stmt (expr);
12174 /* Look for the final `;'. */
12175 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12177 break;
12179 case RID_GOTO:
12180 if (parser->in_function_body
12181 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12183 error ("%<goto%> in %<constexpr%> function");
12184 cp_function_chain->invalid_constexpr = true;
12187 /* Create the goto-statement. */
12188 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12190 /* Issue a warning about this use of a GNU extension. */
12191 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12192 /* Consume the '*' token. */
12193 cp_lexer_consume_token (parser->lexer);
12194 /* Parse the dependent expression. */
12195 finish_goto_stmt (cp_parser_expression (parser));
12197 else
12198 finish_goto_stmt (cp_parser_identifier (parser));
12199 /* Look for the final `;'. */
12200 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12201 break;
12203 default:
12204 cp_parser_error (parser, "expected jump-statement");
12205 break;
12208 return statement;
12211 /* Parse a declaration-statement.
12213 declaration-statement:
12214 block-declaration */
12216 static void
12217 cp_parser_declaration_statement (cp_parser* parser)
12219 void *p;
12221 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12222 p = obstack_alloc (&declarator_obstack, 0);
12224 /* Parse the block-declaration. */
12225 cp_parser_block_declaration (parser, /*statement_p=*/true);
12227 /* Free any declarators allocated. */
12228 obstack_free (&declarator_obstack, p);
12231 /* Some dependent statements (like `if (cond) statement'), are
12232 implicitly in their own scope. In other words, if the statement is
12233 a single statement (as opposed to a compound-statement), it is
12234 none-the-less treated as if it were enclosed in braces. Any
12235 declarations appearing in the dependent statement are out of scope
12236 after control passes that point. This function parses a statement,
12237 but ensures that is in its own scope, even if it is not a
12238 compound-statement.
12240 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12241 is a (possibly labeled) if statement which is not enclosed in
12242 braces and has an else clause. This is used to implement
12243 -Wparentheses.
12245 CHAIN is a vector of if-else-if conditions. This is used to implement
12246 -Wduplicated-cond.
12248 Returns the new statement. */
12250 static tree
12251 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12252 const token_indent_info &guard_tinfo,
12253 vec<tree> *chain)
12255 tree statement;
12256 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12257 token_indent_info body_tinfo
12258 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12260 if (if_p != NULL)
12261 *if_p = false;
12263 /* Mark if () ; with a special NOP_EXPR. */
12264 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12266 cp_lexer_consume_token (parser->lexer);
12267 statement = add_stmt (build_empty_stmt (body_loc));
12269 if (guard_tinfo.keyword == RID_IF
12270 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12271 warning_at (body_loc, OPT_Wempty_body,
12272 "suggest braces around empty body in an %<if%> statement");
12273 else if (guard_tinfo.keyword == RID_ELSE)
12274 warning_at (body_loc, OPT_Wempty_body,
12275 "suggest braces around empty body in an %<else%> statement");
12277 /* if a compound is opened, we simply parse the statement directly. */
12278 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12279 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12280 /* If the token is not a `{', then we must take special action. */
12281 else
12283 /* Create a compound-statement. */
12284 statement = begin_compound_stmt (0);
12285 /* Parse the dependent-statement. */
12286 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12287 /* Finish the dummy compound-statement. */
12288 finish_compound_stmt (statement);
12291 token_indent_info next_tinfo
12292 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12293 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12295 /* Return the statement. */
12296 return statement;
12299 /* For some dependent statements (like `while (cond) statement'), we
12300 have already created a scope. Therefore, even if the dependent
12301 statement is a compound-statement, we do not want to create another
12302 scope. */
12304 static void
12305 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12306 const token_indent_info &guard_tinfo)
12308 /* If the token is a `{', then we must take special action. */
12309 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12311 token_indent_info body_tinfo
12312 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12314 cp_parser_statement (parser, NULL_TREE, false, if_p);
12315 token_indent_info next_tinfo
12316 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12317 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12319 else
12321 /* Avoid calling cp_parser_compound_statement, so that we
12322 don't create a new scope. Do everything else by hand. */
12323 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12324 /* If the next keyword is `__label__' we have a label declaration. */
12325 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12326 cp_parser_label_declaration (parser);
12327 /* Parse an (optional) statement-seq. */
12328 cp_parser_statement_seq_opt (parser, NULL_TREE);
12329 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12333 /* Declarations [gram.dcl.dcl] */
12335 /* Parse an optional declaration-sequence.
12337 declaration-seq:
12338 declaration
12339 declaration-seq declaration */
12341 static void
12342 cp_parser_declaration_seq_opt (cp_parser* parser)
12344 while (true)
12346 cp_token *token;
12348 token = cp_lexer_peek_token (parser->lexer);
12350 if (token->type == CPP_CLOSE_BRACE
12351 || token->type == CPP_EOF
12352 || token->type == CPP_PRAGMA_EOL)
12353 break;
12355 if (token->type == CPP_SEMICOLON)
12357 /* A declaration consisting of a single semicolon is
12358 invalid. Allow it unless we're being pedantic. */
12359 cp_lexer_consume_token (parser->lexer);
12360 if (!in_system_header_at (input_location))
12361 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12362 continue;
12365 /* If we're entering or exiting a region that's implicitly
12366 extern "C", modify the lang context appropriately. */
12367 if (!parser->implicit_extern_c && token->implicit_extern_c)
12369 push_lang_context (lang_name_c);
12370 parser->implicit_extern_c = true;
12372 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12374 pop_lang_context ();
12375 parser->implicit_extern_c = false;
12378 if (token->type == CPP_PRAGMA)
12380 /* A top-level declaration can consist solely of a #pragma.
12381 A nested declaration cannot, so this is done here and not
12382 in cp_parser_declaration. (A #pragma at block scope is
12383 handled in cp_parser_statement.) */
12384 cp_parser_pragma (parser, pragma_external, NULL);
12385 continue;
12388 /* Parse the declaration itself. */
12389 cp_parser_declaration (parser);
12393 /* Parse a declaration.
12395 declaration:
12396 block-declaration
12397 function-definition
12398 template-declaration
12399 explicit-instantiation
12400 explicit-specialization
12401 linkage-specification
12402 namespace-definition
12404 C++17:
12405 deduction-guide
12407 GNU extension:
12409 declaration:
12410 __extension__ declaration */
12412 static void
12413 cp_parser_declaration (cp_parser* parser)
12415 cp_token token1;
12416 cp_token token2;
12417 int saved_pedantic;
12418 void *p;
12419 tree attributes = NULL_TREE;
12421 /* Check for the `__extension__' keyword. */
12422 if (cp_parser_extension_opt (parser, &saved_pedantic))
12424 /* Parse the qualified declaration. */
12425 cp_parser_declaration (parser);
12426 /* Restore the PEDANTIC flag. */
12427 pedantic = saved_pedantic;
12429 return;
12432 /* Try to figure out what kind of declaration is present. */
12433 token1 = *cp_lexer_peek_token (parser->lexer);
12435 if (token1.type != CPP_EOF)
12436 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12437 else
12439 token2.type = CPP_EOF;
12440 token2.keyword = RID_MAX;
12443 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12444 p = obstack_alloc (&declarator_obstack, 0);
12446 /* If the next token is `extern' and the following token is a string
12447 literal, then we have a linkage specification. */
12448 if (token1.keyword == RID_EXTERN
12449 && cp_parser_is_pure_string_literal (&token2))
12450 cp_parser_linkage_specification (parser);
12451 /* If the next token is `template', then we have either a template
12452 declaration, an explicit instantiation, or an explicit
12453 specialization. */
12454 else if (token1.keyword == RID_TEMPLATE)
12456 /* `template <>' indicates a template specialization. */
12457 if (token2.type == CPP_LESS
12458 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12459 cp_parser_explicit_specialization (parser);
12460 /* `template <' indicates a template declaration. */
12461 else if (token2.type == CPP_LESS)
12462 cp_parser_template_declaration (parser, /*member_p=*/false);
12463 /* Anything else must be an explicit instantiation. */
12464 else
12465 cp_parser_explicit_instantiation (parser);
12467 /* If the next token is `export', then we have a template
12468 declaration. */
12469 else if (token1.keyword == RID_EXPORT)
12470 cp_parser_template_declaration (parser, /*member_p=*/false);
12471 /* If the next token is `extern', 'static' or 'inline' and the one
12472 after that is `template', we have a GNU extended explicit
12473 instantiation directive. */
12474 else if (cp_parser_allow_gnu_extensions_p (parser)
12475 && (token1.keyword == RID_EXTERN
12476 || token1.keyword == RID_STATIC
12477 || token1.keyword == RID_INLINE)
12478 && token2.keyword == RID_TEMPLATE)
12479 cp_parser_explicit_instantiation (parser);
12480 /* If the next token is `namespace', check for a named or unnamed
12481 namespace definition. */
12482 else if (token1.keyword == RID_NAMESPACE
12483 && (/* A named namespace definition. */
12484 (token2.type == CPP_NAME
12485 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12486 != CPP_EQ))
12487 || (token2.type == CPP_OPEN_SQUARE
12488 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12489 == CPP_OPEN_SQUARE)
12490 /* An unnamed namespace definition. */
12491 || token2.type == CPP_OPEN_BRACE
12492 || token2.keyword == RID_ATTRIBUTE))
12493 cp_parser_namespace_definition (parser);
12494 /* An inline (associated) namespace definition. */
12495 else if (token1.keyword == RID_INLINE
12496 && token2.keyword == RID_NAMESPACE)
12497 cp_parser_namespace_definition (parser);
12498 /* Objective-C++ declaration/definition. */
12499 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12500 cp_parser_objc_declaration (parser, NULL_TREE);
12501 else if (c_dialect_objc ()
12502 && token1.keyword == RID_ATTRIBUTE
12503 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12504 cp_parser_objc_declaration (parser, attributes);
12505 /* At this point we may have a template declared by a concept
12506 introduction. */
12507 else if (flag_concepts
12508 && cp_parser_template_declaration_after_export (parser,
12509 /*member_p=*/false))
12510 /* We did. */;
12511 else
12512 /* Try to parse a block-declaration, or a function-definition. */
12513 cp_parser_block_declaration (parser, /*statement_p=*/false);
12515 /* Free any declarators allocated. */
12516 obstack_free (&declarator_obstack, p);
12519 /* Parse a block-declaration.
12521 block-declaration:
12522 simple-declaration
12523 asm-definition
12524 namespace-alias-definition
12525 using-declaration
12526 using-directive
12528 GNU Extension:
12530 block-declaration:
12531 __extension__ block-declaration
12533 C++0x Extension:
12535 block-declaration:
12536 static_assert-declaration
12538 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12539 part of a declaration-statement. */
12541 static void
12542 cp_parser_block_declaration (cp_parser *parser,
12543 bool statement_p)
12545 cp_token *token1;
12546 int saved_pedantic;
12548 /* Check for the `__extension__' keyword. */
12549 if (cp_parser_extension_opt (parser, &saved_pedantic))
12551 /* Parse the qualified declaration. */
12552 cp_parser_block_declaration (parser, statement_p);
12553 /* Restore the PEDANTIC flag. */
12554 pedantic = saved_pedantic;
12556 return;
12559 /* Peek at the next token to figure out which kind of declaration is
12560 present. */
12561 token1 = cp_lexer_peek_token (parser->lexer);
12563 /* If the next keyword is `asm', we have an asm-definition. */
12564 if (token1->keyword == RID_ASM)
12566 if (statement_p)
12567 cp_parser_commit_to_tentative_parse (parser);
12568 cp_parser_asm_definition (parser);
12570 /* If the next keyword is `namespace', we have a
12571 namespace-alias-definition. */
12572 else if (token1->keyword == RID_NAMESPACE)
12573 cp_parser_namespace_alias_definition (parser);
12574 /* If the next keyword is `using', we have a
12575 using-declaration, a using-directive, or an alias-declaration. */
12576 else if (token1->keyword == RID_USING)
12578 cp_token *token2;
12580 if (statement_p)
12581 cp_parser_commit_to_tentative_parse (parser);
12582 /* If the token after `using' is `namespace', then we have a
12583 using-directive. */
12584 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12585 if (token2->keyword == RID_NAMESPACE)
12586 cp_parser_using_directive (parser);
12587 /* If the second token after 'using' is '=', then we have an
12588 alias-declaration. */
12589 else if (cxx_dialect >= cxx11
12590 && token2->type == CPP_NAME
12591 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12592 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12593 cp_parser_alias_declaration (parser);
12594 /* Otherwise, it's a using-declaration. */
12595 else
12596 cp_parser_using_declaration (parser,
12597 /*access_declaration_p=*/false);
12599 /* If the next keyword is `__label__' we have a misplaced label
12600 declaration. */
12601 else if (token1->keyword == RID_LABEL)
12603 cp_lexer_consume_token (parser->lexer);
12604 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12605 cp_parser_skip_to_end_of_statement (parser);
12606 /* If the next token is now a `;', consume it. */
12607 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12608 cp_lexer_consume_token (parser->lexer);
12610 /* If the next token is `static_assert' we have a static assertion. */
12611 else if (token1->keyword == RID_STATIC_ASSERT)
12612 cp_parser_static_assert (parser, /*member_p=*/false);
12613 /* Anything else must be a simple-declaration. */
12614 else
12615 cp_parser_simple_declaration (parser, !statement_p,
12616 /*maybe_range_for_decl*/NULL);
12619 /* Parse a simple-declaration.
12621 simple-declaration:
12622 decl-specifier-seq [opt] init-declarator-list [opt] ;
12623 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12624 brace-or-equal-initializer ;
12626 init-declarator-list:
12627 init-declarator
12628 init-declarator-list , init-declarator
12630 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12631 function-definition as a simple-declaration.
12633 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12634 parsed declaration if it is an uninitialized single declarator not followed
12635 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12636 if present, will not be consumed. */
12638 static void
12639 cp_parser_simple_declaration (cp_parser* parser,
12640 bool function_definition_allowed_p,
12641 tree *maybe_range_for_decl)
12643 cp_decl_specifier_seq decl_specifiers;
12644 int declares_class_or_enum;
12645 bool saw_declarator;
12646 location_t comma_loc = UNKNOWN_LOCATION;
12647 location_t init_loc = UNKNOWN_LOCATION;
12649 if (maybe_range_for_decl)
12650 *maybe_range_for_decl = NULL_TREE;
12652 /* Defer access checks until we know what is being declared; the
12653 checks for names appearing in the decl-specifier-seq should be
12654 done as if we were in the scope of the thing being declared. */
12655 push_deferring_access_checks (dk_deferred);
12657 /* Parse the decl-specifier-seq. We have to keep track of whether
12658 or not the decl-specifier-seq declares a named class or
12659 enumeration type, since that is the only case in which the
12660 init-declarator-list is allowed to be empty.
12662 [dcl.dcl]
12664 In a simple-declaration, the optional init-declarator-list can be
12665 omitted only when declaring a class or enumeration, that is when
12666 the decl-specifier-seq contains either a class-specifier, an
12667 elaborated-type-specifier, or an enum-specifier. */
12668 cp_parser_decl_specifier_seq (parser,
12669 CP_PARSER_FLAGS_OPTIONAL,
12670 &decl_specifiers,
12671 &declares_class_or_enum);
12672 /* We no longer need to defer access checks. */
12673 stop_deferring_access_checks ();
12675 /* In a block scope, a valid declaration must always have a
12676 decl-specifier-seq. By not trying to parse declarators, we can
12677 resolve the declaration/expression ambiguity more quickly. */
12678 if (!function_definition_allowed_p
12679 && !decl_specifiers.any_specifiers_p)
12681 cp_parser_error (parser, "expected declaration");
12682 goto done;
12685 /* If the next two tokens are both identifiers, the code is
12686 erroneous. The usual cause of this situation is code like:
12688 T t;
12690 where "T" should name a type -- but does not. */
12691 if (!decl_specifiers.any_type_specifiers_p
12692 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12694 /* If parsing tentatively, we should commit; we really are
12695 looking at a declaration. */
12696 cp_parser_commit_to_tentative_parse (parser);
12697 /* Give up. */
12698 goto done;
12701 /* If we have seen at least one decl-specifier, and the next token
12702 is not a parenthesis, then we must be looking at a declaration.
12703 (After "int (" we might be looking at a functional cast.) */
12704 if (decl_specifiers.any_specifiers_p
12705 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12706 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12707 && !cp_parser_error_occurred (parser))
12708 cp_parser_commit_to_tentative_parse (parser);
12710 /* Look for C++17 decomposition declaration. */
12711 for (size_t n = 1; ; n++)
12712 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12713 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12714 continue;
12715 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12716 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12717 && decl_specifiers.any_specifiers_p)
12719 tree decl
12720 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12721 maybe_range_for_decl,
12722 &init_loc);
12724 /* The next token should be either a `,' or a `;'. */
12725 cp_token *token = cp_lexer_peek_token (parser->lexer);
12726 /* If it's a `;', we are done. */
12727 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12728 goto finish;
12729 /* Anything else is an error. */
12730 else
12732 /* If we have already issued an error message we don't need
12733 to issue another one. */
12734 if ((decl != error_mark_node
12735 && DECL_INITIAL (decl) != error_mark_node)
12736 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12737 cp_parser_error (parser, "expected %<,%> or %<;%>");
12738 /* Skip tokens until we reach the end of the statement. */
12739 cp_parser_skip_to_end_of_statement (parser);
12740 /* If the next token is now a `;', consume it. */
12741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12742 cp_lexer_consume_token (parser->lexer);
12743 goto done;
12746 else
12747 break;
12749 tree last_type;
12750 bool auto_specifier_p;
12751 /* NULL_TREE if both variable and function declaration are allowed,
12752 error_mark_node if function declaration are not allowed and
12753 a FUNCTION_DECL that should be diagnosed if it is followed by
12754 variable declarations. */
12755 tree auto_function_declaration;
12757 last_type = NULL_TREE;
12758 auto_specifier_p
12759 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12760 auto_function_declaration = NULL_TREE;
12762 /* Keep going until we hit the `;' at the end of the simple
12763 declaration. */
12764 saw_declarator = false;
12765 while (cp_lexer_next_token_is_not (parser->lexer,
12766 CPP_SEMICOLON))
12768 cp_token *token;
12769 bool function_definition_p;
12770 tree decl;
12771 tree auto_result = NULL_TREE;
12773 if (saw_declarator)
12775 /* If we are processing next declarator, comma is expected */
12776 token = cp_lexer_peek_token (parser->lexer);
12777 gcc_assert (token->type == CPP_COMMA);
12778 cp_lexer_consume_token (parser->lexer);
12779 if (maybe_range_for_decl)
12781 *maybe_range_for_decl = error_mark_node;
12782 if (comma_loc == UNKNOWN_LOCATION)
12783 comma_loc = token->location;
12786 else
12787 saw_declarator = true;
12789 /* Parse the init-declarator. */
12790 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12791 /*checks=*/NULL,
12792 function_definition_allowed_p,
12793 /*member_p=*/false,
12794 declares_class_or_enum,
12795 &function_definition_p,
12796 maybe_range_for_decl,
12797 &init_loc,
12798 &auto_result);
12799 /* If an error occurred while parsing tentatively, exit quickly.
12800 (That usually happens when in the body of a function; each
12801 statement is treated as a declaration-statement until proven
12802 otherwise.) */
12803 if (cp_parser_error_occurred (parser))
12804 goto done;
12806 if (auto_specifier_p && cxx_dialect >= cxx14)
12808 /* If the init-declarator-list contains more than one
12809 init-declarator, they shall all form declarations of
12810 variables. */
12811 if (auto_function_declaration == NULL_TREE)
12812 auto_function_declaration
12813 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12814 else if (TREE_CODE (decl) == FUNCTION_DECL
12815 || auto_function_declaration != error_mark_node)
12817 error_at (decl_specifiers.locations[ds_type_spec],
12818 "non-variable %qD in declaration with more than one "
12819 "declarator with placeholder type",
12820 TREE_CODE (decl) == FUNCTION_DECL
12821 ? decl : auto_function_declaration);
12822 auto_function_declaration = error_mark_node;
12826 if (auto_result
12827 && (!processing_template_decl || !type_uses_auto (auto_result)))
12829 if (last_type
12830 && last_type != error_mark_node
12831 && !same_type_p (auto_result, last_type))
12833 /* If the list of declarators contains more than one declarator,
12834 the type of each declared variable is determined as described
12835 above. If the type deduced for the template parameter U is not
12836 the same in each deduction, the program is ill-formed. */
12837 error_at (decl_specifiers.locations[ds_type_spec],
12838 "inconsistent deduction for %qT: %qT and then %qT",
12839 decl_specifiers.type, last_type, auto_result);
12840 last_type = error_mark_node;
12842 else
12843 last_type = auto_result;
12846 /* Handle function definitions specially. */
12847 if (function_definition_p)
12849 /* If the next token is a `,', then we are probably
12850 processing something like:
12852 void f() {}, *p;
12854 which is erroneous. */
12855 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12857 cp_token *token = cp_lexer_peek_token (parser->lexer);
12858 error_at (token->location,
12859 "mixing"
12860 " declarations and function-definitions is forbidden");
12862 /* Otherwise, we're done with the list of declarators. */
12863 else
12865 pop_deferring_access_checks ();
12866 return;
12869 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12870 *maybe_range_for_decl = decl;
12871 /* The next token should be either a `,' or a `;'. */
12872 token = cp_lexer_peek_token (parser->lexer);
12873 /* If it's a `,', there are more declarators to come. */
12874 if (token->type == CPP_COMMA)
12875 /* will be consumed next time around */;
12876 /* If it's a `;', we are done. */
12877 else if (token->type == CPP_SEMICOLON)
12878 break;
12879 else if (maybe_range_for_decl)
12881 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
12882 permerror (decl_specifiers.locations[ds_type_spec],
12883 "types may not be defined in a for-range-declaration");
12884 break;
12886 /* Anything else is an error. */
12887 else
12889 /* If we have already issued an error message we don't need
12890 to issue another one. */
12891 if ((decl != error_mark_node
12892 && DECL_INITIAL (decl) != error_mark_node)
12893 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12894 cp_parser_error (parser, "expected %<,%> or %<;%>");
12895 /* Skip tokens until we reach the end of the statement. */
12896 cp_parser_skip_to_end_of_statement (parser);
12897 /* If the next token is now a `;', consume it. */
12898 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12899 cp_lexer_consume_token (parser->lexer);
12900 goto done;
12902 /* After the first time around, a function-definition is not
12903 allowed -- even if it was OK at first. For example:
12905 int i, f() {}
12907 is not valid. */
12908 function_definition_allowed_p = false;
12911 /* Issue an error message if no declarators are present, and the
12912 decl-specifier-seq does not itself declare a class or
12913 enumeration: [dcl.dcl]/3. */
12914 if (!saw_declarator)
12916 if (cp_parser_declares_only_class_p (parser))
12918 if (!declares_class_or_enum
12919 && decl_specifiers.type
12920 && OVERLOAD_TYPE_P (decl_specifiers.type))
12921 /* Ensure an error is issued anyway when finish_decltype_type,
12922 called via cp_parser_decl_specifier_seq, returns a class or
12923 an enumeration (c++/51786). */
12924 decl_specifiers.type = NULL_TREE;
12925 shadow_tag (&decl_specifiers);
12927 /* Perform any deferred access checks. */
12928 perform_deferred_access_checks (tf_warning_or_error);
12931 /* Consume the `;'. */
12932 finish:
12933 if (!maybe_range_for_decl)
12934 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12935 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12937 if (init_loc != UNKNOWN_LOCATION)
12938 error_at (init_loc, "initializer in range-based %<for%> loop");
12939 if (comma_loc != UNKNOWN_LOCATION)
12940 error_at (comma_loc,
12941 "multiple declarations in range-based %<for%> loop");
12944 done:
12945 pop_deferring_access_checks ();
12948 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12949 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12950 initializer ; */
12952 static tree
12953 cp_parser_decomposition_declaration (cp_parser *parser,
12954 cp_decl_specifier_seq *decl_specifiers,
12955 tree *maybe_range_for_decl,
12956 location_t *init_loc)
12958 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12959 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12960 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12962 /* Parse the identifier-list. */
12963 auto_vec<cp_expr, 10> v;
12964 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12965 while (true)
12967 cp_expr e = cp_parser_identifier (parser);
12968 if (e.get_value () == error_mark_node)
12969 break;
12970 v.safe_push (e);
12971 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12972 break;
12973 cp_lexer_consume_token (parser->lexer);
12976 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12977 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12979 end_loc = UNKNOWN_LOCATION;
12980 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12981 false);
12982 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12983 cp_lexer_consume_token (parser->lexer);
12984 else
12986 cp_parser_skip_to_end_of_statement (parser);
12987 return error_mark_node;
12991 if (cxx_dialect < cxx1z)
12992 pedwarn (loc, 0, "decomposition declaration only available with "
12993 "-std=c++1z or -std=gnu++1z");
12995 tree pushed_scope;
12996 cp_declarator *declarator = make_declarator (cdk_decomp);
12997 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
12998 declarator->id_loc = loc;
12999 if (ref_qual != REF_QUAL_NONE)
13000 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13001 ref_qual == REF_QUAL_RVALUE,
13002 NULL_TREE);
13003 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13004 NULL_TREE, decl_specifiers->attributes,
13005 &pushed_scope);
13006 tree orig_decl = decl;
13008 unsigned int i;
13009 cp_expr e;
13010 cp_decl_specifier_seq decl_specs;
13011 clear_decl_specs (&decl_specs);
13012 decl_specs.type = make_auto ();
13013 tree prev = decl;
13014 FOR_EACH_VEC_ELT (v, i, e)
13016 if (i == 0)
13017 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13018 else
13019 declarator->u.id.unqualified_name = e.get_value ();
13020 declarator->id_loc = e.get_location ();
13021 tree elt_pushed_scope;
13022 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13023 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13024 if (decl2 == error_mark_node)
13025 decl = error_mark_node;
13026 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13028 /* Ensure we've diagnosed redeclaration if we aren't creating
13029 a new VAR_DECL. */
13030 gcc_assert (errorcount);
13031 decl = error_mark_node;
13033 else
13034 prev = decl2;
13035 if (elt_pushed_scope)
13036 pop_scope (elt_pushed_scope);
13039 if (v.is_empty ())
13041 error_at (loc, "empty decomposition declaration");
13042 decl = error_mark_node;
13045 if (maybe_range_for_decl == NULL
13046 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13048 bool non_constant_p = false, is_direct_init = false;
13049 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13050 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13051 &non_constant_p);
13053 if (decl != error_mark_node)
13055 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13056 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13057 cp_finish_decomp (decl, prev, v.length ());
13060 else if (decl != error_mark_node)
13062 *maybe_range_for_decl = prev;
13063 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13064 the underlying DECL. */
13065 cp_finish_decomp (decl, prev, v.length ());
13068 if (pushed_scope)
13069 pop_scope (pushed_scope);
13071 if (decl == error_mark_node && DECL_P (orig_decl))
13073 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13074 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13077 return decl;
13080 /* Parse a decl-specifier-seq.
13082 decl-specifier-seq:
13083 decl-specifier-seq [opt] decl-specifier
13084 decl-specifier attribute-specifier-seq [opt] (C++11)
13086 decl-specifier:
13087 storage-class-specifier
13088 type-specifier
13089 function-specifier
13090 friend
13091 typedef
13093 GNU Extension:
13095 decl-specifier:
13096 attributes
13098 Concepts Extension:
13100 decl-specifier:
13101 concept
13103 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13105 The parser flags FLAGS is used to control type-specifier parsing.
13107 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13108 flags:
13110 1: one of the decl-specifiers is an elaborated-type-specifier
13111 (i.e., a type declaration)
13112 2: one of the decl-specifiers is an enum-specifier or a
13113 class-specifier (i.e., a type definition)
13117 static void
13118 cp_parser_decl_specifier_seq (cp_parser* parser,
13119 cp_parser_flags flags,
13120 cp_decl_specifier_seq *decl_specs,
13121 int* declares_class_or_enum)
13123 bool constructor_possible_p = !parser->in_declarator_p;
13124 bool found_decl_spec = false;
13125 cp_token *start_token = NULL;
13126 cp_decl_spec ds;
13128 /* Clear DECL_SPECS. */
13129 clear_decl_specs (decl_specs);
13131 /* Assume no class or enumeration type is declared. */
13132 *declares_class_or_enum = 0;
13134 /* Keep reading specifiers until there are no more to read. */
13135 while (true)
13137 bool constructor_p;
13138 cp_token *token;
13139 ds = ds_last;
13141 /* Peek at the next token. */
13142 token = cp_lexer_peek_token (parser->lexer);
13144 /* Save the first token of the decl spec list for error
13145 reporting. */
13146 if (!start_token)
13147 start_token = token;
13148 /* Handle attributes. */
13149 if (cp_next_tokens_can_be_attribute_p (parser))
13151 /* Parse the attributes. */
13152 tree attrs = cp_parser_attributes_opt (parser);
13154 /* In a sequence of declaration specifiers, c++11 attributes
13155 appertain to the type that precede them. In that case
13156 [dcl.spec]/1 says:
13158 The attribute-specifier-seq affects the type only for
13159 the declaration it appears in, not other declarations
13160 involving the same type.
13162 But for now let's force the user to position the
13163 attribute either at the beginning of the declaration or
13164 after the declarator-id, which would clearly mean that it
13165 applies to the declarator. */
13166 if (cxx11_attribute_p (attrs))
13168 if (!found_decl_spec)
13169 /* The c++11 attribute is at the beginning of the
13170 declaration. It appertains to the entity being
13171 declared. */;
13172 else
13174 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13176 /* This is an attribute following a
13177 class-specifier. */
13178 if (decl_specs->type_definition_p)
13179 warn_misplaced_attr_for_class_type (token->location,
13180 decl_specs->type);
13181 attrs = NULL_TREE;
13183 else
13185 decl_specs->std_attributes
13186 = chainon (decl_specs->std_attributes,
13187 attrs);
13188 if (decl_specs->locations[ds_std_attribute] == 0)
13189 decl_specs->locations[ds_std_attribute] = token->location;
13191 continue;
13195 decl_specs->attributes
13196 = chainon (decl_specs->attributes,
13197 attrs);
13198 if (decl_specs->locations[ds_attribute] == 0)
13199 decl_specs->locations[ds_attribute] = token->location;
13200 continue;
13202 /* Assume we will find a decl-specifier keyword. */
13203 found_decl_spec = true;
13204 /* If the next token is an appropriate keyword, we can simply
13205 add it to the list. */
13206 switch (token->keyword)
13208 /* decl-specifier:
13209 friend
13210 constexpr */
13211 case RID_FRIEND:
13212 if (!at_class_scope_p ())
13214 error_at (token->location, "%<friend%> used outside of class");
13215 cp_lexer_purge_token (parser->lexer);
13217 else
13219 ds = ds_friend;
13220 /* Consume the token. */
13221 cp_lexer_consume_token (parser->lexer);
13223 break;
13225 case RID_CONSTEXPR:
13226 ds = ds_constexpr;
13227 cp_lexer_consume_token (parser->lexer);
13228 break;
13230 case RID_CONCEPT:
13231 ds = ds_concept;
13232 cp_lexer_consume_token (parser->lexer);
13233 break;
13235 /* function-specifier:
13236 inline
13237 virtual
13238 explicit */
13239 case RID_INLINE:
13240 case RID_VIRTUAL:
13241 case RID_EXPLICIT:
13242 cp_parser_function_specifier_opt (parser, decl_specs);
13243 break;
13245 /* decl-specifier:
13246 typedef */
13247 case RID_TYPEDEF:
13248 ds = ds_typedef;
13249 /* Consume the token. */
13250 cp_lexer_consume_token (parser->lexer);
13251 /* A constructor declarator cannot appear in a typedef. */
13252 constructor_possible_p = false;
13253 /* The "typedef" keyword can only occur in a declaration; we
13254 may as well commit at this point. */
13255 cp_parser_commit_to_tentative_parse (parser);
13257 if (decl_specs->storage_class != sc_none)
13258 decl_specs->conflicting_specifiers_p = true;
13259 break;
13261 /* storage-class-specifier:
13262 auto
13263 register
13264 static
13265 extern
13266 mutable
13268 GNU Extension:
13269 thread */
13270 case RID_AUTO:
13271 if (cxx_dialect == cxx98)
13273 /* Consume the token. */
13274 cp_lexer_consume_token (parser->lexer);
13276 /* Complain about `auto' as a storage specifier, if
13277 we're complaining about C++0x compatibility. */
13278 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13279 " changes meaning in C++11; please remove it");
13281 /* Set the storage class anyway. */
13282 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13283 token);
13285 else
13286 /* C++0x auto type-specifier. */
13287 found_decl_spec = false;
13288 break;
13290 case RID_REGISTER:
13291 case RID_STATIC:
13292 case RID_EXTERN:
13293 case RID_MUTABLE:
13294 /* Consume the token. */
13295 cp_lexer_consume_token (parser->lexer);
13296 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13297 token);
13298 break;
13299 case RID_THREAD:
13300 /* Consume the token. */
13301 ds = ds_thread;
13302 cp_lexer_consume_token (parser->lexer);
13303 break;
13305 default:
13306 /* We did not yet find a decl-specifier yet. */
13307 found_decl_spec = false;
13308 break;
13311 if (found_decl_spec
13312 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13313 && token->keyword != RID_CONSTEXPR)
13314 error ("decl-specifier invalid in condition");
13316 if (found_decl_spec
13317 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13318 && token->keyword != RID_MUTABLE
13319 && token->keyword != RID_CONSTEXPR)
13320 error_at (token->location, "%qD invalid in lambda",
13321 ridpointers[token->keyword]);
13323 if (ds != ds_last)
13324 set_and_check_decl_spec_loc (decl_specs, ds, token);
13326 /* Constructors are a special case. The `S' in `S()' is not a
13327 decl-specifier; it is the beginning of the declarator. */
13328 constructor_p
13329 = (!found_decl_spec
13330 && constructor_possible_p
13331 && (cp_parser_constructor_declarator_p
13332 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13334 /* If we don't have a DECL_SPEC yet, then we must be looking at
13335 a type-specifier. */
13336 if (!found_decl_spec && !constructor_p)
13338 int decl_spec_declares_class_or_enum;
13339 bool is_cv_qualifier;
13340 tree type_spec;
13342 type_spec
13343 = cp_parser_type_specifier (parser, flags,
13344 decl_specs,
13345 /*is_declaration=*/true,
13346 &decl_spec_declares_class_or_enum,
13347 &is_cv_qualifier);
13348 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13350 /* If this type-specifier referenced a user-defined type
13351 (a typedef, class-name, etc.), then we can't allow any
13352 more such type-specifiers henceforth.
13354 [dcl.spec]
13356 The longest sequence of decl-specifiers that could
13357 possibly be a type name is taken as the
13358 decl-specifier-seq of a declaration. The sequence shall
13359 be self-consistent as described below.
13361 [dcl.type]
13363 As a general rule, at most one type-specifier is allowed
13364 in the complete decl-specifier-seq of a declaration. The
13365 only exceptions are the following:
13367 -- const or volatile can be combined with any other
13368 type-specifier.
13370 -- signed or unsigned can be combined with char, long,
13371 short, or int.
13373 -- ..
13375 Example:
13377 typedef char* Pc;
13378 void g (const int Pc);
13380 Here, Pc is *not* part of the decl-specifier seq; it's
13381 the declarator. Therefore, once we see a type-specifier
13382 (other than a cv-qualifier), we forbid any additional
13383 user-defined types. We *do* still allow things like `int
13384 int' to be considered a decl-specifier-seq, and issue the
13385 error message later. */
13386 if (type_spec && !is_cv_qualifier)
13387 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13388 /* A constructor declarator cannot follow a type-specifier. */
13389 if (type_spec)
13391 constructor_possible_p = false;
13392 found_decl_spec = true;
13393 if (!is_cv_qualifier)
13394 decl_specs->any_type_specifiers_p = true;
13398 /* If we still do not have a DECL_SPEC, then there are no more
13399 decl-specifiers. */
13400 if (!found_decl_spec)
13401 break;
13403 decl_specs->any_specifiers_p = true;
13404 /* After we see one decl-specifier, further decl-specifiers are
13405 always optional. */
13406 flags |= CP_PARSER_FLAGS_OPTIONAL;
13409 /* Don't allow a friend specifier with a class definition. */
13410 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13411 && (*declares_class_or_enum & 2))
13412 error_at (decl_specs->locations[ds_friend],
13413 "class definition may not be declared a friend");
13416 /* Parse an (optional) storage-class-specifier.
13418 storage-class-specifier:
13419 auto
13420 register
13421 static
13422 extern
13423 mutable
13425 GNU Extension:
13427 storage-class-specifier:
13428 thread
13430 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13432 static tree
13433 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13435 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13437 case RID_AUTO:
13438 if (cxx_dialect != cxx98)
13439 return NULL_TREE;
13440 /* Fall through for C++98. */
13441 gcc_fallthrough ();
13443 case RID_REGISTER:
13444 case RID_STATIC:
13445 case RID_EXTERN:
13446 case RID_MUTABLE:
13447 case RID_THREAD:
13448 /* Consume the token. */
13449 return cp_lexer_consume_token (parser->lexer)->u.value;
13451 default:
13452 return NULL_TREE;
13456 /* Parse an (optional) function-specifier.
13458 function-specifier:
13459 inline
13460 virtual
13461 explicit
13463 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13464 Updates DECL_SPECS, if it is non-NULL. */
13466 static tree
13467 cp_parser_function_specifier_opt (cp_parser* parser,
13468 cp_decl_specifier_seq *decl_specs)
13470 cp_token *token = cp_lexer_peek_token (parser->lexer);
13471 switch (token->keyword)
13473 case RID_INLINE:
13474 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13475 break;
13477 case RID_VIRTUAL:
13478 /* 14.5.2.3 [temp.mem]
13480 A member function template shall not be virtual. */
13481 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13482 && current_class_type)
13483 error_at (token->location, "templates may not be %<virtual%>");
13484 else
13485 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13486 break;
13488 case RID_EXPLICIT:
13489 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13490 break;
13492 default:
13493 return NULL_TREE;
13496 /* Consume the token. */
13497 return cp_lexer_consume_token (parser->lexer)->u.value;
13500 /* Parse a linkage-specification.
13502 linkage-specification:
13503 extern string-literal { declaration-seq [opt] }
13504 extern string-literal declaration */
13506 static void
13507 cp_parser_linkage_specification (cp_parser* parser)
13509 tree linkage;
13511 /* Look for the `extern' keyword. */
13512 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13514 /* Look for the string-literal. */
13515 linkage = cp_parser_string_literal (parser, false, false);
13517 /* Transform the literal into an identifier. If the literal is a
13518 wide-character string, or contains embedded NULs, then we can't
13519 handle it as the user wants. */
13520 if (strlen (TREE_STRING_POINTER (linkage))
13521 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13523 cp_parser_error (parser, "invalid linkage-specification");
13524 /* Assume C++ linkage. */
13525 linkage = lang_name_cplusplus;
13527 else
13528 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13530 /* We're now using the new linkage. */
13531 push_lang_context (linkage);
13533 /* If the next token is a `{', then we're using the first
13534 production. */
13535 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13537 cp_ensure_no_omp_declare_simd (parser);
13538 cp_ensure_no_oacc_routine (parser);
13540 /* Consume the `{' token. */
13541 cp_lexer_consume_token (parser->lexer);
13542 /* Parse the declarations. */
13543 cp_parser_declaration_seq_opt (parser);
13544 /* Look for the closing `}'. */
13545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13547 /* Otherwise, there's just one declaration. */
13548 else
13550 bool saved_in_unbraced_linkage_specification_p;
13552 saved_in_unbraced_linkage_specification_p
13553 = parser->in_unbraced_linkage_specification_p;
13554 parser->in_unbraced_linkage_specification_p = true;
13555 cp_parser_declaration (parser);
13556 parser->in_unbraced_linkage_specification_p
13557 = saved_in_unbraced_linkage_specification_p;
13560 /* We're done with the linkage-specification. */
13561 pop_lang_context ();
13564 /* Parse a static_assert-declaration.
13566 static_assert-declaration:
13567 static_assert ( constant-expression , string-literal ) ;
13568 static_assert ( constant-expression ) ; (C++1Z)
13570 If MEMBER_P, this static_assert is a class member. */
13572 static void
13573 cp_parser_static_assert(cp_parser *parser, bool member_p)
13575 tree condition;
13576 tree message;
13577 cp_token *token;
13578 location_t saved_loc;
13579 bool dummy;
13581 /* Peek at the `static_assert' token so we can keep track of exactly
13582 where the static assertion started. */
13583 token = cp_lexer_peek_token (parser->lexer);
13584 saved_loc = token->location;
13586 /* Look for the `static_assert' keyword. */
13587 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13588 RT_STATIC_ASSERT))
13589 return;
13591 /* We know we are in a static assertion; commit to any tentative
13592 parse. */
13593 if (cp_parser_parsing_tentatively (parser))
13594 cp_parser_commit_to_tentative_parse (parser);
13596 /* Parse the `(' starting the static assertion condition. */
13597 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13599 /* Parse the constant-expression. Allow a non-constant expression
13600 here in order to give better diagnostics in finish_static_assert. */
13601 condition =
13602 cp_parser_constant_expression (parser,
13603 /*allow_non_constant_p=*/true,
13604 /*non_constant_p=*/&dummy);
13606 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13608 if (cxx_dialect < cxx1z)
13609 pedwarn (input_location, OPT_Wpedantic,
13610 "static_assert without a message "
13611 "only available with -std=c++1z or -std=gnu++1z");
13612 /* Eat the ')' */
13613 cp_lexer_consume_token (parser->lexer);
13614 message = build_string (1, "");
13615 TREE_TYPE (message) = char_array_type_node;
13616 fix_string_type (message);
13618 else
13620 /* Parse the separating `,'. */
13621 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13623 /* Parse the string-literal message. */
13624 message = cp_parser_string_literal (parser,
13625 /*translate=*/false,
13626 /*wide_ok=*/true);
13628 /* A `)' completes the static assertion. */
13629 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13630 cp_parser_skip_to_closing_parenthesis (parser,
13631 /*recovering=*/true,
13632 /*or_comma=*/false,
13633 /*consume_paren=*/true);
13636 /* A semicolon terminates the declaration. */
13637 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13639 /* Complete the static assertion, which may mean either processing
13640 the static assert now or saving it for template instantiation. */
13641 finish_static_assert (condition, message, saved_loc, member_p);
13644 /* Parse the expression in decltype ( expression ). */
13646 static tree
13647 cp_parser_decltype_expr (cp_parser *parser,
13648 bool &id_expression_or_member_access_p)
13650 cp_token *id_expr_start_token;
13651 tree expr;
13653 /* Since we're going to preserve any side-effects from this parse, set up a
13654 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13655 in the expression. */
13656 tentative_firewall firewall (parser);
13658 /* First, try parsing an id-expression. */
13659 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13660 cp_parser_parse_tentatively (parser);
13661 expr = cp_parser_id_expression (parser,
13662 /*template_keyword_p=*/false,
13663 /*check_dependency_p=*/true,
13664 /*template_p=*/NULL,
13665 /*declarator_p=*/false,
13666 /*optional_p=*/false);
13668 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13670 bool non_integral_constant_expression_p = false;
13671 tree id_expression = expr;
13672 cp_id_kind idk;
13673 const char *error_msg;
13675 if (identifier_p (expr))
13676 /* Lookup the name we got back from the id-expression. */
13677 expr = cp_parser_lookup_name_simple (parser, expr,
13678 id_expr_start_token->location);
13680 if (expr
13681 && expr != error_mark_node
13682 && TREE_CODE (expr) != TYPE_DECL
13683 && (TREE_CODE (expr) != BIT_NOT_EXPR
13684 || !TYPE_P (TREE_OPERAND (expr, 0)))
13685 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13687 /* Complete lookup of the id-expression. */
13688 expr = (finish_id_expression
13689 (id_expression, expr, parser->scope, &idk,
13690 /*integral_constant_expression_p=*/false,
13691 /*allow_non_integral_constant_expression_p=*/true,
13692 &non_integral_constant_expression_p,
13693 /*template_p=*/false,
13694 /*done=*/true,
13695 /*address_p=*/false,
13696 /*template_arg_p=*/false,
13697 &error_msg,
13698 id_expr_start_token->location));
13700 if (expr == error_mark_node)
13701 /* We found an id-expression, but it was something that we
13702 should not have found. This is an error, not something
13703 we can recover from, so note that we found an
13704 id-expression and we'll recover as gracefully as
13705 possible. */
13706 id_expression_or_member_access_p = true;
13709 if (expr
13710 && expr != error_mark_node
13711 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13712 /* We have an id-expression. */
13713 id_expression_or_member_access_p = true;
13716 if (!id_expression_or_member_access_p)
13718 /* Abort the id-expression parse. */
13719 cp_parser_abort_tentative_parse (parser);
13721 /* Parsing tentatively, again. */
13722 cp_parser_parse_tentatively (parser);
13724 /* Parse a class member access. */
13725 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13726 /*cast_p=*/false, /*decltype*/true,
13727 /*member_access_only_p=*/true, NULL);
13729 if (expr
13730 && expr != error_mark_node
13731 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13732 /* We have an id-expression. */
13733 id_expression_or_member_access_p = true;
13736 if (id_expression_or_member_access_p)
13737 /* We have parsed the complete id-expression or member access. */
13738 cp_parser_parse_definitely (parser);
13739 else
13741 /* Abort our attempt to parse an id-expression or member access
13742 expression. */
13743 cp_parser_abort_tentative_parse (parser);
13745 /* Parse a full expression. */
13746 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13747 /*decltype_p=*/true);
13750 return expr;
13753 /* Parse a `decltype' type. Returns the type.
13755 simple-type-specifier:
13756 decltype ( expression )
13757 C++14 proposal:
13758 decltype ( auto ) */
13760 static tree
13761 cp_parser_decltype (cp_parser *parser)
13763 tree expr;
13764 bool id_expression_or_member_access_p = false;
13765 const char *saved_message;
13766 bool saved_integral_constant_expression_p;
13767 bool saved_non_integral_constant_expression_p;
13768 bool saved_greater_than_is_operator_p;
13769 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13771 if (start_token->type == CPP_DECLTYPE)
13773 /* Already parsed. */
13774 cp_lexer_consume_token (parser->lexer);
13775 return saved_checks_value (start_token->u.tree_check_value);
13778 /* Look for the `decltype' token. */
13779 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13780 return error_mark_node;
13782 /* Parse the opening `('. */
13783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13784 return error_mark_node;
13786 /* decltype (auto) */
13787 if (cxx_dialect >= cxx14
13788 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13790 cp_lexer_consume_token (parser->lexer);
13791 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13792 return error_mark_node;
13793 expr = make_decltype_auto ();
13794 AUTO_IS_DECLTYPE (expr) = true;
13795 goto rewrite;
13798 /* Types cannot be defined in a `decltype' expression. Save away the
13799 old message. */
13800 saved_message = parser->type_definition_forbidden_message;
13802 /* And create the new one. */
13803 parser->type_definition_forbidden_message
13804 = G_("types may not be defined in %<decltype%> expressions");
13806 /* The restrictions on constant-expressions do not apply inside
13807 decltype expressions. */
13808 saved_integral_constant_expression_p
13809 = parser->integral_constant_expression_p;
13810 saved_non_integral_constant_expression_p
13811 = parser->non_integral_constant_expression_p;
13812 parser->integral_constant_expression_p = false;
13814 /* Within a parenthesized expression, a `>' token is always
13815 the greater-than operator. */
13816 saved_greater_than_is_operator_p
13817 = parser->greater_than_is_operator_p;
13818 parser->greater_than_is_operator_p = true;
13820 /* Do not actually evaluate the expression. */
13821 ++cp_unevaluated_operand;
13823 /* Do not warn about problems with the expression. */
13824 ++c_inhibit_evaluation_warnings;
13826 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13828 /* Go back to evaluating expressions. */
13829 --cp_unevaluated_operand;
13830 --c_inhibit_evaluation_warnings;
13832 /* The `>' token might be the end of a template-id or
13833 template-parameter-list now. */
13834 parser->greater_than_is_operator_p
13835 = saved_greater_than_is_operator_p;
13837 /* Restore the old message and the integral constant expression
13838 flags. */
13839 parser->type_definition_forbidden_message = saved_message;
13840 parser->integral_constant_expression_p
13841 = saved_integral_constant_expression_p;
13842 parser->non_integral_constant_expression_p
13843 = saved_non_integral_constant_expression_p;
13845 /* Parse to the closing `)'. */
13846 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13848 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13849 /*consume_paren=*/true);
13850 return error_mark_node;
13853 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13854 tf_warning_or_error);
13856 rewrite:
13857 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13858 it again. */
13859 start_token->type = CPP_DECLTYPE;
13860 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13861 start_token->u.tree_check_value->value = expr;
13862 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13863 start_token->keyword = RID_MAX;
13864 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13866 return expr;
13869 /* Special member functions [gram.special] */
13871 /* Parse a conversion-function-id.
13873 conversion-function-id:
13874 operator conversion-type-id
13876 Returns an IDENTIFIER_NODE representing the operator. */
13878 static tree
13879 cp_parser_conversion_function_id (cp_parser* parser)
13881 tree type;
13882 tree saved_scope;
13883 tree saved_qualifying_scope;
13884 tree saved_object_scope;
13885 tree pushed_scope = NULL_TREE;
13887 /* Look for the `operator' token. */
13888 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13889 return error_mark_node;
13890 /* When we parse the conversion-type-id, the current scope will be
13891 reset. However, we need that information in able to look up the
13892 conversion function later, so we save it here. */
13893 saved_scope = parser->scope;
13894 saved_qualifying_scope = parser->qualifying_scope;
13895 saved_object_scope = parser->object_scope;
13896 /* We must enter the scope of the class so that the names of
13897 entities declared within the class are available in the
13898 conversion-type-id. For example, consider:
13900 struct S {
13901 typedef int I;
13902 operator I();
13905 S::operator I() { ... }
13907 In order to see that `I' is a type-name in the definition, we
13908 must be in the scope of `S'. */
13909 if (saved_scope)
13910 pushed_scope = push_scope (saved_scope);
13911 /* Parse the conversion-type-id. */
13912 type = cp_parser_conversion_type_id (parser);
13913 /* Leave the scope of the class, if any. */
13914 if (pushed_scope)
13915 pop_scope (pushed_scope);
13916 /* Restore the saved scope. */
13917 parser->scope = saved_scope;
13918 parser->qualifying_scope = saved_qualifying_scope;
13919 parser->object_scope = saved_object_scope;
13920 /* If the TYPE is invalid, indicate failure. */
13921 if (type == error_mark_node)
13922 return error_mark_node;
13923 return mangle_conv_op_name_for_type (type);
13926 /* Parse a conversion-type-id:
13928 conversion-type-id:
13929 type-specifier-seq conversion-declarator [opt]
13931 Returns the TYPE specified. */
13933 static tree
13934 cp_parser_conversion_type_id (cp_parser* parser)
13936 tree attributes;
13937 cp_decl_specifier_seq type_specifiers;
13938 cp_declarator *declarator;
13939 tree type_specified;
13940 const char *saved_message;
13942 /* Parse the attributes. */
13943 attributes = cp_parser_attributes_opt (parser);
13945 saved_message = parser->type_definition_forbidden_message;
13946 parser->type_definition_forbidden_message
13947 = G_("types may not be defined in a conversion-type-id");
13949 /* Parse the type-specifiers. */
13950 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13951 /*is_trailing_return=*/false,
13952 &type_specifiers);
13954 parser->type_definition_forbidden_message = saved_message;
13956 /* If that didn't work, stop. */
13957 if (type_specifiers.type == error_mark_node)
13958 return error_mark_node;
13959 /* Parse the conversion-declarator. */
13960 declarator = cp_parser_conversion_declarator_opt (parser);
13962 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13963 /*initialized=*/0, &attributes);
13964 if (attributes)
13965 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13967 /* Don't give this error when parsing tentatively. This happens to
13968 work because we always parse this definitively once. */
13969 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13970 && type_uses_auto (type_specified))
13972 if (cxx_dialect < cxx14)
13974 error ("invalid use of %<auto%> in conversion operator");
13975 return error_mark_node;
13977 else if (template_parm_scope_p ())
13978 warning (0, "use of %<auto%> in member template "
13979 "conversion operator can never be deduced");
13982 return type_specified;
13985 /* Parse an (optional) conversion-declarator.
13987 conversion-declarator:
13988 ptr-operator conversion-declarator [opt]
13992 static cp_declarator *
13993 cp_parser_conversion_declarator_opt (cp_parser* parser)
13995 enum tree_code code;
13996 tree class_type, std_attributes = NULL_TREE;
13997 cp_cv_quals cv_quals;
13999 /* We don't know if there's a ptr-operator next, or not. */
14000 cp_parser_parse_tentatively (parser);
14001 /* Try the ptr-operator. */
14002 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14003 &std_attributes);
14004 /* If it worked, look for more conversion-declarators. */
14005 if (cp_parser_parse_definitely (parser))
14007 cp_declarator *declarator;
14009 /* Parse another optional declarator. */
14010 declarator = cp_parser_conversion_declarator_opt (parser);
14012 declarator = cp_parser_make_indirect_declarator
14013 (code, class_type, cv_quals, declarator, std_attributes);
14015 return declarator;
14018 return NULL;
14021 /* Parse an (optional) ctor-initializer.
14023 ctor-initializer:
14024 : mem-initializer-list
14026 Returns TRUE iff the ctor-initializer was actually present. */
14028 static bool
14029 cp_parser_ctor_initializer_opt (cp_parser* parser)
14031 /* If the next token is not a `:', then there is no
14032 ctor-initializer. */
14033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14035 /* Do default initialization of any bases and members. */
14036 if (DECL_CONSTRUCTOR_P (current_function_decl))
14037 finish_mem_initializers (NULL_TREE);
14039 return false;
14042 /* Consume the `:' token. */
14043 cp_lexer_consume_token (parser->lexer);
14044 /* And the mem-initializer-list. */
14045 cp_parser_mem_initializer_list (parser);
14047 return true;
14050 /* Parse a mem-initializer-list.
14052 mem-initializer-list:
14053 mem-initializer ... [opt]
14054 mem-initializer ... [opt] , mem-initializer-list */
14056 static void
14057 cp_parser_mem_initializer_list (cp_parser* parser)
14059 tree mem_initializer_list = NULL_TREE;
14060 tree target_ctor = error_mark_node;
14061 cp_token *token = cp_lexer_peek_token (parser->lexer);
14063 /* Let the semantic analysis code know that we are starting the
14064 mem-initializer-list. */
14065 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14066 error_at (token->location,
14067 "only constructors take member initializers");
14069 /* Loop through the list. */
14070 while (true)
14072 tree mem_initializer;
14074 token = cp_lexer_peek_token (parser->lexer);
14075 /* Parse the mem-initializer. */
14076 mem_initializer = cp_parser_mem_initializer (parser);
14077 /* If the next token is a `...', we're expanding member initializers. */
14078 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14080 /* Consume the `...'. */
14081 cp_lexer_consume_token (parser->lexer);
14083 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14084 can be expanded but members cannot. */
14085 if (mem_initializer != error_mark_node
14086 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14088 error_at (token->location,
14089 "cannot expand initializer for member %qD",
14090 TREE_PURPOSE (mem_initializer));
14091 mem_initializer = error_mark_node;
14094 /* Construct the pack expansion type. */
14095 if (mem_initializer != error_mark_node)
14096 mem_initializer = make_pack_expansion (mem_initializer);
14098 if (target_ctor != error_mark_node
14099 && mem_initializer != error_mark_node)
14101 error ("mem-initializer for %qD follows constructor delegation",
14102 TREE_PURPOSE (mem_initializer));
14103 mem_initializer = error_mark_node;
14105 /* Look for a target constructor. */
14106 if (mem_initializer != error_mark_node
14107 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14108 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14110 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14111 if (mem_initializer_list)
14113 error ("constructor delegation follows mem-initializer for %qD",
14114 TREE_PURPOSE (mem_initializer_list));
14115 mem_initializer = error_mark_node;
14117 target_ctor = mem_initializer;
14119 /* Add it to the list, unless it was erroneous. */
14120 if (mem_initializer != error_mark_node)
14122 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14123 mem_initializer_list = mem_initializer;
14125 /* If the next token is not a `,', we're done. */
14126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14127 break;
14128 /* Consume the `,' token. */
14129 cp_lexer_consume_token (parser->lexer);
14132 /* Perform semantic analysis. */
14133 if (DECL_CONSTRUCTOR_P (current_function_decl))
14134 finish_mem_initializers (mem_initializer_list);
14137 /* Parse a mem-initializer.
14139 mem-initializer:
14140 mem-initializer-id ( expression-list [opt] )
14141 mem-initializer-id braced-init-list
14143 GNU extension:
14145 mem-initializer:
14146 ( expression-list [opt] )
14148 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14149 class) or FIELD_DECL (for a non-static data member) to initialize;
14150 the TREE_VALUE is the expression-list. An empty initialization
14151 list is represented by void_list_node. */
14153 static tree
14154 cp_parser_mem_initializer (cp_parser* parser)
14156 tree mem_initializer_id;
14157 tree expression_list;
14158 tree member;
14159 cp_token *token = cp_lexer_peek_token (parser->lexer);
14161 /* Find out what is being initialized. */
14162 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14164 permerror (token->location,
14165 "anachronistic old-style base class initializer");
14166 mem_initializer_id = NULL_TREE;
14168 else
14170 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14171 if (mem_initializer_id == error_mark_node)
14172 return mem_initializer_id;
14174 member = expand_member_init (mem_initializer_id);
14175 if (member && !DECL_P (member))
14176 in_base_initializer = 1;
14178 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14180 bool expr_non_constant_p;
14181 cp_lexer_set_source_position (parser->lexer);
14182 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14183 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14184 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14185 expression_list = build_tree_list (NULL_TREE, expression_list);
14187 else
14189 vec<tree, va_gc> *vec;
14190 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14191 /*cast_p=*/false,
14192 /*allow_expansion_p=*/true,
14193 /*non_constant_p=*/NULL);
14194 if (vec == NULL)
14195 return error_mark_node;
14196 expression_list = build_tree_list_vec (vec);
14197 release_tree_vector (vec);
14200 if (expression_list == error_mark_node)
14201 return error_mark_node;
14202 if (!expression_list)
14203 expression_list = void_type_node;
14205 in_base_initializer = 0;
14207 return member ? build_tree_list (member, expression_list) : error_mark_node;
14210 /* Parse a mem-initializer-id.
14212 mem-initializer-id:
14213 :: [opt] nested-name-specifier [opt] class-name
14214 decltype-specifier (C++11)
14215 identifier
14217 Returns a TYPE indicating the class to be initialized for the first
14218 production (and the second in C++11). Returns an IDENTIFIER_NODE
14219 indicating the data member to be initialized for the last production. */
14221 static tree
14222 cp_parser_mem_initializer_id (cp_parser* parser)
14224 bool global_scope_p;
14225 bool nested_name_specifier_p;
14226 bool template_p = false;
14227 tree id;
14229 cp_token *token = cp_lexer_peek_token (parser->lexer);
14231 /* `typename' is not allowed in this context ([temp.res]). */
14232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14234 error_at (token->location,
14235 "keyword %<typename%> not allowed in this context (a qualified "
14236 "member initializer is implicitly a type)");
14237 cp_lexer_consume_token (parser->lexer);
14239 /* Look for the optional `::' operator. */
14240 global_scope_p
14241 = (cp_parser_global_scope_opt (parser,
14242 /*current_scope_valid_p=*/false)
14243 != NULL_TREE);
14244 /* Look for the optional nested-name-specifier. The simplest way to
14245 implement:
14247 [temp.res]
14249 The keyword `typename' is not permitted in a base-specifier or
14250 mem-initializer; in these contexts a qualified name that
14251 depends on a template-parameter is implicitly assumed to be a
14252 type name.
14254 is to assume that we have seen the `typename' keyword at this
14255 point. */
14256 nested_name_specifier_p
14257 = (cp_parser_nested_name_specifier_opt (parser,
14258 /*typename_keyword_p=*/true,
14259 /*check_dependency_p=*/true,
14260 /*type_p=*/true,
14261 /*is_declaration=*/true)
14262 != NULL_TREE);
14263 if (nested_name_specifier_p)
14264 template_p = cp_parser_optional_template_keyword (parser);
14265 /* If there is a `::' operator or a nested-name-specifier, then we
14266 are definitely looking for a class-name. */
14267 if (global_scope_p || nested_name_specifier_p)
14268 return cp_parser_class_name (parser,
14269 /*typename_keyword_p=*/true,
14270 /*template_keyword_p=*/template_p,
14271 typename_type,
14272 /*check_dependency_p=*/true,
14273 /*class_head_p=*/false,
14274 /*is_declaration=*/true);
14275 /* Otherwise, we could also be looking for an ordinary identifier. */
14276 cp_parser_parse_tentatively (parser);
14277 if (cp_lexer_next_token_is_decltype (parser->lexer))
14278 /* Try a decltype-specifier. */
14279 id = cp_parser_decltype (parser);
14280 else
14281 /* Otherwise, try a class-name. */
14282 id = cp_parser_class_name (parser,
14283 /*typename_keyword_p=*/true,
14284 /*template_keyword_p=*/false,
14285 none_type,
14286 /*check_dependency_p=*/true,
14287 /*class_head_p=*/false,
14288 /*is_declaration=*/true);
14289 /* If we found one, we're done. */
14290 if (cp_parser_parse_definitely (parser))
14291 return id;
14292 /* Otherwise, look for an ordinary identifier. */
14293 return cp_parser_identifier (parser);
14296 /* Overloading [gram.over] */
14298 /* Parse an operator-function-id.
14300 operator-function-id:
14301 operator operator
14303 Returns an IDENTIFIER_NODE for the operator which is a
14304 human-readable spelling of the identifier, e.g., `operator +'. */
14306 static cp_expr
14307 cp_parser_operator_function_id (cp_parser* parser)
14309 /* Look for the `operator' keyword. */
14310 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14311 return error_mark_node;
14312 /* And then the name of the operator itself. */
14313 return cp_parser_operator (parser);
14316 /* Return an identifier node for a user-defined literal operator.
14317 The suffix identifier is chained to the operator name identifier. */
14319 tree
14320 cp_literal_operator_id (const char* name)
14322 tree identifier;
14323 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14324 + strlen (name) + 10);
14325 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14326 identifier = get_identifier (buffer);
14328 return identifier;
14331 /* Parse an operator.
14333 operator:
14334 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14335 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14336 || ++ -- , ->* -> () []
14338 GNU Extensions:
14340 operator:
14341 <? >? <?= >?=
14343 Returns an IDENTIFIER_NODE for the operator which is a
14344 human-readable spelling of the identifier, e.g., `operator +'. */
14346 static cp_expr
14347 cp_parser_operator (cp_parser* parser)
14349 tree id = NULL_TREE;
14350 cp_token *token;
14351 bool utf8 = false;
14353 /* Peek at the next token. */
14354 token = cp_lexer_peek_token (parser->lexer);
14356 location_t start_loc = token->location;
14358 /* Figure out which operator we have. */
14359 switch (token->type)
14361 case CPP_KEYWORD:
14363 enum tree_code op;
14365 /* The keyword should be either `new' or `delete'. */
14366 if (token->keyword == RID_NEW)
14367 op = NEW_EXPR;
14368 else if (token->keyword == RID_DELETE)
14369 op = DELETE_EXPR;
14370 else
14371 break;
14373 /* Consume the `new' or `delete' token. */
14374 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14376 /* Peek at the next token. */
14377 token = cp_lexer_peek_token (parser->lexer);
14378 /* If it's a `[' token then this is the array variant of the
14379 operator. */
14380 if (token->type == CPP_OPEN_SQUARE)
14382 /* Consume the `[' token. */
14383 cp_lexer_consume_token (parser->lexer);
14384 /* Look for the `]' token. */
14385 if (cp_token *close_token
14386 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14387 end_loc = close_token->location;
14388 id = cp_operator_id (op == NEW_EXPR
14389 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14391 /* Otherwise, we have the non-array variant. */
14392 else
14393 id = cp_operator_id (op);
14395 location_t loc = make_location (start_loc, start_loc, end_loc);
14397 return cp_expr (id, loc);
14400 case CPP_PLUS:
14401 id = cp_operator_id (PLUS_EXPR);
14402 break;
14404 case CPP_MINUS:
14405 id = cp_operator_id (MINUS_EXPR);
14406 break;
14408 case CPP_MULT:
14409 id = cp_operator_id (MULT_EXPR);
14410 break;
14412 case CPP_DIV:
14413 id = cp_operator_id (TRUNC_DIV_EXPR);
14414 break;
14416 case CPP_MOD:
14417 id = cp_operator_id (TRUNC_MOD_EXPR);
14418 break;
14420 case CPP_XOR:
14421 id = cp_operator_id (BIT_XOR_EXPR);
14422 break;
14424 case CPP_AND:
14425 id = cp_operator_id (BIT_AND_EXPR);
14426 break;
14428 case CPP_OR:
14429 id = cp_operator_id (BIT_IOR_EXPR);
14430 break;
14432 case CPP_COMPL:
14433 id = cp_operator_id (BIT_NOT_EXPR);
14434 break;
14436 case CPP_NOT:
14437 id = cp_operator_id (TRUTH_NOT_EXPR);
14438 break;
14440 case CPP_EQ:
14441 id = cp_assignment_operator_id (NOP_EXPR);
14442 break;
14444 case CPP_LESS:
14445 id = cp_operator_id (LT_EXPR);
14446 break;
14448 case CPP_GREATER:
14449 id = cp_operator_id (GT_EXPR);
14450 break;
14452 case CPP_PLUS_EQ:
14453 id = cp_assignment_operator_id (PLUS_EXPR);
14454 break;
14456 case CPP_MINUS_EQ:
14457 id = cp_assignment_operator_id (MINUS_EXPR);
14458 break;
14460 case CPP_MULT_EQ:
14461 id = cp_assignment_operator_id (MULT_EXPR);
14462 break;
14464 case CPP_DIV_EQ:
14465 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14466 break;
14468 case CPP_MOD_EQ:
14469 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14470 break;
14472 case CPP_XOR_EQ:
14473 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14474 break;
14476 case CPP_AND_EQ:
14477 id = cp_assignment_operator_id (BIT_AND_EXPR);
14478 break;
14480 case CPP_OR_EQ:
14481 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14482 break;
14484 case CPP_LSHIFT:
14485 id = cp_operator_id (LSHIFT_EXPR);
14486 break;
14488 case CPP_RSHIFT:
14489 id = cp_operator_id (RSHIFT_EXPR);
14490 break;
14492 case CPP_LSHIFT_EQ:
14493 id = cp_assignment_operator_id (LSHIFT_EXPR);
14494 break;
14496 case CPP_RSHIFT_EQ:
14497 id = cp_assignment_operator_id (RSHIFT_EXPR);
14498 break;
14500 case CPP_EQ_EQ:
14501 id = cp_operator_id (EQ_EXPR);
14502 break;
14504 case CPP_NOT_EQ:
14505 id = cp_operator_id (NE_EXPR);
14506 break;
14508 case CPP_LESS_EQ:
14509 id = cp_operator_id (LE_EXPR);
14510 break;
14512 case CPP_GREATER_EQ:
14513 id = cp_operator_id (GE_EXPR);
14514 break;
14516 case CPP_AND_AND:
14517 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14518 break;
14520 case CPP_OR_OR:
14521 id = cp_operator_id (TRUTH_ORIF_EXPR);
14522 break;
14524 case CPP_PLUS_PLUS:
14525 id = cp_operator_id (POSTINCREMENT_EXPR);
14526 break;
14528 case CPP_MINUS_MINUS:
14529 id = cp_operator_id (PREDECREMENT_EXPR);
14530 break;
14532 case CPP_COMMA:
14533 id = cp_operator_id (COMPOUND_EXPR);
14534 break;
14536 case CPP_DEREF_STAR:
14537 id = cp_operator_id (MEMBER_REF);
14538 break;
14540 case CPP_DEREF:
14541 id = cp_operator_id (COMPONENT_REF);
14542 break;
14544 case CPP_OPEN_PAREN:
14545 /* Consume the `('. */
14546 cp_lexer_consume_token (parser->lexer);
14547 /* Look for the matching `)'. */
14548 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14549 return cp_operator_id (CALL_EXPR);
14551 case CPP_OPEN_SQUARE:
14552 /* Consume the `['. */
14553 cp_lexer_consume_token (parser->lexer);
14554 /* Look for the matching `]'. */
14555 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14556 return cp_operator_id (ARRAY_REF);
14558 case CPP_UTF8STRING:
14559 case CPP_UTF8STRING_USERDEF:
14560 utf8 = true;
14561 /* FALLTHRU */
14562 case CPP_STRING:
14563 case CPP_WSTRING:
14564 case CPP_STRING16:
14565 case CPP_STRING32:
14566 case CPP_STRING_USERDEF:
14567 case CPP_WSTRING_USERDEF:
14568 case CPP_STRING16_USERDEF:
14569 case CPP_STRING32_USERDEF:
14571 tree str, string_tree;
14572 int sz, len;
14574 if (cxx_dialect == cxx98)
14575 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14577 /* Consume the string. */
14578 str = cp_parser_string_literal (parser, /*translate=*/true,
14579 /*wide_ok=*/true, /*lookup_udlit=*/false);
14580 if (str == error_mark_node)
14581 return error_mark_node;
14582 else if (TREE_CODE (str) == USERDEF_LITERAL)
14584 string_tree = USERDEF_LITERAL_VALUE (str);
14585 id = USERDEF_LITERAL_SUFFIX_ID (str);
14587 else
14589 string_tree = str;
14590 /* Look for the suffix identifier. */
14591 token = cp_lexer_peek_token (parser->lexer);
14592 if (token->type == CPP_NAME)
14593 id = cp_parser_identifier (parser);
14594 else if (token->type == CPP_KEYWORD)
14596 error ("unexpected keyword;"
14597 " remove space between quotes and suffix identifier");
14598 return error_mark_node;
14600 else
14602 error ("expected suffix identifier");
14603 return error_mark_node;
14606 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14607 (TREE_TYPE (TREE_TYPE (string_tree))));
14608 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14609 if (len != 0)
14611 error ("expected empty string after %<operator%> keyword");
14612 return error_mark_node;
14614 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14615 != char_type_node)
14617 error ("invalid encoding prefix in literal operator");
14618 return error_mark_node;
14620 if (id != error_mark_node)
14622 const char *name = IDENTIFIER_POINTER (id);
14623 id = cp_literal_operator_id (name);
14625 return id;
14628 default:
14629 /* Anything else is an error. */
14630 break;
14633 /* If we have selected an identifier, we need to consume the
14634 operator token. */
14635 if (id)
14636 cp_lexer_consume_token (parser->lexer);
14637 /* Otherwise, no valid operator name was present. */
14638 else
14640 cp_parser_error (parser, "expected operator");
14641 id = error_mark_node;
14644 return cp_expr (id, start_loc);
14647 /* Parse a template-declaration.
14649 template-declaration:
14650 export [opt] template < template-parameter-list > declaration
14652 If MEMBER_P is TRUE, this template-declaration occurs within a
14653 class-specifier.
14655 The grammar rule given by the standard isn't correct. What
14656 is really meant is:
14658 template-declaration:
14659 export [opt] template-parameter-list-seq
14660 decl-specifier-seq [opt] init-declarator [opt] ;
14661 export [opt] template-parameter-list-seq
14662 function-definition
14664 template-parameter-list-seq:
14665 template-parameter-list-seq [opt]
14666 template < template-parameter-list >
14668 Concept Extensions:
14670 template-parameter-list-seq:
14671 template < template-parameter-list > requires-clause [opt]
14673 requires-clause:
14674 requires logical-or-expression */
14676 static void
14677 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14679 /* Check for `export'. */
14680 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14682 /* Consume the `export' token. */
14683 cp_lexer_consume_token (parser->lexer);
14684 /* Warn that we do not support `export'. */
14685 warning (0, "keyword %<export%> not implemented, and will be ignored");
14688 cp_parser_template_declaration_after_export (parser, member_p);
14691 /* Parse a template-parameter-list.
14693 template-parameter-list:
14694 template-parameter
14695 template-parameter-list , template-parameter
14697 Returns a TREE_LIST. Each node represents a template parameter.
14698 The nodes are connected via their TREE_CHAINs. */
14700 static tree
14701 cp_parser_template_parameter_list (cp_parser* parser)
14703 tree parameter_list = NULL_TREE;
14705 begin_template_parm_list ();
14707 /* The loop below parses the template parms. We first need to know
14708 the total number of template parms to be able to compute proper
14709 canonical types of each dependent type. So after the loop, when
14710 we know the total number of template parms,
14711 end_template_parm_list computes the proper canonical types and
14712 fixes up the dependent types accordingly. */
14713 while (true)
14715 tree parameter;
14716 bool is_non_type;
14717 bool is_parameter_pack;
14718 location_t parm_loc;
14720 /* Parse the template-parameter. */
14721 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14722 parameter = cp_parser_template_parameter (parser,
14723 &is_non_type,
14724 &is_parameter_pack);
14725 /* Add it to the list. */
14726 if (parameter != error_mark_node)
14727 parameter_list = process_template_parm (parameter_list,
14728 parm_loc,
14729 parameter,
14730 is_non_type,
14731 is_parameter_pack);
14732 else
14734 tree err_parm = build_tree_list (parameter, parameter);
14735 parameter_list = chainon (parameter_list, err_parm);
14738 /* If the next token is not a `,', we're done. */
14739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14740 break;
14741 /* Otherwise, consume the `,' token. */
14742 cp_lexer_consume_token (parser->lexer);
14745 return end_template_parm_list (parameter_list);
14748 /* Parse a introduction-list.
14750 introduction-list:
14751 introduced-parameter
14752 introduction-list , introduced-parameter
14754 introduced-parameter:
14755 ...[opt] identifier
14757 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14758 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14759 WILDCARD_DECL will also have DECL_NAME set and token location in
14760 DECL_SOURCE_LOCATION. */
14762 static tree
14763 cp_parser_introduction_list (cp_parser *parser)
14765 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14767 while (true)
14769 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14770 if (is_pack)
14771 cp_lexer_consume_token (parser->lexer);
14773 /* Build placeholder. */
14774 tree parm = build_nt (WILDCARD_DECL);
14775 DECL_SOURCE_LOCATION (parm)
14776 = cp_lexer_peek_token (parser->lexer)->location;
14777 DECL_NAME (parm) = cp_parser_identifier (parser);
14778 WILDCARD_PACK_P (parm) = is_pack;
14779 vec_safe_push (introduction_vec, parm);
14781 /* If the next token is not a `,', we're done. */
14782 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14783 break;
14784 /* Otherwise, consume the `,' token. */
14785 cp_lexer_consume_token (parser->lexer);
14788 /* Convert the vec into a TREE_VEC. */
14789 tree introduction_list = make_tree_vec (introduction_vec->length ());
14790 unsigned int n;
14791 tree parm;
14792 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14793 TREE_VEC_ELT (introduction_list, n) = parm;
14795 release_tree_vector (introduction_vec);
14796 return introduction_list;
14799 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14800 is an abstract declarator. */
14802 static inline cp_declarator*
14803 get_id_declarator (cp_declarator *declarator)
14805 cp_declarator *d = declarator;
14806 while (d && d->kind != cdk_id)
14807 d = d->declarator;
14808 return d;
14811 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14812 is an abstract declarator. */
14814 static inline tree
14815 get_unqualified_id (cp_declarator *declarator)
14817 declarator = get_id_declarator (declarator);
14818 if (declarator)
14819 return declarator->u.id.unqualified_name;
14820 else
14821 return NULL_TREE;
14824 /* Returns true if DECL represents a constrained-parameter. */
14826 static inline bool
14827 is_constrained_parameter (tree decl)
14829 return (decl
14830 && TREE_CODE (decl) == TYPE_DECL
14831 && CONSTRAINED_PARM_CONCEPT (decl)
14832 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14835 /* Returns true if PARM declares a constrained-parameter. */
14837 static inline bool
14838 is_constrained_parameter (cp_parameter_declarator *parm)
14840 return is_constrained_parameter (parm->decl_specifiers.type);
14843 /* Check that the type parameter is only a declarator-id, and that its
14844 type is not cv-qualified. */
14846 bool
14847 cp_parser_check_constrained_type_parm (cp_parser *parser,
14848 cp_parameter_declarator *parm)
14850 if (!parm->declarator)
14851 return true;
14853 if (parm->declarator->kind != cdk_id)
14855 cp_parser_error (parser, "invalid constrained type parameter");
14856 return false;
14859 /* Don't allow cv-qualified type parameters. */
14860 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14861 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14863 cp_parser_error (parser, "cv-qualified type parameter");
14864 return false;
14867 return true;
14870 /* Finish parsing/processing a template type parameter and checking
14871 various restrictions. */
14873 static inline tree
14874 cp_parser_constrained_type_template_parm (cp_parser *parser,
14875 tree id,
14876 cp_parameter_declarator* parmdecl)
14878 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14879 return finish_template_type_parm (class_type_node, id);
14880 else
14881 return error_mark_node;
14884 static tree
14885 finish_constrained_template_template_parm (tree proto, tree id)
14887 /* FIXME: This should probably be copied, and we may need to adjust
14888 the template parameter depths. */
14889 tree saved_parms = current_template_parms;
14890 begin_template_parm_list ();
14891 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14892 end_template_parm_list ();
14894 tree parm = finish_template_template_parm (class_type_node, id);
14895 current_template_parms = saved_parms;
14897 return parm;
14900 /* Finish parsing/processing a template template parameter by borrowing
14901 the template parameter list from the prototype parameter. */
14903 static tree
14904 cp_parser_constrained_template_template_parm (cp_parser *parser,
14905 tree proto,
14906 tree id,
14907 cp_parameter_declarator *parmdecl)
14909 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14910 return error_mark_node;
14911 return finish_constrained_template_template_parm (proto, id);
14914 /* Create a new non-type template parameter from the given PARM
14915 declarator. */
14917 static tree
14918 constrained_non_type_template_parm (bool *is_non_type,
14919 cp_parameter_declarator *parm)
14921 *is_non_type = true;
14922 cp_declarator *decl = parm->declarator;
14923 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14924 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14925 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14928 /* Build a constrained template parameter based on the PARMDECL
14929 declarator. The type of PARMDECL is the constrained type, which
14930 refers to the prototype template parameter that ultimately
14931 specifies the type of the declared parameter. */
14933 static tree
14934 finish_constrained_parameter (cp_parser *parser,
14935 cp_parameter_declarator *parmdecl,
14936 bool *is_non_type,
14937 bool *is_parameter_pack)
14939 tree decl = parmdecl->decl_specifiers.type;
14940 tree id = get_unqualified_id (parmdecl->declarator);
14941 tree def = parmdecl->default_argument;
14942 tree proto = DECL_INITIAL (decl);
14944 /* A template parameter constrained by a variadic concept shall also
14945 be declared as a template parameter pack. */
14946 bool is_variadic = template_parameter_pack_p (proto);
14947 if (is_variadic && !*is_parameter_pack)
14948 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14950 /* Build the parameter. Return an error if the declarator was invalid. */
14951 tree parm;
14952 if (TREE_CODE (proto) == TYPE_DECL)
14953 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14954 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14955 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14956 parmdecl);
14957 else
14958 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14959 if (parm == error_mark_node)
14960 return error_mark_node;
14962 /* Finish the parameter decl and create a node attaching the
14963 default argument and constraint. */
14964 parm = build_tree_list (def, parm);
14965 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14967 return parm;
14970 /* Returns true if the parsed type actually represents the declaration
14971 of a type template-parameter. */
14973 static inline bool
14974 declares_constrained_type_template_parameter (tree type)
14976 return (is_constrained_parameter (type)
14977 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14981 /* Returns true if the parsed type actually represents the declaration of
14982 a template template-parameter. */
14984 static bool
14985 declares_constrained_template_template_parameter (tree type)
14987 return (is_constrained_parameter (type)
14988 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14991 /* Parse a default argument for a type template-parameter.
14992 Note that diagnostics are handled in cp_parser_template_parameter. */
14994 static tree
14995 cp_parser_default_type_template_argument (cp_parser *parser)
14997 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14999 /* Consume the `=' token. */
15000 cp_lexer_consume_token (parser->lexer);
15002 cp_token *token = cp_lexer_peek_token (parser->lexer);
15004 /* Parse the default-argument. */
15005 push_deferring_access_checks (dk_no_deferred);
15006 tree default_argument = cp_parser_type_id (parser);
15007 pop_deferring_access_checks ();
15009 if (flag_concepts && type_uses_auto (default_argument))
15011 error_at (token->location,
15012 "invalid use of %<auto%> in default template argument");
15013 return error_mark_node;
15016 return default_argument;
15019 /* Parse a default argument for a template template-parameter. */
15021 static tree
15022 cp_parser_default_template_template_argument (cp_parser *parser)
15024 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15026 bool is_template;
15028 /* Consume the `='. */
15029 cp_lexer_consume_token (parser->lexer);
15030 /* Parse the id-expression. */
15031 push_deferring_access_checks (dk_no_deferred);
15032 /* save token before parsing the id-expression, for error
15033 reporting */
15034 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15035 tree default_argument
15036 = cp_parser_id_expression (parser,
15037 /*template_keyword_p=*/false,
15038 /*check_dependency_p=*/true,
15039 /*template_p=*/&is_template,
15040 /*declarator_p=*/false,
15041 /*optional_p=*/false);
15042 if (TREE_CODE (default_argument) == TYPE_DECL)
15043 /* If the id-expression was a template-id that refers to
15044 a template-class, we already have the declaration here,
15045 so no further lookup is needed. */
15047 else
15048 /* Look up the name. */
15049 default_argument
15050 = cp_parser_lookup_name (parser, default_argument,
15051 none_type,
15052 /*is_template=*/is_template,
15053 /*is_namespace=*/false,
15054 /*check_dependency=*/true,
15055 /*ambiguous_decls=*/NULL,
15056 token->location);
15057 /* See if the default argument is valid. */
15058 default_argument = check_template_template_default_arg (default_argument);
15059 pop_deferring_access_checks ();
15060 return default_argument;
15063 /* Parse a template-parameter.
15065 template-parameter:
15066 type-parameter
15067 parameter-declaration
15069 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15070 the parameter. The TREE_PURPOSE is the default value, if any.
15071 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15072 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15073 set to true iff this parameter is a parameter pack. */
15075 static tree
15076 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15077 bool *is_parameter_pack)
15079 cp_token *token;
15080 cp_parameter_declarator *parameter_declarator;
15081 tree parm;
15083 /* Assume it is a type parameter or a template parameter. */
15084 *is_non_type = false;
15085 /* Assume it not a parameter pack. */
15086 *is_parameter_pack = false;
15087 /* Peek at the next token. */
15088 token = cp_lexer_peek_token (parser->lexer);
15089 /* If it is `class' or `template', we have a type-parameter. */
15090 if (token->keyword == RID_TEMPLATE)
15091 return cp_parser_type_parameter (parser, is_parameter_pack);
15092 /* If it is `class' or `typename' we do not know yet whether it is a
15093 type parameter or a non-type parameter. Consider:
15095 template <typename T, typename T::X X> ...
15099 template <class C, class D*> ...
15101 Here, the first parameter is a type parameter, and the second is
15102 a non-type parameter. We can tell by looking at the token after
15103 the identifier -- if it is a `,', `=', or `>' then we have a type
15104 parameter. */
15105 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15107 /* Peek at the token after `class' or `typename'. */
15108 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15109 /* If it's an ellipsis, we have a template type parameter
15110 pack. */
15111 if (token->type == CPP_ELLIPSIS)
15112 return cp_parser_type_parameter (parser, is_parameter_pack);
15113 /* If it's an identifier, skip it. */
15114 if (token->type == CPP_NAME)
15115 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15116 /* Now, see if the token looks like the end of a template
15117 parameter. */
15118 if (token->type == CPP_COMMA
15119 || token->type == CPP_EQ
15120 || token->type == CPP_GREATER)
15121 return cp_parser_type_parameter (parser, is_parameter_pack);
15124 /* Otherwise, it is a non-type parameter or a constrained parameter.
15126 [temp.param]
15128 When parsing a default template-argument for a non-type
15129 template-parameter, the first non-nested `>' is taken as the end
15130 of the template parameter-list rather than a greater-than
15131 operator. */
15132 parameter_declarator
15133 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15134 /*parenthesized_p=*/NULL);
15136 if (!parameter_declarator)
15137 return error_mark_node;
15139 /* If the parameter declaration is marked as a parameter pack, set
15140 *IS_PARAMETER_PACK to notify the caller. */
15141 if (parameter_declarator->template_parameter_pack_p)
15142 *is_parameter_pack = true;
15144 if (parameter_declarator->default_argument)
15146 /* Can happen in some cases of erroneous input (c++/34892). */
15147 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15148 /* Consume the `...' for better error recovery. */
15149 cp_lexer_consume_token (parser->lexer);
15152 // The parameter may have been constrained.
15153 if (is_constrained_parameter (parameter_declarator))
15154 return finish_constrained_parameter (parser,
15155 parameter_declarator,
15156 is_non_type,
15157 is_parameter_pack);
15159 // Now we're sure that the parameter is a non-type parameter.
15160 *is_non_type = true;
15162 parm = grokdeclarator (parameter_declarator->declarator,
15163 &parameter_declarator->decl_specifiers,
15164 TPARM, /*initialized=*/0,
15165 /*attrlist=*/NULL);
15166 if (parm == error_mark_node)
15167 return error_mark_node;
15169 return build_tree_list (parameter_declarator->default_argument, parm);
15172 /* Parse a type-parameter.
15174 type-parameter:
15175 class identifier [opt]
15176 class identifier [opt] = type-id
15177 typename identifier [opt]
15178 typename identifier [opt] = type-id
15179 template < template-parameter-list > class identifier [opt]
15180 template < template-parameter-list > class identifier [opt]
15181 = id-expression
15183 GNU Extension (variadic templates):
15185 type-parameter:
15186 class ... identifier [opt]
15187 typename ... identifier [opt]
15189 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15190 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15191 the declaration of the parameter.
15193 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15195 static tree
15196 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15198 cp_token *token;
15199 tree parameter;
15201 /* Look for a keyword to tell us what kind of parameter this is. */
15202 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15203 if (!token)
15204 return error_mark_node;
15206 switch (token->keyword)
15208 case RID_CLASS:
15209 case RID_TYPENAME:
15211 tree identifier;
15212 tree default_argument;
15214 /* If the next token is an ellipsis, we have a template
15215 argument pack. */
15216 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15218 /* Consume the `...' token. */
15219 cp_lexer_consume_token (parser->lexer);
15220 maybe_warn_variadic_templates ();
15222 *is_parameter_pack = true;
15225 /* If the next token is an identifier, then it names the
15226 parameter. */
15227 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15228 identifier = cp_parser_identifier (parser);
15229 else
15230 identifier = NULL_TREE;
15232 /* Create the parameter. */
15233 parameter = finish_template_type_parm (class_type_node, identifier);
15235 /* If the next token is an `=', we have a default argument. */
15236 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15238 default_argument
15239 = cp_parser_default_type_template_argument (parser);
15241 /* Template parameter packs cannot have default
15242 arguments. */
15243 if (*is_parameter_pack)
15245 if (identifier)
15246 error_at (token->location,
15247 "template parameter pack %qD cannot have a "
15248 "default argument", identifier);
15249 else
15250 error_at (token->location,
15251 "template parameter packs cannot have "
15252 "default arguments");
15253 default_argument = NULL_TREE;
15255 else if (check_for_bare_parameter_packs (default_argument))
15256 default_argument = error_mark_node;
15258 else
15259 default_argument = NULL_TREE;
15261 /* Create the combined representation of the parameter and the
15262 default argument. */
15263 parameter = build_tree_list (default_argument, parameter);
15265 break;
15267 case RID_TEMPLATE:
15269 tree identifier;
15270 tree default_argument;
15272 /* Look for the `<'. */
15273 cp_parser_require (parser, CPP_LESS, RT_LESS);
15274 /* Parse the template-parameter-list. */
15275 cp_parser_template_parameter_list (parser);
15276 /* Look for the `>'. */
15277 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15279 // If template requirements are present, parse them.
15280 if (flag_concepts)
15282 tree reqs = get_shorthand_constraints (current_template_parms);
15283 if (tree r = cp_parser_requires_clause_opt (parser))
15284 reqs = conjoin_constraints (reqs, normalize_expression (r));
15285 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15288 /* Look for the `class' or 'typename' keywords. */
15289 cp_parser_type_parameter_key (parser);
15290 /* If the next token is an ellipsis, we have a template
15291 argument pack. */
15292 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15294 /* Consume the `...' token. */
15295 cp_lexer_consume_token (parser->lexer);
15296 maybe_warn_variadic_templates ();
15298 *is_parameter_pack = true;
15300 /* If the next token is an `=', then there is a
15301 default-argument. If the next token is a `>', we are at
15302 the end of the parameter-list. If the next token is a `,',
15303 then we are at the end of this parameter. */
15304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15305 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15306 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15308 identifier = cp_parser_identifier (parser);
15309 /* Treat invalid names as if the parameter were nameless. */
15310 if (identifier == error_mark_node)
15311 identifier = NULL_TREE;
15313 else
15314 identifier = NULL_TREE;
15316 /* Create the template parameter. */
15317 parameter = finish_template_template_parm (class_type_node,
15318 identifier);
15320 /* If the next token is an `=', then there is a
15321 default-argument. */
15322 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15324 default_argument
15325 = cp_parser_default_template_template_argument (parser);
15327 /* Template parameter packs cannot have default
15328 arguments. */
15329 if (*is_parameter_pack)
15331 if (identifier)
15332 error_at (token->location,
15333 "template parameter pack %qD cannot "
15334 "have a default argument",
15335 identifier);
15336 else
15337 error_at (token->location, "template parameter packs cannot "
15338 "have default arguments");
15339 default_argument = NULL_TREE;
15342 else
15343 default_argument = NULL_TREE;
15345 /* Create the combined representation of the parameter and the
15346 default argument. */
15347 parameter = build_tree_list (default_argument, parameter);
15349 break;
15351 default:
15352 gcc_unreachable ();
15353 break;
15356 return parameter;
15359 /* Parse a template-id.
15361 template-id:
15362 template-name < template-argument-list [opt] >
15364 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15365 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15366 returned. Otherwise, if the template-name names a function, or set
15367 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15368 names a class, returns a TYPE_DECL for the specialization.
15370 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15371 uninstantiated templates. */
15373 static tree
15374 cp_parser_template_id (cp_parser *parser,
15375 bool template_keyword_p,
15376 bool check_dependency_p,
15377 enum tag_types tag_type,
15378 bool is_declaration)
15380 tree templ;
15381 tree arguments;
15382 tree template_id;
15383 cp_token_position start_of_id = 0;
15384 cp_token *next_token = NULL, *next_token_2 = NULL;
15385 bool is_identifier;
15387 /* If the next token corresponds to a template-id, there is no need
15388 to reparse it. */
15389 next_token = cp_lexer_peek_token (parser->lexer);
15390 if (next_token->type == CPP_TEMPLATE_ID)
15392 cp_lexer_consume_token (parser->lexer);
15393 return saved_checks_value (next_token->u.tree_check_value);
15396 /* Avoid performing name lookup if there is no possibility of
15397 finding a template-id. */
15398 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15399 || (next_token->type == CPP_NAME
15400 && !cp_parser_nth_token_starts_template_argument_list_p
15401 (parser, 2)))
15403 cp_parser_error (parser, "expected template-id");
15404 return error_mark_node;
15407 /* Remember where the template-id starts. */
15408 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15409 start_of_id = cp_lexer_token_position (parser->lexer, false);
15411 push_deferring_access_checks (dk_deferred);
15413 /* Parse the template-name. */
15414 is_identifier = false;
15415 templ = cp_parser_template_name (parser, template_keyword_p,
15416 check_dependency_p,
15417 is_declaration,
15418 tag_type,
15419 &is_identifier);
15420 if (templ == error_mark_node || is_identifier)
15422 pop_deferring_access_checks ();
15423 return templ;
15426 /* Since we're going to preserve any side-effects from this parse, set up a
15427 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15428 in the template arguments. */
15429 tentative_firewall firewall (parser);
15431 /* If we find the sequence `[:' after a template-name, it's probably
15432 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15433 parse correctly the argument list. */
15434 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15435 == CPP_OPEN_SQUARE)
15436 && next_token->flags & DIGRAPH
15437 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15438 == CPP_COLON)
15439 && !(next_token_2->flags & PREV_WHITE))
15441 cp_parser_parse_tentatively (parser);
15442 /* Change `:' into `::'. */
15443 next_token_2->type = CPP_SCOPE;
15444 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15445 CPP_LESS. */
15446 cp_lexer_consume_token (parser->lexer);
15448 /* Parse the arguments. */
15449 arguments = cp_parser_enclosed_template_argument_list (parser);
15450 if (!cp_parser_parse_definitely (parser))
15452 /* If we couldn't parse an argument list, then we revert our changes
15453 and return simply an error. Maybe this is not a template-id
15454 after all. */
15455 next_token_2->type = CPP_COLON;
15456 cp_parser_error (parser, "expected %<<%>");
15457 pop_deferring_access_checks ();
15458 return error_mark_node;
15460 /* Otherwise, emit an error about the invalid digraph, but continue
15461 parsing because we got our argument list. */
15462 if (permerror (next_token->location,
15463 "%<<::%> cannot begin a template-argument list"))
15465 static bool hint = false;
15466 inform (next_token->location,
15467 "%<<:%> is an alternate spelling for %<[%>."
15468 " Insert whitespace between %<<%> and %<::%>");
15469 if (!hint && !flag_permissive)
15471 inform (next_token->location, "(if you use %<-fpermissive%> "
15472 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15473 "accept your code)");
15474 hint = true;
15478 else
15480 /* Look for the `<' that starts the template-argument-list. */
15481 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15483 pop_deferring_access_checks ();
15484 return error_mark_node;
15486 /* Parse the arguments. */
15487 arguments = cp_parser_enclosed_template_argument_list (parser);
15490 /* Build a representation of the specialization. */
15491 if (identifier_p (templ))
15492 template_id = build_min_nt_loc (next_token->location,
15493 TEMPLATE_ID_EXPR,
15494 templ, arguments);
15495 else if (DECL_TYPE_TEMPLATE_P (templ)
15496 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15498 bool entering_scope;
15499 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15500 template (rather than some instantiation thereof) only if
15501 is not nested within some other construct. For example, in
15502 "template <typename T> void f(T) { A<T>::", A<T> is just an
15503 instantiation of A. */
15504 entering_scope = (template_parm_scope_p ()
15505 && cp_lexer_next_token_is (parser->lexer,
15506 CPP_SCOPE));
15507 template_id
15508 = finish_template_type (templ, arguments, entering_scope);
15510 /* A template-like identifier may be a partial concept id. */
15511 else if (flag_concepts
15512 && (template_id = (cp_parser_maybe_partial_concept_id
15513 (parser, templ, arguments))))
15514 return template_id;
15515 else if (variable_template_p (templ))
15517 template_id = lookup_template_variable (templ, arguments);
15518 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15519 SET_EXPR_LOCATION (template_id, next_token->location);
15521 else
15523 /* If it's not a class-template or a template-template, it should be
15524 a function-template. */
15525 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15526 || TREE_CODE (templ) == OVERLOAD
15527 || BASELINK_P (templ)));
15529 template_id = lookup_template_function (templ, arguments);
15530 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15531 SET_EXPR_LOCATION (template_id, next_token->location);
15534 /* If parsing tentatively, replace the sequence of tokens that makes
15535 up the template-id with a CPP_TEMPLATE_ID token. That way,
15536 should we re-parse the token stream, we will not have to repeat
15537 the effort required to do the parse, nor will we issue duplicate
15538 error messages about problems during instantiation of the
15539 template. */
15540 if (start_of_id
15541 /* Don't do this if we had a parse error in a declarator; re-parsing
15542 might succeed if a name changes meaning (60361). */
15543 && !(cp_parser_error_occurred (parser)
15544 && cp_parser_parsing_tentatively (parser)
15545 && parser->in_declarator_p))
15547 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15549 /* Reset the contents of the START_OF_ID token. */
15550 token->type = CPP_TEMPLATE_ID;
15552 /* Update the location to be of the form:
15553 template-name < template-argument-list [opt] >
15554 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15555 with caret == start at the start of the template-name,
15556 ranging until the closing '>'. */
15557 location_t finish_loc
15558 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15559 location_t combined_loc
15560 = make_location (token->location, token->location, finish_loc);
15561 token->location = combined_loc;
15563 /* Retrieve any deferred checks. Do not pop this access checks yet
15564 so the memory will not be reclaimed during token replacing below. */
15565 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15566 token->u.tree_check_value->value = template_id;
15567 token->u.tree_check_value->checks = get_deferred_access_checks ();
15568 token->keyword = RID_MAX;
15570 /* Purge all subsequent tokens. */
15571 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15573 /* ??? Can we actually assume that, if template_id ==
15574 error_mark_node, we will have issued a diagnostic to the
15575 user, as opposed to simply marking the tentative parse as
15576 failed? */
15577 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15578 error_at (token->location, "parse error in template argument list");
15581 pop_to_parent_deferring_access_checks ();
15582 return template_id;
15585 /* Parse a template-name.
15587 template-name:
15588 identifier
15590 The standard should actually say:
15592 template-name:
15593 identifier
15594 operator-function-id
15596 A defect report has been filed about this issue.
15598 A conversion-function-id cannot be a template name because they cannot
15599 be part of a template-id. In fact, looking at this code:
15601 a.operator K<int>()
15603 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15604 It is impossible to call a templated conversion-function-id with an
15605 explicit argument list, since the only allowed template parameter is
15606 the type to which it is converting.
15608 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15609 `template' keyword, in a construction like:
15611 T::template f<3>()
15613 In that case `f' is taken to be a template-name, even though there
15614 is no way of knowing for sure.
15616 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15617 name refers to a set of overloaded functions, at least one of which
15618 is a template, or an IDENTIFIER_NODE with the name of the template,
15619 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15620 names are looked up inside uninstantiated templates. */
15622 static tree
15623 cp_parser_template_name (cp_parser* parser,
15624 bool template_keyword_p,
15625 bool check_dependency_p,
15626 bool is_declaration,
15627 enum tag_types tag_type,
15628 bool *is_identifier)
15630 tree identifier;
15631 tree decl;
15632 tree fns;
15633 cp_token *token = cp_lexer_peek_token (parser->lexer);
15635 /* If the next token is `operator', then we have either an
15636 operator-function-id or a conversion-function-id. */
15637 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15639 /* We don't know whether we're looking at an
15640 operator-function-id or a conversion-function-id. */
15641 cp_parser_parse_tentatively (parser);
15642 /* Try an operator-function-id. */
15643 identifier = cp_parser_operator_function_id (parser);
15644 /* If that didn't work, try a conversion-function-id. */
15645 if (!cp_parser_parse_definitely (parser))
15647 cp_parser_error (parser, "expected template-name");
15648 return error_mark_node;
15651 /* Look for the identifier. */
15652 else
15653 identifier = cp_parser_identifier (parser);
15655 /* If we didn't find an identifier, we don't have a template-id. */
15656 if (identifier == error_mark_node)
15657 return error_mark_node;
15659 /* If the name immediately followed the `template' keyword, then it
15660 is a template-name. However, if the next token is not `<', then
15661 we do not treat it as a template-name, since it is not being used
15662 as part of a template-id. This enables us to handle constructs
15663 like:
15665 template <typename T> struct S { S(); };
15666 template <typename T> S<T>::S();
15668 correctly. We would treat `S' as a template -- if it were `S<T>'
15669 -- but we do not if there is no `<'. */
15671 if (processing_template_decl
15672 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15674 /* In a declaration, in a dependent context, we pretend that the
15675 "template" keyword was present in order to improve error
15676 recovery. For example, given:
15678 template <typename T> void f(T::X<int>);
15680 we want to treat "X<int>" as a template-id. */
15681 if (is_declaration
15682 && !template_keyword_p
15683 && parser->scope && TYPE_P (parser->scope)
15684 && check_dependency_p
15685 && dependent_scope_p (parser->scope)
15686 /* Do not do this for dtors (or ctors), since they never
15687 need the template keyword before their name. */
15688 && !constructor_name_p (identifier, parser->scope))
15690 cp_token_position start = 0;
15692 /* Explain what went wrong. */
15693 error_at (token->location, "non-template %qD used as template",
15694 identifier);
15695 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15696 parser->scope, identifier);
15697 /* If parsing tentatively, find the location of the "<" token. */
15698 if (cp_parser_simulate_error (parser))
15699 start = cp_lexer_token_position (parser->lexer, true);
15700 /* Parse the template arguments so that we can issue error
15701 messages about them. */
15702 cp_lexer_consume_token (parser->lexer);
15703 cp_parser_enclosed_template_argument_list (parser);
15704 /* Skip tokens until we find a good place from which to
15705 continue parsing. */
15706 cp_parser_skip_to_closing_parenthesis (parser,
15707 /*recovering=*/true,
15708 /*or_comma=*/true,
15709 /*consume_paren=*/false);
15710 /* If parsing tentatively, permanently remove the
15711 template argument list. That will prevent duplicate
15712 error messages from being issued about the missing
15713 "template" keyword. */
15714 if (start)
15715 cp_lexer_purge_tokens_after (parser->lexer, start);
15716 if (is_identifier)
15717 *is_identifier = true;
15718 parser->context->object_type = NULL_TREE;
15719 return identifier;
15722 /* If the "template" keyword is present, then there is generally
15723 no point in doing name-lookup, so we just return IDENTIFIER.
15724 But, if the qualifying scope is non-dependent then we can
15725 (and must) do name-lookup normally. */
15726 if (template_keyword_p
15727 && (!parser->scope
15728 || (TYPE_P (parser->scope)
15729 && dependent_type_p (parser->scope))))
15731 /* We're optimizing away the call to cp_parser_lookup_name, but we
15732 still need to do this. */
15733 parser->context->object_type = NULL_TREE;
15734 return identifier;
15738 /* Look up the name. */
15739 decl = cp_parser_lookup_name (parser, identifier,
15740 tag_type,
15741 /*is_template=*/true,
15742 /*is_namespace=*/false,
15743 check_dependency_p,
15744 /*ambiguous_decls=*/NULL,
15745 token->location);
15747 decl = strip_using_decl (decl);
15749 /* If DECL is a template, then the name was a template-name. */
15750 if (TREE_CODE (decl) == TEMPLATE_DECL)
15752 if (TREE_DEPRECATED (decl)
15753 && deprecated_state != DEPRECATED_SUPPRESS)
15754 warn_deprecated_use (decl, NULL_TREE);
15756 else
15758 tree fn = NULL_TREE;
15760 /* The standard does not explicitly indicate whether a name that
15761 names a set of overloaded declarations, some of which are
15762 templates, is a template-name. However, such a name should
15763 be a template-name; otherwise, there is no way to form a
15764 template-id for the overloaded templates. */
15765 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15766 if (TREE_CODE (fns) == OVERLOAD)
15767 for (fn = fns; fn; fn = OVL_NEXT (fn))
15768 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15769 break;
15771 if (!fn)
15773 /* The name does not name a template. */
15774 cp_parser_error (parser, "expected template-name");
15775 return error_mark_node;
15779 /* If DECL is dependent, and refers to a function, then just return
15780 its name; we will look it up again during template instantiation. */
15781 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15783 tree scope = ovl_scope (decl);
15784 if (TYPE_P (scope) && dependent_type_p (scope))
15785 return identifier;
15788 return decl;
15791 /* Parse a template-argument-list.
15793 template-argument-list:
15794 template-argument ... [opt]
15795 template-argument-list , template-argument ... [opt]
15797 Returns a TREE_VEC containing the arguments. */
15799 static tree
15800 cp_parser_template_argument_list (cp_parser* parser)
15802 tree fixed_args[10];
15803 unsigned n_args = 0;
15804 unsigned alloced = 10;
15805 tree *arg_ary = fixed_args;
15806 tree vec;
15807 bool saved_in_template_argument_list_p;
15808 bool saved_ice_p;
15809 bool saved_non_ice_p;
15811 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15812 parser->in_template_argument_list_p = true;
15813 /* Even if the template-id appears in an integral
15814 constant-expression, the contents of the argument list do
15815 not. */
15816 saved_ice_p = parser->integral_constant_expression_p;
15817 parser->integral_constant_expression_p = false;
15818 saved_non_ice_p = parser->non_integral_constant_expression_p;
15819 parser->non_integral_constant_expression_p = false;
15821 /* Parse the arguments. */
15824 tree argument;
15826 if (n_args)
15827 /* Consume the comma. */
15828 cp_lexer_consume_token (parser->lexer);
15830 /* Parse the template-argument. */
15831 argument = cp_parser_template_argument (parser);
15833 /* If the next token is an ellipsis, we're expanding a template
15834 argument pack. */
15835 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15837 if (argument == error_mark_node)
15839 cp_token *token = cp_lexer_peek_token (parser->lexer);
15840 error_at (token->location,
15841 "expected parameter pack before %<...%>");
15843 /* Consume the `...' token. */
15844 cp_lexer_consume_token (parser->lexer);
15846 /* Make the argument into a TYPE_PACK_EXPANSION or
15847 EXPR_PACK_EXPANSION. */
15848 argument = make_pack_expansion (argument);
15851 if (n_args == alloced)
15853 alloced *= 2;
15855 if (arg_ary == fixed_args)
15857 arg_ary = XNEWVEC (tree, alloced);
15858 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15860 else
15861 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15863 arg_ary[n_args++] = argument;
15865 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15867 vec = make_tree_vec (n_args);
15869 while (n_args--)
15870 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15872 if (arg_ary != fixed_args)
15873 free (arg_ary);
15874 parser->non_integral_constant_expression_p = saved_non_ice_p;
15875 parser->integral_constant_expression_p = saved_ice_p;
15876 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15877 if (CHECKING_P)
15878 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15879 return vec;
15882 /* Parse a template-argument.
15884 template-argument:
15885 assignment-expression
15886 type-id
15887 id-expression
15889 The representation is that of an assignment-expression, type-id, or
15890 id-expression -- except that the qualified id-expression is
15891 evaluated, so that the value returned is either a DECL or an
15892 OVERLOAD.
15894 Although the standard says "assignment-expression", it forbids
15895 throw-expressions or assignments in the template argument.
15896 Therefore, we use "conditional-expression" instead. */
15898 static tree
15899 cp_parser_template_argument (cp_parser* parser)
15901 tree argument;
15902 bool template_p;
15903 bool address_p;
15904 bool maybe_type_id = false;
15905 cp_token *token = NULL, *argument_start_token = NULL;
15906 location_t loc = 0;
15907 cp_id_kind idk;
15909 /* There's really no way to know what we're looking at, so we just
15910 try each alternative in order.
15912 [temp.arg]
15914 In a template-argument, an ambiguity between a type-id and an
15915 expression is resolved to a type-id, regardless of the form of
15916 the corresponding template-parameter.
15918 Therefore, we try a type-id first. */
15919 cp_parser_parse_tentatively (parser);
15920 argument = cp_parser_template_type_arg (parser);
15921 /* If there was no error parsing the type-id but the next token is a
15922 '>>', our behavior depends on which dialect of C++ we're
15923 parsing. In C++98, we probably found a typo for '> >'. But there
15924 are type-id which are also valid expressions. For instance:
15926 struct X { int operator >> (int); };
15927 template <int V> struct Foo {};
15928 Foo<X () >> 5> r;
15930 Here 'X()' is a valid type-id of a function type, but the user just
15931 wanted to write the expression "X() >> 5". Thus, we remember that we
15932 found a valid type-id, but we still try to parse the argument as an
15933 expression to see what happens.
15935 In C++0x, the '>>' will be considered two separate '>'
15936 tokens. */
15937 if (!cp_parser_error_occurred (parser)
15938 && cxx_dialect == cxx98
15939 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15941 maybe_type_id = true;
15942 cp_parser_abort_tentative_parse (parser);
15944 else
15946 /* If the next token isn't a `,' or a `>', then this argument wasn't
15947 really finished. This means that the argument is not a valid
15948 type-id. */
15949 if (!cp_parser_next_token_ends_template_argument_p (parser))
15950 cp_parser_error (parser, "expected template-argument");
15951 /* If that worked, we're done. */
15952 if (cp_parser_parse_definitely (parser))
15953 return argument;
15955 /* We're still not sure what the argument will be. */
15956 cp_parser_parse_tentatively (parser);
15957 /* Try a template. */
15958 argument_start_token = cp_lexer_peek_token (parser->lexer);
15959 argument = cp_parser_id_expression (parser,
15960 /*template_keyword_p=*/false,
15961 /*check_dependency_p=*/true,
15962 &template_p,
15963 /*declarator_p=*/false,
15964 /*optional_p=*/false);
15965 /* If the next token isn't a `,' or a `>', then this argument wasn't
15966 really finished. */
15967 if (!cp_parser_next_token_ends_template_argument_p (parser))
15968 cp_parser_error (parser, "expected template-argument");
15969 if (!cp_parser_error_occurred (parser))
15971 /* Figure out what is being referred to. If the id-expression
15972 was for a class template specialization, then we will have a
15973 TYPE_DECL at this point. There is no need to do name lookup
15974 at this point in that case. */
15975 if (TREE_CODE (argument) != TYPE_DECL)
15976 argument = cp_parser_lookup_name (parser, argument,
15977 none_type,
15978 /*is_template=*/template_p,
15979 /*is_namespace=*/false,
15980 /*check_dependency=*/true,
15981 /*ambiguous_decls=*/NULL,
15982 argument_start_token->location);
15983 /* Handle a constrained-type-specifier for a non-type template
15984 parameter. */
15985 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15986 argument = decl;
15987 else if (TREE_CODE (argument) != TEMPLATE_DECL
15988 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15989 cp_parser_error (parser, "expected template-name");
15991 if (cp_parser_parse_definitely (parser))
15993 if (TREE_DEPRECATED (argument))
15994 warn_deprecated_use (argument, NULL_TREE);
15995 return argument;
15997 /* It must be a non-type argument. In C++17 any constant-expression is
15998 allowed. */
15999 if (cxx_dialect > cxx14)
16000 goto general_expr;
16002 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16004 -- an integral constant-expression of integral or enumeration
16005 type; or
16007 -- the name of a non-type template-parameter; or
16009 -- the name of an object or function with external linkage...
16011 -- the address of an object or function with external linkage...
16013 -- a pointer to member... */
16014 /* Look for a non-type template parameter. */
16015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16017 cp_parser_parse_tentatively (parser);
16018 argument = cp_parser_primary_expression (parser,
16019 /*address_p=*/false,
16020 /*cast_p=*/false,
16021 /*template_arg_p=*/true,
16022 &idk);
16023 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16024 || !cp_parser_next_token_ends_template_argument_p (parser))
16025 cp_parser_simulate_error (parser);
16026 if (cp_parser_parse_definitely (parser))
16027 return argument;
16030 /* If the next token is "&", the argument must be the address of an
16031 object or function with external linkage. */
16032 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16033 if (address_p)
16035 loc = cp_lexer_peek_token (parser->lexer)->location;
16036 cp_lexer_consume_token (parser->lexer);
16038 /* See if we might have an id-expression. */
16039 token = cp_lexer_peek_token (parser->lexer);
16040 if (token->type == CPP_NAME
16041 || token->keyword == RID_OPERATOR
16042 || token->type == CPP_SCOPE
16043 || token->type == CPP_TEMPLATE_ID
16044 || token->type == CPP_NESTED_NAME_SPECIFIER)
16046 cp_parser_parse_tentatively (parser);
16047 argument = cp_parser_primary_expression (parser,
16048 address_p,
16049 /*cast_p=*/false,
16050 /*template_arg_p=*/true,
16051 &idk);
16052 if (cp_parser_error_occurred (parser)
16053 || !cp_parser_next_token_ends_template_argument_p (parser))
16054 cp_parser_abort_tentative_parse (parser);
16055 else
16057 tree probe;
16059 if (INDIRECT_REF_P (argument))
16061 /* Strip the dereference temporarily. */
16062 gcc_assert (REFERENCE_REF_P (argument));
16063 argument = TREE_OPERAND (argument, 0);
16066 /* If we're in a template, we represent a qualified-id referring
16067 to a static data member as a SCOPE_REF even if the scope isn't
16068 dependent so that we can check access control later. */
16069 probe = argument;
16070 if (TREE_CODE (probe) == SCOPE_REF)
16071 probe = TREE_OPERAND (probe, 1);
16072 if (VAR_P (probe))
16074 /* A variable without external linkage might still be a
16075 valid constant-expression, so no error is issued here
16076 if the external-linkage check fails. */
16077 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16078 cp_parser_simulate_error (parser);
16080 else if (is_overloaded_fn (argument))
16081 /* All overloaded functions are allowed; if the external
16082 linkage test does not pass, an error will be issued
16083 later. */
16085 else if (address_p
16086 && (TREE_CODE (argument) == OFFSET_REF
16087 || TREE_CODE (argument) == SCOPE_REF))
16088 /* A pointer-to-member. */
16090 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16092 else
16093 cp_parser_simulate_error (parser);
16095 if (cp_parser_parse_definitely (parser))
16097 if (address_p)
16098 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16099 tf_warning_or_error);
16100 else
16101 argument = convert_from_reference (argument);
16102 return argument;
16106 /* If the argument started with "&", there are no other valid
16107 alternatives at this point. */
16108 if (address_p)
16110 cp_parser_error (parser, "invalid non-type template argument");
16111 return error_mark_node;
16114 general_expr:
16115 /* If the argument wasn't successfully parsed as a type-id followed
16116 by '>>', the argument can only be a constant expression now.
16117 Otherwise, we try parsing the constant-expression tentatively,
16118 because the argument could really be a type-id. */
16119 if (maybe_type_id)
16120 cp_parser_parse_tentatively (parser);
16122 if (cxx_dialect <= cxx14)
16123 argument = cp_parser_constant_expression (parser);
16124 else
16126 /* With C++17 generalized non-type template arguments we need to handle
16127 lvalue constant expressions, too. */
16128 argument = cp_parser_assignment_expression (parser);
16129 require_potential_constant_expression (argument);
16132 if (!maybe_type_id)
16133 return argument;
16134 if (!cp_parser_next_token_ends_template_argument_p (parser))
16135 cp_parser_error (parser, "expected template-argument");
16136 if (cp_parser_parse_definitely (parser))
16137 return argument;
16138 /* We did our best to parse the argument as a non type-id, but that
16139 was the only alternative that matched (albeit with a '>' after
16140 it). We can assume it's just a typo from the user, and a
16141 diagnostic will then be issued. */
16142 return cp_parser_template_type_arg (parser);
16145 /* Parse an explicit-instantiation.
16147 explicit-instantiation:
16148 template declaration
16150 Although the standard says `declaration', what it really means is:
16152 explicit-instantiation:
16153 template decl-specifier-seq [opt] declarator [opt] ;
16155 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16156 supposed to be allowed. A defect report has been filed about this
16157 issue.
16159 GNU Extension:
16161 explicit-instantiation:
16162 storage-class-specifier template
16163 decl-specifier-seq [opt] declarator [opt] ;
16164 function-specifier template
16165 decl-specifier-seq [opt] declarator [opt] ; */
16167 static void
16168 cp_parser_explicit_instantiation (cp_parser* parser)
16170 int declares_class_or_enum;
16171 cp_decl_specifier_seq decl_specifiers;
16172 tree extension_specifier = NULL_TREE;
16174 timevar_push (TV_TEMPLATE_INST);
16176 /* Look for an (optional) storage-class-specifier or
16177 function-specifier. */
16178 if (cp_parser_allow_gnu_extensions_p (parser))
16180 extension_specifier
16181 = cp_parser_storage_class_specifier_opt (parser);
16182 if (!extension_specifier)
16183 extension_specifier
16184 = cp_parser_function_specifier_opt (parser,
16185 /*decl_specs=*/NULL);
16188 /* Look for the `template' keyword. */
16189 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16190 /* Let the front end know that we are processing an explicit
16191 instantiation. */
16192 begin_explicit_instantiation ();
16193 /* [temp.explicit] says that we are supposed to ignore access
16194 control while processing explicit instantiation directives. */
16195 push_deferring_access_checks (dk_no_check);
16196 /* Parse a decl-specifier-seq. */
16197 cp_parser_decl_specifier_seq (parser,
16198 CP_PARSER_FLAGS_OPTIONAL,
16199 &decl_specifiers,
16200 &declares_class_or_enum);
16201 /* If there was exactly one decl-specifier, and it declared a class,
16202 and there's no declarator, then we have an explicit type
16203 instantiation. */
16204 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16206 tree type;
16208 type = check_tag_decl (&decl_specifiers,
16209 /*explicit_type_instantiation_p=*/true);
16210 /* Turn access control back on for names used during
16211 template instantiation. */
16212 pop_deferring_access_checks ();
16213 if (type)
16214 do_type_instantiation (type, extension_specifier,
16215 /*complain=*/tf_error);
16217 else
16219 cp_declarator *declarator;
16220 tree decl;
16222 /* Parse the declarator. */
16223 declarator
16224 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16225 /*ctor_dtor_or_conv_p=*/NULL,
16226 /*parenthesized_p=*/NULL,
16227 /*member_p=*/false,
16228 /*friend_p=*/false);
16229 if (declares_class_or_enum & 2)
16230 cp_parser_check_for_definition_in_return_type (declarator,
16231 decl_specifiers.type,
16232 decl_specifiers.locations[ds_type_spec]);
16233 if (declarator != cp_error_declarator)
16235 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16236 permerror (decl_specifiers.locations[ds_inline],
16237 "explicit instantiation shall not use"
16238 " %<inline%> specifier");
16239 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16240 permerror (decl_specifiers.locations[ds_constexpr],
16241 "explicit instantiation shall not use"
16242 " %<constexpr%> specifier");
16244 decl = grokdeclarator (declarator, &decl_specifiers,
16245 NORMAL, 0, &decl_specifiers.attributes);
16246 /* Turn access control back on for names used during
16247 template instantiation. */
16248 pop_deferring_access_checks ();
16249 /* Do the explicit instantiation. */
16250 do_decl_instantiation (decl, extension_specifier);
16252 else
16254 pop_deferring_access_checks ();
16255 /* Skip the body of the explicit instantiation. */
16256 cp_parser_skip_to_end_of_statement (parser);
16259 /* We're done with the instantiation. */
16260 end_explicit_instantiation ();
16262 cp_parser_consume_semicolon_at_end_of_statement (parser);
16264 timevar_pop (TV_TEMPLATE_INST);
16267 /* Parse an explicit-specialization.
16269 explicit-specialization:
16270 template < > declaration
16272 Although the standard says `declaration', what it really means is:
16274 explicit-specialization:
16275 template <> decl-specifier [opt] init-declarator [opt] ;
16276 template <> function-definition
16277 template <> explicit-specialization
16278 template <> template-declaration */
16280 static void
16281 cp_parser_explicit_specialization (cp_parser* parser)
16283 bool need_lang_pop;
16284 cp_token *token = cp_lexer_peek_token (parser->lexer);
16286 /* Look for the `template' keyword. */
16287 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16288 /* Look for the `<'. */
16289 cp_parser_require (parser, CPP_LESS, RT_LESS);
16290 /* Look for the `>'. */
16291 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16292 /* We have processed another parameter list. */
16293 ++parser->num_template_parameter_lists;
16294 /* [temp]
16296 A template ... explicit specialization ... shall not have C
16297 linkage. */
16298 if (current_lang_name == lang_name_c)
16300 error_at (token->location, "template specialization with C linkage");
16301 /* Give it C++ linkage to avoid confusing other parts of the
16302 front end. */
16303 push_lang_context (lang_name_cplusplus);
16304 need_lang_pop = true;
16306 else
16307 need_lang_pop = false;
16308 /* Let the front end know that we are beginning a specialization. */
16309 if (!begin_specialization ())
16311 end_specialization ();
16312 return;
16315 /* If the next keyword is `template', we need to figure out whether
16316 or not we're looking a template-declaration. */
16317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16319 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16320 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16321 cp_parser_template_declaration_after_export (parser,
16322 /*member_p=*/false);
16323 else
16324 cp_parser_explicit_specialization (parser);
16326 else
16327 /* Parse the dependent declaration. */
16328 cp_parser_single_declaration (parser,
16329 /*checks=*/NULL,
16330 /*member_p=*/false,
16331 /*explicit_specialization_p=*/true,
16332 /*friend_p=*/NULL);
16333 /* We're done with the specialization. */
16334 end_specialization ();
16335 /* For the erroneous case of a template with C linkage, we pushed an
16336 implicit C++ linkage scope; exit that scope now. */
16337 if (need_lang_pop)
16338 pop_lang_context ();
16339 /* We're done with this parameter list. */
16340 --parser->num_template_parameter_lists;
16343 /* Parse a type-specifier.
16345 type-specifier:
16346 simple-type-specifier
16347 class-specifier
16348 enum-specifier
16349 elaborated-type-specifier
16350 cv-qualifier
16352 GNU Extension:
16354 type-specifier:
16355 __complex__
16357 Returns a representation of the type-specifier. For a
16358 class-specifier, enum-specifier, or elaborated-type-specifier, a
16359 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16361 The parser flags FLAGS is used to control type-specifier parsing.
16363 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16364 in a decl-specifier-seq.
16366 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16367 class-specifier, enum-specifier, or elaborated-type-specifier, then
16368 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16369 if a type is declared; 2 if it is defined. Otherwise, it is set to
16370 zero.
16372 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16373 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16374 is set to FALSE. */
16376 static tree
16377 cp_parser_type_specifier (cp_parser* parser,
16378 cp_parser_flags flags,
16379 cp_decl_specifier_seq *decl_specs,
16380 bool is_declaration,
16381 int* declares_class_or_enum,
16382 bool* is_cv_qualifier)
16384 tree type_spec = NULL_TREE;
16385 cp_token *token;
16386 enum rid keyword;
16387 cp_decl_spec ds = ds_last;
16389 /* Assume this type-specifier does not declare a new type. */
16390 if (declares_class_or_enum)
16391 *declares_class_or_enum = 0;
16392 /* And that it does not specify a cv-qualifier. */
16393 if (is_cv_qualifier)
16394 *is_cv_qualifier = false;
16395 /* Peek at the next token. */
16396 token = cp_lexer_peek_token (parser->lexer);
16398 /* If we're looking at a keyword, we can use that to guide the
16399 production we choose. */
16400 keyword = token->keyword;
16401 switch (keyword)
16403 case RID_ENUM:
16404 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16405 goto elaborated_type_specifier;
16407 /* Look for the enum-specifier. */
16408 type_spec = cp_parser_enum_specifier (parser);
16409 /* If that worked, we're done. */
16410 if (type_spec)
16412 if (declares_class_or_enum)
16413 *declares_class_or_enum = 2;
16414 if (decl_specs)
16415 cp_parser_set_decl_spec_type (decl_specs,
16416 type_spec,
16417 token,
16418 /*type_definition_p=*/true);
16419 return type_spec;
16421 else
16422 goto elaborated_type_specifier;
16424 /* Any of these indicate either a class-specifier, or an
16425 elaborated-type-specifier. */
16426 case RID_CLASS:
16427 case RID_STRUCT:
16428 case RID_UNION:
16429 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16430 goto elaborated_type_specifier;
16432 /* Parse tentatively so that we can back up if we don't find a
16433 class-specifier. */
16434 cp_parser_parse_tentatively (parser);
16435 /* Look for the class-specifier. */
16436 type_spec = cp_parser_class_specifier (parser);
16437 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16438 /* If that worked, we're done. */
16439 if (cp_parser_parse_definitely (parser))
16441 if (declares_class_or_enum)
16442 *declares_class_or_enum = 2;
16443 if (decl_specs)
16444 cp_parser_set_decl_spec_type (decl_specs,
16445 type_spec,
16446 token,
16447 /*type_definition_p=*/true);
16448 return type_spec;
16451 /* Fall through. */
16452 elaborated_type_specifier:
16453 /* We're declaring (not defining) a class or enum. */
16454 if (declares_class_or_enum)
16455 *declares_class_or_enum = 1;
16457 /* Fall through. */
16458 case RID_TYPENAME:
16459 /* Look for an elaborated-type-specifier. */
16460 type_spec
16461 = (cp_parser_elaborated_type_specifier
16462 (parser,
16463 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16464 is_declaration));
16465 if (decl_specs)
16466 cp_parser_set_decl_spec_type (decl_specs,
16467 type_spec,
16468 token,
16469 /*type_definition_p=*/false);
16470 return type_spec;
16472 case RID_CONST:
16473 ds = ds_const;
16474 if (is_cv_qualifier)
16475 *is_cv_qualifier = true;
16476 break;
16478 case RID_VOLATILE:
16479 ds = ds_volatile;
16480 if (is_cv_qualifier)
16481 *is_cv_qualifier = true;
16482 break;
16484 case RID_RESTRICT:
16485 ds = ds_restrict;
16486 if (is_cv_qualifier)
16487 *is_cv_qualifier = true;
16488 break;
16490 case RID_COMPLEX:
16491 /* The `__complex__' keyword is a GNU extension. */
16492 ds = ds_complex;
16493 break;
16495 default:
16496 break;
16499 /* Handle simple keywords. */
16500 if (ds != ds_last)
16502 if (decl_specs)
16504 set_and_check_decl_spec_loc (decl_specs, ds, token);
16505 decl_specs->any_specifiers_p = true;
16507 return cp_lexer_consume_token (parser->lexer)->u.value;
16510 /* If we do not already have a type-specifier, assume we are looking
16511 at a simple-type-specifier. */
16512 type_spec = cp_parser_simple_type_specifier (parser,
16513 decl_specs,
16514 flags);
16516 /* If we didn't find a type-specifier, and a type-specifier was not
16517 optional in this context, issue an error message. */
16518 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16520 cp_parser_error (parser, "expected type specifier");
16521 return error_mark_node;
16524 return type_spec;
16527 /* Parse a simple-type-specifier.
16529 simple-type-specifier:
16530 :: [opt] nested-name-specifier [opt] type-name
16531 :: [opt] nested-name-specifier template template-id
16532 char
16533 wchar_t
16534 bool
16535 short
16537 long
16538 signed
16539 unsigned
16540 float
16541 double
16542 void
16544 C++11 Extension:
16546 simple-type-specifier:
16547 auto
16548 decltype ( expression )
16549 char16_t
16550 char32_t
16551 __underlying_type ( type-id )
16553 C++17 extension:
16555 nested-name-specifier(opt) template-name
16557 GNU Extension:
16559 simple-type-specifier:
16560 __int128
16561 __typeof__ unary-expression
16562 __typeof__ ( type-id )
16563 __typeof__ ( type-id ) { initializer-list , [opt] }
16565 Concepts Extension:
16567 simple-type-specifier:
16568 constrained-type-specifier
16570 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16571 appropriately updated. */
16573 static tree
16574 cp_parser_simple_type_specifier (cp_parser* parser,
16575 cp_decl_specifier_seq *decl_specs,
16576 cp_parser_flags flags)
16578 tree type = NULL_TREE;
16579 cp_token *token;
16580 int idx;
16582 /* Peek at the next token. */
16583 token = cp_lexer_peek_token (parser->lexer);
16585 /* If we're looking at a keyword, things are easy. */
16586 switch (token->keyword)
16588 case RID_CHAR:
16589 if (decl_specs)
16590 decl_specs->explicit_char_p = true;
16591 type = char_type_node;
16592 break;
16593 case RID_CHAR16:
16594 type = char16_type_node;
16595 break;
16596 case RID_CHAR32:
16597 type = char32_type_node;
16598 break;
16599 case RID_WCHAR:
16600 type = wchar_type_node;
16601 break;
16602 case RID_BOOL:
16603 type = boolean_type_node;
16604 break;
16605 case RID_SHORT:
16606 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16607 type = short_integer_type_node;
16608 break;
16609 case RID_INT:
16610 if (decl_specs)
16611 decl_specs->explicit_int_p = true;
16612 type = integer_type_node;
16613 break;
16614 case RID_INT_N_0:
16615 case RID_INT_N_1:
16616 case RID_INT_N_2:
16617 case RID_INT_N_3:
16618 idx = token->keyword - RID_INT_N_0;
16619 if (! int_n_enabled_p [idx])
16620 break;
16621 if (decl_specs)
16623 decl_specs->explicit_intN_p = true;
16624 decl_specs->int_n_idx = idx;
16626 type = int_n_trees [idx].signed_type;
16627 break;
16628 case RID_LONG:
16629 if (decl_specs)
16630 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16631 type = long_integer_type_node;
16632 break;
16633 case RID_SIGNED:
16634 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16635 type = integer_type_node;
16636 break;
16637 case RID_UNSIGNED:
16638 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16639 type = unsigned_type_node;
16640 break;
16641 case RID_FLOAT:
16642 type = float_type_node;
16643 break;
16644 case RID_DOUBLE:
16645 type = double_type_node;
16646 break;
16647 case RID_VOID:
16648 type = void_type_node;
16649 break;
16651 case RID_AUTO:
16652 maybe_warn_cpp0x (CPP0X_AUTO);
16653 if (parser->auto_is_implicit_function_template_parm_p)
16655 /* The 'auto' might be the placeholder return type for a function decl
16656 with trailing return type. */
16657 bool have_trailing_return_fn_decl = false;
16659 cp_parser_parse_tentatively (parser);
16660 cp_lexer_consume_token (parser->lexer);
16661 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16662 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16663 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16664 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16666 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16668 cp_lexer_consume_token (parser->lexer);
16669 cp_parser_skip_to_closing_parenthesis (parser,
16670 /*recovering*/false,
16671 /*or_comma*/false,
16672 /*consume_paren*/true);
16673 continue;
16676 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16678 have_trailing_return_fn_decl = true;
16679 break;
16682 cp_lexer_consume_token (parser->lexer);
16684 cp_parser_abort_tentative_parse (parser);
16686 if (have_trailing_return_fn_decl)
16688 type = make_auto ();
16689 break;
16692 if (cxx_dialect >= cxx14)
16694 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16695 type = TREE_TYPE (type);
16697 else
16698 type = error_mark_node;
16700 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16702 if (cxx_dialect < cxx14)
16703 error_at (token->location,
16704 "use of %<auto%> in lambda parameter declaration "
16705 "only available with "
16706 "-std=c++14 or -std=gnu++14");
16708 else if (cxx_dialect < cxx14)
16709 error_at (token->location,
16710 "use of %<auto%> in parameter declaration "
16711 "only available with "
16712 "-std=c++14 or -std=gnu++14");
16713 else if (!flag_concepts)
16714 pedwarn (token->location, OPT_Wpedantic,
16715 "ISO C++ forbids use of %<auto%> in parameter "
16716 "declaration");
16718 else
16719 type = make_auto ();
16720 break;
16722 case RID_DECLTYPE:
16723 /* Since DR 743, decltype can either be a simple-type-specifier by
16724 itself or begin a nested-name-specifier. Parsing it will replace
16725 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16726 handling below decide what to do. */
16727 cp_parser_decltype (parser);
16728 cp_lexer_set_token_position (parser->lexer, token);
16729 break;
16731 case RID_TYPEOF:
16732 /* Consume the `typeof' token. */
16733 cp_lexer_consume_token (parser->lexer);
16734 /* Parse the operand to `typeof'. */
16735 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16736 /* If it is not already a TYPE, take its type. */
16737 if (!TYPE_P (type))
16738 type = finish_typeof (type);
16740 if (decl_specs)
16741 cp_parser_set_decl_spec_type (decl_specs, type,
16742 token,
16743 /*type_definition_p=*/false);
16745 return type;
16747 case RID_UNDERLYING_TYPE:
16748 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16749 if (decl_specs)
16750 cp_parser_set_decl_spec_type (decl_specs, type,
16751 token,
16752 /*type_definition_p=*/false);
16754 return type;
16756 case RID_BASES:
16757 case RID_DIRECT_BASES:
16758 type = cp_parser_trait_expr (parser, token->keyword);
16759 if (decl_specs)
16760 cp_parser_set_decl_spec_type (decl_specs, type,
16761 token,
16762 /*type_definition_p=*/false);
16763 return type;
16764 default:
16765 break;
16768 /* If token is an already-parsed decltype not followed by ::,
16769 it's a simple-type-specifier. */
16770 if (token->type == CPP_DECLTYPE
16771 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16773 type = saved_checks_value (token->u.tree_check_value);
16774 if (decl_specs)
16776 cp_parser_set_decl_spec_type (decl_specs, type,
16777 token,
16778 /*type_definition_p=*/false);
16779 /* Remember that we are handling a decltype in order to
16780 implement the resolution of DR 1510 when the argument
16781 isn't instantiation dependent. */
16782 decl_specs->decltype_p = true;
16784 cp_lexer_consume_token (parser->lexer);
16785 return type;
16788 /* If the type-specifier was for a built-in type, we're done. */
16789 if (type)
16791 /* Record the type. */
16792 if (decl_specs
16793 && (token->keyword != RID_SIGNED
16794 && token->keyword != RID_UNSIGNED
16795 && token->keyword != RID_SHORT
16796 && token->keyword != RID_LONG))
16797 cp_parser_set_decl_spec_type (decl_specs,
16798 type,
16799 token,
16800 /*type_definition_p=*/false);
16801 if (decl_specs)
16802 decl_specs->any_specifiers_p = true;
16804 /* Consume the token. */
16805 cp_lexer_consume_token (parser->lexer);
16807 if (type == error_mark_node)
16808 return error_mark_node;
16810 /* There is no valid C++ program where a non-template type is
16811 followed by a "<". That usually indicates that the user thought
16812 that the type was a template. */
16813 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16814 token->location);
16816 return TYPE_NAME (type);
16819 /* The type-specifier must be a user-defined type. */
16820 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16822 bool qualified_p;
16823 bool global_p;
16825 /* Don't gobble tokens or issue error messages if this is an
16826 optional type-specifier. */
16827 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16828 cp_parser_parse_tentatively (parser);
16830 token = cp_lexer_peek_token (parser->lexer);
16832 /* Look for the optional `::' operator. */
16833 global_p
16834 = (cp_parser_global_scope_opt (parser,
16835 /*current_scope_valid_p=*/false)
16836 != NULL_TREE);
16837 /* Look for the nested-name specifier. */
16838 qualified_p
16839 = (cp_parser_nested_name_specifier_opt (parser,
16840 /*typename_keyword_p=*/false,
16841 /*check_dependency_p=*/true,
16842 /*type_p=*/false,
16843 /*is_declaration=*/false)
16844 != NULL_TREE);
16845 /* If we have seen a nested-name-specifier, and the next token
16846 is `template', then we are using the template-id production. */
16847 if (parser->scope
16848 && cp_parser_optional_template_keyword (parser))
16850 /* Look for the template-id. */
16851 type = cp_parser_template_id (parser,
16852 /*template_keyword_p=*/true,
16853 /*check_dependency_p=*/true,
16854 none_type,
16855 /*is_declaration=*/false);
16856 /* If the template-id did not name a type, we are out of
16857 luck. */
16858 if (TREE_CODE (type) != TYPE_DECL)
16860 cp_parser_error (parser, "expected template-id for type");
16861 type = NULL_TREE;
16864 /* Otherwise, look for a type-name. */
16865 else
16866 type = cp_parser_type_name (parser);
16867 /* Keep track of all name-lookups performed in class scopes. */
16868 if (type
16869 && !global_p
16870 && !qualified_p
16871 && TREE_CODE (type) == TYPE_DECL
16872 && identifier_p (DECL_NAME (type)))
16873 maybe_note_name_used_in_class (DECL_NAME (type), type);
16874 /* If it didn't work out, we don't have a TYPE. */
16875 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16876 && !cp_parser_parse_definitely (parser))
16877 type = NULL_TREE;
16878 if (!type && cxx_dialect >= cxx1z)
16880 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16881 cp_parser_parse_tentatively (parser);
16883 cp_parser_global_scope_opt (parser,
16884 /*current_scope_valid_p=*/false);
16885 cp_parser_nested_name_specifier_opt (parser,
16886 /*typename_keyword_p=*/false,
16887 /*check_dependency_p=*/true,
16888 /*type_p=*/false,
16889 /*is_declaration=*/false);
16890 tree name = cp_parser_identifier (parser);
16891 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16892 && parser->scope != error_mark_node)
16894 tree tmpl = cp_parser_lookup_name (parser, name,
16895 none_type,
16896 /*is_template=*/false,
16897 /*is_namespace=*/false,
16898 /*check_dependency=*/true,
16899 /*ambiguous_decls=*/NULL,
16900 token->location);
16901 if (tmpl && tmpl != error_mark_node
16902 && (DECL_CLASS_TEMPLATE_P (tmpl)
16903 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16904 type = make_template_placeholder (tmpl);
16905 else
16907 type = error_mark_node;
16908 if (!cp_parser_simulate_error (parser))
16909 cp_parser_name_lookup_error (parser, name, tmpl,
16910 NLE_TYPE, token->location);
16913 else
16914 type = error_mark_node;
16916 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16917 && !cp_parser_parse_definitely (parser))
16918 type = NULL_TREE;
16920 if (type && decl_specs)
16921 cp_parser_set_decl_spec_type (decl_specs, type,
16922 token,
16923 /*type_definition_p=*/false);
16926 /* If we didn't get a type-name, issue an error message. */
16927 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16929 cp_parser_error (parser, "expected type-name");
16930 return error_mark_node;
16933 if (type && type != error_mark_node)
16935 /* See if TYPE is an Objective-C type, and if so, parse and
16936 accept any protocol references following it. Do this before
16937 the cp_parser_check_for_invalid_template_id() call, because
16938 Objective-C types can be followed by '<...>' which would
16939 enclose protocol names rather than template arguments, and so
16940 everything is fine. */
16941 if (c_dialect_objc () && !parser->scope
16942 && (objc_is_id (type) || objc_is_class_name (type)))
16944 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16945 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16947 /* Clobber the "unqualified" type previously entered into
16948 DECL_SPECS with the new, improved protocol-qualified version. */
16949 if (decl_specs)
16950 decl_specs->type = qual_type;
16952 return qual_type;
16955 /* There is no valid C++ program where a non-template type is
16956 followed by a "<". That usually indicates that the user
16957 thought that the type was a template. */
16958 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16959 none_type,
16960 token->location);
16963 return type;
16966 /* Parse a type-name.
16968 type-name:
16969 class-name
16970 enum-name
16971 typedef-name
16972 simple-template-id [in c++0x]
16974 enum-name:
16975 identifier
16977 typedef-name:
16978 identifier
16980 Concepts:
16982 type-name:
16983 concept-name
16984 partial-concept-id
16986 concept-name:
16987 identifier
16989 Returns a TYPE_DECL for the type. */
16991 static tree
16992 cp_parser_type_name (cp_parser* parser)
16994 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16997 /* See above. */
16998 static tree
16999 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17001 tree type_decl;
17003 /* We can't know yet whether it is a class-name or not. */
17004 cp_parser_parse_tentatively (parser);
17005 /* Try a class-name. */
17006 type_decl = cp_parser_class_name (parser,
17007 typename_keyword_p,
17008 /*template_keyword_p=*/false,
17009 none_type,
17010 /*check_dependency_p=*/true,
17011 /*class_head_p=*/false,
17012 /*is_declaration=*/false);
17013 /* If it's not a class-name, keep looking. */
17014 if (!cp_parser_parse_definitely (parser))
17016 if (cxx_dialect < cxx11)
17017 /* It must be a typedef-name or an enum-name. */
17018 return cp_parser_nonclass_name (parser);
17020 cp_parser_parse_tentatively (parser);
17021 /* It is either a simple-template-id representing an
17022 instantiation of an alias template... */
17023 type_decl = cp_parser_template_id (parser,
17024 /*template_keyword_p=*/false,
17025 /*check_dependency_p=*/true,
17026 none_type,
17027 /*is_declaration=*/false);
17028 /* Note that this must be an instantiation of an alias template
17029 because [temp.names]/6 says:
17031 A template-id that names an alias template specialization
17032 is a type-name.
17034 Whereas [temp.names]/7 says:
17036 A simple-template-id that names a class template
17037 specialization is a class-name.
17039 With concepts, this could also be a partial-concept-id that
17040 declares a non-type template parameter. */
17041 if (type_decl != NULL_TREE
17042 && TREE_CODE (type_decl) == TYPE_DECL
17043 && TYPE_DECL_ALIAS_P (type_decl))
17044 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17045 else if (is_constrained_parameter (type_decl))
17046 /* Don't do anything. */ ;
17047 else
17048 cp_parser_simulate_error (parser);
17050 if (!cp_parser_parse_definitely (parser))
17051 /* ... Or a typedef-name or an enum-name. */
17052 return cp_parser_nonclass_name (parser);
17055 return type_decl;
17058 /* Check if DECL and ARGS can form a constrained-type-specifier.
17059 If ARGS is non-null, we try to form a concept check of the
17060 form DECL<?, ARGS> where ? is a wildcard that matches any
17061 kind of template argument. If ARGS is NULL, then we try to
17062 form a concept check of the form DECL<?>. */
17064 static tree
17065 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17066 tree decl, tree args)
17068 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17070 /* If we a constrained-type-specifier cannot be deduced. */
17071 if (parser->prevent_constrained_type_specifiers)
17072 return NULL_TREE;
17074 /* A constrained type specifier can only be found in an
17075 overload set or as a reference to a template declaration.
17077 FIXME: This might be masking a bug. It's possible that
17078 that the deduction below is causing template specializations
17079 to be formed with the wildcard as an argument. */
17080 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17081 return NULL_TREE;
17083 /* Try to build a call expression that evaluates the
17084 concept. This can fail if the overload set refers
17085 only to non-templates. */
17086 tree placeholder = build_nt (WILDCARD_DECL);
17087 tree check = build_concept_check (decl, placeholder, args);
17088 if (check == error_mark_node)
17089 return NULL_TREE;
17091 /* Deduce the checked constraint and the prototype parameter.
17093 FIXME: In certain cases, failure to deduce should be a
17094 diagnosable error. */
17095 tree conc;
17096 tree proto;
17097 if (!deduce_constrained_parameter (check, conc, proto))
17098 return NULL_TREE;
17100 /* In template parameter scope, this results in a constrained
17101 parameter. Return a descriptor of that parm. */
17102 if (processing_template_parmlist)
17103 return build_constrained_parameter (conc, proto, args);
17105 /* In a parameter-declaration-clause, constrained-type
17106 specifiers result in invented template parameters. */
17107 if (parser->auto_is_implicit_function_template_parm_p)
17109 tree x = build_constrained_parameter (conc, proto, args);
17110 return synthesize_implicit_template_parm (parser, x);
17112 else
17114 /* Otherwise, we're in a context where the constrained
17115 type name is deduced and the constraint applies
17116 after deduction. */
17117 return make_constrained_auto (conc, args);
17120 return NULL_TREE;
17123 /* If DECL refers to a concept, return a TYPE_DECL representing
17124 the result of using the constrained type specifier in the
17125 current context. DECL refers to a concept if
17127 - it is an overload set containing a function concept taking a single
17128 type argument, or
17130 - it is a variable concept taking a single type argument. */
17132 static tree
17133 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17135 if (flag_concepts
17136 && (TREE_CODE (decl) == OVERLOAD
17137 || BASELINK_P (decl)
17138 || variable_concept_p (decl)))
17139 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17140 else
17141 return NULL_TREE;
17144 /* Check if DECL and ARGS form a partial-concept-id. If so,
17145 assign ID to the resulting constrained placeholder.
17147 Returns true if the partial-concept-id designates a placeholder
17148 and false otherwise. Note that *id is set to NULL_TREE in
17149 this case. */
17151 static tree
17152 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17154 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17157 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17158 or a concept-name.
17160 enum-name:
17161 identifier
17163 typedef-name:
17164 identifier
17166 concept-name:
17167 identifier
17169 Returns a TYPE_DECL for the type. */
17171 static tree
17172 cp_parser_nonclass_name (cp_parser* parser)
17174 tree type_decl;
17175 tree identifier;
17177 cp_token *token = cp_lexer_peek_token (parser->lexer);
17178 identifier = cp_parser_identifier (parser);
17179 if (identifier == error_mark_node)
17180 return error_mark_node;
17182 /* Look up the type-name. */
17183 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17185 type_decl = strip_using_decl (type_decl);
17187 /* If we found an overload set, then it may refer to a concept-name. */
17188 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17189 type_decl = decl;
17191 if (TREE_CODE (type_decl) != TYPE_DECL
17192 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17194 /* See if this is an Objective-C type. */
17195 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17196 tree type = objc_get_protocol_qualified_type (identifier, protos);
17197 if (type)
17198 type_decl = TYPE_NAME (type);
17201 /* Issue an error if we did not find a type-name. */
17202 if (TREE_CODE (type_decl) != TYPE_DECL
17203 /* In Objective-C, we have the complication that class names are
17204 normally type names and start declarations (eg, the
17205 "NSObject" in "NSObject *object;"), but can be used in an
17206 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17207 is an expression. So, a classname followed by a dot is not a
17208 valid type-name. */
17209 || (objc_is_class_name (TREE_TYPE (type_decl))
17210 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17212 if (!cp_parser_simulate_error (parser))
17213 cp_parser_name_lookup_error (parser, identifier, type_decl,
17214 NLE_TYPE, token->location);
17215 return error_mark_node;
17217 /* Remember that the name was used in the definition of the
17218 current class so that we can check later to see if the
17219 meaning would have been different after the class was
17220 entirely defined. */
17221 else if (type_decl != error_mark_node
17222 && !parser->scope)
17223 maybe_note_name_used_in_class (identifier, type_decl);
17225 return type_decl;
17228 /* Parse an elaborated-type-specifier. Note that the grammar given
17229 here incorporates the resolution to DR68.
17231 elaborated-type-specifier:
17232 class-key :: [opt] nested-name-specifier [opt] identifier
17233 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17234 enum-key :: [opt] nested-name-specifier [opt] identifier
17235 typename :: [opt] nested-name-specifier identifier
17236 typename :: [opt] nested-name-specifier template [opt]
17237 template-id
17239 GNU extension:
17241 elaborated-type-specifier:
17242 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17243 class-key attributes :: [opt] nested-name-specifier [opt]
17244 template [opt] template-id
17245 enum attributes :: [opt] nested-name-specifier [opt] identifier
17247 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17248 declared `friend'. If IS_DECLARATION is TRUE, then this
17249 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17250 something is being declared.
17252 Returns the TYPE specified. */
17254 static tree
17255 cp_parser_elaborated_type_specifier (cp_parser* parser,
17256 bool is_friend,
17257 bool is_declaration)
17259 enum tag_types tag_type;
17260 tree identifier;
17261 tree type = NULL_TREE;
17262 tree attributes = NULL_TREE;
17263 tree globalscope;
17264 cp_token *token = NULL;
17266 /* See if we're looking at the `enum' keyword. */
17267 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17269 /* Consume the `enum' token. */
17270 cp_lexer_consume_token (parser->lexer);
17271 /* Remember that it's an enumeration type. */
17272 tag_type = enum_type;
17273 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17274 enums) is used here. */
17275 cp_token *token = cp_lexer_peek_token (parser->lexer);
17276 if (cp_parser_is_keyword (token, RID_CLASS)
17277 || cp_parser_is_keyword (token, RID_STRUCT))
17279 gcc_rich_location richloc (token->location);
17280 richloc.add_range (input_location, false);
17281 richloc.add_fixit_remove ();
17282 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17283 "a scoped enum must not use the %qD keyword",
17284 token->u.value);
17285 /* Consume the `struct' or `class' and parse it anyway. */
17286 cp_lexer_consume_token (parser->lexer);
17288 /* Parse the attributes. */
17289 attributes = cp_parser_attributes_opt (parser);
17291 /* Or, it might be `typename'. */
17292 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17293 RID_TYPENAME))
17295 /* Consume the `typename' token. */
17296 cp_lexer_consume_token (parser->lexer);
17297 /* Remember that it's a `typename' type. */
17298 tag_type = typename_type;
17300 /* Otherwise it must be a class-key. */
17301 else
17303 tag_type = cp_parser_class_key (parser);
17304 if (tag_type == none_type)
17305 return error_mark_node;
17306 /* Parse the attributes. */
17307 attributes = cp_parser_attributes_opt (parser);
17310 /* Look for the `::' operator. */
17311 globalscope = cp_parser_global_scope_opt (parser,
17312 /*current_scope_valid_p=*/false);
17313 /* Look for the nested-name-specifier. */
17314 tree nested_name_specifier;
17315 if (tag_type == typename_type && !globalscope)
17317 nested_name_specifier
17318 = cp_parser_nested_name_specifier (parser,
17319 /*typename_keyword_p=*/true,
17320 /*check_dependency_p=*/true,
17321 /*type_p=*/true,
17322 is_declaration);
17323 if (!nested_name_specifier)
17324 return error_mark_node;
17326 else
17327 /* Even though `typename' is not present, the proposed resolution
17328 to Core Issue 180 says that in `class A<T>::B', `B' should be
17329 considered a type-name, even if `A<T>' is dependent. */
17330 nested_name_specifier
17331 = cp_parser_nested_name_specifier_opt (parser,
17332 /*typename_keyword_p=*/true,
17333 /*check_dependency_p=*/true,
17334 /*type_p=*/true,
17335 is_declaration);
17336 /* For everything but enumeration types, consider a template-id.
17337 For an enumeration type, consider only a plain identifier. */
17338 if (tag_type != enum_type)
17340 bool template_p = false;
17341 tree decl;
17343 /* Allow the `template' keyword. */
17344 template_p = cp_parser_optional_template_keyword (parser);
17345 /* If we didn't see `template', we don't know if there's a
17346 template-id or not. */
17347 if (!template_p)
17348 cp_parser_parse_tentatively (parser);
17349 /* Parse the template-id. */
17350 token = cp_lexer_peek_token (parser->lexer);
17351 decl = cp_parser_template_id (parser, template_p,
17352 /*check_dependency_p=*/true,
17353 tag_type,
17354 is_declaration);
17355 /* If we didn't find a template-id, look for an ordinary
17356 identifier. */
17357 if (!template_p && !cp_parser_parse_definitely (parser))
17359 /* We can get here when cp_parser_template_id, called by
17360 cp_parser_class_name with tag_type == none_type, succeeds
17361 and caches a BASELINK. Then, when called again here,
17362 instead of failing and returning an error_mark_node
17363 returns it (see template/typename17.C in C++11).
17364 ??? Could we diagnose this earlier? */
17365 else if (tag_type == typename_type && BASELINK_P (decl))
17367 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17368 type = error_mark_node;
17370 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17371 in effect, then we must assume that, upon instantiation, the
17372 template will correspond to a class. */
17373 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17374 && tag_type == typename_type)
17375 type = make_typename_type (parser->scope, decl,
17376 typename_type,
17377 /*complain=*/tf_error);
17378 /* If the `typename' keyword is in effect and DECL is not a type
17379 decl, then type is non existent. */
17380 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17382 else if (TREE_CODE (decl) == TYPE_DECL)
17384 type = check_elaborated_type_specifier (tag_type, decl,
17385 /*allow_template_p=*/true);
17387 /* If the next token is a semicolon, this must be a specialization,
17388 instantiation, or friend declaration. Check the scope while we
17389 still know whether or not we had a nested-name-specifier. */
17390 if (type != error_mark_node
17391 && !nested_name_specifier && !is_friend
17392 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17393 check_unqualified_spec_or_inst (type, token->location);
17395 else if (decl == error_mark_node)
17396 type = error_mark_node;
17399 if (!type)
17401 token = cp_lexer_peek_token (parser->lexer);
17402 identifier = cp_parser_identifier (parser);
17404 if (identifier == error_mark_node)
17406 parser->scope = NULL_TREE;
17407 return error_mark_node;
17410 /* For a `typename', we needn't call xref_tag. */
17411 if (tag_type == typename_type
17412 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17413 return cp_parser_make_typename_type (parser, identifier,
17414 token->location);
17416 /* Template parameter lists apply only if we are not within a
17417 function parameter list. */
17418 bool template_parm_lists_apply
17419 = parser->num_template_parameter_lists;
17420 if (template_parm_lists_apply)
17421 for (cp_binding_level *s = current_binding_level;
17422 s && s->kind != sk_template_parms;
17423 s = s->level_chain)
17424 if (s->kind == sk_function_parms)
17425 template_parm_lists_apply = false;
17427 /* Look up a qualified name in the usual way. */
17428 if (parser->scope)
17430 tree decl;
17431 tree ambiguous_decls;
17433 decl = cp_parser_lookup_name (parser, identifier,
17434 tag_type,
17435 /*is_template=*/false,
17436 /*is_namespace=*/false,
17437 /*check_dependency=*/true,
17438 &ambiguous_decls,
17439 token->location);
17441 /* If the lookup was ambiguous, an error will already have been
17442 issued. */
17443 if (ambiguous_decls)
17444 return error_mark_node;
17446 /* If we are parsing friend declaration, DECL may be a
17447 TEMPLATE_DECL tree node here. However, we need to check
17448 whether this TEMPLATE_DECL results in valid code. Consider
17449 the following example:
17451 namespace N {
17452 template <class T> class C {};
17454 class X {
17455 template <class T> friend class N::C; // #1, valid code
17457 template <class T> class Y {
17458 friend class N::C; // #2, invalid code
17461 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17462 name lookup of `N::C'. We see that friend declaration must
17463 be template for the code to be valid. Note that
17464 processing_template_decl does not work here since it is
17465 always 1 for the above two cases. */
17467 decl = (cp_parser_maybe_treat_template_as_class
17468 (decl, /*tag_name_p=*/is_friend
17469 && template_parm_lists_apply));
17471 if (TREE_CODE (decl) != TYPE_DECL)
17473 cp_parser_diagnose_invalid_type_name (parser,
17474 identifier,
17475 token->location);
17476 return error_mark_node;
17479 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17481 bool allow_template = (template_parm_lists_apply
17482 || DECL_SELF_REFERENCE_P (decl));
17483 type = check_elaborated_type_specifier (tag_type, decl,
17484 allow_template);
17486 if (type == error_mark_node)
17487 return error_mark_node;
17490 /* Forward declarations of nested types, such as
17492 class C1::C2;
17493 class C1::C2::C3;
17495 are invalid unless all components preceding the final '::'
17496 are complete. If all enclosing types are complete, these
17497 declarations become merely pointless.
17499 Invalid forward declarations of nested types are errors
17500 caught elsewhere in parsing. Those that are pointless arrive
17501 here. */
17503 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17504 && !is_friend && !processing_explicit_instantiation)
17505 warning (0, "declaration %qD does not declare anything", decl);
17507 type = TREE_TYPE (decl);
17509 else
17511 /* An elaborated-type-specifier sometimes introduces a new type and
17512 sometimes names an existing type. Normally, the rule is that it
17513 introduces a new type only if there is not an existing type of
17514 the same name already in scope. For example, given:
17516 struct S {};
17517 void f() { struct S s; }
17519 the `struct S' in the body of `f' is the same `struct S' as in
17520 the global scope; the existing definition is used. However, if
17521 there were no global declaration, this would introduce a new
17522 local class named `S'.
17524 An exception to this rule applies to the following code:
17526 namespace N { struct S; }
17528 Here, the elaborated-type-specifier names a new type
17529 unconditionally; even if there is already an `S' in the
17530 containing scope this declaration names a new type.
17531 This exception only applies if the elaborated-type-specifier
17532 forms the complete declaration:
17534 [class.name]
17536 A declaration consisting solely of `class-key identifier ;' is
17537 either a redeclaration of the name in the current scope or a
17538 forward declaration of the identifier as a class name. It
17539 introduces the name into the current scope.
17541 We are in this situation precisely when the next token is a `;'.
17543 An exception to the exception is that a `friend' declaration does
17544 *not* name a new type; i.e., given:
17546 struct S { friend struct T; };
17548 `T' is not a new type in the scope of `S'.
17550 Also, `new struct S' or `sizeof (struct S)' never results in the
17551 definition of a new type; a new type can only be declared in a
17552 declaration context. */
17554 tag_scope ts;
17555 bool template_p;
17557 if (is_friend)
17558 /* Friends have special name lookup rules. */
17559 ts = ts_within_enclosing_non_class;
17560 else if (is_declaration
17561 && cp_lexer_next_token_is (parser->lexer,
17562 CPP_SEMICOLON))
17563 /* This is a `class-key identifier ;' */
17564 ts = ts_current;
17565 else
17566 ts = ts_global;
17568 template_p =
17569 (template_parm_lists_apply
17570 && (cp_parser_next_token_starts_class_definition_p (parser)
17571 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17572 /* An unqualified name was used to reference this type, so
17573 there were no qualifying templates. */
17574 if (template_parm_lists_apply
17575 && !cp_parser_check_template_parameters (parser,
17576 /*num_templates=*/0,
17577 token->location,
17578 /*declarator=*/NULL))
17579 return error_mark_node;
17580 type = xref_tag (tag_type, identifier, ts, template_p);
17584 if (type == error_mark_node)
17585 return error_mark_node;
17587 /* Allow attributes on forward declarations of classes. */
17588 if (attributes)
17590 if (TREE_CODE (type) == TYPENAME_TYPE)
17591 warning (OPT_Wattributes,
17592 "attributes ignored on uninstantiated type");
17593 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17594 && ! processing_explicit_instantiation)
17595 warning (OPT_Wattributes,
17596 "attributes ignored on template instantiation");
17597 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17598 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17599 else
17600 warning (OPT_Wattributes,
17601 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17604 if (tag_type != enum_type)
17606 /* Indicate whether this class was declared as a `class' or as a
17607 `struct'. */
17608 if (CLASS_TYPE_P (type))
17609 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17610 cp_parser_check_class_key (tag_type, type);
17613 /* A "<" cannot follow an elaborated type specifier. If that
17614 happens, the user was probably trying to form a template-id. */
17615 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17616 token->location);
17618 return type;
17621 /* Parse an enum-specifier.
17623 enum-specifier:
17624 enum-head { enumerator-list [opt] }
17625 enum-head { enumerator-list , } [C++0x]
17627 enum-head:
17628 enum-key identifier [opt] enum-base [opt]
17629 enum-key nested-name-specifier identifier enum-base [opt]
17631 enum-key:
17632 enum
17633 enum class [C++0x]
17634 enum struct [C++0x]
17636 enum-base: [C++0x]
17637 : type-specifier-seq
17639 opaque-enum-specifier:
17640 enum-key identifier enum-base [opt] ;
17642 GNU Extensions:
17643 enum-key attributes[opt] identifier [opt] enum-base [opt]
17644 { enumerator-list [opt] }attributes[opt]
17645 enum-key attributes[opt] identifier [opt] enum-base [opt]
17646 { enumerator-list, }attributes[opt] [C++0x]
17648 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17649 if the token stream isn't an enum-specifier after all. */
17651 static tree
17652 cp_parser_enum_specifier (cp_parser* parser)
17654 tree identifier;
17655 tree type = NULL_TREE;
17656 tree prev_scope;
17657 tree nested_name_specifier = NULL_TREE;
17658 tree attributes;
17659 bool scoped_enum_p = false;
17660 bool has_underlying_type = false;
17661 bool nested_being_defined = false;
17662 bool new_value_list = false;
17663 bool is_new_type = false;
17664 bool is_unnamed = false;
17665 tree underlying_type = NULL_TREE;
17666 cp_token *type_start_token = NULL;
17667 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17669 parser->colon_corrects_to_scope_p = false;
17671 /* Parse tentatively so that we can back up if we don't find a
17672 enum-specifier. */
17673 cp_parser_parse_tentatively (parser);
17675 /* Caller guarantees that the current token is 'enum', an identifier
17676 possibly follows, and the token after that is an opening brace.
17677 If we don't have an identifier, fabricate an anonymous name for
17678 the enumeration being defined. */
17679 cp_lexer_consume_token (parser->lexer);
17681 /* Parse the "class" or "struct", which indicates a scoped
17682 enumeration type in C++0x. */
17683 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17684 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17686 if (cxx_dialect < cxx11)
17687 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17689 /* Consume the `struct' or `class' token. */
17690 cp_lexer_consume_token (parser->lexer);
17692 scoped_enum_p = true;
17695 attributes = cp_parser_attributes_opt (parser);
17697 /* Clear the qualification. */
17698 parser->scope = NULL_TREE;
17699 parser->qualifying_scope = NULL_TREE;
17700 parser->object_scope = NULL_TREE;
17702 /* Figure out in what scope the declaration is being placed. */
17703 prev_scope = current_scope ();
17705 type_start_token = cp_lexer_peek_token (parser->lexer);
17707 push_deferring_access_checks (dk_no_check);
17708 nested_name_specifier
17709 = cp_parser_nested_name_specifier_opt (parser,
17710 /*typename_keyword_p=*/true,
17711 /*check_dependency_p=*/false,
17712 /*type_p=*/false,
17713 /*is_declaration=*/false);
17715 if (nested_name_specifier)
17717 tree name;
17719 identifier = cp_parser_identifier (parser);
17720 name = cp_parser_lookup_name (parser, identifier,
17721 enum_type,
17722 /*is_template=*/false,
17723 /*is_namespace=*/false,
17724 /*check_dependency=*/true,
17725 /*ambiguous_decls=*/NULL,
17726 input_location);
17727 if (name && name != error_mark_node)
17729 type = TREE_TYPE (name);
17730 if (TREE_CODE (type) == TYPENAME_TYPE)
17732 /* Are template enums allowed in ISO? */
17733 if (template_parm_scope_p ())
17734 pedwarn (type_start_token->location, OPT_Wpedantic,
17735 "%qD is an enumeration template", name);
17736 /* ignore a typename reference, for it will be solved by name
17737 in start_enum. */
17738 type = NULL_TREE;
17741 else if (nested_name_specifier == error_mark_node)
17742 /* We already issued an error. */;
17743 else
17745 error_at (type_start_token->location,
17746 "%qD does not name an enumeration in %qT",
17747 identifier, nested_name_specifier);
17748 nested_name_specifier = error_mark_node;
17751 else
17753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17754 identifier = cp_parser_identifier (parser);
17755 else
17757 identifier = make_anon_name ();
17758 is_unnamed = true;
17759 if (scoped_enum_p)
17760 error_at (type_start_token->location,
17761 "unnamed scoped enum is not allowed");
17764 pop_deferring_access_checks ();
17766 /* Check for the `:' that denotes a specified underlying type in C++0x.
17767 Note that a ':' could also indicate a bitfield width, however. */
17768 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17770 cp_decl_specifier_seq type_specifiers;
17772 /* Consume the `:'. */
17773 cp_lexer_consume_token (parser->lexer);
17775 /* Parse the type-specifier-seq. */
17776 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17777 /*is_trailing_return=*/false,
17778 &type_specifiers);
17780 /* At this point this is surely not elaborated type specifier. */
17781 if (!cp_parser_parse_definitely (parser))
17782 return NULL_TREE;
17784 if (cxx_dialect < cxx11)
17785 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17787 has_underlying_type = true;
17789 /* If that didn't work, stop. */
17790 if (type_specifiers.type != error_mark_node)
17792 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17793 /*initialized=*/0, NULL);
17794 if (underlying_type == error_mark_node
17795 || check_for_bare_parameter_packs (underlying_type))
17796 underlying_type = NULL_TREE;
17800 /* Look for the `{' but don't consume it yet. */
17801 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17803 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17805 cp_parser_error (parser, "expected %<{%>");
17806 if (has_underlying_type)
17808 type = NULL_TREE;
17809 goto out;
17812 /* An opaque-enum-specifier must have a ';' here. */
17813 if ((scoped_enum_p || underlying_type)
17814 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17816 cp_parser_error (parser, "expected %<;%> or %<{%>");
17817 if (has_underlying_type)
17819 type = NULL_TREE;
17820 goto out;
17825 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17826 return NULL_TREE;
17828 if (nested_name_specifier)
17830 if (CLASS_TYPE_P (nested_name_specifier))
17832 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17833 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17834 push_scope (nested_name_specifier);
17836 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17838 push_nested_namespace (nested_name_specifier);
17842 /* Issue an error message if type-definitions are forbidden here. */
17843 if (!cp_parser_check_type_definition (parser))
17844 type = error_mark_node;
17845 else
17846 /* Create the new type. We do this before consuming the opening
17847 brace so the enum will be recorded as being on the line of its
17848 tag (or the 'enum' keyword, if there is no tag). */
17849 type = start_enum (identifier, type, underlying_type,
17850 attributes, scoped_enum_p, &is_new_type);
17852 /* If the next token is not '{' it is an opaque-enum-specifier or an
17853 elaborated-type-specifier. */
17854 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17856 timevar_push (TV_PARSE_ENUM);
17857 if (nested_name_specifier
17858 && nested_name_specifier != error_mark_node)
17860 /* The following catches invalid code such as:
17861 enum class S<int>::E { A, B, C }; */
17862 if (!processing_specialization
17863 && CLASS_TYPE_P (nested_name_specifier)
17864 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17865 error_at (type_start_token->location, "cannot add an enumerator "
17866 "list to a template instantiation");
17868 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17870 error_at (type_start_token->location,
17871 "%<%T::%E%> has not been declared",
17872 TYPE_CONTEXT (nested_name_specifier),
17873 nested_name_specifier);
17874 type = error_mark_node;
17876 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17877 && !CLASS_TYPE_P (nested_name_specifier))
17879 error_at (type_start_token->location, "nested name specifier "
17880 "%qT for enum declaration does not name a class "
17881 "or namespace", nested_name_specifier);
17882 type = error_mark_node;
17884 /* If that scope does not contain the scope in which the
17885 class was originally declared, the program is invalid. */
17886 else if (prev_scope && !is_ancestor (prev_scope,
17887 nested_name_specifier))
17889 if (at_namespace_scope_p ())
17890 error_at (type_start_token->location,
17891 "declaration of %qD in namespace %qD which does not "
17892 "enclose %qD",
17893 type, prev_scope, nested_name_specifier);
17894 else
17895 error_at (type_start_token->location,
17896 "declaration of %qD in %qD which does not "
17897 "enclose %qD",
17898 type, prev_scope, nested_name_specifier);
17899 type = error_mark_node;
17901 /* If that scope is the scope where the declaration is being placed
17902 the program is invalid. */
17903 else if (CLASS_TYPE_P (nested_name_specifier)
17904 && CLASS_TYPE_P (prev_scope)
17905 && same_type_p (nested_name_specifier, prev_scope))
17907 permerror (type_start_token->location,
17908 "extra qualification not allowed");
17909 nested_name_specifier = NULL_TREE;
17913 if (scoped_enum_p)
17914 begin_scope (sk_scoped_enum, type);
17916 /* Consume the opening brace. */
17917 cp_lexer_consume_token (parser->lexer);
17919 if (type == error_mark_node)
17920 ; /* Nothing to add */
17921 else if (OPAQUE_ENUM_P (type)
17922 || (cxx_dialect > cxx98 && processing_specialization))
17924 new_value_list = true;
17925 SET_OPAQUE_ENUM_P (type, false);
17926 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17928 else
17930 error_at (type_start_token->location,
17931 "multiple definition of %q#T", type);
17932 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17933 "previous definition here");
17934 type = error_mark_node;
17937 if (type == error_mark_node)
17938 cp_parser_skip_to_end_of_block_or_statement (parser);
17939 /* If the next token is not '}', then there are some enumerators. */
17940 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17942 if (is_unnamed && !scoped_enum_p)
17943 pedwarn (type_start_token->location, OPT_Wpedantic,
17944 "ISO C++ forbids empty unnamed enum");
17946 else
17947 cp_parser_enumerator_list (parser, type);
17949 /* Consume the final '}'. */
17950 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17952 if (scoped_enum_p)
17953 finish_scope ();
17954 timevar_pop (TV_PARSE_ENUM);
17956 else
17958 /* If a ';' follows, then it is an opaque-enum-specifier
17959 and additional restrictions apply. */
17960 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17962 if (is_unnamed)
17963 error_at (type_start_token->location,
17964 "opaque-enum-specifier without name");
17965 else if (nested_name_specifier)
17966 error_at (type_start_token->location,
17967 "opaque-enum-specifier must use a simple identifier");
17971 /* Look for trailing attributes to apply to this enumeration, and
17972 apply them if appropriate. */
17973 if (cp_parser_allow_gnu_extensions_p (parser))
17975 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17976 cplus_decl_attributes (&type,
17977 trailing_attr,
17978 (int) ATTR_FLAG_TYPE_IN_PLACE);
17981 /* Finish up the enumeration. */
17982 if (type != error_mark_node)
17984 if (new_value_list)
17985 finish_enum_value_list (type);
17986 if (is_new_type)
17987 finish_enum (type);
17990 if (nested_name_specifier)
17992 if (CLASS_TYPE_P (nested_name_specifier))
17994 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17995 pop_scope (nested_name_specifier);
17997 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17999 pop_nested_namespace (nested_name_specifier);
18002 out:
18003 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18004 return type;
18007 /* Parse an enumerator-list. The enumerators all have the indicated
18008 TYPE.
18010 enumerator-list:
18011 enumerator-definition
18012 enumerator-list , enumerator-definition */
18014 static void
18015 cp_parser_enumerator_list (cp_parser* parser, tree type)
18017 while (true)
18019 /* Parse an enumerator-definition. */
18020 cp_parser_enumerator_definition (parser, type);
18022 /* If the next token is not a ',', we've reached the end of
18023 the list. */
18024 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18025 break;
18026 /* Otherwise, consume the `,' and keep going. */
18027 cp_lexer_consume_token (parser->lexer);
18028 /* If the next token is a `}', there is a trailing comma. */
18029 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18031 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18032 pedwarn (input_location, OPT_Wpedantic,
18033 "comma at end of enumerator list");
18034 break;
18039 /* Parse an enumerator-definition. The enumerator has the indicated
18040 TYPE.
18042 enumerator-definition:
18043 enumerator
18044 enumerator = constant-expression
18046 enumerator:
18047 identifier
18049 GNU Extensions:
18051 enumerator-definition:
18052 enumerator attributes [opt]
18053 enumerator attributes [opt] = constant-expression */
18055 static void
18056 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18058 tree identifier;
18059 tree value;
18060 location_t loc;
18062 /* Save the input location because we are interested in the location
18063 of the identifier and not the location of the explicit value. */
18064 loc = cp_lexer_peek_token (parser->lexer)->location;
18066 /* Look for the identifier. */
18067 identifier = cp_parser_identifier (parser);
18068 if (identifier == error_mark_node)
18069 return;
18071 /* Parse any specified attributes. */
18072 tree attrs = cp_parser_attributes_opt (parser);
18074 /* If the next token is an '=', then there is an explicit value. */
18075 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18077 /* Consume the `=' token. */
18078 cp_lexer_consume_token (parser->lexer);
18079 /* Parse the value. */
18080 value = cp_parser_constant_expression (parser);
18082 else
18083 value = NULL_TREE;
18085 /* If we are processing a template, make sure the initializer of the
18086 enumerator doesn't contain any bare template parameter pack. */
18087 if (check_for_bare_parameter_packs (value))
18088 value = error_mark_node;
18090 /* Create the enumerator. */
18091 build_enumerator (identifier, value, type, attrs, loc);
18094 /* Parse a namespace-name.
18096 namespace-name:
18097 original-namespace-name
18098 namespace-alias
18100 Returns the NAMESPACE_DECL for the namespace. */
18102 static tree
18103 cp_parser_namespace_name (cp_parser* parser)
18105 tree identifier;
18106 tree namespace_decl;
18108 cp_token *token = cp_lexer_peek_token (parser->lexer);
18110 /* Get the name of the namespace. */
18111 identifier = cp_parser_identifier (parser);
18112 if (identifier == error_mark_node)
18113 return error_mark_node;
18115 /* Look up the identifier in the currently active scope. Look only
18116 for namespaces, due to:
18118 [basic.lookup.udir]
18120 When looking up a namespace-name in a using-directive or alias
18121 definition, only namespace names are considered.
18123 And:
18125 [basic.lookup.qual]
18127 During the lookup of a name preceding the :: scope resolution
18128 operator, object, function, and enumerator names are ignored.
18130 (Note that cp_parser_qualifying_entity only calls this
18131 function if the token after the name is the scope resolution
18132 operator.) */
18133 namespace_decl = cp_parser_lookup_name (parser, identifier,
18134 none_type,
18135 /*is_template=*/false,
18136 /*is_namespace=*/true,
18137 /*check_dependency=*/true,
18138 /*ambiguous_decls=*/NULL,
18139 token->location);
18140 /* If it's not a namespace, issue an error. */
18141 if (namespace_decl == error_mark_node
18142 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18144 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18145 error_at (token->location, "%qD is not a namespace-name", identifier);
18146 cp_parser_error (parser, "expected namespace-name");
18147 namespace_decl = error_mark_node;
18150 return namespace_decl;
18153 /* Parse a namespace-definition.
18155 namespace-definition:
18156 named-namespace-definition
18157 unnamed-namespace-definition
18159 named-namespace-definition:
18160 original-namespace-definition
18161 extension-namespace-definition
18163 original-namespace-definition:
18164 namespace identifier { namespace-body }
18166 extension-namespace-definition:
18167 namespace original-namespace-name { namespace-body }
18169 unnamed-namespace-definition:
18170 namespace { namespace-body } */
18172 static void
18173 cp_parser_namespace_definition (cp_parser* parser)
18175 tree identifier, attribs;
18176 bool has_visibility;
18177 bool is_inline;
18178 cp_token* token;
18179 int nested_definition_count = 0;
18181 cp_ensure_no_omp_declare_simd (parser);
18182 cp_ensure_no_oacc_routine (parser);
18183 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18185 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18186 is_inline = true;
18187 cp_lexer_consume_token (parser->lexer);
18189 else
18190 is_inline = false;
18192 /* Look for the `namespace' keyword. */
18193 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18195 /* Parse any specified attributes before the identifier. */
18196 attribs = cp_parser_attributes_opt (parser);
18198 /* Get the name of the namespace. We do not attempt to distinguish
18199 between an original-namespace-definition and an
18200 extension-namespace-definition at this point. The semantic
18201 analysis routines are responsible for that. */
18202 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18203 identifier = cp_parser_identifier (parser);
18204 else
18205 identifier = NULL_TREE;
18207 /* Parse any specified attributes after the identifier. */
18208 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18209 if (post_ident_attribs)
18211 if (attribs)
18212 attribs = chainon (attribs, post_ident_attribs);
18213 else
18214 attribs = post_ident_attribs;
18217 /* Start the namespace. */
18218 bool ok = push_namespace (identifier);
18220 /* Parse any nested namespace definition. */
18221 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18223 if (attribs)
18224 error_at (token->location, "a nested namespace definition cannot have attributes");
18225 if (cxx_dialect < cxx1z)
18226 pedwarn (input_location, OPT_Wpedantic,
18227 "nested namespace definitions only available with "
18228 "-std=c++1z or -std=gnu++1z");
18229 if (is_inline)
18230 error_at (token->location, "a nested namespace definition cannot be inline");
18231 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18233 cp_lexer_consume_token (parser->lexer);
18234 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18235 identifier = cp_parser_identifier (parser);
18236 else
18238 cp_parser_error (parser, "nested identifier required");
18239 break;
18241 if (push_namespace (identifier))
18242 ++nested_definition_count;
18246 /* Look for the `{' to validate starting the namespace. */
18247 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18249 /* "inline namespace" is equivalent to a stub namespace definition
18250 followed by a strong using directive. */
18251 if (is_inline && ok)
18253 tree name_space = current_namespace;
18254 /* Set up namespace association. */
18255 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18256 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18257 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18258 /* Import the contents of the inline namespace. */
18259 pop_namespace ();
18260 do_using_directive (name_space);
18261 push_namespace (identifier);
18264 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18266 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18268 /* Parse the body of the namespace. */
18269 cp_parser_namespace_body (parser);
18271 if (has_visibility)
18272 pop_visibility (1);
18274 /* Finish the nested namespace definitions. */
18275 while (nested_definition_count--)
18276 pop_namespace ();
18278 /* Finish the namespace. */
18279 if (ok)
18280 pop_namespace ();
18281 /* Look for the final `}'. */
18282 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18285 /* Parse a namespace-body.
18287 namespace-body:
18288 declaration-seq [opt] */
18290 static void
18291 cp_parser_namespace_body (cp_parser* parser)
18293 cp_parser_declaration_seq_opt (parser);
18296 /* Parse a namespace-alias-definition.
18298 namespace-alias-definition:
18299 namespace identifier = qualified-namespace-specifier ; */
18301 static void
18302 cp_parser_namespace_alias_definition (cp_parser* parser)
18304 tree identifier;
18305 tree namespace_specifier;
18307 cp_token *token = cp_lexer_peek_token (parser->lexer);
18309 /* Look for the `namespace' keyword. */
18310 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18311 /* Look for the identifier. */
18312 identifier = cp_parser_identifier (parser);
18313 if (identifier == error_mark_node)
18314 return;
18315 /* Look for the `=' token. */
18316 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18317 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18319 error_at (token->location, "%<namespace%> definition is not allowed here");
18320 /* Skip the definition. */
18321 cp_lexer_consume_token (parser->lexer);
18322 if (cp_parser_skip_to_closing_brace (parser))
18323 cp_lexer_consume_token (parser->lexer);
18324 return;
18326 cp_parser_require (parser, CPP_EQ, RT_EQ);
18327 /* Look for the qualified-namespace-specifier. */
18328 namespace_specifier
18329 = cp_parser_qualified_namespace_specifier (parser);
18330 /* Look for the `;' token. */
18331 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18333 /* Register the alias in the symbol table. */
18334 do_namespace_alias (identifier, namespace_specifier);
18337 /* Parse a qualified-namespace-specifier.
18339 qualified-namespace-specifier:
18340 :: [opt] nested-name-specifier [opt] namespace-name
18342 Returns a NAMESPACE_DECL corresponding to the specified
18343 namespace. */
18345 static tree
18346 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18348 /* Look for the optional `::'. */
18349 cp_parser_global_scope_opt (parser,
18350 /*current_scope_valid_p=*/false);
18352 /* Look for the optional nested-name-specifier. */
18353 cp_parser_nested_name_specifier_opt (parser,
18354 /*typename_keyword_p=*/false,
18355 /*check_dependency_p=*/true,
18356 /*type_p=*/false,
18357 /*is_declaration=*/true);
18359 return cp_parser_namespace_name (parser);
18362 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18363 access declaration.
18365 using-declaration:
18366 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18367 using :: unqualified-id ;
18369 access-declaration:
18370 qualified-id ;
18374 static bool
18375 cp_parser_using_declaration (cp_parser* parser,
18376 bool access_declaration_p)
18378 cp_token *token;
18379 bool typename_p = false;
18380 bool global_scope_p;
18381 tree decl;
18382 tree identifier;
18383 tree qscope;
18384 int oldcount = errorcount;
18385 cp_token *diag_token = NULL;
18387 if (access_declaration_p)
18389 diag_token = cp_lexer_peek_token (parser->lexer);
18390 cp_parser_parse_tentatively (parser);
18392 else
18394 /* Look for the `using' keyword. */
18395 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18397 again:
18398 /* Peek at the next token. */
18399 token = cp_lexer_peek_token (parser->lexer);
18400 /* See if it's `typename'. */
18401 if (token->keyword == RID_TYPENAME)
18403 /* Remember that we've seen it. */
18404 typename_p = true;
18405 /* Consume the `typename' token. */
18406 cp_lexer_consume_token (parser->lexer);
18410 /* Look for the optional global scope qualification. */
18411 global_scope_p
18412 = (cp_parser_global_scope_opt (parser,
18413 /*current_scope_valid_p=*/false)
18414 != NULL_TREE);
18416 /* If we saw `typename', or didn't see `::', then there must be a
18417 nested-name-specifier present. */
18418 if (typename_p || !global_scope_p)
18420 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18421 /*check_dependency_p=*/true,
18422 /*type_p=*/false,
18423 /*is_declaration=*/true);
18424 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18426 cp_parser_skip_to_end_of_block_or_statement (parser);
18427 return false;
18430 /* Otherwise, we could be in either of the two productions. In that
18431 case, treat the nested-name-specifier as optional. */
18432 else
18433 qscope = cp_parser_nested_name_specifier_opt (parser,
18434 /*typename_keyword_p=*/false,
18435 /*check_dependency_p=*/true,
18436 /*type_p=*/false,
18437 /*is_declaration=*/true);
18438 if (!qscope)
18439 qscope = global_namespace;
18440 else if (UNSCOPED_ENUM_P (qscope))
18441 qscope = CP_TYPE_CONTEXT (qscope);
18443 if (access_declaration_p && cp_parser_error_occurred (parser))
18444 /* Something has already gone wrong; there's no need to parse
18445 further. Since an error has occurred, the return value of
18446 cp_parser_parse_definitely will be false, as required. */
18447 return cp_parser_parse_definitely (parser);
18449 token = cp_lexer_peek_token (parser->lexer);
18450 /* Parse the unqualified-id. */
18451 identifier = cp_parser_unqualified_id (parser,
18452 /*template_keyword_p=*/false,
18453 /*check_dependency_p=*/true,
18454 /*declarator_p=*/true,
18455 /*optional_p=*/false);
18457 if (access_declaration_p)
18459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18460 cp_parser_simulate_error (parser);
18461 if (!cp_parser_parse_definitely (parser))
18462 return false;
18464 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18466 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18467 if (cxx_dialect < cxx1z
18468 && !in_system_header_at (ell->location))
18469 pedwarn (ell->location, 0,
18470 "pack expansion in using-declaration only available "
18471 "with -std=c++1z or -std=gnu++1z");
18472 qscope = make_pack_expansion (qscope);
18475 /* The function we call to handle a using-declaration is different
18476 depending on what scope we are in. */
18477 if (qscope == error_mark_node || identifier == error_mark_node)
18479 else if (!identifier_p (identifier)
18480 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18481 /* [namespace.udecl]
18483 A using declaration shall not name a template-id. */
18484 error_at (token->location,
18485 "a template-id may not appear in a using-declaration");
18486 else
18488 if (at_class_scope_p ())
18490 /* Create the USING_DECL. */
18491 decl = do_class_using_decl (qscope, identifier);
18493 if (decl && typename_p)
18494 USING_DECL_TYPENAME_P (decl) = 1;
18496 if (check_for_bare_parameter_packs (decl))
18498 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18499 return false;
18501 else
18502 /* Add it to the list of members in this class. */
18503 finish_member_declaration (decl);
18505 else
18507 decl = cp_parser_lookup_name_simple (parser,
18508 identifier,
18509 token->location);
18510 if (decl == error_mark_node)
18511 cp_parser_name_lookup_error (parser, identifier,
18512 decl, NLE_NULL,
18513 token->location);
18514 else if (check_for_bare_parameter_packs (decl))
18516 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18517 return false;
18519 else if (!at_namespace_scope_p ())
18520 do_local_using_decl (decl, qscope, identifier);
18521 else
18522 do_toplevel_using_decl (decl, qscope, identifier);
18526 if (!access_declaration_p
18527 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18529 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18530 if (cxx_dialect < cxx1z)
18531 pedwarn (comma->location, 0,
18532 "comma-separated list in using-declaration only available "
18533 "with -std=c++1z or -std=gnu++1z");
18534 goto again;
18537 /* Look for the final `;'. */
18538 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18540 if (access_declaration_p && errorcount == oldcount)
18541 warning_at (diag_token->location, OPT_Wdeprecated,
18542 "access declarations are deprecated "
18543 "in favour of using-declarations; "
18544 "suggestion: add the %<using%> keyword");
18546 return true;
18549 /* Parse an alias-declaration.
18551 alias-declaration:
18552 using identifier attribute-specifier-seq [opt] = type-id */
18554 static tree
18555 cp_parser_alias_declaration (cp_parser* parser)
18557 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18558 location_t id_location;
18559 cp_declarator *declarator;
18560 cp_decl_specifier_seq decl_specs;
18561 bool member_p;
18562 const char *saved_message = NULL;
18564 /* Look for the `using' keyword. */
18565 cp_token *using_token
18566 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18567 if (using_token == NULL)
18568 return error_mark_node;
18570 id_location = cp_lexer_peek_token (parser->lexer)->location;
18571 id = cp_parser_identifier (parser);
18572 if (id == error_mark_node)
18573 return error_mark_node;
18575 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18576 attributes = cp_parser_attributes_opt (parser);
18577 if (attributes == error_mark_node)
18578 return error_mark_node;
18580 cp_parser_require (parser, CPP_EQ, RT_EQ);
18582 if (cp_parser_error_occurred (parser))
18583 return error_mark_node;
18585 cp_parser_commit_to_tentative_parse (parser);
18587 /* Now we are going to parse the type-id of the declaration. */
18590 [dcl.type]/3 says:
18592 "A type-specifier-seq shall not define a class or enumeration
18593 unless it appears in the type-id of an alias-declaration (7.1.3) that
18594 is not the declaration of a template-declaration."
18596 In other words, if we currently are in an alias template, the
18597 type-id should not define a type.
18599 So let's set parser->type_definition_forbidden_message in that
18600 case; cp_parser_check_type_definition (called by
18601 cp_parser_class_specifier) will then emit an error if a type is
18602 defined in the type-id. */
18603 if (parser->num_template_parameter_lists)
18605 saved_message = parser->type_definition_forbidden_message;
18606 parser->type_definition_forbidden_message =
18607 G_("types may not be defined in alias template declarations");
18610 type = cp_parser_type_id (parser);
18612 /* Restore the error message if need be. */
18613 if (parser->num_template_parameter_lists)
18614 parser->type_definition_forbidden_message = saved_message;
18616 if (type == error_mark_node
18617 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18619 cp_parser_skip_to_end_of_block_or_statement (parser);
18620 return error_mark_node;
18623 /* A typedef-name can also be introduced by an alias-declaration. The
18624 identifier following the using keyword becomes a typedef-name. It has
18625 the same semantics as if it were introduced by the typedef
18626 specifier. In particular, it does not define a new type and it shall
18627 not appear in the type-id. */
18629 clear_decl_specs (&decl_specs);
18630 decl_specs.type = type;
18631 if (attributes != NULL_TREE)
18633 decl_specs.attributes = attributes;
18634 set_and_check_decl_spec_loc (&decl_specs,
18635 ds_attribute,
18636 attrs_token);
18638 set_and_check_decl_spec_loc (&decl_specs,
18639 ds_typedef,
18640 using_token);
18641 set_and_check_decl_spec_loc (&decl_specs,
18642 ds_alias,
18643 using_token);
18645 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18646 declarator->id_loc = id_location;
18648 member_p = at_class_scope_p ();
18649 if (member_p)
18650 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18651 NULL_TREE, attributes);
18652 else
18653 decl = start_decl (declarator, &decl_specs, 0,
18654 attributes, NULL_TREE, &pushed_scope);
18655 if (decl == error_mark_node)
18656 return decl;
18658 // Attach constraints to the alias declaration.
18659 if (flag_concepts && current_template_parms)
18661 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18662 tree constr = build_constraints (reqs, NULL_TREE);
18663 set_constraints (decl, constr);
18666 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18668 if (pushed_scope)
18669 pop_scope (pushed_scope);
18671 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18672 added into the symbol table; otherwise, return the TYPE_DECL. */
18673 if (DECL_LANG_SPECIFIC (decl)
18674 && DECL_TEMPLATE_INFO (decl)
18675 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18677 decl = DECL_TI_TEMPLATE (decl);
18678 if (member_p)
18679 check_member_template (decl);
18682 return decl;
18685 /* Parse a using-directive.
18687 using-directive:
18688 using namespace :: [opt] nested-name-specifier [opt]
18689 namespace-name ; */
18691 static void
18692 cp_parser_using_directive (cp_parser* parser)
18694 tree namespace_decl;
18695 tree attribs;
18697 /* Look for the `using' keyword. */
18698 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18699 /* And the `namespace' keyword. */
18700 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18701 /* Look for the optional `::' operator. */
18702 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18703 /* And the optional nested-name-specifier. */
18704 cp_parser_nested_name_specifier_opt (parser,
18705 /*typename_keyword_p=*/false,
18706 /*check_dependency_p=*/true,
18707 /*type_p=*/false,
18708 /*is_declaration=*/true);
18709 /* Get the namespace being used. */
18710 namespace_decl = cp_parser_namespace_name (parser);
18711 /* And any specified attributes. */
18712 attribs = cp_parser_attributes_opt (parser);
18713 /* Update the symbol table. */
18714 parse_using_directive (namespace_decl, attribs);
18715 /* Look for the final `;'. */
18716 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18719 /* Parse an asm-definition.
18721 asm-definition:
18722 asm ( string-literal ) ;
18724 GNU Extension:
18726 asm-definition:
18727 asm volatile [opt] ( string-literal ) ;
18728 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18729 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18730 : asm-operand-list [opt] ) ;
18731 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18732 : asm-operand-list [opt]
18733 : asm-clobber-list [opt] ) ;
18734 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18735 : asm-clobber-list [opt]
18736 : asm-goto-list ) ; */
18738 static void
18739 cp_parser_asm_definition (cp_parser* parser)
18741 tree string;
18742 tree outputs = NULL_TREE;
18743 tree inputs = NULL_TREE;
18744 tree clobbers = NULL_TREE;
18745 tree labels = NULL_TREE;
18746 tree asm_stmt;
18747 bool volatile_p = false;
18748 bool extended_p = false;
18749 bool invalid_inputs_p = false;
18750 bool invalid_outputs_p = false;
18751 bool goto_p = false;
18752 required_token missing = RT_NONE;
18754 /* Look for the `asm' keyword. */
18755 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18757 if (parser->in_function_body
18758 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18760 error ("%<asm%> in %<constexpr%> function");
18761 cp_function_chain->invalid_constexpr = true;
18764 /* See if the next token is `volatile'. */
18765 if (cp_parser_allow_gnu_extensions_p (parser)
18766 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18768 /* Remember that we saw the `volatile' keyword. */
18769 volatile_p = true;
18770 /* Consume the token. */
18771 cp_lexer_consume_token (parser->lexer);
18773 if (cp_parser_allow_gnu_extensions_p (parser)
18774 && parser->in_function_body
18775 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18777 /* Remember that we saw the `goto' keyword. */
18778 goto_p = true;
18779 /* Consume the token. */
18780 cp_lexer_consume_token (parser->lexer);
18782 /* Look for the opening `('. */
18783 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18784 return;
18785 /* Look for the string. */
18786 string = cp_parser_string_literal (parser, false, false);
18787 if (string == error_mark_node)
18789 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18790 /*consume_paren=*/true);
18791 return;
18794 /* If we're allowing GNU extensions, check for the extended assembly
18795 syntax. Unfortunately, the `:' tokens need not be separated by
18796 a space in C, and so, for compatibility, we tolerate that here
18797 too. Doing that means that we have to treat the `::' operator as
18798 two `:' tokens. */
18799 if (cp_parser_allow_gnu_extensions_p (parser)
18800 && parser->in_function_body
18801 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18802 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18804 bool inputs_p = false;
18805 bool clobbers_p = false;
18806 bool labels_p = false;
18808 /* The extended syntax was used. */
18809 extended_p = true;
18811 /* Look for outputs. */
18812 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18814 /* Consume the `:'. */
18815 cp_lexer_consume_token (parser->lexer);
18816 /* Parse the output-operands. */
18817 if (cp_lexer_next_token_is_not (parser->lexer,
18818 CPP_COLON)
18819 && cp_lexer_next_token_is_not (parser->lexer,
18820 CPP_SCOPE)
18821 && cp_lexer_next_token_is_not (parser->lexer,
18822 CPP_CLOSE_PAREN)
18823 && !goto_p)
18825 outputs = cp_parser_asm_operand_list (parser);
18826 if (outputs == error_mark_node)
18827 invalid_outputs_p = true;
18830 /* If the next token is `::', there are no outputs, and the
18831 next token is the beginning of the inputs. */
18832 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18833 /* The inputs are coming next. */
18834 inputs_p = true;
18836 /* Look for inputs. */
18837 if (inputs_p
18838 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18840 /* Consume the `:' or `::'. */
18841 cp_lexer_consume_token (parser->lexer);
18842 /* Parse the output-operands. */
18843 if (cp_lexer_next_token_is_not (parser->lexer,
18844 CPP_COLON)
18845 && cp_lexer_next_token_is_not (parser->lexer,
18846 CPP_SCOPE)
18847 && cp_lexer_next_token_is_not (parser->lexer,
18848 CPP_CLOSE_PAREN))
18850 inputs = cp_parser_asm_operand_list (parser);
18851 if (inputs == error_mark_node)
18852 invalid_inputs_p = true;
18855 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18856 /* The clobbers are coming next. */
18857 clobbers_p = true;
18859 /* Look for clobbers. */
18860 if (clobbers_p
18861 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18863 clobbers_p = true;
18864 /* Consume the `:' or `::'. */
18865 cp_lexer_consume_token (parser->lexer);
18866 /* Parse the clobbers. */
18867 if (cp_lexer_next_token_is_not (parser->lexer,
18868 CPP_COLON)
18869 && cp_lexer_next_token_is_not (parser->lexer,
18870 CPP_CLOSE_PAREN))
18871 clobbers = cp_parser_asm_clobber_list (parser);
18873 else if (goto_p
18874 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18875 /* The labels are coming next. */
18876 labels_p = true;
18878 /* Look for labels. */
18879 if (labels_p
18880 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18882 labels_p = true;
18883 /* Consume the `:' or `::'. */
18884 cp_lexer_consume_token (parser->lexer);
18885 /* Parse the labels. */
18886 labels = cp_parser_asm_label_list (parser);
18889 if (goto_p && !labels_p)
18890 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18892 else if (goto_p)
18893 missing = RT_COLON_SCOPE;
18895 /* Look for the closing `)'. */
18896 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18897 missing ? missing : RT_CLOSE_PAREN))
18898 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18899 /*consume_paren=*/true);
18900 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18902 if (!invalid_inputs_p && !invalid_outputs_p)
18904 /* Create the ASM_EXPR. */
18905 if (parser->in_function_body)
18907 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18908 inputs, clobbers, labels);
18909 /* If the extended syntax was not used, mark the ASM_EXPR. */
18910 if (!extended_p)
18912 tree temp = asm_stmt;
18913 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18914 temp = TREE_OPERAND (temp, 0);
18916 ASM_INPUT_P (temp) = 1;
18919 else
18920 symtab->finalize_toplevel_asm (string);
18924 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18925 type that comes from the decl-specifier-seq. */
18927 static tree
18928 strip_declarator_types (tree type, cp_declarator *declarator)
18930 for (cp_declarator *d = declarator; d;)
18931 switch (d->kind)
18933 case cdk_id:
18934 case cdk_decomp:
18935 case cdk_error:
18936 d = NULL;
18937 break;
18939 default:
18940 if (TYPE_PTRMEMFUNC_P (type))
18941 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18942 type = TREE_TYPE (type);
18943 d = d->declarator;
18944 break;
18947 return type;
18950 /* Declarators [gram.dcl.decl] */
18952 /* Parse an init-declarator.
18954 init-declarator:
18955 declarator initializer [opt]
18957 GNU Extension:
18959 init-declarator:
18960 declarator asm-specification [opt] attributes [opt] initializer [opt]
18962 function-definition:
18963 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18964 function-body
18965 decl-specifier-seq [opt] declarator function-try-block
18967 GNU Extension:
18969 function-definition:
18970 __extension__ function-definition
18972 TM Extension:
18974 function-definition:
18975 decl-specifier-seq [opt] declarator function-transaction-block
18977 The DECL_SPECIFIERS apply to this declarator. Returns a
18978 representation of the entity declared. If MEMBER_P is TRUE, then
18979 this declarator appears in a class scope. The new DECL created by
18980 this declarator is returned.
18982 The CHECKS are access checks that should be performed once we know
18983 what entity is being declared (and, therefore, what classes have
18984 befriended it).
18986 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18987 for a function-definition here as well. If the declarator is a
18988 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18989 be TRUE upon return. By that point, the function-definition will
18990 have been completely parsed.
18992 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18993 is FALSE.
18995 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18996 parsed declaration if it is an uninitialized single declarator not followed
18997 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18998 if present, will not be consumed. If returned, this declarator will be
18999 created with SD_INITIALIZED but will not call cp_finish_decl.
19001 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19002 and there is an initializer, the pointed location_t is set to the
19003 location of the '=' or `(', or '{' in C++11 token introducing the
19004 initializer. */
19006 static tree
19007 cp_parser_init_declarator (cp_parser* parser,
19008 cp_decl_specifier_seq *decl_specifiers,
19009 vec<deferred_access_check, va_gc> *checks,
19010 bool function_definition_allowed_p,
19011 bool member_p,
19012 int declares_class_or_enum,
19013 bool* function_definition_p,
19014 tree* maybe_range_for_decl,
19015 location_t* init_loc,
19016 tree* auto_result)
19018 cp_token *token = NULL, *asm_spec_start_token = NULL,
19019 *attributes_start_token = NULL;
19020 cp_declarator *declarator;
19021 tree prefix_attributes;
19022 tree attributes = NULL;
19023 tree asm_specification;
19024 tree initializer;
19025 tree decl = NULL_TREE;
19026 tree scope;
19027 int is_initialized;
19028 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19029 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19030 "(...)". */
19031 enum cpp_ttype initialization_kind;
19032 bool is_direct_init = false;
19033 bool is_non_constant_init;
19034 int ctor_dtor_or_conv_p;
19035 bool friend_p = cp_parser_friend_p (decl_specifiers);
19036 tree pushed_scope = NULL_TREE;
19037 bool range_for_decl_p = false;
19038 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19039 location_t tmp_init_loc = UNKNOWN_LOCATION;
19041 /* Gather the attributes that were provided with the
19042 decl-specifiers. */
19043 prefix_attributes = decl_specifiers->attributes;
19045 /* Assume that this is not the declarator for a function
19046 definition. */
19047 if (function_definition_p)
19048 *function_definition_p = false;
19050 /* Default arguments are only permitted for function parameters. */
19051 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19052 parser->default_arg_ok_p = false;
19054 /* Defer access checks while parsing the declarator; we cannot know
19055 what names are accessible until we know what is being
19056 declared. */
19057 resume_deferring_access_checks ();
19059 token = cp_lexer_peek_token (parser->lexer);
19061 /* Parse the declarator. */
19062 declarator
19063 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19064 &ctor_dtor_or_conv_p,
19065 /*parenthesized_p=*/NULL,
19066 member_p, friend_p);
19067 /* Gather up the deferred checks. */
19068 stop_deferring_access_checks ();
19070 parser->default_arg_ok_p = saved_default_arg_ok_p;
19072 /* If the DECLARATOR was erroneous, there's no need to go
19073 further. */
19074 if (declarator == cp_error_declarator)
19075 return error_mark_node;
19077 /* Check that the number of template-parameter-lists is OK. */
19078 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19079 token->location))
19080 return error_mark_node;
19082 if (declares_class_or_enum & 2)
19083 cp_parser_check_for_definition_in_return_type (declarator,
19084 decl_specifiers->type,
19085 decl_specifiers->locations[ds_type_spec]);
19087 /* Figure out what scope the entity declared by the DECLARATOR is
19088 located in. `grokdeclarator' sometimes changes the scope, so
19089 we compute it now. */
19090 scope = get_scope_of_declarator (declarator);
19092 /* Perform any lookups in the declared type which were thought to be
19093 dependent, but are not in the scope of the declarator. */
19094 decl_specifiers->type
19095 = maybe_update_decl_type (decl_specifiers->type, scope);
19097 /* If we're allowing GNU extensions, look for an
19098 asm-specification. */
19099 if (cp_parser_allow_gnu_extensions_p (parser))
19101 /* Look for an asm-specification. */
19102 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19103 asm_specification = cp_parser_asm_specification_opt (parser);
19105 else
19106 asm_specification = NULL_TREE;
19108 /* Look for attributes. */
19109 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19110 attributes = cp_parser_attributes_opt (parser);
19112 /* Peek at the next token. */
19113 token = cp_lexer_peek_token (parser->lexer);
19115 bool bogus_implicit_tmpl = false;
19117 if (function_declarator_p (declarator))
19119 /* Handle C++17 deduction guides. */
19120 if (!decl_specifiers->type
19121 && ctor_dtor_or_conv_p <= 0
19122 && cxx_dialect >= cxx1z)
19124 cp_declarator *id = get_id_declarator (declarator);
19125 tree name = id->u.id.unqualified_name;
19126 parser->scope = id->u.id.qualifying_scope;
19127 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19128 if (tmpl
19129 && (DECL_CLASS_TEMPLATE_P (tmpl)
19130 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19132 id->u.id.unqualified_name = dguide_name (tmpl);
19133 id->u.id.sfk = sfk_deduction_guide;
19134 ctor_dtor_or_conv_p = 1;
19138 /* Check to see if the token indicates the start of a
19139 function-definition. */
19140 if (cp_parser_token_starts_function_definition_p (token))
19142 if (!function_definition_allowed_p)
19144 /* If a function-definition should not appear here, issue an
19145 error message. */
19146 cp_parser_error (parser,
19147 "a function-definition is not allowed here");
19148 return error_mark_node;
19151 location_t func_brace_location
19152 = cp_lexer_peek_token (parser->lexer)->location;
19154 /* Neither attributes nor an asm-specification are allowed
19155 on a function-definition. */
19156 if (asm_specification)
19157 error_at (asm_spec_start_token->location,
19158 "an asm-specification is not allowed "
19159 "on a function-definition");
19160 if (attributes)
19161 error_at (attributes_start_token->location,
19162 "attributes are not allowed "
19163 "on a function-definition");
19164 /* This is a function-definition. */
19165 *function_definition_p = true;
19167 /* Parse the function definition. */
19168 if (member_p)
19169 decl = cp_parser_save_member_function_body (parser,
19170 decl_specifiers,
19171 declarator,
19172 prefix_attributes);
19173 else
19174 decl =
19175 (cp_parser_function_definition_from_specifiers_and_declarator
19176 (parser, decl_specifiers, prefix_attributes, declarator));
19178 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19180 /* This is where the prologue starts... */
19181 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19182 = func_brace_location;
19185 return decl;
19188 else if (parser->fully_implicit_function_template_p)
19190 /* A non-template declaration involving a function parameter list
19191 containing an implicit template parameter will be made into a
19192 template. If the resulting declaration is not going to be an
19193 actual function then finish the template scope here to prevent it.
19194 An error message will be issued once we have a decl to talk about.
19196 FIXME probably we should do type deduction rather than create an
19197 implicit template, but the standard currently doesn't allow it. */
19198 bogus_implicit_tmpl = true;
19199 finish_fully_implicit_template (parser, NULL_TREE);
19202 /* [dcl.dcl]
19204 Only in function declarations for constructors, destructors, type
19205 conversions, and deduction guides can the decl-specifier-seq be omitted.
19207 We explicitly postpone this check past the point where we handle
19208 function-definitions because we tolerate function-definitions
19209 that are missing their return types in some modes. */
19210 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19212 cp_parser_error (parser,
19213 "expected constructor, destructor, or type conversion");
19214 return error_mark_node;
19217 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19218 if (token->type == CPP_EQ
19219 || token->type == CPP_OPEN_PAREN
19220 || token->type == CPP_OPEN_BRACE)
19222 is_initialized = SD_INITIALIZED;
19223 initialization_kind = token->type;
19224 if (maybe_range_for_decl)
19225 *maybe_range_for_decl = error_mark_node;
19226 tmp_init_loc = token->location;
19227 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19228 *init_loc = tmp_init_loc;
19230 if (token->type == CPP_EQ
19231 && function_declarator_p (declarator))
19233 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19234 if (t2->keyword == RID_DEFAULT)
19235 is_initialized = SD_DEFAULTED;
19236 else if (t2->keyword == RID_DELETE)
19237 is_initialized = SD_DELETED;
19240 else
19242 /* If the init-declarator isn't initialized and isn't followed by a
19243 `,' or `;', it's not a valid init-declarator. */
19244 if (token->type != CPP_COMMA
19245 && token->type != CPP_SEMICOLON)
19247 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19248 range_for_decl_p = true;
19249 else
19251 if (!maybe_range_for_decl)
19252 cp_parser_error (parser, "expected initializer");
19253 return error_mark_node;
19256 is_initialized = SD_UNINITIALIZED;
19257 initialization_kind = CPP_EOF;
19260 /* Because start_decl has side-effects, we should only call it if we
19261 know we're going ahead. By this point, we know that we cannot
19262 possibly be looking at any other construct. */
19263 cp_parser_commit_to_tentative_parse (parser);
19265 /* Enter the newly declared entry in the symbol table. If we're
19266 processing a declaration in a class-specifier, we wait until
19267 after processing the initializer. */
19268 if (!member_p)
19270 if (parser->in_unbraced_linkage_specification_p)
19271 decl_specifiers->storage_class = sc_extern;
19272 decl = start_decl (declarator, decl_specifiers,
19273 range_for_decl_p? SD_INITIALIZED : is_initialized,
19274 attributes, prefix_attributes, &pushed_scope);
19275 cp_finalize_omp_declare_simd (parser, decl);
19276 cp_finalize_oacc_routine (parser, decl, false);
19277 /* Adjust location of decl if declarator->id_loc is more appropriate:
19278 set, and decl wasn't merged with another decl, in which case its
19279 location would be different from input_location, and more accurate. */
19280 if (DECL_P (decl)
19281 && declarator->id_loc != UNKNOWN_LOCATION
19282 && DECL_SOURCE_LOCATION (decl) == input_location)
19283 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19285 else if (scope)
19286 /* Enter the SCOPE. That way unqualified names appearing in the
19287 initializer will be looked up in SCOPE. */
19288 pushed_scope = push_scope (scope);
19290 /* Perform deferred access control checks, now that we know in which
19291 SCOPE the declared entity resides. */
19292 if (!member_p && decl)
19294 tree saved_current_function_decl = NULL_TREE;
19296 /* If the entity being declared is a function, pretend that we
19297 are in its scope. If it is a `friend', it may have access to
19298 things that would not otherwise be accessible. */
19299 if (TREE_CODE (decl) == FUNCTION_DECL)
19301 saved_current_function_decl = current_function_decl;
19302 current_function_decl = decl;
19305 /* Perform access checks for template parameters. */
19306 cp_parser_perform_template_parameter_access_checks (checks);
19308 /* Perform the access control checks for the declarator and the
19309 decl-specifiers. */
19310 perform_deferred_access_checks (tf_warning_or_error);
19312 /* Restore the saved value. */
19313 if (TREE_CODE (decl) == FUNCTION_DECL)
19314 current_function_decl = saved_current_function_decl;
19317 /* Parse the initializer. */
19318 initializer = NULL_TREE;
19319 is_direct_init = false;
19320 is_non_constant_init = true;
19321 if (is_initialized)
19323 if (function_declarator_p (declarator))
19325 if (initialization_kind == CPP_EQ)
19326 initializer = cp_parser_pure_specifier (parser);
19327 else
19329 /* If the declaration was erroneous, we don't really
19330 know what the user intended, so just silently
19331 consume the initializer. */
19332 if (decl != error_mark_node)
19333 error_at (tmp_init_loc, "initializer provided for function");
19334 cp_parser_skip_to_closing_parenthesis (parser,
19335 /*recovering=*/true,
19336 /*or_comma=*/false,
19337 /*consume_paren=*/true);
19340 else
19342 /* We want to record the extra mangling scope for in-class
19343 initializers of class members and initializers of static data
19344 member templates. The former involves deferring
19345 parsing of the initializer until end of class as with default
19346 arguments. So right here we only handle the latter. */
19347 if (!member_p && processing_template_decl)
19348 start_lambda_scope (decl);
19349 initializer = cp_parser_initializer (parser,
19350 &is_direct_init,
19351 &is_non_constant_init);
19352 if (!member_p && processing_template_decl)
19353 finish_lambda_scope ();
19354 if (initializer == error_mark_node)
19355 cp_parser_skip_to_end_of_statement (parser);
19359 /* The old parser allows attributes to appear after a parenthesized
19360 initializer. Mark Mitchell proposed removing this functionality
19361 on the GCC mailing lists on 2002-08-13. This parser accepts the
19362 attributes -- but ignores them. */
19363 if (cp_parser_allow_gnu_extensions_p (parser)
19364 && initialization_kind == CPP_OPEN_PAREN)
19365 if (cp_parser_attributes_opt (parser))
19366 warning (OPT_Wattributes,
19367 "attributes after parenthesized initializer ignored");
19369 /* And now complain about a non-function implicit template. */
19370 if (bogus_implicit_tmpl && decl != error_mark_node)
19371 error_at (DECL_SOURCE_LOCATION (decl),
19372 "non-function %qD declared as implicit template", decl);
19374 /* For an in-class declaration, use `grokfield' to create the
19375 declaration. */
19376 if (member_p)
19378 if (pushed_scope)
19380 pop_scope (pushed_scope);
19381 pushed_scope = NULL_TREE;
19383 decl = grokfield (declarator, decl_specifiers,
19384 initializer, !is_non_constant_init,
19385 /*asmspec=*/NULL_TREE,
19386 chainon (attributes, prefix_attributes));
19387 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19388 cp_parser_save_default_args (parser, decl);
19389 cp_finalize_omp_declare_simd (parser, decl);
19390 cp_finalize_oacc_routine (parser, decl, false);
19393 /* Finish processing the declaration. But, skip member
19394 declarations. */
19395 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19397 cp_finish_decl (decl,
19398 initializer, !is_non_constant_init,
19399 asm_specification,
19400 /* If the initializer is in parentheses, then this is
19401 a direct-initialization, which means that an
19402 `explicit' constructor is OK. Otherwise, an
19403 `explicit' constructor cannot be used. */
19404 ((is_direct_init || !is_initialized)
19405 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19407 else if ((cxx_dialect != cxx98) && friend_p
19408 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19409 /* Core issue #226 (C++0x only): A default template-argument
19410 shall not be specified in a friend class template
19411 declaration. */
19412 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19413 /*is_partial=*/false, /*is_friend_decl=*/1);
19415 if (!friend_p && pushed_scope)
19416 pop_scope (pushed_scope);
19418 if (function_declarator_p (declarator)
19419 && parser->fully_implicit_function_template_p)
19421 if (member_p)
19422 decl = finish_fully_implicit_template (parser, decl);
19423 else
19424 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19427 if (auto_result && is_initialized && decl_specifiers->type
19428 && type_uses_auto (decl_specifiers->type))
19429 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19431 return decl;
19434 /* Parse a declarator.
19436 declarator:
19437 direct-declarator
19438 ptr-operator declarator
19440 abstract-declarator:
19441 ptr-operator abstract-declarator [opt]
19442 direct-abstract-declarator
19444 GNU Extensions:
19446 declarator:
19447 attributes [opt] direct-declarator
19448 attributes [opt] ptr-operator declarator
19450 abstract-declarator:
19451 attributes [opt] ptr-operator abstract-declarator [opt]
19452 attributes [opt] direct-abstract-declarator
19454 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19455 detect constructors, destructors, deduction guides, or conversion operators.
19456 It is set to -1 if the declarator is a name, and +1 if it is a
19457 function. Otherwise it is set to zero. Usually you just want to
19458 test for >0, but internally the negative value is used.
19460 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19461 a decl-specifier-seq unless it declares a constructor, destructor,
19462 or conversion. It might seem that we could check this condition in
19463 semantic analysis, rather than parsing, but that makes it difficult
19464 to handle something like `f()'. We want to notice that there are
19465 no decl-specifiers, and therefore realize that this is an
19466 expression, not a declaration.)
19468 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19469 the declarator is a direct-declarator of the form "(...)".
19471 MEMBER_P is true iff this declarator is a member-declarator.
19473 FRIEND_P is true iff this declarator is a friend. */
19475 static cp_declarator *
19476 cp_parser_declarator (cp_parser* parser,
19477 cp_parser_declarator_kind dcl_kind,
19478 int* ctor_dtor_or_conv_p,
19479 bool* parenthesized_p,
19480 bool member_p, bool friend_p)
19482 cp_declarator *declarator;
19483 enum tree_code code;
19484 cp_cv_quals cv_quals;
19485 tree class_type;
19486 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19488 /* Assume this is not a constructor, destructor, or type-conversion
19489 operator. */
19490 if (ctor_dtor_or_conv_p)
19491 *ctor_dtor_or_conv_p = 0;
19493 if (cp_parser_allow_gnu_extensions_p (parser))
19494 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19496 /* Check for the ptr-operator production. */
19497 cp_parser_parse_tentatively (parser);
19498 /* Parse the ptr-operator. */
19499 code = cp_parser_ptr_operator (parser,
19500 &class_type,
19501 &cv_quals,
19502 &std_attributes);
19504 /* If that worked, then we have a ptr-operator. */
19505 if (cp_parser_parse_definitely (parser))
19507 /* If a ptr-operator was found, then this declarator was not
19508 parenthesized. */
19509 if (parenthesized_p)
19510 *parenthesized_p = true;
19511 /* The dependent declarator is optional if we are parsing an
19512 abstract-declarator. */
19513 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19514 cp_parser_parse_tentatively (parser);
19516 /* Parse the dependent declarator. */
19517 declarator = cp_parser_declarator (parser, dcl_kind,
19518 /*ctor_dtor_or_conv_p=*/NULL,
19519 /*parenthesized_p=*/NULL,
19520 /*member_p=*/false,
19521 friend_p);
19523 /* If we are parsing an abstract-declarator, we must handle the
19524 case where the dependent declarator is absent. */
19525 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19526 && !cp_parser_parse_definitely (parser))
19527 declarator = NULL;
19529 declarator = cp_parser_make_indirect_declarator
19530 (code, class_type, cv_quals, declarator, std_attributes);
19532 /* Everything else is a direct-declarator. */
19533 else
19535 if (parenthesized_p)
19536 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19537 CPP_OPEN_PAREN);
19538 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19539 ctor_dtor_or_conv_p,
19540 member_p, friend_p);
19543 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19544 declarator->attributes = gnu_attributes;
19545 return declarator;
19548 /* Parse a direct-declarator or direct-abstract-declarator.
19550 direct-declarator:
19551 declarator-id
19552 direct-declarator ( parameter-declaration-clause )
19553 cv-qualifier-seq [opt]
19554 ref-qualifier [opt]
19555 exception-specification [opt]
19556 direct-declarator [ constant-expression [opt] ]
19557 ( declarator )
19559 direct-abstract-declarator:
19560 direct-abstract-declarator [opt]
19561 ( parameter-declaration-clause )
19562 cv-qualifier-seq [opt]
19563 ref-qualifier [opt]
19564 exception-specification [opt]
19565 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19566 ( abstract-declarator )
19568 Returns a representation of the declarator. DCL_KIND is
19569 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19570 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19571 we are parsing a direct-declarator. It is
19572 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19573 of ambiguity we prefer an abstract declarator, as per
19574 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19575 as for cp_parser_declarator. */
19577 static cp_declarator *
19578 cp_parser_direct_declarator (cp_parser* parser,
19579 cp_parser_declarator_kind dcl_kind,
19580 int* ctor_dtor_or_conv_p,
19581 bool member_p, bool friend_p)
19583 cp_token *token;
19584 cp_declarator *declarator = NULL;
19585 tree scope = NULL_TREE;
19586 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19587 bool saved_in_declarator_p = parser->in_declarator_p;
19588 bool first = true;
19589 tree pushed_scope = NULL_TREE;
19591 while (true)
19593 /* Peek at the next token. */
19594 token = cp_lexer_peek_token (parser->lexer);
19595 if (token->type == CPP_OPEN_PAREN)
19597 /* This is either a parameter-declaration-clause, or a
19598 parenthesized declarator. When we know we are parsing a
19599 named declarator, it must be a parenthesized declarator
19600 if FIRST is true. For instance, `(int)' is a
19601 parameter-declaration-clause, with an omitted
19602 direct-abstract-declarator. But `((*))', is a
19603 parenthesized abstract declarator. Finally, when T is a
19604 template parameter `(T)' is a
19605 parameter-declaration-clause, and not a parenthesized
19606 named declarator.
19608 We first try and parse a parameter-declaration-clause,
19609 and then try a nested declarator (if FIRST is true).
19611 It is not an error for it not to be a
19612 parameter-declaration-clause, even when FIRST is
19613 false. Consider,
19615 int i (int);
19616 int i (3);
19618 The first is the declaration of a function while the
19619 second is the definition of a variable, including its
19620 initializer.
19622 Having seen only the parenthesis, we cannot know which of
19623 these two alternatives should be selected. Even more
19624 complex are examples like:
19626 int i (int (a));
19627 int i (int (3));
19629 The former is a function-declaration; the latter is a
19630 variable initialization.
19632 Thus again, we try a parameter-declaration-clause, and if
19633 that fails, we back out and return. */
19635 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19637 tree params;
19638 bool is_declarator = false;
19640 /* In a member-declarator, the only valid interpretation
19641 of a parenthesis is the start of a
19642 parameter-declaration-clause. (It is invalid to
19643 initialize a static data member with a parenthesized
19644 initializer; only the "=" form of initialization is
19645 permitted.) */
19646 if (!member_p)
19647 cp_parser_parse_tentatively (parser);
19649 /* Consume the `('. */
19650 cp_lexer_consume_token (parser->lexer);
19651 if (first)
19653 /* If this is going to be an abstract declarator, we're
19654 in a declarator and we can't have default args. */
19655 parser->default_arg_ok_p = false;
19656 parser->in_declarator_p = true;
19659 begin_scope (sk_function_parms, NULL_TREE);
19661 /* Parse the parameter-declaration-clause. */
19662 params = cp_parser_parameter_declaration_clause (parser);
19664 /* Consume the `)'. */
19665 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19667 /* If all went well, parse the cv-qualifier-seq,
19668 ref-qualifier and the exception-specification. */
19669 if (member_p || cp_parser_parse_definitely (parser))
19671 cp_cv_quals cv_quals;
19672 cp_virt_specifiers virt_specifiers;
19673 cp_ref_qualifier ref_qual;
19674 tree exception_specification;
19675 tree late_return;
19676 tree attrs;
19677 bool memfn = (member_p || (pushed_scope
19678 && CLASS_TYPE_P (pushed_scope)));
19680 is_declarator = true;
19682 if (ctor_dtor_or_conv_p)
19683 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19684 first = false;
19686 /* Parse the cv-qualifier-seq. */
19687 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19688 /* Parse the ref-qualifier. */
19689 ref_qual = cp_parser_ref_qualifier_opt (parser);
19690 /* Parse the tx-qualifier. */
19691 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19692 /* And the exception-specification. */
19693 exception_specification
19694 = cp_parser_exception_specification_opt (parser);
19696 attrs = cp_parser_std_attribute_spec_seq (parser);
19698 /* In here, we handle cases where attribute is used after
19699 the function declaration. For example:
19700 void func (int x) __attribute__((vector(..))); */
19701 tree gnu_attrs = NULL_TREE;
19702 if (flag_cilkplus
19703 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19705 cp_parser_parse_tentatively (parser);
19706 tree attr = cp_parser_gnu_attributes_opt (parser);
19707 if (cp_lexer_next_token_is_not (parser->lexer,
19708 CPP_SEMICOLON)
19709 && cp_lexer_next_token_is_not (parser->lexer,
19710 CPP_OPEN_BRACE))
19711 cp_parser_abort_tentative_parse (parser);
19712 else if (!cp_parser_parse_definitely (parser))
19714 else
19715 gnu_attrs = attr;
19717 tree requires_clause = NULL_TREE;
19718 late_return = (cp_parser_late_return_type_opt
19719 (parser, declarator, requires_clause,
19720 memfn ? cv_quals : -1));
19722 /* Parse the virt-specifier-seq. */
19723 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19725 /* Create the function-declarator. */
19726 declarator = make_call_declarator (declarator,
19727 params,
19728 cv_quals,
19729 virt_specifiers,
19730 ref_qual,
19731 tx_qual,
19732 exception_specification,
19733 late_return,
19734 requires_clause);
19735 declarator->std_attributes = attrs;
19736 declarator->attributes = gnu_attrs;
19737 /* Any subsequent parameter lists are to do with
19738 return type, so are not those of the declared
19739 function. */
19740 parser->default_arg_ok_p = false;
19743 /* Remove the function parms from scope. */
19744 pop_bindings_and_leave_scope ();
19746 if (is_declarator)
19747 /* Repeat the main loop. */
19748 continue;
19751 /* If this is the first, we can try a parenthesized
19752 declarator. */
19753 if (first)
19755 bool saved_in_type_id_in_expr_p;
19757 parser->default_arg_ok_p = saved_default_arg_ok_p;
19758 parser->in_declarator_p = saved_in_declarator_p;
19760 /* Consume the `('. */
19761 cp_lexer_consume_token (parser->lexer);
19762 /* Parse the nested declarator. */
19763 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19764 parser->in_type_id_in_expr_p = true;
19765 declarator
19766 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19767 /*parenthesized_p=*/NULL,
19768 member_p, friend_p);
19769 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19770 first = false;
19771 /* Expect a `)'. */
19772 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19773 declarator = cp_error_declarator;
19774 if (declarator == cp_error_declarator)
19775 break;
19777 goto handle_declarator;
19779 /* Otherwise, we must be done. */
19780 else
19781 break;
19783 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19784 && token->type == CPP_OPEN_SQUARE
19785 && !cp_next_tokens_can_be_attribute_p (parser))
19787 /* Parse an array-declarator. */
19788 tree bounds, attrs;
19790 if (ctor_dtor_or_conv_p)
19791 *ctor_dtor_or_conv_p = 0;
19793 first = false;
19794 parser->default_arg_ok_p = false;
19795 parser->in_declarator_p = true;
19796 /* Consume the `['. */
19797 cp_lexer_consume_token (parser->lexer);
19798 /* Peek at the next token. */
19799 token = cp_lexer_peek_token (parser->lexer);
19800 /* If the next token is `]', then there is no
19801 constant-expression. */
19802 if (token->type != CPP_CLOSE_SQUARE)
19804 bool non_constant_p;
19805 bounds
19806 = cp_parser_constant_expression (parser,
19807 /*allow_non_constant=*/true,
19808 &non_constant_p);
19809 if (!non_constant_p)
19810 /* OK */;
19811 else if (error_operand_p (bounds))
19812 /* Already gave an error. */;
19813 else if (!parser->in_function_body
19814 || current_binding_level->kind == sk_function_parms)
19816 /* Normally, the array bound must be an integral constant
19817 expression. However, as an extension, we allow VLAs
19818 in function scopes as long as they aren't part of a
19819 parameter declaration. */
19820 cp_parser_error (parser,
19821 "array bound is not an integer constant");
19822 bounds = error_mark_node;
19824 else if (processing_template_decl
19825 && !type_dependent_expression_p (bounds))
19827 /* Remember this wasn't a constant-expression. */
19828 bounds = build_nop (TREE_TYPE (bounds), bounds);
19829 TREE_SIDE_EFFECTS (bounds) = 1;
19832 else
19833 bounds = NULL_TREE;
19834 /* Look for the closing `]'. */
19835 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19837 declarator = cp_error_declarator;
19838 break;
19841 attrs = cp_parser_std_attribute_spec_seq (parser);
19842 declarator = make_array_declarator (declarator, bounds);
19843 declarator->std_attributes = attrs;
19845 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19848 tree qualifying_scope;
19849 tree unqualified_name;
19850 tree attrs;
19851 special_function_kind sfk;
19852 bool abstract_ok;
19853 bool pack_expansion_p = false;
19854 cp_token *declarator_id_start_token;
19856 /* Parse a declarator-id */
19857 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19858 if (abstract_ok)
19860 cp_parser_parse_tentatively (parser);
19862 /* If we see an ellipsis, we should be looking at a
19863 parameter pack. */
19864 if (token->type == CPP_ELLIPSIS)
19866 /* Consume the `...' */
19867 cp_lexer_consume_token (parser->lexer);
19869 pack_expansion_p = true;
19873 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19874 unqualified_name
19875 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19876 qualifying_scope = parser->scope;
19877 if (abstract_ok)
19879 bool okay = false;
19881 if (!unqualified_name && pack_expansion_p)
19883 /* Check whether an error occurred. */
19884 okay = !cp_parser_error_occurred (parser);
19886 /* We already consumed the ellipsis to mark a
19887 parameter pack, but we have no way to report it,
19888 so abort the tentative parse. We will be exiting
19889 immediately anyway. */
19890 cp_parser_abort_tentative_parse (parser);
19892 else
19893 okay = cp_parser_parse_definitely (parser);
19895 if (!okay)
19896 unqualified_name = error_mark_node;
19897 else if (unqualified_name
19898 && (qualifying_scope
19899 || (!identifier_p (unqualified_name))))
19901 cp_parser_error (parser, "expected unqualified-id");
19902 unqualified_name = error_mark_node;
19906 if (!unqualified_name)
19907 return NULL;
19908 if (unqualified_name == error_mark_node)
19910 declarator = cp_error_declarator;
19911 pack_expansion_p = false;
19912 declarator->parameter_pack_p = false;
19913 break;
19916 attrs = cp_parser_std_attribute_spec_seq (parser);
19918 if (qualifying_scope && at_namespace_scope_p ()
19919 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19921 /* In the declaration of a member of a template class
19922 outside of the class itself, the SCOPE will sometimes
19923 be a TYPENAME_TYPE. For example, given:
19925 template <typename T>
19926 int S<T>::R::i = 3;
19928 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19929 this context, we must resolve S<T>::R to an ordinary
19930 type, rather than a typename type.
19932 The reason we normally avoid resolving TYPENAME_TYPEs
19933 is that a specialization of `S' might render
19934 `S<T>::R' not a type. However, if `S' is
19935 specialized, then this `i' will not be used, so there
19936 is no harm in resolving the types here. */
19937 tree type;
19939 /* Resolve the TYPENAME_TYPE. */
19940 type = resolve_typename_type (qualifying_scope,
19941 /*only_current_p=*/false);
19942 /* If that failed, the declarator is invalid. */
19943 if (TREE_CODE (type) == TYPENAME_TYPE)
19945 if (typedef_variant_p (type))
19946 error_at (declarator_id_start_token->location,
19947 "cannot define member of dependent typedef "
19948 "%qT", type);
19949 else
19950 error_at (declarator_id_start_token->location,
19951 "%<%T::%E%> is not a type",
19952 TYPE_CONTEXT (qualifying_scope),
19953 TYPE_IDENTIFIER (qualifying_scope));
19955 qualifying_scope = type;
19958 sfk = sfk_none;
19960 if (unqualified_name)
19962 tree class_type;
19964 if (qualifying_scope
19965 && CLASS_TYPE_P (qualifying_scope))
19966 class_type = qualifying_scope;
19967 else
19968 class_type = current_class_type;
19970 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19972 tree name_type = TREE_TYPE (unqualified_name);
19973 if (class_type && same_type_p (name_type, class_type))
19975 if (qualifying_scope
19976 && CLASSTYPE_USE_TEMPLATE (name_type))
19978 error_at (declarator_id_start_token->location,
19979 "invalid use of constructor as a template");
19980 inform (declarator_id_start_token->location,
19981 "use %<%T::%D%> instead of %<%T::%D%> to "
19982 "name the constructor in a qualified name",
19983 class_type,
19984 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19985 class_type, name_type);
19986 declarator = cp_error_declarator;
19987 break;
19989 else
19990 unqualified_name = constructor_name (class_type);
19992 else
19994 /* We do not attempt to print the declarator
19995 here because we do not have enough
19996 information about its original syntactic
19997 form. */
19998 cp_parser_error (parser, "invalid declarator");
19999 declarator = cp_error_declarator;
20000 break;
20004 if (class_type)
20006 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20007 sfk = sfk_destructor;
20008 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
20009 sfk = sfk_conversion;
20010 else if (/* There's no way to declare a constructor
20011 for an unnamed type, even if the type
20012 got a name for linkage purposes. */
20013 !TYPE_WAS_UNNAMED (class_type)
20014 /* Handle correctly (c++/19200):
20016 struct S {
20017 struct T{};
20018 friend void S(T);
20021 and also:
20023 namespace N {
20024 void S();
20027 struct S {
20028 friend void N::S();
20029 }; */
20030 && !(friend_p
20031 && class_type != qualifying_scope)
20032 && constructor_name_p (unqualified_name,
20033 class_type))
20035 unqualified_name = constructor_name (class_type);
20036 sfk = sfk_constructor;
20038 else if (is_overloaded_fn (unqualified_name)
20039 && DECL_CONSTRUCTOR_P (get_first_fn
20040 (unqualified_name)))
20041 sfk = sfk_constructor;
20043 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20044 *ctor_dtor_or_conv_p = -1;
20047 declarator = make_id_declarator (qualifying_scope,
20048 unqualified_name,
20049 sfk);
20050 declarator->std_attributes = attrs;
20051 declarator->id_loc = token->location;
20052 declarator->parameter_pack_p = pack_expansion_p;
20054 if (pack_expansion_p)
20055 maybe_warn_variadic_templates ();
20058 handle_declarator:;
20059 scope = get_scope_of_declarator (declarator);
20060 if (scope)
20062 /* Any names that appear after the declarator-id for a
20063 member are looked up in the containing scope. */
20064 if (at_function_scope_p ())
20066 /* But declarations with qualified-ids can't appear in a
20067 function. */
20068 cp_parser_error (parser, "qualified-id in declaration");
20069 declarator = cp_error_declarator;
20070 break;
20072 pushed_scope = push_scope (scope);
20074 parser->in_declarator_p = true;
20075 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20076 || (declarator && declarator->kind == cdk_id))
20077 /* Default args are only allowed on function
20078 declarations. */
20079 parser->default_arg_ok_p = saved_default_arg_ok_p;
20080 else
20081 parser->default_arg_ok_p = false;
20083 first = false;
20085 /* We're done. */
20086 else
20087 break;
20090 /* For an abstract declarator, we might wind up with nothing at this
20091 point. That's an error; the declarator is not optional. */
20092 if (!declarator)
20093 cp_parser_error (parser, "expected declarator");
20095 /* If we entered a scope, we must exit it now. */
20096 if (pushed_scope)
20097 pop_scope (pushed_scope);
20099 parser->default_arg_ok_p = saved_default_arg_ok_p;
20100 parser->in_declarator_p = saved_in_declarator_p;
20102 return declarator;
20105 /* Parse a ptr-operator.
20107 ptr-operator:
20108 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20109 * cv-qualifier-seq [opt]
20111 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20112 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20114 GNU Extension:
20116 ptr-operator:
20117 & cv-qualifier-seq [opt]
20119 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20120 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20121 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20122 filled in with the TYPE containing the member. *CV_QUALS is
20123 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20124 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20125 Note that the tree codes returned by this function have nothing
20126 to do with the types of trees that will be eventually be created
20127 to represent the pointer or reference type being parsed. They are
20128 just constants with suggestive names. */
20129 static enum tree_code
20130 cp_parser_ptr_operator (cp_parser* parser,
20131 tree* type,
20132 cp_cv_quals *cv_quals,
20133 tree *attributes)
20135 enum tree_code code = ERROR_MARK;
20136 cp_token *token;
20137 tree attrs = NULL_TREE;
20139 /* Assume that it's not a pointer-to-member. */
20140 *type = NULL_TREE;
20141 /* And that there are no cv-qualifiers. */
20142 *cv_quals = TYPE_UNQUALIFIED;
20144 /* Peek at the next token. */
20145 token = cp_lexer_peek_token (parser->lexer);
20147 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20148 if (token->type == CPP_MULT)
20149 code = INDIRECT_REF;
20150 else if (token->type == CPP_AND)
20151 code = ADDR_EXPR;
20152 else if ((cxx_dialect != cxx98) &&
20153 token->type == CPP_AND_AND) /* C++0x only */
20154 code = NON_LVALUE_EXPR;
20156 if (code != ERROR_MARK)
20158 /* Consume the `*', `&' or `&&'. */
20159 cp_lexer_consume_token (parser->lexer);
20161 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20162 `&', if we are allowing GNU extensions. (The only qualifier
20163 that can legally appear after `&' is `restrict', but that is
20164 enforced during semantic analysis. */
20165 if (code == INDIRECT_REF
20166 || cp_parser_allow_gnu_extensions_p (parser))
20167 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20169 attrs = cp_parser_std_attribute_spec_seq (parser);
20170 if (attributes != NULL)
20171 *attributes = attrs;
20173 else
20175 /* Try the pointer-to-member case. */
20176 cp_parser_parse_tentatively (parser);
20177 /* Look for the optional `::' operator. */
20178 cp_parser_global_scope_opt (parser,
20179 /*current_scope_valid_p=*/false);
20180 /* Look for the nested-name specifier. */
20181 token = cp_lexer_peek_token (parser->lexer);
20182 cp_parser_nested_name_specifier (parser,
20183 /*typename_keyword_p=*/false,
20184 /*check_dependency_p=*/true,
20185 /*type_p=*/false,
20186 /*is_declaration=*/false);
20187 /* If we found it, and the next token is a `*', then we are
20188 indeed looking at a pointer-to-member operator. */
20189 if (!cp_parser_error_occurred (parser)
20190 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20192 /* Indicate that the `*' operator was used. */
20193 code = INDIRECT_REF;
20195 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20196 error_at (token->location, "%qD is a namespace", parser->scope);
20197 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20198 error_at (token->location, "cannot form pointer to member of "
20199 "non-class %q#T", parser->scope);
20200 else
20202 /* The type of which the member is a member is given by the
20203 current SCOPE. */
20204 *type = parser->scope;
20205 /* The next name will not be qualified. */
20206 parser->scope = NULL_TREE;
20207 parser->qualifying_scope = NULL_TREE;
20208 parser->object_scope = NULL_TREE;
20209 /* Look for optional c++11 attributes. */
20210 attrs = cp_parser_std_attribute_spec_seq (parser);
20211 if (attributes != NULL)
20212 *attributes = attrs;
20213 /* Look for the optional cv-qualifier-seq. */
20214 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20217 /* If that didn't work we don't have a ptr-operator. */
20218 if (!cp_parser_parse_definitely (parser))
20219 cp_parser_error (parser, "expected ptr-operator");
20222 return code;
20225 /* Parse an (optional) cv-qualifier-seq.
20227 cv-qualifier-seq:
20228 cv-qualifier cv-qualifier-seq [opt]
20230 cv-qualifier:
20231 const
20232 volatile
20234 GNU Extension:
20236 cv-qualifier:
20237 __restrict__
20239 Returns a bitmask representing the cv-qualifiers. */
20241 static cp_cv_quals
20242 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20244 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20246 while (true)
20248 cp_token *token;
20249 cp_cv_quals cv_qualifier;
20251 /* Peek at the next token. */
20252 token = cp_lexer_peek_token (parser->lexer);
20253 /* See if it's a cv-qualifier. */
20254 switch (token->keyword)
20256 case RID_CONST:
20257 cv_qualifier = TYPE_QUAL_CONST;
20258 break;
20260 case RID_VOLATILE:
20261 cv_qualifier = TYPE_QUAL_VOLATILE;
20262 break;
20264 case RID_RESTRICT:
20265 cv_qualifier = TYPE_QUAL_RESTRICT;
20266 break;
20268 default:
20269 cv_qualifier = TYPE_UNQUALIFIED;
20270 break;
20273 if (!cv_qualifier)
20274 break;
20276 if (cv_quals & cv_qualifier)
20278 gcc_rich_location richloc (token->location);
20279 richloc.add_fixit_remove ();
20280 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20281 cp_lexer_purge_token (parser->lexer);
20283 else
20285 cp_lexer_consume_token (parser->lexer);
20286 cv_quals |= cv_qualifier;
20290 return cv_quals;
20293 /* Parse an (optional) ref-qualifier
20295 ref-qualifier:
20299 Returns cp_ref_qualifier representing ref-qualifier. */
20301 static cp_ref_qualifier
20302 cp_parser_ref_qualifier_opt (cp_parser* parser)
20304 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20306 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20307 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20308 return ref_qual;
20310 while (true)
20312 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20313 cp_token *token = cp_lexer_peek_token (parser->lexer);
20315 switch (token->type)
20317 case CPP_AND:
20318 curr_ref_qual = REF_QUAL_LVALUE;
20319 break;
20321 case CPP_AND_AND:
20322 curr_ref_qual = REF_QUAL_RVALUE;
20323 break;
20325 default:
20326 curr_ref_qual = REF_QUAL_NONE;
20327 break;
20330 if (!curr_ref_qual)
20331 break;
20332 else if (ref_qual)
20334 error_at (token->location, "multiple ref-qualifiers");
20335 cp_lexer_purge_token (parser->lexer);
20337 else
20339 ref_qual = curr_ref_qual;
20340 cp_lexer_consume_token (parser->lexer);
20344 return ref_qual;
20347 /* Parse an optional tx-qualifier.
20349 tx-qualifier:
20350 transaction_safe
20351 transaction_safe_dynamic */
20353 static tree
20354 cp_parser_tx_qualifier_opt (cp_parser *parser)
20356 cp_token *token = cp_lexer_peek_token (parser->lexer);
20357 if (token->type == CPP_NAME)
20359 tree name = token->u.value;
20360 const char *p = IDENTIFIER_POINTER (name);
20361 const int len = strlen ("transaction_safe");
20362 if (!strncmp (p, "transaction_safe", len))
20364 p += len;
20365 if (*p == '\0'
20366 || !strcmp (p, "_dynamic"))
20368 cp_lexer_consume_token (parser->lexer);
20369 if (!flag_tm)
20371 error ("%qE requires %<-fgnu-tm%>", name);
20372 return NULL_TREE;
20374 else
20375 return name;
20379 return NULL_TREE;
20382 /* Parse an (optional) virt-specifier-seq.
20384 virt-specifier-seq:
20385 virt-specifier virt-specifier-seq [opt]
20387 virt-specifier:
20388 override
20389 final
20391 Returns a bitmask representing the virt-specifiers. */
20393 static cp_virt_specifiers
20394 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20396 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20398 while (true)
20400 cp_token *token;
20401 cp_virt_specifiers virt_specifier;
20403 /* Peek at the next token. */
20404 token = cp_lexer_peek_token (parser->lexer);
20405 /* See if it's a virt-specifier-qualifier. */
20406 if (token->type != CPP_NAME)
20407 break;
20408 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20410 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20411 virt_specifier = VIRT_SPEC_OVERRIDE;
20413 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20415 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20416 virt_specifier = VIRT_SPEC_FINAL;
20418 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20420 virt_specifier = VIRT_SPEC_FINAL;
20422 else
20423 break;
20425 if (virt_specifiers & virt_specifier)
20427 gcc_rich_location richloc (token->location);
20428 richloc.add_fixit_remove ();
20429 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20430 cp_lexer_purge_token (parser->lexer);
20432 else
20434 cp_lexer_consume_token (parser->lexer);
20435 virt_specifiers |= virt_specifier;
20438 return virt_specifiers;
20441 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20442 is in scope even though it isn't real. */
20444 void
20445 inject_this_parameter (tree ctype, cp_cv_quals quals)
20447 tree this_parm;
20449 if (current_class_ptr)
20451 /* We don't clear this between NSDMIs. Is it already what we want? */
20452 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20453 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20454 && cp_type_quals (type) == quals)
20455 return;
20458 this_parm = build_this_parm (ctype, quals);
20459 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20460 current_class_ptr = NULL_TREE;
20461 current_class_ref
20462 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20463 current_class_ptr = this_parm;
20466 /* Return true iff our current scope is a non-static data member
20467 initializer. */
20469 bool
20470 parsing_nsdmi (void)
20472 /* We recognize NSDMI context by the context-less 'this' pointer set up
20473 by the function above. */
20474 if (current_class_ptr
20475 && TREE_CODE (current_class_ptr) == PARM_DECL
20476 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20477 return true;
20478 return false;
20481 /* Return true iff our current scope is a default capturing generic lambda
20482 defined within a template. FIXME: This is part of a workaround (see
20483 semantics.c) to handle building lambda closure types correctly in templates
20484 which we ultimately want to defer to instantiation time. */
20486 bool
20487 parsing_default_capturing_generic_lambda_in_template (void)
20489 if (!processing_template_decl || !current_class_type)
20490 return false;
20492 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20493 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20494 return false;
20496 tree callop = lambda_function (lam);
20497 if (!callop)
20498 return false;
20500 return (DECL_TEMPLATE_INFO (callop)
20501 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20502 && ((current_nonlambda_class_type ()
20503 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20504 || ((current_nonlambda_function ()
20505 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20508 /* Parse a late-specified return type, if any. This is not a separate
20509 non-terminal, but part of a function declarator, which looks like
20511 -> trailing-type-specifier-seq abstract-declarator(opt)
20513 Returns the type indicated by the type-id.
20515 In addition to this, parse any queued up #pragma omp declare simd
20516 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20517 #pragma acc routine clauses.
20519 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20520 function. */
20522 static tree
20523 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20524 tree& requires_clause, cp_cv_quals quals)
20526 cp_token *token;
20527 tree type = NULL_TREE;
20528 bool declare_simd_p = (parser->omp_declare_simd
20529 && declarator
20530 && declarator->kind == cdk_id);
20532 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20533 && declarator && declarator->kind == cdk_id);
20535 bool oacc_routine_p = (parser->oacc_routine
20536 && declarator
20537 && declarator->kind == cdk_id);
20539 /* Peek at the next token. */
20540 token = cp_lexer_peek_token (parser->lexer);
20541 /* A late-specified return type is indicated by an initial '->'. */
20542 if (token->type != CPP_DEREF
20543 && token->keyword != RID_REQUIRES
20544 && !(token->type == CPP_NAME
20545 && token->u.value == ridpointers[RID_REQUIRES])
20546 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20547 return NULL_TREE;
20549 tree save_ccp = current_class_ptr;
20550 tree save_ccr = current_class_ref;
20551 if (quals >= 0)
20553 /* DR 1207: 'this' is in scope in the trailing return type. */
20554 inject_this_parameter (current_class_type, quals);
20557 if (token->type == CPP_DEREF)
20559 /* Consume the ->. */
20560 cp_lexer_consume_token (parser->lexer);
20562 type = cp_parser_trailing_type_id (parser);
20565 /* Function declarations may be followed by a trailing
20566 requires-clause. */
20567 requires_clause = cp_parser_requires_clause_opt (parser);
20569 if (cilk_simd_fn_vector_p)
20570 declarator->attributes
20571 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20572 declarator->attributes);
20573 if (declare_simd_p)
20574 declarator->attributes
20575 = cp_parser_late_parsing_omp_declare_simd (parser,
20576 declarator->attributes);
20577 if (oacc_routine_p)
20578 declarator->attributes
20579 = cp_parser_late_parsing_oacc_routine (parser,
20580 declarator->attributes);
20582 if (quals >= 0)
20584 current_class_ptr = save_ccp;
20585 current_class_ref = save_ccr;
20588 return type;
20591 /* Parse a declarator-id.
20593 declarator-id:
20594 id-expression
20595 :: [opt] nested-name-specifier [opt] type-name
20597 In the `id-expression' case, the value returned is as for
20598 cp_parser_id_expression if the id-expression was an unqualified-id.
20599 If the id-expression was a qualified-id, then a SCOPE_REF is
20600 returned. The first operand is the scope (either a NAMESPACE_DECL
20601 or TREE_TYPE), but the second is still just a representation of an
20602 unqualified-id. */
20604 static tree
20605 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20607 tree id;
20608 /* The expression must be an id-expression. Assume that qualified
20609 names are the names of types so that:
20611 template <class T>
20612 int S<T>::R::i = 3;
20614 will work; we must treat `S<T>::R' as the name of a type.
20615 Similarly, assume that qualified names are templates, where
20616 required, so that:
20618 template <class T>
20619 int S<T>::R<T>::i = 3;
20621 will work, too. */
20622 id = cp_parser_id_expression (parser,
20623 /*template_keyword_p=*/false,
20624 /*check_dependency_p=*/false,
20625 /*template_p=*/NULL,
20626 /*declarator_p=*/true,
20627 optional_p);
20628 if (id && BASELINK_P (id))
20629 id = BASELINK_FUNCTIONS (id);
20630 return id;
20633 /* Parse a type-id.
20635 type-id:
20636 type-specifier-seq abstract-declarator [opt]
20638 Returns the TYPE specified. */
20640 static tree
20641 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20642 bool is_trailing_return)
20644 cp_decl_specifier_seq type_specifier_seq;
20645 cp_declarator *abstract_declarator;
20647 /* Parse the type-specifier-seq. */
20648 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20649 is_trailing_return,
20650 &type_specifier_seq);
20651 if (is_template_arg && type_specifier_seq.type
20652 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20653 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20654 /* A bare template name as a template argument is a template template
20655 argument, not a placeholder, so fail parsing it as a type argument. */
20657 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20658 cp_parser_simulate_error (parser);
20659 return error_mark_node;
20661 if (type_specifier_seq.type == error_mark_node)
20662 return error_mark_node;
20664 /* There might or might not be an abstract declarator. */
20665 cp_parser_parse_tentatively (parser);
20666 /* Look for the declarator. */
20667 abstract_declarator
20668 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20669 /*parenthesized_p=*/NULL,
20670 /*member_p=*/false,
20671 /*friend_p=*/false);
20672 /* Check to see if there really was a declarator. */
20673 if (!cp_parser_parse_definitely (parser))
20674 abstract_declarator = NULL;
20676 if (type_specifier_seq.type
20677 /* The concepts TS allows 'auto' as a type-id. */
20678 && (!flag_concepts || parser->in_type_id_in_expr_p)
20679 /* None of the valid uses of 'auto' in C++14 involve the type-id
20680 nonterminal, but it is valid in a trailing-return-type. */
20681 && !(cxx_dialect >= cxx14 && is_trailing_return))
20682 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20684 /* A type-id with type 'auto' is only ok if the abstract declarator
20685 is a function declarator with a late-specified return type.
20687 A type-id with 'auto' is also valid in a trailing-return-type
20688 in a compound-requirement. */
20689 if (abstract_declarator
20690 && abstract_declarator->kind == cdk_function
20691 && abstract_declarator->u.function.late_return_type)
20692 /* OK */;
20693 else if (parser->in_result_type_constraint_p)
20694 /* OK */;
20695 else
20697 location_t loc = type_specifier_seq.locations[ds_type_spec];
20698 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20700 error_at (loc, "missing template arguments after %qT",
20701 auto_node);
20702 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20703 tmpl);
20705 else
20706 error_at (loc, "invalid use of %qT", auto_node);
20707 return error_mark_node;
20711 return groktypename (&type_specifier_seq, abstract_declarator,
20712 is_template_arg);
20715 static tree
20716 cp_parser_type_id (cp_parser *parser)
20718 return cp_parser_type_id_1 (parser, false, false);
20721 static tree
20722 cp_parser_template_type_arg (cp_parser *parser)
20724 tree r;
20725 const char *saved_message = parser->type_definition_forbidden_message;
20726 parser->type_definition_forbidden_message
20727 = G_("types may not be defined in template arguments");
20728 r = cp_parser_type_id_1 (parser, true, false);
20729 parser->type_definition_forbidden_message = saved_message;
20730 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20732 error ("invalid use of %<auto%> in template argument");
20733 r = error_mark_node;
20735 return r;
20738 static tree
20739 cp_parser_trailing_type_id (cp_parser *parser)
20741 return cp_parser_type_id_1 (parser, false, true);
20744 /* Parse a type-specifier-seq.
20746 type-specifier-seq:
20747 type-specifier type-specifier-seq [opt]
20749 GNU extension:
20751 type-specifier-seq:
20752 attributes type-specifier-seq [opt]
20754 If IS_DECLARATION is true, we are at the start of a "condition" or
20755 exception-declaration, so we might be followed by a declarator-id.
20757 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20758 i.e. we've just seen "->".
20760 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20762 static void
20763 cp_parser_type_specifier_seq (cp_parser* parser,
20764 bool is_declaration,
20765 bool is_trailing_return,
20766 cp_decl_specifier_seq *type_specifier_seq)
20768 bool seen_type_specifier = false;
20769 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20770 cp_token *start_token = NULL;
20772 /* Clear the TYPE_SPECIFIER_SEQ. */
20773 clear_decl_specs (type_specifier_seq);
20775 /* In the context of a trailing return type, enum E { } is an
20776 elaborated-type-specifier followed by a function-body, not an
20777 enum-specifier. */
20778 if (is_trailing_return)
20779 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20781 /* Parse the type-specifiers and attributes. */
20782 while (true)
20784 tree type_specifier;
20785 bool is_cv_qualifier;
20787 /* Check for attributes first. */
20788 if (cp_next_tokens_can_be_attribute_p (parser))
20790 type_specifier_seq->attributes =
20791 chainon (type_specifier_seq->attributes,
20792 cp_parser_attributes_opt (parser));
20793 continue;
20796 /* record the token of the beginning of the type specifier seq,
20797 for error reporting purposes*/
20798 if (!start_token)
20799 start_token = cp_lexer_peek_token (parser->lexer);
20801 /* Look for the type-specifier. */
20802 type_specifier = cp_parser_type_specifier (parser,
20803 flags,
20804 type_specifier_seq,
20805 /*is_declaration=*/false,
20806 NULL,
20807 &is_cv_qualifier);
20808 if (!type_specifier)
20810 /* If the first type-specifier could not be found, this is not a
20811 type-specifier-seq at all. */
20812 if (!seen_type_specifier)
20814 /* Set in_declarator_p to avoid skipping to the semicolon. */
20815 int in_decl = parser->in_declarator_p;
20816 parser->in_declarator_p = true;
20818 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20819 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20820 cp_parser_error (parser, "expected type-specifier");
20822 parser->in_declarator_p = in_decl;
20824 type_specifier_seq->type = error_mark_node;
20825 return;
20827 /* If subsequent type-specifiers could not be found, the
20828 type-specifier-seq is complete. */
20829 break;
20832 seen_type_specifier = true;
20833 /* The standard says that a condition can be:
20835 type-specifier-seq declarator = assignment-expression
20837 However, given:
20839 struct S {};
20840 if (int S = ...)
20842 we should treat the "S" as a declarator, not as a
20843 type-specifier. The standard doesn't say that explicitly for
20844 type-specifier-seq, but it does say that for
20845 decl-specifier-seq in an ordinary declaration. Perhaps it
20846 would be clearer just to allow a decl-specifier-seq here, and
20847 then add a semantic restriction that if any decl-specifiers
20848 that are not type-specifiers appear, the program is invalid. */
20849 if (is_declaration && !is_cv_qualifier)
20850 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20854 /* Return whether the function currently being declared has an associated
20855 template parameter list. */
20857 static bool
20858 function_being_declared_is_template_p (cp_parser* parser)
20860 if (!current_template_parms || processing_template_parmlist)
20861 return false;
20863 if (parser->implicit_template_scope)
20864 return true;
20866 if (at_class_scope_p ()
20867 && TYPE_BEING_DEFINED (current_class_type))
20868 return parser->num_template_parameter_lists != 0;
20870 return ((int) parser->num_template_parameter_lists > template_class_depth
20871 (current_class_type));
20874 /* Parse a parameter-declaration-clause.
20876 parameter-declaration-clause:
20877 parameter-declaration-list [opt] ... [opt]
20878 parameter-declaration-list , ...
20880 Returns a representation for the parameter declarations. A return
20881 value of NULL indicates a parameter-declaration-clause consisting
20882 only of an ellipsis. */
20884 static tree
20885 cp_parser_parameter_declaration_clause (cp_parser* parser)
20887 tree parameters;
20888 cp_token *token;
20889 bool ellipsis_p;
20890 bool is_error;
20892 struct cleanup {
20893 cp_parser* parser;
20894 int auto_is_implicit_function_template_parm_p;
20895 ~cleanup() {
20896 parser->auto_is_implicit_function_template_parm_p
20897 = auto_is_implicit_function_template_parm_p;
20899 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20901 (void) cleanup;
20903 if (!processing_specialization
20904 && !processing_template_parmlist
20905 && !processing_explicit_instantiation)
20906 if (!current_function_decl
20907 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20908 parser->auto_is_implicit_function_template_parm_p = true;
20910 /* Peek at the next token. */
20911 token = cp_lexer_peek_token (parser->lexer);
20912 /* Check for trivial parameter-declaration-clauses. */
20913 if (token->type == CPP_ELLIPSIS)
20915 /* Consume the `...' token. */
20916 cp_lexer_consume_token (parser->lexer);
20917 return NULL_TREE;
20919 else if (token->type == CPP_CLOSE_PAREN)
20920 /* There are no parameters. */
20922 #ifndef NO_IMPLICIT_EXTERN_C
20923 if (in_system_header_at (input_location)
20924 && current_class_type == NULL
20925 && current_lang_name == lang_name_c)
20926 return NULL_TREE;
20927 else
20928 #endif
20929 return void_list_node;
20931 /* Check for `(void)', too, which is a special case. */
20932 else if (token->keyword == RID_VOID
20933 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20934 == CPP_CLOSE_PAREN))
20936 /* Consume the `void' token. */
20937 cp_lexer_consume_token (parser->lexer);
20938 /* There are no parameters. */
20939 return void_list_node;
20942 /* Parse the parameter-declaration-list. */
20943 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20944 /* If a parse error occurred while parsing the
20945 parameter-declaration-list, then the entire
20946 parameter-declaration-clause is erroneous. */
20947 if (is_error)
20948 return NULL;
20950 /* Peek at the next token. */
20951 token = cp_lexer_peek_token (parser->lexer);
20952 /* If it's a `,', the clause should terminate with an ellipsis. */
20953 if (token->type == CPP_COMMA)
20955 /* Consume the `,'. */
20956 cp_lexer_consume_token (parser->lexer);
20957 /* Expect an ellipsis. */
20958 ellipsis_p
20959 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20961 /* It might also be `...' if the optional trailing `,' was
20962 omitted. */
20963 else if (token->type == CPP_ELLIPSIS)
20965 /* Consume the `...' token. */
20966 cp_lexer_consume_token (parser->lexer);
20967 /* And remember that we saw it. */
20968 ellipsis_p = true;
20970 else
20971 ellipsis_p = false;
20973 /* Finish the parameter list. */
20974 if (!ellipsis_p)
20975 parameters = chainon (parameters, void_list_node);
20977 return parameters;
20980 /* Parse a parameter-declaration-list.
20982 parameter-declaration-list:
20983 parameter-declaration
20984 parameter-declaration-list , parameter-declaration
20986 Returns a representation of the parameter-declaration-list, as for
20987 cp_parser_parameter_declaration_clause. However, the
20988 `void_list_node' is never appended to the list. Upon return,
20989 *IS_ERROR will be true iff an error occurred. */
20991 static tree
20992 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20994 tree parameters = NULL_TREE;
20995 tree *tail = &parameters;
20996 bool saved_in_unbraced_linkage_specification_p;
20997 int index = 0;
20999 /* Assume all will go well. */
21000 *is_error = false;
21001 /* The special considerations that apply to a function within an
21002 unbraced linkage specifications do not apply to the parameters
21003 to the function. */
21004 saved_in_unbraced_linkage_specification_p
21005 = parser->in_unbraced_linkage_specification_p;
21006 parser->in_unbraced_linkage_specification_p = false;
21008 /* Look for more parameters. */
21009 while (true)
21011 cp_parameter_declarator *parameter;
21012 tree decl = error_mark_node;
21013 bool parenthesized_p = false;
21014 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21015 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21016 (current_template_parms)) : 0);
21018 /* Parse the parameter. */
21019 parameter
21020 = cp_parser_parameter_declaration (parser,
21021 /*template_parm_p=*/false,
21022 &parenthesized_p);
21024 /* We don't know yet if the enclosing context is deprecated, so wait
21025 and warn in grokparms if appropriate. */
21026 deprecated_state = DEPRECATED_SUPPRESS;
21028 if (parameter)
21030 /* If a function parameter pack was specified and an implicit template
21031 parameter was introduced during cp_parser_parameter_declaration,
21032 change any implicit parameters introduced into packs. */
21033 if (parser->implicit_template_parms
21034 && parameter->declarator
21035 && parameter->declarator->parameter_pack_p)
21037 int latest_template_parm_idx = TREE_VEC_LENGTH
21038 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21040 if (latest_template_parm_idx != template_parm_idx)
21041 parameter->decl_specifiers.type = convert_generic_types_to_packs
21042 (parameter->decl_specifiers.type,
21043 template_parm_idx, latest_template_parm_idx);
21046 decl = grokdeclarator (parameter->declarator,
21047 &parameter->decl_specifiers,
21048 PARM,
21049 parameter->default_argument != NULL_TREE,
21050 &parameter->decl_specifiers.attributes);
21053 deprecated_state = DEPRECATED_NORMAL;
21055 /* If a parse error occurred parsing the parameter declaration,
21056 then the entire parameter-declaration-list is erroneous. */
21057 if (decl == error_mark_node)
21059 *is_error = true;
21060 parameters = error_mark_node;
21061 break;
21064 if (parameter->decl_specifiers.attributes)
21065 cplus_decl_attributes (&decl,
21066 parameter->decl_specifiers.attributes,
21068 if (DECL_NAME (decl))
21069 decl = pushdecl (decl);
21071 if (decl != error_mark_node)
21073 retrofit_lang_decl (decl);
21074 DECL_PARM_INDEX (decl) = ++index;
21075 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21078 /* Add the new parameter to the list. */
21079 *tail = build_tree_list (parameter->default_argument, decl);
21080 tail = &TREE_CHAIN (*tail);
21082 /* Peek at the next token. */
21083 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21084 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21085 /* These are for Objective-C++ */
21086 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21087 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21088 /* The parameter-declaration-list is complete. */
21089 break;
21090 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21092 cp_token *token;
21094 /* Peek at the next token. */
21095 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21096 /* If it's an ellipsis, then the list is complete. */
21097 if (token->type == CPP_ELLIPSIS)
21098 break;
21099 /* Otherwise, there must be more parameters. Consume the
21100 `,'. */
21101 cp_lexer_consume_token (parser->lexer);
21102 /* When parsing something like:
21104 int i(float f, double d)
21106 we can tell after seeing the declaration for "f" that we
21107 are not looking at an initialization of a variable "i",
21108 but rather at the declaration of a function "i".
21110 Due to the fact that the parsing of template arguments
21111 (as specified to a template-id) requires backtracking we
21112 cannot use this technique when inside a template argument
21113 list. */
21114 if (!parser->in_template_argument_list_p
21115 && !parser->in_type_id_in_expr_p
21116 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21117 /* However, a parameter-declaration of the form
21118 "float(f)" (which is a valid declaration of a
21119 parameter "f") can also be interpreted as an
21120 expression (the conversion of "f" to "float"). */
21121 && !parenthesized_p)
21122 cp_parser_commit_to_tentative_parse (parser);
21124 else
21126 cp_parser_error (parser, "expected %<,%> or %<...%>");
21127 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21128 cp_parser_skip_to_closing_parenthesis (parser,
21129 /*recovering=*/true,
21130 /*or_comma=*/false,
21131 /*consume_paren=*/false);
21132 break;
21136 parser->in_unbraced_linkage_specification_p
21137 = saved_in_unbraced_linkage_specification_p;
21139 /* Reset implicit_template_scope if we are about to leave the function
21140 parameter list that introduced it. Note that for out-of-line member
21141 definitions, there will be one or more class scopes before we get to
21142 the template parameter scope. */
21144 if (cp_binding_level *its = parser->implicit_template_scope)
21145 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21147 while (maybe_its->kind == sk_class)
21148 maybe_its = maybe_its->level_chain;
21149 if (maybe_its == its)
21151 parser->implicit_template_parms = 0;
21152 parser->implicit_template_scope = 0;
21156 return parameters;
21159 /* Parse a parameter declaration.
21161 parameter-declaration:
21162 decl-specifier-seq ... [opt] declarator
21163 decl-specifier-seq declarator = assignment-expression
21164 decl-specifier-seq ... [opt] abstract-declarator [opt]
21165 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21167 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21168 declares a template parameter. (In that case, a non-nested `>'
21169 token encountered during the parsing of the assignment-expression
21170 is not interpreted as a greater-than operator.)
21172 Returns a representation of the parameter, or NULL if an error
21173 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21174 true iff the declarator is of the form "(p)". */
21176 static cp_parameter_declarator *
21177 cp_parser_parameter_declaration (cp_parser *parser,
21178 bool template_parm_p,
21179 bool *parenthesized_p)
21181 int declares_class_or_enum;
21182 cp_decl_specifier_seq decl_specifiers;
21183 cp_declarator *declarator;
21184 tree default_argument;
21185 cp_token *token = NULL, *declarator_token_start = NULL;
21186 const char *saved_message;
21187 bool template_parameter_pack_p = false;
21189 /* In a template parameter, `>' is not an operator.
21191 [temp.param]
21193 When parsing a default template-argument for a non-type
21194 template-parameter, the first non-nested `>' is taken as the end
21195 of the template parameter-list rather than a greater-than
21196 operator. */
21198 /* Type definitions may not appear in parameter types. */
21199 saved_message = parser->type_definition_forbidden_message;
21200 parser->type_definition_forbidden_message
21201 = G_("types may not be defined in parameter types");
21203 /* Parse the declaration-specifiers. */
21204 cp_parser_decl_specifier_seq (parser,
21205 CP_PARSER_FLAGS_NONE,
21206 &decl_specifiers,
21207 &declares_class_or_enum);
21209 /* Complain about missing 'typename' or other invalid type names. */
21210 if (!decl_specifiers.any_type_specifiers_p
21211 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21212 decl_specifiers.type = error_mark_node;
21214 /* If an error occurred, there's no reason to attempt to parse the
21215 rest of the declaration. */
21216 if (cp_parser_error_occurred (parser))
21218 parser->type_definition_forbidden_message = saved_message;
21219 return NULL;
21222 /* Peek at the next token. */
21223 token = cp_lexer_peek_token (parser->lexer);
21225 /* If the next token is a `)', `,', `=', `>', or `...', then there
21226 is no declarator. However, when variadic templates are enabled,
21227 there may be a declarator following `...'. */
21228 if (token->type == CPP_CLOSE_PAREN
21229 || token->type == CPP_COMMA
21230 || token->type == CPP_EQ
21231 || token->type == CPP_GREATER)
21233 declarator = NULL;
21234 if (parenthesized_p)
21235 *parenthesized_p = false;
21237 /* Otherwise, there should be a declarator. */
21238 else
21240 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21241 parser->default_arg_ok_p = false;
21243 /* After seeing a decl-specifier-seq, if the next token is not a
21244 "(", there is no possibility that the code is a valid
21245 expression. Therefore, if parsing tentatively, we commit at
21246 this point. */
21247 if (!parser->in_template_argument_list_p
21248 /* In an expression context, having seen:
21250 (int((char ...
21252 we cannot be sure whether we are looking at a
21253 function-type (taking a "char" as a parameter) or a cast
21254 of some object of type "char" to "int". */
21255 && !parser->in_type_id_in_expr_p
21256 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21257 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21258 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21259 cp_parser_commit_to_tentative_parse (parser);
21260 /* Parse the declarator. */
21261 declarator_token_start = token;
21262 declarator = cp_parser_declarator (parser,
21263 CP_PARSER_DECLARATOR_EITHER,
21264 /*ctor_dtor_or_conv_p=*/NULL,
21265 parenthesized_p,
21266 /*member_p=*/false,
21267 /*friend_p=*/false);
21268 parser->default_arg_ok_p = saved_default_arg_ok_p;
21269 /* After the declarator, allow more attributes. */
21270 decl_specifiers.attributes
21271 = chainon (decl_specifiers.attributes,
21272 cp_parser_attributes_opt (parser));
21274 /* If the declarator is a template parameter pack, remember that and
21275 clear the flag in the declarator itself so we don't get errors
21276 from grokdeclarator. */
21277 if (template_parm_p && declarator && declarator->parameter_pack_p)
21279 declarator->parameter_pack_p = false;
21280 template_parameter_pack_p = true;
21284 /* If the next token is an ellipsis, and we have not seen a declarator
21285 name, and if either the type of the declarator contains parameter
21286 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21287 for, eg, abbreviated integral type names), then we actually have a
21288 parameter pack expansion expression. Otherwise, leave the ellipsis
21289 for a C-style variadic function. */
21290 token = cp_lexer_peek_token (parser->lexer);
21291 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21293 tree type = decl_specifiers.type;
21295 if (type && DECL_P (type))
21296 type = TREE_TYPE (type);
21298 if (((type
21299 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21300 && (template_parm_p || uses_parameter_packs (type)))
21301 || (!type && template_parm_p))
21302 && declarator_can_be_parameter_pack (declarator))
21304 /* Consume the `...'. */
21305 cp_lexer_consume_token (parser->lexer);
21306 maybe_warn_variadic_templates ();
21308 /* Build a pack expansion type */
21309 if (template_parm_p)
21310 template_parameter_pack_p = true;
21311 else if (declarator)
21312 declarator->parameter_pack_p = true;
21313 else
21314 decl_specifiers.type = make_pack_expansion (type);
21318 /* The restriction on defining new types applies only to the type
21319 of the parameter, not to the default argument. */
21320 parser->type_definition_forbidden_message = saved_message;
21322 /* If the next token is `=', then process a default argument. */
21323 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21325 tree type = decl_specifiers.type;
21326 token = cp_lexer_peek_token (parser->lexer);
21327 /* If we are defining a class, then the tokens that make up the
21328 default argument must be saved and processed later. */
21329 if (!template_parm_p && at_class_scope_p ()
21330 && TYPE_BEING_DEFINED (current_class_type)
21331 && !LAMBDA_TYPE_P (current_class_type))
21332 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21334 // A constrained-type-specifier may declare a type template-parameter.
21335 else if (declares_constrained_type_template_parameter (type))
21336 default_argument
21337 = cp_parser_default_type_template_argument (parser);
21339 // A constrained-type-specifier may declare a template-template-parameter.
21340 else if (declares_constrained_template_template_parameter (type))
21341 default_argument
21342 = cp_parser_default_template_template_argument (parser);
21344 /* Outside of a class definition, we can just parse the
21345 assignment-expression. */
21346 else
21347 default_argument
21348 = cp_parser_default_argument (parser, template_parm_p);
21350 if (!parser->default_arg_ok_p)
21352 permerror (token->location,
21353 "default arguments are only "
21354 "permitted for function parameters");
21356 else if ((declarator && declarator->parameter_pack_p)
21357 || template_parameter_pack_p
21358 || (decl_specifiers.type
21359 && PACK_EXPANSION_P (decl_specifiers.type)))
21361 /* Find the name of the parameter pack. */
21362 cp_declarator *id_declarator = declarator;
21363 while (id_declarator && id_declarator->kind != cdk_id)
21364 id_declarator = id_declarator->declarator;
21366 if (id_declarator && id_declarator->kind == cdk_id)
21367 error_at (declarator_token_start->location,
21368 template_parm_p
21369 ? G_("template parameter pack %qD "
21370 "cannot have a default argument")
21371 : G_("parameter pack %qD cannot have "
21372 "a default argument"),
21373 id_declarator->u.id.unqualified_name);
21374 else
21375 error_at (declarator_token_start->location,
21376 template_parm_p
21377 ? G_("template parameter pack cannot have "
21378 "a default argument")
21379 : G_("parameter pack cannot have a "
21380 "default argument"));
21382 default_argument = NULL_TREE;
21385 else
21386 default_argument = NULL_TREE;
21388 return make_parameter_declarator (&decl_specifiers,
21389 declarator,
21390 default_argument,
21391 template_parameter_pack_p);
21394 /* Parse a default argument and return it.
21396 TEMPLATE_PARM_P is true if this is a default argument for a
21397 non-type template parameter. */
21398 static tree
21399 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21401 tree default_argument = NULL_TREE;
21402 bool saved_greater_than_is_operator_p;
21403 bool saved_local_variables_forbidden_p;
21404 bool non_constant_p, is_direct_init;
21406 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21407 set correctly. */
21408 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21409 parser->greater_than_is_operator_p = !template_parm_p;
21410 /* Local variable names (and the `this' keyword) may not
21411 appear in a default argument. */
21412 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21413 parser->local_variables_forbidden_p = true;
21414 /* Parse the assignment-expression. */
21415 if (template_parm_p)
21416 push_deferring_access_checks (dk_no_deferred);
21417 tree saved_class_ptr = NULL_TREE;
21418 tree saved_class_ref = NULL_TREE;
21419 /* The "this" pointer is not valid in a default argument. */
21420 if (cfun)
21422 saved_class_ptr = current_class_ptr;
21423 cp_function_chain->x_current_class_ptr = NULL_TREE;
21424 saved_class_ref = current_class_ref;
21425 cp_function_chain->x_current_class_ref = NULL_TREE;
21427 default_argument
21428 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21429 /* Restore the "this" pointer. */
21430 if (cfun)
21432 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21433 cp_function_chain->x_current_class_ref = saved_class_ref;
21435 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21436 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21437 if (template_parm_p)
21438 pop_deferring_access_checks ();
21439 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21440 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21442 return default_argument;
21445 /* Parse a function-body.
21447 function-body:
21448 compound_statement */
21450 static void
21451 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21453 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21454 ? BCS_TRY_BLOCK : BCS_NORMAL),
21455 true);
21458 /* Parse a ctor-initializer-opt followed by a function-body. Return
21459 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21460 is true we are parsing a function-try-block. */
21462 static bool
21463 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21464 bool in_function_try_block)
21466 tree body, list;
21467 bool ctor_initializer_p;
21468 const bool check_body_p =
21469 DECL_CONSTRUCTOR_P (current_function_decl)
21470 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21471 tree last = NULL;
21473 /* Begin the function body. */
21474 body = begin_function_body ();
21475 /* Parse the optional ctor-initializer. */
21476 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21478 /* If we're parsing a constexpr constructor definition, we need
21479 to check that the constructor body is indeed empty. However,
21480 before we get to cp_parser_function_body lot of junk has been
21481 generated, so we can't just check that we have an empty block.
21482 Rather we take a snapshot of the outermost block, and check whether
21483 cp_parser_function_body changed its state. */
21484 if (check_body_p)
21486 list = cur_stmt_list;
21487 if (STATEMENT_LIST_TAIL (list))
21488 last = STATEMENT_LIST_TAIL (list)->stmt;
21490 /* Parse the function-body. */
21491 cp_parser_function_body (parser, in_function_try_block);
21492 if (check_body_p)
21493 check_constexpr_ctor_body (last, list, /*complain=*/true);
21494 /* Finish the function body. */
21495 finish_function_body (body);
21497 return ctor_initializer_p;
21500 /* Parse an initializer.
21502 initializer:
21503 = initializer-clause
21504 ( expression-list )
21506 Returns an expression representing the initializer. If no
21507 initializer is present, NULL_TREE is returned.
21509 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21510 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21511 set to TRUE if there is no initializer present. If there is an
21512 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21513 is set to true; otherwise it is set to false. */
21515 static tree
21516 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21517 bool* non_constant_p)
21519 cp_token *token;
21520 tree init;
21522 /* Peek at the next token. */
21523 token = cp_lexer_peek_token (parser->lexer);
21525 /* Let our caller know whether or not this initializer was
21526 parenthesized. */
21527 *is_direct_init = (token->type != CPP_EQ);
21528 /* Assume that the initializer is constant. */
21529 *non_constant_p = false;
21531 if (token->type == CPP_EQ)
21533 /* Consume the `='. */
21534 cp_lexer_consume_token (parser->lexer);
21535 /* Parse the initializer-clause. */
21536 init = cp_parser_initializer_clause (parser, non_constant_p);
21538 else if (token->type == CPP_OPEN_PAREN)
21540 vec<tree, va_gc> *vec;
21541 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21542 /*cast_p=*/false,
21543 /*allow_expansion_p=*/true,
21544 non_constant_p);
21545 if (vec == NULL)
21546 return error_mark_node;
21547 init = build_tree_list_vec (vec);
21548 release_tree_vector (vec);
21550 else if (token->type == CPP_OPEN_BRACE)
21552 cp_lexer_set_source_position (parser->lexer);
21553 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21554 init = cp_parser_braced_list (parser, non_constant_p);
21555 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21557 else
21559 /* Anything else is an error. */
21560 cp_parser_error (parser, "expected initializer");
21561 init = error_mark_node;
21564 if (check_for_bare_parameter_packs (init))
21565 init = error_mark_node;
21567 return init;
21570 /* Parse an initializer-clause.
21572 initializer-clause:
21573 assignment-expression
21574 braced-init-list
21576 Returns an expression representing the initializer.
21578 If the `assignment-expression' production is used the value
21579 returned is simply a representation for the expression.
21581 Otherwise, calls cp_parser_braced_list. */
21583 static cp_expr
21584 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21586 cp_expr initializer;
21588 /* Assume the expression is constant. */
21589 *non_constant_p = false;
21591 /* If it is not a `{', then we are looking at an
21592 assignment-expression. */
21593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21595 initializer
21596 = cp_parser_constant_expression (parser,
21597 /*allow_non_constant_p=*/true,
21598 non_constant_p);
21600 else
21601 initializer = cp_parser_braced_list (parser, non_constant_p);
21603 return initializer;
21606 /* Parse a brace-enclosed initializer list.
21608 braced-init-list:
21609 { initializer-list , [opt] }
21612 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21613 the elements of the initializer-list (or NULL, if the last
21614 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21615 NULL_TREE. There is no way to detect whether or not the optional
21616 trailing `,' was provided. NON_CONSTANT_P is as for
21617 cp_parser_initializer. */
21619 static cp_expr
21620 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21622 tree initializer;
21623 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21625 /* Consume the `{' token. */
21626 cp_lexer_consume_token (parser->lexer);
21627 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21628 initializer = make_node (CONSTRUCTOR);
21629 /* If it's not a `}', then there is a non-trivial initializer. */
21630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21632 /* Parse the initializer list. */
21633 CONSTRUCTOR_ELTS (initializer)
21634 = cp_parser_initializer_list (parser, non_constant_p);
21635 /* A trailing `,' token is allowed. */
21636 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21637 cp_lexer_consume_token (parser->lexer);
21639 else
21640 *non_constant_p = false;
21641 /* Now, there should be a trailing `}'. */
21642 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21643 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21644 TREE_TYPE (initializer) = init_list_type_node;
21646 cp_expr result (initializer);
21647 /* Build a location of the form:
21648 { ... }
21649 ^~~~~~~
21650 with caret==start at the open brace, finish at the close brace. */
21651 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21652 result.set_location (combined_loc);
21653 return result;
21656 /* Consume tokens up to, and including, the next non-nested closing `]'.
21657 Returns true iff we found a closing `]'. */
21659 static bool
21660 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21662 unsigned square_depth = 0;
21664 while (true)
21666 cp_token * token = cp_lexer_peek_token (parser->lexer);
21668 switch (token->type)
21670 case CPP_EOF:
21671 case CPP_PRAGMA_EOL:
21672 /* If we've run out of tokens, then there is no closing `]'. */
21673 return false;
21675 case CPP_OPEN_SQUARE:
21676 ++square_depth;
21677 break;
21679 case CPP_CLOSE_SQUARE:
21680 if (!square_depth--)
21682 cp_lexer_consume_token (parser->lexer);
21683 return true;
21685 break;
21687 default:
21688 break;
21691 /* Consume the token. */
21692 cp_lexer_consume_token (parser->lexer);
21696 /* Return true if we are looking at an array-designator, false otherwise. */
21698 static bool
21699 cp_parser_array_designator_p (cp_parser *parser)
21701 /* Consume the `['. */
21702 cp_lexer_consume_token (parser->lexer);
21704 cp_lexer_save_tokens (parser->lexer);
21706 /* Skip tokens until the next token is a closing square bracket.
21707 If we find the closing `]', and the next token is a `=', then
21708 we are looking at an array designator. */
21709 bool array_designator_p
21710 = (cp_parser_skip_to_closing_square_bracket (parser)
21711 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21713 /* Roll back the tokens we skipped. */
21714 cp_lexer_rollback_tokens (parser->lexer);
21716 return array_designator_p;
21719 /* Parse an initializer-list.
21721 initializer-list:
21722 initializer-clause ... [opt]
21723 initializer-list , initializer-clause ... [opt]
21725 GNU Extension:
21727 initializer-list:
21728 designation initializer-clause ...[opt]
21729 initializer-list , designation initializer-clause ...[opt]
21731 designation:
21732 . identifier =
21733 identifier :
21734 [ constant-expression ] =
21736 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21737 for the initializer. If the INDEX of the elt is non-NULL, it is the
21738 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21739 as for cp_parser_initializer. */
21741 static vec<constructor_elt, va_gc> *
21742 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21744 vec<constructor_elt, va_gc> *v = NULL;
21746 /* Assume all of the expressions are constant. */
21747 *non_constant_p = false;
21749 /* Parse the rest of the list. */
21750 while (true)
21752 cp_token *token;
21753 tree designator;
21754 tree initializer;
21755 bool clause_non_constant_p;
21757 /* If the next token is an identifier and the following one is a
21758 colon, we are looking at the GNU designated-initializer
21759 syntax. */
21760 if (cp_parser_allow_gnu_extensions_p (parser)
21761 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21762 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21764 /* Warn the user that they are using an extension. */
21765 pedwarn (input_location, OPT_Wpedantic,
21766 "ISO C++ does not allow designated initializers");
21767 /* Consume the identifier. */
21768 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21769 /* Consume the `:'. */
21770 cp_lexer_consume_token (parser->lexer);
21772 /* Also handle the C99 syntax, '. id ='. */
21773 else if (cp_parser_allow_gnu_extensions_p (parser)
21774 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21775 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21776 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21778 /* Warn the user that they are using an extension. */
21779 pedwarn (input_location, OPT_Wpedantic,
21780 "ISO C++ does not allow C99 designated initializers");
21781 /* Consume the `.'. */
21782 cp_lexer_consume_token (parser->lexer);
21783 /* Consume the identifier. */
21784 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21785 /* Consume the `='. */
21786 cp_lexer_consume_token (parser->lexer);
21788 /* Also handle C99 array designators, '[ const ] ='. */
21789 else if (cp_parser_allow_gnu_extensions_p (parser)
21790 && !c_dialect_objc ()
21791 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21793 /* In C++11, [ could start a lambda-introducer. */
21794 bool non_const = false;
21796 cp_parser_parse_tentatively (parser);
21798 if (!cp_parser_array_designator_p (parser))
21800 cp_parser_simulate_error (parser);
21801 designator = NULL_TREE;
21803 else
21805 designator = cp_parser_constant_expression (parser, true,
21806 &non_const);
21807 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21808 cp_parser_require (parser, CPP_EQ, RT_EQ);
21811 if (!cp_parser_parse_definitely (parser))
21812 designator = NULL_TREE;
21813 else if (non_const)
21814 require_potential_rvalue_constant_expression (designator);
21816 else
21817 designator = NULL_TREE;
21819 /* Parse the initializer. */
21820 initializer = cp_parser_initializer_clause (parser,
21821 &clause_non_constant_p);
21822 /* If any clause is non-constant, so is the entire initializer. */
21823 if (clause_non_constant_p)
21824 *non_constant_p = true;
21826 /* If we have an ellipsis, this is an initializer pack
21827 expansion. */
21828 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21830 /* Consume the `...'. */
21831 cp_lexer_consume_token (parser->lexer);
21833 /* Turn the initializer into an initializer expansion. */
21834 initializer = make_pack_expansion (initializer);
21837 /* Add it to the vector. */
21838 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21840 /* If the next token is not a comma, we have reached the end of
21841 the list. */
21842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21843 break;
21845 /* Peek at the next token. */
21846 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21847 /* If the next token is a `}', then we're still done. An
21848 initializer-clause can have a trailing `,' after the
21849 initializer-list and before the closing `}'. */
21850 if (token->type == CPP_CLOSE_BRACE)
21851 break;
21853 /* Consume the `,' token. */
21854 cp_lexer_consume_token (parser->lexer);
21857 return v;
21860 /* Classes [gram.class] */
21862 /* Parse a class-name.
21864 class-name:
21865 identifier
21866 template-id
21868 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21869 to indicate that names looked up in dependent types should be
21870 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21871 keyword has been used to indicate that the name that appears next
21872 is a template. TAG_TYPE indicates the explicit tag given before
21873 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21874 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21875 is the class being defined in a class-head. If ENUM_OK is TRUE,
21876 enum-names are also accepted.
21878 Returns the TYPE_DECL representing the class. */
21880 static tree
21881 cp_parser_class_name (cp_parser *parser,
21882 bool typename_keyword_p,
21883 bool template_keyword_p,
21884 enum tag_types tag_type,
21885 bool check_dependency_p,
21886 bool class_head_p,
21887 bool is_declaration,
21888 bool enum_ok)
21890 tree decl;
21891 tree scope;
21892 bool typename_p;
21893 cp_token *token;
21894 tree identifier = NULL_TREE;
21896 /* All class-names start with an identifier. */
21897 token = cp_lexer_peek_token (parser->lexer);
21898 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21900 cp_parser_error (parser, "expected class-name");
21901 return error_mark_node;
21904 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21905 to a template-id, so we save it here. */
21906 scope = parser->scope;
21907 if (scope == error_mark_node)
21908 return error_mark_node;
21910 /* Any name names a type if we're following the `typename' keyword
21911 in a qualified name where the enclosing scope is type-dependent. */
21912 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21913 && dependent_type_p (scope));
21914 /* Handle the common case (an identifier, but not a template-id)
21915 efficiently. */
21916 if (token->type == CPP_NAME
21917 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21919 cp_token *identifier_token;
21920 bool ambiguous_p;
21922 /* Look for the identifier. */
21923 identifier_token = cp_lexer_peek_token (parser->lexer);
21924 ambiguous_p = identifier_token->error_reported;
21925 identifier = cp_parser_identifier (parser);
21926 /* If the next token isn't an identifier, we are certainly not
21927 looking at a class-name. */
21928 if (identifier == error_mark_node)
21929 decl = error_mark_node;
21930 /* If we know this is a type-name, there's no need to look it
21931 up. */
21932 else if (typename_p)
21933 decl = identifier;
21934 else
21936 tree ambiguous_decls;
21937 /* If we already know that this lookup is ambiguous, then
21938 we've already issued an error message; there's no reason
21939 to check again. */
21940 if (ambiguous_p)
21942 cp_parser_simulate_error (parser);
21943 return error_mark_node;
21945 /* If the next token is a `::', then the name must be a type
21946 name.
21948 [basic.lookup.qual]
21950 During the lookup for a name preceding the :: scope
21951 resolution operator, object, function, and enumerator
21952 names are ignored. */
21953 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21954 tag_type = scope_type;
21955 /* Look up the name. */
21956 decl = cp_parser_lookup_name (parser, identifier,
21957 tag_type,
21958 /*is_template=*/false,
21959 /*is_namespace=*/false,
21960 check_dependency_p,
21961 &ambiguous_decls,
21962 identifier_token->location);
21963 if (ambiguous_decls)
21965 if (cp_parser_parsing_tentatively (parser))
21966 cp_parser_simulate_error (parser);
21967 return error_mark_node;
21971 else
21973 /* Try a template-id. */
21974 decl = cp_parser_template_id (parser, template_keyword_p,
21975 check_dependency_p,
21976 tag_type,
21977 is_declaration);
21978 if (decl == error_mark_node)
21979 return error_mark_node;
21982 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21984 /* If this is a typename, create a TYPENAME_TYPE. */
21985 if (typename_p && decl != error_mark_node)
21987 decl = make_typename_type (scope, decl, typename_type,
21988 /*complain=*/tf_error);
21989 if (decl != error_mark_node)
21990 decl = TYPE_NAME (decl);
21993 decl = strip_using_decl (decl);
21995 /* Check to see that it is really the name of a class. */
21996 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21997 && identifier_p (TREE_OPERAND (decl, 0))
21998 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21999 /* Situations like this:
22001 template <typename T> struct A {
22002 typename T::template X<int>::I i;
22005 are problematic. Is `T::template X<int>' a class-name? The
22006 standard does not seem to be definitive, but there is no other
22007 valid interpretation of the following `::'. Therefore, those
22008 names are considered class-names. */
22010 decl = make_typename_type (scope, decl, tag_type, tf_error);
22011 if (decl != error_mark_node)
22012 decl = TYPE_NAME (decl);
22014 else if (TREE_CODE (decl) != TYPE_DECL
22015 || TREE_TYPE (decl) == error_mark_node
22016 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22017 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22018 /* In Objective-C 2.0, a classname followed by '.' starts a
22019 dot-syntax expression, and it's not a type-name. */
22020 || (c_dialect_objc ()
22021 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22022 && objc_is_class_name (decl)))
22023 decl = error_mark_node;
22025 if (decl == error_mark_node)
22026 cp_parser_error (parser, "expected class-name");
22027 else if (identifier && !parser->scope)
22028 maybe_note_name_used_in_class (identifier, decl);
22030 return decl;
22033 /* Parse a class-specifier.
22035 class-specifier:
22036 class-head { member-specification [opt] }
22038 Returns the TREE_TYPE representing the class. */
22040 static tree
22041 cp_parser_class_specifier_1 (cp_parser* parser)
22043 tree type;
22044 tree attributes = NULL_TREE;
22045 bool nested_name_specifier_p;
22046 unsigned saved_num_template_parameter_lists;
22047 bool saved_in_function_body;
22048 unsigned char in_statement;
22049 bool in_switch_statement_p;
22050 bool saved_in_unbraced_linkage_specification_p;
22051 tree old_scope = NULL_TREE;
22052 tree scope = NULL_TREE;
22053 cp_token *closing_brace;
22055 push_deferring_access_checks (dk_no_deferred);
22057 /* Parse the class-head. */
22058 type = cp_parser_class_head (parser,
22059 &nested_name_specifier_p);
22060 /* If the class-head was a semantic disaster, skip the entire body
22061 of the class. */
22062 if (!type)
22064 cp_parser_skip_to_end_of_block_or_statement (parser);
22065 pop_deferring_access_checks ();
22066 return error_mark_node;
22069 /* Look for the `{'. */
22070 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22072 pop_deferring_access_checks ();
22073 return error_mark_node;
22076 cp_ensure_no_omp_declare_simd (parser);
22077 cp_ensure_no_oacc_routine (parser);
22079 /* Issue an error message if type-definitions are forbidden here. */
22080 cp_parser_check_type_definition (parser);
22081 /* Remember that we are defining one more class. */
22082 ++parser->num_classes_being_defined;
22083 /* Inside the class, surrounding template-parameter-lists do not
22084 apply. */
22085 saved_num_template_parameter_lists
22086 = parser->num_template_parameter_lists;
22087 parser->num_template_parameter_lists = 0;
22088 /* We are not in a function body. */
22089 saved_in_function_body = parser->in_function_body;
22090 parser->in_function_body = false;
22091 /* Or in a loop. */
22092 in_statement = parser->in_statement;
22093 parser->in_statement = 0;
22094 /* Or in a switch. */
22095 in_switch_statement_p = parser->in_switch_statement_p;
22096 parser->in_switch_statement_p = false;
22097 /* We are not immediately inside an extern "lang" block. */
22098 saved_in_unbraced_linkage_specification_p
22099 = parser->in_unbraced_linkage_specification_p;
22100 parser->in_unbraced_linkage_specification_p = false;
22102 // Associate constraints with the type.
22103 if (flag_concepts)
22104 type = associate_classtype_constraints (type);
22106 /* Start the class. */
22107 if (nested_name_specifier_p)
22109 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22110 old_scope = push_inner_scope (scope);
22112 type = begin_class_definition (type);
22114 if (type == error_mark_node)
22115 /* If the type is erroneous, skip the entire body of the class. */
22116 cp_parser_skip_to_closing_brace (parser);
22117 else
22118 /* Parse the member-specification. */
22119 cp_parser_member_specification_opt (parser);
22121 /* Look for the trailing `}'. */
22122 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22123 /* Look for trailing attributes to apply to this class. */
22124 if (cp_parser_allow_gnu_extensions_p (parser))
22125 attributes = cp_parser_gnu_attributes_opt (parser);
22126 if (type != error_mark_node)
22127 type = finish_struct (type, attributes);
22128 if (nested_name_specifier_p)
22129 pop_inner_scope (old_scope, scope);
22131 /* We've finished a type definition. Check for the common syntax
22132 error of forgetting a semicolon after the definition. We need to
22133 be careful, as we can't just check for not-a-semicolon and be done
22134 with it; the user might have typed:
22136 class X { } c = ...;
22137 class X { } *p = ...;
22139 and so forth. Instead, enumerate all the possible tokens that
22140 might follow this production; if we don't see one of them, then
22141 complain and silently insert the semicolon. */
22143 cp_token *token = cp_lexer_peek_token (parser->lexer);
22144 bool want_semicolon = true;
22146 if (cp_next_tokens_can_be_std_attribute_p (parser))
22147 /* Don't try to parse c++11 attributes here. As per the
22148 grammar, that should be a task for
22149 cp_parser_decl_specifier_seq. */
22150 want_semicolon = false;
22152 switch (token->type)
22154 case CPP_NAME:
22155 case CPP_SEMICOLON:
22156 case CPP_MULT:
22157 case CPP_AND:
22158 case CPP_OPEN_PAREN:
22159 case CPP_CLOSE_PAREN:
22160 case CPP_COMMA:
22161 want_semicolon = false;
22162 break;
22164 /* While it's legal for type qualifiers and storage class
22165 specifiers to follow type definitions in the grammar, only
22166 compiler testsuites contain code like that. Assume that if
22167 we see such code, then what we're really seeing is a case
22168 like:
22170 class X { }
22171 const <type> var = ...;
22175 class Y { }
22176 static <type> func (...) ...
22178 i.e. the qualifier or specifier applies to the next
22179 declaration. To do so, however, we need to look ahead one
22180 more token to see if *that* token is a type specifier.
22182 This code could be improved to handle:
22184 class Z { }
22185 static const <type> var = ...; */
22186 case CPP_KEYWORD:
22187 if (keyword_is_decl_specifier (token->keyword))
22189 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22191 /* Handling user-defined types here would be nice, but very
22192 tricky. */
22193 want_semicolon
22194 = (lookahead->type == CPP_KEYWORD
22195 && keyword_begins_type_specifier (lookahead->keyword));
22197 break;
22198 default:
22199 break;
22202 /* If we don't have a type, then something is very wrong and we
22203 shouldn't try to do anything clever. Likewise for not seeing the
22204 closing brace. */
22205 if (closing_brace && TYPE_P (type) && want_semicolon)
22207 /* Locate the closing brace. */
22208 cp_token_position prev
22209 = cp_lexer_previous_token_position (parser->lexer);
22210 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22211 location_t loc = prev_token->location;
22213 /* We want to suggest insertion of a ';' immediately *after* the
22214 closing brace, so, if we can, offset the location by 1 column. */
22215 location_t next_loc = loc;
22216 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22217 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22219 rich_location richloc (line_table, next_loc);
22221 /* If we successfully offset the location, suggest the fix-it. */
22222 if (next_loc != loc)
22223 richloc.add_fixit_insert_before (next_loc, ";");
22225 if (CLASSTYPE_DECLARED_CLASS (type))
22226 error_at_rich_loc (&richloc,
22227 "expected %<;%> after class definition");
22228 else if (TREE_CODE (type) == RECORD_TYPE)
22229 error_at_rich_loc (&richloc,
22230 "expected %<;%> after struct definition");
22231 else if (TREE_CODE (type) == UNION_TYPE)
22232 error_at_rich_loc (&richloc,
22233 "expected %<;%> after union definition");
22234 else
22235 gcc_unreachable ();
22237 /* Unget one token and smash it to look as though we encountered
22238 a semicolon in the input stream. */
22239 cp_lexer_set_token_position (parser->lexer, prev);
22240 token = cp_lexer_peek_token (parser->lexer);
22241 token->type = CPP_SEMICOLON;
22242 token->keyword = RID_MAX;
22246 /* If this class is not itself within the scope of another class,
22247 then we need to parse the bodies of all of the queued function
22248 definitions. Note that the queued functions defined in a class
22249 are not always processed immediately following the
22250 class-specifier for that class. Consider:
22252 struct A {
22253 struct B { void f() { sizeof (A); } };
22256 If `f' were processed before the processing of `A' were
22257 completed, there would be no way to compute the size of `A'.
22258 Note that the nesting we are interested in here is lexical --
22259 not the semantic nesting given by TYPE_CONTEXT. In particular,
22260 for:
22262 struct A { struct B; };
22263 struct A::B { void f() { } };
22265 there is no need to delay the parsing of `A::B::f'. */
22266 if (--parser->num_classes_being_defined == 0)
22268 tree decl;
22269 tree class_type = NULL_TREE;
22270 tree pushed_scope = NULL_TREE;
22271 unsigned ix;
22272 cp_default_arg_entry *e;
22273 tree save_ccp, save_ccr;
22275 /* In a first pass, parse default arguments to the functions.
22276 Then, in a second pass, parse the bodies of the functions.
22277 This two-phased approach handles cases like:
22279 struct S {
22280 void f() { g(); }
22281 void g(int i = 3);
22285 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22287 decl = e->decl;
22288 /* If there are default arguments that have not yet been processed,
22289 take care of them now. */
22290 if (class_type != e->class_type)
22292 if (pushed_scope)
22293 pop_scope (pushed_scope);
22294 class_type = e->class_type;
22295 pushed_scope = push_scope (class_type);
22297 /* Make sure that any template parameters are in scope. */
22298 maybe_begin_member_template_processing (decl);
22299 /* Parse the default argument expressions. */
22300 cp_parser_late_parsing_default_args (parser, decl);
22301 /* Remove any template parameters from the symbol table. */
22302 maybe_end_member_template_processing ();
22304 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22305 /* Now parse any NSDMIs. */
22306 save_ccp = current_class_ptr;
22307 save_ccr = current_class_ref;
22308 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22310 if (class_type != DECL_CONTEXT (decl))
22312 if (pushed_scope)
22313 pop_scope (pushed_scope);
22314 class_type = DECL_CONTEXT (decl);
22315 pushed_scope = push_scope (class_type);
22317 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22318 cp_parser_late_parsing_nsdmi (parser, decl);
22320 vec_safe_truncate (unparsed_nsdmis, 0);
22321 current_class_ptr = save_ccp;
22322 current_class_ref = save_ccr;
22323 if (pushed_scope)
22324 pop_scope (pushed_scope);
22326 /* Now do some post-NSDMI bookkeeping. */
22327 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22328 after_nsdmi_defaulted_late_checks (class_type);
22329 vec_safe_truncate (unparsed_classes, 0);
22330 after_nsdmi_defaulted_late_checks (type);
22332 /* Now parse the body of the functions. */
22333 if (flag_openmp)
22335 /* OpenMP UDRs need to be parsed before all other functions. */
22336 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22337 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22338 cp_parser_late_parsing_for_member (parser, decl);
22339 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22340 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22341 cp_parser_late_parsing_for_member (parser, decl);
22343 else
22344 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22345 cp_parser_late_parsing_for_member (parser, decl);
22346 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22348 else
22349 vec_safe_push (unparsed_classes, type);
22351 /* Put back any saved access checks. */
22352 pop_deferring_access_checks ();
22354 /* Restore saved state. */
22355 parser->in_switch_statement_p = in_switch_statement_p;
22356 parser->in_statement = in_statement;
22357 parser->in_function_body = saved_in_function_body;
22358 parser->num_template_parameter_lists
22359 = saved_num_template_parameter_lists;
22360 parser->in_unbraced_linkage_specification_p
22361 = saved_in_unbraced_linkage_specification_p;
22363 return type;
22366 static tree
22367 cp_parser_class_specifier (cp_parser* parser)
22369 tree ret;
22370 timevar_push (TV_PARSE_STRUCT);
22371 ret = cp_parser_class_specifier_1 (parser);
22372 timevar_pop (TV_PARSE_STRUCT);
22373 return ret;
22376 /* Parse a class-head.
22378 class-head:
22379 class-key identifier [opt] base-clause [opt]
22380 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22381 class-key nested-name-specifier [opt] template-id
22382 base-clause [opt]
22384 class-virt-specifier:
22385 final
22387 GNU Extensions:
22388 class-key attributes identifier [opt] base-clause [opt]
22389 class-key attributes nested-name-specifier identifier base-clause [opt]
22390 class-key attributes nested-name-specifier [opt] template-id
22391 base-clause [opt]
22393 Upon return BASES is initialized to the list of base classes (or
22394 NULL, if there are none) in the same form returned by
22395 cp_parser_base_clause.
22397 Returns the TYPE of the indicated class. Sets
22398 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22399 involving a nested-name-specifier was used, and FALSE otherwise.
22401 Returns error_mark_node if this is not a class-head.
22403 Returns NULL_TREE if the class-head is syntactically valid, but
22404 semantically invalid in a way that means we should skip the entire
22405 body of the class. */
22407 static tree
22408 cp_parser_class_head (cp_parser* parser,
22409 bool* nested_name_specifier_p)
22411 tree nested_name_specifier;
22412 enum tag_types class_key;
22413 tree id = NULL_TREE;
22414 tree type = NULL_TREE;
22415 tree attributes;
22416 tree bases;
22417 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22418 bool template_id_p = false;
22419 bool qualified_p = false;
22420 bool invalid_nested_name_p = false;
22421 bool invalid_explicit_specialization_p = false;
22422 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22423 tree pushed_scope = NULL_TREE;
22424 unsigned num_templates;
22425 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22426 /* Assume no nested-name-specifier will be present. */
22427 *nested_name_specifier_p = false;
22428 /* Assume no template parameter lists will be used in defining the
22429 type. */
22430 num_templates = 0;
22431 parser->colon_corrects_to_scope_p = false;
22433 /* Look for the class-key. */
22434 class_key = cp_parser_class_key (parser);
22435 if (class_key == none_type)
22436 return error_mark_node;
22438 location_t class_head_start_location = input_location;
22440 /* Parse the attributes. */
22441 attributes = cp_parser_attributes_opt (parser);
22443 /* If the next token is `::', that is invalid -- but sometimes
22444 people do try to write:
22446 struct ::S {};
22448 Handle this gracefully by accepting the extra qualifier, and then
22449 issuing an error about it later if this really is a
22450 class-head. If it turns out just to be an elaborated type
22451 specifier, remain silent. */
22452 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22453 qualified_p = true;
22455 push_deferring_access_checks (dk_no_check);
22457 /* Determine the name of the class. Begin by looking for an
22458 optional nested-name-specifier. */
22459 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22460 nested_name_specifier
22461 = cp_parser_nested_name_specifier_opt (parser,
22462 /*typename_keyword_p=*/false,
22463 /*check_dependency_p=*/false,
22464 /*type_p=*/true,
22465 /*is_declaration=*/false);
22466 /* If there was a nested-name-specifier, then there *must* be an
22467 identifier. */
22468 if (nested_name_specifier)
22470 type_start_token = cp_lexer_peek_token (parser->lexer);
22471 /* Although the grammar says `identifier', it really means
22472 `class-name' or `template-name'. You are only allowed to
22473 define a class that has already been declared with this
22474 syntax.
22476 The proposed resolution for Core Issue 180 says that wherever
22477 you see `class T::X' you should treat `X' as a type-name.
22479 It is OK to define an inaccessible class; for example:
22481 class A { class B; };
22482 class A::B {};
22484 We do not know if we will see a class-name, or a
22485 template-name. We look for a class-name first, in case the
22486 class-name is a template-id; if we looked for the
22487 template-name first we would stop after the template-name. */
22488 cp_parser_parse_tentatively (parser);
22489 type = cp_parser_class_name (parser,
22490 /*typename_keyword_p=*/false,
22491 /*template_keyword_p=*/false,
22492 class_type,
22493 /*check_dependency_p=*/false,
22494 /*class_head_p=*/true,
22495 /*is_declaration=*/false);
22496 /* If that didn't work, ignore the nested-name-specifier. */
22497 if (!cp_parser_parse_definitely (parser))
22499 invalid_nested_name_p = true;
22500 type_start_token = cp_lexer_peek_token (parser->lexer);
22501 id = cp_parser_identifier (parser);
22502 if (id == error_mark_node)
22503 id = NULL_TREE;
22505 /* If we could not find a corresponding TYPE, treat this
22506 declaration like an unqualified declaration. */
22507 if (type == error_mark_node)
22508 nested_name_specifier = NULL_TREE;
22509 /* Otherwise, count the number of templates used in TYPE and its
22510 containing scopes. */
22511 else
22513 tree scope;
22515 for (scope = TREE_TYPE (type);
22516 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22517 scope = get_containing_scope (scope))
22518 if (TYPE_P (scope)
22519 && CLASS_TYPE_P (scope)
22520 && CLASSTYPE_TEMPLATE_INFO (scope)
22521 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22522 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22523 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22524 ++num_templates;
22527 /* Otherwise, the identifier is optional. */
22528 else
22530 /* We don't know whether what comes next is a template-id,
22531 an identifier, or nothing at all. */
22532 cp_parser_parse_tentatively (parser);
22533 /* Check for a template-id. */
22534 type_start_token = cp_lexer_peek_token (parser->lexer);
22535 id = cp_parser_template_id (parser,
22536 /*template_keyword_p=*/false,
22537 /*check_dependency_p=*/true,
22538 class_key,
22539 /*is_declaration=*/true);
22540 /* If that didn't work, it could still be an identifier. */
22541 if (!cp_parser_parse_definitely (parser))
22543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22545 type_start_token = cp_lexer_peek_token (parser->lexer);
22546 id = cp_parser_identifier (parser);
22548 else
22549 id = NULL_TREE;
22551 else
22553 template_id_p = true;
22554 ++num_templates;
22558 pop_deferring_access_checks ();
22560 if (id)
22562 cp_parser_check_for_invalid_template_id (parser, id,
22563 class_key,
22564 type_start_token->location);
22566 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22568 /* If it's not a `:' or a `{' then we can't really be looking at a
22569 class-head, since a class-head only appears as part of a
22570 class-specifier. We have to detect this situation before calling
22571 xref_tag, since that has irreversible side-effects. */
22572 if (!cp_parser_next_token_starts_class_definition_p (parser))
22574 cp_parser_error (parser, "expected %<{%> or %<:%>");
22575 type = error_mark_node;
22576 goto out;
22579 /* At this point, we're going ahead with the class-specifier, even
22580 if some other problem occurs. */
22581 cp_parser_commit_to_tentative_parse (parser);
22582 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22584 cp_parser_error (parser,
22585 "cannot specify %<override%> for a class");
22586 type = error_mark_node;
22587 goto out;
22589 /* Issue the error about the overly-qualified name now. */
22590 if (qualified_p)
22592 cp_parser_error (parser,
22593 "global qualification of class name is invalid");
22594 type = error_mark_node;
22595 goto out;
22597 else if (invalid_nested_name_p)
22599 cp_parser_error (parser,
22600 "qualified name does not name a class");
22601 type = error_mark_node;
22602 goto out;
22604 else if (nested_name_specifier)
22606 tree scope;
22608 /* Reject typedef-names in class heads. */
22609 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22611 error_at (type_start_token->location,
22612 "invalid class name in declaration of %qD",
22613 type);
22614 type = NULL_TREE;
22615 goto done;
22618 /* Figure out in what scope the declaration is being placed. */
22619 scope = current_scope ();
22620 /* If that scope does not contain the scope in which the
22621 class was originally declared, the program is invalid. */
22622 if (scope && !is_ancestor (scope, nested_name_specifier))
22624 if (at_namespace_scope_p ())
22625 error_at (type_start_token->location,
22626 "declaration of %qD in namespace %qD which does not "
22627 "enclose %qD",
22628 type, scope, nested_name_specifier);
22629 else
22630 error_at (type_start_token->location,
22631 "declaration of %qD in %qD which does not enclose %qD",
22632 type, scope, nested_name_specifier);
22633 type = NULL_TREE;
22634 goto done;
22636 /* [dcl.meaning]
22638 A declarator-id shall not be qualified except for the
22639 definition of a ... nested class outside of its class
22640 ... [or] the definition or explicit instantiation of a
22641 class member of a namespace outside of its namespace. */
22642 if (scope == nested_name_specifier)
22644 permerror (nested_name_specifier_token_start->location,
22645 "extra qualification not allowed");
22646 nested_name_specifier = NULL_TREE;
22647 num_templates = 0;
22650 /* An explicit-specialization must be preceded by "template <>". If
22651 it is not, try to recover gracefully. */
22652 if (at_namespace_scope_p ()
22653 && parser->num_template_parameter_lists == 0
22654 && !processing_template_parmlist
22655 && template_id_p)
22657 /* Build a location of this form:
22658 struct typename <ARGS>
22659 ^~~~~~~~~~~~~~~~~~~~~~
22660 with caret==start at the start token, and
22661 finishing at the end of the type. */
22662 location_t reported_loc
22663 = make_location (class_head_start_location,
22664 class_head_start_location,
22665 get_finish (type_start_token->location));
22666 rich_location richloc (line_table, reported_loc);
22667 richloc.add_fixit_insert_before (class_head_start_location,
22668 "template <> ");
22669 error_at_rich_loc
22670 (&richloc,
22671 "an explicit specialization must be preceded by %<template <>%>");
22672 invalid_explicit_specialization_p = true;
22673 /* Take the same action that would have been taken by
22674 cp_parser_explicit_specialization. */
22675 ++parser->num_template_parameter_lists;
22676 begin_specialization ();
22678 /* There must be no "return" statements between this point and the
22679 end of this function; set "type "to the correct return value and
22680 use "goto done;" to return. */
22681 /* Make sure that the right number of template parameters were
22682 present. */
22683 if (!cp_parser_check_template_parameters (parser, num_templates,
22684 type_start_token->location,
22685 /*declarator=*/NULL))
22687 /* If something went wrong, there is no point in even trying to
22688 process the class-definition. */
22689 type = NULL_TREE;
22690 goto done;
22693 /* Look up the type. */
22694 if (template_id_p)
22696 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22697 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22698 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22700 error_at (type_start_token->location,
22701 "function template %qD redeclared as a class template", id);
22702 type = error_mark_node;
22704 else
22706 type = TREE_TYPE (id);
22707 type = maybe_process_partial_specialization (type);
22709 /* Check the scope while we still know whether or not we had a
22710 nested-name-specifier. */
22711 if (type != error_mark_node)
22712 check_unqualified_spec_or_inst (type, type_start_token->location);
22714 if (nested_name_specifier)
22715 pushed_scope = push_scope (nested_name_specifier);
22717 else if (nested_name_specifier)
22719 tree class_type;
22721 /* Given:
22723 template <typename T> struct S { struct T };
22724 template <typename T> struct S<T>::T { };
22726 we will get a TYPENAME_TYPE when processing the definition of
22727 `S::T'. We need to resolve it to the actual type before we
22728 try to define it. */
22729 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22731 class_type = resolve_typename_type (TREE_TYPE (type),
22732 /*only_current_p=*/false);
22733 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22734 type = TYPE_NAME (class_type);
22735 else
22737 cp_parser_error (parser, "could not resolve typename type");
22738 type = error_mark_node;
22742 if (maybe_process_partial_specialization (TREE_TYPE (type))
22743 == error_mark_node)
22745 type = NULL_TREE;
22746 goto done;
22749 class_type = current_class_type;
22750 /* Enter the scope indicated by the nested-name-specifier. */
22751 pushed_scope = push_scope (nested_name_specifier);
22752 /* Get the canonical version of this type. */
22753 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22754 /* Call push_template_decl if it seems like we should be defining a
22755 template either from the template headers or the type we're
22756 defining, so that we diagnose both extra and missing headers. */
22757 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22758 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22759 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22761 type = push_template_decl (type);
22762 if (type == error_mark_node)
22764 type = NULL_TREE;
22765 goto done;
22769 type = TREE_TYPE (type);
22770 *nested_name_specifier_p = true;
22772 else /* The name is not a nested name. */
22774 /* If the class was unnamed, create a dummy name. */
22775 if (!id)
22776 id = make_anon_name ();
22777 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22778 ? ts_within_enclosing_non_class
22779 : ts_current);
22780 type = xref_tag (class_key, id, tag_scope,
22781 parser->num_template_parameter_lists);
22784 /* Indicate whether this class was declared as a `class' or as a
22785 `struct'. */
22786 if (TREE_CODE (type) == RECORD_TYPE)
22787 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22788 cp_parser_check_class_key (class_key, type);
22790 /* If this type was already complete, and we see another definition,
22791 that's an error. */
22792 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22794 error_at (type_start_token->location, "redefinition of %q#T",
22795 type);
22796 inform (location_of (type), "previous definition of %q#T",
22797 type);
22798 type = NULL_TREE;
22799 goto done;
22801 else if (type == error_mark_node)
22802 type = NULL_TREE;
22804 if (type)
22806 /* Apply attributes now, before any use of the class as a template
22807 argument in its base list. */
22808 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22809 fixup_attribute_variants (type);
22812 /* We will have entered the scope containing the class; the names of
22813 base classes should be looked up in that context. For example:
22815 struct A { struct B {}; struct C; };
22816 struct A::C : B {};
22818 is valid. */
22820 /* Get the list of base-classes, if there is one. */
22821 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22823 /* PR59482: enter the class scope so that base-specifiers are looked
22824 up correctly. */
22825 if (type)
22826 pushclass (type);
22827 bases = cp_parser_base_clause (parser);
22828 /* PR59482: get out of the previously pushed class scope so that the
22829 subsequent pops pop the right thing. */
22830 if (type)
22831 popclass ();
22833 else
22834 bases = NULL_TREE;
22836 /* If we're really defining a class, process the base classes.
22837 If they're invalid, fail. */
22838 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22839 xref_basetypes (type, bases);
22841 done:
22842 /* Leave the scope given by the nested-name-specifier. We will
22843 enter the class scope itself while processing the members. */
22844 if (pushed_scope)
22845 pop_scope (pushed_scope);
22847 if (invalid_explicit_specialization_p)
22849 end_specialization ();
22850 --parser->num_template_parameter_lists;
22853 if (type)
22854 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22855 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22856 CLASSTYPE_FINAL (type) = 1;
22857 out:
22858 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22859 return type;
22862 /* Parse a class-key.
22864 class-key:
22865 class
22866 struct
22867 union
22869 Returns the kind of class-key specified, or none_type to indicate
22870 error. */
22872 static enum tag_types
22873 cp_parser_class_key (cp_parser* parser)
22875 cp_token *token;
22876 enum tag_types tag_type;
22878 /* Look for the class-key. */
22879 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22880 if (!token)
22881 return none_type;
22883 /* Check to see if the TOKEN is a class-key. */
22884 tag_type = cp_parser_token_is_class_key (token);
22885 if (!tag_type)
22886 cp_parser_error (parser, "expected class-key");
22887 return tag_type;
22890 /* Parse a type-parameter-key.
22892 type-parameter-key:
22893 class
22894 typename
22897 static void
22898 cp_parser_type_parameter_key (cp_parser* parser)
22900 /* Look for the type-parameter-key. */
22901 enum tag_types tag_type = none_type;
22902 cp_token *token = cp_lexer_peek_token (parser->lexer);
22903 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22905 cp_lexer_consume_token (parser->lexer);
22906 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22907 /* typename is not allowed in a template template parameter
22908 by the standard until C++1Z. */
22909 pedwarn (token->location, OPT_Wpedantic,
22910 "ISO C++ forbids typename key in template template parameter;"
22911 " use -std=c++1z or -std=gnu++1z");
22913 else
22914 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22916 return;
22919 /* Parse an (optional) member-specification.
22921 member-specification:
22922 member-declaration member-specification [opt]
22923 access-specifier : member-specification [opt] */
22925 static void
22926 cp_parser_member_specification_opt (cp_parser* parser)
22928 while (true)
22930 cp_token *token;
22931 enum rid keyword;
22933 /* Peek at the next token. */
22934 token = cp_lexer_peek_token (parser->lexer);
22935 /* If it's a `}', or EOF then we've seen all the members. */
22936 if (token->type == CPP_CLOSE_BRACE
22937 || token->type == CPP_EOF
22938 || token->type == CPP_PRAGMA_EOL)
22939 break;
22941 /* See if this token is a keyword. */
22942 keyword = token->keyword;
22943 switch (keyword)
22945 case RID_PUBLIC:
22946 case RID_PROTECTED:
22947 case RID_PRIVATE:
22948 /* Consume the access-specifier. */
22949 cp_lexer_consume_token (parser->lexer);
22950 /* Remember which access-specifier is active. */
22951 current_access_specifier = token->u.value;
22952 /* Look for the `:'. */
22953 cp_parser_require (parser, CPP_COLON, RT_COLON);
22954 break;
22956 default:
22957 /* Accept #pragmas at class scope. */
22958 if (token->type == CPP_PRAGMA)
22960 cp_parser_pragma (parser, pragma_member, NULL);
22961 break;
22964 /* Otherwise, the next construction must be a
22965 member-declaration. */
22966 cp_parser_member_declaration (parser);
22971 /* Parse a member-declaration.
22973 member-declaration:
22974 decl-specifier-seq [opt] member-declarator-list [opt] ;
22975 function-definition ; [opt]
22976 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22977 using-declaration
22978 template-declaration
22979 alias-declaration
22981 member-declarator-list:
22982 member-declarator
22983 member-declarator-list , member-declarator
22985 member-declarator:
22986 declarator pure-specifier [opt]
22987 declarator constant-initializer [opt]
22988 identifier [opt] : constant-expression
22990 GNU Extensions:
22992 member-declaration:
22993 __extension__ member-declaration
22995 member-declarator:
22996 declarator attributes [opt] pure-specifier [opt]
22997 declarator attributes [opt] constant-initializer [opt]
22998 identifier [opt] attributes [opt] : constant-expression
23000 C++0x Extensions:
23002 member-declaration:
23003 static_assert-declaration */
23005 static void
23006 cp_parser_member_declaration (cp_parser* parser)
23008 cp_decl_specifier_seq decl_specifiers;
23009 tree prefix_attributes;
23010 tree decl;
23011 int declares_class_or_enum;
23012 bool friend_p;
23013 cp_token *token = NULL;
23014 cp_token *decl_spec_token_start = NULL;
23015 cp_token *initializer_token_start = NULL;
23016 int saved_pedantic;
23017 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23019 /* Check for the `__extension__' keyword. */
23020 if (cp_parser_extension_opt (parser, &saved_pedantic))
23022 /* Recurse. */
23023 cp_parser_member_declaration (parser);
23024 /* Restore the old value of the PEDANTIC flag. */
23025 pedantic = saved_pedantic;
23027 return;
23030 /* Check for a template-declaration. */
23031 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23033 /* An explicit specialization here is an error condition, and we
23034 expect the specialization handler to detect and report this. */
23035 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23036 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23037 cp_parser_explicit_specialization (parser);
23038 else
23039 cp_parser_template_declaration (parser, /*member_p=*/true);
23041 return;
23043 /* Check for a template introduction. */
23044 else if (cp_parser_template_declaration_after_export (parser, true))
23045 return;
23047 /* Check for a using-declaration. */
23048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23050 if (cxx_dialect < cxx11)
23052 /* Parse the using-declaration. */
23053 cp_parser_using_declaration (parser,
23054 /*access_declaration_p=*/false);
23055 return;
23057 else
23059 tree decl;
23060 bool alias_decl_expected;
23061 cp_parser_parse_tentatively (parser);
23062 decl = cp_parser_alias_declaration (parser);
23063 /* Note that if we actually see the '=' token after the
23064 identifier, cp_parser_alias_declaration commits the
23065 tentative parse. In that case, we really expect an
23066 alias-declaration. Otherwise, we expect a using
23067 declaration. */
23068 alias_decl_expected =
23069 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23070 cp_parser_parse_definitely (parser);
23072 if (alias_decl_expected)
23073 finish_member_declaration (decl);
23074 else
23075 cp_parser_using_declaration (parser,
23076 /*access_declaration_p=*/false);
23077 return;
23081 /* Check for @defs. */
23082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23084 tree ivar, member;
23085 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23086 ivar = ivar_chains;
23087 while (ivar)
23089 member = ivar;
23090 ivar = TREE_CHAIN (member);
23091 TREE_CHAIN (member) = NULL_TREE;
23092 finish_member_declaration (member);
23094 return;
23097 /* If the next token is `static_assert' we have a static assertion. */
23098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23100 cp_parser_static_assert (parser, /*member_p=*/true);
23101 return;
23104 parser->colon_corrects_to_scope_p = false;
23106 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23107 goto out;
23109 /* Parse the decl-specifier-seq. */
23110 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23111 cp_parser_decl_specifier_seq (parser,
23112 CP_PARSER_FLAGS_OPTIONAL,
23113 &decl_specifiers,
23114 &declares_class_or_enum);
23115 /* Check for an invalid type-name. */
23116 if (!decl_specifiers.any_type_specifiers_p
23117 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23118 goto out;
23119 /* If there is no declarator, then the decl-specifier-seq should
23120 specify a type. */
23121 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23123 /* If there was no decl-specifier-seq, and the next token is a
23124 `;', then we have something like:
23126 struct S { ; };
23128 [class.mem]
23130 Each member-declaration shall declare at least one member
23131 name of the class. */
23132 if (!decl_specifiers.any_specifiers_p)
23134 cp_token *token = cp_lexer_peek_token (parser->lexer);
23135 if (!in_system_header_at (token->location))
23137 gcc_rich_location richloc (token->location);
23138 richloc.add_fixit_remove ();
23139 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23142 else
23144 tree type;
23146 /* See if this declaration is a friend. */
23147 friend_p = cp_parser_friend_p (&decl_specifiers);
23148 /* If there were decl-specifiers, check to see if there was
23149 a class-declaration. */
23150 type = check_tag_decl (&decl_specifiers,
23151 /*explicit_type_instantiation_p=*/false);
23152 /* Nested classes have already been added to the class, but
23153 a `friend' needs to be explicitly registered. */
23154 if (friend_p)
23156 /* If the `friend' keyword was present, the friend must
23157 be introduced with a class-key. */
23158 if (!declares_class_or_enum && cxx_dialect < cxx11)
23159 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23160 "in C++03 a class-key must be used "
23161 "when declaring a friend");
23162 /* In this case:
23164 template <typename T> struct A {
23165 friend struct A<T>::B;
23168 A<T>::B will be represented by a TYPENAME_TYPE, and
23169 therefore not recognized by check_tag_decl. */
23170 if (!type)
23172 type = decl_specifiers.type;
23173 if (type && TREE_CODE (type) == TYPE_DECL)
23174 type = TREE_TYPE (type);
23176 if (!type || !TYPE_P (type))
23177 error_at (decl_spec_token_start->location,
23178 "friend declaration does not name a class or "
23179 "function");
23180 else
23181 make_friend_class (current_class_type, type,
23182 /*complain=*/true);
23184 /* If there is no TYPE, an error message will already have
23185 been issued. */
23186 else if (!type || type == error_mark_node)
23188 /* An anonymous aggregate has to be handled specially; such
23189 a declaration really declares a data member (with a
23190 particular type), as opposed to a nested class. */
23191 else if (ANON_AGGR_TYPE_P (type))
23193 /* C++11 9.5/6. */
23194 if (decl_specifiers.storage_class != sc_none)
23195 error_at (decl_spec_token_start->location,
23196 "a storage class on an anonymous aggregate "
23197 "in class scope is not allowed");
23199 /* Remove constructors and such from TYPE, now that we
23200 know it is an anonymous aggregate. */
23201 fixup_anonymous_aggr (type);
23202 /* And make the corresponding data member. */
23203 decl = build_decl (decl_spec_token_start->location,
23204 FIELD_DECL, NULL_TREE, type);
23205 /* Add it to the class. */
23206 finish_member_declaration (decl);
23208 else
23209 cp_parser_check_access_in_redeclaration
23210 (TYPE_NAME (type),
23211 decl_spec_token_start->location);
23214 else
23216 bool assume_semicolon = false;
23218 /* Clear attributes from the decl_specifiers but keep them
23219 around as prefix attributes that apply them to the entity
23220 being declared. */
23221 prefix_attributes = decl_specifiers.attributes;
23222 decl_specifiers.attributes = NULL_TREE;
23224 /* See if these declarations will be friends. */
23225 friend_p = cp_parser_friend_p (&decl_specifiers);
23227 /* Keep going until we hit the `;' at the end of the
23228 declaration. */
23229 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23231 tree attributes = NULL_TREE;
23232 tree first_attribute;
23234 /* Peek at the next token. */
23235 token = cp_lexer_peek_token (parser->lexer);
23237 /* Check for a bitfield declaration. */
23238 if (token->type == CPP_COLON
23239 || (token->type == CPP_NAME
23240 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23241 == CPP_COLON))
23243 tree identifier;
23244 tree width;
23246 /* Get the name of the bitfield. Note that we cannot just
23247 check TOKEN here because it may have been invalidated by
23248 the call to cp_lexer_peek_nth_token above. */
23249 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23250 identifier = cp_parser_identifier (parser);
23251 else
23252 identifier = NULL_TREE;
23254 /* Consume the `:' token. */
23255 cp_lexer_consume_token (parser->lexer);
23256 /* Get the width of the bitfield. */
23257 width
23258 = cp_parser_constant_expression (parser);
23260 /* Look for attributes that apply to the bitfield. */
23261 attributes = cp_parser_attributes_opt (parser);
23262 /* Remember which attributes are prefix attributes and
23263 which are not. */
23264 first_attribute = attributes;
23265 /* Combine the attributes. */
23266 attributes = chainon (prefix_attributes, attributes);
23268 /* Create the bitfield declaration. */
23269 decl = grokbitfield (identifier
23270 ? make_id_declarator (NULL_TREE,
23271 identifier,
23272 sfk_none)
23273 : NULL,
23274 &decl_specifiers,
23275 width,
23276 attributes);
23278 else
23280 cp_declarator *declarator;
23281 tree initializer;
23282 tree asm_specification;
23283 int ctor_dtor_or_conv_p;
23285 /* Parse the declarator. */
23286 declarator
23287 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23288 &ctor_dtor_or_conv_p,
23289 /*parenthesized_p=*/NULL,
23290 /*member_p=*/true,
23291 friend_p);
23293 /* If something went wrong parsing the declarator, make sure
23294 that we at least consume some tokens. */
23295 if (declarator == cp_error_declarator)
23297 /* Skip to the end of the statement. */
23298 cp_parser_skip_to_end_of_statement (parser);
23299 /* If the next token is not a semicolon, that is
23300 probably because we just skipped over the body of
23301 a function. So, we consume a semicolon if
23302 present, but do not issue an error message if it
23303 is not present. */
23304 if (cp_lexer_next_token_is (parser->lexer,
23305 CPP_SEMICOLON))
23306 cp_lexer_consume_token (parser->lexer);
23307 goto out;
23310 if (declares_class_or_enum & 2)
23311 cp_parser_check_for_definition_in_return_type
23312 (declarator, decl_specifiers.type,
23313 decl_specifiers.locations[ds_type_spec]);
23315 /* Look for an asm-specification. */
23316 asm_specification = cp_parser_asm_specification_opt (parser);
23317 /* Look for attributes that apply to the declaration. */
23318 attributes = cp_parser_attributes_opt (parser);
23319 /* Remember which attributes are prefix attributes and
23320 which are not. */
23321 first_attribute = attributes;
23322 /* Combine the attributes. */
23323 attributes = chainon (prefix_attributes, attributes);
23325 /* If it's an `=', then we have a constant-initializer or a
23326 pure-specifier. It is not correct to parse the
23327 initializer before registering the member declaration
23328 since the member declaration should be in scope while
23329 its initializer is processed. However, the rest of the
23330 front end does not yet provide an interface that allows
23331 us to handle this correctly. */
23332 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23334 /* In [class.mem]:
23336 A pure-specifier shall be used only in the declaration of
23337 a virtual function.
23339 A member-declarator can contain a constant-initializer
23340 only if it declares a static member of integral or
23341 enumeration type.
23343 Therefore, if the DECLARATOR is for a function, we look
23344 for a pure-specifier; otherwise, we look for a
23345 constant-initializer. When we call `grokfield', it will
23346 perform more stringent semantics checks. */
23347 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23348 if (function_declarator_p (declarator)
23349 || (decl_specifiers.type
23350 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23351 && declarator->kind == cdk_id
23352 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23353 == FUNCTION_TYPE)))
23354 initializer = cp_parser_pure_specifier (parser);
23355 else if (decl_specifiers.storage_class != sc_static)
23356 initializer = cp_parser_save_nsdmi (parser);
23357 else if (cxx_dialect >= cxx11)
23359 bool nonconst;
23360 /* Don't require a constant rvalue in C++11, since we
23361 might want a reference constant. We'll enforce
23362 constancy later. */
23363 cp_lexer_consume_token (parser->lexer);
23364 /* Parse the initializer. */
23365 initializer = cp_parser_initializer_clause (parser,
23366 &nonconst);
23368 else
23369 /* Parse the initializer. */
23370 initializer = cp_parser_constant_initializer (parser);
23372 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23373 && !function_declarator_p (declarator))
23375 bool x;
23376 if (decl_specifiers.storage_class != sc_static)
23377 initializer = cp_parser_save_nsdmi (parser);
23378 else
23379 initializer = cp_parser_initializer (parser, &x, &x);
23381 /* Otherwise, there is no initializer. */
23382 else
23383 initializer = NULL_TREE;
23385 /* See if we are probably looking at a function
23386 definition. We are certainly not looking at a
23387 member-declarator. Calling `grokfield' has
23388 side-effects, so we must not do it unless we are sure
23389 that we are looking at a member-declarator. */
23390 if (cp_parser_token_starts_function_definition_p
23391 (cp_lexer_peek_token (parser->lexer)))
23393 /* The grammar does not allow a pure-specifier to be
23394 used when a member function is defined. (It is
23395 possible that this fact is an oversight in the
23396 standard, since a pure function may be defined
23397 outside of the class-specifier. */
23398 if (initializer && initializer_token_start)
23399 error_at (initializer_token_start->location,
23400 "pure-specifier on function-definition");
23401 decl = cp_parser_save_member_function_body (parser,
23402 &decl_specifiers,
23403 declarator,
23404 attributes);
23405 if (parser->fully_implicit_function_template_p)
23406 decl = finish_fully_implicit_template (parser, decl);
23407 /* If the member was not a friend, declare it here. */
23408 if (!friend_p)
23409 finish_member_declaration (decl);
23410 /* Peek at the next token. */
23411 token = cp_lexer_peek_token (parser->lexer);
23412 /* If the next token is a semicolon, consume it. */
23413 if (token->type == CPP_SEMICOLON)
23415 location_t semicolon_loc
23416 = cp_lexer_consume_token (parser->lexer)->location;
23417 gcc_rich_location richloc (semicolon_loc);
23418 richloc.add_fixit_remove ();
23419 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23420 "extra %<;%> after in-class "
23421 "function definition");
23423 goto out;
23425 else
23426 if (declarator->kind == cdk_function)
23427 declarator->id_loc = token->location;
23428 /* Create the declaration. */
23429 decl = grokfield (declarator, &decl_specifiers,
23430 initializer, /*init_const_expr_p=*/true,
23431 asm_specification, attributes);
23432 if (parser->fully_implicit_function_template_p)
23434 if (friend_p)
23435 finish_fully_implicit_template (parser, 0);
23436 else
23437 decl = finish_fully_implicit_template (parser, decl);
23441 cp_finalize_omp_declare_simd (parser, decl);
23442 cp_finalize_oacc_routine (parser, decl, false);
23444 /* Reset PREFIX_ATTRIBUTES. */
23445 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23446 attributes = TREE_CHAIN (attributes);
23447 if (attributes)
23448 TREE_CHAIN (attributes) = NULL_TREE;
23450 /* If there is any qualification still in effect, clear it
23451 now; we will be starting fresh with the next declarator. */
23452 parser->scope = NULL_TREE;
23453 parser->qualifying_scope = NULL_TREE;
23454 parser->object_scope = NULL_TREE;
23455 /* If it's a `,', then there are more declarators. */
23456 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23458 cp_lexer_consume_token (parser->lexer);
23459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23461 cp_token *token = cp_lexer_previous_token (parser->lexer);
23462 gcc_rich_location richloc (token->location);
23463 richloc.add_fixit_remove ();
23464 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23465 "member declaration");
23468 /* If the next token isn't a `;', then we have a parse error. */
23469 else if (cp_lexer_next_token_is_not (parser->lexer,
23470 CPP_SEMICOLON))
23472 /* The next token might be a ways away from where the
23473 actual semicolon is missing. Find the previous token
23474 and use that for our error position. */
23475 cp_token *token = cp_lexer_previous_token (parser->lexer);
23476 gcc_rich_location richloc (token->location);
23477 richloc.add_fixit_insert_after (";");
23478 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23479 "member declaration");
23481 /* Assume that the user meant to provide a semicolon. If
23482 we were to cp_parser_skip_to_end_of_statement, we might
23483 skip to a semicolon inside a member function definition
23484 and issue nonsensical error messages. */
23485 assume_semicolon = true;
23488 if (decl)
23490 /* Add DECL to the list of members. */
23491 if (!friend_p
23492 /* Explicitly include, eg, NSDMIs, for better error
23493 recovery (c++/58650). */
23494 || !DECL_DECLARES_FUNCTION_P (decl))
23495 finish_member_declaration (decl);
23497 if (TREE_CODE (decl) == FUNCTION_DECL)
23498 cp_parser_save_default_args (parser, decl);
23499 else if (TREE_CODE (decl) == FIELD_DECL
23500 && !DECL_C_BIT_FIELD (decl)
23501 && DECL_INITIAL (decl))
23502 /* Add DECL to the queue of NSDMI to be parsed later. */
23503 vec_safe_push (unparsed_nsdmis, decl);
23506 if (assume_semicolon)
23507 goto out;
23511 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23512 out:
23513 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23516 /* Parse a pure-specifier.
23518 pure-specifier:
23521 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23522 Otherwise, ERROR_MARK_NODE is returned. */
23524 static tree
23525 cp_parser_pure_specifier (cp_parser* parser)
23527 cp_token *token;
23529 /* Look for the `=' token. */
23530 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23531 return error_mark_node;
23532 /* Look for the `0' token. */
23533 token = cp_lexer_peek_token (parser->lexer);
23535 if (token->type == CPP_EOF
23536 || token->type == CPP_PRAGMA_EOL)
23537 return error_mark_node;
23539 cp_lexer_consume_token (parser->lexer);
23541 /* Accept = default or = delete in c++0x mode. */
23542 if (token->keyword == RID_DEFAULT
23543 || token->keyword == RID_DELETE)
23545 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23546 return token->u.value;
23549 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23550 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23552 cp_parser_error (parser,
23553 "invalid pure specifier (only %<= 0%> is allowed)");
23554 cp_parser_skip_to_end_of_statement (parser);
23555 return error_mark_node;
23557 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23559 error_at (token->location, "templates may not be %<virtual%>");
23560 return error_mark_node;
23563 return integer_zero_node;
23566 /* Parse a constant-initializer.
23568 constant-initializer:
23569 = constant-expression
23571 Returns a representation of the constant-expression. */
23573 static tree
23574 cp_parser_constant_initializer (cp_parser* parser)
23576 /* Look for the `=' token. */
23577 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23578 return error_mark_node;
23580 /* It is invalid to write:
23582 struct S { static const int i = { 7 }; };
23585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23587 cp_parser_error (parser,
23588 "a brace-enclosed initializer is not allowed here");
23589 /* Consume the opening brace. */
23590 cp_lexer_consume_token (parser->lexer);
23591 /* Skip the initializer. */
23592 cp_parser_skip_to_closing_brace (parser);
23593 /* Look for the trailing `}'. */
23594 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23596 return error_mark_node;
23599 return cp_parser_constant_expression (parser);
23602 /* Derived classes [gram.class.derived] */
23604 /* Parse a base-clause.
23606 base-clause:
23607 : base-specifier-list
23609 base-specifier-list:
23610 base-specifier ... [opt]
23611 base-specifier-list , base-specifier ... [opt]
23613 Returns a TREE_LIST representing the base-classes, in the order in
23614 which they were declared. The representation of each node is as
23615 described by cp_parser_base_specifier.
23617 In the case that no bases are specified, this function will return
23618 NULL_TREE, not ERROR_MARK_NODE. */
23620 static tree
23621 cp_parser_base_clause (cp_parser* parser)
23623 tree bases = NULL_TREE;
23625 /* Look for the `:' that begins the list. */
23626 cp_parser_require (parser, CPP_COLON, RT_COLON);
23628 /* Scan the base-specifier-list. */
23629 while (true)
23631 cp_token *token;
23632 tree base;
23633 bool pack_expansion_p = false;
23635 /* Look for the base-specifier. */
23636 base = cp_parser_base_specifier (parser);
23637 /* Look for the (optional) ellipsis. */
23638 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23640 /* Consume the `...'. */
23641 cp_lexer_consume_token (parser->lexer);
23643 pack_expansion_p = true;
23646 /* Add BASE to the front of the list. */
23647 if (base && base != error_mark_node)
23649 if (pack_expansion_p)
23650 /* Make this a pack expansion type. */
23651 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23653 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23655 TREE_CHAIN (base) = bases;
23656 bases = base;
23659 /* Peek at the next token. */
23660 token = cp_lexer_peek_token (parser->lexer);
23661 /* If it's not a comma, then the list is complete. */
23662 if (token->type != CPP_COMMA)
23663 break;
23664 /* Consume the `,'. */
23665 cp_lexer_consume_token (parser->lexer);
23668 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23669 base class had a qualified name. However, the next name that
23670 appears is certainly not qualified. */
23671 parser->scope = NULL_TREE;
23672 parser->qualifying_scope = NULL_TREE;
23673 parser->object_scope = NULL_TREE;
23675 return nreverse (bases);
23678 /* Parse a base-specifier.
23680 base-specifier:
23681 :: [opt] nested-name-specifier [opt] class-name
23682 virtual access-specifier [opt] :: [opt] nested-name-specifier
23683 [opt] class-name
23684 access-specifier virtual [opt] :: [opt] nested-name-specifier
23685 [opt] class-name
23687 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23688 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23689 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23690 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23692 static tree
23693 cp_parser_base_specifier (cp_parser* parser)
23695 cp_token *token;
23696 bool done = false;
23697 bool virtual_p = false;
23698 bool duplicate_virtual_error_issued_p = false;
23699 bool duplicate_access_error_issued_p = false;
23700 bool class_scope_p, template_p;
23701 tree access = access_default_node;
23702 tree type;
23704 /* Process the optional `virtual' and `access-specifier'. */
23705 while (!done)
23707 /* Peek at the next token. */
23708 token = cp_lexer_peek_token (parser->lexer);
23709 /* Process `virtual'. */
23710 switch (token->keyword)
23712 case RID_VIRTUAL:
23713 /* If `virtual' appears more than once, issue an error. */
23714 if (virtual_p && !duplicate_virtual_error_issued_p)
23716 cp_parser_error (parser,
23717 "%<virtual%> specified more than once in base-specified");
23718 duplicate_virtual_error_issued_p = true;
23721 virtual_p = true;
23723 /* Consume the `virtual' token. */
23724 cp_lexer_consume_token (parser->lexer);
23726 break;
23728 case RID_PUBLIC:
23729 case RID_PROTECTED:
23730 case RID_PRIVATE:
23731 /* If more than one access specifier appears, issue an
23732 error. */
23733 if (access != access_default_node
23734 && !duplicate_access_error_issued_p)
23736 cp_parser_error (parser,
23737 "more than one access specifier in base-specified");
23738 duplicate_access_error_issued_p = true;
23741 access = ridpointers[(int) token->keyword];
23743 /* Consume the access-specifier. */
23744 cp_lexer_consume_token (parser->lexer);
23746 break;
23748 default:
23749 done = true;
23750 break;
23753 /* It is not uncommon to see programs mechanically, erroneously, use
23754 the 'typename' keyword to denote (dependent) qualified types
23755 as base classes. */
23756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23758 token = cp_lexer_peek_token (parser->lexer);
23759 if (!processing_template_decl)
23760 error_at (token->location,
23761 "keyword %<typename%> not allowed outside of templates");
23762 else
23763 error_at (token->location,
23764 "keyword %<typename%> not allowed in this context "
23765 "(the base class is implicitly a type)");
23766 cp_lexer_consume_token (parser->lexer);
23769 /* Look for the optional `::' operator. */
23770 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23771 /* Look for the nested-name-specifier. The simplest way to
23772 implement:
23774 [temp.res]
23776 The keyword `typename' is not permitted in a base-specifier or
23777 mem-initializer; in these contexts a qualified name that
23778 depends on a template-parameter is implicitly assumed to be a
23779 type name.
23781 is to pretend that we have seen the `typename' keyword at this
23782 point. */
23783 cp_parser_nested_name_specifier_opt (parser,
23784 /*typename_keyword_p=*/true,
23785 /*check_dependency_p=*/true,
23786 /*type_p=*/true,
23787 /*is_declaration=*/true);
23788 /* If the base class is given by a qualified name, assume that names
23789 we see are type names or templates, as appropriate. */
23790 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23791 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23793 if (!parser->scope
23794 && cp_lexer_next_token_is_decltype (parser->lexer))
23795 /* DR 950 allows decltype as a base-specifier. */
23796 type = cp_parser_decltype (parser);
23797 else
23799 /* Otherwise, look for the class-name. */
23800 type = cp_parser_class_name (parser,
23801 class_scope_p,
23802 template_p,
23803 typename_type,
23804 /*check_dependency_p=*/true,
23805 /*class_head_p=*/false,
23806 /*is_declaration=*/true);
23807 type = TREE_TYPE (type);
23810 if (type == error_mark_node)
23811 return error_mark_node;
23813 return finish_base_specifier (type, access, virtual_p);
23816 /* Exception handling [gram.exception] */
23818 /* Parse an (optional) noexcept-specification.
23820 noexcept-specification:
23821 noexcept ( constant-expression ) [opt]
23823 If no noexcept-specification is present, returns NULL_TREE.
23824 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23825 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23826 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23827 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23828 in which case a boolean condition is returned instead. */
23830 static tree
23831 cp_parser_noexcept_specification_opt (cp_parser* parser,
23832 bool require_constexpr,
23833 bool* consumed_expr,
23834 bool return_cond)
23836 cp_token *token;
23837 const char *saved_message;
23839 /* Peek at the next token. */
23840 token = cp_lexer_peek_token (parser->lexer);
23842 /* Is it a noexcept-specification? */
23843 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23845 tree expr;
23846 cp_lexer_consume_token (parser->lexer);
23848 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23850 cp_lexer_consume_token (parser->lexer);
23852 if (require_constexpr)
23854 /* Types may not be defined in an exception-specification. */
23855 saved_message = parser->type_definition_forbidden_message;
23856 parser->type_definition_forbidden_message
23857 = G_("types may not be defined in an exception-specification");
23859 expr = cp_parser_constant_expression (parser);
23861 /* Restore the saved message. */
23862 parser->type_definition_forbidden_message = saved_message;
23864 else
23866 expr = cp_parser_expression (parser);
23867 *consumed_expr = true;
23870 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23872 else
23874 expr = boolean_true_node;
23875 if (!require_constexpr)
23876 *consumed_expr = false;
23879 /* We cannot build a noexcept-spec right away because this will check
23880 that expr is a constexpr. */
23881 if (!return_cond)
23882 return build_noexcept_spec (expr, tf_warning_or_error);
23883 else
23884 return expr;
23886 else
23887 return NULL_TREE;
23890 /* Parse an (optional) exception-specification.
23892 exception-specification:
23893 throw ( type-id-list [opt] )
23895 Returns a TREE_LIST representing the exception-specification. The
23896 TREE_VALUE of each node is a type. */
23898 static tree
23899 cp_parser_exception_specification_opt (cp_parser* parser)
23901 cp_token *token;
23902 tree type_id_list;
23903 const char *saved_message;
23905 /* Peek at the next token. */
23906 token = cp_lexer_peek_token (parser->lexer);
23908 /* Is it a noexcept-specification? */
23909 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23910 false);
23911 if (type_id_list != NULL_TREE)
23912 return type_id_list;
23914 /* If it's not `throw', then there's no exception-specification. */
23915 if (!cp_parser_is_keyword (token, RID_THROW))
23916 return NULL_TREE;
23918 location_t loc = token->location;
23920 /* Consume the `throw'. */
23921 cp_lexer_consume_token (parser->lexer);
23923 /* Look for the `('. */
23924 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23926 /* Peek at the next token. */
23927 token = cp_lexer_peek_token (parser->lexer);
23928 /* If it's not a `)', then there is a type-id-list. */
23929 if (token->type != CPP_CLOSE_PAREN)
23931 /* Types may not be defined in an exception-specification. */
23932 saved_message = parser->type_definition_forbidden_message;
23933 parser->type_definition_forbidden_message
23934 = G_("types may not be defined in an exception-specification");
23935 /* Parse the type-id-list. */
23936 type_id_list = cp_parser_type_id_list (parser);
23937 /* Restore the saved message. */
23938 parser->type_definition_forbidden_message = saved_message;
23940 if (cxx_dialect >= cxx1z)
23942 error_at (loc, "ISO C++1z does not allow dynamic exception "
23943 "specifications");
23944 type_id_list = NULL_TREE;
23946 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23947 warning_at (loc, OPT_Wdeprecated,
23948 "dynamic exception specifications are deprecated in "
23949 "C++11");
23951 /* In C++17, throw() is equivalent to noexcept (true). throw()
23952 is deprecated in C++11 and above as well, but is still widely used,
23953 so don't warn about it yet. */
23954 else if (cxx_dialect >= cxx1z)
23955 type_id_list = noexcept_true_spec;
23956 else
23957 type_id_list = empty_except_spec;
23959 /* Look for the `)'. */
23960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23962 return type_id_list;
23965 /* Parse an (optional) type-id-list.
23967 type-id-list:
23968 type-id ... [opt]
23969 type-id-list , type-id ... [opt]
23971 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23972 in the order that the types were presented. */
23974 static tree
23975 cp_parser_type_id_list (cp_parser* parser)
23977 tree types = NULL_TREE;
23979 while (true)
23981 cp_token *token;
23982 tree type;
23984 token = cp_lexer_peek_token (parser->lexer);
23986 /* Get the next type-id. */
23987 type = cp_parser_type_id (parser);
23988 /* Check for invalid 'auto'. */
23989 if (flag_concepts && type_uses_auto (type))
23991 error_at (token->location,
23992 "invalid use of %<auto%> in exception-specification");
23993 type = error_mark_node;
23995 /* Parse the optional ellipsis. */
23996 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23998 /* Consume the `...'. */
23999 cp_lexer_consume_token (parser->lexer);
24001 /* Turn the type into a pack expansion expression. */
24002 type = make_pack_expansion (type);
24004 /* Add it to the list. */
24005 types = add_exception_specifier (types, type, /*complain=*/1);
24006 /* Peek at the next token. */
24007 token = cp_lexer_peek_token (parser->lexer);
24008 /* If it is not a `,', we are done. */
24009 if (token->type != CPP_COMMA)
24010 break;
24011 /* Consume the `,'. */
24012 cp_lexer_consume_token (parser->lexer);
24015 return nreverse (types);
24018 /* Parse a try-block.
24020 try-block:
24021 try compound-statement handler-seq */
24023 static tree
24024 cp_parser_try_block (cp_parser* parser)
24026 tree try_block;
24028 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24029 if (parser->in_function_body
24030 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24031 error ("%<try%> in %<constexpr%> function");
24033 try_block = begin_try_block ();
24034 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24035 finish_try_block (try_block);
24036 cp_parser_handler_seq (parser);
24037 finish_handler_sequence (try_block);
24039 return try_block;
24042 /* Parse a function-try-block.
24044 function-try-block:
24045 try ctor-initializer [opt] function-body handler-seq */
24047 static bool
24048 cp_parser_function_try_block (cp_parser* parser)
24050 tree compound_stmt;
24051 tree try_block;
24052 bool ctor_initializer_p;
24054 /* Look for the `try' keyword. */
24055 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24056 return false;
24057 /* Let the rest of the front end know where we are. */
24058 try_block = begin_function_try_block (&compound_stmt);
24059 /* Parse the function-body. */
24060 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24061 (parser, /*in_function_try_block=*/true);
24062 /* We're done with the `try' part. */
24063 finish_function_try_block (try_block);
24064 /* Parse the handlers. */
24065 cp_parser_handler_seq (parser);
24066 /* We're done with the handlers. */
24067 finish_function_handler_sequence (try_block, compound_stmt);
24069 return ctor_initializer_p;
24072 /* Parse a handler-seq.
24074 handler-seq:
24075 handler handler-seq [opt] */
24077 static void
24078 cp_parser_handler_seq (cp_parser* parser)
24080 while (true)
24082 cp_token *token;
24084 /* Parse the handler. */
24085 cp_parser_handler (parser);
24086 /* Peek at the next token. */
24087 token = cp_lexer_peek_token (parser->lexer);
24088 /* If it's not `catch' then there are no more handlers. */
24089 if (!cp_parser_is_keyword (token, RID_CATCH))
24090 break;
24094 /* Parse a handler.
24096 handler:
24097 catch ( exception-declaration ) compound-statement */
24099 static void
24100 cp_parser_handler (cp_parser* parser)
24102 tree handler;
24103 tree declaration;
24105 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24106 handler = begin_handler ();
24107 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24108 declaration = cp_parser_exception_declaration (parser);
24109 finish_handler_parms (declaration, handler);
24110 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24111 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24112 finish_handler (handler);
24115 /* Parse an exception-declaration.
24117 exception-declaration:
24118 type-specifier-seq declarator
24119 type-specifier-seq abstract-declarator
24120 type-specifier-seq
24123 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24124 ellipsis variant is used. */
24126 static tree
24127 cp_parser_exception_declaration (cp_parser* parser)
24129 cp_decl_specifier_seq type_specifiers;
24130 cp_declarator *declarator;
24131 const char *saved_message;
24133 /* If it's an ellipsis, it's easy to handle. */
24134 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24136 /* Consume the `...' token. */
24137 cp_lexer_consume_token (parser->lexer);
24138 return NULL_TREE;
24141 /* Types may not be defined in exception-declarations. */
24142 saved_message = parser->type_definition_forbidden_message;
24143 parser->type_definition_forbidden_message
24144 = G_("types may not be defined in exception-declarations");
24146 /* Parse the type-specifier-seq. */
24147 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24148 /*is_trailing_return=*/false,
24149 &type_specifiers);
24150 /* If it's a `)', then there is no declarator. */
24151 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24152 declarator = NULL;
24153 else
24154 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24155 /*ctor_dtor_or_conv_p=*/NULL,
24156 /*parenthesized_p=*/NULL,
24157 /*member_p=*/false,
24158 /*friend_p=*/false);
24160 /* Restore the saved message. */
24161 parser->type_definition_forbidden_message = saved_message;
24163 if (!type_specifiers.any_specifiers_p)
24164 return error_mark_node;
24166 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24169 /* Parse a throw-expression.
24171 throw-expression:
24172 throw assignment-expression [opt]
24174 Returns a THROW_EXPR representing the throw-expression. */
24176 static tree
24177 cp_parser_throw_expression (cp_parser* parser)
24179 tree expression;
24180 cp_token* token;
24182 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24183 token = cp_lexer_peek_token (parser->lexer);
24184 /* Figure out whether or not there is an assignment-expression
24185 following the "throw" keyword. */
24186 if (token->type == CPP_COMMA
24187 || token->type == CPP_SEMICOLON
24188 || token->type == CPP_CLOSE_PAREN
24189 || token->type == CPP_CLOSE_SQUARE
24190 || token->type == CPP_CLOSE_BRACE
24191 || token->type == CPP_COLON)
24192 expression = NULL_TREE;
24193 else
24194 expression = cp_parser_assignment_expression (parser);
24196 return build_throw (expression);
24199 /* GNU Extensions */
24201 /* Parse an (optional) asm-specification.
24203 asm-specification:
24204 asm ( string-literal )
24206 If the asm-specification is present, returns a STRING_CST
24207 corresponding to the string-literal. Otherwise, returns
24208 NULL_TREE. */
24210 static tree
24211 cp_parser_asm_specification_opt (cp_parser* parser)
24213 cp_token *token;
24214 tree asm_specification;
24216 /* Peek at the next token. */
24217 token = cp_lexer_peek_token (parser->lexer);
24218 /* If the next token isn't the `asm' keyword, then there's no
24219 asm-specification. */
24220 if (!cp_parser_is_keyword (token, RID_ASM))
24221 return NULL_TREE;
24223 /* Consume the `asm' token. */
24224 cp_lexer_consume_token (parser->lexer);
24225 /* Look for the `('. */
24226 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24228 /* Look for the string-literal. */
24229 asm_specification = cp_parser_string_literal (parser, false, false);
24231 /* Look for the `)'. */
24232 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24234 return asm_specification;
24237 /* Parse an asm-operand-list.
24239 asm-operand-list:
24240 asm-operand
24241 asm-operand-list , asm-operand
24243 asm-operand:
24244 string-literal ( expression )
24245 [ string-literal ] string-literal ( expression )
24247 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24248 each node is the expression. The TREE_PURPOSE is itself a
24249 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24250 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24251 is a STRING_CST for the string literal before the parenthesis. Returns
24252 ERROR_MARK_NODE if any of the operands are invalid. */
24254 static tree
24255 cp_parser_asm_operand_list (cp_parser* parser)
24257 tree asm_operands = NULL_TREE;
24258 bool invalid_operands = false;
24260 while (true)
24262 tree string_literal;
24263 tree expression;
24264 tree name;
24266 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24268 /* Consume the `[' token. */
24269 cp_lexer_consume_token (parser->lexer);
24270 /* Read the operand name. */
24271 name = cp_parser_identifier (parser);
24272 if (name != error_mark_node)
24273 name = build_string (IDENTIFIER_LENGTH (name),
24274 IDENTIFIER_POINTER (name));
24275 /* Look for the closing `]'. */
24276 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24278 else
24279 name = NULL_TREE;
24280 /* Look for the string-literal. */
24281 string_literal = cp_parser_string_literal (parser, false, false);
24283 /* Look for the `('. */
24284 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24285 /* Parse the expression. */
24286 expression = cp_parser_expression (parser);
24287 /* Look for the `)'. */
24288 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24290 if (name == error_mark_node
24291 || string_literal == error_mark_node
24292 || expression == error_mark_node)
24293 invalid_operands = true;
24295 /* Add this operand to the list. */
24296 asm_operands = tree_cons (build_tree_list (name, string_literal),
24297 expression,
24298 asm_operands);
24299 /* If the next token is not a `,', there are no more
24300 operands. */
24301 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24302 break;
24303 /* Consume the `,'. */
24304 cp_lexer_consume_token (parser->lexer);
24307 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24310 /* Parse an asm-clobber-list.
24312 asm-clobber-list:
24313 string-literal
24314 asm-clobber-list , string-literal
24316 Returns a TREE_LIST, indicating the clobbers in the order that they
24317 appeared. The TREE_VALUE of each node is a STRING_CST. */
24319 static tree
24320 cp_parser_asm_clobber_list (cp_parser* parser)
24322 tree clobbers = NULL_TREE;
24324 while (true)
24326 tree string_literal;
24328 /* Look for the string literal. */
24329 string_literal = cp_parser_string_literal (parser, false, false);
24330 /* Add it to the list. */
24331 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24332 /* If the next token is not a `,', then the list is
24333 complete. */
24334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24335 break;
24336 /* Consume the `,' token. */
24337 cp_lexer_consume_token (parser->lexer);
24340 return clobbers;
24343 /* Parse an asm-label-list.
24345 asm-label-list:
24346 identifier
24347 asm-label-list , identifier
24349 Returns a TREE_LIST, indicating the labels in the order that they
24350 appeared. The TREE_VALUE of each node is a label. */
24352 static tree
24353 cp_parser_asm_label_list (cp_parser* parser)
24355 tree labels = NULL_TREE;
24357 while (true)
24359 tree identifier, label, name;
24361 /* Look for the identifier. */
24362 identifier = cp_parser_identifier (parser);
24363 if (!error_operand_p (identifier))
24365 label = lookup_label (identifier);
24366 if (TREE_CODE (label) == LABEL_DECL)
24368 TREE_USED (label) = 1;
24369 check_goto (label);
24370 name = build_string (IDENTIFIER_LENGTH (identifier),
24371 IDENTIFIER_POINTER (identifier));
24372 labels = tree_cons (name, label, labels);
24375 /* If the next token is not a `,', then the list is
24376 complete. */
24377 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24378 break;
24379 /* Consume the `,' token. */
24380 cp_lexer_consume_token (parser->lexer);
24383 return nreverse (labels);
24386 /* Return TRUE iff the next tokens in the stream are possibly the
24387 beginning of a GNU extension attribute. */
24389 static bool
24390 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24392 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24395 /* Return TRUE iff the next tokens in the stream are possibly the
24396 beginning of a standard C++-11 attribute specifier. */
24398 static bool
24399 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24401 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24404 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24405 beginning of a standard C++-11 attribute specifier. */
24407 static bool
24408 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24410 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24412 return (cxx_dialect >= cxx11
24413 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24414 || (token->type == CPP_OPEN_SQUARE
24415 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24416 && token->type == CPP_OPEN_SQUARE)));
24419 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24420 beginning of a GNU extension attribute. */
24422 static bool
24423 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24425 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24427 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24430 /* Return true iff the next tokens can be the beginning of either a
24431 GNU attribute list, or a standard C++11 attribute sequence. */
24433 static bool
24434 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24436 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24437 || cp_next_tokens_can_be_std_attribute_p (parser));
24440 /* Return true iff the next Nth tokens can be the beginning of either
24441 a GNU attribute list, or a standard C++11 attribute sequence. */
24443 static bool
24444 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24446 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24447 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24450 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24451 of GNU attributes, or return NULL. */
24453 static tree
24454 cp_parser_attributes_opt (cp_parser *parser)
24456 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24457 return cp_parser_gnu_attributes_opt (parser);
24458 return cp_parser_std_attribute_spec_seq (parser);
24461 #define CILK_SIMD_FN_CLAUSE_MASK \
24462 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24463 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24464 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24465 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24466 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24468 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24469 vector [(<clauses>)] */
24471 static void
24472 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24474 bool first_p = parser->cilk_simd_fn_info == NULL;
24475 cp_token *token = v_token;
24476 if (first_p)
24478 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24479 parser->cilk_simd_fn_info->error_seen = false;
24480 parser->cilk_simd_fn_info->fndecl_seen = false;
24481 parser->cilk_simd_fn_info->tokens = vNULL;
24482 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24484 int paren_scope = 0;
24485 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24487 cp_lexer_consume_token (parser->lexer);
24488 v_token = cp_lexer_peek_token (parser->lexer);
24489 paren_scope++;
24491 while (paren_scope > 0)
24493 token = cp_lexer_peek_token (parser->lexer);
24494 if (token->type == CPP_OPEN_PAREN)
24495 paren_scope++;
24496 else if (token->type == CPP_CLOSE_PAREN)
24497 paren_scope--;
24498 /* Do not push the last ')' */
24499 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24500 cp_lexer_consume_token (parser->lexer);
24503 token->type = CPP_PRAGMA_EOL;
24504 parser->lexer->next_token = token;
24505 cp_lexer_consume_token (parser->lexer);
24507 struct cp_token_cache *cp
24508 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24509 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24512 /* Parse an (optional) series of attributes.
24514 attributes:
24515 attributes attribute
24517 attribute:
24518 __attribute__ (( attribute-list [opt] ))
24520 The return value is as for cp_parser_gnu_attribute_list. */
24522 static tree
24523 cp_parser_gnu_attributes_opt (cp_parser* parser)
24525 tree attributes = NULL_TREE;
24527 while (true)
24529 cp_token *token;
24530 tree attribute_list;
24531 bool ok = true;
24533 /* Peek at the next token. */
24534 token = cp_lexer_peek_token (parser->lexer);
24535 /* If it's not `__attribute__', then we're done. */
24536 if (token->keyword != RID_ATTRIBUTE)
24537 break;
24539 /* Consume the `__attribute__' keyword. */
24540 cp_lexer_consume_token (parser->lexer);
24541 /* Look for the two `(' tokens. */
24542 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24543 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24545 /* Peek at the next token. */
24546 token = cp_lexer_peek_token (parser->lexer);
24547 if (token->type != CPP_CLOSE_PAREN)
24548 /* Parse the attribute-list. */
24549 attribute_list = cp_parser_gnu_attribute_list (parser);
24550 else
24551 /* If the next token is a `)', then there is no attribute
24552 list. */
24553 attribute_list = NULL;
24555 /* Look for the two `)' tokens. */
24556 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24557 ok = false;
24558 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24559 ok = false;
24560 if (!ok)
24561 cp_parser_skip_to_end_of_statement (parser);
24563 /* Add these new attributes to the list. */
24564 attributes = chainon (attributes, attribute_list);
24567 return attributes;
24570 /* Parse a GNU attribute-list.
24572 attribute-list:
24573 attribute
24574 attribute-list , attribute
24576 attribute:
24577 identifier
24578 identifier ( identifier )
24579 identifier ( identifier , expression-list )
24580 identifier ( expression-list )
24582 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24583 to an attribute. The TREE_PURPOSE of each node is the identifier
24584 indicating which attribute is in use. The TREE_VALUE represents
24585 the arguments, if any. */
24587 static tree
24588 cp_parser_gnu_attribute_list (cp_parser* parser)
24590 tree attribute_list = NULL_TREE;
24591 bool save_translate_strings_p = parser->translate_strings_p;
24593 parser->translate_strings_p = false;
24594 while (true)
24596 cp_token *token;
24597 tree identifier;
24598 tree attribute;
24600 /* Look for the identifier. We also allow keywords here; for
24601 example `__attribute__ ((const))' is legal. */
24602 token = cp_lexer_peek_token (parser->lexer);
24603 if (token->type == CPP_NAME
24604 || token->type == CPP_KEYWORD)
24606 tree arguments = NULL_TREE;
24608 /* Consume the token, but save it since we need it for the
24609 SIMD enabled function parsing. */
24610 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24612 /* Save away the identifier that indicates which attribute
24613 this is. */
24614 identifier = (token->type == CPP_KEYWORD)
24615 /* For keywords, use the canonical spelling, not the
24616 parsed identifier. */
24617 ? ridpointers[(int) token->keyword]
24618 : id_token->u.value;
24620 attribute = build_tree_list (identifier, NULL_TREE);
24622 /* Peek at the next token. */
24623 token = cp_lexer_peek_token (parser->lexer);
24624 /* If it's an `(', then parse the attribute arguments. */
24625 if (token->type == CPP_OPEN_PAREN)
24627 vec<tree, va_gc> *vec;
24628 int attr_flag = (attribute_takes_identifier_p (identifier)
24629 ? id_attr : normal_attr);
24630 if (is_cilkplus_vector_p (identifier))
24632 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24633 continue;
24635 else
24636 vec = cp_parser_parenthesized_expression_list
24637 (parser, attr_flag, /*cast_p=*/false,
24638 /*allow_expansion_p=*/false,
24639 /*non_constant_p=*/NULL);
24640 if (vec == NULL)
24641 arguments = error_mark_node;
24642 else
24644 arguments = build_tree_list_vec (vec);
24645 release_tree_vector (vec);
24647 /* Save the arguments away. */
24648 TREE_VALUE (attribute) = arguments;
24650 else if (is_cilkplus_vector_p (identifier))
24652 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24653 continue;
24656 if (arguments != error_mark_node)
24658 /* Add this attribute to the list. */
24659 TREE_CHAIN (attribute) = attribute_list;
24660 attribute_list = attribute;
24663 token = cp_lexer_peek_token (parser->lexer);
24665 /* Now, look for more attributes. If the next token isn't a
24666 `,', we're done. */
24667 if (token->type != CPP_COMMA)
24668 break;
24670 /* Consume the comma and keep going. */
24671 cp_lexer_consume_token (parser->lexer);
24673 parser->translate_strings_p = save_translate_strings_p;
24675 /* We built up the list in reverse order. */
24676 return nreverse (attribute_list);
24679 /* Parse a standard C++11 attribute.
24681 The returned representation is a TREE_LIST which TREE_PURPOSE is
24682 the scoped name of the attribute, and the TREE_VALUE is its
24683 arguments list.
24685 Note that the scoped name of the attribute is itself a TREE_LIST
24686 which TREE_PURPOSE is the namespace of the attribute, and
24687 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24688 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24689 and which TREE_PURPOSE is directly the attribute name.
24691 Clients of the attribute code should use get_attribute_namespace
24692 and get_attribute_name to get the actual namespace and name of
24693 attributes, regardless of their being GNU or C++11 attributes.
24695 attribute:
24696 attribute-token attribute-argument-clause [opt]
24698 attribute-token:
24699 identifier
24700 attribute-scoped-token
24702 attribute-scoped-token:
24703 attribute-namespace :: identifier
24705 attribute-namespace:
24706 identifier
24708 attribute-argument-clause:
24709 ( balanced-token-seq )
24711 balanced-token-seq:
24712 balanced-token [opt]
24713 balanced-token-seq balanced-token
24715 balanced-token:
24716 ( balanced-token-seq )
24717 [ balanced-token-seq ]
24718 { balanced-token-seq }. */
24720 static tree
24721 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24723 tree attribute, attr_id = NULL_TREE, arguments;
24724 cp_token *token;
24726 /* First, parse name of the attribute, a.k.a attribute-token. */
24728 token = cp_lexer_peek_token (parser->lexer);
24729 if (token->type == CPP_NAME)
24730 attr_id = token->u.value;
24731 else if (token->type == CPP_KEYWORD)
24732 attr_id = ridpointers[(int) token->keyword];
24733 else if (token->flags & NAMED_OP)
24734 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24736 if (attr_id == NULL_TREE)
24737 return NULL_TREE;
24739 cp_lexer_consume_token (parser->lexer);
24741 token = cp_lexer_peek_token (parser->lexer);
24742 if (token->type == CPP_SCOPE)
24744 /* We are seeing a scoped attribute token. */
24746 cp_lexer_consume_token (parser->lexer);
24747 if (attr_ns)
24748 error_at (token->location, "attribute using prefix used together "
24749 "with scoped attribute token");
24750 attr_ns = attr_id;
24752 token = cp_lexer_consume_token (parser->lexer);
24753 if (token->type == CPP_NAME)
24754 attr_id = token->u.value;
24755 else if (token->type == CPP_KEYWORD)
24756 attr_id = ridpointers[(int) token->keyword];
24757 else if (token->flags & NAMED_OP)
24758 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24759 else
24761 error_at (token->location,
24762 "expected an identifier for the attribute name");
24763 return error_mark_node;
24765 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24766 NULL_TREE);
24767 token = cp_lexer_peek_token (parser->lexer);
24769 else if (attr_ns)
24770 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24771 NULL_TREE);
24772 else
24774 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24775 NULL_TREE);
24776 /* C++11 noreturn attribute is equivalent to GNU's. */
24777 if (is_attribute_p ("noreturn", attr_id))
24778 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24779 /* C++14 deprecated attribute is equivalent to GNU's. */
24780 else if (is_attribute_p ("deprecated", attr_id))
24781 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24782 /* C++17 fallthrough attribute is equivalent to GNU's. */
24783 else if (is_attribute_p ("fallthrough", attr_id))
24784 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24785 /* Transactional Memory TS optimize_for_synchronized attribute is
24786 equivalent to GNU transaction_callable. */
24787 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24788 TREE_PURPOSE (attribute)
24789 = get_identifier ("transaction_callable");
24790 /* Transactional Memory attributes are GNU attributes. */
24791 else if (tm_attr_to_mask (attr_id))
24792 TREE_PURPOSE (attribute) = attr_id;
24795 /* Now parse the optional argument clause of the attribute. */
24797 if (token->type != CPP_OPEN_PAREN)
24798 return attribute;
24801 vec<tree, va_gc> *vec;
24802 int attr_flag = normal_attr;
24804 if (attr_ns == get_identifier ("gnu")
24805 && attribute_takes_identifier_p (attr_id))
24806 /* A GNU attribute that takes an identifier in parameter. */
24807 attr_flag = id_attr;
24809 vec = cp_parser_parenthesized_expression_list
24810 (parser, attr_flag, /*cast_p=*/false,
24811 /*allow_expansion_p=*/true,
24812 /*non_constant_p=*/NULL);
24813 if (vec == NULL)
24814 arguments = error_mark_node;
24815 else
24817 arguments = build_tree_list_vec (vec);
24818 release_tree_vector (vec);
24821 if (arguments == error_mark_node)
24822 attribute = error_mark_node;
24823 else
24824 TREE_VALUE (attribute) = arguments;
24827 return attribute;
24830 /* Check that the attribute ATTRIBUTE appears at most once in the
24831 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24832 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24833 isn't implemented yet in GCC. */
24835 static void
24836 cp_parser_check_std_attribute (tree attributes, tree attribute)
24838 if (attributes)
24840 tree name = get_attribute_name (attribute);
24841 if (is_attribute_p ("noreturn", name)
24842 && lookup_attribute ("noreturn", attributes))
24843 error ("attribute %<noreturn%> can appear at most once "
24844 "in an attribute-list");
24845 else if (is_attribute_p ("deprecated", name)
24846 && lookup_attribute ("deprecated", attributes))
24847 error ("attribute %<deprecated%> can appear at most once "
24848 "in an attribute-list");
24852 /* Parse a list of standard C++-11 attributes.
24854 attribute-list:
24855 attribute [opt]
24856 attribute-list , attribute[opt]
24857 attribute ...
24858 attribute-list , attribute ...
24861 static tree
24862 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24864 tree attributes = NULL_TREE, attribute = NULL_TREE;
24865 cp_token *token = NULL;
24867 while (true)
24869 attribute = cp_parser_std_attribute (parser, attr_ns);
24870 if (attribute == error_mark_node)
24871 break;
24872 if (attribute != NULL_TREE)
24874 cp_parser_check_std_attribute (attributes, attribute);
24875 TREE_CHAIN (attribute) = attributes;
24876 attributes = attribute;
24878 token = cp_lexer_peek_token (parser->lexer);
24879 if (token->type == CPP_ELLIPSIS)
24881 cp_lexer_consume_token (parser->lexer);
24882 if (attribute == NULL_TREE)
24883 error_at (token->location,
24884 "expected attribute before %<...%>");
24885 else
24887 tree pack = make_pack_expansion (TREE_VALUE (attribute));
24888 if (pack == error_mark_node)
24889 return error_mark_node;
24890 TREE_VALUE (attribute) = pack;
24892 token = cp_lexer_peek_token (parser->lexer);
24894 if (token->type != CPP_COMMA)
24895 break;
24896 cp_lexer_consume_token (parser->lexer);
24898 attributes = nreverse (attributes);
24899 return attributes;
24902 /* Parse a standard C++-11 attribute specifier.
24904 attribute-specifier:
24905 [ [ attribute-using-prefix [opt] attribute-list ] ]
24906 alignment-specifier
24908 attribute-using-prefix:
24909 using attribute-namespace :
24911 alignment-specifier:
24912 alignas ( type-id ... [opt] )
24913 alignas ( alignment-expression ... [opt] ). */
24915 static tree
24916 cp_parser_std_attribute_spec (cp_parser *parser)
24918 tree attributes = NULL_TREE;
24919 cp_token *token = cp_lexer_peek_token (parser->lexer);
24921 if (token->type == CPP_OPEN_SQUARE
24922 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24924 tree attr_ns = NULL_TREE;
24926 cp_lexer_consume_token (parser->lexer);
24927 cp_lexer_consume_token (parser->lexer);
24929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24931 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24932 if (token->type == CPP_NAME)
24933 attr_ns = token->u.value;
24934 else if (token->type == CPP_KEYWORD)
24935 attr_ns = ridpointers[(int) token->keyword];
24936 else if (token->flags & NAMED_OP)
24937 attr_ns = get_identifier (cpp_type2name (token->type,
24938 token->flags));
24939 if (attr_ns
24940 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24942 if (cxx_dialect < cxx1z
24943 && !in_system_header_at (input_location))
24944 pedwarn (input_location, 0,
24945 "attribute using prefix only available "
24946 "with -std=c++1z or -std=gnu++1z");
24948 cp_lexer_consume_token (parser->lexer);
24949 cp_lexer_consume_token (parser->lexer);
24950 cp_lexer_consume_token (parser->lexer);
24952 else
24953 attr_ns = NULL_TREE;
24956 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24958 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24959 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24960 cp_parser_skip_to_end_of_statement (parser);
24961 else
24962 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24963 when we are sure that we have actually parsed them. */
24964 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24966 else
24968 tree alignas_expr;
24970 /* Look for an alignment-specifier. */
24972 token = cp_lexer_peek_token (parser->lexer);
24974 if (token->type != CPP_KEYWORD
24975 || token->keyword != RID_ALIGNAS)
24976 return NULL_TREE;
24978 cp_lexer_consume_token (parser->lexer);
24979 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24981 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24983 cp_parser_error (parser, "expected %<(%>");
24984 return error_mark_node;
24987 cp_parser_parse_tentatively (parser);
24988 alignas_expr = cp_parser_type_id (parser);
24990 if (!cp_parser_parse_definitely (parser))
24992 alignas_expr = cp_parser_assignment_expression (parser);
24993 if (alignas_expr == error_mark_node)
24994 cp_parser_skip_to_end_of_statement (parser);
24995 if (alignas_expr == NULL_TREE
24996 || alignas_expr == error_mark_node)
24997 return alignas_expr;
25000 alignas_expr = cxx_alignas_expr (alignas_expr);
25001 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25003 /* Handle alignas (pack...). */
25004 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25006 cp_lexer_consume_token (parser->lexer);
25007 alignas_expr = make_pack_expansion (alignas_expr);
25010 /* Something went wrong, so don't build the attribute. */
25011 if (alignas_expr == error_mark_node)
25012 return error_mark_node;
25014 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25016 cp_parser_error (parser, "expected %<)%>");
25017 return error_mark_node;
25020 /* Build the C++-11 representation of an 'aligned'
25021 attribute. */
25022 attributes =
25023 build_tree_list (build_tree_list (get_identifier ("gnu"),
25024 get_identifier ("aligned")),
25025 alignas_expr);
25028 return attributes;
25031 /* Parse a standard C++-11 attribute-specifier-seq.
25033 attribute-specifier-seq:
25034 attribute-specifier-seq [opt] attribute-specifier
25037 static tree
25038 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25040 tree attr_specs = NULL_TREE;
25041 tree attr_last = NULL_TREE;
25043 while (true)
25045 tree attr_spec = cp_parser_std_attribute_spec (parser);
25046 if (attr_spec == NULL_TREE)
25047 break;
25048 if (attr_spec == error_mark_node)
25049 return error_mark_node;
25051 if (attr_last)
25052 TREE_CHAIN (attr_last) = attr_spec;
25053 else
25054 attr_specs = attr_last = attr_spec;
25055 attr_last = tree_last (attr_last);
25058 return attr_specs;
25061 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25062 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25063 current value of the PEDANTIC flag, regardless of whether or not
25064 the `__extension__' keyword is present. The caller is responsible
25065 for restoring the value of the PEDANTIC flag. */
25067 static bool
25068 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25070 /* Save the old value of the PEDANTIC flag. */
25071 *saved_pedantic = pedantic;
25073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25075 /* Consume the `__extension__' token. */
25076 cp_lexer_consume_token (parser->lexer);
25077 /* We're not being pedantic while the `__extension__' keyword is
25078 in effect. */
25079 pedantic = 0;
25081 return true;
25084 return false;
25087 /* Parse a label declaration.
25089 label-declaration:
25090 __label__ label-declarator-seq ;
25092 label-declarator-seq:
25093 identifier , label-declarator-seq
25094 identifier */
25096 static void
25097 cp_parser_label_declaration (cp_parser* parser)
25099 /* Look for the `__label__' keyword. */
25100 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25102 while (true)
25104 tree identifier;
25106 /* Look for an identifier. */
25107 identifier = cp_parser_identifier (parser);
25108 /* If we failed, stop. */
25109 if (identifier == error_mark_node)
25110 break;
25111 /* Declare it as a label. */
25112 finish_label_decl (identifier);
25113 /* If the next token is a `;', stop. */
25114 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25115 break;
25116 /* Look for the `,' separating the label declarations. */
25117 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25120 /* Look for the final `;'. */
25121 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25124 // -------------------------------------------------------------------------- //
25125 // Requires Clause
25127 // Parse a requires clause.
25129 // requires-clause:
25130 // 'requires' logical-or-expression
25132 // The required logical-or-expression must be a constant expression. Note
25133 // that we don't check that the expression is constepxr here. We defer until
25134 // we analyze constraints and then, we only check atomic constraints.
25135 static tree
25136 cp_parser_requires_clause (cp_parser *parser)
25138 // Parse the requires clause so that it is not automatically folded.
25139 ++processing_template_decl;
25140 tree expr = cp_parser_binary_expression (parser, false, false,
25141 PREC_NOT_OPERATOR, NULL);
25142 if (check_for_bare_parameter_packs (expr))
25143 expr = error_mark_node;
25144 --processing_template_decl;
25145 return expr;
25148 // Optionally parse a requires clause:
25149 static tree
25150 cp_parser_requires_clause_opt (cp_parser *parser)
25152 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25153 if (tok->keyword != RID_REQUIRES)
25155 if (!flag_concepts && tok->type == CPP_NAME
25156 && tok->u.value == ridpointers[RID_REQUIRES])
25158 error_at (cp_lexer_peek_token (parser->lexer)->location,
25159 "%<requires%> only available with -fconcepts");
25160 /* Parse and discard the requires-clause. */
25161 cp_lexer_consume_token (parser->lexer);
25162 cp_parser_requires_clause (parser);
25164 return NULL_TREE;
25166 cp_lexer_consume_token (parser->lexer);
25167 return cp_parser_requires_clause (parser);
25171 /*---------------------------------------------------------------------------
25172 Requires expressions
25173 ---------------------------------------------------------------------------*/
25175 /* Parse a requires expression
25177 requirement-expression:
25178 'requires' requirement-parameter-list [opt] requirement-body */
25179 static tree
25180 cp_parser_requires_expression (cp_parser *parser)
25182 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25183 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25185 /* A requires-expression shall appear only within a concept
25186 definition or a requires-clause.
25188 TODO: Implement this diagnostic correctly. */
25189 if (!processing_template_decl)
25191 error_at (loc, "a requires expression cannot appear outside a template");
25192 cp_parser_skip_to_end_of_statement (parser);
25193 return error_mark_node;
25196 tree parms, reqs;
25198 /* Local parameters are delared as variables within the scope
25199 of the expression. They are not visible past the end of
25200 the expression. Expressions within the requires-expression
25201 are unevaluated. */
25202 struct scope_sentinel
25204 scope_sentinel ()
25206 ++cp_unevaluated_operand;
25207 begin_scope (sk_block, NULL_TREE);
25210 ~scope_sentinel ()
25212 pop_bindings_and_leave_scope ();
25213 --cp_unevaluated_operand;
25215 } s;
25217 /* Parse the optional parameter list. */
25218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25220 parms = cp_parser_requirement_parameter_list (parser);
25221 if (parms == error_mark_node)
25222 return error_mark_node;
25224 else
25225 parms = NULL_TREE;
25227 /* Parse the requirement body. */
25228 reqs = cp_parser_requirement_body (parser);
25229 if (reqs == error_mark_node)
25230 return error_mark_node;
25233 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25234 the parm chain. */
25235 grokparms (parms, &parms);
25236 return finish_requires_expr (parms, reqs);
25239 /* Parse a parameterized requirement.
25241 requirement-parameter-list:
25242 '(' parameter-declaration-clause ')' */
25243 static tree
25244 cp_parser_requirement_parameter_list (cp_parser *parser)
25246 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25247 return error_mark_node;
25249 tree parms = cp_parser_parameter_declaration_clause (parser);
25251 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25252 return error_mark_node;
25254 return parms;
25257 /* Parse the body of a requirement.
25259 requirement-body:
25260 '{' requirement-list '}' */
25261 static tree
25262 cp_parser_requirement_body (cp_parser *parser)
25264 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25265 return error_mark_node;
25267 tree reqs = cp_parser_requirement_list (parser);
25269 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25270 return error_mark_node;
25272 return reqs;
25275 /* Parse a list of requirements.
25277 requirement-list:
25278 requirement
25279 requirement-list ';' requirement[opt] */
25280 static tree
25281 cp_parser_requirement_list (cp_parser *parser)
25283 tree result = NULL_TREE;
25284 while (true)
25286 tree req = cp_parser_requirement (parser);
25287 if (req == error_mark_node)
25288 return error_mark_node;
25290 result = tree_cons (NULL_TREE, req, result);
25292 /* If we see a semi-colon, consume it. */
25293 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25294 cp_lexer_consume_token (parser->lexer);
25296 /* Stop processing at the end of the list. */
25297 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25298 break;
25301 /* Reverse the order of requirements so they are analyzed in
25302 declaration order. */
25303 return nreverse (result);
25306 /* Parse a syntactic requirement or type requirement.
25308 requirement:
25309 simple-requirement
25310 compound-requirement
25311 type-requirement
25312 nested-requirement */
25313 static tree
25314 cp_parser_requirement (cp_parser *parser)
25316 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25317 return cp_parser_compound_requirement (parser);
25318 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25319 return cp_parser_type_requirement (parser);
25320 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25321 return cp_parser_nested_requirement (parser);
25322 else
25323 return cp_parser_simple_requirement (parser);
25326 /* Parse a simple requirement.
25328 simple-requirement:
25329 expression ';' */
25330 static tree
25331 cp_parser_simple_requirement (cp_parser *parser)
25333 tree expr = cp_parser_expression (parser, NULL, false, false);
25334 if (!expr || expr == error_mark_node)
25335 return error_mark_node;
25337 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25338 return error_mark_node;
25340 return finish_simple_requirement (expr);
25343 /* Parse a type requirement
25345 type-requirement
25346 nested-name-specifier [opt] required-type-name ';'
25348 required-type-name:
25349 type-name
25350 'template' [opt] simple-template-id */
25351 static tree
25352 cp_parser_type_requirement (cp_parser *parser)
25354 cp_lexer_consume_token (parser->lexer);
25356 // Save the scope before parsing name specifiers.
25357 tree saved_scope = parser->scope;
25358 tree saved_object_scope = parser->object_scope;
25359 tree saved_qualifying_scope = parser->qualifying_scope;
25360 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25361 cp_parser_nested_name_specifier_opt (parser,
25362 /*typename_keyword_p=*/true,
25363 /*check_dependency_p=*/false,
25364 /*type_p=*/true,
25365 /*is_declaration=*/false);
25367 tree type;
25368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25370 cp_lexer_consume_token (parser->lexer);
25371 type = cp_parser_template_id (parser,
25372 /*template_keyword_p=*/true,
25373 /*check_dependency=*/false,
25374 /*tag_type=*/none_type,
25375 /*is_declaration=*/false);
25376 type = make_typename_type (parser->scope, type, typename_type,
25377 /*complain=*/tf_error);
25379 else
25380 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25382 if (TREE_CODE (type) == TYPE_DECL)
25383 type = TREE_TYPE (type);
25385 parser->scope = saved_scope;
25386 parser->object_scope = saved_object_scope;
25387 parser->qualifying_scope = saved_qualifying_scope;
25389 if (type == error_mark_node)
25390 cp_parser_skip_to_end_of_statement (parser);
25392 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25393 return error_mark_node;
25394 if (type == error_mark_node)
25395 return error_mark_node;
25397 return finish_type_requirement (type);
25400 /* Parse a compound requirement
25402 compound-requirement:
25403 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25404 static tree
25405 cp_parser_compound_requirement (cp_parser *parser)
25407 /* Parse an expression enclosed in '{ }'s. */
25408 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25409 return error_mark_node;
25411 tree expr = cp_parser_expression (parser, NULL, false, false);
25412 if (!expr || expr == error_mark_node)
25413 return error_mark_node;
25415 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25416 return error_mark_node;
25418 /* Parse the optional noexcept. */
25419 bool noexcept_p = false;
25420 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25422 cp_lexer_consume_token (parser->lexer);
25423 noexcept_p = true;
25426 /* Parse the optional trailing return type. */
25427 tree type = NULL_TREE;
25428 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25430 cp_lexer_consume_token (parser->lexer);
25431 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25432 parser->in_result_type_constraint_p = true;
25433 type = cp_parser_trailing_type_id (parser);
25434 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25435 if (type == error_mark_node)
25436 return error_mark_node;
25439 return finish_compound_requirement (expr, type, noexcept_p);
25442 /* Parse a nested requirement. This is the same as a requires clause.
25444 nested-requirement:
25445 requires-clause */
25446 static tree
25447 cp_parser_nested_requirement (cp_parser *parser)
25449 cp_lexer_consume_token (parser->lexer);
25450 tree req = cp_parser_requires_clause (parser);
25451 if (req == error_mark_node)
25452 return error_mark_node;
25453 return finish_nested_requirement (req);
25456 /* Support Functions */
25458 /* Return the appropriate prefer_type argument for lookup_name_real based on
25459 tag_type and template_mem_access. */
25461 static inline int
25462 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25464 /* DR 141: When looking in the current enclosing context for a template-name
25465 after -> or ., only consider class templates. */
25466 if (template_mem_access)
25467 return 2;
25468 switch (tag_type)
25470 case none_type: return 0; // No preference.
25471 case scope_type: return 1; // Type or namespace.
25472 default: return 2; // Type only.
25476 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25477 NAME should have one of the representations used for an
25478 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25479 is returned. If PARSER->SCOPE is a dependent type, then a
25480 SCOPE_REF is returned.
25482 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25483 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25484 was formed. Abstractly, such entities should not be passed to this
25485 function, because they do not need to be looked up, but it is
25486 simpler to check for this special case here, rather than at the
25487 call-sites.
25489 In cases not explicitly covered above, this function returns a
25490 DECL, OVERLOAD, or baselink representing the result of the lookup.
25491 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25492 is returned.
25494 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25495 (e.g., "struct") that was used. In that case bindings that do not
25496 refer to types are ignored.
25498 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25499 ignored.
25501 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25502 are ignored.
25504 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25505 types.
25507 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25508 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25509 NULL_TREE otherwise. */
25511 static cp_expr
25512 cp_parser_lookup_name (cp_parser *parser, tree name,
25513 enum tag_types tag_type,
25514 bool is_template,
25515 bool is_namespace,
25516 bool check_dependency,
25517 tree *ambiguous_decls,
25518 location_t name_location)
25520 tree decl;
25521 tree object_type = parser->context->object_type;
25523 /* Assume that the lookup will be unambiguous. */
25524 if (ambiguous_decls)
25525 *ambiguous_decls = NULL_TREE;
25527 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25528 no longer valid. Note that if we are parsing tentatively, and
25529 the parse fails, OBJECT_TYPE will be automatically restored. */
25530 parser->context->object_type = NULL_TREE;
25532 if (name == error_mark_node)
25533 return error_mark_node;
25535 /* A template-id has already been resolved; there is no lookup to
25536 do. */
25537 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25538 return name;
25539 if (BASELINK_P (name))
25541 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25542 == TEMPLATE_ID_EXPR);
25543 return name;
25546 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25547 it should already have been checked to make sure that the name
25548 used matches the type being destroyed. */
25549 if (TREE_CODE (name) == BIT_NOT_EXPR)
25551 tree type;
25553 /* Figure out to which type this destructor applies. */
25554 if (parser->scope)
25555 type = parser->scope;
25556 else if (object_type)
25557 type = object_type;
25558 else
25559 type = current_class_type;
25560 /* If that's not a class type, there is no destructor. */
25561 if (!type || !CLASS_TYPE_P (type))
25562 return error_mark_node;
25563 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25564 lazily_declare_fn (sfk_destructor, type);
25565 if (!CLASSTYPE_DESTRUCTORS (type))
25566 return error_mark_node;
25567 /* If it was a class type, return the destructor. */
25568 return CLASSTYPE_DESTRUCTORS (type);
25571 /* By this point, the NAME should be an ordinary identifier. If
25572 the id-expression was a qualified name, the qualifying scope is
25573 stored in PARSER->SCOPE at this point. */
25574 gcc_assert (identifier_p (name));
25576 /* Perform the lookup. */
25577 if (parser->scope)
25579 bool dependent_p;
25581 if (parser->scope == error_mark_node)
25582 return error_mark_node;
25584 /* If the SCOPE is dependent, the lookup must be deferred until
25585 the template is instantiated -- unless we are explicitly
25586 looking up names in uninstantiated templates. Even then, we
25587 cannot look up the name if the scope is not a class type; it
25588 might, for example, be a template type parameter. */
25589 dependent_p = (TYPE_P (parser->scope)
25590 && dependent_scope_p (parser->scope));
25591 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25592 && dependent_p)
25593 /* Defer lookup. */
25594 decl = error_mark_node;
25595 else
25597 tree pushed_scope = NULL_TREE;
25599 /* If PARSER->SCOPE is a dependent type, then it must be a
25600 class type, and we must not be checking dependencies;
25601 otherwise, we would have processed this lookup above. So
25602 that PARSER->SCOPE is not considered a dependent base by
25603 lookup_member, we must enter the scope here. */
25604 if (dependent_p)
25605 pushed_scope = push_scope (parser->scope);
25607 /* If the PARSER->SCOPE is a template specialization, it
25608 may be instantiated during name lookup. In that case,
25609 errors may be issued. Even if we rollback the current
25610 tentative parse, those errors are valid. */
25611 decl = lookup_qualified_name (parser->scope, name,
25612 prefer_type_arg (tag_type),
25613 /*complain=*/true);
25615 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25616 lookup result and the nested-name-specifier nominates a class C:
25617 * if the name specified after the nested-name-specifier, when
25618 looked up in C, is the injected-class-name of C (Clause 9), or
25619 * if the name specified after the nested-name-specifier is the
25620 same as the identifier or the simple-template-id's template-
25621 name in the last component of the nested-name-specifier,
25622 the name is instead considered to name the constructor of
25623 class C. [ Note: for example, the constructor is not an
25624 acceptable lookup result in an elaborated-type-specifier so
25625 the constructor would not be used in place of the
25626 injected-class-name. --end note ] Such a constructor name
25627 shall be used only in the declarator-id of a declaration that
25628 names a constructor or in a using-declaration. */
25629 if (tag_type == none_type
25630 && DECL_SELF_REFERENCE_P (decl)
25631 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25632 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25633 prefer_type_arg (tag_type),
25634 /*complain=*/true);
25636 /* If we have a single function from a using decl, pull it out. */
25637 if (TREE_CODE (decl) == OVERLOAD
25638 && !really_overloaded_fn (decl))
25639 decl = OVL_FUNCTION (decl);
25641 if (pushed_scope)
25642 pop_scope (pushed_scope);
25645 /* If the scope is a dependent type and either we deferred lookup or
25646 we did lookup but didn't find the name, rememeber the name. */
25647 if (decl == error_mark_node && TYPE_P (parser->scope)
25648 && dependent_type_p (parser->scope))
25650 if (tag_type)
25652 tree type;
25654 /* The resolution to Core Issue 180 says that `struct
25655 A::B' should be considered a type-name, even if `A'
25656 is dependent. */
25657 type = make_typename_type (parser->scope, name, tag_type,
25658 /*complain=*/tf_error);
25659 if (type != error_mark_node)
25660 decl = TYPE_NAME (type);
25662 else if (is_template
25663 && (cp_parser_next_token_ends_template_argument_p (parser)
25664 || cp_lexer_next_token_is (parser->lexer,
25665 CPP_CLOSE_PAREN)))
25666 decl = make_unbound_class_template (parser->scope,
25667 name, NULL_TREE,
25668 /*complain=*/tf_error);
25669 else
25670 decl = build_qualified_name (/*type=*/NULL_TREE,
25671 parser->scope, name,
25672 is_template);
25674 parser->qualifying_scope = parser->scope;
25675 parser->object_scope = NULL_TREE;
25677 else if (object_type)
25679 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25680 OBJECT_TYPE is not a class. */
25681 if (CLASS_TYPE_P (object_type))
25682 /* If the OBJECT_TYPE is a template specialization, it may
25683 be instantiated during name lookup. In that case, errors
25684 may be issued. Even if we rollback the current tentative
25685 parse, those errors are valid. */
25686 decl = lookup_member (object_type,
25687 name,
25688 /*protect=*/0,
25689 prefer_type_arg (tag_type),
25690 tf_warning_or_error);
25691 else
25692 decl = NULL_TREE;
25694 if (!decl)
25695 /* Look it up in the enclosing context. DR 141: When looking for a
25696 template-name after -> or ., only consider class templates. */
25697 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25698 /*nonclass=*/0,
25699 /*block_p=*/true, is_namespace, 0);
25700 if (object_type == unknown_type_node)
25701 /* The object is type-dependent, so we can't look anything up; we used
25702 this to get the DR 141 behavior. */
25703 object_type = NULL_TREE;
25704 parser->object_scope = object_type;
25705 parser->qualifying_scope = NULL_TREE;
25707 else
25709 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25710 /*nonclass=*/0,
25711 /*block_p=*/true, is_namespace, 0);
25712 parser->qualifying_scope = NULL_TREE;
25713 parser->object_scope = NULL_TREE;
25716 /* If the lookup failed, let our caller know. */
25717 if (!decl || decl == error_mark_node)
25718 return error_mark_node;
25720 /* Pull out the template from an injected-class-name (or multiple). */
25721 if (is_template)
25722 decl = maybe_get_template_decl_from_type_decl (decl);
25724 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25725 if (TREE_CODE (decl) == TREE_LIST)
25727 if (ambiguous_decls)
25728 *ambiguous_decls = decl;
25729 /* The error message we have to print is too complicated for
25730 cp_parser_error, so we incorporate its actions directly. */
25731 if (!cp_parser_simulate_error (parser))
25733 error_at (name_location, "reference to %qD is ambiguous",
25734 name);
25735 print_candidates (decl);
25737 return error_mark_node;
25740 gcc_assert (DECL_P (decl)
25741 || TREE_CODE (decl) == OVERLOAD
25742 || TREE_CODE (decl) == SCOPE_REF
25743 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25744 || BASELINK_P (decl));
25746 /* If we have resolved the name of a member declaration, check to
25747 see if the declaration is accessible. When the name resolves to
25748 set of overloaded functions, accessibility is checked when
25749 overload resolution is done.
25751 During an explicit instantiation, access is not checked at all,
25752 as per [temp.explicit]. */
25753 if (DECL_P (decl))
25754 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25756 maybe_record_typedef_use (decl);
25758 return cp_expr (decl, name_location);
25761 /* Like cp_parser_lookup_name, but for use in the typical case where
25762 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25763 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25765 static tree
25766 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25768 return cp_parser_lookup_name (parser, name,
25769 none_type,
25770 /*is_template=*/false,
25771 /*is_namespace=*/false,
25772 /*check_dependency=*/true,
25773 /*ambiguous_decls=*/NULL,
25774 location);
25777 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25778 the current context, return the TYPE_DECL. If TAG_NAME_P is
25779 true, the DECL indicates the class being defined in a class-head,
25780 or declared in an elaborated-type-specifier.
25782 Otherwise, return DECL. */
25784 static tree
25785 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25787 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25788 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25790 struct A {
25791 template <typename T> struct B;
25794 template <typename T> struct A::B {};
25796 Similarly, in an elaborated-type-specifier:
25798 namespace N { struct X{}; }
25800 struct A {
25801 template <typename T> friend struct N::X;
25804 However, if the DECL refers to a class type, and we are in
25805 the scope of the class, then the name lookup automatically
25806 finds the TYPE_DECL created by build_self_reference rather
25807 than a TEMPLATE_DECL. For example, in:
25809 template <class T> struct S {
25810 S s;
25813 there is no need to handle such case. */
25815 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25816 return DECL_TEMPLATE_RESULT (decl);
25818 return decl;
25821 /* If too many, or too few, template-parameter lists apply to the
25822 declarator, issue an error message. Returns TRUE if all went well,
25823 and FALSE otherwise. */
25825 static bool
25826 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25827 cp_declarator *declarator,
25828 location_t declarator_location)
25830 switch (declarator->kind)
25832 case cdk_id:
25834 unsigned num_templates = 0;
25835 tree scope = declarator->u.id.qualifying_scope;
25837 if (scope)
25838 num_templates = num_template_headers_for_class (scope);
25839 else if (TREE_CODE (declarator->u.id.unqualified_name)
25840 == TEMPLATE_ID_EXPR)
25841 /* If the DECLARATOR has the form `X<y>' then it uses one
25842 additional level of template parameters. */
25843 ++num_templates;
25845 return cp_parser_check_template_parameters
25846 (parser, num_templates, declarator_location, declarator);
25849 case cdk_function:
25850 case cdk_array:
25851 case cdk_pointer:
25852 case cdk_reference:
25853 case cdk_ptrmem:
25854 return (cp_parser_check_declarator_template_parameters
25855 (parser, declarator->declarator, declarator_location));
25857 case cdk_decomp:
25858 case cdk_error:
25859 return true;
25861 default:
25862 gcc_unreachable ();
25864 return false;
25867 /* NUM_TEMPLATES were used in the current declaration. If that is
25868 invalid, return FALSE and issue an error messages. Otherwise,
25869 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25870 declarator and we can print more accurate diagnostics. */
25872 static bool
25873 cp_parser_check_template_parameters (cp_parser* parser,
25874 unsigned num_templates,
25875 location_t location,
25876 cp_declarator *declarator)
25878 /* If there are the same number of template classes and parameter
25879 lists, that's OK. */
25880 if (parser->num_template_parameter_lists == num_templates)
25881 return true;
25882 /* If there are more, but only one more, then we are referring to a
25883 member template. That's OK too. */
25884 if (parser->num_template_parameter_lists == num_templates + 1)
25885 return true;
25886 /* If there are more template classes than parameter lists, we have
25887 something like:
25889 template <class T> void S<T>::R<T>::f (); */
25890 if (parser->num_template_parameter_lists < num_templates)
25892 if (declarator && !current_function_decl)
25893 error_at (location, "specializing member %<%T::%E%> "
25894 "requires %<template<>%> syntax",
25895 declarator->u.id.qualifying_scope,
25896 declarator->u.id.unqualified_name);
25897 else if (declarator)
25898 error_at (location, "invalid declaration of %<%T::%E%>",
25899 declarator->u.id.qualifying_scope,
25900 declarator->u.id.unqualified_name);
25901 else
25902 error_at (location, "too few template-parameter-lists");
25903 return false;
25905 /* Otherwise, there are too many template parameter lists. We have
25906 something like:
25908 template <class T> template <class U> void S::f(); */
25909 error_at (location, "too many template-parameter-lists");
25910 return false;
25913 /* Parse an optional `::' token indicating that the following name is
25914 from the global namespace. If so, PARSER->SCOPE is set to the
25915 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25916 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25917 Returns the new value of PARSER->SCOPE, if the `::' token is
25918 present, and NULL_TREE otherwise. */
25920 static tree
25921 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25923 cp_token *token;
25925 /* Peek at the next token. */
25926 token = cp_lexer_peek_token (parser->lexer);
25927 /* If we're looking at a `::' token then we're starting from the
25928 global namespace, not our current location. */
25929 if (token->type == CPP_SCOPE)
25931 /* Consume the `::' token. */
25932 cp_lexer_consume_token (parser->lexer);
25933 /* Set the SCOPE so that we know where to start the lookup. */
25934 parser->scope = global_namespace;
25935 parser->qualifying_scope = global_namespace;
25936 parser->object_scope = NULL_TREE;
25938 return parser->scope;
25940 else if (!current_scope_valid_p)
25942 parser->scope = NULL_TREE;
25943 parser->qualifying_scope = NULL_TREE;
25944 parser->object_scope = NULL_TREE;
25947 return NULL_TREE;
25950 /* Returns TRUE if the upcoming token sequence is the start of a
25951 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25952 declarator is preceded by the `friend' specifier. */
25954 static bool
25955 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25957 bool constructor_p;
25958 bool outside_class_specifier_p;
25959 tree nested_name_specifier;
25960 cp_token *next_token;
25962 /* The common case is that this is not a constructor declarator, so
25963 try to avoid doing lots of work if at all possible. It's not
25964 valid declare a constructor at function scope. */
25965 if (parser->in_function_body)
25966 return false;
25967 /* And only certain tokens can begin a constructor declarator. */
25968 next_token = cp_lexer_peek_token (parser->lexer);
25969 if (next_token->type != CPP_NAME
25970 && next_token->type != CPP_SCOPE
25971 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25972 && next_token->type != CPP_TEMPLATE_ID)
25973 return false;
25975 /* Parse tentatively; we are going to roll back all of the tokens
25976 consumed here. */
25977 cp_parser_parse_tentatively (parser);
25978 /* Assume that we are looking at a constructor declarator. */
25979 constructor_p = true;
25981 /* Look for the optional `::' operator. */
25982 cp_parser_global_scope_opt (parser,
25983 /*current_scope_valid_p=*/false);
25984 /* Look for the nested-name-specifier. */
25985 nested_name_specifier
25986 = (cp_parser_nested_name_specifier_opt (parser,
25987 /*typename_keyword_p=*/false,
25988 /*check_dependency_p=*/false,
25989 /*type_p=*/false,
25990 /*is_declaration=*/false));
25992 outside_class_specifier_p = (!at_class_scope_p ()
25993 || !TYPE_BEING_DEFINED (current_class_type)
25994 || friend_p);
25996 /* Outside of a class-specifier, there must be a
25997 nested-name-specifier. Except in C++17 mode, where we
25998 might be declaring a guiding declaration. */
25999 if (!nested_name_specifier && outside_class_specifier_p
26000 && cxx_dialect < cxx1z)
26001 constructor_p = false;
26002 else if (nested_name_specifier == error_mark_node)
26003 constructor_p = false;
26005 /* If we have a class scope, this is easy; DR 147 says that S::S always
26006 names the constructor, and no other qualified name could. */
26007 if (constructor_p && nested_name_specifier
26008 && CLASS_TYPE_P (nested_name_specifier))
26010 tree id = cp_parser_unqualified_id (parser,
26011 /*template_keyword_p=*/false,
26012 /*check_dependency_p=*/false,
26013 /*declarator_p=*/true,
26014 /*optional_p=*/false);
26015 if (is_overloaded_fn (id))
26016 id = DECL_NAME (get_first_fn (id));
26017 if (!constructor_name_p (id, nested_name_specifier))
26018 constructor_p = false;
26020 /* If we still think that this might be a constructor-declarator,
26021 look for a class-name. */
26022 else if (constructor_p)
26024 /* If we have:
26026 template <typename T> struct S {
26027 S();
26030 we must recognize that the nested `S' names a class. */
26031 if (cxx_dialect >= cxx1z)
26032 cp_parser_parse_tentatively (parser);
26034 tree type_decl;
26035 type_decl = cp_parser_class_name (parser,
26036 /*typename_keyword_p=*/false,
26037 /*template_keyword_p=*/false,
26038 none_type,
26039 /*check_dependency_p=*/false,
26040 /*class_head_p=*/false,
26041 /*is_declaration=*/false);
26043 if (cxx_dialect >= cxx1z
26044 && !cp_parser_parse_definitely (parser))
26046 type_decl = NULL_TREE;
26047 tree tmpl = cp_parser_template_name (parser,
26048 /*template_keyword*/false,
26049 /*check_dependency_p*/false,
26050 /*is_declaration*/false,
26051 none_type,
26052 /*is_identifier*/NULL);
26053 if (DECL_CLASS_TEMPLATE_P (tmpl)
26054 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26055 /* It's a deduction guide, return true. */;
26056 else
26057 cp_parser_simulate_error (parser);
26060 /* If there was no class-name, then this is not a constructor.
26061 Otherwise, if we are in a class-specifier and we aren't
26062 handling a friend declaration, check that its type matches
26063 current_class_type (c++/38313). Note: error_mark_node
26064 is left alone for error recovery purposes. */
26065 constructor_p = (!cp_parser_error_occurred (parser)
26066 && (outside_class_specifier_p
26067 || type_decl == NULL_TREE
26068 || type_decl == error_mark_node
26069 || same_type_p (current_class_type,
26070 TREE_TYPE (type_decl))));
26072 /* If we're still considering a constructor, we have to see a `(',
26073 to begin the parameter-declaration-clause, followed by either a
26074 `)', an `...', or a decl-specifier. We need to check for a
26075 type-specifier to avoid being fooled into thinking that:
26077 S (f) (int);
26079 is a constructor. (It is actually a function named `f' that
26080 takes one parameter (of type `int') and returns a value of type
26081 `S'. */
26082 if (constructor_p
26083 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26084 constructor_p = false;
26086 if (constructor_p
26087 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26088 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26089 /* A parameter declaration begins with a decl-specifier,
26090 which is either the "attribute" keyword, a storage class
26091 specifier, or (usually) a type-specifier. */
26092 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26094 tree type;
26095 tree pushed_scope = NULL_TREE;
26096 unsigned saved_num_template_parameter_lists;
26098 /* Names appearing in the type-specifier should be looked up
26099 in the scope of the class. */
26100 if (current_class_type)
26101 type = NULL_TREE;
26102 else if (type_decl)
26104 type = TREE_TYPE (type_decl);
26105 if (TREE_CODE (type) == TYPENAME_TYPE)
26107 type = resolve_typename_type (type,
26108 /*only_current_p=*/false);
26109 if (TREE_CODE (type) == TYPENAME_TYPE)
26111 cp_parser_abort_tentative_parse (parser);
26112 return false;
26115 pushed_scope = push_scope (type);
26118 /* Inside the constructor parameter list, surrounding
26119 template-parameter-lists do not apply. */
26120 saved_num_template_parameter_lists
26121 = parser->num_template_parameter_lists;
26122 parser->num_template_parameter_lists = 0;
26124 /* Look for the type-specifier. */
26125 cp_parser_type_specifier (parser,
26126 CP_PARSER_FLAGS_NONE,
26127 /*decl_specs=*/NULL,
26128 /*is_declarator=*/true,
26129 /*declares_class_or_enum=*/NULL,
26130 /*is_cv_qualifier=*/NULL);
26132 parser->num_template_parameter_lists
26133 = saved_num_template_parameter_lists;
26135 /* Leave the scope of the class. */
26136 if (pushed_scope)
26137 pop_scope (pushed_scope);
26139 constructor_p = !cp_parser_error_occurred (parser);
26143 /* We did not really want to consume any tokens. */
26144 cp_parser_abort_tentative_parse (parser);
26146 return constructor_p;
26149 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26150 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26151 they must be performed once we are in the scope of the function.
26153 Returns the function defined. */
26155 static tree
26156 cp_parser_function_definition_from_specifiers_and_declarator
26157 (cp_parser* parser,
26158 cp_decl_specifier_seq *decl_specifiers,
26159 tree attributes,
26160 const cp_declarator *declarator)
26162 tree fn;
26163 bool success_p;
26165 /* Begin the function-definition. */
26166 success_p = start_function (decl_specifiers, declarator, attributes);
26168 /* The things we're about to see are not directly qualified by any
26169 template headers we've seen thus far. */
26170 reset_specialization ();
26172 /* If there were names looked up in the decl-specifier-seq that we
26173 did not check, check them now. We must wait until we are in the
26174 scope of the function to perform the checks, since the function
26175 might be a friend. */
26176 perform_deferred_access_checks (tf_warning_or_error);
26178 if (success_p)
26180 cp_finalize_omp_declare_simd (parser, current_function_decl);
26181 parser->omp_declare_simd = NULL;
26182 cp_finalize_oacc_routine (parser, current_function_decl, true);
26183 parser->oacc_routine = NULL;
26186 if (!success_p)
26188 /* Skip the entire function. */
26189 cp_parser_skip_to_end_of_block_or_statement (parser);
26190 fn = error_mark_node;
26192 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26194 /* Seen already, skip it. An error message has already been output. */
26195 cp_parser_skip_to_end_of_block_or_statement (parser);
26196 fn = current_function_decl;
26197 current_function_decl = NULL_TREE;
26198 /* If this is a function from a class, pop the nested class. */
26199 if (current_class_name)
26200 pop_nested_class ();
26202 else
26204 timevar_id_t tv;
26205 if (DECL_DECLARED_INLINE_P (current_function_decl))
26206 tv = TV_PARSE_INLINE;
26207 else
26208 tv = TV_PARSE_FUNC;
26209 timevar_push (tv);
26210 fn = cp_parser_function_definition_after_declarator (parser,
26211 /*inline_p=*/false);
26212 timevar_pop (tv);
26215 return fn;
26218 /* Parse the part of a function-definition that follows the
26219 declarator. INLINE_P is TRUE iff this function is an inline
26220 function defined within a class-specifier.
26222 Returns the function defined. */
26224 static tree
26225 cp_parser_function_definition_after_declarator (cp_parser* parser,
26226 bool inline_p)
26228 tree fn;
26229 bool ctor_initializer_p = false;
26230 bool saved_in_unbraced_linkage_specification_p;
26231 bool saved_in_function_body;
26232 unsigned saved_num_template_parameter_lists;
26233 cp_token *token;
26234 bool fully_implicit_function_template_p
26235 = parser->fully_implicit_function_template_p;
26236 parser->fully_implicit_function_template_p = false;
26237 tree implicit_template_parms
26238 = parser->implicit_template_parms;
26239 parser->implicit_template_parms = 0;
26240 cp_binding_level* implicit_template_scope
26241 = parser->implicit_template_scope;
26242 parser->implicit_template_scope = 0;
26244 saved_in_function_body = parser->in_function_body;
26245 parser->in_function_body = true;
26246 /* If the next token is `return', then the code may be trying to
26247 make use of the "named return value" extension that G++ used to
26248 support. */
26249 token = cp_lexer_peek_token (parser->lexer);
26250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26252 /* Consume the `return' keyword. */
26253 cp_lexer_consume_token (parser->lexer);
26254 /* Look for the identifier that indicates what value is to be
26255 returned. */
26256 cp_parser_identifier (parser);
26257 /* Issue an error message. */
26258 error_at (token->location,
26259 "named return values are no longer supported");
26260 /* Skip tokens until we reach the start of the function body. */
26261 while (true)
26263 cp_token *token = cp_lexer_peek_token (parser->lexer);
26264 if (token->type == CPP_OPEN_BRACE
26265 || token->type == CPP_EOF
26266 || token->type == CPP_PRAGMA_EOL)
26267 break;
26268 cp_lexer_consume_token (parser->lexer);
26271 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26272 anything declared inside `f'. */
26273 saved_in_unbraced_linkage_specification_p
26274 = parser->in_unbraced_linkage_specification_p;
26275 parser->in_unbraced_linkage_specification_p = false;
26276 /* Inside the function, surrounding template-parameter-lists do not
26277 apply. */
26278 saved_num_template_parameter_lists
26279 = parser->num_template_parameter_lists;
26280 parser->num_template_parameter_lists = 0;
26282 start_lambda_scope (current_function_decl);
26284 /* If the next token is `try', `__transaction_atomic', or
26285 `__transaction_relaxed`, then we are looking at either function-try-block
26286 or function-transaction-block. Note that all of these include the
26287 function-body. */
26288 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26289 ctor_initializer_p = cp_parser_function_transaction (parser,
26290 RID_TRANSACTION_ATOMIC);
26291 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26292 RID_TRANSACTION_RELAXED))
26293 ctor_initializer_p = cp_parser_function_transaction (parser,
26294 RID_TRANSACTION_RELAXED);
26295 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26296 ctor_initializer_p = cp_parser_function_try_block (parser);
26297 else
26298 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26299 (parser, /*in_function_try_block=*/false);
26301 finish_lambda_scope ();
26303 /* Finish the function. */
26304 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26305 (inline_p ? 2 : 0));
26306 /* Generate code for it, if necessary. */
26307 expand_or_defer_fn (fn);
26308 /* Restore the saved values. */
26309 parser->in_unbraced_linkage_specification_p
26310 = saved_in_unbraced_linkage_specification_p;
26311 parser->num_template_parameter_lists
26312 = saved_num_template_parameter_lists;
26313 parser->in_function_body = saved_in_function_body;
26315 parser->fully_implicit_function_template_p
26316 = fully_implicit_function_template_p;
26317 parser->implicit_template_parms
26318 = implicit_template_parms;
26319 parser->implicit_template_scope
26320 = implicit_template_scope;
26322 if (parser->fully_implicit_function_template_p)
26323 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26325 return fn;
26328 /* Parse a template-declaration body (following argument list). */
26330 static void
26331 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26332 tree parameter_list,
26333 bool member_p)
26335 tree decl = NULL_TREE;
26336 bool friend_p = false;
26338 /* We just processed one more parameter list. */
26339 ++parser->num_template_parameter_lists;
26341 /* Get the deferred access checks from the parameter list. These
26342 will be checked once we know what is being declared, as for a
26343 member template the checks must be performed in the scope of the
26344 class containing the member. */
26345 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26347 /* Tentatively parse for a new template parameter list, which can either be
26348 the template keyword or a template introduction. */
26349 if (cp_parser_template_declaration_after_export (parser, member_p))
26350 /* OK */;
26351 else if (cxx_dialect >= cxx11
26352 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26353 decl = cp_parser_alias_declaration (parser);
26354 else
26356 /* There are no access checks when parsing a template, as we do not
26357 know if a specialization will be a friend. */
26358 push_deferring_access_checks (dk_no_check);
26359 cp_token *token = cp_lexer_peek_token (parser->lexer);
26360 decl = cp_parser_single_declaration (parser,
26361 checks,
26362 member_p,
26363 /*explicit_specialization_p=*/false,
26364 &friend_p);
26365 pop_deferring_access_checks ();
26367 /* If this is a member template declaration, let the front
26368 end know. */
26369 if (member_p && !friend_p && decl)
26371 if (TREE_CODE (decl) == TYPE_DECL)
26372 cp_parser_check_access_in_redeclaration (decl, token->location);
26374 decl = finish_member_template_decl (decl);
26376 else if (friend_p && decl
26377 && DECL_DECLARES_TYPE_P (decl))
26378 make_friend_class (current_class_type, TREE_TYPE (decl),
26379 /*complain=*/true);
26381 /* We are done with the current parameter list. */
26382 --parser->num_template_parameter_lists;
26384 pop_deferring_access_checks ();
26386 /* Finish up. */
26387 finish_template_decl (parameter_list);
26389 /* Check the template arguments for a literal operator template. */
26390 if (decl
26391 && DECL_DECLARES_FUNCTION_P (decl)
26392 && UDLIT_OPER_P (DECL_NAME (decl)))
26394 bool ok = true;
26395 if (parameter_list == NULL_TREE)
26396 ok = false;
26397 else
26399 int num_parms = TREE_VEC_LENGTH (parameter_list);
26400 if (num_parms == 1)
26402 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26403 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26404 if (TREE_TYPE (parm) != char_type_node
26405 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26406 ok = false;
26408 else if (num_parms == 2 && cxx_dialect >= cxx14)
26410 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26411 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26412 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26413 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26414 if (parm == error_mark_node
26415 || TREE_TYPE (parm) != TREE_TYPE (type)
26416 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26417 ok = false;
26419 else
26420 ok = false;
26422 if (!ok)
26424 if (cxx_dialect >= cxx14)
26425 error ("literal operator template %qD has invalid parameter list."
26426 " Expected non-type template argument pack <char...>"
26427 " or <typename CharT, CharT...>",
26428 decl);
26429 else
26430 error ("literal operator template %qD has invalid parameter list."
26431 " Expected non-type template argument pack <char...>",
26432 decl);
26436 /* Register member declarations. */
26437 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26438 finish_member_declaration (decl);
26439 /* If DECL is a function template, we must return to parse it later.
26440 (Even though there is no definition, there might be default
26441 arguments that need handling.) */
26442 if (member_p && decl
26443 && DECL_DECLARES_FUNCTION_P (decl))
26444 vec_safe_push (unparsed_funs_with_definitions, decl);
26447 /* Parse a template introduction header for a template-declaration. Returns
26448 false if tentative parse fails. */
26450 static bool
26451 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26453 cp_parser_parse_tentatively (parser);
26455 tree saved_scope = parser->scope;
26456 tree saved_object_scope = parser->object_scope;
26457 tree saved_qualifying_scope = parser->qualifying_scope;
26459 /* Look for the optional `::' operator. */
26460 cp_parser_global_scope_opt (parser,
26461 /*current_scope_valid_p=*/false);
26462 /* Look for the nested-name-specifier. */
26463 cp_parser_nested_name_specifier_opt (parser,
26464 /*typename_keyword_p=*/false,
26465 /*check_dependency_p=*/true,
26466 /*type_p=*/false,
26467 /*is_declaration=*/false);
26469 cp_token *token = cp_lexer_peek_token (parser->lexer);
26470 tree concept_name = cp_parser_identifier (parser);
26472 /* Look up the concept for which we will be matching
26473 template parameters. */
26474 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26475 token->location);
26476 parser->scope = saved_scope;
26477 parser->object_scope = saved_object_scope;
26478 parser->qualifying_scope = saved_qualifying_scope;
26480 if (concept_name == error_mark_node)
26481 cp_parser_simulate_error (parser);
26483 /* Look for opening brace for introduction. */
26484 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26486 if (!cp_parser_parse_definitely (parser))
26487 return false;
26489 push_deferring_access_checks (dk_deferred);
26491 /* Build vector of placeholder parameters and grab
26492 matching identifiers. */
26493 tree introduction_list = cp_parser_introduction_list (parser);
26495 /* The introduction-list shall not be empty. */
26496 int nargs = TREE_VEC_LENGTH (introduction_list);
26497 if (nargs == 0)
26499 error ("empty introduction-list");
26500 return true;
26503 /* Look for closing brace for introduction. */
26504 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26505 return true;
26507 if (tmpl_decl == error_mark_node)
26509 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26510 token->location);
26511 return true;
26514 /* Build and associate the constraint. */
26515 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26516 if (parms && parms != error_mark_node)
26518 cp_parser_template_declaration_after_parameters (parser, parms,
26519 member_p);
26520 return true;
26523 error_at (token->location, "no matching concept for template-introduction");
26524 return true;
26527 /* Parse a normal template-declaration following the template keyword. */
26529 static void
26530 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26532 tree parameter_list;
26533 bool need_lang_pop;
26534 location_t location = input_location;
26536 /* Look for the `<' token. */
26537 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26538 return;
26539 if (at_class_scope_p () && current_function_decl)
26541 /* 14.5.2.2 [temp.mem]
26543 A local class shall not have member templates. */
26544 error_at (location,
26545 "invalid declaration of member template in local class");
26546 cp_parser_skip_to_end_of_block_or_statement (parser);
26547 return;
26549 /* [temp]
26551 A template ... shall not have C linkage. */
26552 if (current_lang_name == lang_name_c)
26554 error_at (location, "template with C linkage");
26555 /* Give it C++ linkage to avoid confusing other parts of the
26556 front end. */
26557 push_lang_context (lang_name_cplusplus);
26558 need_lang_pop = true;
26560 else
26561 need_lang_pop = false;
26563 /* We cannot perform access checks on the template parameter
26564 declarations until we know what is being declared, just as we
26565 cannot check the decl-specifier list. */
26566 push_deferring_access_checks (dk_deferred);
26568 /* If the next token is `>', then we have an invalid
26569 specialization. Rather than complain about an invalid template
26570 parameter, issue an error message here. */
26571 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26573 cp_parser_error (parser, "invalid explicit specialization");
26574 begin_specialization ();
26575 parameter_list = NULL_TREE;
26577 else
26579 /* Parse the template parameters. */
26580 parameter_list = cp_parser_template_parameter_list (parser);
26583 /* Look for the `>'. */
26584 cp_parser_skip_to_end_of_template_parameter_list (parser);
26586 /* Manage template requirements */
26587 if (flag_concepts)
26589 tree reqs = get_shorthand_constraints (current_template_parms);
26590 if (tree r = cp_parser_requires_clause_opt (parser))
26591 reqs = conjoin_constraints (reqs, normalize_expression (r));
26592 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26595 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26596 member_p);
26598 /* For the erroneous case of a template with C linkage, we pushed an
26599 implicit C++ linkage scope; exit that scope now. */
26600 if (need_lang_pop)
26601 pop_lang_context ();
26604 /* Parse a template-declaration, assuming that the `export' (and
26605 `extern') keywords, if present, has already been scanned. MEMBER_P
26606 is as for cp_parser_template_declaration. */
26608 static bool
26609 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26613 cp_lexer_consume_token (parser->lexer);
26614 cp_parser_explicit_template_declaration (parser, member_p);
26615 return true;
26617 else if (flag_concepts)
26618 return cp_parser_template_introduction (parser, member_p);
26620 return false;
26623 /* Perform the deferred access checks from a template-parameter-list.
26624 CHECKS is a TREE_LIST of access checks, as returned by
26625 get_deferred_access_checks. */
26627 static void
26628 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26630 ++processing_template_parmlist;
26631 perform_access_checks (checks, tf_warning_or_error);
26632 --processing_template_parmlist;
26635 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26636 `function-definition' sequence that follows a template header.
26637 If MEMBER_P is true, this declaration appears in a class scope.
26639 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26640 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26642 static tree
26643 cp_parser_single_declaration (cp_parser* parser,
26644 vec<deferred_access_check, va_gc> *checks,
26645 bool member_p,
26646 bool explicit_specialization_p,
26647 bool* friend_p)
26649 int declares_class_or_enum;
26650 tree decl = NULL_TREE;
26651 cp_decl_specifier_seq decl_specifiers;
26652 bool function_definition_p = false;
26653 cp_token *decl_spec_token_start;
26655 /* This function is only used when processing a template
26656 declaration. */
26657 gcc_assert (innermost_scope_kind () == sk_template_parms
26658 || innermost_scope_kind () == sk_template_spec);
26660 /* Defer access checks until we know what is being declared. */
26661 push_deferring_access_checks (dk_deferred);
26663 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26664 alternative. */
26665 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26666 cp_parser_decl_specifier_seq (parser,
26667 CP_PARSER_FLAGS_OPTIONAL,
26668 &decl_specifiers,
26669 &declares_class_or_enum);
26670 if (friend_p)
26671 *friend_p = cp_parser_friend_p (&decl_specifiers);
26673 /* There are no template typedefs. */
26674 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26676 error_at (decl_spec_token_start->location,
26677 "template declaration of %<typedef%>");
26678 decl = error_mark_node;
26681 /* Gather up the access checks that occurred the
26682 decl-specifier-seq. */
26683 stop_deferring_access_checks ();
26685 /* Check for the declaration of a template class. */
26686 if (declares_class_or_enum)
26688 if (cp_parser_declares_only_class_p (parser)
26689 || (declares_class_or_enum & 2))
26691 // If this is a declaration, but not a definition, associate
26692 // any constraints with the type declaration. Constraints
26693 // are associated with definitions in cp_parser_class_specifier.
26694 if (declares_class_or_enum == 1)
26695 associate_classtype_constraints (decl_specifiers.type);
26697 decl = shadow_tag (&decl_specifiers);
26699 /* In this case:
26701 struct C {
26702 friend template <typename T> struct A<T>::B;
26705 A<T>::B will be represented by a TYPENAME_TYPE, and
26706 therefore not recognized by shadow_tag. */
26707 if (friend_p && *friend_p
26708 && !decl
26709 && decl_specifiers.type
26710 && TYPE_P (decl_specifiers.type))
26711 decl = decl_specifiers.type;
26713 if (decl && decl != error_mark_node)
26714 decl = TYPE_NAME (decl);
26715 else
26716 decl = error_mark_node;
26718 /* Perform access checks for template parameters. */
26719 cp_parser_perform_template_parameter_access_checks (checks);
26721 /* Give a helpful diagnostic for
26722 template <class T> struct A { } a;
26723 if we aren't already recovering from an error. */
26724 if (!cp_parser_declares_only_class_p (parser)
26725 && !seen_error ())
26727 error_at (cp_lexer_peek_token (parser->lexer)->location,
26728 "a class template declaration must not declare "
26729 "anything else");
26730 cp_parser_skip_to_end_of_block_or_statement (parser);
26731 goto out;
26736 /* Complain about missing 'typename' or other invalid type names. */
26737 if (!decl_specifiers.any_type_specifiers_p
26738 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26740 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26741 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26742 the rest of this declaration. */
26743 decl = error_mark_node;
26744 goto out;
26747 /* If it's not a template class, try for a template function. If
26748 the next token is a `;', then this declaration does not declare
26749 anything. But, if there were errors in the decl-specifiers, then
26750 the error might well have come from an attempted class-specifier.
26751 In that case, there's no need to warn about a missing declarator. */
26752 if (!decl
26753 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26754 || decl_specifiers.type != error_mark_node))
26756 decl = cp_parser_init_declarator (parser,
26757 &decl_specifiers,
26758 checks,
26759 /*function_definition_allowed_p=*/true,
26760 member_p,
26761 declares_class_or_enum,
26762 &function_definition_p,
26763 NULL, NULL, NULL);
26765 /* 7.1.1-1 [dcl.stc]
26767 A storage-class-specifier shall not be specified in an explicit
26768 specialization... */
26769 if (decl
26770 && explicit_specialization_p
26771 && decl_specifiers.storage_class != sc_none)
26773 error_at (decl_spec_token_start->location,
26774 "explicit template specialization cannot have a storage class");
26775 decl = error_mark_node;
26778 if (decl && VAR_P (decl))
26779 check_template_variable (decl);
26782 /* Look for a trailing `;' after the declaration. */
26783 if (!function_definition_p
26784 && (decl == error_mark_node
26785 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26786 cp_parser_skip_to_end_of_block_or_statement (parser);
26788 out:
26789 pop_deferring_access_checks ();
26791 /* Clear any current qualification; whatever comes next is the start
26792 of something new. */
26793 parser->scope = NULL_TREE;
26794 parser->qualifying_scope = NULL_TREE;
26795 parser->object_scope = NULL_TREE;
26797 return decl;
26800 /* Parse a cast-expression that is not the operand of a unary "&". */
26802 static cp_expr
26803 cp_parser_simple_cast_expression (cp_parser *parser)
26805 return cp_parser_cast_expression (parser, /*address_p=*/false,
26806 /*cast_p=*/false, /*decltype*/false, NULL);
26809 /* Parse a functional cast to TYPE. Returns an expression
26810 representing the cast. */
26812 static cp_expr
26813 cp_parser_functional_cast (cp_parser* parser, tree type)
26815 vec<tree, va_gc> *vec;
26816 tree expression_list;
26817 cp_expr cast;
26818 bool nonconst_p;
26820 location_t start_loc = input_location;
26822 if (!type)
26823 type = error_mark_node;
26825 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26827 cp_lexer_set_source_position (parser->lexer);
26828 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26829 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26830 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26831 if (TREE_CODE (type) == TYPE_DECL)
26832 type = TREE_TYPE (type);
26834 cast = finish_compound_literal (type, expression_list,
26835 tf_warning_or_error, fcl_functional);
26836 /* Create a location of the form:
26837 type_name{i, f}
26838 ^~~~~~~~~~~~~~~
26839 with caret == start at the start of the type name,
26840 finishing at the closing brace. */
26841 location_t finish_loc
26842 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26843 location_t combined_loc = make_location (start_loc, start_loc,
26844 finish_loc);
26845 cast.set_location (combined_loc);
26846 return cast;
26850 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26851 /*cast_p=*/true,
26852 /*allow_expansion_p=*/true,
26853 /*non_constant_p=*/NULL);
26854 if (vec == NULL)
26855 expression_list = error_mark_node;
26856 else
26858 expression_list = build_tree_list_vec (vec);
26859 release_tree_vector (vec);
26862 cast = build_functional_cast (type, expression_list,
26863 tf_warning_or_error);
26864 /* [expr.const]/1: In an integral constant expression "only type
26865 conversions to integral or enumeration type can be used". */
26866 if (TREE_CODE (type) == TYPE_DECL)
26867 type = TREE_TYPE (type);
26868 if (cast != error_mark_node
26869 && !cast_valid_in_integral_constant_expression_p (type)
26870 && cp_parser_non_integral_constant_expression (parser,
26871 NIC_CONSTRUCTOR))
26872 return error_mark_node;
26874 /* Create a location of the form:
26875 float(i)
26876 ^~~~~~~~
26877 with caret == start at the start of the type name,
26878 finishing at the closing paren. */
26879 location_t finish_loc
26880 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26881 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26882 cast.set_location (combined_loc);
26883 return cast;
26886 /* Save the tokens that make up the body of a member function defined
26887 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26888 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26889 specifiers applied to the declaration. Returns the FUNCTION_DECL
26890 for the member function. */
26892 static tree
26893 cp_parser_save_member_function_body (cp_parser* parser,
26894 cp_decl_specifier_seq *decl_specifiers,
26895 cp_declarator *declarator,
26896 tree attributes)
26898 cp_token *first;
26899 cp_token *last;
26900 tree fn;
26901 bool function_try_block = false;
26903 /* Create the FUNCTION_DECL. */
26904 fn = grokmethod (decl_specifiers, declarator, attributes);
26905 cp_finalize_omp_declare_simd (parser, fn);
26906 cp_finalize_oacc_routine (parser, fn, true);
26907 /* If something went badly wrong, bail out now. */
26908 if (fn == error_mark_node)
26910 /* If there's a function-body, skip it. */
26911 if (cp_parser_token_starts_function_definition_p
26912 (cp_lexer_peek_token (parser->lexer)))
26913 cp_parser_skip_to_end_of_block_or_statement (parser);
26914 return error_mark_node;
26917 /* Remember it, if there default args to post process. */
26918 cp_parser_save_default_args (parser, fn);
26920 /* Save away the tokens that make up the body of the
26921 function. */
26922 first = parser->lexer->next_token;
26924 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26925 cp_lexer_consume_token (parser->lexer);
26926 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26927 RID_TRANSACTION_ATOMIC))
26929 cp_lexer_consume_token (parser->lexer);
26930 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26931 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26932 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26933 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26934 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26935 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26936 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26938 cp_lexer_consume_token (parser->lexer);
26939 cp_lexer_consume_token (parser->lexer);
26940 cp_lexer_consume_token (parser->lexer);
26941 cp_lexer_consume_token (parser->lexer);
26942 cp_lexer_consume_token (parser->lexer);
26944 else
26945 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26946 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26948 cp_lexer_consume_token (parser->lexer);
26949 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26950 break;
26954 /* Handle function try blocks. */
26955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26957 cp_lexer_consume_token (parser->lexer);
26958 function_try_block = true;
26960 /* We can have braced-init-list mem-initializers before the fn body. */
26961 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26963 cp_lexer_consume_token (parser->lexer);
26964 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26966 /* cache_group will stop after an un-nested { } pair, too. */
26967 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26968 break;
26970 /* variadic mem-inits have ... after the ')'. */
26971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26972 cp_lexer_consume_token (parser->lexer);
26975 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26976 /* Handle function try blocks. */
26977 if (function_try_block)
26978 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26979 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26980 last = parser->lexer->next_token;
26982 /* Save away the inline definition; we will process it when the
26983 class is complete. */
26984 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26985 DECL_PENDING_INLINE_P (fn) = 1;
26987 /* We need to know that this was defined in the class, so that
26988 friend templates are handled correctly. */
26989 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26991 /* Add FN to the queue of functions to be parsed later. */
26992 vec_safe_push (unparsed_funs_with_definitions, fn);
26994 return fn;
26997 /* Save the tokens that make up the in-class initializer for a non-static
26998 data member. Returns a DEFAULT_ARG. */
27000 static tree
27001 cp_parser_save_nsdmi (cp_parser* parser)
27003 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27006 /* Parse a template-argument-list, as well as the trailing ">" (but
27007 not the opening "<"). See cp_parser_template_argument_list for the
27008 return value. */
27010 static tree
27011 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27013 tree arguments;
27014 tree saved_scope;
27015 tree saved_qualifying_scope;
27016 tree saved_object_scope;
27017 bool saved_greater_than_is_operator_p;
27018 int saved_unevaluated_operand;
27019 int saved_inhibit_evaluation_warnings;
27021 /* [temp.names]
27023 When parsing a template-id, the first non-nested `>' is taken as
27024 the end of the template-argument-list rather than a greater-than
27025 operator. */
27026 saved_greater_than_is_operator_p
27027 = parser->greater_than_is_operator_p;
27028 parser->greater_than_is_operator_p = false;
27029 /* Parsing the argument list may modify SCOPE, so we save it
27030 here. */
27031 saved_scope = parser->scope;
27032 saved_qualifying_scope = parser->qualifying_scope;
27033 saved_object_scope = parser->object_scope;
27034 /* We need to evaluate the template arguments, even though this
27035 template-id may be nested within a "sizeof". */
27036 saved_unevaluated_operand = cp_unevaluated_operand;
27037 cp_unevaluated_operand = 0;
27038 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27039 c_inhibit_evaluation_warnings = 0;
27040 /* Parse the template-argument-list itself. */
27041 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27042 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27043 arguments = NULL_TREE;
27044 else
27045 arguments = cp_parser_template_argument_list (parser);
27046 /* Look for the `>' that ends the template-argument-list. If we find
27047 a '>>' instead, it's probably just a typo. */
27048 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27050 if (cxx_dialect != cxx98)
27052 /* In C++0x, a `>>' in a template argument list or cast
27053 expression is considered to be two separate `>'
27054 tokens. So, change the current token to a `>', but don't
27055 consume it: it will be consumed later when the outer
27056 template argument list (or cast expression) is parsed.
27057 Note that this replacement of `>' for `>>' is necessary
27058 even if we are parsing tentatively: in the tentative
27059 case, after calling
27060 cp_parser_enclosed_template_argument_list we will always
27061 throw away all of the template arguments and the first
27062 closing `>', either because the template argument list
27063 was erroneous or because we are replacing those tokens
27064 with a CPP_TEMPLATE_ID token. The second `>' (which will
27065 not have been thrown away) is needed either to close an
27066 outer template argument list or to complete a new-style
27067 cast. */
27068 cp_token *token = cp_lexer_peek_token (parser->lexer);
27069 token->type = CPP_GREATER;
27071 else if (!saved_greater_than_is_operator_p)
27073 /* If we're in a nested template argument list, the '>>' has
27074 to be a typo for '> >'. We emit the error message, but we
27075 continue parsing and we push a '>' as next token, so that
27076 the argument list will be parsed correctly. Note that the
27077 global source location is still on the token before the
27078 '>>', so we need to say explicitly where we want it. */
27079 cp_token *token = cp_lexer_peek_token (parser->lexer);
27080 gcc_rich_location richloc (token->location);
27081 richloc.add_fixit_replace ("> >");
27082 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27083 "within a nested template argument list");
27085 token->type = CPP_GREATER;
27087 else
27089 /* If this is not a nested template argument list, the '>>'
27090 is a typo for '>'. Emit an error message and continue.
27091 Same deal about the token location, but here we can get it
27092 right by consuming the '>>' before issuing the diagnostic. */
27093 cp_token *token = cp_lexer_consume_token (parser->lexer);
27094 error_at (token->location,
27095 "spurious %<>>%>, use %<>%> to terminate "
27096 "a template argument list");
27099 else
27100 cp_parser_skip_to_end_of_template_parameter_list (parser);
27101 /* The `>' token might be a greater-than operator again now. */
27102 parser->greater_than_is_operator_p
27103 = saved_greater_than_is_operator_p;
27104 /* Restore the SAVED_SCOPE. */
27105 parser->scope = saved_scope;
27106 parser->qualifying_scope = saved_qualifying_scope;
27107 parser->object_scope = saved_object_scope;
27108 cp_unevaluated_operand = saved_unevaluated_operand;
27109 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27111 return arguments;
27114 /* MEMBER_FUNCTION is a member function, or a friend. If default
27115 arguments, or the body of the function have not yet been parsed,
27116 parse them now. */
27118 static void
27119 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27121 timevar_push (TV_PARSE_INMETH);
27122 /* If this member is a template, get the underlying
27123 FUNCTION_DECL. */
27124 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27125 member_function = DECL_TEMPLATE_RESULT (member_function);
27127 /* There should not be any class definitions in progress at this
27128 point; the bodies of members are only parsed outside of all class
27129 definitions. */
27130 gcc_assert (parser->num_classes_being_defined == 0);
27131 /* While we're parsing the member functions we might encounter more
27132 classes. We want to handle them right away, but we don't want
27133 them getting mixed up with functions that are currently in the
27134 queue. */
27135 push_unparsed_function_queues (parser);
27137 /* Make sure that any template parameters are in scope. */
27138 maybe_begin_member_template_processing (member_function);
27140 /* If the body of the function has not yet been parsed, parse it
27141 now. */
27142 if (DECL_PENDING_INLINE_P (member_function))
27144 tree function_scope;
27145 cp_token_cache *tokens;
27147 /* The function is no longer pending; we are processing it. */
27148 tokens = DECL_PENDING_INLINE_INFO (member_function);
27149 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27150 DECL_PENDING_INLINE_P (member_function) = 0;
27152 /* If this is a local class, enter the scope of the containing
27153 function. */
27154 function_scope = current_function_decl;
27155 if (function_scope)
27156 push_function_context ();
27158 /* Push the body of the function onto the lexer stack. */
27159 cp_parser_push_lexer_for_tokens (parser, tokens);
27161 /* Let the front end know that we going to be defining this
27162 function. */
27163 start_preparsed_function (member_function, NULL_TREE,
27164 SF_PRE_PARSED | SF_INCLASS_INLINE);
27166 /* Don't do access checking if it is a templated function. */
27167 if (processing_template_decl)
27168 push_deferring_access_checks (dk_no_check);
27170 /* #pragma omp declare reduction needs special parsing. */
27171 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27173 parser->lexer->in_pragma = true;
27174 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27175 finish_function (/*inline*/2);
27176 cp_check_omp_declare_reduction (member_function);
27178 else
27179 /* Now, parse the body of the function. */
27180 cp_parser_function_definition_after_declarator (parser,
27181 /*inline_p=*/true);
27183 if (processing_template_decl)
27184 pop_deferring_access_checks ();
27186 /* Leave the scope of the containing function. */
27187 if (function_scope)
27188 pop_function_context ();
27189 cp_parser_pop_lexer (parser);
27192 /* Remove any template parameters from the symbol table. */
27193 maybe_end_member_template_processing ();
27195 /* Restore the queue. */
27196 pop_unparsed_function_queues (parser);
27197 timevar_pop (TV_PARSE_INMETH);
27200 /* If DECL contains any default args, remember it on the unparsed
27201 functions queue. */
27203 static void
27204 cp_parser_save_default_args (cp_parser* parser, tree decl)
27206 tree probe;
27208 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27209 probe;
27210 probe = TREE_CHAIN (probe))
27211 if (TREE_PURPOSE (probe))
27213 cp_default_arg_entry entry = {current_class_type, decl};
27214 vec_safe_push (unparsed_funs_with_default_args, entry);
27215 break;
27219 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27220 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27221 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27222 from the parameter-type-list. */
27224 static tree
27225 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27226 tree default_arg, tree parmtype)
27228 cp_token_cache *tokens;
27229 tree parsed_arg;
27230 bool dummy;
27232 if (default_arg == error_mark_node)
27233 return error_mark_node;
27235 /* Push the saved tokens for the default argument onto the parser's
27236 lexer stack. */
27237 tokens = DEFARG_TOKENS (default_arg);
27238 cp_parser_push_lexer_for_tokens (parser, tokens);
27240 start_lambda_scope (decl);
27242 /* Parse the default argument. */
27243 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27244 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27245 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27247 finish_lambda_scope ();
27249 if (parsed_arg == error_mark_node)
27250 cp_parser_skip_to_end_of_statement (parser);
27252 if (!processing_template_decl)
27254 /* In a non-template class, check conversions now. In a template,
27255 we'll wait and instantiate these as needed. */
27256 if (TREE_CODE (decl) == PARM_DECL)
27257 parsed_arg = check_default_argument (parmtype, parsed_arg,
27258 tf_warning_or_error);
27259 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27260 parsed_arg = error_mark_node;
27261 else
27262 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27265 /* If the token stream has not been completely used up, then
27266 there was extra junk after the end of the default
27267 argument. */
27268 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27270 if (TREE_CODE (decl) == PARM_DECL)
27271 cp_parser_error (parser, "expected %<,%>");
27272 else
27273 cp_parser_error (parser, "expected %<;%>");
27276 /* Revert to the main lexer. */
27277 cp_parser_pop_lexer (parser);
27279 return parsed_arg;
27282 /* FIELD is a non-static data member with an initializer which we saved for
27283 later; parse it now. */
27285 static void
27286 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27288 tree def;
27290 maybe_begin_member_template_processing (field);
27292 push_unparsed_function_queues (parser);
27293 def = cp_parser_late_parse_one_default_arg (parser, field,
27294 DECL_INITIAL (field),
27295 NULL_TREE);
27296 pop_unparsed_function_queues (parser);
27298 maybe_end_member_template_processing ();
27300 DECL_INITIAL (field) = def;
27303 /* FN is a FUNCTION_DECL which may contains a parameter with an
27304 unparsed DEFAULT_ARG. Parse the default args now. This function
27305 assumes that the current scope is the scope in which the default
27306 argument should be processed. */
27308 static void
27309 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27311 bool saved_local_variables_forbidden_p;
27312 tree parm, parmdecl;
27314 /* While we're parsing the default args, we might (due to the
27315 statement expression extension) encounter more classes. We want
27316 to handle them right away, but we don't want them getting mixed
27317 up with default args that are currently in the queue. */
27318 push_unparsed_function_queues (parser);
27320 /* Local variable names (and the `this' keyword) may not appear
27321 in a default argument. */
27322 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27323 parser->local_variables_forbidden_p = true;
27325 push_defarg_context (fn);
27327 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27328 parmdecl = DECL_ARGUMENTS (fn);
27329 parm && parm != void_list_node;
27330 parm = TREE_CHAIN (parm),
27331 parmdecl = DECL_CHAIN (parmdecl))
27333 tree default_arg = TREE_PURPOSE (parm);
27334 tree parsed_arg;
27335 vec<tree, va_gc> *insts;
27336 tree copy;
27337 unsigned ix;
27339 if (!default_arg)
27340 continue;
27342 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27343 /* This can happen for a friend declaration for a function
27344 already declared with default arguments. */
27345 continue;
27347 parsed_arg
27348 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27349 default_arg,
27350 TREE_VALUE (parm));
27351 if (parsed_arg == error_mark_node)
27353 continue;
27356 TREE_PURPOSE (parm) = parsed_arg;
27358 /* Update any instantiations we've already created. */
27359 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27360 vec_safe_iterate (insts, ix, &copy); ix++)
27361 TREE_PURPOSE (copy) = parsed_arg;
27364 pop_defarg_context ();
27366 /* Make sure no default arg is missing. */
27367 check_default_args (fn);
27369 /* Restore the state of local_variables_forbidden_p. */
27370 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27372 /* Restore the queue. */
27373 pop_unparsed_function_queues (parser);
27376 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27378 sizeof ... ( identifier )
27380 where the 'sizeof' token has already been consumed. */
27382 static tree
27383 cp_parser_sizeof_pack (cp_parser *parser)
27385 /* Consume the `...'. */
27386 cp_lexer_consume_token (parser->lexer);
27387 maybe_warn_variadic_templates ();
27389 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27390 if (paren)
27391 cp_lexer_consume_token (parser->lexer);
27392 else
27393 permerror (cp_lexer_peek_token (parser->lexer)->location,
27394 "%<sizeof...%> argument must be surrounded by parentheses");
27396 cp_token *token = cp_lexer_peek_token (parser->lexer);
27397 tree name = cp_parser_identifier (parser);
27398 if (name == error_mark_node)
27399 return error_mark_node;
27400 /* The name is not qualified. */
27401 parser->scope = NULL_TREE;
27402 parser->qualifying_scope = NULL_TREE;
27403 parser->object_scope = NULL_TREE;
27404 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27405 if (expr == error_mark_node)
27406 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27407 token->location);
27408 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27409 expr = TREE_TYPE (expr);
27410 else if (TREE_CODE (expr) == CONST_DECL)
27411 expr = DECL_INITIAL (expr);
27412 expr = make_pack_expansion (expr);
27413 PACK_EXPANSION_SIZEOF_P (expr) = true;
27415 if (paren)
27416 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27418 return expr;
27421 /* Parse the operand of `sizeof' (or a similar operator). Returns
27422 either a TYPE or an expression, depending on the form of the
27423 input. The KEYWORD indicates which kind of expression we have
27424 encountered. */
27426 static tree
27427 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27429 tree expr = NULL_TREE;
27430 const char *saved_message;
27431 char *tmp;
27432 bool saved_integral_constant_expression_p;
27433 bool saved_non_integral_constant_expression_p;
27435 /* If it's a `...', then we are computing the length of a parameter
27436 pack. */
27437 if (keyword == RID_SIZEOF
27438 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27439 return cp_parser_sizeof_pack (parser);
27441 /* Types cannot be defined in a `sizeof' expression. Save away the
27442 old message. */
27443 saved_message = parser->type_definition_forbidden_message;
27444 /* And create the new one. */
27445 tmp = concat ("types may not be defined in %<",
27446 IDENTIFIER_POINTER (ridpointers[keyword]),
27447 "%> expressions", NULL);
27448 parser->type_definition_forbidden_message = tmp;
27450 /* The restrictions on constant-expressions do not apply inside
27451 sizeof expressions. */
27452 saved_integral_constant_expression_p
27453 = parser->integral_constant_expression_p;
27454 saved_non_integral_constant_expression_p
27455 = parser->non_integral_constant_expression_p;
27456 parser->integral_constant_expression_p = false;
27458 /* Do not actually evaluate the expression. */
27459 ++cp_unevaluated_operand;
27460 ++c_inhibit_evaluation_warnings;
27461 /* If it's a `(', then we might be looking at the type-id
27462 construction. */
27463 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27465 tree type = NULL_TREE;
27467 /* We can't be sure yet whether we're looking at a type-id or an
27468 expression. */
27469 cp_parser_parse_tentatively (parser);
27470 /* Note: as a GNU Extension, compound literals are considered
27471 postfix-expressions as they are in C99, so they are valid
27472 arguments to sizeof. See comment in cp_parser_cast_expression
27473 for details. */
27474 if (cp_parser_compound_literal_p (parser))
27475 cp_parser_simulate_error (parser);
27476 else
27478 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27479 parser->in_type_id_in_expr_p = true;
27480 /* Look for the type-id. */
27481 type = cp_parser_type_id (parser);
27482 /* Look for the closing `)'. */
27483 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27484 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27487 /* If all went well, then we're done. */
27488 if (cp_parser_parse_definitely (parser))
27490 cp_decl_specifier_seq decl_specs;
27492 /* Build a trivial decl-specifier-seq. */
27493 clear_decl_specs (&decl_specs);
27494 decl_specs.type = type;
27496 /* Call grokdeclarator to figure out what type this is. */
27497 expr = grokdeclarator (NULL,
27498 &decl_specs,
27499 TYPENAME,
27500 /*initialized=*/0,
27501 /*attrlist=*/NULL);
27505 /* If the type-id production did not work out, then we must be
27506 looking at the unary-expression production. */
27507 if (!expr)
27508 expr = cp_parser_unary_expression (parser);
27510 /* Go back to evaluating expressions. */
27511 --cp_unevaluated_operand;
27512 --c_inhibit_evaluation_warnings;
27514 /* Free the message we created. */
27515 free (tmp);
27516 /* And restore the old one. */
27517 parser->type_definition_forbidden_message = saved_message;
27518 parser->integral_constant_expression_p
27519 = saved_integral_constant_expression_p;
27520 parser->non_integral_constant_expression_p
27521 = saved_non_integral_constant_expression_p;
27523 return expr;
27526 /* If the current declaration has no declarator, return true. */
27528 static bool
27529 cp_parser_declares_only_class_p (cp_parser *parser)
27531 /* If the next token is a `;' or a `,' then there is no
27532 declarator. */
27533 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27534 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27537 /* Update the DECL_SPECS to reflect the storage class indicated by
27538 KEYWORD. */
27540 static void
27541 cp_parser_set_storage_class (cp_parser *parser,
27542 cp_decl_specifier_seq *decl_specs,
27543 enum rid keyword,
27544 cp_token *token)
27546 cp_storage_class storage_class;
27548 if (parser->in_unbraced_linkage_specification_p)
27550 error_at (token->location, "invalid use of %qD in linkage specification",
27551 ridpointers[keyword]);
27552 return;
27554 else if (decl_specs->storage_class != sc_none)
27556 decl_specs->conflicting_specifiers_p = true;
27557 return;
27560 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27561 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27562 && decl_specs->gnu_thread_keyword_p)
27564 pedwarn (decl_specs->locations[ds_thread], 0,
27565 "%<__thread%> before %qD", ridpointers[keyword]);
27568 switch (keyword)
27570 case RID_AUTO:
27571 storage_class = sc_auto;
27572 break;
27573 case RID_REGISTER:
27574 storage_class = sc_register;
27575 break;
27576 case RID_STATIC:
27577 storage_class = sc_static;
27578 break;
27579 case RID_EXTERN:
27580 storage_class = sc_extern;
27581 break;
27582 case RID_MUTABLE:
27583 storage_class = sc_mutable;
27584 break;
27585 default:
27586 gcc_unreachable ();
27588 decl_specs->storage_class = storage_class;
27589 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27591 /* A storage class specifier cannot be applied alongside a typedef
27592 specifier. If there is a typedef specifier present then set
27593 conflicting_specifiers_p which will trigger an error later
27594 on in grokdeclarator. */
27595 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27596 decl_specs->conflicting_specifiers_p = true;
27599 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27600 is true, the type is a class or enum definition. */
27602 static void
27603 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27604 tree type_spec,
27605 cp_token *token,
27606 bool type_definition_p)
27608 decl_specs->any_specifiers_p = true;
27610 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27611 (with, for example, in "typedef int wchar_t;") we remember that
27612 this is what happened. In system headers, we ignore these
27613 declarations so that G++ can work with system headers that are not
27614 C++-safe. */
27615 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27616 && !type_definition_p
27617 && (type_spec == boolean_type_node
27618 || type_spec == char16_type_node
27619 || type_spec == char32_type_node
27620 || type_spec == wchar_type_node)
27621 && (decl_specs->type
27622 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27623 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27624 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27625 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27627 decl_specs->redefined_builtin_type = type_spec;
27628 set_and_check_decl_spec_loc (decl_specs,
27629 ds_redefined_builtin_type_spec,
27630 token);
27631 if (!decl_specs->type)
27633 decl_specs->type = type_spec;
27634 decl_specs->type_definition_p = false;
27635 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27638 else if (decl_specs->type)
27639 decl_specs->multiple_types_p = true;
27640 else
27642 decl_specs->type = type_spec;
27643 decl_specs->type_definition_p = type_definition_p;
27644 decl_specs->redefined_builtin_type = NULL_TREE;
27645 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27649 /* True iff TOKEN is the GNU keyword __thread. */
27651 static bool
27652 token_is__thread (cp_token *token)
27654 gcc_assert (token->keyword == RID_THREAD);
27655 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27658 /* Set the location for a declarator specifier and check if it is
27659 duplicated.
27661 DECL_SPECS is the sequence of declarator specifiers onto which to
27662 set the location.
27664 DS is the single declarator specifier to set which location is to
27665 be set onto the existing sequence of declarators.
27667 LOCATION is the location for the declarator specifier to
27668 consider. */
27670 static void
27671 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27672 cp_decl_spec ds, cp_token *token)
27674 gcc_assert (ds < ds_last);
27676 if (decl_specs == NULL)
27677 return;
27679 source_location location = token->location;
27681 if (decl_specs->locations[ds] == 0)
27683 decl_specs->locations[ds] = location;
27684 if (ds == ds_thread)
27685 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27687 else
27689 if (ds == ds_long)
27691 if (decl_specs->locations[ds_long_long] != 0)
27692 error_at (location,
27693 "%<long long long%> is too long for GCC");
27694 else
27696 decl_specs->locations[ds_long_long] = location;
27697 pedwarn_cxx98 (location,
27698 OPT_Wlong_long,
27699 "ISO C++ 1998 does not support %<long long%>");
27702 else if (ds == ds_thread)
27704 bool gnu = token_is__thread (token);
27705 if (gnu != decl_specs->gnu_thread_keyword_p)
27706 error_at (location,
27707 "both %<__thread%> and %<thread_local%> specified");
27708 else
27710 gcc_rich_location richloc (location);
27711 richloc.add_fixit_remove ();
27712 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27715 else
27717 static const char *const decl_spec_names[] = {
27718 "signed",
27719 "unsigned",
27720 "short",
27721 "long",
27722 "const",
27723 "volatile",
27724 "restrict",
27725 "inline",
27726 "virtual",
27727 "explicit",
27728 "friend",
27729 "typedef",
27730 "using",
27731 "constexpr",
27732 "__complex"
27734 gcc_rich_location richloc (location);
27735 richloc.add_fixit_remove ();
27736 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27741 /* Return true iff the declarator specifier DS is present in the
27742 sequence of declarator specifiers DECL_SPECS. */
27744 bool
27745 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27746 cp_decl_spec ds)
27748 gcc_assert (ds < ds_last);
27750 if (decl_specs == NULL)
27751 return false;
27753 return decl_specs->locations[ds] != 0;
27756 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27757 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27759 static bool
27760 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27762 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27765 /* Issue an error message indicating that TOKEN_DESC was expected.
27766 If KEYWORD is true, it indicated this function is called by
27767 cp_parser_require_keword and the required token can only be
27768 a indicated keyword. */
27770 static void
27771 cp_parser_required_error (cp_parser *parser,
27772 required_token token_desc,
27773 bool keyword)
27775 switch (token_desc)
27777 case RT_NEW:
27778 cp_parser_error (parser, "expected %<new%>");
27779 return;
27780 case RT_DELETE:
27781 cp_parser_error (parser, "expected %<delete%>");
27782 return;
27783 case RT_RETURN:
27784 cp_parser_error (parser, "expected %<return%>");
27785 return;
27786 case RT_WHILE:
27787 cp_parser_error (parser, "expected %<while%>");
27788 return;
27789 case RT_EXTERN:
27790 cp_parser_error (parser, "expected %<extern%>");
27791 return;
27792 case RT_STATIC_ASSERT:
27793 cp_parser_error (parser, "expected %<static_assert%>");
27794 return;
27795 case RT_DECLTYPE:
27796 cp_parser_error (parser, "expected %<decltype%>");
27797 return;
27798 case RT_OPERATOR:
27799 cp_parser_error (parser, "expected %<operator%>");
27800 return;
27801 case RT_CLASS:
27802 cp_parser_error (parser, "expected %<class%>");
27803 return;
27804 case RT_TEMPLATE:
27805 cp_parser_error (parser, "expected %<template%>");
27806 return;
27807 case RT_NAMESPACE:
27808 cp_parser_error (parser, "expected %<namespace%>");
27809 return;
27810 case RT_USING:
27811 cp_parser_error (parser, "expected %<using%>");
27812 return;
27813 case RT_ASM:
27814 cp_parser_error (parser, "expected %<asm%>");
27815 return;
27816 case RT_TRY:
27817 cp_parser_error (parser, "expected %<try%>");
27818 return;
27819 case RT_CATCH:
27820 cp_parser_error (parser, "expected %<catch%>");
27821 return;
27822 case RT_THROW:
27823 cp_parser_error (parser, "expected %<throw%>");
27824 return;
27825 case RT_LABEL:
27826 cp_parser_error (parser, "expected %<__label__%>");
27827 return;
27828 case RT_AT_TRY:
27829 cp_parser_error (parser, "expected %<@try%>");
27830 return;
27831 case RT_AT_SYNCHRONIZED:
27832 cp_parser_error (parser, "expected %<@synchronized%>");
27833 return;
27834 case RT_AT_THROW:
27835 cp_parser_error (parser, "expected %<@throw%>");
27836 return;
27837 case RT_TRANSACTION_ATOMIC:
27838 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27839 return;
27840 case RT_TRANSACTION_RELAXED:
27841 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27842 return;
27843 default:
27844 break;
27846 if (!keyword)
27848 switch (token_desc)
27850 case RT_SEMICOLON:
27851 cp_parser_error (parser, "expected %<;%>");
27852 return;
27853 case RT_OPEN_PAREN:
27854 cp_parser_error (parser, "expected %<(%>");
27855 return;
27856 case RT_CLOSE_BRACE:
27857 cp_parser_error (parser, "expected %<}%>");
27858 return;
27859 case RT_OPEN_BRACE:
27860 cp_parser_error (parser, "expected %<{%>");
27861 return;
27862 case RT_CLOSE_SQUARE:
27863 cp_parser_error (parser, "expected %<]%>");
27864 return;
27865 case RT_OPEN_SQUARE:
27866 cp_parser_error (parser, "expected %<[%>");
27867 return;
27868 case RT_COMMA:
27869 cp_parser_error (parser, "expected %<,%>");
27870 return;
27871 case RT_SCOPE:
27872 cp_parser_error (parser, "expected %<::%>");
27873 return;
27874 case RT_LESS:
27875 cp_parser_error (parser, "expected %<<%>");
27876 return;
27877 case RT_GREATER:
27878 cp_parser_error (parser, "expected %<>%>");
27879 return;
27880 case RT_EQ:
27881 cp_parser_error (parser, "expected %<=%>");
27882 return;
27883 case RT_ELLIPSIS:
27884 cp_parser_error (parser, "expected %<...%>");
27885 return;
27886 case RT_MULT:
27887 cp_parser_error (parser, "expected %<*%>");
27888 return;
27889 case RT_COMPL:
27890 cp_parser_error (parser, "expected %<~%>");
27891 return;
27892 case RT_COLON:
27893 cp_parser_error (parser, "expected %<:%>");
27894 return;
27895 case RT_COLON_SCOPE:
27896 cp_parser_error (parser, "expected %<:%> or %<::%>");
27897 return;
27898 case RT_CLOSE_PAREN:
27899 cp_parser_error (parser, "expected %<)%>");
27900 return;
27901 case RT_COMMA_CLOSE_PAREN:
27902 cp_parser_error (parser, "expected %<,%> or %<)%>");
27903 return;
27904 case RT_PRAGMA_EOL:
27905 cp_parser_error (parser, "expected end of line");
27906 return;
27907 case RT_NAME:
27908 cp_parser_error (parser, "expected identifier");
27909 return;
27910 case RT_SELECT:
27911 cp_parser_error (parser, "expected selection-statement");
27912 return;
27913 case RT_INTERATION:
27914 cp_parser_error (parser, "expected iteration-statement");
27915 return;
27916 case RT_JUMP:
27917 cp_parser_error (parser, "expected jump-statement");
27918 return;
27919 case RT_CLASS_KEY:
27920 cp_parser_error (parser, "expected class-key");
27921 return;
27922 case RT_CLASS_TYPENAME_TEMPLATE:
27923 cp_parser_error (parser,
27924 "expected %<class%>, %<typename%>, or %<template%>");
27925 return;
27926 default:
27927 gcc_unreachable ();
27930 else
27931 gcc_unreachable ();
27936 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27937 issue an error message indicating that TOKEN_DESC was expected.
27939 Returns the token consumed, if the token had the appropriate type.
27940 Otherwise, returns NULL. */
27942 static cp_token *
27943 cp_parser_require (cp_parser* parser,
27944 enum cpp_ttype type,
27945 required_token token_desc)
27947 if (cp_lexer_next_token_is (parser->lexer, type))
27948 return cp_lexer_consume_token (parser->lexer);
27949 else
27951 /* Output the MESSAGE -- unless we're parsing tentatively. */
27952 if (!cp_parser_simulate_error (parser))
27953 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27954 return NULL;
27958 /* An error message is produced if the next token is not '>'.
27959 All further tokens are skipped until the desired token is
27960 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27962 static void
27963 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27965 /* Current level of '< ... >'. */
27966 unsigned level = 0;
27967 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27968 unsigned nesting_depth = 0;
27970 /* Are we ready, yet? If not, issue error message. */
27971 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27972 return;
27974 /* Skip tokens until the desired token is found. */
27975 while (true)
27977 /* Peek at the next token. */
27978 switch (cp_lexer_peek_token (parser->lexer)->type)
27980 case CPP_LESS:
27981 if (!nesting_depth)
27982 ++level;
27983 break;
27985 case CPP_RSHIFT:
27986 if (cxx_dialect == cxx98)
27987 /* C++0x views the `>>' operator as two `>' tokens, but
27988 C++98 does not. */
27989 break;
27990 else if (!nesting_depth && level-- == 0)
27992 /* We've hit a `>>' where the first `>' closes the
27993 template argument list, and the second `>' is
27994 spurious. Just consume the `>>' and stop; we've
27995 already produced at least one error. */
27996 cp_lexer_consume_token (parser->lexer);
27997 return;
27999 /* Fall through for C++0x, so we handle the second `>' in
28000 the `>>'. */
28001 gcc_fallthrough ();
28003 case CPP_GREATER:
28004 if (!nesting_depth && level-- == 0)
28006 /* We've reached the token we want, consume it and stop. */
28007 cp_lexer_consume_token (parser->lexer);
28008 return;
28010 break;
28012 case CPP_OPEN_PAREN:
28013 case CPP_OPEN_SQUARE:
28014 ++nesting_depth;
28015 break;
28017 case CPP_CLOSE_PAREN:
28018 case CPP_CLOSE_SQUARE:
28019 if (nesting_depth-- == 0)
28020 return;
28021 break;
28023 case CPP_EOF:
28024 case CPP_PRAGMA_EOL:
28025 case CPP_SEMICOLON:
28026 case CPP_OPEN_BRACE:
28027 case CPP_CLOSE_BRACE:
28028 /* The '>' was probably forgotten, don't look further. */
28029 return;
28031 default:
28032 break;
28035 /* Consume this token. */
28036 cp_lexer_consume_token (parser->lexer);
28040 /* If the next token is the indicated keyword, consume it. Otherwise,
28041 issue an error message indicating that TOKEN_DESC was expected.
28043 Returns the token consumed, if the token had the appropriate type.
28044 Otherwise, returns NULL. */
28046 static cp_token *
28047 cp_parser_require_keyword (cp_parser* parser,
28048 enum rid keyword,
28049 required_token token_desc)
28051 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28053 if (token && token->keyword != keyword)
28055 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28056 return NULL;
28059 return token;
28062 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28063 function-definition. */
28065 static bool
28066 cp_parser_token_starts_function_definition_p (cp_token* token)
28068 return (/* An ordinary function-body begins with an `{'. */
28069 token->type == CPP_OPEN_BRACE
28070 /* A ctor-initializer begins with a `:'. */
28071 || token->type == CPP_COLON
28072 /* A function-try-block begins with `try'. */
28073 || token->keyword == RID_TRY
28074 /* A function-transaction-block begins with `__transaction_atomic'
28075 or `__transaction_relaxed'. */
28076 || token->keyword == RID_TRANSACTION_ATOMIC
28077 || token->keyword == RID_TRANSACTION_RELAXED
28078 /* The named return value extension begins with `return'. */
28079 || token->keyword == RID_RETURN);
28082 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28083 definition. */
28085 static bool
28086 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28088 cp_token *token;
28090 token = cp_lexer_peek_token (parser->lexer);
28091 return (token->type == CPP_OPEN_BRACE
28092 || (token->type == CPP_COLON
28093 && !parser->colon_doesnt_start_class_def_p));
28096 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28097 C++0x) ending a template-argument. */
28099 static bool
28100 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28102 cp_token *token;
28104 token = cp_lexer_peek_token (parser->lexer);
28105 return (token->type == CPP_COMMA
28106 || token->type == CPP_GREATER
28107 || token->type == CPP_ELLIPSIS
28108 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28111 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28112 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28114 static bool
28115 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28116 size_t n)
28118 cp_token *token;
28120 token = cp_lexer_peek_nth_token (parser->lexer, n);
28121 if (token->type == CPP_LESS)
28122 return true;
28123 /* Check for the sequence `<::' in the original code. It would be lexed as
28124 `[:', where `[' is a digraph, and there is no whitespace before
28125 `:'. */
28126 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28128 cp_token *token2;
28129 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28130 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28131 return true;
28133 return false;
28136 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28137 or none_type otherwise. */
28139 static enum tag_types
28140 cp_parser_token_is_class_key (cp_token* token)
28142 switch (token->keyword)
28144 case RID_CLASS:
28145 return class_type;
28146 case RID_STRUCT:
28147 return record_type;
28148 case RID_UNION:
28149 return union_type;
28151 default:
28152 return none_type;
28156 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28157 or none_type otherwise or if the token is null. */
28159 static enum tag_types
28160 cp_parser_token_is_type_parameter_key (cp_token* token)
28162 if (!token)
28163 return none_type;
28165 switch (token->keyword)
28167 case RID_CLASS:
28168 return class_type;
28169 case RID_TYPENAME:
28170 return typename_type;
28172 default:
28173 return none_type;
28177 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28179 static void
28180 cp_parser_check_class_key (enum tag_types class_key, tree type)
28182 if (type == error_mark_node)
28183 return;
28184 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28186 if (permerror (input_location, "%qs tag used in naming %q#T",
28187 class_key == union_type ? "union"
28188 : class_key == record_type ? "struct" : "class",
28189 type))
28190 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28191 "%q#T was previously declared here", type);
28195 /* Issue an error message if DECL is redeclared with different
28196 access than its original declaration [class.access.spec/3].
28197 This applies to nested classes, nested class templates and
28198 enumerations [class.mem/1]. */
28200 static void
28201 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28203 if (!decl
28204 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28205 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28206 return;
28208 if ((TREE_PRIVATE (decl)
28209 != (current_access_specifier == access_private_node))
28210 || (TREE_PROTECTED (decl)
28211 != (current_access_specifier == access_protected_node)))
28212 error_at (location, "%qD redeclared with different access", decl);
28215 /* Look for the `template' keyword, as a syntactic disambiguator.
28216 Return TRUE iff it is present, in which case it will be
28217 consumed. */
28219 static bool
28220 cp_parser_optional_template_keyword (cp_parser *parser)
28222 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28224 /* In C++98 the `template' keyword can only be used within templates;
28225 outside templates the parser can always figure out what is a
28226 template and what is not. In C++11, per the resolution of DR 468,
28227 `template' is allowed in cases where it is not strictly necessary. */
28228 if (!processing_template_decl
28229 && pedantic && cxx_dialect == cxx98)
28231 cp_token *token = cp_lexer_peek_token (parser->lexer);
28232 pedwarn (token->location, OPT_Wpedantic,
28233 "in C++98 %<template%> (as a disambiguator) is only "
28234 "allowed within templates");
28235 /* If this part of the token stream is rescanned, the same
28236 error message would be generated. So, we purge the token
28237 from the stream. */
28238 cp_lexer_purge_token (parser->lexer);
28239 return false;
28241 else
28243 /* Consume the `template' keyword. */
28244 cp_lexer_consume_token (parser->lexer);
28245 return true;
28248 return false;
28251 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28252 set PARSER->SCOPE, and perform other related actions. */
28254 static void
28255 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28257 struct tree_check *check_value;
28259 /* Get the stored value. */
28260 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28261 /* Set the scope from the stored value. */
28262 parser->scope = saved_checks_value (check_value);
28263 parser->qualifying_scope = check_value->qualifying_scope;
28264 parser->object_scope = NULL_TREE;
28267 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28268 encounter the end of a block before what we were looking for. */
28270 static bool
28271 cp_parser_cache_group (cp_parser *parser,
28272 enum cpp_ttype end,
28273 unsigned depth)
28275 while (true)
28277 cp_token *token = cp_lexer_peek_token (parser->lexer);
28279 /* Abort a parenthesized expression if we encounter a semicolon. */
28280 if ((end == CPP_CLOSE_PAREN || depth == 0)
28281 && token->type == CPP_SEMICOLON)
28282 return true;
28283 /* If we've reached the end of the file, stop. */
28284 if (token->type == CPP_EOF
28285 || (end != CPP_PRAGMA_EOL
28286 && token->type == CPP_PRAGMA_EOL))
28287 return true;
28288 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28289 /* We've hit the end of an enclosing block, so there's been some
28290 kind of syntax error. */
28291 return true;
28293 /* Consume the token. */
28294 cp_lexer_consume_token (parser->lexer);
28295 /* See if it starts a new group. */
28296 if (token->type == CPP_OPEN_BRACE)
28298 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28299 /* In theory this should probably check end == '}', but
28300 cp_parser_save_member_function_body needs it to exit
28301 after either '}' or ')' when called with ')'. */
28302 if (depth == 0)
28303 return false;
28305 else if (token->type == CPP_OPEN_PAREN)
28307 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28308 if (depth == 0 && end == CPP_CLOSE_PAREN)
28309 return false;
28311 else if (token->type == CPP_PRAGMA)
28312 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28313 else if (token->type == end)
28314 return false;
28318 /* Like above, for caching a default argument or NSDMI. Both of these are
28319 terminated by a non-nested comma, but it can be unclear whether or not a
28320 comma is nested in a template argument list unless we do more parsing.
28321 In order to handle this ambiguity, when we encounter a ',' after a '<'
28322 we try to parse what follows as a parameter-declaration-list (in the
28323 case of a default argument) or a member-declarator (in the case of an
28324 NSDMI). If that succeeds, then we stop caching. */
28326 static tree
28327 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28329 unsigned depth = 0;
28330 int maybe_template_id = 0;
28331 cp_token *first_token;
28332 cp_token *token;
28333 tree default_argument;
28335 /* Add tokens until we have processed the entire default
28336 argument. We add the range [first_token, token). */
28337 first_token = cp_lexer_peek_token (parser->lexer);
28338 if (first_token->type == CPP_OPEN_BRACE)
28340 /* For list-initialization, this is straightforward. */
28341 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28342 token = cp_lexer_peek_token (parser->lexer);
28344 else while (true)
28346 bool done = false;
28348 /* Peek at the next token. */
28349 token = cp_lexer_peek_token (parser->lexer);
28350 /* What we do depends on what token we have. */
28351 switch (token->type)
28353 /* In valid code, a default argument must be
28354 immediately followed by a `,' `)', or `...'. */
28355 case CPP_COMMA:
28356 if (depth == 0 && maybe_template_id)
28358 /* If we've seen a '<', we might be in a
28359 template-argument-list. Until Core issue 325 is
28360 resolved, we don't know how this situation ought
28361 to be handled, so try to DTRT. We check whether
28362 what comes after the comma is a valid parameter
28363 declaration list. If it is, then the comma ends
28364 the default argument; otherwise the default
28365 argument continues. */
28366 bool error = false;
28367 cp_token *peek;
28369 /* Set ITALP so cp_parser_parameter_declaration_list
28370 doesn't decide to commit to this parse. */
28371 bool saved_italp = parser->in_template_argument_list_p;
28372 parser->in_template_argument_list_p = true;
28374 cp_parser_parse_tentatively (parser);
28376 if (nsdmi)
28378 /* Parse declarators until we reach a non-comma or
28379 somthing that cannot be an initializer.
28380 Just checking whether we're looking at a single
28381 declarator is insufficient. Consider:
28382 int var = tuple<T,U>::x;
28383 The template parameter 'U' looks exactly like a
28384 declarator. */
28387 int ctor_dtor_or_conv_p;
28388 cp_lexer_consume_token (parser->lexer);
28389 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28390 &ctor_dtor_or_conv_p,
28391 /*parenthesized_p=*/NULL,
28392 /*member_p=*/true,
28393 /*friend_p=*/false);
28394 peek = cp_lexer_peek_token (parser->lexer);
28395 if (cp_parser_error_occurred (parser))
28396 break;
28398 while (peek->type == CPP_COMMA);
28399 /* If we met an '=' or ';' then the original comma
28400 was the end of the NSDMI. Otherwise assume
28401 we're still in the NSDMI. */
28402 error = (peek->type != CPP_EQ
28403 && peek->type != CPP_SEMICOLON);
28405 else
28407 cp_lexer_consume_token (parser->lexer);
28408 begin_scope (sk_function_parms, NULL_TREE);
28409 cp_parser_parameter_declaration_list (parser, &error);
28410 pop_bindings_and_leave_scope ();
28412 if (!cp_parser_error_occurred (parser) && !error)
28413 done = true;
28414 cp_parser_abort_tentative_parse (parser);
28416 parser->in_template_argument_list_p = saved_italp;
28417 break;
28419 /* FALLTHRU */
28420 case CPP_CLOSE_PAREN:
28421 case CPP_ELLIPSIS:
28422 /* If we run into a non-nested `;', `}', or `]',
28423 then the code is invalid -- but the default
28424 argument is certainly over. */
28425 case CPP_SEMICOLON:
28426 case CPP_CLOSE_BRACE:
28427 case CPP_CLOSE_SQUARE:
28428 if (depth == 0
28429 /* Handle correctly int n = sizeof ... ( p ); */
28430 && token->type != CPP_ELLIPSIS)
28431 done = true;
28432 /* Update DEPTH, if necessary. */
28433 else if (token->type == CPP_CLOSE_PAREN
28434 || token->type == CPP_CLOSE_BRACE
28435 || token->type == CPP_CLOSE_SQUARE)
28436 --depth;
28437 break;
28439 case CPP_OPEN_PAREN:
28440 case CPP_OPEN_SQUARE:
28441 case CPP_OPEN_BRACE:
28442 ++depth;
28443 break;
28445 case CPP_LESS:
28446 if (depth == 0)
28447 /* This might be the comparison operator, or it might
28448 start a template argument list. */
28449 ++maybe_template_id;
28450 break;
28452 case CPP_RSHIFT:
28453 if (cxx_dialect == cxx98)
28454 break;
28455 /* Fall through for C++0x, which treats the `>>'
28456 operator like two `>' tokens in certain
28457 cases. */
28458 gcc_fallthrough ();
28460 case CPP_GREATER:
28461 if (depth == 0)
28463 /* This might be an operator, or it might close a
28464 template argument list. But if a previous '<'
28465 started a template argument list, this will have
28466 closed it, so we can't be in one anymore. */
28467 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28468 if (maybe_template_id < 0)
28469 maybe_template_id = 0;
28471 break;
28473 /* If we run out of tokens, issue an error message. */
28474 case CPP_EOF:
28475 case CPP_PRAGMA_EOL:
28476 error_at (token->location, "file ends in default argument");
28477 return error_mark_node;
28479 case CPP_NAME:
28480 case CPP_SCOPE:
28481 /* In these cases, we should look for template-ids.
28482 For example, if the default argument is
28483 `X<int, double>()', we need to do name lookup to
28484 figure out whether or not `X' is a template; if
28485 so, the `,' does not end the default argument.
28487 That is not yet done. */
28488 break;
28490 default:
28491 break;
28494 /* If we've reached the end, stop. */
28495 if (done)
28496 break;
28498 /* Add the token to the token block. */
28499 token = cp_lexer_consume_token (parser->lexer);
28502 /* Create a DEFAULT_ARG to represent the unparsed default
28503 argument. */
28504 default_argument = make_node (DEFAULT_ARG);
28505 DEFARG_TOKENS (default_argument)
28506 = cp_token_cache_new (first_token, token);
28507 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28509 return default_argument;
28512 /* Begin parsing tentatively. We always save tokens while parsing
28513 tentatively so that if the tentative parsing fails we can restore the
28514 tokens. */
28516 static void
28517 cp_parser_parse_tentatively (cp_parser* parser)
28519 /* Enter a new parsing context. */
28520 parser->context = cp_parser_context_new (parser->context);
28521 /* Begin saving tokens. */
28522 cp_lexer_save_tokens (parser->lexer);
28523 /* In order to avoid repetitive access control error messages,
28524 access checks are queued up until we are no longer parsing
28525 tentatively. */
28526 push_deferring_access_checks (dk_deferred);
28529 /* Commit to the currently active tentative parse. */
28531 static void
28532 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28534 cp_parser_context *context;
28535 cp_lexer *lexer;
28537 /* Mark all of the levels as committed. */
28538 lexer = parser->lexer;
28539 for (context = parser->context; context->next; context = context->next)
28541 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28542 break;
28543 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28544 while (!cp_lexer_saving_tokens (lexer))
28545 lexer = lexer->next;
28546 cp_lexer_commit_tokens (lexer);
28550 /* Commit to the topmost currently active tentative parse.
28552 Note that this function shouldn't be called when there are
28553 irreversible side-effects while in a tentative state. For
28554 example, we shouldn't create a permanent entry in the symbol
28555 table, or issue an error message that might not apply if the
28556 tentative parse is aborted. */
28558 static void
28559 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28561 cp_parser_context *context = parser->context;
28562 cp_lexer *lexer = parser->lexer;
28564 if (context)
28566 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28567 return;
28568 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28570 while (!cp_lexer_saving_tokens (lexer))
28571 lexer = lexer->next;
28572 cp_lexer_commit_tokens (lexer);
28576 /* Abort the currently active tentative parse. All consumed tokens
28577 will be rolled back, and no diagnostics will be issued. */
28579 static void
28580 cp_parser_abort_tentative_parse (cp_parser* parser)
28582 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28583 || errorcount > 0);
28584 cp_parser_simulate_error (parser);
28585 /* Now, pretend that we want to see if the construct was
28586 successfully parsed. */
28587 cp_parser_parse_definitely (parser);
28590 /* Stop parsing tentatively. If a parse error has occurred, restore the
28591 token stream. Otherwise, commit to the tokens we have consumed.
28592 Returns true if no error occurred; false otherwise. */
28594 static bool
28595 cp_parser_parse_definitely (cp_parser* parser)
28597 bool error_occurred;
28598 cp_parser_context *context;
28600 /* Remember whether or not an error occurred, since we are about to
28601 destroy that information. */
28602 error_occurred = cp_parser_error_occurred (parser);
28603 /* Remove the topmost context from the stack. */
28604 context = parser->context;
28605 parser->context = context->next;
28606 /* If no parse errors occurred, commit to the tentative parse. */
28607 if (!error_occurred)
28609 /* Commit to the tokens read tentatively, unless that was
28610 already done. */
28611 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28612 cp_lexer_commit_tokens (parser->lexer);
28614 pop_to_parent_deferring_access_checks ();
28616 /* Otherwise, if errors occurred, roll back our state so that things
28617 are just as they were before we began the tentative parse. */
28618 else
28620 cp_lexer_rollback_tokens (parser->lexer);
28621 pop_deferring_access_checks ();
28623 /* Add the context to the front of the free list. */
28624 context->next = cp_parser_context_free_list;
28625 cp_parser_context_free_list = context;
28627 return !error_occurred;
28630 /* Returns true if we are parsing tentatively and are not committed to
28631 this tentative parse. */
28633 static bool
28634 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28636 return (cp_parser_parsing_tentatively (parser)
28637 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28640 /* Returns nonzero iff an error has occurred during the most recent
28641 tentative parse. */
28643 static bool
28644 cp_parser_error_occurred (cp_parser* parser)
28646 return (cp_parser_parsing_tentatively (parser)
28647 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28650 /* Returns nonzero if GNU extensions are allowed. */
28652 static bool
28653 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28655 return parser->allow_gnu_extensions_p;
28658 /* Objective-C++ Productions */
28661 /* Parse an Objective-C expression, which feeds into a primary-expression
28662 above.
28664 objc-expression:
28665 objc-message-expression
28666 objc-string-literal
28667 objc-encode-expression
28668 objc-protocol-expression
28669 objc-selector-expression
28671 Returns a tree representation of the expression. */
28673 static cp_expr
28674 cp_parser_objc_expression (cp_parser* parser)
28676 /* Try to figure out what kind of declaration is present. */
28677 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28679 switch (kwd->type)
28681 case CPP_OPEN_SQUARE:
28682 return cp_parser_objc_message_expression (parser);
28684 case CPP_OBJC_STRING:
28685 kwd = cp_lexer_consume_token (parser->lexer);
28686 return objc_build_string_object (kwd->u.value);
28688 case CPP_KEYWORD:
28689 switch (kwd->keyword)
28691 case RID_AT_ENCODE:
28692 return cp_parser_objc_encode_expression (parser);
28694 case RID_AT_PROTOCOL:
28695 return cp_parser_objc_protocol_expression (parser);
28697 case RID_AT_SELECTOR:
28698 return cp_parser_objc_selector_expression (parser);
28700 default:
28701 break;
28703 default:
28704 error_at (kwd->location,
28705 "misplaced %<@%D%> Objective-C++ construct",
28706 kwd->u.value);
28707 cp_parser_skip_to_end_of_block_or_statement (parser);
28710 return error_mark_node;
28713 /* Parse an Objective-C message expression.
28715 objc-message-expression:
28716 [ objc-message-receiver objc-message-args ]
28718 Returns a representation of an Objective-C message. */
28720 static tree
28721 cp_parser_objc_message_expression (cp_parser* parser)
28723 tree receiver, messageargs;
28725 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28726 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28727 receiver = cp_parser_objc_message_receiver (parser);
28728 messageargs = cp_parser_objc_message_args (parser);
28729 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28730 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28732 tree result = objc_build_message_expr (receiver, messageargs);
28734 /* Construct a location e.g.
28735 [self func1:5]
28736 ^~~~~~~~~~~~~~
28737 ranging from the '[' to the ']', with the caret at the start. */
28738 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28739 protected_set_expr_location (result, combined_loc);
28741 return result;
28744 /* Parse an objc-message-receiver.
28746 objc-message-receiver:
28747 expression
28748 simple-type-specifier
28750 Returns a representation of the type or expression. */
28752 static tree
28753 cp_parser_objc_message_receiver (cp_parser* parser)
28755 tree rcv;
28757 /* An Objective-C message receiver may be either (1) a type
28758 or (2) an expression. */
28759 cp_parser_parse_tentatively (parser);
28760 rcv = cp_parser_expression (parser);
28762 /* If that worked out, fine. */
28763 if (cp_parser_parse_definitely (parser))
28764 return rcv;
28766 cp_parser_parse_tentatively (parser);
28767 rcv = cp_parser_simple_type_specifier (parser,
28768 /*decl_specs=*/NULL,
28769 CP_PARSER_FLAGS_NONE);
28771 if (cp_parser_parse_definitely (parser))
28772 return objc_get_class_reference (rcv);
28774 cp_parser_error (parser, "objective-c++ message receiver expected");
28775 return error_mark_node;
28778 /* Parse the arguments and selectors comprising an Objective-C message.
28780 objc-message-args:
28781 objc-selector
28782 objc-selector-args
28783 objc-selector-args , objc-comma-args
28785 objc-selector-args:
28786 objc-selector [opt] : assignment-expression
28787 objc-selector-args objc-selector [opt] : assignment-expression
28789 objc-comma-args:
28790 assignment-expression
28791 objc-comma-args , assignment-expression
28793 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28794 selector arguments and TREE_VALUE containing a list of comma
28795 arguments. */
28797 static tree
28798 cp_parser_objc_message_args (cp_parser* parser)
28800 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28801 bool maybe_unary_selector_p = true;
28802 cp_token *token = cp_lexer_peek_token (parser->lexer);
28804 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28806 tree selector = NULL_TREE, arg;
28808 if (token->type != CPP_COLON)
28809 selector = cp_parser_objc_selector (parser);
28811 /* Detect if we have a unary selector. */
28812 if (maybe_unary_selector_p
28813 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28814 return build_tree_list (selector, NULL_TREE);
28816 maybe_unary_selector_p = false;
28817 cp_parser_require (parser, CPP_COLON, RT_COLON);
28818 arg = cp_parser_assignment_expression (parser);
28820 sel_args
28821 = chainon (sel_args,
28822 build_tree_list (selector, arg));
28824 token = cp_lexer_peek_token (parser->lexer);
28827 /* Handle non-selector arguments, if any. */
28828 while (token->type == CPP_COMMA)
28830 tree arg;
28832 cp_lexer_consume_token (parser->lexer);
28833 arg = cp_parser_assignment_expression (parser);
28835 addl_args
28836 = chainon (addl_args,
28837 build_tree_list (NULL_TREE, arg));
28839 token = cp_lexer_peek_token (parser->lexer);
28842 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28844 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28845 return build_tree_list (error_mark_node, error_mark_node);
28848 return build_tree_list (sel_args, addl_args);
28851 /* Parse an Objective-C encode expression.
28853 objc-encode-expression:
28854 @encode objc-typename
28856 Returns an encoded representation of the type argument. */
28858 static cp_expr
28859 cp_parser_objc_encode_expression (cp_parser* parser)
28861 tree type;
28862 cp_token *token;
28863 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28865 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28867 token = cp_lexer_peek_token (parser->lexer);
28868 type = complete_type (cp_parser_type_id (parser));
28869 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28871 if (!type)
28873 error_at (token->location,
28874 "%<@encode%> must specify a type as an argument");
28875 return error_mark_node;
28878 /* This happens if we find @encode(T) (where T is a template
28879 typename or something dependent on a template typename) when
28880 parsing a template. In that case, we can't compile it
28881 immediately, but we rather create an AT_ENCODE_EXPR which will
28882 need to be instantiated when the template is used.
28884 if (dependent_type_p (type))
28886 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28887 TREE_READONLY (value) = 1;
28888 return value;
28892 /* Build a location of the form:
28893 @encode(int)
28894 ^~~~~~~~~~~~
28895 with caret==start at the @ token, finishing at the close paren. */
28896 location_t combined_loc
28897 = make_location (start_loc, start_loc,
28898 cp_lexer_previous_token (parser->lexer)->location);
28900 return cp_expr (objc_build_encode_expr (type), combined_loc);
28903 /* Parse an Objective-C @defs expression. */
28905 static tree
28906 cp_parser_objc_defs_expression (cp_parser *parser)
28908 tree name;
28910 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28911 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28912 name = cp_parser_identifier (parser);
28913 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28915 return objc_get_class_ivars (name);
28918 /* Parse an Objective-C protocol expression.
28920 objc-protocol-expression:
28921 @protocol ( identifier )
28923 Returns a representation of the protocol expression. */
28925 static tree
28926 cp_parser_objc_protocol_expression (cp_parser* parser)
28928 tree proto;
28929 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28931 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28932 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28933 proto = cp_parser_identifier (parser);
28934 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28936 /* Build a location of the form:
28937 @protocol(prot)
28938 ^~~~~~~~~~~~~~~
28939 with caret==start at the @ token, finishing at the close paren. */
28940 location_t combined_loc
28941 = make_location (start_loc, start_loc,
28942 cp_lexer_previous_token (parser->lexer)->location);
28943 tree result = objc_build_protocol_expr (proto);
28944 protected_set_expr_location (result, combined_loc);
28945 return result;
28948 /* Parse an Objective-C selector expression.
28950 objc-selector-expression:
28951 @selector ( objc-method-signature )
28953 objc-method-signature:
28954 objc-selector
28955 objc-selector-seq
28957 objc-selector-seq:
28958 objc-selector :
28959 objc-selector-seq objc-selector :
28961 Returns a representation of the method selector. */
28963 static tree
28964 cp_parser_objc_selector_expression (cp_parser* parser)
28966 tree sel_seq = NULL_TREE;
28967 bool maybe_unary_selector_p = true;
28968 cp_token *token;
28969 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28971 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28972 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28973 token = cp_lexer_peek_token (parser->lexer);
28975 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28976 || token->type == CPP_SCOPE)
28978 tree selector = NULL_TREE;
28980 if (token->type != CPP_COLON
28981 || token->type == CPP_SCOPE)
28982 selector = cp_parser_objc_selector (parser);
28984 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28985 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28987 /* Detect if we have a unary selector. */
28988 if (maybe_unary_selector_p)
28990 sel_seq = selector;
28991 goto finish_selector;
28993 else
28995 cp_parser_error (parser, "expected %<:%>");
28998 maybe_unary_selector_p = false;
28999 token = cp_lexer_consume_token (parser->lexer);
29001 if (token->type == CPP_SCOPE)
29003 sel_seq
29004 = chainon (sel_seq,
29005 build_tree_list (selector, NULL_TREE));
29006 sel_seq
29007 = chainon (sel_seq,
29008 build_tree_list (NULL_TREE, NULL_TREE));
29010 else
29011 sel_seq
29012 = chainon (sel_seq,
29013 build_tree_list (selector, NULL_TREE));
29015 token = cp_lexer_peek_token (parser->lexer);
29018 finish_selector:
29019 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29022 /* Build a location of the form:
29023 @selector(func)
29024 ^~~~~~~~~~~~~~~
29025 with caret==start at the @ token, finishing at the close paren. */
29026 location_t combined_loc
29027 = make_location (loc, loc,
29028 cp_lexer_previous_token (parser->lexer)->location);
29029 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29030 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29031 protected_set_expr_location (result, combined_loc);
29032 return result;
29035 /* Parse a list of identifiers.
29037 objc-identifier-list:
29038 identifier
29039 objc-identifier-list , identifier
29041 Returns a TREE_LIST of identifier nodes. */
29043 static tree
29044 cp_parser_objc_identifier_list (cp_parser* parser)
29046 tree identifier;
29047 tree list;
29048 cp_token *sep;
29050 identifier = cp_parser_identifier (parser);
29051 if (identifier == error_mark_node)
29052 return error_mark_node;
29054 list = build_tree_list (NULL_TREE, identifier);
29055 sep = cp_lexer_peek_token (parser->lexer);
29057 while (sep->type == CPP_COMMA)
29059 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29060 identifier = cp_parser_identifier (parser);
29061 if (identifier == error_mark_node)
29062 return list;
29064 list = chainon (list, build_tree_list (NULL_TREE,
29065 identifier));
29066 sep = cp_lexer_peek_token (parser->lexer);
29069 return list;
29072 /* Parse an Objective-C alias declaration.
29074 objc-alias-declaration:
29075 @compatibility_alias identifier identifier ;
29077 This function registers the alias mapping with the Objective-C front end.
29078 It returns nothing. */
29080 static void
29081 cp_parser_objc_alias_declaration (cp_parser* parser)
29083 tree alias, orig;
29085 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29086 alias = cp_parser_identifier (parser);
29087 orig = cp_parser_identifier (parser);
29088 objc_declare_alias (alias, orig);
29089 cp_parser_consume_semicolon_at_end_of_statement (parser);
29092 /* Parse an Objective-C class forward-declaration.
29094 objc-class-declaration:
29095 @class objc-identifier-list ;
29097 The function registers the forward declarations with the Objective-C
29098 front end. It returns nothing. */
29100 static void
29101 cp_parser_objc_class_declaration (cp_parser* parser)
29103 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29104 while (true)
29106 tree id;
29108 id = cp_parser_identifier (parser);
29109 if (id == error_mark_node)
29110 break;
29112 objc_declare_class (id);
29114 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29115 cp_lexer_consume_token (parser->lexer);
29116 else
29117 break;
29119 cp_parser_consume_semicolon_at_end_of_statement (parser);
29122 /* Parse a list of Objective-C protocol references.
29124 objc-protocol-refs-opt:
29125 objc-protocol-refs [opt]
29127 objc-protocol-refs:
29128 < objc-identifier-list >
29130 Returns a TREE_LIST of identifiers, if any. */
29132 static tree
29133 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29135 tree protorefs = NULL_TREE;
29137 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29139 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29140 protorefs = cp_parser_objc_identifier_list (parser);
29141 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29144 return protorefs;
29147 /* Parse a Objective-C visibility specification. */
29149 static void
29150 cp_parser_objc_visibility_spec (cp_parser* parser)
29152 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29154 switch (vis->keyword)
29156 case RID_AT_PRIVATE:
29157 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29158 break;
29159 case RID_AT_PROTECTED:
29160 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29161 break;
29162 case RID_AT_PUBLIC:
29163 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29164 break;
29165 case RID_AT_PACKAGE:
29166 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29167 break;
29168 default:
29169 return;
29172 /* Eat '@private'/'@protected'/'@public'. */
29173 cp_lexer_consume_token (parser->lexer);
29176 /* Parse an Objective-C method type. Return 'true' if it is a class
29177 (+) method, and 'false' if it is an instance (-) method. */
29179 static inline bool
29180 cp_parser_objc_method_type (cp_parser* parser)
29182 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29183 return true;
29184 else
29185 return false;
29188 /* Parse an Objective-C protocol qualifier. */
29190 static tree
29191 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29193 tree quals = NULL_TREE, node;
29194 cp_token *token = cp_lexer_peek_token (parser->lexer);
29196 node = token->u.value;
29198 while (node && identifier_p (node)
29199 && (node == ridpointers [(int) RID_IN]
29200 || node == ridpointers [(int) RID_OUT]
29201 || node == ridpointers [(int) RID_INOUT]
29202 || node == ridpointers [(int) RID_BYCOPY]
29203 || node == ridpointers [(int) RID_BYREF]
29204 || node == ridpointers [(int) RID_ONEWAY]))
29206 quals = tree_cons (NULL_TREE, node, quals);
29207 cp_lexer_consume_token (parser->lexer);
29208 token = cp_lexer_peek_token (parser->lexer);
29209 node = token->u.value;
29212 return quals;
29215 /* Parse an Objective-C typename. */
29217 static tree
29218 cp_parser_objc_typename (cp_parser* parser)
29220 tree type_name = NULL_TREE;
29222 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29224 tree proto_quals, cp_type = NULL_TREE;
29226 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29227 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29229 /* An ObjC type name may consist of just protocol qualifiers, in which
29230 case the type shall default to 'id'. */
29231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29233 cp_type = cp_parser_type_id (parser);
29235 /* If the type could not be parsed, an error has already
29236 been produced. For error recovery, behave as if it had
29237 not been specified, which will use the default type
29238 'id'. */
29239 if (cp_type == error_mark_node)
29241 cp_type = NULL_TREE;
29242 /* We need to skip to the closing parenthesis as
29243 cp_parser_type_id() does not seem to do it for
29244 us. */
29245 cp_parser_skip_to_closing_parenthesis (parser,
29246 /*recovering=*/true,
29247 /*or_comma=*/false,
29248 /*consume_paren=*/false);
29252 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29253 type_name = build_tree_list (proto_quals, cp_type);
29256 return type_name;
29259 /* Check to see if TYPE refers to an Objective-C selector name. */
29261 static bool
29262 cp_parser_objc_selector_p (enum cpp_ttype type)
29264 return (type == CPP_NAME || type == CPP_KEYWORD
29265 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29266 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29267 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29268 || type == CPP_XOR || type == CPP_XOR_EQ);
29271 /* Parse an Objective-C selector. */
29273 static tree
29274 cp_parser_objc_selector (cp_parser* parser)
29276 cp_token *token = cp_lexer_consume_token (parser->lexer);
29278 if (!cp_parser_objc_selector_p (token->type))
29280 error_at (token->location, "invalid Objective-C++ selector name");
29281 return error_mark_node;
29284 /* C++ operator names are allowed to appear in ObjC selectors. */
29285 switch (token->type)
29287 case CPP_AND_AND: return get_identifier ("and");
29288 case CPP_AND_EQ: return get_identifier ("and_eq");
29289 case CPP_AND: return get_identifier ("bitand");
29290 case CPP_OR: return get_identifier ("bitor");
29291 case CPP_COMPL: return get_identifier ("compl");
29292 case CPP_NOT: return get_identifier ("not");
29293 case CPP_NOT_EQ: return get_identifier ("not_eq");
29294 case CPP_OR_OR: return get_identifier ("or");
29295 case CPP_OR_EQ: return get_identifier ("or_eq");
29296 case CPP_XOR: return get_identifier ("xor");
29297 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29298 default: return token->u.value;
29302 /* Parse an Objective-C params list. */
29304 static tree
29305 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29307 tree params = NULL_TREE;
29308 bool maybe_unary_selector_p = true;
29309 cp_token *token = cp_lexer_peek_token (parser->lexer);
29311 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29313 tree selector = NULL_TREE, type_name, identifier;
29314 tree parm_attr = NULL_TREE;
29316 if (token->keyword == RID_ATTRIBUTE)
29317 break;
29319 if (token->type != CPP_COLON)
29320 selector = cp_parser_objc_selector (parser);
29322 /* Detect if we have a unary selector. */
29323 if (maybe_unary_selector_p
29324 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29326 params = selector; /* Might be followed by attributes. */
29327 break;
29330 maybe_unary_selector_p = false;
29331 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29333 /* Something went quite wrong. There should be a colon
29334 here, but there is not. Stop parsing parameters. */
29335 break;
29337 type_name = cp_parser_objc_typename (parser);
29338 /* New ObjC allows attributes on parameters too. */
29339 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29340 parm_attr = cp_parser_attributes_opt (parser);
29341 identifier = cp_parser_identifier (parser);
29343 params
29344 = chainon (params,
29345 objc_build_keyword_decl (selector,
29346 type_name,
29347 identifier,
29348 parm_attr));
29350 token = cp_lexer_peek_token (parser->lexer);
29353 if (params == NULL_TREE)
29355 cp_parser_error (parser, "objective-c++ method declaration is expected");
29356 return error_mark_node;
29359 /* We allow tail attributes for the method. */
29360 if (token->keyword == RID_ATTRIBUTE)
29362 *attributes = cp_parser_attributes_opt (parser);
29363 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29364 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29365 return params;
29366 cp_parser_error (parser,
29367 "method attributes must be specified at the end");
29368 return error_mark_node;
29371 if (params == NULL_TREE)
29373 cp_parser_error (parser, "objective-c++ method declaration is expected");
29374 return error_mark_node;
29376 return params;
29379 /* Parse the non-keyword Objective-C params. */
29381 static tree
29382 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29383 tree* attributes)
29385 tree params = make_node (TREE_LIST);
29386 cp_token *token = cp_lexer_peek_token (parser->lexer);
29387 *ellipsisp = false; /* Initially, assume no ellipsis. */
29389 while (token->type == CPP_COMMA)
29391 cp_parameter_declarator *parmdecl;
29392 tree parm;
29394 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29395 token = cp_lexer_peek_token (parser->lexer);
29397 if (token->type == CPP_ELLIPSIS)
29399 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29400 *ellipsisp = true;
29401 token = cp_lexer_peek_token (parser->lexer);
29402 break;
29405 /* TODO: parse attributes for tail parameters. */
29406 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29407 parm = grokdeclarator (parmdecl->declarator,
29408 &parmdecl->decl_specifiers,
29409 PARM, /*initialized=*/0,
29410 /*attrlist=*/NULL);
29412 chainon (params, build_tree_list (NULL_TREE, parm));
29413 token = cp_lexer_peek_token (parser->lexer);
29416 /* We allow tail attributes for the method. */
29417 if (token->keyword == RID_ATTRIBUTE)
29419 if (*attributes == NULL_TREE)
29421 *attributes = cp_parser_attributes_opt (parser);
29422 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29423 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29424 return params;
29426 else
29427 /* We have an error, but parse the attributes, so that we can
29428 carry on. */
29429 *attributes = cp_parser_attributes_opt (parser);
29431 cp_parser_error (parser,
29432 "method attributes must be specified at the end");
29433 return error_mark_node;
29436 return params;
29439 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29441 static void
29442 cp_parser_objc_interstitial_code (cp_parser* parser)
29444 cp_token *token = cp_lexer_peek_token (parser->lexer);
29446 /* If the next token is `extern' and the following token is a string
29447 literal, then we have a linkage specification. */
29448 if (token->keyword == RID_EXTERN
29449 && cp_parser_is_pure_string_literal
29450 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29451 cp_parser_linkage_specification (parser);
29452 /* Handle #pragma, if any. */
29453 else if (token->type == CPP_PRAGMA)
29454 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29455 /* Allow stray semicolons. */
29456 else if (token->type == CPP_SEMICOLON)
29457 cp_lexer_consume_token (parser->lexer);
29458 /* Mark methods as optional or required, when building protocols. */
29459 else if (token->keyword == RID_AT_OPTIONAL)
29461 cp_lexer_consume_token (parser->lexer);
29462 objc_set_method_opt (true);
29464 else if (token->keyword == RID_AT_REQUIRED)
29466 cp_lexer_consume_token (parser->lexer);
29467 objc_set_method_opt (false);
29469 else if (token->keyword == RID_NAMESPACE)
29470 cp_parser_namespace_definition (parser);
29471 /* Other stray characters must generate errors. */
29472 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29474 cp_lexer_consume_token (parser->lexer);
29475 error ("stray %qs between Objective-C++ methods",
29476 token->type == CPP_OPEN_BRACE ? "{" : "}");
29478 /* Finally, try to parse a block-declaration, or a function-definition. */
29479 else
29480 cp_parser_block_declaration (parser, /*statement_p=*/false);
29483 /* Parse a method signature. */
29485 static tree
29486 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29488 tree rettype, kwdparms, optparms;
29489 bool ellipsis = false;
29490 bool is_class_method;
29492 is_class_method = cp_parser_objc_method_type (parser);
29493 rettype = cp_parser_objc_typename (parser);
29494 *attributes = NULL_TREE;
29495 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29496 if (kwdparms == error_mark_node)
29497 return error_mark_node;
29498 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29499 if (optparms == error_mark_node)
29500 return error_mark_node;
29502 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29505 static bool
29506 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29508 tree tattr;
29509 cp_lexer_save_tokens (parser->lexer);
29510 tattr = cp_parser_attributes_opt (parser);
29511 gcc_assert (tattr) ;
29513 /* If the attributes are followed by a method introducer, this is not allowed.
29514 Dump the attributes and flag the situation. */
29515 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29516 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29517 return true;
29519 /* Otherwise, the attributes introduce some interstitial code, possibly so
29520 rewind to allow that check. */
29521 cp_lexer_rollback_tokens (parser->lexer);
29522 return false;
29525 /* Parse an Objective-C method prototype list. */
29527 static void
29528 cp_parser_objc_method_prototype_list (cp_parser* parser)
29530 cp_token *token = cp_lexer_peek_token (parser->lexer);
29532 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29534 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29536 tree attributes, sig;
29537 bool is_class_method;
29538 if (token->type == CPP_PLUS)
29539 is_class_method = true;
29540 else
29541 is_class_method = false;
29542 sig = cp_parser_objc_method_signature (parser, &attributes);
29543 if (sig == error_mark_node)
29545 cp_parser_skip_to_end_of_block_or_statement (parser);
29546 token = cp_lexer_peek_token (parser->lexer);
29547 continue;
29549 objc_add_method_declaration (is_class_method, sig, attributes);
29550 cp_parser_consume_semicolon_at_end_of_statement (parser);
29552 else if (token->keyword == RID_AT_PROPERTY)
29553 cp_parser_objc_at_property_declaration (parser);
29554 else if (token->keyword == RID_ATTRIBUTE
29555 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29556 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29557 OPT_Wattributes,
29558 "prefix attributes are ignored for methods");
29559 else
29560 /* Allow for interspersed non-ObjC++ code. */
29561 cp_parser_objc_interstitial_code (parser);
29563 token = cp_lexer_peek_token (parser->lexer);
29566 if (token->type != CPP_EOF)
29567 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29568 else
29569 cp_parser_error (parser, "expected %<@end%>");
29571 objc_finish_interface ();
29574 /* Parse an Objective-C method definition list. */
29576 static void
29577 cp_parser_objc_method_definition_list (cp_parser* parser)
29579 cp_token *token = cp_lexer_peek_token (parser->lexer);
29581 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29583 tree meth;
29585 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29587 cp_token *ptk;
29588 tree sig, attribute;
29589 bool is_class_method;
29590 if (token->type == CPP_PLUS)
29591 is_class_method = true;
29592 else
29593 is_class_method = false;
29594 push_deferring_access_checks (dk_deferred);
29595 sig = cp_parser_objc_method_signature (parser, &attribute);
29596 if (sig == error_mark_node)
29598 cp_parser_skip_to_end_of_block_or_statement (parser);
29599 token = cp_lexer_peek_token (parser->lexer);
29600 continue;
29602 objc_start_method_definition (is_class_method, sig, attribute,
29603 NULL_TREE);
29605 /* For historical reasons, we accept an optional semicolon. */
29606 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29607 cp_lexer_consume_token (parser->lexer);
29609 ptk = cp_lexer_peek_token (parser->lexer);
29610 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29611 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29613 perform_deferred_access_checks (tf_warning_or_error);
29614 stop_deferring_access_checks ();
29615 meth = cp_parser_function_definition_after_declarator (parser,
29616 false);
29617 pop_deferring_access_checks ();
29618 objc_finish_method_definition (meth);
29621 /* The following case will be removed once @synthesize is
29622 completely implemented. */
29623 else if (token->keyword == RID_AT_PROPERTY)
29624 cp_parser_objc_at_property_declaration (parser);
29625 else if (token->keyword == RID_AT_SYNTHESIZE)
29626 cp_parser_objc_at_synthesize_declaration (parser);
29627 else if (token->keyword == RID_AT_DYNAMIC)
29628 cp_parser_objc_at_dynamic_declaration (parser);
29629 else if (token->keyword == RID_ATTRIBUTE
29630 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29631 warning_at (token->location, OPT_Wattributes,
29632 "prefix attributes are ignored for methods");
29633 else
29634 /* Allow for interspersed non-ObjC++ code. */
29635 cp_parser_objc_interstitial_code (parser);
29637 token = cp_lexer_peek_token (parser->lexer);
29640 if (token->type != CPP_EOF)
29641 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29642 else
29643 cp_parser_error (parser, "expected %<@end%>");
29645 objc_finish_implementation ();
29648 /* Parse Objective-C ivars. */
29650 static void
29651 cp_parser_objc_class_ivars (cp_parser* parser)
29653 cp_token *token = cp_lexer_peek_token (parser->lexer);
29655 if (token->type != CPP_OPEN_BRACE)
29656 return; /* No ivars specified. */
29658 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29659 token = cp_lexer_peek_token (parser->lexer);
29661 while (token->type != CPP_CLOSE_BRACE
29662 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29664 cp_decl_specifier_seq declspecs;
29665 int decl_class_or_enum_p;
29666 tree prefix_attributes;
29668 cp_parser_objc_visibility_spec (parser);
29670 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29671 break;
29673 cp_parser_decl_specifier_seq (parser,
29674 CP_PARSER_FLAGS_OPTIONAL,
29675 &declspecs,
29676 &decl_class_or_enum_p);
29678 /* auto, register, static, extern, mutable. */
29679 if (declspecs.storage_class != sc_none)
29681 cp_parser_error (parser, "invalid type for instance variable");
29682 declspecs.storage_class = sc_none;
29685 /* thread_local. */
29686 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29688 cp_parser_error (parser, "invalid type for instance variable");
29689 declspecs.locations[ds_thread] = 0;
29692 /* typedef. */
29693 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29695 cp_parser_error (parser, "invalid type for instance variable");
29696 declspecs.locations[ds_typedef] = 0;
29699 prefix_attributes = declspecs.attributes;
29700 declspecs.attributes = NULL_TREE;
29702 /* Keep going until we hit the `;' at the end of the
29703 declaration. */
29704 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29706 tree width = NULL_TREE, attributes, first_attribute, decl;
29707 cp_declarator *declarator = NULL;
29708 int ctor_dtor_or_conv_p;
29710 /* Check for a (possibly unnamed) bitfield declaration. */
29711 token = cp_lexer_peek_token (parser->lexer);
29712 if (token->type == CPP_COLON)
29713 goto eat_colon;
29715 if (token->type == CPP_NAME
29716 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29717 == CPP_COLON))
29719 /* Get the name of the bitfield. */
29720 declarator = make_id_declarator (NULL_TREE,
29721 cp_parser_identifier (parser),
29722 sfk_none);
29724 eat_colon:
29725 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29726 /* Get the width of the bitfield. */
29727 width
29728 = cp_parser_constant_expression (parser);
29730 else
29732 /* Parse the declarator. */
29733 declarator
29734 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29735 &ctor_dtor_or_conv_p,
29736 /*parenthesized_p=*/NULL,
29737 /*member_p=*/false,
29738 /*friend_p=*/false);
29741 /* Look for attributes that apply to the ivar. */
29742 attributes = cp_parser_attributes_opt (parser);
29743 /* Remember which attributes are prefix attributes and
29744 which are not. */
29745 first_attribute = attributes;
29746 /* Combine the attributes. */
29747 attributes = chainon (prefix_attributes, attributes);
29749 if (width)
29750 /* Create the bitfield declaration. */
29751 decl = grokbitfield (declarator, &declspecs,
29752 width,
29753 attributes);
29754 else
29755 decl = grokfield (declarator, &declspecs,
29756 NULL_TREE, /*init_const_expr_p=*/false,
29757 NULL_TREE, attributes);
29759 /* Add the instance variable. */
29760 if (decl != error_mark_node && decl != NULL_TREE)
29761 objc_add_instance_variable (decl);
29763 /* Reset PREFIX_ATTRIBUTES. */
29764 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29765 attributes = TREE_CHAIN (attributes);
29766 if (attributes)
29767 TREE_CHAIN (attributes) = NULL_TREE;
29769 token = cp_lexer_peek_token (parser->lexer);
29771 if (token->type == CPP_COMMA)
29773 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29774 continue;
29776 break;
29779 cp_parser_consume_semicolon_at_end_of_statement (parser);
29780 token = cp_lexer_peek_token (parser->lexer);
29783 if (token->keyword == RID_AT_END)
29784 cp_parser_error (parser, "expected %<}%>");
29786 /* Do not consume the RID_AT_END, so it will be read again as terminating
29787 the @interface of @implementation. */
29788 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29789 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29791 /* For historical reasons, we accept an optional semicolon. */
29792 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29793 cp_lexer_consume_token (parser->lexer);
29796 /* Parse an Objective-C protocol declaration. */
29798 static void
29799 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29801 tree proto, protorefs;
29802 cp_token *tok;
29804 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29805 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29807 tok = cp_lexer_peek_token (parser->lexer);
29808 error_at (tok->location, "identifier expected after %<@protocol%>");
29809 cp_parser_consume_semicolon_at_end_of_statement (parser);
29810 return;
29813 /* See if we have a forward declaration or a definition. */
29814 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29816 /* Try a forward declaration first. */
29817 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29819 while (true)
29821 tree id;
29823 id = cp_parser_identifier (parser);
29824 if (id == error_mark_node)
29825 break;
29827 objc_declare_protocol (id, attributes);
29829 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29830 cp_lexer_consume_token (parser->lexer);
29831 else
29832 break;
29834 cp_parser_consume_semicolon_at_end_of_statement (parser);
29837 /* Ok, we got a full-fledged definition (or at least should). */
29838 else
29840 proto = cp_parser_identifier (parser);
29841 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29842 objc_start_protocol (proto, protorefs, attributes);
29843 cp_parser_objc_method_prototype_list (parser);
29847 /* Parse an Objective-C superclass or category. */
29849 static void
29850 cp_parser_objc_superclass_or_category (cp_parser *parser,
29851 bool iface_p,
29852 tree *super,
29853 tree *categ, bool *is_class_extension)
29855 cp_token *next = cp_lexer_peek_token (parser->lexer);
29857 *super = *categ = NULL_TREE;
29858 *is_class_extension = false;
29859 if (next->type == CPP_COLON)
29861 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29862 *super = cp_parser_identifier (parser);
29864 else if (next->type == CPP_OPEN_PAREN)
29866 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29868 /* If there is no category name, and this is an @interface, we
29869 have a class extension. */
29870 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29872 *categ = NULL_TREE;
29873 *is_class_extension = true;
29875 else
29876 *categ = cp_parser_identifier (parser);
29878 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29882 /* Parse an Objective-C class interface. */
29884 static void
29885 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29887 tree name, super, categ, protos;
29888 bool is_class_extension;
29890 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29891 name = cp_parser_identifier (parser);
29892 if (name == error_mark_node)
29894 /* It's hard to recover because even if valid @interface stuff
29895 is to follow, we can't compile it (or validate it) if we
29896 don't even know which class it refers to. Let's assume this
29897 was a stray '@interface' token in the stream and skip it.
29899 return;
29901 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29902 &is_class_extension);
29903 protos = cp_parser_objc_protocol_refs_opt (parser);
29905 /* We have either a class or a category on our hands. */
29906 if (categ || is_class_extension)
29907 objc_start_category_interface (name, categ, protos, attributes);
29908 else
29910 objc_start_class_interface (name, super, protos, attributes);
29911 /* Handle instance variable declarations, if any. */
29912 cp_parser_objc_class_ivars (parser);
29913 objc_continue_interface ();
29916 cp_parser_objc_method_prototype_list (parser);
29919 /* Parse an Objective-C class implementation. */
29921 static void
29922 cp_parser_objc_class_implementation (cp_parser* parser)
29924 tree name, super, categ;
29925 bool is_class_extension;
29927 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29928 name = cp_parser_identifier (parser);
29929 if (name == error_mark_node)
29931 /* It's hard to recover because even if valid @implementation
29932 stuff is to follow, we can't compile it (or validate it) if
29933 we don't even know which class it refers to. Let's assume
29934 this was a stray '@implementation' token in the stream and
29935 skip it.
29937 return;
29939 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29940 &is_class_extension);
29942 /* We have either a class or a category on our hands. */
29943 if (categ)
29944 objc_start_category_implementation (name, categ);
29945 else
29947 objc_start_class_implementation (name, super);
29948 /* Handle instance variable declarations, if any. */
29949 cp_parser_objc_class_ivars (parser);
29950 objc_continue_implementation ();
29953 cp_parser_objc_method_definition_list (parser);
29956 /* Consume the @end token and finish off the implementation. */
29958 static void
29959 cp_parser_objc_end_implementation (cp_parser* parser)
29961 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29962 objc_finish_implementation ();
29965 /* Parse an Objective-C declaration. */
29967 static void
29968 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29970 /* Try to figure out what kind of declaration is present. */
29971 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29973 if (attributes)
29974 switch (kwd->keyword)
29976 case RID_AT_ALIAS:
29977 case RID_AT_CLASS:
29978 case RID_AT_END:
29979 error_at (kwd->location, "attributes may not be specified before"
29980 " the %<@%D%> Objective-C++ keyword",
29981 kwd->u.value);
29982 attributes = NULL;
29983 break;
29984 case RID_AT_IMPLEMENTATION:
29985 warning_at (kwd->location, OPT_Wattributes,
29986 "prefix attributes are ignored before %<@%D%>",
29987 kwd->u.value);
29988 attributes = NULL;
29989 default:
29990 break;
29993 switch (kwd->keyword)
29995 case RID_AT_ALIAS:
29996 cp_parser_objc_alias_declaration (parser);
29997 break;
29998 case RID_AT_CLASS:
29999 cp_parser_objc_class_declaration (parser);
30000 break;
30001 case RID_AT_PROTOCOL:
30002 cp_parser_objc_protocol_declaration (parser, attributes);
30003 break;
30004 case RID_AT_INTERFACE:
30005 cp_parser_objc_class_interface (parser, attributes);
30006 break;
30007 case RID_AT_IMPLEMENTATION:
30008 cp_parser_objc_class_implementation (parser);
30009 break;
30010 case RID_AT_END:
30011 cp_parser_objc_end_implementation (parser);
30012 break;
30013 default:
30014 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30015 kwd->u.value);
30016 cp_parser_skip_to_end_of_block_or_statement (parser);
30020 /* Parse an Objective-C try-catch-finally statement.
30022 objc-try-catch-finally-stmt:
30023 @try compound-statement objc-catch-clause-seq [opt]
30024 objc-finally-clause [opt]
30026 objc-catch-clause-seq:
30027 objc-catch-clause objc-catch-clause-seq [opt]
30029 objc-catch-clause:
30030 @catch ( objc-exception-declaration ) compound-statement
30032 objc-finally-clause:
30033 @finally compound-statement
30035 objc-exception-declaration:
30036 parameter-declaration
30037 '...'
30039 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30041 Returns NULL_TREE.
30043 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30044 for C. Keep them in sync. */
30046 static tree
30047 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30049 location_t location;
30050 tree stmt;
30052 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30053 location = cp_lexer_peek_token (parser->lexer)->location;
30054 objc_maybe_warn_exceptions (location);
30055 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30056 node, lest it get absorbed into the surrounding block. */
30057 stmt = push_stmt_list ();
30058 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30059 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30061 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30063 cp_parameter_declarator *parm;
30064 tree parameter_declaration = error_mark_node;
30065 bool seen_open_paren = false;
30067 cp_lexer_consume_token (parser->lexer);
30068 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30069 seen_open_paren = true;
30070 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30072 /* We have "@catch (...)" (where the '...' are literally
30073 what is in the code). Skip the '...'.
30074 parameter_declaration is set to NULL_TREE, and
30075 objc_being_catch_clauses() knows that that means
30076 '...'. */
30077 cp_lexer_consume_token (parser->lexer);
30078 parameter_declaration = NULL_TREE;
30080 else
30082 /* We have "@catch (NSException *exception)" or something
30083 like that. Parse the parameter declaration. */
30084 parm = cp_parser_parameter_declaration (parser, false, NULL);
30085 if (parm == NULL)
30086 parameter_declaration = error_mark_node;
30087 else
30088 parameter_declaration = grokdeclarator (parm->declarator,
30089 &parm->decl_specifiers,
30090 PARM, /*initialized=*/0,
30091 /*attrlist=*/NULL);
30093 if (seen_open_paren)
30094 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30095 else
30097 /* If there was no open parenthesis, we are recovering from
30098 an error, and we are trying to figure out what mistake
30099 the user has made. */
30101 /* If there is an immediate closing parenthesis, the user
30102 probably forgot the opening one (ie, they typed "@catch
30103 NSException *e)". Parse the closing parenthesis and keep
30104 going. */
30105 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30106 cp_lexer_consume_token (parser->lexer);
30108 /* If these is no immediate closing parenthesis, the user
30109 probably doesn't know that parenthesis are required at
30110 all (ie, they typed "@catch NSException *e"). So, just
30111 forget about the closing parenthesis and keep going. */
30113 objc_begin_catch_clause (parameter_declaration);
30114 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30115 objc_finish_catch_clause ();
30117 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30119 cp_lexer_consume_token (parser->lexer);
30120 location = cp_lexer_peek_token (parser->lexer)->location;
30121 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30122 node, lest it get absorbed into the surrounding block. */
30123 stmt = push_stmt_list ();
30124 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30125 objc_build_finally_clause (location, pop_stmt_list (stmt));
30128 return objc_finish_try_stmt ();
30131 /* Parse an Objective-C synchronized statement.
30133 objc-synchronized-stmt:
30134 @synchronized ( expression ) compound-statement
30136 Returns NULL_TREE. */
30138 static tree
30139 cp_parser_objc_synchronized_statement (cp_parser *parser)
30141 location_t location;
30142 tree lock, stmt;
30144 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30146 location = cp_lexer_peek_token (parser->lexer)->location;
30147 objc_maybe_warn_exceptions (location);
30148 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30149 lock = cp_parser_expression (parser);
30150 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30152 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30153 node, lest it get absorbed into the surrounding block. */
30154 stmt = push_stmt_list ();
30155 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30157 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30160 /* Parse an Objective-C throw statement.
30162 objc-throw-stmt:
30163 @throw assignment-expression [opt] ;
30165 Returns a constructed '@throw' statement. */
30167 static tree
30168 cp_parser_objc_throw_statement (cp_parser *parser)
30170 tree expr = NULL_TREE;
30171 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30173 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30175 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30176 expr = cp_parser_expression (parser);
30178 cp_parser_consume_semicolon_at_end_of_statement (parser);
30180 return objc_build_throw_stmt (loc, expr);
30183 /* Parse an Objective-C statement. */
30185 static tree
30186 cp_parser_objc_statement (cp_parser * parser)
30188 /* Try to figure out what kind of declaration is present. */
30189 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30191 switch (kwd->keyword)
30193 case RID_AT_TRY:
30194 return cp_parser_objc_try_catch_finally_statement (parser);
30195 case RID_AT_SYNCHRONIZED:
30196 return cp_parser_objc_synchronized_statement (parser);
30197 case RID_AT_THROW:
30198 return cp_parser_objc_throw_statement (parser);
30199 default:
30200 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30201 kwd->u.value);
30202 cp_parser_skip_to_end_of_block_or_statement (parser);
30205 return error_mark_node;
30208 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30209 look ahead to see if an objc keyword follows the attributes. This
30210 is to detect the use of prefix attributes on ObjC @interface and
30211 @protocol. */
30213 static bool
30214 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30216 cp_lexer_save_tokens (parser->lexer);
30217 *attrib = cp_parser_attributes_opt (parser);
30218 gcc_assert (*attrib);
30219 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30221 cp_lexer_commit_tokens (parser->lexer);
30222 return true;
30224 cp_lexer_rollback_tokens (parser->lexer);
30225 return false;
30228 /* This routine is a minimal replacement for
30229 c_parser_struct_declaration () used when parsing the list of
30230 types/names or ObjC++ properties. For example, when parsing the
30231 code
30233 @property (readonly) int a, b, c;
30235 this function is responsible for parsing "int a, int b, int c" and
30236 returning the declarations as CHAIN of DECLs.
30238 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30239 similar parsing. */
30240 static tree
30241 cp_parser_objc_struct_declaration (cp_parser *parser)
30243 tree decls = NULL_TREE;
30244 cp_decl_specifier_seq declspecs;
30245 int decl_class_or_enum_p;
30246 tree prefix_attributes;
30248 cp_parser_decl_specifier_seq (parser,
30249 CP_PARSER_FLAGS_NONE,
30250 &declspecs,
30251 &decl_class_or_enum_p);
30253 if (declspecs.type == error_mark_node)
30254 return error_mark_node;
30256 /* auto, register, static, extern, mutable. */
30257 if (declspecs.storage_class != sc_none)
30259 cp_parser_error (parser, "invalid type for property");
30260 declspecs.storage_class = sc_none;
30263 /* thread_local. */
30264 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30266 cp_parser_error (parser, "invalid type for property");
30267 declspecs.locations[ds_thread] = 0;
30270 /* typedef. */
30271 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30273 cp_parser_error (parser, "invalid type for property");
30274 declspecs.locations[ds_typedef] = 0;
30277 prefix_attributes = declspecs.attributes;
30278 declspecs.attributes = NULL_TREE;
30280 /* Keep going until we hit the `;' at the end of the declaration. */
30281 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30283 tree attributes, first_attribute, decl;
30284 cp_declarator *declarator;
30285 cp_token *token;
30287 /* Parse the declarator. */
30288 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30289 NULL, NULL, false, false);
30291 /* Look for attributes that apply to the ivar. */
30292 attributes = cp_parser_attributes_opt (parser);
30293 /* Remember which attributes are prefix attributes and
30294 which are not. */
30295 first_attribute = attributes;
30296 /* Combine the attributes. */
30297 attributes = chainon (prefix_attributes, attributes);
30299 decl = grokfield (declarator, &declspecs,
30300 NULL_TREE, /*init_const_expr_p=*/false,
30301 NULL_TREE, attributes);
30303 if (decl == error_mark_node || decl == NULL_TREE)
30304 return error_mark_node;
30306 /* Reset PREFIX_ATTRIBUTES. */
30307 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30308 attributes = TREE_CHAIN (attributes);
30309 if (attributes)
30310 TREE_CHAIN (attributes) = NULL_TREE;
30312 DECL_CHAIN (decl) = decls;
30313 decls = decl;
30315 token = cp_lexer_peek_token (parser->lexer);
30316 if (token->type == CPP_COMMA)
30318 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30319 continue;
30321 else
30322 break;
30324 return decls;
30327 /* Parse an Objective-C @property declaration. The syntax is:
30329 objc-property-declaration:
30330 '@property' objc-property-attributes[opt] struct-declaration ;
30332 objc-property-attributes:
30333 '(' objc-property-attribute-list ')'
30335 objc-property-attribute-list:
30336 objc-property-attribute
30337 objc-property-attribute-list, objc-property-attribute
30339 objc-property-attribute
30340 'getter' = identifier
30341 'setter' = identifier
30342 'readonly'
30343 'readwrite'
30344 'assign'
30345 'retain'
30346 'copy'
30347 'nonatomic'
30349 For example:
30350 @property NSString *name;
30351 @property (readonly) id object;
30352 @property (retain, nonatomic, getter=getTheName) id name;
30353 @property int a, b, c;
30355 PS: This function is identical to
30356 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30357 static void
30358 cp_parser_objc_at_property_declaration (cp_parser *parser)
30360 /* The following variables hold the attributes of the properties as
30361 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30362 seen. When we see an attribute, we set them to 'true' (if they
30363 are boolean properties) or to the identifier (if they have an
30364 argument, ie, for getter and setter). Note that here we only
30365 parse the list of attributes, check the syntax and accumulate the
30366 attributes that we find. objc_add_property_declaration() will
30367 then process the information. */
30368 bool property_assign = false;
30369 bool property_copy = false;
30370 tree property_getter_ident = NULL_TREE;
30371 bool property_nonatomic = false;
30372 bool property_readonly = false;
30373 bool property_readwrite = false;
30374 bool property_retain = false;
30375 tree property_setter_ident = NULL_TREE;
30377 /* 'properties' is the list of properties that we read. Usually a
30378 single one, but maybe more (eg, in "@property int a, b, c;" there
30379 are three). */
30380 tree properties;
30381 location_t loc;
30383 loc = cp_lexer_peek_token (parser->lexer)->location;
30385 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30387 /* Parse the optional attribute list... */
30388 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30390 /* Eat the '('. */
30391 cp_lexer_consume_token (parser->lexer);
30393 while (true)
30395 bool syntax_error = false;
30396 cp_token *token = cp_lexer_peek_token (parser->lexer);
30397 enum rid keyword;
30399 if (token->type != CPP_NAME)
30401 cp_parser_error (parser, "expected identifier");
30402 break;
30404 keyword = C_RID_CODE (token->u.value);
30405 cp_lexer_consume_token (parser->lexer);
30406 switch (keyword)
30408 case RID_ASSIGN: property_assign = true; break;
30409 case RID_COPY: property_copy = true; break;
30410 case RID_NONATOMIC: property_nonatomic = true; break;
30411 case RID_READONLY: property_readonly = true; break;
30412 case RID_READWRITE: property_readwrite = true; break;
30413 case RID_RETAIN: property_retain = true; break;
30415 case RID_GETTER:
30416 case RID_SETTER:
30417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30419 if (keyword == RID_GETTER)
30420 cp_parser_error (parser,
30421 "missing %<=%> (after %<getter%> attribute)");
30422 else
30423 cp_parser_error (parser,
30424 "missing %<=%> (after %<setter%> attribute)");
30425 syntax_error = true;
30426 break;
30428 cp_lexer_consume_token (parser->lexer); /* eat the = */
30429 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30431 cp_parser_error (parser, "expected identifier");
30432 syntax_error = true;
30433 break;
30435 if (keyword == RID_SETTER)
30437 if (property_setter_ident != NULL_TREE)
30439 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30440 cp_lexer_consume_token (parser->lexer);
30442 else
30443 property_setter_ident = cp_parser_objc_selector (parser);
30444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30445 cp_parser_error (parser, "setter name must terminate with %<:%>");
30446 else
30447 cp_lexer_consume_token (parser->lexer);
30449 else
30451 if (property_getter_ident != NULL_TREE)
30453 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30454 cp_lexer_consume_token (parser->lexer);
30456 else
30457 property_getter_ident = cp_parser_objc_selector (parser);
30459 break;
30460 default:
30461 cp_parser_error (parser, "unknown property attribute");
30462 syntax_error = true;
30463 break;
30466 if (syntax_error)
30467 break;
30469 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30470 cp_lexer_consume_token (parser->lexer);
30471 else
30472 break;
30475 /* FIXME: "@property (setter, assign);" will generate a spurious
30476 "error: expected ‘)’ before ‘,’ token". This is because
30477 cp_parser_require, unlike the C counterpart, will produce an
30478 error even if we are in error recovery. */
30479 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30481 cp_parser_skip_to_closing_parenthesis (parser,
30482 /*recovering=*/true,
30483 /*or_comma=*/false,
30484 /*consume_paren=*/true);
30488 /* ... and the property declaration(s). */
30489 properties = cp_parser_objc_struct_declaration (parser);
30491 if (properties == error_mark_node)
30493 cp_parser_skip_to_end_of_statement (parser);
30494 /* If the next token is now a `;', consume it. */
30495 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30496 cp_lexer_consume_token (parser->lexer);
30497 return;
30500 if (properties == NULL_TREE)
30501 cp_parser_error (parser, "expected identifier");
30502 else
30504 /* Comma-separated properties are chained together in
30505 reverse order; add them one by one. */
30506 properties = nreverse (properties);
30508 for (; properties; properties = TREE_CHAIN (properties))
30509 objc_add_property_declaration (loc, copy_node (properties),
30510 property_readonly, property_readwrite,
30511 property_assign, property_retain,
30512 property_copy, property_nonatomic,
30513 property_getter_ident, property_setter_ident);
30516 cp_parser_consume_semicolon_at_end_of_statement (parser);
30519 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30521 objc-synthesize-declaration:
30522 @synthesize objc-synthesize-identifier-list ;
30524 objc-synthesize-identifier-list:
30525 objc-synthesize-identifier
30526 objc-synthesize-identifier-list, objc-synthesize-identifier
30528 objc-synthesize-identifier
30529 identifier
30530 identifier = identifier
30532 For example:
30533 @synthesize MyProperty;
30534 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30536 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30537 for C. Keep them in sync.
30539 static void
30540 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30542 tree list = NULL_TREE;
30543 location_t loc;
30544 loc = cp_lexer_peek_token (parser->lexer)->location;
30546 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30547 while (true)
30549 tree property, ivar;
30550 property = cp_parser_identifier (parser);
30551 if (property == error_mark_node)
30553 cp_parser_consume_semicolon_at_end_of_statement (parser);
30554 return;
30556 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30558 cp_lexer_consume_token (parser->lexer);
30559 ivar = cp_parser_identifier (parser);
30560 if (ivar == error_mark_node)
30562 cp_parser_consume_semicolon_at_end_of_statement (parser);
30563 return;
30566 else
30567 ivar = NULL_TREE;
30568 list = chainon (list, build_tree_list (ivar, property));
30569 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30570 cp_lexer_consume_token (parser->lexer);
30571 else
30572 break;
30574 cp_parser_consume_semicolon_at_end_of_statement (parser);
30575 objc_add_synthesize_declaration (loc, list);
30578 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30580 objc-dynamic-declaration:
30581 @dynamic identifier-list ;
30583 For example:
30584 @dynamic MyProperty;
30585 @dynamic MyProperty, AnotherProperty;
30587 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30588 for C. Keep them in sync.
30590 static void
30591 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30593 tree list = NULL_TREE;
30594 location_t loc;
30595 loc = cp_lexer_peek_token (parser->lexer)->location;
30597 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30598 while (true)
30600 tree property;
30601 property = cp_parser_identifier (parser);
30602 if (property == error_mark_node)
30604 cp_parser_consume_semicolon_at_end_of_statement (parser);
30605 return;
30607 list = chainon (list, build_tree_list (NULL, property));
30608 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30609 cp_lexer_consume_token (parser->lexer);
30610 else
30611 break;
30613 cp_parser_consume_semicolon_at_end_of_statement (parser);
30614 objc_add_dynamic_declaration (loc, list);
30618 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30620 /* Returns name of the next clause.
30621 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30622 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30623 returned and the token is consumed. */
30625 static pragma_omp_clause
30626 cp_parser_omp_clause_name (cp_parser *parser)
30628 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30630 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30631 result = PRAGMA_OACC_CLAUSE_AUTO;
30632 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30633 result = PRAGMA_OMP_CLAUSE_IF;
30634 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30635 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30636 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30637 result = PRAGMA_OACC_CLAUSE_DELETE;
30638 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30639 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30640 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30641 result = PRAGMA_OMP_CLAUSE_FOR;
30642 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30644 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30645 const char *p = IDENTIFIER_POINTER (id);
30647 switch (p[0])
30649 case 'a':
30650 if (!strcmp ("aligned", p))
30651 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30652 else if (!strcmp ("async", p))
30653 result = PRAGMA_OACC_CLAUSE_ASYNC;
30654 break;
30655 case 'c':
30656 if (!strcmp ("collapse", p))
30657 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30658 else if (!strcmp ("copy", p))
30659 result = PRAGMA_OACC_CLAUSE_COPY;
30660 else if (!strcmp ("copyin", p))
30661 result = PRAGMA_OMP_CLAUSE_COPYIN;
30662 else if (!strcmp ("copyout", p))
30663 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30664 else if (!strcmp ("copyprivate", p))
30665 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30666 else if (!strcmp ("create", p))
30667 result = PRAGMA_OACC_CLAUSE_CREATE;
30668 break;
30669 case 'd':
30670 if (!strcmp ("defaultmap", p))
30671 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30672 else if (!strcmp ("depend", p))
30673 result = PRAGMA_OMP_CLAUSE_DEPEND;
30674 else if (!strcmp ("device", p))
30675 result = PRAGMA_OMP_CLAUSE_DEVICE;
30676 else if (!strcmp ("deviceptr", p))
30677 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30678 else if (!strcmp ("device_resident", p))
30679 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30680 else if (!strcmp ("dist_schedule", p))
30681 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30682 break;
30683 case 'f':
30684 if (!strcmp ("final", p))
30685 result = PRAGMA_OMP_CLAUSE_FINAL;
30686 else if (!strcmp ("firstprivate", p))
30687 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30688 else if (!strcmp ("from", p))
30689 result = PRAGMA_OMP_CLAUSE_FROM;
30690 break;
30691 case 'g':
30692 if (!strcmp ("gang", p))
30693 result = PRAGMA_OACC_CLAUSE_GANG;
30694 else if (!strcmp ("grainsize", p))
30695 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30696 break;
30697 case 'h':
30698 if (!strcmp ("hint", p))
30699 result = PRAGMA_OMP_CLAUSE_HINT;
30700 else if (!strcmp ("host", p))
30701 result = PRAGMA_OACC_CLAUSE_HOST;
30702 break;
30703 case 'i':
30704 if (!strcmp ("inbranch", p))
30705 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30706 else if (!strcmp ("independent", p))
30707 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30708 else if (!strcmp ("is_device_ptr", p))
30709 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30710 break;
30711 case 'l':
30712 if (!strcmp ("lastprivate", p))
30713 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30714 else if (!strcmp ("linear", p))
30715 result = PRAGMA_OMP_CLAUSE_LINEAR;
30716 else if (!strcmp ("link", p))
30717 result = PRAGMA_OMP_CLAUSE_LINK;
30718 break;
30719 case 'm':
30720 if (!strcmp ("map", p))
30721 result = PRAGMA_OMP_CLAUSE_MAP;
30722 else if (!strcmp ("mergeable", p))
30723 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30724 else if (flag_cilkplus && !strcmp ("mask", p))
30725 result = PRAGMA_CILK_CLAUSE_MASK;
30726 break;
30727 case 'n':
30728 if (!strcmp ("nogroup", p))
30729 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30730 else if (!strcmp ("notinbranch", p))
30731 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30732 else if (!strcmp ("nowait", p))
30733 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30734 else if (flag_cilkplus && !strcmp ("nomask", p))
30735 result = PRAGMA_CILK_CLAUSE_NOMASK;
30736 else if (!strcmp ("num_gangs", p))
30737 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30738 else if (!strcmp ("num_tasks", p))
30739 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30740 else if (!strcmp ("num_teams", p))
30741 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30742 else if (!strcmp ("num_threads", p))
30743 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30744 else if (!strcmp ("num_workers", p))
30745 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30746 break;
30747 case 'o':
30748 if (!strcmp ("ordered", p))
30749 result = PRAGMA_OMP_CLAUSE_ORDERED;
30750 break;
30751 case 'p':
30752 if (!strcmp ("parallel", p))
30753 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30754 else if (!strcmp ("present", p))
30755 result = PRAGMA_OACC_CLAUSE_PRESENT;
30756 else if (!strcmp ("present_or_copy", p)
30757 || !strcmp ("pcopy", p))
30758 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30759 else if (!strcmp ("present_or_copyin", p)
30760 || !strcmp ("pcopyin", p))
30761 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30762 else if (!strcmp ("present_or_copyout", p)
30763 || !strcmp ("pcopyout", p))
30764 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30765 else if (!strcmp ("present_or_create", p)
30766 || !strcmp ("pcreate", p))
30767 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30768 else if (!strcmp ("priority", p))
30769 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30770 else if (!strcmp ("proc_bind", p))
30771 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30772 break;
30773 case 'r':
30774 if (!strcmp ("reduction", p))
30775 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30776 break;
30777 case 's':
30778 if (!strcmp ("safelen", p))
30779 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30780 else if (!strcmp ("schedule", p))
30781 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30782 else if (!strcmp ("sections", p))
30783 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30784 else if (!strcmp ("self", p))
30785 result = PRAGMA_OACC_CLAUSE_SELF;
30786 else if (!strcmp ("seq", p))
30787 result = PRAGMA_OACC_CLAUSE_SEQ;
30788 else if (!strcmp ("shared", p))
30789 result = PRAGMA_OMP_CLAUSE_SHARED;
30790 else if (!strcmp ("simd", p))
30791 result = PRAGMA_OMP_CLAUSE_SIMD;
30792 else if (!strcmp ("simdlen", p))
30793 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30794 break;
30795 case 't':
30796 if (!strcmp ("taskgroup", p))
30797 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30798 else if (!strcmp ("thread_limit", p))
30799 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30800 else if (!strcmp ("threads", p))
30801 result = PRAGMA_OMP_CLAUSE_THREADS;
30802 else if (!strcmp ("tile", p))
30803 result = PRAGMA_OACC_CLAUSE_TILE;
30804 else if (!strcmp ("to", p))
30805 result = PRAGMA_OMP_CLAUSE_TO;
30806 break;
30807 case 'u':
30808 if (!strcmp ("uniform", p))
30809 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30810 else if (!strcmp ("untied", p))
30811 result = PRAGMA_OMP_CLAUSE_UNTIED;
30812 else if (!strcmp ("use_device", p))
30813 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30814 else if (!strcmp ("use_device_ptr", p))
30815 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30816 break;
30817 case 'v':
30818 if (!strcmp ("vector", p))
30819 result = PRAGMA_OACC_CLAUSE_VECTOR;
30820 else if (!strcmp ("vector_length", p))
30821 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30822 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30823 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30824 break;
30825 case 'w':
30826 if (!strcmp ("wait", p))
30827 result = PRAGMA_OACC_CLAUSE_WAIT;
30828 else if (!strcmp ("worker", p))
30829 result = PRAGMA_OACC_CLAUSE_WORKER;
30830 break;
30834 if (result != PRAGMA_OMP_CLAUSE_NONE)
30835 cp_lexer_consume_token (parser->lexer);
30837 return result;
30840 /* Validate that a clause of the given type does not already exist. */
30842 static void
30843 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30844 const char *name, location_t location)
30846 tree c;
30848 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30849 if (OMP_CLAUSE_CODE (c) == code)
30851 error_at (location, "too many %qs clauses", name);
30852 break;
30856 /* OpenMP 2.5:
30857 variable-list:
30858 identifier
30859 variable-list , identifier
30861 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30862 colon). An opening parenthesis will have been consumed by the caller.
30864 If KIND is nonzero, create the appropriate node and install the decl
30865 in OMP_CLAUSE_DECL and add the node to the head of the list.
30867 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30868 return the list created.
30870 COLON can be NULL if only closing parenthesis should end the list,
30871 or pointer to bool which will receive false if the list is terminated
30872 by closing parenthesis or true if the list is terminated by colon. */
30874 static tree
30875 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30876 tree list, bool *colon)
30878 cp_token *token;
30879 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30880 if (colon)
30882 parser->colon_corrects_to_scope_p = false;
30883 *colon = false;
30885 while (1)
30887 tree name, decl;
30889 token = cp_lexer_peek_token (parser->lexer);
30890 if (kind != 0
30891 && current_class_ptr
30892 && cp_parser_is_keyword (token, RID_THIS))
30894 decl = finish_this_expr ();
30895 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30896 || CONVERT_EXPR_P (decl))
30897 decl = TREE_OPERAND (decl, 0);
30898 cp_lexer_consume_token (parser->lexer);
30900 else
30902 name = cp_parser_id_expression (parser, /*template_p=*/false,
30903 /*check_dependency_p=*/true,
30904 /*template_p=*/NULL,
30905 /*declarator_p=*/false,
30906 /*optional_p=*/false);
30907 if (name == error_mark_node)
30908 goto skip_comma;
30910 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30911 if (decl == error_mark_node)
30912 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30913 token->location);
30915 if (decl == error_mark_node)
30917 else if (kind != 0)
30919 switch (kind)
30921 case OMP_CLAUSE__CACHE_:
30922 /* The OpenACC cache directive explicitly only allows "array
30923 elements or subarrays". */
30924 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30926 error_at (token->location, "expected %<[%>");
30927 decl = error_mark_node;
30928 break;
30930 /* FALLTHROUGH. */
30931 case OMP_CLAUSE_MAP:
30932 case OMP_CLAUSE_FROM:
30933 case OMP_CLAUSE_TO:
30934 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30936 location_t loc
30937 = cp_lexer_peek_token (parser->lexer)->location;
30938 cp_id_kind idk = CP_ID_KIND_NONE;
30939 cp_lexer_consume_token (parser->lexer);
30940 decl = convert_from_reference (decl);
30941 decl
30942 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30943 decl, false,
30944 &idk, loc);
30946 /* FALLTHROUGH. */
30947 case OMP_CLAUSE_DEPEND:
30948 case OMP_CLAUSE_REDUCTION:
30949 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30951 tree low_bound = NULL_TREE, length = NULL_TREE;
30953 parser->colon_corrects_to_scope_p = false;
30954 cp_lexer_consume_token (parser->lexer);
30955 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30956 low_bound = cp_parser_expression (parser);
30957 if (!colon)
30958 parser->colon_corrects_to_scope_p
30959 = saved_colon_corrects_to_scope_p;
30960 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30961 length = integer_one_node;
30962 else
30964 /* Look for `:'. */
30965 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30966 goto skip_comma;
30967 if (!cp_lexer_next_token_is (parser->lexer,
30968 CPP_CLOSE_SQUARE))
30969 length = cp_parser_expression (parser);
30971 /* Look for the closing `]'. */
30972 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30973 RT_CLOSE_SQUARE))
30974 goto skip_comma;
30976 decl = tree_cons (low_bound, length, decl);
30978 break;
30979 default:
30980 break;
30983 tree u = build_omp_clause (token->location, kind);
30984 OMP_CLAUSE_DECL (u) = decl;
30985 OMP_CLAUSE_CHAIN (u) = list;
30986 list = u;
30988 else
30989 list = tree_cons (decl, NULL_TREE, list);
30991 get_comma:
30992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30993 break;
30994 cp_lexer_consume_token (parser->lexer);
30997 if (colon)
30998 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31000 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31002 *colon = true;
31003 cp_parser_require (parser, CPP_COLON, RT_COLON);
31004 return list;
31007 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31009 int ending;
31011 /* Try to resync to an unnested comma. Copied from
31012 cp_parser_parenthesized_expression_list. */
31013 skip_comma:
31014 if (colon)
31015 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31016 ending = cp_parser_skip_to_closing_parenthesis (parser,
31017 /*recovering=*/true,
31018 /*or_comma=*/true,
31019 /*consume_paren=*/true);
31020 if (ending < 0)
31021 goto get_comma;
31024 return list;
31027 /* Similarly, but expect leading and trailing parenthesis. This is a very
31028 common case for omp clauses. */
31030 static tree
31031 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31033 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31034 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31035 return list;
31038 /* OpenACC 2.0:
31039 copy ( variable-list )
31040 copyin ( variable-list )
31041 copyout ( variable-list )
31042 create ( variable-list )
31043 delete ( variable-list )
31044 present ( variable-list )
31045 present_or_copy ( variable-list )
31046 pcopy ( variable-list )
31047 present_or_copyin ( variable-list )
31048 pcopyin ( variable-list )
31049 present_or_copyout ( variable-list )
31050 pcopyout ( variable-list )
31051 present_or_create ( variable-list )
31052 pcreate ( variable-list ) */
31054 static tree
31055 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31056 tree list)
31058 enum gomp_map_kind kind;
31059 switch (c_kind)
31061 case PRAGMA_OACC_CLAUSE_COPY:
31062 kind = GOMP_MAP_FORCE_TOFROM;
31063 break;
31064 case PRAGMA_OACC_CLAUSE_COPYIN:
31065 kind = GOMP_MAP_FORCE_TO;
31066 break;
31067 case PRAGMA_OACC_CLAUSE_COPYOUT:
31068 kind = GOMP_MAP_FORCE_FROM;
31069 break;
31070 case PRAGMA_OACC_CLAUSE_CREATE:
31071 kind = GOMP_MAP_FORCE_ALLOC;
31072 break;
31073 case PRAGMA_OACC_CLAUSE_DELETE:
31074 kind = GOMP_MAP_DELETE;
31075 break;
31076 case PRAGMA_OACC_CLAUSE_DEVICE:
31077 kind = GOMP_MAP_FORCE_TO;
31078 break;
31079 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31080 kind = GOMP_MAP_DEVICE_RESIDENT;
31081 break;
31082 case PRAGMA_OACC_CLAUSE_HOST:
31083 case PRAGMA_OACC_CLAUSE_SELF:
31084 kind = GOMP_MAP_FORCE_FROM;
31085 break;
31086 case PRAGMA_OACC_CLAUSE_LINK:
31087 kind = GOMP_MAP_LINK;
31088 break;
31089 case PRAGMA_OACC_CLAUSE_PRESENT:
31090 kind = GOMP_MAP_FORCE_PRESENT;
31091 break;
31092 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31093 kind = GOMP_MAP_TOFROM;
31094 break;
31095 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31096 kind = GOMP_MAP_TO;
31097 break;
31098 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31099 kind = GOMP_MAP_FROM;
31100 break;
31101 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31102 kind = GOMP_MAP_ALLOC;
31103 break;
31104 default:
31105 gcc_unreachable ();
31107 tree nl, c;
31108 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31110 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31111 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31113 return nl;
31116 /* OpenACC 2.0:
31117 deviceptr ( variable-list ) */
31119 static tree
31120 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31122 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31123 tree vars, t;
31125 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31126 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31127 variable-list must only allow for pointer variables. */
31128 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31129 for (t = vars; t; t = TREE_CHAIN (t))
31131 tree v = TREE_PURPOSE (t);
31132 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31133 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31134 OMP_CLAUSE_DECL (u) = v;
31135 OMP_CLAUSE_CHAIN (u) = list;
31136 list = u;
31139 return list;
31142 /* OpenACC 2.0:
31143 auto
31144 independent
31145 nohost
31146 seq */
31148 static tree
31149 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31150 enum omp_clause_code code,
31151 tree list, location_t location)
31153 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31154 tree c = build_omp_clause (location, code);
31155 OMP_CLAUSE_CHAIN (c) = list;
31156 return c;
31159 /* OpenACC:
31160 num_gangs ( expression )
31161 num_workers ( expression )
31162 vector_length ( expression ) */
31164 static tree
31165 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31166 const char *str, tree list)
31168 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31170 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31171 return list;
31173 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31175 if (t == error_mark_node
31176 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31178 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31179 /*or_comma=*/false,
31180 /*consume_paren=*/true);
31181 return list;
31184 check_no_duplicate_clause (list, code, str, loc);
31186 tree c = build_omp_clause (loc, code);
31187 OMP_CLAUSE_OPERAND (c, 0) = t;
31188 OMP_CLAUSE_CHAIN (c) = list;
31189 return c;
31192 /* OpenACC:
31194 gang [( gang-arg-list )]
31195 worker [( [num:] int-expr )]
31196 vector [( [length:] int-expr )]
31198 where gang-arg is one of:
31200 [num:] int-expr
31201 static: size-expr
31203 and size-expr may be:
31206 int-expr
31209 static tree
31210 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31211 const char *str, tree list)
31213 const char *id = "num";
31214 cp_lexer *lexer = parser->lexer;
31215 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31216 location_t loc = cp_lexer_peek_token (lexer)->location;
31218 if (kind == OMP_CLAUSE_VECTOR)
31219 id = "length";
31221 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31223 cp_lexer_consume_token (lexer);
31227 cp_token *next = cp_lexer_peek_token (lexer);
31228 int idx = 0;
31230 /* Gang static argument. */
31231 if (kind == OMP_CLAUSE_GANG
31232 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31234 cp_lexer_consume_token (lexer);
31236 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31237 goto cleanup_error;
31239 idx = 1;
31240 if (ops[idx] != NULL)
31242 cp_parser_error (parser, "too many %<static%> arguments");
31243 goto cleanup_error;
31246 /* Check for the '*' argument. */
31247 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31248 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31249 || cp_lexer_nth_token_is (parser->lexer, 2,
31250 CPP_CLOSE_PAREN)))
31252 cp_lexer_consume_token (lexer);
31253 ops[idx] = integer_minus_one_node;
31255 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31257 cp_lexer_consume_token (lexer);
31258 continue;
31260 else break;
31263 /* Worker num: argument and vector length: arguments. */
31264 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31265 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31266 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31268 cp_lexer_consume_token (lexer); /* id */
31269 cp_lexer_consume_token (lexer); /* ':' */
31272 /* Now collect the actual argument. */
31273 if (ops[idx] != NULL_TREE)
31275 cp_parser_error (parser, "unexpected argument");
31276 goto cleanup_error;
31279 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31280 false);
31281 if (expr == error_mark_node)
31282 goto cleanup_error;
31284 mark_exp_read (expr);
31285 ops[idx] = expr;
31287 if (kind == OMP_CLAUSE_GANG
31288 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31290 cp_lexer_consume_token (lexer);
31291 continue;
31293 break;
31295 while (1);
31297 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31298 goto cleanup_error;
31301 check_no_duplicate_clause (list, kind, str, loc);
31303 c = build_omp_clause (loc, kind);
31305 if (ops[1])
31306 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31308 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31309 OMP_CLAUSE_CHAIN (c) = list;
31311 return c;
31313 cleanup_error:
31314 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31315 return list;
31318 /* OpenACC 2.0:
31319 tile ( size-expr-list ) */
31321 static tree
31322 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31324 tree c, expr = error_mark_node;
31325 tree tile = NULL_TREE;
31327 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31328 so, but the spec authors never considered such a case and have
31329 differing opinions on what it might mean, including 'not
31330 allowed'.) */
31331 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31332 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31333 clause_loc);
31335 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31336 return list;
31340 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31341 return list;
31343 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31344 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31345 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31347 cp_lexer_consume_token (parser->lexer);
31348 expr = integer_zero_node;
31350 else
31351 expr = cp_parser_constant_expression (parser);
31353 tile = tree_cons (NULL_TREE, expr, tile);
31355 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31357 /* Consume the trailing ')'. */
31358 cp_lexer_consume_token (parser->lexer);
31360 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31361 tile = nreverse (tile);
31362 OMP_CLAUSE_TILE_LIST (c) = tile;
31363 OMP_CLAUSE_CHAIN (c) = list;
31364 return c;
31367 /* OpenACC 2.0
31368 Parse wait clause or directive parameters. */
31370 static tree
31371 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31373 vec<tree, va_gc> *args;
31374 tree t, args_tree;
31376 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31377 /*cast_p=*/false,
31378 /*allow_expansion_p=*/true,
31379 /*non_constant_p=*/NULL);
31381 if (args == NULL || args->length () == 0)
31383 cp_parser_error (parser, "expected integer expression before ')'");
31384 if (args != NULL)
31385 release_tree_vector (args);
31386 return list;
31389 args_tree = build_tree_list_vec (args);
31391 release_tree_vector (args);
31393 for (t = args_tree; t; t = TREE_CHAIN (t))
31395 tree targ = TREE_VALUE (t);
31397 if (targ != error_mark_node)
31399 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31400 error ("%<wait%> expression must be integral");
31401 else
31403 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31405 mark_rvalue_use (targ);
31406 OMP_CLAUSE_DECL (c) = targ;
31407 OMP_CLAUSE_CHAIN (c) = list;
31408 list = c;
31413 return list;
31416 /* OpenACC:
31417 wait ( int-expr-list ) */
31419 static tree
31420 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31422 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31424 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31425 return list;
31427 list = cp_parser_oacc_wait_list (parser, location, list);
31429 return list;
31432 /* OpenMP 3.0:
31433 collapse ( constant-expression ) */
31435 static tree
31436 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31438 tree c, num;
31439 location_t loc;
31440 HOST_WIDE_INT n;
31442 loc = cp_lexer_peek_token (parser->lexer)->location;
31443 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31444 return list;
31446 num = cp_parser_constant_expression (parser);
31448 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31449 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31450 /*or_comma=*/false,
31451 /*consume_paren=*/true);
31453 if (num == error_mark_node)
31454 return list;
31455 num = fold_non_dependent_expr (num);
31456 if (!tree_fits_shwi_p (num)
31457 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31458 || (n = tree_to_shwi (num)) <= 0
31459 || (int) n != n)
31461 error_at (loc, "collapse argument needs positive constant integer expression");
31462 return list;
31465 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31466 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31467 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31468 OMP_CLAUSE_CHAIN (c) = list;
31469 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31471 return c;
31474 /* OpenMP 2.5:
31475 default ( shared | none )
31477 OpenACC 2.0
31478 default (none) */
31480 static tree
31481 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31482 location_t location, bool is_oacc)
31484 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31485 tree c;
31487 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31488 return list;
31489 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31491 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31492 const char *p = IDENTIFIER_POINTER (id);
31494 switch (p[0])
31496 case 'n':
31497 if (strcmp ("none", p) != 0)
31498 goto invalid_kind;
31499 kind = OMP_CLAUSE_DEFAULT_NONE;
31500 break;
31502 case 's':
31503 if (strcmp ("shared", p) != 0 || is_oacc)
31504 goto invalid_kind;
31505 kind = OMP_CLAUSE_DEFAULT_SHARED;
31506 break;
31508 default:
31509 goto invalid_kind;
31512 cp_lexer_consume_token (parser->lexer);
31514 else
31516 invalid_kind:
31517 if (is_oacc)
31518 cp_parser_error (parser, "expected %<none%>");
31519 else
31520 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31523 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31524 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31525 /*or_comma=*/false,
31526 /*consume_paren=*/true);
31528 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31529 return list;
31531 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31532 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31533 OMP_CLAUSE_CHAIN (c) = list;
31534 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31536 return c;
31539 /* OpenMP 3.1:
31540 final ( expression ) */
31542 static tree
31543 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31545 tree t, c;
31547 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31548 return list;
31550 t = cp_parser_condition (parser);
31552 if (t == error_mark_node
31553 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31554 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31555 /*or_comma=*/false,
31556 /*consume_paren=*/true);
31558 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31560 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31561 OMP_CLAUSE_FINAL_EXPR (c) = t;
31562 OMP_CLAUSE_CHAIN (c) = list;
31564 return c;
31567 /* OpenMP 2.5:
31568 if ( expression )
31570 OpenMP 4.5:
31571 if ( directive-name-modifier : expression )
31573 directive-name-modifier:
31574 parallel | task | taskloop | target data | target | target update
31575 | target enter data | target exit data */
31577 static tree
31578 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31579 bool is_omp)
31581 tree t, c;
31582 enum tree_code if_modifier = ERROR_MARK;
31584 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31585 return list;
31587 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31589 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31590 const char *p = IDENTIFIER_POINTER (id);
31591 int n = 2;
31593 if (strcmp ("parallel", p) == 0)
31594 if_modifier = OMP_PARALLEL;
31595 else if (strcmp ("task", p) == 0)
31596 if_modifier = OMP_TASK;
31597 else if (strcmp ("taskloop", p) == 0)
31598 if_modifier = OMP_TASKLOOP;
31599 else if (strcmp ("target", p) == 0)
31601 if_modifier = OMP_TARGET;
31602 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31604 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31605 p = IDENTIFIER_POINTER (id);
31606 if (strcmp ("data", p) == 0)
31607 if_modifier = OMP_TARGET_DATA;
31608 else if (strcmp ("update", p) == 0)
31609 if_modifier = OMP_TARGET_UPDATE;
31610 else if (strcmp ("enter", p) == 0)
31611 if_modifier = OMP_TARGET_ENTER_DATA;
31612 else if (strcmp ("exit", p) == 0)
31613 if_modifier = OMP_TARGET_EXIT_DATA;
31614 if (if_modifier != OMP_TARGET)
31615 n = 3;
31616 else
31618 location_t loc
31619 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31620 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31621 "or %<exit%>");
31622 if_modifier = ERROR_MARK;
31624 if (if_modifier == OMP_TARGET_ENTER_DATA
31625 || if_modifier == OMP_TARGET_EXIT_DATA)
31627 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31629 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31630 p = IDENTIFIER_POINTER (id);
31631 if (strcmp ("data", p) == 0)
31632 n = 4;
31634 if (n != 4)
31636 location_t loc
31637 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31638 error_at (loc, "expected %<data%>");
31639 if_modifier = ERROR_MARK;
31644 if (if_modifier != ERROR_MARK)
31646 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31648 while (n-- > 0)
31649 cp_lexer_consume_token (parser->lexer);
31651 else
31653 if (n > 2)
31655 location_t loc
31656 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31657 error_at (loc, "expected %<:%>");
31659 if_modifier = ERROR_MARK;
31664 t = cp_parser_condition (parser);
31666 if (t == error_mark_node
31667 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31668 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31669 /*or_comma=*/false,
31670 /*consume_paren=*/true);
31672 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31673 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31675 if (if_modifier != ERROR_MARK
31676 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31678 const char *p = NULL;
31679 switch (if_modifier)
31681 case OMP_PARALLEL: p = "parallel"; break;
31682 case OMP_TASK: p = "task"; break;
31683 case OMP_TASKLOOP: p = "taskloop"; break;
31684 case OMP_TARGET_DATA: p = "target data"; break;
31685 case OMP_TARGET: p = "target"; break;
31686 case OMP_TARGET_UPDATE: p = "target update"; break;
31687 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31688 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31689 default: gcc_unreachable ();
31691 error_at (location, "too many %<if%> clauses with %qs modifier",
31693 return list;
31695 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31697 if (!is_omp)
31698 error_at (location, "too many %<if%> clauses");
31699 else
31700 error_at (location, "too many %<if%> clauses without modifier");
31701 return list;
31703 else if (if_modifier == ERROR_MARK
31704 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31706 error_at (location, "if any %<if%> clause has modifier, then all "
31707 "%<if%> clauses have to use modifier");
31708 return list;
31712 c = build_omp_clause (location, OMP_CLAUSE_IF);
31713 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31714 OMP_CLAUSE_IF_EXPR (c) = t;
31715 OMP_CLAUSE_CHAIN (c) = list;
31717 return c;
31720 /* OpenMP 3.1:
31721 mergeable */
31723 static tree
31724 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31725 tree list, location_t location)
31727 tree c;
31729 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31730 location);
31732 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31733 OMP_CLAUSE_CHAIN (c) = list;
31734 return c;
31737 /* OpenMP 2.5:
31738 nowait */
31740 static tree
31741 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31742 tree list, location_t location)
31744 tree c;
31746 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31748 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31749 OMP_CLAUSE_CHAIN (c) = list;
31750 return c;
31753 /* OpenMP 2.5:
31754 num_threads ( expression ) */
31756 static tree
31757 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31758 location_t location)
31760 tree t, c;
31762 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31763 return list;
31765 t = cp_parser_expression (parser);
31767 if (t == error_mark_node
31768 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31769 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31770 /*or_comma=*/false,
31771 /*consume_paren=*/true);
31773 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31774 "num_threads", location);
31776 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31777 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31778 OMP_CLAUSE_CHAIN (c) = list;
31780 return c;
31783 /* OpenMP 4.5:
31784 num_tasks ( expression ) */
31786 static tree
31787 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31788 location_t location)
31790 tree t, c;
31792 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31793 return list;
31795 t = cp_parser_expression (parser);
31797 if (t == error_mark_node
31798 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31799 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31800 /*or_comma=*/false,
31801 /*consume_paren=*/true);
31803 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31804 "num_tasks", location);
31806 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31807 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31808 OMP_CLAUSE_CHAIN (c) = list;
31810 return c;
31813 /* OpenMP 4.5:
31814 grainsize ( expression ) */
31816 static tree
31817 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31818 location_t location)
31820 tree t, c;
31822 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31823 return list;
31825 t = cp_parser_expression (parser);
31827 if (t == error_mark_node
31828 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31829 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31830 /*or_comma=*/false,
31831 /*consume_paren=*/true);
31833 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31834 "grainsize", location);
31836 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31837 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31838 OMP_CLAUSE_CHAIN (c) = list;
31840 return c;
31843 /* OpenMP 4.5:
31844 priority ( expression ) */
31846 static tree
31847 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31848 location_t location)
31850 tree t, c;
31852 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31853 return list;
31855 t = cp_parser_expression (parser);
31857 if (t == error_mark_node
31858 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31859 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31860 /*or_comma=*/false,
31861 /*consume_paren=*/true);
31863 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31864 "priority", location);
31866 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31867 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31868 OMP_CLAUSE_CHAIN (c) = list;
31870 return c;
31873 /* OpenMP 4.5:
31874 hint ( expression ) */
31876 static tree
31877 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31878 location_t location)
31880 tree t, c;
31882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31883 return list;
31885 t = cp_parser_expression (parser);
31887 if (t == error_mark_node
31888 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31889 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31890 /*or_comma=*/false,
31891 /*consume_paren=*/true);
31893 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31895 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31896 OMP_CLAUSE_HINT_EXPR (c) = t;
31897 OMP_CLAUSE_CHAIN (c) = list;
31899 return c;
31902 /* OpenMP 4.5:
31903 defaultmap ( tofrom : scalar ) */
31905 static tree
31906 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31907 location_t location)
31909 tree c, id;
31910 const char *p;
31912 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31913 return list;
31915 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31917 cp_parser_error (parser, "expected %<tofrom%>");
31918 goto out_err;
31920 id = cp_lexer_peek_token (parser->lexer)->u.value;
31921 p = IDENTIFIER_POINTER (id);
31922 if (strcmp (p, "tofrom") != 0)
31924 cp_parser_error (parser, "expected %<tofrom%>");
31925 goto out_err;
31927 cp_lexer_consume_token (parser->lexer);
31928 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31929 goto out_err;
31931 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31933 cp_parser_error (parser, "expected %<scalar%>");
31934 goto out_err;
31936 id = cp_lexer_peek_token (parser->lexer)->u.value;
31937 p = IDENTIFIER_POINTER (id);
31938 if (strcmp (p, "scalar") != 0)
31940 cp_parser_error (parser, "expected %<scalar%>");
31941 goto out_err;
31943 cp_lexer_consume_token (parser->lexer);
31944 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31945 goto out_err;
31947 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31948 location);
31950 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31951 OMP_CLAUSE_CHAIN (c) = list;
31952 return c;
31954 out_err:
31955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31956 /*or_comma=*/false,
31957 /*consume_paren=*/true);
31958 return list;
31961 /* OpenMP 2.5:
31962 ordered
31964 OpenMP 4.5:
31965 ordered ( constant-expression ) */
31967 static tree
31968 cp_parser_omp_clause_ordered (cp_parser *parser,
31969 tree list, location_t location)
31971 tree c, num = NULL_TREE;
31972 HOST_WIDE_INT n;
31974 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31975 "ordered", location);
31977 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31979 cp_lexer_consume_token (parser->lexer);
31981 num = cp_parser_constant_expression (parser);
31983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31985 /*or_comma=*/false,
31986 /*consume_paren=*/true);
31988 if (num == error_mark_node)
31989 return list;
31990 num = fold_non_dependent_expr (num);
31991 if (!tree_fits_shwi_p (num)
31992 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31993 || (n = tree_to_shwi (num)) <= 0
31994 || (int) n != n)
31996 error_at (location,
31997 "ordered argument needs positive constant integer "
31998 "expression");
31999 return list;
32003 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32004 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32005 OMP_CLAUSE_CHAIN (c) = list;
32006 return c;
32009 /* OpenMP 2.5:
32010 reduction ( reduction-operator : variable-list )
32012 reduction-operator:
32013 One of: + * - & ^ | && ||
32015 OpenMP 3.1:
32017 reduction-operator:
32018 One of: + * - & ^ | && || min max
32020 OpenMP 4.0:
32022 reduction-operator:
32023 One of: + * - & ^ | && ||
32024 id-expression */
32026 static tree
32027 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32029 enum tree_code code = ERROR_MARK;
32030 tree nlist, c, id = NULL_TREE;
32032 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32033 return list;
32035 switch (cp_lexer_peek_token (parser->lexer)->type)
32037 case CPP_PLUS: code = PLUS_EXPR; break;
32038 case CPP_MULT: code = MULT_EXPR; break;
32039 case CPP_MINUS: code = MINUS_EXPR; break;
32040 case CPP_AND: code = BIT_AND_EXPR; break;
32041 case CPP_XOR: code = BIT_XOR_EXPR; break;
32042 case CPP_OR: code = BIT_IOR_EXPR; break;
32043 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32044 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32045 default: break;
32048 if (code != ERROR_MARK)
32049 cp_lexer_consume_token (parser->lexer);
32050 else
32052 bool saved_colon_corrects_to_scope_p;
32053 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32054 parser->colon_corrects_to_scope_p = false;
32055 id = cp_parser_id_expression (parser, /*template_p=*/false,
32056 /*check_dependency_p=*/true,
32057 /*template_p=*/NULL,
32058 /*declarator_p=*/false,
32059 /*optional_p=*/false);
32060 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32061 if (identifier_p (id))
32063 const char *p = IDENTIFIER_POINTER (id);
32065 if (strcmp (p, "min") == 0)
32066 code = MIN_EXPR;
32067 else if (strcmp (p, "max") == 0)
32068 code = MAX_EXPR;
32069 else if (id == cp_operator_id (PLUS_EXPR))
32070 code = PLUS_EXPR;
32071 else if (id == cp_operator_id (MULT_EXPR))
32072 code = MULT_EXPR;
32073 else if (id == cp_operator_id (MINUS_EXPR))
32074 code = MINUS_EXPR;
32075 else if (id == cp_operator_id (BIT_AND_EXPR))
32076 code = BIT_AND_EXPR;
32077 else if (id == cp_operator_id (BIT_IOR_EXPR))
32078 code = BIT_IOR_EXPR;
32079 else if (id == cp_operator_id (BIT_XOR_EXPR))
32080 code = BIT_XOR_EXPR;
32081 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32082 code = TRUTH_ANDIF_EXPR;
32083 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32084 code = TRUTH_ORIF_EXPR;
32085 id = omp_reduction_id (code, id, NULL_TREE);
32086 tree scope = parser->scope;
32087 if (scope)
32088 id = build_qualified_name (NULL_TREE, scope, id, false);
32089 parser->scope = NULL_TREE;
32090 parser->qualifying_scope = NULL_TREE;
32091 parser->object_scope = NULL_TREE;
32093 else
32095 error ("invalid reduction-identifier");
32096 resync_fail:
32097 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32098 /*or_comma=*/false,
32099 /*consume_paren=*/true);
32100 return list;
32104 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32105 goto resync_fail;
32107 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32108 NULL);
32109 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32111 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32112 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32115 return nlist;
32118 /* OpenMP 2.5:
32119 schedule ( schedule-kind )
32120 schedule ( schedule-kind , expression )
32122 schedule-kind:
32123 static | dynamic | guided | runtime | auto
32125 OpenMP 4.5:
32126 schedule ( schedule-modifier : schedule-kind )
32127 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32129 schedule-modifier:
32130 simd
32131 monotonic
32132 nonmonotonic */
32134 static tree
32135 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32137 tree c, t;
32138 int modifiers = 0, nmodifiers = 0;
32140 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32141 return list;
32143 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32145 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32147 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32148 const char *p = IDENTIFIER_POINTER (id);
32149 if (strcmp ("simd", p) == 0)
32150 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32151 else if (strcmp ("monotonic", p) == 0)
32152 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32153 else if (strcmp ("nonmonotonic", p) == 0)
32154 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32155 else
32156 break;
32157 cp_lexer_consume_token (parser->lexer);
32158 if (nmodifiers++ == 0
32159 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32160 cp_lexer_consume_token (parser->lexer);
32161 else
32163 cp_parser_require (parser, CPP_COLON, RT_COLON);
32164 break;
32168 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32170 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32171 const char *p = IDENTIFIER_POINTER (id);
32173 switch (p[0])
32175 case 'd':
32176 if (strcmp ("dynamic", p) != 0)
32177 goto invalid_kind;
32178 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32179 break;
32181 case 'g':
32182 if (strcmp ("guided", p) != 0)
32183 goto invalid_kind;
32184 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32185 break;
32187 case 'r':
32188 if (strcmp ("runtime", p) != 0)
32189 goto invalid_kind;
32190 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32191 break;
32193 default:
32194 goto invalid_kind;
32197 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32198 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32199 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32200 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32201 else
32202 goto invalid_kind;
32203 cp_lexer_consume_token (parser->lexer);
32205 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32206 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32207 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32208 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32210 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32211 "specified");
32212 modifiers = 0;
32215 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32217 cp_token *token;
32218 cp_lexer_consume_token (parser->lexer);
32220 token = cp_lexer_peek_token (parser->lexer);
32221 t = cp_parser_assignment_expression (parser);
32223 if (t == error_mark_node)
32224 goto resync_fail;
32225 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32226 error_at (token->location, "schedule %<runtime%> does not take "
32227 "a %<chunk_size%> parameter");
32228 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32229 error_at (token->location, "schedule %<auto%> does not take "
32230 "a %<chunk_size%> parameter");
32231 else
32232 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32234 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32235 goto resync_fail;
32237 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32238 goto resync_fail;
32240 OMP_CLAUSE_SCHEDULE_KIND (c)
32241 = (enum omp_clause_schedule_kind)
32242 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32244 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32245 OMP_CLAUSE_CHAIN (c) = list;
32246 return c;
32248 invalid_kind:
32249 cp_parser_error (parser, "invalid schedule kind");
32250 resync_fail:
32251 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32252 /*or_comma=*/false,
32253 /*consume_paren=*/true);
32254 return list;
32257 /* OpenMP 3.0:
32258 untied */
32260 static tree
32261 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32262 tree list, location_t location)
32264 tree c;
32266 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32268 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32269 OMP_CLAUSE_CHAIN (c) = list;
32270 return c;
32273 /* OpenMP 4.0:
32274 inbranch
32275 notinbranch */
32277 static tree
32278 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32279 tree list, location_t location)
32281 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32282 tree c = build_omp_clause (location, code);
32283 OMP_CLAUSE_CHAIN (c) = list;
32284 return c;
32287 /* OpenMP 4.0:
32288 parallel
32290 sections
32291 taskgroup */
32293 static tree
32294 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32295 enum omp_clause_code code,
32296 tree list, location_t location)
32298 tree c = build_omp_clause (location, code);
32299 OMP_CLAUSE_CHAIN (c) = list;
32300 return c;
32303 /* OpenMP 4.5:
32304 nogroup */
32306 static tree
32307 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32308 tree list, location_t location)
32310 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32311 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32312 OMP_CLAUSE_CHAIN (c) = list;
32313 return c;
32316 /* OpenMP 4.5:
32317 simd
32318 threads */
32320 static tree
32321 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32322 enum omp_clause_code code,
32323 tree list, location_t location)
32325 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32326 tree c = build_omp_clause (location, code);
32327 OMP_CLAUSE_CHAIN (c) = list;
32328 return c;
32331 /* OpenMP 4.0:
32332 num_teams ( expression ) */
32334 static tree
32335 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32336 location_t location)
32338 tree t, c;
32340 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32341 return list;
32343 t = cp_parser_expression (parser);
32345 if (t == error_mark_node
32346 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32347 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32348 /*or_comma=*/false,
32349 /*consume_paren=*/true);
32351 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32352 "num_teams", location);
32354 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32355 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32356 OMP_CLAUSE_CHAIN (c) = list;
32358 return c;
32361 /* OpenMP 4.0:
32362 thread_limit ( expression ) */
32364 static tree
32365 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32366 location_t location)
32368 tree t, c;
32370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32371 return list;
32373 t = cp_parser_expression (parser);
32375 if (t == error_mark_node
32376 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32378 /*or_comma=*/false,
32379 /*consume_paren=*/true);
32381 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32382 "thread_limit", location);
32384 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32385 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32386 OMP_CLAUSE_CHAIN (c) = list;
32388 return c;
32391 /* OpenMP 4.0:
32392 aligned ( variable-list )
32393 aligned ( variable-list : constant-expression ) */
32395 static tree
32396 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32398 tree nlist, c, alignment = NULL_TREE;
32399 bool colon;
32401 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32402 return list;
32404 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32405 &colon);
32407 if (colon)
32409 alignment = cp_parser_constant_expression (parser);
32411 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32412 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32413 /*or_comma=*/false,
32414 /*consume_paren=*/true);
32416 if (alignment == error_mark_node)
32417 alignment = NULL_TREE;
32420 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32421 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32423 return nlist;
32426 /* OpenMP 4.0:
32427 linear ( variable-list )
32428 linear ( variable-list : expression )
32430 OpenMP 4.5:
32431 linear ( modifier ( variable-list ) )
32432 linear ( modifier ( variable-list ) : expression ) */
32434 static tree
32435 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32436 bool is_cilk_simd_fn, bool declare_simd)
32438 tree nlist, c, step = integer_one_node;
32439 bool colon;
32440 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32442 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32443 return list;
32445 if (!is_cilk_simd_fn
32446 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32448 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32449 const char *p = IDENTIFIER_POINTER (id);
32451 if (strcmp ("ref", p) == 0)
32452 kind = OMP_CLAUSE_LINEAR_REF;
32453 else if (strcmp ("val", p) == 0)
32454 kind = OMP_CLAUSE_LINEAR_VAL;
32455 else if (strcmp ("uval", p) == 0)
32456 kind = OMP_CLAUSE_LINEAR_UVAL;
32457 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32458 cp_lexer_consume_token (parser->lexer);
32459 else
32460 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32463 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32464 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32465 &colon);
32466 else
32468 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32469 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32470 if (colon)
32471 cp_parser_require (parser, CPP_COLON, RT_COLON);
32472 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32474 /*or_comma=*/false,
32475 /*consume_paren=*/true);
32478 if (colon)
32480 step = NULL_TREE;
32481 if (declare_simd
32482 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32483 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32485 cp_token *token = cp_lexer_peek_token (parser->lexer);
32486 cp_parser_parse_tentatively (parser);
32487 step = cp_parser_id_expression (parser, /*template_p=*/false,
32488 /*check_dependency_p=*/true,
32489 /*template_p=*/NULL,
32490 /*declarator_p=*/false,
32491 /*optional_p=*/false);
32492 if (step != error_mark_node)
32493 step = cp_parser_lookup_name_simple (parser, step, token->location);
32494 if (step == error_mark_node)
32496 step = NULL_TREE;
32497 cp_parser_abort_tentative_parse (parser);
32499 else if (!cp_parser_parse_definitely (parser))
32500 step = NULL_TREE;
32502 if (!step)
32503 step = cp_parser_expression (parser);
32505 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32507 sorry ("using parameters for %<linear%> step is not supported yet");
32508 step = integer_one_node;
32510 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32511 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32512 /*or_comma=*/false,
32513 /*consume_paren=*/true);
32515 if (step == error_mark_node)
32516 return list;
32519 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32521 OMP_CLAUSE_LINEAR_STEP (c) = step;
32522 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32525 return nlist;
32528 /* OpenMP 4.0:
32529 safelen ( constant-expression ) */
32531 static tree
32532 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32533 location_t location)
32535 tree t, c;
32537 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32538 return list;
32540 t = cp_parser_constant_expression (parser);
32542 if (t == error_mark_node
32543 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32544 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32545 /*or_comma=*/false,
32546 /*consume_paren=*/true);
32548 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32550 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32551 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32552 OMP_CLAUSE_CHAIN (c) = list;
32554 return c;
32557 /* OpenMP 4.0:
32558 simdlen ( constant-expression ) */
32560 static tree
32561 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32562 location_t location)
32564 tree t, c;
32566 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32567 return list;
32569 t = cp_parser_constant_expression (parser);
32571 if (t == error_mark_node
32572 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32574 /*or_comma=*/false,
32575 /*consume_paren=*/true);
32577 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32579 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32580 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32581 OMP_CLAUSE_CHAIN (c) = list;
32583 return c;
32586 /* OpenMP 4.5:
32587 vec:
32588 identifier [+/- integer]
32589 vec , identifier [+/- integer]
32592 static tree
32593 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32594 tree list)
32596 tree vec = NULL;
32598 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32600 cp_parser_error (parser, "expected identifier");
32601 return list;
32604 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32606 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32607 tree t, identifier = cp_parser_identifier (parser);
32608 tree addend = NULL;
32610 if (identifier == error_mark_node)
32611 t = error_mark_node;
32612 else
32614 t = cp_parser_lookup_name_simple
32615 (parser, identifier,
32616 cp_lexer_peek_token (parser->lexer)->location);
32617 if (t == error_mark_node)
32618 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32619 id_loc);
32622 bool neg = false;
32623 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32624 neg = true;
32625 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32627 addend = integer_zero_node;
32628 goto add_to_vector;
32630 cp_lexer_consume_token (parser->lexer);
32632 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32634 cp_parser_error (parser, "expected integer");
32635 return list;
32638 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32639 if (TREE_CODE (addend) != INTEGER_CST)
32641 cp_parser_error (parser, "expected integer");
32642 return list;
32644 cp_lexer_consume_token (parser->lexer);
32646 add_to_vector:
32647 if (t != error_mark_node)
32649 vec = tree_cons (addend, t, vec);
32650 if (neg)
32651 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32654 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32655 break;
32657 cp_lexer_consume_token (parser->lexer);
32660 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32662 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32663 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32664 OMP_CLAUSE_DECL (u) = nreverse (vec);
32665 OMP_CLAUSE_CHAIN (u) = list;
32666 return u;
32668 return list;
32671 /* OpenMP 4.0:
32672 depend ( depend-kind : variable-list )
32674 depend-kind:
32675 in | out | inout
32677 OpenMP 4.5:
32678 depend ( source )
32680 depend ( sink : vec ) */
32682 static tree
32683 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32685 tree nlist, c;
32686 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32688 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32689 return list;
32691 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32693 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32694 const char *p = IDENTIFIER_POINTER (id);
32696 if (strcmp ("in", p) == 0)
32697 kind = OMP_CLAUSE_DEPEND_IN;
32698 else if (strcmp ("inout", p) == 0)
32699 kind = OMP_CLAUSE_DEPEND_INOUT;
32700 else if (strcmp ("out", p) == 0)
32701 kind = OMP_CLAUSE_DEPEND_OUT;
32702 else if (strcmp ("source", p) == 0)
32703 kind = OMP_CLAUSE_DEPEND_SOURCE;
32704 else if (strcmp ("sink", p) == 0)
32705 kind = OMP_CLAUSE_DEPEND_SINK;
32706 else
32707 goto invalid_kind;
32709 else
32710 goto invalid_kind;
32712 cp_lexer_consume_token (parser->lexer);
32714 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32716 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32717 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32718 OMP_CLAUSE_DECL (c) = NULL_TREE;
32719 OMP_CLAUSE_CHAIN (c) = list;
32720 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32722 /*or_comma=*/false,
32723 /*consume_paren=*/true);
32724 return c;
32727 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32728 goto resync_fail;
32730 if (kind == OMP_CLAUSE_DEPEND_SINK)
32731 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32732 else
32734 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32735 list, NULL);
32737 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32738 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32740 return nlist;
32742 invalid_kind:
32743 cp_parser_error (parser, "invalid depend kind");
32744 resync_fail:
32745 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32746 /*or_comma=*/false,
32747 /*consume_paren=*/true);
32748 return list;
32751 /* OpenMP 4.0:
32752 map ( map-kind : variable-list )
32753 map ( variable-list )
32755 map-kind:
32756 alloc | to | from | tofrom
32758 OpenMP 4.5:
32759 map-kind:
32760 alloc | to | from | tofrom | release | delete
32762 map ( always [,] map-kind: variable-list ) */
32764 static tree
32765 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32767 tree nlist, c;
32768 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32769 bool always = false;
32771 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32772 return list;
32774 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32776 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32777 const char *p = IDENTIFIER_POINTER (id);
32779 if (strcmp ("always", p) == 0)
32781 int nth = 2;
32782 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32783 nth++;
32784 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32785 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32786 == RID_DELETE))
32787 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32788 == CPP_COLON))
32790 always = true;
32791 cp_lexer_consume_token (parser->lexer);
32792 if (nth == 3)
32793 cp_lexer_consume_token (parser->lexer);
32798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32799 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32801 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32802 const char *p = IDENTIFIER_POINTER (id);
32804 if (strcmp ("alloc", p) == 0)
32805 kind = GOMP_MAP_ALLOC;
32806 else if (strcmp ("to", p) == 0)
32807 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32808 else if (strcmp ("from", p) == 0)
32809 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32810 else if (strcmp ("tofrom", p) == 0)
32811 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32812 else if (strcmp ("release", p) == 0)
32813 kind = GOMP_MAP_RELEASE;
32814 else
32816 cp_parser_error (parser, "invalid map kind");
32817 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32818 /*or_comma=*/false,
32819 /*consume_paren=*/true);
32820 return list;
32822 cp_lexer_consume_token (parser->lexer);
32823 cp_lexer_consume_token (parser->lexer);
32825 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32826 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32828 kind = GOMP_MAP_DELETE;
32829 cp_lexer_consume_token (parser->lexer);
32830 cp_lexer_consume_token (parser->lexer);
32833 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32834 NULL);
32836 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32837 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32839 return nlist;
32842 /* OpenMP 4.0:
32843 device ( expression ) */
32845 static tree
32846 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32847 location_t location)
32849 tree t, c;
32851 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32852 return list;
32854 t = cp_parser_expression (parser);
32856 if (t == error_mark_node
32857 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32858 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32859 /*or_comma=*/false,
32860 /*consume_paren=*/true);
32862 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32863 "device", location);
32865 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32866 OMP_CLAUSE_DEVICE_ID (c) = t;
32867 OMP_CLAUSE_CHAIN (c) = list;
32869 return c;
32872 /* OpenMP 4.0:
32873 dist_schedule ( static )
32874 dist_schedule ( static , expression ) */
32876 static tree
32877 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32878 location_t location)
32880 tree c, t;
32882 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32883 return list;
32885 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32887 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32888 goto invalid_kind;
32889 cp_lexer_consume_token (parser->lexer);
32891 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32893 cp_lexer_consume_token (parser->lexer);
32895 t = cp_parser_assignment_expression (parser);
32897 if (t == error_mark_node)
32898 goto resync_fail;
32899 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32901 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32902 goto resync_fail;
32904 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32905 goto resync_fail;
32907 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32908 location);
32909 OMP_CLAUSE_CHAIN (c) = list;
32910 return c;
32912 invalid_kind:
32913 cp_parser_error (parser, "invalid dist_schedule kind");
32914 resync_fail:
32915 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32916 /*or_comma=*/false,
32917 /*consume_paren=*/true);
32918 return list;
32921 /* OpenMP 4.0:
32922 proc_bind ( proc-bind-kind )
32924 proc-bind-kind:
32925 master | close | spread */
32927 static tree
32928 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32929 location_t location)
32931 tree c;
32932 enum omp_clause_proc_bind_kind kind;
32934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32935 return list;
32937 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32939 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32940 const char *p = IDENTIFIER_POINTER (id);
32942 if (strcmp ("master", p) == 0)
32943 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32944 else if (strcmp ("close", p) == 0)
32945 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32946 else if (strcmp ("spread", p) == 0)
32947 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32948 else
32949 goto invalid_kind;
32951 else
32952 goto invalid_kind;
32954 cp_lexer_consume_token (parser->lexer);
32955 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32956 goto resync_fail;
32958 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32959 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32960 location);
32961 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32962 OMP_CLAUSE_CHAIN (c) = list;
32963 return c;
32965 invalid_kind:
32966 cp_parser_error (parser, "invalid depend kind");
32967 resync_fail:
32968 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32969 /*or_comma=*/false,
32970 /*consume_paren=*/true);
32971 return list;
32974 /* OpenACC:
32975 async [( int-expr )] */
32977 static tree
32978 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32980 tree c, t;
32981 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32983 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32985 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32987 cp_lexer_consume_token (parser->lexer);
32989 t = cp_parser_expression (parser);
32990 if (t == error_mark_node
32991 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32993 /*or_comma=*/false,
32994 /*consume_paren=*/true);
32997 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32999 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33000 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33001 OMP_CLAUSE_CHAIN (c) = list;
33002 list = c;
33004 return list;
33007 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33008 is a bitmask in MASK. Return the list of clauses found. */
33010 static tree
33011 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33012 const char *where, cp_token *pragma_tok,
33013 bool finish_p = true)
33015 tree clauses = NULL;
33016 bool first = true;
33018 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33020 location_t here;
33021 pragma_omp_clause c_kind;
33022 omp_clause_code code;
33023 const char *c_name;
33024 tree prev = clauses;
33026 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33027 cp_lexer_consume_token (parser->lexer);
33029 here = cp_lexer_peek_token (parser->lexer)->location;
33030 c_kind = cp_parser_omp_clause_name (parser);
33032 switch (c_kind)
33034 case PRAGMA_OACC_CLAUSE_ASYNC:
33035 clauses = cp_parser_oacc_clause_async (parser, clauses);
33036 c_name = "async";
33037 break;
33038 case PRAGMA_OACC_CLAUSE_AUTO:
33039 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33040 clauses, here);
33041 c_name = "auto";
33042 break;
33043 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33044 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33045 c_name = "collapse";
33046 break;
33047 case PRAGMA_OACC_CLAUSE_COPY:
33048 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33049 c_name = "copy";
33050 break;
33051 case PRAGMA_OACC_CLAUSE_COPYIN:
33052 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33053 c_name = "copyin";
33054 break;
33055 case PRAGMA_OACC_CLAUSE_COPYOUT:
33056 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33057 c_name = "copyout";
33058 break;
33059 case PRAGMA_OACC_CLAUSE_CREATE:
33060 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33061 c_name = "create";
33062 break;
33063 case PRAGMA_OACC_CLAUSE_DELETE:
33064 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33065 c_name = "delete";
33066 break;
33067 case PRAGMA_OMP_CLAUSE_DEFAULT:
33068 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33069 c_name = "default";
33070 break;
33071 case PRAGMA_OACC_CLAUSE_DEVICE:
33072 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33073 c_name = "device";
33074 break;
33075 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33076 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33077 c_name = "deviceptr";
33078 break;
33079 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33080 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33081 c_name = "device_resident";
33082 break;
33083 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33084 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33085 clauses);
33086 c_name = "firstprivate";
33087 break;
33088 case PRAGMA_OACC_CLAUSE_GANG:
33089 c_name = "gang";
33090 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33091 c_name, clauses);
33092 break;
33093 case PRAGMA_OACC_CLAUSE_HOST:
33094 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33095 c_name = "host";
33096 break;
33097 case PRAGMA_OACC_CLAUSE_IF:
33098 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33099 c_name = "if";
33100 break;
33101 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33102 clauses = cp_parser_oacc_simple_clause (parser,
33103 OMP_CLAUSE_INDEPENDENT,
33104 clauses, here);
33105 c_name = "independent";
33106 break;
33107 case PRAGMA_OACC_CLAUSE_LINK:
33108 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33109 c_name = "link";
33110 break;
33111 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33112 code = OMP_CLAUSE_NUM_GANGS;
33113 c_name = "num_gangs";
33114 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33115 clauses);
33116 break;
33117 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33118 c_name = "num_workers";
33119 code = OMP_CLAUSE_NUM_WORKERS;
33120 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33121 clauses);
33122 break;
33123 case PRAGMA_OACC_CLAUSE_PRESENT:
33124 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33125 c_name = "present";
33126 break;
33127 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33128 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33129 c_name = "present_or_copy";
33130 break;
33131 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33132 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33133 c_name = "present_or_copyin";
33134 break;
33135 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33136 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33137 c_name = "present_or_copyout";
33138 break;
33139 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33140 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33141 c_name = "present_or_create";
33142 break;
33143 case PRAGMA_OACC_CLAUSE_PRIVATE:
33144 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33145 clauses);
33146 c_name = "private";
33147 break;
33148 case PRAGMA_OACC_CLAUSE_REDUCTION:
33149 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33150 c_name = "reduction";
33151 break;
33152 case PRAGMA_OACC_CLAUSE_SELF:
33153 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33154 c_name = "self";
33155 break;
33156 case PRAGMA_OACC_CLAUSE_SEQ:
33157 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33158 clauses, here);
33159 c_name = "seq";
33160 break;
33161 case PRAGMA_OACC_CLAUSE_TILE:
33162 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33163 c_name = "tile";
33164 break;
33165 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33166 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33167 clauses);
33168 c_name = "use_device";
33169 break;
33170 case PRAGMA_OACC_CLAUSE_VECTOR:
33171 c_name = "vector";
33172 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33173 c_name, clauses);
33174 break;
33175 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33176 c_name = "vector_length";
33177 code = OMP_CLAUSE_VECTOR_LENGTH;
33178 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33179 clauses);
33180 break;
33181 case PRAGMA_OACC_CLAUSE_WAIT:
33182 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33183 c_name = "wait";
33184 break;
33185 case PRAGMA_OACC_CLAUSE_WORKER:
33186 c_name = "worker";
33187 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33188 c_name, clauses);
33189 break;
33190 default:
33191 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33192 goto saw_error;
33195 first = false;
33197 if (((mask >> c_kind) & 1) == 0)
33199 /* Remove the invalid clause(s) from the list to avoid
33200 confusing the rest of the compiler. */
33201 clauses = prev;
33202 error_at (here, "%qs is not valid for %qs", c_name, where);
33206 saw_error:
33207 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33209 if (finish_p)
33210 return finish_omp_clauses (clauses, C_ORT_ACC);
33212 return clauses;
33215 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33216 is a bitmask in MASK. Return the list of clauses found; the result
33217 of clause default goes in *pdefault. */
33219 static tree
33220 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33221 const char *where, cp_token *pragma_tok,
33222 bool finish_p = true)
33224 tree clauses = NULL;
33225 bool first = true;
33226 cp_token *token = NULL;
33228 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33230 pragma_omp_clause c_kind;
33231 const char *c_name;
33232 tree prev = clauses;
33234 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33235 cp_lexer_consume_token (parser->lexer);
33237 token = cp_lexer_peek_token (parser->lexer);
33238 c_kind = cp_parser_omp_clause_name (parser);
33240 switch (c_kind)
33242 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33243 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33244 token->location);
33245 c_name = "collapse";
33246 break;
33247 case PRAGMA_OMP_CLAUSE_COPYIN:
33248 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33249 c_name = "copyin";
33250 break;
33251 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33252 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33253 clauses);
33254 c_name = "copyprivate";
33255 break;
33256 case PRAGMA_OMP_CLAUSE_DEFAULT:
33257 clauses = cp_parser_omp_clause_default (parser, clauses,
33258 token->location, false);
33259 c_name = "default";
33260 break;
33261 case PRAGMA_OMP_CLAUSE_FINAL:
33262 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33263 c_name = "final";
33264 break;
33265 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33266 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33267 clauses);
33268 c_name = "firstprivate";
33269 break;
33270 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33271 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33272 token->location);
33273 c_name = "grainsize";
33274 break;
33275 case PRAGMA_OMP_CLAUSE_HINT:
33276 clauses = cp_parser_omp_clause_hint (parser, clauses,
33277 token->location);
33278 c_name = "hint";
33279 break;
33280 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33281 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33282 token->location);
33283 c_name = "defaultmap";
33284 break;
33285 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33286 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33287 clauses);
33288 c_name = "use_device_ptr";
33289 break;
33290 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33291 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33292 clauses);
33293 c_name = "is_device_ptr";
33294 break;
33295 case PRAGMA_OMP_CLAUSE_IF:
33296 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33297 true);
33298 c_name = "if";
33299 break;
33300 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33301 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33302 clauses);
33303 c_name = "lastprivate";
33304 break;
33305 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33306 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33307 token->location);
33308 c_name = "mergeable";
33309 break;
33310 case PRAGMA_OMP_CLAUSE_NOWAIT:
33311 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33312 c_name = "nowait";
33313 break;
33314 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33315 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33316 token->location);
33317 c_name = "num_tasks";
33318 break;
33319 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33320 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33321 token->location);
33322 c_name = "num_threads";
33323 break;
33324 case PRAGMA_OMP_CLAUSE_ORDERED:
33325 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33326 token->location);
33327 c_name = "ordered";
33328 break;
33329 case PRAGMA_OMP_CLAUSE_PRIORITY:
33330 clauses = cp_parser_omp_clause_priority (parser, clauses,
33331 token->location);
33332 c_name = "priority";
33333 break;
33334 case PRAGMA_OMP_CLAUSE_PRIVATE:
33335 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33336 clauses);
33337 c_name = "private";
33338 break;
33339 case PRAGMA_OMP_CLAUSE_REDUCTION:
33340 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33341 c_name = "reduction";
33342 break;
33343 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33344 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33345 token->location);
33346 c_name = "schedule";
33347 break;
33348 case PRAGMA_OMP_CLAUSE_SHARED:
33349 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33350 clauses);
33351 c_name = "shared";
33352 break;
33353 case PRAGMA_OMP_CLAUSE_UNTIED:
33354 clauses = cp_parser_omp_clause_untied (parser, clauses,
33355 token->location);
33356 c_name = "untied";
33357 break;
33358 case PRAGMA_OMP_CLAUSE_INBRANCH:
33359 case PRAGMA_CILK_CLAUSE_MASK:
33360 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33361 clauses, token->location);
33362 c_name = "inbranch";
33363 break;
33364 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33365 case PRAGMA_CILK_CLAUSE_NOMASK:
33366 clauses = cp_parser_omp_clause_branch (parser,
33367 OMP_CLAUSE_NOTINBRANCH,
33368 clauses, token->location);
33369 c_name = "notinbranch";
33370 break;
33371 case PRAGMA_OMP_CLAUSE_PARALLEL:
33372 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33373 clauses, token->location);
33374 c_name = "parallel";
33375 if (!first)
33377 clause_not_first:
33378 error_at (token->location, "%qs must be the first clause of %qs",
33379 c_name, where);
33380 clauses = prev;
33382 break;
33383 case PRAGMA_OMP_CLAUSE_FOR:
33384 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33385 clauses, token->location);
33386 c_name = "for";
33387 if (!first)
33388 goto clause_not_first;
33389 break;
33390 case PRAGMA_OMP_CLAUSE_SECTIONS:
33391 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33392 clauses, token->location);
33393 c_name = "sections";
33394 if (!first)
33395 goto clause_not_first;
33396 break;
33397 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33398 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33399 clauses, token->location);
33400 c_name = "taskgroup";
33401 if (!first)
33402 goto clause_not_first;
33403 break;
33404 case PRAGMA_OMP_CLAUSE_LINK:
33405 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33406 c_name = "to";
33407 break;
33408 case PRAGMA_OMP_CLAUSE_TO:
33409 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33410 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33411 clauses);
33412 else
33413 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33414 c_name = "to";
33415 break;
33416 case PRAGMA_OMP_CLAUSE_FROM:
33417 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33418 c_name = "from";
33419 break;
33420 case PRAGMA_OMP_CLAUSE_UNIFORM:
33421 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33422 clauses);
33423 c_name = "uniform";
33424 break;
33425 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33426 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33427 token->location);
33428 c_name = "num_teams";
33429 break;
33430 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33431 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33432 token->location);
33433 c_name = "thread_limit";
33434 break;
33435 case PRAGMA_OMP_CLAUSE_ALIGNED:
33436 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33437 c_name = "aligned";
33438 break;
33439 case PRAGMA_OMP_CLAUSE_LINEAR:
33441 bool cilk_simd_fn = false, declare_simd = false;
33442 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33443 cilk_simd_fn = true;
33444 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33445 declare_simd = true;
33446 clauses = cp_parser_omp_clause_linear (parser, clauses,
33447 cilk_simd_fn, declare_simd);
33449 c_name = "linear";
33450 break;
33451 case PRAGMA_OMP_CLAUSE_DEPEND:
33452 clauses = cp_parser_omp_clause_depend (parser, clauses,
33453 token->location);
33454 c_name = "depend";
33455 break;
33456 case PRAGMA_OMP_CLAUSE_MAP:
33457 clauses = cp_parser_omp_clause_map (parser, clauses);
33458 c_name = "map";
33459 break;
33460 case PRAGMA_OMP_CLAUSE_DEVICE:
33461 clauses = cp_parser_omp_clause_device (parser, clauses,
33462 token->location);
33463 c_name = "device";
33464 break;
33465 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33466 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33467 token->location);
33468 c_name = "dist_schedule";
33469 break;
33470 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33471 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33472 token->location);
33473 c_name = "proc_bind";
33474 break;
33475 case PRAGMA_OMP_CLAUSE_SAFELEN:
33476 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33477 token->location);
33478 c_name = "safelen";
33479 break;
33480 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33481 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33482 token->location);
33483 c_name = "simdlen";
33484 break;
33485 case PRAGMA_OMP_CLAUSE_NOGROUP:
33486 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33487 token->location);
33488 c_name = "nogroup";
33489 break;
33490 case PRAGMA_OMP_CLAUSE_THREADS:
33491 clauses
33492 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33493 clauses, token->location);
33494 c_name = "threads";
33495 break;
33496 case PRAGMA_OMP_CLAUSE_SIMD:
33497 clauses
33498 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33499 clauses, token->location);
33500 c_name = "simd";
33501 break;
33502 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33503 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33504 c_name = "simdlen";
33505 break;
33506 default:
33507 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33508 goto saw_error;
33511 first = false;
33513 if (((mask >> c_kind) & 1) == 0)
33515 /* Remove the invalid clause(s) from the list to avoid
33516 confusing the rest of the compiler. */
33517 clauses = prev;
33518 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33521 saw_error:
33522 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33523 no reason to skip to the end. */
33524 if (!(flag_cilkplus && pragma_tok == NULL))
33525 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33526 if (finish_p)
33528 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33529 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33530 else
33531 return finish_omp_clauses (clauses, C_ORT_OMP);
33533 return clauses;
33536 /* OpenMP 2.5:
33537 structured-block:
33538 statement
33540 In practice, we're also interested in adding the statement to an
33541 outer node. So it is convenient if we work around the fact that
33542 cp_parser_statement calls add_stmt. */
33544 static unsigned
33545 cp_parser_begin_omp_structured_block (cp_parser *parser)
33547 unsigned save = parser->in_statement;
33549 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33550 This preserves the "not within loop or switch" style error messages
33551 for nonsense cases like
33552 void foo() {
33553 #pragma omp single
33554 break;
33557 if (parser->in_statement)
33558 parser->in_statement = IN_OMP_BLOCK;
33560 return save;
33563 static void
33564 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33566 parser->in_statement = save;
33569 static tree
33570 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33572 tree stmt = begin_omp_structured_block ();
33573 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33575 cp_parser_statement (parser, NULL_TREE, false, if_p);
33577 cp_parser_end_omp_structured_block (parser, save);
33578 return finish_omp_structured_block (stmt);
33581 /* OpenMP 2.5:
33582 # pragma omp atomic new-line
33583 expression-stmt
33585 expression-stmt:
33586 x binop= expr | x++ | ++x | x-- | --x
33587 binop:
33588 +, *, -, /, &, ^, |, <<, >>
33590 where x is an lvalue expression with scalar type.
33592 OpenMP 3.1:
33593 # pragma omp atomic new-line
33594 update-stmt
33596 # pragma omp atomic read new-line
33597 read-stmt
33599 # pragma omp atomic write new-line
33600 write-stmt
33602 # pragma omp atomic update new-line
33603 update-stmt
33605 # pragma omp atomic capture new-line
33606 capture-stmt
33608 # pragma omp atomic capture new-line
33609 capture-block
33611 read-stmt:
33612 v = x
33613 write-stmt:
33614 x = expr
33615 update-stmt:
33616 expression-stmt | x = x binop expr
33617 capture-stmt:
33618 v = expression-stmt
33619 capture-block:
33620 { v = x; update-stmt; } | { update-stmt; v = x; }
33622 OpenMP 4.0:
33623 update-stmt:
33624 expression-stmt | x = x binop expr | x = expr binop x
33625 capture-stmt:
33626 v = update-stmt
33627 capture-block:
33628 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33630 where x and v are lvalue expressions with scalar type. */
33632 static void
33633 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33635 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33636 tree rhs1 = NULL_TREE, orig_lhs;
33637 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33638 bool structured_block = false;
33639 bool seq_cst = false;
33641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33644 const char *p = IDENTIFIER_POINTER (id);
33646 if (!strcmp (p, "seq_cst"))
33648 seq_cst = true;
33649 cp_lexer_consume_token (parser->lexer);
33650 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33651 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33652 cp_lexer_consume_token (parser->lexer);
33655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33658 const char *p = IDENTIFIER_POINTER (id);
33660 if (!strcmp (p, "read"))
33661 code = OMP_ATOMIC_READ;
33662 else if (!strcmp (p, "write"))
33663 code = NOP_EXPR;
33664 else if (!strcmp (p, "update"))
33665 code = OMP_ATOMIC;
33666 else if (!strcmp (p, "capture"))
33667 code = OMP_ATOMIC_CAPTURE_NEW;
33668 else
33669 p = NULL;
33670 if (p)
33671 cp_lexer_consume_token (parser->lexer);
33673 if (!seq_cst)
33675 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33676 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33677 cp_lexer_consume_token (parser->lexer);
33679 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33681 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33682 const char *p = IDENTIFIER_POINTER (id);
33684 if (!strcmp (p, "seq_cst"))
33686 seq_cst = true;
33687 cp_lexer_consume_token (parser->lexer);
33691 cp_parser_require_pragma_eol (parser, pragma_tok);
33693 switch (code)
33695 case OMP_ATOMIC_READ:
33696 case NOP_EXPR: /* atomic write */
33697 v = cp_parser_unary_expression (parser);
33698 if (v == error_mark_node)
33699 goto saw_error;
33700 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33701 goto saw_error;
33702 if (code == NOP_EXPR)
33703 lhs = cp_parser_expression (parser);
33704 else
33705 lhs = cp_parser_unary_expression (parser);
33706 if (lhs == error_mark_node)
33707 goto saw_error;
33708 if (code == NOP_EXPR)
33710 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33711 opcode. */
33712 code = OMP_ATOMIC;
33713 rhs = lhs;
33714 lhs = v;
33715 v = NULL_TREE;
33717 goto done;
33718 case OMP_ATOMIC_CAPTURE_NEW:
33719 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33721 cp_lexer_consume_token (parser->lexer);
33722 structured_block = true;
33724 else
33726 v = cp_parser_unary_expression (parser);
33727 if (v == error_mark_node)
33728 goto saw_error;
33729 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33730 goto saw_error;
33732 default:
33733 break;
33736 restart:
33737 lhs = cp_parser_unary_expression (parser);
33738 orig_lhs = lhs;
33739 switch (TREE_CODE (lhs))
33741 case ERROR_MARK:
33742 goto saw_error;
33744 case POSTINCREMENT_EXPR:
33745 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33746 code = OMP_ATOMIC_CAPTURE_OLD;
33747 /* FALLTHROUGH */
33748 case PREINCREMENT_EXPR:
33749 lhs = TREE_OPERAND (lhs, 0);
33750 opcode = PLUS_EXPR;
33751 rhs = integer_one_node;
33752 break;
33754 case POSTDECREMENT_EXPR:
33755 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33756 code = OMP_ATOMIC_CAPTURE_OLD;
33757 /* FALLTHROUGH */
33758 case PREDECREMENT_EXPR:
33759 lhs = TREE_OPERAND (lhs, 0);
33760 opcode = MINUS_EXPR;
33761 rhs = integer_one_node;
33762 break;
33764 case COMPOUND_EXPR:
33765 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33766 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33767 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33768 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33769 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33770 (TREE_OPERAND (lhs, 1), 0), 0)))
33771 == BOOLEAN_TYPE)
33772 /* Undo effects of boolean_increment for post {in,de}crement. */
33773 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33774 /* FALLTHRU */
33775 case MODIFY_EXPR:
33776 if (TREE_CODE (lhs) == MODIFY_EXPR
33777 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33779 /* Undo effects of boolean_increment. */
33780 if (integer_onep (TREE_OPERAND (lhs, 1)))
33782 /* This is pre or post increment. */
33783 rhs = TREE_OPERAND (lhs, 1);
33784 lhs = TREE_OPERAND (lhs, 0);
33785 opcode = NOP_EXPR;
33786 if (code == OMP_ATOMIC_CAPTURE_NEW
33787 && !structured_block
33788 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33789 code = OMP_ATOMIC_CAPTURE_OLD;
33790 break;
33793 /* FALLTHRU */
33794 default:
33795 switch (cp_lexer_peek_token (parser->lexer)->type)
33797 case CPP_MULT_EQ:
33798 opcode = MULT_EXPR;
33799 break;
33800 case CPP_DIV_EQ:
33801 opcode = TRUNC_DIV_EXPR;
33802 break;
33803 case CPP_PLUS_EQ:
33804 opcode = PLUS_EXPR;
33805 break;
33806 case CPP_MINUS_EQ:
33807 opcode = MINUS_EXPR;
33808 break;
33809 case CPP_LSHIFT_EQ:
33810 opcode = LSHIFT_EXPR;
33811 break;
33812 case CPP_RSHIFT_EQ:
33813 opcode = RSHIFT_EXPR;
33814 break;
33815 case CPP_AND_EQ:
33816 opcode = BIT_AND_EXPR;
33817 break;
33818 case CPP_OR_EQ:
33819 opcode = BIT_IOR_EXPR;
33820 break;
33821 case CPP_XOR_EQ:
33822 opcode = BIT_XOR_EXPR;
33823 break;
33824 case CPP_EQ:
33825 enum cp_parser_prec oprec;
33826 cp_token *token;
33827 cp_lexer_consume_token (parser->lexer);
33828 cp_parser_parse_tentatively (parser);
33829 rhs1 = cp_parser_simple_cast_expression (parser);
33830 if (rhs1 == error_mark_node)
33832 cp_parser_abort_tentative_parse (parser);
33833 cp_parser_simple_cast_expression (parser);
33834 goto saw_error;
33836 token = cp_lexer_peek_token (parser->lexer);
33837 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33839 cp_parser_abort_tentative_parse (parser);
33840 cp_parser_parse_tentatively (parser);
33841 rhs = cp_parser_binary_expression (parser, false, true,
33842 PREC_NOT_OPERATOR, NULL);
33843 if (rhs == error_mark_node)
33845 cp_parser_abort_tentative_parse (parser);
33846 cp_parser_binary_expression (parser, false, true,
33847 PREC_NOT_OPERATOR, NULL);
33848 goto saw_error;
33850 switch (TREE_CODE (rhs))
33852 case MULT_EXPR:
33853 case TRUNC_DIV_EXPR:
33854 case RDIV_EXPR:
33855 case PLUS_EXPR:
33856 case MINUS_EXPR:
33857 case LSHIFT_EXPR:
33858 case RSHIFT_EXPR:
33859 case BIT_AND_EXPR:
33860 case BIT_IOR_EXPR:
33861 case BIT_XOR_EXPR:
33862 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33864 if (cp_parser_parse_definitely (parser))
33866 opcode = TREE_CODE (rhs);
33867 rhs1 = TREE_OPERAND (rhs, 0);
33868 rhs = TREE_OPERAND (rhs, 1);
33869 goto stmt_done;
33871 else
33872 goto saw_error;
33874 break;
33875 default:
33876 break;
33878 cp_parser_abort_tentative_parse (parser);
33879 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33881 rhs = cp_parser_expression (parser);
33882 if (rhs == error_mark_node)
33883 goto saw_error;
33884 opcode = NOP_EXPR;
33885 rhs1 = NULL_TREE;
33886 goto stmt_done;
33888 cp_parser_error (parser,
33889 "invalid form of %<#pragma omp atomic%>");
33890 goto saw_error;
33892 if (!cp_parser_parse_definitely (parser))
33893 goto saw_error;
33894 switch (token->type)
33896 case CPP_SEMICOLON:
33897 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33899 code = OMP_ATOMIC_CAPTURE_OLD;
33900 v = lhs;
33901 lhs = NULL_TREE;
33902 lhs1 = rhs1;
33903 rhs1 = NULL_TREE;
33904 cp_lexer_consume_token (parser->lexer);
33905 goto restart;
33907 else if (structured_block)
33909 opcode = NOP_EXPR;
33910 rhs = rhs1;
33911 rhs1 = NULL_TREE;
33912 goto stmt_done;
33914 cp_parser_error (parser,
33915 "invalid form of %<#pragma omp atomic%>");
33916 goto saw_error;
33917 case CPP_MULT:
33918 opcode = MULT_EXPR;
33919 break;
33920 case CPP_DIV:
33921 opcode = TRUNC_DIV_EXPR;
33922 break;
33923 case CPP_PLUS:
33924 opcode = PLUS_EXPR;
33925 break;
33926 case CPP_MINUS:
33927 opcode = MINUS_EXPR;
33928 break;
33929 case CPP_LSHIFT:
33930 opcode = LSHIFT_EXPR;
33931 break;
33932 case CPP_RSHIFT:
33933 opcode = RSHIFT_EXPR;
33934 break;
33935 case CPP_AND:
33936 opcode = BIT_AND_EXPR;
33937 break;
33938 case CPP_OR:
33939 opcode = BIT_IOR_EXPR;
33940 break;
33941 case CPP_XOR:
33942 opcode = BIT_XOR_EXPR;
33943 break;
33944 default:
33945 cp_parser_error (parser,
33946 "invalid operator for %<#pragma omp atomic%>");
33947 goto saw_error;
33949 oprec = TOKEN_PRECEDENCE (token);
33950 gcc_assert (oprec != PREC_NOT_OPERATOR);
33951 if (commutative_tree_code (opcode))
33952 oprec = (enum cp_parser_prec) (oprec - 1);
33953 cp_lexer_consume_token (parser->lexer);
33954 rhs = cp_parser_binary_expression (parser, false, false,
33955 oprec, NULL);
33956 if (rhs == error_mark_node)
33957 goto saw_error;
33958 goto stmt_done;
33959 /* FALLTHROUGH */
33960 default:
33961 cp_parser_error (parser,
33962 "invalid operator for %<#pragma omp atomic%>");
33963 goto saw_error;
33965 cp_lexer_consume_token (parser->lexer);
33967 rhs = cp_parser_expression (parser);
33968 if (rhs == error_mark_node)
33969 goto saw_error;
33970 break;
33972 stmt_done:
33973 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33975 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33976 goto saw_error;
33977 v = cp_parser_unary_expression (parser);
33978 if (v == error_mark_node)
33979 goto saw_error;
33980 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33981 goto saw_error;
33982 lhs1 = cp_parser_unary_expression (parser);
33983 if (lhs1 == error_mark_node)
33984 goto saw_error;
33986 if (structured_block)
33988 cp_parser_consume_semicolon_at_end_of_statement (parser);
33989 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33991 done:
33992 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33993 if (!structured_block)
33994 cp_parser_consume_semicolon_at_end_of_statement (parser);
33995 return;
33997 saw_error:
33998 cp_parser_skip_to_end_of_block_or_statement (parser);
33999 if (structured_block)
34001 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34002 cp_lexer_consume_token (parser->lexer);
34003 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34005 cp_parser_skip_to_end_of_block_or_statement (parser);
34006 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34007 cp_lexer_consume_token (parser->lexer);
34013 /* OpenMP 2.5:
34014 # pragma omp barrier new-line */
34016 static void
34017 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34019 cp_parser_require_pragma_eol (parser, pragma_tok);
34020 finish_omp_barrier ();
34023 /* OpenMP 2.5:
34024 # pragma omp critical [(name)] new-line
34025 structured-block
34027 OpenMP 4.5:
34028 # pragma omp critical [(name) [hint(expression)]] new-line
34029 structured-block */
34031 #define OMP_CRITICAL_CLAUSE_MASK \
34032 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34034 static tree
34035 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34037 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34039 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34041 cp_lexer_consume_token (parser->lexer);
34043 name = cp_parser_identifier (parser);
34045 if (name == error_mark_node
34046 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34047 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34048 /*or_comma=*/false,
34049 /*consume_paren=*/true);
34050 if (name == error_mark_node)
34051 name = NULL;
34053 clauses = cp_parser_omp_all_clauses (parser,
34054 OMP_CRITICAL_CLAUSE_MASK,
34055 "#pragma omp critical", pragma_tok);
34057 else
34058 cp_parser_require_pragma_eol (parser, pragma_tok);
34060 stmt = cp_parser_omp_structured_block (parser, if_p);
34061 return c_finish_omp_critical (input_location, stmt, name, clauses);
34064 /* OpenMP 2.5:
34065 # pragma omp flush flush-vars[opt] new-line
34067 flush-vars:
34068 ( variable-list ) */
34070 static void
34071 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34073 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34074 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34075 cp_parser_require_pragma_eol (parser, pragma_tok);
34077 finish_omp_flush ();
34080 /* Helper function, to parse omp for increment expression. */
34082 static tree
34083 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34085 tree cond = cp_parser_binary_expression (parser, false, true,
34086 PREC_NOT_OPERATOR, NULL);
34087 if (cond == error_mark_node
34088 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34090 cp_parser_skip_to_end_of_statement (parser);
34091 return error_mark_node;
34094 switch (TREE_CODE (cond))
34096 case GT_EXPR:
34097 case GE_EXPR:
34098 case LT_EXPR:
34099 case LE_EXPR:
34100 break;
34101 case NE_EXPR:
34102 if (code == CILK_SIMD || code == CILK_FOR)
34103 break;
34104 /* Fall through: OpenMP disallows NE_EXPR. */
34105 gcc_fallthrough ();
34106 default:
34107 return error_mark_node;
34110 /* If decl is an iterator, preserve LHS and RHS of the relational
34111 expr until finish_omp_for. */
34112 if (decl
34113 && (type_dependent_expression_p (decl)
34114 || CLASS_TYPE_P (TREE_TYPE (decl))))
34115 return cond;
34117 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34118 TREE_CODE (cond),
34119 TREE_OPERAND (cond, 0), ERROR_MARK,
34120 TREE_OPERAND (cond, 1), ERROR_MARK,
34121 /*overload=*/NULL, tf_warning_or_error);
34124 /* Helper function, to parse omp for increment expression. */
34126 static tree
34127 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34129 cp_token *token = cp_lexer_peek_token (parser->lexer);
34130 enum tree_code op;
34131 tree lhs, rhs;
34132 cp_id_kind idk;
34133 bool decl_first;
34135 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34137 op = (token->type == CPP_PLUS_PLUS
34138 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34139 cp_lexer_consume_token (parser->lexer);
34140 lhs = cp_parser_simple_cast_expression (parser);
34141 if (lhs != decl
34142 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34143 return error_mark_node;
34144 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34147 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34148 if (lhs != decl
34149 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34150 return error_mark_node;
34152 token = cp_lexer_peek_token (parser->lexer);
34153 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34155 op = (token->type == CPP_PLUS_PLUS
34156 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34157 cp_lexer_consume_token (parser->lexer);
34158 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34161 op = cp_parser_assignment_operator_opt (parser);
34162 if (op == ERROR_MARK)
34163 return error_mark_node;
34165 if (op != NOP_EXPR)
34167 rhs = cp_parser_assignment_expression (parser);
34168 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34169 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34172 lhs = cp_parser_binary_expression (parser, false, false,
34173 PREC_ADDITIVE_EXPRESSION, NULL);
34174 token = cp_lexer_peek_token (parser->lexer);
34175 decl_first = (lhs == decl
34176 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34177 if (decl_first)
34178 lhs = NULL_TREE;
34179 if (token->type != CPP_PLUS
34180 && token->type != CPP_MINUS)
34181 return error_mark_node;
34185 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34186 cp_lexer_consume_token (parser->lexer);
34187 rhs = cp_parser_binary_expression (parser, false, false,
34188 PREC_ADDITIVE_EXPRESSION, NULL);
34189 token = cp_lexer_peek_token (parser->lexer);
34190 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34192 if (lhs == NULL_TREE)
34194 if (op == PLUS_EXPR)
34195 lhs = rhs;
34196 else
34197 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34198 tf_warning_or_error);
34200 else
34201 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34202 ERROR_MARK, NULL, tf_warning_or_error);
34205 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34207 if (!decl_first)
34209 if ((rhs != decl
34210 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34211 || op == MINUS_EXPR)
34212 return error_mark_node;
34213 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34215 else
34216 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34218 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34221 /* Parse the initialization statement of either an OpenMP for loop or
34222 a Cilk Plus for loop.
34224 Return true if the resulting construct should have an
34225 OMP_CLAUSE_PRIVATE added to it. */
34227 static tree
34228 cp_parser_omp_for_loop_init (cp_parser *parser,
34229 enum tree_code code,
34230 tree &this_pre_body,
34231 vec<tree, va_gc> *for_block,
34232 tree &init,
34233 tree &orig_init,
34234 tree &decl,
34235 tree &real_decl)
34237 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34238 return NULL_TREE;
34240 tree add_private_clause = NULL_TREE;
34242 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34244 init-expr:
34245 var = lb
34246 integer-type var = lb
34247 random-access-iterator-type var = lb
34248 pointer-type var = lb
34250 cp_decl_specifier_seq type_specifiers;
34252 /* First, try to parse as an initialized declaration. See
34253 cp_parser_condition, from whence the bulk of this is copied. */
34255 cp_parser_parse_tentatively (parser);
34256 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34257 /*is_trailing_return=*/false,
34258 &type_specifiers);
34259 if (cp_parser_parse_definitely (parser))
34261 /* If parsing a type specifier seq succeeded, then this
34262 MUST be a initialized declaration. */
34263 tree asm_specification, attributes;
34264 cp_declarator *declarator;
34266 declarator = cp_parser_declarator (parser,
34267 CP_PARSER_DECLARATOR_NAMED,
34268 /*ctor_dtor_or_conv_p=*/NULL,
34269 /*parenthesized_p=*/NULL,
34270 /*member_p=*/false,
34271 /*friend_p=*/false);
34272 attributes = cp_parser_attributes_opt (parser);
34273 asm_specification = cp_parser_asm_specification_opt (parser);
34275 if (declarator == cp_error_declarator)
34276 cp_parser_skip_to_end_of_statement (parser);
34278 else
34280 tree pushed_scope, auto_node;
34282 decl = start_decl (declarator, &type_specifiers,
34283 SD_INITIALIZED, attributes,
34284 /*prefix_attributes=*/NULL_TREE,
34285 &pushed_scope);
34287 auto_node = type_uses_auto (TREE_TYPE (decl));
34288 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34290 if (cp_lexer_next_token_is (parser->lexer,
34291 CPP_OPEN_PAREN))
34293 if (code != CILK_SIMD && code != CILK_FOR)
34294 error ("parenthesized initialization is not allowed in "
34295 "OpenMP %<for%> loop");
34296 else
34297 error ("parenthesized initialization is "
34298 "not allowed in for-loop");
34300 else
34301 /* Trigger an error. */
34302 cp_parser_require (parser, CPP_EQ, RT_EQ);
34304 init = error_mark_node;
34305 cp_parser_skip_to_end_of_statement (parser);
34307 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34308 || type_dependent_expression_p (decl)
34309 || auto_node)
34311 bool is_direct_init, is_non_constant_init;
34313 init = cp_parser_initializer (parser,
34314 &is_direct_init,
34315 &is_non_constant_init);
34317 if (auto_node)
34319 TREE_TYPE (decl)
34320 = do_auto_deduction (TREE_TYPE (decl), init,
34321 auto_node);
34323 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34324 && !type_dependent_expression_p (decl))
34325 goto non_class;
34328 cp_finish_decl (decl, init, !is_non_constant_init,
34329 asm_specification,
34330 LOOKUP_ONLYCONVERTING);
34331 orig_init = init;
34332 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34334 vec_safe_push (for_block, this_pre_body);
34335 init = NULL_TREE;
34337 else
34339 init = pop_stmt_list (this_pre_body);
34340 if (init && TREE_CODE (init) == STATEMENT_LIST)
34342 tree_stmt_iterator i = tsi_start (init);
34343 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34344 while (!tsi_end_p (i))
34346 tree t = tsi_stmt (i);
34347 if (TREE_CODE (t) == DECL_EXPR
34348 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34350 tsi_delink (&i);
34351 vec_safe_push (for_block, t);
34352 continue;
34354 break;
34356 if (tsi_one_before_end_p (i))
34358 tree t = tsi_stmt (i);
34359 tsi_delink (&i);
34360 free_stmt_list (init);
34361 init = t;
34365 this_pre_body = NULL_TREE;
34367 else
34369 /* Consume '='. */
34370 cp_lexer_consume_token (parser->lexer);
34371 init = cp_parser_assignment_expression (parser);
34373 non_class:
34374 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34375 init = error_mark_node;
34376 else
34377 cp_finish_decl (decl, NULL_TREE,
34378 /*init_const_expr_p=*/false,
34379 asm_specification,
34380 LOOKUP_ONLYCONVERTING);
34383 if (pushed_scope)
34384 pop_scope (pushed_scope);
34387 else
34389 cp_id_kind idk;
34390 /* If parsing a type specifier sequence failed, then
34391 this MUST be a simple expression. */
34392 if (code == CILK_FOR)
34393 error ("%<_Cilk_for%> allows expression instead of declaration only "
34394 "in C, not in C++");
34395 cp_parser_parse_tentatively (parser);
34396 decl = cp_parser_primary_expression (parser, false, false,
34397 false, &idk);
34398 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34399 if (!cp_parser_error_occurred (parser)
34400 && decl
34401 && (TREE_CODE (decl) == COMPONENT_REF
34402 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34404 cp_parser_abort_tentative_parse (parser);
34405 cp_parser_parse_tentatively (parser);
34406 cp_token *token = cp_lexer_peek_token (parser->lexer);
34407 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34408 /*check_dependency_p=*/true,
34409 /*template_p=*/NULL,
34410 /*declarator_p=*/false,
34411 /*optional_p=*/false);
34412 if (name != error_mark_node
34413 && last_tok == cp_lexer_peek_token (parser->lexer))
34415 decl = cp_parser_lookup_name_simple (parser, name,
34416 token->location);
34417 if (TREE_CODE (decl) == FIELD_DECL)
34418 add_private_clause = omp_privatize_field (decl, false);
34420 cp_parser_abort_tentative_parse (parser);
34421 cp_parser_parse_tentatively (parser);
34422 decl = cp_parser_primary_expression (parser, false, false,
34423 false, &idk);
34425 if (!cp_parser_error_occurred (parser)
34426 && decl
34427 && DECL_P (decl)
34428 && CLASS_TYPE_P (TREE_TYPE (decl)))
34430 tree rhs;
34432 cp_parser_parse_definitely (parser);
34433 cp_parser_require (parser, CPP_EQ, RT_EQ);
34434 rhs = cp_parser_assignment_expression (parser);
34435 orig_init = rhs;
34436 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34437 decl, NOP_EXPR,
34438 rhs,
34439 tf_warning_or_error));
34440 if (!add_private_clause)
34441 add_private_clause = decl;
34443 else
34445 decl = NULL;
34446 cp_parser_abort_tentative_parse (parser);
34447 init = cp_parser_expression (parser);
34448 if (init)
34450 if (TREE_CODE (init) == MODIFY_EXPR
34451 || TREE_CODE (init) == MODOP_EXPR)
34452 real_decl = TREE_OPERAND (init, 0);
34456 return add_private_clause;
34459 /* Parse the restricted form of the for statement allowed by OpenMP. */
34461 static tree
34462 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34463 tree *cclauses, bool *if_p)
34465 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34466 tree real_decl, initv, condv, incrv, declv;
34467 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34468 location_t loc_first;
34469 bool collapse_err = false;
34470 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34471 vec<tree, va_gc> *for_block = make_tree_vector ();
34472 auto_vec<tree, 4> orig_inits;
34473 bool tiling = false;
34475 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34476 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34477 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34478 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34480 tiling = true;
34481 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34483 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34484 && OMP_CLAUSE_ORDERED_EXPR (cl))
34486 ordered_cl = cl;
34487 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34490 if (ordered && ordered < collapse)
34492 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34493 "%<ordered%> clause parameter is less than %<collapse%>");
34494 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34495 = build_int_cst (NULL_TREE, collapse);
34496 ordered = collapse;
34498 if (ordered)
34500 for (tree *pc = &clauses; *pc; )
34501 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34503 error_at (OMP_CLAUSE_LOCATION (*pc),
34504 "%<linear%> clause may not be specified together "
34505 "with %<ordered%> clause with a parameter");
34506 *pc = OMP_CLAUSE_CHAIN (*pc);
34508 else
34509 pc = &OMP_CLAUSE_CHAIN (*pc);
34512 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34513 count = ordered ? ordered : collapse;
34515 declv = make_tree_vec (count);
34516 initv = make_tree_vec (count);
34517 condv = make_tree_vec (count);
34518 incrv = make_tree_vec (count);
34520 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34522 for (i = 0; i < count; i++)
34524 int bracecount = 0;
34525 tree add_private_clause = NULL_TREE;
34526 location_t loc;
34528 if (code != CILK_FOR
34529 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34531 if (!collapse_err)
34532 cp_parser_error (parser, "for statement expected");
34533 return NULL;
34535 if (code == CILK_FOR
34536 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34538 if (!collapse_err)
34539 cp_parser_error (parser, "_Cilk_for statement expected");
34540 return NULL;
34542 loc = cp_lexer_consume_token (parser->lexer)->location;
34544 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34545 return NULL;
34547 init = orig_init = decl = real_decl = NULL;
34548 this_pre_body = push_stmt_list ();
34550 add_private_clause
34551 = cp_parser_omp_for_loop_init (parser, code,
34552 this_pre_body, for_block,
34553 init, orig_init, decl, real_decl);
34555 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34556 if (this_pre_body)
34558 this_pre_body = pop_stmt_list (this_pre_body);
34559 if (pre_body)
34561 tree t = pre_body;
34562 pre_body = push_stmt_list ();
34563 add_stmt (t);
34564 add_stmt (this_pre_body);
34565 pre_body = pop_stmt_list (pre_body);
34567 else
34568 pre_body = this_pre_body;
34571 if (decl)
34572 real_decl = decl;
34573 if (cclauses != NULL
34574 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34575 && real_decl != NULL_TREE)
34577 tree *c;
34578 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34579 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34580 && OMP_CLAUSE_DECL (*c) == real_decl)
34582 error_at (loc, "iteration variable %qD"
34583 " should not be firstprivate", real_decl);
34584 *c = OMP_CLAUSE_CHAIN (*c);
34586 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34587 && OMP_CLAUSE_DECL (*c) == real_decl)
34589 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34590 tree l = *c;
34591 *c = OMP_CLAUSE_CHAIN (*c);
34592 if (code == OMP_SIMD)
34594 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34595 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34597 else
34599 OMP_CLAUSE_CHAIN (l) = clauses;
34600 clauses = l;
34602 add_private_clause = NULL_TREE;
34604 else
34606 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34607 && OMP_CLAUSE_DECL (*c) == real_decl)
34608 add_private_clause = NULL_TREE;
34609 c = &OMP_CLAUSE_CHAIN (*c);
34613 if (add_private_clause)
34615 tree c;
34616 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34618 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34619 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34620 && OMP_CLAUSE_DECL (c) == decl)
34621 break;
34622 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34623 && OMP_CLAUSE_DECL (c) == decl)
34624 error_at (loc, "iteration variable %qD "
34625 "should not be firstprivate",
34626 decl);
34627 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34628 && OMP_CLAUSE_DECL (c) == decl)
34629 error_at (loc, "iteration variable %qD should not be reduction",
34630 decl);
34632 if (c == NULL)
34634 if (code != OMP_SIMD)
34635 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34636 else if (collapse == 1)
34637 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34638 else
34639 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34640 OMP_CLAUSE_DECL (c) = add_private_clause;
34641 c = finish_omp_clauses (c, C_ORT_OMP);
34642 if (c)
34644 OMP_CLAUSE_CHAIN (c) = clauses;
34645 clauses = c;
34646 /* For linear, signal that we need to fill up
34647 the so far unknown linear step. */
34648 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34649 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34654 cond = NULL;
34655 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34656 cond = cp_parser_omp_for_cond (parser, decl, code);
34657 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34659 incr = NULL;
34660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34662 /* If decl is an iterator, preserve the operator on decl
34663 until finish_omp_for. */
34664 if (real_decl
34665 && ((processing_template_decl
34666 && (TREE_TYPE (real_decl) == NULL_TREE
34667 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34668 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34669 incr = cp_parser_omp_for_incr (parser, real_decl);
34670 else
34671 incr = cp_parser_expression (parser);
34672 if (!EXPR_HAS_LOCATION (incr))
34673 protected_set_expr_location (incr, input_location);
34676 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34678 /*or_comma=*/false,
34679 /*consume_paren=*/true);
34681 TREE_VEC_ELT (declv, i) = decl;
34682 TREE_VEC_ELT (initv, i) = init;
34683 TREE_VEC_ELT (condv, i) = cond;
34684 TREE_VEC_ELT (incrv, i) = incr;
34685 if (orig_init)
34687 orig_inits.safe_grow_cleared (i + 1);
34688 orig_inits[i] = orig_init;
34691 if (i == count - 1)
34692 break;
34694 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34695 in between the collapsed for loops to be still considered perfectly
34696 nested. Hopefully the final version clarifies this.
34697 For now handle (multiple) {'s and empty statements. */
34698 cp_parser_parse_tentatively (parser);
34699 for (;;)
34701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34702 break;
34703 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34705 cp_lexer_consume_token (parser->lexer);
34706 bracecount++;
34708 else if (bracecount
34709 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34710 cp_lexer_consume_token (parser->lexer);
34711 else
34713 loc = cp_lexer_peek_token (parser->lexer)->location;
34714 error_at (loc, "not enough for loops to collapse");
34715 collapse_err = true;
34716 cp_parser_abort_tentative_parse (parser);
34717 declv = NULL_TREE;
34718 break;
34722 if (declv)
34724 cp_parser_parse_definitely (parser);
34725 nbraces += bracecount;
34729 if (nbraces)
34730 if_p = NULL;
34732 /* Note that we saved the original contents of this flag when we entered
34733 the structured block, and so we don't need to re-save it here. */
34734 if (code == CILK_SIMD || code == CILK_FOR)
34735 parser->in_statement = IN_CILK_SIMD_FOR;
34736 else
34737 parser->in_statement = IN_OMP_FOR;
34739 /* Note that the grammar doesn't call for a structured block here,
34740 though the loop as a whole is a structured block. */
34741 body = push_stmt_list ();
34742 cp_parser_statement (parser, NULL_TREE, false, if_p);
34743 body = pop_stmt_list (body);
34745 if (declv == NULL_TREE)
34746 ret = NULL_TREE;
34747 else
34748 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34749 body, pre_body, &orig_inits, clauses);
34751 while (nbraces)
34753 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34755 cp_lexer_consume_token (parser->lexer);
34756 nbraces--;
34758 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34759 cp_lexer_consume_token (parser->lexer);
34760 else
34762 if (!collapse_err)
34764 error_at (cp_lexer_peek_token (parser->lexer)->location,
34765 "collapsed loops not perfectly nested");
34767 collapse_err = true;
34768 cp_parser_statement_seq_opt (parser, NULL);
34769 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34770 break;
34774 while (!for_block->is_empty ())
34776 tree t = for_block->pop ();
34777 if (TREE_CODE (t) == STATEMENT_LIST)
34778 add_stmt (pop_stmt_list (t));
34779 else
34780 add_stmt (t);
34782 release_tree_vector (for_block);
34784 return ret;
34787 /* Helper function for OpenMP parsing, split clauses and call
34788 finish_omp_clauses on each of the set of clauses afterwards. */
34790 static void
34791 cp_omp_split_clauses (location_t loc, enum tree_code code,
34792 omp_clause_mask mask, tree clauses, tree *cclauses)
34794 int i;
34795 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34796 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34797 if (cclauses[i])
34798 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34801 /* OpenMP 4.0:
34802 #pragma omp simd simd-clause[optseq] new-line
34803 for-loop */
34805 #define OMP_SIMD_CLAUSE_MASK \
34806 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34808 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34809 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34810 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34811 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34812 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34813 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34815 static tree
34816 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34817 char *p_name, omp_clause_mask mask, tree *cclauses,
34818 bool *if_p)
34820 tree clauses, sb, ret;
34821 unsigned int save;
34822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34824 strcat (p_name, " simd");
34825 mask |= OMP_SIMD_CLAUSE_MASK;
34827 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34828 cclauses == NULL);
34829 if (cclauses)
34831 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34832 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34833 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34834 OMP_CLAUSE_ORDERED);
34835 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34837 error_at (OMP_CLAUSE_LOCATION (c),
34838 "%<ordered%> clause with parameter may not be specified "
34839 "on %qs construct", p_name);
34840 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34844 sb = begin_omp_structured_block ();
34845 save = cp_parser_begin_omp_structured_block (parser);
34847 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34849 cp_parser_end_omp_structured_block (parser, save);
34850 add_stmt (finish_omp_structured_block (sb));
34852 return ret;
34855 /* OpenMP 2.5:
34856 #pragma omp for for-clause[optseq] new-line
34857 for-loop
34859 OpenMP 4.0:
34860 #pragma omp for simd for-simd-clause[optseq] new-line
34861 for-loop */
34863 #define OMP_FOR_CLAUSE_MASK \
34864 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34866 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34867 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34868 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34874 static tree
34875 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34876 char *p_name, omp_clause_mask mask, tree *cclauses,
34877 bool *if_p)
34879 tree clauses, sb, ret;
34880 unsigned int save;
34881 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34883 strcat (p_name, " for");
34884 mask |= OMP_FOR_CLAUSE_MASK;
34885 /* parallel for{, simd} disallows nowait clause, but for
34886 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34887 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34888 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34889 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34890 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34891 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34893 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34895 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34896 const char *p = IDENTIFIER_POINTER (id);
34898 if (strcmp (p, "simd") == 0)
34900 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34901 if (cclauses == NULL)
34902 cclauses = cclauses_buf;
34904 cp_lexer_consume_token (parser->lexer);
34905 if (!flag_openmp) /* flag_openmp_simd */
34906 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34907 cclauses, if_p);
34908 sb = begin_omp_structured_block ();
34909 save = cp_parser_begin_omp_structured_block (parser);
34910 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34911 cclauses, if_p);
34912 cp_parser_end_omp_structured_block (parser, save);
34913 tree body = finish_omp_structured_block (sb);
34914 if (ret == NULL)
34915 return ret;
34916 ret = make_node (OMP_FOR);
34917 TREE_TYPE (ret) = void_type_node;
34918 OMP_FOR_BODY (ret) = body;
34919 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34920 SET_EXPR_LOCATION (ret, loc);
34921 add_stmt (ret);
34922 return ret;
34925 if (!flag_openmp) /* flag_openmp_simd */
34927 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34928 return NULL_TREE;
34931 /* Composite distribute parallel for disallows linear clause. */
34932 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34933 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34935 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34936 cclauses == NULL);
34937 if (cclauses)
34939 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34940 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34943 sb = begin_omp_structured_block ();
34944 save = cp_parser_begin_omp_structured_block (parser);
34946 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34948 cp_parser_end_omp_structured_block (parser, save);
34949 add_stmt (finish_omp_structured_block (sb));
34951 return ret;
34954 /* OpenMP 2.5:
34955 # pragma omp master new-line
34956 structured-block */
34958 static tree
34959 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34961 cp_parser_require_pragma_eol (parser, pragma_tok);
34962 return c_finish_omp_master (input_location,
34963 cp_parser_omp_structured_block (parser, if_p));
34966 /* OpenMP 2.5:
34967 # pragma omp ordered new-line
34968 structured-block
34970 OpenMP 4.5:
34971 # pragma omp ordered ordered-clauses new-line
34972 structured-block */
34974 #define OMP_ORDERED_CLAUSE_MASK \
34975 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34976 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34978 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34979 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34981 static bool
34982 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34983 enum pragma_context context, bool *if_p)
34985 location_t loc = pragma_tok->location;
34987 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34989 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34990 const char *p = IDENTIFIER_POINTER (id);
34992 if (strcmp (p, "depend") == 0)
34994 if (context == pragma_stmt)
34996 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34997 "%<depend%> clause may only be used in compound "
34998 "statements");
34999 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35000 return false;
35002 tree clauses
35003 = cp_parser_omp_all_clauses (parser,
35004 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35005 "#pragma omp ordered", pragma_tok);
35006 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35007 return false;
35011 tree clauses
35012 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35013 "#pragma omp ordered", pragma_tok);
35014 c_finish_omp_ordered (loc, clauses,
35015 cp_parser_omp_structured_block (parser, if_p));
35016 return true;
35019 /* OpenMP 2.5:
35021 section-scope:
35022 { section-sequence }
35024 section-sequence:
35025 section-directive[opt] structured-block
35026 section-sequence section-directive structured-block */
35028 static tree
35029 cp_parser_omp_sections_scope (cp_parser *parser)
35031 tree stmt, substmt;
35032 bool error_suppress = false;
35033 cp_token *tok;
35035 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35036 return NULL_TREE;
35038 stmt = push_stmt_list ();
35040 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35041 != PRAGMA_OMP_SECTION)
35043 substmt = cp_parser_omp_structured_block (parser, NULL);
35044 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35045 add_stmt (substmt);
35048 while (1)
35050 tok = cp_lexer_peek_token (parser->lexer);
35051 if (tok->type == CPP_CLOSE_BRACE)
35052 break;
35053 if (tok->type == CPP_EOF)
35054 break;
35056 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35058 cp_lexer_consume_token (parser->lexer);
35059 cp_parser_require_pragma_eol (parser, tok);
35060 error_suppress = false;
35062 else if (!error_suppress)
35064 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35065 error_suppress = true;
35068 substmt = cp_parser_omp_structured_block (parser, NULL);
35069 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35070 add_stmt (substmt);
35072 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35074 substmt = pop_stmt_list (stmt);
35076 stmt = make_node (OMP_SECTIONS);
35077 TREE_TYPE (stmt) = void_type_node;
35078 OMP_SECTIONS_BODY (stmt) = substmt;
35080 add_stmt (stmt);
35081 return stmt;
35084 /* OpenMP 2.5:
35085 # pragma omp sections sections-clause[optseq] newline
35086 sections-scope */
35088 #define OMP_SECTIONS_CLAUSE_MASK \
35089 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35090 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35095 static tree
35096 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35097 char *p_name, omp_clause_mask mask, tree *cclauses)
35099 tree clauses, ret;
35100 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35102 strcat (p_name, " sections");
35103 mask |= OMP_SECTIONS_CLAUSE_MASK;
35104 if (cclauses)
35105 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35107 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35108 cclauses == NULL);
35109 if (cclauses)
35111 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35112 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35115 ret = cp_parser_omp_sections_scope (parser);
35116 if (ret)
35117 OMP_SECTIONS_CLAUSES (ret) = clauses;
35119 return ret;
35122 /* OpenMP 2.5:
35123 # pragma omp parallel parallel-clause[optseq] new-line
35124 structured-block
35125 # pragma omp parallel for parallel-for-clause[optseq] new-line
35126 structured-block
35127 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35128 structured-block
35130 OpenMP 4.0:
35131 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35132 structured-block */
35134 #define OMP_PARALLEL_CLAUSE_MASK \
35135 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35139 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35140 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35141 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35142 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35143 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35145 static tree
35146 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35147 char *p_name, omp_clause_mask mask, tree *cclauses,
35148 bool *if_p)
35150 tree stmt, clauses, block;
35151 unsigned int save;
35152 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35154 strcat (p_name, " parallel");
35155 mask |= OMP_PARALLEL_CLAUSE_MASK;
35156 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35157 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35158 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35159 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35161 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35163 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35164 if (cclauses == NULL)
35165 cclauses = cclauses_buf;
35167 cp_lexer_consume_token (parser->lexer);
35168 if (!flag_openmp) /* flag_openmp_simd */
35169 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35170 if_p);
35171 block = begin_omp_parallel ();
35172 save = cp_parser_begin_omp_structured_block (parser);
35173 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35174 if_p);
35175 cp_parser_end_omp_structured_block (parser, save);
35176 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35177 block);
35178 if (ret == NULL_TREE)
35179 return ret;
35180 OMP_PARALLEL_COMBINED (stmt) = 1;
35181 return stmt;
35183 /* When combined with distribute, parallel has to be followed by for.
35184 #pragma omp target parallel is allowed though. */
35185 else if (cclauses
35186 && (mask & (OMP_CLAUSE_MASK_1
35187 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35189 error_at (loc, "expected %<for%> after %qs", p_name);
35190 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35191 return NULL_TREE;
35193 else if (!flag_openmp) /* flag_openmp_simd */
35195 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35196 return NULL_TREE;
35198 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35200 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35201 const char *p = IDENTIFIER_POINTER (id);
35202 if (strcmp (p, "sections") == 0)
35204 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35205 cclauses = cclauses_buf;
35207 cp_lexer_consume_token (parser->lexer);
35208 block = begin_omp_parallel ();
35209 save = cp_parser_begin_omp_structured_block (parser);
35210 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35211 cp_parser_end_omp_structured_block (parser, save);
35212 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35213 block);
35214 OMP_PARALLEL_COMBINED (stmt) = 1;
35215 return stmt;
35219 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35220 cclauses == NULL);
35221 if (cclauses)
35223 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35224 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35227 block = begin_omp_parallel ();
35228 save = cp_parser_begin_omp_structured_block (parser);
35229 cp_parser_statement (parser, NULL_TREE, false, if_p);
35230 cp_parser_end_omp_structured_block (parser, save);
35231 stmt = finish_omp_parallel (clauses, block);
35232 return stmt;
35235 /* OpenMP 2.5:
35236 # pragma omp single single-clause[optseq] new-line
35237 structured-block */
35239 #define OMP_SINGLE_CLAUSE_MASK \
35240 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35245 static tree
35246 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35248 tree stmt = make_node (OMP_SINGLE);
35249 TREE_TYPE (stmt) = void_type_node;
35251 OMP_SINGLE_CLAUSES (stmt)
35252 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35253 "#pragma omp single", pragma_tok);
35254 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35256 return add_stmt (stmt);
35259 /* OpenMP 3.0:
35260 # pragma omp task task-clause[optseq] new-line
35261 structured-block */
35263 #define OMP_TASK_CLAUSE_MASK \
35264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35270 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35271 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35275 static tree
35276 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35278 tree clauses, block;
35279 unsigned int save;
35281 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35282 "#pragma omp task", pragma_tok);
35283 block = begin_omp_task ();
35284 save = cp_parser_begin_omp_structured_block (parser);
35285 cp_parser_statement (parser, NULL_TREE, false, if_p);
35286 cp_parser_end_omp_structured_block (parser, save);
35287 return finish_omp_task (clauses, block);
35290 /* OpenMP 3.0:
35291 # pragma omp taskwait new-line */
35293 static void
35294 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35296 cp_parser_require_pragma_eol (parser, pragma_tok);
35297 finish_omp_taskwait ();
35300 /* OpenMP 3.1:
35301 # pragma omp taskyield new-line */
35303 static void
35304 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35306 cp_parser_require_pragma_eol (parser, pragma_tok);
35307 finish_omp_taskyield ();
35310 /* OpenMP 4.0:
35311 # pragma omp taskgroup new-line
35312 structured-block */
35314 static tree
35315 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35317 cp_parser_require_pragma_eol (parser, pragma_tok);
35318 return c_finish_omp_taskgroup (input_location,
35319 cp_parser_omp_structured_block (parser,
35320 if_p));
35324 /* OpenMP 2.5:
35325 # pragma omp threadprivate (variable-list) */
35327 static void
35328 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35330 tree vars;
35332 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35333 cp_parser_require_pragma_eol (parser, pragma_tok);
35335 finish_omp_threadprivate (vars);
35338 /* OpenMP 4.0:
35339 # pragma omp cancel cancel-clause[optseq] new-line */
35341 #define OMP_CANCEL_CLAUSE_MASK \
35342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35348 static void
35349 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35351 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35352 "#pragma omp cancel", pragma_tok);
35353 finish_omp_cancel (clauses);
35356 /* OpenMP 4.0:
35357 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35359 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35365 static void
35366 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35367 enum pragma_context context)
35369 tree clauses;
35370 bool point_seen = false;
35372 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35374 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35375 const char *p = IDENTIFIER_POINTER (id);
35377 if (strcmp (p, "point") == 0)
35379 cp_lexer_consume_token (parser->lexer);
35380 point_seen = true;
35383 if (!point_seen)
35385 cp_parser_error (parser, "expected %<point%>");
35386 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35387 return;
35390 if (context != pragma_compound)
35392 if (context == pragma_stmt)
35393 error_at (pragma_tok->location,
35394 "%<#pragma %s%> may only be used in compound statements",
35395 "omp cancellation point");
35396 else
35397 cp_parser_error (parser, "expected declaration specifiers");
35398 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35399 return;
35402 clauses = cp_parser_omp_all_clauses (parser,
35403 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35404 "#pragma omp cancellation point",
35405 pragma_tok);
35406 finish_omp_cancellation_point (clauses);
35409 /* OpenMP 4.0:
35410 #pragma omp distribute distribute-clause[optseq] new-line
35411 for-loop */
35413 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35414 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35420 static tree
35421 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35422 char *p_name, omp_clause_mask mask, tree *cclauses,
35423 bool *if_p)
35425 tree clauses, sb, ret;
35426 unsigned int save;
35427 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35429 strcat (p_name, " distribute");
35430 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35432 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35434 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35435 const char *p = IDENTIFIER_POINTER (id);
35436 bool simd = false;
35437 bool parallel = false;
35439 if (strcmp (p, "simd") == 0)
35440 simd = true;
35441 else
35442 parallel = strcmp (p, "parallel") == 0;
35443 if (parallel || simd)
35445 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35446 if (cclauses == NULL)
35447 cclauses = cclauses_buf;
35448 cp_lexer_consume_token (parser->lexer);
35449 if (!flag_openmp) /* flag_openmp_simd */
35451 if (simd)
35452 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35453 cclauses, if_p);
35454 else
35455 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35456 cclauses, if_p);
35458 sb = begin_omp_structured_block ();
35459 save = cp_parser_begin_omp_structured_block (parser);
35460 if (simd)
35461 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35462 cclauses, if_p);
35463 else
35464 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35465 cclauses, if_p);
35466 cp_parser_end_omp_structured_block (parser, save);
35467 tree body = finish_omp_structured_block (sb);
35468 if (ret == NULL)
35469 return ret;
35470 ret = make_node (OMP_DISTRIBUTE);
35471 TREE_TYPE (ret) = void_type_node;
35472 OMP_FOR_BODY (ret) = body;
35473 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35474 SET_EXPR_LOCATION (ret, loc);
35475 add_stmt (ret);
35476 return ret;
35479 if (!flag_openmp) /* flag_openmp_simd */
35481 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35482 return NULL_TREE;
35485 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35486 cclauses == NULL);
35487 if (cclauses)
35489 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35490 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35493 sb = begin_omp_structured_block ();
35494 save = cp_parser_begin_omp_structured_block (parser);
35496 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35498 cp_parser_end_omp_structured_block (parser, save);
35499 add_stmt (finish_omp_structured_block (sb));
35501 return ret;
35504 /* OpenMP 4.0:
35505 # pragma omp teams teams-clause[optseq] new-line
35506 structured-block */
35508 #define OMP_TEAMS_CLAUSE_MASK \
35509 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35517 static tree
35518 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35519 char *p_name, omp_clause_mask mask, tree *cclauses,
35520 bool *if_p)
35522 tree clauses, sb, ret;
35523 unsigned int save;
35524 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35526 strcat (p_name, " teams");
35527 mask |= OMP_TEAMS_CLAUSE_MASK;
35529 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35531 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35532 const char *p = IDENTIFIER_POINTER (id);
35533 if (strcmp (p, "distribute") == 0)
35535 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35536 if (cclauses == NULL)
35537 cclauses = cclauses_buf;
35539 cp_lexer_consume_token (parser->lexer);
35540 if (!flag_openmp) /* flag_openmp_simd */
35541 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35542 cclauses, if_p);
35543 sb = begin_omp_structured_block ();
35544 save = cp_parser_begin_omp_structured_block (parser);
35545 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35546 cclauses, if_p);
35547 cp_parser_end_omp_structured_block (parser, save);
35548 tree body = finish_omp_structured_block (sb);
35549 if (ret == NULL)
35550 return ret;
35551 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35552 ret = make_node (OMP_TEAMS);
35553 TREE_TYPE (ret) = void_type_node;
35554 OMP_TEAMS_CLAUSES (ret) = clauses;
35555 OMP_TEAMS_BODY (ret) = body;
35556 OMP_TEAMS_COMBINED (ret) = 1;
35557 SET_EXPR_LOCATION (ret, loc);
35558 return add_stmt (ret);
35561 if (!flag_openmp) /* flag_openmp_simd */
35563 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35564 return NULL_TREE;
35567 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35568 cclauses == NULL);
35569 if (cclauses)
35571 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35572 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35575 tree stmt = make_node (OMP_TEAMS);
35576 TREE_TYPE (stmt) = void_type_node;
35577 OMP_TEAMS_CLAUSES (stmt) = clauses;
35578 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35579 SET_EXPR_LOCATION (stmt, loc);
35581 return add_stmt (stmt);
35584 /* OpenMP 4.0:
35585 # pragma omp target data target-data-clause[optseq] new-line
35586 structured-block */
35588 #define OMP_TARGET_DATA_CLAUSE_MASK \
35589 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35594 static tree
35595 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35597 tree clauses
35598 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35599 "#pragma omp target data", pragma_tok);
35600 int map_seen = 0;
35601 for (tree *pc = &clauses; *pc;)
35603 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35604 switch (OMP_CLAUSE_MAP_KIND (*pc))
35606 case GOMP_MAP_TO:
35607 case GOMP_MAP_ALWAYS_TO:
35608 case GOMP_MAP_FROM:
35609 case GOMP_MAP_ALWAYS_FROM:
35610 case GOMP_MAP_TOFROM:
35611 case GOMP_MAP_ALWAYS_TOFROM:
35612 case GOMP_MAP_ALLOC:
35613 map_seen = 3;
35614 break;
35615 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35616 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35617 case GOMP_MAP_ALWAYS_POINTER:
35618 break;
35619 default:
35620 map_seen |= 1;
35621 error_at (OMP_CLAUSE_LOCATION (*pc),
35622 "%<#pragma omp target data%> with map-type other "
35623 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35624 "on %<map%> clause");
35625 *pc = OMP_CLAUSE_CHAIN (*pc);
35626 continue;
35628 pc = &OMP_CLAUSE_CHAIN (*pc);
35631 if (map_seen != 3)
35633 if (map_seen == 0)
35634 error_at (pragma_tok->location,
35635 "%<#pragma omp target data%> must contain at least "
35636 "one %<map%> clause");
35637 return NULL_TREE;
35640 tree stmt = make_node (OMP_TARGET_DATA);
35641 TREE_TYPE (stmt) = void_type_node;
35642 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35644 keep_next_level (true);
35645 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35647 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35648 return add_stmt (stmt);
35651 /* OpenMP 4.5:
35652 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35653 structured-block */
35655 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35662 static tree
35663 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35664 enum pragma_context context)
35666 bool data_seen = false;
35667 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35669 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35670 const char *p = IDENTIFIER_POINTER (id);
35672 if (strcmp (p, "data") == 0)
35674 cp_lexer_consume_token (parser->lexer);
35675 data_seen = true;
35678 if (!data_seen)
35680 cp_parser_error (parser, "expected %<data%>");
35681 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35682 return NULL_TREE;
35685 if (context == pragma_stmt)
35687 error_at (pragma_tok->location,
35688 "%<#pragma %s%> may only be used in compound statements",
35689 "omp target enter data");
35690 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35691 return NULL_TREE;
35694 tree clauses
35695 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35696 "#pragma omp target enter data", pragma_tok);
35697 int map_seen = 0;
35698 for (tree *pc = &clauses; *pc;)
35700 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35701 switch (OMP_CLAUSE_MAP_KIND (*pc))
35703 case GOMP_MAP_TO:
35704 case GOMP_MAP_ALWAYS_TO:
35705 case GOMP_MAP_ALLOC:
35706 map_seen = 3;
35707 break;
35708 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35709 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35710 case GOMP_MAP_ALWAYS_POINTER:
35711 break;
35712 default:
35713 map_seen |= 1;
35714 error_at (OMP_CLAUSE_LOCATION (*pc),
35715 "%<#pragma omp target enter data%> with map-type other "
35716 "than %<to%> or %<alloc%> on %<map%> clause");
35717 *pc = OMP_CLAUSE_CHAIN (*pc);
35718 continue;
35720 pc = &OMP_CLAUSE_CHAIN (*pc);
35723 if (map_seen != 3)
35725 if (map_seen == 0)
35726 error_at (pragma_tok->location,
35727 "%<#pragma omp target enter data%> must contain at least "
35728 "one %<map%> clause");
35729 return NULL_TREE;
35732 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35733 TREE_TYPE (stmt) = void_type_node;
35734 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35735 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35736 return add_stmt (stmt);
35739 /* OpenMP 4.5:
35740 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35741 structured-block */
35743 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35750 static tree
35751 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35752 enum pragma_context context)
35754 bool data_seen = false;
35755 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35757 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35758 const char *p = IDENTIFIER_POINTER (id);
35760 if (strcmp (p, "data") == 0)
35762 cp_lexer_consume_token (parser->lexer);
35763 data_seen = true;
35766 if (!data_seen)
35768 cp_parser_error (parser, "expected %<data%>");
35769 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35770 return NULL_TREE;
35773 if (context == pragma_stmt)
35775 error_at (pragma_tok->location,
35776 "%<#pragma %s%> may only be used in compound statements",
35777 "omp target exit data");
35778 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35779 return NULL_TREE;
35782 tree clauses
35783 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35784 "#pragma omp target exit data", pragma_tok);
35785 int map_seen = 0;
35786 for (tree *pc = &clauses; *pc;)
35788 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35789 switch (OMP_CLAUSE_MAP_KIND (*pc))
35791 case GOMP_MAP_FROM:
35792 case GOMP_MAP_ALWAYS_FROM:
35793 case GOMP_MAP_RELEASE:
35794 case GOMP_MAP_DELETE:
35795 map_seen = 3;
35796 break;
35797 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35798 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35799 case GOMP_MAP_ALWAYS_POINTER:
35800 break;
35801 default:
35802 map_seen |= 1;
35803 error_at (OMP_CLAUSE_LOCATION (*pc),
35804 "%<#pragma omp target exit data%> with map-type other "
35805 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35806 " clause");
35807 *pc = OMP_CLAUSE_CHAIN (*pc);
35808 continue;
35810 pc = &OMP_CLAUSE_CHAIN (*pc);
35813 if (map_seen != 3)
35815 if (map_seen == 0)
35816 error_at (pragma_tok->location,
35817 "%<#pragma omp target exit data%> must contain at least "
35818 "one %<map%> clause");
35819 return NULL_TREE;
35822 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35823 TREE_TYPE (stmt) = void_type_node;
35824 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35825 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35826 return add_stmt (stmt);
35829 /* OpenMP 4.0:
35830 # pragma omp target update target-update-clause[optseq] new-line */
35832 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35833 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35840 static bool
35841 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35842 enum pragma_context context)
35844 if (context == pragma_stmt)
35846 error_at (pragma_tok->location,
35847 "%<#pragma %s%> may only be used in compound statements",
35848 "omp target update");
35849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35850 return false;
35853 tree clauses
35854 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35855 "#pragma omp target update", pragma_tok);
35856 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35857 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35859 error_at (pragma_tok->location,
35860 "%<#pragma omp target update%> must contain at least one "
35861 "%<from%> or %<to%> clauses");
35862 return false;
35865 tree stmt = make_node (OMP_TARGET_UPDATE);
35866 TREE_TYPE (stmt) = void_type_node;
35867 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35868 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35869 add_stmt (stmt);
35870 return false;
35873 /* OpenMP 4.0:
35874 # pragma omp target target-clause[optseq] new-line
35875 structured-block */
35877 #define OMP_TARGET_CLAUSE_MASK \
35878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35888 static bool
35889 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35890 enum pragma_context context, bool *if_p)
35892 tree *pc = NULL, stmt;
35894 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35896 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35897 const char *p = IDENTIFIER_POINTER (id);
35898 enum tree_code ccode = ERROR_MARK;
35900 if (strcmp (p, "teams") == 0)
35901 ccode = OMP_TEAMS;
35902 else if (strcmp (p, "parallel") == 0)
35903 ccode = OMP_PARALLEL;
35904 else if (strcmp (p, "simd") == 0)
35905 ccode = OMP_SIMD;
35906 if (ccode != ERROR_MARK)
35908 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35909 char p_name[sizeof ("#pragma omp target teams distribute "
35910 "parallel for simd")];
35912 cp_lexer_consume_token (parser->lexer);
35913 strcpy (p_name, "#pragma omp target");
35914 if (!flag_openmp) /* flag_openmp_simd */
35916 tree stmt;
35917 switch (ccode)
35919 case OMP_TEAMS:
35920 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35921 OMP_TARGET_CLAUSE_MASK,
35922 cclauses, if_p);
35923 break;
35924 case OMP_PARALLEL:
35925 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35926 OMP_TARGET_CLAUSE_MASK,
35927 cclauses, if_p);
35928 break;
35929 case OMP_SIMD:
35930 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35931 OMP_TARGET_CLAUSE_MASK,
35932 cclauses, if_p);
35933 break;
35934 default:
35935 gcc_unreachable ();
35937 return stmt != NULL_TREE;
35939 keep_next_level (true);
35940 tree sb = begin_omp_structured_block (), ret;
35941 unsigned save = cp_parser_begin_omp_structured_block (parser);
35942 switch (ccode)
35944 case OMP_TEAMS:
35945 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35946 OMP_TARGET_CLAUSE_MASK, cclauses,
35947 if_p);
35948 break;
35949 case OMP_PARALLEL:
35950 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35951 OMP_TARGET_CLAUSE_MASK, cclauses,
35952 if_p);
35953 break;
35954 case OMP_SIMD:
35955 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35956 OMP_TARGET_CLAUSE_MASK, cclauses,
35957 if_p);
35958 break;
35959 default:
35960 gcc_unreachable ();
35962 cp_parser_end_omp_structured_block (parser, save);
35963 tree body = finish_omp_structured_block (sb);
35964 if (ret == NULL_TREE)
35965 return false;
35966 if (ccode == OMP_TEAMS && !processing_template_decl)
35968 /* For combined target teams, ensure the num_teams and
35969 thread_limit clause expressions are evaluated on the host,
35970 before entering the target construct. */
35971 tree c;
35972 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35973 c; c = OMP_CLAUSE_CHAIN (c))
35974 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35975 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35976 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35978 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35979 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35980 if (expr == error_mark_node)
35981 continue;
35982 tree tmp = TARGET_EXPR_SLOT (expr);
35983 add_stmt (expr);
35984 OMP_CLAUSE_OPERAND (c, 0) = expr;
35985 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35986 OMP_CLAUSE_FIRSTPRIVATE);
35987 OMP_CLAUSE_DECL (tc) = tmp;
35988 OMP_CLAUSE_CHAIN (tc)
35989 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35990 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35993 tree stmt = make_node (OMP_TARGET);
35994 TREE_TYPE (stmt) = void_type_node;
35995 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35996 OMP_TARGET_BODY (stmt) = body;
35997 OMP_TARGET_COMBINED (stmt) = 1;
35998 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35999 add_stmt (stmt);
36000 pc = &OMP_TARGET_CLAUSES (stmt);
36001 goto check_clauses;
36003 else if (!flag_openmp) /* flag_openmp_simd */
36005 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36006 return false;
36008 else if (strcmp (p, "data") == 0)
36010 cp_lexer_consume_token (parser->lexer);
36011 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36012 return true;
36014 else if (strcmp (p, "enter") == 0)
36016 cp_lexer_consume_token (parser->lexer);
36017 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36018 return false;
36020 else if (strcmp (p, "exit") == 0)
36022 cp_lexer_consume_token (parser->lexer);
36023 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36024 return false;
36026 else if (strcmp (p, "update") == 0)
36028 cp_lexer_consume_token (parser->lexer);
36029 return cp_parser_omp_target_update (parser, pragma_tok, context);
36032 if (!flag_openmp) /* flag_openmp_simd */
36034 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36035 return false;
36038 stmt = make_node (OMP_TARGET);
36039 TREE_TYPE (stmt) = void_type_node;
36041 OMP_TARGET_CLAUSES (stmt)
36042 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36043 "#pragma omp target", pragma_tok);
36044 pc = &OMP_TARGET_CLAUSES (stmt);
36045 keep_next_level (true);
36046 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36048 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36049 add_stmt (stmt);
36051 check_clauses:
36052 while (*pc)
36054 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36055 switch (OMP_CLAUSE_MAP_KIND (*pc))
36057 case GOMP_MAP_TO:
36058 case GOMP_MAP_ALWAYS_TO:
36059 case GOMP_MAP_FROM:
36060 case GOMP_MAP_ALWAYS_FROM:
36061 case GOMP_MAP_TOFROM:
36062 case GOMP_MAP_ALWAYS_TOFROM:
36063 case GOMP_MAP_ALLOC:
36064 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36065 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36066 case GOMP_MAP_ALWAYS_POINTER:
36067 break;
36068 default:
36069 error_at (OMP_CLAUSE_LOCATION (*pc),
36070 "%<#pragma omp target%> with map-type other "
36071 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36072 "on %<map%> clause");
36073 *pc = OMP_CLAUSE_CHAIN (*pc);
36074 continue;
36076 pc = &OMP_CLAUSE_CHAIN (*pc);
36078 return true;
36081 /* OpenACC 2.0:
36082 # pragma acc cache (variable-list) new-line
36085 static tree
36086 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36088 tree stmt, clauses;
36090 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36091 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36093 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36095 stmt = make_node (OACC_CACHE);
36096 TREE_TYPE (stmt) = void_type_node;
36097 OACC_CACHE_CLAUSES (stmt) = clauses;
36098 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36099 add_stmt (stmt);
36101 return stmt;
36104 /* OpenACC 2.0:
36105 # pragma acc data oacc-data-clause[optseq] new-line
36106 structured-block */
36108 #define OACC_DATA_CLAUSE_MASK \
36109 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36113 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36121 static tree
36122 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36124 tree stmt, clauses, block;
36125 unsigned int save;
36127 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36128 "#pragma acc data", pragma_tok);
36130 block = begin_omp_parallel ();
36131 save = cp_parser_begin_omp_structured_block (parser);
36132 cp_parser_statement (parser, NULL_TREE, false, if_p);
36133 cp_parser_end_omp_structured_block (parser, save);
36134 stmt = finish_oacc_data (clauses, block);
36135 return stmt;
36138 /* OpenACC 2.0:
36139 # pragma acc host_data <clauses> new-line
36140 structured-block */
36142 #define OACC_HOST_DATA_CLAUSE_MASK \
36143 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36145 static tree
36146 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36148 tree stmt, clauses, block;
36149 unsigned int save;
36151 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36152 "#pragma acc host_data", pragma_tok);
36154 block = begin_omp_parallel ();
36155 save = cp_parser_begin_omp_structured_block (parser);
36156 cp_parser_statement (parser, NULL_TREE, false, if_p);
36157 cp_parser_end_omp_structured_block (parser, save);
36158 stmt = finish_oacc_host_data (clauses, block);
36159 return stmt;
36162 /* OpenACC 2.0:
36163 # pragma acc declare oacc-data-clause[optseq] new-line
36166 #define OACC_DECLARE_CLAUSE_MASK \
36167 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36168 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36169 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36170 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36171 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36180 static tree
36181 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36183 tree clauses, stmt;
36184 bool error = false;
36186 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36187 "#pragma acc declare", pragma_tok, true);
36190 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36192 error_at (pragma_tok->location,
36193 "no valid clauses specified in %<#pragma acc declare%>");
36194 return NULL_TREE;
36197 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36199 location_t loc = OMP_CLAUSE_LOCATION (t);
36200 tree decl = OMP_CLAUSE_DECL (t);
36201 if (!DECL_P (decl))
36203 error_at (loc, "array section in %<#pragma acc declare%>");
36204 error = true;
36205 continue;
36207 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36208 switch (OMP_CLAUSE_MAP_KIND (t))
36210 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36211 case GOMP_MAP_FORCE_ALLOC:
36212 case GOMP_MAP_FORCE_TO:
36213 case GOMP_MAP_FORCE_DEVICEPTR:
36214 case GOMP_MAP_DEVICE_RESIDENT:
36215 break;
36217 case GOMP_MAP_LINK:
36218 if (!global_bindings_p ()
36219 && (TREE_STATIC (decl)
36220 || !DECL_EXTERNAL (decl)))
36222 error_at (loc,
36223 "%qD must be a global variable in "
36224 "%<#pragma acc declare link%>",
36225 decl);
36226 error = true;
36227 continue;
36229 break;
36231 default:
36232 if (global_bindings_p ())
36234 error_at (loc, "invalid OpenACC clause at file scope");
36235 error = true;
36236 continue;
36238 if (DECL_EXTERNAL (decl))
36240 error_at (loc,
36241 "invalid use of %<extern%> variable %qD "
36242 "in %<#pragma acc declare%>", decl);
36243 error = true;
36244 continue;
36246 else if (TREE_PUBLIC (decl))
36248 error_at (loc,
36249 "invalid use of %<global%> variable %qD "
36250 "in %<#pragma acc declare%>", decl);
36251 error = true;
36252 continue;
36254 break;
36257 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36258 || lookup_attribute ("omp declare target link",
36259 DECL_ATTRIBUTES (decl)))
36261 error_at (loc, "variable %qD used more than once with "
36262 "%<#pragma acc declare%>", decl);
36263 error = true;
36264 continue;
36267 if (!error)
36269 tree id;
36271 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36272 id = get_identifier ("omp declare target link");
36273 else
36274 id = get_identifier ("omp declare target");
36276 DECL_ATTRIBUTES (decl)
36277 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36278 if (global_bindings_p ())
36280 symtab_node *node = symtab_node::get (decl);
36281 if (node != NULL)
36283 node->offloadable = 1;
36284 if (ENABLE_OFFLOADING)
36286 g->have_offload = true;
36287 if (is_a <varpool_node *> (node))
36288 vec_safe_push (offload_vars, decl);
36295 if (error || global_bindings_p ())
36296 return NULL_TREE;
36298 stmt = make_node (OACC_DECLARE);
36299 TREE_TYPE (stmt) = void_type_node;
36300 OACC_DECLARE_CLAUSES (stmt) = clauses;
36301 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36303 add_stmt (stmt);
36305 return NULL_TREE;
36308 /* OpenACC 2.0:
36309 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36313 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36315 LOC is the location of the #pragma token.
36318 #define OACC_ENTER_DATA_CLAUSE_MASK \
36319 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36320 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36321 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36322 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36323 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36324 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36325 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36327 #define OACC_EXIT_DATA_CLAUSE_MASK \
36328 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36334 static tree
36335 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36336 bool enter)
36338 location_t loc = pragma_tok->location;
36339 tree stmt, clauses;
36340 const char *p = "";
36342 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36343 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36345 if (strcmp (p, "data") != 0)
36347 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36348 enter ? "enter" : "exit");
36349 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36350 return NULL_TREE;
36353 cp_lexer_consume_token (parser->lexer);
36355 if (enter)
36356 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36357 "#pragma acc enter data", pragma_tok);
36358 else
36359 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36360 "#pragma acc exit data", pragma_tok);
36362 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36364 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36365 enter ? "enter" : "exit");
36366 return NULL_TREE;
36369 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36370 TREE_TYPE (stmt) = void_type_node;
36371 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36372 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36373 add_stmt (stmt);
36374 return stmt;
36377 /* OpenACC 2.0:
36378 # pragma acc loop oacc-loop-clause[optseq] new-line
36379 structured-block */
36381 #define OACC_LOOP_CLAUSE_MASK \
36382 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36393 static tree
36394 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36395 omp_clause_mask mask, tree *cclauses, bool *if_p)
36397 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36399 strcat (p_name, " loop");
36400 mask |= OACC_LOOP_CLAUSE_MASK;
36402 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36403 cclauses == NULL);
36404 if (cclauses)
36406 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36407 if (*cclauses)
36408 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36409 if (clauses)
36410 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36413 tree block = begin_omp_structured_block ();
36414 int save = cp_parser_begin_omp_structured_block (parser);
36415 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36416 cp_parser_end_omp_structured_block (parser, save);
36417 add_stmt (finish_omp_structured_block (block));
36419 return stmt;
36422 /* OpenACC 2.0:
36423 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36424 structured-block
36428 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36429 structured-block
36432 #define OACC_KERNELS_CLAUSE_MASK \
36433 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36434 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36435 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36436 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36437 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36448 #define OACC_PARALLEL_CLAUSE_MASK \
36449 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36470 static tree
36471 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36472 char *p_name, bool *if_p)
36474 omp_clause_mask mask;
36475 enum tree_code code;
36476 switch (cp_parser_pragma_kind (pragma_tok))
36478 case PRAGMA_OACC_KERNELS:
36479 strcat (p_name, " kernels");
36480 mask = OACC_KERNELS_CLAUSE_MASK;
36481 code = OACC_KERNELS;
36482 break;
36483 case PRAGMA_OACC_PARALLEL:
36484 strcat (p_name, " parallel");
36485 mask = OACC_PARALLEL_CLAUSE_MASK;
36486 code = OACC_PARALLEL;
36487 break;
36488 default:
36489 gcc_unreachable ();
36492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36494 const char *p
36495 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36496 if (strcmp (p, "loop") == 0)
36498 cp_lexer_consume_token (parser->lexer);
36499 tree block = begin_omp_parallel ();
36500 tree clauses;
36501 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36502 if_p);
36503 return finish_omp_construct (code, block, clauses);
36507 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36509 tree block = begin_omp_parallel ();
36510 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36511 cp_parser_statement (parser, NULL_TREE, false, if_p);
36512 cp_parser_end_omp_structured_block (parser, save);
36513 return finish_omp_construct (code, block, clauses);
36516 /* OpenACC 2.0:
36517 # pragma acc update oacc-update-clause[optseq] new-line
36520 #define OACC_UPDATE_CLAUSE_MASK \
36521 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36524 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36525 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36528 static tree
36529 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36531 tree stmt, clauses;
36533 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36534 "#pragma acc update", pragma_tok);
36536 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36538 error_at (pragma_tok->location,
36539 "%<#pragma acc update%> must contain at least one "
36540 "%<device%> or %<host%> or %<self%> clause");
36541 return NULL_TREE;
36544 stmt = make_node (OACC_UPDATE);
36545 TREE_TYPE (stmt) = void_type_node;
36546 OACC_UPDATE_CLAUSES (stmt) = clauses;
36547 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36548 add_stmt (stmt);
36549 return stmt;
36552 /* OpenACC 2.0:
36553 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36555 LOC is the location of the #pragma token.
36558 #define OACC_WAIT_CLAUSE_MASK \
36559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36561 static tree
36562 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36564 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36565 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36567 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36568 list = cp_parser_oacc_wait_list (parser, loc, list);
36570 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36571 "#pragma acc wait", pragma_tok);
36573 stmt = c_finish_oacc_wait (loc, list, clauses);
36574 stmt = finish_expr_stmt (stmt);
36576 return stmt;
36579 /* OpenMP 4.0:
36580 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36582 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36583 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36590 static void
36591 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36592 enum pragma_context context)
36594 bool first_p = parser->omp_declare_simd == NULL;
36595 cp_omp_declare_simd_data data;
36596 if (first_p)
36598 data.error_seen = false;
36599 data.fndecl_seen = false;
36600 data.tokens = vNULL;
36601 data.clauses = NULL_TREE;
36602 /* It is safe to take the address of a local variable; it will only be
36603 used while this scope is live. */
36604 parser->omp_declare_simd = &data;
36607 /* Store away all pragma tokens. */
36608 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36609 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36610 cp_lexer_consume_token (parser->lexer);
36611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36612 parser->omp_declare_simd->error_seen = true;
36613 cp_parser_require_pragma_eol (parser, pragma_tok);
36614 struct cp_token_cache *cp
36615 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36616 parser->omp_declare_simd->tokens.safe_push (cp);
36618 if (first_p)
36620 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36621 cp_parser_pragma (parser, context, NULL);
36622 switch (context)
36624 case pragma_external:
36625 cp_parser_declaration (parser);
36626 break;
36627 case pragma_member:
36628 cp_parser_member_declaration (parser);
36629 break;
36630 case pragma_objc_icode:
36631 cp_parser_block_declaration (parser, /*statement_p=*/false);
36632 break;
36633 default:
36634 cp_parser_declaration_statement (parser);
36635 break;
36637 if (parser->omp_declare_simd
36638 && !parser->omp_declare_simd->error_seen
36639 && !parser->omp_declare_simd->fndecl_seen)
36640 error_at (pragma_tok->location,
36641 "%<#pragma omp declare simd%> not immediately followed by "
36642 "function declaration or definition");
36643 data.tokens.release ();
36644 parser->omp_declare_simd = NULL;
36648 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36649 This function is modelled similar to the late parsing of omp declare
36650 simd. */
36652 static tree
36653 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36655 struct cp_token_cache *ce;
36656 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36657 int ii = 0;
36659 if (parser->omp_declare_simd != NULL
36660 || lookup_attribute ("simd", attrs))
36662 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36663 "used in the same function marked as a Cilk Plus SIMD-enabled "
36664 "function");
36665 parser->cilk_simd_fn_info->tokens.release ();
36666 XDELETE (parser->cilk_simd_fn_info);
36667 parser->cilk_simd_fn_info = NULL;
36668 return attrs;
36670 if (!info->error_seen && info->fndecl_seen)
36672 error ("vector attribute not immediately followed by a single function"
36673 " declaration or definition");
36674 info->error_seen = true;
36676 if (info->error_seen)
36677 return attrs;
36679 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36681 tree c, cl;
36683 cp_parser_push_lexer_for_tokens (parser, ce);
36684 parser->lexer->in_pragma = true;
36685 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36686 "SIMD-enabled functions attribute",
36687 NULL);
36688 cp_parser_pop_lexer (parser);
36689 if (cl)
36690 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36692 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36693 TREE_CHAIN (c) = attrs;
36694 attrs = c;
36696 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36697 TREE_CHAIN (c) = attrs;
36698 if (processing_template_decl)
36699 ATTR_IS_DEPENDENT (c) = 1;
36700 attrs = c;
36702 info->fndecl_seen = true;
36703 parser->cilk_simd_fn_info->tokens.release ();
36704 XDELETE (parser->cilk_simd_fn_info);
36705 parser->cilk_simd_fn_info = NULL;
36706 return attrs;
36709 /* Finalize #pragma omp declare simd clauses after direct declarator has
36710 been parsed, and put that into "omp declare simd" attribute. */
36712 static tree
36713 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36715 struct cp_token_cache *ce;
36716 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36717 int i;
36719 if (!data->error_seen && data->fndecl_seen)
36721 error ("%<#pragma omp declare simd%> not immediately followed by "
36722 "a single function declaration or definition");
36723 data->error_seen = true;
36725 if (data->error_seen)
36726 return attrs;
36728 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36730 tree c, cl;
36732 cp_parser_push_lexer_for_tokens (parser, ce);
36733 parser->lexer->in_pragma = true;
36734 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36735 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36736 cp_lexer_consume_token (parser->lexer);
36737 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36738 "#pragma omp declare simd", pragma_tok);
36739 cp_parser_pop_lexer (parser);
36740 if (cl)
36741 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36742 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36743 TREE_CHAIN (c) = attrs;
36744 if (processing_template_decl)
36745 ATTR_IS_DEPENDENT (c) = 1;
36746 attrs = c;
36749 data->fndecl_seen = true;
36750 return attrs;
36754 /* OpenMP 4.0:
36755 # pragma omp declare target new-line
36756 declarations and definitions
36757 # pragma omp end declare target new-line
36759 OpenMP 4.5:
36760 # pragma omp declare target ( extended-list ) new-line
36762 # pragma omp declare target declare-target-clauses[seq] new-line */
36764 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36765 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36768 static void
36769 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36771 tree clauses = NULL_TREE;
36772 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36773 clauses
36774 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36775 "#pragma omp declare target", pragma_tok);
36776 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36778 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36779 clauses);
36780 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36781 cp_parser_require_pragma_eol (parser, pragma_tok);
36783 else
36785 cp_parser_require_pragma_eol (parser, pragma_tok);
36786 scope_chain->omp_declare_target_attribute++;
36787 return;
36789 if (scope_chain->omp_declare_target_attribute)
36790 error_at (pragma_tok->location,
36791 "%<#pragma omp declare target%> with clauses in between "
36792 "%<#pragma omp declare target%> without clauses and "
36793 "%<#pragma omp end declare target%>");
36794 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36796 tree t = OMP_CLAUSE_DECL (c), id;
36797 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36798 tree at2 = lookup_attribute ("omp declare target link",
36799 DECL_ATTRIBUTES (t));
36800 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36802 id = get_identifier ("omp declare target link");
36803 std::swap (at1, at2);
36805 else
36806 id = get_identifier ("omp declare target");
36807 if (at2)
36809 error_at (OMP_CLAUSE_LOCATION (c),
36810 "%qD specified both in declare target %<link%> and %<to%>"
36811 " clauses", t);
36812 continue;
36814 if (!at1)
36816 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36817 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36818 continue;
36820 symtab_node *node = symtab_node::get (t);
36821 if (node != NULL)
36823 node->offloadable = 1;
36824 if (ENABLE_OFFLOADING)
36826 g->have_offload = true;
36827 if (is_a <varpool_node *> (node))
36828 vec_safe_push (offload_vars, t);
36835 static void
36836 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36838 const char *p = "";
36839 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36841 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36842 p = IDENTIFIER_POINTER (id);
36844 if (strcmp (p, "declare") == 0)
36846 cp_lexer_consume_token (parser->lexer);
36847 p = "";
36848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36851 p = IDENTIFIER_POINTER (id);
36853 if (strcmp (p, "target") == 0)
36854 cp_lexer_consume_token (parser->lexer);
36855 else
36857 cp_parser_error (parser, "expected %<target%>");
36858 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36859 return;
36862 else
36864 cp_parser_error (parser, "expected %<declare%>");
36865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36866 return;
36868 cp_parser_require_pragma_eol (parser, pragma_tok);
36869 if (!scope_chain->omp_declare_target_attribute)
36870 error_at (pragma_tok->location,
36871 "%<#pragma omp end declare target%> without corresponding "
36872 "%<#pragma omp declare target%>");
36873 else
36874 scope_chain->omp_declare_target_attribute--;
36877 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36878 expression and optional initializer clause of
36879 #pragma omp declare reduction. We store the expression(s) as
36880 either 3, 6 or 7 special statements inside of the artificial function's
36881 body. The first two statements are DECL_EXPRs for the artificial
36882 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36883 expression that uses those variables.
36884 If there was any INITIALIZER clause, this is followed by further statements,
36885 the fourth and fifth statements are DECL_EXPRs for the artificial
36886 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36887 constructor variant (first token after open paren is not omp_priv),
36888 then the sixth statement is a statement with the function call expression
36889 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36890 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36891 to initialize the OMP_PRIV artificial variable and there is seventh
36892 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36894 static bool
36895 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36897 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36898 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36899 type = TREE_TYPE (type);
36900 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36901 DECL_ARTIFICIAL (omp_out) = 1;
36902 pushdecl (omp_out);
36903 add_decl_expr (omp_out);
36904 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36905 DECL_ARTIFICIAL (omp_in) = 1;
36906 pushdecl (omp_in);
36907 add_decl_expr (omp_in);
36908 tree combiner;
36909 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36911 keep_next_level (true);
36912 tree block = begin_omp_structured_block ();
36913 combiner = cp_parser_expression (parser);
36914 finish_expr_stmt (combiner);
36915 block = finish_omp_structured_block (block);
36916 add_stmt (block);
36918 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36919 return false;
36921 const char *p = "";
36922 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36924 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36925 p = IDENTIFIER_POINTER (id);
36928 if (strcmp (p, "initializer") == 0)
36930 cp_lexer_consume_token (parser->lexer);
36931 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36932 return false;
36934 p = "";
36935 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36937 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36938 p = IDENTIFIER_POINTER (id);
36941 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36942 DECL_ARTIFICIAL (omp_priv) = 1;
36943 pushdecl (omp_priv);
36944 add_decl_expr (omp_priv);
36945 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36946 DECL_ARTIFICIAL (omp_orig) = 1;
36947 pushdecl (omp_orig);
36948 add_decl_expr (omp_orig);
36950 keep_next_level (true);
36951 block = begin_omp_structured_block ();
36953 bool ctor = false;
36954 if (strcmp (p, "omp_priv") == 0)
36956 bool is_direct_init, is_non_constant_init;
36957 ctor = true;
36958 cp_lexer_consume_token (parser->lexer);
36959 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36960 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36961 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36962 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36963 == CPP_CLOSE_PAREN
36964 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36965 == CPP_CLOSE_PAREN))
36967 finish_omp_structured_block (block);
36968 error ("invalid initializer clause");
36969 return false;
36971 initializer = cp_parser_initializer (parser, &is_direct_init,
36972 &is_non_constant_init);
36973 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36974 NULL_TREE, LOOKUP_ONLYCONVERTING);
36976 else
36978 cp_parser_parse_tentatively (parser);
36979 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36980 /*check_dependency_p=*/true,
36981 /*template_p=*/NULL,
36982 /*declarator_p=*/false,
36983 /*optional_p=*/false);
36984 vec<tree, va_gc> *args;
36985 if (fn_name == error_mark_node
36986 || cp_parser_error_occurred (parser)
36987 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36988 || ((args = cp_parser_parenthesized_expression_list
36989 (parser, non_attr, /*cast_p=*/false,
36990 /*allow_expansion_p=*/true,
36991 /*non_constant_p=*/NULL)),
36992 cp_parser_error_occurred (parser)))
36994 finish_omp_structured_block (block);
36995 cp_parser_abort_tentative_parse (parser);
36996 cp_parser_error (parser, "expected id-expression (arguments)");
36997 return false;
36999 unsigned int i;
37000 tree arg;
37001 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37002 if (arg == omp_priv
37003 || (TREE_CODE (arg) == ADDR_EXPR
37004 && TREE_OPERAND (arg, 0) == omp_priv))
37005 break;
37006 cp_parser_abort_tentative_parse (parser);
37007 if (arg == NULL_TREE)
37008 error ("one of the initializer call arguments should be %<omp_priv%>"
37009 " or %<&omp_priv%>");
37010 initializer = cp_parser_postfix_expression (parser, false, false, false,
37011 false, NULL);
37012 finish_expr_stmt (initializer);
37015 block = finish_omp_structured_block (block);
37016 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37017 add_stmt (block);
37019 if (ctor)
37020 add_decl_expr (omp_orig);
37022 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37023 return false;
37026 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37027 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37029 return true;
37032 /* OpenMP 4.0
37033 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37034 initializer-clause[opt] new-line
37036 initializer-clause:
37037 initializer (omp_priv initializer)
37038 initializer (function-name (argument-list)) */
37040 static void
37041 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37042 enum pragma_context)
37044 auto_vec<tree> types;
37045 enum tree_code reduc_code = ERROR_MARK;
37046 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37047 unsigned int i;
37048 cp_token *first_token;
37049 cp_token_cache *cp;
37050 int errs;
37051 void *p;
37053 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37054 p = obstack_alloc (&declarator_obstack, 0);
37056 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37057 goto fail;
37059 switch (cp_lexer_peek_token (parser->lexer)->type)
37061 case CPP_PLUS:
37062 reduc_code = PLUS_EXPR;
37063 break;
37064 case CPP_MULT:
37065 reduc_code = MULT_EXPR;
37066 break;
37067 case CPP_MINUS:
37068 reduc_code = MINUS_EXPR;
37069 break;
37070 case CPP_AND:
37071 reduc_code = BIT_AND_EXPR;
37072 break;
37073 case CPP_XOR:
37074 reduc_code = BIT_XOR_EXPR;
37075 break;
37076 case CPP_OR:
37077 reduc_code = BIT_IOR_EXPR;
37078 break;
37079 case CPP_AND_AND:
37080 reduc_code = TRUTH_ANDIF_EXPR;
37081 break;
37082 case CPP_OR_OR:
37083 reduc_code = TRUTH_ORIF_EXPR;
37084 break;
37085 case CPP_NAME:
37086 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37087 break;
37088 default:
37089 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37090 "%<|%>, %<&&%>, %<||%> or identifier");
37091 goto fail;
37094 if (reduc_code != ERROR_MARK)
37095 cp_lexer_consume_token (parser->lexer);
37097 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37098 if (reduc_id == error_mark_node)
37099 goto fail;
37101 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37102 goto fail;
37104 /* Types may not be defined in declare reduction type list. */
37105 const char *saved_message;
37106 saved_message = parser->type_definition_forbidden_message;
37107 parser->type_definition_forbidden_message
37108 = G_("types may not be defined in declare reduction type list");
37109 bool saved_colon_corrects_to_scope_p;
37110 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37111 parser->colon_corrects_to_scope_p = false;
37112 bool saved_colon_doesnt_start_class_def_p;
37113 saved_colon_doesnt_start_class_def_p
37114 = parser->colon_doesnt_start_class_def_p;
37115 parser->colon_doesnt_start_class_def_p = true;
37117 while (true)
37119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37120 type = cp_parser_type_id (parser);
37121 if (type == error_mark_node)
37123 else if (ARITHMETIC_TYPE_P (type)
37124 && (orig_reduc_id == NULL_TREE
37125 || (TREE_CODE (type) != COMPLEX_TYPE
37126 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37127 "min") == 0
37128 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37129 "max") == 0))))
37130 error_at (loc, "predeclared arithmetic type %qT in "
37131 "%<#pragma omp declare reduction%>", type);
37132 else if (TREE_CODE (type) == FUNCTION_TYPE
37133 || TREE_CODE (type) == METHOD_TYPE
37134 || TREE_CODE (type) == ARRAY_TYPE)
37135 error_at (loc, "function or array type %qT in "
37136 "%<#pragma omp declare reduction%>", type);
37137 else if (TREE_CODE (type) == REFERENCE_TYPE)
37138 error_at (loc, "reference type %qT in "
37139 "%<#pragma omp declare reduction%>", type);
37140 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37141 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37142 "%<#pragma omp declare reduction%>", type);
37143 else
37144 types.safe_push (type);
37146 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37147 cp_lexer_consume_token (parser->lexer);
37148 else
37149 break;
37152 /* Restore the saved message. */
37153 parser->type_definition_forbidden_message = saved_message;
37154 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37155 parser->colon_doesnt_start_class_def_p
37156 = saved_colon_doesnt_start_class_def_p;
37158 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37159 || types.is_empty ())
37161 fail:
37162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37163 goto done;
37166 first_token = cp_lexer_peek_token (parser->lexer);
37167 cp = NULL;
37168 errs = errorcount;
37169 FOR_EACH_VEC_ELT (types, i, type)
37171 tree fntype
37172 = build_function_type_list (void_type_node,
37173 cp_build_reference_type (type, false),
37174 NULL_TREE);
37175 tree this_reduc_id = reduc_id;
37176 if (!dependent_type_p (type))
37177 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37178 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37179 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37180 DECL_ARTIFICIAL (fndecl) = 1;
37181 DECL_EXTERNAL (fndecl) = 1;
37182 DECL_DECLARED_INLINE_P (fndecl) = 1;
37183 DECL_IGNORED_P (fndecl) = 1;
37184 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37185 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37186 DECL_ATTRIBUTES (fndecl)
37187 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37188 DECL_ATTRIBUTES (fndecl));
37189 if (processing_template_decl)
37190 fndecl = push_template_decl (fndecl);
37191 bool block_scope = false;
37192 tree block = NULL_TREE;
37193 if (current_function_decl)
37195 block_scope = true;
37196 DECL_CONTEXT (fndecl) = global_namespace;
37197 if (!processing_template_decl)
37198 pushdecl (fndecl);
37200 else if (current_class_type)
37202 if (cp == NULL)
37204 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37205 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37206 cp_lexer_consume_token (parser->lexer);
37207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37208 goto fail;
37209 cp = cp_token_cache_new (first_token,
37210 cp_lexer_peek_nth_token (parser->lexer,
37211 2));
37213 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37214 finish_member_declaration (fndecl);
37215 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37216 DECL_PENDING_INLINE_P (fndecl) = 1;
37217 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37218 continue;
37220 else
37222 DECL_CONTEXT (fndecl) = current_namespace;
37223 pushdecl (fndecl);
37225 if (!block_scope)
37226 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37227 else
37228 block = begin_omp_structured_block ();
37229 if (cp)
37231 cp_parser_push_lexer_for_tokens (parser, cp);
37232 parser->lexer->in_pragma = true;
37234 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37236 if (!block_scope)
37237 finish_function (0);
37238 else
37239 DECL_CONTEXT (fndecl) = current_function_decl;
37240 if (cp)
37241 cp_parser_pop_lexer (parser);
37242 goto fail;
37244 if (cp)
37245 cp_parser_pop_lexer (parser);
37246 if (!block_scope)
37247 finish_function (0);
37248 else
37250 DECL_CONTEXT (fndecl) = current_function_decl;
37251 block = finish_omp_structured_block (block);
37252 if (TREE_CODE (block) == BIND_EXPR)
37253 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37254 else if (TREE_CODE (block) == STATEMENT_LIST)
37255 DECL_SAVED_TREE (fndecl) = block;
37256 if (processing_template_decl)
37257 add_decl_expr (fndecl);
37259 cp_check_omp_declare_reduction (fndecl);
37260 if (cp == NULL && types.length () > 1)
37261 cp = cp_token_cache_new (first_token,
37262 cp_lexer_peek_nth_token (parser->lexer, 2));
37263 if (errs != errorcount)
37264 break;
37267 cp_parser_require_pragma_eol (parser, pragma_tok);
37269 done:
37270 /* Free any declarators allocated. */
37271 obstack_free (&declarator_obstack, p);
37274 /* OpenMP 4.0
37275 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37276 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37277 initializer-clause[opt] new-line
37278 #pragma omp declare target new-line */
37280 static void
37281 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37282 enum pragma_context context)
37284 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37286 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37287 const char *p = IDENTIFIER_POINTER (id);
37289 if (strcmp (p, "simd") == 0)
37291 cp_lexer_consume_token (parser->lexer);
37292 cp_parser_omp_declare_simd (parser, pragma_tok,
37293 context);
37294 return;
37296 cp_ensure_no_omp_declare_simd (parser);
37297 if (strcmp (p, "reduction") == 0)
37299 cp_lexer_consume_token (parser->lexer);
37300 cp_parser_omp_declare_reduction (parser, pragma_tok,
37301 context);
37302 return;
37304 if (!flag_openmp) /* flag_openmp_simd */
37306 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37307 return;
37309 if (strcmp (p, "target") == 0)
37311 cp_lexer_consume_token (parser->lexer);
37312 cp_parser_omp_declare_target (parser, pragma_tok);
37313 return;
37316 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37317 "or %<target%>");
37318 cp_parser_require_pragma_eol (parser, pragma_tok);
37321 /* OpenMP 4.5:
37322 #pragma omp taskloop taskloop-clause[optseq] new-line
37323 for-loop
37325 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37326 for-loop */
37328 #define OMP_TASKLOOP_CLAUSE_MASK \
37329 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37334 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37335 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37344 static tree
37345 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37346 char *p_name, omp_clause_mask mask, tree *cclauses,
37347 bool *if_p)
37349 tree clauses, sb, ret;
37350 unsigned int save;
37351 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37353 strcat (p_name, " taskloop");
37354 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37356 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37358 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37359 const char *p = IDENTIFIER_POINTER (id);
37361 if (strcmp (p, "simd") == 0)
37363 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37364 if (cclauses == NULL)
37365 cclauses = cclauses_buf;
37367 cp_lexer_consume_token (parser->lexer);
37368 if (!flag_openmp) /* flag_openmp_simd */
37369 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37370 cclauses, if_p);
37371 sb = begin_omp_structured_block ();
37372 save = cp_parser_begin_omp_structured_block (parser);
37373 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37374 cclauses, if_p);
37375 cp_parser_end_omp_structured_block (parser, save);
37376 tree body = finish_omp_structured_block (sb);
37377 if (ret == NULL)
37378 return ret;
37379 ret = make_node (OMP_TASKLOOP);
37380 TREE_TYPE (ret) = void_type_node;
37381 OMP_FOR_BODY (ret) = body;
37382 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37383 SET_EXPR_LOCATION (ret, loc);
37384 add_stmt (ret);
37385 return ret;
37388 if (!flag_openmp) /* flag_openmp_simd */
37390 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37391 return NULL_TREE;
37394 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37395 cclauses == NULL);
37396 if (cclauses)
37398 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37399 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37402 sb = begin_omp_structured_block ();
37403 save = cp_parser_begin_omp_structured_block (parser);
37405 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37406 if_p);
37408 cp_parser_end_omp_structured_block (parser, save);
37409 add_stmt (finish_omp_structured_block (sb));
37411 return ret;
37415 /* OpenACC 2.0:
37416 # pragma acc routine oacc-routine-clause[optseq] new-line
37417 function-definition
37419 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37422 #define OACC_ROUTINE_CLAUSE_MASK \
37423 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37424 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37425 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37426 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37429 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37430 component, which must resolve to a declared namespace-scope
37431 function. The clauses are either processed directly (for a named
37432 function), or defered until the immediatley following declaration
37433 is parsed. */
37435 static void
37436 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37437 enum pragma_context context)
37439 gcc_checking_assert (context == pragma_external);
37440 /* The checking for "another pragma following this one" in the "no optional
37441 '( name )'" case makes sure that we dont re-enter. */
37442 gcc_checking_assert (parser->oacc_routine == NULL);
37444 cp_oacc_routine_data data;
37445 data.error_seen = false;
37446 data.fndecl_seen = false;
37447 data.tokens = vNULL;
37448 data.clauses = NULL_TREE;
37449 data.loc = pragma_tok->location;
37450 /* It is safe to take the address of a local variable; it will only be
37451 used while this scope is live. */
37452 parser->oacc_routine = &data;
37454 /* Look for optional '( name )'. */
37455 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37457 cp_lexer_consume_token (parser->lexer); /* '(' */
37459 /* We parse the name as an id-expression. If it resolves to
37460 anything other than a non-overloaded function at namespace
37461 scope, it's an error. */
37462 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37463 tree name = cp_parser_id_expression (parser,
37464 /*template_keyword_p=*/false,
37465 /*check_dependency_p=*/false,
37466 /*template_p=*/NULL,
37467 /*declarator_p=*/false,
37468 /*optional_p=*/false);
37469 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37470 if (name != error_mark_node && decl == error_mark_node)
37471 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37473 if (decl == error_mark_node
37474 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37476 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37477 parser->oacc_routine = NULL;
37478 return;
37481 data.clauses
37482 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37483 "#pragma acc routine",
37484 cp_lexer_peek_token (parser->lexer));
37486 if (decl && is_overloaded_fn (decl)
37487 && (TREE_CODE (decl) != FUNCTION_DECL
37488 || DECL_FUNCTION_TEMPLATE_P (decl)))
37490 error_at (name_loc,
37491 "%<#pragma acc routine%> names a set of overloads");
37492 parser->oacc_routine = NULL;
37493 return;
37496 /* Perhaps we should use the same rule as declarations in different
37497 namespaces? */
37498 if (!DECL_NAMESPACE_SCOPE_P (decl))
37500 error_at (name_loc,
37501 "%qD does not refer to a namespace scope function", decl);
37502 parser->oacc_routine = NULL;
37503 return;
37506 if (TREE_CODE (decl) != FUNCTION_DECL)
37508 error_at (name_loc, "%qD does not refer to a function", decl);
37509 parser->oacc_routine = NULL;
37510 return;
37513 cp_finalize_oacc_routine (parser, decl, false);
37514 parser->oacc_routine = NULL;
37516 else /* No optional '( name )'. */
37518 /* Store away all pragma tokens. */
37519 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37520 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37521 cp_lexer_consume_token (parser->lexer);
37522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37523 parser->oacc_routine->error_seen = true;
37524 cp_parser_require_pragma_eol (parser, pragma_tok);
37525 struct cp_token_cache *cp
37526 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37527 parser->oacc_routine->tokens.safe_push (cp);
37529 /* Emit a helpful diagnostic if there's another pragma following this
37530 one. */
37531 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37533 cp_ensure_no_oacc_routine (parser);
37534 data.tokens.release ();
37535 /* ..., and then just keep going. */
37536 return;
37539 /* We only have to consider the pragma_external case here. */
37540 cp_parser_declaration (parser);
37541 if (parser->oacc_routine
37542 && !parser->oacc_routine->fndecl_seen)
37543 cp_ensure_no_oacc_routine (parser);
37544 else
37545 parser->oacc_routine = NULL;
37546 data.tokens.release ();
37550 /* Finalize #pragma acc routine clauses after direct declarator has
37551 been parsed. */
37553 static tree
37554 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37556 struct cp_token_cache *ce;
37557 cp_oacc_routine_data *data = parser->oacc_routine;
37559 if (!data->error_seen && data->fndecl_seen)
37561 error_at (data->loc,
37562 "%<#pragma acc routine%> not immediately followed by "
37563 "a single function declaration or definition");
37564 data->error_seen = true;
37566 if (data->error_seen)
37567 return attrs;
37569 gcc_checking_assert (data->tokens.length () == 1);
37570 ce = data->tokens[0];
37572 cp_parser_push_lexer_for_tokens (parser, ce);
37573 parser->lexer->in_pragma = true;
37574 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37576 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37577 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37578 parser->oacc_routine->clauses
37579 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37580 "#pragma acc routine", pragma_tok);
37581 cp_parser_pop_lexer (parser);
37582 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37583 fndecl_seen. */
37585 return attrs;
37588 /* Apply any saved OpenACC routine clauses to a just-parsed
37589 declaration. */
37591 static void
37592 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37594 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37596 /* Keep going if we're in error reporting mode. */
37597 if (parser->oacc_routine->error_seen
37598 || fndecl == error_mark_node)
37599 return;
37601 if (parser->oacc_routine->fndecl_seen)
37603 error_at (parser->oacc_routine->loc,
37604 "%<#pragma acc routine%> not immediately followed by"
37605 " a single function declaration or definition");
37606 parser->oacc_routine = NULL;
37607 return;
37609 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37611 cp_ensure_no_oacc_routine (parser);
37612 return;
37615 if (oacc_get_fn_attrib (fndecl))
37617 error_at (parser->oacc_routine->loc,
37618 "%<#pragma acc routine%> already applied to %qD", fndecl);
37619 parser->oacc_routine = NULL;
37620 return;
37623 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37625 error_at (parser->oacc_routine->loc,
37626 TREE_USED (fndecl)
37627 ? G_("%<#pragma acc routine%> must be applied before use")
37628 : G_("%<#pragma acc routine%> must be applied before "
37629 "definition"));
37630 parser->oacc_routine = NULL;
37631 return;
37634 /* Process the routine's dimension clauses. */
37635 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37636 oacc_replace_fn_attrib (fndecl, dims);
37638 /* Add an "omp declare target" attribute. */
37639 DECL_ATTRIBUTES (fndecl)
37640 = tree_cons (get_identifier ("omp declare target"),
37641 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37643 /* Don't unset parser->oacc_routine here: we may still need it to
37644 diagnose wrong usage. But, remember that we've used this "#pragma acc
37645 routine". */
37646 parser->oacc_routine->fndecl_seen = true;
37650 /* Main entry point to OpenMP statement pragmas. */
37652 static void
37653 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37655 tree stmt;
37656 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37657 omp_clause_mask mask (0);
37659 switch (cp_parser_pragma_kind (pragma_tok))
37661 case PRAGMA_OACC_ATOMIC:
37662 cp_parser_omp_atomic (parser, pragma_tok);
37663 return;
37664 case PRAGMA_OACC_CACHE:
37665 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37666 break;
37667 case PRAGMA_OACC_DATA:
37668 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37669 break;
37670 case PRAGMA_OACC_ENTER_DATA:
37671 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37672 break;
37673 case PRAGMA_OACC_EXIT_DATA:
37674 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37675 break;
37676 case PRAGMA_OACC_HOST_DATA:
37677 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37678 break;
37679 case PRAGMA_OACC_KERNELS:
37680 case PRAGMA_OACC_PARALLEL:
37681 strcpy (p_name, "#pragma acc");
37682 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37683 if_p);
37684 break;
37685 case PRAGMA_OACC_LOOP:
37686 strcpy (p_name, "#pragma acc");
37687 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37688 if_p);
37689 break;
37690 case PRAGMA_OACC_UPDATE:
37691 stmt = cp_parser_oacc_update (parser, pragma_tok);
37692 break;
37693 case PRAGMA_OACC_WAIT:
37694 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37695 break;
37696 case PRAGMA_OMP_ATOMIC:
37697 cp_parser_omp_atomic (parser, pragma_tok);
37698 return;
37699 case PRAGMA_OMP_CRITICAL:
37700 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37701 break;
37702 case PRAGMA_OMP_DISTRIBUTE:
37703 strcpy (p_name, "#pragma omp");
37704 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37705 if_p);
37706 break;
37707 case PRAGMA_OMP_FOR:
37708 strcpy (p_name, "#pragma omp");
37709 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37710 if_p);
37711 break;
37712 case PRAGMA_OMP_MASTER:
37713 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37714 break;
37715 case PRAGMA_OMP_PARALLEL:
37716 strcpy (p_name, "#pragma omp");
37717 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37718 if_p);
37719 break;
37720 case PRAGMA_OMP_SECTIONS:
37721 strcpy (p_name, "#pragma omp");
37722 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37723 break;
37724 case PRAGMA_OMP_SIMD:
37725 strcpy (p_name, "#pragma omp");
37726 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37727 if_p);
37728 break;
37729 case PRAGMA_OMP_SINGLE:
37730 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37731 break;
37732 case PRAGMA_OMP_TASK:
37733 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37734 break;
37735 case PRAGMA_OMP_TASKGROUP:
37736 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37737 break;
37738 case PRAGMA_OMP_TASKLOOP:
37739 strcpy (p_name, "#pragma omp");
37740 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37741 if_p);
37742 break;
37743 case PRAGMA_OMP_TEAMS:
37744 strcpy (p_name, "#pragma omp");
37745 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37746 if_p);
37747 break;
37748 default:
37749 gcc_unreachable ();
37752 protected_set_expr_location (stmt, pragma_tok->location);
37755 /* Transactional Memory parsing routines. */
37757 /* Parse a transaction attribute.
37759 txn-attribute:
37760 attribute
37761 [ [ identifier ] ]
37763 We use this instead of cp_parser_attributes_opt for transactions to avoid
37764 the pedwarn in C++98 mode. */
37766 static tree
37767 cp_parser_txn_attribute_opt (cp_parser *parser)
37769 cp_token *token;
37770 tree attr_name, attr = NULL;
37772 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37773 return cp_parser_attributes_opt (parser);
37775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37776 return NULL_TREE;
37777 cp_lexer_consume_token (parser->lexer);
37778 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37779 goto error1;
37781 token = cp_lexer_peek_token (parser->lexer);
37782 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37784 token = cp_lexer_consume_token (parser->lexer);
37786 attr_name = (token->type == CPP_KEYWORD
37787 /* For keywords, use the canonical spelling,
37788 not the parsed identifier. */
37789 ? ridpointers[(int) token->keyword]
37790 : token->u.value);
37791 attr = build_tree_list (attr_name, NULL_TREE);
37793 else
37794 cp_parser_error (parser, "expected identifier");
37796 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37797 error1:
37798 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37799 return attr;
37802 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37804 transaction-statement:
37805 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37806 compound-statement
37807 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37810 static tree
37811 cp_parser_transaction (cp_parser *parser, cp_token *token)
37813 unsigned char old_in = parser->in_transaction;
37814 unsigned char this_in = 1, new_in;
37815 enum rid keyword = token->keyword;
37816 tree stmt, attrs, noex;
37818 cp_lexer_consume_token (parser->lexer);
37820 if (keyword == RID_TRANSACTION_RELAXED
37821 || keyword == RID_SYNCHRONIZED)
37822 this_in |= TM_STMT_ATTR_RELAXED;
37823 else
37825 attrs = cp_parser_txn_attribute_opt (parser);
37826 if (attrs)
37827 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37830 /* Parse a noexcept specification. */
37831 if (keyword == RID_ATOMIC_NOEXCEPT)
37832 noex = boolean_true_node;
37833 else if (keyword == RID_ATOMIC_CANCEL)
37835 /* cancel-and-throw is unimplemented. */
37836 sorry ("atomic_cancel");
37837 noex = NULL_TREE;
37839 else
37840 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37842 /* Keep track if we're in the lexical scope of an outer transaction. */
37843 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37845 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37847 parser->in_transaction = new_in;
37848 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37849 parser->in_transaction = old_in;
37851 finish_transaction_stmt (stmt, NULL, this_in, noex);
37853 return stmt;
37856 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37858 transaction-expression:
37859 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37860 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37863 static tree
37864 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37866 unsigned char old_in = parser->in_transaction;
37867 unsigned char this_in = 1;
37868 cp_token *token;
37869 tree expr, noex;
37870 bool noex_expr;
37871 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37873 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37874 || keyword == RID_TRANSACTION_RELAXED);
37876 if (!flag_tm)
37877 error_at (loc,
37878 keyword == RID_TRANSACTION_RELAXED
37879 ? G_("%<__transaction_relaxed%> without transactional memory "
37880 "support enabled")
37881 : G_("%<__transaction_atomic%> without transactional memory "
37882 "support enabled"));
37884 token = cp_parser_require_keyword (parser, keyword,
37885 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37886 : RT_TRANSACTION_RELAXED));
37887 gcc_assert (token != NULL);
37889 if (keyword == RID_TRANSACTION_RELAXED)
37890 this_in |= TM_STMT_ATTR_RELAXED;
37892 /* Set this early. This might mean that we allow transaction_cancel in
37893 an expression that we find out later actually has to be a constexpr.
37894 However, we expect that cxx_constant_value will be able to deal with
37895 this; also, if the noexcept has no constexpr, then what we parse next
37896 really is a transaction's body. */
37897 parser->in_transaction = this_in;
37899 /* Parse a noexcept specification. */
37900 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37901 true);
37903 if (!noex || !noex_expr
37904 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37906 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37908 expr = cp_parser_expression (parser);
37909 expr = finish_parenthesized_expr (expr);
37911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37913 else
37915 /* The only expression that is available got parsed for the noexcept
37916 already. noexcept is true then. */
37917 expr = noex;
37918 noex = boolean_true_node;
37921 expr = build_transaction_expr (token->location, expr, this_in, noex);
37922 parser->in_transaction = old_in;
37924 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37925 return error_mark_node;
37927 return (flag_tm ? expr : error_mark_node);
37930 /* Parse a function-transaction-block.
37932 function-transaction-block:
37933 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37934 function-body
37935 __transaction_atomic txn-attribute[opt] function-try-block
37936 __transaction_relaxed ctor-initializer[opt] function-body
37937 __transaction_relaxed function-try-block
37940 static bool
37941 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37943 unsigned char old_in = parser->in_transaction;
37944 unsigned char new_in = 1;
37945 tree compound_stmt, stmt, attrs;
37946 bool ctor_initializer_p;
37947 cp_token *token;
37949 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37950 || keyword == RID_TRANSACTION_RELAXED);
37951 token = cp_parser_require_keyword (parser, keyword,
37952 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37953 : RT_TRANSACTION_RELAXED));
37954 gcc_assert (token != NULL);
37956 if (keyword == RID_TRANSACTION_RELAXED)
37957 new_in |= TM_STMT_ATTR_RELAXED;
37958 else
37960 attrs = cp_parser_txn_attribute_opt (parser);
37961 if (attrs)
37962 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37965 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37967 parser->in_transaction = new_in;
37969 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37970 ctor_initializer_p = cp_parser_function_try_block (parser);
37971 else
37972 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37973 (parser, /*in_function_try_block=*/false);
37975 parser->in_transaction = old_in;
37977 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37979 return ctor_initializer_p;
37982 /* Parse a __transaction_cancel statement.
37984 cancel-statement:
37985 __transaction_cancel txn-attribute[opt] ;
37986 __transaction_cancel txn-attribute[opt] throw-expression ;
37988 ??? Cancel and throw is not yet implemented. */
37990 static tree
37991 cp_parser_transaction_cancel (cp_parser *parser)
37993 cp_token *token;
37994 bool is_outer = false;
37995 tree stmt, attrs;
37997 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37998 RT_TRANSACTION_CANCEL);
37999 gcc_assert (token != NULL);
38001 attrs = cp_parser_txn_attribute_opt (parser);
38002 if (attrs)
38003 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38005 /* ??? Parse cancel-and-throw here. */
38007 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38009 if (!flag_tm)
38011 error_at (token->location, "%<__transaction_cancel%> without "
38012 "transactional memory support enabled");
38013 return error_mark_node;
38015 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38017 error_at (token->location, "%<__transaction_cancel%> within a "
38018 "%<__transaction_relaxed%>");
38019 return error_mark_node;
38021 else if (is_outer)
38023 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38024 && !is_tm_may_cancel_outer (current_function_decl))
38026 error_at (token->location, "outer %<__transaction_cancel%> not "
38027 "within outer %<__transaction_atomic%>");
38028 error_at (token->location,
38029 " or a %<transaction_may_cancel_outer%> function");
38030 return error_mark_node;
38033 else if (parser->in_transaction == 0)
38035 error_at (token->location, "%<__transaction_cancel%> not within "
38036 "%<__transaction_atomic%>");
38037 return error_mark_node;
38040 stmt = build_tm_abort_call (token->location, is_outer);
38041 add_stmt (stmt);
38043 return stmt;
38046 /* The parser. */
38048 static GTY (()) cp_parser *the_parser;
38051 /* Special handling for the first token or line in the file. The first
38052 thing in the file might be #pragma GCC pch_preprocess, which loads a
38053 PCH file, which is a GC collection point. So we need to handle this
38054 first pragma without benefit of an existing lexer structure.
38056 Always returns one token to the caller in *FIRST_TOKEN. This is
38057 either the true first token of the file, or the first token after
38058 the initial pragma. */
38060 static void
38061 cp_parser_initial_pragma (cp_token *first_token)
38063 tree name = NULL;
38065 cp_lexer_get_preprocessor_token (NULL, first_token);
38066 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38067 return;
38069 cp_lexer_get_preprocessor_token (NULL, first_token);
38070 if (first_token->type == CPP_STRING)
38072 name = first_token->u.value;
38074 cp_lexer_get_preprocessor_token (NULL, first_token);
38075 if (first_token->type != CPP_PRAGMA_EOL)
38076 error_at (first_token->location,
38077 "junk at end of %<#pragma GCC pch_preprocess%>");
38079 else
38080 error_at (first_token->location, "expected string literal");
38082 /* Skip to the end of the pragma. */
38083 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38084 cp_lexer_get_preprocessor_token (NULL, first_token);
38086 /* Now actually load the PCH file. */
38087 if (name)
38088 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38090 /* Read one more token to return to our caller. We have to do this
38091 after reading the PCH file in, since its pointers have to be
38092 live. */
38093 cp_lexer_get_preprocessor_token (NULL, first_token);
38096 /* Parses the grainsize pragma for the _Cilk_for statement.
38097 Syntax:
38098 #pragma cilk grainsize = <VALUE>. */
38100 static void
38101 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38103 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38105 tree exp = cp_parser_binary_expression (parser, false, false,
38106 PREC_NOT_OPERATOR, NULL);
38107 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38108 if (!exp || exp == error_mark_node)
38110 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38111 return;
38114 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38116 cp_parser_cilk_for (parser, exp, if_p);
38117 else
38118 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38119 "%<#pragma cilk grainsize%> is not followed by "
38120 "%<_Cilk_for%>");
38121 return;
38123 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38126 /* Normal parsing of a pragma token. Here we can (and must) use the
38127 regular lexer. */
38129 static bool
38130 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38132 cp_token *pragma_tok;
38133 unsigned int id;
38134 tree stmt;
38135 bool ret;
38137 pragma_tok = cp_lexer_consume_token (parser->lexer);
38138 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38139 parser->lexer->in_pragma = true;
38141 id = cp_parser_pragma_kind (pragma_tok);
38142 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38143 cp_ensure_no_omp_declare_simd (parser);
38144 switch (id)
38146 case PRAGMA_GCC_PCH_PREPROCESS:
38147 error_at (pragma_tok->location,
38148 "%<#pragma GCC pch_preprocess%> must be first");
38149 break;
38151 case PRAGMA_OMP_BARRIER:
38152 switch (context)
38154 case pragma_compound:
38155 cp_parser_omp_barrier (parser, pragma_tok);
38156 return false;
38157 case pragma_stmt:
38158 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38159 "used in compound statements", "omp barrier");
38160 break;
38161 default:
38162 goto bad_stmt;
38164 break;
38166 case PRAGMA_OMP_FLUSH:
38167 switch (context)
38169 case pragma_compound:
38170 cp_parser_omp_flush (parser, pragma_tok);
38171 return false;
38172 case pragma_stmt:
38173 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38174 "used in compound statements", "omp flush");
38175 break;
38176 default:
38177 goto bad_stmt;
38179 break;
38181 case PRAGMA_OMP_TASKWAIT:
38182 switch (context)
38184 case pragma_compound:
38185 cp_parser_omp_taskwait (parser, pragma_tok);
38186 return false;
38187 case pragma_stmt:
38188 error_at (pragma_tok->location,
38189 "%<#pragma %s%> may only be used in compound statements",
38190 "omp taskwait");
38191 break;
38192 default:
38193 goto bad_stmt;
38195 break;
38197 case PRAGMA_OMP_TASKYIELD:
38198 switch (context)
38200 case pragma_compound:
38201 cp_parser_omp_taskyield (parser, pragma_tok);
38202 return false;
38203 case pragma_stmt:
38204 error_at (pragma_tok->location,
38205 "%<#pragma %s%> may only be used in compound statements",
38206 "omp taskyield");
38207 break;
38208 default:
38209 goto bad_stmt;
38211 break;
38213 case PRAGMA_OMP_CANCEL:
38214 switch (context)
38216 case pragma_compound:
38217 cp_parser_omp_cancel (parser, pragma_tok);
38218 return false;
38219 case pragma_stmt:
38220 error_at (pragma_tok->location,
38221 "%<#pragma %s%> may only be used in compound statements",
38222 "omp cancel");
38223 break;
38224 default:
38225 goto bad_stmt;
38227 break;
38229 case PRAGMA_OMP_CANCELLATION_POINT:
38230 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38231 return false;
38233 case PRAGMA_OMP_THREADPRIVATE:
38234 cp_parser_omp_threadprivate (parser, pragma_tok);
38235 return false;
38237 case PRAGMA_OMP_DECLARE:
38238 cp_parser_omp_declare (parser, pragma_tok, context);
38239 return false;
38241 case PRAGMA_OACC_DECLARE:
38242 cp_parser_oacc_declare (parser, pragma_tok);
38243 return false;
38245 case PRAGMA_OACC_ENTER_DATA:
38246 if (context == pragma_stmt)
38248 error_at (pragma_tok->location,
38249 "%<#pragma %s%> may only be used in compound statements",
38250 "acc enter data");
38251 break;
38253 else if (context != pragma_compound)
38254 goto bad_stmt;
38255 cp_parser_omp_construct (parser, pragma_tok, if_p);
38256 return true;
38258 case PRAGMA_OACC_EXIT_DATA:
38259 if (context == pragma_stmt)
38261 error_at (pragma_tok->location,
38262 "%<#pragma %s%> may only be used in compound statements",
38263 "acc exit data");
38264 break;
38266 else if (context != pragma_compound)
38267 goto bad_stmt;
38268 cp_parser_omp_construct (parser, pragma_tok, if_p);
38269 return true;
38271 case PRAGMA_OACC_ROUTINE:
38272 if (context != pragma_external)
38274 error_at (pragma_tok->location,
38275 "%<#pragma acc routine%> must be at file scope");
38276 break;
38278 cp_parser_oacc_routine (parser, pragma_tok, context);
38279 return false;
38281 case PRAGMA_OACC_UPDATE:
38282 if (context == pragma_stmt)
38284 error_at (pragma_tok->location,
38285 "%<#pragma %s%> may only be used in compound statements",
38286 "acc update");
38287 break;
38289 else if (context != pragma_compound)
38290 goto bad_stmt;
38291 cp_parser_omp_construct (parser, pragma_tok, if_p);
38292 return true;
38294 case PRAGMA_OACC_WAIT:
38295 if (context == pragma_stmt)
38297 error_at (pragma_tok->location,
38298 "%<#pragma %s%> may only be used in compound statements",
38299 "acc wait");
38300 break;
38302 else if (context != pragma_compound)
38303 goto bad_stmt;
38304 cp_parser_omp_construct (parser, pragma_tok, if_p);
38305 return true;
38307 case PRAGMA_OACC_ATOMIC:
38308 case PRAGMA_OACC_CACHE:
38309 case PRAGMA_OACC_DATA:
38310 case PRAGMA_OACC_HOST_DATA:
38311 case PRAGMA_OACC_KERNELS:
38312 case PRAGMA_OACC_PARALLEL:
38313 case PRAGMA_OACC_LOOP:
38314 case PRAGMA_OMP_ATOMIC:
38315 case PRAGMA_OMP_CRITICAL:
38316 case PRAGMA_OMP_DISTRIBUTE:
38317 case PRAGMA_OMP_FOR:
38318 case PRAGMA_OMP_MASTER:
38319 case PRAGMA_OMP_PARALLEL:
38320 case PRAGMA_OMP_SECTIONS:
38321 case PRAGMA_OMP_SIMD:
38322 case PRAGMA_OMP_SINGLE:
38323 case PRAGMA_OMP_TASK:
38324 case PRAGMA_OMP_TASKGROUP:
38325 case PRAGMA_OMP_TASKLOOP:
38326 case PRAGMA_OMP_TEAMS:
38327 if (context != pragma_stmt && context != pragma_compound)
38328 goto bad_stmt;
38329 stmt = push_omp_privatization_clauses (false);
38330 cp_parser_omp_construct (parser, pragma_tok, if_p);
38331 pop_omp_privatization_clauses (stmt);
38332 return true;
38334 case PRAGMA_OMP_ORDERED:
38335 if (context != pragma_stmt && context != pragma_compound)
38336 goto bad_stmt;
38337 stmt = push_omp_privatization_clauses (false);
38338 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38339 pop_omp_privatization_clauses (stmt);
38340 return ret;
38342 case PRAGMA_OMP_TARGET:
38343 if (context != pragma_stmt && context != pragma_compound)
38344 goto bad_stmt;
38345 stmt = push_omp_privatization_clauses (false);
38346 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38347 pop_omp_privatization_clauses (stmt);
38348 return ret;
38350 case PRAGMA_OMP_END_DECLARE_TARGET:
38351 cp_parser_omp_end_declare_target (parser, pragma_tok);
38352 return false;
38354 case PRAGMA_OMP_SECTION:
38355 error_at (pragma_tok->location,
38356 "%<#pragma omp section%> may only be used in "
38357 "%<#pragma omp sections%> construct");
38358 break;
38360 case PRAGMA_IVDEP:
38362 if (context == pragma_external)
38364 error_at (pragma_tok->location,
38365 "%<#pragma GCC ivdep%> must be inside a function");
38366 break;
38368 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38369 cp_token *tok;
38370 tok = cp_lexer_peek_token (the_parser->lexer);
38371 if (tok->type != CPP_KEYWORD
38372 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38373 && tok->keyword != RID_DO))
38375 cp_parser_error (parser, "for, while or do statement expected");
38376 return false;
38378 cp_parser_iteration_statement (parser, if_p, true);
38379 return true;
38382 case PRAGMA_CILK_SIMD:
38383 if (context == pragma_external)
38385 error_at (pragma_tok->location,
38386 "%<#pragma simd%> must be inside a function");
38387 break;
38389 stmt = push_omp_privatization_clauses (false);
38390 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38391 pop_omp_privatization_clauses (stmt);
38392 return true;
38394 case PRAGMA_CILK_GRAINSIZE:
38395 if (context == pragma_external)
38397 error_at (pragma_tok->location,
38398 "%<#pragma cilk grainsize%> must be inside a function");
38399 break;
38402 /* Ignore the pragma if Cilk Plus is not enabled. */
38403 if (flag_cilkplus)
38405 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38406 return true;
38408 else
38410 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38411 "%<#pragma cilk grainsize%>");
38412 break;
38415 default:
38416 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38417 c_invoke_pragma_handler (id);
38418 break;
38420 bad_stmt:
38421 cp_parser_error (parser, "expected declaration specifiers");
38422 break;
38425 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38426 return false;
38429 /* The interface the pragma parsers have to the lexer. */
38431 enum cpp_ttype
38432 pragma_lex (tree *value, location_t *loc)
38434 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38435 enum cpp_ttype ret = tok->type;
38437 *value = tok->u.value;
38438 if (loc)
38439 *loc = tok->location;
38441 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38442 ret = CPP_EOF;
38443 else if (ret == CPP_STRING)
38444 *value = cp_parser_string_literal (the_parser, false, false);
38445 else
38447 if (ret == CPP_KEYWORD)
38448 ret = CPP_NAME;
38449 cp_lexer_consume_token (the_parser->lexer);
38452 return ret;
38456 /* External interface. */
38458 /* Parse one entire translation unit. */
38460 void
38461 c_parse_file (void)
38463 static bool already_called = false;
38465 if (already_called)
38466 fatal_error (input_location,
38467 "inter-module optimizations not implemented for C++");
38468 already_called = true;
38470 the_parser = cp_parser_new ();
38471 push_deferring_access_checks (flag_access_control
38472 ? dk_no_deferred : dk_no_check);
38473 cp_parser_translation_unit (the_parser);
38474 the_parser = NULL;
38477 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38478 vectorlength clause:
38479 Syntax:
38480 vectorlength ( constant-expression ) */
38482 static tree
38483 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38484 bool is_simd_fn)
38486 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38487 tree expr;
38488 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38489 safelen clause. Thus, vectorlength is represented as OMP 4.0
38490 safelen. For SIMD-enabled function it is represented by OMP 4.0
38491 simdlen. */
38492 if (!is_simd_fn)
38493 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38494 loc);
38495 else
38496 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38497 loc);
38499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38500 return error_mark_node;
38502 expr = cp_parser_constant_expression (parser);
38503 expr = maybe_constant_value (expr);
38505 /* If expr == error_mark_node, then don't emit any errors nor
38506 create a clause. if any of the above functions returns
38507 error mark node then they would have emitted an error message. */
38508 if (expr == error_mark_node)
38510 else if (!TREE_TYPE (expr)
38511 || !TREE_CONSTANT (expr)
38512 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38513 error_at (loc, "vectorlength must be an integer constant");
38514 else if (TREE_CONSTANT (expr)
38515 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38516 error_at (loc, "vectorlength must be a power of 2");
38517 else
38519 tree c;
38520 if (!is_simd_fn)
38522 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38523 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38524 OMP_CLAUSE_CHAIN (c) = clauses;
38525 clauses = c;
38527 else
38529 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38530 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38531 OMP_CLAUSE_CHAIN (c) = clauses;
38532 clauses = c;
38536 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38537 return error_mark_node;
38538 return clauses;
38541 /* Handles the Cilk Plus #pragma simd linear clause.
38542 Syntax:
38543 linear ( simd-linear-variable-list )
38545 simd-linear-variable-list:
38546 simd-linear-variable
38547 simd-linear-variable-list , simd-linear-variable
38549 simd-linear-variable:
38550 id-expression
38551 id-expression : simd-linear-step
38553 simd-linear-step:
38554 conditional-expression */
38556 static tree
38557 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38559 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38561 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38562 return clauses;
38563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38565 cp_parser_error (parser, "expected identifier");
38566 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38567 return error_mark_node;
38570 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38571 parser->colon_corrects_to_scope_p = false;
38572 while (1)
38574 cp_token *token = cp_lexer_peek_token (parser->lexer);
38575 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38577 cp_parser_error (parser, "expected variable-name");
38578 clauses = error_mark_node;
38579 break;
38582 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38583 false, false);
38584 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38585 token->location);
38586 if (decl == error_mark_node)
38588 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38589 token->location);
38590 clauses = error_mark_node;
38592 else
38594 tree e = NULL_TREE;
38595 tree step_size = integer_one_node;
38597 /* If present, parse the linear step. Otherwise, assume the default
38598 value of 1. */
38599 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38601 cp_lexer_consume_token (parser->lexer);
38603 e = cp_parser_assignment_expression (parser);
38604 e = maybe_constant_value (e);
38606 if (e == error_mark_node)
38608 /* If an error has occurred, then the whole pragma is
38609 considered ill-formed. Thus, no reason to keep
38610 parsing. */
38611 clauses = error_mark_node;
38612 break;
38614 else if (type_dependent_expression_p (e)
38615 || value_dependent_expression_p (e)
38616 || (TREE_TYPE (e)
38617 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38618 && (TREE_CONSTANT (e)
38619 || DECL_P (e))))
38620 step_size = e;
38621 else
38622 cp_parser_error (parser,
38623 "step size must be an integer constant "
38624 "expression or an integer variable");
38627 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38628 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38629 OMP_CLAUSE_DECL (l) = decl;
38630 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38631 OMP_CLAUSE_CHAIN (l) = clauses;
38632 clauses = l;
38634 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38635 cp_lexer_consume_token (parser->lexer);
38636 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38637 break;
38638 else
38640 error_at (cp_lexer_peek_token (parser->lexer)->location,
38641 "expected %<,%> or %<)%> after %qE", decl);
38642 clauses = error_mark_node;
38643 break;
38646 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38647 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38648 return clauses;
38651 /* Returns the name of the next clause. If the clause is not
38652 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38653 token is not consumed. Otherwise, the appropriate enum from the
38654 pragma_simd_clause is returned and the token is consumed. */
38656 static pragma_omp_clause
38657 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38659 pragma_omp_clause clause_type;
38660 cp_token *token = cp_lexer_peek_token (parser->lexer);
38662 if (token->keyword == RID_PRIVATE)
38663 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38664 else if (!token->u.value || token->type != CPP_NAME)
38665 return PRAGMA_CILK_CLAUSE_NONE;
38666 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38667 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38668 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38669 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38670 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38671 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38672 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38673 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38674 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38675 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38676 else
38677 return PRAGMA_CILK_CLAUSE_NONE;
38679 cp_lexer_consume_token (parser->lexer);
38680 return clause_type;
38683 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38685 static tree
38686 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38688 tree clauses = NULL_TREE;
38690 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38691 && clauses != error_mark_node)
38693 pragma_omp_clause c_kind;
38694 c_kind = cp_parser_cilk_simd_clause_name (parser);
38695 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38696 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38697 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38698 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38699 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38700 /* Use the OpenMP 4.0 equivalent function. */
38701 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38702 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38703 /* Use the OpenMP 4.0 equivalent function. */
38704 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38705 clauses);
38706 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38707 /* Use the OMP 4.0 equivalent function. */
38708 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38709 clauses);
38710 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38711 /* Use the OMP 4.0 equivalent function. */
38712 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38713 else
38715 clauses = error_mark_node;
38716 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38717 break;
38721 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38723 if (clauses == error_mark_node)
38724 return error_mark_node;
38725 else
38726 return finish_omp_clauses (clauses, C_ORT_CILK);
38729 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38731 static void
38732 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38734 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38736 if (clauses == error_mark_node)
38737 return;
38739 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38741 error_at (cp_lexer_peek_token (parser->lexer)->location,
38742 "for statement expected");
38743 return;
38746 tree sb = begin_omp_structured_block ();
38747 int save = cp_parser_begin_omp_structured_block (parser);
38748 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38749 if (ret)
38750 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38751 cp_parser_end_omp_structured_block (parser, save);
38752 add_stmt (finish_omp_structured_block (sb));
38755 /* Main entry-point for parsing Cilk Plus _Cilk_for
38756 loops. The return value is error_mark_node
38757 when errors happen and CILK_FOR tree on success. */
38759 static tree
38760 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38762 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38763 gcc_unreachable ();
38765 tree sb = begin_omp_structured_block ();
38766 int save = cp_parser_begin_omp_structured_block (parser);
38768 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38769 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38770 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38771 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38773 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38774 if (ret)
38775 cpp_validate_cilk_plus_loop (ret);
38776 else
38777 ret = error_mark_node;
38779 cp_parser_end_omp_structured_block (parser, save);
38780 add_stmt (finish_omp_structured_block (sb));
38781 return ret;
38784 /* Create an identifier for a generic parameter type (a synthesized
38785 template parameter implied by `auto' or a concept identifier). */
38787 static GTY(()) int generic_parm_count;
38788 static tree
38789 make_generic_type_name ()
38791 char buf[32];
38792 sprintf (buf, "auto:%d", ++generic_parm_count);
38793 return get_identifier (buf);
38796 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38797 (creating a new template parameter list if necessary). Returns the newly
38798 created template type parm. */
38800 static tree
38801 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38803 gcc_assert (current_binding_level->kind == sk_function_parms);
38805 /* Before committing to modifying any scope, if we're in an
38806 implicit template scope, and we're trying to synthesize a
38807 constrained parameter, try to find a previous parameter with
38808 the same name. This is the same-type rule for abbreviated
38809 function templates.
38811 NOTE: We can generate implicit parameters when tentatively
38812 parsing a nested name specifier, only to reject that parse
38813 later. However, matching the same template-id as part of a
38814 direct-declarator should generate an identical template
38815 parameter, so this rule will merge them. */
38816 if (parser->implicit_template_scope && constr)
38818 tree t = parser->implicit_template_parms;
38819 while (t)
38821 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38823 tree d = TREE_VALUE (t);
38824 if (TREE_CODE (d) == PARM_DECL)
38825 /* Return the TEMPLATE_PARM_INDEX. */
38826 d = DECL_INITIAL (d);
38827 return d;
38829 t = TREE_CHAIN (t);
38833 /* We are either continuing a function template that already contains implicit
38834 template parameters, creating a new fully-implicit function template, or
38835 extending an existing explicit function template with implicit template
38836 parameters. */
38838 cp_binding_level *const entry_scope = current_binding_level;
38840 bool become_template = false;
38841 cp_binding_level *parent_scope = 0;
38843 if (parser->implicit_template_scope)
38845 gcc_assert (parser->implicit_template_parms);
38847 current_binding_level = parser->implicit_template_scope;
38849 else
38851 /* Roll back to the existing template parameter scope (in the case of
38852 extending an explicit function template) or introduce a new template
38853 parameter scope ahead of the function parameter scope (or class scope
38854 in the case of out-of-line member definitions). The function scope is
38855 added back after template parameter synthesis below. */
38857 cp_binding_level *scope = entry_scope;
38859 while (scope->kind == sk_function_parms)
38861 parent_scope = scope;
38862 scope = scope->level_chain;
38864 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38866 /* If not defining a class, then any class scope is a scope level in
38867 an out-of-line member definition. In this case simply wind back
38868 beyond the first such scope to inject the template parameter list.
38869 Otherwise wind back to the class being defined. The latter can
38870 occur in class member friend declarations such as:
38872 class A {
38873 void foo (auto);
38875 class B {
38876 friend void A::foo (auto);
38879 The template parameter list synthesized for the friend declaration
38880 must be injected in the scope of 'B'. This can also occur in
38881 erroneous cases such as:
38883 struct A {
38884 struct B {
38885 void foo (auto);
38887 void B::foo (auto) {}
38890 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38891 but, nevertheless, the template parameter list synthesized for the
38892 declarator should be injected into the scope of 'A' as if the
38893 ill-formed template was specified explicitly. */
38895 while (scope->kind == sk_class && !scope->defining_class_p)
38897 parent_scope = scope;
38898 scope = scope->level_chain;
38902 current_binding_level = scope;
38904 if (scope->kind != sk_template_parms
38905 || !function_being_declared_is_template_p (parser))
38907 /* Introduce a new template parameter list for implicit template
38908 parameters. */
38910 become_template = true;
38912 parser->implicit_template_scope
38913 = begin_scope (sk_template_parms, NULL);
38915 ++processing_template_decl;
38917 parser->fully_implicit_function_template_p = true;
38918 ++parser->num_template_parameter_lists;
38920 else
38922 /* Synthesize implicit template parameters at the end of the explicit
38923 template parameter list. */
38925 gcc_assert (current_template_parms);
38927 parser->implicit_template_scope = scope;
38929 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38930 parser->implicit_template_parms
38931 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38935 /* Synthesize a new template parameter and track the current template
38936 parameter chain with implicit_template_parms. */
38938 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38939 tree synth_id = make_generic_type_name ();
38940 tree synth_tmpl_parm;
38941 bool non_type = false;
38943 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38944 synth_tmpl_parm
38945 = finish_template_type_parm (class_type_node, synth_id);
38946 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38947 synth_tmpl_parm
38948 = finish_constrained_template_template_parm (proto, synth_id);
38949 else
38951 synth_tmpl_parm = copy_decl (proto);
38952 DECL_NAME (synth_tmpl_parm) = synth_id;
38953 non_type = true;
38956 // Attach the constraint to the parm before processing.
38957 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38958 TREE_TYPE (node) = constr;
38959 tree new_parm
38960 = process_template_parm (parser->implicit_template_parms,
38961 input_location,
38962 node,
38963 /*non_type=*/non_type,
38964 /*param_pack=*/false);
38966 // Chain the new parameter to the list of implicit parameters.
38967 if (parser->implicit_template_parms)
38968 parser->implicit_template_parms
38969 = TREE_CHAIN (parser->implicit_template_parms);
38970 else
38971 parser->implicit_template_parms = new_parm;
38973 tree new_decl = get_local_decls ();
38974 if (non_type)
38975 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38976 new_decl = DECL_INITIAL (new_decl);
38978 /* If creating a fully implicit function template, start the new implicit
38979 template parameter list with this synthesized type, otherwise grow the
38980 current template parameter list. */
38982 if (become_template)
38984 parent_scope->level_chain = current_binding_level;
38986 tree new_parms = make_tree_vec (1);
38987 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38988 current_template_parms = tree_cons (size_int (processing_template_decl),
38989 new_parms, current_template_parms);
38991 else
38993 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38994 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38995 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38996 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38999 // If the new parameter was constrained, we need to add that to the
39000 // constraints in the template parameter list.
39001 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39003 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39004 reqs = conjoin_constraints (reqs, req);
39005 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39008 current_binding_level = entry_scope;
39010 return new_decl;
39013 /* Finish the declaration of a fully implicit function template. Such a
39014 template has no explicit template parameter list so has not been through the
39015 normal template head and tail processing. synthesize_implicit_template_parm
39016 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39017 provided if the declaration is a class member such that its template
39018 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39019 form is returned. Otherwise NULL_TREE is returned. */
39021 static tree
39022 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39024 gcc_assert (parser->fully_implicit_function_template_p);
39026 if (member_decl_opt && member_decl_opt != error_mark_node
39027 && DECL_VIRTUAL_P (member_decl_opt))
39029 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39030 "implicit templates may not be %<virtual%>");
39031 DECL_VIRTUAL_P (member_decl_opt) = false;
39034 if (member_decl_opt)
39035 member_decl_opt = finish_member_template_decl (member_decl_opt);
39036 end_template_decl ();
39038 parser->fully_implicit_function_template_p = false;
39039 --parser->num_template_parameter_lists;
39041 return member_decl_opt;
39044 #include "gt-cp-parser.h"