2016-05-02 Tristan Gingold <gingold@adacore.com>
[official-gcc.git] / gcc / cp / parser.c
blobded0dee6b5fac4133868dc29dd190ad3d121e2ac
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;
3897 gcc_obstack_init (&str_ob);
3898 count = 0;
3902 last_tok_loc = tok->location;
3903 cp_lexer_consume_token (parser->lexer);
3904 count++;
3905 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3906 str.len = TREE_STRING_LENGTH (string_tree);
3908 if (curr_tok_is_userdef_p)
3910 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3911 if (have_suffix_p == 0)
3913 suffix_id = curr_suffix_id;
3914 have_suffix_p = 1;
3916 else if (have_suffix_p == 1
3917 && curr_suffix_id != suffix_id)
3919 error ("inconsistent user-defined literal suffixes"
3920 " %qD and %qD in string literal",
3921 suffix_id, curr_suffix_id);
3922 have_suffix_p = -1;
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3926 else
3927 curr_type = tok->type;
3929 if (type != curr_type)
3931 if (type == CPP_STRING)
3932 type = curr_type;
3933 else if (curr_type != CPP_STRING)
3934 error_at (tok->location,
3935 "unsupported non-standard concatenation "
3936 "of string literals");
3939 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3941 tok = cp_lexer_peek_token (parser->lexer);
3942 if (cpp_userdef_string_p (tok->type))
3944 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3945 curr_type = cpp_userdef_string_remove_type (tok->type);
3946 curr_tok_is_userdef_p = true;
3948 else
3950 string_tree = tok->u.value;
3951 curr_type = tok->type;
3952 curr_tok_is_userdef_p = false;
3955 while (cp_parser_is_string_literal (tok));
3957 /* A string literal built by concatenation has its caret=start at
3958 the start of the initial string, and its finish at the finish of
3959 the final string literal. */
3960 loc = make_location (loc, loc, get_finish (last_tok_loc));
3962 strs = (cpp_string *) obstack_finish (&str_ob);
3965 if (type != CPP_STRING && !wide_ok)
3967 cp_parser_error (parser, "a wide string is invalid in this context");
3968 type = CPP_STRING;
3971 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3972 (parse_in, strs, count, &istr, type))
3974 value = build_string (istr.len, (const char *)istr.text);
3975 free (CONST_CAST (unsigned char *, istr.text));
3977 switch (type)
3979 default:
3980 case CPP_STRING:
3981 case CPP_UTF8STRING:
3982 TREE_TYPE (value) = char_array_type_node;
3983 break;
3984 case CPP_STRING16:
3985 TREE_TYPE (value) = char16_array_type_node;
3986 break;
3987 case CPP_STRING32:
3988 TREE_TYPE (value) = char32_array_type_node;
3989 break;
3990 case CPP_WSTRING:
3991 TREE_TYPE (value) = wchar_array_type_node;
3992 break;
3995 value = fix_string_type (value);
3997 if (have_suffix_p)
3999 tree literal = build_userdef_literal (suffix_id, value,
4000 OT_NONE, NULL_TREE);
4001 if (lookup_udlit)
4002 value = cp_parser_userdef_string_literal (literal);
4003 else
4004 value = literal;
4007 else
4008 /* cpp_interpret_string has issued an error. */
4009 value = error_mark_node;
4011 if (count > 1)
4012 obstack_free (&str_ob, 0);
4014 return cp_expr (value, loc);
4017 /* Look up a literal operator with the name and the exact arguments. */
4019 static tree
4020 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4022 tree decl, fns;
4023 decl = lookup_name (name);
4024 if (!decl || !is_overloaded_fn (decl))
4025 return error_mark_node;
4027 for (fns = decl; fns; fns = OVL_NEXT (fns))
4029 unsigned int ix;
4030 bool found = true;
4031 tree fn = OVL_CURRENT (fns);
4032 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4033 if (parmtypes != NULL_TREE)
4035 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4036 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4038 tree tparm = TREE_VALUE (parmtypes);
4039 tree targ = TREE_TYPE ((*args)[ix]);
4040 bool ptr = TYPE_PTR_P (tparm);
4041 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4042 if ((ptr || arr || !same_type_p (tparm, targ))
4043 && (!ptr || !arr
4044 || !same_type_p (TREE_TYPE (tparm),
4045 TREE_TYPE (targ))))
4046 found = false;
4048 if (found
4049 && ix == vec_safe_length (args)
4050 /* May be this should be sufficient_parms_p instead,
4051 depending on how exactly should user-defined literals
4052 work in presence of default arguments on the literal
4053 operator parameters. */
4054 && parmtypes == void_list_node)
4055 return decl;
4059 return error_mark_node;
4062 /* Parse a user-defined char constant. Returns a call to a user-defined
4063 literal operator taking the character as an argument. */
4065 static cp_expr
4066 cp_parser_userdef_char_literal (cp_parser *parser)
4068 cp_token *token = cp_lexer_consume_token (parser->lexer);
4069 tree literal = token->u.value;
4070 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4071 tree value = USERDEF_LITERAL_VALUE (literal);
4072 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4073 tree decl, result;
4075 /* Build up a call to the user-defined operator */
4076 /* Lookup the name we got back from the id-expression. */
4077 vec<tree, va_gc> *args = make_tree_vector ();
4078 vec_safe_push (args, value);
4079 decl = lookup_literal_operator (name, args);
4080 if (!decl || decl == error_mark_node)
4082 error ("unable to find character literal operator %qD with %qT argument",
4083 name, TREE_TYPE (value));
4084 release_tree_vector (args);
4085 return error_mark_node;
4087 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4088 release_tree_vector (args);
4089 return result;
4092 /* A subroutine of cp_parser_userdef_numeric_literal to
4093 create a char... template parameter pack from a string node. */
4095 static tree
4096 make_char_string_pack (tree value)
4098 tree charvec;
4099 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4100 const char *str = TREE_STRING_POINTER (value);
4101 int i, len = TREE_STRING_LENGTH (value) - 1;
4102 tree argvec = make_tree_vec (1);
4104 /* Fill in CHARVEC with all of the parameters. */
4105 charvec = make_tree_vec (len);
4106 for (i = 0; i < len; ++i)
4107 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4109 /* Build the argument packs. */
4110 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4111 TREE_TYPE (argpack) = char_type_node;
4113 TREE_VEC_ELT (argvec, 0) = argpack;
4115 return argvec;
4118 /* A subroutine of cp_parser_userdef_numeric_literal to
4119 create a char... template parameter pack from a string node. */
4121 static tree
4122 make_string_pack (tree value)
4124 tree charvec;
4125 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4126 const unsigned char *str
4127 = (const unsigned char *) TREE_STRING_POINTER (value);
4128 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4129 int len = TREE_STRING_LENGTH (value) / sz - 1;
4130 tree argvec = make_tree_vec (2);
4132 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4133 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4135 /* First template parm is character type. */
4136 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4138 /* Fill in CHARVEC with all of the parameters. */
4139 charvec = make_tree_vec (len);
4140 for (int i = 0; i < len; ++i)
4141 TREE_VEC_ELT (charvec, i)
4142 = double_int_to_tree (str_char_type_node,
4143 double_int::from_buffer (str + i * sz, sz));
4145 /* Build the argument packs. */
4146 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4147 TREE_TYPE (argpack) = str_char_type_node;
4149 TREE_VEC_ELT (argvec, 1) = argpack;
4151 return argvec;
4154 /* Parse a user-defined numeric constant. returns a call to a user-defined
4155 literal operator. */
4157 static cp_expr
4158 cp_parser_userdef_numeric_literal (cp_parser *parser)
4160 cp_token *token = cp_lexer_consume_token (parser->lexer);
4161 tree literal = token->u.value;
4162 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4163 tree value = USERDEF_LITERAL_VALUE (literal);
4164 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4165 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4166 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4167 tree decl, result;
4168 vec<tree, va_gc> *args;
4170 /* Look for a literal operator taking the exact type of numeric argument
4171 as the literal value. */
4172 args = make_tree_vector ();
4173 vec_safe_push (args, value);
4174 decl = lookup_literal_operator (name, args);
4175 if (decl && decl != error_mark_node)
4177 result = finish_call_expr (decl, &args, false, true,
4178 tf_warning_or_error);
4180 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4182 warning_at (token->location, OPT_Woverflow,
4183 "integer literal exceeds range of %qT type",
4184 long_long_unsigned_type_node);
4186 else
4188 if (overflow > 0)
4189 warning_at (token->location, OPT_Woverflow,
4190 "floating literal exceeds range of %qT type",
4191 long_double_type_node);
4192 else if (overflow < 0)
4193 warning_at (token->location, OPT_Woverflow,
4194 "floating literal truncated to zero");
4197 release_tree_vector (args);
4198 return result;
4200 release_tree_vector (args);
4202 /* If the numeric argument didn't work, look for a raw literal
4203 operator taking a const char* argument consisting of the number
4204 in string format. */
4205 args = make_tree_vector ();
4206 vec_safe_push (args, num_string);
4207 decl = lookup_literal_operator (name, args);
4208 if (decl && decl != error_mark_node)
4210 result = finish_call_expr (decl, &args, false, true,
4211 tf_warning_or_error);
4212 release_tree_vector (args);
4213 return result;
4215 release_tree_vector (args);
4217 /* If the raw literal didn't work, look for a non-type template
4218 function with parameter pack char.... Call the function with
4219 template parameter characters representing the number. */
4220 args = make_tree_vector ();
4221 decl = lookup_literal_operator (name, args);
4222 if (decl && decl != error_mark_node)
4224 tree tmpl_args = make_char_string_pack (num_string);
4225 decl = lookup_template_function (decl, tmpl_args);
4226 result = finish_call_expr (decl, &args, false, true,
4227 tf_warning_or_error);
4228 release_tree_vector (args);
4229 return result;
4232 release_tree_vector (args);
4234 error ("unable to find numeric literal operator %qD", name);
4235 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4236 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4237 "to enable more built-in suffixes");
4238 return error_mark_node;
4241 /* Parse a user-defined string constant. Returns a call to a user-defined
4242 literal operator taking a character pointer and the length of the string
4243 as arguments. */
4245 static tree
4246 cp_parser_userdef_string_literal (tree literal)
4248 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4249 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4250 tree value = USERDEF_LITERAL_VALUE (literal);
4251 int len = TREE_STRING_LENGTH (value)
4252 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4253 tree decl, result;
4254 vec<tree, va_gc> *args;
4256 /* Build up a call to the user-defined operator. */
4257 /* Lookup the name we got back from the id-expression. */
4258 args = make_tree_vector ();
4259 vec_safe_push (args, value);
4260 vec_safe_push (args, build_int_cst (size_type_node, len));
4261 decl = lookup_literal_operator (name, args);
4263 if (decl && decl != error_mark_node)
4265 result = finish_call_expr (decl, &args, false, true,
4266 tf_warning_or_error);
4267 release_tree_vector (args);
4268 return result;
4270 release_tree_vector (args);
4272 /* Look for a template function with typename parameter CharT
4273 and parameter pack CharT... Call the function with
4274 template parameter characters representing the string. */
4275 args = make_tree_vector ();
4276 decl = lookup_literal_operator (name, args);
4277 if (decl && decl != error_mark_node)
4279 tree tmpl_args = make_string_pack (value);
4280 decl = lookup_template_function (decl, tmpl_args);
4281 result = finish_call_expr (decl, &args, false, true,
4282 tf_warning_or_error);
4283 release_tree_vector (args);
4284 return result;
4286 release_tree_vector (args);
4288 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4289 name, TREE_TYPE (value), size_type_node);
4290 return error_mark_node;
4294 /* Basic concepts [gram.basic] */
4296 /* Parse a translation-unit.
4298 translation-unit:
4299 declaration-seq [opt]
4301 Returns TRUE if all went well. */
4303 static bool
4304 cp_parser_translation_unit (cp_parser* parser)
4306 /* The address of the first non-permanent object on the declarator
4307 obstack. */
4308 static void *declarator_obstack_base;
4310 bool success;
4312 /* Create the declarator obstack, if necessary. */
4313 if (!cp_error_declarator)
4315 gcc_obstack_init (&declarator_obstack);
4316 /* Create the error declarator. */
4317 cp_error_declarator = make_declarator (cdk_error);
4318 /* Create the empty parameter list. */
4319 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4320 /* Remember where the base of the declarator obstack lies. */
4321 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4324 cp_parser_declaration_seq_opt (parser);
4326 /* If there are no tokens left then all went well. */
4327 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4329 /* Get rid of the token array; we don't need it any more. */
4330 cp_lexer_destroy (parser->lexer);
4331 parser->lexer = NULL;
4333 /* This file might have been a context that's implicitly extern
4334 "C". If so, pop the lang context. (Only relevant for PCH.) */
4335 if (parser->implicit_extern_c)
4337 pop_lang_context ();
4338 parser->implicit_extern_c = false;
4341 /* Finish up. */
4342 finish_translation_unit ();
4344 success = true;
4346 else
4348 cp_parser_error (parser, "expected declaration");
4349 success = false;
4352 /* Make sure the declarator obstack was fully cleaned up. */
4353 gcc_assert (obstack_next_free (&declarator_obstack)
4354 == declarator_obstack_base);
4356 /* All went well. */
4357 return success;
4360 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4361 decltype context. */
4363 static inline tsubst_flags_t
4364 complain_flags (bool decltype_p)
4366 tsubst_flags_t complain = tf_warning_or_error;
4367 if (decltype_p)
4368 complain |= tf_decltype;
4369 return complain;
4372 /* We're about to parse a collection of statements. If we're currently
4373 parsing tentatively, set up a firewall so that any nested
4374 cp_parser_commit_to_tentative_parse won't affect the current context. */
4376 static cp_token_position
4377 cp_parser_start_tentative_firewall (cp_parser *parser)
4379 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4380 return 0;
4382 cp_parser_parse_tentatively (parser);
4383 cp_parser_commit_to_topmost_tentative_parse (parser);
4384 return cp_lexer_token_position (parser->lexer, false);
4387 /* We've finished parsing the collection of statements. Wrap up the
4388 firewall and replace the relevant tokens with the parsed form. */
4390 static void
4391 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4392 tree expr)
4394 if (!start)
4395 return;
4397 /* Finish the firewall level. */
4398 cp_parser_parse_definitely (parser);
4399 /* And remember the result of the parse for when we try again. */
4400 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4401 token->type = CPP_PREPARSED_EXPR;
4402 token->u.value = expr;
4403 token->keyword = RID_MAX;
4404 cp_lexer_purge_tokens_after (parser->lexer, start);
4407 /* Like the above functions, but let the user modify the tokens. Used by
4408 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4409 later parses, so it makes sense to localize the effects of
4410 cp_parser_commit_to_tentative_parse. */
4412 struct tentative_firewall
4414 cp_parser *parser;
4415 bool set;
4417 tentative_firewall (cp_parser *p): parser(p)
4419 /* If we're currently parsing tentatively, start a committed level as a
4420 firewall and then an inner tentative parse. */
4421 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4423 cp_parser_parse_tentatively (parser);
4424 cp_parser_commit_to_topmost_tentative_parse (parser);
4425 cp_parser_parse_tentatively (parser);
4429 ~tentative_firewall()
4431 if (set)
4433 /* Finish the inner tentative parse and the firewall, propagating any
4434 uncommitted error state to the outer tentative parse. */
4435 bool err = cp_parser_error_occurred (parser);
4436 cp_parser_parse_definitely (parser);
4437 cp_parser_parse_definitely (parser);
4438 if (err)
4439 cp_parser_simulate_error (parser);
4444 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4445 enclosing parentheses. */
4447 static cp_expr
4448 cp_parser_statement_expr (cp_parser *parser)
4450 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4452 /* Consume the '('. */
4453 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4454 cp_lexer_consume_token (parser->lexer);
4455 /* Start the statement-expression. */
4456 tree expr = begin_stmt_expr ();
4457 /* Parse the compound-statement. */
4458 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4459 /* Finish up. */
4460 expr = finish_stmt_expr (expr, false);
4461 /* Consume the ')'. */
4462 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4463 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4464 cp_parser_skip_to_end_of_statement (parser);
4466 cp_parser_end_tentative_firewall (parser, start, expr);
4467 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4468 return cp_expr (expr, combined_loc);
4471 /* Expressions [gram.expr] */
4473 /* Parse a fold-operator.
4475 fold-operator:
4476 - * / % ^ & | = < > << >>
4477 = -= *= /= %= ^= &= |= <<= >>=
4478 == != <= >= && || , .* ->*
4480 This returns the tree code corresponding to the matched operator
4481 as an int. When the current token matches a compound assignment
4482 opertor, the resulting tree code is the negative value of the
4483 non-assignment operator. */
4485 static int
4486 cp_parser_fold_operator (cp_token *token)
4488 switch (token->type)
4490 case CPP_PLUS: return PLUS_EXPR;
4491 case CPP_MINUS: return MINUS_EXPR;
4492 case CPP_MULT: return MULT_EXPR;
4493 case CPP_DIV: return TRUNC_DIV_EXPR;
4494 case CPP_MOD: return TRUNC_MOD_EXPR;
4495 case CPP_XOR: return BIT_XOR_EXPR;
4496 case CPP_AND: return BIT_AND_EXPR;
4497 case CPP_OR: return BIT_IOR_EXPR;
4498 case CPP_LSHIFT: return LSHIFT_EXPR;
4499 case CPP_RSHIFT: return RSHIFT_EXPR;
4501 case CPP_EQ: return -NOP_EXPR;
4502 case CPP_PLUS_EQ: return -PLUS_EXPR;
4503 case CPP_MINUS_EQ: return -MINUS_EXPR;
4504 case CPP_MULT_EQ: return -MULT_EXPR;
4505 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4506 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4507 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4508 case CPP_AND_EQ: return -BIT_AND_EXPR;
4509 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4510 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4511 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4513 case CPP_EQ_EQ: return EQ_EXPR;
4514 case CPP_NOT_EQ: return NE_EXPR;
4515 case CPP_LESS: return LT_EXPR;
4516 case CPP_GREATER: return GT_EXPR;
4517 case CPP_LESS_EQ: return LE_EXPR;
4518 case CPP_GREATER_EQ: return GE_EXPR;
4520 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4521 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4523 case CPP_COMMA: return COMPOUND_EXPR;
4525 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4526 case CPP_DEREF_STAR: return MEMBER_REF;
4528 default: return ERROR_MARK;
4532 /* Returns true if CODE indicates a binary expression, which is not allowed in
4533 the LHS of a fold-expression. More codes will need to be added to use this
4534 function in other contexts. */
4536 static bool
4537 is_binary_op (tree_code code)
4539 switch (code)
4541 case PLUS_EXPR:
4542 case POINTER_PLUS_EXPR:
4543 case MINUS_EXPR:
4544 case MULT_EXPR:
4545 case TRUNC_DIV_EXPR:
4546 case TRUNC_MOD_EXPR:
4547 case BIT_XOR_EXPR:
4548 case BIT_AND_EXPR:
4549 case BIT_IOR_EXPR:
4550 case LSHIFT_EXPR:
4551 case RSHIFT_EXPR:
4553 case MODOP_EXPR:
4555 case EQ_EXPR:
4556 case NE_EXPR:
4557 case LE_EXPR:
4558 case GE_EXPR:
4559 case LT_EXPR:
4560 case GT_EXPR:
4562 case TRUTH_ANDIF_EXPR:
4563 case TRUTH_ORIF_EXPR:
4565 case COMPOUND_EXPR:
4567 case DOTSTAR_EXPR:
4568 case MEMBER_REF:
4569 return true;
4571 default:
4572 return false;
4576 /* If the next token is a suitable fold operator, consume it and return as
4577 the function above. */
4579 static int
4580 cp_parser_fold_operator (cp_parser *parser)
4582 cp_token* token = cp_lexer_peek_token (parser->lexer);
4583 int code = cp_parser_fold_operator (token);
4584 if (code != ERROR_MARK)
4585 cp_lexer_consume_token (parser->lexer);
4586 return code;
4589 /* Parse a fold-expression.
4591 fold-expression:
4592 ( ... folding-operator cast-expression)
4593 ( cast-expression folding-operator ... )
4594 ( cast-expression folding operator ... folding-operator cast-expression)
4596 Note that the '(' and ')' are matched in primary expression. */
4598 static cp_expr
4599 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4601 cp_id_kind pidk;
4603 // Left fold.
4604 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4606 cp_lexer_consume_token (parser->lexer);
4607 int op = cp_parser_fold_operator (parser);
4608 if (op == ERROR_MARK)
4610 cp_parser_error (parser, "expected binary operator");
4611 return error_mark_node;
4614 tree expr = cp_parser_cast_expression (parser, false, false,
4615 false, &pidk);
4616 if (expr == error_mark_node)
4617 return error_mark_node;
4618 return finish_left_unary_fold_expr (expr, op);
4621 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4622 int op = cp_parser_fold_operator (parser);
4623 if (op == ERROR_MARK)
4625 cp_parser_error (parser, "expected binary operator");
4626 return error_mark_node;
4629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4631 cp_parser_error (parser, "expected ...");
4632 return error_mark_node;
4634 cp_lexer_consume_token (parser->lexer);
4636 /* The operands of a fold-expression are cast-expressions, so binary or
4637 conditional expressions are not allowed. We check this here to avoid
4638 tentative parsing. */
4639 if (is_binary_op (TREE_CODE (expr1)))
4640 error_at (location_of (expr1),
4641 "binary expression in operand of fold-expression");
4642 else if (TREE_CODE (expr1) == COND_EXPR)
4643 error_at (location_of (expr1),
4644 "conditional expression in operand of fold-expression");
4646 // Right fold.
4647 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4648 return finish_right_unary_fold_expr (expr1, op);
4650 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4652 cp_parser_error (parser, "mismatched operator in fold-expression");
4653 return error_mark_node;
4655 cp_lexer_consume_token (parser->lexer);
4657 // Binary left or right fold.
4658 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4659 if (expr2 == error_mark_node)
4660 return error_mark_node;
4661 return finish_binary_fold_expr (expr1, expr2, op);
4664 /* Parse a primary-expression.
4666 primary-expression:
4667 literal
4668 this
4669 ( expression )
4670 id-expression
4671 lambda-expression (C++11)
4673 GNU Extensions:
4675 primary-expression:
4676 ( compound-statement )
4677 __builtin_va_arg ( assignment-expression , type-id )
4678 __builtin_offsetof ( type-id , offsetof-expression )
4680 C++ Extensions:
4681 __has_nothrow_assign ( type-id )
4682 __has_nothrow_constructor ( type-id )
4683 __has_nothrow_copy ( type-id )
4684 __has_trivial_assign ( type-id )
4685 __has_trivial_constructor ( type-id )
4686 __has_trivial_copy ( type-id )
4687 __has_trivial_destructor ( type-id )
4688 __has_virtual_destructor ( type-id )
4689 __is_abstract ( type-id )
4690 __is_base_of ( type-id , type-id )
4691 __is_class ( type-id )
4692 __is_empty ( type-id )
4693 __is_enum ( type-id )
4694 __is_final ( type-id )
4695 __is_literal_type ( type-id )
4696 __is_pod ( type-id )
4697 __is_polymorphic ( type-id )
4698 __is_std_layout ( type-id )
4699 __is_trivial ( type-id )
4700 __is_union ( type-id )
4702 Objective-C++ Extension:
4704 primary-expression:
4705 objc-expression
4707 literal:
4708 __null
4710 ADDRESS_P is true iff this expression was immediately preceded by
4711 "&" and therefore might denote a pointer-to-member. CAST_P is true
4712 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4713 true iff this expression is a template argument.
4715 Returns a representation of the expression. Upon return, *IDK
4716 indicates what kind of id-expression (if any) was present. */
4718 static cp_expr
4719 cp_parser_primary_expression (cp_parser *parser,
4720 bool address_p,
4721 bool cast_p,
4722 bool template_arg_p,
4723 bool decltype_p,
4724 cp_id_kind *idk)
4726 cp_token *token = NULL;
4728 /* Assume the primary expression is not an id-expression. */
4729 *idk = CP_ID_KIND_NONE;
4731 /* Peek at the next token. */
4732 token = cp_lexer_peek_token (parser->lexer);
4733 switch ((int) token->type)
4735 /* literal:
4736 integer-literal
4737 character-literal
4738 floating-literal
4739 string-literal
4740 boolean-literal
4741 pointer-literal
4742 user-defined-literal */
4743 case CPP_CHAR:
4744 case CPP_CHAR16:
4745 case CPP_CHAR32:
4746 case CPP_WCHAR:
4747 case CPP_UTF8CHAR:
4748 case CPP_NUMBER:
4749 case CPP_PREPARSED_EXPR:
4750 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4751 return cp_parser_userdef_numeric_literal (parser);
4752 token = cp_lexer_consume_token (parser->lexer);
4753 if (TREE_CODE (token->u.value) == FIXED_CST)
4755 error_at (token->location,
4756 "fixed-point types not supported in C++");
4757 return error_mark_node;
4759 /* Floating-point literals are only allowed in an integral
4760 constant expression if they are cast to an integral or
4761 enumeration type. */
4762 if (TREE_CODE (token->u.value) == REAL_CST
4763 && parser->integral_constant_expression_p
4764 && pedantic)
4766 /* CAST_P will be set even in invalid code like "int(2.7 +
4767 ...)". Therefore, we have to check that the next token
4768 is sure to end the cast. */
4769 if (cast_p)
4771 cp_token *next_token;
4773 next_token = cp_lexer_peek_token (parser->lexer);
4774 if (/* The comma at the end of an
4775 enumerator-definition. */
4776 next_token->type != CPP_COMMA
4777 /* The curly brace at the end of an enum-specifier. */
4778 && next_token->type != CPP_CLOSE_BRACE
4779 /* The end of a statement. */
4780 && next_token->type != CPP_SEMICOLON
4781 /* The end of the cast-expression. */
4782 && next_token->type != CPP_CLOSE_PAREN
4783 /* The end of an array bound. */
4784 && next_token->type != CPP_CLOSE_SQUARE
4785 /* The closing ">" in a template-argument-list. */
4786 && (next_token->type != CPP_GREATER
4787 || parser->greater_than_is_operator_p)
4788 /* C++0x only: A ">>" treated like two ">" tokens,
4789 in a template-argument-list. */
4790 && (next_token->type != CPP_RSHIFT
4791 || (cxx_dialect == cxx98)
4792 || parser->greater_than_is_operator_p))
4793 cast_p = false;
4796 /* If we are within a cast, then the constraint that the
4797 cast is to an integral or enumeration type will be
4798 checked at that point. If we are not within a cast, then
4799 this code is invalid. */
4800 if (!cast_p)
4801 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4803 return cp_expr (token->u.value, token->location);
4805 case CPP_CHAR_USERDEF:
4806 case CPP_CHAR16_USERDEF:
4807 case CPP_CHAR32_USERDEF:
4808 case CPP_WCHAR_USERDEF:
4809 case CPP_UTF8CHAR_USERDEF:
4810 return cp_parser_userdef_char_literal (parser);
4812 case CPP_STRING:
4813 case CPP_STRING16:
4814 case CPP_STRING32:
4815 case CPP_WSTRING:
4816 case CPP_UTF8STRING:
4817 case CPP_STRING_USERDEF:
4818 case CPP_STRING16_USERDEF:
4819 case CPP_STRING32_USERDEF:
4820 case CPP_WSTRING_USERDEF:
4821 case CPP_UTF8STRING_USERDEF:
4822 /* ??? Should wide strings be allowed when parser->translate_strings_p
4823 is false (i.e. in attributes)? If not, we can kill the third
4824 argument to cp_parser_string_literal. */
4825 return cp_parser_string_literal (parser,
4826 parser->translate_strings_p,
4827 true);
4829 case CPP_OPEN_PAREN:
4830 /* If we see `( { ' then we are looking at the beginning of
4831 a GNU statement-expression. */
4832 if (cp_parser_allow_gnu_extensions_p (parser)
4833 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4835 /* Statement-expressions are not allowed by the standard. */
4836 pedwarn (token->location, OPT_Wpedantic,
4837 "ISO C++ forbids braced-groups within expressions");
4839 /* And they're not allowed outside of a function-body; you
4840 cannot, for example, write:
4842 int i = ({ int j = 3; j + 1; });
4844 at class or namespace scope. */
4845 if (!parser->in_function_body
4846 || parser->in_template_argument_list_p)
4848 error_at (token->location,
4849 "statement-expressions are not allowed outside "
4850 "functions nor in template-argument lists");
4851 cp_parser_skip_to_end_of_block_or_statement (parser);
4852 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4853 cp_lexer_consume_token (parser->lexer);
4854 return error_mark_node;
4856 else
4857 return cp_parser_statement_expr (parser);
4859 /* Otherwise it's a normal parenthesized expression. */
4861 cp_expr expr;
4862 bool saved_greater_than_is_operator_p;
4864 location_t open_paren_loc = token->location;
4866 /* Consume the `('. */
4867 cp_lexer_consume_token (parser->lexer);
4868 /* Within a parenthesized expression, a `>' token is always
4869 the greater-than operator. */
4870 saved_greater_than_is_operator_p
4871 = parser->greater_than_is_operator_p;
4872 parser->greater_than_is_operator_p = true;
4874 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4875 /* Left fold expression. */
4876 expr = NULL_TREE;
4877 else
4878 /* Parse the parenthesized expression. */
4879 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4881 token = cp_lexer_peek_token (parser->lexer);
4882 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4884 expr = cp_parser_fold_expression (parser, expr);
4885 if (expr != error_mark_node
4886 && cxx_dialect < cxx1z
4887 && !in_system_header_at (input_location))
4888 pedwarn (input_location, 0, "fold-expressions only available "
4889 "with -std=c++1z or -std=gnu++1z");
4891 else
4892 /* Let the front end know that this expression was
4893 enclosed in parentheses. This matters in case, for
4894 example, the expression is of the form `A::B', since
4895 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4896 not. */
4897 expr = finish_parenthesized_expr (expr);
4899 /* DR 705: Wrapping an unqualified name in parentheses
4900 suppresses arg-dependent lookup. We want to pass back
4901 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4902 (c++/37862), but none of the others. */
4903 if (*idk != CP_ID_KIND_QUALIFIED)
4904 *idk = CP_ID_KIND_NONE;
4906 /* The `>' token might be the end of a template-id or
4907 template-parameter-list now. */
4908 parser->greater_than_is_operator_p
4909 = saved_greater_than_is_operator_p;
4911 /* Consume the `)'. */
4912 token = cp_lexer_peek_token (parser->lexer);
4913 location_t close_paren_loc = token->location;
4914 expr.set_range (open_paren_loc, close_paren_loc);
4915 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4916 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4917 cp_parser_skip_to_end_of_statement (parser);
4919 return expr;
4922 case CPP_OPEN_SQUARE:
4924 if (c_dialect_objc ())
4926 /* We might have an Objective-C++ message. */
4927 cp_parser_parse_tentatively (parser);
4928 tree msg = cp_parser_objc_message_expression (parser);
4929 /* If that works out, we're done ... */
4930 if (cp_parser_parse_definitely (parser))
4931 return msg;
4932 /* ... else, fall though to see if it's a lambda. */
4934 cp_expr lam = cp_parser_lambda_expression (parser);
4935 /* Don't warn about a failed tentative parse. */
4936 if (cp_parser_error_occurred (parser))
4937 return error_mark_node;
4938 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4939 return lam;
4942 case CPP_OBJC_STRING:
4943 if (c_dialect_objc ())
4944 /* We have an Objective-C++ string literal. */
4945 return cp_parser_objc_expression (parser);
4946 cp_parser_error (parser, "expected primary-expression");
4947 return error_mark_node;
4949 case CPP_KEYWORD:
4950 switch (token->keyword)
4952 /* These two are the boolean literals. */
4953 case RID_TRUE:
4954 cp_lexer_consume_token (parser->lexer);
4955 return cp_expr (boolean_true_node, token->location);
4956 case RID_FALSE:
4957 cp_lexer_consume_token (parser->lexer);
4958 return cp_expr (boolean_false_node, token->location);
4960 /* The `__null' literal. */
4961 case RID_NULL:
4962 cp_lexer_consume_token (parser->lexer);
4963 return cp_expr (null_node, token->location);
4965 /* The `nullptr' literal. */
4966 case RID_NULLPTR:
4967 cp_lexer_consume_token (parser->lexer);
4968 return cp_expr (nullptr_node, token->location);
4970 /* Recognize the `this' keyword. */
4971 case RID_THIS:
4972 cp_lexer_consume_token (parser->lexer);
4973 if (parser->local_variables_forbidden_p)
4975 error_at (token->location,
4976 "%<this%> may not be used in this context");
4977 return error_mark_node;
4979 /* Pointers cannot appear in constant-expressions. */
4980 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4981 return error_mark_node;
4982 return cp_expr (finish_this_expr (), token->location);
4984 /* The `operator' keyword can be the beginning of an
4985 id-expression. */
4986 case RID_OPERATOR:
4987 goto id_expression;
4989 case RID_FUNCTION_NAME:
4990 case RID_PRETTY_FUNCTION_NAME:
4991 case RID_C99_FUNCTION_NAME:
4993 non_integral_constant name;
4995 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4996 __func__ are the names of variables -- but they are
4997 treated specially. Therefore, they are handled here,
4998 rather than relying on the generic id-expression logic
4999 below. Grammatically, these names are id-expressions.
5001 Consume the token. */
5002 token = cp_lexer_consume_token (parser->lexer);
5004 switch (token->keyword)
5006 case RID_FUNCTION_NAME:
5007 name = NIC_FUNC_NAME;
5008 break;
5009 case RID_PRETTY_FUNCTION_NAME:
5010 name = NIC_PRETTY_FUNC;
5011 break;
5012 case RID_C99_FUNCTION_NAME:
5013 name = NIC_C99_FUNC;
5014 break;
5015 default:
5016 gcc_unreachable ();
5019 if (cp_parser_non_integral_constant_expression (parser, name))
5020 return error_mark_node;
5022 /* Look up the name. */
5023 return finish_fname (token->u.value);
5026 case RID_VA_ARG:
5028 tree expression;
5029 tree type;
5030 source_location type_location;
5031 location_t start_loc
5032 = cp_lexer_peek_token (parser->lexer)->location;
5033 /* The `__builtin_va_arg' construct is used to handle
5034 `va_arg'. Consume the `__builtin_va_arg' token. */
5035 cp_lexer_consume_token (parser->lexer);
5036 /* Look for the opening `('. */
5037 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5038 /* Now, parse the assignment-expression. */
5039 expression = cp_parser_assignment_expression (parser);
5040 /* Look for the `,'. */
5041 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5042 type_location = cp_lexer_peek_token (parser->lexer)->location;
5043 /* Parse the type-id. */
5045 type_id_in_expr_sentinel s (parser);
5046 type = cp_parser_type_id (parser);
5048 /* Look for the closing `)'. */
5049 location_t finish_loc
5050 = cp_lexer_peek_token (parser->lexer)->location;
5051 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5052 /* Using `va_arg' in a constant-expression is not
5053 allowed. */
5054 if (cp_parser_non_integral_constant_expression (parser,
5055 NIC_VA_ARG))
5056 return error_mark_node;
5057 /* Construct a location of the form:
5058 __builtin_va_arg (v, int)
5059 ~~~~~~~~~~~~~~~~~~~~~^~~~
5060 with the caret at the type, ranging from the start of the
5061 "__builtin_va_arg" token to the close paren. */
5062 location_t combined_loc
5063 = make_location (type_location, start_loc, finish_loc);
5064 return build_x_va_arg (combined_loc, expression, type);
5067 case RID_OFFSETOF:
5068 return cp_parser_builtin_offsetof (parser);
5070 case RID_HAS_NOTHROW_ASSIGN:
5071 case RID_HAS_NOTHROW_CONSTRUCTOR:
5072 case RID_HAS_NOTHROW_COPY:
5073 case RID_HAS_TRIVIAL_ASSIGN:
5074 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5075 case RID_HAS_TRIVIAL_COPY:
5076 case RID_HAS_TRIVIAL_DESTRUCTOR:
5077 case RID_HAS_VIRTUAL_DESTRUCTOR:
5078 case RID_IS_ABSTRACT:
5079 case RID_IS_BASE_OF:
5080 case RID_IS_CLASS:
5081 case RID_IS_EMPTY:
5082 case RID_IS_ENUM:
5083 case RID_IS_FINAL:
5084 case RID_IS_LITERAL_TYPE:
5085 case RID_IS_POD:
5086 case RID_IS_POLYMORPHIC:
5087 case RID_IS_SAME_AS:
5088 case RID_IS_STD_LAYOUT:
5089 case RID_IS_TRIVIAL:
5090 case RID_IS_TRIVIALLY_ASSIGNABLE:
5091 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5092 case RID_IS_TRIVIALLY_COPYABLE:
5093 case RID_IS_UNION:
5094 return cp_parser_trait_expr (parser, token->keyword);
5096 // C++ concepts
5097 case RID_REQUIRES:
5098 return cp_parser_requires_expression (parser);
5100 /* Objective-C++ expressions. */
5101 case RID_AT_ENCODE:
5102 case RID_AT_PROTOCOL:
5103 case RID_AT_SELECTOR:
5104 return cp_parser_objc_expression (parser);
5106 case RID_TEMPLATE:
5107 if (parser->in_function_body
5108 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5109 == CPP_LESS))
5111 error_at (token->location,
5112 "a template declaration cannot appear at block scope");
5113 cp_parser_skip_to_end_of_block_or_statement (parser);
5114 return error_mark_node;
5116 default:
5117 cp_parser_error (parser, "expected primary-expression");
5118 return error_mark_node;
5121 /* An id-expression can start with either an identifier, a
5122 `::' as the beginning of a qualified-id, or the "operator"
5123 keyword. */
5124 case CPP_NAME:
5125 case CPP_SCOPE:
5126 case CPP_TEMPLATE_ID:
5127 case CPP_NESTED_NAME_SPECIFIER:
5129 id_expression:
5130 cp_expr id_expression;
5131 cp_expr decl;
5132 const char *error_msg;
5133 bool template_p;
5134 bool done;
5135 cp_token *id_expr_token;
5137 /* Parse the id-expression. */
5138 id_expression
5139 = cp_parser_id_expression (parser,
5140 /*template_keyword_p=*/false,
5141 /*check_dependency_p=*/true,
5142 &template_p,
5143 /*declarator_p=*/false,
5144 /*optional_p=*/false);
5145 if (id_expression == error_mark_node)
5146 return error_mark_node;
5147 id_expr_token = token;
5148 token = cp_lexer_peek_token (parser->lexer);
5149 done = (token->type != CPP_OPEN_SQUARE
5150 && token->type != CPP_OPEN_PAREN
5151 && token->type != CPP_DOT
5152 && token->type != CPP_DEREF
5153 && token->type != CPP_PLUS_PLUS
5154 && token->type != CPP_MINUS_MINUS);
5155 /* If we have a template-id, then no further lookup is
5156 required. If the template-id was for a template-class, we
5157 will sometimes have a TYPE_DECL at this point. */
5158 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5159 || TREE_CODE (id_expression) == TYPE_DECL)
5160 decl = id_expression;
5161 /* Look up the name. */
5162 else
5164 tree ambiguous_decls;
5166 /* If we already know that this lookup is ambiguous, then
5167 we've already issued an error message; there's no reason
5168 to check again. */
5169 if (id_expr_token->type == CPP_NAME
5170 && id_expr_token->error_reported)
5172 cp_parser_simulate_error (parser);
5173 return error_mark_node;
5176 decl = cp_parser_lookup_name (parser, id_expression,
5177 none_type,
5178 template_p,
5179 /*is_namespace=*/false,
5180 /*check_dependency=*/true,
5181 &ambiguous_decls,
5182 id_expr_token->location);
5183 /* If the lookup was ambiguous, an error will already have
5184 been issued. */
5185 if (ambiguous_decls)
5186 return error_mark_node;
5188 /* In Objective-C++, we may have an Objective-C 2.0
5189 dot-syntax for classes here. */
5190 if (c_dialect_objc ()
5191 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5192 && TREE_CODE (decl) == TYPE_DECL
5193 && objc_is_class_name (decl))
5195 tree component;
5196 cp_lexer_consume_token (parser->lexer);
5197 component = cp_parser_identifier (parser);
5198 if (component == error_mark_node)
5199 return error_mark_node;
5201 tree result = objc_build_class_component_ref (id_expression,
5202 component);
5203 /* Build a location of the form:
5204 expr.component
5205 ~~~~~^~~~~~~~~
5206 with caret at the start of the component name (at
5207 input_location), ranging from the start of the id_expression
5208 to the end of the component name. */
5209 location_t combined_loc
5210 = make_location (input_location, id_expression.get_start (),
5211 get_finish (input_location));
5212 protected_set_expr_location (result, combined_loc);
5213 return result;
5216 /* In Objective-C++, an instance variable (ivar) may be preferred
5217 to whatever cp_parser_lookup_name() found.
5218 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5219 rest of c-family, we have to do a little extra work to preserve
5220 any location information in cp_expr "decl". Given that
5221 objc_lookup_ivar is implemented in "c-family" and "objc", we
5222 have a trip through the pure "tree" type, rather than cp_expr.
5223 Naively copying it back to "decl" would implicitly give the
5224 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5225 store an EXPR_LOCATION. Hence we only update "decl" (and
5226 hence its location_t) if we get back a different tree node. */
5227 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5228 id_expression);
5229 if (decl_tree != decl.get_value ())
5230 decl = cp_expr (decl_tree);
5232 /* If name lookup gives us a SCOPE_REF, then the
5233 qualifying scope was dependent. */
5234 if (TREE_CODE (decl) == SCOPE_REF)
5236 /* At this point, we do not know if DECL is a valid
5237 integral constant expression. We assume that it is
5238 in fact such an expression, so that code like:
5240 template <int N> struct A {
5241 int a[B<N>::i];
5244 is accepted. At template-instantiation time, we
5245 will check that B<N>::i is actually a constant. */
5246 return decl;
5248 /* Check to see if DECL is a local variable in a context
5249 where that is forbidden. */
5250 if (parser->local_variables_forbidden_p
5251 && local_variable_p (decl))
5253 /* It might be that we only found DECL because we are
5254 trying to be generous with pre-ISO scoping rules.
5255 For example, consider:
5257 int i;
5258 void g() {
5259 for (int i = 0; i < 10; ++i) {}
5260 extern void f(int j = i);
5263 Here, name look up will originally find the out
5264 of scope `i'. We need to issue a warning message,
5265 but then use the global `i'. */
5266 decl = check_for_out_of_scope_variable (decl);
5267 if (local_variable_p (decl))
5269 error_at (id_expr_token->location,
5270 "local variable %qD may not appear in this context",
5271 decl.get_value ());
5272 return error_mark_node;
5277 decl = (finish_id_expression
5278 (id_expression, decl, parser->scope,
5279 idk,
5280 parser->integral_constant_expression_p,
5281 parser->allow_non_integral_constant_expression_p,
5282 &parser->non_integral_constant_expression_p,
5283 template_p, done, address_p,
5284 template_arg_p,
5285 &error_msg,
5286 id_expr_token->location));
5287 if (error_msg)
5288 cp_parser_error (parser, error_msg);
5289 decl.set_location (id_expr_token->location);
5290 return decl;
5293 /* Anything else is an error. */
5294 default:
5295 cp_parser_error (parser, "expected primary-expression");
5296 return error_mark_node;
5300 static inline cp_expr
5301 cp_parser_primary_expression (cp_parser *parser,
5302 bool address_p,
5303 bool cast_p,
5304 bool template_arg_p,
5305 cp_id_kind *idk)
5307 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5308 /*decltype*/false, idk);
5311 /* Parse an id-expression.
5313 id-expression:
5314 unqualified-id
5315 qualified-id
5317 qualified-id:
5318 :: [opt] nested-name-specifier template [opt] unqualified-id
5319 :: identifier
5320 :: operator-function-id
5321 :: template-id
5323 Return a representation of the unqualified portion of the
5324 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5325 a `::' or nested-name-specifier.
5327 Often, if the id-expression was a qualified-id, the caller will
5328 want to make a SCOPE_REF to represent the qualified-id. This
5329 function does not do this in order to avoid wastefully creating
5330 SCOPE_REFs when they are not required.
5332 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5333 `template' keyword.
5335 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5336 uninstantiated templates.
5338 If *TEMPLATE_P is non-NULL, it is set to true iff the
5339 `template' keyword is used to explicitly indicate that the entity
5340 named is a template.
5342 If DECLARATOR_P is true, the id-expression is appearing as part of
5343 a declarator, rather than as part of an expression. */
5345 static cp_expr
5346 cp_parser_id_expression (cp_parser *parser,
5347 bool template_keyword_p,
5348 bool check_dependency_p,
5349 bool *template_p,
5350 bool declarator_p,
5351 bool optional_p)
5353 bool global_scope_p;
5354 bool nested_name_specifier_p;
5356 /* Assume the `template' keyword was not used. */
5357 if (template_p)
5358 *template_p = template_keyword_p;
5360 /* Look for the optional `::' operator. */
5361 global_scope_p
5362 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5363 != NULL_TREE);
5364 /* Look for the optional nested-name-specifier. */
5365 nested_name_specifier_p
5366 = (cp_parser_nested_name_specifier_opt (parser,
5367 /*typename_keyword_p=*/false,
5368 check_dependency_p,
5369 /*type_p=*/false,
5370 declarator_p)
5371 != NULL_TREE);
5372 /* If there is a nested-name-specifier, then we are looking at
5373 the first qualified-id production. */
5374 if (nested_name_specifier_p)
5376 tree saved_scope;
5377 tree saved_object_scope;
5378 tree saved_qualifying_scope;
5379 tree unqualified_id;
5380 bool is_template;
5382 /* See if the next token is the `template' keyword. */
5383 if (!template_p)
5384 template_p = &is_template;
5385 *template_p = cp_parser_optional_template_keyword (parser);
5386 /* Name lookup we do during the processing of the
5387 unqualified-id might obliterate SCOPE. */
5388 saved_scope = parser->scope;
5389 saved_object_scope = parser->object_scope;
5390 saved_qualifying_scope = parser->qualifying_scope;
5391 /* Process the final unqualified-id. */
5392 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5393 check_dependency_p,
5394 declarator_p,
5395 /*optional_p=*/false);
5396 /* Restore the SAVED_SCOPE for our caller. */
5397 parser->scope = saved_scope;
5398 parser->object_scope = saved_object_scope;
5399 parser->qualifying_scope = saved_qualifying_scope;
5401 return unqualified_id;
5403 /* Otherwise, if we are in global scope, then we are looking at one
5404 of the other qualified-id productions. */
5405 else if (global_scope_p)
5407 cp_token *token;
5408 tree id;
5410 /* Peek at the next token. */
5411 token = cp_lexer_peek_token (parser->lexer);
5413 /* If it's an identifier, and the next token is not a "<", then
5414 we can avoid the template-id case. This is an optimization
5415 for this common case. */
5416 if (token->type == CPP_NAME
5417 && !cp_parser_nth_token_starts_template_argument_list_p
5418 (parser, 2))
5419 return cp_parser_identifier (parser);
5421 cp_parser_parse_tentatively (parser);
5422 /* Try a template-id. */
5423 id = cp_parser_template_id (parser,
5424 /*template_keyword_p=*/false,
5425 /*check_dependency_p=*/true,
5426 none_type,
5427 declarator_p);
5428 /* If that worked, we're done. */
5429 if (cp_parser_parse_definitely (parser))
5430 return id;
5432 /* Peek at the next token. (Changes in the token buffer may
5433 have invalidated the pointer obtained above.) */
5434 token = cp_lexer_peek_token (parser->lexer);
5436 switch (token->type)
5438 case CPP_NAME:
5439 return cp_parser_identifier (parser);
5441 case CPP_KEYWORD:
5442 if (token->keyword == RID_OPERATOR)
5443 return cp_parser_operator_function_id (parser);
5444 /* Fall through. */
5446 default:
5447 cp_parser_error (parser, "expected id-expression");
5448 return error_mark_node;
5451 else
5452 return cp_parser_unqualified_id (parser, template_keyword_p,
5453 /*check_dependency_p=*/true,
5454 declarator_p,
5455 optional_p);
5458 /* Parse an unqualified-id.
5460 unqualified-id:
5461 identifier
5462 operator-function-id
5463 conversion-function-id
5464 ~ class-name
5465 template-id
5467 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5468 keyword, in a construct like `A::template ...'.
5470 Returns a representation of unqualified-id. For the `identifier'
5471 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5472 production a BIT_NOT_EXPR is returned; the operand of the
5473 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5474 other productions, see the documentation accompanying the
5475 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5476 names are looked up in uninstantiated templates. If DECLARATOR_P
5477 is true, the unqualified-id is appearing as part of a declarator,
5478 rather than as part of an expression. */
5480 static cp_expr
5481 cp_parser_unqualified_id (cp_parser* parser,
5482 bool template_keyword_p,
5483 bool check_dependency_p,
5484 bool declarator_p,
5485 bool optional_p)
5487 cp_token *token;
5489 /* Peek at the next token. */
5490 token = cp_lexer_peek_token (parser->lexer);
5492 switch ((int) token->type)
5494 case CPP_NAME:
5496 tree id;
5498 /* We don't know yet whether or not this will be a
5499 template-id. */
5500 cp_parser_parse_tentatively (parser);
5501 /* Try a template-id. */
5502 id = cp_parser_template_id (parser, template_keyword_p,
5503 check_dependency_p,
5504 none_type,
5505 declarator_p);
5506 /* If it worked, we're done. */
5507 if (cp_parser_parse_definitely (parser))
5508 return id;
5509 /* Otherwise, it's an ordinary identifier. */
5510 return cp_parser_identifier (parser);
5513 case CPP_TEMPLATE_ID:
5514 return cp_parser_template_id (parser, template_keyword_p,
5515 check_dependency_p,
5516 none_type,
5517 declarator_p);
5519 case CPP_COMPL:
5521 tree type_decl;
5522 tree qualifying_scope;
5523 tree object_scope;
5524 tree scope;
5525 bool done;
5527 /* Consume the `~' token. */
5528 cp_lexer_consume_token (parser->lexer);
5529 /* Parse the class-name. The standard, as written, seems to
5530 say that:
5532 template <typename T> struct S { ~S (); };
5533 template <typename T> S<T>::~S() {}
5535 is invalid, since `~' must be followed by a class-name, but
5536 `S<T>' is dependent, and so not known to be a class.
5537 That's not right; we need to look in uninstantiated
5538 templates. A further complication arises from:
5540 template <typename T> void f(T t) {
5541 t.T::~T();
5544 Here, it is not possible to look up `T' in the scope of `T'
5545 itself. We must look in both the current scope, and the
5546 scope of the containing complete expression.
5548 Yet another issue is:
5550 struct S {
5551 int S;
5552 ~S();
5555 S::~S() {}
5557 The standard does not seem to say that the `S' in `~S'
5558 should refer to the type `S' and not the data member
5559 `S::S'. */
5561 /* DR 244 says that we look up the name after the "~" in the
5562 same scope as we looked up the qualifying name. That idea
5563 isn't fully worked out; it's more complicated than that. */
5564 scope = parser->scope;
5565 object_scope = parser->object_scope;
5566 qualifying_scope = parser->qualifying_scope;
5568 /* Check for invalid scopes. */
5569 if (scope == error_mark_node)
5571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5572 cp_lexer_consume_token (parser->lexer);
5573 return error_mark_node;
5575 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5577 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5578 error_at (token->location,
5579 "scope %qT before %<~%> is not a class-name",
5580 scope);
5581 cp_parser_simulate_error (parser);
5582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5583 cp_lexer_consume_token (parser->lexer);
5584 return error_mark_node;
5586 gcc_assert (!scope || TYPE_P (scope));
5588 /* If the name is of the form "X::~X" it's OK even if X is a
5589 typedef. */
5590 token = cp_lexer_peek_token (parser->lexer);
5591 if (scope
5592 && token->type == CPP_NAME
5593 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5594 != CPP_LESS)
5595 && (token->u.value == TYPE_IDENTIFIER (scope)
5596 || (CLASS_TYPE_P (scope)
5597 && constructor_name_p (token->u.value, scope))))
5599 cp_lexer_consume_token (parser->lexer);
5600 return build_nt (BIT_NOT_EXPR, scope);
5603 /* ~auto means the destructor of whatever the object is. */
5604 if (cp_parser_is_keyword (token, RID_AUTO))
5606 if (cxx_dialect < cxx14)
5607 pedwarn (input_location, 0,
5608 "%<~auto%> only available with "
5609 "-std=c++14 or -std=gnu++14");
5610 cp_lexer_consume_token (parser->lexer);
5611 return build_nt (BIT_NOT_EXPR, make_auto ());
5614 /* If there was an explicit qualification (S::~T), first look
5615 in the scope given by the qualification (i.e., S).
5617 Note: in the calls to cp_parser_class_name below we pass
5618 typename_type so that lookup finds the injected-class-name
5619 rather than the constructor. */
5620 done = false;
5621 type_decl = NULL_TREE;
5622 if (scope)
5624 cp_parser_parse_tentatively (parser);
5625 type_decl = cp_parser_class_name (parser,
5626 /*typename_keyword_p=*/false,
5627 /*template_keyword_p=*/false,
5628 typename_type,
5629 /*check_dependency=*/false,
5630 /*class_head_p=*/false,
5631 declarator_p);
5632 if (cp_parser_parse_definitely (parser))
5633 done = true;
5635 /* In "N::S::~S", look in "N" as well. */
5636 if (!done && scope && qualifying_scope)
5638 cp_parser_parse_tentatively (parser);
5639 parser->scope = qualifying_scope;
5640 parser->object_scope = NULL_TREE;
5641 parser->qualifying_scope = NULL_TREE;
5642 type_decl
5643 = cp_parser_class_name (parser,
5644 /*typename_keyword_p=*/false,
5645 /*template_keyword_p=*/false,
5646 typename_type,
5647 /*check_dependency=*/false,
5648 /*class_head_p=*/false,
5649 declarator_p);
5650 if (cp_parser_parse_definitely (parser))
5651 done = true;
5653 /* In "p->S::~T", look in the scope given by "*p" as well. */
5654 else if (!done && object_scope)
5656 cp_parser_parse_tentatively (parser);
5657 parser->scope = object_scope;
5658 parser->object_scope = NULL_TREE;
5659 parser->qualifying_scope = NULL_TREE;
5660 type_decl
5661 = cp_parser_class_name (parser,
5662 /*typename_keyword_p=*/false,
5663 /*template_keyword_p=*/false,
5664 typename_type,
5665 /*check_dependency=*/false,
5666 /*class_head_p=*/false,
5667 declarator_p);
5668 if (cp_parser_parse_definitely (parser))
5669 done = true;
5671 /* Look in the surrounding context. */
5672 if (!done)
5674 parser->scope = NULL_TREE;
5675 parser->object_scope = NULL_TREE;
5676 parser->qualifying_scope = NULL_TREE;
5677 if (processing_template_decl)
5678 cp_parser_parse_tentatively (parser);
5679 type_decl
5680 = cp_parser_class_name (parser,
5681 /*typename_keyword_p=*/false,
5682 /*template_keyword_p=*/false,
5683 typename_type,
5684 /*check_dependency=*/false,
5685 /*class_head_p=*/false,
5686 declarator_p);
5687 if (processing_template_decl
5688 && ! cp_parser_parse_definitely (parser))
5690 /* We couldn't find a type with this name. If we're parsing
5691 tentatively, fail and try something else. */
5692 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5694 cp_parser_simulate_error (parser);
5695 return error_mark_node;
5697 /* Otherwise, accept it and check for a match at instantiation
5698 time. */
5699 type_decl = cp_parser_identifier (parser);
5700 if (type_decl != error_mark_node)
5701 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5702 return type_decl;
5705 /* If an error occurred, assume that the name of the
5706 destructor is the same as the name of the qualifying
5707 class. That allows us to keep parsing after running
5708 into ill-formed destructor names. */
5709 if (type_decl == error_mark_node && scope)
5710 return build_nt (BIT_NOT_EXPR, scope);
5711 else if (type_decl == error_mark_node)
5712 return error_mark_node;
5714 /* Check that destructor name and scope match. */
5715 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5717 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5718 error_at (token->location,
5719 "declaration of %<~%T%> as member of %qT",
5720 type_decl, scope);
5721 cp_parser_simulate_error (parser);
5722 return error_mark_node;
5725 /* [class.dtor]
5727 A typedef-name that names a class shall not be used as the
5728 identifier in the declarator for a destructor declaration. */
5729 if (declarator_p
5730 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5731 && !DECL_SELF_REFERENCE_P (type_decl)
5732 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5733 error_at (token->location,
5734 "typedef-name %qD used as destructor declarator",
5735 type_decl);
5737 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5740 case CPP_KEYWORD:
5741 if (token->keyword == RID_OPERATOR)
5743 cp_expr id;
5745 /* This could be a template-id, so we try that first. */
5746 cp_parser_parse_tentatively (parser);
5747 /* Try a template-id. */
5748 id = cp_parser_template_id (parser, template_keyword_p,
5749 /*check_dependency_p=*/true,
5750 none_type,
5751 declarator_p);
5752 /* If that worked, we're done. */
5753 if (cp_parser_parse_definitely (parser))
5754 return id;
5755 /* We still don't know whether we're looking at an
5756 operator-function-id or a conversion-function-id. */
5757 cp_parser_parse_tentatively (parser);
5758 /* Try an operator-function-id. */
5759 id = cp_parser_operator_function_id (parser);
5760 /* If that didn't work, try a conversion-function-id. */
5761 if (!cp_parser_parse_definitely (parser))
5762 id = cp_parser_conversion_function_id (parser);
5763 else if (UDLIT_OPER_P (id))
5765 /* 17.6.3.3.5 */
5766 const char *name = UDLIT_OP_SUFFIX (id);
5767 if (name[0] != '_' && !in_system_header_at (input_location)
5768 && declarator_p)
5769 warning (0, "literal operator suffixes not preceded by %<_%>"
5770 " are reserved for future standardization");
5773 return id;
5775 /* Fall through. */
5777 default:
5778 if (optional_p)
5779 return NULL_TREE;
5780 cp_parser_error (parser, "expected unqualified-id");
5781 return error_mark_node;
5785 /* Parse an (optional) nested-name-specifier.
5787 nested-name-specifier: [C++98]
5788 class-or-namespace-name :: nested-name-specifier [opt]
5789 class-or-namespace-name :: template nested-name-specifier [opt]
5791 nested-name-specifier: [C++0x]
5792 type-name ::
5793 namespace-name ::
5794 nested-name-specifier identifier ::
5795 nested-name-specifier template [opt] simple-template-id ::
5797 PARSER->SCOPE should be set appropriately before this function is
5798 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5799 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5800 in name lookups.
5802 Sets PARSER->SCOPE to the class (TYPE) or namespace
5803 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5804 it unchanged if there is no nested-name-specifier. Returns the new
5805 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5807 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5808 part of a declaration and/or decl-specifier. */
5810 static tree
5811 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5812 bool typename_keyword_p,
5813 bool check_dependency_p,
5814 bool type_p,
5815 bool is_declaration)
5817 bool success = false;
5818 cp_token_position start = 0;
5819 cp_token *token;
5821 /* Remember where the nested-name-specifier starts. */
5822 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5824 start = cp_lexer_token_position (parser->lexer, false);
5825 push_deferring_access_checks (dk_deferred);
5828 while (true)
5830 tree new_scope;
5831 tree old_scope;
5832 tree saved_qualifying_scope;
5833 bool template_keyword_p;
5835 /* Spot cases that cannot be the beginning of a
5836 nested-name-specifier. */
5837 token = cp_lexer_peek_token (parser->lexer);
5839 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5840 the already parsed nested-name-specifier. */
5841 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5843 /* Grab the nested-name-specifier and continue the loop. */
5844 cp_parser_pre_parsed_nested_name_specifier (parser);
5845 /* If we originally encountered this nested-name-specifier
5846 with IS_DECLARATION set to false, we will not have
5847 resolved TYPENAME_TYPEs, so we must do so here. */
5848 if (is_declaration
5849 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5851 new_scope = resolve_typename_type (parser->scope,
5852 /*only_current_p=*/false);
5853 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5854 parser->scope = new_scope;
5856 success = true;
5857 continue;
5860 /* Spot cases that cannot be the beginning of a
5861 nested-name-specifier. On the second and subsequent times
5862 through the loop, we look for the `template' keyword. */
5863 if (success && token->keyword == RID_TEMPLATE)
5865 /* A template-id can start a nested-name-specifier. */
5866 else if (token->type == CPP_TEMPLATE_ID)
5868 /* DR 743: decltype can be used in a nested-name-specifier. */
5869 else if (token_is_decltype (token))
5871 else
5873 /* If the next token is not an identifier, then it is
5874 definitely not a type-name or namespace-name. */
5875 if (token->type != CPP_NAME)
5876 break;
5877 /* If the following token is neither a `<' (to begin a
5878 template-id), nor a `::', then we are not looking at a
5879 nested-name-specifier. */
5880 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5882 if (token->type == CPP_COLON
5883 && parser->colon_corrects_to_scope_p
5884 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5886 error_at (token->location,
5887 "found %<:%> in nested-name-specifier, expected %<::%>");
5888 token->type = CPP_SCOPE;
5891 if (token->type != CPP_SCOPE
5892 && !cp_parser_nth_token_starts_template_argument_list_p
5893 (parser, 2))
5894 break;
5897 /* The nested-name-specifier is optional, so we parse
5898 tentatively. */
5899 cp_parser_parse_tentatively (parser);
5901 /* Look for the optional `template' keyword, if this isn't the
5902 first time through the loop. */
5903 if (success)
5904 template_keyword_p = cp_parser_optional_template_keyword (parser);
5905 else
5906 template_keyword_p = false;
5908 /* Save the old scope since the name lookup we are about to do
5909 might destroy it. */
5910 old_scope = parser->scope;
5911 saved_qualifying_scope = parser->qualifying_scope;
5912 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5913 look up names in "X<T>::I" in order to determine that "Y" is
5914 a template. So, if we have a typename at this point, we make
5915 an effort to look through it. */
5916 if (is_declaration
5917 && !typename_keyword_p
5918 && parser->scope
5919 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5920 parser->scope = resolve_typename_type (parser->scope,
5921 /*only_current_p=*/false);
5922 /* Parse the qualifying entity. */
5923 new_scope
5924 = cp_parser_qualifying_entity (parser,
5925 typename_keyword_p,
5926 template_keyword_p,
5927 check_dependency_p,
5928 type_p,
5929 is_declaration);
5930 /* Look for the `::' token. */
5931 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5933 /* If we found what we wanted, we keep going; otherwise, we're
5934 done. */
5935 if (!cp_parser_parse_definitely (parser))
5937 bool error_p = false;
5939 /* Restore the OLD_SCOPE since it was valid before the
5940 failed attempt at finding the last
5941 class-or-namespace-name. */
5942 parser->scope = old_scope;
5943 parser->qualifying_scope = saved_qualifying_scope;
5945 /* If the next token is a decltype, and the one after that is a
5946 `::', then the decltype has failed to resolve to a class or
5947 enumeration type. Give this error even when parsing
5948 tentatively since it can't possibly be valid--and we're going
5949 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5950 won't get another chance.*/
5951 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5952 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5953 == CPP_SCOPE))
5955 token = cp_lexer_consume_token (parser->lexer);
5956 error_at (token->location, "decltype evaluates to %qT, "
5957 "which is not a class or enumeration type",
5958 token->u.tree_check_value->value);
5959 parser->scope = error_mark_node;
5960 error_p = true;
5961 /* As below. */
5962 success = true;
5963 cp_lexer_consume_token (parser->lexer);
5966 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
5967 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
5969 /* If we have a non-type template-id followed by ::, it can't
5970 possibly be valid. */
5971 token = cp_lexer_peek_token (parser->lexer);
5972 tree tid = token->u.tree_check_value->value;
5973 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
5974 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
5976 tree tmpl = NULL_TREE;
5977 if (is_overloaded_fn (tid))
5979 tree fns = get_fns (tid);
5980 if (!OVL_CHAIN (fns))
5981 tmpl = OVL_CURRENT (fns);
5982 error_at (token->location, "function template-id %qD "
5983 "in nested-name-specifier", tid);
5985 else
5987 /* Variable template. */
5988 tmpl = TREE_OPERAND (tid, 0);
5989 gcc_assert (variable_template_p (tmpl));
5990 error_at (token->location, "variable template-id %qD "
5991 "in nested-name-specifier", tid);
5993 if (tmpl)
5994 inform (DECL_SOURCE_LOCATION (tmpl),
5995 "%qD declared here", tmpl);
5997 parser->scope = error_mark_node;
5998 error_p = true;
5999 /* As below. */
6000 success = true;
6001 cp_lexer_consume_token (parser->lexer);
6002 cp_lexer_consume_token (parser->lexer);
6006 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6007 break;
6008 /* If the next token is an identifier, and the one after
6009 that is a `::', then any valid interpretation would have
6010 found a class-or-namespace-name. */
6011 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6012 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6013 == CPP_SCOPE)
6014 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6015 != CPP_COMPL))
6017 token = cp_lexer_consume_token (parser->lexer);
6018 if (!error_p)
6020 if (!token->error_reported)
6022 tree decl;
6023 tree ambiguous_decls;
6025 decl = cp_parser_lookup_name (parser, token->u.value,
6026 none_type,
6027 /*is_template=*/false,
6028 /*is_namespace=*/false,
6029 /*check_dependency=*/true,
6030 &ambiguous_decls,
6031 token->location);
6032 if (TREE_CODE (decl) == TEMPLATE_DECL)
6033 error_at (token->location,
6034 "%qD used without template parameters",
6035 decl);
6036 else if (ambiguous_decls)
6038 // cp_parser_lookup_name has the same diagnostic,
6039 // thus make sure to emit it at most once.
6040 if (cp_parser_uncommitted_to_tentative_parse_p
6041 (parser))
6043 error_at (token->location,
6044 "reference to %qD is ambiguous",
6045 token->u.value);
6046 print_candidates (ambiguous_decls);
6048 decl = error_mark_node;
6050 else
6052 if (cxx_dialect != cxx98)
6053 cp_parser_name_lookup_error
6054 (parser, token->u.value, decl, NLE_NOT_CXX98,
6055 token->location);
6056 else
6057 cp_parser_name_lookup_error
6058 (parser, token->u.value, decl, NLE_CXX98,
6059 token->location);
6062 parser->scope = error_mark_node;
6063 error_p = true;
6064 /* Treat this as a successful nested-name-specifier
6065 due to:
6067 [basic.lookup.qual]
6069 If the name found is not a class-name (clause
6070 _class_) or namespace-name (_namespace.def_), the
6071 program is ill-formed. */
6072 success = true;
6074 cp_lexer_consume_token (parser->lexer);
6076 break;
6078 /* We've found one valid nested-name-specifier. */
6079 success = true;
6080 /* Name lookup always gives us a DECL. */
6081 if (TREE_CODE (new_scope) == TYPE_DECL)
6082 new_scope = TREE_TYPE (new_scope);
6083 /* Uses of "template" must be followed by actual templates. */
6084 if (template_keyword_p
6085 && !(CLASS_TYPE_P (new_scope)
6086 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6087 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6088 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6089 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6090 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6091 == TEMPLATE_ID_EXPR)))
6092 permerror (input_location, TYPE_P (new_scope)
6093 ? G_("%qT is not a template")
6094 : G_("%qD is not a template"),
6095 new_scope);
6096 /* If it is a class scope, try to complete it; we are about to
6097 be looking up names inside the class. */
6098 if (TYPE_P (new_scope)
6099 /* Since checking types for dependency can be expensive,
6100 avoid doing it if the type is already complete. */
6101 && !COMPLETE_TYPE_P (new_scope)
6102 /* Do not try to complete dependent types. */
6103 && !dependent_type_p (new_scope))
6105 new_scope = complete_type (new_scope);
6106 /* If it is a typedef to current class, use the current
6107 class instead, as the typedef won't have any names inside
6108 it yet. */
6109 if (!COMPLETE_TYPE_P (new_scope)
6110 && currently_open_class (new_scope))
6111 new_scope = TYPE_MAIN_VARIANT (new_scope);
6113 /* Make sure we look in the right scope the next time through
6114 the loop. */
6115 parser->scope = new_scope;
6118 /* If parsing tentatively, replace the sequence of tokens that makes
6119 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6120 token. That way, should we re-parse the token stream, we will
6121 not have to repeat the effort required to do the parse, nor will
6122 we issue duplicate error messages. */
6123 if (success && start)
6125 cp_token *token;
6127 token = cp_lexer_token_at (parser->lexer, start);
6128 /* Reset the contents of the START token. */
6129 token->type = CPP_NESTED_NAME_SPECIFIER;
6130 /* Retrieve any deferred checks. Do not pop this access checks yet
6131 so the memory will not be reclaimed during token replacing below. */
6132 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6133 token->u.tree_check_value->value = parser->scope;
6134 token->u.tree_check_value->checks = get_deferred_access_checks ();
6135 token->u.tree_check_value->qualifying_scope =
6136 parser->qualifying_scope;
6137 token->keyword = RID_MAX;
6139 /* Purge all subsequent tokens. */
6140 cp_lexer_purge_tokens_after (parser->lexer, start);
6143 if (start)
6144 pop_to_parent_deferring_access_checks ();
6146 return success ? parser->scope : NULL_TREE;
6149 /* Parse a nested-name-specifier. See
6150 cp_parser_nested_name_specifier_opt for details. This function
6151 behaves identically, except that it will an issue an error if no
6152 nested-name-specifier is present. */
6154 static tree
6155 cp_parser_nested_name_specifier (cp_parser *parser,
6156 bool typename_keyword_p,
6157 bool check_dependency_p,
6158 bool type_p,
6159 bool is_declaration)
6161 tree scope;
6163 /* Look for the nested-name-specifier. */
6164 scope = cp_parser_nested_name_specifier_opt (parser,
6165 typename_keyword_p,
6166 check_dependency_p,
6167 type_p,
6168 is_declaration);
6169 /* If it was not present, issue an error message. */
6170 if (!scope)
6172 cp_parser_error (parser, "expected nested-name-specifier");
6173 parser->scope = NULL_TREE;
6176 return scope;
6179 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6180 this is either a class-name or a namespace-name (which corresponds
6181 to the class-or-namespace-name production in the grammar). For
6182 C++0x, it can also be a type-name that refers to an enumeration
6183 type or a simple-template-id.
6185 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6186 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6187 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6188 TYPE_P is TRUE iff the next name should be taken as a class-name,
6189 even the same name is declared to be another entity in the same
6190 scope.
6192 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6193 specified by the class-or-namespace-name. If neither is found the
6194 ERROR_MARK_NODE is returned. */
6196 static tree
6197 cp_parser_qualifying_entity (cp_parser *parser,
6198 bool typename_keyword_p,
6199 bool template_keyword_p,
6200 bool check_dependency_p,
6201 bool type_p,
6202 bool is_declaration)
6204 tree saved_scope;
6205 tree saved_qualifying_scope;
6206 tree saved_object_scope;
6207 tree scope;
6208 bool only_class_p;
6209 bool successful_parse_p;
6211 /* DR 743: decltype can appear in a nested-name-specifier. */
6212 if (cp_lexer_next_token_is_decltype (parser->lexer))
6214 scope = cp_parser_decltype (parser);
6215 if (TREE_CODE (scope) != ENUMERAL_TYPE
6216 && !MAYBE_CLASS_TYPE_P (scope))
6218 cp_parser_simulate_error (parser);
6219 return error_mark_node;
6221 if (TYPE_NAME (scope))
6222 scope = TYPE_NAME (scope);
6223 return scope;
6226 /* Before we try to parse the class-name, we must save away the
6227 current PARSER->SCOPE since cp_parser_class_name will destroy
6228 it. */
6229 saved_scope = parser->scope;
6230 saved_qualifying_scope = parser->qualifying_scope;
6231 saved_object_scope = parser->object_scope;
6232 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6233 there is no need to look for a namespace-name. */
6234 only_class_p = template_keyword_p
6235 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6236 if (!only_class_p)
6237 cp_parser_parse_tentatively (parser);
6238 scope = cp_parser_class_name (parser,
6239 typename_keyword_p,
6240 template_keyword_p,
6241 type_p ? class_type : none_type,
6242 check_dependency_p,
6243 /*class_head_p=*/false,
6244 is_declaration,
6245 /*enum_ok=*/cxx_dialect > cxx98);
6246 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6247 /* If that didn't work, try for a namespace-name. */
6248 if (!only_class_p && !successful_parse_p)
6250 /* Restore the saved scope. */
6251 parser->scope = saved_scope;
6252 parser->qualifying_scope = saved_qualifying_scope;
6253 parser->object_scope = saved_object_scope;
6254 /* If we are not looking at an identifier followed by the scope
6255 resolution operator, then this is not part of a
6256 nested-name-specifier. (Note that this function is only used
6257 to parse the components of a nested-name-specifier.) */
6258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6259 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6260 return error_mark_node;
6261 scope = cp_parser_namespace_name (parser);
6264 return scope;
6267 /* Return true if we are looking at a compound-literal, false otherwise. */
6269 static bool
6270 cp_parser_compound_literal_p (cp_parser *parser)
6272 /* Consume the `('. */
6273 cp_lexer_consume_token (parser->lexer);
6275 cp_lexer_save_tokens (parser->lexer);
6277 /* Skip tokens until the next token is a closing parenthesis.
6278 If we find the closing `)', and the next token is a `{', then
6279 we are looking at a compound-literal. */
6280 bool compound_literal_p
6281 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6282 /*consume_paren=*/true)
6283 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6285 /* Roll back the tokens we skipped. */
6286 cp_lexer_rollback_tokens (parser->lexer);
6288 return compound_literal_p;
6291 /* Parse a postfix-expression.
6293 postfix-expression:
6294 primary-expression
6295 postfix-expression [ expression ]
6296 postfix-expression ( expression-list [opt] )
6297 simple-type-specifier ( expression-list [opt] )
6298 typename :: [opt] nested-name-specifier identifier
6299 ( expression-list [opt] )
6300 typename :: [opt] nested-name-specifier template [opt] template-id
6301 ( expression-list [opt] )
6302 postfix-expression . template [opt] id-expression
6303 postfix-expression -> template [opt] id-expression
6304 postfix-expression . pseudo-destructor-name
6305 postfix-expression -> pseudo-destructor-name
6306 postfix-expression ++
6307 postfix-expression --
6308 dynamic_cast < type-id > ( expression )
6309 static_cast < type-id > ( expression )
6310 reinterpret_cast < type-id > ( expression )
6311 const_cast < type-id > ( expression )
6312 typeid ( expression )
6313 typeid ( type-id )
6315 GNU Extension:
6317 postfix-expression:
6318 ( type-id ) { initializer-list , [opt] }
6320 This extension is a GNU version of the C99 compound-literal
6321 construct. (The C99 grammar uses `type-name' instead of `type-id',
6322 but they are essentially the same concept.)
6324 If ADDRESS_P is true, the postfix expression is the operand of the
6325 `&' operator. CAST_P is true if this expression is the target of a
6326 cast.
6328 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6329 class member access expressions [expr.ref].
6331 Returns a representation of the expression. */
6333 static cp_expr
6334 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6335 bool member_access_only_p, bool decltype_p,
6336 cp_id_kind * pidk_return)
6338 cp_token *token;
6339 location_t loc;
6340 enum rid keyword;
6341 cp_id_kind idk = CP_ID_KIND_NONE;
6342 cp_expr postfix_expression = NULL_TREE;
6343 bool is_member_access = false;
6344 int saved_in_statement = -1;
6346 /* Peek at the next token. */
6347 token = cp_lexer_peek_token (parser->lexer);
6348 loc = token->location;
6349 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6351 /* Some of the productions are determined by keywords. */
6352 keyword = token->keyword;
6353 switch (keyword)
6355 case RID_DYNCAST:
6356 case RID_STATCAST:
6357 case RID_REINTCAST:
6358 case RID_CONSTCAST:
6360 tree type;
6361 cp_expr expression;
6362 const char *saved_message;
6363 bool saved_in_type_id_in_expr_p;
6365 /* All of these can be handled in the same way from the point
6366 of view of parsing. Begin by consuming the token
6367 identifying the cast. */
6368 cp_lexer_consume_token (parser->lexer);
6370 /* New types cannot be defined in the cast. */
6371 saved_message = parser->type_definition_forbidden_message;
6372 parser->type_definition_forbidden_message
6373 = G_("types may not be defined in casts");
6375 /* Look for the opening `<'. */
6376 cp_parser_require (parser, CPP_LESS, RT_LESS);
6377 /* Parse the type to which we are casting. */
6378 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6379 parser->in_type_id_in_expr_p = true;
6380 type = cp_parser_type_id (parser);
6381 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6382 /* Look for the closing `>'. */
6383 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6384 /* Restore the old message. */
6385 parser->type_definition_forbidden_message = saved_message;
6387 bool saved_greater_than_is_operator_p
6388 = parser->greater_than_is_operator_p;
6389 parser->greater_than_is_operator_p = true;
6391 /* And the expression which is being cast. */
6392 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6393 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6394 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6395 RT_CLOSE_PAREN);
6396 location_t end_loc = close_paren ?
6397 close_paren->location : UNKNOWN_LOCATION;
6399 parser->greater_than_is_operator_p
6400 = saved_greater_than_is_operator_p;
6402 /* Only type conversions to integral or enumeration types
6403 can be used in constant-expressions. */
6404 if (!cast_valid_in_integral_constant_expression_p (type)
6405 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6406 return error_mark_node;
6408 switch (keyword)
6410 case RID_DYNCAST:
6411 postfix_expression
6412 = build_dynamic_cast (type, expression, tf_warning_or_error);
6413 break;
6414 case RID_STATCAST:
6415 postfix_expression
6416 = build_static_cast (type, expression, tf_warning_or_error);
6417 break;
6418 case RID_REINTCAST:
6419 postfix_expression
6420 = build_reinterpret_cast (type, expression,
6421 tf_warning_or_error);
6422 break;
6423 case RID_CONSTCAST:
6424 postfix_expression
6425 = build_const_cast (type, expression, tf_warning_or_error);
6426 break;
6427 default:
6428 gcc_unreachable ();
6431 /* Construct a location e.g. :
6432 reinterpret_cast <int *> (expr)
6433 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6434 ranging from the start of the "*_cast" token to the final closing
6435 paren, with the caret at the start. */
6436 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6437 postfix_expression.set_location (cp_cast_loc);
6439 break;
6441 case RID_TYPEID:
6443 tree type;
6444 const char *saved_message;
6445 bool saved_in_type_id_in_expr_p;
6447 /* Consume the `typeid' token. */
6448 cp_lexer_consume_token (parser->lexer);
6449 /* Look for the `(' token. */
6450 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6451 /* Types cannot be defined in a `typeid' expression. */
6452 saved_message = parser->type_definition_forbidden_message;
6453 parser->type_definition_forbidden_message
6454 = G_("types may not be defined in a %<typeid%> expression");
6455 /* We can't be sure yet whether we're looking at a type-id or an
6456 expression. */
6457 cp_parser_parse_tentatively (parser);
6458 /* Try a type-id first. */
6459 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6460 parser->in_type_id_in_expr_p = true;
6461 type = cp_parser_type_id (parser);
6462 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6463 /* Look for the `)' token. Otherwise, we can't be sure that
6464 we're not looking at an expression: consider `typeid (int
6465 (3))', for example. */
6466 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6467 /* If all went well, simply lookup the type-id. */
6468 if (cp_parser_parse_definitely (parser))
6469 postfix_expression = get_typeid (type, tf_warning_or_error);
6470 /* Otherwise, fall back to the expression variant. */
6471 else
6473 tree expression;
6475 /* Look for an expression. */
6476 expression = cp_parser_expression (parser, & idk);
6477 /* Compute its typeid. */
6478 postfix_expression = build_typeid (expression, tf_warning_or_error);
6479 /* Look for the `)' token. */
6480 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6482 /* Restore the saved message. */
6483 parser->type_definition_forbidden_message = saved_message;
6484 /* `typeid' may not appear in an integral constant expression. */
6485 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6486 return error_mark_node;
6488 break;
6490 case RID_TYPENAME:
6492 tree type;
6493 /* The syntax permitted here is the same permitted for an
6494 elaborated-type-specifier. */
6495 ++parser->prevent_constrained_type_specifiers;
6496 type = cp_parser_elaborated_type_specifier (parser,
6497 /*is_friend=*/false,
6498 /*is_declaration=*/false);
6499 --parser->prevent_constrained_type_specifiers;
6500 postfix_expression = cp_parser_functional_cast (parser, type);
6502 break;
6504 case RID_CILK_SPAWN:
6506 location_t cilk_spawn_loc
6507 = cp_lexer_peek_token (parser->lexer)->location;
6508 cp_lexer_consume_token (parser->lexer);
6509 token = cp_lexer_peek_token (parser->lexer);
6510 if (token->type == CPP_SEMICOLON)
6512 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6513 "an expression");
6514 postfix_expression = error_mark_node;
6515 break;
6517 else if (!current_function_decl)
6519 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6520 "inside a function");
6521 postfix_expression = error_mark_node;
6522 break;
6524 else
6526 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6527 saved_in_statement = parser->in_statement;
6528 parser->in_statement |= IN_CILK_SPAWN;
6530 cfun->calls_cilk_spawn = 1;
6531 postfix_expression =
6532 cp_parser_postfix_expression (parser, false, false,
6533 false, false, &idk);
6534 if (!flag_cilkplus)
6536 error_at (token->location, "-fcilkplus must be enabled to use"
6537 " %<_Cilk_spawn%>");
6538 cfun->calls_cilk_spawn = 0;
6540 else if (saved_in_statement & IN_CILK_SPAWN)
6542 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6543 "are not permitted");
6544 postfix_expression = error_mark_node;
6545 cfun->calls_cilk_spawn = 0;
6547 else
6549 location_t loc = postfix_expression.get_location ();
6550 postfix_expression = build_cilk_spawn (token->location,
6551 postfix_expression);
6552 /* Build a location of the form:
6553 _Cilk_spawn expr
6554 ~~~~~~~~~~~~^~~~
6555 with caret at the expr, ranging from the start of the
6556 _Cilk_spawn token to the end of the expression. */
6557 location_t combined_loc =
6558 make_location (loc, cilk_spawn_loc, get_finish (loc));
6559 postfix_expression.set_location (combined_loc);
6560 if (postfix_expression != error_mark_node)
6561 SET_EXPR_LOCATION (postfix_expression, input_location);
6562 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6564 break;
6567 case RID_BUILTIN_SHUFFLE:
6569 vec<tree, va_gc> *vec;
6570 unsigned int i;
6571 tree p;
6573 cp_lexer_consume_token (parser->lexer);
6574 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6575 /*cast_p=*/false, /*allow_expansion_p=*/true,
6576 /*non_constant_p=*/NULL);
6577 if (vec == NULL)
6578 return error_mark_node;
6580 FOR_EACH_VEC_ELT (*vec, i, p)
6581 mark_exp_read (p);
6583 if (vec->length () == 2)
6584 return build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE, (*vec)[1],
6585 tf_warning_or_error);
6586 else if (vec->length () == 3)
6587 return build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1], (*vec)[2],
6588 tf_warning_or_error);
6589 else
6591 error_at (loc, "wrong number of arguments to "
6592 "%<__builtin_shuffle%>");
6593 return error_mark_node;
6595 break;
6598 default:
6600 tree type;
6602 /* If the next thing is a simple-type-specifier, we may be
6603 looking at a functional cast. We could also be looking at
6604 an id-expression. So, we try the functional cast, and if
6605 that doesn't work we fall back to the primary-expression. */
6606 cp_parser_parse_tentatively (parser);
6607 /* Look for the simple-type-specifier. */
6608 ++parser->prevent_constrained_type_specifiers;
6609 type = cp_parser_simple_type_specifier (parser,
6610 /*decl_specs=*/NULL,
6611 CP_PARSER_FLAGS_NONE);
6612 --parser->prevent_constrained_type_specifiers;
6613 /* Parse the cast itself. */
6614 if (!cp_parser_error_occurred (parser))
6615 postfix_expression
6616 = cp_parser_functional_cast (parser, type);
6617 /* If that worked, we're done. */
6618 if (cp_parser_parse_definitely (parser))
6619 break;
6621 /* If the functional-cast didn't work out, try a
6622 compound-literal. */
6623 if (cp_parser_allow_gnu_extensions_p (parser)
6624 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6626 cp_expr initializer = NULL_TREE;
6628 cp_parser_parse_tentatively (parser);
6630 /* Avoid calling cp_parser_type_id pointlessly, see comment
6631 in cp_parser_cast_expression about c++/29234. */
6632 if (!cp_parser_compound_literal_p (parser))
6633 cp_parser_simulate_error (parser);
6634 else
6636 /* Parse the type. */
6637 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6638 parser->in_type_id_in_expr_p = true;
6639 type = cp_parser_type_id (parser);
6640 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6641 /* Look for the `)'. */
6642 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6645 /* If things aren't going well, there's no need to
6646 keep going. */
6647 if (!cp_parser_error_occurred (parser))
6649 bool non_constant_p;
6650 /* Parse the brace-enclosed initializer list. */
6651 initializer = cp_parser_braced_list (parser,
6652 &non_constant_p);
6654 /* If that worked, we're definitely looking at a
6655 compound-literal expression. */
6656 if (cp_parser_parse_definitely (parser))
6658 /* Warn the user that a compound literal is not
6659 allowed in standard C++. */
6660 pedwarn (input_location, OPT_Wpedantic,
6661 "ISO C++ forbids compound-literals");
6662 /* For simplicity, we disallow compound literals in
6663 constant-expressions. We could
6664 allow compound literals of integer type, whose
6665 initializer was a constant, in constant
6666 expressions. Permitting that usage, as a further
6667 extension, would not change the meaning of any
6668 currently accepted programs. (Of course, as
6669 compound literals are not part of ISO C++, the
6670 standard has nothing to say.) */
6671 if (cp_parser_non_integral_constant_expression (parser,
6672 NIC_NCC))
6674 postfix_expression = error_mark_node;
6675 break;
6677 /* Form the representation of the compound-literal. */
6678 postfix_expression
6679 = finish_compound_literal (type, initializer,
6680 tf_warning_or_error);
6681 postfix_expression.set_location (initializer.get_location ());
6682 break;
6686 /* It must be a primary-expression. */
6687 postfix_expression
6688 = cp_parser_primary_expression (parser, address_p, cast_p,
6689 /*template_arg_p=*/false,
6690 decltype_p,
6691 &idk);
6693 break;
6696 /* Note that we don't need to worry about calling build_cplus_new on a
6697 class-valued CALL_EXPR in decltype when it isn't the end of the
6698 postfix-expression; unary_complex_lvalue will take care of that for
6699 all these cases. */
6701 /* Keep looping until the postfix-expression is complete. */
6702 while (true)
6704 if (idk == CP_ID_KIND_UNQUALIFIED
6705 && identifier_p (postfix_expression)
6706 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6707 /* It is not a Koenig lookup function call. */
6708 postfix_expression
6709 = unqualified_name_lookup_error (postfix_expression);
6711 /* Peek at the next token. */
6712 token = cp_lexer_peek_token (parser->lexer);
6714 switch (token->type)
6716 case CPP_OPEN_SQUARE:
6717 if (cp_next_tokens_can_be_std_attribute_p (parser))
6719 cp_parser_error (parser,
6720 "two consecutive %<[%> shall "
6721 "only introduce an attribute");
6722 return error_mark_node;
6724 postfix_expression
6725 = cp_parser_postfix_open_square_expression (parser,
6726 postfix_expression,
6727 false,
6728 decltype_p);
6729 postfix_expression.set_range (start_loc,
6730 postfix_expression.get_location ());
6732 idk = CP_ID_KIND_NONE;
6733 is_member_access = false;
6734 break;
6736 case CPP_OPEN_PAREN:
6737 /* postfix-expression ( expression-list [opt] ) */
6739 bool koenig_p;
6740 bool is_builtin_constant_p;
6741 bool saved_integral_constant_expression_p = false;
6742 bool saved_non_integral_constant_expression_p = false;
6743 tsubst_flags_t complain = complain_flags (decltype_p);
6744 vec<tree, va_gc> *args;
6745 location_t close_paren_loc = UNKNOWN_LOCATION;
6747 is_member_access = false;
6749 is_builtin_constant_p
6750 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6751 if (is_builtin_constant_p)
6753 /* The whole point of __builtin_constant_p is to allow
6754 non-constant expressions to appear as arguments. */
6755 saved_integral_constant_expression_p
6756 = parser->integral_constant_expression_p;
6757 saved_non_integral_constant_expression_p
6758 = parser->non_integral_constant_expression_p;
6759 parser->integral_constant_expression_p = false;
6761 args = (cp_parser_parenthesized_expression_list
6762 (parser, non_attr,
6763 /*cast_p=*/false, /*allow_expansion_p=*/true,
6764 /*non_constant_p=*/NULL,
6765 /*close_paren_loc=*/&close_paren_loc));
6766 if (is_builtin_constant_p)
6768 parser->integral_constant_expression_p
6769 = saved_integral_constant_expression_p;
6770 parser->non_integral_constant_expression_p
6771 = saved_non_integral_constant_expression_p;
6774 if (args == NULL)
6776 postfix_expression = error_mark_node;
6777 break;
6780 /* Function calls are not permitted in
6781 constant-expressions. */
6782 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6783 && cp_parser_non_integral_constant_expression (parser,
6784 NIC_FUNC_CALL))
6786 postfix_expression = error_mark_node;
6787 release_tree_vector (args);
6788 break;
6791 koenig_p = false;
6792 if (idk == CP_ID_KIND_UNQUALIFIED
6793 || idk == CP_ID_KIND_TEMPLATE_ID)
6795 if (identifier_p (postfix_expression))
6797 if (!args->is_empty ())
6799 koenig_p = true;
6800 if (!any_type_dependent_arguments_p (args))
6801 postfix_expression
6802 = perform_koenig_lookup (postfix_expression, args,
6803 complain);
6805 else
6806 postfix_expression
6807 = unqualified_fn_lookup_error (postfix_expression);
6809 /* We do not perform argument-dependent lookup if
6810 normal lookup finds a non-function, in accordance
6811 with the expected resolution of DR 218. */
6812 else if (!args->is_empty ()
6813 && is_overloaded_fn (postfix_expression))
6815 tree fn = get_first_fn (postfix_expression);
6816 fn = STRIP_TEMPLATE (fn);
6818 /* Do not do argument dependent lookup if regular
6819 lookup finds a member function or a block-scope
6820 function declaration. [basic.lookup.argdep]/3 */
6821 if (!DECL_FUNCTION_MEMBER_P (fn)
6822 && !DECL_LOCAL_FUNCTION_P (fn))
6824 koenig_p = true;
6825 if (!any_type_dependent_arguments_p (args))
6826 postfix_expression
6827 = perform_koenig_lookup (postfix_expression, args,
6828 complain);
6833 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6834 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6835 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6836 && vec_safe_length (args) == 3)
6838 tree arg0 = (*args)[0];
6839 tree arg1 = (*args)[1];
6840 tree arg2 = (*args)[2];
6841 int literal_mask = ((!!integer_zerop (arg1) << 1)
6842 | (!!integer_zerop (arg2) << 2));
6843 if (TREE_CODE (arg2) == CONST_DECL)
6844 arg2 = DECL_INITIAL (arg2);
6845 warn_for_memset (input_location, arg0, arg2, literal_mask);
6848 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6850 tree instance = TREE_OPERAND (postfix_expression, 0);
6851 tree fn = TREE_OPERAND (postfix_expression, 1);
6853 if (processing_template_decl
6854 && (type_dependent_expression_p (instance)
6855 || (!BASELINK_P (fn)
6856 && TREE_CODE (fn) != FIELD_DECL)
6857 || type_dependent_expression_p (fn)
6858 || any_type_dependent_arguments_p (args)))
6860 postfix_expression
6861 = build_nt_call_vec (postfix_expression, args);
6862 release_tree_vector (args);
6863 break;
6866 if (BASELINK_P (fn))
6868 postfix_expression
6869 = (build_new_method_call
6870 (instance, fn, &args, NULL_TREE,
6871 (idk == CP_ID_KIND_QUALIFIED
6872 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6873 : LOOKUP_NORMAL),
6874 /*fn_p=*/NULL,
6875 complain));
6877 else
6878 postfix_expression
6879 = finish_call_expr (postfix_expression, &args,
6880 /*disallow_virtual=*/false,
6881 /*koenig_p=*/false,
6882 complain);
6884 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6885 || TREE_CODE (postfix_expression) == MEMBER_REF
6886 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6887 postfix_expression = (build_offset_ref_call_from_tree
6888 (postfix_expression, &args,
6889 complain));
6890 else if (idk == CP_ID_KIND_QUALIFIED)
6891 /* A call to a static class member, or a namespace-scope
6892 function. */
6893 postfix_expression
6894 = finish_call_expr (postfix_expression, &args,
6895 /*disallow_virtual=*/true,
6896 koenig_p,
6897 complain);
6898 else
6899 /* All other function calls. */
6900 postfix_expression
6901 = finish_call_expr (postfix_expression, &args,
6902 /*disallow_virtual=*/false,
6903 koenig_p,
6904 complain);
6906 if (close_paren_loc != UNKNOWN_LOCATION)
6908 location_t combined_loc = make_location (token->location,
6909 start_loc,
6910 close_paren_loc);
6911 postfix_expression.set_location (combined_loc);
6914 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
6915 idk = CP_ID_KIND_NONE;
6917 release_tree_vector (args);
6919 break;
6921 case CPP_DOT:
6922 case CPP_DEREF:
6923 /* postfix-expression . template [opt] id-expression
6924 postfix-expression . pseudo-destructor-name
6925 postfix-expression -> template [opt] id-expression
6926 postfix-expression -> pseudo-destructor-name */
6928 /* Consume the `.' or `->' operator. */
6929 cp_lexer_consume_token (parser->lexer);
6931 postfix_expression
6932 = cp_parser_postfix_dot_deref_expression (parser, token->type,
6933 postfix_expression,
6934 false, &idk, loc);
6936 is_member_access = true;
6937 break;
6939 case CPP_PLUS_PLUS:
6940 /* postfix-expression ++ */
6941 /* Consume the `++' token. */
6942 cp_lexer_consume_token (parser->lexer);
6943 /* Generate a representation for the complete expression. */
6944 postfix_expression
6945 = finish_increment_expr (postfix_expression,
6946 POSTINCREMENT_EXPR);
6947 /* Increments may not appear in constant-expressions. */
6948 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
6949 postfix_expression = error_mark_node;
6950 idk = CP_ID_KIND_NONE;
6951 is_member_access = false;
6952 break;
6954 case CPP_MINUS_MINUS:
6955 /* postfix-expression -- */
6956 /* Consume the `--' token. */
6957 cp_lexer_consume_token (parser->lexer);
6958 /* Generate a representation for the complete expression. */
6959 postfix_expression
6960 = finish_increment_expr (postfix_expression,
6961 POSTDECREMENT_EXPR);
6962 /* Decrements may not appear in constant-expressions. */
6963 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
6964 postfix_expression = error_mark_node;
6965 idk = CP_ID_KIND_NONE;
6966 is_member_access = false;
6967 break;
6969 default:
6970 if (pidk_return != NULL)
6971 * pidk_return = idk;
6972 if (member_access_only_p)
6973 return is_member_access
6974 ? postfix_expression
6975 : cp_expr (error_mark_node);
6976 else
6977 return postfix_expression;
6981 /* We should never get here. */
6982 gcc_unreachable ();
6983 return error_mark_node;
6986 /* This function parses Cilk Plus array notations. If a normal array expr. is
6987 parsed then the array index is passed back to the caller through *INIT_INDEX
6988 and the function returns a NULL_TREE. If array notation expr. is parsed,
6989 then *INIT_INDEX is ignored by the caller and the function returns
6990 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
6991 error_mark_node. */
6993 static tree
6994 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
6995 tree array_value)
6997 cp_token *token = NULL;
6998 tree length_index, stride = NULL_TREE, value_tree, array_type;
6999 if (!array_value || array_value == error_mark_node)
7001 cp_parser_skip_to_end_of_statement (parser);
7002 return error_mark_node;
7005 array_type = TREE_TYPE (array_value);
7007 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7008 parser->colon_corrects_to_scope_p = false;
7009 token = cp_lexer_peek_token (parser->lexer);
7011 if (!token)
7013 cp_parser_error (parser, "expected %<:%> or numeral");
7014 return error_mark_node;
7016 else if (token->type == CPP_COLON)
7018 /* Consume the ':'. */
7019 cp_lexer_consume_token (parser->lexer);
7021 /* If we are here, then we have a case like this A[:]. */
7022 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7024 cp_parser_error (parser, "expected %<]%>");
7025 cp_parser_skip_to_end_of_statement (parser);
7026 return error_mark_node;
7028 *init_index = NULL_TREE;
7029 stride = NULL_TREE;
7030 length_index = NULL_TREE;
7032 else
7034 /* If we are here, then there are three valid possibilities:
7035 1. ARRAY [ EXP ]
7036 2. ARRAY [ EXP : EXP ]
7037 3. ARRAY [ EXP : EXP : EXP ] */
7039 *init_index = cp_parser_expression (parser);
7040 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7042 /* This indicates that we have a normal array expression. */
7043 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7044 return NULL_TREE;
7047 /* Consume the ':'. */
7048 cp_lexer_consume_token (parser->lexer);
7049 length_index = cp_parser_expression (parser);
7050 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7052 cp_lexer_consume_token (parser->lexer);
7053 stride = cp_parser_expression (parser);
7056 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7058 if (*init_index == error_mark_node || length_index == error_mark_node
7059 || stride == error_mark_node || array_type == error_mark_node)
7061 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7062 cp_lexer_consume_token (parser->lexer);
7063 return error_mark_node;
7065 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7067 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7068 length_index, stride, array_type);
7069 return value_tree;
7072 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7073 by cp_parser_builtin_offsetof. We're looking for
7075 postfix-expression [ expression ]
7076 postfix-expression [ braced-init-list ] (C++11)
7078 FOR_OFFSETOF is set if we're being called in that context, which
7079 changes how we deal with integer constant expressions. */
7081 static tree
7082 cp_parser_postfix_open_square_expression (cp_parser *parser,
7083 tree postfix_expression,
7084 bool for_offsetof,
7085 bool decltype_p)
7087 tree index = NULL_TREE;
7088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7089 bool saved_greater_than_is_operator_p;
7091 /* Consume the `[' token. */
7092 cp_lexer_consume_token (parser->lexer);
7094 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7095 parser->greater_than_is_operator_p = true;
7097 /* Parse the index expression. */
7098 /* ??? For offsetof, there is a question of what to allow here. If
7099 offsetof is not being used in an integral constant expression context,
7100 then we *could* get the right answer by computing the value at runtime.
7101 If we are in an integral constant expression context, then we might
7102 could accept any constant expression; hard to say without analysis.
7103 Rather than open the barn door too wide right away, allow only integer
7104 constant expressions here. */
7105 if (for_offsetof)
7106 index = cp_parser_constant_expression (parser);
7107 else
7109 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7111 bool expr_nonconst_p;
7112 cp_lexer_set_source_position (parser->lexer);
7113 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7114 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7115 if (flag_cilkplus
7116 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7118 error_at (cp_lexer_peek_token (parser->lexer)->location,
7119 "braced list index is not allowed with array "
7120 "notation");
7121 cp_parser_skip_to_end_of_statement (parser);
7122 return error_mark_node;
7125 else if (flag_cilkplus)
7127 /* Here are have these two options:
7128 ARRAY[EXP : EXP] - Array notation expr with default
7129 stride of 1.
7130 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7131 stride. */
7132 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7133 postfix_expression);
7134 if (an_exp)
7135 return an_exp;
7137 else
7138 index = cp_parser_expression (parser);
7141 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7143 /* Look for the closing `]'. */
7144 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7146 /* Build the ARRAY_REF. */
7147 postfix_expression = grok_array_decl (loc, postfix_expression,
7148 index, decltype_p);
7150 /* When not doing offsetof, array references are not permitted in
7151 constant-expressions. */
7152 if (!for_offsetof
7153 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7154 postfix_expression = error_mark_node;
7156 return postfix_expression;
7159 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7160 by cp_parser_builtin_offsetof. We're looking for
7162 postfix-expression . template [opt] id-expression
7163 postfix-expression . pseudo-destructor-name
7164 postfix-expression -> template [opt] id-expression
7165 postfix-expression -> pseudo-destructor-name
7167 FOR_OFFSETOF is set if we're being called in that context. That sorta
7168 limits what of the above we'll actually accept, but nevermind.
7169 TOKEN_TYPE is the "." or "->" token, which will already have been
7170 removed from the stream. */
7172 static tree
7173 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7174 enum cpp_ttype token_type,
7175 cp_expr postfix_expression,
7176 bool for_offsetof, cp_id_kind *idk,
7177 location_t location)
7179 tree name;
7180 bool dependent_p;
7181 bool pseudo_destructor_p;
7182 tree scope = NULL_TREE;
7183 location_t start_loc = postfix_expression.get_start ();
7185 /* If this is a `->' operator, dereference the pointer. */
7186 if (token_type == CPP_DEREF)
7187 postfix_expression = build_x_arrow (location, postfix_expression,
7188 tf_warning_or_error);
7189 /* Check to see whether or not the expression is type-dependent. */
7190 dependent_p = type_dependent_expression_p (postfix_expression);
7191 /* The identifier following the `->' or `.' is not qualified. */
7192 parser->scope = NULL_TREE;
7193 parser->qualifying_scope = NULL_TREE;
7194 parser->object_scope = NULL_TREE;
7195 *idk = CP_ID_KIND_NONE;
7197 /* Enter the scope corresponding to the type of the object
7198 given by the POSTFIX_EXPRESSION. */
7199 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
7201 scope = TREE_TYPE (postfix_expression);
7202 /* According to the standard, no expression should ever have
7203 reference type. Unfortunately, we do not currently match
7204 the standard in this respect in that our internal representation
7205 of an expression may have reference type even when the standard
7206 says it does not. Therefore, we have to manually obtain the
7207 underlying type here. */
7208 scope = non_reference (scope);
7209 /* The type of the POSTFIX_EXPRESSION must be complete. */
7210 if (scope == unknown_type_node)
7212 error_at (location, "%qE does not have class type",
7213 postfix_expression.get_value ());
7214 scope = NULL_TREE;
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 else if (postfix_expression != current_class_ref
7220 && !(processing_template_decl && scope == current_class_type))
7221 scope = complete_type_or_else (scope, NULL_TREE);
7222 /* Let the name lookup machinery know that we are processing a
7223 class member access expression. */
7224 parser->context->object_type = scope;
7225 /* If something went wrong, we want to be able to discern that case,
7226 as opposed to the case where there was no SCOPE due to the type
7227 of expression being dependent. */
7228 if (!scope)
7229 scope = error_mark_node;
7230 /* If the SCOPE was erroneous, make the various semantic analysis
7231 functions exit quickly -- and without issuing additional error
7232 messages. */
7233 if (scope == error_mark_node)
7234 postfix_expression = error_mark_node;
7236 else
7237 /* Tell cp_parser_lookup_name that there was an object, even though it's
7238 type-dependent. */
7239 parser->context->object_type = unknown_type_node;
7241 /* Assume this expression is not a pseudo-destructor access. */
7242 pseudo_destructor_p = false;
7244 /* If the SCOPE is a scalar type, then, if this is a valid program,
7245 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7246 is type dependent, it can be pseudo-destructor-name or something else.
7247 Try to parse it as pseudo-destructor-name first. */
7248 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7250 tree s;
7251 tree type;
7253 cp_parser_parse_tentatively (parser);
7254 /* Parse the pseudo-destructor-name. */
7255 s = NULL_TREE;
7256 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7257 &s, &type);
7258 if (dependent_p
7259 && (cp_parser_error_occurred (parser)
7260 || !SCALAR_TYPE_P (type)))
7261 cp_parser_abort_tentative_parse (parser);
7262 else if (cp_parser_parse_definitely (parser))
7264 pseudo_destructor_p = true;
7265 postfix_expression
7266 = finish_pseudo_destructor_expr (postfix_expression,
7267 s, type, location);
7271 if (!pseudo_destructor_p)
7273 /* If the SCOPE is not a scalar type, we are looking at an
7274 ordinary class member access expression, rather than a
7275 pseudo-destructor-name. */
7276 bool template_p;
7277 cp_token *token = cp_lexer_peek_token (parser->lexer);
7278 /* Parse the id-expression. */
7279 name = (cp_parser_id_expression
7280 (parser,
7281 cp_parser_optional_template_keyword (parser),
7282 /*check_dependency_p=*/true,
7283 &template_p,
7284 /*declarator_p=*/false,
7285 /*optional_p=*/false));
7286 /* In general, build a SCOPE_REF if the member name is qualified.
7287 However, if the name was not dependent and has already been
7288 resolved; there is no need to build the SCOPE_REF. For example;
7290 struct X { void f(); };
7291 template <typename T> void f(T* t) { t->X::f(); }
7293 Even though "t" is dependent, "X::f" is not and has been resolved
7294 to a BASELINK; there is no need to include scope information. */
7296 /* But we do need to remember that there was an explicit scope for
7297 virtual function calls. */
7298 if (parser->scope)
7299 *idk = CP_ID_KIND_QUALIFIED;
7301 /* If the name is a template-id that names a type, we will get a
7302 TYPE_DECL here. That is invalid code. */
7303 if (TREE_CODE (name) == TYPE_DECL)
7305 error_at (token->location, "invalid use of %qD", name);
7306 postfix_expression = error_mark_node;
7308 else
7310 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7312 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7314 error_at (token->location, "%<%D::%D%> is not a class member",
7315 parser->scope, name);
7316 postfix_expression = error_mark_node;
7318 else
7319 name = build_qualified_name (/*type=*/NULL_TREE,
7320 parser->scope,
7321 name,
7322 template_p);
7323 parser->scope = NULL_TREE;
7324 parser->qualifying_scope = NULL_TREE;
7325 parser->object_scope = NULL_TREE;
7327 if (parser->scope && name && BASELINK_P (name))
7328 adjust_result_of_qualified_name_lookup
7329 (name, parser->scope, scope);
7330 postfix_expression
7331 = finish_class_member_access_expr (postfix_expression, name,
7332 template_p,
7333 tf_warning_or_error);
7334 /* Build a location e.g.:
7335 ptr->access_expr
7336 ~~~^~~~~~~~~~~~~
7337 where the caret is at the deref token, ranging from
7338 the start of postfix_expression to the end of the access expr. */
7339 location_t end_loc
7340 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7341 location_t combined_loc
7342 = make_location (input_location, start_loc, end_loc);
7343 protected_set_expr_location (postfix_expression, combined_loc);
7347 /* We no longer need to look up names in the scope of the object on
7348 the left-hand side of the `.' or `->' operator. */
7349 parser->context->object_type = NULL_TREE;
7351 /* Outside of offsetof, these operators may not appear in
7352 constant-expressions. */
7353 if (!for_offsetof
7354 && (cp_parser_non_integral_constant_expression
7355 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7356 postfix_expression = error_mark_node;
7358 return postfix_expression;
7361 /* Parse a parenthesized expression-list.
7363 expression-list:
7364 assignment-expression
7365 expression-list, assignment-expression
7367 attribute-list:
7368 expression-list
7369 identifier
7370 identifier, expression-list
7372 CAST_P is true if this expression is the target of a cast.
7374 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7375 argument pack.
7377 Returns a vector of trees. Each element is a representation of an
7378 assignment-expression. NULL is returned if the ( and or ) are
7379 missing. An empty, but allocated, vector is returned on no
7380 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7381 if we are parsing an attribute list for an attribute that wants a
7382 plain identifier argument, normal_attr for an attribute that wants
7383 an expression, or non_attr if we aren't parsing an attribute list. If
7384 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7385 not all of the expressions in the list were constant.
7386 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7387 will be written to with the location of the closing parenthesis. If
7388 an error occurs, it may or may not be written to. */
7390 static vec<tree, va_gc> *
7391 cp_parser_parenthesized_expression_list (cp_parser* parser,
7392 int is_attribute_list,
7393 bool cast_p,
7394 bool allow_expansion_p,
7395 bool *non_constant_p,
7396 location_t *close_paren_loc)
7398 vec<tree, va_gc> *expression_list;
7399 bool fold_expr_p = is_attribute_list != non_attr;
7400 tree identifier = NULL_TREE;
7401 bool saved_greater_than_is_operator_p;
7403 /* Assume all the expressions will be constant. */
7404 if (non_constant_p)
7405 *non_constant_p = false;
7407 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7408 return NULL;
7410 expression_list = make_tree_vector ();
7412 /* Within a parenthesized expression, a `>' token is always
7413 the greater-than operator. */
7414 saved_greater_than_is_operator_p
7415 = parser->greater_than_is_operator_p;
7416 parser->greater_than_is_operator_p = true;
7418 /* Consume expressions until there are no more. */
7419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7420 while (true)
7422 tree expr;
7424 /* At the beginning of attribute lists, check to see if the
7425 next token is an identifier. */
7426 if (is_attribute_list == id_attr
7427 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7429 cp_token *token;
7431 /* Consume the identifier. */
7432 token = cp_lexer_consume_token (parser->lexer);
7433 /* Save the identifier. */
7434 identifier = token->u.value;
7436 else
7438 bool expr_non_constant_p;
7440 /* Parse the next assignment-expression. */
7441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7443 /* A braced-init-list. */
7444 cp_lexer_set_source_position (parser->lexer);
7445 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7446 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7447 if (non_constant_p && expr_non_constant_p)
7448 *non_constant_p = true;
7450 else if (non_constant_p)
7452 expr = (cp_parser_constant_expression
7453 (parser, /*allow_non_constant_p=*/true,
7454 &expr_non_constant_p));
7455 if (expr_non_constant_p)
7456 *non_constant_p = true;
7458 else
7459 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7460 cast_p);
7462 if (fold_expr_p)
7463 expr = instantiate_non_dependent_expr (expr);
7465 /* If we have an ellipsis, then this is an expression
7466 expansion. */
7467 if (allow_expansion_p
7468 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7470 /* Consume the `...'. */
7471 cp_lexer_consume_token (parser->lexer);
7473 /* Build the argument pack. */
7474 expr = make_pack_expansion (expr);
7477 /* Add it to the list. We add error_mark_node
7478 expressions to the list, so that we can still tell if
7479 the correct form for a parenthesized expression-list
7480 is found. That gives better errors. */
7481 vec_safe_push (expression_list, expr);
7483 if (expr == error_mark_node)
7484 goto skip_comma;
7487 /* After the first item, attribute lists look the same as
7488 expression lists. */
7489 is_attribute_list = non_attr;
7491 get_comma:;
7492 /* If the next token isn't a `,', then we are done. */
7493 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7494 break;
7496 /* Otherwise, consume the `,' and keep going. */
7497 cp_lexer_consume_token (parser->lexer);
7500 if (close_paren_loc)
7501 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7505 int ending;
7507 skip_comma:;
7508 /* We try and resync to an unnested comma, as that will give the
7509 user better diagnostics. */
7510 ending = cp_parser_skip_to_closing_parenthesis (parser,
7511 /*recovering=*/true,
7512 /*or_comma=*/true,
7513 /*consume_paren=*/true);
7514 if (ending < 0)
7515 goto get_comma;
7516 if (!ending)
7518 parser->greater_than_is_operator_p
7519 = saved_greater_than_is_operator_p;
7520 return NULL;
7524 parser->greater_than_is_operator_p
7525 = saved_greater_than_is_operator_p;
7527 if (identifier)
7528 vec_safe_insert (expression_list, 0, identifier);
7530 return expression_list;
7533 /* Parse a pseudo-destructor-name.
7535 pseudo-destructor-name:
7536 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7537 :: [opt] nested-name-specifier template template-id :: ~ type-name
7538 :: [opt] nested-name-specifier [opt] ~ type-name
7540 If either of the first two productions is used, sets *SCOPE to the
7541 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7542 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7543 or ERROR_MARK_NODE if the parse fails. */
7545 static void
7546 cp_parser_pseudo_destructor_name (cp_parser* parser,
7547 tree object,
7548 tree* scope,
7549 tree* type)
7551 bool nested_name_specifier_p;
7553 /* Handle ~auto. */
7554 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7555 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7556 && !type_dependent_expression_p (object))
7558 if (cxx_dialect < cxx14)
7559 pedwarn (input_location, 0,
7560 "%<~auto%> only available with "
7561 "-std=c++14 or -std=gnu++14");
7562 cp_lexer_consume_token (parser->lexer);
7563 cp_lexer_consume_token (parser->lexer);
7564 *scope = NULL_TREE;
7565 *type = TREE_TYPE (object);
7566 return;
7569 /* Assume that things will not work out. */
7570 *type = error_mark_node;
7572 /* Look for the optional `::' operator. */
7573 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7574 /* Look for the optional nested-name-specifier. */
7575 nested_name_specifier_p
7576 = (cp_parser_nested_name_specifier_opt (parser,
7577 /*typename_keyword_p=*/false,
7578 /*check_dependency_p=*/true,
7579 /*type_p=*/false,
7580 /*is_declaration=*/false)
7581 != NULL_TREE);
7582 /* Now, if we saw a nested-name-specifier, we might be doing the
7583 second production. */
7584 if (nested_name_specifier_p
7585 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7587 /* Consume the `template' keyword. */
7588 cp_lexer_consume_token (parser->lexer);
7589 /* Parse the template-id. */
7590 cp_parser_template_id (parser,
7591 /*template_keyword_p=*/true,
7592 /*check_dependency_p=*/false,
7593 class_type,
7594 /*is_declaration=*/true);
7595 /* Look for the `::' token. */
7596 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7598 /* If the next token is not a `~', then there might be some
7599 additional qualification. */
7600 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7602 /* At this point, we're looking for "type-name :: ~". The type-name
7603 must not be a class-name, since this is a pseudo-destructor. So,
7604 it must be either an enum-name, or a typedef-name -- both of which
7605 are just identifiers. So, we peek ahead to check that the "::"
7606 and "~" tokens are present; if they are not, then we can avoid
7607 calling type_name. */
7608 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7609 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7610 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7612 cp_parser_error (parser, "non-scalar type");
7613 return;
7616 /* Look for the type-name. */
7617 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7618 if (*scope == error_mark_node)
7619 return;
7621 /* Look for the `::' token. */
7622 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7624 else
7625 *scope = NULL_TREE;
7627 /* Look for the `~'. */
7628 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7630 /* Once we see the ~, this has to be a pseudo-destructor. */
7631 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7632 cp_parser_commit_to_topmost_tentative_parse (parser);
7634 /* Look for the type-name again. We are not responsible for
7635 checking that it matches the first type-name. */
7636 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7639 /* Parse a unary-expression.
7641 unary-expression:
7642 postfix-expression
7643 ++ cast-expression
7644 -- cast-expression
7645 unary-operator cast-expression
7646 sizeof unary-expression
7647 sizeof ( type-id )
7648 alignof ( type-id ) [C++0x]
7649 new-expression
7650 delete-expression
7652 GNU Extensions:
7654 unary-expression:
7655 __extension__ cast-expression
7656 __alignof__ unary-expression
7657 __alignof__ ( type-id )
7658 alignof unary-expression [C++0x]
7659 __real__ cast-expression
7660 __imag__ cast-expression
7661 && identifier
7662 sizeof ( type-id ) { initializer-list , [opt] }
7663 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7664 __alignof__ ( type-id ) { initializer-list , [opt] }
7666 ADDRESS_P is true iff the unary-expression is appearing as the
7667 operand of the `&' operator. CAST_P is true if this expression is
7668 the target of a cast.
7670 Returns a representation of the expression. */
7672 static cp_expr
7673 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7674 bool address_p, bool cast_p, bool decltype_p)
7676 cp_token *token;
7677 enum tree_code unary_operator;
7679 /* Peek at the next token. */
7680 token = cp_lexer_peek_token (parser->lexer);
7681 /* Some keywords give away the kind of expression. */
7682 if (token->type == CPP_KEYWORD)
7684 enum rid keyword = token->keyword;
7686 switch (keyword)
7688 case RID_ALIGNOF:
7689 case RID_SIZEOF:
7691 tree operand, ret;
7692 enum tree_code op;
7693 location_t first_loc;
7695 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7696 /* Consume the token. */
7697 cp_lexer_consume_token (parser->lexer);
7698 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7699 /* Parse the operand. */
7700 operand = cp_parser_sizeof_operand (parser, keyword);
7702 if (TYPE_P (operand))
7703 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7704 else
7706 /* ISO C++ defines alignof only with types, not with
7707 expressions. So pedwarn if alignof is used with a non-
7708 type expression. However, __alignof__ is ok. */
7709 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7710 pedwarn (token->location, OPT_Wpedantic,
7711 "ISO C++ does not allow %<alignof%> "
7712 "with a non-type");
7714 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7716 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7717 SIZEOF_EXPR with the original operand. */
7718 if (op == SIZEOF_EXPR && ret != error_mark_node)
7720 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7722 if (!processing_template_decl && TYPE_P (operand))
7724 ret = build_min (SIZEOF_EXPR, size_type_node,
7725 build1 (NOP_EXPR, operand,
7726 error_mark_node));
7727 SIZEOF_EXPR_TYPE_P (ret) = 1;
7729 else
7730 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7731 TREE_SIDE_EFFECTS (ret) = 0;
7732 TREE_READONLY (ret) = 1;
7734 SET_EXPR_LOCATION (ret, first_loc);
7736 return ret;
7739 case RID_NEW:
7740 return cp_parser_new_expression (parser);
7742 case RID_DELETE:
7743 return cp_parser_delete_expression (parser);
7745 case RID_EXTENSION:
7747 /* The saved value of the PEDANTIC flag. */
7748 int saved_pedantic;
7749 tree expr;
7751 /* Save away the PEDANTIC flag. */
7752 cp_parser_extension_opt (parser, &saved_pedantic);
7753 /* Parse the cast-expression. */
7754 expr = cp_parser_simple_cast_expression (parser);
7755 /* Restore the PEDANTIC flag. */
7756 pedantic = saved_pedantic;
7758 return expr;
7761 case RID_REALPART:
7762 case RID_IMAGPART:
7764 tree expression;
7766 /* Consume the `__real__' or `__imag__' token. */
7767 cp_lexer_consume_token (parser->lexer);
7768 /* Parse the cast-expression. */
7769 expression = cp_parser_simple_cast_expression (parser);
7770 /* Create the complete representation. */
7771 return build_x_unary_op (token->location,
7772 (keyword == RID_REALPART
7773 ? REALPART_EXPR : IMAGPART_EXPR),
7774 expression,
7775 tf_warning_or_error);
7777 break;
7779 case RID_TRANSACTION_ATOMIC:
7780 case RID_TRANSACTION_RELAXED:
7781 return cp_parser_transaction_expression (parser, keyword);
7783 case RID_NOEXCEPT:
7785 tree expr;
7786 const char *saved_message;
7787 bool saved_integral_constant_expression_p;
7788 bool saved_non_integral_constant_expression_p;
7789 bool saved_greater_than_is_operator_p;
7791 cp_lexer_consume_token (parser->lexer);
7792 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7794 saved_message = parser->type_definition_forbidden_message;
7795 parser->type_definition_forbidden_message
7796 = G_("types may not be defined in %<noexcept%> expressions");
7798 saved_integral_constant_expression_p
7799 = parser->integral_constant_expression_p;
7800 saved_non_integral_constant_expression_p
7801 = parser->non_integral_constant_expression_p;
7802 parser->integral_constant_expression_p = false;
7804 saved_greater_than_is_operator_p
7805 = parser->greater_than_is_operator_p;
7806 parser->greater_than_is_operator_p = true;
7808 ++cp_unevaluated_operand;
7809 ++c_inhibit_evaluation_warnings;
7810 ++cp_noexcept_operand;
7811 expr = cp_parser_expression (parser);
7812 --cp_noexcept_operand;
7813 --c_inhibit_evaluation_warnings;
7814 --cp_unevaluated_operand;
7816 parser->greater_than_is_operator_p
7817 = saved_greater_than_is_operator_p;
7819 parser->integral_constant_expression_p
7820 = saved_integral_constant_expression_p;
7821 parser->non_integral_constant_expression_p
7822 = saved_non_integral_constant_expression_p;
7824 parser->type_definition_forbidden_message = saved_message;
7826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7827 return finish_noexcept_expr (expr, tf_warning_or_error);
7830 default:
7831 break;
7835 /* Look for the `:: new' and `:: delete', which also signal the
7836 beginning of a new-expression, or delete-expression,
7837 respectively. If the next token is `::', then it might be one of
7838 these. */
7839 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7841 enum rid keyword;
7843 /* See if the token after the `::' is one of the keywords in
7844 which we're interested. */
7845 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7846 /* If it's `new', we have a new-expression. */
7847 if (keyword == RID_NEW)
7848 return cp_parser_new_expression (parser);
7849 /* Similarly, for `delete'. */
7850 else if (keyword == RID_DELETE)
7851 return cp_parser_delete_expression (parser);
7854 /* Look for a unary operator. */
7855 unary_operator = cp_parser_unary_operator (token);
7856 /* The `++' and `--' operators can be handled similarly, even though
7857 they are not technically unary-operators in the grammar. */
7858 if (unary_operator == ERROR_MARK)
7860 if (token->type == CPP_PLUS_PLUS)
7861 unary_operator = PREINCREMENT_EXPR;
7862 else if (token->type == CPP_MINUS_MINUS)
7863 unary_operator = PREDECREMENT_EXPR;
7864 /* Handle the GNU address-of-label extension. */
7865 else if (cp_parser_allow_gnu_extensions_p (parser)
7866 && token->type == CPP_AND_AND)
7868 tree identifier;
7869 tree expression;
7870 location_t start_loc = token->location;
7872 /* Consume the '&&' token. */
7873 cp_lexer_consume_token (parser->lexer);
7874 /* Look for the identifier. */
7875 location_t finish_loc
7876 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
7877 identifier = cp_parser_identifier (parser);
7878 /* Construct a location of the form:
7879 &&label
7880 ^~~~~~~
7881 with caret==start at the "&&", finish at the end of the label. */
7882 location_t combined_loc
7883 = make_location (start_loc, start_loc, finish_loc);
7884 /* Create an expression representing the address. */
7885 expression = finish_label_address_expr (identifier, combined_loc);
7886 if (cp_parser_non_integral_constant_expression (parser,
7887 NIC_ADDR_LABEL))
7888 expression = error_mark_node;
7889 return expression;
7892 if (unary_operator != ERROR_MARK)
7894 cp_expr cast_expression;
7895 cp_expr expression = error_mark_node;
7896 non_integral_constant non_constant_p = NIC_NONE;
7897 location_t loc = token->location;
7898 tsubst_flags_t complain = complain_flags (decltype_p);
7900 /* Consume the operator token. */
7901 token = cp_lexer_consume_token (parser->lexer);
7902 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
7904 /* Parse the cast-expression. */
7905 cast_expression
7906 = cp_parser_cast_expression (parser,
7907 unary_operator == ADDR_EXPR,
7908 /*cast_p=*/false,
7909 /*decltype*/false,
7910 pidk);
7912 /* Make a location:
7913 OP_TOKEN CAST_EXPRESSION
7914 ^~~~~~~~~~~~~~~~~~~~~~~~~
7915 with start==caret at the operator token, and
7916 extending to the end of the cast_expression. */
7917 loc = make_location (loc, loc, cast_expression.get_finish ());
7919 /* Now, build an appropriate representation. */
7920 switch (unary_operator)
7922 case INDIRECT_REF:
7923 non_constant_p = NIC_STAR;
7924 expression = build_x_indirect_ref (loc, cast_expression,
7925 RO_UNARY_STAR,
7926 complain);
7927 /* TODO: build_x_indirect_ref does not always honor the
7928 location, so ensure it is set. */
7929 expression.set_location (loc);
7930 break;
7932 case ADDR_EXPR:
7933 non_constant_p = NIC_ADDR;
7934 /* Fall through. */
7935 case BIT_NOT_EXPR:
7936 expression = build_x_unary_op (loc, unary_operator,
7937 cast_expression,
7938 complain);
7939 /* TODO: build_x_unary_op does not always honor the location,
7940 so ensure it is set. */
7941 expression.set_location (loc);
7942 break;
7944 case PREINCREMENT_EXPR:
7945 case PREDECREMENT_EXPR:
7946 non_constant_p = unary_operator == PREINCREMENT_EXPR
7947 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
7948 /* Fall through. */
7949 case NEGATE_EXPR:
7950 /* Immediately fold negation of a constant, unless the constant is 0
7951 (since -0 == 0) or it would overflow. */
7952 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
7953 && CONSTANT_CLASS_P (cast_expression)
7954 && !integer_zerop (cast_expression)
7955 && !TREE_OVERFLOW (cast_expression))
7957 tree folded = fold_build1 (unary_operator,
7958 TREE_TYPE (cast_expression),
7959 cast_expression);
7960 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
7962 expression = cp_expr (folded, loc);
7963 break;
7966 /* Fall through. */
7967 case UNARY_PLUS_EXPR:
7968 case TRUTH_NOT_EXPR:
7969 expression = finish_unary_op_expr (loc, unary_operator,
7970 cast_expression, complain);
7971 break;
7973 default:
7974 gcc_unreachable ();
7977 if (non_constant_p != NIC_NONE
7978 && cp_parser_non_integral_constant_expression (parser,
7979 non_constant_p))
7980 expression = error_mark_node;
7982 return expression;
7985 return cp_parser_postfix_expression (parser, address_p, cast_p,
7986 /*member_access_only_p=*/false,
7987 decltype_p,
7988 pidk);
7991 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
7992 unary-operator, the corresponding tree code is returned. */
7994 static enum tree_code
7995 cp_parser_unary_operator (cp_token* token)
7997 switch (token->type)
7999 case CPP_MULT:
8000 return INDIRECT_REF;
8002 case CPP_AND:
8003 return ADDR_EXPR;
8005 case CPP_PLUS:
8006 return UNARY_PLUS_EXPR;
8008 case CPP_MINUS:
8009 return NEGATE_EXPR;
8011 case CPP_NOT:
8012 return TRUTH_NOT_EXPR;
8014 case CPP_COMPL:
8015 return BIT_NOT_EXPR;
8017 default:
8018 return ERROR_MARK;
8022 /* Parse a new-expression.
8024 new-expression:
8025 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8026 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8028 Returns a representation of the expression. */
8030 static tree
8031 cp_parser_new_expression (cp_parser* parser)
8033 bool global_scope_p;
8034 vec<tree, va_gc> *placement;
8035 tree type;
8036 vec<tree, va_gc> *initializer;
8037 tree nelts = NULL_TREE;
8038 tree ret;
8040 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8042 /* Look for the optional `::' operator. */
8043 global_scope_p
8044 = (cp_parser_global_scope_opt (parser,
8045 /*current_scope_valid_p=*/false)
8046 != NULL_TREE);
8047 /* Look for the `new' operator. */
8048 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8049 /* There's no easy way to tell a new-placement from the
8050 `( type-id )' construct. */
8051 cp_parser_parse_tentatively (parser);
8052 /* Look for a new-placement. */
8053 placement = cp_parser_new_placement (parser);
8054 /* If that didn't work out, there's no new-placement. */
8055 if (!cp_parser_parse_definitely (parser))
8057 if (placement != NULL)
8058 release_tree_vector (placement);
8059 placement = NULL;
8062 /* If the next token is a `(', then we have a parenthesized
8063 type-id. */
8064 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8066 cp_token *token;
8067 const char *saved_message = parser->type_definition_forbidden_message;
8069 /* Consume the `('. */
8070 cp_lexer_consume_token (parser->lexer);
8072 /* Parse the type-id. */
8073 parser->type_definition_forbidden_message
8074 = G_("types may not be defined in a new-expression");
8076 type_id_in_expr_sentinel s (parser);
8077 type = cp_parser_type_id (parser);
8079 parser->type_definition_forbidden_message = saved_message;
8081 /* Look for the closing `)'. */
8082 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8083 token = cp_lexer_peek_token (parser->lexer);
8084 /* There should not be a direct-new-declarator in this production,
8085 but GCC used to allowed this, so we check and emit a sensible error
8086 message for this case. */
8087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8089 error_at (token->location,
8090 "array bound forbidden after parenthesized type-id");
8091 inform (token->location,
8092 "try removing the parentheses around the type-id");
8093 cp_parser_direct_new_declarator (parser);
8096 /* Otherwise, there must be a new-type-id. */
8097 else
8098 type = cp_parser_new_type_id (parser, &nelts);
8100 /* If the next token is a `(' or '{', then we have a new-initializer. */
8101 cp_token *token = cp_lexer_peek_token (parser->lexer);
8102 if (token->type == CPP_OPEN_PAREN
8103 || token->type == CPP_OPEN_BRACE)
8104 initializer = cp_parser_new_initializer (parser);
8105 else
8106 initializer = NULL;
8108 /* A new-expression may not appear in an integral constant
8109 expression. */
8110 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8111 ret = error_mark_node;
8112 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8113 of a new-type-id or type-id of a new-expression, the new-expression shall
8114 contain a new-initializer of the form ( assignment-expression )".
8115 Additionally, consistently with the spirit of DR 1467, we want to accept
8116 'new auto { 2 }' too. */
8117 else if (type_uses_auto (type)
8118 && (vec_safe_length (initializer) != 1
8119 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8120 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8122 error_at (token->location,
8123 "initialization of new-expression for type %<auto%> "
8124 "requires exactly one element");
8125 ret = error_mark_node;
8127 else
8129 /* Construct a location e.g.:
8130 ptr = new int[100]
8131 ^~~~~~~~~~~~
8132 with caret == start at the start of the "new" token, and the end
8133 at the end of the final token we consumed. */
8134 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8135 location_t end_loc = get_finish (end_tok->location);
8136 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8138 /* Create a representation of the new-expression. */
8139 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8140 tf_warning_or_error);
8141 protected_set_expr_location (ret, combined_loc);
8144 if (placement != NULL)
8145 release_tree_vector (placement);
8146 if (initializer != NULL)
8147 release_tree_vector (initializer);
8149 return ret;
8152 /* Parse a new-placement.
8154 new-placement:
8155 ( expression-list )
8157 Returns the same representation as for an expression-list. */
8159 static vec<tree, va_gc> *
8160 cp_parser_new_placement (cp_parser* parser)
8162 vec<tree, va_gc> *expression_list;
8164 /* Parse the expression-list. */
8165 expression_list = (cp_parser_parenthesized_expression_list
8166 (parser, non_attr, /*cast_p=*/false,
8167 /*allow_expansion_p=*/true,
8168 /*non_constant_p=*/NULL));
8170 if (expression_list && expression_list->is_empty ())
8171 error ("expected expression-list or type-id");
8173 return expression_list;
8176 /* Parse a new-type-id.
8178 new-type-id:
8179 type-specifier-seq new-declarator [opt]
8181 Returns the TYPE allocated. If the new-type-id indicates an array
8182 type, *NELTS is set to the number of elements in the last array
8183 bound; the TYPE will not include the last array bound. */
8185 static tree
8186 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8188 cp_decl_specifier_seq type_specifier_seq;
8189 cp_declarator *new_declarator;
8190 cp_declarator *declarator;
8191 cp_declarator *outer_declarator;
8192 const char *saved_message;
8194 /* The type-specifier sequence must not contain type definitions.
8195 (It cannot contain declarations of new types either, but if they
8196 are not definitions we will catch that because they are not
8197 complete.) */
8198 saved_message = parser->type_definition_forbidden_message;
8199 parser->type_definition_forbidden_message
8200 = G_("types may not be defined in a new-type-id");
8201 /* Parse the type-specifier-seq. */
8202 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8203 /*is_trailing_return=*/false,
8204 &type_specifier_seq);
8205 /* Restore the old message. */
8206 parser->type_definition_forbidden_message = saved_message;
8208 if (type_specifier_seq.type == error_mark_node)
8209 return error_mark_node;
8211 /* Parse the new-declarator. */
8212 new_declarator = cp_parser_new_declarator_opt (parser);
8214 /* Determine the number of elements in the last array dimension, if
8215 any. */
8216 *nelts = NULL_TREE;
8217 /* Skip down to the last array dimension. */
8218 declarator = new_declarator;
8219 outer_declarator = NULL;
8220 while (declarator && (declarator->kind == cdk_pointer
8221 || declarator->kind == cdk_ptrmem))
8223 outer_declarator = declarator;
8224 declarator = declarator->declarator;
8226 while (declarator
8227 && declarator->kind == cdk_array
8228 && declarator->declarator
8229 && declarator->declarator->kind == cdk_array)
8231 outer_declarator = declarator;
8232 declarator = declarator->declarator;
8235 if (declarator && declarator->kind == cdk_array)
8237 *nelts = declarator->u.array.bounds;
8238 if (*nelts == error_mark_node)
8239 *nelts = integer_one_node;
8241 if (outer_declarator)
8242 outer_declarator->declarator = declarator->declarator;
8243 else
8244 new_declarator = NULL;
8247 return groktypename (&type_specifier_seq, new_declarator, false);
8250 /* Parse an (optional) new-declarator.
8252 new-declarator:
8253 ptr-operator new-declarator [opt]
8254 direct-new-declarator
8256 Returns the declarator. */
8258 static cp_declarator *
8259 cp_parser_new_declarator_opt (cp_parser* parser)
8261 enum tree_code code;
8262 tree type, std_attributes = NULL_TREE;
8263 cp_cv_quals cv_quals;
8265 /* We don't know if there's a ptr-operator next, or not. */
8266 cp_parser_parse_tentatively (parser);
8267 /* Look for a ptr-operator. */
8268 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8269 /* If that worked, look for more new-declarators. */
8270 if (cp_parser_parse_definitely (parser))
8272 cp_declarator *declarator;
8274 /* Parse another optional declarator. */
8275 declarator = cp_parser_new_declarator_opt (parser);
8277 declarator = cp_parser_make_indirect_declarator
8278 (code, type, cv_quals, declarator, std_attributes);
8280 return declarator;
8283 /* If the next token is a `[', there is a direct-new-declarator. */
8284 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8285 return cp_parser_direct_new_declarator (parser);
8287 return NULL;
8290 /* Parse a direct-new-declarator.
8292 direct-new-declarator:
8293 [ expression ]
8294 direct-new-declarator [constant-expression]
8298 static cp_declarator *
8299 cp_parser_direct_new_declarator (cp_parser* parser)
8301 cp_declarator *declarator = NULL;
8303 while (true)
8305 tree expression;
8306 cp_token *token;
8308 /* Look for the opening `['. */
8309 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8311 token = cp_lexer_peek_token (parser->lexer);
8312 expression = cp_parser_expression (parser);
8313 /* The standard requires that the expression have integral
8314 type. DR 74 adds enumeration types. We believe that the
8315 real intent is that these expressions be handled like the
8316 expression in a `switch' condition, which also allows
8317 classes with a single conversion to integral or
8318 enumeration type. */
8319 if (!processing_template_decl)
8321 expression
8322 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8323 expression,
8324 /*complain=*/true);
8325 if (!expression)
8327 error_at (token->location,
8328 "expression in new-declarator must have integral "
8329 "or enumeration type");
8330 expression = error_mark_node;
8334 /* Look for the closing `]'. */
8335 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8337 /* Add this bound to the declarator. */
8338 declarator = make_array_declarator (declarator, expression);
8340 /* If the next token is not a `[', then there are no more
8341 bounds. */
8342 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8343 break;
8346 return declarator;
8349 /* Parse a new-initializer.
8351 new-initializer:
8352 ( expression-list [opt] )
8353 braced-init-list
8355 Returns a representation of the expression-list. */
8357 static vec<tree, va_gc> *
8358 cp_parser_new_initializer (cp_parser* parser)
8360 vec<tree, va_gc> *expression_list;
8362 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8364 tree t;
8365 bool expr_non_constant_p;
8366 cp_lexer_set_source_position (parser->lexer);
8367 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8368 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8369 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8370 expression_list = make_tree_vector_single (t);
8372 else
8373 expression_list = (cp_parser_parenthesized_expression_list
8374 (parser, non_attr, /*cast_p=*/false,
8375 /*allow_expansion_p=*/true,
8376 /*non_constant_p=*/NULL));
8378 return expression_list;
8381 /* Parse a delete-expression.
8383 delete-expression:
8384 :: [opt] delete cast-expression
8385 :: [opt] delete [ ] cast-expression
8387 Returns a representation of the expression. */
8389 static tree
8390 cp_parser_delete_expression (cp_parser* parser)
8392 bool global_scope_p;
8393 bool array_p;
8394 tree expression;
8396 /* Look for the optional `::' operator. */
8397 global_scope_p
8398 = (cp_parser_global_scope_opt (parser,
8399 /*current_scope_valid_p=*/false)
8400 != NULL_TREE);
8401 /* Look for the `delete' keyword. */
8402 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8403 /* See if the array syntax is in use. */
8404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8406 /* Consume the `[' token. */
8407 cp_lexer_consume_token (parser->lexer);
8408 /* Look for the `]' token. */
8409 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8410 /* Remember that this is the `[]' construct. */
8411 array_p = true;
8413 else
8414 array_p = false;
8416 /* Parse the cast-expression. */
8417 expression = cp_parser_simple_cast_expression (parser);
8419 /* A delete-expression may not appear in an integral constant
8420 expression. */
8421 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8422 return error_mark_node;
8424 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8425 tf_warning_or_error);
8428 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8429 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8430 0 otherwise. */
8432 static int
8433 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8435 cp_token *token = cp_lexer_peek_token (parser->lexer);
8436 switch (token->type)
8438 case CPP_COMMA:
8439 case CPP_SEMICOLON:
8440 case CPP_QUERY:
8441 case CPP_COLON:
8442 case CPP_CLOSE_SQUARE:
8443 case CPP_CLOSE_PAREN:
8444 case CPP_CLOSE_BRACE:
8445 case CPP_OPEN_BRACE:
8446 case CPP_DOT:
8447 case CPP_DOT_STAR:
8448 case CPP_DEREF:
8449 case CPP_DEREF_STAR:
8450 case CPP_DIV:
8451 case CPP_MOD:
8452 case CPP_LSHIFT:
8453 case CPP_RSHIFT:
8454 case CPP_LESS:
8455 case CPP_GREATER:
8456 case CPP_LESS_EQ:
8457 case CPP_GREATER_EQ:
8458 case CPP_EQ_EQ:
8459 case CPP_NOT_EQ:
8460 case CPP_EQ:
8461 case CPP_MULT_EQ:
8462 case CPP_DIV_EQ:
8463 case CPP_MOD_EQ:
8464 case CPP_PLUS_EQ:
8465 case CPP_MINUS_EQ:
8466 case CPP_RSHIFT_EQ:
8467 case CPP_LSHIFT_EQ:
8468 case CPP_AND_EQ:
8469 case CPP_XOR_EQ:
8470 case CPP_OR_EQ:
8471 case CPP_XOR:
8472 case CPP_OR:
8473 case CPP_OR_OR:
8474 case CPP_EOF:
8475 case CPP_ELLIPSIS:
8476 return 0;
8478 case CPP_OPEN_PAREN:
8479 /* In ((type ()) () the last () isn't a valid cast-expression,
8480 so the whole must be parsed as postfix-expression. */
8481 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8482 != CPP_CLOSE_PAREN;
8484 case CPP_OPEN_SQUARE:
8485 /* '[' may start a primary-expression in obj-c++ and in C++11,
8486 as a lambda-expression, eg, '(void)[]{}'. */
8487 if (cxx_dialect >= cxx11)
8488 return -1;
8489 return c_dialect_objc ();
8491 case CPP_PLUS_PLUS:
8492 case CPP_MINUS_MINUS:
8493 /* '++' and '--' may or may not start a cast-expression:
8495 struct T { void operator++(int); };
8496 void f() { (T())++; }
8500 int a;
8501 (int)++a; */
8502 return -1;
8504 default:
8505 return 1;
8509 /* Parse a cast-expression.
8511 cast-expression:
8512 unary-expression
8513 ( type-id ) cast-expression
8515 ADDRESS_P is true iff the unary-expression is appearing as the
8516 operand of the `&' operator. CAST_P is true if this expression is
8517 the target of a cast.
8519 Returns a representation of the expression. */
8521 static cp_expr
8522 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8523 bool decltype_p, cp_id_kind * pidk)
8525 /* If it's a `(', then we might be looking at a cast. */
8526 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8528 tree type = NULL_TREE;
8529 cp_expr expr (NULL_TREE);
8530 int cast_expression = 0;
8531 const char *saved_message;
8533 /* There's no way to know yet whether or not this is a cast.
8534 For example, `(int (3))' is a unary-expression, while `(int)
8535 3' is a cast. So, we resort to parsing tentatively. */
8536 cp_parser_parse_tentatively (parser);
8537 /* Types may not be defined in a cast. */
8538 saved_message = parser->type_definition_forbidden_message;
8539 parser->type_definition_forbidden_message
8540 = G_("types may not be defined in casts");
8541 /* Consume the `('. */
8542 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8543 location_t open_paren_loc = open_paren->location;
8545 /* A very tricky bit is that `(struct S) { 3 }' is a
8546 compound-literal (which we permit in C++ as an extension).
8547 But, that construct is not a cast-expression -- it is a
8548 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8549 is legal; if the compound-literal were a cast-expression,
8550 you'd need an extra set of parentheses.) But, if we parse
8551 the type-id, and it happens to be a class-specifier, then we
8552 will commit to the parse at that point, because we cannot
8553 undo the action that is done when creating a new class. So,
8554 then we cannot back up and do a postfix-expression.
8556 Another tricky case is the following (c++/29234):
8558 struct S { void operator () (); };
8560 void foo ()
8562 ( S()() );
8565 As a type-id we parse the parenthesized S()() as a function
8566 returning a function, groktypename complains and we cannot
8567 back up in this case either.
8569 Therefore, we scan ahead to the closing `)', and check to see
8570 if the tokens after the `)' can start a cast-expression. Otherwise
8571 we are dealing with an unary-expression, a postfix-expression
8572 or something else.
8574 Yet another tricky case, in C++11, is the following (c++/54891):
8576 (void)[]{};
8578 The issue is that usually, besides the case of lambda-expressions,
8579 the parenthesized type-id cannot be followed by '[', and, eg, we
8580 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8581 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8582 we don't commit, we try a cast-expression, then an unary-expression.
8584 Save tokens so that we can put them back. */
8585 cp_lexer_save_tokens (parser->lexer);
8587 /* We may be looking at a cast-expression. */
8588 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8589 /*consume_paren=*/true))
8590 cast_expression
8591 = cp_parser_tokens_start_cast_expression (parser);
8593 /* Roll back the tokens we skipped. */
8594 cp_lexer_rollback_tokens (parser->lexer);
8595 /* If we aren't looking at a cast-expression, simulate an error so
8596 that the call to cp_parser_error_occurred below returns true. */
8597 if (!cast_expression)
8598 cp_parser_simulate_error (parser);
8599 else
8601 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8602 parser->in_type_id_in_expr_p = true;
8603 /* Look for the type-id. */
8604 type = cp_parser_type_id (parser);
8605 /* Look for the closing `)'. */
8606 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8607 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8610 /* Restore the saved message. */
8611 parser->type_definition_forbidden_message = saved_message;
8613 /* At this point this can only be either a cast or a
8614 parenthesized ctor such as `(T ())' that looks like a cast to
8615 function returning T. */
8616 if (!cp_parser_error_occurred (parser))
8618 /* Only commit if the cast-expression doesn't start with
8619 '++', '--', or '[' in C++11. */
8620 if (cast_expression > 0)
8621 cp_parser_commit_to_topmost_tentative_parse (parser);
8623 expr = cp_parser_cast_expression (parser,
8624 /*address_p=*/false,
8625 /*cast_p=*/true,
8626 /*decltype_p=*/false,
8627 pidk);
8629 if (cp_parser_parse_definitely (parser))
8631 /* Warn about old-style casts, if so requested. */
8632 if (warn_old_style_cast
8633 && !in_system_header_at (input_location)
8634 && !VOID_TYPE_P (type)
8635 && current_lang_name != lang_name_c)
8636 warning (OPT_Wold_style_cast, "use of old-style cast");
8638 /* Only type conversions to integral or enumeration types
8639 can be used in constant-expressions. */
8640 if (!cast_valid_in_integral_constant_expression_p (type)
8641 && cp_parser_non_integral_constant_expression (parser,
8642 NIC_CAST))
8643 return error_mark_node;
8645 /* Perform the cast. */
8646 /* Make a location:
8647 (TYPE) EXPR
8648 ^~~~~~~~~~~
8649 with start==caret at the open paren, extending to the
8650 end of "expr". */
8651 location_t cast_loc = make_location (open_paren_loc,
8652 open_paren_loc,
8653 expr.get_finish ());
8654 expr = build_c_cast (cast_loc, type, expr);
8655 return expr;
8658 else
8659 cp_parser_abort_tentative_parse (parser);
8662 /* If we get here, then it's not a cast, so it must be a
8663 unary-expression. */
8664 return cp_parser_unary_expression (parser, pidk, address_p,
8665 cast_p, decltype_p);
8668 /* Parse a binary expression of the general form:
8670 pm-expression:
8671 cast-expression
8672 pm-expression .* cast-expression
8673 pm-expression ->* cast-expression
8675 multiplicative-expression:
8676 pm-expression
8677 multiplicative-expression * pm-expression
8678 multiplicative-expression / pm-expression
8679 multiplicative-expression % pm-expression
8681 additive-expression:
8682 multiplicative-expression
8683 additive-expression + multiplicative-expression
8684 additive-expression - multiplicative-expression
8686 shift-expression:
8687 additive-expression
8688 shift-expression << additive-expression
8689 shift-expression >> additive-expression
8691 relational-expression:
8692 shift-expression
8693 relational-expression < shift-expression
8694 relational-expression > shift-expression
8695 relational-expression <= shift-expression
8696 relational-expression >= shift-expression
8698 GNU Extension:
8700 relational-expression:
8701 relational-expression <? shift-expression
8702 relational-expression >? shift-expression
8704 equality-expression:
8705 relational-expression
8706 equality-expression == relational-expression
8707 equality-expression != relational-expression
8709 and-expression:
8710 equality-expression
8711 and-expression & equality-expression
8713 exclusive-or-expression:
8714 and-expression
8715 exclusive-or-expression ^ and-expression
8717 inclusive-or-expression:
8718 exclusive-or-expression
8719 inclusive-or-expression | exclusive-or-expression
8721 logical-and-expression:
8722 inclusive-or-expression
8723 logical-and-expression && inclusive-or-expression
8725 logical-or-expression:
8726 logical-and-expression
8727 logical-or-expression || logical-and-expression
8729 All these are implemented with a single function like:
8731 binary-expression:
8732 simple-cast-expression
8733 binary-expression <token> binary-expression
8735 CAST_P is true if this expression is the target of a cast.
8737 The binops_by_token map is used to get the tree codes for each <token> type.
8738 binary-expressions are associated according to a precedence table. */
8740 #define TOKEN_PRECEDENCE(token) \
8741 (((token->type == CPP_GREATER \
8742 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8743 && !parser->greater_than_is_operator_p) \
8744 ? PREC_NOT_OPERATOR \
8745 : binops_by_token[token->type].prec)
8747 static cp_expr
8748 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8749 bool no_toplevel_fold_p,
8750 bool decltype_p,
8751 enum cp_parser_prec prec,
8752 cp_id_kind * pidk)
8754 cp_parser_expression_stack stack;
8755 cp_parser_expression_stack_entry *sp = &stack[0];
8756 cp_parser_expression_stack_entry current;
8757 cp_expr rhs;
8758 cp_token *token;
8759 enum tree_code rhs_type;
8760 enum cp_parser_prec new_prec, lookahead_prec;
8761 tree overload;
8763 /* Parse the first expression. */
8764 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8765 ? TRUTH_NOT_EXPR : ERROR_MARK);
8766 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8767 cast_p, decltype_p, pidk);
8768 current.prec = prec;
8770 if (cp_parser_error_occurred (parser))
8771 return error_mark_node;
8773 for (;;)
8775 /* Get an operator token. */
8776 token = cp_lexer_peek_token (parser->lexer);
8778 if (warn_cxx11_compat
8779 && token->type == CPP_RSHIFT
8780 && !parser->greater_than_is_operator_p)
8782 if (warning_at (token->location, OPT_Wc__11_compat,
8783 "%<>>%> operator is treated"
8784 " as two right angle brackets in C++11"))
8785 inform (token->location,
8786 "suggest parentheses around %<>>%> expression");
8789 new_prec = TOKEN_PRECEDENCE (token);
8790 if (new_prec != PREC_NOT_OPERATOR
8791 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8792 /* This is a fold-expression; handle it later. */
8793 new_prec = PREC_NOT_OPERATOR;
8795 /* Popping an entry off the stack means we completed a subexpression:
8796 - either we found a token which is not an operator (`>' where it is not
8797 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8798 will happen repeatedly;
8799 - or, we found an operator which has lower priority. This is the case
8800 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8801 parsing `3 * 4'. */
8802 if (new_prec <= current.prec)
8804 if (sp == stack)
8805 break;
8806 else
8807 goto pop;
8810 get_rhs:
8811 current.tree_type = binops_by_token[token->type].tree_type;
8812 current.loc = token->location;
8814 /* We used the operator token. */
8815 cp_lexer_consume_token (parser->lexer);
8817 /* For "false && x" or "true || x", x will never be executed;
8818 disable warnings while evaluating it. */
8819 if (current.tree_type == TRUTH_ANDIF_EXPR)
8820 c_inhibit_evaluation_warnings +=
8821 cp_fully_fold (current.lhs) == truthvalue_false_node;
8822 else if (current.tree_type == TRUTH_ORIF_EXPR)
8823 c_inhibit_evaluation_warnings +=
8824 cp_fully_fold (current.lhs) == truthvalue_true_node;
8826 /* Extract another operand. It may be the RHS of this expression
8827 or the LHS of a new, higher priority expression. */
8828 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8829 ? TRUTH_NOT_EXPR : ERROR_MARK);
8830 rhs = cp_parser_simple_cast_expression (parser);
8832 /* Get another operator token. Look up its precedence to avoid
8833 building a useless (immediately popped) stack entry for common
8834 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8835 token = cp_lexer_peek_token (parser->lexer);
8836 lookahead_prec = TOKEN_PRECEDENCE (token);
8837 if (lookahead_prec != PREC_NOT_OPERATOR
8838 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8839 lookahead_prec = PREC_NOT_OPERATOR;
8840 if (lookahead_prec > new_prec)
8842 /* ... and prepare to parse the RHS of the new, higher priority
8843 expression. Since precedence levels on the stack are
8844 monotonically increasing, we do not have to care about
8845 stack overflows. */
8846 *sp = current;
8847 ++sp;
8848 current.lhs = rhs;
8849 current.lhs_type = rhs_type;
8850 current.prec = new_prec;
8851 new_prec = lookahead_prec;
8852 goto get_rhs;
8854 pop:
8855 lookahead_prec = new_prec;
8856 /* If the stack is not empty, we have parsed into LHS the right side
8857 (`4' in the example above) of an expression we had suspended.
8858 We can use the information on the stack to recover the LHS (`3')
8859 from the stack together with the tree code (`MULT_EXPR'), and
8860 the precedence of the higher level subexpression
8861 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8862 which will be used to actually build the additive expression. */
8863 rhs = current.lhs;
8864 rhs_type = current.lhs_type;
8865 --sp;
8866 current = *sp;
8869 /* Undo the disabling of warnings done above. */
8870 if (current.tree_type == TRUTH_ANDIF_EXPR)
8871 c_inhibit_evaluation_warnings -=
8872 cp_fully_fold (current.lhs) == truthvalue_false_node;
8873 else if (current.tree_type == TRUTH_ORIF_EXPR)
8874 c_inhibit_evaluation_warnings -=
8875 cp_fully_fold (current.lhs) == truthvalue_true_node;
8877 if (warn_logical_not_paren
8878 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
8879 && current.lhs_type == TRUTH_NOT_EXPR
8880 /* Avoid warning for !!x == y. */
8881 && (TREE_CODE (current.lhs) != NE_EXPR
8882 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
8883 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
8884 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
8885 /* Avoid warning for !b == y where b is boolean. */
8886 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
8887 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
8888 != BOOLEAN_TYPE))))
8889 /* Avoid warning for !!b == y where b is boolean. */
8890 && (!DECL_P (current.lhs)
8891 || TREE_TYPE (current.lhs) == NULL_TREE
8892 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
8893 warn_logical_not_parentheses (current.loc, current.tree_type,
8894 maybe_constant_value (rhs));
8896 overload = NULL;
8898 location_t combined_loc = make_location (current.loc,
8899 current.lhs.get_start (),
8900 rhs.get_finish ());
8902 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
8903 ERROR_MARK for everything that is not a binary expression.
8904 This makes warn_about_parentheses miss some warnings that
8905 involve unary operators. For unary expressions we should
8906 pass the correct tree_code unless the unary expression was
8907 surrounded by parentheses.
8909 if (no_toplevel_fold_p
8910 && lookahead_prec <= current.prec
8911 && sp == stack)
8912 current.lhs = build2_loc (combined_loc,
8913 current.tree_type,
8914 TREE_CODE_CLASS (current.tree_type)
8915 == tcc_comparison
8916 ? boolean_type_node : TREE_TYPE (current.lhs),
8917 current.lhs, rhs);
8918 else
8920 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
8921 current.lhs, current.lhs_type,
8922 rhs, rhs_type, &overload,
8923 complain_flags (decltype_p));
8924 /* TODO: build_x_binary_op doesn't always honor the location. */
8925 current.lhs.set_location (combined_loc);
8927 current.lhs_type = current.tree_type;
8929 /* If the binary operator required the use of an overloaded operator,
8930 then this expression cannot be an integral constant-expression.
8931 An overloaded operator can be used even if both operands are
8932 otherwise permissible in an integral constant-expression if at
8933 least one of the operands is of enumeration type. */
8935 if (overload
8936 && cp_parser_non_integral_constant_expression (parser,
8937 NIC_OVERLOADED))
8938 return error_mark_node;
8941 return current.lhs;
8944 static cp_expr
8945 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8946 bool no_toplevel_fold_p,
8947 enum cp_parser_prec prec,
8948 cp_id_kind * pidk)
8950 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
8951 /*decltype*/false, prec, pidk);
8954 /* Parse the `? expression : assignment-expression' part of a
8955 conditional-expression. The LOGICAL_OR_EXPR is the
8956 logical-or-expression that started the conditional-expression.
8957 Returns a representation of the entire conditional-expression.
8959 This routine is used by cp_parser_assignment_expression.
8961 ? expression : assignment-expression
8963 GNU Extensions:
8965 ? : assignment-expression */
8967 static tree
8968 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
8970 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
8971 cp_expr assignment_expr;
8972 struct cp_token *token;
8973 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8975 /* Consume the `?' token. */
8976 cp_lexer_consume_token (parser->lexer);
8977 token = cp_lexer_peek_token (parser->lexer);
8978 if (cp_parser_allow_gnu_extensions_p (parser)
8979 && token->type == CPP_COLON)
8981 pedwarn (token->location, OPT_Wpedantic,
8982 "ISO C++ does not allow ?: with omitted middle operand");
8983 /* Implicit true clause. */
8984 expr = NULL_TREE;
8985 c_inhibit_evaluation_warnings +=
8986 folded_logical_or_expr == truthvalue_true_node;
8987 warn_for_omitted_condop (token->location, logical_or_expr);
8989 else
8991 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8992 parser->colon_corrects_to_scope_p = false;
8993 /* Parse the expression. */
8994 c_inhibit_evaluation_warnings +=
8995 folded_logical_or_expr == truthvalue_false_node;
8996 expr = cp_parser_expression (parser);
8997 c_inhibit_evaluation_warnings +=
8998 ((folded_logical_or_expr == truthvalue_true_node)
8999 - (folded_logical_or_expr == truthvalue_false_node));
9000 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9003 /* The next token should be a `:'. */
9004 cp_parser_require (parser, CPP_COLON, RT_COLON);
9005 /* Parse the assignment-expression. */
9006 assignment_expr = cp_parser_assignment_expression (parser);
9007 c_inhibit_evaluation_warnings -=
9008 folded_logical_or_expr == truthvalue_true_node;
9010 /* Make a location:
9011 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9012 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9013 with the caret at the "?", ranging from the start of
9014 the logical_or_expr to the end of the assignment_expr. */
9015 loc = make_location (loc,
9016 logical_or_expr.get_start (),
9017 assignment_expr.get_finish ());
9019 /* Build the conditional-expression. */
9020 return build_x_conditional_expr (loc, logical_or_expr,
9021 expr,
9022 assignment_expr,
9023 tf_warning_or_error);
9026 /* Parse an assignment-expression.
9028 assignment-expression:
9029 conditional-expression
9030 logical-or-expression assignment-operator assignment_expression
9031 throw-expression
9033 CAST_P is true if this expression is the target of a cast.
9034 DECLTYPE_P is true if this expression is the operand of decltype.
9036 Returns a representation for the expression. */
9038 static cp_expr
9039 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9040 bool cast_p, bool decltype_p)
9042 cp_expr expr;
9044 /* If the next token is the `throw' keyword, then we're looking at
9045 a throw-expression. */
9046 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9047 expr = cp_parser_throw_expression (parser);
9048 /* Otherwise, it must be that we are looking at a
9049 logical-or-expression. */
9050 else
9052 /* Parse the binary expressions (logical-or-expression). */
9053 expr = cp_parser_binary_expression (parser, cast_p, false,
9054 decltype_p,
9055 PREC_NOT_OPERATOR, pidk);
9056 /* If the next token is a `?' then we're actually looking at a
9057 conditional-expression. */
9058 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9059 return cp_parser_question_colon_clause (parser, expr);
9060 else
9062 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9064 /* If it's an assignment-operator, we're using the second
9065 production. */
9066 enum tree_code assignment_operator
9067 = cp_parser_assignment_operator_opt (parser);
9068 if (assignment_operator != ERROR_MARK)
9070 bool non_constant_p;
9072 /* Parse the right-hand side of the assignment. */
9073 cp_expr rhs = cp_parser_initializer_clause (parser,
9074 &non_constant_p);
9076 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9077 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9079 /* An assignment may not appear in a
9080 constant-expression. */
9081 if (cp_parser_non_integral_constant_expression (parser,
9082 NIC_ASSIGNMENT))
9083 return error_mark_node;
9084 /* Build the assignment expression. Its default
9085 location:
9086 LHS = RHS
9087 ~~~~^~~~~
9088 is the location of the '=' token as the
9089 caret, ranging from the start of the lhs to the
9090 end of the rhs. */
9091 loc = make_location (loc,
9092 expr.get_start (),
9093 rhs.get_finish ());
9094 expr = build_x_modify_expr (loc, expr,
9095 assignment_operator,
9096 rhs,
9097 complain_flags (decltype_p));
9098 /* TODO: build_x_modify_expr doesn't honor the location,
9099 so we must set it here. */
9100 expr.set_location (loc);
9105 return expr;
9108 /* Parse an (optional) assignment-operator.
9110 assignment-operator: one of
9111 = *= /= %= += -= >>= <<= &= ^= |=
9113 GNU Extension:
9115 assignment-operator: one of
9116 <?= >?=
9118 If the next token is an assignment operator, the corresponding tree
9119 code is returned, and the token is consumed. For example, for
9120 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9121 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9122 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9123 operator, ERROR_MARK is returned. */
9125 static enum tree_code
9126 cp_parser_assignment_operator_opt (cp_parser* parser)
9128 enum tree_code op;
9129 cp_token *token;
9131 /* Peek at the next token. */
9132 token = cp_lexer_peek_token (parser->lexer);
9134 switch (token->type)
9136 case CPP_EQ:
9137 op = NOP_EXPR;
9138 break;
9140 case CPP_MULT_EQ:
9141 op = MULT_EXPR;
9142 break;
9144 case CPP_DIV_EQ:
9145 op = TRUNC_DIV_EXPR;
9146 break;
9148 case CPP_MOD_EQ:
9149 op = TRUNC_MOD_EXPR;
9150 break;
9152 case CPP_PLUS_EQ:
9153 op = PLUS_EXPR;
9154 break;
9156 case CPP_MINUS_EQ:
9157 op = MINUS_EXPR;
9158 break;
9160 case CPP_RSHIFT_EQ:
9161 op = RSHIFT_EXPR;
9162 break;
9164 case CPP_LSHIFT_EQ:
9165 op = LSHIFT_EXPR;
9166 break;
9168 case CPP_AND_EQ:
9169 op = BIT_AND_EXPR;
9170 break;
9172 case CPP_XOR_EQ:
9173 op = BIT_XOR_EXPR;
9174 break;
9176 case CPP_OR_EQ:
9177 op = BIT_IOR_EXPR;
9178 break;
9180 default:
9181 /* Nothing else is an assignment operator. */
9182 op = ERROR_MARK;
9185 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9186 if (op != ERROR_MARK
9187 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9188 op = ERROR_MARK;
9190 /* If it was an assignment operator, consume it. */
9191 if (op != ERROR_MARK)
9192 cp_lexer_consume_token (parser->lexer);
9194 return op;
9197 /* Parse an expression.
9199 expression:
9200 assignment-expression
9201 expression , assignment-expression
9203 CAST_P is true if this expression is the target of a cast.
9204 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9205 except possibly parenthesized or on the RHS of a comma (N3276).
9207 Returns a representation of the expression. */
9209 static cp_expr
9210 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9211 bool cast_p, bool decltype_p)
9213 cp_expr expression = NULL_TREE;
9214 location_t loc = UNKNOWN_LOCATION;
9216 while (true)
9218 cp_expr assignment_expression;
9220 /* Parse the next assignment-expression. */
9221 assignment_expression
9222 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9224 /* We don't create a temporary for a call that is the immediate operand
9225 of decltype or on the RHS of a comma. But when we see a comma, we
9226 need to create a temporary for a call on the LHS. */
9227 if (decltype_p && !processing_template_decl
9228 && TREE_CODE (assignment_expression) == CALL_EXPR
9229 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9230 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9231 assignment_expression
9232 = build_cplus_new (TREE_TYPE (assignment_expression),
9233 assignment_expression, tf_warning_or_error);
9235 /* If this is the first assignment-expression, we can just
9236 save it away. */
9237 if (!expression)
9238 expression = assignment_expression;
9239 else
9241 /* Create a location with caret at the comma, ranging
9242 from the start of the LHS to the end of the RHS. */
9243 loc = make_location (loc,
9244 expression.get_start (),
9245 assignment_expression.get_finish ());
9246 expression = build_x_compound_expr (loc, expression,
9247 assignment_expression,
9248 complain_flags (decltype_p));
9249 expression.set_location (loc);
9251 /* If the next token is not a comma, or we're in a fold-expression, then
9252 we are done with the expression. */
9253 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9254 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9255 break;
9256 /* Consume the `,'. */
9257 loc = cp_lexer_peek_token (parser->lexer)->location;
9258 cp_lexer_consume_token (parser->lexer);
9259 /* A comma operator cannot appear in a constant-expression. */
9260 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9261 expression = error_mark_node;
9264 return expression;
9267 /* Parse a constant-expression.
9269 constant-expression:
9270 conditional-expression
9272 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9273 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9274 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9275 is false, NON_CONSTANT_P should be NULL. */
9277 static cp_expr
9278 cp_parser_constant_expression (cp_parser* parser,
9279 bool allow_non_constant_p,
9280 bool *non_constant_p)
9282 bool saved_integral_constant_expression_p;
9283 bool saved_allow_non_integral_constant_expression_p;
9284 bool saved_non_integral_constant_expression_p;
9285 cp_expr expression;
9287 /* It might seem that we could simply parse the
9288 conditional-expression, and then check to see if it were
9289 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9290 one that the compiler can figure out is constant, possibly after
9291 doing some simplifications or optimizations. The standard has a
9292 precise definition of constant-expression, and we must honor
9293 that, even though it is somewhat more restrictive.
9295 For example:
9297 int i[(2, 3)];
9299 is not a legal declaration, because `(2, 3)' is not a
9300 constant-expression. The `,' operator is forbidden in a
9301 constant-expression. However, GCC's constant-folding machinery
9302 will fold this operation to an INTEGER_CST for `3'. */
9304 /* Save the old settings. */
9305 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9306 saved_allow_non_integral_constant_expression_p
9307 = parser->allow_non_integral_constant_expression_p;
9308 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9309 /* We are now parsing a constant-expression. */
9310 parser->integral_constant_expression_p = true;
9311 parser->allow_non_integral_constant_expression_p
9312 = (allow_non_constant_p || cxx_dialect >= cxx11);
9313 parser->non_integral_constant_expression_p = false;
9314 /* Although the grammar says "conditional-expression", we parse an
9315 "assignment-expression", which also permits "throw-expression"
9316 and the use of assignment operators. In the case that
9317 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9318 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9319 actually essential that we look for an assignment-expression.
9320 For example, cp_parser_initializer_clauses uses this function to
9321 determine whether a particular assignment-expression is in fact
9322 constant. */
9323 expression = cp_parser_assignment_expression (parser);
9324 /* Restore the old settings. */
9325 parser->integral_constant_expression_p
9326 = saved_integral_constant_expression_p;
9327 parser->allow_non_integral_constant_expression_p
9328 = saved_allow_non_integral_constant_expression_p;
9329 if (cxx_dialect >= cxx11)
9331 /* Require an rvalue constant expression here; that's what our
9332 callers expect. Reference constant expressions are handled
9333 separately in e.g. cp_parser_template_argument. */
9334 bool is_const = potential_rvalue_constant_expression (expression);
9335 parser->non_integral_constant_expression_p = !is_const;
9336 if (!is_const && !allow_non_constant_p)
9337 require_potential_rvalue_constant_expression (expression);
9339 if (allow_non_constant_p)
9340 *non_constant_p = parser->non_integral_constant_expression_p;
9341 parser->non_integral_constant_expression_p
9342 = saved_non_integral_constant_expression_p;
9344 return expression;
9347 /* Parse __builtin_offsetof.
9349 offsetof-expression:
9350 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9352 offsetof-member-designator:
9353 id-expression
9354 | offsetof-member-designator "." id-expression
9355 | offsetof-member-designator "[" expression "]"
9356 | offsetof-member-designator "->" id-expression */
9358 static cp_expr
9359 cp_parser_builtin_offsetof (cp_parser *parser)
9361 int save_ice_p, save_non_ice_p;
9362 tree type;
9363 cp_expr expr;
9364 cp_id_kind dummy;
9365 cp_token *token;
9366 location_t finish_loc;
9368 /* We're about to accept non-integral-constant things, but will
9369 definitely yield an integral constant expression. Save and
9370 restore these values around our local parsing. */
9371 save_ice_p = parser->integral_constant_expression_p;
9372 save_non_ice_p = parser->non_integral_constant_expression_p;
9374 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9376 /* Consume the "__builtin_offsetof" token. */
9377 cp_lexer_consume_token (parser->lexer);
9378 /* Consume the opening `('. */
9379 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9380 /* Parse the type-id. */
9381 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9382 type = cp_parser_type_id (parser);
9383 /* Look for the `,'. */
9384 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9385 token = cp_lexer_peek_token (parser->lexer);
9387 /* Build the (type *)null that begins the traditional offsetof macro. */
9388 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
9389 tf_warning_or_error);
9391 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9392 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
9393 true, &dummy, token->location);
9394 while (true)
9396 token = cp_lexer_peek_token (parser->lexer);
9397 switch (token->type)
9399 case CPP_OPEN_SQUARE:
9400 /* offsetof-member-designator "[" expression "]" */
9401 expr = cp_parser_postfix_open_square_expression (parser, expr,
9402 true, false);
9403 break;
9405 case CPP_DEREF:
9406 /* offsetof-member-designator "->" identifier */
9407 expr = grok_array_decl (token->location, expr,
9408 integer_zero_node, false);
9409 /* FALLTHRU */
9411 case CPP_DOT:
9412 /* offsetof-member-designator "." identifier */
9413 cp_lexer_consume_token (parser->lexer);
9414 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9415 expr, true, &dummy,
9416 token->location);
9417 break;
9419 case CPP_CLOSE_PAREN:
9420 /* Consume the ")" token. */
9421 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9422 cp_lexer_consume_token (parser->lexer);
9423 goto success;
9425 default:
9426 /* Error. We know the following require will fail, but
9427 that gives the proper error message. */
9428 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9429 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9430 expr = error_mark_node;
9431 goto failure;
9435 success:
9436 /* Make a location of the form:
9437 __builtin_offsetof (struct s, f)
9438 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9439 with caret at the type-id, ranging from the start of the
9440 "_builtin_offsetof" token to the close paren. */
9441 loc = make_location (loc, start_loc, finish_loc);
9442 /* The result will be an INTEGER_CST, so we need to explicitly
9443 preserve the location. */
9444 expr = cp_expr (finish_offsetof (expr, loc), loc);
9446 failure:
9447 parser->integral_constant_expression_p = save_ice_p;
9448 parser->non_integral_constant_expression_p = save_non_ice_p;
9450 return expr;
9453 /* Parse a trait expression.
9455 Returns a representation of the expression, the underlying type
9456 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9458 static tree
9459 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9461 cp_trait_kind kind;
9462 tree type1, type2 = NULL_TREE;
9463 bool binary = false;
9464 bool variadic = false;
9466 switch (keyword)
9468 case RID_HAS_NOTHROW_ASSIGN:
9469 kind = CPTK_HAS_NOTHROW_ASSIGN;
9470 break;
9471 case RID_HAS_NOTHROW_CONSTRUCTOR:
9472 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9473 break;
9474 case RID_HAS_NOTHROW_COPY:
9475 kind = CPTK_HAS_NOTHROW_COPY;
9476 break;
9477 case RID_HAS_TRIVIAL_ASSIGN:
9478 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9479 break;
9480 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9481 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9482 break;
9483 case RID_HAS_TRIVIAL_COPY:
9484 kind = CPTK_HAS_TRIVIAL_COPY;
9485 break;
9486 case RID_HAS_TRIVIAL_DESTRUCTOR:
9487 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9488 break;
9489 case RID_HAS_VIRTUAL_DESTRUCTOR:
9490 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9491 break;
9492 case RID_IS_ABSTRACT:
9493 kind = CPTK_IS_ABSTRACT;
9494 break;
9495 case RID_IS_BASE_OF:
9496 kind = CPTK_IS_BASE_OF;
9497 binary = true;
9498 break;
9499 case RID_IS_CLASS:
9500 kind = CPTK_IS_CLASS;
9501 break;
9502 case RID_IS_EMPTY:
9503 kind = CPTK_IS_EMPTY;
9504 break;
9505 case RID_IS_ENUM:
9506 kind = CPTK_IS_ENUM;
9507 break;
9508 case RID_IS_FINAL:
9509 kind = CPTK_IS_FINAL;
9510 break;
9511 case RID_IS_LITERAL_TYPE:
9512 kind = CPTK_IS_LITERAL_TYPE;
9513 break;
9514 case RID_IS_POD:
9515 kind = CPTK_IS_POD;
9516 break;
9517 case RID_IS_POLYMORPHIC:
9518 kind = CPTK_IS_POLYMORPHIC;
9519 break;
9520 case RID_IS_SAME_AS:
9521 kind = CPTK_IS_SAME_AS;
9522 binary = true;
9523 break;
9524 case RID_IS_STD_LAYOUT:
9525 kind = CPTK_IS_STD_LAYOUT;
9526 break;
9527 case RID_IS_TRIVIAL:
9528 kind = CPTK_IS_TRIVIAL;
9529 break;
9530 case RID_IS_TRIVIALLY_ASSIGNABLE:
9531 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9532 binary = true;
9533 break;
9534 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9535 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9536 variadic = true;
9537 break;
9538 case RID_IS_TRIVIALLY_COPYABLE:
9539 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9540 break;
9541 case RID_IS_UNION:
9542 kind = CPTK_IS_UNION;
9543 break;
9544 case RID_UNDERLYING_TYPE:
9545 kind = CPTK_UNDERLYING_TYPE;
9546 break;
9547 case RID_BASES:
9548 kind = CPTK_BASES;
9549 break;
9550 case RID_DIRECT_BASES:
9551 kind = CPTK_DIRECT_BASES;
9552 break;
9553 default:
9554 gcc_unreachable ();
9557 /* Consume the token. */
9558 cp_lexer_consume_token (parser->lexer);
9560 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9563 type_id_in_expr_sentinel s (parser);
9564 type1 = cp_parser_type_id (parser);
9567 if (type1 == error_mark_node)
9568 return error_mark_node;
9570 if (binary)
9572 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9575 type_id_in_expr_sentinel s (parser);
9576 type2 = cp_parser_type_id (parser);
9579 if (type2 == error_mark_node)
9580 return error_mark_node;
9582 else if (variadic)
9584 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9586 cp_lexer_consume_token (parser->lexer);
9587 tree elt = cp_parser_type_id (parser);
9588 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9590 cp_lexer_consume_token (parser->lexer);
9591 elt = make_pack_expansion (elt);
9593 if (elt == error_mark_node)
9594 return error_mark_node;
9595 type2 = tree_cons (NULL_TREE, elt, type2);
9599 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9601 /* Complete the trait expression, which may mean either processing
9602 the trait expr now or saving it for template instantiation. */
9603 switch(kind)
9605 case CPTK_UNDERLYING_TYPE:
9606 return finish_underlying_type (type1);
9607 case CPTK_BASES:
9608 return finish_bases (type1, false);
9609 case CPTK_DIRECT_BASES:
9610 return finish_bases (type1, true);
9611 default:
9612 return finish_trait_expr (kind, type1, type2);
9616 /* Lambdas that appear in variable initializer or default argument scope
9617 get that in their mangling, so we need to record it. We might as well
9618 use the count for function and namespace scopes as well. */
9619 static GTY(()) tree lambda_scope;
9620 static GTY(()) int lambda_count;
9621 struct GTY(()) tree_int
9623 tree t;
9624 int i;
9626 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9628 static void
9629 start_lambda_scope (tree decl)
9631 tree_int ti;
9632 gcc_assert (decl);
9633 /* Once we're inside a function, we ignore other scopes and just push
9634 the function again so that popping works properly. */
9635 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9636 decl = current_function_decl;
9637 ti.t = lambda_scope;
9638 ti.i = lambda_count;
9639 vec_safe_push (lambda_scope_stack, ti);
9640 if (lambda_scope != decl)
9642 /* Don't reset the count if we're still in the same function. */
9643 lambda_scope = decl;
9644 lambda_count = 0;
9648 static void
9649 record_lambda_scope (tree lambda)
9651 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9652 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9655 static void
9656 finish_lambda_scope (void)
9658 tree_int *p = &lambda_scope_stack->last ();
9659 if (lambda_scope != p->t)
9661 lambda_scope = p->t;
9662 lambda_count = p->i;
9664 lambda_scope_stack->pop ();
9667 /* Parse a lambda expression.
9669 lambda-expression:
9670 lambda-introducer lambda-declarator [opt] compound-statement
9672 Returns a representation of the expression. */
9674 static cp_expr
9675 cp_parser_lambda_expression (cp_parser* parser)
9677 tree lambda_expr = build_lambda_expr ();
9678 tree type;
9679 bool ok = true;
9680 cp_token *token = cp_lexer_peek_token (parser->lexer);
9681 cp_token_position start = 0;
9683 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9685 if (cp_unevaluated_operand)
9687 if (!token->error_reported)
9689 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9690 "lambda-expression in unevaluated context");
9691 token->error_reported = true;
9693 ok = false;
9695 else if (parser->in_template_argument_list_p)
9697 if (!token->error_reported)
9699 error_at (token->location, "lambda-expression in template-argument");
9700 token->error_reported = true;
9702 ok = false;
9705 /* We may be in the middle of deferred access check. Disable
9706 it now. */
9707 push_deferring_access_checks (dk_no_deferred);
9709 cp_parser_lambda_introducer (parser, lambda_expr);
9711 type = begin_lambda_type (lambda_expr);
9712 if (type == error_mark_node)
9713 return error_mark_node;
9715 record_lambda_scope (lambda_expr);
9717 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9718 determine_visibility (TYPE_NAME (type));
9720 /* Now that we've started the type, add the capture fields for any
9721 explicit captures. */
9722 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9725 /* Inside the class, surrounding template-parameter-lists do not apply. */
9726 unsigned int saved_num_template_parameter_lists
9727 = parser->num_template_parameter_lists;
9728 unsigned char in_statement = parser->in_statement;
9729 bool in_switch_statement_p = parser->in_switch_statement_p;
9730 bool fully_implicit_function_template_p
9731 = parser->fully_implicit_function_template_p;
9732 tree implicit_template_parms = parser->implicit_template_parms;
9733 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9734 bool auto_is_implicit_function_template_parm_p
9735 = parser->auto_is_implicit_function_template_parm_p;
9737 parser->num_template_parameter_lists = 0;
9738 parser->in_statement = 0;
9739 parser->in_switch_statement_p = false;
9740 parser->fully_implicit_function_template_p = false;
9741 parser->implicit_template_parms = 0;
9742 parser->implicit_template_scope = 0;
9743 parser->auto_is_implicit_function_template_parm_p = false;
9745 /* By virtue of defining a local class, a lambda expression has access to
9746 the private variables of enclosing classes. */
9748 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9750 if (ok)
9752 if (!cp_parser_error_occurred (parser)
9753 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9754 && cp_parser_start_tentative_firewall (parser))
9755 start = token;
9756 cp_parser_lambda_body (parser, lambda_expr);
9758 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9760 if (cp_parser_skip_to_closing_brace (parser))
9761 cp_lexer_consume_token (parser->lexer);
9764 /* The capture list was built up in reverse order; fix that now. */
9765 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9766 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9768 if (ok)
9769 maybe_add_lambda_conv_op (type);
9771 type = finish_struct (type, /*attributes=*/NULL_TREE);
9773 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9774 parser->in_statement = in_statement;
9775 parser->in_switch_statement_p = in_switch_statement_p;
9776 parser->fully_implicit_function_template_p
9777 = fully_implicit_function_template_p;
9778 parser->implicit_template_parms = implicit_template_parms;
9779 parser->implicit_template_scope = implicit_template_scope;
9780 parser->auto_is_implicit_function_template_parm_p
9781 = auto_is_implicit_function_template_parm_p;
9784 /* This field is only used during parsing of the lambda. */
9785 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9787 /* This lambda shouldn't have any proxies left at this point. */
9788 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9789 /* And now that we're done, push proxies for an enclosing lambda. */
9790 insert_pending_capture_proxies ();
9792 if (ok)
9793 lambda_expr = build_lambda_object (lambda_expr);
9794 else
9795 lambda_expr = error_mark_node;
9797 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9799 pop_deferring_access_checks ();
9801 return lambda_expr;
9804 /* Parse the beginning of a lambda expression.
9806 lambda-introducer:
9807 [ lambda-capture [opt] ]
9809 LAMBDA_EXPR is the current representation of the lambda expression. */
9811 static void
9812 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9814 /* Need commas after the first capture. */
9815 bool first = true;
9817 /* Eat the leading `['. */
9818 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9820 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9821 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9822 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9823 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9824 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9825 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9827 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9829 cp_lexer_consume_token (parser->lexer);
9830 first = false;
9833 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9835 cp_token* capture_token;
9836 tree capture_id;
9837 tree capture_init_expr;
9838 cp_id_kind idk = CP_ID_KIND_NONE;
9839 bool explicit_init_p = false;
9841 enum capture_kind_type
9843 BY_COPY,
9844 BY_REFERENCE
9846 enum capture_kind_type capture_kind = BY_COPY;
9848 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9850 error ("expected end of capture-list");
9851 return;
9854 if (first)
9855 first = false;
9856 else
9857 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9859 /* Possibly capture `this'. */
9860 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
9862 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9863 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
9864 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
9865 "with by-copy capture default");
9866 cp_lexer_consume_token (parser->lexer);
9867 add_capture (lambda_expr,
9868 /*id=*/this_identifier,
9869 /*initializer=*/finish_this_expr(),
9870 /*by_reference_p=*/false,
9871 explicit_init_p);
9872 continue;
9875 /* Remember whether we want to capture as a reference or not. */
9876 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
9878 capture_kind = BY_REFERENCE;
9879 cp_lexer_consume_token (parser->lexer);
9882 /* Get the identifier. */
9883 capture_token = cp_lexer_peek_token (parser->lexer);
9884 capture_id = cp_parser_identifier (parser);
9886 if (capture_id == error_mark_node)
9887 /* Would be nice to have a cp_parser_skip_to_closing_x for general
9888 delimiters, but I modified this to stop on unnested ']' as well. It
9889 was already changed to stop on unnested '}', so the
9890 "closing_parenthesis" name is no more misleading with my change. */
9892 cp_parser_skip_to_closing_parenthesis (parser,
9893 /*recovering=*/true,
9894 /*or_comma=*/true,
9895 /*consume_paren=*/true);
9896 break;
9899 /* Find the initializer for this capture. */
9900 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
9901 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
9902 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9904 bool direct, non_constant;
9905 /* An explicit initializer exists. */
9906 if (cxx_dialect < cxx14)
9907 pedwarn (input_location, 0,
9908 "lambda capture initializers "
9909 "only available with -std=c++14 or -std=gnu++14");
9910 capture_init_expr = cp_parser_initializer (parser, &direct,
9911 &non_constant);
9912 explicit_init_p = true;
9913 if (capture_init_expr == NULL_TREE)
9915 error ("empty initializer for lambda init-capture");
9916 capture_init_expr = error_mark_node;
9919 else
9921 const char* error_msg;
9923 /* Turn the identifier into an id-expression. */
9924 capture_init_expr
9925 = cp_parser_lookup_name_simple (parser, capture_id,
9926 capture_token->location);
9928 if (capture_init_expr == error_mark_node)
9930 unqualified_name_lookup_error (capture_id);
9931 continue;
9933 else if (DECL_P (capture_init_expr)
9934 && (!VAR_P (capture_init_expr)
9935 && TREE_CODE (capture_init_expr) != PARM_DECL))
9937 error_at (capture_token->location,
9938 "capture of non-variable %qD ",
9939 capture_init_expr);
9940 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9941 "%q#D declared here", capture_init_expr);
9942 continue;
9944 if (VAR_P (capture_init_expr)
9945 && decl_storage_duration (capture_init_expr) != dk_auto)
9947 if (pedwarn (capture_token->location, 0, "capture of variable "
9948 "%qD with non-automatic storage duration",
9949 capture_init_expr))
9950 inform (DECL_SOURCE_LOCATION (capture_init_expr),
9951 "%q#D declared here", capture_init_expr);
9952 continue;
9955 capture_init_expr
9956 = finish_id_expression
9957 (capture_id,
9958 capture_init_expr,
9959 parser->scope,
9960 &idk,
9961 /*integral_constant_expression_p=*/false,
9962 /*allow_non_integral_constant_expression_p=*/false,
9963 /*non_integral_constant_expression_p=*/NULL,
9964 /*template_p=*/false,
9965 /*done=*/true,
9966 /*address_p=*/false,
9967 /*template_arg_p=*/false,
9968 &error_msg,
9969 capture_token->location);
9971 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9973 cp_lexer_consume_token (parser->lexer);
9974 capture_init_expr = make_pack_expansion (capture_init_expr);
9976 else
9977 check_for_bare_parameter_packs (capture_init_expr);
9980 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
9981 && !explicit_init_p)
9983 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
9984 && capture_kind == BY_COPY)
9985 pedwarn (capture_token->location, 0, "explicit by-copy capture "
9986 "of %qD redundant with by-copy capture default",
9987 capture_id);
9988 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
9989 && capture_kind == BY_REFERENCE)
9990 pedwarn (capture_token->location, 0, "explicit by-reference "
9991 "capture of %qD redundant with by-reference capture "
9992 "default", capture_id);
9995 add_capture (lambda_expr,
9996 capture_id,
9997 capture_init_expr,
9998 /*by_reference_p=*/capture_kind == BY_REFERENCE,
9999 explicit_init_p);
10002 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10005 /* Parse the (optional) middle of a lambda expression.
10007 lambda-declarator:
10008 < template-parameter-list [opt] >
10009 ( parameter-declaration-clause [opt] )
10010 attribute-specifier [opt]
10011 mutable [opt]
10012 exception-specification [opt]
10013 lambda-return-type-clause [opt]
10015 LAMBDA_EXPR is the current representation of the lambda expression. */
10017 static bool
10018 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10020 /* 5.1.1.4 of the standard says:
10021 If a lambda-expression does not include a lambda-declarator, it is as if
10022 the lambda-declarator were ().
10023 This means an empty parameter list, no attributes, and no exception
10024 specification. */
10025 tree param_list = void_list_node;
10026 tree attributes = NULL_TREE;
10027 tree exception_spec = NULL_TREE;
10028 tree template_param_list = NULL_TREE;
10029 tree tx_qual = NULL_TREE;
10031 /* The template-parameter-list is optional, but must begin with
10032 an opening angle if present. */
10033 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10035 if (cxx_dialect < cxx14)
10036 pedwarn (parser->lexer->next_token->location, 0,
10037 "lambda templates are only available with "
10038 "-std=c++14 or -std=gnu++14");
10040 cp_lexer_consume_token (parser->lexer);
10042 template_param_list = cp_parser_template_parameter_list (parser);
10044 cp_parser_skip_to_end_of_template_parameter_list (parser);
10046 /* We just processed one more parameter list. */
10047 ++parser->num_template_parameter_lists;
10050 /* The parameter-declaration-clause is optional (unless
10051 template-parameter-list was given), but must begin with an
10052 opening parenthesis if present. */
10053 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10055 cp_lexer_consume_token (parser->lexer);
10057 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10059 /* Parse parameters. */
10060 param_list = cp_parser_parameter_declaration_clause (parser);
10062 /* Default arguments shall not be specified in the
10063 parameter-declaration-clause of a lambda-declarator. */
10064 for (tree t = param_list; t; t = TREE_CHAIN (t))
10065 if (TREE_PURPOSE (t) && cxx_dialect < cxx14)
10066 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10067 "default argument specified for lambda parameter");
10069 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10071 attributes = cp_parser_attributes_opt (parser);
10073 /* Parse optional `mutable' keyword. */
10074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
10076 cp_lexer_consume_token (parser->lexer);
10077 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10080 tx_qual = cp_parser_tx_qualifier_opt (parser);
10082 /* Parse optional exception specification. */
10083 exception_spec = cp_parser_exception_specification_opt (parser);
10085 /* Parse optional trailing return type. */
10086 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10088 cp_lexer_consume_token (parser->lexer);
10089 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10090 = cp_parser_trailing_type_id (parser);
10093 /* The function parameters must be in scope all the way until after the
10094 trailing-return-type in case of decltype. */
10095 pop_bindings_and_leave_scope ();
10097 else if (template_param_list != NULL_TREE) // generate diagnostic
10098 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10100 /* Create the function call operator.
10102 Messing with declarators like this is no uglier than building up the
10103 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10104 other code. */
10106 cp_decl_specifier_seq return_type_specs;
10107 cp_declarator* declarator;
10108 tree fco;
10109 int quals;
10110 void *p;
10112 clear_decl_specs (&return_type_specs);
10113 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10114 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10115 else
10116 /* Maybe we will deduce the return type later. */
10117 return_type_specs.type = make_auto ();
10119 p = obstack_alloc (&declarator_obstack, 0);
10121 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
10122 sfk_none);
10124 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10125 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10126 declarator = make_call_declarator (declarator, param_list, quals,
10127 VIRT_SPEC_UNSPECIFIED,
10128 REF_QUAL_NONE,
10129 tx_qual,
10130 exception_spec,
10131 /*late_return_type=*/NULL_TREE,
10132 /*requires_clause*/NULL_TREE);
10133 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10135 fco = grokmethod (&return_type_specs,
10136 declarator,
10137 attributes);
10138 if (fco != error_mark_node)
10140 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10141 DECL_ARTIFICIAL (fco) = 1;
10142 /* Give the object parameter a different name. */
10143 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10144 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10145 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10147 if (template_param_list)
10149 fco = finish_member_template_decl (fco);
10150 finish_template_decl (template_param_list);
10151 --parser->num_template_parameter_lists;
10153 else if (parser->fully_implicit_function_template_p)
10154 fco = finish_fully_implicit_template (parser, fco);
10156 finish_member_declaration (fco);
10158 obstack_free (&declarator_obstack, p);
10160 return (fco != error_mark_node);
10164 /* Parse the body of a lambda expression, which is simply
10166 compound-statement
10168 but which requires special handling.
10169 LAMBDA_EXPR is the current representation of the lambda expression. */
10171 static void
10172 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10174 bool nested = (current_function_decl != NULL_TREE);
10175 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10176 if (nested)
10177 push_function_context ();
10178 else
10179 /* Still increment function_depth so that we don't GC in the
10180 middle of an expression. */
10181 ++function_depth;
10182 vec<tree> omp_privatization_save;
10183 save_omp_privatization_clauses (omp_privatization_save);
10184 /* Clear this in case we're in the middle of a default argument. */
10185 parser->local_variables_forbidden_p = false;
10187 /* Finish the function call operator
10188 - class_specifier
10189 + late_parsing_for_member
10190 + function_definition_after_declarator
10191 + ctor_initializer_opt_and_function_body */
10193 tree fco = lambda_function (lambda_expr);
10194 tree body;
10195 bool done = false;
10196 tree compound_stmt;
10197 tree cap;
10199 /* Let the front end know that we are going to be defining this
10200 function. */
10201 start_preparsed_function (fco,
10202 NULL_TREE,
10203 SF_PRE_PARSED | SF_INCLASS_INLINE);
10205 start_lambda_scope (fco);
10206 body = begin_function_body ();
10208 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10209 goto out;
10211 /* Push the proxies for any explicit captures. */
10212 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10213 cap = TREE_CHAIN (cap))
10214 build_capture_proxy (TREE_PURPOSE (cap));
10216 compound_stmt = begin_compound_stmt (0);
10218 /* 5.1.1.4 of the standard says:
10219 If a lambda-expression does not include a trailing-return-type, it
10220 is as if the trailing-return-type denotes the following type:
10221 * if the compound-statement is of the form
10222 { return attribute-specifier [opt] expression ; }
10223 the type of the returned expression after lvalue-to-rvalue
10224 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10225 (_conv.array_ 4.2), and function-to-pointer conversion
10226 (_conv.func_ 4.3);
10227 * otherwise, void. */
10229 /* In a lambda that has neither a lambda-return-type-clause
10230 nor a deducible form, errors should be reported for return statements
10231 in the body. Since we used void as the placeholder return type, parsing
10232 the body as usual will give such desired behavior. */
10233 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10234 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10235 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10237 tree expr = NULL_TREE;
10238 cp_id_kind idk = CP_ID_KIND_NONE;
10240 /* Parse tentatively in case there's more after the initial return
10241 statement. */
10242 cp_parser_parse_tentatively (parser);
10244 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10246 expr = cp_parser_expression (parser, &idk);
10248 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10249 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10251 if (cp_parser_parse_definitely (parser))
10253 if (!processing_template_decl)
10255 tree type = lambda_return_type (expr);
10256 apply_deduced_return_type (fco, type);
10257 if (type == error_mark_node)
10258 expr = error_mark_node;
10261 /* Will get error here if type not deduced yet. */
10262 finish_return_stmt (expr);
10264 done = true;
10268 if (!done)
10270 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10271 cp_parser_label_declaration (parser);
10272 cp_parser_statement_seq_opt (parser, NULL_TREE);
10273 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10276 finish_compound_stmt (compound_stmt);
10278 out:
10279 finish_function_body (body);
10280 finish_lambda_scope ();
10282 /* Finish the function and generate code for it if necessary. */
10283 tree fn = finish_function (/*inline*/2);
10285 /* Only expand if the call op is not a template. */
10286 if (!DECL_TEMPLATE_INFO (fco))
10287 expand_or_defer_fn (fn);
10290 restore_omp_privatization_clauses (omp_privatization_save);
10291 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10292 if (nested)
10293 pop_function_context();
10294 else
10295 --function_depth;
10298 /* Statements [gram.stmt.stmt] */
10300 /* Parse a statement.
10302 statement:
10303 labeled-statement
10304 expression-statement
10305 compound-statement
10306 selection-statement
10307 iteration-statement
10308 jump-statement
10309 declaration-statement
10310 try-block
10312 C++11:
10314 statement:
10315 labeled-statement
10316 attribute-specifier-seq (opt) expression-statement
10317 attribute-specifier-seq (opt) compound-statement
10318 attribute-specifier-seq (opt) selection-statement
10319 attribute-specifier-seq (opt) iteration-statement
10320 attribute-specifier-seq (opt) jump-statement
10321 declaration-statement
10322 attribute-specifier-seq (opt) try-block
10324 TM Extension:
10326 statement:
10327 atomic-statement
10329 IN_COMPOUND is true when the statement is nested inside a
10330 cp_parser_compound_statement; this matters for certain pragmas.
10332 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10333 is a (possibly labeled) if statement which is not enclosed in braces
10334 and has an else clause. This is used to implement -Wparentheses.
10336 CHAIN is a vector of if-else-if conditions. */
10338 static void
10339 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10340 bool in_compound, bool *if_p, vec<tree> *chain)
10342 tree statement, std_attrs = NULL_TREE;
10343 cp_token *token;
10344 location_t statement_location, attrs_location;
10346 restart:
10347 if (if_p != NULL)
10348 *if_p = false;
10349 /* There is no statement yet. */
10350 statement = NULL_TREE;
10352 saved_token_sentinel saved_tokens (parser->lexer);
10353 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10354 if (c_dialect_objc ())
10355 /* In obj-c++, seeing '[[' might be the either the beginning of
10356 c++11 attributes, or a nested objc-message-expression. So
10357 let's parse the c++11 attributes tentatively. */
10358 cp_parser_parse_tentatively (parser);
10359 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10360 if (c_dialect_objc ())
10362 if (!cp_parser_parse_definitely (parser))
10363 std_attrs = NULL_TREE;
10366 /* Peek at the next token. */
10367 token = cp_lexer_peek_token (parser->lexer);
10368 /* Remember the location of the first token in the statement. */
10369 statement_location = token->location;
10370 /* If this is a keyword, then that will often determine what kind of
10371 statement we have. */
10372 if (token->type == CPP_KEYWORD)
10374 enum rid keyword = token->keyword;
10376 switch (keyword)
10378 case RID_CASE:
10379 case RID_DEFAULT:
10380 /* Looks like a labeled-statement with a case label.
10381 Parse the label, and then use tail recursion to parse
10382 the statement. */
10383 cp_parser_label_for_labeled_statement (parser, std_attrs);
10384 in_compound = false;
10385 goto restart;
10387 case RID_IF:
10388 case RID_SWITCH:
10389 statement = cp_parser_selection_statement (parser, if_p, chain);
10390 break;
10392 case RID_WHILE:
10393 case RID_DO:
10394 case RID_FOR:
10395 statement = cp_parser_iteration_statement (parser, if_p, false);
10396 break;
10398 case RID_CILK_FOR:
10399 if (!flag_cilkplus)
10401 error_at (cp_lexer_peek_token (parser->lexer)->location,
10402 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10403 cp_lexer_consume_token (parser->lexer);
10404 statement = error_mark_node;
10406 else
10407 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10408 break;
10410 case RID_BREAK:
10411 case RID_CONTINUE:
10412 case RID_RETURN:
10413 case RID_GOTO:
10414 statement = cp_parser_jump_statement (parser);
10415 break;
10417 case RID_CILK_SYNC:
10418 cp_lexer_consume_token (parser->lexer);
10419 if (flag_cilkplus)
10421 tree sync_expr = build_cilk_sync ();
10422 SET_EXPR_LOCATION (sync_expr,
10423 token->location);
10424 statement = finish_expr_stmt (sync_expr);
10426 else
10428 error_at (token->location, "-fcilkplus must be enabled to use"
10429 " %<_Cilk_sync%>");
10430 statement = error_mark_node;
10432 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10433 break;
10435 /* Objective-C++ exception-handling constructs. */
10436 case RID_AT_TRY:
10437 case RID_AT_CATCH:
10438 case RID_AT_FINALLY:
10439 case RID_AT_SYNCHRONIZED:
10440 case RID_AT_THROW:
10441 statement = cp_parser_objc_statement (parser);
10442 break;
10444 case RID_TRY:
10445 statement = cp_parser_try_block (parser);
10446 break;
10448 case RID_NAMESPACE:
10449 /* This must be a namespace alias definition. */
10450 cp_parser_declaration_statement (parser);
10451 return;
10453 case RID_TRANSACTION_ATOMIC:
10454 case RID_TRANSACTION_RELAXED:
10455 case RID_SYNCHRONIZED:
10456 case RID_ATOMIC_NOEXCEPT:
10457 case RID_ATOMIC_CANCEL:
10458 statement = cp_parser_transaction (parser, token);
10459 break;
10460 case RID_TRANSACTION_CANCEL:
10461 statement = cp_parser_transaction_cancel (parser);
10462 break;
10464 default:
10465 /* It might be a keyword like `int' that can start a
10466 declaration-statement. */
10467 break;
10470 else if (token->type == CPP_NAME)
10472 /* If the next token is a `:', then we are looking at a
10473 labeled-statement. */
10474 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10475 if (token->type == CPP_COLON)
10477 /* Looks like a labeled-statement with an ordinary label.
10478 Parse the label, and then use tail recursion to parse
10479 the statement. */
10481 cp_parser_label_for_labeled_statement (parser, std_attrs);
10482 in_compound = false;
10483 goto restart;
10486 /* Anything that starts with a `{' must be a compound-statement. */
10487 else if (token->type == CPP_OPEN_BRACE)
10488 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10489 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10490 a statement all its own. */
10491 else if (token->type == CPP_PRAGMA)
10493 /* Only certain OpenMP pragmas are attached to statements, and thus
10494 are considered statements themselves. All others are not. In
10495 the context of a compound, accept the pragma as a "statement" and
10496 return so that we can check for a close brace. Otherwise we
10497 require a real statement and must go back and read one. */
10498 if (in_compound)
10499 cp_parser_pragma (parser, pragma_compound, if_p);
10500 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10501 goto restart;
10502 return;
10504 else if (token->type == CPP_EOF)
10506 cp_parser_error (parser, "expected statement");
10507 return;
10510 /* Everything else must be a declaration-statement or an
10511 expression-statement. Try for the declaration-statement
10512 first, unless we are looking at a `;', in which case we know that
10513 we have an expression-statement. */
10514 if (!statement)
10516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10518 if (std_attrs != NULL_TREE)
10520 /* Attributes should be parsed as part of the the
10521 declaration, so let's un-parse them. */
10522 saved_tokens.rollback();
10523 std_attrs = NULL_TREE;
10526 cp_parser_parse_tentatively (parser);
10527 /* Try to parse the declaration-statement. */
10528 cp_parser_declaration_statement (parser);
10529 /* If that worked, we're done. */
10530 if (cp_parser_parse_definitely (parser))
10531 return;
10533 /* Look for an expression-statement instead. */
10534 statement = cp_parser_expression_statement (parser, in_statement_expr);
10537 /* Set the line number for the statement. */
10538 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10539 SET_EXPR_LOCATION (statement, statement_location);
10541 /* Note that for now, we don't do anything with c++11 statements
10542 parsed at this level. */
10543 if (std_attrs != NULL_TREE)
10544 warning_at (attrs_location,
10545 OPT_Wattributes,
10546 "attributes at the beginning of statement are ignored");
10549 /* Parse the label for a labeled-statement, i.e.
10551 identifier :
10552 case constant-expression :
10553 default :
10555 GNU Extension:
10556 case constant-expression ... constant-expression : statement
10558 When a label is parsed without errors, the label is added to the
10559 parse tree by the finish_* functions, so this function doesn't
10560 have to return the label. */
10562 static void
10563 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10565 cp_token *token;
10566 tree label = NULL_TREE;
10567 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10569 /* The next token should be an identifier. */
10570 token = cp_lexer_peek_token (parser->lexer);
10571 if (token->type != CPP_NAME
10572 && token->type != CPP_KEYWORD)
10574 cp_parser_error (parser, "expected labeled-statement");
10575 return;
10578 parser->colon_corrects_to_scope_p = false;
10579 switch (token->keyword)
10581 case RID_CASE:
10583 tree expr, expr_hi;
10584 cp_token *ellipsis;
10586 /* Consume the `case' token. */
10587 cp_lexer_consume_token (parser->lexer);
10588 /* Parse the constant-expression. */
10589 expr = cp_parser_constant_expression (parser);
10590 if (check_for_bare_parameter_packs (expr))
10591 expr = error_mark_node;
10593 ellipsis = cp_lexer_peek_token (parser->lexer);
10594 if (ellipsis->type == CPP_ELLIPSIS)
10596 /* Consume the `...' token. */
10597 cp_lexer_consume_token (parser->lexer);
10598 expr_hi = cp_parser_constant_expression (parser);
10599 if (check_for_bare_parameter_packs (expr_hi))
10600 expr_hi = error_mark_node;
10602 /* We don't need to emit warnings here, as the common code
10603 will do this for us. */
10605 else
10606 expr_hi = NULL_TREE;
10608 if (parser->in_switch_statement_p)
10609 finish_case_label (token->location, expr, expr_hi);
10610 else
10611 error_at (token->location,
10612 "case label %qE not within a switch statement",
10613 expr);
10615 break;
10617 case RID_DEFAULT:
10618 /* Consume the `default' token. */
10619 cp_lexer_consume_token (parser->lexer);
10621 if (parser->in_switch_statement_p)
10622 finish_case_label (token->location, NULL_TREE, NULL_TREE);
10623 else
10624 error_at (token->location, "case label not within a switch statement");
10625 break;
10627 default:
10628 /* Anything else must be an ordinary label. */
10629 label = finish_label_stmt (cp_parser_identifier (parser));
10630 break;
10633 /* Require the `:' token. */
10634 cp_parser_require (parser, CPP_COLON, RT_COLON);
10636 /* An ordinary label may optionally be followed by attributes.
10637 However, this is only permitted if the attributes are then
10638 followed by a semicolon. This is because, for backward
10639 compatibility, when parsing
10640 lab: __attribute__ ((unused)) int i;
10641 we want the attribute to attach to "i", not "lab". */
10642 if (label != NULL_TREE
10643 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10645 tree attrs;
10646 cp_parser_parse_tentatively (parser);
10647 attrs = cp_parser_gnu_attributes_opt (parser);
10648 if (attrs == NULL_TREE
10649 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10650 cp_parser_abort_tentative_parse (parser);
10651 else if (!cp_parser_parse_definitely (parser))
10653 else
10654 attributes = chainon (attributes, attrs);
10657 if (attributes != NULL_TREE)
10658 cplus_decl_attributes (&label, attributes, 0);
10660 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10663 /* Parse an expression-statement.
10665 expression-statement:
10666 expression [opt] ;
10668 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10669 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10670 indicates whether this expression-statement is part of an
10671 expression statement. */
10673 static tree
10674 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10676 tree statement = NULL_TREE;
10677 cp_token *token = cp_lexer_peek_token (parser->lexer);
10679 /* If the next token is a ';', then there is no expression
10680 statement. */
10681 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10683 statement = cp_parser_expression (parser);
10684 if (statement == error_mark_node
10685 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10687 cp_parser_skip_to_end_of_block_or_statement (parser);
10688 return error_mark_node;
10692 /* Give a helpful message for "A<T>::type t;" and the like. */
10693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10694 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10696 if (TREE_CODE (statement) == SCOPE_REF)
10697 error_at (token->location, "need %<typename%> before %qE because "
10698 "%qT is a dependent scope",
10699 statement, TREE_OPERAND (statement, 0));
10700 else if (is_overloaded_fn (statement)
10701 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10703 /* A::A a; */
10704 tree fn = get_first_fn (statement);
10705 error_at (token->location,
10706 "%<%T::%D%> names the constructor, not the type",
10707 DECL_CONTEXT (fn), DECL_NAME (fn));
10711 /* Consume the final `;'. */
10712 cp_parser_consume_semicolon_at_end_of_statement (parser);
10714 if (in_statement_expr
10715 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10716 /* This is the final expression statement of a statement
10717 expression. */
10718 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10719 else if (statement)
10720 statement = finish_expr_stmt (statement);
10722 return statement;
10725 /* Parse a compound-statement.
10727 compound-statement:
10728 { statement-seq [opt] }
10730 GNU extension:
10732 compound-statement:
10733 { label-declaration-seq [opt] statement-seq [opt] }
10735 label-declaration-seq:
10736 label-declaration
10737 label-declaration-seq label-declaration
10739 Returns a tree representing the statement. */
10741 static tree
10742 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10743 int bcs_flags, bool function_body)
10745 tree compound_stmt;
10747 /* Consume the `{'. */
10748 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10749 return error_mark_node;
10750 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10751 && !function_body && cxx_dialect < cxx14)
10752 pedwarn (input_location, OPT_Wpedantic,
10753 "compound-statement in constexpr function");
10754 /* Begin the compound-statement. */
10755 compound_stmt = begin_compound_stmt (bcs_flags);
10756 /* If the next keyword is `__label__' we have a label declaration. */
10757 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10758 cp_parser_label_declaration (parser);
10759 /* Parse an (optional) statement-seq. */
10760 cp_parser_statement_seq_opt (parser, in_statement_expr);
10761 /* Finish the compound-statement. */
10762 finish_compound_stmt (compound_stmt);
10763 /* Consume the `}'. */
10764 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10766 return compound_stmt;
10769 /* Parse an (optional) statement-seq.
10771 statement-seq:
10772 statement
10773 statement-seq [opt] statement */
10775 static void
10776 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
10778 /* Scan statements until there aren't any more. */
10779 while (true)
10781 cp_token *token = cp_lexer_peek_token (parser->lexer);
10783 /* If we are looking at a `}', then we have run out of
10784 statements; the same is true if we have reached the end
10785 of file, or have stumbled upon a stray '@end'. */
10786 if (token->type == CPP_CLOSE_BRACE
10787 || token->type == CPP_EOF
10788 || token->type == CPP_PRAGMA_EOL
10789 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
10790 break;
10792 /* If we are in a compound statement and find 'else' then
10793 something went wrong. */
10794 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
10796 if (parser->in_statement & IN_IF_STMT)
10797 break;
10798 else
10800 token = cp_lexer_consume_token (parser->lexer);
10801 error_at (token->location, "%<else%> without a previous %<if%>");
10805 /* Parse the statement. */
10806 cp_parser_statement (parser, in_statement_expr, true, NULL);
10810 /* Parse a selection-statement.
10812 selection-statement:
10813 if ( condition ) statement
10814 if ( condition ) statement else statement
10815 switch ( condition ) statement
10817 Returns the new IF_STMT or SWITCH_STMT.
10819 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10820 is a (possibly labeled) if statement which is not enclosed in
10821 braces and has an else clause. This is used to implement
10822 -Wparentheses.
10824 CHAIN is a vector of if-else-if conditions. This is used to implement
10825 -Wduplicated-cond. */
10827 static tree
10828 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
10829 vec<tree> *chain)
10831 cp_token *token;
10832 enum rid keyword;
10833 token_indent_info guard_tinfo;
10835 if (if_p != NULL)
10836 *if_p = false;
10838 /* Peek at the next token. */
10839 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
10840 guard_tinfo = get_token_indent_info (token);
10842 /* See what kind of keyword it is. */
10843 keyword = token->keyword;
10844 switch (keyword)
10846 case RID_IF:
10847 case RID_SWITCH:
10849 tree statement;
10850 tree condition;
10852 /* Look for the `('. */
10853 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10855 cp_parser_skip_to_end_of_statement (parser);
10856 return error_mark_node;
10859 /* Begin the selection-statement. */
10860 if (keyword == RID_IF)
10861 statement = begin_if_stmt ();
10862 else
10863 statement = begin_switch_stmt ();
10865 /* Parse the condition. */
10866 condition = cp_parser_condition (parser);
10867 /* Look for the `)'. */
10868 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10869 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10870 /*consume_paren=*/true);
10872 if (keyword == RID_IF)
10874 bool nested_if;
10875 unsigned char in_statement;
10877 /* Add the condition. */
10878 finish_if_stmt_cond (condition, statement);
10880 if (warn_duplicated_cond)
10881 warn_duplicated_cond_add_or_warn (token->location, condition,
10882 &chain);
10884 /* Parse the then-clause. */
10885 in_statement = parser->in_statement;
10886 parser->in_statement |= IN_IF_STMT;
10887 cp_parser_implicitly_scoped_statement (parser, &nested_if,
10888 guard_tinfo);
10889 parser->in_statement = in_statement;
10891 finish_then_clause (statement);
10893 /* If the next token is `else', parse the else-clause. */
10894 if (cp_lexer_next_token_is_keyword (parser->lexer,
10895 RID_ELSE))
10897 guard_tinfo
10898 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
10899 /* Consume the `else' keyword. */
10900 cp_lexer_consume_token (parser->lexer);
10901 if (warn_duplicated_cond)
10903 if (cp_lexer_next_token_is_keyword (parser->lexer,
10904 RID_IF)
10905 && chain == NULL)
10907 /* We've got "if (COND) else if (COND2)". Start
10908 the condition chain and add COND as the first
10909 element. */
10910 chain = new vec<tree> ();
10911 if (!CONSTANT_CLASS_P (condition)
10912 && !TREE_SIDE_EFFECTS (condition))
10914 /* Wrap it in a NOP_EXPR so that we can set the
10915 location of the condition. */
10916 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
10917 condition);
10918 SET_EXPR_LOCATION (e, token->location);
10919 chain->safe_push (e);
10922 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
10923 RID_IF))
10925 /* This is if-else without subsequent if. Zap the
10926 condition chain; we would have already warned at
10927 this point. */
10928 delete chain;
10929 chain = NULL;
10932 begin_else_clause (statement);
10933 /* Parse the else-clause. */
10934 cp_parser_implicitly_scoped_statement (parser, NULL,
10935 guard_tinfo, chain);
10937 finish_else_clause (statement);
10939 /* If we are currently parsing a then-clause, then
10940 IF_P will not be NULL. We set it to true to
10941 indicate that this if statement has an else clause.
10942 This may trigger the Wparentheses warning below
10943 when we get back up to the parent if statement. */
10944 if (if_p != NULL)
10945 *if_p = true;
10947 else
10949 /* This if statement does not have an else clause. If
10950 NESTED_IF is true, then the then-clause has an if
10951 statement which does have an else clause. We warn
10952 about the potential ambiguity. */
10953 if (nested_if)
10954 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
10955 "suggest explicit braces to avoid ambiguous"
10956 " %<else%>");
10957 if (warn_duplicated_cond)
10959 /* We don't need the condition chain anymore. */
10960 delete chain;
10961 chain = NULL;
10965 /* Now we're all done with the if-statement. */
10966 finish_if_stmt (statement);
10968 else
10970 bool in_switch_statement_p;
10971 unsigned char in_statement;
10973 /* Add the condition. */
10974 finish_switch_cond (condition, statement);
10976 /* Parse the body of the switch-statement. */
10977 in_switch_statement_p = parser->in_switch_statement_p;
10978 in_statement = parser->in_statement;
10979 parser->in_switch_statement_p = true;
10980 parser->in_statement |= IN_SWITCH_STMT;
10981 cp_parser_implicitly_scoped_statement (parser, NULL,
10982 guard_tinfo);
10983 parser->in_switch_statement_p = in_switch_statement_p;
10984 parser->in_statement = in_statement;
10986 /* Now we're all done with the switch-statement. */
10987 finish_switch_stmt (statement);
10990 return statement;
10992 break;
10994 default:
10995 cp_parser_error (parser, "expected selection-statement");
10996 return error_mark_node;
11000 /* Parse a condition.
11002 condition:
11003 expression
11004 type-specifier-seq declarator = initializer-clause
11005 type-specifier-seq declarator braced-init-list
11007 GNU Extension:
11009 condition:
11010 type-specifier-seq declarator asm-specification [opt]
11011 attributes [opt] = assignment-expression
11013 Returns the expression that should be tested. */
11015 static tree
11016 cp_parser_condition (cp_parser* parser)
11018 cp_decl_specifier_seq type_specifiers;
11019 const char *saved_message;
11020 int declares_class_or_enum;
11022 /* Try the declaration first. */
11023 cp_parser_parse_tentatively (parser);
11024 /* New types are not allowed in the type-specifier-seq for a
11025 condition. */
11026 saved_message = parser->type_definition_forbidden_message;
11027 parser->type_definition_forbidden_message
11028 = G_("types may not be defined in conditions");
11029 /* Parse the type-specifier-seq. */
11030 cp_parser_decl_specifier_seq (parser,
11031 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11032 &type_specifiers,
11033 &declares_class_or_enum);
11034 /* Restore the saved message. */
11035 parser->type_definition_forbidden_message = saved_message;
11036 /* If all is well, we might be looking at a declaration. */
11037 if (!cp_parser_error_occurred (parser))
11039 tree decl;
11040 tree asm_specification;
11041 tree attributes;
11042 cp_declarator *declarator;
11043 tree initializer = NULL_TREE;
11045 /* Parse the declarator. */
11046 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11047 /*ctor_dtor_or_conv_p=*/NULL,
11048 /*parenthesized_p=*/NULL,
11049 /*member_p=*/false,
11050 /*friend_p=*/false);
11051 /* Parse the attributes. */
11052 attributes = cp_parser_attributes_opt (parser);
11053 /* Parse the asm-specification. */
11054 asm_specification = cp_parser_asm_specification_opt (parser);
11055 /* If the next token is not an `=' or '{', then we might still be
11056 looking at an expression. For example:
11058 if (A(a).x)
11060 looks like a decl-specifier-seq and a declarator -- but then
11061 there is no `=', so this is an expression. */
11062 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11063 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11064 cp_parser_simulate_error (parser);
11066 /* If we did see an `=' or '{', then we are looking at a declaration
11067 for sure. */
11068 if (cp_parser_parse_definitely (parser))
11070 tree pushed_scope;
11071 bool non_constant_p;
11072 bool flags = LOOKUP_ONLYCONVERTING;
11074 /* Create the declaration. */
11075 decl = start_decl (declarator, &type_specifiers,
11076 /*initialized_p=*/true,
11077 attributes, /*prefix_attributes=*/NULL_TREE,
11078 &pushed_scope);
11080 /* Parse the initializer. */
11081 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11083 initializer = cp_parser_braced_list (parser, &non_constant_p);
11084 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11085 flags = 0;
11087 else
11089 /* Consume the `='. */
11090 cp_parser_require (parser, CPP_EQ, RT_EQ);
11091 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11093 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11094 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11096 /* Process the initializer. */
11097 cp_finish_decl (decl,
11098 initializer, !non_constant_p,
11099 asm_specification,
11100 flags);
11102 if (pushed_scope)
11103 pop_scope (pushed_scope);
11105 return convert_from_reference (decl);
11108 /* If we didn't even get past the declarator successfully, we are
11109 definitely not looking at a declaration. */
11110 else
11111 cp_parser_abort_tentative_parse (parser);
11113 /* Otherwise, we are looking at an expression. */
11114 return cp_parser_expression (parser);
11117 /* Parses a for-statement or range-for-statement until the closing ')',
11118 not included. */
11120 static tree
11121 cp_parser_for (cp_parser *parser, bool ivdep)
11123 tree init, scope, decl;
11124 bool is_range_for;
11126 /* Begin the for-statement. */
11127 scope = begin_for_scope (&init);
11129 /* Parse the initialization. */
11130 is_range_for = cp_parser_for_init_statement (parser, &decl);
11132 if (is_range_for)
11133 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11134 else
11135 return cp_parser_c_for (parser, scope, init, ivdep);
11138 static tree
11139 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11141 /* Normal for loop */
11142 tree condition = NULL_TREE;
11143 tree expression = NULL_TREE;
11144 tree stmt;
11146 stmt = begin_for_stmt (scope, init);
11147 /* The for-init-statement has already been parsed in
11148 cp_parser_for_init_statement, so no work is needed here. */
11149 finish_for_init_stmt (stmt);
11151 /* If there's a condition, process it. */
11152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11153 condition = cp_parser_condition (parser);
11154 else if (ivdep)
11156 cp_parser_error (parser, "missing loop condition in loop with "
11157 "%<GCC ivdep%> pragma");
11158 condition = error_mark_node;
11160 finish_for_cond (condition, stmt, ivdep);
11161 /* Look for the `;'. */
11162 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11164 /* If there's an expression, process it. */
11165 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11166 expression = cp_parser_expression (parser);
11167 finish_for_expr (expression, stmt);
11169 return stmt;
11172 /* Tries to parse a range-based for-statement:
11174 range-based-for:
11175 decl-specifier-seq declarator : expression
11177 The decl-specifier-seq declarator and the `:' are already parsed by
11178 cp_parser_for_init_statement. If processing_template_decl it returns a
11179 newly created RANGE_FOR_STMT; if not, it is converted to a
11180 regular FOR_STMT. */
11182 static tree
11183 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11184 bool ivdep)
11186 tree stmt, range_expr;
11188 /* Get the range declaration momentarily out of the way so that
11189 the range expression doesn't clash with it. */
11190 if (range_decl != error_mark_node)
11191 pop_binding (DECL_NAME (range_decl), range_decl);
11193 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11195 bool expr_non_constant_p;
11196 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11198 else
11199 range_expr = cp_parser_expression (parser);
11201 /* Put the range declaration back into scope. */
11202 if (range_decl != error_mark_node)
11203 push_binding (DECL_NAME (range_decl), range_decl, current_binding_level);
11205 /* If in template, STMT is converted to a normal for-statement
11206 at instantiation. If not, it is done just ahead. */
11207 if (processing_template_decl)
11209 if (check_for_bare_parameter_packs (range_expr))
11210 range_expr = error_mark_node;
11211 stmt = begin_range_for_stmt (scope, init);
11212 if (ivdep)
11213 RANGE_FOR_IVDEP (stmt) = 1;
11214 finish_range_for_decl (stmt, range_decl, range_expr);
11215 if (!type_dependent_expression_p (range_expr)
11216 /* do_auto_deduction doesn't mess with template init-lists. */
11217 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11218 do_range_for_auto_deduction (range_decl, range_expr);
11220 else
11222 stmt = begin_for_stmt (scope, init);
11223 stmt = cp_convert_range_for (stmt, range_decl, range_expr, ivdep);
11225 return stmt;
11228 /* Subroutine of cp_convert_range_for: given the initializer expression,
11229 builds up the range temporary. */
11231 static tree
11232 build_range_temp (tree range_expr)
11234 tree range_type, range_temp;
11236 /* Find out the type deduced by the declaration
11237 `auto &&__range = range_expr'. */
11238 range_type = cp_build_reference_type (make_auto (), true);
11239 range_type = do_auto_deduction (range_type, range_expr,
11240 type_uses_auto (range_type));
11242 /* Create the __range variable. */
11243 range_temp = build_decl (input_location, VAR_DECL,
11244 get_identifier ("__for_range"), range_type);
11245 TREE_USED (range_temp) = 1;
11246 DECL_ARTIFICIAL (range_temp) = 1;
11248 return range_temp;
11251 /* Used by cp_parser_range_for in template context: we aren't going to
11252 do a full conversion yet, but we still need to resolve auto in the
11253 type of the for-range-declaration if present. This is basically
11254 a shortcut version of cp_convert_range_for. */
11256 static void
11257 do_range_for_auto_deduction (tree decl, tree range_expr)
11259 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11260 if (auto_node)
11262 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11263 range_temp = convert_from_reference (build_range_temp (range_expr));
11264 iter_type = (cp_parser_perform_range_for_lookup
11265 (range_temp, &begin_dummy, &end_dummy));
11266 if (iter_type)
11268 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11269 iter_type);
11270 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11271 tf_warning_or_error);
11272 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11273 iter_decl, auto_node);
11278 /* Converts a range-based for-statement into a normal
11279 for-statement, as per the definition.
11281 for (RANGE_DECL : RANGE_EXPR)
11282 BLOCK
11284 should be equivalent to:
11287 auto &&__range = RANGE_EXPR;
11288 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11289 __begin != __end;
11290 ++__begin)
11292 RANGE_DECL = *__begin;
11293 BLOCK
11297 If RANGE_EXPR is an array:
11298 BEGIN_EXPR = __range
11299 END_EXPR = __range + ARRAY_SIZE(__range)
11300 Else if RANGE_EXPR has a member 'begin' or 'end':
11301 BEGIN_EXPR = __range.begin()
11302 END_EXPR = __range.end()
11303 Else:
11304 BEGIN_EXPR = begin(__range)
11305 END_EXPR = end(__range);
11307 If __range has a member 'begin' but not 'end', or vice versa, we must
11308 still use the second alternative (it will surely fail, however).
11309 When calling begin()/end() in the third alternative we must use
11310 argument dependent lookup, but always considering 'std' as an associated
11311 namespace. */
11313 tree
11314 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11315 bool ivdep)
11317 tree begin, end;
11318 tree iter_type, begin_expr, end_expr;
11319 tree condition, expression;
11321 if (range_decl == error_mark_node || range_expr == error_mark_node)
11322 /* If an error happened previously do nothing or else a lot of
11323 unhelpful errors would be issued. */
11324 begin_expr = end_expr = iter_type = error_mark_node;
11325 else
11327 tree range_temp;
11329 if (VAR_P (range_expr)
11330 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11331 /* Can't bind a reference to an array of runtime bound. */
11332 range_temp = range_expr;
11333 else
11335 range_temp = build_range_temp (range_expr);
11336 pushdecl (range_temp);
11337 cp_finish_decl (range_temp, range_expr,
11338 /*is_constant_init*/false, NULL_TREE,
11339 LOOKUP_ONLYCONVERTING);
11340 range_temp = convert_from_reference (range_temp);
11342 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11343 &begin_expr, &end_expr);
11346 /* The new for initialization statement. */
11347 begin = build_decl (input_location, VAR_DECL,
11348 get_identifier ("__for_begin"), iter_type);
11349 TREE_USED (begin) = 1;
11350 DECL_ARTIFICIAL (begin) = 1;
11351 pushdecl (begin);
11352 cp_finish_decl (begin, begin_expr,
11353 /*is_constant_init*/false, NULL_TREE,
11354 LOOKUP_ONLYCONVERTING);
11356 if (cxx_dialect >= cxx1z)
11357 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11358 end = build_decl (input_location, VAR_DECL,
11359 get_identifier ("__for_end"), iter_type);
11360 TREE_USED (end) = 1;
11361 DECL_ARTIFICIAL (end) = 1;
11362 pushdecl (end);
11363 cp_finish_decl (end, end_expr,
11364 /*is_constant_init*/false, NULL_TREE,
11365 LOOKUP_ONLYCONVERTING);
11367 finish_for_init_stmt (statement);
11369 /* The new for condition. */
11370 condition = build_x_binary_op (input_location, NE_EXPR,
11371 begin, ERROR_MARK,
11372 end, ERROR_MARK,
11373 NULL, tf_warning_or_error);
11374 finish_for_cond (condition, statement, ivdep);
11376 /* The new increment expression. */
11377 expression = finish_unary_op_expr (input_location,
11378 PREINCREMENT_EXPR, begin,
11379 tf_warning_or_error);
11380 finish_for_expr (expression, statement);
11382 /* The declaration is initialized with *__begin inside the loop body. */
11383 cp_finish_decl (range_decl,
11384 build_x_indirect_ref (input_location, begin, RO_NULL,
11385 tf_warning_or_error),
11386 /*is_constant_init*/false, NULL_TREE,
11387 LOOKUP_ONLYCONVERTING);
11389 return statement;
11392 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11393 We need to solve both at the same time because the method used
11394 depends on the existence of members begin or end.
11395 Returns the type deduced for the iterator expression. */
11397 static tree
11398 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11400 if (error_operand_p (range))
11402 *begin = *end = error_mark_node;
11403 return error_mark_node;
11406 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11408 error ("range-based %<for%> expression of type %qT "
11409 "has incomplete type", TREE_TYPE (range));
11410 *begin = *end = error_mark_node;
11411 return error_mark_node;
11413 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11415 /* If RANGE is an array, we will use pointer arithmetic. */
11416 *begin = decay_conversion (range, tf_warning_or_error);
11417 *end = build_binary_op (input_location, PLUS_EXPR,
11418 range,
11419 array_type_nelts_top (TREE_TYPE (range)),
11421 return TREE_TYPE (*begin);
11423 else
11425 /* If it is not an array, we must do a bit of magic. */
11426 tree id_begin, id_end;
11427 tree member_begin, member_end;
11429 *begin = *end = error_mark_node;
11431 id_begin = get_identifier ("begin");
11432 id_end = get_identifier ("end");
11433 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11434 /*protect=*/2, /*want_type=*/false,
11435 tf_warning_or_error);
11436 member_end = lookup_member (TREE_TYPE (range), id_end,
11437 /*protect=*/2, /*want_type=*/false,
11438 tf_warning_or_error);
11440 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11442 /* Use the member functions. */
11443 if (member_begin != NULL_TREE)
11444 *begin = cp_parser_range_for_member_function (range, id_begin);
11445 else
11446 error ("range-based %<for%> expression of type %qT has an "
11447 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11449 if (member_end != NULL_TREE)
11450 *end = cp_parser_range_for_member_function (range, id_end);
11451 else
11452 error ("range-based %<for%> expression of type %qT has a "
11453 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11455 else
11457 /* Use global functions with ADL. */
11458 vec<tree, va_gc> *vec;
11459 vec = make_tree_vector ();
11461 vec_safe_push (vec, range);
11463 member_begin = perform_koenig_lookup (id_begin, vec,
11464 tf_warning_or_error);
11465 *begin = finish_call_expr (member_begin, &vec, false, true,
11466 tf_warning_or_error);
11467 member_end = perform_koenig_lookup (id_end, vec,
11468 tf_warning_or_error);
11469 *end = finish_call_expr (member_end, &vec, false, true,
11470 tf_warning_or_error);
11472 release_tree_vector (vec);
11475 /* Last common checks. */
11476 if (*begin == error_mark_node || *end == error_mark_node)
11478 /* If one of the expressions is an error do no more checks. */
11479 *begin = *end = error_mark_node;
11480 return error_mark_node;
11482 else if (type_dependent_expression_p (*begin)
11483 || type_dependent_expression_p (*end))
11484 /* Can happen, when, eg, in a template context, Koenig lookup
11485 can't resolve begin/end (c++/58503). */
11486 return NULL_TREE;
11487 else
11489 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11490 /* The unqualified type of the __begin and __end temporaries should
11491 be the same, as required by the multiple auto declaration. */
11492 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11494 if (cxx_dialect >= cxx1z
11495 && (build_x_binary_op (input_location, NE_EXPR,
11496 *begin, ERROR_MARK,
11497 *end, ERROR_MARK,
11498 NULL, tf_none)
11499 != error_mark_node))
11500 /* P0184R0 allows __begin and __end to have different types,
11501 but make sure they are comparable so we can give a better
11502 diagnostic. */;
11503 else
11504 error ("inconsistent begin/end types in range-based %<for%> "
11505 "statement: %qT and %qT",
11506 TREE_TYPE (*begin), TREE_TYPE (*end));
11508 return iter_type;
11513 /* Helper function for cp_parser_perform_range_for_lookup.
11514 Builds a tree for RANGE.IDENTIFIER(). */
11516 static tree
11517 cp_parser_range_for_member_function (tree range, tree identifier)
11519 tree member, res;
11520 vec<tree, va_gc> *vec;
11522 member = finish_class_member_access_expr (range, identifier,
11523 false, tf_warning_or_error);
11524 if (member == error_mark_node)
11525 return error_mark_node;
11527 vec = make_tree_vector ();
11528 res = finish_call_expr (member, &vec,
11529 /*disallow_virtual=*/false,
11530 /*koenig_p=*/false,
11531 tf_warning_or_error);
11532 release_tree_vector (vec);
11533 return res;
11536 /* Parse an iteration-statement.
11538 iteration-statement:
11539 while ( condition ) statement
11540 do statement while ( expression ) ;
11541 for ( for-init-statement condition [opt] ; expression [opt] )
11542 statement
11544 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11546 static tree
11547 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11549 cp_token *token;
11550 enum rid keyword;
11551 tree statement;
11552 unsigned char in_statement;
11553 token_indent_info guard_tinfo;
11555 /* Peek at the next token. */
11556 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11557 if (!token)
11558 return error_mark_node;
11560 guard_tinfo = get_token_indent_info (token);
11562 /* Remember whether or not we are already within an iteration
11563 statement. */
11564 in_statement = parser->in_statement;
11566 /* See what kind of keyword it is. */
11567 keyword = token->keyword;
11568 switch (keyword)
11570 case RID_WHILE:
11572 tree condition;
11574 /* Begin the while-statement. */
11575 statement = begin_while_stmt ();
11576 /* Look for the `('. */
11577 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11578 /* Parse the condition. */
11579 condition = cp_parser_condition (parser);
11580 finish_while_stmt_cond (condition, statement, ivdep);
11581 /* Look for the `)'. */
11582 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11583 /* Parse the dependent statement. */
11584 parser->in_statement = IN_ITERATION_STMT;
11585 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11586 parser->in_statement = in_statement;
11587 /* We're done with the while-statement. */
11588 finish_while_stmt (statement);
11590 break;
11592 case RID_DO:
11594 tree expression;
11596 /* Begin the do-statement. */
11597 statement = begin_do_stmt ();
11598 /* Parse the body of the do-statement. */
11599 parser->in_statement = IN_ITERATION_STMT;
11600 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11601 parser->in_statement = in_statement;
11602 finish_do_body (statement);
11603 /* Look for the `while' keyword. */
11604 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11605 /* Look for the `('. */
11606 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11607 /* Parse the expression. */
11608 expression = cp_parser_expression (parser);
11609 /* We're done with the do-statement. */
11610 finish_do_stmt (expression, statement, ivdep);
11611 /* Look for the `)'. */
11612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11613 /* Look for the `;'. */
11614 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11616 break;
11618 case RID_FOR:
11620 /* Look for the `('. */
11621 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11623 statement = cp_parser_for (parser, ivdep);
11625 /* Look for the `)'. */
11626 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11628 /* Parse the body of the for-statement. */
11629 parser->in_statement = IN_ITERATION_STMT;
11630 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11631 parser->in_statement = in_statement;
11633 /* We're done with the for-statement. */
11634 finish_for_stmt (statement);
11636 break;
11638 default:
11639 cp_parser_error (parser, "expected iteration-statement");
11640 statement = error_mark_node;
11641 break;
11644 return statement;
11647 /* Parse a for-init-statement or the declarator of a range-based-for.
11648 Returns true if a range-based-for declaration is seen.
11650 for-init-statement:
11651 expression-statement
11652 simple-declaration */
11654 static bool
11655 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
11657 /* If the next token is a `;', then we have an empty
11658 expression-statement. Grammatically, this is also a
11659 simple-declaration, but an invalid one, because it does not
11660 declare anything. Therefore, if we did not handle this case
11661 specially, we would issue an error message about an invalid
11662 declaration. */
11663 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11665 bool is_range_for = false;
11666 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11668 /* A colon is used in range-based for. */
11669 parser->colon_corrects_to_scope_p = false;
11671 /* We're going to speculatively look for a declaration, falling back
11672 to an expression, if necessary. */
11673 cp_parser_parse_tentatively (parser);
11674 /* Parse the declaration. */
11675 cp_parser_simple_declaration (parser,
11676 /*function_definition_allowed_p=*/false,
11677 decl);
11678 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11679 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11681 /* It is a range-for, consume the ':' */
11682 cp_lexer_consume_token (parser->lexer);
11683 is_range_for = true;
11684 if (cxx_dialect < cxx11)
11686 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11687 "range-based %<for%> loops only available with "
11688 "-std=c++11 or -std=gnu++11");
11689 *decl = error_mark_node;
11692 else
11693 /* The ';' is not consumed yet because we told
11694 cp_parser_simple_declaration not to. */
11695 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11697 if (cp_parser_parse_definitely (parser))
11698 return is_range_for;
11699 /* If the tentative parse failed, then we shall need to look for an
11700 expression-statement. */
11702 /* If we are here, it is an expression-statement. */
11703 cp_parser_expression_statement (parser, NULL_TREE);
11704 return false;
11707 /* Parse a jump-statement.
11709 jump-statement:
11710 break ;
11711 continue ;
11712 return expression [opt] ;
11713 return braced-init-list ;
11714 goto identifier ;
11716 GNU extension:
11718 jump-statement:
11719 goto * expression ;
11721 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
11723 static tree
11724 cp_parser_jump_statement (cp_parser* parser)
11726 tree statement = error_mark_node;
11727 cp_token *token;
11728 enum rid keyword;
11729 unsigned char in_statement;
11731 /* Peek at the next token. */
11732 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
11733 if (!token)
11734 return error_mark_node;
11736 /* See what kind of keyword it is. */
11737 keyword = token->keyword;
11738 switch (keyword)
11740 case RID_BREAK:
11741 in_statement = parser->in_statement & ~IN_IF_STMT;
11742 switch (in_statement)
11744 case 0:
11745 error_at (token->location, "break statement not within loop or switch");
11746 break;
11747 default:
11748 gcc_assert ((in_statement & IN_SWITCH_STMT)
11749 || in_statement == IN_ITERATION_STMT);
11750 statement = finish_break_stmt ();
11751 if (in_statement == IN_ITERATION_STMT)
11752 break_maybe_infinite_loop ();
11753 break;
11754 case IN_OMP_BLOCK:
11755 error_at (token->location, "invalid exit from OpenMP structured block");
11756 break;
11757 case IN_OMP_FOR:
11758 error_at (token->location, "break statement used with OpenMP for loop");
11759 break;
11760 case IN_CILK_SIMD_FOR:
11761 error_at (token->location, "break statement used with Cilk Plus for loop");
11762 break;
11764 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11765 break;
11767 case RID_CONTINUE:
11768 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
11770 case 0:
11771 error_at (token->location, "continue statement not within a loop");
11772 break;
11773 case IN_CILK_SIMD_FOR:
11774 error_at (token->location,
11775 "continue statement within %<#pragma simd%> loop body");
11776 /* Fall through. */
11777 case IN_ITERATION_STMT:
11778 case IN_OMP_FOR:
11779 statement = finish_continue_stmt ();
11780 break;
11781 case IN_OMP_BLOCK:
11782 error_at (token->location, "invalid exit from OpenMP structured block");
11783 break;
11784 default:
11785 gcc_unreachable ();
11787 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11788 break;
11790 case RID_RETURN:
11792 tree expr;
11793 bool expr_non_constant_p;
11795 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11797 cp_lexer_set_source_position (parser->lexer);
11798 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11799 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11801 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11802 expr = cp_parser_expression (parser);
11803 else
11804 /* If the next token is a `;', then there is no
11805 expression. */
11806 expr = NULL_TREE;
11807 /* Build the return-statement. */
11808 statement = finish_return_stmt (expr);
11809 /* Look for the final `;'. */
11810 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11812 break;
11814 case RID_GOTO:
11815 if (parser->in_function_body
11816 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
11818 error ("%<goto%> in %<constexpr%> function");
11819 cp_function_chain->invalid_constexpr = true;
11822 /* Create the goto-statement. */
11823 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
11825 /* Issue a warning about this use of a GNU extension. */
11826 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
11827 /* Consume the '*' token. */
11828 cp_lexer_consume_token (parser->lexer);
11829 /* Parse the dependent expression. */
11830 finish_goto_stmt (cp_parser_expression (parser));
11832 else
11833 finish_goto_stmt (cp_parser_identifier (parser));
11834 /* Look for the final `;'. */
11835 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11836 break;
11838 default:
11839 cp_parser_error (parser, "expected jump-statement");
11840 break;
11843 return statement;
11846 /* Parse a declaration-statement.
11848 declaration-statement:
11849 block-declaration */
11851 static void
11852 cp_parser_declaration_statement (cp_parser* parser)
11854 void *p;
11856 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
11857 p = obstack_alloc (&declarator_obstack, 0);
11859 /* Parse the block-declaration. */
11860 cp_parser_block_declaration (parser, /*statement_p=*/true);
11862 /* Free any declarators allocated. */
11863 obstack_free (&declarator_obstack, p);
11866 /* Some dependent statements (like `if (cond) statement'), are
11867 implicitly in their own scope. In other words, if the statement is
11868 a single statement (as opposed to a compound-statement), it is
11869 none-the-less treated as if it were enclosed in braces. Any
11870 declarations appearing in the dependent statement are out of scope
11871 after control passes that point. This function parses a statement,
11872 but ensures that is in its own scope, even if it is not a
11873 compound-statement.
11875 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11876 is a (possibly labeled) if statement which is not enclosed in
11877 braces and has an else clause. This is used to implement
11878 -Wparentheses.
11880 CHAIN is a vector of if-else-if conditions. This is used to implement
11881 -Wduplicated-cond.
11883 Returns the new statement. */
11885 static tree
11886 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
11887 const token_indent_info &guard_tinfo,
11888 vec<tree> *chain)
11890 tree statement;
11891 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
11892 token_indent_info body_tinfo
11893 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11895 if (if_p != NULL)
11896 *if_p = false;
11898 /* Mark if () ; with a special NOP_EXPR. */
11899 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11901 cp_lexer_consume_token (parser->lexer);
11902 statement = add_stmt (build_empty_stmt (body_loc));
11904 if (guard_tinfo.keyword == RID_IF
11905 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
11906 warning_at (body_loc, OPT_Wempty_body,
11907 "suggest braces around empty body in an %<if%> statement");
11908 else if (guard_tinfo.keyword == RID_ELSE)
11909 warning_at (body_loc, OPT_Wempty_body,
11910 "suggest braces around empty body in an %<else%> statement");
11912 /* if a compound is opened, we simply parse the statement directly. */
11913 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11914 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
11915 /* If the token is not a `{', then we must take special action. */
11916 else
11918 /* Create a compound-statement. */
11919 statement = begin_compound_stmt (0);
11920 /* Parse the dependent-statement. */
11921 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
11922 /* Finish the dummy compound-statement. */
11923 finish_compound_stmt (statement);
11926 token_indent_info next_tinfo
11927 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11928 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11930 /* Return the statement. */
11931 return statement;
11934 /* For some dependent statements (like `while (cond) statement'), we
11935 have already created a scope. Therefore, even if the dependent
11936 statement is a compound-statement, we do not want to create another
11937 scope. */
11939 static void
11940 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
11941 const token_indent_info &guard_tinfo)
11943 /* If the token is a `{', then we must take special action. */
11944 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11946 token_indent_info body_tinfo
11947 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11949 cp_parser_statement (parser, NULL_TREE, false, if_p);
11950 token_indent_info next_tinfo
11951 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11952 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
11954 else
11956 /* Avoid calling cp_parser_compound_statement, so that we
11957 don't create a new scope. Do everything else by hand. */
11958 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
11959 /* If the next keyword is `__label__' we have a label declaration. */
11960 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11961 cp_parser_label_declaration (parser);
11962 /* Parse an (optional) statement-seq. */
11963 cp_parser_statement_seq_opt (parser, NULL_TREE);
11964 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11968 /* Declarations [gram.dcl.dcl] */
11970 /* Parse an optional declaration-sequence.
11972 declaration-seq:
11973 declaration
11974 declaration-seq declaration */
11976 static void
11977 cp_parser_declaration_seq_opt (cp_parser* parser)
11979 while (true)
11981 cp_token *token;
11983 token = cp_lexer_peek_token (parser->lexer);
11985 if (token->type == CPP_CLOSE_BRACE
11986 || token->type == CPP_EOF
11987 || token->type == CPP_PRAGMA_EOL)
11988 break;
11990 if (token->type == CPP_SEMICOLON)
11992 /* A declaration consisting of a single semicolon is
11993 invalid. Allow it unless we're being pedantic. */
11994 cp_lexer_consume_token (parser->lexer);
11995 if (!in_system_header_at (input_location))
11996 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
11997 continue;
12000 /* If we're entering or exiting a region that's implicitly
12001 extern "C", modify the lang context appropriately. */
12002 if (!parser->implicit_extern_c && token->implicit_extern_c)
12004 push_lang_context (lang_name_c);
12005 parser->implicit_extern_c = true;
12007 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12009 pop_lang_context ();
12010 parser->implicit_extern_c = false;
12013 if (token->type == CPP_PRAGMA)
12015 /* A top-level declaration can consist solely of a #pragma.
12016 A nested declaration cannot, so this is done here and not
12017 in cp_parser_declaration. (A #pragma at block scope is
12018 handled in cp_parser_statement.) */
12019 cp_parser_pragma (parser, pragma_external, NULL);
12020 continue;
12023 /* Parse the declaration itself. */
12024 cp_parser_declaration (parser);
12028 /* Parse a declaration.
12030 declaration:
12031 block-declaration
12032 function-definition
12033 template-declaration
12034 explicit-instantiation
12035 explicit-specialization
12036 linkage-specification
12037 namespace-definition
12039 GNU extension:
12041 declaration:
12042 __extension__ declaration */
12044 static void
12045 cp_parser_declaration (cp_parser* parser)
12047 cp_token token1;
12048 cp_token token2;
12049 int saved_pedantic;
12050 void *p;
12051 tree attributes = NULL_TREE;
12053 /* Check for the `__extension__' keyword. */
12054 if (cp_parser_extension_opt (parser, &saved_pedantic))
12056 /* Parse the qualified declaration. */
12057 cp_parser_declaration (parser);
12058 /* Restore the PEDANTIC flag. */
12059 pedantic = saved_pedantic;
12061 return;
12064 /* Try to figure out what kind of declaration is present. */
12065 token1 = *cp_lexer_peek_token (parser->lexer);
12067 if (token1.type != CPP_EOF)
12068 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12069 else
12071 token2.type = CPP_EOF;
12072 token2.keyword = RID_MAX;
12075 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12076 p = obstack_alloc (&declarator_obstack, 0);
12078 /* If the next token is `extern' and the following token is a string
12079 literal, then we have a linkage specification. */
12080 if (token1.keyword == RID_EXTERN
12081 && cp_parser_is_pure_string_literal (&token2))
12082 cp_parser_linkage_specification (parser);
12083 /* If the next token is `template', then we have either a template
12084 declaration, an explicit instantiation, or an explicit
12085 specialization. */
12086 else if (token1.keyword == RID_TEMPLATE)
12088 /* `template <>' indicates a template specialization. */
12089 if (token2.type == CPP_LESS
12090 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12091 cp_parser_explicit_specialization (parser);
12092 /* `template <' indicates a template declaration. */
12093 else if (token2.type == CPP_LESS)
12094 cp_parser_template_declaration (parser, /*member_p=*/false);
12095 /* Anything else must be an explicit instantiation. */
12096 else
12097 cp_parser_explicit_instantiation (parser);
12099 /* If the next token is `export', then we have a template
12100 declaration. */
12101 else if (token1.keyword == RID_EXPORT)
12102 cp_parser_template_declaration (parser, /*member_p=*/false);
12103 /* If the next token is `extern', 'static' or 'inline' and the one
12104 after that is `template', we have a GNU extended explicit
12105 instantiation directive. */
12106 else if (cp_parser_allow_gnu_extensions_p (parser)
12107 && (token1.keyword == RID_EXTERN
12108 || token1.keyword == RID_STATIC
12109 || token1.keyword == RID_INLINE)
12110 && token2.keyword == RID_TEMPLATE)
12111 cp_parser_explicit_instantiation (parser);
12112 /* If the next token is `namespace', check for a named or unnamed
12113 namespace definition. */
12114 else if (token1.keyword == RID_NAMESPACE
12115 && (/* A named namespace definition. */
12116 (token2.type == CPP_NAME
12117 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12118 != CPP_EQ))
12119 || (token2.type == CPP_OPEN_SQUARE
12120 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12121 == CPP_OPEN_SQUARE)
12122 /* An unnamed namespace definition. */
12123 || token2.type == CPP_OPEN_BRACE
12124 || token2.keyword == RID_ATTRIBUTE))
12125 cp_parser_namespace_definition (parser);
12126 /* An inline (associated) namespace definition. */
12127 else if (token1.keyword == RID_INLINE
12128 && token2.keyword == RID_NAMESPACE)
12129 cp_parser_namespace_definition (parser);
12130 /* Objective-C++ declaration/definition. */
12131 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12132 cp_parser_objc_declaration (parser, NULL_TREE);
12133 else if (c_dialect_objc ()
12134 && token1.keyword == RID_ATTRIBUTE
12135 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12136 cp_parser_objc_declaration (parser, attributes);
12137 /* At this point we may have a template declared by a concept
12138 introduction. */
12139 else if (flag_concepts
12140 && cp_parser_template_declaration_after_export (parser,
12141 /*member_p=*/false))
12142 /* We did. */;
12143 else
12144 /* Try to parse a block-declaration, or a function-definition. */
12145 cp_parser_block_declaration (parser, /*statement_p=*/false);
12147 /* Free any declarators allocated. */
12148 obstack_free (&declarator_obstack, p);
12151 /* Parse a block-declaration.
12153 block-declaration:
12154 simple-declaration
12155 asm-definition
12156 namespace-alias-definition
12157 using-declaration
12158 using-directive
12160 GNU Extension:
12162 block-declaration:
12163 __extension__ block-declaration
12165 C++0x Extension:
12167 block-declaration:
12168 static_assert-declaration
12170 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12171 part of a declaration-statement. */
12173 static void
12174 cp_parser_block_declaration (cp_parser *parser,
12175 bool statement_p)
12177 cp_token *token1;
12178 int saved_pedantic;
12180 /* Check for the `__extension__' keyword. */
12181 if (cp_parser_extension_opt (parser, &saved_pedantic))
12183 /* Parse the qualified declaration. */
12184 cp_parser_block_declaration (parser, statement_p);
12185 /* Restore the PEDANTIC flag. */
12186 pedantic = saved_pedantic;
12188 return;
12191 /* Peek at the next token to figure out which kind of declaration is
12192 present. */
12193 token1 = cp_lexer_peek_token (parser->lexer);
12195 /* If the next keyword is `asm', we have an asm-definition. */
12196 if (token1->keyword == RID_ASM)
12198 if (statement_p)
12199 cp_parser_commit_to_tentative_parse (parser);
12200 cp_parser_asm_definition (parser);
12202 /* If the next keyword is `namespace', we have a
12203 namespace-alias-definition. */
12204 else if (token1->keyword == RID_NAMESPACE)
12205 cp_parser_namespace_alias_definition (parser);
12206 /* If the next keyword is `using', we have a
12207 using-declaration, a using-directive, or an alias-declaration. */
12208 else if (token1->keyword == RID_USING)
12210 cp_token *token2;
12212 if (statement_p)
12213 cp_parser_commit_to_tentative_parse (parser);
12214 /* If the token after `using' is `namespace', then we have a
12215 using-directive. */
12216 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12217 if (token2->keyword == RID_NAMESPACE)
12218 cp_parser_using_directive (parser);
12219 /* If the second token after 'using' is '=', then we have an
12220 alias-declaration. */
12221 else if (cxx_dialect >= cxx11
12222 && token2->type == CPP_NAME
12223 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12224 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12225 cp_parser_alias_declaration (parser);
12226 /* Otherwise, it's a using-declaration. */
12227 else
12228 cp_parser_using_declaration (parser,
12229 /*access_declaration_p=*/false);
12231 /* If the next keyword is `__label__' we have a misplaced label
12232 declaration. */
12233 else if (token1->keyword == RID_LABEL)
12235 cp_lexer_consume_token (parser->lexer);
12236 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12237 cp_parser_skip_to_end_of_statement (parser);
12238 /* If the next token is now a `;', consume it. */
12239 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12240 cp_lexer_consume_token (parser->lexer);
12242 /* If the next token is `static_assert' we have a static assertion. */
12243 else if (token1->keyword == RID_STATIC_ASSERT)
12244 cp_parser_static_assert (parser, /*member_p=*/false);
12245 /* Anything else must be a simple-declaration. */
12246 else
12247 cp_parser_simple_declaration (parser, !statement_p,
12248 /*maybe_range_for_decl*/NULL);
12251 /* Parse a simple-declaration.
12253 simple-declaration:
12254 decl-specifier-seq [opt] init-declarator-list [opt] ;
12256 init-declarator-list:
12257 init-declarator
12258 init-declarator-list , init-declarator
12260 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12261 function-definition as a simple-declaration.
12263 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12264 parsed declaration if it is an uninitialized single declarator not followed
12265 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12266 if present, will not be consumed. */
12268 static void
12269 cp_parser_simple_declaration (cp_parser* parser,
12270 bool function_definition_allowed_p,
12271 tree *maybe_range_for_decl)
12273 cp_decl_specifier_seq decl_specifiers;
12274 int declares_class_or_enum;
12275 bool saw_declarator;
12276 location_t comma_loc = UNKNOWN_LOCATION;
12277 location_t init_loc = UNKNOWN_LOCATION;
12279 if (maybe_range_for_decl)
12280 *maybe_range_for_decl = NULL_TREE;
12282 /* Defer access checks until we know what is being declared; the
12283 checks for names appearing in the decl-specifier-seq should be
12284 done as if we were in the scope of the thing being declared. */
12285 push_deferring_access_checks (dk_deferred);
12287 /* Parse the decl-specifier-seq. We have to keep track of whether
12288 or not the decl-specifier-seq declares a named class or
12289 enumeration type, since that is the only case in which the
12290 init-declarator-list is allowed to be empty.
12292 [dcl.dcl]
12294 In a simple-declaration, the optional init-declarator-list can be
12295 omitted only when declaring a class or enumeration, that is when
12296 the decl-specifier-seq contains either a class-specifier, an
12297 elaborated-type-specifier, or an enum-specifier. */
12298 cp_parser_decl_specifier_seq (parser,
12299 CP_PARSER_FLAGS_OPTIONAL,
12300 &decl_specifiers,
12301 &declares_class_or_enum);
12302 /* We no longer need to defer access checks. */
12303 stop_deferring_access_checks ();
12305 /* In a block scope, a valid declaration must always have a
12306 decl-specifier-seq. By not trying to parse declarators, we can
12307 resolve the declaration/expression ambiguity more quickly. */
12308 if (!function_definition_allowed_p
12309 && !decl_specifiers.any_specifiers_p)
12311 cp_parser_error (parser, "expected declaration");
12312 goto done;
12315 /* If the next two tokens are both identifiers, the code is
12316 erroneous. The usual cause of this situation is code like:
12318 T t;
12320 where "T" should name a type -- but does not. */
12321 if (!decl_specifiers.any_type_specifiers_p
12322 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12324 /* If parsing tentatively, we should commit; we really are
12325 looking at a declaration. */
12326 cp_parser_commit_to_tentative_parse (parser);
12327 /* Give up. */
12328 goto done;
12331 /* If we have seen at least one decl-specifier, and the next token
12332 is not a parenthesis, then we must be looking at a declaration.
12333 (After "int (" we might be looking at a functional cast.) */
12334 if (decl_specifiers.any_specifiers_p
12335 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12336 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12337 && !cp_parser_error_occurred (parser))
12338 cp_parser_commit_to_tentative_parse (parser);
12340 tree last_type;
12342 last_type = NULL_TREE;
12344 /* Keep going until we hit the `;' at the end of the simple
12345 declaration. */
12346 saw_declarator = false;
12347 while (cp_lexer_next_token_is_not (parser->lexer,
12348 CPP_SEMICOLON))
12350 cp_token *token;
12351 bool function_definition_p;
12352 tree decl;
12353 tree auto_result = NULL_TREE;
12355 if (saw_declarator)
12357 /* If we are processing next declarator, comma is expected */
12358 token = cp_lexer_peek_token (parser->lexer);
12359 gcc_assert (token->type == CPP_COMMA);
12360 cp_lexer_consume_token (parser->lexer);
12361 if (maybe_range_for_decl)
12363 *maybe_range_for_decl = error_mark_node;
12364 if (comma_loc == UNKNOWN_LOCATION)
12365 comma_loc = token->location;
12368 else
12369 saw_declarator = true;
12371 /* Parse the init-declarator. */
12372 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12373 /*checks=*/NULL,
12374 function_definition_allowed_p,
12375 /*member_p=*/false,
12376 declares_class_or_enum,
12377 &function_definition_p,
12378 maybe_range_for_decl,
12379 &init_loc,
12380 &auto_result);
12381 /* If an error occurred while parsing tentatively, exit quickly.
12382 (That usually happens when in the body of a function; each
12383 statement is treated as a declaration-statement until proven
12384 otherwise.) */
12385 if (cp_parser_error_occurred (parser))
12386 goto done;
12388 if (auto_result)
12390 if (last_type && last_type != error_mark_node
12391 && !same_type_p (auto_result, last_type))
12393 /* If the list of declarators contains more than one declarator,
12394 the type of each declared variable is determined as described
12395 above. If the type deduced for the template parameter U is not
12396 the same in each deduction, the program is ill-formed. */
12397 error_at (decl_specifiers.locations[ds_type_spec],
12398 "inconsistent deduction for %qT: %qT and then %qT",
12399 decl_specifiers.type, last_type, auto_result);
12400 last_type = error_mark_node;
12402 else
12403 last_type = auto_result;
12406 /* Handle function definitions specially. */
12407 if (function_definition_p)
12409 /* If the next token is a `,', then we are probably
12410 processing something like:
12412 void f() {}, *p;
12414 which is erroneous. */
12415 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12417 cp_token *token = cp_lexer_peek_token (parser->lexer);
12418 error_at (token->location,
12419 "mixing"
12420 " declarations and function-definitions is forbidden");
12422 /* Otherwise, we're done with the list of declarators. */
12423 else
12425 pop_deferring_access_checks ();
12426 return;
12429 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12430 *maybe_range_for_decl = decl;
12431 /* The next token should be either a `,' or a `;'. */
12432 token = cp_lexer_peek_token (parser->lexer);
12433 /* If it's a `,', there are more declarators to come. */
12434 if (token->type == CPP_COMMA)
12435 /* will be consumed next time around */;
12436 /* If it's a `;', we are done. */
12437 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12438 break;
12439 /* Anything else is an error. */
12440 else
12442 /* If we have already issued an error message we don't need
12443 to issue another one. */
12444 if ((decl != error_mark_node
12445 && DECL_INITIAL (decl) != error_mark_node)
12446 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12447 cp_parser_error (parser, "expected %<,%> or %<;%>");
12448 /* Skip tokens until we reach the end of the statement. */
12449 cp_parser_skip_to_end_of_statement (parser);
12450 /* If the next token is now a `;', consume it. */
12451 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12452 cp_lexer_consume_token (parser->lexer);
12453 goto done;
12455 /* After the first time around, a function-definition is not
12456 allowed -- even if it was OK at first. For example:
12458 int i, f() {}
12460 is not valid. */
12461 function_definition_allowed_p = false;
12464 /* Issue an error message if no declarators are present, and the
12465 decl-specifier-seq does not itself declare a class or
12466 enumeration: [dcl.dcl]/3. */
12467 if (!saw_declarator)
12469 if (cp_parser_declares_only_class_p (parser))
12471 if (!declares_class_or_enum
12472 && decl_specifiers.type
12473 && OVERLOAD_TYPE_P (decl_specifiers.type))
12474 /* Ensure an error is issued anyway when finish_decltype_type,
12475 called via cp_parser_decl_specifier_seq, returns a class or
12476 an enumeration (c++/51786). */
12477 decl_specifiers.type = NULL_TREE;
12478 shadow_tag (&decl_specifiers);
12480 /* Perform any deferred access checks. */
12481 perform_deferred_access_checks (tf_warning_or_error);
12484 /* Consume the `;'. */
12485 if (!maybe_range_for_decl)
12486 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12487 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12489 if (init_loc != UNKNOWN_LOCATION)
12490 error_at (init_loc, "initializer in range-based %<for%> loop");
12491 if (comma_loc != UNKNOWN_LOCATION)
12492 error_at (comma_loc,
12493 "multiple declarations in range-based %<for%> loop");
12496 done:
12497 pop_deferring_access_checks ();
12500 /* Parse a decl-specifier-seq.
12502 decl-specifier-seq:
12503 decl-specifier-seq [opt] decl-specifier
12504 decl-specifier attribute-specifier-seq [opt] (C++11)
12506 decl-specifier:
12507 storage-class-specifier
12508 type-specifier
12509 function-specifier
12510 friend
12511 typedef
12513 GNU Extension:
12515 decl-specifier:
12516 attributes
12518 Concepts Extension:
12520 decl-specifier:
12521 concept
12523 Set *DECL_SPECS to a representation of the decl-specifier-seq.
12525 The parser flags FLAGS is used to control type-specifier parsing.
12527 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
12528 flags:
12530 1: one of the decl-specifiers is an elaborated-type-specifier
12531 (i.e., a type declaration)
12532 2: one of the decl-specifiers is an enum-specifier or a
12533 class-specifier (i.e., a type definition)
12537 static void
12538 cp_parser_decl_specifier_seq (cp_parser* parser,
12539 cp_parser_flags flags,
12540 cp_decl_specifier_seq *decl_specs,
12541 int* declares_class_or_enum)
12543 bool constructor_possible_p = !parser->in_declarator_p;
12544 bool found_decl_spec = false;
12545 cp_token *start_token = NULL;
12546 cp_decl_spec ds;
12548 /* Clear DECL_SPECS. */
12549 clear_decl_specs (decl_specs);
12551 /* Assume no class or enumeration type is declared. */
12552 *declares_class_or_enum = 0;
12554 /* Keep reading specifiers until there are no more to read. */
12555 while (true)
12557 bool constructor_p;
12558 cp_token *token;
12559 ds = ds_last;
12561 /* Peek at the next token. */
12562 token = cp_lexer_peek_token (parser->lexer);
12564 /* Save the first token of the decl spec list for error
12565 reporting. */
12566 if (!start_token)
12567 start_token = token;
12568 /* Handle attributes. */
12569 if (cp_next_tokens_can_be_attribute_p (parser))
12571 /* Parse the attributes. */
12572 tree attrs = cp_parser_attributes_opt (parser);
12574 /* In a sequence of declaration specifiers, c++11 attributes
12575 appertain to the type that precede them. In that case
12576 [dcl.spec]/1 says:
12578 The attribute-specifier-seq affects the type only for
12579 the declaration it appears in, not other declarations
12580 involving the same type.
12582 But for now let's force the user to position the
12583 attribute either at the beginning of the declaration or
12584 after the declarator-id, which would clearly mean that it
12585 applies to the declarator. */
12586 if (cxx11_attribute_p (attrs))
12588 if (!found_decl_spec)
12589 /* The c++11 attribute is at the beginning of the
12590 declaration. It appertains to the entity being
12591 declared. */;
12592 else
12594 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
12596 /* This is an attribute following a
12597 class-specifier. */
12598 if (decl_specs->type_definition_p)
12599 warn_misplaced_attr_for_class_type (token->location,
12600 decl_specs->type);
12601 attrs = NULL_TREE;
12603 else
12605 decl_specs->std_attributes
12606 = chainon (decl_specs->std_attributes,
12607 attrs);
12608 if (decl_specs->locations[ds_std_attribute] == 0)
12609 decl_specs->locations[ds_std_attribute] = token->location;
12611 continue;
12615 decl_specs->attributes
12616 = chainon (decl_specs->attributes,
12617 attrs);
12618 if (decl_specs->locations[ds_attribute] == 0)
12619 decl_specs->locations[ds_attribute] = token->location;
12620 continue;
12622 /* Assume we will find a decl-specifier keyword. */
12623 found_decl_spec = true;
12624 /* If the next token is an appropriate keyword, we can simply
12625 add it to the list. */
12626 switch (token->keyword)
12628 /* decl-specifier:
12629 friend
12630 constexpr */
12631 case RID_FRIEND:
12632 if (!at_class_scope_p ())
12634 error_at (token->location, "%<friend%> used outside of class");
12635 cp_lexer_purge_token (parser->lexer);
12637 else
12639 ds = ds_friend;
12640 /* Consume the token. */
12641 cp_lexer_consume_token (parser->lexer);
12643 break;
12645 case RID_CONSTEXPR:
12646 ds = ds_constexpr;
12647 cp_lexer_consume_token (parser->lexer);
12648 break;
12650 case RID_CONCEPT:
12651 ds = ds_concept;
12652 cp_lexer_consume_token (parser->lexer);
12653 break;
12655 /* function-specifier:
12656 inline
12657 virtual
12658 explicit */
12659 case RID_INLINE:
12660 case RID_VIRTUAL:
12661 case RID_EXPLICIT:
12662 cp_parser_function_specifier_opt (parser, decl_specs);
12663 break;
12665 /* decl-specifier:
12666 typedef */
12667 case RID_TYPEDEF:
12668 ds = ds_typedef;
12669 /* Consume the token. */
12670 cp_lexer_consume_token (parser->lexer);
12671 /* A constructor declarator cannot appear in a typedef. */
12672 constructor_possible_p = false;
12673 /* The "typedef" keyword can only occur in a declaration; we
12674 may as well commit at this point. */
12675 cp_parser_commit_to_tentative_parse (parser);
12677 if (decl_specs->storage_class != sc_none)
12678 decl_specs->conflicting_specifiers_p = true;
12679 break;
12681 /* storage-class-specifier:
12682 auto
12683 register
12684 static
12685 extern
12686 mutable
12688 GNU Extension:
12689 thread */
12690 case RID_AUTO:
12691 if (cxx_dialect == cxx98)
12693 /* Consume the token. */
12694 cp_lexer_consume_token (parser->lexer);
12696 /* Complain about `auto' as a storage specifier, if
12697 we're complaining about C++0x compatibility. */
12698 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
12699 " changes meaning in C++11; please remove it");
12701 /* Set the storage class anyway. */
12702 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
12703 token);
12705 else
12706 /* C++0x auto type-specifier. */
12707 found_decl_spec = false;
12708 break;
12710 case RID_REGISTER:
12711 case RID_STATIC:
12712 case RID_EXTERN:
12713 case RID_MUTABLE:
12714 /* Consume the token. */
12715 cp_lexer_consume_token (parser->lexer);
12716 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
12717 token);
12718 break;
12719 case RID_THREAD:
12720 /* Consume the token. */
12721 ds = ds_thread;
12722 cp_lexer_consume_token (parser->lexer);
12723 break;
12725 default:
12726 /* We did not yet find a decl-specifier yet. */
12727 found_decl_spec = false;
12728 break;
12731 if (found_decl_spec
12732 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
12733 && token->keyword != RID_CONSTEXPR)
12734 error ("decl-specifier invalid in condition");
12736 if (ds != ds_last)
12737 set_and_check_decl_spec_loc (decl_specs, ds, token);
12739 /* Constructors are a special case. The `S' in `S()' is not a
12740 decl-specifier; it is the beginning of the declarator. */
12741 constructor_p
12742 = (!found_decl_spec
12743 && constructor_possible_p
12744 && (cp_parser_constructor_declarator_p
12745 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
12747 /* If we don't have a DECL_SPEC yet, then we must be looking at
12748 a type-specifier. */
12749 if (!found_decl_spec && !constructor_p)
12751 int decl_spec_declares_class_or_enum;
12752 bool is_cv_qualifier;
12753 tree type_spec;
12755 type_spec
12756 = cp_parser_type_specifier (parser, flags,
12757 decl_specs,
12758 /*is_declaration=*/true,
12759 &decl_spec_declares_class_or_enum,
12760 &is_cv_qualifier);
12761 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
12763 /* If this type-specifier referenced a user-defined type
12764 (a typedef, class-name, etc.), then we can't allow any
12765 more such type-specifiers henceforth.
12767 [dcl.spec]
12769 The longest sequence of decl-specifiers that could
12770 possibly be a type name is taken as the
12771 decl-specifier-seq of a declaration. The sequence shall
12772 be self-consistent as described below.
12774 [dcl.type]
12776 As a general rule, at most one type-specifier is allowed
12777 in the complete decl-specifier-seq of a declaration. The
12778 only exceptions are the following:
12780 -- const or volatile can be combined with any other
12781 type-specifier.
12783 -- signed or unsigned can be combined with char, long,
12784 short, or int.
12786 -- ..
12788 Example:
12790 typedef char* Pc;
12791 void g (const int Pc);
12793 Here, Pc is *not* part of the decl-specifier seq; it's
12794 the declarator. Therefore, once we see a type-specifier
12795 (other than a cv-qualifier), we forbid any additional
12796 user-defined types. We *do* still allow things like `int
12797 int' to be considered a decl-specifier-seq, and issue the
12798 error message later. */
12799 if (type_spec && !is_cv_qualifier)
12800 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12801 /* A constructor declarator cannot follow a type-specifier. */
12802 if (type_spec)
12804 constructor_possible_p = false;
12805 found_decl_spec = true;
12806 if (!is_cv_qualifier)
12807 decl_specs->any_type_specifiers_p = true;
12811 /* If we still do not have a DECL_SPEC, then there are no more
12812 decl-specifiers. */
12813 if (!found_decl_spec)
12814 break;
12816 decl_specs->any_specifiers_p = true;
12817 /* After we see one decl-specifier, further decl-specifiers are
12818 always optional. */
12819 flags |= CP_PARSER_FLAGS_OPTIONAL;
12822 /* Don't allow a friend specifier with a class definition. */
12823 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
12824 && (*declares_class_or_enum & 2))
12825 error_at (decl_specs->locations[ds_friend],
12826 "class definition may not be declared a friend");
12829 /* Parse an (optional) storage-class-specifier.
12831 storage-class-specifier:
12832 auto
12833 register
12834 static
12835 extern
12836 mutable
12838 GNU Extension:
12840 storage-class-specifier:
12841 thread
12843 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
12845 static tree
12846 cp_parser_storage_class_specifier_opt (cp_parser* parser)
12848 switch (cp_lexer_peek_token (parser->lexer)->keyword)
12850 case RID_AUTO:
12851 if (cxx_dialect != cxx98)
12852 return NULL_TREE;
12853 /* Fall through for C++98. */
12855 case RID_REGISTER:
12856 case RID_STATIC:
12857 case RID_EXTERN:
12858 case RID_MUTABLE:
12859 case RID_THREAD:
12860 /* Consume the token. */
12861 return cp_lexer_consume_token (parser->lexer)->u.value;
12863 default:
12864 return NULL_TREE;
12868 /* Parse an (optional) function-specifier.
12870 function-specifier:
12871 inline
12872 virtual
12873 explicit
12875 Returns an IDENTIFIER_NODE corresponding to the keyword used.
12876 Updates DECL_SPECS, if it is non-NULL. */
12878 static tree
12879 cp_parser_function_specifier_opt (cp_parser* parser,
12880 cp_decl_specifier_seq *decl_specs)
12882 cp_token *token = cp_lexer_peek_token (parser->lexer);
12883 switch (token->keyword)
12885 case RID_INLINE:
12886 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
12887 break;
12889 case RID_VIRTUAL:
12890 /* 14.5.2.3 [temp.mem]
12892 A member function template shall not be virtual. */
12893 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
12894 error_at (token->location, "templates may not be %<virtual%>");
12895 else
12896 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
12897 break;
12899 case RID_EXPLICIT:
12900 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
12901 break;
12903 default:
12904 return NULL_TREE;
12907 /* Consume the token. */
12908 return cp_lexer_consume_token (parser->lexer)->u.value;
12911 /* Parse a linkage-specification.
12913 linkage-specification:
12914 extern string-literal { declaration-seq [opt] }
12915 extern string-literal declaration */
12917 static void
12918 cp_parser_linkage_specification (cp_parser* parser)
12920 tree linkage;
12922 /* Look for the `extern' keyword. */
12923 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
12925 /* Look for the string-literal. */
12926 linkage = cp_parser_string_literal (parser, false, false);
12928 /* Transform the literal into an identifier. If the literal is a
12929 wide-character string, or contains embedded NULs, then we can't
12930 handle it as the user wants. */
12931 if (strlen (TREE_STRING_POINTER (linkage))
12932 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
12934 cp_parser_error (parser, "invalid linkage-specification");
12935 /* Assume C++ linkage. */
12936 linkage = lang_name_cplusplus;
12938 else
12939 linkage = get_identifier (TREE_STRING_POINTER (linkage));
12941 /* We're now using the new linkage. */
12942 push_lang_context (linkage);
12944 /* If the next token is a `{', then we're using the first
12945 production. */
12946 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12948 cp_ensure_no_omp_declare_simd (parser);
12949 cp_ensure_no_oacc_routine (parser);
12951 /* Consume the `{' token. */
12952 cp_lexer_consume_token (parser->lexer);
12953 /* Parse the declarations. */
12954 cp_parser_declaration_seq_opt (parser);
12955 /* Look for the closing `}'. */
12956 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12958 /* Otherwise, there's just one declaration. */
12959 else
12961 bool saved_in_unbraced_linkage_specification_p;
12963 saved_in_unbraced_linkage_specification_p
12964 = parser->in_unbraced_linkage_specification_p;
12965 parser->in_unbraced_linkage_specification_p = true;
12966 cp_parser_declaration (parser);
12967 parser->in_unbraced_linkage_specification_p
12968 = saved_in_unbraced_linkage_specification_p;
12971 /* We're done with the linkage-specification. */
12972 pop_lang_context ();
12975 /* Parse a static_assert-declaration.
12977 static_assert-declaration:
12978 static_assert ( constant-expression , string-literal ) ;
12979 static_assert ( constant-expression ) ; (C++1Z)
12981 If MEMBER_P, this static_assert is a class member. */
12983 static void
12984 cp_parser_static_assert(cp_parser *parser, bool member_p)
12986 tree condition;
12987 tree message;
12988 cp_token *token;
12989 location_t saved_loc;
12990 bool dummy;
12992 /* Peek at the `static_assert' token so we can keep track of exactly
12993 where the static assertion started. */
12994 token = cp_lexer_peek_token (parser->lexer);
12995 saved_loc = token->location;
12997 /* Look for the `static_assert' keyword. */
12998 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
12999 RT_STATIC_ASSERT))
13000 return;
13002 /* We know we are in a static assertion; commit to any tentative
13003 parse. */
13004 if (cp_parser_parsing_tentatively (parser))
13005 cp_parser_commit_to_tentative_parse (parser);
13007 /* Parse the `(' starting the static assertion condition. */
13008 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13010 /* Parse the constant-expression. Allow a non-constant expression
13011 here in order to give better diagnostics in finish_static_assert. */
13012 condition =
13013 cp_parser_constant_expression (parser,
13014 /*allow_non_constant_p=*/true,
13015 /*non_constant_p=*/&dummy);
13017 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13019 if (cxx_dialect < cxx1z)
13020 pedwarn (input_location, OPT_Wpedantic,
13021 "static_assert without a message "
13022 "only available with -std=c++1z or -std=gnu++1z");
13023 /* Eat the ')' */
13024 cp_lexer_consume_token (parser->lexer);
13025 message = build_string (1, "");
13026 TREE_TYPE (message) = char_array_type_node;
13027 fix_string_type (message);
13029 else
13031 /* Parse the separating `,'. */
13032 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13034 /* Parse the string-literal message. */
13035 message = cp_parser_string_literal (parser,
13036 /*translate=*/false,
13037 /*wide_ok=*/true);
13039 /* A `)' completes the static assertion. */
13040 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13041 cp_parser_skip_to_closing_parenthesis (parser,
13042 /*recovering=*/true,
13043 /*or_comma=*/false,
13044 /*consume_paren=*/true);
13047 /* A semicolon terminates the declaration. */
13048 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13050 /* Complete the static assertion, which may mean either processing
13051 the static assert now or saving it for template instantiation. */
13052 finish_static_assert (condition, message, saved_loc, member_p);
13055 /* Parse the expression in decltype ( expression ). */
13057 static tree
13058 cp_parser_decltype_expr (cp_parser *parser,
13059 bool &id_expression_or_member_access_p)
13061 cp_token *id_expr_start_token;
13062 tree expr;
13064 /* Since we're going to preserve any side-effects from this parse, set up a
13065 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13066 in the expression. */
13067 tentative_firewall firewall (parser);
13069 /* First, try parsing an id-expression. */
13070 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13071 cp_parser_parse_tentatively (parser);
13072 expr = cp_parser_id_expression (parser,
13073 /*template_keyword_p=*/false,
13074 /*check_dependency_p=*/true,
13075 /*template_p=*/NULL,
13076 /*declarator_p=*/false,
13077 /*optional_p=*/false);
13079 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13081 bool non_integral_constant_expression_p = false;
13082 tree id_expression = expr;
13083 cp_id_kind idk;
13084 const char *error_msg;
13086 if (identifier_p (expr))
13087 /* Lookup the name we got back from the id-expression. */
13088 expr = cp_parser_lookup_name_simple (parser, expr,
13089 id_expr_start_token->location);
13091 if (expr
13092 && expr != error_mark_node
13093 && TREE_CODE (expr) != TYPE_DECL
13094 && (TREE_CODE (expr) != BIT_NOT_EXPR
13095 || !TYPE_P (TREE_OPERAND (expr, 0)))
13096 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13098 /* Complete lookup of the id-expression. */
13099 expr = (finish_id_expression
13100 (id_expression, expr, parser->scope, &idk,
13101 /*integral_constant_expression_p=*/false,
13102 /*allow_non_integral_constant_expression_p=*/true,
13103 &non_integral_constant_expression_p,
13104 /*template_p=*/false,
13105 /*done=*/true,
13106 /*address_p=*/false,
13107 /*template_arg_p=*/false,
13108 &error_msg,
13109 id_expr_start_token->location));
13111 if (expr == error_mark_node)
13112 /* We found an id-expression, but it was something that we
13113 should not have found. This is an error, not something
13114 we can recover from, so note that we found an
13115 id-expression and we'll recover as gracefully as
13116 possible. */
13117 id_expression_or_member_access_p = true;
13120 if (expr
13121 && expr != error_mark_node
13122 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13123 /* We have an id-expression. */
13124 id_expression_or_member_access_p = true;
13127 if (!id_expression_or_member_access_p)
13129 /* Abort the id-expression parse. */
13130 cp_parser_abort_tentative_parse (parser);
13132 /* Parsing tentatively, again. */
13133 cp_parser_parse_tentatively (parser);
13135 /* Parse a class member access. */
13136 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13137 /*cast_p=*/false, /*decltype*/true,
13138 /*member_access_only_p=*/true, NULL);
13140 if (expr
13141 && expr != error_mark_node
13142 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13143 /* We have an id-expression. */
13144 id_expression_or_member_access_p = true;
13147 if (id_expression_or_member_access_p)
13148 /* We have parsed the complete id-expression or member access. */
13149 cp_parser_parse_definitely (parser);
13150 else
13152 /* Abort our attempt to parse an id-expression or member access
13153 expression. */
13154 cp_parser_abort_tentative_parse (parser);
13156 /* Parse a full expression. */
13157 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13158 /*decltype_p=*/true);
13161 return expr;
13164 /* Parse a `decltype' type. Returns the type.
13166 simple-type-specifier:
13167 decltype ( expression )
13168 C++14 proposal:
13169 decltype ( auto ) */
13171 static tree
13172 cp_parser_decltype (cp_parser *parser)
13174 tree expr;
13175 bool id_expression_or_member_access_p = false;
13176 const char *saved_message;
13177 bool saved_integral_constant_expression_p;
13178 bool saved_non_integral_constant_expression_p;
13179 bool saved_greater_than_is_operator_p;
13180 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13182 if (start_token->type == CPP_DECLTYPE)
13184 /* Already parsed. */
13185 cp_lexer_consume_token (parser->lexer);
13186 return saved_checks_value (start_token->u.tree_check_value);
13189 /* Look for the `decltype' token. */
13190 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13191 return error_mark_node;
13193 /* Parse the opening `('. */
13194 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13195 return error_mark_node;
13197 /* decltype (auto) */
13198 if (cxx_dialect >= cxx14
13199 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13201 cp_lexer_consume_token (parser->lexer);
13202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13203 return error_mark_node;
13204 expr = make_decltype_auto ();
13205 AUTO_IS_DECLTYPE (expr) = true;
13206 goto rewrite;
13209 /* Types cannot be defined in a `decltype' expression. Save away the
13210 old message. */
13211 saved_message = parser->type_definition_forbidden_message;
13213 /* And create the new one. */
13214 parser->type_definition_forbidden_message
13215 = G_("types may not be defined in %<decltype%> expressions");
13217 /* The restrictions on constant-expressions do not apply inside
13218 decltype expressions. */
13219 saved_integral_constant_expression_p
13220 = parser->integral_constant_expression_p;
13221 saved_non_integral_constant_expression_p
13222 = parser->non_integral_constant_expression_p;
13223 parser->integral_constant_expression_p = false;
13225 /* Within a parenthesized expression, a `>' token is always
13226 the greater-than operator. */
13227 saved_greater_than_is_operator_p
13228 = parser->greater_than_is_operator_p;
13229 parser->greater_than_is_operator_p = true;
13231 /* Do not actually evaluate the expression. */
13232 ++cp_unevaluated_operand;
13234 /* Do not warn about problems with the expression. */
13235 ++c_inhibit_evaluation_warnings;
13237 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13239 /* Go back to evaluating expressions. */
13240 --cp_unevaluated_operand;
13241 --c_inhibit_evaluation_warnings;
13243 /* The `>' token might be the end of a template-id or
13244 template-parameter-list now. */
13245 parser->greater_than_is_operator_p
13246 = saved_greater_than_is_operator_p;
13248 /* Restore the old message and the integral constant expression
13249 flags. */
13250 parser->type_definition_forbidden_message = saved_message;
13251 parser->integral_constant_expression_p
13252 = saved_integral_constant_expression_p;
13253 parser->non_integral_constant_expression_p
13254 = saved_non_integral_constant_expression_p;
13256 /* Parse to the closing `)'. */
13257 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13259 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13260 /*consume_paren=*/true);
13261 return error_mark_node;
13264 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13265 tf_warning_or_error);
13267 rewrite:
13268 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13269 it again. */
13270 start_token->type = CPP_DECLTYPE;
13271 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13272 start_token->u.tree_check_value->value = expr;
13273 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13274 start_token->keyword = RID_MAX;
13275 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13277 return expr;
13280 /* Special member functions [gram.special] */
13282 /* Parse a conversion-function-id.
13284 conversion-function-id:
13285 operator conversion-type-id
13287 Returns an IDENTIFIER_NODE representing the operator. */
13289 static tree
13290 cp_parser_conversion_function_id (cp_parser* parser)
13292 tree type;
13293 tree saved_scope;
13294 tree saved_qualifying_scope;
13295 tree saved_object_scope;
13296 tree pushed_scope = NULL_TREE;
13298 /* Look for the `operator' token. */
13299 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13300 return error_mark_node;
13301 /* When we parse the conversion-type-id, the current scope will be
13302 reset. However, we need that information in able to look up the
13303 conversion function later, so we save it here. */
13304 saved_scope = parser->scope;
13305 saved_qualifying_scope = parser->qualifying_scope;
13306 saved_object_scope = parser->object_scope;
13307 /* We must enter the scope of the class so that the names of
13308 entities declared within the class are available in the
13309 conversion-type-id. For example, consider:
13311 struct S {
13312 typedef int I;
13313 operator I();
13316 S::operator I() { ... }
13318 In order to see that `I' is a type-name in the definition, we
13319 must be in the scope of `S'. */
13320 if (saved_scope)
13321 pushed_scope = push_scope (saved_scope);
13322 /* Parse the conversion-type-id. */
13323 type = cp_parser_conversion_type_id (parser);
13324 /* Leave the scope of the class, if any. */
13325 if (pushed_scope)
13326 pop_scope (pushed_scope);
13327 /* Restore the saved scope. */
13328 parser->scope = saved_scope;
13329 parser->qualifying_scope = saved_qualifying_scope;
13330 parser->object_scope = saved_object_scope;
13331 /* If the TYPE is invalid, indicate failure. */
13332 if (type == error_mark_node)
13333 return error_mark_node;
13334 return mangle_conv_op_name_for_type (type);
13337 /* Parse a conversion-type-id:
13339 conversion-type-id:
13340 type-specifier-seq conversion-declarator [opt]
13342 Returns the TYPE specified. */
13344 static tree
13345 cp_parser_conversion_type_id (cp_parser* parser)
13347 tree attributes;
13348 cp_decl_specifier_seq type_specifiers;
13349 cp_declarator *declarator;
13350 tree type_specified;
13351 const char *saved_message;
13353 /* Parse the attributes. */
13354 attributes = cp_parser_attributes_opt (parser);
13356 saved_message = parser->type_definition_forbidden_message;
13357 parser->type_definition_forbidden_message
13358 = G_("types may not be defined in a conversion-type-id");
13360 /* Parse the type-specifiers. */
13361 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13362 /*is_trailing_return=*/false,
13363 &type_specifiers);
13365 parser->type_definition_forbidden_message = saved_message;
13367 /* If that didn't work, stop. */
13368 if (type_specifiers.type == error_mark_node)
13369 return error_mark_node;
13370 /* Parse the conversion-declarator. */
13371 declarator = cp_parser_conversion_declarator_opt (parser);
13373 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13374 /*initialized=*/0, &attributes);
13375 if (attributes)
13376 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13378 /* Don't give this error when parsing tentatively. This happens to
13379 work because we always parse this definitively once. */
13380 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13381 && type_uses_auto (type_specified))
13383 if (cxx_dialect < cxx14)
13385 error ("invalid use of %<auto%> in conversion operator");
13386 return error_mark_node;
13388 else if (template_parm_scope_p ())
13389 warning (0, "use of %<auto%> in member template "
13390 "conversion operator can never be deduced");
13393 return type_specified;
13396 /* Parse an (optional) conversion-declarator.
13398 conversion-declarator:
13399 ptr-operator conversion-declarator [opt]
13403 static cp_declarator *
13404 cp_parser_conversion_declarator_opt (cp_parser* parser)
13406 enum tree_code code;
13407 tree class_type, std_attributes = NULL_TREE;
13408 cp_cv_quals cv_quals;
13410 /* We don't know if there's a ptr-operator next, or not. */
13411 cp_parser_parse_tentatively (parser);
13412 /* Try the ptr-operator. */
13413 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
13414 &std_attributes);
13415 /* If it worked, look for more conversion-declarators. */
13416 if (cp_parser_parse_definitely (parser))
13418 cp_declarator *declarator;
13420 /* Parse another optional declarator. */
13421 declarator = cp_parser_conversion_declarator_opt (parser);
13423 declarator = cp_parser_make_indirect_declarator
13424 (code, class_type, cv_quals, declarator, std_attributes);
13426 return declarator;
13429 return NULL;
13432 /* Parse an (optional) ctor-initializer.
13434 ctor-initializer:
13435 : mem-initializer-list
13437 Returns TRUE iff the ctor-initializer was actually present. */
13439 static bool
13440 cp_parser_ctor_initializer_opt (cp_parser* parser)
13442 /* If the next token is not a `:', then there is no
13443 ctor-initializer. */
13444 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13446 /* Do default initialization of any bases and members. */
13447 if (DECL_CONSTRUCTOR_P (current_function_decl))
13448 finish_mem_initializers (NULL_TREE);
13450 return false;
13453 /* Consume the `:' token. */
13454 cp_lexer_consume_token (parser->lexer);
13455 /* And the mem-initializer-list. */
13456 cp_parser_mem_initializer_list (parser);
13458 return true;
13461 /* Parse a mem-initializer-list.
13463 mem-initializer-list:
13464 mem-initializer ... [opt]
13465 mem-initializer ... [opt] , mem-initializer-list */
13467 static void
13468 cp_parser_mem_initializer_list (cp_parser* parser)
13470 tree mem_initializer_list = NULL_TREE;
13471 tree target_ctor = error_mark_node;
13472 cp_token *token = cp_lexer_peek_token (parser->lexer);
13474 /* Let the semantic analysis code know that we are starting the
13475 mem-initializer-list. */
13476 if (!DECL_CONSTRUCTOR_P (current_function_decl))
13477 error_at (token->location,
13478 "only constructors take member initializers");
13480 /* Loop through the list. */
13481 while (true)
13483 tree mem_initializer;
13485 token = cp_lexer_peek_token (parser->lexer);
13486 /* Parse the mem-initializer. */
13487 mem_initializer = cp_parser_mem_initializer (parser);
13488 /* If the next token is a `...', we're expanding member initializers. */
13489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13491 /* Consume the `...'. */
13492 cp_lexer_consume_token (parser->lexer);
13494 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
13495 can be expanded but members cannot. */
13496 if (mem_initializer != error_mark_node
13497 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
13499 error_at (token->location,
13500 "cannot expand initializer for member %<%D%>",
13501 TREE_PURPOSE (mem_initializer));
13502 mem_initializer = error_mark_node;
13505 /* Construct the pack expansion type. */
13506 if (mem_initializer != error_mark_node)
13507 mem_initializer = make_pack_expansion (mem_initializer);
13509 if (target_ctor != error_mark_node
13510 && mem_initializer != error_mark_node)
13512 error ("mem-initializer for %qD follows constructor delegation",
13513 TREE_PURPOSE (mem_initializer));
13514 mem_initializer = error_mark_node;
13516 /* Look for a target constructor. */
13517 if (mem_initializer != error_mark_node
13518 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
13519 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
13521 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
13522 if (mem_initializer_list)
13524 error ("constructor delegation follows mem-initializer for %qD",
13525 TREE_PURPOSE (mem_initializer_list));
13526 mem_initializer = error_mark_node;
13528 target_ctor = mem_initializer;
13530 /* Add it to the list, unless it was erroneous. */
13531 if (mem_initializer != error_mark_node)
13533 TREE_CHAIN (mem_initializer) = mem_initializer_list;
13534 mem_initializer_list = mem_initializer;
13536 /* If the next token is not a `,', we're done. */
13537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13538 break;
13539 /* Consume the `,' token. */
13540 cp_lexer_consume_token (parser->lexer);
13543 /* Perform semantic analysis. */
13544 if (DECL_CONSTRUCTOR_P (current_function_decl))
13545 finish_mem_initializers (mem_initializer_list);
13548 /* Parse a mem-initializer.
13550 mem-initializer:
13551 mem-initializer-id ( expression-list [opt] )
13552 mem-initializer-id braced-init-list
13554 GNU extension:
13556 mem-initializer:
13557 ( expression-list [opt] )
13559 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
13560 class) or FIELD_DECL (for a non-static data member) to initialize;
13561 the TREE_VALUE is the expression-list. An empty initialization
13562 list is represented by void_list_node. */
13564 static tree
13565 cp_parser_mem_initializer (cp_parser* parser)
13567 tree mem_initializer_id;
13568 tree expression_list;
13569 tree member;
13570 cp_token *token = cp_lexer_peek_token (parser->lexer);
13572 /* Find out what is being initialized. */
13573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
13575 permerror (token->location,
13576 "anachronistic old-style base class initializer");
13577 mem_initializer_id = NULL_TREE;
13579 else
13581 mem_initializer_id = cp_parser_mem_initializer_id (parser);
13582 if (mem_initializer_id == error_mark_node)
13583 return mem_initializer_id;
13585 member = expand_member_init (mem_initializer_id);
13586 if (member && !DECL_P (member))
13587 in_base_initializer = 1;
13589 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13591 bool expr_non_constant_p;
13592 cp_lexer_set_source_position (parser->lexer);
13593 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
13594 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
13595 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
13596 expression_list = build_tree_list (NULL_TREE, expression_list);
13598 else
13600 vec<tree, va_gc> *vec;
13601 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
13602 /*cast_p=*/false,
13603 /*allow_expansion_p=*/true,
13604 /*non_constant_p=*/NULL);
13605 if (vec == NULL)
13606 return error_mark_node;
13607 expression_list = build_tree_list_vec (vec);
13608 release_tree_vector (vec);
13611 if (expression_list == error_mark_node)
13612 return error_mark_node;
13613 if (!expression_list)
13614 expression_list = void_type_node;
13616 in_base_initializer = 0;
13618 return member ? build_tree_list (member, expression_list) : error_mark_node;
13621 /* Parse a mem-initializer-id.
13623 mem-initializer-id:
13624 :: [opt] nested-name-specifier [opt] class-name
13625 decltype-specifier (C++11)
13626 identifier
13628 Returns a TYPE indicating the class to be initialized for the first
13629 production (and the second in C++11). Returns an IDENTIFIER_NODE
13630 indicating the data member to be initialized for the last production. */
13632 static tree
13633 cp_parser_mem_initializer_id (cp_parser* parser)
13635 bool global_scope_p;
13636 bool nested_name_specifier_p;
13637 bool template_p = false;
13638 tree id;
13640 cp_token *token = cp_lexer_peek_token (parser->lexer);
13642 /* `typename' is not allowed in this context ([temp.res]). */
13643 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13645 error_at (token->location,
13646 "keyword %<typename%> not allowed in this context (a qualified "
13647 "member initializer is implicitly a type)");
13648 cp_lexer_consume_token (parser->lexer);
13650 /* Look for the optional `::' operator. */
13651 global_scope_p
13652 = (cp_parser_global_scope_opt (parser,
13653 /*current_scope_valid_p=*/false)
13654 != NULL_TREE);
13655 /* Look for the optional nested-name-specifier. The simplest way to
13656 implement:
13658 [temp.res]
13660 The keyword `typename' is not permitted in a base-specifier or
13661 mem-initializer; in these contexts a qualified name that
13662 depends on a template-parameter is implicitly assumed to be a
13663 type name.
13665 is to assume that we have seen the `typename' keyword at this
13666 point. */
13667 nested_name_specifier_p
13668 = (cp_parser_nested_name_specifier_opt (parser,
13669 /*typename_keyword_p=*/true,
13670 /*check_dependency_p=*/true,
13671 /*type_p=*/true,
13672 /*is_declaration=*/true)
13673 != NULL_TREE);
13674 if (nested_name_specifier_p)
13675 template_p = cp_parser_optional_template_keyword (parser);
13676 /* If there is a `::' operator or a nested-name-specifier, then we
13677 are definitely looking for a class-name. */
13678 if (global_scope_p || nested_name_specifier_p)
13679 return cp_parser_class_name (parser,
13680 /*typename_keyword_p=*/true,
13681 /*template_keyword_p=*/template_p,
13682 typename_type,
13683 /*check_dependency_p=*/true,
13684 /*class_head_p=*/false,
13685 /*is_declaration=*/true);
13686 /* Otherwise, we could also be looking for an ordinary identifier. */
13687 cp_parser_parse_tentatively (parser);
13688 if (cp_lexer_next_token_is_decltype (parser->lexer))
13689 /* Try a decltype-specifier. */
13690 id = cp_parser_decltype (parser);
13691 else
13692 /* Otherwise, try a class-name. */
13693 id = cp_parser_class_name (parser,
13694 /*typename_keyword_p=*/true,
13695 /*template_keyword_p=*/false,
13696 none_type,
13697 /*check_dependency_p=*/true,
13698 /*class_head_p=*/false,
13699 /*is_declaration=*/true);
13700 /* If we found one, we're done. */
13701 if (cp_parser_parse_definitely (parser))
13702 return id;
13703 /* Otherwise, look for an ordinary identifier. */
13704 return cp_parser_identifier (parser);
13707 /* Overloading [gram.over] */
13709 /* Parse an operator-function-id.
13711 operator-function-id:
13712 operator operator
13714 Returns an IDENTIFIER_NODE for the operator which is a
13715 human-readable spelling of the identifier, e.g., `operator +'. */
13717 static cp_expr
13718 cp_parser_operator_function_id (cp_parser* parser)
13720 /* Look for the `operator' keyword. */
13721 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13722 return error_mark_node;
13723 /* And then the name of the operator itself. */
13724 return cp_parser_operator (parser);
13727 /* Return an identifier node for a user-defined literal operator.
13728 The suffix identifier is chained to the operator name identifier. */
13730 static tree
13731 cp_literal_operator_id (const char* name)
13733 tree identifier;
13734 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
13735 + strlen (name) + 10);
13736 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
13737 identifier = get_identifier (buffer);
13739 return identifier;
13742 /* Parse an operator.
13744 operator:
13745 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
13746 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
13747 || ++ -- , ->* -> () []
13749 GNU Extensions:
13751 operator:
13752 <? >? <?= >?=
13754 Returns an IDENTIFIER_NODE for the operator which is a
13755 human-readable spelling of the identifier, e.g., `operator +'. */
13757 static cp_expr
13758 cp_parser_operator (cp_parser* parser)
13760 tree id = NULL_TREE;
13761 cp_token *token;
13762 bool utf8 = false;
13764 /* Peek at the next token. */
13765 token = cp_lexer_peek_token (parser->lexer);
13767 location_t start_loc = token->location;
13769 /* Figure out which operator we have. */
13770 switch (token->type)
13772 case CPP_KEYWORD:
13774 enum tree_code op;
13776 /* The keyword should be either `new' or `delete'. */
13777 if (token->keyword == RID_NEW)
13778 op = NEW_EXPR;
13779 else if (token->keyword == RID_DELETE)
13780 op = DELETE_EXPR;
13781 else
13782 break;
13784 /* Consume the `new' or `delete' token. */
13785 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
13787 /* Peek at the next token. */
13788 token = cp_lexer_peek_token (parser->lexer);
13789 /* If it's a `[' token then this is the array variant of the
13790 operator. */
13791 if (token->type == CPP_OPEN_SQUARE)
13793 /* Consume the `[' token. */
13794 cp_lexer_consume_token (parser->lexer);
13795 /* Look for the `]' token. */
13796 end_loc = cp_parser_require (parser, CPP_CLOSE_SQUARE,
13797 RT_CLOSE_SQUARE)->location;
13798 id = ansi_opname (op == NEW_EXPR
13799 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
13801 /* Otherwise, we have the non-array variant. */
13802 else
13803 id = ansi_opname (op);
13805 location_t loc = make_location (start_loc, start_loc, end_loc);
13807 return cp_expr (id, loc);
13810 case CPP_PLUS:
13811 id = ansi_opname (PLUS_EXPR);
13812 break;
13814 case CPP_MINUS:
13815 id = ansi_opname (MINUS_EXPR);
13816 break;
13818 case CPP_MULT:
13819 id = ansi_opname (MULT_EXPR);
13820 break;
13822 case CPP_DIV:
13823 id = ansi_opname (TRUNC_DIV_EXPR);
13824 break;
13826 case CPP_MOD:
13827 id = ansi_opname (TRUNC_MOD_EXPR);
13828 break;
13830 case CPP_XOR:
13831 id = ansi_opname (BIT_XOR_EXPR);
13832 break;
13834 case CPP_AND:
13835 id = ansi_opname (BIT_AND_EXPR);
13836 break;
13838 case CPP_OR:
13839 id = ansi_opname (BIT_IOR_EXPR);
13840 break;
13842 case CPP_COMPL:
13843 id = ansi_opname (BIT_NOT_EXPR);
13844 break;
13846 case CPP_NOT:
13847 id = ansi_opname (TRUTH_NOT_EXPR);
13848 break;
13850 case CPP_EQ:
13851 id = ansi_assopname (NOP_EXPR);
13852 break;
13854 case CPP_LESS:
13855 id = ansi_opname (LT_EXPR);
13856 break;
13858 case CPP_GREATER:
13859 id = ansi_opname (GT_EXPR);
13860 break;
13862 case CPP_PLUS_EQ:
13863 id = ansi_assopname (PLUS_EXPR);
13864 break;
13866 case CPP_MINUS_EQ:
13867 id = ansi_assopname (MINUS_EXPR);
13868 break;
13870 case CPP_MULT_EQ:
13871 id = ansi_assopname (MULT_EXPR);
13872 break;
13874 case CPP_DIV_EQ:
13875 id = ansi_assopname (TRUNC_DIV_EXPR);
13876 break;
13878 case CPP_MOD_EQ:
13879 id = ansi_assopname (TRUNC_MOD_EXPR);
13880 break;
13882 case CPP_XOR_EQ:
13883 id = ansi_assopname (BIT_XOR_EXPR);
13884 break;
13886 case CPP_AND_EQ:
13887 id = ansi_assopname (BIT_AND_EXPR);
13888 break;
13890 case CPP_OR_EQ:
13891 id = ansi_assopname (BIT_IOR_EXPR);
13892 break;
13894 case CPP_LSHIFT:
13895 id = ansi_opname (LSHIFT_EXPR);
13896 break;
13898 case CPP_RSHIFT:
13899 id = ansi_opname (RSHIFT_EXPR);
13900 break;
13902 case CPP_LSHIFT_EQ:
13903 id = ansi_assopname (LSHIFT_EXPR);
13904 break;
13906 case CPP_RSHIFT_EQ:
13907 id = ansi_assopname (RSHIFT_EXPR);
13908 break;
13910 case CPP_EQ_EQ:
13911 id = ansi_opname (EQ_EXPR);
13912 break;
13914 case CPP_NOT_EQ:
13915 id = ansi_opname (NE_EXPR);
13916 break;
13918 case CPP_LESS_EQ:
13919 id = ansi_opname (LE_EXPR);
13920 break;
13922 case CPP_GREATER_EQ:
13923 id = ansi_opname (GE_EXPR);
13924 break;
13926 case CPP_AND_AND:
13927 id = ansi_opname (TRUTH_ANDIF_EXPR);
13928 break;
13930 case CPP_OR_OR:
13931 id = ansi_opname (TRUTH_ORIF_EXPR);
13932 break;
13934 case CPP_PLUS_PLUS:
13935 id = ansi_opname (POSTINCREMENT_EXPR);
13936 break;
13938 case CPP_MINUS_MINUS:
13939 id = ansi_opname (PREDECREMENT_EXPR);
13940 break;
13942 case CPP_COMMA:
13943 id = ansi_opname (COMPOUND_EXPR);
13944 break;
13946 case CPP_DEREF_STAR:
13947 id = ansi_opname (MEMBER_REF);
13948 break;
13950 case CPP_DEREF:
13951 id = ansi_opname (COMPONENT_REF);
13952 break;
13954 case CPP_OPEN_PAREN:
13955 /* Consume the `('. */
13956 cp_lexer_consume_token (parser->lexer);
13957 /* Look for the matching `)'. */
13958 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
13959 return ansi_opname (CALL_EXPR);
13961 case CPP_OPEN_SQUARE:
13962 /* Consume the `['. */
13963 cp_lexer_consume_token (parser->lexer);
13964 /* Look for the matching `]'. */
13965 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
13966 return ansi_opname (ARRAY_REF);
13968 case CPP_UTF8STRING:
13969 case CPP_UTF8STRING_USERDEF:
13970 utf8 = true;
13971 case CPP_STRING:
13972 case CPP_WSTRING:
13973 case CPP_STRING16:
13974 case CPP_STRING32:
13975 case CPP_STRING_USERDEF:
13976 case CPP_WSTRING_USERDEF:
13977 case CPP_STRING16_USERDEF:
13978 case CPP_STRING32_USERDEF:
13980 tree str, string_tree;
13981 int sz, len;
13983 if (cxx_dialect == cxx98)
13984 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
13986 /* Consume the string. */
13987 str = cp_parser_string_literal (parser, /*translate=*/true,
13988 /*wide_ok=*/true, /*lookup_udlit=*/false);
13989 if (str == error_mark_node)
13990 return error_mark_node;
13991 else if (TREE_CODE (str) == USERDEF_LITERAL)
13993 string_tree = USERDEF_LITERAL_VALUE (str);
13994 id = USERDEF_LITERAL_SUFFIX_ID (str);
13996 else
13998 string_tree = str;
13999 /* Look for the suffix identifier. */
14000 token = cp_lexer_peek_token (parser->lexer);
14001 if (token->type == CPP_NAME)
14002 id = cp_parser_identifier (parser);
14003 else if (token->type == CPP_KEYWORD)
14005 error ("unexpected keyword;"
14006 " remove space between quotes and suffix identifier");
14007 return error_mark_node;
14009 else
14011 error ("expected suffix identifier");
14012 return error_mark_node;
14015 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14016 (TREE_TYPE (TREE_TYPE (string_tree))));
14017 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14018 if (len != 0)
14020 error ("expected empty string after %<operator%> keyword");
14021 return error_mark_node;
14023 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14024 != char_type_node)
14026 error ("invalid encoding prefix in literal operator");
14027 return error_mark_node;
14029 if (id != error_mark_node)
14031 const char *name = IDENTIFIER_POINTER (id);
14032 id = cp_literal_operator_id (name);
14034 return id;
14037 default:
14038 /* Anything else is an error. */
14039 break;
14042 /* If we have selected an identifier, we need to consume the
14043 operator token. */
14044 if (id)
14045 cp_lexer_consume_token (parser->lexer);
14046 /* Otherwise, no valid operator name was present. */
14047 else
14049 cp_parser_error (parser, "expected operator");
14050 id = error_mark_node;
14053 return cp_expr (id, start_loc);
14056 /* Parse a template-declaration.
14058 template-declaration:
14059 export [opt] template < template-parameter-list > declaration
14061 If MEMBER_P is TRUE, this template-declaration occurs within a
14062 class-specifier.
14064 The grammar rule given by the standard isn't correct. What
14065 is really meant is:
14067 template-declaration:
14068 export [opt] template-parameter-list-seq
14069 decl-specifier-seq [opt] init-declarator [opt] ;
14070 export [opt] template-parameter-list-seq
14071 function-definition
14073 template-parameter-list-seq:
14074 template-parameter-list-seq [opt]
14075 template < template-parameter-list >
14077 Concept Extensions:
14079 template-parameter-list-seq:
14080 template < template-parameter-list > requires-clause [opt]
14082 requires-clause:
14083 requires logical-or-expression */
14085 static void
14086 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14088 /* Check for `export'. */
14089 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14091 /* Consume the `export' token. */
14092 cp_lexer_consume_token (parser->lexer);
14093 /* Warn that we do not support `export'. */
14094 warning (0, "keyword %<export%> not implemented, and will be ignored");
14097 cp_parser_template_declaration_after_export (parser, member_p);
14100 /* Parse a template-parameter-list.
14102 template-parameter-list:
14103 template-parameter
14104 template-parameter-list , template-parameter
14106 Returns a TREE_LIST. Each node represents a template parameter.
14107 The nodes are connected via their TREE_CHAINs. */
14109 static tree
14110 cp_parser_template_parameter_list (cp_parser* parser)
14112 tree parameter_list = NULL_TREE;
14114 begin_template_parm_list ();
14116 /* The loop below parses the template parms. We first need to know
14117 the total number of template parms to be able to compute proper
14118 canonical types of each dependent type. So after the loop, when
14119 we know the total number of template parms,
14120 end_template_parm_list computes the proper canonical types and
14121 fixes up the dependent types accordingly. */
14122 while (true)
14124 tree parameter;
14125 bool is_non_type;
14126 bool is_parameter_pack;
14127 location_t parm_loc;
14129 /* Parse the template-parameter. */
14130 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14131 parameter = cp_parser_template_parameter (parser,
14132 &is_non_type,
14133 &is_parameter_pack);
14134 /* Add it to the list. */
14135 if (parameter != error_mark_node)
14136 parameter_list = process_template_parm (parameter_list,
14137 parm_loc,
14138 parameter,
14139 is_non_type,
14140 is_parameter_pack);
14141 else
14143 tree err_parm = build_tree_list (parameter, parameter);
14144 parameter_list = chainon (parameter_list, err_parm);
14147 /* If the next token is not a `,', we're done. */
14148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14149 break;
14150 /* Otherwise, consume the `,' token. */
14151 cp_lexer_consume_token (parser->lexer);
14154 return end_template_parm_list (parameter_list);
14157 /* Parse a introduction-list.
14159 introduction-list:
14160 introduced-parameter
14161 introduction-list , introduced-parameter
14163 introduced-parameter:
14164 ...[opt] identifier
14166 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14167 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14168 WILDCARD_DECL will also have DECL_NAME set and token location in
14169 DECL_SOURCE_LOCATION. */
14171 static tree
14172 cp_parser_introduction_list (cp_parser *parser)
14174 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14176 while (true)
14178 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14179 if (is_pack)
14180 cp_lexer_consume_token (parser->lexer);
14182 /* Build placeholder. */
14183 tree parm = build_nt (WILDCARD_DECL);
14184 DECL_SOURCE_LOCATION (parm)
14185 = cp_lexer_peek_token (parser->lexer)->location;
14186 DECL_NAME (parm) = cp_parser_identifier (parser);
14187 WILDCARD_PACK_P (parm) = is_pack;
14188 vec_safe_push (introduction_vec, parm);
14190 /* If the next token is not a `,', we're done. */
14191 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14192 break;
14193 /* Otherwise, consume the `,' token. */
14194 cp_lexer_consume_token (parser->lexer);
14197 /* Convert the vec into a TREE_VEC. */
14198 tree introduction_list = make_tree_vec (introduction_vec->length ());
14199 unsigned int n;
14200 tree parm;
14201 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14202 TREE_VEC_ELT (introduction_list, n) = parm;
14204 release_tree_vector (introduction_vec);
14205 return introduction_list;
14208 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14209 is an abstract declarator. */
14211 static inline cp_declarator*
14212 get_id_declarator (cp_declarator *declarator)
14214 cp_declarator *d = declarator;
14215 while (d && d->kind != cdk_id)
14216 d = d->declarator;
14217 return d;
14220 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14221 is an abstract declarator. */
14223 static inline tree
14224 get_unqualified_id (cp_declarator *declarator)
14226 declarator = get_id_declarator (declarator);
14227 if (declarator)
14228 return declarator->u.id.unqualified_name;
14229 else
14230 return NULL_TREE;
14233 /* Returns true if DECL represents a constrained-parameter. */
14235 static inline bool
14236 is_constrained_parameter (tree decl)
14238 return (decl
14239 && TREE_CODE (decl) == TYPE_DECL
14240 && CONSTRAINED_PARM_CONCEPT (decl)
14241 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14244 /* Returns true if PARM declares a constrained-parameter. */
14246 static inline bool
14247 is_constrained_parameter (cp_parameter_declarator *parm)
14249 return is_constrained_parameter (parm->decl_specifiers.type);
14252 /* Check that the type parameter is only a declarator-id, and that its
14253 type is not cv-qualified. */
14255 bool
14256 cp_parser_check_constrained_type_parm (cp_parser *parser,
14257 cp_parameter_declarator *parm)
14259 if (!parm->declarator)
14260 return true;
14262 if (parm->declarator->kind != cdk_id)
14264 cp_parser_error (parser, "invalid constrained type parameter");
14265 return false;
14268 /* Don't allow cv-qualified type parameters. */
14269 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14270 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14272 cp_parser_error (parser, "cv-qualified type parameter");
14273 return false;
14276 return true;
14279 /* Finish parsing/processing a template type parameter and checking
14280 various restrictions. */
14282 static inline tree
14283 cp_parser_constrained_type_template_parm (cp_parser *parser,
14284 tree id,
14285 cp_parameter_declarator* parmdecl)
14287 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14288 return finish_template_type_parm (class_type_node, id);
14289 else
14290 return error_mark_node;
14293 static tree
14294 finish_constrained_template_template_parm (tree proto, tree id)
14296 /* FIXME: This should probably be copied, and we may need to adjust
14297 the template parameter depths. */
14298 tree saved_parms = current_template_parms;
14299 begin_template_parm_list ();
14300 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14301 end_template_parm_list ();
14303 tree parm = finish_template_template_parm (class_type_node, id);
14304 current_template_parms = saved_parms;
14306 return parm;
14309 /* Finish parsing/processing a template template parameter by borrowing
14310 the template parameter list from the prototype parameter. */
14312 static tree
14313 cp_parser_constrained_template_template_parm (cp_parser *parser,
14314 tree proto,
14315 tree id,
14316 cp_parameter_declarator *parmdecl)
14318 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14319 return error_mark_node;
14320 return finish_constrained_template_template_parm (proto, id);
14323 /* Create a new non-type template parameter from the given PARM
14324 declarator. */
14326 static tree
14327 constrained_non_type_template_parm (bool *is_non_type,
14328 cp_parameter_declarator *parm)
14330 *is_non_type = true;
14331 cp_declarator *decl = parm->declarator;
14332 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14333 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14334 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14337 /* Build a constrained template parameter based on the PARMDECL
14338 declarator. The type of PARMDECL is the constrained type, which
14339 refers to the prototype template parameter that ultimately
14340 specifies the type of the declared parameter. */
14342 static tree
14343 finish_constrained_parameter (cp_parser *parser,
14344 cp_parameter_declarator *parmdecl,
14345 bool *is_non_type,
14346 bool *is_parameter_pack)
14348 tree decl = parmdecl->decl_specifiers.type;
14349 tree id = get_unqualified_id (parmdecl->declarator);
14350 tree def = parmdecl->default_argument;
14351 tree proto = DECL_INITIAL (decl);
14353 /* A template parameter constrained by a variadic concept shall also
14354 be declared as a template parameter pack. */
14355 bool is_variadic = template_parameter_pack_p (proto);
14356 if (is_variadic && !*is_parameter_pack)
14357 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14359 /* Build the parameter. Return an error if the declarator was invalid. */
14360 tree parm;
14361 if (TREE_CODE (proto) == TYPE_DECL)
14362 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14363 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14364 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14365 parmdecl);
14366 else
14367 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14368 if (parm == error_mark_node)
14369 return error_mark_node;
14371 /* Finish the parameter decl and create a node attaching the
14372 default argument and constraint. */
14373 parm = build_tree_list (def, parm);
14374 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14376 return parm;
14379 /* Returns true if the parsed type actually represents the declaration
14380 of a type template-parameter. */
14382 static inline bool
14383 declares_constrained_type_template_parameter (tree type)
14385 return (is_constrained_parameter (type)
14386 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14390 /* Returns true if the parsed type actually represents the declaration of
14391 a template template-parameter. */
14393 static bool
14394 declares_constrained_template_template_parameter (tree type)
14396 return (is_constrained_parameter (type)
14397 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14400 /* Parse a default argument for a type template-parameter.
14401 Note that diagnostics are handled in cp_parser_template_parameter. */
14403 static tree
14404 cp_parser_default_type_template_argument (cp_parser *parser)
14406 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14408 /* Consume the `=' token. */
14409 cp_lexer_consume_token (parser->lexer);
14411 cp_token *token = cp_lexer_peek_token (parser->lexer);
14413 /* Parse the default-argument. */
14414 push_deferring_access_checks (dk_no_deferred);
14415 tree default_argument = cp_parser_type_id (parser);
14416 pop_deferring_access_checks ();
14418 if (flag_concepts && type_uses_auto (default_argument))
14420 error_at (token->location,
14421 "invalid use of %<auto%> in default template argument");
14422 return error_mark_node;
14425 return default_argument;
14428 /* Parse a default argument for a template template-parameter. */
14430 static tree
14431 cp_parser_default_template_template_argument (cp_parser *parser)
14433 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
14435 bool is_template;
14437 /* Consume the `='. */
14438 cp_lexer_consume_token (parser->lexer);
14439 /* Parse the id-expression. */
14440 push_deferring_access_checks (dk_no_deferred);
14441 /* save token before parsing the id-expression, for error
14442 reporting */
14443 const cp_token* token = cp_lexer_peek_token (parser->lexer);
14444 tree default_argument
14445 = cp_parser_id_expression (parser,
14446 /*template_keyword_p=*/false,
14447 /*check_dependency_p=*/true,
14448 /*template_p=*/&is_template,
14449 /*declarator_p=*/false,
14450 /*optional_p=*/false);
14451 if (TREE_CODE (default_argument) == TYPE_DECL)
14452 /* If the id-expression was a template-id that refers to
14453 a template-class, we already have the declaration here,
14454 so no further lookup is needed. */
14456 else
14457 /* Look up the name. */
14458 default_argument
14459 = cp_parser_lookup_name (parser, default_argument,
14460 none_type,
14461 /*is_template=*/is_template,
14462 /*is_namespace=*/false,
14463 /*check_dependency=*/true,
14464 /*ambiguous_decls=*/NULL,
14465 token->location);
14466 /* See if the default argument is valid. */
14467 default_argument = check_template_template_default_arg (default_argument);
14468 pop_deferring_access_checks ();
14469 return default_argument;
14472 /* Parse a template-parameter.
14474 template-parameter:
14475 type-parameter
14476 parameter-declaration
14478 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
14479 the parameter. The TREE_PURPOSE is the default value, if any.
14480 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
14481 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
14482 set to true iff this parameter is a parameter pack. */
14484 static tree
14485 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
14486 bool *is_parameter_pack)
14488 cp_token *token;
14489 cp_parameter_declarator *parameter_declarator;
14490 tree parm;
14492 /* Assume it is a type parameter or a template parameter. */
14493 *is_non_type = false;
14494 /* Assume it not a parameter pack. */
14495 *is_parameter_pack = false;
14496 /* Peek at the next token. */
14497 token = cp_lexer_peek_token (parser->lexer);
14498 /* If it is `class' or `template', we have a type-parameter. */
14499 if (token->keyword == RID_TEMPLATE)
14500 return cp_parser_type_parameter (parser, is_parameter_pack);
14501 /* If it is `class' or `typename' we do not know yet whether it is a
14502 type parameter or a non-type parameter. Consider:
14504 template <typename T, typename T::X X> ...
14508 template <class C, class D*> ...
14510 Here, the first parameter is a type parameter, and the second is
14511 a non-type parameter. We can tell by looking at the token after
14512 the identifier -- if it is a `,', `=', or `>' then we have a type
14513 parameter. */
14514 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
14516 /* Peek at the token after `class' or `typename'. */
14517 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14518 /* If it's an ellipsis, we have a template type parameter
14519 pack. */
14520 if (token->type == CPP_ELLIPSIS)
14521 return cp_parser_type_parameter (parser, is_parameter_pack);
14522 /* If it's an identifier, skip it. */
14523 if (token->type == CPP_NAME)
14524 token = cp_lexer_peek_nth_token (parser->lexer, 3);
14525 /* Now, see if the token looks like the end of a template
14526 parameter. */
14527 if (token->type == CPP_COMMA
14528 || token->type == CPP_EQ
14529 || token->type == CPP_GREATER)
14530 return cp_parser_type_parameter (parser, is_parameter_pack);
14533 /* Otherwise, it is a non-type parameter or a constrained parameter.
14535 [temp.param]
14537 When parsing a default template-argument for a non-type
14538 template-parameter, the first non-nested `>' is taken as the end
14539 of the template parameter-list rather than a greater-than
14540 operator. */
14541 parameter_declarator
14542 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
14543 /*parenthesized_p=*/NULL);
14545 if (!parameter_declarator)
14546 return error_mark_node;
14548 /* If the parameter declaration is marked as a parameter pack, set
14549 *IS_PARAMETER_PACK to notify the caller. */
14550 if (parameter_declarator->template_parameter_pack_p)
14551 *is_parameter_pack = true;
14553 if (parameter_declarator->default_argument)
14555 /* Can happen in some cases of erroneous input (c++/34892). */
14556 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14557 /* Consume the `...' for better error recovery. */
14558 cp_lexer_consume_token (parser->lexer);
14561 // The parameter may have been constrained.
14562 if (is_constrained_parameter (parameter_declarator))
14563 return finish_constrained_parameter (parser,
14564 parameter_declarator,
14565 is_non_type,
14566 is_parameter_pack);
14568 // Now we're sure that the parameter is a non-type parameter.
14569 *is_non_type = true;
14571 parm = grokdeclarator (parameter_declarator->declarator,
14572 &parameter_declarator->decl_specifiers,
14573 TPARM, /*initialized=*/0,
14574 /*attrlist=*/NULL);
14575 if (parm == error_mark_node)
14576 return error_mark_node;
14578 return build_tree_list (parameter_declarator->default_argument, parm);
14581 /* Parse a type-parameter.
14583 type-parameter:
14584 class identifier [opt]
14585 class identifier [opt] = type-id
14586 typename identifier [opt]
14587 typename identifier [opt] = type-id
14588 template < template-parameter-list > class identifier [opt]
14589 template < template-parameter-list > class identifier [opt]
14590 = id-expression
14592 GNU Extension (variadic templates):
14594 type-parameter:
14595 class ... identifier [opt]
14596 typename ... identifier [opt]
14598 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
14599 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
14600 the declaration of the parameter.
14602 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
14604 static tree
14605 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
14607 cp_token *token;
14608 tree parameter;
14610 /* Look for a keyword to tell us what kind of parameter this is. */
14611 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
14612 if (!token)
14613 return error_mark_node;
14615 switch (token->keyword)
14617 case RID_CLASS:
14618 case RID_TYPENAME:
14620 tree identifier;
14621 tree default_argument;
14623 /* If the next token is an ellipsis, we have a template
14624 argument pack. */
14625 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14627 /* Consume the `...' token. */
14628 cp_lexer_consume_token (parser->lexer);
14629 maybe_warn_variadic_templates ();
14631 *is_parameter_pack = true;
14634 /* If the next token is an identifier, then it names the
14635 parameter. */
14636 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14637 identifier = cp_parser_identifier (parser);
14638 else
14639 identifier = NULL_TREE;
14641 /* Create the parameter. */
14642 parameter = finish_template_type_parm (class_type_node, identifier);
14644 /* If the next token is an `=', we have a default argument. */
14645 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14647 default_argument
14648 = cp_parser_default_type_template_argument (parser);
14650 /* Template parameter packs cannot have default
14651 arguments. */
14652 if (*is_parameter_pack)
14654 if (identifier)
14655 error_at (token->location,
14656 "template parameter pack %qD cannot have a "
14657 "default argument", identifier);
14658 else
14659 error_at (token->location,
14660 "template parameter packs cannot have "
14661 "default arguments");
14662 default_argument = NULL_TREE;
14664 else if (check_for_bare_parameter_packs (default_argument))
14665 default_argument = error_mark_node;
14667 else
14668 default_argument = NULL_TREE;
14670 /* Create the combined representation of the parameter and the
14671 default argument. */
14672 parameter = build_tree_list (default_argument, parameter);
14674 break;
14676 case RID_TEMPLATE:
14678 tree identifier;
14679 tree default_argument;
14681 /* Look for the `<'. */
14682 cp_parser_require (parser, CPP_LESS, RT_LESS);
14683 /* Parse the template-parameter-list. */
14684 cp_parser_template_parameter_list (parser);
14685 /* Look for the `>'. */
14686 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
14688 // If template requirements are present, parse them.
14689 tree reqs = get_shorthand_constraints (current_template_parms);
14690 if (tree r = cp_parser_requires_clause_opt (parser))
14691 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
14692 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
14694 /* Look for the `class' or 'typename' keywords. */
14695 cp_parser_type_parameter_key (parser);
14696 /* If the next token is an ellipsis, we have a template
14697 argument pack. */
14698 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14700 /* Consume the `...' token. */
14701 cp_lexer_consume_token (parser->lexer);
14702 maybe_warn_variadic_templates ();
14704 *is_parameter_pack = true;
14706 /* If the next token is an `=', then there is a
14707 default-argument. If the next token is a `>', we are at
14708 the end of the parameter-list. If the next token is a `,',
14709 then we are at the end of this parameter. */
14710 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
14711 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
14712 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14714 identifier = cp_parser_identifier (parser);
14715 /* Treat invalid names as if the parameter were nameless. */
14716 if (identifier == error_mark_node)
14717 identifier = NULL_TREE;
14719 else
14720 identifier = NULL_TREE;
14722 /* Create the template parameter. */
14723 parameter = finish_template_template_parm (class_type_node,
14724 identifier);
14726 /* If the next token is an `=', then there is a
14727 default-argument. */
14728 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14730 default_argument
14731 = cp_parser_default_template_template_argument (parser);
14733 /* Template parameter packs cannot have default
14734 arguments. */
14735 if (*is_parameter_pack)
14737 if (identifier)
14738 error_at (token->location,
14739 "template parameter pack %qD cannot "
14740 "have a default argument",
14741 identifier);
14742 else
14743 error_at (token->location, "template parameter packs cannot "
14744 "have default arguments");
14745 default_argument = NULL_TREE;
14748 else
14749 default_argument = NULL_TREE;
14751 /* Create the combined representation of the parameter and the
14752 default argument. */
14753 parameter = build_tree_list (default_argument, parameter);
14755 break;
14757 default:
14758 gcc_unreachable ();
14759 break;
14762 return parameter;
14765 /* Parse a template-id.
14767 template-id:
14768 template-name < template-argument-list [opt] >
14770 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
14771 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
14772 returned. Otherwise, if the template-name names a function, or set
14773 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
14774 names a class, returns a TYPE_DECL for the specialization.
14776 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
14777 uninstantiated templates. */
14779 static tree
14780 cp_parser_template_id (cp_parser *parser,
14781 bool template_keyword_p,
14782 bool check_dependency_p,
14783 enum tag_types tag_type,
14784 bool is_declaration)
14786 tree templ;
14787 tree arguments;
14788 tree template_id;
14789 cp_token_position start_of_id = 0;
14790 cp_token *next_token = NULL, *next_token_2 = NULL;
14791 bool is_identifier;
14793 /* If the next token corresponds to a template-id, there is no need
14794 to reparse it. */
14795 next_token = cp_lexer_peek_token (parser->lexer);
14796 if (next_token->type == CPP_TEMPLATE_ID)
14798 cp_lexer_consume_token (parser->lexer);
14799 return saved_checks_value (next_token->u.tree_check_value);
14802 /* Avoid performing name lookup if there is no possibility of
14803 finding a template-id. */
14804 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
14805 || (next_token->type == CPP_NAME
14806 && !cp_parser_nth_token_starts_template_argument_list_p
14807 (parser, 2)))
14809 cp_parser_error (parser, "expected template-id");
14810 return error_mark_node;
14813 /* Remember where the template-id starts. */
14814 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
14815 start_of_id = cp_lexer_token_position (parser->lexer, false);
14817 push_deferring_access_checks (dk_deferred);
14819 /* Parse the template-name. */
14820 is_identifier = false;
14821 templ = cp_parser_template_name (parser, template_keyword_p,
14822 check_dependency_p,
14823 is_declaration,
14824 tag_type,
14825 &is_identifier);
14826 if (templ == error_mark_node || is_identifier)
14828 pop_deferring_access_checks ();
14829 return templ;
14832 /* Since we're going to preserve any side-effects from this parse, set up a
14833 firewall to protect our callers from cp_parser_commit_to_tentative_parse
14834 in the template arguments. */
14835 tentative_firewall firewall (parser);
14837 /* If we find the sequence `[:' after a template-name, it's probably
14838 a digraph-typo for `< ::'. Substitute the tokens and check if we can
14839 parse correctly the argument list. */
14840 next_token = cp_lexer_peek_token (parser->lexer);
14841 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14842 if (next_token->type == CPP_OPEN_SQUARE
14843 && next_token->flags & DIGRAPH
14844 && next_token_2->type == CPP_COLON
14845 && !(next_token_2->flags & PREV_WHITE))
14847 cp_parser_parse_tentatively (parser);
14848 /* Change `:' into `::'. */
14849 next_token_2->type = CPP_SCOPE;
14850 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
14851 CPP_LESS. */
14852 cp_lexer_consume_token (parser->lexer);
14854 /* Parse the arguments. */
14855 arguments = cp_parser_enclosed_template_argument_list (parser);
14856 if (!cp_parser_parse_definitely (parser))
14858 /* If we couldn't parse an argument list, then we revert our changes
14859 and return simply an error. Maybe this is not a template-id
14860 after all. */
14861 next_token_2->type = CPP_COLON;
14862 cp_parser_error (parser, "expected %<<%>");
14863 pop_deferring_access_checks ();
14864 return error_mark_node;
14866 /* Otherwise, emit an error about the invalid digraph, but continue
14867 parsing because we got our argument list. */
14868 if (permerror (next_token->location,
14869 "%<<::%> cannot begin a template-argument list"))
14871 static bool hint = false;
14872 inform (next_token->location,
14873 "%<<:%> is an alternate spelling for %<[%>."
14874 " Insert whitespace between %<<%> and %<::%>");
14875 if (!hint && !flag_permissive)
14877 inform (next_token->location, "(if you use %<-fpermissive%> "
14878 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
14879 "accept your code)");
14880 hint = true;
14884 else
14886 /* Look for the `<' that starts the template-argument-list. */
14887 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
14889 pop_deferring_access_checks ();
14890 return error_mark_node;
14892 /* Parse the arguments. */
14893 arguments = cp_parser_enclosed_template_argument_list (parser);
14896 /* Build a representation of the specialization. */
14897 if (identifier_p (templ))
14898 template_id = build_min_nt_loc (next_token->location,
14899 TEMPLATE_ID_EXPR,
14900 templ, arguments);
14901 else if (DECL_TYPE_TEMPLATE_P (templ)
14902 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
14904 bool entering_scope;
14905 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
14906 template (rather than some instantiation thereof) only if
14907 is not nested within some other construct. For example, in
14908 "template <typename T> void f(T) { A<T>::", A<T> is just an
14909 instantiation of A. */
14910 entering_scope = (template_parm_scope_p ()
14911 && cp_lexer_next_token_is (parser->lexer,
14912 CPP_SCOPE));
14913 template_id
14914 = finish_template_type (templ, arguments, entering_scope);
14916 /* A template-like identifier may be a partial concept id. */
14917 else if (flag_concepts
14918 && (template_id = (cp_parser_maybe_partial_concept_id
14919 (parser, templ, arguments))))
14920 return template_id;
14921 else if (variable_template_p (templ))
14923 template_id = lookup_template_variable (templ, arguments);
14924 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14925 SET_EXPR_LOCATION (template_id, next_token->location);
14927 else
14929 /* If it's not a class-template or a template-template, it should be
14930 a function-template. */
14931 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
14932 || TREE_CODE (templ) == OVERLOAD
14933 || BASELINK_P (templ)));
14935 template_id = lookup_template_function (templ, arguments);
14936 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
14937 SET_EXPR_LOCATION (template_id, next_token->location);
14940 /* If parsing tentatively, replace the sequence of tokens that makes
14941 up the template-id with a CPP_TEMPLATE_ID token. That way,
14942 should we re-parse the token stream, we will not have to repeat
14943 the effort required to do the parse, nor will we issue duplicate
14944 error messages about problems during instantiation of the
14945 template. */
14946 if (start_of_id
14947 /* Don't do this if we had a parse error in a declarator; re-parsing
14948 might succeed if a name changes meaning (60361). */
14949 && !(cp_parser_error_occurred (parser)
14950 && cp_parser_parsing_tentatively (parser)
14951 && parser->in_declarator_p))
14953 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
14955 /* Reset the contents of the START_OF_ID token. */
14956 token->type = CPP_TEMPLATE_ID;
14958 /* Update the location to be of the form:
14959 template-name < template-argument-list [opt] >
14960 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14961 with caret == start at the start of the template-name,
14962 ranging until the closing '>'. */
14963 location_t finish_loc
14964 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
14965 location_t combined_loc
14966 = make_location (token->location, token->location, finish_loc);
14967 token->location = combined_loc;
14969 /* Retrieve any deferred checks. Do not pop this access checks yet
14970 so the memory will not be reclaimed during token replacing below. */
14971 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14972 token->u.tree_check_value->value = template_id;
14973 token->u.tree_check_value->checks = get_deferred_access_checks ();
14974 token->keyword = RID_MAX;
14976 /* Purge all subsequent tokens. */
14977 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
14979 /* ??? Can we actually assume that, if template_id ==
14980 error_mark_node, we will have issued a diagnostic to the
14981 user, as opposed to simply marking the tentative parse as
14982 failed? */
14983 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
14984 error_at (token->location, "parse error in template argument list");
14987 pop_to_parent_deferring_access_checks ();
14988 return template_id;
14991 /* Parse a template-name.
14993 template-name:
14994 identifier
14996 The standard should actually say:
14998 template-name:
14999 identifier
15000 operator-function-id
15002 A defect report has been filed about this issue.
15004 A conversion-function-id cannot be a template name because they cannot
15005 be part of a template-id. In fact, looking at this code:
15007 a.operator K<int>()
15009 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15010 It is impossible to call a templated conversion-function-id with an
15011 explicit argument list, since the only allowed template parameter is
15012 the type to which it is converting.
15014 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15015 `template' keyword, in a construction like:
15017 T::template f<3>()
15019 In that case `f' is taken to be a template-name, even though there
15020 is no way of knowing for sure.
15022 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15023 name refers to a set of overloaded functions, at least one of which
15024 is a template, or an IDENTIFIER_NODE with the name of the template,
15025 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15026 names are looked up inside uninstantiated templates. */
15028 static tree
15029 cp_parser_template_name (cp_parser* parser,
15030 bool template_keyword_p,
15031 bool check_dependency_p,
15032 bool is_declaration,
15033 enum tag_types tag_type,
15034 bool *is_identifier)
15036 tree identifier;
15037 tree decl;
15038 tree fns;
15039 cp_token *token = cp_lexer_peek_token (parser->lexer);
15041 /* If the next token is `operator', then we have either an
15042 operator-function-id or a conversion-function-id. */
15043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15045 /* We don't know whether we're looking at an
15046 operator-function-id or a conversion-function-id. */
15047 cp_parser_parse_tentatively (parser);
15048 /* Try an operator-function-id. */
15049 identifier = cp_parser_operator_function_id (parser);
15050 /* If that didn't work, try a conversion-function-id. */
15051 if (!cp_parser_parse_definitely (parser))
15053 cp_parser_error (parser, "expected template-name");
15054 return error_mark_node;
15057 /* Look for the identifier. */
15058 else
15059 identifier = cp_parser_identifier (parser);
15061 /* If we didn't find an identifier, we don't have a template-id. */
15062 if (identifier == error_mark_node)
15063 return error_mark_node;
15065 /* If the name immediately followed the `template' keyword, then it
15066 is a template-name. However, if the next token is not `<', then
15067 we do not treat it as a template-name, since it is not being used
15068 as part of a template-id. This enables us to handle constructs
15069 like:
15071 template <typename T> struct S { S(); };
15072 template <typename T> S<T>::S();
15074 correctly. We would treat `S' as a template -- if it were `S<T>'
15075 -- but we do not if there is no `<'. */
15077 if (processing_template_decl
15078 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15080 /* In a declaration, in a dependent context, we pretend that the
15081 "template" keyword was present in order to improve error
15082 recovery. For example, given:
15084 template <typename T> void f(T::X<int>);
15086 we want to treat "X<int>" as a template-id. */
15087 if (is_declaration
15088 && !template_keyword_p
15089 && parser->scope && TYPE_P (parser->scope)
15090 && check_dependency_p
15091 && dependent_scope_p (parser->scope)
15092 /* Do not do this for dtors (or ctors), since they never
15093 need the template keyword before their name. */
15094 && !constructor_name_p (identifier, parser->scope))
15096 cp_token_position start = 0;
15098 /* Explain what went wrong. */
15099 error_at (token->location, "non-template %qD used as template",
15100 identifier);
15101 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15102 parser->scope, identifier);
15103 /* If parsing tentatively, find the location of the "<" token. */
15104 if (cp_parser_simulate_error (parser))
15105 start = cp_lexer_token_position (parser->lexer, true);
15106 /* Parse the template arguments so that we can issue error
15107 messages about them. */
15108 cp_lexer_consume_token (parser->lexer);
15109 cp_parser_enclosed_template_argument_list (parser);
15110 /* Skip tokens until we find a good place from which to
15111 continue parsing. */
15112 cp_parser_skip_to_closing_parenthesis (parser,
15113 /*recovering=*/true,
15114 /*or_comma=*/true,
15115 /*consume_paren=*/false);
15116 /* If parsing tentatively, permanently remove the
15117 template argument list. That will prevent duplicate
15118 error messages from being issued about the missing
15119 "template" keyword. */
15120 if (start)
15121 cp_lexer_purge_tokens_after (parser->lexer, start);
15122 if (is_identifier)
15123 *is_identifier = true;
15124 return identifier;
15127 /* If the "template" keyword is present, then there is generally
15128 no point in doing name-lookup, so we just return IDENTIFIER.
15129 But, if the qualifying scope is non-dependent then we can
15130 (and must) do name-lookup normally. */
15131 if (template_keyword_p
15132 && (!parser->scope
15133 || (TYPE_P (parser->scope)
15134 && dependent_type_p (parser->scope))))
15135 return identifier;
15138 /* Look up the name. */
15139 decl = cp_parser_lookup_name (parser, identifier,
15140 tag_type,
15141 /*is_template=*/true,
15142 /*is_namespace=*/false,
15143 check_dependency_p,
15144 /*ambiguous_decls=*/NULL,
15145 token->location);
15147 decl = strip_using_decl (decl);
15149 /* If DECL is a template, then the name was a template-name. */
15150 if (TREE_CODE (decl) == TEMPLATE_DECL)
15152 if (TREE_DEPRECATED (decl)
15153 && deprecated_state != DEPRECATED_SUPPRESS)
15154 warn_deprecated_use (decl, NULL_TREE);
15156 else
15158 tree fn = NULL_TREE;
15160 /* The standard does not explicitly indicate whether a name that
15161 names a set of overloaded declarations, some of which are
15162 templates, is a template-name. However, such a name should
15163 be a template-name; otherwise, there is no way to form a
15164 template-id for the overloaded templates. */
15165 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15166 if (TREE_CODE (fns) == OVERLOAD)
15167 for (fn = fns; fn; fn = OVL_NEXT (fn))
15168 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15169 break;
15171 if (!fn)
15173 /* The name does not name a template. */
15174 cp_parser_error (parser, "expected template-name");
15175 return error_mark_node;
15179 /* If DECL is dependent, and refers to a function, then just return
15180 its name; we will look it up again during template instantiation. */
15181 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15183 tree scope = ovl_scope (decl);
15184 if (TYPE_P (scope) && dependent_type_p (scope))
15185 return identifier;
15188 return decl;
15191 /* Parse a template-argument-list.
15193 template-argument-list:
15194 template-argument ... [opt]
15195 template-argument-list , template-argument ... [opt]
15197 Returns a TREE_VEC containing the arguments. */
15199 static tree
15200 cp_parser_template_argument_list (cp_parser* parser)
15202 tree fixed_args[10];
15203 unsigned n_args = 0;
15204 unsigned alloced = 10;
15205 tree *arg_ary = fixed_args;
15206 tree vec;
15207 bool saved_in_template_argument_list_p;
15208 bool saved_ice_p;
15209 bool saved_non_ice_p;
15211 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15212 parser->in_template_argument_list_p = true;
15213 /* Even if the template-id appears in an integral
15214 constant-expression, the contents of the argument list do
15215 not. */
15216 saved_ice_p = parser->integral_constant_expression_p;
15217 parser->integral_constant_expression_p = false;
15218 saved_non_ice_p = parser->non_integral_constant_expression_p;
15219 parser->non_integral_constant_expression_p = false;
15221 /* Parse the arguments. */
15224 tree argument;
15226 if (n_args)
15227 /* Consume the comma. */
15228 cp_lexer_consume_token (parser->lexer);
15230 /* Parse the template-argument. */
15231 argument = cp_parser_template_argument (parser);
15233 /* If the next token is an ellipsis, we're expanding a template
15234 argument pack. */
15235 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15237 if (argument == error_mark_node)
15239 cp_token *token = cp_lexer_peek_token (parser->lexer);
15240 error_at (token->location,
15241 "expected parameter pack before %<...%>");
15243 /* Consume the `...' token. */
15244 cp_lexer_consume_token (parser->lexer);
15246 /* Make the argument into a TYPE_PACK_EXPANSION or
15247 EXPR_PACK_EXPANSION. */
15248 argument = make_pack_expansion (argument);
15251 if (n_args == alloced)
15253 alloced *= 2;
15255 if (arg_ary == fixed_args)
15257 arg_ary = XNEWVEC (tree, alloced);
15258 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15260 else
15261 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15263 arg_ary[n_args++] = argument;
15265 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15267 vec = make_tree_vec (n_args);
15269 while (n_args--)
15270 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15272 if (arg_ary != fixed_args)
15273 free (arg_ary);
15274 parser->non_integral_constant_expression_p = saved_non_ice_p;
15275 parser->integral_constant_expression_p = saved_ice_p;
15276 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15277 if (CHECKING_P)
15278 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15279 return vec;
15282 /* Parse a template-argument.
15284 template-argument:
15285 assignment-expression
15286 type-id
15287 id-expression
15289 The representation is that of an assignment-expression, type-id, or
15290 id-expression -- except that the qualified id-expression is
15291 evaluated, so that the value returned is either a DECL or an
15292 OVERLOAD.
15294 Although the standard says "assignment-expression", it forbids
15295 throw-expressions or assignments in the template argument.
15296 Therefore, we use "conditional-expression" instead. */
15298 static tree
15299 cp_parser_template_argument (cp_parser* parser)
15301 tree argument;
15302 bool template_p;
15303 bool address_p;
15304 bool maybe_type_id = false;
15305 cp_token *token = NULL, *argument_start_token = NULL;
15306 location_t loc = 0;
15307 cp_id_kind idk;
15309 /* There's really no way to know what we're looking at, so we just
15310 try each alternative in order.
15312 [temp.arg]
15314 In a template-argument, an ambiguity between a type-id and an
15315 expression is resolved to a type-id, regardless of the form of
15316 the corresponding template-parameter.
15318 Therefore, we try a type-id first. */
15319 cp_parser_parse_tentatively (parser);
15320 argument = cp_parser_template_type_arg (parser);
15321 /* If there was no error parsing the type-id but the next token is a
15322 '>>', our behavior depends on which dialect of C++ we're
15323 parsing. In C++98, we probably found a typo for '> >'. But there
15324 are type-id which are also valid expressions. For instance:
15326 struct X { int operator >> (int); };
15327 template <int V> struct Foo {};
15328 Foo<X () >> 5> r;
15330 Here 'X()' is a valid type-id of a function type, but the user just
15331 wanted to write the expression "X() >> 5". Thus, we remember that we
15332 found a valid type-id, but we still try to parse the argument as an
15333 expression to see what happens.
15335 In C++0x, the '>>' will be considered two separate '>'
15336 tokens. */
15337 if (!cp_parser_error_occurred (parser)
15338 && cxx_dialect == cxx98
15339 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15341 maybe_type_id = true;
15342 cp_parser_abort_tentative_parse (parser);
15344 else
15346 /* If the next token isn't a `,' or a `>', then this argument wasn't
15347 really finished. This means that the argument is not a valid
15348 type-id. */
15349 if (!cp_parser_next_token_ends_template_argument_p (parser))
15350 cp_parser_error (parser, "expected template-argument");
15351 /* If that worked, we're done. */
15352 if (cp_parser_parse_definitely (parser))
15353 return argument;
15355 /* We're still not sure what the argument will be. */
15356 cp_parser_parse_tentatively (parser);
15357 /* Try a template. */
15358 argument_start_token = cp_lexer_peek_token (parser->lexer);
15359 argument = cp_parser_id_expression (parser,
15360 /*template_keyword_p=*/false,
15361 /*check_dependency_p=*/true,
15362 &template_p,
15363 /*declarator_p=*/false,
15364 /*optional_p=*/false);
15365 /* If the next token isn't a `,' or a `>', then this argument wasn't
15366 really finished. */
15367 if (!cp_parser_next_token_ends_template_argument_p (parser))
15368 cp_parser_error (parser, "expected template-argument");
15369 if (!cp_parser_error_occurred (parser))
15371 /* Figure out what is being referred to. If the id-expression
15372 was for a class template specialization, then we will have a
15373 TYPE_DECL at this point. There is no need to do name lookup
15374 at this point in that case. */
15375 if (TREE_CODE (argument) != TYPE_DECL)
15376 argument = cp_parser_lookup_name (parser, argument,
15377 none_type,
15378 /*is_template=*/template_p,
15379 /*is_namespace=*/false,
15380 /*check_dependency=*/true,
15381 /*ambiguous_decls=*/NULL,
15382 argument_start_token->location);
15383 /* Handle a constrained-type-specifier for a non-type template
15384 parameter. */
15385 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15386 argument = decl;
15387 else if (TREE_CODE (argument) != TEMPLATE_DECL
15388 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15389 cp_parser_error (parser, "expected template-name");
15391 if (cp_parser_parse_definitely (parser))
15393 if (TREE_DEPRECATED (argument))
15394 warn_deprecated_use (argument, NULL_TREE);
15395 return argument;
15397 /* It must be a non-type argument. In C++17 any constant-expression is
15398 allowed. */
15399 if (cxx_dialect > cxx14)
15400 goto general_expr;
15402 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
15404 -- an integral constant-expression of integral or enumeration
15405 type; or
15407 -- the name of a non-type template-parameter; or
15409 -- the name of an object or function with external linkage...
15411 -- the address of an object or function with external linkage...
15413 -- a pointer to member... */
15414 /* Look for a non-type template parameter. */
15415 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15417 cp_parser_parse_tentatively (parser);
15418 argument = cp_parser_primary_expression (parser,
15419 /*address_p=*/false,
15420 /*cast_p=*/false,
15421 /*template_arg_p=*/true,
15422 &idk);
15423 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
15424 || !cp_parser_next_token_ends_template_argument_p (parser))
15425 cp_parser_simulate_error (parser);
15426 if (cp_parser_parse_definitely (parser))
15427 return argument;
15430 /* If the next token is "&", the argument must be the address of an
15431 object or function with external linkage. */
15432 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
15433 if (address_p)
15435 loc = cp_lexer_peek_token (parser->lexer)->location;
15436 cp_lexer_consume_token (parser->lexer);
15438 /* See if we might have an id-expression. */
15439 token = cp_lexer_peek_token (parser->lexer);
15440 if (token->type == CPP_NAME
15441 || token->keyword == RID_OPERATOR
15442 || token->type == CPP_SCOPE
15443 || token->type == CPP_TEMPLATE_ID
15444 || token->type == CPP_NESTED_NAME_SPECIFIER)
15446 cp_parser_parse_tentatively (parser);
15447 argument = cp_parser_primary_expression (parser,
15448 address_p,
15449 /*cast_p=*/false,
15450 /*template_arg_p=*/true,
15451 &idk);
15452 if (cp_parser_error_occurred (parser)
15453 || !cp_parser_next_token_ends_template_argument_p (parser))
15454 cp_parser_abort_tentative_parse (parser);
15455 else
15457 tree probe;
15459 if (INDIRECT_REF_P (argument))
15461 /* Strip the dereference temporarily. */
15462 gcc_assert (REFERENCE_REF_P (argument));
15463 argument = TREE_OPERAND (argument, 0);
15466 /* If we're in a template, we represent a qualified-id referring
15467 to a static data member as a SCOPE_REF even if the scope isn't
15468 dependent so that we can check access control later. */
15469 probe = argument;
15470 if (TREE_CODE (probe) == SCOPE_REF)
15471 probe = TREE_OPERAND (probe, 1);
15472 if (VAR_P (probe))
15474 /* A variable without external linkage might still be a
15475 valid constant-expression, so no error is issued here
15476 if the external-linkage check fails. */
15477 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
15478 cp_parser_simulate_error (parser);
15480 else if (is_overloaded_fn (argument))
15481 /* All overloaded functions are allowed; if the external
15482 linkage test does not pass, an error will be issued
15483 later. */
15485 else if (address_p
15486 && (TREE_CODE (argument) == OFFSET_REF
15487 || TREE_CODE (argument) == SCOPE_REF))
15488 /* A pointer-to-member. */
15490 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
15492 else
15493 cp_parser_simulate_error (parser);
15495 if (cp_parser_parse_definitely (parser))
15497 if (address_p)
15498 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
15499 tf_warning_or_error);
15500 else
15501 argument = convert_from_reference (argument);
15502 return argument;
15506 /* If the argument started with "&", there are no other valid
15507 alternatives at this point. */
15508 if (address_p)
15510 cp_parser_error (parser, "invalid non-type template argument");
15511 return error_mark_node;
15514 general_expr:
15515 /* If the argument wasn't successfully parsed as a type-id followed
15516 by '>>', the argument can only be a constant expression now.
15517 Otherwise, we try parsing the constant-expression tentatively,
15518 because the argument could really be a type-id. */
15519 if (maybe_type_id)
15520 cp_parser_parse_tentatively (parser);
15522 if (cxx_dialect <= cxx14)
15523 argument = cp_parser_constant_expression (parser);
15524 else
15526 /* With C++17 generalized non-type template arguments we need to handle
15527 lvalue constant expressions, too. */
15528 argument = cp_parser_assignment_expression (parser);
15529 require_potential_constant_expression (argument);
15532 if (!maybe_type_id)
15533 return argument;
15534 if (!cp_parser_next_token_ends_template_argument_p (parser))
15535 cp_parser_error (parser, "expected template-argument");
15536 if (cp_parser_parse_definitely (parser))
15537 return argument;
15538 /* We did our best to parse the argument as a non type-id, but that
15539 was the only alternative that matched (albeit with a '>' after
15540 it). We can assume it's just a typo from the user, and a
15541 diagnostic will then be issued. */
15542 return cp_parser_template_type_arg (parser);
15545 /* Parse an explicit-instantiation.
15547 explicit-instantiation:
15548 template declaration
15550 Although the standard says `declaration', what it really means is:
15552 explicit-instantiation:
15553 template decl-specifier-seq [opt] declarator [opt] ;
15555 Things like `template int S<int>::i = 5, int S<double>::j;' are not
15556 supposed to be allowed. A defect report has been filed about this
15557 issue.
15559 GNU Extension:
15561 explicit-instantiation:
15562 storage-class-specifier template
15563 decl-specifier-seq [opt] declarator [opt] ;
15564 function-specifier template
15565 decl-specifier-seq [opt] declarator [opt] ; */
15567 static void
15568 cp_parser_explicit_instantiation (cp_parser* parser)
15570 int declares_class_or_enum;
15571 cp_decl_specifier_seq decl_specifiers;
15572 tree extension_specifier = NULL_TREE;
15574 timevar_push (TV_TEMPLATE_INST);
15576 /* Look for an (optional) storage-class-specifier or
15577 function-specifier. */
15578 if (cp_parser_allow_gnu_extensions_p (parser))
15580 extension_specifier
15581 = cp_parser_storage_class_specifier_opt (parser);
15582 if (!extension_specifier)
15583 extension_specifier
15584 = cp_parser_function_specifier_opt (parser,
15585 /*decl_specs=*/NULL);
15588 /* Look for the `template' keyword. */
15589 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15590 /* Let the front end know that we are processing an explicit
15591 instantiation. */
15592 begin_explicit_instantiation ();
15593 /* [temp.explicit] says that we are supposed to ignore access
15594 control while processing explicit instantiation directives. */
15595 push_deferring_access_checks (dk_no_check);
15596 /* Parse a decl-specifier-seq. */
15597 cp_parser_decl_specifier_seq (parser,
15598 CP_PARSER_FLAGS_OPTIONAL,
15599 &decl_specifiers,
15600 &declares_class_or_enum);
15601 /* If there was exactly one decl-specifier, and it declared a class,
15602 and there's no declarator, then we have an explicit type
15603 instantiation. */
15604 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
15606 tree type;
15608 type = check_tag_decl (&decl_specifiers,
15609 /*explicit_type_instantiation_p=*/true);
15610 /* Turn access control back on for names used during
15611 template instantiation. */
15612 pop_deferring_access_checks ();
15613 if (type)
15614 do_type_instantiation (type, extension_specifier,
15615 /*complain=*/tf_error);
15617 else
15619 cp_declarator *declarator;
15620 tree decl;
15622 /* Parse the declarator. */
15623 declarator
15624 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15625 /*ctor_dtor_or_conv_p=*/NULL,
15626 /*parenthesized_p=*/NULL,
15627 /*member_p=*/false,
15628 /*friend_p=*/false);
15629 if (declares_class_or_enum & 2)
15630 cp_parser_check_for_definition_in_return_type (declarator,
15631 decl_specifiers.type,
15632 decl_specifiers.locations[ds_type_spec]);
15633 if (declarator != cp_error_declarator)
15635 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
15636 permerror (decl_specifiers.locations[ds_inline],
15637 "explicit instantiation shall not use"
15638 " %<inline%> specifier");
15639 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
15640 permerror (decl_specifiers.locations[ds_constexpr],
15641 "explicit instantiation shall not use"
15642 " %<constexpr%> specifier");
15644 decl = grokdeclarator (declarator, &decl_specifiers,
15645 NORMAL, 0, &decl_specifiers.attributes);
15646 /* Turn access control back on for names used during
15647 template instantiation. */
15648 pop_deferring_access_checks ();
15649 /* Do the explicit instantiation. */
15650 do_decl_instantiation (decl, extension_specifier);
15652 else
15654 pop_deferring_access_checks ();
15655 /* Skip the body of the explicit instantiation. */
15656 cp_parser_skip_to_end_of_statement (parser);
15659 /* We're done with the instantiation. */
15660 end_explicit_instantiation ();
15662 cp_parser_consume_semicolon_at_end_of_statement (parser);
15664 timevar_pop (TV_TEMPLATE_INST);
15667 /* Parse an explicit-specialization.
15669 explicit-specialization:
15670 template < > declaration
15672 Although the standard says `declaration', what it really means is:
15674 explicit-specialization:
15675 template <> decl-specifier [opt] init-declarator [opt] ;
15676 template <> function-definition
15677 template <> explicit-specialization
15678 template <> template-declaration */
15680 static void
15681 cp_parser_explicit_specialization (cp_parser* parser)
15683 bool need_lang_pop;
15684 cp_token *token = cp_lexer_peek_token (parser->lexer);
15686 /* Look for the `template' keyword. */
15687 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
15688 /* Look for the `<'. */
15689 cp_parser_require (parser, CPP_LESS, RT_LESS);
15690 /* Look for the `>'. */
15691 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15692 /* We have processed another parameter list. */
15693 ++parser->num_template_parameter_lists;
15694 /* [temp]
15696 A template ... explicit specialization ... shall not have C
15697 linkage. */
15698 if (current_lang_name == lang_name_c)
15700 error_at (token->location, "template specialization with C linkage");
15701 /* Give it C++ linkage to avoid confusing other parts of the
15702 front end. */
15703 push_lang_context (lang_name_cplusplus);
15704 need_lang_pop = true;
15706 else
15707 need_lang_pop = false;
15708 /* Let the front end know that we are beginning a specialization. */
15709 if (!begin_specialization ())
15711 end_specialization ();
15712 return;
15715 /* If the next keyword is `template', we need to figure out whether
15716 or not we're looking a template-declaration. */
15717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15719 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15720 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
15721 cp_parser_template_declaration_after_export (parser,
15722 /*member_p=*/false);
15723 else
15724 cp_parser_explicit_specialization (parser);
15726 else
15727 /* Parse the dependent declaration. */
15728 cp_parser_single_declaration (parser,
15729 /*checks=*/NULL,
15730 /*member_p=*/false,
15731 /*explicit_specialization_p=*/true,
15732 /*friend_p=*/NULL);
15733 /* We're done with the specialization. */
15734 end_specialization ();
15735 /* For the erroneous case of a template with C linkage, we pushed an
15736 implicit C++ linkage scope; exit that scope now. */
15737 if (need_lang_pop)
15738 pop_lang_context ();
15739 /* We're done with this parameter list. */
15740 --parser->num_template_parameter_lists;
15743 /* Parse a type-specifier.
15745 type-specifier:
15746 simple-type-specifier
15747 class-specifier
15748 enum-specifier
15749 elaborated-type-specifier
15750 cv-qualifier
15752 GNU Extension:
15754 type-specifier:
15755 __complex__
15757 Returns a representation of the type-specifier. For a
15758 class-specifier, enum-specifier, or elaborated-type-specifier, a
15759 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
15761 The parser flags FLAGS is used to control type-specifier parsing.
15763 If IS_DECLARATION is TRUE, then this type-specifier is appearing
15764 in a decl-specifier-seq.
15766 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
15767 class-specifier, enum-specifier, or elaborated-type-specifier, then
15768 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
15769 if a type is declared; 2 if it is defined. Otherwise, it is set to
15770 zero.
15772 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
15773 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
15774 is set to FALSE. */
15776 static tree
15777 cp_parser_type_specifier (cp_parser* parser,
15778 cp_parser_flags flags,
15779 cp_decl_specifier_seq *decl_specs,
15780 bool is_declaration,
15781 int* declares_class_or_enum,
15782 bool* is_cv_qualifier)
15784 tree type_spec = NULL_TREE;
15785 cp_token *token;
15786 enum rid keyword;
15787 cp_decl_spec ds = ds_last;
15789 /* Assume this type-specifier does not declare a new type. */
15790 if (declares_class_or_enum)
15791 *declares_class_or_enum = 0;
15792 /* And that it does not specify a cv-qualifier. */
15793 if (is_cv_qualifier)
15794 *is_cv_qualifier = false;
15795 /* Peek at the next token. */
15796 token = cp_lexer_peek_token (parser->lexer);
15798 /* If we're looking at a keyword, we can use that to guide the
15799 production we choose. */
15800 keyword = token->keyword;
15801 switch (keyword)
15803 case RID_ENUM:
15804 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15805 goto elaborated_type_specifier;
15807 /* Look for the enum-specifier. */
15808 type_spec = cp_parser_enum_specifier (parser);
15809 /* If that worked, we're done. */
15810 if (type_spec)
15812 if (declares_class_or_enum)
15813 *declares_class_or_enum = 2;
15814 if (decl_specs)
15815 cp_parser_set_decl_spec_type (decl_specs,
15816 type_spec,
15817 token,
15818 /*type_definition_p=*/true);
15819 return type_spec;
15821 else
15822 goto elaborated_type_specifier;
15824 /* Any of these indicate either a class-specifier, or an
15825 elaborated-type-specifier. */
15826 case RID_CLASS:
15827 case RID_STRUCT:
15828 case RID_UNION:
15829 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
15830 goto elaborated_type_specifier;
15832 /* Parse tentatively so that we can back up if we don't find a
15833 class-specifier. */
15834 cp_parser_parse_tentatively (parser);
15835 /* Look for the class-specifier. */
15836 type_spec = cp_parser_class_specifier (parser);
15837 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
15838 /* If that worked, we're done. */
15839 if (cp_parser_parse_definitely (parser))
15841 if (declares_class_or_enum)
15842 *declares_class_or_enum = 2;
15843 if (decl_specs)
15844 cp_parser_set_decl_spec_type (decl_specs,
15845 type_spec,
15846 token,
15847 /*type_definition_p=*/true);
15848 return type_spec;
15851 /* Fall through. */
15852 elaborated_type_specifier:
15853 /* We're declaring (not defining) a class or enum. */
15854 if (declares_class_or_enum)
15855 *declares_class_or_enum = 1;
15857 /* Fall through. */
15858 case RID_TYPENAME:
15859 /* Look for an elaborated-type-specifier. */
15860 type_spec
15861 = (cp_parser_elaborated_type_specifier
15862 (parser,
15863 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
15864 is_declaration));
15865 if (decl_specs)
15866 cp_parser_set_decl_spec_type (decl_specs,
15867 type_spec,
15868 token,
15869 /*type_definition_p=*/false);
15870 return type_spec;
15872 case RID_CONST:
15873 ds = ds_const;
15874 if (is_cv_qualifier)
15875 *is_cv_qualifier = true;
15876 break;
15878 case RID_VOLATILE:
15879 ds = ds_volatile;
15880 if (is_cv_qualifier)
15881 *is_cv_qualifier = true;
15882 break;
15884 case RID_RESTRICT:
15885 ds = ds_restrict;
15886 if (is_cv_qualifier)
15887 *is_cv_qualifier = true;
15888 break;
15890 case RID_COMPLEX:
15891 /* The `__complex__' keyword is a GNU extension. */
15892 ds = ds_complex;
15893 break;
15895 default:
15896 break;
15899 /* Handle simple keywords. */
15900 if (ds != ds_last)
15902 if (decl_specs)
15904 set_and_check_decl_spec_loc (decl_specs, ds, token);
15905 decl_specs->any_specifiers_p = true;
15907 return cp_lexer_consume_token (parser->lexer)->u.value;
15910 /* If we do not already have a type-specifier, assume we are looking
15911 at a simple-type-specifier. */
15912 type_spec = cp_parser_simple_type_specifier (parser,
15913 decl_specs,
15914 flags);
15916 /* If we didn't find a type-specifier, and a type-specifier was not
15917 optional in this context, issue an error message. */
15918 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
15920 cp_parser_error (parser, "expected type specifier");
15921 return error_mark_node;
15924 return type_spec;
15927 /* Parse a simple-type-specifier.
15929 simple-type-specifier:
15930 :: [opt] nested-name-specifier [opt] type-name
15931 :: [opt] nested-name-specifier template template-id
15932 char
15933 wchar_t
15934 bool
15935 short
15937 long
15938 signed
15939 unsigned
15940 float
15941 double
15942 void
15944 C++0x Extension:
15946 simple-type-specifier:
15947 auto
15948 decltype ( expression )
15949 char16_t
15950 char32_t
15951 __underlying_type ( type-id )
15953 GNU Extension:
15955 simple-type-specifier:
15956 __int128
15957 __typeof__ unary-expression
15958 __typeof__ ( type-id )
15959 __typeof__ ( type-id ) { initializer-list , [opt] }
15961 Concepts Extension:
15963 simple-type-specifier:
15964 constrained-type-specifier
15966 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
15967 appropriately updated. */
15969 static tree
15970 cp_parser_simple_type_specifier (cp_parser* parser,
15971 cp_decl_specifier_seq *decl_specs,
15972 cp_parser_flags flags)
15974 tree type = NULL_TREE;
15975 cp_token *token;
15976 int idx;
15978 /* Peek at the next token. */
15979 token = cp_lexer_peek_token (parser->lexer);
15981 /* If we're looking at a keyword, things are easy. */
15982 switch (token->keyword)
15984 case RID_CHAR:
15985 if (decl_specs)
15986 decl_specs->explicit_char_p = true;
15987 type = char_type_node;
15988 break;
15989 case RID_CHAR16:
15990 type = char16_type_node;
15991 break;
15992 case RID_CHAR32:
15993 type = char32_type_node;
15994 break;
15995 case RID_WCHAR:
15996 type = wchar_type_node;
15997 break;
15998 case RID_BOOL:
15999 type = boolean_type_node;
16000 break;
16001 case RID_SHORT:
16002 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16003 type = short_integer_type_node;
16004 break;
16005 case RID_INT:
16006 if (decl_specs)
16007 decl_specs->explicit_int_p = true;
16008 type = integer_type_node;
16009 break;
16010 case RID_INT_N_0:
16011 case RID_INT_N_1:
16012 case RID_INT_N_2:
16013 case RID_INT_N_3:
16014 idx = token->keyword - RID_INT_N_0;
16015 if (! int_n_enabled_p [idx])
16016 break;
16017 if (decl_specs)
16019 decl_specs->explicit_intN_p = true;
16020 decl_specs->int_n_idx = idx;
16022 type = int_n_trees [idx].signed_type;
16023 break;
16024 case RID_LONG:
16025 if (decl_specs)
16026 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16027 type = long_integer_type_node;
16028 break;
16029 case RID_SIGNED:
16030 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16031 type = integer_type_node;
16032 break;
16033 case RID_UNSIGNED:
16034 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16035 type = unsigned_type_node;
16036 break;
16037 case RID_FLOAT:
16038 type = float_type_node;
16039 break;
16040 case RID_DOUBLE:
16041 type = double_type_node;
16042 break;
16043 case RID_VOID:
16044 type = void_type_node;
16045 break;
16047 case RID_AUTO:
16048 maybe_warn_cpp0x (CPP0X_AUTO);
16049 if (parser->auto_is_implicit_function_template_parm_p)
16051 /* The 'auto' might be the placeholder return type for a function decl
16052 with trailing return type. */
16053 bool have_trailing_return_fn_decl = false;
16055 cp_parser_parse_tentatively (parser);
16056 cp_lexer_consume_token (parser->lexer);
16057 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16058 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16059 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16060 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16062 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16064 cp_lexer_consume_token (parser->lexer);
16065 cp_parser_skip_to_closing_parenthesis (parser,
16066 /*recovering*/false,
16067 /*or_comma*/false,
16068 /*consume_paren*/true);
16069 continue;
16072 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16074 have_trailing_return_fn_decl = true;
16075 break;
16078 cp_lexer_consume_token (parser->lexer);
16080 cp_parser_abort_tentative_parse (parser);
16082 if (have_trailing_return_fn_decl)
16084 type = make_auto ();
16085 break;
16088 if (cxx_dialect >= cxx14)
16090 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16091 type = TREE_TYPE (type);
16093 else
16094 type = error_mark_node;
16096 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16098 if (cxx_dialect < cxx14)
16099 error_at (token->location,
16100 "use of %<auto%> in lambda parameter declaration "
16101 "only available with "
16102 "-std=c++14 or -std=gnu++14");
16104 else if (cxx_dialect < cxx14)
16105 error_at (token->location,
16106 "use of %<auto%> in parameter declaration "
16107 "only available with "
16108 "-std=c++14 or -std=gnu++14");
16109 else if (!flag_concepts)
16110 pedwarn (token->location, OPT_Wpedantic,
16111 "ISO C++ forbids use of %<auto%> in parameter "
16112 "declaration");
16114 else
16115 type = make_auto ();
16116 break;
16118 case RID_DECLTYPE:
16119 /* Since DR 743, decltype can either be a simple-type-specifier by
16120 itself or begin a nested-name-specifier. Parsing it will replace
16121 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16122 handling below decide what to do. */
16123 cp_parser_decltype (parser);
16124 cp_lexer_set_token_position (parser->lexer, token);
16125 break;
16127 case RID_TYPEOF:
16128 /* Consume the `typeof' token. */
16129 cp_lexer_consume_token (parser->lexer);
16130 /* Parse the operand to `typeof'. */
16131 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16132 /* If it is not already a TYPE, take its type. */
16133 if (!TYPE_P (type))
16134 type = finish_typeof (type);
16136 if (decl_specs)
16137 cp_parser_set_decl_spec_type (decl_specs, type,
16138 token,
16139 /*type_definition_p=*/false);
16141 return type;
16143 case RID_UNDERLYING_TYPE:
16144 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16145 if (decl_specs)
16146 cp_parser_set_decl_spec_type (decl_specs, type,
16147 token,
16148 /*type_definition_p=*/false);
16150 return type;
16152 case RID_BASES:
16153 case RID_DIRECT_BASES:
16154 type = cp_parser_trait_expr (parser, token->keyword);
16155 if (decl_specs)
16156 cp_parser_set_decl_spec_type (decl_specs, type,
16157 token,
16158 /*type_definition_p=*/false);
16159 return type;
16160 default:
16161 break;
16164 /* If token is an already-parsed decltype not followed by ::,
16165 it's a simple-type-specifier. */
16166 if (token->type == CPP_DECLTYPE
16167 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16169 type = saved_checks_value (token->u.tree_check_value);
16170 if (decl_specs)
16172 cp_parser_set_decl_spec_type (decl_specs, type,
16173 token,
16174 /*type_definition_p=*/false);
16175 /* Remember that we are handling a decltype in order to
16176 implement the resolution of DR 1510 when the argument
16177 isn't instantiation dependent. */
16178 decl_specs->decltype_p = true;
16180 cp_lexer_consume_token (parser->lexer);
16181 return type;
16184 /* If the type-specifier was for a built-in type, we're done. */
16185 if (type)
16187 /* Record the type. */
16188 if (decl_specs
16189 && (token->keyword != RID_SIGNED
16190 && token->keyword != RID_UNSIGNED
16191 && token->keyword != RID_SHORT
16192 && token->keyword != RID_LONG))
16193 cp_parser_set_decl_spec_type (decl_specs,
16194 type,
16195 token,
16196 /*type_definition_p=*/false);
16197 if (decl_specs)
16198 decl_specs->any_specifiers_p = true;
16200 /* Consume the token. */
16201 cp_lexer_consume_token (parser->lexer);
16203 if (type == error_mark_node)
16204 return error_mark_node;
16206 /* There is no valid C++ program where a non-template type is
16207 followed by a "<". That usually indicates that the user thought
16208 that the type was a template. */
16209 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16210 token->location);
16212 return TYPE_NAME (type);
16215 /* The type-specifier must be a user-defined type. */
16216 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16218 bool qualified_p;
16219 bool global_p;
16221 /* Don't gobble tokens or issue error messages if this is an
16222 optional type-specifier. */
16223 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16224 cp_parser_parse_tentatively (parser);
16226 /* Look for the optional `::' operator. */
16227 global_p
16228 = (cp_parser_global_scope_opt (parser,
16229 /*current_scope_valid_p=*/false)
16230 != NULL_TREE);
16231 /* Look for the nested-name specifier. */
16232 qualified_p
16233 = (cp_parser_nested_name_specifier_opt (parser,
16234 /*typename_keyword_p=*/false,
16235 /*check_dependency_p=*/true,
16236 /*type_p=*/false,
16237 /*is_declaration=*/false)
16238 != NULL_TREE);
16239 token = cp_lexer_peek_token (parser->lexer);
16240 /* If we have seen a nested-name-specifier, and the next token
16241 is `template', then we are using the template-id production. */
16242 if (parser->scope
16243 && cp_parser_optional_template_keyword (parser))
16245 /* Look for the template-id. */
16246 type = cp_parser_template_id (parser,
16247 /*template_keyword_p=*/true,
16248 /*check_dependency_p=*/true,
16249 none_type,
16250 /*is_declaration=*/false);
16251 /* If the template-id did not name a type, we are out of
16252 luck. */
16253 if (TREE_CODE (type) != TYPE_DECL)
16255 cp_parser_error (parser, "expected template-id for type");
16256 type = NULL_TREE;
16259 /* Otherwise, look for a type-name. */
16260 else
16261 type = cp_parser_type_name (parser);
16262 /* Keep track of all name-lookups performed in class scopes. */
16263 if (type
16264 && !global_p
16265 && !qualified_p
16266 && TREE_CODE (type) == TYPE_DECL
16267 && identifier_p (DECL_NAME (type)))
16268 maybe_note_name_used_in_class (DECL_NAME (type), type);
16269 /* If it didn't work out, we don't have a TYPE. */
16270 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16271 && !cp_parser_parse_definitely (parser))
16272 type = NULL_TREE;
16273 if (type && decl_specs)
16274 cp_parser_set_decl_spec_type (decl_specs, type,
16275 token,
16276 /*type_definition_p=*/false);
16279 /* If we didn't get a type-name, issue an error message. */
16280 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16282 cp_parser_error (parser, "expected type-name");
16283 return error_mark_node;
16286 if (type && type != error_mark_node)
16288 /* See if TYPE is an Objective-C type, and if so, parse and
16289 accept any protocol references following it. Do this before
16290 the cp_parser_check_for_invalid_template_id() call, because
16291 Objective-C types can be followed by '<...>' which would
16292 enclose protocol names rather than template arguments, and so
16293 everything is fine. */
16294 if (c_dialect_objc () && !parser->scope
16295 && (objc_is_id (type) || objc_is_class_name (type)))
16297 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16298 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16300 /* Clobber the "unqualified" type previously entered into
16301 DECL_SPECS with the new, improved protocol-qualified version. */
16302 if (decl_specs)
16303 decl_specs->type = qual_type;
16305 return qual_type;
16308 /* There is no valid C++ program where a non-template type is
16309 followed by a "<". That usually indicates that the user
16310 thought that the type was a template. */
16311 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16312 none_type,
16313 token->location);
16316 return type;
16319 /* Parse a type-name.
16321 type-name:
16322 class-name
16323 enum-name
16324 typedef-name
16325 simple-template-id [in c++0x]
16327 enum-name:
16328 identifier
16330 typedef-name:
16331 identifier
16333 Concepts:
16335 type-name:
16336 concept-name
16337 partial-concept-id
16339 concept-name:
16340 identifier
16342 Returns a TYPE_DECL for the type. */
16344 static tree
16345 cp_parser_type_name (cp_parser* parser)
16347 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16350 /* See above. */
16351 static tree
16352 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16354 tree type_decl;
16356 /* We can't know yet whether it is a class-name or not. */
16357 cp_parser_parse_tentatively (parser);
16358 /* Try a class-name. */
16359 type_decl = cp_parser_class_name (parser,
16360 typename_keyword_p,
16361 /*template_keyword_p=*/false,
16362 none_type,
16363 /*check_dependency_p=*/true,
16364 /*class_head_p=*/false,
16365 /*is_declaration=*/false);
16366 /* If it's not a class-name, keep looking. */
16367 if (!cp_parser_parse_definitely (parser))
16369 if (cxx_dialect < cxx11)
16370 /* It must be a typedef-name or an enum-name. */
16371 return cp_parser_nonclass_name (parser);
16373 cp_parser_parse_tentatively (parser);
16374 /* It is either a simple-template-id representing an
16375 instantiation of an alias template... */
16376 type_decl = cp_parser_template_id (parser,
16377 /*template_keyword_p=*/false,
16378 /*check_dependency_p=*/true,
16379 none_type,
16380 /*is_declaration=*/false);
16381 /* Note that this must be an instantiation of an alias template
16382 because [temp.names]/6 says:
16384 A template-id that names an alias template specialization
16385 is a type-name.
16387 Whereas [temp.names]/7 says:
16389 A simple-template-id that names a class template
16390 specialization is a class-name.
16392 With concepts, this could also be a partial-concept-id that
16393 declares a non-type template parameter. */
16394 if (type_decl != NULL_TREE
16395 && TREE_CODE (type_decl) == TYPE_DECL
16396 && TYPE_DECL_ALIAS_P (type_decl))
16397 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
16398 else if (is_constrained_parameter (type_decl))
16399 /* Don't do anything. */ ;
16400 else
16401 cp_parser_simulate_error (parser);
16403 if (!cp_parser_parse_definitely (parser))
16404 /* ... Or a typedef-name or an enum-name. */
16405 return cp_parser_nonclass_name (parser);
16408 return type_decl;
16411 /* Check if DECL and ARGS can form a constrained-type-specifier.
16412 If ARGS is non-null, we try to form a concept check of the
16413 form DECL<?, ARGS> where ? is a wildcard that matches any
16414 kind of template argument. If ARGS is NULL, then we try to
16415 form a concept check of the form DECL<?>. */
16417 static tree
16418 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
16419 tree decl, tree args)
16421 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
16423 /* If we a constrained-type-specifier cannot be deduced. */
16424 if (parser->prevent_constrained_type_specifiers)
16425 return NULL_TREE;
16427 /* A constrained type specifier can only be found in an
16428 overload set or as a reference to a template declaration.
16430 FIXME: This might be masking a bug. It's possible that
16431 that the deduction below is causing template specializations
16432 to be formed with the wildcard as an argument. */
16433 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
16434 return NULL_TREE;
16436 /* Try to build a call expression that evaluates the
16437 concept. This can fail if the overload set refers
16438 only to non-templates. */
16439 tree placeholder = build_nt (WILDCARD_DECL);
16440 tree check = build_concept_check (decl, placeholder, args);
16441 if (check == error_mark_node)
16442 return NULL_TREE;
16444 /* Deduce the checked constraint and the prototype parameter.
16446 FIXME: In certain cases, failure to deduce should be a
16447 diagnosable error. */
16448 tree conc;
16449 tree proto;
16450 if (!deduce_constrained_parameter (check, conc, proto))
16451 return NULL_TREE;
16453 /* In template parameter scope, this results in a constrained
16454 parameter. Return a descriptor of that parm. */
16455 if (processing_template_parmlist)
16456 return build_constrained_parameter (conc, proto, args);
16458 /* In a parameter-declaration-clause, constrained-type
16459 specifiers result in invented template parameters. */
16460 if (parser->auto_is_implicit_function_template_parm_p)
16462 tree x = build_constrained_parameter (conc, proto, args);
16463 return synthesize_implicit_template_parm (parser, x);
16465 else
16467 /* Otherwise, we're in a context where the constrained
16468 type name is deduced and the constraint applies
16469 after deduction. */
16470 return make_constrained_auto (conc, args);
16473 return NULL_TREE;
16476 /* If DECL refers to a concept, return a TYPE_DECL representing
16477 the result of using the constrained type specifier in the
16478 current context. DECL refers to a concept if
16480 - it is an overload set containing a function concept taking a single
16481 type argument, or
16483 - it is a variable concept taking a single type argument. */
16485 static tree
16486 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
16488 if (flag_concepts
16489 && (TREE_CODE (decl) == OVERLOAD
16490 || BASELINK_P (decl)
16491 || variable_concept_p (decl)))
16492 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
16493 else
16494 return NULL_TREE;
16497 /* Check if DECL and ARGS form a partial-concept-id. If so,
16498 assign ID to the resulting constrained placeholder.
16500 Returns true if the partial-concept-id designates a placeholder
16501 and false otherwise. Note that *id is set to NULL_TREE in
16502 this case. */
16504 static tree
16505 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
16507 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
16510 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
16511 or a concept-name.
16513 enum-name:
16514 identifier
16516 typedef-name:
16517 identifier
16519 concept-name:
16520 identifier
16522 Returns a TYPE_DECL for the type. */
16524 static tree
16525 cp_parser_nonclass_name (cp_parser* parser)
16527 tree type_decl;
16528 tree identifier;
16530 cp_token *token = cp_lexer_peek_token (parser->lexer);
16531 identifier = cp_parser_identifier (parser);
16532 if (identifier == error_mark_node)
16533 return error_mark_node;
16535 /* Look up the type-name. */
16536 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
16538 type_decl = strip_using_decl (type_decl);
16540 /* If we found an overload set, then it may refer to a concept-name. */
16541 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
16542 type_decl = decl;
16544 if (TREE_CODE (type_decl) != TYPE_DECL
16545 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
16547 /* See if this is an Objective-C type. */
16548 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16549 tree type = objc_get_protocol_qualified_type (identifier, protos);
16550 if (type)
16551 type_decl = TYPE_NAME (type);
16554 /* Issue an error if we did not find a type-name. */
16555 if (TREE_CODE (type_decl) != TYPE_DECL
16556 /* In Objective-C, we have the complication that class names are
16557 normally type names and start declarations (eg, the
16558 "NSObject" in "NSObject *object;"), but can be used in an
16559 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
16560 is an expression. So, a classname followed by a dot is not a
16561 valid type-name. */
16562 || (objc_is_class_name (TREE_TYPE (type_decl))
16563 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
16565 if (!cp_parser_simulate_error (parser))
16566 cp_parser_name_lookup_error (parser, identifier, type_decl,
16567 NLE_TYPE, token->location);
16568 return error_mark_node;
16570 /* Remember that the name was used in the definition of the
16571 current class so that we can check later to see if the
16572 meaning would have been different after the class was
16573 entirely defined. */
16574 else if (type_decl != error_mark_node
16575 && !parser->scope)
16576 maybe_note_name_used_in_class (identifier, type_decl);
16578 return type_decl;
16581 /* Parse an elaborated-type-specifier. Note that the grammar given
16582 here incorporates the resolution to DR68.
16584 elaborated-type-specifier:
16585 class-key :: [opt] nested-name-specifier [opt] identifier
16586 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
16587 enum-key :: [opt] nested-name-specifier [opt] identifier
16588 typename :: [opt] nested-name-specifier identifier
16589 typename :: [opt] nested-name-specifier template [opt]
16590 template-id
16592 GNU extension:
16594 elaborated-type-specifier:
16595 class-key attributes :: [opt] nested-name-specifier [opt] identifier
16596 class-key attributes :: [opt] nested-name-specifier [opt]
16597 template [opt] template-id
16598 enum attributes :: [opt] nested-name-specifier [opt] identifier
16600 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
16601 declared `friend'. If IS_DECLARATION is TRUE, then this
16602 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
16603 something is being declared.
16605 Returns the TYPE specified. */
16607 static tree
16608 cp_parser_elaborated_type_specifier (cp_parser* parser,
16609 bool is_friend,
16610 bool is_declaration)
16612 enum tag_types tag_type;
16613 tree identifier;
16614 tree type = NULL_TREE;
16615 tree attributes = NULL_TREE;
16616 tree globalscope;
16617 cp_token *token = NULL;
16619 /* See if we're looking at the `enum' keyword. */
16620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
16622 /* Consume the `enum' token. */
16623 cp_lexer_consume_token (parser->lexer);
16624 /* Remember that it's an enumeration type. */
16625 tag_type = enum_type;
16626 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
16627 enums) is used here. */
16628 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
16629 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
16631 pedwarn (input_location, 0, "elaborated-type-specifier "
16632 "for a scoped enum must not use the %<%D%> keyword",
16633 cp_lexer_peek_token (parser->lexer)->u.value);
16634 /* Consume the `struct' or `class' and parse it anyway. */
16635 cp_lexer_consume_token (parser->lexer);
16637 /* Parse the attributes. */
16638 attributes = cp_parser_attributes_opt (parser);
16640 /* Or, it might be `typename'. */
16641 else if (cp_lexer_next_token_is_keyword (parser->lexer,
16642 RID_TYPENAME))
16644 /* Consume the `typename' token. */
16645 cp_lexer_consume_token (parser->lexer);
16646 /* Remember that it's a `typename' type. */
16647 tag_type = typename_type;
16649 /* Otherwise it must be a class-key. */
16650 else
16652 tag_type = cp_parser_class_key (parser);
16653 if (tag_type == none_type)
16654 return error_mark_node;
16655 /* Parse the attributes. */
16656 attributes = cp_parser_attributes_opt (parser);
16659 /* Look for the `::' operator. */
16660 globalscope = cp_parser_global_scope_opt (parser,
16661 /*current_scope_valid_p=*/false);
16662 /* Look for the nested-name-specifier. */
16663 if (tag_type == typename_type && !globalscope)
16665 if (!cp_parser_nested_name_specifier (parser,
16666 /*typename_keyword_p=*/true,
16667 /*check_dependency_p=*/true,
16668 /*type_p=*/true,
16669 is_declaration))
16670 return error_mark_node;
16672 else
16673 /* Even though `typename' is not present, the proposed resolution
16674 to Core Issue 180 says that in `class A<T>::B', `B' should be
16675 considered a type-name, even if `A<T>' is dependent. */
16676 cp_parser_nested_name_specifier_opt (parser,
16677 /*typename_keyword_p=*/true,
16678 /*check_dependency_p=*/true,
16679 /*type_p=*/true,
16680 is_declaration);
16681 /* For everything but enumeration types, consider a template-id.
16682 For an enumeration type, consider only a plain identifier. */
16683 if (tag_type != enum_type)
16685 bool template_p = false;
16686 tree decl;
16688 /* Allow the `template' keyword. */
16689 template_p = cp_parser_optional_template_keyword (parser);
16690 /* If we didn't see `template', we don't know if there's a
16691 template-id or not. */
16692 if (!template_p)
16693 cp_parser_parse_tentatively (parser);
16694 /* Parse the template-id. */
16695 token = cp_lexer_peek_token (parser->lexer);
16696 decl = cp_parser_template_id (parser, template_p,
16697 /*check_dependency_p=*/true,
16698 tag_type,
16699 is_declaration);
16700 /* If we didn't find a template-id, look for an ordinary
16701 identifier. */
16702 if (!template_p && !cp_parser_parse_definitely (parser))
16704 /* We can get here when cp_parser_template_id, called by
16705 cp_parser_class_name with tag_type == none_type, succeeds
16706 and caches a BASELINK. Then, when called again here,
16707 instead of failing and returning an error_mark_node
16708 returns it (see template/typename17.C in C++11).
16709 ??? Could we diagnose this earlier? */
16710 else if (tag_type == typename_type && BASELINK_P (decl))
16712 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
16713 type = error_mark_node;
16715 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
16716 in effect, then we must assume that, upon instantiation, the
16717 template will correspond to a class. */
16718 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16719 && tag_type == typename_type)
16720 type = make_typename_type (parser->scope, decl,
16721 typename_type,
16722 /*complain=*/tf_error);
16723 /* If the `typename' keyword is in effect and DECL is not a type
16724 decl, then type is non existent. */
16725 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
16727 else if (TREE_CODE (decl) == TYPE_DECL)
16728 type = check_elaborated_type_specifier (tag_type, decl,
16729 /*allow_template_p=*/true);
16730 else if (decl == error_mark_node)
16731 type = error_mark_node;
16734 if (!type)
16736 token = cp_lexer_peek_token (parser->lexer);
16737 identifier = cp_parser_identifier (parser);
16739 if (identifier == error_mark_node)
16741 parser->scope = NULL_TREE;
16742 return error_mark_node;
16745 /* For a `typename', we needn't call xref_tag. */
16746 if (tag_type == typename_type
16747 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
16748 return cp_parser_make_typename_type (parser, identifier,
16749 token->location);
16751 /* Template parameter lists apply only if we are not within a
16752 function parameter list. */
16753 bool template_parm_lists_apply
16754 = parser->num_template_parameter_lists;
16755 if (template_parm_lists_apply)
16756 for (cp_binding_level *s = current_binding_level;
16757 s && s->kind != sk_template_parms;
16758 s = s->level_chain)
16759 if (s->kind == sk_function_parms)
16760 template_parm_lists_apply = false;
16762 /* Look up a qualified name in the usual way. */
16763 if (parser->scope)
16765 tree decl;
16766 tree ambiguous_decls;
16768 decl = cp_parser_lookup_name (parser, identifier,
16769 tag_type,
16770 /*is_template=*/false,
16771 /*is_namespace=*/false,
16772 /*check_dependency=*/true,
16773 &ambiguous_decls,
16774 token->location);
16776 /* If the lookup was ambiguous, an error will already have been
16777 issued. */
16778 if (ambiguous_decls)
16779 return error_mark_node;
16781 /* If we are parsing friend declaration, DECL may be a
16782 TEMPLATE_DECL tree node here. However, we need to check
16783 whether this TEMPLATE_DECL results in valid code. Consider
16784 the following example:
16786 namespace N {
16787 template <class T> class C {};
16789 class X {
16790 template <class T> friend class N::C; // #1, valid code
16792 template <class T> class Y {
16793 friend class N::C; // #2, invalid code
16796 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
16797 name lookup of `N::C'. We see that friend declaration must
16798 be template for the code to be valid. Note that
16799 processing_template_decl does not work here since it is
16800 always 1 for the above two cases. */
16802 decl = (cp_parser_maybe_treat_template_as_class
16803 (decl, /*tag_name_p=*/is_friend
16804 && template_parm_lists_apply));
16806 if (TREE_CODE (decl) != TYPE_DECL)
16808 cp_parser_diagnose_invalid_type_name (parser,
16809 identifier,
16810 token->location);
16811 return error_mark_node;
16814 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
16816 bool allow_template = (template_parm_lists_apply
16817 || DECL_SELF_REFERENCE_P (decl));
16818 type = check_elaborated_type_specifier (tag_type, decl,
16819 allow_template);
16821 if (type == error_mark_node)
16822 return error_mark_node;
16825 /* Forward declarations of nested types, such as
16827 class C1::C2;
16828 class C1::C2::C3;
16830 are invalid unless all components preceding the final '::'
16831 are complete. If all enclosing types are complete, these
16832 declarations become merely pointless.
16834 Invalid forward declarations of nested types are errors
16835 caught elsewhere in parsing. Those that are pointless arrive
16836 here. */
16838 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16839 && !is_friend && !processing_explicit_instantiation)
16840 warning (0, "declaration %qD does not declare anything", decl);
16842 type = TREE_TYPE (decl);
16844 else
16846 /* An elaborated-type-specifier sometimes introduces a new type and
16847 sometimes names an existing type. Normally, the rule is that it
16848 introduces a new type only if there is not an existing type of
16849 the same name already in scope. For example, given:
16851 struct S {};
16852 void f() { struct S s; }
16854 the `struct S' in the body of `f' is the same `struct S' as in
16855 the global scope; the existing definition is used. However, if
16856 there were no global declaration, this would introduce a new
16857 local class named `S'.
16859 An exception to this rule applies to the following code:
16861 namespace N { struct S; }
16863 Here, the elaborated-type-specifier names a new type
16864 unconditionally; even if there is already an `S' in the
16865 containing scope this declaration names a new type.
16866 This exception only applies if the elaborated-type-specifier
16867 forms the complete declaration:
16869 [class.name]
16871 A declaration consisting solely of `class-key identifier ;' is
16872 either a redeclaration of the name in the current scope or a
16873 forward declaration of the identifier as a class name. It
16874 introduces the name into the current scope.
16876 We are in this situation precisely when the next token is a `;'.
16878 An exception to the exception is that a `friend' declaration does
16879 *not* name a new type; i.e., given:
16881 struct S { friend struct T; };
16883 `T' is not a new type in the scope of `S'.
16885 Also, `new struct S' or `sizeof (struct S)' never results in the
16886 definition of a new type; a new type can only be declared in a
16887 declaration context. */
16889 tag_scope ts;
16890 bool template_p;
16892 if (is_friend)
16893 /* Friends have special name lookup rules. */
16894 ts = ts_within_enclosing_non_class;
16895 else if (is_declaration
16896 && cp_lexer_next_token_is (parser->lexer,
16897 CPP_SEMICOLON))
16898 /* This is a `class-key identifier ;' */
16899 ts = ts_current;
16900 else
16901 ts = ts_global;
16903 template_p =
16904 (template_parm_lists_apply
16905 && (cp_parser_next_token_starts_class_definition_p (parser)
16906 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
16907 /* An unqualified name was used to reference this type, so
16908 there were no qualifying templates. */
16909 if (template_parm_lists_apply
16910 && !cp_parser_check_template_parameters (parser,
16911 /*num_templates=*/0,
16912 token->location,
16913 /*declarator=*/NULL))
16914 return error_mark_node;
16915 type = xref_tag (tag_type, identifier, ts, template_p);
16919 if (type == error_mark_node)
16920 return error_mark_node;
16922 /* Allow attributes on forward declarations of classes. */
16923 if (attributes)
16925 if (TREE_CODE (type) == TYPENAME_TYPE)
16926 warning (OPT_Wattributes,
16927 "attributes ignored on uninstantiated type");
16928 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
16929 && ! processing_explicit_instantiation)
16930 warning (OPT_Wattributes,
16931 "attributes ignored on template instantiation");
16932 else if (is_declaration && cp_parser_declares_only_class_p (parser))
16933 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
16934 else
16935 warning (OPT_Wattributes,
16936 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
16939 if (tag_type != enum_type)
16941 /* Indicate whether this class was declared as a `class' or as a
16942 `struct'. */
16943 if (CLASS_TYPE_P (type))
16944 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
16945 cp_parser_check_class_key (tag_type, type);
16948 /* A "<" cannot follow an elaborated type specifier. If that
16949 happens, the user was probably trying to form a template-id. */
16950 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
16951 token->location);
16953 return type;
16956 /* Parse an enum-specifier.
16958 enum-specifier:
16959 enum-head { enumerator-list [opt] }
16960 enum-head { enumerator-list , } [C++0x]
16962 enum-head:
16963 enum-key identifier [opt] enum-base [opt]
16964 enum-key nested-name-specifier identifier enum-base [opt]
16966 enum-key:
16967 enum
16968 enum class [C++0x]
16969 enum struct [C++0x]
16971 enum-base: [C++0x]
16972 : type-specifier-seq
16974 opaque-enum-specifier:
16975 enum-key identifier enum-base [opt] ;
16977 GNU Extensions:
16978 enum-key attributes[opt] identifier [opt] enum-base [opt]
16979 { enumerator-list [opt] }attributes[opt]
16980 enum-key attributes[opt] identifier [opt] enum-base [opt]
16981 { enumerator-list, }attributes[opt] [C++0x]
16983 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
16984 if the token stream isn't an enum-specifier after all. */
16986 static tree
16987 cp_parser_enum_specifier (cp_parser* parser)
16989 tree identifier;
16990 tree type = NULL_TREE;
16991 tree prev_scope;
16992 tree nested_name_specifier = NULL_TREE;
16993 tree attributes;
16994 bool scoped_enum_p = false;
16995 bool has_underlying_type = false;
16996 bool nested_being_defined = false;
16997 bool new_value_list = false;
16998 bool is_new_type = false;
16999 bool is_anonymous = false;
17000 tree underlying_type = NULL_TREE;
17001 cp_token *type_start_token = NULL;
17002 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17004 parser->colon_corrects_to_scope_p = false;
17006 /* Parse tentatively so that we can back up if we don't find a
17007 enum-specifier. */
17008 cp_parser_parse_tentatively (parser);
17010 /* Caller guarantees that the current token is 'enum', an identifier
17011 possibly follows, and the token after that is an opening brace.
17012 If we don't have an identifier, fabricate an anonymous name for
17013 the enumeration being defined. */
17014 cp_lexer_consume_token (parser->lexer);
17016 /* Parse the "class" or "struct", which indicates a scoped
17017 enumeration type in C++0x. */
17018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17019 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17021 if (cxx_dialect < cxx11)
17022 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17024 /* Consume the `struct' or `class' token. */
17025 cp_lexer_consume_token (parser->lexer);
17027 scoped_enum_p = true;
17030 attributes = cp_parser_attributes_opt (parser);
17032 /* Clear the qualification. */
17033 parser->scope = NULL_TREE;
17034 parser->qualifying_scope = NULL_TREE;
17035 parser->object_scope = NULL_TREE;
17037 /* Figure out in what scope the declaration is being placed. */
17038 prev_scope = current_scope ();
17040 type_start_token = cp_lexer_peek_token (parser->lexer);
17042 push_deferring_access_checks (dk_no_check);
17043 nested_name_specifier
17044 = cp_parser_nested_name_specifier_opt (parser,
17045 /*typename_keyword_p=*/true,
17046 /*check_dependency_p=*/false,
17047 /*type_p=*/false,
17048 /*is_declaration=*/false);
17050 if (nested_name_specifier)
17052 tree name;
17054 identifier = cp_parser_identifier (parser);
17055 name = cp_parser_lookup_name (parser, identifier,
17056 enum_type,
17057 /*is_template=*/false,
17058 /*is_namespace=*/false,
17059 /*check_dependency=*/true,
17060 /*ambiguous_decls=*/NULL,
17061 input_location);
17062 if (name && name != error_mark_node)
17064 type = TREE_TYPE (name);
17065 if (TREE_CODE (type) == TYPENAME_TYPE)
17067 /* Are template enums allowed in ISO? */
17068 if (template_parm_scope_p ())
17069 pedwarn (type_start_token->location, OPT_Wpedantic,
17070 "%qD is an enumeration template", name);
17071 /* ignore a typename reference, for it will be solved by name
17072 in start_enum. */
17073 type = NULL_TREE;
17076 else if (nested_name_specifier == error_mark_node)
17077 /* We already issued an error. */;
17078 else
17080 error_at (type_start_token->location,
17081 "%qD does not name an enumeration in %qT",
17082 identifier, nested_name_specifier);
17083 nested_name_specifier = error_mark_node;
17086 else
17088 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17089 identifier = cp_parser_identifier (parser);
17090 else
17092 identifier = make_anon_name ();
17093 is_anonymous = true;
17094 if (scoped_enum_p)
17095 error_at (type_start_token->location,
17096 "anonymous scoped enum is not allowed");
17099 pop_deferring_access_checks ();
17101 /* Check for the `:' that denotes a specified underlying type in C++0x.
17102 Note that a ':' could also indicate a bitfield width, however. */
17103 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17105 cp_decl_specifier_seq type_specifiers;
17107 /* Consume the `:'. */
17108 cp_lexer_consume_token (parser->lexer);
17110 /* Parse the type-specifier-seq. */
17111 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17112 /*is_trailing_return=*/false,
17113 &type_specifiers);
17115 /* At this point this is surely not elaborated type specifier. */
17116 if (!cp_parser_parse_definitely (parser))
17117 return NULL_TREE;
17119 if (cxx_dialect < cxx11)
17120 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17122 has_underlying_type = true;
17124 /* If that didn't work, stop. */
17125 if (type_specifiers.type != error_mark_node)
17127 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17128 /*initialized=*/0, NULL);
17129 if (underlying_type == error_mark_node
17130 || check_for_bare_parameter_packs (underlying_type))
17131 underlying_type = NULL_TREE;
17135 /* Look for the `{' but don't consume it yet. */
17136 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17138 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17140 cp_parser_error (parser, "expected %<{%>");
17141 if (has_underlying_type)
17143 type = NULL_TREE;
17144 goto out;
17147 /* An opaque-enum-specifier must have a ';' here. */
17148 if ((scoped_enum_p || underlying_type)
17149 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17151 cp_parser_error (parser, "expected %<;%> or %<{%>");
17152 if (has_underlying_type)
17154 type = NULL_TREE;
17155 goto out;
17160 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17161 return NULL_TREE;
17163 if (nested_name_specifier)
17165 if (CLASS_TYPE_P (nested_name_specifier))
17167 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17168 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17169 push_scope (nested_name_specifier);
17171 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17173 push_nested_namespace (nested_name_specifier);
17177 /* Issue an error message if type-definitions are forbidden here. */
17178 if (!cp_parser_check_type_definition (parser))
17179 type = error_mark_node;
17180 else
17181 /* Create the new type. We do this before consuming the opening
17182 brace so the enum will be recorded as being on the line of its
17183 tag (or the 'enum' keyword, if there is no tag). */
17184 type = start_enum (identifier, type, underlying_type,
17185 attributes, scoped_enum_p, &is_new_type);
17187 /* If the next token is not '{' it is an opaque-enum-specifier or an
17188 elaborated-type-specifier. */
17189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17191 timevar_push (TV_PARSE_ENUM);
17192 if (nested_name_specifier
17193 && nested_name_specifier != error_mark_node)
17195 /* The following catches invalid code such as:
17196 enum class S<int>::E { A, B, C }; */
17197 if (!processing_specialization
17198 && CLASS_TYPE_P (nested_name_specifier)
17199 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17200 error_at (type_start_token->location, "cannot add an enumerator "
17201 "list to a template instantiation");
17203 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17205 error_at (type_start_token->location,
17206 "%<%T::%E%> has not been declared",
17207 TYPE_CONTEXT (nested_name_specifier),
17208 nested_name_specifier);
17209 type = error_mark_node;
17211 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17212 && !CLASS_TYPE_P (nested_name_specifier))
17214 error_at (type_start_token->location, "nested name specifier "
17215 "%qT for enum declaration does not name a class "
17216 "or namespace", nested_name_specifier);
17217 type = error_mark_node;
17219 /* If that scope does not contain the scope in which the
17220 class was originally declared, the program is invalid. */
17221 else if (prev_scope && !is_ancestor (prev_scope,
17222 nested_name_specifier))
17224 if (at_namespace_scope_p ())
17225 error_at (type_start_token->location,
17226 "declaration of %qD in namespace %qD which does not "
17227 "enclose %qD",
17228 type, prev_scope, nested_name_specifier);
17229 else
17230 error_at (type_start_token->location,
17231 "declaration of %qD in %qD which does not "
17232 "enclose %qD",
17233 type, prev_scope, nested_name_specifier);
17234 type = error_mark_node;
17236 /* If that scope is the scope where the declaration is being placed
17237 the program is invalid. */
17238 else if (CLASS_TYPE_P (nested_name_specifier)
17239 && CLASS_TYPE_P (prev_scope)
17240 && same_type_p (nested_name_specifier, prev_scope))
17242 permerror (type_start_token->location,
17243 "extra qualification not allowed");
17244 nested_name_specifier = NULL_TREE;
17248 if (scoped_enum_p)
17249 begin_scope (sk_scoped_enum, type);
17251 /* Consume the opening brace. */
17252 cp_lexer_consume_token (parser->lexer);
17254 if (type == error_mark_node)
17255 ; /* Nothing to add */
17256 else if (OPAQUE_ENUM_P (type)
17257 || (cxx_dialect > cxx98 && processing_specialization))
17259 new_value_list = true;
17260 SET_OPAQUE_ENUM_P (type, false);
17261 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17263 else
17265 error_at (type_start_token->location,
17266 "multiple definition of %q#T", type);
17267 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17268 "previous definition here");
17269 type = error_mark_node;
17272 if (type == error_mark_node)
17273 cp_parser_skip_to_end_of_block_or_statement (parser);
17274 /* If the next token is not '}', then there are some enumerators. */
17275 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17277 if (is_anonymous && !scoped_enum_p)
17278 pedwarn (type_start_token->location, OPT_Wpedantic,
17279 "ISO C++ forbids empty anonymous enum");
17281 else
17282 cp_parser_enumerator_list (parser, type);
17284 /* Consume the final '}'. */
17285 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17287 if (scoped_enum_p)
17288 finish_scope ();
17289 timevar_pop (TV_PARSE_ENUM);
17291 else
17293 /* If a ';' follows, then it is an opaque-enum-specifier
17294 and additional restrictions apply. */
17295 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17297 if (is_anonymous)
17298 error_at (type_start_token->location,
17299 "opaque-enum-specifier without name");
17300 else if (nested_name_specifier)
17301 error_at (type_start_token->location,
17302 "opaque-enum-specifier must use a simple identifier");
17306 /* Look for trailing attributes to apply to this enumeration, and
17307 apply them if appropriate. */
17308 if (cp_parser_allow_gnu_extensions_p (parser))
17310 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17311 cplus_decl_attributes (&type,
17312 trailing_attr,
17313 (int) ATTR_FLAG_TYPE_IN_PLACE);
17316 /* Finish up the enumeration. */
17317 if (type != error_mark_node)
17319 if (new_value_list)
17320 finish_enum_value_list (type);
17321 if (is_new_type)
17322 finish_enum (type);
17325 if (nested_name_specifier)
17327 if (CLASS_TYPE_P (nested_name_specifier))
17329 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17330 pop_scope (nested_name_specifier);
17332 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17334 pop_nested_namespace (nested_name_specifier);
17337 out:
17338 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17339 return type;
17342 /* Parse an enumerator-list. The enumerators all have the indicated
17343 TYPE.
17345 enumerator-list:
17346 enumerator-definition
17347 enumerator-list , enumerator-definition */
17349 static void
17350 cp_parser_enumerator_list (cp_parser* parser, tree type)
17352 while (true)
17354 /* Parse an enumerator-definition. */
17355 cp_parser_enumerator_definition (parser, type);
17357 /* If the next token is not a ',', we've reached the end of
17358 the list. */
17359 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17360 break;
17361 /* Otherwise, consume the `,' and keep going. */
17362 cp_lexer_consume_token (parser->lexer);
17363 /* If the next token is a `}', there is a trailing comma. */
17364 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17366 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
17367 pedwarn (input_location, OPT_Wpedantic,
17368 "comma at end of enumerator list");
17369 break;
17374 /* Parse an enumerator-definition. The enumerator has the indicated
17375 TYPE.
17377 enumerator-definition:
17378 enumerator
17379 enumerator = constant-expression
17381 enumerator:
17382 identifier
17384 GNU Extensions:
17386 enumerator-definition:
17387 enumerator attributes [opt]
17388 enumerator attributes [opt] = constant-expression */
17390 static void
17391 cp_parser_enumerator_definition (cp_parser* parser, tree type)
17393 tree identifier;
17394 tree value;
17395 location_t loc;
17397 /* Save the input location because we are interested in the location
17398 of the identifier and not the location of the explicit value. */
17399 loc = cp_lexer_peek_token (parser->lexer)->location;
17401 /* Look for the identifier. */
17402 identifier = cp_parser_identifier (parser);
17403 if (identifier == error_mark_node)
17404 return;
17406 /* Parse any specified attributes. */
17407 tree attrs = cp_parser_attributes_opt (parser);
17409 /* If the next token is an '=', then there is an explicit value. */
17410 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17412 /* Consume the `=' token. */
17413 cp_lexer_consume_token (parser->lexer);
17414 /* Parse the value. */
17415 value = cp_parser_constant_expression (parser);
17417 else
17418 value = NULL_TREE;
17420 /* If we are processing a template, make sure the initializer of the
17421 enumerator doesn't contain any bare template parameter pack. */
17422 if (check_for_bare_parameter_packs (value))
17423 value = error_mark_node;
17425 /* Create the enumerator. */
17426 build_enumerator (identifier, value, type, attrs, loc);
17429 /* Parse a namespace-name.
17431 namespace-name:
17432 original-namespace-name
17433 namespace-alias
17435 Returns the NAMESPACE_DECL for the namespace. */
17437 static tree
17438 cp_parser_namespace_name (cp_parser* parser)
17440 tree identifier;
17441 tree namespace_decl;
17443 cp_token *token = cp_lexer_peek_token (parser->lexer);
17445 /* Get the name of the namespace. */
17446 identifier = cp_parser_identifier (parser);
17447 if (identifier == error_mark_node)
17448 return error_mark_node;
17450 /* Look up the identifier in the currently active scope. Look only
17451 for namespaces, due to:
17453 [basic.lookup.udir]
17455 When looking up a namespace-name in a using-directive or alias
17456 definition, only namespace names are considered.
17458 And:
17460 [basic.lookup.qual]
17462 During the lookup of a name preceding the :: scope resolution
17463 operator, object, function, and enumerator names are ignored.
17465 (Note that cp_parser_qualifying_entity only calls this
17466 function if the token after the name is the scope resolution
17467 operator.) */
17468 namespace_decl = cp_parser_lookup_name (parser, identifier,
17469 none_type,
17470 /*is_template=*/false,
17471 /*is_namespace=*/true,
17472 /*check_dependency=*/true,
17473 /*ambiguous_decls=*/NULL,
17474 token->location);
17475 /* If it's not a namespace, issue an error. */
17476 if (namespace_decl == error_mark_node
17477 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
17479 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17480 error_at (token->location, "%qD is not a namespace-name", identifier);
17481 cp_parser_error (parser, "expected namespace-name");
17482 namespace_decl = error_mark_node;
17485 return namespace_decl;
17488 /* Parse a namespace-definition.
17490 namespace-definition:
17491 named-namespace-definition
17492 unnamed-namespace-definition
17494 named-namespace-definition:
17495 original-namespace-definition
17496 extension-namespace-definition
17498 original-namespace-definition:
17499 namespace identifier { namespace-body }
17501 extension-namespace-definition:
17502 namespace original-namespace-name { namespace-body }
17504 unnamed-namespace-definition:
17505 namespace { namespace-body } */
17507 static void
17508 cp_parser_namespace_definition (cp_parser* parser)
17510 tree identifier, attribs;
17511 bool has_visibility;
17512 bool is_inline;
17513 cp_token* token;
17514 int nested_definition_count = 0;
17516 cp_ensure_no_omp_declare_simd (parser);
17517 cp_ensure_no_oacc_routine (parser);
17518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
17520 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
17521 is_inline = true;
17522 cp_lexer_consume_token (parser->lexer);
17524 else
17525 is_inline = false;
17527 /* Look for the `namespace' keyword. */
17528 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17530 /* Parse any specified attributes before the identifier. */
17531 attribs = cp_parser_attributes_opt (parser);
17533 /* Get the name of the namespace. We do not attempt to distinguish
17534 between an original-namespace-definition and an
17535 extension-namespace-definition at this point. The semantic
17536 analysis routines are responsible for that. */
17537 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17538 identifier = cp_parser_identifier (parser);
17539 else
17540 identifier = NULL_TREE;
17542 /* Parse any specified attributes after the identifier. */
17543 tree post_ident_attribs = cp_parser_attributes_opt (parser);
17544 if (post_ident_attribs)
17546 if (attribs)
17547 attribs = chainon (attribs, post_ident_attribs);
17548 else
17549 attribs = post_ident_attribs;
17552 /* Start the namespace. */
17553 push_namespace (identifier);
17555 /* Parse any nested namespace definition. */
17556 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17558 if (attribs)
17559 error_at (token->location, "a nested namespace definition cannot have attributes");
17560 if (cxx_dialect < cxx1z)
17561 pedwarn (input_location, OPT_Wpedantic,
17562 "nested namespace definitions only available with "
17563 "-std=c++1z or -std=gnu++1z");
17564 if (is_inline)
17565 error_at (token->location, "a nested namespace definition cannot be inline");
17566 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17568 cp_lexer_consume_token (parser->lexer);
17569 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17570 identifier = cp_parser_identifier (parser);
17571 else
17573 cp_parser_error (parser, "nested identifier required");
17574 break;
17576 ++nested_definition_count;
17577 push_namespace (identifier);
17581 /* Look for the `{' to validate starting the namespace. */
17582 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
17584 /* "inline namespace" is equivalent to a stub namespace definition
17585 followed by a strong using directive. */
17586 if (is_inline)
17588 tree name_space = current_namespace;
17589 /* Set up namespace association. */
17590 DECL_NAMESPACE_ASSOCIATIONS (name_space)
17591 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
17592 DECL_NAMESPACE_ASSOCIATIONS (name_space));
17593 /* Import the contents of the inline namespace. */
17594 pop_namespace ();
17595 do_using_directive (name_space);
17596 push_namespace (identifier);
17599 has_visibility = handle_namespace_attrs (current_namespace, attribs);
17601 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
17603 /* Parse the body of the namespace. */
17604 cp_parser_namespace_body (parser);
17606 if (has_visibility)
17607 pop_visibility (1);
17609 /* Finish the nested namespace definitions. */
17610 while (nested_definition_count--)
17611 pop_namespace ();
17613 /* Finish the namespace. */
17614 pop_namespace ();
17615 /* Look for the final `}'. */
17616 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17619 /* Parse a namespace-body.
17621 namespace-body:
17622 declaration-seq [opt] */
17624 static void
17625 cp_parser_namespace_body (cp_parser* parser)
17627 cp_parser_declaration_seq_opt (parser);
17630 /* Parse a namespace-alias-definition.
17632 namespace-alias-definition:
17633 namespace identifier = qualified-namespace-specifier ; */
17635 static void
17636 cp_parser_namespace_alias_definition (cp_parser* parser)
17638 tree identifier;
17639 tree namespace_specifier;
17641 cp_token *token = cp_lexer_peek_token (parser->lexer);
17643 /* Look for the `namespace' keyword. */
17644 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
17645 /* Look for the identifier. */
17646 identifier = cp_parser_identifier (parser);
17647 if (identifier == error_mark_node)
17648 return;
17649 /* Look for the `=' token. */
17650 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
17651 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17653 error_at (token->location, "%<namespace%> definition is not allowed here");
17654 /* Skip the definition. */
17655 cp_lexer_consume_token (parser->lexer);
17656 if (cp_parser_skip_to_closing_brace (parser))
17657 cp_lexer_consume_token (parser->lexer);
17658 return;
17660 cp_parser_require (parser, CPP_EQ, RT_EQ);
17661 /* Look for the qualified-namespace-specifier. */
17662 namespace_specifier
17663 = cp_parser_qualified_namespace_specifier (parser);
17664 /* Look for the `;' token. */
17665 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17667 /* Register the alias in the symbol table. */
17668 do_namespace_alias (identifier, namespace_specifier);
17671 /* Parse a qualified-namespace-specifier.
17673 qualified-namespace-specifier:
17674 :: [opt] nested-name-specifier [opt] namespace-name
17676 Returns a NAMESPACE_DECL corresponding to the specified
17677 namespace. */
17679 static tree
17680 cp_parser_qualified_namespace_specifier (cp_parser* parser)
17682 /* Look for the optional `::'. */
17683 cp_parser_global_scope_opt (parser,
17684 /*current_scope_valid_p=*/false);
17686 /* Look for the optional nested-name-specifier. */
17687 cp_parser_nested_name_specifier_opt (parser,
17688 /*typename_keyword_p=*/false,
17689 /*check_dependency_p=*/true,
17690 /*type_p=*/false,
17691 /*is_declaration=*/true);
17693 return cp_parser_namespace_name (parser);
17696 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
17697 access declaration.
17699 using-declaration:
17700 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
17701 using :: unqualified-id ;
17703 access-declaration:
17704 qualified-id ;
17708 static bool
17709 cp_parser_using_declaration (cp_parser* parser,
17710 bool access_declaration_p)
17712 cp_token *token;
17713 bool typename_p = false;
17714 bool global_scope_p;
17715 tree decl;
17716 tree identifier;
17717 tree qscope;
17718 int oldcount = errorcount;
17719 cp_token *diag_token = NULL;
17721 if (access_declaration_p)
17723 diag_token = cp_lexer_peek_token (parser->lexer);
17724 cp_parser_parse_tentatively (parser);
17726 else
17728 /* Look for the `using' keyword. */
17729 cp_parser_require_keyword (parser, RID_USING, RT_USING);
17731 /* Peek at the next token. */
17732 token = cp_lexer_peek_token (parser->lexer);
17733 /* See if it's `typename'. */
17734 if (token->keyword == RID_TYPENAME)
17736 /* Remember that we've seen it. */
17737 typename_p = true;
17738 /* Consume the `typename' token. */
17739 cp_lexer_consume_token (parser->lexer);
17743 /* Look for the optional global scope qualification. */
17744 global_scope_p
17745 = (cp_parser_global_scope_opt (parser,
17746 /*current_scope_valid_p=*/false)
17747 != NULL_TREE);
17749 /* If we saw `typename', or didn't see `::', then there must be a
17750 nested-name-specifier present. */
17751 if (typename_p || !global_scope_p)
17753 qscope = cp_parser_nested_name_specifier (parser, typename_p,
17754 /*check_dependency_p=*/true,
17755 /*type_p=*/false,
17756 /*is_declaration=*/true);
17757 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
17759 cp_parser_skip_to_end_of_block_or_statement (parser);
17760 return false;
17763 /* Otherwise, we could be in either of the two productions. In that
17764 case, treat the nested-name-specifier as optional. */
17765 else
17766 qscope = cp_parser_nested_name_specifier_opt (parser,
17767 /*typename_keyword_p=*/false,
17768 /*check_dependency_p=*/true,
17769 /*type_p=*/false,
17770 /*is_declaration=*/true);
17771 if (!qscope)
17772 qscope = global_namespace;
17773 else if (UNSCOPED_ENUM_P (qscope))
17774 qscope = CP_TYPE_CONTEXT (qscope);
17776 if (access_declaration_p && cp_parser_error_occurred (parser))
17777 /* Something has already gone wrong; there's no need to parse
17778 further. Since an error has occurred, the return value of
17779 cp_parser_parse_definitely will be false, as required. */
17780 return cp_parser_parse_definitely (parser);
17782 token = cp_lexer_peek_token (parser->lexer);
17783 /* Parse the unqualified-id. */
17784 identifier = cp_parser_unqualified_id (parser,
17785 /*template_keyword_p=*/false,
17786 /*check_dependency_p=*/true,
17787 /*declarator_p=*/true,
17788 /*optional_p=*/false);
17790 if (access_declaration_p)
17792 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17793 cp_parser_simulate_error (parser);
17794 if (!cp_parser_parse_definitely (parser))
17795 return false;
17798 /* The function we call to handle a using-declaration is different
17799 depending on what scope we are in. */
17800 if (qscope == error_mark_node || identifier == error_mark_node)
17802 else if (!identifier_p (identifier)
17803 && TREE_CODE (identifier) != BIT_NOT_EXPR)
17804 /* [namespace.udecl]
17806 A using declaration shall not name a template-id. */
17807 error_at (token->location,
17808 "a template-id may not appear in a using-declaration");
17809 else
17811 if (at_class_scope_p ())
17813 /* Create the USING_DECL. */
17814 decl = do_class_using_decl (parser->scope, identifier);
17816 if (decl && typename_p)
17817 USING_DECL_TYPENAME_P (decl) = 1;
17819 if (check_for_bare_parameter_packs (decl))
17821 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17822 return false;
17824 else
17825 /* Add it to the list of members in this class. */
17826 finish_member_declaration (decl);
17828 else
17830 decl = cp_parser_lookup_name_simple (parser,
17831 identifier,
17832 token->location);
17833 if (decl == error_mark_node)
17834 cp_parser_name_lookup_error (parser, identifier,
17835 decl, NLE_NULL,
17836 token->location);
17837 else if (check_for_bare_parameter_packs (decl))
17839 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17840 return false;
17842 else if (!at_namespace_scope_p ())
17843 do_local_using_decl (decl, qscope, identifier);
17844 else
17845 do_toplevel_using_decl (decl, qscope, identifier);
17849 /* Look for the final `;'. */
17850 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17852 if (access_declaration_p && errorcount == oldcount)
17853 warning_at (diag_token->location, OPT_Wdeprecated,
17854 "access declarations are deprecated "
17855 "in favour of using-declarations; "
17856 "suggestion: add the %<using%> keyword");
17858 return true;
17861 /* Parse an alias-declaration.
17863 alias-declaration:
17864 using identifier attribute-specifier-seq [opt] = type-id */
17866 static tree
17867 cp_parser_alias_declaration (cp_parser* parser)
17869 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
17870 location_t id_location;
17871 cp_declarator *declarator;
17872 cp_decl_specifier_seq decl_specs;
17873 bool member_p;
17874 const char *saved_message = NULL;
17876 /* Look for the `using' keyword. */
17877 cp_token *using_token
17878 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
17879 if (using_token == NULL)
17880 return error_mark_node;
17882 id_location = cp_lexer_peek_token (parser->lexer)->location;
17883 id = cp_parser_identifier (parser);
17884 if (id == error_mark_node)
17885 return error_mark_node;
17887 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
17888 attributes = cp_parser_attributes_opt (parser);
17889 if (attributes == error_mark_node)
17890 return error_mark_node;
17892 cp_parser_require (parser, CPP_EQ, RT_EQ);
17894 if (cp_parser_error_occurred (parser))
17895 return error_mark_node;
17897 cp_parser_commit_to_tentative_parse (parser);
17899 /* Now we are going to parse the type-id of the declaration. */
17902 [dcl.type]/3 says:
17904 "A type-specifier-seq shall not define a class or enumeration
17905 unless it appears in the type-id of an alias-declaration (7.1.3) that
17906 is not the declaration of a template-declaration."
17908 In other words, if we currently are in an alias template, the
17909 type-id should not define a type.
17911 So let's set parser->type_definition_forbidden_message in that
17912 case; cp_parser_check_type_definition (called by
17913 cp_parser_class_specifier) will then emit an error if a type is
17914 defined in the type-id. */
17915 if (parser->num_template_parameter_lists)
17917 saved_message = parser->type_definition_forbidden_message;
17918 parser->type_definition_forbidden_message =
17919 G_("types may not be defined in alias template declarations");
17922 type = cp_parser_type_id (parser);
17924 /* Restore the error message if need be. */
17925 if (parser->num_template_parameter_lists)
17926 parser->type_definition_forbidden_message = saved_message;
17928 if (type == error_mark_node
17929 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
17931 cp_parser_skip_to_end_of_block_or_statement (parser);
17932 return error_mark_node;
17935 /* A typedef-name can also be introduced by an alias-declaration. The
17936 identifier following the using keyword becomes a typedef-name. It has
17937 the same semantics as if it were introduced by the typedef
17938 specifier. In particular, it does not define a new type and it shall
17939 not appear in the type-id. */
17941 clear_decl_specs (&decl_specs);
17942 decl_specs.type = type;
17943 if (attributes != NULL_TREE)
17945 decl_specs.attributes = attributes;
17946 set_and_check_decl_spec_loc (&decl_specs,
17947 ds_attribute,
17948 attrs_token);
17950 set_and_check_decl_spec_loc (&decl_specs,
17951 ds_typedef,
17952 using_token);
17953 set_and_check_decl_spec_loc (&decl_specs,
17954 ds_alias,
17955 using_token);
17957 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
17958 declarator->id_loc = id_location;
17960 member_p = at_class_scope_p ();
17961 if (member_p)
17962 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
17963 NULL_TREE, attributes);
17964 else
17965 decl = start_decl (declarator, &decl_specs, 0,
17966 attributes, NULL_TREE, &pushed_scope);
17967 if (decl == error_mark_node)
17968 return decl;
17970 // Attach constraints to the alias declaration.
17971 if (flag_concepts && current_template_parms)
17973 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
17974 tree constr = build_constraints (reqs, NULL_TREE);
17975 set_constraints (decl, constr);
17978 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
17980 if (pushed_scope)
17981 pop_scope (pushed_scope);
17983 /* If decl is a template, return its TEMPLATE_DECL so that it gets
17984 added into the symbol table; otherwise, return the TYPE_DECL. */
17985 if (DECL_LANG_SPECIFIC (decl)
17986 && DECL_TEMPLATE_INFO (decl)
17987 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
17989 decl = DECL_TI_TEMPLATE (decl);
17990 if (member_p)
17991 check_member_template (decl);
17994 return decl;
17997 /* Parse a using-directive.
17999 using-directive:
18000 using namespace :: [opt] nested-name-specifier [opt]
18001 namespace-name ; */
18003 static void
18004 cp_parser_using_directive (cp_parser* parser)
18006 tree namespace_decl;
18007 tree attribs;
18009 /* Look for the `using' keyword. */
18010 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18011 /* And the `namespace' keyword. */
18012 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18013 /* Look for the optional `::' operator. */
18014 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18015 /* And the optional nested-name-specifier. */
18016 cp_parser_nested_name_specifier_opt (parser,
18017 /*typename_keyword_p=*/false,
18018 /*check_dependency_p=*/true,
18019 /*type_p=*/false,
18020 /*is_declaration=*/true);
18021 /* Get the namespace being used. */
18022 namespace_decl = cp_parser_namespace_name (parser);
18023 /* And any specified attributes. */
18024 attribs = cp_parser_attributes_opt (parser);
18025 /* Update the symbol table. */
18026 parse_using_directive (namespace_decl, attribs);
18027 /* Look for the final `;'. */
18028 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18031 /* Parse an asm-definition.
18033 asm-definition:
18034 asm ( string-literal ) ;
18036 GNU Extension:
18038 asm-definition:
18039 asm volatile [opt] ( string-literal ) ;
18040 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18041 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18042 : asm-operand-list [opt] ) ;
18043 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18044 : asm-operand-list [opt]
18045 : asm-clobber-list [opt] ) ;
18046 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18047 : asm-clobber-list [opt]
18048 : asm-goto-list ) ; */
18050 static void
18051 cp_parser_asm_definition (cp_parser* parser)
18053 tree string;
18054 tree outputs = NULL_TREE;
18055 tree inputs = NULL_TREE;
18056 tree clobbers = NULL_TREE;
18057 tree labels = NULL_TREE;
18058 tree asm_stmt;
18059 bool volatile_p = false;
18060 bool extended_p = false;
18061 bool invalid_inputs_p = false;
18062 bool invalid_outputs_p = false;
18063 bool goto_p = false;
18064 required_token missing = RT_NONE;
18066 /* Look for the `asm' keyword. */
18067 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18069 if (parser->in_function_body
18070 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18072 error ("%<asm%> in %<constexpr%> function");
18073 cp_function_chain->invalid_constexpr = true;
18076 /* See if the next token is `volatile'. */
18077 if (cp_parser_allow_gnu_extensions_p (parser)
18078 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18080 /* Remember that we saw the `volatile' keyword. */
18081 volatile_p = true;
18082 /* Consume the token. */
18083 cp_lexer_consume_token (parser->lexer);
18085 if (cp_parser_allow_gnu_extensions_p (parser)
18086 && parser->in_function_body
18087 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18089 /* Remember that we saw the `goto' keyword. */
18090 goto_p = true;
18091 /* Consume the token. */
18092 cp_lexer_consume_token (parser->lexer);
18094 /* Look for the opening `('. */
18095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18096 return;
18097 /* Look for the string. */
18098 string = cp_parser_string_literal (parser, false, false);
18099 if (string == error_mark_node)
18101 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18102 /*consume_paren=*/true);
18103 return;
18106 /* If we're allowing GNU extensions, check for the extended assembly
18107 syntax. Unfortunately, the `:' tokens need not be separated by
18108 a space in C, and so, for compatibility, we tolerate that here
18109 too. Doing that means that we have to treat the `::' operator as
18110 two `:' tokens. */
18111 if (cp_parser_allow_gnu_extensions_p (parser)
18112 && parser->in_function_body
18113 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18114 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18116 bool inputs_p = false;
18117 bool clobbers_p = false;
18118 bool labels_p = false;
18120 /* The extended syntax was used. */
18121 extended_p = true;
18123 /* Look for outputs. */
18124 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18126 /* Consume the `:'. */
18127 cp_lexer_consume_token (parser->lexer);
18128 /* Parse the output-operands. */
18129 if (cp_lexer_next_token_is_not (parser->lexer,
18130 CPP_COLON)
18131 && cp_lexer_next_token_is_not (parser->lexer,
18132 CPP_SCOPE)
18133 && cp_lexer_next_token_is_not (parser->lexer,
18134 CPP_CLOSE_PAREN)
18135 && !goto_p)
18137 outputs = cp_parser_asm_operand_list (parser);
18138 if (outputs == error_mark_node)
18139 invalid_outputs_p = true;
18142 /* If the next token is `::', there are no outputs, and the
18143 next token is the beginning of the inputs. */
18144 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18145 /* The inputs are coming next. */
18146 inputs_p = true;
18148 /* Look for inputs. */
18149 if (inputs_p
18150 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18152 /* Consume the `:' or `::'. */
18153 cp_lexer_consume_token (parser->lexer);
18154 /* Parse the output-operands. */
18155 if (cp_lexer_next_token_is_not (parser->lexer,
18156 CPP_COLON)
18157 && cp_lexer_next_token_is_not (parser->lexer,
18158 CPP_SCOPE)
18159 && cp_lexer_next_token_is_not (parser->lexer,
18160 CPP_CLOSE_PAREN))
18162 inputs = cp_parser_asm_operand_list (parser);
18163 if (inputs == error_mark_node)
18164 invalid_inputs_p = true;
18167 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18168 /* The clobbers are coming next. */
18169 clobbers_p = true;
18171 /* Look for clobbers. */
18172 if (clobbers_p
18173 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18175 clobbers_p = true;
18176 /* Consume the `:' or `::'. */
18177 cp_lexer_consume_token (parser->lexer);
18178 /* Parse the clobbers. */
18179 if (cp_lexer_next_token_is_not (parser->lexer,
18180 CPP_COLON)
18181 && cp_lexer_next_token_is_not (parser->lexer,
18182 CPP_CLOSE_PAREN))
18183 clobbers = cp_parser_asm_clobber_list (parser);
18185 else if (goto_p
18186 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18187 /* The labels are coming next. */
18188 labels_p = true;
18190 /* Look for labels. */
18191 if (labels_p
18192 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18194 labels_p = true;
18195 /* Consume the `:' or `::'. */
18196 cp_lexer_consume_token (parser->lexer);
18197 /* Parse the labels. */
18198 labels = cp_parser_asm_label_list (parser);
18201 if (goto_p && !labels_p)
18202 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18204 else if (goto_p)
18205 missing = RT_COLON_SCOPE;
18207 /* Look for the closing `)'. */
18208 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18209 missing ? missing : RT_CLOSE_PAREN))
18210 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18211 /*consume_paren=*/true);
18212 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18214 if (!invalid_inputs_p && !invalid_outputs_p)
18216 /* Create the ASM_EXPR. */
18217 if (parser->in_function_body)
18219 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18220 inputs, clobbers, labels);
18221 /* If the extended syntax was not used, mark the ASM_EXPR. */
18222 if (!extended_p)
18224 tree temp = asm_stmt;
18225 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18226 temp = TREE_OPERAND (temp, 0);
18228 ASM_INPUT_P (temp) = 1;
18231 else
18232 symtab->finalize_toplevel_asm (string);
18236 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18237 type that comes from the decl-specifier-seq. */
18239 static tree
18240 strip_declarator_types (tree type, cp_declarator *declarator)
18242 for (cp_declarator *d = declarator; d;)
18243 switch (d->kind)
18245 case cdk_id:
18246 case cdk_error:
18247 d = NULL;
18248 break;
18250 default:
18251 if (TYPE_PTRMEMFUNC_P (type))
18252 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18253 type = TREE_TYPE (type);
18254 d = d->declarator;
18255 break;
18258 return type;
18261 /* Declarators [gram.dcl.decl] */
18263 /* Parse an init-declarator.
18265 init-declarator:
18266 declarator initializer [opt]
18268 GNU Extension:
18270 init-declarator:
18271 declarator asm-specification [opt] attributes [opt] initializer [opt]
18273 function-definition:
18274 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18275 function-body
18276 decl-specifier-seq [opt] declarator function-try-block
18278 GNU Extension:
18280 function-definition:
18281 __extension__ function-definition
18283 TM Extension:
18285 function-definition:
18286 decl-specifier-seq [opt] declarator function-transaction-block
18288 The DECL_SPECIFIERS apply to this declarator. Returns a
18289 representation of the entity declared. If MEMBER_P is TRUE, then
18290 this declarator appears in a class scope. The new DECL created by
18291 this declarator is returned.
18293 The CHECKS are access checks that should be performed once we know
18294 what entity is being declared (and, therefore, what classes have
18295 befriended it).
18297 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18298 for a function-definition here as well. If the declarator is a
18299 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18300 be TRUE upon return. By that point, the function-definition will
18301 have been completely parsed.
18303 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18304 is FALSE.
18306 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18307 parsed declaration if it is an uninitialized single declarator not followed
18308 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18309 if present, will not be consumed. If returned, this declarator will be
18310 created with SD_INITIALIZED but will not call cp_finish_decl.
18312 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18313 and there is an initializer, the pointed location_t is set to the
18314 location of the '=' or `(', or '{' in C++11 token introducing the
18315 initializer. */
18317 static tree
18318 cp_parser_init_declarator (cp_parser* parser,
18319 cp_decl_specifier_seq *decl_specifiers,
18320 vec<deferred_access_check, va_gc> *checks,
18321 bool function_definition_allowed_p,
18322 bool member_p,
18323 int declares_class_or_enum,
18324 bool* function_definition_p,
18325 tree* maybe_range_for_decl,
18326 location_t* init_loc,
18327 tree* auto_result)
18329 cp_token *token = NULL, *asm_spec_start_token = NULL,
18330 *attributes_start_token = NULL;
18331 cp_declarator *declarator;
18332 tree prefix_attributes;
18333 tree attributes = NULL;
18334 tree asm_specification;
18335 tree initializer;
18336 tree decl = NULL_TREE;
18337 tree scope;
18338 int is_initialized;
18339 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
18340 initialized with "= ..", CPP_OPEN_PAREN if initialized with
18341 "(...)". */
18342 enum cpp_ttype initialization_kind;
18343 bool is_direct_init = false;
18344 bool is_non_constant_init;
18345 int ctor_dtor_or_conv_p;
18346 bool friend_p = cp_parser_friend_p (decl_specifiers);
18347 tree pushed_scope = NULL_TREE;
18348 bool range_for_decl_p = false;
18349 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18350 location_t tmp_init_loc = UNKNOWN_LOCATION;
18352 /* Gather the attributes that were provided with the
18353 decl-specifiers. */
18354 prefix_attributes = decl_specifiers->attributes;
18356 /* Assume that this is not the declarator for a function
18357 definition. */
18358 if (function_definition_p)
18359 *function_definition_p = false;
18361 /* Default arguments are only permitted for function parameters. */
18362 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
18363 parser->default_arg_ok_p = false;
18365 /* Defer access checks while parsing the declarator; we cannot know
18366 what names are accessible until we know what is being
18367 declared. */
18368 resume_deferring_access_checks ();
18370 /* Parse the declarator. */
18371 token = cp_lexer_peek_token (parser->lexer);
18372 declarator
18373 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18374 &ctor_dtor_or_conv_p,
18375 /*parenthesized_p=*/NULL,
18376 member_p, friend_p);
18377 /* Gather up the deferred checks. */
18378 stop_deferring_access_checks ();
18380 parser->default_arg_ok_p = saved_default_arg_ok_p;
18382 /* If the DECLARATOR was erroneous, there's no need to go
18383 further. */
18384 if (declarator == cp_error_declarator)
18385 return error_mark_node;
18387 /* Check that the number of template-parameter-lists is OK. */
18388 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
18389 token->location))
18390 return error_mark_node;
18392 if (declares_class_or_enum & 2)
18393 cp_parser_check_for_definition_in_return_type (declarator,
18394 decl_specifiers->type,
18395 decl_specifiers->locations[ds_type_spec]);
18397 /* Figure out what scope the entity declared by the DECLARATOR is
18398 located in. `grokdeclarator' sometimes changes the scope, so
18399 we compute it now. */
18400 scope = get_scope_of_declarator (declarator);
18402 /* Perform any lookups in the declared type which were thought to be
18403 dependent, but are not in the scope of the declarator. */
18404 decl_specifiers->type
18405 = maybe_update_decl_type (decl_specifiers->type, scope);
18407 /* If we're allowing GNU extensions, look for an
18408 asm-specification. */
18409 if (cp_parser_allow_gnu_extensions_p (parser))
18411 /* Look for an asm-specification. */
18412 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
18413 asm_specification = cp_parser_asm_specification_opt (parser);
18415 else
18416 asm_specification = NULL_TREE;
18418 /* Look for attributes. */
18419 attributes_start_token = cp_lexer_peek_token (parser->lexer);
18420 attributes = cp_parser_attributes_opt (parser);
18422 /* Peek at the next token. */
18423 token = cp_lexer_peek_token (parser->lexer);
18425 bool bogus_implicit_tmpl = false;
18427 if (function_declarator_p (declarator))
18429 /* Check to see if the token indicates the start of a
18430 function-definition. */
18431 if (cp_parser_token_starts_function_definition_p (token))
18433 if (!function_definition_allowed_p)
18435 /* If a function-definition should not appear here, issue an
18436 error message. */
18437 cp_parser_error (parser,
18438 "a function-definition is not allowed here");
18439 return error_mark_node;
18442 location_t func_brace_location
18443 = cp_lexer_peek_token (parser->lexer)->location;
18445 /* Neither attributes nor an asm-specification are allowed
18446 on a function-definition. */
18447 if (asm_specification)
18448 error_at (asm_spec_start_token->location,
18449 "an asm-specification is not allowed "
18450 "on a function-definition");
18451 if (attributes)
18452 error_at (attributes_start_token->location,
18453 "attributes are not allowed "
18454 "on a function-definition");
18455 /* This is a function-definition. */
18456 *function_definition_p = true;
18458 /* Parse the function definition. */
18459 if (member_p)
18460 decl = cp_parser_save_member_function_body (parser,
18461 decl_specifiers,
18462 declarator,
18463 prefix_attributes);
18464 else
18465 decl =
18466 (cp_parser_function_definition_from_specifiers_and_declarator
18467 (parser, decl_specifiers, prefix_attributes, declarator));
18469 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
18471 /* This is where the prologue starts... */
18472 DECL_STRUCT_FUNCTION (decl)->function_start_locus
18473 = func_brace_location;
18476 return decl;
18479 else if (parser->fully_implicit_function_template_p)
18481 /* A non-template declaration involving a function parameter list
18482 containing an implicit template parameter will be made into a
18483 template. If the resulting declaration is not going to be an
18484 actual function then finish the template scope here to prevent it.
18485 An error message will be issued once we have a decl to talk about.
18487 FIXME probably we should do type deduction rather than create an
18488 implicit template, but the standard currently doesn't allow it. */
18489 bogus_implicit_tmpl = true;
18490 finish_fully_implicit_template (parser, NULL_TREE);
18493 /* [dcl.dcl]
18495 Only in function declarations for constructors, destructors, and
18496 type conversions can the decl-specifier-seq be omitted.
18498 We explicitly postpone this check past the point where we handle
18499 function-definitions because we tolerate function-definitions
18500 that are missing their return types in some modes. */
18501 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
18503 cp_parser_error (parser,
18504 "expected constructor, destructor, or type conversion");
18505 return error_mark_node;
18508 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
18509 if (token->type == CPP_EQ
18510 || token->type == CPP_OPEN_PAREN
18511 || token->type == CPP_OPEN_BRACE)
18513 is_initialized = SD_INITIALIZED;
18514 initialization_kind = token->type;
18515 if (maybe_range_for_decl)
18516 *maybe_range_for_decl = error_mark_node;
18517 tmp_init_loc = token->location;
18518 if (init_loc && *init_loc == UNKNOWN_LOCATION)
18519 *init_loc = tmp_init_loc;
18521 if (token->type == CPP_EQ
18522 && function_declarator_p (declarator))
18524 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
18525 if (t2->keyword == RID_DEFAULT)
18526 is_initialized = SD_DEFAULTED;
18527 else if (t2->keyword == RID_DELETE)
18528 is_initialized = SD_DELETED;
18531 else
18533 /* If the init-declarator isn't initialized and isn't followed by a
18534 `,' or `;', it's not a valid init-declarator. */
18535 if (token->type != CPP_COMMA
18536 && token->type != CPP_SEMICOLON)
18538 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
18539 range_for_decl_p = true;
18540 else
18542 if (!maybe_range_for_decl)
18543 cp_parser_error (parser, "expected initializer");
18544 return error_mark_node;
18547 is_initialized = SD_UNINITIALIZED;
18548 initialization_kind = CPP_EOF;
18551 /* Because start_decl has side-effects, we should only call it if we
18552 know we're going ahead. By this point, we know that we cannot
18553 possibly be looking at any other construct. */
18554 cp_parser_commit_to_tentative_parse (parser);
18556 /* Enter the newly declared entry in the symbol table. If we're
18557 processing a declaration in a class-specifier, we wait until
18558 after processing the initializer. */
18559 if (!member_p)
18561 if (parser->in_unbraced_linkage_specification_p)
18562 decl_specifiers->storage_class = sc_extern;
18563 decl = start_decl (declarator, decl_specifiers,
18564 range_for_decl_p? SD_INITIALIZED : is_initialized,
18565 attributes, prefix_attributes, &pushed_scope);
18566 cp_finalize_omp_declare_simd (parser, decl);
18567 cp_finalize_oacc_routine (parser, decl, false);
18568 /* Adjust location of decl if declarator->id_loc is more appropriate:
18569 set, and decl wasn't merged with another decl, in which case its
18570 location would be different from input_location, and more accurate. */
18571 if (DECL_P (decl)
18572 && declarator->id_loc != UNKNOWN_LOCATION
18573 && DECL_SOURCE_LOCATION (decl) == input_location)
18574 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
18576 else if (scope)
18577 /* Enter the SCOPE. That way unqualified names appearing in the
18578 initializer will be looked up in SCOPE. */
18579 pushed_scope = push_scope (scope);
18581 /* Perform deferred access control checks, now that we know in which
18582 SCOPE the declared entity resides. */
18583 if (!member_p && decl)
18585 tree saved_current_function_decl = NULL_TREE;
18587 /* If the entity being declared is a function, pretend that we
18588 are in its scope. If it is a `friend', it may have access to
18589 things that would not otherwise be accessible. */
18590 if (TREE_CODE (decl) == FUNCTION_DECL)
18592 saved_current_function_decl = current_function_decl;
18593 current_function_decl = decl;
18596 /* Perform access checks for template parameters. */
18597 cp_parser_perform_template_parameter_access_checks (checks);
18599 /* Perform the access control checks for the declarator and the
18600 decl-specifiers. */
18601 perform_deferred_access_checks (tf_warning_or_error);
18603 /* Restore the saved value. */
18604 if (TREE_CODE (decl) == FUNCTION_DECL)
18605 current_function_decl = saved_current_function_decl;
18608 /* Parse the initializer. */
18609 initializer = NULL_TREE;
18610 is_direct_init = false;
18611 is_non_constant_init = true;
18612 if (is_initialized)
18614 if (function_declarator_p (declarator))
18616 if (initialization_kind == CPP_EQ)
18617 initializer = cp_parser_pure_specifier (parser);
18618 else
18620 /* If the declaration was erroneous, we don't really
18621 know what the user intended, so just silently
18622 consume the initializer. */
18623 if (decl != error_mark_node)
18624 error_at (tmp_init_loc, "initializer provided for function");
18625 cp_parser_skip_to_closing_parenthesis (parser,
18626 /*recovering=*/true,
18627 /*or_comma=*/false,
18628 /*consume_paren=*/true);
18631 else
18633 /* We want to record the extra mangling scope for in-class
18634 initializers of class members and initializers of static data
18635 member templates. The former involves deferring
18636 parsing of the initializer until end of class as with default
18637 arguments. So right here we only handle the latter. */
18638 if (!member_p && processing_template_decl)
18639 start_lambda_scope (decl);
18640 initializer = cp_parser_initializer (parser,
18641 &is_direct_init,
18642 &is_non_constant_init);
18643 if (!member_p && processing_template_decl)
18644 finish_lambda_scope ();
18645 if (initializer == error_mark_node)
18646 cp_parser_skip_to_end_of_statement (parser);
18650 /* The old parser allows attributes to appear after a parenthesized
18651 initializer. Mark Mitchell proposed removing this functionality
18652 on the GCC mailing lists on 2002-08-13. This parser accepts the
18653 attributes -- but ignores them. */
18654 if (cp_parser_allow_gnu_extensions_p (parser)
18655 && initialization_kind == CPP_OPEN_PAREN)
18656 if (cp_parser_attributes_opt (parser))
18657 warning (OPT_Wattributes,
18658 "attributes after parenthesized initializer ignored");
18660 /* And now complain about a non-function implicit template. */
18661 if (bogus_implicit_tmpl && decl != error_mark_node)
18662 error_at (DECL_SOURCE_LOCATION (decl),
18663 "non-function %qD declared as implicit template", decl);
18665 /* For an in-class declaration, use `grokfield' to create the
18666 declaration. */
18667 if (member_p)
18669 if (pushed_scope)
18671 pop_scope (pushed_scope);
18672 pushed_scope = NULL_TREE;
18674 decl = grokfield (declarator, decl_specifiers,
18675 initializer, !is_non_constant_init,
18676 /*asmspec=*/NULL_TREE,
18677 chainon (attributes, prefix_attributes));
18678 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
18679 cp_parser_save_default_args (parser, decl);
18680 cp_finalize_omp_declare_simd (parser, decl);
18681 cp_finalize_oacc_routine (parser, decl, false);
18684 /* Finish processing the declaration. But, skip member
18685 declarations. */
18686 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
18688 cp_finish_decl (decl,
18689 initializer, !is_non_constant_init,
18690 asm_specification,
18691 /* If the initializer is in parentheses, then this is
18692 a direct-initialization, which means that an
18693 `explicit' constructor is OK. Otherwise, an
18694 `explicit' constructor cannot be used. */
18695 ((is_direct_init || !is_initialized)
18696 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
18698 else if ((cxx_dialect != cxx98) && friend_p
18699 && decl && TREE_CODE (decl) == FUNCTION_DECL)
18700 /* Core issue #226 (C++0x only): A default template-argument
18701 shall not be specified in a friend class template
18702 declaration. */
18703 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
18704 /*is_partial=*/false, /*is_friend_decl=*/1);
18706 if (!friend_p && pushed_scope)
18707 pop_scope (pushed_scope);
18709 if (function_declarator_p (declarator)
18710 && parser->fully_implicit_function_template_p)
18712 if (member_p)
18713 decl = finish_fully_implicit_template (parser, decl);
18714 else
18715 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
18718 if (auto_result && is_initialized && decl_specifiers->type
18719 && type_uses_auto (decl_specifiers->type))
18720 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
18722 return decl;
18725 /* Parse a declarator.
18727 declarator:
18728 direct-declarator
18729 ptr-operator declarator
18731 abstract-declarator:
18732 ptr-operator abstract-declarator [opt]
18733 direct-abstract-declarator
18735 GNU Extensions:
18737 declarator:
18738 attributes [opt] direct-declarator
18739 attributes [opt] ptr-operator declarator
18741 abstract-declarator:
18742 attributes [opt] ptr-operator abstract-declarator [opt]
18743 attributes [opt] direct-abstract-declarator
18745 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
18746 detect constructor, destructor or conversion operators. It is set
18747 to -1 if the declarator is a name, and +1 if it is a
18748 function. Otherwise it is set to zero. Usually you just want to
18749 test for >0, but internally the negative value is used.
18751 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
18752 a decl-specifier-seq unless it declares a constructor, destructor,
18753 or conversion. It might seem that we could check this condition in
18754 semantic analysis, rather than parsing, but that makes it difficult
18755 to handle something like `f()'. We want to notice that there are
18756 no decl-specifiers, and therefore realize that this is an
18757 expression, not a declaration.)
18759 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
18760 the declarator is a direct-declarator of the form "(...)".
18762 MEMBER_P is true iff this declarator is a member-declarator.
18764 FRIEND_P is true iff this declarator is a friend. */
18766 static cp_declarator *
18767 cp_parser_declarator (cp_parser* parser,
18768 cp_parser_declarator_kind dcl_kind,
18769 int* ctor_dtor_or_conv_p,
18770 bool* parenthesized_p,
18771 bool member_p, bool friend_p)
18773 cp_declarator *declarator;
18774 enum tree_code code;
18775 cp_cv_quals cv_quals;
18776 tree class_type;
18777 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
18779 /* Assume this is not a constructor, destructor, or type-conversion
18780 operator. */
18781 if (ctor_dtor_or_conv_p)
18782 *ctor_dtor_or_conv_p = 0;
18784 if (cp_parser_allow_gnu_extensions_p (parser))
18785 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
18787 /* Check for the ptr-operator production. */
18788 cp_parser_parse_tentatively (parser);
18789 /* Parse the ptr-operator. */
18790 code = cp_parser_ptr_operator (parser,
18791 &class_type,
18792 &cv_quals,
18793 &std_attributes);
18795 /* If that worked, then we have a ptr-operator. */
18796 if (cp_parser_parse_definitely (parser))
18798 /* If a ptr-operator was found, then this declarator was not
18799 parenthesized. */
18800 if (parenthesized_p)
18801 *parenthesized_p = true;
18802 /* The dependent declarator is optional if we are parsing an
18803 abstract-declarator. */
18804 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18805 cp_parser_parse_tentatively (parser);
18807 /* Parse the dependent declarator. */
18808 declarator = cp_parser_declarator (parser, dcl_kind,
18809 /*ctor_dtor_or_conv_p=*/NULL,
18810 /*parenthesized_p=*/NULL,
18811 /*member_p=*/false,
18812 friend_p);
18814 /* If we are parsing an abstract-declarator, we must handle the
18815 case where the dependent declarator is absent. */
18816 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
18817 && !cp_parser_parse_definitely (parser))
18818 declarator = NULL;
18820 declarator = cp_parser_make_indirect_declarator
18821 (code, class_type, cv_quals, declarator, std_attributes);
18823 /* Everything else is a direct-declarator. */
18824 else
18826 if (parenthesized_p)
18827 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
18828 CPP_OPEN_PAREN);
18829 declarator = cp_parser_direct_declarator (parser, dcl_kind,
18830 ctor_dtor_or_conv_p,
18831 member_p, friend_p);
18834 if (gnu_attributes && declarator && declarator != cp_error_declarator)
18835 declarator->attributes = gnu_attributes;
18836 return declarator;
18839 /* Parse a direct-declarator or direct-abstract-declarator.
18841 direct-declarator:
18842 declarator-id
18843 direct-declarator ( parameter-declaration-clause )
18844 cv-qualifier-seq [opt]
18845 ref-qualifier [opt]
18846 exception-specification [opt]
18847 direct-declarator [ constant-expression [opt] ]
18848 ( declarator )
18850 direct-abstract-declarator:
18851 direct-abstract-declarator [opt]
18852 ( parameter-declaration-clause )
18853 cv-qualifier-seq [opt]
18854 ref-qualifier [opt]
18855 exception-specification [opt]
18856 direct-abstract-declarator [opt] [ constant-expression [opt] ]
18857 ( abstract-declarator )
18859 Returns a representation of the declarator. DCL_KIND is
18860 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
18861 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
18862 we are parsing a direct-declarator. It is
18863 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
18864 of ambiguity we prefer an abstract declarator, as per
18865 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
18866 as for cp_parser_declarator. */
18868 static cp_declarator *
18869 cp_parser_direct_declarator (cp_parser* parser,
18870 cp_parser_declarator_kind dcl_kind,
18871 int* ctor_dtor_or_conv_p,
18872 bool member_p, bool friend_p)
18874 cp_token *token;
18875 cp_declarator *declarator = NULL;
18876 tree scope = NULL_TREE;
18877 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
18878 bool saved_in_declarator_p = parser->in_declarator_p;
18879 bool first = true;
18880 tree pushed_scope = NULL_TREE;
18882 while (true)
18884 /* Peek at the next token. */
18885 token = cp_lexer_peek_token (parser->lexer);
18886 if (token->type == CPP_OPEN_PAREN)
18888 /* This is either a parameter-declaration-clause, or a
18889 parenthesized declarator. When we know we are parsing a
18890 named declarator, it must be a parenthesized declarator
18891 if FIRST is true. For instance, `(int)' is a
18892 parameter-declaration-clause, with an omitted
18893 direct-abstract-declarator. But `((*))', is a
18894 parenthesized abstract declarator. Finally, when T is a
18895 template parameter `(T)' is a
18896 parameter-declaration-clause, and not a parenthesized
18897 named declarator.
18899 We first try and parse a parameter-declaration-clause,
18900 and then try a nested declarator (if FIRST is true).
18902 It is not an error for it not to be a
18903 parameter-declaration-clause, even when FIRST is
18904 false. Consider,
18906 int i (int);
18907 int i (3);
18909 The first is the declaration of a function while the
18910 second is the definition of a variable, including its
18911 initializer.
18913 Having seen only the parenthesis, we cannot know which of
18914 these two alternatives should be selected. Even more
18915 complex are examples like:
18917 int i (int (a));
18918 int i (int (3));
18920 The former is a function-declaration; the latter is a
18921 variable initialization.
18923 Thus again, we try a parameter-declaration-clause, and if
18924 that fails, we back out and return. */
18926 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
18928 tree params;
18929 bool is_declarator = false;
18931 /* In a member-declarator, the only valid interpretation
18932 of a parenthesis is the start of a
18933 parameter-declaration-clause. (It is invalid to
18934 initialize a static data member with a parenthesized
18935 initializer; only the "=" form of initialization is
18936 permitted.) */
18937 if (!member_p)
18938 cp_parser_parse_tentatively (parser);
18940 /* Consume the `('. */
18941 cp_lexer_consume_token (parser->lexer);
18942 if (first)
18944 /* If this is going to be an abstract declarator, we're
18945 in a declarator and we can't have default args. */
18946 parser->default_arg_ok_p = false;
18947 parser->in_declarator_p = true;
18950 begin_scope (sk_function_parms, NULL_TREE);
18952 /* Parse the parameter-declaration-clause. */
18953 params = cp_parser_parameter_declaration_clause (parser);
18955 /* Consume the `)'. */
18956 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18958 /* If all went well, parse the cv-qualifier-seq,
18959 ref-qualifier and the exception-specification. */
18960 if (member_p || cp_parser_parse_definitely (parser))
18962 cp_cv_quals cv_quals;
18963 cp_virt_specifiers virt_specifiers;
18964 cp_ref_qualifier ref_qual;
18965 tree exception_specification;
18966 tree late_return;
18967 tree attrs;
18968 bool memfn = (member_p || (pushed_scope
18969 && CLASS_TYPE_P (pushed_scope)));
18971 is_declarator = true;
18973 if (ctor_dtor_or_conv_p)
18974 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
18975 first = false;
18977 /* Parse the cv-qualifier-seq. */
18978 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
18979 /* Parse the ref-qualifier. */
18980 ref_qual = cp_parser_ref_qualifier_opt (parser);
18981 /* Parse the tx-qualifier. */
18982 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
18983 /* And the exception-specification. */
18984 exception_specification
18985 = cp_parser_exception_specification_opt (parser);
18987 attrs = cp_parser_std_attribute_spec_seq (parser);
18989 /* In here, we handle cases where attribute is used after
18990 the function declaration. For example:
18991 void func (int x) __attribute__((vector(..))); */
18992 tree gnu_attrs = NULL_TREE;
18993 if (flag_cilkplus
18994 && cp_next_tokens_can_be_gnu_attribute_p (parser))
18996 cp_parser_parse_tentatively (parser);
18997 tree attr = cp_parser_gnu_attributes_opt (parser);
18998 if (cp_lexer_next_token_is_not (parser->lexer,
18999 CPP_SEMICOLON)
19000 && cp_lexer_next_token_is_not (parser->lexer,
19001 CPP_OPEN_BRACE))
19002 cp_parser_abort_tentative_parse (parser);
19003 else if (!cp_parser_parse_definitely (parser))
19005 else
19006 gnu_attrs = attr;
19008 tree requires_clause = NULL_TREE;
19009 late_return = (cp_parser_late_return_type_opt
19010 (parser, declarator, requires_clause,
19011 memfn ? cv_quals : -1));
19013 /* Parse the virt-specifier-seq. */
19014 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19016 /* Create the function-declarator. */
19017 declarator = make_call_declarator (declarator,
19018 params,
19019 cv_quals,
19020 virt_specifiers,
19021 ref_qual,
19022 tx_qual,
19023 exception_specification,
19024 late_return,
19025 requires_clause);
19026 declarator->std_attributes = attrs;
19027 declarator->attributes = gnu_attrs;
19028 /* Any subsequent parameter lists are to do with
19029 return type, so are not those of the declared
19030 function. */
19031 parser->default_arg_ok_p = false;
19034 /* Remove the function parms from scope. */
19035 pop_bindings_and_leave_scope ();
19037 if (is_declarator)
19038 /* Repeat the main loop. */
19039 continue;
19042 /* If this is the first, we can try a parenthesized
19043 declarator. */
19044 if (first)
19046 bool saved_in_type_id_in_expr_p;
19048 parser->default_arg_ok_p = saved_default_arg_ok_p;
19049 parser->in_declarator_p = saved_in_declarator_p;
19051 /* Consume the `('. */
19052 cp_lexer_consume_token (parser->lexer);
19053 /* Parse the nested declarator. */
19054 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19055 parser->in_type_id_in_expr_p = true;
19056 declarator
19057 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19058 /*parenthesized_p=*/NULL,
19059 member_p, friend_p);
19060 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19061 first = false;
19062 /* Expect a `)'. */
19063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19064 declarator = cp_error_declarator;
19065 if (declarator == cp_error_declarator)
19066 break;
19068 goto handle_declarator;
19070 /* Otherwise, we must be done. */
19071 else
19072 break;
19074 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19075 && token->type == CPP_OPEN_SQUARE
19076 && !cp_next_tokens_can_be_attribute_p (parser))
19078 /* Parse an array-declarator. */
19079 tree bounds, attrs;
19081 if (ctor_dtor_or_conv_p)
19082 *ctor_dtor_or_conv_p = 0;
19084 first = false;
19085 parser->default_arg_ok_p = false;
19086 parser->in_declarator_p = true;
19087 /* Consume the `['. */
19088 cp_lexer_consume_token (parser->lexer);
19089 /* Peek at the next token. */
19090 token = cp_lexer_peek_token (parser->lexer);
19091 /* If the next token is `]', then there is no
19092 constant-expression. */
19093 if (token->type != CPP_CLOSE_SQUARE)
19095 bool non_constant_p;
19096 bounds
19097 = cp_parser_constant_expression (parser,
19098 /*allow_non_constant=*/true,
19099 &non_constant_p);
19100 if (!non_constant_p)
19101 /* OK */;
19102 else if (error_operand_p (bounds))
19103 /* Already gave an error. */;
19104 else if (!parser->in_function_body
19105 || current_binding_level->kind == sk_function_parms)
19107 /* Normally, the array bound must be an integral constant
19108 expression. However, as an extension, we allow VLAs
19109 in function scopes as long as they aren't part of a
19110 parameter declaration. */
19111 cp_parser_error (parser,
19112 "array bound is not an integer constant");
19113 bounds = error_mark_node;
19115 else if (processing_template_decl
19116 && !type_dependent_expression_p (bounds))
19118 /* Remember this wasn't a constant-expression. */
19119 bounds = build_nop (TREE_TYPE (bounds), bounds);
19120 TREE_SIDE_EFFECTS (bounds) = 1;
19123 else
19124 bounds = NULL_TREE;
19125 /* Look for the closing `]'. */
19126 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19128 declarator = cp_error_declarator;
19129 break;
19132 attrs = cp_parser_std_attribute_spec_seq (parser);
19133 declarator = make_array_declarator (declarator, bounds);
19134 declarator->std_attributes = attrs;
19136 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19139 tree qualifying_scope;
19140 tree unqualified_name;
19141 tree attrs;
19142 special_function_kind sfk;
19143 bool abstract_ok;
19144 bool pack_expansion_p = false;
19145 cp_token *declarator_id_start_token;
19147 /* Parse a declarator-id */
19148 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19149 if (abstract_ok)
19151 cp_parser_parse_tentatively (parser);
19153 /* If we see an ellipsis, we should be looking at a
19154 parameter pack. */
19155 if (token->type == CPP_ELLIPSIS)
19157 /* Consume the `...' */
19158 cp_lexer_consume_token (parser->lexer);
19160 pack_expansion_p = true;
19164 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19165 unqualified_name
19166 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19167 qualifying_scope = parser->scope;
19168 if (abstract_ok)
19170 bool okay = false;
19172 if (!unqualified_name && pack_expansion_p)
19174 /* Check whether an error occurred. */
19175 okay = !cp_parser_error_occurred (parser);
19177 /* We already consumed the ellipsis to mark a
19178 parameter pack, but we have no way to report it,
19179 so abort the tentative parse. We will be exiting
19180 immediately anyway. */
19181 cp_parser_abort_tentative_parse (parser);
19183 else
19184 okay = cp_parser_parse_definitely (parser);
19186 if (!okay)
19187 unqualified_name = error_mark_node;
19188 else if (unqualified_name
19189 && (qualifying_scope
19190 || (!identifier_p (unqualified_name))))
19192 cp_parser_error (parser, "expected unqualified-id");
19193 unqualified_name = error_mark_node;
19197 if (!unqualified_name)
19198 return NULL;
19199 if (unqualified_name == error_mark_node)
19201 declarator = cp_error_declarator;
19202 pack_expansion_p = false;
19203 declarator->parameter_pack_p = false;
19204 break;
19207 attrs = cp_parser_std_attribute_spec_seq (parser);
19209 if (qualifying_scope && at_namespace_scope_p ()
19210 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19212 /* In the declaration of a member of a template class
19213 outside of the class itself, the SCOPE will sometimes
19214 be a TYPENAME_TYPE. For example, given:
19216 template <typename T>
19217 int S<T>::R::i = 3;
19219 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19220 this context, we must resolve S<T>::R to an ordinary
19221 type, rather than a typename type.
19223 The reason we normally avoid resolving TYPENAME_TYPEs
19224 is that a specialization of `S' might render
19225 `S<T>::R' not a type. However, if `S' is
19226 specialized, then this `i' will not be used, so there
19227 is no harm in resolving the types here. */
19228 tree type;
19230 /* Resolve the TYPENAME_TYPE. */
19231 type = resolve_typename_type (qualifying_scope,
19232 /*only_current_p=*/false);
19233 /* If that failed, the declarator is invalid. */
19234 if (TREE_CODE (type) == TYPENAME_TYPE)
19236 if (typedef_variant_p (type))
19237 error_at (declarator_id_start_token->location,
19238 "cannot define member of dependent typedef "
19239 "%qT", type);
19240 else
19241 error_at (declarator_id_start_token->location,
19242 "%<%T::%E%> is not a type",
19243 TYPE_CONTEXT (qualifying_scope),
19244 TYPE_IDENTIFIER (qualifying_scope));
19246 qualifying_scope = type;
19249 sfk = sfk_none;
19251 if (unqualified_name)
19253 tree class_type;
19255 if (qualifying_scope
19256 && CLASS_TYPE_P (qualifying_scope))
19257 class_type = qualifying_scope;
19258 else
19259 class_type = current_class_type;
19261 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19263 tree name_type = TREE_TYPE (unqualified_name);
19264 if (class_type && same_type_p (name_type, class_type))
19266 if (qualifying_scope
19267 && CLASSTYPE_USE_TEMPLATE (name_type))
19269 error_at (declarator_id_start_token->location,
19270 "invalid use of constructor as a template");
19271 inform (declarator_id_start_token->location,
19272 "use %<%T::%D%> instead of %<%T::%D%> to "
19273 "name the constructor in a qualified name",
19274 class_type,
19275 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19276 class_type, name_type);
19277 declarator = cp_error_declarator;
19278 break;
19280 else
19281 unqualified_name = constructor_name (class_type);
19283 else
19285 /* We do not attempt to print the declarator
19286 here because we do not have enough
19287 information about its original syntactic
19288 form. */
19289 cp_parser_error (parser, "invalid declarator");
19290 declarator = cp_error_declarator;
19291 break;
19295 if (class_type)
19297 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
19298 sfk = sfk_destructor;
19299 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
19300 sfk = sfk_conversion;
19301 else if (/* There's no way to declare a constructor
19302 for an anonymous type, even if the type
19303 got a name for linkage purposes. */
19304 !TYPE_WAS_ANONYMOUS (class_type)
19305 /* Handle correctly (c++/19200):
19307 struct S {
19308 struct T{};
19309 friend void S(T);
19312 and also:
19314 namespace N {
19315 void S();
19318 struct S {
19319 friend void N::S();
19320 }; */
19321 && !(friend_p
19322 && class_type != qualifying_scope)
19323 && constructor_name_p (unqualified_name,
19324 class_type))
19326 unqualified_name = constructor_name (class_type);
19327 sfk = sfk_constructor;
19329 else if (is_overloaded_fn (unqualified_name)
19330 && DECL_CONSTRUCTOR_P (get_first_fn
19331 (unqualified_name)))
19332 sfk = sfk_constructor;
19334 if (ctor_dtor_or_conv_p && sfk != sfk_none)
19335 *ctor_dtor_or_conv_p = -1;
19338 declarator = make_id_declarator (qualifying_scope,
19339 unqualified_name,
19340 sfk);
19341 declarator->std_attributes = attrs;
19342 declarator->id_loc = token->location;
19343 declarator->parameter_pack_p = pack_expansion_p;
19345 if (pack_expansion_p)
19346 maybe_warn_variadic_templates ();
19349 handle_declarator:;
19350 scope = get_scope_of_declarator (declarator);
19351 if (scope)
19353 /* Any names that appear after the declarator-id for a
19354 member are looked up in the containing scope. */
19355 if (at_function_scope_p ())
19357 /* But declarations with qualified-ids can't appear in a
19358 function. */
19359 cp_parser_error (parser, "qualified-id in declaration");
19360 declarator = cp_error_declarator;
19361 break;
19363 pushed_scope = push_scope (scope);
19365 parser->in_declarator_p = true;
19366 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
19367 || (declarator && declarator->kind == cdk_id))
19368 /* Default args are only allowed on function
19369 declarations. */
19370 parser->default_arg_ok_p = saved_default_arg_ok_p;
19371 else
19372 parser->default_arg_ok_p = false;
19374 first = false;
19376 /* We're done. */
19377 else
19378 break;
19381 /* For an abstract declarator, we might wind up with nothing at this
19382 point. That's an error; the declarator is not optional. */
19383 if (!declarator)
19384 cp_parser_error (parser, "expected declarator");
19386 /* If we entered a scope, we must exit it now. */
19387 if (pushed_scope)
19388 pop_scope (pushed_scope);
19390 parser->default_arg_ok_p = saved_default_arg_ok_p;
19391 parser->in_declarator_p = saved_in_declarator_p;
19393 return declarator;
19396 /* Parse a ptr-operator.
19398 ptr-operator:
19399 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19400 * cv-qualifier-seq [opt]
19402 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
19403 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
19405 GNU Extension:
19407 ptr-operator:
19408 & cv-qualifier-seq [opt]
19410 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
19411 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
19412 an rvalue reference. In the case of a pointer-to-member, *TYPE is
19413 filled in with the TYPE containing the member. *CV_QUALS is
19414 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
19415 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
19416 Note that the tree codes returned by this function have nothing
19417 to do with the types of trees that will be eventually be created
19418 to represent the pointer or reference type being parsed. They are
19419 just constants with suggestive names. */
19420 static enum tree_code
19421 cp_parser_ptr_operator (cp_parser* parser,
19422 tree* type,
19423 cp_cv_quals *cv_quals,
19424 tree *attributes)
19426 enum tree_code code = ERROR_MARK;
19427 cp_token *token;
19428 tree attrs = NULL_TREE;
19430 /* Assume that it's not a pointer-to-member. */
19431 *type = NULL_TREE;
19432 /* And that there are no cv-qualifiers. */
19433 *cv_quals = TYPE_UNQUALIFIED;
19435 /* Peek at the next token. */
19436 token = cp_lexer_peek_token (parser->lexer);
19438 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
19439 if (token->type == CPP_MULT)
19440 code = INDIRECT_REF;
19441 else if (token->type == CPP_AND)
19442 code = ADDR_EXPR;
19443 else if ((cxx_dialect != cxx98) &&
19444 token->type == CPP_AND_AND) /* C++0x only */
19445 code = NON_LVALUE_EXPR;
19447 if (code != ERROR_MARK)
19449 /* Consume the `*', `&' or `&&'. */
19450 cp_lexer_consume_token (parser->lexer);
19452 /* A `*' can be followed by a cv-qualifier-seq, and so can a
19453 `&', if we are allowing GNU extensions. (The only qualifier
19454 that can legally appear after `&' is `restrict', but that is
19455 enforced during semantic analysis. */
19456 if (code == INDIRECT_REF
19457 || cp_parser_allow_gnu_extensions_p (parser))
19458 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19460 attrs = cp_parser_std_attribute_spec_seq (parser);
19461 if (attributes != NULL)
19462 *attributes = attrs;
19464 else
19466 /* Try the pointer-to-member case. */
19467 cp_parser_parse_tentatively (parser);
19468 /* Look for the optional `::' operator. */
19469 cp_parser_global_scope_opt (parser,
19470 /*current_scope_valid_p=*/false);
19471 /* Look for the nested-name specifier. */
19472 token = cp_lexer_peek_token (parser->lexer);
19473 cp_parser_nested_name_specifier (parser,
19474 /*typename_keyword_p=*/false,
19475 /*check_dependency_p=*/true,
19476 /*type_p=*/false,
19477 /*is_declaration=*/false);
19478 /* If we found it, and the next token is a `*', then we are
19479 indeed looking at a pointer-to-member operator. */
19480 if (!cp_parser_error_occurred (parser)
19481 && cp_parser_require (parser, CPP_MULT, RT_MULT))
19483 /* Indicate that the `*' operator was used. */
19484 code = INDIRECT_REF;
19486 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
19487 error_at (token->location, "%qD is a namespace", parser->scope);
19488 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
19489 error_at (token->location, "cannot form pointer to member of "
19490 "non-class %q#T", parser->scope);
19491 else
19493 /* The type of which the member is a member is given by the
19494 current SCOPE. */
19495 *type = parser->scope;
19496 /* The next name will not be qualified. */
19497 parser->scope = NULL_TREE;
19498 parser->qualifying_scope = NULL_TREE;
19499 parser->object_scope = NULL_TREE;
19500 /* Look for optional c++11 attributes. */
19501 attrs = cp_parser_std_attribute_spec_seq (parser);
19502 if (attributes != NULL)
19503 *attributes = attrs;
19504 /* Look for the optional cv-qualifier-seq. */
19505 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19508 /* If that didn't work we don't have a ptr-operator. */
19509 if (!cp_parser_parse_definitely (parser))
19510 cp_parser_error (parser, "expected ptr-operator");
19513 return code;
19516 /* Parse an (optional) cv-qualifier-seq.
19518 cv-qualifier-seq:
19519 cv-qualifier cv-qualifier-seq [opt]
19521 cv-qualifier:
19522 const
19523 volatile
19525 GNU Extension:
19527 cv-qualifier:
19528 __restrict__
19530 Returns a bitmask representing the cv-qualifiers. */
19532 static cp_cv_quals
19533 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
19535 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
19537 while (true)
19539 cp_token *token;
19540 cp_cv_quals cv_qualifier;
19542 /* Peek at the next token. */
19543 token = cp_lexer_peek_token (parser->lexer);
19544 /* See if it's a cv-qualifier. */
19545 switch (token->keyword)
19547 case RID_CONST:
19548 cv_qualifier = TYPE_QUAL_CONST;
19549 break;
19551 case RID_VOLATILE:
19552 cv_qualifier = TYPE_QUAL_VOLATILE;
19553 break;
19555 case RID_RESTRICT:
19556 cv_qualifier = TYPE_QUAL_RESTRICT;
19557 break;
19559 default:
19560 cv_qualifier = TYPE_UNQUALIFIED;
19561 break;
19564 if (!cv_qualifier)
19565 break;
19567 if (cv_quals & cv_qualifier)
19569 error_at (token->location, "duplicate cv-qualifier");
19570 cp_lexer_purge_token (parser->lexer);
19572 else
19574 cp_lexer_consume_token (parser->lexer);
19575 cv_quals |= cv_qualifier;
19579 return cv_quals;
19582 /* Parse an (optional) ref-qualifier
19584 ref-qualifier:
19588 Returns cp_ref_qualifier representing ref-qualifier. */
19590 static cp_ref_qualifier
19591 cp_parser_ref_qualifier_opt (cp_parser* parser)
19593 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
19595 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
19596 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
19597 return ref_qual;
19599 while (true)
19601 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
19602 cp_token *token = cp_lexer_peek_token (parser->lexer);
19604 switch (token->type)
19606 case CPP_AND:
19607 curr_ref_qual = REF_QUAL_LVALUE;
19608 break;
19610 case CPP_AND_AND:
19611 curr_ref_qual = REF_QUAL_RVALUE;
19612 break;
19614 default:
19615 curr_ref_qual = REF_QUAL_NONE;
19616 break;
19619 if (!curr_ref_qual)
19620 break;
19621 else if (ref_qual)
19623 error_at (token->location, "multiple ref-qualifiers");
19624 cp_lexer_purge_token (parser->lexer);
19626 else
19628 ref_qual = curr_ref_qual;
19629 cp_lexer_consume_token (parser->lexer);
19633 return ref_qual;
19636 /* Parse an optional tx-qualifier.
19638 tx-qualifier:
19639 transaction_safe
19640 transaction_safe_dynamic */
19642 static tree
19643 cp_parser_tx_qualifier_opt (cp_parser *parser)
19645 cp_token *token = cp_lexer_peek_token (parser->lexer);
19646 if (token->type == CPP_NAME)
19648 tree name = token->u.value;
19649 const char *p = IDENTIFIER_POINTER (name);
19650 const int len = strlen ("transaction_safe");
19651 if (!strncmp (p, "transaction_safe", len))
19653 p += len;
19654 if (*p == '\0'
19655 || !strcmp (p, "_dynamic"))
19657 cp_lexer_consume_token (parser->lexer);
19658 if (!flag_tm)
19660 error ("%E requires %<-fgnu-tm%>", name);
19661 return NULL_TREE;
19663 else
19664 return name;
19668 return NULL_TREE;
19671 /* Parse an (optional) virt-specifier-seq.
19673 virt-specifier-seq:
19674 virt-specifier virt-specifier-seq [opt]
19676 virt-specifier:
19677 override
19678 final
19680 Returns a bitmask representing the virt-specifiers. */
19682 static cp_virt_specifiers
19683 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
19685 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
19687 while (true)
19689 cp_token *token;
19690 cp_virt_specifiers virt_specifier;
19692 /* Peek at the next token. */
19693 token = cp_lexer_peek_token (parser->lexer);
19694 /* See if it's a virt-specifier-qualifier. */
19695 if (token->type != CPP_NAME)
19696 break;
19697 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
19699 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19700 virt_specifier = VIRT_SPEC_OVERRIDE;
19702 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
19704 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
19705 virt_specifier = VIRT_SPEC_FINAL;
19707 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
19709 virt_specifier = VIRT_SPEC_FINAL;
19711 else
19712 break;
19714 if (virt_specifiers & virt_specifier)
19716 error_at (token->location, "duplicate virt-specifier");
19717 cp_lexer_purge_token (parser->lexer);
19719 else
19721 cp_lexer_consume_token (parser->lexer);
19722 virt_specifiers |= virt_specifier;
19725 return virt_specifiers;
19728 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
19729 is in scope even though it isn't real. */
19731 void
19732 inject_this_parameter (tree ctype, cp_cv_quals quals)
19734 tree this_parm;
19736 if (current_class_ptr)
19738 /* We don't clear this between NSDMIs. Is it already what we want? */
19739 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
19740 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
19741 && cp_type_quals (type) == quals)
19742 return;
19745 this_parm = build_this_parm (ctype, quals);
19746 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
19747 current_class_ptr = NULL_TREE;
19748 current_class_ref
19749 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
19750 current_class_ptr = this_parm;
19753 /* Return true iff our current scope is a non-static data member
19754 initializer. */
19756 bool
19757 parsing_nsdmi (void)
19759 /* We recognize NSDMI context by the context-less 'this' pointer set up
19760 by the function above. */
19761 if (current_class_ptr
19762 && TREE_CODE (current_class_ptr) == PARM_DECL
19763 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
19764 return true;
19765 return false;
19768 /* Parse a late-specified return type, if any. This is not a separate
19769 non-terminal, but part of a function declarator, which looks like
19771 -> trailing-type-specifier-seq abstract-declarator(opt)
19773 Returns the type indicated by the type-id.
19775 In addition to this, parse any queued up omp declare simd
19776 clauses and Cilk Plus SIMD-enabled function's vector attributes.
19778 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
19779 function. */
19781 static tree
19782 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
19783 tree& requires_clause, cp_cv_quals quals)
19785 cp_token *token;
19786 tree type = NULL_TREE;
19787 bool declare_simd_p = (parser->omp_declare_simd
19788 && declarator
19789 && declarator->kind == cdk_id);
19791 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
19792 && declarator && declarator->kind == cdk_id);
19794 bool oacc_routine_p = (parser->oacc_routine
19795 && declarator
19796 && declarator->kind == cdk_id);
19798 /* Peek at the next token. */
19799 token = cp_lexer_peek_token (parser->lexer);
19800 /* A late-specified return type is indicated by an initial '->'. */
19801 if (token->type != CPP_DEREF
19802 && token->keyword != RID_REQUIRES
19803 && !(token->type == CPP_NAME
19804 && token->u.value == ridpointers[RID_REQUIRES])
19805 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
19806 return NULL_TREE;
19808 tree save_ccp = current_class_ptr;
19809 tree save_ccr = current_class_ref;
19810 if (quals >= 0)
19812 /* DR 1207: 'this' is in scope in the trailing return type. */
19813 inject_this_parameter (current_class_type, quals);
19816 if (token->type == CPP_DEREF)
19818 /* Consume the ->. */
19819 cp_lexer_consume_token (parser->lexer);
19821 type = cp_parser_trailing_type_id (parser);
19824 /* Function declarations may be followed by a trailing
19825 requires-clause. */
19826 requires_clause = cp_parser_requires_clause_opt (parser);
19828 if (cilk_simd_fn_vector_p)
19829 declarator->attributes
19830 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
19831 declarator->attributes);
19832 if (declare_simd_p)
19833 declarator->attributes
19834 = cp_parser_late_parsing_omp_declare_simd (parser,
19835 declarator->attributes);
19836 if (oacc_routine_p)
19837 declarator->attributes
19838 = cp_parser_late_parsing_oacc_routine (parser,
19839 declarator->attributes);
19841 if (quals >= 0)
19843 current_class_ptr = save_ccp;
19844 current_class_ref = save_ccr;
19847 return type;
19850 /* Parse a declarator-id.
19852 declarator-id:
19853 id-expression
19854 :: [opt] nested-name-specifier [opt] type-name
19856 In the `id-expression' case, the value returned is as for
19857 cp_parser_id_expression if the id-expression was an unqualified-id.
19858 If the id-expression was a qualified-id, then a SCOPE_REF is
19859 returned. The first operand is the scope (either a NAMESPACE_DECL
19860 or TREE_TYPE), but the second is still just a representation of an
19861 unqualified-id. */
19863 static tree
19864 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
19866 tree id;
19867 /* The expression must be an id-expression. Assume that qualified
19868 names are the names of types so that:
19870 template <class T>
19871 int S<T>::R::i = 3;
19873 will work; we must treat `S<T>::R' as the name of a type.
19874 Similarly, assume that qualified names are templates, where
19875 required, so that:
19877 template <class T>
19878 int S<T>::R<T>::i = 3;
19880 will work, too. */
19881 id = cp_parser_id_expression (parser,
19882 /*template_keyword_p=*/false,
19883 /*check_dependency_p=*/false,
19884 /*template_p=*/NULL,
19885 /*declarator_p=*/true,
19886 optional_p);
19887 if (id && BASELINK_P (id))
19888 id = BASELINK_FUNCTIONS (id);
19889 return id;
19892 /* Parse a type-id.
19894 type-id:
19895 type-specifier-seq abstract-declarator [opt]
19897 Returns the TYPE specified. */
19899 static tree
19900 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
19901 bool is_trailing_return)
19903 cp_decl_specifier_seq type_specifier_seq;
19904 cp_declarator *abstract_declarator;
19906 /* Parse the type-specifier-seq. */
19907 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
19908 is_trailing_return,
19909 &type_specifier_seq);
19910 if (type_specifier_seq.type == error_mark_node)
19911 return error_mark_node;
19913 /* There might or might not be an abstract declarator. */
19914 cp_parser_parse_tentatively (parser);
19915 /* Look for the declarator. */
19916 abstract_declarator
19917 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
19918 /*parenthesized_p=*/NULL,
19919 /*member_p=*/false,
19920 /*friend_p=*/false);
19921 /* Check to see if there really was a declarator. */
19922 if (!cp_parser_parse_definitely (parser))
19923 abstract_declarator = NULL;
19925 if (type_specifier_seq.type
19926 /* The concepts TS allows 'auto' as a type-id. */
19927 && (!flag_concepts || parser->in_type_id_in_expr_p)
19928 /* None of the valid uses of 'auto' in C++14 involve the type-id
19929 nonterminal, but it is valid in a trailing-return-type. */
19930 && !(cxx_dialect >= cxx14 && is_trailing_return)
19931 && type_uses_auto (type_specifier_seq.type))
19933 /* A type-id with type 'auto' is only ok if the abstract declarator
19934 is a function declarator with a late-specified return type.
19936 A type-id with 'auto' is also valid in a trailing-return-type
19937 in a compound-requirement. */
19938 if (abstract_declarator
19939 && abstract_declarator->kind == cdk_function
19940 && abstract_declarator->u.function.late_return_type)
19941 /* OK */;
19942 else if (parser->in_result_type_constraint_p)
19943 /* OK */;
19944 else
19946 error ("invalid use of %<auto%>");
19947 return error_mark_node;
19951 return groktypename (&type_specifier_seq, abstract_declarator,
19952 is_template_arg);
19955 static tree
19956 cp_parser_type_id (cp_parser *parser)
19958 return cp_parser_type_id_1 (parser, false, false);
19961 static tree
19962 cp_parser_template_type_arg (cp_parser *parser)
19964 tree r;
19965 const char *saved_message = parser->type_definition_forbidden_message;
19966 parser->type_definition_forbidden_message
19967 = G_("types may not be defined in template arguments");
19968 r = cp_parser_type_id_1 (parser, true, false);
19969 parser->type_definition_forbidden_message = saved_message;
19970 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
19972 error ("invalid use of %<auto%> in template argument");
19973 r = error_mark_node;
19975 return r;
19978 static tree
19979 cp_parser_trailing_type_id (cp_parser *parser)
19981 return cp_parser_type_id_1 (parser, false, true);
19984 /* Parse a type-specifier-seq.
19986 type-specifier-seq:
19987 type-specifier type-specifier-seq [opt]
19989 GNU extension:
19991 type-specifier-seq:
19992 attributes type-specifier-seq [opt]
19994 If IS_DECLARATION is true, we are at the start of a "condition" or
19995 exception-declaration, so we might be followed by a declarator-id.
19997 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
19998 i.e. we've just seen "->".
20000 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20002 static void
20003 cp_parser_type_specifier_seq (cp_parser* parser,
20004 bool is_declaration,
20005 bool is_trailing_return,
20006 cp_decl_specifier_seq *type_specifier_seq)
20008 bool seen_type_specifier = false;
20009 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20010 cp_token *start_token = NULL;
20012 /* Clear the TYPE_SPECIFIER_SEQ. */
20013 clear_decl_specs (type_specifier_seq);
20015 /* In the context of a trailing return type, enum E { } is an
20016 elaborated-type-specifier followed by a function-body, not an
20017 enum-specifier. */
20018 if (is_trailing_return)
20019 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20021 /* Parse the type-specifiers and attributes. */
20022 while (true)
20024 tree type_specifier;
20025 bool is_cv_qualifier;
20027 /* Check for attributes first. */
20028 if (cp_next_tokens_can_be_attribute_p (parser))
20030 type_specifier_seq->attributes =
20031 chainon (type_specifier_seq->attributes,
20032 cp_parser_attributes_opt (parser));
20033 continue;
20036 /* record the token of the beginning of the type specifier seq,
20037 for error reporting purposes*/
20038 if (!start_token)
20039 start_token = cp_lexer_peek_token (parser->lexer);
20041 /* Look for the type-specifier. */
20042 type_specifier = cp_parser_type_specifier (parser,
20043 flags,
20044 type_specifier_seq,
20045 /*is_declaration=*/false,
20046 NULL,
20047 &is_cv_qualifier);
20048 if (!type_specifier)
20050 /* If the first type-specifier could not be found, this is not a
20051 type-specifier-seq at all. */
20052 if (!seen_type_specifier)
20054 /* Set in_declarator_p to avoid skipping to the semicolon. */
20055 int in_decl = parser->in_declarator_p;
20056 parser->in_declarator_p = true;
20058 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20059 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20060 cp_parser_error (parser, "expected type-specifier");
20062 parser->in_declarator_p = in_decl;
20064 type_specifier_seq->type = error_mark_node;
20065 return;
20067 /* If subsequent type-specifiers could not be found, the
20068 type-specifier-seq is complete. */
20069 break;
20072 seen_type_specifier = true;
20073 /* The standard says that a condition can be:
20075 type-specifier-seq declarator = assignment-expression
20077 However, given:
20079 struct S {};
20080 if (int S = ...)
20082 we should treat the "S" as a declarator, not as a
20083 type-specifier. The standard doesn't say that explicitly for
20084 type-specifier-seq, but it does say that for
20085 decl-specifier-seq in an ordinary declaration. Perhaps it
20086 would be clearer just to allow a decl-specifier-seq here, and
20087 then add a semantic restriction that if any decl-specifiers
20088 that are not type-specifiers appear, the program is invalid. */
20089 if (is_declaration && !is_cv_qualifier)
20090 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20094 /* Return whether the function currently being declared has an associated
20095 template parameter list. */
20097 static bool
20098 function_being_declared_is_template_p (cp_parser* parser)
20100 if (!current_template_parms || processing_template_parmlist)
20101 return false;
20103 if (parser->implicit_template_scope)
20104 return true;
20106 if (at_class_scope_p ()
20107 && TYPE_BEING_DEFINED (current_class_type))
20108 return parser->num_template_parameter_lists != 0;
20110 return ((int) parser->num_template_parameter_lists > template_class_depth
20111 (current_class_type));
20114 /* Parse a parameter-declaration-clause.
20116 parameter-declaration-clause:
20117 parameter-declaration-list [opt] ... [opt]
20118 parameter-declaration-list , ...
20120 Returns a representation for the parameter declarations. A return
20121 value of NULL indicates a parameter-declaration-clause consisting
20122 only of an ellipsis. */
20124 static tree
20125 cp_parser_parameter_declaration_clause (cp_parser* parser)
20127 tree parameters;
20128 cp_token *token;
20129 bool ellipsis_p;
20130 bool is_error;
20132 struct cleanup {
20133 cp_parser* parser;
20134 int auto_is_implicit_function_template_parm_p;
20135 ~cleanup() {
20136 parser->auto_is_implicit_function_template_parm_p
20137 = auto_is_implicit_function_template_parm_p;
20139 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20141 (void) cleanup;
20143 if (!processing_specialization
20144 && !processing_template_parmlist
20145 && !processing_explicit_instantiation)
20146 if (!current_function_decl
20147 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20148 parser->auto_is_implicit_function_template_parm_p = true;
20150 /* Peek at the next token. */
20151 token = cp_lexer_peek_token (parser->lexer);
20152 /* Check for trivial parameter-declaration-clauses. */
20153 if (token->type == CPP_ELLIPSIS)
20155 /* Consume the `...' token. */
20156 cp_lexer_consume_token (parser->lexer);
20157 return NULL_TREE;
20159 else if (token->type == CPP_CLOSE_PAREN)
20160 /* There are no parameters. */
20162 #ifndef NO_IMPLICIT_EXTERN_C
20163 if (in_system_header_at (input_location)
20164 && current_class_type == NULL
20165 && current_lang_name == lang_name_c)
20166 return NULL_TREE;
20167 else
20168 #endif
20169 return void_list_node;
20171 /* Check for `(void)', too, which is a special case. */
20172 else if (token->keyword == RID_VOID
20173 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20174 == CPP_CLOSE_PAREN))
20176 /* Consume the `void' token. */
20177 cp_lexer_consume_token (parser->lexer);
20178 /* There are no parameters. */
20179 return void_list_node;
20182 /* Parse the parameter-declaration-list. */
20183 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20184 /* If a parse error occurred while parsing the
20185 parameter-declaration-list, then the entire
20186 parameter-declaration-clause is erroneous. */
20187 if (is_error)
20188 return NULL;
20190 /* Peek at the next token. */
20191 token = cp_lexer_peek_token (parser->lexer);
20192 /* If it's a `,', the clause should terminate with an ellipsis. */
20193 if (token->type == CPP_COMMA)
20195 /* Consume the `,'. */
20196 cp_lexer_consume_token (parser->lexer);
20197 /* Expect an ellipsis. */
20198 ellipsis_p
20199 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20201 /* It might also be `...' if the optional trailing `,' was
20202 omitted. */
20203 else if (token->type == CPP_ELLIPSIS)
20205 /* Consume the `...' token. */
20206 cp_lexer_consume_token (parser->lexer);
20207 /* And remember that we saw it. */
20208 ellipsis_p = true;
20210 else
20211 ellipsis_p = false;
20213 /* Finish the parameter list. */
20214 if (!ellipsis_p)
20215 parameters = chainon (parameters, void_list_node);
20217 return parameters;
20220 /* Parse a parameter-declaration-list.
20222 parameter-declaration-list:
20223 parameter-declaration
20224 parameter-declaration-list , parameter-declaration
20226 Returns a representation of the parameter-declaration-list, as for
20227 cp_parser_parameter_declaration_clause. However, the
20228 `void_list_node' is never appended to the list. Upon return,
20229 *IS_ERROR will be true iff an error occurred. */
20231 static tree
20232 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20234 tree parameters = NULL_TREE;
20235 tree *tail = &parameters;
20236 bool saved_in_unbraced_linkage_specification_p;
20237 int index = 0;
20239 /* Assume all will go well. */
20240 *is_error = false;
20241 /* The special considerations that apply to a function within an
20242 unbraced linkage specifications do not apply to the parameters
20243 to the function. */
20244 saved_in_unbraced_linkage_specification_p
20245 = parser->in_unbraced_linkage_specification_p;
20246 parser->in_unbraced_linkage_specification_p = false;
20248 /* Look for more parameters. */
20249 while (true)
20251 cp_parameter_declarator *parameter;
20252 tree decl = error_mark_node;
20253 bool parenthesized_p = false;
20254 int template_parm_idx = (function_being_declared_is_template_p (parser)?
20255 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
20256 (current_template_parms)) : 0);
20258 /* Parse the parameter. */
20259 parameter
20260 = cp_parser_parameter_declaration (parser,
20261 /*template_parm_p=*/false,
20262 &parenthesized_p);
20264 /* We don't know yet if the enclosing context is deprecated, so wait
20265 and warn in grokparms if appropriate. */
20266 deprecated_state = DEPRECATED_SUPPRESS;
20268 if (parameter)
20270 /* If a function parameter pack was specified and an implicit template
20271 parameter was introduced during cp_parser_parameter_declaration,
20272 change any implicit parameters introduced into packs. */
20273 if (parser->implicit_template_parms
20274 && parameter->declarator
20275 && parameter->declarator->parameter_pack_p)
20277 int latest_template_parm_idx = TREE_VEC_LENGTH
20278 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
20280 if (latest_template_parm_idx != template_parm_idx)
20281 parameter->decl_specifiers.type = convert_generic_types_to_packs
20282 (parameter->decl_specifiers.type,
20283 template_parm_idx, latest_template_parm_idx);
20286 decl = grokdeclarator (parameter->declarator,
20287 &parameter->decl_specifiers,
20288 PARM,
20289 parameter->default_argument != NULL_TREE,
20290 &parameter->decl_specifiers.attributes);
20293 deprecated_state = DEPRECATED_NORMAL;
20295 /* If a parse error occurred parsing the parameter declaration,
20296 then the entire parameter-declaration-list is erroneous. */
20297 if (decl == error_mark_node)
20299 *is_error = true;
20300 parameters = error_mark_node;
20301 break;
20304 if (parameter->decl_specifiers.attributes)
20305 cplus_decl_attributes (&decl,
20306 parameter->decl_specifiers.attributes,
20308 if (DECL_NAME (decl))
20309 decl = pushdecl (decl);
20311 if (decl != error_mark_node)
20313 retrofit_lang_decl (decl);
20314 DECL_PARM_INDEX (decl) = ++index;
20315 DECL_PARM_LEVEL (decl) = function_parm_depth ();
20318 /* Add the new parameter to the list. */
20319 *tail = build_tree_list (parameter->default_argument, decl);
20320 tail = &TREE_CHAIN (*tail);
20322 /* Peek at the next token. */
20323 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
20324 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
20325 /* These are for Objective-C++ */
20326 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20327 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20328 /* The parameter-declaration-list is complete. */
20329 break;
20330 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20332 cp_token *token;
20334 /* Peek at the next token. */
20335 token = cp_lexer_peek_nth_token (parser->lexer, 2);
20336 /* If it's an ellipsis, then the list is complete. */
20337 if (token->type == CPP_ELLIPSIS)
20338 break;
20339 /* Otherwise, there must be more parameters. Consume the
20340 `,'. */
20341 cp_lexer_consume_token (parser->lexer);
20342 /* When parsing something like:
20344 int i(float f, double d)
20346 we can tell after seeing the declaration for "f" that we
20347 are not looking at an initialization of a variable "i",
20348 but rather at the declaration of a function "i".
20350 Due to the fact that the parsing of template arguments
20351 (as specified to a template-id) requires backtracking we
20352 cannot use this technique when inside a template argument
20353 list. */
20354 if (!parser->in_template_argument_list_p
20355 && !parser->in_type_id_in_expr_p
20356 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20357 /* However, a parameter-declaration of the form
20358 "float(f)" (which is a valid declaration of a
20359 parameter "f") can also be interpreted as an
20360 expression (the conversion of "f" to "float"). */
20361 && !parenthesized_p)
20362 cp_parser_commit_to_tentative_parse (parser);
20364 else
20366 cp_parser_error (parser, "expected %<,%> or %<...%>");
20367 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20368 cp_parser_skip_to_closing_parenthesis (parser,
20369 /*recovering=*/true,
20370 /*or_comma=*/false,
20371 /*consume_paren=*/false);
20372 break;
20376 parser->in_unbraced_linkage_specification_p
20377 = saved_in_unbraced_linkage_specification_p;
20379 /* Reset implicit_template_scope if we are about to leave the function
20380 parameter list that introduced it. Note that for out-of-line member
20381 definitions, there will be one or more class scopes before we get to
20382 the template parameter scope. */
20384 if (cp_binding_level *its = parser->implicit_template_scope)
20385 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
20387 while (maybe_its->kind == sk_class)
20388 maybe_its = maybe_its->level_chain;
20389 if (maybe_its == its)
20391 parser->implicit_template_parms = 0;
20392 parser->implicit_template_scope = 0;
20396 return parameters;
20399 /* Parse a parameter declaration.
20401 parameter-declaration:
20402 decl-specifier-seq ... [opt] declarator
20403 decl-specifier-seq declarator = assignment-expression
20404 decl-specifier-seq ... [opt] abstract-declarator [opt]
20405 decl-specifier-seq abstract-declarator [opt] = assignment-expression
20407 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
20408 declares a template parameter. (In that case, a non-nested `>'
20409 token encountered during the parsing of the assignment-expression
20410 is not interpreted as a greater-than operator.)
20412 Returns a representation of the parameter, or NULL if an error
20413 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
20414 true iff the declarator is of the form "(p)". */
20416 static cp_parameter_declarator *
20417 cp_parser_parameter_declaration (cp_parser *parser,
20418 bool template_parm_p,
20419 bool *parenthesized_p)
20421 int declares_class_or_enum;
20422 cp_decl_specifier_seq decl_specifiers;
20423 cp_declarator *declarator;
20424 tree default_argument;
20425 cp_token *token = NULL, *declarator_token_start = NULL;
20426 const char *saved_message;
20427 bool template_parameter_pack_p = false;
20429 /* In a template parameter, `>' is not an operator.
20431 [temp.param]
20433 When parsing a default template-argument for a non-type
20434 template-parameter, the first non-nested `>' is taken as the end
20435 of the template parameter-list rather than a greater-than
20436 operator. */
20438 /* Type definitions may not appear in parameter types. */
20439 saved_message = parser->type_definition_forbidden_message;
20440 parser->type_definition_forbidden_message
20441 = G_("types may not be defined in parameter types");
20443 /* Parse the declaration-specifiers. */
20444 cp_parser_decl_specifier_seq (parser,
20445 CP_PARSER_FLAGS_NONE,
20446 &decl_specifiers,
20447 &declares_class_or_enum);
20449 /* Complain about missing 'typename' or other invalid type names. */
20450 if (!decl_specifiers.any_type_specifiers_p
20451 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20452 decl_specifiers.type = error_mark_node;
20454 /* If an error occurred, there's no reason to attempt to parse the
20455 rest of the declaration. */
20456 if (cp_parser_error_occurred (parser))
20458 parser->type_definition_forbidden_message = saved_message;
20459 return NULL;
20462 /* Peek at the next token. */
20463 token = cp_lexer_peek_token (parser->lexer);
20465 /* If the next token is a `)', `,', `=', `>', or `...', then there
20466 is no declarator. However, when variadic templates are enabled,
20467 there may be a declarator following `...'. */
20468 if (token->type == CPP_CLOSE_PAREN
20469 || token->type == CPP_COMMA
20470 || token->type == CPP_EQ
20471 || token->type == CPP_GREATER)
20473 declarator = NULL;
20474 if (parenthesized_p)
20475 *parenthesized_p = false;
20477 /* Otherwise, there should be a declarator. */
20478 else
20480 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
20481 parser->default_arg_ok_p = false;
20483 /* After seeing a decl-specifier-seq, if the next token is not a
20484 "(", there is no possibility that the code is a valid
20485 expression. Therefore, if parsing tentatively, we commit at
20486 this point. */
20487 if (!parser->in_template_argument_list_p
20488 /* In an expression context, having seen:
20490 (int((char ...
20492 we cannot be sure whether we are looking at a
20493 function-type (taking a "char" as a parameter) or a cast
20494 of some object of type "char" to "int". */
20495 && !parser->in_type_id_in_expr_p
20496 && cp_parser_uncommitted_to_tentative_parse_p (parser)
20497 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20498 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
20499 cp_parser_commit_to_tentative_parse (parser);
20500 /* Parse the declarator. */
20501 declarator_token_start = token;
20502 declarator = cp_parser_declarator (parser,
20503 CP_PARSER_DECLARATOR_EITHER,
20504 /*ctor_dtor_or_conv_p=*/NULL,
20505 parenthesized_p,
20506 /*member_p=*/false,
20507 /*friend_p=*/false);
20508 parser->default_arg_ok_p = saved_default_arg_ok_p;
20509 /* After the declarator, allow more attributes. */
20510 decl_specifiers.attributes
20511 = chainon (decl_specifiers.attributes,
20512 cp_parser_attributes_opt (parser));
20514 /* If the declarator is a template parameter pack, remember that and
20515 clear the flag in the declarator itself so we don't get errors
20516 from grokdeclarator. */
20517 if (template_parm_p && declarator && declarator->parameter_pack_p)
20519 declarator->parameter_pack_p = false;
20520 template_parameter_pack_p = true;
20524 /* If the next token is an ellipsis, and we have not seen a declarator
20525 name, and if either the type of the declarator contains parameter
20526 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
20527 for, eg, abbreviated integral type names), then we actually have a
20528 parameter pack expansion expression. Otherwise, leave the ellipsis
20529 for a C-style variadic function. */
20530 token = cp_lexer_peek_token (parser->lexer);
20531 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20533 tree type = decl_specifiers.type;
20535 if (type && DECL_P (type))
20536 type = TREE_TYPE (type);
20538 if (((type
20539 && TREE_CODE (type) != TYPE_PACK_EXPANSION
20540 && (template_parm_p || uses_parameter_packs (type)))
20541 || (!type && template_parm_p))
20542 && declarator_can_be_parameter_pack (declarator))
20544 /* Consume the `...'. */
20545 cp_lexer_consume_token (parser->lexer);
20546 maybe_warn_variadic_templates ();
20548 /* Build a pack expansion type */
20549 if (template_parm_p)
20550 template_parameter_pack_p = true;
20551 else if (declarator)
20552 declarator->parameter_pack_p = true;
20553 else
20554 decl_specifiers.type = make_pack_expansion (type);
20558 /* The restriction on defining new types applies only to the type
20559 of the parameter, not to the default argument. */
20560 parser->type_definition_forbidden_message = saved_message;
20562 /* If the next token is `=', then process a default argument. */
20563 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
20565 tree type = decl_specifiers.type;
20566 token = cp_lexer_peek_token (parser->lexer);
20567 /* If we are defining a class, then the tokens that make up the
20568 default argument must be saved and processed later. */
20569 if (!template_parm_p && at_class_scope_p ()
20570 && TYPE_BEING_DEFINED (current_class_type)
20571 && !LAMBDA_TYPE_P (current_class_type))
20572 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
20574 // A constrained-type-specifier may declare a type template-parameter.
20575 else if (declares_constrained_type_template_parameter (type))
20576 default_argument
20577 = cp_parser_default_type_template_argument (parser);
20579 // A constrained-type-specifier may declare a template-template-parameter.
20580 else if (declares_constrained_template_template_parameter (type))
20581 default_argument
20582 = cp_parser_default_template_template_argument (parser);
20584 /* Outside of a class definition, we can just parse the
20585 assignment-expression. */
20586 else
20587 default_argument
20588 = cp_parser_default_argument (parser, template_parm_p);
20590 if (!parser->default_arg_ok_p)
20592 permerror (token->location,
20593 "default arguments are only "
20594 "permitted for function parameters");
20596 else if ((declarator && declarator->parameter_pack_p)
20597 || template_parameter_pack_p
20598 || (decl_specifiers.type
20599 && PACK_EXPANSION_P (decl_specifiers.type)))
20601 /* Find the name of the parameter pack. */
20602 cp_declarator *id_declarator = declarator;
20603 while (id_declarator && id_declarator->kind != cdk_id)
20604 id_declarator = id_declarator->declarator;
20606 if (id_declarator && id_declarator->kind == cdk_id)
20607 error_at (declarator_token_start->location,
20608 template_parm_p
20609 ? G_("template parameter pack %qD "
20610 "cannot have a default argument")
20611 : G_("parameter pack %qD cannot have "
20612 "a default argument"),
20613 id_declarator->u.id.unqualified_name);
20614 else
20615 error_at (declarator_token_start->location,
20616 template_parm_p
20617 ? G_("template parameter pack cannot have "
20618 "a default argument")
20619 : G_("parameter pack cannot have a "
20620 "default argument"));
20622 default_argument = NULL_TREE;
20625 else
20626 default_argument = NULL_TREE;
20628 return make_parameter_declarator (&decl_specifiers,
20629 declarator,
20630 default_argument,
20631 template_parameter_pack_p);
20634 /* Parse a default argument and return it.
20636 TEMPLATE_PARM_P is true if this is a default argument for a
20637 non-type template parameter. */
20638 static tree
20639 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
20641 tree default_argument = NULL_TREE;
20642 bool saved_greater_than_is_operator_p;
20643 bool saved_local_variables_forbidden_p;
20644 bool non_constant_p, is_direct_init;
20646 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
20647 set correctly. */
20648 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
20649 parser->greater_than_is_operator_p = !template_parm_p;
20650 /* Local variable names (and the `this' keyword) may not
20651 appear in a default argument. */
20652 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20653 parser->local_variables_forbidden_p = true;
20654 /* Parse the assignment-expression. */
20655 if (template_parm_p)
20656 push_deferring_access_checks (dk_no_deferred);
20657 tree saved_class_ptr = NULL_TREE;
20658 tree saved_class_ref = NULL_TREE;
20659 /* The "this" pointer is not valid in a default argument. */
20660 if (cfun)
20662 saved_class_ptr = current_class_ptr;
20663 cp_function_chain->x_current_class_ptr = NULL_TREE;
20664 saved_class_ref = current_class_ref;
20665 cp_function_chain->x_current_class_ref = NULL_TREE;
20667 default_argument
20668 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
20669 /* Restore the "this" pointer. */
20670 if (cfun)
20672 cp_function_chain->x_current_class_ptr = saved_class_ptr;
20673 cp_function_chain->x_current_class_ref = saved_class_ref;
20675 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
20676 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20677 if (template_parm_p)
20678 pop_deferring_access_checks ();
20679 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
20680 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20682 return default_argument;
20685 /* Parse a function-body.
20687 function-body:
20688 compound_statement */
20690 static void
20691 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
20693 cp_parser_compound_statement (parser, NULL, (in_function_try_block
20694 ? BCS_TRY_BLOCK : BCS_NORMAL),
20695 true);
20698 /* Parse a ctor-initializer-opt followed by a function-body. Return
20699 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
20700 is true we are parsing a function-try-block. */
20702 static bool
20703 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
20704 bool in_function_try_block)
20706 tree body, list;
20707 bool ctor_initializer_p;
20708 const bool check_body_p =
20709 DECL_CONSTRUCTOR_P (current_function_decl)
20710 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
20711 tree last = NULL;
20713 /* Begin the function body. */
20714 body = begin_function_body ();
20715 /* Parse the optional ctor-initializer. */
20716 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
20718 /* If we're parsing a constexpr constructor definition, we need
20719 to check that the constructor body is indeed empty. However,
20720 before we get to cp_parser_function_body lot of junk has been
20721 generated, so we can't just check that we have an empty block.
20722 Rather we take a snapshot of the outermost block, and check whether
20723 cp_parser_function_body changed its state. */
20724 if (check_body_p)
20726 list = cur_stmt_list;
20727 if (STATEMENT_LIST_TAIL (list))
20728 last = STATEMENT_LIST_TAIL (list)->stmt;
20730 /* Parse the function-body. */
20731 cp_parser_function_body (parser, in_function_try_block);
20732 if (check_body_p)
20733 check_constexpr_ctor_body (last, list, /*complain=*/true);
20734 /* Finish the function body. */
20735 finish_function_body (body);
20737 return ctor_initializer_p;
20740 /* Parse an initializer.
20742 initializer:
20743 = initializer-clause
20744 ( expression-list )
20746 Returns an expression representing the initializer. If no
20747 initializer is present, NULL_TREE is returned.
20749 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
20750 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
20751 set to TRUE if there is no initializer present. If there is an
20752 initializer, and it is not a constant-expression, *NON_CONSTANT_P
20753 is set to true; otherwise it is set to false. */
20755 static tree
20756 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
20757 bool* non_constant_p)
20759 cp_token *token;
20760 tree init;
20762 /* Peek at the next token. */
20763 token = cp_lexer_peek_token (parser->lexer);
20765 /* Let our caller know whether or not this initializer was
20766 parenthesized. */
20767 *is_direct_init = (token->type != CPP_EQ);
20768 /* Assume that the initializer is constant. */
20769 *non_constant_p = false;
20771 if (token->type == CPP_EQ)
20773 /* Consume the `='. */
20774 cp_lexer_consume_token (parser->lexer);
20775 /* Parse the initializer-clause. */
20776 init = cp_parser_initializer_clause (parser, non_constant_p);
20778 else if (token->type == CPP_OPEN_PAREN)
20780 vec<tree, va_gc> *vec;
20781 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20782 /*cast_p=*/false,
20783 /*allow_expansion_p=*/true,
20784 non_constant_p);
20785 if (vec == NULL)
20786 return error_mark_node;
20787 init = build_tree_list_vec (vec);
20788 release_tree_vector (vec);
20790 else if (token->type == CPP_OPEN_BRACE)
20792 cp_lexer_set_source_position (parser->lexer);
20793 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20794 init = cp_parser_braced_list (parser, non_constant_p);
20795 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
20797 else
20799 /* Anything else is an error. */
20800 cp_parser_error (parser, "expected initializer");
20801 init = error_mark_node;
20804 return init;
20807 /* Parse an initializer-clause.
20809 initializer-clause:
20810 assignment-expression
20811 braced-init-list
20813 Returns an expression representing the initializer.
20815 If the `assignment-expression' production is used the value
20816 returned is simply a representation for the expression.
20818 Otherwise, calls cp_parser_braced_list. */
20820 static cp_expr
20821 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
20823 cp_expr initializer;
20825 /* Assume the expression is constant. */
20826 *non_constant_p = false;
20828 /* If it is not a `{', then we are looking at an
20829 assignment-expression. */
20830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
20832 initializer
20833 = cp_parser_constant_expression (parser,
20834 /*allow_non_constant_p=*/true,
20835 non_constant_p);
20837 else
20838 initializer = cp_parser_braced_list (parser, non_constant_p);
20840 return initializer;
20843 /* Parse a brace-enclosed initializer list.
20845 braced-init-list:
20846 { initializer-list , [opt] }
20849 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
20850 the elements of the initializer-list (or NULL, if the last
20851 production is used). The TREE_TYPE for the CONSTRUCTOR will be
20852 NULL_TREE. There is no way to detect whether or not the optional
20853 trailing `,' was provided. NON_CONSTANT_P is as for
20854 cp_parser_initializer. */
20856 static cp_expr
20857 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
20859 tree initializer;
20860 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
20862 /* Consume the `{' token. */
20863 cp_lexer_consume_token (parser->lexer);
20864 /* Create a CONSTRUCTOR to represent the braced-initializer. */
20865 initializer = make_node (CONSTRUCTOR);
20866 /* If it's not a `}', then there is a non-trivial initializer. */
20867 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
20869 /* Parse the initializer list. */
20870 CONSTRUCTOR_ELTS (initializer)
20871 = cp_parser_initializer_list (parser, non_constant_p);
20872 /* A trailing `,' token is allowed. */
20873 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20874 cp_lexer_consume_token (parser->lexer);
20876 else
20877 *non_constant_p = false;
20878 /* Now, there should be a trailing `}'. */
20879 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
20880 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
20881 TREE_TYPE (initializer) = init_list_type_node;
20883 cp_expr result (initializer);
20884 /* Build a location of the form:
20885 { ... }
20886 ^~~~~~~
20887 with caret==start at the open brace, finish at the close brace. */
20888 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
20889 result.set_location (combined_loc);
20890 return result;
20893 /* Consume tokens up to, and including, the next non-nested closing `]'.
20894 Returns true iff we found a closing `]'. */
20896 static bool
20897 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
20899 unsigned square_depth = 0;
20901 while (true)
20903 cp_token * token = cp_lexer_peek_token (parser->lexer);
20905 switch (token->type)
20907 case CPP_EOF:
20908 case CPP_PRAGMA_EOL:
20909 /* If we've run out of tokens, then there is no closing `]'. */
20910 return false;
20912 case CPP_OPEN_SQUARE:
20913 ++square_depth;
20914 break;
20916 case CPP_CLOSE_SQUARE:
20917 if (!square_depth--)
20919 cp_lexer_consume_token (parser->lexer);
20920 return true;
20922 break;
20924 default:
20925 break;
20928 /* Consume the token. */
20929 cp_lexer_consume_token (parser->lexer);
20933 /* Return true if we are looking at an array-designator, false otherwise. */
20935 static bool
20936 cp_parser_array_designator_p (cp_parser *parser)
20938 /* Consume the `['. */
20939 cp_lexer_consume_token (parser->lexer);
20941 cp_lexer_save_tokens (parser->lexer);
20943 /* Skip tokens until the next token is a closing square bracket.
20944 If we find the closing `]', and the next token is a `=', then
20945 we are looking at an array designator. */
20946 bool array_designator_p
20947 = (cp_parser_skip_to_closing_square_bracket (parser)
20948 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
20950 /* Roll back the tokens we skipped. */
20951 cp_lexer_rollback_tokens (parser->lexer);
20953 return array_designator_p;
20956 /* Parse an initializer-list.
20958 initializer-list:
20959 initializer-clause ... [opt]
20960 initializer-list , initializer-clause ... [opt]
20962 GNU Extension:
20964 initializer-list:
20965 designation initializer-clause ...[opt]
20966 initializer-list , designation initializer-clause ...[opt]
20968 designation:
20969 . identifier =
20970 identifier :
20971 [ constant-expression ] =
20973 Returns a vec of constructor_elt. The VALUE of each elt is an expression
20974 for the initializer. If the INDEX of the elt is non-NULL, it is the
20975 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
20976 as for cp_parser_initializer. */
20978 static vec<constructor_elt, va_gc> *
20979 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
20981 vec<constructor_elt, va_gc> *v = NULL;
20983 /* Assume all of the expressions are constant. */
20984 *non_constant_p = false;
20986 /* Parse the rest of the list. */
20987 while (true)
20989 cp_token *token;
20990 tree designator;
20991 tree initializer;
20992 bool clause_non_constant_p;
20994 /* If the next token is an identifier and the following one is a
20995 colon, we are looking at the GNU designated-initializer
20996 syntax. */
20997 if (cp_parser_allow_gnu_extensions_p (parser)
20998 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
20999 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21001 /* Warn the user that they are using an extension. */
21002 pedwarn (input_location, OPT_Wpedantic,
21003 "ISO C++ does not allow designated initializers");
21004 /* Consume the identifier. */
21005 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21006 /* Consume the `:'. */
21007 cp_lexer_consume_token (parser->lexer);
21009 /* Also handle the C99 syntax, '. id ='. */
21010 else if (cp_parser_allow_gnu_extensions_p (parser)
21011 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21012 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21013 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21015 /* Warn the user that they are using an extension. */
21016 pedwarn (input_location, OPT_Wpedantic,
21017 "ISO C++ does not allow C99 designated initializers");
21018 /* Consume the `.'. */
21019 cp_lexer_consume_token (parser->lexer);
21020 /* Consume the identifier. */
21021 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21022 /* Consume the `='. */
21023 cp_lexer_consume_token (parser->lexer);
21025 /* Also handle C99 array designators, '[ const ] ='. */
21026 else if (cp_parser_allow_gnu_extensions_p (parser)
21027 && !c_dialect_objc ()
21028 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21030 /* In C++11, [ could start a lambda-introducer. */
21031 bool non_const = false;
21033 cp_parser_parse_tentatively (parser);
21035 if (!cp_parser_array_designator_p (parser))
21037 cp_parser_simulate_error (parser);
21038 designator = NULL_TREE;
21040 else
21042 designator = cp_parser_constant_expression (parser, true,
21043 &non_const);
21044 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21045 cp_parser_require (parser, CPP_EQ, RT_EQ);
21048 if (!cp_parser_parse_definitely (parser))
21049 designator = NULL_TREE;
21050 else if (non_const)
21051 require_potential_rvalue_constant_expression (designator);
21053 else
21054 designator = NULL_TREE;
21056 /* Parse the initializer. */
21057 initializer = cp_parser_initializer_clause (parser,
21058 &clause_non_constant_p);
21059 /* If any clause is non-constant, so is the entire initializer. */
21060 if (clause_non_constant_p)
21061 *non_constant_p = true;
21063 /* If we have an ellipsis, this is an initializer pack
21064 expansion. */
21065 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21067 /* Consume the `...'. */
21068 cp_lexer_consume_token (parser->lexer);
21070 /* Turn the initializer into an initializer expansion. */
21071 initializer = make_pack_expansion (initializer);
21074 /* Add it to the vector. */
21075 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21077 /* If the next token is not a comma, we have reached the end of
21078 the list. */
21079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21080 break;
21082 /* Peek at the next token. */
21083 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21084 /* If the next token is a `}', then we're still done. An
21085 initializer-clause can have a trailing `,' after the
21086 initializer-list and before the closing `}'. */
21087 if (token->type == CPP_CLOSE_BRACE)
21088 break;
21090 /* Consume the `,' token. */
21091 cp_lexer_consume_token (parser->lexer);
21094 return v;
21097 /* Classes [gram.class] */
21099 /* Parse a class-name.
21101 class-name:
21102 identifier
21103 template-id
21105 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21106 to indicate that names looked up in dependent types should be
21107 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21108 keyword has been used to indicate that the name that appears next
21109 is a template. TAG_TYPE indicates the explicit tag given before
21110 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21111 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21112 is the class being defined in a class-head. If ENUM_OK is TRUE,
21113 enum-names are also accepted.
21115 Returns the TYPE_DECL representing the class. */
21117 static tree
21118 cp_parser_class_name (cp_parser *parser,
21119 bool typename_keyword_p,
21120 bool template_keyword_p,
21121 enum tag_types tag_type,
21122 bool check_dependency_p,
21123 bool class_head_p,
21124 bool is_declaration,
21125 bool enum_ok)
21127 tree decl;
21128 tree scope;
21129 bool typename_p;
21130 cp_token *token;
21131 tree identifier = NULL_TREE;
21133 /* All class-names start with an identifier. */
21134 token = cp_lexer_peek_token (parser->lexer);
21135 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21137 cp_parser_error (parser, "expected class-name");
21138 return error_mark_node;
21141 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21142 to a template-id, so we save it here. */
21143 scope = parser->scope;
21144 if (scope == error_mark_node)
21145 return error_mark_node;
21147 /* Any name names a type if we're following the `typename' keyword
21148 in a qualified name where the enclosing scope is type-dependent. */
21149 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21150 && dependent_type_p (scope));
21151 /* Handle the common case (an identifier, but not a template-id)
21152 efficiently. */
21153 if (token->type == CPP_NAME
21154 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21156 cp_token *identifier_token;
21157 bool ambiguous_p;
21159 /* Look for the identifier. */
21160 identifier_token = cp_lexer_peek_token (parser->lexer);
21161 ambiguous_p = identifier_token->error_reported;
21162 identifier = cp_parser_identifier (parser);
21163 /* If the next token isn't an identifier, we are certainly not
21164 looking at a class-name. */
21165 if (identifier == error_mark_node)
21166 decl = error_mark_node;
21167 /* If we know this is a type-name, there's no need to look it
21168 up. */
21169 else if (typename_p)
21170 decl = identifier;
21171 else
21173 tree ambiguous_decls;
21174 /* If we already know that this lookup is ambiguous, then
21175 we've already issued an error message; there's no reason
21176 to check again. */
21177 if (ambiguous_p)
21179 cp_parser_simulate_error (parser);
21180 return error_mark_node;
21182 /* If the next token is a `::', then the name must be a type
21183 name.
21185 [basic.lookup.qual]
21187 During the lookup for a name preceding the :: scope
21188 resolution operator, object, function, and enumerator
21189 names are ignored. */
21190 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21191 tag_type = typename_type;
21192 /* Look up the name. */
21193 decl = cp_parser_lookup_name (parser, identifier,
21194 tag_type,
21195 /*is_template=*/false,
21196 /*is_namespace=*/false,
21197 check_dependency_p,
21198 &ambiguous_decls,
21199 identifier_token->location);
21200 if (ambiguous_decls)
21202 if (cp_parser_parsing_tentatively (parser))
21203 cp_parser_simulate_error (parser);
21204 return error_mark_node;
21208 else
21210 /* Try a template-id. */
21211 decl = cp_parser_template_id (parser, template_keyword_p,
21212 check_dependency_p,
21213 tag_type,
21214 is_declaration);
21215 if (decl == error_mark_node)
21216 return error_mark_node;
21219 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21221 /* If this is a typename, create a TYPENAME_TYPE. */
21222 if (typename_p && decl != error_mark_node)
21224 decl = make_typename_type (scope, decl, typename_type,
21225 /*complain=*/tf_error);
21226 if (decl != error_mark_node)
21227 decl = TYPE_NAME (decl);
21230 decl = strip_using_decl (decl);
21232 /* Check to see that it is really the name of a class. */
21233 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21234 && identifier_p (TREE_OPERAND (decl, 0))
21235 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21236 /* Situations like this:
21238 template <typename T> struct A {
21239 typename T::template X<int>::I i;
21242 are problematic. Is `T::template X<int>' a class-name? The
21243 standard does not seem to be definitive, but there is no other
21244 valid interpretation of the following `::'. Therefore, those
21245 names are considered class-names. */
21247 decl = make_typename_type (scope, decl, tag_type, tf_error);
21248 if (decl != error_mark_node)
21249 decl = TYPE_NAME (decl);
21251 else if (TREE_CODE (decl) != TYPE_DECL
21252 || TREE_TYPE (decl) == error_mark_node
21253 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
21254 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
21255 /* In Objective-C 2.0, a classname followed by '.' starts a
21256 dot-syntax expression, and it's not a type-name. */
21257 || (c_dialect_objc ()
21258 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
21259 && objc_is_class_name (decl)))
21260 decl = error_mark_node;
21262 if (decl == error_mark_node)
21263 cp_parser_error (parser, "expected class-name");
21264 else if (identifier && !parser->scope)
21265 maybe_note_name_used_in_class (identifier, decl);
21267 return decl;
21270 /* Parse a class-specifier.
21272 class-specifier:
21273 class-head { member-specification [opt] }
21275 Returns the TREE_TYPE representing the class. */
21277 static tree
21278 cp_parser_class_specifier_1 (cp_parser* parser)
21280 tree type;
21281 tree attributes = NULL_TREE;
21282 bool nested_name_specifier_p;
21283 unsigned saved_num_template_parameter_lists;
21284 bool saved_in_function_body;
21285 unsigned char in_statement;
21286 bool in_switch_statement_p;
21287 bool saved_in_unbraced_linkage_specification_p;
21288 tree old_scope = NULL_TREE;
21289 tree scope = NULL_TREE;
21290 cp_token *closing_brace;
21292 push_deferring_access_checks (dk_no_deferred);
21294 /* Parse the class-head. */
21295 type = cp_parser_class_head (parser,
21296 &nested_name_specifier_p);
21297 /* If the class-head was a semantic disaster, skip the entire body
21298 of the class. */
21299 if (!type)
21301 cp_parser_skip_to_end_of_block_or_statement (parser);
21302 pop_deferring_access_checks ();
21303 return error_mark_node;
21306 /* Look for the `{'. */
21307 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
21309 pop_deferring_access_checks ();
21310 return error_mark_node;
21313 cp_ensure_no_omp_declare_simd (parser);
21314 cp_ensure_no_oacc_routine (parser);
21316 /* Issue an error message if type-definitions are forbidden here. */
21317 cp_parser_check_type_definition (parser);
21318 /* Remember that we are defining one more class. */
21319 ++parser->num_classes_being_defined;
21320 /* Inside the class, surrounding template-parameter-lists do not
21321 apply. */
21322 saved_num_template_parameter_lists
21323 = parser->num_template_parameter_lists;
21324 parser->num_template_parameter_lists = 0;
21325 /* We are not in a function body. */
21326 saved_in_function_body = parser->in_function_body;
21327 parser->in_function_body = false;
21328 /* Or in a loop. */
21329 in_statement = parser->in_statement;
21330 parser->in_statement = 0;
21331 /* Or in a switch. */
21332 in_switch_statement_p = parser->in_switch_statement_p;
21333 parser->in_switch_statement_p = false;
21334 /* We are not immediately inside an extern "lang" block. */
21335 saved_in_unbraced_linkage_specification_p
21336 = parser->in_unbraced_linkage_specification_p;
21337 parser->in_unbraced_linkage_specification_p = false;
21339 // Associate constraints with the type.
21340 if (flag_concepts)
21341 type = associate_classtype_constraints (type);
21343 /* Start the class. */
21344 if (nested_name_specifier_p)
21346 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
21347 old_scope = push_inner_scope (scope);
21349 type = begin_class_definition (type);
21351 if (type == error_mark_node)
21352 /* If the type is erroneous, skip the entire body of the class. */
21353 cp_parser_skip_to_closing_brace (parser);
21354 else
21355 /* Parse the member-specification. */
21356 cp_parser_member_specification_opt (parser);
21358 /* Look for the trailing `}'. */
21359 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21360 /* Look for trailing attributes to apply to this class. */
21361 if (cp_parser_allow_gnu_extensions_p (parser))
21362 attributes = cp_parser_gnu_attributes_opt (parser);
21363 if (type != error_mark_node)
21364 type = finish_struct (type, attributes);
21365 if (nested_name_specifier_p)
21366 pop_inner_scope (old_scope, scope);
21368 /* We've finished a type definition. Check for the common syntax
21369 error of forgetting a semicolon after the definition. We need to
21370 be careful, as we can't just check for not-a-semicolon and be done
21371 with it; the user might have typed:
21373 class X { } c = ...;
21374 class X { } *p = ...;
21376 and so forth. Instead, enumerate all the possible tokens that
21377 might follow this production; if we don't see one of them, then
21378 complain and silently insert the semicolon. */
21380 cp_token *token = cp_lexer_peek_token (parser->lexer);
21381 bool want_semicolon = true;
21383 if (cp_next_tokens_can_be_std_attribute_p (parser))
21384 /* Don't try to parse c++11 attributes here. As per the
21385 grammar, that should be a task for
21386 cp_parser_decl_specifier_seq. */
21387 want_semicolon = false;
21389 switch (token->type)
21391 case CPP_NAME:
21392 case CPP_SEMICOLON:
21393 case CPP_MULT:
21394 case CPP_AND:
21395 case CPP_OPEN_PAREN:
21396 case CPP_CLOSE_PAREN:
21397 case CPP_COMMA:
21398 want_semicolon = false;
21399 break;
21401 /* While it's legal for type qualifiers and storage class
21402 specifiers to follow type definitions in the grammar, only
21403 compiler testsuites contain code like that. Assume that if
21404 we see such code, then what we're really seeing is a case
21405 like:
21407 class X { }
21408 const <type> var = ...;
21412 class Y { }
21413 static <type> func (...) ...
21415 i.e. the qualifier or specifier applies to the next
21416 declaration. To do so, however, we need to look ahead one
21417 more token to see if *that* token is a type specifier.
21419 This code could be improved to handle:
21421 class Z { }
21422 static const <type> var = ...; */
21423 case CPP_KEYWORD:
21424 if (keyword_is_decl_specifier (token->keyword))
21426 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
21428 /* Handling user-defined types here would be nice, but very
21429 tricky. */
21430 want_semicolon
21431 = (lookahead->type == CPP_KEYWORD
21432 && keyword_begins_type_specifier (lookahead->keyword));
21434 break;
21435 default:
21436 break;
21439 /* If we don't have a type, then something is very wrong and we
21440 shouldn't try to do anything clever. Likewise for not seeing the
21441 closing brace. */
21442 if (closing_brace && TYPE_P (type) && want_semicolon)
21444 cp_token_position prev
21445 = cp_lexer_previous_token_position (parser->lexer);
21446 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
21447 location_t loc = prev_token->location;
21449 if (CLASSTYPE_DECLARED_CLASS (type))
21450 error_at (loc, "expected %<;%> after class definition");
21451 else if (TREE_CODE (type) == RECORD_TYPE)
21452 error_at (loc, "expected %<;%> after struct definition");
21453 else if (TREE_CODE (type) == UNION_TYPE)
21454 error_at (loc, "expected %<;%> after union definition");
21455 else
21456 gcc_unreachable ();
21458 /* Unget one token and smash it to look as though we encountered
21459 a semicolon in the input stream. */
21460 cp_lexer_set_token_position (parser->lexer, prev);
21461 token = cp_lexer_peek_token (parser->lexer);
21462 token->type = CPP_SEMICOLON;
21463 token->keyword = RID_MAX;
21467 /* If this class is not itself within the scope of another class,
21468 then we need to parse the bodies of all of the queued function
21469 definitions. Note that the queued functions defined in a class
21470 are not always processed immediately following the
21471 class-specifier for that class. Consider:
21473 struct A {
21474 struct B { void f() { sizeof (A); } };
21477 If `f' were processed before the processing of `A' were
21478 completed, there would be no way to compute the size of `A'.
21479 Note that the nesting we are interested in here is lexical --
21480 not the semantic nesting given by TYPE_CONTEXT. In particular,
21481 for:
21483 struct A { struct B; };
21484 struct A::B { void f() { } };
21486 there is no need to delay the parsing of `A::B::f'. */
21487 if (--parser->num_classes_being_defined == 0)
21489 tree decl;
21490 tree class_type = NULL_TREE;
21491 tree pushed_scope = NULL_TREE;
21492 unsigned ix;
21493 cp_default_arg_entry *e;
21494 tree save_ccp, save_ccr;
21496 /* In a first pass, parse default arguments to the functions.
21497 Then, in a second pass, parse the bodies of the functions.
21498 This two-phased approach handles cases like:
21500 struct S {
21501 void f() { g(); }
21502 void g(int i = 3);
21506 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
21508 decl = e->decl;
21509 /* If there are default arguments that have not yet been processed,
21510 take care of them now. */
21511 if (class_type != e->class_type)
21513 if (pushed_scope)
21514 pop_scope (pushed_scope);
21515 class_type = e->class_type;
21516 pushed_scope = push_scope (class_type);
21518 /* Make sure that any template parameters are in scope. */
21519 maybe_begin_member_template_processing (decl);
21520 /* Parse the default argument expressions. */
21521 cp_parser_late_parsing_default_args (parser, decl);
21522 /* Remove any template parameters from the symbol table. */
21523 maybe_end_member_template_processing ();
21525 vec_safe_truncate (unparsed_funs_with_default_args, 0);
21526 /* Now parse any NSDMIs. */
21527 save_ccp = current_class_ptr;
21528 save_ccr = current_class_ref;
21529 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
21531 if (class_type != DECL_CONTEXT (decl))
21533 if (pushed_scope)
21534 pop_scope (pushed_scope);
21535 class_type = DECL_CONTEXT (decl);
21536 pushed_scope = push_scope (class_type);
21538 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
21539 cp_parser_late_parsing_nsdmi (parser, decl);
21541 vec_safe_truncate (unparsed_nsdmis, 0);
21542 current_class_ptr = save_ccp;
21543 current_class_ref = save_ccr;
21544 if (pushed_scope)
21545 pop_scope (pushed_scope);
21547 /* Now do some post-NSDMI bookkeeping. */
21548 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
21549 after_nsdmi_defaulted_late_checks (class_type);
21550 vec_safe_truncate (unparsed_classes, 0);
21551 after_nsdmi_defaulted_late_checks (type);
21553 /* Now parse the body of the functions. */
21554 if (flag_openmp)
21556 /* OpenMP UDRs need to be parsed before all other functions. */
21557 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21558 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
21559 cp_parser_late_parsing_for_member (parser, decl);
21560 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21561 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
21562 cp_parser_late_parsing_for_member (parser, decl);
21564 else
21565 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
21566 cp_parser_late_parsing_for_member (parser, decl);
21567 vec_safe_truncate (unparsed_funs_with_definitions, 0);
21569 else
21570 vec_safe_push (unparsed_classes, type);
21572 /* Put back any saved access checks. */
21573 pop_deferring_access_checks ();
21575 /* Restore saved state. */
21576 parser->in_switch_statement_p = in_switch_statement_p;
21577 parser->in_statement = in_statement;
21578 parser->in_function_body = saved_in_function_body;
21579 parser->num_template_parameter_lists
21580 = saved_num_template_parameter_lists;
21581 parser->in_unbraced_linkage_specification_p
21582 = saved_in_unbraced_linkage_specification_p;
21584 return type;
21587 static tree
21588 cp_parser_class_specifier (cp_parser* parser)
21590 tree ret;
21591 timevar_push (TV_PARSE_STRUCT);
21592 ret = cp_parser_class_specifier_1 (parser);
21593 timevar_pop (TV_PARSE_STRUCT);
21594 return ret;
21597 /* Parse a class-head.
21599 class-head:
21600 class-key identifier [opt] base-clause [opt]
21601 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
21602 class-key nested-name-specifier [opt] template-id
21603 base-clause [opt]
21605 class-virt-specifier:
21606 final
21608 GNU Extensions:
21609 class-key attributes identifier [opt] base-clause [opt]
21610 class-key attributes nested-name-specifier identifier base-clause [opt]
21611 class-key attributes nested-name-specifier [opt] template-id
21612 base-clause [opt]
21614 Upon return BASES is initialized to the list of base classes (or
21615 NULL, if there are none) in the same form returned by
21616 cp_parser_base_clause.
21618 Returns the TYPE of the indicated class. Sets
21619 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
21620 involving a nested-name-specifier was used, and FALSE otherwise.
21622 Returns error_mark_node if this is not a class-head.
21624 Returns NULL_TREE if the class-head is syntactically valid, but
21625 semantically invalid in a way that means we should skip the entire
21626 body of the class. */
21628 static tree
21629 cp_parser_class_head (cp_parser* parser,
21630 bool* nested_name_specifier_p)
21632 tree nested_name_specifier;
21633 enum tag_types class_key;
21634 tree id = NULL_TREE;
21635 tree type = NULL_TREE;
21636 tree attributes;
21637 tree bases;
21638 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
21639 bool template_id_p = false;
21640 bool qualified_p = false;
21641 bool invalid_nested_name_p = false;
21642 bool invalid_explicit_specialization_p = false;
21643 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
21644 tree pushed_scope = NULL_TREE;
21645 unsigned num_templates;
21646 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
21647 /* Assume no nested-name-specifier will be present. */
21648 *nested_name_specifier_p = false;
21649 /* Assume no template parameter lists will be used in defining the
21650 type. */
21651 num_templates = 0;
21652 parser->colon_corrects_to_scope_p = false;
21654 /* Look for the class-key. */
21655 class_key = cp_parser_class_key (parser);
21656 if (class_key == none_type)
21657 return error_mark_node;
21659 /* Parse the attributes. */
21660 attributes = cp_parser_attributes_opt (parser);
21662 /* If the next token is `::', that is invalid -- but sometimes
21663 people do try to write:
21665 struct ::S {};
21667 Handle this gracefully by accepting the extra qualifier, and then
21668 issuing an error about it later if this really is a
21669 class-head. If it turns out just to be an elaborated type
21670 specifier, remain silent. */
21671 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
21672 qualified_p = true;
21674 push_deferring_access_checks (dk_no_check);
21676 /* Determine the name of the class. Begin by looking for an
21677 optional nested-name-specifier. */
21678 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
21679 nested_name_specifier
21680 = cp_parser_nested_name_specifier_opt (parser,
21681 /*typename_keyword_p=*/false,
21682 /*check_dependency_p=*/false,
21683 /*type_p=*/true,
21684 /*is_declaration=*/false);
21685 /* If there was a nested-name-specifier, then there *must* be an
21686 identifier. */
21687 if (nested_name_specifier)
21689 type_start_token = cp_lexer_peek_token (parser->lexer);
21690 /* Although the grammar says `identifier', it really means
21691 `class-name' or `template-name'. You are only allowed to
21692 define a class that has already been declared with this
21693 syntax.
21695 The proposed resolution for Core Issue 180 says that wherever
21696 you see `class T::X' you should treat `X' as a type-name.
21698 It is OK to define an inaccessible class; for example:
21700 class A { class B; };
21701 class A::B {};
21703 We do not know if we will see a class-name, or a
21704 template-name. We look for a class-name first, in case the
21705 class-name is a template-id; if we looked for the
21706 template-name first we would stop after the template-name. */
21707 cp_parser_parse_tentatively (parser);
21708 type = cp_parser_class_name (parser,
21709 /*typename_keyword_p=*/false,
21710 /*template_keyword_p=*/false,
21711 class_type,
21712 /*check_dependency_p=*/false,
21713 /*class_head_p=*/true,
21714 /*is_declaration=*/false);
21715 /* If that didn't work, ignore the nested-name-specifier. */
21716 if (!cp_parser_parse_definitely (parser))
21718 invalid_nested_name_p = true;
21719 type_start_token = cp_lexer_peek_token (parser->lexer);
21720 id = cp_parser_identifier (parser);
21721 if (id == error_mark_node)
21722 id = NULL_TREE;
21724 /* If we could not find a corresponding TYPE, treat this
21725 declaration like an unqualified declaration. */
21726 if (type == error_mark_node)
21727 nested_name_specifier = NULL_TREE;
21728 /* Otherwise, count the number of templates used in TYPE and its
21729 containing scopes. */
21730 else
21732 tree scope;
21734 for (scope = TREE_TYPE (type);
21735 scope && TREE_CODE (scope) != NAMESPACE_DECL;
21736 scope = get_containing_scope (scope))
21737 if (TYPE_P (scope)
21738 && CLASS_TYPE_P (scope)
21739 && CLASSTYPE_TEMPLATE_INFO (scope)
21740 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
21741 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
21742 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
21743 ++num_templates;
21746 /* Otherwise, the identifier is optional. */
21747 else
21749 /* We don't know whether what comes next is a template-id,
21750 an identifier, or nothing at all. */
21751 cp_parser_parse_tentatively (parser);
21752 /* Check for a template-id. */
21753 type_start_token = cp_lexer_peek_token (parser->lexer);
21754 id = cp_parser_template_id (parser,
21755 /*template_keyword_p=*/false,
21756 /*check_dependency_p=*/true,
21757 class_key,
21758 /*is_declaration=*/true);
21759 /* If that didn't work, it could still be an identifier. */
21760 if (!cp_parser_parse_definitely (parser))
21762 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21764 type_start_token = cp_lexer_peek_token (parser->lexer);
21765 id = cp_parser_identifier (parser);
21767 else
21768 id = NULL_TREE;
21770 else
21772 template_id_p = true;
21773 ++num_templates;
21777 pop_deferring_access_checks ();
21779 if (id)
21781 cp_parser_check_for_invalid_template_id (parser, id,
21782 class_key,
21783 type_start_token->location);
21785 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
21787 /* If it's not a `:' or a `{' then we can't really be looking at a
21788 class-head, since a class-head only appears as part of a
21789 class-specifier. We have to detect this situation before calling
21790 xref_tag, since that has irreversible side-effects. */
21791 if (!cp_parser_next_token_starts_class_definition_p (parser))
21793 cp_parser_error (parser, "expected %<{%> or %<:%>");
21794 type = error_mark_node;
21795 goto out;
21798 /* At this point, we're going ahead with the class-specifier, even
21799 if some other problem occurs. */
21800 cp_parser_commit_to_tentative_parse (parser);
21801 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
21803 cp_parser_error (parser,
21804 "cannot specify %<override%> for a class");
21805 type = error_mark_node;
21806 goto out;
21808 /* Issue the error about the overly-qualified name now. */
21809 if (qualified_p)
21811 cp_parser_error (parser,
21812 "global qualification of class name is invalid");
21813 type = error_mark_node;
21814 goto out;
21816 else if (invalid_nested_name_p)
21818 cp_parser_error (parser,
21819 "qualified name does not name a class");
21820 type = error_mark_node;
21821 goto out;
21823 else if (nested_name_specifier)
21825 tree scope;
21827 /* Reject typedef-names in class heads. */
21828 if (!DECL_IMPLICIT_TYPEDEF_P (type))
21830 error_at (type_start_token->location,
21831 "invalid class name in declaration of %qD",
21832 type);
21833 type = NULL_TREE;
21834 goto done;
21837 /* Figure out in what scope the declaration is being placed. */
21838 scope = current_scope ();
21839 /* If that scope does not contain the scope in which the
21840 class was originally declared, the program is invalid. */
21841 if (scope && !is_ancestor (scope, nested_name_specifier))
21843 if (at_namespace_scope_p ())
21844 error_at (type_start_token->location,
21845 "declaration of %qD in namespace %qD which does not "
21846 "enclose %qD",
21847 type, scope, nested_name_specifier);
21848 else
21849 error_at (type_start_token->location,
21850 "declaration of %qD in %qD which does not enclose %qD",
21851 type, scope, nested_name_specifier);
21852 type = NULL_TREE;
21853 goto done;
21855 /* [dcl.meaning]
21857 A declarator-id shall not be qualified except for the
21858 definition of a ... nested class outside of its class
21859 ... [or] the definition or explicit instantiation of a
21860 class member of a namespace outside of its namespace. */
21861 if (scope == nested_name_specifier)
21863 permerror (nested_name_specifier_token_start->location,
21864 "extra qualification not allowed");
21865 nested_name_specifier = NULL_TREE;
21866 num_templates = 0;
21869 /* An explicit-specialization must be preceded by "template <>". If
21870 it is not, try to recover gracefully. */
21871 if (at_namespace_scope_p ()
21872 && parser->num_template_parameter_lists == 0
21873 && template_id_p)
21875 error_at (type_start_token->location,
21876 "an explicit specialization must be preceded by %<template <>%>");
21877 invalid_explicit_specialization_p = true;
21878 /* Take the same action that would have been taken by
21879 cp_parser_explicit_specialization. */
21880 ++parser->num_template_parameter_lists;
21881 begin_specialization ();
21883 /* There must be no "return" statements between this point and the
21884 end of this function; set "type "to the correct return value and
21885 use "goto done;" to return. */
21886 /* Make sure that the right number of template parameters were
21887 present. */
21888 if (!cp_parser_check_template_parameters (parser, num_templates,
21889 type_start_token->location,
21890 /*declarator=*/NULL))
21892 /* If something went wrong, there is no point in even trying to
21893 process the class-definition. */
21894 type = NULL_TREE;
21895 goto done;
21898 /* Look up the type. */
21899 if (template_id_p)
21901 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
21902 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
21903 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
21905 error_at (type_start_token->location,
21906 "function template %qD redeclared as a class template", id);
21907 type = error_mark_node;
21909 else
21911 type = TREE_TYPE (id);
21912 type = maybe_process_partial_specialization (type);
21914 if (nested_name_specifier)
21915 pushed_scope = push_scope (nested_name_specifier);
21917 else if (nested_name_specifier)
21919 tree class_type;
21921 /* Given:
21923 template <typename T> struct S { struct T };
21924 template <typename T> struct S<T>::T { };
21926 we will get a TYPENAME_TYPE when processing the definition of
21927 `S::T'. We need to resolve it to the actual type before we
21928 try to define it. */
21929 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
21931 class_type = resolve_typename_type (TREE_TYPE (type),
21932 /*only_current_p=*/false);
21933 if (TREE_CODE (class_type) != TYPENAME_TYPE)
21934 type = TYPE_NAME (class_type);
21935 else
21937 cp_parser_error (parser, "could not resolve typename type");
21938 type = error_mark_node;
21942 if (maybe_process_partial_specialization (TREE_TYPE (type))
21943 == error_mark_node)
21945 type = NULL_TREE;
21946 goto done;
21949 class_type = current_class_type;
21950 /* Enter the scope indicated by the nested-name-specifier. */
21951 pushed_scope = push_scope (nested_name_specifier);
21952 /* Get the canonical version of this type. */
21953 type = TYPE_MAIN_DECL (TREE_TYPE (type));
21954 /* Call push_template_decl if it seems like we should be defining a
21955 template either from the template headers or the type we're
21956 defining, so that we diagnose both extra and missing headers. */
21957 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
21958 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
21959 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
21961 type = push_template_decl (type);
21962 if (type == error_mark_node)
21964 type = NULL_TREE;
21965 goto done;
21969 type = TREE_TYPE (type);
21970 *nested_name_specifier_p = true;
21972 else /* The name is not a nested name. */
21974 /* If the class was unnamed, create a dummy name. */
21975 if (!id)
21976 id = make_anon_name ();
21977 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
21978 parser->num_template_parameter_lists);
21981 /* Indicate whether this class was declared as a `class' or as a
21982 `struct'. */
21983 if (TREE_CODE (type) == RECORD_TYPE)
21984 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
21985 cp_parser_check_class_key (class_key, type);
21987 /* If this type was already complete, and we see another definition,
21988 that's an error. */
21989 if (type != error_mark_node && COMPLETE_TYPE_P (type))
21991 error_at (type_start_token->location, "redefinition of %q#T",
21992 type);
21993 error_at (type_start_token->location, "previous definition of %q+#T",
21994 type);
21995 type = NULL_TREE;
21996 goto done;
21998 else if (type == error_mark_node)
21999 type = NULL_TREE;
22001 if (type)
22003 /* Apply attributes now, before any use of the class as a template
22004 argument in its base list. */
22005 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22006 fixup_attribute_variants (type);
22009 /* We will have entered the scope containing the class; the names of
22010 base classes should be looked up in that context. For example:
22012 struct A { struct B {}; struct C; };
22013 struct A::C : B {};
22015 is valid. */
22017 /* Get the list of base-classes, if there is one. */
22018 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22020 /* PR59482: enter the class scope so that base-specifiers are looked
22021 up correctly. */
22022 if (type)
22023 pushclass (type);
22024 bases = cp_parser_base_clause (parser);
22025 /* PR59482: get out of the previously pushed class scope so that the
22026 subsequent pops pop the right thing. */
22027 if (type)
22028 popclass ();
22030 else
22031 bases = NULL_TREE;
22033 /* If we're really defining a class, process the base classes.
22034 If they're invalid, fail. */
22035 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22036 && !xref_basetypes (type, bases))
22037 type = NULL_TREE;
22039 done:
22040 /* Leave the scope given by the nested-name-specifier. We will
22041 enter the class scope itself while processing the members. */
22042 if (pushed_scope)
22043 pop_scope (pushed_scope);
22045 if (invalid_explicit_specialization_p)
22047 end_specialization ();
22048 --parser->num_template_parameter_lists;
22051 if (type)
22052 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22053 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22054 CLASSTYPE_FINAL (type) = 1;
22055 out:
22056 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22057 return type;
22060 /* Parse a class-key.
22062 class-key:
22063 class
22064 struct
22065 union
22067 Returns the kind of class-key specified, or none_type to indicate
22068 error. */
22070 static enum tag_types
22071 cp_parser_class_key (cp_parser* parser)
22073 cp_token *token;
22074 enum tag_types tag_type;
22076 /* Look for the class-key. */
22077 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22078 if (!token)
22079 return none_type;
22081 /* Check to see if the TOKEN is a class-key. */
22082 tag_type = cp_parser_token_is_class_key (token);
22083 if (!tag_type)
22084 cp_parser_error (parser, "expected class-key");
22085 return tag_type;
22088 /* Parse a type-parameter-key.
22090 type-parameter-key:
22091 class
22092 typename
22095 static void
22096 cp_parser_type_parameter_key (cp_parser* parser)
22098 /* Look for the type-parameter-key. */
22099 enum tag_types tag_type = none_type;
22100 cp_token *token = cp_lexer_peek_token (parser->lexer);
22101 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22103 cp_lexer_consume_token (parser->lexer);
22104 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22105 /* typename is not allowed in a template template parameter
22106 by the standard until C++1Z. */
22107 pedwarn (token->location, OPT_Wpedantic,
22108 "ISO C++ forbids typename key in template template parameter;"
22109 " use -std=c++1z or -std=gnu++1z");
22111 else
22112 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22114 return;
22117 /* Parse an (optional) member-specification.
22119 member-specification:
22120 member-declaration member-specification [opt]
22121 access-specifier : member-specification [opt] */
22123 static void
22124 cp_parser_member_specification_opt (cp_parser* parser)
22126 while (true)
22128 cp_token *token;
22129 enum rid keyword;
22131 /* Peek at the next token. */
22132 token = cp_lexer_peek_token (parser->lexer);
22133 /* If it's a `}', or EOF then we've seen all the members. */
22134 if (token->type == CPP_CLOSE_BRACE
22135 || token->type == CPP_EOF
22136 || token->type == CPP_PRAGMA_EOL)
22137 break;
22139 /* See if this token is a keyword. */
22140 keyword = token->keyword;
22141 switch (keyword)
22143 case RID_PUBLIC:
22144 case RID_PROTECTED:
22145 case RID_PRIVATE:
22146 /* Consume the access-specifier. */
22147 cp_lexer_consume_token (parser->lexer);
22148 /* Remember which access-specifier is active. */
22149 current_access_specifier = token->u.value;
22150 /* Look for the `:'. */
22151 cp_parser_require (parser, CPP_COLON, RT_COLON);
22152 break;
22154 default:
22155 /* Accept #pragmas at class scope. */
22156 if (token->type == CPP_PRAGMA)
22158 cp_parser_pragma (parser, pragma_member, NULL);
22159 break;
22162 /* Otherwise, the next construction must be a
22163 member-declaration. */
22164 cp_parser_member_declaration (parser);
22169 /* Parse a member-declaration.
22171 member-declaration:
22172 decl-specifier-seq [opt] member-declarator-list [opt] ;
22173 function-definition ; [opt]
22174 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22175 using-declaration
22176 template-declaration
22177 alias-declaration
22179 member-declarator-list:
22180 member-declarator
22181 member-declarator-list , member-declarator
22183 member-declarator:
22184 declarator pure-specifier [opt]
22185 declarator constant-initializer [opt]
22186 identifier [opt] : constant-expression
22188 GNU Extensions:
22190 member-declaration:
22191 __extension__ member-declaration
22193 member-declarator:
22194 declarator attributes [opt] pure-specifier [opt]
22195 declarator attributes [opt] constant-initializer [opt]
22196 identifier [opt] attributes [opt] : constant-expression
22198 C++0x Extensions:
22200 member-declaration:
22201 static_assert-declaration */
22203 static void
22204 cp_parser_member_declaration (cp_parser* parser)
22206 cp_decl_specifier_seq decl_specifiers;
22207 tree prefix_attributes;
22208 tree decl;
22209 int declares_class_or_enum;
22210 bool friend_p;
22211 cp_token *token = NULL;
22212 cp_token *decl_spec_token_start = NULL;
22213 cp_token *initializer_token_start = NULL;
22214 int saved_pedantic;
22215 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22217 /* Check for the `__extension__' keyword. */
22218 if (cp_parser_extension_opt (parser, &saved_pedantic))
22220 /* Recurse. */
22221 cp_parser_member_declaration (parser);
22222 /* Restore the old value of the PEDANTIC flag. */
22223 pedantic = saved_pedantic;
22225 return;
22228 /* Check for a template-declaration. */
22229 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22231 /* An explicit specialization here is an error condition, and we
22232 expect the specialization handler to detect and report this. */
22233 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
22234 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
22235 cp_parser_explicit_specialization (parser);
22236 else
22237 cp_parser_template_declaration (parser, /*member_p=*/true);
22239 return;
22241 /* Check for a template introduction. */
22242 else if (cp_parser_template_declaration_after_export (parser, true))
22243 return;
22245 /* Check for a using-declaration. */
22246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
22248 if (cxx_dialect < cxx11)
22250 /* Parse the using-declaration. */
22251 cp_parser_using_declaration (parser,
22252 /*access_declaration_p=*/false);
22253 return;
22255 else
22257 tree decl;
22258 bool alias_decl_expected;
22259 cp_parser_parse_tentatively (parser);
22260 decl = cp_parser_alias_declaration (parser);
22261 /* Note that if we actually see the '=' token after the
22262 identifier, cp_parser_alias_declaration commits the
22263 tentative parse. In that case, we really expect an
22264 alias-declaration. Otherwise, we expect a using
22265 declaration. */
22266 alias_decl_expected =
22267 !cp_parser_uncommitted_to_tentative_parse_p (parser);
22268 cp_parser_parse_definitely (parser);
22270 if (alias_decl_expected)
22271 finish_member_declaration (decl);
22272 else
22273 cp_parser_using_declaration (parser,
22274 /*access_declaration_p=*/false);
22275 return;
22279 /* Check for @defs. */
22280 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
22282 tree ivar, member;
22283 tree ivar_chains = cp_parser_objc_defs_expression (parser);
22284 ivar = ivar_chains;
22285 while (ivar)
22287 member = ivar;
22288 ivar = TREE_CHAIN (member);
22289 TREE_CHAIN (member) = NULL_TREE;
22290 finish_member_declaration (member);
22292 return;
22295 /* If the next token is `static_assert' we have a static assertion. */
22296 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
22298 cp_parser_static_assert (parser, /*member_p=*/true);
22299 return;
22302 parser->colon_corrects_to_scope_p = false;
22304 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
22305 goto out;
22307 /* Parse the decl-specifier-seq. */
22308 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
22309 cp_parser_decl_specifier_seq (parser,
22310 CP_PARSER_FLAGS_OPTIONAL,
22311 &decl_specifiers,
22312 &declares_class_or_enum);
22313 /* Check for an invalid type-name. */
22314 if (!decl_specifiers.any_type_specifiers_p
22315 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
22316 goto out;
22317 /* If there is no declarator, then the decl-specifier-seq should
22318 specify a type. */
22319 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22321 /* If there was no decl-specifier-seq, and the next token is a
22322 `;', then we have something like:
22324 struct S { ; };
22326 [class.mem]
22328 Each member-declaration shall declare at least one member
22329 name of the class. */
22330 if (!decl_specifiers.any_specifiers_p)
22332 cp_token *token = cp_lexer_peek_token (parser->lexer);
22333 if (!in_system_header_at (token->location))
22334 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
22336 else
22338 tree type;
22340 /* See if this declaration is a friend. */
22341 friend_p = cp_parser_friend_p (&decl_specifiers);
22342 /* If there were decl-specifiers, check to see if there was
22343 a class-declaration. */
22344 type = check_tag_decl (&decl_specifiers,
22345 /*explicit_type_instantiation_p=*/false);
22346 /* Nested classes have already been added to the class, but
22347 a `friend' needs to be explicitly registered. */
22348 if (friend_p)
22350 /* If the `friend' keyword was present, the friend must
22351 be introduced with a class-key. */
22352 if (!declares_class_or_enum && cxx_dialect < cxx11)
22353 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
22354 "in C++03 a class-key must be used "
22355 "when declaring a friend");
22356 /* In this case:
22358 template <typename T> struct A {
22359 friend struct A<T>::B;
22362 A<T>::B will be represented by a TYPENAME_TYPE, and
22363 therefore not recognized by check_tag_decl. */
22364 if (!type)
22366 type = decl_specifiers.type;
22367 if (type && TREE_CODE (type) == TYPE_DECL)
22368 type = TREE_TYPE (type);
22370 if (!type || !TYPE_P (type))
22371 error_at (decl_spec_token_start->location,
22372 "friend declaration does not name a class or "
22373 "function");
22374 else
22375 make_friend_class (current_class_type, type,
22376 /*complain=*/true);
22378 /* If there is no TYPE, an error message will already have
22379 been issued. */
22380 else if (!type || type == error_mark_node)
22382 /* An anonymous aggregate has to be handled specially; such
22383 a declaration really declares a data member (with a
22384 particular type), as opposed to a nested class. */
22385 else if (ANON_AGGR_TYPE_P (type))
22387 /* C++11 9.5/6. */
22388 if (decl_specifiers.storage_class != sc_none)
22389 error_at (decl_spec_token_start->location,
22390 "a storage class on an anonymous aggregate "
22391 "in class scope is not allowed");
22393 /* Remove constructors and such from TYPE, now that we
22394 know it is an anonymous aggregate. */
22395 fixup_anonymous_aggr (type);
22396 /* And make the corresponding data member. */
22397 decl = build_decl (decl_spec_token_start->location,
22398 FIELD_DECL, NULL_TREE, type);
22399 /* Add it to the class. */
22400 finish_member_declaration (decl);
22402 else
22403 cp_parser_check_access_in_redeclaration
22404 (TYPE_NAME (type),
22405 decl_spec_token_start->location);
22408 else
22410 bool assume_semicolon = false;
22412 /* Clear attributes from the decl_specifiers but keep them
22413 around as prefix attributes that apply them to the entity
22414 being declared. */
22415 prefix_attributes = decl_specifiers.attributes;
22416 decl_specifiers.attributes = NULL_TREE;
22418 /* See if these declarations will be friends. */
22419 friend_p = cp_parser_friend_p (&decl_specifiers);
22421 /* Keep going until we hit the `;' at the end of the
22422 declaration. */
22423 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22425 tree attributes = NULL_TREE;
22426 tree first_attribute;
22428 /* Peek at the next token. */
22429 token = cp_lexer_peek_token (parser->lexer);
22431 /* Check for a bitfield declaration. */
22432 if (token->type == CPP_COLON
22433 || (token->type == CPP_NAME
22434 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
22435 == CPP_COLON))
22437 tree identifier;
22438 tree width;
22440 /* Get the name of the bitfield. Note that we cannot just
22441 check TOKEN here because it may have been invalidated by
22442 the call to cp_lexer_peek_nth_token above. */
22443 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
22444 identifier = cp_parser_identifier (parser);
22445 else
22446 identifier = NULL_TREE;
22448 /* Consume the `:' token. */
22449 cp_lexer_consume_token (parser->lexer);
22450 /* Get the width of the bitfield. */
22451 width
22452 = cp_parser_constant_expression (parser);
22454 /* Look for attributes that apply to the bitfield. */
22455 attributes = cp_parser_attributes_opt (parser);
22456 /* Remember which attributes are prefix attributes and
22457 which are not. */
22458 first_attribute = attributes;
22459 /* Combine the attributes. */
22460 attributes = chainon (prefix_attributes, attributes);
22462 /* Create the bitfield declaration. */
22463 decl = grokbitfield (identifier
22464 ? make_id_declarator (NULL_TREE,
22465 identifier,
22466 sfk_none)
22467 : NULL,
22468 &decl_specifiers,
22469 width,
22470 attributes);
22472 else
22474 cp_declarator *declarator;
22475 tree initializer;
22476 tree asm_specification;
22477 int ctor_dtor_or_conv_p;
22479 /* Parse the declarator. */
22480 declarator
22481 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22482 &ctor_dtor_or_conv_p,
22483 /*parenthesized_p=*/NULL,
22484 /*member_p=*/true,
22485 friend_p);
22487 /* If something went wrong parsing the declarator, make sure
22488 that we at least consume some tokens. */
22489 if (declarator == cp_error_declarator)
22491 /* Skip to the end of the statement. */
22492 cp_parser_skip_to_end_of_statement (parser);
22493 /* If the next token is not a semicolon, that is
22494 probably because we just skipped over the body of
22495 a function. So, we consume a semicolon if
22496 present, but do not issue an error message if it
22497 is not present. */
22498 if (cp_lexer_next_token_is (parser->lexer,
22499 CPP_SEMICOLON))
22500 cp_lexer_consume_token (parser->lexer);
22501 goto out;
22504 if (declares_class_or_enum & 2)
22505 cp_parser_check_for_definition_in_return_type
22506 (declarator, decl_specifiers.type,
22507 decl_specifiers.locations[ds_type_spec]);
22509 /* Look for an asm-specification. */
22510 asm_specification = cp_parser_asm_specification_opt (parser);
22511 /* Look for attributes that apply to the declaration. */
22512 attributes = cp_parser_attributes_opt (parser);
22513 /* Remember which attributes are prefix attributes and
22514 which are not. */
22515 first_attribute = attributes;
22516 /* Combine the attributes. */
22517 attributes = chainon (prefix_attributes, attributes);
22519 /* If it's an `=', then we have a constant-initializer or a
22520 pure-specifier. It is not correct to parse the
22521 initializer before registering the member declaration
22522 since the member declaration should be in scope while
22523 its initializer is processed. However, the rest of the
22524 front end does not yet provide an interface that allows
22525 us to handle this correctly. */
22526 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
22528 /* In [class.mem]:
22530 A pure-specifier shall be used only in the declaration of
22531 a virtual function.
22533 A member-declarator can contain a constant-initializer
22534 only if it declares a static member of integral or
22535 enumeration type.
22537 Therefore, if the DECLARATOR is for a function, we look
22538 for a pure-specifier; otherwise, we look for a
22539 constant-initializer. When we call `grokfield', it will
22540 perform more stringent semantics checks. */
22541 initializer_token_start = cp_lexer_peek_token (parser->lexer);
22542 if (function_declarator_p (declarator)
22543 || (decl_specifiers.type
22544 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
22545 && declarator->kind == cdk_id
22546 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
22547 == FUNCTION_TYPE)))
22548 initializer = cp_parser_pure_specifier (parser);
22549 else if (decl_specifiers.storage_class != sc_static)
22550 initializer = cp_parser_save_nsdmi (parser);
22551 else if (cxx_dialect >= cxx11)
22553 bool nonconst;
22554 /* Don't require a constant rvalue in C++11, since we
22555 might want a reference constant. We'll enforce
22556 constancy later. */
22557 cp_lexer_consume_token (parser->lexer);
22558 /* Parse the initializer. */
22559 initializer = cp_parser_initializer_clause (parser,
22560 &nonconst);
22562 else
22563 /* Parse the initializer. */
22564 initializer = cp_parser_constant_initializer (parser);
22566 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
22567 && !function_declarator_p (declarator))
22569 bool x;
22570 if (decl_specifiers.storage_class != sc_static)
22571 initializer = cp_parser_save_nsdmi (parser);
22572 else
22573 initializer = cp_parser_initializer (parser, &x, &x);
22575 /* Otherwise, there is no initializer. */
22576 else
22577 initializer = NULL_TREE;
22579 /* See if we are probably looking at a function
22580 definition. We are certainly not looking at a
22581 member-declarator. Calling `grokfield' has
22582 side-effects, so we must not do it unless we are sure
22583 that we are looking at a member-declarator. */
22584 if (cp_parser_token_starts_function_definition_p
22585 (cp_lexer_peek_token (parser->lexer)))
22587 /* The grammar does not allow a pure-specifier to be
22588 used when a member function is defined. (It is
22589 possible that this fact is an oversight in the
22590 standard, since a pure function may be defined
22591 outside of the class-specifier. */
22592 if (initializer && initializer_token_start)
22593 error_at (initializer_token_start->location,
22594 "pure-specifier on function-definition");
22595 decl = cp_parser_save_member_function_body (parser,
22596 &decl_specifiers,
22597 declarator,
22598 attributes);
22599 if (parser->fully_implicit_function_template_p)
22600 decl = finish_fully_implicit_template (parser, decl);
22601 /* If the member was not a friend, declare it here. */
22602 if (!friend_p)
22603 finish_member_declaration (decl);
22604 /* Peek at the next token. */
22605 token = cp_lexer_peek_token (parser->lexer);
22606 /* If the next token is a semicolon, consume it. */
22607 if (token->type == CPP_SEMICOLON)
22608 cp_lexer_consume_token (parser->lexer);
22609 goto out;
22611 else
22612 if (declarator->kind == cdk_function)
22613 declarator->id_loc = token->location;
22614 /* Create the declaration. */
22615 decl = grokfield (declarator, &decl_specifiers,
22616 initializer, /*init_const_expr_p=*/true,
22617 asm_specification, attributes);
22618 if (parser->fully_implicit_function_template_p)
22620 if (friend_p)
22621 finish_fully_implicit_template (parser, 0);
22622 else
22623 decl = finish_fully_implicit_template (parser, decl);
22627 cp_finalize_omp_declare_simd (parser, decl);
22628 cp_finalize_oacc_routine (parser, decl, false);
22630 /* Reset PREFIX_ATTRIBUTES. */
22631 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22632 attributes = TREE_CHAIN (attributes);
22633 if (attributes)
22634 TREE_CHAIN (attributes) = NULL_TREE;
22636 /* If there is any qualification still in effect, clear it
22637 now; we will be starting fresh with the next declarator. */
22638 parser->scope = NULL_TREE;
22639 parser->qualifying_scope = NULL_TREE;
22640 parser->object_scope = NULL_TREE;
22641 /* If it's a `,', then there are more declarators. */
22642 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22644 cp_lexer_consume_token (parser->lexer);
22645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22647 cp_token *token = cp_lexer_previous_token (parser->lexer);
22648 error_at (token->location,
22649 "stray %<,%> at end of member declaration");
22652 /* If the next token isn't a `;', then we have a parse error. */
22653 else if (cp_lexer_next_token_is_not (parser->lexer,
22654 CPP_SEMICOLON))
22656 /* The next token might be a ways away from where the
22657 actual semicolon is missing. Find the previous token
22658 and use that for our error position. */
22659 cp_token *token = cp_lexer_previous_token (parser->lexer);
22660 error_at (token->location,
22661 "expected %<;%> at end of member declaration");
22663 /* Assume that the user meant to provide a semicolon. If
22664 we were to cp_parser_skip_to_end_of_statement, we might
22665 skip to a semicolon inside a member function definition
22666 and issue nonsensical error messages. */
22667 assume_semicolon = true;
22670 if (decl)
22672 /* Add DECL to the list of members. */
22673 if (!friend_p
22674 /* Explicitly include, eg, NSDMIs, for better error
22675 recovery (c++/58650). */
22676 || !DECL_DECLARES_FUNCTION_P (decl))
22677 finish_member_declaration (decl);
22679 if (TREE_CODE (decl) == FUNCTION_DECL)
22680 cp_parser_save_default_args (parser, decl);
22681 else if (TREE_CODE (decl) == FIELD_DECL
22682 && !DECL_C_BIT_FIELD (decl)
22683 && DECL_INITIAL (decl))
22684 /* Add DECL to the queue of NSDMI to be parsed later. */
22685 vec_safe_push (unparsed_nsdmis, decl);
22688 if (assume_semicolon)
22689 goto out;
22693 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
22694 out:
22695 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22698 /* Parse a pure-specifier.
22700 pure-specifier:
22703 Returns INTEGER_ZERO_NODE if a pure specifier is found.
22704 Otherwise, ERROR_MARK_NODE is returned. */
22706 static tree
22707 cp_parser_pure_specifier (cp_parser* parser)
22709 cp_token *token;
22711 /* Look for the `=' token. */
22712 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22713 return error_mark_node;
22714 /* Look for the `0' token. */
22715 token = cp_lexer_peek_token (parser->lexer);
22717 if (token->type == CPP_EOF
22718 || token->type == CPP_PRAGMA_EOL)
22719 return error_mark_node;
22721 cp_lexer_consume_token (parser->lexer);
22723 /* Accept = default or = delete in c++0x mode. */
22724 if (token->keyword == RID_DEFAULT
22725 || token->keyword == RID_DELETE)
22727 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
22728 return token->u.value;
22731 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
22732 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
22734 cp_parser_error (parser,
22735 "invalid pure specifier (only %<= 0%> is allowed)");
22736 cp_parser_skip_to_end_of_statement (parser);
22737 return error_mark_node;
22739 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
22741 error_at (token->location, "templates may not be %<virtual%>");
22742 return error_mark_node;
22745 return integer_zero_node;
22748 /* Parse a constant-initializer.
22750 constant-initializer:
22751 = constant-expression
22753 Returns a representation of the constant-expression. */
22755 static tree
22756 cp_parser_constant_initializer (cp_parser* parser)
22758 /* Look for the `=' token. */
22759 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
22760 return error_mark_node;
22762 /* It is invalid to write:
22764 struct S { static const int i = { 7 }; };
22767 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22769 cp_parser_error (parser,
22770 "a brace-enclosed initializer is not allowed here");
22771 /* Consume the opening brace. */
22772 cp_lexer_consume_token (parser->lexer);
22773 /* Skip the initializer. */
22774 cp_parser_skip_to_closing_brace (parser);
22775 /* Look for the trailing `}'. */
22776 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22778 return error_mark_node;
22781 return cp_parser_constant_expression (parser);
22784 /* Derived classes [gram.class.derived] */
22786 /* Parse a base-clause.
22788 base-clause:
22789 : base-specifier-list
22791 base-specifier-list:
22792 base-specifier ... [opt]
22793 base-specifier-list , base-specifier ... [opt]
22795 Returns a TREE_LIST representing the base-classes, in the order in
22796 which they were declared. The representation of each node is as
22797 described by cp_parser_base_specifier.
22799 In the case that no bases are specified, this function will return
22800 NULL_TREE, not ERROR_MARK_NODE. */
22802 static tree
22803 cp_parser_base_clause (cp_parser* parser)
22805 tree bases = NULL_TREE;
22807 /* Look for the `:' that begins the list. */
22808 cp_parser_require (parser, CPP_COLON, RT_COLON);
22810 /* Scan the base-specifier-list. */
22811 while (true)
22813 cp_token *token;
22814 tree base;
22815 bool pack_expansion_p = false;
22817 /* Look for the base-specifier. */
22818 base = cp_parser_base_specifier (parser);
22819 /* Look for the (optional) ellipsis. */
22820 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22822 /* Consume the `...'. */
22823 cp_lexer_consume_token (parser->lexer);
22825 pack_expansion_p = true;
22828 /* Add BASE to the front of the list. */
22829 if (base && base != error_mark_node)
22831 if (pack_expansion_p)
22832 /* Make this a pack expansion type. */
22833 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
22835 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
22837 TREE_CHAIN (base) = bases;
22838 bases = base;
22841 /* Peek at the next token. */
22842 token = cp_lexer_peek_token (parser->lexer);
22843 /* If it's not a comma, then the list is complete. */
22844 if (token->type != CPP_COMMA)
22845 break;
22846 /* Consume the `,'. */
22847 cp_lexer_consume_token (parser->lexer);
22850 /* PARSER->SCOPE may still be non-NULL at this point, if the last
22851 base class had a qualified name. However, the next name that
22852 appears is certainly not qualified. */
22853 parser->scope = NULL_TREE;
22854 parser->qualifying_scope = NULL_TREE;
22855 parser->object_scope = NULL_TREE;
22857 return nreverse (bases);
22860 /* Parse a base-specifier.
22862 base-specifier:
22863 :: [opt] nested-name-specifier [opt] class-name
22864 virtual access-specifier [opt] :: [opt] nested-name-specifier
22865 [opt] class-name
22866 access-specifier virtual [opt] :: [opt] nested-name-specifier
22867 [opt] class-name
22869 Returns a TREE_LIST. The TREE_PURPOSE will be one of
22870 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
22871 indicate the specifiers provided. The TREE_VALUE will be a TYPE
22872 (or the ERROR_MARK_NODE) indicating the type that was specified. */
22874 static tree
22875 cp_parser_base_specifier (cp_parser* parser)
22877 cp_token *token;
22878 bool done = false;
22879 bool virtual_p = false;
22880 bool duplicate_virtual_error_issued_p = false;
22881 bool duplicate_access_error_issued_p = false;
22882 bool class_scope_p, template_p;
22883 tree access = access_default_node;
22884 tree type;
22886 /* Process the optional `virtual' and `access-specifier'. */
22887 while (!done)
22889 /* Peek at the next token. */
22890 token = cp_lexer_peek_token (parser->lexer);
22891 /* Process `virtual'. */
22892 switch (token->keyword)
22894 case RID_VIRTUAL:
22895 /* If `virtual' appears more than once, issue an error. */
22896 if (virtual_p && !duplicate_virtual_error_issued_p)
22898 cp_parser_error (parser,
22899 "%<virtual%> specified more than once in base-specified");
22900 duplicate_virtual_error_issued_p = true;
22903 virtual_p = true;
22905 /* Consume the `virtual' token. */
22906 cp_lexer_consume_token (parser->lexer);
22908 break;
22910 case RID_PUBLIC:
22911 case RID_PROTECTED:
22912 case RID_PRIVATE:
22913 /* If more than one access specifier appears, issue an
22914 error. */
22915 if (access != access_default_node
22916 && !duplicate_access_error_issued_p)
22918 cp_parser_error (parser,
22919 "more than one access specifier in base-specified");
22920 duplicate_access_error_issued_p = true;
22923 access = ridpointers[(int) token->keyword];
22925 /* Consume the access-specifier. */
22926 cp_lexer_consume_token (parser->lexer);
22928 break;
22930 default:
22931 done = true;
22932 break;
22935 /* It is not uncommon to see programs mechanically, erroneously, use
22936 the 'typename' keyword to denote (dependent) qualified types
22937 as base classes. */
22938 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
22940 token = cp_lexer_peek_token (parser->lexer);
22941 if (!processing_template_decl)
22942 error_at (token->location,
22943 "keyword %<typename%> not allowed outside of templates");
22944 else
22945 error_at (token->location,
22946 "keyword %<typename%> not allowed in this context "
22947 "(the base class is implicitly a type)");
22948 cp_lexer_consume_token (parser->lexer);
22951 /* Look for the optional `::' operator. */
22952 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
22953 /* Look for the nested-name-specifier. The simplest way to
22954 implement:
22956 [temp.res]
22958 The keyword `typename' is not permitted in a base-specifier or
22959 mem-initializer; in these contexts a qualified name that
22960 depends on a template-parameter is implicitly assumed to be a
22961 type name.
22963 is to pretend that we have seen the `typename' keyword at this
22964 point. */
22965 cp_parser_nested_name_specifier_opt (parser,
22966 /*typename_keyword_p=*/true,
22967 /*check_dependency_p=*/true,
22968 typename_type,
22969 /*is_declaration=*/true);
22970 /* If the base class is given by a qualified name, assume that names
22971 we see are type names or templates, as appropriate. */
22972 class_scope_p = (parser->scope && TYPE_P (parser->scope));
22973 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
22975 if (!parser->scope
22976 && cp_lexer_next_token_is_decltype (parser->lexer))
22977 /* DR 950 allows decltype as a base-specifier. */
22978 type = cp_parser_decltype (parser);
22979 else
22981 /* Otherwise, look for the class-name. */
22982 type = cp_parser_class_name (parser,
22983 class_scope_p,
22984 template_p,
22985 typename_type,
22986 /*check_dependency_p=*/true,
22987 /*class_head_p=*/false,
22988 /*is_declaration=*/true);
22989 type = TREE_TYPE (type);
22992 if (type == error_mark_node)
22993 return error_mark_node;
22995 return finish_base_specifier (type, access, virtual_p);
22998 /* Exception handling [gram.exception] */
23000 /* Parse an (optional) noexcept-specification.
23002 noexcept-specification:
23003 noexcept ( constant-expression ) [opt]
23005 If no noexcept-specification is present, returns NULL_TREE.
23006 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23007 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23008 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23009 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23010 in which case a boolean condition is returned instead. */
23012 static tree
23013 cp_parser_noexcept_specification_opt (cp_parser* parser,
23014 bool require_constexpr,
23015 bool* consumed_expr,
23016 bool return_cond)
23018 cp_token *token;
23019 const char *saved_message;
23021 /* Peek at the next token. */
23022 token = cp_lexer_peek_token (parser->lexer);
23024 /* Is it a noexcept-specification? */
23025 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23027 tree expr;
23028 cp_lexer_consume_token (parser->lexer);
23030 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23032 cp_lexer_consume_token (parser->lexer);
23034 if (require_constexpr)
23036 /* Types may not be defined in an exception-specification. */
23037 saved_message = parser->type_definition_forbidden_message;
23038 parser->type_definition_forbidden_message
23039 = G_("types may not be defined in an exception-specification");
23041 expr = cp_parser_constant_expression (parser);
23043 /* Restore the saved message. */
23044 parser->type_definition_forbidden_message = saved_message;
23046 else
23048 expr = cp_parser_expression (parser);
23049 *consumed_expr = true;
23052 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23054 else
23056 expr = boolean_true_node;
23057 if (!require_constexpr)
23058 *consumed_expr = false;
23061 /* We cannot build a noexcept-spec right away because this will check
23062 that expr is a constexpr. */
23063 if (!return_cond)
23064 return build_noexcept_spec (expr, tf_warning_or_error);
23065 else
23066 return expr;
23068 else
23069 return NULL_TREE;
23072 /* Parse an (optional) exception-specification.
23074 exception-specification:
23075 throw ( type-id-list [opt] )
23077 Returns a TREE_LIST representing the exception-specification. The
23078 TREE_VALUE of each node is a type. */
23080 static tree
23081 cp_parser_exception_specification_opt (cp_parser* parser)
23083 cp_token *token;
23084 tree type_id_list;
23085 const char *saved_message;
23087 /* Peek at the next token. */
23088 token = cp_lexer_peek_token (parser->lexer);
23090 /* Is it a noexcept-specification? */
23091 type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
23092 false);
23093 if (type_id_list != NULL_TREE)
23094 return type_id_list;
23096 /* If it's not `throw', then there's no exception-specification. */
23097 if (!cp_parser_is_keyword (token, RID_THROW))
23098 return NULL_TREE;
23100 #if 0
23101 /* Enable this once a lot of code has transitioned to noexcept? */
23102 if (cxx_dialect >= cxx11 && !in_system_header_at (input_location))
23103 warning (OPT_Wdeprecated, "dynamic exception specifications are "
23104 "deprecated in C++0x; use %<noexcept%> instead");
23105 #endif
23107 /* Consume the `throw'. */
23108 cp_lexer_consume_token (parser->lexer);
23110 /* Look for the `('. */
23111 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23113 /* Peek at the next token. */
23114 token = cp_lexer_peek_token (parser->lexer);
23115 /* If it's not a `)', then there is a type-id-list. */
23116 if (token->type != CPP_CLOSE_PAREN)
23118 /* Types may not be defined in an exception-specification. */
23119 saved_message = parser->type_definition_forbidden_message;
23120 parser->type_definition_forbidden_message
23121 = G_("types may not be defined in an exception-specification");
23122 /* Parse the type-id-list. */
23123 type_id_list = cp_parser_type_id_list (parser);
23124 /* Restore the saved message. */
23125 parser->type_definition_forbidden_message = saved_message;
23127 else
23128 type_id_list = empty_except_spec;
23130 /* Look for the `)'. */
23131 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23133 return type_id_list;
23136 /* Parse an (optional) type-id-list.
23138 type-id-list:
23139 type-id ... [opt]
23140 type-id-list , type-id ... [opt]
23142 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23143 in the order that the types were presented. */
23145 static tree
23146 cp_parser_type_id_list (cp_parser* parser)
23148 tree types = NULL_TREE;
23150 while (true)
23152 cp_token *token;
23153 tree type;
23155 token = cp_lexer_peek_token (parser->lexer);
23157 /* Get the next type-id. */
23158 type = cp_parser_type_id (parser);
23159 /* Check for invalid 'auto'. */
23160 if (flag_concepts && type_uses_auto (type))
23162 error_at (token->location,
23163 "invalid use of %<auto%> in exception-specification");
23164 type = error_mark_node;
23166 /* Parse the optional ellipsis. */
23167 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23169 /* Consume the `...'. */
23170 cp_lexer_consume_token (parser->lexer);
23172 /* Turn the type into a pack expansion expression. */
23173 type = make_pack_expansion (type);
23175 /* Add it to the list. */
23176 types = add_exception_specifier (types, type, /*complain=*/1);
23177 /* Peek at the next token. */
23178 token = cp_lexer_peek_token (parser->lexer);
23179 /* If it is not a `,', we are done. */
23180 if (token->type != CPP_COMMA)
23181 break;
23182 /* Consume the `,'. */
23183 cp_lexer_consume_token (parser->lexer);
23186 return nreverse (types);
23189 /* Parse a try-block.
23191 try-block:
23192 try compound-statement handler-seq */
23194 static tree
23195 cp_parser_try_block (cp_parser* parser)
23197 tree try_block;
23199 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
23200 if (parser->in_function_body
23201 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
23202 error ("%<try%> in %<constexpr%> function");
23204 try_block = begin_try_block ();
23205 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
23206 finish_try_block (try_block);
23207 cp_parser_handler_seq (parser);
23208 finish_handler_sequence (try_block);
23210 return try_block;
23213 /* Parse a function-try-block.
23215 function-try-block:
23216 try ctor-initializer [opt] function-body handler-seq */
23218 static bool
23219 cp_parser_function_try_block (cp_parser* parser)
23221 tree compound_stmt;
23222 tree try_block;
23223 bool ctor_initializer_p;
23225 /* Look for the `try' keyword. */
23226 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
23227 return false;
23228 /* Let the rest of the front end know where we are. */
23229 try_block = begin_function_try_block (&compound_stmt);
23230 /* Parse the function-body. */
23231 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
23232 (parser, /*in_function_try_block=*/true);
23233 /* We're done with the `try' part. */
23234 finish_function_try_block (try_block);
23235 /* Parse the handlers. */
23236 cp_parser_handler_seq (parser);
23237 /* We're done with the handlers. */
23238 finish_function_handler_sequence (try_block, compound_stmt);
23240 return ctor_initializer_p;
23243 /* Parse a handler-seq.
23245 handler-seq:
23246 handler handler-seq [opt] */
23248 static void
23249 cp_parser_handler_seq (cp_parser* parser)
23251 while (true)
23253 cp_token *token;
23255 /* Parse the handler. */
23256 cp_parser_handler (parser);
23257 /* Peek at the next token. */
23258 token = cp_lexer_peek_token (parser->lexer);
23259 /* If it's not `catch' then there are no more handlers. */
23260 if (!cp_parser_is_keyword (token, RID_CATCH))
23261 break;
23265 /* Parse a handler.
23267 handler:
23268 catch ( exception-declaration ) compound-statement */
23270 static void
23271 cp_parser_handler (cp_parser* parser)
23273 tree handler;
23274 tree declaration;
23276 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
23277 handler = begin_handler ();
23278 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23279 declaration = cp_parser_exception_declaration (parser);
23280 finish_handler_parms (declaration, handler);
23281 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23282 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
23283 finish_handler (handler);
23286 /* Parse an exception-declaration.
23288 exception-declaration:
23289 type-specifier-seq declarator
23290 type-specifier-seq abstract-declarator
23291 type-specifier-seq
23294 Returns a VAR_DECL for the declaration, or NULL_TREE if the
23295 ellipsis variant is used. */
23297 static tree
23298 cp_parser_exception_declaration (cp_parser* parser)
23300 cp_decl_specifier_seq type_specifiers;
23301 cp_declarator *declarator;
23302 const char *saved_message;
23304 /* If it's an ellipsis, it's easy to handle. */
23305 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23307 /* Consume the `...' token. */
23308 cp_lexer_consume_token (parser->lexer);
23309 return NULL_TREE;
23312 /* Types may not be defined in exception-declarations. */
23313 saved_message = parser->type_definition_forbidden_message;
23314 parser->type_definition_forbidden_message
23315 = G_("types may not be defined in exception-declarations");
23317 /* Parse the type-specifier-seq. */
23318 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
23319 /*is_trailing_return=*/false,
23320 &type_specifiers);
23321 /* If it's a `)', then there is no declarator. */
23322 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23323 declarator = NULL;
23324 else
23325 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
23326 /*ctor_dtor_or_conv_p=*/NULL,
23327 /*parenthesized_p=*/NULL,
23328 /*member_p=*/false,
23329 /*friend_p=*/false);
23331 /* Restore the saved message. */
23332 parser->type_definition_forbidden_message = saved_message;
23334 if (!type_specifiers.any_specifiers_p)
23335 return error_mark_node;
23337 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
23340 /* Parse a throw-expression.
23342 throw-expression:
23343 throw assignment-expression [opt]
23345 Returns a THROW_EXPR representing the throw-expression. */
23347 static tree
23348 cp_parser_throw_expression (cp_parser* parser)
23350 tree expression;
23351 cp_token* token;
23353 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
23354 token = cp_lexer_peek_token (parser->lexer);
23355 /* Figure out whether or not there is an assignment-expression
23356 following the "throw" keyword. */
23357 if (token->type == CPP_COMMA
23358 || token->type == CPP_SEMICOLON
23359 || token->type == CPP_CLOSE_PAREN
23360 || token->type == CPP_CLOSE_SQUARE
23361 || token->type == CPP_CLOSE_BRACE
23362 || token->type == CPP_COLON)
23363 expression = NULL_TREE;
23364 else
23365 expression = cp_parser_assignment_expression (parser);
23367 return build_throw (expression);
23370 /* GNU Extensions */
23372 /* Parse an (optional) asm-specification.
23374 asm-specification:
23375 asm ( string-literal )
23377 If the asm-specification is present, returns a STRING_CST
23378 corresponding to the string-literal. Otherwise, returns
23379 NULL_TREE. */
23381 static tree
23382 cp_parser_asm_specification_opt (cp_parser* parser)
23384 cp_token *token;
23385 tree asm_specification;
23387 /* Peek at the next token. */
23388 token = cp_lexer_peek_token (parser->lexer);
23389 /* If the next token isn't the `asm' keyword, then there's no
23390 asm-specification. */
23391 if (!cp_parser_is_keyword (token, RID_ASM))
23392 return NULL_TREE;
23394 /* Consume the `asm' token. */
23395 cp_lexer_consume_token (parser->lexer);
23396 /* Look for the `('. */
23397 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23399 /* Look for the string-literal. */
23400 asm_specification = cp_parser_string_literal (parser, false, false);
23402 /* Look for the `)'. */
23403 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23405 return asm_specification;
23408 /* Parse an asm-operand-list.
23410 asm-operand-list:
23411 asm-operand
23412 asm-operand-list , asm-operand
23414 asm-operand:
23415 string-literal ( expression )
23416 [ string-literal ] string-literal ( expression )
23418 Returns a TREE_LIST representing the operands. The TREE_VALUE of
23419 each node is the expression. The TREE_PURPOSE is itself a
23420 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
23421 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
23422 is a STRING_CST for the string literal before the parenthesis. Returns
23423 ERROR_MARK_NODE if any of the operands are invalid. */
23425 static tree
23426 cp_parser_asm_operand_list (cp_parser* parser)
23428 tree asm_operands = NULL_TREE;
23429 bool invalid_operands = false;
23431 while (true)
23433 tree string_literal;
23434 tree expression;
23435 tree name;
23437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
23439 /* Consume the `[' token. */
23440 cp_lexer_consume_token (parser->lexer);
23441 /* Read the operand name. */
23442 name = cp_parser_identifier (parser);
23443 if (name != error_mark_node)
23444 name = build_string (IDENTIFIER_LENGTH (name),
23445 IDENTIFIER_POINTER (name));
23446 /* Look for the closing `]'. */
23447 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23449 else
23450 name = NULL_TREE;
23451 /* Look for the string-literal. */
23452 string_literal = cp_parser_string_literal (parser, false, false);
23454 /* Look for the `('. */
23455 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23456 /* Parse the expression. */
23457 expression = cp_parser_expression (parser);
23458 /* Look for the `)'. */
23459 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23461 if (name == error_mark_node
23462 || string_literal == error_mark_node
23463 || expression == error_mark_node)
23464 invalid_operands = true;
23466 /* Add this operand to the list. */
23467 asm_operands = tree_cons (build_tree_list (name, string_literal),
23468 expression,
23469 asm_operands);
23470 /* If the next token is not a `,', there are no more
23471 operands. */
23472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23473 break;
23474 /* Consume the `,'. */
23475 cp_lexer_consume_token (parser->lexer);
23478 return invalid_operands ? error_mark_node : nreverse (asm_operands);
23481 /* Parse an asm-clobber-list.
23483 asm-clobber-list:
23484 string-literal
23485 asm-clobber-list , string-literal
23487 Returns a TREE_LIST, indicating the clobbers in the order that they
23488 appeared. The TREE_VALUE of each node is a STRING_CST. */
23490 static tree
23491 cp_parser_asm_clobber_list (cp_parser* parser)
23493 tree clobbers = NULL_TREE;
23495 while (true)
23497 tree string_literal;
23499 /* Look for the string literal. */
23500 string_literal = cp_parser_string_literal (parser, false, false);
23501 /* Add it to the list. */
23502 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
23503 /* If the next token is not a `,', then the list is
23504 complete. */
23505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23506 break;
23507 /* Consume the `,' token. */
23508 cp_lexer_consume_token (parser->lexer);
23511 return clobbers;
23514 /* Parse an asm-label-list.
23516 asm-label-list:
23517 identifier
23518 asm-label-list , identifier
23520 Returns a TREE_LIST, indicating the labels in the order that they
23521 appeared. The TREE_VALUE of each node is a label. */
23523 static tree
23524 cp_parser_asm_label_list (cp_parser* parser)
23526 tree labels = NULL_TREE;
23528 while (true)
23530 tree identifier, label, name;
23532 /* Look for the identifier. */
23533 identifier = cp_parser_identifier (parser);
23534 if (!error_operand_p (identifier))
23536 label = lookup_label (identifier);
23537 if (TREE_CODE (label) == LABEL_DECL)
23539 TREE_USED (label) = 1;
23540 check_goto (label);
23541 name = build_string (IDENTIFIER_LENGTH (identifier),
23542 IDENTIFIER_POINTER (identifier));
23543 labels = tree_cons (name, label, labels);
23546 /* If the next token is not a `,', then the list is
23547 complete. */
23548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23549 break;
23550 /* Consume the `,' token. */
23551 cp_lexer_consume_token (parser->lexer);
23554 return nreverse (labels);
23557 /* Return TRUE iff the next tokens in the stream are possibly the
23558 beginning of a GNU extension attribute. */
23560 static bool
23561 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
23563 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
23566 /* Return TRUE iff the next tokens in the stream are possibly the
23567 beginning of a standard C++-11 attribute specifier. */
23569 static bool
23570 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
23572 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
23575 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23576 beginning of a standard C++-11 attribute specifier. */
23578 static bool
23579 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
23581 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23583 return (cxx_dialect >= cxx11
23584 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
23585 || (token->type == CPP_OPEN_SQUARE
23586 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
23587 && token->type == CPP_OPEN_SQUARE)));
23590 /* Return TRUE iff the next Nth tokens in the stream are possibly the
23591 beginning of a GNU extension attribute. */
23593 static bool
23594 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
23596 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
23598 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
23601 /* Return true iff the next tokens can be the beginning of either a
23602 GNU attribute list, or a standard C++11 attribute sequence. */
23604 static bool
23605 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
23607 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
23608 || cp_next_tokens_can_be_std_attribute_p (parser));
23611 /* Return true iff the next Nth tokens can be the beginning of either
23612 a GNU attribute list, or a standard C++11 attribute sequence. */
23614 static bool
23615 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
23617 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
23618 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
23621 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
23622 of GNU attributes, or return NULL. */
23624 static tree
23625 cp_parser_attributes_opt (cp_parser *parser)
23627 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
23628 return cp_parser_gnu_attributes_opt (parser);
23629 return cp_parser_std_attribute_spec_seq (parser);
23632 #define CILK_SIMD_FN_CLAUSE_MASK \
23633 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
23634 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
23635 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
23636 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
23637 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
23639 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
23640 vector [(<clauses>)] */
23642 static void
23643 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
23645 bool first_p = parser->cilk_simd_fn_info == NULL;
23646 cp_token *token = v_token;
23647 if (first_p)
23649 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
23650 parser->cilk_simd_fn_info->error_seen = false;
23651 parser->cilk_simd_fn_info->fndecl_seen = false;
23652 parser->cilk_simd_fn_info->tokens = vNULL;
23654 int paren_scope = 0;
23655 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23657 cp_lexer_consume_token (parser->lexer);
23658 v_token = cp_lexer_peek_token (parser->lexer);
23659 paren_scope++;
23661 while (paren_scope > 0)
23663 token = cp_lexer_peek_token (parser->lexer);
23664 if (token->type == CPP_OPEN_PAREN)
23665 paren_scope++;
23666 else if (token->type == CPP_CLOSE_PAREN)
23667 paren_scope--;
23668 /* Do not push the last ')' */
23669 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
23670 cp_lexer_consume_token (parser->lexer);
23673 token->type = CPP_PRAGMA_EOL;
23674 parser->lexer->next_token = token;
23675 cp_lexer_consume_token (parser->lexer);
23677 struct cp_token_cache *cp
23678 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
23679 parser->cilk_simd_fn_info->tokens.safe_push (cp);
23682 /* Parse an (optional) series of attributes.
23684 attributes:
23685 attributes attribute
23687 attribute:
23688 __attribute__ (( attribute-list [opt] ))
23690 The return value is as for cp_parser_gnu_attribute_list. */
23692 static tree
23693 cp_parser_gnu_attributes_opt (cp_parser* parser)
23695 tree attributes = NULL_TREE;
23697 while (true)
23699 cp_token *token;
23700 tree attribute_list;
23701 bool ok = true;
23703 /* Peek at the next token. */
23704 token = cp_lexer_peek_token (parser->lexer);
23705 /* If it's not `__attribute__', then we're done. */
23706 if (token->keyword != RID_ATTRIBUTE)
23707 break;
23709 /* Consume the `__attribute__' keyword. */
23710 cp_lexer_consume_token (parser->lexer);
23711 /* Look for the two `(' tokens. */
23712 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23713 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23715 /* Peek at the next token. */
23716 token = cp_lexer_peek_token (parser->lexer);
23717 if (token->type != CPP_CLOSE_PAREN)
23718 /* Parse the attribute-list. */
23719 attribute_list = cp_parser_gnu_attribute_list (parser);
23720 else
23721 /* If the next token is a `)', then there is no attribute
23722 list. */
23723 attribute_list = NULL;
23725 /* Look for the two `)' tokens. */
23726 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23727 ok = false;
23728 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23729 ok = false;
23730 if (!ok)
23731 cp_parser_skip_to_end_of_statement (parser);
23733 /* Add these new attributes to the list. */
23734 attributes = chainon (attributes, attribute_list);
23737 return attributes;
23740 /* Parse a GNU attribute-list.
23742 attribute-list:
23743 attribute
23744 attribute-list , attribute
23746 attribute:
23747 identifier
23748 identifier ( identifier )
23749 identifier ( identifier , expression-list )
23750 identifier ( expression-list )
23752 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
23753 to an attribute. The TREE_PURPOSE of each node is the identifier
23754 indicating which attribute is in use. The TREE_VALUE represents
23755 the arguments, if any. */
23757 static tree
23758 cp_parser_gnu_attribute_list (cp_parser* parser)
23760 tree attribute_list = NULL_TREE;
23761 bool save_translate_strings_p = parser->translate_strings_p;
23763 parser->translate_strings_p = false;
23764 while (true)
23766 cp_token *token;
23767 tree identifier;
23768 tree attribute;
23770 /* Look for the identifier. We also allow keywords here; for
23771 example `__attribute__ ((const))' is legal. */
23772 token = cp_lexer_peek_token (parser->lexer);
23773 if (token->type == CPP_NAME
23774 || token->type == CPP_KEYWORD)
23776 tree arguments = NULL_TREE;
23778 /* Consume the token, but save it since we need it for the
23779 SIMD enabled function parsing. */
23780 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
23782 /* Save away the identifier that indicates which attribute
23783 this is. */
23784 identifier = (token->type == CPP_KEYWORD)
23785 /* For keywords, use the canonical spelling, not the
23786 parsed identifier. */
23787 ? ridpointers[(int) token->keyword]
23788 : id_token->u.value;
23790 attribute = build_tree_list (identifier, NULL_TREE);
23792 /* Peek at the next token. */
23793 token = cp_lexer_peek_token (parser->lexer);
23794 /* If it's an `(', then parse the attribute arguments. */
23795 if (token->type == CPP_OPEN_PAREN)
23797 vec<tree, va_gc> *vec;
23798 int attr_flag = (attribute_takes_identifier_p (identifier)
23799 ? id_attr : normal_attr);
23800 if (is_cilkplus_vector_p (identifier))
23802 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23803 continue;
23805 else
23806 vec = cp_parser_parenthesized_expression_list
23807 (parser, attr_flag, /*cast_p=*/false,
23808 /*allow_expansion_p=*/false,
23809 /*non_constant_p=*/NULL);
23810 if (vec == NULL)
23811 arguments = error_mark_node;
23812 else
23814 arguments = build_tree_list_vec (vec);
23815 release_tree_vector (vec);
23817 /* Save the arguments away. */
23818 TREE_VALUE (attribute) = arguments;
23820 else if (is_cilkplus_vector_p (identifier))
23822 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
23823 continue;
23826 if (arguments != error_mark_node)
23828 /* Add this attribute to the list. */
23829 TREE_CHAIN (attribute) = attribute_list;
23830 attribute_list = attribute;
23833 token = cp_lexer_peek_token (parser->lexer);
23835 /* Now, look for more attributes. If the next token isn't a
23836 `,', we're done. */
23837 if (token->type != CPP_COMMA)
23838 break;
23840 /* Consume the comma and keep going. */
23841 cp_lexer_consume_token (parser->lexer);
23843 parser->translate_strings_p = save_translate_strings_p;
23845 /* We built up the list in reverse order. */
23846 return nreverse (attribute_list);
23849 /* Parse a standard C++11 attribute.
23851 The returned representation is a TREE_LIST which TREE_PURPOSE is
23852 the scoped name of the attribute, and the TREE_VALUE is its
23853 arguments list.
23855 Note that the scoped name of the attribute is itself a TREE_LIST
23856 which TREE_PURPOSE is the namespace of the attribute, and
23857 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
23858 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
23859 and which TREE_PURPOSE is directly the attribute name.
23861 Clients of the attribute code should use get_attribute_namespace
23862 and get_attribute_name to get the actual namespace and name of
23863 attributes, regardless of their being GNU or C++11 attributes.
23865 attribute:
23866 attribute-token attribute-argument-clause [opt]
23868 attribute-token:
23869 identifier
23870 attribute-scoped-token
23872 attribute-scoped-token:
23873 attribute-namespace :: identifier
23875 attribute-namespace:
23876 identifier
23878 attribute-argument-clause:
23879 ( balanced-token-seq )
23881 balanced-token-seq:
23882 balanced-token [opt]
23883 balanced-token-seq balanced-token
23885 balanced-token:
23886 ( balanced-token-seq )
23887 [ balanced-token-seq ]
23888 { balanced-token-seq }. */
23890 static tree
23891 cp_parser_std_attribute (cp_parser *parser)
23893 tree attribute, attr_ns = NULL_TREE, attr_id = NULL_TREE, arguments;
23894 cp_token *token;
23896 /* First, parse name of the attribute, a.k.a attribute-token. */
23898 token = cp_lexer_peek_token (parser->lexer);
23899 if (token->type == CPP_NAME)
23900 attr_id = token->u.value;
23901 else if (token->type == CPP_KEYWORD)
23902 attr_id = ridpointers[(int) token->keyword];
23903 else if (token->flags & NAMED_OP)
23904 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
23906 if (attr_id == NULL_TREE)
23907 return NULL_TREE;
23909 cp_lexer_consume_token (parser->lexer);
23911 token = cp_lexer_peek_token (parser->lexer);
23912 if (token->type == CPP_SCOPE)
23914 /* We are seeing a scoped attribute token. */
23916 cp_lexer_consume_token (parser->lexer);
23917 attr_ns = attr_id;
23919 token = cp_lexer_consume_token (parser->lexer);
23920 if (token->type == CPP_NAME)
23921 attr_id = token->u.value;
23922 else if (token->type == CPP_KEYWORD)
23923 attr_id = ridpointers[(int) token->keyword];
23924 else
23926 error_at (token->location,
23927 "expected an identifier for the attribute name");
23928 return error_mark_node;
23930 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
23931 NULL_TREE);
23932 token = cp_lexer_peek_token (parser->lexer);
23934 else
23936 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
23937 NULL_TREE);
23938 /* C++11 noreturn attribute is equivalent to GNU's. */
23939 if (is_attribute_p ("noreturn", attr_id))
23940 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23941 /* C++14 deprecated attribute is equivalent to GNU's. */
23942 else if (cxx_dialect >= cxx11 && is_attribute_p ("deprecated", attr_id))
23944 if (cxx_dialect == cxx11)
23945 pedwarn (token->location, OPT_Wpedantic,
23946 "%<deprecated%> is a C++14 feature;"
23947 " use %<gnu::deprecated%>");
23948 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
23950 /* Transactional Memory TS optimize_for_synchronized attribute is
23951 equivalent to GNU transaction_callable. */
23952 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
23953 TREE_PURPOSE (attribute)
23954 = get_identifier ("transaction_callable");
23955 /* Transactional Memory attributes are GNU attributes. */
23956 else if (tm_attr_to_mask (attr_id))
23957 TREE_PURPOSE (attribute) = attr_id;
23960 /* Now parse the optional argument clause of the attribute. */
23962 if (token->type != CPP_OPEN_PAREN)
23963 return attribute;
23966 vec<tree, va_gc> *vec;
23967 int attr_flag = normal_attr;
23969 if (attr_ns == get_identifier ("gnu")
23970 && attribute_takes_identifier_p (attr_id))
23971 /* A GNU attribute that takes an identifier in parameter. */
23972 attr_flag = id_attr;
23974 vec = cp_parser_parenthesized_expression_list
23975 (parser, attr_flag, /*cast_p=*/false,
23976 /*allow_expansion_p=*/true,
23977 /*non_constant_p=*/NULL);
23978 if (vec == NULL)
23979 arguments = error_mark_node;
23980 else
23982 arguments = build_tree_list_vec (vec);
23983 release_tree_vector (vec);
23986 if (arguments == error_mark_node)
23987 attribute = error_mark_node;
23988 else
23989 TREE_VALUE (attribute) = arguments;
23992 return attribute;
23995 /* Check that the attribute ATTRIBUTE appears at most once in the
23996 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
23997 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
23998 isn't implemented yet in GCC. */
24000 static void
24001 cp_parser_check_std_attribute (tree attributes, tree attribute)
24003 if (attributes)
24005 tree name = get_attribute_name (attribute);
24006 if (is_attribute_p ("noreturn", name)
24007 && lookup_attribute ("noreturn", attributes))
24008 error ("attribute noreturn can appear at most once "
24009 "in an attribute-list");
24010 else if (is_attribute_p ("deprecated", name)
24011 && lookup_attribute ("deprecated", attributes))
24012 error ("attribute deprecated can appear at most once "
24013 "in an attribute-list");
24017 /* Parse a list of standard C++-11 attributes.
24019 attribute-list:
24020 attribute [opt]
24021 attribute-list , attribute[opt]
24022 attribute ...
24023 attribute-list , attribute ...
24026 static tree
24027 cp_parser_std_attribute_list (cp_parser *parser)
24029 tree attributes = NULL_TREE, attribute = NULL_TREE;
24030 cp_token *token = NULL;
24032 while (true)
24034 attribute = cp_parser_std_attribute (parser);
24035 if (attribute == error_mark_node)
24036 break;
24037 if (attribute != NULL_TREE)
24039 cp_parser_check_std_attribute (attributes, attribute);
24040 TREE_CHAIN (attribute) = attributes;
24041 attributes = attribute;
24043 token = cp_lexer_peek_token (parser->lexer);
24044 if (token->type == CPP_ELLIPSIS)
24046 cp_lexer_consume_token (parser->lexer);
24047 TREE_VALUE (attribute)
24048 = make_pack_expansion (TREE_VALUE (attribute));
24049 token = cp_lexer_peek_token (parser->lexer);
24051 if (token->type != CPP_COMMA)
24052 break;
24053 cp_lexer_consume_token (parser->lexer);
24055 attributes = nreverse (attributes);
24056 return attributes;
24059 /* Parse a standard C++-11 attribute specifier.
24061 attribute-specifier:
24062 [ [ attribute-list ] ]
24063 alignment-specifier
24065 alignment-specifier:
24066 alignas ( type-id ... [opt] )
24067 alignas ( alignment-expression ... [opt] ). */
24069 static tree
24070 cp_parser_std_attribute_spec (cp_parser *parser)
24072 tree attributes = NULL_TREE;
24073 cp_token *token = cp_lexer_peek_token (parser->lexer);
24075 if (token->type == CPP_OPEN_SQUARE
24076 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24078 cp_lexer_consume_token (parser->lexer);
24079 cp_lexer_consume_token (parser->lexer);
24081 attributes = cp_parser_std_attribute_list (parser);
24083 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24084 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24085 cp_parser_skip_to_end_of_statement (parser);
24086 else
24087 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24088 when we are sure that we have actually parsed them. */
24089 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24091 else
24093 tree alignas_expr;
24095 /* Look for an alignment-specifier. */
24097 token = cp_lexer_peek_token (parser->lexer);
24099 if (token->type != CPP_KEYWORD
24100 || token->keyword != RID_ALIGNAS)
24101 return NULL_TREE;
24103 cp_lexer_consume_token (parser->lexer);
24104 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24106 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24108 cp_parser_error (parser, "expected %<(%>");
24109 return error_mark_node;
24112 cp_parser_parse_tentatively (parser);
24113 alignas_expr = cp_parser_type_id (parser);
24115 if (!cp_parser_parse_definitely (parser))
24117 gcc_assert (alignas_expr == error_mark_node
24118 || alignas_expr == NULL_TREE);
24120 alignas_expr =
24121 cp_parser_assignment_expression (parser);
24122 if (alignas_expr == error_mark_node)
24123 cp_parser_skip_to_end_of_statement (parser);
24124 if (alignas_expr == NULL_TREE
24125 || alignas_expr == error_mark_node)
24126 return alignas_expr;
24129 alignas_expr = cxx_alignas_expr (alignas_expr);
24130 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24132 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24134 cp_lexer_consume_token (parser->lexer);
24135 alignas_expr = make_pack_expansion (alignas_expr);
24138 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24140 cp_parser_error (parser, "expected %<)%>");
24141 return error_mark_node;
24144 /* Build the C++-11 representation of an 'aligned'
24145 attribute. */
24146 attributes =
24147 build_tree_list (build_tree_list (get_identifier ("gnu"),
24148 get_identifier ("aligned")),
24149 alignas_expr);
24152 return attributes;
24155 /* Parse a standard C++-11 attribute-specifier-seq.
24157 attribute-specifier-seq:
24158 attribute-specifier-seq [opt] attribute-specifier
24161 static tree
24162 cp_parser_std_attribute_spec_seq (cp_parser *parser)
24164 tree attr_specs = NULL_TREE;
24165 tree attr_last = NULL_TREE;
24167 while (true)
24169 tree attr_spec = cp_parser_std_attribute_spec (parser);
24170 if (attr_spec == NULL_TREE)
24171 break;
24172 if (attr_spec == error_mark_node)
24173 return error_mark_node;
24175 if (attr_last)
24176 TREE_CHAIN (attr_last) = attr_spec;
24177 else
24178 attr_specs = attr_last = attr_spec;
24179 attr_last = tree_last (attr_last);
24182 return attr_specs;
24185 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
24186 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
24187 current value of the PEDANTIC flag, regardless of whether or not
24188 the `__extension__' keyword is present. The caller is responsible
24189 for restoring the value of the PEDANTIC flag. */
24191 static bool
24192 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
24194 /* Save the old value of the PEDANTIC flag. */
24195 *saved_pedantic = pedantic;
24197 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
24199 /* Consume the `__extension__' token. */
24200 cp_lexer_consume_token (parser->lexer);
24201 /* We're not being pedantic while the `__extension__' keyword is
24202 in effect. */
24203 pedantic = 0;
24205 return true;
24208 return false;
24211 /* Parse a label declaration.
24213 label-declaration:
24214 __label__ label-declarator-seq ;
24216 label-declarator-seq:
24217 identifier , label-declarator-seq
24218 identifier */
24220 static void
24221 cp_parser_label_declaration (cp_parser* parser)
24223 /* Look for the `__label__' keyword. */
24224 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
24226 while (true)
24228 tree identifier;
24230 /* Look for an identifier. */
24231 identifier = cp_parser_identifier (parser);
24232 /* If we failed, stop. */
24233 if (identifier == error_mark_node)
24234 break;
24235 /* Declare it as a label. */
24236 finish_label_decl (identifier);
24237 /* If the next token is a `;', stop. */
24238 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24239 break;
24240 /* Look for the `,' separating the label declarations. */
24241 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
24244 /* Look for the final `;'. */
24245 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24248 // -------------------------------------------------------------------------- //
24249 // Requires Clause
24251 // Parse a requires clause.
24253 // requires-clause:
24254 // 'requires' logical-or-expression
24256 // The required logical-or-expression must be a constant expression. Note
24257 // that we don't check that the expression is constepxr here. We defer until
24258 // we analyze constraints and then, we only check atomic constraints.
24259 static tree
24260 cp_parser_requires_clause (cp_parser *parser)
24262 // Parse the requires clause so that it is not automatically folded.
24263 ++processing_template_decl;
24264 tree expr = cp_parser_binary_expression (parser, false, false,
24265 PREC_NOT_OPERATOR, NULL);
24266 if (check_for_bare_parameter_packs (expr))
24267 expr = error_mark_node;
24268 --processing_template_decl;
24269 return expr;
24272 // Optionally parse a requires clause:
24273 static tree
24274 cp_parser_requires_clause_opt (cp_parser *parser)
24276 cp_token *tok = cp_lexer_peek_token (parser->lexer);
24277 if (tok->keyword != RID_REQUIRES)
24279 if (!flag_concepts && tok->type == CPP_NAME
24280 && tok->u.value == ridpointers[RID_REQUIRES])
24282 error_at (cp_lexer_peek_token (parser->lexer)->location,
24283 "%<requires%> only available with -fconcepts");
24284 /* Parse and discard the requires-clause. */
24285 cp_lexer_consume_token (parser->lexer);
24286 cp_parser_requires_clause (parser);
24288 return NULL_TREE;
24290 cp_lexer_consume_token (parser->lexer);
24291 return cp_parser_requires_clause (parser);
24295 /*---------------------------------------------------------------------------
24296 Requires expressions
24297 ---------------------------------------------------------------------------*/
24299 /* Parse a requires expression
24301 requirement-expression:
24302 'requires' requirement-parameter-list [opt] requirement-body */
24303 static tree
24304 cp_parser_requires_expression (cp_parser *parser)
24306 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
24307 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
24309 /* A requires-expression shall appear only within a concept
24310 definition or a requires-clause.
24312 TODO: Implement this diagnostic correctly. */
24313 if (!processing_template_decl)
24315 error_at (loc, "a requires expression cannot appear outside a template");
24316 cp_parser_skip_to_end_of_statement (parser);
24317 return error_mark_node;
24320 tree parms, reqs;
24322 /* Local parameters are delared as variables within the scope
24323 of the expression. They are not visible past the end of
24324 the expression. Expressions within the requires-expression
24325 are unevaluated. */
24326 struct scope_sentinel
24328 scope_sentinel ()
24330 ++cp_unevaluated_operand;
24331 begin_scope (sk_block, NULL_TREE);
24334 ~scope_sentinel ()
24336 pop_bindings_and_leave_scope ();
24337 --cp_unevaluated_operand;
24339 } s;
24341 /* Parse the optional parameter list. */
24342 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24344 parms = cp_parser_requirement_parameter_list (parser);
24345 if (parms == error_mark_node)
24346 return error_mark_node;
24348 else
24349 parms = NULL_TREE;
24351 /* Parse the requirement body. */
24352 reqs = cp_parser_requirement_body (parser);
24353 if (reqs == error_mark_node)
24354 return error_mark_node;
24357 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
24358 the parm chain. */
24359 grokparms (parms, &parms);
24360 return finish_requires_expr (parms, reqs);
24363 /* Parse a parameterized requirement.
24365 requirement-parameter-list:
24366 '(' parameter-declaration-clause ')' */
24367 static tree
24368 cp_parser_requirement_parameter_list (cp_parser *parser)
24370 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24371 return error_mark_node;
24373 tree parms = cp_parser_parameter_declaration_clause (parser);
24375 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24376 return error_mark_node;
24378 return parms;
24381 /* Parse the body of a requirement.
24383 requirement-body:
24384 '{' requirement-list '}' */
24385 static tree
24386 cp_parser_requirement_body (cp_parser *parser)
24388 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24389 return error_mark_node;
24391 tree reqs = cp_parser_requirement_list (parser);
24393 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24394 return error_mark_node;
24396 return reqs;
24399 /* Parse a list of requirements.
24401 requirement-list:
24402 requirement
24403 requirement-list ';' requirement[opt] */
24404 static tree
24405 cp_parser_requirement_list (cp_parser *parser)
24407 tree result = NULL_TREE;
24408 while (true)
24410 tree req = cp_parser_requirement (parser);
24411 if (req == error_mark_node)
24412 return error_mark_node;
24414 result = tree_cons (NULL_TREE, req, result);
24416 /* If we see a semi-colon, consume it. */
24417 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24418 cp_lexer_consume_token (parser->lexer);
24420 /* Stop processing at the end of the list. */
24421 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24422 break;
24425 /* Reverse the order of requirements so they are analyzed in
24426 declaration order. */
24427 return nreverse (result);
24430 /* Parse a syntactic requirement or type requirement.
24432 requirement:
24433 simple-requirement
24434 compound-requirement
24435 type-requirement
24436 nested-requirement */
24437 static tree
24438 cp_parser_requirement (cp_parser *parser)
24440 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24441 return cp_parser_compound_requirement (parser);
24442 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24443 return cp_parser_type_requirement (parser);
24444 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
24445 return cp_parser_nested_requirement (parser);
24446 else
24447 return cp_parser_simple_requirement (parser);
24450 /* Parse a simple requirement.
24452 simple-requirement:
24453 expression ';' */
24454 static tree
24455 cp_parser_simple_requirement (cp_parser *parser)
24457 tree expr = cp_parser_expression (parser, NULL, false, false);
24458 if (!expr || expr == error_mark_node)
24459 return error_mark_node;
24461 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24462 return error_mark_node;
24464 return finish_simple_requirement (expr);
24467 /* Parse a type requirement
24469 type-requirement
24470 nested-name-specifier [opt] required-type-name ';'
24472 required-type-name:
24473 type-name
24474 'template' [opt] simple-template-id */
24475 static tree
24476 cp_parser_type_requirement (cp_parser *parser)
24478 cp_lexer_consume_token (parser->lexer);
24480 // Save the scope before parsing name specifiers.
24481 tree saved_scope = parser->scope;
24482 tree saved_object_scope = parser->object_scope;
24483 tree saved_qualifying_scope = parser->qualifying_scope;
24484 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
24485 cp_parser_nested_name_specifier_opt (parser,
24486 /*typename_keyword_p=*/true,
24487 /*check_dependency_p=*/false,
24488 /*type_p=*/true,
24489 /*is_declaration=*/false);
24491 tree type;
24492 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
24494 cp_lexer_consume_token (parser->lexer);
24495 type = cp_parser_template_id (parser,
24496 /*template_keyword_p=*/true,
24497 /*check_dependency=*/false,
24498 /*tag_type=*/none_type,
24499 /*is_declaration=*/false);
24500 type = make_typename_type (parser->scope, type, typename_type,
24501 /*complain=*/tf_error);
24503 else
24504 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
24506 if (TREE_CODE (type) == TYPE_DECL)
24507 type = TREE_TYPE (type);
24509 parser->scope = saved_scope;
24510 parser->object_scope = saved_object_scope;
24511 parser->qualifying_scope = saved_qualifying_scope;
24513 if (type == error_mark_node)
24514 cp_parser_skip_to_end_of_statement (parser);
24516 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24517 return error_mark_node;
24518 if (type == error_mark_node)
24519 return error_mark_node;
24521 return finish_type_requirement (type);
24524 /* Parse a compound requirement
24526 compound-requirement:
24527 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
24528 static tree
24529 cp_parser_compound_requirement (cp_parser *parser)
24531 /* Parse an expression enclosed in '{ }'s. */
24532 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24533 return error_mark_node;
24535 tree expr = cp_parser_expression (parser, NULL, false, false);
24536 if (!expr || expr == error_mark_node)
24537 return error_mark_node;
24539 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
24540 return error_mark_node;
24542 /* Parse the optional noexcept. */
24543 bool noexcept_p = false;
24544 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
24546 cp_lexer_consume_token (parser->lexer);
24547 noexcept_p = true;
24550 /* Parse the optional trailing return type. */
24551 tree type = NULL_TREE;
24552 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
24554 cp_lexer_consume_token (parser->lexer);
24555 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
24556 parser->in_result_type_constraint_p = true;
24557 type = cp_parser_trailing_type_id (parser);
24558 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
24559 if (type == error_mark_node)
24560 return error_mark_node;
24563 return finish_compound_requirement (expr, type, noexcept_p);
24566 /* Parse a nested requirement. This is the same as a requires clause.
24568 nested-requirement:
24569 requires-clause */
24570 static tree
24571 cp_parser_nested_requirement (cp_parser *parser)
24573 cp_lexer_consume_token (parser->lexer);
24574 tree req = cp_parser_requires_clause (parser);
24575 if (req == error_mark_node)
24576 return error_mark_node;
24577 return finish_nested_requirement (req);
24580 /* Support Functions */
24582 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
24583 NAME should have one of the representations used for an
24584 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
24585 is returned. If PARSER->SCOPE is a dependent type, then a
24586 SCOPE_REF is returned.
24588 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
24589 returned; the name was already resolved when the TEMPLATE_ID_EXPR
24590 was formed. Abstractly, such entities should not be passed to this
24591 function, because they do not need to be looked up, but it is
24592 simpler to check for this special case here, rather than at the
24593 call-sites.
24595 In cases not explicitly covered above, this function returns a
24596 DECL, OVERLOAD, or baselink representing the result of the lookup.
24597 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
24598 is returned.
24600 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
24601 (e.g., "struct") that was used. In that case bindings that do not
24602 refer to types are ignored.
24604 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
24605 ignored.
24607 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
24608 are ignored.
24610 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
24611 types.
24613 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
24614 TREE_LIST of candidates if name-lookup results in an ambiguity, and
24615 NULL_TREE otherwise. */
24617 static cp_expr
24618 cp_parser_lookup_name (cp_parser *parser, tree name,
24619 enum tag_types tag_type,
24620 bool is_template,
24621 bool is_namespace,
24622 bool check_dependency,
24623 tree *ambiguous_decls,
24624 location_t name_location)
24626 tree decl;
24627 tree object_type = parser->context->object_type;
24629 /* Assume that the lookup will be unambiguous. */
24630 if (ambiguous_decls)
24631 *ambiguous_decls = NULL_TREE;
24633 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
24634 no longer valid. Note that if we are parsing tentatively, and
24635 the parse fails, OBJECT_TYPE will be automatically restored. */
24636 parser->context->object_type = NULL_TREE;
24638 if (name == error_mark_node)
24639 return error_mark_node;
24641 /* A template-id has already been resolved; there is no lookup to
24642 do. */
24643 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
24644 return name;
24645 if (BASELINK_P (name))
24647 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
24648 == TEMPLATE_ID_EXPR);
24649 return name;
24652 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
24653 it should already have been checked to make sure that the name
24654 used matches the type being destroyed. */
24655 if (TREE_CODE (name) == BIT_NOT_EXPR)
24657 tree type;
24659 /* Figure out to which type this destructor applies. */
24660 if (parser->scope)
24661 type = parser->scope;
24662 else if (object_type)
24663 type = object_type;
24664 else
24665 type = current_class_type;
24666 /* If that's not a class type, there is no destructor. */
24667 if (!type || !CLASS_TYPE_P (type))
24668 return error_mark_node;
24669 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
24670 lazily_declare_fn (sfk_destructor, type);
24671 if (!CLASSTYPE_DESTRUCTORS (type))
24672 return error_mark_node;
24673 /* If it was a class type, return the destructor. */
24674 return CLASSTYPE_DESTRUCTORS (type);
24677 /* By this point, the NAME should be an ordinary identifier. If
24678 the id-expression was a qualified name, the qualifying scope is
24679 stored in PARSER->SCOPE at this point. */
24680 gcc_assert (identifier_p (name));
24682 /* Perform the lookup. */
24683 if (parser->scope)
24685 bool dependent_p;
24687 if (parser->scope == error_mark_node)
24688 return error_mark_node;
24690 /* If the SCOPE is dependent, the lookup must be deferred until
24691 the template is instantiated -- unless we are explicitly
24692 looking up names in uninstantiated templates. Even then, we
24693 cannot look up the name if the scope is not a class type; it
24694 might, for example, be a template type parameter. */
24695 dependent_p = (TYPE_P (parser->scope)
24696 && dependent_scope_p (parser->scope));
24697 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
24698 && dependent_p)
24699 /* Defer lookup. */
24700 decl = error_mark_node;
24701 else
24703 tree pushed_scope = NULL_TREE;
24705 /* If PARSER->SCOPE is a dependent type, then it must be a
24706 class type, and we must not be checking dependencies;
24707 otherwise, we would have processed this lookup above. So
24708 that PARSER->SCOPE is not considered a dependent base by
24709 lookup_member, we must enter the scope here. */
24710 if (dependent_p)
24711 pushed_scope = push_scope (parser->scope);
24713 /* If the PARSER->SCOPE is a template specialization, it
24714 may be instantiated during name lookup. In that case,
24715 errors may be issued. Even if we rollback the current
24716 tentative parse, those errors are valid. */
24717 decl = lookup_qualified_name (parser->scope, name,
24718 tag_type != none_type,
24719 /*complain=*/true);
24721 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
24722 lookup result and the nested-name-specifier nominates a class C:
24723 * if the name specified after the nested-name-specifier, when
24724 looked up in C, is the injected-class-name of C (Clause 9), or
24725 * if the name specified after the nested-name-specifier is the
24726 same as the identifier or the simple-template-id's template-
24727 name in the last component of the nested-name-specifier,
24728 the name is instead considered to name the constructor of
24729 class C. [ Note: for example, the constructor is not an
24730 acceptable lookup result in an elaborated-type-specifier so
24731 the constructor would not be used in place of the
24732 injected-class-name. --end note ] Such a constructor name
24733 shall be used only in the declarator-id of a declaration that
24734 names a constructor or in a using-declaration. */
24735 if (tag_type == none_type
24736 && DECL_SELF_REFERENCE_P (decl)
24737 && same_type_p (DECL_CONTEXT (decl), parser->scope))
24738 decl = lookup_qualified_name (parser->scope, ctor_identifier,
24739 tag_type != none_type,
24740 /*complain=*/true);
24742 /* If we have a single function from a using decl, pull it out. */
24743 if (TREE_CODE (decl) == OVERLOAD
24744 && !really_overloaded_fn (decl))
24745 decl = OVL_FUNCTION (decl);
24747 if (pushed_scope)
24748 pop_scope (pushed_scope);
24751 /* If the scope is a dependent type and either we deferred lookup or
24752 we did lookup but didn't find the name, rememeber the name. */
24753 if (decl == error_mark_node && TYPE_P (parser->scope)
24754 && dependent_type_p (parser->scope))
24756 if (tag_type)
24758 tree type;
24760 /* The resolution to Core Issue 180 says that `struct
24761 A::B' should be considered a type-name, even if `A'
24762 is dependent. */
24763 type = make_typename_type (parser->scope, name, tag_type,
24764 /*complain=*/tf_error);
24765 if (type != error_mark_node)
24766 decl = TYPE_NAME (type);
24768 else if (is_template
24769 && (cp_parser_next_token_ends_template_argument_p (parser)
24770 || cp_lexer_next_token_is (parser->lexer,
24771 CPP_CLOSE_PAREN)))
24772 decl = make_unbound_class_template (parser->scope,
24773 name, NULL_TREE,
24774 /*complain=*/tf_error);
24775 else
24776 decl = build_qualified_name (/*type=*/NULL_TREE,
24777 parser->scope, name,
24778 is_template);
24780 parser->qualifying_scope = parser->scope;
24781 parser->object_scope = NULL_TREE;
24783 else if (object_type)
24785 /* Look up the name in the scope of the OBJECT_TYPE, unless the
24786 OBJECT_TYPE is not a class. */
24787 if (CLASS_TYPE_P (object_type))
24788 /* If the OBJECT_TYPE is a template specialization, it may
24789 be instantiated during name lookup. In that case, errors
24790 may be issued. Even if we rollback the current tentative
24791 parse, those errors are valid. */
24792 decl = lookup_member (object_type,
24793 name,
24794 /*protect=*/0,
24795 tag_type != none_type,
24796 tf_warning_or_error);
24797 else
24798 decl = NULL_TREE;
24800 if (!decl)
24802 /* Look it up in the enclosing context. */
24803 decl = lookup_name_real (name, tag_type != none_type,
24804 /*nonclass=*/0,
24805 /*block_p=*/true, is_namespace, 0);
24806 /* DR 141 says when looking for a template-name after -> or ., only
24807 consider class templates. We need to fix our handling of
24808 dependent expressions to implement that properly, but for now
24809 let's ignore namespace-scope function templates. */
24810 if (decl && is_template && !DECL_TYPE_TEMPLATE_P (decl))
24812 tree d = decl;
24813 if (is_overloaded_fn (d))
24814 d = get_first_fn (d);
24815 if (DECL_P (d) && !DECL_CLASS_SCOPE_P (d))
24816 decl = NULL_TREE;
24819 if (object_type == unknown_type_node)
24820 /* The object is type-dependent, so we can't look anything up; we used
24821 this to get the DR 141 behavior. */
24822 object_type = NULL_TREE;
24823 parser->object_scope = object_type;
24824 parser->qualifying_scope = NULL_TREE;
24826 else
24828 decl = lookup_name_real (name, tag_type != none_type,
24829 /*nonclass=*/0,
24830 /*block_p=*/true, is_namespace, 0);
24831 parser->qualifying_scope = NULL_TREE;
24832 parser->object_scope = NULL_TREE;
24835 /* If the lookup failed, let our caller know. */
24836 if (!decl || decl == error_mark_node)
24837 return error_mark_node;
24839 /* Pull out the template from an injected-class-name (or multiple). */
24840 if (is_template)
24841 decl = maybe_get_template_decl_from_type_decl (decl);
24843 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
24844 if (TREE_CODE (decl) == TREE_LIST)
24846 if (ambiguous_decls)
24847 *ambiguous_decls = decl;
24848 /* The error message we have to print is too complicated for
24849 cp_parser_error, so we incorporate its actions directly. */
24850 if (!cp_parser_simulate_error (parser))
24852 error_at (name_location, "reference to %qD is ambiguous",
24853 name);
24854 print_candidates (decl);
24856 return error_mark_node;
24859 gcc_assert (DECL_P (decl)
24860 || TREE_CODE (decl) == OVERLOAD
24861 || TREE_CODE (decl) == SCOPE_REF
24862 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
24863 || BASELINK_P (decl));
24865 /* If we have resolved the name of a member declaration, check to
24866 see if the declaration is accessible. When the name resolves to
24867 set of overloaded functions, accessibility is checked when
24868 overload resolution is done.
24870 During an explicit instantiation, access is not checked at all,
24871 as per [temp.explicit]. */
24872 if (DECL_P (decl))
24873 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
24875 maybe_record_typedef_use (decl);
24877 return cp_expr (decl, name_location);
24880 /* Like cp_parser_lookup_name, but for use in the typical case where
24881 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
24882 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
24884 static tree
24885 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
24887 return cp_parser_lookup_name (parser, name,
24888 none_type,
24889 /*is_template=*/false,
24890 /*is_namespace=*/false,
24891 /*check_dependency=*/true,
24892 /*ambiguous_decls=*/NULL,
24893 location);
24896 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
24897 the current context, return the TYPE_DECL. If TAG_NAME_P is
24898 true, the DECL indicates the class being defined in a class-head,
24899 or declared in an elaborated-type-specifier.
24901 Otherwise, return DECL. */
24903 static tree
24904 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
24906 /* If the TEMPLATE_DECL is being declared as part of a class-head,
24907 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
24909 struct A {
24910 template <typename T> struct B;
24913 template <typename T> struct A::B {};
24915 Similarly, in an elaborated-type-specifier:
24917 namespace N { struct X{}; }
24919 struct A {
24920 template <typename T> friend struct N::X;
24923 However, if the DECL refers to a class type, and we are in
24924 the scope of the class, then the name lookup automatically
24925 finds the TYPE_DECL created by build_self_reference rather
24926 than a TEMPLATE_DECL. For example, in:
24928 template <class T> struct S {
24929 S s;
24932 there is no need to handle such case. */
24934 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
24935 return DECL_TEMPLATE_RESULT (decl);
24937 return decl;
24940 /* If too many, or too few, template-parameter lists apply to the
24941 declarator, issue an error message. Returns TRUE if all went well,
24942 and FALSE otherwise. */
24944 static bool
24945 cp_parser_check_declarator_template_parameters (cp_parser* parser,
24946 cp_declarator *declarator,
24947 location_t declarator_location)
24949 switch (declarator->kind)
24951 case cdk_id:
24953 unsigned num_templates = 0;
24954 tree scope = declarator->u.id.qualifying_scope;
24956 if (scope)
24957 num_templates = num_template_headers_for_class (scope);
24958 else if (TREE_CODE (declarator->u.id.unqualified_name)
24959 == TEMPLATE_ID_EXPR)
24960 /* If the DECLARATOR has the form `X<y>' then it uses one
24961 additional level of template parameters. */
24962 ++num_templates;
24964 return cp_parser_check_template_parameters
24965 (parser, num_templates, declarator_location, declarator);
24968 case cdk_function:
24969 case cdk_array:
24970 case cdk_pointer:
24971 case cdk_reference:
24972 case cdk_ptrmem:
24973 return (cp_parser_check_declarator_template_parameters
24974 (parser, declarator->declarator, declarator_location));
24976 case cdk_error:
24977 return true;
24979 default:
24980 gcc_unreachable ();
24982 return false;
24985 /* NUM_TEMPLATES were used in the current declaration. If that is
24986 invalid, return FALSE and issue an error messages. Otherwise,
24987 return TRUE. If DECLARATOR is non-NULL, then we are checking a
24988 declarator and we can print more accurate diagnostics. */
24990 static bool
24991 cp_parser_check_template_parameters (cp_parser* parser,
24992 unsigned num_templates,
24993 location_t location,
24994 cp_declarator *declarator)
24996 /* If there are the same number of template classes and parameter
24997 lists, that's OK. */
24998 if (parser->num_template_parameter_lists == num_templates)
24999 return true;
25000 /* If there are more, but only one more, then we are referring to a
25001 member template. That's OK too. */
25002 if (parser->num_template_parameter_lists == num_templates + 1)
25003 return true;
25004 /* If there are more template classes than parameter lists, we have
25005 something like:
25007 template <class T> void S<T>::R<T>::f (); */
25008 if (parser->num_template_parameter_lists < num_templates)
25010 if (declarator && !current_function_decl)
25011 error_at (location, "specializing member %<%T::%E%> "
25012 "requires %<template<>%> syntax",
25013 declarator->u.id.qualifying_scope,
25014 declarator->u.id.unqualified_name);
25015 else if (declarator)
25016 error_at (location, "invalid declaration of %<%T::%E%>",
25017 declarator->u.id.qualifying_scope,
25018 declarator->u.id.unqualified_name);
25019 else
25020 error_at (location, "too few template-parameter-lists");
25021 return false;
25023 /* Otherwise, there are too many template parameter lists. We have
25024 something like:
25026 template <class T> template <class U> void S::f(); */
25027 error_at (location, "too many template-parameter-lists");
25028 return false;
25031 /* Parse an optional `::' token indicating that the following name is
25032 from the global namespace. If so, PARSER->SCOPE is set to the
25033 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25034 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25035 Returns the new value of PARSER->SCOPE, if the `::' token is
25036 present, and NULL_TREE otherwise. */
25038 static tree
25039 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25041 cp_token *token;
25043 /* Peek at the next token. */
25044 token = cp_lexer_peek_token (parser->lexer);
25045 /* If we're looking at a `::' token then we're starting from the
25046 global namespace, not our current location. */
25047 if (token->type == CPP_SCOPE)
25049 /* Consume the `::' token. */
25050 cp_lexer_consume_token (parser->lexer);
25051 /* Set the SCOPE so that we know where to start the lookup. */
25052 parser->scope = global_namespace;
25053 parser->qualifying_scope = global_namespace;
25054 parser->object_scope = NULL_TREE;
25056 return parser->scope;
25058 else if (!current_scope_valid_p)
25060 parser->scope = NULL_TREE;
25061 parser->qualifying_scope = NULL_TREE;
25062 parser->object_scope = NULL_TREE;
25065 return NULL_TREE;
25068 /* Returns TRUE if the upcoming token sequence is the start of a
25069 constructor declarator. If FRIEND_P is true, the declarator is
25070 preceded by the `friend' specifier. */
25072 static bool
25073 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25075 bool constructor_p;
25076 bool outside_class_specifier_p;
25077 tree nested_name_specifier;
25078 cp_token *next_token;
25080 /* The common case is that this is not a constructor declarator, so
25081 try to avoid doing lots of work if at all possible. It's not
25082 valid declare a constructor at function scope. */
25083 if (parser->in_function_body)
25084 return false;
25085 /* And only certain tokens can begin a constructor declarator. */
25086 next_token = cp_lexer_peek_token (parser->lexer);
25087 if (next_token->type != CPP_NAME
25088 && next_token->type != CPP_SCOPE
25089 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25090 && next_token->type != CPP_TEMPLATE_ID)
25091 return false;
25093 /* Parse tentatively; we are going to roll back all of the tokens
25094 consumed here. */
25095 cp_parser_parse_tentatively (parser);
25096 /* Assume that we are looking at a constructor declarator. */
25097 constructor_p = true;
25099 /* Look for the optional `::' operator. */
25100 cp_parser_global_scope_opt (parser,
25101 /*current_scope_valid_p=*/false);
25102 /* Look for the nested-name-specifier. */
25103 nested_name_specifier
25104 = (cp_parser_nested_name_specifier_opt (parser,
25105 /*typename_keyword_p=*/false,
25106 /*check_dependency_p=*/false,
25107 /*type_p=*/false,
25108 /*is_declaration=*/false));
25110 outside_class_specifier_p = (!at_class_scope_p ()
25111 || !TYPE_BEING_DEFINED (current_class_type)
25112 || friend_p);
25114 /* Outside of a class-specifier, there must be a
25115 nested-name-specifier. */
25116 if (!nested_name_specifier && outside_class_specifier_p)
25117 constructor_p = false;
25118 else if (nested_name_specifier == error_mark_node)
25119 constructor_p = false;
25121 /* If we have a class scope, this is easy; DR 147 says that S::S always
25122 names the constructor, and no other qualified name could. */
25123 if (constructor_p && nested_name_specifier
25124 && CLASS_TYPE_P (nested_name_specifier))
25126 tree id = cp_parser_unqualified_id (parser,
25127 /*template_keyword_p=*/false,
25128 /*check_dependency_p=*/false,
25129 /*declarator_p=*/true,
25130 /*optional_p=*/false);
25131 if (is_overloaded_fn (id))
25132 id = DECL_NAME (get_first_fn (id));
25133 if (!constructor_name_p (id, nested_name_specifier))
25134 constructor_p = false;
25136 /* If we still think that this might be a constructor-declarator,
25137 look for a class-name. */
25138 else if (constructor_p)
25140 /* If we have:
25142 template <typename T> struct S {
25143 S();
25146 we must recognize that the nested `S' names a class. */
25147 tree type_decl;
25148 type_decl = cp_parser_class_name (parser,
25149 /*typename_keyword_p=*/false,
25150 /*template_keyword_p=*/false,
25151 none_type,
25152 /*check_dependency_p=*/false,
25153 /*class_head_p=*/false,
25154 /*is_declaration=*/false);
25155 /* If there was no class-name, then this is not a constructor.
25156 Otherwise, if we are in a class-specifier and we aren't
25157 handling a friend declaration, check that its type matches
25158 current_class_type (c++/38313). Note: error_mark_node
25159 is left alone for error recovery purposes. */
25160 constructor_p = (!cp_parser_error_occurred (parser)
25161 && (outside_class_specifier_p
25162 || type_decl == error_mark_node
25163 || same_type_p (current_class_type,
25164 TREE_TYPE (type_decl))));
25166 /* If we're still considering a constructor, we have to see a `(',
25167 to begin the parameter-declaration-clause, followed by either a
25168 `)', an `...', or a decl-specifier. We need to check for a
25169 type-specifier to avoid being fooled into thinking that:
25171 S (f) (int);
25173 is a constructor. (It is actually a function named `f' that
25174 takes one parameter (of type `int') and returns a value of type
25175 `S'. */
25176 if (constructor_p
25177 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25178 constructor_p = false;
25180 if (constructor_p
25181 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
25182 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
25183 /* A parameter declaration begins with a decl-specifier,
25184 which is either the "attribute" keyword, a storage class
25185 specifier, or (usually) a type-specifier. */
25186 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
25188 tree type;
25189 tree pushed_scope = NULL_TREE;
25190 unsigned saved_num_template_parameter_lists;
25192 /* Names appearing in the type-specifier should be looked up
25193 in the scope of the class. */
25194 if (current_class_type)
25195 type = NULL_TREE;
25196 else
25198 type = TREE_TYPE (type_decl);
25199 if (TREE_CODE (type) == TYPENAME_TYPE)
25201 type = resolve_typename_type (type,
25202 /*only_current_p=*/false);
25203 if (TREE_CODE (type) == TYPENAME_TYPE)
25205 cp_parser_abort_tentative_parse (parser);
25206 return false;
25209 pushed_scope = push_scope (type);
25212 /* Inside the constructor parameter list, surrounding
25213 template-parameter-lists do not apply. */
25214 saved_num_template_parameter_lists
25215 = parser->num_template_parameter_lists;
25216 parser->num_template_parameter_lists = 0;
25218 /* Look for the type-specifier. */
25219 cp_parser_type_specifier (parser,
25220 CP_PARSER_FLAGS_NONE,
25221 /*decl_specs=*/NULL,
25222 /*is_declarator=*/true,
25223 /*declares_class_or_enum=*/NULL,
25224 /*is_cv_qualifier=*/NULL);
25226 parser->num_template_parameter_lists
25227 = saved_num_template_parameter_lists;
25229 /* Leave the scope of the class. */
25230 if (pushed_scope)
25231 pop_scope (pushed_scope);
25233 constructor_p = !cp_parser_error_occurred (parser);
25237 /* We did not really want to consume any tokens. */
25238 cp_parser_abort_tentative_parse (parser);
25240 return constructor_p;
25243 /* Parse the definition of the function given by the DECL_SPECIFIERS,
25244 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
25245 they must be performed once we are in the scope of the function.
25247 Returns the function defined. */
25249 static tree
25250 cp_parser_function_definition_from_specifiers_and_declarator
25251 (cp_parser* parser,
25252 cp_decl_specifier_seq *decl_specifiers,
25253 tree attributes,
25254 const cp_declarator *declarator)
25256 tree fn;
25257 bool success_p;
25259 /* Begin the function-definition. */
25260 success_p = start_function (decl_specifiers, declarator, attributes);
25262 /* The things we're about to see are not directly qualified by any
25263 template headers we've seen thus far. */
25264 reset_specialization ();
25266 /* If there were names looked up in the decl-specifier-seq that we
25267 did not check, check them now. We must wait until we are in the
25268 scope of the function to perform the checks, since the function
25269 might be a friend. */
25270 perform_deferred_access_checks (tf_warning_or_error);
25272 if (success_p)
25274 cp_finalize_omp_declare_simd (parser, current_function_decl);
25275 parser->omp_declare_simd = NULL;
25276 cp_finalize_oacc_routine (parser, current_function_decl, true);
25277 parser->oacc_routine = NULL;
25280 if (!success_p)
25282 /* Skip the entire function. */
25283 cp_parser_skip_to_end_of_block_or_statement (parser);
25284 fn = error_mark_node;
25286 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
25288 /* Seen already, skip it. An error message has already been output. */
25289 cp_parser_skip_to_end_of_block_or_statement (parser);
25290 fn = current_function_decl;
25291 current_function_decl = NULL_TREE;
25292 /* If this is a function from a class, pop the nested class. */
25293 if (current_class_name)
25294 pop_nested_class ();
25296 else
25298 timevar_id_t tv;
25299 if (DECL_DECLARED_INLINE_P (current_function_decl))
25300 tv = TV_PARSE_INLINE;
25301 else
25302 tv = TV_PARSE_FUNC;
25303 timevar_push (tv);
25304 fn = cp_parser_function_definition_after_declarator (parser,
25305 /*inline_p=*/false);
25306 timevar_pop (tv);
25309 return fn;
25312 /* Parse the part of a function-definition that follows the
25313 declarator. INLINE_P is TRUE iff this function is an inline
25314 function defined within a class-specifier.
25316 Returns the function defined. */
25318 static tree
25319 cp_parser_function_definition_after_declarator (cp_parser* parser,
25320 bool inline_p)
25322 tree fn;
25323 bool ctor_initializer_p = false;
25324 bool saved_in_unbraced_linkage_specification_p;
25325 bool saved_in_function_body;
25326 unsigned saved_num_template_parameter_lists;
25327 cp_token *token;
25328 bool fully_implicit_function_template_p
25329 = parser->fully_implicit_function_template_p;
25330 parser->fully_implicit_function_template_p = false;
25331 tree implicit_template_parms
25332 = parser->implicit_template_parms;
25333 parser->implicit_template_parms = 0;
25334 cp_binding_level* implicit_template_scope
25335 = parser->implicit_template_scope;
25336 parser->implicit_template_scope = 0;
25338 saved_in_function_body = parser->in_function_body;
25339 parser->in_function_body = true;
25340 /* If the next token is `return', then the code may be trying to
25341 make use of the "named return value" extension that G++ used to
25342 support. */
25343 token = cp_lexer_peek_token (parser->lexer);
25344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
25346 /* Consume the `return' keyword. */
25347 cp_lexer_consume_token (parser->lexer);
25348 /* Look for the identifier that indicates what value is to be
25349 returned. */
25350 cp_parser_identifier (parser);
25351 /* Issue an error message. */
25352 error_at (token->location,
25353 "named return values are no longer supported");
25354 /* Skip tokens until we reach the start of the function body. */
25355 while (true)
25357 cp_token *token = cp_lexer_peek_token (parser->lexer);
25358 if (token->type == CPP_OPEN_BRACE
25359 || token->type == CPP_EOF
25360 || token->type == CPP_PRAGMA_EOL)
25361 break;
25362 cp_lexer_consume_token (parser->lexer);
25365 /* The `extern' in `extern "C" void f () { ... }' does not apply to
25366 anything declared inside `f'. */
25367 saved_in_unbraced_linkage_specification_p
25368 = parser->in_unbraced_linkage_specification_p;
25369 parser->in_unbraced_linkage_specification_p = false;
25370 /* Inside the function, surrounding template-parameter-lists do not
25371 apply. */
25372 saved_num_template_parameter_lists
25373 = parser->num_template_parameter_lists;
25374 parser->num_template_parameter_lists = 0;
25376 start_lambda_scope (current_function_decl);
25378 /* If the next token is `try', `__transaction_atomic', or
25379 `__transaction_relaxed`, then we are looking at either function-try-block
25380 or function-transaction-block. Note that all of these include the
25381 function-body. */
25382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
25383 ctor_initializer_p = cp_parser_function_transaction (parser,
25384 RID_TRANSACTION_ATOMIC);
25385 else if (cp_lexer_next_token_is_keyword (parser->lexer,
25386 RID_TRANSACTION_RELAXED))
25387 ctor_initializer_p = cp_parser_function_transaction (parser,
25388 RID_TRANSACTION_RELAXED);
25389 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
25390 ctor_initializer_p = cp_parser_function_try_block (parser);
25391 else
25392 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
25393 (parser, /*in_function_try_block=*/false);
25395 finish_lambda_scope ();
25397 /* Finish the function. */
25398 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
25399 (inline_p ? 2 : 0));
25400 /* Generate code for it, if necessary. */
25401 expand_or_defer_fn (fn);
25402 /* Restore the saved values. */
25403 parser->in_unbraced_linkage_specification_p
25404 = saved_in_unbraced_linkage_specification_p;
25405 parser->num_template_parameter_lists
25406 = saved_num_template_parameter_lists;
25407 parser->in_function_body = saved_in_function_body;
25409 parser->fully_implicit_function_template_p
25410 = fully_implicit_function_template_p;
25411 parser->implicit_template_parms
25412 = implicit_template_parms;
25413 parser->implicit_template_scope
25414 = implicit_template_scope;
25416 if (parser->fully_implicit_function_template_p)
25417 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
25419 return fn;
25422 /* Parse a template-declaration body (following argument list). */
25424 static void
25425 cp_parser_template_declaration_after_parameters (cp_parser* parser,
25426 tree parameter_list,
25427 bool member_p)
25429 tree decl = NULL_TREE;
25430 bool friend_p = false;
25432 /* We just processed one more parameter list. */
25433 ++parser->num_template_parameter_lists;
25435 /* Get the deferred access checks from the parameter list. These
25436 will be checked once we know what is being declared, as for a
25437 member template the checks must be performed in the scope of the
25438 class containing the member. */
25439 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
25441 /* Tentatively parse for a new template parameter list, which can either be
25442 the template keyword or a template introduction. */
25443 if (cp_parser_template_declaration_after_export (parser, member_p))
25444 /* OK */;
25445 else if (cxx_dialect >= cxx11
25446 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25447 decl = cp_parser_alias_declaration (parser);
25448 else
25450 /* There are no access checks when parsing a template, as we do not
25451 know if a specialization will be a friend. */
25452 push_deferring_access_checks (dk_no_check);
25453 cp_token *token = cp_lexer_peek_token (parser->lexer);
25454 decl = cp_parser_single_declaration (parser,
25455 checks,
25456 member_p,
25457 /*explicit_specialization_p=*/false,
25458 &friend_p);
25459 pop_deferring_access_checks ();
25461 /* If this is a member template declaration, let the front
25462 end know. */
25463 if (member_p && !friend_p && decl)
25465 if (TREE_CODE (decl) == TYPE_DECL)
25466 cp_parser_check_access_in_redeclaration (decl, token->location);
25468 decl = finish_member_template_decl (decl);
25470 else if (friend_p && decl
25471 && DECL_DECLARES_TYPE_P (decl))
25472 make_friend_class (current_class_type, TREE_TYPE (decl),
25473 /*complain=*/true);
25475 /* We are done with the current parameter list. */
25476 --parser->num_template_parameter_lists;
25478 pop_deferring_access_checks ();
25480 /* Finish up. */
25481 finish_template_decl (parameter_list);
25483 /* Check the template arguments for a literal operator template. */
25484 if (decl
25485 && DECL_DECLARES_FUNCTION_P (decl)
25486 && UDLIT_OPER_P (DECL_NAME (decl)))
25488 bool ok = true;
25489 if (parameter_list == NULL_TREE)
25490 ok = false;
25491 else
25493 int num_parms = TREE_VEC_LENGTH (parameter_list);
25494 if (num_parms == 1)
25496 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
25497 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25498 if (TREE_TYPE (parm) != char_type_node
25499 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25500 ok = false;
25502 else if (num_parms == 2 && cxx_dialect >= cxx14)
25504 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
25505 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
25506 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
25507 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
25508 if (TREE_TYPE (parm) != TREE_TYPE (type)
25509 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
25510 ok = false;
25512 else
25513 ok = false;
25515 if (!ok)
25517 if (cxx_dialect >= cxx14)
25518 error ("literal operator template %qD has invalid parameter list."
25519 " Expected non-type template argument pack <char...>"
25520 " or <typename CharT, CharT...>",
25521 decl);
25522 else
25523 error ("literal operator template %qD has invalid parameter list."
25524 " Expected non-type template argument pack <char...>",
25525 decl);
25529 /* Register member declarations. */
25530 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
25531 finish_member_declaration (decl);
25532 /* If DECL is a function template, we must return to parse it later.
25533 (Even though there is no definition, there might be default
25534 arguments that need handling.) */
25535 if (member_p && decl
25536 && DECL_DECLARES_FUNCTION_P (decl))
25537 vec_safe_push (unparsed_funs_with_definitions, decl);
25540 /* Parse a template introduction header for a template-declaration. Returns
25541 false if tentative parse fails. */
25543 static bool
25544 cp_parser_template_introduction (cp_parser* parser, bool member_p)
25546 cp_parser_parse_tentatively (parser);
25548 tree saved_scope = parser->scope;
25549 tree saved_object_scope = parser->object_scope;
25550 tree saved_qualifying_scope = parser->qualifying_scope;
25552 /* Look for the optional `::' operator. */
25553 cp_parser_global_scope_opt (parser,
25554 /*current_scope_valid_p=*/false);
25555 /* Look for the nested-name-specifier. */
25556 cp_parser_nested_name_specifier_opt (parser,
25557 /*typename_keyword_p=*/false,
25558 /*check_dependency_p=*/true,
25559 /*type_p=*/false,
25560 /*is_declaration=*/false);
25562 cp_token *token = cp_lexer_peek_token (parser->lexer);
25563 tree concept_name = cp_parser_identifier (parser);
25565 /* Look up the concept for which we will be matching
25566 template parameters. */
25567 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
25568 token->location);
25569 parser->scope = saved_scope;
25570 parser->object_scope = saved_object_scope;
25571 parser->qualifying_scope = saved_qualifying_scope;
25573 if (concept_name == error_mark_node)
25574 cp_parser_simulate_error (parser);
25576 /* Look for opening brace for introduction. */
25577 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
25579 if (!cp_parser_parse_definitely (parser))
25580 return false;
25582 push_deferring_access_checks (dk_deferred);
25584 /* Build vector of placeholder parameters and grab
25585 matching identifiers. */
25586 tree introduction_list = cp_parser_introduction_list (parser);
25588 /* The introduction-list shall not be empty. */
25589 int nargs = TREE_VEC_LENGTH (introduction_list);
25590 if (nargs == 0)
25592 error ("empty introduction-list");
25593 return true;
25596 /* Look for closing brace for introduction. */
25597 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25598 return true;
25600 if (tmpl_decl == error_mark_node)
25602 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
25603 token->location);
25604 return true;
25607 /* Build and associate the constraint. */
25608 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
25609 if (parms && parms != error_mark_node)
25611 cp_parser_template_declaration_after_parameters (parser, parms,
25612 member_p);
25613 return true;
25616 error_at (token->location, "no matching concept for template-introduction");
25617 return true;
25620 /* Parse a normal template-declaration following the template keyword. */
25622 static void
25623 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
25625 tree parameter_list;
25626 bool need_lang_pop;
25627 location_t location = input_location;
25629 /* Look for the `<' token. */
25630 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
25631 return;
25632 if (at_class_scope_p () && current_function_decl)
25634 /* 14.5.2.2 [temp.mem]
25636 A local class shall not have member templates. */
25637 error_at (location,
25638 "invalid declaration of member template in local class");
25639 cp_parser_skip_to_end_of_block_or_statement (parser);
25640 return;
25642 /* [temp]
25644 A template ... shall not have C linkage. */
25645 if (current_lang_name == lang_name_c)
25647 error_at (location, "template with C linkage");
25648 /* Give it C++ linkage to avoid confusing other parts of the
25649 front end. */
25650 push_lang_context (lang_name_cplusplus);
25651 need_lang_pop = true;
25653 else
25654 need_lang_pop = false;
25656 /* We cannot perform access checks on the template parameter
25657 declarations until we know what is being declared, just as we
25658 cannot check the decl-specifier list. */
25659 push_deferring_access_checks (dk_deferred);
25661 /* If the next token is `>', then we have an invalid
25662 specialization. Rather than complain about an invalid template
25663 parameter, issue an error message here. */
25664 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
25666 cp_parser_error (parser, "invalid explicit specialization");
25667 begin_specialization ();
25668 parameter_list = NULL_TREE;
25670 else
25672 /* Parse the template parameters. */
25673 parameter_list = cp_parser_template_parameter_list (parser);
25676 /* Look for the `>'. */
25677 cp_parser_skip_to_end_of_template_parameter_list (parser);
25679 /* Manage template requirements */
25680 tree reqs = get_shorthand_constraints (current_template_parms);
25681 if (tree r = cp_parser_requires_clause_opt (parser))
25682 reqs = conjoin_constraints (reqs, make_predicate_constraint (r));
25683 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
25685 cp_parser_template_declaration_after_parameters (parser, parameter_list,
25686 member_p);
25688 /* For the erroneous case of a template with C linkage, we pushed an
25689 implicit C++ linkage scope; exit that scope now. */
25690 if (need_lang_pop)
25691 pop_lang_context ();
25694 /* Parse a template-declaration, assuming that the `export' (and
25695 `extern') keywords, if present, has already been scanned. MEMBER_P
25696 is as for cp_parser_template_declaration. */
25698 static bool
25699 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
25701 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25703 cp_lexer_consume_token (parser->lexer);
25704 cp_parser_explicit_template_declaration (parser, member_p);
25705 return true;
25707 else if (flag_concepts)
25708 return cp_parser_template_introduction (parser, member_p);
25710 return false;
25713 /* Perform the deferred access checks from a template-parameter-list.
25714 CHECKS is a TREE_LIST of access checks, as returned by
25715 get_deferred_access_checks. */
25717 static void
25718 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
25720 ++processing_template_parmlist;
25721 perform_access_checks (checks, tf_warning_or_error);
25722 --processing_template_parmlist;
25725 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
25726 `function-definition' sequence that follows a template header.
25727 If MEMBER_P is true, this declaration appears in a class scope.
25729 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
25730 *FRIEND_P is set to TRUE iff the declaration is a friend. */
25732 static tree
25733 cp_parser_single_declaration (cp_parser* parser,
25734 vec<deferred_access_check, va_gc> *checks,
25735 bool member_p,
25736 bool explicit_specialization_p,
25737 bool* friend_p)
25739 int declares_class_or_enum;
25740 tree decl = NULL_TREE;
25741 cp_decl_specifier_seq decl_specifiers;
25742 bool function_definition_p = false;
25743 cp_token *decl_spec_token_start;
25745 /* This function is only used when processing a template
25746 declaration. */
25747 gcc_assert (innermost_scope_kind () == sk_template_parms
25748 || innermost_scope_kind () == sk_template_spec);
25750 /* Defer access checks until we know what is being declared. */
25751 push_deferring_access_checks (dk_deferred);
25753 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
25754 alternative. */
25755 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
25756 cp_parser_decl_specifier_seq (parser,
25757 CP_PARSER_FLAGS_OPTIONAL,
25758 &decl_specifiers,
25759 &declares_class_or_enum);
25760 if (friend_p)
25761 *friend_p = cp_parser_friend_p (&decl_specifiers);
25763 /* There are no template typedefs. */
25764 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
25766 error_at (decl_spec_token_start->location,
25767 "template declaration of %<typedef%>");
25768 decl = error_mark_node;
25771 /* Gather up the access checks that occurred the
25772 decl-specifier-seq. */
25773 stop_deferring_access_checks ();
25775 /* Check for the declaration of a template class. */
25776 if (declares_class_or_enum)
25778 if (cp_parser_declares_only_class_p (parser)
25779 || (declares_class_or_enum & 2))
25781 // If this is a declaration, but not a definition, associate
25782 // any constraints with the type declaration. Constraints
25783 // are associated with definitions in cp_parser_class_specifier.
25784 if (declares_class_or_enum == 1)
25785 associate_classtype_constraints (decl_specifiers.type);
25787 decl = shadow_tag (&decl_specifiers);
25789 /* In this case:
25791 struct C {
25792 friend template <typename T> struct A<T>::B;
25795 A<T>::B will be represented by a TYPENAME_TYPE, and
25796 therefore not recognized by shadow_tag. */
25797 if (friend_p && *friend_p
25798 && !decl
25799 && decl_specifiers.type
25800 && TYPE_P (decl_specifiers.type))
25801 decl = decl_specifiers.type;
25803 if (decl && decl != error_mark_node)
25804 decl = TYPE_NAME (decl);
25805 else
25806 decl = error_mark_node;
25808 /* Perform access checks for template parameters. */
25809 cp_parser_perform_template_parameter_access_checks (checks);
25811 /* Give a helpful diagnostic for
25812 template <class T> struct A { } a;
25813 if we aren't already recovering from an error. */
25814 if (!cp_parser_declares_only_class_p (parser)
25815 && !seen_error ())
25817 error_at (cp_lexer_peek_token (parser->lexer)->location,
25818 "a class template declaration must not declare "
25819 "anything else");
25820 cp_parser_skip_to_end_of_block_or_statement (parser);
25821 goto out;
25826 /* Complain about missing 'typename' or other invalid type names. */
25827 if (!decl_specifiers.any_type_specifiers_p
25828 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
25830 /* cp_parser_parse_and_diagnose_invalid_type_name calls
25831 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
25832 the rest of this declaration. */
25833 decl = error_mark_node;
25834 goto out;
25837 /* If it's not a template class, try for a template function. If
25838 the next token is a `;', then this declaration does not declare
25839 anything. But, if there were errors in the decl-specifiers, then
25840 the error might well have come from an attempted class-specifier.
25841 In that case, there's no need to warn about a missing declarator. */
25842 if (!decl
25843 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
25844 || decl_specifiers.type != error_mark_node))
25846 decl = cp_parser_init_declarator (parser,
25847 &decl_specifiers,
25848 checks,
25849 /*function_definition_allowed_p=*/true,
25850 member_p,
25851 declares_class_or_enum,
25852 &function_definition_p,
25853 NULL, NULL, NULL);
25855 /* 7.1.1-1 [dcl.stc]
25857 A storage-class-specifier shall not be specified in an explicit
25858 specialization... */
25859 if (decl
25860 && explicit_specialization_p
25861 && decl_specifiers.storage_class != sc_none)
25863 error_at (decl_spec_token_start->location,
25864 "explicit template specialization cannot have a storage class");
25865 decl = error_mark_node;
25868 if (decl && VAR_P (decl))
25869 check_template_variable (decl);
25872 /* Look for a trailing `;' after the declaration. */
25873 if (!function_definition_p
25874 && (decl == error_mark_node
25875 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
25876 cp_parser_skip_to_end_of_block_or_statement (parser);
25878 out:
25879 pop_deferring_access_checks ();
25881 /* Clear any current qualification; whatever comes next is the start
25882 of something new. */
25883 parser->scope = NULL_TREE;
25884 parser->qualifying_scope = NULL_TREE;
25885 parser->object_scope = NULL_TREE;
25887 return decl;
25890 /* Parse a cast-expression that is not the operand of a unary "&". */
25892 static cp_expr
25893 cp_parser_simple_cast_expression (cp_parser *parser)
25895 return cp_parser_cast_expression (parser, /*address_p=*/false,
25896 /*cast_p=*/false, /*decltype*/false, NULL);
25899 /* Parse a functional cast to TYPE. Returns an expression
25900 representing the cast. */
25902 static cp_expr
25903 cp_parser_functional_cast (cp_parser* parser, tree type)
25905 vec<tree, va_gc> *vec;
25906 tree expression_list;
25907 cp_expr cast;
25908 bool nonconst_p;
25910 location_t start_loc = input_location;
25912 if (!type)
25913 type = error_mark_node;
25915 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25917 cp_lexer_set_source_position (parser->lexer);
25918 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
25919 expression_list = cp_parser_braced_list (parser, &nonconst_p);
25920 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
25921 if (TREE_CODE (type) == TYPE_DECL)
25922 type = TREE_TYPE (type);
25924 cast = finish_compound_literal (type, expression_list,
25925 tf_warning_or_error);
25926 /* Create a location of the form:
25927 type_name{i, f}
25928 ^~~~~~~~~~~~~~~
25929 with caret == start at the start of the type name,
25930 finishing at the closing brace. */
25931 location_t finish_loc
25932 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25933 location_t combined_loc = make_location (start_loc, start_loc,
25934 finish_loc);
25935 cast.set_location (combined_loc);
25936 return cast;
25940 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
25941 /*cast_p=*/true,
25942 /*allow_expansion_p=*/true,
25943 /*non_constant_p=*/NULL);
25944 if (vec == NULL)
25945 expression_list = error_mark_node;
25946 else
25948 expression_list = build_tree_list_vec (vec);
25949 release_tree_vector (vec);
25952 cast = build_functional_cast (type, expression_list,
25953 tf_warning_or_error);
25954 /* [expr.const]/1: In an integral constant expression "only type
25955 conversions to integral or enumeration type can be used". */
25956 if (TREE_CODE (type) == TYPE_DECL)
25957 type = TREE_TYPE (type);
25958 if (cast != error_mark_node
25959 && !cast_valid_in_integral_constant_expression_p (type)
25960 && cp_parser_non_integral_constant_expression (parser,
25961 NIC_CONSTRUCTOR))
25962 return error_mark_node;
25964 /* Create a location of the form:
25965 float(i)
25966 ^~~~~~~~
25967 with caret == start at the start of the type name,
25968 finishing at the closing paren. */
25969 location_t finish_loc
25970 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
25971 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
25972 cast.set_location (combined_loc);
25973 return cast;
25976 /* Save the tokens that make up the body of a member function defined
25977 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
25978 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
25979 specifiers applied to the declaration. Returns the FUNCTION_DECL
25980 for the member function. */
25982 static tree
25983 cp_parser_save_member_function_body (cp_parser* parser,
25984 cp_decl_specifier_seq *decl_specifiers,
25985 cp_declarator *declarator,
25986 tree attributes)
25988 cp_token *first;
25989 cp_token *last;
25990 tree fn;
25992 /* Create the FUNCTION_DECL. */
25993 fn = grokmethod (decl_specifiers, declarator, attributes);
25994 cp_finalize_omp_declare_simd (parser, fn);
25995 cp_finalize_oacc_routine (parser, fn, true);
25996 /* If something went badly wrong, bail out now. */
25997 if (fn == error_mark_node)
25999 /* If there's a function-body, skip it. */
26000 if (cp_parser_token_starts_function_definition_p
26001 (cp_lexer_peek_token (parser->lexer)))
26002 cp_parser_skip_to_end_of_block_or_statement (parser);
26003 return error_mark_node;
26006 /* Remember it, if there default args to post process. */
26007 cp_parser_save_default_args (parser, fn);
26009 /* Save away the tokens that make up the body of the
26010 function. */
26011 first = parser->lexer->next_token;
26012 /* Handle function try blocks. */
26013 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26014 cp_lexer_consume_token (parser->lexer);
26015 /* We can have braced-init-list mem-initializers before the fn body. */
26016 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26018 cp_lexer_consume_token (parser->lexer);
26019 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26021 /* cache_group will stop after an un-nested { } pair, too. */
26022 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26023 break;
26025 /* variadic mem-inits have ... after the ')'. */
26026 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26027 cp_lexer_consume_token (parser->lexer);
26030 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26031 /* Handle function try blocks. */
26032 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26033 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26034 last = parser->lexer->next_token;
26036 /* Save away the inline definition; we will process it when the
26037 class is complete. */
26038 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26039 DECL_PENDING_INLINE_P (fn) = 1;
26041 /* We need to know that this was defined in the class, so that
26042 friend templates are handled correctly. */
26043 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26045 /* Add FN to the queue of functions to be parsed later. */
26046 vec_safe_push (unparsed_funs_with_definitions, fn);
26048 return fn;
26051 /* Save the tokens that make up the in-class initializer for a non-static
26052 data member. Returns a DEFAULT_ARG. */
26054 static tree
26055 cp_parser_save_nsdmi (cp_parser* parser)
26057 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26060 /* Parse a template-argument-list, as well as the trailing ">" (but
26061 not the opening "<"). See cp_parser_template_argument_list for the
26062 return value. */
26064 static tree
26065 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26067 tree arguments;
26068 tree saved_scope;
26069 tree saved_qualifying_scope;
26070 tree saved_object_scope;
26071 bool saved_greater_than_is_operator_p;
26072 int saved_unevaluated_operand;
26073 int saved_inhibit_evaluation_warnings;
26075 /* [temp.names]
26077 When parsing a template-id, the first non-nested `>' is taken as
26078 the end of the template-argument-list rather than a greater-than
26079 operator. */
26080 saved_greater_than_is_operator_p
26081 = parser->greater_than_is_operator_p;
26082 parser->greater_than_is_operator_p = false;
26083 /* Parsing the argument list may modify SCOPE, so we save it
26084 here. */
26085 saved_scope = parser->scope;
26086 saved_qualifying_scope = parser->qualifying_scope;
26087 saved_object_scope = parser->object_scope;
26088 /* We need to evaluate the template arguments, even though this
26089 template-id may be nested within a "sizeof". */
26090 saved_unevaluated_operand = cp_unevaluated_operand;
26091 cp_unevaluated_operand = 0;
26092 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
26093 c_inhibit_evaluation_warnings = 0;
26094 /* Parse the template-argument-list itself. */
26095 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
26096 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26097 arguments = NULL_TREE;
26098 else
26099 arguments = cp_parser_template_argument_list (parser);
26100 /* Look for the `>' that ends the template-argument-list. If we find
26101 a '>>' instead, it's probably just a typo. */
26102 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
26104 if (cxx_dialect != cxx98)
26106 /* In C++0x, a `>>' in a template argument list or cast
26107 expression is considered to be two separate `>'
26108 tokens. So, change the current token to a `>', but don't
26109 consume it: it will be consumed later when the outer
26110 template argument list (or cast expression) is parsed.
26111 Note that this replacement of `>' for `>>' is necessary
26112 even if we are parsing tentatively: in the tentative
26113 case, after calling
26114 cp_parser_enclosed_template_argument_list we will always
26115 throw away all of the template arguments and the first
26116 closing `>', either because the template argument list
26117 was erroneous or because we are replacing those tokens
26118 with a CPP_TEMPLATE_ID token. The second `>' (which will
26119 not have been thrown away) is needed either to close an
26120 outer template argument list or to complete a new-style
26121 cast. */
26122 cp_token *token = cp_lexer_peek_token (parser->lexer);
26123 token->type = CPP_GREATER;
26125 else if (!saved_greater_than_is_operator_p)
26127 /* If we're in a nested template argument list, the '>>' has
26128 to be a typo for '> >'. We emit the error message, but we
26129 continue parsing and we push a '>' as next token, so that
26130 the argument list will be parsed correctly. Note that the
26131 global source location is still on the token before the
26132 '>>', so we need to say explicitly where we want it. */
26133 cp_token *token = cp_lexer_peek_token (parser->lexer);
26134 error_at (token->location, "%<>>%> should be %<> >%> "
26135 "within a nested template argument list");
26137 token->type = CPP_GREATER;
26139 else
26141 /* If this is not a nested template argument list, the '>>'
26142 is a typo for '>'. Emit an error message and continue.
26143 Same deal about the token location, but here we can get it
26144 right by consuming the '>>' before issuing the diagnostic. */
26145 cp_token *token = cp_lexer_consume_token (parser->lexer);
26146 error_at (token->location,
26147 "spurious %<>>%>, use %<>%> to terminate "
26148 "a template argument list");
26151 else
26152 cp_parser_skip_to_end_of_template_parameter_list (parser);
26153 /* The `>' token might be a greater-than operator again now. */
26154 parser->greater_than_is_operator_p
26155 = saved_greater_than_is_operator_p;
26156 /* Restore the SAVED_SCOPE. */
26157 parser->scope = saved_scope;
26158 parser->qualifying_scope = saved_qualifying_scope;
26159 parser->object_scope = saved_object_scope;
26160 cp_unevaluated_operand = saved_unevaluated_operand;
26161 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
26163 return arguments;
26166 /* MEMBER_FUNCTION is a member function, or a friend. If default
26167 arguments, or the body of the function have not yet been parsed,
26168 parse them now. */
26170 static void
26171 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
26173 timevar_push (TV_PARSE_INMETH);
26174 /* If this member is a template, get the underlying
26175 FUNCTION_DECL. */
26176 if (DECL_FUNCTION_TEMPLATE_P (member_function))
26177 member_function = DECL_TEMPLATE_RESULT (member_function);
26179 /* There should not be any class definitions in progress at this
26180 point; the bodies of members are only parsed outside of all class
26181 definitions. */
26182 gcc_assert (parser->num_classes_being_defined == 0);
26183 /* While we're parsing the member functions we might encounter more
26184 classes. We want to handle them right away, but we don't want
26185 them getting mixed up with functions that are currently in the
26186 queue. */
26187 push_unparsed_function_queues (parser);
26189 /* Make sure that any template parameters are in scope. */
26190 maybe_begin_member_template_processing (member_function);
26192 /* If the body of the function has not yet been parsed, parse it
26193 now. */
26194 if (DECL_PENDING_INLINE_P (member_function))
26196 tree function_scope;
26197 cp_token_cache *tokens;
26199 /* The function is no longer pending; we are processing it. */
26200 tokens = DECL_PENDING_INLINE_INFO (member_function);
26201 DECL_PENDING_INLINE_INFO (member_function) = NULL;
26202 DECL_PENDING_INLINE_P (member_function) = 0;
26204 /* If this is a local class, enter the scope of the containing
26205 function. */
26206 function_scope = current_function_decl;
26207 if (function_scope)
26208 push_function_context ();
26210 /* Push the body of the function onto the lexer stack. */
26211 cp_parser_push_lexer_for_tokens (parser, tokens);
26213 /* Let the front end know that we going to be defining this
26214 function. */
26215 start_preparsed_function (member_function, NULL_TREE,
26216 SF_PRE_PARSED | SF_INCLASS_INLINE);
26218 /* Don't do access checking if it is a templated function. */
26219 if (processing_template_decl)
26220 push_deferring_access_checks (dk_no_check);
26222 /* #pragma omp declare reduction needs special parsing. */
26223 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
26225 parser->lexer->in_pragma = true;
26226 cp_parser_omp_declare_reduction_exprs (member_function, parser);
26227 finish_function (/*inline*/2);
26228 cp_check_omp_declare_reduction (member_function);
26230 else
26231 /* Now, parse the body of the function. */
26232 cp_parser_function_definition_after_declarator (parser,
26233 /*inline_p=*/true);
26235 if (processing_template_decl)
26236 pop_deferring_access_checks ();
26238 /* Leave the scope of the containing function. */
26239 if (function_scope)
26240 pop_function_context ();
26241 cp_parser_pop_lexer (parser);
26244 /* Remove any template parameters from the symbol table. */
26245 maybe_end_member_template_processing ();
26247 /* Restore the queue. */
26248 pop_unparsed_function_queues (parser);
26249 timevar_pop (TV_PARSE_INMETH);
26252 /* If DECL contains any default args, remember it on the unparsed
26253 functions queue. */
26255 static void
26256 cp_parser_save_default_args (cp_parser* parser, tree decl)
26258 tree probe;
26260 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
26261 probe;
26262 probe = TREE_CHAIN (probe))
26263 if (TREE_PURPOSE (probe))
26265 cp_default_arg_entry entry = {current_class_type, decl};
26266 vec_safe_push (unparsed_funs_with_default_args, entry);
26267 break;
26271 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
26272 which is either a FIELD_DECL or PARM_DECL. Parse it and return
26273 the result. For a PARM_DECL, PARMTYPE is the corresponding type
26274 from the parameter-type-list. */
26276 static tree
26277 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
26278 tree default_arg, tree parmtype)
26280 cp_token_cache *tokens;
26281 tree parsed_arg;
26282 bool dummy;
26284 if (default_arg == error_mark_node)
26285 return error_mark_node;
26287 /* Push the saved tokens for the default argument onto the parser's
26288 lexer stack. */
26289 tokens = DEFARG_TOKENS (default_arg);
26290 cp_parser_push_lexer_for_tokens (parser, tokens);
26292 start_lambda_scope (decl);
26294 /* Parse the default argument. */
26295 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
26296 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
26297 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26299 finish_lambda_scope ();
26301 if (parsed_arg == error_mark_node)
26302 cp_parser_skip_to_end_of_statement (parser);
26304 if (!processing_template_decl)
26306 /* In a non-template class, check conversions now. In a template,
26307 we'll wait and instantiate these as needed. */
26308 if (TREE_CODE (decl) == PARM_DECL)
26309 parsed_arg = check_default_argument (parmtype, parsed_arg,
26310 tf_warning_or_error);
26311 else
26312 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
26315 /* If the token stream has not been completely used up, then
26316 there was extra junk after the end of the default
26317 argument. */
26318 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26320 if (TREE_CODE (decl) == PARM_DECL)
26321 cp_parser_error (parser, "expected %<,%>");
26322 else
26323 cp_parser_error (parser, "expected %<;%>");
26326 /* Revert to the main lexer. */
26327 cp_parser_pop_lexer (parser);
26329 return parsed_arg;
26332 /* FIELD is a non-static data member with an initializer which we saved for
26333 later; parse it now. */
26335 static void
26336 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
26338 tree def;
26340 maybe_begin_member_template_processing (field);
26342 push_unparsed_function_queues (parser);
26343 def = cp_parser_late_parse_one_default_arg (parser, field,
26344 DECL_INITIAL (field),
26345 NULL_TREE);
26346 pop_unparsed_function_queues (parser);
26348 maybe_end_member_template_processing ();
26350 DECL_INITIAL (field) = def;
26353 /* FN is a FUNCTION_DECL which may contains a parameter with an
26354 unparsed DEFAULT_ARG. Parse the default args now. This function
26355 assumes that the current scope is the scope in which the default
26356 argument should be processed. */
26358 static void
26359 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
26361 bool saved_local_variables_forbidden_p;
26362 tree parm, parmdecl;
26364 /* While we're parsing the default args, we might (due to the
26365 statement expression extension) encounter more classes. We want
26366 to handle them right away, but we don't want them getting mixed
26367 up with default args that are currently in the queue. */
26368 push_unparsed_function_queues (parser);
26370 /* Local variable names (and the `this' keyword) may not appear
26371 in a default argument. */
26372 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
26373 parser->local_variables_forbidden_p = true;
26375 push_defarg_context (fn);
26377 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
26378 parmdecl = DECL_ARGUMENTS (fn);
26379 parm && parm != void_list_node;
26380 parm = TREE_CHAIN (parm),
26381 parmdecl = DECL_CHAIN (parmdecl))
26383 tree default_arg = TREE_PURPOSE (parm);
26384 tree parsed_arg;
26385 vec<tree, va_gc> *insts;
26386 tree copy;
26387 unsigned ix;
26389 if (!default_arg)
26390 continue;
26392 if (TREE_CODE (default_arg) != DEFAULT_ARG)
26393 /* This can happen for a friend declaration for a function
26394 already declared with default arguments. */
26395 continue;
26397 parsed_arg
26398 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
26399 default_arg,
26400 TREE_VALUE (parm));
26401 if (parsed_arg == error_mark_node)
26403 continue;
26406 TREE_PURPOSE (parm) = parsed_arg;
26408 /* Update any instantiations we've already created. */
26409 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
26410 vec_safe_iterate (insts, ix, &copy); ix++)
26411 TREE_PURPOSE (copy) = parsed_arg;
26414 pop_defarg_context ();
26416 /* Make sure no default arg is missing. */
26417 check_default_args (fn);
26419 /* Restore the state of local_variables_forbidden_p. */
26420 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
26422 /* Restore the queue. */
26423 pop_unparsed_function_queues (parser);
26426 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
26428 sizeof ... ( identifier )
26430 where the 'sizeof' token has already been consumed. */
26432 static tree
26433 cp_parser_sizeof_pack (cp_parser *parser)
26435 /* Consume the `...'. */
26436 cp_lexer_consume_token (parser->lexer);
26437 maybe_warn_variadic_templates ();
26439 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
26440 if (paren)
26441 cp_lexer_consume_token (parser->lexer);
26442 else
26443 permerror (cp_lexer_peek_token (parser->lexer)->location,
26444 "%<sizeof...%> argument must be surrounded by parentheses");
26446 cp_token *token = cp_lexer_peek_token (parser->lexer);
26447 tree name = cp_parser_identifier (parser);
26448 if (name == error_mark_node)
26449 return error_mark_node;
26450 /* The name is not qualified. */
26451 parser->scope = NULL_TREE;
26452 parser->qualifying_scope = NULL_TREE;
26453 parser->object_scope = NULL_TREE;
26454 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
26455 if (expr == error_mark_node)
26456 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
26457 token->location);
26458 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
26459 expr = TREE_TYPE (expr);
26460 else if (TREE_CODE (expr) == CONST_DECL)
26461 expr = DECL_INITIAL (expr);
26462 expr = make_pack_expansion (expr);
26463 PACK_EXPANSION_SIZEOF_P (expr) = true;
26465 if (paren)
26466 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26468 return expr;
26471 /* Parse the operand of `sizeof' (or a similar operator). Returns
26472 either a TYPE or an expression, depending on the form of the
26473 input. The KEYWORD indicates which kind of expression we have
26474 encountered. */
26476 static tree
26477 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
26479 tree expr = NULL_TREE;
26480 const char *saved_message;
26481 char *tmp;
26482 bool saved_integral_constant_expression_p;
26483 bool saved_non_integral_constant_expression_p;
26485 /* If it's a `...', then we are computing the length of a parameter
26486 pack. */
26487 if (keyword == RID_SIZEOF
26488 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26489 return cp_parser_sizeof_pack (parser);
26491 /* Types cannot be defined in a `sizeof' expression. Save away the
26492 old message. */
26493 saved_message = parser->type_definition_forbidden_message;
26494 /* And create the new one. */
26495 tmp = concat ("types may not be defined in %<",
26496 IDENTIFIER_POINTER (ridpointers[keyword]),
26497 "%> expressions", NULL);
26498 parser->type_definition_forbidden_message = tmp;
26500 /* The restrictions on constant-expressions do not apply inside
26501 sizeof expressions. */
26502 saved_integral_constant_expression_p
26503 = parser->integral_constant_expression_p;
26504 saved_non_integral_constant_expression_p
26505 = parser->non_integral_constant_expression_p;
26506 parser->integral_constant_expression_p = false;
26508 /* Do not actually evaluate the expression. */
26509 ++cp_unevaluated_operand;
26510 ++c_inhibit_evaluation_warnings;
26511 /* If it's a `(', then we might be looking at the type-id
26512 construction. */
26513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26515 tree type = NULL_TREE;
26517 /* We can't be sure yet whether we're looking at a type-id or an
26518 expression. */
26519 cp_parser_parse_tentatively (parser);
26520 /* Note: as a GNU Extension, compound literals are considered
26521 postfix-expressions as they are in C99, so they are valid
26522 arguments to sizeof. See comment in cp_parser_cast_expression
26523 for details. */
26524 if (cp_parser_compound_literal_p (parser))
26525 cp_parser_simulate_error (parser);
26526 else
26528 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
26529 parser->in_type_id_in_expr_p = true;
26530 /* Look for the type-id. */
26531 type = cp_parser_type_id (parser);
26532 /* Look for the closing `)'. */
26533 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26534 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
26537 /* If all went well, then we're done. */
26538 if (cp_parser_parse_definitely (parser))
26540 cp_decl_specifier_seq decl_specs;
26542 /* Build a trivial decl-specifier-seq. */
26543 clear_decl_specs (&decl_specs);
26544 decl_specs.type = type;
26546 /* Call grokdeclarator to figure out what type this is. */
26547 expr = grokdeclarator (NULL,
26548 &decl_specs,
26549 TYPENAME,
26550 /*initialized=*/0,
26551 /*attrlist=*/NULL);
26555 /* If the type-id production did not work out, then we must be
26556 looking at the unary-expression production. */
26557 if (!expr)
26558 expr = cp_parser_unary_expression (parser);
26560 /* Go back to evaluating expressions. */
26561 --cp_unevaluated_operand;
26562 --c_inhibit_evaluation_warnings;
26564 /* Free the message we created. */
26565 free (tmp);
26566 /* And restore the old one. */
26567 parser->type_definition_forbidden_message = saved_message;
26568 parser->integral_constant_expression_p
26569 = saved_integral_constant_expression_p;
26570 parser->non_integral_constant_expression_p
26571 = saved_non_integral_constant_expression_p;
26573 return expr;
26576 /* If the current declaration has no declarator, return true. */
26578 static bool
26579 cp_parser_declares_only_class_p (cp_parser *parser)
26581 /* If the next token is a `;' or a `,' then there is no
26582 declarator. */
26583 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
26584 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
26587 /* Update the DECL_SPECS to reflect the storage class indicated by
26588 KEYWORD. */
26590 static void
26591 cp_parser_set_storage_class (cp_parser *parser,
26592 cp_decl_specifier_seq *decl_specs,
26593 enum rid keyword,
26594 cp_token *token)
26596 cp_storage_class storage_class;
26598 if (parser->in_unbraced_linkage_specification_p)
26600 error_at (token->location, "invalid use of %qD in linkage specification",
26601 ridpointers[keyword]);
26602 return;
26604 else if (decl_specs->storage_class != sc_none)
26606 decl_specs->conflicting_specifiers_p = true;
26607 return;
26610 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
26611 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
26612 && decl_specs->gnu_thread_keyword_p)
26614 pedwarn (decl_specs->locations[ds_thread], 0,
26615 "%<__thread%> before %qD", ridpointers[keyword]);
26618 switch (keyword)
26620 case RID_AUTO:
26621 storage_class = sc_auto;
26622 break;
26623 case RID_REGISTER:
26624 storage_class = sc_register;
26625 break;
26626 case RID_STATIC:
26627 storage_class = sc_static;
26628 break;
26629 case RID_EXTERN:
26630 storage_class = sc_extern;
26631 break;
26632 case RID_MUTABLE:
26633 storage_class = sc_mutable;
26634 break;
26635 default:
26636 gcc_unreachable ();
26638 decl_specs->storage_class = storage_class;
26639 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
26641 /* A storage class specifier cannot be applied alongside a typedef
26642 specifier. If there is a typedef specifier present then set
26643 conflicting_specifiers_p which will trigger an error later
26644 on in grokdeclarator. */
26645 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
26646 decl_specs->conflicting_specifiers_p = true;
26649 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
26650 is true, the type is a class or enum definition. */
26652 static void
26653 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
26654 tree type_spec,
26655 cp_token *token,
26656 bool type_definition_p)
26658 decl_specs->any_specifiers_p = true;
26660 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
26661 (with, for example, in "typedef int wchar_t;") we remember that
26662 this is what happened. In system headers, we ignore these
26663 declarations so that G++ can work with system headers that are not
26664 C++-safe. */
26665 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
26666 && !type_definition_p
26667 && (type_spec == boolean_type_node
26668 || type_spec == char16_type_node
26669 || type_spec == char32_type_node
26670 || type_spec == wchar_type_node)
26671 && (decl_specs->type
26672 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
26673 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
26674 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
26675 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
26677 decl_specs->redefined_builtin_type = type_spec;
26678 set_and_check_decl_spec_loc (decl_specs,
26679 ds_redefined_builtin_type_spec,
26680 token);
26681 if (!decl_specs->type)
26683 decl_specs->type = type_spec;
26684 decl_specs->type_definition_p = false;
26685 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
26688 else if (decl_specs->type)
26689 decl_specs->multiple_types_p = true;
26690 else
26692 decl_specs->type = type_spec;
26693 decl_specs->type_definition_p = type_definition_p;
26694 decl_specs->redefined_builtin_type = NULL_TREE;
26695 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
26699 /* True iff TOKEN is the GNU keyword __thread. */
26701 static bool
26702 token_is__thread (cp_token *token)
26704 gcc_assert (token->keyword == RID_THREAD);
26705 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
26708 /* Set the location for a declarator specifier and check if it is
26709 duplicated.
26711 DECL_SPECS is the sequence of declarator specifiers onto which to
26712 set the location.
26714 DS is the single declarator specifier to set which location is to
26715 be set onto the existing sequence of declarators.
26717 LOCATION is the location for the declarator specifier to
26718 consider. */
26720 static void
26721 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
26722 cp_decl_spec ds, cp_token *token)
26724 gcc_assert (ds < ds_last);
26726 if (decl_specs == NULL)
26727 return;
26729 source_location location = token->location;
26731 if (decl_specs->locations[ds] == 0)
26733 decl_specs->locations[ds] = location;
26734 if (ds == ds_thread)
26735 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
26737 else
26739 if (ds == ds_long)
26741 if (decl_specs->locations[ds_long_long] != 0)
26742 error_at (location,
26743 "%<long long long%> is too long for GCC");
26744 else
26746 decl_specs->locations[ds_long_long] = location;
26747 pedwarn_cxx98 (location,
26748 OPT_Wlong_long,
26749 "ISO C++ 1998 does not support %<long long%>");
26752 else if (ds == ds_thread)
26754 bool gnu = token_is__thread (token);
26755 if (gnu != decl_specs->gnu_thread_keyword_p)
26756 error_at (location,
26757 "both %<__thread%> and %<thread_local%> specified");
26758 else
26759 error_at (location, "duplicate %qD", token->u.value);
26761 else
26763 static const char *const decl_spec_names[] = {
26764 "signed",
26765 "unsigned",
26766 "short",
26767 "long",
26768 "const",
26769 "volatile",
26770 "restrict",
26771 "inline",
26772 "virtual",
26773 "explicit",
26774 "friend",
26775 "typedef",
26776 "using",
26777 "constexpr",
26778 "__complex"
26780 error_at (location,
26781 "duplicate %qs", decl_spec_names[ds]);
26786 /* Return true iff the declarator specifier DS is present in the
26787 sequence of declarator specifiers DECL_SPECS. */
26789 bool
26790 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
26791 cp_decl_spec ds)
26793 gcc_assert (ds < ds_last);
26795 if (decl_specs == NULL)
26796 return false;
26798 return decl_specs->locations[ds] != 0;
26801 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
26802 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
26804 static bool
26805 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
26807 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
26810 /* Issue an error message indicating that TOKEN_DESC was expected.
26811 If KEYWORD is true, it indicated this function is called by
26812 cp_parser_require_keword and the required token can only be
26813 a indicated keyword. */
26815 static void
26816 cp_parser_required_error (cp_parser *parser,
26817 required_token token_desc,
26818 bool keyword)
26820 switch (token_desc)
26822 case RT_NEW:
26823 cp_parser_error (parser, "expected %<new%>");
26824 return;
26825 case RT_DELETE:
26826 cp_parser_error (parser, "expected %<delete%>");
26827 return;
26828 case RT_RETURN:
26829 cp_parser_error (parser, "expected %<return%>");
26830 return;
26831 case RT_WHILE:
26832 cp_parser_error (parser, "expected %<while%>");
26833 return;
26834 case RT_EXTERN:
26835 cp_parser_error (parser, "expected %<extern%>");
26836 return;
26837 case RT_STATIC_ASSERT:
26838 cp_parser_error (parser, "expected %<static_assert%>");
26839 return;
26840 case RT_DECLTYPE:
26841 cp_parser_error (parser, "expected %<decltype%>");
26842 return;
26843 case RT_OPERATOR:
26844 cp_parser_error (parser, "expected %<operator%>");
26845 return;
26846 case RT_CLASS:
26847 cp_parser_error (parser, "expected %<class%>");
26848 return;
26849 case RT_TEMPLATE:
26850 cp_parser_error (parser, "expected %<template%>");
26851 return;
26852 case RT_NAMESPACE:
26853 cp_parser_error (parser, "expected %<namespace%>");
26854 return;
26855 case RT_USING:
26856 cp_parser_error (parser, "expected %<using%>");
26857 return;
26858 case RT_ASM:
26859 cp_parser_error (parser, "expected %<asm%>");
26860 return;
26861 case RT_TRY:
26862 cp_parser_error (parser, "expected %<try%>");
26863 return;
26864 case RT_CATCH:
26865 cp_parser_error (parser, "expected %<catch%>");
26866 return;
26867 case RT_THROW:
26868 cp_parser_error (parser, "expected %<throw%>");
26869 return;
26870 case RT_LABEL:
26871 cp_parser_error (parser, "expected %<__label__%>");
26872 return;
26873 case RT_AT_TRY:
26874 cp_parser_error (parser, "expected %<@try%>");
26875 return;
26876 case RT_AT_SYNCHRONIZED:
26877 cp_parser_error (parser, "expected %<@synchronized%>");
26878 return;
26879 case RT_AT_THROW:
26880 cp_parser_error (parser, "expected %<@throw%>");
26881 return;
26882 case RT_TRANSACTION_ATOMIC:
26883 cp_parser_error (parser, "expected %<__transaction_atomic%>");
26884 return;
26885 case RT_TRANSACTION_RELAXED:
26886 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
26887 return;
26888 default:
26889 break;
26891 if (!keyword)
26893 switch (token_desc)
26895 case RT_SEMICOLON:
26896 cp_parser_error (parser, "expected %<;%>");
26897 return;
26898 case RT_OPEN_PAREN:
26899 cp_parser_error (parser, "expected %<(%>");
26900 return;
26901 case RT_CLOSE_BRACE:
26902 cp_parser_error (parser, "expected %<}%>");
26903 return;
26904 case RT_OPEN_BRACE:
26905 cp_parser_error (parser, "expected %<{%>");
26906 return;
26907 case RT_CLOSE_SQUARE:
26908 cp_parser_error (parser, "expected %<]%>");
26909 return;
26910 case RT_OPEN_SQUARE:
26911 cp_parser_error (parser, "expected %<[%>");
26912 return;
26913 case RT_COMMA:
26914 cp_parser_error (parser, "expected %<,%>");
26915 return;
26916 case RT_SCOPE:
26917 cp_parser_error (parser, "expected %<::%>");
26918 return;
26919 case RT_LESS:
26920 cp_parser_error (parser, "expected %<<%>");
26921 return;
26922 case RT_GREATER:
26923 cp_parser_error (parser, "expected %<>%>");
26924 return;
26925 case RT_EQ:
26926 cp_parser_error (parser, "expected %<=%>");
26927 return;
26928 case RT_ELLIPSIS:
26929 cp_parser_error (parser, "expected %<...%>");
26930 return;
26931 case RT_MULT:
26932 cp_parser_error (parser, "expected %<*%>");
26933 return;
26934 case RT_COMPL:
26935 cp_parser_error (parser, "expected %<~%>");
26936 return;
26937 case RT_COLON:
26938 cp_parser_error (parser, "expected %<:%>");
26939 return;
26940 case RT_COLON_SCOPE:
26941 cp_parser_error (parser, "expected %<:%> or %<::%>");
26942 return;
26943 case RT_CLOSE_PAREN:
26944 cp_parser_error (parser, "expected %<)%>");
26945 return;
26946 case RT_COMMA_CLOSE_PAREN:
26947 cp_parser_error (parser, "expected %<,%> or %<)%>");
26948 return;
26949 case RT_PRAGMA_EOL:
26950 cp_parser_error (parser, "expected end of line");
26951 return;
26952 case RT_NAME:
26953 cp_parser_error (parser, "expected identifier");
26954 return;
26955 case RT_SELECT:
26956 cp_parser_error (parser, "expected selection-statement");
26957 return;
26958 case RT_INTERATION:
26959 cp_parser_error (parser, "expected iteration-statement");
26960 return;
26961 case RT_JUMP:
26962 cp_parser_error (parser, "expected jump-statement");
26963 return;
26964 case RT_CLASS_KEY:
26965 cp_parser_error (parser, "expected class-key");
26966 return;
26967 case RT_CLASS_TYPENAME_TEMPLATE:
26968 cp_parser_error (parser,
26969 "expected %<class%>, %<typename%>, or %<template%>");
26970 return;
26971 default:
26972 gcc_unreachable ();
26975 else
26976 gcc_unreachable ();
26981 /* If the next token is of the indicated TYPE, consume it. Otherwise,
26982 issue an error message indicating that TOKEN_DESC was expected.
26984 Returns the token consumed, if the token had the appropriate type.
26985 Otherwise, returns NULL. */
26987 static cp_token *
26988 cp_parser_require (cp_parser* parser,
26989 enum cpp_ttype type,
26990 required_token token_desc)
26992 if (cp_lexer_next_token_is (parser->lexer, type))
26993 return cp_lexer_consume_token (parser->lexer);
26994 else
26996 /* Output the MESSAGE -- unless we're parsing tentatively. */
26997 if (!cp_parser_simulate_error (parser))
26998 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
26999 return NULL;
27003 /* An error message is produced if the next token is not '>'.
27004 All further tokens are skipped until the desired token is
27005 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27007 static void
27008 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27010 /* Current level of '< ... >'. */
27011 unsigned level = 0;
27012 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27013 unsigned nesting_depth = 0;
27015 /* Are we ready, yet? If not, issue error message. */
27016 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27017 return;
27019 /* Skip tokens until the desired token is found. */
27020 while (true)
27022 /* Peek at the next token. */
27023 switch (cp_lexer_peek_token (parser->lexer)->type)
27025 case CPP_LESS:
27026 if (!nesting_depth)
27027 ++level;
27028 break;
27030 case CPP_RSHIFT:
27031 if (cxx_dialect == cxx98)
27032 /* C++0x views the `>>' operator as two `>' tokens, but
27033 C++98 does not. */
27034 break;
27035 else if (!nesting_depth && level-- == 0)
27037 /* We've hit a `>>' where the first `>' closes the
27038 template argument list, and the second `>' is
27039 spurious. Just consume the `>>' and stop; we've
27040 already produced at least one error. */
27041 cp_lexer_consume_token (parser->lexer);
27042 return;
27044 /* Fall through for C++0x, so we handle the second `>' in
27045 the `>>'. */
27047 case CPP_GREATER:
27048 if (!nesting_depth && level-- == 0)
27050 /* We've reached the token we want, consume it and stop. */
27051 cp_lexer_consume_token (parser->lexer);
27052 return;
27054 break;
27056 case CPP_OPEN_PAREN:
27057 case CPP_OPEN_SQUARE:
27058 ++nesting_depth;
27059 break;
27061 case CPP_CLOSE_PAREN:
27062 case CPP_CLOSE_SQUARE:
27063 if (nesting_depth-- == 0)
27064 return;
27065 break;
27067 case CPP_EOF:
27068 case CPP_PRAGMA_EOL:
27069 case CPP_SEMICOLON:
27070 case CPP_OPEN_BRACE:
27071 case CPP_CLOSE_BRACE:
27072 /* The '>' was probably forgotten, don't look further. */
27073 return;
27075 default:
27076 break;
27079 /* Consume this token. */
27080 cp_lexer_consume_token (parser->lexer);
27084 /* If the next token is the indicated keyword, consume it. Otherwise,
27085 issue an error message indicating that TOKEN_DESC was expected.
27087 Returns the token consumed, if the token had the appropriate type.
27088 Otherwise, returns NULL. */
27090 static cp_token *
27091 cp_parser_require_keyword (cp_parser* parser,
27092 enum rid keyword,
27093 required_token token_desc)
27095 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
27097 if (token && token->keyword != keyword)
27099 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
27100 return NULL;
27103 return token;
27106 /* Returns TRUE iff TOKEN is a token that can begin the body of a
27107 function-definition. */
27109 static bool
27110 cp_parser_token_starts_function_definition_p (cp_token* token)
27112 return (/* An ordinary function-body begins with an `{'. */
27113 token->type == CPP_OPEN_BRACE
27114 /* A ctor-initializer begins with a `:'. */
27115 || token->type == CPP_COLON
27116 /* A function-try-block begins with `try'. */
27117 || token->keyword == RID_TRY
27118 /* A function-transaction-block begins with `__transaction_atomic'
27119 or `__transaction_relaxed'. */
27120 || token->keyword == RID_TRANSACTION_ATOMIC
27121 || token->keyword == RID_TRANSACTION_RELAXED
27122 /* The named return value extension begins with `return'. */
27123 || token->keyword == RID_RETURN);
27126 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
27127 definition. */
27129 static bool
27130 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
27132 cp_token *token;
27134 token = cp_lexer_peek_token (parser->lexer);
27135 return (token->type == CPP_OPEN_BRACE
27136 || (token->type == CPP_COLON
27137 && !parser->colon_doesnt_start_class_def_p));
27140 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
27141 C++0x) ending a template-argument. */
27143 static bool
27144 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
27146 cp_token *token;
27148 token = cp_lexer_peek_token (parser->lexer);
27149 return (token->type == CPP_COMMA
27150 || token->type == CPP_GREATER
27151 || token->type == CPP_ELLIPSIS
27152 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
27155 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
27156 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
27158 static bool
27159 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
27160 size_t n)
27162 cp_token *token;
27164 token = cp_lexer_peek_nth_token (parser->lexer, n);
27165 if (token->type == CPP_LESS)
27166 return true;
27167 /* Check for the sequence `<::' in the original code. It would be lexed as
27168 `[:', where `[' is a digraph, and there is no whitespace before
27169 `:'. */
27170 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
27172 cp_token *token2;
27173 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
27174 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
27175 return true;
27177 return false;
27180 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
27181 or none_type otherwise. */
27183 static enum tag_types
27184 cp_parser_token_is_class_key (cp_token* token)
27186 switch (token->keyword)
27188 case RID_CLASS:
27189 return class_type;
27190 case RID_STRUCT:
27191 return record_type;
27192 case RID_UNION:
27193 return union_type;
27195 default:
27196 return none_type;
27200 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
27201 or none_type otherwise or if the token is null. */
27203 static enum tag_types
27204 cp_parser_token_is_type_parameter_key (cp_token* token)
27206 if (!token)
27207 return none_type;
27209 switch (token->keyword)
27211 case RID_CLASS:
27212 return class_type;
27213 case RID_TYPENAME:
27214 return typename_type;
27216 default:
27217 return none_type;
27221 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
27223 static void
27224 cp_parser_check_class_key (enum tag_types class_key, tree type)
27226 if (type == error_mark_node)
27227 return;
27228 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
27230 if (permerror (input_location, "%qs tag used in naming %q#T",
27231 class_key == union_type ? "union"
27232 : class_key == record_type ? "struct" : "class",
27233 type))
27234 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
27235 "%q#T was previously declared here", type);
27239 /* Issue an error message if DECL is redeclared with different
27240 access than its original declaration [class.access.spec/3].
27241 This applies to nested classes, nested class templates and
27242 enumerations [class.mem/1]. */
27244 static void
27245 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
27247 if (!decl
27248 || (!CLASS_TYPE_P (TREE_TYPE (decl))
27249 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
27250 return;
27252 if ((TREE_PRIVATE (decl)
27253 != (current_access_specifier == access_private_node))
27254 || (TREE_PROTECTED (decl)
27255 != (current_access_specifier == access_protected_node)))
27256 error_at (location, "%qD redeclared with different access", decl);
27259 /* Look for the `template' keyword, as a syntactic disambiguator.
27260 Return TRUE iff it is present, in which case it will be
27261 consumed. */
27263 static bool
27264 cp_parser_optional_template_keyword (cp_parser *parser)
27266 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
27268 /* In C++98 the `template' keyword can only be used within templates;
27269 outside templates the parser can always figure out what is a
27270 template and what is not. In C++11, per the resolution of DR 468,
27271 `template' is allowed in cases where it is not strictly necessary. */
27272 if (!processing_template_decl
27273 && pedantic && cxx_dialect == cxx98)
27275 cp_token *token = cp_lexer_peek_token (parser->lexer);
27276 pedwarn (token->location, OPT_Wpedantic,
27277 "in C++98 %<template%> (as a disambiguator) is only "
27278 "allowed within templates");
27279 /* If this part of the token stream is rescanned, the same
27280 error message would be generated. So, we purge the token
27281 from the stream. */
27282 cp_lexer_purge_token (parser->lexer);
27283 return false;
27285 else
27287 /* Consume the `template' keyword. */
27288 cp_lexer_consume_token (parser->lexer);
27289 return true;
27292 return false;
27295 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
27296 set PARSER->SCOPE, and perform other related actions. */
27298 static void
27299 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
27301 struct tree_check *check_value;
27303 /* Get the stored value. */
27304 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
27305 /* Set the scope from the stored value. */
27306 parser->scope = saved_checks_value (check_value);
27307 parser->qualifying_scope = check_value->qualifying_scope;
27308 parser->object_scope = NULL_TREE;
27311 /* Consume tokens up through a non-nested END token. Returns TRUE if we
27312 encounter the end of a block before what we were looking for. */
27314 static bool
27315 cp_parser_cache_group (cp_parser *parser,
27316 enum cpp_ttype end,
27317 unsigned depth)
27319 while (true)
27321 cp_token *token = cp_lexer_peek_token (parser->lexer);
27323 /* Abort a parenthesized expression if we encounter a semicolon. */
27324 if ((end == CPP_CLOSE_PAREN || depth == 0)
27325 && token->type == CPP_SEMICOLON)
27326 return true;
27327 /* If we've reached the end of the file, stop. */
27328 if (token->type == CPP_EOF
27329 || (end != CPP_PRAGMA_EOL
27330 && token->type == CPP_PRAGMA_EOL))
27331 return true;
27332 if (token->type == CPP_CLOSE_BRACE && depth == 0)
27333 /* We've hit the end of an enclosing block, so there's been some
27334 kind of syntax error. */
27335 return true;
27337 /* Consume the token. */
27338 cp_lexer_consume_token (parser->lexer);
27339 /* See if it starts a new group. */
27340 if (token->type == CPP_OPEN_BRACE)
27342 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
27343 /* In theory this should probably check end == '}', but
27344 cp_parser_save_member_function_body needs it to exit
27345 after either '}' or ')' when called with ')'. */
27346 if (depth == 0)
27347 return false;
27349 else if (token->type == CPP_OPEN_PAREN)
27351 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
27352 if (depth == 0 && end == CPP_CLOSE_PAREN)
27353 return false;
27355 else if (token->type == CPP_PRAGMA)
27356 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
27357 else if (token->type == end)
27358 return false;
27362 /* Like above, for caching a default argument or NSDMI. Both of these are
27363 terminated by a non-nested comma, but it can be unclear whether or not a
27364 comma is nested in a template argument list unless we do more parsing.
27365 In order to handle this ambiguity, when we encounter a ',' after a '<'
27366 we try to parse what follows as a parameter-declaration-list (in the
27367 case of a default argument) or a member-declarator (in the case of an
27368 NSDMI). If that succeeds, then we stop caching. */
27370 static tree
27371 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
27373 unsigned depth = 0;
27374 int maybe_template_id = 0;
27375 cp_token *first_token;
27376 cp_token *token;
27377 tree default_argument;
27379 /* Add tokens until we have processed the entire default
27380 argument. We add the range [first_token, token). */
27381 first_token = cp_lexer_peek_token (parser->lexer);
27382 if (first_token->type == CPP_OPEN_BRACE)
27384 /* For list-initialization, this is straightforward. */
27385 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27386 token = cp_lexer_peek_token (parser->lexer);
27388 else while (true)
27390 bool done = false;
27392 /* Peek at the next token. */
27393 token = cp_lexer_peek_token (parser->lexer);
27394 /* What we do depends on what token we have. */
27395 switch (token->type)
27397 /* In valid code, a default argument must be
27398 immediately followed by a `,' `)', or `...'. */
27399 case CPP_COMMA:
27400 if (depth == 0 && maybe_template_id)
27402 /* If we've seen a '<', we might be in a
27403 template-argument-list. Until Core issue 325 is
27404 resolved, we don't know how this situation ought
27405 to be handled, so try to DTRT. We check whether
27406 what comes after the comma is a valid parameter
27407 declaration list. If it is, then the comma ends
27408 the default argument; otherwise the default
27409 argument continues. */
27410 bool error = false;
27411 cp_token *peek;
27413 /* Set ITALP so cp_parser_parameter_declaration_list
27414 doesn't decide to commit to this parse. */
27415 bool saved_italp = parser->in_template_argument_list_p;
27416 parser->in_template_argument_list_p = true;
27418 cp_parser_parse_tentatively (parser);
27420 if (nsdmi)
27422 /* Parse declarators until we reach a non-comma or
27423 somthing that cannot be an initializer.
27424 Just checking whether we're looking at a single
27425 declarator is insufficient. Consider:
27426 int var = tuple<T,U>::x;
27427 The template parameter 'U' looks exactly like a
27428 declarator. */
27431 int ctor_dtor_or_conv_p;
27432 cp_lexer_consume_token (parser->lexer);
27433 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
27434 &ctor_dtor_or_conv_p,
27435 /*parenthesized_p=*/NULL,
27436 /*member_p=*/true,
27437 /*friend_p=*/false);
27438 peek = cp_lexer_peek_token (parser->lexer);
27439 if (cp_parser_error_occurred (parser))
27440 break;
27442 while (peek->type == CPP_COMMA);
27443 /* If we met an '=' or ';' then the original comma
27444 was the end of the NSDMI. Otherwise assume
27445 we're still in the NSDMI. */
27446 error = (peek->type != CPP_EQ
27447 && peek->type != CPP_SEMICOLON);
27449 else
27451 cp_lexer_consume_token (parser->lexer);
27452 begin_scope (sk_function_parms, NULL_TREE);
27453 cp_parser_parameter_declaration_list (parser, &error);
27454 pop_bindings_and_leave_scope ();
27456 if (!cp_parser_error_occurred (parser) && !error)
27457 done = true;
27458 cp_parser_abort_tentative_parse (parser);
27460 parser->in_template_argument_list_p = saved_italp;
27461 break;
27463 case CPP_CLOSE_PAREN:
27464 case CPP_ELLIPSIS:
27465 /* If we run into a non-nested `;', `}', or `]',
27466 then the code is invalid -- but the default
27467 argument is certainly over. */
27468 case CPP_SEMICOLON:
27469 case CPP_CLOSE_BRACE:
27470 case CPP_CLOSE_SQUARE:
27471 if (depth == 0
27472 /* Handle correctly int n = sizeof ... ( p ); */
27473 && token->type != CPP_ELLIPSIS)
27474 done = true;
27475 /* Update DEPTH, if necessary. */
27476 else if (token->type == CPP_CLOSE_PAREN
27477 || token->type == CPP_CLOSE_BRACE
27478 || token->type == CPP_CLOSE_SQUARE)
27479 --depth;
27480 break;
27482 case CPP_OPEN_PAREN:
27483 case CPP_OPEN_SQUARE:
27484 case CPP_OPEN_BRACE:
27485 ++depth;
27486 break;
27488 case CPP_LESS:
27489 if (depth == 0)
27490 /* This might be the comparison operator, or it might
27491 start a template argument list. */
27492 ++maybe_template_id;
27493 break;
27495 case CPP_RSHIFT:
27496 if (cxx_dialect == cxx98)
27497 break;
27498 /* Fall through for C++0x, which treats the `>>'
27499 operator like two `>' tokens in certain
27500 cases. */
27502 case CPP_GREATER:
27503 if (depth == 0)
27505 /* This might be an operator, or it might close a
27506 template argument list. But if a previous '<'
27507 started a template argument list, this will have
27508 closed it, so we can't be in one anymore. */
27509 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
27510 if (maybe_template_id < 0)
27511 maybe_template_id = 0;
27513 break;
27515 /* If we run out of tokens, issue an error message. */
27516 case CPP_EOF:
27517 case CPP_PRAGMA_EOL:
27518 error_at (token->location, "file ends in default argument");
27519 return error_mark_node;
27521 case CPP_NAME:
27522 case CPP_SCOPE:
27523 /* In these cases, we should look for template-ids.
27524 For example, if the default argument is
27525 `X<int, double>()', we need to do name lookup to
27526 figure out whether or not `X' is a template; if
27527 so, the `,' does not end the default argument.
27529 That is not yet done. */
27530 break;
27532 default:
27533 break;
27536 /* If we've reached the end, stop. */
27537 if (done)
27538 break;
27540 /* Add the token to the token block. */
27541 token = cp_lexer_consume_token (parser->lexer);
27544 /* Create a DEFAULT_ARG to represent the unparsed default
27545 argument. */
27546 default_argument = make_node (DEFAULT_ARG);
27547 DEFARG_TOKENS (default_argument)
27548 = cp_token_cache_new (first_token, token);
27549 DEFARG_INSTANTIATIONS (default_argument) = NULL;
27551 return default_argument;
27554 /* Begin parsing tentatively. We always save tokens while parsing
27555 tentatively so that if the tentative parsing fails we can restore the
27556 tokens. */
27558 static void
27559 cp_parser_parse_tentatively (cp_parser* parser)
27561 /* Enter a new parsing context. */
27562 parser->context = cp_parser_context_new (parser->context);
27563 /* Begin saving tokens. */
27564 cp_lexer_save_tokens (parser->lexer);
27565 /* In order to avoid repetitive access control error messages,
27566 access checks are queued up until we are no longer parsing
27567 tentatively. */
27568 push_deferring_access_checks (dk_deferred);
27571 /* Commit to the currently active tentative parse. */
27573 static void
27574 cp_parser_commit_to_tentative_parse (cp_parser* parser)
27576 cp_parser_context *context;
27577 cp_lexer *lexer;
27579 /* Mark all of the levels as committed. */
27580 lexer = parser->lexer;
27581 for (context = parser->context; context->next; context = context->next)
27583 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27584 break;
27585 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27586 while (!cp_lexer_saving_tokens (lexer))
27587 lexer = lexer->next;
27588 cp_lexer_commit_tokens (lexer);
27592 /* Commit to the topmost currently active tentative parse.
27594 Note that this function shouldn't be called when there are
27595 irreversible side-effects while in a tentative state. For
27596 example, we shouldn't create a permanent entry in the symbol
27597 table, or issue an error message that might not apply if the
27598 tentative parse is aborted. */
27600 static void
27601 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
27603 cp_parser_context *context = parser->context;
27604 cp_lexer *lexer = parser->lexer;
27606 if (context)
27608 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
27609 return;
27610 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
27612 while (!cp_lexer_saving_tokens (lexer))
27613 lexer = lexer->next;
27614 cp_lexer_commit_tokens (lexer);
27618 /* Abort the currently active tentative parse. All consumed tokens
27619 will be rolled back, and no diagnostics will be issued. */
27621 static void
27622 cp_parser_abort_tentative_parse (cp_parser* parser)
27624 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
27625 || errorcount > 0);
27626 cp_parser_simulate_error (parser);
27627 /* Now, pretend that we want to see if the construct was
27628 successfully parsed. */
27629 cp_parser_parse_definitely (parser);
27632 /* Stop parsing tentatively. If a parse error has occurred, restore the
27633 token stream. Otherwise, commit to the tokens we have consumed.
27634 Returns true if no error occurred; false otherwise. */
27636 static bool
27637 cp_parser_parse_definitely (cp_parser* parser)
27639 bool error_occurred;
27640 cp_parser_context *context;
27642 /* Remember whether or not an error occurred, since we are about to
27643 destroy that information. */
27644 error_occurred = cp_parser_error_occurred (parser);
27645 /* Remove the topmost context from the stack. */
27646 context = parser->context;
27647 parser->context = context->next;
27648 /* If no parse errors occurred, commit to the tentative parse. */
27649 if (!error_occurred)
27651 /* Commit to the tokens read tentatively, unless that was
27652 already done. */
27653 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
27654 cp_lexer_commit_tokens (parser->lexer);
27656 pop_to_parent_deferring_access_checks ();
27658 /* Otherwise, if errors occurred, roll back our state so that things
27659 are just as they were before we began the tentative parse. */
27660 else
27662 cp_lexer_rollback_tokens (parser->lexer);
27663 pop_deferring_access_checks ();
27665 /* Add the context to the front of the free list. */
27666 context->next = cp_parser_context_free_list;
27667 cp_parser_context_free_list = context;
27669 return !error_occurred;
27672 /* Returns true if we are parsing tentatively and are not committed to
27673 this tentative parse. */
27675 static bool
27676 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
27678 return (cp_parser_parsing_tentatively (parser)
27679 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
27682 /* Returns nonzero iff an error has occurred during the most recent
27683 tentative parse. */
27685 static bool
27686 cp_parser_error_occurred (cp_parser* parser)
27688 return (cp_parser_parsing_tentatively (parser)
27689 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
27692 /* Returns nonzero if GNU extensions are allowed. */
27694 static bool
27695 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
27697 return parser->allow_gnu_extensions_p;
27700 /* Objective-C++ Productions */
27703 /* Parse an Objective-C expression, which feeds into a primary-expression
27704 above.
27706 objc-expression:
27707 objc-message-expression
27708 objc-string-literal
27709 objc-encode-expression
27710 objc-protocol-expression
27711 objc-selector-expression
27713 Returns a tree representation of the expression. */
27715 static cp_expr
27716 cp_parser_objc_expression (cp_parser* parser)
27718 /* Try to figure out what kind of declaration is present. */
27719 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
27721 switch (kwd->type)
27723 case CPP_OPEN_SQUARE:
27724 return cp_parser_objc_message_expression (parser);
27726 case CPP_OBJC_STRING:
27727 kwd = cp_lexer_consume_token (parser->lexer);
27728 return objc_build_string_object (kwd->u.value);
27730 case CPP_KEYWORD:
27731 switch (kwd->keyword)
27733 case RID_AT_ENCODE:
27734 return cp_parser_objc_encode_expression (parser);
27736 case RID_AT_PROTOCOL:
27737 return cp_parser_objc_protocol_expression (parser);
27739 case RID_AT_SELECTOR:
27740 return cp_parser_objc_selector_expression (parser);
27742 default:
27743 break;
27745 default:
27746 error_at (kwd->location,
27747 "misplaced %<@%D%> Objective-C++ construct",
27748 kwd->u.value);
27749 cp_parser_skip_to_end_of_block_or_statement (parser);
27752 return error_mark_node;
27755 /* Parse an Objective-C message expression.
27757 objc-message-expression:
27758 [ objc-message-receiver objc-message-args ]
27760 Returns a representation of an Objective-C message. */
27762 static tree
27763 cp_parser_objc_message_expression (cp_parser* parser)
27765 tree receiver, messageargs;
27767 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27768 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
27769 receiver = cp_parser_objc_message_receiver (parser);
27770 messageargs = cp_parser_objc_message_args (parser);
27771 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
27772 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
27774 tree result = objc_build_message_expr (receiver, messageargs);
27776 /* Construct a location e.g.
27777 [self func1:5]
27778 ^~~~~~~~~~~~~~
27779 ranging from the '[' to the ']', with the caret at the start. */
27780 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
27781 protected_set_expr_location (result, combined_loc);
27783 return result;
27786 /* Parse an objc-message-receiver.
27788 objc-message-receiver:
27789 expression
27790 simple-type-specifier
27792 Returns a representation of the type or expression. */
27794 static tree
27795 cp_parser_objc_message_receiver (cp_parser* parser)
27797 tree rcv;
27799 /* An Objective-C message receiver may be either (1) a type
27800 or (2) an expression. */
27801 cp_parser_parse_tentatively (parser);
27802 rcv = cp_parser_expression (parser);
27804 /* If that worked out, fine. */
27805 if (cp_parser_parse_definitely (parser))
27806 return rcv;
27808 cp_parser_parse_tentatively (parser);
27809 rcv = cp_parser_simple_type_specifier (parser,
27810 /*decl_specs=*/NULL,
27811 CP_PARSER_FLAGS_NONE);
27813 if (cp_parser_parse_definitely (parser))
27814 return objc_get_class_reference (rcv);
27816 cp_parser_error (parser, "objective-c++ message receiver expected");
27817 return error_mark_node;
27820 /* Parse the arguments and selectors comprising an Objective-C message.
27822 objc-message-args:
27823 objc-selector
27824 objc-selector-args
27825 objc-selector-args , objc-comma-args
27827 objc-selector-args:
27828 objc-selector [opt] : assignment-expression
27829 objc-selector-args objc-selector [opt] : assignment-expression
27831 objc-comma-args:
27832 assignment-expression
27833 objc-comma-args , assignment-expression
27835 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
27836 selector arguments and TREE_VALUE containing a list of comma
27837 arguments. */
27839 static tree
27840 cp_parser_objc_message_args (cp_parser* parser)
27842 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
27843 bool maybe_unary_selector_p = true;
27844 cp_token *token = cp_lexer_peek_token (parser->lexer);
27846 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
27848 tree selector = NULL_TREE, arg;
27850 if (token->type != CPP_COLON)
27851 selector = cp_parser_objc_selector (parser);
27853 /* Detect if we have a unary selector. */
27854 if (maybe_unary_selector_p
27855 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
27856 return build_tree_list (selector, NULL_TREE);
27858 maybe_unary_selector_p = false;
27859 cp_parser_require (parser, CPP_COLON, RT_COLON);
27860 arg = cp_parser_assignment_expression (parser);
27862 sel_args
27863 = chainon (sel_args,
27864 build_tree_list (selector, arg));
27866 token = cp_lexer_peek_token (parser->lexer);
27869 /* Handle non-selector arguments, if any. */
27870 while (token->type == CPP_COMMA)
27872 tree arg;
27874 cp_lexer_consume_token (parser->lexer);
27875 arg = cp_parser_assignment_expression (parser);
27877 addl_args
27878 = chainon (addl_args,
27879 build_tree_list (NULL_TREE, arg));
27881 token = cp_lexer_peek_token (parser->lexer);
27884 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
27886 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
27887 return build_tree_list (error_mark_node, error_mark_node);
27890 return build_tree_list (sel_args, addl_args);
27893 /* Parse an Objective-C encode expression.
27895 objc-encode-expression:
27896 @encode objc-typename
27898 Returns an encoded representation of the type argument. */
27900 static cp_expr
27901 cp_parser_objc_encode_expression (cp_parser* parser)
27903 tree type;
27904 cp_token *token;
27905 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27907 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
27908 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27909 token = cp_lexer_peek_token (parser->lexer);
27910 type = complete_type (cp_parser_type_id (parser));
27911 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27913 if (!type)
27915 error_at (token->location,
27916 "%<@encode%> must specify a type as an argument");
27917 return error_mark_node;
27920 /* This happens if we find @encode(T) (where T is a template
27921 typename or something dependent on a template typename) when
27922 parsing a template. In that case, we can't compile it
27923 immediately, but we rather create an AT_ENCODE_EXPR which will
27924 need to be instantiated when the template is used.
27926 if (dependent_type_p (type))
27928 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
27929 TREE_READONLY (value) = 1;
27930 return value;
27934 /* Build a location of the form:
27935 @encode(int)
27936 ^~~~~~~~~~~~
27937 with caret==start at the @ token, finishing at the close paren. */
27938 location_t combined_loc
27939 = make_location (start_loc, start_loc,
27940 cp_lexer_previous_token (parser->lexer)->location);
27942 return cp_expr (objc_build_encode_expr (type), combined_loc);
27945 /* Parse an Objective-C @defs expression. */
27947 static tree
27948 cp_parser_objc_defs_expression (cp_parser *parser)
27950 tree name;
27952 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
27953 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27954 name = cp_parser_identifier (parser);
27955 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27957 return objc_get_class_ivars (name);
27960 /* Parse an Objective-C protocol expression.
27962 objc-protocol-expression:
27963 @protocol ( identifier )
27965 Returns a representation of the protocol expression. */
27967 static tree
27968 cp_parser_objc_protocol_expression (cp_parser* parser)
27970 tree proto;
27971 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
27973 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
27974 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27975 proto = cp_parser_identifier (parser);
27976 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27978 /* Build a location of the form:
27979 @protocol(prot)
27980 ^~~~~~~~~~~~~~~
27981 with caret==start at the @ token, finishing at the close paren. */
27982 location_t combined_loc
27983 = make_location (start_loc, start_loc,
27984 cp_lexer_previous_token (parser->lexer)->location);
27985 tree result = objc_build_protocol_expr (proto);
27986 protected_set_expr_location (result, combined_loc);
27987 return result;
27990 /* Parse an Objective-C selector expression.
27992 objc-selector-expression:
27993 @selector ( objc-method-signature )
27995 objc-method-signature:
27996 objc-selector
27997 objc-selector-seq
27999 objc-selector-seq:
28000 objc-selector :
28001 objc-selector-seq objc-selector :
28003 Returns a representation of the method selector. */
28005 static tree
28006 cp_parser_objc_selector_expression (cp_parser* parser)
28008 tree sel_seq = NULL_TREE;
28009 bool maybe_unary_selector_p = true;
28010 cp_token *token;
28011 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28013 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28014 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28015 token = cp_lexer_peek_token (parser->lexer);
28017 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28018 || token->type == CPP_SCOPE)
28020 tree selector = NULL_TREE;
28022 if (token->type != CPP_COLON
28023 || token->type == CPP_SCOPE)
28024 selector = cp_parser_objc_selector (parser);
28026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28027 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28029 /* Detect if we have a unary selector. */
28030 if (maybe_unary_selector_p)
28032 sel_seq = selector;
28033 goto finish_selector;
28035 else
28037 cp_parser_error (parser, "expected %<:%>");
28040 maybe_unary_selector_p = false;
28041 token = cp_lexer_consume_token (parser->lexer);
28043 if (token->type == CPP_SCOPE)
28045 sel_seq
28046 = chainon (sel_seq,
28047 build_tree_list (selector, NULL_TREE));
28048 sel_seq
28049 = chainon (sel_seq,
28050 build_tree_list (NULL_TREE, NULL_TREE));
28052 else
28053 sel_seq
28054 = chainon (sel_seq,
28055 build_tree_list (selector, NULL_TREE));
28057 token = cp_lexer_peek_token (parser->lexer);
28060 finish_selector:
28061 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28064 /* Build a location of the form:
28065 @selector(func)
28066 ^~~~~~~~~~~~~~~
28067 with caret==start at the @ token, finishing at the close paren. */
28068 location_t combined_loc
28069 = make_location (loc, loc,
28070 cp_lexer_previous_token (parser->lexer)->location);
28071 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28072 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28073 protected_set_expr_location (result, combined_loc);
28074 return result;
28077 /* Parse a list of identifiers.
28079 objc-identifier-list:
28080 identifier
28081 objc-identifier-list , identifier
28083 Returns a TREE_LIST of identifier nodes. */
28085 static tree
28086 cp_parser_objc_identifier_list (cp_parser* parser)
28088 tree identifier;
28089 tree list;
28090 cp_token *sep;
28092 identifier = cp_parser_identifier (parser);
28093 if (identifier == error_mark_node)
28094 return error_mark_node;
28096 list = build_tree_list (NULL_TREE, identifier);
28097 sep = cp_lexer_peek_token (parser->lexer);
28099 while (sep->type == CPP_COMMA)
28101 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28102 identifier = cp_parser_identifier (parser);
28103 if (identifier == error_mark_node)
28104 return list;
28106 list = chainon (list, build_tree_list (NULL_TREE,
28107 identifier));
28108 sep = cp_lexer_peek_token (parser->lexer);
28111 return list;
28114 /* Parse an Objective-C alias declaration.
28116 objc-alias-declaration:
28117 @compatibility_alias identifier identifier ;
28119 This function registers the alias mapping with the Objective-C front end.
28120 It returns nothing. */
28122 static void
28123 cp_parser_objc_alias_declaration (cp_parser* parser)
28125 tree alias, orig;
28127 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
28128 alias = cp_parser_identifier (parser);
28129 orig = cp_parser_identifier (parser);
28130 objc_declare_alias (alias, orig);
28131 cp_parser_consume_semicolon_at_end_of_statement (parser);
28134 /* Parse an Objective-C class forward-declaration.
28136 objc-class-declaration:
28137 @class objc-identifier-list ;
28139 The function registers the forward declarations with the Objective-C
28140 front end. It returns nothing. */
28142 static void
28143 cp_parser_objc_class_declaration (cp_parser* parser)
28145 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
28146 while (true)
28148 tree id;
28150 id = cp_parser_identifier (parser);
28151 if (id == error_mark_node)
28152 break;
28154 objc_declare_class (id);
28156 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28157 cp_lexer_consume_token (parser->lexer);
28158 else
28159 break;
28161 cp_parser_consume_semicolon_at_end_of_statement (parser);
28164 /* Parse a list of Objective-C protocol references.
28166 objc-protocol-refs-opt:
28167 objc-protocol-refs [opt]
28169 objc-protocol-refs:
28170 < objc-identifier-list >
28172 Returns a TREE_LIST of identifiers, if any. */
28174 static tree
28175 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
28177 tree protorefs = NULL_TREE;
28179 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
28181 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
28182 protorefs = cp_parser_objc_identifier_list (parser);
28183 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
28186 return protorefs;
28189 /* Parse a Objective-C visibility specification. */
28191 static void
28192 cp_parser_objc_visibility_spec (cp_parser* parser)
28194 cp_token *vis = cp_lexer_peek_token (parser->lexer);
28196 switch (vis->keyword)
28198 case RID_AT_PRIVATE:
28199 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
28200 break;
28201 case RID_AT_PROTECTED:
28202 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
28203 break;
28204 case RID_AT_PUBLIC:
28205 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
28206 break;
28207 case RID_AT_PACKAGE:
28208 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
28209 break;
28210 default:
28211 return;
28214 /* Eat '@private'/'@protected'/'@public'. */
28215 cp_lexer_consume_token (parser->lexer);
28218 /* Parse an Objective-C method type. Return 'true' if it is a class
28219 (+) method, and 'false' if it is an instance (-) method. */
28221 static inline bool
28222 cp_parser_objc_method_type (cp_parser* parser)
28224 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
28225 return true;
28226 else
28227 return false;
28230 /* Parse an Objective-C protocol qualifier. */
28232 static tree
28233 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
28235 tree quals = NULL_TREE, node;
28236 cp_token *token = cp_lexer_peek_token (parser->lexer);
28238 node = token->u.value;
28240 while (node && identifier_p (node)
28241 && (node == ridpointers [(int) RID_IN]
28242 || node == ridpointers [(int) RID_OUT]
28243 || node == ridpointers [(int) RID_INOUT]
28244 || node == ridpointers [(int) RID_BYCOPY]
28245 || node == ridpointers [(int) RID_BYREF]
28246 || node == ridpointers [(int) RID_ONEWAY]))
28248 quals = tree_cons (NULL_TREE, node, quals);
28249 cp_lexer_consume_token (parser->lexer);
28250 token = cp_lexer_peek_token (parser->lexer);
28251 node = token->u.value;
28254 return quals;
28257 /* Parse an Objective-C typename. */
28259 static tree
28260 cp_parser_objc_typename (cp_parser* parser)
28262 tree type_name = NULL_TREE;
28264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
28266 tree proto_quals, cp_type = NULL_TREE;
28268 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28269 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
28271 /* An ObjC type name may consist of just protocol qualifiers, in which
28272 case the type shall default to 'id'. */
28273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
28275 cp_type = cp_parser_type_id (parser);
28277 /* If the type could not be parsed, an error has already
28278 been produced. For error recovery, behave as if it had
28279 not been specified, which will use the default type
28280 'id'. */
28281 if (cp_type == error_mark_node)
28283 cp_type = NULL_TREE;
28284 /* We need to skip to the closing parenthesis as
28285 cp_parser_type_id() does not seem to do it for
28286 us. */
28287 cp_parser_skip_to_closing_parenthesis (parser,
28288 /*recovering=*/true,
28289 /*or_comma=*/false,
28290 /*consume_paren=*/false);
28294 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28295 type_name = build_tree_list (proto_quals, cp_type);
28298 return type_name;
28301 /* Check to see if TYPE refers to an Objective-C selector name. */
28303 static bool
28304 cp_parser_objc_selector_p (enum cpp_ttype type)
28306 return (type == CPP_NAME || type == CPP_KEYWORD
28307 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
28308 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
28309 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
28310 || type == CPP_XOR || type == CPP_XOR_EQ);
28313 /* Parse an Objective-C selector. */
28315 static tree
28316 cp_parser_objc_selector (cp_parser* parser)
28318 cp_token *token = cp_lexer_consume_token (parser->lexer);
28320 if (!cp_parser_objc_selector_p (token->type))
28322 error_at (token->location, "invalid Objective-C++ selector name");
28323 return error_mark_node;
28326 /* C++ operator names are allowed to appear in ObjC selectors. */
28327 switch (token->type)
28329 case CPP_AND_AND: return get_identifier ("and");
28330 case CPP_AND_EQ: return get_identifier ("and_eq");
28331 case CPP_AND: return get_identifier ("bitand");
28332 case CPP_OR: return get_identifier ("bitor");
28333 case CPP_COMPL: return get_identifier ("compl");
28334 case CPP_NOT: return get_identifier ("not");
28335 case CPP_NOT_EQ: return get_identifier ("not_eq");
28336 case CPP_OR_OR: return get_identifier ("or");
28337 case CPP_OR_EQ: return get_identifier ("or_eq");
28338 case CPP_XOR: return get_identifier ("xor");
28339 case CPP_XOR_EQ: return get_identifier ("xor_eq");
28340 default: return token->u.value;
28344 /* Parse an Objective-C params list. */
28346 static tree
28347 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
28349 tree params = NULL_TREE;
28350 bool maybe_unary_selector_p = true;
28351 cp_token *token = cp_lexer_peek_token (parser->lexer);
28353 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28355 tree selector = NULL_TREE, type_name, identifier;
28356 tree parm_attr = NULL_TREE;
28358 if (token->keyword == RID_ATTRIBUTE)
28359 break;
28361 if (token->type != CPP_COLON)
28362 selector = cp_parser_objc_selector (parser);
28364 /* Detect if we have a unary selector. */
28365 if (maybe_unary_selector_p
28366 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28368 params = selector; /* Might be followed by attributes. */
28369 break;
28372 maybe_unary_selector_p = false;
28373 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
28375 /* Something went quite wrong. There should be a colon
28376 here, but there is not. Stop parsing parameters. */
28377 break;
28379 type_name = cp_parser_objc_typename (parser);
28380 /* New ObjC allows attributes on parameters too. */
28381 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
28382 parm_attr = cp_parser_attributes_opt (parser);
28383 identifier = cp_parser_identifier (parser);
28385 params
28386 = chainon (params,
28387 objc_build_keyword_decl (selector,
28388 type_name,
28389 identifier,
28390 parm_attr));
28392 token = cp_lexer_peek_token (parser->lexer);
28395 if (params == NULL_TREE)
28397 cp_parser_error (parser, "objective-c++ method declaration is expected");
28398 return error_mark_node;
28401 /* We allow tail attributes for the method. */
28402 if (token->keyword == RID_ATTRIBUTE)
28404 *attributes = cp_parser_attributes_opt (parser);
28405 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28406 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28407 return params;
28408 cp_parser_error (parser,
28409 "method attributes must be specified at the end");
28410 return error_mark_node;
28413 if (params == NULL_TREE)
28415 cp_parser_error (parser, "objective-c++ method declaration is expected");
28416 return error_mark_node;
28418 return params;
28421 /* Parse the non-keyword Objective-C params. */
28423 static tree
28424 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
28425 tree* attributes)
28427 tree params = make_node (TREE_LIST);
28428 cp_token *token = cp_lexer_peek_token (parser->lexer);
28429 *ellipsisp = false; /* Initially, assume no ellipsis. */
28431 while (token->type == CPP_COMMA)
28433 cp_parameter_declarator *parmdecl;
28434 tree parm;
28436 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28437 token = cp_lexer_peek_token (parser->lexer);
28439 if (token->type == CPP_ELLIPSIS)
28441 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
28442 *ellipsisp = true;
28443 token = cp_lexer_peek_token (parser->lexer);
28444 break;
28447 /* TODO: parse attributes for tail parameters. */
28448 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
28449 parm = grokdeclarator (parmdecl->declarator,
28450 &parmdecl->decl_specifiers,
28451 PARM, /*initialized=*/0,
28452 /*attrlist=*/NULL);
28454 chainon (params, build_tree_list (NULL_TREE, parm));
28455 token = cp_lexer_peek_token (parser->lexer);
28458 /* We allow tail attributes for the method. */
28459 if (token->keyword == RID_ATTRIBUTE)
28461 if (*attributes == NULL_TREE)
28463 *attributes = cp_parser_attributes_opt (parser);
28464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
28465 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
28466 return params;
28468 else
28469 /* We have an error, but parse the attributes, so that we can
28470 carry on. */
28471 *attributes = cp_parser_attributes_opt (parser);
28473 cp_parser_error (parser,
28474 "method attributes must be specified at the end");
28475 return error_mark_node;
28478 return params;
28481 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
28483 static void
28484 cp_parser_objc_interstitial_code (cp_parser* parser)
28486 cp_token *token = cp_lexer_peek_token (parser->lexer);
28488 /* If the next token is `extern' and the following token is a string
28489 literal, then we have a linkage specification. */
28490 if (token->keyword == RID_EXTERN
28491 && cp_parser_is_pure_string_literal
28492 (cp_lexer_peek_nth_token (parser->lexer, 2)))
28493 cp_parser_linkage_specification (parser);
28494 /* Handle #pragma, if any. */
28495 else if (token->type == CPP_PRAGMA)
28496 cp_parser_pragma (parser, pragma_objc_icode, NULL);
28497 /* Allow stray semicolons. */
28498 else if (token->type == CPP_SEMICOLON)
28499 cp_lexer_consume_token (parser->lexer);
28500 /* Mark methods as optional or required, when building protocols. */
28501 else if (token->keyword == RID_AT_OPTIONAL)
28503 cp_lexer_consume_token (parser->lexer);
28504 objc_set_method_opt (true);
28506 else if (token->keyword == RID_AT_REQUIRED)
28508 cp_lexer_consume_token (parser->lexer);
28509 objc_set_method_opt (false);
28511 else if (token->keyword == RID_NAMESPACE)
28512 cp_parser_namespace_definition (parser);
28513 /* Other stray characters must generate errors. */
28514 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
28516 cp_lexer_consume_token (parser->lexer);
28517 error ("stray %qs between Objective-C++ methods",
28518 token->type == CPP_OPEN_BRACE ? "{" : "}");
28520 /* Finally, try to parse a block-declaration, or a function-definition. */
28521 else
28522 cp_parser_block_declaration (parser, /*statement_p=*/false);
28525 /* Parse a method signature. */
28527 static tree
28528 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
28530 tree rettype, kwdparms, optparms;
28531 bool ellipsis = false;
28532 bool is_class_method;
28534 is_class_method = cp_parser_objc_method_type (parser);
28535 rettype = cp_parser_objc_typename (parser);
28536 *attributes = NULL_TREE;
28537 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
28538 if (kwdparms == error_mark_node)
28539 return error_mark_node;
28540 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
28541 if (optparms == error_mark_node)
28542 return error_mark_node;
28544 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
28547 static bool
28548 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
28550 tree tattr;
28551 cp_lexer_save_tokens (parser->lexer);
28552 tattr = cp_parser_attributes_opt (parser);
28553 gcc_assert (tattr) ;
28555 /* If the attributes are followed by a method introducer, this is not allowed.
28556 Dump the attributes and flag the situation. */
28557 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
28558 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
28559 return true;
28561 /* Otherwise, the attributes introduce some interstitial code, possibly so
28562 rewind to allow that check. */
28563 cp_lexer_rollback_tokens (parser->lexer);
28564 return false;
28567 /* Parse an Objective-C method prototype list. */
28569 static void
28570 cp_parser_objc_method_prototype_list (cp_parser* parser)
28572 cp_token *token = cp_lexer_peek_token (parser->lexer);
28574 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28576 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28578 tree attributes, sig;
28579 bool is_class_method;
28580 if (token->type == CPP_PLUS)
28581 is_class_method = true;
28582 else
28583 is_class_method = false;
28584 sig = cp_parser_objc_method_signature (parser, &attributes);
28585 if (sig == error_mark_node)
28587 cp_parser_skip_to_end_of_block_or_statement (parser);
28588 token = cp_lexer_peek_token (parser->lexer);
28589 continue;
28591 objc_add_method_declaration (is_class_method, sig, attributes);
28592 cp_parser_consume_semicolon_at_end_of_statement (parser);
28594 else if (token->keyword == RID_AT_PROPERTY)
28595 cp_parser_objc_at_property_declaration (parser);
28596 else if (token->keyword == RID_ATTRIBUTE
28597 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28598 warning_at (cp_lexer_peek_token (parser->lexer)->location,
28599 OPT_Wattributes,
28600 "prefix attributes are ignored for methods");
28601 else
28602 /* Allow for interspersed non-ObjC++ code. */
28603 cp_parser_objc_interstitial_code (parser);
28605 token = cp_lexer_peek_token (parser->lexer);
28608 if (token->type != CPP_EOF)
28609 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28610 else
28611 cp_parser_error (parser, "expected %<@end%>");
28613 objc_finish_interface ();
28616 /* Parse an Objective-C method definition list. */
28618 static void
28619 cp_parser_objc_method_definition_list (cp_parser* parser)
28621 cp_token *token = cp_lexer_peek_token (parser->lexer);
28623 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
28625 tree meth;
28627 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
28629 cp_token *ptk;
28630 tree sig, attribute;
28631 bool is_class_method;
28632 if (token->type == CPP_PLUS)
28633 is_class_method = true;
28634 else
28635 is_class_method = false;
28636 push_deferring_access_checks (dk_deferred);
28637 sig = cp_parser_objc_method_signature (parser, &attribute);
28638 if (sig == error_mark_node)
28640 cp_parser_skip_to_end_of_block_or_statement (parser);
28641 token = cp_lexer_peek_token (parser->lexer);
28642 continue;
28644 objc_start_method_definition (is_class_method, sig, attribute,
28645 NULL_TREE);
28647 /* For historical reasons, we accept an optional semicolon. */
28648 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28649 cp_lexer_consume_token (parser->lexer);
28651 ptk = cp_lexer_peek_token (parser->lexer);
28652 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
28653 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
28655 perform_deferred_access_checks (tf_warning_or_error);
28656 stop_deferring_access_checks ();
28657 meth = cp_parser_function_definition_after_declarator (parser,
28658 false);
28659 pop_deferring_access_checks ();
28660 objc_finish_method_definition (meth);
28663 /* The following case will be removed once @synthesize is
28664 completely implemented. */
28665 else if (token->keyword == RID_AT_PROPERTY)
28666 cp_parser_objc_at_property_declaration (parser);
28667 else if (token->keyword == RID_AT_SYNTHESIZE)
28668 cp_parser_objc_at_synthesize_declaration (parser);
28669 else if (token->keyword == RID_AT_DYNAMIC)
28670 cp_parser_objc_at_dynamic_declaration (parser);
28671 else if (token->keyword == RID_ATTRIBUTE
28672 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
28673 warning_at (token->location, OPT_Wattributes,
28674 "prefix attributes are ignored for methods");
28675 else
28676 /* Allow for interspersed non-ObjC++ code. */
28677 cp_parser_objc_interstitial_code (parser);
28679 token = cp_lexer_peek_token (parser->lexer);
28682 if (token->type != CPP_EOF)
28683 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
28684 else
28685 cp_parser_error (parser, "expected %<@end%>");
28687 objc_finish_implementation ();
28690 /* Parse Objective-C ivars. */
28692 static void
28693 cp_parser_objc_class_ivars (cp_parser* parser)
28695 cp_token *token = cp_lexer_peek_token (parser->lexer);
28697 if (token->type != CPP_OPEN_BRACE)
28698 return; /* No ivars specified. */
28700 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
28701 token = cp_lexer_peek_token (parser->lexer);
28703 while (token->type != CPP_CLOSE_BRACE
28704 && token->keyword != RID_AT_END && token->type != CPP_EOF)
28706 cp_decl_specifier_seq declspecs;
28707 int decl_class_or_enum_p;
28708 tree prefix_attributes;
28710 cp_parser_objc_visibility_spec (parser);
28712 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
28713 break;
28715 cp_parser_decl_specifier_seq (parser,
28716 CP_PARSER_FLAGS_OPTIONAL,
28717 &declspecs,
28718 &decl_class_or_enum_p);
28720 /* auto, register, static, extern, mutable. */
28721 if (declspecs.storage_class != sc_none)
28723 cp_parser_error (parser, "invalid type for instance variable");
28724 declspecs.storage_class = sc_none;
28727 /* thread_local. */
28728 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
28730 cp_parser_error (parser, "invalid type for instance variable");
28731 declspecs.locations[ds_thread] = 0;
28734 /* typedef. */
28735 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
28737 cp_parser_error (parser, "invalid type for instance variable");
28738 declspecs.locations[ds_typedef] = 0;
28741 prefix_attributes = declspecs.attributes;
28742 declspecs.attributes = NULL_TREE;
28744 /* Keep going until we hit the `;' at the end of the
28745 declaration. */
28746 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
28748 tree width = NULL_TREE, attributes, first_attribute, decl;
28749 cp_declarator *declarator = NULL;
28750 int ctor_dtor_or_conv_p;
28752 /* Check for a (possibly unnamed) bitfield declaration. */
28753 token = cp_lexer_peek_token (parser->lexer);
28754 if (token->type == CPP_COLON)
28755 goto eat_colon;
28757 if (token->type == CPP_NAME
28758 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
28759 == CPP_COLON))
28761 /* Get the name of the bitfield. */
28762 declarator = make_id_declarator (NULL_TREE,
28763 cp_parser_identifier (parser),
28764 sfk_none);
28766 eat_colon:
28767 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28768 /* Get the width of the bitfield. */
28769 width
28770 = cp_parser_constant_expression (parser);
28772 else
28774 /* Parse the declarator. */
28775 declarator
28776 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28777 &ctor_dtor_or_conv_p,
28778 /*parenthesized_p=*/NULL,
28779 /*member_p=*/false,
28780 /*friend_p=*/false);
28783 /* Look for attributes that apply to the ivar. */
28784 attributes = cp_parser_attributes_opt (parser);
28785 /* Remember which attributes are prefix attributes and
28786 which are not. */
28787 first_attribute = attributes;
28788 /* Combine the attributes. */
28789 attributes = chainon (prefix_attributes, attributes);
28791 if (width)
28792 /* Create the bitfield declaration. */
28793 decl = grokbitfield (declarator, &declspecs,
28794 width,
28795 attributes);
28796 else
28797 decl = grokfield (declarator, &declspecs,
28798 NULL_TREE, /*init_const_expr_p=*/false,
28799 NULL_TREE, attributes);
28801 /* Add the instance variable. */
28802 if (decl != error_mark_node && decl != NULL_TREE)
28803 objc_add_instance_variable (decl);
28805 /* Reset PREFIX_ATTRIBUTES. */
28806 while (attributes && TREE_CHAIN (attributes) != first_attribute)
28807 attributes = TREE_CHAIN (attributes);
28808 if (attributes)
28809 TREE_CHAIN (attributes) = NULL_TREE;
28811 token = cp_lexer_peek_token (parser->lexer);
28813 if (token->type == CPP_COMMA)
28815 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
28816 continue;
28818 break;
28821 cp_parser_consume_semicolon_at_end_of_statement (parser);
28822 token = cp_lexer_peek_token (parser->lexer);
28825 if (token->keyword == RID_AT_END)
28826 cp_parser_error (parser, "expected %<}%>");
28828 /* Do not consume the RID_AT_END, so it will be read again as terminating
28829 the @interface of @implementation. */
28830 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
28831 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
28833 /* For historical reasons, we accept an optional semicolon. */
28834 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
28835 cp_lexer_consume_token (parser->lexer);
28838 /* Parse an Objective-C protocol declaration. */
28840 static void
28841 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
28843 tree proto, protorefs;
28844 cp_token *tok;
28846 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28847 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
28849 tok = cp_lexer_peek_token (parser->lexer);
28850 error_at (tok->location, "identifier expected after %<@protocol%>");
28851 cp_parser_consume_semicolon_at_end_of_statement (parser);
28852 return;
28855 /* See if we have a forward declaration or a definition. */
28856 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
28858 /* Try a forward declaration first. */
28859 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
28861 while (true)
28863 tree id;
28865 id = cp_parser_identifier (parser);
28866 if (id == error_mark_node)
28867 break;
28869 objc_declare_protocol (id, attributes);
28871 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
28872 cp_lexer_consume_token (parser->lexer);
28873 else
28874 break;
28876 cp_parser_consume_semicolon_at_end_of_statement (parser);
28879 /* Ok, we got a full-fledged definition (or at least should). */
28880 else
28882 proto = cp_parser_identifier (parser);
28883 protorefs = cp_parser_objc_protocol_refs_opt (parser);
28884 objc_start_protocol (proto, protorefs, attributes);
28885 cp_parser_objc_method_prototype_list (parser);
28889 /* Parse an Objective-C superclass or category. */
28891 static void
28892 cp_parser_objc_superclass_or_category (cp_parser *parser,
28893 bool iface_p,
28894 tree *super,
28895 tree *categ, bool *is_class_extension)
28897 cp_token *next = cp_lexer_peek_token (parser->lexer);
28899 *super = *categ = NULL_TREE;
28900 *is_class_extension = false;
28901 if (next->type == CPP_COLON)
28903 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
28904 *super = cp_parser_identifier (parser);
28906 else if (next->type == CPP_OPEN_PAREN)
28908 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
28910 /* If there is no category name, and this is an @interface, we
28911 have a class extension. */
28912 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
28914 *categ = NULL_TREE;
28915 *is_class_extension = true;
28917 else
28918 *categ = cp_parser_identifier (parser);
28920 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28924 /* Parse an Objective-C class interface. */
28926 static void
28927 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
28929 tree name, super, categ, protos;
28930 bool is_class_extension;
28932 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
28933 name = cp_parser_identifier (parser);
28934 if (name == error_mark_node)
28936 /* It's hard to recover because even if valid @interface stuff
28937 is to follow, we can't compile it (or validate it) if we
28938 don't even know which class it refers to. Let's assume this
28939 was a stray '@interface' token in the stream and skip it.
28941 return;
28943 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
28944 &is_class_extension);
28945 protos = cp_parser_objc_protocol_refs_opt (parser);
28947 /* We have either a class or a category on our hands. */
28948 if (categ || is_class_extension)
28949 objc_start_category_interface (name, categ, protos, attributes);
28950 else
28952 objc_start_class_interface (name, super, protos, attributes);
28953 /* Handle instance variable declarations, if any. */
28954 cp_parser_objc_class_ivars (parser);
28955 objc_continue_interface ();
28958 cp_parser_objc_method_prototype_list (parser);
28961 /* Parse an Objective-C class implementation. */
28963 static void
28964 cp_parser_objc_class_implementation (cp_parser* parser)
28966 tree name, super, categ;
28967 bool is_class_extension;
28969 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
28970 name = cp_parser_identifier (parser);
28971 if (name == error_mark_node)
28973 /* It's hard to recover because even if valid @implementation
28974 stuff is to follow, we can't compile it (or validate it) if
28975 we don't even know which class it refers to. Let's assume
28976 this was a stray '@implementation' token in the stream and
28977 skip it.
28979 return;
28981 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
28982 &is_class_extension);
28984 /* We have either a class or a category on our hands. */
28985 if (categ)
28986 objc_start_category_implementation (name, categ);
28987 else
28989 objc_start_class_implementation (name, super);
28990 /* Handle instance variable declarations, if any. */
28991 cp_parser_objc_class_ivars (parser);
28992 objc_continue_implementation ();
28995 cp_parser_objc_method_definition_list (parser);
28998 /* Consume the @end token and finish off the implementation. */
29000 static void
29001 cp_parser_objc_end_implementation (cp_parser* parser)
29003 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29004 objc_finish_implementation ();
29007 /* Parse an Objective-C declaration. */
29009 static void
29010 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29012 /* Try to figure out what kind of declaration is present. */
29013 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29015 if (attributes)
29016 switch (kwd->keyword)
29018 case RID_AT_ALIAS:
29019 case RID_AT_CLASS:
29020 case RID_AT_END:
29021 error_at (kwd->location, "attributes may not be specified before"
29022 " the %<@%D%> Objective-C++ keyword",
29023 kwd->u.value);
29024 attributes = NULL;
29025 break;
29026 case RID_AT_IMPLEMENTATION:
29027 warning_at (kwd->location, OPT_Wattributes,
29028 "prefix attributes are ignored before %<@%D%>",
29029 kwd->u.value);
29030 attributes = NULL;
29031 default:
29032 break;
29035 switch (kwd->keyword)
29037 case RID_AT_ALIAS:
29038 cp_parser_objc_alias_declaration (parser);
29039 break;
29040 case RID_AT_CLASS:
29041 cp_parser_objc_class_declaration (parser);
29042 break;
29043 case RID_AT_PROTOCOL:
29044 cp_parser_objc_protocol_declaration (parser, attributes);
29045 break;
29046 case RID_AT_INTERFACE:
29047 cp_parser_objc_class_interface (parser, attributes);
29048 break;
29049 case RID_AT_IMPLEMENTATION:
29050 cp_parser_objc_class_implementation (parser);
29051 break;
29052 case RID_AT_END:
29053 cp_parser_objc_end_implementation (parser);
29054 break;
29055 default:
29056 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29057 kwd->u.value);
29058 cp_parser_skip_to_end_of_block_or_statement (parser);
29062 /* Parse an Objective-C try-catch-finally statement.
29064 objc-try-catch-finally-stmt:
29065 @try compound-statement objc-catch-clause-seq [opt]
29066 objc-finally-clause [opt]
29068 objc-catch-clause-seq:
29069 objc-catch-clause objc-catch-clause-seq [opt]
29071 objc-catch-clause:
29072 @catch ( objc-exception-declaration ) compound-statement
29074 objc-finally-clause:
29075 @finally compound-statement
29077 objc-exception-declaration:
29078 parameter-declaration
29079 '...'
29081 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29083 Returns NULL_TREE.
29085 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29086 for C. Keep them in sync. */
29088 static tree
29089 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
29091 location_t location;
29092 tree stmt;
29094 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
29095 location = cp_lexer_peek_token (parser->lexer)->location;
29096 objc_maybe_warn_exceptions (location);
29097 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
29098 node, lest it get absorbed into the surrounding block. */
29099 stmt = push_stmt_list ();
29100 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29101 objc_begin_try_stmt (location, pop_stmt_list (stmt));
29103 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
29105 cp_parameter_declarator *parm;
29106 tree parameter_declaration = error_mark_node;
29107 bool seen_open_paren = false;
29109 cp_lexer_consume_token (parser->lexer);
29110 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
29111 seen_open_paren = true;
29112 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
29114 /* We have "@catch (...)" (where the '...' are literally
29115 what is in the code). Skip the '...'.
29116 parameter_declaration is set to NULL_TREE, and
29117 objc_being_catch_clauses() knows that that means
29118 '...'. */
29119 cp_lexer_consume_token (parser->lexer);
29120 parameter_declaration = NULL_TREE;
29122 else
29124 /* We have "@catch (NSException *exception)" or something
29125 like that. Parse the parameter declaration. */
29126 parm = cp_parser_parameter_declaration (parser, false, NULL);
29127 if (parm == NULL)
29128 parameter_declaration = error_mark_node;
29129 else
29130 parameter_declaration = grokdeclarator (parm->declarator,
29131 &parm->decl_specifiers,
29132 PARM, /*initialized=*/0,
29133 /*attrlist=*/NULL);
29135 if (seen_open_paren)
29136 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29137 else
29139 /* If there was no open parenthesis, we are recovering from
29140 an error, and we are trying to figure out what mistake
29141 the user has made. */
29143 /* If there is an immediate closing parenthesis, the user
29144 probably forgot the opening one (ie, they typed "@catch
29145 NSException *e)". Parse the closing parenthesis and keep
29146 going. */
29147 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29148 cp_lexer_consume_token (parser->lexer);
29150 /* If these is no immediate closing parenthesis, the user
29151 probably doesn't know that parenthesis are required at
29152 all (ie, they typed "@catch NSException *e"). So, just
29153 forget about the closing parenthesis and keep going. */
29155 objc_begin_catch_clause (parameter_declaration);
29156 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29157 objc_finish_catch_clause ();
29159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
29161 cp_lexer_consume_token (parser->lexer);
29162 location = cp_lexer_peek_token (parser->lexer)->location;
29163 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
29164 node, lest it get absorbed into the surrounding block. */
29165 stmt = push_stmt_list ();
29166 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29167 objc_build_finally_clause (location, pop_stmt_list (stmt));
29170 return objc_finish_try_stmt ();
29173 /* Parse an Objective-C synchronized statement.
29175 objc-synchronized-stmt:
29176 @synchronized ( expression ) compound-statement
29178 Returns NULL_TREE. */
29180 static tree
29181 cp_parser_objc_synchronized_statement (cp_parser *parser)
29183 location_t location;
29184 tree lock, stmt;
29186 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
29188 location = cp_lexer_peek_token (parser->lexer)->location;
29189 objc_maybe_warn_exceptions (location);
29190 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29191 lock = cp_parser_expression (parser);
29192 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29194 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
29195 node, lest it get absorbed into the surrounding block. */
29196 stmt = push_stmt_list ();
29197 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
29199 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
29202 /* Parse an Objective-C throw statement.
29204 objc-throw-stmt:
29205 @throw assignment-expression [opt] ;
29207 Returns a constructed '@throw' statement. */
29209 static tree
29210 cp_parser_objc_throw_statement (cp_parser *parser)
29212 tree expr = NULL_TREE;
29213 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29215 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
29217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29218 expr = cp_parser_expression (parser);
29220 cp_parser_consume_semicolon_at_end_of_statement (parser);
29222 return objc_build_throw_stmt (loc, expr);
29225 /* Parse an Objective-C statement. */
29227 static tree
29228 cp_parser_objc_statement (cp_parser * parser)
29230 /* Try to figure out what kind of declaration is present. */
29231 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29233 switch (kwd->keyword)
29235 case RID_AT_TRY:
29236 return cp_parser_objc_try_catch_finally_statement (parser);
29237 case RID_AT_SYNCHRONIZED:
29238 return cp_parser_objc_synchronized_statement (parser);
29239 case RID_AT_THROW:
29240 return cp_parser_objc_throw_statement (parser);
29241 default:
29242 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29243 kwd->u.value);
29244 cp_parser_skip_to_end_of_block_or_statement (parser);
29247 return error_mark_node;
29250 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
29251 look ahead to see if an objc keyword follows the attributes. This
29252 is to detect the use of prefix attributes on ObjC @interface and
29253 @protocol. */
29255 static bool
29256 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
29258 cp_lexer_save_tokens (parser->lexer);
29259 *attrib = cp_parser_attributes_opt (parser);
29260 gcc_assert (*attrib);
29261 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
29263 cp_lexer_commit_tokens (parser->lexer);
29264 return true;
29266 cp_lexer_rollback_tokens (parser->lexer);
29267 return false;
29270 /* This routine is a minimal replacement for
29271 c_parser_struct_declaration () used when parsing the list of
29272 types/names or ObjC++ properties. For example, when parsing the
29273 code
29275 @property (readonly) int a, b, c;
29277 this function is responsible for parsing "int a, int b, int c" and
29278 returning the declarations as CHAIN of DECLs.
29280 TODO: Share this code with cp_parser_objc_class_ivars. It's very
29281 similar parsing. */
29282 static tree
29283 cp_parser_objc_struct_declaration (cp_parser *parser)
29285 tree decls = NULL_TREE;
29286 cp_decl_specifier_seq declspecs;
29287 int decl_class_or_enum_p;
29288 tree prefix_attributes;
29290 cp_parser_decl_specifier_seq (parser,
29291 CP_PARSER_FLAGS_NONE,
29292 &declspecs,
29293 &decl_class_or_enum_p);
29295 if (declspecs.type == error_mark_node)
29296 return error_mark_node;
29298 /* auto, register, static, extern, mutable. */
29299 if (declspecs.storage_class != sc_none)
29301 cp_parser_error (parser, "invalid type for property");
29302 declspecs.storage_class = sc_none;
29305 /* thread_local. */
29306 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29308 cp_parser_error (parser, "invalid type for property");
29309 declspecs.locations[ds_thread] = 0;
29312 /* typedef. */
29313 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29315 cp_parser_error (parser, "invalid type for property");
29316 declspecs.locations[ds_typedef] = 0;
29319 prefix_attributes = declspecs.attributes;
29320 declspecs.attributes = NULL_TREE;
29322 /* Keep going until we hit the `;' at the end of the declaration. */
29323 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29325 tree attributes, first_attribute, decl;
29326 cp_declarator *declarator;
29327 cp_token *token;
29329 /* Parse the declarator. */
29330 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29331 NULL, NULL, false, false);
29333 /* Look for attributes that apply to the ivar. */
29334 attributes = cp_parser_attributes_opt (parser);
29335 /* Remember which attributes are prefix attributes and
29336 which are not. */
29337 first_attribute = attributes;
29338 /* Combine the attributes. */
29339 attributes = chainon (prefix_attributes, attributes);
29341 decl = grokfield (declarator, &declspecs,
29342 NULL_TREE, /*init_const_expr_p=*/false,
29343 NULL_TREE, attributes);
29345 if (decl == error_mark_node || decl == NULL_TREE)
29346 return error_mark_node;
29348 /* Reset PREFIX_ATTRIBUTES. */
29349 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29350 attributes = TREE_CHAIN (attributes);
29351 if (attributes)
29352 TREE_CHAIN (attributes) = NULL_TREE;
29354 DECL_CHAIN (decl) = decls;
29355 decls = decl;
29357 token = cp_lexer_peek_token (parser->lexer);
29358 if (token->type == CPP_COMMA)
29360 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29361 continue;
29363 else
29364 break;
29366 return decls;
29369 /* Parse an Objective-C @property declaration. The syntax is:
29371 objc-property-declaration:
29372 '@property' objc-property-attributes[opt] struct-declaration ;
29374 objc-property-attributes:
29375 '(' objc-property-attribute-list ')'
29377 objc-property-attribute-list:
29378 objc-property-attribute
29379 objc-property-attribute-list, objc-property-attribute
29381 objc-property-attribute
29382 'getter' = identifier
29383 'setter' = identifier
29384 'readonly'
29385 'readwrite'
29386 'assign'
29387 'retain'
29388 'copy'
29389 'nonatomic'
29391 For example:
29392 @property NSString *name;
29393 @property (readonly) id object;
29394 @property (retain, nonatomic, getter=getTheName) id name;
29395 @property int a, b, c;
29397 PS: This function is identical to
29398 c_parser_objc_at_property_declaration for C. Keep them in sync. */
29399 static void
29400 cp_parser_objc_at_property_declaration (cp_parser *parser)
29402 /* The following variables hold the attributes of the properties as
29403 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
29404 seen. When we see an attribute, we set them to 'true' (if they
29405 are boolean properties) or to the identifier (if they have an
29406 argument, ie, for getter and setter). Note that here we only
29407 parse the list of attributes, check the syntax and accumulate the
29408 attributes that we find. objc_add_property_declaration() will
29409 then process the information. */
29410 bool property_assign = false;
29411 bool property_copy = false;
29412 tree property_getter_ident = NULL_TREE;
29413 bool property_nonatomic = false;
29414 bool property_readonly = false;
29415 bool property_readwrite = false;
29416 bool property_retain = false;
29417 tree property_setter_ident = NULL_TREE;
29419 /* 'properties' is the list of properties that we read. Usually a
29420 single one, but maybe more (eg, in "@property int a, b, c;" there
29421 are three). */
29422 tree properties;
29423 location_t loc;
29425 loc = cp_lexer_peek_token (parser->lexer)->location;
29427 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
29429 /* Parse the optional attribute list... */
29430 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29432 /* Eat the '('. */
29433 cp_lexer_consume_token (parser->lexer);
29435 while (true)
29437 bool syntax_error = false;
29438 cp_token *token = cp_lexer_peek_token (parser->lexer);
29439 enum rid keyword;
29441 if (token->type != CPP_NAME)
29443 cp_parser_error (parser, "expected identifier");
29444 break;
29446 keyword = C_RID_CODE (token->u.value);
29447 cp_lexer_consume_token (parser->lexer);
29448 switch (keyword)
29450 case RID_ASSIGN: property_assign = true; break;
29451 case RID_COPY: property_copy = true; break;
29452 case RID_NONATOMIC: property_nonatomic = true; break;
29453 case RID_READONLY: property_readonly = true; break;
29454 case RID_READWRITE: property_readwrite = true; break;
29455 case RID_RETAIN: property_retain = true; break;
29457 case RID_GETTER:
29458 case RID_SETTER:
29459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
29461 if (keyword == RID_GETTER)
29462 cp_parser_error (parser,
29463 "missing %<=%> (after %<getter%> attribute)");
29464 else
29465 cp_parser_error (parser,
29466 "missing %<=%> (after %<setter%> attribute)");
29467 syntax_error = true;
29468 break;
29470 cp_lexer_consume_token (parser->lexer); /* eat the = */
29471 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
29473 cp_parser_error (parser, "expected identifier");
29474 syntax_error = true;
29475 break;
29477 if (keyword == RID_SETTER)
29479 if (property_setter_ident != NULL_TREE)
29481 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
29482 cp_lexer_consume_token (parser->lexer);
29484 else
29485 property_setter_ident = cp_parser_objc_selector (parser);
29486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29487 cp_parser_error (parser, "setter name must terminate with %<:%>");
29488 else
29489 cp_lexer_consume_token (parser->lexer);
29491 else
29493 if (property_getter_ident != NULL_TREE)
29495 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
29496 cp_lexer_consume_token (parser->lexer);
29498 else
29499 property_getter_ident = cp_parser_objc_selector (parser);
29501 break;
29502 default:
29503 cp_parser_error (parser, "unknown property attribute");
29504 syntax_error = true;
29505 break;
29508 if (syntax_error)
29509 break;
29511 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29512 cp_lexer_consume_token (parser->lexer);
29513 else
29514 break;
29517 /* FIXME: "@property (setter, assign);" will generate a spurious
29518 "error: expected ‘)’ before ‘,’ token". This is because
29519 cp_parser_require, unlike the C counterpart, will produce an
29520 error even if we are in error recovery. */
29521 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
29523 cp_parser_skip_to_closing_parenthesis (parser,
29524 /*recovering=*/true,
29525 /*or_comma=*/false,
29526 /*consume_paren=*/true);
29530 /* ... and the property declaration(s). */
29531 properties = cp_parser_objc_struct_declaration (parser);
29533 if (properties == error_mark_node)
29535 cp_parser_skip_to_end_of_statement (parser);
29536 /* If the next token is now a `;', consume it. */
29537 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29538 cp_lexer_consume_token (parser->lexer);
29539 return;
29542 if (properties == NULL_TREE)
29543 cp_parser_error (parser, "expected identifier");
29544 else
29546 /* Comma-separated properties are chained together in
29547 reverse order; add them one by one. */
29548 properties = nreverse (properties);
29550 for (; properties; properties = TREE_CHAIN (properties))
29551 objc_add_property_declaration (loc, copy_node (properties),
29552 property_readonly, property_readwrite,
29553 property_assign, property_retain,
29554 property_copy, property_nonatomic,
29555 property_getter_ident, property_setter_ident);
29558 cp_parser_consume_semicolon_at_end_of_statement (parser);
29561 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
29563 objc-synthesize-declaration:
29564 @synthesize objc-synthesize-identifier-list ;
29566 objc-synthesize-identifier-list:
29567 objc-synthesize-identifier
29568 objc-synthesize-identifier-list, objc-synthesize-identifier
29570 objc-synthesize-identifier
29571 identifier
29572 identifier = identifier
29574 For example:
29575 @synthesize MyProperty;
29576 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
29578 PS: This function is identical to c_parser_objc_at_synthesize_declaration
29579 for C. Keep them in sync.
29581 static void
29582 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
29584 tree list = NULL_TREE;
29585 location_t loc;
29586 loc = cp_lexer_peek_token (parser->lexer)->location;
29588 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
29589 while (true)
29591 tree property, ivar;
29592 property = cp_parser_identifier (parser);
29593 if (property == error_mark_node)
29595 cp_parser_consume_semicolon_at_end_of_statement (parser);
29596 return;
29598 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
29600 cp_lexer_consume_token (parser->lexer);
29601 ivar = cp_parser_identifier (parser);
29602 if (ivar == error_mark_node)
29604 cp_parser_consume_semicolon_at_end_of_statement (parser);
29605 return;
29608 else
29609 ivar = NULL_TREE;
29610 list = chainon (list, build_tree_list (ivar, property));
29611 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29612 cp_lexer_consume_token (parser->lexer);
29613 else
29614 break;
29616 cp_parser_consume_semicolon_at_end_of_statement (parser);
29617 objc_add_synthesize_declaration (loc, list);
29620 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
29622 objc-dynamic-declaration:
29623 @dynamic identifier-list ;
29625 For example:
29626 @dynamic MyProperty;
29627 @dynamic MyProperty, AnotherProperty;
29629 PS: This function is identical to c_parser_objc_at_dynamic_declaration
29630 for C. Keep them in sync.
29632 static void
29633 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
29635 tree list = NULL_TREE;
29636 location_t loc;
29637 loc = cp_lexer_peek_token (parser->lexer)->location;
29639 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
29640 while (true)
29642 tree property;
29643 property = cp_parser_identifier (parser);
29644 if (property == error_mark_node)
29646 cp_parser_consume_semicolon_at_end_of_statement (parser);
29647 return;
29649 list = chainon (list, build_tree_list (NULL, property));
29650 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29651 cp_lexer_consume_token (parser->lexer);
29652 else
29653 break;
29655 cp_parser_consume_semicolon_at_end_of_statement (parser);
29656 objc_add_dynamic_declaration (loc, list);
29660 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
29662 /* Returns name of the next clause.
29663 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
29664 the token is not consumed. Otherwise appropriate pragma_omp_clause is
29665 returned and the token is consumed. */
29667 static pragma_omp_clause
29668 cp_parser_omp_clause_name (cp_parser *parser)
29670 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
29672 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
29673 result = PRAGMA_OACC_CLAUSE_AUTO;
29674 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
29675 result = PRAGMA_OMP_CLAUSE_IF;
29676 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
29677 result = PRAGMA_OMP_CLAUSE_DEFAULT;
29678 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
29679 result = PRAGMA_OACC_CLAUSE_DELETE;
29680 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
29681 result = PRAGMA_OMP_CLAUSE_PRIVATE;
29682 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
29683 result = PRAGMA_OMP_CLAUSE_FOR;
29684 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
29686 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
29687 const char *p = IDENTIFIER_POINTER (id);
29689 switch (p[0])
29691 case 'a':
29692 if (!strcmp ("aligned", p))
29693 result = PRAGMA_OMP_CLAUSE_ALIGNED;
29694 else if (!strcmp ("async", p))
29695 result = PRAGMA_OACC_CLAUSE_ASYNC;
29696 break;
29697 case 'c':
29698 if (!strcmp ("collapse", p))
29699 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
29700 else if (!strcmp ("copy", p))
29701 result = PRAGMA_OACC_CLAUSE_COPY;
29702 else if (!strcmp ("copyin", p))
29703 result = PRAGMA_OMP_CLAUSE_COPYIN;
29704 else if (!strcmp ("copyout", p))
29705 result = PRAGMA_OACC_CLAUSE_COPYOUT;
29706 else if (!strcmp ("copyprivate", p))
29707 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
29708 else if (!strcmp ("create", p))
29709 result = PRAGMA_OACC_CLAUSE_CREATE;
29710 break;
29711 case 'd':
29712 if (!strcmp ("defaultmap", p))
29713 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
29714 else if (!strcmp ("depend", p))
29715 result = PRAGMA_OMP_CLAUSE_DEPEND;
29716 else if (!strcmp ("device", p))
29717 result = PRAGMA_OMP_CLAUSE_DEVICE;
29718 else if (!strcmp ("deviceptr", p))
29719 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
29720 else if (!strcmp ("device_resident", p))
29721 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
29722 else if (!strcmp ("dist_schedule", p))
29723 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
29724 break;
29725 case 'f':
29726 if (!strcmp ("final", p))
29727 result = PRAGMA_OMP_CLAUSE_FINAL;
29728 else if (!strcmp ("firstprivate", p))
29729 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
29730 else if (!strcmp ("from", p))
29731 result = PRAGMA_OMP_CLAUSE_FROM;
29732 break;
29733 case 'g':
29734 if (!strcmp ("gang", p))
29735 result = PRAGMA_OACC_CLAUSE_GANG;
29736 else if (!strcmp ("grainsize", p))
29737 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
29738 break;
29739 case 'h':
29740 if (!strcmp ("hint", p))
29741 result = PRAGMA_OMP_CLAUSE_HINT;
29742 else if (!strcmp ("host", p))
29743 result = PRAGMA_OACC_CLAUSE_HOST;
29744 break;
29745 case 'i':
29746 if (!strcmp ("inbranch", p))
29747 result = PRAGMA_OMP_CLAUSE_INBRANCH;
29748 else if (!strcmp ("independent", p))
29749 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
29750 else if (!strcmp ("is_device_ptr", p))
29751 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
29752 break;
29753 case 'l':
29754 if (!strcmp ("lastprivate", p))
29755 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
29756 else if (!strcmp ("linear", p))
29757 result = PRAGMA_OMP_CLAUSE_LINEAR;
29758 else if (!strcmp ("link", p))
29759 result = PRAGMA_OMP_CLAUSE_LINK;
29760 break;
29761 case 'm':
29762 if (!strcmp ("map", p))
29763 result = PRAGMA_OMP_CLAUSE_MAP;
29764 else if (!strcmp ("mergeable", p))
29765 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
29766 else if (flag_cilkplus && !strcmp ("mask", p))
29767 result = PRAGMA_CILK_CLAUSE_MASK;
29768 break;
29769 case 'n':
29770 if (!strcmp ("nogroup", p))
29771 result = PRAGMA_OMP_CLAUSE_NOGROUP;
29772 else if (!strcmp ("notinbranch", p))
29773 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
29774 else if (!strcmp ("nowait", p))
29775 result = PRAGMA_OMP_CLAUSE_NOWAIT;
29776 else if (flag_cilkplus && !strcmp ("nomask", p))
29777 result = PRAGMA_CILK_CLAUSE_NOMASK;
29778 else if (!strcmp ("num_gangs", p))
29779 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
29780 else if (!strcmp ("num_tasks", p))
29781 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
29782 else if (!strcmp ("num_teams", p))
29783 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
29784 else if (!strcmp ("num_threads", p))
29785 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
29786 else if (!strcmp ("num_workers", p))
29787 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
29788 break;
29789 case 'o':
29790 if (!strcmp ("ordered", p))
29791 result = PRAGMA_OMP_CLAUSE_ORDERED;
29792 break;
29793 case 'p':
29794 if (!strcmp ("parallel", p))
29795 result = PRAGMA_OMP_CLAUSE_PARALLEL;
29796 else if (!strcmp ("present", p))
29797 result = PRAGMA_OACC_CLAUSE_PRESENT;
29798 else if (!strcmp ("present_or_copy", p)
29799 || !strcmp ("pcopy", p))
29800 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
29801 else if (!strcmp ("present_or_copyin", p)
29802 || !strcmp ("pcopyin", p))
29803 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
29804 else if (!strcmp ("present_or_copyout", p)
29805 || !strcmp ("pcopyout", p))
29806 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
29807 else if (!strcmp ("present_or_create", p)
29808 || !strcmp ("pcreate", p))
29809 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
29810 else if (!strcmp ("priority", p))
29811 result = PRAGMA_OMP_CLAUSE_PRIORITY;
29812 else if (!strcmp ("proc_bind", p))
29813 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
29814 break;
29815 case 'r':
29816 if (!strcmp ("reduction", p))
29817 result = PRAGMA_OMP_CLAUSE_REDUCTION;
29818 break;
29819 case 's':
29820 if (!strcmp ("safelen", p))
29821 result = PRAGMA_OMP_CLAUSE_SAFELEN;
29822 else if (!strcmp ("schedule", p))
29823 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
29824 else if (!strcmp ("sections", p))
29825 result = PRAGMA_OMP_CLAUSE_SECTIONS;
29826 else if (!strcmp ("self", p))
29827 result = PRAGMA_OACC_CLAUSE_SELF;
29828 else if (!strcmp ("seq", p))
29829 result = PRAGMA_OACC_CLAUSE_SEQ;
29830 else if (!strcmp ("shared", p))
29831 result = PRAGMA_OMP_CLAUSE_SHARED;
29832 else if (!strcmp ("simd", p))
29833 result = PRAGMA_OMP_CLAUSE_SIMD;
29834 else if (!strcmp ("simdlen", p))
29835 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
29836 break;
29837 case 't':
29838 if (!strcmp ("taskgroup", p))
29839 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
29840 else if (!strcmp ("thread_limit", p))
29841 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
29842 else if (!strcmp ("threads", p))
29843 result = PRAGMA_OMP_CLAUSE_THREADS;
29844 else if (!strcmp ("tile", p))
29845 result = PRAGMA_OACC_CLAUSE_TILE;
29846 else if (!strcmp ("to", p))
29847 result = PRAGMA_OMP_CLAUSE_TO;
29848 break;
29849 case 'u':
29850 if (!strcmp ("uniform", p))
29851 result = PRAGMA_OMP_CLAUSE_UNIFORM;
29852 else if (!strcmp ("untied", p))
29853 result = PRAGMA_OMP_CLAUSE_UNTIED;
29854 else if (!strcmp ("use_device", p))
29855 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
29856 else if (!strcmp ("use_device_ptr", p))
29857 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
29858 break;
29859 case 'v':
29860 if (!strcmp ("vector", p))
29861 result = PRAGMA_OACC_CLAUSE_VECTOR;
29862 else if (!strcmp ("vector_length", p))
29863 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
29864 else if (flag_cilkplus && !strcmp ("vectorlength", p))
29865 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
29866 break;
29867 case 'w':
29868 if (!strcmp ("wait", p))
29869 result = PRAGMA_OACC_CLAUSE_WAIT;
29870 else if (!strcmp ("worker", p))
29871 result = PRAGMA_OACC_CLAUSE_WORKER;
29872 break;
29876 if (result != PRAGMA_OMP_CLAUSE_NONE)
29877 cp_lexer_consume_token (parser->lexer);
29879 return result;
29882 /* Validate that a clause of the given type does not already exist. */
29884 static void
29885 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
29886 const char *name, location_t location)
29888 tree c;
29890 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
29891 if (OMP_CLAUSE_CODE (c) == code)
29893 error_at (location, "too many %qs clauses", name);
29894 break;
29898 /* OpenMP 2.5:
29899 variable-list:
29900 identifier
29901 variable-list , identifier
29903 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
29904 colon). An opening parenthesis will have been consumed by the caller.
29906 If KIND is nonzero, create the appropriate node and install the decl
29907 in OMP_CLAUSE_DECL and add the node to the head of the list.
29909 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
29910 return the list created.
29912 COLON can be NULL if only closing parenthesis should end the list,
29913 or pointer to bool which will receive false if the list is terminated
29914 by closing parenthesis or true if the list is terminated by colon. */
29916 static tree
29917 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
29918 tree list, bool *colon)
29920 cp_token *token;
29921 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
29922 if (colon)
29924 parser->colon_corrects_to_scope_p = false;
29925 *colon = false;
29927 while (1)
29929 tree name, decl;
29931 token = cp_lexer_peek_token (parser->lexer);
29932 if (kind != 0
29933 && current_class_ptr
29934 && cp_parser_is_keyword (token, RID_THIS))
29936 decl = finish_this_expr ();
29937 if (TREE_CODE (decl) == NON_LVALUE_EXPR
29938 || CONVERT_EXPR_P (decl))
29939 decl = TREE_OPERAND (decl, 0);
29940 cp_lexer_consume_token (parser->lexer);
29942 else
29944 name = cp_parser_id_expression (parser, /*template_p=*/false,
29945 /*check_dependency_p=*/true,
29946 /*template_p=*/NULL,
29947 /*declarator_p=*/false,
29948 /*optional_p=*/false);
29949 if (name == error_mark_node)
29950 goto skip_comma;
29952 decl = cp_parser_lookup_name_simple (parser, name, token->location);
29953 if (decl == error_mark_node)
29954 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
29955 token->location);
29957 if (decl == error_mark_node)
29959 else if (kind != 0)
29961 switch (kind)
29963 case OMP_CLAUSE__CACHE_:
29964 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
29966 error_at (token->location, "expected %<[%>");
29967 decl = error_mark_node;
29968 break;
29970 /* FALLTHROUGH. */
29971 case OMP_CLAUSE_MAP:
29972 case OMP_CLAUSE_FROM:
29973 case OMP_CLAUSE_TO:
29974 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
29976 location_t loc
29977 = cp_lexer_peek_token (parser->lexer)->location;
29978 cp_id_kind idk = CP_ID_KIND_NONE;
29979 cp_lexer_consume_token (parser->lexer);
29980 decl
29981 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
29982 decl, false,
29983 &idk, loc);
29985 /* FALLTHROUGH. */
29986 case OMP_CLAUSE_DEPEND:
29987 case OMP_CLAUSE_REDUCTION:
29988 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
29990 tree low_bound = NULL_TREE, length = NULL_TREE;
29992 parser->colon_corrects_to_scope_p = false;
29993 cp_lexer_consume_token (parser->lexer);
29994 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
29995 low_bound = cp_parser_expression (parser);
29996 if (!colon)
29997 parser->colon_corrects_to_scope_p
29998 = saved_colon_corrects_to_scope_p;
29999 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30000 length = integer_one_node;
30001 else
30003 /* Look for `:'. */
30004 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30005 goto skip_comma;
30006 if (!cp_lexer_next_token_is (parser->lexer,
30007 CPP_CLOSE_SQUARE))
30008 length = cp_parser_expression (parser);
30010 /* Look for the closing `]'. */
30011 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30012 RT_CLOSE_SQUARE))
30013 goto skip_comma;
30015 if (kind == OMP_CLAUSE__CACHE_)
30017 if (TREE_CODE (low_bound) != INTEGER_CST
30018 && !TREE_READONLY (low_bound))
30020 error_at (token->location,
30021 "%qD is not a constant", low_bound);
30022 decl = error_mark_node;
30025 if (TREE_CODE (length) != INTEGER_CST
30026 && !TREE_READONLY (length))
30028 error_at (token->location,
30029 "%qD is not a constant", length);
30030 decl = error_mark_node;
30034 decl = tree_cons (low_bound, length, decl);
30036 break;
30037 default:
30038 break;
30041 tree u = build_omp_clause (token->location, kind);
30042 OMP_CLAUSE_DECL (u) = decl;
30043 OMP_CLAUSE_CHAIN (u) = list;
30044 list = u;
30046 else
30047 list = tree_cons (decl, NULL_TREE, list);
30049 get_comma:
30050 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30051 break;
30052 cp_lexer_consume_token (parser->lexer);
30055 if (colon)
30056 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30058 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30060 *colon = true;
30061 cp_parser_require (parser, CPP_COLON, RT_COLON);
30062 return list;
30065 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30067 int ending;
30069 /* Try to resync to an unnested comma. Copied from
30070 cp_parser_parenthesized_expression_list. */
30071 skip_comma:
30072 if (colon)
30073 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30074 ending = cp_parser_skip_to_closing_parenthesis (parser,
30075 /*recovering=*/true,
30076 /*or_comma=*/true,
30077 /*consume_paren=*/true);
30078 if (ending < 0)
30079 goto get_comma;
30082 return list;
30085 /* Similarly, but expect leading and trailing parenthesis. This is a very
30086 common case for omp clauses. */
30088 static tree
30089 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30091 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30092 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30093 return list;
30096 /* OpenACC 2.0:
30097 copy ( variable-list )
30098 copyin ( variable-list )
30099 copyout ( variable-list )
30100 create ( variable-list )
30101 delete ( variable-list )
30102 present ( variable-list )
30103 present_or_copy ( variable-list )
30104 pcopy ( variable-list )
30105 present_or_copyin ( variable-list )
30106 pcopyin ( variable-list )
30107 present_or_copyout ( variable-list )
30108 pcopyout ( variable-list )
30109 present_or_create ( variable-list )
30110 pcreate ( variable-list ) */
30112 static tree
30113 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
30114 tree list)
30116 enum gomp_map_kind kind;
30117 switch (c_kind)
30119 case PRAGMA_OACC_CLAUSE_COPY:
30120 kind = GOMP_MAP_FORCE_TOFROM;
30121 break;
30122 case PRAGMA_OACC_CLAUSE_COPYIN:
30123 kind = GOMP_MAP_FORCE_TO;
30124 break;
30125 case PRAGMA_OACC_CLAUSE_COPYOUT:
30126 kind = GOMP_MAP_FORCE_FROM;
30127 break;
30128 case PRAGMA_OACC_CLAUSE_CREATE:
30129 kind = GOMP_MAP_FORCE_ALLOC;
30130 break;
30131 case PRAGMA_OACC_CLAUSE_DELETE:
30132 kind = GOMP_MAP_DELETE;
30133 break;
30134 case PRAGMA_OACC_CLAUSE_DEVICE:
30135 kind = GOMP_MAP_FORCE_TO;
30136 break;
30137 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
30138 kind = GOMP_MAP_DEVICE_RESIDENT;
30139 break;
30140 case PRAGMA_OACC_CLAUSE_HOST:
30141 case PRAGMA_OACC_CLAUSE_SELF:
30142 kind = GOMP_MAP_FORCE_FROM;
30143 break;
30144 case PRAGMA_OACC_CLAUSE_LINK:
30145 kind = GOMP_MAP_LINK;
30146 break;
30147 case PRAGMA_OACC_CLAUSE_PRESENT:
30148 kind = GOMP_MAP_FORCE_PRESENT;
30149 break;
30150 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
30151 kind = GOMP_MAP_TOFROM;
30152 break;
30153 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
30154 kind = GOMP_MAP_TO;
30155 break;
30156 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
30157 kind = GOMP_MAP_FROM;
30158 break;
30159 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
30160 kind = GOMP_MAP_ALLOC;
30161 break;
30162 default:
30163 gcc_unreachable ();
30165 tree nl, c;
30166 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
30168 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
30169 OMP_CLAUSE_SET_MAP_KIND (c, kind);
30171 return nl;
30174 /* OpenACC 2.0:
30175 deviceptr ( variable-list ) */
30177 static tree
30178 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
30180 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30181 tree vars, t;
30183 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
30184 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
30185 variable-list must only allow for pointer variables. */
30186 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
30187 for (t = vars; t; t = TREE_CHAIN (t))
30189 tree v = TREE_PURPOSE (t);
30190 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
30191 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
30192 OMP_CLAUSE_DECL (u) = v;
30193 OMP_CLAUSE_CHAIN (u) = list;
30194 list = u;
30197 return list;
30200 /* OpenACC 2.0:
30201 auto
30202 independent
30203 nohost
30204 seq */
30206 static tree
30207 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
30208 enum omp_clause_code code,
30209 tree list, location_t location)
30211 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
30212 tree c = build_omp_clause (location, code);
30213 OMP_CLAUSE_CHAIN (c) = list;
30214 return c;
30217 /* OpenACC:
30218 num_gangs ( expression )
30219 num_workers ( expression )
30220 vector_length ( expression ) */
30222 static tree
30223 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
30224 const char *str, tree list)
30226 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30228 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30229 return list;
30231 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
30233 if (t == error_mark_node
30234 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30236 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30237 /*or_comma=*/false,
30238 /*consume_paren=*/true);
30239 return list;
30242 check_no_duplicate_clause (list, code, str, loc);
30244 tree c = build_omp_clause (loc, code);
30245 OMP_CLAUSE_OPERAND (c, 0) = t;
30246 OMP_CLAUSE_CHAIN (c) = list;
30247 return c;
30250 /* OpenACC:
30252 gang [( gang-arg-list )]
30253 worker [( [num:] int-expr )]
30254 vector [( [length:] int-expr )]
30256 where gang-arg is one of:
30258 [num:] int-expr
30259 static: size-expr
30261 and size-expr may be:
30264 int-expr
30267 static tree
30268 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
30269 const char *str, tree list)
30271 const char *id = "num";
30272 cp_lexer *lexer = parser->lexer;
30273 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
30274 location_t loc = cp_lexer_peek_token (lexer)->location;
30276 if (kind == OMP_CLAUSE_VECTOR)
30277 id = "length";
30279 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
30281 cp_lexer_consume_token (lexer);
30285 cp_token *next = cp_lexer_peek_token (lexer);
30286 int idx = 0;
30288 /* Gang static argument. */
30289 if (kind == OMP_CLAUSE_GANG
30290 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
30292 cp_lexer_consume_token (lexer);
30294 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30295 goto cleanup_error;
30297 idx = 1;
30298 if (ops[idx] != NULL)
30300 cp_parser_error (parser, "too many %<static%> arguments");
30301 goto cleanup_error;
30304 /* Check for the '*' argument. */
30305 if (cp_lexer_next_token_is (lexer, CPP_MULT)
30306 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30307 || cp_lexer_nth_token_is (parser->lexer, 2,
30308 CPP_CLOSE_PAREN)))
30310 cp_lexer_consume_token (lexer);
30311 ops[idx] = integer_minus_one_node;
30313 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
30315 cp_lexer_consume_token (lexer);
30316 continue;
30318 else break;
30321 /* Worker num: argument and vector length: arguments. */
30322 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
30323 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
30324 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
30326 cp_lexer_consume_token (lexer); /* id */
30327 cp_lexer_consume_token (lexer); /* ':' */
30330 /* Now collect the actual argument. */
30331 if (ops[idx] != NULL_TREE)
30333 cp_parser_error (parser, "unexpected argument");
30334 goto cleanup_error;
30337 tree expr = cp_parser_assignment_expression (parser, NULL, false,
30338 false);
30339 if (expr == error_mark_node)
30340 goto cleanup_error;
30342 mark_exp_read (expr);
30343 ops[idx] = expr;
30345 if (kind == OMP_CLAUSE_GANG
30346 && cp_lexer_next_token_is (lexer, CPP_COMMA))
30348 cp_lexer_consume_token (lexer);
30349 continue;
30351 break;
30353 while (1);
30355 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30356 goto cleanup_error;
30359 check_no_duplicate_clause (list, kind, str, loc);
30361 c = build_omp_clause (loc, kind);
30363 if (ops[1])
30364 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
30366 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
30367 OMP_CLAUSE_CHAIN (c) = list;
30369 return c;
30371 cleanup_error:
30372 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
30373 return list;
30376 /* OpenACC 2.0:
30377 tile ( size-expr-list ) */
30379 static tree
30380 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
30382 tree c, expr = error_mark_node;
30383 tree tile = NULL_TREE;
30385 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
30387 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30388 return list;
30392 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
30393 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
30394 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
30396 cp_lexer_consume_token (parser->lexer);
30397 expr = integer_minus_one_node;
30399 else
30400 expr = cp_parser_assignment_expression (parser, NULL, false, false);
30402 if (expr == error_mark_node)
30403 return list;
30405 tile = tree_cons (NULL_TREE, expr, tile);
30407 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30408 cp_lexer_consume_token (parser->lexer);
30410 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
30412 /* Consume the trailing ')'. */
30413 cp_lexer_consume_token (parser->lexer);
30415 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
30416 tile = nreverse (tile);
30417 OMP_CLAUSE_TILE_LIST (c) = tile;
30418 OMP_CLAUSE_CHAIN (c) = list;
30419 return c;
30422 /* OpenACC 2.0
30423 Parse wait clause or directive parameters. */
30425 static tree
30426 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
30428 vec<tree, va_gc> *args;
30429 tree t, args_tree;
30431 args = cp_parser_parenthesized_expression_list (parser, non_attr,
30432 /*cast_p=*/false,
30433 /*allow_expansion_p=*/true,
30434 /*non_constant_p=*/NULL);
30436 if (args == NULL || args->length () == 0)
30438 cp_parser_error (parser, "expected integer expression before ')'");
30439 if (args != NULL)
30440 release_tree_vector (args);
30441 return list;
30444 args_tree = build_tree_list_vec (args);
30446 release_tree_vector (args);
30448 for (t = args_tree; t; t = TREE_CHAIN (t))
30450 tree targ = TREE_VALUE (t);
30452 if (targ != error_mark_node)
30454 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
30455 error ("%<wait%> expression must be integral");
30456 else
30458 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
30460 mark_rvalue_use (targ);
30461 OMP_CLAUSE_DECL (c) = targ;
30462 OMP_CLAUSE_CHAIN (c) = list;
30463 list = c;
30468 return list;
30471 /* OpenACC:
30472 wait ( int-expr-list ) */
30474 static tree
30475 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
30477 location_t location = cp_lexer_peek_token (parser->lexer)->location;
30479 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
30480 return list;
30482 list = cp_parser_oacc_wait_list (parser, location, list);
30484 return list;
30487 /* OpenMP 3.0:
30488 collapse ( constant-expression ) */
30490 static tree
30491 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
30493 tree c, num;
30494 location_t loc;
30495 HOST_WIDE_INT n;
30497 loc = cp_lexer_peek_token (parser->lexer)->location;
30498 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30499 return list;
30501 num = cp_parser_constant_expression (parser);
30503 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30504 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30505 /*or_comma=*/false,
30506 /*consume_paren=*/true);
30508 if (num == error_mark_node)
30509 return list;
30510 num = fold_non_dependent_expr (num);
30511 if (!tree_fits_shwi_p (num)
30512 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
30513 || (n = tree_to_shwi (num)) <= 0
30514 || (int) n != n)
30516 error_at (loc, "collapse argument needs positive constant integer expression");
30517 return list;
30520 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
30521 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
30522 OMP_CLAUSE_CHAIN (c) = list;
30523 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
30525 return c;
30528 /* OpenMP 2.5:
30529 default ( shared | none )
30531 OpenACC 2.0
30532 default (none) */
30534 static tree
30535 cp_parser_omp_clause_default (cp_parser *parser, tree list,
30536 location_t location, bool is_oacc)
30538 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
30539 tree c;
30541 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30542 return list;
30543 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30545 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30546 const char *p = IDENTIFIER_POINTER (id);
30548 switch (p[0])
30550 case 'n':
30551 if (strcmp ("none", p) != 0)
30552 goto invalid_kind;
30553 kind = OMP_CLAUSE_DEFAULT_NONE;
30554 break;
30556 case 's':
30557 if (strcmp ("shared", p) != 0 || is_oacc)
30558 goto invalid_kind;
30559 kind = OMP_CLAUSE_DEFAULT_SHARED;
30560 break;
30562 default:
30563 goto invalid_kind;
30566 cp_lexer_consume_token (parser->lexer);
30568 else
30570 invalid_kind:
30571 if (is_oacc)
30572 cp_parser_error (parser, "expected %<none%>");
30573 else
30574 cp_parser_error (parser, "expected %<none%> or %<shared%>");
30577 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30578 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30579 /*or_comma=*/false,
30580 /*consume_paren=*/true);
30582 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
30583 return list;
30585 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
30586 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
30587 OMP_CLAUSE_CHAIN (c) = list;
30588 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
30590 return c;
30593 /* OpenMP 3.1:
30594 final ( expression ) */
30596 static tree
30597 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
30599 tree t, c;
30601 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30602 return list;
30604 t = cp_parser_condition (parser);
30606 if (t == error_mark_node
30607 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30608 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30609 /*or_comma=*/false,
30610 /*consume_paren=*/true);
30612 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
30614 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
30615 OMP_CLAUSE_FINAL_EXPR (c) = t;
30616 OMP_CLAUSE_CHAIN (c) = list;
30618 return c;
30621 /* OpenMP 2.5:
30622 if ( expression )
30624 OpenMP 4.5:
30625 if ( directive-name-modifier : expression )
30627 directive-name-modifier:
30628 parallel | task | taskloop | target data | target | target update
30629 | target enter data | target exit data */
30631 static tree
30632 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
30633 bool is_omp)
30635 tree t, c;
30636 enum tree_code if_modifier = ERROR_MARK;
30638 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30639 return list;
30641 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30643 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30644 const char *p = IDENTIFIER_POINTER (id);
30645 int n = 2;
30647 if (strcmp ("parallel", p) == 0)
30648 if_modifier = OMP_PARALLEL;
30649 else if (strcmp ("task", p) == 0)
30650 if_modifier = OMP_TASK;
30651 else if (strcmp ("taskloop", p) == 0)
30652 if_modifier = OMP_TASKLOOP;
30653 else if (strcmp ("target", p) == 0)
30655 if_modifier = OMP_TARGET;
30656 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
30658 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
30659 p = IDENTIFIER_POINTER (id);
30660 if (strcmp ("data", p) == 0)
30661 if_modifier = OMP_TARGET_DATA;
30662 else if (strcmp ("update", p) == 0)
30663 if_modifier = OMP_TARGET_UPDATE;
30664 else if (strcmp ("enter", p) == 0)
30665 if_modifier = OMP_TARGET_ENTER_DATA;
30666 else if (strcmp ("exit", p) == 0)
30667 if_modifier = OMP_TARGET_EXIT_DATA;
30668 if (if_modifier != OMP_TARGET)
30669 n = 3;
30670 else
30672 location_t loc
30673 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
30674 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
30675 "or %<exit%>");
30676 if_modifier = ERROR_MARK;
30678 if (if_modifier == OMP_TARGET_ENTER_DATA
30679 || if_modifier == OMP_TARGET_EXIT_DATA)
30681 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
30683 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
30684 p = IDENTIFIER_POINTER (id);
30685 if (strcmp ("data", p) == 0)
30686 n = 4;
30688 if (n != 4)
30690 location_t loc
30691 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
30692 error_at (loc, "expected %<data%>");
30693 if_modifier = ERROR_MARK;
30698 if (if_modifier != ERROR_MARK)
30700 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
30702 while (n-- > 0)
30703 cp_lexer_consume_token (parser->lexer);
30705 else
30707 if (n > 2)
30709 location_t loc
30710 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
30711 error_at (loc, "expected %<:%>");
30713 if_modifier = ERROR_MARK;
30718 t = cp_parser_condition (parser);
30720 if (t == error_mark_node
30721 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30722 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30723 /*or_comma=*/false,
30724 /*consume_paren=*/true);
30726 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
30727 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
30729 if (if_modifier != ERROR_MARK
30730 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30732 const char *p = NULL;
30733 switch (if_modifier)
30735 case OMP_PARALLEL: p = "parallel"; break;
30736 case OMP_TASK: p = "task"; break;
30737 case OMP_TASKLOOP: p = "taskloop"; break;
30738 case OMP_TARGET_DATA: p = "target data"; break;
30739 case OMP_TARGET: p = "target"; break;
30740 case OMP_TARGET_UPDATE: p = "target update"; break;
30741 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
30742 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
30743 default: gcc_unreachable ();
30745 error_at (location, "too many %<if%> clauses with %qs modifier",
30747 return list;
30749 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
30751 if (!is_omp)
30752 error_at (location, "too many %<if%> clauses");
30753 else
30754 error_at (location, "too many %<if%> clauses without modifier");
30755 return list;
30757 else if (if_modifier == ERROR_MARK
30758 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
30760 error_at (location, "if any %<if%> clause has modifier, then all "
30761 "%<if%> clauses have to use modifier");
30762 return list;
30766 c = build_omp_clause (location, OMP_CLAUSE_IF);
30767 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
30768 OMP_CLAUSE_IF_EXPR (c) = t;
30769 OMP_CLAUSE_CHAIN (c) = list;
30771 return c;
30774 /* OpenMP 3.1:
30775 mergeable */
30777 static tree
30778 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
30779 tree list, location_t location)
30781 tree c;
30783 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
30784 location);
30786 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
30787 OMP_CLAUSE_CHAIN (c) = list;
30788 return c;
30791 /* OpenMP 2.5:
30792 nowait */
30794 static tree
30795 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
30796 tree list, location_t location)
30798 tree c;
30800 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
30802 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
30803 OMP_CLAUSE_CHAIN (c) = list;
30804 return c;
30807 /* OpenMP 2.5:
30808 num_threads ( expression ) */
30810 static tree
30811 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
30812 location_t location)
30814 tree t, c;
30816 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30817 return list;
30819 t = cp_parser_expression (parser);
30821 if (t == error_mark_node
30822 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30824 /*or_comma=*/false,
30825 /*consume_paren=*/true);
30827 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
30828 "num_threads", location);
30830 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
30831 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
30832 OMP_CLAUSE_CHAIN (c) = list;
30834 return c;
30837 /* OpenMP 4.5:
30838 num_tasks ( expression ) */
30840 static tree
30841 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
30842 location_t location)
30844 tree t, c;
30846 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30847 return list;
30849 t = cp_parser_expression (parser);
30851 if (t == error_mark_node
30852 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30853 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30854 /*or_comma=*/false,
30855 /*consume_paren=*/true);
30857 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
30858 "num_tasks", location);
30860 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
30861 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
30862 OMP_CLAUSE_CHAIN (c) = list;
30864 return c;
30867 /* OpenMP 4.5:
30868 grainsize ( expression ) */
30870 static tree
30871 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
30872 location_t location)
30874 tree t, c;
30876 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30877 return list;
30879 t = cp_parser_expression (parser);
30881 if (t == error_mark_node
30882 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30883 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30884 /*or_comma=*/false,
30885 /*consume_paren=*/true);
30887 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
30888 "grainsize", location);
30890 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
30891 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
30892 OMP_CLAUSE_CHAIN (c) = list;
30894 return c;
30897 /* OpenMP 4.5:
30898 priority ( expression ) */
30900 static tree
30901 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
30902 location_t location)
30904 tree t, c;
30906 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30907 return list;
30909 t = cp_parser_expression (parser);
30911 if (t == error_mark_node
30912 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30913 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30914 /*or_comma=*/false,
30915 /*consume_paren=*/true);
30917 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
30918 "priority", location);
30920 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
30921 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
30922 OMP_CLAUSE_CHAIN (c) = list;
30924 return c;
30927 /* OpenMP 4.5:
30928 hint ( expression ) */
30930 static tree
30931 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
30932 location_t location)
30934 tree t, c;
30936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30937 return list;
30939 t = cp_parser_expression (parser);
30941 if (t == error_mark_node
30942 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
30944 /*or_comma=*/false,
30945 /*consume_paren=*/true);
30947 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
30949 c = build_omp_clause (location, OMP_CLAUSE_HINT);
30950 OMP_CLAUSE_HINT_EXPR (c) = t;
30951 OMP_CLAUSE_CHAIN (c) = list;
30953 return c;
30956 /* OpenMP 4.5:
30957 defaultmap ( tofrom : scalar ) */
30959 static tree
30960 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
30961 location_t location)
30963 tree c, id;
30964 const char *p;
30966 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30967 return list;
30969 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30971 cp_parser_error (parser, "expected %<tofrom%>");
30972 goto out_err;
30974 id = cp_lexer_peek_token (parser->lexer)->u.value;
30975 p = IDENTIFIER_POINTER (id);
30976 if (strcmp (p, "tofrom") != 0)
30978 cp_parser_error (parser, "expected %<tofrom%>");
30979 goto out_err;
30981 cp_lexer_consume_token (parser->lexer);
30982 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30983 goto out_err;
30985 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30987 cp_parser_error (parser, "expected %<scalar%>");
30988 goto out_err;
30990 id = cp_lexer_peek_token (parser->lexer)->u.value;
30991 p = IDENTIFIER_POINTER (id);
30992 if (strcmp (p, "scalar") != 0)
30994 cp_parser_error (parser, "expected %<scalar%>");
30995 goto out_err;
30997 cp_lexer_consume_token (parser->lexer);
30998 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30999 goto out_err;
31001 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31002 location);
31004 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31005 OMP_CLAUSE_CHAIN (c) = list;
31006 return c;
31008 out_err:
31009 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31010 /*or_comma=*/false,
31011 /*consume_paren=*/true);
31012 return list;
31015 /* OpenMP 2.5:
31016 ordered
31018 OpenMP 4.5:
31019 ordered ( constant-expression ) */
31021 static tree
31022 cp_parser_omp_clause_ordered (cp_parser *parser,
31023 tree list, location_t location)
31025 tree c, num = NULL_TREE;
31026 HOST_WIDE_INT n;
31028 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31029 "ordered", location);
31031 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31033 cp_lexer_consume_token (parser->lexer);
31035 num = cp_parser_constant_expression (parser);
31037 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31038 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31039 /*or_comma=*/false,
31040 /*consume_paren=*/true);
31042 if (num == error_mark_node)
31043 return list;
31044 num = fold_non_dependent_expr (num);
31045 if (!tree_fits_shwi_p (num)
31046 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31047 || (n = tree_to_shwi (num)) <= 0
31048 || (int) n != n)
31050 error_at (location,
31051 "ordered argument needs positive constant integer "
31052 "expression");
31053 return list;
31057 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31058 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31059 OMP_CLAUSE_CHAIN (c) = list;
31060 return c;
31063 /* OpenMP 2.5:
31064 reduction ( reduction-operator : variable-list )
31066 reduction-operator:
31067 One of: + * - & ^ | && ||
31069 OpenMP 3.1:
31071 reduction-operator:
31072 One of: + * - & ^ | && || min max
31074 OpenMP 4.0:
31076 reduction-operator:
31077 One of: + * - & ^ | && ||
31078 id-expression */
31080 static tree
31081 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31083 enum tree_code code = ERROR_MARK;
31084 tree nlist, c, id = NULL_TREE;
31086 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31087 return list;
31089 switch (cp_lexer_peek_token (parser->lexer)->type)
31091 case CPP_PLUS: code = PLUS_EXPR; break;
31092 case CPP_MULT: code = MULT_EXPR; break;
31093 case CPP_MINUS: code = MINUS_EXPR; break;
31094 case CPP_AND: code = BIT_AND_EXPR; break;
31095 case CPP_XOR: code = BIT_XOR_EXPR; break;
31096 case CPP_OR: code = BIT_IOR_EXPR; break;
31097 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31098 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
31099 default: break;
31102 if (code != ERROR_MARK)
31103 cp_lexer_consume_token (parser->lexer);
31104 else
31106 bool saved_colon_corrects_to_scope_p;
31107 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31108 parser->colon_corrects_to_scope_p = false;
31109 id = cp_parser_id_expression (parser, /*template_p=*/false,
31110 /*check_dependency_p=*/true,
31111 /*template_p=*/NULL,
31112 /*declarator_p=*/false,
31113 /*optional_p=*/false);
31114 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31115 if (identifier_p (id))
31117 const char *p = IDENTIFIER_POINTER (id);
31119 if (strcmp (p, "min") == 0)
31120 code = MIN_EXPR;
31121 else if (strcmp (p, "max") == 0)
31122 code = MAX_EXPR;
31123 else if (id == ansi_opname (PLUS_EXPR))
31124 code = PLUS_EXPR;
31125 else if (id == ansi_opname (MULT_EXPR))
31126 code = MULT_EXPR;
31127 else if (id == ansi_opname (MINUS_EXPR))
31128 code = MINUS_EXPR;
31129 else if (id == ansi_opname (BIT_AND_EXPR))
31130 code = BIT_AND_EXPR;
31131 else if (id == ansi_opname (BIT_IOR_EXPR))
31132 code = BIT_IOR_EXPR;
31133 else if (id == ansi_opname (BIT_XOR_EXPR))
31134 code = BIT_XOR_EXPR;
31135 else if (id == ansi_opname (TRUTH_ANDIF_EXPR))
31136 code = TRUTH_ANDIF_EXPR;
31137 else if (id == ansi_opname (TRUTH_ORIF_EXPR))
31138 code = TRUTH_ORIF_EXPR;
31139 id = omp_reduction_id (code, id, NULL_TREE);
31140 tree scope = parser->scope;
31141 if (scope)
31142 id = build_qualified_name (NULL_TREE, scope, id, false);
31143 parser->scope = NULL_TREE;
31144 parser->qualifying_scope = NULL_TREE;
31145 parser->object_scope = NULL_TREE;
31147 else
31149 error ("invalid reduction-identifier");
31150 resync_fail:
31151 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31152 /*or_comma=*/false,
31153 /*consume_paren=*/true);
31154 return list;
31158 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31159 goto resync_fail;
31161 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
31162 NULL);
31163 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31165 OMP_CLAUSE_REDUCTION_CODE (c) = code;
31166 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
31169 return nlist;
31172 /* OpenMP 2.5:
31173 schedule ( schedule-kind )
31174 schedule ( schedule-kind , expression )
31176 schedule-kind:
31177 static | dynamic | guided | runtime | auto
31179 OpenMP 4.5:
31180 schedule ( schedule-modifier : schedule-kind )
31181 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
31183 schedule-modifier:
31184 simd
31185 monotonic
31186 nonmonotonic */
31188 static tree
31189 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
31191 tree c, t;
31192 int modifiers = 0, nmodifiers = 0;
31194 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31195 return list;
31197 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
31199 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31201 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31202 const char *p = IDENTIFIER_POINTER (id);
31203 if (strcmp ("simd", p) == 0)
31204 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
31205 else if (strcmp ("monotonic", p) == 0)
31206 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
31207 else if (strcmp ("nonmonotonic", p) == 0)
31208 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
31209 else
31210 break;
31211 cp_lexer_consume_token (parser->lexer);
31212 if (nmodifiers++ == 0
31213 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31214 cp_lexer_consume_token (parser->lexer);
31215 else
31217 cp_parser_require (parser, CPP_COLON, RT_COLON);
31218 break;
31222 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31224 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31225 const char *p = IDENTIFIER_POINTER (id);
31227 switch (p[0])
31229 case 'd':
31230 if (strcmp ("dynamic", p) != 0)
31231 goto invalid_kind;
31232 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
31233 break;
31235 case 'g':
31236 if (strcmp ("guided", p) != 0)
31237 goto invalid_kind;
31238 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
31239 break;
31241 case 'r':
31242 if (strcmp ("runtime", p) != 0)
31243 goto invalid_kind;
31244 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
31245 break;
31247 default:
31248 goto invalid_kind;
31251 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31252 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
31253 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31254 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
31255 else
31256 goto invalid_kind;
31257 cp_lexer_consume_token (parser->lexer);
31259 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
31260 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31261 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
31262 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
31264 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
31265 "specified");
31266 modifiers = 0;
31269 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31271 cp_token *token;
31272 cp_lexer_consume_token (parser->lexer);
31274 token = cp_lexer_peek_token (parser->lexer);
31275 t = cp_parser_assignment_expression (parser);
31277 if (t == error_mark_node)
31278 goto resync_fail;
31279 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
31280 error_at (token->location, "schedule %<runtime%> does not take "
31281 "a %<chunk_size%> parameter");
31282 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
31283 error_at (token->location, "schedule %<auto%> does not take "
31284 "a %<chunk_size%> parameter");
31285 else
31286 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
31288 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31289 goto resync_fail;
31291 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31292 goto resync_fail;
31294 OMP_CLAUSE_SCHEDULE_KIND (c)
31295 = (enum omp_clause_schedule_kind)
31296 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
31298 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
31299 OMP_CLAUSE_CHAIN (c) = list;
31300 return c;
31302 invalid_kind:
31303 cp_parser_error (parser, "invalid schedule kind");
31304 resync_fail:
31305 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31306 /*or_comma=*/false,
31307 /*consume_paren=*/true);
31308 return list;
31311 /* OpenMP 3.0:
31312 untied */
31314 static tree
31315 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
31316 tree list, location_t location)
31318 tree c;
31320 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
31322 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
31323 OMP_CLAUSE_CHAIN (c) = list;
31324 return c;
31327 /* OpenMP 4.0:
31328 inbranch
31329 notinbranch */
31331 static tree
31332 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
31333 tree list, location_t location)
31335 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31336 tree c = build_omp_clause (location, code);
31337 OMP_CLAUSE_CHAIN (c) = list;
31338 return c;
31341 /* OpenMP 4.0:
31342 parallel
31344 sections
31345 taskgroup */
31347 static tree
31348 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
31349 enum omp_clause_code code,
31350 tree list, location_t location)
31352 tree c = build_omp_clause (location, code);
31353 OMP_CLAUSE_CHAIN (c) = list;
31354 return c;
31357 /* OpenMP 4.5:
31358 nogroup */
31360 static tree
31361 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
31362 tree list, location_t location)
31364 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
31365 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
31366 OMP_CLAUSE_CHAIN (c) = list;
31367 return c;
31370 /* OpenMP 4.5:
31371 simd
31372 threads */
31374 static tree
31375 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
31376 enum omp_clause_code code,
31377 tree list, location_t location)
31379 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31380 tree c = build_omp_clause (location, code);
31381 OMP_CLAUSE_CHAIN (c) = list;
31382 return c;
31385 /* OpenMP 4.0:
31386 num_teams ( expression ) */
31388 static tree
31389 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
31390 location_t location)
31392 tree t, c;
31394 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31395 return list;
31397 t = cp_parser_expression (parser);
31399 if (t == error_mark_node
31400 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31401 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31402 /*or_comma=*/false,
31403 /*consume_paren=*/true);
31405 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
31406 "num_teams", location);
31408 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
31409 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
31410 OMP_CLAUSE_CHAIN (c) = list;
31412 return c;
31415 /* OpenMP 4.0:
31416 thread_limit ( expression ) */
31418 static tree
31419 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
31420 location_t location)
31422 tree t, c;
31424 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31425 return list;
31427 t = cp_parser_expression (parser);
31429 if (t == error_mark_node
31430 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31431 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31432 /*or_comma=*/false,
31433 /*consume_paren=*/true);
31435 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
31436 "thread_limit", location);
31438 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
31439 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
31440 OMP_CLAUSE_CHAIN (c) = list;
31442 return c;
31445 /* OpenMP 4.0:
31446 aligned ( variable-list )
31447 aligned ( variable-list : constant-expression ) */
31449 static tree
31450 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
31452 tree nlist, c, alignment = NULL_TREE;
31453 bool colon;
31455 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31456 return list;
31458 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
31459 &colon);
31461 if (colon)
31463 alignment = cp_parser_constant_expression (parser);
31465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31467 /*or_comma=*/false,
31468 /*consume_paren=*/true);
31470 if (alignment == error_mark_node)
31471 alignment = NULL_TREE;
31474 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31475 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
31477 return nlist;
31480 /* OpenMP 4.0:
31481 linear ( variable-list )
31482 linear ( variable-list : expression )
31484 OpenMP 4.5:
31485 linear ( modifier ( variable-list ) )
31486 linear ( modifier ( variable-list ) : expression ) */
31488 static tree
31489 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
31490 bool is_cilk_simd_fn, bool declare_simd)
31492 tree nlist, c, step = integer_one_node;
31493 bool colon;
31494 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
31496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31497 return list;
31499 if (!is_cilk_simd_fn
31500 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31502 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31503 const char *p = IDENTIFIER_POINTER (id);
31505 if (strcmp ("ref", p) == 0)
31506 kind = OMP_CLAUSE_LINEAR_REF;
31507 else if (strcmp ("val", p) == 0)
31508 kind = OMP_CLAUSE_LINEAR_VAL;
31509 else if (strcmp ("uval", p) == 0)
31510 kind = OMP_CLAUSE_LINEAR_UVAL;
31511 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
31512 cp_lexer_consume_token (parser->lexer);
31513 else
31514 kind = OMP_CLAUSE_LINEAR_DEFAULT;
31517 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
31518 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
31519 &colon);
31520 else
31522 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
31523 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
31524 if (colon)
31525 cp_parser_require (parser, CPP_COLON, RT_COLON);
31526 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31527 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31528 /*or_comma=*/false,
31529 /*consume_paren=*/true);
31532 if (colon)
31534 step = NULL_TREE;
31535 if (declare_simd
31536 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31537 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
31539 cp_token *token = cp_lexer_peek_token (parser->lexer);
31540 cp_parser_parse_tentatively (parser);
31541 step = cp_parser_id_expression (parser, /*template_p=*/false,
31542 /*check_dependency_p=*/true,
31543 /*template_p=*/NULL,
31544 /*declarator_p=*/false,
31545 /*optional_p=*/false);
31546 if (step != error_mark_node)
31547 step = cp_parser_lookup_name_simple (parser, step, token->location);
31548 if (step == error_mark_node)
31550 step = NULL_TREE;
31551 cp_parser_abort_tentative_parse (parser);
31553 else if (!cp_parser_parse_definitely (parser))
31554 step = NULL_TREE;
31556 if (!step)
31557 step = cp_parser_expression (parser);
31559 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
31561 sorry ("using parameters for %<linear%> step is not supported yet");
31562 step = integer_one_node;
31564 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31565 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31566 /*or_comma=*/false,
31567 /*consume_paren=*/true);
31569 if (step == error_mark_node)
31570 return list;
31573 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31575 OMP_CLAUSE_LINEAR_STEP (c) = step;
31576 OMP_CLAUSE_LINEAR_KIND (c) = kind;
31579 return nlist;
31582 /* OpenMP 4.0:
31583 safelen ( constant-expression ) */
31585 static tree
31586 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
31587 location_t location)
31589 tree t, c;
31591 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31592 return list;
31594 t = cp_parser_constant_expression (parser);
31596 if (t == error_mark_node
31597 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31599 /*or_comma=*/false,
31600 /*consume_paren=*/true);
31602 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
31604 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
31605 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
31606 OMP_CLAUSE_CHAIN (c) = list;
31608 return c;
31611 /* OpenMP 4.0:
31612 simdlen ( constant-expression ) */
31614 static tree
31615 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
31616 location_t location)
31618 tree t, c;
31620 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31621 return list;
31623 t = cp_parser_constant_expression (parser);
31625 if (t == error_mark_node
31626 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31627 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31628 /*or_comma=*/false,
31629 /*consume_paren=*/true);
31631 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
31633 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
31634 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
31635 OMP_CLAUSE_CHAIN (c) = list;
31637 return c;
31640 /* OpenMP 4.5:
31641 vec:
31642 identifier [+/- integer]
31643 vec , identifier [+/- integer]
31646 static tree
31647 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
31648 tree list)
31650 tree vec = NULL;
31652 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
31654 cp_parser_error (parser, "expected identifier");
31655 return list;
31658 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31660 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
31661 tree t, identifier = cp_parser_identifier (parser);
31662 tree addend = NULL;
31664 if (identifier == error_mark_node)
31665 t = error_mark_node;
31666 else
31668 t = cp_parser_lookup_name_simple
31669 (parser, identifier,
31670 cp_lexer_peek_token (parser->lexer)->location);
31671 if (t == error_mark_node)
31672 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
31673 id_loc);
31676 bool neg = false;
31677 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
31678 neg = true;
31679 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
31681 addend = integer_zero_node;
31682 goto add_to_vector;
31684 cp_lexer_consume_token (parser->lexer);
31686 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
31688 cp_parser_error (parser, "expected integer");
31689 return list;
31692 addend = cp_lexer_peek_token (parser->lexer)->u.value;
31693 if (TREE_CODE (addend) != INTEGER_CST)
31695 cp_parser_error (parser, "expected integer");
31696 return list;
31698 cp_lexer_consume_token (parser->lexer);
31700 add_to_vector:
31701 if (t != error_mark_node)
31703 vec = tree_cons (addend, t, vec);
31704 if (neg)
31705 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
31708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31709 break;
31711 cp_lexer_consume_token (parser->lexer);
31714 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
31716 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
31717 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
31718 OMP_CLAUSE_DECL (u) = nreverse (vec);
31719 OMP_CLAUSE_CHAIN (u) = list;
31720 return u;
31722 return list;
31725 /* OpenMP 4.0:
31726 depend ( depend-kind : variable-list )
31728 depend-kind:
31729 in | out | inout
31731 OpenMP 4.5:
31732 depend ( source )
31734 depend ( sink : vec ) */
31736 static tree
31737 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
31739 tree nlist, c;
31740 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
31742 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31743 return list;
31745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31747 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31748 const char *p = IDENTIFIER_POINTER (id);
31750 if (strcmp ("in", p) == 0)
31751 kind = OMP_CLAUSE_DEPEND_IN;
31752 else if (strcmp ("inout", p) == 0)
31753 kind = OMP_CLAUSE_DEPEND_INOUT;
31754 else if (strcmp ("out", p) == 0)
31755 kind = OMP_CLAUSE_DEPEND_OUT;
31756 else if (strcmp ("source", p) == 0)
31757 kind = OMP_CLAUSE_DEPEND_SOURCE;
31758 else if (strcmp ("sink", p) == 0)
31759 kind = OMP_CLAUSE_DEPEND_SINK;
31760 else
31761 goto invalid_kind;
31763 else
31764 goto invalid_kind;
31766 cp_lexer_consume_token (parser->lexer);
31768 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
31770 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
31771 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31772 OMP_CLAUSE_DECL (c) = NULL_TREE;
31773 OMP_CLAUSE_CHAIN (c) = list;
31774 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31775 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31776 /*or_comma=*/false,
31777 /*consume_paren=*/true);
31778 return c;
31781 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31782 goto resync_fail;
31784 if (kind == OMP_CLAUSE_DEPEND_SINK)
31785 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
31786 else
31788 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
31789 list, NULL);
31791 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31792 OMP_CLAUSE_DEPEND_KIND (c) = kind;
31794 return nlist;
31796 invalid_kind:
31797 cp_parser_error (parser, "invalid depend kind");
31798 resync_fail:
31799 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31800 /*or_comma=*/false,
31801 /*consume_paren=*/true);
31802 return list;
31805 /* OpenMP 4.0:
31806 map ( map-kind : variable-list )
31807 map ( variable-list )
31809 map-kind:
31810 alloc | to | from | tofrom
31812 OpenMP 4.5:
31813 map-kind:
31814 alloc | to | from | tofrom | release | delete
31816 map ( always [,] map-kind: variable-list ) */
31818 static tree
31819 cp_parser_omp_clause_map (cp_parser *parser, tree list)
31821 tree nlist, c;
31822 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
31823 bool always = false;
31825 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31826 return list;
31828 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31830 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31831 const char *p = IDENTIFIER_POINTER (id);
31833 if (strcmp ("always", p) == 0)
31835 int nth = 2;
31836 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
31837 nth++;
31838 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
31839 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
31840 == RID_DELETE))
31841 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
31842 == CPP_COLON))
31844 always = true;
31845 cp_lexer_consume_token (parser->lexer);
31846 if (nth == 3)
31847 cp_lexer_consume_token (parser->lexer);
31852 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
31853 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31856 const char *p = IDENTIFIER_POINTER (id);
31858 if (strcmp ("alloc", p) == 0)
31859 kind = GOMP_MAP_ALLOC;
31860 else if (strcmp ("to", p) == 0)
31861 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
31862 else if (strcmp ("from", p) == 0)
31863 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
31864 else if (strcmp ("tofrom", p) == 0)
31865 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
31866 else if (strcmp ("release", p) == 0)
31867 kind = GOMP_MAP_RELEASE;
31868 else
31870 cp_parser_error (parser, "invalid map kind");
31871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31872 /*or_comma=*/false,
31873 /*consume_paren=*/true);
31874 return list;
31876 cp_lexer_consume_token (parser->lexer);
31877 cp_lexer_consume_token (parser->lexer);
31879 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
31880 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
31882 kind = GOMP_MAP_DELETE;
31883 cp_lexer_consume_token (parser->lexer);
31884 cp_lexer_consume_token (parser->lexer);
31887 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
31888 NULL);
31890 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
31891 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31893 return nlist;
31896 /* OpenMP 4.0:
31897 device ( expression ) */
31899 static tree
31900 cp_parser_omp_clause_device (cp_parser *parser, tree list,
31901 location_t location)
31903 tree t, c;
31905 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31906 return list;
31908 t = cp_parser_expression (parser);
31910 if (t == error_mark_node
31911 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31912 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31913 /*or_comma=*/false,
31914 /*consume_paren=*/true);
31916 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
31917 "device", location);
31919 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
31920 OMP_CLAUSE_DEVICE_ID (c) = t;
31921 OMP_CLAUSE_CHAIN (c) = list;
31923 return c;
31926 /* OpenMP 4.0:
31927 dist_schedule ( static )
31928 dist_schedule ( static , expression ) */
31930 static tree
31931 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
31932 location_t location)
31934 tree c, t;
31936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31937 return list;
31939 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
31941 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
31942 goto invalid_kind;
31943 cp_lexer_consume_token (parser->lexer);
31945 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
31947 cp_lexer_consume_token (parser->lexer);
31949 t = cp_parser_assignment_expression (parser);
31951 if (t == error_mark_node)
31952 goto resync_fail;
31953 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
31955 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31956 goto resync_fail;
31958 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
31959 goto resync_fail;
31961 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
31962 location);
31963 OMP_CLAUSE_CHAIN (c) = list;
31964 return c;
31966 invalid_kind:
31967 cp_parser_error (parser, "invalid dist_schedule kind");
31968 resync_fail:
31969 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31970 /*or_comma=*/false,
31971 /*consume_paren=*/true);
31972 return list;
31975 /* OpenMP 4.0:
31976 proc_bind ( proc-bind-kind )
31978 proc-bind-kind:
31979 master | close | spread */
31981 static tree
31982 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
31983 location_t location)
31985 tree c;
31986 enum omp_clause_proc_bind_kind kind;
31988 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31989 return list;
31991 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31993 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31994 const char *p = IDENTIFIER_POINTER (id);
31996 if (strcmp ("master", p) == 0)
31997 kind = OMP_CLAUSE_PROC_BIND_MASTER;
31998 else if (strcmp ("close", p) == 0)
31999 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32000 else if (strcmp ("spread", p) == 0)
32001 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32002 else
32003 goto invalid_kind;
32005 else
32006 goto invalid_kind;
32008 cp_lexer_consume_token (parser->lexer);
32009 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32010 goto resync_fail;
32012 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32013 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32014 location);
32015 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32016 OMP_CLAUSE_CHAIN (c) = list;
32017 return c;
32019 invalid_kind:
32020 cp_parser_error (parser, "invalid depend kind");
32021 resync_fail:
32022 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32023 /*or_comma=*/false,
32024 /*consume_paren=*/true);
32025 return list;
32028 /* OpenACC:
32029 async [( int-expr )] */
32031 static tree
32032 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32034 tree c, t;
32035 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32037 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32039 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32041 cp_lexer_consume_token (parser->lexer);
32043 t = cp_parser_expression (parser);
32044 if (t == error_mark_node
32045 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32046 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32047 /*or_comma=*/false,
32048 /*consume_paren=*/true);
32051 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32053 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32054 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32055 OMP_CLAUSE_CHAIN (c) = list;
32056 list = c;
32058 return list;
32061 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32062 is a bitmask in MASK. Return the list of clauses found. */
32064 static tree
32065 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32066 const char *where, cp_token *pragma_tok,
32067 bool finish_p = true)
32069 tree clauses = NULL;
32070 bool first = true;
32072 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32074 location_t here;
32075 pragma_omp_clause c_kind;
32076 omp_clause_code code;
32077 const char *c_name;
32078 tree prev = clauses;
32080 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32081 cp_lexer_consume_token (parser->lexer);
32083 here = cp_lexer_peek_token (parser->lexer)->location;
32084 c_kind = cp_parser_omp_clause_name (parser);
32086 switch (c_kind)
32088 case PRAGMA_OACC_CLAUSE_ASYNC:
32089 clauses = cp_parser_oacc_clause_async (parser, clauses);
32090 c_name = "async";
32091 break;
32092 case PRAGMA_OACC_CLAUSE_AUTO:
32093 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32094 clauses, here);
32095 c_name = "auto";
32096 break;
32097 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32098 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
32099 c_name = "collapse";
32100 break;
32101 case PRAGMA_OACC_CLAUSE_COPY:
32102 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32103 c_name = "copy";
32104 break;
32105 case PRAGMA_OACC_CLAUSE_COPYIN:
32106 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32107 c_name = "copyin";
32108 break;
32109 case PRAGMA_OACC_CLAUSE_COPYOUT:
32110 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32111 c_name = "copyout";
32112 break;
32113 case PRAGMA_OACC_CLAUSE_CREATE:
32114 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32115 c_name = "create";
32116 break;
32117 case PRAGMA_OACC_CLAUSE_DELETE:
32118 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32119 c_name = "delete";
32120 break;
32121 case PRAGMA_OMP_CLAUSE_DEFAULT:
32122 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
32123 c_name = "default";
32124 break;
32125 case PRAGMA_OACC_CLAUSE_DEVICE:
32126 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32127 c_name = "device";
32128 break;
32129 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
32130 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
32131 c_name = "deviceptr";
32132 break;
32133 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
32134 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32135 c_name = "device_resident";
32136 break;
32137 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
32138 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32139 clauses);
32140 c_name = "firstprivate";
32141 break;
32142 case PRAGMA_OACC_CLAUSE_GANG:
32143 c_name = "gang";
32144 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
32145 c_name, clauses);
32146 break;
32147 case PRAGMA_OACC_CLAUSE_HOST:
32148 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32149 c_name = "host";
32150 break;
32151 case PRAGMA_OACC_CLAUSE_IF:
32152 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
32153 c_name = "if";
32154 break;
32155 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
32156 clauses = cp_parser_oacc_simple_clause (parser,
32157 OMP_CLAUSE_INDEPENDENT,
32158 clauses, here);
32159 c_name = "independent";
32160 break;
32161 case PRAGMA_OACC_CLAUSE_LINK:
32162 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32163 c_name = "link";
32164 break;
32165 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
32166 code = OMP_CLAUSE_NUM_GANGS;
32167 c_name = "num_gangs";
32168 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32169 clauses);
32170 break;
32171 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
32172 c_name = "num_workers";
32173 code = OMP_CLAUSE_NUM_WORKERS;
32174 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32175 clauses);
32176 break;
32177 case PRAGMA_OACC_CLAUSE_PRESENT:
32178 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32179 c_name = "present";
32180 break;
32181 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
32182 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32183 c_name = "present_or_copy";
32184 break;
32185 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
32186 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32187 c_name = "present_or_copyin";
32188 break;
32189 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
32190 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32191 c_name = "present_or_copyout";
32192 break;
32193 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
32194 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32195 c_name = "present_or_create";
32196 break;
32197 case PRAGMA_OACC_CLAUSE_PRIVATE:
32198 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32199 clauses);
32200 c_name = "private";
32201 break;
32202 case PRAGMA_OACC_CLAUSE_REDUCTION:
32203 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32204 c_name = "reduction";
32205 break;
32206 case PRAGMA_OACC_CLAUSE_SELF:
32207 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
32208 c_name = "self";
32209 break;
32210 case PRAGMA_OACC_CLAUSE_SEQ:
32211 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
32212 clauses, here);
32213 c_name = "seq";
32214 break;
32215 case PRAGMA_OACC_CLAUSE_TILE:
32216 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
32217 c_name = "tile";
32218 break;
32219 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
32220 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32221 clauses);
32222 c_name = "use_device";
32223 break;
32224 case PRAGMA_OACC_CLAUSE_VECTOR:
32225 c_name = "vector";
32226 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
32227 c_name, clauses);
32228 break;
32229 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
32230 c_name = "vector_length";
32231 code = OMP_CLAUSE_VECTOR_LENGTH;
32232 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
32233 clauses);
32234 break;
32235 case PRAGMA_OACC_CLAUSE_WAIT:
32236 clauses = cp_parser_oacc_clause_wait (parser, clauses);
32237 c_name = "wait";
32238 break;
32239 case PRAGMA_OACC_CLAUSE_WORKER:
32240 c_name = "worker";
32241 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
32242 c_name, clauses);
32243 break;
32244 default:
32245 cp_parser_error (parser, "expected %<#pragma acc%> clause");
32246 goto saw_error;
32249 first = false;
32251 if (((mask >> c_kind) & 1) == 0)
32253 /* Remove the invalid clause(s) from the list to avoid
32254 confusing the rest of the compiler. */
32255 clauses = prev;
32256 error_at (here, "%qs is not valid for %qs", c_name, where);
32260 saw_error:
32261 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32263 if (finish_p)
32264 return finish_omp_clauses (clauses, false);
32266 return clauses;
32269 /* Parse all OpenMP clauses. The set clauses allowed by the directive
32270 is a bitmask in MASK. Return the list of clauses found; the result
32271 of clause default goes in *pdefault. */
32273 static tree
32274 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
32275 const char *where, cp_token *pragma_tok,
32276 bool finish_p = true)
32278 tree clauses = NULL;
32279 bool first = true;
32280 cp_token *token = NULL;
32282 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32284 pragma_omp_clause c_kind;
32285 const char *c_name;
32286 tree prev = clauses;
32288 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32289 cp_lexer_consume_token (parser->lexer);
32291 token = cp_lexer_peek_token (parser->lexer);
32292 c_kind = cp_parser_omp_clause_name (parser);
32294 switch (c_kind)
32296 case PRAGMA_OMP_CLAUSE_COLLAPSE:
32297 clauses = cp_parser_omp_clause_collapse (parser, clauses,
32298 token->location);
32299 c_name = "collapse";
32300 break;
32301 case PRAGMA_OMP_CLAUSE_COPYIN:
32302 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
32303 c_name = "copyin";
32304 break;
32305 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
32306 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
32307 clauses);
32308 c_name = "copyprivate";
32309 break;
32310 case PRAGMA_OMP_CLAUSE_DEFAULT:
32311 clauses = cp_parser_omp_clause_default (parser, clauses,
32312 token->location, false);
32313 c_name = "default";
32314 break;
32315 case PRAGMA_OMP_CLAUSE_FINAL:
32316 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
32317 c_name = "final";
32318 break;
32319 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
32320 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
32321 clauses);
32322 c_name = "firstprivate";
32323 break;
32324 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
32325 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
32326 token->location);
32327 c_name = "grainsize";
32328 break;
32329 case PRAGMA_OMP_CLAUSE_HINT:
32330 clauses = cp_parser_omp_clause_hint (parser, clauses,
32331 token->location);
32332 c_name = "hint";
32333 break;
32334 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
32335 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
32336 token->location);
32337 c_name = "defaultmap";
32338 break;
32339 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
32340 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
32341 clauses);
32342 c_name = "use_device_ptr";
32343 break;
32344 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
32345 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
32346 clauses);
32347 c_name = "is_device_ptr";
32348 break;
32349 case PRAGMA_OMP_CLAUSE_IF:
32350 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
32351 true);
32352 c_name = "if";
32353 break;
32354 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
32355 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
32356 clauses);
32357 c_name = "lastprivate";
32358 break;
32359 case PRAGMA_OMP_CLAUSE_MERGEABLE:
32360 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
32361 token->location);
32362 c_name = "mergeable";
32363 break;
32364 case PRAGMA_OMP_CLAUSE_NOWAIT:
32365 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
32366 c_name = "nowait";
32367 break;
32368 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
32369 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
32370 token->location);
32371 c_name = "num_tasks";
32372 break;
32373 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
32374 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
32375 token->location);
32376 c_name = "num_threads";
32377 break;
32378 case PRAGMA_OMP_CLAUSE_ORDERED:
32379 clauses = cp_parser_omp_clause_ordered (parser, clauses,
32380 token->location);
32381 c_name = "ordered";
32382 break;
32383 case PRAGMA_OMP_CLAUSE_PRIORITY:
32384 clauses = cp_parser_omp_clause_priority (parser, clauses,
32385 token->location);
32386 c_name = "priority";
32387 break;
32388 case PRAGMA_OMP_CLAUSE_PRIVATE:
32389 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
32390 clauses);
32391 c_name = "private";
32392 break;
32393 case PRAGMA_OMP_CLAUSE_REDUCTION:
32394 clauses = cp_parser_omp_clause_reduction (parser, clauses);
32395 c_name = "reduction";
32396 break;
32397 case PRAGMA_OMP_CLAUSE_SCHEDULE:
32398 clauses = cp_parser_omp_clause_schedule (parser, clauses,
32399 token->location);
32400 c_name = "schedule";
32401 break;
32402 case PRAGMA_OMP_CLAUSE_SHARED:
32403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
32404 clauses);
32405 c_name = "shared";
32406 break;
32407 case PRAGMA_OMP_CLAUSE_UNTIED:
32408 clauses = cp_parser_omp_clause_untied (parser, clauses,
32409 token->location);
32410 c_name = "untied";
32411 break;
32412 case PRAGMA_OMP_CLAUSE_INBRANCH:
32413 case PRAGMA_CILK_CLAUSE_MASK:
32414 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
32415 clauses, token->location);
32416 c_name = "inbranch";
32417 break;
32418 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
32419 case PRAGMA_CILK_CLAUSE_NOMASK:
32420 clauses = cp_parser_omp_clause_branch (parser,
32421 OMP_CLAUSE_NOTINBRANCH,
32422 clauses, token->location);
32423 c_name = "notinbranch";
32424 break;
32425 case PRAGMA_OMP_CLAUSE_PARALLEL:
32426 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
32427 clauses, token->location);
32428 c_name = "parallel";
32429 if (!first)
32431 clause_not_first:
32432 error_at (token->location, "%qs must be the first clause of %qs",
32433 c_name, where);
32434 clauses = prev;
32436 break;
32437 case PRAGMA_OMP_CLAUSE_FOR:
32438 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
32439 clauses, token->location);
32440 c_name = "for";
32441 if (!first)
32442 goto clause_not_first;
32443 break;
32444 case PRAGMA_OMP_CLAUSE_SECTIONS:
32445 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
32446 clauses, token->location);
32447 c_name = "sections";
32448 if (!first)
32449 goto clause_not_first;
32450 break;
32451 case PRAGMA_OMP_CLAUSE_TASKGROUP:
32452 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
32453 clauses, token->location);
32454 c_name = "taskgroup";
32455 if (!first)
32456 goto clause_not_first;
32457 break;
32458 case PRAGMA_OMP_CLAUSE_LINK:
32459 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
32460 c_name = "to";
32461 break;
32462 case PRAGMA_OMP_CLAUSE_TO:
32463 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
32464 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
32465 clauses);
32466 else
32467 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
32468 c_name = "to";
32469 break;
32470 case PRAGMA_OMP_CLAUSE_FROM:
32471 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
32472 c_name = "from";
32473 break;
32474 case PRAGMA_OMP_CLAUSE_UNIFORM:
32475 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
32476 clauses);
32477 c_name = "uniform";
32478 break;
32479 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
32480 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
32481 token->location);
32482 c_name = "num_teams";
32483 break;
32484 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
32485 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
32486 token->location);
32487 c_name = "thread_limit";
32488 break;
32489 case PRAGMA_OMP_CLAUSE_ALIGNED:
32490 clauses = cp_parser_omp_clause_aligned (parser, clauses);
32491 c_name = "aligned";
32492 break;
32493 case PRAGMA_OMP_CLAUSE_LINEAR:
32495 bool cilk_simd_fn = false, declare_simd = false;
32496 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
32497 cilk_simd_fn = true;
32498 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
32499 declare_simd = true;
32500 clauses = cp_parser_omp_clause_linear (parser, clauses,
32501 cilk_simd_fn, declare_simd);
32503 c_name = "linear";
32504 break;
32505 case PRAGMA_OMP_CLAUSE_DEPEND:
32506 clauses = cp_parser_omp_clause_depend (parser, clauses,
32507 token->location);
32508 c_name = "depend";
32509 break;
32510 case PRAGMA_OMP_CLAUSE_MAP:
32511 clauses = cp_parser_omp_clause_map (parser, clauses);
32512 c_name = "map";
32513 break;
32514 case PRAGMA_OMP_CLAUSE_DEVICE:
32515 clauses = cp_parser_omp_clause_device (parser, clauses,
32516 token->location);
32517 c_name = "device";
32518 break;
32519 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
32520 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
32521 token->location);
32522 c_name = "dist_schedule";
32523 break;
32524 case PRAGMA_OMP_CLAUSE_PROC_BIND:
32525 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
32526 token->location);
32527 c_name = "proc_bind";
32528 break;
32529 case PRAGMA_OMP_CLAUSE_SAFELEN:
32530 clauses = cp_parser_omp_clause_safelen (parser, clauses,
32531 token->location);
32532 c_name = "safelen";
32533 break;
32534 case PRAGMA_OMP_CLAUSE_SIMDLEN:
32535 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
32536 token->location);
32537 c_name = "simdlen";
32538 break;
32539 case PRAGMA_OMP_CLAUSE_NOGROUP:
32540 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
32541 token->location);
32542 c_name = "nogroup";
32543 break;
32544 case PRAGMA_OMP_CLAUSE_THREADS:
32545 clauses
32546 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
32547 clauses, token->location);
32548 c_name = "threads";
32549 break;
32550 case PRAGMA_OMP_CLAUSE_SIMD:
32551 clauses
32552 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
32553 clauses, token->location);
32554 c_name = "simd";
32555 break;
32556 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
32557 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
32558 c_name = "simdlen";
32559 break;
32560 default:
32561 cp_parser_error (parser, "expected %<#pragma omp%> clause");
32562 goto saw_error;
32565 first = false;
32567 if (((mask >> c_kind) & 1) == 0)
32569 /* Remove the invalid clause(s) from the list to avoid
32570 confusing the rest of the compiler. */
32571 clauses = prev;
32572 error_at (token->location, "%qs is not valid for %qs", c_name, where);
32575 saw_error:
32576 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
32577 no reason to skip to the end. */
32578 if (!(flag_cilkplus && pragma_tok == NULL))
32579 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
32580 if (finish_p)
32582 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
32583 return finish_omp_clauses (clauses, false, true);
32584 else
32585 return finish_omp_clauses (clauses, true);
32587 return clauses;
32590 /* OpenMP 2.5:
32591 structured-block:
32592 statement
32594 In practice, we're also interested in adding the statement to an
32595 outer node. So it is convenient if we work around the fact that
32596 cp_parser_statement calls add_stmt. */
32598 static unsigned
32599 cp_parser_begin_omp_structured_block (cp_parser *parser)
32601 unsigned save = parser->in_statement;
32603 /* Only move the values to IN_OMP_BLOCK if they weren't false.
32604 This preserves the "not within loop or switch" style error messages
32605 for nonsense cases like
32606 void foo() {
32607 #pragma omp single
32608 break;
32611 if (parser->in_statement)
32612 parser->in_statement = IN_OMP_BLOCK;
32614 return save;
32617 static void
32618 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
32620 parser->in_statement = save;
32623 static tree
32624 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
32626 tree stmt = begin_omp_structured_block ();
32627 unsigned int save = cp_parser_begin_omp_structured_block (parser);
32629 cp_parser_statement (parser, NULL_TREE, false, if_p);
32631 cp_parser_end_omp_structured_block (parser, save);
32632 return finish_omp_structured_block (stmt);
32635 /* OpenMP 2.5:
32636 # pragma omp atomic new-line
32637 expression-stmt
32639 expression-stmt:
32640 x binop= expr | x++ | ++x | x-- | --x
32641 binop:
32642 +, *, -, /, &, ^, |, <<, >>
32644 where x is an lvalue expression with scalar type.
32646 OpenMP 3.1:
32647 # pragma omp atomic new-line
32648 update-stmt
32650 # pragma omp atomic read new-line
32651 read-stmt
32653 # pragma omp atomic write new-line
32654 write-stmt
32656 # pragma omp atomic update new-line
32657 update-stmt
32659 # pragma omp atomic capture new-line
32660 capture-stmt
32662 # pragma omp atomic capture new-line
32663 capture-block
32665 read-stmt:
32666 v = x
32667 write-stmt:
32668 x = expr
32669 update-stmt:
32670 expression-stmt | x = x binop expr
32671 capture-stmt:
32672 v = expression-stmt
32673 capture-block:
32674 { v = x; update-stmt; } | { update-stmt; v = x; }
32676 OpenMP 4.0:
32677 update-stmt:
32678 expression-stmt | x = x binop expr | x = expr binop x
32679 capture-stmt:
32680 v = update-stmt
32681 capture-block:
32682 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
32684 where x and v are lvalue expressions with scalar type. */
32686 static void
32687 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
32689 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
32690 tree rhs1 = NULL_TREE, orig_lhs;
32691 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
32692 bool structured_block = false;
32693 bool seq_cst = false;
32695 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32697 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32698 const char *p = IDENTIFIER_POINTER (id);
32700 if (!strcmp (p, "seq_cst"))
32702 seq_cst = true;
32703 cp_lexer_consume_token (parser->lexer);
32704 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32705 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32706 cp_lexer_consume_token (parser->lexer);
32709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32711 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32712 const char *p = IDENTIFIER_POINTER (id);
32714 if (!strcmp (p, "read"))
32715 code = OMP_ATOMIC_READ;
32716 else if (!strcmp (p, "write"))
32717 code = NOP_EXPR;
32718 else if (!strcmp (p, "update"))
32719 code = OMP_ATOMIC;
32720 else if (!strcmp (p, "capture"))
32721 code = OMP_ATOMIC_CAPTURE_NEW;
32722 else
32723 p = NULL;
32724 if (p)
32725 cp_lexer_consume_token (parser->lexer);
32727 if (!seq_cst)
32729 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
32730 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
32731 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, "seq_cst"))
32740 seq_cst = true;
32741 cp_lexer_consume_token (parser->lexer);
32745 cp_parser_require_pragma_eol (parser, pragma_tok);
32747 switch (code)
32749 case OMP_ATOMIC_READ:
32750 case NOP_EXPR: /* atomic write */
32751 v = cp_parser_unary_expression (parser);
32752 if (v == error_mark_node)
32753 goto saw_error;
32754 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32755 goto saw_error;
32756 if (code == NOP_EXPR)
32757 lhs = cp_parser_expression (parser);
32758 else
32759 lhs = cp_parser_unary_expression (parser);
32760 if (lhs == error_mark_node)
32761 goto saw_error;
32762 if (code == NOP_EXPR)
32764 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
32765 opcode. */
32766 code = OMP_ATOMIC;
32767 rhs = lhs;
32768 lhs = v;
32769 v = NULL_TREE;
32771 goto done;
32772 case OMP_ATOMIC_CAPTURE_NEW:
32773 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
32775 cp_lexer_consume_token (parser->lexer);
32776 structured_block = true;
32778 else
32780 v = cp_parser_unary_expression (parser);
32781 if (v == error_mark_node)
32782 goto saw_error;
32783 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
32784 goto saw_error;
32786 default:
32787 break;
32790 restart:
32791 lhs = cp_parser_unary_expression (parser);
32792 orig_lhs = lhs;
32793 switch (TREE_CODE (lhs))
32795 case ERROR_MARK:
32796 goto saw_error;
32798 case POSTINCREMENT_EXPR:
32799 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32800 code = OMP_ATOMIC_CAPTURE_OLD;
32801 /* FALLTHROUGH */
32802 case PREINCREMENT_EXPR:
32803 lhs = TREE_OPERAND (lhs, 0);
32804 opcode = PLUS_EXPR;
32805 rhs = integer_one_node;
32806 break;
32808 case POSTDECREMENT_EXPR:
32809 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
32810 code = OMP_ATOMIC_CAPTURE_OLD;
32811 /* FALLTHROUGH */
32812 case PREDECREMENT_EXPR:
32813 lhs = TREE_OPERAND (lhs, 0);
32814 opcode = MINUS_EXPR;
32815 rhs = integer_one_node;
32816 break;
32818 case COMPOUND_EXPR:
32819 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
32820 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
32821 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
32822 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
32823 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
32824 (TREE_OPERAND (lhs, 1), 0), 0)))
32825 == BOOLEAN_TYPE)
32826 /* Undo effects of boolean_increment for post {in,de}crement. */
32827 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
32828 /* FALLTHRU */
32829 case MODIFY_EXPR:
32830 if (TREE_CODE (lhs) == MODIFY_EXPR
32831 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
32833 /* Undo effects of boolean_increment. */
32834 if (integer_onep (TREE_OPERAND (lhs, 1)))
32836 /* This is pre or post increment. */
32837 rhs = TREE_OPERAND (lhs, 1);
32838 lhs = TREE_OPERAND (lhs, 0);
32839 opcode = NOP_EXPR;
32840 if (code == OMP_ATOMIC_CAPTURE_NEW
32841 && !structured_block
32842 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
32843 code = OMP_ATOMIC_CAPTURE_OLD;
32844 break;
32847 /* FALLTHRU */
32848 default:
32849 switch (cp_lexer_peek_token (parser->lexer)->type)
32851 case CPP_MULT_EQ:
32852 opcode = MULT_EXPR;
32853 break;
32854 case CPP_DIV_EQ:
32855 opcode = TRUNC_DIV_EXPR;
32856 break;
32857 case CPP_PLUS_EQ:
32858 opcode = PLUS_EXPR;
32859 break;
32860 case CPP_MINUS_EQ:
32861 opcode = MINUS_EXPR;
32862 break;
32863 case CPP_LSHIFT_EQ:
32864 opcode = LSHIFT_EXPR;
32865 break;
32866 case CPP_RSHIFT_EQ:
32867 opcode = RSHIFT_EXPR;
32868 break;
32869 case CPP_AND_EQ:
32870 opcode = BIT_AND_EXPR;
32871 break;
32872 case CPP_OR_EQ:
32873 opcode = BIT_IOR_EXPR;
32874 break;
32875 case CPP_XOR_EQ:
32876 opcode = BIT_XOR_EXPR;
32877 break;
32878 case CPP_EQ:
32879 enum cp_parser_prec oprec;
32880 cp_token *token;
32881 cp_lexer_consume_token (parser->lexer);
32882 cp_parser_parse_tentatively (parser);
32883 rhs1 = cp_parser_simple_cast_expression (parser);
32884 if (rhs1 == error_mark_node)
32886 cp_parser_abort_tentative_parse (parser);
32887 cp_parser_simple_cast_expression (parser);
32888 goto saw_error;
32890 token = cp_lexer_peek_token (parser->lexer);
32891 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
32893 cp_parser_abort_tentative_parse (parser);
32894 cp_parser_parse_tentatively (parser);
32895 rhs = cp_parser_binary_expression (parser, false, true,
32896 PREC_NOT_OPERATOR, NULL);
32897 if (rhs == error_mark_node)
32899 cp_parser_abort_tentative_parse (parser);
32900 cp_parser_binary_expression (parser, false, true,
32901 PREC_NOT_OPERATOR, NULL);
32902 goto saw_error;
32904 switch (TREE_CODE (rhs))
32906 case MULT_EXPR:
32907 case TRUNC_DIV_EXPR:
32908 case RDIV_EXPR:
32909 case PLUS_EXPR:
32910 case MINUS_EXPR:
32911 case LSHIFT_EXPR:
32912 case RSHIFT_EXPR:
32913 case BIT_AND_EXPR:
32914 case BIT_IOR_EXPR:
32915 case BIT_XOR_EXPR:
32916 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
32918 if (cp_parser_parse_definitely (parser))
32920 opcode = TREE_CODE (rhs);
32921 rhs1 = TREE_OPERAND (rhs, 0);
32922 rhs = TREE_OPERAND (rhs, 1);
32923 goto stmt_done;
32925 else
32926 goto saw_error;
32928 break;
32929 default:
32930 break;
32932 cp_parser_abort_tentative_parse (parser);
32933 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
32935 rhs = cp_parser_expression (parser);
32936 if (rhs == error_mark_node)
32937 goto saw_error;
32938 opcode = NOP_EXPR;
32939 rhs1 = NULL_TREE;
32940 goto stmt_done;
32942 cp_parser_error (parser,
32943 "invalid form of %<#pragma omp atomic%>");
32944 goto saw_error;
32946 if (!cp_parser_parse_definitely (parser))
32947 goto saw_error;
32948 switch (token->type)
32950 case CPP_SEMICOLON:
32951 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
32953 code = OMP_ATOMIC_CAPTURE_OLD;
32954 v = lhs;
32955 lhs = NULL_TREE;
32956 lhs1 = rhs1;
32957 rhs1 = NULL_TREE;
32958 cp_lexer_consume_token (parser->lexer);
32959 goto restart;
32961 else if (structured_block)
32963 opcode = NOP_EXPR;
32964 rhs = rhs1;
32965 rhs1 = NULL_TREE;
32966 goto stmt_done;
32968 cp_parser_error (parser,
32969 "invalid form of %<#pragma omp atomic%>");
32970 goto saw_error;
32971 case CPP_MULT:
32972 opcode = MULT_EXPR;
32973 break;
32974 case CPP_DIV:
32975 opcode = TRUNC_DIV_EXPR;
32976 break;
32977 case CPP_PLUS:
32978 opcode = PLUS_EXPR;
32979 break;
32980 case CPP_MINUS:
32981 opcode = MINUS_EXPR;
32982 break;
32983 case CPP_LSHIFT:
32984 opcode = LSHIFT_EXPR;
32985 break;
32986 case CPP_RSHIFT:
32987 opcode = RSHIFT_EXPR;
32988 break;
32989 case CPP_AND:
32990 opcode = BIT_AND_EXPR;
32991 break;
32992 case CPP_OR:
32993 opcode = BIT_IOR_EXPR;
32994 break;
32995 case CPP_XOR:
32996 opcode = BIT_XOR_EXPR;
32997 break;
32998 default:
32999 cp_parser_error (parser,
33000 "invalid operator for %<#pragma omp atomic%>");
33001 goto saw_error;
33003 oprec = TOKEN_PRECEDENCE (token);
33004 gcc_assert (oprec != PREC_NOT_OPERATOR);
33005 if (commutative_tree_code (opcode))
33006 oprec = (enum cp_parser_prec) (oprec - 1);
33007 cp_lexer_consume_token (parser->lexer);
33008 rhs = cp_parser_binary_expression (parser, false, false,
33009 oprec, NULL);
33010 if (rhs == error_mark_node)
33011 goto saw_error;
33012 goto stmt_done;
33013 /* FALLTHROUGH */
33014 default:
33015 cp_parser_error (parser,
33016 "invalid operator for %<#pragma omp atomic%>");
33017 goto saw_error;
33019 cp_lexer_consume_token (parser->lexer);
33021 rhs = cp_parser_expression (parser);
33022 if (rhs == error_mark_node)
33023 goto saw_error;
33024 break;
33026 stmt_done:
33027 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33029 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33030 goto saw_error;
33031 v = cp_parser_unary_expression (parser);
33032 if (v == error_mark_node)
33033 goto saw_error;
33034 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33035 goto saw_error;
33036 lhs1 = cp_parser_unary_expression (parser);
33037 if (lhs1 == error_mark_node)
33038 goto saw_error;
33040 if (structured_block)
33042 cp_parser_consume_semicolon_at_end_of_statement (parser);
33043 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33045 done:
33046 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33047 if (!structured_block)
33048 cp_parser_consume_semicolon_at_end_of_statement (parser);
33049 return;
33051 saw_error:
33052 cp_parser_skip_to_end_of_block_or_statement (parser);
33053 if (structured_block)
33055 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33056 cp_lexer_consume_token (parser->lexer);
33057 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33059 cp_parser_skip_to_end_of_block_or_statement (parser);
33060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33061 cp_lexer_consume_token (parser->lexer);
33067 /* OpenMP 2.5:
33068 # pragma omp barrier new-line */
33070 static void
33071 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33073 cp_parser_require_pragma_eol (parser, pragma_tok);
33074 finish_omp_barrier ();
33077 /* OpenMP 2.5:
33078 # pragma omp critical [(name)] new-line
33079 structured-block
33081 OpenMP 4.5:
33082 # pragma omp critical [(name) [hint(expression)]] new-line
33083 structured-block */
33085 #define OMP_CRITICAL_CLAUSE_MASK \
33086 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33088 static tree
33089 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33091 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33093 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33095 cp_lexer_consume_token (parser->lexer);
33097 name = cp_parser_identifier (parser);
33099 if (name == error_mark_node
33100 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33101 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33102 /*or_comma=*/false,
33103 /*consume_paren=*/true);
33104 if (name == error_mark_node)
33105 name = NULL;
33107 clauses = cp_parser_omp_all_clauses (parser,
33108 OMP_CRITICAL_CLAUSE_MASK,
33109 "#pragma omp critical", pragma_tok);
33111 else
33112 cp_parser_require_pragma_eol (parser, pragma_tok);
33114 stmt = cp_parser_omp_structured_block (parser, if_p);
33115 return c_finish_omp_critical (input_location, stmt, name, clauses);
33118 /* OpenMP 2.5:
33119 # pragma omp flush flush-vars[opt] new-line
33121 flush-vars:
33122 ( variable-list ) */
33124 static void
33125 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
33127 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33128 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
33129 cp_parser_require_pragma_eol (parser, pragma_tok);
33131 finish_omp_flush ();
33134 /* Helper function, to parse omp for increment expression. */
33136 static tree
33137 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
33139 tree cond = cp_parser_binary_expression (parser, false, true,
33140 PREC_NOT_OPERATOR, NULL);
33141 if (cond == error_mark_node
33142 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33144 cp_parser_skip_to_end_of_statement (parser);
33145 return error_mark_node;
33148 switch (TREE_CODE (cond))
33150 case GT_EXPR:
33151 case GE_EXPR:
33152 case LT_EXPR:
33153 case LE_EXPR:
33154 break;
33155 case NE_EXPR:
33156 if (code == CILK_SIMD || code == CILK_FOR)
33157 break;
33158 /* Fall through: OpenMP disallows NE_EXPR. */
33159 default:
33160 return error_mark_node;
33163 /* If decl is an iterator, preserve LHS and RHS of the relational
33164 expr until finish_omp_for. */
33165 if (decl
33166 && (type_dependent_expression_p (decl)
33167 || CLASS_TYPE_P (TREE_TYPE (decl))))
33168 return cond;
33170 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
33171 TREE_CODE (cond),
33172 TREE_OPERAND (cond, 0), ERROR_MARK,
33173 TREE_OPERAND (cond, 1), ERROR_MARK,
33174 /*overload=*/NULL, tf_warning_or_error);
33177 /* Helper function, to parse omp for increment expression. */
33179 static tree
33180 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
33182 cp_token *token = cp_lexer_peek_token (parser->lexer);
33183 enum tree_code op;
33184 tree lhs, rhs;
33185 cp_id_kind idk;
33186 bool decl_first;
33188 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33190 op = (token->type == CPP_PLUS_PLUS
33191 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
33192 cp_lexer_consume_token (parser->lexer);
33193 lhs = cp_parser_simple_cast_expression (parser);
33194 if (lhs != decl
33195 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33196 return error_mark_node;
33197 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33200 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
33201 if (lhs != decl
33202 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
33203 return error_mark_node;
33205 token = cp_lexer_peek_token (parser->lexer);
33206 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
33208 op = (token->type == CPP_PLUS_PLUS
33209 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
33210 cp_lexer_consume_token (parser->lexer);
33211 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
33214 op = cp_parser_assignment_operator_opt (parser);
33215 if (op == ERROR_MARK)
33216 return error_mark_node;
33218 if (op != NOP_EXPR)
33220 rhs = cp_parser_assignment_expression (parser);
33221 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
33222 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33225 lhs = cp_parser_binary_expression (parser, false, false,
33226 PREC_ADDITIVE_EXPRESSION, NULL);
33227 token = cp_lexer_peek_token (parser->lexer);
33228 decl_first = (lhs == decl
33229 || (processing_template_decl && cp_tree_equal (lhs, decl)));
33230 if (decl_first)
33231 lhs = NULL_TREE;
33232 if (token->type != CPP_PLUS
33233 && token->type != CPP_MINUS)
33234 return error_mark_node;
33238 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
33239 cp_lexer_consume_token (parser->lexer);
33240 rhs = cp_parser_binary_expression (parser, false, false,
33241 PREC_ADDITIVE_EXPRESSION, NULL);
33242 token = cp_lexer_peek_token (parser->lexer);
33243 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
33245 if (lhs == NULL_TREE)
33247 if (op == PLUS_EXPR)
33248 lhs = rhs;
33249 else
33250 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
33251 tf_warning_or_error);
33253 else
33254 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
33255 ERROR_MARK, NULL, tf_warning_or_error);
33258 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
33260 if (!decl_first)
33262 if ((rhs != decl
33263 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
33264 || op == MINUS_EXPR)
33265 return error_mark_node;
33266 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
33268 else
33269 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
33271 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
33274 /* Parse the initialization statement of either an OpenMP for loop or
33275 a Cilk Plus for loop.
33277 Return true if the resulting construct should have an
33278 OMP_CLAUSE_PRIVATE added to it. */
33280 static tree
33281 cp_parser_omp_for_loop_init (cp_parser *parser,
33282 enum tree_code code,
33283 tree &this_pre_body,
33284 vec<tree, va_gc> *for_block,
33285 tree &init,
33286 tree &orig_init,
33287 tree &decl,
33288 tree &real_decl)
33290 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33291 return NULL_TREE;
33293 tree add_private_clause = NULL_TREE;
33295 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
33297 init-expr:
33298 var = lb
33299 integer-type var = lb
33300 random-access-iterator-type var = lb
33301 pointer-type var = lb
33303 cp_decl_specifier_seq type_specifiers;
33305 /* First, try to parse as an initialized declaration. See
33306 cp_parser_condition, from whence the bulk of this is copied. */
33308 cp_parser_parse_tentatively (parser);
33309 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
33310 /*is_trailing_return=*/false,
33311 &type_specifiers);
33312 if (cp_parser_parse_definitely (parser))
33314 /* If parsing a type specifier seq succeeded, then this
33315 MUST be a initialized declaration. */
33316 tree asm_specification, attributes;
33317 cp_declarator *declarator;
33319 declarator = cp_parser_declarator (parser,
33320 CP_PARSER_DECLARATOR_NAMED,
33321 /*ctor_dtor_or_conv_p=*/NULL,
33322 /*parenthesized_p=*/NULL,
33323 /*member_p=*/false,
33324 /*friend_p=*/false);
33325 attributes = cp_parser_attributes_opt (parser);
33326 asm_specification = cp_parser_asm_specification_opt (parser);
33328 if (declarator == cp_error_declarator)
33329 cp_parser_skip_to_end_of_statement (parser);
33331 else
33333 tree pushed_scope, auto_node;
33335 decl = start_decl (declarator, &type_specifiers,
33336 SD_INITIALIZED, attributes,
33337 /*prefix_attributes=*/NULL_TREE,
33338 &pushed_scope);
33340 auto_node = type_uses_auto (TREE_TYPE (decl));
33341 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
33343 if (cp_lexer_next_token_is (parser->lexer,
33344 CPP_OPEN_PAREN))
33346 if (code != CILK_SIMD && code != CILK_FOR)
33347 error ("parenthesized initialization is not allowed in "
33348 "OpenMP %<for%> loop");
33349 else
33350 error ("parenthesized initialization is "
33351 "not allowed in for-loop");
33353 else
33354 /* Trigger an error. */
33355 cp_parser_require (parser, CPP_EQ, RT_EQ);
33357 init = error_mark_node;
33358 cp_parser_skip_to_end_of_statement (parser);
33360 else if (CLASS_TYPE_P (TREE_TYPE (decl))
33361 || type_dependent_expression_p (decl)
33362 || auto_node)
33364 bool is_direct_init, is_non_constant_init;
33366 init = cp_parser_initializer (parser,
33367 &is_direct_init,
33368 &is_non_constant_init);
33370 if (auto_node)
33372 TREE_TYPE (decl)
33373 = do_auto_deduction (TREE_TYPE (decl), init,
33374 auto_node);
33376 if (!CLASS_TYPE_P (TREE_TYPE (decl))
33377 && !type_dependent_expression_p (decl))
33378 goto non_class;
33381 cp_finish_decl (decl, init, !is_non_constant_init,
33382 asm_specification,
33383 LOOKUP_ONLYCONVERTING);
33384 orig_init = init;
33385 if (CLASS_TYPE_P (TREE_TYPE (decl)))
33387 vec_safe_push (for_block, this_pre_body);
33388 init = NULL_TREE;
33390 else
33391 init = pop_stmt_list (this_pre_body);
33392 this_pre_body = NULL_TREE;
33394 else
33396 /* Consume '='. */
33397 cp_lexer_consume_token (parser->lexer);
33398 init = cp_parser_assignment_expression (parser);
33400 non_class:
33401 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
33402 init = error_mark_node;
33403 else
33404 cp_finish_decl (decl, NULL_TREE,
33405 /*init_const_expr_p=*/false,
33406 asm_specification,
33407 LOOKUP_ONLYCONVERTING);
33410 if (pushed_scope)
33411 pop_scope (pushed_scope);
33414 else
33416 cp_id_kind idk;
33417 /* If parsing a type specifier sequence failed, then
33418 this MUST be a simple expression. */
33419 if (code == CILK_FOR)
33420 error ("%<_Cilk_for%> allows expression instead of declaration only "
33421 "in C, not in C++");
33422 cp_parser_parse_tentatively (parser);
33423 decl = cp_parser_primary_expression (parser, false, false,
33424 false, &idk);
33425 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
33426 if (!cp_parser_error_occurred (parser)
33427 && decl
33428 && (TREE_CODE (decl) == COMPONENT_REF
33429 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
33431 cp_parser_abort_tentative_parse (parser);
33432 cp_parser_parse_tentatively (parser);
33433 cp_token *token = cp_lexer_peek_token (parser->lexer);
33434 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
33435 /*check_dependency_p=*/true,
33436 /*template_p=*/NULL,
33437 /*declarator_p=*/false,
33438 /*optional_p=*/false);
33439 if (name != error_mark_node
33440 && last_tok == cp_lexer_peek_token (parser->lexer))
33442 decl = cp_parser_lookup_name_simple (parser, name,
33443 token->location);
33444 if (TREE_CODE (decl) == FIELD_DECL)
33445 add_private_clause = omp_privatize_field (decl, false);
33447 cp_parser_abort_tentative_parse (parser);
33448 cp_parser_parse_tentatively (parser);
33449 decl = cp_parser_primary_expression (parser, false, false,
33450 false, &idk);
33452 if (!cp_parser_error_occurred (parser)
33453 && decl
33454 && DECL_P (decl)
33455 && CLASS_TYPE_P (TREE_TYPE (decl)))
33457 tree rhs;
33459 cp_parser_parse_definitely (parser);
33460 cp_parser_require (parser, CPP_EQ, RT_EQ);
33461 rhs = cp_parser_assignment_expression (parser);
33462 orig_init = rhs;
33463 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
33464 decl, NOP_EXPR,
33465 rhs,
33466 tf_warning_or_error));
33467 if (!add_private_clause)
33468 add_private_clause = decl;
33470 else
33472 decl = NULL;
33473 cp_parser_abort_tentative_parse (parser);
33474 init = cp_parser_expression (parser);
33475 if (init)
33477 if (TREE_CODE (init) == MODIFY_EXPR
33478 || TREE_CODE (init) == MODOP_EXPR)
33479 real_decl = TREE_OPERAND (init, 0);
33483 return add_private_clause;
33486 /* Parse the restricted form of the for statement allowed by OpenMP. */
33488 static tree
33489 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
33490 tree *cclauses, bool *if_p)
33492 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
33493 tree real_decl, initv, condv, incrv, declv;
33494 tree this_pre_body, cl, ordered_cl = NULL_TREE;
33495 location_t loc_first;
33496 bool collapse_err = false;
33497 int i, collapse = 1, ordered = 0, count, nbraces = 0;
33498 vec<tree, va_gc> *for_block = make_tree_vector ();
33499 auto_vec<tree, 4> orig_inits;
33501 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
33502 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
33503 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
33504 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
33505 && OMP_CLAUSE_ORDERED_EXPR (cl))
33507 ordered_cl = cl;
33508 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
33511 if (ordered && ordered < collapse)
33513 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
33514 "%<ordered%> clause parameter is less than %<collapse%>");
33515 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
33516 = build_int_cst (NULL_TREE, collapse);
33517 ordered = collapse;
33519 if (ordered)
33521 for (tree *pc = &clauses; *pc; )
33522 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
33524 error_at (OMP_CLAUSE_LOCATION (*pc),
33525 "%<linear%> clause may not be specified together "
33526 "with %<ordered%> clause with a parameter");
33527 *pc = OMP_CLAUSE_CHAIN (*pc);
33529 else
33530 pc = &OMP_CLAUSE_CHAIN (*pc);
33533 gcc_assert (collapse >= 1 && ordered >= 0);
33534 count = ordered ? ordered : collapse;
33536 declv = make_tree_vec (count);
33537 initv = make_tree_vec (count);
33538 condv = make_tree_vec (count);
33539 incrv = make_tree_vec (count);
33541 loc_first = cp_lexer_peek_token (parser->lexer)->location;
33543 for (i = 0; i < count; i++)
33545 int bracecount = 0;
33546 tree add_private_clause = NULL_TREE;
33547 location_t loc;
33549 if (code != CILK_FOR
33550 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33552 cp_parser_error (parser, "for statement expected");
33553 return NULL;
33555 if (code == CILK_FOR
33556 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
33558 cp_parser_error (parser, "_Cilk_for statement expected");
33559 return NULL;
33561 loc = cp_lexer_consume_token (parser->lexer)->location;
33563 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33564 return NULL;
33566 init = orig_init = decl = real_decl = NULL;
33567 this_pre_body = push_stmt_list ();
33569 add_private_clause
33570 = cp_parser_omp_for_loop_init (parser, code,
33571 this_pre_body, for_block,
33572 init, orig_init, decl, real_decl);
33574 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33575 if (this_pre_body)
33577 this_pre_body = pop_stmt_list (this_pre_body);
33578 if (pre_body)
33580 tree t = pre_body;
33581 pre_body = push_stmt_list ();
33582 add_stmt (t);
33583 add_stmt (this_pre_body);
33584 pre_body = pop_stmt_list (pre_body);
33586 else
33587 pre_body = this_pre_body;
33590 if (decl)
33591 real_decl = decl;
33592 if (cclauses != NULL
33593 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
33594 && real_decl != NULL_TREE)
33596 tree *c;
33597 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
33598 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
33599 && OMP_CLAUSE_DECL (*c) == real_decl)
33601 error_at (loc, "iteration variable %qD"
33602 " should not be firstprivate", real_decl);
33603 *c = OMP_CLAUSE_CHAIN (*c);
33605 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
33606 && OMP_CLAUSE_DECL (*c) == real_decl)
33608 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
33609 tree l = *c;
33610 *c = OMP_CLAUSE_CHAIN (*c);
33611 if (code == OMP_SIMD)
33613 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33614 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
33616 else
33618 OMP_CLAUSE_CHAIN (l) = clauses;
33619 clauses = l;
33621 add_private_clause = NULL_TREE;
33623 else
33625 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
33626 && OMP_CLAUSE_DECL (*c) == real_decl)
33627 add_private_clause = NULL_TREE;
33628 c = &OMP_CLAUSE_CHAIN (*c);
33632 if (add_private_clause)
33634 tree c;
33635 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
33637 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
33638 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
33639 && OMP_CLAUSE_DECL (c) == decl)
33640 break;
33641 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
33642 && OMP_CLAUSE_DECL (c) == decl)
33643 error_at (loc, "iteration variable %qD "
33644 "should not be firstprivate",
33645 decl);
33646 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
33647 && OMP_CLAUSE_DECL (c) == decl)
33648 error_at (loc, "iteration variable %qD should not be reduction",
33649 decl);
33651 if (c == NULL)
33653 if (code != OMP_SIMD)
33654 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
33655 else if (collapse == 1)
33656 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
33657 else
33658 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
33659 OMP_CLAUSE_DECL (c) = add_private_clause;
33660 c = finish_omp_clauses (c, true);
33661 if (c)
33663 OMP_CLAUSE_CHAIN (c) = clauses;
33664 clauses = c;
33665 /* For linear, signal that we need to fill up
33666 the so far unknown linear step. */
33667 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
33668 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
33673 cond = NULL;
33674 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
33675 cond = cp_parser_omp_for_cond (parser, decl, code);
33676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
33678 incr = NULL;
33679 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
33681 /* If decl is an iterator, preserve the operator on decl
33682 until finish_omp_for. */
33683 if (real_decl
33684 && ((processing_template_decl
33685 && (TREE_TYPE (real_decl) == NULL_TREE
33686 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
33687 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
33688 incr = cp_parser_omp_for_incr (parser, real_decl);
33689 else
33690 incr = cp_parser_expression (parser);
33691 if (!EXPR_HAS_LOCATION (incr))
33692 protected_set_expr_location (incr, input_location);
33695 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33696 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33697 /*or_comma=*/false,
33698 /*consume_paren=*/true);
33700 TREE_VEC_ELT (declv, i) = decl;
33701 TREE_VEC_ELT (initv, i) = init;
33702 TREE_VEC_ELT (condv, i) = cond;
33703 TREE_VEC_ELT (incrv, i) = incr;
33704 if (orig_init)
33706 orig_inits.safe_grow_cleared (i + 1);
33707 orig_inits[i] = orig_init;
33710 if (i == count - 1)
33711 break;
33713 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
33714 in between the collapsed for loops to be still considered perfectly
33715 nested. Hopefully the final version clarifies this.
33716 For now handle (multiple) {'s and empty statements. */
33717 cp_parser_parse_tentatively (parser);
33720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
33721 break;
33722 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33724 cp_lexer_consume_token (parser->lexer);
33725 bracecount++;
33727 else if (bracecount
33728 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33729 cp_lexer_consume_token (parser->lexer);
33730 else
33732 loc = cp_lexer_peek_token (parser->lexer)->location;
33733 error_at (loc, "not enough collapsed for loops");
33734 collapse_err = true;
33735 cp_parser_abort_tentative_parse (parser);
33736 declv = NULL_TREE;
33737 break;
33740 while (1);
33742 if (declv)
33744 cp_parser_parse_definitely (parser);
33745 nbraces += bracecount;
33749 if (nbraces)
33750 if_p = NULL;
33752 /* Note that we saved the original contents of this flag when we entered
33753 the structured block, and so we don't need to re-save it here. */
33754 if (code == CILK_SIMD || code == CILK_FOR)
33755 parser->in_statement = IN_CILK_SIMD_FOR;
33756 else
33757 parser->in_statement = IN_OMP_FOR;
33759 /* Note that the grammar doesn't call for a structured block here,
33760 though the loop as a whole is a structured block. */
33761 body = push_stmt_list ();
33762 cp_parser_statement (parser, NULL_TREE, false, if_p);
33763 body = pop_stmt_list (body);
33765 if (declv == NULL_TREE)
33766 ret = NULL_TREE;
33767 else
33768 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
33769 body, pre_body, &orig_inits, clauses);
33771 while (nbraces)
33773 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33775 cp_lexer_consume_token (parser->lexer);
33776 nbraces--;
33778 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
33779 cp_lexer_consume_token (parser->lexer);
33780 else
33782 if (!collapse_err)
33784 error_at (cp_lexer_peek_token (parser->lexer)->location,
33785 "collapsed loops not perfectly nested");
33787 collapse_err = true;
33788 cp_parser_statement_seq_opt (parser, NULL);
33789 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
33790 break;
33794 while (!for_block->is_empty ())
33795 add_stmt (pop_stmt_list (for_block->pop ()));
33796 release_tree_vector (for_block);
33798 return ret;
33801 /* Helper function for OpenMP parsing, split clauses and call
33802 finish_omp_clauses on each of the set of clauses afterwards. */
33804 static void
33805 cp_omp_split_clauses (location_t loc, enum tree_code code,
33806 omp_clause_mask mask, tree clauses, tree *cclauses)
33808 int i;
33809 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
33810 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
33811 if (cclauses[i])
33812 cclauses[i] = finish_omp_clauses (cclauses[i], true);
33815 /* OpenMP 4.0:
33816 #pragma omp simd simd-clause[optseq] new-line
33817 for-loop */
33819 #define OMP_SIMD_CLAUSE_MASK \
33820 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
33821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
33822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
33824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33829 static tree
33830 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
33831 char *p_name, omp_clause_mask mask, tree *cclauses,
33832 bool *if_p)
33834 tree clauses, sb, ret;
33835 unsigned int save;
33836 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33838 strcat (p_name, " simd");
33839 mask |= OMP_SIMD_CLAUSE_MASK;
33841 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33842 cclauses == NULL);
33843 if (cclauses)
33845 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
33846 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
33847 tree c = find_omp_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
33848 OMP_CLAUSE_ORDERED);
33849 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
33851 error_at (OMP_CLAUSE_LOCATION (c),
33852 "%<ordered%> clause with parameter may not be specified "
33853 "on %qs construct", p_name);
33854 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
33858 sb = begin_omp_structured_block ();
33859 save = cp_parser_begin_omp_structured_block (parser);
33861 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
33863 cp_parser_end_omp_structured_block (parser, save);
33864 add_stmt (finish_omp_structured_block (sb));
33866 return ret;
33869 /* OpenMP 2.5:
33870 #pragma omp for for-clause[optseq] new-line
33871 for-loop
33873 OpenMP 4.0:
33874 #pragma omp for simd for-simd-clause[optseq] new-line
33875 for-loop */
33877 #define OMP_FOR_CLAUSE_MASK \
33878 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
33879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
33880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
33881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
33882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
33883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
33884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
33885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
33886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
33888 static tree
33889 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
33890 char *p_name, omp_clause_mask mask, tree *cclauses,
33891 bool *if_p)
33893 tree clauses, sb, ret;
33894 unsigned int save;
33895 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33897 strcat (p_name, " for");
33898 mask |= OMP_FOR_CLAUSE_MASK;
33899 if (cclauses)
33900 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
33901 /* Composite distribute parallel for{, simd} disallows ordered clause. */
33902 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33903 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
33905 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33907 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33908 const char *p = IDENTIFIER_POINTER (id);
33910 if (strcmp (p, "simd") == 0)
33912 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
33913 if (cclauses == NULL)
33914 cclauses = cclauses_buf;
33916 cp_lexer_consume_token (parser->lexer);
33917 if (!flag_openmp) /* flag_openmp_simd */
33918 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33919 cclauses, if_p);
33920 sb = begin_omp_structured_block ();
33921 save = cp_parser_begin_omp_structured_block (parser);
33922 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
33923 cclauses, if_p);
33924 cp_parser_end_omp_structured_block (parser, save);
33925 tree body = finish_omp_structured_block (sb);
33926 if (ret == NULL)
33927 return ret;
33928 ret = make_node (OMP_FOR);
33929 TREE_TYPE (ret) = void_type_node;
33930 OMP_FOR_BODY (ret) = body;
33931 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33932 SET_EXPR_LOCATION (ret, loc);
33933 add_stmt (ret);
33934 return ret;
33937 if (!flag_openmp) /* flag_openmp_simd */
33939 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33940 return NULL_TREE;
33943 /* Composite distribute parallel for disallows linear clause. */
33944 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
33945 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
33947 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
33948 cclauses == NULL);
33949 if (cclauses)
33951 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
33952 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
33955 sb = begin_omp_structured_block ();
33956 save = cp_parser_begin_omp_structured_block (parser);
33958 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
33960 cp_parser_end_omp_structured_block (parser, save);
33961 add_stmt (finish_omp_structured_block (sb));
33963 return ret;
33966 /* OpenMP 2.5:
33967 # pragma omp master new-line
33968 structured-block */
33970 static tree
33971 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33973 cp_parser_require_pragma_eol (parser, pragma_tok);
33974 return c_finish_omp_master (input_location,
33975 cp_parser_omp_structured_block (parser, if_p));
33978 /* OpenMP 2.5:
33979 # pragma omp ordered new-line
33980 structured-block
33982 OpenMP 4.5:
33983 # pragma omp ordered ordered-clauses new-line
33984 structured-block */
33986 #define OMP_ORDERED_CLAUSE_MASK \
33987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
33988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
33990 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
33991 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
33993 static bool
33994 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
33995 enum pragma_context context, bool *if_p)
33997 location_t loc = pragma_tok->location;
33999 if (context != pragma_stmt && context != pragma_compound)
34001 cp_parser_error (parser, "expected declaration specifiers");
34002 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34003 return false;
34006 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34008 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34009 const char *p = IDENTIFIER_POINTER (id);
34011 if (strcmp (p, "depend") == 0)
34013 if (context == pragma_stmt)
34015 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34016 "%<depend%> clause may only be used in compound "
34017 "statements");
34018 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34019 return false;
34021 tree clauses
34022 = cp_parser_omp_all_clauses (parser,
34023 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34024 "#pragma omp ordered", pragma_tok);
34025 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34026 return false;
34030 tree clauses
34031 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34032 "#pragma omp ordered", pragma_tok);
34033 c_finish_omp_ordered (loc, clauses,
34034 cp_parser_omp_structured_block (parser, if_p));
34035 return true;
34038 /* OpenMP 2.5:
34040 section-scope:
34041 { section-sequence }
34043 section-sequence:
34044 section-directive[opt] structured-block
34045 section-sequence section-directive structured-block */
34047 static tree
34048 cp_parser_omp_sections_scope (cp_parser *parser)
34050 tree stmt, substmt;
34051 bool error_suppress = false;
34052 cp_token *tok;
34054 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34055 return NULL_TREE;
34057 stmt = push_stmt_list ();
34059 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34060 != PRAGMA_OMP_SECTION)
34062 substmt = cp_parser_omp_structured_block (parser, NULL);
34063 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34064 add_stmt (substmt);
34067 while (1)
34069 tok = cp_lexer_peek_token (parser->lexer);
34070 if (tok->type == CPP_CLOSE_BRACE)
34071 break;
34072 if (tok->type == CPP_EOF)
34073 break;
34075 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
34077 cp_lexer_consume_token (parser->lexer);
34078 cp_parser_require_pragma_eol (parser, tok);
34079 error_suppress = false;
34081 else if (!error_suppress)
34083 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
34084 error_suppress = true;
34087 substmt = cp_parser_omp_structured_block (parser, NULL);
34088 substmt = build1 (OMP_SECTION, void_type_node, substmt);
34089 add_stmt (substmt);
34091 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34093 substmt = pop_stmt_list (stmt);
34095 stmt = make_node (OMP_SECTIONS);
34096 TREE_TYPE (stmt) = void_type_node;
34097 OMP_SECTIONS_BODY (stmt) = substmt;
34099 add_stmt (stmt);
34100 return stmt;
34103 /* OpenMP 2.5:
34104 # pragma omp sections sections-clause[optseq] newline
34105 sections-scope */
34107 #define OMP_SECTIONS_CLAUSE_MASK \
34108 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34109 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34110 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34111 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34112 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34114 static tree
34115 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
34116 char *p_name, omp_clause_mask mask, tree *cclauses)
34118 tree clauses, ret;
34119 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34121 strcat (p_name, " sections");
34122 mask |= OMP_SECTIONS_CLAUSE_MASK;
34123 if (cclauses)
34124 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34126 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34127 cclauses == NULL);
34128 if (cclauses)
34130 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
34131 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
34134 ret = cp_parser_omp_sections_scope (parser);
34135 if (ret)
34136 OMP_SECTIONS_CLAUSES (ret) = clauses;
34138 return ret;
34141 /* OpenMP 2.5:
34142 # pragma omp parallel parallel-clause[optseq] new-line
34143 structured-block
34144 # pragma omp parallel for parallel-for-clause[optseq] new-line
34145 structured-block
34146 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
34147 structured-block
34149 OpenMP 4.0:
34150 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
34151 structured-block */
34153 #define OMP_PARALLEL_CLAUSE_MASK \
34154 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34155 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34156 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34157 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
34158 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34159 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
34160 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
34162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
34164 static tree
34165 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
34166 char *p_name, omp_clause_mask mask, tree *cclauses,
34167 bool *if_p)
34169 tree stmt, clauses, block;
34170 unsigned int save;
34171 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34173 strcat (p_name, " parallel");
34174 mask |= OMP_PARALLEL_CLAUSE_MASK;
34175 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
34176 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
34177 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
34178 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
34180 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34182 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34183 if (cclauses == NULL)
34184 cclauses = cclauses_buf;
34186 cp_lexer_consume_token (parser->lexer);
34187 if (!flag_openmp) /* flag_openmp_simd */
34188 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34189 if_p);
34190 block = begin_omp_parallel ();
34191 save = cp_parser_begin_omp_structured_block (parser);
34192 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
34193 if_p);
34194 cp_parser_end_omp_structured_block (parser, save);
34195 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34196 block);
34197 if (ret == NULL_TREE)
34198 return ret;
34199 OMP_PARALLEL_COMBINED (stmt) = 1;
34200 return stmt;
34202 /* When combined with distribute, parallel has to be followed by for.
34203 #pragma omp target parallel is allowed though. */
34204 else if (cclauses
34205 && (mask & (OMP_CLAUSE_MASK_1
34206 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34208 error_at (loc, "expected %<for%> after %qs", p_name);
34209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34210 return NULL_TREE;
34212 else if (!flag_openmp) /* flag_openmp_simd */
34214 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34215 return NULL_TREE;
34217 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34219 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34220 const char *p = IDENTIFIER_POINTER (id);
34221 if (strcmp (p, "sections") == 0)
34223 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34224 cclauses = cclauses_buf;
34226 cp_lexer_consume_token (parser->lexer);
34227 block = begin_omp_parallel ();
34228 save = cp_parser_begin_omp_structured_block (parser);
34229 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
34230 cp_parser_end_omp_structured_block (parser, save);
34231 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
34232 block);
34233 OMP_PARALLEL_COMBINED (stmt) = 1;
34234 return stmt;
34238 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
34239 if (cclauses)
34241 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
34242 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
34245 block = begin_omp_parallel ();
34246 save = cp_parser_begin_omp_structured_block (parser);
34247 cp_parser_statement (parser, NULL_TREE, false, if_p);
34248 cp_parser_end_omp_structured_block (parser, save);
34249 stmt = finish_omp_parallel (clauses, block);
34250 return stmt;
34253 /* OpenMP 2.5:
34254 # pragma omp single single-clause[optseq] new-line
34255 structured-block */
34257 #define OMP_SINGLE_CLAUSE_MASK \
34258 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34259 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34260 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
34261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34263 static tree
34264 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34266 tree stmt = make_node (OMP_SINGLE);
34267 TREE_TYPE (stmt) = void_type_node;
34269 OMP_SINGLE_CLAUSES (stmt)
34270 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
34271 "#pragma omp single", pragma_tok);
34272 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34274 return add_stmt (stmt);
34277 /* OpenMP 3.0:
34278 # pragma omp task task-clause[optseq] new-line
34279 structured-block */
34281 #define OMP_TASK_CLAUSE_MASK \
34282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
34284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
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_SHARED) \
34288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
34289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
34290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
34293 static tree
34294 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34296 tree clauses, block;
34297 unsigned int save;
34299 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
34300 "#pragma omp task", pragma_tok);
34301 block = begin_omp_task ();
34302 save = cp_parser_begin_omp_structured_block (parser);
34303 cp_parser_statement (parser, NULL_TREE, false, if_p);
34304 cp_parser_end_omp_structured_block (parser, save);
34305 return finish_omp_task (clauses, block);
34308 /* OpenMP 3.0:
34309 # pragma omp taskwait new-line */
34311 static void
34312 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
34314 cp_parser_require_pragma_eol (parser, pragma_tok);
34315 finish_omp_taskwait ();
34318 /* OpenMP 3.1:
34319 # pragma omp taskyield new-line */
34321 static void
34322 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
34324 cp_parser_require_pragma_eol (parser, pragma_tok);
34325 finish_omp_taskyield ();
34328 /* OpenMP 4.0:
34329 # pragma omp taskgroup new-line
34330 structured-block */
34332 static tree
34333 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34335 cp_parser_require_pragma_eol (parser, pragma_tok);
34336 return c_finish_omp_taskgroup (input_location,
34337 cp_parser_omp_structured_block (parser,
34338 if_p));
34342 /* OpenMP 2.5:
34343 # pragma omp threadprivate (variable-list) */
34345 static void
34346 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
34348 tree vars;
34350 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34351 cp_parser_require_pragma_eol (parser, pragma_tok);
34353 finish_omp_threadprivate (vars);
34356 /* OpenMP 4.0:
34357 # pragma omp cancel cancel-clause[optseq] new-line */
34359 #define OMP_CANCEL_CLAUSE_MASK \
34360 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34361 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34362 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34363 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
34364 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
34366 static void
34367 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
34369 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
34370 "#pragma omp cancel", pragma_tok);
34371 finish_omp_cancel (clauses);
34374 /* OpenMP 4.0:
34375 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
34377 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
34378 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
34379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
34380 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
34381 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
34383 static void
34384 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok)
34386 tree clauses;
34387 bool point_seen = false;
34389 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34391 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34392 const char *p = IDENTIFIER_POINTER (id);
34394 if (strcmp (p, "point") == 0)
34396 cp_lexer_consume_token (parser->lexer);
34397 point_seen = true;
34400 if (!point_seen)
34402 cp_parser_error (parser, "expected %<point%>");
34403 cp_parser_require_pragma_eol (parser, pragma_tok);
34404 return;
34407 clauses = cp_parser_omp_all_clauses (parser,
34408 OMP_CANCELLATION_POINT_CLAUSE_MASK,
34409 "#pragma omp cancellation point",
34410 pragma_tok);
34411 finish_omp_cancellation_point (clauses);
34414 /* OpenMP 4.0:
34415 #pragma omp distribute distribute-clause[optseq] new-line
34416 for-loop */
34418 #define OMP_DISTRIBUTE_CLAUSE_MASK \
34419 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34422 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
34423 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34425 static tree
34426 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
34427 char *p_name, omp_clause_mask mask, tree *cclauses,
34428 bool *if_p)
34430 tree clauses, sb, ret;
34431 unsigned int save;
34432 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34434 strcat (p_name, " distribute");
34435 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
34437 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34439 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34440 const char *p = IDENTIFIER_POINTER (id);
34441 bool simd = false;
34442 bool parallel = false;
34444 if (strcmp (p, "simd") == 0)
34445 simd = true;
34446 else
34447 parallel = strcmp (p, "parallel") == 0;
34448 if (parallel || simd)
34450 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34451 if (cclauses == NULL)
34452 cclauses = cclauses_buf;
34453 cp_lexer_consume_token (parser->lexer);
34454 if (!flag_openmp) /* flag_openmp_simd */
34456 if (simd)
34457 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34458 cclauses, if_p);
34459 else
34460 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34461 cclauses, if_p);
34463 sb = begin_omp_structured_block ();
34464 save = cp_parser_begin_omp_structured_block (parser);
34465 if (simd)
34466 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34467 cclauses, if_p);
34468 else
34469 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
34470 cclauses, if_p);
34471 cp_parser_end_omp_structured_block (parser, save);
34472 tree body = finish_omp_structured_block (sb);
34473 if (ret == NULL)
34474 return ret;
34475 ret = make_node (OMP_DISTRIBUTE);
34476 TREE_TYPE (ret) = void_type_node;
34477 OMP_FOR_BODY (ret) = body;
34478 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34479 SET_EXPR_LOCATION (ret, loc);
34480 add_stmt (ret);
34481 return ret;
34484 if (!flag_openmp) /* flag_openmp_simd */
34486 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34487 return NULL_TREE;
34490 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34491 cclauses == NULL);
34492 if (cclauses)
34494 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
34495 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
34498 sb = begin_omp_structured_block ();
34499 save = cp_parser_begin_omp_structured_block (parser);
34501 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
34503 cp_parser_end_omp_structured_block (parser, save);
34504 add_stmt (finish_omp_structured_block (sb));
34506 return ret;
34509 /* OpenMP 4.0:
34510 # pragma omp teams teams-clause[optseq] new-line
34511 structured-block */
34513 #define OMP_TEAMS_CLAUSE_MASK \
34514 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
34517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
34519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
34520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
34522 static tree
34523 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
34524 char *p_name, omp_clause_mask mask, tree *cclauses,
34525 bool *if_p)
34527 tree clauses, sb, ret;
34528 unsigned int save;
34529 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34531 strcat (p_name, " teams");
34532 mask |= OMP_TEAMS_CLAUSE_MASK;
34534 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34536 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34537 const char *p = IDENTIFIER_POINTER (id);
34538 if (strcmp (p, "distribute") == 0)
34540 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34541 if (cclauses == NULL)
34542 cclauses = cclauses_buf;
34544 cp_lexer_consume_token (parser->lexer);
34545 if (!flag_openmp) /* flag_openmp_simd */
34546 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34547 cclauses, if_p);
34548 sb = begin_omp_structured_block ();
34549 save = cp_parser_begin_omp_structured_block (parser);
34550 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
34551 cclauses, if_p);
34552 cp_parser_end_omp_structured_block (parser, save);
34553 tree body = finish_omp_structured_block (sb);
34554 if (ret == NULL)
34555 return ret;
34556 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34557 ret = make_node (OMP_TEAMS);
34558 TREE_TYPE (ret) = void_type_node;
34559 OMP_TEAMS_CLAUSES (ret) = clauses;
34560 OMP_TEAMS_BODY (ret) = body;
34561 OMP_TEAMS_COMBINED (ret) = 1;
34562 return add_stmt (ret);
34565 if (!flag_openmp) /* flag_openmp_simd */
34567 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34568 return NULL_TREE;
34571 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34572 cclauses == NULL);
34573 if (cclauses)
34575 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
34576 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34579 tree stmt = make_node (OMP_TEAMS);
34580 TREE_TYPE (stmt) = void_type_node;
34581 OMP_TEAMS_CLAUSES (stmt) = clauses;
34582 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34584 return add_stmt (stmt);
34587 /* OpenMP 4.0:
34588 # pragma omp target data target-data-clause[optseq] new-line
34589 structured-block */
34591 #define OMP_TARGET_DATA_CLAUSE_MASK \
34592 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
34597 static tree
34598 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34600 tree clauses
34601 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
34602 "#pragma omp target data", pragma_tok);
34603 int map_seen = 0;
34604 for (tree *pc = &clauses; *pc;)
34606 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34607 switch (OMP_CLAUSE_MAP_KIND (*pc))
34609 case GOMP_MAP_TO:
34610 case GOMP_MAP_ALWAYS_TO:
34611 case GOMP_MAP_FROM:
34612 case GOMP_MAP_ALWAYS_FROM:
34613 case GOMP_MAP_TOFROM:
34614 case GOMP_MAP_ALWAYS_TOFROM:
34615 case GOMP_MAP_ALLOC:
34616 map_seen = 3;
34617 break;
34618 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34619 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34620 case GOMP_MAP_ALWAYS_POINTER:
34621 break;
34622 default:
34623 map_seen |= 1;
34624 error_at (OMP_CLAUSE_LOCATION (*pc),
34625 "%<#pragma omp target data%> with map-type other "
34626 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
34627 "on %<map%> clause");
34628 *pc = OMP_CLAUSE_CHAIN (*pc);
34629 continue;
34631 pc = &OMP_CLAUSE_CHAIN (*pc);
34634 if (map_seen != 3)
34636 if (map_seen == 0)
34637 error_at (pragma_tok->location,
34638 "%<#pragma omp target data%> must contain at least "
34639 "one %<map%> clause");
34640 return NULL_TREE;
34643 tree stmt = make_node (OMP_TARGET_DATA);
34644 TREE_TYPE (stmt) = void_type_node;
34645 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
34647 keep_next_level (true);
34648 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
34650 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34651 return add_stmt (stmt);
34654 /* OpenMP 4.5:
34655 # pragma omp target enter data target-enter-data-clause[optseq] new-line
34656 structured-block */
34658 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
34659 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34660 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34661 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34662 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34663 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34665 static tree
34666 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
34667 enum pragma_context context)
34669 bool data_seen = false;
34670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34672 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34673 const char *p = IDENTIFIER_POINTER (id);
34675 if (strcmp (p, "data") == 0)
34677 cp_lexer_consume_token (parser->lexer);
34678 data_seen = true;
34681 if (!data_seen)
34683 cp_parser_error (parser, "expected %<data%>");
34684 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34685 return NULL_TREE;
34688 if (context == pragma_stmt)
34690 error_at (pragma_tok->location,
34691 "%<#pragma omp target enter data%> may only be "
34692 "used in compound statements");
34693 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34694 return NULL_TREE;
34697 tree clauses
34698 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
34699 "#pragma omp target enter data", pragma_tok);
34700 int map_seen = 0;
34701 for (tree *pc = &clauses; *pc;)
34703 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34704 switch (OMP_CLAUSE_MAP_KIND (*pc))
34706 case GOMP_MAP_TO:
34707 case GOMP_MAP_ALWAYS_TO:
34708 case GOMP_MAP_ALLOC:
34709 map_seen = 3;
34710 break;
34711 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34712 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34713 case GOMP_MAP_ALWAYS_POINTER:
34714 break;
34715 default:
34716 map_seen |= 1;
34717 error_at (OMP_CLAUSE_LOCATION (*pc),
34718 "%<#pragma omp target enter data%> with map-type other "
34719 "than %<to%> or %<alloc%> on %<map%> clause");
34720 *pc = OMP_CLAUSE_CHAIN (*pc);
34721 continue;
34723 pc = &OMP_CLAUSE_CHAIN (*pc);
34726 if (map_seen != 3)
34728 if (map_seen == 0)
34729 error_at (pragma_tok->location,
34730 "%<#pragma omp target enter data%> must contain at least "
34731 "one %<map%> clause");
34732 return NULL_TREE;
34735 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
34736 TREE_TYPE (stmt) = void_type_node;
34737 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
34738 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34739 return add_stmt (stmt);
34742 /* OpenMP 4.5:
34743 # pragma omp target exit data target-enter-data-clause[optseq] new-line
34744 structured-block */
34746 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
34747 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34753 static tree
34754 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
34755 enum pragma_context context)
34757 bool data_seen = false;
34758 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34760 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34761 const char *p = IDENTIFIER_POINTER (id);
34763 if (strcmp (p, "data") == 0)
34765 cp_lexer_consume_token (parser->lexer);
34766 data_seen = true;
34769 if (!data_seen)
34771 cp_parser_error (parser, "expected %<data%>");
34772 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34773 return NULL_TREE;
34776 if (context == pragma_stmt)
34778 error_at (pragma_tok->location,
34779 "%<#pragma omp target exit data%> may only be "
34780 "used in compound statements");
34781 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34782 return NULL_TREE;
34785 tree clauses
34786 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
34787 "#pragma omp target exit data", pragma_tok);
34788 int map_seen = 0;
34789 for (tree *pc = &clauses; *pc;)
34791 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
34792 switch (OMP_CLAUSE_MAP_KIND (*pc))
34794 case GOMP_MAP_FROM:
34795 case GOMP_MAP_ALWAYS_FROM:
34796 case GOMP_MAP_RELEASE:
34797 case GOMP_MAP_DELETE:
34798 map_seen = 3;
34799 break;
34800 case GOMP_MAP_FIRSTPRIVATE_POINTER:
34801 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
34802 case GOMP_MAP_ALWAYS_POINTER:
34803 break;
34804 default:
34805 map_seen |= 1;
34806 error_at (OMP_CLAUSE_LOCATION (*pc),
34807 "%<#pragma omp target exit data%> with map-type other "
34808 "than %<from%>, %<release%> or %<delete%> on %<map%>"
34809 " clause");
34810 *pc = OMP_CLAUSE_CHAIN (*pc);
34811 continue;
34813 pc = &OMP_CLAUSE_CHAIN (*pc);
34816 if (map_seen != 3)
34818 if (map_seen == 0)
34819 error_at (pragma_tok->location,
34820 "%<#pragma omp target exit data%> must contain at least "
34821 "one %<map%> clause");
34822 return NULL_TREE;
34825 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
34826 TREE_TYPE (stmt) = void_type_node;
34827 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
34828 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34829 return add_stmt (stmt);
34832 /* OpenMP 4.0:
34833 # pragma omp target update target-update-clause[optseq] new-line */
34835 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
34836 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
34837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
34838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34840 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34841 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
34843 static bool
34844 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
34845 enum pragma_context context)
34847 if (context == pragma_stmt)
34849 error_at (pragma_tok->location,
34850 "%<#pragma omp target update%> may only be "
34851 "used in compound statements");
34852 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34853 return false;
34856 tree clauses
34857 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
34858 "#pragma omp target update", pragma_tok);
34859 if (find_omp_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
34860 && find_omp_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
34862 error_at (pragma_tok->location,
34863 "%<#pragma omp target update%> must contain at least one "
34864 "%<from%> or %<to%> clauses");
34865 return false;
34868 tree stmt = make_node (OMP_TARGET_UPDATE);
34869 TREE_TYPE (stmt) = void_type_node;
34870 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
34871 SET_EXPR_LOCATION (stmt, pragma_tok->location);
34872 add_stmt (stmt);
34873 return false;
34876 /* OpenMP 4.0:
34877 # pragma omp target target-clause[optseq] new-line
34878 structured-block */
34880 #define OMP_TARGET_CLAUSE_MASK \
34881 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
34882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
34883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
34884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
34885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34888 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
34889 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
34891 static bool
34892 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
34893 enum pragma_context context, bool *if_p)
34895 tree *pc = NULL, stmt;
34897 if (context != pragma_stmt && context != pragma_compound)
34899 cp_parser_error (parser, "expected declaration specifiers");
34900 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34901 return false;
34904 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34906 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34907 const char *p = IDENTIFIER_POINTER (id);
34908 enum tree_code ccode = ERROR_MARK;
34910 if (strcmp (p, "teams") == 0)
34911 ccode = OMP_TEAMS;
34912 else if (strcmp (p, "parallel") == 0)
34913 ccode = OMP_PARALLEL;
34914 else if (strcmp (p, "simd") == 0)
34915 ccode = OMP_SIMD;
34916 if (ccode != ERROR_MARK)
34918 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
34919 char p_name[sizeof ("#pragma omp target teams distribute "
34920 "parallel for simd")];
34922 cp_lexer_consume_token (parser->lexer);
34923 strcpy (p_name, "#pragma omp target");
34924 if (!flag_openmp) /* flag_openmp_simd */
34926 tree stmt;
34927 switch (ccode)
34929 case OMP_TEAMS:
34930 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
34931 OMP_TARGET_CLAUSE_MASK,
34932 cclauses, if_p);
34933 break;
34934 case OMP_PARALLEL:
34935 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34936 OMP_TARGET_CLAUSE_MASK,
34937 cclauses, if_p);
34938 break;
34939 case OMP_SIMD:
34940 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
34941 OMP_TARGET_CLAUSE_MASK,
34942 cclauses, if_p);
34943 break;
34944 default:
34945 gcc_unreachable ();
34947 return stmt != NULL_TREE;
34949 keep_next_level (true);
34950 tree sb = begin_omp_structured_block (), ret;
34951 unsigned save = cp_parser_begin_omp_structured_block (parser);
34952 switch (ccode)
34954 case OMP_TEAMS:
34955 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
34956 OMP_TARGET_CLAUSE_MASK, cclauses,
34957 if_p);
34958 break;
34959 case OMP_PARALLEL:
34960 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
34961 OMP_TARGET_CLAUSE_MASK, cclauses,
34962 if_p);
34963 break;
34964 case OMP_SIMD:
34965 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
34966 OMP_TARGET_CLAUSE_MASK, cclauses,
34967 if_p);
34968 break;
34969 default:
34970 gcc_unreachable ();
34972 cp_parser_end_omp_structured_block (parser, save);
34973 tree body = finish_omp_structured_block (sb);
34974 if (ret == NULL_TREE)
34975 return false;
34976 if (ccode == OMP_TEAMS && !processing_template_decl)
34978 /* For combined target teams, ensure the num_teams and
34979 thread_limit clause expressions are evaluated on the host,
34980 before entering the target construct. */
34981 tree c;
34982 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
34983 c; c = OMP_CLAUSE_CHAIN (c))
34984 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
34985 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
34986 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
34988 tree expr = OMP_CLAUSE_OPERAND (c, 0);
34989 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
34990 if (expr == error_mark_node)
34991 continue;
34992 tree tmp = TARGET_EXPR_SLOT (expr);
34993 add_stmt (expr);
34994 OMP_CLAUSE_OPERAND (c, 0) = expr;
34995 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
34996 OMP_CLAUSE_FIRSTPRIVATE);
34997 OMP_CLAUSE_DECL (tc) = tmp;
34998 OMP_CLAUSE_CHAIN (tc)
34999 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35000 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35003 tree stmt = make_node (OMP_TARGET);
35004 TREE_TYPE (stmt) = void_type_node;
35005 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35006 OMP_TARGET_BODY (stmt) = body;
35007 OMP_TARGET_COMBINED (stmt) = 1;
35008 add_stmt (stmt);
35009 pc = &OMP_TARGET_CLAUSES (stmt);
35010 goto check_clauses;
35012 else if (!flag_openmp) /* flag_openmp_simd */
35014 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35015 return false;
35017 else if (strcmp (p, "data") == 0)
35019 cp_lexer_consume_token (parser->lexer);
35020 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35021 return true;
35023 else if (strcmp (p, "enter") == 0)
35025 cp_lexer_consume_token (parser->lexer);
35026 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35027 return false;
35029 else if (strcmp (p, "exit") == 0)
35031 cp_lexer_consume_token (parser->lexer);
35032 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35033 return false;
35035 else if (strcmp (p, "update") == 0)
35037 cp_lexer_consume_token (parser->lexer);
35038 return cp_parser_omp_target_update (parser, pragma_tok, context);
35042 stmt = make_node (OMP_TARGET);
35043 TREE_TYPE (stmt) = void_type_node;
35045 OMP_TARGET_CLAUSES (stmt)
35046 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35047 "#pragma omp target", pragma_tok);
35048 pc = &OMP_TARGET_CLAUSES (stmt);
35049 keep_next_level (true);
35050 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35052 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35053 add_stmt (stmt);
35055 check_clauses:
35056 while (*pc)
35058 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35059 switch (OMP_CLAUSE_MAP_KIND (*pc))
35061 case GOMP_MAP_TO:
35062 case GOMP_MAP_ALWAYS_TO:
35063 case GOMP_MAP_FROM:
35064 case GOMP_MAP_ALWAYS_FROM:
35065 case GOMP_MAP_TOFROM:
35066 case GOMP_MAP_ALWAYS_TOFROM:
35067 case GOMP_MAP_ALLOC:
35068 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35069 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35070 case GOMP_MAP_ALWAYS_POINTER:
35071 break;
35072 default:
35073 error_at (OMP_CLAUSE_LOCATION (*pc),
35074 "%<#pragma omp target%> with map-type other "
35075 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35076 "on %<map%> clause");
35077 *pc = OMP_CLAUSE_CHAIN (*pc);
35078 continue;
35080 pc = &OMP_CLAUSE_CHAIN (*pc);
35082 return true;
35085 /* OpenACC 2.0:
35086 # pragma acc cache (variable-list) new-line
35089 static tree
35090 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
35092 tree stmt, clauses;
35094 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
35095 clauses = finish_omp_clauses (clauses, false);
35097 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
35099 stmt = make_node (OACC_CACHE);
35100 TREE_TYPE (stmt) = void_type_node;
35101 OACC_CACHE_CLAUSES (stmt) = clauses;
35102 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35103 add_stmt (stmt);
35105 return stmt;
35108 /* OpenACC 2.0:
35109 # pragma acc data oacc-data-clause[optseq] new-line
35110 structured-block */
35112 #define OACC_DATA_CLAUSE_MASK \
35113 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35114 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35115 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35116 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35117 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35118 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35119 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35125 static tree
35126 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35128 tree stmt, clauses, block;
35129 unsigned int save;
35131 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
35132 "#pragma acc data", pragma_tok);
35134 block = begin_omp_parallel ();
35135 save = cp_parser_begin_omp_structured_block (parser);
35136 cp_parser_statement (parser, NULL_TREE, false, if_p);
35137 cp_parser_end_omp_structured_block (parser, save);
35138 stmt = finish_oacc_data (clauses, block);
35139 return stmt;
35142 /* OpenACC 2.0:
35143 # pragma acc host_data <clauses> new-line
35144 structured-block */
35146 #define OACC_HOST_DATA_CLAUSE_MASK \
35147 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
35149 static tree
35150 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35152 tree stmt, clauses, block;
35153 unsigned int save;
35155 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
35156 "#pragma acc host_data", pragma_tok);
35158 block = begin_omp_parallel ();
35159 save = cp_parser_begin_omp_structured_block (parser);
35160 cp_parser_statement (parser, NULL_TREE, false, if_p);
35161 cp_parser_end_omp_structured_block (parser, save);
35162 stmt = finish_oacc_host_data (clauses, block);
35163 return stmt;
35166 /* OpenACC 2.0:
35167 # pragma acc declare oacc-data-clause[optseq] new-line
35170 #define OACC_DECLARE_CLAUSE_MASK \
35171 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35172 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35173 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35174 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35175 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35176 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
35177 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
35178 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35179 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35180 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35181 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35182 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
35184 static tree
35185 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
35187 tree clauses, stmt;
35188 bool error = false;
35190 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
35191 "#pragma acc declare", pragma_tok, true);
35194 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35196 error_at (pragma_tok->location,
35197 "no valid clauses specified in %<#pragma acc declare%>");
35198 return NULL_TREE;
35201 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
35203 location_t loc = OMP_CLAUSE_LOCATION (t);
35204 tree decl = OMP_CLAUSE_DECL (t);
35205 if (!DECL_P (decl))
35207 error_at (loc, "array section in %<#pragma acc declare%>");
35208 error = true;
35209 continue;
35211 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
35212 switch (OMP_CLAUSE_MAP_KIND (t))
35214 case GOMP_MAP_FORCE_ALLOC:
35215 case GOMP_MAP_FORCE_TO:
35216 case GOMP_MAP_FORCE_DEVICEPTR:
35217 case GOMP_MAP_DEVICE_RESIDENT:
35218 break;
35220 case GOMP_MAP_POINTER:
35221 /* Generated by c_finish_omp_clauses from array sections;
35222 avoid spurious diagnostics. */
35223 break;
35225 case GOMP_MAP_LINK:
35226 if (!global_bindings_p ()
35227 && (TREE_STATIC (decl)
35228 || !DECL_EXTERNAL (decl)))
35230 error_at (loc,
35231 "%qD must be a global variable in"
35232 "%<#pragma acc declare link%>",
35233 decl);
35234 error = true;
35235 continue;
35237 break;
35239 default:
35240 if (global_bindings_p ())
35242 error_at (loc, "invalid OpenACC clause at file scope");
35243 error = true;
35244 continue;
35246 if (DECL_EXTERNAL (decl))
35248 error_at (loc,
35249 "invalid use of %<extern%> variable %qD "
35250 "in %<#pragma acc declare%>", decl);
35251 error = true;
35252 continue;
35254 else if (TREE_PUBLIC (decl))
35256 error_at (loc,
35257 "invalid use of %<global%> variable %qD "
35258 "in %<#pragma acc declare%>", decl);
35259 error = true;
35260 continue;
35262 break;
35265 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
35266 || lookup_attribute ("omp declare target link",
35267 DECL_ATTRIBUTES (decl)))
35269 error_at (loc, "variable %qD used more than once with "
35270 "%<#pragma acc declare%>", decl);
35271 error = true;
35272 continue;
35275 if (!error)
35277 tree id;
35279 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
35280 id = get_identifier ("omp declare target link");
35281 else
35282 id = get_identifier ("omp declare target");
35284 DECL_ATTRIBUTES (decl)
35285 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
35286 if (global_bindings_p ())
35288 symtab_node *node = symtab_node::get (decl);
35289 if (node != NULL)
35291 node->offloadable = 1;
35292 if (ENABLE_OFFLOADING)
35294 g->have_offload = true;
35295 if (is_a <varpool_node *> (node))
35296 vec_safe_push (offload_vars, decl);
35303 if (error || global_bindings_p ())
35304 return NULL_TREE;
35306 stmt = make_node (OACC_DECLARE);
35307 TREE_TYPE (stmt) = void_type_node;
35308 OACC_DECLARE_CLAUSES (stmt) = clauses;
35309 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35311 add_stmt (stmt);
35313 return NULL_TREE;
35316 /* OpenACC 2.0:
35317 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
35321 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
35323 LOC is the location of the #pragma token.
35326 #define OACC_ENTER_DATA_CLAUSE_MASK \
35327 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35328 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35329 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35330 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35331 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35332 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35333 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35335 #define OACC_EXIT_DATA_CLAUSE_MASK \
35336 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
35340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35342 static tree
35343 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
35344 bool enter)
35346 tree stmt, clauses;
35348 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)
35349 || cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
35351 cp_parser_error (parser, enter
35352 ? "expected %<data%> in %<#pragma acc enter data%>"
35353 : "expected %<data%> in %<#pragma acc exit data%>");
35354 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35355 return NULL_TREE;
35358 const char *p =
35359 IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35360 if (strcmp (p, "data") != 0)
35362 cp_parser_error (parser, "invalid pragma");
35363 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35364 return NULL_TREE;
35367 cp_lexer_consume_token (parser->lexer);
35369 if (enter)
35370 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
35371 "#pragma acc enter data", pragma_tok);
35372 else
35373 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
35374 "#pragma acc exit data", pragma_tok);
35376 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35378 error_at (pragma_tok->location,
35379 "%<#pragma acc enter data%> has no data movement clause");
35380 return NULL_TREE;
35383 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
35384 TREE_TYPE (stmt) = void_type_node;
35385 OMP_STANDALONE_CLAUSES (stmt) = clauses;
35386 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35387 add_stmt (stmt);
35388 return stmt;
35391 /* OpenACC 2.0:
35392 # pragma acc loop oacc-loop-clause[optseq] new-line
35393 structured-block */
35395 #define OACC_LOOP_CLAUSE_MASK \
35396 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
35400 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
35401 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
35402 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
35403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
35404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
35405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
35407 static tree
35408 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
35409 omp_clause_mask mask, tree *cclauses, bool *if_p)
35411 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
35413 strcat (p_name, " loop");
35414 mask |= OACC_LOOP_CLAUSE_MASK;
35416 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
35417 cclauses == NULL);
35418 if (cclauses)
35420 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
35421 if (*cclauses)
35422 *cclauses = finish_omp_clauses (*cclauses, false);
35423 if (clauses)
35424 clauses = finish_omp_clauses (clauses, false);
35427 tree block = begin_omp_structured_block ();
35428 int save = cp_parser_begin_omp_structured_block (parser);
35429 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
35430 cp_parser_end_omp_structured_block (parser, save);
35431 add_stmt (finish_omp_structured_block (block));
35433 return stmt;
35436 /* OpenACC 2.0:
35437 # pragma acc kernels oacc-kernels-clause[optseq] new-line
35438 structured-block
35442 # pragma acc parallel oacc-parallel-clause[optseq] new-line
35443 structured-block
35446 #define OACC_KERNELS_CLAUSE_MASK \
35447 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35452 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35453 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35462 #define OACC_PARALLEL_CLAUSE_MASK \
35463 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
35473 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
35474 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
35475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
35476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
35477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
35478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
35479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
35480 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
35481 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
35482 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
35484 static tree
35485 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
35486 char *p_name, bool *if_p)
35488 omp_clause_mask mask;
35489 enum tree_code code;
35490 switch (cp_parser_pragma_kind (pragma_tok))
35492 case PRAGMA_OACC_KERNELS:
35493 strcat (p_name, " kernels");
35494 mask = OACC_KERNELS_CLAUSE_MASK;
35495 code = OACC_KERNELS;
35496 break;
35497 case PRAGMA_OACC_PARALLEL:
35498 strcat (p_name, " parallel");
35499 mask = OACC_PARALLEL_CLAUSE_MASK;
35500 code = OACC_PARALLEL;
35501 break;
35502 default:
35503 gcc_unreachable ();
35506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35508 const char *p
35509 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
35510 if (strcmp (p, "loop") == 0)
35512 cp_lexer_consume_token (parser->lexer);
35513 tree block = begin_omp_parallel ();
35514 tree clauses;
35515 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
35516 if_p);
35517 return finish_omp_construct (code, block, clauses);
35521 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
35523 tree block = begin_omp_parallel ();
35524 unsigned int save = cp_parser_begin_omp_structured_block (parser);
35525 cp_parser_statement (parser, NULL_TREE, false, if_p);
35526 cp_parser_end_omp_structured_block (parser, save);
35527 return finish_omp_construct (code, block, clauses);
35530 /* OpenACC 2.0:
35531 # pragma acc update oacc-update-clause[optseq] new-line
35534 #define OACC_UPDATE_CLAUSE_MASK \
35535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
35536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
35539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
35542 static tree
35543 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
35545 tree stmt, clauses;
35547 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
35548 "#pragma acc update", pragma_tok);
35550 if (find_omp_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
35552 error_at (pragma_tok->location,
35553 "%<#pragma acc update%> must contain at least one "
35554 "%<device%> or %<host%> or %<self%> clause");
35555 return NULL_TREE;
35558 stmt = make_node (OACC_UPDATE);
35559 TREE_TYPE (stmt) = void_type_node;
35560 OACC_UPDATE_CLAUSES (stmt) = clauses;
35561 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35562 add_stmt (stmt);
35563 return stmt;
35566 /* OpenACC 2.0:
35567 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
35569 LOC is the location of the #pragma token.
35572 #define OACC_WAIT_CLAUSE_MASK \
35573 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
35575 static tree
35576 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
35578 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
35579 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35581 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
35582 list = cp_parser_oacc_wait_list (parser, loc, list);
35584 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
35585 "#pragma acc wait", pragma_tok);
35587 stmt = c_finish_oacc_wait (loc, list, clauses);
35588 stmt = finish_expr_stmt (stmt);
35590 return stmt;
35593 /* OpenMP 4.0:
35594 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
35596 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
35597 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35598 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35599 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
35601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
35602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
35604 static void
35605 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
35606 enum pragma_context context)
35608 bool first_p = parser->omp_declare_simd == NULL;
35609 cp_omp_declare_simd_data data;
35610 if (first_p)
35612 data.error_seen = false;
35613 data.fndecl_seen = false;
35614 data.tokens = vNULL;
35615 parser->omp_declare_simd = &data;
35617 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
35618 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
35619 cp_lexer_consume_token (parser->lexer);
35620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
35621 parser->omp_declare_simd->error_seen = true;
35622 cp_parser_require_pragma_eol (parser, pragma_tok);
35623 struct cp_token_cache *cp
35624 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
35625 parser->omp_declare_simd->tokens.safe_push (cp);
35626 if (first_p)
35628 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
35629 cp_parser_pragma (parser, context, NULL);
35630 switch (context)
35632 case pragma_external:
35633 cp_parser_declaration (parser);
35634 break;
35635 case pragma_member:
35636 cp_parser_member_declaration (parser);
35637 break;
35638 case pragma_objc_icode:
35639 cp_parser_block_declaration (parser, /*statement_p=*/false);
35640 break;
35641 default:
35642 cp_parser_declaration_statement (parser);
35643 break;
35645 if (parser->omp_declare_simd
35646 && !parser->omp_declare_simd->error_seen
35647 && !parser->omp_declare_simd->fndecl_seen)
35648 error_at (pragma_tok->location,
35649 "%<#pragma omp declare simd%> not immediately followed by "
35650 "function declaration or definition");
35651 data.tokens.release ();
35652 parser->omp_declare_simd = NULL;
35656 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
35657 This function is modelled similar to the late parsing of omp declare
35658 simd. */
35660 static tree
35661 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
35663 struct cp_token_cache *ce;
35664 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
35665 int ii = 0;
35667 if (parser->omp_declare_simd != NULL
35668 || lookup_attribute ("simd", attrs))
35670 error ("%<#pragma omp declare simd%> of %<simd%> attribute cannot be "
35671 "used in the same function marked as a Cilk Plus SIMD-enabled "
35672 " function");
35673 parser->cilk_simd_fn_info->tokens.release ();
35674 XDELETE (parser->cilk_simd_fn_info);
35675 parser->cilk_simd_fn_info = NULL;
35676 return attrs;
35678 if (!info->error_seen && info->fndecl_seen)
35680 error ("vector attribute not immediately followed by a single function"
35681 " declaration or definition");
35682 info->error_seen = true;
35684 if (info->error_seen)
35685 return attrs;
35687 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
35689 tree c, cl;
35691 cp_parser_push_lexer_for_tokens (parser, ce);
35692 parser->lexer->in_pragma = true;
35693 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
35694 "SIMD-enabled functions attribute",
35695 NULL);
35696 cp_parser_pop_lexer (parser);
35697 if (cl)
35698 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35700 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
35701 TREE_CHAIN (c) = attrs;
35702 attrs = c;
35704 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35705 TREE_CHAIN (c) = attrs;
35706 if (processing_template_decl)
35707 ATTR_IS_DEPENDENT (c) = 1;
35708 attrs = c;
35710 info->fndecl_seen = true;
35711 parser->cilk_simd_fn_info->tokens.release ();
35712 XDELETE (parser->cilk_simd_fn_info);
35713 parser->cilk_simd_fn_info = NULL;
35714 return attrs;
35717 /* Finalize #pragma omp declare simd clauses after direct declarator has
35718 been parsed, and put that into "omp declare simd" attribute. */
35720 static tree
35721 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
35723 struct cp_token_cache *ce;
35724 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
35725 int i;
35727 if (!data->error_seen && data->fndecl_seen)
35729 error ("%<#pragma omp declare simd%> not immediately followed by "
35730 "a single function declaration or definition");
35731 data->error_seen = true;
35732 return attrs;
35734 if (data->error_seen)
35735 return attrs;
35737 FOR_EACH_VEC_ELT (data->tokens, i, ce)
35739 tree c, cl;
35741 cp_parser_push_lexer_for_tokens (parser, ce);
35742 parser->lexer->in_pragma = true;
35743 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
35744 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
35745 cp_lexer_consume_token (parser->lexer);
35746 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
35747 "#pragma omp declare simd", pragma_tok);
35748 cp_parser_pop_lexer (parser);
35749 if (cl)
35750 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
35751 c = build_tree_list (get_identifier ("omp declare simd"), cl);
35752 TREE_CHAIN (c) = attrs;
35753 if (processing_template_decl)
35754 ATTR_IS_DEPENDENT (c) = 1;
35755 attrs = c;
35758 data->fndecl_seen = true;
35759 return attrs;
35763 /* OpenMP 4.0:
35764 # pragma omp declare target new-line
35765 declarations and definitions
35766 # pragma omp end declare target new-line
35768 OpenMP 4.5:
35769 # pragma omp declare target ( extended-list ) new-line
35771 # pragma omp declare target declare-target-clauses[seq] new-line */
35773 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
35774 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35775 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
35777 static void
35778 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
35780 tree clauses = NULL_TREE;
35781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35782 clauses
35783 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
35784 "#pragma omp declare target", pragma_tok);
35785 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
35787 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
35788 clauses);
35789 clauses = finish_omp_clauses (clauses, true);
35790 cp_parser_require_pragma_eol (parser, pragma_tok);
35792 else
35794 cp_parser_require_pragma_eol (parser, pragma_tok);
35795 scope_chain->omp_declare_target_attribute++;
35796 return;
35798 if (scope_chain->omp_declare_target_attribute)
35799 error_at (pragma_tok->location,
35800 "%<#pragma omp declare target%> with clauses in between "
35801 "%<#pragma omp declare target%> without clauses and "
35802 "%<#pragma omp end declare target%>");
35803 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
35805 tree t = OMP_CLAUSE_DECL (c), id;
35806 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
35807 tree at2 = lookup_attribute ("omp declare target link",
35808 DECL_ATTRIBUTES (t));
35809 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
35811 id = get_identifier ("omp declare target link");
35812 std::swap (at1, at2);
35814 else
35815 id = get_identifier ("omp declare target");
35816 if (at2)
35818 error_at (OMP_CLAUSE_LOCATION (c),
35819 "%qD specified both in declare target %<link%> and %<to%>"
35820 " clauses", t);
35821 continue;
35823 if (!at1)
35825 symtab_node *node = symtab_node::get (t);
35826 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
35827 if (node != NULL)
35829 node->offloadable = 1;
35830 if (ENABLE_OFFLOADING)
35832 g->have_offload = true;
35833 if (is_a <varpool_node *> (node))
35834 vec_safe_push (offload_vars, t);
35841 static void
35842 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
35844 const char *p = "";
35845 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35847 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35848 p = IDENTIFIER_POINTER (id);
35850 if (strcmp (p, "declare") == 0)
35852 cp_lexer_consume_token (parser->lexer);
35853 p = "";
35854 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35856 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35857 p = IDENTIFIER_POINTER (id);
35859 if (strcmp (p, "target") == 0)
35860 cp_lexer_consume_token (parser->lexer);
35861 else
35863 cp_parser_error (parser, "expected %<target%>");
35864 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35865 return;
35868 else
35870 cp_parser_error (parser, "expected %<declare%>");
35871 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35872 return;
35874 cp_parser_require_pragma_eol (parser, pragma_tok);
35875 if (!scope_chain->omp_declare_target_attribute)
35876 error_at (pragma_tok->location,
35877 "%<#pragma omp end declare target%> without corresponding "
35878 "%<#pragma omp declare target%>");
35879 else
35880 scope_chain->omp_declare_target_attribute--;
35883 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
35884 expression and optional initializer clause of
35885 #pragma omp declare reduction. We store the expression(s) as
35886 either 3, 6 or 7 special statements inside of the artificial function's
35887 body. The first two statements are DECL_EXPRs for the artificial
35888 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
35889 expression that uses those variables.
35890 If there was any INITIALIZER clause, this is followed by further statements,
35891 the fourth and fifth statements are DECL_EXPRs for the artificial
35892 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
35893 constructor variant (first token after open paren is not omp_priv),
35894 then the sixth statement is a statement with the function call expression
35895 that uses the OMP_PRIV and optionally OMP_ORIG variable.
35896 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
35897 to initialize the OMP_PRIV artificial variable and there is seventh
35898 statement, a DECL_EXPR of the OMP_PRIV statement again. */
35900 static bool
35901 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
35903 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
35904 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
35905 type = TREE_TYPE (type);
35906 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
35907 DECL_ARTIFICIAL (omp_out) = 1;
35908 pushdecl (omp_out);
35909 add_decl_expr (omp_out);
35910 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
35911 DECL_ARTIFICIAL (omp_in) = 1;
35912 pushdecl (omp_in);
35913 add_decl_expr (omp_in);
35914 tree combiner;
35915 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
35917 keep_next_level (true);
35918 tree block = begin_omp_structured_block ();
35919 combiner = cp_parser_expression (parser);
35920 finish_expr_stmt (combiner);
35921 block = finish_omp_structured_block (block);
35922 add_stmt (block);
35924 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
35925 return false;
35927 const char *p = "";
35928 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35930 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35931 p = IDENTIFIER_POINTER (id);
35934 if (strcmp (p, "initializer") == 0)
35936 cp_lexer_consume_token (parser->lexer);
35937 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
35938 return false;
35940 p = "";
35941 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35943 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35944 p = IDENTIFIER_POINTER (id);
35947 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
35948 DECL_ARTIFICIAL (omp_priv) = 1;
35949 pushdecl (omp_priv);
35950 add_decl_expr (omp_priv);
35951 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
35952 DECL_ARTIFICIAL (omp_orig) = 1;
35953 pushdecl (omp_orig);
35954 add_decl_expr (omp_orig);
35956 keep_next_level (true);
35957 block = begin_omp_structured_block ();
35959 bool ctor = false;
35960 if (strcmp (p, "omp_priv") == 0)
35962 bool is_direct_init, is_non_constant_init;
35963 ctor = true;
35964 cp_lexer_consume_token (parser->lexer);
35965 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
35966 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
35967 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35968 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
35969 == CPP_CLOSE_PAREN
35970 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
35971 == CPP_CLOSE_PAREN))
35973 finish_omp_structured_block (block);
35974 error ("invalid initializer clause");
35975 return false;
35977 initializer = cp_parser_initializer (parser, &is_direct_init,
35978 &is_non_constant_init);
35979 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
35980 NULL_TREE, LOOKUP_ONLYCONVERTING);
35982 else
35984 cp_parser_parse_tentatively (parser);
35985 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
35986 /*check_dependency_p=*/true,
35987 /*template_p=*/NULL,
35988 /*declarator_p=*/false,
35989 /*optional_p=*/false);
35990 vec<tree, va_gc> *args;
35991 if (fn_name == error_mark_node
35992 || cp_parser_error_occurred (parser)
35993 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
35994 || ((args = cp_parser_parenthesized_expression_list
35995 (parser, non_attr, /*cast_p=*/false,
35996 /*allow_expansion_p=*/true,
35997 /*non_constant_p=*/NULL)),
35998 cp_parser_error_occurred (parser)))
36000 finish_omp_structured_block (block);
36001 cp_parser_abort_tentative_parse (parser);
36002 cp_parser_error (parser, "expected id-expression (arguments)");
36003 return false;
36005 unsigned int i;
36006 tree arg;
36007 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36008 if (arg == omp_priv
36009 || (TREE_CODE (arg) == ADDR_EXPR
36010 && TREE_OPERAND (arg, 0) == omp_priv))
36011 break;
36012 cp_parser_abort_tentative_parse (parser);
36013 if (arg == NULL_TREE)
36014 error ("one of the initializer call arguments should be %<omp_priv%>"
36015 " or %<&omp_priv%>");
36016 initializer = cp_parser_postfix_expression (parser, false, false, false,
36017 false, NULL);
36018 finish_expr_stmt (initializer);
36021 block = finish_omp_structured_block (block);
36022 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36023 add_stmt (block);
36025 if (ctor)
36026 add_decl_expr (omp_orig);
36028 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36029 return false;
36032 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36033 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36035 return true;
36038 /* OpenMP 4.0
36039 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36040 initializer-clause[opt] new-line
36042 initializer-clause:
36043 initializer (omp_priv initializer)
36044 initializer (function-name (argument-list)) */
36046 static void
36047 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36048 enum pragma_context)
36050 auto_vec<tree> types;
36051 enum tree_code reduc_code = ERROR_MARK;
36052 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
36053 unsigned int i;
36054 cp_token *first_token;
36055 cp_token_cache *cp;
36056 int errs;
36057 void *p;
36059 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
36060 p = obstack_alloc (&declarator_obstack, 0);
36062 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36063 goto fail;
36065 switch (cp_lexer_peek_token (parser->lexer)->type)
36067 case CPP_PLUS:
36068 reduc_code = PLUS_EXPR;
36069 break;
36070 case CPP_MULT:
36071 reduc_code = MULT_EXPR;
36072 break;
36073 case CPP_MINUS:
36074 reduc_code = MINUS_EXPR;
36075 break;
36076 case CPP_AND:
36077 reduc_code = BIT_AND_EXPR;
36078 break;
36079 case CPP_XOR:
36080 reduc_code = BIT_XOR_EXPR;
36081 break;
36082 case CPP_OR:
36083 reduc_code = BIT_IOR_EXPR;
36084 break;
36085 case CPP_AND_AND:
36086 reduc_code = TRUTH_ANDIF_EXPR;
36087 break;
36088 case CPP_OR_OR:
36089 reduc_code = TRUTH_ORIF_EXPR;
36090 break;
36091 case CPP_NAME:
36092 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
36093 break;
36094 default:
36095 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
36096 "%<|%>, %<&&%>, %<||%> or identifier");
36097 goto fail;
36100 if (reduc_code != ERROR_MARK)
36101 cp_lexer_consume_token (parser->lexer);
36103 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
36104 if (reduc_id == error_mark_node)
36105 goto fail;
36107 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
36108 goto fail;
36110 /* Types may not be defined in declare reduction type list. */
36111 const char *saved_message;
36112 saved_message = parser->type_definition_forbidden_message;
36113 parser->type_definition_forbidden_message
36114 = G_("types may not be defined in declare reduction type list");
36115 bool saved_colon_corrects_to_scope_p;
36116 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
36117 parser->colon_corrects_to_scope_p = false;
36118 bool saved_colon_doesnt_start_class_def_p;
36119 saved_colon_doesnt_start_class_def_p
36120 = parser->colon_doesnt_start_class_def_p;
36121 parser->colon_doesnt_start_class_def_p = true;
36123 while (true)
36125 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36126 type = cp_parser_type_id (parser);
36127 if (type == error_mark_node)
36129 else if (ARITHMETIC_TYPE_P (type)
36130 && (orig_reduc_id == NULL_TREE
36131 || (TREE_CODE (type) != COMPLEX_TYPE
36132 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36133 "min") == 0
36134 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
36135 "max") == 0))))
36136 error_at (loc, "predeclared arithmetic type %qT in "
36137 "%<#pragma omp declare reduction%>", type);
36138 else if (TREE_CODE (type) == FUNCTION_TYPE
36139 || TREE_CODE (type) == METHOD_TYPE
36140 || TREE_CODE (type) == ARRAY_TYPE)
36141 error_at (loc, "function or array type %qT in "
36142 "%<#pragma omp declare reduction%>", type);
36143 else if (TREE_CODE (type) == REFERENCE_TYPE)
36144 error_at (loc, "reference type %qT in "
36145 "%<#pragma omp declare reduction%>", type);
36146 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
36147 error_at (loc, "const, volatile or __restrict qualified type %qT in "
36148 "%<#pragma omp declare reduction%>", type);
36149 else
36150 types.safe_push (type);
36152 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
36153 cp_lexer_consume_token (parser->lexer);
36154 else
36155 break;
36158 /* Restore the saved message. */
36159 parser->type_definition_forbidden_message = saved_message;
36160 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
36161 parser->colon_doesnt_start_class_def_p
36162 = saved_colon_doesnt_start_class_def_p;
36164 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
36165 || types.is_empty ())
36167 fail:
36168 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36169 goto done;
36172 first_token = cp_lexer_peek_token (parser->lexer);
36173 cp = NULL;
36174 errs = errorcount;
36175 FOR_EACH_VEC_ELT (types, i, type)
36177 tree fntype
36178 = build_function_type_list (void_type_node,
36179 cp_build_reference_type (type, false),
36180 NULL_TREE);
36181 tree this_reduc_id = reduc_id;
36182 if (!dependent_type_p (type))
36183 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
36184 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
36185 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
36186 DECL_ARTIFICIAL (fndecl) = 1;
36187 DECL_EXTERNAL (fndecl) = 1;
36188 DECL_DECLARED_INLINE_P (fndecl) = 1;
36189 DECL_IGNORED_P (fndecl) = 1;
36190 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
36191 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
36192 DECL_ATTRIBUTES (fndecl)
36193 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
36194 DECL_ATTRIBUTES (fndecl));
36195 if (processing_template_decl)
36196 fndecl = push_template_decl (fndecl);
36197 bool block_scope = false;
36198 tree block = NULL_TREE;
36199 if (current_function_decl)
36201 block_scope = true;
36202 DECL_CONTEXT (fndecl) = global_namespace;
36203 if (!processing_template_decl)
36204 pushdecl (fndecl);
36206 else if (current_class_type)
36208 if (cp == NULL)
36210 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36211 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36212 cp_lexer_consume_token (parser->lexer);
36213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36214 goto fail;
36215 cp = cp_token_cache_new (first_token,
36216 cp_lexer_peek_nth_token (parser->lexer,
36217 2));
36219 DECL_STATIC_FUNCTION_P (fndecl) = 1;
36220 finish_member_declaration (fndecl);
36221 DECL_PENDING_INLINE_INFO (fndecl) = cp;
36222 DECL_PENDING_INLINE_P (fndecl) = 1;
36223 vec_safe_push (unparsed_funs_with_definitions, fndecl);
36224 continue;
36226 else
36228 DECL_CONTEXT (fndecl) = current_namespace;
36229 pushdecl (fndecl);
36231 if (!block_scope)
36232 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
36233 else
36234 block = begin_omp_structured_block ();
36235 if (cp)
36237 cp_parser_push_lexer_for_tokens (parser, cp);
36238 parser->lexer->in_pragma = true;
36240 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
36242 if (!block_scope)
36243 finish_function (0);
36244 else
36245 DECL_CONTEXT (fndecl) = current_function_decl;
36246 if (cp)
36247 cp_parser_pop_lexer (parser);
36248 goto fail;
36250 if (cp)
36251 cp_parser_pop_lexer (parser);
36252 if (!block_scope)
36253 finish_function (0);
36254 else
36256 DECL_CONTEXT (fndecl) = current_function_decl;
36257 block = finish_omp_structured_block (block);
36258 if (TREE_CODE (block) == BIND_EXPR)
36259 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
36260 else if (TREE_CODE (block) == STATEMENT_LIST)
36261 DECL_SAVED_TREE (fndecl) = block;
36262 if (processing_template_decl)
36263 add_decl_expr (fndecl);
36265 cp_check_omp_declare_reduction (fndecl);
36266 if (cp == NULL && types.length () > 1)
36267 cp = cp_token_cache_new (first_token,
36268 cp_lexer_peek_nth_token (parser->lexer, 2));
36269 if (errs != errorcount)
36270 break;
36273 cp_parser_require_pragma_eol (parser, pragma_tok);
36275 done:
36276 /* Free any declarators allocated. */
36277 obstack_free (&declarator_obstack, p);
36280 /* OpenMP 4.0
36281 #pragma omp declare simd declare-simd-clauses[optseq] new-line
36282 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36283 initializer-clause[opt] new-line
36284 #pragma omp declare target new-line */
36286 static void
36287 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
36288 enum pragma_context context)
36290 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36292 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36293 const char *p = IDENTIFIER_POINTER (id);
36295 if (strcmp (p, "simd") == 0)
36297 cp_lexer_consume_token (parser->lexer);
36298 cp_parser_omp_declare_simd (parser, pragma_tok,
36299 context);
36300 return;
36302 cp_ensure_no_omp_declare_simd (parser);
36303 if (strcmp (p, "reduction") == 0)
36305 cp_lexer_consume_token (parser->lexer);
36306 cp_parser_omp_declare_reduction (parser, pragma_tok,
36307 context);
36308 return;
36310 if (!flag_openmp) /* flag_openmp_simd */
36312 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36313 return;
36315 if (strcmp (p, "target") == 0)
36317 cp_lexer_consume_token (parser->lexer);
36318 cp_parser_omp_declare_target (parser, pragma_tok);
36319 return;
36322 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
36323 "or %<target%>");
36324 cp_parser_require_pragma_eol (parser, pragma_tok);
36327 /* OpenMP 4.5:
36328 #pragma omp taskloop taskloop-clause[optseq] new-line
36329 for-loop
36331 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
36332 for-loop */
36334 #define OMP_TASKLOOP_CLAUSE_MASK \
36335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
36350 static tree
36351 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
36352 char *p_name, omp_clause_mask mask, tree *cclauses,
36353 bool *if_p)
36355 tree clauses, sb, ret;
36356 unsigned int save;
36357 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36359 strcat (p_name, " taskloop");
36360 mask |= OMP_TASKLOOP_CLAUSE_MASK;
36362 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36364 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36365 const char *p = IDENTIFIER_POINTER (id);
36367 if (strcmp (p, "simd") == 0)
36369 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
36370 if (cclauses == NULL)
36371 cclauses = cclauses_buf;
36373 cp_lexer_consume_token (parser->lexer);
36374 if (!flag_openmp) /* flag_openmp_simd */
36375 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36376 cclauses, if_p);
36377 sb = begin_omp_structured_block ();
36378 save = cp_parser_begin_omp_structured_block (parser);
36379 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
36380 cclauses, if_p);
36381 cp_parser_end_omp_structured_block (parser, save);
36382 tree body = finish_omp_structured_block (sb);
36383 if (ret == NULL)
36384 return ret;
36385 ret = make_node (OMP_TASKLOOP);
36386 TREE_TYPE (ret) = void_type_node;
36387 OMP_FOR_BODY (ret) = body;
36388 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36389 SET_EXPR_LOCATION (ret, loc);
36390 add_stmt (ret);
36391 return ret;
36394 if (!flag_openmp) /* flag_openmp_simd */
36396 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36397 return NULL_TREE;
36400 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
36401 cclauses == NULL);
36402 if (cclauses)
36404 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
36405 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
36408 sb = begin_omp_structured_block ();
36409 save = cp_parser_begin_omp_structured_block (parser);
36411 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
36412 if_p);
36414 cp_parser_end_omp_structured_block (parser, save);
36415 add_stmt (finish_omp_structured_block (sb));
36417 return ret;
36421 /* OpenACC 2.0:
36422 # pragma acc routine oacc-routine-clause[optseq] new-line
36423 function-definition
36425 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
36428 #define OACC_ROUTINE_CLAUSE_MASK \
36429 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36430 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36431 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36432 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
36435 /* Parse the OpenACC routine pragma. This has an optional '( name )'
36436 component, which must resolve to a declared namespace-scope
36437 function. The clauses are either processed directly (for a named
36438 function), or defered until the immediatley following declaration
36439 is parsed. */
36441 static void
36442 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
36443 enum pragma_context context)
36445 bool first_p = parser->oacc_routine == NULL;
36446 location_t loc = pragma_tok->location;
36447 cp_omp_declare_simd_data data;
36448 if (first_p)
36450 data.error_seen = false;
36451 data.fndecl_seen = false;
36452 data.tokens = vNULL;
36453 data.clauses = NULL_TREE;
36454 parser->oacc_routine = &data;
36457 tree decl = NULL_TREE;
36458 /* Create a dummy claue, to record location. */
36459 tree c_head = build_omp_clause (pragma_tok->location, OMP_CLAUSE_SEQ);
36461 if (context != pragma_external)
36463 cp_parser_error (parser, "%<#pragma acc routine%> not at file scope");
36464 parser->oacc_routine->error_seen = true;
36465 parser->oacc_routine = NULL;
36466 return;
36469 /* Look for optional '( name )'. */
36470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36472 if (!first_p)
36474 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36475 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36476 cp_lexer_consume_token (parser->lexer);
36477 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36478 parser->oacc_routine->error_seen = true;
36479 cp_parser_require_pragma_eol (parser, pragma_tok);
36481 error_at (OMP_CLAUSE_LOCATION (parser->oacc_routine->clauses),
36482 "%<#pragma acc routine%> not followed by a "
36483 "function declaration or definition");
36485 parser->oacc_routine->error_seen = true;
36486 return;
36489 cp_lexer_consume_token (parser->lexer);
36490 cp_token *token = cp_lexer_peek_token (parser->lexer);
36492 /* We parse the name as an id-expression. If it resolves to
36493 anything other than a non-overloaded function at namespace
36494 scope, it's an error. */
36495 tree id = cp_parser_id_expression (parser,
36496 /*template_keyword_p=*/false,
36497 /*check_dependency_p=*/false,
36498 /*template_p=*/NULL,
36499 /*declarator_p=*/false,
36500 /*optional_p=*/false);
36501 decl = cp_parser_lookup_name_simple (parser, id, token->location);
36502 if (id != error_mark_node && decl == error_mark_node)
36503 cp_parser_name_lookup_error (parser, id, decl, NLE_NULL,
36504 token->location);
36506 if (decl == error_mark_node
36507 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36509 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36510 parser->oacc_routine = NULL;
36511 return;
36514 /* Build a chain of clauses. */
36515 parser->lexer->in_pragma = true;
36516 tree clauses = NULL_TREE;
36517 clauses = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36518 "#pragma acc routine",
36519 cp_lexer_peek_token
36520 (parser->lexer));
36522 /* Force clauses to be non-null, by attaching context to it. */
36523 clauses = tree_cons (c_head, clauses, NULL_TREE);
36525 if (decl && is_overloaded_fn (decl)
36526 && (TREE_CODE (decl) != FUNCTION_DECL
36527 || DECL_FUNCTION_TEMPLATE_P (decl)))
36529 error_at (loc, "%<#pragma acc routine%> names a set of overloads");
36530 parser->oacc_routine = NULL;
36531 return;
36534 /* Perhaps we should use the same rule as declarations in different
36535 namespaces? */
36536 if (!DECL_NAMESPACE_SCOPE_P (decl))
36538 error_at (loc, "%<#pragma acc routine%> does not refer to a "
36539 "namespace scope function");
36540 parser->oacc_routine = NULL;
36541 return;
36544 if (!decl || TREE_CODE (decl) != FUNCTION_DECL)
36546 error_at (loc,
36547 "%<#pragma acc routine%> does not refer to a function");
36548 parser->oacc_routine = NULL;
36549 return;
36552 data.clauses = clauses;
36554 cp_finalize_oacc_routine (parser, decl, false);
36555 data.tokens.release ();
36556 parser->oacc_routine = NULL;
36558 else
36560 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36561 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36562 cp_lexer_consume_token (parser->lexer);
36563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36564 parser->oacc_routine->error_seen = true;
36565 cp_parser_require_pragma_eol (parser, pragma_tok);
36567 struct cp_token_cache *cp
36568 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36569 parser->oacc_routine->tokens.safe_push (cp);
36571 if (first_p)
36572 parser->oacc_routine->clauses = c_head;
36574 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36575 cp_parser_pragma (parser, context, NULL);
36577 if (first_p)
36579 /* Create an empty list of clauses. */
36580 parser->oacc_routine->clauses = tree_cons (c_head, NULL_TREE,
36581 NULL_TREE);
36582 cp_parser_declaration (parser);
36584 if (parser->oacc_routine
36585 && !parser->oacc_routine->error_seen
36586 && !parser->oacc_routine->fndecl_seen)
36587 error_at (loc, "%<#pragma acc routine%> not followed by a "
36588 "function declaration or definition");
36590 data.tokens.release ();
36591 parser->oacc_routine = NULL;
36596 /* Finalize #pragma acc routine clauses after direct declarator has
36597 been parsed, and put that into "oacc function" attribute. */
36599 static tree
36600 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
36602 struct cp_token_cache *ce;
36603 cp_omp_declare_simd_data *data = parser->oacc_routine;
36604 tree cl, clauses = parser->oacc_routine->clauses;
36605 location_t loc;
36607 loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36609 if ((!data->error_seen && data->fndecl_seen)
36610 || data->tokens.length () != 1)
36612 error_at (loc, "%<#pragma acc routine%> not followed by a "
36613 "function declaration or definition");
36614 data->error_seen = true;
36615 return attrs;
36617 if (data->error_seen)
36618 return attrs;
36620 ce = data->tokens[0];
36622 cp_parser_push_lexer_for_tokens (parser, ce);
36623 parser->lexer->in_pragma = true;
36624 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36626 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36627 cl = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
36628 "#pragma acc routine", pragma_tok);
36629 cp_parser_pop_lexer (parser);
36631 tree c_head = build_omp_clause (loc, OMP_CLAUSE_SEQ);
36633 /* Force clauses to be non-null, by attaching context to it. */
36634 parser->oacc_routine->clauses = tree_cons (c_head, cl, NULL_TREE);
36636 data->fndecl_seen = true;
36637 return attrs;
36640 /* Apply any saved OpenACC routine clauses to a just-parsed
36641 declaration. */
36643 static void
36644 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
36646 if (__builtin_expect (parser->oacc_routine != NULL, 0))
36648 tree clauses = parser->oacc_routine->clauses;
36649 location_t loc = OMP_CLAUSE_LOCATION (TREE_PURPOSE(clauses));
36651 if (parser->oacc_routine->error_seen)
36652 return;
36654 if (fndecl == error_mark_node)
36656 parser->oacc_routine = NULL;
36657 return;
36660 if (TREE_CODE (fndecl) != FUNCTION_DECL)
36662 cp_ensure_no_oacc_routine (parser);
36663 return;
36666 if (!fndecl || TREE_CODE (fndecl) != FUNCTION_DECL)
36668 error_at (loc,
36669 "%<#pragma acc routine%> not followed by a function "
36670 "declaration or definition");
36671 parser->oacc_routine = NULL;
36674 if (get_oacc_fn_attrib (fndecl))
36676 error_at (loc, "%<#pragma acc routine%> already applied to %D",
36677 fndecl);
36678 parser->oacc_routine = NULL;
36681 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
36683 error_at (loc, "%<#pragma acc routine%> must be applied before %s",
36684 TREE_USED (fndecl) ? "use" : "definition");
36685 parser->oacc_routine = NULL;
36688 /* Process for function attrib */
36689 tree dims = build_oacc_routine_dims (TREE_VALUE (clauses));
36690 replace_oacc_fn_attrib (fndecl, dims);
36692 /* Add an "omp target" attribute. */
36693 DECL_ATTRIBUTES (fndecl)
36694 = tree_cons (get_identifier ("omp declare target"),
36695 NULL_TREE, DECL_ATTRIBUTES (fndecl));
36699 /* Main entry point to OpenMP statement pragmas. */
36701 static void
36702 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36704 tree stmt;
36705 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
36706 omp_clause_mask mask (0);
36708 switch (cp_parser_pragma_kind (pragma_tok))
36710 case PRAGMA_OACC_ATOMIC:
36711 cp_parser_omp_atomic (parser, pragma_tok);
36712 return;
36713 case PRAGMA_OACC_CACHE:
36714 stmt = cp_parser_oacc_cache (parser, pragma_tok);
36715 break;
36716 case PRAGMA_OACC_DATA:
36717 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
36718 break;
36719 case PRAGMA_OACC_ENTER_DATA:
36720 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
36721 break;
36722 case PRAGMA_OACC_EXIT_DATA:
36723 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
36724 break;
36725 case PRAGMA_OACC_HOST_DATA:
36726 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
36727 break;
36728 case PRAGMA_OACC_KERNELS:
36729 case PRAGMA_OACC_PARALLEL:
36730 strcpy (p_name, "#pragma acc");
36731 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
36732 if_p);
36733 break;
36734 case PRAGMA_OACC_LOOP:
36735 strcpy (p_name, "#pragma acc");
36736 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
36737 if_p);
36738 break;
36739 case PRAGMA_OACC_UPDATE:
36740 stmt = cp_parser_oacc_update (parser, pragma_tok);
36741 break;
36742 case PRAGMA_OACC_WAIT:
36743 stmt = cp_parser_oacc_wait (parser, pragma_tok);
36744 break;
36745 case PRAGMA_OMP_ATOMIC:
36746 cp_parser_omp_atomic (parser, pragma_tok);
36747 return;
36748 case PRAGMA_OMP_CRITICAL:
36749 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
36750 break;
36751 case PRAGMA_OMP_DISTRIBUTE:
36752 strcpy (p_name, "#pragma omp");
36753 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
36754 if_p);
36755 break;
36756 case PRAGMA_OMP_FOR:
36757 strcpy (p_name, "#pragma omp");
36758 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
36759 if_p);
36760 break;
36761 case PRAGMA_OMP_MASTER:
36762 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
36763 break;
36764 case PRAGMA_OMP_PARALLEL:
36765 strcpy (p_name, "#pragma omp");
36766 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
36767 if_p);
36768 break;
36769 case PRAGMA_OMP_SECTIONS:
36770 strcpy (p_name, "#pragma omp");
36771 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
36772 break;
36773 case PRAGMA_OMP_SIMD:
36774 strcpy (p_name, "#pragma omp");
36775 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
36776 if_p);
36777 break;
36778 case PRAGMA_OMP_SINGLE:
36779 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
36780 break;
36781 case PRAGMA_OMP_TASK:
36782 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
36783 break;
36784 case PRAGMA_OMP_TASKGROUP:
36785 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
36786 break;
36787 case PRAGMA_OMP_TASKLOOP:
36788 strcpy (p_name, "#pragma omp");
36789 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
36790 if_p);
36791 break;
36792 case PRAGMA_OMP_TEAMS:
36793 strcpy (p_name, "#pragma omp");
36794 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
36795 if_p);
36796 break;
36797 default:
36798 gcc_unreachable ();
36801 protected_set_expr_location (stmt, pragma_tok->location);
36804 /* Transactional Memory parsing routines. */
36806 /* Parse a transaction attribute.
36808 txn-attribute:
36809 attribute
36810 [ [ identifier ] ]
36812 We use this instead of cp_parser_attributes_opt for transactions to avoid
36813 the pedwarn in C++98 mode. */
36815 static tree
36816 cp_parser_txn_attribute_opt (cp_parser *parser)
36818 cp_token *token;
36819 tree attr_name, attr = NULL;
36821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
36822 return cp_parser_attributes_opt (parser);
36824 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
36825 return NULL_TREE;
36826 cp_lexer_consume_token (parser->lexer);
36827 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
36828 goto error1;
36830 token = cp_lexer_peek_token (parser->lexer);
36831 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
36833 token = cp_lexer_consume_token (parser->lexer);
36835 attr_name = (token->type == CPP_KEYWORD
36836 /* For keywords, use the canonical spelling,
36837 not the parsed identifier. */
36838 ? ridpointers[(int) token->keyword]
36839 : token->u.value);
36840 attr = build_tree_list (attr_name, NULL_TREE);
36842 else
36843 cp_parser_error (parser, "expected identifier");
36845 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36846 error1:
36847 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
36848 return attr;
36851 /* Parse a __transaction_atomic or __transaction_relaxed statement.
36853 transaction-statement:
36854 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
36855 compound-statement
36856 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
36859 static tree
36860 cp_parser_transaction (cp_parser *parser, cp_token *token)
36862 unsigned char old_in = parser->in_transaction;
36863 unsigned char this_in = 1, new_in;
36864 enum rid keyword = token->keyword;
36865 tree stmt, attrs, noex;
36867 cp_lexer_consume_token (parser->lexer);
36869 if (keyword == RID_TRANSACTION_RELAXED
36870 || keyword == RID_SYNCHRONIZED)
36871 this_in |= TM_STMT_ATTR_RELAXED;
36872 else
36874 attrs = cp_parser_txn_attribute_opt (parser);
36875 if (attrs)
36876 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
36879 /* Parse a noexcept specification. */
36880 if (keyword == RID_ATOMIC_NOEXCEPT)
36881 noex = boolean_true_node;
36882 else if (keyword == RID_ATOMIC_CANCEL)
36884 /* cancel-and-throw is unimplemented. */
36885 sorry ("atomic_cancel");
36886 noex = NULL_TREE;
36888 else
36889 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
36891 /* Keep track if we're in the lexical scope of an outer transaction. */
36892 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
36894 stmt = begin_transaction_stmt (token->location, NULL, this_in);
36896 parser->in_transaction = new_in;
36897 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
36898 parser->in_transaction = old_in;
36900 finish_transaction_stmt (stmt, NULL, this_in, noex);
36902 return stmt;
36905 /* Parse a __transaction_atomic or __transaction_relaxed expression.
36907 transaction-expression:
36908 __transaction_atomic txn-noexcept-spec[opt] ( expression )
36909 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
36912 static tree
36913 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
36915 unsigned char old_in = parser->in_transaction;
36916 unsigned char this_in = 1;
36917 cp_token *token;
36918 tree expr, noex;
36919 bool noex_expr;
36920 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36922 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36923 || keyword == RID_TRANSACTION_RELAXED);
36925 if (!flag_tm)
36926 error_at (loc,
36927 keyword == RID_TRANSACTION_RELAXED
36928 ? G_("%<__transaction_relaxed%> without transactional memory "
36929 "support enabled")
36930 : G_("%<__transaction_atomic%> without transactional memory "
36931 "support enabled"));
36933 token = cp_parser_require_keyword (parser, keyword,
36934 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
36935 : RT_TRANSACTION_RELAXED));
36936 gcc_assert (token != NULL);
36938 if (keyword == RID_TRANSACTION_RELAXED)
36939 this_in |= TM_STMT_ATTR_RELAXED;
36941 /* Set this early. This might mean that we allow transaction_cancel in
36942 an expression that we find out later actually has to be a constexpr.
36943 However, we expect that cxx_constant_value will be able to deal with
36944 this; also, if the noexcept has no constexpr, then what we parse next
36945 really is a transaction's body. */
36946 parser->in_transaction = this_in;
36948 /* Parse a noexcept specification. */
36949 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
36950 true);
36952 if (!noex || !noex_expr
36953 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36955 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
36957 expr = cp_parser_expression (parser);
36958 expr = finish_parenthesized_expr (expr);
36960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
36962 else
36964 /* The only expression that is available got parsed for the noexcept
36965 already. noexcept is true then. */
36966 expr = noex;
36967 noex = boolean_true_node;
36970 expr = build_transaction_expr (token->location, expr, this_in, noex);
36971 parser->in_transaction = old_in;
36973 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
36974 return error_mark_node;
36976 return (flag_tm ? expr : error_mark_node);
36979 /* Parse a function-transaction-block.
36981 function-transaction-block:
36982 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
36983 function-body
36984 __transaction_atomic txn-attribute[opt] function-try-block
36985 __transaction_relaxed ctor-initializer[opt] function-body
36986 __transaction_relaxed function-try-block
36989 static bool
36990 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
36992 unsigned char old_in = parser->in_transaction;
36993 unsigned char new_in = 1;
36994 tree compound_stmt, stmt, attrs;
36995 bool ctor_initializer_p;
36996 cp_token *token;
36998 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
36999 || keyword == RID_TRANSACTION_RELAXED);
37000 token = cp_parser_require_keyword (parser, keyword,
37001 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37002 : RT_TRANSACTION_RELAXED));
37003 gcc_assert (token != NULL);
37005 if (keyword == RID_TRANSACTION_RELAXED)
37006 new_in |= TM_STMT_ATTR_RELAXED;
37007 else
37009 attrs = cp_parser_txn_attribute_opt (parser);
37010 if (attrs)
37011 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37014 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37016 parser->in_transaction = new_in;
37018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37019 ctor_initializer_p = cp_parser_function_try_block (parser);
37020 else
37021 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37022 (parser, /*in_function_try_block=*/false);
37024 parser->in_transaction = old_in;
37026 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37028 return ctor_initializer_p;
37031 /* Parse a __transaction_cancel statement.
37033 cancel-statement:
37034 __transaction_cancel txn-attribute[opt] ;
37035 __transaction_cancel txn-attribute[opt] throw-expression ;
37037 ??? Cancel and throw is not yet implemented. */
37039 static tree
37040 cp_parser_transaction_cancel (cp_parser *parser)
37042 cp_token *token;
37043 bool is_outer = false;
37044 tree stmt, attrs;
37046 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37047 RT_TRANSACTION_CANCEL);
37048 gcc_assert (token != NULL);
37050 attrs = cp_parser_txn_attribute_opt (parser);
37051 if (attrs)
37052 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37054 /* ??? Parse cancel-and-throw here. */
37056 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37058 if (!flag_tm)
37060 error_at (token->location, "%<__transaction_cancel%> without "
37061 "transactional memory support enabled");
37062 return error_mark_node;
37064 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37066 error_at (token->location, "%<__transaction_cancel%> within a "
37067 "%<__transaction_relaxed%>");
37068 return error_mark_node;
37070 else if (is_outer)
37072 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37073 && !is_tm_may_cancel_outer (current_function_decl))
37075 error_at (token->location, "outer %<__transaction_cancel%> not "
37076 "within outer %<__transaction_atomic%>");
37077 error_at (token->location,
37078 " or a %<transaction_may_cancel_outer%> function");
37079 return error_mark_node;
37082 else if (parser->in_transaction == 0)
37084 error_at (token->location, "%<__transaction_cancel%> not within "
37085 "%<__transaction_atomic%>");
37086 return error_mark_node;
37089 stmt = build_tm_abort_call (token->location, is_outer);
37090 add_stmt (stmt);
37092 return stmt;
37095 /* The parser. */
37097 static GTY (()) cp_parser *the_parser;
37100 /* Special handling for the first token or line in the file. The first
37101 thing in the file might be #pragma GCC pch_preprocess, which loads a
37102 PCH file, which is a GC collection point. So we need to handle this
37103 first pragma without benefit of an existing lexer structure.
37105 Always returns one token to the caller in *FIRST_TOKEN. This is
37106 either the true first token of the file, or the first token after
37107 the initial pragma. */
37109 static void
37110 cp_parser_initial_pragma (cp_token *first_token)
37112 tree name = NULL;
37114 cp_lexer_get_preprocessor_token (NULL, first_token);
37115 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
37116 return;
37118 cp_lexer_get_preprocessor_token (NULL, first_token);
37119 if (first_token->type == CPP_STRING)
37121 name = first_token->u.value;
37123 cp_lexer_get_preprocessor_token (NULL, first_token);
37124 if (first_token->type != CPP_PRAGMA_EOL)
37125 error_at (first_token->location,
37126 "junk at end of %<#pragma GCC pch_preprocess%>");
37128 else
37129 error_at (first_token->location, "expected string literal");
37131 /* Skip to the end of the pragma. */
37132 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
37133 cp_lexer_get_preprocessor_token (NULL, first_token);
37135 /* Now actually load the PCH file. */
37136 if (name)
37137 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
37139 /* Read one more token to return to our caller. We have to do this
37140 after reading the PCH file in, since its pointers have to be
37141 live. */
37142 cp_lexer_get_preprocessor_token (NULL, first_token);
37145 /* Parses the grainsize pragma for the _Cilk_for statement.
37146 Syntax:
37147 #pragma cilk grainsize = <VALUE>. */
37149 static void
37150 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37152 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
37154 tree exp = cp_parser_binary_expression (parser, false, false,
37155 PREC_NOT_OPERATOR, NULL);
37156 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37157 if (!exp || exp == error_mark_node)
37159 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
37160 return;
37163 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
37164 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
37165 cp_parser_cilk_for (parser, exp, if_p);
37166 else
37167 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
37168 "%<#pragma cilk grainsize%> is not followed by "
37169 "%<_Cilk_for%>");
37170 return;
37172 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37175 /* Normal parsing of a pragma token. Here we can (and must) use the
37176 regular lexer. */
37178 static bool
37179 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
37181 cp_token *pragma_tok;
37182 unsigned int id;
37183 tree stmt;
37184 bool ret;
37186 pragma_tok = cp_lexer_consume_token (parser->lexer);
37187 gcc_assert (pragma_tok->type == CPP_PRAGMA);
37188 parser->lexer->in_pragma = true;
37190 id = cp_parser_pragma_kind (pragma_tok);
37191 if (id != PRAGMA_OMP_DECLARE_REDUCTION && id != PRAGMA_OACC_ROUTINE)
37192 cp_ensure_no_omp_declare_simd (parser);
37193 switch (id)
37195 case PRAGMA_GCC_PCH_PREPROCESS:
37196 error_at (pragma_tok->location,
37197 "%<#pragma GCC pch_preprocess%> must be first");
37198 break;
37200 case PRAGMA_OMP_BARRIER:
37201 switch (context)
37203 case pragma_compound:
37204 cp_parser_omp_barrier (parser, pragma_tok);
37205 return false;
37206 case pragma_stmt:
37207 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
37208 "used in compound statements");
37209 break;
37210 default:
37211 goto bad_stmt;
37213 break;
37215 case PRAGMA_OMP_FLUSH:
37216 switch (context)
37218 case pragma_compound:
37219 cp_parser_omp_flush (parser, pragma_tok);
37220 return false;
37221 case pragma_stmt:
37222 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
37223 "used in compound statements");
37224 break;
37225 default:
37226 goto bad_stmt;
37228 break;
37230 case PRAGMA_OMP_TASKWAIT:
37231 switch (context)
37233 case pragma_compound:
37234 cp_parser_omp_taskwait (parser, pragma_tok);
37235 return false;
37236 case pragma_stmt:
37237 error_at (pragma_tok->location,
37238 "%<#pragma omp taskwait%> may only be "
37239 "used in compound statements");
37240 break;
37241 default:
37242 goto bad_stmt;
37244 break;
37246 case PRAGMA_OMP_TASKYIELD:
37247 switch (context)
37249 case pragma_compound:
37250 cp_parser_omp_taskyield (parser, pragma_tok);
37251 return false;
37252 case pragma_stmt:
37253 error_at (pragma_tok->location,
37254 "%<#pragma omp taskyield%> may only be "
37255 "used in compound statements");
37256 break;
37257 default:
37258 goto bad_stmt;
37260 break;
37262 case PRAGMA_OMP_CANCEL:
37263 switch (context)
37265 case pragma_compound:
37266 cp_parser_omp_cancel (parser, pragma_tok);
37267 return false;
37268 case pragma_stmt:
37269 error_at (pragma_tok->location,
37270 "%<#pragma omp cancel%> may only be "
37271 "used in compound statements");
37272 break;
37273 default:
37274 goto bad_stmt;
37276 break;
37278 case PRAGMA_OMP_CANCELLATION_POINT:
37279 switch (context)
37281 case pragma_compound:
37282 cp_parser_omp_cancellation_point (parser, pragma_tok);
37283 return false;
37284 case pragma_stmt:
37285 error_at (pragma_tok->location,
37286 "%<#pragma omp cancellation point%> may only be "
37287 "used in compound statements");
37288 break;
37289 default:
37290 goto bad_stmt;
37292 break;
37294 case PRAGMA_OMP_THREADPRIVATE:
37295 cp_parser_omp_threadprivate (parser, pragma_tok);
37296 return false;
37298 case PRAGMA_OMP_DECLARE_REDUCTION:
37299 cp_parser_omp_declare (parser, pragma_tok, context);
37300 return false;
37302 case PRAGMA_OACC_DECLARE:
37303 cp_parser_oacc_declare (parser, pragma_tok);
37304 return false;
37306 case PRAGMA_OACC_ROUTINE:
37307 cp_parser_oacc_routine (parser, pragma_tok, context);
37308 return false;
37310 case PRAGMA_OACC_ATOMIC:
37311 case PRAGMA_OACC_CACHE:
37312 case PRAGMA_OACC_DATA:
37313 case PRAGMA_OACC_ENTER_DATA:
37314 case PRAGMA_OACC_EXIT_DATA:
37315 case PRAGMA_OACC_HOST_DATA:
37316 case PRAGMA_OACC_KERNELS:
37317 case PRAGMA_OACC_PARALLEL:
37318 case PRAGMA_OACC_LOOP:
37319 case PRAGMA_OACC_UPDATE:
37320 case PRAGMA_OACC_WAIT:
37321 case PRAGMA_OMP_ATOMIC:
37322 case PRAGMA_OMP_CRITICAL:
37323 case PRAGMA_OMP_DISTRIBUTE:
37324 case PRAGMA_OMP_FOR:
37325 case PRAGMA_OMP_MASTER:
37326 case PRAGMA_OMP_PARALLEL:
37327 case PRAGMA_OMP_SECTIONS:
37328 case PRAGMA_OMP_SIMD:
37329 case PRAGMA_OMP_SINGLE:
37330 case PRAGMA_OMP_TASK:
37331 case PRAGMA_OMP_TASKGROUP:
37332 case PRAGMA_OMP_TASKLOOP:
37333 case PRAGMA_OMP_TEAMS:
37334 if (context != pragma_stmt && context != pragma_compound)
37335 goto bad_stmt;
37336 stmt = push_omp_privatization_clauses (false);
37337 cp_parser_omp_construct (parser, pragma_tok, if_p);
37338 pop_omp_privatization_clauses (stmt);
37339 return true;
37341 case PRAGMA_OMP_ORDERED:
37342 stmt = push_omp_privatization_clauses (false);
37343 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
37344 pop_omp_privatization_clauses (stmt);
37345 return ret;
37347 case PRAGMA_OMP_TARGET:
37348 stmt = push_omp_privatization_clauses (false);
37349 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
37350 pop_omp_privatization_clauses (stmt);
37351 return ret;
37353 case PRAGMA_OMP_END_DECLARE_TARGET:
37354 cp_parser_omp_end_declare_target (parser, pragma_tok);
37355 return false;
37357 case PRAGMA_OMP_SECTION:
37358 error_at (pragma_tok->location,
37359 "%<#pragma omp section%> may only be used in "
37360 "%<#pragma omp sections%> construct");
37361 break;
37363 case PRAGMA_IVDEP:
37365 if (context == pragma_external)
37367 error_at (pragma_tok->location,
37368 "%<#pragma GCC ivdep%> must be inside a function");
37369 break;
37371 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37372 cp_token *tok;
37373 tok = cp_lexer_peek_token (the_parser->lexer);
37374 if (tok->type != CPP_KEYWORD
37375 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
37376 && tok->keyword != RID_DO))
37378 cp_parser_error (parser, "for, while or do statement expected");
37379 return false;
37381 cp_parser_iteration_statement (parser, if_p, true);
37382 return true;
37385 case PRAGMA_CILK_SIMD:
37386 if (context == pragma_external)
37388 error_at (pragma_tok->location,
37389 "%<#pragma simd%> must be inside a function");
37390 break;
37392 stmt = push_omp_privatization_clauses (false);
37393 cp_parser_cilk_simd (parser, pragma_tok, if_p);
37394 pop_omp_privatization_clauses (stmt);
37395 return true;
37397 case PRAGMA_CILK_GRAINSIZE:
37398 if (context == pragma_external)
37400 error_at (pragma_tok->location,
37401 "%<#pragma cilk grainsize%> must be inside a function");
37402 break;
37405 /* Ignore the pragma if Cilk Plus is not enabled. */
37406 if (flag_cilkplus)
37408 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
37409 return true;
37411 else
37413 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
37414 "%<#pragma cilk grainsize%>");
37415 break;
37418 default:
37419 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
37420 c_invoke_pragma_handler (id);
37421 break;
37423 bad_stmt:
37424 cp_parser_error (parser, "expected declaration specifiers");
37425 break;
37428 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37429 return false;
37432 /* The interface the pragma parsers have to the lexer. */
37434 enum cpp_ttype
37435 pragma_lex (tree *value, location_t *loc)
37437 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
37438 enum cpp_ttype ret = tok->type;
37440 *value = tok->u.value;
37441 if (loc)
37442 *loc = tok->location;
37444 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
37445 ret = CPP_EOF;
37446 else if (ret == CPP_STRING)
37447 *value = cp_parser_string_literal (the_parser, false, false);
37448 else
37450 if (ret == CPP_KEYWORD)
37451 ret = CPP_NAME;
37452 cp_lexer_consume_token (the_parser->lexer);
37455 return ret;
37459 /* External interface. */
37461 /* Parse one entire translation unit. */
37463 void
37464 c_parse_file (void)
37466 static bool already_called = false;
37468 if (already_called)
37469 fatal_error (input_location,
37470 "inter-module optimizations not implemented for C++");
37471 already_called = true;
37473 the_parser = cp_parser_new ();
37474 push_deferring_access_checks (flag_access_control
37475 ? dk_no_deferred : dk_no_check);
37476 cp_parser_translation_unit (the_parser);
37477 the_parser = NULL;
37480 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
37481 vectorlength clause:
37482 Syntax:
37483 vectorlength ( constant-expression ) */
37485 static tree
37486 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
37487 bool is_simd_fn)
37489 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37490 tree expr;
37491 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
37492 safelen clause. Thus, vectorlength is represented as OMP 4.0
37493 safelen. For SIMD-enabled function it is represented by OMP 4.0
37494 simdlen. */
37495 if (!is_simd_fn)
37496 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
37497 loc);
37498 else
37499 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
37500 loc);
37502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37503 return error_mark_node;
37505 expr = cp_parser_constant_expression (parser);
37506 expr = maybe_constant_value (expr);
37508 /* If expr == error_mark_node, then don't emit any errors nor
37509 create a clause. if any of the above functions returns
37510 error mark node then they would have emitted an error message. */
37511 if (expr == error_mark_node)
37513 else if (!TREE_TYPE (expr)
37514 || !TREE_CONSTANT (expr)
37515 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
37516 error_at (loc, "vectorlength must be an integer constant");
37517 else if (TREE_CONSTANT (expr)
37518 && exact_log2 (TREE_INT_CST_LOW (expr)) == -1)
37519 error_at (loc, "vectorlength must be a power of 2");
37520 else
37522 tree c;
37523 if (!is_simd_fn)
37525 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
37526 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
37527 OMP_CLAUSE_CHAIN (c) = clauses;
37528 clauses = c;
37530 else
37532 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
37533 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
37534 OMP_CLAUSE_CHAIN (c) = clauses;
37535 clauses = c;
37539 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37540 return error_mark_node;
37541 return clauses;
37544 /* Handles the Cilk Plus #pragma simd linear clause.
37545 Syntax:
37546 linear ( simd-linear-variable-list )
37548 simd-linear-variable-list:
37549 simd-linear-variable
37550 simd-linear-variable-list , simd-linear-variable
37552 simd-linear-variable:
37553 id-expression
37554 id-expression : simd-linear-step
37556 simd-linear-step:
37557 conditional-expression */
37559 static tree
37560 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
37562 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37564 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37565 return clauses;
37566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37568 cp_parser_error (parser, "expected identifier");
37569 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37570 return error_mark_node;
37573 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37574 parser->colon_corrects_to_scope_p = false;
37575 while (1)
37577 cp_token *token = cp_lexer_peek_token (parser->lexer);
37578 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
37580 cp_parser_error (parser, "expected variable-name");
37581 clauses = error_mark_node;
37582 break;
37585 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
37586 false, false);
37587 tree decl = cp_parser_lookup_name_simple (parser, var_name,
37588 token->location);
37589 if (decl == error_mark_node)
37591 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
37592 token->location);
37593 clauses = error_mark_node;
37595 else
37597 tree e = NULL_TREE;
37598 tree step_size = integer_one_node;
37600 /* If present, parse the linear step. Otherwise, assume the default
37601 value of 1. */
37602 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
37604 cp_lexer_consume_token (parser->lexer);
37606 e = cp_parser_assignment_expression (parser);
37607 e = maybe_constant_value (e);
37609 if (e == error_mark_node)
37611 /* If an error has occurred, then the whole pragma is
37612 considered ill-formed. Thus, no reason to keep
37613 parsing. */
37614 clauses = error_mark_node;
37615 break;
37617 else if (type_dependent_expression_p (e)
37618 || value_dependent_expression_p (e)
37619 || (TREE_TYPE (e)
37620 && INTEGRAL_TYPE_P (TREE_TYPE (e))
37621 && (TREE_CONSTANT (e)
37622 || DECL_P (e))))
37623 step_size = e;
37624 else
37625 cp_parser_error (parser,
37626 "step size must be an integer constant "
37627 "expression or an integer variable");
37630 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
37631 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
37632 OMP_CLAUSE_DECL (l) = decl;
37633 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
37634 OMP_CLAUSE_CHAIN (l) = clauses;
37635 clauses = l;
37637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37638 cp_lexer_consume_token (parser->lexer);
37639 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
37640 break;
37641 else
37643 error_at (cp_lexer_peek_token (parser->lexer)->location,
37644 "expected %<,%> or %<)%> after %qE", decl);
37645 clauses = error_mark_node;
37646 break;
37649 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37650 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
37651 return clauses;
37654 /* Returns the name of the next clause. If the clause is not
37655 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
37656 token is not consumed. Otherwise, the appropriate enum from the
37657 pragma_simd_clause is returned and the token is consumed. */
37659 static pragma_omp_clause
37660 cp_parser_cilk_simd_clause_name (cp_parser *parser)
37662 pragma_omp_clause clause_type;
37663 cp_token *token = cp_lexer_peek_token (parser->lexer);
37665 if (token->keyword == RID_PRIVATE)
37666 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
37667 else if (!token->u.value || token->type != CPP_NAME)
37668 return PRAGMA_CILK_CLAUSE_NONE;
37669 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
37670 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
37671 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
37672 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
37673 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
37674 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
37675 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
37676 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
37677 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
37678 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
37679 else
37680 return PRAGMA_CILK_CLAUSE_NONE;
37682 cp_lexer_consume_token (parser->lexer);
37683 return clause_type;
37686 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
37688 static tree
37689 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
37691 tree clauses = NULL_TREE;
37693 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37694 && clauses != error_mark_node)
37696 pragma_omp_clause c_kind;
37697 c_kind = cp_parser_cilk_simd_clause_name (parser);
37698 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
37699 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
37700 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
37701 clauses = cp_parser_cilk_simd_linear (parser, clauses);
37702 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
37703 /* Use the OpenMP 4.0 equivalent function. */
37704 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
37705 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
37706 /* Use the OpenMP 4.0 equivalent function. */
37707 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
37708 clauses);
37709 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
37710 /* Use the OMP 4.0 equivalent function. */
37711 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
37712 clauses);
37713 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
37714 /* Use the OMP 4.0 equivalent function. */
37715 clauses = cp_parser_omp_clause_reduction (parser, clauses);
37716 else
37718 clauses = error_mark_node;
37719 cp_parser_error (parser, "expected %<#pragma simd%> clause");
37720 break;
37724 cp_parser_skip_to_pragma_eol (parser, pragma_token);
37726 if (clauses == error_mark_node)
37727 return error_mark_node;
37728 else
37729 return finish_omp_clauses (clauses, false, false, true);
37732 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
37734 static void
37735 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
37737 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
37739 if (clauses == error_mark_node)
37740 return;
37742 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
37744 error_at (cp_lexer_peek_token (parser->lexer)->location,
37745 "for statement expected");
37746 return;
37749 tree sb = begin_omp_structured_block ();
37750 int save = cp_parser_begin_omp_structured_block (parser);
37751 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
37752 if (ret)
37753 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
37754 cp_parser_end_omp_structured_block (parser, save);
37755 add_stmt (finish_omp_structured_block (sb));
37758 /* Main entry-point for parsing Cilk Plus _Cilk_for
37759 loops. The return value is error_mark_node
37760 when errors happen and CILK_FOR tree on success. */
37762 static tree
37763 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
37765 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
37766 gcc_unreachable ();
37768 tree sb = begin_omp_structured_block ();
37769 int save = cp_parser_begin_omp_structured_block (parser);
37771 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
37772 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
37773 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
37774 clauses = finish_omp_clauses (clauses, false);
37776 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
37777 if (ret)
37778 cpp_validate_cilk_plus_loop (ret);
37779 else
37780 ret = error_mark_node;
37782 cp_parser_end_omp_structured_block (parser, save);
37783 add_stmt (finish_omp_structured_block (sb));
37784 return ret;
37787 /* Create an identifier for a generic parameter type (a synthesized
37788 template parameter implied by `auto' or a concept identifier). */
37790 static GTY(()) int generic_parm_count;
37791 static tree
37792 make_generic_type_name ()
37794 char buf[32];
37795 sprintf (buf, "auto:%d", ++generic_parm_count);
37796 return get_identifier (buf);
37799 /* Predicate that behaves as is_auto_or_concept but matches the parent
37800 node of the generic type rather than the generic type itself. This
37801 allows for type transformation in add_implicit_template_parms. */
37803 static inline bool
37804 tree_type_is_auto_or_concept (const_tree t)
37806 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
37809 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
37810 (creating a new template parameter list if necessary). Returns the newly
37811 created template type parm. */
37813 static tree
37814 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
37816 gcc_assert (current_binding_level->kind == sk_function_parms);
37818 /* Before committing to modifying any scope, if we're in an
37819 implicit template scope, and we're trying to synthesize a
37820 constrained parameter, try to find a previous parameter with
37821 the same name. This is the same-type rule for abbreviated
37822 function templates. */
37823 if (parser->implicit_template_scope && constr)
37825 tree t = parser->implicit_template_parms;
37826 while (t)
37828 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
37830 tree d = TREE_VALUE (t);
37831 if (TREE_CODE (d) == PARM_DECL)
37832 /* Return the TEMPLATE_PARM_INDEX. */
37833 d = DECL_INITIAL (d);
37834 return d;
37836 t = TREE_CHAIN (t);
37840 /* We are either continuing a function template that already contains implicit
37841 template parameters, creating a new fully-implicit function template, or
37842 extending an existing explicit function template with implicit template
37843 parameters. */
37845 cp_binding_level *const entry_scope = current_binding_level;
37847 bool become_template = false;
37848 cp_binding_level *parent_scope = 0;
37850 if (parser->implicit_template_scope)
37852 gcc_assert (parser->implicit_template_parms);
37854 current_binding_level = parser->implicit_template_scope;
37856 else
37858 /* Roll back to the existing template parameter scope (in the case of
37859 extending an explicit function template) or introduce a new template
37860 parameter scope ahead of the function parameter scope (or class scope
37861 in the case of out-of-line member definitions). The function scope is
37862 added back after template parameter synthesis below. */
37864 cp_binding_level *scope = entry_scope;
37866 while (scope->kind == sk_function_parms)
37868 parent_scope = scope;
37869 scope = scope->level_chain;
37871 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
37873 /* If not defining a class, then any class scope is a scope level in
37874 an out-of-line member definition. In this case simply wind back
37875 beyond the first such scope to inject the template parameter list.
37876 Otherwise wind back to the class being defined. The latter can
37877 occur in class member friend declarations such as:
37879 class A {
37880 void foo (auto);
37882 class B {
37883 friend void A::foo (auto);
37886 The template parameter list synthesized for the friend declaration
37887 must be injected in the scope of 'B'. This can also occur in
37888 erroneous cases such as:
37890 struct A {
37891 struct B {
37892 void foo (auto);
37894 void B::foo (auto) {}
37897 Here the attempted definition of 'B::foo' within 'A' is ill-formed
37898 but, nevertheless, the template parameter list synthesized for the
37899 declarator should be injected into the scope of 'A' as if the
37900 ill-formed template was specified explicitly. */
37902 while (scope->kind == sk_class && !scope->defining_class_p)
37904 parent_scope = scope;
37905 scope = scope->level_chain;
37909 current_binding_level = scope;
37911 if (scope->kind != sk_template_parms
37912 || !function_being_declared_is_template_p (parser))
37914 /* Introduce a new template parameter list for implicit template
37915 parameters. */
37917 become_template = true;
37919 parser->implicit_template_scope
37920 = begin_scope (sk_template_parms, NULL);
37922 ++processing_template_decl;
37924 parser->fully_implicit_function_template_p = true;
37925 ++parser->num_template_parameter_lists;
37927 else
37929 /* Synthesize implicit template parameters at the end of the explicit
37930 template parameter list. */
37932 gcc_assert (current_template_parms);
37934 parser->implicit_template_scope = scope;
37936 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
37937 parser->implicit_template_parms
37938 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
37942 /* Synthesize a new template parameter and track the current template
37943 parameter chain with implicit_template_parms. */
37945 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
37946 tree synth_id = make_generic_type_name ();
37947 tree synth_tmpl_parm;
37948 bool non_type = false;
37950 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
37951 synth_tmpl_parm
37952 = finish_template_type_parm (class_type_node, synth_id);
37953 else if (TREE_CODE (proto) == TEMPLATE_DECL)
37954 synth_tmpl_parm
37955 = finish_constrained_template_template_parm (proto, synth_id);
37956 else
37958 synth_tmpl_parm = copy_decl (proto);
37959 DECL_NAME (synth_tmpl_parm) = synth_id;
37960 non_type = true;
37963 // Attach the constraint to the parm before processing.
37964 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
37965 TREE_TYPE (node) = constr;
37966 tree new_parm
37967 = process_template_parm (parser->implicit_template_parms,
37968 input_location,
37969 node,
37970 /*non_type=*/non_type,
37971 /*param_pack=*/false);
37973 // Chain the new parameter to the list of implicit parameters.
37974 if (parser->implicit_template_parms)
37975 parser->implicit_template_parms
37976 = TREE_CHAIN (parser->implicit_template_parms);
37977 else
37978 parser->implicit_template_parms = new_parm;
37980 tree new_decl = getdecls ();
37981 if (non_type)
37982 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
37983 new_decl = DECL_INITIAL (new_decl);
37985 /* If creating a fully implicit function template, start the new implicit
37986 template parameter list with this synthesized type, otherwise grow the
37987 current template parameter list. */
37989 if (become_template)
37991 parent_scope->level_chain = current_binding_level;
37993 tree new_parms = make_tree_vec (1);
37994 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
37995 current_template_parms = tree_cons (size_int (processing_template_decl),
37996 new_parms, current_template_parms);
37998 else
38000 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38001 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38002 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38003 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38006 // If the new parameter was constrained, we need to add that to the
38007 // constraints in the template parameter list.
38008 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38010 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38011 reqs = conjoin_constraints (reqs, req);
38012 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38015 current_binding_level = entry_scope;
38017 return new_decl;
38020 /* Finish the declaration of a fully implicit function template. Such a
38021 template has no explicit template parameter list so has not been through the
38022 normal template head and tail processing. synthesize_implicit_template_parm
38023 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38024 provided if the declaration is a class member such that its template
38025 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38026 form is returned. Otherwise NULL_TREE is returned. */
38028 static tree
38029 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38031 gcc_assert (parser->fully_implicit_function_template_p);
38033 if (member_decl_opt && member_decl_opt != error_mark_node
38034 && DECL_VIRTUAL_P (member_decl_opt))
38036 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38037 "implicit templates may not be %<virtual%>");
38038 DECL_VIRTUAL_P (member_decl_opt) = false;
38041 if (member_decl_opt)
38042 member_decl_opt = finish_member_template_decl (member_decl_opt);
38043 end_template_decl ();
38045 parser->fully_implicit_function_template_p = false;
38046 --parser->num_template_parameter_lists;
38048 return member_decl_opt;
38051 #include "gt-cp-parser.h"