* tree-scalar-evolution.c (iv_can_overflow_p): New function.
[official-gcc.git] / gcc / cp / parser.c
blobef35aa9cfb05bea32683c2812c8cb9f4989ac504
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2016 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 "omp-low.h"
39 #include "gomp-constants.h"
40 #include "c-family/c-indentation.h"
41 #include "context.h"
42 #include "cp-cilkplus.h"
45 /* The lexer. */
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token =
52 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
55 /* The various kinds of non integral constant we encounter. */
56 enum non_integral_constant {
57 NIC_NONE,
58 /* floating-point literal */
59 NIC_FLOAT,
60 /* %<this%> */
61 NIC_THIS,
62 /* %<__FUNCTION__%> */
63 NIC_FUNC_NAME,
64 /* %<__PRETTY_FUNCTION__%> */
65 NIC_PRETTY_FUNC,
66 /* %<__func__%> */
67 NIC_C99_FUNC,
68 /* "%<va_arg%> */
69 NIC_VA_ARG,
70 /* a cast */
71 NIC_CAST,
72 /* %<typeid%> operator */
73 NIC_TYPEID,
74 /* non-constant compound literals */
75 NIC_NCC,
76 /* a function call */
77 NIC_FUNC_CALL,
78 /* an increment */
79 NIC_INC,
80 /* an decrement */
81 NIC_DEC,
82 /* an array reference */
83 NIC_ARRAY_REF,
84 /* %<->%> */
85 NIC_ARROW,
86 /* %<.%> */
87 NIC_POINT,
88 /* the address of a label */
89 NIC_ADDR_LABEL,
90 /* %<*%> */
91 NIC_STAR,
92 /* %<&%> */
93 NIC_ADDR,
94 /* %<++%> */
95 NIC_PREINCREMENT,
96 /* %<--%> */
97 NIC_PREDECREMENT,
98 /* %<new%> */
99 NIC_NEW,
100 /* %<delete%> */
101 NIC_DEL,
102 /* calls to overloaded operators */
103 NIC_OVERLOADED,
104 /* an assignment */
105 NIC_ASSIGNMENT,
106 /* a comma operator */
107 NIC_COMMA,
108 /* a call to a constructor */
109 NIC_CONSTRUCTOR,
110 /* a transaction expression */
111 NIC_TRANSACTION
114 /* The various kinds of errors about name-lookup failing. */
115 enum name_lookup_error {
116 /* NULL */
117 NLE_NULL,
118 /* is not a type */
119 NLE_TYPE,
120 /* is not a class or namespace */
121 NLE_CXX98,
122 /* is not a class, namespace, or enumeration */
123 NLE_NOT_CXX98
126 /* The various kinds of required token */
127 enum required_token {
128 RT_NONE,
129 RT_SEMICOLON, /* ';' */
130 RT_OPEN_PAREN, /* '(' */
131 RT_CLOSE_BRACE, /* '}' */
132 RT_OPEN_BRACE, /* '{' */
133 RT_CLOSE_SQUARE, /* ']' */
134 RT_OPEN_SQUARE, /* '[' */
135 RT_COMMA, /* ',' */
136 RT_SCOPE, /* '::' */
137 RT_LESS, /* '<' */
138 RT_GREATER, /* '>' */
139 RT_EQ, /* '=' */
140 RT_ELLIPSIS, /* '...' */
141 RT_MULT, /* '*' */
142 RT_COMPL, /* '~' */
143 RT_COLON, /* ':' */
144 RT_COLON_SCOPE, /* ':' or '::' */
145 RT_CLOSE_PAREN, /* ')' */
146 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147 RT_PRAGMA_EOL, /* end of line */
148 RT_NAME, /* identifier */
150 /* The type is CPP_KEYWORD */
151 RT_NEW, /* new */
152 RT_DELETE, /* delete */
153 RT_RETURN, /* return */
154 RT_WHILE, /* while */
155 RT_EXTERN, /* extern */
156 RT_STATIC_ASSERT, /* static_assert */
157 RT_DECLTYPE, /* decltype */
158 RT_OPERATOR, /* operator */
159 RT_CLASS, /* class */
160 RT_TEMPLATE, /* template */
161 RT_NAMESPACE, /* namespace */
162 RT_USING, /* using */
163 RT_ASM, /* asm */
164 RT_TRY, /* try */
165 RT_CATCH, /* catch */
166 RT_THROW, /* throw */
167 RT_LABEL, /* __label__ */
168 RT_AT_TRY, /* @try */
169 RT_AT_SYNCHRONIZED, /* @synchronized */
170 RT_AT_THROW, /* @throw */
172 RT_SELECT, /* selection-statement */
173 RT_INTERATION, /* iteration-statement */
174 RT_JUMP, /* jump-statement */
175 RT_CLASS_KEY, /* class-key */
176 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179 RT_TRANSACTION_CANCEL /* __transaction_cancel */
182 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
183 reverting it on destruction. */
185 class type_id_in_expr_sentinel
187 cp_parser *parser;
188 bool saved;
189 public:
190 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
191 : parser (parser),
192 saved (parser->in_type_id_in_expr_p)
193 { parser->in_type_id_in_expr_p = set; }
194 ~type_id_in_expr_sentinel ()
195 { parser->in_type_id_in_expr_p = saved; }
198 /* Prototypes. */
200 static cp_lexer *cp_lexer_new_main
201 (void);
202 static cp_lexer *cp_lexer_new_from_tokens
203 (cp_token_cache *tokens);
204 static void cp_lexer_destroy
205 (cp_lexer *);
206 static int cp_lexer_saving_tokens
207 (const cp_lexer *);
208 static cp_token *cp_lexer_token_at
209 (cp_lexer *, cp_token_position);
210 static void cp_lexer_get_preprocessor_token
211 (cp_lexer *, cp_token *);
212 static inline cp_token *cp_lexer_peek_token
213 (cp_lexer *);
214 static cp_token *cp_lexer_peek_nth_token
215 (cp_lexer *, size_t);
216 static inline bool cp_lexer_next_token_is
217 (cp_lexer *, enum cpp_ttype);
218 static bool cp_lexer_next_token_is_not
219 (cp_lexer *, enum cpp_ttype);
220 static bool cp_lexer_next_token_is_keyword
221 (cp_lexer *, enum rid);
222 static cp_token *cp_lexer_consume_token
223 (cp_lexer *);
224 static void cp_lexer_purge_token
225 (cp_lexer *);
226 static void cp_lexer_purge_tokens_after
227 (cp_lexer *, cp_token_position);
228 static void cp_lexer_save_tokens
229 (cp_lexer *);
230 static void cp_lexer_commit_tokens
231 (cp_lexer *);
232 static void cp_lexer_rollback_tokens
233 (cp_lexer *);
234 static void cp_lexer_print_token
235 (FILE *, cp_token *);
236 static inline bool cp_lexer_debugging_p
237 (cp_lexer *);
238 static void cp_lexer_start_debugging
239 (cp_lexer *) ATTRIBUTE_UNUSED;
240 static void cp_lexer_stop_debugging
241 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static cp_token_cache *cp_token_cache_new
244 (cp_token *, cp_token *);
246 static void cp_parser_initial_pragma
247 (cp_token *);
249 static tree cp_literal_operator_id
250 (const char *);
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 != lexer->buffer->address ());
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 the next token is a keyword for a decl-specifier. */
942 static bool
943 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
945 cp_token *token;
947 token = cp_lexer_peek_token (lexer);
948 switch (token->keyword)
950 /* auto specifier: storage-class-specifier in C++,
951 simple-type-specifier in C++0x. */
952 case RID_AUTO:
953 /* Storage classes. */
954 case RID_REGISTER:
955 case RID_STATIC:
956 case RID_EXTERN:
957 case RID_MUTABLE:
958 case RID_THREAD:
959 /* Elaborated type specifiers. */
960 case RID_ENUM:
961 case RID_CLASS:
962 case RID_STRUCT:
963 case RID_UNION:
964 case RID_TYPENAME:
965 /* Simple type specifiers. */
966 case RID_CHAR:
967 case RID_CHAR16:
968 case RID_CHAR32:
969 case RID_WCHAR:
970 case RID_BOOL:
971 case RID_SHORT:
972 case RID_INT:
973 case RID_LONG:
974 case RID_SIGNED:
975 case RID_UNSIGNED:
976 case RID_FLOAT:
977 case RID_DOUBLE:
978 case RID_VOID:
979 /* GNU extensions. */
980 case RID_ATTRIBUTE:
981 case RID_TYPEOF:
982 /* C++0x extensions. */
983 case RID_DECLTYPE:
984 case RID_UNDERLYING_TYPE:
985 return true;
987 default:
988 if (token->keyword >= RID_FIRST_INT_N
989 && token->keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
990 && int_n_enabled_p[token->keyword - RID_FIRST_INT_N])
991 return true;
992 return false;
996 /* Returns TRUE iff the token T begins a decltype type. */
998 static bool
999 token_is_decltype (cp_token *t)
1001 return (t->keyword == RID_DECLTYPE
1002 || t->type == CPP_DECLTYPE);
1005 /* Returns TRUE iff the next token begins a decltype type. */
1007 static bool
1008 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1010 cp_token *t = cp_lexer_peek_token (lexer);
1011 return token_is_decltype (t);
1014 /* Called when processing a token with tree_check_value; perform or defer the
1015 associated checks and return the value. */
1017 static tree
1018 saved_checks_value (struct tree_check *check_value)
1020 /* Perform any access checks that were deferred. */
1021 vec<deferred_access_check, va_gc> *checks;
1022 deferred_access_check *chk;
1023 checks = check_value->checks;
1024 if (checks)
1026 int i;
1027 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1028 perform_or_defer_access_check (chk->binfo,
1029 chk->decl,
1030 chk->diag_decl, tf_warning_or_error);
1032 /* Return the stored value. */
1033 return check_value->value;
1036 /* Return a pointer to the Nth token in the token stream. If N is 1,
1037 then this is precisely equivalent to cp_lexer_peek_token (except
1038 that it is not inline). One would like to disallow that case, but
1039 there is one case (cp_parser_nth_token_starts_template_id) where
1040 the caller passes a variable for N and it might be 1. */
1042 static cp_token *
1043 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1045 cp_token *token;
1047 /* N is 1-based, not zero-based. */
1048 gcc_assert (n > 0);
1050 if (cp_lexer_debugging_p (lexer))
1051 fprintf (cp_lexer_debug_stream,
1052 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1054 --n;
1055 token = lexer->next_token;
1056 gcc_assert (!n || token != &eof_token);
1057 while (n != 0)
1059 ++token;
1060 if (token == lexer->last_token)
1062 token = &eof_token;
1063 break;
1066 if (!token->purged_p)
1067 --n;
1070 if (cp_lexer_debugging_p (lexer))
1072 cp_lexer_print_token (cp_lexer_debug_stream, token);
1073 putc ('\n', cp_lexer_debug_stream);
1076 return token;
1079 /* Return the next token, and advance the lexer's next_token pointer
1080 to point to the next non-purged token. */
1082 static cp_token *
1083 cp_lexer_consume_token (cp_lexer* lexer)
1085 cp_token *token = lexer->next_token;
1087 gcc_assert (token != &eof_token);
1088 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1092 lexer->next_token++;
1093 if (lexer->next_token == lexer->last_token)
1095 lexer->next_token = &eof_token;
1096 break;
1100 while (lexer->next_token->purged_p);
1102 cp_lexer_set_source_position_from_token (token);
1104 /* Provide debugging output. */
1105 if (cp_lexer_debugging_p (lexer))
1107 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1108 cp_lexer_print_token (cp_lexer_debug_stream, token);
1109 putc ('\n', cp_lexer_debug_stream);
1112 return token;
1115 /* Permanently remove the next token from the token stream, and
1116 advance the next_token pointer to refer to the next non-purged
1117 token. */
1119 static void
1120 cp_lexer_purge_token (cp_lexer *lexer)
1122 cp_token *tok = lexer->next_token;
1124 gcc_assert (tok != &eof_token);
1125 tok->purged_p = true;
1126 tok->location = UNKNOWN_LOCATION;
1127 tok->u.value = NULL_TREE;
1128 tok->keyword = RID_MAX;
1132 tok++;
1133 if (tok == lexer->last_token)
1135 tok = &eof_token;
1136 break;
1139 while (tok->purged_p);
1140 lexer->next_token = tok;
1143 /* Permanently remove all tokens after TOK, up to, but not
1144 including, the token that will be returned next by
1145 cp_lexer_peek_token. */
1147 static void
1148 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1150 cp_token *peek = lexer->next_token;
1152 if (peek == &eof_token)
1153 peek = lexer->last_token;
1155 gcc_assert (tok < peek);
1157 for ( tok += 1; tok != peek; tok += 1)
1159 tok->purged_p = true;
1160 tok->location = UNKNOWN_LOCATION;
1161 tok->u.value = NULL_TREE;
1162 tok->keyword = RID_MAX;
1166 /* Begin saving tokens. All tokens consumed after this point will be
1167 preserved. */
1169 static void
1170 cp_lexer_save_tokens (cp_lexer* lexer)
1172 /* Provide debugging output. */
1173 if (cp_lexer_debugging_p (lexer))
1174 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1176 lexer->saved_tokens.safe_push (lexer->next_token);
1179 /* Commit to the portion of the token stream most recently saved. */
1181 static void
1182 cp_lexer_commit_tokens (cp_lexer* lexer)
1184 /* Provide debugging output. */
1185 if (cp_lexer_debugging_p (lexer))
1186 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1188 lexer->saved_tokens.pop ();
1191 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1192 to the token stream. Stop saving tokens. */
1194 static void
1195 cp_lexer_rollback_tokens (cp_lexer* lexer)
1197 /* Provide debugging output. */
1198 if (cp_lexer_debugging_p (lexer))
1199 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1201 lexer->next_token = lexer->saved_tokens.pop ();
1204 /* RAII wrapper around the above functions, with sanity checking. Creating
1205 a variable saves tokens, which are committed when the variable is
1206 destroyed unless they are explicitly rolled back by calling the rollback
1207 member function. */
1209 struct saved_token_sentinel
1211 cp_lexer *lexer;
1212 unsigned len;
1213 bool commit;
1214 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1216 len = lexer->saved_tokens.length ();
1217 cp_lexer_save_tokens (lexer);
1219 void rollback ()
1221 cp_lexer_rollback_tokens (lexer);
1222 commit = false;
1224 ~saved_token_sentinel()
1226 if (commit)
1227 cp_lexer_commit_tokens (lexer);
1228 gcc_assert (lexer->saved_tokens.length () == len);
1232 /* Print a representation of the TOKEN on the STREAM. */
1234 static void
1235 cp_lexer_print_token (FILE * stream, cp_token *token)
1237 /* We don't use cpp_type2name here because the parser defines
1238 a few tokens of its own. */
1239 static const char *const token_names[] = {
1240 /* cpplib-defined token types */
1241 #define OP(e, s) #e,
1242 #define TK(e, s) #e,
1243 TTYPE_TABLE
1244 #undef OP
1245 #undef TK
1246 /* C++ parser token types - see "Manifest constants", above. */
1247 "KEYWORD",
1248 "TEMPLATE_ID",
1249 "NESTED_NAME_SPECIFIER",
1252 /* For some tokens, print the associated data. */
1253 switch (token->type)
1255 case CPP_KEYWORD:
1256 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1257 For example, `struct' is mapped to an INTEGER_CST. */
1258 if (!identifier_p (token->u.value))
1259 break;
1260 /* else fall through */
1261 case CPP_NAME:
1262 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1263 break;
1265 case CPP_STRING:
1266 case CPP_STRING16:
1267 case CPP_STRING32:
1268 case CPP_WSTRING:
1269 case CPP_UTF8STRING:
1270 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1271 break;
1273 case CPP_NUMBER:
1274 print_generic_expr (stream, token->u.value, 0);
1275 break;
1277 default:
1278 /* If we have a name for the token, print it out. Otherwise, we
1279 simply give the numeric code. */
1280 if (token->type < ARRAY_SIZE(token_names))
1281 fputs (token_names[token->type], stream);
1282 else
1283 fprintf (stream, "[%d]", token->type);
1284 break;
1288 DEBUG_FUNCTION void
1289 debug (cp_token &ref)
1291 cp_lexer_print_token (stderr, &ref);
1292 fprintf (stderr, "\n");
1295 DEBUG_FUNCTION void
1296 debug (cp_token *ptr)
1298 if (ptr)
1299 debug (*ptr);
1300 else
1301 fprintf (stderr, "<nil>\n");
1305 /* Start emitting debugging information. */
1307 static void
1308 cp_lexer_start_debugging (cp_lexer* lexer)
1310 if (!LEXER_DEBUGGING_ENABLED_P)
1311 fatal_error (input_location,
1312 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1314 lexer->debugging_p = true;
1315 cp_lexer_debug_stream = stderr;
1318 /* Stop emitting debugging information. */
1320 static void
1321 cp_lexer_stop_debugging (cp_lexer* lexer)
1323 if (!LEXER_DEBUGGING_ENABLED_P)
1324 fatal_error (input_location,
1325 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1327 lexer->debugging_p = false;
1328 cp_lexer_debug_stream = NULL;
1331 /* Create a new cp_token_cache, representing a range of tokens. */
1333 static cp_token_cache *
1334 cp_token_cache_new (cp_token *first, cp_token *last)
1336 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1337 cache->first = first;
1338 cache->last = last;
1339 return cache;
1342 /* Diagnose if #pragma omp declare simd isn't followed immediately
1343 by function declaration or definition. */
1345 static inline void
1346 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1348 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1350 error ("%<#pragma omp declare simd%> not immediately followed by "
1351 "function declaration or definition");
1352 parser->omp_declare_simd = NULL;
1356 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1357 and put that into "omp declare simd" attribute. */
1359 static inline void
1360 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1362 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1364 if (fndecl == error_mark_node)
1366 parser->omp_declare_simd = NULL;
1367 return;
1369 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1371 cp_ensure_no_omp_declare_simd (parser);
1372 return;
1377 /* Diagnose if #pragma acc routine isn't followed immediately by function
1378 declaration or definition. */
1380 static inline void
1381 cp_ensure_no_oacc_routine (cp_parser *parser)
1383 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1385 tree clauses = parser->oacc_routine->clauses;
1386 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE (clauses));
1388 error_at (loc, "%<#pragma acc routine%> not followed by a function "
1389 "declaration or definition");
1390 parser->oacc_routine = NULL;
1394 /* Decl-specifiers. */
1396 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1398 static void
1399 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1401 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1404 /* Declarators. */
1406 /* Nothing other than the parser should be creating declarators;
1407 declarators are a semi-syntactic representation of C++ entities.
1408 Other parts of the front end that need to create entities (like
1409 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1411 static cp_declarator *make_call_declarator
1412 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1413 static cp_declarator *make_array_declarator
1414 (cp_declarator *, tree);
1415 static cp_declarator *make_pointer_declarator
1416 (cp_cv_quals, cp_declarator *, tree);
1417 static cp_declarator *make_reference_declarator
1418 (cp_cv_quals, cp_declarator *, bool, tree);
1419 static cp_declarator *make_ptrmem_declarator
1420 (cp_cv_quals, tree, cp_declarator *, tree);
1422 /* An erroneous declarator. */
1423 static cp_declarator *cp_error_declarator;
1425 /* The obstack on which declarators and related data structures are
1426 allocated. */
1427 static struct obstack declarator_obstack;
1429 /* Alloc BYTES from the declarator memory pool. */
1431 static inline void *
1432 alloc_declarator (size_t bytes)
1434 return obstack_alloc (&declarator_obstack, bytes);
1437 /* Allocate a declarator of the indicated KIND. Clear fields that are
1438 common to all declarators. */
1440 static cp_declarator *
1441 make_declarator (cp_declarator_kind kind)
1443 cp_declarator *declarator;
1445 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1446 declarator->kind = kind;
1447 declarator->attributes = NULL_TREE;
1448 declarator->std_attributes = NULL_TREE;
1449 declarator->declarator = NULL;
1450 declarator->parameter_pack_p = false;
1451 declarator->id_loc = UNKNOWN_LOCATION;
1453 return declarator;
1456 /* Make a declarator for a generalized identifier. If
1457 QUALIFYING_SCOPE is non-NULL, the identifier is
1458 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1459 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1460 is, if any. */
1462 static cp_declarator *
1463 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1464 special_function_kind sfk)
1466 cp_declarator *declarator;
1468 /* It is valid to write:
1470 class C { void f(); };
1471 typedef C D;
1472 void D::f();
1474 The standard is not clear about whether `typedef const C D' is
1475 legal; as of 2002-09-15 the committee is considering that
1476 question. EDG 3.0 allows that syntax. Therefore, we do as
1477 well. */
1478 if (qualifying_scope && TYPE_P (qualifying_scope))
1479 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1481 gcc_assert (identifier_p (unqualified_name)
1482 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1483 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1485 declarator = make_declarator (cdk_id);
1486 declarator->u.id.qualifying_scope = qualifying_scope;
1487 declarator->u.id.unqualified_name = unqualified_name;
1488 declarator->u.id.sfk = sfk;
1490 return declarator;
1493 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1494 of modifiers such as const or volatile to apply to the pointer
1495 type, represented as identifiers. ATTRIBUTES represent the attributes that
1496 appertain to the pointer or reference. */
1498 cp_declarator *
1499 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1500 tree attributes)
1502 cp_declarator *declarator;
1504 declarator = make_declarator (cdk_pointer);
1505 declarator->declarator = target;
1506 declarator->u.pointer.qualifiers = cv_qualifiers;
1507 declarator->u.pointer.class_type = NULL_TREE;
1508 if (target)
1510 declarator->id_loc = target->id_loc;
1511 declarator->parameter_pack_p = target->parameter_pack_p;
1512 target->parameter_pack_p = false;
1514 else
1515 declarator->parameter_pack_p = false;
1517 declarator->std_attributes = attributes;
1519 return declarator;
1522 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1523 represent the attributes that appertain to the pointer or
1524 reference. */
1526 cp_declarator *
1527 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1528 bool rvalue_ref, tree attributes)
1530 cp_declarator *declarator;
1532 declarator = make_declarator (cdk_reference);
1533 declarator->declarator = target;
1534 declarator->u.reference.qualifiers = cv_qualifiers;
1535 declarator->u.reference.rvalue_ref = rvalue_ref;
1536 if (target)
1538 declarator->id_loc = target->id_loc;
1539 declarator->parameter_pack_p = target->parameter_pack_p;
1540 target->parameter_pack_p = false;
1542 else
1543 declarator->parameter_pack_p = false;
1545 declarator->std_attributes = attributes;
1547 return declarator;
1550 /* Like make_pointer_declarator -- but for a pointer to a non-static
1551 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1552 appertain to the pointer or reference. */
1554 cp_declarator *
1555 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1556 cp_declarator *pointee,
1557 tree attributes)
1559 cp_declarator *declarator;
1561 declarator = make_declarator (cdk_ptrmem);
1562 declarator->declarator = pointee;
1563 declarator->u.pointer.qualifiers = cv_qualifiers;
1564 declarator->u.pointer.class_type = class_type;
1566 if (pointee)
1568 declarator->parameter_pack_p = pointee->parameter_pack_p;
1569 pointee->parameter_pack_p = false;
1571 else
1572 declarator->parameter_pack_p = false;
1574 declarator->std_attributes = attributes;
1576 return declarator;
1579 /* Make a declarator for the function given by TARGET, with the
1580 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1581 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1582 indicates what exceptions can be thrown. */
1584 cp_declarator *
1585 make_call_declarator (cp_declarator *target,
1586 tree parms,
1587 cp_cv_quals cv_qualifiers,
1588 cp_virt_specifiers virt_specifiers,
1589 cp_ref_qualifier ref_qualifier,
1590 tree tx_qualifier,
1591 tree exception_specification,
1592 tree late_return_type,
1593 tree requires_clause)
1595 cp_declarator *declarator;
1597 declarator = make_declarator (cdk_function);
1598 declarator->declarator = target;
1599 declarator->u.function.parameters = parms;
1600 declarator->u.function.qualifiers = cv_qualifiers;
1601 declarator->u.function.virt_specifiers = virt_specifiers;
1602 declarator->u.function.ref_qualifier = ref_qualifier;
1603 declarator->u.function.tx_qualifier = tx_qualifier;
1604 declarator->u.function.exception_specification = exception_specification;
1605 declarator->u.function.late_return_type = late_return_type;
1606 declarator->u.function.requires_clause = requires_clause;
1607 if (target)
1609 declarator->id_loc = target->id_loc;
1610 declarator->parameter_pack_p = target->parameter_pack_p;
1611 target->parameter_pack_p = false;
1613 else
1614 declarator->parameter_pack_p = false;
1616 return declarator;
1619 /* Make a declarator for an array of BOUNDS elements, each of which is
1620 defined by ELEMENT. */
1622 cp_declarator *
1623 make_array_declarator (cp_declarator *element, tree bounds)
1625 cp_declarator *declarator;
1627 declarator = make_declarator (cdk_array);
1628 declarator->declarator = element;
1629 declarator->u.array.bounds = bounds;
1630 if (element)
1632 declarator->id_loc = element->id_loc;
1633 declarator->parameter_pack_p = element->parameter_pack_p;
1634 element->parameter_pack_p = false;
1636 else
1637 declarator->parameter_pack_p = false;
1639 return declarator;
1642 /* Determine whether the declarator we've seen so far can be a
1643 parameter pack, when followed by an ellipsis. */
1644 static bool
1645 declarator_can_be_parameter_pack (cp_declarator *declarator)
1647 if (declarator && declarator->parameter_pack_p)
1648 /* We already saw an ellipsis. */
1649 return false;
1651 /* Search for a declarator name, or any other declarator that goes
1652 after the point where the ellipsis could appear in a parameter
1653 pack. If we find any of these, then this declarator can not be
1654 made into a parameter pack. */
1655 bool found = false;
1656 while (declarator && !found)
1658 switch ((int)declarator->kind)
1660 case cdk_id:
1661 case cdk_array:
1662 found = true;
1663 break;
1665 case cdk_error:
1666 return true;
1668 default:
1669 declarator = declarator->declarator;
1670 break;
1674 return !found;
1677 cp_parameter_declarator *no_parameters;
1679 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1680 DECLARATOR and DEFAULT_ARGUMENT. */
1682 cp_parameter_declarator *
1683 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1684 cp_declarator *declarator,
1685 tree default_argument,
1686 bool template_parameter_pack_p = false)
1688 cp_parameter_declarator *parameter;
1690 parameter = ((cp_parameter_declarator *)
1691 alloc_declarator (sizeof (cp_parameter_declarator)));
1692 parameter->next = NULL;
1693 if (decl_specifiers)
1694 parameter->decl_specifiers = *decl_specifiers;
1695 else
1696 clear_decl_specs (&parameter->decl_specifiers);
1697 parameter->declarator = declarator;
1698 parameter->default_argument = default_argument;
1699 parameter->template_parameter_pack_p = template_parameter_pack_p;
1701 return parameter;
1704 /* Returns true iff DECLARATOR is a declaration for a function. */
1706 static bool
1707 function_declarator_p (const cp_declarator *declarator)
1709 while (declarator)
1711 if (declarator->kind == cdk_function
1712 && declarator->declarator->kind == cdk_id)
1713 return true;
1714 if (declarator->kind == cdk_id
1715 || declarator->kind == cdk_error)
1716 return false;
1717 declarator = declarator->declarator;
1719 return false;
1722 /* The parser. */
1724 /* Overview
1725 --------
1727 A cp_parser parses the token stream as specified by the C++
1728 grammar. Its job is purely parsing, not semantic analysis. For
1729 example, the parser breaks the token stream into declarators,
1730 expressions, statements, and other similar syntactic constructs.
1731 It does not check that the types of the expressions on either side
1732 of an assignment-statement are compatible, or that a function is
1733 not declared with a parameter of type `void'.
1735 The parser invokes routines elsewhere in the compiler to perform
1736 semantic analysis and to build up the abstract syntax tree for the
1737 code processed.
1739 The parser (and the template instantiation code, which is, in a
1740 way, a close relative of parsing) are the only parts of the
1741 compiler that should be calling push_scope and pop_scope, or
1742 related functions. The parser (and template instantiation code)
1743 keeps track of what scope is presently active; everything else
1744 should simply honor that. (The code that generates static
1745 initializers may also need to set the scope, in order to check
1746 access control correctly when emitting the initializers.)
1748 Methodology
1749 -----------
1751 The parser is of the standard recursive-descent variety. Upcoming
1752 tokens in the token stream are examined in order to determine which
1753 production to use when parsing a non-terminal. Some C++ constructs
1754 require arbitrary look ahead to disambiguate. For example, it is
1755 impossible, in the general case, to tell whether a statement is an
1756 expression or declaration without scanning the entire statement.
1757 Therefore, the parser is capable of "parsing tentatively." When the
1758 parser is not sure what construct comes next, it enters this mode.
1759 Then, while we attempt to parse the construct, the parser queues up
1760 error messages, rather than issuing them immediately, and saves the
1761 tokens it consumes. If the construct is parsed successfully, the
1762 parser "commits", i.e., it issues any queued error messages and
1763 the tokens that were being preserved are permanently discarded.
1764 If, however, the construct is not parsed successfully, the parser
1765 rolls back its state completely so that it can resume parsing using
1766 a different alternative.
1768 Future Improvements
1769 -------------------
1771 The performance of the parser could probably be improved substantially.
1772 We could often eliminate the need to parse tentatively by looking ahead
1773 a little bit. In some places, this approach might not entirely eliminate
1774 the need to parse tentatively, but it might still speed up the average
1775 case. */
1777 /* Flags that are passed to some parsing functions. These values can
1778 be bitwise-ored together. */
1780 enum
1782 /* No flags. */
1783 CP_PARSER_FLAGS_NONE = 0x0,
1784 /* The construct is optional. If it is not present, then no error
1785 should be issued. */
1786 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1787 /* When parsing a type-specifier, treat user-defined type-names
1788 as non-type identifiers. */
1789 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1790 /* When parsing a type-specifier, do not try to parse a class-specifier
1791 or enum-specifier. */
1792 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1793 /* When parsing a decl-specifier-seq, only allow type-specifier or
1794 constexpr. */
1795 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1798 /* This type is used for parameters and variables which hold
1799 combinations of the above flags. */
1800 typedef int cp_parser_flags;
1802 /* The different kinds of declarators we want to parse. */
1804 enum cp_parser_declarator_kind
1806 /* We want an abstract declarator. */
1807 CP_PARSER_DECLARATOR_ABSTRACT,
1808 /* We want a named declarator. */
1809 CP_PARSER_DECLARATOR_NAMED,
1810 /* We don't mind, but the name must be an unqualified-id. */
1811 CP_PARSER_DECLARATOR_EITHER
1814 /* The precedence values used to parse binary expressions. The minimum value
1815 of PREC must be 1, because zero is reserved to quickly discriminate
1816 binary operators from other tokens. */
1818 enum cp_parser_prec
1820 PREC_NOT_OPERATOR,
1821 PREC_LOGICAL_OR_EXPRESSION,
1822 PREC_LOGICAL_AND_EXPRESSION,
1823 PREC_INCLUSIVE_OR_EXPRESSION,
1824 PREC_EXCLUSIVE_OR_EXPRESSION,
1825 PREC_AND_EXPRESSION,
1826 PREC_EQUALITY_EXPRESSION,
1827 PREC_RELATIONAL_EXPRESSION,
1828 PREC_SHIFT_EXPRESSION,
1829 PREC_ADDITIVE_EXPRESSION,
1830 PREC_MULTIPLICATIVE_EXPRESSION,
1831 PREC_PM_EXPRESSION,
1832 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1835 /* A mapping from a token type to a corresponding tree node type, with a
1836 precedence value. */
1838 struct cp_parser_binary_operations_map_node
1840 /* The token type. */
1841 enum cpp_ttype token_type;
1842 /* The corresponding tree code. */
1843 enum tree_code tree_type;
1844 /* The precedence of this operator. */
1845 enum cp_parser_prec prec;
1848 struct cp_parser_expression_stack_entry
1850 /* Left hand side of the binary operation we are currently
1851 parsing. */
1852 cp_expr lhs;
1853 /* Original tree code for left hand side, if it was a binary
1854 expression itself (used for -Wparentheses). */
1855 enum tree_code lhs_type;
1856 /* Tree code for the binary operation we are parsing. */
1857 enum tree_code tree_type;
1858 /* Precedence of the binary operation we are parsing. */
1859 enum cp_parser_prec prec;
1860 /* Location of the binary operation we are parsing. */
1861 location_t loc;
1864 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1865 entries because precedence levels on the stack are monotonically
1866 increasing. */
1867 typedef struct cp_parser_expression_stack_entry
1868 cp_parser_expression_stack[NUM_PREC_VALUES];
1870 /* Prototypes. */
1872 /* Constructors and destructors. */
1874 static cp_parser_context *cp_parser_context_new
1875 (cp_parser_context *);
1877 /* Class variables. */
1879 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1881 /* The operator-precedence table used by cp_parser_binary_expression.
1882 Transformed into an associative array (binops_by_token) by
1883 cp_parser_new. */
1885 static const cp_parser_binary_operations_map_node binops[] = {
1886 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1887 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1889 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1890 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1891 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1893 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1894 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1896 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1897 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1899 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1900 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1901 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1902 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1904 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1905 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1907 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1909 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1911 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1913 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1915 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1918 /* The same as binops, but initialized by cp_parser_new so that
1919 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1920 for speed. */
1921 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1923 /* Constructors and destructors. */
1925 /* Construct a new context. The context below this one on the stack
1926 is given by NEXT. */
1928 static cp_parser_context *
1929 cp_parser_context_new (cp_parser_context* next)
1931 cp_parser_context *context;
1933 /* Allocate the storage. */
1934 if (cp_parser_context_free_list != NULL)
1936 /* Pull the first entry from the free list. */
1937 context = cp_parser_context_free_list;
1938 cp_parser_context_free_list = context->next;
1939 memset (context, 0, sizeof (*context));
1941 else
1942 context = ggc_cleared_alloc<cp_parser_context> ();
1944 /* No errors have occurred yet in this context. */
1945 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1946 /* If this is not the bottommost context, copy information that we
1947 need from the previous context. */
1948 if (next)
1950 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1951 expression, then we are parsing one in this context, too. */
1952 context->object_type = next->object_type;
1953 /* Thread the stack. */
1954 context->next = next;
1957 return context;
1960 /* Managing the unparsed function queues. */
1962 #define unparsed_funs_with_default_args \
1963 parser->unparsed_queues->last ().funs_with_default_args
1964 #define unparsed_funs_with_definitions \
1965 parser->unparsed_queues->last ().funs_with_definitions
1966 #define unparsed_nsdmis \
1967 parser->unparsed_queues->last ().nsdmis
1968 #define unparsed_classes \
1969 parser->unparsed_queues->last ().classes
1971 static void
1972 push_unparsed_function_queues (cp_parser *parser)
1974 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1975 vec_safe_push (parser->unparsed_queues, e);
1978 static void
1979 pop_unparsed_function_queues (cp_parser *parser)
1981 release_tree_vector (unparsed_funs_with_definitions);
1982 parser->unparsed_queues->pop ();
1985 /* Prototypes. */
1987 /* Constructors and destructors. */
1989 static cp_parser *cp_parser_new
1990 (void);
1992 /* Routines to parse various constructs.
1994 Those that return `tree' will return the error_mark_node (rather
1995 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1996 Sometimes, they will return an ordinary node if error-recovery was
1997 attempted, even though a parse error occurred. So, to check
1998 whether or not a parse error occurred, you should always use
1999 cp_parser_error_occurred. If the construct is optional (indicated
2000 either by an `_opt' in the name of the function that does the
2001 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2002 the construct is not present. */
2004 /* Lexical conventions [gram.lex] */
2006 static cp_expr cp_parser_identifier
2007 (cp_parser *);
2008 static cp_expr cp_parser_string_literal
2009 (cp_parser *, bool, bool, bool);
2010 static cp_expr cp_parser_userdef_char_literal
2011 (cp_parser *);
2012 static tree cp_parser_userdef_string_literal
2013 (tree);
2014 static cp_expr cp_parser_userdef_numeric_literal
2015 (cp_parser *);
2017 /* Basic concepts [gram.basic] */
2019 static bool cp_parser_translation_unit
2020 (cp_parser *);
2022 /* Expressions [gram.expr] */
2024 static cp_expr cp_parser_primary_expression
2025 (cp_parser *, bool, bool, bool, cp_id_kind *);
2026 static cp_expr cp_parser_id_expression
2027 (cp_parser *, bool, bool, bool *, bool, bool);
2028 static cp_expr cp_parser_unqualified_id
2029 (cp_parser *, bool, bool, bool, bool);
2030 static tree cp_parser_nested_name_specifier_opt
2031 (cp_parser *, bool, bool, bool, bool);
2032 static tree cp_parser_nested_name_specifier
2033 (cp_parser *, bool, bool, bool, bool);
2034 static tree cp_parser_qualifying_entity
2035 (cp_parser *, bool, bool, bool, bool, bool);
2036 static cp_expr cp_parser_postfix_expression
2037 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2038 static tree cp_parser_postfix_open_square_expression
2039 (cp_parser *, tree, bool, bool);
2040 static tree cp_parser_postfix_dot_deref_expression
2041 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2042 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2043 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2044 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2045 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2046 static void cp_parser_pseudo_destructor_name
2047 (cp_parser *, tree, tree *, tree *);
2048 static cp_expr cp_parser_unary_expression
2049 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2050 static enum tree_code cp_parser_unary_operator
2051 (cp_token *);
2052 static tree cp_parser_new_expression
2053 (cp_parser *);
2054 static vec<tree, va_gc> *cp_parser_new_placement
2055 (cp_parser *);
2056 static tree cp_parser_new_type_id
2057 (cp_parser *, tree *);
2058 static cp_declarator *cp_parser_new_declarator_opt
2059 (cp_parser *);
2060 static cp_declarator *cp_parser_direct_new_declarator
2061 (cp_parser *);
2062 static vec<tree, va_gc> *cp_parser_new_initializer
2063 (cp_parser *);
2064 static tree cp_parser_delete_expression
2065 (cp_parser *);
2066 static cp_expr cp_parser_cast_expression
2067 (cp_parser *, bool, bool, bool, cp_id_kind *);
2068 static cp_expr cp_parser_binary_expression
2069 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2070 static tree cp_parser_question_colon_clause
2071 (cp_parser *, cp_expr);
2072 static cp_expr cp_parser_assignment_expression
2073 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2074 static enum tree_code cp_parser_assignment_operator_opt
2075 (cp_parser *);
2076 static cp_expr cp_parser_expression
2077 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2078 static cp_expr cp_parser_constant_expression
2079 (cp_parser *, bool = false, bool * = NULL);
2080 static cp_expr cp_parser_builtin_offsetof
2081 (cp_parser *);
2082 static cp_expr cp_parser_lambda_expression
2083 (cp_parser *);
2084 static void cp_parser_lambda_introducer
2085 (cp_parser *, tree);
2086 static bool cp_parser_lambda_declarator_opt
2087 (cp_parser *, tree);
2088 static void cp_parser_lambda_body
2089 (cp_parser *, tree);
2091 /* Statements [gram.stmt.stmt] */
2093 static void cp_parser_statement
2094 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2095 static void cp_parser_label_for_labeled_statement
2096 (cp_parser *, tree);
2097 static tree cp_parser_expression_statement
2098 (cp_parser *, tree);
2099 static tree cp_parser_compound_statement
2100 (cp_parser *, tree, int, bool);
2101 static void cp_parser_statement_seq_opt
2102 (cp_parser *, tree);
2103 static tree cp_parser_selection_statement
2104 (cp_parser *, bool *, vec<tree> *);
2105 static tree cp_parser_condition
2106 (cp_parser *);
2107 static tree cp_parser_iteration_statement
2108 (cp_parser *, bool *, bool);
2109 static bool cp_parser_for_init_statement
2110 (cp_parser *, tree *decl);
2111 static tree cp_parser_for
2112 (cp_parser *, bool);
2113 static tree cp_parser_c_for
2114 (cp_parser *, tree, tree, bool);
2115 static tree cp_parser_range_for
2116 (cp_parser *, tree, tree, tree, bool);
2117 static void do_range_for_auto_deduction
2118 (tree, tree);
2119 static tree cp_parser_perform_range_for_lookup
2120 (tree, tree *, tree *);
2121 static tree cp_parser_range_for_member_function
2122 (tree, tree);
2123 static tree cp_parser_jump_statement
2124 (cp_parser *);
2125 static void cp_parser_declaration_statement
2126 (cp_parser *);
2128 static tree cp_parser_implicitly_scoped_statement
2129 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2130 static void cp_parser_already_scoped_statement
2131 (cp_parser *, bool *, const token_indent_info &);
2133 /* Declarations [gram.dcl.dcl] */
2135 static void cp_parser_declaration_seq_opt
2136 (cp_parser *);
2137 static void cp_parser_declaration
2138 (cp_parser *);
2139 static void cp_parser_block_declaration
2140 (cp_parser *, bool);
2141 static void cp_parser_simple_declaration
2142 (cp_parser *, bool, tree *);
2143 static void cp_parser_decl_specifier_seq
2144 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2145 static tree cp_parser_storage_class_specifier_opt
2146 (cp_parser *);
2147 static tree cp_parser_function_specifier_opt
2148 (cp_parser *, cp_decl_specifier_seq *);
2149 static tree cp_parser_type_specifier
2150 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2151 int *, bool *);
2152 static tree cp_parser_simple_type_specifier
2153 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2154 static tree cp_parser_type_name
2155 (cp_parser *, bool);
2156 static tree cp_parser_type_name
2157 (cp_parser *);
2158 static tree cp_parser_nonclass_name
2159 (cp_parser* parser);
2160 static tree cp_parser_elaborated_type_specifier
2161 (cp_parser *, bool, bool);
2162 static tree cp_parser_enum_specifier
2163 (cp_parser *);
2164 static void cp_parser_enumerator_list
2165 (cp_parser *, tree);
2166 static void cp_parser_enumerator_definition
2167 (cp_parser *, tree);
2168 static tree cp_parser_namespace_name
2169 (cp_parser *);
2170 static void cp_parser_namespace_definition
2171 (cp_parser *);
2172 static void cp_parser_namespace_body
2173 (cp_parser *);
2174 static tree cp_parser_qualified_namespace_specifier
2175 (cp_parser *);
2176 static void cp_parser_namespace_alias_definition
2177 (cp_parser *);
2178 static bool cp_parser_using_declaration
2179 (cp_parser *, bool);
2180 static void cp_parser_using_directive
2181 (cp_parser *);
2182 static tree cp_parser_alias_declaration
2183 (cp_parser *);
2184 static void cp_parser_asm_definition
2185 (cp_parser *);
2186 static void cp_parser_linkage_specification
2187 (cp_parser *);
2188 static void cp_parser_static_assert
2189 (cp_parser *, bool);
2190 static tree cp_parser_decltype
2191 (cp_parser *);
2193 /* Declarators [gram.dcl.decl] */
2195 static tree cp_parser_init_declarator
2196 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2197 bool, bool, int, bool *, tree *, location_t *, tree *);
2198 static cp_declarator *cp_parser_declarator
2199 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2200 static cp_declarator *cp_parser_direct_declarator
2201 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2202 static enum tree_code cp_parser_ptr_operator
2203 (cp_parser *, tree *, cp_cv_quals *, tree *);
2204 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2205 (cp_parser *);
2206 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2207 (cp_parser *);
2208 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2209 (cp_parser *);
2210 static tree cp_parser_tx_qualifier_opt
2211 (cp_parser *);
2212 static tree cp_parser_late_return_type_opt
2213 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2214 static tree cp_parser_declarator_id
2215 (cp_parser *, bool);
2216 static tree cp_parser_type_id
2217 (cp_parser *);
2218 static tree cp_parser_template_type_arg
2219 (cp_parser *);
2220 static tree cp_parser_trailing_type_id (cp_parser *);
2221 static tree cp_parser_type_id_1
2222 (cp_parser *, bool, bool);
2223 static void cp_parser_type_specifier_seq
2224 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2225 static tree cp_parser_parameter_declaration_clause
2226 (cp_parser *);
2227 static tree cp_parser_parameter_declaration_list
2228 (cp_parser *, bool *);
2229 static cp_parameter_declarator *cp_parser_parameter_declaration
2230 (cp_parser *, bool, bool *);
2231 static tree cp_parser_default_argument
2232 (cp_parser *, bool);
2233 static void cp_parser_function_body
2234 (cp_parser *, bool);
2235 static tree cp_parser_initializer
2236 (cp_parser *, bool *, bool *);
2237 static cp_expr cp_parser_initializer_clause
2238 (cp_parser *, bool *);
2239 static cp_expr cp_parser_braced_list
2240 (cp_parser*, bool*);
2241 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2242 (cp_parser *, bool *);
2244 static bool cp_parser_ctor_initializer_opt_and_function_body
2245 (cp_parser *, bool);
2247 static tree cp_parser_late_parsing_omp_declare_simd
2248 (cp_parser *, tree);
2250 static tree cp_parser_late_parsing_cilk_simd_fn_info
2251 (cp_parser *, tree);
2253 static tree cp_parser_late_parsing_oacc_routine
2254 (cp_parser *, tree);
2256 static tree synthesize_implicit_template_parm
2257 (cp_parser *, tree);
2258 static tree finish_fully_implicit_template
2259 (cp_parser *, tree);
2261 /* Classes [gram.class] */
2263 static tree cp_parser_class_name
2264 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2265 static tree cp_parser_class_specifier
2266 (cp_parser *);
2267 static tree cp_parser_class_head
2268 (cp_parser *, bool *);
2269 static enum tag_types cp_parser_class_key
2270 (cp_parser *);
2271 static void cp_parser_type_parameter_key
2272 (cp_parser* parser);
2273 static void cp_parser_member_specification_opt
2274 (cp_parser *);
2275 static void cp_parser_member_declaration
2276 (cp_parser *);
2277 static tree cp_parser_pure_specifier
2278 (cp_parser *);
2279 static tree cp_parser_constant_initializer
2280 (cp_parser *);
2282 /* Derived classes [gram.class.derived] */
2284 static tree cp_parser_base_clause
2285 (cp_parser *);
2286 static tree cp_parser_base_specifier
2287 (cp_parser *);
2289 /* Special member functions [gram.special] */
2291 static tree cp_parser_conversion_function_id
2292 (cp_parser *);
2293 static tree cp_parser_conversion_type_id
2294 (cp_parser *);
2295 static cp_declarator *cp_parser_conversion_declarator_opt
2296 (cp_parser *);
2297 static bool cp_parser_ctor_initializer_opt
2298 (cp_parser *);
2299 static void cp_parser_mem_initializer_list
2300 (cp_parser *);
2301 static tree cp_parser_mem_initializer
2302 (cp_parser *);
2303 static tree cp_parser_mem_initializer_id
2304 (cp_parser *);
2306 /* Overloading [gram.over] */
2308 static cp_expr cp_parser_operator_function_id
2309 (cp_parser *);
2310 static cp_expr cp_parser_operator
2311 (cp_parser *);
2313 /* Templates [gram.temp] */
2315 static void cp_parser_template_declaration
2316 (cp_parser *, bool);
2317 static tree cp_parser_template_parameter_list
2318 (cp_parser *);
2319 static tree cp_parser_template_parameter
2320 (cp_parser *, bool *, bool *);
2321 static tree cp_parser_type_parameter
2322 (cp_parser *, bool *);
2323 static tree cp_parser_template_id
2324 (cp_parser *, bool, bool, enum tag_types, bool);
2325 static tree cp_parser_template_name
2326 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2327 static tree cp_parser_template_argument_list
2328 (cp_parser *);
2329 static tree cp_parser_template_argument
2330 (cp_parser *);
2331 static void cp_parser_explicit_instantiation
2332 (cp_parser *);
2333 static void cp_parser_explicit_specialization
2334 (cp_parser *);
2336 /* Exception handling [gram.exception] */
2338 static tree cp_parser_try_block
2339 (cp_parser *);
2340 static bool cp_parser_function_try_block
2341 (cp_parser *);
2342 static void cp_parser_handler_seq
2343 (cp_parser *);
2344 static void cp_parser_handler
2345 (cp_parser *);
2346 static tree cp_parser_exception_declaration
2347 (cp_parser *);
2348 static tree cp_parser_throw_expression
2349 (cp_parser *);
2350 static tree cp_parser_exception_specification_opt
2351 (cp_parser *);
2352 static tree cp_parser_type_id_list
2353 (cp_parser *);
2355 /* GNU Extensions */
2357 static tree cp_parser_asm_specification_opt
2358 (cp_parser *);
2359 static tree cp_parser_asm_operand_list
2360 (cp_parser *);
2361 static tree cp_parser_asm_clobber_list
2362 (cp_parser *);
2363 static tree cp_parser_asm_label_list
2364 (cp_parser *);
2365 static bool cp_next_tokens_can_be_attribute_p
2366 (cp_parser *);
2367 static bool cp_next_tokens_can_be_gnu_attribute_p
2368 (cp_parser *);
2369 static bool cp_next_tokens_can_be_std_attribute_p
2370 (cp_parser *);
2371 static bool cp_nth_tokens_can_be_std_attribute_p
2372 (cp_parser *, size_t);
2373 static bool cp_nth_tokens_can_be_gnu_attribute_p
2374 (cp_parser *, size_t);
2375 static bool cp_nth_tokens_can_be_attribute_p
2376 (cp_parser *, size_t);
2377 static tree cp_parser_attributes_opt
2378 (cp_parser *);
2379 static tree cp_parser_gnu_attributes_opt
2380 (cp_parser *);
2381 static tree cp_parser_gnu_attribute_list
2382 (cp_parser *);
2383 static tree cp_parser_std_attribute
2384 (cp_parser *);
2385 static tree cp_parser_std_attribute_spec
2386 (cp_parser *);
2387 static tree cp_parser_std_attribute_spec_seq
2388 (cp_parser *);
2389 static bool cp_parser_extension_opt
2390 (cp_parser *, int *);
2391 static void cp_parser_label_declaration
2392 (cp_parser *);
2394 /* Concept Extensions */
2396 static tree cp_parser_requires_clause
2397 (cp_parser *);
2398 static tree cp_parser_requires_clause_opt
2399 (cp_parser *);
2400 static tree cp_parser_requires_expression
2401 (cp_parser *);
2402 static tree cp_parser_requirement_parameter_list
2403 (cp_parser *);
2404 static tree cp_parser_requirement_body
2405 (cp_parser *);
2406 static tree cp_parser_requirement_list
2407 (cp_parser *);
2408 static tree cp_parser_requirement
2409 (cp_parser *);
2410 static tree cp_parser_simple_requirement
2411 (cp_parser *);
2412 static tree cp_parser_compound_requirement
2413 (cp_parser *);
2414 static tree cp_parser_type_requirement
2415 (cp_parser *);
2416 static tree cp_parser_nested_requirement
2417 (cp_parser *);
2419 /* Transactional Memory Extensions */
2421 static tree cp_parser_transaction
2422 (cp_parser *, cp_token *);
2423 static tree cp_parser_transaction_expression
2424 (cp_parser *, enum rid);
2425 static bool cp_parser_function_transaction
2426 (cp_parser *, enum rid);
2427 static tree cp_parser_transaction_cancel
2428 (cp_parser *);
2430 enum pragma_context {
2431 pragma_external,
2432 pragma_member,
2433 pragma_objc_icode,
2434 pragma_stmt,
2435 pragma_compound
2437 static bool cp_parser_pragma
2438 (cp_parser *, enum pragma_context, bool *);
2440 /* Objective-C++ Productions */
2442 static tree cp_parser_objc_message_receiver
2443 (cp_parser *);
2444 static tree cp_parser_objc_message_args
2445 (cp_parser *);
2446 static tree cp_parser_objc_message_expression
2447 (cp_parser *);
2448 static cp_expr cp_parser_objc_encode_expression
2449 (cp_parser *);
2450 static tree cp_parser_objc_defs_expression
2451 (cp_parser *);
2452 static tree cp_parser_objc_protocol_expression
2453 (cp_parser *);
2454 static tree cp_parser_objc_selector_expression
2455 (cp_parser *);
2456 static cp_expr cp_parser_objc_expression
2457 (cp_parser *);
2458 static bool cp_parser_objc_selector_p
2459 (enum cpp_ttype);
2460 static tree cp_parser_objc_selector
2461 (cp_parser *);
2462 static tree cp_parser_objc_protocol_refs_opt
2463 (cp_parser *);
2464 static void cp_parser_objc_declaration
2465 (cp_parser *, tree);
2466 static tree cp_parser_objc_statement
2467 (cp_parser *);
2468 static bool cp_parser_objc_valid_prefix_attributes
2469 (cp_parser *, tree *);
2470 static void cp_parser_objc_at_property_declaration
2471 (cp_parser *) ;
2472 static void cp_parser_objc_at_synthesize_declaration
2473 (cp_parser *) ;
2474 static void cp_parser_objc_at_dynamic_declaration
2475 (cp_parser *) ;
2476 static tree cp_parser_objc_struct_declaration
2477 (cp_parser *) ;
2479 /* Utility Routines */
2481 static cp_expr cp_parser_lookup_name
2482 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2483 static tree cp_parser_lookup_name_simple
2484 (cp_parser *, tree, location_t);
2485 static tree cp_parser_maybe_treat_template_as_class
2486 (tree, bool);
2487 static bool cp_parser_check_declarator_template_parameters
2488 (cp_parser *, cp_declarator *, location_t);
2489 static bool cp_parser_check_template_parameters
2490 (cp_parser *, unsigned, location_t, cp_declarator *);
2491 static cp_expr cp_parser_simple_cast_expression
2492 (cp_parser *);
2493 static tree cp_parser_global_scope_opt
2494 (cp_parser *, bool);
2495 static bool cp_parser_constructor_declarator_p
2496 (cp_parser *, bool);
2497 static tree cp_parser_function_definition_from_specifiers_and_declarator
2498 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2499 static tree cp_parser_function_definition_after_declarator
2500 (cp_parser *, bool);
2501 static bool cp_parser_template_declaration_after_export
2502 (cp_parser *, bool);
2503 static void cp_parser_perform_template_parameter_access_checks
2504 (vec<deferred_access_check, va_gc> *);
2505 static tree cp_parser_single_declaration
2506 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2507 static cp_expr cp_parser_functional_cast
2508 (cp_parser *, tree);
2509 static tree cp_parser_save_member_function_body
2510 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2511 static tree cp_parser_save_nsdmi
2512 (cp_parser *);
2513 static tree cp_parser_enclosed_template_argument_list
2514 (cp_parser *);
2515 static void cp_parser_save_default_args
2516 (cp_parser *, tree);
2517 static void cp_parser_late_parsing_for_member
2518 (cp_parser *, tree);
2519 static tree cp_parser_late_parse_one_default_arg
2520 (cp_parser *, tree, tree, tree);
2521 static void cp_parser_late_parsing_nsdmi
2522 (cp_parser *, tree);
2523 static void cp_parser_late_parsing_default_args
2524 (cp_parser *, tree);
2525 static tree cp_parser_sizeof_operand
2526 (cp_parser *, enum rid);
2527 static tree cp_parser_trait_expr
2528 (cp_parser *, enum rid);
2529 static bool cp_parser_declares_only_class_p
2530 (cp_parser *);
2531 static void cp_parser_set_storage_class
2532 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2533 static void cp_parser_set_decl_spec_type
2534 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2535 static void set_and_check_decl_spec_loc
2536 (cp_decl_specifier_seq *decl_specs,
2537 cp_decl_spec ds, cp_token *);
2538 static bool cp_parser_friend_p
2539 (const cp_decl_specifier_seq *);
2540 static void cp_parser_required_error
2541 (cp_parser *, required_token, bool);
2542 static cp_token *cp_parser_require
2543 (cp_parser *, enum cpp_ttype, required_token);
2544 static cp_token *cp_parser_require_keyword
2545 (cp_parser *, enum rid, required_token);
2546 static bool cp_parser_token_starts_function_definition_p
2547 (cp_token *);
2548 static bool cp_parser_next_token_starts_class_definition_p
2549 (cp_parser *);
2550 static bool cp_parser_next_token_ends_template_argument_p
2551 (cp_parser *);
2552 static bool cp_parser_nth_token_starts_template_argument_list_p
2553 (cp_parser *, size_t);
2554 static enum tag_types cp_parser_token_is_class_key
2555 (cp_token *);
2556 static enum tag_types cp_parser_token_is_type_parameter_key
2557 (cp_token *);
2558 static void cp_parser_check_class_key
2559 (enum tag_types, tree type);
2560 static void cp_parser_check_access_in_redeclaration
2561 (tree type, location_t location);
2562 static bool cp_parser_optional_template_keyword
2563 (cp_parser *);
2564 static void cp_parser_pre_parsed_nested_name_specifier
2565 (cp_parser *);
2566 static bool cp_parser_cache_group
2567 (cp_parser *, enum cpp_ttype, unsigned);
2568 static tree cp_parser_cache_defarg
2569 (cp_parser *parser, bool nsdmi);
2570 static void cp_parser_parse_tentatively
2571 (cp_parser *);
2572 static void cp_parser_commit_to_tentative_parse
2573 (cp_parser *);
2574 static void cp_parser_commit_to_topmost_tentative_parse
2575 (cp_parser *);
2576 static void cp_parser_abort_tentative_parse
2577 (cp_parser *);
2578 static bool cp_parser_parse_definitely
2579 (cp_parser *);
2580 static inline bool cp_parser_parsing_tentatively
2581 (cp_parser *);
2582 static bool cp_parser_uncommitted_to_tentative_parse_p
2583 (cp_parser *);
2584 static void cp_parser_error
2585 (cp_parser *, const char *);
2586 static void cp_parser_name_lookup_error
2587 (cp_parser *, tree, tree, name_lookup_error, location_t);
2588 static bool cp_parser_simulate_error
2589 (cp_parser *);
2590 static bool cp_parser_check_type_definition
2591 (cp_parser *);
2592 static void cp_parser_check_for_definition_in_return_type
2593 (cp_declarator *, tree, location_t type_location);
2594 static void cp_parser_check_for_invalid_template_id
2595 (cp_parser *, tree, enum tag_types, location_t location);
2596 static bool cp_parser_non_integral_constant_expression
2597 (cp_parser *, non_integral_constant);
2598 static void cp_parser_diagnose_invalid_type_name
2599 (cp_parser *, tree, location_t);
2600 static bool cp_parser_parse_and_diagnose_invalid_type_name
2601 (cp_parser *);
2602 static int cp_parser_skip_to_closing_parenthesis
2603 (cp_parser *, bool, bool, bool);
2604 static void cp_parser_skip_to_end_of_statement
2605 (cp_parser *);
2606 static void cp_parser_consume_semicolon_at_end_of_statement
2607 (cp_parser *);
2608 static void cp_parser_skip_to_end_of_block_or_statement
2609 (cp_parser *);
2610 static bool cp_parser_skip_to_closing_brace
2611 (cp_parser *);
2612 static void cp_parser_skip_to_end_of_template_parameter_list
2613 (cp_parser *);
2614 static void cp_parser_skip_to_pragma_eol
2615 (cp_parser*, cp_token *);
2616 static bool cp_parser_error_occurred
2617 (cp_parser *);
2618 static bool cp_parser_allow_gnu_extensions_p
2619 (cp_parser *);
2620 static bool cp_parser_is_pure_string_literal
2621 (cp_token *);
2622 static bool cp_parser_is_string_literal
2623 (cp_token *);
2624 static bool cp_parser_is_keyword
2625 (cp_token *, enum rid);
2626 static tree cp_parser_make_typename_type
2627 (cp_parser *, tree, location_t location);
2628 static cp_declarator * cp_parser_make_indirect_declarator
2629 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2630 static bool cp_parser_compound_literal_p
2631 (cp_parser *);
2632 static bool cp_parser_array_designator_p
2633 (cp_parser *);
2634 static bool cp_parser_skip_to_closing_square_bracket
2635 (cp_parser *);
2637 /* Concept-related syntactic transformations */
2639 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2640 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2642 // -------------------------------------------------------------------------- //
2643 // Unevaluated Operand Guard
2645 // Implementation of an RAII helper for unevaluated operand parsing.
2646 cp_unevaluated::cp_unevaluated ()
2648 ++cp_unevaluated_operand;
2649 ++c_inhibit_evaluation_warnings;
2652 cp_unevaluated::~cp_unevaluated ()
2654 --c_inhibit_evaluation_warnings;
2655 --cp_unevaluated_operand;
2658 // -------------------------------------------------------------------------- //
2659 // Tentative Parsing
2661 /* Returns nonzero if we are parsing tentatively. */
2663 static inline bool
2664 cp_parser_parsing_tentatively (cp_parser* parser)
2666 return parser->context->next != NULL;
2669 /* Returns nonzero if TOKEN is a string literal. */
2671 static bool
2672 cp_parser_is_pure_string_literal (cp_token* token)
2674 return (token->type == CPP_STRING ||
2675 token->type == CPP_STRING16 ||
2676 token->type == CPP_STRING32 ||
2677 token->type == CPP_WSTRING ||
2678 token->type == CPP_UTF8STRING);
2681 /* Returns nonzero if TOKEN is a string literal
2682 of a user-defined string literal. */
2684 static bool
2685 cp_parser_is_string_literal (cp_token* token)
2687 return (cp_parser_is_pure_string_literal (token) ||
2688 token->type == CPP_STRING_USERDEF ||
2689 token->type == CPP_STRING16_USERDEF ||
2690 token->type == CPP_STRING32_USERDEF ||
2691 token->type == CPP_WSTRING_USERDEF ||
2692 token->type == CPP_UTF8STRING_USERDEF);
2695 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2697 static bool
2698 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2700 return token->keyword == keyword;
2703 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2704 PRAGMA_NONE. */
2706 static enum pragma_kind
2707 cp_parser_pragma_kind (cp_token *token)
2709 if (token->type != CPP_PRAGMA)
2710 return PRAGMA_NONE;
2711 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2712 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2715 /* Helper function for cp_parser_error.
2716 Having peeked a token of kind TOK1_KIND that might signify
2717 a conflict marker, peek successor tokens to determine
2718 if we actually do have a conflict marker.
2719 Specifically, we consider a run of 7 '<', '=' or '>' characters
2720 at the start of a line as a conflict marker.
2721 These come through the lexer as three pairs and a single,
2722 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2723 If it returns true, *OUT_LOC is written to with the location/range
2724 of the marker. */
2726 static bool
2727 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2728 location_t *out_loc)
2730 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2731 if (token2->type != tok1_kind)
2732 return false;
2733 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2734 if (token3->type != tok1_kind)
2735 return false;
2736 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2737 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2738 return false;
2740 /* It must be at the start of the line. */
2741 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2742 if (LOCATION_COLUMN (start_loc) != 1)
2743 return false;
2745 /* We have a conflict marker. Construct a location of the form:
2746 <<<<<<<
2747 ^~~~~~~
2748 with start == caret, finishing at the end of the marker. */
2749 location_t finish_loc = get_finish (token4->location);
2750 *out_loc = make_location (start_loc, start_loc, finish_loc);
2752 return true;
2755 /* If not parsing tentatively, issue a diagnostic of the form
2756 FILE:LINE: MESSAGE before TOKEN
2757 where TOKEN is the next token in the input stream. MESSAGE
2758 (specified by the caller) is usually of the form "expected
2759 OTHER-TOKEN". */
2761 static void
2762 cp_parser_error (cp_parser* parser, const char* gmsgid)
2764 if (!cp_parser_simulate_error (parser))
2766 cp_token *token = cp_lexer_peek_token (parser->lexer);
2767 /* This diagnostic makes more sense if it is tagged to the line
2768 of the token we just peeked at. */
2769 cp_lexer_set_source_position_from_token (token);
2771 if (token->type == CPP_PRAGMA)
2773 error_at (token->location,
2774 "%<#pragma%> is not allowed here");
2775 cp_parser_skip_to_pragma_eol (parser, token);
2776 return;
2779 /* If this is actually a conflict marker, report it as such. */
2780 if (token->type == CPP_LSHIFT
2781 || token->type == CPP_RSHIFT
2782 || token->type == CPP_EQ_EQ)
2784 location_t loc;
2785 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2787 error_at (loc, "version control conflict marker in file");
2788 return;
2792 c_parse_error (gmsgid,
2793 /* Because c_parser_error does not understand
2794 CPP_KEYWORD, keywords are treated like
2795 identifiers. */
2796 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2797 token->u.value, token->flags);
2801 /* Issue an error about name-lookup failing. NAME is the
2802 IDENTIFIER_NODE DECL is the result of
2803 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2804 the thing that we hoped to find. */
2806 static void
2807 cp_parser_name_lookup_error (cp_parser* parser,
2808 tree name,
2809 tree decl,
2810 name_lookup_error desired,
2811 location_t location)
2813 /* If name lookup completely failed, tell the user that NAME was not
2814 declared. */
2815 if (decl == error_mark_node)
2817 if (parser->scope && parser->scope != global_namespace)
2818 error_at (location, "%<%E::%E%> has not been declared",
2819 parser->scope, name);
2820 else if (parser->scope == global_namespace)
2821 error_at (location, "%<::%E%> has not been declared", name);
2822 else if (parser->object_scope
2823 && !CLASS_TYPE_P (parser->object_scope))
2824 error_at (location, "request for member %qE in non-class type %qT",
2825 name, parser->object_scope);
2826 else if (parser->object_scope)
2827 error_at (location, "%<%T::%E%> has not been declared",
2828 parser->object_scope, name);
2829 else
2830 error_at (location, "%qE has not been declared", name);
2832 else if (parser->scope && parser->scope != global_namespace)
2834 switch (desired)
2836 case NLE_TYPE:
2837 error_at (location, "%<%E::%E%> is not a type",
2838 parser->scope, name);
2839 break;
2840 case NLE_CXX98:
2841 error_at (location, "%<%E::%E%> is not a class or namespace",
2842 parser->scope, name);
2843 break;
2844 case NLE_NOT_CXX98:
2845 error_at (location,
2846 "%<%E::%E%> is not a class, namespace, or enumeration",
2847 parser->scope, name);
2848 break;
2849 default:
2850 gcc_unreachable ();
2854 else if (parser->scope == global_namespace)
2856 switch (desired)
2858 case NLE_TYPE:
2859 error_at (location, "%<::%E%> is not a type", name);
2860 break;
2861 case NLE_CXX98:
2862 error_at (location, "%<::%E%> is not a class or namespace", name);
2863 break;
2864 case NLE_NOT_CXX98:
2865 error_at (location,
2866 "%<::%E%> is not a class, namespace, or enumeration",
2867 name);
2868 break;
2869 default:
2870 gcc_unreachable ();
2873 else
2875 switch (desired)
2877 case NLE_TYPE:
2878 error_at (location, "%qE is not a type", name);
2879 break;
2880 case NLE_CXX98:
2881 error_at (location, "%qE is not a class or namespace", name);
2882 break;
2883 case NLE_NOT_CXX98:
2884 error_at (location,
2885 "%qE is not a class, namespace, or enumeration", name);
2886 break;
2887 default:
2888 gcc_unreachable ();
2893 /* If we are parsing tentatively, remember that an error has occurred
2894 during this tentative parse. Returns true if the error was
2895 simulated; false if a message should be issued by the caller. */
2897 static bool
2898 cp_parser_simulate_error (cp_parser* parser)
2900 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2902 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2903 return true;
2905 return false;
2908 /* This function is called when a type is defined. If type
2909 definitions are forbidden at this point, an error message is
2910 issued. */
2912 static bool
2913 cp_parser_check_type_definition (cp_parser* parser)
2915 /* If types are forbidden here, issue a message. */
2916 if (parser->type_definition_forbidden_message)
2918 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2919 in the message need to be interpreted. */
2920 error (parser->type_definition_forbidden_message);
2921 return false;
2923 return true;
2926 /* This function is called when the DECLARATOR is processed. The TYPE
2927 was a type defined in the decl-specifiers. If it is invalid to
2928 define a type in the decl-specifiers for DECLARATOR, an error is
2929 issued. TYPE_LOCATION is the location of TYPE and is used
2930 for error reporting. */
2932 static void
2933 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2934 tree type, location_t type_location)
2936 /* [dcl.fct] forbids type definitions in return types.
2937 Unfortunately, it's not easy to know whether or not we are
2938 processing a return type until after the fact. */
2939 while (declarator
2940 && (declarator->kind == cdk_pointer
2941 || declarator->kind == cdk_reference
2942 || declarator->kind == cdk_ptrmem))
2943 declarator = declarator->declarator;
2944 if (declarator
2945 && declarator->kind == cdk_function)
2947 error_at (type_location,
2948 "new types may not be defined in a return type");
2949 inform (type_location,
2950 "(perhaps a semicolon is missing after the definition of %qT)",
2951 type);
2955 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2956 "<" in any valid C++ program. If the next token is indeed "<",
2957 issue a message warning the user about what appears to be an
2958 invalid attempt to form a template-id. LOCATION is the location
2959 of the type-specifier (TYPE) */
2961 static void
2962 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2963 tree type,
2964 enum tag_types tag_type,
2965 location_t location)
2967 cp_token_position start = 0;
2969 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2971 if (TYPE_P (type))
2972 error_at (location, "%qT is not a template", type);
2973 else if (identifier_p (type))
2975 if (tag_type != none_type)
2976 error_at (location, "%qE is not a class template", type);
2977 else
2978 error_at (location, "%qE is not a template", type);
2980 else
2981 error_at (location, "invalid template-id");
2982 /* Remember the location of the invalid "<". */
2983 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2984 start = cp_lexer_token_position (parser->lexer, true);
2985 /* Consume the "<". */
2986 cp_lexer_consume_token (parser->lexer);
2987 /* Parse the template arguments. */
2988 cp_parser_enclosed_template_argument_list (parser);
2989 /* Permanently remove the invalid template arguments so that
2990 this error message is not issued again. */
2991 if (start)
2992 cp_lexer_purge_tokens_after (parser->lexer, start);
2996 /* If parsing an integral constant-expression, issue an error message
2997 about the fact that THING appeared and return true. Otherwise,
2998 return false. In either case, set
2999 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3001 static bool
3002 cp_parser_non_integral_constant_expression (cp_parser *parser,
3003 non_integral_constant thing)
3005 parser->non_integral_constant_expression_p = true;
3006 if (parser->integral_constant_expression_p)
3008 if (!parser->allow_non_integral_constant_expression_p)
3010 const char *msg = NULL;
3011 switch (thing)
3013 case NIC_FLOAT:
3014 error ("floating-point literal "
3015 "cannot appear in a constant-expression");
3016 return true;
3017 case NIC_CAST:
3018 error ("a cast to a type other than an integral or "
3019 "enumeration type cannot appear in a "
3020 "constant-expression");
3021 return true;
3022 case NIC_TYPEID:
3023 error ("%<typeid%> operator "
3024 "cannot appear in a constant-expression");
3025 return true;
3026 case NIC_NCC:
3027 error ("non-constant compound literals "
3028 "cannot appear in a constant-expression");
3029 return true;
3030 case NIC_FUNC_CALL:
3031 error ("a function call "
3032 "cannot appear in a constant-expression");
3033 return true;
3034 case NIC_INC:
3035 error ("an increment "
3036 "cannot appear in a constant-expression");
3037 return true;
3038 case NIC_DEC:
3039 error ("an decrement "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_ARRAY_REF:
3043 error ("an array reference "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_ADDR_LABEL:
3047 error ("the address of a label "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_OVERLOADED:
3051 error ("calls to overloaded operators "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_ASSIGNMENT:
3055 error ("an assignment cannot appear in a constant-expression");
3056 return true;
3057 case NIC_COMMA:
3058 error ("a comma operator "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_CONSTRUCTOR:
3062 error ("a call to a constructor "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_TRANSACTION:
3066 error ("a transaction expression "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_THIS:
3070 msg = "this";
3071 break;
3072 case NIC_FUNC_NAME:
3073 msg = "__FUNCTION__";
3074 break;
3075 case NIC_PRETTY_FUNC:
3076 msg = "__PRETTY_FUNCTION__";
3077 break;
3078 case NIC_C99_FUNC:
3079 msg = "__func__";
3080 break;
3081 case NIC_VA_ARG:
3082 msg = "va_arg";
3083 break;
3084 case NIC_ARROW:
3085 msg = "->";
3086 break;
3087 case NIC_POINT:
3088 msg = ".";
3089 break;
3090 case NIC_STAR:
3091 msg = "*";
3092 break;
3093 case NIC_ADDR:
3094 msg = "&";
3095 break;
3096 case NIC_PREINCREMENT:
3097 msg = "++";
3098 break;
3099 case NIC_PREDECREMENT:
3100 msg = "--";
3101 break;
3102 case NIC_NEW:
3103 msg = "new";
3104 break;
3105 case NIC_DEL:
3106 msg = "delete";
3107 break;
3108 default:
3109 gcc_unreachable ();
3111 if (msg)
3112 error ("%qs cannot appear in a constant-expression", msg);
3113 return true;
3116 return false;
3119 /* Emit a diagnostic for an invalid type name. This function commits
3120 to the current active tentative parse, if any. (Otherwise, the
3121 problematic construct might be encountered again later, resulting
3122 in duplicate error messages.) LOCATION is the location of ID. */
3124 static void
3125 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3126 location_t location)
3128 tree decl, ambiguous_decls;
3129 cp_parser_commit_to_tentative_parse (parser);
3130 /* Try to lookup the identifier. */
3131 decl = cp_parser_lookup_name (parser, id, none_type,
3132 /*is_template=*/false,
3133 /*is_namespace=*/false,
3134 /*check_dependency=*/true,
3135 &ambiguous_decls, location);
3136 if (ambiguous_decls)
3137 /* If the lookup was ambiguous, an error will already have
3138 been issued. */
3139 return;
3140 /* If the lookup found a template-name, it means that the user forgot
3141 to specify an argument list. Emit a useful error message. */
3142 if (DECL_TYPE_TEMPLATE_P (decl))
3144 error_at (location,
3145 "invalid use of template-name %qE without an argument list",
3146 decl);
3147 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3149 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3150 error_at (location, "invalid use of destructor %qD as a type", id);
3151 else if (TREE_CODE (decl) == TYPE_DECL)
3152 /* Something like 'unsigned A a;' */
3153 error_at (location, "invalid combination of multiple type-specifiers");
3154 else if (!parser->scope)
3156 /* Issue an error message. */
3157 error_at (location, "%qE does not name a type", id);
3158 /* If we're in a template class, it's possible that the user was
3159 referring to a type from a base class. For example:
3161 template <typename T> struct A { typedef T X; };
3162 template <typename T> struct B : public A<T> { X x; };
3164 The user should have said "typename A<T>::X". */
3165 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3166 inform (location, "C++11 %<constexpr%> only available with "
3167 "-std=c++11 or -std=gnu++11");
3168 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3169 inform (location, "C++11 %<noexcept%> only available with "
3170 "-std=c++11 or -std=gnu++11");
3171 else if (cxx_dialect < cxx11
3172 && TREE_CODE (id) == IDENTIFIER_NODE
3173 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3174 inform (location, "C++11 %<thread_local%> only available with "
3175 "-std=c++11 or -std=gnu++11");
3176 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3177 inform (location, "%<concept%> only available with -fconcepts");
3178 else if (processing_template_decl && current_class_type
3179 && TYPE_BINFO (current_class_type))
3181 tree b;
3183 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3185 b = TREE_CHAIN (b))
3187 tree base_type = BINFO_TYPE (b);
3188 if (CLASS_TYPE_P (base_type)
3189 && dependent_type_p (base_type))
3191 tree field;
3192 /* Go from a particular instantiation of the
3193 template (which will have an empty TYPE_FIELDs),
3194 to the main version. */
3195 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3196 for (field = TYPE_FIELDS (base_type);
3197 field;
3198 field = DECL_CHAIN (field))
3199 if (TREE_CODE (field) == TYPE_DECL
3200 && DECL_NAME (field) == id)
3202 inform (location,
3203 "(perhaps %<typename %T::%E%> was intended)",
3204 BINFO_TYPE (b), id);
3205 break;
3207 if (field)
3208 break;
3213 /* Here we diagnose qualified-ids where the scope is actually correct,
3214 but the identifier does not resolve to a valid type name. */
3215 else if (parser->scope != error_mark_node)
3217 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3219 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3220 error_at (location_of (id),
3221 "%qE in namespace %qE does not name a template type",
3222 id, parser->scope);
3223 else
3224 error_at (location_of (id),
3225 "%qE in namespace %qE does not name a type",
3226 id, parser->scope);
3227 if (DECL_P (decl))
3228 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3230 else if (CLASS_TYPE_P (parser->scope)
3231 && constructor_name_p (id, parser->scope))
3233 /* A<T>::A<T>() */
3234 error_at (location, "%<%T::%E%> names the constructor, not"
3235 " the type", parser->scope, id);
3236 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3237 error_at (location, "and %qT has no template constructors",
3238 parser->scope);
3240 else if (TYPE_P (parser->scope)
3241 && dependent_scope_p (parser->scope))
3242 error_at (location, "need %<typename%> before %<%T::%E%> because "
3243 "%qT is a dependent scope",
3244 parser->scope, id, parser->scope);
3245 else if (TYPE_P (parser->scope))
3247 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3248 error_at (location_of (id),
3249 "%qE in %q#T does not name a template type",
3250 id, parser->scope);
3251 else
3252 error_at (location_of (id),
3253 "%qE in %q#T does not name a type",
3254 id, parser->scope);
3255 if (DECL_P (decl))
3256 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3258 else
3259 gcc_unreachable ();
3263 /* Check for a common situation where a type-name should be present,
3264 but is not, and issue a sensible error message. Returns true if an
3265 invalid type-name was detected.
3267 The situation handled by this function are variable declarations of the
3268 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3269 Usually, `ID' should name a type, but if we got here it means that it
3270 does not. We try to emit the best possible error message depending on
3271 how exactly the id-expression looks like. */
3273 static bool
3274 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3276 tree id;
3277 cp_token *token = cp_lexer_peek_token (parser->lexer);
3279 /* Avoid duplicate error about ambiguous lookup. */
3280 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3282 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3283 if (next->type == CPP_NAME && next->error_reported)
3284 goto out;
3287 cp_parser_parse_tentatively (parser);
3288 id = cp_parser_id_expression (parser,
3289 /*template_keyword_p=*/false,
3290 /*check_dependency_p=*/true,
3291 /*template_p=*/NULL,
3292 /*declarator_p=*/true,
3293 /*optional_p=*/false);
3294 /* If the next token is a (, this is a function with no explicit return
3295 type, i.e. constructor, destructor or conversion op. */
3296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3297 || TREE_CODE (id) == TYPE_DECL)
3299 cp_parser_abort_tentative_parse (parser);
3300 return false;
3302 if (!cp_parser_parse_definitely (parser))
3303 return false;
3305 /* Emit a diagnostic for the invalid type. */
3306 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3307 out:
3308 /* If we aren't in the middle of a declarator (i.e. in a
3309 parameter-declaration-clause), skip to the end of the declaration;
3310 there's no point in trying to process it. */
3311 if (!parser->in_declarator_p)
3312 cp_parser_skip_to_end_of_block_or_statement (parser);
3313 return true;
3316 /* Consume tokens up to, and including, the next non-nested closing `)'.
3317 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3318 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3319 found an unnested token of that type. */
3321 static int
3322 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3323 bool recovering,
3324 cpp_ttype or_ttype,
3325 bool consume_paren)
3327 unsigned paren_depth = 0;
3328 unsigned brace_depth = 0;
3329 unsigned square_depth = 0;
3331 if (recovering && or_ttype == CPP_EOF
3332 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3333 return 0;
3335 while (true)
3337 cp_token * token = cp_lexer_peek_token (parser->lexer);
3339 /* Have we found what we're looking for before the closing paren? */
3340 if (token->type == or_ttype && or_ttype != CPP_EOF
3341 && !brace_depth && !paren_depth && !square_depth)
3342 return -1;
3344 switch (token->type)
3346 case CPP_EOF:
3347 case CPP_PRAGMA_EOL:
3348 /* If we've run out of tokens, then there is no closing `)'. */
3349 return 0;
3351 /* This is good for lambda expression capture-lists. */
3352 case CPP_OPEN_SQUARE:
3353 ++square_depth;
3354 break;
3355 case CPP_CLOSE_SQUARE:
3356 if (!square_depth--)
3357 return 0;
3358 break;
3360 case CPP_SEMICOLON:
3361 /* This matches the processing in skip_to_end_of_statement. */
3362 if (!brace_depth)
3363 return 0;
3364 break;
3366 case CPP_OPEN_BRACE:
3367 ++brace_depth;
3368 break;
3369 case CPP_CLOSE_BRACE:
3370 if (!brace_depth--)
3371 return 0;
3372 break;
3374 case CPP_OPEN_PAREN:
3375 if (!brace_depth)
3376 ++paren_depth;
3377 break;
3379 case CPP_CLOSE_PAREN:
3380 if (!brace_depth && !paren_depth--)
3382 if (consume_paren)
3383 cp_lexer_consume_token (parser->lexer);
3384 return 1;
3386 break;
3388 default:
3389 break;
3392 /* Consume the token. */
3393 cp_lexer_consume_token (parser->lexer);
3397 /* Consume tokens up to, and including, the next non-nested closing `)'.
3398 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3399 are doing error recovery. Returns -1 if OR_COMMA is true and we
3400 found an unnested token of that type. */
3402 static int
3403 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3404 bool recovering,
3405 bool or_comma,
3406 bool consume_paren)
3408 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3409 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3410 ttype, consume_paren);
3413 /* Consume tokens until we reach the end of the current statement.
3414 Normally, that will be just before consuming a `;'. However, if a
3415 non-nested `}' comes first, then we stop before consuming that. */
3417 static void
3418 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3420 unsigned nesting_depth = 0;
3422 /* Unwind generic function template scope if necessary. */
3423 if (parser->fully_implicit_function_template_p)
3424 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3426 while (true)
3428 cp_token *token = cp_lexer_peek_token (parser->lexer);
3430 switch (token->type)
3432 case CPP_EOF:
3433 case CPP_PRAGMA_EOL:
3434 /* If we've run out of tokens, stop. */
3435 return;
3437 case CPP_SEMICOLON:
3438 /* If the next token is a `;', we have reached the end of the
3439 statement. */
3440 if (!nesting_depth)
3441 return;
3442 break;
3444 case CPP_CLOSE_BRACE:
3445 /* If this is a non-nested '}', stop before consuming it.
3446 That way, when confronted with something like:
3448 { 3 + }
3450 we stop before consuming the closing '}', even though we
3451 have not yet reached a `;'. */
3452 if (nesting_depth == 0)
3453 return;
3455 /* If it is the closing '}' for a block that we have
3456 scanned, stop -- but only after consuming the token.
3457 That way given:
3459 void f g () { ... }
3460 typedef int I;
3462 we will stop after the body of the erroneously declared
3463 function, but before consuming the following `typedef'
3464 declaration. */
3465 if (--nesting_depth == 0)
3467 cp_lexer_consume_token (parser->lexer);
3468 return;
3471 case CPP_OPEN_BRACE:
3472 ++nesting_depth;
3473 break;
3475 default:
3476 break;
3479 /* Consume the token. */
3480 cp_lexer_consume_token (parser->lexer);
3484 /* This function is called at the end of a statement or declaration.
3485 If the next token is a semicolon, it is consumed; otherwise, error
3486 recovery is attempted. */
3488 static void
3489 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3491 /* Look for the trailing `;'. */
3492 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3494 /* If there is additional (erroneous) input, skip to the end of
3495 the statement. */
3496 cp_parser_skip_to_end_of_statement (parser);
3497 /* If the next token is now a `;', consume it. */
3498 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3499 cp_lexer_consume_token (parser->lexer);
3503 /* Skip tokens until we have consumed an entire block, or until we
3504 have consumed a non-nested `;'. */
3506 static void
3507 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3509 int nesting_depth = 0;
3511 /* Unwind generic function template scope if necessary. */
3512 if (parser->fully_implicit_function_template_p)
3513 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3515 while (nesting_depth >= 0)
3517 cp_token *token = cp_lexer_peek_token (parser->lexer);
3519 switch (token->type)
3521 case CPP_EOF:
3522 case CPP_PRAGMA_EOL:
3523 /* If we've run out of tokens, stop. */
3524 return;
3526 case CPP_SEMICOLON:
3527 /* Stop if this is an unnested ';'. */
3528 if (!nesting_depth)
3529 nesting_depth = -1;
3530 break;
3532 case CPP_CLOSE_BRACE:
3533 /* Stop if this is an unnested '}', or closes the outermost
3534 nesting level. */
3535 nesting_depth--;
3536 if (nesting_depth < 0)
3537 return;
3538 if (!nesting_depth)
3539 nesting_depth = -1;
3540 break;
3542 case CPP_OPEN_BRACE:
3543 /* Nest. */
3544 nesting_depth++;
3545 break;
3547 default:
3548 break;
3551 /* Consume the token. */
3552 cp_lexer_consume_token (parser->lexer);
3556 /* Skip tokens until a non-nested closing curly brace is the next
3557 token, or there are no more tokens. Return true in the first case,
3558 false otherwise. */
3560 static bool
3561 cp_parser_skip_to_closing_brace (cp_parser *parser)
3563 unsigned nesting_depth = 0;
3565 while (true)
3567 cp_token *token = cp_lexer_peek_token (parser->lexer);
3569 switch (token->type)
3571 case CPP_EOF:
3572 case CPP_PRAGMA_EOL:
3573 /* If we've run out of tokens, stop. */
3574 return false;
3576 case CPP_CLOSE_BRACE:
3577 /* If the next token is a non-nested `}', then we have reached
3578 the end of the current block. */
3579 if (nesting_depth-- == 0)
3580 return true;
3581 break;
3583 case CPP_OPEN_BRACE:
3584 /* If it the next token is a `{', then we are entering a new
3585 block. Consume the entire block. */
3586 ++nesting_depth;
3587 break;
3589 default:
3590 break;
3593 /* Consume the token. */
3594 cp_lexer_consume_token (parser->lexer);
3598 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3599 parameter is the PRAGMA token, allowing us to purge the entire pragma
3600 sequence. */
3602 static void
3603 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3605 cp_token *token;
3607 parser->lexer->in_pragma = false;
3610 token = cp_lexer_consume_token (parser->lexer);
3611 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3613 /* Ensure that the pragma is not parsed again. */
3614 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3617 /* Require pragma end of line, resyncing with it as necessary. The
3618 arguments are as for cp_parser_skip_to_pragma_eol. */
3620 static void
3621 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3623 parser->lexer->in_pragma = false;
3624 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3625 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3628 /* This is a simple wrapper around make_typename_type. When the id is
3629 an unresolved identifier node, we can provide a superior diagnostic
3630 using cp_parser_diagnose_invalid_type_name. */
3632 static tree
3633 cp_parser_make_typename_type (cp_parser *parser, tree id,
3634 location_t id_location)
3636 tree result;
3637 if (identifier_p (id))
3639 result = make_typename_type (parser->scope, id, typename_type,
3640 /*complain=*/tf_none);
3641 if (result == error_mark_node)
3642 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3643 return result;
3645 return make_typename_type (parser->scope, id, typename_type, tf_error);
3648 /* This is a wrapper around the
3649 make_{pointer,ptrmem,reference}_declarator functions that decides
3650 which one to call based on the CODE and CLASS_TYPE arguments. The
3651 CODE argument should be one of the values returned by
3652 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3653 appertain to the pointer or reference. */
3655 static cp_declarator *
3656 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3657 cp_cv_quals cv_qualifiers,
3658 cp_declarator *target,
3659 tree attributes)
3661 if (code == ERROR_MARK)
3662 return cp_error_declarator;
3664 if (code == INDIRECT_REF)
3665 if (class_type == NULL_TREE)
3666 return make_pointer_declarator (cv_qualifiers, target, attributes);
3667 else
3668 return make_ptrmem_declarator (cv_qualifiers, class_type,
3669 target, attributes);
3670 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3671 return make_reference_declarator (cv_qualifiers, target,
3672 false, attributes);
3673 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3674 return make_reference_declarator (cv_qualifiers, target,
3675 true, attributes);
3676 gcc_unreachable ();
3679 /* Create a new C++ parser. */
3681 static cp_parser *
3682 cp_parser_new (void)
3684 cp_parser *parser;
3685 cp_lexer *lexer;
3686 unsigned i;
3688 /* cp_lexer_new_main is called before doing GC allocation because
3689 cp_lexer_new_main might load a PCH file. */
3690 lexer = cp_lexer_new_main ();
3692 /* Initialize the binops_by_token so that we can get the tree
3693 directly from the token. */
3694 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3695 binops_by_token[binops[i].token_type] = binops[i];
3697 parser = ggc_cleared_alloc<cp_parser> ();
3698 parser->lexer = lexer;
3699 parser->context = cp_parser_context_new (NULL);
3701 /* For now, we always accept GNU extensions. */
3702 parser->allow_gnu_extensions_p = 1;
3704 /* The `>' token is a greater-than operator, not the end of a
3705 template-id. */
3706 parser->greater_than_is_operator_p = true;
3708 parser->default_arg_ok_p = true;
3710 /* We are not parsing a constant-expression. */
3711 parser->integral_constant_expression_p = false;
3712 parser->allow_non_integral_constant_expression_p = false;
3713 parser->non_integral_constant_expression_p = false;
3715 /* Local variable names are not forbidden. */
3716 parser->local_variables_forbidden_p = false;
3718 /* We are not processing an `extern "C"' declaration. */
3719 parser->in_unbraced_linkage_specification_p = false;
3721 /* We are not processing a declarator. */
3722 parser->in_declarator_p = false;
3724 /* We are not processing a template-argument-list. */
3725 parser->in_template_argument_list_p = false;
3727 /* We are not in an iteration statement. */
3728 parser->in_statement = 0;
3730 /* We are not in a switch statement. */
3731 parser->in_switch_statement_p = false;
3733 /* We are not parsing a type-id inside an expression. */
3734 parser->in_type_id_in_expr_p = false;
3736 /* Declarations aren't implicitly extern "C". */
3737 parser->implicit_extern_c = false;
3739 /* String literals should be translated to the execution character set. */
3740 parser->translate_strings_p = true;
3742 /* We are not parsing a function body. */
3743 parser->in_function_body = false;
3745 /* We can correct until told otherwise. */
3746 parser->colon_corrects_to_scope_p = true;
3748 /* The unparsed function queue is empty. */
3749 push_unparsed_function_queues (parser);
3751 /* There are no classes being defined. */
3752 parser->num_classes_being_defined = 0;
3754 /* No template parameters apply. */
3755 parser->num_template_parameter_lists = 0;
3757 /* Not declaring an implicit function template. */
3758 parser->auto_is_implicit_function_template_parm_p = false;
3759 parser->fully_implicit_function_template_p = false;
3760 parser->implicit_template_parms = 0;
3761 parser->implicit_template_scope = 0;
3763 /* Active OpenACC routine clauses. */
3764 parser->oacc_routine = NULL;
3766 /* Allow constrained-type-specifiers. */
3767 parser->prevent_constrained_type_specifiers = 0;
3769 return parser;
3772 /* Create a cp_lexer structure which will emit the tokens in CACHE
3773 and push it onto the parser's lexer stack. This is used for delayed
3774 parsing of in-class method bodies and default arguments, and should
3775 not be confused with tentative parsing. */
3776 static void
3777 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3779 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3780 lexer->next = parser->lexer;
3781 parser->lexer = lexer;
3783 /* Move the current source position to that of the first token in the
3784 new lexer. */
3785 cp_lexer_set_source_position_from_token (lexer->next_token);
3788 /* Pop the top lexer off the parser stack. This is never used for the
3789 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3790 static void
3791 cp_parser_pop_lexer (cp_parser *parser)
3793 cp_lexer *lexer = parser->lexer;
3794 parser->lexer = lexer->next;
3795 cp_lexer_destroy (lexer);
3797 /* Put the current source position back where it was before this
3798 lexer was pushed. */
3799 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3802 /* Lexical conventions [gram.lex] */
3804 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3805 identifier. */
3807 static cp_expr
3808 cp_parser_identifier (cp_parser* parser)
3810 cp_token *token;
3812 /* Look for the identifier. */
3813 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3814 /* Return the value. */
3815 if (token)
3816 return cp_expr (token->u.value, token->location);
3817 else
3818 return error_mark_node;
3821 /* Parse a sequence of adjacent string constants. Returns a
3822 TREE_STRING representing the combined, nul-terminated string
3823 constant. If TRANSLATE is true, translate the string to the
3824 execution character set. If WIDE_OK is true, a wide string is
3825 invalid here.
3827 C++98 [lex.string] says that if a narrow string literal token is
3828 adjacent to a wide string literal token, the behavior is undefined.
3829 However, C99 6.4.5p4 says that this results in a wide string literal.
3830 We follow C99 here, for consistency with the C front end.
3832 This code is largely lifted from lex_string() in c-lex.c.
3834 FUTURE: ObjC++ will need to handle @-strings here. */
3835 static cp_expr
3836 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3837 bool lookup_udlit = true)
3839 tree value;
3840 size_t count;
3841 struct obstack str_ob;
3842 cpp_string str, istr, *strs;
3843 cp_token *tok;
3844 enum cpp_ttype type, curr_type;
3845 int have_suffix_p = 0;
3846 tree string_tree;
3847 tree suffix_id = NULL_TREE;
3848 bool curr_tok_is_userdef_p = false;
3850 tok = cp_lexer_peek_token (parser->lexer);
3851 if (!cp_parser_is_string_literal (tok))
3853 cp_parser_error (parser, "expected string-literal");
3854 return error_mark_node;
3857 location_t loc = tok->location;
3859 if (cpp_userdef_string_p (tok->type))
3861 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3862 curr_type = cpp_userdef_string_remove_type (tok->type);
3863 curr_tok_is_userdef_p = true;
3865 else
3867 string_tree = tok->u.value;
3868 curr_type = tok->type;
3870 type = curr_type;
3872 /* Try to avoid the overhead of creating and destroying an obstack
3873 for the common case of just one string. */
3874 if (!cp_parser_is_string_literal
3875 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3877 cp_lexer_consume_token (parser->lexer);
3879 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3880 str.len = TREE_STRING_LENGTH (string_tree);
3881 count = 1;
3883 if (curr_tok_is_userdef_p)
3885 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3886 have_suffix_p = 1;
3887 curr_type = cpp_userdef_string_remove_type (tok->type);
3889 else
3890 curr_type = tok->type;
3892 strs = &str;
3894 else
3896 location_t last_tok_loc = tok->location;
3897 gcc_obstack_init (&str_ob);
3898 count = 0;
3902 cp_lexer_consume_token (parser->lexer);
3903 count++;
3904 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3905 str.len = TREE_STRING_LENGTH (string_tree);
3907 if (curr_tok_is_userdef_p)
3909 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3910 if (have_suffix_p == 0)
3912 suffix_id = curr_suffix_id;
3913 have_suffix_p = 1;
3915 else if (have_suffix_p == 1
3916 && curr_suffix_id != suffix_id)
3918 error ("inconsistent user-defined literal suffixes"
3919 " %qD and %qD in string literal",
3920 suffix_id, curr_suffix_id);
3921 have_suffix_p = -1;
3923 curr_type = cpp_userdef_string_remove_type (tok->type);
3925 else
3926 curr_type = tok->type;
3928 if (type != curr_type)
3930 if (type == CPP_STRING)
3931 type = curr_type;
3932 else if (curr_type != CPP_STRING)
3934 rich_location rich_loc (line_table, tok->location);
3935 rich_loc.add_range (last_tok_loc, false);
3936 error_at_rich_loc (&rich_loc,
3937 "unsupported non-standard concatenation "
3938 "of string literals");
3942 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3944 last_tok_loc = tok->location;
3946 tok = cp_lexer_peek_token (parser->lexer);
3947 if (cpp_userdef_string_p (tok->type))
3949 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3950 curr_type = cpp_userdef_string_remove_type (tok->type);
3951 curr_tok_is_userdef_p = true;
3953 else
3955 string_tree = tok->u.value;
3956 curr_type = tok->type;
3957 curr_tok_is_userdef_p = false;
3960 while (cp_parser_is_string_literal (tok));
3962 /* A string literal built by concatenation has its caret=start at
3963 the start of the initial string, and its finish at the finish of
3964 the final string literal. */
3965 loc = make_location (loc, loc, get_finish (last_tok_loc));
3967 strs = (cpp_string *) obstack_finish (&str_ob);
3970 if (type != CPP_STRING && !wide_ok)
3972 cp_parser_error (parser, "a wide string is invalid in this context");
3973 type = CPP_STRING;
3976 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3977 (parse_in, strs, count, &istr, type))
3979 value = build_string (istr.len, (const char *)istr.text);
3980 free (CONST_CAST (unsigned char *, istr.text));
3982 switch (type)
3984 default:
3985 case CPP_STRING:
3986 case CPP_UTF8STRING:
3987 TREE_TYPE (value) = char_array_type_node;
3988 break;
3989 case CPP_STRING16:
3990 TREE_TYPE (value) = char16_array_type_node;
3991 break;
3992 case CPP_STRING32:
3993 TREE_TYPE (value) = char32_array_type_node;
3994 break;
3995 case CPP_WSTRING:
3996 TREE_TYPE (value) = wchar_array_type_node;
3997 break;
4000 value = fix_string_type (value);
4002 if (have_suffix_p)
4004 tree literal = build_userdef_literal (suffix_id, value,
4005 OT_NONE, NULL_TREE);
4006 if (lookup_udlit)
4007 value = cp_parser_userdef_string_literal (literal);
4008 else
4009 value = literal;
4012 else
4013 /* cpp_interpret_string has issued an error. */
4014 value = error_mark_node;
4016 if (count > 1)
4017 obstack_free (&str_ob, 0);
4019 return cp_expr (value, loc);
4022 /* Look up a literal operator with the name and the exact arguments. */
4024 static tree
4025 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4027 tree decl, fns;
4028 decl = lookup_name (name);
4029 if (!decl || !is_overloaded_fn (decl))
4030 return error_mark_node;
4032 for (fns = decl; fns; fns = OVL_NEXT (fns))
4034 unsigned int ix;
4035 bool found = true;
4036 tree fn = OVL_CURRENT (fns);
4037 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4038 if (parmtypes != NULL_TREE)
4040 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4041 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4043 tree tparm = TREE_VALUE (parmtypes);
4044 tree targ = TREE_TYPE ((*args)[ix]);
4045 bool ptr = TYPE_PTR_P (tparm);
4046 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4047 if ((ptr || arr || !same_type_p (tparm, targ))
4048 && (!ptr || !arr
4049 || !same_type_p (TREE_TYPE (tparm),
4050 TREE_TYPE (targ))))
4051 found = false;
4053 if (found
4054 && ix == vec_safe_length (args)
4055 /* May be this should be sufficient_parms_p instead,
4056 depending on how exactly should user-defined literals
4057 work in presence of default arguments on the literal
4058 operator parameters. */
4059 && parmtypes == void_list_node)
4060 return decl;
4064 return error_mark_node;
4067 /* Parse a user-defined char constant. Returns a call to a user-defined
4068 literal operator taking the character as an argument. */
4070 static cp_expr
4071 cp_parser_userdef_char_literal (cp_parser *parser)
4073 cp_token *token = cp_lexer_consume_token (parser->lexer);
4074 tree literal = token->u.value;
4075 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4076 tree value = USERDEF_LITERAL_VALUE (literal);
4077 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4078 tree decl, result;
4080 /* Build up a call to the user-defined operator */
4081 /* Lookup the name we got back from the id-expression. */
4082 vec<tree, va_gc> *args = make_tree_vector ();
4083 vec_safe_push (args, value);
4084 decl = lookup_literal_operator (name, args);
4085 if (!decl || decl == error_mark_node)
4087 error ("unable to find character literal operator %qD with %qT argument",
4088 name, TREE_TYPE (value));
4089 release_tree_vector (args);
4090 return error_mark_node;
4092 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4093 release_tree_vector (args);
4094 return result;
4097 /* A subroutine of cp_parser_userdef_numeric_literal to
4098 create a char... template parameter pack from a string node. */
4100 static tree
4101 make_char_string_pack (tree value)
4103 tree charvec;
4104 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4105 const char *str = TREE_STRING_POINTER (value);
4106 int i, len = TREE_STRING_LENGTH (value) - 1;
4107 tree argvec = make_tree_vec (1);
4109 /* Fill in CHARVEC with all of the parameters. */
4110 charvec = make_tree_vec (len);
4111 for (i = 0; i < len; ++i)
4112 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4114 /* Build the argument packs. */
4115 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4116 TREE_TYPE (argpack) = char_type_node;
4118 TREE_VEC_ELT (argvec, 0) = argpack;
4120 return argvec;
4123 /* A subroutine of cp_parser_userdef_numeric_literal to
4124 create a char... template parameter pack from a string node. */
4126 static tree
4127 make_string_pack (tree value)
4129 tree charvec;
4130 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4131 const unsigned char *str
4132 = (const unsigned char *) TREE_STRING_POINTER (value);
4133 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4134 int len = TREE_STRING_LENGTH (value) / sz - 1;
4135 tree argvec = make_tree_vec (2);
4137 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4138 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4140 /* First template parm is character type. */
4141 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4143 /* Fill in CHARVEC with all of the parameters. */
4144 charvec = make_tree_vec (len);
4145 for (int i = 0; i < len; ++i)
4146 TREE_VEC_ELT (charvec, i)
4147 = double_int_to_tree (str_char_type_node,
4148 double_int::from_buffer (str + i * sz, sz));
4150 /* Build the argument packs. */
4151 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4152 TREE_TYPE (argpack) = str_char_type_node;
4154 TREE_VEC_ELT (argvec, 1) = argpack;
4156 return argvec;
4159 /* Parse a user-defined numeric constant. returns a call to a user-defined
4160 literal operator. */
4162 static cp_expr
4163 cp_parser_userdef_numeric_literal (cp_parser *parser)
4165 cp_token *token = cp_lexer_consume_token (parser->lexer);
4166 tree literal = token->u.value;
4167 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4168 tree value = USERDEF_LITERAL_VALUE (literal);
4169 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4170 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4171 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4172 tree decl, result;
4173 vec<tree, va_gc> *args;
4175 /* Look for a literal operator taking the exact type of numeric argument
4176 as the literal value. */
4177 args = make_tree_vector ();
4178 vec_safe_push (args, value);
4179 decl = lookup_literal_operator (name, args);
4180 if (decl && decl != error_mark_node)
4182 result = finish_call_expr (decl, &args, false, true,
4183 tf_warning_or_error);
4185 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4187 warning_at (token->location, OPT_Woverflow,
4188 "integer literal exceeds range of %qT type",
4189 long_long_unsigned_type_node);
4191 else
4193 if (overflow > 0)
4194 warning_at (token->location, OPT_Woverflow,
4195 "floating literal exceeds range of %qT type",
4196 long_double_type_node);
4197 else if (overflow < 0)
4198 warning_at (token->location, OPT_Woverflow,
4199 "floating literal truncated to zero");
4202 release_tree_vector (args);
4203 return result;
4205 release_tree_vector (args);
4207 /* If the numeric argument didn't work, look for a raw literal
4208 operator taking a const char* argument consisting of the number
4209 in string format. */
4210 args = make_tree_vector ();
4211 vec_safe_push (args, num_string);
4212 decl = lookup_literal_operator (name, args);
4213 if (decl && decl != error_mark_node)
4215 result = finish_call_expr (decl, &args, false, true,
4216 tf_warning_or_error);
4217 release_tree_vector (args);
4218 return result;
4220 release_tree_vector (args);
4222 /* If the raw literal didn't work, look for a non-type template
4223 function with parameter pack char.... Call the function with
4224 template parameter characters representing the number. */
4225 args = make_tree_vector ();
4226 decl = lookup_literal_operator (name, args);
4227 if (decl && decl != error_mark_node)
4229 tree tmpl_args = make_char_string_pack (num_string);
4230 decl = lookup_template_function (decl, tmpl_args);
4231 result = finish_call_expr (decl, &args, false, true,
4232 tf_warning_or_error);
4233 release_tree_vector (args);
4234 return result;
4237 release_tree_vector (args);
4239 error ("unable to find numeric literal operator %qD", name);
4240 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4241 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4242 "to enable more built-in suffixes");
4243 return error_mark_node;
4246 /* Parse a user-defined string constant. Returns a call to a user-defined
4247 literal operator taking a character pointer and the length of the string
4248 as arguments. */
4250 static tree
4251 cp_parser_userdef_string_literal (tree literal)
4253 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4254 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4255 tree value = USERDEF_LITERAL_VALUE (literal);
4256 int len = TREE_STRING_LENGTH (value)
4257 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4258 tree decl, result;
4259 vec<tree, va_gc> *args;
4261 /* Build up a call to the user-defined operator. */
4262 /* Lookup the name we got back from the id-expression. */
4263 args = make_tree_vector ();
4264 vec_safe_push (args, value);
4265 vec_safe_push (args, build_int_cst (size_type_node, len));
4266 decl = lookup_literal_operator (name, args);
4268 if (decl && decl != error_mark_node)
4270 result = finish_call_expr (decl, &args, false, true,
4271 tf_warning_or_error);
4272 release_tree_vector (args);
4273 return result;
4275 release_tree_vector (args);
4277 /* Look for a template function with typename parameter CharT
4278 and parameter pack CharT... Call the function with
4279 template parameter characters representing the string. */
4280 args = make_tree_vector ();
4281 decl = lookup_literal_operator (name, args);
4282 if (decl && decl != error_mark_node)
4284 tree tmpl_args = make_string_pack (value);
4285 decl = lookup_template_function (decl, tmpl_args);
4286 result = finish_call_expr (decl, &args, false, true,
4287 tf_warning_or_error);
4288 release_tree_vector (args);
4289 return result;
4291 release_tree_vector (args);
4293 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4294 name, TREE_TYPE (value), size_type_node);
4295 return error_mark_node;
4299 /* Basic concepts [gram.basic] */
4301 /* Parse a translation-unit.
4303 translation-unit:
4304 declaration-seq [opt]
4306 Returns TRUE if all went well. */
4308 static bool
4309 cp_parser_translation_unit (cp_parser* parser)
4311 /* The address of the first non-permanent object on the declarator
4312 obstack. */
4313 static void *declarator_obstack_base;
4315 bool success;
4317 /* Create the declarator obstack, if necessary. */
4318 if (!cp_error_declarator)
4320 gcc_obstack_init (&declarator_obstack);
4321 /* Create the error declarator. */
4322 cp_error_declarator = make_declarator (cdk_error);
4323 /* Create the empty parameter list. */
4324 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4325 /* Remember where the base of the declarator obstack lies. */
4326 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4329 cp_parser_declaration_seq_opt (parser);
4331 /* If there are no tokens left then all went well. */
4332 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4334 /* Get rid of the token array; we don't need it any more. */
4335 cp_lexer_destroy (parser->lexer);
4336 parser->lexer = NULL;
4338 /* This file might have been a context that's implicitly extern
4339 "C". If so, pop the lang context. (Only relevant for PCH.) */
4340 if (parser->implicit_extern_c)
4342 pop_lang_context ();
4343 parser->implicit_extern_c = false;
4346 /* Finish up. */
4347 finish_translation_unit ();
4349 success = true;
4351 else
4353 cp_parser_error (parser, "expected declaration");
4354 success = false;
4357 /* Make sure the declarator obstack was fully cleaned up. */
4358 gcc_assert (obstack_next_free (&declarator_obstack)
4359 == declarator_obstack_base);
4361 /* All went well. */
4362 return success;
4365 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4366 decltype context. */
4368 static inline tsubst_flags_t
4369 complain_flags (bool decltype_p)
4371 tsubst_flags_t complain = tf_warning_or_error;
4372 if (decltype_p)
4373 complain |= tf_decltype;
4374 return complain;
4377 /* We're about to parse a collection of statements. If we're currently
4378 parsing tentatively, set up a firewall so that any nested
4379 cp_parser_commit_to_tentative_parse won't affect the current context. */
4381 static cp_token_position
4382 cp_parser_start_tentative_firewall (cp_parser *parser)
4384 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4385 return 0;
4387 cp_parser_parse_tentatively (parser);
4388 cp_parser_commit_to_topmost_tentative_parse (parser);
4389 return cp_lexer_token_position (parser->lexer, false);
4392 /* We've finished parsing the collection of statements. Wrap up the
4393 firewall and replace the relevant tokens with the parsed form. */
4395 static void
4396 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4397 tree expr)
4399 if (!start)
4400 return;
4402 /* Finish the firewall level. */
4403 cp_parser_parse_definitely (parser);
4404 /* And remember the result of the parse for when we try again. */
4405 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4406 token->type = CPP_PREPARSED_EXPR;
4407 token->u.value = expr;
4408 token->keyword = RID_MAX;
4409 cp_lexer_purge_tokens_after (parser->lexer, start);
4412 /* Like the above functions, but let the user modify the tokens. Used by
4413 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4414 later parses, so it makes sense to localize the effects of
4415 cp_parser_commit_to_tentative_parse. */
4417 struct tentative_firewall
4419 cp_parser *parser;
4420 bool set;
4422 tentative_firewall (cp_parser *p): parser(p)
4424 /* If we're currently parsing tentatively, start a committed level as a
4425 firewall and then an inner tentative parse. */
4426 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4428 cp_parser_parse_tentatively (parser);
4429 cp_parser_commit_to_topmost_tentative_parse (parser);
4430 cp_parser_parse_tentatively (parser);
4434 ~tentative_firewall()
4436 if (set)
4438 /* Finish the inner tentative parse and the firewall, propagating any
4439 uncommitted error state to the outer tentative parse. */
4440 bool err = cp_parser_error_occurred (parser);
4441 cp_parser_parse_definitely (parser);
4442 cp_parser_parse_definitely (parser);
4443 if (err)
4444 cp_parser_simulate_error (parser);
4449 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4450 enclosing parentheses. */
4452 static cp_expr
4453 cp_parser_statement_expr (cp_parser *parser)
4455 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4457 /* Consume the '('. */
4458 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4459 cp_lexer_consume_token (parser->lexer);
4460 /* Start the statement-expression. */
4461 tree expr = begin_stmt_expr ();
4462 /* Parse the compound-statement. */
4463 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4464 /* Finish up. */
4465 expr = finish_stmt_expr (expr, false);
4466 /* Consume the ')'. */
4467 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4468 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4469 cp_parser_skip_to_end_of_statement (parser);
4471 cp_parser_end_tentative_firewall (parser, start, expr);
4472 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4473 return cp_expr (expr, combined_loc);
4476 /* Expressions [gram.expr] */
4478 /* Parse a fold-operator.
4480 fold-operator:
4481 - * / % ^ & | = < > << >>
4482 = -= *= /= %= ^= &= |= <<= >>=
4483 == != <= >= && || , .* ->*
4485 This returns the tree code corresponding to the matched operator
4486 as an int. When the current token matches a compound assignment
4487 opertor, the resulting tree code is the negative value of the
4488 non-assignment operator. */
4490 static int
4491 cp_parser_fold_operator (cp_token *token)
4493 switch (token->type)
4495 case CPP_PLUS: return PLUS_EXPR;
4496 case CPP_MINUS: return MINUS_EXPR;
4497 case CPP_MULT: return MULT_EXPR;
4498 case CPP_DIV: return TRUNC_DIV_EXPR;
4499 case CPP_MOD: return TRUNC_MOD_EXPR;
4500 case CPP_XOR: return BIT_XOR_EXPR;
4501 case CPP_AND: return BIT_AND_EXPR;
4502 case CPP_OR: return BIT_IOR_EXPR;
4503 case CPP_LSHIFT: return LSHIFT_EXPR;
4504 case CPP_RSHIFT: return RSHIFT_EXPR;
4506 case CPP_EQ: return -NOP_EXPR;
4507 case CPP_PLUS_EQ: return -PLUS_EXPR;
4508 case CPP_MINUS_EQ: return -MINUS_EXPR;
4509 case CPP_MULT_EQ: return -MULT_EXPR;
4510 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4511 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4512 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4513 case CPP_AND_EQ: return -BIT_AND_EXPR;
4514 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4515 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4516 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4518 case CPP_EQ_EQ: return EQ_EXPR;
4519 case CPP_NOT_EQ: return NE_EXPR;
4520 case CPP_LESS: return LT_EXPR;
4521 case CPP_GREATER: return GT_EXPR;
4522 case CPP_LESS_EQ: return LE_EXPR;
4523 case CPP_GREATER_EQ: return GE_EXPR;
4525 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4526 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4528 case CPP_COMMA: return COMPOUND_EXPR;
4530 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4531 case CPP_DEREF_STAR: return MEMBER_REF;
4533 default: return ERROR_MARK;
4537 /* Returns true if CODE indicates a binary expression, which is not allowed in
4538 the LHS of a fold-expression. More codes will need to be added to use this
4539 function in other contexts. */
4541 static bool
4542 is_binary_op (tree_code code)
4544 switch (code)
4546 case PLUS_EXPR:
4547 case POINTER_PLUS_EXPR:
4548 case MINUS_EXPR:
4549 case MULT_EXPR:
4550 case TRUNC_DIV_EXPR:
4551 case TRUNC_MOD_EXPR:
4552 case BIT_XOR_EXPR:
4553 case BIT_AND_EXPR:
4554 case BIT_IOR_EXPR:
4555 case LSHIFT_EXPR:
4556 case RSHIFT_EXPR:
4558 case MODOP_EXPR:
4560 case EQ_EXPR:
4561 case NE_EXPR:
4562 case LE_EXPR:
4563 case GE_EXPR:
4564 case LT_EXPR:
4565 case GT_EXPR:
4567 case TRUTH_ANDIF_EXPR:
4568 case TRUTH_ORIF_EXPR:
4570 case COMPOUND_EXPR:
4572 case DOTSTAR_EXPR:
4573 case MEMBER_REF:
4574 return true;
4576 default:
4577 return false;
4581 /* If the next token is a suitable fold operator, consume it and return as
4582 the function above. */
4584 static int
4585 cp_parser_fold_operator (cp_parser *parser)
4587 cp_token* token = cp_lexer_peek_token (parser->lexer);
4588 int code = cp_parser_fold_operator (token);
4589 if (code != ERROR_MARK)
4590 cp_lexer_consume_token (parser->lexer);
4591 return code;
4594 /* Parse a fold-expression.
4596 fold-expression:
4597 ( ... folding-operator cast-expression)
4598 ( cast-expression folding-operator ... )
4599 ( cast-expression folding operator ... folding-operator cast-expression)
4601 Note that the '(' and ')' are matched in primary expression. */
4603 static cp_expr
4604 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4606 cp_id_kind pidk;
4608 // Left fold.
4609 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4611 cp_lexer_consume_token (parser->lexer);
4612 int op = cp_parser_fold_operator (parser);
4613 if (op == ERROR_MARK)
4615 cp_parser_error (parser, "expected binary operator");
4616 return error_mark_node;
4619 tree expr = cp_parser_cast_expression (parser, false, false,
4620 false, &pidk);
4621 if (expr == error_mark_node)
4622 return error_mark_node;
4623 return finish_left_unary_fold_expr (expr, op);
4626 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4627 int op = cp_parser_fold_operator (parser);
4628 if (op == ERROR_MARK)
4630 cp_parser_error (parser, "expected binary operator");
4631 return error_mark_node;
4634 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4636 cp_parser_error (parser, "expected ...");
4637 return error_mark_node;
4639 cp_lexer_consume_token (parser->lexer);
4641 /* The operands of a fold-expression are cast-expressions, so binary or
4642 conditional expressions are not allowed. We check this here to avoid
4643 tentative parsing. */
4644 if (is_binary_op (TREE_CODE (expr1)))
4645 error_at (location_of (expr1),
4646 "binary expression in operand of fold-expression");
4647 else if (TREE_CODE (expr1) == COND_EXPR)
4648 error_at (location_of (expr1),
4649 "conditional expression in operand of fold-expression");
4651 // Right fold.
4652 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4653 return finish_right_unary_fold_expr (expr1, op);
4655 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4657 cp_parser_error (parser, "mismatched operator in fold-expression");
4658 return error_mark_node;
4660 cp_lexer_consume_token (parser->lexer);
4662 // Binary left or right fold.
4663 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4664 if (expr2 == error_mark_node)
4665 return error_mark_node;
4666 return finish_binary_fold_expr (expr1, expr2, op);
4669 /* Parse a primary-expression.
4671 primary-expression:
4672 literal
4673 this
4674 ( expression )
4675 id-expression
4676 lambda-expression (C++11)
4678 GNU Extensions:
4680 primary-expression:
4681 ( compound-statement )
4682 __builtin_va_arg ( assignment-expression , type-id )
4683 __builtin_offsetof ( type-id , offsetof-expression )
4685 C++ Extensions:
4686 __has_nothrow_assign ( type-id )
4687 __has_nothrow_constructor ( type-id )
4688 __has_nothrow_copy ( type-id )
4689 __has_trivial_assign ( type-id )
4690 __has_trivial_constructor ( type-id )
4691 __has_trivial_copy ( type-id )
4692 __has_trivial_destructor ( type-id )
4693 __has_virtual_destructor ( type-id )
4694 __is_abstract ( type-id )
4695 __is_base_of ( type-id , type-id )
4696 __is_class ( type-id )
4697 __is_empty ( type-id )
4698 __is_enum ( type-id )
4699 __is_final ( type-id )
4700 __is_literal_type ( type-id )
4701 __is_pod ( type-id )
4702 __is_polymorphic ( type-id )
4703 __is_std_layout ( type-id )
4704 __is_trivial ( type-id )
4705 __is_union ( type-id )
4707 Objective-C++ Extension:
4709 primary-expression:
4710 objc-expression
4712 literal:
4713 __null
4715 ADDRESS_P is true iff this expression was immediately preceded by
4716 "&" and therefore might denote a pointer-to-member. CAST_P is true
4717 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4718 true iff this expression is a template argument.
4720 Returns a representation of the expression. Upon return, *IDK
4721 indicates what kind of id-expression (if any) was present. */
4723 static cp_expr
4724 cp_parser_primary_expression (cp_parser *parser,
4725 bool address_p,
4726 bool cast_p,
4727 bool template_arg_p,
4728 bool decltype_p,
4729 cp_id_kind *idk)
4731 cp_token *token = NULL;
4733 /* Assume the primary expression is not an id-expression. */
4734 *idk = CP_ID_KIND_NONE;
4736 /* Peek at the next token. */
4737 token = cp_lexer_peek_token (parser->lexer);
4738 switch ((int) token->type)
4740 /* literal:
4741 integer-literal
4742 character-literal
4743 floating-literal
4744 string-literal
4745 boolean-literal
4746 pointer-literal
4747 user-defined-literal */
4748 case CPP_CHAR:
4749 case CPP_CHAR16:
4750 case CPP_CHAR32:
4751 case CPP_WCHAR:
4752 case CPP_UTF8CHAR:
4753 case CPP_NUMBER:
4754 case CPP_PREPARSED_EXPR:
4755 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4756 return cp_parser_userdef_numeric_literal (parser);
4757 token = cp_lexer_consume_token (parser->lexer);
4758 if (TREE_CODE (token->u.value) == FIXED_CST)
4760 error_at (token->location,
4761 "fixed-point types not supported in C++");
4762 return error_mark_node;
4764 /* Floating-point literals are only allowed in an integral
4765 constant expression if they are cast to an integral or
4766 enumeration type. */
4767 if (TREE_CODE (token->u.value) == REAL_CST
4768 && parser->integral_constant_expression_p
4769 && pedantic)
4771 /* CAST_P will be set even in invalid code like "int(2.7 +
4772 ...)". Therefore, we have to check that the next token
4773 is sure to end the cast. */
4774 if (cast_p)
4776 cp_token *next_token;
4778 next_token = cp_lexer_peek_token (parser->lexer);
4779 if (/* The comma at the end of an
4780 enumerator-definition. */
4781 next_token->type != CPP_COMMA
4782 /* The curly brace at the end of an enum-specifier. */
4783 && next_token->type != CPP_CLOSE_BRACE
4784 /* The end of a statement. */
4785 && next_token->type != CPP_SEMICOLON
4786 /* The end of the cast-expression. */
4787 && next_token->type != CPP_CLOSE_PAREN
4788 /* The end of an array bound. */
4789 && next_token->type != CPP_CLOSE_SQUARE
4790 /* The closing ">" in a template-argument-list. */
4791 && (next_token->type != CPP_GREATER
4792 || parser->greater_than_is_operator_p)
4793 /* C++0x only: A ">>" treated like two ">" tokens,
4794 in a template-argument-list. */
4795 && (next_token->type != CPP_RSHIFT
4796 || (cxx_dialect == cxx98)
4797 || parser->greater_than_is_operator_p))
4798 cast_p = false;
4801 /* If we are within a cast, then the constraint that the
4802 cast is to an integral or enumeration type will be
4803 checked at that point. If we are not within a cast, then
4804 this code is invalid. */
4805 if (!cast_p)
4806 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4808 return cp_expr (token->u.value, token->location);
4810 case CPP_CHAR_USERDEF:
4811 case CPP_CHAR16_USERDEF:
4812 case CPP_CHAR32_USERDEF:
4813 case CPP_WCHAR_USERDEF:
4814 case CPP_UTF8CHAR_USERDEF:
4815 return cp_parser_userdef_char_literal (parser);
4817 case CPP_STRING:
4818 case CPP_STRING16:
4819 case CPP_STRING32:
4820 case CPP_WSTRING:
4821 case CPP_UTF8STRING:
4822 case CPP_STRING_USERDEF:
4823 case CPP_STRING16_USERDEF:
4824 case CPP_STRING32_USERDEF:
4825 case CPP_WSTRING_USERDEF:
4826 case CPP_UTF8STRING_USERDEF:
4827 /* ??? Should wide strings be allowed when parser->translate_strings_p
4828 is false (i.e. in attributes)? If not, we can kill the third
4829 argument to cp_parser_string_literal. */
4830 return cp_parser_string_literal (parser,
4831 parser->translate_strings_p,
4832 true);
4834 case CPP_OPEN_PAREN:
4835 /* If we see `( { ' then we are looking at the beginning of
4836 a GNU statement-expression. */
4837 if (cp_parser_allow_gnu_extensions_p (parser)
4838 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4840 /* Statement-expressions are not allowed by the standard. */
4841 pedwarn (token->location, OPT_Wpedantic,
4842 "ISO C++ forbids braced-groups within expressions");
4844 /* And they're not allowed outside of a function-body; you
4845 cannot, for example, write:
4847 int i = ({ int j = 3; j + 1; });
4849 at class or namespace scope. */
4850 if (!parser->in_function_body
4851 || parser->in_template_argument_list_p)
4853 error_at (token->location,
4854 "statement-expressions are not allowed outside "
4855 "functions nor in template-argument lists");
4856 cp_parser_skip_to_end_of_block_or_statement (parser);
4857 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4858 cp_lexer_consume_token (parser->lexer);
4859 return error_mark_node;
4861 else
4862 return cp_parser_statement_expr (parser);
4864 /* Otherwise it's a normal parenthesized expression. */
4866 cp_expr expr;
4867 bool saved_greater_than_is_operator_p;
4869 location_t open_paren_loc = token->location;
4871 /* Consume the `('. */
4872 cp_lexer_consume_token (parser->lexer);
4873 /* Within a parenthesized expression, a `>' token is always
4874 the greater-than operator. */
4875 saved_greater_than_is_operator_p
4876 = parser->greater_than_is_operator_p;
4877 parser->greater_than_is_operator_p = true;
4879 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4880 /* Left fold expression. */
4881 expr = NULL_TREE;
4882 else
4883 /* Parse the parenthesized expression. */
4884 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4886 token = cp_lexer_peek_token (parser->lexer);
4887 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4889 expr = cp_parser_fold_expression (parser, expr);
4890 if (expr != error_mark_node
4891 && cxx_dialect < cxx1z
4892 && !in_system_header_at (input_location))
4893 pedwarn (input_location, 0, "fold-expressions only available "
4894 "with -std=c++1z or -std=gnu++1z");
4896 else
4897 /* Let the front end know that this expression was
4898 enclosed in parentheses. This matters in case, for
4899 example, the expression is of the form `A::B', since
4900 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4901 not. */
4902 expr = finish_parenthesized_expr (expr);
4904 /* DR 705: Wrapping an unqualified name in parentheses
4905 suppresses arg-dependent lookup. We want to pass back
4906 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4907 (c++/37862), but none of the others. */
4908 if (*idk != CP_ID_KIND_QUALIFIED)
4909 *idk = CP_ID_KIND_NONE;
4911 /* The `>' token might be the end of a template-id or
4912 template-parameter-list now. */
4913 parser->greater_than_is_operator_p
4914 = saved_greater_than_is_operator_p;
4916 /* Consume the `)'. */
4917 token = cp_lexer_peek_token (parser->lexer);
4918 location_t close_paren_loc = token->location;
4919 expr.set_range (open_paren_loc, close_paren_loc);
4920 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4921 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4922 cp_parser_skip_to_end_of_statement (parser);
4924 return expr;
4927 case CPP_OPEN_SQUARE:
4929 if (c_dialect_objc ())
4931 /* We might have an Objective-C++ message. */
4932 cp_parser_parse_tentatively (parser);
4933 tree msg = cp_parser_objc_message_expression (parser);
4934 /* If that works out, we're done ... */
4935 if (cp_parser_parse_definitely (parser))
4936 return msg;
4937 /* ... else, fall though to see if it's a lambda. */
4939 cp_expr lam = cp_parser_lambda_expression (parser);
4940 /* Don't warn about a failed tentative parse. */
4941 if (cp_parser_error_occurred (parser))
4942 return error_mark_node;
4943 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4944 return lam;
4947 case CPP_OBJC_STRING:
4948 if (c_dialect_objc ())
4949 /* We have an Objective-C++ string literal. */
4950 return cp_parser_objc_expression (parser);
4951 cp_parser_error (parser, "expected primary-expression");
4952 return error_mark_node;
4954 case CPP_KEYWORD:
4955 switch (token->keyword)
4957 /* These two are the boolean literals. */
4958 case RID_TRUE:
4959 cp_lexer_consume_token (parser->lexer);
4960 return cp_expr (boolean_true_node, token->location);
4961 case RID_FALSE:
4962 cp_lexer_consume_token (parser->lexer);
4963 return cp_expr (boolean_false_node, token->location);
4965 /* The `__null' literal. */
4966 case RID_NULL:
4967 cp_lexer_consume_token (parser->lexer);
4968 return cp_expr (null_node, token->location);
4970 /* The `nullptr' literal. */
4971 case RID_NULLPTR:
4972 cp_lexer_consume_token (parser->lexer);
4973 return cp_expr (nullptr_node, token->location);
4975 /* Recognize the `this' keyword. */
4976 case RID_THIS:
4977 cp_lexer_consume_token (parser->lexer);
4978 if (parser->local_variables_forbidden_p)
4980 error_at (token->location,
4981 "%<this%> may not be used in this context");
4982 return error_mark_node;
4984 /* Pointers cannot appear in constant-expressions. */
4985 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4986 return error_mark_node;
4987 return cp_expr (finish_this_expr (), token->location);
4989 /* The `operator' keyword can be the beginning of an
4990 id-expression. */
4991 case RID_OPERATOR:
4992 goto id_expression;
4994 case RID_FUNCTION_NAME:
4995 case RID_PRETTY_FUNCTION_NAME:
4996 case RID_C99_FUNCTION_NAME:
4998 non_integral_constant name;
5000 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5001 __func__ are the names of variables -- but they are
5002 treated specially. Therefore, they are handled here,
5003 rather than relying on the generic id-expression logic
5004 below. Grammatically, these names are id-expressions.
5006 Consume the token. */
5007 token = cp_lexer_consume_token (parser->lexer);
5009 switch (token->keyword)
5011 case RID_FUNCTION_NAME:
5012 name = NIC_FUNC_NAME;
5013 break;
5014 case RID_PRETTY_FUNCTION_NAME:
5015 name = NIC_PRETTY_FUNC;
5016 break;
5017 case RID_C99_FUNCTION_NAME:
5018 name = NIC_C99_FUNC;
5019 break;
5020 default:
5021 gcc_unreachable ();
5024 if (cp_parser_non_integral_constant_expression (parser, name))
5025 return error_mark_node;
5027 /* Look up the name. */
5028 return finish_fname (token->u.value);
5031 case RID_VA_ARG:
5033 tree expression;
5034 tree type;
5035 source_location type_location;
5036 location_t start_loc
5037 = cp_lexer_peek_token (parser->lexer)->location;
5038 /* The `__builtin_va_arg' construct is used to handle
5039 `va_arg'. Consume the `__builtin_va_arg' token. */
5040 cp_lexer_consume_token (parser->lexer);
5041 /* Look for the opening `('. */
5042 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5043 /* Now, parse the assignment-expression. */
5044 expression = cp_parser_assignment_expression (parser);
5045 /* Look for the `,'. */
5046 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5047 type_location = cp_lexer_peek_token (parser->lexer)->location;
5048 /* Parse the type-id. */
5050 type_id_in_expr_sentinel s (parser);
5051 type = cp_parser_type_id (parser);
5053 /* Look for the closing `)'. */
5054 location_t finish_loc
5055 = cp_lexer_peek_token (parser->lexer)->location;
5056 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5057 /* Using `va_arg' in a constant-expression is not
5058 allowed. */
5059 if (cp_parser_non_integral_constant_expression (parser,
5060 NIC_VA_ARG))
5061 return error_mark_node;
5062 /* Construct a location of the form:
5063 __builtin_va_arg (v, int)
5064 ~~~~~~~~~~~~~~~~~~~~~^~~~
5065 with the caret at the type, ranging from the start of the
5066 "__builtin_va_arg" token to the close paren. */
5067 location_t combined_loc
5068 = make_location (type_location, start_loc, finish_loc);
5069 return build_x_va_arg (combined_loc, expression, type);
5072 case RID_OFFSETOF:
5073 return cp_parser_builtin_offsetof (parser);
5075 case RID_HAS_NOTHROW_ASSIGN:
5076 case RID_HAS_NOTHROW_CONSTRUCTOR:
5077 case RID_HAS_NOTHROW_COPY:
5078 case RID_HAS_TRIVIAL_ASSIGN:
5079 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5080 case RID_HAS_TRIVIAL_COPY:
5081 case RID_HAS_TRIVIAL_DESTRUCTOR:
5082 case RID_HAS_VIRTUAL_DESTRUCTOR:
5083 case RID_IS_ABSTRACT:
5084 case RID_IS_BASE_OF:
5085 case RID_IS_CLASS:
5086 case RID_IS_EMPTY:
5087 case RID_IS_ENUM:
5088 case RID_IS_FINAL:
5089 case RID_IS_LITERAL_TYPE:
5090 case RID_IS_POD:
5091 case RID_IS_POLYMORPHIC:
5092 case RID_IS_SAME_AS:
5093 case RID_IS_STD_LAYOUT:
5094 case RID_IS_TRIVIAL:
5095 case RID_IS_TRIVIALLY_ASSIGNABLE:
5096 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5097 case RID_IS_TRIVIALLY_COPYABLE:
5098 case RID_IS_UNION:
5099 return cp_parser_trait_expr (parser, token->keyword);
5101 // C++ concepts
5102 case RID_REQUIRES:
5103 return cp_parser_requires_expression (parser);
5105 /* Objective-C++ expressions. */
5106 case RID_AT_ENCODE:
5107 case RID_AT_PROTOCOL:
5108 case RID_AT_SELECTOR:
5109 return cp_parser_objc_expression (parser);
5111 case RID_TEMPLATE:
5112 if (parser->in_function_body
5113 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5114 == CPP_LESS))
5116 error_at (token->location,
5117 "a template declaration cannot appear at block scope");
5118 cp_parser_skip_to_end_of_block_or_statement (parser);
5119 return error_mark_node;
5121 default:
5122 cp_parser_error (parser, "expected primary-expression");
5123 return error_mark_node;
5126 /* An id-expression can start with either an identifier, a
5127 `::' as the beginning of a qualified-id, or the "operator"
5128 keyword. */
5129 case CPP_NAME:
5130 case CPP_SCOPE:
5131 case CPP_TEMPLATE_ID:
5132 case CPP_NESTED_NAME_SPECIFIER:
5134 id_expression:
5135 cp_expr id_expression;
5136 cp_expr decl;
5137 const char *error_msg;
5138 bool template_p;
5139 bool done;
5140 cp_token *id_expr_token;
5142 /* Parse the id-expression. */
5143 id_expression
5144 = cp_parser_id_expression (parser,
5145 /*template_keyword_p=*/false,
5146 /*check_dependency_p=*/true,
5147 &template_p,
5148 /*declarator_p=*/false,
5149 /*optional_p=*/false);
5150 if (id_expression == error_mark_node)
5151 return error_mark_node;
5152 id_expr_token = token;
5153 token = cp_lexer_peek_token (parser->lexer);
5154 done = (token->type != CPP_OPEN_SQUARE
5155 && token->type != CPP_OPEN_PAREN
5156 && token->type != CPP_DOT
5157 && token->type != CPP_DEREF
5158 && token->type != CPP_PLUS_PLUS
5159 && token->type != CPP_MINUS_MINUS);
5160 /* If we have a template-id, then no further lookup is
5161 required. If the template-id was for a template-class, we
5162 will sometimes have a TYPE_DECL at this point. */
5163 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5164 || TREE_CODE (id_expression) == TYPE_DECL)
5165 decl = id_expression;
5166 /* Look up the name. */
5167 else
5169 tree ambiguous_decls;
5171 /* If we already know that this lookup is ambiguous, then
5172 we've already issued an error message; there's no reason
5173 to check again. */
5174 if (id_expr_token->type == CPP_NAME
5175 && id_expr_token->error_reported)
5177 cp_parser_simulate_error (parser);
5178 return error_mark_node;
5181 decl = cp_parser_lookup_name (parser, id_expression,
5182 none_type,
5183 template_p,
5184 /*is_namespace=*/false,
5185 /*check_dependency=*/true,
5186 &ambiguous_decls,
5187 id_expr_token->location);
5188 /* If the lookup was ambiguous, an error will already have
5189 been issued. */
5190 if (ambiguous_decls)
5191 return error_mark_node;
5193 /* In Objective-C++, we may have an Objective-C 2.0
5194 dot-syntax for classes here. */
5195 if (c_dialect_objc ()
5196 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5197 && TREE_CODE (decl) == TYPE_DECL
5198 && objc_is_class_name (decl))
5200 tree component;
5201 cp_lexer_consume_token (parser->lexer);
5202 component = cp_parser_identifier (parser);
5203 if (component == error_mark_node)
5204 return error_mark_node;
5206 tree result = objc_build_class_component_ref (id_expression,
5207 component);
5208 /* Build a location of the form:
5209 expr.component
5210 ~~~~~^~~~~~~~~
5211 with caret at the start of the component name (at
5212 input_location), ranging from the start of the id_expression
5213 to the end of the component name. */
5214 location_t combined_loc
5215 = make_location (input_location, id_expression.get_start (),
5216 get_finish (input_location));
5217 protected_set_expr_location (result, combined_loc);
5218 return result;
5221 /* In Objective-C++, an instance variable (ivar) may be preferred
5222 to whatever cp_parser_lookup_name() found.
5223 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5224 rest of c-family, we have to do a little extra work to preserve
5225 any location information in cp_expr "decl". Given that
5226 objc_lookup_ivar is implemented in "c-family" and "objc", we
5227 have a trip through the pure "tree" type, rather than cp_expr.
5228 Naively copying it back to "decl" would implicitly give the
5229 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5230 store an EXPR_LOCATION. Hence we only update "decl" (and
5231 hence its location_t) if we get back a different tree node. */
5232 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5233 id_expression);
5234 if (decl_tree != decl.get_value ())
5235 decl = cp_expr (decl_tree);
5237 /* If name lookup gives us a SCOPE_REF, then the
5238 qualifying scope was dependent. */
5239 if (TREE_CODE (decl) == SCOPE_REF)
5241 /* At this point, we do not know if DECL is a valid
5242 integral constant expression. We assume that it is
5243 in fact such an expression, so that code like:
5245 template <int N> struct A {
5246 int a[B<N>::i];
5249 is accepted. At template-instantiation time, we
5250 will check that B<N>::i is actually a constant. */
5251 return decl;
5253 /* Check to see if DECL is a local variable in a context
5254 where that is forbidden. */
5255 if (parser->local_variables_forbidden_p
5256 && local_variable_p (decl))
5258 /* It might be that we only found DECL because we are
5259 trying to be generous with pre-ISO scoping rules.
5260 For example, consider:
5262 int i;
5263 void g() {
5264 for (int i = 0; i < 10; ++i) {}
5265 extern void f(int j = i);
5268 Here, name look up will originally find the out
5269 of scope `i'. We need to issue a warning message,
5270 but then use the global `i'. */
5271 decl = check_for_out_of_scope_variable (decl);
5272 if (local_variable_p (decl))
5274 error_at (id_expr_token->location,
5275 "local variable %qD may not appear in this context",
5276 decl.get_value ());
5277 return error_mark_node;
5282 decl = (finish_id_expression
5283 (id_expression, decl, parser->scope,
5284 idk,
5285 parser->integral_constant_expression_p,
5286 parser->allow_non_integral_constant_expression_p,
5287 &parser->non_integral_constant_expression_p,
5288 template_p, done, address_p,
5289 template_arg_p,
5290 &error_msg,
5291 id_expr_token->location));
5292 if (error_msg)
5293 cp_parser_error (parser, error_msg);
5294 decl.set_location (id_expr_token->location);
5295 return decl;
5298 /* Anything else is an error. */
5299 default:
5300 cp_parser_error (parser, "expected primary-expression");
5301 return error_mark_node;
5305 static inline cp_expr
5306 cp_parser_primary_expression (cp_parser *parser,
5307 bool address_p,
5308 bool cast_p,
5309 bool template_arg_p,
5310 cp_id_kind *idk)
5312 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5313 /*decltype*/false, idk);
5316 /* Parse an id-expression.
5318 id-expression:
5319 unqualified-id
5320 qualified-id
5322 qualified-id:
5323 :: [opt] nested-name-specifier template [opt] unqualified-id
5324 :: identifier
5325 :: operator-function-id
5326 :: template-id
5328 Return a representation of the unqualified portion of the
5329 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5330 a `::' or nested-name-specifier.
5332 Often, if the id-expression was a qualified-id, the caller will
5333 want to make a SCOPE_REF to represent the qualified-id. This
5334 function does not do this in order to avoid wastefully creating
5335 SCOPE_REFs when they are not required.
5337 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5338 `template' keyword.
5340 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5341 uninstantiated templates.
5343 If *TEMPLATE_P is non-NULL, it is set to true iff the
5344 `template' keyword is used to explicitly indicate that the entity
5345 named is a template.
5347 If DECLARATOR_P is true, the id-expression is appearing as part of
5348 a declarator, rather than as part of an expression. */
5350 static cp_expr
5351 cp_parser_id_expression (cp_parser *parser,
5352 bool template_keyword_p,
5353 bool check_dependency_p,
5354 bool *template_p,
5355 bool declarator_p,
5356 bool optional_p)
5358 bool global_scope_p;
5359 bool nested_name_specifier_p;
5361 /* Assume the `template' keyword was not used. */
5362 if (template_p)
5363 *template_p = template_keyword_p;
5365 /* Look for the optional `::' operator. */
5366 global_scope_p
5367 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5368 != NULL_TREE);
5369 /* Look for the optional nested-name-specifier. */
5370 nested_name_specifier_p
5371 = (cp_parser_nested_name_specifier_opt (parser,
5372 /*typename_keyword_p=*/false,
5373 check_dependency_p,
5374 /*type_p=*/false,
5375 declarator_p)
5376 != NULL_TREE);
5377 /* If there is a nested-name-specifier, then we are looking at
5378 the first qualified-id production. */
5379 if (nested_name_specifier_p)
5381 tree saved_scope;
5382 tree saved_object_scope;
5383 tree saved_qualifying_scope;
5384 tree unqualified_id;
5385 bool is_template;
5387 /* See if the next token is the `template' keyword. */
5388 if (!template_p)
5389 template_p = &is_template;
5390 *template_p = cp_parser_optional_template_keyword (parser);
5391 /* Name lookup we do during the processing of the
5392 unqualified-id might obliterate SCOPE. */
5393 saved_scope = parser->scope;
5394 saved_object_scope = parser->object_scope;
5395 saved_qualifying_scope = parser->qualifying_scope;
5396 /* Process the final unqualified-id. */
5397 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5398 check_dependency_p,
5399 declarator_p,
5400 /*optional_p=*/false);
5401 /* Restore the SAVED_SCOPE for our caller. */
5402 parser->scope = saved_scope;
5403 parser->object_scope = saved_object_scope;
5404 parser->qualifying_scope = saved_qualifying_scope;
5406 return unqualified_id;
5408 /* Otherwise, if we are in global scope, then we are looking at one
5409 of the other qualified-id productions. */
5410 else if (global_scope_p)
5412 cp_token *token;
5413 tree id;
5415 /* Peek at the next token. */
5416 token = cp_lexer_peek_token (parser->lexer);
5418 /* If it's an identifier, and the next token is not a "<", then
5419 we can avoid the template-id case. This is an optimization
5420 for this common case. */
5421 if (token->type == CPP_NAME
5422 && !cp_parser_nth_token_starts_template_argument_list_p
5423 (parser, 2))
5424 return cp_parser_identifier (parser);
5426 cp_parser_parse_tentatively (parser);
5427 /* Try a template-id. */
5428 id = cp_parser_template_id (parser,
5429 /*template_keyword_p=*/false,
5430 /*check_dependency_p=*/true,
5431 none_type,
5432 declarator_p);
5433 /* If that worked, we're done. */
5434 if (cp_parser_parse_definitely (parser))
5435 return id;
5437 /* Peek at the next token. (Changes in the token buffer may
5438 have invalidated the pointer obtained above.) */
5439 token = cp_lexer_peek_token (parser->lexer);
5441 switch (token->type)
5443 case CPP_NAME:
5444 return cp_parser_identifier (parser);
5446 case CPP_KEYWORD:
5447 if (token->keyword == RID_OPERATOR)
5448 return cp_parser_operator_function_id (parser);
5449 /* Fall through. */
5451 default:
5452 cp_parser_error (parser, "expected id-expression");
5453 return error_mark_node;
5456 else
5457 return cp_parser_unqualified_id (parser, template_keyword_p,
5458 /*check_dependency_p=*/true,
5459 declarator_p,
5460 optional_p);
5463 /* Parse an unqualified-id.
5465 unqualified-id:
5466 identifier
5467 operator-function-id
5468 conversion-function-id
5469 ~ class-name
5470 template-id
5472 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5473 keyword, in a construct like `A::template ...'.
5475 Returns a representation of unqualified-id. For the `identifier'
5476 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5477 production a BIT_NOT_EXPR is returned; the operand of the
5478 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5479 other productions, see the documentation accompanying the
5480 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5481 names are looked up in uninstantiated templates. If DECLARATOR_P
5482 is true, the unqualified-id is appearing as part of a declarator,
5483 rather than as part of an expression. */
5485 static cp_expr
5486 cp_parser_unqualified_id (cp_parser* parser,
5487 bool template_keyword_p,
5488 bool check_dependency_p,
5489 bool declarator_p,
5490 bool optional_p)
5492 cp_token *token;
5494 /* Peek at the next token. */
5495 token = cp_lexer_peek_token (parser->lexer);
5497 switch ((int) token->type)
5499 case CPP_NAME:
5501 tree id;
5503 /* We don't know yet whether or not this will be a
5504 template-id. */
5505 cp_parser_parse_tentatively (parser);
5506 /* Try a template-id. */
5507 id = cp_parser_template_id (parser, template_keyword_p,
5508 check_dependency_p,
5509 none_type,
5510 declarator_p);
5511 /* If it worked, we're done. */
5512 if (cp_parser_parse_definitely (parser))
5513 return id;
5514 /* Otherwise, it's an ordinary identifier. */
5515 return cp_parser_identifier (parser);
5518 case CPP_TEMPLATE_ID:
5519 return cp_parser_template_id (parser, template_keyword_p,
5520 check_dependency_p,
5521 none_type,
5522 declarator_p);
5524 case CPP_COMPL:
5526 tree type_decl;
5527 tree qualifying_scope;
5528 tree object_scope;
5529 tree scope;
5530 bool done;
5532 /* Consume the `~' token. */
5533 cp_lexer_consume_token (parser->lexer);
5534 /* Parse the class-name. The standard, as written, seems to
5535 say that:
5537 template <typename T> struct S { ~S (); };
5538 template <typename T> S<T>::~S() {}
5540 is invalid, since `~' must be followed by a class-name, but
5541 `S<T>' is dependent, and so not known to be a class.
5542 That's not right; we need to look in uninstantiated
5543 templates. A further complication arises from:
5545 template <typename T> void f(T t) {
5546 t.T::~T();
5549 Here, it is not possible to look up `T' in the scope of `T'
5550 itself. We must look in both the current scope, and the
5551 scope of the containing complete expression.
5553 Yet another issue is:
5555 struct S {
5556 int S;
5557 ~S();
5560 S::~S() {}
5562 The standard does not seem to say that the `S' in `~S'
5563 should refer to the type `S' and not the data member
5564 `S::S'. */
5566 /* DR 244 says that we look up the name after the "~" in the
5567 same scope as we looked up the qualifying name. That idea
5568 isn't fully worked out; it's more complicated than that. */
5569 scope = parser->scope;
5570 object_scope = parser->object_scope;
5571 qualifying_scope = parser->qualifying_scope;
5573 /* Check for invalid scopes. */
5574 if (scope == error_mark_node)
5576 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5577 cp_lexer_consume_token (parser->lexer);
5578 return error_mark_node;
5580 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5582 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5583 error_at (token->location,
5584 "scope %qT before %<~%> is not a class-name",
5585 scope);
5586 cp_parser_simulate_error (parser);
5587 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5588 cp_lexer_consume_token (parser->lexer);
5589 return error_mark_node;
5591 gcc_assert (!scope || TYPE_P (scope));
5593 /* If the name is of the form "X::~X" it's OK even if X is a
5594 typedef. */
5595 token = cp_lexer_peek_token (parser->lexer);
5596 if (scope
5597 && token->type == CPP_NAME
5598 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5599 != CPP_LESS)
5600 && (token->u.value == TYPE_IDENTIFIER (scope)
5601 || (CLASS_TYPE_P (scope)
5602 && constructor_name_p (token->u.value, scope))))
5604 cp_lexer_consume_token (parser->lexer);
5605 return build_nt (BIT_NOT_EXPR, scope);
5608 /* ~auto means the destructor of whatever the object is. */
5609 if (cp_parser_is_keyword (token, RID_AUTO))
5611 if (cxx_dialect < cxx14)
5612 pedwarn (input_location, 0,
5613 "%<~auto%> only available with "
5614 "-std=c++14 or -std=gnu++14");
5615 cp_lexer_consume_token (parser->lexer);
5616 return build_nt (BIT_NOT_EXPR, make_auto ());
5619 /* If there was an explicit qualification (S::~T), first look
5620 in the scope given by the qualification (i.e., S).
5622 Note: in the calls to cp_parser_class_name below we pass
5623 typename_type so that lookup finds the injected-class-name
5624 rather than the constructor. */
5625 done = false;
5626 type_decl = NULL_TREE;
5627 if (scope)
5629 cp_parser_parse_tentatively (parser);
5630 type_decl = cp_parser_class_name (parser,
5631 /*typename_keyword_p=*/false,
5632 /*template_keyword_p=*/false,
5633 typename_type,
5634 /*check_dependency=*/false,
5635 /*class_head_p=*/false,
5636 declarator_p);
5637 if (cp_parser_parse_definitely (parser))
5638 done = true;
5640 /* In "N::S::~S", look in "N" as well. */
5641 if (!done && scope && qualifying_scope)
5643 cp_parser_parse_tentatively (parser);
5644 parser->scope = qualifying_scope;
5645 parser->object_scope = NULL_TREE;
5646 parser->qualifying_scope = NULL_TREE;
5647 type_decl
5648 = cp_parser_class_name (parser,
5649 /*typename_keyword_p=*/false,
5650 /*template_keyword_p=*/false,
5651 typename_type,
5652 /*check_dependency=*/false,
5653 /*class_head_p=*/false,
5654 declarator_p);
5655 if (cp_parser_parse_definitely (parser))
5656 done = true;
5658 /* In "p->S::~T", look in the scope given by "*p" as well. */
5659 else if (!done && object_scope)
5661 cp_parser_parse_tentatively (parser);
5662 parser->scope = object_scope;
5663 parser->object_scope = NULL_TREE;
5664 parser->qualifying_scope = NULL_TREE;
5665 type_decl
5666 = cp_parser_class_name (parser,
5667 /*typename_keyword_p=*/false,
5668 /*template_keyword_p=*/false,
5669 typename_type,
5670 /*check_dependency=*/false,
5671 /*class_head_p=*/false,
5672 declarator_p);
5673 if (cp_parser_parse_definitely (parser))
5674 done = true;
5676 /* Look in the surrounding context. */
5677 if (!done)
5679 parser->scope = NULL_TREE;
5680 parser->object_scope = NULL_TREE;
5681 parser->qualifying_scope = NULL_TREE;
5682 if (processing_template_decl)
5683 cp_parser_parse_tentatively (parser);
5684 type_decl
5685 = cp_parser_class_name (parser,
5686 /*typename_keyword_p=*/false,
5687 /*template_keyword_p=*/false,
5688 typename_type,
5689 /*check_dependency=*/false,
5690 /*class_head_p=*/false,
5691 declarator_p);
5692 if (processing_template_decl
5693 && ! cp_parser_parse_definitely (parser))
5695 /* We couldn't find a type with this name. If we're parsing
5696 tentatively, fail and try something else. */
5697 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5699 cp_parser_simulate_error (parser);
5700 return error_mark_node;
5702 /* Otherwise, accept it and check for a match at instantiation
5703 time. */
5704 type_decl = cp_parser_identifier (parser);
5705 if (type_decl != error_mark_node)
5706 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5707 return type_decl;
5710 /* If an error occurred, assume that the name of the
5711 destructor is the same as the name of the qualifying
5712 class. That allows us to keep parsing after running
5713 into ill-formed destructor names. */
5714 if (type_decl == error_mark_node && scope)
5715 return build_nt (BIT_NOT_EXPR, scope);
5716 else if (type_decl == error_mark_node)
5717 return error_mark_node;
5719 /* Check that destructor name and scope match. */
5720 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5722 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5723 error_at (token->location,
5724 "declaration of %<~%T%> as member of %qT",
5725 type_decl, scope);
5726 cp_parser_simulate_error (parser);
5727 return error_mark_node;
5730 /* [class.dtor]
5732 A typedef-name that names a class shall not be used as the
5733 identifier in the declarator for a destructor declaration. */
5734 if (declarator_p
5735 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5736 && !DECL_SELF_REFERENCE_P (type_decl)
5737 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5738 error_at (token->location,
5739 "typedef-name %qD used as destructor declarator",
5740 type_decl);
5742 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5745 case CPP_KEYWORD:
5746 if (token->keyword == RID_OPERATOR)
5748 cp_expr id;
5750 /* This could be a template-id, so we try that first. */
5751 cp_parser_parse_tentatively (parser);
5752 /* Try a template-id. */
5753 id = cp_parser_template_id (parser, template_keyword_p,
5754 /*check_dependency_p=*/true,
5755 none_type,
5756 declarator_p);
5757 /* If that worked, we're done. */
5758 if (cp_parser_parse_definitely (parser))
5759 return id;
5760 /* We still don't know whether we're looking at an
5761 operator-function-id or a conversion-function-id. */
5762 cp_parser_parse_tentatively (parser);
5763 /* Try an operator-function-id. */
5764 id = cp_parser_operator_function_id (parser);
5765 /* If that didn't work, try a conversion-function-id. */
5766 if (!cp_parser_parse_definitely (parser))
5767 id = cp_parser_conversion_function_id (parser);
5768 else if (UDLIT_OPER_P (id))
5770 /* 17.6.3.3.5 */
5771 const char *name = UDLIT_OP_SUFFIX (id);
5772 if (name[0] != '_' && !in_system_header_at (input_location)
5773 && declarator_p)
5774 warning (0, "literal operator suffixes not preceded by %<_%>"
5775 " are reserved for future standardization");
5778 return id;
5780 /* Fall through. */
5782 default:
5783 if (optional_p)
5784 return NULL_TREE;
5785 cp_parser_error (parser, "expected unqualified-id");
5786 return error_mark_node;
5790 /* Parse an (optional) nested-name-specifier.
5792 nested-name-specifier: [C++98]
5793 class-or-namespace-name :: nested-name-specifier [opt]
5794 class-or-namespace-name :: template nested-name-specifier [opt]
5796 nested-name-specifier: [C++0x]
5797 type-name ::
5798 namespace-name ::
5799 nested-name-specifier identifier ::
5800 nested-name-specifier template [opt] simple-template-id ::
5802 PARSER->SCOPE should be set appropriately before this function is
5803 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5804 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5805 in name lookups.
5807 Sets PARSER->SCOPE to the class (TYPE) or namespace
5808 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5809 it unchanged if there is no nested-name-specifier. Returns the new
5810 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5812 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5813 part of a declaration and/or decl-specifier. */
5815 static tree
5816 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5817 bool typename_keyword_p,
5818 bool check_dependency_p,
5819 bool type_p,
5820 bool is_declaration)
5822 bool success = false;
5823 cp_token_position start = 0;
5824 cp_token *token;
5826 /* Remember where the nested-name-specifier starts. */
5827 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5829 start = cp_lexer_token_position (parser->lexer, false);
5830 push_deferring_access_checks (dk_deferred);
5833 while (true)
5835 tree new_scope;
5836 tree old_scope;
5837 tree saved_qualifying_scope;
5838 bool template_keyword_p;
5840 /* Spot cases that cannot be the beginning of a
5841 nested-name-specifier. */
5842 token = cp_lexer_peek_token (parser->lexer);
5844 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5845 the already parsed nested-name-specifier. */
5846 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5848 /* Grab the nested-name-specifier and continue the loop. */
5849 cp_parser_pre_parsed_nested_name_specifier (parser);
5850 /* If we originally encountered this nested-name-specifier
5851 with IS_DECLARATION set to false, we will not have
5852 resolved TYPENAME_TYPEs, so we must do so here. */
5853 if (is_declaration
5854 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5856 new_scope = resolve_typename_type (parser->scope,
5857 /*only_current_p=*/false);
5858 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5859 parser->scope = new_scope;
5861 success = true;
5862 continue;
5865 /* Spot cases that cannot be the beginning of a
5866 nested-name-specifier. On the second and subsequent times
5867 through the loop, we look for the `template' keyword. */
5868 if (success && token->keyword == RID_TEMPLATE)
5870 /* A template-id can start a nested-name-specifier. */
5871 else if (token->type == CPP_TEMPLATE_ID)
5873 /* DR 743: decltype can be used in a nested-name-specifier. */
5874 else if (token_is_decltype (token))
5876 else
5878 /* If the next token is not an identifier, then it is
5879 definitely not a type-name or namespace-name. */
5880 if (token->type != CPP_NAME)
5881 break;
5882 /* If the following token is neither a `<' (to begin a
5883 template-id), nor a `::', then we are not looking at a
5884 nested-name-specifier. */
5885 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5887 if (token->type == CPP_COLON
5888 && parser->colon_corrects_to_scope_p
5889 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5891 error_at (token->location,
5892 "found %<:%> in nested-name-specifier, expected %<::%>");
5893 token->type = CPP_SCOPE;
5896 if (token->type != CPP_SCOPE
5897 && !cp_parser_nth_token_starts_template_argument_list_p
5898 (parser, 2))
5899 break;
5902 /* The nested-name-specifier is optional, so we parse
5903 tentatively. */
5904 cp_parser_parse_tentatively (parser);
5906 /* Look for the optional `template' keyword, if this isn't the
5907 first time through the loop. */
5908 if (success)
5909 template_keyword_p = cp_parser_optional_template_keyword (parser);
5910 else
5911 template_keyword_p = false;
5913 /* Save the old scope since the name lookup we are about to do
5914 might destroy it. */
5915 old_scope = parser->scope;
5916 saved_qualifying_scope = parser->qualifying_scope;
5917 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5918 look up names in "X<T>::I" in order to determine that "Y" is
5919 a template. So, if we have a typename at this point, we make
5920 an effort to look through it. */
5921 if (is_declaration
5922 && !typename_keyword_p
5923 && parser->scope
5924 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5925 parser->scope = resolve_typename_type (parser->scope,
5926 /*only_current_p=*/false);
5927 /* Parse the qualifying entity. */
5928 new_scope
5929 = cp_parser_qualifying_entity (parser,
5930 typename_keyword_p,
5931 template_keyword_p,
5932 check_dependency_p,
5933 type_p,
5934 is_declaration);
5935 /* Look for the `::' token. */
5936 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5938 /* If we found what we wanted, we keep going; otherwise, we're
5939 done. */
5940 if (!cp_parser_parse_definitely (parser))
5942 bool error_p = false;
5944 /* Restore the OLD_SCOPE since it was valid before the
5945 failed attempt at finding the last
5946 class-or-namespace-name. */
5947 parser->scope = old_scope;
5948 parser->qualifying_scope = saved_qualifying_scope;
5950 /* If the next token is a decltype, and the one after that is a
5951 `::', then the decltype has failed to resolve to a class or
5952 enumeration type. Give this error even when parsing
5953 tentatively since it can't possibly be valid--and we're going
5954 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5955 won't get another chance.*/
5956 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5957 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5958 == CPP_SCOPE))
5960 token = cp_lexer_consume_token (parser->lexer);
5961 error_at (token->location, "decltype evaluates to %qT, "
5962 "which is not a class or enumeration type",
5963 token->u.tree_check_value->value);
5964 parser->scope = error_mark_node;
5965 error_p = true;
5966 /* As below. */
5967 success = true;
5968 cp_lexer_consume_token (parser->lexer);
5971 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5972 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5974 /* If we have a non-type template-id followed by ::, it can't
5975 possibly be valid. */
5976 token = cp_lexer_peek_token (parser->lexer);
5977 tree tid = token->u.tree_check_value->value;
5978 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5979 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5981 tree tmpl = NULL_TREE;
5982 if (is_overloaded_fn (tid))
5984 tree fns = get_fns (tid);
5985 if (!OVL_CHAIN (fns))
5986 tmpl = OVL_CURRENT (fns);
5987 error_at (token->location, "function template-id %qD "
5988 "in nested-name-specifier", tid);
5990 else
5992 /* Variable template. */
5993 tmpl = TREE_OPERAND (tid, 0);
5994 gcc_assert (variable_template_p (tmpl));
5995 error_at (token->location, "variable template-id %qD "
5996 "in nested-name-specifier", tid);
5998 if (tmpl)
5999 inform (DECL_SOURCE_LOCATION (tmpl),
6000 "%qD declared here", tmpl);
6002 parser->scope = error_mark_node;
6003 error_p = true;
6004 /* As below. */
6005 success = true;
6006 cp_lexer_consume_token (parser->lexer);
6007 cp_lexer_consume_token (parser->lexer);
6011 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6012 break;
6013 /* If the next token is an identifier, and the one after
6014 that is a `::', then any valid interpretation would have
6015 found a class-or-namespace-name. */
6016 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6018 == CPP_SCOPE)
6019 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6020 != CPP_COMPL))
6022 token = cp_lexer_consume_token (parser->lexer);
6023 if (!error_p)
6025 if (!token->error_reported)
6027 tree decl;
6028 tree ambiguous_decls;
6030 decl = cp_parser_lookup_name (parser, token->u.value,
6031 none_type,
6032 /*is_template=*/false,
6033 /*is_namespace=*/false,
6034 /*check_dependency=*/true,
6035 &ambiguous_decls,
6036 token->location);
6037 if (TREE_CODE (decl) == TEMPLATE_DECL)
6038 error_at (token->location,
6039 "%qD used without template parameters",
6040 decl);
6041 else if (ambiguous_decls)
6043 // cp_parser_lookup_name has the same diagnostic,
6044 // thus make sure to emit it at most once.
6045 if (cp_parser_uncommitted_to_tentative_parse_p
6046 (parser))
6048 error_at (token->location,
6049 "reference to %qD is ambiguous",
6050 token->u.value);
6051 print_candidates (ambiguous_decls);
6053 decl = error_mark_node;
6055 else
6057 if (cxx_dialect != cxx98)
6058 cp_parser_name_lookup_error
6059 (parser, token->u.value, decl, NLE_NOT_CXX98,
6060 token->location);
6061 else
6062 cp_parser_name_lookup_error
6063 (parser, token->u.value, decl, NLE_CXX98,
6064 token->location);
6067 parser->scope = error_mark_node;
6068 error_p = true;
6069 /* Treat this as a successful nested-name-specifier
6070 due to:
6072 [basic.lookup.qual]
6074 If the name found is not a class-name (clause
6075 _class_) or namespace-name (_namespace.def_), the
6076 program is ill-formed. */
6077 success = true;
6079 cp_lexer_consume_token (parser->lexer);
6081 break;
6083 /* We've found one valid nested-name-specifier. */
6084 success = true;
6085 /* Name lookup always gives us a DECL. */
6086 if (TREE_CODE (new_scope) == TYPE_DECL)
6087 new_scope = TREE_TYPE (new_scope);
6088 /* Uses of "template" must be followed by actual templates. */
6089 if (template_keyword_p
6090 && !(CLASS_TYPE_P (new_scope)
6091 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6092 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6093 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6094 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6095 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6096 == TEMPLATE_ID_EXPR)))
6097 permerror (input_location, TYPE_P (new_scope)
6098 ? G_("%qT is not a template")
6099 : G_("%qD is not a template"),
6100 new_scope);
6101 /* If it is a class scope, try to complete it; we are about to
6102 be looking up names inside the class. */
6103 if (TYPE_P (new_scope)
6104 /* Since checking types for dependency can be expensive,
6105 avoid doing it if the type is already complete. */
6106 && !COMPLETE_TYPE_P (new_scope)
6107 /* Do not try to complete dependent types. */
6108 && !dependent_type_p (new_scope))
6110 new_scope = complete_type (new_scope);
6111 /* If it is a typedef to current class, use the current
6112 class instead, as the typedef won't have any names inside
6113 it yet. */
6114 if (!COMPLETE_TYPE_P (new_scope)
6115 && currently_open_class (new_scope))
6116 new_scope = TYPE_MAIN_VARIANT (new_scope);
6118 /* Make sure we look in the right scope the next time through
6119 the loop. */
6120 parser->scope = new_scope;
6123 /* If parsing tentatively, replace the sequence of tokens that makes
6124 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6125 token. That way, should we re-parse the token stream, we will
6126 not have to repeat the effort required to do the parse, nor will
6127 we issue duplicate error messages. */
6128 if (success && start)
6130 cp_token *token;
6132 token = cp_lexer_token_at (parser->lexer, start);
6133 /* Reset the contents of the START token. */
6134 token->type = CPP_NESTED_NAME_SPECIFIER;
6135 /* Retrieve any deferred checks. Do not pop this access checks yet
6136 so the memory will not be reclaimed during token replacing below. */
6137 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6138 token->u.tree_check_value->value = parser->scope;
6139 token->u.tree_check_value->checks = get_deferred_access_checks ();
6140 token->u.tree_check_value->qualifying_scope =
6141 parser->qualifying_scope;
6142 token->keyword = RID_MAX;
6144 /* Purge all subsequent tokens. */
6145 cp_lexer_purge_tokens_after (parser->lexer, start);
6148 if (start)
6149 pop_to_parent_deferring_access_checks ();
6151 return success ? parser->scope : NULL_TREE;
6154 /* Parse a nested-name-specifier. See
6155 cp_parser_nested_name_specifier_opt for details. This function
6156 behaves identically, except that it will an issue an error if no
6157 nested-name-specifier is present. */
6159 static tree
6160 cp_parser_nested_name_specifier (cp_parser *parser,
6161 bool typename_keyword_p,
6162 bool check_dependency_p,
6163 bool type_p,
6164 bool is_declaration)
6166 tree scope;
6168 /* Look for the nested-name-specifier. */
6169 scope = cp_parser_nested_name_specifier_opt (parser,
6170 typename_keyword_p,
6171 check_dependency_p,
6172 type_p,
6173 is_declaration);
6174 /* If it was not present, issue an error message. */
6175 if (!scope)
6177 cp_parser_error (parser, "expected nested-name-specifier");
6178 parser->scope = NULL_TREE;
6181 return scope;
6184 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6185 this is either a class-name or a namespace-name (which corresponds
6186 to the class-or-namespace-name production in the grammar). For
6187 C++0x, it can also be a type-name that refers to an enumeration
6188 type or a simple-template-id.
6190 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6191 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6192 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6193 TYPE_P is TRUE iff the next name should be taken as a class-name,
6194 even the same name is declared to be another entity in the same
6195 scope.
6197 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6198 specified by the class-or-namespace-name. If neither is found the
6199 ERROR_MARK_NODE is returned. */
6201 static tree
6202 cp_parser_qualifying_entity (cp_parser *parser,
6203 bool typename_keyword_p,
6204 bool template_keyword_p,
6205 bool check_dependency_p,
6206 bool type_p,
6207 bool is_declaration)
6209 tree saved_scope;
6210 tree saved_qualifying_scope;
6211 tree saved_object_scope;
6212 tree scope;
6213 bool only_class_p;
6214 bool successful_parse_p;
6216 /* DR 743: decltype can appear in a nested-name-specifier. */
6217 if (cp_lexer_next_token_is_decltype (parser->lexer))
6219 scope = cp_parser_decltype (parser);
6220 if (TREE_CODE (scope) != ENUMERAL_TYPE
6221 && !MAYBE_CLASS_TYPE_P (scope))
6223 cp_parser_simulate_error (parser);
6224 return error_mark_node;
6226 if (TYPE_NAME (scope))
6227 scope = TYPE_NAME (scope);
6228 return scope;
6231 /* Before we try to parse the class-name, we must save away the
6232 current PARSER->SCOPE since cp_parser_class_name will destroy
6233 it. */
6234 saved_scope = parser->scope;
6235 saved_qualifying_scope = parser->qualifying_scope;
6236 saved_object_scope = parser->object_scope;
6237 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6238 there is no need to look for a namespace-name. */
6239 only_class_p = template_keyword_p
6240 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6241 if (!only_class_p)
6242 cp_parser_parse_tentatively (parser);
6243 scope = cp_parser_class_name (parser,
6244 typename_keyword_p,
6245 template_keyword_p,
6246 type_p ? class_type : none_type,
6247 check_dependency_p,
6248 /*class_head_p=*/false,
6249 is_declaration,
6250 /*enum_ok=*/cxx_dialect > cxx98);
6251 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6252 /* If that didn't work, try for a namespace-name. */
6253 if (!only_class_p && !successful_parse_p)
6255 /* Restore the saved scope. */
6256 parser->scope = saved_scope;
6257 parser->qualifying_scope = saved_qualifying_scope;
6258 parser->object_scope = saved_object_scope;
6259 /* If we are not looking at an identifier followed by the scope
6260 resolution operator, then this is not part of a
6261 nested-name-specifier. (Note that this function is only used
6262 to parse the components of a nested-name-specifier.) */
6263 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6264 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6265 return error_mark_node;
6266 scope = cp_parser_namespace_name (parser);
6269 return scope;
6272 /* Return true if we are looking at a compound-literal, false otherwise. */
6274 static bool
6275 cp_parser_compound_literal_p (cp_parser *parser)
6277 /* Consume the `('. */
6278 cp_lexer_consume_token (parser->lexer);
6280 cp_lexer_save_tokens (parser->lexer);
6282 /* Skip tokens until the next token is a closing parenthesis.
6283 If we find the closing `)', and the next token is a `{', then
6284 we are looking at a compound-literal. */
6285 bool compound_literal_p
6286 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6287 /*consume_paren=*/true)
6288 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6290 /* Roll back the tokens we skipped. */
6291 cp_lexer_rollback_tokens (parser->lexer);
6293 return compound_literal_p;
6296 /* Parse a postfix-expression.
6298 postfix-expression:
6299 primary-expression
6300 postfix-expression [ expression ]
6301 postfix-expression ( expression-list [opt] )
6302 simple-type-specifier ( expression-list [opt] )
6303 typename :: [opt] nested-name-specifier identifier
6304 ( expression-list [opt] )
6305 typename :: [opt] nested-name-specifier template [opt] template-id
6306 ( expression-list [opt] )
6307 postfix-expression . template [opt] id-expression
6308 postfix-expression -> template [opt] id-expression
6309 postfix-expression . pseudo-destructor-name
6310 postfix-expression -> pseudo-destructor-name
6311 postfix-expression ++
6312 postfix-expression --
6313 dynamic_cast < type-id > ( expression )
6314 static_cast < type-id > ( expression )
6315 reinterpret_cast < type-id > ( expression )
6316 const_cast < type-id > ( expression )
6317 typeid ( expression )
6318 typeid ( type-id )
6320 GNU Extension:
6322 postfix-expression:
6323 ( type-id ) { initializer-list , [opt] }
6325 This extension is a GNU version of the C99 compound-literal
6326 construct. (The C99 grammar uses `type-name' instead of `type-id',
6327 but they are essentially the same concept.)
6329 If ADDRESS_P is true, the postfix expression is the operand of the
6330 `&' operator. CAST_P is true if this expression is the target of a
6331 cast.
6333 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6334 class member access expressions [expr.ref].
6336 Returns a representation of the expression. */
6338 static cp_expr
6339 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6340 bool member_access_only_p, bool decltype_p,
6341 cp_id_kind * pidk_return)
6343 cp_token *token;
6344 location_t loc;
6345 enum rid keyword;
6346 cp_id_kind idk = CP_ID_KIND_NONE;
6347 cp_expr postfix_expression = NULL_TREE;
6348 bool is_member_access = false;
6349 int saved_in_statement = -1;
6351 /* Peek at the next token. */
6352 token = cp_lexer_peek_token (parser->lexer);
6353 loc = token->location;
6354 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6356 /* Some of the productions are determined by keywords. */
6357 keyword = token->keyword;
6358 switch (keyword)
6360 case RID_DYNCAST:
6361 case RID_STATCAST:
6362 case RID_REINTCAST:
6363 case RID_CONSTCAST:
6365 tree type;
6366 cp_expr expression;
6367 const char *saved_message;
6368 bool saved_in_type_id_in_expr_p;
6370 /* All of these can be handled in the same way from the point
6371 of view of parsing. Begin by consuming the token
6372 identifying the cast. */
6373 cp_lexer_consume_token (parser->lexer);
6375 /* New types cannot be defined in the cast. */
6376 saved_message = parser->type_definition_forbidden_message;
6377 parser->type_definition_forbidden_message
6378 = G_("types may not be defined in casts");
6380 /* Look for the opening `<'. */
6381 cp_parser_require (parser, CPP_LESS, RT_LESS);
6382 /* Parse the type to which we are casting. */
6383 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6384 parser->in_type_id_in_expr_p = true;
6385 type = cp_parser_type_id (parser);
6386 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6387 /* Look for the closing `>'. */
6388 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6389 /* Restore the old message. */
6390 parser->type_definition_forbidden_message = saved_message;
6392 bool saved_greater_than_is_operator_p
6393 = parser->greater_than_is_operator_p;
6394 parser->greater_than_is_operator_p = true;
6396 /* And the expression which is being cast. */
6397 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6398 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6399 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6400 RT_CLOSE_PAREN);
6401 location_t end_loc = close_paren ?
6402 close_paren->location : UNKNOWN_LOCATION;
6404 parser->greater_than_is_operator_p
6405 = saved_greater_than_is_operator_p;
6407 /* Only type conversions to integral or enumeration types
6408 can be used in constant-expressions. */
6409 if (!cast_valid_in_integral_constant_expression_p (type)
6410 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6411 return error_mark_node;
6413 switch (keyword)
6415 case RID_DYNCAST:
6416 postfix_expression
6417 = build_dynamic_cast (type, expression, tf_warning_or_error);
6418 break;
6419 case RID_STATCAST:
6420 postfix_expression
6421 = build_static_cast (type, expression, tf_warning_or_error);
6422 break;
6423 case RID_REINTCAST:
6424 postfix_expression
6425 = build_reinterpret_cast (type, expression,
6426 tf_warning_or_error);
6427 break;
6428 case RID_CONSTCAST:
6429 postfix_expression
6430 = build_const_cast (type, expression, tf_warning_or_error);
6431 break;
6432 default:
6433 gcc_unreachable ();
6436 /* Construct a location e.g. :
6437 reinterpret_cast <int *> (expr)
6438 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6439 ranging from the start of the "*_cast" token to the final closing
6440 paren, with the caret at the start. */
6441 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6442 postfix_expression.set_location (cp_cast_loc);
6444 break;
6446 case RID_TYPEID:
6448 tree type;
6449 const char *saved_message;
6450 bool saved_in_type_id_in_expr_p;
6452 /* Consume the `typeid' token. */
6453 cp_lexer_consume_token (parser->lexer);
6454 /* Look for the `(' token. */
6455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6456 /* Types cannot be defined in a `typeid' expression. */
6457 saved_message = parser->type_definition_forbidden_message;
6458 parser->type_definition_forbidden_message
6459 = G_("types may not be defined in a %<typeid%> expression");
6460 /* We can't be sure yet whether we're looking at a type-id or an
6461 expression. */
6462 cp_parser_parse_tentatively (parser);
6463 /* Try a type-id first. */
6464 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6465 parser->in_type_id_in_expr_p = true;
6466 type = cp_parser_type_id (parser);
6467 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6468 /* Look for the `)' token. Otherwise, we can't be sure that
6469 we're not looking at an expression: consider `typeid (int
6470 (3))', for example. */
6471 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6472 /* If all went well, simply lookup the type-id. */
6473 if (cp_parser_parse_definitely (parser))
6474 postfix_expression = get_typeid (type, tf_warning_or_error);
6475 /* Otherwise, fall back to the expression variant. */
6476 else
6478 tree expression;
6480 /* Look for an expression. */
6481 expression = cp_parser_expression (parser, & idk);
6482 /* Compute its typeid. */
6483 postfix_expression = build_typeid (expression, tf_warning_or_error);
6484 /* Look for the `)' token. */
6485 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6487 /* Restore the saved message. */
6488 parser->type_definition_forbidden_message = saved_message;
6489 /* `typeid' may not appear in an integral constant expression. */
6490 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6491 return error_mark_node;
6493 break;
6495 case RID_TYPENAME:
6497 tree type;
6498 /* The syntax permitted here is the same permitted for an
6499 elaborated-type-specifier. */
6500 ++parser->prevent_constrained_type_specifiers;
6501 type = cp_parser_elaborated_type_specifier (parser,
6502 /*is_friend=*/false,
6503 /*is_declaration=*/false);
6504 --parser->prevent_constrained_type_specifiers;
6505 postfix_expression = cp_parser_functional_cast (parser, type);
6507 break;
6509 case RID_CILK_SPAWN:
6511 location_t cilk_spawn_loc
6512 = cp_lexer_peek_token (parser->lexer)->location;
6513 cp_lexer_consume_token (parser->lexer);
6514 token = cp_lexer_peek_token (parser->lexer);
6515 if (token->type == CPP_SEMICOLON)
6517 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6518 "an expression");
6519 postfix_expression = error_mark_node;
6520 break;
6522 else if (!current_function_decl)
6524 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6525 "inside a function");
6526 postfix_expression = error_mark_node;
6527 break;
6529 else
6531 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6532 saved_in_statement = parser->in_statement;
6533 parser->in_statement |= IN_CILK_SPAWN;
6535 cfun->calls_cilk_spawn = 1;
6536 postfix_expression =
6537 cp_parser_postfix_expression (parser, false, false,
6538 false, false, &idk);
6539 if (!flag_cilkplus)
6541 error_at (token->location, "-fcilkplus must be enabled to use"
6542 " %<_Cilk_spawn%>");
6543 cfun->calls_cilk_spawn = 0;
6545 else if (saved_in_statement & IN_CILK_SPAWN)
6547 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6548 "are not permitted");
6549 postfix_expression = error_mark_node;
6550 cfun->calls_cilk_spawn = 0;
6552 else
6554 location_t loc = postfix_expression.get_location ();
6555 postfix_expression = build_cilk_spawn (token->location,
6556 postfix_expression);
6557 /* Build a location of the form:
6558 _Cilk_spawn expr
6559 ~~~~~~~~~~~~^~~~
6560 with caret at the expr, ranging from the start of the
6561 _Cilk_spawn token to the end of the expression. */
6562 location_t combined_loc =
6563 make_location (loc, cilk_spawn_loc, get_finish (loc));
6564 postfix_expression.set_location (combined_loc);
6565 if (postfix_expression != error_mark_node)
6566 SET_EXPR_LOCATION (postfix_expression, input_location);
6567 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6569 break;
6572 case RID_BUILTIN_SHUFFLE:
6574 vec<tree, va_gc> *vec;
6575 unsigned int i;
6576 tree p;
6578 cp_lexer_consume_token (parser->lexer);
6579 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6580 /*cast_p=*/false, /*allow_expansion_p=*/true,
6581 /*non_constant_p=*/NULL);
6582 if (vec == NULL)
6583 return error_mark_node;
6585 FOR_EACH_VEC_ELT (*vec, i, p)
6586 mark_exp_read (p);
6588 if (vec->length () == 2)
6589 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6590 tf_warning_or_error);
6591 else if (vec->length () == 3)
6592 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6593 tf_warning_or_error);
6594 else
6596 error_at (loc, "wrong number of arguments to "
6597 "%<__builtin_shuffle%>");
6598 return error_mark_node;
6600 break;
6603 default:
6605 tree type;
6607 /* If the next thing is a simple-type-specifier, we may be
6608 looking at a functional cast. We could also be looking at
6609 an id-expression. So, we try the functional cast, and if
6610 that doesn't work we fall back to the primary-expression. */
6611 cp_parser_parse_tentatively (parser);
6612 /* Look for the simple-type-specifier. */
6613 ++parser->prevent_constrained_type_specifiers;
6614 type = cp_parser_simple_type_specifier (parser,
6615 /*decl_specs=*/NULL,
6616 CP_PARSER_FLAGS_NONE);
6617 --parser->prevent_constrained_type_specifiers;
6618 /* Parse the cast itself. */
6619 if (!cp_parser_error_occurred (parser))
6620 postfix_expression
6621 = cp_parser_functional_cast (parser, type);
6622 /* If that worked, we're done. */
6623 if (cp_parser_parse_definitely (parser))
6624 break;
6626 /* If the functional-cast didn't work out, try a
6627 compound-literal. */
6628 if (cp_parser_allow_gnu_extensions_p (parser)
6629 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6631 cp_expr initializer = NULL_TREE;
6633 cp_parser_parse_tentatively (parser);
6635 /* Avoid calling cp_parser_type_id pointlessly, see comment
6636 in cp_parser_cast_expression about c++/29234. */
6637 if (!cp_parser_compound_literal_p (parser))
6638 cp_parser_simulate_error (parser);
6639 else
6641 /* Parse the type. */
6642 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6643 parser->in_type_id_in_expr_p = true;
6644 type = cp_parser_type_id (parser);
6645 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6646 /* Look for the `)'. */
6647 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6650 /* If things aren't going well, there's no need to
6651 keep going. */
6652 if (!cp_parser_error_occurred (parser))
6654 bool non_constant_p;
6655 /* Parse the brace-enclosed initializer list. */
6656 initializer = cp_parser_braced_list (parser,
6657 &non_constant_p);
6659 /* If that worked, we're definitely looking at a
6660 compound-literal expression. */
6661 if (cp_parser_parse_definitely (parser))
6663 /* Warn the user that a compound literal is not
6664 allowed in standard C++. */
6665 pedwarn (input_location, OPT_Wpedantic,
6666 "ISO C++ forbids compound-literals");
6667 /* For simplicity, we disallow compound literals in
6668 constant-expressions. We could
6669 allow compound literals of integer type, whose
6670 initializer was a constant, in constant
6671 expressions. Permitting that usage, as a further
6672 extension, would not change the meaning of any
6673 currently accepted programs. (Of course, as
6674 compound literals are not part of ISO C++, the
6675 standard has nothing to say.) */
6676 if (cp_parser_non_integral_constant_expression (parser,
6677 NIC_NCC))
6679 postfix_expression = error_mark_node;
6680 break;
6682 /* Form the representation of the compound-literal. */
6683 postfix_expression
6684 = finish_compound_literal (type, initializer,
6685 tf_warning_or_error);
6686 postfix_expression.set_location (initializer.get_location ());
6687 break;
6691 /* It must be a primary-expression. */
6692 postfix_expression
6693 = cp_parser_primary_expression (parser, address_p, cast_p,
6694 /*template_arg_p=*/false,
6695 decltype_p,
6696 &idk);
6698 break;
6701 /* Note that we don't need to worry about calling build_cplus_new on a
6702 class-valued CALL_EXPR in decltype when it isn't the end of the
6703 postfix-expression; unary_complex_lvalue will take care of that for
6704 all these cases. */
6706 /* Keep looping until the postfix-expression is complete. */
6707 while (true)
6709 if (idk == CP_ID_KIND_UNQUALIFIED
6710 && identifier_p (postfix_expression)
6711 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6712 /* It is not a Koenig lookup function call. */
6713 postfix_expression
6714 = unqualified_name_lookup_error (postfix_expression);
6716 /* Peek at the next token. */
6717 token = cp_lexer_peek_token (parser->lexer);
6719 switch (token->type)
6721 case CPP_OPEN_SQUARE:
6722 if (cp_next_tokens_can_be_std_attribute_p (parser))
6724 cp_parser_error (parser,
6725 "two consecutive %<[%> shall "
6726 "only introduce an attribute");
6727 return error_mark_node;
6729 postfix_expression
6730 = cp_parser_postfix_open_square_expression (parser,
6731 postfix_expression,
6732 false,
6733 decltype_p);
6734 postfix_expression.set_range (start_loc,
6735 postfix_expression.get_location ());
6737 idk = CP_ID_KIND_NONE;
6738 is_member_access = false;
6739 break;
6741 case CPP_OPEN_PAREN:
6742 /* postfix-expression ( expression-list [opt] ) */
6744 bool koenig_p;
6745 bool is_builtin_constant_p;
6746 bool saved_integral_constant_expression_p = false;
6747 bool saved_non_integral_constant_expression_p = false;
6748 tsubst_flags_t complain = complain_flags (decltype_p);
6749 vec<tree, va_gc> *args;
6750 location_t close_paren_loc = UNKNOWN_LOCATION;
6752 is_member_access = false;
6754 is_builtin_constant_p
6755 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6756 if (is_builtin_constant_p)
6758 /* The whole point of __builtin_constant_p is to allow
6759 non-constant expressions to appear as arguments. */
6760 saved_integral_constant_expression_p
6761 = parser->integral_constant_expression_p;
6762 saved_non_integral_constant_expression_p
6763 = parser->non_integral_constant_expression_p;
6764 parser->integral_constant_expression_p = false;
6766 args = (cp_parser_parenthesized_expression_list
6767 (parser, non_attr,
6768 /*cast_p=*/false, /*allow_expansion_p=*/true,
6769 /*non_constant_p=*/NULL,
6770 /*close_paren_loc=*/&close_paren_loc));
6771 if (is_builtin_constant_p)
6773 parser->integral_constant_expression_p
6774 = saved_integral_constant_expression_p;
6775 parser->non_integral_constant_expression_p
6776 = saved_non_integral_constant_expression_p;
6779 if (args == NULL)
6781 postfix_expression = error_mark_node;
6782 break;
6785 /* Function calls are not permitted in
6786 constant-expressions. */
6787 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6788 && cp_parser_non_integral_constant_expression (parser,
6789 NIC_FUNC_CALL))
6791 postfix_expression = error_mark_node;
6792 release_tree_vector (args);
6793 break;
6796 koenig_p = false;
6797 if (idk == CP_ID_KIND_UNQUALIFIED
6798 || idk == CP_ID_KIND_TEMPLATE_ID)
6800 if (identifier_p (postfix_expression))
6802 if (!args->is_empty ())
6804 koenig_p = true;
6805 if (!any_type_dependent_arguments_p (args))
6806 postfix_expression
6807 = perform_koenig_lookup (postfix_expression, args,
6808 complain);
6810 else
6811 postfix_expression
6812 = unqualified_fn_lookup_error (postfix_expression);
6814 /* We do not perform argument-dependent lookup if
6815 normal lookup finds a non-function, in accordance
6816 with the expected resolution of DR 218. */
6817 else if (!args->is_empty ()
6818 && is_overloaded_fn (postfix_expression))
6820 tree fn = get_first_fn (postfix_expression);
6821 fn = STRIP_TEMPLATE (fn);
6823 /* Do not do argument dependent lookup if regular
6824 lookup finds a member function or a block-scope
6825 function declaration. [basic.lookup.argdep]/3 */
6826 if (!DECL_FUNCTION_MEMBER_P (fn)
6827 && !DECL_LOCAL_FUNCTION_P (fn))
6829 koenig_p = true;
6830 if (!any_type_dependent_arguments_p (args))
6831 postfix_expression
6832 = perform_koenig_lookup (postfix_expression, args,
6833 complain);
6838 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6839 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6840 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6841 && vec_safe_length (args) == 3)
6843 tree arg0 = (*args)[0];
6844 tree arg1 = (*args)[1];
6845 tree arg2 = (*args)[2];
6846 int literal_mask = ((!!integer_zerop (arg1) << 1)
6847 | (!!integer_zerop (arg2) << 2));
6848 if (TREE_CODE (arg2) == CONST_DECL)
6849 arg2 = DECL_INITIAL (arg2);
6850 warn_for_memset (input_location, arg0, arg2, literal_mask);
6853 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6855 tree instance = TREE_OPERAND (postfix_expression, 0);
6856 tree fn = TREE_OPERAND (postfix_expression, 1);
6858 if (processing_template_decl
6859 && (type_dependent_object_expression_p (instance)
6860 || (!BASELINK_P (fn)
6861 && TREE_CODE (fn) != FIELD_DECL)
6862 || type_dependent_expression_p (fn)
6863 || any_type_dependent_arguments_p (args)))
6865 postfix_expression
6866 = build_nt_call_vec (postfix_expression, args);
6867 release_tree_vector (args);
6868 break;
6871 if (BASELINK_P (fn))
6873 postfix_expression
6874 = (build_new_method_call
6875 (instance, fn, &args, NULL_TREE,
6876 (idk == CP_ID_KIND_QUALIFIED
6877 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6878 : LOOKUP_NORMAL),
6879 /*fn_p=*/NULL,
6880 complain));
6882 else
6883 postfix_expression
6884 = finish_call_expr (postfix_expression, &args,
6885 /*disallow_virtual=*/false,
6886 /*koenig_p=*/false,
6887 complain);
6889 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6890 || TREE_CODE (postfix_expression) == MEMBER_REF
6891 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6892 postfix_expression = (build_offset_ref_call_from_tree
6893 (postfix_expression, &args,
6894 complain));
6895 else if (idk == CP_ID_KIND_QUALIFIED)
6896 /* A call to a static class member, or a namespace-scope
6897 function. */
6898 postfix_expression
6899 = finish_call_expr (postfix_expression, &args,
6900 /*disallow_virtual=*/true,
6901 koenig_p,
6902 complain);
6903 else
6904 /* All other function calls. */
6905 postfix_expression
6906 = finish_call_expr (postfix_expression, &args,
6907 /*disallow_virtual=*/false,
6908 koenig_p,
6909 complain);
6911 if (close_paren_loc != UNKNOWN_LOCATION)
6913 location_t combined_loc = make_location (token->location,
6914 start_loc,
6915 close_paren_loc);
6916 postfix_expression.set_location (combined_loc);
6919 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6920 idk = CP_ID_KIND_NONE;
6922 release_tree_vector (args);
6924 break;
6926 case CPP_DOT:
6927 case CPP_DEREF:
6928 /* postfix-expression . template [opt] id-expression
6929 postfix-expression . pseudo-destructor-name
6930 postfix-expression -> template [opt] id-expression
6931 postfix-expression -> pseudo-destructor-name */
6933 /* Consume the `.' or `->' operator. */
6934 cp_lexer_consume_token (parser->lexer);
6936 postfix_expression
6937 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6938 postfix_expression,
6939 false, &idk, loc);
6941 is_member_access = true;
6942 break;
6944 case CPP_PLUS_PLUS:
6945 /* postfix-expression ++ */
6946 /* Consume the `++' token. */
6947 cp_lexer_consume_token (parser->lexer);
6948 /* Generate a representation for the complete expression. */
6949 postfix_expression
6950 = finish_increment_expr (postfix_expression,
6951 POSTINCREMENT_EXPR);
6952 /* Increments may not appear in constant-expressions. */
6953 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6954 postfix_expression = error_mark_node;
6955 idk = CP_ID_KIND_NONE;
6956 is_member_access = false;
6957 break;
6959 case CPP_MINUS_MINUS:
6960 /* postfix-expression -- */
6961 /* Consume the `--' token. */
6962 cp_lexer_consume_token (parser->lexer);
6963 /* Generate a representation for the complete expression. */
6964 postfix_expression
6965 = finish_increment_expr (postfix_expression,
6966 POSTDECREMENT_EXPR);
6967 /* Decrements may not appear in constant-expressions. */
6968 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6969 postfix_expression = error_mark_node;
6970 idk = CP_ID_KIND_NONE;
6971 is_member_access = false;
6972 break;
6974 default:
6975 if (pidk_return != NULL)
6976 * pidk_return = idk;
6977 if (member_access_only_p)
6978 return is_member_access
6979 ? postfix_expression
6980 : cp_expr (error_mark_node);
6981 else
6982 return postfix_expression;
6986 /* We should never get here. */
6987 gcc_unreachable ();
6988 return error_mark_node;
6991 /* This function parses Cilk Plus array notations. If a normal array expr. is
6992 parsed then the array index is passed back to the caller through *INIT_INDEX
6993 and the function returns a NULL_TREE. If array notation expr. is parsed,
6994 then *INIT_INDEX is ignored by the caller and the function returns
6995 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6996 error_mark_node. */
6998 static tree
6999 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7000 tree array_value)
7002 cp_token *token = NULL;
7003 tree length_index, stride = NULL_TREE, value_tree, array_type;
7004 if (!array_value || array_value == error_mark_node)
7006 cp_parser_skip_to_end_of_statement (parser);
7007 return error_mark_node;
7010 array_type = TREE_TYPE (array_value);
7012 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7013 parser->colon_corrects_to_scope_p = false;
7014 token = cp_lexer_peek_token (parser->lexer);
7016 if (!token)
7018 cp_parser_error (parser, "expected %<:%> or numeral");
7019 return error_mark_node;
7021 else if (token->type == CPP_COLON)
7023 /* Consume the ':'. */
7024 cp_lexer_consume_token (parser->lexer);
7026 /* If we are here, then we have a case like this A[:]. */
7027 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7029 cp_parser_error (parser, "expected %<]%>");
7030 cp_parser_skip_to_end_of_statement (parser);
7031 return error_mark_node;
7033 *init_index = NULL_TREE;
7034 stride = NULL_TREE;
7035 length_index = NULL_TREE;
7037 else
7039 /* If we are here, then there are three valid possibilities:
7040 1. ARRAY [ EXP ]
7041 2. ARRAY [ EXP : EXP ]
7042 3. ARRAY [ EXP : EXP : EXP ] */
7044 *init_index = cp_parser_expression (parser);
7045 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7047 /* This indicates that we have a normal array expression. */
7048 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7049 return NULL_TREE;
7052 /* Consume the ':'. */
7053 cp_lexer_consume_token (parser->lexer);
7054 length_index = cp_parser_expression (parser);
7055 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7057 cp_lexer_consume_token (parser->lexer);
7058 stride = cp_parser_expression (parser);
7061 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7063 if (*init_index == error_mark_node || length_index == error_mark_node
7064 || stride == error_mark_node || array_type == error_mark_node)
7066 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7067 cp_lexer_consume_token (parser->lexer);
7068 return error_mark_node;
7070 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7072 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7073 length_index, stride, array_type);
7074 return value_tree;
7077 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7078 by cp_parser_builtin_offsetof. We're looking for
7080 postfix-expression [ expression ]
7081 postfix-expression [ braced-init-list ] (C++11)
7083 FOR_OFFSETOF is set if we're being called in that context, which
7084 changes how we deal with integer constant expressions. */
7086 static tree
7087 cp_parser_postfix_open_square_expression (cp_parser *parser,
7088 tree postfix_expression,
7089 bool for_offsetof,
7090 bool decltype_p)
7092 tree index = NULL_TREE;
7093 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7094 bool saved_greater_than_is_operator_p;
7096 /* Consume the `[' token. */
7097 cp_lexer_consume_token (parser->lexer);
7099 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7100 parser->greater_than_is_operator_p = true;
7102 /* Parse the index expression. */
7103 /* ??? For offsetof, there is a question of what to allow here. If
7104 offsetof is not being used in an integral constant expression context,
7105 then we *could* get the right answer by computing the value at runtime.
7106 If we are in an integral constant expression context, then we might
7107 could accept any constant expression; hard to say without analysis.
7108 Rather than open the barn door too wide right away, allow only integer
7109 constant expressions here. */
7110 if (for_offsetof)
7111 index = cp_parser_constant_expression (parser);
7112 else
7114 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7116 bool expr_nonconst_p;
7117 cp_lexer_set_source_position (parser->lexer);
7118 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7119 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7120 if (flag_cilkplus
7121 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7123 error_at (cp_lexer_peek_token (parser->lexer)->location,
7124 "braced list index is not allowed with array "
7125 "notation");
7126 cp_parser_skip_to_end_of_statement (parser);
7127 return error_mark_node;
7130 else if (flag_cilkplus)
7132 /* Here are have these two options:
7133 ARRAY[EXP : EXP] - Array notation expr with default
7134 stride of 1.
7135 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7136 stride. */
7137 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7138 postfix_expression);
7139 if (an_exp)
7140 return an_exp;
7142 else
7143 index = cp_parser_expression (parser);
7146 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7148 /* Look for the closing `]'. */
7149 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7151 /* Build the ARRAY_REF. */
7152 postfix_expression = grok_array_decl (loc, postfix_expression,
7153 index, decltype_p);
7155 /* When not doing offsetof, array references are not permitted in
7156 constant-expressions. */
7157 if (!for_offsetof
7158 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7159 postfix_expression = error_mark_node;
7161 return postfix_expression;
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 . template [opt] id-expression
7168 postfix-expression . pseudo-destructor-name
7169 postfix-expression -> template [opt] id-expression
7170 postfix-expression -> pseudo-destructor-name
7172 FOR_OFFSETOF is set if we're being called in that context. That sorta
7173 limits what of the above we'll actually accept, but nevermind.
7174 TOKEN_TYPE is the "." or "->" token, which will already have been
7175 removed from the stream. */
7177 static tree
7178 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7179 enum cpp_ttype token_type,
7180 cp_expr postfix_expression,
7181 bool for_offsetof, cp_id_kind *idk,
7182 location_t location)
7184 tree name;
7185 bool dependent_p;
7186 bool pseudo_destructor_p;
7187 tree scope = NULL_TREE;
7188 location_t start_loc = postfix_expression.get_start ();
7190 /* If this is a `->' operator, dereference the pointer. */
7191 if (token_type == CPP_DEREF)
7192 postfix_expression = build_x_arrow (location, postfix_expression,
7193 tf_warning_or_error);
7194 /* Check to see whether or not the expression is type-dependent and
7195 not the current instantiation. */
7196 dependent_p = type_dependent_object_expression_p (postfix_expression);
7197 /* The identifier following the `->' or `.' is not qualified. */
7198 parser->scope = NULL_TREE;
7199 parser->qualifying_scope = NULL_TREE;
7200 parser->object_scope = NULL_TREE;
7201 *idk = CP_ID_KIND_NONE;
7203 /* Enter the scope corresponding to the type of the object
7204 given by the POSTFIX_EXPRESSION. */
7205 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7207 scope = TREE_TYPE (postfix_expression);
7208 /* According to the standard, no expression should ever have
7209 reference type. Unfortunately, we do not currently match
7210 the standard in this respect in that our internal representation
7211 of an expression may have reference type even when the standard
7212 says it does not. Therefore, we have to manually obtain the
7213 underlying type here. */
7214 scope = non_reference (scope);
7215 /* The type of the POSTFIX_EXPRESSION must be complete. */
7216 /* Unlike the object expression in other contexts, *this is not
7217 required to be of complete type for purposes of class member
7218 access (5.2.5) outside the member function body. */
7219 if (postfix_expression != current_class_ref
7220 && !(processing_template_decl
7221 && current_class_type
7222 && (same_type_ignoring_top_level_qualifiers_p
7223 (scope, current_class_type))))
7224 scope = complete_type_or_else (scope, postfix_expression);
7225 /* Let the name lookup machinery know that we are processing a
7226 class member access expression. */
7227 parser->context->object_type = scope;
7228 /* If something went wrong, we want to be able to discern that case,
7229 as opposed to the case where there was no SCOPE due to the type
7230 of expression being dependent. */
7231 if (!scope)
7232 scope = error_mark_node;
7233 /* If the SCOPE was erroneous, make the various semantic analysis
7234 functions exit quickly -- and without issuing additional error
7235 messages. */
7236 if (scope == error_mark_node)
7237 postfix_expression = error_mark_node;
7239 else
7240 /* Tell cp_parser_lookup_name that there was an object, even though it's
7241 type-dependent. */
7242 parser->context->object_type = unknown_type_node;
7244 /* Assume this expression is not a pseudo-destructor access. */
7245 pseudo_destructor_p = false;
7247 /* If the SCOPE is a scalar type, then, if this is a valid program,
7248 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7249 is type dependent, it can be pseudo-destructor-name or something else.
7250 Try to parse it as pseudo-destructor-name first. */
7251 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7253 tree s;
7254 tree type;
7256 cp_parser_parse_tentatively (parser);
7257 /* Parse the pseudo-destructor-name. */
7258 s = NULL_TREE;
7259 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7260 &s, &type);
7261 if (dependent_p
7262 && (cp_parser_error_occurred (parser)
7263 || !SCALAR_TYPE_P (type)))
7264 cp_parser_abort_tentative_parse (parser);
7265 else if (cp_parser_parse_definitely (parser))
7267 pseudo_destructor_p = true;
7268 postfix_expression
7269 = finish_pseudo_destructor_expr (postfix_expression,
7270 s, type, location);
7274 if (!pseudo_destructor_p)
7276 /* If the SCOPE is not a scalar type, we are looking at an
7277 ordinary class member access expression, rather than a
7278 pseudo-destructor-name. */
7279 bool template_p;
7280 cp_token *token = cp_lexer_peek_token (parser->lexer);
7281 /* Parse the id-expression. */
7282 name = (cp_parser_id_expression
7283 (parser,
7284 cp_parser_optional_template_keyword (parser),
7285 /*check_dependency_p=*/true,
7286 &template_p,
7287 /*declarator_p=*/false,
7288 /*optional_p=*/false));
7289 /* In general, build a SCOPE_REF if the member name is qualified.
7290 However, if the name was not dependent and has already been
7291 resolved; there is no need to build the SCOPE_REF. For example;
7293 struct X { void f(); };
7294 template <typename T> void f(T* t) { t->X::f(); }
7296 Even though "t" is dependent, "X::f" is not and has been resolved
7297 to a BASELINK; there is no need to include scope information. */
7299 /* But we do need to remember that there was an explicit scope for
7300 virtual function calls. */
7301 if (parser->scope)
7302 *idk = CP_ID_KIND_QUALIFIED;
7304 /* If the name is a template-id that names a type, we will get a
7305 TYPE_DECL here. That is invalid code. */
7306 if (TREE_CODE (name) == TYPE_DECL)
7308 error_at (token->location, "invalid use of %qD", name);
7309 postfix_expression = error_mark_node;
7311 else
7313 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7315 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7317 error_at (token->location, "%<%D::%D%> is not a class member",
7318 parser->scope, name);
7319 postfix_expression = error_mark_node;
7321 else
7322 name = build_qualified_name (/*type=*/NULL_TREE,
7323 parser->scope,
7324 name,
7325 template_p);
7326 parser->scope = NULL_TREE;
7327 parser->qualifying_scope = NULL_TREE;
7328 parser->object_scope = NULL_TREE;
7330 if (parser->scope && name && BASELINK_P (name))
7331 adjust_result_of_qualified_name_lookup
7332 (name, parser->scope, scope);
7333 postfix_expression
7334 = finish_class_member_access_expr (postfix_expression, name,
7335 template_p,
7336 tf_warning_or_error);
7337 /* Build a location e.g.:
7338 ptr->access_expr
7339 ~~~^~~~~~~~~~~~~
7340 where the caret is at the deref token, ranging from
7341 the start of postfix_expression to the end of the access expr. */
7342 location_t end_loc
7343 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7344 location_t combined_loc
7345 = make_location (input_location, start_loc, end_loc);
7346 protected_set_expr_location (postfix_expression, combined_loc);
7350 /* We no longer need to look up names in the scope of the object on
7351 the left-hand side of the `.' or `->' operator. */
7352 parser->context->object_type = NULL_TREE;
7354 /* Outside of offsetof, these operators may not appear in
7355 constant-expressions. */
7356 if (!for_offsetof
7357 && (cp_parser_non_integral_constant_expression
7358 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7359 postfix_expression = error_mark_node;
7361 return postfix_expression;
7364 /* Parse a parenthesized expression-list.
7366 expression-list:
7367 assignment-expression
7368 expression-list, assignment-expression
7370 attribute-list:
7371 expression-list
7372 identifier
7373 identifier, expression-list
7375 CAST_P is true if this expression is the target of a cast.
7377 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7378 argument pack.
7380 Returns a vector of trees. Each element is a representation of an
7381 assignment-expression. NULL is returned if the ( and or ) are
7382 missing. An empty, but allocated, vector is returned on no
7383 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7384 if we are parsing an attribute list for an attribute that wants a
7385 plain identifier argument, normal_attr for an attribute that wants
7386 an expression, or non_attr if we aren't parsing an attribute list. If
7387 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7388 not all of the expressions in the list were constant.
7389 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7390 will be written to with the location of the closing parenthesis. If
7391 an error occurs, it may or may not be written to. */
7393 static vec<tree, va_gc> *
7394 cp_parser_parenthesized_expression_list (cp_parser* parser,
7395 int is_attribute_list,
7396 bool cast_p,
7397 bool allow_expansion_p,
7398 bool *non_constant_p,
7399 location_t *close_paren_loc)
7401 vec<tree, va_gc> *expression_list;
7402 bool fold_expr_p = is_attribute_list != non_attr;
7403 tree identifier = NULL_TREE;
7404 bool saved_greater_than_is_operator_p;
7406 /* Assume all the expressions will be constant. */
7407 if (non_constant_p)
7408 *non_constant_p = false;
7410 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7411 return NULL;
7413 expression_list = make_tree_vector ();
7415 /* Within a parenthesized expression, a `>' token is always
7416 the greater-than operator. */
7417 saved_greater_than_is_operator_p
7418 = parser->greater_than_is_operator_p;
7419 parser->greater_than_is_operator_p = true;
7421 /* Consume expressions until there are no more. */
7422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7423 while (true)
7425 tree expr;
7427 /* At the beginning of attribute lists, check to see if the
7428 next token is an identifier. */
7429 if (is_attribute_list == id_attr
7430 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7432 cp_token *token;
7434 /* Consume the identifier. */
7435 token = cp_lexer_consume_token (parser->lexer);
7436 /* Save the identifier. */
7437 identifier = token->u.value;
7439 else
7441 bool expr_non_constant_p;
7443 /* Parse the next assignment-expression. */
7444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7446 /* A braced-init-list. */
7447 cp_lexer_set_source_position (parser->lexer);
7448 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7449 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7450 if (non_constant_p && expr_non_constant_p)
7451 *non_constant_p = true;
7453 else if (non_constant_p)
7455 expr = (cp_parser_constant_expression
7456 (parser, /*allow_non_constant_p=*/true,
7457 &expr_non_constant_p));
7458 if (expr_non_constant_p)
7459 *non_constant_p = true;
7461 else
7462 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7463 cast_p);
7465 if (fold_expr_p)
7466 expr = instantiate_non_dependent_expr (expr);
7468 /* If we have an ellipsis, then this is an expression
7469 expansion. */
7470 if (allow_expansion_p
7471 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7473 /* Consume the `...'. */
7474 cp_lexer_consume_token (parser->lexer);
7476 /* Build the argument pack. */
7477 expr = make_pack_expansion (expr);
7480 /* Add it to the list. We add error_mark_node
7481 expressions to the list, so that we can still tell if
7482 the correct form for a parenthesized expression-list
7483 is found. That gives better errors. */
7484 vec_safe_push (expression_list, expr);
7486 if (expr == error_mark_node)
7487 goto skip_comma;
7490 /* After the first item, attribute lists look the same as
7491 expression lists. */
7492 is_attribute_list = non_attr;
7494 get_comma:;
7495 /* If the next token isn't a `,', then we are done. */
7496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7497 break;
7499 /* Otherwise, consume the `,' and keep going. */
7500 cp_lexer_consume_token (parser->lexer);
7503 if (close_paren_loc)
7504 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7506 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7508 int ending;
7510 skip_comma:;
7511 /* We try and resync to an unnested comma, as that will give the
7512 user better diagnostics. */
7513 ending = cp_parser_skip_to_closing_parenthesis (parser,
7514 /*recovering=*/true,
7515 /*or_comma=*/true,
7516 /*consume_paren=*/true);
7517 if (ending < 0)
7518 goto get_comma;
7519 if (!ending)
7521 parser->greater_than_is_operator_p
7522 = saved_greater_than_is_operator_p;
7523 return NULL;
7527 parser->greater_than_is_operator_p
7528 = saved_greater_than_is_operator_p;
7530 if (identifier)
7531 vec_safe_insert (expression_list, 0, identifier);
7533 return expression_list;
7536 /* Parse a pseudo-destructor-name.
7538 pseudo-destructor-name:
7539 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7540 :: [opt] nested-name-specifier template template-id :: ~ type-name
7541 :: [opt] nested-name-specifier [opt] ~ type-name
7543 If either of the first two productions is used, sets *SCOPE to the
7544 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7545 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7546 or ERROR_MARK_NODE if the parse fails. */
7548 static void
7549 cp_parser_pseudo_destructor_name (cp_parser* parser,
7550 tree object,
7551 tree* scope,
7552 tree* type)
7554 bool nested_name_specifier_p;
7556 /* Handle ~auto. */
7557 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7558 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7559 && !type_dependent_expression_p (object))
7561 if (cxx_dialect < cxx14)
7562 pedwarn (input_location, 0,
7563 "%<~auto%> only available with "
7564 "-std=c++14 or -std=gnu++14");
7565 cp_lexer_consume_token (parser->lexer);
7566 cp_lexer_consume_token (parser->lexer);
7567 *scope = NULL_TREE;
7568 *type = TREE_TYPE (object);
7569 return;
7572 /* Assume that things will not work out. */
7573 *type = error_mark_node;
7575 /* Look for the optional `::' operator. */
7576 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7577 /* Look for the optional nested-name-specifier. */
7578 nested_name_specifier_p
7579 = (cp_parser_nested_name_specifier_opt (parser,
7580 /*typename_keyword_p=*/false,
7581 /*check_dependency_p=*/true,
7582 /*type_p=*/false,
7583 /*is_declaration=*/false)
7584 != NULL_TREE);
7585 /* Now, if we saw a nested-name-specifier, we might be doing the
7586 second production. */
7587 if (nested_name_specifier_p
7588 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7590 /* Consume the `template' keyword. */
7591 cp_lexer_consume_token (parser->lexer);
7592 /* Parse the template-id. */
7593 cp_parser_template_id (parser,
7594 /*template_keyword_p=*/true,
7595 /*check_dependency_p=*/false,
7596 class_type,
7597 /*is_declaration=*/true);
7598 /* Look for the `::' token. */
7599 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7601 /* If the next token is not a `~', then there might be some
7602 additional qualification. */
7603 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7605 /* At this point, we're looking for "type-name :: ~". The type-name
7606 must not be a class-name, since this is a pseudo-destructor. So,
7607 it must be either an enum-name, or a typedef-name -- both of which
7608 are just identifiers. So, we peek ahead to check that the "::"
7609 and "~" tokens are present; if they are not, then we can avoid
7610 calling type_name. */
7611 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7612 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7613 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7615 cp_parser_error (parser, "non-scalar type");
7616 return;
7619 /* Look for the type-name. */
7620 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7621 if (*scope == error_mark_node)
7622 return;
7624 /* Look for the `::' token. */
7625 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7627 else
7628 *scope = NULL_TREE;
7630 /* Look for the `~'. */
7631 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7633 /* Once we see the ~, this has to be a pseudo-destructor. */
7634 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7635 cp_parser_commit_to_topmost_tentative_parse (parser);
7637 /* Look for the type-name again. We are not responsible for
7638 checking that it matches the first type-name. */
7639 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7642 /* Parse a unary-expression.
7644 unary-expression:
7645 postfix-expression
7646 ++ cast-expression
7647 -- cast-expression
7648 unary-operator cast-expression
7649 sizeof unary-expression
7650 sizeof ( type-id )
7651 alignof ( type-id ) [C++0x]
7652 new-expression
7653 delete-expression
7655 GNU Extensions:
7657 unary-expression:
7658 __extension__ cast-expression
7659 __alignof__ unary-expression
7660 __alignof__ ( type-id )
7661 alignof unary-expression [C++0x]
7662 __real__ cast-expression
7663 __imag__ cast-expression
7664 && identifier
7665 sizeof ( type-id ) { initializer-list , [opt] }
7666 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7667 __alignof__ ( type-id ) { initializer-list , [opt] }
7669 ADDRESS_P is true iff the unary-expression is appearing as the
7670 operand of the `&' operator. CAST_P is true if this expression is
7671 the target of a cast.
7673 Returns a representation of the expression. */
7675 static cp_expr
7676 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7677 bool address_p, bool cast_p, bool decltype_p)
7679 cp_token *token;
7680 enum tree_code unary_operator;
7682 /* Peek at the next token. */
7683 token = cp_lexer_peek_token (parser->lexer);
7684 /* Some keywords give away the kind of expression. */
7685 if (token->type == CPP_KEYWORD)
7687 enum rid keyword = token->keyword;
7689 switch (keyword)
7691 case RID_ALIGNOF:
7692 case RID_SIZEOF:
7694 tree operand, ret;
7695 enum tree_code op;
7696 location_t first_loc;
7698 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7699 /* Consume the token. */
7700 cp_lexer_consume_token (parser->lexer);
7701 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7702 /* Parse the operand. */
7703 operand = cp_parser_sizeof_operand (parser, keyword);
7705 if (TYPE_P (operand))
7706 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7707 else
7709 /* ISO C++ defines alignof only with types, not with
7710 expressions. So pedwarn if alignof is used with a non-
7711 type expression. However, __alignof__ is ok. */
7712 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7713 pedwarn (token->location, OPT_Wpedantic,
7714 "ISO C++ does not allow %<alignof%> "
7715 "with a non-type");
7717 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7719 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7720 SIZEOF_EXPR with the original operand. */
7721 if (op == SIZEOF_EXPR && ret != error_mark_node)
7723 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7725 if (!processing_template_decl && TYPE_P (operand))
7727 ret = build_min (SIZEOF_EXPR, size_type_node,
7728 build1 (NOP_EXPR, operand,
7729 error_mark_node));
7730 SIZEOF_EXPR_TYPE_P (ret) = 1;
7732 else
7733 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7734 TREE_SIDE_EFFECTS (ret) = 0;
7735 TREE_READONLY (ret) = 1;
7737 SET_EXPR_LOCATION (ret, first_loc);
7739 return ret;
7742 case RID_NEW:
7743 return cp_parser_new_expression (parser);
7745 case RID_DELETE:
7746 return cp_parser_delete_expression (parser);
7748 case RID_EXTENSION:
7750 /* The saved value of the PEDANTIC flag. */
7751 int saved_pedantic;
7752 tree expr;
7754 /* Save away the PEDANTIC flag. */
7755 cp_parser_extension_opt (parser, &saved_pedantic);
7756 /* Parse the cast-expression. */
7757 expr = cp_parser_simple_cast_expression (parser);
7758 /* Restore the PEDANTIC flag. */
7759 pedantic = saved_pedantic;
7761 return expr;
7764 case RID_REALPART:
7765 case RID_IMAGPART:
7767 tree expression;
7769 /* Consume the `__real__' or `__imag__' token. */
7770 cp_lexer_consume_token (parser->lexer);
7771 /* Parse the cast-expression. */
7772 expression = cp_parser_simple_cast_expression (parser);
7773 /* Create the complete representation. */
7774 return build_x_unary_op (token->location,
7775 (keyword == RID_REALPART
7776 ? REALPART_EXPR : IMAGPART_EXPR),
7777 expression,
7778 tf_warning_or_error);
7780 break;
7782 case RID_TRANSACTION_ATOMIC:
7783 case RID_TRANSACTION_RELAXED:
7784 return cp_parser_transaction_expression (parser, keyword);
7786 case RID_NOEXCEPT:
7788 tree expr;
7789 const char *saved_message;
7790 bool saved_integral_constant_expression_p;
7791 bool saved_non_integral_constant_expression_p;
7792 bool saved_greater_than_is_operator_p;
7794 cp_lexer_consume_token (parser->lexer);
7795 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7797 saved_message = parser->type_definition_forbidden_message;
7798 parser->type_definition_forbidden_message
7799 = G_("types may not be defined in %<noexcept%> expressions");
7801 saved_integral_constant_expression_p
7802 = parser->integral_constant_expression_p;
7803 saved_non_integral_constant_expression_p
7804 = parser->non_integral_constant_expression_p;
7805 parser->integral_constant_expression_p = false;
7807 saved_greater_than_is_operator_p
7808 = parser->greater_than_is_operator_p;
7809 parser->greater_than_is_operator_p = true;
7811 ++cp_unevaluated_operand;
7812 ++c_inhibit_evaluation_warnings;
7813 ++cp_noexcept_operand;
7814 expr = cp_parser_expression (parser);
7815 --cp_noexcept_operand;
7816 --c_inhibit_evaluation_warnings;
7817 --cp_unevaluated_operand;
7819 parser->greater_than_is_operator_p
7820 = saved_greater_than_is_operator_p;
7822 parser->integral_constant_expression_p
7823 = saved_integral_constant_expression_p;
7824 parser->non_integral_constant_expression_p
7825 = saved_non_integral_constant_expression_p;
7827 parser->type_definition_forbidden_message = saved_message;
7829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7830 return finish_noexcept_expr (expr, tf_warning_or_error);
7833 default:
7834 break;
7838 /* Look for the `:: new' and `:: delete', which also signal the
7839 beginning of a new-expression, or delete-expression,
7840 respectively. If the next token is `::', then it might be one of
7841 these. */
7842 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7844 enum rid keyword;
7846 /* See if the token after the `::' is one of the keywords in
7847 which we're interested. */
7848 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7849 /* If it's `new', we have a new-expression. */
7850 if (keyword == RID_NEW)
7851 return cp_parser_new_expression (parser);
7852 /* Similarly, for `delete'. */
7853 else if (keyword == RID_DELETE)
7854 return cp_parser_delete_expression (parser);
7857 /* Look for a unary operator. */
7858 unary_operator = cp_parser_unary_operator (token);
7859 /* The `++' and `--' operators can be handled similarly, even though
7860 they are not technically unary-operators in the grammar. */
7861 if (unary_operator == ERROR_MARK)
7863 if (token->type == CPP_PLUS_PLUS)
7864 unary_operator = PREINCREMENT_EXPR;
7865 else if (token->type == CPP_MINUS_MINUS)
7866 unary_operator = PREDECREMENT_EXPR;
7867 /* Handle the GNU address-of-label extension. */
7868 else if (cp_parser_allow_gnu_extensions_p (parser)
7869 && token->type == CPP_AND_AND)
7871 tree identifier;
7872 tree expression;
7873 location_t start_loc = token->location;
7875 /* Consume the '&&' token. */
7876 cp_lexer_consume_token (parser->lexer);
7877 /* Look for the identifier. */
7878 location_t finish_loc
7879 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7880 identifier = cp_parser_identifier (parser);
7881 /* Construct a location of the form:
7882 &&label
7883 ^~~~~~~
7884 with caret==start at the "&&", finish at the end of the label. */
7885 location_t combined_loc
7886 = make_location (start_loc, start_loc, finish_loc);
7887 /* Create an expression representing the address. */
7888 expression = finish_label_address_expr (identifier, combined_loc);
7889 if (cp_parser_non_integral_constant_expression (parser,
7890 NIC_ADDR_LABEL))
7891 expression = error_mark_node;
7892 return expression;
7895 if (unary_operator != ERROR_MARK)
7897 cp_expr cast_expression;
7898 cp_expr expression = error_mark_node;
7899 non_integral_constant non_constant_p = NIC_NONE;
7900 location_t loc = token->location;
7901 tsubst_flags_t complain = complain_flags (decltype_p);
7903 /* Consume the operator token. */
7904 token = cp_lexer_consume_token (parser->lexer);
7905 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7907 /* Parse the cast-expression. */
7908 cast_expression
7909 = cp_parser_cast_expression (parser,
7910 unary_operator == ADDR_EXPR,
7911 /*cast_p=*/false,
7912 /*decltype*/false,
7913 pidk);
7915 /* Make a location:
7916 OP_TOKEN CAST_EXPRESSION
7917 ^~~~~~~~~~~~~~~~~~~~~~~~~
7918 with start==caret at the operator token, and
7919 extending to the end of the cast_expression. */
7920 loc = make_location (loc, loc, cast_expression.get_finish ());
7922 /* Now, build an appropriate representation. */
7923 switch (unary_operator)
7925 case INDIRECT_REF:
7926 non_constant_p = NIC_STAR;
7927 expression = build_x_indirect_ref (loc, cast_expression,
7928 RO_UNARY_STAR,
7929 complain);
7930 /* TODO: build_x_indirect_ref does not always honor the
7931 location, so ensure it is set. */
7932 expression.set_location (loc);
7933 break;
7935 case ADDR_EXPR:
7936 non_constant_p = NIC_ADDR;
7937 /* Fall through. */
7938 case BIT_NOT_EXPR:
7939 expression = build_x_unary_op (loc, unary_operator,
7940 cast_expression,
7941 complain);
7942 /* TODO: build_x_unary_op does not always honor the location,
7943 so ensure it is set. */
7944 expression.set_location (loc);
7945 break;
7947 case PREINCREMENT_EXPR:
7948 case PREDECREMENT_EXPR:
7949 non_constant_p = unary_operator == PREINCREMENT_EXPR
7950 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7951 /* Fall through. */
7952 case NEGATE_EXPR:
7953 /* Immediately fold negation of a constant, unless the constant is 0
7954 (since -0 == 0) or it would overflow. */
7955 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7956 && CONSTANT_CLASS_P (cast_expression)
7957 && !integer_zerop (cast_expression)
7958 && !TREE_OVERFLOW (cast_expression))
7960 tree folded = fold_build1 (unary_operator,
7961 TREE_TYPE (cast_expression),
7962 cast_expression);
7963 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7965 expression = cp_expr (folded, loc);
7966 break;
7969 /* Fall through. */
7970 case UNARY_PLUS_EXPR:
7971 case TRUTH_NOT_EXPR:
7972 expression = finish_unary_op_expr (loc, unary_operator,
7973 cast_expression, complain);
7974 break;
7976 default:
7977 gcc_unreachable ();
7980 if (non_constant_p != NIC_NONE
7981 && cp_parser_non_integral_constant_expression (parser,
7982 non_constant_p))
7983 expression = error_mark_node;
7985 return expression;
7988 return cp_parser_postfix_expression (parser, address_p, cast_p,
7989 /*member_access_only_p=*/false,
7990 decltype_p,
7991 pidk);
7994 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7995 unary-operator, the corresponding tree code is returned. */
7997 static enum tree_code
7998 cp_parser_unary_operator (cp_token* token)
8000 switch (token->type)
8002 case CPP_MULT:
8003 return INDIRECT_REF;
8005 case CPP_AND:
8006 return ADDR_EXPR;
8008 case CPP_PLUS:
8009 return UNARY_PLUS_EXPR;
8011 case CPP_MINUS:
8012 return NEGATE_EXPR;
8014 case CPP_NOT:
8015 return TRUTH_NOT_EXPR;
8017 case CPP_COMPL:
8018 return BIT_NOT_EXPR;
8020 default:
8021 return ERROR_MARK;
8025 /* Parse a new-expression.
8027 new-expression:
8028 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8029 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8031 Returns a representation of the expression. */
8033 static tree
8034 cp_parser_new_expression (cp_parser* parser)
8036 bool global_scope_p;
8037 vec<tree, va_gc> *placement;
8038 tree type;
8039 vec<tree, va_gc> *initializer;
8040 tree nelts = NULL_TREE;
8041 tree ret;
8043 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8045 /* Look for the optional `::' operator. */
8046 global_scope_p
8047 = (cp_parser_global_scope_opt (parser,
8048 /*current_scope_valid_p=*/false)
8049 != NULL_TREE);
8050 /* Look for the `new' operator. */
8051 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8052 /* There's no easy way to tell a new-placement from the
8053 `( type-id )' construct. */
8054 cp_parser_parse_tentatively (parser);
8055 /* Look for a new-placement. */
8056 placement = cp_parser_new_placement (parser);
8057 /* If that didn't work out, there's no new-placement. */
8058 if (!cp_parser_parse_definitely (parser))
8060 if (placement != NULL)
8061 release_tree_vector (placement);
8062 placement = NULL;
8065 /* If the next token is a `(', then we have a parenthesized
8066 type-id. */
8067 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8069 cp_token *token;
8070 const char *saved_message = parser->type_definition_forbidden_message;
8072 /* Consume the `('. */
8073 cp_lexer_consume_token (parser->lexer);
8075 /* Parse the type-id. */
8076 parser->type_definition_forbidden_message
8077 = G_("types may not be defined in a new-expression");
8079 type_id_in_expr_sentinel s (parser);
8080 type = cp_parser_type_id (parser);
8082 parser->type_definition_forbidden_message = saved_message;
8084 /* Look for the closing `)'. */
8085 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8086 token = cp_lexer_peek_token (parser->lexer);
8087 /* There should not be a direct-new-declarator in this production,
8088 but GCC used to allowed this, so we check and emit a sensible error
8089 message for this case. */
8090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8092 error_at (token->location,
8093 "array bound forbidden after parenthesized type-id");
8094 inform (token->location,
8095 "try removing the parentheses around the type-id");
8096 cp_parser_direct_new_declarator (parser);
8099 /* Otherwise, there must be a new-type-id. */
8100 else
8101 type = cp_parser_new_type_id (parser, &nelts);
8103 /* If the next token is a `(' or '{', then we have a new-initializer. */
8104 cp_token *token = cp_lexer_peek_token (parser->lexer);
8105 if (token->type == CPP_OPEN_PAREN
8106 || token->type == CPP_OPEN_BRACE)
8107 initializer = cp_parser_new_initializer (parser);
8108 else
8109 initializer = NULL;
8111 /* A new-expression may not appear in an integral constant
8112 expression. */
8113 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8114 ret = error_mark_node;
8115 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8116 of a new-type-id or type-id of a new-expression, the new-expression shall
8117 contain a new-initializer of the form ( assignment-expression )".
8118 Additionally, consistently with the spirit of DR 1467, we want to accept
8119 'new auto { 2 }' too. */
8120 else if (type_uses_auto (type)
8121 && (vec_safe_length (initializer) != 1
8122 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8123 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8125 error_at (token->location,
8126 "initialization of new-expression for type %<auto%> "
8127 "requires exactly one element");
8128 ret = error_mark_node;
8130 else
8132 /* Construct a location e.g.:
8133 ptr = new int[100]
8134 ^~~~~~~~~~~~
8135 with caret == start at the start of the "new" token, and the end
8136 at the end of the final token we consumed. */
8137 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8138 location_t end_loc = get_finish (end_tok->location);
8139 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8141 /* Create a representation of the new-expression. */
8142 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8143 tf_warning_or_error);
8144 protected_set_expr_location (ret, combined_loc);
8147 if (placement != NULL)
8148 release_tree_vector (placement);
8149 if (initializer != NULL)
8150 release_tree_vector (initializer);
8152 return ret;
8155 /* Parse a new-placement.
8157 new-placement:
8158 ( expression-list )
8160 Returns the same representation as for an expression-list. */
8162 static vec<tree, va_gc> *
8163 cp_parser_new_placement (cp_parser* parser)
8165 vec<tree, va_gc> *expression_list;
8167 /* Parse the expression-list. */
8168 expression_list = (cp_parser_parenthesized_expression_list
8169 (parser, non_attr, /*cast_p=*/false,
8170 /*allow_expansion_p=*/true,
8171 /*non_constant_p=*/NULL));
8173 if (expression_list && expression_list->is_empty ())
8174 error ("expected expression-list or type-id");
8176 return expression_list;
8179 /* Parse a new-type-id.
8181 new-type-id:
8182 type-specifier-seq new-declarator [opt]
8184 Returns the TYPE allocated. If the new-type-id indicates an array
8185 type, *NELTS is set to the number of elements in the last array
8186 bound; the TYPE will not include the last array bound. */
8188 static tree
8189 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8191 cp_decl_specifier_seq type_specifier_seq;
8192 cp_declarator *new_declarator;
8193 cp_declarator *declarator;
8194 cp_declarator *outer_declarator;
8195 const char *saved_message;
8197 /* The type-specifier sequence must not contain type definitions.
8198 (It cannot contain declarations of new types either, but if they
8199 are not definitions we will catch that because they are not
8200 complete.) */
8201 saved_message = parser->type_definition_forbidden_message;
8202 parser->type_definition_forbidden_message
8203 = G_("types may not be defined in a new-type-id");
8204 /* Parse the type-specifier-seq. */
8205 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8206 /*is_trailing_return=*/false,
8207 &type_specifier_seq);
8208 /* Restore the old message. */
8209 parser->type_definition_forbidden_message = saved_message;
8211 if (type_specifier_seq.type == error_mark_node)
8212 return error_mark_node;
8214 /* Parse the new-declarator. */
8215 new_declarator = cp_parser_new_declarator_opt (parser);
8217 /* Determine the number of elements in the last array dimension, if
8218 any. */
8219 *nelts = NULL_TREE;
8220 /* Skip down to the last array dimension. */
8221 declarator = new_declarator;
8222 outer_declarator = NULL;
8223 while (declarator && (declarator->kind == cdk_pointer
8224 || declarator->kind == cdk_ptrmem))
8226 outer_declarator = declarator;
8227 declarator = declarator->declarator;
8229 while (declarator
8230 && declarator->kind == cdk_array
8231 && declarator->declarator
8232 && declarator->declarator->kind == cdk_array)
8234 outer_declarator = declarator;
8235 declarator = declarator->declarator;
8238 if (declarator && declarator->kind == cdk_array)
8240 *nelts = declarator->u.array.bounds;
8241 if (*nelts == error_mark_node)
8242 *nelts = integer_one_node;
8244 if (outer_declarator)
8245 outer_declarator->declarator = declarator->declarator;
8246 else
8247 new_declarator = NULL;
8250 return groktypename (&type_specifier_seq, new_declarator, false);
8253 /* Parse an (optional) new-declarator.
8255 new-declarator:
8256 ptr-operator new-declarator [opt]
8257 direct-new-declarator
8259 Returns the declarator. */
8261 static cp_declarator *
8262 cp_parser_new_declarator_opt (cp_parser* parser)
8264 enum tree_code code;
8265 tree type, std_attributes = NULL_TREE;
8266 cp_cv_quals cv_quals;
8268 /* We don't know if there's a ptr-operator next, or not. */
8269 cp_parser_parse_tentatively (parser);
8270 /* Look for a ptr-operator. */
8271 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8272 /* If that worked, look for more new-declarators. */
8273 if (cp_parser_parse_definitely (parser))
8275 cp_declarator *declarator;
8277 /* Parse another optional declarator. */
8278 declarator = cp_parser_new_declarator_opt (parser);
8280 declarator = cp_parser_make_indirect_declarator
8281 (code, type, cv_quals, declarator, std_attributes);
8283 return declarator;
8286 /* If the next token is a `[', there is a direct-new-declarator. */
8287 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8288 return cp_parser_direct_new_declarator (parser);
8290 return NULL;
8293 /* Parse a direct-new-declarator.
8295 direct-new-declarator:
8296 [ expression ]
8297 direct-new-declarator [constant-expression]
8301 static cp_declarator *
8302 cp_parser_direct_new_declarator (cp_parser* parser)
8304 cp_declarator *declarator = NULL;
8306 while (true)
8308 tree expression;
8309 cp_token *token;
8311 /* Look for the opening `['. */
8312 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8314 token = cp_lexer_peek_token (parser->lexer);
8315 expression = cp_parser_expression (parser);
8316 /* The standard requires that the expression have integral
8317 type. DR 74 adds enumeration types. We believe that the
8318 real intent is that these expressions be handled like the
8319 expression in a `switch' condition, which also allows
8320 classes with a single conversion to integral or
8321 enumeration type. */
8322 if (!processing_template_decl)
8324 expression
8325 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8326 expression,
8327 /*complain=*/true);
8328 if (!expression)
8330 error_at (token->location,
8331 "expression in new-declarator must have integral "
8332 "or enumeration type");
8333 expression = error_mark_node;
8337 /* Look for the closing `]'. */
8338 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8340 /* Add this bound to the declarator. */
8341 declarator = make_array_declarator (declarator, expression);
8343 /* If the next token is not a `[', then there are no more
8344 bounds. */
8345 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8346 break;
8349 return declarator;
8352 /* Parse a new-initializer.
8354 new-initializer:
8355 ( expression-list [opt] )
8356 braced-init-list
8358 Returns a representation of the expression-list. */
8360 static vec<tree, va_gc> *
8361 cp_parser_new_initializer (cp_parser* parser)
8363 vec<tree, va_gc> *expression_list;
8365 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8367 tree t;
8368 bool expr_non_constant_p;
8369 cp_lexer_set_source_position (parser->lexer);
8370 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8371 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8372 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8373 expression_list = make_tree_vector_single (t);
8375 else
8376 expression_list = (cp_parser_parenthesized_expression_list
8377 (parser, non_attr, /*cast_p=*/false,
8378 /*allow_expansion_p=*/true,
8379 /*non_constant_p=*/NULL));
8381 return expression_list;
8384 /* Parse a delete-expression.
8386 delete-expression:
8387 :: [opt] delete cast-expression
8388 :: [opt] delete [ ] cast-expression
8390 Returns a representation of the expression. */
8392 static tree
8393 cp_parser_delete_expression (cp_parser* parser)
8395 bool global_scope_p;
8396 bool array_p;
8397 tree expression;
8399 /* Look for the optional `::' operator. */
8400 global_scope_p
8401 = (cp_parser_global_scope_opt (parser,
8402 /*current_scope_valid_p=*/false)
8403 != NULL_TREE);
8404 /* Look for the `delete' keyword. */
8405 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8406 /* See if the array syntax is in use. */
8407 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8409 /* Consume the `[' token. */
8410 cp_lexer_consume_token (parser->lexer);
8411 /* Look for the `]' token. */
8412 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8413 /* Remember that this is the `[]' construct. */
8414 array_p = true;
8416 else
8417 array_p = false;
8419 /* Parse the cast-expression. */
8420 expression = cp_parser_simple_cast_expression (parser);
8422 /* A delete-expression may not appear in an integral constant
8423 expression. */
8424 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8425 return error_mark_node;
8427 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8428 tf_warning_or_error);
8431 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8432 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8433 0 otherwise. */
8435 static int
8436 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8438 cp_token *token = cp_lexer_peek_token (parser->lexer);
8439 switch (token->type)
8441 case CPP_COMMA:
8442 case CPP_SEMICOLON:
8443 case CPP_QUERY:
8444 case CPP_COLON:
8445 case CPP_CLOSE_SQUARE:
8446 case CPP_CLOSE_PAREN:
8447 case CPP_CLOSE_BRACE:
8448 case CPP_OPEN_BRACE:
8449 case CPP_DOT:
8450 case CPP_DOT_STAR:
8451 case CPP_DEREF:
8452 case CPP_DEREF_STAR:
8453 case CPP_DIV:
8454 case CPP_MOD:
8455 case CPP_LSHIFT:
8456 case CPP_RSHIFT:
8457 case CPP_LESS:
8458 case CPP_GREATER:
8459 case CPP_LESS_EQ:
8460 case CPP_GREATER_EQ:
8461 case CPP_EQ_EQ:
8462 case CPP_NOT_EQ:
8463 case CPP_EQ:
8464 case CPP_MULT_EQ:
8465 case CPP_DIV_EQ:
8466 case CPP_MOD_EQ:
8467 case CPP_PLUS_EQ:
8468 case CPP_MINUS_EQ:
8469 case CPP_RSHIFT_EQ:
8470 case CPP_LSHIFT_EQ:
8471 case CPP_AND_EQ:
8472 case CPP_XOR_EQ:
8473 case CPP_OR_EQ:
8474 case CPP_XOR:
8475 case CPP_OR:
8476 case CPP_OR_OR:
8477 case CPP_EOF:
8478 case CPP_ELLIPSIS:
8479 return 0;
8481 case CPP_OPEN_PAREN:
8482 /* In ((type ()) () the last () isn't a valid cast-expression,
8483 so the whole must be parsed as postfix-expression. */
8484 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8485 != CPP_CLOSE_PAREN;
8487 case CPP_OPEN_SQUARE:
8488 /* '[' may start a primary-expression in obj-c++ and in C++11,
8489 as a lambda-expression, eg, '(void)[]{}'. */
8490 if (cxx_dialect >= cxx11)
8491 return -1;
8492 return c_dialect_objc ();
8494 case CPP_PLUS_PLUS:
8495 case CPP_MINUS_MINUS:
8496 /* '++' and '--' may or may not start a cast-expression:
8498 struct T { void operator++(int); };
8499 void f() { (T())++; }
8503 int a;
8504 (int)++a; */
8505 return -1;
8507 default:
8508 return 1;
8512 /* Parse a cast-expression.
8514 cast-expression:
8515 unary-expression
8516 ( type-id ) cast-expression
8518 ADDRESS_P is true iff the unary-expression is appearing as the
8519 operand of the `&' operator. CAST_P is true if this expression is
8520 the target of a cast.
8522 Returns a representation of the expression. */
8524 static cp_expr
8525 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8526 bool decltype_p, cp_id_kind * pidk)
8528 /* If it's a `(', then we might be looking at a cast. */
8529 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8531 tree type = NULL_TREE;
8532 cp_expr expr (NULL_TREE);
8533 int cast_expression = 0;
8534 const char *saved_message;
8536 /* There's no way to know yet whether or not this is a cast.
8537 For example, `(int (3))' is a unary-expression, while `(int)
8538 3' is a cast. So, we resort to parsing tentatively. */
8539 cp_parser_parse_tentatively (parser);
8540 /* Types may not be defined in a cast. */
8541 saved_message = parser->type_definition_forbidden_message;
8542 parser->type_definition_forbidden_message
8543 = G_("types may not be defined in casts");
8544 /* Consume the `('. */
8545 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8546 location_t open_paren_loc = open_paren->location;
8548 /* A very tricky bit is that `(struct S) { 3 }' is a
8549 compound-literal (which we permit in C++ as an extension).
8550 But, that construct is not a cast-expression -- it is a
8551 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8552 is legal; if the compound-literal were a cast-expression,
8553 you'd need an extra set of parentheses.) But, if we parse
8554 the type-id, and it happens to be a class-specifier, then we
8555 will commit to the parse at that point, because we cannot
8556 undo the action that is done when creating a new class. So,
8557 then we cannot back up and do a postfix-expression.
8559 Another tricky case is the following (c++/29234):
8561 struct S { void operator () (); };
8563 void foo ()
8565 ( S()() );
8568 As a type-id we parse the parenthesized S()() as a function
8569 returning a function, groktypename complains and we cannot
8570 back up in this case either.
8572 Therefore, we scan ahead to the closing `)', and check to see
8573 if the tokens after the `)' can start a cast-expression. Otherwise
8574 we are dealing with an unary-expression, a postfix-expression
8575 or something else.
8577 Yet another tricky case, in C++11, is the following (c++/54891):
8579 (void)[]{};
8581 The issue is that usually, besides the case of lambda-expressions,
8582 the parenthesized type-id cannot be followed by '[', and, eg, we
8583 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8584 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8585 we don't commit, we try a cast-expression, then an unary-expression.
8587 Save tokens so that we can put them back. */
8588 cp_lexer_save_tokens (parser->lexer);
8590 /* We may be looking at a cast-expression. */
8591 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8592 /*consume_paren=*/true))
8593 cast_expression
8594 = cp_parser_tokens_start_cast_expression (parser);
8596 /* Roll back the tokens we skipped. */
8597 cp_lexer_rollback_tokens (parser->lexer);
8598 /* If we aren't looking at a cast-expression, simulate an error so
8599 that the call to cp_parser_error_occurred below returns true. */
8600 if (!cast_expression)
8601 cp_parser_simulate_error (parser);
8602 else
8604 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8605 parser->in_type_id_in_expr_p = true;
8606 /* Look for the type-id. */
8607 type = cp_parser_type_id (parser);
8608 /* Look for the closing `)'. */
8609 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8610 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8613 /* Restore the saved message. */
8614 parser->type_definition_forbidden_message = saved_message;
8616 /* At this point this can only be either a cast or a
8617 parenthesized ctor such as `(T ())' that looks like a cast to
8618 function returning T. */
8619 if (!cp_parser_error_occurred (parser))
8621 /* Only commit if the cast-expression doesn't start with
8622 '++', '--', or '[' in C++11. */
8623 if (cast_expression > 0)
8624 cp_parser_commit_to_topmost_tentative_parse (parser);
8626 expr = cp_parser_cast_expression (parser,
8627 /*address_p=*/false,
8628 /*cast_p=*/true,
8629 /*decltype_p=*/false,
8630 pidk);
8632 if (cp_parser_parse_definitely (parser))
8634 /* Warn about old-style casts, if so requested. */
8635 if (warn_old_style_cast
8636 && !in_system_header_at (input_location)
8637 && !VOID_TYPE_P (type)
8638 && current_lang_name != lang_name_c)
8639 warning (OPT_Wold_style_cast, "use of old-style cast");
8641 /* Only type conversions to integral or enumeration types
8642 can be used in constant-expressions. */
8643 if (!cast_valid_in_integral_constant_expression_p (type)
8644 && cp_parser_non_integral_constant_expression (parser,
8645 NIC_CAST))
8646 return error_mark_node;
8648 /* Perform the cast. */
8649 /* Make a location:
8650 (TYPE) EXPR
8651 ^~~~~~~~~~~
8652 with start==caret at the open paren, extending to the
8653 end of "expr". */
8654 location_t cast_loc = make_location (open_paren_loc,
8655 open_paren_loc,
8656 expr.get_finish ());
8657 expr = build_c_cast (cast_loc, type, expr);
8658 return expr;
8661 else
8662 cp_parser_abort_tentative_parse (parser);
8665 /* If we get here, then it's not a cast, so it must be a
8666 unary-expression. */
8667 return cp_parser_unary_expression (parser, pidk, address_p,
8668 cast_p, decltype_p);
8671 /* Parse a binary expression of the general form:
8673 pm-expression:
8674 cast-expression
8675 pm-expression .* cast-expression
8676 pm-expression ->* cast-expression
8678 multiplicative-expression:
8679 pm-expression
8680 multiplicative-expression * pm-expression
8681 multiplicative-expression / pm-expression
8682 multiplicative-expression % pm-expression
8684 additive-expression:
8685 multiplicative-expression
8686 additive-expression + multiplicative-expression
8687 additive-expression - multiplicative-expression
8689 shift-expression:
8690 additive-expression
8691 shift-expression << additive-expression
8692 shift-expression >> additive-expression
8694 relational-expression:
8695 shift-expression
8696 relational-expression < shift-expression
8697 relational-expression > shift-expression
8698 relational-expression <= shift-expression
8699 relational-expression >= shift-expression
8701 GNU Extension:
8703 relational-expression:
8704 relational-expression <? shift-expression
8705 relational-expression >? shift-expression
8707 equality-expression:
8708 relational-expression
8709 equality-expression == relational-expression
8710 equality-expression != relational-expression
8712 and-expression:
8713 equality-expression
8714 and-expression & equality-expression
8716 exclusive-or-expression:
8717 and-expression
8718 exclusive-or-expression ^ and-expression
8720 inclusive-or-expression:
8721 exclusive-or-expression
8722 inclusive-or-expression | exclusive-or-expression
8724 logical-and-expression:
8725 inclusive-or-expression
8726 logical-and-expression && inclusive-or-expression
8728 logical-or-expression:
8729 logical-and-expression
8730 logical-or-expression || logical-and-expression
8732 All these are implemented with a single function like:
8734 binary-expression:
8735 simple-cast-expression
8736 binary-expression <token> binary-expression
8738 CAST_P is true if this expression is the target of a cast.
8740 The binops_by_token map is used to get the tree codes for each <token> type.
8741 binary-expressions are associated according to a precedence table. */
8743 #define TOKEN_PRECEDENCE(token) \
8744 (((token->type == CPP_GREATER \
8745 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8746 && !parser->greater_than_is_operator_p) \
8747 ? PREC_NOT_OPERATOR \
8748 : binops_by_token[token->type].prec)
8750 static cp_expr
8751 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8752 bool no_toplevel_fold_p,
8753 bool decltype_p,
8754 enum cp_parser_prec prec,
8755 cp_id_kind * pidk)
8757 cp_parser_expression_stack stack;
8758 cp_parser_expression_stack_entry *sp = &stack[0];
8759 cp_parser_expression_stack_entry current;
8760 cp_expr rhs;
8761 cp_token *token;
8762 enum tree_code rhs_type;
8763 enum cp_parser_prec new_prec, lookahead_prec;
8764 tree overload;
8766 /* Parse the first expression. */
8767 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8768 ? TRUTH_NOT_EXPR : ERROR_MARK);
8769 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8770 cast_p, decltype_p, pidk);
8771 current.prec = prec;
8773 if (cp_parser_error_occurred (parser))
8774 return error_mark_node;
8776 for (;;)
8778 /* Get an operator token. */
8779 token = cp_lexer_peek_token (parser->lexer);
8781 if (warn_cxx11_compat
8782 && token->type == CPP_RSHIFT
8783 && !parser->greater_than_is_operator_p)
8785 if (warning_at (token->location, OPT_Wc__11_compat,
8786 "%<>>%> operator is treated"
8787 " as two right angle brackets in C++11"))
8788 inform (token->location,
8789 "suggest parentheses around %<>>%> expression");
8792 new_prec = TOKEN_PRECEDENCE (token);
8793 if (new_prec != PREC_NOT_OPERATOR
8794 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8795 /* This is a fold-expression; handle it later. */
8796 new_prec = PREC_NOT_OPERATOR;
8798 /* Popping an entry off the stack means we completed a subexpression:
8799 - either we found a token which is not an operator (`>' where it is not
8800 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8801 will happen repeatedly;
8802 - or, we found an operator which has lower priority. This is the case
8803 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8804 parsing `3 * 4'. */
8805 if (new_prec <= current.prec)
8807 if (sp == stack)
8808 break;
8809 else
8810 goto pop;
8813 get_rhs:
8814 current.tree_type = binops_by_token[token->type].tree_type;
8815 current.loc = token->location;
8817 /* We used the operator token. */
8818 cp_lexer_consume_token (parser->lexer);
8820 /* For "false && x" or "true || x", x will never be executed;
8821 disable warnings while evaluating it. */
8822 if (current.tree_type == TRUTH_ANDIF_EXPR)
8823 c_inhibit_evaluation_warnings +=
8824 cp_fully_fold (current.lhs) == truthvalue_false_node;
8825 else if (current.tree_type == TRUTH_ORIF_EXPR)
8826 c_inhibit_evaluation_warnings +=
8827 cp_fully_fold (current.lhs) == truthvalue_true_node;
8829 /* Extract another operand. It may be the RHS of this expression
8830 or the LHS of a new, higher priority expression. */
8831 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8832 ? TRUTH_NOT_EXPR : ERROR_MARK);
8833 rhs = cp_parser_simple_cast_expression (parser);
8835 /* Get another operator token. Look up its precedence to avoid
8836 building a useless (immediately popped) stack entry for common
8837 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8838 token = cp_lexer_peek_token (parser->lexer);
8839 lookahead_prec = TOKEN_PRECEDENCE (token);
8840 if (lookahead_prec != PREC_NOT_OPERATOR
8841 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8842 lookahead_prec = PREC_NOT_OPERATOR;
8843 if (lookahead_prec > new_prec)
8845 /* ... and prepare to parse the RHS of the new, higher priority
8846 expression. Since precedence levels on the stack are
8847 monotonically increasing, we do not have to care about
8848 stack overflows. */
8849 *sp = current;
8850 ++sp;
8851 current.lhs = rhs;
8852 current.lhs_type = rhs_type;
8853 current.prec = new_prec;
8854 new_prec = lookahead_prec;
8855 goto get_rhs;
8857 pop:
8858 lookahead_prec = new_prec;
8859 /* If the stack is not empty, we have parsed into LHS the right side
8860 (`4' in the example above) of an expression we had suspended.
8861 We can use the information on the stack to recover the LHS (`3')
8862 from the stack together with the tree code (`MULT_EXPR'), and
8863 the precedence of the higher level subexpression
8864 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8865 which will be used to actually build the additive expression. */
8866 rhs = current.lhs;
8867 rhs_type = current.lhs_type;
8868 --sp;
8869 current = *sp;
8872 /* Undo the disabling of warnings done above. */
8873 if (current.tree_type == TRUTH_ANDIF_EXPR)
8874 c_inhibit_evaluation_warnings -=
8875 cp_fully_fold (current.lhs) == truthvalue_false_node;
8876 else if (current.tree_type == TRUTH_ORIF_EXPR)
8877 c_inhibit_evaluation_warnings -=
8878 cp_fully_fold (current.lhs) == truthvalue_true_node;
8880 if (warn_logical_not_paren
8881 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8882 && current.lhs_type == TRUTH_NOT_EXPR
8883 /* Avoid warning for !!x == y. */
8884 && (TREE_CODE (current.lhs) != NE_EXPR
8885 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8886 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8887 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8888 /* Avoid warning for !b == y where b is boolean. */
8889 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8890 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8891 != BOOLEAN_TYPE))))
8892 /* Avoid warning for !!b == y where b is boolean. */
8893 && (!DECL_P (current.lhs)
8894 || TREE_TYPE (current.lhs) == NULL_TREE
8895 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8896 warn_logical_not_parentheses (current.loc, current.tree_type,
8897 maybe_constant_value (rhs));
8899 overload = NULL;
8901 location_t combined_loc = make_location (current.loc,
8902 current.lhs.get_start (),
8903 rhs.get_finish ());
8905 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8906 ERROR_MARK for everything that is not a binary expression.
8907 This makes warn_about_parentheses miss some warnings that
8908 involve unary operators. For unary expressions we should
8909 pass the correct tree_code unless the unary expression was
8910 surrounded by parentheses.
8912 if (no_toplevel_fold_p
8913 && lookahead_prec <= current.prec
8914 && sp == stack)
8915 current.lhs = build2_loc (combined_loc,
8916 current.tree_type,
8917 TREE_CODE_CLASS (current.tree_type)
8918 == tcc_comparison
8919 ? boolean_type_node : TREE_TYPE (current.lhs),
8920 current.lhs, rhs);
8921 else
8923 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8924 current.lhs, current.lhs_type,
8925 rhs, rhs_type, &overload,
8926 complain_flags (decltype_p));
8927 /* TODO: build_x_binary_op doesn't always honor the location. */
8928 current.lhs.set_location (combined_loc);
8930 current.lhs_type = current.tree_type;
8932 /* If the binary operator required the use of an overloaded operator,
8933 then this expression cannot be an integral constant-expression.
8934 An overloaded operator can be used even if both operands are
8935 otherwise permissible in an integral constant-expression if at
8936 least one of the operands is of enumeration type. */
8938 if (overload
8939 && cp_parser_non_integral_constant_expression (parser,
8940 NIC_OVERLOADED))
8941 return error_mark_node;
8944 return current.lhs;
8947 static cp_expr
8948 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8949 bool no_toplevel_fold_p,
8950 enum cp_parser_prec prec,
8951 cp_id_kind * pidk)
8953 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8954 /*decltype*/false, prec, pidk);
8957 /* Parse the `? expression : assignment-expression' part of a
8958 conditional-expression. The LOGICAL_OR_EXPR is the
8959 logical-or-expression that started the conditional-expression.
8960 Returns a representation of the entire conditional-expression.
8962 This routine is used by cp_parser_assignment_expression.
8964 ? expression : assignment-expression
8966 GNU Extensions:
8968 ? : assignment-expression */
8970 static tree
8971 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8973 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8974 cp_expr assignment_expr;
8975 struct cp_token *token;
8976 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8978 /* Consume the `?' token. */
8979 cp_lexer_consume_token (parser->lexer);
8980 token = cp_lexer_peek_token (parser->lexer);
8981 if (cp_parser_allow_gnu_extensions_p (parser)
8982 && token->type == CPP_COLON)
8984 pedwarn (token->location, OPT_Wpedantic,
8985 "ISO C++ does not allow ?: with omitted middle operand");
8986 /* Implicit true clause. */
8987 expr = NULL_TREE;
8988 c_inhibit_evaluation_warnings +=
8989 folded_logical_or_expr == truthvalue_true_node;
8990 warn_for_omitted_condop (token->location, logical_or_expr);
8992 else
8994 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8995 parser->colon_corrects_to_scope_p = false;
8996 /* Parse the expression. */
8997 c_inhibit_evaluation_warnings +=
8998 folded_logical_or_expr == truthvalue_false_node;
8999 expr = cp_parser_expression (parser);
9000 c_inhibit_evaluation_warnings +=
9001 ((folded_logical_or_expr == truthvalue_true_node)
9002 - (folded_logical_or_expr == truthvalue_false_node));
9003 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9006 /* The next token should be a `:'. */
9007 cp_parser_require (parser, CPP_COLON, RT_COLON);
9008 /* Parse the assignment-expression. */
9009 assignment_expr = cp_parser_assignment_expression (parser);
9010 c_inhibit_evaluation_warnings -=
9011 folded_logical_or_expr == truthvalue_true_node;
9013 /* Make a location:
9014 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9015 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9016 with the caret at the "?", ranging from the start of
9017 the logical_or_expr to the end of the assignment_expr. */
9018 loc = make_location (loc,
9019 logical_or_expr.get_start (),
9020 assignment_expr.get_finish ());
9022 /* Build the conditional-expression. */
9023 return build_x_conditional_expr (loc, logical_or_expr,
9024 expr,
9025 assignment_expr,
9026 tf_warning_or_error);
9029 /* Parse an assignment-expression.
9031 assignment-expression:
9032 conditional-expression
9033 logical-or-expression assignment-operator assignment_expression
9034 throw-expression
9036 CAST_P is true if this expression is the target of a cast.
9037 DECLTYPE_P is true if this expression is the operand of decltype.
9039 Returns a representation for the expression. */
9041 static cp_expr
9042 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9043 bool cast_p, bool decltype_p)
9045 cp_expr expr;
9047 /* If the next token is the `throw' keyword, then we're looking at
9048 a throw-expression. */
9049 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9050 expr = cp_parser_throw_expression (parser);
9051 /* Otherwise, it must be that we are looking at a
9052 logical-or-expression. */
9053 else
9055 /* Parse the binary expressions (logical-or-expression). */
9056 expr = cp_parser_binary_expression (parser, cast_p, false,
9057 decltype_p,
9058 PREC_NOT_OPERATOR, pidk);
9059 /* If the next token is a `?' then we're actually looking at a
9060 conditional-expression. */
9061 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9062 return cp_parser_question_colon_clause (parser, expr);
9063 else
9065 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9067 /* If it's an assignment-operator, we're using the second
9068 production. */
9069 enum tree_code assignment_operator
9070 = cp_parser_assignment_operator_opt (parser);
9071 if (assignment_operator != ERROR_MARK)
9073 bool non_constant_p;
9075 /* Parse the right-hand side of the assignment. */
9076 cp_expr rhs = cp_parser_initializer_clause (parser,
9077 &non_constant_p);
9079 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9082 /* An assignment may not appear in a
9083 constant-expression. */
9084 if (cp_parser_non_integral_constant_expression (parser,
9085 NIC_ASSIGNMENT))
9086 return error_mark_node;
9087 /* Build the assignment expression. Its default
9088 location:
9089 LHS = RHS
9090 ~~~~^~~~~
9091 is the location of the '=' token as the
9092 caret, ranging from the start of the lhs to the
9093 end of the rhs. */
9094 loc = make_location (loc,
9095 expr.get_start (),
9096 rhs.get_finish ());
9097 expr = build_x_modify_expr (loc, expr,
9098 assignment_operator,
9099 rhs,
9100 complain_flags (decltype_p));
9101 /* TODO: build_x_modify_expr doesn't honor the location,
9102 so we must set it here. */
9103 expr.set_location (loc);
9108 return expr;
9111 /* Parse an (optional) assignment-operator.
9113 assignment-operator: one of
9114 = *= /= %= += -= >>= <<= &= ^= |=
9116 GNU Extension:
9118 assignment-operator: one of
9119 <?= >?=
9121 If the next token is an assignment operator, the corresponding tree
9122 code is returned, and the token is consumed. For example, for
9123 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9124 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9125 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9126 operator, ERROR_MARK is returned. */
9128 static enum tree_code
9129 cp_parser_assignment_operator_opt (cp_parser* parser)
9131 enum tree_code op;
9132 cp_token *token;
9134 /* Peek at the next token. */
9135 token = cp_lexer_peek_token (parser->lexer);
9137 switch (token->type)
9139 case CPP_EQ:
9140 op = NOP_EXPR;
9141 break;
9143 case CPP_MULT_EQ:
9144 op = MULT_EXPR;
9145 break;
9147 case CPP_DIV_EQ:
9148 op = TRUNC_DIV_EXPR;
9149 break;
9151 case CPP_MOD_EQ:
9152 op = TRUNC_MOD_EXPR;
9153 break;
9155 case CPP_PLUS_EQ:
9156 op = PLUS_EXPR;
9157 break;
9159 case CPP_MINUS_EQ:
9160 op = MINUS_EXPR;
9161 break;
9163 case CPP_RSHIFT_EQ:
9164 op = RSHIFT_EXPR;
9165 break;
9167 case CPP_LSHIFT_EQ:
9168 op = LSHIFT_EXPR;
9169 break;
9171 case CPP_AND_EQ:
9172 op = BIT_AND_EXPR;
9173 break;
9175 case CPP_XOR_EQ:
9176 op = BIT_XOR_EXPR;
9177 break;
9179 case CPP_OR_EQ:
9180 op = BIT_IOR_EXPR;
9181 break;
9183 default:
9184 /* Nothing else is an assignment operator. */
9185 op = ERROR_MARK;
9188 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9189 if (op != ERROR_MARK
9190 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9191 op = ERROR_MARK;
9193 /* If it was an assignment operator, consume it. */
9194 if (op != ERROR_MARK)
9195 cp_lexer_consume_token (parser->lexer);
9197 return op;
9200 /* Parse an expression.
9202 expression:
9203 assignment-expression
9204 expression , assignment-expression
9206 CAST_P is true if this expression is the target of a cast.
9207 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9208 except possibly parenthesized or on the RHS of a comma (N3276).
9210 Returns a representation of the expression. */
9212 static cp_expr
9213 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9214 bool cast_p, bool decltype_p)
9216 cp_expr expression = NULL_TREE;
9217 location_t loc = UNKNOWN_LOCATION;
9219 while (true)
9221 cp_expr assignment_expression;
9223 /* Parse the next assignment-expression. */
9224 assignment_expression
9225 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9227 /* We don't create a temporary for a call that is the immediate operand
9228 of decltype or on the RHS of a comma. But when we see a comma, we
9229 need to create a temporary for a call on the LHS. */
9230 if (decltype_p && !processing_template_decl
9231 && TREE_CODE (assignment_expression) == CALL_EXPR
9232 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9233 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9234 assignment_expression
9235 = build_cplus_new (TREE_TYPE (assignment_expression),
9236 assignment_expression, tf_warning_or_error);
9238 /* If this is the first assignment-expression, we can just
9239 save it away. */
9240 if (!expression)
9241 expression = assignment_expression;
9242 else
9244 /* Create a location with caret at the comma, ranging
9245 from the start of the LHS to the end of the RHS. */
9246 loc = make_location (loc,
9247 expression.get_start (),
9248 assignment_expression.get_finish ());
9249 expression = build_x_compound_expr (loc, expression,
9250 assignment_expression,
9251 complain_flags (decltype_p));
9252 expression.set_location (loc);
9254 /* If the next token is not a comma, or we're in a fold-expression, then
9255 we are done with the expression. */
9256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9257 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9258 break;
9259 /* Consume the `,'. */
9260 loc = cp_lexer_peek_token (parser->lexer)->location;
9261 cp_lexer_consume_token (parser->lexer);
9262 /* A comma operator cannot appear in a constant-expression. */
9263 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9264 expression = error_mark_node;
9267 return expression;
9270 /* Parse a constant-expression.
9272 constant-expression:
9273 conditional-expression
9275 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9276 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9277 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9278 is false, NON_CONSTANT_P should be NULL. */
9280 static cp_expr
9281 cp_parser_constant_expression (cp_parser* parser,
9282 bool allow_non_constant_p,
9283 bool *non_constant_p)
9285 bool saved_integral_constant_expression_p;
9286 bool saved_allow_non_integral_constant_expression_p;
9287 bool saved_non_integral_constant_expression_p;
9288 cp_expr expression;
9290 /* It might seem that we could simply parse the
9291 conditional-expression, and then check to see if it were
9292 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9293 one that the compiler can figure out is constant, possibly after
9294 doing some simplifications or optimizations. The standard has a
9295 precise definition of constant-expression, and we must honor
9296 that, even though it is somewhat more restrictive.
9298 For example:
9300 int i[(2, 3)];
9302 is not a legal declaration, because `(2, 3)' is not a
9303 constant-expression. The `,' operator is forbidden in a
9304 constant-expression. However, GCC's constant-folding machinery
9305 will fold this operation to an INTEGER_CST for `3'. */
9307 /* Save the old settings. */
9308 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9309 saved_allow_non_integral_constant_expression_p
9310 = parser->allow_non_integral_constant_expression_p;
9311 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9312 /* We are now parsing a constant-expression. */
9313 parser->integral_constant_expression_p = true;
9314 parser->allow_non_integral_constant_expression_p
9315 = (allow_non_constant_p || cxx_dialect >= cxx11);
9316 parser->non_integral_constant_expression_p = false;
9317 /* Although the grammar says "conditional-expression", we parse an
9318 "assignment-expression", which also permits "throw-expression"
9319 and the use of assignment operators. In the case that
9320 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9321 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9322 actually essential that we look for an assignment-expression.
9323 For example, cp_parser_initializer_clauses uses this function to
9324 determine whether a particular assignment-expression is in fact
9325 constant. */
9326 expression = cp_parser_assignment_expression (parser);
9327 /* Restore the old settings. */
9328 parser->integral_constant_expression_p
9329 = saved_integral_constant_expression_p;
9330 parser->allow_non_integral_constant_expression_p
9331 = saved_allow_non_integral_constant_expression_p;
9332 if (cxx_dialect >= cxx11)
9334 /* Require an rvalue constant expression here; that's what our
9335 callers expect. Reference constant expressions are handled
9336 separately in e.g. cp_parser_template_argument. */
9337 bool is_const = potential_rvalue_constant_expression (expression);
9338 parser->non_integral_constant_expression_p = !is_const;
9339 if (!is_const && !allow_non_constant_p)
9340 require_potential_rvalue_constant_expression (expression);
9342 if (allow_non_constant_p)
9343 *non_constant_p = parser->non_integral_constant_expression_p;
9344 parser->non_integral_constant_expression_p
9345 = saved_non_integral_constant_expression_p;
9347 return expression;
9350 /* Parse __builtin_offsetof.
9352 offsetof-expression:
9353 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9355 offsetof-member-designator:
9356 id-expression
9357 | offsetof-member-designator "." id-expression
9358 | offsetof-member-designator "[" expression "]"
9359 | offsetof-member-designator "->" id-expression */
9361 static cp_expr
9362 cp_parser_builtin_offsetof (cp_parser *parser)
9364 int save_ice_p, save_non_ice_p;
9365 tree type;
9366 cp_expr expr;
9367 cp_id_kind dummy;
9368 cp_token *token;
9369 location_t finish_loc;
9371 /* We're about to accept non-integral-constant things, but will
9372 definitely yield an integral constant expression. Save and
9373 restore these values around our local parsing. */
9374 save_ice_p = parser->integral_constant_expression_p;
9375 save_non_ice_p = parser->non_integral_constant_expression_p;
9377 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9379 /* Consume the "__builtin_offsetof" token. */
9380 cp_lexer_consume_token (parser->lexer);
9381 /* Consume the opening `('. */
9382 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9383 /* Parse the type-id. */
9384 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9385 type = cp_parser_type_id (parser);
9386 /* Look for the `,'. */
9387 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9388 token = cp_lexer_peek_token (parser->lexer);
9390 /* Build the (type *)null that begins the traditional offsetof macro. */
9391 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9392 tf_warning_or_error);
9394 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9395 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9396 true, &dummy, token->location);
9397 while (true)
9399 token = cp_lexer_peek_token (parser->lexer);
9400 switch (token->type)
9402 case CPP_OPEN_SQUARE:
9403 /* offsetof-member-designator "[" expression "]" */
9404 expr = cp_parser_postfix_open_square_expression (parser, expr,
9405 true, false);
9406 break;
9408 case CPP_DEREF:
9409 /* offsetof-member-designator "->" identifier */
9410 expr = grok_array_decl (token->location, expr,
9411 integer_zero_node, false);
9412 /* FALLTHRU */
9414 case CPP_DOT:
9415 /* offsetof-member-designator "." identifier */
9416 cp_lexer_consume_token (parser->lexer);
9417 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9418 expr, true, &dummy,
9419 token->location);
9420 break;
9422 case CPP_CLOSE_PAREN:
9423 /* Consume the ")" token. */
9424 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9425 cp_lexer_consume_token (parser->lexer);
9426 goto success;
9428 default:
9429 /* Error. We know the following require will fail, but
9430 that gives the proper error message. */
9431 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9432 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9433 expr = error_mark_node;
9434 goto failure;
9438 success:
9439 /* Make a location of the form:
9440 __builtin_offsetof (struct s, f)
9441 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9442 with caret at the type-id, ranging from the start of the
9443 "_builtin_offsetof" token to the close paren. */
9444 loc = make_location (loc, start_loc, finish_loc);
9445 /* The result will be an INTEGER_CST, so we need to explicitly
9446 preserve the location. */
9447 expr = cp_expr (finish_offsetof (expr, loc), loc);
9449 failure:
9450 parser->integral_constant_expression_p = save_ice_p;
9451 parser->non_integral_constant_expression_p = save_non_ice_p;
9453 return expr;
9456 /* Parse a trait expression.
9458 Returns a representation of the expression, the underlying type
9459 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9461 static tree
9462 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9464 cp_trait_kind kind;
9465 tree type1, type2 = NULL_TREE;
9466 bool binary = false;
9467 bool variadic = false;
9469 switch (keyword)
9471 case RID_HAS_NOTHROW_ASSIGN:
9472 kind = CPTK_HAS_NOTHROW_ASSIGN;
9473 break;
9474 case RID_HAS_NOTHROW_CONSTRUCTOR:
9475 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9476 break;
9477 case RID_HAS_NOTHROW_COPY:
9478 kind = CPTK_HAS_NOTHROW_COPY;
9479 break;
9480 case RID_HAS_TRIVIAL_ASSIGN:
9481 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9482 break;
9483 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9484 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9485 break;
9486 case RID_HAS_TRIVIAL_COPY:
9487 kind = CPTK_HAS_TRIVIAL_COPY;
9488 break;
9489 case RID_HAS_TRIVIAL_DESTRUCTOR:
9490 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9491 break;
9492 case RID_HAS_VIRTUAL_DESTRUCTOR:
9493 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9494 break;
9495 case RID_IS_ABSTRACT:
9496 kind = CPTK_IS_ABSTRACT;
9497 break;
9498 case RID_IS_BASE_OF:
9499 kind = CPTK_IS_BASE_OF;
9500 binary = true;
9501 break;
9502 case RID_IS_CLASS:
9503 kind = CPTK_IS_CLASS;
9504 break;
9505 case RID_IS_EMPTY:
9506 kind = CPTK_IS_EMPTY;
9507 break;
9508 case RID_IS_ENUM:
9509 kind = CPTK_IS_ENUM;
9510 break;
9511 case RID_IS_FINAL:
9512 kind = CPTK_IS_FINAL;
9513 break;
9514 case RID_IS_LITERAL_TYPE:
9515 kind = CPTK_IS_LITERAL_TYPE;
9516 break;
9517 case RID_IS_POD:
9518 kind = CPTK_IS_POD;
9519 break;
9520 case RID_IS_POLYMORPHIC:
9521 kind = CPTK_IS_POLYMORPHIC;
9522 break;
9523 case RID_IS_SAME_AS:
9524 kind = CPTK_IS_SAME_AS;
9525 binary = true;
9526 break;
9527 case RID_IS_STD_LAYOUT:
9528 kind = CPTK_IS_STD_LAYOUT;
9529 break;
9530 case RID_IS_TRIVIAL:
9531 kind = CPTK_IS_TRIVIAL;
9532 break;
9533 case RID_IS_TRIVIALLY_ASSIGNABLE:
9534 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9535 binary = true;
9536 break;
9537 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9538 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9539 variadic = true;
9540 break;
9541 case RID_IS_TRIVIALLY_COPYABLE:
9542 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9543 break;
9544 case RID_IS_UNION:
9545 kind = CPTK_IS_UNION;
9546 break;
9547 case RID_UNDERLYING_TYPE:
9548 kind = CPTK_UNDERLYING_TYPE;
9549 break;
9550 case RID_BASES:
9551 kind = CPTK_BASES;
9552 break;
9553 case RID_DIRECT_BASES:
9554 kind = CPTK_DIRECT_BASES;
9555 break;
9556 default:
9557 gcc_unreachable ();
9560 /* Consume the token. */
9561 cp_lexer_consume_token (parser->lexer);
9563 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9566 type_id_in_expr_sentinel s (parser);
9567 type1 = cp_parser_type_id (parser);
9570 if (type1 == error_mark_node)
9571 return error_mark_node;
9573 if (binary)
9575 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9578 type_id_in_expr_sentinel s (parser);
9579 type2 = cp_parser_type_id (parser);
9582 if (type2 == error_mark_node)
9583 return error_mark_node;
9585 else if (variadic)
9587 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9589 cp_lexer_consume_token (parser->lexer);
9590 tree elt = cp_parser_type_id (parser);
9591 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9593 cp_lexer_consume_token (parser->lexer);
9594 elt = make_pack_expansion (elt);
9596 if (elt == error_mark_node)
9597 return error_mark_node;
9598 type2 = tree_cons (NULL_TREE, elt, type2);
9602 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9604 /* Complete the trait expression, which may mean either processing
9605 the trait expr now or saving it for template instantiation. */
9606 switch(kind)
9608 case CPTK_UNDERLYING_TYPE:
9609 return finish_underlying_type (type1);
9610 case CPTK_BASES:
9611 return finish_bases (type1, false);
9612 case CPTK_DIRECT_BASES:
9613 return finish_bases (type1, true);
9614 default:
9615 return finish_trait_expr (kind, type1, type2);
9619 /* Lambdas that appear in variable initializer or default argument scope
9620 get that in their mangling, so we need to record it. We might as well
9621 use the count for function and namespace scopes as well. */
9622 static GTY(()) tree lambda_scope;
9623 static GTY(()) int lambda_count;
9624 struct GTY(()) tree_int
9626 tree t;
9627 int i;
9629 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9631 static void
9632 start_lambda_scope (tree decl)
9634 tree_int ti;
9635 gcc_assert (decl);
9636 /* Once we're inside a function, we ignore other scopes and just push
9637 the function again so that popping works properly. */
9638 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9639 decl = current_function_decl;
9640 ti.t = lambda_scope;
9641 ti.i = lambda_count;
9642 vec_safe_push (lambda_scope_stack, ti);
9643 if (lambda_scope != decl)
9645 /* Don't reset the count if we're still in the same function. */
9646 lambda_scope = decl;
9647 lambda_count = 0;
9651 static void
9652 record_lambda_scope (tree lambda)
9654 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9655 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9658 static void
9659 finish_lambda_scope (void)
9661 tree_int *p = &lambda_scope_stack->last ();
9662 if (lambda_scope != p->t)
9664 lambda_scope = p->t;
9665 lambda_count = p->i;
9667 lambda_scope_stack->pop ();
9670 /* Parse a lambda expression.
9672 lambda-expression:
9673 lambda-introducer lambda-declarator [opt] compound-statement
9675 Returns a representation of the expression. */
9677 static cp_expr
9678 cp_parser_lambda_expression (cp_parser* parser)
9680 tree lambda_expr = build_lambda_expr ();
9681 tree type;
9682 bool ok = true;
9683 cp_token *token = cp_lexer_peek_token (parser->lexer);
9684 cp_token_position start = 0;
9686 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9688 if (cp_unevaluated_operand)
9690 if (!token->error_reported)
9692 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9693 "lambda-expression in unevaluated context");
9694 token->error_reported = true;
9696 ok = false;
9698 else if (parser->in_template_argument_list_p)
9700 if (!token->error_reported)
9702 error_at (token->location, "lambda-expression in template-argument");
9703 token->error_reported = true;
9705 ok = false;
9708 /* We may be in the middle of deferred access check. Disable
9709 it now. */
9710 push_deferring_access_checks (dk_no_deferred);
9712 cp_parser_lambda_introducer (parser, lambda_expr);
9714 type = begin_lambda_type (lambda_expr);
9715 if (type == error_mark_node)
9716 return error_mark_node;
9718 record_lambda_scope (lambda_expr);
9720 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9721 determine_visibility (TYPE_NAME (type));
9723 /* Now that we've started the type, add the capture fields for any
9724 explicit captures. */
9725 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9728 /* Inside the class, surrounding template-parameter-lists do not apply. */
9729 unsigned int saved_num_template_parameter_lists
9730 = parser->num_template_parameter_lists;
9731 unsigned char in_statement = parser->in_statement;
9732 bool in_switch_statement_p = parser->in_switch_statement_p;
9733 bool fully_implicit_function_template_p
9734 = parser->fully_implicit_function_template_p;
9735 tree implicit_template_parms = parser->implicit_template_parms;
9736 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9737 bool auto_is_implicit_function_template_parm_p
9738 = parser->auto_is_implicit_function_template_parm_p;
9740 parser->num_template_parameter_lists = 0;
9741 parser->in_statement = 0;
9742 parser->in_switch_statement_p = false;
9743 parser->fully_implicit_function_template_p = false;
9744 parser->implicit_template_parms = 0;
9745 parser->implicit_template_scope = 0;
9746 parser->auto_is_implicit_function_template_parm_p = false;
9748 /* By virtue of defining a local class, a lambda expression has access to
9749 the private variables of enclosing classes. */
9751 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9753 if (ok)
9755 if (!cp_parser_error_occurred (parser)
9756 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9757 && cp_parser_start_tentative_firewall (parser))
9758 start = token;
9759 cp_parser_lambda_body (parser, lambda_expr);
9761 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9763 if (cp_parser_skip_to_closing_brace (parser))
9764 cp_lexer_consume_token (parser->lexer);
9767 /* The capture list was built up in reverse order; fix that now. */
9768 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9769 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9771 if (ok)
9772 maybe_add_lambda_conv_op (type);
9774 type = finish_struct (type, /*attributes=*/NULL_TREE);
9776 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9777 parser->in_statement = in_statement;
9778 parser->in_switch_statement_p = in_switch_statement_p;
9779 parser->fully_implicit_function_template_p
9780 = fully_implicit_function_template_p;
9781 parser->implicit_template_parms = implicit_template_parms;
9782 parser->implicit_template_scope = implicit_template_scope;
9783 parser->auto_is_implicit_function_template_parm_p
9784 = auto_is_implicit_function_template_parm_p;
9787 /* This field is only used during parsing of the lambda. */
9788 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9790 /* This lambda shouldn't have any proxies left at this point. */
9791 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9792 /* And now that we're done, push proxies for an enclosing lambda. */
9793 insert_pending_capture_proxies ();
9795 if (ok)
9796 lambda_expr = build_lambda_object (lambda_expr);
9797 else
9798 lambda_expr = error_mark_node;
9800 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9802 pop_deferring_access_checks ();
9804 return lambda_expr;
9807 /* Parse the beginning of a lambda expression.
9809 lambda-introducer:
9810 [ lambda-capture [opt] ]
9812 LAMBDA_EXPR is the current representation of the lambda expression. */
9814 static void
9815 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9817 /* Need commas after the first capture. */
9818 bool first = true;
9820 /* Eat the leading `['. */
9821 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9823 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9824 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9825 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9826 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9827 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9828 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9830 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9832 cp_lexer_consume_token (parser->lexer);
9833 first = false;
9836 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9838 cp_token* capture_token;
9839 tree capture_id;
9840 tree capture_init_expr;
9841 cp_id_kind idk = CP_ID_KIND_NONE;
9842 bool explicit_init_p = false;
9844 enum capture_kind_type
9846 BY_COPY,
9847 BY_REFERENCE
9849 enum capture_kind_type capture_kind = BY_COPY;
9851 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9853 error ("expected end of capture-list");
9854 return;
9857 if (first)
9858 first = false;
9859 else
9860 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9862 /* Possibly capture `this'. */
9863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9866 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9867 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9868 "with by-copy capture default");
9869 cp_lexer_consume_token (parser->lexer);
9870 add_capture (lambda_expr,
9871 /*id=*/this_identifier,
9872 /*initializer=*/finish_this_expr(),
9873 /*by_reference_p=*/false,
9874 explicit_init_p);
9875 continue;
9878 /* Remember whether we want to capture as a reference or not. */
9879 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9881 capture_kind = BY_REFERENCE;
9882 cp_lexer_consume_token (parser->lexer);
9885 /* Get the identifier. */
9886 capture_token = cp_lexer_peek_token (parser->lexer);
9887 capture_id = cp_parser_identifier (parser);
9889 if (capture_id == error_mark_node)
9890 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9891 delimiters, but I modified this to stop on unnested ']' as well. It
9892 was already changed to stop on unnested '}', so the
9893 "closing_parenthesis" name is no more misleading with my change. */
9895 cp_parser_skip_to_closing_parenthesis (parser,
9896 /*recovering=*/true,
9897 /*or_comma=*/true,
9898 /*consume_paren=*/true);
9899 break;
9902 /* Find the initializer for this capture. */
9903 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9904 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9905 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9907 bool direct, non_constant;
9908 /* An explicit initializer exists. */
9909 if (cxx_dialect < cxx14)
9910 pedwarn (input_location, 0,
9911 "lambda capture initializers "
9912 "only available with -std=c++14 or -std=gnu++14");
9913 capture_init_expr = cp_parser_initializer (parser, &direct,
9914 &non_constant);
9915 explicit_init_p = true;
9916 if (capture_init_expr == NULL_TREE)
9918 error ("empty initializer for lambda init-capture");
9919 capture_init_expr = error_mark_node;
9922 else
9924 const char* error_msg;
9926 /* Turn the identifier into an id-expression. */
9927 capture_init_expr
9928 = cp_parser_lookup_name_simple (parser, capture_id,
9929 capture_token->location);
9931 if (capture_init_expr == error_mark_node)
9933 unqualified_name_lookup_error (capture_id);
9934 continue;
9936 else if (DECL_P (capture_init_expr)
9937 && (!VAR_P (capture_init_expr)
9938 && TREE_CODE (capture_init_expr) != PARM_DECL))
9940 error_at (capture_token->location,
9941 "capture of non-variable %qD ",
9942 capture_init_expr);
9943 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9944 "%q#D declared here", capture_init_expr);
9945 continue;
9947 if (VAR_P (capture_init_expr)
9948 && decl_storage_duration (capture_init_expr) != dk_auto)
9950 if (pedwarn (capture_token->location, 0, "capture of variable "
9951 "%qD with non-automatic storage duration",
9952 capture_init_expr))
9953 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9954 "%q#D declared here", capture_init_expr);
9955 continue;
9958 capture_init_expr
9959 = finish_id_expression
9960 (capture_id,
9961 capture_init_expr,
9962 parser->scope,
9963 &idk,
9964 /*integral_constant_expression_p=*/false,
9965 /*allow_non_integral_constant_expression_p=*/false,
9966 /*non_integral_constant_expression_p=*/NULL,
9967 /*template_p=*/false,
9968 /*done=*/true,
9969 /*address_p=*/false,
9970 /*template_arg_p=*/false,
9971 &error_msg,
9972 capture_token->location);
9974 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9976 cp_lexer_consume_token (parser->lexer);
9977 capture_init_expr = make_pack_expansion (capture_init_expr);
9979 else
9980 check_for_bare_parameter_packs (capture_init_expr);
9983 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9984 && !explicit_init_p)
9986 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9987 && capture_kind == BY_COPY)
9988 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9989 "of %qD redundant with by-copy capture default",
9990 capture_id);
9991 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9992 && capture_kind == BY_REFERENCE)
9993 pedwarn (capture_token->location, 0, "explicit by-reference "
9994 "capture of %qD redundant with by-reference capture "
9995 "default", capture_id);
9998 add_capture (lambda_expr,
9999 capture_id,
10000 capture_init_expr,
10001 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10002 explicit_init_p);
10005 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10008 /* Parse the (optional) middle of a lambda expression.
10010 lambda-declarator:
10011 < template-parameter-list [opt] >
10012 ( parameter-declaration-clause [opt] )
10013 attribute-specifier [opt]
10014 mutable [opt]
10015 exception-specification [opt]
10016 lambda-return-type-clause [opt]
10018 LAMBDA_EXPR is the current representation of the lambda expression. */
10020 static bool
10021 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10023 /* 5.1.1.4 of the standard says:
10024 If a lambda-expression does not include a lambda-declarator, it is as if
10025 the lambda-declarator were ().
10026 This means an empty parameter list, no attributes, and no exception
10027 specification. */
10028 tree param_list = void_list_node;
10029 tree attributes = NULL_TREE;
10030 tree exception_spec = NULL_TREE;
10031 tree template_param_list = NULL_TREE;
10032 tree tx_qual = NULL_TREE;
10034 /* The template-parameter-list is optional, but must begin with
10035 an opening angle if present. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10038 if (cxx_dialect < cxx14)
10039 pedwarn (parser->lexer->next_token->location, 0,
10040 "lambda templates are only available with "
10041 "-std=c++14 or -std=gnu++14");
10043 cp_lexer_consume_token (parser->lexer);
10045 template_param_list = cp_parser_template_parameter_list (parser);
10047 cp_parser_skip_to_end_of_template_parameter_list (parser);
10049 /* We just processed one more parameter list. */
10050 ++parser->num_template_parameter_lists;
10053 /* The parameter-declaration-clause is optional (unless
10054 template-parameter-list was given), but must begin with an
10055 opening parenthesis if present. */
10056 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10058 cp_lexer_consume_token (parser->lexer);
10060 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10062 /* Parse parameters. */
10063 param_list = cp_parser_parameter_declaration_clause (parser);
10065 /* Default arguments shall not be specified in the
10066 parameter-declaration-clause of a lambda-declarator. */
10067 for (tree t = param_list; t; t = TREE_CHAIN (t))
10068 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10069 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10070 "default argument specified for lambda parameter");
10072 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10074 attributes = cp_parser_attributes_opt (parser);
10076 /* Parse optional `mutable' keyword. */
10077 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10079 cp_lexer_consume_token (parser->lexer);
10080 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10083 tx_qual = cp_parser_tx_qualifier_opt (parser);
10085 /* Parse optional exception specification. */
10086 exception_spec = cp_parser_exception_specification_opt (parser);
10088 /* Parse optional trailing return type. */
10089 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10091 cp_lexer_consume_token (parser->lexer);
10092 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10093 = cp_parser_trailing_type_id (parser);
10096 /* The function parameters must be in scope all the way until after the
10097 trailing-return-type in case of decltype. */
10098 pop_bindings_and_leave_scope ();
10100 else if (template_param_list != NULL_TREE) // generate diagnostic
10101 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10103 /* Create the function call operator.
10105 Messing with declarators like this is no uglier than building up the
10106 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10107 other code. */
10109 cp_decl_specifier_seq return_type_specs;
10110 cp_declarator* declarator;
10111 tree fco;
10112 int quals;
10113 void *p;
10115 clear_decl_specs (&return_type_specs);
10116 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10117 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10118 else
10119 /* Maybe we will deduce the return type later. */
10120 return_type_specs.type = make_auto ();
10122 p = obstack_alloc (&declarator_obstack, 0);
10124 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10125 sfk_none);
10127 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10128 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10129 declarator = make_call_declarator (declarator, param_list, quals,
10130 VIRT_SPEC_UNSPECIFIED,
10131 REF_QUAL_NONE,
10132 tx_qual,
10133 exception_spec,
10134 /*late_return_type=*/NULL_TREE,
10135 /*requires_clause*/NULL_TREE);
10136 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10138 fco = grokmethod (&return_type_specs,
10139 declarator,
10140 attributes);
10141 if (fco != error_mark_node)
10143 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10144 DECL_ARTIFICIAL (fco) = 1;
10145 /* Give the object parameter a different name. */
10146 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10147 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10148 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10150 if (template_param_list)
10152 fco = finish_member_template_decl (fco);
10153 finish_template_decl (template_param_list);
10154 --parser->num_template_parameter_lists;
10156 else if (parser->fully_implicit_function_template_p)
10157 fco = finish_fully_implicit_template (parser, fco);
10159 finish_member_declaration (fco);
10161 obstack_free (&declarator_obstack, p);
10163 return (fco != error_mark_node);
10167 /* Parse the body of a lambda expression, which is simply
10169 compound-statement
10171 but which requires special handling.
10172 LAMBDA_EXPR is the current representation of the lambda expression. */
10174 static void
10175 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10177 bool nested = (current_function_decl != NULL_TREE);
10178 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10179 if (nested)
10180 push_function_context ();
10181 else
10182 /* Still increment function_depth so that we don't GC in the
10183 middle of an expression. */
10184 ++function_depth;
10185 vec<tree> omp_privatization_save;
10186 save_omp_privatization_clauses (omp_privatization_save);
10187 /* Clear this in case we're in the middle of a default argument. */
10188 parser->local_variables_forbidden_p = false;
10190 /* Finish the function call operator
10191 - class_specifier
10192 + late_parsing_for_member
10193 + function_definition_after_declarator
10194 + ctor_initializer_opt_and_function_body */
10196 tree fco = lambda_function (lambda_expr);
10197 tree body;
10198 bool done = false;
10199 tree compound_stmt;
10200 tree cap;
10202 /* Let the front end know that we are going to be defining this
10203 function. */
10204 start_preparsed_function (fco,
10205 NULL_TREE,
10206 SF_PRE_PARSED | SF_INCLASS_INLINE);
10208 start_lambda_scope (fco);
10209 body = begin_function_body ();
10211 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10212 goto out;
10214 /* Push the proxies for any explicit captures. */
10215 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10216 cap = TREE_CHAIN (cap))
10217 build_capture_proxy (TREE_PURPOSE (cap));
10219 compound_stmt = begin_compound_stmt (0);
10221 /* 5.1.1.4 of the standard says:
10222 If a lambda-expression does not include a trailing-return-type, it
10223 is as if the trailing-return-type denotes the following type:
10224 * if the compound-statement is of the form
10225 { return attribute-specifier [opt] expression ; }
10226 the type of the returned expression after lvalue-to-rvalue
10227 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10228 (_conv.array_ 4.2), and function-to-pointer conversion
10229 (_conv.func_ 4.3);
10230 * otherwise, void. */
10232 /* In a lambda that has neither a lambda-return-type-clause
10233 nor a deducible form, errors should be reported for return statements
10234 in the body. Since we used void as the placeholder return type, parsing
10235 the body as usual will give such desired behavior. */
10236 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10237 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10238 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10240 tree expr = NULL_TREE;
10241 cp_id_kind idk = CP_ID_KIND_NONE;
10243 /* Parse tentatively in case there's more after the initial return
10244 statement. */
10245 cp_parser_parse_tentatively (parser);
10247 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10249 expr = cp_parser_expression (parser, &idk);
10251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10252 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10254 if (cp_parser_parse_definitely (parser))
10256 if (!processing_template_decl)
10258 tree type = lambda_return_type (expr);
10259 apply_deduced_return_type (fco, type);
10260 if (type == error_mark_node)
10261 expr = error_mark_node;
10264 /* Will get error here if type not deduced yet. */
10265 finish_return_stmt (expr);
10267 done = true;
10271 if (!done)
10273 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10274 cp_parser_label_declaration (parser);
10275 cp_parser_statement_seq_opt (parser, NULL_TREE);
10276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10279 finish_compound_stmt (compound_stmt);
10281 out:
10282 finish_function_body (body);
10283 finish_lambda_scope ();
10285 /* Finish the function and generate code for it if necessary. */
10286 tree fn = finish_function (/*inline*/2);
10288 /* Only expand if the call op is not a template. */
10289 if (!DECL_TEMPLATE_INFO (fco))
10290 expand_or_defer_fn (fn);
10293 restore_omp_privatization_clauses (omp_privatization_save);
10294 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10295 if (nested)
10296 pop_function_context();
10297 else
10298 --function_depth;
10301 /* Statements [gram.stmt.stmt] */
10303 /* Parse a statement.
10305 statement:
10306 labeled-statement
10307 expression-statement
10308 compound-statement
10309 selection-statement
10310 iteration-statement
10311 jump-statement
10312 declaration-statement
10313 try-block
10315 C++11:
10317 statement:
10318 labeled-statement
10319 attribute-specifier-seq (opt) expression-statement
10320 attribute-specifier-seq (opt) compound-statement
10321 attribute-specifier-seq (opt) selection-statement
10322 attribute-specifier-seq (opt) iteration-statement
10323 attribute-specifier-seq (opt) jump-statement
10324 declaration-statement
10325 attribute-specifier-seq (opt) try-block
10327 TM Extension:
10329 statement:
10330 atomic-statement
10332 IN_COMPOUND is true when the statement is nested inside a
10333 cp_parser_compound_statement; this matters for certain pragmas.
10335 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10336 is a (possibly labeled) if statement which is not enclosed in braces
10337 and has an else clause. This is used to implement -Wparentheses.
10339 CHAIN is a vector of if-else-if conditions. */
10341 static void
10342 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10343 bool in_compound, bool *if_p, vec<tree> *chain)
10345 tree statement, std_attrs = NULL_TREE;
10346 cp_token *token;
10347 location_t statement_location, attrs_location;
10349 restart:
10350 if (if_p != NULL)
10351 *if_p = false;
10352 /* There is no statement yet. */
10353 statement = NULL_TREE;
10355 saved_token_sentinel saved_tokens (parser->lexer);
10356 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10357 if (c_dialect_objc ())
10358 /* In obj-c++, seeing '[[' might be the either the beginning of
10359 c++11 attributes, or a nested objc-message-expression. So
10360 let's parse the c++11 attributes tentatively. */
10361 cp_parser_parse_tentatively (parser);
10362 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10363 if (c_dialect_objc ())
10365 if (!cp_parser_parse_definitely (parser))
10366 std_attrs = NULL_TREE;
10369 /* Peek at the next token. */
10370 token = cp_lexer_peek_token (parser->lexer);
10371 /* Remember the location of the first token in the statement. */
10372 statement_location = token->location;
10373 /* If this is a keyword, then that will often determine what kind of
10374 statement we have. */
10375 if (token->type == CPP_KEYWORD)
10377 enum rid keyword = token->keyword;
10379 switch (keyword)
10381 case RID_CASE:
10382 case RID_DEFAULT:
10383 /* Looks like a labeled-statement with a case label.
10384 Parse the label, and then use tail recursion to parse
10385 the statement. */
10386 cp_parser_label_for_labeled_statement (parser, std_attrs);
10387 in_compound = false;
10388 goto restart;
10390 case RID_IF:
10391 case RID_SWITCH:
10392 statement = cp_parser_selection_statement (parser, if_p, chain);
10393 break;
10395 case RID_WHILE:
10396 case RID_DO:
10397 case RID_FOR:
10398 statement = cp_parser_iteration_statement (parser, if_p, false);
10399 break;
10401 case RID_CILK_FOR:
10402 if (!flag_cilkplus)
10404 error_at (cp_lexer_peek_token (parser->lexer)->location,
10405 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10406 cp_lexer_consume_token (parser->lexer);
10407 statement = error_mark_node;
10409 else
10410 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10411 break;
10413 case RID_BREAK:
10414 case RID_CONTINUE:
10415 case RID_RETURN:
10416 case RID_GOTO:
10417 statement = cp_parser_jump_statement (parser);
10418 break;
10420 case RID_CILK_SYNC:
10421 cp_lexer_consume_token (parser->lexer);
10422 if (flag_cilkplus)
10424 tree sync_expr = build_cilk_sync ();
10425 SET_EXPR_LOCATION (sync_expr,
10426 token->location);
10427 statement = finish_expr_stmt (sync_expr);
10429 else
10431 error_at (token->location, "-fcilkplus must be enabled to use"
10432 " %<_Cilk_sync%>");
10433 statement = error_mark_node;
10435 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10436 break;
10438 /* Objective-C++ exception-handling constructs. */
10439 case RID_AT_TRY:
10440 case RID_AT_CATCH:
10441 case RID_AT_FINALLY:
10442 case RID_AT_SYNCHRONIZED:
10443 case RID_AT_THROW:
10444 statement = cp_parser_objc_statement (parser);
10445 break;
10447 case RID_TRY:
10448 statement = cp_parser_try_block (parser);
10449 break;
10451 case RID_NAMESPACE:
10452 /* This must be a namespace alias definition. */
10453 cp_parser_declaration_statement (parser);
10454 return;
10456 case RID_TRANSACTION_ATOMIC:
10457 case RID_TRANSACTION_RELAXED:
10458 case RID_SYNCHRONIZED:
10459 case RID_ATOMIC_NOEXCEPT:
10460 case RID_ATOMIC_CANCEL:
10461 statement = cp_parser_transaction (parser, token);
10462 break;
10463 case RID_TRANSACTION_CANCEL:
10464 statement = cp_parser_transaction_cancel (parser);
10465 break;
10467 default:
10468 /* It might be a keyword like `int' that can start a
10469 declaration-statement. */
10470 break;
10473 else if (token->type == CPP_NAME)
10475 /* If the next token is a `:', then we are looking at a
10476 labeled-statement. */
10477 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10478 if (token->type == CPP_COLON)
10480 /* Looks like a labeled-statement with an ordinary label.
10481 Parse the label, and then use tail recursion to parse
10482 the statement. */
10484 cp_parser_label_for_labeled_statement (parser, std_attrs);
10485 in_compound = false;
10486 goto restart;
10489 /* Anything that starts with a `{' must be a compound-statement. */
10490 else if (token->type == CPP_OPEN_BRACE)
10491 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10492 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10493 a statement all its own. */
10494 else if (token->type == CPP_PRAGMA)
10496 /* Only certain OpenMP pragmas are attached to statements, and thus
10497 are considered statements themselves. All others are not. In
10498 the context of a compound, accept the pragma as a "statement" and
10499 return so that we can check for a close brace. Otherwise we
10500 require a real statement and must go back and read one. */
10501 if (in_compound)
10502 cp_parser_pragma (parser, pragma_compound, if_p);
10503 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10504 goto restart;
10505 return;
10507 else if (token->type == CPP_EOF)
10509 cp_parser_error (parser, "expected statement");
10510 return;
10513 /* Everything else must be a declaration-statement or an
10514 expression-statement. Try for the declaration-statement
10515 first, unless we are looking at a `;', in which case we know that
10516 we have an expression-statement. */
10517 if (!statement)
10519 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10521 if (std_attrs != NULL_TREE)
10523 /* Attributes should be parsed as part of the the
10524 declaration, so let's un-parse them. */
10525 saved_tokens.rollback();
10526 std_attrs = NULL_TREE;
10529 cp_parser_parse_tentatively (parser);
10530 /* Try to parse the declaration-statement. */
10531 cp_parser_declaration_statement (parser);
10532 /* If that worked, we're done. */
10533 if (cp_parser_parse_definitely (parser))
10534 return;
10536 /* Look for an expression-statement instead. */
10537 statement = cp_parser_expression_statement (parser, in_statement_expr);
10540 /* Set the line number for the statement. */
10541 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10542 SET_EXPR_LOCATION (statement, statement_location);
10544 /* Note that for now, we don't do anything with c++11 statements
10545 parsed at this level. */
10546 if (std_attrs != NULL_TREE)
10547 warning_at (attrs_location,
10548 OPT_Wattributes,
10549 "attributes at the beginning of statement are ignored");
10552 /* Parse the label for a labeled-statement, i.e.
10554 identifier :
10555 case constant-expression :
10556 default :
10558 GNU Extension:
10559 case constant-expression ... constant-expression : statement
10561 When a label is parsed without errors, the label is added to the
10562 parse tree by the finish_* functions, so this function doesn't
10563 have to return the label. */
10565 static void
10566 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10568 cp_token *token;
10569 tree label = NULL_TREE;
10570 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10572 /* The next token should be an identifier. */
10573 token = cp_lexer_peek_token (parser->lexer);
10574 if (token->type != CPP_NAME
10575 && token->type != CPP_KEYWORD)
10577 cp_parser_error (parser, "expected labeled-statement");
10578 return;
10581 parser->colon_corrects_to_scope_p = false;
10582 switch (token->keyword)
10584 case RID_CASE:
10586 tree expr, expr_hi;
10587 cp_token *ellipsis;
10589 /* Consume the `case' token. */
10590 cp_lexer_consume_token (parser->lexer);
10591 /* Parse the constant-expression. */
10592 expr = cp_parser_constant_expression (parser);
10593 if (check_for_bare_parameter_packs (expr))
10594 expr = error_mark_node;
10596 ellipsis = cp_lexer_peek_token (parser->lexer);
10597 if (ellipsis->type == CPP_ELLIPSIS)
10599 /* Consume the `...' token. */
10600 cp_lexer_consume_token (parser->lexer);
10601 expr_hi = cp_parser_constant_expression (parser);
10602 if (check_for_bare_parameter_packs (expr_hi))
10603 expr_hi = error_mark_node;
10605 /* We don't need to emit warnings here, as the common code
10606 will do this for us. */
10608 else
10609 expr_hi = NULL_TREE;
10611 if (parser->in_switch_statement_p)
10612 finish_case_label (token->location, expr, expr_hi);
10613 else
10614 error_at (token->location,
10615 "case label %qE not within a switch statement",
10616 expr);
10618 break;
10620 case RID_DEFAULT:
10621 /* Consume the `default' token. */
10622 cp_lexer_consume_token (parser->lexer);
10624 if (parser->in_switch_statement_p)
10625 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10626 else
10627 error_at (token->location, "case label not within a switch statement");
10628 break;
10630 default:
10631 /* Anything else must be an ordinary label. */
10632 label = finish_label_stmt (cp_parser_identifier (parser));
10633 break;
10636 /* Require the `:' token. */
10637 cp_parser_require (parser, CPP_COLON, RT_COLON);
10639 /* An ordinary label may optionally be followed by attributes.
10640 However, this is only permitted if the attributes are then
10641 followed by a semicolon. This is because, for backward
10642 compatibility, when parsing
10643 lab: __attribute__ ((unused)) int i;
10644 we want the attribute to attach to "i", not "lab". */
10645 if (label != NULL_TREE
10646 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10648 tree attrs;
10649 cp_parser_parse_tentatively (parser);
10650 attrs = cp_parser_gnu_attributes_opt (parser);
10651 if (attrs == NULL_TREE
10652 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10653 cp_parser_abort_tentative_parse (parser);
10654 else if (!cp_parser_parse_definitely (parser))
10656 else
10657 attributes = chainon (attributes, attrs);
10660 if (attributes != NULL_TREE)
10661 cplus_decl_attributes (&label, attributes, 0);
10663 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10666 /* Parse an expression-statement.
10668 expression-statement:
10669 expression [opt] ;
10671 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10672 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10673 indicates whether this expression-statement is part of an
10674 expression statement. */
10676 static tree
10677 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10679 tree statement = NULL_TREE;
10680 cp_token *token = cp_lexer_peek_token (parser->lexer);
10682 /* If the next token is a ';', then there is no expression
10683 statement. */
10684 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10686 statement = cp_parser_expression (parser);
10687 if (statement == error_mark_node
10688 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10690 cp_parser_skip_to_end_of_block_or_statement (parser);
10691 return error_mark_node;
10695 /* Give a helpful message for "A<T>::type t;" and the like. */
10696 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10697 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10699 if (TREE_CODE (statement) == SCOPE_REF)
10700 error_at (token->location, "need %<typename%> before %qE because "
10701 "%qT is a dependent scope",
10702 statement, TREE_OPERAND (statement, 0));
10703 else if (is_overloaded_fn (statement)
10704 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10706 /* A::A a; */
10707 tree fn = get_first_fn (statement);
10708 error_at (token->location,
10709 "%<%T::%D%> names the constructor, not the type",
10710 DECL_CONTEXT (fn), DECL_NAME (fn));
10714 /* Consume the final `;'. */
10715 cp_parser_consume_semicolon_at_end_of_statement (parser);
10717 if (in_statement_expr
10718 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10719 /* This is the final expression statement of a statement
10720 expression. */
10721 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10722 else if (statement)
10723 statement = finish_expr_stmt (statement);
10725 return statement;
10728 /* Parse a compound-statement.
10730 compound-statement:
10731 { statement-seq [opt] }
10733 GNU extension:
10735 compound-statement:
10736 { label-declaration-seq [opt] statement-seq [opt] }
10738 label-declaration-seq:
10739 label-declaration
10740 label-declaration-seq label-declaration
10742 Returns a tree representing the statement. */
10744 static tree
10745 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10746 int bcs_flags, bool function_body)
10748 tree compound_stmt;
10750 /* Consume the `{'. */
10751 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10752 return error_mark_node;
10753 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10754 && !function_body && cxx_dialect < cxx14)
10755 pedwarn (input_location, OPT_Wpedantic,
10756 "compound-statement in constexpr function");
10757 /* Begin the compound-statement. */
10758 compound_stmt = begin_compound_stmt (bcs_flags);
10759 /* If the next keyword is `__label__' we have a label declaration. */
10760 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10761 cp_parser_label_declaration (parser);
10762 /* Parse an (optional) statement-seq. */
10763 cp_parser_statement_seq_opt (parser, in_statement_expr);
10764 /* Finish the compound-statement. */
10765 finish_compound_stmt (compound_stmt);
10766 /* Consume the `}'. */
10767 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10769 return compound_stmt;
10772 /* Parse an (optional) statement-seq.
10774 statement-seq:
10775 statement
10776 statement-seq [opt] statement */
10778 static void
10779 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10781 /* Scan statements until there aren't any more. */
10782 while (true)
10784 cp_token *token = cp_lexer_peek_token (parser->lexer);
10786 /* If we are looking at a `}', then we have run out of
10787 statements; the same is true if we have reached the end
10788 of file, or have stumbled upon a stray '@end'. */
10789 if (token->type == CPP_CLOSE_BRACE
10790 || token->type == CPP_EOF
10791 || token->type == CPP_PRAGMA_EOL
10792 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10793 break;
10795 /* If we are in a compound statement and find 'else' then
10796 something went wrong. */
10797 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10799 if (parser->in_statement & IN_IF_STMT)
10800 break;
10801 else
10803 token = cp_lexer_consume_token (parser->lexer);
10804 error_at (token->location, "%<else%> without a previous %<if%>");
10808 /* Parse the statement. */
10809 cp_parser_statement (parser, in_statement_expr, true, NULL);
10813 /* Parse a selection-statement.
10815 selection-statement:
10816 if ( condition ) statement
10817 if ( condition ) statement else statement
10818 switch ( condition ) statement
10820 Returns the new IF_STMT or SWITCH_STMT.
10822 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10823 is a (possibly labeled) if statement which is not enclosed in
10824 braces and has an else clause. This is used to implement
10825 -Wparentheses.
10827 CHAIN is a vector of if-else-if conditions. This is used to implement
10828 -Wduplicated-cond. */
10830 static tree
10831 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10832 vec<tree> *chain)
10834 cp_token *token;
10835 enum rid keyword;
10836 token_indent_info guard_tinfo;
10838 if (if_p != NULL)
10839 *if_p = false;
10841 /* Peek at the next token. */
10842 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10843 guard_tinfo = get_token_indent_info (token);
10845 /* See what kind of keyword it is. */
10846 keyword = token->keyword;
10847 switch (keyword)
10849 case RID_IF:
10850 case RID_SWITCH:
10852 tree statement;
10853 tree condition;
10855 /* Look for the `('. */
10856 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10858 cp_parser_skip_to_end_of_statement (parser);
10859 return error_mark_node;
10862 /* Begin the selection-statement. */
10863 if (keyword == RID_IF)
10864 statement = begin_if_stmt ();
10865 else
10866 statement = begin_switch_stmt ();
10868 /* Parse the condition. */
10869 condition = cp_parser_condition (parser);
10870 /* Look for the `)'. */
10871 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10872 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10873 /*consume_paren=*/true);
10875 if (keyword == RID_IF)
10877 bool nested_if;
10878 unsigned char in_statement;
10880 /* Add the condition. */
10881 finish_if_stmt_cond (condition, statement);
10883 if (warn_duplicated_cond)
10884 warn_duplicated_cond_add_or_warn (token->location, condition,
10885 &chain);
10887 /* Parse the then-clause. */
10888 in_statement = parser->in_statement;
10889 parser->in_statement |= IN_IF_STMT;
10890 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10891 guard_tinfo);
10892 parser->in_statement = in_statement;
10894 finish_then_clause (statement);
10896 /* If the next token is `else', parse the else-clause. */
10897 if (cp_lexer_next_token_is_keyword (parser->lexer,
10898 RID_ELSE))
10900 guard_tinfo
10901 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10902 /* Consume the `else' keyword. */
10903 cp_lexer_consume_token (parser->lexer);
10904 if (warn_duplicated_cond)
10906 if (cp_lexer_next_token_is_keyword (parser->lexer,
10907 RID_IF)
10908 && chain == NULL)
10910 /* We've got "if (COND) else if (COND2)". Start
10911 the condition chain and add COND as the first
10912 element. */
10913 chain = new vec<tree> ();
10914 if (!CONSTANT_CLASS_P (condition)
10915 && !TREE_SIDE_EFFECTS (condition))
10917 /* Wrap it in a NOP_EXPR so that we can set the
10918 location of the condition. */
10919 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10920 condition);
10921 SET_EXPR_LOCATION (e, token->location);
10922 chain->safe_push (e);
10925 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10926 RID_IF))
10928 /* This is if-else without subsequent if. Zap the
10929 condition chain; we would have already warned at
10930 this point. */
10931 delete chain;
10932 chain = NULL;
10935 begin_else_clause (statement);
10936 /* Parse the else-clause. */
10937 cp_parser_implicitly_scoped_statement (parser, NULL,
10938 guard_tinfo, chain);
10940 finish_else_clause (statement);
10942 /* If we are currently parsing a then-clause, then
10943 IF_P will not be NULL. We set it to true to
10944 indicate that this if statement has an else clause.
10945 This may trigger the Wparentheses warning below
10946 when we get back up to the parent if statement. */
10947 if (if_p != NULL)
10948 *if_p = true;
10950 else
10952 /* This if statement does not have an else clause. If
10953 NESTED_IF is true, then the then-clause has an if
10954 statement which does have an else clause. We warn
10955 about the potential ambiguity. */
10956 if (nested_if)
10957 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
10958 "suggest explicit braces to avoid ambiguous"
10959 " %<else%>");
10960 if (warn_duplicated_cond)
10962 /* We don't need the condition chain anymore. */
10963 delete chain;
10964 chain = NULL;
10968 /* Now we're all done with the if-statement. */
10969 finish_if_stmt (statement);
10971 else
10973 bool in_switch_statement_p;
10974 unsigned char in_statement;
10976 /* Add the condition. */
10977 finish_switch_cond (condition, statement);
10979 /* Parse the body of the switch-statement. */
10980 in_switch_statement_p = parser->in_switch_statement_p;
10981 in_statement = parser->in_statement;
10982 parser->in_switch_statement_p = true;
10983 parser->in_statement |= IN_SWITCH_STMT;
10984 cp_parser_implicitly_scoped_statement (parser, if_p,
10985 guard_tinfo);
10986 parser->in_switch_statement_p = in_switch_statement_p;
10987 parser->in_statement = in_statement;
10989 /* Now we're all done with the switch-statement. */
10990 finish_switch_stmt (statement);
10993 return statement;
10995 break;
10997 default:
10998 cp_parser_error (parser, "expected selection-statement");
10999 return error_mark_node;
11003 /* Parse a condition.
11005 condition:
11006 expression
11007 type-specifier-seq declarator = initializer-clause
11008 type-specifier-seq declarator braced-init-list
11010 GNU Extension:
11012 condition:
11013 type-specifier-seq declarator asm-specification [opt]
11014 attributes [opt] = assignment-expression
11016 Returns the expression that should be tested. */
11018 static tree
11019 cp_parser_condition (cp_parser* parser)
11021 cp_decl_specifier_seq type_specifiers;
11022 const char *saved_message;
11023 int declares_class_or_enum;
11025 /* Try the declaration first. */
11026 cp_parser_parse_tentatively (parser);
11027 /* New types are not allowed in the type-specifier-seq for a
11028 condition. */
11029 saved_message = parser->type_definition_forbidden_message;
11030 parser->type_definition_forbidden_message
11031 = G_("types may not be defined in conditions");
11032 /* Parse the type-specifier-seq. */
11033 cp_parser_decl_specifier_seq (parser,
11034 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11035 &type_specifiers,
11036 &declares_class_or_enum);
11037 /* Restore the saved message. */
11038 parser->type_definition_forbidden_message = saved_message;
11039 /* If all is well, we might be looking at a declaration. */
11040 if (!cp_parser_error_occurred (parser))
11042 tree decl;
11043 tree asm_specification;
11044 tree attributes;
11045 cp_declarator *declarator;
11046 tree initializer = NULL_TREE;
11048 /* Parse the declarator. */
11049 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11050 /*ctor_dtor_or_conv_p=*/NULL,
11051 /*parenthesized_p=*/NULL,
11052 /*member_p=*/false,
11053 /*friend_p=*/false);
11054 /* Parse the attributes. */
11055 attributes = cp_parser_attributes_opt (parser);
11056 /* Parse the asm-specification. */
11057 asm_specification = cp_parser_asm_specification_opt (parser);
11058 /* If the next token is not an `=' or '{', then we might still be
11059 looking at an expression. For example:
11061 if (A(a).x)
11063 looks like a decl-specifier-seq and a declarator -- but then
11064 there is no `=', so this is an expression. */
11065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11066 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11067 cp_parser_simulate_error (parser);
11069 /* If we did see an `=' or '{', then we are looking at a declaration
11070 for sure. */
11071 if (cp_parser_parse_definitely (parser))
11073 tree pushed_scope;
11074 bool non_constant_p;
11075 bool flags = LOOKUP_ONLYCONVERTING;
11077 /* Create the declaration. */
11078 decl = start_decl (declarator, &type_specifiers,
11079 /*initialized_p=*/true,
11080 attributes, /*prefix_attributes=*/NULL_TREE,
11081 &pushed_scope);
11083 /* Parse the initializer. */
11084 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11086 initializer = cp_parser_braced_list (parser, &non_constant_p);
11087 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11088 flags = 0;
11090 else
11092 /* Consume the `='. */
11093 cp_parser_require (parser, CPP_EQ, RT_EQ);
11094 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11096 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11097 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11099 /* Process the initializer. */
11100 cp_finish_decl (decl,
11101 initializer, !non_constant_p,
11102 asm_specification,
11103 flags);
11105 if (pushed_scope)
11106 pop_scope (pushed_scope);
11108 return convert_from_reference (decl);
11111 /* If we didn't even get past the declarator successfully, we are
11112 definitely not looking at a declaration. */
11113 else
11114 cp_parser_abort_tentative_parse (parser);
11116 /* Otherwise, we are looking at an expression. */
11117 return cp_parser_expression (parser);
11120 /* Parses a for-statement or range-for-statement until the closing ')',
11121 not included. */
11123 static tree
11124 cp_parser_for (cp_parser *parser, bool ivdep)
11126 tree init, scope, decl;
11127 bool is_range_for;
11129 /* Begin the for-statement. */
11130 scope = begin_for_scope (&init);
11132 /* Parse the initialization. */
11133 is_range_for = cp_parser_for_init_statement (parser, &decl);
11135 if (is_range_for)
11136 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11137 else
11138 return cp_parser_c_for (parser, scope, init, ivdep);
11141 static tree
11142 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11144 /* Normal for loop */
11145 tree condition = NULL_TREE;
11146 tree expression = NULL_TREE;
11147 tree stmt;
11149 stmt = begin_for_stmt (scope, init);
11150 /* The for-init-statement has already been parsed in
11151 cp_parser_for_init_statement, so no work is needed here. */
11152 finish_for_init_stmt (stmt);
11154 /* If there's a condition, process it. */
11155 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11156 condition = cp_parser_condition (parser);
11157 else if (ivdep)
11159 cp_parser_error (parser, "missing loop condition in loop with "
11160 "%<GCC ivdep%> pragma");
11161 condition = error_mark_node;
11163 finish_for_cond (condition, stmt, ivdep);
11164 /* Look for the `;'. */
11165 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11167 /* If there's an expression, process it. */
11168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11169 expression = cp_parser_expression (parser);
11170 finish_for_expr (expression, stmt);
11172 return stmt;
11175 /* Tries to parse a range-based for-statement:
11177 range-based-for:
11178 decl-specifier-seq declarator : expression
11180 The decl-specifier-seq declarator and the `:' are already parsed by
11181 cp_parser_for_init_statement. If processing_template_decl it returns a
11182 newly created RANGE_FOR_STMT; if not, it is converted to a
11183 regular FOR_STMT. */
11185 static tree
11186 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11187 bool ivdep)
11189 tree stmt, range_expr;
11191 /* Get the range declaration momentarily out of the way so that
11192 the range expression doesn't clash with it. */
11193 if (range_decl != error_mark_node)
11194 pop_binding (DECL_NAME (range_decl), range_decl);
11196 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11198 bool expr_non_constant_p;
11199 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11201 else
11202 range_expr = cp_parser_expression (parser);
11204 /* Put the range declaration back into scope. */
11205 if (range_decl != error_mark_node)
11206 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11208 /* If in template, STMT is converted to a normal for-statement
11209 at instantiation. If not, it is done just ahead. */
11210 if (processing_template_decl)
11212 if (check_for_bare_parameter_packs (range_expr))
11213 range_expr = error_mark_node;
11214 stmt = begin_range_for_stmt (scope, init);
11215 if (ivdep)
11216 RANGE_FOR_IVDEP (stmt) = 1;
11217 finish_range_for_decl (stmt, range_decl, range_expr);
11218 if (!type_dependent_expression_p (range_expr)
11219 /* do_auto_deduction doesn't mess with template init-lists. */
11220 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11221 do_range_for_auto_deduction (range_decl, range_expr);
11223 else
11225 stmt = begin_for_stmt (scope, init);
11226 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11228 return stmt;
11231 /* Subroutine of cp_convert_range_for: given the initializer expression,
11232 builds up the range temporary. */
11234 static tree
11235 build_range_temp (tree range_expr)
11237 tree range_type, range_temp;
11239 /* Find out the type deduced by the declaration
11240 `auto &&__range = range_expr'. */
11241 range_type = cp_build_reference_type (make_auto (), true);
11242 range_type = do_auto_deduction (range_type, range_expr,
11243 type_uses_auto (range_type));
11245 /* Create the __range variable. */
11246 range_temp = build_decl (input_location, VAR_DECL,
11247 get_identifier ("__for_range"), range_type);
11248 TREE_USED (range_temp) = 1;
11249 DECL_ARTIFICIAL (range_temp) = 1;
11251 return range_temp;
11254 /* Used by cp_parser_range_for in template context: we aren't going to
11255 do a full conversion yet, but we still need to resolve auto in the
11256 type of the for-range-declaration if present. This is basically
11257 a shortcut version of cp_convert_range_for. */
11259 static void
11260 do_range_for_auto_deduction (tree decl, tree range_expr)
11262 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11263 if (auto_node)
11265 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11266 range_temp = convert_from_reference (build_range_temp (range_expr));
11267 iter_type = (cp_parser_perform_range_for_lookup
11268 (range_temp, &begin_dummy, &end_dummy));
11269 if (iter_type)
11271 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11272 iter_type);
11273 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11274 tf_warning_or_error);
11275 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11276 iter_decl, auto_node);
11281 /* Converts a range-based for-statement into a normal
11282 for-statement, as per the definition.
11284 for (RANGE_DECL : RANGE_EXPR)
11285 BLOCK
11287 should be equivalent to:
11290 auto &&__range = RANGE_EXPR;
11291 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11292 __begin != __end;
11293 ++__begin)
11295 RANGE_DECL = *__begin;
11296 BLOCK
11300 If RANGE_EXPR is an array:
11301 BEGIN_EXPR = __range
11302 END_EXPR = __range + ARRAY_SIZE(__range)
11303 Else if RANGE_EXPR has a member 'begin' or 'end':
11304 BEGIN_EXPR = __range.begin()
11305 END_EXPR = __range.end()
11306 Else:
11307 BEGIN_EXPR = begin(__range)
11308 END_EXPR = end(__range);
11310 If __range has a member 'begin' but not 'end', or vice versa, we must
11311 still use the second alternative (it will surely fail, however).
11312 When calling begin()/end() in the third alternative we must use
11313 argument dependent lookup, but always considering 'std' as an associated
11314 namespace. */
11316 tree
11317 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11318 bool ivdep)
11320 tree begin, end;
11321 tree iter_type, begin_expr, end_expr;
11322 tree condition, expression;
11324 if (range_decl == error_mark_node || range_expr == error_mark_node)
11325 /* If an error happened previously do nothing or else a lot of
11326 unhelpful errors would be issued. */
11327 begin_expr = end_expr = iter_type = error_mark_node;
11328 else
11330 tree range_temp;
11332 if (VAR_P (range_expr)
11333 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11334 /* Can't bind a reference to an array of runtime bound. */
11335 range_temp = range_expr;
11336 else
11338 range_temp = build_range_temp (range_expr);
11339 pushdecl (range_temp);
11340 cp_finish_decl (range_temp, range_expr,
11341 /*is_constant_init*/false, NULL_TREE,
11342 LOOKUP_ONLYCONVERTING);
11343 range_temp = convert_from_reference (range_temp);
11345 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11346 &begin_expr, &end_expr);
11349 /* The new for initialization statement. */
11350 begin = build_decl (input_location, VAR_DECL,
11351 get_identifier ("__for_begin"), iter_type);
11352 TREE_USED (begin) = 1;
11353 DECL_ARTIFICIAL (begin) = 1;
11354 pushdecl (begin);
11355 cp_finish_decl (begin, begin_expr,
11356 /*is_constant_init*/false, NULL_TREE,
11357 LOOKUP_ONLYCONVERTING);
11359 if (cxx_dialect >= cxx1z)
11360 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11361 end = build_decl (input_location, VAR_DECL,
11362 get_identifier ("__for_end"), iter_type);
11363 TREE_USED (end) = 1;
11364 DECL_ARTIFICIAL (end) = 1;
11365 pushdecl (end);
11366 cp_finish_decl (end, end_expr,
11367 /*is_constant_init*/false, NULL_TREE,
11368 LOOKUP_ONLYCONVERTING);
11370 finish_for_init_stmt (statement);
11372 /* The new for condition. */
11373 condition = build_x_binary_op (input_location, NE_EXPR,
11374 begin, ERROR_MARK,
11375 end, ERROR_MARK,
11376 NULL, tf_warning_or_error);
11377 finish_for_cond (condition, statement, ivdep);
11379 /* The new increment expression. */
11380 expression = finish_unary_op_expr (input_location,
11381 PREINCREMENT_EXPR, begin,
11382 tf_warning_or_error);
11383 finish_for_expr (expression, statement);
11385 /* The declaration is initialized with *__begin inside the loop body. */
11386 cp_finish_decl (range_decl,
11387 build_x_indirect_ref (input_location, begin, RO_NULL,
11388 tf_warning_or_error),
11389 /*is_constant_init*/false, NULL_TREE,
11390 LOOKUP_ONLYCONVERTING);
11392 return statement;
11395 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11396 We need to solve both at the same time because the method used
11397 depends on the existence of members begin or end.
11398 Returns the type deduced for the iterator expression. */
11400 static tree
11401 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11403 if (error_operand_p (range))
11405 *begin = *end = error_mark_node;
11406 return error_mark_node;
11409 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11411 error ("range-based %<for%> expression of type %qT "
11412 "has incomplete type", TREE_TYPE (range));
11413 *begin = *end = error_mark_node;
11414 return error_mark_node;
11416 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11418 /* If RANGE is an array, we will use pointer arithmetic. */
11419 *begin = decay_conversion (range, tf_warning_or_error);
11420 *end = build_binary_op (input_location, PLUS_EXPR,
11421 range,
11422 array_type_nelts_top (TREE_TYPE (range)),
11424 return TREE_TYPE (*begin);
11426 else
11428 /* If it is not an array, we must do a bit of magic. */
11429 tree id_begin, id_end;
11430 tree member_begin, member_end;
11432 *begin = *end = error_mark_node;
11434 id_begin = get_identifier ("begin");
11435 id_end = get_identifier ("end");
11436 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11437 /*protect=*/2, /*want_type=*/false,
11438 tf_warning_or_error);
11439 member_end = lookup_member (TREE_TYPE (range), id_end,
11440 /*protect=*/2, /*want_type=*/false,
11441 tf_warning_or_error);
11443 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11445 /* Use the member functions. */
11446 if (member_begin != NULL_TREE)
11447 *begin = cp_parser_range_for_member_function (range, id_begin);
11448 else
11449 error ("range-based %<for%> expression of type %qT has an "
11450 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11452 if (member_end != NULL_TREE)
11453 *end = cp_parser_range_for_member_function (range, id_end);
11454 else
11455 error ("range-based %<for%> expression of type %qT has a "
11456 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11458 else
11460 /* Use global functions with ADL. */
11461 vec<tree, va_gc> *vec;
11462 vec = make_tree_vector ();
11464 vec_safe_push (vec, range);
11466 member_begin = perform_koenig_lookup (id_begin, vec,
11467 tf_warning_or_error);
11468 *begin = finish_call_expr (member_begin, &vec, false, true,
11469 tf_warning_or_error);
11470 member_end = perform_koenig_lookup (id_end, vec,
11471 tf_warning_or_error);
11472 *end = finish_call_expr (member_end, &vec, false, true,
11473 tf_warning_or_error);
11475 release_tree_vector (vec);
11478 /* Last common checks. */
11479 if (*begin == error_mark_node || *end == error_mark_node)
11481 /* If one of the expressions is an error do no more checks. */
11482 *begin = *end = error_mark_node;
11483 return error_mark_node;
11485 else if (type_dependent_expression_p (*begin)
11486 || type_dependent_expression_p (*end))
11487 /* Can happen, when, eg, in a template context, Koenig lookup
11488 can't resolve begin/end (c++/58503). */
11489 return NULL_TREE;
11490 else
11492 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11493 /* The unqualified type of the __begin and __end temporaries should
11494 be the same, as required by the multiple auto declaration. */
11495 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11497 if (cxx_dialect >= cxx1z
11498 && (build_x_binary_op (input_location, NE_EXPR,
11499 *begin, ERROR_MARK,
11500 *end, ERROR_MARK,
11501 NULL, tf_none)
11502 != error_mark_node))
11503 /* P0184R0 allows __begin and __end to have different types,
11504 but make sure they are comparable so we can give a better
11505 diagnostic. */;
11506 else
11507 error ("inconsistent begin/end types in range-based %<for%> "
11508 "statement: %qT and %qT",
11509 TREE_TYPE (*begin), TREE_TYPE (*end));
11511 return iter_type;
11516 /* Helper function for cp_parser_perform_range_for_lookup.
11517 Builds a tree for RANGE.IDENTIFIER(). */
11519 static tree
11520 cp_parser_range_for_member_function (tree range, tree identifier)
11522 tree member, res;
11523 vec<tree, va_gc> *vec;
11525 member = finish_class_member_access_expr (range, identifier,
11526 false, tf_warning_or_error);
11527 if (member == error_mark_node)
11528 return error_mark_node;
11530 vec = make_tree_vector ();
11531 res = finish_call_expr (member, &vec,
11532 /*disallow_virtual=*/false,
11533 /*koenig_p=*/false,
11534 tf_warning_or_error);
11535 release_tree_vector (vec);
11536 return res;
11539 /* Parse an iteration-statement.
11541 iteration-statement:
11542 while ( condition ) statement
11543 do statement while ( expression ) ;
11544 for ( for-init-statement condition [opt] ; expression [opt] )
11545 statement
11547 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11549 static tree
11550 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11552 cp_token *token;
11553 enum rid keyword;
11554 tree statement;
11555 unsigned char in_statement;
11556 token_indent_info guard_tinfo;
11558 /* Peek at the next token. */
11559 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11560 if (!token)
11561 return error_mark_node;
11563 guard_tinfo = get_token_indent_info (token);
11565 /* Remember whether or not we are already within an iteration
11566 statement. */
11567 in_statement = parser->in_statement;
11569 /* See what kind of keyword it is. */
11570 keyword = token->keyword;
11571 switch (keyword)
11573 case RID_WHILE:
11575 tree condition;
11577 /* Begin the while-statement. */
11578 statement = begin_while_stmt ();
11579 /* Look for the `('. */
11580 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11581 /* Parse the condition. */
11582 condition = cp_parser_condition (parser);
11583 finish_while_stmt_cond (condition, statement, ivdep);
11584 /* Look for the `)'. */
11585 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11586 /* Parse the dependent statement. */
11587 parser->in_statement = IN_ITERATION_STMT;
11588 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11589 parser->in_statement = in_statement;
11590 /* We're done with the while-statement. */
11591 finish_while_stmt (statement);
11593 break;
11595 case RID_DO:
11597 tree expression;
11599 /* Begin the do-statement. */
11600 statement = begin_do_stmt ();
11601 /* Parse the body of the do-statement. */
11602 parser->in_statement = IN_ITERATION_STMT;
11603 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11604 parser->in_statement = in_statement;
11605 finish_do_body (statement);
11606 /* Look for the `while' keyword. */
11607 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11608 /* Look for the `('. */
11609 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11610 /* Parse the expression. */
11611 expression = cp_parser_expression (parser);
11612 /* We're done with the do-statement. */
11613 finish_do_stmt (expression, statement, ivdep);
11614 /* Look for the `)'. */
11615 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11616 /* Look for the `;'. */
11617 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11619 break;
11621 case RID_FOR:
11623 /* Look for the `('. */
11624 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11626 statement = cp_parser_for (parser, ivdep);
11628 /* Look for the `)'. */
11629 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11631 /* Parse the body of the for-statement. */
11632 parser->in_statement = IN_ITERATION_STMT;
11633 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11634 parser->in_statement = in_statement;
11636 /* We're done with the for-statement. */
11637 finish_for_stmt (statement);
11639 break;
11641 default:
11642 cp_parser_error (parser, "expected iteration-statement");
11643 statement = error_mark_node;
11644 break;
11647 return statement;
11650 /* Parse a for-init-statement or the declarator of a range-based-for.
11651 Returns true if a range-based-for declaration is seen.
11653 for-init-statement:
11654 expression-statement
11655 simple-declaration */
11657 static bool
11658 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11660 /* If the next token is a `;', then we have an empty
11661 expression-statement. Grammatically, this is also a
11662 simple-declaration, but an invalid one, because it does not
11663 declare anything. Therefore, if we did not handle this case
11664 specially, we would issue an error message about an invalid
11665 declaration. */
11666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11668 bool is_range_for = false;
11669 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11671 /* A colon is used in range-based for. */
11672 parser->colon_corrects_to_scope_p = false;
11674 /* We're going to speculatively look for a declaration, falling back
11675 to an expression, if necessary. */
11676 cp_parser_parse_tentatively (parser);
11677 /* Parse the declaration. */
11678 cp_parser_simple_declaration (parser,
11679 /*function_definition_allowed_p=*/false,
11680 decl);
11681 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11682 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11684 /* It is a range-for, consume the ':' */
11685 cp_lexer_consume_token (parser->lexer);
11686 is_range_for = true;
11687 if (cxx_dialect < cxx11)
11689 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11690 "range-based %<for%> loops only available with "
11691 "-std=c++11 or -std=gnu++11");
11692 *decl = error_mark_node;
11695 else
11696 /* The ';' is not consumed yet because we told
11697 cp_parser_simple_declaration not to. */
11698 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11700 if (cp_parser_parse_definitely (parser))
11701 return is_range_for;
11702 /* If the tentative parse failed, then we shall need to look for an
11703 expression-statement. */
11705 /* If we are here, it is an expression-statement. */
11706 cp_parser_expression_statement (parser, NULL_TREE);
11707 return false;
11710 /* Parse a jump-statement.
11712 jump-statement:
11713 break ;
11714 continue ;
11715 return expression [opt] ;
11716 return braced-init-list ;
11717 goto identifier ;
11719 GNU extension:
11721 jump-statement:
11722 goto * expression ;
11724 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11726 static tree
11727 cp_parser_jump_statement (cp_parser* parser)
11729 tree statement = error_mark_node;
11730 cp_token *token;
11731 enum rid keyword;
11732 unsigned char in_statement;
11734 /* Peek at the next token. */
11735 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11736 if (!token)
11737 return error_mark_node;
11739 /* See what kind of keyword it is. */
11740 keyword = token->keyword;
11741 switch (keyword)
11743 case RID_BREAK:
11744 in_statement = parser->in_statement & ~IN_IF_STMT;
11745 switch (in_statement)
11747 case 0:
11748 error_at (token->location, "break statement not within loop or switch");
11749 break;
11750 default:
11751 gcc_assert ((in_statement & IN_SWITCH_STMT)
11752 || in_statement == IN_ITERATION_STMT);
11753 statement = finish_break_stmt ();
11754 if (in_statement == IN_ITERATION_STMT)
11755 break_maybe_infinite_loop ();
11756 break;
11757 case IN_OMP_BLOCK:
11758 error_at (token->location, "invalid exit from OpenMP structured block");
11759 break;
11760 case IN_OMP_FOR:
11761 error_at (token->location, "break statement used with OpenMP for loop");
11762 break;
11763 case IN_CILK_SIMD_FOR:
11764 error_at (token->location, "break statement used with Cilk Plus for loop");
11765 break;
11767 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11768 break;
11770 case RID_CONTINUE:
11771 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11773 case 0:
11774 error_at (token->location, "continue statement not within a loop");
11775 break;
11776 case IN_CILK_SIMD_FOR:
11777 error_at (token->location,
11778 "continue statement within %<#pragma simd%> loop body");
11779 /* Fall through. */
11780 case IN_ITERATION_STMT:
11781 case IN_OMP_FOR:
11782 statement = finish_continue_stmt ();
11783 break;
11784 case IN_OMP_BLOCK:
11785 error_at (token->location, "invalid exit from OpenMP structured block");
11786 break;
11787 default:
11788 gcc_unreachable ();
11790 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11791 break;
11793 case RID_RETURN:
11795 tree expr;
11796 bool expr_non_constant_p;
11798 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11800 cp_lexer_set_source_position (parser->lexer);
11801 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11802 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11804 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11805 expr = cp_parser_expression (parser);
11806 else
11807 /* If the next token is a `;', then there is no
11808 expression. */
11809 expr = NULL_TREE;
11810 /* Build the return-statement. */
11811 statement = finish_return_stmt (expr);
11812 /* Look for the final `;'. */
11813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11815 break;
11817 case RID_GOTO:
11818 if (parser->in_function_body
11819 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11821 error ("%<goto%> in %<constexpr%> function");
11822 cp_function_chain->invalid_constexpr = true;
11825 /* Create the goto-statement. */
11826 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11828 /* Issue a warning about this use of a GNU extension. */
11829 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11830 /* Consume the '*' token. */
11831 cp_lexer_consume_token (parser->lexer);
11832 /* Parse the dependent expression. */
11833 finish_goto_stmt (cp_parser_expression (parser));
11835 else
11836 finish_goto_stmt (cp_parser_identifier (parser));
11837 /* Look for the final `;'. */
11838 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11839 break;
11841 default:
11842 cp_parser_error (parser, "expected jump-statement");
11843 break;
11846 return statement;
11849 /* Parse a declaration-statement.
11851 declaration-statement:
11852 block-declaration */
11854 static void
11855 cp_parser_declaration_statement (cp_parser* parser)
11857 void *p;
11859 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11860 p = obstack_alloc (&declarator_obstack, 0);
11862 /* Parse the block-declaration. */
11863 cp_parser_block_declaration (parser, /*statement_p=*/true);
11865 /* Free any declarators allocated. */
11866 obstack_free (&declarator_obstack, p);
11869 /* Some dependent statements (like `if (cond) statement'), are
11870 implicitly in their own scope. In other words, if the statement is
11871 a single statement (as opposed to a compound-statement), it is
11872 none-the-less treated as if it were enclosed in braces. Any
11873 declarations appearing in the dependent statement are out of scope
11874 after control passes that point. This function parses a statement,
11875 but ensures that is in its own scope, even if it is not a
11876 compound-statement.
11878 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11879 is a (possibly labeled) if statement which is not enclosed in
11880 braces and has an else clause. This is used to implement
11881 -Wparentheses.
11883 CHAIN is a vector of if-else-if conditions. This is used to implement
11884 -Wduplicated-cond.
11886 Returns the new statement. */
11888 static tree
11889 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11890 const token_indent_info &guard_tinfo,
11891 vec<tree> *chain)
11893 tree statement;
11894 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11895 token_indent_info body_tinfo
11896 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11898 if (if_p != NULL)
11899 *if_p = false;
11901 /* Mark if () ; with a special NOP_EXPR. */
11902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11904 cp_lexer_consume_token (parser->lexer);
11905 statement = add_stmt (build_empty_stmt (body_loc));
11907 if (guard_tinfo.keyword == RID_IF
11908 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11909 warning_at (body_loc, OPT_Wempty_body,
11910 "suggest braces around empty body in an %<if%> statement");
11911 else if (guard_tinfo.keyword == RID_ELSE)
11912 warning_at (body_loc, OPT_Wempty_body,
11913 "suggest braces around empty body in an %<else%> statement");
11915 /* if a compound is opened, we simply parse the statement directly. */
11916 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11917 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11918 /* If the token is not a `{', then we must take special action. */
11919 else
11921 /* Create a compound-statement. */
11922 statement = begin_compound_stmt (0);
11923 /* Parse the dependent-statement. */
11924 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11925 /* Finish the dummy compound-statement. */
11926 finish_compound_stmt (statement);
11929 token_indent_info next_tinfo
11930 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11931 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11933 /* Return the statement. */
11934 return statement;
11937 /* For some dependent statements (like `while (cond) statement'), we
11938 have already created a scope. Therefore, even if the dependent
11939 statement is a compound-statement, we do not want to create another
11940 scope. */
11942 static void
11943 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11944 const token_indent_info &guard_tinfo)
11946 /* If the token is a `{', then we must take special action. */
11947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11949 token_indent_info body_tinfo
11950 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11952 cp_parser_statement (parser, NULL_TREE, false, if_p);
11953 token_indent_info next_tinfo
11954 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11955 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11957 else
11959 /* Avoid calling cp_parser_compound_statement, so that we
11960 don't create a new scope. Do everything else by hand. */
11961 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11962 /* If the next keyword is `__label__' we have a label declaration. */
11963 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11964 cp_parser_label_declaration (parser);
11965 /* Parse an (optional) statement-seq. */
11966 cp_parser_statement_seq_opt (parser, NULL_TREE);
11967 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11971 /* Declarations [gram.dcl.dcl] */
11973 /* Parse an optional declaration-sequence.
11975 declaration-seq:
11976 declaration
11977 declaration-seq declaration */
11979 static void
11980 cp_parser_declaration_seq_opt (cp_parser* parser)
11982 while (true)
11984 cp_token *token;
11986 token = cp_lexer_peek_token (parser->lexer);
11988 if (token->type == CPP_CLOSE_BRACE
11989 || token->type == CPP_EOF
11990 || token->type == CPP_PRAGMA_EOL)
11991 break;
11993 if (token->type == CPP_SEMICOLON)
11995 /* A declaration consisting of a single semicolon is
11996 invalid. Allow it unless we're being pedantic. */
11997 cp_lexer_consume_token (parser->lexer);
11998 if (!in_system_header_at (input_location))
11999 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12000 continue;
12003 /* If we're entering or exiting a region that's implicitly
12004 extern "C", modify the lang context appropriately. */
12005 if (!parser->implicit_extern_c && token->implicit_extern_c)
12007 push_lang_context (lang_name_c);
12008 parser->implicit_extern_c = true;
12010 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12012 pop_lang_context ();
12013 parser->implicit_extern_c = false;
12016 if (token->type == CPP_PRAGMA)
12018 /* A top-level declaration can consist solely of a #pragma.
12019 A nested declaration cannot, so this is done here and not
12020 in cp_parser_declaration. (A #pragma at block scope is
12021 handled in cp_parser_statement.) */
12022 cp_parser_pragma (parser, pragma_external, NULL);
12023 continue;
12026 /* Parse the declaration itself. */
12027 cp_parser_declaration (parser);
12031 /* Parse a declaration.
12033 declaration:
12034 block-declaration
12035 function-definition
12036 template-declaration
12037 explicit-instantiation
12038 explicit-specialization
12039 linkage-specification
12040 namespace-definition
12042 GNU extension:
12044 declaration:
12045 __extension__ declaration */
12047 static void
12048 cp_parser_declaration (cp_parser* parser)
12050 cp_token token1;
12051 cp_token token2;
12052 int saved_pedantic;
12053 void *p;
12054 tree attributes = NULL_TREE;
12056 /* Check for the `__extension__' keyword. */
12057 if (cp_parser_extension_opt (parser, &saved_pedantic))
12059 /* Parse the qualified declaration. */
12060 cp_parser_declaration (parser);
12061 /* Restore the PEDANTIC flag. */
12062 pedantic = saved_pedantic;
12064 return;
12067 /* Try to figure out what kind of declaration is present. */
12068 token1 = *cp_lexer_peek_token (parser->lexer);
12070 if (token1.type != CPP_EOF)
12071 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12072 else
12074 token2.type = CPP_EOF;
12075 token2.keyword = RID_MAX;
12078 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12079 p = obstack_alloc (&declarator_obstack, 0);
12081 /* If the next token is `extern' and the following token is a string
12082 literal, then we have a linkage specification. */
12083 if (token1.keyword == RID_EXTERN
12084 && cp_parser_is_pure_string_literal (&token2))
12085 cp_parser_linkage_specification (parser);
12086 /* If the next token is `template', then we have either a template
12087 declaration, an explicit instantiation, or an explicit
12088 specialization. */
12089 else if (token1.keyword == RID_TEMPLATE)
12091 /* `template <>' indicates a template specialization. */
12092 if (token2.type == CPP_LESS
12093 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12094 cp_parser_explicit_specialization (parser);
12095 /* `template <' indicates a template declaration. */
12096 else if (token2.type == CPP_LESS)
12097 cp_parser_template_declaration (parser, /*member_p=*/false);
12098 /* Anything else must be an explicit instantiation. */
12099 else
12100 cp_parser_explicit_instantiation (parser);
12102 /* If the next token is `export', then we have a template
12103 declaration. */
12104 else if (token1.keyword == RID_EXPORT)
12105 cp_parser_template_declaration (parser, /*member_p=*/false);
12106 /* If the next token is `extern', 'static' or 'inline' and the one
12107 after that is `template', we have a GNU extended explicit
12108 instantiation directive. */
12109 else if (cp_parser_allow_gnu_extensions_p (parser)
12110 && (token1.keyword == RID_EXTERN
12111 || token1.keyword == RID_STATIC
12112 || token1.keyword == RID_INLINE)
12113 && token2.keyword == RID_TEMPLATE)
12114 cp_parser_explicit_instantiation (parser);
12115 /* If the next token is `namespace', check for a named or unnamed
12116 namespace definition. */
12117 else if (token1.keyword == RID_NAMESPACE
12118 && (/* A named namespace definition. */
12119 (token2.type == CPP_NAME
12120 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12121 != CPP_EQ))
12122 || (token2.type == CPP_OPEN_SQUARE
12123 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12124 == CPP_OPEN_SQUARE)
12125 /* An unnamed namespace definition. */
12126 || token2.type == CPP_OPEN_BRACE
12127 || token2.keyword == RID_ATTRIBUTE))
12128 cp_parser_namespace_definition (parser);
12129 /* An inline (associated) namespace definition. */
12130 else if (token1.keyword == RID_INLINE
12131 && token2.keyword == RID_NAMESPACE)
12132 cp_parser_namespace_definition (parser);
12133 /* Objective-C++ declaration/definition. */
12134 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12135 cp_parser_objc_declaration (parser, NULL_TREE);
12136 else if (c_dialect_objc ()
12137 && token1.keyword == RID_ATTRIBUTE
12138 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12139 cp_parser_objc_declaration (parser, attributes);
12140 /* At this point we may have a template declared by a concept
12141 introduction. */
12142 else if (flag_concepts
12143 && cp_parser_template_declaration_after_export (parser,
12144 /*member_p=*/false))
12145 /* We did. */;
12146 else
12147 /* Try to parse a block-declaration, or a function-definition. */
12148 cp_parser_block_declaration (parser, /*statement_p=*/false);
12150 /* Free any declarators allocated. */
12151 obstack_free (&declarator_obstack, p);
12154 /* Parse a block-declaration.
12156 block-declaration:
12157 simple-declaration
12158 asm-definition
12159 namespace-alias-definition
12160 using-declaration
12161 using-directive
12163 GNU Extension:
12165 block-declaration:
12166 __extension__ block-declaration
12168 C++0x Extension:
12170 block-declaration:
12171 static_assert-declaration
12173 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12174 part of a declaration-statement. */
12176 static void
12177 cp_parser_block_declaration (cp_parser *parser,
12178 bool statement_p)
12180 cp_token *token1;
12181 int saved_pedantic;
12183 /* Check for the `__extension__' keyword. */
12184 if (cp_parser_extension_opt (parser, &saved_pedantic))
12186 /* Parse the qualified declaration. */
12187 cp_parser_block_declaration (parser, statement_p);
12188 /* Restore the PEDANTIC flag. */
12189 pedantic = saved_pedantic;
12191 return;
12194 /* Peek at the next token to figure out which kind of declaration is
12195 present. */
12196 token1 = cp_lexer_peek_token (parser->lexer);
12198 /* If the next keyword is `asm', we have an asm-definition. */
12199 if (token1->keyword == RID_ASM)
12201 if (statement_p)
12202 cp_parser_commit_to_tentative_parse (parser);
12203 cp_parser_asm_definition (parser);
12205 /* If the next keyword is `namespace', we have a
12206 namespace-alias-definition. */
12207 else if (token1->keyword == RID_NAMESPACE)
12208 cp_parser_namespace_alias_definition (parser);
12209 /* If the next keyword is `using', we have a
12210 using-declaration, a using-directive, or an alias-declaration. */
12211 else if (token1->keyword == RID_USING)
12213 cp_token *token2;
12215 if (statement_p)
12216 cp_parser_commit_to_tentative_parse (parser);
12217 /* If the token after `using' is `namespace', then we have a
12218 using-directive. */
12219 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12220 if (token2->keyword == RID_NAMESPACE)
12221 cp_parser_using_directive (parser);
12222 /* If the second token after 'using' is '=', then we have an
12223 alias-declaration. */
12224 else if (cxx_dialect >= cxx11
12225 && token2->type == CPP_NAME
12226 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12227 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12228 cp_parser_alias_declaration (parser);
12229 /* Otherwise, it's a using-declaration. */
12230 else
12231 cp_parser_using_declaration (parser,
12232 /*access_declaration_p=*/false);
12234 /* If the next keyword is `__label__' we have a misplaced label
12235 declaration. */
12236 else if (token1->keyword == RID_LABEL)
12238 cp_lexer_consume_token (parser->lexer);
12239 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12240 cp_parser_skip_to_end_of_statement (parser);
12241 /* If the next token is now a `;', consume it. */
12242 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12243 cp_lexer_consume_token (parser->lexer);
12245 /* If the next token is `static_assert' we have a static assertion. */
12246 else if (token1->keyword == RID_STATIC_ASSERT)
12247 cp_parser_static_assert (parser, /*member_p=*/false);
12248 /* Anything else must be a simple-declaration. */
12249 else
12250 cp_parser_simple_declaration (parser, !statement_p,
12251 /*maybe_range_for_decl*/NULL);
12254 /* Parse a simple-declaration.
12256 simple-declaration:
12257 decl-specifier-seq [opt] init-declarator-list [opt] ;
12259 init-declarator-list:
12260 init-declarator
12261 init-declarator-list , init-declarator
12263 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12264 function-definition as a simple-declaration.
12266 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12267 parsed declaration if it is an uninitialized single declarator not followed
12268 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12269 if present, will not be consumed. */
12271 static void
12272 cp_parser_simple_declaration (cp_parser* parser,
12273 bool function_definition_allowed_p,
12274 tree *maybe_range_for_decl)
12276 cp_decl_specifier_seq decl_specifiers;
12277 int declares_class_or_enum;
12278 bool saw_declarator;
12279 location_t comma_loc = UNKNOWN_LOCATION;
12280 location_t init_loc = UNKNOWN_LOCATION;
12282 if (maybe_range_for_decl)
12283 *maybe_range_for_decl = NULL_TREE;
12285 /* Defer access checks until we know what is being declared; the
12286 checks for names appearing in the decl-specifier-seq should be
12287 done as if we were in the scope of the thing being declared. */
12288 push_deferring_access_checks (dk_deferred);
12290 /* Parse the decl-specifier-seq. We have to keep track of whether
12291 or not the decl-specifier-seq declares a named class or
12292 enumeration type, since that is the only case in which the
12293 init-declarator-list is allowed to be empty.
12295 [dcl.dcl]
12297 In a simple-declaration, the optional init-declarator-list can be
12298 omitted only when declaring a class or enumeration, that is when
12299 the decl-specifier-seq contains either a class-specifier, an
12300 elaborated-type-specifier, or an enum-specifier. */
12301 cp_parser_decl_specifier_seq (parser,
12302 CP_PARSER_FLAGS_OPTIONAL,
12303 &decl_specifiers,
12304 &declares_class_or_enum);
12305 /* We no longer need to defer access checks. */
12306 stop_deferring_access_checks ();
12308 /* In a block scope, a valid declaration must always have a
12309 decl-specifier-seq. By not trying to parse declarators, we can
12310 resolve the declaration/expression ambiguity more quickly. */
12311 if (!function_definition_allowed_p
12312 && !decl_specifiers.any_specifiers_p)
12314 cp_parser_error (parser, "expected declaration");
12315 goto done;
12318 /* If the next two tokens are both identifiers, the code is
12319 erroneous. The usual cause of this situation is code like:
12321 T t;
12323 where "T" should name a type -- but does not. */
12324 if (!decl_specifiers.any_type_specifiers_p
12325 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12327 /* If parsing tentatively, we should commit; we really are
12328 looking at a declaration. */
12329 cp_parser_commit_to_tentative_parse (parser);
12330 /* Give up. */
12331 goto done;
12334 /* If we have seen at least one decl-specifier, and the next token
12335 is not a parenthesis, then we must be looking at a declaration.
12336 (After "int (" we might be looking at a functional cast.) */
12337 if (decl_specifiers.any_specifiers_p
12338 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12339 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12340 && !cp_parser_error_occurred (parser))
12341 cp_parser_commit_to_tentative_parse (parser);
12343 tree last_type;
12345 last_type = NULL_TREE;
12347 /* Keep going until we hit the `;' at the end of the simple
12348 declaration. */
12349 saw_declarator = false;
12350 while (cp_lexer_next_token_is_not (parser->lexer,
12351 CPP_SEMICOLON))
12353 cp_token *token;
12354 bool function_definition_p;
12355 tree decl;
12356 tree auto_result = NULL_TREE;
12358 if (saw_declarator)
12360 /* If we are processing next declarator, comma is expected */
12361 token = cp_lexer_peek_token (parser->lexer);
12362 gcc_assert (token->type == CPP_COMMA);
12363 cp_lexer_consume_token (parser->lexer);
12364 if (maybe_range_for_decl)
12366 *maybe_range_for_decl = error_mark_node;
12367 if (comma_loc == UNKNOWN_LOCATION)
12368 comma_loc = token->location;
12371 else
12372 saw_declarator = true;
12374 /* Parse the init-declarator. */
12375 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12376 /*checks=*/NULL,
12377 function_definition_allowed_p,
12378 /*member_p=*/false,
12379 declares_class_or_enum,
12380 &function_definition_p,
12381 maybe_range_for_decl,
12382 &init_loc,
12383 &auto_result);
12384 /* If an error occurred while parsing tentatively, exit quickly.
12385 (That usually happens when in the body of a function; each
12386 statement is treated as a declaration-statement until proven
12387 otherwise.) */
12388 if (cp_parser_error_occurred (parser))
12389 goto done;
12391 if (auto_result)
12393 if (last_type && last_type != error_mark_node
12394 && !same_type_p (auto_result, last_type))
12396 /* If the list of declarators contains more than one declarator,
12397 the type of each declared variable is determined as described
12398 above. If the type deduced for the template parameter U is not
12399 the same in each deduction, the program is ill-formed. */
12400 error_at (decl_specifiers.locations[ds_type_spec],
12401 "inconsistent deduction for %qT: %qT and then %qT",
12402 decl_specifiers.type, last_type, auto_result);
12403 last_type = error_mark_node;
12405 else
12406 last_type = auto_result;
12409 /* Handle function definitions specially. */
12410 if (function_definition_p)
12412 /* If the next token is a `,', then we are probably
12413 processing something like:
12415 void f() {}, *p;
12417 which is erroneous. */
12418 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12420 cp_token *token = cp_lexer_peek_token (parser->lexer);
12421 error_at (token->location,
12422 "mixing"
12423 " declarations and function-definitions is forbidden");
12425 /* Otherwise, we're done with the list of declarators. */
12426 else
12428 pop_deferring_access_checks ();
12429 return;
12432 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12433 *maybe_range_for_decl = decl;
12434 /* The next token should be either a `,' or a `;'. */
12435 token = cp_lexer_peek_token (parser->lexer);
12436 /* If it's a `,', there are more declarators to come. */
12437 if (token->type == CPP_COMMA)
12438 /* will be consumed next time around */;
12439 /* If it's a `;', we are done. */
12440 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12441 break;
12442 /* Anything else is an error. */
12443 else
12445 /* If we have already issued an error message we don't need
12446 to issue another one. */
12447 if ((decl != error_mark_node
12448 && DECL_INITIAL (decl) != error_mark_node)
12449 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12450 cp_parser_error (parser, "expected %<,%> or %<;%>");
12451 /* Skip tokens until we reach the end of the statement. */
12452 cp_parser_skip_to_end_of_statement (parser);
12453 /* If the next token is now a `;', consume it. */
12454 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12455 cp_lexer_consume_token (parser->lexer);
12456 goto done;
12458 /* After the first time around, a function-definition is not
12459 allowed -- even if it was OK at first. For example:
12461 int i, f() {}
12463 is not valid. */
12464 function_definition_allowed_p = false;
12467 /* Issue an error message if no declarators are present, and the
12468 decl-specifier-seq does not itself declare a class or
12469 enumeration: [dcl.dcl]/3. */
12470 if (!saw_declarator)
12472 if (cp_parser_declares_only_class_p (parser))
12474 if (!declares_class_or_enum
12475 && decl_specifiers.type
12476 && OVERLOAD_TYPE_P (decl_specifiers.type))
12477 /* Ensure an error is issued anyway when finish_decltype_type,
12478 called via cp_parser_decl_specifier_seq, returns a class or
12479 an enumeration (c++/51786). */
12480 decl_specifiers.type = NULL_TREE;
12481 shadow_tag (&decl_specifiers);
12483 /* Perform any deferred access checks. */
12484 perform_deferred_access_checks (tf_warning_or_error);
12487 /* Consume the `;'. */
12488 if (!maybe_range_for_decl)
12489 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12490 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12492 if (init_loc != UNKNOWN_LOCATION)
12493 error_at (init_loc, "initializer in range-based %<for%> loop");
12494 if (comma_loc != UNKNOWN_LOCATION)
12495 error_at (comma_loc,
12496 "multiple declarations in range-based %<for%> loop");
12499 done:
12500 pop_deferring_access_checks ();
12503 /* Parse a decl-specifier-seq.
12505 decl-specifier-seq:
12506 decl-specifier-seq [opt] decl-specifier
12507 decl-specifier attribute-specifier-seq [opt] (C++11)
12509 decl-specifier:
12510 storage-class-specifier
12511 type-specifier
12512 function-specifier
12513 friend
12514 typedef
12516 GNU Extension:
12518 decl-specifier:
12519 attributes
12521 Concepts Extension:
12523 decl-specifier:
12524 concept
12526 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12528 The parser flags FLAGS is used to control type-specifier parsing.
12530 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12531 flags:
12533 1: one of the decl-specifiers is an elaborated-type-specifier
12534 (i.e., a type declaration)
12535 2: one of the decl-specifiers is an enum-specifier or a
12536 class-specifier (i.e., a type definition)
12540 static void
12541 cp_parser_decl_specifier_seq (cp_parser* parser,
12542 cp_parser_flags flags,
12543 cp_decl_specifier_seq *decl_specs,
12544 int* declares_class_or_enum)
12546 bool constructor_possible_p = !parser->in_declarator_p;
12547 bool found_decl_spec = false;
12548 cp_token *start_token = NULL;
12549 cp_decl_spec ds;
12551 /* Clear DECL_SPECS. */
12552 clear_decl_specs (decl_specs);
12554 /* Assume no class or enumeration type is declared. */
12555 *declares_class_or_enum = 0;
12557 /* Keep reading specifiers until there are no more to read. */
12558 while (true)
12560 bool constructor_p;
12561 cp_token *token;
12562 ds = ds_last;
12564 /* Peek at the next token. */
12565 token = cp_lexer_peek_token (parser->lexer);
12567 /* Save the first token of the decl spec list for error
12568 reporting. */
12569 if (!start_token)
12570 start_token = token;
12571 /* Handle attributes. */
12572 if (cp_next_tokens_can_be_attribute_p (parser))
12574 /* Parse the attributes. */
12575 tree attrs = cp_parser_attributes_opt (parser);
12577 /* In a sequence of declaration specifiers, c++11 attributes
12578 appertain to the type that precede them. In that case
12579 [dcl.spec]/1 says:
12581 The attribute-specifier-seq affects the type only for
12582 the declaration it appears in, not other declarations
12583 involving the same type.
12585 But for now let's force the user to position the
12586 attribute either at the beginning of the declaration or
12587 after the declarator-id, which would clearly mean that it
12588 applies to the declarator. */
12589 if (cxx11_attribute_p (attrs))
12591 if (!found_decl_spec)
12592 /* The c++11 attribute is at the beginning of the
12593 declaration. It appertains to the entity being
12594 declared. */;
12595 else
12597 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12599 /* This is an attribute following a
12600 class-specifier. */
12601 if (decl_specs->type_definition_p)
12602 warn_misplaced_attr_for_class_type (token->location,
12603 decl_specs->type);
12604 attrs = NULL_TREE;
12606 else
12608 decl_specs->std_attributes
12609 = chainon (decl_specs->std_attributes,
12610 attrs);
12611 if (decl_specs->locations[ds_std_attribute] == 0)
12612 decl_specs->locations[ds_std_attribute] = token->location;
12614 continue;
12618 decl_specs->attributes
12619 = chainon (decl_specs->attributes,
12620 attrs);
12621 if (decl_specs->locations[ds_attribute] == 0)
12622 decl_specs->locations[ds_attribute] = token->location;
12623 continue;
12625 /* Assume we will find a decl-specifier keyword. */
12626 found_decl_spec = true;
12627 /* If the next token is an appropriate keyword, we can simply
12628 add it to the list. */
12629 switch (token->keyword)
12631 /* decl-specifier:
12632 friend
12633 constexpr */
12634 case RID_FRIEND:
12635 if (!at_class_scope_p ())
12637 error_at (token->location, "%<friend%> used outside of class");
12638 cp_lexer_purge_token (parser->lexer);
12640 else
12642 ds = ds_friend;
12643 /* Consume the token. */
12644 cp_lexer_consume_token (parser->lexer);
12646 break;
12648 case RID_CONSTEXPR:
12649 ds = ds_constexpr;
12650 cp_lexer_consume_token (parser->lexer);
12651 break;
12653 case RID_CONCEPT:
12654 ds = ds_concept;
12655 cp_lexer_consume_token (parser->lexer);
12656 break;
12658 /* function-specifier:
12659 inline
12660 virtual
12661 explicit */
12662 case RID_INLINE:
12663 case RID_VIRTUAL:
12664 case RID_EXPLICIT:
12665 cp_parser_function_specifier_opt (parser, decl_specs);
12666 break;
12668 /* decl-specifier:
12669 typedef */
12670 case RID_TYPEDEF:
12671 ds = ds_typedef;
12672 /* Consume the token. */
12673 cp_lexer_consume_token (parser->lexer);
12674 /* A constructor declarator cannot appear in a typedef. */
12675 constructor_possible_p = false;
12676 /* The "typedef" keyword can only occur in a declaration; we
12677 may as well commit at this point. */
12678 cp_parser_commit_to_tentative_parse (parser);
12680 if (decl_specs->storage_class != sc_none)
12681 decl_specs->conflicting_specifiers_p = true;
12682 break;
12684 /* storage-class-specifier:
12685 auto
12686 register
12687 static
12688 extern
12689 mutable
12691 GNU Extension:
12692 thread */
12693 case RID_AUTO:
12694 if (cxx_dialect == cxx98)
12696 /* Consume the token. */
12697 cp_lexer_consume_token (parser->lexer);
12699 /* Complain about `auto' as a storage specifier, if
12700 we're complaining about C++0x compatibility. */
12701 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12702 " changes meaning in C++11; please remove it");
12704 /* Set the storage class anyway. */
12705 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12706 token);
12708 else
12709 /* C++0x auto type-specifier. */
12710 found_decl_spec = false;
12711 break;
12713 case RID_REGISTER:
12714 case RID_STATIC:
12715 case RID_EXTERN:
12716 case RID_MUTABLE:
12717 /* Consume the token. */
12718 cp_lexer_consume_token (parser->lexer);
12719 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12720 token);
12721 break;
12722 case RID_THREAD:
12723 /* Consume the token. */
12724 ds = ds_thread;
12725 cp_lexer_consume_token (parser->lexer);
12726 break;
12728 default:
12729 /* We did not yet find a decl-specifier yet. */
12730 found_decl_spec = false;
12731 break;
12734 if (found_decl_spec
12735 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12736 && token->keyword != RID_CONSTEXPR)
12737 error ("decl-specifier invalid in condition");
12739 if (ds != ds_last)
12740 set_and_check_decl_spec_loc (decl_specs, ds, token);
12742 /* Constructors are a special case. The `S' in `S()' is not a
12743 decl-specifier; it is the beginning of the declarator. */
12744 constructor_p
12745 = (!found_decl_spec
12746 && constructor_possible_p
12747 && (cp_parser_constructor_declarator_p
12748 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12750 /* If we don't have a DECL_SPEC yet, then we must be looking at
12751 a type-specifier. */
12752 if (!found_decl_spec && !constructor_p)
12754 int decl_spec_declares_class_or_enum;
12755 bool is_cv_qualifier;
12756 tree type_spec;
12758 type_spec
12759 = cp_parser_type_specifier (parser, flags,
12760 decl_specs,
12761 /*is_declaration=*/true,
12762 &decl_spec_declares_class_or_enum,
12763 &is_cv_qualifier);
12764 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12766 /* If this type-specifier referenced a user-defined type
12767 (a typedef, class-name, etc.), then we can't allow any
12768 more such type-specifiers henceforth.
12770 [dcl.spec]
12772 The longest sequence of decl-specifiers that could
12773 possibly be a type name is taken as the
12774 decl-specifier-seq of a declaration. The sequence shall
12775 be self-consistent as described below.
12777 [dcl.type]
12779 As a general rule, at most one type-specifier is allowed
12780 in the complete decl-specifier-seq of a declaration. The
12781 only exceptions are the following:
12783 -- const or volatile can be combined with any other
12784 type-specifier.
12786 -- signed or unsigned can be combined with char, long,
12787 short, or int.
12789 -- ..
12791 Example:
12793 typedef char* Pc;
12794 void g (const int Pc);
12796 Here, Pc is *not* part of the decl-specifier seq; it's
12797 the declarator. Therefore, once we see a type-specifier
12798 (other than a cv-qualifier), we forbid any additional
12799 user-defined types. We *do* still allow things like `int
12800 int' to be considered a decl-specifier-seq, and issue the
12801 error message later. */
12802 if (type_spec && !is_cv_qualifier)
12803 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12804 /* A constructor declarator cannot follow a type-specifier. */
12805 if (type_spec)
12807 constructor_possible_p = false;
12808 found_decl_spec = true;
12809 if (!is_cv_qualifier)
12810 decl_specs->any_type_specifiers_p = true;
12814 /* If we still do not have a DECL_SPEC, then there are no more
12815 decl-specifiers. */
12816 if (!found_decl_spec)
12817 break;
12819 decl_specs->any_specifiers_p = true;
12820 /* After we see one decl-specifier, further decl-specifiers are
12821 always optional. */
12822 flags |= CP_PARSER_FLAGS_OPTIONAL;
12825 /* Don't allow a friend specifier with a class definition. */
12826 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12827 && (*declares_class_or_enum & 2))
12828 error_at (decl_specs->locations[ds_friend],
12829 "class definition may not be declared a friend");
12832 /* Parse an (optional) storage-class-specifier.
12834 storage-class-specifier:
12835 auto
12836 register
12837 static
12838 extern
12839 mutable
12841 GNU Extension:
12843 storage-class-specifier:
12844 thread
12846 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12848 static tree
12849 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12851 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12853 case RID_AUTO:
12854 if (cxx_dialect != cxx98)
12855 return NULL_TREE;
12856 /* Fall through for C++98. */
12858 case RID_REGISTER:
12859 case RID_STATIC:
12860 case RID_EXTERN:
12861 case RID_MUTABLE:
12862 case RID_THREAD:
12863 /* Consume the token. */
12864 return cp_lexer_consume_token (parser->lexer)->u.value;
12866 default:
12867 return NULL_TREE;
12871 /* Parse an (optional) function-specifier.
12873 function-specifier:
12874 inline
12875 virtual
12876 explicit
12878 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12879 Updates DECL_SPECS, if it is non-NULL. */
12881 static tree
12882 cp_parser_function_specifier_opt (cp_parser* parser,
12883 cp_decl_specifier_seq *decl_specs)
12885 cp_token *token = cp_lexer_peek_token (parser->lexer);
12886 switch (token->keyword)
12888 case RID_INLINE:
12889 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12890 break;
12892 case RID_VIRTUAL:
12893 /* 14.5.2.3 [temp.mem]
12895 A member function template shall not be virtual. */
12896 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12897 && current_class_type)
12898 error_at (token->location, "templates may not be %<virtual%>");
12899 else
12900 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12901 break;
12903 case RID_EXPLICIT:
12904 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12905 break;
12907 default:
12908 return NULL_TREE;
12911 /* Consume the token. */
12912 return cp_lexer_consume_token (parser->lexer)->u.value;
12915 /* Parse a linkage-specification.
12917 linkage-specification:
12918 extern string-literal { declaration-seq [opt] }
12919 extern string-literal declaration */
12921 static void
12922 cp_parser_linkage_specification (cp_parser* parser)
12924 tree linkage;
12926 /* Look for the `extern' keyword. */
12927 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12929 /* Look for the string-literal. */
12930 linkage = cp_parser_string_literal (parser, false, false);
12932 /* Transform the literal into an identifier. If the literal is a
12933 wide-character string, or contains embedded NULs, then we can't
12934 handle it as the user wants. */
12935 if (strlen (TREE_STRING_POINTER (linkage))
12936 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12938 cp_parser_error (parser, "invalid linkage-specification");
12939 /* Assume C++ linkage. */
12940 linkage = lang_name_cplusplus;
12942 else
12943 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12945 /* We're now using the new linkage. */
12946 push_lang_context (linkage);
12948 /* If the next token is a `{', then we're using the first
12949 production. */
12950 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12952 cp_ensure_no_omp_declare_simd (parser);
12953 cp_ensure_no_oacc_routine (parser);
12955 /* Consume the `{' token. */
12956 cp_lexer_consume_token (parser->lexer);
12957 /* Parse the declarations. */
12958 cp_parser_declaration_seq_opt (parser);
12959 /* Look for the closing `}'. */
12960 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12962 /* Otherwise, there's just one declaration. */
12963 else
12965 bool saved_in_unbraced_linkage_specification_p;
12967 saved_in_unbraced_linkage_specification_p
12968 = parser->in_unbraced_linkage_specification_p;
12969 parser->in_unbraced_linkage_specification_p = true;
12970 cp_parser_declaration (parser);
12971 parser->in_unbraced_linkage_specification_p
12972 = saved_in_unbraced_linkage_specification_p;
12975 /* We're done with the linkage-specification. */
12976 pop_lang_context ();
12979 /* Parse a static_assert-declaration.
12981 static_assert-declaration:
12982 static_assert ( constant-expression , string-literal ) ;
12983 static_assert ( constant-expression ) ; (C++1Z)
12985 If MEMBER_P, this static_assert is a class member. */
12987 static void
12988 cp_parser_static_assert(cp_parser *parser, bool member_p)
12990 tree condition;
12991 tree message;
12992 cp_token *token;
12993 location_t saved_loc;
12994 bool dummy;
12996 /* Peek at the `static_assert' token so we can keep track of exactly
12997 where the static assertion started. */
12998 token = cp_lexer_peek_token (parser->lexer);
12999 saved_loc = token->location;
13001 /* Look for the `static_assert' keyword. */
13002 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13003 RT_STATIC_ASSERT))
13004 return;
13006 /* We know we are in a static assertion; commit to any tentative
13007 parse. */
13008 if (cp_parser_parsing_tentatively (parser))
13009 cp_parser_commit_to_tentative_parse (parser);
13011 /* Parse the `(' starting the static assertion condition. */
13012 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13014 /* Parse the constant-expression. Allow a non-constant expression
13015 here in order to give better diagnostics in finish_static_assert. */
13016 condition =
13017 cp_parser_constant_expression (parser,
13018 /*allow_non_constant_p=*/true,
13019 /*non_constant_p=*/&dummy);
13021 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13023 if (cxx_dialect < cxx1z)
13024 pedwarn (input_location, OPT_Wpedantic,
13025 "static_assert without a message "
13026 "only available with -std=c++1z or -std=gnu++1z");
13027 /* Eat the ')' */
13028 cp_lexer_consume_token (parser->lexer);
13029 message = build_string (1, "");
13030 TREE_TYPE (message) = char_array_type_node;
13031 fix_string_type (message);
13033 else
13035 /* Parse the separating `,'. */
13036 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13038 /* Parse the string-literal message. */
13039 message = cp_parser_string_literal (parser,
13040 /*translate=*/false,
13041 /*wide_ok=*/true);
13043 /* A `)' completes the static assertion. */
13044 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13045 cp_parser_skip_to_closing_parenthesis (parser,
13046 /*recovering=*/true,
13047 /*or_comma=*/false,
13048 /*consume_paren=*/true);
13051 /* A semicolon terminates the declaration. */
13052 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13054 /* Complete the static assertion, which may mean either processing
13055 the static assert now or saving it for template instantiation. */
13056 finish_static_assert (condition, message, saved_loc, member_p);
13059 /* Parse the expression in decltype ( expression ). */
13061 static tree
13062 cp_parser_decltype_expr (cp_parser *parser,
13063 bool &id_expression_or_member_access_p)
13065 cp_token *id_expr_start_token;
13066 tree expr;
13068 /* Since we're going to preserve any side-effects from this parse, set up a
13069 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13070 in the expression. */
13071 tentative_firewall firewall (parser);
13073 /* First, try parsing an id-expression. */
13074 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13075 cp_parser_parse_tentatively (parser);
13076 expr = cp_parser_id_expression (parser,
13077 /*template_keyword_p=*/false,
13078 /*check_dependency_p=*/true,
13079 /*template_p=*/NULL,
13080 /*declarator_p=*/false,
13081 /*optional_p=*/false);
13083 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13085 bool non_integral_constant_expression_p = false;
13086 tree id_expression = expr;
13087 cp_id_kind idk;
13088 const char *error_msg;
13090 if (identifier_p (expr))
13091 /* Lookup the name we got back from the id-expression. */
13092 expr = cp_parser_lookup_name_simple (parser, expr,
13093 id_expr_start_token->location);
13095 if (expr
13096 && expr != error_mark_node
13097 && TREE_CODE (expr) != TYPE_DECL
13098 && (TREE_CODE (expr) != BIT_NOT_EXPR
13099 || !TYPE_P (TREE_OPERAND (expr, 0)))
13100 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13102 /* Complete lookup of the id-expression. */
13103 expr = (finish_id_expression
13104 (id_expression, expr, parser->scope, &idk,
13105 /*integral_constant_expression_p=*/false,
13106 /*allow_non_integral_constant_expression_p=*/true,
13107 &non_integral_constant_expression_p,
13108 /*template_p=*/false,
13109 /*done=*/true,
13110 /*address_p=*/false,
13111 /*template_arg_p=*/false,
13112 &error_msg,
13113 id_expr_start_token->location));
13115 if (expr == error_mark_node)
13116 /* We found an id-expression, but it was something that we
13117 should not have found. This is an error, not something
13118 we can recover from, so note that we found an
13119 id-expression and we'll recover as gracefully as
13120 possible. */
13121 id_expression_or_member_access_p = true;
13124 if (expr
13125 && expr != error_mark_node
13126 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13127 /* We have an id-expression. */
13128 id_expression_or_member_access_p = true;
13131 if (!id_expression_or_member_access_p)
13133 /* Abort the id-expression parse. */
13134 cp_parser_abort_tentative_parse (parser);
13136 /* Parsing tentatively, again. */
13137 cp_parser_parse_tentatively (parser);
13139 /* Parse a class member access. */
13140 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13141 /*cast_p=*/false, /*decltype*/true,
13142 /*member_access_only_p=*/true, NULL);
13144 if (expr
13145 && expr != error_mark_node
13146 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13147 /* We have an id-expression. */
13148 id_expression_or_member_access_p = true;
13151 if (id_expression_or_member_access_p)
13152 /* We have parsed the complete id-expression or member access. */
13153 cp_parser_parse_definitely (parser);
13154 else
13156 /* Abort our attempt to parse an id-expression or member access
13157 expression. */
13158 cp_parser_abort_tentative_parse (parser);
13160 /* Parse a full expression. */
13161 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13162 /*decltype_p=*/true);
13165 return expr;
13168 /* Parse a `decltype' type. Returns the type.
13170 simple-type-specifier:
13171 decltype ( expression )
13172 C++14 proposal:
13173 decltype ( auto ) */
13175 static tree
13176 cp_parser_decltype (cp_parser *parser)
13178 tree expr;
13179 bool id_expression_or_member_access_p = false;
13180 const char *saved_message;
13181 bool saved_integral_constant_expression_p;
13182 bool saved_non_integral_constant_expression_p;
13183 bool saved_greater_than_is_operator_p;
13184 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13186 if (start_token->type == CPP_DECLTYPE)
13188 /* Already parsed. */
13189 cp_lexer_consume_token (parser->lexer);
13190 return saved_checks_value (start_token->u.tree_check_value);
13193 /* Look for the `decltype' token. */
13194 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13195 return error_mark_node;
13197 /* Parse the opening `('. */
13198 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13199 return error_mark_node;
13201 /* decltype (auto) */
13202 if (cxx_dialect >= cxx14
13203 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13205 cp_lexer_consume_token (parser->lexer);
13206 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13207 return error_mark_node;
13208 expr = make_decltype_auto ();
13209 AUTO_IS_DECLTYPE (expr) = true;
13210 goto rewrite;
13213 /* Types cannot be defined in a `decltype' expression. Save away the
13214 old message. */
13215 saved_message = parser->type_definition_forbidden_message;
13217 /* And create the new one. */
13218 parser->type_definition_forbidden_message
13219 = G_("types may not be defined in %<decltype%> expressions");
13221 /* The restrictions on constant-expressions do not apply inside
13222 decltype expressions. */
13223 saved_integral_constant_expression_p
13224 = parser->integral_constant_expression_p;
13225 saved_non_integral_constant_expression_p
13226 = parser->non_integral_constant_expression_p;
13227 parser->integral_constant_expression_p = false;
13229 /* Within a parenthesized expression, a `>' token is always
13230 the greater-than operator. */
13231 saved_greater_than_is_operator_p
13232 = parser->greater_than_is_operator_p;
13233 parser->greater_than_is_operator_p = true;
13235 /* Do not actually evaluate the expression. */
13236 ++cp_unevaluated_operand;
13238 /* Do not warn about problems with the expression. */
13239 ++c_inhibit_evaluation_warnings;
13241 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13243 /* Go back to evaluating expressions. */
13244 --cp_unevaluated_operand;
13245 --c_inhibit_evaluation_warnings;
13247 /* The `>' token might be the end of a template-id or
13248 template-parameter-list now. */
13249 parser->greater_than_is_operator_p
13250 = saved_greater_than_is_operator_p;
13252 /* Restore the old message and the integral constant expression
13253 flags. */
13254 parser->type_definition_forbidden_message = saved_message;
13255 parser->integral_constant_expression_p
13256 = saved_integral_constant_expression_p;
13257 parser->non_integral_constant_expression_p
13258 = saved_non_integral_constant_expression_p;
13260 /* Parse to the closing `)'. */
13261 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13263 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13264 /*consume_paren=*/true);
13265 return error_mark_node;
13268 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13269 tf_warning_or_error);
13271 rewrite:
13272 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13273 it again. */
13274 start_token->type = CPP_DECLTYPE;
13275 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13276 start_token->u.tree_check_value->value = expr;
13277 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13278 start_token->keyword = RID_MAX;
13279 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13281 return expr;
13284 /* Special member functions [gram.special] */
13286 /* Parse a conversion-function-id.
13288 conversion-function-id:
13289 operator conversion-type-id
13291 Returns an IDENTIFIER_NODE representing the operator. */
13293 static tree
13294 cp_parser_conversion_function_id (cp_parser* parser)
13296 tree type;
13297 tree saved_scope;
13298 tree saved_qualifying_scope;
13299 tree saved_object_scope;
13300 tree pushed_scope = NULL_TREE;
13302 /* Look for the `operator' token. */
13303 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13304 return error_mark_node;
13305 /* When we parse the conversion-type-id, the current scope will be
13306 reset. However, we need that information in able to look up the
13307 conversion function later, so we save it here. */
13308 saved_scope = parser->scope;
13309 saved_qualifying_scope = parser->qualifying_scope;
13310 saved_object_scope = parser->object_scope;
13311 /* We must enter the scope of the class so that the names of
13312 entities declared within the class are available in the
13313 conversion-type-id. For example, consider:
13315 struct S {
13316 typedef int I;
13317 operator I();
13320 S::operator I() { ... }
13322 In order to see that `I' is a type-name in the definition, we
13323 must be in the scope of `S'. */
13324 if (saved_scope)
13325 pushed_scope = push_scope (saved_scope);
13326 /* Parse the conversion-type-id. */
13327 type = cp_parser_conversion_type_id (parser);
13328 /* Leave the scope of the class, if any. */
13329 if (pushed_scope)
13330 pop_scope (pushed_scope);
13331 /* Restore the saved scope. */
13332 parser->scope = saved_scope;
13333 parser->qualifying_scope = saved_qualifying_scope;
13334 parser->object_scope = saved_object_scope;
13335 /* If the TYPE is invalid, indicate failure. */
13336 if (type == error_mark_node)
13337 return error_mark_node;
13338 return mangle_conv_op_name_for_type (type);
13341 /* Parse a conversion-type-id:
13343 conversion-type-id:
13344 type-specifier-seq conversion-declarator [opt]
13346 Returns the TYPE specified. */
13348 static tree
13349 cp_parser_conversion_type_id (cp_parser* parser)
13351 tree attributes;
13352 cp_decl_specifier_seq type_specifiers;
13353 cp_declarator *declarator;
13354 tree type_specified;
13355 const char *saved_message;
13357 /* Parse the attributes. */
13358 attributes = cp_parser_attributes_opt (parser);
13360 saved_message = parser->type_definition_forbidden_message;
13361 parser->type_definition_forbidden_message
13362 = G_("types may not be defined in a conversion-type-id");
13364 /* Parse the type-specifiers. */
13365 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13366 /*is_trailing_return=*/false,
13367 &type_specifiers);
13369 parser->type_definition_forbidden_message = saved_message;
13371 /* If that didn't work, stop. */
13372 if (type_specifiers.type == error_mark_node)
13373 return error_mark_node;
13374 /* Parse the conversion-declarator. */
13375 declarator = cp_parser_conversion_declarator_opt (parser);
13377 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13378 /*initialized=*/0, &attributes);
13379 if (attributes)
13380 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13382 /* Don't give this error when parsing tentatively. This happens to
13383 work because we always parse this definitively once. */
13384 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13385 && type_uses_auto (type_specified))
13387 if (cxx_dialect < cxx14)
13389 error ("invalid use of %<auto%> in conversion operator");
13390 return error_mark_node;
13392 else if (template_parm_scope_p ())
13393 warning (0, "use of %<auto%> in member template "
13394 "conversion operator can never be deduced");
13397 return type_specified;
13400 /* Parse an (optional) conversion-declarator.
13402 conversion-declarator:
13403 ptr-operator conversion-declarator [opt]
13407 static cp_declarator *
13408 cp_parser_conversion_declarator_opt (cp_parser* parser)
13410 enum tree_code code;
13411 tree class_type, std_attributes = NULL_TREE;
13412 cp_cv_quals cv_quals;
13414 /* We don't know if there's a ptr-operator next, or not. */
13415 cp_parser_parse_tentatively (parser);
13416 /* Try the ptr-operator. */
13417 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13418 &std_attributes);
13419 /* If it worked, look for more conversion-declarators. */
13420 if (cp_parser_parse_definitely (parser))
13422 cp_declarator *declarator;
13424 /* Parse another optional declarator. */
13425 declarator = cp_parser_conversion_declarator_opt (parser);
13427 declarator = cp_parser_make_indirect_declarator
13428 (code, class_type, cv_quals, declarator, std_attributes);
13430 return declarator;
13433 return NULL;
13436 /* Parse an (optional) ctor-initializer.
13438 ctor-initializer:
13439 : mem-initializer-list
13441 Returns TRUE iff the ctor-initializer was actually present. */
13443 static bool
13444 cp_parser_ctor_initializer_opt (cp_parser* parser)
13446 /* If the next token is not a `:', then there is no
13447 ctor-initializer. */
13448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13450 /* Do default initialization of any bases and members. */
13451 if (DECL_CONSTRUCTOR_P (current_function_decl))
13452 finish_mem_initializers (NULL_TREE);
13454 return false;
13457 /* Consume the `:' token. */
13458 cp_lexer_consume_token (parser->lexer);
13459 /* And the mem-initializer-list. */
13460 cp_parser_mem_initializer_list (parser);
13462 return true;
13465 /* Parse a mem-initializer-list.
13467 mem-initializer-list:
13468 mem-initializer ... [opt]
13469 mem-initializer ... [opt] , mem-initializer-list */
13471 static void
13472 cp_parser_mem_initializer_list (cp_parser* parser)
13474 tree mem_initializer_list = NULL_TREE;
13475 tree target_ctor = error_mark_node;
13476 cp_token *token = cp_lexer_peek_token (parser->lexer);
13478 /* Let the semantic analysis code know that we are starting the
13479 mem-initializer-list. */
13480 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13481 error_at (token->location,
13482 "only constructors take member initializers");
13484 /* Loop through the list. */
13485 while (true)
13487 tree mem_initializer;
13489 token = cp_lexer_peek_token (parser->lexer);
13490 /* Parse the mem-initializer. */
13491 mem_initializer = cp_parser_mem_initializer (parser);
13492 /* If the next token is a `...', we're expanding member initializers. */
13493 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13495 /* Consume the `...'. */
13496 cp_lexer_consume_token (parser->lexer);
13498 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13499 can be expanded but members cannot. */
13500 if (mem_initializer != error_mark_node
13501 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13503 error_at (token->location,
13504 "cannot expand initializer for member %<%D%>",
13505 TREE_PURPOSE (mem_initializer));
13506 mem_initializer = error_mark_node;
13509 /* Construct the pack expansion type. */
13510 if (mem_initializer != error_mark_node)
13511 mem_initializer = make_pack_expansion (mem_initializer);
13513 if (target_ctor != error_mark_node
13514 && mem_initializer != error_mark_node)
13516 error ("mem-initializer for %qD follows constructor delegation",
13517 TREE_PURPOSE (mem_initializer));
13518 mem_initializer = error_mark_node;
13520 /* Look for a target constructor. */
13521 if (mem_initializer != error_mark_node
13522 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13523 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13525 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13526 if (mem_initializer_list)
13528 error ("constructor delegation follows mem-initializer for %qD",
13529 TREE_PURPOSE (mem_initializer_list));
13530 mem_initializer = error_mark_node;
13532 target_ctor = mem_initializer;
13534 /* Add it to the list, unless it was erroneous. */
13535 if (mem_initializer != error_mark_node)
13537 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13538 mem_initializer_list = mem_initializer;
13540 /* If the next token is not a `,', we're done. */
13541 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13542 break;
13543 /* Consume the `,' token. */
13544 cp_lexer_consume_token (parser->lexer);
13547 /* Perform semantic analysis. */
13548 if (DECL_CONSTRUCTOR_P (current_function_decl))
13549 finish_mem_initializers (mem_initializer_list);
13552 /* Parse a mem-initializer.
13554 mem-initializer:
13555 mem-initializer-id ( expression-list [opt] )
13556 mem-initializer-id braced-init-list
13558 GNU extension:
13560 mem-initializer:
13561 ( expression-list [opt] )
13563 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13564 class) or FIELD_DECL (for a non-static data member) to initialize;
13565 the TREE_VALUE is the expression-list. An empty initialization
13566 list is represented by void_list_node. */
13568 static tree
13569 cp_parser_mem_initializer (cp_parser* parser)
13571 tree mem_initializer_id;
13572 tree expression_list;
13573 tree member;
13574 cp_token *token = cp_lexer_peek_token (parser->lexer);
13576 /* Find out what is being initialized. */
13577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13579 permerror (token->location,
13580 "anachronistic old-style base class initializer");
13581 mem_initializer_id = NULL_TREE;
13583 else
13585 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13586 if (mem_initializer_id == error_mark_node)
13587 return mem_initializer_id;
13589 member = expand_member_init (mem_initializer_id);
13590 if (member && !DECL_P (member))
13591 in_base_initializer = 1;
13593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13595 bool expr_non_constant_p;
13596 cp_lexer_set_source_position (parser->lexer);
13597 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13598 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13599 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13600 expression_list = build_tree_list (NULL_TREE, expression_list);
13602 else
13604 vec<tree, va_gc> *vec;
13605 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13606 /*cast_p=*/false,
13607 /*allow_expansion_p=*/true,
13608 /*non_constant_p=*/NULL);
13609 if (vec == NULL)
13610 return error_mark_node;
13611 expression_list = build_tree_list_vec (vec);
13612 release_tree_vector (vec);
13615 if (expression_list == error_mark_node)
13616 return error_mark_node;
13617 if (!expression_list)
13618 expression_list = void_type_node;
13620 in_base_initializer = 0;
13622 return member ? build_tree_list (member, expression_list) : error_mark_node;
13625 /* Parse a mem-initializer-id.
13627 mem-initializer-id:
13628 :: [opt] nested-name-specifier [opt] class-name
13629 decltype-specifier (C++11)
13630 identifier
13632 Returns a TYPE indicating the class to be initialized for the first
13633 production (and the second in C++11). Returns an IDENTIFIER_NODE
13634 indicating the data member to be initialized for the last production. */
13636 static tree
13637 cp_parser_mem_initializer_id (cp_parser* parser)
13639 bool global_scope_p;
13640 bool nested_name_specifier_p;
13641 bool template_p = false;
13642 tree id;
13644 cp_token *token = cp_lexer_peek_token (parser->lexer);
13646 /* `typename' is not allowed in this context ([temp.res]). */
13647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13649 error_at (token->location,
13650 "keyword %<typename%> not allowed in this context (a qualified "
13651 "member initializer is implicitly a type)");
13652 cp_lexer_consume_token (parser->lexer);
13654 /* Look for the optional `::' operator. */
13655 global_scope_p
13656 = (cp_parser_global_scope_opt (parser,
13657 /*current_scope_valid_p=*/false)
13658 != NULL_TREE);
13659 /* Look for the optional nested-name-specifier. The simplest way to
13660 implement:
13662 [temp.res]
13664 The keyword `typename' is not permitted in a base-specifier or
13665 mem-initializer; in these contexts a qualified name that
13666 depends on a template-parameter is implicitly assumed to be a
13667 type name.
13669 is to assume that we have seen the `typename' keyword at this
13670 point. */
13671 nested_name_specifier_p
13672 = (cp_parser_nested_name_specifier_opt (parser,
13673 /*typename_keyword_p=*/true,
13674 /*check_dependency_p=*/true,
13675 /*type_p=*/true,
13676 /*is_declaration=*/true)
13677 != NULL_TREE);
13678 if (nested_name_specifier_p)
13679 template_p = cp_parser_optional_template_keyword (parser);
13680 /* If there is a `::' operator or a nested-name-specifier, then we
13681 are definitely looking for a class-name. */
13682 if (global_scope_p || nested_name_specifier_p)
13683 return cp_parser_class_name (parser,
13684 /*typename_keyword_p=*/true,
13685 /*template_keyword_p=*/template_p,
13686 typename_type,
13687 /*check_dependency_p=*/true,
13688 /*class_head_p=*/false,
13689 /*is_declaration=*/true);
13690 /* Otherwise, we could also be looking for an ordinary identifier. */
13691 cp_parser_parse_tentatively (parser);
13692 if (cp_lexer_next_token_is_decltype (parser->lexer))
13693 /* Try a decltype-specifier. */
13694 id = cp_parser_decltype (parser);
13695 else
13696 /* Otherwise, try a class-name. */
13697 id = cp_parser_class_name (parser,
13698 /*typename_keyword_p=*/true,
13699 /*template_keyword_p=*/false,
13700 none_type,
13701 /*check_dependency_p=*/true,
13702 /*class_head_p=*/false,
13703 /*is_declaration=*/true);
13704 /* If we found one, we're done. */
13705 if (cp_parser_parse_definitely (parser))
13706 return id;
13707 /* Otherwise, look for an ordinary identifier. */
13708 return cp_parser_identifier (parser);
13711 /* Overloading [gram.over] */
13713 /* Parse an operator-function-id.
13715 operator-function-id:
13716 operator operator
13718 Returns an IDENTIFIER_NODE for the operator which is a
13719 human-readable spelling of the identifier, e.g., `operator +'. */
13721 static cp_expr
13722 cp_parser_operator_function_id (cp_parser* parser)
13724 /* Look for the `operator' keyword. */
13725 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13726 return error_mark_node;
13727 /* And then the name of the operator itself. */
13728 return cp_parser_operator (parser);
13731 /* Return an identifier node for a user-defined literal operator.
13732 The suffix identifier is chained to the operator name identifier. */
13734 static tree
13735 cp_literal_operator_id (const char* name)
13737 tree identifier;
13738 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13739 + strlen (name) + 10);
13740 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13741 identifier = get_identifier (buffer);
13743 return identifier;
13746 /* Parse an operator.
13748 operator:
13749 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13750 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13751 || ++ -- , ->* -> () []
13753 GNU Extensions:
13755 operator:
13756 <? >? <?= >?=
13758 Returns an IDENTIFIER_NODE for the operator which is a
13759 human-readable spelling of the identifier, e.g., `operator +'. */
13761 static cp_expr
13762 cp_parser_operator (cp_parser* parser)
13764 tree id = NULL_TREE;
13765 cp_token *token;
13766 bool utf8 = false;
13768 /* Peek at the next token. */
13769 token = cp_lexer_peek_token (parser->lexer);
13771 location_t start_loc = token->location;
13773 /* Figure out which operator we have. */
13774 switch (token->type)
13776 case CPP_KEYWORD:
13778 enum tree_code op;
13780 /* The keyword should be either `new' or `delete'. */
13781 if (token->keyword == RID_NEW)
13782 op = NEW_EXPR;
13783 else if (token->keyword == RID_DELETE)
13784 op = DELETE_EXPR;
13785 else
13786 break;
13788 /* Consume the `new' or `delete' token. */
13789 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13791 /* Peek at the next token. */
13792 token = cp_lexer_peek_token (parser->lexer);
13793 /* If it's a `[' token then this is the array variant of the
13794 operator. */
13795 if (token->type == CPP_OPEN_SQUARE)
13797 /* Consume the `[' token. */
13798 cp_lexer_consume_token (parser->lexer);
13799 /* Look for the `]' token. */
13800 if (cp_token *close_token
13801 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13802 end_loc = close_token->location;
13803 id = ansi_opname (op == NEW_EXPR
13804 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13806 /* Otherwise, we have the non-array variant. */
13807 else
13808 id = ansi_opname (op);
13810 location_t loc = make_location (start_loc, start_loc, end_loc);
13812 return cp_expr (id, loc);
13815 case CPP_PLUS:
13816 id = ansi_opname (PLUS_EXPR);
13817 break;
13819 case CPP_MINUS:
13820 id = ansi_opname (MINUS_EXPR);
13821 break;
13823 case CPP_MULT:
13824 id = ansi_opname (MULT_EXPR);
13825 break;
13827 case CPP_DIV:
13828 id = ansi_opname (TRUNC_DIV_EXPR);
13829 break;
13831 case CPP_MOD:
13832 id = ansi_opname (TRUNC_MOD_EXPR);
13833 break;
13835 case CPP_XOR:
13836 id = ansi_opname (BIT_XOR_EXPR);
13837 break;
13839 case CPP_AND:
13840 id = ansi_opname (BIT_AND_EXPR);
13841 break;
13843 case CPP_OR:
13844 id = ansi_opname (BIT_IOR_EXPR);
13845 break;
13847 case CPP_COMPL:
13848 id = ansi_opname (BIT_NOT_EXPR);
13849 break;
13851 case CPP_NOT:
13852 id = ansi_opname (TRUTH_NOT_EXPR);
13853 break;
13855 case CPP_EQ:
13856 id = ansi_assopname (NOP_EXPR);
13857 break;
13859 case CPP_LESS:
13860 id = ansi_opname (LT_EXPR);
13861 break;
13863 case CPP_GREATER:
13864 id = ansi_opname (GT_EXPR);
13865 break;
13867 case CPP_PLUS_EQ:
13868 id = ansi_assopname (PLUS_EXPR);
13869 break;
13871 case CPP_MINUS_EQ:
13872 id = ansi_assopname (MINUS_EXPR);
13873 break;
13875 case CPP_MULT_EQ:
13876 id = ansi_assopname (MULT_EXPR);
13877 break;
13879 case CPP_DIV_EQ:
13880 id = ansi_assopname (TRUNC_DIV_EXPR);
13881 break;
13883 case CPP_MOD_EQ:
13884 id = ansi_assopname (TRUNC_MOD_EXPR);
13885 break;
13887 case CPP_XOR_EQ:
13888 id = ansi_assopname (BIT_XOR_EXPR);
13889 break;
13891 case CPP_AND_EQ:
13892 id = ansi_assopname (BIT_AND_EXPR);
13893 break;
13895 case CPP_OR_EQ:
13896 id = ansi_assopname (BIT_IOR_EXPR);
13897 break;
13899 case CPP_LSHIFT:
13900 id = ansi_opname (LSHIFT_EXPR);
13901 break;
13903 case CPP_RSHIFT:
13904 id = ansi_opname (RSHIFT_EXPR);
13905 break;
13907 case CPP_LSHIFT_EQ:
13908 id = ansi_assopname (LSHIFT_EXPR);
13909 break;
13911 case CPP_RSHIFT_EQ:
13912 id = ansi_assopname (RSHIFT_EXPR);
13913 break;
13915 case CPP_EQ_EQ:
13916 id = ansi_opname (EQ_EXPR);
13917 break;
13919 case CPP_NOT_EQ:
13920 id = ansi_opname (NE_EXPR);
13921 break;
13923 case CPP_LESS_EQ:
13924 id = ansi_opname (LE_EXPR);
13925 break;
13927 case CPP_GREATER_EQ:
13928 id = ansi_opname (GE_EXPR);
13929 break;
13931 case CPP_AND_AND:
13932 id = ansi_opname (TRUTH_ANDIF_EXPR);
13933 break;
13935 case CPP_OR_OR:
13936 id = ansi_opname (TRUTH_ORIF_EXPR);
13937 break;
13939 case CPP_PLUS_PLUS:
13940 id = ansi_opname (POSTINCREMENT_EXPR);
13941 break;
13943 case CPP_MINUS_MINUS:
13944 id = ansi_opname (PREDECREMENT_EXPR);
13945 break;
13947 case CPP_COMMA:
13948 id = ansi_opname (COMPOUND_EXPR);
13949 break;
13951 case CPP_DEREF_STAR:
13952 id = ansi_opname (MEMBER_REF);
13953 break;
13955 case CPP_DEREF:
13956 id = ansi_opname (COMPONENT_REF);
13957 break;
13959 case CPP_OPEN_PAREN:
13960 /* Consume the `('. */
13961 cp_lexer_consume_token (parser->lexer);
13962 /* Look for the matching `)'. */
13963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13964 return ansi_opname (CALL_EXPR);
13966 case CPP_OPEN_SQUARE:
13967 /* Consume the `['. */
13968 cp_lexer_consume_token (parser->lexer);
13969 /* Look for the matching `]'. */
13970 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13971 return ansi_opname (ARRAY_REF);
13973 case CPP_UTF8STRING:
13974 case CPP_UTF8STRING_USERDEF:
13975 utf8 = true;
13976 case CPP_STRING:
13977 case CPP_WSTRING:
13978 case CPP_STRING16:
13979 case CPP_STRING32:
13980 case CPP_STRING_USERDEF:
13981 case CPP_WSTRING_USERDEF:
13982 case CPP_STRING16_USERDEF:
13983 case CPP_STRING32_USERDEF:
13985 tree str, string_tree;
13986 int sz, len;
13988 if (cxx_dialect == cxx98)
13989 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13991 /* Consume the string. */
13992 str = cp_parser_string_literal (parser, /*translate=*/true,
13993 /*wide_ok=*/true, /*lookup_udlit=*/false);
13994 if (str == error_mark_node)
13995 return error_mark_node;
13996 else if (TREE_CODE (str) == USERDEF_LITERAL)
13998 string_tree = USERDEF_LITERAL_VALUE (str);
13999 id = USERDEF_LITERAL_SUFFIX_ID (str);
14001 else
14003 string_tree = str;
14004 /* Look for the suffix identifier. */
14005 token = cp_lexer_peek_token (parser->lexer);
14006 if (token->type == CPP_NAME)
14007 id = cp_parser_identifier (parser);
14008 else if (token->type == CPP_KEYWORD)
14010 error ("unexpected keyword;"
14011 " remove space between quotes and suffix identifier");
14012 return error_mark_node;
14014 else
14016 error ("expected suffix identifier");
14017 return error_mark_node;
14020 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14021 (TREE_TYPE (TREE_TYPE (string_tree))));
14022 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14023 if (len != 0)
14025 error ("expected empty string after %<operator%> keyword");
14026 return error_mark_node;
14028 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14029 != char_type_node)
14031 error ("invalid encoding prefix in literal operator");
14032 return error_mark_node;
14034 if (id != error_mark_node)
14036 const char *name = IDENTIFIER_POINTER (id);
14037 id = cp_literal_operator_id (name);
14039 return id;
14042 default:
14043 /* Anything else is an error. */
14044 break;
14047 /* If we have selected an identifier, we need to consume the
14048 operator token. */
14049 if (id)
14050 cp_lexer_consume_token (parser->lexer);
14051 /* Otherwise, no valid operator name was present. */
14052 else
14054 cp_parser_error (parser, "expected operator");
14055 id = error_mark_node;
14058 return cp_expr (id, start_loc);
14061 /* Parse a template-declaration.
14063 template-declaration:
14064 export [opt] template < template-parameter-list > declaration
14066 If MEMBER_P is TRUE, this template-declaration occurs within a
14067 class-specifier.
14069 The grammar rule given by the standard isn't correct. What
14070 is really meant is:
14072 template-declaration:
14073 export [opt] template-parameter-list-seq
14074 decl-specifier-seq [opt] init-declarator [opt] ;
14075 export [opt] template-parameter-list-seq
14076 function-definition
14078 template-parameter-list-seq:
14079 template-parameter-list-seq [opt]
14080 template < template-parameter-list >
14082 Concept Extensions:
14084 template-parameter-list-seq:
14085 template < template-parameter-list > requires-clause [opt]
14087 requires-clause:
14088 requires logical-or-expression */
14090 static void
14091 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14093 /* Check for `export'. */
14094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14096 /* Consume the `export' token. */
14097 cp_lexer_consume_token (parser->lexer);
14098 /* Warn that we do not support `export'. */
14099 warning (0, "keyword %<export%> not implemented, and will be ignored");
14102 cp_parser_template_declaration_after_export (parser, member_p);
14105 /* Parse a template-parameter-list.
14107 template-parameter-list:
14108 template-parameter
14109 template-parameter-list , template-parameter
14111 Returns a TREE_LIST. Each node represents a template parameter.
14112 The nodes are connected via their TREE_CHAINs. */
14114 static tree
14115 cp_parser_template_parameter_list (cp_parser* parser)
14117 tree parameter_list = NULL_TREE;
14119 begin_template_parm_list ();
14121 /* The loop below parses the template parms. We first need to know
14122 the total number of template parms to be able to compute proper
14123 canonical types of each dependent type. So after the loop, when
14124 we know the total number of template parms,
14125 end_template_parm_list computes the proper canonical types and
14126 fixes up the dependent types accordingly. */
14127 while (true)
14129 tree parameter;
14130 bool is_non_type;
14131 bool is_parameter_pack;
14132 location_t parm_loc;
14134 /* Parse the template-parameter. */
14135 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14136 parameter = cp_parser_template_parameter (parser,
14137 &is_non_type,
14138 &is_parameter_pack);
14139 /* Add it to the list. */
14140 if (parameter != error_mark_node)
14141 parameter_list = process_template_parm (parameter_list,
14142 parm_loc,
14143 parameter,
14144 is_non_type,
14145 is_parameter_pack);
14146 else
14148 tree err_parm = build_tree_list (parameter, parameter);
14149 parameter_list = chainon (parameter_list, err_parm);
14152 /* If the next token is not a `,', we're done. */
14153 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14154 break;
14155 /* Otherwise, consume the `,' token. */
14156 cp_lexer_consume_token (parser->lexer);
14159 return end_template_parm_list (parameter_list);
14162 /* Parse a introduction-list.
14164 introduction-list:
14165 introduced-parameter
14166 introduction-list , introduced-parameter
14168 introduced-parameter:
14169 ...[opt] identifier
14171 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14172 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14173 WILDCARD_DECL will also have DECL_NAME set and token location in
14174 DECL_SOURCE_LOCATION. */
14176 static tree
14177 cp_parser_introduction_list (cp_parser *parser)
14179 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14181 while (true)
14183 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14184 if (is_pack)
14185 cp_lexer_consume_token (parser->lexer);
14187 /* Build placeholder. */
14188 tree parm = build_nt (WILDCARD_DECL);
14189 DECL_SOURCE_LOCATION (parm)
14190 = cp_lexer_peek_token (parser->lexer)->location;
14191 DECL_NAME (parm) = cp_parser_identifier (parser);
14192 WILDCARD_PACK_P (parm) = is_pack;
14193 vec_safe_push (introduction_vec, parm);
14195 /* If the next token is not a `,', we're done. */
14196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14197 break;
14198 /* Otherwise, consume the `,' token. */
14199 cp_lexer_consume_token (parser->lexer);
14202 /* Convert the vec into a TREE_VEC. */
14203 tree introduction_list = make_tree_vec (introduction_vec->length ());
14204 unsigned int n;
14205 tree parm;
14206 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14207 TREE_VEC_ELT (introduction_list, n) = parm;
14209 release_tree_vector (introduction_vec);
14210 return introduction_list;
14213 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14214 is an abstract declarator. */
14216 static inline cp_declarator*
14217 get_id_declarator (cp_declarator *declarator)
14219 cp_declarator *d = declarator;
14220 while (d && d->kind != cdk_id)
14221 d = d->declarator;
14222 return d;
14225 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14226 is an abstract declarator. */
14228 static inline tree
14229 get_unqualified_id (cp_declarator *declarator)
14231 declarator = get_id_declarator (declarator);
14232 if (declarator)
14233 return declarator->u.id.unqualified_name;
14234 else
14235 return NULL_TREE;
14238 /* Returns true if DECL represents a constrained-parameter. */
14240 static inline bool
14241 is_constrained_parameter (tree decl)
14243 return (decl
14244 && TREE_CODE (decl) == TYPE_DECL
14245 && CONSTRAINED_PARM_CONCEPT (decl)
14246 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14249 /* Returns true if PARM declares a constrained-parameter. */
14251 static inline bool
14252 is_constrained_parameter (cp_parameter_declarator *parm)
14254 return is_constrained_parameter (parm->decl_specifiers.type);
14257 /* Check that the type parameter is only a declarator-id, and that its
14258 type is not cv-qualified. */
14260 bool
14261 cp_parser_check_constrained_type_parm (cp_parser *parser,
14262 cp_parameter_declarator *parm)
14264 if (!parm->declarator)
14265 return true;
14267 if (parm->declarator->kind != cdk_id)
14269 cp_parser_error (parser, "invalid constrained type parameter");
14270 return false;
14273 /* Don't allow cv-qualified type parameters. */
14274 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14275 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14277 cp_parser_error (parser, "cv-qualified type parameter");
14278 return false;
14281 return true;
14284 /* Finish parsing/processing a template type parameter and checking
14285 various restrictions. */
14287 static inline tree
14288 cp_parser_constrained_type_template_parm (cp_parser *parser,
14289 tree id,
14290 cp_parameter_declarator* parmdecl)
14292 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14293 return finish_template_type_parm (class_type_node, id);
14294 else
14295 return error_mark_node;
14298 static tree
14299 finish_constrained_template_template_parm (tree proto, tree id)
14301 /* FIXME: This should probably be copied, and we may need to adjust
14302 the template parameter depths. */
14303 tree saved_parms = current_template_parms;
14304 begin_template_parm_list ();
14305 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14306 end_template_parm_list ();
14308 tree parm = finish_template_template_parm (class_type_node, id);
14309 current_template_parms = saved_parms;
14311 return parm;
14314 /* Finish parsing/processing a template template parameter by borrowing
14315 the template parameter list from the prototype parameter. */
14317 static tree
14318 cp_parser_constrained_template_template_parm (cp_parser *parser,
14319 tree proto,
14320 tree id,
14321 cp_parameter_declarator *parmdecl)
14323 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14324 return error_mark_node;
14325 return finish_constrained_template_template_parm (proto, id);
14328 /* Create a new non-type template parameter from the given PARM
14329 declarator. */
14331 static tree
14332 constrained_non_type_template_parm (bool *is_non_type,
14333 cp_parameter_declarator *parm)
14335 *is_non_type = true;
14336 cp_declarator *decl = parm->declarator;
14337 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14338 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14339 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14342 /* Build a constrained template parameter based on the PARMDECL
14343 declarator. The type of PARMDECL is the constrained type, which
14344 refers to the prototype template parameter that ultimately
14345 specifies the type of the declared parameter. */
14347 static tree
14348 finish_constrained_parameter (cp_parser *parser,
14349 cp_parameter_declarator *parmdecl,
14350 bool *is_non_type,
14351 bool *is_parameter_pack)
14353 tree decl = parmdecl->decl_specifiers.type;
14354 tree id = get_unqualified_id (parmdecl->declarator);
14355 tree def = parmdecl->default_argument;
14356 tree proto = DECL_INITIAL (decl);
14358 /* A template parameter constrained by a variadic concept shall also
14359 be declared as a template parameter pack. */
14360 bool is_variadic = template_parameter_pack_p (proto);
14361 if (is_variadic && !*is_parameter_pack)
14362 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14364 /* Build the parameter. Return an error if the declarator was invalid. */
14365 tree parm;
14366 if (TREE_CODE (proto) == TYPE_DECL)
14367 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14368 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14369 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14370 parmdecl);
14371 else
14372 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14373 if (parm == error_mark_node)
14374 return error_mark_node;
14376 /* Finish the parameter decl and create a node attaching the
14377 default argument and constraint. */
14378 parm = build_tree_list (def, parm);
14379 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14381 return parm;
14384 /* Returns true if the parsed type actually represents the declaration
14385 of a type template-parameter. */
14387 static inline bool
14388 declares_constrained_type_template_parameter (tree type)
14390 return (is_constrained_parameter (type)
14391 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14395 /* Returns true if the parsed type actually represents the declaration of
14396 a template template-parameter. */
14398 static bool
14399 declares_constrained_template_template_parameter (tree type)
14401 return (is_constrained_parameter (type)
14402 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14405 /* Parse a default argument for a type template-parameter.
14406 Note that diagnostics are handled in cp_parser_template_parameter. */
14408 static tree
14409 cp_parser_default_type_template_argument (cp_parser *parser)
14411 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14413 /* Consume the `=' token. */
14414 cp_lexer_consume_token (parser->lexer);
14416 cp_token *token = cp_lexer_peek_token (parser->lexer);
14418 /* Parse the default-argument. */
14419 push_deferring_access_checks (dk_no_deferred);
14420 tree default_argument = cp_parser_type_id (parser);
14421 pop_deferring_access_checks ();
14423 if (flag_concepts && type_uses_auto (default_argument))
14425 error_at (token->location,
14426 "invalid use of %<auto%> in default template argument");
14427 return error_mark_node;
14430 return default_argument;
14433 /* Parse a default argument for a template template-parameter. */
14435 static tree
14436 cp_parser_default_template_template_argument (cp_parser *parser)
14438 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14440 bool is_template;
14442 /* Consume the `='. */
14443 cp_lexer_consume_token (parser->lexer);
14444 /* Parse the id-expression. */
14445 push_deferring_access_checks (dk_no_deferred);
14446 /* save token before parsing the id-expression, for error
14447 reporting */
14448 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14449 tree default_argument
14450 = cp_parser_id_expression (parser,
14451 /*template_keyword_p=*/false,
14452 /*check_dependency_p=*/true,
14453 /*template_p=*/&is_template,
14454 /*declarator_p=*/false,
14455 /*optional_p=*/false);
14456 if (TREE_CODE (default_argument) == TYPE_DECL)
14457 /* If the id-expression was a template-id that refers to
14458 a template-class, we already have the declaration here,
14459 so no further lookup is needed. */
14461 else
14462 /* Look up the name. */
14463 default_argument
14464 = cp_parser_lookup_name (parser, default_argument,
14465 none_type,
14466 /*is_template=*/is_template,
14467 /*is_namespace=*/false,
14468 /*check_dependency=*/true,
14469 /*ambiguous_decls=*/NULL,
14470 token->location);
14471 /* See if the default argument is valid. */
14472 default_argument = check_template_template_default_arg (default_argument);
14473 pop_deferring_access_checks ();
14474 return default_argument;
14477 /* Parse a template-parameter.
14479 template-parameter:
14480 type-parameter
14481 parameter-declaration
14483 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14484 the parameter. The TREE_PURPOSE is the default value, if any.
14485 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14486 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14487 set to true iff this parameter is a parameter pack. */
14489 static tree
14490 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14491 bool *is_parameter_pack)
14493 cp_token *token;
14494 cp_parameter_declarator *parameter_declarator;
14495 tree parm;
14497 /* Assume it is a type parameter or a template parameter. */
14498 *is_non_type = false;
14499 /* Assume it not a parameter pack. */
14500 *is_parameter_pack = false;
14501 /* Peek at the next token. */
14502 token = cp_lexer_peek_token (parser->lexer);
14503 /* If it is `class' or `template', we have a type-parameter. */
14504 if (token->keyword == RID_TEMPLATE)
14505 return cp_parser_type_parameter (parser, is_parameter_pack);
14506 /* If it is `class' or `typename' we do not know yet whether it is a
14507 type parameter or a non-type parameter. Consider:
14509 template <typename T, typename T::X X> ...
14513 template <class C, class D*> ...
14515 Here, the first parameter is a type parameter, and the second is
14516 a non-type parameter. We can tell by looking at the token after
14517 the identifier -- if it is a `,', `=', or `>' then we have a type
14518 parameter. */
14519 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14521 /* Peek at the token after `class' or `typename'. */
14522 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14523 /* If it's an ellipsis, we have a template type parameter
14524 pack. */
14525 if (token->type == CPP_ELLIPSIS)
14526 return cp_parser_type_parameter (parser, is_parameter_pack);
14527 /* If it's an identifier, skip it. */
14528 if (token->type == CPP_NAME)
14529 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14530 /* Now, see if the token looks like the end of a template
14531 parameter. */
14532 if (token->type == CPP_COMMA
14533 || token->type == CPP_EQ
14534 || token->type == CPP_GREATER)
14535 return cp_parser_type_parameter (parser, is_parameter_pack);
14538 /* Otherwise, it is a non-type parameter or a constrained parameter.
14540 [temp.param]
14542 When parsing a default template-argument for a non-type
14543 template-parameter, the first non-nested `>' is taken as the end
14544 of the template parameter-list rather than a greater-than
14545 operator. */
14546 parameter_declarator
14547 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14548 /*parenthesized_p=*/NULL);
14550 if (!parameter_declarator)
14551 return error_mark_node;
14553 /* If the parameter declaration is marked as a parameter pack, set
14554 *IS_PARAMETER_PACK to notify the caller. */
14555 if (parameter_declarator->template_parameter_pack_p)
14556 *is_parameter_pack = true;
14558 if (parameter_declarator->default_argument)
14560 /* Can happen in some cases of erroneous input (c++/34892). */
14561 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14562 /* Consume the `...' for better error recovery. */
14563 cp_lexer_consume_token (parser->lexer);
14566 // The parameter may have been constrained.
14567 if (is_constrained_parameter (parameter_declarator))
14568 return finish_constrained_parameter (parser,
14569 parameter_declarator,
14570 is_non_type,
14571 is_parameter_pack);
14573 // Now we're sure that the parameter is a non-type parameter.
14574 *is_non_type = true;
14576 parm = grokdeclarator (parameter_declarator->declarator,
14577 &parameter_declarator->decl_specifiers,
14578 TPARM, /*initialized=*/0,
14579 /*attrlist=*/NULL);
14580 if (parm == error_mark_node)
14581 return error_mark_node;
14583 return build_tree_list (parameter_declarator->default_argument, parm);
14586 /* Parse a type-parameter.
14588 type-parameter:
14589 class identifier [opt]
14590 class identifier [opt] = type-id
14591 typename identifier [opt]
14592 typename identifier [opt] = type-id
14593 template < template-parameter-list > class identifier [opt]
14594 template < template-parameter-list > class identifier [opt]
14595 = id-expression
14597 GNU Extension (variadic templates):
14599 type-parameter:
14600 class ... identifier [opt]
14601 typename ... identifier [opt]
14603 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14604 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14605 the declaration of the parameter.
14607 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14609 static tree
14610 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14612 cp_token *token;
14613 tree parameter;
14615 /* Look for a keyword to tell us what kind of parameter this is. */
14616 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14617 if (!token)
14618 return error_mark_node;
14620 switch (token->keyword)
14622 case RID_CLASS:
14623 case RID_TYPENAME:
14625 tree identifier;
14626 tree default_argument;
14628 /* If the next token is an ellipsis, we have a template
14629 argument pack. */
14630 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14632 /* Consume the `...' token. */
14633 cp_lexer_consume_token (parser->lexer);
14634 maybe_warn_variadic_templates ();
14636 *is_parameter_pack = true;
14639 /* If the next token is an identifier, then it names the
14640 parameter. */
14641 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14642 identifier = cp_parser_identifier (parser);
14643 else
14644 identifier = NULL_TREE;
14646 /* Create the parameter. */
14647 parameter = finish_template_type_parm (class_type_node, identifier);
14649 /* If the next token is an `=', we have a default argument. */
14650 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14652 default_argument
14653 = cp_parser_default_type_template_argument (parser);
14655 /* Template parameter packs cannot have default
14656 arguments. */
14657 if (*is_parameter_pack)
14659 if (identifier)
14660 error_at (token->location,
14661 "template parameter pack %qD cannot have a "
14662 "default argument", identifier);
14663 else
14664 error_at (token->location,
14665 "template parameter packs cannot have "
14666 "default arguments");
14667 default_argument = NULL_TREE;
14669 else if (check_for_bare_parameter_packs (default_argument))
14670 default_argument = error_mark_node;
14672 else
14673 default_argument = NULL_TREE;
14675 /* Create the combined representation of the parameter and the
14676 default argument. */
14677 parameter = build_tree_list (default_argument, parameter);
14679 break;
14681 case RID_TEMPLATE:
14683 tree identifier;
14684 tree default_argument;
14686 /* Look for the `<'. */
14687 cp_parser_require (parser, CPP_LESS, RT_LESS);
14688 /* Parse the template-parameter-list. */
14689 cp_parser_template_parameter_list (parser);
14690 /* Look for the `>'. */
14691 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14693 // If template requirements are present, parse them.
14694 tree reqs = get_shorthand_constraints (current_template_parms);
14695 if (tree r = cp_parser_requires_clause_opt (parser))
14696 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14697 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14699 /* Look for the `class' or 'typename' keywords. */
14700 cp_parser_type_parameter_key (parser);
14701 /* If the next token is an ellipsis, we have a template
14702 argument pack. */
14703 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14705 /* Consume the `...' token. */
14706 cp_lexer_consume_token (parser->lexer);
14707 maybe_warn_variadic_templates ();
14709 *is_parameter_pack = true;
14711 /* If the next token is an `=', then there is a
14712 default-argument. If the next token is a `>', we are at
14713 the end of the parameter-list. If the next token is a `,',
14714 then we are at the end of this parameter. */
14715 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14716 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14717 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14719 identifier = cp_parser_identifier (parser);
14720 /* Treat invalid names as if the parameter were nameless. */
14721 if (identifier == error_mark_node)
14722 identifier = NULL_TREE;
14724 else
14725 identifier = NULL_TREE;
14727 /* Create the template parameter. */
14728 parameter = finish_template_template_parm (class_type_node,
14729 identifier);
14731 /* If the next token is an `=', then there is a
14732 default-argument. */
14733 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14735 default_argument
14736 = cp_parser_default_template_template_argument (parser);
14738 /* Template parameter packs cannot have default
14739 arguments. */
14740 if (*is_parameter_pack)
14742 if (identifier)
14743 error_at (token->location,
14744 "template parameter pack %qD cannot "
14745 "have a default argument",
14746 identifier);
14747 else
14748 error_at (token->location, "template parameter packs cannot "
14749 "have default arguments");
14750 default_argument = NULL_TREE;
14753 else
14754 default_argument = NULL_TREE;
14756 /* Create the combined representation of the parameter and the
14757 default argument. */
14758 parameter = build_tree_list (default_argument, parameter);
14760 break;
14762 default:
14763 gcc_unreachable ();
14764 break;
14767 return parameter;
14770 /* Parse a template-id.
14772 template-id:
14773 template-name < template-argument-list [opt] >
14775 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14776 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14777 returned. Otherwise, if the template-name names a function, or set
14778 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14779 names a class, returns a TYPE_DECL for the specialization.
14781 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14782 uninstantiated templates. */
14784 static tree
14785 cp_parser_template_id (cp_parser *parser,
14786 bool template_keyword_p,
14787 bool check_dependency_p,
14788 enum tag_types tag_type,
14789 bool is_declaration)
14791 tree templ;
14792 tree arguments;
14793 tree template_id;
14794 cp_token_position start_of_id = 0;
14795 cp_token *next_token = NULL, *next_token_2 = NULL;
14796 bool is_identifier;
14798 /* If the next token corresponds to a template-id, there is no need
14799 to reparse it. */
14800 next_token = cp_lexer_peek_token (parser->lexer);
14801 if (next_token->type == CPP_TEMPLATE_ID)
14803 cp_lexer_consume_token (parser->lexer);
14804 return saved_checks_value (next_token->u.tree_check_value);
14807 /* Avoid performing name lookup if there is no possibility of
14808 finding a template-id. */
14809 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14810 || (next_token->type == CPP_NAME
14811 && !cp_parser_nth_token_starts_template_argument_list_p
14812 (parser, 2)))
14814 cp_parser_error (parser, "expected template-id");
14815 return error_mark_node;
14818 /* Remember where the template-id starts. */
14819 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14820 start_of_id = cp_lexer_token_position (parser->lexer, false);
14822 push_deferring_access_checks (dk_deferred);
14824 /* Parse the template-name. */
14825 is_identifier = false;
14826 templ = cp_parser_template_name (parser, template_keyword_p,
14827 check_dependency_p,
14828 is_declaration,
14829 tag_type,
14830 &is_identifier);
14831 if (templ == error_mark_node || is_identifier)
14833 pop_deferring_access_checks ();
14834 return templ;
14837 /* Since we're going to preserve any side-effects from this parse, set up a
14838 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14839 in the template arguments. */
14840 tentative_firewall firewall (parser);
14842 /* If we find the sequence `[:' after a template-name, it's probably
14843 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14844 parse correctly the argument list. */
14845 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
14846 == CPP_OPEN_SQUARE)
14847 && next_token->flags & DIGRAPH
14848 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
14849 == CPP_COLON)
14850 && !(next_token_2->flags & PREV_WHITE))
14852 cp_parser_parse_tentatively (parser);
14853 /* Change `:' into `::'. */
14854 next_token_2->type = CPP_SCOPE;
14855 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14856 CPP_LESS. */
14857 cp_lexer_consume_token (parser->lexer);
14859 /* Parse the arguments. */
14860 arguments = cp_parser_enclosed_template_argument_list (parser);
14861 if (!cp_parser_parse_definitely (parser))
14863 /* If we couldn't parse an argument list, then we revert our changes
14864 and return simply an error. Maybe this is not a template-id
14865 after all. */
14866 next_token_2->type = CPP_COLON;
14867 cp_parser_error (parser, "expected %<<%>");
14868 pop_deferring_access_checks ();
14869 return error_mark_node;
14871 /* Otherwise, emit an error about the invalid digraph, but continue
14872 parsing because we got our argument list. */
14873 if (permerror (next_token->location,
14874 "%<<::%> cannot begin a template-argument list"))
14876 static bool hint = false;
14877 inform (next_token->location,
14878 "%<<:%> is an alternate spelling for %<[%>."
14879 " Insert whitespace between %<<%> and %<::%>");
14880 if (!hint && !flag_permissive)
14882 inform (next_token->location, "(if you use %<-fpermissive%> "
14883 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14884 "accept your code)");
14885 hint = true;
14889 else
14891 /* Look for the `<' that starts the template-argument-list. */
14892 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14894 pop_deferring_access_checks ();
14895 return error_mark_node;
14897 /* Parse the arguments. */
14898 arguments = cp_parser_enclosed_template_argument_list (parser);
14901 /* Build a representation of the specialization. */
14902 if (identifier_p (templ))
14903 template_id = build_min_nt_loc (next_token->location,
14904 TEMPLATE_ID_EXPR,
14905 templ, arguments);
14906 else if (DECL_TYPE_TEMPLATE_P (templ)
14907 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14909 bool entering_scope;
14910 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14911 template (rather than some instantiation thereof) only if
14912 is not nested within some other construct. For example, in
14913 "template <typename T> void f(T) { A<T>::", A<T> is just an
14914 instantiation of A. */
14915 entering_scope = (template_parm_scope_p ()
14916 && cp_lexer_next_token_is (parser->lexer,
14917 CPP_SCOPE));
14918 template_id
14919 = finish_template_type (templ, arguments, entering_scope);
14921 /* A template-like identifier may be a partial concept id. */
14922 else if (flag_concepts
14923 && (template_id = (cp_parser_maybe_partial_concept_id
14924 (parser, templ, arguments))))
14925 return template_id;
14926 else if (variable_template_p (templ))
14928 template_id = lookup_template_variable (templ, arguments);
14929 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14930 SET_EXPR_LOCATION (template_id, next_token->location);
14932 else
14934 /* If it's not a class-template or a template-template, it should be
14935 a function-template. */
14936 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14937 || TREE_CODE (templ) == OVERLOAD
14938 || BASELINK_P (templ)));
14940 template_id = lookup_template_function (templ, arguments);
14941 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14942 SET_EXPR_LOCATION (template_id, next_token->location);
14945 /* If parsing tentatively, replace the sequence of tokens that makes
14946 up the template-id with a CPP_TEMPLATE_ID token. That way,
14947 should we re-parse the token stream, we will not have to repeat
14948 the effort required to do the parse, nor will we issue duplicate
14949 error messages about problems during instantiation of the
14950 template. */
14951 if (start_of_id
14952 /* Don't do this if we had a parse error in a declarator; re-parsing
14953 might succeed if a name changes meaning (60361). */
14954 && !(cp_parser_error_occurred (parser)
14955 && cp_parser_parsing_tentatively (parser)
14956 && parser->in_declarator_p))
14958 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14960 /* Reset the contents of the START_OF_ID token. */
14961 token->type = CPP_TEMPLATE_ID;
14963 /* Update the location to be of the form:
14964 template-name < template-argument-list [opt] >
14965 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14966 with caret == start at the start of the template-name,
14967 ranging until the closing '>'. */
14968 location_t finish_loc
14969 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14970 location_t combined_loc
14971 = make_location (token->location, token->location, finish_loc);
14972 token->location = combined_loc;
14974 /* Retrieve any deferred checks. Do not pop this access checks yet
14975 so the memory will not be reclaimed during token replacing below. */
14976 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14977 token->u.tree_check_value->value = template_id;
14978 token->u.tree_check_value->checks = get_deferred_access_checks ();
14979 token->keyword = RID_MAX;
14981 /* Purge all subsequent tokens. */
14982 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14984 /* ??? Can we actually assume that, if template_id ==
14985 error_mark_node, we will have issued a diagnostic to the
14986 user, as opposed to simply marking the tentative parse as
14987 failed? */
14988 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14989 error_at (token->location, "parse error in template argument list");
14992 pop_to_parent_deferring_access_checks ();
14993 return template_id;
14996 /* Parse a template-name.
14998 template-name:
14999 identifier
15001 The standard should actually say:
15003 template-name:
15004 identifier
15005 operator-function-id
15007 A defect report has been filed about this issue.
15009 A conversion-function-id cannot be a template name because they cannot
15010 be part of a template-id. In fact, looking at this code:
15012 a.operator K<int>()
15014 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15015 It is impossible to call a templated conversion-function-id with an
15016 explicit argument list, since the only allowed template parameter is
15017 the type to which it is converting.
15019 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15020 `template' keyword, in a construction like:
15022 T::template f<3>()
15024 In that case `f' is taken to be a template-name, even though there
15025 is no way of knowing for sure.
15027 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15028 name refers to a set of overloaded functions, at least one of which
15029 is a template, or an IDENTIFIER_NODE with the name of the template,
15030 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15031 names are looked up inside uninstantiated templates. */
15033 static tree
15034 cp_parser_template_name (cp_parser* parser,
15035 bool template_keyword_p,
15036 bool check_dependency_p,
15037 bool is_declaration,
15038 enum tag_types tag_type,
15039 bool *is_identifier)
15041 tree identifier;
15042 tree decl;
15043 tree fns;
15044 cp_token *token = cp_lexer_peek_token (parser->lexer);
15046 /* If the next token is `operator', then we have either an
15047 operator-function-id or a conversion-function-id. */
15048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15050 /* We don't know whether we're looking at an
15051 operator-function-id or a conversion-function-id. */
15052 cp_parser_parse_tentatively (parser);
15053 /* Try an operator-function-id. */
15054 identifier = cp_parser_operator_function_id (parser);
15055 /* If that didn't work, try a conversion-function-id. */
15056 if (!cp_parser_parse_definitely (parser))
15058 cp_parser_error (parser, "expected template-name");
15059 return error_mark_node;
15062 /* Look for the identifier. */
15063 else
15064 identifier = cp_parser_identifier (parser);
15066 /* If we didn't find an identifier, we don't have a template-id. */
15067 if (identifier == error_mark_node)
15068 return error_mark_node;
15070 /* If the name immediately followed the `template' keyword, then it
15071 is a template-name. However, if the next token is not `<', then
15072 we do not treat it as a template-name, since it is not being used
15073 as part of a template-id. This enables us to handle constructs
15074 like:
15076 template <typename T> struct S { S(); };
15077 template <typename T> S<T>::S();
15079 correctly. We would treat `S' as a template -- if it were `S<T>'
15080 -- but we do not if there is no `<'. */
15082 if (processing_template_decl
15083 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15085 /* In a declaration, in a dependent context, we pretend that the
15086 "template" keyword was present in order to improve error
15087 recovery. For example, given:
15089 template <typename T> void f(T::X<int>);
15091 we want to treat "X<int>" as a template-id. */
15092 if (is_declaration
15093 && !template_keyword_p
15094 && parser->scope && TYPE_P (parser->scope)
15095 && check_dependency_p
15096 && dependent_scope_p (parser->scope)
15097 /* Do not do this for dtors (or ctors), since they never
15098 need the template keyword before their name. */
15099 && !constructor_name_p (identifier, parser->scope))
15101 cp_token_position start = 0;
15103 /* Explain what went wrong. */
15104 error_at (token->location, "non-template %qD used as template",
15105 identifier);
15106 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15107 parser->scope, identifier);
15108 /* If parsing tentatively, find the location of the "<" token. */
15109 if (cp_parser_simulate_error (parser))
15110 start = cp_lexer_token_position (parser->lexer, true);
15111 /* Parse the template arguments so that we can issue error
15112 messages about them. */
15113 cp_lexer_consume_token (parser->lexer);
15114 cp_parser_enclosed_template_argument_list (parser);
15115 /* Skip tokens until we find a good place from which to
15116 continue parsing. */
15117 cp_parser_skip_to_closing_parenthesis (parser,
15118 /*recovering=*/true,
15119 /*or_comma=*/true,
15120 /*consume_paren=*/false);
15121 /* If parsing tentatively, permanently remove the
15122 template argument list. That will prevent duplicate
15123 error messages from being issued about the missing
15124 "template" keyword. */
15125 if (start)
15126 cp_lexer_purge_tokens_after (parser->lexer, start);
15127 if (is_identifier)
15128 *is_identifier = true;
15129 return identifier;
15132 /* If the "template" keyword is present, then there is generally
15133 no point in doing name-lookup, so we just return IDENTIFIER.
15134 But, if the qualifying scope is non-dependent then we can
15135 (and must) do name-lookup normally. */
15136 if (template_keyword_p
15137 && (!parser->scope
15138 || (TYPE_P (parser->scope)
15139 && dependent_type_p (parser->scope))))
15140 return identifier;
15143 /* Look up the name. */
15144 decl = cp_parser_lookup_name (parser, identifier,
15145 tag_type,
15146 /*is_template=*/true,
15147 /*is_namespace=*/false,
15148 check_dependency_p,
15149 /*ambiguous_decls=*/NULL,
15150 token->location);
15152 decl = strip_using_decl (decl);
15154 /* If DECL is a template, then the name was a template-name. */
15155 if (TREE_CODE (decl) == TEMPLATE_DECL)
15157 if (TREE_DEPRECATED (decl)
15158 && deprecated_state != DEPRECATED_SUPPRESS)
15159 warn_deprecated_use (decl, NULL_TREE);
15161 else
15163 tree fn = NULL_TREE;
15165 /* The standard does not explicitly indicate whether a name that
15166 names a set of overloaded declarations, some of which are
15167 templates, is a template-name. However, such a name should
15168 be a template-name; otherwise, there is no way to form a
15169 template-id for the overloaded templates. */
15170 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15171 if (TREE_CODE (fns) == OVERLOAD)
15172 for (fn = fns; fn; fn = OVL_NEXT (fn))
15173 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15174 break;
15176 if (!fn)
15178 /* The name does not name a template. */
15179 cp_parser_error (parser, "expected template-name");
15180 return error_mark_node;
15184 /* If DECL is dependent, and refers to a function, then just return
15185 its name; we will look it up again during template instantiation. */
15186 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15188 tree scope = ovl_scope (decl);
15189 if (TYPE_P (scope) && dependent_type_p (scope))
15190 return identifier;
15193 return decl;
15196 /* Parse a template-argument-list.
15198 template-argument-list:
15199 template-argument ... [opt]
15200 template-argument-list , template-argument ... [opt]
15202 Returns a TREE_VEC containing the arguments. */
15204 static tree
15205 cp_parser_template_argument_list (cp_parser* parser)
15207 tree fixed_args[10];
15208 unsigned n_args = 0;
15209 unsigned alloced = 10;
15210 tree *arg_ary = fixed_args;
15211 tree vec;
15212 bool saved_in_template_argument_list_p;
15213 bool saved_ice_p;
15214 bool saved_non_ice_p;
15216 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15217 parser->in_template_argument_list_p = true;
15218 /* Even if the template-id appears in an integral
15219 constant-expression, the contents of the argument list do
15220 not. */
15221 saved_ice_p = parser->integral_constant_expression_p;
15222 parser->integral_constant_expression_p = false;
15223 saved_non_ice_p = parser->non_integral_constant_expression_p;
15224 parser->non_integral_constant_expression_p = false;
15226 /* Parse the arguments. */
15229 tree argument;
15231 if (n_args)
15232 /* Consume the comma. */
15233 cp_lexer_consume_token (parser->lexer);
15235 /* Parse the template-argument. */
15236 argument = cp_parser_template_argument (parser);
15238 /* If the next token is an ellipsis, we're expanding a template
15239 argument pack. */
15240 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15242 if (argument == error_mark_node)
15244 cp_token *token = cp_lexer_peek_token (parser->lexer);
15245 error_at (token->location,
15246 "expected parameter pack before %<...%>");
15248 /* Consume the `...' token. */
15249 cp_lexer_consume_token (parser->lexer);
15251 /* Make the argument into a TYPE_PACK_EXPANSION or
15252 EXPR_PACK_EXPANSION. */
15253 argument = make_pack_expansion (argument);
15256 if (n_args == alloced)
15258 alloced *= 2;
15260 if (arg_ary == fixed_args)
15262 arg_ary = XNEWVEC (tree, alloced);
15263 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15265 else
15266 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15268 arg_ary[n_args++] = argument;
15270 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15272 vec = make_tree_vec (n_args);
15274 while (n_args--)
15275 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15277 if (arg_ary != fixed_args)
15278 free (arg_ary);
15279 parser->non_integral_constant_expression_p = saved_non_ice_p;
15280 parser->integral_constant_expression_p = saved_ice_p;
15281 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15282 if (CHECKING_P)
15283 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15284 return vec;
15287 /* Parse a template-argument.
15289 template-argument:
15290 assignment-expression
15291 type-id
15292 id-expression
15294 The representation is that of an assignment-expression, type-id, or
15295 id-expression -- except that the qualified id-expression is
15296 evaluated, so that the value returned is either a DECL or an
15297 OVERLOAD.
15299 Although the standard says "assignment-expression", it forbids
15300 throw-expressions or assignments in the template argument.
15301 Therefore, we use "conditional-expression" instead. */
15303 static tree
15304 cp_parser_template_argument (cp_parser* parser)
15306 tree argument;
15307 bool template_p;
15308 bool address_p;
15309 bool maybe_type_id = false;
15310 cp_token *token = NULL, *argument_start_token = NULL;
15311 location_t loc = 0;
15312 cp_id_kind idk;
15314 /* There's really no way to know what we're looking at, so we just
15315 try each alternative in order.
15317 [temp.arg]
15319 In a template-argument, an ambiguity between a type-id and an
15320 expression is resolved to a type-id, regardless of the form of
15321 the corresponding template-parameter.
15323 Therefore, we try a type-id first. */
15324 cp_parser_parse_tentatively (parser);
15325 argument = cp_parser_template_type_arg (parser);
15326 /* If there was no error parsing the type-id but the next token is a
15327 '>>', our behavior depends on which dialect of C++ we're
15328 parsing. In C++98, we probably found a typo for '> >'. But there
15329 are type-id which are also valid expressions. For instance:
15331 struct X { int operator >> (int); };
15332 template <int V> struct Foo {};
15333 Foo<X () >> 5> r;
15335 Here 'X()' is a valid type-id of a function type, but the user just
15336 wanted to write the expression "X() >> 5". Thus, we remember that we
15337 found a valid type-id, but we still try to parse the argument as an
15338 expression to see what happens.
15340 In C++0x, the '>>' will be considered two separate '>'
15341 tokens. */
15342 if (!cp_parser_error_occurred (parser)
15343 && cxx_dialect == cxx98
15344 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15346 maybe_type_id = true;
15347 cp_parser_abort_tentative_parse (parser);
15349 else
15351 /* If the next token isn't a `,' or a `>', then this argument wasn't
15352 really finished. This means that the argument is not a valid
15353 type-id. */
15354 if (!cp_parser_next_token_ends_template_argument_p (parser))
15355 cp_parser_error (parser, "expected template-argument");
15356 /* If that worked, we're done. */
15357 if (cp_parser_parse_definitely (parser))
15358 return argument;
15360 /* We're still not sure what the argument will be. */
15361 cp_parser_parse_tentatively (parser);
15362 /* Try a template. */
15363 argument_start_token = cp_lexer_peek_token (parser->lexer);
15364 argument = cp_parser_id_expression (parser,
15365 /*template_keyword_p=*/false,
15366 /*check_dependency_p=*/true,
15367 &template_p,
15368 /*declarator_p=*/false,
15369 /*optional_p=*/false);
15370 /* If the next token isn't a `,' or a `>', then this argument wasn't
15371 really finished. */
15372 if (!cp_parser_next_token_ends_template_argument_p (parser))
15373 cp_parser_error (parser, "expected template-argument");
15374 if (!cp_parser_error_occurred (parser))
15376 /* Figure out what is being referred to. If the id-expression
15377 was for a class template specialization, then we will have a
15378 TYPE_DECL at this point. There is no need to do name lookup
15379 at this point in that case. */
15380 if (TREE_CODE (argument) != TYPE_DECL)
15381 argument = cp_parser_lookup_name (parser, argument,
15382 none_type,
15383 /*is_template=*/template_p,
15384 /*is_namespace=*/false,
15385 /*check_dependency=*/true,
15386 /*ambiguous_decls=*/NULL,
15387 argument_start_token->location);
15388 /* Handle a constrained-type-specifier for a non-type template
15389 parameter. */
15390 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15391 argument = decl;
15392 else if (TREE_CODE (argument) != TEMPLATE_DECL
15393 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15394 cp_parser_error (parser, "expected template-name");
15396 if (cp_parser_parse_definitely (parser))
15398 if (TREE_DEPRECATED (argument))
15399 warn_deprecated_use (argument, NULL_TREE);
15400 return argument;
15402 /* It must be a non-type argument. In C++17 any constant-expression is
15403 allowed. */
15404 if (cxx_dialect > cxx14)
15405 goto general_expr;
15407 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15409 -- an integral constant-expression of integral or enumeration
15410 type; or
15412 -- the name of a non-type template-parameter; or
15414 -- the name of an object or function with external linkage...
15416 -- the address of an object or function with external linkage...
15418 -- a pointer to member... */
15419 /* Look for a non-type template parameter. */
15420 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15422 cp_parser_parse_tentatively (parser);
15423 argument = cp_parser_primary_expression (parser,
15424 /*address_p=*/false,
15425 /*cast_p=*/false,
15426 /*template_arg_p=*/true,
15427 &idk);
15428 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15429 || !cp_parser_next_token_ends_template_argument_p (parser))
15430 cp_parser_simulate_error (parser);
15431 if (cp_parser_parse_definitely (parser))
15432 return argument;
15435 /* If the next token is "&", the argument must be the address of an
15436 object or function with external linkage. */
15437 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15438 if (address_p)
15440 loc = cp_lexer_peek_token (parser->lexer)->location;
15441 cp_lexer_consume_token (parser->lexer);
15443 /* See if we might have an id-expression. */
15444 token = cp_lexer_peek_token (parser->lexer);
15445 if (token->type == CPP_NAME
15446 || token->keyword == RID_OPERATOR
15447 || token->type == CPP_SCOPE
15448 || token->type == CPP_TEMPLATE_ID
15449 || token->type == CPP_NESTED_NAME_SPECIFIER)
15451 cp_parser_parse_tentatively (parser);
15452 argument = cp_parser_primary_expression (parser,
15453 address_p,
15454 /*cast_p=*/false,
15455 /*template_arg_p=*/true,
15456 &idk);
15457 if (cp_parser_error_occurred (parser)
15458 || !cp_parser_next_token_ends_template_argument_p (parser))
15459 cp_parser_abort_tentative_parse (parser);
15460 else
15462 tree probe;
15464 if (INDIRECT_REF_P (argument))
15466 /* Strip the dereference temporarily. */
15467 gcc_assert (REFERENCE_REF_P (argument));
15468 argument = TREE_OPERAND (argument, 0);
15471 /* If we're in a template, we represent a qualified-id referring
15472 to a static data member as a SCOPE_REF even if the scope isn't
15473 dependent so that we can check access control later. */
15474 probe = argument;
15475 if (TREE_CODE (probe) == SCOPE_REF)
15476 probe = TREE_OPERAND (probe, 1);
15477 if (VAR_P (probe))
15479 /* A variable without external linkage might still be a
15480 valid constant-expression, so no error is issued here
15481 if the external-linkage check fails. */
15482 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15483 cp_parser_simulate_error (parser);
15485 else if (is_overloaded_fn (argument))
15486 /* All overloaded functions are allowed; if the external
15487 linkage test does not pass, an error will be issued
15488 later. */
15490 else if (address_p
15491 && (TREE_CODE (argument) == OFFSET_REF
15492 || TREE_CODE (argument) == SCOPE_REF))
15493 /* A pointer-to-member. */
15495 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15497 else
15498 cp_parser_simulate_error (parser);
15500 if (cp_parser_parse_definitely (parser))
15502 if (address_p)
15503 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15504 tf_warning_or_error);
15505 else
15506 argument = convert_from_reference (argument);
15507 return argument;
15511 /* If the argument started with "&", there are no other valid
15512 alternatives at this point. */
15513 if (address_p)
15515 cp_parser_error (parser, "invalid non-type template argument");
15516 return error_mark_node;
15519 general_expr:
15520 /* If the argument wasn't successfully parsed as a type-id followed
15521 by '>>', the argument can only be a constant expression now.
15522 Otherwise, we try parsing the constant-expression tentatively,
15523 because the argument could really be a type-id. */
15524 if (maybe_type_id)
15525 cp_parser_parse_tentatively (parser);
15527 if (cxx_dialect <= cxx14)
15528 argument = cp_parser_constant_expression (parser);
15529 else
15531 /* With C++17 generalized non-type template arguments we need to handle
15532 lvalue constant expressions, too. */
15533 argument = cp_parser_assignment_expression (parser);
15534 require_potential_constant_expression (argument);
15537 if (!maybe_type_id)
15538 return argument;
15539 if (!cp_parser_next_token_ends_template_argument_p (parser))
15540 cp_parser_error (parser, "expected template-argument");
15541 if (cp_parser_parse_definitely (parser))
15542 return argument;
15543 /* We did our best to parse the argument as a non type-id, but that
15544 was the only alternative that matched (albeit with a '>' after
15545 it). We can assume it's just a typo from the user, and a
15546 diagnostic will then be issued. */
15547 return cp_parser_template_type_arg (parser);
15550 /* Parse an explicit-instantiation.
15552 explicit-instantiation:
15553 template declaration
15555 Although the standard says `declaration', what it really means is:
15557 explicit-instantiation:
15558 template decl-specifier-seq [opt] declarator [opt] ;
15560 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15561 supposed to be allowed. A defect report has been filed about this
15562 issue.
15564 GNU Extension:
15566 explicit-instantiation:
15567 storage-class-specifier template
15568 decl-specifier-seq [opt] declarator [opt] ;
15569 function-specifier template
15570 decl-specifier-seq [opt] declarator [opt] ; */
15572 static void
15573 cp_parser_explicit_instantiation (cp_parser* parser)
15575 int declares_class_or_enum;
15576 cp_decl_specifier_seq decl_specifiers;
15577 tree extension_specifier = NULL_TREE;
15579 timevar_push (TV_TEMPLATE_INST);
15581 /* Look for an (optional) storage-class-specifier or
15582 function-specifier. */
15583 if (cp_parser_allow_gnu_extensions_p (parser))
15585 extension_specifier
15586 = cp_parser_storage_class_specifier_opt (parser);
15587 if (!extension_specifier)
15588 extension_specifier
15589 = cp_parser_function_specifier_opt (parser,
15590 /*decl_specs=*/NULL);
15593 /* Look for the `template' keyword. */
15594 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15595 /* Let the front end know that we are processing an explicit
15596 instantiation. */
15597 begin_explicit_instantiation ();
15598 /* [temp.explicit] says that we are supposed to ignore access
15599 control while processing explicit instantiation directives. */
15600 push_deferring_access_checks (dk_no_check);
15601 /* Parse a decl-specifier-seq. */
15602 cp_parser_decl_specifier_seq (parser,
15603 CP_PARSER_FLAGS_OPTIONAL,
15604 &decl_specifiers,
15605 &declares_class_or_enum);
15606 /* If there was exactly one decl-specifier, and it declared a class,
15607 and there's no declarator, then we have an explicit type
15608 instantiation. */
15609 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15611 tree type;
15613 type = check_tag_decl (&decl_specifiers,
15614 /*explicit_type_instantiation_p=*/true);
15615 /* Turn access control back on for names used during
15616 template instantiation. */
15617 pop_deferring_access_checks ();
15618 if (type)
15619 do_type_instantiation (type, extension_specifier,
15620 /*complain=*/tf_error);
15622 else
15624 cp_declarator *declarator;
15625 tree decl;
15627 /* Parse the declarator. */
15628 declarator
15629 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15630 /*ctor_dtor_or_conv_p=*/NULL,
15631 /*parenthesized_p=*/NULL,
15632 /*member_p=*/false,
15633 /*friend_p=*/false);
15634 if (declares_class_or_enum & 2)
15635 cp_parser_check_for_definition_in_return_type (declarator,
15636 decl_specifiers.type,
15637 decl_specifiers.locations[ds_type_spec]);
15638 if (declarator != cp_error_declarator)
15640 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15641 permerror (decl_specifiers.locations[ds_inline],
15642 "explicit instantiation shall not use"
15643 " %<inline%> specifier");
15644 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15645 permerror (decl_specifiers.locations[ds_constexpr],
15646 "explicit instantiation shall not use"
15647 " %<constexpr%> specifier");
15649 decl = grokdeclarator (declarator, &decl_specifiers,
15650 NORMAL, 0, &decl_specifiers.attributes);
15651 /* Turn access control back on for names used during
15652 template instantiation. */
15653 pop_deferring_access_checks ();
15654 /* Do the explicit instantiation. */
15655 do_decl_instantiation (decl, extension_specifier);
15657 else
15659 pop_deferring_access_checks ();
15660 /* Skip the body of the explicit instantiation. */
15661 cp_parser_skip_to_end_of_statement (parser);
15664 /* We're done with the instantiation. */
15665 end_explicit_instantiation ();
15667 cp_parser_consume_semicolon_at_end_of_statement (parser);
15669 timevar_pop (TV_TEMPLATE_INST);
15672 /* Parse an explicit-specialization.
15674 explicit-specialization:
15675 template < > declaration
15677 Although the standard says `declaration', what it really means is:
15679 explicit-specialization:
15680 template <> decl-specifier [opt] init-declarator [opt] ;
15681 template <> function-definition
15682 template <> explicit-specialization
15683 template <> template-declaration */
15685 static void
15686 cp_parser_explicit_specialization (cp_parser* parser)
15688 bool need_lang_pop;
15689 cp_token *token = cp_lexer_peek_token (parser->lexer);
15691 /* Look for the `template' keyword. */
15692 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15693 /* Look for the `<'. */
15694 cp_parser_require (parser, CPP_LESS, RT_LESS);
15695 /* Look for the `>'. */
15696 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15697 /* We have processed another parameter list. */
15698 ++parser->num_template_parameter_lists;
15699 /* [temp]
15701 A template ... explicit specialization ... shall not have C
15702 linkage. */
15703 if (current_lang_name == lang_name_c)
15705 error_at (token->location, "template specialization with C linkage");
15706 /* Give it C++ linkage to avoid confusing other parts of the
15707 front end. */
15708 push_lang_context (lang_name_cplusplus);
15709 need_lang_pop = true;
15711 else
15712 need_lang_pop = false;
15713 /* Let the front end know that we are beginning a specialization. */
15714 if (!begin_specialization ())
15716 end_specialization ();
15717 return;
15720 /* If the next keyword is `template', we need to figure out whether
15721 or not we're looking a template-declaration. */
15722 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15724 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15725 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15726 cp_parser_template_declaration_after_export (parser,
15727 /*member_p=*/false);
15728 else
15729 cp_parser_explicit_specialization (parser);
15731 else
15732 /* Parse the dependent declaration. */
15733 cp_parser_single_declaration (parser,
15734 /*checks=*/NULL,
15735 /*member_p=*/false,
15736 /*explicit_specialization_p=*/true,
15737 /*friend_p=*/NULL);
15738 /* We're done with the specialization. */
15739 end_specialization ();
15740 /* For the erroneous case of a template with C linkage, we pushed an
15741 implicit C++ linkage scope; exit that scope now. */
15742 if (need_lang_pop)
15743 pop_lang_context ();
15744 /* We're done with this parameter list. */
15745 --parser->num_template_parameter_lists;
15748 /* Parse a type-specifier.
15750 type-specifier:
15751 simple-type-specifier
15752 class-specifier
15753 enum-specifier
15754 elaborated-type-specifier
15755 cv-qualifier
15757 GNU Extension:
15759 type-specifier:
15760 __complex__
15762 Returns a representation of the type-specifier. For a
15763 class-specifier, enum-specifier, or elaborated-type-specifier, a
15764 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15766 The parser flags FLAGS is used to control type-specifier parsing.
15768 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15769 in a decl-specifier-seq.
15771 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15772 class-specifier, enum-specifier, or elaborated-type-specifier, then
15773 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15774 if a type is declared; 2 if it is defined. Otherwise, it is set to
15775 zero.
15777 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15778 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15779 is set to FALSE. */
15781 static tree
15782 cp_parser_type_specifier (cp_parser* parser,
15783 cp_parser_flags flags,
15784 cp_decl_specifier_seq *decl_specs,
15785 bool is_declaration,
15786 int* declares_class_or_enum,
15787 bool* is_cv_qualifier)
15789 tree type_spec = NULL_TREE;
15790 cp_token *token;
15791 enum rid keyword;
15792 cp_decl_spec ds = ds_last;
15794 /* Assume this type-specifier does not declare a new type. */
15795 if (declares_class_or_enum)
15796 *declares_class_or_enum = 0;
15797 /* And that it does not specify a cv-qualifier. */
15798 if (is_cv_qualifier)
15799 *is_cv_qualifier = false;
15800 /* Peek at the next token. */
15801 token = cp_lexer_peek_token (parser->lexer);
15803 /* If we're looking at a keyword, we can use that to guide the
15804 production we choose. */
15805 keyword = token->keyword;
15806 switch (keyword)
15808 case RID_ENUM:
15809 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15810 goto elaborated_type_specifier;
15812 /* Look for the enum-specifier. */
15813 type_spec = cp_parser_enum_specifier (parser);
15814 /* If that worked, we're done. */
15815 if (type_spec)
15817 if (declares_class_or_enum)
15818 *declares_class_or_enum = 2;
15819 if (decl_specs)
15820 cp_parser_set_decl_spec_type (decl_specs,
15821 type_spec,
15822 token,
15823 /*type_definition_p=*/true);
15824 return type_spec;
15826 else
15827 goto elaborated_type_specifier;
15829 /* Any of these indicate either a class-specifier, or an
15830 elaborated-type-specifier. */
15831 case RID_CLASS:
15832 case RID_STRUCT:
15833 case RID_UNION:
15834 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15835 goto elaborated_type_specifier;
15837 /* Parse tentatively so that we can back up if we don't find a
15838 class-specifier. */
15839 cp_parser_parse_tentatively (parser);
15840 /* Look for the class-specifier. */
15841 type_spec = cp_parser_class_specifier (parser);
15842 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15843 /* If that worked, we're done. */
15844 if (cp_parser_parse_definitely (parser))
15846 if (declares_class_or_enum)
15847 *declares_class_or_enum = 2;
15848 if (decl_specs)
15849 cp_parser_set_decl_spec_type (decl_specs,
15850 type_spec,
15851 token,
15852 /*type_definition_p=*/true);
15853 return type_spec;
15856 /* Fall through. */
15857 elaborated_type_specifier:
15858 /* We're declaring (not defining) a class or enum. */
15859 if (declares_class_or_enum)
15860 *declares_class_or_enum = 1;
15862 /* Fall through. */
15863 case RID_TYPENAME:
15864 /* Look for an elaborated-type-specifier. */
15865 type_spec
15866 = (cp_parser_elaborated_type_specifier
15867 (parser,
15868 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15869 is_declaration));
15870 if (decl_specs)
15871 cp_parser_set_decl_spec_type (decl_specs,
15872 type_spec,
15873 token,
15874 /*type_definition_p=*/false);
15875 return type_spec;
15877 case RID_CONST:
15878 ds = ds_const;
15879 if (is_cv_qualifier)
15880 *is_cv_qualifier = true;
15881 break;
15883 case RID_VOLATILE:
15884 ds = ds_volatile;
15885 if (is_cv_qualifier)
15886 *is_cv_qualifier = true;
15887 break;
15889 case RID_RESTRICT:
15890 ds = ds_restrict;
15891 if (is_cv_qualifier)
15892 *is_cv_qualifier = true;
15893 break;
15895 case RID_COMPLEX:
15896 /* The `__complex__' keyword is a GNU extension. */
15897 ds = ds_complex;
15898 break;
15900 default:
15901 break;
15904 /* Handle simple keywords. */
15905 if (ds != ds_last)
15907 if (decl_specs)
15909 set_and_check_decl_spec_loc (decl_specs, ds, token);
15910 decl_specs->any_specifiers_p = true;
15912 return cp_lexer_consume_token (parser->lexer)->u.value;
15915 /* If we do not already have a type-specifier, assume we are looking
15916 at a simple-type-specifier. */
15917 type_spec = cp_parser_simple_type_specifier (parser,
15918 decl_specs,
15919 flags);
15921 /* If we didn't find a type-specifier, and a type-specifier was not
15922 optional in this context, issue an error message. */
15923 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15925 cp_parser_error (parser, "expected type specifier");
15926 return error_mark_node;
15929 return type_spec;
15932 /* Parse a simple-type-specifier.
15934 simple-type-specifier:
15935 :: [opt] nested-name-specifier [opt] type-name
15936 :: [opt] nested-name-specifier template template-id
15937 char
15938 wchar_t
15939 bool
15940 short
15942 long
15943 signed
15944 unsigned
15945 float
15946 double
15947 void
15949 C++0x Extension:
15951 simple-type-specifier:
15952 auto
15953 decltype ( expression )
15954 char16_t
15955 char32_t
15956 __underlying_type ( type-id )
15958 GNU Extension:
15960 simple-type-specifier:
15961 __int128
15962 __typeof__ unary-expression
15963 __typeof__ ( type-id )
15964 __typeof__ ( type-id ) { initializer-list , [opt] }
15966 Concepts Extension:
15968 simple-type-specifier:
15969 constrained-type-specifier
15971 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15972 appropriately updated. */
15974 static tree
15975 cp_parser_simple_type_specifier (cp_parser* parser,
15976 cp_decl_specifier_seq *decl_specs,
15977 cp_parser_flags flags)
15979 tree type = NULL_TREE;
15980 cp_token *token;
15981 int idx;
15983 /* Peek at the next token. */
15984 token = cp_lexer_peek_token (parser->lexer);
15986 /* If we're looking at a keyword, things are easy. */
15987 switch (token->keyword)
15989 case RID_CHAR:
15990 if (decl_specs)
15991 decl_specs->explicit_char_p = true;
15992 type = char_type_node;
15993 break;
15994 case RID_CHAR16:
15995 type = char16_type_node;
15996 break;
15997 case RID_CHAR32:
15998 type = char32_type_node;
15999 break;
16000 case RID_WCHAR:
16001 type = wchar_type_node;
16002 break;
16003 case RID_BOOL:
16004 type = boolean_type_node;
16005 break;
16006 case RID_SHORT:
16007 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16008 type = short_integer_type_node;
16009 break;
16010 case RID_INT:
16011 if (decl_specs)
16012 decl_specs->explicit_int_p = true;
16013 type = integer_type_node;
16014 break;
16015 case RID_INT_N_0:
16016 case RID_INT_N_1:
16017 case RID_INT_N_2:
16018 case RID_INT_N_3:
16019 idx = token->keyword - RID_INT_N_0;
16020 if (! int_n_enabled_p [idx])
16021 break;
16022 if (decl_specs)
16024 decl_specs->explicit_intN_p = true;
16025 decl_specs->int_n_idx = idx;
16027 type = int_n_trees [idx].signed_type;
16028 break;
16029 case RID_LONG:
16030 if (decl_specs)
16031 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16032 type = long_integer_type_node;
16033 break;
16034 case RID_SIGNED:
16035 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16036 type = integer_type_node;
16037 break;
16038 case RID_UNSIGNED:
16039 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16040 type = unsigned_type_node;
16041 break;
16042 case RID_FLOAT:
16043 type = float_type_node;
16044 break;
16045 case RID_DOUBLE:
16046 type = double_type_node;
16047 break;
16048 case RID_VOID:
16049 type = void_type_node;
16050 break;
16052 case RID_AUTO:
16053 maybe_warn_cpp0x (CPP0X_AUTO);
16054 if (parser->auto_is_implicit_function_template_parm_p)
16056 /* The 'auto' might be the placeholder return type for a function decl
16057 with trailing return type. */
16058 bool have_trailing_return_fn_decl = false;
16060 cp_parser_parse_tentatively (parser);
16061 cp_lexer_consume_token (parser->lexer);
16062 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16063 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16064 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16065 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16067 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16069 cp_lexer_consume_token (parser->lexer);
16070 cp_parser_skip_to_closing_parenthesis (parser,
16071 /*recovering*/false,
16072 /*or_comma*/false,
16073 /*consume_paren*/true);
16074 continue;
16077 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16079 have_trailing_return_fn_decl = true;
16080 break;
16083 cp_lexer_consume_token (parser->lexer);
16085 cp_parser_abort_tentative_parse (parser);
16087 if (have_trailing_return_fn_decl)
16089 type = make_auto ();
16090 break;
16093 if (cxx_dialect >= cxx14)
16095 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16096 type = TREE_TYPE (type);
16098 else
16099 type = error_mark_node;
16101 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16103 if (cxx_dialect < cxx14)
16104 error_at (token->location,
16105 "use of %<auto%> in lambda parameter declaration "
16106 "only available with "
16107 "-std=c++14 or -std=gnu++14");
16109 else if (cxx_dialect < cxx14)
16110 error_at (token->location,
16111 "use of %<auto%> in parameter declaration "
16112 "only available with "
16113 "-std=c++14 or -std=gnu++14");
16114 else if (!flag_concepts)
16115 pedwarn (token->location, OPT_Wpedantic,
16116 "ISO C++ forbids use of %<auto%> in parameter "
16117 "declaration");
16119 else
16120 type = make_auto ();
16121 break;
16123 case RID_DECLTYPE:
16124 /* Since DR 743, decltype can either be a simple-type-specifier by
16125 itself or begin a nested-name-specifier. Parsing it will replace
16126 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16127 handling below decide what to do. */
16128 cp_parser_decltype (parser);
16129 cp_lexer_set_token_position (parser->lexer, token);
16130 break;
16132 case RID_TYPEOF:
16133 /* Consume the `typeof' token. */
16134 cp_lexer_consume_token (parser->lexer);
16135 /* Parse the operand to `typeof'. */
16136 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16137 /* If it is not already a TYPE, take its type. */
16138 if (!TYPE_P (type))
16139 type = finish_typeof (type);
16141 if (decl_specs)
16142 cp_parser_set_decl_spec_type (decl_specs, type,
16143 token,
16144 /*type_definition_p=*/false);
16146 return type;
16148 case RID_UNDERLYING_TYPE:
16149 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16150 if (decl_specs)
16151 cp_parser_set_decl_spec_type (decl_specs, type,
16152 token,
16153 /*type_definition_p=*/false);
16155 return type;
16157 case RID_BASES:
16158 case RID_DIRECT_BASES:
16159 type = cp_parser_trait_expr (parser, token->keyword);
16160 if (decl_specs)
16161 cp_parser_set_decl_spec_type (decl_specs, type,
16162 token,
16163 /*type_definition_p=*/false);
16164 return type;
16165 default:
16166 break;
16169 /* If token is an already-parsed decltype not followed by ::,
16170 it's a simple-type-specifier. */
16171 if (token->type == CPP_DECLTYPE
16172 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16174 type = saved_checks_value (token->u.tree_check_value);
16175 if (decl_specs)
16177 cp_parser_set_decl_spec_type (decl_specs, type,
16178 token,
16179 /*type_definition_p=*/false);
16180 /* Remember that we are handling a decltype in order to
16181 implement the resolution of DR 1510 when the argument
16182 isn't instantiation dependent. */
16183 decl_specs->decltype_p = true;
16185 cp_lexer_consume_token (parser->lexer);
16186 return type;
16189 /* If the type-specifier was for a built-in type, we're done. */
16190 if (type)
16192 /* Record the type. */
16193 if (decl_specs
16194 && (token->keyword != RID_SIGNED
16195 && token->keyword != RID_UNSIGNED
16196 && token->keyword != RID_SHORT
16197 && token->keyword != RID_LONG))
16198 cp_parser_set_decl_spec_type (decl_specs,
16199 type,
16200 token,
16201 /*type_definition_p=*/false);
16202 if (decl_specs)
16203 decl_specs->any_specifiers_p = true;
16205 /* Consume the token. */
16206 cp_lexer_consume_token (parser->lexer);
16208 if (type == error_mark_node)
16209 return error_mark_node;
16211 /* There is no valid C++ program where a non-template type is
16212 followed by a "<". That usually indicates that the user thought
16213 that the type was a template. */
16214 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16215 token->location);
16217 return TYPE_NAME (type);
16220 /* The type-specifier must be a user-defined type. */
16221 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16223 bool qualified_p;
16224 bool global_p;
16226 /* Don't gobble tokens or issue error messages if this is an
16227 optional type-specifier. */
16228 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16229 cp_parser_parse_tentatively (parser);
16231 /* Look for the optional `::' operator. */
16232 global_p
16233 = (cp_parser_global_scope_opt (parser,
16234 /*current_scope_valid_p=*/false)
16235 != NULL_TREE);
16236 /* Look for the nested-name specifier. */
16237 qualified_p
16238 = (cp_parser_nested_name_specifier_opt (parser,
16239 /*typename_keyword_p=*/false,
16240 /*check_dependency_p=*/true,
16241 /*type_p=*/false,
16242 /*is_declaration=*/false)
16243 != NULL_TREE);
16244 token = cp_lexer_peek_token (parser->lexer);
16245 /* If we have seen a nested-name-specifier, and the next token
16246 is `template', then we are using the template-id production. */
16247 if (parser->scope
16248 && cp_parser_optional_template_keyword (parser))
16250 /* Look for the template-id. */
16251 type = cp_parser_template_id (parser,
16252 /*template_keyword_p=*/true,
16253 /*check_dependency_p=*/true,
16254 none_type,
16255 /*is_declaration=*/false);
16256 /* If the template-id did not name a type, we are out of
16257 luck. */
16258 if (TREE_CODE (type) != TYPE_DECL)
16260 cp_parser_error (parser, "expected template-id for type");
16261 type = NULL_TREE;
16264 /* Otherwise, look for a type-name. */
16265 else
16266 type = cp_parser_type_name (parser);
16267 /* Keep track of all name-lookups performed in class scopes. */
16268 if (type
16269 && !global_p
16270 && !qualified_p
16271 && TREE_CODE (type) == TYPE_DECL
16272 && identifier_p (DECL_NAME (type)))
16273 maybe_note_name_used_in_class (DECL_NAME (type), type);
16274 /* If it didn't work out, we don't have a TYPE. */
16275 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16276 && !cp_parser_parse_definitely (parser))
16277 type = NULL_TREE;
16278 if (type && decl_specs)
16279 cp_parser_set_decl_spec_type (decl_specs, type,
16280 token,
16281 /*type_definition_p=*/false);
16284 /* If we didn't get a type-name, issue an error message. */
16285 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16287 cp_parser_error (parser, "expected type-name");
16288 return error_mark_node;
16291 if (type && type != error_mark_node)
16293 /* See if TYPE is an Objective-C type, and if so, parse and
16294 accept any protocol references following it. Do this before
16295 the cp_parser_check_for_invalid_template_id() call, because
16296 Objective-C types can be followed by '<...>' which would
16297 enclose protocol names rather than template arguments, and so
16298 everything is fine. */
16299 if (c_dialect_objc () && !parser->scope
16300 && (objc_is_id (type) || objc_is_class_name (type)))
16302 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16303 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16305 /* Clobber the "unqualified" type previously entered into
16306 DECL_SPECS with the new, improved protocol-qualified version. */
16307 if (decl_specs)
16308 decl_specs->type = qual_type;
16310 return qual_type;
16313 /* There is no valid C++ program where a non-template type is
16314 followed by a "<". That usually indicates that the user
16315 thought that the type was a template. */
16316 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16317 none_type,
16318 token->location);
16321 return type;
16324 /* Parse a type-name.
16326 type-name:
16327 class-name
16328 enum-name
16329 typedef-name
16330 simple-template-id [in c++0x]
16332 enum-name:
16333 identifier
16335 typedef-name:
16336 identifier
16338 Concepts:
16340 type-name:
16341 concept-name
16342 partial-concept-id
16344 concept-name:
16345 identifier
16347 Returns a TYPE_DECL for the type. */
16349 static tree
16350 cp_parser_type_name (cp_parser* parser)
16352 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16355 /* See above. */
16356 static tree
16357 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16359 tree type_decl;
16361 /* We can't know yet whether it is a class-name or not. */
16362 cp_parser_parse_tentatively (parser);
16363 /* Try a class-name. */
16364 type_decl = cp_parser_class_name (parser,
16365 typename_keyword_p,
16366 /*template_keyword_p=*/false,
16367 none_type,
16368 /*check_dependency_p=*/true,
16369 /*class_head_p=*/false,
16370 /*is_declaration=*/false);
16371 /* If it's not a class-name, keep looking. */
16372 if (!cp_parser_parse_definitely (parser))
16374 if (cxx_dialect < cxx11)
16375 /* It must be a typedef-name or an enum-name. */
16376 return cp_parser_nonclass_name (parser);
16378 cp_parser_parse_tentatively (parser);
16379 /* It is either a simple-template-id representing an
16380 instantiation of an alias template... */
16381 type_decl = cp_parser_template_id (parser,
16382 /*template_keyword_p=*/false,
16383 /*check_dependency_p=*/true,
16384 none_type,
16385 /*is_declaration=*/false);
16386 /* Note that this must be an instantiation of an alias template
16387 because [temp.names]/6 says:
16389 A template-id that names an alias template specialization
16390 is a type-name.
16392 Whereas [temp.names]/7 says:
16394 A simple-template-id that names a class template
16395 specialization is a class-name.
16397 With concepts, this could also be a partial-concept-id that
16398 declares a non-type template parameter. */
16399 if (type_decl != NULL_TREE
16400 && TREE_CODE (type_decl) == TYPE_DECL
16401 && TYPE_DECL_ALIAS_P (type_decl))
16402 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16403 else if (is_constrained_parameter (type_decl))
16404 /* Don't do anything. */ ;
16405 else
16406 cp_parser_simulate_error (parser);
16408 if (!cp_parser_parse_definitely (parser))
16409 /* ... Or a typedef-name or an enum-name. */
16410 return cp_parser_nonclass_name (parser);
16413 return type_decl;
16416 /* Check if DECL and ARGS can form a constrained-type-specifier.
16417 If ARGS is non-null, we try to form a concept check of the
16418 form DECL<?, ARGS> where ? is a wildcard that matches any
16419 kind of template argument. If ARGS is NULL, then we try to
16420 form a concept check of the form DECL<?>. */
16422 static tree
16423 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16424 tree decl, tree args)
16426 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16428 /* If we a constrained-type-specifier cannot be deduced. */
16429 if (parser->prevent_constrained_type_specifiers)
16430 return NULL_TREE;
16432 /* A constrained type specifier can only be found in an
16433 overload set or as a reference to a template declaration.
16435 FIXME: This might be masking a bug. It's possible that
16436 that the deduction below is causing template specializations
16437 to be formed with the wildcard as an argument. */
16438 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16439 return NULL_TREE;
16441 /* Try to build a call expression that evaluates the
16442 concept. This can fail if the overload set refers
16443 only to non-templates. */
16444 tree placeholder = build_nt (WILDCARD_DECL);
16445 tree check = build_concept_check (decl, placeholder, args);
16446 if (check == error_mark_node)
16447 return NULL_TREE;
16449 /* Deduce the checked constraint and the prototype parameter.
16451 FIXME: In certain cases, failure to deduce should be a
16452 diagnosable error. */
16453 tree conc;
16454 tree proto;
16455 if (!deduce_constrained_parameter (check, conc, proto))
16456 return NULL_TREE;
16458 /* In template parameter scope, this results in a constrained
16459 parameter. Return a descriptor of that parm. */
16460 if (processing_template_parmlist)
16461 return build_constrained_parameter (conc, proto, args);
16463 /* In a parameter-declaration-clause, constrained-type
16464 specifiers result in invented template parameters. */
16465 if (parser->auto_is_implicit_function_template_parm_p)
16467 tree x = build_constrained_parameter (conc, proto, args);
16468 return synthesize_implicit_template_parm (parser, x);
16470 else
16472 /* Otherwise, we're in a context where the constrained
16473 type name is deduced and the constraint applies
16474 after deduction. */
16475 return make_constrained_auto (conc, args);
16478 return NULL_TREE;
16481 /* If DECL refers to a concept, return a TYPE_DECL representing
16482 the result of using the constrained type specifier in the
16483 current context. DECL refers to a concept if
16485 - it is an overload set containing a function concept taking a single
16486 type argument, or
16488 - it is a variable concept taking a single type argument. */
16490 static tree
16491 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16493 if (flag_concepts
16494 && (TREE_CODE (decl) == OVERLOAD
16495 || BASELINK_P (decl)
16496 || variable_concept_p (decl)))
16497 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16498 else
16499 return NULL_TREE;
16502 /* Check if DECL and ARGS form a partial-concept-id. If so,
16503 assign ID to the resulting constrained placeholder.
16505 Returns true if the partial-concept-id designates a placeholder
16506 and false otherwise. Note that *id is set to NULL_TREE in
16507 this case. */
16509 static tree
16510 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16512 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16515 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16516 or a concept-name.
16518 enum-name:
16519 identifier
16521 typedef-name:
16522 identifier
16524 concept-name:
16525 identifier
16527 Returns a TYPE_DECL for the type. */
16529 static tree
16530 cp_parser_nonclass_name (cp_parser* parser)
16532 tree type_decl;
16533 tree identifier;
16535 cp_token *token = cp_lexer_peek_token (parser->lexer);
16536 identifier = cp_parser_identifier (parser);
16537 if (identifier == error_mark_node)
16538 return error_mark_node;
16540 /* Look up the type-name. */
16541 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16543 type_decl = strip_using_decl (type_decl);
16545 /* If we found an overload set, then it may refer to a concept-name. */
16546 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16547 type_decl = decl;
16549 if (TREE_CODE (type_decl) != TYPE_DECL
16550 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16552 /* See if this is an Objective-C type. */
16553 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16554 tree type = objc_get_protocol_qualified_type (identifier, protos);
16555 if (type)
16556 type_decl = TYPE_NAME (type);
16559 /* Issue an error if we did not find a type-name. */
16560 if (TREE_CODE (type_decl) != TYPE_DECL
16561 /* In Objective-C, we have the complication that class names are
16562 normally type names and start declarations (eg, the
16563 "NSObject" in "NSObject *object;"), but can be used in an
16564 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16565 is an expression. So, a classname followed by a dot is not a
16566 valid type-name. */
16567 || (objc_is_class_name (TREE_TYPE (type_decl))
16568 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16570 if (!cp_parser_simulate_error (parser))
16571 cp_parser_name_lookup_error (parser, identifier, type_decl,
16572 NLE_TYPE, token->location);
16573 return error_mark_node;
16575 /* Remember that the name was used in the definition of the
16576 current class so that we can check later to see if the
16577 meaning would have been different after the class was
16578 entirely defined. */
16579 else if (type_decl != error_mark_node
16580 && !parser->scope)
16581 maybe_note_name_used_in_class (identifier, type_decl);
16583 return type_decl;
16586 /* Parse an elaborated-type-specifier. Note that the grammar given
16587 here incorporates the resolution to DR68.
16589 elaborated-type-specifier:
16590 class-key :: [opt] nested-name-specifier [opt] identifier
16591 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16592 enum-key :: [opt] nested-name-specifier [opt] identifier
16593 typename :: [opt] nested-name-specifier identifier
16594 typename :: [opt] nested-name-specifier template [opt]
16595 template-id
16597 GNU extension:
16599 elaborated-type-specifier:
16600 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16601 class-key attributes :: [opt] nested-name-specifier [opt]
16602 template [opt] template-id
16603 enum attributes :: [opt] nested-name-specifier [opt] identifier
16605 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16606 declared `friend'. If IS_DECLARATION is TRUE, then this
16607 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16608 something is being declared.
16610 Returns the TYPE specified. */
16612 static tree
16613 cp_parser_elaborated_type_specifier (cp_parser* parser,
16614 bool is_friend,
16615 bool is_declaration)
16617 enum tag_types tag_type;
16618 tree identifier;
16619 tree type = NULL_TREE;
16620 tree attributes = NULL_TREE;
16621 tree globalscope;
16622 cp_token *token = NULL;
16624 /* See if we're looking at the `enum' keyword. */
16625 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16627 /* Consume the `enum' token. */
16628 cp_lexer_consume_token (parser->lexer);
16629 /* Remember that it's an enumeration type. */
16630 tag_type = enum_type;
16631 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16632 enums) is used here. */
16633 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16634 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16636 pedwarn (input_location, 0, "elaborated-type-specifier "
16637 "for a scoped enum must not use the %<%D%> keyword",
16638 cp_lexer_peek_token (parser->lexer)->u.value);
16639 /* Consume the `struct' or `class' and parse it anyway. */
16640 cp_lexer_consume_token (parser->lexer);
16642 /* Parse the attributes. */
16643 attributes = cp_parser_attributes_opt (parser);
16645 /* Or, it might be `typename'. */
16646 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16647 RID_TYPENAME))
16649 /* Consume the `typename' token. */
16650 cp_lexer_consume_token (parser->lexer);
16651 /* Remember that it's a `typename' type. */
16652 tag_type = typename_type;
16654 /* Otherwise it must be a class-key. */
16655 else
16657 tag_type = cp_parser_class_key (parser);
16658 if (tag_type == none_type)
16659 return error_mark_node;
16660 /* Parse the attributes. */
16661 attributes = cp_parser_attributes_opt (parser);
16664 /* Look for the `::' operator. */
16665 globalscope = cp_parser_global_scope_opt (parser,
16666 /*current_scope_valid_p=*/false);
16667 /* Look for the nested-name-specifier. */
16668 if (tag_type == typename_type && !globalscope)
16670 if (!cp_parser_nested_name_specifier (parser,
16671 /*typename_keyword_p=*/true,
16672 /*check_dependency_p=*/true,
16673 /*type_p=*/true,
16674 is_declaration))
16675 return error_mark_node;
16677 else
16678 /* Even though `typename' is not present, the proposed resolution
16679 to Core Issue 180 says that in `class A<T>::B', `B' should be
16680 considered a type-name, even if `A<T>' is dependent. */
16681 cp_parser_nested_name_specifier_opt (parser,
16682 /*typename_keyword_p=*/true,
16683 /*check_dependency_p=*/true,
16684 /*type_p=*/true,
16685 is_declaration);
16686 /* For everything but enumeration types, consider a template-id.
16687 For an enumeration type, consider only a plain identifier. */
16688 if (tag_type != enum_type)
16690 bool template_p = false;
16691 tree decl;
16693 /* Allow the `template' keyword. */
16694 template_p = cp_parser_optional_template_keyword (parser);
16695 /* If we didn't see `template', we don't know if there's a
16696 template-id or not. */
16697 if (!template_p)
16698 cp_parser_parse_tentatively (parser);
16699 /* Parse the template-id. */
16700 token = cp_lexer_peek_token (parser->lexer);
16701 decl = cp_parser_template_id (parser, template_p,
16702 /*check_dependency_p=*/true,
16703 tag_type,
16704 is_declaration);
16705 /* If we didn't find a template-id, look for an ordinary
16706 identifier. */
16707 if (!template_p && !cp_parser_parse_definitely (parser))
16709 /* We can get here when cp_parser_template_id, called by
16710 cp_parser_class_name with tag_type == none_type, succeeds
16711 and caches a BASELINK. Then, when called again here,
16712 instead of failing and returning an error_mark_node
16713 returns it (see template/typename17.C in C++11).
16714 ??? Could we diagnose this earlier? */
16715 else if (tag_type == typename_type && BASELINK_P (decl))
16717 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16718 type = error_mark_node;
16720 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16721 in effect, then we must assume that, upon instantiation, the
16722 template will correspond to a class. */
16723 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16724 && tag_type == typename_type)
16725 type = make_typename_type (parser->scope, decl,
16726 typename_type,
16727 /*complain=*/tf_error);
16728 /* If the `typename' keyword is in effect and DECL is not a type
16729 decl, then type is non existent. */
16730 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16732 else if (TREE_CODE (decl) == TYPE_DECL)
16733 type = check_elaborated_type_specifier (tag_type, decl,
16734 /*allow_template_p=*/true);
16735 else if (decl == error_mark_node)
16736 type = error_mark_node;
16739 if (!type)
16741 token = cp_lexer_peek_token (parser->lexer);
16742 identifier = cp_parser_identifier (parser);
16744 if (identifier == error_mark_node)
16746 parser->scope = NULL_TREE;
16747 return error_mark_node;
16750 /* For a `typename', we needn't call xref_tag. */
16751 if (tag_type == typename_type
16752 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16753 return cp_parser_make_typename_type (parser, identifier,
16754 token->location);
16756 /* Template parameter lists apply only if we are not within a
16757 function parameter list. */
16758 bool template_parm_lists_apply
16759 = parser->num_template_parameter_lists;
16760 if (template_parm_lists_apply)
16761 for (cp_binding_level *s = current_binding_level;
16762 s && s->kind != sk_template_parms;
16763 s = s->level_chain)
16764 if (s->kind == sk_function_parms)
16765 template_parm_lists_apply = false;
16767 /* Look up a qualified name in the usual way. */
16768 if (parser->scope)
16770 tree decl;
16771 tree ambiguous_decls;
16773 decl = cp_parser_lookup_name (parser, identifier,
16774 tag_type,
16775 /*is_template=*/false,
16776 /*is_namespace=*/false,
16777 /*check_dependency=*/true,
16778 &ambiguous_decls,
16779 token->location);
16781 /* If the lookup was ambiguous, an error will already have been
16782 issued. */
16783 if (ambiguous_decls)
16784 return error_mark_node;
16786 /* If we are parsing friend declaration, DECL may be a
16787 TEMPLATE_DECL tree node here. However, we need to check
16788 whether this TEMPLATE_DECL results in valid code. Consider
16789 the following example:
16791 namespace N {
16792 template <class T> class C {};
16794 class X {
16795 template <class T> friend class N::C; // #1, valid code
16797 template <class T> class Y {
16798 friend class N::C; // #2, invalid code
16801 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16802 name lookup of `N::C'. We see that friend declaration must
16803 be template for the code to be valid. Note that
16804 processing_template_decl does not work here since it is
16805 always 1 for the above two cases. */
16807 decl = (cp_parser_maybe_treat_template_as_class
16808 (decl, /*tag_name_p=*/is_friend
16809 && template_parm_lists_apply));
16811 if (TREE_CODE (decl) != TYPE_DECL)
16813 cp_parser_diagnose_invalid_type_name (parser,
16814 identifier,
16815 token->location);
16816 return error_mark_node;
16819 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16821 bool allow_template = (template_parm_lists_apply
16822 || DECL_SELF_REFERENCE_P (decl));
16823 type = check_elaborated_type_specifier (tag_type, decl,
16824 allow_template);
16826 if (type == error_mark_node)
16827 return error_mark_node;
16830 /* Forward declarations of nested types, such as
16832 class C1::C2;
16833 class C1::C2::C3;
16835 are invalid unless all components preceding the final '::'
16836 are complete. If all enclosing types are complete, these
16837 declarations become merely pointless.
16839 Invalid forward declarations of nested types are errors
16840 caught elsewhere in parsing. Those that are pointless arrive
16841 here. */
16843 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16844 && !is_friend && !processing_explicit_instantiation)
16845 warning (0, "declaration %qD does not declare anything", decl);
16847 type = TREE_TYPE (decl);
16849 else
16851 /* An elaborated-type-specifier sometimes introduces a new type and
16852 sometimes names an existing type. Normally, the rule is that it
16853 introduces a new type only if there is not an existing type of
16854 the same name already in scope. For example, given:
16856 struct S {};
16857 void f() { struct S s; }
16859 the `struct S' in the body of `f' is the same `struct S' as in
16860 the global scope; the existing definition is used. However, if
16861 there were no global declaration, this would introduce a new
16862 local class named `S'.
16864 An exception to this rule applies to the following code:
16866 namespace N { struct S; }
16868 Here, the elaborated-type-specifier names a new type
16869 unconditionally; even if there is already an `S' in the
16870 containing scope this declaration names a new type.
16871 This exception only applies if the elaborated-type-specifier
16872 forms the complete declaration:
16874 [class.name]
16876 A declaration consisting solely of `class-key identifier ;' is
16877 either a redeclaration of the name in the current scope or a
16878 forward declaration of the identifier as a class name. It
16879 introduces the name into the current scope.
16881 We are in this situation precisely when the next token is a `;'.
16883 An exception to the exception is that a `friend' declaration does
16884 *not* name a new type; i.e., given:
16886 struct S { friend struct T; };
16888 `T' is not a new type in the scope of `S'.
16890 Also, `new struct S' or `sizeof (struct S)' never results in the
16891 definition of a new type; a new type can only be declared in a
16892 declaration context. */
16894 tag_scope ts;
16895 bool template_p;
16897 if (is_friend)
16898 /* Friends have special name lookup rules. */
16899 ts = ts_within_enclosing_non_class;
16900 else if (is_declaration
16901 && cp_lexer_next_token_is (parser->lexer,
16902 CPP_SEMICOLON))
16903 /* This is a `class-key identifier ;' */
16904 ts = ts_current;
16905 else
16906 ts = ts_global;
16908 template_p =
16909 (template_parm_lists_apply
16910 && (cp_parser_next_token_starts_class_definition_p (parser)
16911 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16912 /* An unqualified name was used to reference this type, so
16913 there were no qualifying templates. */
16914 if (template_parm_lists_apply
16915 && !cp_parser_check_template_parameters (parser,
16916 /*num_templates=*/0,
16917 token->location,
16918 /*declarator=*/NULL))
16919 return error_mark_node;
16920 type = xref_tag (tag_type, identifier, ts, template_p);
16924 if (type == error_mark_node)
16925 return error_mark_node;
16927 /* Allow attributes on forward declarations of classes. */
16928 if (attributes)
16930 if (TREE_CODE (type) == TYPENAME_TYPE)
16931 warning (OPT_Wattributes,
16932 "attributes ignored on uninstantiated type");
16933 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16934 && ! processing_explicit_instantiation)
16935 warning (OPT_Wattributes,
16936 "attributes ignored on template instantiation");
16937 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16938 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16939 else
16940 warning (OPT_Wattributes,
16941 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16944 if (tag_type != enum_type)
16946 /* Indicate whether this class was declared as a `class' or as a
16947 `struct'. */
16948 if (CLASS_TYPE_P (type))
16949 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16950 cp_parser_check_class_key (tag_type, type);
16953 /* A "<" cannot follow an elaborated type specifier. If that
16954 happens, the user was probably trying to form a template-id. */
16955 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16956 token->location);
16958 return type;
16961 /* Parse an enum-specifier.
16963 enum-specifier:
16964 enum-head { enumerator-list [opt] }
16965 enum-head { enumerator-list , } [C++0x]
16967 enum-head:
16968 enum-key identifier [opt] enum-base [opt]
16969 enum-key nested-name-specifier identifier enum-base [opt]
16971 enum-key:
16972 enum
16973 enum class [C++0x]
16974 enum struct [C++0x]
16976 enum-base: [C++0x]
16977 : type-specifier-seq
16979 opaque-enum-specifier:
16980 enum-key identifier enum-base [opt] ;
16982 GNU Extensions:
16983 enum-key attributes[opt] identifier [opt] enum-base [opt]
16984 { enumerator-list [opt] }attributes[opt]
16985 enum-key attributes[opt] identifier [opt] enum-base [opt]
16986 { enumerator-list, }attributes[opt] [C++0x]
16988 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16989 if the token stream isn't an enum-specifier after all. */
16991 static tree
16992 cp_parser_enum_specifier (cp_parser* parser)
16994 tree identifier;
16995 tree type = NULL_TREE;
16996 tree prev_scope;
16997 tree nested_name_specifier = NULL_TREE;
16998 tree attributes;
16999 bool scoped_enum_p = false;
17000 bool has_underlying_type = false;
17001 bool nested_being_defined = false;
17002 bool new_value_list = false;
17003 bool is_new_type = false;
17004 bool is_anonymous = false;
17005 tree underlying_type = NULL_TREE;
17006 cp_token *type_start_token = NULL;
17007 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17009 parser->colon_corrects_to_scope_p = false;
17011 /* Parse tentatively so that we can back up if we don't find a
17012 enum-specifier. */
17013 cp_parser_parse_tentatively (parser);
17015 /* Caller guarantees that the current token is 'enum', an identifier
17016 possibly follows, and the token after that is an opening brace.
17017 If we don't have an identifier, fabricate an anonymous name for
17018 the enumeration being defined. */
17019 cp_lexer_consume_token (parser->lexer);
17021 /* Parse the "class" or "struct", which indicates a scoped
17022 enumeration type in C++0x. */
17023 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17024 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17026 if (cxx_dialect < cxx11)
17027 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17029 /* Consume the `struct' or `class' token. */
17030 cp_lexer_consume_token (parser->lexer);
17032 scoped_enum_p = true;
17035 attributes = cp_parser_attributes_opt (parser);
17037 /* Clear the qualification. */
17038 parser->scope = NULL_TREE;
17039 parser->qualifying_scope = NULL_TREE;
17040 parser->object_scope = NULL_TREE;
17042 /* Figure out in what scope the declaration is being placed. */
17043 prev_scope = current_scope ();
17045 type_start_token = cp_lexer_peek_token (parser->lexer);
17047 push_deferring_access_checks (dk_no_check);
17048 nested_name_specifier
17049 = cp_parser_nested_name_specifier_opt (parser,
17050 /*typename_keyword_p=*/true,
17051 /*check_dependency_p=*/false,
17052 /*type_p=*/false,
17053 /*is_declaration=*/false);
17055 if (nested_name_specifier)
17057 tree name;
17059 identifier = cp_parser_identifier (parser);
17060 name = cp_parser_lookup_name (parser, identifier,
17061 enum_type,
17062 /*is_template=*/false,
17063 /*is_namespace=*/false,
17064 /*check_dependency=*/true,
17065 /*ambiguous_decls=*/NULL,
17066 input_location);
17067 if (name && name != error_mark_node)
17069 type = TREE_TYPE (name);
17070 if (TREE_CODE (type) == TYPENAME_TYPE)
17072 /* Are template enums allowed in ISO? */
17073 if (template_parm_scope_p ())
17074 pedwarn (type_start_token->location, OPT_Wpedantic,
17075 "%qD is an enumeration template", name);
17076 /* ignore a typename reference, for it will be solved by name
17077 in start_enum. */
17078 type = NULL_TREE;
17081 else if (nested_name_specifier == error_mark_node)
17082 /* We already issued an error. */;
17083 else
17085 error_at (type_start_token->location,
17086 "%qD does not name an enumeration in %qT",
17087 identifier, nested_name_specifier);
17088 nested_name_specifier = error_mark_node;
17091 else
17093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17094 identifier = cp_parser_identifier (parser);
17095 else
17097 identifier = make_anon_name ();
17098 is_anonymous = true;
17099 if (scoped_enum_p)
17100 error_at (type_start_token->location,
17101 "anonymous scoped enum is not allowed");
17104 pop_deferring_access_checks ();
17106 /* Check for the `:' that denotes a specified underlying type in C++0x.
17107 Note that a ':' could also indicate a bitfield width, however. */
17108 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17110 cp_decl_specifier_seq type_specifiers;
17112 /* Consume the `:'. */
17113 cp_lexer_consume_token (parser->lexer);
17115 /* Parse the type-specifier-seq. */
17116 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17117 /*is_trailing_return=*/false,
17118 &type_specifiers);
17120 /* At this point this is surely not elaborated type specifier. */
17121 if (!cp_parser_parse_definitely (parser))
17122 return NULL_TREE;
17124 if (cxx_dialect < cxx11)
17125 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17127 has_underlying_type = true;
17129 /* If that didn't work, stop. */
17130 if (type_specifiers.type != error_mark_node)
17132 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17133 /*initialized=*/0, NULL);
17134 if (underlying_type == error_mark_node
17135 || check_for_bare_parameter_packs (underlying_type))
17136 underlying_type = NULL_TREE;
17140 /* Look for the `{' but don't consume it yet. */
17141 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17143 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17145 cp_parser_error (parser, "expected %<{%>");
17146 if (has_underlying_type)
17148 type = NULL_TREE;
17149 goto out;
17152 /* An opaque-enum-specifier must have a ';' here. */
17153 if ((scoped_enum_p || underlying_type)
17154 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17156 cp_parser_error (parser, "expected %<;%> or %<{%>");
17157 if (has_underlying_type)
17159 type = NULL_TREE;
17160 goto out;
17165 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17166 return NULL_TREE;
17168 if (nested_name_specifier)
17170 if (CLASS_TYPE_P (nested_name_specifier))
17172 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17173 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17174 push_scope (nested_name_specifier);
17176 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17178 push_nested_namespace (nested_name_specifier);
17182 /* Issue an error message if type-definitions are forbidden here. */
17183 if (!cp_parser_check_type_definition (parser))
17184 type = error_mark_node;
17185 else
17186 /* Create the new type. We do this before consuming the opening
17187 brace so the enum will be recorded as being on the line of its
17188 tag (or the 'enum' keyword, if there is no tag). */
17189 type = start_enum (identifier, type, underlying_type,
17190 attributes, scoped_enum_p, &is_new_type);
17192 /* If the next token is not '{' it is an opaque-enum-specifier or an
17193 elaborated-type-specifier. */
17194 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17196 timevar_push (TV_PARSE_ENUM);
17197 if (nested_name_specifier
17198 && nested_name_specifier != error_mark_node)
17200 /* The following catches invalid code such as:
17201 enum class S<int>::E { A, B, C }; */
17202 if (!processing_specialization
17203 && CLASS_TYPE_P (nested_name_specifier)
17204 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17205 error_at (type_start_token->location, "cannot add an enumerator "
17206 "list to a template instantiation");
17208 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17210 error_at (type_start_token->location,
17211 "%<%T::%E%> has not been declared",
17212 TYPE_CONTEXT (nested_name_specifier),
17213 nested_name_specifier);
17214 type = error_mark_node;
17216 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17217 && !CLASS_TYPE_P (nested_name_specifier))
17219 error_at (type_start_token->location, "nested name specifier "
17220 "%qT for enum declaration does not name a class "
17221 "or namespace", nested_name_specifier);
17222 type = error_mark_node;
17224 /* If that scope does not contain the scope in which the
17225 class was originally declared, the program is invalid. */
17226 else if (prev_scope && !is_ancestor (prev_scope,
17227 nested_name_specifier))
17229 if (at_namespace_scope_p ())
17230 error_at (type_start_token->location,
17231 "declaration of %qD in namespace %qD which does not "
17232 "enclose %qD",
17233 type, prev_scope, nested_name_specifier);
17234 else
17235 error_at (type_start_token->location,
17236 "declaration of %qD in %qD which does not "
17237 "enclose %qD",
17238 type, prev_scope, nested_name_specifier);
17239 type = error_mark_node;
17241 /* If that scope is the scope where the declaration is being placed
17242 the program is invalid. */
17243 else if (CLASS_TYPE_P (nested_name_specifier)
17244 && CLASS_TYPE_P (prev_scope)
17245 && same_type_p (nested_name_specifier, prev_scope))
17247 permerror (type_start_token->location,
17248 "extra qualification not allowed");
17249 nested_name_specifier = NULL_TREE;
17253 if (scoped_enum_p)
17254 begin_scope (sk_scoped_enum, type);
17256 /* Consume the opening brace. */
17257 cp_lexer_consume_token (parser->lexer);
17259 if (type == error_mark_node)
17260 ; /* Nothing to add */
17261 else if (OPAQUE_ENUM_P (type)
17262 || (cxx_dialect > cxx98 && processing_specialization))
17264 new_value_list = true;
17265 SET_OPAQUE_ENUM_P (type, false);
17266 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17268 else
17270 error_at (type_start_token->location,
17271 "multiple definition of %q#T", type);
17272 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17273 "previous definition here");
17274 type = error_mark_node;
17277 if (type == error_mark_node)
17278 cp_parser_skip_to_end_of_block_or_statement (parser);
17279 /* If the next token is not '}', then there are some enumerators. */
17280 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17282 if (is_anonymous && !scoped_enum_p)
17283 pedwarn (type_start_token->location, OPT_Wpedantic,
17284 "ISO C++ forbids empty anonymous enum");
17286 else
17287 cp_parser_enumerator_list (parser, type);
17289 /* Consume the final '}'. */
17290 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17292 if (scoped_enum_p)
17293 finish_scope ();
17294 timevar_pop (TV_PARSE_ENUM);
17296 else
17298 /* If a ';' follows, then it is an opaque-enum-specifier
17299 and additional restrictions apply. */
17300 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17302 if (is_anonymous)
17303 error_at (type_start_token->location,
17304 "opaque-enum-specifier without name");
17305 else if (nested_name_specifier)
17306 error_at (type_start_token->location,
17307 "opaque-enum-specifier must use a simple identifier");
17311 /* Look for trailing attributes to apply to this enumeration, and
17312 apply them if appropriate. */
17313 if (cp_parser_allow_gnu_extensions_p (parser))
17315 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17316 cplus_decl_attributes (&type,
17317 trailing_attr,
17318 (int) ATTR_FLAG_TYPE_IN_PLACE);
17321 /* Finish up the enumeration. */
17322 if (type != error_mark_node)
17324 if (new_value_list)
17325 finish_enum_value_list (type);
17326 if (is_new_type)
17327 finish_enum (type);
17330 if (nested_name_specifier)
17332 if (CLASS_TYPE_P (nested_name_specifier))
17334 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17335 pop_scope (nested_name_specifier);
17337 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17339 pop_nested_namespace (nested_name_specifier);
17342 out:
17343 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17344 return type;
17347 /* Parse an enumerator-list. The enumerators all have the indicated
17348 TYPE.
17350 enumerator-list:
17351 enumerator-definition
17352 enumerator-list , enumerator-definition */
17354 static void
17355 cp_parser_enumerator_list (cp_parser* parser, tree type)
17357 while (true)
17359 /* Parse an enumerator-definition. */
17360 cp_parser_enumerator_definition (parser, type);
17362 /* If the next token is not a ',', we've reached the end of
17363 the list. */
17364 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17365 break;
17366 /* Otherwise, consume the `,' and keep going. */
17367 cp_lexer_consume_token (parser->lexer);
17368 /* If the next token is a `}', there is a trailing comma. */
17369 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17371 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17372 pedwarn (input_location, OPT_Wpedantic,
17373 "comma at end of enumerator list");
17374 break;
17379 /* Parse an enumerator-definition. The enumerator has the indicated
17380 TYPE.
17382 enumerator-definition:
17383 enumerator
17384 enumerator = constant-expression
17386 enumerator:
17387 identifier
17389 GNU Extensions:
17391 enumerator-definition:
17392 enumerator attributes [opt]
17393 enumerator attributes [opt] = constant-expression */
17395 static void
17396 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17398 tree identifier;
17399 tree value;
17400 location_t loc;
17402 /* Save the input location because we are interested in the location
17403 of the identifier and not the location of the explicit value. */
17404 loc = cp_lexer_peek_token (parser->lexer)->location;
17406 /* Look for the identifier. */
17407 identifier = cp_parser_identifier (parser);
17408 if (identifier == error_mark_node)
17409 return;
17411 /* Parse any specified attributes. */
17412 tree attrs = cp_parser_attributes_opt (parser);
17414 /* If the next token is an '=', then there is an explicit value. */
17415 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17417 /* Consume the `=' token. */
17418 cp_lexer_consume_token (parser->lexer);
17419 /* Parse the value. */
17420 value = cp_parser_constant_expression (parser);
17422 else
17423 value = NULL_TREE;
17425 /* If we are processing a template, make sure the initializer of the
17426 enumerator doesn't contain any bare template parameter pack. */
17427 if (check_for_bare_parameter_packs (value))
17428 value = error_mark_node;
17430 /* Create the enumerator. */
17431 build_enumerator (identifier, value, type, attrs, loc);
17434 /* Parse a namespace-name.
17436 namespace-name:
17437 original-namespace-name
17438 namespace-alias
17440 Returns the NAMESPACE_DECL for the namespace. */
17442 static tree
17443 cp_parser_namespace_name (cp_parser* parser)
17445 tree identifier;
17446 tree namespace_decl;
17448 cp_token *token = cp_lexer_peek_token (parser->lexer);
17450 /* Get the name of the namespace. */
17451 identifier = cp_parser_identifier (parser);
17452 if (identifier == error_mark_node)
17453 return error_mark_node;
17455 /* Look up the identifier in the currently active scope. Look only
17456 for namespaces, due to:
17458 [basic.lookup.udir]
17460 When looking up a namespace-name in a using-directive or alias
17461 definition, only namespace names are considered.
17463 And:
17465 [basic.lookup.qual]
17467 During the lookup of a name preceding the :: scope resolution
17468 operator, object, function, and enumerator names are ignored.
17470 (Note that cp_parser_qualifying_entity only calls this
17471 function if the token after the name is the scope resolution
17472 operator.) */
17473 namespace_decl = cp_parser_lookup_name (parser, identifier,
17474 none_type,
17475 /*is_template=*/false,
17476 /*is_namespace=*/true,
17477 /*check_dependency=*/true,
17478 /*ambiguous_decls=*/NULL,
17479 token->location);
17480 /* If it's not a namespace, issue an error. */
17481 if (namespace_decl == error_mark_node
17482 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17484 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17485 error_at (token->location, "%qD is not a namespace-name", identifier);
17486 cp_parser_error (parser, "expected namespace-name");
17487 namespace_decl = error_mark_node;
17490 return namespace_decl;
17493 /* Parse a namespace-definition.
17495 namespace-definition:
17496 named-namespace-definition
17497 unnamed-namespace-definition
17499 named-namespace-definition:
17500 original-namespace-definition
17501 extension-namespace-definition
17503 original-namespace-definition:
17504 namespace identifier { namespace-body }
17506 extension-namespace-definition:
17507 namespace original-namespace-name { namespace-body }
17509 unnamed-namespace-definition:
17510 namespace { namespace-body } */
17512 static void
17513 cp_parser_namespace_definition (cp_parser* parser)
17515 tree identifier, attribs;
17516 bool has_visibility;
17517 bool is_inline;
17518 cp_token* token;
17519 int nested_definition_count = 0;
17521 cp_ensure_no_omp_declare_simd (parser);
17522 cp_ensure_no_oacc_routine (parser);
17523 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17525 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17526 is_inline = true;
17527 cp_lexer_consume_token (parser->lexer);
17529 else
17530 is_inline = false;
17532 /* Look for the `namespace' keyword. */
17533 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17535 /* Parse any specified attributes before the identifier. */
17536 attribs = cp_parser_attributes_opt (parser);
17538 /* Get the name of the namespace. We do not attempt to distinguish
17539 between an original-namespace-definition and an
17540 extension-namespace-definition at this point. The semantic
17541 analysis routines are responsible for that. */
17542 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17543 identifier = cp_parser_identifier (parser);
17544 else
17545 identifier = NULL_TREE;
17547 /* Parse any specified attributes after the identifier. */
17548 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17549 if (post_ident_attribs)
17551 if (attribs)
17552 attribs = chainon (attribs, post_ident_attribs);
17553 else
17554 attribs = post_ident_attribs;
17557 /* Start the namespace. */
17558 bool ok = push_namespace (identifier);
17560 /* Parse any nested namespace definition. */
17561 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17563 if (attribs)
17564 error_at (token->location, "a nested namespace definition cannot have attributes");
17565 if (cxx_dialect < cxx1z)
17566 pedwarn (input_location, OPT_Wpedantic,
17567 "nested namespace definitions only available with "
17568 "-std=c++1z or -std=gnu++1z");
17569 if (is_inline)
17570 error_at (token->location, "a nested namespace definition cannot be inline");
17571 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17573 cp_lexer_consume_token (parser->lexer);
17574 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17575 identifier = cp_parser_identifier (parser);
17576 else
17578 cp_parser_error (parser, "nested identifier required");
17579 break;
17581 ++nested_definition_count;
17582 push_namespace (identifier);
17586 /* Look for the `{' to validate starting the namespace. */
17587 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17589 /* "inline namespace" is equivalent to a stub namespace definition
17590 followed by a strong using directive. */
17591 if (is_inline && ok)
17593 tree name_space = current_namespace;
17594 /* Set up namespace association. */
17595 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17596 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17597 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17598 /* Import the contents of the inline namespace. */
17599 pop_namespace ();
17600 do_using_directive (name_space);
17601 push_namespace (identifier);
17604 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17606 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17608 /* Parse the body of the namespace. */
17609 cp_parser_namespace_body (parser);
17611 if (has_visibility)
17612 pop_visibility (1);
17614 /* Finish the nested namespace definitions. */
17615 while (nested_definition_count--)
17616 pop_namespace ();
17618 /* Finish the namespace. */
17619 if (ok)
17620 pop_namespace ();
17621 /* Look for the final `}'. */
17622 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17625 /* Parse a namespace-body.
17627 namespace-body:
17628 declaration-seq [opt] */
17630 static void
17631 cp_parser_namespace_body (cp_parser* parser)
17633 cp_parser_declaration_seq_opt (parser);
17636 /* Parse a namespace-alias-definition.
17638 namespace-alias-definition:
17639 namespace identifier = qualified-namespace-specifier ; */
17641 static void
17642 cp_parser_namespace_alias_definition (cp_parser* parser)
17644 tree identifier;
17645 tree namespace_specifier;
17647 cp_token *token = cp_lexer_peek_token (parser->lexer);
17649 /* Look for the `namespace' keyword. */
17650 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17651 /* Look for the identifier. */
17652 identifier = cp_parser_identifier (parser);
17653 if (identifier == error_mark_node)
17654 return;
17655 /* Look for the `=' token. */
17656 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17657 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17659 error_at (token->location, "%<namespace%> definition is not allowed here");
17660 /* Skip the definition. */
17661 cp_lexer_consume_token (parser->lexer);
17662 if (cp_parser_skip_to_closing_brace (parser))
17663 cp_lexer_consume_token (parser->lexer);
17664 return;
17666 cp_parser_require (parser, CPP_EQ, RT_EQ);
17667 /* Look for the qualified-namespace-specifier. */
17668 namespace_specifier
17669 = cp_parser_qualified_namespace_specifier (parser);
17670 /* Look for the `;' token. */
17671 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17673 /* Register the alias in the symbol table. */
17674 do_namespace_alias (identifier, namespace_specifier);
17677 /* Parse a qualified-namespace-specifier.
17679 qualified-namespace-specifier:
17680 :: [opt] nested-name-specifier [opt] namespace-name
17682 Returns a NAMESPACE_DECL corresponding to the specified
17683 namespace. */
17685 static tree
17686 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17688 /* Look for the optional `::'. */
17689 cp_parser_global_scope_opt (parser,
17690 /*current_scope_valid_p=*/false);
17692 /* Look for the optional nested-name-specifier. */
17693 cp_parser_nested_name_specifier_opt (parser,
17694 /*typename_keyword_p=*/false,
17695 /*check_dependency_p=*/true,
17696 /*type_p=*/false,
17697 /*is_declaration=*/true);
17699 return cp_parser_namespace_name (parser);
17702 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17703 access declaration.
17705 using-declaration:
17706 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17707 using :: unqualified-id ;
17709 access-declaration:
17710 qualified-id ;
17714 static bool
17715 cp_parser_using_declaration (cp_parser* parser,
17716 bool access_declaration_p)
17718 cp_token *token;
17719 bool typename_p = false;
17720 bool global_scope_p;
17721 tree decl;
17722 tree identifier;
17723 tree qscope;
17724 int oldcount = errorcount;
17725 cp_token *diag_token = NULL;
17727 if (access_declaration_p)
17729 diag_token = cp_lexer_peek_token (parser->lexer);
17730 cp_parser_parse_tentatively (parser);
17732 else
17734 /* Look for the `using' keyword. */
17735 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17737 /* Peek at the next token. */
17738 token = cp_lexer_peek_token (parser->lexer);
17739 /* See if it's `typename'. */
17740 if (token->keyword == RID_TYPENAME)
17742 /* Remember that we've seen it. */
17743 typename_p = true;
17744 /* Consume the `typename' token. */
17745 cp_lexer_consume_token (parser->lexer);
17749 /* Look for the optional global scope qualification. */
17750 global_scope_p
17751 = (cp_parser_global_scope_opt (parser,
17752 /*current_scope_valid_p=*/false)
17753 != NULL_TREE);
17755 /* If we saw `typename', or didn't see `::', then there must be a
17756 nested-name-specifier present. */
17757 if (typename_p || !global_scope_p)
17759 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17760 /*check_dependency_p=*/true,
17761 /*type_p=*/false,
17762 /*is_declaration=*/true);
17763 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17765 cp_parser_skip_to_end_of_block_or_statement (parser);
17766 return false;
17769 /* Otherwise, we could be in either of the two productions. In that
17770 case, treat the nested-name-specifier as optional. */
17771 else
17772 qscope = cp_parser_nested_name_specifier_opt (parser,
17773 /*typename_keyword_p=*/false,
17774 /*check_dependency_p=*/true,
17775 /*type_p=*/false,
17776 /*is_declaration=*/true);
17777 if (!qscope)
17778 qscope = global_namespace;
17779 else if (UNSCOPED_ENUM_P (qscope))
17780 qscope = CP_TYPE_CONTEXT (qscope);
17782 if (access_declaration_p && cp_parser_error_occurred (parser))
17783 /* Something has already gone wrong; there's no need to parse
17784 further. Since an error has occurred, the return value of
17785 cp_parser_parse_definitely will be false, as required. */
17786 return cp_parser_parse_definitely (parser);
17788 token = cp_lexer_peek_token (parser->lexer);
17789 /* Parse the unqualified-id. */
17790 identifier = cp_parser_unqualified_id (parser,
17791 /*template_keyword_p=*/false,
17792 /*check_dependency_p=*/true,
17793 /*declarator_p=*/true,
17794 /*optional_p=*/false);
17796 if (access_declaration_p)
17798 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17799 cp_parser_simulate_error (parser);
17800 if (!cp_parser_parse_definitely (parser))
17801 return false;
17804 /* The function we call to handle a using-declaration is different
17805 depending on what scope we are in. */
17806 if (qscope == error_mark_node || identifier == error_mark_node)
17808 else if (!identifier_p (identifier)
17809 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17810 /* [namespace.udecl]
17812 A using declaration shall not name a template-id. */
17813 error_at (token->location,
17814 "a template-id may not appear in a using-declaration");
17815 else
17817 if (at_class_scope_p ())
17819 /* Create the USING_DECL. */
17820 decl = do_class_using_decl (parser->scope, identifier);
17822 if (decl && typename_p)
17823 USING_DECL_TYPENAME_P (decl) = 1;
17825 if (check_for_bare_parameter_packs (decl))
17827 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17828 return false;
17830 else
17831 /* Add it to the list of members in this class. */
17832 finish_member_declaration (decl);
17834 else
17836 decl = cp_parser_lookup_name_simple (parser,
17837 identifier,
17838 token->location);
17839 if (decl == error_mark_node)
17840 cp_parser_name_lookup_error (parser, identifier,
17841 decl, NLE_NULL,
17842 token->location);
17843 else if (check_for_bare_parameter_packs (decl))
17845 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17846 return false;
17848 else if (!at_namespace_scope_p ())
17849 do_local_using_decl (decl, qscope, identifier);
17850 else
17851 do_toplevel_using_decl (decl, qscope, identifier);
17855 /* Look for the final `;'. */
17856 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17858 if (access_declaration_p && errorcount == oldcount)
17859 warning_at (diag_token->location, OPT_Wdeprecated,
17860 "access declarations are deprecated "
17861 "in favour of using-declarations; "
17862 "suggestion: add the %<using%> keyword");
17864 return true;
17867 /* Parse an alias-declaration.
17869 alias-declaration:
17870 using identifier attribute-specifier-seq [opt] = type-id */
17872 static tree
17873 cp_parser_alias_declaration (cp_parser* parser)
17875 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17876 location_t id_location;
17877 cp_declarator *declarator;
17878 cp_decl_specifier_seq decl_specs;
17879 bool member_p;
17880 const char *saved_message = NULL;
17882 /* Look for the `using' keyword. */
17883 cp_token *using_token
17884 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17885 if (using_token == NULL)
17886 return error_mark_node;
17888 id_location = cp_lexer_peek_token (parser->lexer)->location;
17889 id = cp_parser_identifier (parser);
17890 if (id == error_mark_node)
17891 return error_mark_node;
17893 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17894 attributes = cp_parser_attributes_opt (parser);
17895 if (attributes == error_mark_node)
17896 return error_mark_node;
17898 cp_parser_require (parser, CPP_EQ, RT_EQ);
17900 if (cp_parser_error_occurred (parser))
17901 return error_mark_node;
17903 cp_parser_commit_to_tentative_parse (parser);
17905 /* Now we are going to parse the type-id of the declaration. */
17908 [dcl.type]/3 says:
17910 "A type-specifier-seq shall not define a class or enumeration
17911 unless it appears in the type-id of an alias-declaration (7.1.3) that
17912 is not the declaration of a template-declaration."
17914 In other words, if we currently are in an alias template, the
17915 type-id should not define a type.
17917 So let's set parser->type_definition_forbidden_message in that
17918 case; cp_parser_check_type_definition (called by
17919 cp_parser_class_specifier) will then emit an error if a type is
17920 defined in the type-id. */
17921 if (parser->num_template_parameter_lists)
17923 saved_message = parser->type_definition_forbidden_message;
17924 parser->type_definition_forbidden_message =
17925 G_("types may not be defined in alias template declarations");
17928 type = cp_parser_type_id (parser);
17930 /* Restore the error message if need be. */
17931 if (parser->num_template_parameter_lists)
17932 parser->type_definition_forbidden_message = saved_message;
17934 if (type == error_mark_node
17935 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17937 cp_parser_skip_to_end_of_block_or_statement (parser);
17938 return error_mark_node;
17941 /* A typedef-name can also be introduced by an alias-declaration. The
17942 identifier following the using keyword becomes a typedef-name. It has
17943 the same semantics as if it were introduced by the typedef
17944 specifier. In particular, it does not define a new type and it shall
17945 not appear in the type-id. */
17947 clear_decl_specs (&decl_specs);
17948 decl_specs.type = type;
17949 if (attributes != NULL_TREE)
17951 decl_specs.attributes = attributes;
17952 set_and_check_decl_spec_loc (&decl_specs,
17953 ds_attribute,
17954 attrs_token);
17956 set_and_check_decl_spec_loc (&decl_specs,
17957 ds_typedef,
17958 using_token);
17959 set_and_check_decl_spec_loc (&decl_specs,
17960 ds_alias,
17961 using_token);
17963 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17964 declarator->id_loc = id_location;
17966 member_p = at_class_scope_p ();
17967 if (member_p)
17968 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17969 NULL_TREE, attributes);
17970 else
17971 decl = start_decl (declarator, &decl_specs, 0,
17972 attributes, NULL_TREE, &pushed_scope);
17973 if (decl == error_mark_node)
17974 return decl;
17976 // Attach constraints to the alias declaration.
17977 if (flag_concepts && current_template_parms)
17979 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17980 tree constr = build_constraints (reqs, NULL_TREE);
17981 set_constraints (decl, constr);
17984 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17986 if (pushed_scope)
17987 pop_scope (pushed_scope);
17989 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17990 added into the symbol table; otherwise, return the TYPE_DECL. */
17991 if (DECL_LANG_SPECIFIC (decl)
17992 && DECL_TEMPLATE_INFO (decl)
17993 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17995 decl = DECL_TI_TEMPLATE (decl);
17996 if (member_p)
17997 check_member_template (decl);
18000 return decl;
18003 /* Parse a using-directive.
18005 using-directive:
18006 using namespace :: [opt] nested-name-specifier [opt]
18007 namespace-name ; */
18009 static void
18010 cp_parser_using_directive (cp_parser* parser)
18012 tree namespace_decl;
18013 tree attribs;
18015 /* Look for the `using' keyword. */
18016 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18017 /* And the `namespace' keyword. */
18018 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18019 /* Look for the optional `::' operator. */
18020 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18021 /* And the optional nested-name-specifier. */
18022 cp_parser_nested_name_specifier_opt (parser,
18023 /*typename_keyword_p=*/false,
18024 /*check_dependency_p=*/true,
18025 /*type_p=*/false,
18026 /*is_declaration=*/true);
18027 /* Get the namespace being used. */
18028 namespace_decl = cp_parser_namespace_name (parser);
18029 /* And any specified attributes. */
18030 attribs = cp_parser_attributes_opt (parser);
18031 /* Update the symbol table. */
18032 parse_using_directive (namespace_decl, attribs);
18033 /* Look for the final `;'. */
18034 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18037 /* Parse an asm-definition.
18039 asm-definition:
18040 asm ( string-literal ) ;
18042 GNU Extension:
18044 asm-definition:
18045 asm volatile [opt] ( string-literal ) ;
18046 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18047 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18048 : asm-operand-list [opt] ) ;
18049 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18050 : asm-operand-list [opt]
18051 : asm-clobber-list [opt] ) ;
18052 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18053 : asm-clobber-list [opt]
18054 : asm-goto-list ) ; */
18056 static void
18057 cp_parser_asm_definition (cp_parser* parser)
18059 tree string;
18060 tree outputs = NULL_TREE;
18061 tree inputs = NULL_TREE;
18062 tree clobbers = NULL_TREE;
18063 tree labels = NULL_TREE;
18064 tree asm_stmt;
18065 bool volatile_p = false;
18066 bool extended_p = false;
18067 bool invalid_inputs_p = false;
18068 bool invalid_outputs_p = false;
18069 bool goto_p = false;
18070 required_token missing = RT_NONE;
18072 /* Look for the `asm' keyword. */
18073 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18075 if (parser->in_function_body
18076 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18078 error ("%<asm%> in %<constexpr%> function");
18079 cp_function_chain->invalid_constexpr = true;
18082 /* See if the next token is `volatile'. */
18083 if (cp_parser_allow_gnu_extensions_p (parser)
18084 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18086 /* Remember that we saw the `volatile' keyword. */
18087 volatile_p = true;
18088 /* Consume the token. */
18089 cp_lexer_consume_token (parser->lexer);
18091 if (cp_parser_allow_gnu_extensions_p (parser)
18092 && parser->in_function_body
18093 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18095 /* Remember that we saw the `goto' keyword. */
18096 goto_p = true;
18097 /* Consume the token. */
18098 cp_lexer_consume_token (parser->lexer);
18100 /* Look for the opening `('. */
18101 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18102 return;
18103 /* Look for the string. */
18104 string = cp_parser_string_literal (parser, false, false);
18105 if (string == error_mark_node)
18107 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18108 /*consume_paren=*/true);
18109 return;
18112 /* If we're allowing GNU extensions, check for the extended assembly
18113 syntax. Unfortunately, the `:' tokens need not be separated by
18114 a space in C, and so, for compatibility, we tolerate that here
18115 too. Doing that means that we have to treat the `::' operator as
18116 two `:' tokens. */
18117 if (cp_parser_allow_gnu_extensions_p (parser)
18118 && parser->in_function_body
18119 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18120 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18122 bool inputs_p = false;
18123 bool clobbers_p = false;
18124 bool labels_p = false;
18126 /* The extended syntax was used. */
18127 extended_p = true;
18129 /* Look for outputs. */
18130 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18132 /* Consume the `:'. */
18133 cp_lexer_consume_token (parser->lexer);
18134 /* Parse the output-operands. */
18135 if (cp_lexer_next_token_is_not (parser->lexer,
18136 CPP_COLON)
18137 && cp_lexer_next_token_is_not (parser->lexer,
18138 CPP_SCOPE)
18139 && cp_lexer_next_token_is_not (parser->lexer,
18140 CPP_CLOSE_PAREN)
18141 && !goto_p)
18143 outputs = cp_parser_asm_operand_list (parser);
18144 if (outputs == error_mark_node)
18145 invalid_outputs_p = true;
18148 /* If the next token is `::', there are no outputs, and the
18149 next token is the beginning of the inputs. */
18150 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18151 /* The inputs are coming next. */
18152 inputs_p = true;
18154 /* Look for inputs. */
18155 if (inputs_p
18156 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18158 /* Consume the `:' or `::'. */
18159 cp_lexer_consume_token (parser->lexer);
18160 /* Parse the output-operands. */
18161 if (cp_lexer_next_token_is_not (parser->lexer,
18162 CPP_COLON)
18163 && cp_lexer_next_token_is_not (parser->lexer,
18164 CPP_SCOPE)
18165 && cp_lexer_next_token_is_not (parser->lexer,
18166 CPP_CLOSE_PAREN))
18168 inputs = cp_parser_asm_operand_list (parser);
18169 if (inputs == error_mark_node)
18170 invalid_inputs_p = true;
18173 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18174 /* The clobbers are coming next. */
18175 clobbers_p = true;
18177 /* Look for clobbers. */
18178 if (clobbers_p
18179 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18181 clobbers_p = true;
18182 /* Consume the `:' or `::'. */
18183 cp_lexer_consume_token (parser->lexer);
18184 /* Parse the clobbers. */
18185 if (cp_lexer_next_token_is_not (parser->lexer,
18186 CPP_COLON)
18187 && cp_lexer_next_token_is_not (parser->lexer,
18188 CPP_CLOSE_PAREN))
18189 clobbers = cp_parser_asm_clobber_list (parser);
18191 else if (goto_p
18192 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18193 /* The labels are coming next. */
18194 labels_p = true;
18196 /* Look for labels. */
18197 if (labels_p
18198 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18200 labels_p = true;
18201 /* Consume the `:' or `::'. */
18202 cp_lexer_consume_token (parser->lexer);
18203 /* Parse the labels. */
18204 labels = cp_parser_asm_label_list (parser);
18207 if (goto_p && !labels_p)
18208 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18210 else if (goto_p)
18211 missing = RT_COLON_SCOPE;
18213 /* Look for the closing `)'. */
18214 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18215 missing ? missing : RT_CLOSE_PAREN))
18216 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18217 /*consume_paren=*/true);
18218 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18220 if (!invalid_inputs_p && !invalid_outputs_p)
18222 /* Create the ASM_EXPR. */
18223 if (parser->in_function_body)
18225 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18226 inputs, clobbers, labels);
18227 /* If the extended syntax was not used, mark the ASM_EXPR. */
18228 if (!extended_p)
18230 tree temp = asm_stmt;
18231 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18232 temp = TREE_OPERAND (temp, 0);
18234 ASM_INPUT_P (temp) = 1;
18237 else
18238 symtab->finalize_toplevel_asm (string);
18242 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18243 type that comes from the decl-specifier-seq. */
18245 static tree
18246 strip_declarator_types (tree type, cp_declarator *declarator)
18248 for (cp_declarator *d = declarator; d;)
18249 switch (d->kind)
18251 case cdk_id:
18252 case cdk_error:
18253 d = NULL;
18254 break;
18256 default:
18257 if (TYPE_PTRMEMFUNC_P (type))
18258 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18259 type = TREE_TYPE (type);
18260 d = d->declarator;
18261 break;
18264 return type;
18267 /* Declarators [gram.dcl.decl] */
18269 /* Parse an init-declarator.
18271 init-declarator:
18272 declarator initializer [opt]
18274 GNU Extension:
18276 init-declarator:
18277 declarator asm-specification [opt] attributes [opt] initializer [opt]
18279 function-definition:
18280 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18281 function-body
18282 decl-specifier-seq [opt] declarator function-try-block
18284 GNU Extension:
18286 function-definition:
18287 __extension__ function-definition
18289 TM Extension:
18291 function-definition:
18292 decl-specifier-seq [opt] declarator function-transaction-block
18294 The DECL_SPECIFIERS apply to this declarator. Returns a
18295 representation of the entity declared. If MEMBER_P is TRUE, then
18296 this declarator appears in a class scope. The new DECL created by
18297 this declarator is returned.
18299 The CHECKS are access checks that should be performed once we know
18300 what entity is being declared (and, therefore, what classes have
18301 befriended it).
18303 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18304 for a function-definition here as well. If the declarator is a
18305 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18306 be TRUE upon return. By that point, the function-definition will
18307 have been completely parsed.
18309 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18310 is FALSE.
18312 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18313 parsed declaration if it is an uninitialized single declarator not followed
18314 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18315 if present, will not be consumed. If returned, this declarator will be
18316 created with SD_INITIALIZED but will not call cp_finish_decl.
18318 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18319 and there is an initializer, the pointed location_t is set to the
18320 location of the '=' or `(', or '{' in C++11 token introducing the
18321 initializer. */
18323 static tree
18324 cp_parser_init_declarator (cp_parser* parser,
18325 cp_decl_specifier_seq *decl_specifiers,
18326 vec<deferred_access_check, va_gc> *checks,
18327 bool function_definition_allowed_p,
18328 bool member_p,
18329 int declares_class_or_enum,
18330 bool* function_definition_p,
18331 tree* maybe_range_for_decl,
18332 location_t* init_loc,
18333 tree* auto_result)
18335 cp_token *token = NULL, *asm_spec_start_token = NULL,
18336 *attributes_start_token = NULL;
18337 cp_declarator *declarator;
18338 tree prefix_attributes;
18339 tree attributes = NULL;
18340 tree asm_specification;
18341 tree initializer;
18342 tree decl = NULL_TREE;
18343 tree scope;
18344 int is_initialized;
18345 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18346 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18347 "(...)". */
18348 enum cpp_ttype initialization_kind;
18349 bool is_direct_init = false;
18350 bool is_non_constant_init;
18351 int ctor_dtor_or_conv_p;
18352 bool friend_p = cp_parser_friend_p (decl_specifiers);
18353 tree pushed_scope = NULL_TREE;
18354 bool range_for_decl_p = false;
18355 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18356 location_t tmp_init_loc = UNKNOWN_LOCATION;
18358 /* Gather the attributes that were provided with the
18359 decl-specifiers. */
18360 prefix_attributes = decl_specifiers->attributes;
18362 /* Assume that this is not the declarator for a function
18363 definition. */
18364 if (function_definition_p)
18365 *function_definition_p = false;
18367 /* Default arguments are only permitted for function parameters. */
18368 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18369 parser->default_arg_ok_p = false;
18371 /* Defer access checks while parsing the declarator; we cannot know
18372 what names are accessible until we know what is being
18373 declared. */
18374 resume_deferring_access_checks ();
18376 /* Parse the declarator. */
18377 token = cp_lexer_peek_token (parser->lexer);
18378 declarator
18379 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18380 &ctor_dtor_or_conv_p,
18381 /*parenthesized_p=*/NULL,
18382 member_p, friend_p);
18383 /* Gather up the deferred checks. */
18384 stop_deferring_access_checks ();
18386 parser->default_arg_ok_p = saved_default_arg_ok_p;
18388 /* If the DECLARATOR was erroneous, there's no need to go
18389 further. */
18390 if (declarator == cp_error_declarator)
18391 return error_mark_node;
18393 /* Check that the number of template-parameter-lists is OK. */
18394 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18395 token->location))
18396 return error_mark_node;
18398 if (declares_class_or_enum & 2)
18399 cp_parser_check_for_definition_in_return_type (declarator,
18400 decl_specifiers->type,
18401 decl_specifiers->locations[ds_type_spec]);
18403 /* Figure out what scope the entity declared by the DECLARATOR is
18404 located in. `grokdeclarator' sometimes changes the scope, so
18405 we compute it now. */
18406 scope = get_scope_of_declarator (declarator);
18408 /* Perform any lookups in the declared type which were thought to be
18409 dependent, but are not in the scope of the declarator. */
18410 decl_specifiers->type
18411 = maybe_update_decl_type (decl_specifiers->type, scope);
18413 /* If we're allowing GNU extensions, look for an
18414 asm-specification. */
18415 if (cp_parser_allow_gnu_extensions_p (parser))
18417 /* Look for an asm-specification. */
18418 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18419 asm_specification = cp_parser_asm_specification_opt (parser);
18421 else
18422 asm_specification = NULL_TREE;
18424 /* Look for attributes. */
18425 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18426 attributes = cp_parser_attributes_opt (parser);
18428 /* Peek at the next token. */
18429 token = cp_lexer_peek_token (parser->lexer);
18431 bool bogus_implicit_tmpl = false;
18433 if (function_declarator_p (declarator))
18435 /* Check to see if the token indicates the start of a
18436 function-definition. */
18437 if (cp_parser_token_starts_function_definition_p (token))
18439 if (!function_definition_allowed_p)
18441 /* If a function-definition should not appear here, issue an
18442 error message. */
18443 cp_parser_error (parser,
18444 "a function-definition is not allowed here");
18445 return error_mark_node;
18448 location_t func_brace_location
18449 = cp_lexer_peek_token (parser->lexer)->location;
18451 /* Neither attributes nor an asm-specification are allowed
18452 on a function-definition. */
18453 if (asm_specification)
18454 error_at (asm_spec_start_token->location,
18455 "an asm-specification is not allowed "
18456 "on a function-definition");
18457 if (attributes)
18458 error_at (attributes_start_token->location,
18459 "attributes are not allowed "
18460 "on a function-definition");
18461 /* This is a function-definition. */
18462 *function_definition_p = true;
18464 /* Parse the function definition. */
18465 if (member_p)
18466 decl = cp_parser_save_member_function_body (parser,
18467 decl_specifiers,
18468 declarator,
18469 prefix_attributes);
18470 else
18471 decl =
18472 (cp_parser_function_definition_from_specifiers_and_declarator
18473 (parser, decl_specifiers, prefix_attributes, declarator));
18475 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18477 /* This is where the prologue starts... */
18478 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18479 = func_brace_location;
18482 return decl;
18485 else if (parser->fully_implicit_function_template_p)
18487 /* A non-template declaration involving a function parameter list
18488 containing an implicit template parameter will be made into a
18489 template. If the resulting declaration is not going to be an
18490 actual function then finish the template scope here to prevent it.
18491 An error message will be issued once we have a decl to talk about.
18493 FIXME probably we should do type deduction rather than create an
18494 implicit template, but the standard currently doesn't allow it. */
18495 bogus_implicit_tmpl = true;
18496 finish_fully_implicit_template (parser, NULL_TREE);
18499 /* [dcl.dcl]
18501 Only in function declarations for constructors, destructors, and
18502 type conversions can the decl-specifier-seq be omitted.
18504 We explicitly postpone this check past the point where we handle
18505 function-definitions because we tolerate function-definitions
18506 that are missing their return types in some modes. */
18507 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18509 cp_parser_error (parser,
18510 "expected constructor, destructor, or type conversion");
18511 return error_mark_node;
18514 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18515 if (token->type == CPP_EQ
18516 || token->type == CPP_OPEN_PAREN
18517 || token->type == CPP_OPEN_BRACE)
18519 is_initialized = SD_INITIALIZED;
18520 initialization_kind = token->type;
18521 if (maybe_range_for_decl)
18522 *maybe_range_for_decl = error_mark_node;
18523 tmp_init_loc = token->location;
18524 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18525 *init_loc = tmp_init_loc;
18527 if (token->type == CPP_EQ
18528 && function_declarator_p (declarator))
18530 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18531 if (t2->keyword == RID_DEFAULT)
18532 is_initialized = SD_DEFAULTED;
18533 else if (t2->keyword == RID_DELETE)
18534 is_initialized = SD_DELETED;
18537 else
18539 /* If the init-declarator isn't initialized and isn't followed by a
18540 `,' or `;', it's not a valid init-declarator. */
18541 if (token->type != CPP_COMMA
18542 && token->type != CPP_SEMICOLON)
18544 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18545 range_for_decl_p = true;
18546 else
18548 if (!maybe_range_for_decl)
18549 cp_parser_error (parser, "expected initializer");
18550 return error_mark_node;
18553 is_initialized = SD_UNINITIALIZED;
18554 initialization_kind = CPP_EOF;
18557 /* Because start_decl has side-effects, we should only call it if we
18558 know we're going ahead. By this point, we know that we cannot
18559 possibly be looking at any other construct. */
18560 cp_parser_commit_to_tentative_parse (parser);
18562 /* Enter the newly declared entry in the symbol table. If we're
18563 processing a declaration in a class-specifier, we wait until
18564 after processing the initializer. */
18565 if (!member_p)
18567 if (parser->in_unbraced_linkage_specification_p)
18568 decl_specifiers->storage_class = sc_extern;
18569 decl = start_decl (declarator, decl_specifiers,
18570 range_for_decl_p? SD_INITIALIZED : is_initialized,
18571 attributes, prefix_attributes, &pushed_scope);
18572 cp_finalize_omp_declare_simd (parser, decl);
18573 cp_finalize_oacc_routine (parser, decl, false);
18574 /* Adjust location of decl if declarator->id_loc is more appropriate:
18575 set, and decl wasn't merged with another decl, in which case its
18576 location would be different from input_location, and more accurate. */
18577 if (DECL_P (decl)
18578 && declarator->id_loc != UNKNOWN_LOCATION
18579 && DECL_SOURCE_LOCATION (decl) == input_location)
18580 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18582 else if (scope)
18583 /* Enter the SCOPE. That way unqualified names appearing in the
18584 initializer will be looked up in SCOPE. */
18585 pushed_scope = push_scope (scope);
18587 /* Perform deferred access control checks, now that we know in which
18588 SCOPE the declared entity resides. */
18589 if (!member_p && decl)
18591 tree saved_current_function_decl = NULL_TREE;
18593 /* If the entity being declared is a function, pretend that we
18594 are in its scope. If it is a `friend', it may have access to
18595 things that would not otherwise be accessible. */
18596 if (TREE_CODE (decl) == FUNCTION_DECL)
18598 saved_current_function_decl = current_function_decl;
18599 current_function_decl = decl;
18602 /* Perform access checks for template parameters. */
18603 cp_parser_perform_template_parameter_access_checks (checks);
18605 /* Perform the access control checks for the declarator and the
18606 decl-specifiers. */
18607 perform_deferred_access_checks (tf_warning_or_error);
18609 /* Restore the saved value. */
18610 if (TREE_CODE (decl) == FUNCTION_DECL)
18611 current_function_decl = saved_current_function_decl;
18614 /* Parse the initializer. */
18615 initializer = NULL_TREE;
18616 is_direct_init = false;
18617 is_non_constant_init = true;
18618 if (is_initialized)
18620 if (function_declarator_p (declarator))
18622 if (initialization_kind == CPP_EQ)
18623 initializer = cp_parser_pure_specifier (parser);
18624 else
18626 /* If the declaration was erroneous, we don't really
18627 know what the user intended, so just silently
18628 consume the initializer. */
18629 if (decl != error_mark_node)
18630 error_at (tmp_init_loc, "initializer provided for function");
18631 cp_parser_skip_to_closing_parenthesis (parser,
18632 /*recovering=*/true,
18633 /*or_comma=*/false,
18634 /*consume_paren=*/true);
18637 else
18639 /* We want to record the extra mangling scope for in-class
18640 initializers of class members and initializers of static data
18641 member templates. The former involves deferring
18642 parsing of the initializer until end of class as with default
18643 arguments. So right here we only handle the latter. */
18644 if (!member_p && processing_template_decl)
18645 start_lambda_scope (decl);
18646 initializer = cp_parser_initializer (parser,
18647 &is_direct_init,
18648 &is_non_constant_init);
18649 if (!member_p && processing_template_decl)
18650 finish_lambda_scope ();
18651 if (initializer == error_mark_node)
18652 cp_parser_skip_to_end_of_statement (parser);
18656 /* The old parser allows attributes to appear after a parenthesized
18657 initializer. Mark Mitchell proposed removing this functionality
18658 on the GCC mailing lists on 2002-08-13. This parser accepts the
18659 attributes -- but ignores them. */
18660 if (cp_parser_allow_gnu_extensions_p (parser)
18661 && initialization_kind == CPP_OPEN_PAREN)
18662 if (cp_parser_attributes_opt (parser))
18663 warning (OPT_Wattributes,
18664 "attributes after parenthesized initializer ignored");
18666 /* And now complain about a non-function implicit template. */
18667 if (bogus_implicit_tmpl && decl != error_mark_node)
18668 error_at (DECL_SOURCE_LOCATION (decl),
18669 "non-function %qD declared as implicit template", decl);
18671 /* For an in-class declaration, use `grokfield' to create the
18672 declaration. */
18673 if (member_p)
18675 if (pushed_scope)
18677 pop_scope (pushed_scope);
18678 pushed_scope = NULL_TREE;
18680 decl = grokfield (declarator, decl_specifiers,
18681 initializer, !is_non_constant_init,
18682 /*asmspec=*/NULL_TREE,
18683 chainon (attributes, prefix_attributes));
18684 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18685 cp_parser_save_default_args (parser, decl);
18686 cp_finalize_omp_declare_simd (parser, decl);
18687 cp_finalize_oacc_routine (parser, decl, false);
18690 /* Finish processing the declaration. But, skip member
18691 declarations. */
18692 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18694 cp_finish_decl (decl,
18695 initializer, !is_non_constant_init,
18696 asm_specification,
18697 /* If the initializer is in parentheses, then this is
18698 a direct-initialization, which means that an
18699 `explicit' constructor is OK. Otherwise, an
18700 `explicit' constructor cannot be used. */
18701 ((is_direct_init || !is_initialized)
18702 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18704 else if ((cxx_dialect != cxx98) && friend_p
18705 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18706 /* Core issue #226 (C++0x only): A default template-argument
18707 shall not be specified in a friend class template
18708 declaration. */
18709 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18710 /*is_partial=*/false, /*is_friend_decl=*/1);
18712 if (!friend_p && pushed_scope)
18713 pop_scope (pushed_scope);
18715 if (function_declarator_p (declarator)
18716 && parser->fully_implicit_function_template_p)
18718 if (member_p)
18719 decl = finish_fully_implicit_template (parser, decl);
18720 else
18721 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18724 if (auto_result && is_initialized && decl_specifiers->type
18725 && type_uses_auto (decl_specifiers->type))
18726 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18728 return decl;
18731 /* Parse a declarator.
18733 declarator:
18734 direct-declarator
18735 ptr-operator declarator
18737 abstract-declarator:
18738 ptr-operator abstract-declarator [opt]
18739 direct-abstract-declarator
18741 GNU Extensions:
18743 declarator:
18744 attributes [opt] direct-declarator
18745 attributes [opt] ptr-operator declarator
18747 abstract-declarator:
18748 attributes [opt] ptr-operator abstract-declarator [opt]
18749 attributes [opt] direct-abstract-declarator
18751 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18752 detect constructor, destructor or conversion operators. It is set
18753 to -1 if the declarator is a name, and +1 if it is a
18754 function. Otherwise it is set to zero. Usually you just want to
18755 test for >0, but internally the negative value is used.
18757 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18758 a decl-specifier-seq unless it declares a constructor, destructor,
18759 or conversion. It might seem that we could check this condition in
18760 semantic analysis, rather than parsing, but that makes it difficult
18761 to handle something like `f()'. We want to notice that there are
18762 no decl-specifiers, and therefore realize that this is an
18763 expression, not a declaration.)
18765 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18766 the declarator is a direct-declarator of the form "(...)".
18768 MEMBER_P is true iff this declarator is a member-declarator.
18770 FRIEND_P is true iff this declarator is a friend. */
18772 static cp_declarator *
18773 cp_parser_declarator (cp_parser* parser,
18774 cp_parser_declarator_kind dcl_kind,
18775 int* ctor_dtor_or_conv_p,
18776 bool* parenthesized_p,
18777 bool member_p, bool friend_p)
18779 cp_declarator *declarator;
18780 enum tree_code code;
18781 cp_cv_quals cv_quals;
18782 tree class_type;
18783 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18785 /* Assume this is not a constructor, destructor, or type-conversion
18786 operator. */
18787 if (ctor_dtor_or_conv_p)
18788 *ctor_dtor_or_conv_p = 0;
18790 if (cp_parser_allow_gnu_extensions_p (parser))
18791 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18793 /* Check for the ptr-operator production. */
18794 cp_parser_parse_tentatively (parser);
18795 /* Parse the ptr-operator. */
18796 code = cp_parser_ptr_operator (parser,
18797 &class_type,
18798 &cv_quals,
18799 &std_attributes);
18801 /* If that worked, then we have a ptr-operator. */
18802 if (cp_parser_parse_definitely (parser))
18804 /* If a ptr-operator was found, then this declarator was not
18805 parenthesized. */
18806 if (parenthesized_p)
18807 *parenthesized_p = true;
18808 /* The dependent declarator is optional if we are parsing an
18809 abstract-declarator. */
18810 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18811 cp_parser_parse_tentatively (parser);
18813 /* Parse the dependent declarator. */
18814 declarator = cp_parser_declarator (parser, dcl_kind,
18815 /*ctor_dtor_or_conv_p=*/NULL,
18816 /*parenthesized_p=*/NULL,
18817 /*member_p=*/false,
18818 friend_p);
18820 /* If we are parsing an abstract-declarator, we must handle the
18821 case where the dependent declarator is absent. */
18822 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18823 && !cp_parser_parse_definitely (parser))
18824 declarator = NULL;
18826 declarator = cp_parser_make_indirect_declarator
18827 (code, class_type, cv_quals, declarator, std_attributes);
18829 /* Everything else is a direct-declarator. */
18830 else
18832 if (parenthesized_p)
18833 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18834 CPP_OPEN_PAREN);
18835 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18836 ctor_dtor_or_conv_p,
18837 member_p, friend_p);
18840 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18841 declarator->attributes = gnu_attributes;
18842 return declarator;
18845 /* Parse a direct-declarator or direct-abstract-declarator.
18847 direct-declarator:
18848 declarator-id
18849 direct-declarator ( parameter-declaration-clause )
18850 cv-qualifier-seq [opt]
18851 ref-qualifier [opt]
18852 exception-specification [opt]
18853 direct-declarator [ constant-expression [opt] ]
18854 ( declarator )
18856 direct-abstract-declarator:
18857 direct-abstract-declarator [opt]
18858 ( parameter-declaration-clause )
18859 cv-qualifier-seq [opt]
18860 ref-qualifier [opt]
18861 exception-specification [opt]
18862 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18863 ( abstract-declarator )
18865 Returns a representation of the declarator. DCL_KIND is
18866 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18867 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18868 we are parsing a direct-declarator. It is
18869 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18870 of ambiguity we prefer an abstract declarator, as per
18871 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18872 as for cp_parser_declarator. */
18874 static cp_declarator *
18875 cp_parser_direct_declarator (cp_parser* parser,
18876 cp_parser_declarator_kind dcl_kind,
18877 int* ctor_dtor_or_conv_p,
18878 bool member_p, bool friend_p)
18880 cp_token *token;
18881 cp_declarator *declarator = NULL;
18882 tree scope = NULL_TREE;
18883 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18884 bool saved_in_declarator_p = parser->in_declarator_p;
18885 bool first = true;
18886 tree pushed_scope = NULL_TREE;
18888 while (true)
18890 /* Peek at the next token. */
18891 token = cp_lexer_peek_token (parser->lexer);
18892 if (token->type == CPP_OPEN_PAREN)
18894 /* This is either a parameter-declaration-clause, or a
18895 parenthesized declarator. When we know we are parsing a
18896 named declarator, it must be a parenthesized declarator
18897 if FIRST is true. For instance, `(int)' is a
18898 parameter-declaration-clause, with an omitted
18899 direct-abstract-declarator. But `((*))', is a
18900 parenthesized abstract declarator. Finally, when T is a
18901 template parameter `(T)' is a
18902 parameter-declaration-clause, and not a parenthesized
18903 named declarator.
18905 We first try and parse a parameter-declaration-clause,
18906 and then try a nested declarator (if FIRST is true).
18908 It is not an error for it not to be a
18909 parameter-declaration-clause, even when FIRST is
18910 false. Consider,
18912 int i (int);
18913 int i (3);
18915 The first is the declaration of a function while the
18916 second is the definition of a variable, including its
18917 initializer.
18919 Having seen only the parenthesis, we cannot know which of
18920 these two alternatives should be selected. Even more
18921 complex are examples like:
18923 int i (int (a));
18924 int i (int (3));
18926 The former is a function-declaration; the latter is a
18927 variable initialization.
18929 Thus again, we try a parameter-declaration-clause, and if
18930 that fails, we back out and return. */
18932 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18934 tree params;
18935 bool is_declarator = false;
18937 /* In a member-declarator, the only valid interpretation
18938 of a parenthesis is the start of a
18939 parameter-declaration-clause. (It is invalid to
18940 initialize a static data member with a parenthesized
18941 initializer; only the "=" form of initialization is
18942 permitted.) */
18943 if (!member_p)
18944 cp_parser_parse_tentatively (parser);
18946 /* Consume the `('. */
18947 cp_lexer_consume_token (parser->lexer);
18948 if (first)
18950 /* If this is going to be an abstract declarator, we're
18951 in a declarator and we can't have default args. */
18952 parser->default_arg_ok_p = false;
18953 parser->in_declarator_p = true;
18956 begin_scope (sk_function_parms, NULL_TREE);
18958 /* Parse the parameter-declaration-clause. */
18959 params = cp_parser_parameter_declaration_clause (parser);
18961 /* Consume the `)'. */
18962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18964 /* If all went well, parse the cv-qualifier-seq,
18965 ref-qualifier and the exception-specification. */
18966 if (member_p || cp_parser_parse_definitely (parser))
18968 cp_cv_quals cv_quals;
18969 cp_virt_specifiers virt_specifiers;
18970 cp_ref_qualifier ref_qual;
18971 tree exception_specification;
18972 tree late_return;
18973 tree attrs;
18974 bool memfn = (member_p || (pushed_scope
18975 && CLASS_TYPE_P (pushed_scope)));
18977 is_declarator = true;
18979 if (ctor_dtor_or_conv_p)
18980 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18981 first = false;
18983 /* Parse the cv-qualifier-seq. */
18984 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18985 /* Parse the ref-qualifier. */
18986 ref_qual = cp_parser_ref_qualifier_opt (parser);
18987 /* Parse the tx-qualifier. */
18988 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18989 /* And the exception-specification. */
18990 exception_specification
18991 = cp_parser_exception_specification_opt (parser);
18993 attrs = cp_parser_std_attribute_spec_seq (parser);
18995 /* In here, we handle cases where attribute is used after
18996 the function declaration. For example:
18997 void func (int x) __attribute__((vector(..))); */
18998 tree gnu_attrs = NULL_TREE;
18999 if (flag_cilkplus
19000 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19002 cp_parser_parse_tentatively (parser);
19003 tree attr = cp_parser_gnu_attributes_opt (parser);
19004 if (cp_lexer_next_token_is_not (parser->lexer,
19005 CPP_SEMICOLON)
19006 && cp_lexer_next_token_is_not (parser->lexer,
19007 CPP_OPEN_BRACE))
19008 cp_parser_abort_tentative_parse (parser);
19009 else if (!cp_parser_parse_definitely (parser))
19011 else
19012 gnu_attrs = attr;
19014 tree requires_clause = NULL_TREE;
19015 late_return = (cp_parser_late_return_type_opt
19016 (parser, declarator, requires_clause,
19017 memfn ? cv_quals : -1));
19019 /* Parse the virt-specifier-seq. */
19020 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19022 /* Create the function-declarator. */
19023 declarator = make_call_declarator (declarator,
19024 params,
19025 cv_quals,
19026 virt_specifiers,
19027 ref_qual,
19028 tx_qual,
19029 exception_specification,
19030 late_return,
19031 requires_clause);
19032 declarator->std_attributes = attrs;
19033 declarator->attributes = gnu_attrs;
19034 /* Any subsequent parameter lists are to do with
19035 return type, so are not those of the declared
19036 function. */
19037 parser->default_arg_ok_p = false;
19040 /* Remove the function parms from scope. */
19041 pop_bindings_and_leave_scope ();
19043 if (is_declarator)
19044 /* Repeat the main loop. */
19045 continue;
19048 /* If this is the first, we can try a parenthesized
19049 declarator. */
19050 if (first)
19052 bool saved_in_type_id_in_expr_p;
19054 parser->default_arg_ok_p = saved_default_arg_ok_p;
19055 parser->in_declarator_p = saved_in_declarator_p;
19057 /* Consume the `('. */
19058 cp_lexer_consume_token (parser->lexer);
19059 /* Parse the nested declarator. */
19060 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19061 parser->in_type_id_in_expr_p = true;
19062 declarator
19063 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19064 /*parenthesized_p=*/NULL,
19065 member_p, friend_p);
19066 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19067 first = false;
19068 /* Expect a `)'. */
19069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19070 declarator = cp_error_declarator;
19071 if (declarator == cp_error_declarator)
19072 break;
19074 goto handle_declarator;
19076 /* Otherwise, we must be done. */
19077 else
19078 break;
19080 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19081 && token->type == CPP_OPEN_SQUARE
19082 && !cp_next_tokens_can_be_attribute_p (parser))
19084 /* Parse an array-declarator. */
19085 tree bounds, attrs;
19087 if (ctor_dtor_or_conv_p)
19088 *ctor_dtor_or_conv_p = 0;
19090 first = false;
19091 parser->default_arg_ok_p = false;
19092 parser->in_declarator_p = true;
19093 /* Consume the `['. */
19094 cp_lexer_consume_token (parser->lexer);
19095 /* Peek at the next token. */
19096 token = cp_lexer_peek_token (parser->lexer);
19097 /* If the next token is `]', then there is no
19098 constant-expression. */
19099 if (token->type != CPP_CLOSE_SQUARE)
19101 bool non_constant_p;
19102 bounds
19103 = cp_parser_constant_expression (parser,
19104 /*allow_non_constant=*/true,
19105 &non_constant_p);
19106 if (!non_constant_p)
19107 /* OK */;
19108 else if (error_operand_p (bounds))
19109 /* Already gave an error. */;
19110 else if (!parser->in_function_body
19111 || current_binding_level->kind == sk_function_parms)
19113 /* Normally, the array bound must be an integral constant
19114 expression. However, as an extension, we allow VLAs
19115 in function scopes as long as they aren't part of a
19116 parameter declaration. */
19117 cp_parser_error (parser,
19118 "array bound is not an integer constant");
19119 bounds = error_mark_node;
19121 else if (processing_template_decl
19122 && !type_dependent_expression_p (bounds))
19124 /* Remember this wasn't a constant-expression. */
19125 bounds = build_nop (TREE_TYPE (bounds), bounds);
19126 TREE_SIDE_EFFECTS (bounds) = 1;
19129 else
19130 bounds = NULL_TREE;
19131 /* Look for the closing `]'. */
19132 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19134 declarator = cp_error_declarator;
19135 break;
19138 attrs = cp_parser_std_attribute_spec_seq (parser);
19139 declarator = make_array_declarator (declarator, bounds);
19140 declarator->std_attributes = attrs;
19142 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19145 tree qualifying_scope;
19146 tree unqualified_name;
19147 tree attrs;
19148 special_function_kind sfk;
19149 bool abstract_ok;
19150 bool pack_expansion_p = false;
19151 cp_token *declarator_id_start_token;
19153 /* Parse a declarator-id */
19154 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19155 if (abstract_ok)
19157 cp_parser_parse_tentatively (parser);
19159 /* If we see an ellipsis, we should be looking at a
19160 parameter pack. */
19161 if (token->type == CPP_ELLIPSIS)
19163 /* Consume the `...' */
19164 cp_lexer_consume_token (parser->lexer);
19166 pack_expansion_p = true;
19170 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19171 unqualified_name
19172 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19173 qualifying_scope = parser->scope;
19174 if (abstract_ok)
19176 bool okay = false;
19178 if (!unqualified_name && pack_expansion_p)
19180 /* Check whether an error occurred. */
19181 okay = !cp_parser_error_occurred (parser);
19183 /* We already consumed the ellipsis to mark a
19184 parameter pack, but we have no way to report it,
19185 so abort the tentative parse. We will be exiting
19186 immediately anyway. */
19187 cp_parser_abort_tentative_parse (parser);
19189 else
19190 okay = cp_parser_parse_definitely (parser);
19192 if (!okay)
19193 unqualified_name = error_mark_node;
19194 else if (unqualified_name
19195 && (qualifying_scope
19196 || (!identifier_p (unqualified_name))))
19198 cp_parser_error (parser, "expected unqualified-id");
19199 unqualified_name = error_mark_node;
19203 if (!unqualified_name)
19204 return NULL;
19205 if (unqualified_name == error_mark_node)
19207 declarator = cp_error_declarator;
19208 pack_expansion_p = false;
19209 declarator->parameter_pack_p = false;
19210 break;
19213 attrs = cp_parser_std_attribute_spec_seq (parser);
19215 if (qualifying_scope && at_namespace_scope_p ()
19216 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19218 /* In the declaration of a member of a template class
19219 outside of the class itself, the SCOPE will sometimes
19220 be a TYPENAME_TYPE. For example, given:
19222 template <typename T>
19223 int S<T>::R::i = 3;
19225 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19226 this context, we must resolve S<T>::R to an ordinary
19227 type, rather than a typename type.
19229 The reason we normally avoid resolving TYPENAME_TYPEs
19230 is that a specialization of `S' might render
19231 `S<T>::R' not a type. However, if `S' is
19232 specialized, then this `i' will not be used, so there
19233 is no harm in resolving the types here. */
19234 tree type;
19236 /* Resolve the TYPENAME_TYPE. */
19237 type = resolve_typename_type (qualifying_scope,
19238 /*only_current_p=*/false);
19239 /* If that failed, the declarator is invalid. */
19240 if (TREE_CODE (type) == TYPENAME_TYPE)
19242 if (typedef_variant_p (type))
19243 error_at (declarator_id_start_token->location,
19244 "cannot define member of dependent typedef "
19245 "%qT", type);
19246 else
19247 error_at (declarator_id_start_token->location,
19248 "%<%T::%E%> is not a type",
19249 TYPE_CONTEXT (qualifying_scope),
19250 TYPE_IDENTIFIER (qualifying_scope));
19252 qualifying_scope = type;
19255 sfk = sfk_none;
19257 if (unqualified_name)
19259 tree class_type;
19261 if (qualifying_scope
19262 && CLASS_TYPE_P (qualifying_scope))
19263 class_type = qualifying_scope;
19264 else
19265 class_type = current_class_type;
19267 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19269 tree name_type = TREE_TYPE (unqualified_name);
19270 if (class_type && same_type_p (name_type, class_type))
19272 if (qualifying_scope
19273 && CLASSTYPE_USE_TEMPLATE (name_type))
19275 error_at (declarator_id_start_token->location,
19276 "invalid use of constructor as a template");
19277 inform (declarator_id_start_token->location,
19278 "use %<%T::%D%> instead of %<%T::%D%> to "
19279 "name the constructor in a qualified name",
19280 class_type,
19281 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19282 class_type, name_type);
19283 declarator = cp_error_declarator;
19284 break;
19286 else
19287 unqualified_name = constructor_name (class_type);
19289 else
19291 /* We do not attempt to print the declarator
19292 here because we do not have enough
19293 information about its original syntactic
19294 form. */
19295 cp_parser_error (parser, "invalid declarator");
19296 declarator = cp_error_declarator;
19297 break;
19301 if (class_type)
19303 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19304 sfk = sfk_destructor;
19305 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19306 sfk = sfk_conversion;
19307 else if (/* There's no way to declare a constructor
19308 for an anonymous type, even if the type
19309 got a name for linkage purposes. */
19310 !TYPE_WAS_ANONYMOUS (class_type)
19311 /* Handle correctly (c++/19200):
19313 struct S {
19314 struct T{};
19315 friend void S(T);
19318 and also:
19320 namespace N {
19321 void S();
19324 struct S {
19325 friend void N::S();
19326 }; */
19327 && !(friend_p
19328 && class_type != qualifying_scope)
19329 && constructor_name_p (unqualified_name,
19330 class_type))
19332 unqualified_name = constructor_name (class_type);
19333 sfk = sfk_constructor;
19335 else if (is_overloaded_fn (unqualified_name)
19336 && DECL_CONSTRUCTOR_P (get_first_fn
19337 (unqualified_name)))
19338 sfk = sfk_constructor;
19340 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19341 *ctor_dtor_or_conv_p = -1;
19344 declarator = make_id_declarator (qualifying_scope,
19345 unqualified_name,
19346 sfk);
19347 declarator->std_attributes = attrs;
19348 declarator->id_loc = token->location;
19349 declarator->parameter_pack_p = pack_expansion_p;
19351 if (pack_expansion_p)
19352 maybe_warn_variadic_templates ();
19355 handle_declarator:;
19356 scope = get_scope_of_declarator (declarator);
19357 if (scope)
19359 /* Any names that appear after the declarator-id for a
19360 member are looked up in the containing scope. */
19361 if (at_function_scope_p ())
19363 /* But declarations with qualified-ids can't appear in a
19364 function. */
19365 cp_parser_error (parser, "qualified-id in declaration");
19366 declarator = cp_error_declarator;
19367 break;
19369 pushed_scope = push_scope (scope);
19371 parser->in_declarator_p = true;
19372 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19373 || (declarator && declarator->kind == cdk_id))
19374 /* Default args are only allowed on function
19375 declarations. */
19376 parser->default_arg_ok_p = saved_default_arg_ok_p;
19377 else
19378 parser->default_arg_ok_p = false;
19380 first = false;
19382 /* We're done. */
19383 else
19384 break;
19387 /* For an abstract declarator, we might wind up with nothing at this
19388 point. That's an error; the declarator is not optional. */
19389 if (!declarator)
19390 cp_parser_error (parser, "expected declarator");
19392 /* If we entered a scope, we must exit it now. */
19393 if (pushed_scope)
19394 pop_scope (pushed_scope);
19396 parser->default_arg_ok_p = saved_default_arg_ok_p;
19397 parser->in_declarator_p = saved_in_declarator_p;
19399 return declarator;
19402 /* Parse a ptr-operator.
19404 ptr-operator:
19405 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19406 * cv-qualifier-seq [opt]
19408 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19409 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19411 GNU Extension:
19413 ptr-operator:
19414 & cv-qualifier-seq [opt]
19416 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19417 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19418 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19419 filled in with the TYPE containing the member. *CV_QUALS is
19420 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19421 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19422 Note that the tree codes returned by this function have nothing
19423 to do with the types of trees that will be eventually be created
19424 to represent the pointer or reference type being parsed. They are
19425 just constants with suggestive names. */
19426 static enum tree_code
19427 cp_parser_ptr_operator (cp_parser* parser,
19428 tree* type,
19429 cp_cv_quals *cv_quals,
19430 tree *attributes)
19432 enum tree_code code = ERROR_MARK;
19433 cp_token *token;
19434 tree attrs = NULL_TREE;
19436 /* Assume that it's not a pointer-to-member. */
19437 *type = NULL_TREE;
19438 /* And that there are no cv-qualifiers. */
19439 *cv_quals = TYPE_UNQUALIFIED;
19441 /* Peek at the next token. */
19442 token = cp_lexer_peek_token (parser->lexer);
19444 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19445 if (token->type == CPP_MULT)
19446 code = INDIRECT_REF;
19447 else if (token->type == CPP_AND)
19448 code = ADDR_EXPR;
19449 else if ((cxx_dialect != cxx98) &&
19450 token->type == CPP_AND_AND) /* C++0x only */
19451 code = NON_LVALUE_EXPR;
19453 if (code != ERROR_MARK)
19455 /* Consume the `*', `&' or `&&'. */
19456 cp_lexer_consume_token (parser->lexer);
19458 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19459 `&', if we are allowing GNU extensions. (The only qualifier
19460 that can legally appear after `&' is `restrict', but that is
19461 enforced during semantic analysis. */
19462 if (code == INDIRECT_REF
19463 || cp_parser_allow_gnu_extensions_p (parser))
19464 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19466 attrs = cp_parser_std_attribute_spec_seq (parser);
19467 if (attributes != NULL)
19468 *attributes = attrs;
19470 else
19472 /* Try the pointer-to-member case. */
19473 cp_parser_parse_tentatively (parser);
19474 /* Look for the optional `::' operator. */
19475 cp_parser_global_scope_opt (parser,
19476 /*current_scope_valid_p=*/false);
19477 /* Look for the nested-name specifier. */
19478 token = cp_lexer_peek_token (parser->lexer);
19479 cp_parser_nested_name_specifier (parser,
19480 /*typename_keyword_p=*/false,
19481 /*check_dependency_p=*/true,
19482 /*type_p=*/false,
19483 /*is_declaration=*/false);
19484 /* If we found it, and the next token is a `*', then we are
19485 indeed looking at a pointer-to-member operator. */
19486 if (!cp_parser_error_occurred (parser)
19487 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19489 /* Indicate that the `*' operator was used. */
19490 code = INDIRECT_REF;
19492 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19493 error_at (token->location, "%qD is a namespace", parser->scope);
19494 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19495 error_at (token->location, "cannot form pointer to member of "
19496 "non-class %q#T", parser->scope);
19497 else
19499 /* The type of which the member is a member is given by the
19500 current SCOPE. */
19501 *type = parser->scope;
19502 /* The next name will not be qualified. */
19503 parser->scope = NULL_TREE;
19504 parser->qualifying_scope = NULL_TREE;
19505 parser->object_scope = NULL_TREE;
19506 /* Look for optional c++11 attributes. */
19507 attrs = cp_parser_std_attribute_spec_seq (parser);
19508 if (attributes != NULL)
19509 *attributes = attrs;
19510 /* Look for the optional cv-qualifier-seq. */
19511 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19514 /* If that didn't work we don't have a ptr-operator. */
19515 if (!cp_parser_parse_definitely (parser))
19516 cp_parser_error (parser, "expected ptr-operator");
19519 return code;
19522 /* Parse an (optional) cv-qualifier-seq.
19524 cv-qualifier-seq:
19525 cv-qualifier cv-qualifier-seq [opt]
19527 cv-qualifier:
19528 const
19529 volatile
19531 GNU Extension:
19533 cv-qualifier:
19534 __restrict__
19536 Returns a bitmask representing the cv-qualifiers. */
19538 static cp_cv_quals
19539 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19541 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19543 while (true)
19545 cp_token *token;
19546 cp_cv_quals cv_qualifier;
19548 /* Peek at the next token. */
19549 token = cp_lexer_peek_token (parser->lexer);
19550 /* See if it's a cv-qualifier. */
19551 switch (token->keyword)
19553 case RID_CONST:
19554 cv_qualifier = TYPE_QUAL_CONST;
19555 break;
19557 case RID_VOLATILE:
19558 cv_qualifier = TYPE_QUAL_VOLATILE;
19559 break;
19561 case RID_RESTRICT:
19562 cv_qualifier = TYPE_QUAL_RESTRICT;
19563 break;
19565 default:
19566 cv_qualifier = TYPE_UNQUALIFIED;
19567 break;
19570 if (!cv_qualifier)
19571 break;
19573 if (cv_quals & cv_qualifier)
19575 error_at (token->location, "duplicate cv-qualifier");
19576 cp_lexer_purge_token (parser->lexer);
19578 else
19580 cp_lexer_consume_token (parser->lexer);
19581 cv_quals |= cv_qualifier;
19585 return cv_quals;
19588 /* Parse an (optional) ref-qualifier
19590 ref-qualifier:
19594 Returns cp_ref_qualifier representing ref-qualifier. */
19596 static cp_ref_qualifier
19597 cp_parser_ref_qualifier_opt (cp_parser* parser)
19599 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19601 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19602 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19603 return ref_qual;
19605 while (true)
19607 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19608 cp_token *token = cp_lexer_peek_token (parser->lexer);
19610 switch (token->type)
19612 case CPP_AND:
19613 curr_ref_qual = REF_QUAL_LVALUE;
19614 break;
19616 case CPP_AND_AND:
19617 curr_ref_qual = REF_QUAL_RVALUE;
19618 break;
19620 default:
19621 curr_ref_qual = REF_QUAL_NONE;
19622 break;
19625 if (!curr_ref_qual)
19626 break;
19627 else if (ref_qual)
19629 error_at (token->location, "multiple ref-qualifiers");
19630 cp_lexer_purge_token (parser->lexer);
19632 else
19634 ref_qual = curr_ref_qual;
19635 cp_lexer_consume_token (parser->lexer);
19639 return ref_qual;
19642 /* Parse an optional tx-qualifier.
19644 tx-qualifier:
19645 transaction_safe
19646 transaction_safe_dynamic */
19648 static tree
19649 cp_parser_tx_qualifier_opt (cp_parser *parser)
19651 cp_token *token = cp_lexer_peek_token (parser->lexer);
19652 if (token->type == CPP_NAME)
19654 tree name = token->u.value;
19655 const char *p = IDENTIFIER_POINTER (name);
19656 const int len = strlen ("transaction_safe");
19657 if (!strncmp (p, "transaction_safe", len))
19659 p += len;
19660 if (*p == '\0'
19661 || !strcmp (p, "_dynamic"))
19663 cp_lexer_consume_token (parser->lexer);
19664 if (!flag_tm)
19666 error ("%E requires %<-fgnu-tm%>", name);
19667 return NULL_TREE;
19669 else
19670 return name;
19674 return NULL_TREE;
19677 /* Parse an (optional) virt-specifier-seq.
19679 virt-specifier-seq:
19680 virt-specifier virt-specifier-seq [opt]
19682 virt-specifier:
19683 override
19684 final
19686 Returns a bitmask representing the virt-specifiers. */
19688 static cp_virt_specifiers
19689 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19691 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19693 while (true)
19695 cp_token *token;
19696 cp_virt_specifiers virt_specifier;
19698 /* Peek at the next token. */
19699 token = cp_lexer_peek_token (parser->lexer);
19700 /* See if it's a virt-specifier-qualifier. */
19701 if (token->type != CPP_NAME)
19702 break;
19703 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19705 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19706 virt_specifier = VIRT_SPEC_OVERRIDE;
19708 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19710 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19711 virt_specifier = VIRT_SPEC_FINAL;
19713 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19715 virt_specifier = VIRT_SPEC_FINAL;
19717 else
19718 break;
19720 if (virt_specifiers & virt_specifier)
19722 error_at (token->location, "duplicate virt-specifier");
19723 cp_lexer_purge_token (parser->lexer);
19725 else
19727 cp_lexer_consume_token (parser->lexer);
19728 virt_specifiers |= virt_specifier;
19731 return virt_specifiers;
19734 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19735 is in scope even though it isn't real. */
19737 void
19738 inject_this_parameter (tree ctype, cp_cv_quals quals)
19740 tree this_parm;
19742 if (current_class_ptr)
19744 /* We don't clear this between NSDMIs. Is it already what we want? */
19745 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19746 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19747 && cp_type_quals (type) == quals)
19748 return;
19751 this_parm = build_this_parm (ctype, quals);
19752 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19753 current_class_ptr = NULL_TREE;
19754 current_class_ref
19755 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19756 current_class_ptr = this_parm;
19759 /* Return true iff our current scope is a non-static data member
19760 initializer. */
19762 bool
19763 parsing_nsdmi (void)
19765 /* We recognize NSDMI context by the context-less 'this' pointer set up
19766 by the function above. */
19767 if (current_class_ptr
19768 && TREE_CODE (current_class_ptr) == PARM_DECL
19769 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19770 return true;
19771 return false;
19774 /* Parse a late-specified return type, if any. This is not a separate
19775 non-terminal, but part of a function declarator, which looks like
19777 -> trailing-type-specifier-seq abstract-declarator(opt)
19779 Returns the type indicated by the type-id.
19781 In addition to this, parse any queued up omp declare simd
19782 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19784 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19785 function. */
19787 static tree
19788 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19789 tree& requires_clause, cp_cv_quals quals)
19791 cp_token *token;
19792 tree type = NULL_TREE;
19793 bool declare_simd_p = (parser->omp_declare_simd
19794 && declarator
19795 && declarator->kind == cdk_id);
19797 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19798 && declarator && declarator->kind == cdk_id);
19800 bool oacc_routine_p = (parser->oacc_routine
19801 && declarator
19802 && declarator->kind == cdk_id);
19804 /* Peek at the next token. */
19805 token = cp_lexer_peek_token (parser->lexer);
19806 /* A late-specified return type is indicated by an initial '->'. */
19807 if (token->type != CPP_DEREF
19808 && token->keyword != RID_REQUIRES
19809 && !(token->type == CPP_NAME
19810 && token->u.value == ridpointers[RID_REQUIRES])
19811 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19812 return NULL_TREE;
19814 tree save_ccp = current_class_ptr;
19815 tree save_ccr = current_class_ref;
19816 if (quals >= 0)
19818 /* DR 1207: 'this' is in scope in the trailing return type. */
19819 inject_this_parameter (current_class_type, quals);
19822 if (token->type == CPP_DEREF)
19824 /* Consume the ->. */
19825 cp_lexer_consume_token (parser->lexer);
19827 type = cp_parser_trailing_type_id (parser);
19830 /* Function declarations may be followed by a trailing
19831 requires-clause. */
19832 requires_clause = cp_parser_requires_clause_opt (parser);
19834 if (cilk_simd_fn_vector_p)
19835 declarator->attributes
19836 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19837 declarator->attributes);
19838 if (declare_simd_p)
19839 declarator->attributes
19840 = cp_parser_late_parsing_omp_declare_simd (parser,
19841 declarator->attributes);
19842 if (oacc_routine_p)
19843 declarator->attributes
19844 = cp_parser_late_parsing_oacc_routine (parser,
19845 declarator->attributes);
19847 if (quals >= 0)
19849 current_class_ptr = save_ccp;
19850 current_class_ref = save_ccr;
19853 return type;
19856 /* Parse a declarator-id.
19858 declarator-id:
19859 id-expression
19860 :: [opt] nested-name-specifier [opt] type-name
19862 In the `id-expression' case, the value returned is as for
19863 cp_parser_id_expression if the id-expression was an unqualified-id.
19864 If the id-expression was a qualified-id, then a SCOPE_REF is
19865 returned. The first operand is the scope (either a NAMESPACE_DECL
19866 or TREE_TYPE), but the second is still just a representation of an
19867 unqualified-id. */
19869 static tree
19870 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19872 tree id;
19873 /* The expression must be an id-expression. Assume that qualified
19874 names are the names of types so that:
19876 template <class T>
19877 int S<T>::R::i = 3;
19879 will work; we must treat `S<T>::R' as the name of a type.
19880 Similarly, assume that qualified names are templates, where
19881 required, so that:
19883 template <class T>
19884 int S<T>::R<T>::i = 3;
19886 will work, too. */
19887 id = cp_parser_id_expression (parser,
19888 /*template_keyword_p=*/false,
19889 /*check_dependency_p=*/false,
19890 /*template_p=*/NULL,
19891 /*declarator_p=*/true,
19892 optional_p);
19893 if (id && BASELINK_P (id))
19894 id = BASELINK_FUNCTIONS (id);
19895 return id;
19898 /* Parse a type-id.
19900 type-id:
19901 type-specifier-seq abstract-declarator [opt]
19903 Returns the TYPE specified. */
19905 static tree
19906 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19907 bool is_trailing_return)
19909 cp_decl_specifier_seq type_specifier_seq;
19910 cp_declarator *abstract_declarator;
19912 /* Parse the type-specifier-seq. */
19913 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19914 is_trailing_return,
19915 &type_specifier_seq);
19916 if (type_specifier_seq.type == error_mark_node)
19917 return error_mark_node;
19919 /* There might or might not be an abstract declarator. */
19920 cp_parser_parse_tentatively (parser);
19921 /* Look for the declarator. */
19922 abstract_declarator
19923 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19924 /*parenthesized_p=*/NULL,
19925 /*member_p=*/false,
19926 /*friend_p=*/false);
19927 /* Check to see if there really was a declarator. */
19928 if (!cp_parser_parse_definitely (parser))
19929 abstract_declarator = NULL;
19931 if (type_specifier_seq.type
19932 /* The concepts TS allows 'auto' as a type-id. */
19933 && (!flag_concepts || parser->in_type_id_in_expr_p)
19934 /* None of the valid uses of 'auto' in C++14 involve the type-id
19935 nonterminal, but it is valid in a trailing-return-type. */
19936 && !(cxx_dialect >= cxx14 && is_trailing_return)
19937 && type_uses_auto (type_specifier_seq.type))
19939 /* A type-id with type 'auto' is only ok if the abstract declarator
19940 is a function declarator with a late-specified return type.
19942 A type-id with 'auto' is also valid in a trailing-return-type
19943 in a compound-requirement. */
19944 if (abstract_declarator
19945 && abstract_declarator->kind == cdk_function
19946 && abstract_declarator->u.function.late_return_type)
19947 /* OK */;
19948 else if (parser->in_result_type_constraint_p)
19949 /* OK */;
19950 else
19952 error ("invalid use of %<auto%>");
19953 return error_mark_node;
19957 return groktypename (&type_specifier_seq, abstract_declarator,
19958 is_template_arg);
19961 static tree
19962 cp_parser_type_id (cp_parser *parser)
19964 return cp_parser_type_id_1 (parser, false, false);
19967 static tree
19968 cp_parser_template_type_arg (cp_parser *parser)
19970 tree r;
19971 const char *saved_message = parser->type_definition_forbidden_message;
19972 parser->type_definition_forbidden_message
19973 = G_("types may not be defined in template arguments");
19974 r = cp_parser_type_id_1 (parser, true, false);
19975 parser->type_definition_forbidden_message = saved_message;
19976 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19978 error ("invalid use of %<auto%> in template argument");
19979 r = error_mark_node;
19981 return r;
19984 static tree
19985 cp_parser_trailing_type_id (cp_parser *parser)
19987 return cp_parser_type_id_1 (parser, false, true);
19990 /* Parse a type-specifier-seq.
19992 type-specifier-seq:
19993 type-specifier type-specifier-seq [opt]
19995 GNU extension:
19997 type-specifier-seq:
19998 attributes type-specifier-seq [opt]
20000 If IS_DECLARATION is true, we are at the start of a "condition" or
20001 exception-declaration, so we might be followed by a declarator-id.
20003 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20004 i.e. we've just seen "->".
20006 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20008 static void
20009 cp_parser_type_specifier_seq (cp_parser* parser,
20010 bool is_declaration,
20011 bool is_trailing_return,
20012 cp_decl_specifier_seq *type_specifier_seq)
20014 bool seen_type_specifier = false;
20015 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20016 cp_token *start_token = NULL;
20018 /* Clear the TYPE_SPECIFIER_SEQ. */
20019 clear_decl_specs (type_specifier_seq);
20021 /* In the context of a trailing return type, enum E { } is an
20022 elaborated-type-specifier followed by a function-body, not an
20023 enum-specifier. */
20024 if (is_trailing_return)
20025 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20027 /* Parse the type-specifiers and attributes. */
20028 while (true)
20030 tree type_specifier;
20031 bool is_cv_qualifier;
20033 /* Check for attributes first. */
20034 if (cp_next_tokens_can_be_attribute_p (parser))
20036 type_specifier_seq->attributes =
20037 chainon (type_specifier_seq->attributes,
20038 cp_parser_attributes_opt (parser));
20039 continue;
20042 /* record the token of the beginning of the type specifier seq,
20043 for error reporting purposes*/
20044 if (!start_token)
20045 start_token = cp_lexer_peek_token (parser->lexer);
20047 /* Look for the type-specifier. */
20048 type_specifier = cp_parser_type_specifier (parser,
20049 flags,
20050 type_specifier_seq,
20051 /*is_declaration=*/false,
20052 NULL,
20053 &is_cv_qualifier);
20054 if (!type_specifier)
20056 /* If the first type-specifier could not be found, this is not a
20057 type-specifier-seq at all. */
20058 if (!seen_type_specifier)
20060 /* Set in_declarator_p to avoid skipping to the semicolon. */
20061 int in_decl = parser->in_declarator_p;
20062 parser->in_declarator_p = true;
20064 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20065 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20066 cp_parser_error (parser, "expected type-specifier");
20068 parser->in_declarator_p = in_decl;
20070 type_specifier_seq->type = error_mark_node;
20071 return;
20073 /* If subsequent type-specifiers could not be found, the
20074 type-specifier-seq is complete. */
20075 break;
20078 seen_type_specifier = true;
20079 /* The standard says that a condition can be:
20081 type-specifier-seq declarator = assignment-expression
20083 However, given:
20085 struct S {};
20086 if (int S = ...)
20088 we should treat the "S" as a declarator, not as a
20089 type-specifier. The standard doesn't say that explicitly for
20090 type-specifier-seq, but it does say that for
20091 decl-specifier-seq in an ordinary declaration. Perhaps it
20092 would be clearer just to allow a decl-specifier-seq here, and
20093 then add a semantic restriction that if any decl-specifiers
20094 that are not type-specifiers appear, the program is invalid. */
20095 if (is_declaration && !is_cv_qualifier)
20096 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20100 /* Return whether the function currently being declared has an associated
20101 template parameter list. */
20103 static bool
20104 function_being_declared_is_template_p (cp_parser* parser)
20106 if (!current_template_parms || processing_template_parmlist)
20107 return false;
20109 if (parser->implicit_template_scope)
20110 return true;
20112 if (at_class_scope_p ()
20113 && TYPE_BEING_DEFINED (current_class_type))
20114 return parser->num_template_parameter_lists != 0;
20116 return ((int) parser->num_template_parameter_lists > template_class_depth
20117 (current_class_type));
20120 /* Parse a parameter-declaration-clause.
20122 parameter-declaration-clause:
20123 parameter-declaration-list [opt] ... [opt]
20124 parameter-declaration-list , ...
20126 Returns a representation for the parameter declarations. A return
20127 value of NULL indicates a parameter-declaration-clause consisting
20128 only of an ellipsis. */
20130 static tree
20131 cp_parser_parameter_declaration_clause (cp_parser* parser)
20133 tree parameters;
20134 cp_token *token;
20135 bool ellipsis_p;
20136 bool is_error;
20138 struct cleanup {
20139 cp_parser* parser;
20140 int auto_is_implicit_function_template_parm_p;
20141 ~cleanup() {
20142 parser->auto_is_implicit_function_template_parm_p
20143 = auto_is_implicit_function_template_parm_p;
20145 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20147 (void) cleanup;
20149 if (!processing_specialization
20150 && !processing_template_parmlist
20151 && !processing_explicit_instantiation)
20152 if (!current_function_decl
20153 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20154 parser->auto_is_implicit_function_template_parm_p = true;
20156 /* Peek at the next token. */
20157 token = cp_lexer_peek_token (parser->lexer);
20158 /* Check for trivial parameter-declaration-clauses. */
20159 if (token->type == CPP_ELLIPSIS)
20161 /* Consume the `...' token. */
20162 cp_lexer_consume_token (parser->lexer);
20163 return NULL_TREE;
20165 else if (token->type == CPP_CLOSE_PAREN)
20166 /* There are no parameters. */
20168 #ifndef NO_IMPLICIT_EXTERN_C
20169 if (in_system_header_at (input_location)
20170 && current_class_type == NULL
20171 && current_lang_name == lang_name_c)
20172 return NULL_TREE;
20173 else
20174 #endif
20175 return void_list_node;
20177 /* Check for `(void)', too, which is a special case. */
20178 else if (token->keyword == RID_VOID
20179 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20180 == CPP_CLOSE_PAREN))
20182 /* Consume the `void' token. */
20183 cp_lexer_consume_token (parser->lexer);
20184 /* There are no parameters. */
20185 return void_list_node;
20188 /* Parse the parameter-declaration-list. */
20189 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20190 /* If a parse error occurred while parsing the
20191 parameter-declaration-list, then the entire
20192 parameter-declaration-clause is erroneous. */
20193 if (is_error)
20194 return NULL;
20196 /* Peek at the next token. */
20197 token = cp_lexer_peek_token (parser->lexer);
20198 /* If it's a `,', the clause should terminate with an ellipsis. */
20199 if (token->type == CPP_COMMA)
20201 /* Consume the `,'. */
20202 cp_lexer_consume_token (parser->lexer);
20203 /* Expect an ellipsis. */
20204 ellipsis_p
20205 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20207 /* It might also be `...' if the optional trailing `,' was
20208 omitted. */
20209 else if (token->type == CPP_ELLIPSIS)
20211 /* Consume the `...' token. */
20212 cp_lexer_consume_token (parser->lexer);
20213 /* And remember that we saw it. */
20214 ellipsis_p = true;
20216 else
20217 ellipsis_p = false;
20219 /* Finish the parameter list. */
20220 if (!ellipsis_p)
20221 parameters = chainon (parameters, void_list_node);
20223 return parameters;
20226 /* Parse a parameter-declaration-list.
20228 parameter-declaration-list:
20229 parameter-declaration
20230 parameter-declaration-list , parameter-declaration
20232 Returns a representation of the parameter-declaration-list, as for
20233 cp_parser_parameter_declaration_clause. However, the
20234 `void_list_node' is never appended to the list. Upon return,
20235 *IS_ERROR will be true iff an error occurred. */
20237 static tree
20238 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20240 tree parameters = NULL_TREE;
20241 tree *tail = &parameters;
20242 bool saved_in_unbraced_linkage_specification_p;
20243 int index = 0;
20245 /* Assume all will go well. */
20246 *is_error = false;
20247 /* The special considerations that apply to a function within an
20248 unbraced linkage specifications do not apply to the parameters
20249 to the function. */
20250 saved_in_unbraced_linkage_specification_p
20251 = parser->in_unbraced_linkage_specification_p;
20252 parser->in_unbraced_linkage_specification_p = false;
20254 /* Look for more parameters. */
20255 while (true)
20257 cp_parameter_declarator *parameter;
20258 tree decl = error_mark_node;
20259 bool parenthesized_p = false;
20260 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20261 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20262 (current_template_parms)) : 0);
20264 /* Parse the parameter. */
20265 parameter
20266 = cp_parser_parameter_declaration (parser,
20267 /*template_parm_p=*/false,
20268 &parenthesized_p);
20270 /* We don't know yet if the enclosing context is deprecated, so wait
20271 and warn in grokparms if appropriate. */
20272 deprecated_state = DEPRECATED_SUPPRESS;
20274 if (parameter)
20276 /* If a function parameter pack was specified and an implicit template
20277 parameter was introduced during cp_parser_parameter_declaration,
20278 change any implicit parameters introduced into packs. */
20279 if (parser->implicit_template_parms
20280 && parameter->declarator
20281 && parameter->declarator->parameter_pack_p)
20283 int latest_template_parm_idx = TREE_VEC_LENGTH
20284 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20286 if (latest_template_parm_idx != template_parm_idx)
20287 parameter->decl_specifiers.type = convert_generic_types_to_packs
20288 (parameter->decl_specifiers.type,
20289 template_parm_idx, latest_template_parm_idx);
20292 decl = grokdeclarator (parameter->declarator,
20293 &parameter->decl_specifiers,
20294 PARM,
20295 parameter->default_argument != NULL_TREE,
20296 &parameter->decl_specifiers.attributes);
20299 deprecated_state = DEPRECATED_NORMAL;
20301 /* If a parse error occurred parsing the parameter declaration,
20302 then the entire parameter-declaration-list is erroneous. */
20303 if (decl == error_mark_node)
20305 *is_error = true;
20306 parameters = error_mark_node;
20307 break;
20310 if (parameter->decl_specifiers.attributes)
20311 cplus_decl_attributes (&decl,
20312 parameter->decl_specifiers.attributes,
20314 if (DECL_NAME (decl))
20315 decl = pushdecl (decl);
20317 if (decl != error_mark_node)
20319 retrofit_lang_decl (decl);
20320 DECL_PARM_INDEX (decl) = ++index;
20321 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20324 /* Add the new parameter to the list. */
20325 *tail = build_tree_list (parameter->default_argument, decl);
20326 tail = &TREE_CHAIN (*tail);
20328 /* Peek at the next token. */
20329 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20330 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20331 /* These are for Objective-C++ */
20332 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20333 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20334 /* The parameter-declaration-list is complete. */
20335 break;
20336 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20338 cp_token *token;
20340 /* Peek at the next token. */
20341 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20342 /* If it's an ellipsis, then the list is complete. */
20343 if (token->type == CPP_ELLIPSIS)
20344 break;
20345 /* Otherwise, there must be more parameters. Consume the
20346 `,'. */
20347 cp_lexer_consume_token (parser->lexer);
20348 /* When parsing something like:
20350 int i(float f, double d)
20352 we can tell after seeing the declaration for "f" that we
20353 are not looking at an initialization of a variable "i",
20354 but rather at the declaration of a function "i".
20356 Due to the fact that the parsing of template arguments
20357 (as specified to a template-id) requires backtracking we
20358 cannot use this technique when inside a template argument
20359 list. */
20360 if (!parser->in_template_argument_list_p
20361 && !parser->in_type_id_in_expr_p
20362 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20363 /* However, a parameter-declaration of the form
20364 "float(f)" (which is a valid declaration of a
20365 parameter "f") can also be interpreted as an
20366 expression (the conversion of "f" to "float"). */
20367 && !parenthesized_p)
20368 cp_parser_commit_to_tentative_parse (parser);
20370 else
20372 cp_parser_error (parser, "expected %<,%> or %<...%>");
20373 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20374 cp_parser_skip_to_closing_parenthesis (parser,
20375 /*recovering=*/true,
20376 /*or_comma=*/false,
20377 /*consume_paren=*/false);
20378 break;
20382 parser->in_unbraced_linkage_specification_p
20383 = saved_in_unbraced_linkage_specification_p;
20385 /* Reset implicit_template_scope if we are about to leave the function
20386 parameter list that introduced it. Note that for out-of-line member
20387 definitions, there will be one or more class scopes before we get to
20388 the template parameter scope. */
20390 if (cp_binding_level *its = parser->implicit_template_scope)
20391 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20393 while (maybe_its->kind == sk_class)
20394 maybe_its = maybe_its->level_chain;
20395 if (maybe_its == its)
20397 parser->implicit_template_parms = 0;
20398 parser->implicit_template_scope = 0;
20402 return parameters;
20405 /* Parse a parameter declaration.
20407 parameter-declaration:
20408 decl-specifier-seq ... [opt] declarator
20409 decl-specifier-seq declarator = assignment-expression
20410 decl-specifier-seq ... [opt] abstract-declarator [opt]
20411 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20413 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20414 declares a template parameter. (In that case, a non-nested `>'
20415 token encountered during the parsing of the assignment-expression
20416 is not interpreted as a greater-than operator.)
20418 Returns a representation of the parameter, or NULL if an error
20419 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20420 true iff the declarator is of the form "(p)". */
20422 static cp_parameter_declarator *
20423 cp_parser_parameter_declaration (cp_parser *parser,
20424 bool template_parm_p,
20425 bool *parenthesized_p)
20427 int declares_class_or_enum;
20428 cp_decl_specifier_seq decl_specifiers;
20429 cp_declarator *declarator;
20430 tree default_argument;
20431 cp_token *token = NULL, *declarator_token_start = NULL;
20432 const char *saved_message;
20433 bool template_parameter_pack_p = false;
20435 /* In a template parameter, `>' is not an operator.
20437 [temp.param]
20439 When parsing a default template-argument for a non-type
20440 template-parameter, the first non-nested `>' is taken as the end
20441 of the template parameter-list rather than a greater-than
20442 operator. */
20444 /* Type definitions may not appear in parameter types. */
20445 saved_message = parser->type_definition_forbidden_message;
20446 parser->type_definition_forbidden_message
20447 = G_("types may not be defined in parameter types");
20449 /* Parse the declaration-specifiers. */
20450 cp_parser_decl_specifier_seq (parser,
20451 CP_PARSER_FLAGS_NONE,
20452 &decl_specifiers,
20453 &declares_class_or_enum);
20455 /* Complain about missing 'typename' or other invalid type names. */
20456 if (!decl_specifiers.any_type_specifiers_p
20457 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20458 decl_specifiers.type = error_mark_node;
20460 /* If an error occurred, there's no reason to attempt to parse the
20461 rest of the declaration. */
20462 if (cp_parser_error_occurred (parser))
20464 parser->type_definition_forbidden_message = saved_message;
20465 return NULL;
20468 /* Peek at the next token. */
20469 token = cp_lexer_peek_token (parser->lexer);
20471 /* If the next token is a `)', `,', `=', `>', or `...', then there
20472 is no declarator. However, when variadic templates are enabled,
20473 there may be a declarator following `...'. */
20474 if (token->type == CPP_CLOSE_PAREN
20475 || token->type == CPP_COMMA
20476 || token->type == CPP_EQ
20477 || token->type == CPP_GREATER)
20479 declarator = NULL;
20480 if (parenthesized_p)
20481 *parenthesized_p = false;
20483 /* Otherwise, there should be a declarator. */
20484 else
20486 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20487 parser->default_arg_ok_p = false;
20489 /* After seeing a decl-specifier-seq, if the next token is not a
20490 "(", there is no possibility that the code is a valid
20491 expression. Therefore, if parsing tentatively, we commit at
20492 this point. */
20493 if (!parser->in_template_argument_list_p
20494 /* In an expression context, having seen:
20496 (int((char ...
20498 we cannot be sure whether we are looking at a
20499 function-type (taking a "char" as a parameter) or a cast
20500 of some object of type "char" to "int". */
20501 && !parser->in_type_id_in_expr_p
20502 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20503 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20504 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20505 cp_parser_commit_to_tentative_parse (parser);
20506 /* Parse the declarator. */
20507 declarator_token_start = token;
20508 declarator = cp_parser_declarator (parser,
20509 CP_PARSER_DECLARATOR_EITHER,
20510 /*ctor_dtor_or_conv_p=*/NULL,
20511 parenthesized_p,
20512 /*member_p=*/false,
20513 /*friend_p=*/false);
20514 parser->default_arg_ok_p = saved_default_arg_ok_p;
20515 /* After the declarator, allow more attributes. */
20516 decl_specifiers.attributes
20517 = chainon (decl_specifiers.attributes,
20518 cp_parser_attributes_opt (parser));
20520 /* If the declarator is a template parameter pack, remember that and
20521 clear the flag in the declarator itself so we don't get errors
20522 from grokdeclarator. */
20523 if (template_parm_p && declarator && declarator->parameter_pack_p)
20525 declarator->parameter_pack_p = false;
20526 template_parameter_pack_p = true;
20530 /* If the next token is an ellipsis, and we have not seen a declarator
20531 name, and if either the type of the declarator contains parameter
20532 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20533 for, eg, abbreviated integral type names), then we actually have a
20534 parameter pack expansion expression. Otherwise, leave the ellipsis
20535 for a C-style variadic function. */
20536 token = cp_lexer_peek_token (parser->lexer);
20537 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20539 tree type = decl_specifiers.type;
20541 if (type && DECL_P (type))
20542 type = TREE_TYPE (type);
20544 if (((type
20545 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20546 && (template_parm_p || uses_parameter_packs (type)))
20547 || (!type && template_parm_p))
20548 && declarator_can_be_parameter_pack (declarator))
20550 /* Consume the `...'. */
20551 cp_lexer_consume_token (parser->lexer);
20552 maybe_warn_variadic_templates ();
20554 /* Build a pack expansion type */
20555 if (template_parm_p)
20556 template_parameter_pack_p = true;
20557 else if (declarator)
20558 declarator->parameter_pack_p = true;
20559 else
20560 decl_specifiers.type = make_pack_expansion (type);
20564 /* The restriction on defining new types applies only to the type
20565 of the parameter, not to the default argument. */
20566 parser->type_definition_forbidden_message = saved_message;
20568 /* If the next token is `=', then process a default argument. */
20569 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20571 tree type = decl_specifiers.type;
20572 token = cp_lexer_peek_token (parser->lexer);
20573 /* If we are defining a class, then the tokens that make up the
20574 default argument must be saved and processed later. */
20575 if (!template_parm_p && at_class_scope_p ()
20576 && TYPE_BEING_DEFINED (current_class_type)
20577 && !LAMBDA_TYPE_P (current_class_type))
20578 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20580 // A constrained-type-specifier may declare a type template-parameter.
20581 else if (declares_constrained_type_template_parameter (type))
20582 default_argument
20583 = cp_parser_default_type_template_argument (parser);
20585 // A constrained-type-specifier may declare a template-template-parameter.
20586 else if (declares_constrained_template_template_parameter (type))
20587 default_argument
20588 = cp_parser_default_template_template_argument (parser);
20590 /* Outside of a class definition, we can just parse the
20591 assignment-expression. */
20592 else
20593 default_argument
20594 = cp_parser_default_argument (parser, template_parm_p);
20596 if (!parser->default_arg_ok_p)
20598 permerror (token->location,
20599 "default arguments are only "
20600 "permitted for function parameters");
20602 else if ((declarator && declarator->parameter_pack_p)
20603 || template_parameter_pack_p
20604 || (decl_specifiers.type
20605 && PACK_EXPANSION_P (decl_specifiers.type)))
20607 /* Find the name of the parameter pack. */
20608 cp_declarator *id_declarator = declarator;
20609 while (id_declarator && id_declarator->kind != cdk_id)
20610 id_declarator = id_declarator->declarator;
20612 if (id_declarator && id_declarator->kind == cdk_id)
20613 error_at (declarator_token_start->location,
20614 template_parm_p
20615 ? G_("template parameter pack %qD "
20616 "cannot have a default argument")
20617 : G_("parameter pack %qD cannot have "
20618 "a default argument"),
20619 id_declarator->u.id.unqualified_name);
20620 else
20621 error_at (declarator_token_start->location,
20622 template_parm_p
20623 ? G_("template parameter pack cannot have "
20624 "a default argument")
20625 : G_("parameter pack cannot have a "
20626 "default argument"));
20628 default_argument = NULL_TREE;
20631 else
20632 default_argument = NULL_TREE;
20634 return make_parameter_declarator (&decl_specifiers,
20635 declarator,
20636 default_argument,
20637 template_parameter_pack_p);
20640 /* Parse a default argument and return it.
20642 TEMPLATE_PARM_P is true if this is a default argument for a
20643 non-type template parameter. */
20644 static tree
20645 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20647 tree default_argument = NULL_TREE;
20648 bool saved_greater_than_is_operator_p;
20649 bool saved_local_variables_forbidden_p;
20650 bool non_constant_p, is_direct_init;
20652 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20653 set correctly. */
20654 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20655 parser->greater_than_is_operator_p = !template_parm_p;
20656 /* Local variable names (and the `this' keyword) may not
20657 appear in a default argument. */
20658 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20659 parser->local_variables_forbidden_p = true;
20660 /* Parse the assignment-expression. */
20661 if (template_parm_p)
20662 push_deferring_access_checks (dk_no_deferred);
20663 tree saved_class_ptr = NULL_TREE;
20664 tree saved_class_ref = NULL_TREE;
20665 /* The "this" pointer is not valid in a default argument. */
20666 if (cfun)
20668 saved_class_ptr = current_class_ptr;
20669 cp_function_chain->x_current_class_ptr = NULL_TREE;
20670 saved_class_ref = current_class_ref;
20671 cp_function_chain->x_current_class_ref = NULL_TREE;
20673 default_argument
20674 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20675 /* Restore the "this" pointer. */
20676 if (cfun)
20678 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20679 cp_function_chain->x_current_class_ref = saved_class_ref;
20681 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20682 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20683 if (template_parm_p)
20684 pop_deferring_access_checks ();
20685 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20686 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20688 return default_argument;
20691 /* Parse a function-body.
20693 function-body:
20694 compound_statement */
20696 static void
20697 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20699 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20700 ? BCS_TRY_BLOCK : BCS_NORMAL),
20701 true);
20704 /* Parse a ctor-initializer-opt followed by a function-body. Return
20705 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20706 is true we are parsing a function-try-block. */
20708 static bool
20709 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20710 bool in_function_try_block)
20712 tree body, list;
20713 bool ctor_initializer_p;
20714 const bool check_body_p =
20715 DECL_CONSTRUCTOR_P (current_function_decl)
20716 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20717 tree last = NULL;
20719 /* Begin the function body. */
20720 body = begin_function_body ();
20721 /* Parse the optional ctor-initializer. */
20722 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20724 /* If we're parsing a constexpr constructor definition, we need
20725 to check that the constructor body is indeed empty. However,
20726 before we get to cp_parser_function_body lot of junk has been
20727 generated, so we can't just check that we have an empty block.
20728 Rather we take a snapshot of the outermost block, and check whether
20729 cp_parser_function_body changed its state. */
20730 if (check_body_p)
20732 list = cur_stmt_list;
20733 if (STATEMENT_LIST_TAIL (list))
20734 last = STATEMENT_LIST_TAIL (list)->stmt;
20736 /* Parse the function-body. */
20737 cp_parser_function_body (parser, in_function_try_block);
20738 if (check_body_p)
20739 check_constexpr_ctor_body (last, list, /*complain=*/true);
20740 /* Finish the function body. */
20741 finish_function_body (body);
20743 return ctor_initializer_p;
20746 /* Parse an initializer.
20748 initializer:
20749 = initializer-clause
20750 ( expression-list )
20752 Returns an expression representing the initializer. If no
20753 initializer is present, NULL_TREE is returned.
20755 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20756 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20757 set to TRUE if there is no initializer present. If there is an
20758 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20759 is set to true; otherwise it is set to false. */
20761 static tree
20762 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20763 bool* non_constant_p)
20765 cp_token *token;
20766 tree init;
20768 /* Peek at the next token. */
20769 token = cp_lexer_peek_token (parser->lexer);
20771 /* Let our caller know whether or not this initializer was
20772 parenthesized. */
20773 *is_direct_init = (token->type != CPP_EQ);
20774 /* Assume that the initializer is constant. */
20775 *non_constant_p = false;
20777 if (token->type == CPP_EQ)
20779 /* Consume the `='. */
20780 cp_lexer_consume_token (parser->lexer);
20781 /* Parse the initializer-clause. */
20782 init = cp_parser_initializer_clause (parser, non_constant_p);
20784 else if (token->type == CPP_OPEN_PAREN)
20786 vec<tree, va_gc> *vec;
20787 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20788 /*cast_p=*/false,
20789 /*allow_expansion_p=*/true,
20790 non_constant_p);
20791 if (vec == NULL)
20792 return error_mark_node;
20793 init = build_tree_list_vec (vec);
20794 release_tree_vector (vec);
20796 else if (token->type == CPP_OPEN_BRACE)
20798 cp_lexer_set_source_position (parser->lexer);
20799 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20800 init = cp_parser_braced_list (parser, non_constant_p);
20801 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20803 else
20805 /* Anything else is an error. */
20806 cp_parser_error (parser, "expected initializer");
20807 init = error_mark_node;
20810 if (check_for_bare_parameter_packs (init))
20811 init = error_mark_node;
20813 return init;
20816 /* Parse an initializer-clause.
20818 initializer-clause:
20819 assignment-expression
20820 braced-init-list
20822 Returns an expression representing the initializer.
20824 If the `assignment-expression' production is used the value
20825 returned is simply a representation for the expression.
20827 Otherwise, calls cp_parser_braced_list. */
20829 static cp_expr
20830 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20832 cp_expr initializer;
20834 /* Assume the expression is constant. */
20835 *non_constant_p = false;
20837 /* If it is not a `{', then we are looking at an
20838 assignment-expression. */
20839 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20841 initializer
20842 = cp_parser_constant_expression (parser,
20843 /*allow_non_constant_p=*/true,
20844 non_constant_p);
20846 else
20847 initializer = cp_parser_braced_list (parser, non_constant_p);
20849 return initializer;
20852 /* Parse a brace-enclosed initializer list.
20854 braced-init-list:
20855 { initializer-list , [opt] }
20858 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20859 the elements of the initializer-list (or NULL, if the last
20860 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20861 NULL_TREE. There is no way to detect whether or not the optional
20862 trailing `,' was provided. NON_CONSTANT_P is as for
20863 cp_parser_initializer. */
20865 static cp_expr
20866 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20868 tree initializer;
20869 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20871 /* Consume the `{' token. */
20872 cp_lexer_consume_token (parser->lexer);
20873 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20874 initializer = make_node (CONSTRUCTOR);
20875 /* If it's not a `}', then there is a non-trivial initializer. */
20876 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20878 /* Parse the initializer list. */
20879 CONSTRUCTOR_ELTS (initializer)
20880 = cp_parser_initializer_list (parser, non_constant_p);
20881 /* A trailing `,' token is allowed. */
20882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20883 cp_lexer_consume_token (parser->lexer);
20885 else
20886 *non_constant_p = false;
20887 /* Now, there should be a trailing `}'. */
20888 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20889 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20890 TREE_TYPE (initializer) = init_list_type_node;
20892 cp_expr result (initializer);
20893 /* Build a location of the form:
20894 { ... }
20895 ^~~~~~~
20896 with caret==start at the open brace, finish at the close brace. */
20897 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20898 result.set_location (combined_loc);
20899 return result;
20902 /* Consume tokens up to, and including, the next non-nested closing `]'.
20903 Returns true iff we found a closing `]'. */
20905 static bool
20906 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20908 unsigned square_depth = 0;
20910 while (true)
20912 cp_token * token = cp_lexer_peek_token (parser->lexer);
20914 switch (token->type)
20916 case CPP_EOF:
20917 case CPP_PRAGMA_EOL:
20918 /* If we've run out of tokens, then there is no closing `]'. */
20919 return false;
20921 case CPP_OPEN_SQUARE:
20922 ++square_depth;
20923 break;
20925 case CPP_CLOSE_SQUARE:
20926 if (!square_depth--)
20928 cp_lexer_consume_token (parser->lexer);
20929 return true;
20931 break;
20933 default:
20934 break;
20937 /* Consume the token. */
20938 cp_lexer_consume_token (parser->lexer);
20942 /* Return true if we are looking at an array-designator, false otherwise. */
20944 static bool
20945 cp_parser_array_designator_p (cp_parser *parser)
20947 /* Consume the `['. */
20948 cp_lexer_consume_token (parser->lexer);
20950 cp_lexer_save_tokens (parser->lexer);
20952 /* Skip tokens until the next token is a closing square bracket.
20953 If we find the closing `]', and the next token is a `=', then
20954 we are looking at an array designator. */
20955 bool array_designator_p
20956 = (cp_parser_skip_to_closing_square_bracket (parser)
20957 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20959 /* Roll back the tokens we skipped. */
20960 cp_lexer_rollback_tokens (parser->lexer);
20962 return array_designator_p;
20965 /* Parse an initializer-list.
20967 initializer-list:
20968 initializer-clause ... [opt]
20969 initializer-list , initializer-clause ... [opt]
20971 GNU Extension:
20973 initializer-list:
20974 designation initializer-clause ...[opt]
20975 initializer-list , designation initializer-clause ...[opt]
20977 designation:
20978 . identifier =
20979 identifier :
20980 [ constant-expression ] =
20982 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20983 for the initializer. If the INDEX of the elt is non-NULL, it is the
20984 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20985 as for cp_parser_initializer. */
20987 static vec<constructor_elt, va_gc> *
20988 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20990 vec<constructor_elt, va_gc> *v = NULL;
20992 /* Assume all of the expressions are constant. */
20993 *non_constant_p = false;
20995 /* Parse the rest of the list. */
20996 while (true)
20998 cp_token *token;
20999 tree designator;
21000 tree initializer;
21001 bool clause_non_constant_p;
21003 /* If the next token is an identifier and the following one is a
21004 colon, we are looking at the GNU designated-initializer
21005 syntax. */
21006 if (cp_parser_allow_gnu_extensions_p (parser)
21007 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21008 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21010 /* Warn the user that they are using an extension. */
21011 pedwarn (input_location, OPT_Wpedantic,
21012 "ISO C++ does not allow designated initializers");
21013 /* Consume the identifier. */
21014 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21015 /* Consume the `:'. */
21016 cp_lexer_consume_token (parser->lexer);
21018 /* Also handle the C99 syntax, '. id ='. */
21019 else if (cp_parser_allow_gnu_extensions_p (parser)
21020 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21021 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21022 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21024 /* Warn the user that they are using an extension. */
21025 pedwarn (input_location, OPT_Wpedantic,
21026 "ISO C++ does not allow C99 designated initializers");
21027 /* Consume the `.'. */
21028 cp_lexer_consume_token (parser->lexer);
21029 /* Consume the identifier. */
21030 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21031 /* Consume the `='. */
21032 cp_lexer_consume_token (parser->lexer);
21034 /* Also handle C99 array designators, '[ const ] ='. */
21035 else if (cp_parser_allow_gnu_extensions_p (parser)
21036 && !c_dialect_objc ()
21037 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21039 /* In C++11, [ could start a lambda-introducer. */
21040 bool non_const = false;
21042 cp_parser_parse_tentatively (parser);
21044 if (!cp_parser_array_designator_p (parser))
21046 cp_parser_simulate_error (parser);
21047 designator = NULL_TREE;
21049 else
21051 designator = cp_parser_constant_expression (parser, true,
21052 &non_const);
21053 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21054 cp_parser_require (parser, CPP_EQ, RT_EQ);
21057 if (!cp_parser_parse_definitely (parser))
21058 designator = NULL_TREE;
21059 else if (non_const)
21060 require_potential_rvalue_constant_expression (designator);
21062 else
21063 designator = NULL_TREE;
21065 /* Parse the initializer. */
21066 initializer = cp_parser_initializer_clause (parser,
21067 &clause_non_constant_p);
21068 /* If any clause is non-constant, so is the entire initializer. */
21069 if (clause_non_constant_p)
21070 *non_constant_p = true;
21072 /* If we have an ellipsis, this is an initializer pack
21073 expansion. */
21074 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21076 /* Consume the `...'. */
21077 cp_lexer_consume_token (parser->lexer);
21079 /* Turn the initializer into an initializer expansion. */
21080 initializer = make_pack_expansion (initializer);
21083 /* Add it to the vector. */
21084 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21086 /* If the next token is not a comma, we have reached the end of
21087 the list. */
21088 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21089 break;
21091 /* Peek at the next token. */
21092 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21093 /* If the next token is a `}', then we're still done. An
21094 initializer-clause can have a trailing `,' after the
21095 initializer-list and before the closing `}'. */
21096 if (token->type == CPP_CLOSE_BRACE)
21097 break;
21099 /* Consume the `,' token. */
21100 cp_lexer_consume_token (parser->lexer);
21103 return v;
21106 /* Classes [gram.class] */
21108 /* Parse a class-name.
21110 class-name:
21111 identifier
21112 template-id
21114 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21115 to indicate that names looked up in dependent types should be
21116 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21117 keyword has been used to indicate that the name that appears next
21118 is a template. TAG_TYPE indicates the explicit tag given before
21119 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21120 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21121 is the class being defined in a class-head. If ENUM_OK is TRUE,
21122 enum-names are also accepted.
21124 Returns the TYPE_DECL representing the class. */
21126 static tree
21127 cp_parser_class_name (cp_parser *parser,
21128 bool typename_keyword_p,
21129 bool template_keyword_p,
21130 enum tag_types tag_type,
21131 bool check_dependency_p,
21132 bool class_head_p,
21133 bool is_declaration,
21134 bool enum_ok)
21136 tree decl;
21137 tree scope;
21138 bool typename_p;
21139 cp_token *token;
21140 tree identifier = NULL_TREE;
21142 /* All class-names start with an identifier. */
21143 token = cp_lexer_peek_token (parser->lexer);
21144 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21146 cp_parser_error (parser, "expected class-name");
21147 return error_mark_node;
21150 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21151 to a template-id, so we save it here. */
21152 scope = parser->scope;
21153 if (scope == error_mark_node)
21154 return error_mark_node;
21156 /* Any name names a type if we're following the `typename' keyword
21157 in a qualified name where the enclosing scope is type-dependent. */
21158 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21159 && dependent_type_p (scope));
21160 /* Handle the common case (an identifier, but not a template-id)
21161 efficiently. */
21162 if (token->type == CPP_NAME
21163 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21165 cp_token *identifier_token;
21166 bool ambiguous_p;
21168 /* Look for the identifier. */
21169 identifier_token = cp_lexer_peek_token (parser->lexer);
21170 ambiguous_p = identifier_token->error_reported;
21171 identifier = cp_parser_identifier (parser);
21172 /* If the next token isn't an identifier, we are certainly not
21173 looking at a class-name. */
21174 if (identifier == error_mark_node)
21175 decl = error_mark_node;
21176 /* If we know this is a type-name, there's no need to look it
21177 up. */
21178 else if (typename_p)
21179 decl = identifier;
21180 else
21182 tree ambiguous_decls;
21183 /* If we already know that this lookup is ambiguous, then
21184 we've already issued an error message; there's no reason
21185 to check again. */
21186 if (ambiguous_p)
21188 cp_parser_simulate_error (parser);
21189 return error_mark_node;
21191 /* If the next token is a `::', then the name must be a type
21192 name.
21194 [basic.lookup.qual]
21196 During the lookup for a name preceding the :: scope
21197 resolution operator, object, function, and enumerator
21198 names are ignored. */
21199 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21200 tag_type = scope_type;
21201 /* Look up the name. */
21202 decl = cp_parser_lookup_name (parser, identifier,
21203 tag_type,
21204 /*is_template=*/false,
21205 /*is_namespace=*/false,
21206 check_dependency_p,
21207 &ambiguous_decls,
21208 identifier_token->location);
21209 if (ambiguous_decls)
21211 if (cp_parser_parsing_tentatively (parser))
21212 cp_parser_simulate_error (parser);
21213 return error_mark_node;
21217 else
21219 /* Try a template-id. */
21220 decl = cp_parser_template_id (parser, template_keyword_p,
21221 check_dependency_p,
21222 tag_type,
21223 is_declaration);
21224 if (decl == error_mark_node)
21225 return error_mark_node;
21228 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21230 /* If this is a typename, create a TYPENAME_TYPE. */
21231 if (typename_p && decl != error_mark_node)
21233 decl = make_typename_type (scope, decl, typename_type,
21234 /*complain=*/tf_error);
21235 if (decl != error_mark_node)
21236 decl = TYPE_NAME (decl);
21239 decl = strip_using_decl (decl);
21241 /* Check to see that it is really the name of a class. */
21242 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21243 && identifier_p (TREE_OPERAND (decl, 0))
21244 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21245 /* Situations like this:
21247 template <typename T> struct A {
21248 typename T::template X<int>::I i;
21251 are problematic. Is `T::template X<int>' a class-name? The
21252 standard does not seem to be definitive, but there is no other
21253 valid interpretation of the following `::'. Therefore, those
21254 names are considered class-names. */
21256 decl = make_typename_type (scope, decl, tag_type, tf_error);
21257 if (decl != error_mark_node)
21258 decl = TYPE_NAME (decl);
21260 else if (TREE_CODE (decl) != TYPE_DECL
21261 || TREE_TYPE (decl) == error_mark_node
21262 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21263 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21264 /* In Objective-C 2.0, a classname followed by '.' starts a
21265 dot-syntax expression, and it's not a type-name. */
21266 || (c_dialect_objc ()
21267 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21268 && objc_is_class_name (decl)))
21269 decl = error_mark_node;
21271 if (decl == error_mark_node)
21272 cp_parser_error (parser, "expected class-name");
21273 else if (identifier && !parser->scope)
21274 maybe_note_name_used_in_class (identifier, decl);
21276 return decl;
21279 /* Parse a class-specifier.
21281 class-specifier:
21282 class-head { member-specification [opt] }
21284 Returns the TREE_TYPE representing the class. */
21286 static tree
21287 cp_parser_class_specifier_1 (cp_parser* parser)
21289 tree type;
21290 tree attributes = NULL_TREE;
21291 bool nested_name_specifier_p;
21292 unsigned saved_num_template_parameter_lists;
21293 bool saved_in_function_body;
21294 unsigned char in_statement;
21295 bool in_switch_statement_p;
21296 bool saved_in_unbraced_linkage_specification_p;
21297 tree old_scope = NULL_TREE;
21298 tree scope = NULL_TREE;
21299 cp_token *closing_brace;
21301 push_deferring_access_checks (dk_no_deferred);
21303 /* Parse the class-head. */
21304 type = cp_parser_class_head (parser,
21305 &nested_name_specifier_p);
21306 /* If the class-head was a semantic disaster, skip the entire body
21307 of the class. */
21308 if (!type)
21310 cp_parser_skip_to_end_of_block_or_statement (parser);
21311 pop_deferring_access_checks ();
21312 return error_mark_node;
21315 /* Look for the `{'. */
21316 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21318 pop_deferring_access_checks ();
21319 return error_mark_node;
21322 cp_ensure_no_omp_declare_simd (parser);
21323 cp_ensure_no_oacc_routine (parser);
21325 /* Issue an error message if type-definitions are forbidden here. */
21326 cp_parser_check_type_definition (parser);
21327 /* Remember that we are defining one more class. */
21328 ++parser->num_classes_being_defined;
21329 /* Inside the class, surrounding template-parameter-lists do not
21330 apply. */
21331 saved_num_template_parameter_lists
21332 = parser->num_template_parameter_lists;
21333 parser->num_template_parameter_lists = 0;
21334 /* We are not in a function body. */
21335 saved_in_function_body = parser->in_function_body;
21336 parser->in_function_body = false;
21337 /* Or in a loop. */
21338 in_statement = parser->in_statement;
21339 parser->in_statement = 0;
21340 /* Or in a switch. */
21341 in_switch_statement_p = parser->in_switch_statement_p;
21342 parser->in_switch_statement_p = false;
21343 /* We are not immediately inside an extern "lang" block. */
21344 saved_in_unbraced_linkage_specification_p
21345 = parser->in_unbraced_linkage_specification_p;
21346 parser->in_unbraced_linkage_specification_p = false;
21348 // Associate constraints with the type.
21349 if (flag_concepts)
21350 type = associate_classtype_constraints (type);
21352 /* Start the class. */
21353 if (nested_name_specifier_p)
21355 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21356 old_scope = push_inner_scope (scope);
21358 type = begin_class_definition (type);
21360 if (type == error_mark_node)
21361 /* If the type is erroneous, skip the entire body of the class. */
21362 cp_parser_skip_to_closing_brace (parser);
21363 else
21364 /* Parse the member-specification. */
21365 cp_parser_member_specification_opt (parser);
21367 /* Look for the trailing `}'. */
21368 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21369 /* Look for trailing attributes to apply to this class. */
21370 if (cp_parser_allow_gnu_extensions_p (parser))
21371 attributes = cp_parser_gnu_attributes_opt (parser);
21372 if (type != error_mark_node)
21373 type = finish_struct (type, attributes);
21374 if (nested_name_specifier_p)
21375 pop_inner_scope (old_scope, scope);
21377 /* We've finished a type definition. Check for the common syntax
21378 error of forgetting a semicolon after the definition. We need to
21379 be careful, as we can't just check for not-a-semicolon and be done
21380 with it; the user might have typed:
21382 class X { } c = ...;
21383 class X { } *p = ...;
21385 and so forth. Instead, enumerate all the possible tokens that
21386 might follow this production; if we don't see one of them, then
21387 complain and silently insert the semicolon. */
21389 cp_token *token = cp_lexer_peek_token (parser->lexer);
21390 bool want_semicolon = true;
21392 if (cp_next_tokens_can_be_std_attribute_p (parser))
21393 /* Don't try to parse c++11 attributes here. As per the
21394 grammar, that should be a task for
21395 cp_parser_decl_specifier_seq. */
21396 want_semicolon = false;
21398 switch (token->type)
21400 case CPP_NAME:
21401 case CPP_SEMICOLON:
21402 case CPP_MULT:
21403 case CPP_AND:
21404 case CPP_OPEN_PAREN:
21405 case CPP_CLOSE_PAREN:
21406 case CPP_COMMA:
21407 want_semicolon = false;
21408 break;
21410 /* While it's legal for type qualifiers and storage class
21411 specifiers to follow type definitions in the grammar, only
21412 compiler testsuites contain code like that. Assume that if
21413 we see such code, then what we're really seeing is a case
21414 like:
21416 class X { }
21417 const <type> var = ...;
21421 class Y { }
21422 static <type> func (...) ...
21424 i.e. the qualifier or specifier applies to the next
21425 declaration. To do so, however, we need to look ahead one
21426 more token to see if *that* token is a type specifier.
21428 This code could be improved to handle:
21430 class Z { }
21431 static const <type> var = ...; */
21432 case CPP_KEYWORD:
21433 if (keyword_is_decl_specifier (token->keyword))
21435 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21437 /* Handling user-defined types here would be nice, but very
21438 tricky. */
21439 want_semicolon
21440 = (lookahead->type == CPP_KEYWORD
21441 && keyword_begins_type_specifier (lookahead->keyword));
21443 break;
21444 default:
21445 break;
21448 /* If we don't have a type, then something is very wrong and we
21449 shouldn't try to do anything clever. Likewise for not seeing the
21450 closing brace. */
21451 if (closing_brace && TYPE_P (type) && want_semicolon)
21453 /* Locate the closing brace. */
21454 cp_token_position prev
21455 = cp_lexer_previous_token_position (parser->lexer);
21456 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21457 location_t loc = prev_token->location;
21459 /* We want to suggest insertion of a ';' immediately *after* the
21460 closing brace, so, if we can, offset the location by 1 column. */
21461 location_t next_loc = loc;
21462 if (!linemap_location_from_macro_expansion_p (line_table, loc))
21463 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
21465 rich_location richloc (line_table, next_loc);
21466 richloc.add_fixit_insert (next_loc, ";");
21468 if (CLASSTYPE_DECLARED_CLASS (type))
21469 error_at_rich_loc (&richloc,
21470 "expected %<;%> after class definition");
21471 else if (TREE_CODE (type) == RECORD_TYPE)
21472 error_at_rich_loc (&richloc,
21473 "expected %<;%> after struct definition");
21474 else if (TREE_CODE (type) == UNION_TYPE)
21475 error_at_rich_loc (&richloc,
21476 "expected %<;%> after union definition");
21477 else
21478 gcc_unreachable ();
21480 /* Unget one token and smash it to look as though we encountered
21481 a semicolon in the input stream. */
21482 cp_lexer_set_token_position (parser->lexer, prev);
21483 token = cp_lexer_peek_token (parser->lexer);
21484 token->type = CPP_SEMICOLON;
21485 token->keyword = RID_MAX;
21489 /* If this class is not itself within the scope of another class,
21490 then we need to parse the bodies of all of the queued function
21491 definitions. Note that the queued functions defined in a class
21492 are not always processed immediately following the
21493 class-specifier for that class. Consider:
21495 struct A {
21496 struct B { void f() { sizeof (A); } };
21499 If `f' were processed before the processing of `A' were
21500 completed, there would be no way to compute the size of `A'.
21501 Note that the nesting we are interested in here is lexical --
21502 not the semantic nesting given by TYPE_CONTEXT. In particular,
21503 for:
21505 struct A { struct B; };
21506 struct A::B { void f() { } };
21508 there is no need to delay the parsing of `A::B::f'. */
21509 if (--parser->num_classes_being_defined == 0)
21511 tree decl;
21512 tree class_type = NULL_TREE;
21513 tree pushed_scope = NULL_TREE;
21514 unsigned ix;
21515 cp_default_arg_entry *e;
21516 tree save_ccp, save_ccr;
21518 /* In a first pass, parse default arguments to the functions.
21519 Then, in a second pass, parse the bodies of the functions.
21520 This two-phased approach handles cases like:
21522 struct S {
21523 void f() { g(); }
21524 void g(int i = 3);
21528 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21530 decl = e->decl;
21531 /* If there are default arguments that have not yet been processed,
21532 take care of them now. */
21533 if (class_type != e->class_type)
21535 if (pushed_scope)
21536 pop_scope (pushed_scope);
21537 class_type = e->class_type;
21538 pushed_scope = push_scope (class_type);
21540 /* Make sure that any template parameters are in scope. */
21541 maybe_begin_member_template_processing (decl);
21542 /* Parse the default argument expressions. */
21543 cp_parser_late_parsing_default_args (parser, decl);
21544 /* Remove any template parameters from the symbol table. */
21545 maybe_end_member_template_processing ();
21547 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21548 /* Now parse any NSDMIs. */
21549 save_ccp = current_class_ptr;
21550 save_ccr = current_class_ref;
21551 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21553 if (class_type != DECL_CONTEXT (decl))
21555 if (pushed_scope)
21556 pop_scope (pushed_scope);
21557 class_type = DECL_CONTEXT (decl);
21558 pushed_scope = push_scope (class_type);
21560 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21561 cp_parser_late_parsing_nsdmi (parser, decl);
21563 vec_safe_truncate (unparsed_nsdmis, 0);
21564 current_class_ptr = save_ccp;
21565 current_class_ref = save_ccr;
21566 if (pushed_scope)
21567 pop_scope (pushed_scope);
21569 /* Now do some post-NSDMI bookkeeping. */
21570 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21571 after_nsdmi_defaulted_late_checks (class_type);
21572 vec_safe_truncate (unparsed_classes, 0);
21573 after_nsdmi_defaulted_late_checks (type);
21575 /* Now parse the body of the functions. */
21576 if (flag_openmp)
21578 /* OpenMP UDRs need to be parsed before all other functions. */
21579 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21580 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21581 cp_parser_late_parsing_for_member (parser, decl);
21582 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21583 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21584 cp_parser_late_parsing_for_member (parser, decl);
21586 else
21587 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21588 cp_parser_late_parsing_for_member (parser, decl);
21589 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21591 else
21592 vec_safe_push (unparsed_classes, type);
21594 /* Put back any saved access checks. */
21595 pop_deferring_access_checks ();
21597 /* Restore saved state. */
21598 parser->in_switch_statement_p = in_switch_statement_p;
21599 parser->in_statement = in_statement;
21600 parser->in_function_body = saved_in_function_body;
21601 parser->num_template_parameter_lists
21602 = saved_num_template_parameter_lists;
21603 parser->in_unbraced_linkage_specification_p
21604 = saved_in_unbraced_linkage_specification_p;
21606 return type;
21609 static tree
21610 cp_parser_class_specifier (cp_parser* parser)
21612 tree ret;
21613 timevar_push (TV_PARSE_STRUCT);
21614 ret = cp_parser_class_specifier_1 (parser);
21615 timevar_pop (TV_PARSE_STRUCT);
21616 return ret;
21619 /* Parse a class-head.
21621 class-head:
21622 class-key identifier [opt] base-clause [opt]
21623 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21624 class-key nested-name-specifier [opt] template-id
21625 base-clause [opt]
21627 class-virt-specifier:
21628 final
21630 GNU Extensions:
21631 class-key attributes identifier [opt] base-clause [opt]
21632 class-key attributes nested-name-specifier identifier base-clause [opt]
21633 class-key attributes nested-name-specifier [opt] template-id
21634 base-clause [opt]
21636 Upon return BASES is initialized to the list of base classes (or
21637 NULL, if there are none) in the same form returned by
21638 cp_parser_base_clause.
21640 Returns the TYPE of the indicated class. Sets
21641 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21642 involving a nested-name-specifier was used, and FALSE otherwise.
21644 Returns error_mark_node if this is not a class-head.
21646 Returns NULL_TREE if the class-head is syntactically valid, but
21647 semantically invalid in a way that means we should skip the entire
21648 body of the class. */
21650 static tree
21651 cp_parser_class_head (cp_parser* parser,
21652 bool* nested_name_specifier_p)
21654 tree nested_name_specifier;
21655 enum tag_types class_key;
21656 tree id = NULL_TREE;
21657 tree type = NULL_TREE;
21658 tree attributes;
21659 tree bases;
21660 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21661 bool template_id_p = false;
21662 bool qualified_p = false;
21663 bool invalid_nested_name_p = false;
21664 bool invalid_explicit_specialization_p = false;
21665 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21666 tree pushed_scope = NULL_TREE;
21667 unsigned num_templates;
21668 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21669 /* Assume no nested-name-specifier will be present. */
21670 *nested_name_specifier_p = false;
21671 /* Assume no template parameter lists will be used in defining the
21672 type. */
21673 num_templates = 0;
21674 parser->colon_corrects_to_scope_p = false;
21676 /* Look for the class-key. */
21677 class_key = cp_parser_class_key (parser);
21678 if (class_key == none_type)
21679 return error_mark_node;
21681 location_t class_head_start_location = input_location;
21683 /* Parse the attributes. */
21684 attributes = cp_parser_attributes_opt (parser);
21686 /* If the next token is `::', that is invalid -- but sometimes
21687 people do try to write:
21689 struct ::S {};
21691 Handle this gracefully by accepting the extra qualifier, and then
21692 issuing an error about it later if this really is a
21693 class-head. If it turns out just to be an elaborated type
21694 specifier, remain silent. */
21695 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21696 qualified_p = true;
21698 push_deferring_access_checks (dk_no_check);
21700 /* Determine the name of the class. Begin by looking for an
21701 optional nested-name-specifier. */
21702 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21703 nested_name_specifier
21704 = cp_parser_nested_name_specifier_opt (parser,
21705 /*typename_keyword_p=*/false,
21706 /*check_dependency_p=*/false,
21707 /*type_p=*/true,
21708 /*is_declaration=*/false);
21709 /* If there was a nested-name-specifier, then there *must* be an
21710 identifier. */
21711 if (nested_name_specifier)
21713 type_start_token = cp_lexer_peek_token (parser->lexer);
21714 /* Although the grammar says `identifier', it really means
21715 `class-name' or `template-name'. You are only allowed to
21716 define a class that has already been declared with this
21717 syntax.
21719 The proposed resolution for Core Issue 180 says that wherever
21720 you see `class T::X' you should treat `X' as a type-name.
21722 It is OK to define an inaccessible class; for example:
21724 class A { class B; };
21725 class A::B {};
21727 We do not know if we will see a class-name, or a
21728 template-name. We look for a class-name first, in case the
21729 class-name is a template-id; if we looked for the
21730 template-name first we would stop after the template-name. */
21731 cp_parser_parse_tentatively (parser);
21732 type = cp_parser_class_name (parser,
21733 /*typename_keyword_p=*/false,
21734 /*template_keyword_p=*/false,
21735 class_type,
21736 /*check_dependency_p=*/false,
21737 /*class_head_p=*/true,
21738 /*is_declaration=*/false);
21739 /* If that didn't work, ignore the nested-name-specifier. */
21740 if (!cp_parser_parse_definitely (parser))
21742 invalid_nested_name_p = true;
21743 type_start_token = cp_lexer_peek_token (parser->lexer);
21744 id = cp_parser_identifier (parser);
21745 if (id == error_mark_node)
21746 id = NULL_TREE;
21748 /* If we could not find a corresponding TYPE, treat this
21749 declaration like an unqualified declaration. */
21750 if (type == error_mark_node)
21751 nested_name_specifier = NULL_TREE;
21752 /* Otherwise, count the number of templates used in TYPE and its
21753 containing scopes. */
21754 else
21756 tree scope;
21758 for (scope = TREE_TYPE (type);
21759 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21760 scope = get_containing_scope (scope))
21761 if (TYPE_P (scope)
21762 && CLASS_TYPE_P (scope)
21763 && CLASSTYPE_TEMPLATE_INFO (scope)
21764 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21765 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21766 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21767 ++num_templates;
21770 /* Otherwise, the identifier is optional. */
21771 else
21773 /* We don't know whether what comes next is a template-id,
21774 an identifier, or nothing at all. */
21775 cp_parser_parse_tentatively (parser);
21776 /* Check for a template-id. */
21777 type_start_token = cp_lexer_peek_token (parser->lexer);
21778 id = cp_parser_template_id (parser,
21779 /*template_keyword_p=*/false,
21780 /*check_dependency_p=*/true,
21781 class_key,
21782 /*is_declaration=*/true);
21783 /* If that didn't work, it could still be an identifier. */
21784 if (!cp_parser_parse_definitely (parser))
21786 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21788 type_start_token = cp_lexer_peek_token (parser->lexer);
21789 id = cp_parser_identifier (parser);
21791 else
21792 id = NULL_TREE;
21794 else
21796 template_id_p = true;
21797 ++num_templates;
21801 pop_deferring_access_checks ();
21803 if (id)
21805 cp_parser_check_for_invalid_template_id (parser, id,
21806 class_key,
21807 type_start_token->location);
21809 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21811 /* If it's not a `:' or a `{' then we can't really be looking at a
21812 class-head, since a class-head only appears as part of a
21813 class-specifier. We have to detect this situation before calling
21814 xref_tag, since that has irreversible side-effects. */
21815 if (!cp_parser_next_token_starts_class_definition_p (parser))
21817 cp_parser_error (parser, "expected %<{%> or %<:%>");
21818 type = error_mark_node;
21819 goto out;
21822 /* At this point, we're going ahead with the class-specifier, even
21823 if some other problem occurs. */
21824 cp_parser_commit_to_tentative_parse (parser);
21825 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21827 cp_parser_error (parser,
21828 "cannot specify %<override%> for a class");
21829 type = error_mark_node;
21830 goto out;
21832 /* Issue the error about the overly-qualified name now. */
21833 if (qualified_p)
21835 cp_parser_error (parser,
21836 "global qualification of class name is invalid");
21837 type = error_mark_node;
21838 goto out;
21840 else if (invalid_nested_name_p)
21842 cp_parser_error (parser,
21843 "qualified name does not name a class");
21844 type = error_mark_node;
21845 goto out;
21847 else if (nested_name_specifier)
21849 tree scope;
21851 /* Reject typedef-names in class heads. */
21852 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21854 error_at (type_start_token->location,
21855 "invalid class name in declaration of %qD",
21856 type);
21857 type = NULL_TREE;
21858 goto done;
21861 /* Figure out in what scope the declaration is being placed. */
21862 scope = current_scope ();
21863 /* If that scope does not contain the scope in which the
21864 class was originally declared, the program is invalid. */
21865 if (scope && !is_ancestor (scope, nested_name_specifier))
21867 if (at_namespace_scope_p ())
21868 error_at (type_start_token->location,
21869 "declaration of %qD in namespace %qD which does not "
21870 "enclose %qD",
21871 type, scope, nested_name_specifier);
21872 else
21873 error_at (type_start_token->location,
21874 "declaration of %qD in %qD which does not enclose %qD",
21875 type, scope, nested_name_specifier);
21876 type = NULL_TREE;
21877 goto done;
21879 /* [dcl.meaning]
21881 A declarator-id shall not be qualified except for the
21882 definition of a ... nested class outside of its class
21883 ... [or] the definition or explicit instantiation of a
21884 class member of a namespace outside of its namespace. */
21885 if (scope == nested_name_specifier)
21887 permerror (nested_name_specifier_token_start->location,
21888 "extra qualification not allowed");
21889 nested_name_specifier = NULL_TREE;
21890 num_templates = 0;
21893 /* An explicit-specialization must be preceded by "template <>". If
21894 it is not, try to recover gracefully. */
21895 if (at_namespace_scope_p ()
21896 && parser->num_template_parameter_lists == 0
21897 && template_id_p)
21899 /* Build a location of this form:
21900 struct typename <ARGS>
21901 ^~~~~~~~~~~~~~~~~~~~~~
21902 with caret==start at the start token, and
21903 finishing at the end of the type. */
21904 location_t reported_loc
21905 = make_location (class_head_start_location,
21906 class_head_start_location,
21907 get_finish (type_start_token->location));
21908 rich_location richloc (line_table, reported_loc);
21909 richloc.add_fixit_insert (class_head_start_location, "template <> ");
21910 error_at_rich_loc
21911 (&richloc,
21912 "an explicit specialization must be preceded by %<template <>%>");
21913 invalid_explicit_specialization_p = true;
21914 /* Take the same action that would have been taken by
21915 cp_parser_explicit_specialization. */
21916 ++parser->num_template_parameter_lists;
21917 begin_specialization ();
21919 /* There must be no "return" statements between this point and the
21920 end of this function; set "type "to the correct return value and
21921 use "goto done;" to return. */
21922 /* Make sure that the right number of template parameters were
21923 present. */
21924 if (!cp_parser_check_template_parameters (parser, num_templates,
21925 type_start_token->location,
21926 /*declarator=*/NULL))
21928 /* If something went wrong, there is no point in even trying to
21929 process the class-definition. */
21930 type = NULL_TREE;
21931 goto done;
21934 /* Look up the type. */
21935 if (template_id_p)
21937 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21938 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21939 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21941 error_at (type_start_token->location,
21942 "function template %qD redeclared as a class template", id);
21943 type = error_mark_node;
21945 else
21947 type = TREE_TYPE (id);
21948 type = maybe_process_partial_specialization (type);
21950 if (nested_name_specifier)
21951 pushed_scope = push_scope (nested_name_specifier);
21953 else if (nested_name_specifier)
21955 tree class_type;
21957 /* Given:
21959 template <typename T> struct S { struct T };
21960 template <typename T> struct S<T>::T { };
21962 we will get a TYPENAME_TYPE when processing the definition of
21963 `S::T'. We need to resolve it to the actual type before we
21964 try to define it. */
21965 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21967 class_type = resolve_typename_type (TREE_TYPE (type),
21968 /*only_current_p=*/false);
21969 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21970 type = TYPE_NAME (class_type);
21971 else
21973 cp_parser_error (parser, "could not resolve typename type");
21974 type = error_mark_node;
21978 if (maybe_process_partial_specialization (TREE_TYPE (type))
21979 == error_mark_node)
21981 type = NULL_TREE;
21982 goto done;
21985 class_type = current_class_type;
21986 /* Enter the scope indicated by the nested-name-specifier. */
21987 pushed_scope = push_scope (nested_name_specifier);
21988 /* Get the canonical version of this type. */
21989 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21990 /* Call push_template_decl if it seems like we should be defining a
21991 template either from the template headers or the type we're
21992 defining, so that we diagnose both extra and missing headers. */
21993 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21994 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21995 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21997 type = push_template_decl (type);
21998 if (type == error_mark_node)
22000 type = NULL_TREE;
22001 goto done;
22005 type = TREE_TYPE (type);
22006 *nested_name_specifier_p = true;
22008 else /* The name is not a nested name. */
22010 /* If the class was unnamed, create a dummy name. */
22011 if (!id)
22012 id = make_anon_name ();
22013 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22014 parser->num_template_parameter_lists);
22017 /* Indicate whether this class was declared as a `class' or as a
22018 `struct'. */
22019 if (TREE_CODE (type) == RECORD_TYPE)
22020 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22021 cp_parser_check_class_key (class_key, type);
22023 /* If this type was already complete, and we see another definition,
22024 that's an error. */
22025 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22027 error_at (type_start_token->location, "redefinition of %q#T",
22028 type);
22029 inform (location_of (type), "previous definition of %q#T",
22030 type);
22031 type = NULL_TREE;
22032 goto done;
22034 else if (type == error_mark_node)
22035 type = NULL_TREE;
22037 if (type)
22039 /* Apply attributes now, before any use of the class as a template
22040 argument in its base list. */
22041 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22042 fixup_attribute_variants (type);
22045 /* We will have entered the scope containing the class; the names of
22046 base classes should be looked up in that context. For example:
22048 struct A { struct B {}; struct C; };
22049 struct A::C : B {};
22051 is valid. */
22053 /* Get the list of base-classes, if there is one. */
22054 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22056 /* PR59482: enter the class scope so that base-specifiers are looked
22057 up correctly. */
22058 if (type)
22059 pushclass (type);
22060 bases = cp_parser_base_clause (parser);
22061 /* PR59482: get out of the previously pushed class scope so that the
22062 subsequent pops pop the right thing. */
22063 if (type)
22064 popclass ();
22066 else
22067 bases = NULL_TREE;
22069 /* If we're really defining a class, process the base classes.
22070 If they're invalid, fail. */
22071 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22072 xref_basetypes (type, bases);
22074 done:
22075 /* Leave the scope given by the nested-name-specifier. We will
22076 enter the class scope itself while processing the members. */
22077 if (pushed_scope)
22078 pop_scope (pushed_scope);
22080 if (invalid_explicit_specialization_p)
22082 end_specialization ();
22083 --parser->num_template_parameter_lists;
22086 if (type)
22087 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22088 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22089 CLASSTYPE_FINAL (type) = 1;
22090 out:
22091 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22092 return type;
22095 /* Parse a class-key.
22097 class-key:
22098 class
22099 struct
22100 union
22102 Returns the kind of class-key specified, or none_type to indicate
22103 error. */
22105 static enum tag_types
22106 cp_parser_class_key (cp_parser* parser)
22108 cp_token *token;
22109 enum tag_types tag_type;
22111 /* Look for the class-key. */
22112 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22113 if (!token)
22114 return none_type;
22116 /* Check to see if the TOKEN is a class-key. */
22117 tag_type = cp_parser_token_is_class_key (token);
22118 if (!tag_type)
22119 cp_parser_error (parser, "expected class-key");
22120 return tag_type;
22123 /* Parse a type-parameter-key.
22125 type-parameter-key:
22126 class
22127 typename
22130 static void
22131 cp_parser_type_parameter_key (cp_parser* parser)
22133 /* Look for the type-parameter-key. */
22134 enum tag_types tag_type = none_type;
22135 cp_token *token = cp_lexer_peek_token (parser->lexer);
22136 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22138 cp_lexer_consume_token (parser->lexer);
22139 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22140 /* typename is not allowed in a template template parameter
22141 by the standard until C++1Z. */
22142 pedwarn (token->location, OPT_Wpedantic,
22143 "ISO C++ forbids typename key in template template parameter;"
22144 " use -std=c++1z or -std=gnu++1z");
22146 else
22147 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22149 return;
22152 /* Parse an (optional) member-specification.
22154 member-specification:
22155 member-declaration member-specification [opt]
22156 access-specifier : member-specification [opt] */
22158 static void
22159 cp_parser_member_specification_opt (cp_parser* parser)
22161 while (true)
22163 cp_token *token;
22164 enum rid keyword;
22166 /* Peek at the next token. */
22167 token = cp_lexer_peek_token (parser->lexer);
22168 /* If it's a `}', or EOF then we've seen all the members. */
22169 if (token->type == CPP_CLOSE_BRACE
22170 || token->type == CPP_EOF
22171 || token->type == CPP_PRAGMA_EOL)
22172 break;
22174 /* See if this token is a keyword. */
22175 keyword = token->keyword;
22176 switch (keyword)
22178 case RID_PUBLIC:
22179 case RID_PROTECTED:
22180 case RID_PRIVATE:
22181 /* Consume the access-specifier. */
22182 cp_lexer_consume_token (parser->lexer);
22183 /* Remember which access-specifier is active. */
22184 current_access_specifier = token->u.value;
22185 /* Look for the `:'. */
22186 cp_parser_require (parser, CPP_COLON, RT_COLON);
22187 break;
22189 default:
22190 /* Accept #pragmas at class scope. */
22191 if (token->type == CPP_PRAGMA)
22193 cp_parser_pragma (parser, pragma_member, NULL);
22194 break;
22197 /* Otherwise, the next construction must be a
22198 member-declaration. */
22199 cp_parser_member_declaration (parser);
22204 /* Parse a member-declaration.
22206 member-declaration:
22207 decl-specifier-seq [opt] member-declarator-list [opt] ;
22208 function-definition ; [opt]
22209 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22210 using-declaration
22211 template-declaration
22212 alias-declaration
22214 member-declarator-list:
22215 member-declarator
22216 member-declarator-list , member-declarator
22218 member-declarator:
22219 declarator pure-specifier [opt]
22220 declarator constant-initializer [opt]
22221 identifier [opt] : constant-expression
22223 GNU Extensions:
22225 member-declaration:
22226 __extension__ member-declaration
22228 member-declarator:
22229 declarator attributes [opt] pure-specifier [opt]
22230 declarator attributes [opt] constant-initializer [opt]
22231 identifier [opt] attributes [opt] : constant-expression
22233 C++0x Extensions:
22235 member-declaration:
22236 static_assert-declaration */
22238 static void
22239 cp_parser_member_declaration (cp_parser* parser)
22241 cp_decl_specifier_seq decl_specifiers;
22242 tree prefix_attributes;
22243 tree decl;
22244 int declares_class_or_enum;
22245 bool friend_p;
22246 cp_token *token = NULL;
22247 cp_token *decl_spec_token_start = NULL;
22248 cp_token *initializer_token_start = NULL;
22249 int saved_pedantic;
22250 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22252 /* Check for the `__extension__' keyword. */
22253 if (cp_parser_extension_opt (parser, &saved_pedantic))
22255 /* Recurse. */
22256 cp_parser_member_declaration (parser);
22257 /* Restore the old value of the PEDANTIC flag. */
22258 pedantic = saved_pedantic;
22260 return;
22263 /* Check for a template-declaration. */
22264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22266 /* An explicit specialization here is an error condition, and we
22267 expect the specialization handler to detect and report this. */
22268 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22269 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22270 cp_parser_explicit_specialization (parser);
22271 else
22272 cp_parser_template_declaration (parser, /*member_p=*/true);
22274 return;
22276 /* Check for a template introduction. */
22277 else if (cp_parser_template_declaration_after_export (parser, true))
22278 return;
22280 /* Check for a using-declaration. */
22281 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22283 if (cxx_dialect < cxx11)
22285 /* Parse the using-declaration. */
22286 cp_parser_using_declaration (parser,
22287 /*access_declaration_p=*/false);
22288 return;
22290 else
22292 tree decl;
22293 bool alias_decl_expected;
22294 cp_parser_parse_tentatively (parser);
22295 decl = cp_parser_alias_declaration (parser);
22296 /* Note that if we actually see the '=' token after the
22297 identifier, cp_parser_alias_declaration commits the
22298 tentative parse. In that case, we really expect an
22299 alias-declaration. Otherwise, we expect a using
22300 declaration. */
22301 alias_decl_expected =
22302 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22303 cp_parser_parse_definitely (parser);
22305 if (alias_decl_expected)
22306 finish_member_declaration (decl);
22307 else
22308 cp_parser_using_declaration (parser,
22309 /*access_declaration_p=*/false);
22310 return;
22314 /* Check for @defs. */
22315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22317 tree ivar, member;
22318 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22319 ivar = ivar_chains;
22320 while (ivar)
22322 member = ivar;
22323 ivar = TREE_CHAIN (member);
22324 TREE_CHAIN (member) = NULL_TREE;
22325 finish_member_declaration (member);
22327 return;
22330 /* If the next token is `static_assert' we have a static assertion. */
22331 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22333 cp_parser_static_assert (parser, /*member_p=*/true);
22334 return;
22337 parser->colon_corrects_to_scope_p = false;
22339 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22340 goto out;
22342 /* Parse the decl-specifier-seq. */
22343 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22344 cp_parser_decl_specifier_seq (parser,
22345 CP_PARSER_FLAGS_OPTIONAL,
22346 &decl_specifiers,
22347 &declares_class_or_enum);
22348 /* Check for an invalid type-name. */
22349 if (!decl_specifiers.any_type_specifiers_p
22350 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22351 goto out;
22352 /* If there is no declarator, then the decl-specifier-seq should
22353 specify a type. */
22354 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22356 /* If there was no decl-specifier-seq, and the next token is a
22357 `;', then we have something like:
22359 struct S { ; };
22361 [class.mem]
22363 Each member-declaration shall declare at least one member
22364 name of the class. */
22365 if (!decl_specifiers.any_specifiers_p)
22367 cp_token *token = cp_lexer_peek_token (parser->lexer);
22368 if (!in_system_header_at (token->location))
22369 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22371 else
22373 tree type;
22375 /* See if this declaration is a friend. */
22376 friend_p = cp_parser_friend_p (&decl_specifiers);
22377 /* If there were decl-specifiers, check to see if there was
22378 a class-declaration. */
22379 type = check_tag_decl (&decl_specifiers,
22380 /*explicit_type_instantiation_p=*/false);
22381 /* Nested classes have already been added to the class, but
22382 a `friend' needs to be explicitly registered. */
22383 if (friend_p)
22385 /* If the `friend' keyword was present, the friend must
22386 be introduced with a class-key. */
22387 if (!declares_class_or_enum && cxx_dialect < cxx11)
22388 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22389 "in C++03 a class-key must be used "
22390 "when declaring a friend");
22391 /* In this case:
22393 template <typename T> struct A {
22394 friend struct A<T>::B;
22397 A<T>::B will be represented by a TYPENAME_TYPE, and
22398 therefore not recognized by check_tag_decl. */
22399 if (!type)
22401 type = decl_specifiers.type;
22402 if (type && TREE_CODE (type) == TYPE_DECL)
22403 type = TREE_TYPE (type);
22405 if (!type || !TYPE_P (type))
22406 error_at (decl_spec_token_start->location,
22407 "friend declaration does not name a class or "
22408 "function");
22409 else
22410 make_friend_class (current_class_type, type,
22411 /*complain=*/true);
22413 /* If there is no TYPE, an error message will already have
22414 been issued. */
22415 else if (!type || type == error_mark_node)
22417 /* An anonymous aggregate has to be handled specially; such
22418 a declaration really declares a data member (with a
22419 particular type), as opposed to a nested class. */
22420 else if (ANON_AGGR_TYPE_P (type))
22422 /* C++11 9.5/6. */
22423 if (decl_specifiers.storage_class != sc_none)
22424 error_at (decl_spec_token_start->location,
22425 "a storage class on an anonymous aggregate "
22426 "in class scope is not allowed");
22428 /* Remove constructors and such from TYPE, now that we
22429 know it is an anonymous aggregate. */
22430 fixup_anonymous_aggr (type);
22431 /* And make the corresponding data member. */
22432 decl = build_decl (decl_spec_token_start->location,
22433 FIELD_DECL, NULL_TREE, type);
22434 /* Add it to the class. */
22435 finish_member_declaration (decl);
22437 else
22438 cp_parser_check_access_in_redeclaration
22439 (TYPE_NAME (type),
22440 decl_spec_token_start->location);
22443 else
22445 bool assume_semicolon = false;
22447 /* Clear attributes from the decl_specifiers but keep them
22448 around as prefix attributes that apply them to the entity
22449 being declared. */
22450 prefix_attributes = decl_specifiers.attributes;
22451 decl_specifiers.attributes = NULL_TREE;
22453 /* See if these declarations will be friends. */
22454 friend_p = cp_parser_friend_p (&decl_specifiers);
22456 /* Keep going until we hit the `;' at the end of the
22457 declaration. */
22458 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22460 tree attributes = NULL_TREE;
22461 tree first_attribute;
22463 /* Peek at the next token. */
22464 token = cp_lexer_peek_token (parser->lexer);
22466 /* Check for a bitfield declaration. */
22467 if (token->type == CPP_COLON
22468 || (token->type == CPP_NAME
22469 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22470 == CPP_COLON))
22472 tree identifier;
22473 tree width;
22475 /* Get the name of the bitfield. Note that we cannot just
22476 check TOKEN here because it may have been invalidated by
22477 the call to cp_lexer_peek_nth_token above. */
22478 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22479 identifier = cp_parser_identifier (parser);
22480 else
22481 identifier = NULL_TREE;
22483 /* Consume the `:' token. */
22484 cp_lexer_consume_token (parser->lexer);
22485 /* Get the width of the bitfield. */
22486 width
22487 = cp_parser_constant_expression (parser);
22489 /* Look for attributes that apply to the bitfield. */
22490 attributes = cp_parser_attributes_opt (parser);
22491 /* Remember which attributes are prefix attributes and
22492 which are not. */
22493 first_attribute = attributes;
22494 /* Combine the attributes. */
22495 attributes = chainon (prefix_attributes, attributes);
22497 /* Create the bitfield declaration. */
22498 decl = grokbitfield (identifier
22499 ? make_id_declarator (NULL_TREE,
22500 identifier,
22501 sfk_none)
22502 : NULL,
22503 &decl_specifiers,
22504 width,
22505 attributes);
22507 else
22509 cp_declarator *declarator;
22510 tree initializer;
22511 tree asm_specification;
22512 int ctor_dtor_or_conv_p;
22514 /* Parse the declarator. */
22515 declarator
22516 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22517 &ctor_dtor_or_conv_p,
22518 /*parenthesized_p=*/NULL,
22519 /*member_p=*/true,
22520 friend_p);
22522 /* If something went wrong parsing the declarator, make sure
22523 that we at least consume some tokens. */
22524 if (declarator == cp_error_declarator)
22526 /* Skip to the end of the statement. */
22527 cp_parser_skip_to_end_of_statement (parser);
22528 /* If the next token is not a semicolon, that is
22529 probably because we just skipped over the body of
22530 a function. So, we consume a semicolon if
22531 present, but do not issue an error message if it
22532 is not present. */
22533 if (cp_lexer_next_token_is (parser->lexer,
22534 CPP_SEMICOLON))
22535 cp_lexer_consume_token (parser->lexer);
22536 goto out;
22539 if (declares_class_or_enum & 2)
22540 cp_parser_check_for_definition_in_return_type
22541 (declarator, decl_specifiers.type,
22542 decl_specifiers.locations[ds_type_spec]);
22544 /* Look for an asm-specification. */
22545 asm_specification = cp_parser_asm_specification_opt (parser);
22546 /* Look for attributes that apply to the declaration. */
22547 attributes = cp_parser_attributes_opt (parser);
22548 /* Remember which attributes are prefix attributes and
22549 which are not. */
22550 first_attribute = attributes;
22551 /* Combine the attributes. */
22552 attributes = chainon (prefix_attributes, attributes);
22554 /* If it's an `=', then we have a constant-initializer or a
22555 pure-specifier. It is not correct to parse the
22556 initializer before registering the member declaration
22557 since the member declaration should be in scope while
22558 its initializer is processed. However, the rest of the
22559 front end does not yet provide an interface that allows
22560 us to handle this correctly. */
22561 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22563 /* In [class.mem]:
22565 A pure-specifier shall be used only in the declaration of
22566 a virtual function.
22568 A member-declarator can contain a constant-initializer
22569 only if it declares a static member of integral or
22570 enumeration type.
22572 Therefore, if the DECLARATOR is for a function, we look
22573 for a pure-specifier; otherwise, we look for a
22574 constant-initializer. When we call `grokfield', it will
22575 perform more stringent semantics checks. */
22576 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22577 if (function_declarator_p (declarator)
22578 || (decl_specifiers.type
22579 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22580 && declarator->kind == cdk_id
22581 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22582 == FUNCTION_TYPE)))
22583 initializer = cp_parser_pure_specifier (parser);
22584 else if (decl_specifiers.storage_class != sc_static)
22585 initializer = cp_parser_save_nsdmi (parser);
22586 else if (cxx_dialect >= cxx11)
22588 bool nonconst;
22589 /* Don't require a constant rvalue in C++11, since we
22590 might want a reference constant. We'll enforce
22591 constancy later. */
22592 cp_lexer_consume_token (parser->lexer);
22593 /* Parse the initializer. */
22594 initializer = cp_parser_initializer_clause (parser,
22595 &nonconst);
22597 else
22598 /* Parse the initializer. */
22599 initializer = cp_parser_constant_initializer (parser);
22601 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22602 && !function_declarator_p (declarator))
22604 bool x;
22605 if (decl_specifiers.storage_class != sc_static)
22606 initializer = cp_parser_save_nsdmi (parser);
22607 else
22608 initializer = cp_parser_initializer (parser, &x, &x);
22610 /* Otherwise, there is no initializer. */
22611 else
22612 initializer = NULL_TREE;
22614 /* See if we are probably looking at a function
22615 definition. We are certainly not looking at a
22616 member-declarator. Calling `grokfield' has
22617 side-effects, so we must not do it unless we are sure
22618 that we are looking at a member-declarator. */
22619 if (cp_parser_token_starts_function_definition_p
22620 (cp_lexer_peek_token (parser->lexer)))
22622 /* The grammar does not allow a pure-specifier to be
22623 used when a member function is defined. (It is
22624 possible that this fact is an oversight in the
22625 standard, since a pure function may be defined
22626 outside of the class-specifier. */
22627 if (initializer && initializer_token_start)
22628 error_at (initializer_token_start->location,
22629 "pure-specifier on function-definition");
22630 decl = cp_parser_save_member_function_body (parser,
22631 &decl_specifiers,
22632 declarator,
22633 attributes);
22634 if (parser->fully_implicit_function_template_p)
22635 decl = finish_fully_implicit_template (parser, decl);
22636 /* If the member was not a friend, declare it here. */
22637 if (!friend_p)
22638 finish_member_declaration (decl);
22639 /* Peek at the next token. */
22640 token = cp_lexer_peek_token (parser->lexer);
22641 /* If the next token is a semicolon, consume it. */
22642 if (token->type == CPP_SEMICOLON)
22643 cp_lexer_consume_token (parser->lexer);
22644 goto out;
22646 else
22647 if (declarator->kind == cdk_function)
22648 declarator->id_loc = token->location;
22649 /* Create the declaration. */
22650 decl = grokfield (declarator, &decl_specifiers,
22651 initializer, /*init_const_expr_p=*/true,
22652 asm_specification, attributes);
22653 if (parser->fully_implicit_function_template_p)
22655 if (friend_p)
22656 finish_fully_implicit_template (parser, 0);
22657 else
22658 decl = finish_fully_implicit_template (parser, decl);
22662 cp_finalize_omp_declare_simd (parser, decl);
22663 cp_finalize_oacc_routine (parser, decl, false);
22665 /* Reset PREFIX_ATTRIBUTES. */
22666 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22667 attributes = TREE_CHAIN (attributes);
22668 if (attributes)
22669 TREE_CHAIN (attributes) = NULL_TREE;
22671 /* If there is any qualification still in effect, clear it
22672 now; we will be starting fresh with the next declarator. */
22673 parser->scope = NULL_TREE;
22674 parser->qualifying_scope = NULL_TREE;
22675 parser->object_scope = NULL_TREE;
22676 /* If it's a `,', then there are more declarators. */
22677 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22679 cp_lexer_consume_token (parser->lexer);
22680 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22682 cp_token *token = cp_lexer_previous_token (parser->lexer);
22683 error_at (token->location,
22684 "stray %<,%> at end of member declaration");
22687 /* If the next token isn't a `;', then we have a parse error. */
22688 else if (cp_lexer_next_token_is_not (parser->lexer,
22689 CPP_SEMICOLON))
22691 /* The next token might be a ways away from where the
22692 actual semicolon is missing. Find the previous token
22693 and use that for our error position. */
22694 cp_token *token = cp_lexer_previous_token (parser->lexer);
22695 error_at (token->location,
22696 "expected %<;%> at end of member declaration");
22698 /* Assume that the user meant to provide a semicolon. If
22699 we were to cp_parser_skip_to_end_of_statement, we might
22700 skip to a semicolon inside a member function definition
22701 and issue nonsensical error messages. */
22702 assume_semicolon = true;
22705 if (decl)
22707 /* Add DECL to the list of members. */
22708 if (!friend_p
22709 /* Explicitly include, eg, NSDMIs, for better error
22710 recovery (c++/58650). */
22711 || !DECL_DECLARES_FUNCTION_P (decl))
22712 finish_member_declaration (decl);
22714 if (TREE_CODE (decl) == FUNCTION_DECL)
22715 cp_parser_save_default_args (parser, decl);
22716 else if (TREE_CODE (decl) == FIELD_DECL
22717 && !DECL_C_BIT_FIELD (decl)
22718 && DECL_INITIAL (decl))
22719 /* Add DECL to the queue of NSDMI to be parsed later. */
22720 vec_safe_push (unparsed_nsdmis, decl);
22723 if (assume_semicolon)
22724 goto out;
22728 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22729 out:
22730 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22733 /* Parse a pure-specifier.
22735 pure-specifier:
22738 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22739 Otherwise, ERROR_MARK_NODE is returned. */
22741 static tree
22742 cp_parser_pure_specifier (cp_parser* parser)
22744 cp_token *token;
22746 /* Look for the `=' token. */
22747 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22748 return error_mark_node;
22749 /* Look for the `0' token. */
22750 token = cp_lexer_peek_token (parser->lexer);
22752 if (token->type == CPP_EOF
22753 || token->type == CPP_PRAGMA_EOL)
22754 return error_mark_node;
22756 cp_lexer_consume_token (parser->lexer);
22758 /* Accept = default or = delete in c++0x mode. */
22759 if (token->keyword == RID_DEFAULT
22760 || token->keyword == RID_DELETE)
22762 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22763 return token->u.value;
22766 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22767 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22769 cp_parser_error (parser,
22770 "invalid pure specifier (only %<= 0%> is allowed)");
22771 cp_parser_skip_to_end_of_statement (parser);
22772 return error_mark_node;
22774 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22776 error_at (token->location, "templates may not be %<virtual%>");
22777 return error_mark_node;
22780 return integer_zero_node;
22783 /* Parse a constant-initializer.
22785 constant-initializer:
22786 = constant-expression
22788 Returns a representation of the constant-expression. */
22790 static tree
22791 cp_parser_constant_initializer (cp_parser* parser)
22793 /* Look for the `=' token. */
22794 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22795 return error_mark_node;
22797 /* It is invalid to write:
22799 struct S { static const int i = { 7 }; };
22802 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22804 cp_parser_error (parser,
22805 "a brace-enclosed initializer is not allowed here");
22806 /* Consume the opening brace. */
22807 cp_lexer_consume_token (parser->lexer);
22808 /* Skip the initializer. */
22809 cp_parser_skip_to_closing_brace (parser);
22810 /* Look for the trailing `}'. */
22811 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22813 return error_mark_node;
22816 return cp_parser_constant_expression (parser);
22819 /* Derived classes [gram.class.derived] */
22821 /* Parse a base-clause.
22823 base-clause:
22824 : base-specifier-list
22826 base-specifier-list:
22827 base-specifier ... [opt]
22828 base-specifier-list , base-specifier ... [opt]
22830 Returns a TREE_LIST representing the base-classes, in the order in
22831 which they were declared. The representation of each node is as
22832 described by cp_parser_base_specifier.
22834 In the case that no bases are specified, this function will return
22835 NULL_TREE, not ERROR_MARK_NODE. */
22837 static tree
22838 cp_parser_base_clause (cp_parser* parser)
22840 tree bases = NULL_TREE;
22842 /* Look for the `:' that begins the list. */
22843 cp_parser_require (parser, CPP_COLON, RT_COLON);
22845 /* Scan the base-specifier-list. */
22846 while (true)
22848 cp_token *token;
22849 tree base;
22850 bool pack_expansion_p = false;
22852 /* Look for the base-specifier. */
22853 base = cp_parser_base_specifier (parser);
22854 /* Look for the (optional) ellipsis. */
22855 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22857 /* Consume the `...'. */
22858 cp_lexer_consume_token (parser->lexer);
22860 pack_expansion_p = true;
22863 /* Add BASE to the front of the list. */
22864 if (base && base != error_mark_node)
22866 if (pack_expansion_p)
22867 /* Make this a pack expansion type. */
22868 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22870 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22872 TREE_CHAIN (base) = bases;
22873 bases = base;
22876 /* Peek at the next token. */
22877 token = cp_lexer_peek_token (parser->lexer);
22878 /* If it's not a comma, then the list is complete. */
22879 if (token->type != CPP_COMMA)
22880 break;
22881 /* Consume the `,'. */
22882 cp_lexer_consume_token (parser->lexer);
22885 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22886 base class had a qualified name. However, the next name that
22887 appears is certainly not qualified. */
22888 parser->scope = NULL_TREE;
22889 parser->qualifying_scope = NULL_TREE;
22890 parser->object_scope = NULL_TREE;
22892 return nreverse (bases);
22895 /* Parse a base-specifier.
22897 base-specifier:
22898 :: [opt] nested-name-specifier [opt] class-name
22899 virtual access-specifier [opt] :: [opt] nested-name-specifier
22900 [opt] class-name
22901 access-specifier virtual [opt] :: [opt] nested-name-specifier
22902 [opt] class-name
22904 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22905 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22906 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22907 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22909 static tree
22910 cp_parser_base_specifier (cp_parser* parser)
22912 cp_token *token;
22913 bool done = false;
22914 bool virtual_p = false;
22915 bool duplicate_virtual_error_issued_p = false;
22916 bool duplicate_access_error_issued_p = false;
22917 bool class_scope_p, template_p;
22918 tree access = access_default_node;
22919 tree type;
22921 /* Process the optional `virtual' and `access-specifier'. */
22922 while (!done)
22924 /* Peek at the next token. */
22925 token = cp_lexer_peek_token (parser->lexer);
22926 /* Process `virtual'. */
22927 switch (token->keyword)
22929 case RID_VIRTUAL:
22930 /* If `virtual' appears more than once, issue an error. */
22931 if (virtual_p && !duplicate_virtual_error_issued_p)
22933 cp_parser_error (parser,
22934 "%<virtual%> specified more than once in base-specified");
22935 duplicate_virtual_error_issued_p = true;
22938 virtual_p = true;
22940 /* Consume the `virtual' token. */
22941 cp_lexer_consume_token (parser->lexer);
22943 break;
22945 case RID_PUBLIC:
22946 case RID_PROTECTED:
22947 case RID_PRIVATE:
22948 /* If more than one access specifier appears, issue an
22949 error. */
22950 if (access != access_default_node
22951 && !duplicate_access_error_issued_p)
22953 cp_parser_error (parser,
22954 "more than one access specifier in base-specified");
22955 duplicate_access_error_issued_p = true;
22958 access = ridpointers[(int) token->keyword];
22960 /* Consume the access-specifier. */
22961 cp_lexer_consume_token (parser->lexer);
22963 break;
22965 default:
22966 done = true;
22967 break;
22970 /* It is not uncommon to see programs mechanically, erroneously, use
22971 the 'typename' keyword to denote (dependent) qualified types
22972 as base classes. */
22973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22975 token = cp_lexer_peek_token (parser->lexer);
22976 if (!processing_template_decl)
22977 error_at (token->location,
22978 "keyword %<typename%> not allowed outside of templates");
22979 else
22980 error_at (token->location,
22981 "keyword %<typename%> not allowed in this context "
22982 "(the base class is implicitly a type)");
22983 cp_lexer_consume_token (parser->lexer);
22986 /* Look for the optional `::' operator. */
22987 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22988 /* Look for the nested-name-specifier. The simplest way to
22989 implement:
22991 [temp.res]
22993 The keyword `typename' is not permitted in a base-specifier or
22994 mem-initializer; in these contexts a qualified name that
22995 depends on a template-parameter is implicitly assumed to be a
22996 type name.
22998 is to pretend that we have seen the `typename' keyword at this
22999 point. */
23000 cp_parser_nested_name_specifier_opt (parser,
23001 /*typename_keyword_p=*/true,
23002 /*check_dependency_p=*/true,
23003 typename_type,
23004 /*is_declaration=*/true);
23005 /* If the base class is given by a qualified name, assume that names
23006 we see are type names or templates, as appropriate. */
23007 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23008 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23010 if (!parser->scope
23011 && cp_lexer_next_token_is_decltype (parser->lexer))
23012 /* DR 950 allows decltype as a base-specifier. */
23013 type = cp_parser_decltype (parser);
23014 else
23016 /* Otherwise, look for the class-name. */
23017 type = cp_parser_class_name (parser,
23018 class_scope_p,
23019 template_p,
23020 typename_type,
23021 /*check_dependency_p=*/true,
23022 /*class_head_p=*/false,
23023 /*is_declaration=*/true);
23024 type = TREE_TYPE (type);
23027 if (type == error_mark_node)
23028 return error_mark_node;
23030 return finish_base_specifier (type, access, virtual_p);
23033 /* Exception handling [gram.exception] */
23035 /* Parse an (optional) noexcept-specification.
23037 noexcept-specification:
23038 noexcept ( constant-expression ) [opt]
23040 If no noexcept-specification is present, returns NULL_TREE.
23041 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23042 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23043 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23044 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23045 in which case a boolean condition is returned instead. */
23047 static tree
23048 cp_parser_noexcept_specification_opt (cp_parser* parser,
23049 bool require_constexpr,
23050 bool* consumed_expr,
23051 bool return_cond)
23053 cp_token *token;
23054 const char *saved_message;
23056 /* Peek at the next token. */
23057 token = cp_lexer_peek_token (parser->lexer);
23059 /* Is it a noexcept-specification? */
23060 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23062 tree expr;
23063 cp_lexer_consume_token (parser->lexer);
23065 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23067 cp_lexer_consume_token (parser->lexer);
23069 if (require_constexpr)
23071 /* Types may not be defined in an exception-specification. */
23072 saved_message = parser->type_definition_forbidden_message;
23073 parser->type_definition_forbidden_message
23074 = G_("types may not be defined in an exception-specification");
23076 expr = cp_parser_constant_expression (parser);
23078 /* Restore the saved message. */
23079 parser->type_definition_forbidden_message = saved_message;
23081 else
23083 expr = cp_parser_expression (parser);
23084 *consumed_expr = true;
23087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23089 else
23091 expr = boolean_true_node;
23092 if (!require_constexpr)
23093 *consumed_expr = false;
23096 /* We cannot build a noexcept-spec right away because this will check
23097 that expr is a constexpr. */
23098 if (!return_cond)
23099 return build_noexcept_spec (expr, tf_warning_or_error);
23100 else
23101 return expr;
23103 else
23104 return NULL_TREE;
23107 /* Parse an (optional) exception-specification.
23109 exception-specification:
23110 throw ( type-id-list [opt] )
23112 Returns a TREE_LIST representing the exception-specification. The
23113 TREE_VALUE of each node is a type. */
23115 static tree
23116 cp_parser_exception_specification_opt (cp_parser* parser)
23118 cp_token *token;
23119 tree type_id_list;
23120 const char *saved_message;
23122 /* Peek at the next token. */
23123 token = cp_lexer_peek_token (parser->lexer);
23125 /* Is it a noexcept-specification? */
23126 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23127 false);
23128 if (type_id_list != NULL_TREE)
23129 return type_id_list;
23131 /* If it's not `throw', then there's no exception-specification. */
23132 if (!cp_parser_is_keyword (token, RID_THROW))
23133 return NULL_TREE;
23135 #if 0
23136 /* Enable this once a lot of code has transitioned to noexcept? */
23137 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23138 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23139 "deprecated in C++0x; use %<noexcept%> instead");
23140 #endif
23142 /* Consume the `throw'. */
23143 cp_lexer_consume_token (parser->lexer);
23145 /* Look for the `('. */
23146 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23148 /* Peek at the next token. */
23149 token = cp_lexer_peek_token (parser->lexer);
23150 /* If it's not a `)', then there is a type-id-list. */
23151 if (token->type != CPP_CLOSE_PAREN)
23153 /* Types may not be defined in an exception-specification. */
23154 saved_message = parser->type_definition_forbidden_message;
23155 parser->type_definition_forbidden_message
23156 = G_("types may not be defined in an exception-specification");
23157 /* Parse the type-id-list. */
23158 type_id_list = cp_parser_type_id_list (parser);
23159 /* Restore the saved message. */
23160 parser->type_definition_forbidden_message = saved_message;
23162 else
23163 type_id_list = empty_except_spec;
23165 /* Look for the `)'. */
23166 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23168 return type_id_list;
23171 /* Parse an (optional) type-id-list.
23173 type-id-list:
23174 type-id ... [opt]
23175 type-id-list , type-id ... [opt]
23177 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23178 in the order that the types were presented. */
23180 static tree
23181 cp_parser_type_id_list (cp_parser* parser)
23183 tree types = NULL_TREE;
23185 while (true)
23187 cp_token *token;
23188 tree type;
23190 token = cp_lexer_peek_token (parser->lexer);
23192 /* Get the next type-id. */
23193 type = cp_parser_type_id (parser);
23194 /* Check for invalid 'auto'. */
23195 if (flag_concepts && type_uses_auto (type))
23197 error_at (token->location,
23198 "invalid use of %<auto%> in exception-specification");
23199 type = error_mark_node;
23201 /* Parse the optional ellipsis. */
23202 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23204 /* Consume the `...'. */
23205 cp_lexer_consume_token (parser->lexer);
23207 /* Turn the type into a pack expansion expression. */
23208 type = make_pack_expansion (type);
23210 /* Add it to the list. */
23211 types = add_exception_specifier (types, type, /*complain=*/1);
23212 /* Peek at the next token. */
23213 token = cp_lexer_peek_token (parser->lexer);
23214 /* If it is not a `,', we are done. */
23215 if (token->type != CPP_COMMA)
23216 break;
23217 /* Consume the `,'. */
23218 cp_lexer_consume_token (parser->lexer);
23221 return nreverse (types);
23224 /* Parse a try-block.
23226 try-block:
23227 try compound-statement handler-seq */
23229 static tree
23230 cp_parser_try_block (cp_parser* parser)
23232 tree try_block;
23234 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23235 if (parser->in_function_body
23236 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23237 error ("%<try%> in %<constexpr%> function");
23239 try_block = begin_try_block ();
23240 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23241 finish_try_block (try_block);
23242 cp_parser_handler_seq (parser);
23243 finish_handler_sequence (try_block);
23245 return try_block;
23248 /* Parse a function-try-block.
23250 function-try-block:
23251 try ctor-initializer [opt] function-body handler-seq */
23253 static bool
23254 cp_parser_function_try_block (cp_parser* parser)
23256 tree compound_stmt;
23257 tree try_block;
23258 bool ctor_initializer_p;
23260 /* Look for the `try' keyword. */
23261 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23262 return false;
23263 /* Let the rest of the front end know where we are. */
23264 try_block = begin_function_try_block (&compound_stmt);
23265 /* Parse the function-body. */
23266 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23267 (parser, /*in_function_try_block=*/true);
23268 /* We're done with the `try' part. */
23269 finish_function_try_block (try_block);
23270 /* Parse the handlers. */
23271 cp_parser_handler_seq (parser);
23272 /* We're done with the handlers. */
23273 finish_function_handler_sequence (try_block, compound_stmt);
23275 return ctor_initializer_p;
23278 /* Parse a handler-seq.
23280 handler-seq:
23281 handler handler-seq [opt] */
23283 static void
23284 cp_parser_handler_seq (cp_parser* parser)
23286 while (true)
23288 cp_token *token;
23290 /* Parse the handler. */
23291 cp_parser_handler (parser);
23292 /* Peek at the next token. */
23293 token = cp_lexer_peek_token (parser->lexer);
23294 /* If it's not `catch' then there are no more handlers. */
23295 if (!cp_parser_is_keyword (token, RID_CATCH))
23296 break;
23300 /* Parse a handler.
23302 handler:
23303 catch ( exception-declaration ) compound-statement */
23305 static void
23306 cp_parser_handler (cp_parser* parser)
23308 tree handler;
23309 tree declaration;
23311 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23312 handler = begin_handler ();
23313 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23314 declaration = cp_parser_exception_declaration (parser);
23315 finish_handler_parms (declaration, handler);
23316 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23317 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23318 finish_handler (handler);
23321 /* Parse an exception-declaration.
23323 exception-declaration:
23324 type-specifier-seq declarator
23325 type-specifier-seq abstract-declarator
23326 type-specifier-seq
23329 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23330 ellipsis variant is used. */
23332 static tree
23333 cp_parser_exception_declaration (cp_parser* parser)
23335 cp_decl_specifier_seq type_specifiers;
23336 cp_declarator *declarator;
23337 const char *saved_message;
23339 /* If it's an ellipsis, it's easy to handle. */
23340 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23342 /* Consume the `...' token. */
23343 cp_lexer_consume_token (parser->lexer);
23344 return NULL_TREE;
23347 /* Types may not be defined in exception-declarations. */
23348 saved_message = parser->type_definition_forbidden_message;
23349 parser->type_definition_forbidden_message
23350 = G_("types may not be defined in exception-declarations");
23352 /* Parse the type-specifier-seq. */
23353 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23354 /*is_trailing_return=*/false,
23355 &type_specifiers);
23356 /* If it's a `)', then there is no declarator. */
23357 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23358 declarator = NULL;
23359 else
23360 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23361 /*ctor_dtor_or_conv_p=*/NULL,
23362 /*parenthesized_p=*/NULL,
23363 /*member_p=*/false,
23364 /*friend_p=*/false);
23366 /* Restore the saved message. */
23367 parser->type_definition_forbidden_message = saved_message;
23369 if (!type_specifiers.any_specifiers_p)
23370 return error_mark_node;
23372 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23375 /* Parse a throw-expression.
23377 throw-expression:
23378 throw assignment-expression [opt]
23380 Returns a THROW_EXPR representing the throw-expression. */
23382 static tree
23383 cp_parser_throw_expression (cp_parser* parser)
23385 tree expression;
23386 cp_token* token;
23388 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23389 token = cp_lexer_peek_token (parser->lexer);
23390 /* Figure out whether or not there is an assignment-expression
23391 following the "throw" keyword. */
23392 if (token->type == CPP_COMMA
23393 || token->type == CPP_SEMICOLON
23394 || token->type == CPP_CLOSE_PAREN
23395 || token->type == CPP_CLOSE_SQUARE
23396 || token->type == CPP_CLOSE_BRACE
23397 || token->type == CPP_COLON)
23398 expression = NULL_TREE;
23399 else
23400 expression = cp_parser_assignment_expression (parser);
23402 return build_throw (expression);
23405 /* GNU Extensions */
23407 /* Parse an (optional) asm-specification.
23409 asm-specification:
23410 asm ( string-literal )
23412 If the asm-specification is present, returns a STRING_CST
23413 corresponding to the string-literal. Otherwise, returns
23414 NULL_TREE. */
23416 static tree
23417 cp_parser_asm_specification_opt (cp_parser* parser)
23419 cp_token *token;
23420 tree asm_specification;
23422 /* Peek at the next token. */
23423 token = cp_lexer_peek_token (parser->lexer);
23424 /* If the next token isn't the `asm' keyword, then there's no
23425 asm-specification. */
23426 if (!cp_parser_is_keyword (token, RID_ASM))
23427 return NULL_TREE;
23429 /* Consume the `asm' token. */
23430 cp_lexer_consume_token (parser->lexer);
23431 /* Look for the `('. */
23432 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23434 /* Look for the string-literal. */
23435 asm_specification = cp_parser_string_literal (parser, false, false);
23437 /* Look for the `)'. */
23438 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23440 return asm_specification;
23443 /* Parse an asm-operand-list.
23445 asm-operand-list:
23446 asm-operand
23447 asm-operand-list , asm-operand
23449 asm-operand:
23450 string-literal ( expression )
23451 [ string-literal ] string-literal ( expression )
23453 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23454 each node is the expression. The TREE_PURPOSE is itself a
23455 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23456 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23457 is a STRING_CST for the string literal before the parenthesis. Returns
23458 ERROR_MARK_NODE if any of the operands are invalid. */
23460 static tree
23461 cp_parser_asm_operand_list (cp_parser* parser)
23463 tree asm_operands = NULL_TREE;
23464 bool invalid_operands = false;
23466 while (true)
23468 tree string_literal;
23469 tree expression;
23470 tree name;
23472 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23474 /* Consume the `[' token. */
23475 cp_lexer_consume_token (parser->lexer);
23476 /* Read the operand name. */
23477 name = cp_parser_identifier (parser);
23478 if (name != error_mark_node)
23479 name = build_string (IDENTIFIER_LENGTH (name),
23480 IDENTIFIER_POINTER (name));
23481 /* Look for the closing `]'. */
23482 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23484 else
23485 name = NULL_TREE;
23486 /* Look for the string-literal. */
23487 string_literal = cp_parser_string_literal (parser, false, false);
23489 /* Look for the `('. */
23490 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23491 /* Parse the expression. */
23492 expression = cp_parser_expression (parser);
23493 /* Look for the `)'. */
23494 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23496 if (name == error_mark_node
23497 || string_literal == error_mark_node
23498 || expression == error_mark_node)
23499 invalid_operands = true;
23501 /* Add this operand to the list. */
23502 asm_operands = tree_cons (build_tree_list (name, string_literal),
23503 expression,
23504 asm_operands);
23505 /* If the next token is not a `,', there are no more
23506 operands. */
23507 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23508 break;
23509 /* Consume the `,'. */
23510 cp_lexer_consume_token (parser->lexer);
23513 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23516 /* Parse an asm-clobber-list.
23518 asm-clobber-list:
23519 string-literal
23520 asm-clobber-list , string-literal
23522 Returns a TREE_LIST, indicating the clobbers in the order that they
23523 appeared. The TREE_VALUE of each node is a STRING_CST. */
23525 static tree
23526 cp_parser_asm_clobber_list (cp_parser* parser)
23528 tree clobbers = NULL_TREE;
23530 while (true)
23532 tree string_literal;
23534 /* Look for the string literal. */
23535 string_literal = cp_parser_string_literal (parser, false, false);
23536 /* Add it to the list. */
23537 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23538 /* If the next token is not a `,', then the list is
23539 complete. */
23540 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23541 break;
23542 /* Consume the `,' token. */
23543 cp_lexer_consume_token (parser->lexer);
23546 return clobbers;
23549 /* Parse an asm-label-list.
23551 asm-label-list:
23552 identifier
23553 asm-label-list , identifier
23555 Returns a TREE_LIST, indicating the labels in the order that they
23556 appeared. The TREE_VALUE of each node is a label. */
23558 static tree
23559 cp_parser_asm_label_list (cp_parser* parser)
23561 tree labels = NULL_TREE;
23563 while (true)
23565 tree identifier, label, name;
23567 /* Look for the identifier. */
23568 identifier = cp_parser_identifier (parser);
23569 if (!error_operand_p (identifier))
23571 label = lookup_label (identifier);
23572 if (TREE_CODE (label) == LABEL_DECL)
23574 TREE_USED (label) = 1;
23575 check_goto (label);
23576 name = build_string (IDENTIFIER_LENGTH (identifier),
23577 IDENTIFIER_POINTER (identifier));
23578 labels = tree_cons (name, label, labels);
23581 /* If the next token is not a `,', then the list is
23582 complete. */
23583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23584 break;
23585 /* Consume the `,' token. */
23586 cp_lexer_consume_token (parser->lexer);
23589 return nreverse (labels);
23592 /* Return TRUE iff the next tokens in the stream are possibly the
23593 beginning of a GNU extension attribute. */
23595 static bool
23596 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23598 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23601 /* Return TRUE iff the next tokens in the stream are possibly the
23602 beginning of a standard C++-11 attribute specifier. */
23604 static bool
23605 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23607 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23610 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23611 beginning of a standard C++-11 attribute specifier. */
23613 static bool
23614 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23616 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23618 return (cxx_dialect >= cxx11
23619 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23620 || (token->type == CPP_OPEN_SQUARE
23621 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23622 && token->type == CPP_OPEN_SQUARE)));
23625 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23626 beginning of a GNU extension attribute. */
23628 static bool
23629 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23631 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23633 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23636 /* Return true iff the next tokens can be the beginning of either a
23637 GNU attribute list, or a standard C++11 attribute sequence. */
23639 static bool
23640 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23642 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23643 || cp_next_tokens_can_be_std_attribute_p (parser));
23646 /* Return true iff the next Nth tokens can be the beginning of either
23647 a GNU attribute list, or a standard C++11 attribute sequence. */
23649 static bool
23650 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23652 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23653 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23656 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23657 of GNU attributes, or return NULL. */
23659 static tree
23660 cp_parser_attributes_opt (cp_parser *parser)
23662 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23663 return cp_parser_gnu_attributes_opt (parser);
23664 return cp_parser_std_attribute_spec_seq (parser);
23667 #define CILK_SIMD_FN_CLAUSE_MASK \
23668 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23669 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23670 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23671 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23672 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23674 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23675 vector [(<clauses>)] */
23677 static void
23678 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23680 bool first_p = parser->cilk_simd_fn_info == NULL;
23681 cp_token *token = v_token;
23682 if (first_p)
23684 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23685 parser->cilk_simd_fn_info->error_seen = false;
23686 parser->cilk_simd_fn_info->fndecl_seen = false;
23687 parser->cilk_simd_fn_info->tokens = vNULL;
23689 int paren_scope = 0;
23690 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23692 cp_lexer_consume_token (parser->lexer);
23693 v_token = cp_lexer_peek_token (parser->lexer);
23694 paren_scope++;
23696 while (paren_scope > 0)
23698 token = cp_lexer_peek_token (parser->lexer);
23699 if (token->type == CPP_OPEN_PAREN)
23700 paren_scope++;
23701 else if (token->type == CPP_CLOSE_PAREN)
23702 paren_scope--;
23703 /* Do not push the last ')' */
23704 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23705 cp_lexer_consume_token (parser->lexer);
23708 token->type = CPP_PRAGMA_EOL;
23709 parser->lexer->next_token = token;
23710 cp_lexer_consume_token (parser->lexer);
23712 struct cp_token_cache *cp
23713 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23714 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23717 /* Parse an (optional) series of attributes.
23719 attributes:
23720 attributes attribute
23722 attribute:
23723 __attribute__ (( attribute-list [opt] ))
23725 The return value is as for cp_parser_gnu_attribute_list. */
23727 static tree
23728 cp_parser_gnu_attributes_opt (cp_parser* parser)
23730 tree attributes = NULL_TREE;
23732 while (true)
23734 cp_token *token;
23735 tree attribute_list;
23736 bool ok = true;
23738 /* Peek at the next token. */
23739 token = cp_lexer_peek_token (parser->lexer);
23740 /* If it's not `__attribute__', then we're done. */
23741 if (token->keyword != RID_ATTRIBUTE)
23742 break;
23744 /* Consume the `__attribute__' keyword. */
23745 cp_lexer_consume_token (parser->lexer);
23746 /* Look for the two `(' tokens. */
23747 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23748 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23750 /* Peek at the next token. */
23751 token = cp_lexer_peek_token (parser->lexer);
23752 if (token->type != CPP_CLOSE_PAREN)
23753 /* Parse the attribute-list. */
23754 attribute_list = cp_parser_gnu_attribute_list (parser);
23755 else
23756 /* If the next token is a `)', then there is no attribute
23757 list. */
23758 attribute_list = NULL;
23760 /* Look for the two `)' tokens. */
23761 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23762 ok = false;
23763 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23764 ok = false;
23765 if (!ok)
23766 cp_parser_skip_to_end_of_statement (parser);
23768 /* Add these new attributes to the list. */
23769 attributes = chainon (attributes, attribute_list);
23772 return attributes;
23775 /* Parse a GNU attribute-list.
23777 attribute-list:
23778 attribute
23779 attribute-list , attribute
23781 attribute:
23782 identifier
23783 identifier ( identifier )
23784 identifier ( identifier , expression-list )
23785 identifier ( expression-list )
23787 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23788 to an attribute. The TREE_PURPOSE of each node is the identifier
23789 indicating which attribute is in use. The TREE_VALUE represents
23790 the arguments, if any. */
23792 static tree
23793 cp_parser_gnu_attribute_list (cp_parser* parser)
23795 tree attribute_list = NULL_TREE;
23796 bool save_translate_strings_p = parser->translate_strings_p;
23798 parser->translate_strings_p = false;
23799 while (true)
23801 cp_token *token;
23802 tree identifier;
23803 tree attribute;
23805 /* Look for the identifier. We also allow keywords here; for
23806 example `__attribute__ ((const))' is legal. */
23807 token = cp_lexer_peek_token (parser->lexer);
23808 if (token->type == CPP_NAME
23809 || token->type == CPP_KEYWORD)
23811 tree arguments = NULL_TREE;
23813 /* Consume the token, but save it since we need it for the
23814 SIMD enabled function parsing. */
23815 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23817 /* Save away the identifier that indicates which attribute
23818 this is. */
23819 identifier = (token->type == CPP_KEYWORD)
23820 /* For keywords, use the canonical spelling, not the
23821 parsed identifier. */
23822 ? ridpointers[(int) token->keyword]
23823 : id_token->u.value;
23825 attribute = build_tree_list (identifier, NULL_TREE);
23827 /* Peek at the next token. */
23828 token = cp_lexer_peek_token (parser->lexer);
23829 /* If it's an `(', then parse the attribute arguments. */
23830 if (token->type == CPP_OPEN_PAREN)
23832 vec<tree, va_gc> *vec;
23833 int attr_flag = (attribute_takes_identifier_p (identifier)
23834 ? id_attr : normal_attr);
23835 if (is_cilkplus_vector_p (identifier))
23837 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23838 continue;
23840 else
23841 vec = cp_parser_parenthesized_expression_list
23842 (parser, attr_flag, /*cast_p=*/false,
23843 /*allow_expansion_p=*/false,
23844 /*non_constant_p=*/NULL);
23845 if (vec == NULL)
23846 arguments = error_mark_node;
23847 else
23849 arguments = build_tree_list_vec (vec);
23850 release_tree_vector (vec);
23852 /* Save the arguments away. */
23853 TREE_VALUE (attribute) = arguments;
23855 else if (is_cilkplus_vector_p (identifier))
23857 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23858 continue;
23861 if (arguments != error_mark_node)
23863 /* Add this attribute to the list. */
23864 TREE_CHAIN (attribute) = attribute_list;
23865 attribute_list = attribute;
23868 token = cp_lexer_peek_token (parser->lexer);
23870 /* Now, look for more attributes. If the next token isn't a
23871 `,', we're done. */
23872 if (token->type != CPP_COMMA)
23873 break;
23875 /* Consume the comma and keep going. */
23876 cp_lexer_consume_token (parser->lexer);
23878 parser->translate_strings_p = save_translate_strings_p;
23880 /* We built up the list in reverse order. */
23881 return nreverse (attribute_list);
23884 /* Parse a standard C++11 attribute.
23886 The returned representation is a TREE_LIST which TREE_PURPOSE is
23887 the scoped name of the attribute, and the TREE_VALUE is its
23888 arguments list.
23890 Note that the scoped name of the attribute is itself a TREE_LIST
23891 which TREE_PURPOSE is the namespace of the attribute, and
23892 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23893 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23894 and which TREE_PURPOSE is directly the attribute name.
23896 Clients of the attribute code should use get_attribute_namespace
23897 and get_attribute_name to get the actual namespace and name of
23898 attributes, regardless of their being GNU or C++11 attributes.
23900 attribute:
23901 attribute-token attribute-argument-clause [opt]
23903 attribute-token:
23904 identifier
23905 attribute-scoped-token
23907 attribute-scoped-token:
23908 attribute-namespace :: identifier
23910 attribute-namespace:
23911 identifier
23913 attribute-argument-clause:
23914 ( balanced-token-seq )
23916 balanced-token-seq:
23917 balanced-token [opt]
23918 balanced-token-seq balanced-token
23920 balanced-token:
23921 ( balanced-token-seq )
23922 [ balanced-token-seq ]
23923 { balanced-token-seq }. */
23925 static tree
23926 cp_parser_std_attribute (cp_parser *parser)
23928 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23929 cp_token *token;
23931 /* First, parse name of the attribute, a.k.a attribute-token. */
23933 token = cp_lexer_peek_token (parser->lexer);
23934 if (token->type == CPP_NAME)
23935 attr_id = token->u.value;
23936 else if (token->type == CPP_KEYWORD)
23937 attr_id = ridpointers[(int) token->keyword];
23938 else if (token->flags & NAMED_OP)
23939 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23941 if (attr_id == NULL_TREE)
23942 return NULL_TREE;
23944 cp_lexer_consume_token (parser->lexer);
23946 token = cp_lexer_peek_token (parser->lexer);
23947 if (token->type == CPP_SCOPE)
23949 /* We are seeing a scoped attribute token. */
23951 cp_lexer_consume_token (parser->lexer);
23952 attr_ns = attr_id;
23954 token = cp_lexer_consume_token (parser->lexer);
23955 if (token->type == CPP_NAME)
23956 attr_id = token->u.value;
23957 else if (token->type == CPP_KEYWORD)
23958 attr_id = ridpointers[(int) token->keyword];
23959 else
23961 error_at (token->location,
23962 "expected an identifier for the attribute name");
23963 return error_mark_node;
23965 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23966 NULL_TREE);
23967 token = cp_lexer_peek_token (parser->lexer);
23969 else
23971 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23972 NULL_TREE);
23973 /* C++11 noreturn attribute is equivalent to GNU's. */
23974 if (is_attribute_p ("noreturn", attr_id))
23975 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23976 /* C++14 deprecated attribute is equivalent to GNU's. */
23977 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23979 if (cxx_dialect == cxx11)
23980 pedwarn (token->location, OPT_Wpedantic,
23981 "%<deprecated%> is a C++14 feature;"
23982 " use %<gnu::deprecated%>");
23983 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23985 /* Transactional Memory TS optimize_for_synchronized attribute is
23986 equivalent to GNU transaction_callable. */
23987 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23988 TREE_PURPOSE (attribute)
23989 = get_identifier ("transaction_callable");
23990 /* Transactional Memory attributes are GNU attributes. */
23991 else if (tm_attr_to_mask (attr_id))
23992 TREE_PURPOSE (attribute) = attr_id;
23995 /* Now parse the optional argument clause of the attribute. */
23997 if (token->type != CPP_OPEN_PAREN)
23998 return attribute;
24001 vec<tree, va_gc> *vec;
24002 int attr_flag = normal_attr;
24004 if (attr_ns == get_identifier ("gnu")
24005 && attribute_takes_identifier_p (attr_id))
24006 /* A GNU attribute that takes an identifier in parameter. */
24007 attr_flag = id_attr;
24009 vec = cp_parser_parenthesized_expression_list
24010 (parser, attr_flag, /*cast_p=*/false,
24011 /*allow_expansion_p=*/true,
24012 /*non_constant_p=*/NULL);
24013 if (vec == NULL)
24014 arguments = error_mark_node;
24015 else
24017 arguments = build_tree_list_vec (vec);
24018 release_tree_vector (vec);
24021 if (arguments == error_mark_node)
24022 attribute = error_mark_node;
24023 else
24024 TREE_VALUE (attribute) = arguments;
24027 return attribute;
24030 /* Check that the attribute ATTRIBUTE appears at most once in the
24031 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24032 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24033 isn't implemented yet in GCC. */
24035 static void
24036 cp_parser_check_std_attribute (tree attributes, tree attribute)
24038 if (attributes)
24040 tree name = get_attribute_name (attribute);
24041 if (is_attribute_p ("noreturn", name)
24042 && lookup_attribute ("noreturn", attributes))
24043 error ("attribute noreturn can appear at most once "
24044 "in an attribute-list");
24045 else if (is_attribute_p ("deprecated", name)
24046 && lookup_attribute ("deprecated", attributes))
24047 error ("attribute deprecated can appear at most once "
24048 "in an attribute-list");
24052 /* Parse a list of standard C++-11 attributes.
24054 attribute-list:
24055 attribute [opt]
24056 attribute-list , attribute[opt]
24057 attribute ...
24058 attribute-list , attribute ...
24061 static tree
24062 cp_parser_std_attribute_list (cp_parser *parser)
24064 tree attributes = NULL_TREE, attribute = NULL_TREE;
24065 cp_token *token = NULL;
24067 while (true)
24069 attribute = cp_parser_std_attribute (parser);
24070 if (attribute == error_mark_node)
24071 break;
24072 if (attribute != NULL_TREE)
24074 cp_parser_check_std_attribute (attributes, attribute);
24075 TREE_CHAIN (attribute) = attributes;
24076 attributes = attribute;
24078 token = cp_lexer_peek_token (parser->lexer);
24079 if (token->type == CPP_ELLIPSIS)
24081 cp_lexer_consume_token (parser->lexer);
24082 TREE_VALUE (attribute)
24083 = make_pack_expansion (TREE_VALUE (attribute));
24084 token = cp_lexer_peek_token (parser->lexer);
24086 if (token->type != CPP_COMMA)
24087 break;
24088 cp_lexer_consume_token (parser->lexer);
24090 attributes = nreverse (attributes);
24091 return attributes;
24094 /* Parse a standard C++-11 attribute specifier.
24096 attribute-specifier:
24097 [ [ attribute-list ] ]
24098 alignment-specifier
24100 alignment-specifier:
24101 alignas ( type-id ... [opt] )
24102 alignas ( alignment-expression ... [opt] ). */
24104 static tree
24105 cp_parser_std_attribute_spec (cp_parser *parser)
24107 tree attributes = NULL_TREE;
24108 cp_token *token = cp_lexer_peek_token (parser->lexer);
24110 if (token->type == CPP_OPEN_SQUARE
24111 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24113 cp_lexer_consume_token (parser->lexer);
24114 cp_lexer_consume_token (parser->lexer);
24116 attributes = cp_parser_std_attribute_list (parser);
24118 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24119 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24120 cp_parser_skip_to_end_of_statement (parser);
24121 else
24122 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24123 when we are sure that we have actually parsed them. */
24124 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24126 else
24128 tree alignas_expr;
24130 /* Look for an alignment-specifier. */
24132 token = cp_lexer_peek_token (parser->lexer);
24134 if (token->type != CPP_KEYWORD
24135 || token->keyword != RID_ALIGNAS)
24136 return NULL_TREE;
24138 cp_lexer_consume_token (parser->lexer);
24139 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24141 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24143 cp_parser_error (parser, "expected %<(%>");
24144 return error_mark_node;
24147 cp_parser_parse_tentatively (parser);
24148 alignas_expr = cp_parser_type_id (parser);
24150 if (!cp_parser_parse_definitely (parser))
24152 gcc_assert (alignas_expr == error_mark_node
24153 || alignas_expr == NULL_TREE);
24155 alignas_expr =
24156 cp_parser_assignment_expression (parser);
24157 if (alignas_expr == error_mark_node)
24158 cp_parser_skip_to_end_of_statement (parser);
24159 if (alignas_expr == NULL_TREE
24160 || alignas_expr == error_mark_node)
24161 return alignas_expr;
24164 alignas_expr = cxx_alignas_expr (alignas_expr);
24165 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24167 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24169 cp_lexer_consume_token (parser->lexer);
24170 alignas_expr = make_pack_expansion (alignas_expr);
24173 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24175 cp_parser_error (parser, "expected %<)%>");
24176 return error_mark_node;
24179 /* Build the C++-11 representation of an 'aligned'
24180 attribute. */
24181 attributes =
24182 build_tree_list (build_tree_list (get_identifier ("gnu"),
24183 get_identifier ("aligned")),
24184 alignas_expr);
24187 return attributes;
24190 /* Parse a standard C++-11 attribute-specifier-seq.
24192 attribute-specifier-seq:
24193 attribute-specifier-seq [opt] attribute-specifier
24196 static tree
24197 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24199 tree attr_specs = NULL_TREE;
24200 tree attr_last = NULL_TREE;
24202 while (true)
24204 tree attr_spec = cp_parser_std_attribute_spec (parser);
24205 if (attr_spec == NULL_TREE)
24206 break;
24207 if (attr_spec == error_mark_node)
24208 return error_mark_node;
24210 if (attr_last)
24211 TREE_CHAIN (attr_last) = attr_spec;
24212 else
24213 attr_specs = attr_last = attr_spec;
24214 attr_last = tree_last (attr_last);
24217 return attr_specs;
24220 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24221 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24222 current value of the PEDANTIC flag, regardless of whether or not
24223 the `__extension__' keyword is present. The caller is responsible
24224 for restoring the value of the PEDANTIC flag. */
24226 static bool
24227 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24229 /* Save the old value of the PEDANTIC flag. */
24230 *saved_pedantic = pedantic;
24232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24234 /* Consume the `__extension__' token. */
24235 cp_lexer_consume_token (parser->lexer);
24236 /* We're not being pedantic while the `__extension__' keyword is
24237 in effect. */
24238 pedantic = 0;
24240 return true;
24243 return false;
24246 /* Parse a label declaration.
24248 label-declaration:
24249 __label__ label-declarator-seq ;
24251 label-declarator-seq:
24252 identifier , label-declarator-seq
24253 identifier */
24255 static void
24256 cp_parser_label_declaration (cp_parser* parser)
24258 /* Look for the `__label__' keyword. */
24259 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24261 while (true)
24263 tree identifier;
24265 /* Look for an identifier. */
24266 identifier = cp_parser_identifier (parser);
24267 /* If we failed, stop. */
24268 if (identifier == error_mark_node)
24269 break;
24270 /* Declare it as a label. */
24271 finish_label_decl (identifier);
24272 /* If the next token is a `;', stop. */
24273 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24274 break;
24275 /* Look for the `,' separating the label declarations. */
24276 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24279 /* Look for the final `;'. */
24280 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24283 // -------------------------------------------------------------------------- //
24284 // Requires Clause
24286 // Parse a requires clause.
24288 // requires-clause:
24289 // 'requires' logical-or-expression
24291 // The required logical-or-expression must be a constant expression. Note
24292 // that we don't check that the expression is constepxr here. We defer until
24293 // we analyze constraints and then, we only check atomic constraints.
24294 static tree
24295 cp_parser_requires_clause (cp_parser *parser)
24297 // Parse the requires clause so that it is not automatically folded.
24298 ++processing_template_decl;
24299 tree expr = cp_parser_binary_expression (parser, false, false,
24300 PREC_NOT_OPERATOR, NULL);
24301 if (check_for_bare_parameter_packs (expr))
24302 expr = error_mark_node;
24303 --processing_template_decl;
24304 return expr;
24307 // Optionally parse a requires clause:
24308 static tree
24309 cp_parser_requires_clause_opt (cp_parser *parser)
24311 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24312 if (tok->keyword != RID_REQUIRES)
24314 if (!flag_concepts && tok->type == CPP_NAME
24315 && tok->u.value == ridpointers[RID_REQUIRES])
24317 error_at (cp_lexer_peek_token (parser->lexer)->location,
24318 "%<requires%> only available with -fconcepts");
24319 /* Parse and discard the requires-clause. */
24320 cp_lexer_consume_token (parser->lexer);
24321 cp_parser_requires_clause (parser);
24323 return NULL_TREE;
24325 cp_lexer_consume_token (parser->lexer);
24326 return cp_parser_requires_clause (parser);
24330 /*---------------------------------------------------------------------------
24331 Requires expressions
24332 ---------------------------------------------------------------------------*/
24334 /* Parse a requires expression
24336 requirement-expression:
24337 'requires' requirement-parameter-list [opt] requirement-body */
24338 static tree
24339 cp_parser_requires_expression (cp_parser *parser)
24341 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24342 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24344 /* A requires-expression shall appear only within a concept
24345 definition or a requires-clause.
24347 TODO: Implement this diagnostic correctly. */
24348 if (!processing_template_decl)
24350 error_at (loc, "a requires expression cannot appear outside a template");
24351 cp_parser_skip_to_end_of_statement (parser);
24352 return error_mark_node;
24355 tree parms, reqs;
24357 /* Local parameters are delared as variables within the scope
24358 of the expression. They are not visible past the end of
24359 the expression. Expressions within the requires-expression
24360 are unevaluated. */
24361 struct scope_sentinel
24363 scope_sentinel ()
24365 ++cp_unevaluated_operand;
24366 begin_scope (sk_block, NULL_TREE);
24369 ~scope_sentinel ()
24371 pop_bindings_and_leave_scope ();
24372 --cp_unevaluated_operand;
24374 } s;
24376 /* Parse the optional parameter list. */
24377 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24379 parms = cp_parser_requirement_parameter_list (parser);
24380 if (parms == error_mark_node)
24381 return error_mark_node;
24383 else
24384 parms = NULL_TREE;
24386 /* Parse the requirement body. */
24387 reqs = cp_parser_requirement_body (parser);
24388 if (reqs == error_mark_node)
24389 return error_mark_node;
24392 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24393 the parm chain. */
24394 grokparms (parms, &parms);
24395 return finish_requires_expr (parms, reqs);
24398 /* Parse a parameterized requirement.
24400 requirement-parameter-list:
24401 '(' parameter-declaration-clause ')' */
24402 static tree
24403 cp_parser_requirement_parameter_list (cp_parser *parser)
24405 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24406 return error_mark_node;
24408 tree parms = cp_parser_parameter_declaration_clause (parser);
24410 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24411 return error_mark_node;
24413 return parms;
24416 /* Parse the body of a requirement.
24418 requirement-body:
24419 '{' requirement-list '}' */
24420 static tree
24421 cp_parser_requirement_body (cp_parser *parser)
24423 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24424 return error_mark_node;
24426 tree reqs = cp_parser_requirement_list (parser);
24428 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24429 return error_mark_node;
24431 return reqs;
24434 /* Parse a list of requirements.
24436 requirement-list:
24437 requirement
24438 requirement-list ';' requirement[opt] */
24439 static tree
24440 cp_parser_requirement_list (cp_parser *parser)
24442 tree result = NULL_TREE;
24443 while (true)
24445 tree req = cp_parser_requirement (parser);
24446 if (req == error_mark_node)
24447 return error_mark_node;
24449 result = tree_cons (NULL_TREE, req, result);
24451 /* If we see a semi-colon, consume it. */
24452 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24453 cp_lexer_consume_token (parser->lexer);
24455 /* Stop processing at the end of the list. */
24456 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24457 break;
24460 /* Reverse the order of requirements so they are analyzed in
24461 declaration order. */
24462 return nreverse (result);
24465 /* Parse a syntactic requirement or type requirement.
24467 requirement:
24468 simple-requirement
24469 compound-requirement
24470 type-requirement
24471 nested-requirement */
24472 static tree
24473 cp_parser_requirement (cp_parser *parser)
24475 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24476 return cp_parser_compound_requirement (parser);
24477 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24478 return cp_parser_type_requirement (parser);
24479 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24480 return cp_parser_nested_requirement (parser);
24481 else
24482 return cp_parser_simple_requirement (parser);
24485 /* Parse a simple requirement.
24487 simple-requirement:
24488 expression ';' */
24489 static tree
24490 cp_parser_simple_requirement (cp_parser *parser)
24492 tree expr = cp_parser_expression (parser, NULL, false, false);
24493 if (!expr || expr == error_mark_node)
24494 return error_mark_node;
24496 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24497 return error_mark_node;
24499 return finish_simple_requirement (expr);
24502 /* Parse a type requirement
24504 type-requirement
24505 nested-name-specifier [opt] required-type-name ';'
24507 required-type-name:
24508 type-name
24509 'template' [opt] simple-template-id */
24510 static tree
24511 cp_parser_type_requirement (cp_parser *parser)
24513 cp_lexer_consume_token (parser->lexer);
24515 // Save the scope before parsing name specifiers.
24516 tree saved_scope = parser->scope;
24517 tree saved_object_scope = parser->object_scope;
24518 tree saved_qualifying_scope = parser->qualifying_scope;
24519 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24520 cp_parser_nested_name_specifier_opt (parser,
24521 /*typename_keyword_p=*/true,
24522 /*check_dependency_p=*/false,
24523 /*type_p=*/true,
24524 /*is_declaration=*/false);
24526 tree type;
24527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24529 cp_lexer_consume_token (parser->lexer);
24530 type = cp_parser_template_id (parser,
24531 /*template_keyword_p=*/true,
24532 /*check_dependency=*/false,
24533 /*tag_type=*/none_type,
24534 /*is_declaration=*/false);
24535 type = make_typename_type (parser->scope, type, typename_type,
24536 /*complain=*/tf_error);
24538 else
24539 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24541 if (TREE_CODE (type) == TYPE_DECL)
24542 type = TREE_TYPE (type);
24544 parser->scope = saved_scope;
24545 parser->object_scope = saved_object_scope;
24546 parser->qualifying_scope = saved_qualifying_scope;
24548 if (type == error_mark_node)
24549 cp_parser_skip_to_end_of_statement (parser);
24551 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24552 return error_mark_node;
24553 if (type == error_mark_node)
24554 return error_mark_node;
24556 return finish_type_requirement (type);
24559 /* Parse a compound requirement
24561 compound-requirement:
24562 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24563 static tree
24564 cp_parser_compound_requirement (cp_parser *parser)
24566 /* Parse an expression enclosed in '{ }'s. */
24567 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24568 return error_mark_node;
24570 tree expr = cp_parser_expression (parser, NULL, false, false);
24571 if (!expr || expr == error_mark_node)
24572 return error_mark_node;
24574 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24575 return error_mark_node;
24577 /* Parse the optional noexcept. */
24578 bool noexcept_p = false;
24579 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24581 cp_lexer_consume_token (parser->lexer);
24582 noexcept_p = true;
24585 /* Parse the optional trailing return type. */
24586 tree type = NULL_TREE;
24587 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24589 cp_lexer_consume_token (parser->lexer);
24590 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24591 parser->in_result_type_constraint_p = true;
24592 type = cp_parser_trailing_type_id (parser);
24593 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24594 if (type == error_mark_node)
24595 return error_mark_node;
24598 return finish_compound_requirement (expr, type, noexcept_p);
24601 /* Parse a nested requirement. This is the same as a requires clause.
24603 nested-requirement:
24604 requires-clause */
24605 static tree
24606 cp_parser_nested_requirement (cp_parser *parser)
24608 cp_lexer_consume_token (parser->lexer);
24609 tree req = cp_parser_requires_clause (parser);
24610 if (req == error_mark_node)
24611 return error_mark_node;
24612 return finish_nested_requirement (req);
24615 /* Support Functions */
24617 /* Return the appropriate prefer_type argument for lookup_name_real based on
24618 tag_type and template_mem_access. */
24620 static inline int
24621 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
24623 /* DR 141: When looking in the current enclosing context for a template-name
24624 after -> or ., only consider class templates. */
24625 if (template_mem_access)
24626 return 2;
24627 switch (tag_type)
24629 case none_type: return 0; // No preference.
24630 case scope_type: return 1; // Type or namespace.
24631 default: return 2; // Type only.
24635 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24636 NAME should have one of the representations used for an
24637 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24638 is returned. If PARSER->SCOPE is a dependent type, then a
24639 SCOPE_REF is returned.
24641 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24642 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24643 was formed. Abstractly, such entities should not be passed to this
24644 function, because they do not need to be looked up, but it is
24645 simpler to check for this special case here, rather than at the
24646 call-sites.
24648 In cases not explicitly covered above, this function returns a
24649 DECL, OVERLOAD, or baselink representing the result of the lookup.
24650 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24651 is returned.
24653 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24654 (e.g., "struct") that was used. In that case bindings that do not
24655 refer to types are ignored.
24657 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24658 ignored.
24660 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24661 are ignored.
24663 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24664 types.
24666 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24667 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24668 NULL_TREE otherwise. */
24670 static cp_expr
24671 cp_parser_lookup_name (cp_parser *parser, tree name,
24672 enum tag_types tag_type,
24673 bool is_template,
24674 bool is_namespace,
24675 bool check_dependency,
24676 tree *ambiguous_decls,
24677 location_t name_location)
24679 tree decl;
24680 tree object_type = parser->context->object_type;
24682 /* Assume that the lookup will be unambiguous. */
24683 if (ambiguous_decls)
24684 *ambiguous_decls = NULL_TREE;
24686 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24687 no longer valid. Note that if we are parsing tentatively, and
24688 the parse fails, OBJECT_TYPE will be automatically restored. */
24689 parser->context->object_type = NULL_TREE;
24691 if (name == error_mark_node)
24692 return error_mark_node;
24694 /* A template-id has already been resolved; there is no lookup to
24695 do. */
24696 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24697 return name;
24698 if (BASELINK_P (name))
24700 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24701 == TEMPLATE_ID_EXPR);
24702 return name;
24705 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24706 it should already have been checked to make sure that the name
24707 used matches the type being destroyed. */
24708 if (TREE_CODE (name) == BIT_NOT_EXPR)
24710 tree type;
24712 /* Figure out to which type this destructor applies. */
24713 if (parser->scope)
24714 type = parser->scope;
24715 else if (object_type)
24716 type = object_type;
24717 else
24718 type = current_class_type;
24719 /* If that's not a class type, there is no destructor. */
24720 if (!type || !CLASS_TYPE_P (type))
24721 return error_mark_node;
24722 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24723 lazily_declare_fn (sfk_destructor, type);
24724 if (!CLASSTYPE_DESTRUCTORS (type))
24725 return error_mark_node;
24726 /* If it was a class type, return the destructor. */
24727 return CLASSTYPE_DESTRUCTORS (type);
24730 /* By this point, the NAME should be an ordinary identifier. If
24731 the id-expression was a qualified name, the qualifying scope is
24732 stored in PARSER->SCOPE at this point. */
24733 gcc_assert (identifier_p (name));
24735 /* Perform the lookup. */
24736 if (parser->scope)
24738 bool dependent_p;
24740 if (parser->scope == error_mark_node)
24741 return error_mark_node;
24743 /* If the SCOPE is dependent, the lookup must be deferred until
24744 the template is instantiated -- unless we are explicitly
24745 looking up names in uninstantiated templates. Even then, we
24746 cannot look up the name if the scope is not a class type; it
24747 might, for example, be a template type parameter. */
24748 dependent_p = (TYPE_P (parser->scope)
24749 && dependent_scope_p (parser->scope));
24750 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24751 && dependent_p)
24752 /* Defer lookup. */
24753 decl = error_mark_node;
24754 else
24756 tree pushed_scope = NULL_TREE;
24758 /* If PARSER->SCOPE is a dependent type, then it must be a
24759 class type, and we must not be checking dependencies;
24760 otherwise, we would have processed this lookup above. So
24761 that PARSER->SCOPE is not considered a dependent base by
24762 lookup_member, we must enter the scope here. */
24763 if (dependent_p)
24764 pushed_scope = push_scope (parser->scope);
24766 /* If the PARSER->SCOPE is a template specialization, it
24767 may be instantiated during name lookup. In that case,
24768 errors may be issued. Even if we rollback the current
24769 tentative parse, those errors are valid. */
24770 decl = lookup_qualified_name (parser->scope, name,
24771 prefer_type_arg (tag_type),
24772 /*complain=*/true);
24774 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24775 lookup result and the nested-name-specifier nominates a class C:
24776 * if the name specified after the nested-name-specifier, when
24777 looked up in C, is the injected-class-name of C (Clause 9), or
24778 * if the name specified after the nested-name-specifier is the
24779 same as the identifier or the simple-template-id's template-
24780 name in the last component of the nested-name-specifier,
24781 the name is instead considered to name the constructor of
24782 class C. [ Note: for example, the constructor is not an
24783 acceptable lookup result in an elaborated-type-specifier so
24784 the constructor would not be used in place of the
24785 injected-class-name. --end note ] Such a constructor name
24786 shall be used only in the declarator-id of a declaration that
24787 names a constructor or in a using-declaration. */
24788 if (tag_type == none_type
24789 && DECL_SELF_REFERENCE_P (decl)
24790 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24791 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24792 prefer_type_arg (tag_type),
24793 /*complain=*/true);
24795 /* If we have a single function from a using decl, pull it out. */
24796 if (TREE_CODE (decl) == OVERLOAD
24797 && !really_overloaded_fn (decl))
24798 decl = OVL_FUNCTION (decl);
24800 if (pushed_scope)
24801 pop_scope (pushed_scope);
24804 /* If the scope is a dependent type and either we deferred lookup or
24805 we did lookup but didn't find the name, rememeber the name. */
24806 if (decl == error_mark_node && TYPE_P (parser->scope)
24807 && dependent_type_p (parser->scope))
24809 if (tag_type)
24811 tree type;
24813 /* The resolution to Core Issue 180 says that `struct
24814 A::B' should be considered a type-name, even if `A'
24815 is dependent. */
24816 type = make_typename_type (parser->scope, name, tag_type,
24817 /*complain=*/tf_error);
24818 if (type != error_mark_node)
24819 decl = TYPE_NAME (type);
24821 else if (is_template
24822 && (cp_parser_next_token_ends_template_argument_p (parser)
24823 || cp_lexer_next_token_is (parser->lexer,
24824 CPP_CLOSE_PAREN)))
24825 decl = make_unbound_class_template (parser->scope,
24826 name, NULL_TREE,
24827 /*complain=*/tf_error);
24828 else
24829 decl = build_qualified_name (/*type=*/NULL_TREE,
24830 parser->scope, name,
24831 is_template);
24833 parser->qualifying_scope = parser->scope;
24834 parser->object_scope = NULL_TREE;
24836 else if (object_type)
24838 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24839 OBJECT_TYPE is not a class. */
24840 if (CLASS_TYPE_P (object_type))
24841 /* If the OBJECT_TYPE is a template specialization, it may
24842 be instantiated during name lookup. In that case, errors
24843 may be issued. Even if we rollback the current tentative
24844 parse, those errors are valid. */
24845 decl = lookup_member (object_type,
24846 name,
24847 /*protect=*/0,
24848 prefer_type_arg (tag_type),
24849 tf_warning_or_error);
24850 else
24851 decl = NULL_TREE;
24853 if (!decl)
24854 /* Look it up in the enclosing context. DR 141: When looking for a
24855 template-name after -> or ., only consider class templates. */
24856 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
24857 /*nonclass=*/0,
24858 /*block_p=*/true, is_namespace, 0);
24859 if (object_type == unknown_type_node)
24860 /* The object is type-dependent, so we can't look anything up; we used
24861 this to get the DR 141 behavior. */
24862 object_type = NULL_TREE;
24863 parser->object_scope = object_type;
24864 parser->qualifying_scope = NULL_TREE;
24866 else
24868 decl = lookup_name_real (name, prefer_type_arg (tag_type),
24869 /*nonclass=*/0,
24870 /*block_p=*/true, is_namespace, 0);
24871 parser->qualifying_scope = NULL_TREE;
24872 parser->object_scope = NULL_TREE;
24875 /* If the lookup failed, let our caller know. */
24876 if (!decl || decl == error_mark_node)
24877 return error_mark_node;
24879 /* Pull out the template from an injected-class-name (or multiple). */
24880 if (is_template)
24881 decl = maybe_get_template_decl_from_type_decl (decl);
24883 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24884 if (TREE_CODE (decl) == TREE_LIST)
24886 if (ambiguous_decls)
24887 *ambiguous_decls = decl;
24888 /* The error message we have to print is too complicated for
24889 cp_parser_error, so we incorporate its actions directly. */
24890 if (!cp_parser_simulate_error (parser))
24892 error_at (name_location, "reference to %qD is ambiguous",
24893 name);
24894 print_candidates (decl);
24896 return error_mark_node;
24899 gcc_assert (DECL_P (decl)
24900 || TREE_CODE (decl) == OVERLOAD
24901 || TREE_CODE (decl) == SCOPE_REF
24902 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24903 || BASELINK_P (decl));
24905 /* If we have resolved the name of a member declaration, check to
24906 see if the declaration is accessible. When the name resolves to
24907 set of overloaded functions, accessibility is checked when
24908 overload resolution is done.
24910 During an explicit instantiation, access is not checked at all,
24911 as per [temp.explicit]. */
24912 if (DECL_P (decl))
24913 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24915 maybe_record_typedef_use (decl);
24917 return cp_expr (decl, name_location);
24920 /* Like cp_parser_lookup_name, but for use in the typical case where
24921 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24922 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24924 static tree
24925 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24927 return cp_parser_lookup_name (parser, name,
24928 none_type,
24929 /*is_template=*/false,
24930 /*is_namespace=*/false,
24931 /*check_dependency=*/true,
24932 /*ambiguous_decls=*/NULL,
24933 location);
24936 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24937 the current context, return the TYPE_DECL. If TAG_NAME_P is
24938 true, the DECL indicates the class being defined in a class-head,
24939 or declared in an elaborated-type-specifier.
24941 Otherwise, return DECL. */
24943 static tree
24944 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24946 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24947 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24949 struct A {
24950 template <typename T> struct B;
24953 template <typename T> struct A::B {};
24955 Similarly, in an elaborated-type-specifier:
24957 namespace N { struct X{}; }
24959 struct A {
24960 template <typename T> friend struct N::X;
24963 However, if the DECL refers to a class type, and we are in
24964 the scope of the class, then the name lookup automatically
24965 finds the TYPE_DECL created by build_self_reference rather
24966 than a TEMPLATE_DECL. For example, in:
24968 template <class T> struct S {
24969 S s;
24972 there is no need to handle such case. */
24974 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24975 return DECL_TEMPLATE_RESULT (decl);
24977 return decl;
24980 /* If too many, or too few, template-parameter lists apply to the
24981 declarator, issue an error message. Returns TRUE if all went well,
24982 and FALSE otherwise. */
24984 static bool
24985 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24986 cp_declarator *declarator,
24987 location_t declarator_location)
24989 switch (declarator->kind)
24991 case cdk_id:
24993 unsigned num_templates = 0;
24994 tree scope = declarator->u.id.qualifying_scope;
24996 if (scope)
24997 num_templates = num_template_headers_for_class (scope);
24998 else if (TREE_CODE (declarator->u.id.unqualified_name)
24999 == TEMPLATE_ID_EXPR)
25000 /* If the DECLARATOR has the form `X<y>' then it uses one
25001 additional level of template parameters. */
25002 ++num_templates;
25004 return cp_parser_check_template_parameters
25005 (parser, num_templates, declarator_location, declarator);
25008 case cdk_function:
25009 case cdk_array:
25010 case cdk_pointer:
25011 case cdk_reference:
25012 case cdk_ptrmem:
25013 return (cp_parser_check_declarator_template_parameters
25014 (parser, declarator->declarator, declarator_location));
25016 case cdk_error:
25017 return true;
25019 default:
25020 gcc_unreachable ();
25022 return false;
25025 /* NUM_TEMPLATES were used in the current declaration. If that is
25026 invalid, return FALSE and issue an error messages. Otherwise,
25027 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25028 declarator and we can print more accurate diagnostics. */
25030 static bool
25031 cp_parser_check_template_parameters (cp_parser* parser,
25032 unsigned num_templates,
25033 location_t location,
25034 cp_declarator *declarator)
25036 /* If there are the same number of template classes and parameter
25037 lists, that's OK. */
25038 if (parser->num_template_parameter_lists == num_templates)
25039 return true;
25040 /* If there are more, but only one more, then we are referring to a
25041 member template. That's OK too. */
25042 if (parser->num_template_parameter_lists == num_templates + 1)
25043 return true;
25044 /* If there are more template classes than parameter lists, we have
25045 something like:
25047 template <class T> void S<T>::R<T>::f (); */
25048 if (parser->num_template_parameter_lists < num_templates)
25050 if (declarator && !current_function_decl)
25051 error_at (location, "specializing member %<%T::%E%> "
25052 "requires %<template<>%> syntax",
25053 declarator->u.id.qualifying_scope,
25054 declarator->u.id.unqualified_name);
25055 else if (declarator)
25056 error_at (location, "invalid declaration of %<%T::%E%>",
25057 declarator->u.id.qualifying_scope,
25058 declarator->u.id.unqualified_name);
25059 else
25060 error_at (location, "too few template-parameter-lists");
25061 return false;
25063 /* Otherwise, there are too many template parameter lists. We have
25064 something like:
25066 template <class T> template <class U> void S::f(); */
25067 error_at (location, "too many template-parameter-lists");
25068 return false;
25071 /* Parse an optional `::' token indicating that the following name is
25072 from the global namespace. If so, PARSER->SCOPE is set to the
25073 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25074 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25075 Returns the new value of PARSER->SCOPE, if the `::' token is
25076 present, and NULL_TREE otherwise. */
25078 static tree
25079 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25081 cp_token *token;
25083 /* Peek at the next token. */
25084 token = cp_lexer_peek_token (parser->lexer);
25085 /* If we're looking at a `::' token then we're starting from the
25086 global namespace, not our current location. */
25087 if (token->type == CPP_SCOPE)
25089 /* Consume the `::' token. */
25090 cp_lexer_consume_token (parser->lexer);
25091 /* Set the SCOPE so that we know where to start the lookup. */
25092 parser->scope = global_namespace;
25093 parser->qualifying_scope = global_namespace;
25094 parser->object_scope = NULL_TREE;
25096 return parser->scope;
25098 else if (!current_scope_valid_p)
25100 parser->scope = NULL_TREE;
25101 parser->qualifying_scope = NULL_TREE;
25102 parser->object_scope = NULL_TREE;
25105 return NULL_TREE;
25108 /* Returns TRUE if the upcoming token sequence is the start of a
25109 constructor declarator. If FRIEND_P is true, the declarator is
25110 preceded by the `friend' specifier. */
25112 static bool
25113 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25115 bool constructor_p;
25116 bool outside_class_specifier_p;
25117 tree nested_name_specifier;
25118 cp_token *next_token;
25120 /* The common case is that this is not a constructor declarator, so
25121 try to avoid doing lots of work if at all possible. It's not
25122 valid declare a constructor at function scope. */
25123 if (parser->in_function_body)
25124 return false;
25125 /* And only certain tokens can begin a constructor declarator. */
25126 next_token = cp_lexer_peek_token (parser->lexer);
25127 if (next_token->type != CPP_NAME
25128 && next_token->type != CPP_SCOPE
25129 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25130 && next_token->type != CPP_TEMPLATE_ID)
25131 return false;
25133 /* Parse tentatively; we are going to roll back all of the tokens
25134 consumed here. */
25135 cp_parser_parse_tentatively (parser);
25136 /* Assume that we are looking at a constructor declarator. */
25137 constructor_p = true;
25139 /* Look for the optional `::' operator. */
25140 cp_parser_global_scope_opt (parser,
25141 /*current_scope_valid_p=*/false);
25142 /* Look for the nested-name-specifier. */
25143 nested_name_specifier
25144 = (cp_parser_nested_name_specifier_opt (parser,
25145 /*typename_keyword_p=*/false,
25146 /*check_dependency_p=*/false,
25147 /*type_p=*/false,
25148 /*is_declaration=*/false));
25150 outside_class_specifier_p = (!at_class_scope_p ()
25151 || !TYPE_BEING_DEFINED (current_class_type)
25152 || friend_p);
25154 /* Outside of a class-specifier, there must be a
25155 nested-name-specifier. */
25156 if (!nested_name_specifier && outside_class_specifier_p)
25157 constructor_p = false;
25158 else if (nested_name_specifier == error_mark_node)
25159 constructor_p = false;
25161 /* If we have a class scope, this is easy; DR 147 says that S::S always
25162 names the constructor, and no other qualified name could. */
25163 if (constructor_p && nested_name_specifier
25164 && CLASS_TYPE_P (nested_name_specifier))
25166 tree id = cp_parser_unqualified_id (parser,
25167 /*template_keyword_p=*/false,
25168 /*check_dependency_p=*/false,
25169 /*declarator_p=*/true,
25170 /*optional_p=*/false);
25171 if (is_overloaded_fn (id))
25172 id = DECL_NAME (get_first_fn (id));
25173 if (!constructor_name_p (id, nested_name_specifier))
25174 constructor_p = false;
25176 /* If we still think that this might be a constructor-declarator,
25177 look for a class-name. */
25178 else if (constructor_p)
25180 /* If we have:
25182 template <typename T> struct S {
25183 S();
25186 we must recognize that the nested `S' names a class. */
25187 tree type_decl;
25188 type_decl = cp_parser_class_name (parser,
25189 /*typename_keyword_p=*/false,
25190 /*template_keyword_p=*/false,
25191 none_type,
25192 /*check_dependency_p=*/false,
25193 /*class_head_p=*/false,
25194 /*is_declaration=*/false);
25195 /* If there was no class-name, then this is not a constructor.
25196 Otherwise, if we are in a class-specifier and we aren't
25197 handling a friend declaration, check that its type matches
25198 current_class_type (c++/38313). Note: error_mark_node
25199 is left alone for error recovery purposes. */
25200 constructor_p = (!cp_parser_error_occurred (parser)
25201 && (outside_class_specifier_p
25202 || type_decl == error_mark_node
25203 || same_type_p (current_class_type,
25204 TREE_TYPE (type_decl))));
25206 /* If we're still considering a constructor, we have to see a `(',
25207 to begin the parameter-declaration-clause, followed by either a
25208 `)', an `...', or a decl-specifier. We need to check for a
25209 type-specifier to avoid being fooled into thinking that:
25211 S (f) (int);
25213 is a constructor. (It is actually a function named `f' that
25214 takes one parameter (of type `int') and returns a value of type
25215 `S'. */
25216 if (constructor_p
25217 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25218 constructor_p = false;
25220 if (constructor_p
25221 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25222 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25223 /* A parameter declaration begins with a decl-specifier,
25224 which is either the "attribute" keyword, a storage class
25225 specifier, or (usually) a type-specifier. */
25226 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25228 tree type;
25229 tree pushed_scope = NULL_TREE;
25230 unsigned saved_num_template_parameter_lists;
25232 /* Names appearing in the type-specifier should be looked up
25233 in the scope of the class. */
25234 if (current_class_type)
25235 type = NULL_TREE;
25236 else
25238 type = TREE_TYPE (type_decl);
25239 if (TREE_CODE (type) == TYPENAME_TYPE)
25241 type = resolve_typename_type (type,
25242 /*only_current_p=*/false);
25243 if (TREE_CODE (type) == TYPENAME_TYPE)
25245 cp_parser_abort_tentative_parse (parser);
25246 return false;
25249 pushed_scope = push_scope (type);
25252 /* Inside the constructor parameter list, surrounding
25253 template-parameter-lists do not apply. */
25254 saved_num_template_parameter_lists
25255 = parser->num_template_parameter_lists;
25256 parser->num_template_parameter_lists = 0;
25258 /* Look for the type-specifier. */
25259 cp_parser_type_specifier (parser,
25260 CP_PARSER_FLAGS_NONE,
25261 /*decl_specs=*/NULL,
25262 /*is_declarator=*/true,
25263 /*declares_class_or_enum=*/NULL,
25264 /*is_cv_qualifier=*/NULL);
25266 parser->num_template_parameter_lists
25267 = saved_num_template_parameter_lists;
25269 /* Leave the scope of the class. */
25270 if (pushed_scope)
25271 pop_scope (pushed_scope);
25273 constructor_p = !cp_parser_error_occurred (parser);
25277 /* We did not really want to consume any tokens. */
25278 cp_parser_abort_tentative_parse (parser);
25280 return constructor_p;
25283 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25284 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25285 they must be performed once we are in the scope of the function.
25287 Returns the function defined. */
25289 static tree
25290 cp_parser_function_definition_from_specifiers_and_declarator
25291 (cp_parser* parser,
25292 cp_decl_specifier_seq *decl_specifiers,
25293 tree attributes,
25294 const cp_declarator *declarator)
25296 tree fn;
25297 bool success_p;
25299 /* Begin the function-definition. */
25300 success_p = start_function (decl_specifiers, declarator, attributes);
25302 /* The things we're about to see are not directly qualified by any
25303 template headers we've seen thus far. */
25304 reset_specialization ();
25306 /* If there were names looked up in the decl-specifier-seq that we
25307 did not check, check them now. We must wait until we are in the
25308 scope of the function to perform the checks, since the function
25309 might be a friend. */
25310 perform_deferred_access_checks (tf_warning_or_error);
25312 if (success_p)
25314 cp_finalize_omp_declare_simd (parser, current_function_decl);
25315 parser->omp_declare_simd = NULL;
25316 cp_finalize_oacc_routine (parser, current_function_decl, true);
25317 parser->oacc_routine = NULL;
25320 if (!success_p)
25322 /* Skip the entire function. */
25323 cp_parser_skip_to_end_of_block_or_statement (parser);
25324 fn = error_mark_node;
25326 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25328 /* Seen already, skip it. An error message has already been output. */
25329 cp_parser_skip_to_end_of_block_or_statement (parser);
25330 fn = current_function_decl;
25331 current_function_decl = NULL_TREE;
25332 /* If this is a function from a class, pop the nested class. */
25333 if (current_class_name)
25334 pop_nested_class ();
25336 else
25338 timevar_id_t tv;
25339 if (DECL_DECLARED_INLINE_P (current_function_decl))
25340 tv = TV_PARSE_INLINE;
25341 else
25342 tv = TV_PARSE_FUNC;
25343 timevar_push (tv);
25344 fn = cp_parser_function_definition_after_declarator (parser,
25345 /*inline_p=*/false);
25346 timevar_pop (tv);
25349 return fn;
25352 /* Parse the part of a function-definition that follows the
25353 declarator. INLINE_P is TRUE iff this function is an inline
25354 function defined within a class-specifier.
25356 Returns the function defined. */
25358 static tree
25359 cp_parser_function_definition_after_declarator (cp_parser* parser,
25360 bool inline_p)
25362 tree fn;
25363 bool ctor_initializer_p = false;
25364 bool saved_in_unbraced_linkage_specification_p;
25365 bool saved_in_function_body;
25366 unsigned saved_num_template_parameter_lists;
25367 cp_token *token;
25368 bool fully_implicit_function_template_p
25369 = parser->fully_implicit_function_template_p;
25370 parser->fully_implicit_function_template_p = false;
25371 tree implicit_template_parms
25372 = parser->implicit_template_parms;
25373 parser->implicit_template_parms = 0;
25374 cp_binding_level* implicit_template_scope
25375 = parser->implicit_template_scope;
25376 parser->implicit_template_scope = 0;
25378 saved_in_function_body = parser->in_function_body;
25379 parser->in_function_body = true;
25380 /* If the next token is `return', then the code may be trying to
25381 make use of the "named return value" extension that G++ used to
25382 support. */
25383 token = cp_lexer_peek_token (parser->lexer);
25384 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25386 /* Consume the `return' keyword. */
25387 cp_lexer_consume_token (parser->lexer);
25388 /* Look for the identifier that indicates what value is to be
25389 returned. */
25390 cp_parser_identifier (parser);
25391 /* Issue an error message. */
25392 error_at (token->location,
25393 "named return values are no longer supported");
25394 /* Skip tokens until we reach the start of the function body. */
25395 while (true)
25397 cp_token *token = cp_lexer_peek_token (parser->lexer);
25398 if (token->type == CPP_OPEN_BRACE
25399 || token->type == CPP_EOF
25400 || token->type == CPP_PRAGMA_EOL)
25401 break;
25402 cp_lexer_consume_token (parser->lexer);
25405 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25406 anything declared inside `f'. */
25407 saved_in_unbraced_linkage_specification_p
25408 = parser->in_unbraced_linkage_specification_p;
25409 parser->in_unbraced_linkage_specification_p = false;
25410 /* Inside the function, surrounding template-parameter-lists do not
25411 apply. */
25412 saved_num_template_parameter_lists
25413 = parser->num_template_parameter_lists;
25414 parser->num_template_parameter_lists = 0;
25416 start_lambda_scope (current_function_decl);
25418 /* If the next token is `try', `__transaction_atomic', or
25419 `__transaction_relaxed`, then we are looking at either function-try-block
25420 or function-transaction-block. Note that all of these include the
25421 function-body. */
25422 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25423 ctor_initializer_p = cp_parser_function_transaction (parser,
25424 RID_TRANSACTION_ATOMIC);
25425 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25426 RID_TRANSACTION_RELAXED))
25427 ctor_initializer_p = cp_parser_function_transaction (parser,
25428 RID_TRANSACTION_RELAXED);
25429 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25430 ctor_initializer_p = cp_parser_function_try_block (parser);
25431 else
25432 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25433 (parser, /*in_function_try_block=*/false);
25435 finish_lambda_scope ();
25437 /* Finish the function. */
25438 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25439 (inline_p ? 2 : 0));
25440 /* Generate code for it, if necessary. */
25441 expand_or_defer_fn (fn);
25442 /* Restore the saved values. */
25443 parser->in_unbraced_linkage_specification_p
25444 = saved_in_unbraced_linkage_specification_p;
25445 parser->num_template_parameter_lists
25446 = saved_num_template_parameter_lists;
25447 parser->in_function_body = saved_in_function_body;
25449 parser->fully_implicit_function_template_p
25450 = fully_implicit_function_template_p;
25451 parser->implicit_template_parms
25452 = implicit_template_parms;
25453 parser->implicit_template_scope
25454 = implicit_template_scope;
25456 if (parser->fully_implicit_function_template_p)
25457 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25459 return fn;
25462 /* Parse a template-declaration body (following argument list). */
25464 static void
25465 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25466 tree parameter_list,
25467 bool member_p)
25469 tree decl = NULL_TREE;
25470 bool friend_p = false;
25472 /* We just processed one more parameter list. */
25473 ++parser->num_template_parameter_lists;
25475 /* Get the deferred access checks from the parameter list. These
25476 will be checked once we know what is being declared, as for a
25477 member template the checks must be performed in the scope of the
25478 class containing the member. */
25479 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25481 /* Tentatively parse for a new template parameter list, which can either be
25482 the template keyword or a template introduction. */
25483 if (cp_parser_template_declaration_after_export (parser, member_p))
25484 /* OK */;
25485 else if (cxx_dialect >= cxx11
25486 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25487 decl = cp_parser_alias_declaration (parser);
25488 else
25490 /* There are no access checks when parsing a template, as we do not
25491 know if a specialization will be a friend. */
25492 push_deferring_access_checks (dk_no_check);
25493 cp_token *token = cp_lexer_peek_token (parser->lexer);
25494 decl = cp_parser_single_declaration (parser,
25495 checks,
25496 member_p,
25497 /*explicit_specialization_p=*/false,
25498 &friend_p);
25499 pop_deferring_access_checks ();
25501 /* If this is a member template declaration, let the front
25502 end know. */
25503 if (member_p && !friend_p && decl)
25505 if (TREE_CODE (decl) == TYPE_DECL)
25506 cp_parser_check_access_in_redeclaration (decl, token->location);
25508 decl = finish_member_template_decl (decl);
25510 else if (friend_p && decl
25511 && DECL_DECLARES_TYPE_P (decl))
25512 make_friend_class (current_class_type, TREE_TYPE (decl),
25513 /*complain=*/true);
25515 /* We are done with the current parameter list. */
25516 --parser->num_template_parameter_lists;
25518 pop_deferring_access_checks ();
25520 /* Finish up. */
25521 finish_template_decl (parameter_list);
25523 /* Check the template arguments for a literal operator template. */
25524 if (decl
25525 && DECL_DECLARES_FUNCTION_P (decl)
25526 && UDLIT_OPER_P (DECL_NAME (decl)))
25528 bool ok = true;
25529 if (parameter_list == NULL_TREE)
25530 ok = false;
25531 else
25533 int num_parms = TREE_VEC_LENGTH (parameter_list);
25534 if (num_parms == 1)
25536 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25537 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25538 if (TREE_TYPE (parm) != char_type_node
25539 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25540 ok = false;
25542 else if (num_parms == 2 && cxx_dialect >= cxx14)
25544 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25545 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25546 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25547 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25548 if (TREE_TYPE (parm) != TREE_TYPE (type)
25549 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25550 ok = false;
25552 else
25553 ok = false;
25555 if (!ok)
25557 if (cxx_dialect >= cxx14)
25558 error ("literal operator template %qD has invalid parameter list."
25559 " Expected non-type template argument pack <char...>"
25560 " or <typename CharT, CharT...>",
25561 decl);
25562 else
25563 error ("literal operator template %qD has invalid parameter list."
25564 " Expected non-type template argument pack <char...>",
25565 decl);
25569 /* Register member declarations. */
25570 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25571 finish_member_declaration (decl);
25572 /* If DECL is a function template, we must return to parse it later.
25573 (Even though there is no definition, there might be default
25574 arguments that need handling.) */
25575 if (member_p && decl
25576 && DECL_DECLARES_FUNCTION_P (decl))
25577 vec_safe_push (unparsed_funs_with_definitions, decl);
25580 /* Parse a template introduction header for a template-declaration. Returns
25581 false if tentative parse fails. */
25583 static bool
25584 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25586 cp_parser_parse_tentatively (parser);
25588 tree saved_scope = parser->scope;
25589 tree saved_object_scope = parser->object_scope;
25590 tree saved_qualifying_scope = parser->qualifying_scope;
25592 /* Look for the optional `::' operator. */
25593 cp_parser_global_scope_opt (parser,
25594 /*current_scope_valid_p=*/false);
25595 /* Look for the nested-name-specifier. */
25596 cp_parser_nested_name_specifier_opt (parser,
25597 /*typename_keyword_p=*/false,
25598 /*check_dependency_p=*/true,
25599 /*type_p=*/false,
25600 /*is_declaration=*/false);
25602 cp_token *token = cp_lexer_peek_token (parser->lexer);
25603 tree concept_name = cp_parser_identifier (parser);
25605 /* Look up the concept for which we will be matching
25606 template parameters. */
25607 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25608 token->location);
25609 parser->scope = saved_scope;
25610 parser->object_scope = saved_object_scope;
25611 parser->qualifying_scope = saved_qualifying_scope;
25613 if (concept_name == error_mark_node)
25614 cp_parser_simulate_error (parser);
25616 /* Look for opening brace for introduction. */
25617 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25619 if (!cp_parser_parse_definitely (parser))
25620 return false;
25622 push_deferring_access_checks (dk_deferred);
25624 /* Build vector of placeholder parameters and grab
25625 matching identifiers. */
25626 tree introduction_list = cp_parser_introduction_list (parser);
25628 /* The introduction-list shall not be empty. */
25629 int nargs = TREE_VEC_LENGTH (introduction_list);
25630 if (nargs == 0)
25632 error ("empty introduction-list");
25633 return true;
25636 /* Look for closing brace for introduction. */
25637 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25638 return true;
25640 if (tmpl_decl == error_mark_node)
25642 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25643 token->location);
25644 return true;
25647 /* Build and associate the constraint. */
25648 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25649 if (parms && parms != error_mark_node)
25651 cp_parser_template_declaration_after_parameters (parser, parms,
25652 member_p);
25653 return true;
25656 error_at (token->location, "no matching concept for template-introduction");
25657 return true;
25660 /* Parse a normal template-declaration following the template keyword. */
25662 static void
25663 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25665 tree parameter_list;
25666 bool need_lang_pop;
25667 location_t location = input_location;
25669 /* Look for the `<' token. */
25670 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25671 return;
25672 if (at_class_scope_p () && current_function_decl)
25674 /* 14.5.2.2 [temp.mem]
25676 A local class shall not have member templates. */
25677 error_at (location,
25678 "invalid declaration of member template in local class");
25679 cp_parser_skip_to_end_of_block_or_statement (parser);
25680 return;
25682 /* [temp]
25684 A template ... shall not have C linkage. */
25685 if (current_lang_name == lang_name_c)
25687 error_at (location, "template with C linkage");
25688 /* Give it C++ linkage to avoid confusing other parts of the
25689 front end. */
25690 push_lang_context (lang_name_cplusplus);
25691 need_lang_pop = true;
25693 else
25694 need_lang_pop = false;
25696 /* We cannot perform access checks on the template parameter
25697 declarations until we know what is being declared, just as we
25698 cannot check the decl-specifier list. */
25699 push_deferring_access_checks (dk_deferred);
25701 /* If the next token is `>', then we have an invalid
25702 specialization. Rather than complain about an invalid template
25703 parameter, issue an error message here. */
25704 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25706 cp_parser_error (parser, "invalid explicit specialization");
25707 begin_specialization ();
25708 parameter_list = NULL_TREE;
25710 else
25712 /* Parse the template parameters. */
25713 parameter_list = cp_parser_template_parameter_list (parser);
25716 /* Look for the `>'. */
25717 cp_parser_skip_to_end_of_template_parameter_list (parser);
25719 /* Manage template requirements */
25720 tree reqs = get_shorthand_constraints (current_template_parms);
25721 if (tree r = cp_parser_requires_clause_opt (parser))
25722 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25723 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25725 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25726 member_p);
25728 /* For the erroneous case of a template with C linkage, we pushed an
25729 implicit C++ linkage scope; exit that scope now. */
25730 if (need_lang_pop)
25731 pop_lang_context ();
25734 /* Parse a template-declaration, assuming that the `export' (and
25735 `extern') keywords, if present, has already been scanned. MEMBER_P
25736 is as for cp_parser_template_declaration. */
25738 static bool
25739 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25741 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25743 cp_lexer_consume_token (parser->lexer);
25744 cp_parser_explicit_template_declaration (parser, member_p);
25745 return true;
25747 else if (flag_concepts)
25748 return cp_parser_template_introduction (parser, member_p);
25750 return false;
25753 /* Perform the deferred access checks from a template-parameter-list.
25754 CHECKS is a TREE_LIST of access checks, as returned by
25755 get_deferred_access_checks. */
25757 static void
25758 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25760 ++processing_template_parmlist;
25761 perform_access_checks (checks, tf_warning_or_error);
25762 --processing_template_parmlist;
25765 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25766 `function-definition' sequence that follows a template header.
25767 If MEMBER_P is true, this declaration appears in a class scope.
25769 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25770 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25772 static tree
25773 cp_parser_single_declaration (cp_parser* parser,
25774 vec<deferred_access_check, va_gc> *checks,
25775 bool member_p,
25776 bool explicit_specialization_p,
25777 bool* friend_p)
25779 int declares_class_or_enum;
25780 tree decl = NULL_TREE;
25781 cp_decl_specifier_seq decl_specifiers;
25782 bool function_definition_p = false;
25783 cp_token *decl_spec_token_start;
25785 /* This function is only used when processing a template
25786 declaration. */
25787 gcc_assert (innermost_scope_kind () == sk_template_parms
25788 || innermost_scope_kind () == sk_template_spec);
25790 /* Defer access checks until we know what is being declared. */
25791 push_deferring_access_checks (dk_deferred);
25793 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25794 alternative. */
25795 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25796 cp_parser_decl_specifier_seq (parser,
25797 CP_PARSER_FLAGS_OPTIONAL,
25798 &decl_specifiers,
25799 &declares_class_or_enum);
25800 if (friend_p)
25801 *friend_p = cp_parser_friend_p (&decl_specifiers);
25803 /* There are no template typedefs. */
25804 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25806 error_at (decl_spec_token_start->location,
25807 "template declaration of %<typedef%>");
25808 decl = error_mark_node;
25811 /* Gather up the access checks that occurred the
25812 decl-specifier-seq. */
25813 stop_deferring_access_checks ();
25815 /* Check for the declaration of a template class. */
25816 if (declares_class_or_enum)
25818 if (cp_parser_declares_only_class_p (parser)
25819 || (declares_class_or_enum & 2))
25821 // If this is a declaration, but not a definition, associate
25822 // any constraints with the type declaration. Constraints
25823 // are associated with definitions in cp_parser_class_specifier.
25824 if (declares_class_or_enum == 1)
25825 associate_classtype_constraints (decl_specifiers.type);
25827 decl = shadow_tag (&decl_specifiers);
25829 /* In this case:
25831 struct C {
25832 friend template <typename T> struct A<T>::B;
25835 A<T>::B will be represented by a TYPENAME_TYPE, and
25836 therefore not recognized by shadow_tag. */
25837 if (friend_p && *friend_p
25838 && !decl
25839 && decl_specifiers.type
25840 && TYPE_P (decl_specifiers.type))
25841 decl = decl_specifiers.type;
25843 if (decl && decl != error_mark_node)
25844 decl = TYPE_NAME (decl);
25845 else
25846 decl = error_mark_node;
25848 /* Perform access checks for template parameters. */
25849 cp_parser_perform_template_parameter_access_checks (checks);
25851 /* Give a helpful diagnostic for
25852 template <class T> struct A { } a;
25853 if we aren't already recovering from an error. */
25854 if (!cp_parser_declares_only_class_p (parser)
25855 && !seen_error ())
25857 error_at (cp_lexer_peek_token (parser->lexer)->location,
25858 "a class template declaration must not declare "
25859 "anything else");
25860 cp_parser_skip_to_end_of_block_or_statement (parser);
25861 goto out;
25866 /* Complain about missing 'typename' or other invalid type names. */
25867 if (!decl_specifiers.any_type_specifiers_p
25868 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25870 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25871 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25872 the rest of this declaration. */
25873 decl = error_mark_node;
25874 goto out;
25877 /* If it's not a template class, try for a template function. If
25878 the next token is a `;', then this declaration does not declare
25879 anything. But, if there were errors in the decl-specifiers, then
25880 the error might well have come from an attempted class-specifier.
25881 In that case, there's no need to warn about a missing declarator. */
25882 if (!decl
25883 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25884 || decl_specifiers.type != error_mark_node))
25886 decl = cp_parser_init_declarator (parser,
25887 &decl_specifiers,
25888 checks,
25889 /*function_definition_allowed_p=*/true,
25890 member_p,
25891 declares_class_or_enum,
25892 &function_definition_p,
25893 NULL, NULL, NULL);
25895 /* 7.1.1-1 [dcl.stc]
25897 A storage-class-specifier shall not be specified in an explicit
25898 specialization... */
25899 if (decl
25900 && explicit_specialization_p
25901 && decl_specifiers.storage_class != sc_none)
25903 error_at (decl_spec_token_start->location,
25904 "explicit template specialization cannot have a storage class");
25905 decl = error_mark_node;
25908 if (decl && VAR_P (decl))
25909 check_template_variable (decl);
25912 /* Look for a trailing `;' after the declaration. */
25913 if (!function_definition_p
25914 && (decl == error_mark_node
25915 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25916 cp_parser_skip_to_end_of_block_or_statement (parser);
25918 out:
25919 pop_deferring_access_checks ();
25921 /* Clear any current qualification; whatever comes next is the start
25922 of something new. */
25923 parser->scope = NULL_TREE;
25924 parser->qualifying_scope = NULL_TREE;
25925 parser->object_scope = NULL_TREE;
25927 return decl;
25930 /* Parse a cast-expression that is not the operand of a unary "&". */
25932 static cp_expr
25933 cp_parser_simple_cast_expression (cp_parser *parser)
25935 return cp_parser_cast_expression (parser, /*address_p=*/false,
25936 /*cast_p=*/false, /*decltype*/false, NULL);
25939 /* Parse a functional cast to TYPE. Returns an expression
25940 representing the cast. */
25942 static cp_expr
25943 cp_parser_functional_cast (cp_parser* parser, tree type)
25945 vec<tree, va_gc> *vec;
25946 tree expression_list;
25947 cp_expr cast;
25948 bool nonconst_p;
25950 location_t start_loc = input_location;
25952 if (!type)
25953 type = error_mark_node;
25955 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25957 cp_lexer_set_source_position (parser->lexer);
25958 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25959 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25960 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25961 if (TREE_CODE (type) == TYPE_DECL)
25962 type = TREE_TYPE (type);
25964 cast = finish_compound_literal (type, expression_list,
25965 tf_warning_or_error);
25966 /* Create a location of the form:
25967 type_name{i, f}
25968 ^~~~~~~~~~~~~~~
25969 with caret == start at the start of the type name,
25970 finishing at the closing brace. */
25971 location_t finish_loc
25972 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25973 location_t combined_loc = make_location (start_loc, start_loc,
25974 finish_loc);
25975 cast.set_location (combined_loc);
25976 return cast;
25980 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25981 /*cast_p=*/true,
25982 /*allow_expansion_p=*/true,
25983 /*non_constant_p=*/NULL);
25984 if (vec == NULL)
25985 expression_list = error_mark_node;
25986 else
25988 expression_list = build_tree_list_vec (vec);
25989 release_tree_vector (vec);
25992 cast = build_functional_cast (type, expression_list,
25993 tf_warning_or_error);
25994 /* [expr.const]/1: In an integral constant expression "only type
25995 conversions to integral or enumeration type can be used". */
25996 if (TREE_CODE (type) == TYPE_DECL)
25997 type = TREE_TYPE (type);
25998 if (cast != error_mark_node
25999 && !cast_valid_in_integral_constant_expression_p (type)
26000 && cp_parser_non_integral_constant_expression (parser,
26001 NIC_CONSTRUCTOR))
26002 return error_mark_node;
26004 /* Create a location of the form:
26005 float(i)
26006 ^~~~~~~~
26007 with caret == start at the start of the type name,
26008 finishing at the closing paren. */
26009 location_t finish_loc
26010 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26011 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26012 cast.set_location (combined_loc);
26013 return cast;
26016 /* Save the tokens that make up the body of a member function defined
26017 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26018 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26019 specifiers applied to the declaration. Returns the FUNCTION_DECL
26020 for the member function. */
26022 static tree
26023 cp_parser_save_member_function_body (cp_parser* parser,
26024 cp_decl_specifier_seq *decl_specifiers,
26025 cp_declarator *declarator,
26026 tree attributes)
26028 cp_token *first;
26029 cp_token *last;
26030 tree fn;
26032 /* Create the FUNCTION_DECL. */
26033 fn = grokmethod (decl_specifiers, declarator, attributes);
26034 cp_finalize_omp_declare_simd (parser, fn);
26035 cp_finalize_oacc_routine (parser, fn, true);
26036 /* If something went badly wrong, bail out now. */
26037 if (fn == error_mark_node)
26039 /* If there's a function-body, skip it. */
26040 if (cp_parser_token_starts_function_definition_p
26041 (cp_lexer_peek_token (parser->lexer)))
26042 cp_parser_skip_to_end_of_block_or_statement (parser);
26043 return error_mark_node;
26046 /* Remember it, if there default args to post process. */
26047 cp_parser_save_default_args (parser, fn);
26049 /* Save away the tokens that make up the body of the
26050 function. */
26051 first = parser->lexer->next_token;
26052 /* Handle function try blocks. */
26053 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26054 cp_lexer_consume_token (parser->lexer);
26055 /* We can have braced-init-list mem-initializers before the fn body. */
26056 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26058 cp_lexer_consume_token (parser->lexer);
26059 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26061 /* cache_group will stop after an un-nested { } pair, too. */
26062 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26063 break;
26065 /* variadic mem-inits have ... after the ')'. */
26066 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26067 cp_lexer_consume_token (parser->lexer);
26070 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26071 /* Handle function try blocks. */
26072 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26073 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26074 last = parser->lexer->next_token;
26076 /* Save away the inline definition; we will process it when the
26077 class is complete. */
26078 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26079 DECL_PENDING_INLINE_P (fn) = 1;
26081 /* We need to know that this was defined in the class, so that
26082 friend templates are handled correctly. */
26083 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26085 /* Add FN to the queue of functions to be parsed later. */
26086 vec_safe_push (unparsed_funs_with_definitions, fn);
26088 return fn;
26091 /* Save the tokens that make up the in-class initializer for a non-static
26092 data member. Returns a DEFAULT_ARG. */
26094 static tree
26095 cp_parser_save_nsdmi (cp_parser* parser)
26097 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26100 /* Parse a template-argument-list, as well as the trailing ">" (but
26101 not the opening "<"). See cp_parser_template_argument_list for the
26102 return value. */
26104 static tree
26105 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26107 tree arguments;
26108 tree saved_scope;
26109 tree saved_qualifying_scope;
26110 tree saved_object_scope;
26111 bool saved_greater_than_is_operator_p;
26112 int saved_unevaluated_operand;
26113 int saved_inhibit_evaluation_warnings;
26115 /* [temp.names]
26117 When parsing a template-id, the first non-nested `>' is taken as
26118 the end of the template-argument-list rather than a greater-than
26119 operator. */
26120 saved_greater_than_is_operator_p
26121 = parser->greater_than_is_operator_p;
26122 parser->greater_than_is_operator_p = false;
26123 /* Parsing the argument list may modify SCOPE, so we save it
26124 here. */
26125 saved_scope = parser->scope;
26126 saved_qualifying_scope = parser->qualifying_scope;
26127 saved_object_scope = parser->object_scope;
26128 /* We need to evaluate the template arguments, even though this
26129 template-id may be nested within a "sizeof". */
26130 saved_unevaluated_operand = cp_unevaluated_operand;
26131 cp_unevaluated_operand = 0;
26132 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26133 c_inhibit_evaluation_warnings = 0;
26134 /* Parse the template-argument-list itself. */
26135 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26136 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26137 arguments = NULL_TREE;
26138 else
26139 arguments = cp_parser_template_argument_list (parser);
26140 /* Look for the `>' that ends the template-argument-list. If we find
26141 a '>>' instead, it's probably just a typo. */
26142 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26144 if (cxx_dialect != cxx98)
26146 /* In C++0x, a `>>' in a template argument list or cast
26147 expression is considered to be two separate `>'
26148 tokens. So, change the current token to a `>', but don't
26149 consume it: it will be consumed later when the outer
26150 template argument list (or cast expression) is parsed.
26151 Note that this replacement of `>' for `>>' is necessary
26152 even if we are parsing tentatively: in the tentative
26153 case, after calling
26154 cp_parser_enclosed_template_argument_list we will always
26155 throw away all of the template arguments and the first
26156 closing `>', either because the template argument list
26157 was erroneous or because we are replacing those tokens
26158 with a CPP_TEMPLATE_ID token. The second `>' (which will
26159 not have been thrown away) is needed either to close an
26160 outer template argument list or to complete a new-style
26161 cast. */
26162 cp_token *token = cp_lexer_peek_token (parser->lexer);
26163 token->type = CPP_GREATER;
26165 else if (!saved_greater_than_is_operator_p)
26167 /* If we're in a nested template argument list, the '>>' has
26168 to be a typo for '> >'. We emit the error message, but we
26169 continue parsing and we push a '>' as next token, so that
26170 the argument list will be parsed correctly. Note that the
26171 global source location is still on the token before the
26172 '>>', so we need to say explicitly where we want it. */
26173 cp_token *token = cp_lexer_peek_token (parser->lexer);
26174 error_at (token->location, "%<>>%> should be %<> >%> "
26175 "within a nested template argument list");
26177 token->type = CPP_GREATER;
26179 else
26181 /* If this is not a nested template argument list, the '>>'
26182 is a typo for '>'. Emit an error message and continue.
26183 Same deal about the token location, but here we can get it
26184 right by consuming the '>>' before issuing the diagnostic. */
26185 cp_token *token = cp_lexer_consume_token (parser->lexer);
26186 error_at (token->location,
26187 "spurious %<>>%>, use %<>%> to terminate "
26188 "a template argument list");
26191 else
26192 cp_parser_skip_to_end_of_template_parameter_list (parser);
26193 /* The `>' token might be a greater-than operator again now. */
26194 parser->greater_than_is_operator_p
26195 = saved_greater_than_is_operator_p;
26196 /* Restore the SAVED_SCOPE. */
26197 parser->scope = saved_scope;
26198 parser->qualifying_scope = saved_qualifying_scope;
26199 parser->object_scope = saved_object_scope;
26200 cp_unevaluated_operand = saved_unevaluated_operand;
26201 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26203 return arguments;
26206 /* MEMBER_FUNCTION is a member function, or a friend. If default
26207 arguments, or the body of the function have not yet been parsed,
26208 parse them now. */
26210 static void
26211 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26213 timevar_push (TV_PARSE_INMETH);
26214 /* If this member is a template, get the underlying
26215 FUNCTION_DECL. */
26216 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26217 member_function = DECL_TEMPLATE_RESULT (member_function);
26219 /* There should not be any class definitions in progress at this
26220 point; the bodies of members are only parsed outside of all class
26221 definitions. */
26222 gcc_assert (parser->num_classes_being_defined == 0);
26223 /* While we're parsing the member functions we might encounter more
26224 classes. We want to handle them right away, but we don't want
26225 them getting mixed up with functions that are currently in the
26226 queue. */
26227 push_unparsed_function_queues (parser);
26229 /* Make sure that any template parameters are in scope. */
26230 maybe_begin_member_template_processing (member_function);
26232 /* If the body of the function has not yet been parsed, parse it
26233 now. */
26234 if (DECL_PENDING_INLINE_P (member_function))
26236 tree function_scope;
26237 cp_token_cache *tokens;
26239 /* The function is no longer pending; we are processing it. */
26240 tokens = DECL_PENDING_INLINE_INFO (member_function);
26241 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26242 DECL_PENDING_INLINE_P (member_function) = 0;
26244 /* If this is a local class, enter the scope of the containing
26245 function. */
26246 function_scope = current_function_decl;
26247 if (function_scope)
26248 push_function_context ();
26250 /* Push the body of the function onto the lexer stack. */
26251 cp_parser_push_lexer_for_tokens (parser, tokens);
26253 /* Let the front end know that we going to be defining this
26254 function. */
26255 start_preparsed_function (member_function, NULL_TREE,
26256 SF_PRE_PARSED | SF_INCLASS_INLINE);
26258 /* Don't do access checking if it is a templated function. */
26259 if (processing_template_decl)
26260 push_deferring_access_checks (dk_no_check);
26262 /* #pragma omp declare reduction needs special parsing. */
26263 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26265 parser->lexer->in_pragma = true;
26266 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26267 finish_function (/*inline*/2);
26268 cp_check_omp_declare_reduction (member_function);
26270 else
26271 /* Now, parse the body of the function. */
26272 cp_parser_function_definition_after_declarator (parser,
26273 /*inline_p=*/true);
26275 if (processing_template_decl)
26276 pop_deferring_access_checks ();
26278 /* Leave the scope of the containing function. */
26279 if (function_scope)
26280 pop_function_context ();
26281 cp_parser_pop_lexer (parser);
26284 /* Remove any template parameters from the symbol table. */
26285 maybe_end_member_template_processing ();
26287 /* Restore the queue. */
26288 pop_unparsed_function_queues (parser);
26289 timevar_pop (TV_PARSE_INMETH);
26292 /* If DECL contains any default args, remember it on the unparsed
26293 functions queue. */
26295 static void
26296 cp_parser_save_default_args (cp_parser* parser, tree decl)
26298 tree probe;
26300 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26301 probe;
26302 probe = TREE_CHAIN (probe))
26303 if (TREE_PURPOSE (probe))
26305 cp_default_arg_entry entry = {current_class_type, decl};
26306 vec_safe_push (unparsed_funs_with_default_args, entry);
26307 break;
26311 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26312 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26313 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26314 from the parameter-type-list. */
26316 static tree
26317 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26318 tree default_arg, tree parmtype)
26320 cp_token_cache *tokens;
26321 tree parsed_arg;
26322 bool dummy;
26324 if (default_arg == error_mark_node)
26325 return error_mark_node;
26327 /* Push the saved tokens for the default argument onto the parser's
26328 lexer stack. */
26329 tokens = DEFARG_TOKENS (default_arg);
26330 cp_parser_push_lexer_for_tokens (parser, tokens);
26332 start_lambda_scope (decl);
26334 /* Parse the default argument. */
26335 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26336 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26339 finish_lambda_scope ();
26341 if (parsed_arg == error_mark_node)
26342 cp_parser_skip_to_end_of_statement (parser);
26344 if (!processing_template_decl)
26346 /* In a non-template class, check conversions now. In a template,
26347 we'll wait and instantiate these as needed. */
26348 if (TREE_CODE (decl) == PARM_DECL)
26349 parsed_arg = check_default_argument (parmtype, parsed_arg,
26350 tf_warning_or_error);
26351 else
26352 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26355 /* If the token stream has not been completely used up, then
26356 there was extra junk after the end of the default
26357 argument. */
26358 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26360 if (TREE_CODE (decl) == PARM_DECL)
26361 cp_parser_error (parser, "expected %<,%>");
26362 else
26363 cp_parser_error (parser, "expected %<;%>");
26366 /* Revert to the main lexer. */
26367 cp_parser_pop_lexer (parser);
26369 return parsed_arg;
26372 /* FIELD is a non-static data member with an initializer which we saved for
26373 later; parse it now. */
26375 static void
26376 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26378 tree def;
26380 maybe_begin_member_template_processing (field);
26382 push_unparsed_function_queues (parser);
26383 def = cp_parser_late_parse_one_default_arg (parser, field,
26384 DECL_INITIAL (field),
26385 NULL_TREE);
26386 pop_unparsed_function_queues (parser);
26388 maybe_end_member_template_processing ();
26390 DECL_INITIAL (field) = def;
26393 /* FN is a FUNCTION_DECL which may contains a parameter with an
26394 unparsed DEFAULT_ARG. Parse the default args now. This function
26395 assumes that the current scope is the scope in which the default
26396 argument should be processed. */
26398 static void
26399 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26401 bool saved_local_variables_forbidden_p;
26402 tree parm, parmdecl;
26404 /* While we're parsing the default args, we might (due to the
26405 statement expression extension) encounter more classes. We want
26406 to handle them right away, but we don't want them getting mixed
26407 up with default args that are currently in the queue. */
26408 push_unparsed_function_queues (parser);
26410 /* Local variable names (and the `this' keyword) may not appear
26411 in a default argument. */
26412 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26413 parser->local_variables_forbidden_p = true;
26415 push_defarg_context (fn);
26417 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26418 parmdecl = DECL_ARGUMENTS (fn);
26419 parm && parm != void_list_node;
26420 parm = TREE_CHAIN (parm),
26421 parmdecl = DECL_CHAIN (parmdecl))
26423 tree default_arg = TREE_PURPOSE (parm);
26424 tree parsed_arg;
26425 vec<tree, va_gc> *insts;
26426 tree copy;
26427 unsigned ix;
26429 if (!default_arg)
26430 continue;
26432 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26433 /* This can happen for a friend declaration for a function
26434 already declared with default arguments. */
26435 continue;
26437 parsed_arg
26438 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26439 default_arg,
26440 TREE_VALUE (parm));
26441 if (parsed_arg == error_mark_node)
26443 continue;
26446 TREE_PURPOSE (parm) = parsed_arg;
26448 /* Update any instantiations we've already created. */
26449 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26450 vec_safe_iterate (insts, ix, &copy); ix++)
26451 TREE_PURPOSE (copy) = parsed_arg;
26454 pop_defarg_context ();
26456 /* Make sure no default arg is missing. */
26457 check_default_args (fn);
26459 /* Restore the state of local_variables_forbidden_p. */
26460 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26462 /* Restore the queue. */
26463 pop_unparsed_function_queues (parser);
26466 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26468 sizeof ... ( identifier )
26470 where the 'sizeof' token has already been consumed. */
26472 static tree
26473 cp_parser_sizeof_pack (cp_parser *parser)
26475 /* Consume the `...'. */
26476 cp_lexer_consume_token (parser->lexer);
26477 maybe_warn_variadic_templates ();
26479 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26480 if (paren)
26481 cp_lexer_consume_token (parser->lexer);
26482 else
26483 permerror (cp_lexer_peek_token (parser->lexer)->location,
26484 "%<sizeof...%> argument must be surrounded by parentheses");
26486 cp_token *token = cp_lexer_peek_token (parser->lexer);
26487 tree name = cp_parser_identifier (parser);
26488 if (name == error_mark_node)
26489 return error_mark_node;
26490 /* The name is not qualified. */
26491 parser->scope = NULL_TREE;
26492 parser->qualifying_scope = NULL_TREE;
26493 parser->object_scope = NULL_TREE;
26494 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26495 if (expr == error_mark_node)
26496 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26497 token->location);
26498 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26499 expr = TREE_TYPE (expr);
26500 else if (TREE_CODE (expr) == CONST_DECL)
26501 expr = DECL_INITIAL (expr);
26502 expr = make_pack_expansion (expr);
26503 PACK_EXPANSION_SIZEOF_P (expr) = true;
26505 if (paren)
26506 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26508 return expr;
26511 /* Parse the operand of `sizeof' (or a similar operator). Returns
26512 either a TYPE or an expression, depending on the form of the
26513 input. The KEYWORD indicates which kind of expression we have
26514 encountered. */
26516 static tree
26517 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26519 tree expr = NULL_TREE;
26520 const char *saved_message;
26521 char *tmp;
26522 bool saved_integral_constant_expression_p;
26523 bool saved_non_integral_constant_expression_p;
26525 /* If it's a `...', then we are computing the length of a parameter
26526 pack. */
26527 if (keyword == RID_SIZEOF
26528 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26529 return cp_parser_sizeof_pack (parser);
26531 /* Types cannot be defined in a `sizeof' expression. Save away the
26532 old message. */
26533 saved_message = parser->type_definition_forbidden_message;
26534 /* And create the new one. */
26535 tmp = concat ("types may not be defined in %<",
26536 IDENTIFIER_POINTER (ridpointers[keyword]),
26537 "%> expressions", NULL);
26538 parser->type_definition_forbidden_message = tmp;
26540 /* The restrictions on constant-expressions do not apply inside
26541 sizeof expressions. */
26542 saved_integral_constant_expression_p
26543 = parser->integral_constant_expression_p;
26544 saved_non_integral_constant_expression_p
26545 = parser->non_integral_constant_expression_p;
26546 parser->integral_constant_expression_p = false;
26548 /* Do not actually evaluate the expression. */
26549 ++cp_unevaluated_operand;
26550 ++c_inhibit_evaluation_warnings;
26551 /* If it's a `(', then we might be looking at the type-id
26552 construction. */
26553 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26555 tree type = NULL_TREE;
26557 /* We can't be sure yet whether we're looking at a type-id or an
26558 expression. */
26559 cp_parser_parse_tentatively (parser);
26560 /* Note: as a GNU Extension, compound literals are considered
26561 postfix-expressions as they are in C99, so they are valid
26562 arguments to sizeof. See comment in cp_parser_cast_expression
26563 for details. */
26564 if (cp_parser_compound_literal_p (parser))
26565 cp_parser_simulate_error (parser);
26566 else
26568 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26569 parser->in_type_id_in_expr_p = true;
26570 /* Look for the type-id. */
26571 type = cp_parser_type_id (parser);
26572 /* Look for the closing `)'. */
26573 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26574 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26577 /* If all went well, then we're done. */
26578 if (cp_parser_parse_definitely (parser))
26580 cp_decl_specifier_seq decl_specs;
26582 /* Build a trivial decl-specifier-seq. */
26583 clear_decl_specs (&decl_specs);
26584 decl_specs.type = type;
26586 /* Call grokdeclarator to figure out what type this is. */
26587 expr = grokdeclarator (NULL,
26588 &decl_specs,
26589 TYPENAME,
26590 /*initialized=*/0,
26591 /*attrlist=*/NULL);
26595 /* If the type-id production did not work out, then we must be
26596 looking at the unary-expression production. */
26597 if (!expr)
26598 expr = cp_parser_unary_expression (parser);
26600 /* Go back to evaluating expressions. */
26601 --cp_unevaluated_operand;
26602 --c_inhibit_evaluation_warnings;
26604 /* Free the message we created. */
26605 free (tmp);
26606 /* And restore the old one. */
26607 parser->type_definition_forbidden_message = saved_message;
26608 parser->integral_constant_expression_p
26609 = saved_integral_constant_expression_p;
26610 parser->non_integral_constant_expression_p
26611 = saved_non_integral_constant_expression_p;
26613 return expr;
26616 /* If the current declaration has no declarator, return true. */
26618 static bool
26619 cp_parser_declares_only_class_p (cp_parser *parser)
26621 /* If the next token is a `;' or a `,' then there is no
26622 declarator. */
26623 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26624 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26627 /* Update the DECL_SPECS to reflect the storage class indicated by
26628 KEYWORD. */
26630 static void
26631 cp_parser_set_storage_class (cp_parser *parser,
26632 cp_decl_specifier_seq *decl_specs,
26633 enum rid keyword,
26634 cp_token *token)
26636 cp_storage_class storage_class;
26638 if (parser->in_unbraced_linkage_specification_p)
26640 error_at (token->location, "invalid use of %qD in linkage specification",
26641 ridpointers[keyword]);
26642 return;
26644 else if (decl_specs->storage_class != sc_none)
26646 decl_specs->conflicting_specifiers_p = true;
26647 return;
26650 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26651 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26652 && decl_specs->gnu_thread_keyword_p)
26654 pedwarn (decl_specs->locations[ds_thread], 0,
26655 "%<__thread%> before %qD", ridpointers[keyword]);
26658 switch (keyword)
26660 case RID_AUTO:
26661 storage_class = sc_auto;
26662 break;
26663 case RID_REGISTER:
26664 storage_class = sc_register;
26665 break;
26666 case RID_STATIC:
26667 storage_class = sc_static;
26668 break;
26669 case RID_EXTERN:
26670 storage_class = sc_extern;
26671 break;
26672 case RID_MUTABLE:
26673 storage_class = sc_mutable;
26674 break;
26675 default:
26676 gcc_unreachable ();
26678 decl_specs->storage_class = storage_class;
26679 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26681 /* A storage class specifier cannot be applied alongside a typedef
26682 specifier. If there is a typedef specifier present then set
26683 conflicting_specifiers_p which will trigger an error later
26684 on in grokdeclarator. */
26685 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26686 decl_specs->conflicting_specifiers_p = true;
26689 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26690 is true, the type is a class or enum definition. */
26692 static void
26693 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26694 tree type_spec,
26695 cp_token *token,
26696 bool type_definition_p)
26698 decl_specs->any_specifiers_p = true;
26700 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26701 (with, for example, in "typedef int wchar_t;") we remember that
26702 this is what happened. In system headers, we ignore these
26703 declarations so that G++ can work with system headers that are not
26704 C++-safe. */
26705 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26706 && !type_definition_p
26707 && (type_spec == boolean_type_node
26708 || type_spec == char16_type_node
26709 || type_spec == char32_type_node
26710 || type_spec == wchar_type_node)
26711 && (decl_specs->type
26712 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26713 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26714 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26715 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26717 decl_specs->redefined_builtin_type = type_spec;
26718 set_and_check_decl_spec_loc (decl_specs,
26719 ds_redefined_builtin_type_spec,
26720 token);
26721 if (!decl_specs->type)
26723 decl_specs->type = type_spec;
26724 decl_specs->type_definition_p = false;
26725 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26728 else if (decl_specs->type)
26729 decl_specs->multiple_types_p = true;
26730 else
26732 decl_specs->type = type_spec;
26733 decl_specs->type_definition_p = type_definition_p;
26734 decl_specs->redefined_builtin_type = NULL_TREE;
26735 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26739 /* True iff TOKEN is the GNU keyword __thread. */
26741 static bool
26742 token_is__thread (cp_token *token)
26744 gcc_assert (token->keyword == RID_THREAD);
26745 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26748 /* Set the location for a declarator specifier and check if it is
26749 duplicated.
26751 DECL_SPECS is the sequence of declarator specifiers onto which to
26752 set the location.
26754 DS is the single declarator specifier to set which location is to
26755 be set onto the existing sequence of declarators.
26757 LOCATION is the location for the declarator specifier to
26758 consider. */
26760 static void
26761 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26762 cp_decl_spec ds, cp_token *token)
26764 gcc_assert (ds < ds_last);
26766 if (decl_specs == NULL)
26767 return;
26769 source_location location = token->location;
26771 if (decl_specs->locations[ds] == 0)
26773 decl_specs->locations[ds] = location;
26774 if (ds == ds_thread)
26775 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26777 else
26779 if (ds == ds_long)
26781 if (decl_specs->locations[ds_long_long] != 0)
26782 error_at (location,
26783 "%<long long long%> is too long for GCC");
26784 else
26786 decl_specs->locations[ds_long_long] = location;
26787 pedwarn_cxx98 (location,
26788 OPT_Wlong_long,
26789 "ISO C++ 1998 does not support %<long long%>");
26792 else if (ds == ds_thread)
26794 bool gnu = token_is__thread (token);
26795 if (gnu != decl_specs->gnu_thread_keyword_p)
26796 error_at (location,
26797 "both %<__thread%> and %<thread_local%> specified");
26798 else
26799 error_at (location, "duplicate %qD", token->u.value);
26801 else
26803 static const char *const decl_spec_names[] = {
26804 "signed",
26805 "unsigned",
26806 "short",
26807 "long",
26808 "const",
26809 "volatile",
26810 "restrict",
26811 "inline",
26812 "virtual",
26813 "explicit",
26814 "friend",
26815 "typedef",
26816 "using",
26817 "constexpr",
26818 "__complex"
26820 error_at (location,
26821 "duplicate %qs", decl_spec_names[ds]);
26826 /* Return true iff the declarator specifier DS is present in the
26827 sequence of declarator specifiers DECL_SPECS. */
26829 bool
26830 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26831 cp_decl_spec ds)
26833 gcc_assert (ds < ds_last);
26835 if (decl_specs == NULL)
26836 return false;
26838 return decl_specs->locations[ds] != 0;
26841 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26842 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26844 static bool
26845 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26847 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26850 /* Issue an error message indicating that TOKEN_DESC was expected.
26851 If KEYWORD is true, it indicated this function is called by
26852 cp_parser_require_keword and the required token can only be
26853 a indicated keyword. */
26855 static void
26856 cp_parser_required_error (cp_parser *parser,
26857 required_token token_desc,
26858 bool keyword)
26860 switch (token_desc)
26862 case RT_NEW:
26863 cp_parser_error (parser, "expected %<new%>");
26864 return;
26865 case RT_DELETE:
26866 cp_parser_error (parser, "expected %<delete%>");
26867 return;
26868 case RT_RETURN:
26869 cp_parser_error (parser, "expected %<return%>");
26870 return;
26871 case RT_WHILE:
26872 cp_parser_error (parser, "expected %<while%>");
26873 return;
26874 case RT_EXTERN:
26875 cp_parser_error (parser, "expected %<extern%>");
26876 return;
26877 case RT_STATIC_ASSERT:
26878 cp_parser_error (parser, "expected %<static_assert%>");
26879 return;
26880 case RT_DECLTYPE:
26881 cp_parser_error (parser, "expected %<decltype%>");
26882 return;
26883 case RT_OPERATOR:
26884 cp_parser_error (parser, "expected %<operator%>");
26885 return;
26886 case RT_CLASS:
26887 cp_parser_error (parser, "expected %<class%>");
26888 return;
26889 case RT_TEMPLATE:
26890 cp_parser_error (parser, "expected %<template%>");
26891 return;
26892 case RT_NAMESPACE:
26893 cp_parser_error (parser, "expected %<namespace%>");
26894 return;
26895 case RT_USING:
26896 cp_parser_error (parser, "expected %<using%>");
26897 return;
26898 case RT_ASM:
26899 cp_parser_error (parser, "expected %<asm%>");
26900 return;
26901 case RT_TRY:
26902 cp_parser_error (parser, "expected %<try%>");
26903 return;
26904 case RT_CATCH:
26905 cp_parser_error (parser, "expected %<catch%>");
26906 return;
26907 case RT_THROW:
26908 cp_parser_error (parser, "expected %<throw%>");
26909 return;
26910 case RT_LABEL:
26911 cp_parser_error (parser, "expected %<__label__%>");
26912 return;
26913 case RT_AT_TRY:
26914 cp_parser_error (parser, "expected %<@try%>");
26915 return;
26916 case RT_AT_SYNCHRONIZED:
26917 cp_parser_error (parser, "expected %<@synchronized%>");
26918 return;
26919 case RT_AT_THROW:
26920 cp_parser_error (parser, "expected %<@throw%>");
26921 return;
26922 case RT_TRANSACTION_ATOMIC:
26923 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26924 return;
26925 case RT_TRANSACTION_RELAXED:
26926 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26927 return;
26928 default:
26929 break;
26931 if (!keyword)
26933 switch (token_desc)
26935 case RT_SEMICOLON:
26936 cp_parser_error (parser, "expected %<;%>");
26937 return;
26938 case RT_OPEN_PAREN:
26939 cp_parser_error (parser, "expected %<(%>");
26940 return;
26941 case RT_CLOSE_BRACE:
26942 cp_parser_error (parser, "expected %<}%>");
26943 return;
26944 case RT_OPEN_BRACE:
26945 cp_parser_error (parser, "expected %<{%>");
26946 return;
26947 case RT_CLOSE_SQUARE:
26948 cp_parser_error (parser, "expected %<]%>");
26949 return;
26950 case RT_OPEN_SQUARE:
26951 cp_parser_error (parser, "expected %<[%>");
26952 return;
26953 case RT_COMMA:
26954 cp_parser_error (parser, "expected %<,%>");
26955 return;
26956 case RT_SCOPE:
26957 cp_parser_error (parser, "expected %<::%>");
26958 return;
26959 case RT_LESS:
26960 cp_parser_error (parser, "expected %<<%>");
26961 return;
26962 case RT_GREATER:
26963 cp_parser_error (parser, "expected %<>%>");
26964 return;
26965 case RT_EQ:
26966 cp_parser_error (parser, "expected %<=%>");
26967 return;
26968 case RT_ELLIPSIS:
26969 cp_parser_error (parser, "expected %<...%>");
26970 return;
26971 case RT_MULT:
26972 cp_parser_error (parser, "expected %<*%>");
26973 return;
26974 case RT_COMPL:
26975 cp_parser_error (parser, "expected %<~%>");
26976 return;
26977 case RT_COLON:
26978 cp_parser_error (parser, "expected %<:%>");
26979 return;
26980 case RT_COLON_SCOPE:
26981 cp_parser_error (parser, "expected %<:%> or %<::%>");
26982 return;
26983 case RT_CLOSE_PAREN:
26984 cp_parser_error (parser, "expected %<)%>");
26985 return;
26986 case RT_COMMA_CLOSE_PAREN:
26987 cp_parser_error (parser, "expected %<,%> or %<)%>");
26988 return;
26989 case RT_PRAGMA_EOL:
26990 cp_parser_error (parser, "expected end of line");
26991 return;
26992 case RT_NAME:
26993 cp_parser_error (parser, "expected identifier");
26994 return;
26995 case RT_SELECT:
26996 cp_parser_error (parser, "expected selection-statement");
26997 return;
26998 case RT_INTERATION:
26999 cp_parser_error (parser, "expected iteration-statement");
27000 return;
27001 case RT_JUMP:
27002 cp_parser_error (parser, "expected jump-statement");
27003 return;
27004 case RT_CLASS_KEY:
27005 cp_parser_error (parser, "expected class-key");
27006 return;
27007 case RT_CLASS_TYPENAME_TEMPLATE:
27008 cp_parser_error (parser,
27009 "expected %<class%>, %<typename%>, or %<template%>");
27010 return;
27011 default:
27012 gcc_unreachable ();
27015 else
27016 gcc_unreachable ();
27021 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27022 issue an error message indicating that TOKEN_DESC was expected.
27024 Returns the token consumed, if the token had the appropriate type.
27025 Otherwise, returns NULL. */
27027 static cp_token *
27028 cp_parser_require (cp_parser* parser,
27029 enum cpp_ttype type,
27030 required_token token_desc)
27032 if (cp_lexer_next_token_is (parser->lexer, type))
27033 return cp_lexer_consume_token (parser->lexer);
27034 else
27036 /* Output the MESSAGE -- unless we're parsing tentatively. */
27037 if (!cp_parser_simulate_error (parser))
27038 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27039 return NULL;
27043 /* An error message is produced if the next token is not '>'.
27044 All further tokens are skipped until the desired token is
27045 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27047 static void
27048 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27050 /* Current level of '< ... >'. */
27051 unsigned level = 0;
27052 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27053 unsigned nesting_depth = 0;
27055 /* Are we ready, yet? If not, issue error message. */
27056 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27057 return;
27059 /* Skip tokens until the desired token is found. */
27060 while (true)
27062 /* Peek at the next token. */
27063 switch (cp_lexer_peek_token (parser->lexer)->type)
27065 case CPP_LESS:
27066 if (!nesting_depth)
27067 ++level;
27068 break;
27070 case CPP_RSHIFT:
27071 if (cxx_dialect == cxx98)
27072 /* C++0x views the `>>' operator as two `>' tokens, but
27073 C++98 does not. */
27074 break;
27075 else if (!nesting_depth && level-- == 0)
27077 /* We've hit a `>>' where the first `>' closes the
27078 template argument list, and the second `>' is
27079 spurious. Just consume the `>>' and stop; we've
27080 already produced at least one error. */
27081 cp_lexer_consume_token (parser->lexer);
27082 return;
27084 /* Fall through for C++0x, so we handle the second `>' in
27085 the `>>'. */
27087 case CPP_GREATER:
27088 if (!nesting_depth && level-- == 0)
27090 /* We've reached the token we want, consume it and stop. */
27091 cp_lexer_consume_token (parser->lexer);
27092 return;
27094 break;
27096 case CPP_OPEN_PAREN:
27097 case CPP_OPEN_SQUARE:
27098 ++nesting_depth;
27099 break;
27101 case CPP_CLOSE_PAREN:
27102 case CPP_CLOSE_SQUARE:
27103 if (nesting_depth-- == 0)
27104 return;
27105 break;
27107 case CPP_EOF:
27108 case CPP_PRAGMA_EOL:
27109 case CPP_SEMICOLON:
27110 case CPP_OPEN_BRACE:
27111 case CPP_CLOSE_BRACE:
27112 /* The '>' was probably forgotten, don't look further. */
27113 return;
27115 default:
27116 break;
27119 /* Consume this token. */
27120 cp_lexer_consume_token (parser->lexer);
27124 /* If the next token is the indicated keyword, consume it. Otherwise,
27125 issue an error message indicating that TOKEN_DESC was expected.
27127 Returns the token consumed, if the token had the appropriate type.
27128 Otherwise, returns NULL. */
27130 static cp_token *
27131 cp_parser_require_keyword (cp_parser* parser,
27132 enum rid keyword,
27133 required_token token_desc)
27135 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27137 if (token && token->keyword != keyword)
27139 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27140 return NULL;
27143 return token;
27146 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27147 function-definition. */
27149 static bool
27150 cp_parser_token_starts_function_definition_p (cp_token* token)
27152 return (/* An ordinary function-body begins with an `{'. */
27153 token->type == CPP_OPEN_BRACE
27154 /* A ctor-initializer begins with a `:'. */
27155 || token->type == CPP_COLON
27156 /* A function-try-block begins with `try'. */
27157 || token->keyword == RID_TRY
27158 /* A function-transaction-block begins with `__transaction_atomic'
27159 or `__transaction_relaxed'. */
27160 || token->keyword == RID_TRANSACTION_ATOMIC
27161 || token->keyword == RID_TRANSACTION_RELAXED
27162 /* The named return value extension begins with `return'. */
27163 || token->keyword == RID_RETURN);
27166 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27167 definition. */
27169 static bool
27170 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27172 cp_token *token;
27174 token = cp_lexer_peek_token (parser->lexer);
27175 return (token->type == CPP_OPEN_BRACE
27176 || (token->type == CPP_COLON
27177 && !parser->colon_doesnt_start_class_def_p));
27180 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27181 C++0x) ending a template-argument. */
27183 static bool
27184 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27186 cp_token *token;
27188 token = cp_lexer_peek_token (parser->lexer);
27189 return (token->type == CPP_COMMA
27190 || token->type == CPP_GREATER
27191 || token->type == CPP_ELLIPSIS
27192 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27195 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27196 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27198 static bool
27199 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27200 size_t n)
27202 cp_token *token;
27204 token = cp_lexer_peek_nth_token (parser->lexer, n);
27205 if (token->type == CPP_LESS)
27206 return true;
27207 /* Check for the sequence `<::' in the original code. It would be lexed as
27208 `[:', where `[' is a digraph, and there is no whitespace before
27209 `:'. */
27210 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27212 cp_token *token2;
27213 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27214 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27215 return true;
27217 return false;
27220 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27221 or none_type otherwise. */
27223 static enum tag_types
27224 cp_parser_token_is_class_key (cp_token* token)
27226 switch (token->keyword)
27228 case RID_CLASS:
27229 return class_type;
27230 case RID_STRUCT:
27231 return record_type;
27232 case RID_UNION:
27233 return union_type;
27235 default:
27236 return none_type;
27240 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27241 or none_type otherwise or if the token is null. */
27243 static enum tag_types
27244 cp_parser_token_is_type_parameter_key (cp_token* token)
27246 if (!token)
27247 return none_type;
27249 switch (token->keyword)
27251 case RID_CLASS:
27252 return class_type;
27253 case RID_TYPENAME:
27254 return typename_type;
27256 default:
27257 return none_type;
27261 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27263 static void
27264 cp_parser_check_class_key (enum tag_types class_key, tree type)
27266 if (type == error_mark_node)
27267 return;
27268 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27270 if (permerror (input_location, "%qs tag used in naming %q#T",
27271 class_key == union_type ? "union"
27272 : class_key == record_type ? "struct" : "class",
27273 type))
27274 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27275 "%q#T was previously declared here", type);
27279 /* Issue an error message if DECL is redeclared with different
27280 access than its original declaration [class.access.spec/3].
27281 This applies to nested classes, nested class templates and
27282 enumerations [class.mem/1]. */
27284 static void
27285 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27287 if (!decl
27288 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27289 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27290 return;
27292 if ((TREE_PRIVATE (decl)
27293 != (current_access_specifier == access_private_node))
27294 || (TREE_PROTECTED (decl)
27295 != (current_access_specifier == access_protected_node)))
27296 error_at (location, "%qD redeclared with different access", decl);
27299 /* Look for the `template' keyword, as a syntactic disambiguator.
27300 Return TRUE iff it is present, in which case it will be
27301 consumed. */
27303 static bool
27304 cp_parser_optional_template_keyword (cp_parser *parser)
27306 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27308 /* In C++98 the `template' keyword can only be used within templates;
27309 outside templates the parser can always figure out what is a
27310 template and what is not. In C++11, per the resolution of DR 468,
27311 `template' is allowed in cases where it is not strictly necessary. */
27312 if (!processing_template_decl
27313 && pedantic && cxx_dialect == cxx98)
27315 cp_token *token = cp_lexer_peek_token (parser->lexer);
27316 pedwarn (token->location, OPT_Wpedantic,
27317 "in C++98 %<template%> (as a disambiguator) is only "
27318 "allowed within templates");
27319 /* If this part of the token stream is rescanned, the same
27320 error message would be generated. So, we purge the token
27321 from the stream. */
27322 cp_lexer_purge_token (parser->lexer);
27323 return false;
27325 else
27327 /* Consume the `template' keyword. */
27328 cp_lexer_consume_token (parser->lexer);
27329 return true;
27332 return false;
27335 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27336 set PARSER->SCOPE, and perform other related actions. */
27338 static void
27339 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27341 struct tree_check *check_value;
27343 /* Get the stored value. */
27344 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27345 /* Set the scope from the stored value. */
27346 parser->scope = saved_checks_value (check_value);
27347 parser->qualifying_scope = check_value->qualifying_scope;
27348 parser->object_scope = NULL_TREE;
27351 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27352 encounter the end of a block before what we were looking for. */
27354 static bool
27355 cp_parser_cache_group (cp_parser *parser,
27356 enum cpp_ttype end,
27357 unsigned depth)
27359 while (true)
27361 cp_token *token = cp_lexer_peek_token (parser->lexer);
27363 /* Abort a parenthesized expression if we encounter a semicolon. */
27364 if ((end == CPP_CLOSE_PAREN || depth == 0)
27365 && token->type == CPP_SEMICOLON)
27366 return true;
27367 /* If we've reached the end of the file, stop. */
27368 if (token->type == CPP_EOF
27369 || (end != CPP_PRAGMA_EOL
27370 && token->type == CPP_PRAGMA_EOL))
27371 return true;
27372 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27373 /* We've hit the end of an enclosing block, so there's been some
27374 kind of syntax error. */
27375 return true;
27377 /* Consume the token. */
27378 cp_lexer_consume_token (parser->lexer);
27379 /* See if it starts a new group. */
27380 if (token->type == CPP_OPEN_BRACE)
27382 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27383 /* In theory this should probably check end == '}', but
27384 cp_parser_save_member_function_body needs it to exit
27385 after either '}' or ')' when called with ')'. */
27386 if (depth == 0)
27387 return false;
27389 else if (token->type == CPP_OPEN_PAREN)
27391 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27392 if (depth == 0 && end == CPP_CLOSE_PAREN)
27393 return false;
27395 else if (token->type == CPP_PRAGMA)
27396 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27397 else if (token->type == end)
27398 return false;
27402 /* Like above, for caching a default argument or NSDMI. Both of these are
27403 terminated by a non-nested comma, but it can be unclear whether or not a
27404 comma is nested in a template argument list unless we do more parsing.
27405 In order to handle this ambiguity, when we encounter a ',' after a '<'
27406 we try to parse what follows as a parameter-declaration-list (in the
27407 case of a default argument) or a member-declarator (in the case of an
27408 NSDMI). If that succeeds, then we stop caching. */
27410 static tree
27411 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27413 unsigned depth = 0;
27414 int maybe_template_id = 0;
27415 cp_token *first_token;
27416 cp_token *token;
27417 tree default_argument;
27419 /* Add tokens until we have processed the entire default
27420 argument. We add the range [first_token, token). */
27421 first_token = cp_lexer_peek_token (parser->lexer);
27422 if (first_token->type == CPP_OPEN_BRACE)
27424 /* For list-initialization, this is straightforward. */
27425 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27426 token = cp_lexer_peek_token (parser->lexer);
27428 else while (true)
27430 bool done = false;
27432 /* Peek at the next token. */
27433 token = cp_lexer_peek_token (parser->lexer);
27434 /* What we do depends on what token we have. */
27435 switch (token->type)
27437 /* In valid code, a default argument must be
27438 immediately followed by a `,' `)', or `...'. */
27439 case CPP_COMMA:
27440 if (depth == 0 && maybe_template_id)
27442 /* If we've seen a '<', we might be in a
27443 template-argument-list. Until Core issue 325 is
27444 resolved, we don't know how this situation ought
27445 to be handled, so try to DTRT. We check whether
27446 what comes after the comma is a valid parameter
27447 declaration list. If it is, then the comma ends
27448 the default argument; otherwise the default
27449 argument continues. */
27450 bool error = false;
27451 cp_token *peek;
27453 /* Set ITALP so cp_parser_parameter_declaration_list
27454 doesn't decide to commit to this parse. */
27455 bool saved_italp = parser->in_template_argument_list_p;
27456 parser->in_template_argument_list_p = true;
27458 cp_parser_parse_tentatively (parser);
27460 if (nsdmi)
27462 /* Parse declarators until we reach a non-comma or
27463 somthing that cannot be an initializer.
27464 Just checking whether we're looking at a single
27465 declarator is insufficient. Consider:
27466 int var = tuple<T,U>::x;
27467 The template parameter 'U' looks exactly like a
27468 declarator. */
27471 int ctor_dtor_or_conv_p;
27472 cp_lexer_consume_token (parser->lexer);
27473 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27474 &ctor_dtor_or_conv_p,
27475 /*parenthesized_p=*/NULL,
27476 /*member_p=*/true,
27477 /*friend_p=*/false);
27478 peek = cp_lexer_peek_token (parser->lexer);
27479 if (cp_parser_error_occurred (parser))
27480 break;
27482 while (peek->type == CPP_COMMA);
27483 /* If we met an '=' or ';' then the original comma
27484 was the end of the NSDMI. Otherwise assume
27485 we're still in the NSDMI. */
27486 error = (peek->type != CPP_EQ
27487 && peek->type != CPP_SEMICOLON);
27489 else
27491 cp_lexer_consume_token (parser->lexer);
27492 begin_scope (sk_function_parms, NULL_TREE);
27493 cp_parser_parameter_declaration_list (parser, &error);
27494 pop_bindings_and_leave_scope ();
27496 if (!cp_parser_error_occurred (parser) && !error)
27497 done = true;
27498 cp_parser_abort_tentative_parse (parser);
27500 parser->in_template_argument_list_p = saved_italp;
27501 break;
27503 case CPP_CLOSE_PAREN:
27504 case CPP_ELLIPSIS:
27505 /* If we run into a non-nested `;', `}', or `]',
27506 then the code is invalid -- but the default
27507 argument is certainly over. */
27508 case CPP_SEMICOLON:
27509 case CPP_CLOSE_BRACE:
27510 case CPP_CLOSE_SQUARE:
27511 if (depth == 0
27512 /* Handle correctly int n = sizeof ... ( p ); */
27513 && token->type != CPP_ELLIPSIS)
27514 done = true;
27515 /* Update DEPTH, if necessary. */
27516 else if (token->type == CPP_CLOSE_PAREN
27517 || token->type == CPP_CLOSE_BRACE
27518 || token->type == CPP_CLOSE_SQUARE)
27519 --depth;
27520 break;
27522 case CPP_OPEN_PAREN:
27523 case CPP_OPEN_SQUARE:
27524 case CPP_OPEN_BRACE:
27525 ++depth;
27526 break;
27528 case CPP_LESS:
27529 if (depth == 0)
27530 /* This might be the comparison operator, or it might
27531 start a template argument list. */
27532 ++maybe_template_id;
27533 break;
27535 case CPP_RSHIFT:
27536 if (cxx_dialect == cxx98)
27537 break;
27538 /* Fall through for C++0x, which treats the `>>'
27539 operator like two `>' tokens in certain
27540 cases. */
27542 case CPP_GREATER:
27543 if (depth == 0)
27545 /* This might be an operator, or it might close a
27546 template argument list. But if a previous '<'
27547 started a template argument list, this will have
27548 closed it, so we can't be in one anymore. */
27549 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27550 if (maybe_template_id < 0)
27551 maybe_template_id = 0;
27553 break;
27555 /* If we run out of tokens, issue an error message. */
27556 case CPP_EOF:
27557 case CPP_PRAGMA_EOL:
27558 error_at (token->location, "file ends in default argument");
27559 return error_mark_node;
27561 case CPP_NAME:
27562 case CPP_SCOPE:
27563 /* In these cases, we should look for template-ids.
27564 For example, if the default argument is
27565 `X<int, double>()', we need to do name lookup to
27566 figure out whether or not `X' is a template; if
27567 so, the `,' does not end the default argument.
27569 That is not yet done. */
27570 break;
27572 default:
27573 break;
27576 /* If we've reached the end, stop. */
27577 if (done)
27578 break;
27580 /* Add the token to the token block. */
27581 token = cp_lexer_consume_token (parser->lexer);
27584 /* Create a DEFAULT_ARG to represent the unparsed default
27585 argument. */
27586 default_argument = make_node (DEFAULT_ARG);
27587 DEFARG_TOKENS (default_argument)
27588 = cp_token_cache_new (first_token, token);
27589 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27591 return default_argument;
27594 /* Begin parsing tentatively. We always save tokens while parsing
27595 tentatively so that if the tentative parsing fails we can restore the
27596 tokens. */
27598 static void
27599 cp_parser_parse_tentatively (cp_parser* parser)
27601 /* Enter a new parsing context. */
27602 parser->context = cp_parser_context_new (parser->context);
27603 /* Begin saving tokens. */
27604 cp_lexer_save_tokens (parser->lexer);
27605 /* In order to avoid repetitive access control error messages,
27606 access checks are queued up until we are no longer parsing
27607 tentatively. */
27608 push_deferring_access_checks (dk_deferred);
27611 /* Commit to the currently active tentative parse. */
27613 static void
27614 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27616 cp_parser_context *context;
27617 cp_lexer *lexer;
27619 /* Mark all of the levels as committed. */
27620 lexer = parser->lexer;
27621 for (context = parser->context; context->next; context = context->next)
27623 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27624 break;
27625 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27626 while (!cp_lexer_saving_tokens (lexer))
27627 lexer = lexer->next;
27628 cp_lexer_commit_tokens (lexer);
27632 /* Commit to the topmost currently active tentative parse.
27634 Note that this function shouldn't be called when there are
27635 irreversible side-effects while in a tentative state. For
27636 example, we shouldn't create a permanent entry in the symbol
27637 table, or issue an error message that might not apply if the
27638 tentative parse is aborted. */
27640 static void
27641 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27643 cp_parser_context *context = parser->context;
27644 cp_lexer *lexer = parser->lexer;
27646 if (context)
27648 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27649 return;
27650 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27652 while (!cp_lexer_saving_tokens (lexer))
27653 lexer = lexer->next;
27654 cp_lexer_commit_tokens (lexer);
27658 /* Abort the currently active tentative parse. All consumed tokens
27659 will be rolled back, and no diagnostics will be issued. */
27661 static void
27662 cp_parser_abort_tentative_parse (cp_parser* parser)
27664 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27665 || errorcount > 0);
27666 cp_parser_simulate_error (parser);
27667 /* Now, pretend that we want to see if the construct was
27668 successfully parsed. */
27669 cp_parser_parse_definitely (parser);
27672 /* Stop parsing tentatively. If a parse error has occurred, restore the
27673 token stream. Otherwise, commit to the tokens we have consumed.
27674 Returns true if no error occurred; false otherwise. */
27676 static bool
27677 cp_parser_parse_definitely (cp_parser* parser)
27679 bool error_occurred;
27680 cp_parser_context *context;
27682 /* Remember whether or not an error occurred, since we are about to
27683 destroy that information. */
27684 error_occurred = cp_parser_error_occurred (parser);
27685 /* Remove the topmost context from the stack. */
27686 context = parser->context;
27687 parser->context = context->next;
27688 /* If no parse errors occurred, commit to the tentative parse. */
27689 if (!error_occurred)
27691 /* Commit to the tokens read tentatively, unless that was
27692 already done. */
27693 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27694 cp_lexer_commit_tokens (parser->lexer);
27696 pop_to_parent_deferring_access_checks ();
27698 /* Otherwise, if errors occurred, roll back our state so that things
27699 are just as they were before we began the tentative parse. */
27700 else
27702 cp_lexer_rollback_tokens (parser->lexer);
27703 pop_deferring_access_checks ();
27705 /* Add the context to the front of the free list. */
27706 context->next = cp_parser_context_free_list;
27707 cp_parser_context_free_list = context;
27709 return !error_occurred;
27712 /* Returns true if we are parsing tentatively and are not committed to
27713 this tentative parse. */
27715 static bool
27716 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27718 return (cp_parser_parsing_tentatively (parser)
27719 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27722 /* Returns nonzero iff an error has occurred during the most recent
27723 tentative parse. */
27725 static bool
27726 cp_parser_error_occurred (cp_parser* parser)
27728 return (cp_parser_parsing_tentatively (parser)
27729 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27732 /* Returns nonzero if GNU extensions are allowed. */
27734 static bool
27735 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27737 return parser->allow_gnu_extensions_p;
27740 /* Objective-C++ Productions */
27743 /* Parse an Objective-C expression, which feeds into a primary-expression
27744 above.
27746 objc-expression:
27747 objc-message-expression
27748 objc-string-literal
27749 objc-encode-expression
27750 objc-protocol-expression
27751 objc-selector-expression
27753 Returns a tree representation of the expression. */
27755 static cp_expr
27756 cp_parser_objc_expression (cp_parser* parser)
27758 /* Try to figure out what kind of declaration is present. */
27759 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27761 switch (kwd->type)
27763 case CPP_OPEN_SQUARE:
27764 return cp_parser_objc_message_expression (parser);
27766 case CPP_OBJC_STRING:
27767 kwd = cp_lexer_consume_token (parser->lexer);
27768 return objc_build_string_object (kwd->u.value);
27770 case CPP_KEYWORD:
27771 switch (kwd->keyword)
27773 case RID_AT_ENCODE:
27774 return cp_parser_objc_encode_expression (parser);
27776 case RID_AT_PROTOCOL:
27777 return cp_parser_objc_protocol_expression (parser);
27779 case RID_AT_SELECTOR:
27780 return cp_parser_objc_selector_expression (parser);
27782 default:
27783 break;
27785 default:
27786 error_at (kwd->location,
27787 "misplaced %<@%D%> Objective-C++ construct",
27788 kwd->u.value);
27789 cp_parser_skip_to_end_of_block_or_statement (parser);
27792 return error_mark_node;
27795 /* Parse an Objective-C message expression.
27797 objc-message-expression:
27798 [ objc-message-receiver objc-message-args ]
27800 Returns a representation of an Objective-C message. */
27802 static tree
27803 cp_parser_objc_message_expression (cp_parser* parser)
27805 tree receiver, messageargs;
27807 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27808 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27809 receiver = cp_parser_objc_message_receiver (parser);
27810 messageargs = cp_parser_objc_message_args (parser);
27811 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27812 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27814 tree result = objc_build_message_expr (receiver, messageargs);
27816 /* Construct a location e.g.
27817 [self func1:5]
27818 ^~~~~~~~~~~~~~
27819 ranging from the '[' to the ']', with the caret at the start. */
27820 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27821 protected_set_expr_location (result, combined_loc);
27823 return result;
27826 /* Parse an objc-message-receiver.
27828 objc-message-receiver:
27829 expression
27830 simple-type-specifier
27832 Returns a representation of the type or expression. */
27834 static tree
27835 cp_parser_objc_message_receiver (cp_parser* parser)
27837 tree rcv;
27839 /* An Objective-C message receiver may be either (1) a type
27840 or (2) an expression. */
27841 cp_parser_parse_tentatively (parser);
27842 rcv = cp_parser_expression (parser);
27844 /* If that worked out, fine. */
27845 if (cp_parser_parse_definitely (parser))
27846 return rcv;
27848 cp_parser_parse_tentatively (parser);
27849 rcv = cp_parser_simple_type_specifier (parser,
27850 /*decl_specs=*/NULL,
27851 CP_PARSER_FLAGS_NONE);
27853 if (cp_parser_parse_definitely (parser))
27854 return objc_get_class_reference (rcv);
27856 cp_parser_error (parser, "objective-c++ message receiver expected");
27857 return error_mark_node;
27860 /* Parse the arguments and selectors comprising an Objective-C message.
27862 objc-message-args:
27863 objc-selector
27864 objc-selector-args
27865 objc-selector-args , objc-comma-args
27867 objc-selector-args:
27868 objc-selector [opt] : assignment-expression
27869 objc-selector-args objc-selector [opt] : assignment-expression
27871 objc-comma-args:
27872 assignment-expression
27873 objc-comma-args , assignment-expression
27875 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27876 selector arguments and TREE_VALUE containing a list of comma
27877 arguments. */
27879 static tree
27880 cp_parser_objc_message_args (cp_parser* parser)
27882 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27883 bool maybe_unary_selector_p = true;
27884 cp_token *token = cp_lexer_peek_token (parser->lexer);
27886 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27888 tree selector = NULL_TREE, arg;
27890 if (token->type != CPP_COLON)
27891 selector = cp_parser_objc_selector (parser);
27893 /* Detect if we have a unary selector. */
27894 if (maybe_unary_selector_p
27895 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27896 return build_tree_list (selector, NULL_TREE);
27898 maybe_unary_selector_p = false;
27899 cp_parser_require (parser, CPP_COLON, RT_COLON);
27900 arg = cp_parser_assignment_expression (parser);
27902 sel_args
27903 = chainon (sel_args,
27904 build_tree_list (selector, arg));
27906 token = cp_lexer_peek_token (parser->lexer);
27909 /* Handle non-selector arguments, if any. */
27910 while (token->type == CPP_COMMA)
27912 tree arg;
27914 cp_lexer_consume_token (parser->lexer);
27915 arg = cp_parser_assignment_expression (parser);
27917 addl_args
27918 = chainon (addl_args,
27919 build_tree_list (NULL_TREE, arg));
27921 token = cp_lexer_peek_token (parser->lexer);
27924 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27926 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27927 return build_tree_list (error_mark_node, error_mark_node);
27930 return build_tree_list (sel_args, addl_args);
27933 /* Parse an Objective-C encode expression.
27935 objc-encode-expression:
27936 @encode objc-typename
27938 Returns an encoded representation of the type argument. */
27940 static cp_expr
27941 cp_parser_objc_encode_expression (cp_parser* parser)
27943 tree type;
27944 cp_token *token;
27945 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27947 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27948 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27949 token = cp_lexer_peek_token (parser->lexer);
27950 type = complete_type (cp_parser_type_id (parser));
27951 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27953 if (!type)
27955 error_at (token->location,
27956 "%<@encode%> must specify a type as an argument");
27957 return error_mark_node;
27960 /* This happens if we find @encode(T) (where T is a template
27961 typename or something dependent on a template typename) when
27962 parsing a template. In that case, we can't compile it
27963 immediately, but we rather create an AT_ENCODE_EXPR which will
27964 need to be instantiated when the template is used.
27966 if (dependent_type_p (type))
27968 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27969 TREE_READONLY (value) = 1;
27970 return value;
27974 /* Build a location of the form:
27975 @encode(int)
27976 ^~~~~~~~~~~~
27977 with caret==start at the @ token, finishing at the close paren. */
27978 location_t combined_loc
27979 = make_location (start_loc, start_loc,
27980 cp_lexer_previous_token (parser->lexer)->location);
27982 return cp_expr (objc_build_encode_expr (type), combined_loc);
27985 /* Parse an Objective-C @defs expression. */
27987 static tree
27988 cp_parser_objc_defs_expression (cp_parser *parser)
27990 tree name;
27992 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27993 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27994 name = cp_parser_identifier (parser);
27995 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27997 return objc_get_class_ivars (name);
28000 /* Parse an Objective-C protocol expression.
28002 objc-protocol-expression:
28003 @protocol ( identifier )
28005 Returns a representation of the protocol expression. */
28007 static tree
28008 cp_parser_objc_protocol_expression (cp_parser* parser)
28010 tree proto;
28011 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28013 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28014 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28015 proto = cp_parser_identifier (parser);
28016 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28018 /* Build a location of the form:
28019 @protocol(prot)
28020 ^~~~~~~~~~~~~~~
28021 with caret==start at the @ token, finishing at the close paren. */
28022 location_t combined_loc
28023 = make_location (start_loc, start_loc,
28024 cp_lexer_previous_token (parser->lexer)->location);
28025 tree result = objc_build_protocol_expr (proto);
28026 protected_set_expr_location (result, combined_loc);
28027 return result;
28030 /* Parse an Objective-C selector expression.
28032 objc-selector-expression:
28033 @selector ( objc-method-signature )
28035 objc-method-signature:
28036 objc-selector
28037 objc-selector-seq
28039 objc-selector-seq:
28040 objc-selector :
28041 objc-selector-seq objc-selector :
28043 Returns a representation of the method selector. */
28045 static tree
28046 cp_parser_objc_selector_expression (cp_parser* parser)
28048 tree sel_seq = NULL_TREE;
28049 bool maybe_unary_selector_p = true;
28050 cp_token *token;
28051 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28053 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28054 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28055 token = cp_lexer_peek_token (parser->lexer);
28057 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28058 || token->type == CPP_SCOPE)
28060 tree selector = NULL_TREE;
28062 if (token->type != CPP_COLON
28063 || token->type == CPP_SCOPE)
28064 selector = cp_parser_objc_selector (parser);
28066 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28067 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28069 /* Detect if we have a unary selector. */
28070 if (maybe_unary_selector_p)
28072 sel_seq = selector;
28073 goto finish_selector;
28075 else
28077 cp_parser_error (parser, "expected %<:%>");
28080 maybe_unary_selector_p = false;
28081 token = cp_lexer_consume_token (parser->lexer);
28083 if (token->type == CPP_SCOPE)
28085 sel_seq
28086 = chainon (sel_seq,
28087 build_tree_list (selector, NULL_TREE));
28088 sel_seq
28089 = chainon (sel_seq,
28090 build_tree_list (NULL_TREE, NULL_TREE));
28092 else
28093 sel_seq
28094 = chainon (sel_seq,
28095 build_tree_list (selector, NULL_TREE));
28097 token = cp_lexer_peek_token (parser->lexer);
28100 finish_selector:
28101 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28104 /* Build a location of the form:
28105 @selector(func)
28106 ^~~~~~~~~~~~~~~
28107 with caret==start at the @ token, finishing at the close paren. */
28108 location_t combined_loc
28109 = make_location (loc, loc,
28110 cp_lexer_previous_token (parser->lexer)->location);
28111 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28112 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28113 protected_set_expr_location (result, combined_loc);
28114 return result;
28117 /* Parse a list of identifiers.
28119 objc-identifier-list:
28120 identifier
28121 objc-identifier-list , identifier
28123 Returns a TREE_LIST of identifier nodes. */
28125 static tree
28126 cp_parser_objc_identifier_list (cp_parser* parser)
28128 tree identifier;
28129 tree list;
28130 cp_token *sep;
28132 identifier = cp_parser_identifier (parser);
28133 if (identifier == error_mark_node)
28134 return error_mark_node;
28136 list = build_tree_list (NULL_TREE, identifier);
28137 sep = cp_lexer_peek_token (parser->lexer);
28139 while (sep->type == CPP_COMMA)
28141 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28142 identifier = cp_parser_identifier (parser);
28143 if (identifier == error_mark_node)
28144 return list;
28146 list = chainon (list, build_tree_list (NULL_TREE,
28147 identifier));
28148 sep = cp_lexer_peek_token (parser->lexer);
28151 return list;
28154 /* Parse an Objective-C alias declaration.
28156 objc-alias-declaration:
28157 @compatibility_alias identifier identifier ;
28159 This function registers the alias mapping with the Objective-C front end.
28160 It returns nothing. */
28162 static void
28163 cp_parser_objc_alias_declaration (cp_parser* parser)
28165 tree alias, orig;
28167 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28168 alias = cp_parser_identifier (parser);
28169 orig = cp_parser_identifier (parser);
28170 objc_declare_alias (alias, orig);
28171 cp_parser_consume_semicolon_at_end_of_statement (parser);
28174 /* Parse an Objective-C class forward-declaration.
28176 objc-class-declaration:
28177 @class objc-identifier-list ;
28179 The function registers the forward declarations with the Objective-C
28180 front end. It returns nothing. */
28182 static void
28183 cp_parser_objc_class_declaration (cp_parser* parser)
28185 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28186 while (true)
28188 tree id;
28190 id = cp_parser_identifier (parser);
28191 if (id == error_mark_node)
28192 break;
28194 objc_declare_class (id);
28196 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28197 cp_lexer_consume_token (parser->lexer);
28198 else
28199 break;
28201 cp_parser_consume_semicolon_at_end_of_statement (parser);
28204 /* Parse a list of Objective-C protocol references.
28206 objc-protocol-refs-opt:
28207 objc-protocol-refs [opt]
28209 objc-protocol-refs:
28210 < objc-identifier-list >
28212 Returns a TREE_LIST of identifiers, if any. */
28214 static tree
28215 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28217 tree protorefs = NULL_TREE;
28219 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28221 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28222 protorefs = cp_parser_objc_identifier_list (parser);
28223 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28226 return protorefs;
28229 /* Parse a Objective-C visibility specification. */
28231 static void
28232 cp_parser_objc_visibility_spec (cp_parser* parser)
28234 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28236 switch (vis->keyword)
28238 case RID_AT_PRIVATE:
28239 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28240 break;
28241 case RID_AT_PROTECTED:
28242 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28243 break;
28244 case RID_AT_PUBLIC:
28245 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28246 break;
28247 case RID_AT_PACKAGE:
28248 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28249 break;
28250 default:
28251 return;
28254 /* Eat '@private'/'@protected'/'@public'. */
28255 cp_lexer_consume_token (parser->lexer);
28258 /* Parse an Objective-C method type. Return 'true' if it is a class
28259 (+) method, and 'false' if it is an instance (-) method. */
28261 static inline bool
28262 cp_parser_objc_method_type (cp_parser* parser)
28264 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28265 return true;
28266 else
28267 return false;
28270 /* Parse an Objective-C protocol qualifier. */
28272 static tree
28273 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28275 tree quals = NULL_TREE, node;
28276 cp_token *token = cp_lexer_peek_token (parser->lexer);
28278 node = token->u.value;
28280 while (node && identifier_p (node)
28281 && (node == ridpointers [(int) RID_IN]
28282 || node == ridpointers [(int) RID_OUT]
28283 || node == ridpointers [(int) RID_INOUT]
28284 || node == ridpointers [(int) RID_BYCOPY]
28285 || node == ridpointers [(int) RID_BYREF]
28286 || node == ridpointers [(int) RID_ONEWAY]))
28288 quals = tree_cons (NULL_TREE, node, quals);
28289 cp_lexer_consume_token (parser->lexer);
28290 token = cp_lexer_peek_token (parser->lexer);
28291 node = token->u.value;
28294 return quals;
28297 /* Parse an Objective-C typename. */
28299 static tree
28300 cp_parser_objc_typename (cp_parser* parser)
28302 tree type_name = NULL_TREE;
28304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28306 tree proto_quals, cp_type = NULL_TREE;
28308 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28309 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28311 /* An ObjC type name may consist of just protocol qualifiers, in which
28312 case the type shall default to 'id'. */
28313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28315 cp_type = cp_parser_type_id (parser);
28317 /* If the type could not be parsed, an error has already
28318 been produced. For error recovery, behave as if it had
28319 not been specified, which will use the default type
28320 'id'. */
28321 if (cp_type == error_mark_node)
28323 cp_type = NULL_TREE;
28324 /* We need to skip to the closing parenthesis as
28325 cp_parser_type_id() does not seem to do it for
28326 us. */
28327 cp_parser_skip_to_closing_parenthesis (parser,
28328 /*recovering=*/true,
28329 /*or_comma=*/false,
28330 /*consume_paren=*/false);
28334 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28335 type_name = build_tree_list (proto_quals, cp_type);
28338 return type_name;
28341 /* Check to see if TYPE refers to an Objective-C selector name. */
28343 static bool
28344 cp_parser_objc_selector_p (enum cpp_ttype type)
28346 return (type == CPP_NAME || type == CPP_KEYWORD
28347 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28348 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28349 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28350 || type == CPP_XOR || type == CPP_XOR_EQ);
28353 /* Parse an Objective-C selector. */
28355 static tree
28356 cp_parser_objc_selector (cp_parser* parser)
28358 cp_token *token = cp_lexer_consume_token (parser->lexer);
28360 if (!cp_parser_objc_selector_p (token->type))
28362 error_at (token->location, "invalid Objective-C++ selector name");
28363 return error_mark_node;
28366 /* C++ operator names are allowed to appear in ObjC selectors. */
28367 switch (token->type)
28369 case CPP_AND_AND: return get_identifier ("and");
28370 case CPP_AND_EQ: return get_identifier ("and_eq");
28371 case CPP_AND: return get_identifier ("bitand");
28372 case CPP_OR: return get_identifier ("bitor");
28373 case CPP_COMPL: return get_identifier ("compl");
28374 case CPP_NOT: return get_identifier ("not");
28375 case CPP_NOT_EQ: return get_identifier ("not_eq");
28376 case CPP_OR_OR: return get_identifier ("or");
28377 case CPP_OR_EQ: return get_identifier ("or_eq");
28378 case CPP_XOR: return get_identifier ("xor");
28379 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28380 default: return token->u.value;
28384 /* Parse an Objective-C params list. */
28386 static tree
28387 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28389 tree params = NULL_TREE;
28390 bool maybe_unary_selector_p = true;
28391 cp_token *token = cp_lexer_peek_token (parser->lexer);
28393 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28395 tree selector = NULL_TREE, type_name, identifier;
28396 tree parm_attr = NULL_TREE;
28398 if (token->keyword == RID_ATTRIBUTE)
28399 break;
28401 if (token->type != CPP_COLON)
28402 selector = cp_parser_objc_selector (parser);
28404 /* Detect if we have a unary selector. */
28405 if (maybe_unary_selector_p
28406 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28408 params = selector; /* Might be followed by attributes. */
28409 break;
28412 maybe_unary_selector_p = false;
28413 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28415 /* Something went quite wrong. There should be a colon
28416 here, but there is not. Stop parsing parameters. */
28417 break;
28419 type_name = cp_parser_objc_typename (parser);
28420 /* New ObjC allows attributes on parameters too. */
28421 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28422 parm_attr = cp_parser_attributes_opt (parser);
28423 identifier = cp_parser_identifier (parser);
28425 params
28426 = chainon (params,
28427 objc_build_keyword_decl (selector,
28428 type_name,
28429 identifier,
28430 parm_attr));
28432 token = cp_lexer_peek_token (parser->lexer);
28435 if (params == NULL_TREE)
28437 cp_parser_error (parser, "objective-c++ method declaration is expected");
28438 return error_mark_node;
28441 /* We allow tail attributes for the method. */
28442 if (token->keyword == RID_ATTRIBUTE)
28444 *attributes = cp_parser_attributes_opt (parser);
28445 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28446 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28447 return params;
28448 cp_parser_error (parser,
28449 "method attributes must be specified at the end");
28450 return error_mark_node;
28453 if (params == NULL_TREE)
28455 cp_parser_error (parser, "objective-c++ method declaration is expected");
28456 return error_mark_node;
28458 return params;
28461 /* Parse the non-keyword Objective-C params. */
28463 static tree
28464 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28465 tree* attributes)
28467 tree params = make_node (TREE_LIST);
28468 cp_token *token = cp_lexer_peek_token (parser->lexer);
28469 *ellipsisp = false; /* Initially, assume no ellipsis. */
28471 while (token->type == CPP_COMMA)
28473 cp_parameter_declarator *parmdecl;
28474 tree parm;
28476 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28477 token = cp_lexer_peek_token (parser->lexer);
28479 if (token->type == CPP_ELLIPSIS)
28481 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28482 *ellipsisp = true;
28483 token = cp_lexer_peek_token (parser->lexer);
28484 break;
28487 /* TODO: parse attributes for tail parameters. */
28488 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28489 parm = grokdeclarator (parmdecl->declarator,
28490 &parmdecl->decl_specifiers,
28491 PARM, /*initialized=*/0,
28492 /*attrlist=*/NULL);
28494 chainon (params, build_tree_list (NULL_TREE, parm));
28495 token = cp_lexer_peek_token (parser->lexer);
28498 /* We allow tail attributes for the method. */
28499 if (token->keyword == RID_ATTRIBUTE)
28501 if (*attributes == NULL_TREE)
28503 *attributes = cp_parser_attributes_opt (parser);
28504 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28505 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28506 return params;
28508 else
28509 /* We have an error, but parse the attributes, so that we can
28510 carry on. */
28511 *attributes = cp_parser_attributes_opt (parser);
28513 cp_parser_error (parser,
28514 "method attributes must be specified at the end");
28515 return error_mark_node;
28518 return params;
28521 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28523 static void
28524 cp_parser_objc_interstitial_code (cp_parser* parser)
28526 cp_token *token = cp_lexer_peek_token (parser->lexer);
28528 /* If the next token is `extern' and the following token is a string
28529 literal, then we have a linkage specification. */
28530 if (token->keyword == RID_EXTERN
28531 && cp_parser_is_pure_string_literal
28532 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28533 cp_parser_linkage_specification (parser);
28534 /* Handle #pragma, if any. */
28535 else if (token->type == CPP_PRAGMA)
28536 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28537 /* Allow stray semicolons. */
28538 else if (token->type == CPP_SEMICOLON)
28539 cp_lexer_consume_token (parser->lexer);
28540 /* Mark methods as optional or required, when building protocols. */
28541 else if (token->keyword == RID_AT_OPTIONAL)
28543 cp_lexer_consume_token (parser->lexer);
28544 objc_set_method_opt (true);
28546 else if (token->keyword == RID_AT_REQUIRED)
28548 cp_lexer_consume_token (parser->lexer);
28549 objc_set_method_opt (false);
28551 else if (token->keyword == RID_NAMESPACE)
28552 cp_parser_namespace_definition (parser);
28553 /* Other stray characters must generate errors. */
28554 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28556 cp_lexer_consume_token (parser->lexer);
28557 error ("stray %qs between Objective-C++ methods",
28558 token->type == CPP_OPEN_BRACE ? "{" : "}");
28560 /* Finally, try to parse a block-declaration, or a function-definition. */
28561 else
28562 cp_parser_block_declaration (parser, /*statement_p=*/false);
28565 /* Parse a method signature. */
28567 static tree
28568 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28570 tree rettype, kwdparms, optparms;
28571 bool ellipsis = false;
28572 bool is_class_method;
28574 is_class_method = cp_parser_objc_method_type (parser);
28575 rettype = cp_parser_objc_typename (parser);
28576 *attributes = NULL_TREE;
28577 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28578 if (kwdparms == error_mark_node)
28579 return error_mark_node;
28580 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28581 if (optparms == error_mark_node)
28582 return error_mark_node;
28584 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28587 static bool
28588 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28590 tree tattr;
28591 cp_lexer_save_tokens (parser->lexer);
28592 tattr = cp_parser_attributes_opt (parser);
28593 gcc_assert (tattr) ;
28595 /* If the attributes are followed by a method introducer, this is not allowed.
28596 Dump the attributes and flag the situation. */
28597 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28598 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28599 return true;
28601 /* Otherwise, the attributes introduce some interstitial code, possibly so
28602 rewind to allow that check. */
28603 cp_lexer_rollback_tokens (parser->lexer);
28604 return false;
28607 /* Parse an Objective-C method prototype list. */
28609 static void
28610 cp_parser_objc_method_prototype_list (cp_parser* parser)
28612 cp_token *token = cp_lexer_peek_token (parser->lexer);
28614 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28616 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28618 tree attributes, sig;
28619 bool is_class_method;
28620 if (token->type == CPP_PLUS)
28621 is_class_method = true;
28622 else
28623 is_class_method = false;
28624 sig = cp_parser_objc_method_signature (parser, &attributes);
28625 if (sig == error_mark_node)
28627 cp_parser_skip_to_end_of_block_or_statement (parser);
28628 token = cp_lexer_peek_token (parser->lexer);
28629 continue;
28631 objc_add_method_declaration (is_class_method, sig, attributes);
28632 cp_parser_consume_semicolon_at_end_of_statement (parser);
28634 else if (token->keyword == RID_AT_PROPERTY)
28635 cp_parser_objc_at_property_declaration (parser);
28636 else if (token->keyword == RID_ATTRIBUTE
28637 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28638 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28639 OPT_Wattributes,
28640 "prefix attributes are ignored for methods");
28641 else
28642 /* Allow for interspersed non-ObjC++ code. */
28643 cp_parser_objc_interstitial_code (parser);
28645 token = cp_lexer_peek_token (parser->lexer);
28648 if (token->type != CPP_EOF)
28649 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28650 else
28651 cp_parser_error (parser, "expected %<@end%>");
28653 objc_finish_interface ();
28656 /* Parse an Objective-C method definition list. */
28658 static void
28659 cp_parser_objc_method_definition_list (cp_parser* parser)
28661 cp_token *token = cp_lexer_peek_token (parser->lexer);
28663 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28665 tree meth;
28667 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28669 cp_token *ptk;
28670 tree sig, attribute;
28671 bool is_class_method;
28672 if (token->type == CPP_PLUS)
28673 is_class_method = true;
28674 else
28675 is_class_method = false;
28676 push_deferring_access_checks (dk_deferred);
28677 sig = cp_parser_objc_method_signature (parser, &attribute);
28678 if (sig == error_mark_node)
28680 cp_parser_skip_to_end_of_block_or_statement (parser);
28681 token = cp_lexer_peek_token (parser->lexer);
28682 continue;
28684 objc_start_method_definition (is_class_method, sig, attribute,
28685 NULL_TREE);
28687 /* For historical reasons, we accept an optional semicolon. */
28688 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28689 cp_lexer_consume_token (parser->lexer);
28691 ptk = cp_lexer_peek_token (parser->lexer);
28692 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28693 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28695 perform_deferred_access_checks (tf_warning_or_error);
28696 stop_deferring_access_checks ();
28697 meth = cp_parser_function_definition_after_declarator (parser,
28698 false);
28699 pop_deferring_access_checks ();
28700 objc_finish_method_definition (meth);
28703 /* The following case will be removed once @synthesize is
28704 completely implemented. */
28705 else if (token->keyword == RID_AT_PROPERTY)
28706 cp_parser_objc_at_property_declaration (parser);
28707 else if (token->keyword == RID_AT_SYNTHESIZE)
28708 cp_parser_objc_at_synthesize_declaration (parser);
28709 else if (token->keyword == RID_AT_DYNAMIC)
28710 cp_parser_objc_at_dynamic_declaration (parser);
28711 else if (token->keyword == RID_ATTRIBUTE
28712 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28713 warning_at (token->location, OPT_Wattributes,
28714 "prefix attributes are ignored for methods");
28715 else
28716 /* Allow for interspersed non-ObjC++ code. */
28717 cp_parser_objc_interstitial_code (parser);
28719 token = cp_lexer_peek_token (parser->lexer);
28722 if (token->type != CPP_EOF)
28723 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28724 else
28725 cp_parser_error (parser, "expected %<@end%>");
28727 objc_finish_implementation ();
28730 /* Parse Objective-C ivars. */
28732 static void
28733 cp_parser_objc_class_ivars (cp_parser* parser)
28735 cp_token *token = cp_lexer_peek_token (parser->lexer);
28737 if (token->type != CPP_OPEN_BRACE)
28738 return; /* No ivars specified. */
28740 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28741 token = cp_lexer_peek_token (parser->lexer);
28743 while (token->type != CPP_CLOSE_BRACE
28744 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28746 cp_decl_specifier_seq declspecs;
28747 int decl_class_or_enum_p;
28748 tree prefix_attributes;
28750 cp_parser_objc_visibility_spec (parser);
28752 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28753 break;
28755 cp_parser_decl_specifier_seq (parser,
28756 CP_PARSER_FLAGS_OPTIONAL,
28757 &declspecs,
28758 &decl_class_or_enum_p);
28760 /* auto, register, static, extern, mutable. */
28761 if (declspecs.storage_class != sc_none)
28763 cp_parser_error (parser, "invalid type for instance variable");
28764 declspecs.storage_class = sc_none;
28767 /* thread_local. */
28768 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28770 cp_parser_error (parser, "invalid type for instance variable");
28771 declspecs.locations[ds_thread] = 0;
28774 /* typedef. */
28775 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28777 cp_parser_error (parser, "invalid type for instance variable");
28778 declspecs.locations[ds_typedef] = 0;
28781 prefix_attributes = declspecs.attributes;
28782 declspecs.attributes = NULL_TREE;
28784 /* Keep going until we hit the `;' at the end of the
28785 declaration. */
28786 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28788 tree width = NULL_TREE, attributes, first_attribute, decl;
28789 cp_declarator *declarator = NULL;
28790 int ctor_dtor_or_conv_p;
28792 /* Check for a (possibly unnamed) bitfield declaration. */
28793 token = cp_lexer_peek_token (parser->lexer);
28794 if (token->type == CPP_COLON)
28795 goto eat_colon;
28797 if (token->type == CPP_NAME
28798 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28799 == CPP_COLON))
28801 /* Get the name of the bitfield. */
28802 declarator = make_id_declarator (NULL_TREE,
28803 cp_parser_identifier (parser),
28804 sfk_none);
28806 eat_colon:
28807 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28808 /* Get the width of the bitfield. */
28809 width
28810 = cp_parser_constant_expression (parser);
28812 else
28814 /* Parse the declarator. */
28815 declarator
28816 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28817 &ctor_dtor_or_conv_p,
28818 /*parenthesized_p=*/NULL,
28819 /*member_p=*/false,
28820 /*friend_p=*/false);
28823 /* Look for attributes that apply to the ivar. */
28824 attributes = cp_parser_attributes_opt (parser);
28825 /* Remember which attributes are prefix attributes and
28826 which are not. */
28827 first_attribute = attributes;
28828 /* Combine the attributes. */
28829 attributes = chainon (prefix_attributes, attributes);
28831 if (width)
28832 /* Create the bitfield declaration. */
28833 decl = grokbitfield (declarator, &declspecs,
28834 width,
28835 attributes);
28836 else
28837 decl = grokfield (declarator, &declspecs,
28838 NULL_TREE, /*init_const_expr_p=*/false,
28839 NULL_TREE, attributes);
28841 /* Add the instance variable. */
28842 if (decl != error_mark_node && decl != NULL_TREE)
28843 objc_add_instance_variable (decl);
28845 /* Reset PREFIX_ATTRIBUTES. */
28846 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28847 attributes = TREE_CHAIN (attributes);
28848 if (attributes)
28849 TREE_CHAIN (attributes) = NULL_TREE;
28851 token = cp_lexer_peek_token (parser->lexer);
28853 if (token->type == CPP_COMMA)
28855 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28856 continue;
28858 break;
28861 cp_parser_consume_semicolon_at_end_of_statement (parser);
28862 token = cp_lexer_peek_token (parser->lexer);
28865 if (token->keyword == RID_AT_END)
28866 cp_parser_error (parser, "expected %<}%>");
28868 /* Do not consume the RID_AT_END, so it will be read again as terminating
28869 the @interface of @implementation. */
28870 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28871 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28873 /* For historical reasons, we accept an optional semicolon. */
28874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28875 cp_lexer_consume_token (parser->lexer);
28878 /* Parse an Objective-C protocol declaration. */
28880 static void
28881 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28883 tree proto, protorefs;
28884 cp_token *tok;
28886 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28889 tok = cp_lexer_peek_token (parser->lexer);
28890 error_at (tok->location, "identifier expected after %<@protocol%>");
28891 cp_parser_consume_semicolon_at_end_of_statement (parser);
28892 return;
28895 /* See if we have a forward declaration or a definition. */
28896 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28898 /* Try a forward declaration first. */
28899 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28901 while (true)
28903 tree id;
28905 id = cp_parser_identifier (parser);
28906 if (id == error_mark_node)
28907 break;
28909 objc_declare_protocol (id, attributes);
28911 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28912 cp_lexer_consume_token (parser->lexer);
28913 else
28914 break;
28916 cp_parser_consume_semicolon_at_end_of_statement (parser);
28919 /* Ok, we got a full-fledged definition (or at least should). */
28920 else
28922 proto = cp_parser_identifier (parser);
28923 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28924 objc_start_protocol (proto, protorefs, attributes);
28925 cp_parser_objc_method_prototype_list (parser);
28929 /* Parse an Objective-C superclass or category. */
28931 static void
28932 cp_parser_objc_superclass_or_category (cp_parser *parser,
28933 bool iface_p,
28934 tree *super,
28935 tree *categ, bool *is_class_extension)
28937 cp_token *next = cp_lexer_peek_token (parser->lexer);
28939 *super = *categ = NULL_TREE;
28940 *is_class_extension = false;
28941 if (next->type == CPP_COLON)
28943 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28944 *super = cp_parser_identifier (parser);
28946 else if (next->type == CPP_OPEN_PAREN)
28948 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28950 /* If there is no category name, and this is an @interface, we
28951 have a class extension. */
28952 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28954 *categ = NULL_TREE;
28955 *is_class_extension = true;
28957 else
28958 *categ = cp_parser_identifier (parser);
28960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28964 /* Parse an Objective-C class interface. */
28966 static void
28967 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28969 tree name, super, categ, protos;
28970 bool is_class_extension;
28972 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28973 name = cp_parser_identifier (parser);
28974 if (name == error_mark_node)
28976 /* It's hard to recover because even if valid @interface stuff
28977 is to follow, we can't compile it (or validate it) if we
28978 don't even know which class it refers to. Let's assume this
28979 was a stray '@interface' token in the stream and skip it.
28981 return;
28983 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28984 &is_class_extension);
28985 protos = cp_parser_objc_protocol_refs_opt (parser);
28987 /* We have either a class or a category on our hands. */
28988 if (categ || is_class_extension)
28989 objc_start_category_interface (name, categ, protos, attributes);
28990 else
28992 objc_start_class_interface (name, super, protos, attributes);
28993 /* Handle instance variable declarations, if any. */
28994 cp_parser_objc_class_ivars (parser);
28995 objc_continue_interface ();
28998 cp_parser_objc_method_prototype_list (parser);
29001 /* Parse an Objective-C class implementation. */
29003 static void
29004 cp_parser_objc_class_implementation (cp_parser* parser)
29006 tree name, super, categ;
29007 bool is_class_extension;
29009 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29010 name = cp_parser_identifier (parser);
29011 if (name == error_mark_node)
29013 /* It's hard to recover because even if valid @implementation
29014 stuff is to follow, we can't compile it (or validate it) if
29015 we don't even know which class it refers to. Let's assume
29016 this was a stray '@implementation' token in the stream and
29017 skip it.
29019 return;
29021 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29022 &is_class_extension);
29024 /* We have either a class or a category on our hands. */
29025 if (categ)
29026 objc_start_category_implementation (name, categ);
29027 else
29029 objc_start_class_implementation (name, super);
29030 /* Handle instance variable declarations, if any. */
29031 cp_parser_objc_class_ivars (parser);
29032 objc_continue_implementation ();
29035 cp_parser_objc_method_definition_list (parser);
29038 /* Consume the @end token and finish off the implementation. */
29040 static void
29041 cp_parser_objc_end_implementation (cp_parser* parser)
29043 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29044 objc_finish_implementation ();
29047 /* Parse an Objective-C declaration. */
29049 static void
29050 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29052 /* Try to figure out what kind of declaration is present. */
29053 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29055 if (attributes)
29056 switch (kwd->keyword)
29058 case RID_AT_ALIAS:
29059 case RID_AT_CLASS:
29060 case RID_AT_END:
29061 error_at (kwd->location, "attributes may not be specified before"
29062 " the %<@%D%> Objective-C++ keyword",
29063 kwd->u.value);
29064 attributes = NULL;
29065 break;
29066 case RID_AT_IMPLEMENTATION:
29067 warning_at (kwd->location, OPT_Wattributes,
29068 "prefix attributes are ignored before %<@%D%>",
29069 kwd->u.value);
29070 attributes = NULL;
29071 default:
29072 break;
29075 switch (kwd->keyword)
29077 case RID_AT_ALIAS:
29078 cp_parser_objc_alias_declaration (parser);
29079 break;
29080 case RID_AT_CLASS:
29081 cp_parser_objc_class_declaration (parser);
29082 break;
29083 case RID_AT_PROTOCOL:
29084 cp_parser_objc_protocol_declaration (parser, attributes);
29085 break;
29086 case RID_AT_INTERFACE:
29087 cp_parser_objc_class_interface (parser, attributes);
29088 break;
29089 case RID_AT_IMPLEMENTATION:
29090 cp_parser_objc_class_implementation (parser);
29091 break;
29092 case RID_AT_END:
29093 cp_parser_objc_end_implementation (parser);
29094 break;
29095 default:
29096 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29097 kwd->u.value);
29098 cp_parser_skip_to_end_of_block_or_statement (parser);
29102 /* Parse an Objective-C try-catch-finally statement.
29104 objc-try-catch-finally-stmt:
29105 @try compound-statement objc-catch-clause-seq [opt]
29106 objc-finally-clause [opt]
29108 objc-catch-clause-seq:
29109 objc-catch-clause objc-catch-clause-seq [opt]
29111 objc-catch-clause:
29112 @catch ( objc-exception-declaration ) compound-statement
29114 objc-finally-clause:
29115 @finally compound-statement
29117 objc-exception-declaration:
29118 parameter-declaration
29119 '...'
29121 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29123 Returns NULL_TREE.
29125 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29126 for C. Keep them in sync. */
29128 static tree
29129 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29131 location_t location;
29132 tree stmt;
29134 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29135 location = cp_lexer_peek_token (parser->lexer)->location;
29136 objc_maybe_warn_exceptions (location);
29137 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29138 node, lest it get absorbed into the surrounding block. */
29139 stmt = push_stmt_list ();
29140 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29141 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29143 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29145 cp_parameter_declarator *parm;
29146 tree parameter_declaration = error_mark_node;
29147 bool seen_open_paren = false;
29149 cp_lexer_consume_token (parser->lexer);
29150 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29151 seen_open_paren = true;
29152 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29154 /* We have "@catch (...)" (where the '...' are literally
29155 what is in the code). Skip the '...'.
29156 parameter_declaration is set to NULL_TREE, and
29157 objc_being_catch_clauses() knows that that means
29158 '...'. */
29159 cp_lexer_consume_token (parser->lexer);
29160 parameter_declaration = NULL_TREE;
29162 else
29164 /* We have "@catch (NSException *exception)" or something
29165 like that. Parse the parameter declaration. */
29166 parm = cp_parser_parameter_declaration (parser, false, NULL);
29167 if (parm == NULL)
29168 parameter_declaration = error_mark_node;
29169 else
29170 parameter_declaration = grokdeclarator (parm->declarator,
29171 &parm->decl_specifiers,
29172 PARM, /*initialized=*/0,
29173 /*attrlist=*/NULL);
29175 if (seen_open_paren)
29176 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29177 else
29179 /* If there was no open parenthesis, we are recovering from
29180 an error, and we are trying to figure out what mistake
29181 the user has made. */
29183 /* If there is an immediate closing parenthesis, the user
29184 probably forgot the opening one (ie, they typed "@catch
29185 NSException *e)". Parse the closing parenthesis and keep
29186 going. */
29187 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29188 cp_lexer_consume_token (parser->lexer);
29190 /* If these is no immediate closing parenthesis, the user
29191 probably doesn't know that parenthesis are required at
29192 all (ie, they typed "@catch NSException *e"). So, just
29193 forget about the closing parenthesis and keep going. */
29195 objc_begin_catch_clause (parameter_declaration);
29196 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29197 objc_finish_catch_clause ();
29199 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29201 cp_lexer_consume_token (parser->lexer);
29202 location = cp_lexer_peek_token (parser->lexer)->location;
29203 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29204 node, lest it get absorbed into the surrounding block. */
29205 stmt = push_stmt_list ();
29206 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29207 objc_build_finally_clause (location, pop_stmt_list (stmt));
29210 return objc_finish_try_stmt ();
29213 /* Parse an Objective-C synchronized statement.
29215 objc-synchronized-stmt:
29216 @synchronized ( expression ) compound-statement
29218 Returns NULL_TREE. */
29220 static tree
29221 cp_parser_objc_synchronized_statement (cp_parser *parser)
29223 location_t location;
29224 tree lock, stmt;
29226 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29228 location = cp_lexer_peek_token (parser->lexer)->location;
29229 objc_maybe_warn_exceptions (location);
29230 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29231 lock = cp_parser_expression (parser);
29232 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29234 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29235 node, lest it get absorbed into the surrounding block. */
29236 stmt = push_stmt_list ();
29237 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29239 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29242 /* Parse an Objective-C throw statement.
29244 objc-throw-stmt:
29245 @throw assignment-expression [opt] ;
29247 Returns a constructed '@throw' statement. */
29249 static tree
29250 cp_parser_objc_throw_statement (cp_parser *parser)
29252 tree expr = NULL_TREE;
29253 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29255 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29258 expr = cp_parser_expression (parser);
29260 cp_parser_consume_semicolon_at_end_of_statement (parser);
29262 return objc_build_throw_stmt (loc, expr);
29265 /* Parse an Objective-C statement. */
29267 static tree
29268 cp_parser_objc_statement (cp_parser * parser)
29270 /* Try to figure out what kind of declaration is present. */
29271 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29273 switch (kwd->keyword)
29275 case RID_AT_TRY:
29276 return cp_parser_objc_try_catch_finally_statement (parser);
29277 case RID_AT_SYNCHRONIZED:
29278 return cp_parser_objc_synchronized_statement (parser);
29279 case RID_AT_THROW:
29280 return cp_parser_objc_throw_statement (parser);
29281 default:
29282 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29283 kwd->u.value);
29284 cp_parser_skip_to_end_of_block_or_statement (parser);
29287 return error_mark_node;
29290 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29291 look ahead to see if an objc keyword follows the attributes. This
29292 is to detect the use of prefix attributes on ObjC @interface and
29293 @protocol. */
29295 static bool
29296 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29298 cp_lexer_save_tokens (parser->lexer);
29299 *attrib = cp_parser_attributes_opt (parser);
29300 gcc_assert (*attrib);
29301 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29303 cp_lexer_commit_tokens (parser->lexer);
29304 return true;
29306 cp_lexer_rollback_tokens (parser->lexer);
29307 return false;
29310 /* This routine is a minimal replacement for
29311 c_parser_struct_declaration () used when parsing the list of
29312 types/names or ObjC++ properties. For example, when parsing the
29313 code
29315 @property (readonly) int a, b, c;
29317 this function is responsible for parsing "int a, int b, int c" and
29318 returning the declarations as CHAIN of DECLs.
29320 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29321 similar parsing. */
29322 static tree
29323 cp_parser_objc_struct_declaration (cp_parser *parser)
29325 tree decls = NULL_TREE;
29326 cp_decl_specifier_seq declspecs;
29327 int decl_class_or_enum_p;
29328 tree prefix_attributes;
29330 cp_parser_decl_specifier_seq (parser,
29331 CP_PARSER_FLAGS_NONE,
29332 &declspecs,
29333 &decl_class_or_enum_p);
29335 if (declspecs.type == error_mark_node)
29336 return error_mark_node;
29338 /* auto, register, static, extern, mutable. */
29339 if (declspecs.storage_class != sc_none)
29341 cp_parser_error (parser, "invalid type for property");
29342 declspecs.storage_class = sc_none;
29345 /* thread_local. */
29346 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29348 cp_parser_error (parser, "invalid type for property");
29349 declspecs.locations[ds_thread] = 0;
29352 /* typedef. */
29353 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29355 cp_parser_error (parser, "invalid type for property");
29356 declspecs.locations[ds_typedef] = 0;
29359 prefix_attributes = declspecs.attributes;
29360 declspecs.attributes = NULL_TREE;
29362 /* Keep going until we hit the `;' at the end of the declaration. */
29363 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29365 tree attributes, first_attribute, decl;
29366 cp_declarator *declarator;
29367 cp_token *token;
29369 /* Parse the declarator. */
29370 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29371 NULL, NULL, false, false);
29373 /* Look for attributes that apply to the ivar. */
29374 attributes = cp_parser_attributes_opt (parser);
29375 /* Remember which attributes are prefix attributes and
29376 which are not. */
29377 first_attribute = attributes;
29378 /* Combine the attributes. */
29379 attributes = chainon (prefix_attributes, attributes);
29381 decl = grokfield (declarator, &declspecs,
29382 NULL_TREE, /*init_const_expr_p=*/false,
29383 NULL_TREE, attributes);
29385 if (decl == error_mark_node || decl == NULL_TREE)
29386 return error_mark_node;
29388 /* Reset PREFIX_ATTRIBUTES. */
29389 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29390 attributes = TREE_CHAIN (attributes);
29391 if (attributes)
29392 TREE_CHAIN (attributes) = NULL_TREE;
29394 DECL_CHAIN (decl) = decls;
29395 decls = decl;
29397 token = cp_lexer_peek_token (parser->lexer);
29398 if (token->type == CPP_COMMA)
29400 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29401 continue;
29403 else
29404 break;
29406 return decls;
29409 /* Parse an Objective-C @property declaration. The syntax is:
29411 objc-property-declaration:
29412 '@property' objc-property-attributes[opt] struct-declaration ;
29414 objc-property-attributes:
29415 '(' objc-property-attribute-list ')'
29417 objc-property-attribute-list:
29418 objc-property-attribute
29419 objc-property-attribute-list, objc-property-attribute
29421 objc-property-attribute
29422 'getter' = identifier
29423 'setter' = identifier
29424 'readonly'
29425 'readwrite'
29426 'assign'
29427 'retain'
29428 'copy'
29429 'nonatomic'
29431 For example:
29432 @property NSString *name;
29433 @property (readonly) id object;
29434 @property (retain, nonatomic, getter=getTheName) id name;
29435 @property int a, b, c;
29437 PS: This function is identical to
29438 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29439 static void
29440 cp_parser_objc_at_property_declaration (cp_parser *parser)
29442 /* The following variables hold the attributes of the properties as
29443 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29444 seen. When we see an attribute, we set them to 'true' (if they
29445 are boolean properties) or to the identifier (if they have an
29446 argument, ie, for getter and setter). Note that here we only
29447 parse the list of attributes, check the syntax and accumulate the
29448 attributes that we find. objc_add_property_declaration() will
29449 then process the information. */
29450 bool property_assign = false;
29451 bool property_copy = false;
29452 tree property_getter_ident = NULL_TREE;
29453 bool property_nonatomic = false;
29454 bool property_readonly = false;
29455 bool property_readwrite = false;
29456 bool property_retain = false;
29457 tree property_setter_ident = NULL_TREE;
29459 /* 'properties' is the list of properties that we read. Usually a
29460 single one, but maybe more (eg, in "@property int a, b, c;" there
29461 are three). */
29462 tree properties;
29463 location_t loc;
29465 loc = cp_lexer_peek_token (parser->lexer)->location;
29467 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29469 /* Parse the optional attribute list... */
29470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29472 /* Eat the '('. */
29473 cp_lexer_consume_token (parser->lexer);
29475 while (true)
29477 bool syntax_error = false;
29478 cp_token *token = cp_lexer_peek_token (parser->lexer);
29479 enum rid keyword;
29481 if (token->type != CPP_NAME)
29483 cp_parser_error (parser, "expected identifier");
29484 break;
29486 keyword = C_RID_CODE (token->u.value);
29487 cp_lexer_consume_token (parser->lexer);
29488 switch (keyword)
29490 case RID_ASSIGN: property_assign = true; break;
29491 case RID_COPY: property_copy = true; break;
29492 case RID_NONATOMIC: property_nonatomic = true; break;
29493 case RID_READONLY: property_readonly = true; break;
29494 case RID_READWRITE: property_readwrite = true; break;
29495 case RID_RETAIN: property_retain = true; break;
29497 case RID_GETTER:
29498 case RID_SETTER:
29499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29501 if (keyword == RID_GETTER)
29502 cp_parser_error (parser,
29503 "missing %<=%> (after %<getter%> attribute)");
29504 else
29505 cp_parser_error (parser,
29506 "missing %<=%> (after %<setter%> attribute)");
29507 syntax_error = true;
29508 break;
29510 cp_lexer_consume_token (parser->lexer); /* eat the = */
29511 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29513 cp_parser_error (parser, "expected identifier");
29514 syntax_error = true;
29515 break;
29517 if (keyword == RID_SETTER)
29519 if (property_setter_ident != NULL_TREE)
29521 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29522 cp_lexer_consume_token (parser->lexer);
29524 else
29525 property_setter_ident = cp_parser_objc_selector (parser);
29526 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29527 cp_parser_error (parser, "setter name must terminate with %<:%>");
29528 else
29529 cp_lexer_consume_token (parser->lexer);
29531 else
29533 if (property_getter_ident != NULL_TREE)
29535 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29536 cp_lexer_consume_token (parser->lexer);
29538 else
29539 property_getter_ident = cp_parser_objc_selector (parser);
29541 break;
29542 default:
29543 cp_parser_error (parser, "unknown property attribute");
29544 syntax_error = true;
29545 break;
29548 if (syntax_error)
29549 break;
29551 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29552 cp_lexer_consume_token (parser->lexer);
29553 else
29554 break;
29557 /* FIXME: "@property (setter, assign);" will generate a spurious
29558 "error: expected ‘)’ before ‘,’ token". This is because
29559 cp_parser_require, unlike the C counterpart, will produce an
29560 error even if we are in error recovery. */
29561 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29563 cp_parser_skip_to_closing_parenthesis (parser,
29564 /*recovering=*/true,
29565 /*or_comma=*/false,
29566 /*consume_paren=*/true);
29570 /* ... and the property declaration(s). */
29571 properties = cp_parser_objc_struct_declaration (parser);
29573 if (properties == error_mark_node)
29575 cp_parser_skip_to_end_of_statement (parser);
29576 /* If the next token is now a `;', consume it. */
29577 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29578 cp_lexer_consume_token (parser->lexer);
29579 return;
29582 if (properties == NULL_TREE)
29583 cp_parser_error (parser, "expected identifier");
29584 else
29586 /* Comma-separated properties are chained together in
29587 reverse order; add them one by one. */
29588 properties = nreverse (properties);
29590 for (; properties; properties = TREE_CHAIN (properties))
29591 objc_add_property_declaration (loc, copy_node (properties),
29592 property_readonly, property_readwrite,
29593 property_assign, property_retain,
29594 property_copy, property_nonatomic,
29595 property_getter_ident, property_setter_ident);
29598 cp_parser_consume_semicolon_at_end_of_statement (parser);
29601 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29603 objc-synthesize-declaration:
29604 @synthesize objc-synthesize-identifier-list ;
29606 objc-synthesize-identifier-list:
29607 objc-synthesize-identifier
29608 objc-synthesize-identifier-list, objc-synthesize-identifier
29610 objc-synthesize-identifier
29611 identifier
29612 identifier = identifier
29614 For example:
29615 @synthesize MyProperty;
29616 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29618 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29619 for C. Keep them in sync.
29621 static void
29622 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29624 tree list = NULL_TREE;
29625 location_t loc;
29626 loc = cp_lexer_peek_token (parser->lexer)->location;
29628 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29629 while (true)
29631 tree property, ivar;
29632 property = cp_parser_identifier (parser);
29633 if (property == error_mark_node)
29635 cp_parser_consume_semicolon_at_end_of_statement (parser);
29636 return;
29638 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29640 cp_lexer_consume_token (parser->lexer);
29641 ivar = cp_parser_identifier (parser);
29642 if (ivar == error_mark_node)
29644 cp_parser_consume_semicolon_at_end_of_statement (parser);
29645 return;
29648 else
29649 ivar = NULL_TREE;
29650 list = chainon (list, build_tree_list (ivar, property));
29651 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29652 cp_lexer_consume_token (parser->lexer);
29653 else
29654 break;
29656 cp_parser_consume_semicolon_at_end_of_statement (parser);
29657 objc_add_synthesize_declaration (loc, list);
29660 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29662 objc-dynamic-declaration:
29663 @dynamic identifier-list ;
29665 For example:
29666 @dynamic MyProperty;
29667 @dynamic MyProperty, AnotherProperty;
29669 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29670 for C. Keep them in sync.
29672 static void
29673 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29675 tree list = NULL_TREE;
29676 location_t loc;
29677 loc = cp_lexer_peek_token (parser->lexer)->location;
29679 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29680 while (true)
29682 tree property;
29683 property = cp_parser_identifier (parser);
29684 if (property == error_mark_node)
29686 cp_parser_consume_semicolon_at_end_of_statement (parser);
29687 return;
29689 list = chainon (list, build_tree_list (NULL, property));
29690 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29691 cp_lexer_consume_token (parser->lexer);
29692 else
29693 break;
29695 cp_parser_consume_semicolon_at_end_of_statement (parser);
29696 objc_add_dynamic_declaration (loc, list);
29700 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29702 /* Returns name of the next clause.
29703 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29704 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29705 returned and the token is consumed. */
29707 static pragma_omp_clause
29708 cp_parser_omp_clause_name (cp_parser *parser)
29710 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29712 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29713 result = PRAGMA_OACC_CLAUSE_AUTO;
29714 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29715 result = PRAGMA_OMP_CLAUSE_IF;
29716 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29717 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29718 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29719 result = PRAGMA_OACC_CLAUSE_DELETE;
29720 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29721 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29722 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29723 result = PRAGMA_OMP_CLAUSE_FOR;
29724 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29726 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29727 const char *p = IDENTIFIER_POINTER (id);
29729 switch (p[0])
29731 case 'a':
29732 if (!strcmp ("aligned", p))
29733 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29734 else if (!strcmp ("async", p))
29735 result = PRAGMA_OACC_CLAUSE_ASYNC;
29736 break;
29737 case 'c':
29738 if (!strcmp ("collapse", p))
29739 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29740 else if (!strcmp ("copy", p))
29741 result = PRAGMA_OACC_CLAUSE_COPY;
29742 else if (!strcmp ("copyin", p))
29743 result = PRAGMA_OMP_CLAUSE_COPYIN;
29744 else if (!strcmp ("copyout", p))
29745 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29746 else if (!strcmp ("copyprivate", p))
29747 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29748 else if (!strcmp ("create", p))
29749 result = PRAGMA_OACC_CLAUSE_CREATE;
29750 break;
29751 case 'd':
29752 if (!strcmp ("defaultmap", p))
29753 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29754 else if (!strcmp ("depend", p))
29755 result = PRAGMA_OMP_CLAUSE_DEPEND;
29756 else if (!strcmp ("device", p))
29757 result = PRAGMA_OMP_CLAUSE_DEVICE;
29758 else if (!strcmp ("deviceptr", p))
29759 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29760 else if (!strcmp ("device_resident", p))
29761 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29762 else if (!strcmp ("dist_schedule", p))
29763 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29764 break;
29765 case 'f':
29766 if (!strcmp ("final", p))
29767 result = PRAGMA_OMP_CLAUSE_FINAL;
29768 else if (!strcmp ("firstprivate", p))
29769 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29770 else if (!strcmp ("from", p))
29771 result = PRAGMA_OMP_CLAUSE_FROM;
29772 break;
29773 case 'g':
29774 if (!strcmp ("gang", p))
29775 result = PRAGMA_OACC_CLAUSE_GANG;
29776 else if (!strcmp ("grainsize", p))
29777 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29778 break;
29779 case 'h':
29780 if (!strcmp ("hint", p))
29781 result = PRAGMA_OMP_CLAUSE_HINT;
29782 else if (!strcmp ("host", p))
29783 result = PRAGMA_OACC_CLAUSE_HOST;
29784 break;
29785 case 'i':
29786 if (!strcmp ("inbranch", p))
29787 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29788 else if (!strcmp ("independent", p))
29789 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29790 else if (!strcmp ("is_device_ptr", p))
29791 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29792 break;
29793 case 'l':
29794 if (!strcmp ("lastprivate", p))
29795 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29796 else if (!strcmp ("linear", p))
29797 result = PRAGMA_OMP_CLAUSE_LINEAR;
29798 else if (!strcmp ("link", p))
29799 result = PRAGMA_OMP_CLAUSE_LINK;
29800 break;
29801 case 'm':
29802 if (!strcmp ("map", p))
29803 result = PRAGMA_OMP_CLAUSE_MAP;
29804 else if (!strcmp ("mergeable", p))
29805 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29806 else if (flag_cilkplus && !strcmp ("mask", p))
29807 result = PRAGMA_CILK_CLAUSE_MASK;
29808 break;
29809 case 'n':
29810 if (!strcmp ("nogroup", p))
29811 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29812 else if (!strcmp ("notinbranch", p))
29813 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29814 else if (!strcmp ("nowait", p))
29815 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29816 else if (flag_cilkplus && !strcmp ("nomask", p))
29817 result = PRAGMA_CILK_CLAUSE_NOMASK;
29818 else if (!strcmp ("num_gangs", p))
29819 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29820 else if (!strcmp ("num_tasks", p))
29821 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29822 else if (!strcmp ("num_teams", p))
29823 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29824 else if (!strcmp ("num_threads", p))
29825 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29826 else if (!strcmp ("num_workers", p))
29827 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29828 break;
29829 case 'o':
29830 if (!strcmp ("ordered", p))
29831 result = PRAGMA_OMP_CLAUSE_ORDERED;
29832 break;
29833 case 'p':
29834 if (!strcmp ("parallel", p))
29835 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29836 else if (!strcmp ("present", p))
29837 result = PRAGMA_OACC_CLAUSE_PRESENT;
29838 else if (!strcmp ("present_or_copy", p)
29839 || !strcmp ("pcopy", p))
29840 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29841 else if (!strcmp ("present_or_copyin", p)
29842 || !strcmp ("pcopyin", p))
29843 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29844 else if (!strcmp ("present_or_copyout", p)
29845 || !strcmp ("pcopyout", p))
29846 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29847 else if (!strcmp ("present_or_create", p)
29848 || !strcmp ("pcreate", p))
29849 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29850 else if (!strcmp ("priority", p))
29851 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29852 else if (!strcmp ("proc_bind", p))
29853 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29854 break;
29855 case 'r':
29856 if (!strcmp ("reduction", p))
29857 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29858 break;
29859 case 's':
29860 if (!strcmp ("safelen", p))
29861 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29862 else if (!strcmp ("schedule", p))
29863 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29864 else if (!strcmp ("sections", p))
29865 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29866 else if (!strcmp ("self", p))
29867 result = PRAGMA_OACC_CLAUSE_SELF;
29868 else if (!strcmp ("seq", p))
29869 result = PRAGMA_OACC_CLAUSE_SEQ;
29870 else if (!strcmp ("shared", p))
29871 result = PRAGMA_OMP_CLAUSE_SHARED;
29872 else if (!strcmp ("simd", p))
29873 result = PRAGMA_OMP_CLAUSE_SIMD;
29874 else if (!strcmp ("simdlen", p))
29875 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29876 break;
29877 case 't':
29878 if (!strcmp ("taskgroup", p))
29879 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29880 else if (!strcmp ("thread_limit", p))
29881 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29882 else if (!strcmp ("threads", p))
29883 result = PRAGMA_OMP_CLAUSE_THREADS;
29884 else if (!strcmp ("tile", p))
29885 result = PRAGMA_OACC_CLAUSE_TILE;
29886 else if (!strcmp ("to", p))
29887 result = PRAGMA_OMP_CLAUSE_TO;
29888 break;
29889 case 'u':
29890 if (!strcmp ("uniform", p))
29891 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29892 else if (!strcmp ("untied", p))
29893 result = PRAGMA_OMP_CLAUSE_UNTIED;
29894 else if (!strcmp ("use_device", p))
29895 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29896 else if (!strcmp ("use_device_ptr", p))
29897 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29898 break;
29899 case 'v':
29900 if (!strcmp ("vector", p))
29901 result = PRAGMA_OACC_CLAUSE_VECTOR;
29902 else if (!strcmp ("vector_length", p))
29903 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29904 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29905 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29906 break;
29907 case 'w':
29908 if (!strcmp ("wait", p))
29909 result = PRAGMA_OACC_CLAUSE_WAIT;
29910 else if (!strcmp ("worker", p))
29911 result = PRAGMA_OACC_CLAUSE_WORKER;
29912 break;
29916 if (result != PRAGMA_OMP_CLAUSE_NONE)
29917 cp_lexer_consume_token (parser->lexer);
29919 return result;
29922 /* Validate that a clause of the given type does not already exist. */
29924 static void
29925 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29926 const char *name, location_t location)
29928 tree c;
29930 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29931 if (OMP_CLAUSE_CODE (c) == code)
29933 error_at (location, "too many %qs clauses", name);
29934 break;
29938 /* OpenMP 2.5:
29939 variable-list:
29940 identifier
29941 variable-list , identifier
29943 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29944 colon). An opening parenthesis will have been consumed by the caller.
29946 If KIND is nonzero, create the appropriate node and install the decl
29947 in OMP_CLAUSE_DECL and add the node to the head of the list.
29949 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29950 return the list created.
29952 COLON can be NULL if only closing parenthesis should end the list,
29953 or pointer to bool which will receive false if the list is terminated
29954 by closing parenthesis or true if the list is terminated by colon. */
29956 static tree
29957 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29958 tree list, bool *colon)
29960 cp_token *token;
29961 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29962 if (colon)
29964 parser->colon_corrects_to_scope_p = false;
29965 *colon = false;
29967 while (1)
29969 tree name, decl;
29971 token = cp_lexer_peek_token (parser->lexer);
29972 if (kind != 0
29973 && current_class_ptr
29974 && cp_parser_is_keyword (token, RID_THIS))
29976 decl = finish_this_expr ();
29977 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29978 || CONVERT_EXPR_P (decl))
29979 decl = TREE_OPERAND (decl, 0);
29980 cp_lexer_consume_token (parser->lexer);
29982 else
29984 name = cp_parser_id_expression (parser, /*template_p=*/false,
29985 /*check_dependency_p=*/true,
29986 /*template_p=*/NULL,
29987 /*declarator_p=*/false,
29988 /*optional_p=*/false);
29989 if (name == error_mark_node)
29990 goto skip_comma;
29992 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29993 if (decl == error_mark_node)
29994 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29995 token->location);
29997 if (decl == error_mark_node)
29999 else if (kind != 0)
30001 switch (kind)
30003 case OMP_CLAUSE__CACHE_:
30004 /* The OpenACC cache directive explicitly only allows "array
30005 elements or subarrays". */
30006 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30008 error_at (token->location, "expected %<[%>");
30009 decl = error_mark_node;
30010 break;
30012 /* FALLTHROUGH. */
30013 case OMP_CLAUSE_MAP:
30014 case OMP_CLAUSE_FROM:
30015 case OMP_CLAUSE_TO:
30016 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30018 location_t loc
30019 = cp_lexer_peek_token (parser->lexer)->location;
30020 cp_id_kind idk = CP_ID_KIND_NONE;
30021 cp_lexer_consume_token (parser->lexer);
30022 decl = convert_from_reference (decl);
30023 decl
30024 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30025 decl, false,
30026 &idk, loc);
30028 /* FALLTHROUGH. */
30029 case OMP_CLAUSE_DEPEND:
30030 case OMP_CLAUSE_REDUCTION:
30031 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30033 tree low_bound = NULL_TREE, length = NULL_TREE;
30035 parser->colon_corrects_to_scope_p = false;
30036 cp_lexer_consume_token (parser->lexer);
30037 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30038 low_bound = cp_parser_expression (parser);
30039 if (!colon)
30040 parser->colon_corrects_to_scope_p
30041 = saved_colon_corrects_to_scope_p;
30042 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30043 length = integer_one_node;
30044 else
30046 /* Look for `:'. */
30047 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30048 goto skip_comma;
30049 if (!cp_lexer_next_token_is (parser->lexer,
30050 CPP_CLOSE_SQUARE))
30051 length = cp_parser_expression (parser);
30053 /* Look for the closing `]'. */
30054 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30055 RT_CLOSE_SQUARE))
30056 goto skip_comma;
30058 decl = tree_cons (low_bound, length, decl);
30060 break;
30061 default:
30062 break;
30065 tree u = build_omp_clause (token->location, kind);
30066 OMP_CLAUSE_DECL (u) = decl;
30067 OMP_CLAUSE_CHAIN (u) = list;
30068 list = u;
30070 else
30071 list = tree_cons (decl, NULL_TREE, list);
30073 get_comma:
30074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30075 break;
30076 cp_lexer_consume_token (parser->lexer);
30079 if (colon)
30080 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30082 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30084 *colon = true;
30085 cp_parser_require (parser, CPP_COLON, RT_COLON);
30086 return list;
30089 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30091 int ending;
30093 /* Try to resync to an unnested comma. Copied from
30094 cp_parser_parenthesized_expression_list. */
30095 skip_comma:
30096 if (colon)
30097 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30098 ending = cp_parser_skip_to_closing_parenthesis (parser,
30099 /*recovering=*/true,
30100 /*or_comma=*/true,
30101 /*consume_paren=*/true);
30102 if (ending < 0)
30103 goto get_comma;
30106 return list;
30109 /* Similarly, but expect leading and trailing parenthesis. This is a very
30110 common case for omp clauses. */
30112 static tree
30113 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30115 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30116 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30117 return list;
30120 /* OpenACC 2.0:
30121 copy ( variable-list )
30122 copyin ( variable-list )
30123 copyout ( variable-list )
30124 create ( variable-list )
30125 delete ( variable-list )
30126 present ( variable-list )
30127 present_or_copy ( variable-list )
30128 pcopy ( variable-list )
30129 present_or_copyin ( variable-list )
30130 pcopyin ( variable-list )
30131 present_or_copyout ( variable-list )
30132 pcopyout ( variable-list )
30133 present_or_create ( variable-list )
30134 pcreate ( variable-list ) */
30136 static tree
30137 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30138 tree list)
30140 enum gomp_map_kind kind;
30141 switch (c_kind)
30143 case PRAGMA_OACC_CLAUSE_COPY:
30144 kind = GOMP_MAP_FORCE_TOFROM;
30145 break;
30146 case PRAGMA_OACC_CLAUSE_COPYIN:
30147 kind = GOMP_MAP_FORCE_TO;
30148 break;
30149 case PRAGMA_OACC_CLAUSE_COPYOUT:
30150 kind = GOMP_MAP_FORCE_FROM;
30151 break;
30152 case PRAGMA_OACC_CLAUSE_CREATE:
30153 kind = GOMP_MAP_FORCE_ALLOC;
30154 break;
30155 case PRAGMA_OACC_CLAUSE_DELETE:
30156 kind = GOMP_MAP_DELETE;
30157 break;
30158 case PRAGMA_OACC_CLAUSE_DEVICE:
30159 kind = GOMP_MAP_FORCE_TO;
30160 break;
30161 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30162 kind = GOMP_MAP_DEVICE_RESIDENT;
30163 break;
30164 case PRAGMA_OACC_CLAUSE_HOST:
30165 case PRAGMA_OACC_CLAUSE_SELF:
30166 kind = GOMP_MAP_FORCE_FROM;
30167 break;
30168 case PRAGMA_OACC_CLAUSE_LINK:
30169 kind = GOMP_MAP_LINK;
30170 break;
30171 case PRAGMA_OACC_CLAUSE_PRESENT:
30172 kind = GOMP_MAP_FORCE_PRESENT;
30173 break;
30174 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30175 kind = GOMP_MAP_TOFROM;
30176 break;
30177 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30178 kind = GOMP_MAP_TO;
30179 break;
30180 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30181 kind = GOMP_MAP_FROM;
30182 break;
30183 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30184 kind = GOMP_MAP_ALLOC;
30185 break;
30186 default:
30187 gcc_unreachable ();
30189 tree nl, c;
30190 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30192 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30193 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30195 return nl;
30198 /* OpenACC 2.0:
30199 deviceptr ( variable-list ) */
30201 static tree
30202 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30204 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30205 tree vars, t;
30207 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30208 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30209 variable-list must only allow for pointer variables. */
30210 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30211 for (t = vars; t; t = TREE_CHAIN (t))
30213 tree v = TREE_PURPOSE (t);
30214 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30215 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30216 OMP_CLAUSE_DECL (u) = v;
30217 OMP_CLAUSE_CHAIN (u) = list;
30218 list = u;
30221 return list;
30224 /* OpenACC 2.0:
30225 auto
30226 independent
30227 nohost
30228 seq */
30230 static tree
30231 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30232 enum omp_clause_code code,
30233 tree list, location_t location)
30235 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30236 tree c = build_omp_clause (location, code);
30237 OMP_CLAUSE_CHAIN (c) = list;
30238 return c;
30241 /* OpenACC:
30242 num_gangs ( expression )
30243 num_workers ( expression )
30244 vector_length ( expression ) */
30246 static tree
30247 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30248 const char *str, tree list)
30250 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30252 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30253 return list;
30255 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30257 if (t == error_mark_node
30258 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30260 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30261 /*or_comma=*/false,
30262 /*consume_paren=*/true);
30263 return list;
30266 check_no_duplicate_clause (list, code, str, loc);
30268 tree c = build_omp_clause (loc, code);
30269 OMP_CLAUSE_OPERAND (c, 0) = t;
30270 OMP_CLAUSE_CHAIN (c) = list;
30271 return c;
30274 /* OpenACC:
30276 gang [( gang-arg-list )]
30277 worker [( [num:] int-expr )]
30278 vector [( [length:] int-expr )]
30280 where gang-arg is one of:
30282 [num:] int-expr
30283 static: size-expr
30285 and size-expr may be:
30288 int-expr
30291 static tree
30292 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30293 const char *str, tree list)
30295 const char *id = "num";
30296 cp_lexer *lexer = parser->lexer;
30297 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30298 location_t loc = cp_lexer_peek_token (lexer)->location;
30300 if (kind == OMP_CLAUSE_VECTOR)
30301 id = "length";
30303 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30305 cp_lexer_consume_token (lexer);
30309 cp_token *next = cp_lexer_peek_token (lexer);
30310 int idx = 0;
30312 /* Gang static argument. */
30313 if (kind == OMP_CLAUSE_GANG
30314 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30316 cp_lexer_consume_token (lexer);
30318 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30319 goto cleanup_error;
30321 idx = 1;
30322 if (ops[idx] != NULL)
30324 cp_parser_error (parser, "too many %<static%> arguments");
30325 goto cleanup_error;
30328 /* Check for the '*' argument. */
30329 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30330 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30331 || cp_lexer_nth_token_is (parser->lexer, 2,
30332 CPP_CLOSE_PAREN)))
30334 cp_lexer_consume_token (lexer);
30335 ops[idx] = integer_minus_one_node;
30337 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30339 cp_lexer_consume_token (lexer);
30340 continue;
30342 else break;
30345 /* Worker num: argument and vector length: arguments. */
30346 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30347 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30348 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30350 cp_lexer_consume_token (lexer); /* id */
30351 cp_lexer_consume_token (lexer); /* ':' */
30354 /* Now collect the actual argument. */
30355 if (ops[idx] != NULL_TREE)
30357 cp_parser_error (parser, "unexpected argument");
30358 goto cleanup_error;
30361 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30362 false);
30363 if (expr == error_mark_node)
30364 goto cleanup_error;
30366 mark_exp_read (expr);
30367 ops[idx] = expr;
30369 if (kind == OMP_CLAUSE_GANG
30370 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30372 cp_lexer_consume_token (lexer);
30373 continue;
30375 break;
30377 while (1);
30379 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30380 goto cleanup_error;
30383 check_no_duplicate_clause (list, kind, str, loc);
30385 c = build_omp_clause (loc, kind);
30387 if (ops[1])
30388 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30390 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30391 OMP_CLAUSE_CHAIN (c) = list;
30393 return c;
30395 cleanup_error:
30396 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30397 return list;
30400 /* OpenACC 2.0:
30401 tile ( size-expr-list ) */
30403 static tree
30404 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30406 tree c, expr = error_mark_node;
30407 tree tile = NULL_TREE;
30409 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30411 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30412 return list;
30416 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30417 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30418 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30420 cp_lexer_consume_token (parser->lexer);
30421 expr = integer_minus_one_node;
30423 else
30424 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30426 if (expr == error_mark_node)
30427 return list;
30429 tile = tree_cons (NULL_TREE, expr, tile);
30431 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30432 cp_lexer_consume_token (parser->lexer);
30434 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30436 /* Consume the trailing ')'. */
30437 cp_lexer_consume_token (parser->lexer);
30439 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30440 tile = nreverse (tile);
30441 OMP_CLAUSE_TILE_LIST (c) = tile;
30442 OMP_CLAUSE_CHAIN (c) = list;
30443 return c;
30446 /* OpenACC 2.0
30447 Parse wait clause or directive parameters. */
30449 static tree
30450 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30452 vec<tree, va_gc> *args;
30453 tree t, args_tree;
30455 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30456 /*cast_p=*/false,
30457 /*allow_expansion_p=*/true,
30458 /*non_constant_p=*/NULL);
30460 if (args == NULL || args->length () == 0)
30462 cp_parser_error (parser, "expected integer expression before ')'");
30463 if (args != NULL)
30464 release_tree_vector (args);
30465 return list;
30468 args_tree = build_tree_list_vec (args);
30470 release_tree_vector (args);
30472 for (t = args_tree; t; t = TREE_CHAIN (t))
30474 tree targ = TREE_VALUE (t);
30476 if (targ != error_mark_node)
30478 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30479 error ("%<wait%> expression must be integral");
30480 else
30482 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30484 mark_rvalue_use (targ);
30485 OMP_CLAUSE_DECL (c) = targ;
30486 OMP_CLAUSE_CHAIN (c) = list;
30487 list = c;
30492 return list;
30495 /* OpenACC:
30496 wait ( int-expr-list ) */
30498 static tree
30499 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30501 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30503 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30504 return list;
30506 list = cp_parser_oacc_wait_list (parser, location, list);
30508 return list;
30511 /* OpenMP 3.0:
30512 collapse ( constant-expression ) */
30514 static tree
30515 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30517 tree c, num;
30518 location_t loc;
30519 HOST_WIDE_INT n;
30521 loc = cp_lexer_peek_token (parser->lexer)->location;
30522 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30523 return list;
30525 num = cp_parser_constant_expression (parser);
30527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30528 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30529 /*or_comma=*/false,
30530 /*consume_paren=*/true);
30532 if (num == error_mark_node)
30533 return list;
30534 num = fold_non_dependent_expr (num);
30535 if (!tree_fits_shwi_p (num)
30536 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30537 || (n = tree_to_shwi (num)) <= 0
30538 || (int) n != n)
30540 error_at (loc, "collapse argument needs positive constant integer expression");
30541 return list;
30544 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30545 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30546 OMP_CLAUSE_CHAIN (c) = list;
30547 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30549 return c;
30552 /* OpenMP 2.5:
30553 default ( shared | none )
30555 OpenACC 2.0
30556 default (none) */
30558 static tree
30559 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30560 location_t location, bool is_oacc)
30562 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30563 tree c;
30565 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30566 return list;
30567 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30569 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30570 const char *p = IDENTIFIER_POINTER (id);
30572 switch (p[0])
30574 case 'n':
30575 if (strcmp ("none", p) != 0)
30576 goto invalid_kind;
30577 kind = OMP_CLAUSE_DEFAULT_NONE;
30578 break;
30580 case 's':
30581 if (strcmp ("shared", p) != 0 || is_oacc)
30582 goto invalid_kind;
30583 kind = OMP_CLAUSE_DEFAULT_SHARED;
30584 break;
30586 default:
30587 goto invalid_kind;
30590 cp_lexer_consume_token (parser->lexer);
30592 else
30594 invalid_kind:
30595 if (is_oacc)
30596 cp_parser_error (parser, "expected %<none%>");
30597 else
30598 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30601 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30602 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30603 /*or_comma=*/false,
30604 /*consume_paren=*/true);
30606 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30607 return list;
30609 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30610 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30611 OMP_CLAUSE_CHAIN (c) = list;
30612 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30614 return c;
30617 /* OpenMP 3.1:
30618 final ( expression ) */
30620 static tree
30621 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30623 tree t, c;
30625 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30626 return list;
30628 t = cp_parser_condition (parser);
30630 if (t == error_mark_node
30631 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30633 /*or_comma=*/false,
30634 /*consume_paren=*/true);
30636 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30638 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30639 OMP_CLAUSE_FINAL_EXPR (c) = t;
30640 OMP_CLAUSE_CHAIN (c) = list;
30642 return c;
30645 /* OpenMP 2.5:
30646 if ( expression )
30648 OpenMP 4.5:
30649 if ( directive-name-modifier : expression )
30651 directive-name-modifier:
30652 parallel | task | taskloop | target data | target | target update
30653 | target enter data | target exit data */
30655 static tree
30656 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30657 bool is_omp)
30659 tree t, c;
30660 enum tree_code if_modifier = ERROR_MARK;
30662 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30663 return list;
30665 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30667 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30668 const char *p = IDENTIFIER_POINTER (id);
30669 int n = 2;
30671 if (strcmp ("parallel", p) == 0)
30672 if_modifier = OMP_PARALLEL;
30673 else if (strcmp ("task", p) == 0)
30674 if_modifier = OMP_TASK;
30675 else if (strcmp ("taskloop", p) == 0)
30676 if_modifier = OMP_TASKLOOP;
30677 else if (strcmp ("target", p) == 0)
30679 if_modifier = OMP_TARGET;
30680 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30682 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30683 p = IDENTIFIER_POINTER (id);
30684 if (strcmp ("data", p) == 0)
30685 if_modifier = OMP_TARGET_DATA;
30686 else if (strcmp ("update", p) == 0)
30687 if_modifier = OMP_TARGET_UPDATE;
30688 else if (strcmp ("enter", p) == 0)
30689 if_modifier = OMP_TARGET_ENTER_DATA;
30690 else if (strcmp ("exit", p) == 0)
30691 if_modifier = OMP_TARGET_EXIT_DATA;
30692 if (if_modifier != OMP_TARGET)
30693 n = 3;
30694 else
30696 location_t loc
30697 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30698 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30699 "or %<exit%>");
30700 if_modifier = ERROR_MARK;
30702 if (if_modifier == OMP_TARGET_ENTER_DATA
30703 || if_modifier == OMP_TARGET_EXIT_DATA)
30705 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30707 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30708 p = IDENTIFIER_POINTER (id);
30709 if (strcmp ("data", p) == 0)
30710 n = 4;
30712 if (n != 4)
30714 location_t loc
30715 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30716 error_at (loc, "expected %<data%>");
30717 if_modifier = ERROR_MARK;
30722 if (if_modifier != ERROR_MARK)
30724 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30726 while (n-- > 0)
30727 cp_lexer_consume_token (parser->lexer);
30729 else
30731 if (n > 2)
30733 location_t loc
30734 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30735 error_at (loc, "expected %<:%>");
30737 if_modifier = ERROR_MARK;
30742 t = cp_parser_condition (parser);
30744 if (t == error_mark_node
30745 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30746 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30747 /*or_comma=*/false,
30748 /*consume_paren=*/true);
30750 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30751 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30753 if (if_modifier != ERROR_MARK
30754 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30756 const char *p = NULL;
30757 switch (if_modifier)
30759 case OMP_PARALLEL: p = "parallel"; break;
30760 case OMP_TASK: p = "task"; break;
30761 case OMP_TASKLOOP: p = "taskloop"; break;
30762 case OMP_TARGET_DATA: p = "target data"; break;
30763 case OMP_TARGET: p = "target"; break;
30764 case OMP_TARGET_UPDATE: p = "target update"; break;
30765 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30766 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30767 default: gcc_unreachable ();
30769 error_at (location, "too many %<if%> clauses with %qs modifier",
30771 return list;
30773 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30775 if (!is_omp)
30776 error_at (location, "too many %<if%> clauses");
30777 else
30778 error_at (location, "too many %<if%> clauses without modifier");
30779 return list;
30781 else if (if_modifier == ERROR_MARK
30782 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30784 error_at (location, "if any %<if%> clause has modifier, then all "
30785 "%<if%> clauses have to use modifier");
30786 return list;
30790 c = build_omp_clause (location, OMP_CLAUSE_IF);
30791 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30792 OMP_CLAUSE_IF_EXPR (c) = t;
30793 OMP_CLAUSE_CHAIN (c) = list;
30795 return c;
30798 /* OpenMP 3.1:
30799 mergeable */
30801 static tree
30802 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30803 tree list, location_t location)
30805 tree c;
30807 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30808 location);
30810 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30811 OMP_CLAUSE_CHAIN (c) = list;
30812 return c;
30815 /* OpenMP 2.5:
30816 nowait */
30818 static tree
30819 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30820 tree list, location_t location)
30822 tree c;
30824 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30826 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30827 OMP_CLAUSE_CHAIN (c) = list;
30828 return c;
30831 /* OpenMP 2.5:
30832 num_threads ( expression ) */
30834 static tree
30835 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30836 location_t location)
30838 tree t, c;
30840 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30841 return list;
30843 t = cp_parser_expression (parser);
30845 if (t == error_mark_node
30846 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30847 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30848 /*or_comma=*/false,
30849 /*consume_paren=*/true);
30851 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30852 "num_threads", location);
30854 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30855 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30856 OMP_CLAUSE_CHAIN (c) = list;
30858 return c;
30861 /* OpenMP 4.5:
30862 num_tasks ( expression ) */
30864 static tree
30865 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30866 location_t location)
30868 tree t, c;
30870 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30871 return list;
30873 t = cp_parser_expression (parser);
30875 if (t == error_mark_node
30876 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30877 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30878 /*or_comma=*/false,
30879 /*consume_paren=*/true);
30881 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30882 "num_tasks", location);
30884 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30885 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30886 OMP_CLAUSE_CHAIN (c) = list;
30888 return c;
30891 /* OpenMP 4.5:
30892 grainsize ( expression ) */
30894 static tree
30895 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30896 location_t location)
30898 tree t, c;
30900 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30901 return list;
30903 t = cp_parser_expression (parser);
30905 if (t == error_mark_node
30906 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30907 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30908 /*or_comma=*/false,
30909 /*consume_paren=*/true);
30911 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30912 "grainsize", location);
30914 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30915 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30916 OMP_CLAUSE_CHAIN (c) = list;
30918 return c;
30921 /* OpenMP 4.5:
30922 priority ( expression ) */
30924 static tree
30925 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30926 location_t location)
30928 tree t, c;
30930 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30931 return list;
30933 t = cp_parser_expression (parser);
30935 if (t == error_mark_node
30936 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30937 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30938 /*or_comma=*/false,
30939 /*consume_paren=*/true);
30941 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30942 "priority", location);
30944 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30945 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30946 OMP_CLAUSE_CHAIN (c) = list;
30948 return c;
30951 /* OpenMP 4.5:
30952 hint ( expression ) */
30954 static tree
30955 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30956 location_t location)
30958 tree t, c;
30960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30961 return list;
30963 t = cp_parser_expression (parser);
30965 if (t == error_mark_node
30966 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30967 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30968 /*or_comma=*/false,
30969 /*consume_paren=*/true);
30971 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30973 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30974 OMP_CLAUSE_HINT_EXPR (c) = t;
30975 OMP_CLAUSE_CHAIN (c) = list;
30977 return c;
30980 /* OpenMP 4.5:
30981 defaultmap ( tofrom : scalar ) */
30983 static tree
30984 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30985 location_t location)
30987 tree c, id;
30988 const char *p;
30990 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30991 return list;
30993 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30995 cp_parser_error (parser, "expected %<tofrom%>");
30996 goto out_err;
30998 id = cp_lexer_peek_token (parser->lexer)->u.value;
30999 p = IDENTIFIER_POINTER (id);
31000 if (strcmp (p, "tofrom") != 0)
31002 cp_parser_error (parser, "expected %<tofrom%>");
31003 goto out_err;
31005 cp_lexer_consume_token (parser->lexer);
31006 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31007 goto out_err;
31009 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31011 cp_parser_error (parser, "expected %<scalar%>");
31012 goto out_err;
31014 id = cp_lexer_peek_token (parser->lexer)->u.value;
31015 p = IDENTIFIER_POINTER (id);
31016 if (strcmp (p, "scalar") != 0)
31018 cp_parser_error (parser, "expected %<scalar%>");
31019 goto out_err;
31021 cp_lexer_consume_token (parser->lexer);
31022 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31023 goto out_err;
31025 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31026 location);
31028 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31029 OMP_CLAUSE_CHAIN (c) = list;
31030 return c;
31032 out_err:
31033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31034 /*or_comma=*/false,
31035 /*consume_paren=*/true);
31036 return list;
31039 /* OpenMP 2.5:
31040 ordered
31042 OpenMP 4.5:
31043 ordered ( constant-expression ) */
31045 static tree
31046 cp_parser_omp_clause_ordered (cp_parser *parser,
31047 tree list, location_t location)
31049 tree c, num = NULL_TREE;
31050 HOST_WIDE_INT n;
31052 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31053 "ordered", location);
31055 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31057 cp_lexer_consume_token (parser->lexer);
31059 num = cp_parser_constant_expression (parser);
31061 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31062 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31063 /*or_comma=*/false,
31064 /*consume_paren=*/true);
31066 if (num == error_mark_node)
31067 return list;
31068 num = fold_non_dependent_expr (num);
31069 if (!tree_fits_shwi_p (num)
31070 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31071 || (n = tree_to_shwi (num)) <= 0
31072 || (int) n != n)
31074 error_at (location,
31075 "ordered argument needs positive constant integer "
31076 "expression");
31077 return list;
31081 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31082 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31083 OMP_CLAUSE_CHAIN (c) = list;
31084 return c;
31087 /* OpenMP 2.5:
31088 reduction ( reduction-operator : variable-list )
31090 reduction-operator:
31091 One of: + * - & ^ | && ||
31093 OpenMP 3.1:
31095 reduction-operator:
31096 One of: + * - & ^ | && || min max
31098 OpenMP 4.0:
31100 reduction-operator:
31101 One of: + * - & ^ | && ||
31102 id-expression */
31104 static tree
31105 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31107 enum tree_code code = ERROR_MARK;
31108 tree nlist, c, id = NULL_TREE;
31110 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31111 return list;
31113 switch (cp_lexer_peek_token (parser->lexer)->type)
31115 case CPP_PLUS: code = PLUS_EXPR; break;
31116 case CPP_MULT: code = MULT_EXPR; break;
31117 case CPP_MINUS: code = MINUS_EXPR; break;
31118 case CPP_AND: code = BIT_AND_EXPR; break;
31119 case CPP_XOR: code = BIT_XOR_EXPR; break;
31120 case CPP_OR: code = BIT_IOR_EXPR; break;
31121 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31122 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31123 default: break;
31126 if (code != ERROR_MARK)
31127 cp_lexer_consume_token (parser->lexer);
31128 else
31130 bool saved_colon_corrects_to_scope_p;
31131 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31132 parser->colon_corrects_to_scope_p = false;
31133 id = cp_parser_id_expression (parser, /*template_p=*/false,
31134 /*check_dependency_p=*/true,
31135 /*template_p=*/NULL,
31136 /*declarator_p=*/false,
31137 /*optional_p=*/false);
31138 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31139 if (identifier_p (id))
31141 const char *p = IDENTIFIER_POINTER (id);
31143 if (strcmp (p, "min") == 0)
31144 code = MIN_EXPR;
31145 else if (strcmp (p, "max") == 0)
31146 code = MAX_EXPR;
31147 else if (id == ansi_opname (PLUS_EXPR))
31148 code = PLUS_EXPR;
31149 else if (id == ansi_opname (MULT_EXPR))
31150 code = MULT_EXPR;
31151 else if (id == ansi_opname (MINUS_EXPR))
31152 code = MINUS_EXPR;
31153 else if (id == ansi_opname (BIT_AND_EXPR))
31154 code = BIT_AND_EXPR;
31155 else if (id == ansi_opname (BIT_IOR_EXPR))
31156 code = BIT_IOR_EXPR;
31157 else if (id == ansi_opname (BIT_XOR_EXPR))
31158 code = BIT_XOR_EXPR;
31159 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31160 code = TRUTH_ANDIF_EXPR;
31161 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31162 code = TRUTH_ORIF_EXPR;
31163 id = omp_reduction_id (code, id, NULL_TREE);
31164 tree scope = parser->scope;
31165 if (scope)
31166 id = build_qualified_name (NULL_TREE, scope, id, false);
31167 parser->scope = NULL_TREE;
31168 parser->qualifying_scope = NULL_TREE;
31169 parser->object_scope = NULL_TREE;
31171 else
31173 error ("invalid reduction-identifier");
31174 resync_fail:
31175 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31176 /*or_comma=*/false,
31177 /*consume_paren=*/true);
31178 return list;
31182 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31183 goto resync_fail;
31185 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31186 NULL);
31187 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31189 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31190 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31193 return nlist;
31196 /* OpenMP 2.5:
31197 schedule ( schedule-kind )
31198 schedule ( schedule-kind , expression )
31200 schedule-kind:
31201 static | dynamic | guided | runtime | auto
31203 OpenMP 4.5:
31204 schedule ( schedule-modifier : schedule-kind )
31205 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31207 schedule-modifier:
31208 simd
31209 monotonic
31210 nonmonotonic */
31212 static tree
31213 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31215 tree c, t;
31216 int modifiers = 0, nmodifiers = 0;
31218 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31219 return list;
31221 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31223 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31225 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31226 const char *p = IDENTIFIER_POINTER (id);
31227 if (strcmp ("simd", p) == 0)
31228 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31229 else if (strcmp ("monotonic", p) == 0)
31230 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31231 else if (strcmp ("nonmonotonic", p) == 0)
31232 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31233 else
31234 break;
31235 cp_lexer_consume_token (parser->lexer);
31236 if (nmodifiers++ == 0
31237 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31238 cp_lexer_consume_token (parser->lexer);
31239 else
31241 cp_parser_require (parser, CPP_COLON, RT_COLON);
31242 break;
31246 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31248 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31249 const char *p = IDENTIFIER_POINTER (id);
31251 switch (p[0])
31253 case 'd':
31254 if (strcmp ("dynamic", p) != 0)
31255 goto invalid_kind;
31256 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31257 break;
31259 case 'g':
31260 if (strcmp ("guided", p) != 0)
31261 goto invalid_kind;
31262 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31263 break;
31265 case 'r':
31266 if (strcmp ("runtime", p) != 0)
31267 goto invalid_kind;
31268 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31269 break;
31271 default:
31272 goto invalid_kind;
31275 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31276 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31277 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31278 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31279 else
31280 goto invalid_kind;
31281 cp_lexer_consume_token (parser->lexer);
31283 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31284 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31285 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31286 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31288 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31289 "specified");
31290 modifiers = 0;
31293 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31295 cp_token *token;
31296 cp_lexer_consume_token (parser->lexer);
31298 token = cp_lexer_peek_token (parser->lexer);
31299 t = cp_parser_assignment_expression (parser);
31301 if (t == error_mark_node)
31302 goto resync_fail;
31303 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31304 error_at (token->location, "schedule %<runtime%> does not take "
31305 "a %<chunk_size%> parameter");
31306 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31307 error_at (token->location, "schedule %<auto%> does not take "
31308 "a %<chunk_size%> parameter");
31309 else
31310 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31312 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31313 goto resync_fail;
31315 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31316 goto resync_fail;
31318 OMP_CLAUSE_SCHEDULE_KIND (c)
31319 = (enum omp_clause_schedule_kind)
31320 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31322 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31323 OMP_CLAUSE_CHAIN (c) = list;
31324 return c;
31326 invalid_kind:
31327 cp_parser_error (parser, "invalid schedule kind");
31328 resync_fail:
31329 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31330 /*or_comma=*/false,
31331 /*consume_paren=*/true);
31332 return list;
31335 /* OpenMP 3.0:
31336 untied */
31338 static tree
31339 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31340 tree list, location_t location)
31342 tree c;
31344 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31346 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31347 OMP_CLAUSE_CHAIN (c) = list;
31348 return c;
31351 /* OpenMP 4.0:
31352 inbranch
31353 notinbranch */
31355 static tree
31356 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31357 tree list, location_t location)
31359 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31360 tree c = build_omp_clause (location, code);
31361 OMP_CLAUSE_CHAIN (c) = list;
31362 return c;
31365 /* OpenMP 4.0:
31366 parallel
31368 sections
31369 taskgroup */
31371 static tree
31372 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31373 enum omp_clause_code code,
31374 tree list, location_t location)
31376 tree c = build_omp_clause (location, code);
31377 OMP_CLAUSE_CHAIN (c) = list;
31378 return c;
31381 /* OpenMP 4.5:
31382 nogroup */
31384 static tree
31385 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31386 tree list, location_t location)
31388 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31389 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31390 OMP_CLAUSE_CHAIN (c) = list;
31391 return c;
31394 /* OpenMP 4.5:
31395 simd
31396 threads */
31398 static tree
31399 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31400 enum omp_clause_code code,
31401 tree list, location_t location)
31403 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31404 tree c = build_omp_clause (location, code);
31405 OMP_CLAUSE_CHAIN (c) = list;
31406 return c;
31409 /* OpenMP 4.0:
31410 num_teams ( expression ) */
31412 static tree
31413 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31414 location_t location)
31416 tree t, c;
31418 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31419 return list;
31421 t = cp_parser_expression (parser);
31423 if (t == error_mark_node
31424 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31425 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31426 /*or_comma=*/false,
31427 /*consume_paren=*/true);
31429 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31430 "num_teams", location);
31432 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31433 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31434 OMP_CLAUSE_CHAIN (c) = list;
31436 return c;
31439 /* OpenMP 4.0:
31440 thread_limit ( expression ) */
31442 static tree
31443 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31444 location_t location)
31446 tree t, c;
31448 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31449 return list;
31451 t = cp_parser_expression (parser);
31453 if (t == error_mark_node
31454 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31455 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31456 /*or_comma=*/false,
31457 /*consume_paren=*/true);
31459 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31460 "thread_limit", location);
31462 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31463 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31464 OMP_CLAUSE_CHAIN (c) = list;
31466 return c;
31469 /* OpenMP 4.0:
31470 aligned ( variable-list )
31471 aligned ( variable-list : constant-expression ) */
31473 static tree
31474 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31476 tree nlist, c, alignment = NULL_TREE;
31477 bool colon;
31479 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31480 return list;
31482 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31483 &colon);
31485 if (colon)
31487 alignment = cp_parser_constant_expression (parser);
31489 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31490 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31491 /*or_comma=*/false,
31492 /*consume_paren=*/true);
31494 if (alignment == error_mark_node)
31495 alignment = NULL_TREE;
31498 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31499 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31501 return nlist;
31504 /* OpenMP 4.0:
31505 linear ( variable-list )
31506 linear ( variable-list : expression )
31508 OpenMP 4.5:
31509 linear ( modifier ( variable-list ) )
31510 linear ( modifier ( variable-list ) : expression ) */
31512 static tree
31513 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31514 bool is_cilk_simd_fn, bool declare_simd)
31516 tree nlist, c, step = integer_one_node;
31517 bool colon;
31518 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31520 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31521 return list;
31523 if (!is_cilk_simd_fn
31524 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31526 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31527 const char *p = IDENTIFIER_POINTER (id);
31529 if (strcmp ("ref", p) == 0)
31530 kind = OMP_CLAUSE_LINEAR_REF;
31531 else if (strcmp ("val", p) == 0)
31532 kind = OMP_CLAUSE_LINEAR_VAL;
31533 else if (strcmp ("uval", p) == 0)
31534 kind = OMP_CLAUSE_LINEAR_UVAL;
31535 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31536 cp_lexer_consume_token (parser->lexer);
31537 else
31538 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31541 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31542 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31543 &colon);
31544 else
31546 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31547 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31548 if (colon)
31549 cp_parser_require (parser, CPP_COLON, RT_COLON);
31550 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31551 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31552 /*or_comma=*/false,
31553 /*consume_paren=*/true);
31556 if (colon)
31558 step = NULL_TREE;
31559 if (declare_simd
31560 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31561 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31563 cp_token *token = cp_lexer_peek_token (parser->lexer);
31564 cp_parser_parse_tentatively (parser);
31565 step = cp_parser_id_expression (parser, /*template_p=*/false,
31566 /*check_dependency_p=*/true,
31567 /*template_p=*/NULL,
31568 /*declarator_p=*/false,
31569 /*optional_p=*/false);
31570 if (step != error_mark_node)
31571 step = cp_parser_lookup_name_simple (parser, step, token->location);
31572 if (step == error_mark_node)
31574 step = NULL_TREE;
31575 cp_parser_abort_tentative_parse (parser);
31577 else if (!cp_parser_parse_definitely (parser))
31578 step = NULL_TREE;
31580 if (!step)
31581 step = cp_parser_expression (parser);
31583 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31585 sorry ("using parameters for %<linear%> step is not supported yet");
31586 step = integer_one_node;
31588 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31589 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31590 /*or_comma=*/false,
31591 /*consume_paren=*/true);
31593 if (step == error_mark_node)
31594 return list;
31597 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31599 OMP_CLAUSE_LINEAR_STEP (c) = step;
31600 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31603 return nlist;
31606 /* OpenMP 4.0:
31607 safelen ( constant-expression ) */
31609 static tree
31610 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31611 location_t location)
31613 tree t, c;
31615 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31616 return list;
31618 t = cp_parser_constant_expression (parser);
31620 if (t == error_mark_node
31621 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31622 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31623 /*or_comma=*/false,
31624 /*consume_paren=*/true);
31626 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31628 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31629 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31630 OMP_CLAUSE_CHAIN (c) = list;
31632 return c;
31635 /* OpenMP 4.0:
31636 simdlen ( constant-expression ) */
31638 static tree
31639 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31640 location_t location)
31642 tree t, c;
31644 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31645 return list;
31647 t = cp_parser_constant_expression (parser);
31649 if (t == error_mark_node
31650 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31652 /*or_comma=*/false,
31653 /*consume_paren=*/true);
31655 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31657 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31658 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31659 OMP_CLAUSE_CHAIN (c) = list;
31661 return c;
31664 /* OpenMP 4.5:
31665 vec:
31666 identifier [+/- integer]
31667 vec , identifier [+/- integer]
31670 static tree
31671 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31672 tree list)
31674 tree vec = NULL;
31676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31678 cp_parser_error (parser, "expected identifier");
31679 return list;
31682 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31684 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31685 tree t, identifier = cp_parser_identifier (parser);
31686 tree addend = NULL;
31688 if (identifier == error_mark_node)
31689 t = error_mark_node;
31690 else
31692 t = cp_parser_lookup_name_simple
31693 (parser, identifier,
31694 cp_lexer_peek_token (parser->lexer)->location);
31695 if (t == error_mark_node)
31696 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31697 id_loc);
31700 bool neg = false;
31701 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31702 neg = true;
31703 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31705 addend = integer_zero_node;
31706 goto add_to_vector;
31708 cp_lexer_consume_token (parser->lexer);
31710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31712 cp_parser_error (parser, "expected integer");
31713 return list;
31716 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31717 if (TREE_CODE (addend) != INTEGER_CST)
31719 cp_parser_error (parser, "expected integer");
31720 return list;
31722 cp_lexer_consume_token (parser->lexer);
31724 add_to_vector:
31725 if (t != error_mark_node)
31727 vec = tree_cons (addend, t, vec);
31728 if (neg)
31729 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31732 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31733 break;
31735 cp_lexer_consume_token (parser->lexer);
31738 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31740 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31741 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31742 OMP_CLAUSE_DECL (u) = nreverse (vec);
31743 OMP_CLAUSE_CHAIN (u) = list;
31744 return u;
31746 return list;
31749 /* OpenMP 4.0:
31750 depend ( depend-kind : variable-list )
31752 depend-kind:
31753 in | out | inout
31755 OpenMP 4.5:
31756 depend ( source )
31758 depend ( sink : vec ) */
31760 static tree
31761 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31763 tree nlist, c;
31764 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31766 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31767 return list;
31769 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31771 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31772 const char *p = IDENTIFIER_POINTER (id);
31774 if (strcmp ("in", p) == 0)
31775 kind = OMP_CLAUSE_DEPEND_IN;
31776 else if (strcmp ("inout", p) == 0)
31777 kind = OMP_CLAUSE_DEPEND_INOUT;
31778 else if (strcmp ("out", p) == 0)
31779 kind = OMP_CLAUSE_DEPEND_OUT;
31780 else if (strcmp ("source", p) == 0)
31781 kind = OMP_CLAUSE_DEPEND_SOURCE;
31782 else if (strcmp ("sink", p) == 0)
31783 kind = OMP_CLAUSE_DEPEND_SINK;
31784 else
31785 goto invalid_kind;
31787 else
31788 goto invalid_kind;
31790 cp_lexer_consume_token (parser->lexer);
31792 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31794 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31795 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31796 OMP_CLAUSE_DECL (c) = NULL_TREE;
31797 OMP_CLAUSE_CHAIN (c) = list;
31798 if (!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);
31802 return c;
31805 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31806 goto resync_fail;
31808 if (kind == OMP_CLAUSE_DEPEND_SINK)
31809 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31810 else
31812 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31813 list, NULL);
31815 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31816 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31818 return nlist;
31820 invalid_kind:
31821 cp_parser_error (parser, "invalid depend kind");
31822 resync_fail:
31823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31824 /*or_comma=*/false,
31825 /*consume_paren=*/true);
31826 return list;
31829 /* OpenMP 4.0:
31830 map ( map-kind : variable-list )
31831 map ( variable-list )
31833 map-kind:
31834 alloc | to | from | tofrom
31836 OpenMP 4.5:
31837 map-kind:
31838 alloc | to | from | tofrom | release | delete
31840 map ( always [,] map-kind: variable-list ) */
31842 static tree
31843 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31845 tree nlist, c;
31846 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31847 bool always = false;
31849 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31850 return list;
31852 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31854 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31855 const char *p = IDENTIFIER_POINTER (id);
31857 if (strcmp ("always", p) == 0)
31859 int nth = 2;
31860 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31861 nth++;
31862 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31863 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31864 == RID_DELETE))
31865 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31866 == CPP_COLON))
31868 always = true;
31869 cp_lexer_consume_token (parser->lexer);
31870 if (nth == 3)
31871 cp_lexer_consume_token (parser->lexer);
31876 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31877 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31879 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31880 const char *p = IDENTIFIER_POINTER (id);
31882 if (strcmp ("alloc", p) == 0)
31883 kind = GOMP_MAP_ALLOC;
31884 else if (strcmp ("to", p) == 0)
31885 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31886 else if (strcmp ("from", p) == 0)
31887 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31888 else if (strcmp ("tofrom", p) == 0)
31889 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31890 else if (strcmp ("release", p) == 0)
31891 kind = GOMP_MAP_RELEASE;
31892 else
31894 cp_parser_error (parser, "invalid map kind");
31895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31896 /*or_comma=*/false,
31897 /*consume_paren=*/true);
31898 return list;
31900 cp_lexer_consume_token (parser->lexer);
31901 cp_lexer_consume_token (parser->lexer);
31903 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31904 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31906 kind = GOMP_MAP_DELETE;
31907 cp_lexer_consume_token (parser->lexer);
31908 cp_lexer_consume_token (parser->lexer);
31911 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31912 NULL);
31914 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31915 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31917 return nlist;
31920 /* OpenMP 4.0:
31921 device ( expression ) */
31923 static tree
31924 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31925 location_t location)
31927 tree t, c;
31929 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31930 return list;
31932 t = cp_parser_expression (parser);
31934 if (t == error_mark_node
31935 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31936 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31937 /*or_comma=*/false,
31938 /*consume_paren=*/true);
31940 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31941 "device", location);
31943 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31944 OMP_CLAUSE_DEVICE_ID (c) = t;
31945 OMP_CLAUSE_CHAIN (c) = list;
31947 return c;
31950 /* OpenMP 4.0:
31951 dist_schedule ( static )
31952 dist_schedule ( static , expression ) */
31954 static tree
31955 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31956 location_t location)
31958 tree c, t;
31960 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31961 return list;
31963 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31965 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31966 goto invalid_kind;
31967 cp_lexer_consume_token (parser->lexer);
31969 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31971 cp_lexer_consume_token (parser->lexer);
31973 t = cp_parser_assignment_expression (parser);
31975 if (t == error_mark_node)
31976 goto resync_fail;
31977 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31979 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31980 goto resync_fail;
31982 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31983 goto resync_fail;
31985 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31986 location);
31987 OMP_CLAUSE_CHAIN (c) = list;
31988 return c;
31990 invalid_kind:
31991 cp_parser_error (parser, "invalid dist_schedule kind");
31992 resync_fail:
31993 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31994 /*or_comma=*/false,
31995 /*consume_paren=*/true);
31996 return list;
31999 /* OpenMP 4.0:
32000 proc_bind ( proc-bind-kind )
32002 proc-bind-kind:
32003 master | close | spread */
32005 static tree
32006 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32007 location_t location)
32009 tree c;
32010 enum omp_clause_proc_bind_kind kind;
32012 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32013 return list;
32015 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32017 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32018 const char *p = IDENTIFIER_POINTER (id);
32020 if (strcmp ("master", p) == 0)
32021 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32022 else if (strcmp ("close", p) == 0)
32023 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32024 else if (strcmp ("spread", p) == 0)
32025 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32026 else
32027 goto invalid_kind;
32029 else
32030 goto invalid_kind;
32032 cp_lexer_consume_token (parser->lexer);
32033 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32034 goto resync_fail;
32036 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32037 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32038 location);
32039 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32040 OMP_CLAUSE_CHAIN (c) = list;
32041 return c;
32043 invalid_kind:
32044 cp_parser_error (parser, "invalid depend kind");
32045 resync_fail:
32046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32047 /*or_comma=*/false,
32048 /*consume_paren=*/true);
32049 return list;
32052 /* OpenACC:
32053 async [( int-expr )] */
32055 static tree
32056 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32058 tree c, t;
32059 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32061 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32063 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32065 cp_lexer_consume_token (parser->lexer);
32067 t = cp_parser_expression (parser);
32068 if (t == error_mark_node
32069 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32070 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32071 /*or_comma=*/false,
32072 /*consume_paren=*/true);
32075 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32077 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32078 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32079 OMP_CLAUSE_CHAIN (c) = list;
32080 list = c;
32082 return list;
32085 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32086 is a bitmask in MASK. Return the list of clauses found. */
32088 static tree
32089 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32090 const char *where, cp_token *pragma_tok,
32091 bool finish_p = true)
32093 tree clauses = NULL;
32094 bool first = true;
32096 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32098 location_t here;
32099 pragma_omp_clause c_kind;
32100 omp_clause_code code;
32101 const char *c_name;
32102 tree prev = clauses;
32104 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32105 cp_lexer_consume_token (parser->lexer);
32107 here = cp_lexer_peek_token (parser->lexer)->location;
32108 c_kind = cp_parser_omp_clause_name (parser);
32110 switch (c_kind)
32112 case PRAGMA_OACC_CLAUSE_ASYNC:
32113 clauses = cp_parser_oacc_clause_async (parser, clauses);
32114 c_name = "async";
32115 break;
32116 case PRAGMA_OACC_CLAUSE_AUTO:
32117 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32118 clauses, here);
32119 c_name = "auto";
32120 break;
32121 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32122 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32123 c_name = "collapse";
32124 break;
32125 case PRAGMA_OACC_CLAUSE_COPY:
32126 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32127 c_name = "copy";
32128 break;
32129 case PRAGMA_OACC_CLAUSE_COPYIN:
32130 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32131 c_name = "copyin";
32132 break;
32133 case PRAGMA_OACC_CLAUSE_COPYOUT:
32134 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32135 c_name = "copyout";
32136 break;
32137 case PRAGMA_OACC_CLAUSE_CREATE:
32138 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32139 c_name = "create";
32140 break;
32141 case PRAGMA_OACC_CLAUSE_DELETE:
32142 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32143 c_name = "delete";
32144 break;
32145 case PRAGMA_OMP_CLAUSE_DEFAULT:
32146 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32147 c_name = "default";
32148 break;
32149 case PRAGMA_OACC_CLAUSE_DEVICE:
32150 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32151 c_name = "device";
32152 break;
32153 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32154 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32155 c_name = "deviceptr";
32156 break;
32157 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32158 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32159 c_name = "device_resident";
32160 break;
32161 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32162 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32163 clauses);
32164 c_name = "firstprivate";
32165 break;
32166 case PRAGMA_OACC_CLAUSE_GANG:
32167 c_name = "gang";
32168 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32169 c_name, clauses);
32170 break;
32171 case PRAGMA_OACC_CLAUSE_HOST:
32172 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32173 c_name = "host";
32174 break;
32175 case PRAGMA_OACC_CLAUSE_IF:
32176 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32177 c_name = "if";
32178 break;
32179 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32180 clauses = cp_parser_oacc_simple_clause (parser,
32181 OMP_CLAUSE_INDEPENDENT,
32182 clauses, here);
32183 c_name = "independent";
32184 break;
32185 case PRAGMA_OACC_CLAUSE_LINK:
32186 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32187 c_name = "link";
32188 break;
32189 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32190 code = OMP_CLAUSE_NUM_GANGS;
32191 c_name = "num_gangs";
32192 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32193 clauses);
32194 break;
32195 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32196 c_name = "num_workers";
32197 code = OMP_CLAUSE_NUM_WORKERS;
32198 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32199 clauses);
32200 break;
32201 case PRAGMA_OACC_CLAUSE_PRESENT:
32202 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32203 c_name = "present";
32204 break;
32205 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32206 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32207 c_name = "present_or_copy";
32208 break;
32209 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32210 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32211 c_name = "present_or_copyin";
32212 break;
32213 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32214 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32215 c_name = "present_or_copyout";
32216 break;
32217 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32218 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32219 c_name = "present_or_create";
32220 break;
32221 case PRAGMA_OACC_CLAUSE_PRIVATE:
32222 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32223 clauses);
32224 c_name = "private";
32225 break;
32226 case PRAGMA_OACC_CLAUSE_REDUCTION:
32227 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32228 c_name = "reduction";
32229 break;
32230 case PRAGMA_OACC_CLAUSE_SELF:
32231 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32232 c_name = "self";
32233 break;
32234 case PRAGMA_OACC_CLAUSE_SEQ:
32235 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32236 clauses, here);
32237 c_name = "seq";
32238 break;
32239 case PRAGMA_OACC_CLAUSE_TILE:
32240 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32241 c_name = "tile";
32242 break;
32243 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32244 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32245 clauses);
32246 c_name = "use_device";
32247 break;
32248 case PRAGMA_OACC_CLAUSE_VECTOR:
32249 c_name = "vector";
32250 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32251 c_name, clauses);
32252 break;
32253 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32254 c_name = "vector_length";
32255 code = OMP_CLAUSE_VECTOR_LENGTH;
32256 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32257 clauses);
32258 break;
32259 case PRAGMA_OACC_CLAUSE_WAIT:
32260 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32261 c_name = "wait";
32262 break;
32263 case PRAGMA_OACC_CLAUSE_WORKER:
32264 c_name = "worker";
32265 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32266 c_name, clauses);
32267 break;
32268 default:
32269 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32270 goto saw_error;
32273 first = false;
32275 if (((mask >> c_kind) & 1) == 0)
32277 /* Remove the invalid clause(s) from the list to avoid
32278 confusing the rest of the compiler. */
32279 clauses = prev;
32280 error_at (here, "%qs is not valid for %qs", c_name, where);
32284 saw_error:
32285 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32287 if (finish_p)
32288 return finish_omp_clauses (clauses, C_ORT_ACC);
32290 return clauses;
32293 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32294 is a bitmask in MASK. Return the list of clauses found; the result
32295 of clause default goes in *pdefault. */
32297 static tree
32298 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32299 const char *where, cp_token *pragma_tok,
32300 bool finish_p = true)
32302 tree clauses = NULL;
32303 bool first = true;
32304 cp_token *token = NULL;
32306 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32308 pragma_omp_clause c_kind;
32309 const char *c_name;
32310 tree prev = clauses;
32312 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32313 cp_lexer_consume_token (parser->lexer);
32315 token = cp_lexer_peek_token (parser->lexer);
32316 c_kind = cp_parser_omp_clause_name (parser);
32318 switch (c_kind)
32320 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32321 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32322 token->location);
32323 c_name = "collapse";
32324 break;
32325 case PRAGMA_OMP_CLAUSE_COPYIN:
32326 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32327 c_name = "copyin";
32328 break;
32329 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32330 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32331 clauses);
32332 c_name = "copyprivate";
32333 break;
32334 case PRAGMA_OMP_CLAUSE_DEFAULT:
32335 clauses = cp_parser_omp_clause_default (parser, clauses,
32336 token->location, false);
32337 c_name = "default";
32338 break;
32339 case PRAGMA_OMP_CLAUSE_FINAL:
32340 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32341 c_name = "final";
32342 break;
32343 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32344 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32345 clauses);
32346 c_name = "firstprivate";
32347 break;
32348 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32349 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32350 token->location);
32351 c_name = "grainsize";
32352 break;
32353 case PRAGMA_OMP_CLAUSE_HINT:
32354 clauses = cp_parser_omp_clause_hint (parser, clauses,
32355 token->location);
32356 c_name = "hint";
32357 break;
32358 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32359 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32360 token->location);
32361 c_name = "defaultmap";
32362 break;
32363 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32364 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32365 clauses);
32366 c_name = "use_device_ptr";
32367 break;
32368 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32369 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32370 clauses);
32371 c_name = "is_device_ptr";
32372 break;
32373 case PRAGMA_OMP_CLAUSE_IF:
32374 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32375 true);
32376 c_name = "if";
32377 break;
32378 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32379 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32380 clauses);
32381 c_name = "lastprivate";
32382 break;
32383 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32384 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32385 token->location);
32386 c_name = "mergeable";
32387 break;
32388 case PRAGMA_OMP_CLAUSE_NOWAIT:
32389 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32390 c_name = "nowait";
32391 break;
32392 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32393 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32394 token->location);
32395 c_name = "num_tasks";
32396 break;
32397 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32398 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32399 token->location);
32400 c_name = "num_threads";
32401 break;
32402 case PRAGMA_OMP_CLAUSE_ORDERED:
32403 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32404 token->location);
32405 c_name = "ordered";
32406 break;
32407 case PRAGMA_OMP_CLAUSE_PRIORITY:
32408 clauses = cp_parser_omp_clause_priority (parser, clauses,
32409 token->location);
32410 c_name = "priority";
32411 break;
32412 case PRAGMA_OMP_CLAUSE_PRIVATE:
32413 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32414 clauses);
32415 c_name = "private";
32416 break;
32417 case PRAGMA_OMP_CLAUSE_REDUCTION:
32418 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32419 c_name = "reduction";
32420 break;
32421 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32422 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32423 token->location);
32424 c_name = "schedule";
32425 break;
32426 case PRAGMA_OMP_CLAUSE_SHARED:
32427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32428 clauses);
32429 c_name = "shared";
32430 break;
32431 case PRAGMA_OMP_CLAUSE_UNTIED:
32432 clauses = cp_parser_omp_clause_untied (parser, clauses,
32433 token->location);
32434 c_name = "untied";
32435 break;
32436 case PRAGMA_OMP_CLAUSE_INBRANCH:
32437 case PRAGMA_CILK_CLAUSE_MASK:
32438 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32439 clauses, token->location);
32440 c_name = "inbranch";
32441 break;
32442 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32443 case PRAGMA_CILK_CLAUSE_NOMASK:
32444 clauses = cp_parser_omp_clause_branch (parser,
32445 OMP_CLAUSE_NOTINBRANCH,
32446 clauses, token->location);
32447 c_name = "notinbranch";
32448 break;
32449 case PRAGMA_OMP_CLAUSE_PARALLEL:
32450 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32451 clauses, token->location);
32452 c_name = "parallel";
32453 if (!first)
32455 clause_not_first:
32456 error_at (token->location, "%qs must be the first clause of %qs",
32457 c_name, where);
32458 clauses = prev;
32460 break;
32461 case PRAGMA_OMP_CLAUSE_FOR:
32462 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32463 clauses, token->location);
32464 c_name = "for";
32465 if (!first)
32466 goto clause_not_first;
32467 break;
32468 case PRAGMA_OMP_CLAUSE_SECTIONS:
32469 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32470 clauses, token->location);
32471 c_name = "sections";
32472 if (!first)
32473 goto clause_not_first;
32474 break;
32475 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32476 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32477 clauses, token->location);
32478 c_name = "taskgroup";
32479 if (!first)
32480 goto clause_not_first;
32481 break;
32482 case PRAGMA_OMP_CLAUSE_LINK:
32483 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32484 c_name = "to";
32485 break;
32486 case PRAGMA_OMP_CLAUSE_TO:
32487 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32488 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32489 clauses);
32490 else
32491 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32492 c_name = "to";
32493 break;
32494 case PRAGMA_OMP_CLAUSE_FROM:
32495 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32496 c_name = "from";
32497 break;
32498 case PRAGMA_OMP_CLAUSE_UNIFORM:
32499 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32500 clauses);
32501 c_name = "uniform";
32502 break;
32503 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32504 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32505 token->location);
32506 c_name = "num_teams";
32507 break;
32508 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32509 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32510 token->location);
32511 c_name = "thread_limit";
32512 break;
32513 case PRAGMA_OMP_CLAUSE_ALIGNED:
32514 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32515 c_name = "aligned";
32516 break;
32517 case PRAGMA_OMP_CLAUSE_LINEAR:
32519 bool cilk_simd_fn = false, declare_simd = false;
32520 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32521 cilk_simd_fn = true;
32522 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32523 declare_simd = true;
32524 clauses = cp_parser_omp_clause_linear (parser, clauses,
32525 cilk_simd_fn, declare_simd);
32527 c_name = "linear";
32528 break;
32529 case PRAGMA_OMP_CLAUSE_DEPEND:
32530 clauses = cp_parser_omp_clause_depend (parser, clauses,
32531 token->location);
32532 c_name = "depend";
32533 break;
32534 case PRAGMA_OMP_CLAUSE_MAP:
32535 clauses = cp_parser_omp_clause_map (parser, clauses);
32536 c_name = "map";
32537 break;
32538 case PRAGMA_OMP_CLAUSE_DEVICE:
32539 clauses = cp_parser_omp_clause_device (parser, clauses,
32540 token->location);
32541 c_name = "device";
32542 break;
32543 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32544 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32545 token->location);
32546 c_name = "dist_schedule";
32547 break;
32548 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32549 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32550 token->location);
32551 c_name = "proc_bind";
32552 break;
32553 case PRAGMA_OMP_CLAUSE_SAFELEN:
32554 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32555 token->location);
32556 c_name = "safelen";
32557 break;
32558 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32559 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32560 token->location);
32561 c_name = "simdlen";
32562 break;
32563 case PRAGMA_OMP_CLAUSE_NOGROUP:
32564 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32565 token->location);
32566 c_name = "nogroup";
32567 break;
32568 case PRAGMA_OMP_CLAUSE_THREADS:
32569 clauses
32570 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32571 clauses, token->location);
32572 c_name = "threads";
32573 break;
32574 case PRAGMA_OMP_CLAUSE_SIMD:
32575 clauses
32576 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32577 clauses, token->location);
32578 c_name = "simd";
32579 break;
32580 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32581 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32582 c_name = "simdlen";
32583 break;
32584 default:
32585 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32586 goto saw_error;
32589 first = false;
32591 if (((mask >> c_kind) & 1) == 0)
32593 /* Remove the invalid clause(s) from the list to avoid
32594 confusing the rest of the compiler. */
32595 clauses = prev;
32596 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32599 saw_error:
32600 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32601 no reason to skip to the end. */
32602 if (!(flag_cilkplus && pragma_tok == NULL))
32603 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32604 if (finish_p)
32606 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32607 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
32608 else
32609 return finish_omp_clauses (clauses, C_ORT_OMP);
32611 return clauses;
32614 /* OpenMP 2.5:
32615 structured-block:
32616 statement
32618 In practice, we're also interested in adding the statement to an
32619 outer node. So it is convenient if we work around the fact that
32620 cp_parser_statement calls add_stmt. */
32622 static unsigned
32623 cp_parser_begin_omp_structured_block (cp_parser *parser)
32625 unsigned save = parser->in_statement;
32627 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32628 This preserves the "not within loop or switch" style error messages
32629 for nonsense cases like
32630 void foo() {
32631 #pragma omp single
32632 break;
32635 if (parser->in_statement)
32636 parser->in_statement = IN_OMP_BLOCK;
32638 return save;
32641 static void
32642 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32644 parser->in_statement = save;
32647 static tree
32648 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32650 tree stmt = begin_omp_structured_block ();
32651 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32653 cp_parser_statement (parser, NULL_TREE, false, if_p);
32655 cp_parser_end_omp_structured_block (parser, save);
32656 return finish_omp_structured_block (stmt);
32659 /* OpenMP 2.5:
32660 # pragma omp atomic new-line
32661 expression-stmt
32663 expression-stmt:
32664 x binop= expr | x++ | ++x | x-- | --x
32665 binop:
32666 +, *, -, /, &, ^, |, <<, >>
32668 where x is an lvalue expression with scalar type.
32670 OpenMP 3.1:
32671 # pragma omp atomic new-line
32672 update-stmt
32674 # pragma omp atomic read new-line
32675 read-stmt
32677 # pragma omp atomic write new-line
32678 write-stmt
32680 # pragma omp atomic update new-line
32681 update-stmt
32683 # pragma omp atomic capture new-line
32684 capture-stmt
32686 # pragma omp atomic capture new-line
32687 capture-block
32689 read-stmt:
32690 v = x
32691 write-stmt:
32692 x = expr
32693 update-stmt:
32694 expression-stmt | x = x binop expr
32695 capture-stmt:
32696 v = expression-stmt
32697 capture-block:
32698 { v = x; update-stmt; } | { update-stmt; v = x; }
32700 OpenMP 4.0:
32701 update-stmt:
32702 expression-stmt | x = x binop expr | x = expr binop x
32703 capture-stmt:
32704 v = update-stmt
32705 capture-block:
32706 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32708 where x and v are lvalue expressions with scalar type. */
32710 static void
32711 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32713 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32714 tree rhs1 = NULL_TREE, orig_lhs;
32715 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32716 bool structured_block = false;
32717 bool seq_cst = false;
32719 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32721 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32722 const char *p = IDENTIFIER_POINTER (id);
32724 if (!strcmp (p, "seq_cst"))
32726 seq_cst = true;
32727 cp_lexer_consume_token (parser->lexer);
32728 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32729 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32730 cp_lexer_consume_token (parser->lexer);
32733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32735 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32736 const char *p = IDENTIFIER_POINTER (id);
32738 if (!strcmp (p, "read"))
32739 code = OMP_ATOMIC_READ;
32740 else if (!strcmp (p, "write"))
32741 code = NOP_EXPR;
32742 else if (!strcmp (p, "update"))
32743 code = OMP_ATOMIC;
32744 else if (!strcmp (p, "capture"))
32745 code = OMP_ATOMIC_CAPTURE_NEW;
32746 else
32747 p = NULL;
32748 if (p)
32749 cp_lexer_consume_token (parser->lexer);
32751 if (!seq_cst)
32753 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32755 cp_lexer_consume_token (parser->lexer);
32757 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32759 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32760 const char *p = IDENTIFIER_POINTER (id);
32762 if (!strcmp (p, "seq_cst"))
32764 seq_cst = true;
32765 cp_lexer_consume_token (parser->lexer);
32769 cp_parser_require_pragma_eol (parser, pragma_tok);
32771 switch (code)
32773 case OMP_ATOMIC_READ:
32774 case NOP_EXPR: /* atomic write */
32775 v = cp_parser_unary_expression (parser);
32776 if (v == error_mark_node)
32777 goto saw_error;
32778 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32779 goto saw_error;
32780 if (code == NOP_EXPR)
32781 lhs = cp_parser_expression (parser);
32782 else
32783 lhs = cp_parser_unary_expression (parser);
32784 if (lhs == error_mark_node)
32785 goto saw_error;
32786 if (code == NOP_EXPR)
32788 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32789 opcode. */
32790 code = OMP_ATOMIC;
32791 rhs = lhs;
32792 lhs = v;
32793 v = NULL_TREE;
32795 goto done;
32796 case OMP_ATOMIC_CAPTURE_NEW:
32797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32799 cp_lexer_consume_token (parser->lexer);
32800 structured_block = true;
32802 else
32804 v = cp_parser_unary_expression (parser);
32805 if (v == error_mark_node)
32806 goto saw_error;
32807 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32808 goto saw_error;
32810 default:
32811 break;
32814 restart:
32815 lhs = cp_parser_unary_expression (parser);
32816 orig_lhs = lhs;
32817 switch (TREE_CODE (lhs))
32819 case ERROR_MARK:
32820 goto saw_error;
32822 case POSTINCREMENT_EXPR:
32823 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32824 code = OMP_ATOMIC_CAPTURE_OLD;
32825 /* FALLTHROUGH */
32826 case PREINCREMENT_EXPR:
32827 lhs = TREE_OPERAND (lhs, 0);
32828 opcode = PLUS_EXPR;
32829 rhs = integer_one_node;
32830 break;
32832 case POSTDECREMENT_EXPR:
32833 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32834 code = OMP_ATOMIC_CAPTURE_OLD;
32835 /* FALLTHROUGH */
32836 case PREDECREMENT_EXPR:
32837 lhs = TREE_OPERAND (lhs, 0);
32838 opcode = MINUS_EXPR;
32839 rhs = integer_one_node;
32840 break;
32842 case COMPOUND_EXPR:
32843 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32844 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32845 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32846 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32847 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32848 (TREE_OPERAND (lhs, 1), 0), 0)))
32849 == BOOLEAN_TYPE)
32850 /* Undo effects of boolean_increment for post {in,de}crement. */
32851 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32852 /* FALLTHRU */
32853 case MODIFY_EXPR:
32854 if (TREE_CODE (lhs) == MODIFY_EXPR
32855 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32857 /* Undo effects of boolean_increment. */
32858 if (integer_onep (TREE_OPERAND (lhs, 1)))
32860 /* This is pre or post increment. */
32861 rhs = TREE_OPERAND (lhs, 1);
32862 lhs = TREE_OPERAND (lhs, 0);
32863 opcode = NOP_EXPR;
32864 if (code == OMP_ATOMIC_CAPTURE_NEW
32865 && !structured_block
32866 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32867 code = OMP_ATOMIC_CAPTURE_OLD;
32868 break;
32871 /* FALLTHRU */
32872 default:
32873 switch (cp_lexer_peek_token (parser->lexer)->type)
32875 case CPP_MULT_EQ:
32876 opcode = MULT_EXPR;
32877 break;
32878 case CPP_DIV_EQ:
32879 opcode = TRUNC_DIV_EXPR;
32880 break;
32881 case CPP_PLUS_EQ:
32882 opcode = PLUS_EXPR;
32883 break;
32884 case CPP_MINUS_EQ:
32885 opcode = MINUS_EXPR;
32886 break;
32887 case CPP_LSHIFT_EQ:
32888 opcode = LSHIFT_EXPR;
32889 break;
32890 case CPP_RSHIFT_EQ:
32891 opcode = RSHIFT_EXPR;
32892 break;
32893 case CPP_AND_EQ:
32894 opcode = BIT_AND_EXPR;
32895 break;
32896 case CPP_OR_EQ:
32897 opcode = BIT_IOR_EXPR;
32898 break;
32899 case CPP_XOR_EQ:
32900 opcode = BIT_XOR_EXPR;
32901 break;
32902 case CPP_EQ:
32903 enum cp_parser_prec oprec;
32904 cp_token *token;
32905 cp_lexer_consume_token (parser->lexer);
32906 cp_parser_parse_tentatively (parser);
32907 rhs1 = cp_parser_simple_cast_expression (parser);
32908 if (rhs1 == error_mark_node)
32910 cp_parser_abort_tentative_parse (parser);
32911 cp_parser_simple_cast_expression (parser);
32912 goto saw_error;
32914 token = cp_lexer_peek_token (parser->lexer);
32915 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32917 cp_parser_abort_tentative_parse (parser);
32918 cp_parser_parse_tentatively (parser);
32919 rhs = cp_parser_binary_expression (parser, false, true,
32920 PREC_NOT_OPERATOR, NULL);
32921 if (rhs == error_mark_node)
32923 cp_parser_abort_tentative_parse (parser);
32924 cp_parser_binary_expression (parser, false, true,
32925 PREC_NOT_OPERATOR, NULL);
32926 goto saw_error;
32928 switch (TREE_CODE (rhs))
32930 case MULT_EXPR:
32931 case TRUNC_DIV_EXPR:
32932 case RDIV_EXPR:
32933 case PLUS_EXPR:
32934 case MINUS_EXPR:
32935 case LSHIFT_EXPR:
32936 case RSHIFT_EXPR:
32937 case BIT_AND_EXPR:
32938 case BIT_IOR_EXPR:
32939 case BIT_XOR_EXPR:
32940 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32942 if (cp_parser_parse_definitely (parser))
32944 opcode = TREE_CODE (rhs);
32945 rhs1 = TREE_OPERAND (rhs, 0);
32946 rhs = TREE_OPERAND (rhs, 1);
32947 goto stmt_done;
32949 else
32950 goto saw_error;
32952 break;
32953 default:
32954 break;
32956 cp_parser_abort_tentative_parse (parser);
32957 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32959 rhs = cp_parser_expression (parser);
32960 if (rhs == error_mark_node)
32961 goto saw_error;
32962 opcode = NOP_EXPR;
32963 rhs1 = NULL_TREE;
32964 goto stmt_done;
32966 cp_parser_error (parser,
32967 "invalid form of %<#pragma omp atomic%>");
32968 goto saw_error;
32970 if (!cp_parser_parse_definitely (parser))
32971 goto saw_error;
32972 switch (token->type)
32974 case CPP_SEMICOLON:
32975 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32977 code = OMP_ATOMIC_CAPTURE_OLD;
32978 v = lhs;
32979 lhs = NULL_TREE;
32980 lhs1 = rhs1;
32981 rhs1 = NULL_TREE;
32982 cp_lexer_consume_token (parser->lexer);
32983 goto restart;
32985 else if (structured_block)
32987 opcode = NOP_EXPR;
32988 rhs = rhs1;
32989 rhs1 = NULL_TREE;
32990 goto stmt_done;
32992 cp_parser_error (parser,
32993 "invalid form of %<#pragma omp atomic%>");
32994 goto saw_error;
32995 case CPP_MULT:
32996 opcode = MULT_EXPR;
32997 break;
32998 case CPP_DIV:
32999 opcode = TRUNC_DIV_EXPR;
33000 break;
33001 case CPP_PLUS:
33002 opcode = PLUS_EXPR;
33003 break;
33004 case CPP_MINUS:
33005 opcode = MINUS_EXPR;
33006 break;
33007 case CPP_LSHIFT:
33008 opcode = LSHIFT_EXPR;
33009 break;
33010 case CPP_RSHIFT:
33011 opcode = RSHIFT_EXPR;
33012 break;
33013 case CPP_AND:
33014 opcode = BIT_AND_EXPR;
33015 break;
33016 case CPP_OR:
33017 opcode = BIT_IOR_EXPR;
33018 break;
33019 case CPP_XOR:
33020 opcode = BIT_XOR_EXPR;
33021 break;
33022 default:
33023 cp_parser_error (parser,
33024 "invalid operator for %<#pragma omp atomic%>");
33025 goto saw_error;
33027 oprec = TOKEN_PRECEDENCE (token);
33028 gcc_assert (oprec != PREC_NOT_OPERATOR);
33029 if (commutative_tree_code (opcode))
33030 oprec = (enum cp_parser_prec) (oprec - 1);
33031 cp_lexer_consume_token (parser->lexer);
33032 rhs = cp_parser_binary_expression (parser, false, false,
33033 oprec, NULL);
33034 if (rhs == error_mark_node)
33035 goto saw_error;
33036 goto stmt_done;
33037 /* FALLTHROUGH */
33038 default:
33039 cp_parser_error (parser,
33040 "invalid operator for %<#pragma omp atomic%>");
33041 goto saw_error;
33043 cp_lexer_consume_token (parser->lexer);
33045 rhs = cp_parser_expression (parser);
33046 if (rhs == error_mark_node)
33047 goto saw_error;
33048 break;
33050 stmt_done:
33051 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33053 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33054 goto saw_error;
33055 v = cp_parser_unary_expression (parser);
33056 if (v == error_mark_node)
33057 goto saw_error;
33058 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33059 goto saw_error;
33060 lhs1 = cp_parser_unary_expression (parser);
33061 if (lhs1 == error_mark_node)
33062 goto saw_error;
33064 if (structured_block)
33066 cp_parser_consume_semicolon_at_end_of_statement (parser);
33067 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33069 done:
33070 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33071 if (!structured_block)
33072 cp_parser_consume_semicolon_at_end_of_statement (parser);
33073 return;
33075 saw_error:
33076 cp_parser_skip_to_end_of_block_or_statement (parser);
33077 if (structured_block)
33079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33080 cp_lexer_consume_token (parser->lexer);
33081 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33083 cp_parser_skip_to_end_of_block_or_statement (parser);
33084 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33085 cp_lexer_consume_token (parser->lexer);
33091 /* OpenMP 2.5:
33092 # pragma omp barrier new-line */
33094 static void
33095 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33097 cp_parser_require_pragma_eol (parser, pragma_tok);
33098 finish_omp_barrier ();
33101 /* OpenMP 2.5:
33102 # pragma omp critical [(name)] new-line
33103 structured-block
33105 OpenMP 4.5:
33106 # pragma omp critical [(name) [hint(expression)]] new-line
33107 structured-block */
33109 #define OMP_CRITICAL_CLAUSE_MASK \
33110 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33112 static tree
33113 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33115 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33117 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33119 cp_lexer_consume_token (parser->lexer);
33121 name = cp_parser_identifier (parser);
33123 if (name == error_mark_node
33124 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33126 /*or_comma=*/false,
33127 /*consume_paren=*/true);
33128 if (name == error_mark_node)
33129 name = NULL;
33131 clauses = cp_parser_omp_all_clauses (parser,
33132 OMP_CRITICAL_CLAUSE_MASK,
33133 "#pragma omp critical", pragma_tok);
33135 else
33136 cp_parser_require_pragma_eol (parser, pragma_tok);
33138 stmt = cp_parser_omp_structured_block (parser, if_p);
33139 return c_finish_omp_critical (input_location, stmt, name, clauses);
33142 /* OpenMP 2.5:
33143 # pragma omp flush flush-vars[opt] new-line
33145 flush-vars:
33146 ( variable-list ) */
33148 static void
33149 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33151 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33152 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33153 cp_parser_require_pragma_eol (parser, pragma_tok);
33155 finish_omp_flush ();
33158 /* Helper function, to parse omp for increment expression. */
33160 static tree
33161 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33163 tree cond = cp_parser_binary_expression (parser, false, true,
33164 PREC_NOT_OPERATOR, NULL);
33165 if (cond == error_mark_node
33166 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33168 cp_parser_skip_to_end_of_statement (parser);
33169 return error_mark_node;
33172 switch (TREE_CODE (cond))
33174 case GT_EXPR:
33175 case GE_EXPR:
33176 case LT_EXPR:
33177 case LE_EXPR:
33178 break;
33179 case NE_EXPR:
33180 if (code == CILK_SIMD || code == CILK_FOR)
33181 break;
33182 /* Fall through: OpenMP disallows NE_EXPR. */
33183 default:
33184 return error_mark_node;
33187 /* If decl is an iterator, preserve LHS and RHS of the relational
33188 expr until finish_omp_for. */
33189 if (decl
33190 && (type_dependent_expression_p (decl)
33191 || CLASS_TYPE_P (TREE_TYPE (decl))))
33192 return cond;
33194 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33195 TREE_CODE (cond),
33196 TREE_OPERAND (cond, 0), ERROR_MARK,
33197 TREE_OPERAND (cond, 1), ERROR_MARK,
33198 /*overload=*/NULL, tf_warning_or_error);
33201 /* Helper function, to parse omp for increment expression. */
33203 static tree
33204 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33206 cp_token *token = cp_lexer_peek_token (parser->lexer);
33207 enum tree_code op;
33208 tree lhs, rhs;
33209 cp_id_kind idk;
33210 bool decl_first;
33212 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33214 op = (token->type == CPP_PLUS_PLUS
33215 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33216 cp_lexer_consume_token (parser->lexer);
33217 lhs = cp_parser_simple_cast_expression (parser);
33218 if (lhs != decl
33219 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33220 return error_mark_node;
33221 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33224 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33225 if (lhs != decl
33226 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33227 return error_mark_node;
33229 token = cp_lexer_peek_token (parser->lexer);
33230 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33232 op = (token->type == CPP_PLUS_PLUS
33233 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33234 cp_lexer_consume_token (parser->lexer);
33235 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33238 op = cp_parser_assignment_operator_opt (parser);
33239 if (op == ERROR_MARK)
33240 return error_mark_node;
33242 if (op != NOP_EXPR)
33244 rhs = cp_parser_assignment_expression (parser);
33245 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33246 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33249 lhs = cp_parser_binary_expression (parser, false, false,
33250 PREC_ADDITIVE_EXPRESSION, NULL);
33251 token = cp_lexer_peek_token (parser->lexer);
33252 decl_first = (lhs == decl
33253 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33254 if (decl_first)
33255 lhs = NULL_TREE;
33256 if (token->type != CPP_PLUS
33257 && token->type != CPP_MINUS)
33258 return error_mark_node;
33262 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33263 cp_lexer_consume_token (parser->lexer);
33264 rhs = cp_parser_binary_expression (parser, false, false,
33265 PREC_ADDITIVE_EXPRESSION, NULL);
33266 token = cp_lexer_peek_token (parser->lexer);
33267 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33269 if (lhs == NULL_TREE)
33271 if (op == PLUS_EXPR)
33272 lhs = rhs;
33273 else
33274 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33275 tf_warning_or_error);
33277 else
33278 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33279 ERROR_MARK, NULL, tf_warning_or_error);
33282 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33284 if (!decl_first)
33286 if ((rhs != decl
33287 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33288 || op == MINUS_EXPR)
33289 return error_mark_node;
33290 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33292 else
33293 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33295 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33298 /* Parse the initialization statement of either an OpenMP for loop or
33299 a Cilk Plus for loop.
33301 Return true if the resulting construct should have an
33302 OMP_CLAUSE_PRIVATE added to it. */
33304 static tree
33305 cp_parser_omp_for_loop_init (cp_parser *parser,
33306 enum tree_code code,
33307 tree &this_pre_body,
33308 vec<tree, va_gc> *for_block,
33309 tree &init,
33310 tree &orig_init,
33311 tree &decl,
33312 tree &real_decl)
33314 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33315 return NULL_TREE;
33317 tree add_private_clause = NULL_TREE;
33319 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33321 init-expr:
33322 var = lb
33323 integer-type var = lb
33324 random-access-iterator-type var = lb
33325 pointer-type var = lb
33327 cp_decl_specifier_seq type_specifiers;
33329 /* First, try to parse as an initialized declaration. See
33330 cp_parser_condition, from whence the bulk of this is copied. */
33332 cp_parser_parse_tentatively (parser);
33333 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33334 /*is_trailing_return=*/false,
33335 &type_specifiers);
33336 if (cp_parser_parse_definitely (parser))
33338 /* If parsing a type specifier seq succeeded, then this
33339 MUST be a initialized declaration. */
33340 tree asm_specification, attributes;
33341 cp_declarator *declarator;
33343 declarator = cp_parser_declarator (parser,
33344 CP_PARSER_DECLARATOR_NAMED,
33345 /*ctor_dtor_or_conv_p=*/NULL,
33346 /*parenthesized_p=*/NULL,
33347 /*member_p=*/false,
33348 /*friend_p=*/false);
33349 attributes = cp_parser_attributes_opt (parser);
33350 asm_specification = cp_parser_asm_specification_opt (parser);
33352 if (declarator == cp_error_declarator)
33353 cp_parser_skip_to_end_of_statement (parser);
33355 else
33357 tree pushed_scope, auto_node;
33359 decl = start_decl (declarator, &type_specifiers,
33360 SD_INITIALIZED, attributes,
33361 /*prefix_attributes=*/NULL_TREE,
33362 &pushed_scope);
33364 auto_node = type_uses_auto (TREE_TYPE (decl));
33365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33367 if (cp_lexer_next_token_is (parser->lexer,
33368 CPP_OPEN_PAREN))
33370 if (code != CILK_SIMD && code != CILK_FOR)
33371 error ("parenthesized initialization is not allowed in "
33372 "OpenMP %<for%> loop");
33373 else
33374 error ("parenthesized initialization is "
33375 "not allowed in for-loop");
33377 else
33378 /* Trigger an error. */
33379 cp_parser_require (parser, CPP_EQ, RT_EQ);
33381 init = error_mark_node;
33382 cp_parser_skip_to_end_of_statement (parser);
33384 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33385 || type_dependent_expression_p (decl)
33386 || auto_node)
33388 bool is_direct_init, is_non_constant_init;
33390 init = cp_parser_initializer (parser,
33391 &is_direct_init,
33392 &is_non_constant_init);
33394 if (auto_node)
33396 TREE_TYPE (decl)
33397 = do_auto_deduction (TREE_TYPE (decl), init,
33398 auto_node);
33400 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33401 && !type_dependent_expression_p (decl))
33402 goto non_class;
33405 cp_finish_decl (decl, init, !is_non_constant_init,
33406 asm_specification,
33407 LOOKUP_ONLYCONVERTING);
33408 orig_init = init;
33409 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33411 vec_safe_push (for_block, this_pre_body);
33412 init = NULL_TREE;
33414 else
33415 init = pop_stmt_list (this_pre_body);
33416 this_pre_body = NULL_TREE;
33418 else
33420 /* Consume '='. */
33421 cp_lexer_consume_token (parser->lexer);
33422 init = cp_parser_assignment_expression (parser);
33424 non_class:
33425 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33426 init = error_mark_node;
33427 else
33428 cp_finish_decl (decl, NULL_TREE,
33429 /*init_const_expr_p=*/false,
33430 asm_specification,
33431 LOOKUP_ONLYCONVERTING);
33434 if (pushed_scope)
33435 pop_scope (pushed_scope);
33438 else
33440 cp_id_kind idk;
33441 /* If parsing a type specifier sequence failed, then
33442 this MUST be a simple expression. */
33443 if (code == CILK_FOR)
33444 error ("%<_Cilk_for%> allows expression instead of declaration only "
33445 "in C, not in C++");
33446 cp_parser_parse_tentatively (parser);
33447 decl = cp_parser_primary_expression (parser, false, false,
33448 false, &idk);
33449 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33450 if (!cp_parser_error_occurred (parser)
33451 && decl
33452 && (TREE_CODE (decl) == COMPONENT_REF
33453 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33455 cp_parser_abort_tentative_parse (parser);
33456 cp_parser_parse_tentatively (parser);
33457 cp_token *token = cp_lexer_peek_token (parser->lexer);
33458 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33459 /*check_dependency_p=*/true,
33460 /*template_p=*/NULL,
33461 /*declarator_p=*/false,
33462 /*optional_p=*/false);
33463 if (name != error_mark_node
33464 && last_tok == cp_lexer_peek_token (parser->lexer))
33466 decl = cp_parser_lookup_name_simple (parser, name,
33467 token->location);
33468 if (TREE_CODE (decl) == FIELD_DECL)
33469 add_private_clause = omp_privatize_field (decl, false);
33471 cp_parser_abort_tentative_parse (parser);
33472 cp_parser_parse_tentatively (parser);
33473 decl = cp_parser_primary_expression (parser, false, false,
33474 false, &idk);
33476 if (!cp_parser_error_occurred (parser)
33477 && decl
33478 && DECL_P (decl)
33479 && CLASS_TYPE_P (TREE_TYPE (decl)))
33481 tree rhs;
33483 cp_parser_parse_definitely (parser);
33484 cp_parser_require (parser, CPP_EQ, RT_EQ);
33485 rhs = cp_parser_assignment_expression (parser);
33486 orig_init = rhs;
33487 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33488 decl, NOP_EXPR,
33489 rhs,
33490 tf_warning_or_error));
33491 if (!add_private_clause)
33492 add_private_clause = decl;
33494 else
33496 decl = NULL;
33497 cp_parser_abort_tentative_parse (parser);
33498 init = cp_parser_expression (parser);
33499 if (init)
33501 if (TREE_CODE (init) == MODIFY_EXPR
33502 || TREE_CODE (init) == MODOP_EXPR)
33503 real_decl = TREE_OPERAND (init, 0);
33507 return add_private_clause;
33510 /* Parse the restricted form of the for statement allowed by OpenMP. */
33512 static tree
33513 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33514 tree *cclauses, bool *if_p)
33516 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33517 tree real_decl, initv, condv, incrv, declv;
33518 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33519 location_t loc_first;
33520 bool collapse_err = false;
33521 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33522 vec<tree, va_gc> *for_block = make_tree_vector ();
33523 auto_vec<tree, 4> orig_inits;
33525 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33526 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33527 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33528 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33529 && OMP_CLAUSE_ORDERED_EXPR (cl))
33531 ordered_cl = cl;
33532 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33535 if (ordered && ordered < collapse)
33537 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33538 "%<ordered%> clause parameter is less than %<collapse%>");
33539 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33540 = build_int_cst (NULL_TREE, collapse);
33541 ordered = collapse;
33543 if (ordered)
33545 for (tree *pc = &clauses; *pc; )
33546 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33548 error_at (OMP_CLAUSE_LOCATION (*pc),
33549 "%<linear%> clause may not be specified together "
33550 "with %<ordered%> clause with a parameter");
33551 *pc = OMP_CLAUSE_CHAIN (*pc);
33553 else
33554 pc = &OMP_CLAUSE_CHAIN (*pc);
33557 gcc_assert (collapse >= 1 && ordered >= 0);
33558 count = ordered ? ordered : collapse;
33560 declv = make_tree_vec (count);
33561 initv = make_tree_vec (count);
33562 condv = make_tree_vec (count);
33563 incrv = make_tree_vec (count);
33565 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33567 for (i = 0; i < count; i++)
33569 int bracecount = 0;
33570 tree add_private_clause = NULL_TREE;
33571 location_t loc;
33573 if (code != CILK_FOR
33574 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33576 cp_parser_error (parser, "for statement expected");
33577 return NULL;
33579 if (code == CILK_FOR
33580 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33582 cp_parser_error (parser, "_Cilk_for statement expected");
33583 return NULL;
33585 loc = cp_lexer_consume_token (parser->lexer)->location;
33587 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33588 return NULL;
33590 init = orig_init = decl = real_decl = NULL;
33591 this_pre_body = push_stmt_list ();
33593 add_private_clause
33594 = cp_parser_omp_for_loop_init (parser, code,
33595 this_pre_body, for_block,
33596 init, orig_init, decl, real_decl);
33598 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33599 if (this_pre_body)
33601 this_pre_body = pop_stmt_list (this_pre_body);
33602 if (pre_body)
33604 tree t = pre_body;
33605 pre_body = push_stmt_list ();
33606 add_stmt (t);
33607 add_stmt (this_pre_body);
33608 pre_body = pop_stmt_list (pre_body);
33610 else
33611 pre_body = this_pre_body;
33614 if (decl)
33615 real_decl = decl;
33616 if (cclauses != NULL
33617 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33618 && real_decl != NULL_TREE)
33620 tree *c;
33621 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33622 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33623 && OMP_CLAUSE_DECL (*c) == real_decl)
33625 error_at (loc, "iteration variable %qD"
33626 " should not be firstprivate", real_decl);
33627 *c = OMP_CLAUSE_CHAIN (*c);
33629 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33630 && OMP_CLAUSE_DECL (*c) == real_decl)
33632 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33633 tree l = *c;
33634 *c = OMP_CLAUSE_CHAIN (*c);
33635 if (code == OMP_SIMD)
33637 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33638 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33640 else
33642 OMP_CLAUSE_CHAIN (l) = clauses;
33643 clauses = l;
33645 add_private_clause = NULL_TREE;
33647 else
33649 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33650 && OMP_CLAUSE_DECL (*c) == real_decl)
33651 add_private_clause = NULL_TREE;
33652 c = &OMP_CLAUSE_CHAIN (*c);
33656 if (add_private_clause)
33658 tree c;
33659 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33661 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33662 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33663 && OMP_CLAUSE_DECL (c) == decl)
33664 break;
33665 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33666 && OMP_CLAUSE_DECL (c) == decl)
33667 error_at (loc, "iteration variable %qD "
33668 "should not be firstprivate",
33669 decl);
33670 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33671 && OMP_CLAUSE_DECL (c) == decl)
33672 error_at (loc, "iteration variable %qD should not be reduction",
33673 decl);
33675 if (c == NULL)
33677 if (code != OMP_SIMD)
33678 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33679 else if (collapse == 1)
33680 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33681 else
33682 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33683 OMP_CLAUSE_DECL (c) = add_private_clause;
33684 c = finish_omp_clauses (c, C_ORT_OMP);
33685 if (c)
33687 OMP_CLAUSE_CHAIN (c) = clauses;
33688 clauses = c;
33689 /* For linear, signal that we need to fill up
33690 the so far unknown linear step. */
33691 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33692 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33697 cond = NULL;
33698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33699 cond = cp_parser_omp_for_cond (parser, decl, code);
33700 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33702 incr = NULL;
33703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33705 /* If decl is an iterator, preserve the operator on decl
33706 until finish_omp_for. */
33707 if (real_decl
33708 && ((processing_template_decl
33709 && (TREE_TYPE (real_decl) == NULL_TREE
33710 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33711 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33712 incr = cp_parser_omp_for_incr (parser, real_decl);
33713 else
33714 incr = cp_parser_expression (parser);
33715 if (!EXPR_HAS_LOCATION (incr))
33716 protected_set_expr_location (incr, input_location);
33719 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33720 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33721 /*or_comma=*/false,
33722 /*consume_paren=*/true);
33724 TREE_VEC_ELT (declv, i) = decl;
33725 TREE_VEC_ELT (initv, i) = init;
33726 TREE_VEC_ELT (condv, i) = cond;
33727 TREE_VEC_ELT (incrv, i) = incr;
33728 if (orig_init)
33730 orig_inits.safe_grow_cleared (i + 1);
33731 orig_inits[i] = orig_init;
33734 if (i == count - 1)
33735 break;
33737 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33738 in between the collapsed for loops to be still considered perfectly
33739 nested. Hopefully the final version clarifies this.
33740 For now handle (multiple) {'s and empty statements. */
33741 cp_parser_parse_tentatively (parser);
33744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33745 break;
33746 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33748 cp_lexer_consume_token (parser->lexer);
33749 bracecount++;
33751 else if (bracecount
33752 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33753 cp_lexer_consume_token (parser->lexer);
33754 else
33756 loc = cp_lexer_peek_token (parser->lexer)->location;
33757 error_at (loc, "not enough collapsed for loops");
33758 collapse_err = true;
33759 cp_parser_abort_tentative_parse (parser);
33760 declv = NULL_TREE;
33761 break;
33764 while (1);
33766 if (declv)
33768 cp_parser_parse_definitely (parser);
33769 nbraces += bracecount;
33773 if (nbraces)
33774 if_p = NULL;
33776 /* Note that we saved the original contents of this flag when we entered
33777 the structured block, and so we don't need to re-save it here. */
33778 if (code == CILK_SIMD || code == CILK_FOR)
33779 parser->in_statement = IN_CILK_SIMD_FOR;
33780 else
33781 parser->in_statement = IN_OMP_FOR;
33783 /* Note that the grammar doesn't call for a structured block here,
33784 though the loop as a whole is a structured block. */
33785 body = push_stmt_list ();
33786 cp_parser_statement (parser, NULL_TREE, false, if_p);
33787 body = pop_stmt_list (body);
33789 if (declv == NULL_TREE)
33790 ret = NULL_TREE;
33791 else
33792 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33793 body, pre_body, &orig_inits, clauses);
33795 while (nbraces)
33797 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33799 cp_lexer_consume_token (parser->lexer);
33800 nbraces--;
33802 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33803 cp_lexer_consume_token (parser->lexer);
33804 else
33806 if (!collapse_err)
33808 error_at (cp_lexer_peek_token (parser->lexer)->location,
33809 "collapsed loops not perfectly nested");
33811 collapse_err = true;
33812 cp_parser_statement_seq_opt (parser, NULL);
33813 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33814 break;
33818 while (!for_block->is_empty ())
33819 add_stmt (pop_stmt_list (for_block->pop ()));
33820 release_tree_vector (for_block);
33822 return ret;
33825 /* Helper function for OpenMP parsing, split clauses and call
33826 finish_omp_clauses on each of the set of clauses afterwards. */
33828 static void
33829 cp_omp_split_clauses (location_t loc, enum tree_code code,
33830 omp_clause_mask mask, tree clauses, tree *cclauses)
33832 int i;
33833 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33834 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33835 if (cclauses[i])
33836 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
33839 /* OpenMP 4.0:
33840 #pragma omp simd simd-clause[optseq] new-line
33841 for-loop */
33843 #define OMP_SIMD_CLAUSE_MASK \
33844 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33846 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33847 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33848 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33849 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33853 static tree
33854 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33855 char *p_name, omp_clause_mask mask, tree *cclauses,
33856 bool *if_p)
33858 tree clauses, sb, ret;
33859 unsigned int save;
33860 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33862 strcat (p_name, " simd");
33863 mask |= OMP_SIMD_CLAUSE_MASK;
33865 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33866 cclauses == NULL);
33867 if (cclauses)
33869 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33870 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33871 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33872 OMP_CLAUSE_ORDERED);
33873 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33875 error_at (OMP_CLAUSE_LOCATION (c),
33876 "%<ordered%> clause with parameter may not be specified "
33877 "on %qs construct", p_name);
33878 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33882 sb = begin_omp_structured_block ();
33883 save = cp_parser_begin_omp_structured_block (parser);
33885 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33887 cp_parser_end_omp_structured_block (parser, save);
33888 add_stmt (finish_omp_structured_block (sb));
33890 return ret;
33893 /* OpenMP 2.5:
33894 #pragma omp for for-clause[optseq] new-line
33895 for-loop
33897 OpenMP 4.0:
33898 #pragma omp for simd for-simd-clause[optseq] new-line
33899 for-loop */
33901 #define OMP_FOR_CLAUSE_MASK \
33902 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33903 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33904 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33905 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33906 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33907 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33908 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33909 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33910 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33912 static tree
33913 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33914 char *p_name, omp_clause_mask mask, tree *cclauses,
33915 bool *if_p)
33917 tree clauses, sb, ret;
33918 unsigned int save;
33919 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33921 strcat (p_name, " for");
33922 mask |= OMP_FOR_CLAUSE_MASK;
33923 /* parallel for{, simd} disallows nowait clause, but for
33924 target {teams distribute ,}parallel for{, simd} it should be accepted. */
33925 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
33926 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33927 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33928 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33929 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33931 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33933 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33934 const char *p = IDENTIFIER_POINTER (id);
33936 if (strcmp (p, "simd") == 0)
33938 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33939 if (cclauses == NULL)
33940 cclauses = cclauses_buf;
33942 cp_lexer_consume_token (parser->lexer);
33943 if (!flag_openmp) /* flag_openmp_simd */
33944 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33945 cclauses, if_p);
33946 sb = begin_omp_structured_block ();
33947 save = cp_parser_begin_omp_structured_block (parser);
33948 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33949 cclauses, if_p);
33950 cp_parser_end_omp_structured_block (parser, save);
33951 tree body = finish_omp_structured_block (sb);
33952 if (ret == NULL)
33953 return ret;
33954 ret = make_node (OMP_FOR);
33955 TREE_TYPE (ret) = void_type_node;
33956 OMP_FOR_BODY (ret) = body;
33957 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33958 SET_EXPR_LOCATION (ret, loc);
33959 add_stmt (ret);
33960 return ret;
33963 if (!flag_openmp) /* flag_openmp_simd */
33965 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33966 return NULL_TREE;
33969 /* Composite distribute parallel for disallows linear clause. */
33970 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33971 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33973 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33974 cclauses == NULL);
33975 if (cclauses)
33977 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33978 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33981 sb = begin_omp_structured_block ();
33982 save = cp_parser_begin_omp_structured_block (parser);
33984 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
33986 cp_parser_end_omp_structured_block (parser, save);
33987 add_stmt (finish_omp_structured_block (sb));
33989 return ret;
33992 /* OpenMP 2.5:
33993 # pragma omp master new-line
33994 structured-block */
33996 static tree
33997 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33999 cp_parser_require_pragma_eol (parser, pragma_tok);
34000 return c_finish_omp_master (input_location,
34001 cp_parser_omp_structured_block (parser, if_p));
34004 /* OpenMP 2.5:
34005 # pragma omp ordered new-line
34006 structured-block
34008 OpenMP 4.5:
34009 # pragma omp ordered ordered-clauses new-line
34010 structured-block */
34012 #define OMP_ORDERED_CLAUSE_MASK \
34013 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34014 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34016 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34017 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34019 static bool
34020 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34021 enum pragma_context context, bool *if_p)
34023 location_t loc = pragma_tok->location;
34025 if (context != pragma_stmt && context != pragma_compound)
34027 cp_parser_error (parser, "expected declaration specifiers");
34028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34029 return false;
34032 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34034 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34035 const char *p = IDENTIFIER_POINTER (id);
34037 if (strcmp (p, "depend") == 0)
34039 if (context == pragma_stmt)
34041 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34042 "%<depend%> clause may only be used in compound "
34043 "statements");
34044 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34045 return false;
34047 tree clauses
34048 = cp_parser_omp_all_clauses (parser,
34049 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34050 "#pragma omp ordered", pragma_tok);
34051 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34052 return false;
34056 tree clauses
34057 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34058 "#pragma omp ordered", pragma_tok);
34059 c_finish_omp_ordered (loc, clauses,
34060 cp_parser_omp_structured_block (parser, if_p));
34061 return true;
34064 /* OpenMP 2.5:
34066 section-scope:
34067 { section-sequence }
34069 section-sequence:
34070 section-directive[opt] structured-block
34071 section-sequence section-directive structured-block */
34073 static tree
34074 cp_parser_omp_sections_scope (cp_parser *parser)
34076 tree stmt, substmt;
34077 bool error_suppress = false;
34078 cp_token *tok;
34080 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34081 return NULL_TREE;
34083 stmt = push_stmt_list ();
34085 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34086 != PRAGMA_OMP_SECTION)
34088 substmt = cp_parser_omp_structured_block (parser, NULL);
34089 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34090 add_stmt (substmt);
34093 while (1)
34095 tok = cp_lexer_peek_token (parser->lexer);
34096 if (tok->type == CPP_CLOSE_BRACE)
34097 break;
34098 if (tok->type == CPP_EOF)
34099 break;
34101 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34103 cp_lexer_consume_token (parser->lexer);
34104 cp_parser_require_pragma_eol (parser, tok);
34105 error_suppress = false;
34107 else if (!error_suppress)
34109 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34110 error_suppress = true;
34113 substmt = cp_parser_omp_structured_block (parser, NULL);
34114 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34115 add_stmt (substmt);
34117 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34119 substmt = pop_stmt_list (stmt);
34121 stmt = make_node (OMP_SECTIONS);
34122 TREE_TYPE (stmt) = void_type_node;
34123 OMP_SECTIONS_BODY (stmt) = substmt;
34125 add_stmt (stmt);
34126 return stmt;
34129 /* OpenMP 2.5:
34130 # pragma omp sections sections-clause[optseq] newline
34131 sections-scope */
34133 #define OMP_SECTIONS_CLAUSE_MASK \
34134 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34135 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34136 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34137 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34138 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34140 static tree
34141 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34142 char *p_name, omp_clause_mask mask, tree *cclauses)
34144 tree clauses, ret;
34145 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34147 strcat (p_name, " sections");
34148 mask |= OMP_SECTIONS_CLAUSE_MASK;
34149 if (cclauses)
34150 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34152 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34153 cclauses == NULL);
34154 if (cclauses)
34156 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34157 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34160 ret = cp_parser_omp_sections_scope (parser);
34161 if (ret)
34162 OMP_SECTIONS_CLAUSES (ret) = clauses;
34164 return ret;
34167 /* OpenMP 2.5:
34168 # pragma omp parallel parallel-clause[optseq] new-line
34169 structured-block
34170 # pragma omp parallel for parallel-for-clause[optseq] new-line
34171 structured-block
34172 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34173 structured-block
34175 OpenMP 4.0:
34176 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34177 structured-block */
34179 #define OMP_PARALLEL_CLAUSE_MASK \
34180 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34183 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34184 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34186 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34187 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34188 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34190 static tree
34191 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34192 char *p_name, omp_clause_mask mask, tree *cclauses,
34193 bool *if_p)
34195 tree stmt, clauses, block;
34196 unsigned int save;
34197 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34199 strcat (p_name, " parallel");
34200 mask |= OMP_PARALLEL_CLAUSE_MASK;
34201 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34202 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34203 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34204 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34206 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34208 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34209 if (cclauses == NULL)
34210 cclauses = cclauses_buf;
34212 cp_lexer_consume_token (parser->lexer);
34213 if (!flag_openmp) /* flag_openmp_simd */
34214 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34215 if_p);
34216 block = begin_omp_parallel ();
34217 save = cp_parser_begin_omp_structured_block (parser);
34218 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34219 if_p);
34220 cp_parser_end_omp_structured_block (parser, save);
34221 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34222 block);
34223 if (ret == NULL_TREE)
34224 return ret;
34225 OMP_PARALLEL_COMBINED (stmt) = 1;
34226 return stmt;
34228 /* When combined with distribute, parallel has to be followed by for.
34229 #pragma omp target parallel is allowed though. */
34230 else if (cclauses
34231 && (mask & (OMP_CLAUSE_MASK_1
34232 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34234 error_at (loc, "expected %<for%> after %qs", p_name);
34235 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34236 return NULL_TREE;
34238 else if (!flag_openmp) /* flag_openmp_simd */
34240 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34241 return NULL_TREE;
34243 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34245 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34246 const char *p = IDENTIFIER_POINTER (id);
34247 if (strcmp (p, "sections") == 0)
34249 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34250 cclauses = cclauses_buf;
34252 cp_lexer_consume_token (parser->lexer);
34253 block = begin_omp_parallel ();
34254 save = cp_parser_begin_omp_structured_block (parser);
34255 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34256 cp_parser_end_omp_structured_block (parser, save);
34257 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34258 block);
34259 OMP_PARALLEL_COMBINED (stmt) = 1;
34260 return stmt;
34264 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34265 cclauses == NULL);
34266 if (cclauses)
34268 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34269 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34272 block = begin_omp_parallel ();
34273 save = cp_parser_begin_omp_structured_block (parser);
34274 cp_parser_statement (parser, NULL_TREE, false, if_p);
34275 cp_parser_end_omp_structured_block (parser, save);
34276 stmt = finish_omp_parallel (clauses, block);
34277 return stmt;
34280 /* OpenMP 2.5:
34281 # pragma omp single single-clause[optseq] new-line
34282 structured-block */
34284 #define OMP_SINGLE_CLAUSE_MASK \
34285 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34290 static tree
34291 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34293 tree stmt = make_node (OMP_SINGLE);
34294 TREE_TYPE (stmt) = void_type_node;
34296 OMP_SINGLE_CLAUSES (stmt)
34297 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34298 "#pragma omp single", pragma_tok);
34299 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34301 return add_stmt (stmt);
34304 /* OpenMP 3.0:
34305 # pragma omp task task-clause[optseq] new-line
34306 structured-block */
34308 #define OMP_TASK_CLAUSE_MASK \
34309 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34310 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34320 static tree
34321 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34323 tree clauses, block;
34324 unsigned int save;
34326 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34327 "#pragma omp task", pragma_tok);
34328 block = begin_omp_task ();
34329 save = cp_parser_begin_omp_structured_block (parser);
34330 cp_parser_statement (parser, NULL_TREE, false, if_p);
34331 cp_parser_end_omp_structured_block (parser, save);
34332 return finish_omp_task (clauses, block);
34335 /* OpenMP 3.0:
34336 # pragma omp taskwait new-line */
34338 static void
34339 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34341 cp_parser_require_pragma_eol (parser, pragma_tok);
34342 finish_omp_taskwait ();
34345 /* OpenMP 3.1:
34346 # pragma omp taskyield new-line */
34348 static void
34349 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34351 cp_parser_require_pragma_eol (parser, pragma_tok);
34352 finish_omp_taskyield ();
34355 /* OpenMP 4.0:
34356 # pragma omp taskgroup new-line
34357 structured-block */
34359 static tree
34360 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34362 cp_parser_require_pragma_eol (parser, pragma_tok);
34363 return c_finish_omp_taskgroup (input_location,
34364 cp_parser_omp_structured_block (parser,
34365 if_p));
34369 /* OpenMP 2.5:
34370 # pragma omp threadprivate (variable-list) */
34372 static void
34373 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34375 tree vars;
34377 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34378 cp_parser_require_pragma_eol (parser, pragma_tok);
34380 finish_omp_threadprivate (vars);
34383 /* OpenMP 4.0:
34384 # pragma omp cancel cancel-clause[optseq] new-line */
34386 #define OMP_CANCEL_CLAUSE_MASK \
34387 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34393 static void
34394 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34396 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34397 "#pragma omp cancel", pragma_tok);
34398 finish_omp_cancel (clauses);
34401 /* OpenMP 4.0:
34402 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34404 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34405 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34410 static void
34411 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
34412 enum pragma_context context)
34414 tree clauses;
34415 bool point_seen = false;
34417 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34419 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34420 const char *p = IDENTIFIER_POINTER (id);
34422 if (strcmp (p, "point") == 0)
34424 cp_lexer_consume_token (parser->lexer);
34425 point_seen = true;
34428 if (!point_seen)
34430 cp_parser_error (parser, "expected %<point%>");
34431 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34432 return;
34435 if (context != pragma_compound)
34437 if (context == pragma_stmt)
34438 error_at (pragma_tok->location,
34439 "%<#pragma omp cancellation point%> may only be used in"
34440 " compound statements");
34441 else
34442 cp_parser_error (parser, "expected declaration specifiers");
34443 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34444 return;
34447 clauses = cp_parser_omp_all_clauses (parser,
34448 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34449 "#pragma omp cancellation point",
34450 pragma_tok);
34451 finish_omp_cancellation_point (clauses);
34454 /* OpenMP 4.0:
34455 #pragma omp distribute distribute-clause[optseq] new-line
34456 for-loop */
34458 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34459 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34465 static tree
34466 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34467 char *p_name, omp_clause_mask mask, tree *cclauses,
34468 bool *if_p)
34470 tree clauses, sb, ret;
34471 unsigned int save;
34472 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34474 strcat (p_name, " distribute");
34475 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34477 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34479 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34480 const char *p = IDENTIFIER_POINTER (id);
34481 bool simd = false;
34482 bool parallel = false;
34484 if (strcmp (p, "simd") == 0)
34485 simd = true;
34486 else
34487 parallel = strcmp (p, "parallel") == 0;
34488 if (parallel || simd)
34490 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34491 if (cclauses == NULL)
34492 cclauses = cclauses_buf;
34493 cp_lexer_consume_token (parser->lexer);
34494 if (!flag_openmp) /* flag_openmp_simd */
34496 if (simd)
34497 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34498 cclauses, if_p);
34499 else
34500 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34501 cclauses, if_p);
34503 sb = begin_omp_structured_block ();
34504 save = cp_parser_begin_omp_structured_block (parser);
34505 if (simd)
34506 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34507 cclauses, if_p);
34508 else
34509 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34510 cclauses, if_p);
34511 cp_parser_end_omp_structured_block (parser, save);
34512 tree body = finish_omp_structured_block (sb);
34513 if (ret == NULL)
34514 return ret;
34515 ret = make_node (OMP_DISTRIBUTE);
34516 TREE_TYPE (ret) = void_type_node;
34517 OMP_FOR_BODY (ret) = body;
34518 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34519 SET_EXPR_LOCATION (ret, loc);
34520 add_stmt (ret);
34521 return ret;
34524 if (!flag_openmp) /* flag_openmp_simd */
34526 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34527 return NULL_TREE;
34530 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34531 cclauses == NULL);
34532 if (cclauses)
34534 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34535 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34538 sb = begin_omp_structured_block ();
34539 save = cp_parser_begin_omp_structured_block (parser);
34541 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34543 cp_parser_end_omp_structured_block (parser, save);
34544 add_stmt (finish_omp_structured_block (sb));
34546 return ret;
34549 /* OpenMP 4.0:
34550 # pragma omp teams teams-clause[optseq] new-line
34551 structured-block */
34553 #define OMP_TEAMS_CLAUSE_MASK \
34554 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34562 static tree
34563 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34564 char *p_name, omp_clause_mask mask, tree *cclauses,
34565 bool *if_p)
34567 tree clauses, sb, ret;
34568 unsigned int save;
34569 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34571 strcat (p_name, " teams");
34572 mask |= OMP_TEAMS_CLAUSE_MASK;
34574 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34576 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34577 const char *p = IDENTIFIER_POINTER (id);
34578 if (strcmp (p, "distribute") == 0)
34580 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34581 if (cclauses == NULL)
34582 cclauses = cclauses_buf;
34584 cp_lexer_consume_token (parser->lexer);
34585 if (!flag_openmp) /* flag_openmp_simd */
34586 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34587 cclauses, if_p);
34588 sb = begin_omp_structured_block ();
34589 save = cp_parser_begin_omp_structured_block (parser);
34590 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34591 cclauses, if_p);
34592 cp_parser_end_omp_structured_block (parser, save);
34593 tree body = finish_omp_structured_block (sb);
34594 if (ret == NULL)
34595 return ret;
34596 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34597 ret = make_node (OMP_TEAMS);
34598 TREE_TYPE (ret) = void_type_node;
34599 OMP_TEAMS_CLAUSES (ret) = clauses;
34600 OMP_TEAMS_BODY (ret) = body;
34601 OMP_TEAMS_COMBINED (ret) = 1;
34602 return add_stmt (ret);
34605 if (!flag_openmp) /* flag_openmp_simd */
34607 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34608 return NULL_TREE;
34611 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34612 cclauses == NULL);
34613 if (cclauses)
34615 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34616 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34619 tree stmt = make_node (OMP_TEAMS);
34620 TREE_TYPE (stmt) = void_type_node;
34621 OMP_TEAMS_CLAUSES (stmt) = clauses;
34622 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34624 return add_stmt (stmt);
34627 /* OpenMP 4.0:
34628 # pragma omp target data target-data-clause[optseq] new-line
34629 structured-block */
34631 #define OMP_TARGET_DATA_CLAUSE_MASK \
34632 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34637 static tree
34638 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34640 tree clauses
34641 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34642 "#pragma omp target data", pragma_tok);
34643 int map_seen = 0;
34644 for (tree *pc = &clauses; *pc;)
34646 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34647 switch (OMP_CLAUSE_MAP_KIND (*pc))
34649 case GOMP_MAP_TO:
34650 case GOMP_MAP_ALWAYS_TO:
34651 case GOMP_MAP_FROM:
34652 case GOMP_MAP_ALWAYS_FROM:
34653 case GOMP_MAP_TOFROM:
34654 case GOMP_MAP_ALWAYS_TOFROM:
34655 case GOMP_MAP_ALLOC:
34656 map_seen = 3;
34657 break;
34658 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34659 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34660 case GOMP_MAP_ALWAYS_POINTER:
34661 break;
34662 default:
34663 map_seen |= 1;
34664 error_at (OMP_CLAUSE_LOCATION (*pc),
34665 "%<#pragma omp target data%> with map-type other "
34666 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34667 "on %<map%> clause");
34668 *pc = OMP_CLAUSE_CHAIN (*pc);
34669 continue;
34671 pc = &OMP_CLAUSE_CHAIN (*pc);
34674 if (map_seen != 3)
34676 if (map_seen == 0)
34677 error_at (pragma_tok->location,
34678 "%<#pragma omp target data%> must contain at least "
34679 "one %<map%> clause");
34680 return NULL_TREE;
34683 tree stmt = make_node (OMP_TARGET_DATA);
34684 TREE_TYPE (stmt) = void_type_node;
34685 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34687 keep_next_level (true);
34688 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34690 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34691 return add_stmt (stmt);
34694 /* OpenMP 4.5:
34695 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34696 structured-block */
34698 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34705 static tree
34706 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34707 enum pragma_context context)
34709 bool data_seen = false;
34710 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34712 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34713 const char *p = IDENTIFIER_POINTER (id);
34715 if (strcmp (p, "data") == 0)
34717 cp_lexer_consume_token (parser->lexer);
34718 data_seen = true;
34721 if (!data_seen)
34723 cp_parser_error (parser, "expected %<data%>");
34724 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34725 return NULL_TREE;
34728 if (context == pragma_stmt)
34730 error_at (pragma_tok->location,
34731 "%<#pragma omp target enter data%> may only be "
34732 "used in compound statements");
34733 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34734 return NULL_TREE;
34737 tree clauses
34738 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34739 "#pragma omp target enter data", pragma_tok);
34740 int map_seen = 0;
34741 for (tree *pc = &clauses; *pc;)
34743 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34744 switch (OMP_CLAUSE_MAP_KIND (*pc))
34746 case GOMP_MAP_TO:
34747 case GOMP_MAP_ALWAYS_TO:
34748 case GOMP_MAP_ALLOC:
34749 map_seen = 3;
34750 break;
34751 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34752 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34753 case GOMP_MAP_ALWAYS_POINTER:
34754 break;
34755 default:
34756 map_seen |= 1;
34757 error_at (OMP_CLAUSE_LOCATION (*pc),
34758 "%<#pragma omp target enter data%> with map-type other "
34759 "than %<to%> or %<alloc%> on %<map%> clause");
34760 *pc = OMP_CLAUSE_CHAIN (*pc);
34761 continue;
34763 pc = &OMP_CLAUSE_CHAIN (*pc);
34766 if (map_seen != 3)
34768 if (map_seen == 0)
34769 error_at (pragma_tok->location,
34770 "%<#pragma omp target enter data%> must contain at least "
34771 "one %<map%> clause");
34772 return NULL_TREE;
34775 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34776 TREE_TYPE (stmt) = void_type_node;
34777 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34778 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34779 return add_stmt (stmt);
34782 /* OpenMP 4.5:
34783 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34784 structured-block */
34786 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34787 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34793 static tree
34794 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34795 enum pragma_context context)
34797 bool data_seen = false;
34798 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34800 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34801 const char *p = IDENTIFIER_POINTER (id);
34803 if (strcmp (p, "data") == 0)
34805 cp_lexer_consume_token (parser->lexer);
34806 data_seen = true;
34809 if (!data_seen)
34811 cp_parser_error (parser, "expected %<data%>");
34812 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34813 return NULL_TREE;
34816 if (context == pragma_stmt)
34818 error_at (pragma_tok->location,
34819 "%<#pragma omp target exit data%> may only be "
34820 "used in compound statements");
34821 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34822 return NULL_TREE;
34825 tree clauses
34826 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34827 "#pragma omp target exit data", pragma_tok);
34828 int map_seen = 0;
34829 for (tree *pc = &clauses; *pc;)
34831 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34832 switch (OMP_CLAUSE_MAP_KIND (*pc))
34834 case GOMP_MAP_FROM:
34835 case GOMP_MAP_ALWAYS_FROM:
34836 case GOMP_MAP_RELEASE:
34837 case GOMP_MAP_DELETE:
34838 map_seen = 3;
34839 break;
34840 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34841 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34842 case GOMP_MAP_ALWAYS_POINTER:
34843 break;
34844 default:
34845 map_seen |= 1;
34846 error_at (OMP_CLAUSE_LOCATION (*pc),
34847 "%<#pragma omp target exit data%> with map-type other "
34848 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34849 " clause");
34850 *pc = OMP_CLAUSE_CHAIN (*pc);
34851 continue;
34853 pc = &OMP_CLAUSE_CHAIN (*pc);
34856 if (map_seen != 3)
34858 if (map_seen == 0)
34859 error_at (pragma_tok->location,
34860 "%<#pragma omp target exit data%> must contain at least "
34861 "one %<map%> clause");
34862 return NULL_TREE;
34865 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34866 TREE_TYPE (stmt) = void_type_node;
34867 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34868 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34869 return add_stmt (stmt);
34872 /* OpenMP 4.0:
34873 # pragma omp target update target-update-clause[optseq] new-line */
34875 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34876 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34883 static bool
34884 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34885 enum pragma_context context)
34887 if (context == pragma_stmt)
34889 error_at (pragma_tok->location,
34890 "%<#pragma omp target update%> may only be "
34891 "used in compound statements");
34892 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34893 return false;
34896 tree clauses
34897 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34898 "#pragma omp target update", pragma_tok);
34899 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34900 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34902 error_at (pragma_tok->location,
34903 "%<#pragma omp target update%> must contain at least one "
34904 "%<from%> or %<to%> clauses");
34905 return false;
34908 tree stmt = make_node (OMP_TARGET_UPDATE);
34909 TREE_TYPE (stmt) = void_type_node;
34910 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34911 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34912 add_stmt (stmt);
34913 return false;
34916 /* OpenMP 4.0:
34917 # pragma omp target target-clause[optseq] new-line
34918 structured-block */
34920 #define OMP_TARGET_CLAUSE_MASK \
34921 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34924 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34925 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34931 static bool
34932 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34933 enum pragma_context context, bool *if_p)
34935 tree *pc = NULL, stmt;
34937 if (context != pragma_stmt && context != pragma_compound)
34939 cp_parser_error (parser, "expected declaration specifiers");
34940 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34941 return false;
34944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34947 const char *p = IDENTIFIER_POINTER (id);
34948 enum tree_code ccode = ERROR_MARK;
34950 if (strcmp (p, "teams") == 0)
34951 ccode = OMP_TEAMS;
34952 else if (strcmp (p, "parallel") == 0)
34953 ccode = OMP_PARALLEL;
34954 else if (strcmp (p, "simd") == 0)
34955 ccode = OMP_SIMD;
34956 if (ccode != ERROR_MARK)
34958 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34959 char p_name[sizeof ("#pragma omp target teams distribute "
34960 "parallel for simd")];
34962 cp_lexer_consume_token (parser->lexer);
34963 strcpy (p_name, "#pragma omp target");
34964 if (!flag_openmp) /* flag_openmp_simd */
34966 tree stmt;
34967 switch (ccode)
34969 case OMP_TEAMS:
34970 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34971 OMP_TARGET_CLAUSE_MASK,
34972 cclauses, if_p);
34973 break;
34974 case OMP_PARALLEL:
34975 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34976 OMP_TARGET_CLAUSE_MASK,
34977 cclauses, if_p);
34978 break;
34979 case OMP_SIMD:
34980 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34981 OMP_TARGET_CLAUSE_MASK,
34982 cclauses, if_p);
34983 break;
34984 default:
34985 gcc_unreachable ();
34987 return stmt != NULL_TREE;
34989 keep_next_level (true);
34990 tree sb = begin_omp_structured_block (), ret;
34991 unsigned save = cp_parser_begin_omp_structured_block (parser);
34992 switch (ccode)
34994 case OMP_TEAMS:
34995 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34996 OMP_TARGET_CLAUSE_MASK, cclauses,
34997 if_p);
34998 break;
34999 case OMP_PARALLEL:
35000 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35001 OMP_TARGET_CLAUSE_MASK, cclauses,
35002 if_p);
35003 break;
35004 case OMP_SIMD:
35005 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35006 OMP_TARGET_CLAUSE_MASK, cclauses,
35007 if_p);
35008 break;
35009 default:
35010 gcc_unreachable ();
35012 cp_parser_end_omp_structured_block (parser, save);
35013 tree body = finish_omp_structured_block (sb);
35014 if (ret == NULL_TREE)
35015 return false;
35016 if (ccode == OMP_TEAMS && !processing_template_decl)
35018 /* For combined target teams, ensure the num_teams and
35019 thread_limit clause expressions are evaluated on the host,
35020 before entering the target construct. */
35021 tree c;
35022 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35023 c; c = OMP_CLAUSE_CHAIN (c))
35024 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35025 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35026 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35028 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35029 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35030 if (expr == error_mark_node)
35031 continue;
35032 tree tmp = TARGET_EXPR_SLOT (expr);
35033 add_stmt (expr);
35034 OMP_CLAUSE_OPERAND (c, 0) = expr;
35035 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35036 OMP_CLAUSE_FIRSTPRIVATE);
35037 OMP_CLAUSE_DECL (tc) = tmp;
35038 OMP_CLAUSE_CHAIN (tc)
35039 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35040 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35043 tree stmt = make_node (OMP_TARGET);
35044 TREE_TYPE (stmt) = void_type_node;
35045 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35046 OMP_TARGET_BODY (stmt) = body;
35047 OMP_TARGET_COMBINED (stmt) = 1;
35048 add_stmt (stmt);
35049 pc = &OMP_TARGET_CLAUSES (stmt);
35050 goto check_clauses;
35052 else if (!flag_openmp) /* flag_openmp_simd */
35054 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35055 return false;
35057 else if (strcmp (p, "data") == 0)
35059 cp_lexer_consume_token (parser->lexer);
35060 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35061 return true;
35063 else if (strcmp (p, "enter") == 0)
35065 cp_lexer_consume_token (parser->lexer);
35066 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35067 return false;
35069 else if (strcmp (p, "exit") == 0)
35071 cp_lexer_consume_token (parser->lexer);
35072 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35073 return false;
35075 else if (strcmp (p, "update") == 0)
35077 cp_lexer_consume_token (parser->lexer);
35078 return cp_parser_omp_target_update (parser, pragma_tok, context);
35082 stmt = make_node (OMP_TARGET);
35083 TREE_TYPE (stmt) = void_type_node;
35085 OMP_TARGET_CLAUSES (stmt)
35086 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35087 "#pragma omp target", pragma_tok);
35088 pc = &OMP_TARGET_CLAUSES (stmt);
35089 keep_next_level (true);
35090 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35092 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35093 add_stmt (stmt);
35095 check_clauses:
35096 while (*pc)
35098 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35099 switch (OMP_CLAUSE_MAP_KIND (*pc))
35101 case GOMP_MAP_TO:
35102 case GOMP_MAP_ALWAYS_TO:
35103 case GOMP_MAP_FROM:
35104 case GOMP_MAP_ALWAYS_FROM:
35105 case GOMP_MAP_TOFROM:
35106 case GOMP_MAP_ALWAYS_TOFROM:
35107 case GOMP_MAP_ALLOC:
35108 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35109 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35110 case GOMP_MAP_ALWAYS_POINTER:
35111 break;
35112 default:
35113 error_at (OMP_CLAUSE_LOCATION (*pc),
35114 "%<#pragma omp target%> with map-type other "
35115 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35116 "on %<map%> clause");
35117 *pc = OMP_CLAUSE_CHAIN (*pc);
35118 continue;
35120 pc = &OMP_CLAUSE_CHAIN (*pc);
35122 return true;
35125 /* OpenACC 2.0:
35126 # pragma acc cache (variable-list) new-line
35129 static tree
35130 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35132 tree stmt, clauses;
35134 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35135 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35137 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35139 stmt = make_node (OACC_CACHE);
35140 TREE_TYPE (stmt) = void_type_node;
35141 OACC_CACHE_CLAUSES (stmt) = clauses;
35142 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35143 add_stmt (stmt);
35145 return stmt;
35148 /* OpenACC 2.0:
35149 # pragma acc data oacc-data-clause[optseq] new-line
35150 structured-block */
35152 #define OACC_DATA_CLAUSE_MASK \
35153 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35154 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35165 static tree
35166 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35168 tree stmt, clauses, block;
35169 unsigned int save;
35171 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35172 "#pragma acc data", pragma_tok);
35174 block = begin_omp_parallel ();
35175 save = cp_parser_begin_omp_structured_block (parser);
35176 cp_parser_statement (parser, NULL_TREE, false, if_p);
35177 cp_parser_end_omp_structured_block (parser, save);
35178 stmt = finish_oacc_data (clauses, block);
35179 return stmt;
35182 /* OpenACC 2.0:
35183 # pragma acc host_data <clauses> new-line
35184 structured-block */
35186 #define OACC_HOST_DATA_CLAUSE_MASK \
35187 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35189 static tree
35190 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35192 tree stmt, clauses, block;
35193 unsigned int save;
35195 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35196 "#pragma acc host_data", pragma_tok);
35198 block = begin_omp_parallel ();
35199 save = cp_parser_begin_omp_structured_block (parser);
35200 cp_parser_statement (parser, NULL_TREE, false, if_p);
35201 cp_parser_end_omp_structured_block (parser, save);
35202 stmt = finish_oacc_host_data (clauses, block);
35203 return stmt;
35206 /* OpenACC 2.0:
35207 # pragma acc declare oacc-data-clause[optseq] new-line
35210 #define OACC_DECLARE_CLAUSE_MASK \
35211 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35212 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35213 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35214 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35224 static tree
35225 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35227 tree clauses, stmt;
35228 bool error = false;
35230 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35231 "#pragma acc declare", pragma_tok, true);
35234 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35236 error_at (pragma_tok->location,
35237 "no valid clauses specified in %<#pragma acc declare%>");
35238 return NULL_TREE;
35241 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35243 location_t loc = OMP_CLAUSE_LOCATION (t);
35244 tree decl = OMP_CLAUSE_DECL (t);
35245 if (!DECL_P (decl))
35247 error_at (loc, "array section in %<#pragma acc declare%>");
35248 error = true;
35249 continue;
35251 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35252 switch (OMP_CLAUSE_MAP_KIND (t))
35254 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35255 case GOMP_MAP_FORCE_ALLOC:
35256 case GOMP_MAP_FORCE_TO:
35257 case GOMP_MAP_FORCE_DEVICEPTR:
35258 case GOMP_MAP_DEVICE_RESIDENT:
35259 break;
35261 case GOMP_MAP_POINTER:
35262 /* Generated by c_finish_omp_clauses from array sections;
35263 avoid spurious diagnostics. */
35264 break;
35266 case GOMP_MAP_LINK:
35267 if (!global_bindings_p ()
35268 && (TREE_STATIC (decl)
35269 || !DECL_EXTERNAL (decl)))
35271 error_at (loc,
35272 "%qD must be a global variable in"
35273 "%<#pragma acc declare link%>",
35274 decl);
35275 error = true;
35276 continue;
35278 break;
35280 default:
35281 if (global_bindings_p ())
35283 error_at (loc, "invalid OpenACC clause at file scope");
35284 error = true;
35285 continue;
35287 if (DECL_EXTERNAL (decl))
35289 error_at (loc,
35290 "invalid use of %<extern%> variable %qD "
35291 "in %<#pragma acc declare%>", decl);
35292 error = true;
35293 continue;
35295 else if (TREE_PUBLIC (decl))
35297 error_at (loc,
35298 "invalid use of %<global%> variable %qD "
35299 "in %<#pragma acc declare%>", decl);
35300 error = true;
35301 continue;
35303 break;
35306 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35307 || lookup_attribute ("omp declare target link",
35308 DECL_ATTRIBUTES (decl)))
35310 error_at (loc, "variable %qD used more than once with "
35311 "%<#pragma acc declare%>", decl);
35312 error = true;
35313 continue;
35316 if (!error)
35318 tree id;
35320 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35321 id = get_identifier ("omp declare target link");
35322 else
35323 id = get_identifier ("omp declare target");
35325 DECL_ATTRIBUTES (decl)
35326 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35327 if (global_bindings_p ())
35329 symtab_node *node = symtab_node::get (decl);
35330 if (node != NULL)
35332 node->offloadable = 1;
35333 if (ENABLE_OFFLOADING)
35335 g->have_offload = true;
35336 if (is_a <varpool_node *> (node))
35337 vec_safe_push (offload_vars, decl);
35344 if (error || global_bindings_p ())
35345 return NULL_TREE;
35347 stmt = make_node (OACC_DECLARE);
35348 TREE_TYPE (stmt) = void_type_node;
35349 OACC_DECLARE_CLAUSES (stmt) = clauses;
35350 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35352 add_stmt (stmt);
35354 return NULL_TREE;
35357 /* OpenACC 2.0:
35358 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35362 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35364 LOC is the location of the #pragma token.
35367 #define OACC_ENTER_DATA_CLAUSE_MASK \
35368 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35374 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35376 #define OACC_EXIT_DATA_CLAUSE_MASK \
35377 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35383 static tree
35384 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35385 bool enter)
35387 tree stmt, clauses;
35389 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35390 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35392 cp_parser_error (parser, enter
35393 ? "expected %<data%> in %<#pragma acc enter data%>"
35394 : "expected %<data%> in %<#pragma acc exit data%>");
35395 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35396 return NULL_TREE;
35399 const char *p =
35400 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35401 if (strcmp (p, "data") != 0)
35403 cp_parser_error (parser, "invalid pragma");
35404 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35405 return NULL_TREE;
35408 cp_lexer_consume_token (parser->lexer);
35410 if (enter)
35411 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35412 "#pragma acc enter data", pragma_tok);
35413 else
35414 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35415 "#pragma acc exit data", pragma_tok);
35417 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35419 error_at (pragma_tok->location,
35420 "%<#pragma acc enter data%> has no data movement clause");
35421 return NULL_TREE;
35424 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35425 TREE_TYPE (stmt) = void_type_node;
35426 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35427 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35428 add_stmt (stmt);
35429 return stmt;
35432 /* OpenACC 2.0:
35433 # pragma acc loop oacc-loop-clause[optseq] new-line
35434 structured-block */
35436 #define OACC_LOOP_CLAUSE_MASK \
35437 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35438 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35439 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35448 static tree
35449 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35450 omp_clause_mask mask, tree *cclauses, bool *if_p)
35452 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35454 strcat (p_name, " loop");
35455 mask |= OACC_LOOP_CLAUSE_MASK;
35457 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35458 cclauses == NULL);
35459 if (cclauses)
35461 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35462 if (*cclauses)
35463 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
35464 if (clauses)
35465 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
35468 tree block = begin_omp_structured_block ();
35469 int save = cp_parser_begin_omp_structured_block (parser);
35470 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35471 cp_parser_end_omp_structured_block (parser, save);
35472 add_stmt (finish_omp_structured_block (block));
35474 return stmt;
35477 /* OpenACC 2.0:
35478 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35479 structured-block
35483 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35484 structured-block
35487 #define OACC_KERNELS_CLAUSE_MASK \
35488 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35490 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35491 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35492 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35493 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35494 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35501 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35503 #define OACC_PARALLEL_CLAUSE_MASK \
35504 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35522 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35523 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35525 static tree
35526 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35527 char *p_name, bool *if_p)
35529 omp_clause_mask mask;
35530 enum tree_code code;
35531 switch (cp_parser_pragma_kind (pragma_tok))
35533 case PRAGMA_OACC_KERNELS:
35534 strcat (p_name, " kernels");
35535 mask = OACC_KERNELS_CLAUSE_MASK;
35536 code = OACC_KERNELS;
35537 break;
35538 case PRAGMA_OACC_PARALLEL:
35539 strcat (p_name, " parallel");
35540 mask = OACC_PARALLEL_CLAUSE_MASK;
35541 code = OACC_PARALLEL;
35542 break;
35543 default:
35544 gcc_unreachable ();
35547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35549 const char *p
35550 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35551 if (strcmp (p, "loop") == 0)
35553 cp_lexer_consume_token (parser->lexer);
35554 tree block = begin_omp_parallel ();
35555 tree clauses;
35556 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35557 if_p);
35558 return finish_omp_construct (code, block, clauses);
35562 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35564 tree block = begin_omp_parallel ();
35565 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35566 cp_parser_statement (parser, NULL_TREE, false, if_p);
35567 cp_parser_end_omp_structured_block (parser, save);
35568 return finish_omp_construct (code, block, clauses);
35571 /* OpenACC 2.0:
35572 # pragma acc update oacc-update-clause[optseq] new-line
35575 #define OACC_UPDATE_CLAUSE_MASK \
35576 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35577 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35583 static tree
35584 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35586 tree stmt, clauses;
35588 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35589 "#pragma acc update", pragma_tok);
35591 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35593 error_at (pragma_tok->location,
35594 "%<#pragma acc update%> must contain at least one "
35595 "%<device%> or %<host%> or %<self%> clause");
35596 return NULL_TREE;
35599 stmt = make_node (OACC_UPDATE);
35600 TREE_TYPE (stmt) = void_type_node;
35601 OACC_UPDATE_CLAUSES (stmt) = clauses;
35602 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35603 add_stmt (stmt);
35604 return stmt;
35607 /* OpenACC 2.0:
35608 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35610 LOC is the location of the #pragma token.
35613 #define OACC_WAIT_CLAUSE_MASK \
35614 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35616 static tree
35617 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35619 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35620 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35622 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35623 list = cp_parser_oacc_wait_list (parser, loc, list);
35625 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35626 "#pragma acc wait", pragma_tok);
35628 stmt = c_finish_oacc_wait (loc, list, clauses);
35629 stmt = finish_expr_stmt (stmt);
35631 return stmt;
35634 /* OpenMP 4.0:
35635 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35637 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35638 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35645 static void
35646 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35647 enum pragma_context context)
35649 bool first_p = parser->omp_declare_simd == NULL;
35650 cp_omp_declare_simd_data data;
35651 if (first_p)
35653 data.error_seen = false;
35654 data.fndecl_seen = false;
35655 data.tokens = vNULL;
35656 parser->omp_declare_simd = &data;
35658 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35659 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35660 cp_lexer_consume_token (parser->lexer);
35661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35662 parser->omp_declare_simd->error_seen = true;
35663 cp_parser_require_pragma_eol (parser, pragma_tok);
35664 struct cp_token_cache *cp
35665 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35666 parser->omp_declare_simd->tokens.safe_push (cp);
35667 if (first_p)
35669 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35670 cp_parser_pragma (parser, context, NULL);
35671 switch (context)
35673 case pragma_external:
35674 cp_parser_declaration (parser);
35675 break;
35676 case pragma_member:
35677 cp_parser_member_declaration (parser);
35678 break;
35679 case pragma_objc_icode:
35680 cp_parser_block_declaration (parser, /*statement_p=*/false);
35681 break;
35682 default:
35683 cp_parser_declaration_statement (parser);
35684 break;
35686 if (parser->omp_declare_simd
35687 && !parser->omp_declare_simd->error_seen
35688 && !parser->omp_declare_simd->fndecl_seen)
35689 error_at (pragma_tok->location,
35690 "%<#pragma omp declare simd%> not immediately followed by "
35691 "function declaration or definition");
35692 data.tokens.release ();
35693 parser->omp_declare_simd = NULL;
35697 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35698 This function is modelled similar to the late parsing of omp declare
35699 simd. */
35701 static tree
35702 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35704 struct cp_token_cache *ce;
35705 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35706 int ii = 0;
35708 if (parser->omp_declare_simd != NULL
35709 || lookup_attribute ("simd", attrs))
35711 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35712 "used in the same function marked as a Cilk Plus SIMD-enabled "
35713 " function");
35714 parser->cilk_simd_fn_info->tokens.release ();
35715 XDELETE (parser->cilk_simd_fn_info);
35716 parser->cilk_simd_fn_info = NULL;
35717 return attrs;
35719 if (!info->error_seen && info->fndecl_seen)
35721 error ("vector attribute not immediately followed by a single function"
35722 " declaration or definition");
35723 info->error_seen = true;
35725 if (info->error_seen)
35726 return attrs;
35728 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35730 tree c, cl;
35732 cp_parser_push_lexer_for_tokens (parser, ce);
35733 parser->lexer->in_pragma = true;
35734 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35735 "SIMD-enabled functions attribute",
35736 NULL);
35737 cp_parser_pop_lexer (parser);
35738 if (cl)
35739 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35741 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35742 TREE_CHAIN (c) = attrs;
35743 attrs = c;
35745 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35746 TREE_CHAIN (c) = attrs;
35747 if (processing_template_decl)
35748 ATTR_IS_DEPENDENT (c) = 1;
35749 attrs = c;
35751 info->fndecl_seen = true;
35752 parser->cilk_simd_fn_info->tokens.release ();
35753 XDELETE (parser->cilk_simd_fn_info);
35754 parser->cilk_simd_fn_info = NULL;
35755 return attrs;
35758 /* Finalize #pragma omp declare simd clauses after direct declarator has
35759 been parsed, and put that into "omp declare simd" attribute. */
35761 static tree
35762 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35764 struct cp_token_cache *ce;
35765 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35766 int i;
35768 if (!data->error_seen && data->fndecl_seen)
35770 error ("%<#pragma omp declare simd%> not immediately followed by "
35771 "a single function declaration or definition");
35772 data->error_seen = true;
35773 return attrs;
35775 if (data->error_seen)
35776 return attrs;
35778 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35780 tree c, cl;
35782 cp_parser_push_lexer_for_tokens (parser, ce);
35783 parser->lexer->in_pragma = true;
35784 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35785 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35786 cp_lexer_consume_token (parser->lexer);
35787 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35788 "#pragma omp declare simd", pragma_tok);
35789 cp_parser_pop_lexer (parser);
35790 if (cl)
35791 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35792 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35793 TREE_CHAIN (c) = attrs;
35794 if (processing_template_decl)
35795 ATTR_IS_DEPENDENT (c) = 1;
35796 attrs = c;
35799 data->fndecl_seen = true;
35800 return attrs;
35804 /* OpenMP 4.0:
35805 # pragma omp declare target new-line
35806 declarations and definitions
35807 # pragma omp end declare target new-line
35809 OpenMP 4.5:
35810 # pragma omp declare target ( extended-list ) new-line
35812 # pragma omp declare target declare-target-clauses[seq] new-line */
35814 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35815 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35816 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35818 static void
35819 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35821 tree clauses = NULL_TREE;
35822 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35823 clauses
35824 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35825 "#pragma omp declare target", pragma_tok);
35826 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35828 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35829 clauses);
35830 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
35831 cp_parser_require_pragma_eol (parser, pragma_tok);
35833 else
35835 cp_parser_require_pragma_eol (parser, pragma_tok);
35836 scope_chain->omp_declare_target_attribute++;
35837 return;
35839 if (scope_chain->omp_declare_target_attribute)
35840 error_at (pragma_tok->location,
35841 "%<#pragma omp declare target%> with clauses in between "
35842 "%<#pragma omp declare target%> without clauses and "
35843 "%<#pragma omp end declare target%>");
35844 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35846 tree t = OMP_CLAUSE_DECL (c), id;
35847 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35848 tree at2 = lookup_attribute ("omp declare target link",
35849 DECL_ATTRIBUTES (t));
35850 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35852 id = get_identifier ("omp declare target link");
35853 std::swap (at1, at2);
35855 else
35856 id = get_identifier ("omp declare target");
35857 if (at2)
35859 error_at (OMP_CLAUSE_LOCATION (c),
35860 "%qD specified both in declare target %<link%> and %<to%>"
35861 " clauses", t);
35862 continue;
35864 if (!at1)
35866 symtab_node *node = symtab_node::get (t);
35867 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35868 if (node != NULL)
35870 node->offloadable = 1;
35871 if (ENABLE_OFFLOADING)
35873 g->have_offload = true;
35874 if (is_a <varpool_node *> (node))
35875 vec_safe_push (offload_vars, t);
35882 static void
35883 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35885 const char *p = "";
35886 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35888 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35889 p = IDENTIFIER_POINTER (id);
35891 if (strcmp (p, "declare") == 0)
35893 cp_lexer_consume_token (parser->lexer);
35894 p = "";
35895 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35897 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35898 p = IDENTIFIER_POINTER (id);
35900 if (strcmp (p, "target") == 0)
35901 cp_lexer_consume_token (parser->lexer);
35902 else
35904 cp_parser_error (parser, "expected %<target%>");
35905 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35906 return;
35909 else
35911 cp_parser_error (parser, "expected %<declare%>");
35912 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35913 return;
35915 cp_parser_require_pragma_eol (parser, pragma_tok);
35916 if (!scope_chain->omp_declare_target_attribute)
35917 error_at (pragma_tok->location,
35918 "%<#pragma omp end declare target%> without corresponding "
35919 "%<#pragma omp declare target%>");
35920 else
35921 scope_chain->omp_declare_target_attribute--;
35924 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35925 expression and optional initializer clause of
35926 #pragma omp declare reduction. We store the expression(s) as
35927 either 3, 6 or 7 special statements inside of the artificial function's
35928 body. The first two statements are DECL_EXPRs for the artificial
35929 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35930 expression that uses those variables.
35931 If there was any INITIALIZER clause, this is followed by further statements,
35932 the fourth and fifth statements are DECL_EXPRs for the artificial
35933 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35934 constructor variant (first token after open paren is not omp_priv),
35935 then the sixth statement is a statement with the function call expression
35936 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35937 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35938 to initialize the OMP_PRIV artificial variable and there is seventh
35939 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35941 static bool
35942 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35944 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35945 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35946 type = TREE_TYPE (type);
35947 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35948 DECL_ARTIFICIAL (omp_out) = 1;
35949 pushdecl (omp_out);
35950 add_decl_expr (omp_out);
35951 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35952 DECL_ARTIFICIAL (omp_in) = 1;
35953 pushdecl (omp_in);
35954 add_decl_expr (omp_in);
35955 tree combiner;
35956 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35958 keep_next_level (true);
35959 tree block = begin_omp_structured_block ();
35960 combiner = cp_parser_expression (parser);
35961 finish_expr_stmt (combiner);
35962 block = finish_omp_structured_block (block);
35963 add_stmt (block);
35965 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35966 return false;
35968 const char *p = "";
35969 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35971 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35972 p = IDENTIFIER_POINTER (id);
35975 if (strcmp (p, "initializer") == 0)
35977 cp_lexer_consume_token (parser->lexer);
35978 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35979 return false;
35981 p = "";
35982 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35985 p = IDENTIFIER_POINTER (id);
35988 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35989 DECL_ARTIFICIAL (omp_priv) = 1;
35990 pushdecl (omp_priv);
35991 add_decl_expr (omp_priv);
35992 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35993 DECL_ARTIFICIAL (omp_orig) = 1;
35994 pushdecl (omp_orig);
35995 add_decl_expr (omp_orig);
35997 keep_next_level (true);
35998 block = begin_omp_structured_block ();
36000 bool ctor = false;
36001 if (strcmp (p, "omp_priv") == 0)
36003 bool is_direct_init, is_non_constant_init;
36004 ctor = true;
36005 cp_lexer_consume_token (parser->lexer);
36006 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36007 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36008 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36009 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36010 == CPP_CLOSE_PAREN
36011 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36012 == CPP_CLOSE_PAREN))
36014 finish_omp_structured_block (block);
36015 error ("invalid initializer clause");
36016 return false;
36018 initializer = cp_parser_initializer (parser, &is_direct_init,
36019 &is_non_constant_init);
36020 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36021 NULL_TREE, LOOKUP_ONLYCONVERTING);
36023 else
36025 cp_parser_parse_tentatively (parser);
36026 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36027 /*check_dependency_p=*/true,
36028 /*template_p=*/NULL,
36029 /*declarator_p=*/false,
36030 /*optional_p=*/false);
36031 vec<tree, va_gc> *args;
36032 if (fn_name == error_mark_node
36033 || cp_parser_error_occurred (parser)
36034 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36035 || ((args = cp_parser_parenthesized_expression_list
36036 (parser, non_attr, /*cast_p=*/false,
36037 /*allow_expansion_p=*/true,
36038 /*non_constant_p=*/NULL)),
36039 cp_parser_error_occurred (parser)))
36041 finish_omp_structured_block (block);
36042 cp_parser_abort_tentative_parse (parser);
36043 cp_parser_error (parser, "expected id-expression (arguments)");
36044 return false;
36046 unsigned int i;
36047 tree arg;
36048 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36049 if (arg == omp_priv
36050 || (TREE_CODE (arg) == ADDR_EXPR
36051 && TREE_OPERAND (arg, 0) == omp_priv))
36052 break;
36053 cp_parser_abort_tentative_parse (parser);
36054 if (arg == NULL_TREE)
36055 error ("one of the initializer call arguments should be %<omp_priv%>"
36056 " or %<&omp_priv%>");
36057 initializer = cp_parser_postfix_expression (parser, false, false, false,
36058 false, NULL);
36059 finish_expr_stmt (initializer);
36062 block = finish_omp_structured_block (block);
36063 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36064 add_stmt (block);
36066 if (ctor)
36067 add_decl_expr (omp_orig);
36069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36070 return false;
36073 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36074 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36076 return true;
36079 /* OpenMP 4.0
36080 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36081 initializer-clause[opt] new-line
36083 initializer-clause:
36084 initializer (omp_priv initializer)
36085 initializer (function-name (argument-list)) */
36087 static void
36088 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36089 enum pragma_context)
36091 auto_vec<tree> types;
36092 enum tree_code reduc_code = ERROR_MARK;
36093 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36094 unsigned int i;
36095 cp_token *first_token;
36096 cp_token_cache *cp;
36097 int errs;
36098 void *p;
36100 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36101 p = obstack_alloc (&declarator_obstack, 0);
36103 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36104 goto fail;
36106 switch (cp_lexer_peek_token (parser->lexer)->type)
36108 case CPP_PLUS:
36109 reduc_code = PLUS_EXPR;
36110 break;
36111 case CPP_MULT:
36112 reduc_code = MULT_EXPR;
36113 break;
36114 case CPP_MINUS:
36115 reduc_code = MINUS_EXPR;
36116 break;
36117 case CPP_AND:
36118 reduc_code = BIT_AND_EXPR;
36119 break;
36120 case CPP_XOR:
36121 reduc_code = BIT_XOR_EXPR;
36122 break;
36123 case CPP_OR:
36124 reduc_code = BIT_IOR_EXPR;
36125 break;
36126 case CPP_AND_AND:
36127 reduc_code = TRUTH_ANDIF_EXPR;
36128 break;
36129 case CPP_OR_OR:
36130 reduc_code = TRUTH_ORIF_EXPR;
36131 break;
36132 case CPP_NAME:
36133 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36134 break;
36135 default:
36136 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36137 "%<|%>, %<&&%>, %<||%> or identifier");
36138 goto fail;
36141 if (reduc_code != ERROR_MARK)
36142 cp_lexer_consume_token (parser->lexer);
36144 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36145 if (reduc_id == error_mark_node)
36146 goto fail;
36148 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36149 goto fail;
36151 /* Types may not be defined in declare reduction type list. */
36152 const char *saved_message;
36153 saved_message = parser->type_definition_forbidden_message;
36154 parser->type_definition_forbidden_message
36155 = G_("types may not be defined in declare reduction type list");
36156 bool saved_colon_corrects_to_scope_p;
36157 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36158 parser->colon_corrects_to_scope_p = false;
36159 bool saved_colon_doesnt_start_class_def_p;
36160 saved_colon_doesnt_start_class_def_p
36161 = parser->colon_doesnt_start_class_def_p;
36162 parser->colon_doesnt_start_class_def_p = true;
36164 while (true)
36166 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36167 type = cp_parser_type_id (parser);
36168 if (type == error_mark_node)
36170 else if (ARITHMETIC_TYPE_P (type)
36171 && (orig_reduc_id == NULL_TREE
36172 || (TREE_CODE (type) != COMPLEX_TYPE
36173 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36174 "min") == 0
36175 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36176 "max") == 0))))
36177 error_at (loc, "predeclared arithmetic type %qT in "
36178 "%<#pragma omp declare reduction%>", type);
36179 else if (TREE_CODE (type) == FUNCTION_TYPE
36180 || TREE_CODE (type) == METHOD_TYPE
36181 || TREE_CODE (type) == ARRAY_TYPE)
36182 error_at (loc, "function or array type %qT in "
36183 "%<#pragma omp declare reduction%>", type);
36184 else if (TREE_CODE (type) == REFERENCE_TYPE)
36185 error_at (loc, "reference type %qT in "
36186 "%<#pragma omp declare reduction%>", type);
36187 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36188 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36189 "%<#pragma omp declare reduction%>", type);
36190 else
36191 types.safe_push (type);
36193 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36194 cp_lexer_consume_token (parser->lexer);
36195 else
36196 break;
36199 /* Restore the saved message. */
36200 parser->type_definition_forbidden_message = saved_message;
36201 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36202 parser->colon_doesnt_start_class_def_p
36203 = saved_colon_doesnt_start_class_def_p;
36205 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36206 || types.is_empty ())
36208 fail:
36209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36210 goto done;
36213 first_token = cp_lexer_peek_token (parser->lexer);
36214 cp = NULL;
36215 errs = errorcount;
36216 FOR_EACH_VEC_ELT (types, i, type)
36218 tree fntype
36219 = build_function_type_list (void_type_node,
36220 cp_build_reference_type (type, false),
36221 NULL_TREE);
36222 tree this_reduc_id = reduc_id;
36223 if (!dependent_type_p (type))
36224 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36225 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36226 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36227 DECL_ARTIFICIAL (fndecl) = 1;
36228 DECL_EXTERNAL (fndecl) = 1;
36229 DECL_DECLARED_INLINE_P (fndecl) = 1;
36230 DECL_IGNORED_P (fndecl) = 1;
36231 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36232 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36233 DECL_ATTRIBUTES (fndecl)
36234 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36235 DECL_ATTRIBUTES (fndecl));
36236 if (processing_template_decl)
36237 fndecl = push_template_decl (fndecl);
36238 bool block_scope = false;
36239 tree block = NULL_TREE;
36240 if (current_function_decl)
36242 block_scope = true;
36243 DECL_CONTEXT (fndecl) = global_namespace;
36244 if (!processing_template_decl)
36245 pushdecl (fndecl);
36247 else if (current_class_type)
36249 if (cp == NULL)
36251 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36252 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36253 cp_lexer_consume_token (parser->lexer);
36254 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36255 goto fail;
36256 cp = cp_token_cache_new (first_token,
36257 cp_lexer_peek_nth_token (parser->lexer,
36258 2));
36260 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36261 finish_member_declaration (fndecl);
36262 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36263 DECL_PENDING_INLINE_P (fndecl) = 1;
36264 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36265 continue;
36267 else
36269 DECL_CONTEXT (fndecl) = current_namespace;
36270 pushdecl (fndecl);
36272 if (!block_scope)
36273 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36274 else
36275 block = begin_omp_structured_block ();
36276 if (cp)
36278 cp_parser_push_lexer_for_tokens (parser, cp);
36279 parser->lexer->in_pragma = true;
36281 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36283 if (!block_scope)
36284 finish_function (0);
36285 else
36286 DECL_CONTEXT (fndecl) = current_function_decl;
36287 if (cp)
36288 cp_parser_pop_lexer (parser);
36289 goto fail;
36291 if (cp)
36292 cp_parser_pop_lexer (parser);
36293 if (!block_scope)
36294 finish_function (0);
36295 else
36297 DECL_CONTEXT (fndecl) = current_function_decl;
36298 block = finish_omp_structured_block (block);
36299 if (TREE_CODE (block) == BIND_EXPR)
36300 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36301 else if (TREE_CODE (block) == STATEMENT_LIST)
36302 DECL_SAVED_TREE (fndecl) = block;
36303 if (processing_template_decl)
36304 add_decl_expr (fndecl);
36306 cp_check_omp_declare_reduction (fndecl);
36307 if (cp == NULL && types.length () > 1)
36308 cp = cp_token_cache_new (first_token,
36309 cp_lexer_peek_nth_token (parser->lexer, 2));
36310 if (errs != errorcount)
36311 break;
36314 cp_parser_require_pragma_eol (parser, pragma_tok);
36316 done:
36317 /* Free any declarators allocated. */
36318 obstack_free (&declarator_obstack, p);
36321 /* OpenMP 4.0
36322 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36323 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36324 initializer-clause[opt] new-line
36325 #pragma omp declare target new-line */
36327 static void
36328 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36329 enum pragma_context context)
36331 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36333 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36334 const char *p = IDENTIFIER_POINTER (id);
36336 if (strcmp (p, "simd") == 0)
36338 cp_lexer_consume_token (parser->lexer);
36339 cp_parser_omp_declare_simd (parser, pragma_tok,
36340 context);
36341 return;
36343 cp_ensure_no_omp_declare_simd (parser);
36344 if (strcmp (p, "reduction") == 0)
36346 cp_lexer_consume_token (parser->lexer);
36347 cp_parser_omp_declare_reduction (parser, pragma_tok,
36348 context);
36349 return;
36351 if (!flag_openmp) /* flag_openmp_simd */
36353 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36354 return;
36356 if (strcmp (p, "target") == 0)
36358 cp_lexer_consume_token (parser->lexer);
36359 cp_parser_omp_declare_target (parser, pragma_tok);
36360 return;
36363 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36364 "or %<target%>");
36365 cp_parser_require_pragma_eol (parser, pragma_tok);
36368 /* OpenMP 4.5:
36369 #pragma omp taskloop taskloop-clause[optseq] new-line
36370 for-loop
36372 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36373 for-loop */
36375 #define OMP_TASKLOOP_CLAUSE_MASK \
36376 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36382 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36383 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36384 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36385 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36386 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36391 static tree
36392 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36393 char *p_name, omp_clause_mask mask, tree *cclauses,
36394 bool *if_p)
36396 tree clauses, sb, ret;
36397 unsigned int save;
36398 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36400 strcat (p_name, " taskloop");
36401 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36405 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36406 const char *p = IDENTIFIER_POINTER (id);
36408 if (strcmp (p, "simd") == 0)
36410 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36411 if (cclauses == NULL)
36412 cclauses = cclauses_buf;
36414 cp_lexer_consume_token (parser->lexer);
36415 if (!flag_openmp) /* flag_openmp_simd */
36416 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36417 cclauses, if_p);
36418 sb = begin_omp_structured_block ();
36419 save = cp_parser_begin_omp_structured_block (parser);
36420 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36421 cclauses, if_p);
36422 cp_parser_end_omp_structured_block (parser, save);
36423 tree body = finish_omp_structured_block (sb);
36424 if (ret == NULL)
36425 return ret;
36426 ret = make_node (OMP_TASKLOOP);
36427 TREE_TYPE (ret) = void_type_node;
36428 OMP_FOR_BODY (ret) = body;
36429 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36430 SET_EXPR_LOCATION (ret, loc);
36431 add_stmt (ret);
36432 return ret;
36435 if (!flag_openmp) /* flag_openmp_simd */
36437 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36438 return NULL_TREE;
36441 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36442 cclauses == NULL);
36443 if (cclauses)
36445 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36446 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36449 sb = begin_omp_structured_block ();
36450 save = cp_parser_begin_omp_structured_block (parser);
36452 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36453 if_p);
36455 cp_parser_end_omp_structured_block (parser, save);
36456 add_stmt (finish_omp_structured_block (sb));
36458 return ret;
36462 /* OpenACC 2.0:
36463 # pragma acc routine oacc-routine-clause[optseq] new-line
36464 function-definition
36466 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36469 #define OACC_ROUTINE_CLAUSE_MASK \
36470 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36476 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36477 component, which must resolve to a declared namespace-scope
36478 function. The clauses are either processed directly (for a named
36479 function), or defered until the immediatley following declaration
36480 is parsed. */
36482 static void
36483 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36484 enum pragma_context context)
36486 bool first_p = parser->oacc_routine == NULL;
36487 location_t loc = pragma_tok->location;
36488 cp_omp_declare_simd_data data;
36489 if (first_p)
36491 data.error_seen = false;
36492 data.fndecl_seen = false;
36493 data.tokens = vNULL;
36494 data.clauses = NULL_TREE;
36495 parser->oacc_routine = &data;
36498 tree decl = NULL_TREE;
36499 /* Create a dummy claue, to record location. */
36500 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36502 if (context != pragma_external)
36504 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36505 parser->oacc_routine->error_seen = true;
36506 parser->oacc_routine = NULL;
36507 return;
36510 /* Look for optional '( name )'. */
36511 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36513 if (!first_p)
36515 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36516 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36517 cp_lexer_consume_token (parser->lexer);
36518 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36519 parser->oacc_routine->error_seen = true;
36520 cp_parser_require_pragma_eol (parser, pragma_tok);
36522 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36523 "%<#pragma acc routine%> not followed by a "
36524 "function declaration or definition");
36526 parser->oacc_routine->error_seen = true;
36527 return;
36530 cp_lexer_consume_token (parser->lexer);
36531 cp_token *token = cp_lexer_peek_token (parser->lexer);
36533 /* We parse the name as an id-expression. If it resolves to
36534 anything other than a non-overloaded function at namespace
36535 scope, it's an error. */
36536 tree id = cp_parser_id_expression (parser,
36537 /*template_keyword_p=*/false,
36538 /*check_dependency_p=*/false,
36539 /*template_p=*/NULL,
36540 /*declarator_p=*/false,
36541 /*optional_p=*/false);
36542 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36543 if (id != error_mark_node && decl == error_mark_node)
36544 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36545 token->location);
36547 if (decl == error_mark_node
36548 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36550 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36551 parser->oacc_routine = NULL;
36552 return;
36555 /* Build a chain of clauses. */
36556 parser->lexer->in_pragma = true;
36557 tree clauses = NULL_TREE;
36558 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36559 "#pragma acc routine",
36560 cp_lexer_peek_token
36561 (parser->lexer));
36563 /* Force clauses to be non-null, by attaching context to it. */
36564 clauses = tree_cons (c_head, clauses, NULL_TREE);
36566 if (decl && is_overloaded_fn (decl)
36567 && (TREE_CODE (decl) != FUNCTION_DECL
36568 || DECL_FUNCTION_TEMPLATE_P (decl)))
36570 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36571 parser->oacc_routine = NULL;
36572 return;
36575 /* Perhaps we should use the same rule as declarations in different
36576 namespaces? */
36577 if (!DECL_NAMESPACE_SCOPE_P (decl))
36579 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36580 "namespace scope function");
36581 parser->oacc_routine = NULL;
36582 return;
36585 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36587 error_at (loc,
36588 "%<#pragma acc routine%> does not refer to a function");
36589 parser->oacc_routine = NULL;
36590 return;
36593 data.clauses = clauses;
36595 cp_finalize_oacc_routine (parser, decl, false);
36596 data.tokens.release ();
36597 parser->oacc_routine = NULL;
36599 else
36601 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36602 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36603 cp_lexer_consume_token (parser->lexer);
36604 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36605 parser->oacc_routine->error_seen = true;
36606 cp_parser_require_pragma_eol (parser, pragma_tok);
36608 struct cp_token_cache *cp
36609 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36610 parser->oacc_routine->tokens.safe_push (cp);
36612 if (first_p)
36613 parser->oacc_routine->clauses = c_head;
36615 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36616 cp_parser_pragma (parser, context, NULL);
36618 if (first_p)
36620 /* Create an empty list of clauses. */
36621 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36622 NULL_TREE);
36623 cp_parser_declaration (parser);
36625 if (parser->oacc_routine
36626 && !parser->oacc_routine->error_seen
36627 && !parser->oacc_routine->fndecl_seen)
36628 error_at (loc, "%<#pragma acc routine%> not followed by a "
36629 "function declaration or definition");
36631 data.tokens.release ();
36632 parser->oacc_routine = NULL;
36637 /* Finalize #pragma acc routine clauses after direct declarator has
36638 been parsed, and put that into "oacc function" attribute. */
36640 static tree
36641 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36643 struct cp_token_cache *ce;
36644 cp_omp_declare_simd_data *data = parser->oacc_routine;
36645 tree cl, clauses = parser->oacc_routine->clauses;
36646 location_t loc;
36648 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36650 if ((!data->error_seen && data->fndecl_seen)
36651 || data->tokens.length () != 1)
36653 error_at (loc, "%<#pragma acc routine%> not followed by a "
36654 "function declaration or definition");
36655 data->error_seen = true;
36656 return attrs;
36658 if (data->error_seen)
36659 return attrs;
36661 ce = data->tokens[0];
36663 cp_parser_push_lexer_for_tokens (parser, ce);
36664 parser->lexer->in_pragma = true;
36665 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36667 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36668 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36669 "#pragma acc routine", pragma_tok);
36670 cp_parser_pop_lexer (parser);
36672 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36674 /* Force clauses to be non-null, by attaching context to it. */
36675 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36677 data->fndecl_seen = true;
36678 return attrs;
36681 /* Apply any saved OpenACC routine clauses to a just-parsed
36682 declaration. */
36684 static void
36685 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36687 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36689 tree clauses = parser->oacc_routine->clauses;
36690 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36692 if (parser->oacc_routine->error_seen)
36693 return;
36695 if (fndecl == error_mark_node)
36697 parser->oacc_routine = NULL;
36698 return;
36701 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36703 cp_ensure_no_oacc_routine (parser);
36704 return;
36707 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36709 error_at (loc,
36710 "%<#pragma acc routine%> not followed by a function "
36711 "declaration or definition");
36712 parser->oacc_routine = NULL;
36715 if (get_oacc_fn_attrib (fndecl))
36717 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36718 fndecl);
36719 parser->oacc_routine = NULL;
36722 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36724 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36725 TREE_USED (fndecl) ? "use" : "definition");
36726 parser->oacc_routine = NULL;
36729 /* Process for function attrib */
36730 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36731 replace_oacc_fn_attrib (fndecl, dims);
36733 /* Add an "omp target" attribute. */
36734 DECL_ATTRIBUTES (fndecl)
36735 = tree_cons (get_identifier ("omp declare target"),
36736 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36740 /* Main entry point to OpenMP statement pragmas. */
36742 static void
36743 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36745 tree stmt;
36746 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36747 omp_clause_mask mask (0);
36749 switch (cp_parser_pragma_kind (pragma_tok))
36751 case PRAGMA_OACC_ATOMIC:
36752 cp_parser_omp_atomic (parser, pragma_tok);
36753 return;
36754 case PRAGMA_OACC_CACHE:
36755 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36756 break;
36757 case PRAGMA_OACC_DATA:
36758 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36759 break;
36760 case PRAGMA_OACC_ENTER_DATA:
36761 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36762 break;
36763 case PRAGMA_OACC_EXIT_DATA:
36764 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36765 break;
36766 case PRAGMA_OACC_HOST_DATA:
36767 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36768 break;
36769 case PRAGMA_OACC_KERNELS:
36770 case PRAGMA_OACC_PARALLEL:
36771 strcpy (p_name, "#pragma acc");
36772 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36773 if_p);
36774 break;
36775 case PRAGMA_OACC_LOOP:
36776 strcpy (p_name, "#pragma acc");
36777 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36778 if_p);
36779 break;
36780 case PRAGMA_OACC_UPDATE:
36781 stmt = cp_parser_oacc_update (parser, pragma_tok);
36782 break;
36783 case PRAGMA_OACC_WAIT:
36784 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36785 break;
36786 case PRAGMA_OMP_ATOMIC:
36787 cp_parser_omp_atomic (parser, pragma_tok);
36788 return;
36789 case PRAGMA_OMP_CRITICAL:
36790 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36791 break;
36792 case PRAGMA_OMP_DISTRIBUTE:
36793 strcpy (p_name, "#pragma omp");
36794 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36795 if_p);
36796 break;
36797 case PRAGMA_OMP_FOR:
36798 strcpy (p_name, "#pragma omp");
36799 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36800 if_p);
36801 break;
36802 case PRAGMA_OMP_MASTER:
36803 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36804 break;
36805 case PRAGMA_OMP_PARALLEL:
36806 strcpy (p_name, "#pragma omp");
36807 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36808 if_p);
36809 break;
36810 case PRAGMA_OMP_SECTIONS:
36811 strcpy (p_name, "#pragma omp");
36812 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36813 break;
36814 case PRAGMA_OMP_SIMD:
36815 strcpy (p_name, "#pragma omp");
36816 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36817 if_p);
36818 break;
36819 case PRAGMA_OMP_SINGLE:
36820 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36821 break;
36822 case PRAGMA_OMP_TASK:
36823 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36824 break;
36825 case PRAGMA_OMP_TASKGROUP:
36826 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36827 break;
36828 case PRAGMA_OMP_TASKLOOP:
36829 strcpy (p_name, "#pragma omp");
36830 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36831 if_p);
36832 break;
36833 case PRAGMA_OMP_TEAMS:
36834 strcpy (p_name, "#pragma omp");
36835 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36836 if_p);
36837 break;
36838 default:
36839 gcc_unreachable ();
36842 protected_set_expr_location (stmt, pragma_tok->location);
36845 /* Transactional Memory parsing routines. */
36847 /* Parse a transaction attribute.
36849 txn-attribute:
36850 attribute
36851 [ [ identifier ] ]
36853 We use this instead of cp_parser_attributes_opt for transactions to avoid
36854 the pedwarn in C++98 mode. */
36856 static tree
36857 cp_parser_txn_attribute_opt (cp_parser *parser)
36859 cp_token *token;
36860 tree attr_name, attr = NULL;
36862 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36863 return cp_parser_attributes_opt (parser);
36865 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36866 return NULL_TREE;
36867 cp_lexer_consume_token (parser->lexer);
36868 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36869 goto error1;
36871 token = cp_lexer_peek_token (parser->lexer);
36872 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36874 token = cp_lexer_consume_token (parser->lexer);
36876 attr_name = (token->type == CPP_KEYWORD
36877 /* For keywords, use the canonical spelling,
36878 not the parsed identifier. */
36879 ? ridpointers[(int) token->keyword]
36880 : token->u.value);
36881 attr = build_tree_list (attr_name, NULL_TREE);
36883 else
36884 cp_parser_error (parser, "expected identifier");
36886 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36887 error1:
36888 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36889 return attr;
36892 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36894 transaction-statement:
36895 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36896 compound-statement
36897 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36900 static tree
36901 cp_parser_transaction (cp_parser *parser, cp_token *token)
36903 unsigned char old_in = parser->in_transaction;
36904 unsigned char this_in = 1, new_in;
36905 enum rid keyword = token->keyword;
36906 tree stmt, attrs, noex;
36908 cp_lexer_consume_token (parser->lexer);
36910 if (keyword == RID_TRANSACTION_RELAXED
36911 || keyword == RID_SYNCHRONIZED)
36912 this_in |= TM_STMT_ATTR_RELAXED;
36913 else
36915 attrs = cp_parser_txn_attribute_opt (parser);
36916 if (attrs)
36917 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36920 /* Parse a noexcept specification. */
36921 if (keyword == RID_ATOMIC_NOEXCEPT)
36922 noex = boolean_true_node;
36923 else if (keyword == RID_ATOMIC_CANCEL)
36925 /* cancel-and-throw is unimplemented. */
36926 sorry ("atomic_cancel");
36927 noex = NULL_TREE;
36929 else
36930 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36932 /* Keep track if we're in the lexical scope of an outer transaction. */
36933 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36935 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36937 parser->in_transaction = new_in;
36938 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36939 parser->in_transaction = old_in;
36941 finish_transaction_stmt (stmt, NULL, this_in, noex);
36943 return stmt;
36946 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36948 transaction-expression:
36949 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36950 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36953 static tree
36954 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36956 unsigned char old_in = parser->in_transaction;
36957 unsigned char this_in = 1;
36958 cp_token *token;
36959 tree expr, noex;
36960 bool noex_expr;
36961 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36963 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36964 || keyword == RID_TRANSACTION_RELAXED);
36966 if (!flag_tm)
36967 error_at (loc,
36968 keyword == RID_TRANSACTION_RELAXED
36969 ? G_("%<__transaction_relaxed%> without transactional memory "
36970 "support enabled")
36971 : G_("%<__transaction_atomic%> without transactional memory "
36972 "support enabled"));
36974 token = cp_parser_require_keyword (parser, keyword,
36975 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36976 : RT_TRANSACTION_RELAXED));
36977 gcc_assert (token != NULL);
36979 if (keyword == RID_TRANSACTION_RELAXED)
36980 this_in |= TM_STMT_ATTR_RELAXED;
36982 /* Set this early. This might mean that we allow transaction_cancel in
36983 an expression that we find out later actually has to be a constexpr.
36984 However, we expect that cxx_constant_value will be able to deal with
36985 this; also, if the noexcept has no constexpr, then what we parse next
36986 really is a transaction's body. */
36987 parser->in_transaction = this_in;
36989 /* Parse a noexcept specification. */
36990 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36991 true);
36993 if (!noex || !noex_expr
36994 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36996 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36998 expr = cp_parser_expression (parser);
36999 expr = finish_parenthesized_expr (expr);
37001 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37003 else
37005 /* The only expression that is available got parsed for the noexcept
37006 already. noexcept is true then. */
37007 expr = noex;
37008 noex = boolean_true_node;
37011 expr = build_transaction_expr (token->location, expr, this_in, noex);
37012 parser->in_transaction = old_in;
37014 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37015 return error_mark_node;
37017 return (flag_tm ? expr : error_mark_node);
37020 /* Parse a function-transaction-block.
37022 function-transaction-block:
37023 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37024 function-body
37025 __transaction_atomic txn-attribute[opt] function-try-block
37026 __transaction_relaxed ctor-initializer[opt] function-body
37027 __transaction_relaxed function-try-block
37030 static bool
37031 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37033 unsigned char old_in = parser->in_transaction;
37034 unsigned char new_in = 1;
37035 tree compound_stmt, stmt, attrs;
37036 bool ctor_initializer_p;
37037 cp_token *token;
37039 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37040 || keyword == RID_TRANSACTION_RELAXED);
37041 token = cp_parser_require_keyword (parser, keyword,
37042 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37043 : RT_TRANSACTION_RELAXED));
37044 gcc_assert (token != NULL);
37046 if (keyword == RID_TRANSACTION_RELAXED)
37047 new_in |= TM_STMT_ATTR_RELAXED;
37048 else
37050 attrs = cp_parser_txn_attribute_opt (parser);
37051 if (attrs)
37052 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37055 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37057 parser->in_transaction = new_in;
37059 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37060 ctor_initializer_p = cp_parser_function_try_block (parser);
37061 else
37062 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37063 (parser, /*in_function_try_block=*/false);
37065 parser->in_transaction = old_in;
37067 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37069 return ctor_initializer_p;
37072 /* Parse a __transaction_cancel statement.
37074 cancel-statement:
37075 __transaction_cancel txn-attribute[opt] ;
37076 __transaction_cancel txn-attribute[opt] throw-expression ;
37078 ??? Cancel and throw is not yet implemented. */
37080 static tree
37081 cp_parser_transaction_cancel (cp_parser *parser)
37083 cp_token *token;
37084 bool is_outer = false;
37085 tree stmt, attrs;
37087 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37088 RT_TRANSACTION_CANCEL);
37089 gcc_assert (token != NULL);
37091 attrs = cp_parser_txn_attribute_opt (parser);
37092 if (attrs)
37093 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37095 /* ??? Parse cancel-and-throw here. */
37097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37099 if (!flag_tm)
37101 error_at (token->location, "%<__transaction_cancel%> without "
37102 "transactional memory support enabled");
37103 return error_mark_node;
37105 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37107 error_at (token->location, "%<__transaction_cancel%> within a "
37108 "%<__transaction_relaxed%>");
37109 return error_mark_node;
37111 else if (is_outer)
37113 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37114 && !is_tm_may_cancel_outer (current_function_decl))
37116 error_at (token->location, "outer %<__transaction_cancel%> not "
37117 "within outer %<__transaction_atomic%>");
37118 error_at (token->location,
37119 " or a %<transaction_may_cancel_outer%> function");
37120 return error_mark_node;
37123 else if (parser->in_transaction == 0)
37125 error_at (token->location, "%<__transaction_cancel%> not within "
37126 "%<__transaction_atomic%>");
37127 return error_mark_node;
37130 stmt = build_tm_abort_call (token->location, is_outer);
37131 add_stmt (stmt);
37133 return stmt;
37136 /* The parser. */
37138 static GTY (()) cp_parser *the_parser;
37141 /* Special handling for the first token or line in the file. The first
37142 thing in the file might be #pragma GCC pch_preprocess, which loads a
37143 PCH file, which is a GC collection point. So we need to handle this
37144 first pragma without benefit of an existing lexer structure.
37146 Always returns one token to the caller in *FIRST_TOKEN. This is
37147 either the true first token of the file, or the first token after
37148 the initial pragma. */
37150 static void
37151 cp_parser_initial_pragma (cp_token *first_token)
37153 tree name = NULL;
37155 cp_lexer_get_preprocessor_token (NULL, first_token);
37156 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37157 return;
37159 cp_lexer_get_preprocessor_token (NULL, first_token);
37160 if (first_token->type == CPP_STRING)
37162 name = first_token->u.value;
37164 cp_lexer_get_preprocessor_token (NULL, first_token);
37165 if (first_token->type != CPP_PRAGMA_EOL)
37166 error_at (first_token->location,
37167 "junk at end of %<#pragma GCC pch_preprocess%>");
37169 else
37170 error_at (first_token->location, "expected string literal");
37172 /* Skip to the end of the pragma. */
37173 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37174 cp_lexer_get_preprocessor_token (NULL, first_token);
37176 /* Now actually load the PCH file. */
37177 if (name)
37178 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37180 /* Read one more token to return to our caller. We have to do this
37181 after reading the PCH file in, since its pointers have to be
37182 live. */
37183 cp_lexer_get_preprocessor_token (NULL, first_token);
37186 /* Parses the grainsize pragma for the _Cilk_for statement.
37187 Syntax:
37188 #pragma cilk grainsize = <VALUE>. */
37190 static void
37191 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37193 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37195 tree exp = cp_parser_binary_expression (parser, false, false,
37196 PREC_NOT_OPERATOR, NULL);
37197 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37198 if (!exp || exp == error_mark_node)
37200 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37201 return;
37204 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37206 cp_parser_cilk_for (parser, exp, if_p);
37207 else
37208 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37209 "%<#pragma cilk grainsize%> is not followed by "
37210 "%<_Cilk_for%>");
37211 return;
37213 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37216 /* Normal parsing of a pragma token. Here we can (and must) use the
37217 regular lexer. */
37219 static bool
37220 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37222 cp_token *pragma_tok;
37223 unsigned int id;
37224 tree stmt;
37225 bool ret;
37227 pragma_tok = cp_lexer_consume_token (parser->lexer);
37228 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37229 parser->lexer->in_pragma = true;
37231 id = cp_parser_pragma_kind (pragma_tok);
37232 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
37233 cp_ensure_no_omp_declare_simd (parser);
37234 switch (id)
37236 case PRAGMA_GCC_PCH_PREPROCESS:
37237 error_at (pragma_tok->location,
37238 "%<#pragma GCC pch_preprocess%> must be first");
37239 break;
37241 case PRAGMA_OMP_BARRIER:
37242 switch (context)
37244 case pragma_compound:
37245 cp_parser_omp_barrier (parser, pragma_tok);
37246 return false;
37247 case pragma_stmt:
37248 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37249 "used in compound statements");
37250 break;
37251 default:
37252 goto bad_stmt;
37254 break;
37256 case PRAGMA_OMP_FLUSH:
37257 switch (context)
37259 case pragma_compound:
37260 cp_parser_omp_flush (parser, pragma_tok);
37261 return false;
37262 case pragma_stmt:
37263 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37264 "used in compound statements");
37265 break;
37266 default:
37267 goto bad_stmt;
37269 break;
37271 case PRAGMA_OMP_TASKWAIT:
37272 switch (context)
37274 case pragma_compound:
37275 cp_parser_omp_taskwait (parser, pragma_tok);
37276 return false;
37277 case pragma_stmt:
37278 error_at (pragma_tok->location,
37279 "%<#pragma omp taskwait%> may only be "
37280 "used in compound statements");
37281 break;
37282 default:
37283 goto bad_stmt;
37285 break;
37287 case PRAGMA_OMP_TASKYIELD:
37288 switch (context)
37290 case pragma_compound:
37291 cp_parser_omp_taskyield (parser, pragma_tok);
37292 return false;
37293 case pragma_stmt:
37294 error_at (pragma_tok->location,
37295 "%<#pragma omp taskyield%> may only be "
37296 "used in compound statements");
37297 break;
37298 default:
37299 goto bad_stmt;
37301 break;
37303 case PRAGMA_OMP_CANCEL:
37304 switch (context)
37306 case pragma_compound:
37307 cp_parser_omp_cancel (parser, pragma_tok);
37308 return false;
37309 case pragma_stmt:
37310 error_at (pragma_tok->location,
37311 "%<#pragma omp cancel%> may only be "
37312 "used in compound statements");
37313 break;
37314 default:
37315 goto bad_stmt;
37317 break;
37319 case PRAGMA_OMP_CANCELLATION_POINT:
37320 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
37321 return false;
37323 case PRAGMA_OMP_THREADPRIVATE:
37324 cp_parser_omp_threadprivate (parser, pragma_tok);
37325 return false;
37327 case PRAGMA_OMP_DECLARE:
37328 cp_parser_omp_declare (parser, pragma_tok, context);
37329 return false;
37331 case PRAGMA_OACC_DECLARE:
37332 cp_parser_oacc_declare (parser, pragma_tok);
37333 return false;
37335 case PRAGMA_OACC_ROUTINE:
37336 cp_parser_oacc_routine (parser, pragma_tok, context);
37337 return false;
37339 case PRAGMA_OACC_ATOMIC:
37340 case PRAGMA_OACC_CACHE:
37341 case PRAGMA_OACC_DATA:
37342 case PRAGMA_OACC_ENTER_DATA:
37343 case PRAGMA_OACC_EXIT_DATA:
37344 case PRAGMA_OACC_HOST_DATA:
37345 case PRAGMA_OACC_KERNELS:
37346 case PRAGMA_OACC_PARALLEL:
37347 case PRAGMA_OACC_LOOP:
37348 case PRAGMA_OACC_UPDATE:
37349 case PRAGMA_OACC_WAIT:
37350 case PRAGMA_OMP_ATOMIC:
37351 case PRAGMA_OMP_CRITICAL:
37352 case PRAGMA_OMP_DISTRIBUTE:
37353 case PRAGMA_OMP_FOR:
37354 case PRAGMA_OMP_MASTER:
37355 case PRAGMA_OMP_PARALLEL:
37356 case PRAGMA_OMP_SECTIONS:
37357 case PRAGMA_OMP_SIMD:
37358 case PRAGMA_OMP_SINGLE:
37359 case PRAGMA_OMP_TASK:
37360 case PRAGMA_OMP_TASKGROUP:
37361 case PRAGMA_OMP_TASKLOOP:
37362 case PRAGMA_OMP_TEAMS:
37363 if (context != pragma_stmt && context != pragma_compound)
37364 goto bad_stmt;
37365 stmt = push_omp_privatization_clauses (false);
37366 cp_parser_omp_construct (parser, pragma_tok, if_p);
37367 pop_omp_privatization_clauses (stmt);
37368 return true;
37370 case PRAGMA_OMP_ORDERED:
37371 stmt = push_omp_privatization_clauses (false);
37372 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37373 pop_omp_privatization_clauses (stmt);
37374 return ret;
37376 case PRAGMA_OMP_TARGET:
37377 stmt = push_omp_privatization_clauses (false);
37378 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37379 pop_omp_privatization_clauses (stmt);
37380 return ret;
37382 case PRAGMA_OMP_END_DECLARE_TARGET:
37383 cp_parser_omp_end_declare_target (parser, pragma_tok);
37384 return false;
37386 case PRAGMA_OMP_SECTION:
37387 error_at (pragma_tok->location,
37388 "%<#pragma omp section%> may only be used in "
37389 "%<#pragma omp sections%> construct");
37390 break;
37392 case PRAGMA_IVDEP:
37394 if (context == pragma_external)
37396 error_at (pragma_tok->location,
37397 "%<#pragma GCC ivdep%> must be inside a function");
37398 break;
37400 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37401 cp_token *tok;
37402 tok = cp_lexer_peek_token (the_parser->lexer);
37403 if (tok->type != CPP_KEYWORD
37404 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37405 && tok->keyword != RID_DO))
37407 cp_parser_error (parser, "for, while or do statement expected");
37408 return false;
37410 cp_parser_iteration_statement (parser, if_p, true);
37411 return true;
37414 case PRAGMA_CILK_SIMD:
37415 if (context == pragma_external)
37417 error_at (pragma_tok->location,
37418 "%<#pragma simd%> must be inside a function");
37419 break;
37421 stmt = push_omp_privatization_clauses (false);
37422 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37423 pop_omp_privatization_clauses (stmt);
37424 return true;
37426 case PRAGMA_CILK_GRAINSIZE:
37427 if (context == pragma_external)
37429 error_at (pragma_tok->location,
37430 "%<#pragma cilk grainsize%> must be inside a function");
37431 break;
37434 /* Ignore the pragma if Cilk Plus is not enabled. */
37435 if (flag_cilkplus)
37437 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37438 return true;
37440 else
37442 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37443 "%<#pragma cilk grainsize%>");
37444 break;
37447 default:
37448 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37449 c_invoke_pragma_handler (id);
37450 break;
37452 bad_stmt:
37453 cp_parser_error (parser, "expected declaration specifiers");
37454 break;
37457 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37458 return false;
37461 /* The interface the pragma parsers have to the lexer. */
37463 enum cpp_ttype
37464 pragma_lex (tree *value, location_t *loc)
37466 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37467 enum cpp_ttype ret = tok->type;
37469 *value = tok->u.value;
37470 if (loc)
37471 *loc = tok->location;
37473 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37474 ret = CPP_EOF;
37475 else if (ret == CPP_STRING)
37476 *value = cp_parser_string_literal (the_parser, false, false);
37477 else
37479 if (ret == CPP_KEYWORD)
37480 ret = CPP_NAME;
37481 cp_lexer_consume_token (the_parser->lexer);
37484 return ret;
37488 /* External interface. */
37490 /* Parse one entire translation unit. */
37492 void
37493 c_parse_file (void)
37495 static bool already_called = false;
37497 if (already_called)
37498 fatal_error (input_location,
37499 "inter-module optimizations not implemented for C++");
37500 already_called = true;
37502 the_parser = cp_parser_new ();
37503 push_deferring_access_checks (flag_access_control
37504 ? dk_no_deferred : dk_no_check);
37505 cp_parser_translation_unit (the_parser);
37506 the_parser = NULL;
37509 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37510 vectorlength clause:
37511 Syntax:
37512 vectorlength ( constant-expression ) */
37514 static tree
37515 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37516 bool is_simd_fn)
37518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37519 tree expr;
37520 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37521 safelen clause. Thus, vectorlength is represented as OMP 4.0
37522 safelen. For SIMD-enabled function it is represented by OMP 4.0
37523 simdlen. */
37524 if (!is_simd_fn)
37525 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37526 loc);
37527 else
37528 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37529 loc);
37531 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37532 return error_mark_node;
37534 expr = cp_parser_constant_expression (parser);
37535 expr = maybe_constant_value (expr);
37537 /* If expr == error_mark_node, then don't emit any errors nor
37538 create a clause. if any of the above functions returns
37539 error mark node then they would have emitted an error message. */
37540 if (expr == error_mark_node)
37542 else if (!TREE_TYPE (expr)
37543 || !TREE_CONSTANT (expr)
37544 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37545 error_at (loc, "vectorlength must be an integer constant");
37546 else if (TREE_CONSTANT (expr)
37547 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37548 error_at (loc, "vectorlength must be a power of 2");
37549 else
37551 tree c;
37552 if (!is_simd_fn)
37554 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37555 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37556 OMP_CLAUSE_CHAIN (c) = clauses;
37557 clauses = c;
37559 else
37561 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37562 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37563 OMP_CLAUSE_CHAIN (c) = clauses;
37564 clauses = c;
37568 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37569 return error_mark_node;
37570 return clauses;
37573 /* Handles the Cilk Plus #pragma simd linear clause.
37574 Syntax:
37575 linear ( simd-linear-variable-list )
37577 simd-linear-variable-list:
37578 simd-linear-variable
37579 simd-linear-variable-list , simd-linear-variable
37581 simd-linear-variable:
37582 id-expression
37583 id-expression : simd-linear-step
37585 simd-linear-step:
37586 conditional-expression */
37588 static tree
37589 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37591 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37593 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37594 return clauses;
37595 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37597 cp_parser_error (parser, "expected identifier");
37598 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37599 return error_mark_node;
37602 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37603 parser->colon_corrects_to_scope_p = false;
37604 while (1)
37606 cp_token *token = cp_lexer_peek_token (parser->lexer);
37607 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37609 cp_parser_error (parser, "expected variable-name");
37610 clauses = error_mark_node;
37611 break;
37614 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37615 false, false);
37616 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37617 token->location);
37618 if (decl == error_mark_node)
37620 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37621 token->location);
37622 clauses = error_mark_node;
37624 else
37626 tree e = NULL_TREE;
37627 tree step_size = integer_one_node;
37629 /* If present, parse the linear step. Otherwise, assume the default
37630 value of 1. */
37631 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37633 cp_lexer_consume_token (parser->lexer);
37635 e = cp_parser_assignment_expression (parser);
37636 e = maybe_constant_value (e);
37638 if (e == error_mark_node)
37640 /* If an error has occurred, then the whole pragma is
37641 considered ill-formed. Thus, no reason to keep
37642 parsing. */
37643 clauses = error_mark_node;
37644 break;
37646 else if (type_dependent_expression_p (e)
37647 || value_dependent_expression_p (e)
37648 || (TREE_TYPE (e)
37649 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37650 && (TREE_CONSTANT (e)
37651 || DECL_P (e))))
37652 step_size = e;
37653 else
37654 cp_parser_error (parser,
37655 "step size must be an integer constant "
37656 "expression or an integer variable");
37659 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37660 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37661 OMP_CLAUSE_DECL (l) = decl;
37662 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37663 OMP_CLAUSE_CHAIN (l) = clauses;
37664 clauses = l;
37666 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37667 cp_lexer_consume_token (parser->lexer);
37668 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37669 break;
37670 else
37672 error_at (cp_lexer_peek_token (parser->lexer)->location,
37673 "expected %<,%> or %<)%> after %qE", decl);
37674 clauses = error_mark_node;
37675 break;
37678 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37679 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37680 return clauses;
37683 /* Returns the name of the next clause. If the clause is not
37684 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37685 token is not consumed. Otherwise, the appropriate enum from the
37686 pragma_simd_clause is returned and the token is consumed. */
37688 static pragma_omp_clause
37689 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37691 pragma_omp_clause clause_type;
37692 cp_token *token = cp_lexer_peek_token (parser->lexer);
37694 if (token->keyword == RID_PRIVATE)
37695 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37696 else if (!token->u.value || token->type != CPP_NAME)
37697 return PRAGMA_CILK_CLAUSE_NONE;
37698 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37699 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37700 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37701 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37702 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37703 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37704 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37705 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37706 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37707 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37708 else
37709 return PRAGMA_CILK_CLAUSE_NONE;
37711 cp_lexer_consume_token (parser->lexer);
37712 return clause_type;
37715 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37717 static tree
37718 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37720 tree clauses = NULL_TREE;
37722 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37723 && clauses != error_mark_node)
37725 pragma_omp_clause c_kind;
37726 c_kind = cp_parser_cilk_simd_clause_name (parser);
37727 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37728 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37729 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37730 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37731 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37732 /* Use the OpenMP 4.0 equivalent function. */
37733 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37734 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37735 /* Use the OpenMP 4.0 equivalent function. */
37736 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37737 clauses);
37738 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37739 /* Use the OMP 4.0 equivalent function. */
37740 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37741 clauses);
37742 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37743 /* Use the OMP 4.0 equivalent function. */
37744 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37745 else
37747 clauses = error_mark_node;
37748 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37749 break;
37753 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37755 if (clauses == error_mark_node)
37756 return error_mark_node;
37757 else
37758 return finish_omp_clauses (clauses, C_ORT_CILK);
37761 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37763 static void
37764 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37766 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37768 if (clauses == error_mark_node)
37769 return;
37771 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37773 error_at (cp_lexer_peek_token (parser->lexer)->location,
37774 "for statement expected");
37775 return;
37778 tree sb = begin_omp_structured_block ();
37779 int save = cp_parser_begin_omp_structured_block (parser);
37780 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37781 if (ret)
37782 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37783 cp_parser_end_omp_structured_block (parser, save);
37784 add_stmt (finish_omp_structured_block (sb));
37787 /* Main entry-point for parsing Cilk Plus _Cilk_for
37788 loops. The return value is error_mark_node
37789 when errors happen and CILK_FOR tree on success. */
37791 static tree
37792 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37794 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37795 gcc_unreachable ();
37797 tree sb = begin_omp_structured_block ();
37798 int save = cp_parser_begin_omp_structured_block (parser);
37800 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37801 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37802 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37803 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
37805 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37806 if (ret)
37807 cpp_validate_cilk_plus_loop (ret);
37808 else
37809 ret = error_mark_node;
37811 cp_parser_end_omp_structured_block (parser, save);
37812 add_stmt (finish_omp_structured_block (sb));
37813 return ret;
37816 /* Create an identifier for a generic parameter type (a synthesized
37817 template parameter implied by `auto' or a concept identifier). */
37819 static GTY(()) int generic_parm_count;
37820 static tree
37821 make_generic_type_name ()
37823 char buf[32];
37824 sprintf (buf, "auto:%d", ++generic_parm_count);
37825 return get_identifier (buf);
37828 /* Predicate that behaves as is_auto_or_concept but matches the parent
37829 node of the generic type rather than the generic type itself. This
37830 allows for type transformation in add_implicit_template_parms. */
37832 static inline bool
37833 tree_type_is_auto_or_concept (const_tree t)
37835 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37838 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37839 (creating a new template parameter list if necessary). Returns the newly
37840 created template type parm. */
37842 static tree
37843 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37845 gcc_assert (current_binding_level->kind == sk_function_parms);
37847 /* Before committing to modifying any scope, if we're in an
37848 implicit template scope, and we're trying to synthesize a
37849 constrained parameter, try to find a previous parameter with
37850 the same name. This is the same-type rule for abbreviated
37851 function templates. */
37852 if (parser->implicit_template_scope && constr)
37854 tree t = parser->implicit_template_parms;
37855 while (t)
37857 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37859 tree d = TREE_VALUE (t);
37860 if (TREE_CODE (d) == PARM_DECL)
37861 /* Return the TEMPLATE_PARM_INDEX. */
37862 d = DECL_INITIAL (d);
37863 return d;
37865 t = TREE_CHAIN (t);
37869 /* We are either continuing a function template that already contains implicit
37870 template parameters, creating a new fully-implicit function template, or
37871 extending an existing explicit function template with implicit template
37872 parameters. */
37874 cp_binding_level *const entry_scope = current_binding_level;
37876 bool become_template = false;
37877 cp_binding_level *parent_scope = 0;
37879 if (parser->implicit_template_scope)
37881 gcc_assert (parser->implicit_template_parms);
37883 current_binding_level = parser->implicit_template_scope;
37885 else
37887 /* Roll back to the existing template parameter scope (in the case of
37888 extending an explicit function template) or introduce a new template
37889 parameter scope ahead of the function parameter scope (or class scope
37890 in the case of out-of-line member definitions). The function scope is
37891 added back after template parameter synthesis below. */
37893 cp_binding_level *scope = entry_scope;
37895 while (scope->kind == sk_function_parms)
37897 parent_scope = scope;
37898 scope = scope->level_chain;
37900 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37902 /* If not defining a class, then any class scope is a scope level in
37903 an out-of-line member definition. In this case simply wind back
37904 beyond the first such scope to inject the template parameter list.
37905 Otherwise wind back to the class being defined. The latter can
37906 occur in class member friend declarations such as:
37908 class A {
37909 void foo (auto);
37911 class B {
37912 friend void A::foo (auto);
37915 The template parameter list synthesized for the friend declaration
37916 must be injected in the scope of 'B'. This can also occur in
37917 erroneous cases such as:
37919 struct A {
37920 struct B {
37921 void foo (auto);
37923 void B::foo (auto) {}
37926 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37927 but, nevertheless, the template parameter list synthesized for the
37928 declarator should be injected into the scope of 'A' as if the
37929 ill-formed template was specified explicitly. */
37931 while (scope->kind == sk_class && !scope->defining_class_p)
37933 parent_scope = scope;
37934 scope = scope->level_chain;
37938 current_binding_level = scope;
37940 if (scope->kind != sk_template_parms
37941 || !function_being_declared_is_template_p (parser))
37943 /* Introduce a new template parameter list for implicit template
37944 parameters. */
37946 become_template = true;
37948 parser->implicit_template_scope
37949 = begin_scope (sk_template_parms, NULL);
37951 ++processing_template_decl;
37953 parser->fully_implicit_function_template_p = true;
37954 ++parser->num_template_parameter_lists;
37956 else
37958 /* Synthesize implicit template parameters at the end of the explicit
37959 template parameter list. */
37961 gcc_assert (current_template_parms);
37963 parser->implicit_template_scope = scope;
37965 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37966 parser->implicit_template_parms
37967 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37971 /* Synthesize a new template parameter and track the current template
37972 parameter chain with implicit_template_parms. */
37974 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37975 tree synth_id = make_generic_type_name ();
37976 tree synth_tmpl_parm;
37977 bool non_type = false;
37979 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37980 synth_tmpl_parm
37981 = finish_template_type_parm (class_type_node, synth_id);
37982 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37983 synth_tmpl_parm
37984 = finish_constrained_template_template_parm (proto, synth_id);
37985 else
37987 synth_tmpl_parm = copy_decl (proto);
37988 DECL_NAME (synth_tmpl_parm) = synth_id;
37989 non_type = true;
37992 // Attach the constraint to the parm before processing.
37993 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37994 TREE_TYPE (node) = constr;
37995 tree new_parm
37996 = process_template_parm (parser->implicit_template_parms,
37997 input_location,
37998 node,
37999 /*non_type=*/non_type,
38000 /*param_pack=*/false);
38002 // Chain the new parameter to the list of implicit parameters.
38003 if (parser->implicit_template_parms)
38004 parser->implicit_template_parms
38005 = TREE_CHAIN (parser->implicit_template_parms);
38006 else
38007 parser->implicit_template_parms = new_parm;
38009 tree new_decl = getdecls ();
38010 if (non_type)
38011 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38012 new_decl = DECL_INITIAL (new_decl);
38014 /* If creating a fully implicit function template, start the new implicit
38015 template parameter list with this synthesized type, otherwise grow the
38016 current template parameter list. */
38018 if (become_template)
38020 parent_scope->level_chain = current_binding_level;
38022 tree new_parms = make_tree_vec (1);
38023 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38024 current_template_parms = tree_cons (size_int (processing_template_decl),
38025 new_parms, current_template_parms);
38027 else
38029 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38030 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38031 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38032 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38035 // If the new parameter was constrained, we need to add that to the
38036 // constraints in the template parameter list.
38037 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38039 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38040 reqs = conjoin_constraints (reqs, req);
38041 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38044 current_binding_level = entry_scope;
38046 return new_decl;
38049 /* Finish the declaration of a fully implicit function template. Such a
38050 template has no explicit template parameter list so has not been through the
38051 normal template head and tail processing. synthesize_implicit_template_parm
38052 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38053 provided if the declaration is a class member such that its template
38054 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38055 form is returned. Otherwise NULL_TREE is returned. */
38057 static tree
38058 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38060 gcc_assert (parser->fully_implicit_function_template_p);
38062 if (member_decl_opt && member_decl_opt != error_mark_node
38063 && DECL_VIRTUAL_P (member_decl_opt))
38065 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38066 "implicit templates may not be %<virtual%>");
38067 DECL_VIRTUAL_P (member_decl_opt) = false;
38070 if (member_decl_opt)
38071 member_decl_opt = finish_member_template_decl (member_decl_opt);
38072 end_template_decl ();
38074 parser->fully_implicit_function_template_p = false;
38075 --parser->num_template_parameter_lists;
38077 return member_decl_opt;
38080 #include "gt-cp-parser.h"