C/C++: show pertinent open token when missing a close token
[official-gcc.git] / gcc / cp / parser.c
blobb849824fcd0fb441f4fe7a26557bb76756230097
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_ITERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (IDENTIFIER_KEYWORD_P (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool, bool = false);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool, location_t);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token, location_t = UNKNOWN_LOCATION);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2779 if (!cp_parser_simulate_error (parser))
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2786 if (token->type == CPP_PRAGMA)
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2807 rich_location richloc (line_table, input_location);
2808 c_parse_error (gmsgid,
2809 /* Because c_parser_error does not understand
2810 CPP_KEYWORD, keywords are treated like
2811 identifiers. */
2812 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2813 token->u.value, token->flags, &richloc);
2817 /* Issue an error about name-lookup failing. NAME is the
2818 IDENTIFIER_NODE DECL is the result of
2819 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2820 the thing that we hoped to find. */
2822 static void
2823 cp_parser_name_lookup_error (cp_parser* parser,
2824 tree name,
2825 tree decl,
2826 name_lookup_error desired,
2827 location_t location)
2829 /* If name lookup completely failed, tell the user that NAME was not
2830 declared. */
2831 if (decl == error_mark_node)
2833 if (parser->scope && parser->scope != global_namespace)
2834 error_at (location, "%<%E::%E%> has not been declared",
2835 parser->scope, name);
2836 else if (parser->scope == global_namespace)
2837 error_at (location, "%<::%E%> has not been declared", name);
2838 else if (parser->object_scope
2839 && !CLASS_TYPE_P (parser->object_scope))
2840 error_at (location, "request for member %qE in non-class type %qT",
2841 name, parser->object_scope);
2842 else if (parser->object_scope)
2843 error_at (location, "%<%T::%E%> has not been declared",
2844 parser->object_scope, name);
2845 else
2846 error_at (location, "%qE has not been declared", name);
2848 else if (parser->scope && parser->scope != global_namespace)
2850 switch (desired)
2852 case NLE_TYPE:
2853 error_at (location, "%<%E::%E%> is not a type",
2854 parser->scope, name);
2855 break;
2856 case NLE_CXX98:
2857 error_at (location, "%<%E::%E%> is not a class or namespace",
2858 parser->scope, name);
2859 break;
2860 case NLE_NOT_CXX98:
2861 error_at (location,
2862 "%<%E::%E%> is not a class, namespace, or enumeration",
2863 parser->scope, name);
2864 break;
2865 default:
2866 gcc_unreachable ();
2870 else if (parser->scope == global_namespace)
2872 switch (desired)
2874 case NLE_TYPE:
2875 error_at (location, "%<::%E%> is not a type", name);
2876 break;
2877 case NLE_CXX98:
2878 error_at (location, "%<::%E%> is not a class or namespace", name);
2879 break;
2880 case NLE_NOT_CXX98:
2881 error_at (location,
2882 "%<::%E%> is not a class, namespace, or enumeration",
2883 name);
2884 break;
2885 default:
2886 gcc_unreachable ();
2889 else
2891 switch (desired)
2893 case NLE_TYPE:
2894 error_at (location, "%qE is not a type", name);
2895 break;
2896 case NLE_CXX98:
2897 error_at (location, "%qE is not a class or namespace", name);
2898 break;
2899 case NLE_NOT_CXX98:
2900 error_at (location,
2901 "%qE is not a class, namespace, or enumeration", name);
2902 break;
2903 default:
2904 gcc_unreachable ();
2909 /* If we are parsing tentatively, remember that an error has occurred
2910 during this tentative parse. Returns true if the error was
2911 simulated; false if a message should be issued by the caller. */
2913 static bool
2914 cp_parser_simulate_error (cp_parser* parser)
2916 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2918 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2919 return true;
2921 return false;
2924 /* This function is called when a type is defined. If type
2925 definitions are forbidden at this point, an error message is
2926 issued. */
2928 static bool
2929 cp_parser_check_type_definition (cp_parser* parser)
2931 /* If types are forbidden here, issue a message. */
2932 if (parser->type_definition_forbidden_message)
2934 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2935 in the message need to be interpreted. */
2936 error (parser->type_definition_forbidden_message);
2937 return false;
2939 return true;
2942 /* This function is called when the DECLARATOR is processed. The TYPE
2943 was a type defined in the decl-specifiers. If it is invalid to
2944 define a type in the decl-specifiers for DECLARATOR, an error is
2945 issued. TYPE_LOCATION is the location of TYPE and is used
2946 for error reporting. */
2948 static void
2949 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2950 tree type, location_t type_location)
2952 /* [dcl.fct] forbids type definitions in return types.
2953 Unfortunately, it's not easy to know whether or not we are
2954 processing a return type until after the fact. */
2955 while (declarator
2956 && (declarator->kind == cdk_pointer
2957 || declarator->kind == cdk_reference
2958 || declarator->kind == cdk_ptrmem))
2959 declarator = declarator->declarator;
2960 if (declarator
2961 && declarator->kind == cdk_function)
2963 error_at (type_location,
2964 "new types may not be defined in a return type");
2965 inform (type_location,
2966 "(perhaps a semicolon is missing after the definition of %qT)",
2967 type);
2971 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2972 "<" in any valid C++ program. If the next token is indeed "<",
2973 issue a message warning the user about what appears to be an
2974 invalid attempt to form a template-id. LOCATION is the location
2975 of the type-specifier (TYPE) */
2977 static void
2978 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2979 tree type,
2980 enum tag_types tag_type,
2981 location_t location)
2983 cp_token_position start = 0;
2985 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2987 if (TREE_CODE (type) == TYPE_DECL)
2988 type = TREE_TYPE (type);
2989 if (TYPE_P (type) && !template_placeholder_p (type))
2990 error_at (location, "%qT is not a template", type);
2991 else if (identifier_p (type))
2993 if (tag_type != none_type)
2994 error_at (location, "%qE is not a class template", type);
2995 else
2996 error_at (location, "%qE is not a template", type);
2998 else
2999 error_at (location, "invalid template-id");
3000 /* Remember the location of the invalid "<". */
3001 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3002 start = cp_lexer_token_position (parser->lexer, true);
3003 /* Consume the "<". */
3004 cp_lexer_consume_token (parser->lexer);
3005 /* Parse the template arguments. */
3006 cp_parser_enclosed_template_argument_list (parser);
3007 /* Permanently remove the invalid template arguments so that
3008 this error message is not issued again. */
3009 if (start)
3010 cp_lexer_purge_tokens_after (parser->lexer, start);
3014 /* If parsing an integral constant-expression, issue an error message
3015 about the fact that THING appeared and return true. Otherwise,
3016 return false. In either case, set
3017 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3019 static bool
3020 cp_parser_non_integral_constant_expression (cp_parser *parser,
3021 non_integral_constant thing)
3023 parser->non_integral_constant_expression_p = true;
3024 if (parser->integral_constant_expression_p)
3026 if (!parser->allow_non_integral_constant_expression_p)
3028 const char *msg = NULL;
3029 switch (thing)
3031 case NIC_FLOAT:
3032 pedwarn (input_location, OPT_Wpedantic,
3033 "ISO C++ forbids using a floating-point literal "
3034 "in a constant-expression");
3035 return true;
3036 case NIC_CAST:
3037 error ("a cast to a type other than an integral or "
3038 "enumeration type cannot appear in a "
3039 "constant-expression");
3040 return true;
3041 case NIC_TYPEID:
3042 error ("%<typeid%> operator "
3043 "cannot appear in a constant-expression");
3044 return true;
3045 case NIC_NCC:
3046 error ("non-constant compound literals "
3047 "cannot appear in a constant-expression");
3048 return true;
3049 case NIC_FUNC_CALL:
3050 error ("a function call "
3051 "cannot appear in a constant-expression");
3052 return true;
3053 case NIC_INC:
3054 error ("an increment "
3055 "cannot appear in a constant-expression");
3056 return true;
3057 case NIC_DEC:
3058 error ("an decrement "
3059 "cannot appear in a constant-expression");
3060 return true;
3061 case NIC_ARRAY_REF:
3062 error ("an array reference "
3063 "cannot appear in a constant-expression");
3064 return true;
3065 case NIC_ADDR_LABEL:
3066 error ("the address of a label "
3067 "cannot appear in a constant-expression");
3068 return true;
3069 case NIC_OVERLOADED:
3070 error ("calls to overloaded operators "
3071 "cannot appear in a constant-expression");
3072 return true;
3073 case NIC_ASSIGNMENT:
3074 error ("an assignment cannot appear in a constant-expression");
3075 return true;
3076 case NIC_COMMA:
3077 error ("a comma operator "
3078 "cannot appear in a constant-expression");
3079 return true;
3080 case NIC_CONSTRUCTOR:
3081 error ("a call to a constructor "
3082 "cannot appear in a constant-expression");
3083 return true;
3084 case NIC_TRANSACTION:
3085 error ("a transaction expression "
3086 "cannot appear in a constant-expression");
3087 return true;
3088 case NIC_THIS:
3089 msg = "this";
3090 break;
3091 case NIC_FUNC_NAME:
3092 msg = "__FUNCTION__";
3093 break;
3094 case NIC_PRETTY_FUNC:
3095 msg = "__PRETTY_FUNCTION__";
3096 break;
3097 case NIC_C99_FUNC:
3098 msg = "__func__";
3099 break;
3100 case NIC_VA_ARG:
3101 msg = "va_arg";
3102 break;
3103 case NIC_ARROW:
3104 msg = "->";
3105 break;
3106 case NIC_POINT:
3107 msg = ".";
3108 break;
3109 case NIC_STAR:
3110 msg = "*";
3111 break;
3112 case NIC_ADDR:
3113 msg = "&";
3114 break;
3115 case NIC_PREINCREMENT:
3116 msg = "++";
3117 break;
3118 case NIC_PREDECREMENT:
3119 msg = "--";
3120 break;
3121 case NIC_NEW:
3122 msg = "new";
3123 break;
3124 case NIC_DEL:
3125 msg = "delete";
3126 break;
3127 default:
3128 gcc_unreachable ();
3130 if (msg)
3131 error ("%qs cannot appear in a constant-expression", msg);
3132 return true;
3135 return false;
3138 /* Emit a diagnostic for an invalid type name. This function commits
3139 to the current active tentative parse, if any. (Otherwise, the
3140 problematic construct might be encountered again later, resulting
3141 in duplicate error messages.) LOCATION is the location of ID. */
3143 static void
3144 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3145 location_t location)
3147 tree decl, ambiguous_decls;
3148 cp_parser_commit_to_tentative_parse (parser);
3149 /* Try to lookup the identifier. */
3150 decl = cp_parser_lookup_name (parser, id, none_type,
3151 /*is_template=*/false,
3152 /*is_namespace=*/false,
3153 /*check_dependency=*/true,
3154 &ambiguous_decls, location);
3155 if (ambiguous_decls)
3156 /* If the lookup was ambiguous, an error will already have
3157 been issued. */
3158 return;
3159 /* If the lookup found a template-name, it means that the user forgot
3160 to specify an argument list. Emit a useful error message. */
3161 if (DECL_TYPE_TEMPLATE_P (decl))
3163 error_at (location,
3164 "invalid use of template-name %qE without an argument list",
3165 decl);
3166 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3167 inform (location, "class template argument deduction is only available "
3168 "with -std=c++1z or -std=gnu++1z");
3169 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3171 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3172 error_at (location, "invalid use of destructor %qD as a type", id);
3173 else if (TREE_CODE (decl) == TYPE_DECL)
3174 /* Something like 'unsigned A a;' */
3175 error_at (location, "invalid combination of multiple type-specifiers");
3176 else if (!parser->scope)
3178 /* Issue an error message. */
3179 const char *suggestion = NULL;
3180 if (TREE_CODE (id) == IDENTIFIER_NODE)
3181 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3182 if (suggestion)
3184 gcc_rich_location richloc (location);
3185 richloc.add_fixit_replace (suggestion);
3186 error_at_rich_loc (&richloc,
3187 "%qE does not name a type; did you mean %qs?",
3188 id, suggestion);
3190 else
3191 error_at (location, "%qE does not name a type", id);
3192 /* If we're in a template class, it's possible that the user was
3193 referring to a type from a base class. For example:
3195 template <typename T> struct A { typedef T X; };
3196 template <typename T> struct B : public A<T> { X x; };
3198 The user should have said "typename A<T>::X". */
3199 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3200 inform (location, "C++11 %<constexpr%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3203 inform (location, "C++11 %<noexcept%> only available with "
3204 "-std=c++11 or -std=gnu++11");
3205 else if (cxx_dialect < cxx11
3206 && TREE_CODE (id) == IDENTIFIER_NODE
3207 && id_equal (id, "thread_local"))
3208 inform (location, "C++11 %<thread_local%> only available with "
3209 "-std=c++11 or -std=gnu++11");
3210 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3211 inform (location, "%<concept%> only available with -fconcepts");
3212 else if (processing_template_decl && current_class_type
3213 && TYPE_BINFO (current_class_type))
3215 tree b;
3217 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3219 b = TREE_CHAIN (b))
3221 tree base_type = BINFO_TYPE (b);
3222 if (CLASS_TYPE_P (base_type)
3223 && dependent_type_p (base_type))
3225 tree field;
3226 /* Go from a particular instantiation of the
3227 template (which will have an empty TYPE_FIELDs),
3228 to the main version. */
3229 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3230 for (field = TYPE_FIELDS (base_type);
3231 field;
3232 field = DECL_CHAIN (field))
3233 if (TREE_CODE (field) == TYPE_DECL
3234 && DECL_NAME (field) == id)
3236 inform (location,
3237 "(perhaps %<typename %T::%E%> was intended)",
3238 BINFO_TYPE (b), id);
3239 break;
3241 if (field)
3242 break;
3247 /* Here we diagnose qualified-ids where the scope is actually correct,
3248 but the identifier does not resolve to a valid type name. */
3249 else if (parser->scope != error_mark_node)
3251 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3253 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3254 error_at (location_of (id),
3255 "%qE in namespace %qE does not name a template type",
3256 id, parser->scope);
3257 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3258 error_at (location_of (id),
3259 "%qE in namespace %qE does not name a template type",
3260 TREE_OPERAND (id, 0), parser->scope);
3261 else
3262 error_at (location_of (id),
3263 "%qE in namespace %qE does not name a type",
3264 id, parser->scope);
3265 if (DECL_P (decl))
3266 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3268 else if (CLASS_TYPE_P (parser->scope)
3269 && constructor_name_p (id, parser->scope))
3271 /* A<T>::A<T>() */
3272 error_at (location, "%<%T::%E%> names the constructor, not"
3273 " the type", parser->scope, id);
3274 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3275 error_at (location, "and %qT has no template constructors",
3276 parser->scope);
3278 else if (TYPE_P (parser->scope)
3279 && dependent_scope_p (parser->scope))
3281 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3282 error_at (location,
3283 "need %<typename%> before %<%T::%D::%E%> because "
3284 "%<%T::%D%> is a dependent scope",
3285 TYPE_CONTEXT (parser->scope),
3286 TYPENAME_TYPE_FULLNAME (parser->scope),
3288 TYPE_CONTEXT (parser->scope),
3289 TYPENAME_TYPE_FULLNAME (parser->scope));
3290 else
3291 error_at (location, "need %<typename%> before %<%T::%E%> because "
3292 "%qT is a dependent scope",
3293 parser->scope, id, parser->scope);
3295 else if (TYPE_P (parser->scope))
3297 if (!COMPLETE_TYPE_P (parser->scope))
3298 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3299 parser->scope);
3300 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3301 error_at (location_of (id),
3302 "%qE in %q#T does not name a template type",
3303 id, parser->scope);
3304 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3305 error_at (location_of (id),
3306 "%qE in %q#T does not name a template type",
3307 TREE_OPERAND (id, 0), parser->scope);
3308 else
3309 error_at (location_of (id),
3310 "%qE in %q#T does not name a type",
3311 id, parser->scope);
3312 if (DECL_P (decl))
3313 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3315 else
3316 gcc_unreachable ();
3320 /* Check for a common situation where a type-name should be present,
3321 but is not, and issue a sensible error message. Returns true if an
3322 invalid type-name was detected.
3324 The situation handled by this function are variable declarations of the
3325 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3326 Usually, `ID' should name a type, but if we got here it means that it
3327 does not. We try to emit the best possible error message depending on
3328 how exactly the id-expression looks like. */
3330 static bool
3331 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3333 tree id;
3334 cp_token *token = cp_lexer_peek_token (parser->lexer);
3336 /* Avoid duplicate error about ambiguous lookup. */
3337 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3339 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3340 if (next->type == CPP_NAME && next->error_reported)
3341 goto out;
3344 cp_parser_parse_tentatively (parser);
3345 id = cp_parser_id_expression (parser,
3346 /*template_keyword_p=*/false,
3347 /*check_dependency_p=*/true,
3348 /*template_p=*/NULL,
3349 /*declarator_p=*/true,
3350 /*optional_p=*/false);
3351 /* If the next token is a (, this is a function with no explicit return
3352 type, i.e. constructor, destructor or conversion op. */
3353 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3354 || TREE_CODE (id) == TYPE_DECL)
3356 cp_parser_abort_tentative_parse (parser);
3357 return false;
3359 if (!cp_parser_parse_definitely (parser))
3360 return false;
3362 /* Emit a diagnostic for the invalid type. */
3363 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3364 out:
3365 /* If we aren't in the middle of a declarator (i.e. in a
3366 parameter-declaration-clause), skip to the end of the declaration;
3367 there's no point in trying to process it. */
3368 if (!parser->in_declarator_p)
3369 cp_parser_skip_to_end_of_block_or_statement (parser);
3370 return true;
3373 /* Consume tokens up to, and including, the next non-nested closing `)'.
3374 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3375 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3376 found an unnested token of that type. */
3378 static int
3379 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3380 bool recovering,
3381 cpp_ttype or_ttype,
3382 bool consume_paren)
3384 unsigned paren_depth = 0;
3385 unsigned brace_depth = 0;
3386 unsigned square_depth = 0;
3388 if (recovering && or_ttype == CPP_EOF
3389 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3390 return 0;
3392 while (true)
3394 cp_token * token = cp_lexer_peek_token (parser->lexer);
3396 /* Have we found what we're looking for before the closing paren? */
3397 if (token->type == or_ttype && or_ttype != CPP_EOF
3398 && !brace_depth && !paren_depth && !square_depth)
3399 return -1;
3401 switch (token->type)
3403 case CPP_EOF:
3404 case CPP_PRAGMA_EOL:
3405 /* If we've run out of tokens, then there is no closing `)'. */
3406 return 0;
3408 /* This is good for lambda expression capture-lists. */
3409 case CPP_OPEN_SQUARE:
3410 ++square_depth;
3411 break;
3412 case CPP_CLOSE_SQUARE:
3413 if (!square_depth--)
3414 return 0;
3415 break;
3417 case CPP_SEMICOLON:
3418 /* This matches the processing in skip_to_end_of_statement. */
3419 if (!brace_depth)
3420 return 0;
3421 break;
3423 case CPP_OPEN_BRACE:
3424 ++brace_depth;
3425 break;
3426 case CPP_CLOSE_BRACE:
3427 if (!brace_depth--)
3428 return 0;
3429 break;
3431 case CPP_OPEN_PAREN:
3432 if (!brace_depth)
3433 ++paren_depth;
3434 break;
3436 case CPP_CLOSE_PAREN:
3437 if (!brace_depth && !paren_depth--)
3439 if (consume_paren)
3440 cp_lexer_consume_token (parser->lexer);
3441 return 1;
3443 break;
3445 default:
3446 break;
3449 /* Consume the token. */
3450 cp_lexer_consume_token (parser->lexer);
3454 /* Consume tokens up to, and including, the next non-nested closing `)'.
3455 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3456 are doing error recovery. Returns -1 if OR_COMMA is true and we
3457 found an unnested token of that type. */
3459 static int
3460 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3461 bool recovering,
3462 bool or_comma,
3463 bool consume_paren)
3465 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3466 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3467 ttype, consume_paren);
3470 /* Consume tokens until we reach the end of the current statement.
3471 Normally, that will be just before consuming a `;'. However, if a
3472 non-nested `}' comes first, then we stop before consuming that. */
3474 static void
3475 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3477 unsigned nesting_depth = 0;
3479 /* Unwind generic function template scope if necessary. */
3480 if (parser->fully_implicit_function_template_p)
3481 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3483 while (true)
3485 cp_token *token = cp_lexer_peek_token (parser->lexer);
3487 switch (token->type)
3489 case CPP_EOF:
3490 case CPP_PRAGMA_EOL:
3491 /* If we've run out of tokens, stop. */
3492 return;
3494 case CPP_SEMICOLON:
3495 /* If the next token is a `;', we have reached the end of the
3496 statement. */
3497 if (!nesting_depth)
3498 return;
3499 break;
3501 case CPP_CLOSE_BRACE:
3502 /* If this is a non-nested '}', stop before consuming it.
3503 That way, when confronted with something like:
3505 { 3 + }
3507 we stop before consuming the closing '}', even though we
3508 have not yet reached a `;'. */
3509 if (nesting_depth == 0)
3510 return;
3512 /* If it is the closing '}' for a block that we have
3513 scanned, stop -- but only after consuming the token.
3514 That way given:
3516 void f g () { ... }
3517 typedef int I;
3519 we will stop after the body of the erroneously declared
3520 function, but before consuming the following `typedef'
3521 declaration. */
3522 if (--nesting_depth == 0)
3524 cp_lexer_consume_token (parser->lexer);
3525 return;
3527 break;
3529 case CPP_OPEN_BRACE:
3530 ++nesting_depth;
3531 break;
3533 default:
3534 break;
3537 /* Consume the token. */
3538 cp_lexer_consume_token (parser->lexer);
3542 /* This function is called at the end of a statement or declaration.
3543 If the next token is a semicolon, it is consumed; otherwise, error
3544 recovery is attempted. */
3546 static void
3547 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3549 /* Look for the trailing `;'. */
3550 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3552 /* If there is additional (erroneous) input, skip to the end of
3553 the statement. */
3554 cp_parser_skip_to_end_of_statement (parser);
3555 /* If the next token is now a `;', consume it. */
3556 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3557 cp_lexer_consume_token (parser->lexer);
3561 /* Skip tokens until we have consumed an entire block, or until we
3562 have consumed a non-nested `;'. */
3564 static void
3565 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3567 int nesting_depth = 0;
3569 /* Unwind generic function template scope if necessary. */
3570 if (parser->fully_implicit_function_template_p)
3571 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3573 while (nesting_depth >= 0)
3575 cp_token *token = cp_lexer_peek_token (parser->lexer);
3577 switch (token->type)
3579 case CPP_EOF:
3580 case CPP_PRAGMA_EOL:
3581 /* If we've run out of tokens, stop. */
3582 return;
3584 case CPP_SEMICOLON:
3585 /* Stop if this is an unnested ';'. */
3586 if (!nesting_depth)
3587 nesting_depth = -1;
3588 break;
3590 case CPP_CLOSE_BRACE:
3591 /* Stop if this is an unnested '}', or closes the outermost
3592 nesting level. */
3593 nesting_depth--;
3594 if (nesting_depth < 0)
3595 return;
3596 if (!nesting_depth)
3597 nesting_depth = -1;
3598 break;
3600 case CPP_OPEN_BRACE:
3601 /* Nest. */
3602 nesting_depth++;
3603 break;
3605 default:
3606 break;
3609 /* Consume the token. */
3610 cp_lexer_consume_token (parser->lexer);
3614 /* Skip tokens until a non-nested closing curly brace is the next
3615 token, or there are no more tokens. Return true in the first case,
3616 false otherwise. */
3618 static bool
3619 cp_parser_skip_to_closing_brace (cp_parser *parser)
3621 unsigned nesting_depth = 0;
3623 while (true)
3625 cp_token *token = cp_lexer_peek_token (parser->lexer);
3627 switch (token->type)
3629 case CPP_EOF:
3630 case CPP_PRAGMA_EOL:
3631 /* If we've run out of tokens, stop. */
3632 return false;
3634 case CPP_CLOSE_BRACE:
3635 /* If the next token is a non-nested `}', then we have reached
3636 the end of the current block. */
3637 if (nesting_depth-- == 0)
3638 return true;
3639 break;
3641 case CPP_OPEN_BRACE:
3642 /* If it the next token is a `{', then we are entering a new
3643 block. Consume the entire block. */
3644 ++nesting_depth;
3645 break;
3647 default:
3648 break;
3651 /* Consume the token. */
3652 cp_lexer_consume_token (parser->lexer);
3656 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3657 parameter is the PRAGMA token, allowing us to purge the entire pragma
3658 sequence. */
3660 static void
3661 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3663 cp_token *token;
3665 parser->lexer->in_pragma = false;
3668 token = cp_lexer_consume_token (parser->lexer);
3669 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3671 /* Ensure that the pragma is not parsed again. */
3672 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3675 /* Require pragma end of line, resyncing with it as necessary. The
3676 arguments are as for cp_parser_skip_to_pragma_eol. */
3678 static void
3679 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3681 parser->lexer->in_pragma = false;
3682 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3683 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3686 /* This is a simple wrapper around make_typename_type. When the id is
3687 an unresolved identifier node, we can provide a superior diagnostic
3688 using cp_parser_diagnose_invalid_type_name. */
3690 static tree
3691 cp_parser_make_typename_type (cp_parser *parser, tree id,
3692 location_t id_location)
3694 tree result;
3695 if (identifier_p (id))
3697 result = make_typename_type (parser->scope, id, typename_type,
3698 /*complain=*/tf_none);
3699 if (result == error_mark_node)
3700 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3701 return result;
3703 return make_typename_type (parser->scope, id, typename_type, tf_error);
3706 /* This is a wrapper around the
3707 make_{pointer,ptrmem,reference}_declarator functions that decides
3708 which one to call based on the CODE and CLASS_TYPE arguments. The
3709 CODE argument should be one of the values returned by
3710 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3711 appertain to the pointer or reference. */
3713 static cp_declarator *
3714 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3715 cp_cv_quals cv_qualifiers,
3716 cp_declarator *target,
3717 tree attributes)
3719 if (code == ERROR_MARK)
3720 return cp_error_declarator;
3722 if (code == INDIRECT_REF)
3723 if (class_type == NULL_TREE)
3724 return make_pointer_declarator (cv_qualifiers, target, attributes);
3725 else
3726 return make_ptrmem_declarator (cv_qualifiers, class_type,
3727 target, attributes);
3728 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3729 return make_reference_declarator (cv_qualifiers, target,
3730 false, attributes);
3731 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3732 return make_reference_declarator (cv_qualifiers, target,
3733 true, attributes);
3734 gcc_unreachable ();
3737 /* Create a new C++ parser. */
3739 static cp_parser *
3740 cp_parser_new (void)
3742 cp_parser *parser;
3743 cp_lexer *lexer;
3744 unsigned i;
3746 /* cp_lexer_new_main is called before doing GC allocation because
3747 cp_lexer_new_main might load a PCH file. */
3748 lexer = cp_lexer_new_main ();
3750 /* Initialize the binops_by_token so that we can get the tree
3751 directly from the token. */
3752 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3753 binops_by_token[binops[i].token_type] = binops[i];
3755 parser = ggc_cleared_alloc<cp_parser> ();
3756 parser->lexer = lexer;
3757 parser->context = cp_parser_context_new (NULL);
3759 /* For now, we always accept GNU extensions. */
3760 parser->allow_gnu_extensions_p = 1;
3762 /* The `>' token is a greater-than operator, not the end of a
3763 template-id. */
3764 parser->greater_than_is_operator_p = true;
3766 parser->default_arg_ok_p = true;
3768 /* We are not parsing a constant-expression. */
3769 parser->integral_constant_expression_p = false;
3770 parser->allow_non_integral_constant_expression_p = false;
3771 parser->non_integral_constant_expression_p = false;
3773 /* Local variable names are not forbidden. */
3774 parser->local_variables_forbidden_p = false;
3776 /* We are not processing an `extern "C"' declaration. */
3777 parser->in_unbraced_linkage_specification_p = false;
3779 /* We are not processing a declarator. */
3780 parser->in_declarator_p = false;
3782 /* We are not processing a template-argument-list. */
3783 parser->in_template_argument_list_p = false;
3785 /* We are not in an iteration statement. */
3786 parser->in_statement = 0;
3788 /* We are not in a switch statement. */
3789 parser->in_switch_statement_p = false;
3791 /* We are not parsing a type-id inside an expression. */
3792 parser->in_type_id_in_expr_p = false;
3794 /* Declarations aren't implicitly extern "C". */
3795 parser->implicit_extern_c = false;
3797 /* String literals should be translated to the execution character set. */
3798 parser->translate_strings_p = true;
3800 /* We are not parsing a function body. */
3801 parser->in_function_body = false;
3803 /* We can correct until told otherwise. */
3804 parser->colon_corrects_to_scope_p = true;
3806 /* The unparsed function queue is empty. */
3807 push_unparsed_function_queues (parser);
3809 /* There are no classes being defined. */
3810 parser->num_classes_being_defined = 0;
3812 /* No template parameters apply. */
3813 parser->num_template_parameter_lists = 0;
3815 /* Special parsing data structures. */
3816 parser->omp_declare_simd = NULL;
3817 parser->cilk_simd_fn_info = NULL;
3818 parser->oacc_routine = NULL;
3820 /* Not declaring an implicit function template. */
3821 parser->auto_is_implicit_function_template_parm_p = false;
3822 parser->fully_implicit_function_template_p = false;
3823 parser->implicit_template_parms = 0;
3824 parser->implicit_template_scope = 0;
3826 /* Allow constrained-type-specifiers. */
3827 parser->prevent_constrained_type_specifiers = 0;
3829 return parser;
3832 /* Create a cp_lexer structure which will emit the tokens in CACHE
3833 and push it onto the parser's lexer stack. This is used for delayed
3834 parsing of in-class method bodies and default arguments, and should
3835 not be confused with tentative parsing. */
3836 static void
3837 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3839 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3840 lexer->next = parser->lexer;
3841 parser->lexer = lexer;
3843 /* Move the current source position to that of the first token in the
3844 new lexer. */
3845 cp_lexer_set_source_position_from_token (lexer->next_token);
3848 /* Pop the top lexer off the parser stack. This is never used for the
3849 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3850 static void
3851 cp_parser_pop_lexer (cp_parser *parser)
3853 cp_lexer *lexer = parser->lexer;
3854 parser->lexer = lexer->next;
3855 cp_lexer_destroy (lexer);
3857 /* Put the current source position back where it was before this
3858 lexer was pushed. */
3859 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3862 /* Lexical conventions [gram.lex] */
3864 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3865 identifier. */
3867 static cp_expr
3868 cp_parser_identifier (cp_parser* parser)
3870 cp_token *token;
3872 /* Look for the identifier. */
3873 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3874 /* Return the value. */
3875 if (token)
3876 return cp_expr (token->u.value, token->location);
3877 else
3878 return error_mark_node;
3881 /* Parse a sequence of adjacent string constants. Returns a
3882 TREE_STRING representing the combined, nul-terminated string
3883 constant. If TRANSLATE is true, translate the string to the
3884 execution character set. If WIDE_OK is true, a wide string is
3885 invalid here.
3887 C++98 [lex.string] says that if a narrow string literal token is
3888 adjacent to a wide string literal token, the behavior is undefined.
3889 However, C99 6.4.5p4 says that this results in a wide string literal.
3890 We follow C99 here, for consistency with the C front end.
3892 This code is largely lifted from lex_string() in c-lex.c.
3894 FUTURE: ObjC++ will need to handle @-strings here. */
3895 static cp_expr
3896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3897 bool lookup_udlit = true)
3899 tree value;
3900 size_t count;
3901 struct obstack str_ob;
3902 cpp_string str, istr, *strs;
3903 cp_token *tok;
3904 enum cpp_ttype type, curr_type;
3905 int have_suffix_p = 0;
3906 tree string_tree;
3907 tree suffix_id = NULL_TREE;
3908 bool curr_tok_is_userdef_p = false;
3910 tok = cp_lexer_peek_token (parser->lexer);
3911 if (!cp_parser_is_string_literal (tok))
3913 cp_parser_error (parser, "expected string-literal");
3914 return error_mark_node;
3917 location_t loc = tok->location;
3919 if (cpp_userdef_string_p (tok->type))
3921 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3922 curr_type = cpp_userdef_string_remove_type (tok->type);
3923 curr_tok_is_userdef_p = true;
3925 else
3927 string_tree = tok->u.value;
3928 curr_type = tok->type;
3930 type = curr_type;
3932 /* Try to avoid the overhead of creating and destroying an obstack
3933 for the common case of just one string. */
3934 if (!cp_parser_is_string_literal
3935 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3937 cp_lexer_consume_token (parser->lexer);
3939 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3940 str.len = TREE_STRING_LENGTH (string_tree);
3941 count = 1;
3943 if (curr_tok_is_userdef_p)
3945 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3946 have_suffix_p = 1;
3947 curr_type = cpp_userdef_string_remove_type (tok->type);
3949 else
3950 curr_type = tok->type;
3952 strs = &str;
3954 else
3956 location_t last_tok_loc = tok->location;
3957 gcc_obstack_init (&str_ob);
3958 count = 0;
3962 cp_lexer_consume_token (parser->lexer);
3963 count++;
3964 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3965 str.len = TREE_STRING_LENGTH (string_tree);
3967 if (curr_tok_is_userdef_p)
3969 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3970 if (have_suffix_p == 0)
3972 suffix_id = curr_suffix_id;
3973 have_suffix_p = 1;
3975 else if (have_suffix_p == 1
3976 && curr_suffix_id != suffix_id)
3978 error ("inconsistent user-defined literal suffixes"
3979 " %qD and %qD in string literal",
3980 suffix_id, curr_suffix_id);
3981 have_suffix_p = -1;
3983 curr_type = cpp_userdef_string_remove_type (tok->type);
3985 else
3986 curr_type = tok->type;
3988 if (type != curr_type)
3990 if (type == CPP_STRING)
3991 type = curr_type;
3992 else if (curr_type != CPP_STRING)
3994 rich_location rich_loc (line_table, tok->location);
3995 rich_loc.add_range (last_tok_loc, false);
3996 error_at_rich_loc (&rich_loc,
3997 "unsupported non-standard concatenation "
3998 "of string literals");
4002 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4004 last_tok_loc = tok->location;
4006 tok = cp_lexer_peek_token (parser->lexer);
4007 if (cpp_userdef_string_p (tok->type))
4009 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4010 curr_type = cpp_userdef_string_remove_type (tok->type);
4011 curr_tok_is_userdef_p = true;
4013 else
4015 string_tree = tok->u.value;
4016 curr_type = tok->type;
4017 curr_tok_is_userdef_p = false;
4020 while (cp_parser_is_string_literal (tok));
4022 /* A string literal built by concatenation has its caret=start at
4023 the start of the initial string, and its finish at the finish of
4024 the final string literal. */
4025 loc = make_location (loc, loc, get_finish (last_tok_loc));
4027 strs = (cpp_string *) obstack_finish (&str_ob);
4030 if (type != CPP_STRING && !wide_ok)
4032 cp_parser_error (parser, "a wide string is invalid in this context");
4033 type = CPP_STRING;
4036 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4037 (parse_in, strs, count, &istr, type))
4039 value = build_string (istr.len, (const char *)istr.text);
4040 free (CONST_CAST (unsigned char *, istr.text));
4042 switch (type)
4044 default:
4045 case CPP_STRING:
4046 case CPP_UTF8STRING:
4047 TREE_TYPE (value) = char_array_type_node;
4048 break;
4049 case CPP_STRING16:
4050 TREE_TYPE (value) = char16_array_type_node;
4051 break;
4052 case CPP_STRING32:
4053 TREE_TYPE (value) = char32_array_type_node;
4054 break;
4055 case CPP_WSTRING:
4056 TREE_TYPE (value) = wchar_array_type_node;
4057 break;
4060 value = fix_string_type (value);
4062 if (have_suffix_p)
4064 tree literal = build_userdef_literal (suffix_id, value,
4065 OT_NONE, NULL_TREE);
4066 if (lookup_udlit)
4067 value = cp_parser_userdef_string_literal (literal);
4068 else
4069 value = literal;
4072 else
4073 /* cpp_interpret_string has issued an error. */
4074 value = error_mark_node;
4076 if (count > 1)
4077 obstack_free (&str_ob, 0);
4079 return cp_expr (value, loc);
4082 /* Look up a literal operator with the name and the exact arguments. */
4084 static tree
4085 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4087 tree decl;
4088 decl = lookup_name (name);
4089 if (!decl || !is_overloaded_fn (decl))
4090 return error_mark_node;
4092 for (lkp_iterator iter (decl); iter; ++iter)
4094 unsigned int ix;
4095 bool found = true;
4096 tree fn = *iter;
4097 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4098 if (parmtypes != NULL_TREE)
4100 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4101 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4103 tree tparm = TREE_VALUE (parmtypes);
4104 tree targ = TREE_TYPE ((*args)[ix]);
4105 bool ptr = TYPE_PTR_P (tparm);
4106 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4107 if ((ptr || arr || !same_type_p (tparm, targ))
4108 && (!ptr || !arr
4109 || !same_type_p (TREE_TYPE (tparm),
4110 TREE_TYPE (targ))))
4111 found = false;
4113 if (found
4114 && ix == vec_safe_length (args)
4115 /* May be this should be sufficient_parms_p instead,
4116 depending on how exactly should user-defined literals
4117 work in presence of default arguments on the literal
4118 operator parameters. */
4119 && parmtypes == void_list_node)
4120 return decl;
4124 return error_mark_node;
4127 /* Parse a user-defined char constant. Returns a call to a user-defined
4128 literal operator taking the character as an argument. */
4130 static cp_expr
4131 cp_parser_userdef_char_literal (cp_parser *parser)
4133 cp_token *token = cp_lexer_consume_token (parser->lexer);
4134 tree literal = token->u.value;
4135 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4136 tree value = USERDEF_LITERAL_VALUE (literal);
4137 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4138 tree decl, result;
4140 /* Build up a call to the user-defined operator */
4141 /* Lookup the name we got back from the id-expression. */
4142 vec<tree, va_gc> *args = make_tree_vector ();
4143 vec_safe_push (args, value);
4144 decl = lookup_literal_operator (name, args);
4145 if (!decl || decl == error_mark_node)
4147 error ("unable to find character literal operator %qD with %qT argument",
4148 name, TREE_TYPE (value));
4149 release_tree_vector (args);
4150 return error_mark_node;
4152 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4153 release_tree_vector (args);
4154 return result;
4157 /* A subroutine of cp_parser_userdef_numeric_literal to
4158 create a char... template parameter pack from a string node. */
4160 static tree
4161 make_char_string_pack (tree value)
4163 tree charvec;
4164 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4165 const char *str = TREE_STRING_POINTER (value);
4166 int i, len = TREE_STRING_LENGTH (value) - 1;
4167 tree argvec = make_tree_vec (1);
4169 /* Fill in CHARVEC with all of the parameters. */
4170 charvec = make_tree_vec (len);
4171 for (i = 0; i < len; ++i)
4172 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4174 /* Build the argument packs. */
4175 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4177 TREE_VEC_ELT (argvec, 0) = argpack;
4179 return argvec;
4182 /* A subroutine of cp_parser_userdef_numeric_literal to
4183 create a char... template parameter pack from a string node. */
4185 static tree
4186 make_string_pack (tree value)
4188 tree charvec;
4189 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4190 const unsigned char *str
4191 = (const unsigned char *) TREE_STRING_POINTER (value);
4192 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4193 int len = TREE_STRING_LENGTH (value) / sz - 1;
4194 tree argvec = make_tree_vec (2);
4196 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4197 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4199 /* First template parm is character type. */
4200 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4202 /* Fill in CHARVEC with all of the parameters. */
4203 charvec = make_tree_vec (len);
4204 for (int i = 0; i < len; ++i)
4205 TREE_VEC_ELT (charvec, i)
4206 = double_int_to_tree (str_char_type_node,
4207 double_int::from_buffer (str + i * sz, sz));
4209 /* Build the argument packs. */
4210 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4212 TREE_VEC_ELT (argvec, 1) = argpack;
4214 return argvec;
4217 /* Parse a user-defined numeric constant. returns a call to a user-defined
4218 literal operator. */
4220 static cp_expr
4221 cp_parser_userdef_numeric_literal (cp_parser *parser)
4223 cp_token *token = cp_lexer_consume_token (parser->lexer);
4224 tree literal = token->u.value;
4225 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4226 tree value = USERDEF_LITERAL_VALUE (literal);
4227 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4228 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4229 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4230 tree decl, result;
4231 vec<tree, va_gc> *args;
4233 /* Look for a literal operator taking the exact type of numeric argument
4234 as the literal value. */
4235 args = make_tree_vector ();
4236 vec_safe_push (args, value);
4237 decl = lookup_literal_operator (name, args);
4238 if (decl && decl != error_mark_node)
4240 result = finish_call_expr (decl, &args, false, true,
4241 tf_warning_or_error);
4243 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4245 warning_at (token->location, OPT_Woverflow,
4246 "integer literal exceeds range of %qT type",
4247 long_long_unsigned_type_node);
4249 else
4251 if (overflow > 0)
4252 warning_at (token->location, OPT_Woverflow,
4253 "floating literal exceeds range of %qT type",
4254 long_double_type_node);
4255 else if (overflow < 0)
4256 warning_at (token->location, OPT_Woverflow,
4257 "floating literal truncated to zero");
4260 release_tree_vector (args);
4261 return result;
4263 release_tree_vector (args);
4265 /* If the numeric argument didn't work, look for a raw literal
4266 operator taking a const char* argument consisting of the number
4267 in string format. */
4268 args = make_tree_vector ();
4269 vec_safe_push (args, num_string);
4270 decl = lookup_literal_operator (name, args);
4271 if (decl && decl != error_mark_node)
4273 result = finish_call_expr (decl, &args, false, true,
4274 tf_warning_or_error);
4275 release_tree_vector (args);
4276 return result;
4278 release_tree_vector (args);
4280 /* If the raw literal didn't work, look for a non-type template
4281 function with parameter pack char.... Call the function with
4282 template parameter characters representing the number. */
4283 args = make_tree_vector ();
4284 decl = lookup_literal_operator (name, args);
4285 if (decl && decl != error_mark_node)
4287 tree tmpl_args = make_char_string_pack (num_string);
4288 decl = lookup_template_function (decl, tmpl_args);
4289 result = finish_call_expr (decl, &args, false, true,
4290 tf_warning_or_error);
4291 release_tree_vector (args);
4292 return result;
4295 release_tree_vector (args);
4297 error ("unable to find numeric literal operator %qD", name);
4298 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4299 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4300 "to enable more built-in suffixes");
4301 return error_mark_node;
4304 /* Parse a user-defined string constant. Returns a call to a user-defined
4305 literal operator taking a character pointer and the length of the string
4306 as arguments. */
4308 static tree
4309 cp_parser_userdef_string_literal (tree literal)
4311 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4312 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4313 tree value = USERDEF_LITERAL_VALUE (literal);
4314 int len = TREE_STRING_LENGTH (value)
4315 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4316 tree decl, result;
4317 vec<tree, va_gc> *args;
4319 /* Build up a call to the user-defined operator. */
4320 /* Lookup the name we got back from the id-expression. */
4321 args = make_tree_vector ();
4322 vec_safe_push (args, value);
4323 vec_safe_push (args, build_int_cst (size_type_node, len));
4324 decl = lookup_literal_operator (name, args);
4326 if (decl && decl != error_mark_node)
4328 result = finish_call_expr (decl, &args, false, true,
4329 tf_warning_or_error);
4330 release_tree_vector (args);
4331 return result;
4333 release_tree_vector (args);
4335 /* Look for a template function with typename parameter CharT
4336 and parameter pack CharT... Call the function with
4337 template parameter characters representing the string. */
4338 args = make_tree_vector ();
4339 decl = lookup_literal_operator (name, args);
4340 if (decl && decl != error_mark_node)
4342 tree tmpl_args = make_string_pack (value);
4343 decl = lookup_template_function (decl, tmpl_args);
4344 result = finish_call_expr (decl, &args, false, true,
4345 tf_warning_or_error);
4346 release_tree_vector (args);
4347 return result;
4349 release_tree_vector (args);
4351 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4352 name, TREE_TYPE (value), size_type_node);
4353 return error_mark_node;
4357 /* Basic concepts [gram.basic] */
4359 /* Parse a translation-unit.
4361 translation-unit:
4362 declaration-seq [opt]
4364 Returns TRUE if all went well. */
4366 static bool
4367 cp_parser_translation_unit (cp_parser* parser)
4369 /* The address of the first non-permanent object on the declarator
4370 obstack. */
4371 static void *declarator_obstack_base;
4373 bool success;
4375 /* Create the declarator obstack, if necessary. */
4376 if (!cp_error_declarator)
4378 gcc_obstack_init (&declarator_obstack);
4379 /* Create the error declarator. */
4380 cp_error_declarator = make_declarator (cdk_error);
4381 /* Create the empty parameter list. */
4382 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4383 /* Remember where the base of the declarator obstack lies. */
4384 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4387 cp_parser_declaration_seq_opt (parser);
4389 /* If there are no tokens left then all went well. */
4390 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4392 /* Get rid of the token array; we don't need it any more. */
4393 cp_lexer_destroy (parser->lexer);
4394 parser->lexer = NULL;
4396 /* This file might have been a context that's implicitly extern
4397 "C". If so, pop the lang context. (Only relevant for PCH.) */
4398 if (parser->implicit_extern_c)
4400 pop_lang_context ();
4401 parser->implicit_extern_c = false;
4404 /* Finish up. */
4405 finish_translation_unit ();
4407 success = true;
4409 else
4411 cp_parser_error (parser, "expected declaration");
4412 success = false;
4415 /* Make sure the declarator obstack was fully cleaned up. */
4416 gcc_assert (obstack_next_free (&declarator_obstack)
4417 == declarator_obstack_base);
4419 /* All went well. */
4420 return success;
4423 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4424 decltype context. */
4426 static inline tsubst_flags_t
4427 complain_flags (bool decltype_p)
4429 tsubst_flags_t complain = tf_warning_or_error;
4430 if (decltype_p)
4431 complain |= tf_decltype;
4432 return complain;
4435 /* We're about to parse a collection of statements. If we're currently
4436 parsing tentatively, set up a firewall so that any nested
4437 cp_parser_commit_to_tentative_parse won't affect the current context. */
4439 static cp_token_position
4440 cp_parser_start_tentative_firewall (cp_parser *parser)
4442 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4443 return 0;
4445 cp_parser_parse_tentatively (parser);
4446 cp_parser_commit_to_topmost_tentative_parse (parser);
4447 return cp_lexer_token_position (parser->lexer, false);
4450 /* We've finished parsing the collection of statements. Wrap up the
4451 firewall and replace the relevant tokens with the parsed form. */
4453 static void
4454 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4455 tree expr)
4457 if (!start)
4458 return;
4460 /* Finish the firewall level. */
4461 cp_parser_parse_definitely (parser);
4462 /* And remember the result of the parse for when we try again. */
4463 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4464 token->type = CPP_PREPARSED_EXPR;
4465 token->u.value = expr;
4466 token->keyword = RID_MAX;
4467 cp_lexer_purge_tokens_after (parser->lexer, start);
4470 /* Like the above functions, but let the user modify the tokens. Used by
4471 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4472 later parses, so it makes sense to localize the effects of
4473 cp_parser_commit_to_tentative_parse. */
4475 struct tentative_firewall
4477 cp_parser *parser;
4478 bool set;
4480 tentative_firewall (cp_parser *p): parser(p)
4482 /* If we're currently parsing tentatively, start a committed level as a
4483 firewall and then an inner tentative parse. */
4484 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4486 cp_parser_parse_tentatively (parser);
4487 cp_parser_commit_to_topmost_tentative_parse (parser);
4488 cp_parser_parse_tentatively (parser);
4492 ~tentative_firewall()
4494 if (set)
4496 /* Finish the inner tentative parse and the firewall, propagating any
4497 uncommitted error state to the outer tentative parse. */
4498 bool err = cp_parser_error_occurred (parser);
4499 cp_parser_parse_definitely (parser);
4500 cp_parser_parse_definitely (parser);
4501 if (err)
4502 cp_parser_simulate_error (parser);
4507 /* Some tokens naturally come in pairs e.g.'(' and ')'.
4508 This class is for tracking such a matching pair of symbols.
4509 In particular, it tracks the location of the first token,
4510 so that if the second token is missing, we can highlight the
4511 location of the first token when notifying the user about the
4512 problem. */
4514 template <typename traits_t>
4515 class token_pair
4517 public:
4518 /* token_pair's ctor. */
4519 token_pair () : m_open_loc (UNKNOWN_LOCATION) {}
4521 /* If the next token is the opening symbol for this pair, consume it and
4522 return true.
4523 Otherwise, issue an error and return false.
4524 In either case, record the location of the opening token. */
4526 bool require_open (cp_parser *parser)
4528 m_open_loc = cp_lexer_peek_token (parser->lexer)->location;
4529 return cp_parser_require (parser, traits_t::open_token_type,
4530 traits_t::required_token_open);
4533 /* Consume the next token from PARSER, recording its location as
4534 that of the opening token within the pair. */
4536 cp_token * consume_open (cp_parser *parser)
4538 cp_token *tok = cp_lexer_consume_token (parser->lexer);
4539 gcc_assert (tok->type == traits_t::open_token_type);
4540 m_open_loc = tok->location;
4541 return tok;
4544 /* If the next token is the closing symbol for this pair, consume it
4545 and return it.
4546 Otherwise, issue an error, highlighting the location of the
4547 corresponding opening token, and return NULL. */
4549 cp_token *require_close (cp_parser *parser) const
4551 return cp_parser_require (parser, traits_t::close_token_type,
4552 traits_t::required_token_close,
4553 m_open_loc);
4556 private:
4557 location_t m_open_loc;
4560 /* Traits for token_pair<T> for tracking matching pairs of parentheses. */
4562 struct matching_paren_traits
4564 static const enum cpp_ttype open_token_type = CPP_OPEN_PAREN;
4565 static const enum required_token required_token_open = RT_OPEN_PAREN;
4566 static const enum cpp_ttype close_token_type = CPP_CLOSE_PAREN;
4567 static const enum required_token required_token_close = RT_CLOSE_PAREN;
4570 /* "matching_parens" is a token_pair<T> class for tracking matching
4571 pairs of parentheses. */
4573 typedef token_pair<matching_paren_traits> matching_parens;
4575 /* Traits for token_pair<T> for tracking matching pairs of braces. */
4577 struct matching_brace_traits
4579 static const enum cpp_ttype open_token_type = CPP_OPEN_BRACE;
4580 static const enum required_token required_token_open = RT_OPEN_BRACE;
4581 static const enum cpp_ttype close_token_type = CPP_CLOSE_BRACE;
4582 static const enum required_token required_token_close = RT_CLOSE_BRACE;
4585 /* "matching_braces" is a token_pair<T> class for tracking matching
4586 pairs of braces. */
4588 typedef token_pair<matching_brace_traits> matching_braces;
4591 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4592 enclosing parentheses. */
4594 static cp_expr
4595 cp_parser_statement_expr (cp_parser *parser)
4597 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4599 /* Consume the '('. */
4600 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4601 matching_parens parens;
4602 parens.consume_open (parser);
4603 /* Start the statement-expression. */
4604 tree expr = begin_stmt_expr ();
4605 /* Parse the compound-statement. */
4606 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4607 /* Finish up. */
4608 expr = finish_stmt_expr (expr, false);
4609 /* Consume the ')'. */
4610 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4611 if (!parens.require_close (parser))
4612 cp_parser_skip_to_end_of_statement (parser);
4614 cp_parser_end_tentative_firewall (parser, start, expr);
4615 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4616 return cp_expr (expr, combined_loc);
4619 /* Expressions [gram.expr] */
4621 /* Parse a fold-operator.
4623 fold-operator:
4624 - * / % ^ & | = < > << >>
4625 = -= *= /= %= ^= &= |= <<= >>=
4626 == != <= >= && || , .* ->*
4628 This returns the tree code corresponding to the matched operator
4629 as an int. When the current token matches a compound assignment
4630 opertor, the resulting tree code is the negative value of the
4631 non-assignment operator. */
4633 static int
4634 cp_parser_fold_operator (cp_token *token)
4636 switch (token->type)
4638 case CPP_PLUS: return PLUS_EXPR;
4639 case CPP_MINUS: return MINUS_EXPR;
4640 case CPP_MULT: return MULT_EXPR;
4641 case CPP_DIV: return TRUNC_DIV_EXPR;
4642 case CPP_MOD: return TRUNC_MOD_EXPR;
4643 case CPP_XOR: return BIT_XOR_EXPR;
4644 case CPP_AND: return BIT_AND_EXPR;
4645 case CPP_OR: return BIT_IOR_EXPR;
4646 case CPP_LSHIFT: return LSHIFT_EXPR;
4647 case CPP_RSHIFT: return RSHIFT_EXPR;
4649 case CPP_EQ: return -NOP_EXPR;
4650 case CPP_PLUS_EQ: return -PLUS_EXPR;
4651 case CPP_MINUS_EQ: return -MINUS_EXPR;
4652 case CPP_MULT_EQ: return -MULT_EXPR;
4653 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4654 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4655 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4656 case CPP_AND_EQ: return -BIT_AND_EXPR;
4657 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4658 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4659 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4661 case CPP_EQ_EQ: return EQ_EXPR;
4662 case CPP_NOT_EQ: return NE_EXPR;
4663 case CPP_LESS: return LT_EXPR;
4664 case CPP_GREATER: return GT_EXPR;
4665 case CPP_LESS_EQ: return LE_EXPR;
4666 case CPP_GREATER_EQ: return GE_EXPR;
4668 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4669 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4671 case CPP_COMMA: return COMPOUND_EXPR;
4673 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4674 case CPP_DEREF_STAR: return MEMBER_REF;
4676 default: return ERROR_MARK;
4680 /* Returns true if CODE indicates a binary expression, which is not allowed in
4681 the LHS of a fold-expression. More codes will need to be added to use this
4682 function in other contexts. */
4684 static bool
4685 is_binary_op (tree_code code)
4687 switch (code)
4689 case PLUS_EXPR:
4690 case POINTER_PLUS_EXPR:
4691 case MINUS_EXPR:
4692 case MULT_EXPR:
4693 case TRUNC_DIV_EXPR:
4694 case TRUNC_MOD_EXPR:
4695 case BIT_XOR_EXPR:
4696 case BIT_AND_EXPR:
4697 case BIT_IOR_EXPR:
4698 case LSHIFT_EXPR:
4699 case RSHIFT_EXPR:
4701 case MODOP_EXPR:
4703 case EQ_EXPR:
4704 case NE_EXPR:
4705 case LE_EXPR:
4706 case GE_EXPR:
4707 case LT_EXPR:
4708 case GT_EXPR:
4710 case TRUTH_ANDIF_EXPR:
4711 case TRUTH_ORIF_EXPR:
4713 case COMPOUND_EXPR:
4715 case DOTSTAR_EXPR:
4716 case MEMBER_REF:
4717 return true;
4719 default:
4720 return false;
4724 /* If the next token is a suitable fold operator, consume it and return as
4725 the function above. */
4727 static int
4728 cp_parser_fold_operator (cp_parser *parser)
4730 cp_token* token = cp_lexer_peek_token (parser->lexer);
4731 int code = cp_parser_fold_operator (token);
4732 if (code != ERROR_MARK)
4733 cp_lexer_consume_token (parser->lexer);
4734 return code;
4737 /* Parse a fold-expression.
4739 fold-expression:
4740 ( ... folding-operator cast-expression)
4741 ( cast-expression folding-operator ... )
4742 ( cast-expression folding operator ... folding-operator cast-expression)
4744 Note that the '(' and ')' are matched in primary expression. */
4746 static cp_expr
4747 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4749 cp_id_kind pidk;
4751 // Left fold.
4752 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4754 cp_lexer_consume_token (parser->lexer);
4755 int op = cp_parser_fold_operator (parser);
4756 if (op == ERROR_MARK)
4758 cp_parser_error (parser, "expected binary operator");
4759 return error_mark_node;
4762 tree expr = cp_parser_cast_expression (parser, false, false,
4763 false, &pidk);
4764 if (expr == error_mark_node)
4765 return error_mark_node;
4766 return finish_left_unary_fold_expr (expr, op);
4769 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4770 int op = cp_parser_fold_operator (parser);
4771 if (op == ERROR_MARK)
4773 cp_parser_error (parser, "expected binary operator");
4774 return error_mark_node;
4777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4779 cp_parser_error (parser, "expected ...");
4780 return error_mark_node;
4782 cp_lexer_consume_token (parser->lexer);
4784 /* The operands of a fold-expression are cast-expressions, so binary or
4785 conditional expressions are not allowed. We check this here to avoid
4786 tentative parsing. */
4787 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4788 /* OK, the expression was parenthesized. */;
4789 else if (is_binary_op (TREE_CODE (expr1)))
4790 error_at (location_of (expr1),
4791 "binary expression in operand of fold-expression");
4792 else if (TREE_CODE (expr1) == COND_EXPR)
4793 error_at (location_of (expr1),
4794 "conditional expression in operand of fold-expression");
4796 // Right fold.
4797 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4798 return finish_right_unary_fold_expr (expr1, op);
4800 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4802 cp_parser_error (parser, "mismatched operator in fold-expression");
4803 return error_mark_node;
4805 cp_lexer_consume_token (parser->lexer);
4807 // Binary left or right fold.
4808 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4809 if (expr2 == error_mark_node)
4810 return error_mark_node;
4811 return finish_binary_fold_expr (expr1, expr2, op);
4814 /* Parse a primary-expression.
4816 primary-expression:
4817 literal
4818 this
4819 ( expression )
4820 id-expression
4821 lambda-expression (C++11)
4823 GNU Extensions:
4825 primary-expression:
4826 ( compound-statement )
4827 __builtin_va_arg ( assignment-expression , type-id )
4828 __builtin_offsetof ( type-id , offsetof-expression )
4830 C++ Extensions:
4831 __has_nothrow_assign ( type-id )
4832 __has_nothrow_constructor ( type-id )
4833 __has_nothrow_copy ( type-id )
4834 __has_trivial_assign ( type-id )
4835 __has_trivial_constructor ( type-id )
4836 __has_trivial_copy ( type-id )
4837 __has_trivial_destructor ( type-id )
4838 __has_virtual_destructor ( type-id )
4839 __is_abstract ( type-id )
4840 __is_base_of ( type-id , type-id )
4841 __is_class ( type-id )
4842 __is_empty ( type-id )
4843 __is_enum ( type-id )
4844 __is_final ( type-id )
4845 __is_literal_type ( type-id )
4846 __is_pod ( type-id )
4847 __is_polymorphic ( type-id )
4848 __is_std_layout ( type-id )
4849 __is_trivial ( type-id )
4850 __is_union ( type-id )
4852 Objective-C++ Extension:
4854 primary-expression:
4855 objc-expression
4857 literal:
4858 __null
4860 ADDRESS_P is true iff this expression was immediately preceded by
4861 "&" and therefore might denote a pointer-to-member. CAST_P is true
4862 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4863 true iff this expression is a template argument.
4865 Returns a representation of the expression. Upon return, *IDK
4866 indicates what kind of id-expression (if any) was present. */
4868 static cp_expr
4869 cp_parser_primary_expression (cp_parser *parser,
4870 bool address_p,
4871 bool cast_p,
4872 bool template_arg_p,
4873 bool decltype_p,
4874 cp_id_kind *idk)
4876 cp_token *token = NULL;
4878 /* Assume the primary expression is not an id-expression. */
4879 *idk = CP_ID_KIND_NONE;
4881 /* Peek at the next token. */
4882 token = cp_lexer_peek_token (parser->lexer);
4883 switch ((int) token->type)
4885 /* literal:
4886 integer-literal
4887 character-literal
4888 floating-literal
4889 string-literal
4890 boolean-literal
4891 pointer-literal
4892 user-defined-literal */
4893 case CPP_CHAR:
4894 case CPP_CHAR16:
4895 case CPP_CHAR32:
4896 case CPP_WCHAR:
4897 case CPP_UTF8CHAR:
4898 case CPP_NUMBER:
4899 case CPP_PREPARSED_EXPR:
4900 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4901 return cp_parser_userdef_numeric_literal (parser);
4902 token = cp_lexer_consume_token (parser->lexer);
4903 if (TREE_CODE (token->u.value) == FIXED_CST)
4905 error_at (token->location,
4906 "fixed-point types not supported in C++");
4907 return error_mark_node;
4909 /* Floating-point literals are only allowed in an integral
4910 constant expression if they are cast to an integral or
4911 enumeration type. */
4912 if (TREE_CODE (token->u.value) == REAL_CST
4913 && parser->integral_constant_expression_p
4914 && pedantic)
4916 /* CAST_P will be set even in invalid code like "int(2.7 +
4917 ...)". Therefore, we have to check that the next token
4918 is sure to end the cast. */
4919 if (cast_p)
4921 cp_token *next_token;
4923 next_token = cp_lexer_peek_token (parser->lexer);
4924 if (/* The comma at the end of an
4925 enumerator-definition. */
4926 next_token->type != CPP_COMMA
4927 /* The curly brace at the end of an enum-specifier. */
4928 && next_token->type != CPP_CLOSE_BRACE
4929 /* The end of a statement. */
4930 && next_token->type != CPP_SEMICOLON
4931 /* The end of the cast-expression. */
4932 && next_token->type != CPP_CLOSE_PAREN
4933 /* The end of an array bound. */
4934 && next_token->type != CPP_CLOSE_SQUARE
4935 /* The closing ">" in a template-argument-list. */
4936 && (next_token->type != CPP_GREATER
4937 || parser->greater_than_is_operator_p)
4938 /* C++0x only: A ">>" treated like two ">" tokens,
4939 in a template-argument-list. */
4940 && (next_token->type != CPP_RSHIFT
4941 || (cxx_dialect == cxx98)
4942 || parser->greater_than_is_operator_p))
4943 cast_p = false;
4946 /* If we are within a cast, then the constraint that the
4947 cast is to an integral or enumeration type will be
4948 checked at that point. If we are not within a cast, then
4949 this code is invalid. */
4950 if (!cast_p)
4951 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4953 return cp_expr (token->u.value, token->location);
4955 case CPP_CHAR_USERDEF:
4956 case CPP_CHAR16_USERDEF:
4957 case CPP_CHAR32_USERDEF:
4958 case CPP_WCHAR_USERDEF:
4959 case CPP_UTF8CHAR_USERDEF:
4960 return cp_parser_userdef_char_literal (parser);
4962 case CPP_STRING:
4963 case CPP_STRING16:
4964 case CPP_STRING32:
4965 case CPP_WSTRING:
4966 case CPP_UTF8STRING:
4967 case CPP_STRING_USERDEF:
4968 case CPP_STRING16_USERDEF:
4969 case CPP_STRING32_USERDEF:
4970 case CPP_WSTRING_USERDEF:
4971 case CPP_UTF8STRING_USERDEF:
4972 /* ??? Should wide strings be allowed when parser->translate_strings_p
4973 is false (i.e. in attributes)? If not, we can kill the third
4974 argument to cp_parser_string_literal. */
4975 return cp_parser_string_literal (parser,
4976 parser->translate_strings_p,
4977 true);
4979 case CPP_OPEN_PAREN:
4980 /* If we see `( { ' then we are looking at the beginning of
4981 a GNU statement-expression. */
4982 if (cp_parser_allow_gnu_extensions_p (parser)
4983 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4985 /* Statement-expressions are not allowed by the standard. */
4986 pedwarn (token->location, OPT_Wpedantic,
4987 "ISO C++ forbids braced-groups within expressions");
4989 /* And they're not allowed outside of a function-body; you
4990 cannot, for example, write:
4992 int i = ({ int j = 3; j + 1; });
4994 at class or namespace scope. */
4995 if (!parser->in_function_body
4996 || parser->in_template_argument_list_p)
4998 error_at (token->location,
4999 "statement-expressions are not allowed outside "
5000 "functions nor in template-argument lists");
5001 cp_parser_skip_to_end_of_block_or_statement (parser);
5002 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
5003 cp_lexer_consume_token (parser->lexer);
5004 return error_mark_node;
5006 else
5007 return cp_parser_statement_expr (parser);
5009 /* Otherwise it's a normal parenthesized expression. */
5011 cp_expr expr;
5012 bool saved_greater_than_is_operator_p;
5014 location_t open_paren_loc = token->location;
5016 /* Consume the `('. */
5017 matching_parens parens;
5018 parens.consume_open (parser);
5019 /* Within a parenthesized expression, a `>' token is always
5020 the greater-than operator. */
5021 saved_greater_than_is_operator_p
5022 = parser->greater_than_is_operator_p;
5023 parser->greater_than_is_operator_p = true;
5025 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5026 /* Left fold expression. */
5027 expr = NULL_TREE;
5028 else
5029 /* Parse the parenthesized expression. */
5030 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
5032 token = cp_lexer_peek_token (parser->lexer);
5033 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
5035 expr = cp_parser_fold_expression (parser, expr);
5036 if (expr != error_mark_node
5037 && cxx_dialect < cxx1z
5038 && !in_system_header_at (input_location))
5039 pedwarn (input_location, 0, "fold-expressions only available "
5040 "with -std=c++1z or -std=gnu++1z");
5042 else
5043 /* Let the front end know that this expression was
5044 enclosed in parentheses. This matters in case, for
5045 example, the expression is of the form `A::B', since
5046 `&A::B' might be a pointer-to-member, but `&(A::B)' is
5047 not. */
5048 expr = finish_parenthesized_expr (expr);
5050 /* DR 705: Wrapping an unqualified name in parentheses
5051 suppresses arg-dependent lookup. We want to pass back
5052 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
5053 (c++/37862), but none of the others. */
5054 if (*idk != CP_ID_KIND_QUALIFIED)
5055 *idk = CP_ID_KIND_NONE;
5057 /* The `>' token might be the end of a template-id or
5058 template-parameter-list now. */
5059 parser->greater_than_is_operator_p
5060 = saved_greater_than_is_operator_p;
5062 /* Consume the `)'. */
5063 token = cp_lexer_peek_token (parser->lexer);
5064 location_t close_paren_loc = token->location;
5065 expr.set_range (open_paren_loc, close_paren_loc);
5066 if (!parens.require_close (parser)
5067 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5068 cp_parser_skip_to_end_of_statement (parser);
5070 return expr;
5073 case CPP_OPEN_SQUARE:
5075 if (c_dialect_objc ())
5077 /* We might have an Objective-C++ message. */
5078 cp_parser_parse_tentatively (parser);
5079 tree msg = cp_parser_objc_message_expression (parser);
5080 /* If that works out, we're done ... */
5081 if (cp_parser_parse_definitely (parser))
5082 return msg;
5083 /* ... else, fall though to see if it's a lambda. */
5085 cp_expr lam = cp_parser_lambda_expression (parser);
5086 /* Don't warn about a failed tentative parse. */
5087 if (cp_parser_error_occurred (parser))
5088 return error_mark_node;
5089 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5090 return lam;
5093 case CPP_OBJC_STRING:
5094 if (c_dialect_objc ())
5095 /* We have an Objective-C++ string literal. */
5096 return cp_parser_objc_expression (parser);
5097 cp_parser_error (parser, "expected primary-expression");
5098 return error_mark_node;
5100 case CPP_KEYWORD:
5101 switch (token->keyword)
5103 /* These two are the boolean literals. */
5104 case RID_TRUE:
5105 cp_lexer_consume_token (parser->lexer);
5106 return cp_expr (boolean_true_node, token->location);
5107 case RID_FALSE:
5108 cp_lexer_consume_token (parser->lexer);
5109 return cp_expr (boolean_false_node, token->location);
5111 /* The `__null' literal. */
5112 case RID_NULL:
5113 cp_lexer_consume_token (parser->lexer);
5114 return cp_expr (null_node, token->location);
5116 /* The `nullptr' literal. */
5117 case RID_NULLPTR:
5118 cp_lexer_consume_token (parser->lexer);
5119 return cp_expr (nullptr_node, token->location);
5121 /* Recognize the `this' keyword. */
5122 case RID_THIS:
5123 cp_lexer_consume_token (parser->lexer);
5124 if (parser->local_variables_forbidden_p)
5126 error_at (token->location,
5127 "%<this%> may not be used in this context");
5128 return error_mark_node;
5130 /* Pointers cannot appear in constant-expressions. */
5131 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5132 return error_mark_node;
5133 return cp_expr (finish_this_expr (), token->location);
5135 /* The `operator' keyword can be the beginning of an
5136 id-expression. */
5137 case RID_OPERATOR:
5138 goto id_expression;
5140 case RID_FUNCTION_NAME:
5141 case RID_PRETTY_FUNCTION_NAME:
5142 case RID_C99_FUNCTION_NAME:
5144 non_integral_constant name;
5146 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5147 __func__ are the names of variables -- but they are
5148 treated specially. Therefore, they are handled here,
5149 rather than relying on the generic id-expression logic
5150 below. Grammatically, these names are id-expressions.
5152 Consume the token. */
5153 token = cp_lexer_consume_token (parser->lexer);
5155 switch (token->keyword)
5157 case RID_FUNCTION_NAME:
5158 name = NIC_FUNC_NAME;
5159 break;
5160 case RID_PRETTY_FUNCTION_NAME:
5161 name = NIC_PRETTY_FUNC;
5162 break;
5163 case RID_C99_FUNCTION_NAME:
5164 name = NIC_C99_FUNC;
5165 break;
5166 default:
5167 gcc_unreachable ();
5170 if (cp_parser_non_integral_constant_expression (parser, name))
5171 return error_mark_node;
5173 /* Look up the name. */
5174 return finish_fname (token->u.value);
5177 case RID_VA_ARG:
5179 tree expression;
5180 tree type;
5181 source_location type_location;
5182 location_t start_loc
5183 = cp_lexer_peek_token (parser->lexer)->location;
5184 /* The `__builtin_va_arg' construct is used to handle
5185 `va_arg'. Consume the `__builtin_va_arg' token. */
5186 cp_lexer_consume_token (parser->lexer);
5187 /* Look for the opening `('. */
5188 matching_parens parens;
5189 parens.require_open (parser);
5190 /* Now, parse the assignment-expression. */
5191 expression = cp_parser_assignment_expression (parser);
5192 /* Look for the `,'. */
5193 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5194 type_location = cp_lexer_peek_token (parser->lexer)->location;
5195 /* Parse the type-id. */
5197 type_id_in_expr_sentinel s (parser);
5198 type = cp_parser_type_id (parser);
5200 /* Look for the closing `)'. */
5201 location_t finish_loc
5202 = cp_lexer_peek_token (parser->lexer)->location;
5203 parens.require_close (parser);
5204 /* Using `va_arg' in a constant-expression is not
5205 allowed. */
5206 if (cp_parser_non_integral_constant_expression (parser,
5207 NIC_VA_ARG))
5208 return error_mark_node;
5209 /* Construct a location of the form:
5210 __builtin_va_arg (v, int)
5211 ~~~~~~~~~~~~~~~~~~~~~^~~~
5212 with the caret at the type, ranging from the start of the
5213 "__builtin_va_arg" token to the close paren. */
5214 location_t combined_loc
5215 = make_location (type_location, start_loc, finish_loc);
5216 return build_x_va_arg (combined_loc, expression, type);
5219 case RID_OFFSETOF:
5220 return cp_parser_builtin_offsetof (parser);
5222 case RID_HAS_NOTHROW_ASSIGN:
5223 case RID_HAS_NOTHROW_CONSTRUCTOR:
5224 case RID_HAS_NOTHROW_COPY:
5225 case RID_HAS_TRIVIAL_ASSIGN:
5226 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5227 case RID_HAS_TRIVIAL_COPY:
5228 case RID_HAS_TRIVIAL_DESTRUCTOR:
5229 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5230 case RID_HAS_VIRTUAL_DESTRUCTOR:
5231 case RID_IS_ABSTRACT:
5232 case RID_IS_AGGREGATE:
5233 case RID_IS_BASE_OF:
5234 case RID_IS_CLASS:
5235 case RID_IS_EMPTY:
5236 case RID_IS_ENUM:
5237 case RID_IS_FINAL:
5238 case RID_IS_LITERAL_TYPE:
5239 case RID_IS_POD:
5240 case RID_IS_POLYMORPHIC:
5241 case RID_IS_SAME_AS:
5242 case RID_IS_STD_LAYOUT:
5243 case RID_IS_TRIVIAL:
5244 case RID_IS_TRIVIALLY_ASSIGNABLE:
5245 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5246 case RID_IS_TRIVIALLY_COPYABLE:
5247 case RID_IS_UNION:
5248 case RID_IS_ASSIGNABLE:
5249 case RID_IS_CONSTRUCTIBLE:
5250 return cp_parser_trait_expr (parser, token->keyword);
5252 // C++ concepts
5253 case RID_REQUIRES:
5254 return cp_parser_requires_expression (parser);
5256 /* Objective-C++ expressions. */
5257 case RID_AT_ENCODE:
5258 case RID_AT_PROTOCOL:
5259 case RID_AT_SELECTOR:
5260 return cp_parser_objc_expression (parser);
5262 case RID_TEMPLATE:
5263 if (parser->in_function_body
5264 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5265 == CPP_LESS))
5267 error_at (token->location,
5268 "a template declaration cannot appear at block scope");
5269 cp_parser_skip_to_end_of_block_or_statement (parser);
5270 return error_mark_node;
5272 /* FALLTHRU */
5273 default:
5274 cp_parser_error (parser, "expected primary-expression");
5275 return error_mark_node;
5278 /* An id-expression can start with either an identifier, a
5279 `::' as the beginning of a qualified-id, or the "operator"
5280 keyword. */
5281 case CPP_NAME:
5282 case CPP_SCOPE:
5283 case CPP_TEMPLATE_ID:
5284 case CPP_NESTED_NAME_SPECIFIER:
5286 id_expression:
5287 cp_expr id_expression;
5288 cp_expr decl;
5289 const char *error_msg;
5290 bool template_p;
5291 bool done;
5292 cp_token *id_expr_token;
5294 /* Parse the id-expression. */
5295 id_expression
5296 = cp_parser_id_expression (parser,
5297 /*template_keyword_p=*/false,
5298 /*check_dependency_p=*/true,
5299 &template_p,
5300 /*declarator_p=*/false,
5301 /*optional_p=*/false);
5302 if (id_expression == error_mark_node)
5303 return error_mark_node;
5304 id_expr_token = token;
5305 token = cp_lexer_peek_token (parser->lexer);
5306 done = (token->type != CPP_OPEN_SQUARE
5307 && token->type != CPP_OPEN_PAREN
5308 && token->type != CPP_DOT
5309 && token->type != CPP_DEREF
5310 && token->type != CPP_PLUS_PLUS
5311 && token->type != CPP_MINUS_MINUS);
5312 /* If we have a template-id, then no further lookup is
5313 required. If the template-id was for a template-class, we
5314 will sometimes have a TYPE_DECL at this point. */
5315 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5316 || TREE_CODE (id_expression) == TYPE_DECL)
5317 decl = id_expression;
5318 /* Look up the name. */
5319 else
5321 tree ambiguous_decls;
5323 /* If we already know that this lookup is ambiguous, then
5324 we've already issued an error message; there's no reason
5325 to check again. */
5326 if (id_expr_token->type == CPP_NAME
5327 && id_expr_token->error_reported)
5329 cp_parser_simulate_error (parser);
5330 return error_mark_node;
5333 decl = cp_parser_lookup_name (parser, id_expression,
5334 none_type,
5335 template_p,
5336 /*is_namespace=*/false,
5337 /*check_dependency=*/true,
5338 &ambiguous_decls,
5339 id_expr_token->location);
5340 /* If the lookup was ambiguous, an error will already have
5341 been issued. */
5342 if (ambiguous_decls)
5343 return error_mark_node;
5345 /* In Objective-C++, we may have an Objective-C 2.0
5346 dot-syntax for classes here. */
5347 if (c_dialect_objc ()
5348 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5349 && TREE_CODE (decl) == TYPE_DECL
5350 && objc_is_class_name (decl))
5352 tree component;
5353 cp_lexer_consume_token (parser->lexer);
5354 component = cp_parser_identifier (parser);
5355 if (component == error_mark_node)
5356 return error_mark_node;
5358 tree result = objc_build_class_component_ref (id_expression,
5359 component);
5360 /* Build a location of the form:
5361 expr.component
5362 ~~~~~^~~~~~~~~
5363 with caret at the start of the component name (at
5364 input_location), ranging from the start of the id_expression
5365 to the end of the component name. */
5366 location_t combined_loc
5367 = make_location (input_location, id_expression.get_start (),
5368 get_finish (input_location));
5369 protected_set_expr_location (result, combined_loc);
5370 return result;
5373 /* In Objective-C++, an instance variable (ivar) may be preferred
5374 to whatever cp_parser_lookup_name() found.
5375 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5376 rest of c-family, we have to do a little extra work to preserve
5377 any location information in cp_expr "decl". Given that
5378 objc_lookup_ivar is implemented in "c-family" and "objc", we
5379 have a trip through the pure "tree" type, rather than cp_expr.
5380 Naively copying it back to "decl" would implicitly give the
5381 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5382 store an EXPR_LOCATION. Hence we only update "decl" (and
5383 hence its location_t) if we get back a different tree node. */
5384 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5385 id_expression);
5386 if (decl_tree != decl.get_value ())
5387 decl = cp_expr (decl_tree);
5389 /* If name lookup gives us a SCOPE_REF, then the
5390 qualifying scope was dependent. */
5391 if (TREE_CODE (decl) == SCOPE_REF)
5393 /* At this point, we do not know if DECL is a valid
5394 integral constant expression. We assume that it is
5395 in fact such an expression, so that code like:
5397 template <int N> struct A {
5398 int a[B<N>::i];
5401 is accepted. At template-instantiation time, we
5402 will check that B<N>::i is actually a constant. */
5403 return decl;
5405 /* Check to see if DECL is a local variable in a context
5406 where that is forbidden. */
5407 if (parser->local_variables_forbidden_p
5408 && local_variable_p (decl))
5410 /* It might be that we only found DECL because we are
5411 trying to be generous with pre-ISO scoping rules.
5412 For example, consider:
5414 int i;
5415 void g() {
5416 for (int i = 0; i < 10; ++i) {}
5417 extern void f(int j = i);
5420 Here, name look up will originally find the out
5421 of scope `i'. We need to issue a warning message,
5422 but then use the global `i'. */
5423 decl = check_for_out_of_scope_variable (decl);
5424 if (local_variable_p (decl))
5426 error_at (id_expr_token->location,
5427 "local variable %qD may not appear in this context",
5428 decl.get_value ());
5429 return error_mark_node;
5434 decl = (finish_id_expression
5435 (id_expression, decl, parser->scope,
5436 idk,
5437 parser->integral_constant_expression_p,
5438 parser->allow_non_integral_constant_expression_p,
5439 &parser->non_integral_constant_expression_p,
5440 template_p, done, address_p,
5441 template_arg_p,
5442 &error_msg,
5443 id_expression.get_location ()));
5444 if (error_msg)
5445 cp_parser_error (parser, error_msg);
5446 decl.set_location (id_expr_token->location);
5447 return decl;
5450 /* Anything else is an error. */
5451 default:
5452 cp_parser_error (parser, "expected primary-expression");
5453 return error_mark_node;
5457 static inline cp_expr
5458 cp_parser_primary_expression (cp_parser *parser,
5459 bool address_p,
5460 bool cast_p,
5461 bool template_arg_p,
5462 cp_id_kind *idk)
5464 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5465 /*decltype*/false, idk);
5468 /* Parse an id-expression.
5470 id-expression:
5471 unqualified-id
5472 qualified-id
5474 qualified-id:
5475 :: [opt] nested-name-specifier template [opt] unqualified-id
5476 :: identifier
5477 :: operator-function-id
5478 :: template-id
5480 Return a representation of the unqualified portion of the
5481 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5482 a `::' or nested-name-specifier.
5484 Often, if the id-expression was a qualified-id, the caller will
5485 want to make a SCOPE_REF to represent the qualified-id. This
5486 function does not do this in order to avoid wastefully creating
5487 SCOPE_REFs when they are not required.
5489 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5490 `template' keyword.
5492 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5493 uninstantiated templates.
5495 If *TEMPLATE_P is non-NULL, it is set to true iff the
5496 `template' keyword is used to explicitly indicate that the entity
5497 named is a template.
5499 If DECLARATOR_P is true, the id-expression is appearing as part of
5500 a declarator, rather than as part of an expression. */
5502 static cp_expr
5503 cp_parser_id_expression (cp_parser *parser,
5504 bool template_keyword_p,
5505 bool check_dependency_p,
5506 bool *template_p,
5507 bool declarator_p,
5508 bool optional_p)
5510 bool global_scope_p;
5511 bool nested_name_specifier_p;
5513 /* Assume the `template' keyword was not used. */
5514 if (template_p)
5515 *template_p = template_keyword_p;
5517 /* Look for the optional `::' operator. */
5518 global_scope_p
5519 = (!template_keyword_p
5520 && (cp_parser_global_scope_opt (parser,
5521 /*current_scope_valid_p=*/false)
5522 != NULL_TREE));
5524 /* Look for the optional nested-name-specifier. */
5525 nested_name_specifier_p
5526 = (cp_parser_nested_name_specifier_opt (parser,
5527 /*typename_keyword_p=*/false,
5528 check_dependency_p,
5529 /*type_p=*/false,
5530 declarator_p,
5531 template_keyword_p)
5532 != NULL_TREE);
5534 /* If there is a nested-name-specifier, then we are looking at
5535 the first qualified-id production. */
5536 if (nested_name_specifier_p)
5538 tree saved_scope;
5539 tree saved_object_scope;
5540 tree saved_qualifying_scope;
5541 cp_expr unqualified_id;
5542 bool is_template;
5544 /* See if the next token is the `template' keyword. */
5545 if (!template_p)
5546 template_p = &is_template;
5547 *template_p = cp_parser_optional_template_keyword (parser);
5548 /* Name lookup we do during the processing of the
5549 unqualified-id might obliterate SCOPE. */
5550 saved_scope = parser->scope;
5551 saved_object_scope = parser->object_scope;
5552 saved_qualifying_scope = parser->qualifying_scope;
5553 /* Process the final unqualified-id. */
5554 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5555 check_dependency_p,
5556 declarator_p,
5557 /*optional_p=*/false);
5558 /* Restore the SAVED_SCOPE for our caller. */
5559 parser->scope = saved_scope;
5560 parser->object_scope = saved_object_scope;
5561 parser->qualifying_scope = saved_qualifying_scope;
5563 return unqualified_id;
5565 /* Otherwise, if we are in global scope, then we are looking at one
5566 of the other qualified-id productions. */
5567 else if (global_scope_p)
5569 cp_token *token;
5570 tree id;
5572 /* Peek at the next token. */
5573 token = cp_lexer_peek_token (parser->lexer);
5575 /* If it's an identifier, and the next token is not a "<", then
5576 we can avoid the template-id case. This is an optimization
5577 for this common case. */
5578 if (token->type == CPP_NAME
5579 && !cp_parser_nth_token_starts_template_argument_list_p
5580 (parser, 2))
5581 return cp_parser_identifier (parser);
5583 cp_parser_parse_tentatively (parser);
5584 /* Try a template-id. */
5585 id = cp_parser_template_id (parser,
5586 /*template_keyword_p=*/false,
5587 /*check_dependency_p=*/true,
5588 none_type,
5589 declarator_p);
5590 /* If that worked, we're done. */
5591 if (cp_parser_parse_definitely (parser))
5592 return id;
5594 /* Peek at the next token. (Changes in the token buffer may
5595 have invalidated the pointer obtained above.) */
5596 token = cp_lexer_peek_token (parser->lexer);
5598 switch (token->type)
5600 case CPP_NAME:
5601 return cp_parser_identifier (parser);
5603 case CPP_KEYWORD:
5604 if (token->keyword == RID_OPERATOR)
5605 return cp_parser_operator_function_id (parser);
5606 /* Fall through. */
5608 default:
5609 cp_parser_error (parser, "expected id-expression");
5610 return error_mark_node;
5613 else
5614 return cp_parser_unqualified_id (parser, template_keyword_p,
5615 /*check_dependency_p=*/true,
5616 declarator_p,
5617 optional_p);
5620 /* Parse an unqualified-id.
5622 unqualified-id:
5623 identifier
5624 operator-function-id
5625 conversion-function-id
5626 ~ class-name
5627 template-id
5629 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5630 keyword, in a construct like `A::template ...'.
5632 Returns a representation of unqualified-id. For the `identifier'
5633 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5634 production a BIT_NOT_EXPR is returned; the operand of the
5635 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5636 other productions, see the documentation accompanying the
5637 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5638 names are looked up in uninstantiated templates. If DECLARATOR_P
5639 is true, the unqualified-id is appearing as part of a declarator,
5640 rather than as part of an expression. */
5642 static cp_expr
5643 cp_parser_unqualified_id (cp_parser* parser,
5644 bool template_keyword_p,
5645 bool check_dependency_p,
5646 bool declarator_p,
5647 bool optional_p)
5649 cp_token *token;
5651 /* Peek at the next token. */
5652 token = cp_lexer_peek_token (parser->lexer);
5654 switch ((int) token->type)
5656 case CPP_NAME:
5658 tree id;
5660 /* We don't know yet whether or not this will be a
5661 template-id. */
5662 cp_parser_parse_tentatively (parser);
5663 /* Try a template-id. */
5664 id = cp_parser_template_id (parser, template_keyword_p,
5665 check_dependency_p,
5666 none_type,
5667 declarator_p);
5668 /* If it worked, we're done. */
5669 if (cp_parser_parse_definitely (parser))
5670 return id;
5671 /* Otherwise, it's an ordinary identifier. */
5672 return cp_parser_identifier (parser);
5675 case CPP_TEMPLATE_ID:
5676 return cp_parser_template_id (parser, template_keyword_p,
5677 check_dependency_p,
5678 none_type,
5679 declarator_p);
5681 case CPP_COMPL:
5683 tree type_decl;
5684 tree qualifying_scope;
5685 tree object_scope;
5686 tree scope;
5687 bool done;
5689 /* Consume the `~' token. */
5690 cp_lexer_consume_token (parser->lexer);
5691 /* Parse the class-name. The standard, as written, seems to
5692 say that:
5694 template <typename T> struct S { ~S (); };
5695 template <typename T> S<T>::~S() {}
5697 is invalid, since `~' must be followed by a class-name, but
5698 `S<T>' is dependent, and so not known to be a class.
5699 That's not right; we need to look in uninstantiated
5700 templates. A further complication arises from:
5702 template <typename T> void f(T t) {
5703 t.T::~T();
5706 Here, it is not possible to look up `T' in the scope of `T'
5707 itself. We must look in both the current scope, and the
5708 scope of the containing complete expression.
5710 Yet another issue is:
5712 struct S {
5713 int S;
5714 ~S();
5717 S::~S() {}
5719 The standard does not seem to say that the `S' in `~S'
5720 should refer to the type `S' and not the data member
5721 `S::S'. */
5723 /* DR 244 says that we look up the name after the "~" in the
5724 same scope as we looked up the qualifying name. That idea
5725 isn't fully worked out; it's more complicated than that. */
5726 scope = parser->scope;
5727 object_scope = parser->object_scope;
5728 qualifying_scope = parser->qualifying_scope;
5730 /* Check for invalid scopes. */
5731 if (scope == error_mark_node)
5733 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5734 cp_lexer_consume_token (parser->lexer);
5735 return error_mark_node;
5737 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5739 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5740 error_at (token->location,
5741 "scope %qT before %<~%> is not a class-name",
5742 scope);
5743 cp_parser_simulate_error (parser);
5744 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5745 cp_lexer_consume_token (parser->lexer);
5746 return error_mark_node;
5748 gcc_assert (!scope || TYPE_P (scope));
5750 /* If the name is of the form "X::~X" it's OK even if X is a
5751 typedef. */
5752 token = cp_lexer_peek_token (parser->lexer);
5753 if (scope
5754 && token->type == CPP_NAME
5755 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5756 != CPP_LESS)
5757 && (token->u.value == TYPE_IDENTIFIER (scope)
5758 || (CLASS_TYPE_P (scope)
5759 && constructor_name_p (token->u.value, scope))))
5761 cp_lexer_consume_token (parser->lexer);
5762 return build_nt (BIT_NOT_EXPR, scope);
5765 /* ~auto means the destructor of whatever the object is. */
5766 if (cp_parser_is_keyword (token, RID_AUTO))
5768 if (cxx_dialect < cxx14)
5769 pedwarn (input_location, 0,
5770 "%<~auto%> only available with "
5771 "-std=c++14 or -std=gnu++14");
5772 cp_lexer_consume_token (parser->lexer);
5773 return build_nt (BIT_NOT_EXPR, make_auto ());
5776 /* If there was an explicit qualification (S::~T), first look
5777 in the scope given by the qualification (i.e., S).
5779 Note: in the calls to cp_parser_class_name below we pass
5780 typename_type so that lookup finds the injected-class-name
5781 rather than the constructor. */
5782 done = false;
5783 type_decl = NULL_TREE;
5784 if (scope)
5786 cp_parser_parse_tentatively (parser);
5787 type_decl = cp_parser_class_name (parser,
5788 /*typename_keyword_p=*/false,
5789 /*template_keyword_p=*/false,
5790 typename_type,
5791 /*check_dependency=*/false,
5792 /*class_head_p=*/false,
5793 declarator_p);
5794 if (cp_parser_parse_definitely (parser))
5795 done = true;
5797 /* In "N::S::~S", look in "N" as well. */
5798 if (!done && scope && qualifying_scope)
5800 cp_parser_parse_tentatively (parser);
5801 parser->scope = qualifying_scope;
5802 parser->object_scope = NULL_TREE;
5803 parser->qualifying_scope = NULL_TREE;
5804 type_decl
5805 = cp_parser_class_name (parser,
5806 /*typename_keyword_p=*/false,
5807 /*template_keyword_p=*/false,
5808 typename_type,
5809 /*check_dependency=*/false,
5810 /*class_head_p=*/false,
5811 declarator_p);
5812 if (cp_parser_parse_definitely (parser))
5813 done = true;
5815 /* In "p->S::~T", look in the scope given by "*p" as well. */
5816 else if (!done && object_scope)
5818 cp_parser_parse_tentatively (parser);
5819 parser->scope = object_scope;
5820 parser->object_scope = NULL_TREE;
5821 parser->qualifying_scope = NULL_TREE;
5822 type_decl
5823 = cp_parser_class_name (parser,
5824 /*typename_keyword_p=*/false,
5825 /*template_keyword_p=*/false,
5826 typename_type,
5827 /*check_dependency=*/false,
5828 /*class_head_p=*/false,
5829 declarator_p);
5830 if (cp_parser_parse_definitely (parser))
5831 done = true;
5833 /* Look in the surrounding context. */
5834 if (!done)
5836 parser->scope = NULL_TREE;
5837 parser->object_scope = NULL_TREE;
5838 parser->qualifying_scope = NULL_TREE;
5839 if (processing_template_decl)
5840 cp_parser_parse_tentatively (parser);
5841 type_decl
5842 = cp_parser_class_name (parser,
5843 /*typename_keyword_p=*/false,
5844 /*template_keyword_p=*/false,
5845 typename_type,
5846 /*check_dependency=*/false,
5847 /*class_head_p=*/false,
5848 declarator_p);
5849 if (processing_template_decl
5850 && ! cp_parser_parse_definitely (parser))
5852 /* We couldn't find a type with this name. If we're parsing
5853 tentatively, fail and try something else. */
5854 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5856 cp_parser_simulate_error (parser);
5857 return error_mark_node;
5859 /* Otherwise, accept it and check for a match at instantiation
5860 time. */
5861 type_decl = cp_parser_identifier (parser);
5862 if (type_decl != error_mark_node)
5863 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5864 return type_decl;
5867 /* If an error occurred, assume that the name of the
5868 destructor is the same as the name of the qualifying
5869 class. That allows us to keep parsing after running
5870 into ill-formed destructor names. */
5871 if (type_decl == error_mark_node && scope)
5872 return build_nt (BIT_NOT_EXPR, scope);
5873 else if (type_decl == error_mark_node)
5874 return error_mark_node;
5876 /* Check that destructor name and scope match. */
5877 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5879 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5880 error_at (token->location,
5881 "declaration of %<~%T%> as member of %qT",
5882 type_decl, scope);
5883 cp_parser_simulate_error (parser);
5884 return error_mark_node;
5887 /* [class.dtor]
5889 A typedef-name that names a class shall not be used as the
5890 identifier in the declarator for a destructor declaration. */
5891 if (declarator_p
5892 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5893 && !DECL_SELF_REFERENCE_P (type_decl)
5894 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5895 error_at (token->location,
5896 "typedef-name %qD used as destructor declarator",
5897 type_decl);
5899 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5902 case CPP_KEYWORD:
5903 if (token->keyword == RID_OPERATOR)
5905 cp_expr id;
5907 /* This could be a template-id, so we try that first. */
5908 cp_parser_parse_tentatively (parser);
5909 /* Try a template-id. */
5910 id = cp_parser_template_id (parser, template_keyword_p,
5911 /*check_dependency_p=*/true,
5912 none_type,
5913 declarator_p);
5914 /* If that worked, we're done. */
5915 if (cp_parser_parse_definitely (parser))
5916 return id;
5917 /* We still don't know whether we're looking at an
5918 operator-function-id or a conversion-function-id. */
5919 cp_parser_parse_tentatively (parser);
5920 /* Try an operator-function-id. */
5921 id = cp_parser_operator_function_id (parser);
5922 /* If that didn't work, try a conversion-function-id. */
5923 if (!cp_parser_parse_definitely (parser))
5924 id = cp_parser_conversion_function_id (parser);
5925 else if (UDLIT_OPER_P (id))
5927 /* 17.6.3.3.5 */
5928 const char *name = UDLIT_OP_SUFFIX (id);
5929 if (name[0] != '_' && !in_system_header_at (input_location)
5930 && declarator_p)
5931 warning (OPT_Wliteral_suffix,
5932 "literal operator suffixes not preceded by %<_%>"
5933 " are reserved for future standardization");
5936 return id;
5938 /* Fall through. */
5940 default:
5941 if (optional_p)
5942 return NULL_TREE;
5943 cp_parser_error (parser, "expected unqualified-id");
5944 return error_mark_node;
5948 /* Parse an (optional) nested-name-specifier.
5950 nested-name-specifier: [C++98]
5951 class-or-namespace-name :: nested-name-specifier [opt]
5952 class-or-namespace-name :: template nested-name-specifier [opt]
5954 nested-name-specifier: [C++0x]
5955 type-name ::
5956 namespace-name ::
5957 nested-name-specifier identifier ::
5958 nested-name-specifier template [opt] simple-template-id ::
5960 PARSER->SCOPE should be set appropriately before this function is
5961 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5962 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5963 in name lookups.
5965 Sets PARSER->SCOPE to the class (TYPE) or namespace
5966 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5967 it unchanged if there is no nested-name-specifier. Returns the new
5968 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5970 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5971 part of a declaration and/or decl-specifier. */
5973 static tree
5974 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5975 bool typename_keyword_p,
5976 bool check_dependency_p,
5977 bool type_p,
5978 bool is_declaration,
5979 bool template_keyword_p /* = false */)
5981 bool success = false;
5982 cp_token_position start = 0;
5983 cp_token *token;
5985 /* Remember where the nested-name-specifier starts. */
5986 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5988 start = cp_lexer_token_position (parser->lexer, false);
5989 push_deferring_access_checks (dk_deferred);
5992 while (true)
5994 tree new_scope;
5995 tree old_scope;
5996 tree saved_qualifying_scope;
5998 /* Spot cases that cannot be the beginning of a
5999 nested-name-specifier. */
6000 token = cp_lexer_peek_token (parser->lexer);
6002 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
6003 the already parsed nested-name-specifier. */
6004 if (token->type == CPP_NESTED_NAME_SPECIFIER)
6006 /* Grab the nested-name-specifier and continue the loop. */
6007 cp_parser_pre_parsed_nested_name_specifier (parser);
6008 /* If we originally encountered this nested-name-specifier
6009 with IS_DECLARATION set to false, we will not have
6010 resolved TYPENAME_TYPEs, so we must do so here. */
6011 if (is_declaration
6012 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6014 new_scope = resolve_typename_type (parser->scope,
6015 /*only_current_p=*/false);
6016 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
6017 parser->scope = new_scope;
6019 success = true;
6020 continue;
6023 /* Spot cases that cannot be the beginning of a
6024 nested-name-specifier. On the second and subsequent times
6025 through the loop, we look for the `template' keyword. */
6026 if (success && token->keyword == RID_TEMPLATE)
6028 /* A template-id can start a nested-name-specifier. */
6029 else if (token->type == CPP_TEMPLATE_ID)
6031 /* DR 743: decltype can be used in a nested-name-specifier. */
6032 else if (token_is_decltype (token))
6034 else
6036 /* If the next token is not an identifier, then it is
6037 definitely not a type-name or namespace-name. */
6038 if (token->type != CPP_NAME)
6039 break;
6040 /* If the following token is neither a `<' (to begin a
6041 template-id), nor a `::', then we are not looking at a
6042 nested-name-specifier. */
6043 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6045 if (token->type == CPP_COLON
6046 && parser->colon_corrects_to_scope_p
6047 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
6049 gcc_rich_location richloc (token->location);
6050 richloc.add_fixit_replace ("::");
6051 error_at_rich_loc (&richloc,
6052 "found %<:%> in nested-name-specifier, "
6053 "expected %<::%>");
6054 token->type = CPP_SCOPE;
6057 if (token->type != CPP_SCOPE
6058 && !cp_parser_nth_token_starts_template_argument_list_p
6059 (parser, 2))
6060 break;
6063 /* The nested-name-specifier is optional, so we parse
6064 tentatively. */
6065 cp_parser_parse_tentatively (parser);
6067 /* Look for the optional `template' keyword, if this isn't the
6068 first time through the loop. */
6069 if (success)
6070 template_keyword_p = cp_parser_optional_template_keyword (parser);
6072 /* Save the old scope since the name lookup we are about to do
6073 might destroy it. */
6074 old_scope = parser->scope;
6075 saved_qualifying_scope = parser->qualifying_scope;
6076 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
6077 look up names in "X<T>::I" in order to determine that "Y" is
6078 a template. So, if we have a typename at this point, we make
6079 an effort to look through it. */
6080 if (is_declaration
6081 && !typename_keyword_p
6082 && parser->scope
6083 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
6084 parser->scope = resolve_typename_type (parser->scope,
6085 /*only_current_p=*/false);
6086 /* Parse the qualifying entity. */
6087 new_scope
6088 = cp_parser_qualifying_entity (parser,
6089 typename_keyword_p,
6090 template_keyword_p,
6091 check_dependency_p,
6092 type_p,
6093 is_declaration);
6094 /* Look for the `::' token. */
6095 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6097 /* If we found what we wanted, we keep going; otherwise, we're
6098 done. */
6099 if (!cp_parser_parse_definitely (parser))
6101 bool error_p = false;
6103 /* Restore the OLD_SCOPE since it was valid before the
6104 failed attempt at finding the last
6105 class-or-namespace-name. */
6106 parser->scope = old_scope;
6107 parser->qualifying_scope = saved_qualifying_scope;
6109 /* If the next token is a decltype, and the one after that is a
6110 `::', then the decltype has failed to resolve to a class or
6111 enumeration type. Give this error even when parsing
6112 tentatively since it can't possibly be valid--and we're going
6113 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6114 won't get another chance.*/
6115 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6116 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6117 == CPP_SCOPE))
6119 token = cp_lexer_consume_token (parser->lexer);
6120 error_at (token->location, "decltype evaluates to %qT, "
6121 "which is not a class or enumeration type",
6122 token->u.tree_check_value->value);
6123 parser->scope = error_mark_node;
6124 error_p = true;
6125 /* As below. */
6126 success = true;
6127 cp_lexer_consume_token (parser->lexer);
6130 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6131 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6133 /* If we have a non-type template-id followed by ::, it can't
6134 possibly be valid. */
6135 token = cp_lexer_peek_token (parser->lexer);
6136 tree tid = token->u.tree_check_value->value;
6137 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6138 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6140 tree tmpl = NULL_TREE;
6141 if (is_overloaded_fn (tid))
6143 tree fns = get_fns (tid);
6144 if (OVL_SINGLE_P (fns))
6145 tmpl = OVL_FIRST (fns);
6146 error_at (token->location, "function template-id %qD "
6147 "in nested-name-specifier", tid);
6149 else
6151 /* Variable template. */
6152 tmpl = TREE_OPERAND (tid, 0);
6153 gcc_assert (variable_template_p (tmpl));
6154 error_at (token->location, "variable template-id %qD "
6155 "in nested-name-specifier", tid);
6157 if (tmpl)
6158 inform (DECL_SOURCE_LOCATION (tmpl),
6159 "%qD declared here", tmpl);
6161 parser->scope = error_mark_node;
6162 error_p = true;
6163 /* As below. */
6164 success = true;
6165 cp_lexer_consume_token (parser->lexer);
6166 cp_lexer_consume_token (parser->lexer);
6170 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6171 break;
6172 /* If the next token is an identifier, and the one after
6173 that is a `::', then any valid interpretation would have
6174 found a class-or-namespace-name. */
6175 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6176 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6177 == CPP_SCOPE)
6178 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6179 != CPP_COMPL))
6181 token = cp_lexer_consume_token (parser->lexer);
6182 if (!error_p)
6184 if (!token->error_reported)
6186 tree decl;
6187 tree ambiguous_decls;
6189 decl = cp_parser_lookup_name (parser, token->u.value,
6190 none_type,
6191 /*is_template=*/false,
6192 /*is_namespace=*/false,
6193 /*check_dependency=*/true,
6194 &ambiguous_decls,
6195 token->location);
6196 if (TREE_CODE (decl) == TEMPLATE_DECL)
6197 error_at (token->location,
6198 "%qD used without template parameters",
6199 decl);
6200 else if (ambiguous_decls)
6202 // cp_parser_lookup_name has the same diagnostic,
6203 // thus make sure to emit it at most once.
6204 if (cp_parser_uncommitted_to_tentative_parse_p
6205 (parser))
6207 error_at (token->location,
6208 "reference to %qD is ambiguous",
6209 token->u.value);
6210 print_candidates (ambiguous_decls);
6212 decl = error_mark_node;
6214 else
6216 if (cxx_dialect != cxx98)
6217 cp_parser_name_lookup_error
6218 (parser, token->u.value, decl, NLE_NOT_CXX98,
6219 token->location);
6220 else
6221 cp_parser_name_lookup_error
6222 (parser, token->u.value, decl, NLE_CXX98,
6223 token->location);
6226 parser->scope = error_mark_node;
6227 error_p = true;
6228 /* Treat this as a successful nested-name-specifier
6229 due to:
6231 [basic.lookup.qual]
6233 If the name found is not a class-name (clause
6234 _class_) or namespace-name (_namespace.def_), the
6235 program is ill-formed. */
6236 success = true;
6238 cp_lexer_consume_token (parser->lexer);
6240 break;
6242 /* We've found one valid nested-name-specifier. */
6243 success = true;
6244 /* Name lookup always gives us a DECL. */
6245 if (TREE_CODE (new_scope) == TYPE_DECL)
6246 new_scope = TREE_TYPE (new_scope);
6247 /* Uses of "template" must be followed by actual templates. */
6248 if (template_keyword_p
6249 && !(CLASS_TYPE_P (new_scope)
6250 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6251 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6252 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6253 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6254 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6255 == TEMPLATE_ID_EXPR)))
6256 permerror (input_location, TYPE_P (new_scope)
6257 ? G_("%qT is not a template")
6258 : G_("%qD is not a template"),
6259 new_scope);
6260 /* If it is a class scope, try to complete it; we are about to
6261 be looking up names inside the class. */
6262 if (TYPE_P (new_scope)
6263 /* Since checking types for dependency can be expensive,
6264 avoid doing it if the type is already complete. */
6265 && !COMPLETE_TYPE_P (new_scope)
6266 /* Do not try to complete dependent types. */
6267 && !dependent_type_p (new_scope))
6269 new_scope = complete_type (new_scope);
6270 /* If it is a typedef to current class, use the current
6271 class instead, as the typedef won't have any names inside
6272 it yet. */
6273 if (!COMPLETE_TYPE_P (new_scope)
6274 && currently_open_class (new_scope))
6275 new_scope = TYPE_MAIN_VARIANT (new_scope);
6277 /* Make sure we look in the right scope the next time through
6278 the loop. */
6279 parser->scope = new_scope;
6282 /* If parsing tentatively, replace the sequence of tokens that makes
6283 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6284 token. That way, should we re-parse the token stream, we will
6285 not have to repeat the effort required to do the parse, nor will
6286 we issue duplicate error messages. */
6287 if (success && start)
6289 cp_token *token;
6291 token = cp_lexer_token_at (parser->lexer, start);
6292 /* Reset the contents of the START token. */
6293 token->type = CPP_NESTED_NAME_SPECIFIER;
6294 /* Retrieve any deferred checks. Do not pop this access checks yet
6295 so the memory will not be reclaimed during token replacing below. */
6296 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6297 token->u.tree_check_value->value = parser->scope;
6298 token->u.tree_check_value->checks = get_deferred_access_checks ();
6299 token->u.tree_check_value->qualifying_scope =
6300 parser->qualifying_scope;
6301 token->keyword = RID_MAX;
6303 /* Purge all subsequent tokens. */
6304 cp_lexer_purge_tokens_after (parser->lexer, start);
6307 if (start)
6308 pop_to_parent_deferring_access_checks ();
6310 return success ? parser->scope : NULL_TREE;
6313 /* Parse a nested-name-specifier. See
6314 cp_parser_nested_name_specifier_opt for details. This function
6315 behaves identically, except that it will an issue an error if no
6316 nested-name-specifier is present. */
6318 static tree
6319 cp_parser_nested_name_specifier (cp_parser *parser,
6320 bool typename_keyword_p,
6321 bool check_dependency_p,
6322 bool type_p,
6323 bool is_declaration)
6325 tree scope;
6327 /* Look for the nested-name-specifier. */
6328 scope = cp_parser_nested_name_specifier_opt (parser,
6329 typename_keyword_p,
6330 check_dependency_p,
6331 type_p,
6332 is_declaration);
6333 /* If it was not present, issue an error message. */
6334 if (!scope)
6336 cp_parser_error (parser, "expected nested-name-specifier");
6337 parser->scope = NULL_TREE;
6340 return scope;
6343 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6344 this is either a class-name or a namespace-name (which corresponds
6345 to the class-or-namespace-name production in the grammar). For
6346 C++0x, it can also be a type-name that refers to an enumeration
6347 type or a simple-template-id.
6349 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6350 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6351 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6352 TYPE_P is TRUE iff the next name should be taken as a class-name,
6353 even the same name is declared to be another entity in the same
6354 scope.
6356 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6357 specified by the class-or-namespace-name. If neither is found the
6358 ERROR_MARK_NODE is returned. */
6360 static tree
6361 cp_parser_qualifying_entity (cp_parser *parser,
6362 bool typename_keyword_p,
6363 bool template_keyword_p,
6364 bool check_dependency_p,
6365 bool type_p,
6366 bool is_declaration)
6368 tree saved_scope;
6369 tree saved_qualifying_scope;
6370 tree saved_object_scope;
6371 tree scope;
6372 bool only_class_p;
6373 bool successful_parse_p;
6375 /* DR 743: decltype can appear in a nested-name-specifier. */
6376 if (cp_lexer_next_token_is_decltype (parser->lexer))
6378 scope = cp_parser_decltype (parser);
6379 if (TREE_CODE (scope) != ENUMERAL_TYPE
6380 && !MAYBE_CLASS_TYPE_P (scope))
6382 cp_parser_simulate_error (parser);
6383 return error_mark_node;
6385 if (TYPE_NAME (scope))
6386 scope = TYPE_NAME (scope);
6387 return scope;
6390 /* Before we try to parse the class-name, we must save away the
6391 current PARSER->SCOPE since cp_parser_class_name will destroy
6392 it. */
6393 saved_scope = parser->scope;
6394 saved_qualifying_scope = parser->qualifying_scope;
6395 saved_object_scope = parser->object_scope;
6396 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6397 there is no need to look for a namespace-name. */
6398 only_class_p = template_keyword_p
6399 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6400 if (!only_class_p)
6401 cp_parser_parse_tentatively (parser);
6402 scope = cp_parser_class_name (parser,
6403 typename_keyword_p,
6404 template_keyword_p,
6405 type_p ? class_type : none_type,
6406 check_dependency_p,
6407 /*class_head_p=*/false,
6408 is_declaration,
6409 /*enum_ok=*/cxx_dialect > cxx98);
6410 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6411 /* If that didn't work, try for a namespace-name. */
6412 if (!only_class_p && !successful_parse_p)
6414 /* Restore the saved scope. */
6415 parser->scope = saved_scope;
6416 parser->qualifying_scope = saved_qualifying_scope;
6417 parser->object_scope = saved_object_scope;
6418 /* If we are not looking at an identifier followed by the scope
6419 resolution operator, then this is not part of a
6420 nested-name-specifier. (Note that this function is only used
6421 to parse the components of a nested-name-specifier.) */
6422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6423 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6424 return error_mark_node;
6425 scope = cp_parser_namespace_name (parser);
6428 return scope;
6431 /* Return true if we are looking at a compound-literal, false otherwise. */
6433 static bool
6434 cp_parser_compound_literal_p (cp_parser *parser)
6436 cp_lexer_save_tokens (parser->lexer);
6438 /* Skip tokens until the next token is a closing parenthesis.
6439 If we find the closing `)', and the next token is a `{', then
6440 we are looking at a compound-literal. */
6441 bool compound_literal_p
6442 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6443 /*consume_paren=*/true)
6444 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6446 /* Roll back the tokens we skipped. */
6447 cp_lexer_rollback_tokens (parser->lexer);
6449 return compound_literal_p;
6452 /* Parse a postfix-expression.
6454 postfix-expression:
6455 primary-expression
6456 postfix-expression [ expression ]
6457 postfix-expression ( expression-list [opt] )
6458 simple-type-specifier ( expression-list [opt] )
6459 typename :: [opt] nested-name-specifier identifier
6460 ( expression-list [opt] )
6461 typename :: [opt] nested-name-specifier template [opt] template-id
6462 ( expression-list [opt] )
6463 postfix-expression . template [opt] id-expression
6464 postfix-expression -> template [opt] id-expression
6465 postfix-expression . pseudo-destructor-name
6466 postfix-expression -> pseudo-destructor-name
6467 postfix-expression ++
6468 postfix-expression --
6469 dynamic_cast < type-id > ( expression )
6470 static_cast < type-id > ( expression )
6471 reinterpret_cast < type-id > ( expression )
6472 const_cast < type-id > ( expression )
6473 typeid ( expression )
6474 typeid ( type-id )
6476 GNU Extension:
6478 postfix-expression:
6479 ( type-id ) { initializer-list , [opt] }
6481 This extension is a GNU version of the C99 compound-literal
6482 construct. (The C99 grammar uses `type-name' instead of `type-id',
6483 but they are essentially the same concept.)
6485 If ADDRESS_P is true, the postfix expression is the operand of the
6486 `&' operator. CAST_P is true if this expression is the target of a
6487 cast.
6489 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6490 class member access expressions [expr.ref].
6492 Returns a representation of the expression. */
6494 static cp_expr
6495 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6496 bool member_access_only_p, bool decltype_p,
6497 cp_id_kind * pidk_return)
6499 cp_token *token;
6500 location_t loc;
6501 enum rid keyword;
6502 cp_id_kind idk = CP_ID_KIND_NONE;
6503 cp_expr postfix_expression = NULL_TREE;
6504 bool is_member_access = false;
6505 int saved_in_statement = -1;
6507 /* Peek at the next token. */
6508 token = cp_lexer_peek_token (parser->lexer);
6509 loc = token->location;
6510 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6512 /* Some of the productions are determined by keywords. */
6513 keyword = token->keyword;
6514 switch (keyword)
6516 case RID_DYNCAST:
6517 case RID_STATCAST:
6518 case RID_REINTCAST:
6519 case RID_CONSTCAST:
6521 tree type;
6522 cp_expr expression;
6523 const char *saved_message;
6524 bool saved_in_type_id_in_expr_p;
6526 /* All of these can be handled in the same way from the point
6527 of view of parsing. Begin by consuming the token
6528 identifying the cast. */
6529 cp_lexer_consume_token (parser->lexer);
6531 /* New types cannot be defined in the cast. */
6532 saved_message = parser->type_definition_forbidden_message;
6533 parser->type_definition_forbidden_message
6534 = G_("types may not be defined in casts");
6536 /* Look for the opening `<'. */
6537 cp_parser_require (parser, CPP_LESS, RT_LESS);
6538 /* Parse the type to which we are casting. */
6539 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6540 parser->in_type_id_in_expr_p = true;
6541 type = cp_parser_type_id (parser);
6542 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6543 /* Look for the closing `>'. */
6544 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6545 /* Restore the old message. */
6546 parser->type_definition_forbidden_message = saved_message;
6548 bool saved_greater_than_is_operator_p
6549 = parser->greater_than_is_operator_p;
6550 parser->greater_than_is_operator_p = true;
6552 /* And the expression which is being cast. */
6553 matching_parens parens;
6554 parens.require_open (parser);
6555 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6556 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6557 RT_CLOSE_PAREN);
6558 location_t end_loc = close_paren ?
6559 close_paren->location : UNKNOWN_LOCATION;
6561 parser->greater_than_is_operator_p
6562 = saved_greater_than_is_operator_p;
6564 /* Only type conversions to integral or enumeration types
6565 can be used in constant-expressions. */
6566 if (!cast_valid_in_integral_constant_expression_p (type)
6567 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6569 postfix_expression = error_mark_node;
6570 break;
6573 switch (keyword)
6575 case RID_DYNCAST:
6576 postfix_expression
6577 = build_dynamic_cast (type, expression, tf_warning_or_error);
6578 break;
6579 case RID_STATCAST:
6580 postfix_expression
6581 = build_static_cast (type, expression, tf_warning_or_error);
6582 break;
6583 case RID_REINTCAST:
6584 postfix_expression
6585 = build_reinterpret_cast (type, expression,
6586 tf_warning_or_error);
6587 break;
6588 case RID_CONSTCAST:
6589 postfix_expression
6590 = build_const_cast (type, expression, tf_warning_or_error);
6591 break;
6592 default:
6593 gcc_unreachable ();
6596 /* Construct a location e.g. :
6597 reinterpret_cast <int *> (expr)
6598 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6599 ranging from the start of the "*_cast" token to the final closing
6600 paren, with the caret at the start. */
6601 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6602 postfix_expression.set_location (cp_cast_loc);
6604 break;
6606 case RID_TYPEID:
6608 tree type;
6609 const char *saved_message;
6610 bool saved_in_type_id_in_expr_p;
6612 /* Consume the `typeid' token. */
6613 cp_lexer_consume_token (parser->lexer);
6614 /* Look for the `(' token. */
6615 matching_parens parens;
6616 parens.require_open (parser);
6617 /* Types cannot be defined in a `typeid' expression. */
6618 saved_message = parser->type_definition_forbidden_message;
6619 parser->type_definition_forbidden_message
6620 = G_("types may not be defined in a %<typeid%> expression");
6621 /* We can't be sure yet whether we're looking at a type-id or an
6622 expression. */
6623 cp_parser_parse_tentatively (parser);
6624 /* Try a type-id first. */
6625 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6626 parser->in_type_id_in_expr_p = true;
6627 type = cp_parser_type_id (parser);
6628 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6629 /* Look for the `)' token. Otherwise, we can't be sure that
6630 we're not looking at an expression: consider `typeid (int
6631 (3))', for example. */
6632 cp_token *close_paren = parens.require_close (parser);
6633 /* If all went well, simply lookup the type-id. */
6634 if (cp_parser_parse_definitely (parser))
6635 postfix_expression = get_typeid (type, tf_warning_or_error);
6636 /* Otherwise, fall back to the expression variant. */
6637 else
6639 tree expression;
6641 /* Look for an expression. */
6642 expression = cp_parser_expression (parser, & idk);
6643 /* Compute its typeid. */
6644 postfix_expression = build_typeid (expression, tf_warning_or_error);
6645 /* Look for the `)' token. */
6646 close_paren = parens.require_close (parser);
6648 /* Restore the saved message. */
6649 parser->type_definition_forbidden_message = saved_message;
6650 /* `typeid' may not appear in an integral constant expression. */
6651 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6652 postfix_expression = error_mark_node;
6654 /* Construct a location e.g. :
6655 typeid (expr)
6656 ^~~~~~~~~~~~~
6657 ranging from the start of the "typeid" token to the final closing
6658 paren, with the caret at the start. */
6659 if (close_paren)
6661 location_t typeid_loc
6662 = make_location (start_loc, start_loc, close_paren->location);
6663 postfix_expression.set_location (typeid_loc);
6666 break;
6668 case RID_TYPENAME:
6670 tree type;
6671 /* The syntax permitted here is the same permitted for an
6672 elaborated-type-specifier. */
6673 ++parser->prevent_constrained_type_specifiers;
6674 type = cp_parser_elaborated_type_specifier (parser,
6675 /*is_friend=*/false,
6676 /*is_declaration=*/false);
6677 --parser->prevent_constrained_type_specifiers;
6678 postfix_expression = cp_parser_functional_cast (parser, type);
6680 break;
6682 case RID_CILK_SPAWN:
6684 location_t cilk_spawn_loc
6685 = cp_lexer_peek_token (parser->lexer)->location;
6686 cp_lexer_consume_token (parser->lexer);
6687 token = cp_lexer_peek_token (parser->lexer);
6688 if (token->type == CPP_SEMICOLON)
6690 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6691 "an expression");
6692 postfix_expression = error_mark_node;
6693 break;
6695 else if (!current_function_decl)
6697 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6698 "inside a function");
6699 postfix_expression = error_mark_node;
6700 break;
6702 else
6704 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6705 saved_in_statement = parser->in_statement;
6706 parser->in_statement |= IN_CILK_SPAWN;
6708 cfun->calls_cilk_spawn = 1;
6709 postfix_expression =
6710 cp_parser_postfix_expression (parser, false, false,
6711 false, false, &idk);
6712 if (!flag_cilkplus)
6714 error_at (token->location, "-fcilkplus must be enabled to use"
6715 " %<_Cilk_spawn%>");
6716 cfun->calls_cilk_spawn = 0;
6718 else if (saved_in_statement & IN_CILK_SPAWN)
6720 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6721 "are not permitted");
6722 postfix_expression = error_mark_node;
6723 cfun->calls_cilk_spawn = 0;
6725 else
6727 location_t loc = postfix_expression.get_location ();
6728 postfix_expression = build_cilk_spawn (token->location,
6729 postfix_expression);
6730 /* Build a location of the form:
6731 _Cilk_spawn expr
6732 ~~~~~~~~~~~~^~~~
6733 with caret at the expr, ranging from the start of the
6734 _Cilk_spawn token to the end of the expression. */
6735 location_t combined_loc =
6736 make_location (loc, cilk_spawn_loc, get_finish (loc));
6737 postfix_expression.set_location (combined_loc);
6738 if (postfix_expression != error_mark_node)
6739 SET_EXPR_LOCATION (postfix_expression, input_location);
6740 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6742 break;
6745 case RID_ADDRESSOF:
6746 case RID_BUILTIN_SHUFFLE:
6747 case RID_BUILTIN_LAUNDER:
6749 vec<tree, va_gc> *vec;
6750 unsigned int i;
6751 tree p;
6753 cp_lexer_consume_token (parser->lexer);
6754 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6755 /*cast_p=*/false, /*allow_expansion_p=*/true,
6756 /*non_constant_p=*/NULL);
6757 if (vec == NULL)
6759 postfix_expression = error_mark_node;
6760 break;
6763 FOR_EACH_VEC_ELT (*vec, i, p)
6764 mark_exp_read (p);
6766 switch (keyword)
6768 case RID_ADDRESSOF:
6769 if (vec->length () == 1)
6770 postfix_expression
6771 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6772 else
6774 error_at (loc, "wrong number of arguments to "
6775 "%<__builtin_addressof%>");
6776 postfix_expression = error_mark_node;
6778 break;
6780 case RID_BUILTIN_LAUNDER:
6781 if (vec->length () == 1)
6782 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6783 tf_warning_or_error);
6784 else
6786 error_at (loc, "wrong number of arguments to "
6787 "%<__builtin_launder%>");
6788 postfix_expression = error_mark_node;
6790 break;
6792 case RID_BUILTIN_SHUFFLE:
6793 if (vec->length () == 2)
6794 postfix_expression
6795 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6796 (*vec)[1], tf_warning_or_error);
6797 else if (vec->length () == 3)
6798 postfix_expression
6799 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6800 (*vec)[2], tf_warning_or_error);
6801 else
6803 error_at (loc, "wrong number of arguments to "
6804 "%<__builtin_shuffle%>");
6805 postfix_expression = error_mark_node;
6807 break;
6809 default:
6810 gcc_unreachable ();
6812 break;
6815 default:
6817 tree type;
6819 /* If the next thing is a simple-type-specifier, we may be
6820 looking at a functional cast. We could also be looking at
6821 an id-expression. So, we try the functional cast, and if
6822 that doesn't work we fall back to the primary-expression. */
6823 cp_parser_parse_tentatively (parser);
6824 /* Look for the simple-type-specifier. */
6825 ++parser->prevent_constrained_type_specifiers;
6826 type = cp_parser_simple_type_specifier (parser,
6827 /*decl_specs=*/NULL,
6828 CP_PARSER_FLAGS_NONE);
6829 --parser->prevent_constrained_type_specifiers;
6830 /* Parse the cast itself. */
6831 if (!cp_parser_error_occurred (parser))
6832 postfix_expression
6833 = cp_parser_functional_cast (parser, type);
6834 /* If that worked, we're done. */
6835 if (cp_parser_parse_definitely (parser))
6836 break;
6838 /* If the functional-cast didn't work out, try a
6839 compound-literal. */
6840 if (cp_parser_allow_gnu_extensions_p (parser)
6841 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6843 cp_expr initializer = NULL_TREE;
6845 cp_parser_parse_tentatively (parser);
6847 matching_parens parens;
6848 parens.consume_open (parser);
6850 /* Avoid calling cp_parser_type_id pointlessly, see comment
6851 in cp_parser_cast_expression about c++/29234. */
6852 if (!cp_parser_compound_literal_p (parser))
6853 cp_parser_simulate_error (parser);
6854 else
6856 /* Parse the type. */
6857 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6858 parser->in_type_id_in_expr_p = true;
6859 type = cp_parser_type_id (parser);
6860 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6861 parens.require_close (parser);
6864 /* If things aren't going well, there's no need to
6865 keep going. */
6866 if (!cp_parser_error_occurred (parser))
6868 bool non_constant_p;
6869 /* Parse the brace-enclosed initializer list. */
6870 initializer = cp_parser_braced_list (parser,
6871 &non_constant_p);
6873 /* If that worked, we're definitely looking at a
6874 compound-literal expression. */
6875 if (cp_parser_parse_definitely (parser))
6877 /* Warn the user that a compound literal is not
6878 allowed in standard C++. */
6879 pedwarn (input_location, OPT_Wpedantic,
6880 "ISO C++ forbids compound-literals");
6881 /* For simplicity, we disallow compound literals in
6882 constant-expressions. We could
6883 allow compound literals of integer type, whose
6884 initializer was a constant, in constant
6885 expressions. Permitting that usage, as a further
6886 extension, would not change the meaning of any
6887 currently accepted programs. (Of course, as
6888 compound literals are not part of ISO C++, the
6889 standard has nothing to say.) */
6890 if (cp_parser_non_integral_constant_expression (parser,
6891 NIC_NCC))
6893 postfix_expression = error_mark_node;
6894 break;
6896 /* Form the representation of the compound-literal. */
6897 postfix_expression
6898 = finish_compound_literal (type, initializer,
6899 tf_warning_or_error, fcl_c99);
6900 postfix_expression.set_location (initializer.get_location ());
6901 break;
6905 /* It must be a primary-expression. */
6906 postfix_expression
6907 = cp_parser_primary_expression (parser, address_p, cast_p,
6908 /*template_arg_p=*/false,
6909 decltype_p,
6910 &idk);
6912 break;
6915 /* Note that we don't need to worry about calling build_cplus_new on a
6916 class-valued CALL_EXPR in decltype when it isn't the end of the
6917 postfix-expression; unary_complex_lvalue will take care of that for
6918 all these cases. */
6920 /* Keep looping until the postfix-expression is complete. */
6921 while (true)
6923 if (idk == CP_ID_KIND_UNQUALIFIED
6924 && identifier_p (postfix_expression)
6925 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6926 /* It is not a Koenig lookup function call. */
6927 postfix_expression
6928 = unqualified_name_lookup_error (postfix_expression);
6930 /* Peek at the next token. */
6931 token = cp_lexer_peek_token (parser->lexer);
6933 switch (token->type)
6935 case CPP_OPEN_SQUARE:
6936 if (cp_next_tokens_can_be_std_attribute_p (parser))
6938 cp_parser_error (parser,
6939 "two consecutive %<[%> shall "
6940 "only introduce an attribute");
6941 return error_mark_node;
6943 postfix_expression
6944 = cp_parser_postfix_open_square_expression (parser,
6945 postfix_expression,
6946 false,
6947 decltype_p);
6948 postfix_expression.set_range (start_loc,
6949 postfix_expression.get_location ());
6951 idk = CP_ID_KIND_NONE;
6952 is_member_access = false;
6953 break;
6955 case CPP_OPEN_PAREN:
6956 /* postfix-expression ( expression-list [opt] ) */
6958 bool koenig_p;
6959 bool is_builtin_constant_p;
6960 bool saved_integral_constant_expression_p = false;
6961 bool saved_non_integral_constant_expression_p = false;
6962 tsubst_flags_t complain = complain_flags (decltype_p);
6963 vec<tree, va_gc> *args;
6964 location_t close_paren_loc = UNKNOWN_LOCATION;
6966 is_member_access = false;
6968 is_builtin_constant_p
6969 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6970 if (is_builtin_constant_p)
6972 /* The whole point of __builtin_constant_p is to allow
6973 non-constant expressions to appear as arguments. */
6974 saved_integral_constant_expression_p
6975 = parser->integral_constant_expression_p;
6976 saved_non_integral_constant_expression_p
6977 = parser->non_integral_constant_expression_p;
6978 parser->integral_constant_expression_p = false;
6980 args = (cp_parser_parenthesized_expression_list
6981 (parser, non_attr,
6982 /*cast_p=*/false, /*allow_expansion_p=*/true,
6983 /*non_constant_p=*/NULL,
6984 /*close_paren_loc=*/&close_paren_loc));
6985 if (is_builtin_constant_p)
6987 parser->integral_constant_expression_p
6988 = saved_integral_constant_expression_p;
6989 parser->non_integral_constant_expression_p
6990 = saved_non_integral_constant_expression_p;
6993 if (args == NULL)
6995 postfix_expression = error_mark_node;
6996 break;
6999 /* Function calls are not permitted in
7000 constant-expressions. */
7001 if (! builtin_valid_in_constant_expr_p (postfix_expression)
7002 && cp_parser_non_integral_constant_expression (parser,
7003 NIC_FUNC_CALL))
7005 postfix_expression = error_mark_node;
7006 release_tree_vector (args);
7007 break;
7010 koenig_p = false;
7011 if (idk == CP_ID_KIND_UNQUALIFIED
7012 || idk == CP_ID_KIND_TEMPLATE_ID)
7014 if (identifier_p (postfix_expression))
7016 if (!args->is_empty ())
7018 koenig_p = true;
7019 if (!any_type_dependent_arguments_p (args))
7020 postfix_expression
7021 = perform_koenig_lookup (postfix_expression, args,
7022 complain);
7024 else
7025 postfix_expression
7026 = unqualified_fn_lookup_error (postfix_expression);
7028 /* We do not perform argument-dependent lookup if
7029 normal lookup finds a non-function, in accordance
7030 with the expected resolution of DR 218. */
7031 else if (!args->is_empty ()
7032 && is_overloaded_fn (postfix_expression))
7034 tree fn = get_first_fn (postfix_expression);
7035 fn = STRIP_TEMPLATE (fn);
7037 /* Do not do argument dependent lookup if regular
7038 lookup finds a member function or a block-scope
7039 function declaration. [basic.lookup.argdep]/3 */
7040 if (!DECL_FUNCTION_MEMBER_P (fn)
7041 && !DECL_LOCAL_FUNCTION_P (fn))
7043 koenig_p = true;
7044 if (!any_type_dependent_arguments_p (args))
7045 postfix_expression
7046 = perform_koenig_lookup (postfix_expression, args,
7047 complain);
7052 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
7053 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
7054 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
7055 && vec_safe_length (args) == 3)
7057 tree arg0 = (*args)[0];
7058 tree arg1 = (*args)[1];
7059 tree arg2 = (*args)[2];
7060 int literal_mask = ((!!integer_zerop (arg1) << 1)
7061 | (!!integer_zerop (arg2) << 2));
7062 if (TREE_CODE (arg2) == CONST_DECL)
7063 arg2 = DECL_INITIAL (arg2);
7064 warn_for_memset (input_location, arg0, arg2, literal_mask);
7067 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
7069 tree instance = TREE_OPERAND (postfix_expression, 0);
7070 tree fn = TREE_OPERAND (postfix_expression, 1);
7072 if (processing_template_decl
7073 && (type_dependent_object_expression_p (instance)
7074 || (!BASELINK_P (fn)
7075 && TREE_CODE (fn) != FIELD_DECL)
7076 || type_dependent_expression_p (fn)
7077 || any_type_dependent_arguments_p (args)))
7079 maybe_generic_this_capture (instance, fn);
7080 postfix_expression
7081 = build_min_nt_call_vec (postfix_expression, args);
7082 release_tree_vector (args);
7083 break;
7086 if (BASELINK_P (fn))
7088 postfix_expression
7089 = (build_new_method_call
7090 (instance, fn, &args, NULL_TREE,
7091 (idk == CP_ID_KIND_QUALIFIED
7092 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7093 : LOOKUP_NORMAL),
7094 /*fn_p=*/NULL,
7095 complain));
7097 else
7098 postfix_expression
7099 = finish_call_expr (postfix_expression, &args,
7100 /*disallow_virtual=*/false,
7101 /*koenig_p=*/false,
7102 complain);
7104 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7105 || TREE_CODE (postfix_expression) == MEMBER_REF
7106 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7107 postfix_expression = (build_offset_ref_call_from_tree
7108 (postfix_expression, &args,
7109 complain));
7110 else if (idk == CP_ID_KIND_QUALIFIED)
7111 /* A call to a static class member, or a namespace-scope
7112 function. */
7113 postfix_expression
7114 = finish_call_expr (postfix_expression, &args,
7115 /*disallow_virtual=*/true,
7116 koenig_p,
7117 complain);
7118 else
7119 /* All other function calls. */
7120 postfix_expression
7121 = finish_call_expr (postfix_expression, &args,
7122 /*disallow_virtual=*/false,
7123 koenig_p,
7124 complain);
7126 if (close_paren_loc != UNKNOWN_LOCATION)
7128 location_t combined_loc = make_location (token->location,
7129 start_loc,
7130 close_paren_loc);
7131 postfix_expression.set_location (combined_loc);
7134 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7135 idk = CP_ID_KIND_NONE;
7137 release_tree_vector (args);
7139 break;
7141 case CPP_DOT:
7142 case CPP_DEREF:
7143 /* postfix-expression . template [opt] id-expression
7144 postfix-expression . pseudo-destructor-name
7145 postfix-expression -> template [opt] id-expression
7146 postfix-expression -> pseudo-destructor-name */
7148 /* Consume the `.' or `->' operator. */
7149 cp_lexer_consume_token (parser->lexer);
7151 postfix_expression
7152 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7153 postfix_expression,
7154 false, &idk, loc);
7156 is_member_access = true;
7157 break;
7159 case CPP_PLUS_PLUS:
7160 /* postfix-expression ++ */
7161 /* Consume the `++' token. */
7162 cp_lexer_consume_token (parser->lexer);
7163 /* Generate a representation for the complete expression. */
7164 postfix_expression
7165 = finish_increment_expr (postfix_expression,
7166 POSTINCREMENT_EXPR);
7167 /* Increments may not appear in constant-expressions. */
7168 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7169 postfix_expression = error_mark_node;
7170 idk = CP_ID_KIND_NONE;
7171 is_member_access = false;
7172 break;
7174 case CPP_MINUS_MINUS:
7175 /* postfix-expression -- */
7176 /* Consume the `--' token. */
7177 cp_lexer_consume_token (parser->lexer);
7178 /* Generate a representation for the complete expression. */
7179 postfix_expression
7180 = finish_increment_expr (postfix_expression,
7181 POSTDECREMENT_EXPR);
7182 /* Decrements may not appear in constant-expressions. */
7183 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7184 postfix_expression = error_mark_node;
7185 idk = CP_ID_KIND_NONE;
7186 is_member_access = false;
7187 break;
7189 default:
7190 if (pidk_return != NULL)
7191 * pidk_return = idk;
7192 if (member_access_only_p)
7193 return is_member_access
7194 ? postfix_expression
7195 : cp_expr (error_mark_node);
7196 else
7197 return postfix_expression;
7201 /* We should never get here. */
7202 gcc_unreachable ();
7203 return error_mark_node;
7206 /* This function parses Cilk Plus array notations. If a normal array expr. is
7207 parsed then the array index is passed back to the caller through *INIT_INDEX
7208 and the function returns a NULL_TREE. If array notation expr. is parsed,
7209 then *INIT_INDEX is ignored by the caller and the function returns
7210 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7211 error_mark_node. */
7213 static tree
7214 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7215 tree array_value)
7217 cp_token *token = NULL;
7218 tree length_index, stride = NULL_TREE, value_tree, array_type;
7219 if (!array_value || array_value == error_mark_node)
7221 cp_parser_skip_to_end_of_statement (parser);
7222 return error_mark_node;
7225 array_type = TREE_TYPE (array_value);
7227 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7228 parser->colon_corrects_to_scope_p = false;
7229 token = cp_lexer_peek_token (parser->lexer);
7231 if (!token)
7233 cp_parser_error (parser, "expected %<:%> or numeral");
7234 return error_mark_node;
7236 else if (token->type == CPP_COLON)
7238 /* Consume the ':'. */
7239 cp_lexer_consume_token (parser->lexer);
7241 /* If we are here, then we have a case like this A[:]. */
7242 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7244 cp_parser_error (parser, "expected %<]%>");
7245 cp_parser_skip_to_end_of_statement (parser);
7246 return error_mark_node;
7248 *init_index = NULL_TREE;
7249 stride = NULL_TREE;
7250 length_index = NULL_TREE;
7252 else
7254 /* If we are here, then there are three valid possibilities:
7255 1. ARRAY [ EXP ]
7256 2. ARRAY [ EXP : EXP ]
7257 3. ARRAY [ EXP : EXP : EXP ] */
7259 *init_index = cp_parser_expression (parser);
7260 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7262 /* This indicates that we have a normal array expression. */
7263 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7264 return NULL_TREE;
7267 /* Consume the ':'. */
7268 cp_lexer_consume_token (parser->lexer);
7269 length_index = cp_parser_expression (parser);
7270 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7272 cp_lexer_consume_token (parser->lexer);
7273 stride = cp_parser_expression (parser);
7276 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7278 if (*init_index == error_mark_node || length_index == error_mark_node
7279 || stride == error_mark_node || array_type == error_mark_node)
7281 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7282 cp_lexer_consume_token (parser->lexer);
7283 return error_mark_node;
7285 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7287 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7288 length_index, stride, array_type);
7289 return value_tree;
7292 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7293 by cp_parser_builtin_offsetof. We're looking for
7295 postfix-expression [ expression ]
7296 postfix-expression [ braced-init-list ] (C++11)
7298 FOR_OFFSETOF is set if we're being called in that context, which
7299 changes how we deal with integer constant expressions. */
7301 static tree
7302 cp_parser_postfix_open_square_expression (cp_parser *parser,
7303 tree postfix_expression,
7304 bool for_offsetof,
7305 bool decltype_p)
7307 tree index = NULL_TREE;
7308 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7309 bool saved_greater_than_is_operator_p;
7311 /* Consume the `[' token. */
7312 cp_lexer_consume_token (parser->lexer);
7314 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7315 parser->greater_than_is_operator_p = true;
7317 /* Parse the index expression. */
7318 /* ??? For offsetof, there is a question of what to allow here. If
7319 offsetof is not being used in an integral constant expression context,
7320 then we *could* get the right answer by computing the value at runtime.
7321 If we are in an integral constant expression context, then we might
7322 could accept any constant expression; hard to say without analysis.
7323 Rather than open the barn door too wide right away, allow only integer
7324 constant expressions here. */
7325 if (for_offsetof)
7326 index = cp_parser_constant_expression (parser);
7327 else
7329 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7331 bool expr_nonconst_p;
7332 cp_lexer_set_source_position (parser->lexer);
7333 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7334 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7335 if (flag_cilkplus
7336 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7338 error_at (cp_lexer_peek_token (parser->lexer)->location,
7339 "braced list index is not allowed with array "
7340 "notation");
7341 cp_parser_skip_to_end_of_statement (parser);
7342 return error_mark_node;
7345 else if (flag_cilkplus)
7347 /* Here are have these two options:
7348 ARRAY[EXP : EXP] - Array notation expr with default
7349 stride of 1.
7350 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7351 stride. */
7352 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7353 postfix_expression);
7354 if (an_exp)
7355 return an_exp;
7357 else
7358 index = cp_parser_expression (parser);
7361 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7363 /* Look for the closing `]'. */
7364 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7366 /* Build the ARRAY_REF. */
7367 postfix_expression = grok_array_decl (loc, postfix_expression,
7368 index, decltype_p);
7370 /* When not doing offsetof, array references are not permitted in
7371 constant-expressions. */
7372 if (!for_offsetof
7373 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7374 postfix_expression = error_mark_node;
7376 return postfix_expression;
7379 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7380 by cp_parser_builtin_offsetof. We're looking for
7382 postfix-expression . template [opt] id-expression
7383 postfix-expression . pseudo-destructor-name
7384 postfix-expression -> template [opt] id-expression
7385 postfix-expression -> pseudo-destructor-name
7387 FOR_OFFSETOF is set if we're being called in that context. That sorta
7388 limits what of the above we'll actually accept, but nevermind.
7389 TOKEN_TYPE is the "." or "->" token, which will already have been
7390 removed from the stream. */
7392 static tree
7393 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7394 enum cpp_ttype token_type,
7395 cp_expr postfix_expression,
7396 bool for_offsetof, cp_id_kind *idk,
7397 location_t location)
7399 tree name;
7400 bool dependent_p;
7401 bool pseudo_destructor_p;
7402 tree scope = NULL_TREE;
7403 location_t start_loc = postfix_expression.get_start ();
7405 /* If this is a `->' operator, dereference the pointer. */
7406 if (token_type == CPP_DEREF)
7407 postfix_expression = build_x_arrow (location, postfix_expression,
7408 tf_warning_or_error);
7409 /* Check to see whether or not the expression is type-dependent and
7410 not the current instantiation. */
7411 dependent_p = type_dependent_object_expression_p (postfix_expression);
7412 /* The identifier following the `->' or `.' is not qualified. */
7413 parser->scope = NULL_TREE;
7414 parser->qualifying_scope = NULL_TREE;
7415 parser->object_scope = NULL_TREE;
7416 *idk = CP_ID_KIND_NONE;
7418 /* Enter the scope corresponding to the type of the object
7419 given by the POSTFIX_EXPRESSION. */
7420 if (!dependent_p)
7422 scope = TREE_TYPE (postfix_expression);
7423 /* According to the standard, no expression should ever have
7424 reference type. Unfortunately, we do not currently match
7425 the standard in this respect in that our internal representation
7426 of an expression may have reference type even when the standard
7427 says it does not. Therefore, we have to manually obtain the
7428 underlying type here. */
7429 scope = non_reference (scope);
7430 /* The type of the POSTFIX_EXPRESSION must be complete. */
7431 /* Unlike the object expression in other contexts, *this is not
7432 required to be of complete type for purposes of class member
7433 access (5.2.5) outside the member function body. */
7434 if (postfix_expression != current_class_ref
7435 && scope != error_mark_node
7436 && !(processing_template_decl
7437 && current_class_type
7438 && (same_type_ignoring_top_level_qualifiers_p
7439 (scope, current_class_type))))
7441 scope = complete_type (scope);
7442 if (!COMPLETE_TYPE_P (scope)
7443 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7444 && EXPR_P (postfix_expression))
7446 /* In a template, be permissive by treating an object expression
7447 of incomplete type as dependent (after a pedwarn). */
7448 diagnostic_t kind = (processing_template_decl
7449 ? DK_PEDWARN
7450 : DK_ERROR);
7451 cxx_incomplete_type_diagnostic
7452 (location_of (postfix_expression),
7453 postfix_expression, scope, kind);
7454 if (processing_template_decl)
7456 dependent_p = true;
7457 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7462 if (!dependent_p)
7464 /* Let the name lookup machinery know that we are processing a
7465 class member access expression. */
7466 parser->context->object_type = scope;
7467 /* If something went wrong, we want to be able to discern that case,
7468 as opposed to the case where there was no SCOPE due to the type
7469 of expression being dependent. */
7470 if (!scope)
7471 scope = error_mark_node;
7472 /* If the SCOPE was erroneous, make the various semantic analysis
7473 functions exit quickly -- and without issuing additional error
7474 messages. */
7475 if (scope == error_mark_node)
7476 postfix_expression = error_mark_node;
7480 if (dependent_p)
7481 /* Tell cp_parser_lookup_name that there was an object, even though it's
7482 type-dependent. */
7483 parser->context->object_type = unknown_type_node;
7485 /* Assume this expression is not a pseudo-destructor access. */
7486 pseudo_destructor_p = false;
7488 /* If the SCOPE is a scalar type, then, if this is a valid program,
7489 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7490 is type dependent, it can be pseudo-destructor-name or something else.
7491 Try to parse it as pseudo-destructor-name first. */
7492 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7494 tree s;
7495 tree type;
7497 cp_parser_parse_tentatively (parser);
7498 /* Parse the pseudo-destructor-name. */
7499 s = NULL_TREE;
7500 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7501 &s, &type);
7502 if (dependent_p
7503 && (cp_parser_error_occurred (parser)
7504 || !SCALAR_TYPE_P (type)))
7505 cp_parser_abort_tentative_parse (parser);
7506 else if (cp_parser_parse_definitely (parser))
7508 pseudo_destructor_p = true;
7509 postfix_expression
7510 = finish_pseudo_destructor_expr (postfix_expression,
7511 s, type, location);
7515 if (!pseudo_destructor_p)
7517 /* If the SCOPE is not a scalar type, we are looking at an
7518 ordinary class member access expression, rather than a
7519 pseudo-destructor-name. */
7520 bool template_p;
7521 cp_token *token = cp_lexer_peek_token (parser->lexer);
7522 /* Parse the id-expression. */
7523 name = (cp_parser_id_expression
7524 (parser,
7525 cp_parser_optional_template_keyword (parser),
7526 /*check_dependency_p=*/true,
7527 &template_p,
7528 /*declarator_p=*/false,
7529 /*optional_p=*/false));
7530 /* In general, build a SCOPE_REF if the member name is qualified.
7531 However, if the name was not dependent and has already been
7532 resolved; there is no need to build the SCOPE_REF. For example;
7534 struct X { void f(); };
7535 template <typename T> void f(T* t) { t->X::f(); }
7537 Even though "t" is dependent, "X::f" is not and has been resolved
7538 to a BASELINK; there is no need to include scope information. */
7540 /* But we do need to remember that there was an explicit scope for
7541 virtual function calls. */
7542 if (parser->scope)
7543 *idk = CP_ID_KIND_QUALIFIED;
7545 /* If the name is a template-id that names a type, we will get a
7546 TYPE_DECL here. That is invalid code. */
7547 if (TREE_CODE (name) == TYPE_DECL)
7549 error_at (token->location, "invalid use of %qD", name);
7550 postfix_expression = error_mark_node;
7552 else
7554 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7556 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7558 error_at (token->location, "%<%D::%D%> is not a class member",
7559 parser->scope, name);
7560 postfix_expression = error_mark_node;
7562 else
7563 name = build_qualified_name (/*type=*/NULL_TREE,
7564 parser->scope,
7565 name,
7566 template_p);
7567 parser->scope = NULL_TREE;
7568 parser->qualifying_scope = NULL_TREE;
7569 parser->object_scope = NULL_TREE;
7571 if (parser->scope && name && BASELINK_P (name))
7572 adjust_result_of_qualified_name_lookup
7573 (name, parser->scope, scope);
7574 postfix_expression
7575 = finish_class_member_access_expr (postfix_expression, name,
7576 template_p,
7577 tf_warning_or_error);
7578 /* Build a location e.g.:
7579 ptr->access_expr
7580 ~~~^~~~~~~~~~~~~
7581 where the caret is at the deref token, ranging from
7582 the start of postfix_expression to the end of the access expr. */
7583 location_t end_loc
7584 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7585 location_t combined_loc
7586 = make_location (input_location, start_loc, end_loc);
7587 protected_set_expr_location (postfix_expression, combined_loc);
7591 /* We no longer need to look up names in the scope of the object on
7592 the left-hand side of the `.' or `->' operator. */
7593 parser->context->object_type = NULL_TREE;
7595 /* Outside of offsetof, these operators may not appear in
7596 constant-expressions. */
7597 if (!for_offsetof
7598 && (cp_parser_non_integral_constant_expression
7599 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7600 postfix_expression = error_mark_node;
7602 return postfix_expression;
7605 /* Parse a parenthesized expression-list.
7607 expression-list:
7608 assignment-expression
7609 expression-list, assignment-expression
7611 attribute-list:
7612 expression-list
7613 identifier
7614 identifier, expression-list
7616 CAST_P is true if this expression is the target of a cast.
7618 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7619 argument pack.
7621 Returns a vector of trees. Each element is a representation of an
7622 assignment-expression. NULL is returned if the ( and or ) are
7623 missing. An empty, but allocated, vector is returned on no
7624 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7625 if we are parsing an attribute list for an attribute that wants a
7626 plain identifier argument, normal_attr for an attribute that wants
7627 an expression, or non_attr if we aren't parsing an attribute list. If
7628 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7629 not all of the expressions in the list were constant.
7630 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7631 will be written to with the location of the closing parenthesis. If
7632 an error occurs, it may or may not be written to. */
7634 static vec<tree, va_gc> *
7635 cp_parser_parenthesized_expression_list (cp_parser* parser,
7636 int is_attribute_list,
7637 bool cast_p,
7638 bool allow_expansion_p,
7639 bool *non_constant_p,
7640 location_t *close_paren_loc)
7642 vec<tree, va_gc> *expression_list;
7643 bool fold_expr_p = is_attribute_list != non_attr;
7644 tree identifier = NULL_TREE;
7645 bool saved_greater_than_is_operator_p;
7647 /* Assume all the expressions will be constant. */
7648 if (non_constant_p)
7649 *non_constant_p = false;
7651 matching_parens parens;
7652 if (!parens.require_open (parser))
7653 return NULL;
7655 expression_list = make_tree_vector ();
7657 /* Within a parenthesized expression, a `>' token is always
7658 the greater-than operator. */
7659 saved_greater_than_is_operator_p
7660 = parser->greater_than_is_operator_p;
7661 parser->greater_than_is_operator_p = true;
7663 /* Consume expressions until there are no more. */
7664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7665 while (true)
7667 tree expr;
7669 /* At the beginning of attribute lists, check to see if the
7670 next token is an identifier. */
7671 if (is_attribute_list == id_attr
7672 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7674 cp_token *token;
7676 /* Consume the identifier. */
7677 token = cp_lexer_consume_token (parser->lexer);
7678 /* Save the identifier. */
7679 identifier = token->u.value;
7681 else
7683 bool expr_non_constant_p;
7685 /* Parse the next assignment-expression. */
7686 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7688 /* A braced-init-list. */
7689 cp_lexer_set_source_position (parser->lexer);
7690 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7691 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7692 if (non_constant_p && expr_non_constant_p)
7693 *non_constant_p = true;
7695 else if (non_constant_p)
7697 expr = (cp_parser_constant_expression
7698 (parser, /*allow_non_constant_p=*/true,
7699 &expr_non_constant_p));
7700 if (expr_non_constant_p)
7701 *non_constant_p = true;
7703 else
7704 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7705 cast_p);
7707 if (fold_expr_p)
7708 expr = instantiate_non_dependent_expr (expr);
7710 /* If we have an ellipsis, then this is an expression
7711 expansion. */
7712 if (allow_expansion_p
7713 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7715 /* Consume the `...'. */
7716 cp_lexer_consume_token (parser->lexer);
7718 /* Build the argument pack. */
7719 expr = make_pack_expansion (expr);
7722 /* Add it to the list. We add error_mark_node
7723 expressions to the list, so that we can still tell if
7724 the correct form for a parenthesized expression-list
7725 is found. That gives better errors. */
7726 vec_safe_push (expression_list, expr);
7728 if (expr == error_mark_node)
7729 goto skip_comma;
7732 /* After the first item, attribute lists look the same as
7733 expression lists. */
7734 is_attribute_list = non_attr;
7736 get_comma:;
7737 /* If the next token isn't a `,', then we are done. */
7738 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7739 break;
7741 /* Otherwise, consume the `,' and keep going. */
7742 cp_lexer_consume_token (parser->lexer);
7745 if (close_paren_loc)
7746 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7748 if (!parens.require_close (parser))
7750 int ending;
7752 skip_comma:;
7753 /* We try and resync to an unnested comma, as that will give the
7754 user better diagnostics. */
7755 ending = cp_parser_skip_to_closing_parenthesis (parser,
7756 /*recovering=*/true,
7757 /*or_comma=*/true,
7758 /*consume_paren=*/true);
7759 if (ending < 0)
7760 goto get_comma;
7761 if (!ending)
7763 parser->greater_than_is_operator_p
7764 = saved_greater_than_is_operator_p;
7765 return NULL;
7769 parser->greater_than_is_operator_p
7770 = saved_greater_than_is_operator_p;
7772 if (identifier)
7773 vec_safe_insert (expression_list, 0, identifier);
7775 return expression_list;
7778 /* Parse a pseudo-destructor-name.
7780 pseudo-destructor-name:
7781 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7782 :: [opt] nested-name-specifier template template-id :: ~ type-name
7783 :: [opt] nested-name-specifier [opt] ~ type-name
7785 If either of the first two productions is used, sets *SCOPE to the
7786 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7787 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7788 or ERROR_MARK_NODE if the parse fails. */
7790 static void
7791 cp_parser_pseudo_destructor_name (cp_parser* parser,
7792 tree object,
7793 tree* scope,
7794 tree* type)
7796 bool nested_name_specifier_p;
7798 /* Handle ~auto. */
7799 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7800 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7801 && !type_dependent_expression_p (object))
7803 if (cxx_dialect < cxx14)
7804 pedwarn (input_location, 0,
7805 "%<~auto%> only available with "
7806 "-std=c++14 or -std=gnu++14");
7807 cp_lexer_consume_token (parser->lexer);
7808 cp_lexer_consume_token (parser->lexer);
7809 *scope = NULL_TREE;
7810 *type = TREE_TYPE (object);
7811 return;
7814 /* Assume that things will not work out. */
7815 *type = error_mark_node;
7817 /* Look for the optional `::' operator. */
7818 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7819 /* Look for the optional nested-name-specifier. */
7820 nested_name_specifier_p
7821 = (cp_parser_nested_name_specifier_opt (parser,
7822 /*typename_keyword_p=*/false,
7823 /*check_dependency_p=*/true,
7824 /*type_p=*/false,
7825 /*is_declaration=*/false)
7826 != NULL_TREE);
7827 /* Now, if we saw a nested-name-specifier, we might be doing the
7828 second production. */
7829 if (nested_name_specifier_p
7830 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7832 /* Consume the `template' keyword. */
7833 cp_lexer_consume_token (parser->lexer);
7834 /* Parse the template-id. */
7835 cp_parser_template_id (parser,
7836 /*template_keyword_p=*/true,
7837 /*check_dependency_p=*/false,
7838 class_type,
7839 /*is_declaration=*/true);
7840 /* Look for the `::' token. */
7841 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7843 /* If the next token is not a `~', then there might be some
7844 additional qualification. */
7845 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7847 /* At this point, we're looking for "type-name :: ~". The type-name
7848 must not be a class-name, since this is a pseudo-destructor. So,
7849 it must be either an enum-name, or a typedef-name -- both of which
7850 are just identifiers. So, we peek ahead to check that the "::"
7851 and "~" tokens are present; if they are not, then we can avoid
7852 calling type_name. */
7853 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7854 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7855 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7857 cp_parser_error (parser, "non-scalar type");
7858 return;
7861 /* Look for the type-name. */
7862 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7863 if (*scope == error_mark_node)
7864 return;
7866 /* Look for the `::' token. */
7867 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7869 else
7870 *scope = NULL_TREE;
7872 /* Look for the `~'. */
7873 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7875 /* Once we see the ~, this has to be a pseudo-destructor. */
7876 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7877 cp_parser_commit_to_topmost_tentative_parse (parser);
7879 /* Look for the type-name again. We are not responsible for
7880 checking that it matches the first type-name. */
7881 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7884 /* Parse a unary-expression.
7886 unary-expression:
7887 postfix-expression
7888 ++ cast-expression
7889 -- cast-expression
7890 unary-operator cast-expression
7891 sizeof unary-expression
7892 sizeof ( type-id )
7893 alignof ( type-id ) [C++0x]
7894 new-expression
7895 delete-expression
7897 GNU Extensions:
7899 unary-expression:
7900 __extension__ cast-expression
7901 __alignof__ unary-expression
7902 __alignof__ ( type-id )
7903 alignof unary-expression [C++0x]
7904 __real__ cast-expression
7905 __imag__ cast-expression
7906 && identifier
7907 sizeof ( type-id ) { initializer-list , [opt] }
7908 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7909 __alignof__ ( type-id ) { initializer-list , [opt] }
7911 ADDRESS_P is true iff the unary-expression is appearing as the
7912 operand of the `&' operator. CAST_P is true if this expression is
7913 the target of a cast.
7915 Returns a representation of the expression. */
7917 static cp_expr
7918 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7919 bool address_p, bool cast_p, bool decltype_p)
7921 cp_token *token;
7922 enum tree_code unary_operator;
7924 /* Peek at the next token. */
7925 token = cp_lexer_peek_token (parser->lexer);
7926 /* Some keywords give away the kind of expression. */
7927 if (token->type == CPP_KEYWORD)
7929 enum rid keyword = token->keyword;
7931 switch (keyword)
7933 case RID_ALIGNOF:
7934 case RID_SIZEOF:
7936 tree operand, ret;
7937 enum tree_code op;
7938 location_t start_loc = token->location;
7940 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7941 /* Consume the token. */
7942 cp_lexer_consume_token (parser->lexer);
7943 /* Parse the operand. */
7944 operand = cp_parser_sizeof_operand (parser, keyword);
7946 if (TYPE_P (operand))
7947 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7948 else
7950 /* ISO C++ defines alignof only with types, not with
7951 expressions. So pedwarn if alignof is used with a non-
7952 type expression. However, __alignof__ is ok. */
7953 if (id_equal (token->u.value, "alignof"))
7954 pedwarn (token->location, OPT_Wpedantic,
7955 "ISO C++ does not allow %<alignof%> "
7956 "with a non-type");
7958 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7960 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7961 SIZEOF_EXPR with the original operand. */
7962 if (op == SIZEOF_EXPR && ret != error_mark_node)
7964 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7966 if (!processing_template_decl && TYPE_P (operand))
7968 ret = build_min (SIZEOF_EXPR, size_type_node,
7969 build1 (NOP_EXPR, operand,
7970 error_mark_node));
7971 SIZEOF_EXPR_TYPE_P (ret) = 1;
7973 else
7974 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7975 TREE_SIDE_EFFECTS (ret) = 0;
7976 TREE_READONLY (ret) = 1;
7980 /* Construct a location e.g. :
7981 alignof (expr)
7982 ^~~~~~~~~~~~~~
7983 with start == caret at the start of the "alignof"/"sizeof"
7984 token, with the endpoint at the final closing paren. */
7985 location_t finish_loc
7986 = cp_lexer_previous_token (parser->lexer)->location;
7987 location_t compound_loc
7988 = make_location (start_loc, start_loc, finish_loc);
7990 cp_expr ret_expr (ret);
7991 ret_expr.set_location (compound_loc);
7992 return ret_expr;
7995 case RID_NEW:
7996 return cp_parser_new_expression (parser);
7998 case RID_DELETE:
7999 return cp_parser_delete_expression (parser);
8001 case RID_EXTENSION:
8003 /* The saved value of the PEDANTIC flag. */
8004 int saved_pedantic;
8005 tree expr;
8007 /* Save away the PEDANTIC flag. */
8008 cp_parser_extension_opt (parser, &saved_pedantic);
8009 /* Parse the cast-expression. */
8010 expr = cp_parser_simple_cast_expression (parser);
8011 /* Restore the PEDANTIC flag. */
8012 pedantic = saved_pedantic;
8014 return expr;
8017 case RID_REALPART:
8018 case RID_IMAGPART:
8020 tree expression;
8022 /* Consume the `__real__' or `__imag__' token. */
8023 cp_lexer_consume_token (parser->lexer);
8024 /* Parse the cast-expression. */
8025 expression = cp_parser_simple_cast_expression (parser);
8026 /* Create the complete representation. */
8027 return build_x_unary_op (token->location,
8028 (keyword == RID_REALPART
8029 ? REALPART_EXPR : IMAGPART_EXPR),
8030 expression,
8031 tf_warning_or_error);
8033 break;
8035 case RID_TRANSACTION_ATOMIC:
8036 case RID_TRANSACTION_RELAXED:
8037 return cp_parser_transaction_expression (parser, keyword);
8039 case RID_NOEXCEPT:
8041 tree expr;
8042 const char *saved_message;
8043 bool saved_integral_constant_expression_p;
8044 bool saved_non_integral_constant_expression_p;
8045 bool saved_greater_than_is_operator_p;
8047 cp_lexer_consume_token (parser->lexer);
8048 matching_parens parens;
8049 parens.require_open (parser);
8051 saved_message = parser->type_definition_forbidden_message;
8052 parser->type_definition_forbidden_message
8053 = G_("types may not be defined in %<noexcept%> expressions");
8055 saved_integral_constant_expression_p
8056 = parser->integral_constant_expression_p;
8057 saved_non_integral_constant_expression_p
8058 = parser->non_integral_constant_expression_p;
8059 parser->integral_constant_expression_p = false;
8061 saved_greater_than_is_operator_p
8062 = parser->greater_than_is_operator_p;
8063 parser->greater_than_is_operator_p = true;
8065 ++cp_unevaluated_operand;
8066 ++c_inhibit_evaluation_warnings;
8067 ++cp_noexcept_operand;
8068 expr = cp_parser_expression (parser);
8069 --cp_noexcept_operand;
8070 --c_inhibit_evaluation_warnings;
8071 --cp_unevaluated_operand;
8073 parser->greater_than_is_operator_p
8074 = saved_greater_than_is_operator_p;
8076 parser->integral_constant_expression_p
8077 = saved_integral_constant_expression_p;
8078 parser->non_integral_constant_expression_p
8079 = saved_non_integral_constant_expression_p;
8081 parser->type_definition_forbidden_message = saved_message;
8083 parens.require_close (parser);
8084 return finish_noexcept_expr (expr, tf_warning_or_error);
8087 default:
8088 break;
8092 /* Look for the `:: new' and `:: delete', which also signal the
8093 beginning of a new-expression, or delete-expression,
8094 respectively. If the next token is `::', then it might be one of
8095 these. */
8096 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8098 enum rid keyword;
8100 /* See if the token after the `::' is one of the keywords in
8101 which we're interested. */
8102 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8103 /* If it's `new', we have a new-expression. */
8104 if (keyword == RID_NEW)
8105 return cp_parser_new_expression (parser);
8106 /* Similarly, for `delete'. */
8107 else if (keyword == RID_DELETE)
8108 return cp_parser_delete_expression (parser);
8111 /* Look for a unary operator. */
8112 unary_operator = cp_parser_unary_operator (token);
8113 /* The `++' and `--' operators can be handled similarly, even though
8114 they are not technically unary-operators in the grammar. */
8115 if (unary_operator == ERROR_MARK)
8117 if (token->type == CPP_PLUS_PLUS)
8118 unary_operator = PREINCREMENT_EXPR;
8119 else if (token->type == CPP_MINUS_MINUS)
8120 unary_operator = PREDECREMENT_EXPR;
8121 /* Handle the GNU address-of-label extension. */
8122 else if (cp_parser_allow_gnu_extensions_p (parser)
8123 && token->type == CPP_AND_AND)
8125 tree identifier;
8126 tree expression;
8127 location_t start_loc = token->location;
8129 /* Consume the '&&' token. */
8130 cp_lexer_consume_token (parser->lexer);
8131 /* Look for the identifier. */
8132 location_t finish_loc
8133 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8134 identifier = cp_parser_identifier (parser);
8135 /* Construct a location of the form:
8136 &&label
8137 ^~~~~~~
8138 with caret==start at the "&&", finish at the end of the label. */
8139 location_t combined_loc
8140 = make_location (start_loc, start_loc, finish_loc);
8141 /* Create an expression representing the address. */
8142 expression = finish_label_address_expr (identifier, combined_loc);
8143 if (cp_parser_non_integral_constant_expression (parser,
8144 NIC_ADDR_LABEL))
8145 expression = error_mark_node;
8146 return expression;
8149 if (unary_operator != ERROR_MARK)
8151 cp_expr cast_expression;
8152 cp_expr expression = error_mark_node;
8153 non_integral_constant non_constant_p = NIC_NONE;
8154 location_t loc = token->location;
8155 tsubst_flags_t complain = complain_flags (decltype_p);
8157 /* Consume the operator token. */
8158 token = cp_lexer_consume_token (parser->lexer);
8159 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8161 /* Parse the cast-expression. */
8162 cast_expression
8163 = cp_parser_cast_expression (parser,
8164 unary_operator == ADDR_EXPR,
8165 /*cast_p=*/false,
8166 /*decltype*/false,
8167 pidk);
8169 /* Make a location:
8170 OP_TOKEN CAST_EXPRESSION
8171 ^~~~~~~~~~~~~~~~~~~~~~~~~
8172 with start==caret at the operator token, and
8173 extending to the end of the cast_expression. */
8174 loc = make_location (loc, loc, cast_expression.get_finish ());
8176 /* Now, build an appropriate representation. */
8177 switch (unary_operator)
8179 case INDIRECT_REF:
8180 non_constant_p = NIC_STAR;
8181 expression = build_x_indirect_ref (loc, cast_expression,
8182 RO_UNARY_STAR,
8183 complain);
8184 /* TODO: build_x_indirect_ref does not always honor the
8185 location, so ensure it is set. */
8186 expression.set_location (loc);
8187 break;
8189 case ADDR_EXPR:
8190 non_constant_p = NIC_ADDR;
8191 /* Fall through. */
8192 case BIT_NOT_EXPR:
8193 expression = build_x_unary_op (loc, unary_operator,
8194 cast_expression,
8195 complain);
8196 /* TODO: build_x_unary_op does not always honor the location,
8197 so ensure it is set. */
8198 expression.set_location (loc);
8199 break;
8201 case PREINCREMENT_EXPR:
8202 case PREDECREMENT_EXPR:
8203 non_constant_p = unary_operator == PREINCREMENT_EXPR
8204 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8205 /* Fall through. */
8206 case NEGATE_EXPR:
8207 /* Immediately fold negation of a constant, unless the constant is 0
8208 (since -0 == 0) or it would overflow. */
8209 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8210 && CONSTANT_CLASS_P (cast_expression)
8211 && !integer_zerop (cast_expression)
8212 && !TREE_OVERFLOW (cast_expression))
8214 tree folded = fold_build1 (unary_operator,
8215 TREE_TYPE (cast_expression),
8216 cast_expression);
8217 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8219 expression = cp_expr (folded, loc);
8220 break;
8223 /* Fall through. */
8224 case UNARY_PLUS_EXPR:
8225 case TRUTH_NOT_EXPR:
8226 expression = finish_unary_op_expr (loc, unary_operator,
8227 cast_expression, complain);
8228 break;
8230 default:
8231 gcc_unreachable ();
8234 if (non_constant_p != NIC_NONE
8235 && cp_parser_non_integral_constant_expression (parser,
8236 non_constant_p))
8237 expression = error_mark_node;
8239 return expression;
8242 return cp_parser_postfix_expression (parser, address_p, cast_p,
8243 /*member_access_only_p=*/false,
8244 decltype_p,
8245 pidk);
8248 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8249 unary-operator, the corresponding tree code is returned. */
8251 static enum tree_code
8252 cp_parser_unary_operator (cp_token* token)
8254 switch (token->type)
8256 case CPP_MULT:
8257 return INDIRECT_REF;
8259 case CPP_AND:
8260 return ADDR_EXPR;
8262 case CPP_PLUS:
8263 return UNARY_PLUS_EXPR;
8265 case CPP_MINUS:
8266 return NEGATE_EXPR;
8268 case CPP_NOT:
8269 return TRUTH_NOT_EXPR;
8271 case CPP_COMPL:
8272 return BIT_NOT_EXPR;
8274 default:
8275 return ERROR_MARK;
8279 /* Parse a new-expression.
8281 new-expression:
8282 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8283 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8285 Returns a representation of the expression. */
8287 static tree
8288 cp_parser_new_expression (cp_parser* parser)
8290 bool global_scope_p;
8291 vec<tree, va_gc> *placement;
8292 tree type;
8293 vec<tree, va_gc> *initializer;
8294 tree nelts = NULL_TREE;
8295 tree ret;
8297 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8299 /* Look for the optional `::' operator. */
8300 global_scope_p
8301 = (cp_parser_global_scope_opt (parser,
8302 /*current_scope_valid_p=*/false)
8303 != NULL_TREE);
8304 /* Look for the `new' operator. */
8305 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8306 /* There's no easy way to tell a new-placement from the
8307 `( type-id )' construct. */
8308 cp_parser_parse_tentatively (parser);
8309 /* Look for a new-placement. */
8310 placement = cp_parser_new_placement (parser);
8311 /* If that didn't work out, there's no new-placement. */
8312 if (!cp_parser_parse_definitely (parser))
8314 if (placement != NULL)
8315 release_tree_vector (placement);
8316 placement = NULL;
8319 /* If the next token is a `(', then we have a parenthesized
8320 type-id. */
8321 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8323 cp_token *token;
8324 const char *saved_message = parser->type_definition_forbidden_message;
8326 /* Consume the `('. */
8327 matching_parens parens;
8328 parens.consume_open (parser);
8330 /* Parse the type-id. */
8331 parser->type_definition_forbidden_message
8332 = G_("types may not be defined in a new-expression");
8334 type_id_in_expr_sentinel s (parser);
8335 type = cp_parser_type_id (parser);
8337 parser->type_definition_forbidden_message = saved_message;
8339 /* Look for the closing `)'. */
8340 parens.require_close (parser);
8341 token = cp_lexer_peek_token (parser->lexer);
8342 /* There should not be a direct-new-declarator in this production,
8343 but GCC used to allowed this, so we check and emit a sensible error
8344 message for this case. */
8345 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8347 error_at (token->location,
8348 "array bound forbidden after parenthesized type-id");
8349 inform (token->location,
8350 "try removing the parentheses around the type-id");
8351 cp_parser_direct_new_declarator (parser);
8354 /* Otherwise, there must be a new-type-id. */
8355 else
8356 type = cp_parser_new_type_id (parser, &nelts);
8358 /* If the next token is a `(' or '{', then we have a new-initializer. */
8359 cp_token *token = cp_lexer_peek_token (parser->lexer);
8360 if (token->type == CPP_OPEN_PAREN
8361 || token->type == CPP_OPEN_BRACE)
8362 initializer = cp_parser_new_initializer (parser);
8363 else
8364 initializer = NULL;
8366 /* A new-expression may not appear in an integral constant
8367 expression. */
8368 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8369 ret = error_mark_node;
8370 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8371 of a new-type-id or type-id of a new-expression, the new-expression shall
8372 contain a new-initializer of the form ( assignment-expression )".
8373 Additionally, consistently with the spirit of DR 1467, we want to accept
8374 'new auto { 2 }' too. */
8375 else if ((ret = type_uses_auto (type))
8376 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8377 && (vec_safe_length (initializer) != 1
8378 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8379 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8381 error_at (token->location,
8382 "initialization of new-expression for type %<auto%> "
8383 "requires exactly one element");
8384 ret = error_mark_node;
8386 else
8388 /* Construct a location e.g.:
8389 ptr = new int[100]
8390 ^~~~~~~~~~~~
8391 with caret == start at the start of the "new" token, and the end
8392 at the end of the final token we consumed. */
8393 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8394 location_t end_loc = get_finish (end_tok->location);
8395 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8397 /* Create a representation of the new-expression. */
8398 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8399 tf_warning_or_error);
8400 protected_set_expr_location (ret, combined_loc);
8403 if (placement != NULL)
8404 release_tree_vector (placement);
8405 if (initializer != NULL)
8406 release_tree_vector (initializer);
8408 return ret;
8411 /* Parse a new-placement.
8413 new-placement:
8414 ( expression-list )
8416 Returns the same representation as for an expression-list. */
8418 static vec<tree, va_gc> *
8419 cp_parser_new_placement (cp_parser* parser)
8421 vec<tree, va_gc> *expression_list;
8423 /* Parse the expression-list. */
8424 expression_list = (cp_parser_parenthesized_expression_list
8425 (parser, non_attr, /*cast_p=*/false,
8426 /*allow_expansion_p=*/true,
8427 /*non_constant_p=*/NULL));
8429 if (expression_list && expression_list->is_empty ())
8430 error ("expected expression-list or type-id");
8432 return expression_list;
8435 /* Parse a new-type-id.
8437 new-type-id:
8438 type-specifier-seq new-declarator [opt]
8440 Returns the TYPE allocated. If the new-type-id indicates an array
8441 type, *NELTS is set to the number of elements in the last array
8442 bound; the TYPE will not include the last array bound. */
8444 static tree
8445 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8447 cp_decl_specifier_seq type_specifier_seq;
8448 cp_declarator *new_declarator;
8449 cp_declarator *declarator;
8450 cp_declarator *outer_declarator;
8451 const char *saved_message;
8453 /* The type-specifier sequence must not contain type definitions.
8454 (It cannot contain declarations of new types either, but if they
8455 are not definitions we will catch that because they are not
8456 complete.) */
8457 saved_message = parser->type_definition_forbidden_message;
8458 parser->type_definition_forbidden_message
8459 = G_("types may not be defined in a new-type-id");
8460 /* Parse the type-specifier-seq. */
8461 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8462 /*is_trailing_return=*/false,
8463 &type_specifier_seq);
8464 /* Restore the old message. */
8465 parser->type_definition_forbidden_message = saved_message;
8467 if (type_specifier_seq.type == error_mark_node)
8468 return error_mark_node;
8470 /* Parse the new-declarator. */
8471 new_declarator = cp_parser_new_declarator_opt (parser);
8473 /* Determine the number of elements in the last array dimension, if
8474 any. */
8475 *nelts = NULL_TREE;
8476 /* Skip down to the last array dimension. */
8477 declarator = new_declarator;
8478 outer_declarator = NULL;
8479 while (declarator && (declarator->kind == cdk_pointer
8480 || declarator->kind == cdk_ptrmem))
8482 outer_declarator = declarator;
8483 declarator = declarator->declarator;
8485 while (declarator
8486 && declarator->kind == cdk_array
8487 && declarator->declarator
8488 && declarator->declarator->kind == cdk_array)
8490 outer_declarator = declarator;
8491 declarator = declarator->declarator;
8494 if (declarator && declarator->kind == cdk_array)
8496 *nelts = declarator->u.array.bounds;
8497 if (*nelts == error_mark_node)
8498 *nelts = integer_one_node;
8500 if (outer_declarator)
8501 outer_declarator->declarator = declarator->declarator;
8502 else
8503 new_declarator = NULL;
8506 return groktypename (&type_specifier_seq, new_declarator, false);
8509 /* Parse an (optional) new-declarator.
8511 new-declarator:
8512 ptr-operator new-declarator [opt]
8513 direct-new-declarator
8515 Returns the declarator. */
8517 static cp_declarator *
8518 cp_parser_new_declarator_opt (cp_parser* parser)
8520 enum tree_code code;
8521 tree type, std_attributes = NULL_TREE;
8522 cp_cv_quals cv_quals;
8524 /* We don't know if there's a ptr-operator next, or not. */
8525 cp_parser_parse_tentatively (parser);
8526 /* Look for a ptr-operator. */
8527 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8528 /* If that worked, look for more new-declarators. */
8529 if (cp_parser_parse_definitely (parser))
8531 cp_declarator *declarator;
8533 /* Parse another optional declarator. */
8534 declarator = cp_parser_new_declarator_opt (parser);
8536 declarator = cp_parser_make_indirect_declarator
8537 (code, type, cv_quals, declarator, std_attributes);
8539 return declarator;
8542 /* If the next token is a `[', there is a direct-new-declarator. */
8543 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8544 return cp_parser_direct_new_declarator (parser);
8546 return NULL;
8549 /* Parse a direct-new-declarator.
8551 direct-new-declarator:
8552 [ expression ]
8553 direct-new-declarator [constant-expression]
8557 static cp_declarator *
8558 cp_parser_direct_new_declarator (cp_parser* parser)
8560 cp_declarator *declarator = NULL;
8562 while (true)
8564 tree expression;
8565 cp_token *token;
8567 /* Look for the opening `['. */
8568 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8570 token = cp_lexer_peek_token (parser->lexer);
8571 expression = cp_parser_expression (parser);
8572 /* The standard requires that the expression have integral
8573 type. DR 74 adds enumeration types. We believe that the
8574 real intent is that these expressions be handled like the
8575 expression in a `switch' condition, which also allows
8576 classes with a single conversion to integral or
8577 enumeration type. */
8578 if (!processing_template_decl)
8580 expression
8581 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8582 expression,
8583 /*complain=*/true);
8584 if (!expression)
8586 error_at (token->location,
8587 "expression in new-declarator must have integral "
8588 "or enumeration type");
8589 expression = error_mark_node;
8593 /* Look for the closing `]'. */
8594 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8596 /* Add this bound to the declarator. */
8597 declarator = make_array_declarator (declarator, expression);
8599 /* If the next token is not a `[', then there are no more
8600 bounds. */
8601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8602 break;
8605 return declarator;
8608 /* Parse a new-initializer.
8610 new-initializer:
8611 ( expression-list [opt] )
8612 braced-init-list
8614 Returns a representation of the expression-list. */
8616 static vec<tree, va_gc> *
8617 cp_parser_new_initializer (cp_parser* parser)
8619 vec<tree, va_gc> *expression_list;
8621 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8623 tree t;
8624 bool expr_non_constant_p;
8625 cp_lexer_set_source_position (parser->lexer);
8626 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8627 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8628 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8629 expression_list = make_tree_vector_single (t);
8631 else
8632 expression_list = (cp_parser_parenthesized_expression_list
8633 (parser, non_attr, /*cast_p=*/false,
8634 /*allow_expansion_p=*/true,
8635 /*non_constant_p=*/NULL));
8637 return expression_list;
8640 /* Parse a delete-expression.
8642 delete-expression:
8643 :: [opt] delete cast-expression
8644 :: [opt] delete [ ] cast-expression
8646 Returns a representation of the expression. */
8648 static tree
8649 cp_parser_delete_expression (cp_parser* parser)
8651 bool global_scope_p;
8652 bool array_p;
8653 tree expression;
8655 /* Look for the optional `::' operator. */
8656 global_scope_p
8657 = (cp_parser_global_scope_opt (parser,
8658 /*current_scope_valid_p=*/false)
8659 != NULL_TREE);
8660 /* Look for the `delete' keyword. */
8661 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8662 /* See if the array syntax is in use. */
8663 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8665 /* Consume the `[' token. */
8666 cp_lexer_consume_token (parser->lexer);
8667 /* Look for the `]' token. */
8668 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8669 /* Remember that this is the `[]' construct. */
8670 array_p = true;
8672 else
8673 array_p = false;
8675 /* Parse the cast-expression. */
8676 expression = cp_parser_simple_cast_expression (parser);
8678 /* A delete-expression may not appear in an integral constant
8679 expression. */
8680 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8681 return error_mark_node;
8683 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8684 tf_warning_or_error);
8687 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8688 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8689 0 otherwise. */
8691 static int
8692 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8694 cp_token *token = cp_lexer_peek_token (parser->lexer);
8695 switch (token->type)
8697 case CPP_COMMA:
8698 case CPP_SEMICOLON:
8699 case CPP_QUERY:
8700 case CPP_COLON:
8701 case CPP_CLOSE_SQUARE:
8702 case CPP_CLOSE_PAREN:
8703 case CPP_CLOSE_BRACE:
8704 case CPP_OPEN_BRACE:
8705 case CPP_DOT:
8706 case CPP_DOT_STAR:
8707 case CPP_DEREF:
8708 case CPP_DEREF_STAR:
8709 case CPP_DIV:
8710 case CPP_MOD:
8711 case CPP_LSHIFT:
8712 case CPP_RSHIFT:
8713 case CPP_LESS:
8714 case CPP_GREATER:
8715 case CPP_LESS_EQ:
8716 case CPP_GREATER_EQ:
8717 case CPP_EQ_EQ:
8718 case CPP_NOT_EQ:
8719 case CPP_EQ:
8720 case CPP_MULT_EQ:
8721 case CPP_DIV_EQ:
8722 case CPP_MOD_EQ:
8723 case CPP_PLUS_EQ:
8724 case CPP_MINUS_EQ:
8725 case CPP_RSHIFT_EQ:
8726 case CPP_LSHIFT_EQ:
8727 case CPP_AND_EQ:
8728 case CPP_XOR_EQ:
8729 case CPP_OR_EQ:
8730 case CPP_XOR:
8731 case CPP_OR:
8732 case CPP_OR_OR:
8733 case CPP_EOF:
8734 case CPP_ELLIPSIS:
8735 return 0;
8737 case CPP_OPEN_PAREN:
8738 /* In ((type ()) () the last () isn't a valid cast-expression,
8739 so the whole must be parsed as postfix-expression. */
8740 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8741 != CPP_CLOSE_PAREN;
8743 case CPP_OPEN_SQUARE:
8744 /* '[' may start a primary-expression in obj-c++ and in C++11,
8745 as a lambda-expression, eg, '(void)[]{}'. */
8746 if (cxx_dialect >= cxx11)
8747 return -1;
8748 return c_dialect_objc ();
8750 case CPP_PLUS_PLUS:
8751 case CPP_MINUS_MINUS:
8752 /* '++' and '--' may or may not start a cast-expression:
8754 struct T { void operator++(int); };
8755 void f() { (T())++; }
8759 int a;
8760 (int)++a; */
8761 return -1;
8763 default:
8764 return 1;
8768 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8769 in the order: const_cast, static_cast, reinterpret_cast.
8771 Don't suggest dynamic_cast.
8773 Return the first legal cast kind found, or NULL otherwise. */
8775 static const char *
8776 get_cast_suggestion (tree dst_type, tree orig_expr)
8778 tree trial;
8780 /* Reuse the parser logic by attempting to build the various kinds of
8781 cast, with "complain" disabled.
8782 Identify the first such cast that is valid. */
8784 /* Don't attempt to run such logic within template processing. */
8785 if (processing_template_decl)
8786 return NULL;
8788 /* First try const_cast. */
8789 trial = build_const_cast (dst_type, orig_expr, tf_none);
8790 if (trial != error_mark_node)
8791 return "const_cast";
8793 /* If that fails, try static_cast. */
8794 trial = build_static_cast (dst_type, orig_expr, tf_none);
8795 if (trial != error_mark_node)
8796 return "static_cast";
8798 /* Finally, try reinterpret_cast. */
8799 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8800 if (trial != error_mark_node)
8801 return "reinterpret_cast";
8803 /* No such cast possible. */
8804 return NULL;
8807 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8808 suggesting how to convert a C-style cast of the form:
8810 (DST_TYPE)ORIG_EXPR
8812 to a C++-style cast.
8814 The primary range of RICHLOC is asssumed to be that of the original
8815 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8816 of the parens in the C-style cast. */
8818 static void
8819 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8820 location_t close_paren_loc, tree orig_expr,
8821 tree dst_type)
8823 /* This function is non-trivial, so bail out now if the warning isn't
8824 going to be emitted. */
8825 if (!warn_old_style_cast)
8826 return;
8828 /* Try to find a legal C++ cast, trying them in order:
8829 const_cast, static_cast, reinterpret_cast. */
8830 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8831 if (!cast_suggestion)
8832 return;
8834 /* Replace the open paren with "CAST_SUGGESTION<". */
8835 pretty_printer pp;
8836 pp_printf (&pp, "%s<", cast_suggestion);
8837 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8839 /* Replace the close paren with "> (". */
8840 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8842 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8843 rich_loc->add_fixit_insert_after (")");
8847 /* Parse a cast-expression.
8849 cast-expression:
8850 unary-expression
8851 ( type-id ) cast-expression
8853 ADDRESS_P is true iff the unary-expression is appearing as the
8854 operand of the `&' operator. CAST_P is true if this expression is
8855 the target of a cast.
8857 Returns a representation of the expression. */
8859 static cp_expr
8860 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8861 bool decltype_p, cp_id_kind * pidk)
8863 /* If it's a `(', then we might be looking at a cast. */
8864 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8866 tree type = NULL_TREE;
8867 cp_expr expr (NULL_TREE);
8868 int cast_expression = 0;
8869 const char *saved_message;
8871 /* There's no way to know yet whether or not this is a cast.
8872 For example, `(int (3))' is a unary-expression, while `(int)
8873 3' is a cast. So, we resort to parsing tentatively. */
8874 cp_parser_parse_tentatively (parser);
8875 /* Types may not be defined in a cast. */
8876 saved_message = parser->type_definition_forbidden_message;
8877 parser->type_definition_forbidden_message
8878 = G_("types may not be defined in casts");
8879 /* Consume the `('. */
8880 matching_parens parens;
8881 cp_token *open_paren = parens.consume_open (parser);
8882 location_t open_paren_loc = open_paren->location;
8883 location_t close_paren_loc = UNKNOWN_LOCATION;
8885 /* A very tricky bit is that `(struct S) { 3 }' is a
8886 compound-literal (which we permit in C++ as an extension).
8887 But, that construct is not a cast-expression -- it is a
8888 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8889 is legal; if the compound-literal were a cast-expression,
8890 you'd need an extra set of parentheses.) But, if we parse
8891 the type-id, and it happens to be a class-specifier, then we
8892 will commit to the parse at that point, because we cannot
8893 undo the action that is done when creating a new class. So,
8894 then we cannot back up and do a postfix-expression.
8896 Another tricky case is the following (c++/29234):
8898 struct S { void operator () (); };
8900 void foo ()
8902 ( S()() );
8905 As a type-id we parse the parenthesized S()() as a function
8906 returning a function, groktypename complains and we cannot
8907 back up in this case either.
8909 Therefore, we scan ahead to the closing `)', and check to see
8910 if the tokens after the `)' can start a cast-expression. Otherwise
8911 we are dealing with an unary-expression, a postfix-expression
8912 or something else.
8914 Yet another tricky case, in C++11, is the following (c++/54891):
8916 (void)[]{};
8918 The issue is that usually, besides the case of lambda-expressions,
8919 the parenthesized type-id cannot be followed by '[', and, eg, we
8920 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8921 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8922 we don't commit, we try a cast-expression, then an unary-expression.
8924 Save tokens so that we can put them back. */
8925 cp_lexer_save_tokens (parser->lexer);
8927 /* We may be looking at a cast-expression. */
8928 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8929 /*consume_paren=*/true))
8930 cast_expression
8931 = cp_parser_tokens_start_cast_expression (parser);
8933 /* Roll back the tokens we skipped. */
8934 cp_lexer_rollback_tokens (parser->lexer);
8935 /* If we aren't looking at a cast-expression, simulate an error so
8936 that the call to cp_parser_error_occurred below returns true. */
8937 if (!cast_expression)
8938 cp_parser_simulate_error (parser);
8939 else
8941 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8942 parser->in_type_id_in_expr_p = true;
8943 /* Look for the type-id. */
8944 type = cp_parser_type_id (parser);
8945 /* Look for the closing `)'. */
8946 cp_token *close_paren = parens.require_close (parser);
8947 if (close_paren)
8948 close_paren_loc = close_paren->location;
8949 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8952 /* Restore the saved message. */
8953 parser->type_definition_forbidden_message = saved_message;
8955 /* At this point this can only be either a cast or a
8956 parenthesized ctor such as `(T ())' that looks like a cast to
8957 function returning T. */
8958 if (!cp_parser_error_occurred (parser))
8960 /* Only commit if the cast-expression doesn't start with
8961 '++', '--', or '[' in C++11. */
8962 if (cast_expression > 0)
8963 cp_parser_commit_to_topmost_tentative_parse (parser);
8965 expr = cp_parser_cast_expression (parser,
8966 /*address_p=*/false,
8967 /*cast_p=*/true,
8968 /*decltype_p=*/false,
8969 pidk);
8971 if (cp_parser_parse_definitely (parser))
8973 /* Warn about old-style casts, if so requested. */
8974 if (warn_old_style_cast
8975 && !in_system_header_at (input_location)
8976 && !VOID_TYPE_P (type)
8977 && current_lang_name != lang_name_c)
8979 gcc_rich_location rich_loc (input_location);
8980 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8981 expr, type);
8982 warning_at_rich_loc (&rich_loc, OPT_Wold_style_cast,
8983 "use of old-style cast to %q#T", type);
8986 /* Only type conversions to integral or enumeration types
8987 can be used in constant-expressions. */
8988 if (!cast_valid_in_integral_constant_expression_p (type)
8989 && cp_parser_non_integral_constant_expression (parser,
8990 NIC_CAST))
8991 return error_mark_node;
8993 /* Perform the cast. */
8994 /* Make a location:
8995 (TYPE) EXPR
8996 ^~~~~~~~~~~
8997 with start==caret at the open paren, extending to the
8998 end of "expr". */
8999 location_t cast_loc = make_location (open_paren_loc,
9000 open_paren_loc,
9001 expr.get_finish ());
9002 expr = build_c_cast (cast_loc, type, expr);
9003 return expr;
9006 else
9007 cp_parser_abort_tentative_parse (parser);
9010 /* If we get here, then it's not a cast, so it must be a
9011 unary-expression. */
9012 return cp_parser_unary_expression (parser, pidk, address_p,
9013 cast_p, decltype_p);
9016 /* Parse a binary expression of the general form:
9018 pm-expression:
9019 cast-expression
9020 pm-expression .* cast-expression
9021 pm-expression ->* cast-expression
9023 multiplicative-expression:
9024 pm-expression
9025 multiplicative-expression * pm-expression
9026 multiplicative-expression / pm-expression
9027 multiplicative-expression % pm-expression
9029 additive-expression:
9030 multiplicative-expression
9031 additive-expression + multiplicative-expression
9032 additive-expression - multiplicative-expression
9034 shift-expression:
9035 additive-expression
9036 shift-expression << additive-expression
9037 shift-expression >> additive-expression
9039 relational-expression:
9040 shift-expression
9041 relational-expression < shift-expression
9042 relational-expression > shift-expression
9043 relational-expression <= shift-expression
9044 relational-expression >= shift-expression
9046 GNU Extension:
9048 relational-expression:
9049 relational-expression <? shift-expression
9050 relational-expression >? shift-expression
9052 equality-expression:
9053 relational-expression
9054 equality-expression == relational-expression
9055 equality-expression != relational-expression
9057 and-expression:
9058 equality-expression
9059 and-expression & equality-expression
9061 exclusive-or-expression:
9062 and-expression
9063 exclusive-or-expression ^ and-expression
9065 inclusive-or-expression:
9066 exclusive-or-expression
9067 inclusive-or-expression | exclusive-or-expression
9069 logical-and-expression:
9070 inclusive-or-expression
9071 logical-and-expression && inclusive-or-expression
9073 logical-or-expression:
9074 logical-and-expression
9075 logical-or-expression || logical-and-expression
9077 All these are implemented with a single function like:
9079 binary-expression:
9080 simple-cast-expression
9081 binary-expression <token> binary-expression
9083 CAST_P is true if this expression is the target of a cast.
9085 The binops_by_token map is used to get the tree codes for each <token> type.
9086 binary-expressions are associated according to a precedence table. */
9088 #define TOKEN_PRECEDENCE(token) \
9089 (((token->type == CPP_GREATER \
9090 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9091 && !parser->greater_than_is_operator_p) \
9092 ? PREC_NOT_OPERATOR \
9093 : binops_by_token[token->type].prec)
9095 static cp_expr
9096 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9097 bool no_toplevel_fold_p,
9098 bool decltype_p,
9099 enum cp_parser_prec prec,
9100 cp_id_kind * pidk)
9102 cp_parser_expression_stack stack;
9103 cp_parser_expression_stack_entry *sp = &stack[0];
9104 cp_parser_expression_stack_entry current;
9105 cp_expr rhs;
9106 cp_token *token;
9107 enum tree_code rhs_type;
9108 enum cp_parser_prec new_prec, lookahead_prec;
9109 tree overload;
9111 /* Parse the first expression. */
9112 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9113 ? TRUTH_NOT_EXPR : ERROR_MARK);
9114 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9115 cast_p, decltype_p, pidk);
9116 current.prec = prec;
9118 if (cp_parser_error_occurred (parser))
9119 return error_mark_node;
9121 for (;;)
9123 /* Get an operator token. */
9124 token = cp_lexer_peek_token (parser->lexer);
9126 if (warn_cxx11_compat
9127 && token->type == CPP_RSHIFT
9128 && !parser->greater_than_is_operator_p)
9130 if (warning_at (token->location, OPT_Wc__11_compat,
9131 "%<>>%> operator is treated"
9132 " as two right angle brackets in C++11"))
9133 inform (token->location,
9134 "suggest parentheses around %<>>%> expression");
9137 new_prec = TOKEN_PRECEDENCE (token);
9138 if (new_prec != PREC_NOT_OPERATOR
9139 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9140 /* This is a fold-expression; handle it later. */
9141 new_prec = PREC_NOT_OPERATOR;
9143 /* Popping an entry off the stack means we completed a subexpression:
9144 - either we found a token which is not an operator (`>' where it is not
9145 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9146 will happen repeatedly;
9147 - or, we found an operator which has lower priority. This is the case
9148 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9149 parsing `3 * 4'. */
9150 if (new_prec <= current.prec)
9152 if (sp == stack)
9153 break;
9154 else
9155 goto pop;
9158 get_rhs:
9159 current.tree_type = binops_by_token[token->type].tree_type;
9160 current.loc = token->location;
9162 /* We used the operator token. */
9163 cp_lexer_consume_token (parser->lexer);
9165 /* For "false && x" or "true || x", x will never be executed;
9166 disable warnings while evaluating it. */
9167 if (current.tree_type == TRUTH_ANDIF_EXPR)
9168 c_inhibit_evaluation_warnings +=
9169 cp_fully_fold (current.lhs) == truthvalue_false_node;
9170 else if (current.tree_type == TRUTH_ORIF_EXPR)
9171 c_inhibit_evaluation_warnings +=
9172 cp_fully_fold (current.lhs) == truthvalue_true_node;
9174 /* Extract another operand. It may be the RHS of this expression
9175 or the LHS of a new, higher priority expression. */
9176 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9177 ? TRUTH_NOT_EXPR : ERROR_MARK);
9178 rhs = cp_parser_simple_cast_expression (parser);
9180 /* Get another operator token. Look up its precedence to avoid
9181 building a useless (immediately popped) stack entry for common
9182 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9183 token = cp_lexer_peek_token (parser->lexer);
9184 lookahead_prec = TOKEN_PRECEDENCE (token);
9185 if (lookahead_prec != PREC_NOT_OPERATOR
9186 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9187 lookahead_prec = PREC_NOT_OPERATOR;
9188 if (lookahead_prec > new_prec)
9190 /* ... and prepare to parse the RHS of the new, higher priority
9191 expression. Since precedence levels on the stack are
9192 monotonically increasing, we do not have to care about
9193 stack overflows. */
9194 *sp = current;
9195 ++sp;
9196 current.lhs = rhs;
9197 current.lhs_type = rhs_type;
9198 current.prec = new_prec;
9199 new_prec = lookahead_prec;
9200 goto get_rhs;
9202 pop:
9203 lookahead_prec = new_prec;
9204 /* If the stack is not empty, we have parsed into LHS the right side
9205 (`4' in the example above) of an expression we had suspended.
9206 We can use the information on the stack to recover the LHS (`3')
9207 from the stack together with the tree code (`MULT_EXPR'), and
9208 the precedence of the higher level subexpression
9209 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9210 which will be used to actually build the additive expression. */
9211 rhs = current.lhs;
9212 rhs_type = current.lhs_type;
9213 --sp;
9214 current = *sp;
9217 /* Undo the disabling of warnings done above. */
9218 if (current.tree_type == TRUTH_ANDIF_EXPR)
9219 c_inhibit_evaluation_warnings -=
9220 cp_fully_fold (current.lhs) == truthvalue_false_node;
9221 else if (current.tree_type == TRUTH_ORIF_EXPR)
9222 c_inhibit_evaluation_warnings -=
9223 cp_fully_fold (current.lhs) == truthvalue_true_node;
9225 if (warn_logical_not_paren
9226 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9227 && current.lhs_type == TRUTH_NOT_EXPR
9228 /* Avoid warning for !!x == y. */
9229 && (TREE_CODE (current.lhs) != NE_EXPR
9230 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9231 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9232 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9233 /* Avoid warning for !b == y where b is boolean. */
9234 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9235 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9236 != BOOLEAN_TYPE))))
9237 /* Avoid warning for !!b == y where b is boolean. */
9238 && (!DECL_P (current.lhs)
9239 || TREE_TYPE (current.lhs) == NULL_TREE
9240 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9241 warn_logical_not_parentheses (current.loc, current.tree_type,
9242 current.lhs, maybe_constant_value (rhs));
9244 overload = NULL;
9246 location_t combined_loc = make_location (current.loc,
9247 current.lhs.get_start (),
9248 rhs.get_finish ());
9250 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9251 ERROR_MARK for everything that is not a binary expression.
9252 This makes warn_about_parentheses miss some warnings that
9253 involve unary operators. For unary expressions we should
9254 pass the correct tree_code unless the unary expression was
9255 surrounded by parentheses.
9257 if (no_toplevel_fold_p
9258 && lookahead_prec <= current.prec
9259 && sp == stack)
9260 current.lhs = build2_loc (combined_loc,
9261 current.tree_type,
9262 TREE_CODE_CLASS (current.tree_type)
9263 == tcc_comparison
9264 ? boolean_type_node : TREE_TYPE (current.lhs),
9265 current.lhs, rhs);
9266 else
9268 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9269 current.lhs, current.lhs_type,
9270 rhs, rhs_type, &overload,
9271 complain_flags (decltype_p));
9272 /* TODO: build_x_binary_op doesn't always honor the location. */
9273 current.lhs.set_location (combined_loc);
9275 current.lhs_type = current.tree_type;
9277 /* If the binary operator required the use of an overloaded operator,
9278 then this expression cannot be an integral constant-expression.
9279 An overloaded operator can be used even if both operands are
9280 otherwise permissible in an integral constant-expression if at
9281 least one of the operands is of enumeration type. */
9283 if (overload
9284 && cp_parser_non_integral_constant_expression (parser,
9285 NIC_OVERLOADED))
9286 return error_mark_node;
9289 return current.lhs;
9292 static cp_expr
9293 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9294 bool no_toplevel_fold_p,
9295 enum cp_parser_prec prec,
9296 cp_id_kind * pidk)
9298 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9299 /*decltype*/false, prec, pidk);
9302 /* Parse the `? expression : assignment-expression' part of a
9303 conditional-expression. The LOGICAL_OR_EXPR is the
9304 logical-or-expression that started the conditional-expression.
9305 Returns a representation of the entire conditional-expression.
9307 This routine is used by cp_parser_assignment_expression.
9309 ? expression : assignment-expression
9311 GNU Extensions:
9313 ? : assignment-expression */
9315 static tree
9316 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9318 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9319 cp_expr assignment_expr;
9320 struct cp_token *token;
9321 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9323 /* Consume the `?' token. */
9324 cp_lexer_consume_token (parser->lexer);
9325 token = cp_lexer_peek_token (parser->lexer);
9326 if (cp_parser_allow_gnu_extensions_p (parser)
9327 && token->type == CPP_COLON)
9329 pedwarn (token->location, OPT_Wpedantic,
9330 "ISO C++ does not allow ?: with omitted middle operand");
9331 /* Implicit true clause. */
9332 expr = NULL_TREE;
9333 c_inhibit_evaluation_warnings +=
9334 folded_logical_or_expr == truthvalue_true_node;
9335 warn_for_omitted_condop (token->location, logical_or_expr);
9337 else
9339 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9340 parser->colon_corrects_to_scope_p = false;
9341 /* Parse the expression. */
9342 c_inhibit_evaluation_warnings +=
9343 folded_logical_or_expr == truthvalue_false_node;
9344 expr = cp_parser_expression (parser);
9345 c_inhibit_evaluation_warnings +=
9346 ((folded_logical_or_expr == truthvalue_true_node)
9347 - (folded_logical_or_expr == truthvalue_false_node));
9348 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9351 /* The next token should be a `:'. */
9352 cp_parser_require (parser, CPP_COLON, RT_COLON);
9353 /* Parse the assignment-expression. */
9354 assignment_expr = cp_parser_assignment_expression (parser);
9355 c_inhibit_evaluation_warnings -=
9356 folded_logical_or_expr == truthvalue_true_node;
9358 /* Make a location:
9359 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9360 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9361 with the caret at the "?", ranging from the start of
9362 the logical_or_expr to the end of the assignment_expr. */
9363 loc = make_location (loc,
9364 logical_or_expr.get_start (),
9365 assignment_expr.get_finish ());
9367 /* Build the conditional-expression. */
9368 return build_x_conditional_expr (loc, logical_or_expr,
9369 expr,
9370 assignment_expr,
9371 tf_warning_or_error);
9374 /* Parse an assignment-expression.
9376 assignment-expression:
9377 conditional-expression
9378 logical-or-expression assignment-operator assignment_expression
9379 throw-expression
9381 CAST_P is true if this expression is the target of a cast.
9382 DECLTYPE_P is true if this expression is the operand of decltype.
9384 Returns a representation for the expression. */
9386 static cp_expr
9387 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9388 bool cast_p, bool decltype_p)
9390 cp_expr expr;
9392 /* If the next token is the `throw' keyword, then we're looking at
9393 a throw-expression. */
9394 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9395 expr = cp_parser_throw_expression (parser);
9396 /* Otherwise, it must be that we are looking at a
9397 logical-or-expression. */
9398 else
9400 /* Parse the binary expressions (logical-or-expression). */
9401 expr = cp_parser_binary_expression (parser, cast_p, false,
9402 decltype_p,
9403 PREC_NOT_OPERATOR, pidk);
9404 /* If the next token is a `?' then we're actually looking at a
9405 conditional-expression. */
9406 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9407 return cp_parser_question_colon_clause (parser, expr);
9408 else
9410 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9412 /* If it's an assignment-operator, we're using the second
9413 production. */
9414 enum tree_code assignment_operator
9415 = cp_parser_assignment_operator_opt (parser);
9416 if (assignment_operator != ERROR_MARK)
9418 bool non_constant_p;
9420 /* Parse the right-hand side of the assignment. */
9421 cp_expr rhs = cp_parser_initializer_clause (parser,
9422 &non_constant_p);
9424 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9425 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9427 /* An assignment may not appear in a
9428 constant-expression. */
9429 if (cp_parser_non_integral_constant_expression (parser,
9430 NIC_ASSIGNMENT))
9431 return error_mark_node;
9432 /* Build the assignment expression. Its default
9433 location:
9434 LHS = RHS
9435 ~~~~^~~~~
9436 is the location of the '=' token as the
9437 caret, ranging from the start of the lhs to the
9438 end of the rhs. */
9439 loc = make_location (loc,
9440 expr.get_start (),
9441 rhs.get_finish ());
9442 expr = build_x_modify_expr (loc, expr,
9443 assignment_operator,
9444 rhs,
9445 complain_flags (decltype_p));
9446 /* TODO: build_x_modify_expr doesn't honor the location,
9447 so we must set it here. */
9448 expr.set_location (loc);
9453 return expr;
9456 /* Parse an (optional) assignment-operator.
9458 assignment-operator: one of
9459 = *= /= %= += -= >>= <<= &= ^= |=
9461 GNU Extension:
9463 assignment-operator: one of
9464 <?= >?=
9466 If the next token is an assignment operator, the corresponding tree
9467 code is returned, and the token is consumed. For example, for
9468 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9469 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9470 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9471 operator, ERROR_MARK is returned. */
9473 static enum tree_code
9474 cp_parser_assignment_operator_opt (cp_parser* parser)
9476 enum tree_code op;
9477 cp_token *token;
9479 /* Peek at the next token. */
9480 token = cp_lexer_peek_token (parser->lexer);
9482 switch (token->type)
9484 case CPP_EQ:
9485 op = NOP_EXPR;
9486 break;
9488 case CPP_MULT_EQ:
9489 op = MULT_EXPR;
9490 break;
9492 case CPP_DIV_EQ:
9493 op = TRUNC_DIV_EXPR;
9494 break;
9496 case CPP_MOD_EQ:
9497 op = TRUNC_MOD_EXPR;
9498 break;
9500 case CPP_PLUS_EQ:
9501 op = PLUS_EXPR;
9502 break;
9504 case CPP_MINUS_EQ:
9505 op = MINUS_EXPR;
9506 break;
9508 case CPP_RSHIFT_EQ:
9509 op = RSHIFT_EXPR;
9510 break;
9512 case CPP_LSHIFT_EQ:
9513 op = LSHIFT_EXPR;
9514 break;
9516 case CPP_AND_EQ:
9517 op = BIT_AND_EXPR;
9518 break;
9520 case CPP_XOR_EQ:
9521 op = BIT_XOR_EXPR;
9522 break;
9524 case CPP_OR_EQ:
9525 op = BIT_IOR_EXPR;
9526 break;
9528 default:
9529 /* Nothing else is an assignment operator. */
9530 op = ERROR_MARK;
9533 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9534 if (op != ERROR_MARK
9535 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9536 op = ERROR_MARK;
9538 /* If it was an assignment operator, consume it. */
9539 if (op != ERROR_MARK)
9540 cp_lexer_consume_token (parser->lexer);
9542 return op;
9545 /* Parse an expression.
9547 expression:
9548 assignment-expression
9549 expression , assignment-expression
9551 CAST_P is true if this expression is the target of a cast.
9552 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9553 except possibly parenthesized or on the RHS of a comma (N3276).
9555 Returns a representation of the expression. */
9557 static cp_expr
9558 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9559 bool cast_p, bool decltype_p)
9561 cp_expr expression = NULL_TREE;
9562 location_t loc = UNKNOWN_LOCATION;
9564 while (true)
9566 cp_expr assignment_expression;
9568 /* Parse the next assignment-expression. */
9569 assignment_expression
9570 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9572 /* We don't create a temporary for a call that is the immediate operand
9573 of decltype or on the RHS of a comma. But when we see a comma, we
9574 need to create a temporary for a call on the LHS. */
9575 if (decltype_p && !processing_template_decl
9576 && TREE_CODE (assignment_expression) == CALL_EXPR
9577 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9578 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9579 assignment_expression
9580 = build_cplus_new (TREE_TYPE (assignment_expression),
9581 assignment_expression, tf_warning_or_error);
9583 /* If this is the first assignment-expression, we can just
9584 save it away. */
9585 if (!expression)
9586 expression = assignment_expression;
9587 else
9589 /* Create a location with caret at the comma, ranging
9590 from the start of the LHS to the end of the RHS. */
9591 loc = make_location (loc,
9592 expression.get_start (),
9593 assignment_expression.get_finish ());
9594 expression = build_x_compound_expr (loc, expression,
9595 assignment_expression,
9596 complain_flags (decltype_p));
9597 expression.set_location (loc);
9599 /* If the next token is not a comma, or we're in a fold-expression, then
9600 we are done with the expression. */
9601 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9602 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9603 break;
9604 /* Consume the `,'. */
9605 loc = cp_lexer_peek_token (parser->lexer)->location;
9606 cp_lexer_consume_token (parser->lexer);
9607 /* A comma operator cannot appear in a constant-expression. */
9608 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9609 expression = error_mark_node;
9612 return expression;
9615 /* Parse a constant-expression.
9617 constant-expression:
9618 conditional-expression
9620 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9621 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9622 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9623 is false, NON_CONSTANT_P should be NULL. */
9625 static cp_expr
9626 cp_parser_constant_expression (cp_parser* parser,
9627 bool allow_non_constant_p,
9628 bool *non_constant_p)
9630 bool saved_integral_constant_expression_p;
9631 bool saved_allow_non_integral_constant_expression_p;
9632 bool saved_non_integral_constant_expression_p;
9633 cp_expr expression;
9635 /* It might seem that we could simply parse the
9636 conditional-expression, and then check to see if it were
9637 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9638 one that the compiler can figure out is constant, possibly after
9639 doing some simplifications or optimizations. The standard has a
9640 precise definition of constant-expression, and we must honor
9641 that, even though it is somewhat more restrictive.
9643 For example:
9645 int i[(2, 3)];
9647 is not a legal declaration, because `(2, 3)' is not a
9648 constant-expression. The `,' operator is forbidden in a
9649 constant-expression. However, GCC's constant-folding machinery
9650 will fold this operation to an INTEGER_CST for `3'. */
9652 /* Save the old settings. */
9653 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9654 saved_allow_non_integral_constant_expression_p
9655 = parser->allow_non_integral_constant_expression_p;
9656 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9657 /* We are now parsing a constant-expression. */
9658 parser->integral_constant_expression_p = true;
9659 parser->allow_non_integral_constant_expression_p
9660 = (allow_non_constant_p || cxx_dialect >= cxx11);
9661 parser->non_integral_constant_expression_p = false;
9662 /* Although the grammar says "conditional-expression", we parse an
9663 "assignment-expression", which also permits "throw-expression"
9664 and the use of assignment operators. In the case that
9665 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9666 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9667 actually essential that we look for an assignment-expression.
9668 For example, cp_parser_initializer_clauses uses this function to
9669 determine whether a particular assignment-expression is in fact
9670 constant. */
9671 expression = cp_parser_assignment_expression (parser);
9672 /* Restore the old settings. */
9673 parser->integral_constant_expression_p
9674 = saved_integral_constant_expression_p;
9675 parser->allow_non_integral_constant_expression_p
9676 = saved_allow_non_integral_constant_expression_p;
9677 if (cxx_dialect >= cxx11)
9679 /* Require an rvalue constant expression here; that's what our
9680 callers expect. Reference constant expressions are handled
9681 separately in e.g. cp_parser_template_argument. */
9682 tree decay = expression;
9683 if (TREE_TYPE (expression)
9684 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9685 decay = build_address (expression);
9686 bool is_const = potential_rvalue_constant_expression (decay);
9687 parser->non_integral_constant_expression_p = !is_const;
9688 if (!is_const && !allow_non_constant_p)
9689 require_potential_rvalue_constant_expression (decay);
9691 if (allow_non_constant_p)
9692 *non_constant_p = parser->non_integral_constant_expression_p;
9693 parser->non_integral_constant_expression_p
9694 = saved_non_integral_constant_expression_p;
9696 return expression;
9699 /* Parse __builtin_offsetof.
9701 offsetof-expression:
9702 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9704 offsetof-member-designator:
9705 id-expression
9706 | offsetof-member-designator "." id-expression
9707 | offsetof-member-designator "[" expression "]"
9708 | offsetof-member-designator "->" id-expression */
9710 static cp_expr
9711 cp_parser_builtin_offsetof (cp_parser *parser)
9713 int save_ice_p, save_non_ice_p;
9714 tree type;
9715 cp_expr expr;
9716 cp_id_kind dummy;
9717 cp_token *token;
9718 location_t finish_loc;
9720 /* We're about to accept non-integral-constant things, but will
9721 definitely yield an integral constant expression. Save and
9722 restore these values around our local parsing. */
9723 save_ice_p = parser->integral_constant_expression_p;
9724 save_non_ice_p = parser->non_integral_constant_expression_p;
9726 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9728 /* Consume the "__builtin_offsetof" token. */
9729 cp_lexer_consume_token (parser->lexer);
9730 /* Consume the opening `('. */
9731 matching_parens parens;
9732 parens.require_open (parser);
9733 /* Parse the type-id. */
9734 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9735 type = cp_parser_type_id (parser);
9736 /* Look for the `,'. */
9737 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9738 token = cp_lexer_peek_token (parser->lexer);
9740 /* Build the (type *)null that begins the traditional offsetof macro. */
9741 tree object_ptr
9742 = build_static_cast (build_pointer_type (type), null_pointer_node,
9743 tf_warning_or_error);
9745 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9746 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9747 true, &dummy, token->location);
9748 while (true)
9750 token = cp_lexer_peek_token (parser->lexer);
9751 switch (token->type)
9753 case CPP_OPEN_SQUARE:
9754 /* offsetof-member-designator "[" expression "]" */
9755 expr = cp_parser_postfix_open_square_expression (parser, expr,
9756 true, false);
9757 break;
9759 case CPP_DEREF:
9760 /* offsetof-member-designator "->" identifier */
9761 expr = grok_array_decl (token->location, expr,
9762 integer_zero_node, false);
9763 /* FALLTHRU */
9765 case CPP_DOT:
9766 /* offsetof-member-designator "." identifier */
9767 cp_lexer_consume_token (parser->lexer);
9768 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9769 expr, true, &dummy,
9770 token->location);
9771 break;
9773 case CPP_CLOSE_PAREN:
9774 /* Consume the ")" token. */
9775 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9776 cp_lexer_consume_token (parser->lexer);
9777 goto success;
9779 default:
9780 /* Error. We know the following require will fail, but
9781 that gives the proper error message. */
9782 parens.require_close (parser);
9783 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9784 expr = error_mark_node;
9785 goto failure;
9789 success:
9790 /* Make a location of the form:
9791 __builtin_offsetof (struct s, f)
9792 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9793 with caret at the type-id, ranging from the start of the
9794 "_builtin_offsetof" token to the close paren. */
9795 loc = make_location (loc, start_loc, finish_loc);
9796 /* The result will be an INTEGER_CST, so we need to explicitly
9797 preserve the location. */
9798 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9800 failure:
9801 parser->integral_constant_expression_p = save_ice_p;
9802 parser->non_integral_constant_expression_p = save_non_ice_p;
9804 return expr;
9807 /* Parse a trait expression.
9809 Returns a representation of the expression, the underlying type
9810 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9812 static tree
9813 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9815 cp_trait_kind kind;
9816 tree type1, type2 = NULL_TREE;
9817 bool binary = false;
9818 bool variadic = false;
9820 switch (keyword)
9822 case RID_HAS_NOTHROW_ASSIGN:
9823 kind = CPTK_HAS_NOTHROW_ASSIGN;
9824 break;
9825 case RID_HAS_NOTHROW_CONSTRUCTOR:
9826 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9827 break;
9828 case RID_HAS_NOTHROW_COPY:
9829 kind = CPTK_HAS_NOTHROW_COPY;
9830 break;
9831 case RID_HAS_TRIVIAL_ASSIGN:
9832 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9833 break;
9834 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9835 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9836 break;
9837 case RID_HAS_TRIVIAL_COPY:
9838 kind = CPTK_HAS_TRIVIAL_COPY;
9839 break;
9840 case RID_HAS_TRIVIAL_DESTRUCTOR:
9841 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9842 break;
9843 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9844 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9845 break;
9846 case RID_HAS_VIRTUAL_DESTRUCTOR:
9847 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9848 break;
9849 case RID_IS_ABSTRACT:
9850 kind = CPTK_IS_ABSTRACT;
9851 break;
9852 case RID_IS_AGGREGATE:
9853 kind = CPTK_IS_AGGREGATE;
9854 break;
9855 case RID_IS_BASE_OF:
9856 kind = CPTK_IS_BASE_OF;
9857 binary = true;
9858 break;
9859 case RID_IS_CLASS:
9860 kind = CPTK_IS_CLASS;
9861 break;
9862 case RID_IS_EMPTY:
9863 kind = CPTK_IS_EMPTY;
9864 break;
9865 case RID_IS_ENUM:
9866 kind = CPTK_IS_ENUM;
9867 break;
9868 case RID_IS_FINAL:
9869 kind = CPTK_IS_FINAL;
9870 break;
9871 case RID_IS_LITERAL_TYPE:
9872 kind = CPTK_IS_LITERAL_TYPE;
9873 break;
9874 case RID_IS_POD:
9875 kind = CPTK_IS_POD;
9876 break;
9877 case RID_IS_POLYMORPHIC:
9878 kind = CPTK_IS_POLYMORPHIC;
9879 break;
9880 case RID_IS_SAME_AS:
9881 kind = CPTK_IS_SAME_AS;
9882 binary = true;
9883 break;
9884 case RID_IS_STD_LAYOUT:
9885 kind = CPTK_IS_STD_LAYOUT;
9886 break;
9887 case RID_IS_TRIVIAL:
9888 kind = CPTK_IS_TRIVIAL;
9889 break;
9890 case RID_IS_TRIVIALLY_ASSIGNABLE:
9891 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9892 binary = true;
9893 break;
9894 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9895 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9896 variadic = true;
9897 break;
9898 case RID_IS_TRIVIALLY_COPYABLE:
9899 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9900 break;
9901 case RID_IS_UNION:
9902 kind = CPTK_IS_UNION;
9903 break;
9904 case RID_UNDERLYING_TYPE:
9905 kind = CPTK_UNDERLYING_TYPE;
9906 break;
9907 case RID_BASES:
9908 kind = CPTK_BASES;
9909 break;
9910 case RID_DIRECT_BASES:
9911 kind = CPTK_DIRECT_BASES;
9912 break;
9913 case RID_IS_ASSIGNABLE:
9914 kind = CPTK_IS_ASSIGNABLE;
9915 binary = true;
9916 break;
9917 case RID_IS_CONSTRUCTIBLE:
9918 kind = CPTK_IS_CONSTRUCTIBLE;
9919 variadic = true;
9920 break;
9921 default:
9922 gcc_unreachable ();
9925 /* Consume the token. */
9926 cp_lexer_consume_token (parser->lexer);
9928 matching_parens parens;
9929 parens.require_open (parser);
9932 type_id_in_expr_sentinel s (parser);
9933 type1 = cp_parser_type_id (parser);
9936 if (type1 == error_mark_node)
9937 return error_mark_node;
9939 if (binary)
9941 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9944 type_id_in_expr_sentinel s (parser);
9945 type2 = cp_parser_type_id (parser);
9948 if (type2 == error_mark_node)
9949 return error_mark_node;
9951 else if (variadic)
9953 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9955 cp_lexer_consume_token (parser->lexer);
9956 tree elt = cp_parser_type_id (parser);
9957 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9959 cp_lexer_consume_token (parser->lexer);
9960 elt = make_pack_expansion (elt);
9962 if (elt == error_mark_node)
9963 return error_mark_node;
9964 type2 = tree_cons (NULL_TREE, elt, type2);
9968 parens.require_close (parser);
9970 /* Complete the trait expression, which may mean either processing
9971 the trait expr now or saving it for template instantiation. */
9972 switch (kind)
9974 case CPTK_UNDERLYING_TYPE:
9975 return finish_underlying_type (type1);
9976 case CPTK_BASES:
9977 return finish_bases (type1, false);
9978 case CPTK_DIRECT_BASES:
9979 return finish_bases (type1, true);
9980 default:
9981 return finish_trait_expr (kind, type1, type2);
9985 /* Lambdas that appear in variable initializer or default argument scope
9986 get that in their mangling, so we need to record it. We might as well
9987 use the count for function and namespace scopes as well. */
9988 static GTY(()) tree lambda_scope;
9989 static GTY(()) int lambda_count;
9990 struct GTY(()) tree_int
9992 tree t;
9993 int i;
9995 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9997 static void
9998 start_lambda_scope (tree decl)
10000 tree_int ti;
10001 gcc_assert (decl);
10002 /* Once we're inside a function, we ignore other scopes and just push
10003 the function again so that popping works properly. */
10004 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
10005 decl = current_function_decl;
10006 ti.t = lambda_scope;
10007 ti.i = lambda_count;
10008 vec_safe_push (lambda_scope_stack, ti);
10009 if (lambda_scope != decl)
10011 /* Don't reset the count if we're still in the same function. */
10012 lambda_scope = decl;
10013 lambda_count = 0;
10017 static void
10018 record_lambda_scope (tree lambda)
10020 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
10021 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
10024 static void
10025 finish_lambda_scope (void)
10027 tree_int *p = &lambda_scope_stack->last ();
10028 if (lambda_scope != p->t)
10030 lambda_scope = p->t;
10031 lambda_count = p->i;
10033 lambda_scope_stack->pop ();
10036 /* Parse a lambda expression.
10038 lambda-expression:
10039 lambda-introducer lambda-declarator [opt] compound-statement
10041 Returns a representation of the expression. */
10043 static cp_expr
10044 cp_parser_lambda_expression (cp_parser* parser)
10046 tree lambda_expr = build_lambda_expr ();
10047 tree type;
10048 bool ok = true;
10049 cp_token *token = cp_lexer_peek_token (parser->lexer);
10050 cp_token_position start = 0;
10052 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
10054 if (cp_unevaluated_operand)
10056 if (!token->error_reported)
10058 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
10059 "lambda-expression in unevaluated context");
10060 token->error_reported = true;
10062 ok = false;
10064 else if (parser->in_template_argument_list_p)
10066 if (!token->error_reported)
10068 error_at (token->location, "lambda-expression in template-argument");
10069 token->error_reported = true;
10071 ok = false;
10074 /* We may be in the middle of deferred access check. Disable
10075 it now. */
10076 push_deferring_access_checks (dk_no_deferred);
10078 cp_parser_lambda_introducer (parser, lambda_expr);
10080 type = begin_lambda_type (lambda_expr);
10081 if (type == error_mark_node)
10082 return error_mark_node;
10084 record_lambda_scope (lambda_expr);
10086 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
10087 determine_visibility (TYPE_NAME (type));
10089 /* Now that we've started the type, add the capture fields for any
10090 explicit captures. */
10091 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10094 /* Inside the class, surrounding template-parameter-lists do not apply. */
10095 unsigned int saved_num_template_parameter_lists
10096 = parser->num_template_parameter_lists;
10097 unsigned char in_statement = parser->in_statement;
10098 bool in_switch_statement_p = parser->in_switch_statement_p;
10099 bool fully_implicit_function_template_p
10100 = parser->fully_implicit_function_template_p;
10101 tree implicit_template_parms = parser->implicit_template_parms;
10102 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10103 bool auto_is_implicit_function_template_parm_p
10104 = parser->auto_is_implicit_function_template_parm_p;
10106 parser->num_template_parameter_lists = 0;
10107 parser->in_statement = 0;
10108 parser->in_switch_statement_p = false;
10109 parser->fully_implicit_function_template_p = false;
10110 parser->implicit_template_parms = 0;
10111 parser->implicit_template_scope = 0;
10112 parser->auto_is_implicit_function_template_parm_p = false;
10114 /* By virtue of defining a local class, a lambda expression has access to
10115 the private variables of enclosing classes. */
10117 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10119 if (ok && cp_parser_error_occurred (parser))
10120 ok = false;
10122 if (ok)
10124 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10125 && cp_parser_start_tentative_firewall (parser))
10126 start = token;
10127 cp_parser_lambda_body (parser, lambda_expr);
10129 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10131 if (cp_parser_skip_to_closing_brace (parser))
10132 cp_lexer_consume_token (parser->lexer);
10135 /* The capture list was built up in reverse order; fix that now. */
10136 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10137 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10139 if (ok)
10140 maybe_add_lambda_conv_op (type);
10142 type = finish_struct (type, /*attributes=*/NULL_TREE);
10144 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10145 parser->in_statement = in_statement;
10146 parser->in_switch_statement_p = in_switch_statement_p;
10147 parser->fully_implicit_function_template_p
10148 = fully_implicit_function_template_p;
10149 parser->implicit_template_parms = implicit_template_parms;
10150 parser->implicit_template_scope = implicit_template_scope;
10151 parser->auto_is_implicit_function_template_parm_p
10152 = auto_is_implicit_function_template_parm_p;
10155 /* This field is only used during parsing of the lambda. */
10156 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10158 /* This lambda shouldn't have any proxies left at this point. */
10159 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10160 /* And now that we're done, push proxies for an enclosing lambda. */
10161 insert_pending_capture_proxies ();
10163 if (ok)
10164 lambda_expr = build_lambda_object (lambda_expr);
10165 else
10166 lambda_expr = error_mark_node;
10168 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10170 pop_deferring_access_checks ();
10172 return lambda_expr;
10175 /* Parse the beginning of a lambda expression.
10177 lambda-introducer:
10178 [ lambda-capture [opt] ]
10180 LAMBDA_EXPR is the current representation of the lambda expression. */
10182 static void
10183 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10185 /* Need commas after the first capture. */
10186 bool first = true;
10188 /* Eat the leading `['. */
10189 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10191 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10192 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10193 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10194 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10195 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10196 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10198 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10200 cp_lexer_consume_token (parser->lexer);
10201 first = false;
10204 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10206 cp_token* capture_token;
10207 tree capture_id;
10208 tree capture_init_expr;
10209 cp_id_kind idk = CP_ID_KIND_NONE;
10210 bool explicit_init_p = false;
10212 enum capture_kind_type
10214 BY_COPY,
10215 BY_REFERENCE
10217 enum capture_kind_type capture_kind = BY_COPY;
10219 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10221 error ("expected end of capture-list");
10222 return;
10225 if (first)
10226 first = false;
10227 else
10228 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10230 /* Possibly capture `this'. */
10231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10233 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10234 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10235 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10236 "with by-copy capture default");
10237 cp_lexer_consume_token (parser->lexer);
10238 add_capture (lambda_expr,
10239 /*id=*/this_identifier,
10240 /*initializer=*/finish_this_expr (),
10241 /*by_reference_p=*/true,
10242 explicit_init_p);
10243 continue;
10246 /* Possibly capture `*this'. */
10247 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10248 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10250 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10251 if (cxx_dialect < cxx1z)
10252 pedwarn (loc, 0, "%<*this%> capture only available with "
10253 "-std=c++1z or -std=gnu++1z");
10254 cp_lexer_consume_token (parser->lexer);
10255 cp_lexer_consume_token (parser->lexer);
10256 add_capture (lambda_expr,
10257 /*id=*/this_identifier,
10258 /*initializer=*/finish_this_expr (),
10259 /*by_reference_p=*/false,
10260 explicit_init_p);
10261 continue;
10264 /* Remember whether we want to capture as a reference or not. */
10265 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10267 capture_kind = BY_REFERENCE;
10268 cp_lexer_consume_token (parser->lexer);
10271 /* Get the identifier. */
10272 capture_token = cp_lexer_peek_token (parser->lexer);
10273 capture_id = cp_parser_identifier (parser);
10275 if (capture_id == error_mark_node)
10276 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10277 delimiters, but I modified this to stop on unnested ']' as well. It
10278 was already changed to stop on unnested '}', so the
10279 "closing_parenthesis" name is no more misleading with my change. */
10281 cp_parser_skip_to_closing_parenthesis (parser,
10282 /*recovering=*/true,
10283 /*or_comma=*/true,
10284 /*consume_paren=*/true);
10285 break;
10288 /* Find the initializer for this capture. */
10289 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10290 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10291 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10293 bool direct, non_constant;
10294 /* An explicit initializer exists. */
10295 if (cxx_dialect < cxx14)
10296 pedwarn (input_location, 0,
10297 "lambda capture initializers "
10298 "only available with -std=c++14 or -std=gnu++14");
10299 capture_init_expr = cp_parser_initializer (parser, &direct,
10300 &non_constant);
10301 explicit_init_p = true;
10302 if (capture_init_expr == NULL_TREE)
10304 error ("empty initializer for lambda init-capture");
10305 capture_init_expr = error_mark_node;
10308 else
10310 const char* error_msg;
10312 /* Turn the identifier into an id-expression. */
10313 capture_init_expr
10314 = cp_parser_lookup_name_simple (parser, capture_id,
10315 capture_token->location);
10317 if (capture_init_expr == error_mark_node)
10319 unqualified_name_lookup_error (capture_id);
10320 continue;
10322 else if (DECL_P (capture_init_expr)
10323 && (!VAR_P (capture_init_expr)
10324 && TREE_CODE (capture_init_expr) != PARM_DECL))
10326 error_at (capture_token->location,
10327 "capture of non-variable %qD ",
10328 capture_init_expr);
10329 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10330 "%q#D declared here", capture_init_expr);
10331 continue;
10333 if (VAR_P (capture_init_expr)
10334 && decl_storage_duration (capture_init_expr) != dk_auto)
10336 if (pedwarn (capture_token->location, 0, "capture of variable "
10337 "%qD with non-automatic storage duration",
10338 capture_init_expr))
10339 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10340 "%q#D declared here", capture_init_expr);
10341 continue;
10344 capture_init_expr
10345 = finish_id_expression
10346 (capture_id,
10347 capture_init_expr,
10348 parser->scope,
10349 &idk,
10350 /*integral_constant_expression_p=*/false,
10351 /*allow_non_integral_constant_expression_p=*/false,
10352 /*non_integral_constant_expression_p=*/NULL,
10353 /*template_p=*/false,
10354 /*done=*/true,
10355 /*address_p=*/false,
10356 /*template_arg_p=*/false,
10357 &error_msg,
10358 capture_token->location);
10360 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10362 cp_lexer_consume_token (parser->lexer);
10363 capture_init_expr = make_pack_expansion (capture_init_expr);
10365 else
10366 check_for_bare_parameter_packs (capture_init_expr);
10369 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10370 && !explicit_init_p)
10372 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10373 && capture_kind == BY_COPY)
10374 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10375 "of %qD redundant with by-copy capture default",
10376 capture_id);
10377 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10378 && capture_kind == BY_REFERENCE)
10379 pedwarn (capture_token->location, 0, "explicit by-reference "
10380 "capture of %qD redundant with by-reference capture "
10381 "default", capture_id);
10384 add_capture (lambda_expr,
10385 capture_id,
10386 capture_init_expr,
10387 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10388 explicit_init_p);
10391 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10394 /* Parse the (optional) middle of a lambda expression.
10396 lambda-declarator:
10397 < template-parameter-list [opt] >
10398 ( parameter-declaration-clause [opt] )
10399 attribute-specifier [opt]
10400 decl-specifier-seq [opt]
10401 exception-specification [opt]
10402 lambda-return-type-clause [opt]
10404 LAMBDA_EXPR is the current representation of the lambda expression. */
10406 static bool
10407 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10409 /* 5.1.1.4 of the standard says:
10410 If a lambda-expression does not include a lambda-declarator, it is as if
10411 the lambda-declarator were ().
10412 This means an empty parameter list, no attributes, and no exception
10413 specification. */
10414 tree param_list = void_list_node;
10415 tree attributes = NULL_TREE;
10416 tree exception_spec = NULL_TREE;
10417 tree template_param_list = NULL_TREE;
10418 tree tx_qual = NULL_TREE;
10419 cp_decl_specifier_seq lambda_specs;
10420 clear_decl_specs (&lambda_specs);
10422 /* The template-parameter-list is optional, but must begin with
10423 an opening angle if present. */
10424 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10426 if (cxx_dialect < cxx14)
10427 pedwarn (parser->lexer->next_token->location, 0,
10428 "lambda templates are only available with "
10429 "-std=c++14 or -std=gnu++14");
10430 else
10431 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10432 "ISO C++ does not support lambda templates");
10434 cp_lexer_consume_token (parser->lexer);
10436 template_param_list = cp_parser_template_parameter_list (parser);
10438 cp_parser_skip_to_end_of_template_parameter_list (parser);
10440 /* We just processed one more parameter list. */
10441 ++parser->num_template_parameter_lists;
10444 /* The parameter-declaration-clause is optional (unless
10445 template-parameter-list was given), but must begin with an
10446 opening parenthesis if present. */
10447 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10449 matching_parens parens;
10450 parens.consume_open (parser);
10452 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10454 /* Parse parameters. */
10455 param_list = cp_parser_parameter_declaration_clause (parser);
10457 /* Default arguments shall not be specified in the
10458 parameter-declaration-clause of a lambda-declarator. */
10459 if (cxx_dialect < cxx14)
10460 for (tree t = param_list; t; t = TREE_CHAIN (t))
10461 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10462 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10463 "default argument specified for lambda parameter");
10465 parens.require_close (parser);
10467 attributes = cp_parser_attributes_opt (parser);
10469 /* In the decl-specifier-seq of the lambda-declarator, each
10470 decl-specifier shall either be mutable or constexpr. */
10471 int declares_class_or_enum;
10472 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10473 cp_parser_decl_specifier_seq (parser,
10474 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10475 &lambda_specs, &declares_class_or_enum);
10476 if (lambda_specs.storage_class == sc_mutable)
10478 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10479 if (lambda_specs.conflicting_specifiers_p)
10480 error_at (lambda_specs.locations[ds_storage_class],
10481 "duplicate %<mutable%>");
10484 tx_qual = cp_parser_tx_qualifier_opt (parser);
10486 /* Parse optional exception specification. */
10487 exception_spec = cp_parser_exception_specification_opt (parser);
10489 /* Parse optional trailing return type. */
10490 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10492 cp_lexer_consume_token (parser->lexer);
10493 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10494 = cp_parser_trailing_type_id (parser);
10497 /* The function parameters must be in scope all the way until after the
10498 trailing-return-type in case of decltype. */
10499 pop_bindings_and_leave_scope ();
10501 else if (template_param_list != NULL_TREE) // generate diagnostic
10502 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10504 /* Create the function call operator.
10506 Messing with declarators like this is no uglier than building up the
10507 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10508 other code. */
10510 cp_decl_specifier_seq return_type_specs;
10511 cp_declarator* declarator;
10512 tree fco;
10513 int quals;
10514 void *p;
10516 clear_decl_specs (&return_type_specs);
10517 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10518 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10519 else
10520 /* Maybe we will deduce the return type later. */
10521 return_type_specs.type = make_auto ();
10523 if (lambda_specs.locations[ds_constexpr])
10525 if (cxx_dialect >= cxx1z)
10526 return_type_specs.locations[ds_constexpr]
10527 = lambda_specs.locations[ds_constexpr];
10528 else
10529 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10530 "lambda only available with -std=c++1z or -std=gnu++1z");
10533 p = obstack_alloc (&declarator_obstack, 0);
10535 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10536 sfk_none);
10538 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10539 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10540 declarator = make_call_declarator (declarator, param_list, quals,
10541 VIRT_SPEC_UNSPECIFIED,
10542 REF_QUAL_NONE,
10543 tx_qual,
10544 exception_spec,
10545 /*late_return_type=*/NULL_TREE,
10546 /*requires_clause*/NULL_TREE);
10547 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10549 fco = grokmethod (&return_type_specs,
10550 declarator,
10551 attributes);
10552 if (fco != error_mark_node)
10554 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10555 DECL_ARTIFICIAL (fco) = 1;
10556 /* Give the object parameter a different name. */
10557 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10558 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10559 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10561 if (template_param_list)
10563 fco = finish_member_template_decl (fco);
10564 finish_template_decl (template_param_list);
10565 --parser->num_template_parameter_lists;
10567 else if (parser->fully_implicit_function_template_p)
10568 fco = finish_fully_implicit_template (parser, fco);
10570 finish_member_declaration (fco);
10572 obstack_free (&declarator_obstack, p);
10574 return (fco != error_mark_node);
10578 /* Parse the body of a lambda expression, which is simply
10580 compound-statement
10582 but which requires special handling.
10583 LAMBDA_EXPR is the current representation of the lambda expression. */
10585 static void
10586 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10588 bool nested = (current_function_decl != NULL_TREE);
10589 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10590 if (nested)
10591 push_function_context ();
10592 else
10593 /* Still increment function_depth so that we don't GC in the
10594 middle of an expression. */
10595 ++function_depth;
10596 vec<tree> omp_privatization_save;
10597 save_omp_privatization_clauses (omp_privatization_save);
10598 /* Clear this in case we're in the middle of a default argument. */
10599 parser->local_variables_forbidden_p = false;
10601 /* Finish the function call operator
10602 - class_specifier
10603 + late_parsing_for_member
10604 + function_definition_after_declarator
10605 + ctor_initializer_opt_and_function_body */
10607 tree fco = lambda_function (lambda_expr);
10608 tree body;
10609 bool done = false;
10610 tree compound_stmt;
10611 tree cap;
10613 /* Let the front end know that we are going to be defining this
10614 function. */
10615 start_preparsed_function (fco,
10616 NULL_TREE,
10617 SF_PRE_PARSED | SF_INCLASS_INLINE);
10619 start_lambda_scope (fco);
10620 body = begin_function_body ();
10622 matching_braces braces;
10623 if (!braces.require_open (parser))
10624 goto out;
10626 /* Push the proxies for any explicit captures. */
10627 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10628 cap = TREE_CHAIN (cap))
10629 build_capture_proxy (TREE_PURPOSE (cap));
10631 compound_stmt = begin_compound_stmt (0);
10633 /* 5.1.1.4 of the standard says:
10634 If a lambda-expression does not include a trailing-return-type, it
10635 is as if the trailing-return-type denotes the following type:
10636 * if the compound-statement is of the form
10637 { return attribute-specifier [opt] expression ; }
10638 the type of the returned expression after lvalue-to-rvalue
10639 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10640 (_conv.array_ 4.2), and function-to-pointer conversion
10641 (_conv.func_ 4.3);
10642 * otherwise, void. */
10644 /* In a lambda that has neither a lambda-return-type-clause
10645 nor a deducible form, errors should be reported for return statements
10646 in the body. Since we used void as the placeholder return type, parsing
10647 the body as usual will give such desired behavior. */
10648 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10649 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10650 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10652 tree expr = NULL_TREE;
10653 cp_id_kind idk = CP_ID_KIND_NONE;
10655 /* Parse tentatively in case there's more after the initial return
10656 statement. */
10657 cp_parser_parse_tentatively (parser);
10659 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10661 expr = cp_parser_expression (parser, &idk);
10663 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10664 braces.require_close (parser);
10666 if (cp_parser_parse_definitely (parser))
10668 if (!processing_template_decl)
10670 tree type = lambda_return_type (expr);
10671 apply_deduced_return_type (fco, type);
10672 if (type == error_mark_node)
10673 expr = error_mark_node;
10676 /* Will get error here if type not deduced yet. */
10677 finish_return_stmt (expr);
10679 done = true;
10683 if (!done)
10685 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10686 cp_parser_label_declaration (parser);
10687 cp_parser_statement_seq_opt (parser, NULL_TREE);
10688 braces.require_close (parser);
10691 finish_compound_stmt (compound_stmt);
10693 out:
10694 finish_function_body (body);
10695 finish_lambda_scope ();
10697 /* Finish the function and generate code for it if necessary. */
10698 tree fn = finish_function (/*inline*/2);
10700 /* Only expand if the call op is not a template. */
10701 if (!DECL_TEMPLATE_INFO (fco))
10702 expand_or_defer_fn (fn);
10705 restore_omp_privatization_clauses (omp_privatization_save);
10706 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10707 if (nested)
10708 pop_function_context();
10709 else
10710 --function_depth;
10713 /* Statements [gram.stmt.stmt] */
10715 /* Parse a statement.
10717 statement:
10718 labeled-statement
10719 expression-statement
10720 compound-statement
10721 selection-statement
10722 iteration-statement
10723 jump-statement
10724 declaration-statement
10725 try-block
10727 C++11:
10729 statement:
10730 labeled-statement
10731 attribute-specifier-seq (opt) expression-statement
10732 attribute-specifier-seq (opt) compound-statement
10733 attribute-specifier-seq (opt) selection-statement
10734 attribute-specifier-seq (opt) iteration-statement
10735 attribute-specifier-seq (opt) jump-statement
10736 declaration-statement
10737 attribute-specifier-seq (opt) try-block
10739 init-statement:
10740 expression-statement
10741 simple-declaration
10743 TM Extension:
10745 statement:
10746 atomic-statement
10748 IN_COMPOUND is true when the statement is nested inside a
10749 cp_parser_compound_statement; this matters for certain pragmas.
10751 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10752 is a (possibly labeled) if statement which is not enclosed in braces
10753 and has an else clause. This is used to implement -Wparentheses.
10755 CHAIN is a vector of if-else-if conditions. */
10757 static void
10758 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10759 bool in_compound, bool *if_p, vec<tree> *chain,
10760 location_t *loc_after_labels)
10762 tree statement, std_attrs = NULL_TREE;
10763 cp_token *token;
10764 location_t statement_location, attrs_location;
10766 restart:
10767 if (if_p != NULL)
10768 *if_p = false;
10769 /* There is no statement yet. */
10770 statement = NULL_TREE;
10772 saved_token_sentinel saved_tokens (parser->lexer);
10773 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10774 if (c_dialect_objc ())
10775 /* In obj-c++, seeing '[[' might be the either the beginning of
10776 c++11 attributes, or a nested objc-message-expression. So
10777 let's parse the c++11 attributes tentatively. */
10778 cp_parser_parse_tentatively (parser);
10779 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10780 if (c_dialect_objc ())
10782 if (!cp_parser_parse_definitely (parser))
10783 std_attrs = NULL_TREE;
10786 /* Peek at the next token. */
10787 token = cp_lexer_peek_token (parser->lexer);
10788 /* Remember the location of the first token in the statement. */
10789 statement_location = token->location;
10790 /* If this is a keyword, then that will often determine what kind of
10791 statement we have. */
10792 if (token->type == CPP_KEYWORD)
10794 enum rid keyword = token->keyword;
10796 switch (keyword)
10798 case RID_CASE:
10799 case RID_DEFAULT:
10800 /* Looks like a labeled-statement with a case label.
10801 Parse the label, and then use tail recursion to parse
10802 the statement. */
10803 cp_parser_label_for_labeled_statement (parser, std_attrs);
10804 in_compound = false;
10805 goto restart;
10807 case RID_IF:
10808 case RID_SWITCH:
10809 statement = cp_parser_selection_statement (parser, if_p, chain);
10810 break;
10812 case RID_WHILE:
10813 case RID_DO:
10814 case RID_FOR:
10815 statement = cp_parser_iteration_statement (parser, if_p, false);
10816 break;
10818 case RID_CILK_FOR:
10819 if (!flag_cilkplus)
10821 error_at (cp_lexer_peek_token (parser->lexer)->location,
10822 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10823 cp_lexer_consume_token (parser->lexer);
10824 statement = error_mark_node;
10826 else
10827 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10828 break;
10830 case RID_BREAK:
10831 case RID_CONTINUE:
10832 case RID_RETURN:
10833 case RID_GOTO:
10834 statement = cp_parser_jump_statement (parser);
10835 break;
10837 case RID_CILK_SYNC:
10838 cp_lexer_consume_token (parser->lexer);
10839 if (flag_cilkplus)
10841 tree sync_expr = build_cilk_sync ();
10842 SET_EXPR_LOCATION (sync_expr,
10843 token->location);
10844 statement = finish_expr_stmt (sync_expr);
10846 else
10848 error_at (token->location, "-fcilkplus must be enabled to use"
10849 " %<_Cilk_sync%>");
10850 statement = error_mark_node;
10852 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10853 break;
10855 /* Objective-C++ exception-handling constructs. */
10856 case RID_AT_TRY:
10857 case RID_AT_CATCH:
10858 case RID_AT_FINALLY:
10859 case RID_AT_SYNCHRONIZED:
10860 case RID_AT_THROW:
10861 statement = cp_parser_objc_statement (parser);
10862 break;
10864 case RID_TRY:
10865 statement = cp_parser_try_block (parser);
10866 break;
10868 case RID_NAMESPACE:
10869 /* This must be a namespace alias definition. */
10870 cp_parser_declaration_statement (parser);
10871 return;
10873 case RID_TRANSACTION_ATOMIC:
10874 case RID_TRANSACTION_RELAXED:
10875 case RID_SYNCHRONIZED:
10876 case RID_ATOMIC_NOEXCEPT:
10877 case RID_ATOMIC_CANCEL:
10878 statement = cp_parser_transaction (parser, token);
10879 break;
10880 case RID_TRANSACTION_CANCEL:
10881 statement = cp_parser_transaction_cancel (parser);
10882 break;
10884 default:
10885 /* It might be a keyword like `int' that can start a
10886 declaration-statement. */
10887 break;
10890 else if (token->type == CPP_NAME)
10892 /* If the next token is a `:', then we are looking at a
10893 labeled-statement. */
10894 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10895 if (token->type == CPP_COLON)
10897 /* Looks like a labeled-statement with an ordinary label.
10898 Parse the label, and then use tail recursion to parse
10899 the statement. */
10901 cp_parser_label_for_labeled_statement (parser, std_attrs);
10902 in_compound = false;
10903 goto restart;
10906 /* Anything that starts with a `{' must be a compound-statement. */
10907 else if (token->type == CPP_OPEN_BRACE)
10908 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10909 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10910 a statement all its own. */
10911 else if (token->type == CPP_PRAGMA)
10913 /* Only certain OpenMP pragmas are attached to statements, and thus
10914 are considered statements themselves. All others are not. In
10915 the context of a compound, accept the pragma as a "statement" and
10916 return so that we can check for a close brace. Otherwise we
10917 require a real statement and must go back and read one. */
10918 if (in_compound)
10919 cp_parser_pragma (parser, pragma_compound, if_p);
10920 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10921 goto restart;
10922 return;
10924 else if (token->type == CPP_EOF)
10926 cp_parser_error (parser, "expected statement");
10927 return;
10930 /* Everything else must be a declaration-statement or an
10931 expression-statement. Try for the declaration-statement
10932 first, unless we are looking at a `;', in which case we know that
10933 we have an expression-statement. */
10934 if (!statement)
10936 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10938 if (std_attrs != NULL_TREE)
10940 /* Attributes should be parsed as part of the the
10941 declaration, so let's un-parse them. */
10942 saved_tokens.rollback();
10943 std_attrs = NULL_TREE;
10946 cp_parser_parse_tentatively (parser);
10947 /* Try to parse the declaration-statement. */
10948 cp_parser_declaration_statement (parser);
10949 /* If that worked, we're done. */
10950 if (cp_parser_parse_definitely (parser))
10951 return;
10953 /* All preceding labels have been parsed at this point. */
10954 if (loc_after_labels != NULL)
10955 *loc_after_labels = statement_location;
10957 /* Look for an expression-statement instead. */
10958 statement = cp_parser_expression_statement (parser, in_statement_expr);
10960 /* Handle [[fallthrough]];. */
10961 if (attribute_fallthrough_p (std_attrs))
10963 /* The next token after the fallthrough attribute is ';'. */
10964 if (statement == NULL_TREE)
10966 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10967 statement = build_call_expr_internal_loc (statement_location,
10968 IFN_FALLTHROUGH,
10969 void_type_node, 0);
10970 finish_expr_stmt (statement);
10972 else
10973 warning_at (statement_location, OPT_Wattributes,
10974 "%<fallthrough%> attribute not followed by %<;%>");
10975 std_attrs = NULL_TREE;
10979 /* Set the line number for the statement. */
10980 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10981 SET_EXPR_LOCATION (statement, statement_location);
10983 /* Allow "[[fallthrough]];", but warn otherwise. */
10984 if (std_attrs != NULL_TREE)
10985 warning_at (attrs_location,
10986 OPT_Wattributes,
10987 "attributes at the beginning of statement are ignored");
10990 /* Parse the label for a labeled-statement, i.e.
10992 identifier :
10993 case constant-expression :
10994 default :
10996 GNU Extension:
10997 case constant-expression ... constant-expression : statement
10999 When a label is parsed without errors, the label is added to the
11000 parse tree by the finish_* functions, so this function doesn't
11001 have to return the label. */
11003 static void
11004 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
11006 cp_token *token;
11007 tree label = NULL_TREE;
11008 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
11010 /* The next token should be an identifier. */
11011 token = cp_lexer_peek_token (parser->lexer);
11012 if (token->type != CPP_NAME
11013 && token->type != CPP_KEYWORD)
11015 cp_parser_error (parser, "expected labeled-statement");
11016 return;
11019 /* Remember whether this case or a user-defined label is allowed to fall
11020 through to. */
11021 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
11023 parser->colon_corrects_to_scope_p = false;
11024 switch (token->keyword)
11026 case RID_CASE:
11028 tree expr, expr_hi;
11029 cp_token *ellipsis;
11031 /* Consume the `case' token. */
11032 cp_lexer_consume_token (parser->lexer);
11033 /* Parse the constant-expression. */
11034 expr = cp_parser_constant_expression (parser);
11035 if (check_for_bare_parameter_packs (expr))
11036 expr = error_mark_node;
11038 ellipsis = cp_lexer_peek_token (parser->lexer);
11039 if (ellipsis->type == CPP_ELLIPSIS)
11041 /* Consume the `...' token. */
11042 cp_lexer_consume_token (parser->lexer);
11043 expr_hi = cp_parser_constant_expression (parser);
11044 if (check_for_bare_parameter_packs (expr_hi))
11045 expr_hi = error_mark_node;
11047 /* We don't need to emit warnings here, as the common code
11048 will do this for us. */
11050 else
11051 expr_hi = NULL_TREE;
11053 if (parser->in_switch_statement_p)
11055 tree l = finish_case_label (token->location, expr, expr_hi);
11056 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11057 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11059 else
11060 error_at (token->location,
11061 "case label %qE not within a switch statement",
11062 expr);
11064 break;
11066 case RID_DEFAULT:
11067 /* Consume the `default' token. */
11068 cp_lexer_consume_token (parser->lexer);
11070 if (parser->in_switch_statement_p)
11072 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
11073 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
11074 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
11076 else
11077 error_at (token->location, "case label not within a switch statement");
11078 break;
11080 default:
11081 /* Anything else must be an ordinary label. */
11082 label = finish_label_stmt (cp_parser_identifier (parser));
11083 if (label && TREE_CODE (label) == LABEL_DECL)
11084 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
11085 break;
11088 /* Require the `:' token. */
11089 cp_parser_require (parser, CPP_COLON, RT_COLON);
11091 /* An ordinary label may optionally be followed by attributes.
11092 However, this is only permitted if the attributes are then
11093 followed by a semicolon. This is because, for backward
11094 compatibility, when parsing
11095 lab: __attribute__ ((unused)) int i;
11096 we want the attribute to attach to "i", not "lab". */
11097 if (label != NULL_TREE
11098 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11100 tree attrs;
11101 cp_parser_parse_tentatively (parser);
11102 attrs = cp_parser_gnu_attributes_opt (parser);
11103 if (attrs == NULL_TREE
11104 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11105 cp_parser_abort_tentative_parse (parser);
11106 else if (!cp_parser_parse_definitely (parser))
11108 else
11109 attributes = chainon (attributes, attrs);
11112 if (attributes != NULL_TREE)
11113 cplus_decl_attributes (&label, attributes, 0);
11115 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11118 /* Parse an expression-statement.
11120 expression-statement:
11121 expression [opt] ;
11123 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11124 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11125 indicates whether this expression-statement is part of an
11126 expression statement. */
11128 static tree
11129 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11131 tree statement = NULL_TREE;
11132 cp_token *token = cp_lexer_peek_token (parser->lexer);
11133 location_t loc = token->location;
11135 /* There might be attribute fallthrough. */
11136 tree attr = cp_parser_gnu_attributes_opt (parser);
11138 /* If the next token is a ';', then there is no expression
11139 statement. */
11140 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11142 statement = cp_parser_expression (parser);
11143 if (statement == error_mark_node
11144 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11146 cp_parser_skip_to_end_of_block_or_statement (parser);
11147 return error_mark_node;
11151 /* Handle [[fallthrough]];. */
11152 if (attribute_fallthrough_p (attr))
11154 /* The next token after the fallthrough attribute is ';'. */
11155 if (statement == NULL_TREE)
11156 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11157 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11158 void_type_node, 0);
11159 else
11160 warning_at (loc, OPT_Wattributes,
11161 "%<fallthrough%> attribute not followed by %<;%>");
11162 attr = NULL_TREE;
11165 /* Allow "[[fallthrough]];", but warn otherwise. */
11166 if (attr != NULL_TREE)
11167 warning_at (loc, OPT_Wattributes,
11168 "attributes at the beginning of statement are ignored");
11170 /* Give a helpful message for "A<T>::type t;" and the like. */
11171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11172 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11174 if (TREE_CODE (statement) == SCOPE_REF)
11175 error_at (token->location, "need %<typename%> before %qE because "
11176 "%qT is a dependent scope",
11177 statement, TREE_OPERAND (statement, 0));
11178 else if (is_overloaded_fn (statement)
11179 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11181 /* A::A a; */
11182 tree fn = get_first_fn (statement);
11183 error_at (token->location,
11184 "%<%T::%D%> names the constructor, not the type",
11185 DECL_CONTEXT (fn), DECL_NAME (fn));
11189 /* Consume the final `;'. */
11190 cp_parser_consume_semicolon_at_end_of_statement (parser);
11192 if (in_statement_expr
11193 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11194 /* This is the final expression statement of a statement
11195 expression. */
11196 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11197 else if (statement)
11198 statement = finish_expr_stmt (statement);
11200 return statement;
11203 /* Parse a compound-statement.
11205 compound-statement:
11206 { statement-seq [opt] }
11208 GNU extension:
11210 compound-statement:
11211 { label-declaration-seq [opt] statement-seq [opt] }
11213 label-declaration-seq:
11214 label-declaration
11215 label-declaration-seq label-declaration
11217 Returns a tree representing the statement. */
11219 static tree
11220 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11221 int bcs_flags, bool function_body)
11223 tree compound_stmt;
11224 matching_braces braces;
11226 /* Consume the `{'. */
11227 if (!braces.require_open (parser))
11228 return error_mark_node;
11229 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11230 && !function_body && cxx_dialect < cxx14)
11231 pedwarn (input_location, OPT_Wpedantic,
11232 "compound-statement in constexpr function");
11233 /* Begin the compound-statement. */
11234 compound_stmt = begin_compound_stmt (bcs_flags);
11235 /* If the next keyword is `__label__' we have a label declaration. */
11236 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11237 cp_parser_label_declaration (parser);
11238 /* Parse an (optional) statement-seq. */
11239 cp_parser_statement_seq_opt (parser, in_statement_expr);
11240 /* Finish the compound-statement. */
11241 finish_compound_stmt (compound_stmt);
11242 /* Consume the `}'. */
11243 braces.require_close (parser);
11245 return compound_stmt;
11248 /* Parse an (optional) statement-seq.
11250 statement-seq:
11251 statement
11252 statement-seq [opt] statement */
11254 static void
11255 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11257 /* Scan statements until there aren't any more. */
11258 while (true)
11260 cp_token *token = cp_lexer_peek_token (parser->lexer);
11262 /* If we are looking at a `}', then we have run out of
11263 statements; the same is true if we have reached the end
11264 of file, or have stumbled upon a stray '@end'. */
11265 if (token->type == CPP_CLOSE_BRACE
11266 || token->type == CPP_EOF
11267 || token->type == CPP_PRAGMA_EOL
11268 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11269 break;
11271 /* If we are in a compound statement and find 'else' then
11272 something went wrong. */
11273 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11275 if (parser->in_statement & IN_IF_STMT)
11276 break;
11277 else
11279 token = cp_lexer_consume_token (parser->lexer);
11280 error_at (token->location, "%<else%> without a previous %<if%>");
11284 /* Parse the statement. */
11285 cp_parser_statement (parser, in_statement_expr, true, NULL);
11289 /* Return true if we're looking at (init; cond), false otherwise. */
11291 static bool
11292 cp_parser_init_statement_p (cp_parser *parser)
11294 /* Save tokens so that we can put them back. */
11295 cp_lexer_save_tokens (parser->lexer);
11297 /* Look for ';' that is not nested in () or {}. */
11298 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11299 /*recovering=*/false,
11300 CPP_SEMICOLON,
11301 /*consume_paren=*/false);
11303 /* Roll back the tokens we skipped. */
11304 cp_lexer_rollback_tokens (parser->lexer);
11306 return ret == -1;
11309 /* Parse a selection-statement.
11311 selection-statement:
11312 if ( init-statement [opt] condition ) statement
11313 if ( init-statement [opt] condition ) statement else statement
11314 switch ( init-statement [opt] condition ) statement
11316 Returns the new IF_STMT or SWITCH_STMT.
11318 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11319 is a (possibly labeled) if statement which is not enclosed in
11320 braces and has an else clause. This is used to implement
11321 -Wparentheses.
11323 CHAIN is a vector of if-else-if conditions. This is used to implement
11324 -Wduplicated-cond. */
11326 static tree
11327 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11328 vec<tree> *chain)
11330 cp_token *token;
11331 enum rid keyword;
11332 token_indent_info guard_tinfo;
11334 if (if_p != NULL)
11335 *if_p = false;
11337 /* Peek at the next token. */
11338 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11339 guard_tinfo = get_token_indent_info (token);
11341 /* See what kind of keyword it is. */
11342 keyword = token->keyword;
11343 switch (keyword)
11345 case RID_IF:
11346 case RID_SWITCH:
11348 tree statement;
11349 tree condition;
11351 bool cx = false;
11352 if (keyword == RID_IF
11353 && cp_lexer_next_token_is_keyword (parser->lexer,
11354 RID_CONSTEXPR))
11356 cx = true;
11357 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11358 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11359 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11360 "with -std=c++1z or -std=gnu++1z");
11363 /* Look for the `('. */
11364 matching_parens parens;
11365 if (!parens.require_open (parser))
11367 cp_parser_skip_to_end_of_statement (parser);
11368 return error_mark_node;
11371 /* Begin the selection-statement. */
11372 if (keyword == RID_IF)
11374 statement = begin_if_stmt ();
11375 IF_STMT_CONSTEXPR_P (statement) = cx;
11377 else
11378 statement = begin_switch_stmt ();
11380 /* Parse the optional init-statement. */
11381 if (cp_parser_init_statement_p (parser))
11383 tree decl;
11384 if (cxx_dialect < cxx1z)
11385 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11386 "init-statement in selection statements only available "
11387 "with -std=c++1z or -std=gnu++1z");
11388 cp_parser_init_statement (parser, &decl);
11391 /* Parse the condition. */
11392 condition = cp_parser_condition (parser);
11393 /* Look for the `)'. */
11394 if (!parens.require_close (parser))
11395 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11396 /*consume_paren=*/true);
11398 if (keyword == RID_IF)
11400 bool nested_if;
11401 unsigned char in_statement;
11403 /* Add the condition. */
11404 condition = finish_if_stmt_cond (condition, statement);
11406 if (warn_duplicated_cond)
11407 warn_duplicated_cond_add_or_warn (token->location, condition,
11408 &chain);
11410 /* Parse the then-clause. */
11411 in_statement = parser->in_statement;
11412 parser->in_statement |= IN_IF_STMT;
11414 /* Outside a template, the non-selected branch of a constexpr
11415 if is a 'discarded statement', i.e. unevaluated. */
11416 bool was_discarded = in_discarded_stmt;
11417 bool discard_then = (cx && !processing_template_decl
11418 && integer_zerop (condition));
11419 if (discard_then)
11421 in_discarded_stmt = true;
11422 ++c_inhibit_evaluation_warnings;
11425 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11426 guard_tinfo);
11428 parser->in_statement = in_statement;
11430 finish_then_clause (statement);
11432 if (discard_then)
11434 THEN_CLAUSE (statement) = NULL_TREE;
11435 in_discarded_stmt = was_discarded;
11436 --c_inhibit_evaluation_warnings;
11439 /* If the next token is `else', parse the else-clause. */
11440 if (cp_lexer_next_token_is_keyword (parser->lexer,
11441 RID_ELSE))
11443 bool discard_else = (cx && !processing_template_decl
11444 && integer_nonzerop (condition));
11445 if (discard_else)
11447 in_discarded_stmt = true;
11448 ++c_inhibit_evaluation_warnings;
11451 guard_tinfo
11452 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11453 /* Consume the `else' keyword. */
11454 cp_lexer_consume_token (parser->lexer);
11455 if (warn_duplicated_cond)
11457 if (cp_lexer_next_token_is_keyword (parser->lexer,
11458 RID_IF)
11459 && chain == NULL)
11461 /* We've got "if (COND) else if (COND2)". Start
11462 the condition chain and add COND as the first
11463 element. */
11464 chain = new vec<tree> ();
11465 if (!CONSTANT_CLASS_P (condition)
11466 && !TREE_SIDE_EFFECTS (condition))
11468 /* Wrap it in a NOP_EXPR so that we can set the
11469 location of the condition. */
11470 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11471 condition);
11472 SET_EXPR_LOCATION (e, token->location);
11473 chain->safe_push (e);
11476 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11477 RID_IF))
11479 /* This is if-else without subsequent if. Zap the
11480 condition chain; we would have already warned at
11481 this point. */
11482 delete chain;
11483 chain = NULL;
11486 begin_else_clause (statement);
11487 /* Parse the else-clause. */
11488 cp_parser_implicitly_scoped_statement (parser, NULL,
11489 guard_tinfo, chain);
11491 finish_else_clause (statement);
11493 /* If we are currently parsing a then-clause, then
11494 IF_P will not be NULL. We set it to true to
11495 indicate that this if statement has an else clause.
11496 This may trigger the Wparentheses warning below
11497 when we get back up to the parent if statement. */
11498 if (if_p != NULL)
11499 *if_p = true;
11501 if (discard_else)
11503 ELSE_CLAUSE (statement) = NULL_TREE;
11504 in_discarded_stmt = was_discarded;
11505 --c_inhibit_evaluation_warnings;
11508 else
11510 /* This if statement does not have an else clause. If
11511 NESTED_IF is true, then the then-clause has an if
11512 statement which does have an else clause. We warn
11513 about the potential ambiguity. */
11514 if (nested_if)
11515 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11516 "suggest explicit braces to avoid ambiguous"
11517 " %<else%>");
11518 if (warn_duplicated_cond)
11520 /* We don't need the condition chain anymore. */
11521 delete chain;
11522 chain = NULL;
11526 /* Now we're all done with the if-statement. */
11527 finish_if_stmt (statement);
11529 else
11531 bool in_switch_statement_p;
11532 unsigned char in_statement;
11534 /* Add the condition. */
11535 finish_switch_cond (condition, statement);
11537 /* Parse the body of the switch-statement. */
11538 in_switch_statement_p = parser->in_switch_statement_p;
11539 in_statement = parser->in_statement;
11540 parser->in_switch_statement_p = true;
11541 parser->in_statement |= IN_SWITCH_STMT;
11542 cp_parser_implicitly_scoped_statement (parser, if_p,
11543 guard_tinfo);
11544 parser->in_switch_statement_p = in_switch_statement_p;
11545 parser->in_statement = in_statement;
11547 /* Now we're all done with the switch-statement. */
11548 finish_switch_stmt (statement);
11551 return statement;
11553 break;
11555 default:
11556 cp_parser_error (parser, "expected selection-statement");
11557 return error_mark_node;
11561 /* Parse a condition.
11563 condition:
11564 expression
11565 type-specifier-seq declarator = initializer-clause
11566 type-specifier-seq declarator braced-init-list
11568 GNU Extension:
11570 condition:
11571 type-specifier-seq declarator asm-specification [opt]
11572 attributes [opt] = assignment-expression
11574 Returns the expression that should be tested. */
11576 static tree
11577 cp_parser_condition (cp_parser* parser)
11579 cp_decl_specifier_seq type_specifiers;
11580 const char *saved_message;
11581 int declares_class_or_enum;
11583 /* Try the declaration first. */
11584 cp_parser_parse_tentatively (parser);
11585 /* New types are not allowed in the type-specifier-seq for a
11586 condition. */
11587 saved_message = parser->type_definition_forbidden_message;
11588 parser->type_definition_forbidden_message
11589 = G_("types may not be defined in conditions");
11590 /* Parse the type-specifier-seq. */
11591 cp_parser_decl_specifier_seq (parser,
11592 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11593 &type_specifiers,
11594 &declares_class_or_enum);
11595 /* Restore the saved message. */
11596 parser->type_definition_forbidden_message = saved_message;
11597 /* If all is well, we might be looking at a declaration. */
11598 if (!cp_parser_error_occurred (parser))
11600 tree decl;
11601 tree asm_specification;
11602 tree attributes;
11603 cp_declarator *declarator;
11604 tree initializer = NULL_TREE;
11606 /* Parse the declarator. */
11607 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11608 /*ctor_dtor_or_conv_p=*/NULL,
11609 /*parenthesized_p=*/NULL,
11610 /*member_p=*/false,
11611 /*friend_p=*/false);
11612 /* Parse the attributes. */
11613 attributes = cp_parser_attributes_opt (parser);
11614 /* Parse the asm-specification. */
11615 asm_specification = cp_parser_asm_specification_opt (parser);
11616 /* If the next token is not an `=' or '{', then we might still be
11617 looking at an expression. For example:
11619 if (A(a).x)
11621 looks like a decl-specifier-seq and a declarator -- but then
11622 there is no `=', so this is an expression. */
11623 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11624 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11625 cp_parser_simulate_error (parser);
11627 /* If we did see an `=' or '{', then we are looking at a declaration
11628 for sure. */
11629 if (cp_parser_parse_definitely (parser))
11631 tree pushed_scope;
11632 bool non_constant_p;
11633 int flags = LOOKUP_ONLYCONVERTING;
11635 /* Create the declaration. */
11636 decl = start_decl (declarator, &type_specifiers,
11637 /*initialized_p=*/true,
11638 attributes, /*prefix_attributes=*/NULL_TREE,
11639 &pushed_scope);
11641 /* Parse the initializer. */
11642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11644 initializer = cp_parser_braced_list (parser, &non_constant_p);
11645 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11646 flags = 0;
11648 else
11650 /* Consume the `='. */
11651 cp_parser_require (parser, CPP_EQ, RT_EQ);
11652 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11654 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11655 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11657 /* Process the initializer. */
11658 cp_finish_decl (decl,
11659 initializer, !non_constant_p,
11660 asm_specification,
11661 flags);
11663 if (pushed_scope)
11664 pop_scope (pushed_scope);
11666 return convert_from_reference (decl);
11669 /* If we didn't even get past the declarator successfully, we are
11670 definitely not looking at a declaration. */
11671 else
11672 cp_parser_abort_tentative_parse (parser);
11674 /* Otherwise, we are looking at an expression. */
11675 return cp_parser_expression (parser);
11678 /* Parses a for-statement or range-for-statement until the closing ')',
11679 not included. */
11681 static tree
11682 cp_parser_for (cp_parser *parser, bool ivdep)
11684 tree init, scope, decl;
11685 bool is_range_for;
11687 /* Begin the for-statement. */
11688 scope = begin_for_scope (&init);
11690 /* Parse the initialization. */
11691 is_range_for = cp_parser_init_statement (parser, &decl);
11693 if (is_range_for)
11694 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11695 else
11696 return cp_parser_c_for (parser, scope, init, ivdep);
11699 static tree
11700 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11702 /* Normal for loop */
11703 tree condition = NULL_TREE;
11704 tree expression = NULL_TREE;
11705 tree stmt;
11707 stmt = begin_for_stmt (scope, init);
11708 /* The init-statement has already been parsed in
11709 cp_parser_init_statement, so no work is needed here. */
11710 finish_init_stmt (stmt);
11712 /* If there's a condition, process it. */
11713 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11714 condition = cp_parser_condition (parser);
11715 else if (ivdep)
11717 cp_parser_error (parser, "missing loop condition in loop with "
11718 "%<GCC ivdep%> pragma");
11719 condition = error_mark_node;
11721 finish_for_cond (condition, stmt, ivdep);
11722 /* Look for the `;'. */
11723 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11725 /* If there's an expression, process it. */
11726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11727 expression = cp_parser_expression (parser);
11728 finish_for_expr (expression, stmt);
11730 return stmt;
11733 /* Tries to parse a range-based for-statement:
11735 range-based-for:
11736 decl-specifier-seq declarator : expression
11738 The decl-specifier-seq declarator and the `:' are already parsed by
11739 cp_parser_init_statement. If processing_template_decl it returns a
11740 newly created RANGE_FOR_STMT; if not, it is converted to a
11741 regular FOR_STMT. */
11743 static tree
11744 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11745 bool ivdep)
11747 tree stmt, range_expr;
11748 auto_vec <cxx_binding *, 16> bindings;
11749 auto_vec <tree, 16> names;
11750 tree decomp_first_name = NULL_TREE;
11751 unsigned int decomp_cnt = 0;
11753 /* Get the range declaration momentarily out of the way so that
11754 the range expression doesn't clash with it. */
11755 if (range_decl != error_mark_node)
11757 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11759 tree v = DECL_VALUE_EXPR (range_decl);
11760 /* For decomposition declaration get all of the corresponding
11761 declarations out of the way. */
11762 if (TREE_CODE (v) == ARRAY_REF
11763 && VAR_P (TREE_OPERAND (v, 0))
11764 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11766 tree d = range_decl;
11767 range_decl = TREE_OPERAND (v, 0);
11768 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11769 decomp_first_name = d;
11770 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11772 tree name = DECL_NAME (d);
11773 names.safe_push (name);
11774 bindings.safe_push (IDENTIFIER_BINDING (name));
11775 IDENTIFIER_BINDING (name)
11776 = IDENTIFIER_BINDING (name)->previous;
11780 if (names.is_empty ())
11782 tree name = DECL_NAME (range_decl);
11783 names.safe_push (name);
11784 bindings.safe_push (IDENTIFIER_BINDING (name));
11785 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11789 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11791 bool expr_non_constant_p;
11792 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11794 else
11795 range_expr = cp_parser_expression (parser);
11797 /* Put the range declaration(s) back into scope. */
11798 for (unsigned int i = 0; i < names.length (); i++)
11800 cxx_binding *binding = bindings[i];
11801 binding->previous = IDENTIFIER_BINDING (names[i]);
11802 IDENTIFIER_BINDING (names[i]) = binding;
11805 /* If in template, STMT is converted to a normal for-statement
11806 at instantiation. If not, it is done just ahead. */
11807 if (processing_template_decl)
11809 if (check_for_bare_parameter_packs (range_expr))
11810 range_expr = error_mark_node;
11811 stmt = begin_range_for_stmt (scope, init);
11812 if (ivdep)
11813 RANGE_FOR_IVDEP (stmt) = 1;
11814 finish_range_for_decl (stmt, range_decl, range_expr);
11815 if (!type_dependent_expression_p (range_expr)
11816 /* do_auto_deduction doesn't mess with template init-lists. */
11817 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11818 do_range_for_auto_deduction (range_decl, range_expr);
11820 else
11822 stmt = begin_for_stmt (scope, init);
11823 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11824 decomp_first_name, decomp_cnt, ivdep);
11826 return stmt;
11829 /* Subroutine of cp_convert_range_for: given the initializer expression,
11830 builds up the range temporary. */
11832 static tree
11833 build_range_temp (tree range_expr)
11835 tree range_type, range_temp;
11837 /* Find out the type deduced by the declaration
11838 `auto &&__range = range_expr'. */
11839 range_type = cp_build_reference_type (make_auto (), true);
11840 range_type = do_auto_deduction (range_type, range_expr,
11841 type_uses_auto (range_type));
11843 /* Create the __range variable. */
11844 range_temp = build_decl (input_location, VAR_DECL,
11845 get_identifier ("__for_range"), range_type);
11846 TREE_USED (range_temp) = 1;
11847 DECL_ARTIFICIAL (range_temp) = 1;
11849 return range_temp;
11852 /* Used by cp_parser_range_for in template context: we aren't going to
11853 do a full conversion yet, but we still need to resolve auto in the
11854 type of the for-range-declaration if present. This is basically
11855 a shortcut version of cp_convert_range_for. */
11857 static void
11858 do_range_for_auto_deduction (tree decl, tree range_expr)
11860 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11861 if (auto_node)
11863 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11864 range_temp = convert_from_reference (build_range_temp (range_expr));
11865 iter_type = (cp_parser_perform_range_for_lookup
11866 (range_temp, &begin_dummy, &end_dummy));
11867 if (iter_type)
11869 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11870 iter_type);
11871 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11872 tf_warning_or_error);
11873 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11874 iter_decl, auto_node);
11879 /* Converts a range-based for-statement into a normal
11880 for-statement, as per the definition.
11882 for (RANGE_DECL : RANGE_EXPR)
11883 BLOCK
11885 should be equivalent to:
11888 auto &&__range = RANGE_EXPR;
11889 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11890 __begin != __end;
11891 ++__begin)
11893 RANGE_DECL = *__begin;
11894 BLOCK
11898 If RANGE_EXPR is an array:
11899 BEGIN_EXPR = __range
11900 END_EXPR = __range + ARRAY_SIZE(__range)
11901 Else if RANGE_EXPR has a member 'begin' or 'end':
11902 BEGIN_EXPR = __range.begin()
11903 END_EXPR = __range.end()
11904 Else:
11905 BEGIN_EXPR = begin(__range)
11906 END_EXPR = end(__range);
11908 If __range has a member 'begin' but not 'end', or vice versa, we must
11909 still use the second alternative (it will surely fail, however).
11910 When calling begin()/end() in the third alternative we must use
11911 argument dependent lookup, but always considering 'std' as an associated
11912 namespace. */
11914 tree
11915 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11916 tree decomp_first_name, unsigned int decomp_cnt,
11917 bool ivdep)
11919 tree begin, end;
11920 tree iter_type, begin_expr, end_expr;
11921 tree condition, expression;
11923 if (range_decl == error_mark_node || range_expr == error_mark_node)
11924 /* If an error happened previously do nothing or else a lot of
11925 unhelpful errors would be issued. */
11926 begin_expr = end_expr = iter_type = error_mark_node;
11927 else
11929 tree range_temp;
11931 if (VAR_P (range_expr)
11932 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11933 /* Can't bind a reference to an array of runtime bound. */
11934 range_temp = range_expr;
11935 else
11937 range_temp = build_range_temp (range_expr);
11938 pushdecl (range_temp);
11939 cp_finish_decl (range_temp, range_expr,
11940 /*is_constant_init*/false, NULL_TREE,
11941 LOOKUP_ONLYCONVERTING);
11942 range_temp = convert_from_reference (range_temp);
11944 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11945 &begin_expr, &end_expr);
11948 /* The new for initialization statement. */
11949 begin = build_decl (input_location, VAR_DECL,
11950 get_identifier ("__for_begin"), iter_type);
11951 TREE_USED (begin) = 1;
11952 DECL_ARTIFICIAL (begin) = 1;
11953 pushdecl (begin);
11954 cp_finish_decl (begin, begin_expr,
11955 /*is_constant_init*/false, NULL_TREE,
11956 LOOKUP_ONLYCONVERTING);
11958 if (cxx_dialect >= cxx1z)
11959 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11960 end = build_decl (input_location, VAR_DECL,
11961 get_identifier ("__for_end"), iter_type);
11962 TREE_USED (end) = 1;
11963 DECL_ARTIFICIAL (end) = 1;
11964 pushdecl (end);
11965 cp_finish_decl (end, end_expr,
11966 /*is_constant_init*/false, NULL_TREE,
11967 LOOKUP_ONLYCONVERTING);
11969 finish_init_stmt (statement);
11971 /* The new for condition. */
11972 condition = build_x_binary_op (input_location, NE_EXPR,
11973 begin, ERROR_MARK,
11974 end, ERROR_MARK,
11975 NULL, tf_warning_or_error);
11976 finish_for_cond (condition, statement, ivdep);
11978 /* The new increment expression. */
11979 expression = finish_unary_op_expr (input_location,
11980 PREINCREMENT_EXPR, begin,
11981 tf_warning_or_error);
11982 finish_for_expr (expression, statement);
11984 /* The declaration is initialized with *__begin inside the loop body. */
11985 cp_finish_decl (range_decl,
11986 build_x_indirect_ref (input_location, begin, RO_NULL,
11987 tf_warning_or_error),
11988 /*is_constant_init*/false, NULL_TREE,
11989 LOOKUP_ONLYCONVERTING);
11990 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11991 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11993 return statement;
11996 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11997 We need to solve both at the same time because the method used
11998 depends on the existence of members begin or end.
11999 Returns the type deduced for the iterator expression. */
12001 static tree
12002 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
12004 if (error_operand_p (range))
12006 *begin = *end = error_mark_node;
12007 return error_mark_node;
12010 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
12012 error ("range-based %<for%> expression of type %qT "
12013 "has incomplete type", TREE_TYPE (range));
12014 *begin = *end = error_mark_node;
12015 return error_mark_node;
12017 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
12019 /* If RANGE is an array, we will use pointer arithmetic. */
12020 *begin = decay_conversion (range, tf_warning_or_error);
12021 *end = build_binary_op (input_location, PLUS_EXPR,
12022 range,
12023 array_type_nelts_top (TREE_TYPE (range)),
12024 false);
12025 return TREE_TYPE (*begin);
12027 else
12029 /* If it is not an array, we must do a bit of magic. */
12030 tree id_begin, id_end;
12031 tree member_begin, member_end;
12033 *begin = *end = error_mark_node;
12035 id_begin = get_identifier ("begin");
12036 id_end = get_identifier ("end");
12037 member_begin = lookup_member (TREE_TYPE (range), id_begin,
12038 /*protect=*/2, /*want_type=*/false,
12039 tf_warning_or_error);
12040 member_end = lookup_member (TREE_TYPE (range), id_end,
12041 /*protect=*/2, /*want_type=*/false,
12042 tf_warning_or_error);
12044 if (member_begin != NULL_TREE || member_end != NULL_TREE)
12046 /* Use the member functions. */
12047 if (member_begin != NULL_TREE)
12048 *begin = cp_parser_range_for_member_function (range, id_begin);
12049 else
12050 error ("range-based %<for%> expression of type %qT has an "
12051 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
12053 if (member_end != NULL_TREE)
12054 *end = cp_parser_range_for_member_function (range, id_end);
12055 else
12056 error ("range-based %<for%> expression of type %qT has a "
12057 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
12059 else
12061 /* Use global functions with ADL. */
12062 vec<tree, va_gc> *vec;
12063 vec = make_tree_vector ();
12065 vec_safe_push (vec, range);
12067 member_begin = perform_koenig_lookup (id_begin, vec,
12068 tf_warning_or_error);
12069 *begin = finish_call_expr (member_begin, &vec, false, true,
12070 tf_warning_or_error);
12071 member_end = perform_koenig_lookup (id_end, vec,
12072 tf_warning_or_error);
12073 *end = finish_call_expr (member_end, &vec, false, true,
12074 tf_warning_or_error);
12076 release_tree_vector (vec);
12079 /* Last common checks. */
12080 if (*begin == error_mark_node || *end == error_mark_node)
12082 /* If one of the expressions is an error do no more checks. */
12083 *begin = *end = error_mark_node;
12084 return error_mark_node;
12086 else if (type_dependent_expression_p (*begin)
12087 || type_dependent_expression_p (*end))
12088 /* Can happen, when, eg, in a template context, Koenig lookup
12089 can't resolve begin/end (c++/58503). */
12090 return NULL_TREE;
12091 else
12093 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
12094 /* The unqualified type of the __begin and __end temporaries should
12095 be the same, as required by the multiple auto declaration. */
12096 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12098 if (cxx_dialect >= cxx1z
12099 && (build_x_binary_op (input_location, NE_EXPR,
12100 *begin, ERROR_MARK,
12101 *end, ERROR_MARK,
12102 NULL, tf_none)
12103 != error_mark_node))
12104 /* P0184R0 allows __begin and __end to have different types,
12105 but make sure they are comparable so we can give a better
12106 diagnostic. */;
12107 else
12108 error ("inconsistent begin/end types in range-based %<for%> "
12109 "statement: %qT and %qT",
12110 TREE_TYPE (*begin), TREE_TYPE (*end));
12112 return iter_type;
12117 /* Helper function for cp_parser_perform_range_for_lookup.
12118 Builds a tree for RANGE.IDENTIFIER(). */
12120 static tree
12121 cp_parser_range_for_member_function (tree range, tree identifier)
12123 tree member, res;
12124 vec<tree, va_gc> *vec;
12126 member = finish_class_member_access_expr (range, identifier,
12127 false, tf_warning_or_error);
12128 if (member == error_mark_node)
12129 return error_mark_node;
12131 vec = make_tree_vector ();
12132 res = finish_call_expr (member, &vec,
12133 /*disallow_virtual=*/false,
12134 /*koenig_p=*/false,
12135 tf_warning_or_error);
12136 release_tree_vector (vec);
12137 return res;
12140 /* Parse an iteration-statement.
12142 iteration-statement:
12143 while ( condition ) statement
12144 do statement while ( expression ) ;
12145 for ( init-statement condition [opt] ; expression [opt] )
12146 statement
12148 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12150 static tree
12151 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12153 cp_token *token;
12154 enum rid keyword;
12155 tree statement;
12156 unsigned char in_statement;
12157 token_indent_info guard_tinfo;
12159 /* Peek at the next token. */
12160 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12161 if (!token)
12162 return error_mark_node;
12164 guard_tinfo = get_token_indent_info (token);
12166 /* Remember whether or not we are already within an iteration
12167 statement. */
12168 in_statement = parser->in_statement;
12170 /* See what kind of keyword it is. */
12171 keyword = token->keyword;
12172 switch (keyword)
12174 case RID_WHILE:
12176 tree condition;
12178 /* Begin the while-statement. */
12179 statement = begin_while_stmt ();
12180 /* Look for the `('. */
12181 matching_parens parens;
12182 parens.require_open (parser);
12183 /* Parse the condition. */
12184 condition = cp_parser_condition (parser);
12185 finish_while_stmt_cond (condition, statement, ivdep);
12186 /* Look for the `)'. */
12187 parens.require_close (parser);
12188 /* Parse the dependent statement. */
12189 parser->in_statement = IN_ITERATION_STMT;
12190 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12191 parser->in_statement = in_statement;
12192 /* We're done with the while-statement. */
12193 finish_while_stmt (statement);
12195 break;
12197 case RID_DO:
12199 tree expression;
12201 /* Begin the do-statement. */
12202 statement = begin_do_stmt ();
12203 /* Parse the body of the do-statement. */
12204 parser->in_statement = IN_ITERATION_STMT;
12205 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12206 parser->in_statement = in_statement;
12207 finish_do_body (statement);
12208 /* Look for the `while' keyword. */
12209 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12210 /* Look for the `('. */
12211 matching_parens parens;
12212 parens.require_open (parser);
12213 /* Parse the expression. */
12214 expression = cp_parser_expression (parser);
12215 /* We're done with the do-statement. */
12216 finish_do_stmt (expression, statement, ivdep);
12217 /* Look for the `)'. */
12218 parens.require_close (parser);
12219 /* Look for the `;'. */
12220 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12222 break;
12224 case RID_FOR:
12226 /* Look for the `('. */
12227 matching_parens parens;
12228 parens.require_open (parser);
12230 statement = cp_parser_for (parser, ivdep);
12232 /* Look for the `)'. */
12233 parens.require_close (parser);
12235 /* Parse the body of the for-statement. */
12236 parser->in_statement = IN_ITERATION_STMT;
12237 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12238 parser->in_statement = in_statement;
12240 /* We're done with the for-statement. */
12241 finish_for_stmt (statement);
12243 break;
12245 default:
12246 cp_parser_error (parser, "expected iteration-statement");
12247 statement = error_mark_node;
12248 break;
12251 return statement;
12254 /* Parse a init-statement or the declarator of a range-based-for.
12255 Returns true if a range-based-for declaration is seen.
12257 init-statement:
12258 expression-statement
12259 simple-declaration */
12261 static bool
12262 cp_parser_init_statement (cp_parser* parser, tree *decl)
12264 /* If the next token is a `;', then we have an empty
12265 expression-statement. Grammatically, this is also a
12266 simple-declaration, but an invalid one, because it does not
12267 declare anything. Therefore, if we did not handle this case
12268 specially, we would issue an error message about an invalid
12269 declaration. */
12270 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12272 bool is_range_for = false;
12273 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12275 /* A colon is used in range-based for. */
12276 parser->colon_corrects_to_scope_p = false;
12278 /* We're going to speculatively look for a declaration, falling back
12279 to an expression, if necessary. */
12280 cp_parser_parse_tentatively (parser);
12281 /* Parse the declaration. */
12282 cp_parser_simple_declaration (parser,
12283 /*function_definition_allowed_p=*/false,
12284 decl);
12285 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12286 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12288 /* It is a range-for, consume the ':' */
12289 cp_lexer_consume_token (parser->lexer);
12290 is_range_for = true;
12291 if (cxx_dialect < cxx11)
12293 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12294 "range-based %<for%> loops only available with "
12295 "-std=c++11 or -std=gnu++11");
12296 *decl = error_mark_node;
12299 else
12300 /* The ';' is not consumed yet because we told
12301 cp_parser_simple_declaration not to. */
12302 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12304 if (cp_parser_parse_definitely (parser))
12305 return is_range_for;
12306 /* If the tentative parse failed, then we shall need to look for an
12307 expression-statement. */
12309 /* If we are here, it is an expression-statement. */
12310 cp_parser_expression_statement (parser, NULL_TREE);
12311 return false;
12314 /* Parse a jump-statement.
12316 jump-statement:
12317 break ;
12318 continue ;
12319 return expression [opt] ;
12320 return braced-init-list ;
12321 goto identifier ;
12323 GNU extension:
12325 jump-statement:
12326 goto * expression ;
12328 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12330 static tree
12331 cp_parser_jump_statement (cp_parser* parser)
12333 tree statement = error_mark_node;
12334 cp_token *token;
12335 enum rid keyword;
12336 unsigned char in_statement;
12338 /* Peek at the next token. */
12339 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12340 if (!token)
12341 return error_mark_node;
12343 /* See what kind of keyword it is. */
12344 keyword = token->keyword;
12345 switch (keyword)
12347 case RID_BREAK:
12348 in_statement = parser->in_statement & ~IN_IF_STMT;
12349 switch (in_statement)
12351 case 0:
12352 error_at (token->location, "break statement not within loop or switch");
12353 break;
12354 default:
12355 gcc_assert ((in_statement & IN_SWITCH_STMT)
12356 || in_statement == IN_ITERATION_STMT);
12357 statement = finish_break_stmt ();
12358 if (in_statement == IN_ITERATION_STMT)
12359 break_maybe_infinite_loop ();
12360 break;
12361 case IN_OMP_BLOCK:
12362 error_at (token->location, "invalid exit from OpenMP structured block");
12363 break;
12364 case IN_OMP_FOR:
12365 error_at (token->location, "break statement used with OpenMP for loop");
12366 break;
12367 case IN_CILK_SIMD_FOR:
12368 error_at (token->location, "break statement used with Cilk Plus for loop");
12369 break;
12371 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12372 break;
12374 case RID_CONTINUE:
12375 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12377 case 0:
12378 error_at (token->location, "continue statement not within a loop");
12379 break;
12380 case IN_CILK_SIMD_FOR:
12381 error_at (token->location,
12382 "continue statement within %<#pragma simd%> loop body");
12383 /* Fall through. */
12384 case IN_ITERATION_STMT:
12385 case IN_OMP_FOR:
12386 statement = finish_continue_stmt ();
12387 break;
12388 case IN_OMP_BLOCK:
12389 error_at (token->location, "invalid exit from OpenMP structured block");
12390 break;
12391 default:
12392 gcc_unreachable ();
12394 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12395 break;
12397 case RID_RETURN:
12399 tree expr;
12400 bool expr_non_constant_p;
12402 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12404 cp_lexer_set_source_position (parser->lexer);
12405 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12406 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12408 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12409 expr = cp_parser_expression (parser);
12410 else
12411 /* If the next token is a `;', then there is no
12412 expression. */
12413 expr = NULL_TREE;
12414 /* Build the return-statement. */
12415 if (current_function_auto_return_pattern && in_discarded_stmt)
12416 /* Don't deduce from a discarded return statement. */;
12417 else
12418 statement = finish_return_stmt (expr);
12419 /* Look for the final `;'. */
12420 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12422 break;
12424 case RID_GOTO:
12425 if (parser->in_function_body
12426 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12428 error ("%<goto%> in %<constexpr%> function");
12429 cp_function_chain->invalid_constexpr = true;
12432 /* Create the goto-statement. */
12433 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12435 /* Issue a warning about this use of a GNU extension. */
12436 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12437 /* Consume the '*' token. */
12438 cp_lexer_consume_token (parser->lexer);
12439 /* Parse the dependent expression. */
12440 finish_goto_stmt (cp_parser_expression (parser));
12442 else
12443 finish_goto_stmt (cp_parser_identifier (parser));
12444 /* Look for the final `;'. */
12445 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12446 break;
12448 default:
12449 cp_parser_error (parser, "expected jump-statement");
12450 break;
12453 return statement;
12456 /* Parse a declaration-statement.
12458 declaration-statement:
12459 block-declaration */
12461 static void
12462 cp_parser_declaration_statement (cp_parser* parser)
12464 void *p;
12466 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12467 p = obstack_alloc (&declarator_obstack, 0);
12469 /* Parse the block-declaration. */
12470 cp_parser_block_declaration (parser, /*statement_p=*/true);
12472 /* Free any declarators allocated. */
12473 obstack_free (&declarator_obstack, p);
12476 /* Some dependent statements (like `if (cond) statement'), are
12477 implicitly in their own scope. In other words, if the statement is
12478 a single statement (as opposed to a compound-statement), it is
12479 none-the-less treated as if it were enclosed in braces. Any
12480 declarations appearing in the dependent statement are out of scope
12481 after control passes that point. This function parses a statement,
12482 but ensures that is in its own scope, even if it is not a
12483 compound-statement.
12485 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12486 is a (possibly labeled) if statement which is not enclosed in
12487 braces and has an else clause. This is used to implement
12488 -Wparentheses.
12490 CHAIN is a vector of if-else-if conditions. This is used to implement
12491 -Wduplicated-cond.
12493 Returns the new statement. */
12495 static tree
12496 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12497 const token_indent_info &guard_tinfo,
12498 vec<tree> *chain)
12500 tree statement;
12501 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12502 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12503 token_indent_info body_tinfo
12504 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12506 if (if_p != NULL)
12507 *if_p = false;
12509 /* Mark if () ; with a special NOP_EXPR. */
12510 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12512 cp_lexer_consume_token (parser->lexer);
12513 statement = add_stmt (build_empty_stmt (body_loc));
12515 if (guard_tinfo.keyword == RID_IF
12516 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12517 warning_at (body_loc, OPT_Wempty_body,
12518 "suggest braces around empty body in an %<if%> statement");
12519 else if (guard_tinfo.keyword == RID_ELSE)
12520 warning_at (body_loc, OPT_Wempty_body,
12521 "suggest braces around empty body in an %<else%> statement");
12523 /* if a compound is opened, we simply parse the statement directly. */
12524 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12525 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12526 /* If the token is not a `{', then we must take special action. */
12527 else
12529 /* Create a compound-statement. */
12530 statement = begin_compound_stmt (0);
12531 /* Parse the dependent-statement. */
12532 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12533 &body_loc_after_labels);
12534 /* Finish the dummy compound-statement. */
12535 finish_compound_stmt (statement);
12538 token_indent_info next_tinfo
12539 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12540 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12542 if (body_loc_after_labels != UNKNOWN_LOCATION
12543 && next_tinfo.type != CPP_SEMICOLON)
12544 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12545 guard_tinfo.location, guard_tinfo.keyword);
12547 /* Return the statement. */
12548 return statement;
12551 /* For some dependent statements (like `while (cond) statement'), we
12552 have already created a scope. Therefore, even if the dependent
12553 statement is a compound-statement, we do not want to create another
12554 scope. */
12556 static void
12557 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12558 const token_indent_info &guard_tinfo)
12560 /* If the token is a `{', then we must take special action. */
12561 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12563 token_indent_info body_tinfo
12564 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12565 location_t loc_after_labels = UNKNOWN_LOCATION;
12567 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12568 &loc_after_labels);
12569 token_indent_info next_tinfo
12570 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12571 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12573 if (loc_after_labels != UNKNOWN_LOCATION
12574 && next_tinfo.type != CPP_SEMICOLON)
12575 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12576 guard_tinfo.location,
12577 guard_tinfo.keyword);
12579 else
12581 /* Avoid calling cp_parser_compound_statement, so that we
12582 don't create a new scope. Do everything else by hand. */
12583 matching_braces braces;
12584 braces.require_open (parser);
12585 /* If the next keyword is `__label__' we have a label declaration. */
12586 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12587 cp_parser_label_declaration (parser);
12588 /* Parse an (optional) statement-seq. */
12589 cp_parser_statement_seq_opt (parser, NULL_TREE);
12590 braces.require_close (parser);
12594 /* Declarations [gram.dcl.dcl] */
12596 /* Parse an optional declaration-sequence.
12598 declaration-seq:
12599 declaration
12600 declaration-seq declaration */
12602 static void
12603 cp_parser_declaration_seq_opt (cp_parser* parser)
12605 while (true)
12607 cp_token *token;
12609 token = cp_lexer_peek_token (parser->lexer);
12611 if (token->type == CPP_CLOSE_BRACE
12612 || token->type == CPP_EOF
12613 || token->type == CPP_PRAGMA_EOL)
12614 break;
12616 if (token->type == CPP_SEMICOLON)
12618 /* A declaration consisting of a single semicolon is
12619 invalid. Allow it unless we're being pedantic. */
12620 cp_lexer_consume_token (parser->lexer);
12621 if (!in_system_header_at (input_location))
12622 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12623 continue;
12626 /* If we're entering or exiting a region that's implicitly
12627 extern "C", modify the lang context appropriately. */
12628 if (!parser->implicit_extern_c && token->implicit_extern_c)
12630 push_lang_context (lang_name_c);
12631 parser->implicit_extern_c = true;
12633 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12635 pop_lang_context ();
12636 parser->implicit_extern_c = false;
12639 if (token->type == CPP_PRAGMA)
12641 /* A top-level declaration can consist solely of a #pragma.
12642 A nested declaration cannot, so this is done here and not
12643 in cp_parser_declaration. (A #pragma at block scope is
12644 handled in cp_parser_statement.) */
12645 cp_parser_pragma (parser, pragma_external, NULL);
12646 continue;
12649 /* Parse the declaration itself. */
12650 cp_parser_declaration (parser);
12654 /* Parse a declaration.
12656 declaration:
12657 block-declaration
12658 function-definition
12659 template-declaration
12660 explicit-instantiation
12661 explicit-specialization
12662 linkage-specification
12663 namespace-definition
12665 C++17:
12666 deduction-guide
12668 GNU extension:
12670 declaration:
12671 __extension__ declaration */
12673 static void
12674 cp_parser_declaration (cp_parser* parser)
12676 cp_token token1;
12677 cp_token token2;
12678 int saved_pedantic;
12679 void *p;
12680 tree attributes = NULL_TREE;
12682 /* Check for the `__extension__' keyword. */
12683 if (cp_parser_extension_opt (parser, &saved_pedantic))
12685 /* Parse the qualified declaration. */
12686 cp_parser_declaration (parser);
12687 /* Restore the PEDANTIC flag. */
12688 pedantic = saved_pedantic;
12690 return;
12693 /* Try to figure out what kind of declaration is present. */
12694 token1 = *cp_lexer_peek_token (parser->lexer);
12696 if (token1.type != CPP_EOF)
12697 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12698 else
12700 token2.type = CPP_EOF;
12701 token2.keyword = RID_MAX;
12704 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12705 p = obstack_alloc (&declarator_obstack, 0);
12707 /* If the next token is `extern' and the following token is a string
12708 literal, then we have a linkage specification. */
12709 if (token1.keyword == RID_EXTERN
12710 && cp_parser_is_pure_string_literal (&token2))
12711 cp_parser_linkage_specification (parser);
12712 /* If the next token is `template', then we have either a template
12713 declaration, an explicit instantiation, or an explicit
12714 specialization. */
12715 else if (token1.keyword == RID_TEMPLATE)
12717 /* `template <>' indicates a template specialization. */
12718 if (token2.type == CPP_LESS
12719 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12720 cp_parser_explicit_specialization (parser);
12721 /* `template <' indicates a template declaration. */
12722 else if (token2.type == CPP_LESS)
12723 cp_parser_template_declaration (parser, /*member_p=*/false);
12724 /* Anything else must be an explicit instantiation. */
12725 else
12726 cp_parser_explicit_instantiation (parser);
12728 /* If the next token is `export', then we have a template
12729 declaration. */
12730 else if (token1.keyword == RID_EXPORT)
12731 cp_parser_template_declaration (parser, /*member_p=*/false);
12732 /* If the next token is `extern', 'static' or 'inline' and the one
12733 after that is `template', we have a GNU extended explicit
12734 instantiation directive. */
12735 else if (cp_parser_allow_gnu_extensions_p (parser)
12736 && (token1.keyword == RID_EXTERN
12737 || token1.keyword == RID_STATIC
12738 || token1.keyword == RID_INLINE)
12739 && token2.keyword == RID_TEMPLATE)
12740 cp_parser_explicit_instantiation (parser);
12741 /* If the next token is `namespace', check for a named or unnamed
12742 namespace definition. */
12743 else if (token1.keyword == RID_NAMESPACE
12744 && (/* A named namespace definition. */
12745 (token2.type == CPP_NAME
12746 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12747 != CPP_EQ))
12748 || (token2.type == CPP_OPEN_SQUARE
12749 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12750 == CPP_OPEN_SQUARE)
12751 /* An unnamed namespace definition. */
12752 || token2.type == CPP_OPEN_BRACE
12753 || token2.keyword == RID_ATTRIBUTE))
12754 cp_parser_namespace_definition (parser);
12755 /* An inline (associated) namespace definition. */
12756 else if (token1.keyword == RID_INLINE
12757 && token2.keyword == RID_NAMESPACE)
12758 cp_parser_namespace_definition (parser);
12759 /* Objective-C++ declaration/definition. */
12760 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12761 cp_parser_objc_declaration (parser, NULL_TREE);
12762 else if (c_dialect_objc ()
12763 && token1.keyword == RID_ATTRIBUTE
12764 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12765 cp_parser_objc_declaration (parser, attributes);
12766 /* At this point we may have a template declared by a concept
12767 introduction. */
12768 else if (flag_concepts
12769 && cp_parser_template_declaration_after_export (parser,
12770 /*member_p=*/false))
12771 /* We did. */;
12772 else
12773 /* Try to parse a block-declaration, or a function-definition. */
12774 cp_parser_block_declaration (parser, /*statement_p=*/false);
12776 /* Free any declarators allocated. */
12777 obstack_free (&declarator_obstack, p);
12780 /* Parse a block-declaration.
12782 block-declaration:
12783 simple-declaration
12784 asm-definition
12785 namespace-alias-definition
12786 using-declaration
12787 using-directive
12789 GNU Extension:
12791 block-declaration:
12792 __extension__ block-declaration
12794 C++0x Extension:
12796 block-declaration:
12797 static_assert-declaration
12799 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12800 part of a declaration-statement. */
12802 static void
12803 cp_parser_block_declaration (cp_parser *parser,
12804 bool statement_p)
12806 cp_token *token1;
12807 int saved_pedantic;
12809 /* Check for the `__extension__' keyword. */
12810 if (cp_parser_extension_opt (parser, &saved_pedantic))
12812 /* Parse the qualified declaration. */
12813 cp_parser_block_declaration (parser, statement_p);
12814 /* Restore the PEDANTIC flag. */
12815 pedantic = saved_pedantic;
12817 return;
12820 /* Peek at the next token to figure out which kind of declaration is
12821 present. */
12822 token1 = cp_lexer_peek_token (parser->lexer);
12824 /* If the next keyword is `asm', we have an asm-definition. */
12825 if (token1->keyword == RID_ASM)
12827 if (statement_p)
12828 cp_parser_commit_to_tentative_parse (parser);
12829 cp_parser_asm_definition (parser);
12831 /* If the next keyword is `namespace', we have a
12832 namespace-alias-definition. */
12833 else if (token1->keyword == RID_NAMESPACE)
12834 cp_parser_namespace_alias_definition (parser);
12835 /* If the next keyword is `using', we have a
12836 using-declaration, a using-directive, or an alias-declaration. */
12837 else if (token1->keyword == RID_USING)
12839 cp_token *token2;
12841 if (statement_p)
12842 cp_parser_commit_to_tentative_parse (parser);
12843 /* If the token after `using' is `namespace', then we have a
12844 using-directive. */
12845 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12846 if (token2->keyword == RID_NAMESPACE)
12847 cp_parser_using_directive (parser);
12848 /* If the second token after 'using' is '=', then we have an
12849 alias-declaration. */
12850 else if (cxx_dialect >= cxx11
12851 && token2->type == CPP_NAME
12852 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12853 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12854 cp_parser_alias_declaration (parser);
12855 /* Otherwise, it's a using-declaration. */
12856 else
12857 cp_parser_using_declaration (parser,
12858 /*access_declaration_p=*/false);
12860 /* If the next keyword is `__label__' we have a misplaced label
12861 declaration. */
12862 else if (token1->keyword == RID_LABEL)
12864 cp_lexer_consume_token (parser->lexer);
12865 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12866 cp_parser_skip_to_end_of_statement (parser);
12867 /* If the next token is now a `;', consume it. */
12868 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12869 cp_lexer_consume_token (parser->lexer);
12871 /* If the next token is `static_assert' we have a static assertion. */
12872 else if (token1->keyword == RID_STATIC_ASSERT)
12873 cp_parser_static_assert (parser, /*member_p=*/false);
12874 /* Anything else must be a simple-declaration. */
12875 else
12876 cp_parser_simple_declaration (parser, !statement_p,
12877 /*maybe_range_for_decl*/NULL);
12880 /* Parse a simple-declaration.
12882 simple-declaration:
12883 decl-specifier-seq [opt] init-declarator-list [opt] ;
12884 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12885 brace-or-equal-initializer ;
12887 init-declarator-list:
12888 init-declarator
12889 init-declarator-list , init-declarator
12891 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12892 function-definition as a simple-declaration.
12894 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12895 parsed declaration if it is an uninitialized single declarator not followed
12896 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12897 if present, will not be consumed. */
12899 static void
12900 cp_parser_simple_declaration (cp_parser* parser,
12901 bool function_definition_allowed_p,
12902 tree *maybe_range_for_decl)
12904 cp_decl_specifier_seq decl_specifiers;
12905 int declares_class_or_enum;
12906 bool saw_declarator;
12907 location_t comma_loc = UNKNOWN_LOCATION;
12908 location_t init_loc = UNKNOWN_LOCATION;
12910 if (maybe_range_for_decl)
12911 *maybe_range_for_decl = NULL_TREE;
12913 /* Defer access checks until we know what is being declared; the
12914 checks for names appearing in the decl-specifier-seq should be
12915 done as if we were in the scope of the thing being declared. */
12916 push_deferring_access_checks (dk_deferred);
12918 /* Parse the decl-specifier-seq. We have to keep track of whether
12919 or not the decl-specifier-seq declares a named class or
12920 enumeration type, since that is the only case in which the
12921 init-declarator-list is allowed to be empty.
12923 [dcl.dcl]
12925 In a simple-declaration, the optional init-declarator-list can be
12926 omitted only when declaring a class or enumeration, that is when
12927 the decl-specifier-seq contains either a class-specifier, an
12928 elaborated-type-specifier, or an enum-specifier. */
12929 cp_parser_decl_specifier_seq (parser,
12930 CP_PARSER_FLAGS_OPTIONAL,
12931 &decl_specifiers,
12932 &declares_class_or_enum);
12933 /* We no longer need to defer access checks. */
12934 stop_deferring_access_checks ();
12936 /* In a block scope, a valid declaration must always have a
12937 decl-specifier-seq. By not trying to parse declarators, we can
12938 resolve the declaration/expression ambiguity more quickly. */
12939 if (!function_definition_allowed_p
12940 && !decl_specifiers.any_specifiers_p)
12942 cp_parser_error (parser, "expected declaration");
12943 goto done;
12946 /* If the next two tokens are both identifiers, the code is
12947 erroneous. The usual cause of this situation is code like:
12949 T t;
12951 where "T" should name a type -- but does not. */
12952 if (!decl_specifiers.any_type_specifiers_p
12953 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12955 /* If parsing tentatively, we should commit; we really are
12956 looking at a declaration. */
12957 cp_parser_commit_to_tentative_parse (parser);
12958 /* Give up. */
12959 goto done;
12962 /* If we have seen at least one decl-specifier, and the next token
12963 is not a parenthesis, then we must be looking at a declaration.
12964 (After "int (" we might be looking at a functional cast.) */
12965 if (decl_specifiers.any_specifiers_p
12966 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12967 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12968 && !cp_parser_error_occurred (parser))
12969 cp_parser_commit_to_tentative_parse (parser);
12971 /* Look for C++17 decomposition declaration. */
12972 for (size_t n = 1; ; n++)
12973 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12974 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12975 continue;
12976 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12977 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12978 && decl_specifiers.any_specifiers_p)
12980 tree decl
12981 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12982 maybe_range_for_decl,
12983 &init_loc);
12985 /* The next token should be either a `,' or a `;'. */
12986 cp_token *token = cp_lexer_peek_token (parser->lexer);
12987 /* If it's a `;', we are done. */
12988 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12989 goto finish;
12990 /* Anything else is an error. */
12991 else
12993 /* If we have already issued an error message we don't need
12994 to issue another one. */
12995 if ((decl != error_mark_node
12996 && DECL_INITIAL (decl) != error_mark_node)
12997 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12998 cp_parser_error (parser, "expected %<,%> or %<;%>");
12999 /* Skip tokens until we reach the end of the statement. */
13000 cp_parser_skip_to_end_of_statement (parser);
13001 /* If the next token is now a `;', consume it. */
13002 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13003 cp_lexer_consume_token (parser->lexer);
13004 goto done;
13007 else
13008 break;
13010 tree last_type;
13011 bool auto_specifier_p;
13012 /* NULL_TREE if both variable and function declaration are allowed,
13013 error_mark_node if function declaration are not allowed and
13014 a FUNCTION_DECL that should be diagnosed if it is followed by
13015 variable declarations. */
13016 tree auto_function_declaration;
13018 last_type = NULL_TREE;
13019 auto_specifier_p
13020 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
13021 auto_function_declaration = NULL_TREE;
13023 /* Keep going until we hit the `;' at the end of the simple
13024 declaration. */
13025 saw_declarator = false;
13026 while (cp_lexer_next_token_is_not (parser->lexer,
13027 CPP_SEMICOLON))
13029 cp_token *token;
13030 bool function_definition_p;
13031 tree decl;
13032 tree auto_result = NULL_TREE;
13034 if (saw_declarator)
13036 /* If we are processing next declarator, comma is expected */
13037 token = cp_lexer_peek_token (parser->lexer);
13038 gcc_assert (token->type == CPP_COMMA);
13039 cp_lexer_consume_token (parser->lexer);
13040 if (maybe_range_for_decl)
13042 *maybe_range_for_decl = error_mark_node;
13043 if (comma_loc == UNKNOWN_LOCATION)
13044 comma_loc = token->location;
13047 else
13048 saw_declarator = true;
13050 /* Parse the init-declarator. */
13051 decl = cp_parser_init_declarator (parser, &decl_specifiers,
13052 /*checks=*/NULL,
13053 function_definition_allowed_p,
13054 /*member_p=*/false,
13055 declares_class_or_enum,
13056 &function_definition_p,
13057 maybe_range_for_decl,
13058 &init_loc,
13059 &auto_result);
13060 /* If an error occurred while parsing tentatively, exit quickly.
13061 (That usually happens when in the body of a function; each
13062 statement is treated as a declaration-statement until proven
13063 otherwise.) */
13064 if (cp_parser_error_occurred (parser))
13065 goto done;
13067 if (auto_specifier_p && cxx_dialect >= cxx14)
13069 /* If the init-declarator-list contains more than one
13070 init-declarator, they shall all form declarations of
13071 variables. */
13072 if (auto_function_declaration == NULL_TREE)
13073 auto_function_declaration
13074 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
13075 else if (TREE_CODE (decl) == FUNCTION_DECL
13076 || auto_function_declaration != error_mark_node)
13078 error_at (decl_specifiers.locations[ds_type_spec],
13079 "non-variable %qD in declaration with more than one "
13080 "declarator with placeholder type",
13081 TREE_CODE (decl) == FUNCTION_DECL
13082 ? decl : auto_function_declaration);
13083 auto_function_declaration = error_mark_node;
13087 if (auto_result
13088 && (!processing_template_decl || !type_uses_auto (auto_result)))
13090 if (last_type
13091 && last_type != error_mark_node
13092 && !same_type_p (auto_result, last_type))
13094 /* If the list of declarators contains more than one declarator,
13095 the type of each declared variable is determined as described
13096 above. If the type deduced for the template parameter U is not
13097 the same in each deduction, the program is ill-formed. */
13098 error_at (decl_specifiers.locations[ds_type_spec],
13099 "inconsistent deduction for %qT: %qT and then %qT",
13100 decl_specifiers.type, last_type, auto_result);
13101 last_type = error_mark_node;
13103 else
13104 last_type = auto_result;
13107 /* Handle function definitions specially. */
13108 if (function_definition_p)
13110 /* If the next token is a `,', then we are probably
13111 processing something like:
13113 void f() {}, *p;
13115 which is erroneous. */
13116 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13118 cp_token *token = cp_lexer_peek_token (parser->lexer);
13119 error_at (token->location,
13120 "mixing"
13121 " declarations and function-definitions is forbidden");
13123 /* Otherwise, we're done with the list of declarators. */
13124 else
13126 pop_deferring_access_checks ();
13127 return;
13130 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13131 *maybe_range_for_decl = decl;
13132 /* The next token should be either a `,' or a `;'. */
13133 token = cp_lexer_peek_token (parser->lexer);
13134 /* If it's a `,', there are more declarators to come. */
13135 if (token->type == CPP_COMMA)
13136 /* will be consumed next time around */;
13137 /* If it's a `;', we are done. */
13138 else if (token->type == CPP_SEMICOLON)
13139 break;
13140 else if (maybe_range_for_decl)
13142 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13143 permerror (decl_specifiers.locations[ds_type_spec],
13144 "types may not be defined in a for-range-declaration");
13145 break;
13147 /* Anything else is an error. */
13148 else
13150 /* If we have already issued an error message we don't need
13151 to issue another one. */
13152 if ((decl != error_mark_node
13153 && DECL_INITIAL (decl) != error_mark_node)
13154 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13155 cp_parser_error (parser, "expected %<,%> or %<;%>");
13156 /* Skip tokens until we reach the end of the statement. */
13157 cp_parser_skip_to_end_of_statement (parser);
13158 /* If the next token is now a `;', consume it. */
13159 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13160 cp_lexer_consume_token (parser->lexer);
13161 goto done;
13163 /* After the first time around, a function-definition is not
13164 allowed -- even if it was OK at first. For example:
13166 int i, f() {}
13168 is not valid. */
13169 function_definition_allowed_p = false;
13172 /* Issue an error message if no declarators are present, and the
13173 decl-specifier-seq does not itself declare a class or
13174 enumeration: [dcl.dcl]/3. */
13175 if (!saw_declarator)
13177 if (cp_parser_declares_only_class_p (parser))
13179 if (!declares_class_or_enum
13180 && decl_specifiers.type
13181 && OVERLOAD_TYPE_P (decl_specifiers.type))
13182 /* Ensure an error is issued anyway when finish_decltype_type,
13183 called via cp_parser_decl_specifier_seq, returns a class or
13184 an enumeration (c++/51786). */
13185 decl_specifiers.type = NULL_TREE;
13186 shadow_tag (&decl_specifiers);
13188 /* Perform any deferred access checks. */
13189 perform_deferred_access_checks (tf_warning_or_error);
13192 /* Consume the `;'. */
13193 finish:
13194 if (!maybe_range_for_decl)
13195 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13196 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13198 if (init_loc != UNKNOWN_LOCATION)
13199 error_at (init_loc, "initializer in range-based %<for%> loop");
13200 if (comma_loc != UNKNOWN_LOCATION)
13201 error_at (comma_loc,
13202 "multiple declarations in range-based %<for%> loop");
13205 done:
13206 pop_deferring_access_checks ();
13209 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13210 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13211 initializer ; */
13213 static tree
13214 cp_parser_decomposition_declaration (cp_parser *parser,
13215 cp_decl_specifier_seq *decl_specifiers,
13216 tree *maybe_range_for_decl,
13217 location_t *init_loc)
13219 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13220 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13221 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13223 /* Parse the identifier-list. */
13224 auto_vec<cp_expr, 10> v;
13225 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13226 while (true)
13228 cp_expr e = cp_parser_identifier (parser);
13229 if (e.get_value () == error_mark_node)
13230 break;
13231 v.safe_push (e);
13232 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13233 break;
13234 cp_lexer_consume_token (parser->lexer);
13237 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13238 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13240 end_loc = UNKNOWN_LOCATION;
13241 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13242 false);
13243 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13244 cp_lexer_consume_token (parser->lexer);
13245 else
13247 cp_parser_skip_to_end_of_statement (parser);
13248 return error_mark_node;
13252 if (cxx_dialect < cxx1z)
13253 pedwarn (loc, 0, "structured bindings only available with "
13254 "-std=c++1z or -std=gnu++1z");
13256 tree pushed_scope;
13257 cp_declarator *declarator = make_declarator (cdk_decomp);
13258 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13259 declarator->id_loc = loc;
13260 if (ref_qual != REF_QUAL_NONE)
13261 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13262 ref_qual == REF_QUAL_RVALUE,
13263 NULL_TREE);
13264 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13265 NULL_TREE, decl_specifiers->attributes,
13266 &pushed_scope);
13267 tree orig_decl = decl;
13269 unsigned int i;
13270 cp_expr e;
13271 cp_decl_specifier_seq decl_specs;
13272 clear_decl_specs (&decl_specs);
13273 decl_specs.type = make_auto ();
13274 tree prev = decl;
13275 FOR_EACH_VEC_ELT (v, i, e)
13277 if (i == 0)
13278 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13279 else
13280 declarator->u.id.unqualified_name = e.get_value ();
13281 declarator->id_loc = e.get_location ();
13282 tree elt_pushed_scope;
13283 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13284 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13285 if (decl2 == error_mark_node)
13286 decl = error_mark_node;
13287 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13289 /* Ensure we've diagnosed redeclaration if we aren't creating
13290 a new VAR_DECL. */
13291 gcc_assert (errorcount);
13292 decl = error_mark_node;
13294 else
13295 prev = decl2;
13296 if (elt_pushed_scope)
13297 pop_scope (elt_pushed_scope);
13300 if (v.is_empty ())
13302 error_at (loc, "empty structured binding declaration");
13303 decl = error_mark_node;
13306 if (maybe_range_for_decl == NULL
13307 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13309 bool non_constant_p = false, is_direct_init = false;
13310 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13311 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13312 &non_constant_p);
13313 if (initializer == NULL_TREE
13314 || (TREE_CODE (initializer) == TREE_LIST
13315 && TREE_CHAIN (initializer))
13316 || (TREE_CODE (initializer) == CONSTRUCTOR
13317 && CONSTRUCTOR_NELTS (initializer) != 1))
13319 error_at (loc, "invalid initializer for structured binding "
13320 "declaration");
13321 initializer = error_mark_node;
13324 if (decl != error_mark_node)
13326 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13327 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13328 cp_finish_decomp (decl, prev, v.length ());
13331 else if (decl != error_mark_node)
13333 *maybe_range_for_decl = prev;
13334 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13335 the underlying DECL. */
13336 cp_finish_decomp (decl, prev, v.length ());
13339 if (pushed_scope)
13340 pop_scope (pushed_scope);
13342 if (decl == error_mark_node && DECL_P (orig_decl))
13344 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13345 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13348 return decl;
13351 /* Parse a decl-specifier-seq.
13353 decl-specifier-seq:
13354 decl-specifier-seq [opt] decl-specifier
13355 decl-specifier attribute-specifier-seq [opt] (C++11)
13357 decl-specifier:
13358 storage-class-specifier
13359 type-specifier
13360 function-specifier
13361 friend
13362 typedef
13364 GNU Extension:
13366 decl-specifier:
13367 attributes
13369 Concepts Extension:
13371 decl-specifier:
13372 concept
13374 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13376 The parser flags FLAGS is used to control type-specifier parsing.
13378 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13379 flags:
13381 1: one of the decl-specifiers is an elaborated-type-specifier
13382 (i.e., a type declaration)
13383 2: one of the decl-specifiers is an enum-specifier or a
13384 class-specifier (i.e., a type definition)
13388 static void
13389 cp_parser_decl_specifier_seq (cp_parser* parser,
13390 cp_parser_flags flags,
13391 cp_decl_specifier_seq *decl_specs,
13392 int* declares_class_or_enum)
13394 bool constructor_possible_p = !parser->in_declarator_p;
13395 bool found_decl_spec = false;
13396 cp_token *start_token = NULL;
13397 cp_decl_spec ds;
13399 /* Clear DECL_SPECS. */
13400 clear_decl_specs (decl_specs);
13402 /* Assume no class or enumeration type is declared. */
13403 *declares_class_or_enum = 0;
13405 /* Keep reading specifiers until there are no more to read. */
13406 while (true)
13408 bool constructor_p;
13409 cp_token *token;
13410 ds = ds_last;
13412 /* Peek at the next token. */
13413 token = cp_lexer_peek_token (parser->lexer);
13415 /* Save the first token of the decl spec list for error
13416 reporting. */
13417 if (!start_token)
13418 start_token = token;
13419 /* Handle attributes. */
13420 if (cp_next_tokens_can_be_attribute_p (parser))
13422 /* Parse the attributes. */
13423 tree attrs = cp_parser_attributes_opt (parser);
13425 /* In a sequence of declaration specifiers, c++11 attributes
13426 appertain to the type that precede them. In that case
13427 [dcl.spec]/1 says:
13429 The attribute-specifier-seq affects the type only for
13430 the declaration it appears in, not other declarations
13431 involving the same type.
13433 But for now let's force the user to position the
13434 attribute either at the beginning of the declaration or
13435 after the declarator-id, which would clearly mean that it
13436 applies to the declarator. */
13437 if (cxx11_attribute_p (attrs))
13439 if (!found_decl_spec)
13440 /* The c++11 attribute is at the beginning of the
13441 declaration. It appertains to the entity being
13442 declared. */;
13443 else
13445 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13447 /* This is an attribute following a
13448 class-specifier. */
13449 if (decl_specs->type_definition_p)
13450 warn_misplaced_attr_for_class_type (token->location,
13451 decl_specs->type);
13452 attrs = NULL_TREE;
13454 else
13456 decl_specs->std_attributes
13457 = chainon (decl_specs->std_attributes,
13458 attrs);
13459 if (decl_specs->locations[ds_std_attribute] == 0)
13460 decl_specs->locations[ds_std_attribute] = token->location;
13462 continue;
13466 decl_specs->attributes
13467 = chainon (decl_specs->attributes,
13468 attrs);
13469 if (decl_specs->locations[ds_attribute] == 0)
13470 decl_specs->locations[ds_attribute] = token->location;
13471 continue;
13473 /* Assume we will find a decl-specifier keyword. */
13474 found_decl_spec = true;
13475 /* If the next token is an appropriate keyword, we can simply
13476 add it to the list. */
13477 switch (token->keyword)
13479 /* decl-specifier:
13480 friend
13481 constexpr */
13482 case RID_FRIEND:
13483 if (!at_class_scope_p ())
13485 gcc_rich_location richloc (token->location);
13486 richloc.add_fixit_remove ();
13487 error_at_rich_loc (&richloc, "%<friend%> used outside of class");
13488 cp_lexer_purge_token (parser->lexer);
13490 else
13492 ds = ds_friend;
13493 /* Consume the token. */
13494 cp_lexer_consume_token (parser->lexer);
13496 break;
13498 case RID_CONSTEXPR:
13499 ds = ds_constexpr;
13500 cp_lexer_consume_token (parser->lexer);
13501 break;
13503 case RID_CONCEPT:
13504 ds = ds_concept;
13505 cp_lexer_consume_token (parser->lexer);
13506 break;
13508 /* function-specifier:
13509 inline
13510 virtual
13511 explicit */
13512 case RID_INLINE:
13513 case RID_VIRTUAL:
13514 case RID_EXPLICIT:
13515 cp_parser_function_specifier_opt (parser, decl_specs);
13516 break;
13518 /* decl-specifier:
13519 typedef */
13520 case RID_TYPEDEF:
13521 ds = ds_typedef;
13522 /* Consume the token. */
13523 cp_lexer_consume_token (parser->lexer);
13524 /* A constructor declarator cannot appear in a typedef. */
13525 constructor_possible_p = false;
13526 /* The "typedef" keyword can only occur in a declaration; we
13527 may as well commit at this point. */
13528 cp_parser_commit_to_tentative_parse (parser);
13530 if (decl_specs->storage_class != sc_none)
13531 decl_specs->conflicting_specifiers_p = true;
13532 break;
13534 /* storage-class-specifier:
13535 auto
13536 register
13537 static
13538 extern
13539 mutable
13541 GNU Extension:
13542 thread */
13543 case RID_AUTO:
13544 if (cxx_dialect == cxx98)
13546 /* Consume the token. */
13547 cp_lexer_consume_token (parser->lexer);
13549 /* Complain about `auto' as a storage specifier, if
13550 we're complaining about C++0x compatibility. */
13551 gcc_rich_location richloc (token->location);
13552 richloc.add_fixit_remove ();
13553 warning_at_rich_loc (&richloc, OPT_Wc__11_compat,
13554 "%<auto%> changes meaning in C++11; "
13555 "please remove it");
13557 /* Set the storage class anyway. */
13558 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13559 token);
13561 else
13562 /* C++0x auto type-specifier. */
13563 found_decl_spec = false;
13564 break;
13566 case RID_REGISTER:
13567 case RID_STATIC:
13568 case RID_EXTERN:
13569 case RID_MUTABLE:
13570 /* Consume the token. */
13571 cp_lexer_consume_token (parser->lexer);
13572 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13573 token);
13574 break;
13575 case RID_THREAD:
13576 /* Consume the token. */
13577 ds = ds_thread;
13578 cp_lexer_consume_token (parser->lexer);
13579 break;
13581 default:
13582 /* We did not yet find a decl-specifier yet. */
13583 found_decl_spec = false;
13584 break;
13587 if (found_decl_spec
13588 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13589 && token->keyword != RID_CONSTEXPR)
13590 error ("decl-specifier invalid in condition");
13592 if (found_decl_spec
13593 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13594 && token->keyword != RID_MUTABLE
13595 && token->keyword != RID_CONSTEXPR)
13596 error_at (token->location, "%qD invalid in lambda",
13597 ridpointers[token->keyword]);
13599 if (ds != ds_last)
13600 set_and_check_decl_spec_loc (decl_specs, ds, token);
13602 /* Constructors are a special case. The `S' in `S()' is not a
13603 decl-specifier; it is the beginning of the declarator. */
13604 constructor_p
13605 = (!found_decl_spec
13606 && constructor_possible_p
13607 && (cp_parser_constructor_declarator_p
13608 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13610 /* If we don't have a DECL_SPEC yet, then we must be looking at
13611 a type-specifier. */
13612 if (!found_decl_spec && !constructor_p)
13614 int decl_spec_declares_class_or_enum;
13615 bool is_cv_qualifier;
13616 tree type_spec;
13618 type_spec
13619 = cp_parser_type_specifier (parser, flags,
13620 decl_specs,
13621 /*is_declaration=*/true,
13622 &decl_spec_declares_class_or_enum,
13623 &is_cv_qualifier);
13624 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13626 /* If this type-specifier referenced a user-defined type
13627 (a typedef, class-name, etc.), then we can't allow any
13628 more such type-specifiers henceforth.
13630 [dcl.spec]
13632 The longest sequence of decl-specifiers that could
13633 possibly be a type name is taken as the
13634 decl-specifier-seq of a declaration. The sequence shall
13635 be self-consistent as described below.
13637 [dcl.type]
13639 As a general rule, at most one type-specifier is allowed
13640 in the complete decl-specifier-seq of a declaration. The
13641 only exceptions are the following:
13643 -- const or volatile can be combined with any other
13644 type-specifier.
13646 -- signed or unsigned can be combined with char, long,
13647 short, or int.
13649 -- ..
13651 Example:
13653 typedef char* Pc;
13654 void g (const int Pc);
13656 Here, Pc is *not* part of the decl-specifier seq; it's
13657 the declarator. Therefore, once we see a type-specifier
13658 (other than a cv-qualifier), we forbid any additional
13659 user-defined types. We *do* still allow things like `int
13660 int' to be considered a decl-specifier-seq, and issue the
13661 error message later. */
13662 if (type_spec && !is_cv_qualifier)
13663 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13664 /* A constructor declarator cannot follow a type-specifier. */
13665 if (type_spec)
13667 constructor_possible_p = false;
13668 found_decl_spec = true;
13669 if (!is_cv_qualifier)
13670 decl_specs->any_type_specifiers_p = true;
13674 /* If we still do not have a DECL_SPEC, then there are no more
13675 decl-specifiers. */
13676 if (!found_decl_spec)
13677 break;
13679 decl_specs->any_specifiers_p = true;
13680 /* After we see one decl-specifier, further decl-specifiers are
13681 always optional. */
13682 flags |= CP_PARSER_FLAGS_OPTIONAL;
13685 /* Don't allow a friend specifier with a class definition. */
13686 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13687 && (*declares_class_or_enum & 2))
13688 error_at (decl_specs->locations[ds_friend],
13689 "class definition may not be declared a friend");
13692 /* Parse an (optional) storage-class-specifier.
13694 storage-class-specifier:
13695 auto
13696 register
13697 static
13698 extern
13699 mutable
13701 GNU Extension:
13703 storage-class-specifier:
13704 thread
13706 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13708 static tree
13709 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13711 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13713 case RID_AUTO:
13714 if (cxx_dialect != cxx98)
13715 return NULL_TREE;
13716 /* Fall through for C++98. */
13717 gcc_fallthrough ();
13719 case RID_REGISTER:
13720 case RID_STATIC:
13721 case RID_EXTERN:
13722 case RID_MUTABLE:
13723 case RID_THREAD:
13724 /* Consume the token. */
13725 return cp_lexer_consume_token (parser->lexer)->u.value;
13727 default:
13728 return NULL_TREE;
13732 /* Parse an (optional) function-specifier.
13734 function-specifier:
13735 inline
13736 virtual
13737 explicit
13739 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13740 Updates DECL_SPECS, if it is non-NULL. */
13742 static tree
13743 cp_parser_function_specifier_opt (cp_parser* parser,
13744 cp_decl_specifier_seq *decl_specs)
13746 cp_token *token = cp_lexer_peek_token (parser->lexer);
13747 switch (token->keyword)
13749 case RID_INLINE:
13750 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13751 break;
13753 case RID_VIRTUAL:
13754 /* 14.5.2.3 [temp.mem]
13756 A member function template shall not be virtual. */
13757 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13758 && current_class_type)
13759 error_at (token->location, "templates may not be %<virtual%>");
13760 else
13761 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13762 break;
13764 case RID_EXPLICIT:
13765 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13766 break;
13768 default:
13769 return NULL_TREE;
13772 /* Consume the token. */
13773 return cp_lexer_consume_token (parser->lexer)->u.value;
13776 /* Parse a linkage-specification.
13778 linkage-specification:
13779 extern string-literal { declaration-seq [opt] }
13780 extern string-literal declaration */
13782 static void
13783 cp_parser_linkage_specification (cp_parser* parser)
13785 tree linkage;
13787 /* Look for the `extern' keyword. */
13788 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13790 /* Look for the string-literal. */
13791 linkage = cp_parser_string_literal (parser, false, false);
13793 /* Transform the literal into an identifier. If the literal is a
13794 wide-character string, or contains embedded NULs, then we can't
13795 handle it as the user wants. */
13796 if (strlen (TREE_STRING_POINTER (linkage))
13797 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13799 cp_parser_error (parser, "invalid linkage-specification");
13800 /* Assume C++ linkage. */
13801 linkage = lang_name_cplusplus;
13803 else
13804 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13806 /* We're now using the new linkage. */
13807 push_lang_context (linkage);
13809 /* If the next token is a `{', then we're using the first
13810 production. */
13811 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13813 cp_ensure_no_omp_declare_simd (parser);
13814 cp_ensure_no_oacc_routine (parser);
13816 /* Consume the `{' token. */
13817 matching_braces braces;
13818 braces.consume_open (parser)->location;
13819 /* Parse the declarations. */
13820 cp_parser_declaration_seq_opt (parser);
13821 /* Look for the closing `}'. */
13822 braces.require_close (parser);
13824 /* Otherwise, there's just one declaration. */
13825 else
13827 bool saved_in_unbraced_linkage_specification_p;
13829 saved_in_unbraced_linkage_specification_p
13830 = parser->in_unbraced_linkage_specification_p;
13831 parser->in_unbraced_linkage_specification_p = true;
13832 cp_parser_declaration (parser);
13833 parser->in_unbraced_linkage_specification_p
13834 = saved_in_unbraced_linkage_specification_p;
13837 /* We're done with the linkage-specification. */
13838 pop_lang_context ();
13841 /* Parse a static_assert-declaration.
13843 static_assert-declaration:
13844 static_assert ( constant-expression , string-literal ) ;
13845 static_assert ( constant-expression ) ; (C++1Z)
13847 If MEMBER_P, this static_assert is a class member. */
13849 static void
13850 cp_parser_static_assert(cp_parser *parser, bool member_p)
13852 tree condition;
13853 tree message;
13854 cp_token *token;
13855 location_t saved_loc;
13856 bool dummy;
13858 /* Peek at the `static_assert' token so we can keep track of exactly
13859 where the static assertion started. */
13860 token = cp_lexer_peek_token (parser->lexer);
13861 saved_loc = token->location;
13863 /* Look for the `static_assert' keyword. */
13864 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13865 RT_STATIC_ASSERT))
13866 return;
13868 /* We know we are in a static assertion; commit to any tentative
13869 parse. */
13870 if (cp_parser_parsing_tentatively (parser))
13871 cp_parser_commit_to_tentative_parse (parser);
13873 /* Parse the `(' starting the static assertion condition. */
13874 matching_parens parens;
13875 parens.require_open (parser);
13877 /* Parse the constant-expression. Allow a non-constant expression
13878 here in order to give better diagnostics in finish_static_assert. */
13879 condition =
13880 cp_parser_constant_expression (parser,
13881 /*allow_non_constant_p=*/true,
13882 /*non_constant_p=*/&dummy);
13884 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13886 if (cxx_dialect < cxx1z)
13887 pedwarn (input_location, OPT_Wpedantic,
13888 "static_assert without a message "
13889 "only available with -std=c++1z or -std=gnu++1z");
13890 /* Eat the ')' */
13891 cp_lexer_consume_token (parser->lexer);
13892 message = build_string (1, "");
13893 TREE_TYPE (message) = char_array_type_node;
13894 fix_string_type (message);
13896 else
13898 /* Parse the separating `,'. */
13899 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13901 /* Parse the string-literal message. */
13902 message = cp_parser_string_literal (parser,
13903 /*translate=*/false,
13904 /*wide_ok=*/true);
13906 /* A `)' completes the static assertion. */
13907 if (!parens.require_close (parser))
13908 cp_parser_skip_to_closing_parenthesis (parser,
13909 /*recovering=*/true,
13910 /*or_comma=*/false,
13911 /*consume_paren=*/true);
13914 /* A semicolon terminates the declaration. */
13915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13917 /* Complete the static assertion, which may mean either processing
13918 the static assert now or saving it for template instantiation. */
13919 finish_static_assert (condition, message, saved_loc, member_p);
13922 /* Parse the expression in decltype ( expression ). */
13924 static tree
13925 cp_parser_decltype_expr (cp_parser *parser,
13926 bool &id_expression_or_member_access_p)
13928 cp_token *id_expr_start_token;
13929 tree expr;
13931 /* Since we're going to preserve any side-effects from this parse, set up a
13932 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13933 in the expression. */
13934 tentative_firewall firewall (parser);
13936 /* First, try parsing an id-expression. */
13937 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13938 cp_parser_parse_tentatively (parser);
13939 expr = cp_parser_id_expression (parser,
13940 /*template_keyword_p=*/false,
13941 /*check_dependency_p=*/true,
13942 /*template_p=*/NULL,
13943 /*declarator_p=*/false,
13944 /*optional_p=*/false);
13946 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13948 bool non_integral_constant_expression_p = false;
13949 tree id_expression = expr;
13950 cp_id_kind idk;
13951 const char *error_msg;
13953 if (identifier_p (expr))
13954 /* Lookup the name we got back from the id-expression. */
13955 expr = cp_parser_lookup_name_simple (parser, expr,
13956 id_expr_start_token->location);
13958 if (expr
13959 && expr != error_mark_node
13960 && TREE_CODE (expr) != TYPE_DECL
13961 && (TREE_CODE (expr) != BIT_NOT_EXPR
13962 || !TYPE_P (TREE_OPERAND (expr, 0)))
13963 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13965 /* Complete lookup of the id-expression. */
13966 expr = (finish_id_expression
13967 (id_expression, expr, parser->scope, &idk,
13968 /*integral_constant_expression_p=*/false,
13969 /*allow_non_integral_constant_expression_p=*/true,
13970 &non_integral_constant_expression_p,
13971 /*template_p=*/false,
13972 /*done=*/true,
13973 /*address_p=*/false,
13974 /*template_arg_p=*/false,
13975 &error_msg,
13976 id_expr_start_token->location));
13978 if (expr == error_mark_node)
13979 /* We found an id-expression, but it was something that we
13980 should not have found. This is an error, not something
13981 we can recover from, so note that we found an
13982 id-expression and we'll recover as gracefully as
13983 possible. */
13984 id_expression_or_member_access_p = true;
13987 if (expr
13988 && expr != error_mark_node
13989 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13990 /* We have an id-expression. */
13991 id_expression_or_member_access_p = true;
13994 if (!id_expression_or_member_access_p)
13996 /* Abort the id-expression parse. */
13997 cp_parser_abort_tentative_parse (parser);
13999 /* Parsing tentatively, again. */
14000 cp_parser_parse_tentatively (parser);
14002 /* Parse a class member access. */
14003 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
14004 /*cast_p=*/false, /*decltype*/true,
14005 /*member_access_only_p=*/true, NULL);
14007 if (expr
14008 && expr != error_mark_node
14009 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
14010 /* We have an id-expression. */
14011 id_expression_or_member_access_p = true;
14014 if (id_expression_or_member_access_p)
14015 /* We have parsed the complete id-expression or member access. */
14016 cp_parser_parse_definitely (parser);
14017 else
14019 /* Abort our attempt to parse an id-expression or member access
14020 expression. */
14021 cp_parser_abort_tentative_parse (parser);
14023 /* Parse a full expression. */
14024 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
14025 /*decltype_p=*/true);
14028 return expr;
14031 /* Parse a `decltype' type. Returns the type.
14033 simple-type-specifier:
14034 decltype ( expression )
14035 C++14 proposal:
14036 decltype ( auto ) */
14038 static tree
14039 cp_parser_decltype (cp_parser *parser)
14041 tree expr;
14042 bool id_expression_or_member_access_p = false;
14043 const char *saved_message;
14044 bool saved_integral_constant_expression_p;
14045 bool saved_non_integral_constant_expression_p;
14046 bool saved_greater_than_is_operator_p;
14047 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
14049 if (start_token->type == CPP_DECLTYPE)
14051 /* Already parsed. */
14052 cp_lexer_consume_token (parser->lexer);
14053 return saved_checks_value (start_token->u.tree_check_value);
14056 /* Look for the `decltype' token. */
14057 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
14058 return error_mark_node;
14060 /* Parse the opening `('. */
14061 matching_parens parens;
14062 if (!parens.require_open (parser))
14063 return error_mark_node;
14065 /* decltype (auto) */
14066 if (cxx_dialect >= cxx14
14067 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
14069 cp_lexer_consume_token (parser->lexer);
14070 if (!parens.require_close (parser))
14071 return error_mark_node;
14072 expr = make_decltype_auto ();
14073 AUTO_IS_DECLTYPE (expr) = true;
14074 goto rewrite;
14077 /* Types cannot be defined in a `decltype' expression. Save away the
14078 old message. */
14079 saved_message = parser->type_definition_forbidden_message;
14081 /* And create the new one. */
14082 parser->type_definition_forbidden_message
14083 = G_("types may not be defined in %<decltype%> expressions");
14085 /* The restrictions on constant-expressions do not apply inside
14086 decltype expressions. */
14087 saved_integral_constant_expression_p
14088 = parser->integral_constant_expression_p;
14089 saved_non_integral_constant_expression_p
14090 = parser->non_integral_constant_expression_p;
14091 parser->integral_constant_expression_p = false;
14093 /* Within a parenthesized expression, a `>' token is always
14094 the greater-than operator. */
14095 saved_greater_than_is_operator_p
14096 = parser->greater_than_is_operator_p;
14097 parser->greater_than_is_operator_p = true;
14099 /* Do not actually evaluate the expression. */
14100 ++cp_unevaluated_operand;
14102 /* Do not warn about problems with the expression. */
14103 ++c_inhibit_evaluation_warnings;
14105 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14107 /* Go back to evaluating expressions. */
14108 --cp_unevaluated_operand;
14109 --c_inhibit_evaluation_warnings;
14111 /* The `>' token might be the end of a template-id or
14112 template-parameter-list now. */
14113 parser->greater_than_is_operator_p
14114 = saved_greater_than_is_operator_p;
14116 /* Restore the old message and the integral constant expression
14117 flags. */
14118 parser->type_definition_forbidden_message = saved_message;
14119 parser->integral_constant_expression_p
14120 = saved_integral_constant_expression_p;
14121 parser->non_integral_constant_expression_p
14122 = saved_non_integral_constant_expression_p;
14124 /* Parse to the closing `)'. */
14125 if (!parens.require_close (parser))
14127 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14128 /*consume_paren=*/true);
14129 return error_mark_node;
14132 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14133 tf_warning_or_error);
14135 rewrite:
14136 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14137 it again. */
14138 start_token->type = CPP_DECLTYPE;
14139 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14140 start_token->u.tree_check_value->value = expr;
14141 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14142 start_token->keyword = RID_MAX;
14143 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14145 return expr;
14148 /* Special member functions [gram.special] */
14150 /* Parse a conversion-function-id.
14152 conversion-function-id:
14153 operator conversion-type-id
14155 Returns an IDENTIFIER_NODE representing the operator. */
14157 static tree
14158 cp_parser_conversion_function_id (cp_parser* parser)
14160 tree type;
14161 tree saved_scope;
14162 tree saved_qualifying_scope;
14163 tree saved_object_scope;
14164 tree pushed_scope = NULL_TREE;
14166 /* Look for the `operator' token. */
14167 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14168 return error_mark_node;
14169 /* When we parse the conversion-type-id, the current scope will be
14170 reset. However, we need that information in able to look up the
14171 conversion function later, so we save it here. */
14172 saved_scope = parser->scope;
14173 saved_qualifying_scope = parser->qualifying_scope;
14174 saved_object_scope = parser->object_scope;
14175 /* We must enter the scope of the class so that the names of
14176 entities declared within the class are available in the
14177 conversion-type-id. For example, consider:
14179 struct S {
14180 typedef int I;
14181 operator I();
14184 S::operator I() { ... }
14186 In order to see that `I' is a type-name in the definition, we
14187 must be in the scope of `S'. */
14188 if (saved_scope)
14189 pushed_scope = push_scope (saved_scope);
14190 /* Parse the conversion-type-id. */
14191 type = cp_parser_conversion_type_id (parser);
14192 /* Leave the scope of the class, if any. */
14193 if (pushed_scope)
14194 pop_scope (pushed_scope);
14195 /* Restore the saved scope. */
14196 parser->scope = saved_scope;
14197 parser->qualifying_scope = saved_qualifying_scope;
14198 parser->object_scope = saved_object_scope;
14199 /* If the TYPE is invalid, indicate failure. */
14200 if (type == error_mark_node)
14201 return error_mark_node;
14202 return make_conv_op_name (type);
14205 /* Parse a conversion-type-id:
14207 conversion-type-id:
14208 type-specifier-seq conversion-declarator [opt]
14210 Returns the TYPE specified. */
14212 static tree
14213 cp_parser_conversion_type_id (cp_parser* parser)
14215 tree attributes;
14216 cp_decl_specifier_seq type_specifiers;
14217 cp_declarator *declarator;
14218 tree type_specified;
14219 const char *saved_message;
14221 /* Parse the attributes. */
14222 attributes = cp_parser_attributes_opt (parser);
14224 saved_message = parser->type_definition_forbidden_message;
14225 parser->type_definition_forbidden_message
14226 = G_("types may not be defined in a conversion-type-id");
14228 /* Parse the type-specifiers. */
14229 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14230 /*is_trailing_return=*/false,
14231 &type_specifiers);
14233 parser->type_definition_forbidden_message = saved_message;
14235 /* If that didn't work, stop. */
14236 if (type_specifiers.type == error_mark_node)
14237 return error_mark_node;
14238 /* Parse the conversion-declarator. */
14239 declarator = cp_parser_conversion_declarator_opt (parser);
14241 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14242 /*initialized=*/0, &attributes);
14243 if (attributes)
14244 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14246 /* Don't give this error when parsing tentatively. This happens to
14247 work because we always parse this definitively once. */
14248 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14249 && type_uses_auto (type_specified))
14251 if (cxx_dialect < cxx14)
14253 error ("invalid use of %<auto%> in conversion operator");
14254 return error_mark_node;
14256 else if (template_parm_scope_p ())
14257 warning (0, "use of %<auto%> in member template "
14258 "conversion operator can never be deduced");
14261 return type_specified;
14264 /* Parse an (optional) conversion-declarator.
14266 conversion-declarator:
14267 ptr-operator conversion-declarator [opt]
14271 static cp_declarator *
14272 cp_parser_conversion_declarator_opt (cp_parser* parser)
14274 enum tree_code code;
14275 tree class_type, std_attributes = NULL_TREE;
14276 cp_cv_quals cv_quals;
14278 /* We don't know if there's a ptr-operator next, or not. */
14279 cp_parser_parse_tentatively (parser);
14280 /* Try the ptr-operator. */
14281 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14282 &std_attributes);
14283 /* If it worked, look for more conversion-declarators. */
14284 if (cp_parser_parse_definitely (parser))
14286 cp_declarator *declarator;
14288 /* Parse another optional declarator. */
14289 declarator = cp_parser_conversion_declarator_opt (parser);
14291 declarator = cp_parser_make_indirect_declarator
14292 (code, class_type, cv_quals, declarator, std_attributes);
14294 return declarator;
14297 return NULL;
14300 /* Parse an (optional) ctor-initializer.
14302 ctor-initializer:
14303 : mem-initializer-list
14305 Returns TRUE iff the ctor-initializer was actually present. */
14307 static bool
14308 cp_parser_ctor_initializer_opt (cp_parser* parser)
14310 /* If the next token is not a `:', then there is no
14311 ctor-initializer. */
14312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14314 /* Do default initialization of any bases and members. */
14315 if (DECL_CONSTRUCTOR_P (current_function_decl))
14316 finish_mem_initializers (NULL_TREE);
14318 return false;
14321 /* Consume the `:' token. */
14322 cp_lexer_consume_token (parser->lexer);
14323 /* And the mem-initializer-list. */
14324 cp_parser_mem_initializer_list (parser);
14326 return true;
14329 /* Parse a mem-initializer-list.
14331 mem-initializer-list:
14332 mem-initializer ... [opt]
14333 mem-initializer ... [opt] , mem-initializer-list */
14335 static void
14336 cp_parser_mem_initializer_list (cp_parser* parser)
14338 tree mem_initializer_list = NULL_TREE;
14339 tree target_ctor = error_mark_node;
14340 cp_token *token = cp_lexer_peek_token (parser->lexer);
14342 /* Let the semantic analysis code know that we are starting the
14343 mem-initializer-list. */
14344 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14345 error_at (token->location,
14346 "only constructors take member initializers");
14348 /* Loop through the list. */
14349 while (true)
14351 tree mem_initializer;
14353 token = cp_lexer_peek_token (parser->lexer);
14354 /* Parse the mem-initializer. */
14355 mem_initializer = cp_parser_mem_initializer (parser);
14356 /* If the next token is a `...', we're expanding member initializers. */
14357 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14359 /* Consume the `...'. */
14360 cp_lexer_consume_token (parser->lexer);
14362 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14363 can be expanded but members cannot. */
14364 if (mem_initializer != error_mark_node
14365 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14367 error_at (token->location,
14368 "cannot expand initializer for member %qD",
14369 TREE_PURPOSE (mem_initializer));
14370 mem_initializer = error_mark_node;
14373 /* Construct the pack expansion type. */
14374 if (mem_initializer != error_mark_node)
14375 mem_initializer = make_pack_expansion (mem_initializer);
14377 if (target_ctor != error_mark_node
14378 && mem_initializer != error_mark_node)
14380 error ("mem-initializer for %qD follows constructor delegation",
14381 TREE_PURPOSE (mem_initializer));
14382 mem_initializer = error_mark_node;
14384 /* Look for a target constructor. */
14385 if (mem_initializer != error_mark_node
14386 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14387 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14389 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14390 if (mem_initializer_list)
14392 error ("constructor delegation follows mem-initializer for %qD",
14393 TREE_PURPOSE (mem_initializer_list));
14394 mem_initializer = error_mark_node;
14396 target_ctor = mem_initializer;
14398 /* Add it to the list, unless it was erroneous. */
14399 if (mem_initializer != error_mark_node)
14401 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14402 mem_initializer_list = mem_initializer;
14404 /* If the next token is not a `,', we're done. */
14405 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14406 break;
14407 /* Consume the `,' token. */
14408 cp_lexer_consume_token (parser->lexer);
14411 /* Perform semantic analysis. */
14412 if (DECL_CONSTRUCTOR_P (current_function_decl))
14413 finish_mem_initializers (mem_initializer_list);
14416 /* Parse a mem-initializer.
14418 mem-initializer:
14419 mem-initializer-id ( expression-list [opt] )
14420 mem-initializer-id braced-init-list
14422 GNU extension:
14424 mem-initializer:
14425 ( expression-list [opt] )
14427 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14428 class) or FIELD_DECL (for a non-static data member) to initialize;
14429 the TREE_VALUE is the expression-list. An empty initialization
14430 list is represented by void_list_node. */
14432 static tree
14433 cp_parser_mem_initializer (cp_parser* parser)
14435 tree mem_initializer_id;
14436 tree expression_list;
14437 tree member;
14438 cp_token *token = cp_lexer_peek_token (parser->lexer);
14440 /* Find out what is being initialized. */
14441 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14443 permerror (token->location,
14444 "anachronistic old-style base class initializer");
14445 mem_initializer_id = NULL_TREE;
14447 else
14449 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14450 if (mem_initializer_id == error_mark_node)
14451 return mem_initializer_id;
14453 member = expand_member_init (mem_initializer_id);
14454 if (member && !DECL_P (member))
14455 in_base_initializer = 1;
14457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14459 bool expr_non_constant_p;
14460 cp_lexer_set_source_position (parser->lexer);
14461 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14462 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14463 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14464 expression_list = build_tree_list (NULL_TREE, expression_list);
14466 else
14468 vec<tree, va_gc> *vec;
14469 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14470 /*cast_p=*/false,
14471 /*allow_expansion_p=*/true,
14472 /*non_constant_p=*/NULL);
14473 if (vec == NULL)
14474 return error_mark_node;
14475 expression_list = build_tree_list_vec (vec);
14476 release_tree_vector (vec);
14479 if (expression_list == error_mark_node)
14480 return error_mark_node;
14481 if (!expression_list)
14482 expression_list = void_type_node;
14484 in_base_initializer = 0;
14486 return member ? build_tree_list (member, expression_list) : error_mark_node;
14489 /* Parse a mem-initializer-id.
14491 mem-initializer-id:
14492 :: [opt] nested-name-specifier [opt] class-name
14493 decltype-specifier (C++11)
14494 identifier
14496 Returns a TYPE indicating the class to be initialized for the first
14497 production (and the second in C++11). Returns an IDENTIFIER_NODE
14498 indicating the data member to be initialized for the last production. */
14500 static tree
14501 cp_parser_mem_initializer_id (cp_parser* parser)
14503 bool global_scope_p;
14504 bool nested_name_specifier_p;
14505 bool template_p = false;
14506 tree id;
14508 cp_token *token = cp_lexer_peek_token (parser->lexer);
14510 /* `typename' is not allowed in this context ([temp.res]). */
14511 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14513 error_at (token->location,
14514 "keyword %<typename%> not allowed in this context (a qualified "
14515 "member initializer is implicitly a type)");
14516 cp_lexer_consume_token (parser->lexer);
14518 /* Look for the optional `::' operator. */
14519 global_scope_p
14520 = (cp_parser_global_scope_opt (parser,
14521 /*current_scope_valid_p=*/false)
14522 != NULL_TREE);
14523 /* Look for the optional nested-name-specifier. The simplest way to
14524 implement:
14526 [temp.res]
14528 The keyword `typename' is not permitted in a base-specifier or
14529 mem-initializer; in these contexts a qualified name that
14530 depends on a template-parameter is implicitly assumed to be a
14531 type name.
14533 is to assume that we have seen the `typename' keyword at this
14534 point. */
14535 nested_name_specifier_p
14536 = (cp_parser_nested_name_specifier_opt (parser,
14537 /*typename_keyword_p=*/true,
14538 /*check_dependency_p=*/true,
14539 /*type_p=*/true,
14540 /*is_declaration=*/true)
14541 != NULL_TREE);
14542 if (nested_name_specifier_p)
14543 template_p = cp_parser_optional_template_keyword (parser);
14544 /* If there is a `::' operator or a nested-name-specifier, then we
14545 are definitely looking for a class-name. */
14546 if (global_scope_p || nested_name_specifier_p)
14547 return cp_parser_class_name (parser,
14548 /*typename_keyword_p=*/true,
14549 /*template_keyword_p=*/template_p,
14550 typename_type,
14551 /*check_dependency_p=*/true,
14552 /*class_head_p=*/false,
14553 /*is_declaration=*/true);
14554 /* Otherwise, we could also be looking for an ordinary identifier. */
14555 cp_parser_parse_tentatively (parser);
14556 if (cp_lexer_next_token_is_decltype (parser->lexer))
14557 /* Try a decltype-specifier. */
14558 id = cp_parser_decltype (parser);
14559 else
14560 /* Otherwise, try a class-name. */
14561 id = cp_parser_class_name (parser,
14562 /*typename_keyword_p=*/true,
14563 /*template_keyword_p=*/false,
14564 none_type,
14565 /*check_dependency_p=*/true,
14566 /*class_head_p=*/false,
14567 /*is_declaration=*/true);
14568 /* If we found one, we're done. */
14569 if (cp_parser_parse_definitely (parser))
14570 return id;
14571 /* Otherwise, look for an ordinary identifier. */
14572 return cp_parser_identifier (parser);
14575 /* Overloading [gram.over] */
14577 /* Parse an operator-function-id.
14579 operator-function-id:
14580 operator operator
14582 Returns an IDENTIFIER_NODE for the operator which is a
14583 human-readable spelling of the identifier, e.g., `operator +'. */
14585 static cp_expr
14586 cp_parser_operator_function_id (cp_parser* parser)
14588 /* Look for the `operator' keyword. */
14589 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14590 return error_mark_node;
14591 /* And then the name of the operator itself. */
14592 return cp_parser_operator (parser);
14595 /* Return an identifier node for a user-defined literal operator.
14596 The suffix identifier is chained to the operator name identifier. */
14598 tree
14599 cp_literal_operator_id (const char* name)
14601 tree identifier;
14602 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14603 + strlen (name) + 10);
14604 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14605 identifier = get_identifier (buffer);
14607 return identifier;
14610 /* Parse an operator.
14612 operator:
14613 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14614 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14615 || ++ -- , ->* -> () []
14617 GNU Extensions:
14619 operator:
14620 <? >? <?= >?=
14622 Returns an IDENTIFIER_NODE for the operator which is a
14623 human-readable spelling of the identifier, e.g., `operator +'. */
14625 static cp_expr
14626 cp_parser_operator (cp_parser* parser)
14628 tree id = NULL_TREE;
14629 cp_token *token;
14630 bool utf8 = false;
14632 /* Peek at the next token. */
14633 token = cp_lexer_peek_token (parser->lexer);
14635 location_t start_loc = token->location;
14637 /* Figure out which operator we have. */
14638 switch (token->type)
14640 case CPP_KEYWORD:
14642 enum tree_code op;
14644 /* The keyword should be either `new' or `delete'. */
14645 if (token->keyword == RID_NEW)
14646 op = NEW_EXPR;
14647 else if (token->keyword == RID_DELETE)
14648 op = DELETE_EXPR;
14649 else
14650 break;
14652 /* Consume the `new' or `delete' token. */
14653 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14655 /* Peek at the next token. */
14656 token = cp_lexer_peek_token (parser->lexer);
14657 /* If it's a `[' token then this is the array variant of the
14658 operator. */
14659 if (token->type == CPP_OPEN_SQUARE)
14661 /* Consume the `[' token. */
14662 cp_lexer_consume_token (parser->lexer);
14663 /* Look for the `]' token. */
14664 if (cp_token *close_token
14665 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14666 end_loc = close_token->location;
14667 id = cp_operator_id (op == NEW_EXPR
14668 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14670 /* Otherwise, we have the non-array variant. */
14671 else
14672 id = cp_operator_id (op);
14674 location_t loc = make_location (start_loc, start_loc, end_loc);
14676 return cp_expr (id, loc);
14679 case CPP_PLUS:
14680 id = cp_operator_id (PLUS_EXPR);
14681 break;
14683 case CPP_MINUS:
14684 id = cp_operator_id (MINUS_EXPR);
14685 break;
14687 case CPP_MULT:
14688 id = cp_operator_id (MULT_EXPR);
14689 break;
14691 case CPP_DIV:
14692 id = cp_operator_id (TRUNC_DIV_EXPR);
14693 break;
14695 case CPP_MOD:
14696 id = cp_operator_id (TRUNC_MOD_EXPR);
14697 break;
14699 case CPP_XOR:
14700 id = cp_operator_id (BIT_XOR_EXPR);
14701 break;
14703 case CPP_AND:
14704 id = cp_operator_id (BIT_AND_EXPR);
14705 break;
14707 case CPP_OR:
14708 id = cp_operator_id (BIT_IOR_EXPR);
14709 break;
14711 case CPP_COMPL:
14712 id = cp_operator_id (BIT_NOT_EXPR);
14713 break;
14715 case CPP_NOT:
14716 id = cp_operator_id (TRUTH_NOT_EXPR);
14717 break;
14719 case CPP_EQ:
14720 id = cp_assignment_operator_id (NOP_EXPR);
14721 break;
14723 case CPP_LESS:
14724 id = cp_operator_id (LT_EXPR);
14725 break;
14727 case CPP_GREATER:
14728 id = cp_operator_id (GT_EXPR);
14729 break;
14731 case CPP_PLUS_EQ:
14732 id = cp_assignment_operator_id (PLUS_EXPR);
14733 break;
14735 case CPP_MINUS_EQ:
14736 id = cp_assignment_operator_id (MINUS_EXPR);
14737 break;
14739 case CPP_MULT_EQ:
14740 id = cp_assignment_operator_id (MULT_EXPR);
14741 break;
14743 case CPP_DIV_EQ:
14744 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14745 break;
14747 case CPP_MOD_EQ:
14748 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14749 break;
14751 case CPP_XOR_EQ:
14752 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14753 break;
14755 case CPP_AND_EQ:
14756 id = cp_assignment_operator_id (BIT_AND_EXPR);
14757 break;
14759 case CPP_OR_EQ:
14760 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14761 break;
14763 case CPP_LSHIFT:
14764 id = cp_operator_id (LSHIFT_EXPR);
14765 break;
14767 case CPP_RSHIFT:
14768 id = cp_operator_id (RSHIFT_EXPR);
14769 break;
14771 case CPP_LSHIFT_EQ:
14772 id = cp_assignment_operator_id (LSHIFT_EXPR);
14773 break;
14775 case CPP_RSHIFT_EQ:
14776 id = cp_assignment_operator_id (RSHIFT_EXPR);
14777 break;
14779 case CPP_EQ_EQ:
14780 id = cp_operator_id (EQ_EXPR);
14781 break;
14783 case CPP_NOT_EQ:
14784 id = cp_operator_id (NE_EXPR);
14785 break;
14787 case CPP_LESS_EQ:
14788 id = cp_operator_id (LE_EXPR);
14789 break;
14791 case CPP_GREATER_EQ:
14792 id = cp_operator_id (GE_EXPR);
14793 break;
14795 case CPP_AND_AND:
14796 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14797 break;
14799 case CPP_OR_OR:
14800 id = cp_operator_id (TRUTH_ORIF_EXPR);
14801 break;
14803 case CPP_PLUS_PLUS:
14804 id = cp_operator_id (POSTINCREMENT_EXPR);
14805 break;
14807 case CPP_MINUS_MINUS:
14808 id = cp_operator_id (PREDECREMENT_EXPR);
14809 break;
14811 case CPP_COMMA:
14812 id = cp_operator_id (COMPOUND_EXPR);
14813 break;
14815 case CPP_DEREF_STAR:
14816 id = cp_operator_id (MEMBER_REF);
14817 break;
14819 case CPP_DEREF:
14820 id = cp_operator_id (COMPONENT_REF);
14821 break;
14823 case CPP_OPEN_PAREN:
14825 /* Consume the `('. */
14826 matching_parens parens;
14827 parens.consume_open (parser);
14828 /* Look for the matching `)'. */
14829 parens.require_close (parser);
14830 return cp_operator_id (CALL_EXPR);
14833 case CPP_OPEN_SQUARE:
14834 /* Consume the `['. */
14835 cp_lexer_consume_token (parser->lexer);
14836 /* Look for the matching `]'. */
14837 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14838 return cp_operator_id (ARRAY_REF);
14840 case CPP_UTF8STRING:
14841 case CPP_UTF8STRING_USERDEF:
14842 utf8 = true;
14843 /* FALLTHRU */
14844 case CPP_STRING:
14845 case CPP_WSTRING:
14846 case CPP_STRING16:
14847 case CPP_STRING32:
14848 case CPP_STRING_USERDEF:
14849 case CPP_WSTRING_USERDEF:
14850 case CPP_STRING16_USERDEF:
14851 case CPP_STRING32_USERDEF:
14853 tree str, string_tree;
14854 int sz, len;
14856 if (cxx_dialect == cxx98)
14857 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14859 /* Consume the string. */
14860 str = cp_parser_string_literal (parser, /*translate=*/true,
14861 /*wide_ok=*/true, /*lookup_udlit=*/false);
14862 if (str == error_mark_node)
14863 return error_mark_node;
14864 else if (TREE_CODE (str) == USERDEF_LITERAL)
14866 string_tree = USERDEF_LITERAL_VALUE (str);
14867 id = USERDEF_LITERAL_SUFFIX_ID (str);
14869 else
14871 string_tree = str;
14872 /* Look for the suffix identifier. */
14873 token = cp_lexer_peek_token (parser->lexer);
14874 if (token->type == CPP_NAME)
14875 id = cp_parser_identifier (parser);
14876 else if (token->type == CPP_KEYWORD)
14878 error ("unexpected keyword;"
14879 " remove space between quotes and suffix identifier");
14880 return error_mark_node;
14882 else
14884 error ("expected suffix identifier");
14885 return error_mark_node;
14888 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14889 (TREE_TYPE (TREE_TYPE (string_tree))));
14890 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14891 if (len != 0)
14893 error ("expected empty string after %<operator%> keyword");
14894 return error_mark_node;
14896 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14897 != char_type_node)
14899 error ("invalid encoding prefix in literal operator");
14900 return error_mark_node;
14902 if (id != error_mark_node)
14904 const char *name = IDENTIFIER_POINTER (id);
14905 id = cp_literal_operator_id (name);
14907 return id;
14910 default:
14911 /* Anything else is an error. */
14912 break;
14915 /* If we have selected an identifier, we need to consume the
14916 operator token. */
14917 if (id)
14918 cp_lexer_consume_token (parser->lexer);
14919 /* Otherwise, no valid operator name was present. */
14920 else
14922 cp_parser_error (parser, "expected operator");
14923 id = error_mark_node;
14926 return cp_expr (id, start_loc);
14929 /* Parse a template-declaration.
14931 template-declaration:
14932 export [opt] template < template-parameter-list > declaration
14934 If MEMBER_P is TRUE, this template-declaration occurs within a
14935 class-specifier.
14937 The grammar rule given by the standard isn't correct. What
14938 is really meant is:
14940 template-declaration:
14941 export [opt] template-parameter-list-seq
14942 decl-specifier-seq [opt] init-declarator [opt] ;
14943 export [opt] template-parameter-list-seq
14944 function-definition
14946 template-parameter-list-seq:
14947 template-parameter-list-seq [opt]
14948 template < template-parameter-list >
14950 Concept Extensions:
14952 template-parameter-list-seq:
14953 template < template-parameter-list > requires-clause [opt]
14955 requires-clause:
14956 requires logical-or-expression */
14958 static void
14959 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14961 /* Check for `export'. */
14962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14964 /* Consume the `export' token. */
14965 cp_lexer_consume_token (parser->lexer);
14966 /* Warn that we do not support `export'. */
14967 warning (0, "keyword %<export%> not implemented, and will be ignored");
14970 cp_parser_template_declaration_after_export (parser, member_p);
14973 /* Parse a template-parameter-list.
14975 template-parameter-list:
14976 template-parameter
14977 template-parameter-list , template-parameter
14979 Returns a TREE_LIST. Each node represents a template parameter.
14980 The nodes are connected via their TREE_CHAINs. */
14982 static tree
14983 cp_parser_template_parameter_list (cp_parser* parser)
14985 tree parameter_list = NULL_TREE;
14987 begin_template_parm_list ();
14989 /* The loop below parses the template parms. We first need to know
14990 the total number of template parms to be able to compute proper
14991 canonical types of each dependent type. So after the loop, when
14992 we know the total number of template parms,
14993 end_template_parm_list computes the proper canonical types and
14994 fixes up the dependent types accordingly. */
14995 while (true)
14997 tree parameter;
14998 bool is_non_type;
14999 bool is_parameter_pack;
15000 location_t parm_loc;
15002 /* Parse the template-parameter. */
15003 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
15004 parameter = cp_parser_template_parameter (parser,
15005 &is_non_type,
15006 &is_parameter_pack);
15007 /* Add it to the list. */
15008 if (parameter != error_mark_node)
15009 parameter_list = process_template_parm (parameter_list,
15010 parm_loc,
15011 parameter,
15012 is_non_type,
15013 is_parameter_pack);
15014 else
15016 tree err_parm = build_tree_list (parameter, parameter);
15017 parameter_list = chainon (parameter_list, err_parm);
15020 /* If the next token is not a `,', we're done. */
15021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15022 break;
15023 /* Otherwise, consume the `,' token. */
15024 cp_lexer_consume_token (parser->lexer);
15027 return end_template_parm_list (parameter_list);
15030 /* Parse a introduction-list.
15032 introduction-list:
15033 introduced-parameter
15034 introduction-list , introduced-parameter
15036 introduced-parameter:
15037 ...[opt] identifier
15039 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
15040 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
15041 WILDCARD_DECL will also have DECL_NAME set and token location in
15042 DECL_SOURCE_LOCATION. */
15044 static tree
15045 cp_parser_introduction_list (cp_parser *parser)
15047 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
15049 while (true)
15051 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
15052 if (is_pack)
15053 cp_lexer_consume_token (parser->lexer);
15055 /* Build placeholder. */
15056 tree parm = build_nt (WILDCARD_DECL);
15057 DECL_SOURCE_LOCATION (parm)
15058 = cp_lexer_peek_token (parser->lexer)->location;
15059 DECL_NAME (parm) = cp_parser_identifier (parser);
15060 WILDCARD_PACK_P (parm) = is_pack;
15061 vec_safe_push (introduction_vec, parm);
15063 /* If the next token is not a `,', we're done. */
15064 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15065 break;
15066 /* Otherwise, consume the `,' token. */
15067 cp_lexer_consume_token (parser->lexer);
15070 /* Convert the vec into a TREE_VEC. */
15071 tree introduction_list = make_tree_vec (introduction_vec->length ());
15072 unsigned int n;
15073 tree parm;
15074 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
15075 TREE_VEC_ELT (introduction_list, n) = parm;
15077 release_tree_vector (introduction_vec);
15078 return introduction_list;
15081 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
15082 is an abstract declarator. */
15084 static inline cp_declarator*
15085 get_id_declarator (cp_declarator *declarator)
15087 cp_declarator *d = declarator;
15088 while (d && d->kind != cdk_id)
15089 d = d->declarator;
15090 return d;
15093 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
15094 is an abstract declarator. */
15096 static inline tree
15097 get_unqualified_id (cp_declarator *declarator)
15099 declarator = get_id_declarator (declarator);
15100 if (declarator)
15101 return declarator->u.id.unqualified_name;
15102 else
15103 return NULL_TREE;
15106 /* Returns true if DECL represents a constrained-parameter. */
15108 static inline bool
15109 is_constrained_parameter (tree decl)
15111 return (decl
15112 && TREE_CODE (decl) == TYPE_DECL
15113 && CONSTRAINED_PARM_CONCEPT (decl)
15114 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15117 /* Returns true if PARM declares a constrained-parameter. */
15119 static inline bool
15120 is_constrained_parameter (cp_parameter_declarator *parm)
15122 return is_constrained_parameter (parm->decl_specifiers.type);
15125 /* Check that the type parameter is only a declarator-id, and that its
15126 type is not cv-qualified. */
15128 bool
15129 cp_parser_check_constrained_type_parm (cp_parser *parser,
15130 cp_parameter_declarator *parm)
15132 if (!parm->declarator)
15133 return true;
15135 if (parm->declarator->kind != cdk_id)
15137 cp_parser_error (parser, "invalid constrained type parameter");
15138 return false;
15141 /* Don't allow cv-qualified type parameters. */
15142 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15143 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15145 cp_parser_error (parser, "cv-qualified type parameter");
15146 return false;
15149 return true;
15152 /* Finish parsing/processing a template type parameter and checking
15153 various restrictions. */
15155 static inline tree
15156 cp_parser_constrained_type_template_parm (cp_parser *parser,
15157 tree id,
15158 cp_parameter_declarator* parmdecl)
15160 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15161 return finish_template_type_parm (class_type_node, id);
15162 else
15163 return error_mark_node;
15166 static tree
15167 finish_constrained_template_template_parm (tree proto, tree id)
15169 /* FIXME: This should probably be copied, and we may need to adjust
15170 the template parameter depths. */
15171 tree saved_parms = current_template_parms;
15172 begin_template_parm_list ();
15173 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15174 end_template_parm_list ();
15176 tree parm = finish_template_template_parm (class_type_node, id);
15177 current_template_parms = saved_parms;
15179 return parm;
15182 /* Finish parsing/processing a template template parameter by borrowing
15183 the template parameter list from the prototype parameter. */
15185 static tree
15186 cp_parser_constrained_template_template_parm (cp_parser *parser,
15187 tree proto,
15188 tree id,
15189 cp_parameter_declarator *parmdecl)
15191 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15192 return error_mark_node;
15193 return finish_constrained_template_template_parm (proto, id);
15196 /* Create a new non-type template parameter from the given PARM
15197 declarator. */
15199 static tree
15200 constrained_non_type_template_parm (bool *is_non_type,
15201 cp_parameter_declarator *parm)
15203 *is_non_type = true;
15204 cp_declarator *decl = parm->declarator;
15205 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15206 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15207 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15210 /* Build a constrained template parameter based on the PARMDECL
15211 declarator. The type of PARMDECL is the constrained type, which
15212 refers to the prototype template parameter that ultimately
15213 specifies the type of the declared parameter. */
15215 static tree
15216 finish_constrained_parameter (cp_parser *parser,
15217 cp_parameter_declarator *parmdecl,
15218 bool *is_non_type,
15219 bool *is_parameter_pack)
15221 tree decl = parmdecl->decl_specifiers.type;
15222 tree id = get_unqualified_id (parmdecl->declarator);
15223 tree def = parmdecl->default_argument;
15224 tree proto = DECL_INITIAL (decl);
15226 /* A template parameter constrained by a variadic concept shall also
15227 be declared as a template parameter pack. */
15228 bool is_variadic = template_parameter_pack_p (proto);
15229 if (is_variadic && !*is_parameter_pack)
15230 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15232 /* Build the parameter. Return an error if the declarator was invalid. */
15233 tree parm;
15234 if (TREE_CODE (proto) == TYPE_DECL)
15235 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15236 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15237 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15238 parmdecl);
15239 else
15240 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15241 if (parm == error_mark_node)
15242 return error_mark_node;
15244 /* Finish the parameter decl and create a node attaching the
15245 default argument and constraint. */
15246 parm = build_tree_list (def, parm);
15247 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15249 return parm;
15252 /* Returns true if the parsed type actually represents the declaration
15253 of a type template-parameter. */
15255 static inline bool
15256 declares_constrained_type_template_parameter (tree type)
15258 return (is_constrained_parameter (type)
15259 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15263 /* Returns true if the parsed type actually represents the declaration of
15264 a template template-parameter. */
15266 static bool
15267 declares_constrained_template_template_parameter (tree type)
15269 return (is_constrained_parameter (type)
15270 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15273 /* Parse a default argument for a type template-parameter.
15274 Note that diagnostics are handled in cp_parser_template_parameter. */
15276 static tree
15277 cp_parser_default_type_template_argument (cp_parser *parser)
15279 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15281 /* Consume the `=' token. */
15282 cp_lexer_consume_token (parser->lexer);
15284 cp_token *token = cp_lexer_peek_token (parser->lexer);
15286 /* Parse the default-argument. */
15287 push_deferring_access_checks (dk_no_deferred);
15288 tree default_argument = cp_parser_type_id (parser);
15289 pop_deferring_access_checks ();
15291 if (flag_concepts && type_uses_auto (default_argument))
15293 error_at (token->location,
15294 "invalid use of %<auto%> in default template argument");
15295 return error_mark_node;
15298 return default_argument;
15301 /* Parse a default argument for a template template-parameter. */
15303 static tree
15304 cp_parser_default_template_template_argument (cp_parser *parser)
15306 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15308 bool is_template;
15310 /* Consume the `='. */
15311 cp_lexer_consume_token (parser->lexer);
15312 /* Parse the id-expression. */
15313 push_deferring_access_checks (dk_no_deferred);
15314 /* save token before parsing the id-expression, for error
15315 reporting */
15316 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15317 tree default_argument
15318 = cp_parser_id_expression (parser,
15319 /*template_keyword_p=*/false,
15320 /*check_dependency_p=*/true,
15321 /*template_p=*/&is_template,
15322 /*declarator_p=*/false,
15323 /*optional_p=*/false);
15324 if (TREE_CODE (default_argument) == TYPE_DECL)
15325 /* If the id-expression was a template-id that refers to
15326 a template-class, we already have the declaration here,
15327 so no further lookup is needed. */
15329 else
15330 /* Look up the name. */
15331 default_argument
15332 = cp_parser_lookup_name (parser, default_argument,
15333 none_type,
15334 /*is_template=*/is_template,
15335 /*is_namespace=*/false,
15336 /*check_dependency=*/true,
15337 /*ambiguous_decls=*/NULL,
15338 token->location);
15339 /* See if the default argument is valid. */
15340 default_argument = check_template_template_default_arg (default_argument);
15341 pop_deferring_access_checks ();
15342 return default_argument;
15345 /* Parse a template-parameter.
15347 template-parameter:
15348 type-parameter
15349 parameter-declaration
15351 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15352 the parameter. The TREE_PURPOSE is the default value, if any.
15353 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15354 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15355 set to true iff this parameter is a parameter pack. */
15357 static tree
15358 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15359 bool *is_parameter_pack)
15361 cp_token *token;
15362 cp_parameter_declarator *parameter_declarator;
15363 tree parm;
15365 /* Assume it is a type parameter or a template parameter. */
15366 *is_non_type = false;
15367 /* Assume it not a parameter pack. */
15368 *is_parameter_pack = false;
15369 /* Peek at the next token. */
15370 token = cp_lexer_peek_token (parser->lexer);
15371 /* If it is `template', we have a type-parameter. */
15372 if (token->keyword == RID_TEMPLATE)
15373 return cp_parser_type_parameter (parser, is_parameter_pack);
15374 /* If it is `class' or `typename' we do not know yet whether it is a
15375 type parameter or a non-type parameter. Consider:
15377 template <typename T, typename T::X X> ...
15381 template <class C, class D*> ...
15383 Here, the first parameter is a type parameter, and the second is
15384 a non-type parameter. We can tell by looking at the token after
15385 the identifier -- if it is a `,', `=', or `>' then we have a type
15386 parameter. */
15387 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15389 /* Peek at the token after `class' or `typename'. */
15390 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15391 /* If it's an ellipsis, we have a template type parameter
15392 pack. */
15393 if (token->type == CPP_ELLIPSIS)
15394 return cp_parser_type_parameter (parser, is_parameter_pack);
15395 /* If it's an identifier, skip it. */
15396 if (token->type == CPP_NAME)
15397 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15398 /* Now, see if the token looks like the end of a template
15399 parameter. */
15400 if (token->type == CPP_COMMA
15401 || token->type == CPP_EQ
15402 || token->type == CPP_GREATER)
15403 return cp_parser_type_parameter (parser, is_parameter_pack);
15406 /* Otherwise, it is a non-type parameter or a constrained parameter.
15408 [temp.param]
15410 When parsing a default template-argument for a non-type
15411 template-parameter, the first non-nested `>' is taken as the end
15412 of the template parameter-list rather than a greater-than
15413 operator. */
15414 parameter_declarator
15415 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15416 /*parenthesized_p=*/NULL);
15418 if (!parameter_declarator)
15419 return error_mark_node;
15421 /* If the parameter declaration is marked as a parameter pack, set
15422 *IS_PARAMETER_PACK to notify the caller. */
15423 if (parameter_declarator->template_parameter_pack_p)
15424 *is_parameter_pack = true;
15426 if (parameter_declarator->default_argument)
15428 /* Can happen in some cases of erroneous input (c++/34892). */
15429 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15430 /* Consume the `...' for better error recovery. */
15431 cp_lexer_consume_token (parser->lexer);
15434 // The parameter may have been constrained.
15435 if (is_constrained_parameter (parameter_declarator))
15436 return finish_constrained_parameter (parser,
15437 parameter_declarator,
15438 is_non_type,
15439 is_parameter_pack);
15441 // Now we're sure that the parameter is a non-type parameter.
15442 *is_non_type = true;
15444 parm = grokdeclarator (parameter_declarator->declarator,
15445 &parameter_declarator->decl_specifiers,
15446 TPARM, /*initialized=*/0,
15447 /*attrlist=*/NULL);
15448 if (parm == error_mark_node)
15449 return error_mark_node;
15451 return build_tree_list (parameter_declarator->default_argument, parm);
15454 /* Parse a type-parameter.
15456 type-parameter:
15457 class identifier [opt]
15458 class identifier [opt] = type-id
15459 typename identifier [opt]
15460 typename identifier [opt] = type-id
15461 template < template-parameter-list > class identifier [opt]
15462 template < template-parameter-list > class identifier [opt]
15463 = id-expression
15465 GNU Extension (variadic templates):
15467 type-parameter:
15468 class ... identifier [opt]
15469 typename ... identifier [opt]
15471 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15472 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15473 the declaration of the parameter.
15475 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15477 static tree
15478 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15480 cp_token *token;
15481 tree parameter;
15483 /* Look for a keyword to tell us what kind of parameter this is. */
15484 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15485 if (!token)
15486 return error_mark_node;
15488 switch (token->keyword)
15490 case RID_CLASS:
15491 case RID_TYPENAME:
15493 tree identifier;
15494 tree default_argument;
15496 /* If the next token is an ellipsis, we have a template
15497 argument pack. */
15498 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15500 /* Consume the `...' token. */
15501 cp_lexer_consume_token (parser->lexer);
15502 maybe_warn_variadic_templates ();
15504 *is_parameter_pack = true;
15507 /* If the next token is an identifier, then it names the
15508 parameter. */
15509 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15510 identifier = cp_parser_identifier (parser);
15511 else
15512 identifier = NULL_TREE;
15514 /* Create the parameter. */
15515 parameter = finish_template_type_parm (class_type_node, identifier);
15517 /* If the next token is an `=', we have a default argument. */
15518 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15520 default_argument
15521 = cp_parser_default_type_template_argument (parser);
15523 /* Template parameter packs cannot have default
15524 arguments. */
15525 if (*is_parameter_pack)
15527 if (identifier)
15528 error_at (token->location,
15529 "template parameter pack %qD cannot have a "
15530 "default argument", identifier);
15531 else
15532 error_at (token->location,
15533 "template parameter packs cannot have "
15534 "default arguments");
15535 default_argument = NULL_TREE;
15537 else if (check_for_bare_parameter_packs (default_argument))
15538 default_argument = error_mark_node;
15540 else
15541 default_argument = NULL_TREE;
15543 /* Create the combined representation of the parameter and the
15544 default argument. */
15545 parameter = build_tree_list (default_argument, parameter);
15547 break;
15549 case RID_TEMPLATE:
15551 tree identifier;
15552 tree default_argument;
15554 /* Look for the `<'. */
15555 cp_parser_require (parser, CPP_LESS, RT_LESS);
15556 /* Parse the template-parameter-list. */
15557 cp_parser_template_parameter_list (parser);
15558 /* Look for the `>'. */
15559 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15561 // If template requirements are present, parse them.
15562 if (flag_concepts)
15564 tree reqs = get_shorthand_constraints (current_template_parms);
15565 if (tree r = cp_parser_requires_clause_opt (parser))
15566 reqs = conjoin_constraints (reqs, normalize_expression (r));
15567 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15570 /* Look for the `class' or 'typename' keywords. */
15571 cp_parser_type_parameter_key (parser);
15572 /* If the next token is an ellipsis, we have a template
15573 argument pack. */
15574 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15576 /* Consume the `...' token. */
15577 cp_lexer_consume_token (parser->lexer);
15578 maybe_warn_variadic_templates ();
15580 *is_parameter_pack = true;
15582 /* If the next token is an `=', then there is a
15583 default-argument. If the next token is a `>', we are at
15584 the end of the parameter-list. If the next token is a `,',
15585 then we are at the end of this parameter. */
15586 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15587 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15588 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15590 identifier = cp_parser_identifier (parser);
15591 /* Treat invalid names as if the parameter were nameless. */
15592 if (identifier == error_mark_node)
15593 identifier = NULL_TREE;
15595 else
15596 identifier = NULL_TREE;
15598 /* Create the template parameter. */
15599 parameter = finish_template_template_parm (class_type_node,
15600 identifier);
15602 /* If the next token is an `=', then there is a
15603 default-argument. */
15604 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15606 default_argument
15607 = cp_parser_default_template_template_argument (parser);
15609 /* Template parameter packs cannot have default
15610 arguments. */
15611 if (*is_parameter_pack)
15613 if (identifier)
15614 error_at (token->location,
15615 "template parameter pack %qD cannot "
15616 "have a default argument",
15617 identifier);
15618 else
15619 error_at (token->location, "template parameter packs cannot "
15620 "have default arguments");
15621 default_argument = NULL_TREE;
15624 else
15625 default_argument = NULL_TREE;
15627 /* Create the combined representation of the parameter and the
15628 default argument. */
15629 parameter = build_tree_list (default_argument, parameter);
15631 break;
15633 default:
15634 gcc_unreachable ();
15635 break;
15638 return parameter;
15641 /* Parse a template-id.
15643 template-id:
15644 template-name < template-argument-list [opt] >
15646 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15647 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15648 returned. Otherwise, if the template-name names a function, or set
15649 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15650 names a class, returns a TYPE_DECL for the specialization.
15652 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15653 uninstantiated templates. */
15655 static tree
15656 cp_parser_template_id (cp_parser *parser,
15657 bool template_keyword_p,
15658 bool check_dependency_p,
15659 enum tag_types tag_type,
15660 bool is_declaration)
15662 tree templ;
15663 tree arguments;
15664 tree template_id;
15665 cp_token_position start_of_id = 0;
15666 cp_token *next_token = NULL, *next_token_2 = NULL;
15667 bool is_identifier;
15669 /* If the next token corresponds to a template-id, there is no need
15670 to reparse it. */
15671 cp_token *token = cp_lexer_peek_token (parser->lexer);
15672 if (token->type == CPP_TEMPLATE_ID)
15674 cp_lexer_consume_token (parser->lexer);
15675 return saved_checks_value (token->u.tree_check_value);
15678 /* Avoid performing name lookup if there is no possibility of
15679 finding a template-id. */
15680 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15681 || (token->type == CPP_NAME
15682 && !cp_parser_nth_token_starts_template_argument_list_p
15683 (parser, 2)))
15685 cp_parser_error (parser, "expected template-id");
15686 return error_mark_node;
15689 /* Remember where the template-id starts. */
15690 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15691 start_of_id = cp_lexer_token_position (parser->lexer, false);
15693 push_deferring_access_checks (dk_deferred);
15695 /* Parse the template-name. */
15696 is_identifier = false;
15697 templ = cp_parser_template_name (parser, template_keyword_p,
15698 check_dependency_p,
15699 is_declaration,
15700 tag_type,
15701 &is_identifier);
15702 if (templ == error_mark_node || is_identifier)
15704 pop_deferring_access_checks ();
15705 return templ;
15708 /* Since we're going to preserve any side-effects from this parse, set up a
15709 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15710 in the template arguments. */
15711 tentative_firewall firewall (parser);
15713 /* If we find the sequence `[:' after a template-name, it's probably
15714 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15715 parse correctly the argument list. */
15716 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15717 == CPP_OPEN_SQUARE)
15718 && next_token->flags & DIGRAPH
15719 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15720 == CPP_COLON)
15721 && !(next_token_2->flags & PREV_WHITE))
15723 cp_parser_parse_tentatively (parser);
15724 /* Change `:' into `::'. */
15725 next_token_2->type = CPP_SCOPE;
15726 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15727 CPP_LESS. */
15728 cp_lexer_consume_token (parser->lexer);
15730 /* Parse the arguments. */
15731 arguments = cp_parser_enclosed_template_argument_list (parser);
15732 if (!cp_parser_parse_definitely (parser))
15734 /* If we couldn't parse an argument list, then we revert our changes
15735 and return simply an error. Maybe this is not a template-id
15736 after all. */
15737 next_token_2->type = CPP_COLON;
15738 cp_parser_error (parser, "expected %<<%>");
15739 pop_deferring_access_checks ();
15740 return error_mark_node;
15742 /* Otherwise, emit an error about the invalid digraph, but continue
15743 parsing because we got our argument list. */
15744 if (permerror (next_token->location,
15745 "%<<::%> cannot begin a template-argument list"))
15747 static bool hint = false;
15748 inform (next_token->location,
15749 "%<<:%> is an alternate spelling for %<[%>."
15750 " Insert whitespace between %<<%> and %<::%>");
15751 if (!hint && !flag_permissive)
15753 inform (next_token->location, "(if you use %<-fpermissive%> "
15754 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15755 "accept your code)");
15756 hint = true;
15760 else
15762 /* Look for the `<' that starts the template-argument-list. */
15763 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15765 pop_deferring_access_checks ();
15766 return error_mark_node;
15768 /* Parse the arguments. */
15769 arguments = cp_parser_enclosed_template_argument_list (parser);
15772 /* Set the location to be of the form:
15773 template-name < template-argument-list [opt] >
15774 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15775 with caret == start at the start of the template-name,
15776 ranging until the closing '>'. */
15777 location_t finish_loc
15778 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15779 location_t combined_loc
15780 = make_location (token->location, token->location, finish_loc);
15782 /* Build a representation of the specialization. */
15783 if (identifier_p (templ))
15784 template_id = build_min_nt_loc (combined_loc,
15785 TEMPLATE_ID_EXPR,
15786 templ, arguments);
15787 else if (DECL_TYPE_TEMPLATE_P (templ)
15788 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15790 bool entering_scope;
15791 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15792 template (rather than some instantiation thereof) only if
15793 is not nested within some other construct. For example, in
15794 "template <typename T> void f(T) { A<T>::", A<T> is just an
15795 instantiation of A. */
15796 entering_scope = (template_parm_scope_p ()
15797 && cp_lexer_next_token_is (parser->lexer,
15798 CPP_SCOPE));
15799 template_id
15800 = finish_template_type (templ, arguments, entering_scope);
15802 /* A template-like identifier may be a partial concept id. */
15803 else if (flag_concepts
15804 && (template_id = (cp_parser_maybe_partial_concept_id
15805 (parser, templ, arguments))))
15806 return template_id;
15807 else if (variable_template_p (templ))
15809 template_id = lookup_template_variable (templ, arguments);
15810 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15811 SET_EXPR_LOCATION (template_id, combined_loc);
15813 else
15815 /* If it's not a class-template or a template-template, it should be
15816 a function-template. */
15817 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15818 || TREE_CODE (templ) == OVERLOAD
15819 || BASELINK_P (templ)));
15821 template_id = lookup_template_function (templ, arguments);
15822 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15823 SET_EXPR_LOCATION (template_id, combined_loc);
15826 /* If parsing tentatively, replace the sequence of tokens that makes
15827 up the template-id with a CPP_TEMPLATE_ID token. That way,
15828 should we re-parse the token stream, we will not have to repeat
15829 the effort required to do the parse, nor will we issue duplicate
15830 error messages about problems during instantiation of the
15831 template. */
15832 if (start_of_id
15833 /* Don't do this if we had a parse error in a declarator; re-parsing
15834 might succeed if a name changes meaning (60361). */
15835 && !(cp_parser_error_occurred (parser)
15836 && cp_parser_parsing_tentatively (parser)
15837 && parser->in_declarator_p))
15839 /* Reset the contents of the START_OF_ID token. */
15840 token->type = CPP_TEMPLATE_ID;
15841 token->location = combined_loc;
15843 /* We must mark the lookup as kept, so we don't throw it away on
15844 the first parse. */
15845 if (is_overloaded_fn (template_id))
15846 lookup_keep (get_fns (template_id), true);
15848 /* Retrieve any deferred checks. Do not pop this access checks yet
15849 so the memory will not be reclaimed during token replacing below. */
15850 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15851 token->u.tree_check_value->value = template_id;
15852 token->u.tree_check_value->checks = get_deferred_access_checks ();
15853 token->keyword = RID_MAX;
15855 /* Purge all subsequent tokens. */
15856 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15858 /* ??? Can we actually assume that, if template_id ==
15859 error_mark_node, we will have issued a diagnostic to the
15860 user, as opposed to simply marking the tentative parse as
15861 failed? */
15862 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15863 error_at (token->location, "parse error in template argument list");
15866 pop_to_parent_deferring_access_checks ();
15867 return template_id;
15870 /* Parse a template-name.
15872 template-name:
15873 identifier
15875 The standard should actually say:
15877 template-name:
15878 identifier
15879 operator-function-id
15881 A defect report has been filed about this issue.
15883 A conversion-function-id cannot be a template name because they cannot
15884 be part of a template-id. In fact, looking at this code:
15886 a.operator K<int>()
15888 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15889 It is impossible to call a templated conversion-function-id with an
15890 explicit argument list, since the only allowed template parameter is
15891 the type to which it is converting.
15893 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15894 `template' keyword, in a construction like:
15896 T::template f<3>()
15898 In that case `f' is taken to be a template-name, even though there
15899 is no way of knowing for sure.
15901 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15902 name refers to a set of overloaded functions, at least one of which
15903 is a template, or an IDENTIFIER_NODE with the name of the template,
15904 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15905 names are looked up inside uninstantiated templates. */
15907 static tree
15908 cp_parser_template_name (cp_parser* parser,
15909 bool template_keyword_p,
15910 bool check_dependency_p,
15911 bool is_declaration,
15912 enum tag_types tag_type,
15913 bool *is_identifier)
15915 tree identifier;
15916 tree decl;
15917 cp_token *token = cp_lexer_peek_token (parser->lexer);
15919 /* If the next token is `operator', then we have either an
15920 operator-function-id or a conversion-function-id. */
15921 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15923 /* We don't know whether we're looking at an
15924 operator-function-id or a conversion-function-id. */
15925 cp_parser_parse_tentatively (parser);
15926 /* Try an operator-function-id. */
15927 identifier = cp_parser_operator_function_id (parser);
15928 /* If that didn't work, try a conversion-function-id. */
15929 if (!cp_parser_parse_definitely (parser))
15931 cp_parser_error (parser, "expected template-name");
15932 return error_mark_node;
15935 /* Look for the identifier. */
15936 else
15937 identifier = cp_parser_identifier (parser);
15939 /* If we didn't find an identifier, we don't have a template-id. */
15940 if (identifier == error_mark_node)
15941 return error_mark_node;
15943 /* If the name immediately followed the `template' keyword, then it
15944 is a template-name. However, if the next token is not `<', then
15945 we do not treat it as a template-name, since it is not being used
15946 as part of a template-id. This enables us to handle constructs
15947 like:
15949 template <typename T> struct S { S(); };
15950 template <typename T> S<T>::S();
15952 correctly. We would treat `S' as a template -- if it were `S<T>'
15953 -- but we do not if there is no `<'. */
15955 if (processing_template_decl
15956 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15958 /* In a declaration, in a dependent context, we pretend that the
15959 "template" keyword was present in order to improve error
15960 recovery. For example, given:
15962 template <typename T> void f(T::X<int>);
15964 we want to treat "X<int>" as a template-id. */
15965 if (is_declaration
15966 && !template_keyword_p
15967 && parser->scope && TYPE_P (parser->scope)
15968 && check_dependency_p
15969 && dependent_scope_p (parser->scope)
15970 /* Do not do this for dtors (or ctors), since they never
15971 need the template keyword before their name. */
15972 && !constructor_name_p (identifier, parser->scope))
15974 cp_token_position start = 0;
15976 /* Explain what went wrong. */
15977 error_at (token->location, "non-template %qD used as template",
15978 identifier);
15979 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15980 parser->scope, identifier);
15981 /* If parsing tentatively, find the location of the "<" token. */
15982 if (cp_parser_simulate_error (parser))
15983 start = cp_lexer_token_position (parser->lexer, true);
15984 /* Parse the template arguments so that we can issue error
15985 messages about them. */
15986 cp_lexer_consume_token (parser->lexer);
15987 cp_parser_enclosed_template_argument_list (parser);
15988 /* Skip tokens until we find a good place from which to
15989 continue parsing. */
15990 cp_parser_skip_to_closing_parenthesis (parser,
15991 /*recovering=*/true,
15992 /*or_comma=*/true,
15993 /*consume_paren=*/false);
15994 /* If parsing tentatively, permanently remove the
15995 template argument list. That will prevent duplicate
15996 error messages from being issued about the missing
15997 "template" keyword. */
15998 if (start)
15999 cp_lexer_purge_tokens_after (parser->lexer, start);
16000 if (is_identifier)
16001 *is_identifier = true;
16002 parser->context->object_type = NULL_TREE;
16003 return identifier;
16006 /* If the "template" keyword is present, then there is generally
16007 no point in doing name-lookup, so we just return IDENTIFIER.
16008 But, if the qualifying scope is non-dependent then we can
16009 (and must) do name-lookup normally. */
16010 if (template_keyword_p)
16012 tree scope = (parser->scope ? parser->scope
16013 : parser->context->object_type);
16014 if (scope && TYPE_P (scope)
16015 && (!CLASS_TYPE_P (scope)
16016 || (check_dependency_p && dependent_type_p (scope))))
16018 /* We're optimizing away the call to cp_parser_lookup_name, but
16019 we still need to do this. */
16020 parser->context->object_type = NULL_TREE;
16021 return identifier;
16026 /* Look up the name. */
16027 decl = cp_parser_lookup_name (parser, identifier,
16028 tag_type,
16029 /*is_template=*/true,
16030 /*is_namespace=*/false,
16031 check_dependency_p,
16032 /*ambiguous_decls=*/NULL,
16033 token->location);
16035 decl = strip_using_decl (decl);
16037 /* If DECL is a template, then the name was a template-name. */
16038 if (TREE_CODE (decl) == TEMPLATE_DECL)
16040 if (TREE_DEPRECATED (decl)
16041 && deprecated_state != DEPRECATED_SUPPRESS)
16042 warn_deprecated_use (decl, NULL_TREE);
16044 else
16046 /* The standard does not explicitly indicate whether a name that
16047 names a set of overloaded declarations, some of which are
16048 templates, is a template-name. However, such a name should
16049 be a template-name; otherwise, there is no way to form a
16050 template-id for the overloaded templates. */
16051 bool found = false;
16053 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
16054 !found && iter; ++iter)
16055 if (TREE_CODE (*iter) == TEMPLATE_DECL)
16056 found = true;
16058 if (!found)
16060 /* The name does not name a template. */
16061 cp_parser_error (parser, "expected template-name");
16062 return error_mark_node;
16066 /* If DECL is dependent, and refers to a function, then just return
16067 its name; we will look it up again during template instantiation. */
16068 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
16070 tree scope = ovl_scope (decl);
16071 if (TYPE_P (scope) && dependent_type_p (scope))
16072 return identifier;
16075 return decl;
16078 /* Parse a template-argument-list.
16080 template-argument-list:
16081 template-argument ... [opt]
16082 template-argument-list , template-argument ... [opt]
16084 Returns a TREE_VEC containing the arguments. */
16086 static tree
16087 cp_parser_template_argument_list (cp_parser* parser)
16089 tree fixed_args[10];
16090 unsigned n_args = 0;
16091 unsigned alloced = 10;
16092 tree *arg_ary = fixed_args;
16093 tree vec;
16094 bool saved_in_template_argument_list_p;
16095 bool saved_ice_p;
16096 bool saved_non_ice_p;
16098 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
16099 parser->in_template_argument_list_p = true;
16100 /* Even if the template-id appears in an integral
16101 constant-expression, the contents of the argument list do
16102 not. */
16103 saved_ice_p = parser->integral_constant_expression_p;
16104 parser->integral_constant_expression_p = false;
16105 saved_non_ice_p = parser->non_integral_constant_expression_p;
16106 parser->non_integral_constant_expression_p = false;
16108 /* Parse the arguments. */
16111 tree argument;
16113 if (n_args)
16114 /* Consume the comma. */
16115 cp_lexer_consume_token (parser->lexer);
16117 /* Parse the template-argument. */
16118 argument = cp_parser_template_argument (parser);
16120 /* If the next token is an ellipsis, we're expanding a template
16121 argument pack. */
16122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16124 if (argument == error_mark_node)
16126 cp_token *token = cp_lexer_peek_token (parser->lexer);
16127 error_at (token->location,
16128 "expected parameter pack before %<...%>");
16130 /* Consume the `...' token. */
16131 cp_lexer_consume_token (parser->lexer);
16133 /* Make the argument into a TYPE_PACK_EXPANSION or
16134 EXPR_PACK_EXPANSION. */
16135 argument = make_pack_expansion (argument);
16138 if (n_args == alloced)
16140 alloced *= 2;
16142 if (arg_ary == fixed_args)
16144 arg_ary = XNEWVEC (tree, alloced);
16145 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16147 else
16148 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16150 arg_ary[n_args++] = argument;
16152 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16154 vec = make_tree_vec (n_args);
16156 while (n_args--)
16157 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16159 if (arg_ary != fixed_args)
16160 free (arg_ary);
16161 parser->non_integral_constant_expression_p = saved_non_ice_p;
16162 parser->integral_constant_expression_p = saved_ice_p;
16163 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16164 if (CHECKING_P)
16165 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16166 return vec;
16169 /* Parse a template-argument.
16171 template-argument:
16172 assignment-expression
16173 type-id
16174 id-expression
16176 The representation is that of an assignment-expression, type-id, or
16177 id-expression -- except that the qualified id-expression is
16178 evaluated, so that the value returned is either a DECL or an
16179 OVERLOAD.
16181 Although the standard says "assignment-expression", it forbids
16182 throw-expressions or assignments in the template argument.
16183 Therefore, we use "conditional-expression" instead. */
16185 static tree
16186 cp_parser_template_argument (cp_parser* parser)
16188 tree argument;
16189 bool template_p;
16190 bool address_p;
16191 bool maybe_type_id = false;
16192 cp_token *token = NULL, *argument_start_token = NULL;
16193 location_t loc = 0;
16194 cp_id_kind idk;
16196 /* There's really no way to know what we're looking at, so we just
16197 try each alternative in order.
16199 [temp.arg]
16201 In a template-argument, an ambiguity between a type-id and an
16202 expression is resolved to a type-id, regardless of the form of
16203 the corresponding template-parameter.
16205 Therefore, we try a type-id first. */
16206 cp_parser_parse_tentatively (parser);
16207 argument = cp_parser_template_type_arg (parser);
16208 /* If there was no error parsing the type-id but the next token is a
16209 '>>', our behavior depends on which dialect of C++ we're
16210 parsing. In C++98, we probably found a typo for '> >'. But there
16211 are type-id which are also valid expressions. For instance:
16213 struct X { int operator >> (int); };
16214 template <int V> struct Foo {};
16215 Foo<X () >> 5> r;
16217 Here 'X()' is a valid type-id of a function type, but the user just
16218 wanted to write the expression "X() >> 5". Thus, we remember that we
16219 found a valid type-id, but we still try to parse the argument as an
16220 expression to see what happens.
16222 In C++0x, the '>>' will be considered two separate '>'
16223 tokens. */
16224 if (!cp_parser_error_occurred (parser)
16225 && cxx_dialect == cxx98
16226 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16228 maybe_type_id = true;
16229 cp_parser_abort_tentative_parse (parser);
16231 else
16233 /* If the next token isn't a `,' or a `>', then this argument wasn't
16234 really finished. This means that the argument is not a valid
16235 type-id. */
16236 if (!cp_parser_next_token_ends_template_argument_p (parser))
16237 cp_parser_error (parser, "expected template-argument");
16238 /* If that worked, we're done. */
16239 if (cp_parser_parse_definitely (parser))
16240 return argument;
16242 /* We're still not sure what the argument will be. */
16243 cp_parser_parse_tentatively (parser);
16244 /* Try a template. */
16245 argument_start_token = cp_lexer_peek_token (parser->lexer);
16246 argument = cp_parser_id_expression (parser,
16247 /*template_keyword_p=*/false,
16248 /*check_dependency_p=*/true,
16249 &template_p,
16250 /*declarator_p=*/false,
16251 /*optional_p=*/false);
16252 /* If the next token isn't a `,' or a `>', then this argument wasn't
16253 really finished. */
16254 if (!cp_parser_next_token_ends_template_argument_p (parser))
16255 cp_parser_error (parser, "expected template-argument");
16256 if (!cp_parser_error_occurred (parser))
16258 /* Figure out what is being referred to. If the id-expression
16259 was for a class template specialization, then we will have a
16260 TYPE_DECL at this point. There is no need to do name lookup
16261 at this point in that case. */
16262 if (TREE_CODE (argument) != TYPE_DECL)
16263 argument = cp_parser_lookup_name (parser, argument,
16264 none_type,
16265 /*is_template=*/template_p,
16266 /*is_namespace=*/false,
16267 /*check_dependency=*/true,
16268 /*ambiguous_decls=*/NULL,
16269 argument_start_token->location);
16270 /* Handle a constrained-type-specifier for a non-type template
16271 parameter. */
16272 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16273 argument = decl;
16274 else if (TREE_CODE (argument) != TEMPLATE_DECL
16275 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16276 cp_parser_error (parser, "expected template-name");
16278 if (cp_parser_parse_definitely (parser))
16280 if (TREE_DEPRECATED (argument))
16281 warn_deprecated_use (argument, NULL_TREE);
16282 return argument;
16284 /* It must be a non-type argument. In C++17 any constant-expression is
16285 allowed. */
16286 if (cxx_dialect > cxx14)
16287 goto general_expr;
16289 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16291 -- an integral constant-expression of integral or enumeration
16292 type; or
16294 -- the name of a non-type template-parameter; or
16296 -- the name of an object or function with external linkage...
16298 -- the address of an object or function with external linkage...
16300 -- a pointer to member... */
16301 /* Look for a non-type template parameter. */
16302 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16304 cp_parser_parse_tentatively (parser);
16305 argument = cp_parser_primary_expression (parser,
16306 /*address_p=*/false,
16307 /*cast_p=*/false,
16308 /*template_arg_p=*/true,
16309 &idk);
16310 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16311 || !cp_parser_next_token_ends_template_argument_p (parser))
16312 cp_parser_simulate_error (parser);
16313 if (cp_parser_parse_definitely (parser))
16314 return argument;
16317 /* If the next token is "&", the argument must be the address of an
16318 object or function with external linkage. */
16319 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16320 if (address_p)
16322 loc = cp_lexer_peek_token (parser->lexer)->location;
16323 cp_lexer_consume_token (parser->lexer);
16325 /* See if we might have an id-expression. */
16326 token = cp_lexer_peek_token (parser->lexer);
16327 if (token->type == CPP_NAME
16328 || token->keyword == RID_OPERATOR
16329 || token->type == CPP_SCOPE
16330 || token->type == CPP_TEMPLATE_ID
16331 || token->type == CPP_NESTED_NAME_SPECIFIER)
16333 cp_parser_parse_tentatively (parser);
16334 argument = cp_parser_primary_expression (parser,
16335 address_p,
16336 /*cast_p=*/false,
16337 /*template_arg_p=*/true,
16338 &idk);
16339 if (cp_parser_error_occurred (parser)
16340 || !cp_parser_next_token_ends_template_argument_p (parser))
16341 cp_parser_abort_tentative_parse (parser);
16342 else
16344 tree probe;
16346 if (INDIRECT_REF_P (argument))
16348 /* Strip the dereference temporarily. */
16349 gcc_assert (REFERENCE_REF_P (argument));
16350 argument = TREE_OPERAND (argument, 0);
16353 /* If we're in a template, we represent a qualified-id referring
16354 to a static data member as a SCOPE_REF even if the scope isn't
16355 dependent so that we can check access control later. */
16356 probe = argument;
16357 if (TREE_CODE (probe) == SCOPE_REF)
16358 probe = TREE_OPERAND (probe, 1);
16359 if (VAR_P (probe))
16361 /* A variable without external linkage might still be a
16362 valid constant-expression, so no error is issued here
16363 if the external-linkage check fails. */
16364 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16365 cp_parser_simulate_error (parser);
16367 else if (is_overloaded_fn (argument))
16368 /* All overloaded functions are allowed; if the external
16369 linkage test does not pass, an error will be issued
16370 later. */
16372 else if (address_p
16373 && (TREE_CODE (argument) == OFFSET_REF
16374 || TREE_CODE (argument) == SCOPE_REF))
16375 /* A pointer-to-member. */
16377 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16379 else
16380 cp_parser_simulate_error (parser);
16382 if (cp_parser_parse_definitely (parser))
16384 if (address_p)
16385 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16386 tf_warning_or_error);
16387 else
16388 argument = convert_from_reference (argument);
16389 return argument;
16393 /* If the argument started with "&", there are no other valid
16394 alternatives at this point. */
16395 if (address_p)
16397 cp_parser_error (parser, "invalid non-type template argument");
16398 return error_mark_node;
16401 general_expr:
16402 /* If the argument wasn't successfully parsed as a type-id followed
16403 by '>>', the argument can only be a constant expression now.
16404 Otherwise, we try parsing the constant-expression tentatively,
16405 because the argument could really be a type-id. */
16406 if (maybe_type_id)
16407 cp_parser_parse_tentatively (parser);
16409 if (cxx_dialect <= cxx14)
16410 argument = cp_parser_constant_expression (parser);
16411 else
16413 /* With C++17 generalized non-type template arguments we need to handle
16414 lvalue constant expressions, too. */
16415 argument = cp_parser_assignment_expression (parser);
16416 require_potential_constant_expression (argument);
16419 if (!maybe_type_id)
16420 return argument;
16421 if (!cp_parser_next_token_ends_template_argument_p (parser))
16422 cp_parser_error (parser, "expected template-argument");
16423 if (cp_parser_parse_definitely (parser))
16424 return argument;
16425 /* We did our best to parse the argument as a non type-id, but that
16426 was the only alternative that matched (albeit with a '>' after
16427 it). We can assume it's just a typo from the user, and a
16428 diagnostic will then be issued. */
16429 return cp_parser_template_type_arg (parser);
16432 /* Parse an explicit-instantiation.
16434 explicit-instantiation:
16435 template declaration
16437 Although the standard says `declaration', what it really means is:
16439 explicit-instantiation:
16440 template decl-specifier-seq [opt] declarator [opt] ;
16442 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16443 supposed to be allowed. A defect report has been filed about this
16444 issue.
16446 GNU Extension:
16448 explicit-instantiation:
16449 storage-class-specifier template
16450 decl-specifier-seq [opt] declarator [opt] ;
16451 function-specifier template
16452 decl-specifier-seq [opt] declarator [opt] ; */
16454 static void
16455 cp_parser_explicit_instantiation (cp_parser* parser)
16457 int declares_class_or_enum;
16458 cp_decl_specifier_seq decl_specifiers;
16459 tree extension_specifier = NULL_TREE;
16461 timevar_push (TV_TEMPLATE_INST);
16463 /* Look for an (optional) storage-class-specifier or
16464 function-specifier. */
16465 if (cp_parser_allow_gnu_extensions_p (parser))
16467 extension_specifier
16468 = cp_parser_storage_class_specifier_opt (parser);
16469 if (!extension_specifier)
16470 extension_specifier
16471 = cp_parser_function_specifier_opt (parser,
16472 /*decl_specs=*/NULL);
16475 /* Look for the `template' keyword. */
16476 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16477 /* Let the front end know that we are processing an explicit
16478 instantiation. */
16479 begin_explicit_instantiation ();
16480 /* [temp.explicit] says that we are supposed to ignore access
16481 control while processing explicit instantiation directives. */
16482 push_deferring_access_checks (dk_no_check);
16483 /* Parse a decl-specifier-seq. */
16484 cp_parser_decl_specifier_seq (parser,
16485 CP_PARSER_FLAGS_OPTIONAL,
16486 &decl_specifiers,
16487 &declares_class_or_enum);
16488 /* If there was exactly one decl-specifier, and it declared a class,
16489 and there's no declarator, then we have an explicit type
16490 instantiation. */
16491 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16493 tree type;
16495 type = check_tag_decl (&decl_specifiers,
16496 /*explicit_type_instantiation_p=*/true);
16497 /* Turn access control back on for names used during
16498 template instantiation. */
16499 pop_deferring_access_checks ();
16500 if (type)
16501 do_type_instantiation (type, extension_specifier,
16502 /*complain=*/tf_error);
16504 else
16506 cp_declarator *declarator;
16507 tree decl;
16509 /* Parse the declarator. */
16510 declarator
16511 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16512 /*ctor_dtor_or_conv_p=*/NULL,
16513 /*parenthesized_p=*/NULL,
16514 /*member_p=*/false,
16515 /*friend_p=*/false);
16516 if (declares_class_or_enum & 2)
16517 cp_parser_check_for_definition_in_return_type (declarator,
16518 decl_specifiers.type,
16519 decl_specifiers.locations[ds_type_spec]);
16520 if (declarator != cp_error_declarator)
16522 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16523 permerror (decl_specifiers.locations[ds_inline],
16524 "explicit instantiation shall not use"
16525 " %<inline%> specifier");
16526 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16527 permerror (decl_specifiers.locations[ds_constexpr],
16528 "explicit instantiation shall not use"
16529 " %<constexpr%> specifier");
16531 decl = grokdeclarator (declarator, &decl_specifiers,
16532 NORMAL, 0, &decl_specifiers.attributes);
16533 /* Turn access control back on for names used during
16534 template instantiation. */
16535 pop_deferring_access_checks ();
16536 /* Do the explicit instantiation. */
16537 do_decl_instantiation (decl, extension_specifier);
16539 else
16541 pop_deferring_access_checks ();
16542 /* Skip the body of the explicit instantiation. */
16543 cp_parser_skip_to_end_of_statement (parser);
16546 /* We're done with the instantiation. */
16547 end_explicit_instantiation ();
16549 cp_parser_consume_semicolon_at_end_of_statement (parser);
16551 timevar_pop (TV_TEMPLATE_INST);
16554 /* Parse an explicit-specialization.
16556 explicit-specialization:
16557 template < > declaration
16559 Although the standard says `declaration', what it really means is:
16561 explicit-specialization:
16562 template <> decl-specifier [opt] init-declarator [opt] ;
16563 template <> function-definition
16564 template <> explicit-specialization
16565 template <> template-declaration */
16567 static void
16568 cp_parser_explicit_specialization (cp_parser* parser)
16570 bool need_lang_pop;
16571 cp_token *token = cp_lexer_peek_token (parser->lexer);
16573 /* Look for the `template' keyword. */
16574 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16575 /* Look for the `<'. */
16576 cp_parser_require (parser, CPP_LESS, RT_LESS);
16577 /* Look for the `>'. */
16578 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16579 /* We have processed another parameter list. */
16580 ++parser->num_template_parameter_lists;
16581 /* [temp]
16583 A template ... explicit specialization ... shall not have C
16584 linkage. */
16585 if (current_lang_name == lang_name_c)
16587 error_at (token->location, "template specialization with C linkage");
16588 /* Give it C++ linkage to avoid confusing other parts of the
16589 front end. */
16590 push_lang_context (lang_name_cplusplus);
16591 need_lang_pop = true;
16593 else
16594 need_lang_pop = false;
16595 /* Let the front end know that we are beginning a specialization. */
16596 if (!begin_specialization ())
16598 end_specialization ();
16599 return;
16602 /* If the next keyword is `template', we need to figure out whether
16603 or not we're looking a template-declaration. */
16604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16606 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16607 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16608 cp_parser_template_declaration_after_export (parser,
16609 /*member_p=*/false);
16610 else
16611 cp_parser_explicit_specialization (parser);
16613 else
16614 /* Parse the dependent declaration. */
16615 cp_parser_single_declaration (parser,
16616 /*checks=*/NULL,
16617 /*member_p=*/false,
16618 /*explicit_specialization_p=*/true,
16619 /*friend_p=*/NULL);
16620 /* We're done with the specialization. */
16621 end_specialization ();
16622 /* For the erroneous case of a template with C linkage, we pushed an
16623 implicit C++ linkage scope; exit that scope now. */
16624 if (need_lang_pop)
16625 pop_lang_context ();
16626 /* We're done with this parameter list. */
16627 --parser->num_template_parameter_lists;
16630 /* Parse a type-specifier.
16632 type-specifier:
16633 simple-type-specifier
16634 class-specifier
16635 enum-specifier
16636 elaborated-type-specifier
16637 cv-qualifier
16639 GNU Extension:
16641 type-specifier:
16642 __complex__
16644 Returns a representation of the type-specifier. For a
16645 class-specifier, enum-specifier, or elaborated-type-specifier, a
16646 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16648 The parser flags FLAGS is used to control type-specifier parsing.
16650 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16651 in a decl-specifier-seq.
16653 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16654 class-specifier, enum-specifier, or elaborated-type-specifier, then
16655 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16656 if a type is declared; 2 if it is defined. Otherwise, it is set to
16657 zero.
16659 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16660 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16661 is set to FALSE. */
16663 static tree
16664 cp_parser_type_specifier (cp_parser* parser,
16665 cp_parser_flags flags,
16666 cp_decl_specifier_seq *decl_specs,
16667 bool is_declaration,
16668 int* declares_class_or_enum,
16669 bool* is_cv_qualifier)
16671 tree type_spec = NULL_TREE;
16672 cp_token *token;
16673 enum rid keyword;
16674 cp_decl_spec ds = ds_last;
16676 /* Assume this type-specifier does not declare a new type. */
16677 if (declares_class_or_enum)
16678 *declares_class_or_enum = 0;
16679 /* And that it does not specify a cv-qualifier. */
16680 if (is_cv_qualifier)
16681 *is_cv_qualifier = false;
16682 /* Peek at the next token. */
16683 token = cp_lexer_peek_token (parser->lexer);
16685 /* If we're looking at a keyword, we can use that to guide the
16686 production we choose. */
16687 keyword = token->keyword;
16688 switch (keyword)
16690 case RID_ENUM:
16691 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16692 goto elaborated_type_specifier;
16694 /* Look for the enum-specifier. */
16695 type_spec = cp_parser_enum_specifier (parser);
16696 /* If that worked, we're done. */
16697 if (type_spec)
16699 if (declares_class_or_enum)
16700 *declares_class_or_enum = 2;
16701 if (decl_specs)
16702 cp_parser_set_decl_spec_type (decl_specs,
16703 type_spec,
16704 token,
16705 /*type_definition_p=*/true);
16706 return type_spec;
16708 else
16709 goto elaborated_type_specifier;
16711 /* Any of these indicate either a class-specifier, or an
16712 elaborated-type-specifier. */
16713 case RID_CLASS:
16714 case RID_STRUCT:
16715 case RID_UNION:
16716 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16717 goto elaborated_type_specifier;
16719 /* Parse tentatively so that we can back up if we don't find a
16720 class-specifier. */
16721 cp_parser_parse_tentatively (parser);
16722 /* Look for the class-specifier. */
16723 type_spec = cp_parser_class_specifier (parser);
16724 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16725 /* If that worked, we're done. */
16726 if (cp_parser_parse_definitely (parser))
16728 if (declares_class_or_enum)
16729 *declares_class_or_enum = 2;
16730 if (decl_specs)
16731 cp_parser_set_decl_spec_type (decl_specs,
16732 type_spec,
16733 token,
16734 /*type_definition_p=*/true);
16735 return type_spec;
16738 /* Fall through. */
16739 elaborated_type_specifier:
16740 /* We're declaring (not defining) a class or enum. */
16741 if (declares_class_or_enum)
16742 *declares_class_or_enum = 1;
16744 /* Fall through. */
16745 case RID_TYPENAME:
16746 /* Look for an elaborated-type-specifier. */
16747 type_spec
16748 = (cp_parser_elaborated_type_specifier
16749 (parser,
16750 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16751 is_declaration));
16752 if (decl_specs)
16753 cp_parser_set_decl_spec_type (decl_specs,
16754 type_spec,
16755 token,
16756 /*type_definition_p=*/false);
16757 return type_spec;
16759 case RID_CONST:
16760 ds = ds_const;
16761 if (is_cv_qualifier)
16762 *is_cv_qualifier = true;
16763 break;
16765 case RID_VOLATILE:
16766 ds = ds_volatile;
16767 if (is_cv_qualifier)
16768 *is_cv_qualifier = true;
16769 break;
16771 case RID_RESTRICT:
16772 ds = ds_restrict;
16773 if (is_cv_qualifier)
16774 *is_cv_qualifier = true;
16775 break;
16777 case RID_COMPLEX:
16778 /* The `__complex__' keyword is a GNU extension. */
16779 ds = ds_complex;
16780 break;
16782 default:
16783 break;
16786 /* Handle simple keywords. */
16787 if (ds != ds_last)
16789 if (decl_specs)
16791 set_and_check_decl_spec_loc (decl_specs, ds, token);
16792 decl_specs->any_specifiers_p = true;
16794 return cp_lexer_consume_token (parser->lexer)->u.value;
16797 /* If we do not already have a type-specifier, assume we are looking
16798 at a simple-type-specifier. */
16799 type_spec = cp_parser_simple_type_specifier (parser,
16800 decl_specs,
16801 flags);
16803 /* If we didn't find a type-specifier, and a type-specifier was not
16804 optional in this context, issue an error message. */
16805 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16807 cp_parser_error (parser, "expected type specifier");
16808 return error_mark_node;
16811 return type_spec;
16814 /* Parse a simple-type-specifier.
16816 simple-type-specifier:
16817 :: [opt] nested-name-specifier [opt] type-name
16818 :: [opt] nested-name-specifier template template-id
16819 char
16820 wchar_t
16821 bool
16822 short
16824 long
16825 signed
16826 unsigned
16827 float
16828 double
16829 void
16831 C++11 Extension:
16833 simple-type-specifier:
16834 auto
16835 decltype ( expression )
16836 char16_t
16837 char32_t
16838 __underlying_type ( type-id )
16840 C++17 extension:
16842 nested-name-specifier(opt) template-name
16844 GNU Extension:
16846 simple-type-specifier:
16847 __int128
16848 __typeof__ unary-expression
16849 __typeof__ ( type-id )
16850 __typeof__ ( type-id ) { initializer-list , [opt] }
16852 Concepts Extension:
16854 simple-type-specifier:
16855 constrained-type-specifier
16857 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16858 appropriately updated. */
16860 static tree
16861 cp_parser_simple_type_specifier (cp_parser* parser,
16862 cp_decl_specifier_seq *decl_specs,
16863 cp_parser_flags flags)
16865 tree type = NULL_TREE;
16866 cp_token *token;
16867 int idx;
16869 /* Peek at the next token. */
16870 token = cp_lexer_peek_token (parser->lexer);
16872 /* If we're looking at a keyword, things are easy. */
16873 switch (token->keyword)
16875 case RID_CHAR:
16876 if (decl_specs)
16877 decl_specs->explicit_char_p = true;
16878 type = char_type_node;
16879 break;
16880 case RID_CHAR16:
16881 type = char16_type_node;
16882 break;
16883 case RID_CHAR32:
16884 type = char32_type_node;
16885 break;
16886 case RID_WCHAR:
16887 type = wchar_type_node;
16888 break;
16889 case RID_BOOL:
16890 type = boolean_type_node;
16891 break;
16892 case RID_SHORT:
16893 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16894 type = short_integer_type_node;
16895 break;
16896 case RID_INT:
16897 if (decl_specs)
16898 decl_specs->explicit_int_p = true;
16899 type = integer_type_node;
16900 break;
16901 case RID_INT_N_0:
16902 case RID_INT_N_1:
16903 case RID_INT_N_2:
16904 case RID_INT_N_3:
16905 idx = token->keyword - RID_INT_N_0;
16906 if (! int_n_enabled_p [idx])
16907 break;
16908 if (decl_specs)
16910 decl_specs->explicit_intN_p = true;
16911 decl_specs->int_n_idx = idx;
16913 type = int_n_trees [idx].signed_type;
16914 break;
16915 case RID_LONG:
16916 if (decl_specs)
16917 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16918 type = long_integer_type_node;
16919 break;
16920 case RID_SIGNED:
16921 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16922 type = integer_type_node;
16923 break;
16924 case RID_UNSIGNED:
16925 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16926 type = unsigned_type_node;
16927 break;
16928 case RID_FLOAT:
16929 type = float_type_node;
16930 break;
16931 case RID_DOUBLE:
16932 type = double_type_node;
16933 break;
16934 case RID_VOID:
16935 type = void_type_node;
16936 break;
16938 case RID_AUTO:
16939 maybe_warn_cpp0x (CPP0X_AUTO);
16940 if (parser->auto_is_implicit_function_template_parm_p)
16942 /* The 'auto' might be the placeholder return type for a function decl
16943 with trailing return type. */
16944 bool have_trailing_return_fn_decl = false;
16946 cp_parser_parse_tentatively (parser);
16947 cp_lexer_consume_token (parser->lexer);
16948 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16949 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16950 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16951 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16953 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16955 cp_lexer_consume_token (parser->lexer);
16956 cp_parser_skip_to_closing_parenthesis (parser,
16957 /*recovering*/false,
16958 /*or_comma*/false,
16959 /*consume_paren*/true);
16960 continue;
16963 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16965 have_trailing_return_fn_decl = true;
16966 break;
16969 cp_lexer_consume_token (parser->lexer);
16971 cp_parser_abort_tentative_parse (parser);
16973 if (have_trailing_return_fn_decl)
16975 type = make_auto ();
16976 break;
16979 if (cxx_dialect >= cxx14)
16981 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16982 type = TREE_TYPE (type);
16984 else
16985 type = error_mark_node;
16987 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16989 if (cxx_dialect < cxx14)
16990 error_at (token->location,
16991 "use of %<auto%> in lambda parameter declaration "
16992 "only available with "
16993 "-std=c++14 or -std=gnu++14");
16995 else if (cxx_dialect < cxx14)
16996 error_at (token->location,
16997 "use of %<auto%> in parameter declaration "
16998 "only available with "
16999 "-std=c++14 or -std=gnu++14");
17000 else if (!flag_concepts)
17001 pedwarn (token->location, OPT_Wpedantic,
17002 "ISO C++ forbids use of %<auto%> in parameter "
17003 "declaration");
17005 else
17006 type = make_auto ();
17007 break;
17009 case RID_DECLTYPE:
17010 /* Since DR 743, decltype can either be a simple-type-specifier by
17011 itself or begin a nested-name-specifier. Parsing it will replace
17012 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
17013 handling below decide what to do. */
17014 cp_parser_decltype (parser);
17015 cp_lexer_set_token_position (parser->lexer, token);
17016 break;
17018 case RID_TYPEOF:
17019 /* Consume the `typeof' token. */
17020 cp_lexer_consume_token (parser->lexer);
17021 /* Parse the operand to `typeof'. */
17022 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
17023 /* If it is not already a TYPE, take its type. */
17024 if (!TYPE_P (type))
17025 type = finish_typeof (type);
17027 if (decl_specs)
17028 cp_parser_set_decl_spec_type (decl_specs, type,
17029 token,
17030 /*type_definition_p=*/false);
17032 return type;
17034 case RID_UNDERLYING_TYPE:
17035 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
17036 if (decl_specs)
17037 cp_parser_set_decl_spec_type (decl_specs, type,
17038 token,
17039 /*type_definition_p=*/false);
17041 return type;
17043 case RID_BASES:
17044 case RID_DIRECT_BASES:
17045 type = cp_parser_trait_expr (parser, token->keyword);
17046 if (decl_specs)
17047 cp_parser_set_decl_spec_type (decl_specs, type,
17048 token,
17049 /*type_definition_p=*/false);
17050 return type;
17051 default:
17052 break;
17055 /* If token is an already-parsed decltype not followed by ::,
17056 it's a simple-type-specifier. */
17057 if (token->type == CPP_DECLTYPE
17058 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
17060 type = saved_checks_value (token->u.tree_check_value);
17061 if (decl_specs)
17063 cp_parser_set_decl_spec_type (decl_specs, type,
17064 token,
17065 /*type_definition_p=*/false);
17066 /* Remember that we are handling a decltype in order to
17067 implement the resolution of DR 1510 when the argument
17068 isn't instantiation dependent. */
17069 decl_specs->decltype_p = true;
17071 cp_lexer_consume_token (parser->lexer);
17072 return type;
17075 /* If the type-specifier was for a built-in type, we're done. */
17076 if (type)
17078 /* Record the type. */
17079 if (decl_specs
17080 && (token->keyword != RID_SIGNED
17081 && token->keyword != RID_UNSIGNED
17082 && token->keyword != RID_SHORT
17083 && token->keyword != RID_LONG))
17084 cp_parser_set_decl_spec_type (decl_specs,
17085 type,
17086 token,
17087 /*type_definition_p=*/false);
17088 if (decl_specs)
17089 decl_specs->any_specifiers_p = true;
17091 /* Consume the token. */
17092 cp_lexer_consume_token (parser->lexer);
17094 if (type == error_mark_node)
17095 return error_mark_node;
17097 /* There is no valid C++ program where a non-template type is
17098 followed by a "<". That usually indicates that the user thought
17099 that the type was a template. */
17100 cp_parser_check_for_invalid_template_id (parser, type, none_type,
17101 token->location);
17103 return TYPE_NAME (type);
17106 /* The type-specifier must be a user-defined type. */
17107 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17109 bool qualified_p;
17110 bool global_p;
17112 /* Don't gobble tokens or issue error messages if this is an
17113 optional type-specifier. */
17114 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17115 cp_parser_parse_tentatively (parser);
17117 token = cp_lexer_peek_token (parser->lexer);
17119 /* Look for the optional `::' operator. */
17120 global_p
17121 = (cp_parser_global_scope_opt (parser,
17122 /*current_scope_valid_p=*/false)
17123 != NULL_TREE);
17124 /* Look for the nested-name specifier. */
17125 qualified_p
17126 = (cp_parser_nested_name_specifier_opt (parser,
17127 /*typename_keyword_p=*/false,
17128 /*check_dependency_p=*/true,
17129 /*type_p=*/false,
17130 /*is_declaration=*/false)
17131 != NULL_TREE);
17132 /* If we have seen a nested-name-specifier, and the next token
17133 is `template', then we are using the template-id production. */
17134 if (parser->scope
17135 && cp_parser_optional_template_keyword (parser))
17137 /* Look for the template-id. */
17138 type = cp_parser_template_id (parser,
17139 /*template_keyword_p=*/true,
17140 /*check_dependency_p=*/true,
17141 none_type,
17142 /*is_declaration=*/false);
17143 /* If the template-id did not name a type, we are out of
17144 luck. */
17145 if (TREE_CODE (type) != TYPE_DECL)
17147 cp_parser_error (parser, "expected template-id for type");
17148 type = NULL_TREE;
17151 /* Otherwise, look for a type-name. */
17152 else
17153 type = cp_parser_type_name (parser);
17154 /* Keep track of all name-lookups performed in class scopes. */
17155 if (type
17156 && !global_p
17157 && !qualified_p
17158 && TREE_CODE (type) == TYPE_DECL
17159 && identifier_p (DECL_NAME (type)))
17160 maybe_note_name_used_in_class (DECL_NAME (type), type);
17161 /* If it didn't work out, we don't have a TYPE. */
17162 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17163 && !cp_parser_parse_definitely (parser))
17164 type = NULL_TREE;
17165 if (!type && cxx_dialect >= cxx1z)
17167 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17168 cp_parser_parse_tentatively (parser);
17170 cp_parser_global_scope_opt (parser,
17171 /*current_scope_valid_p=*/false);
17172 cp_parser_nested_name_specifier_opt (parser,
17173 /*typename_keyword_p=*/false,
17174 /*check_dependency_p=*/true,
17175 /*type_p=*/false,
17176 /*is_declaration=*/false);
17177 tree name = cp_parser_identifier (parser);
17178 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17179 && parser->scope != error_mark_node)
17181 tree tmpl = cp_parser_lookup_name (parser, name,
17182 none_type,
17183 /*is_template=*/false,
17184 /*is_namespace=*/false,
17185 /*check_dependency=*/true,
17186 /*ambiguous_decls=*/NULL,
17187 token->location);
17188 if (tmpl && tmpl != error_mark_node
17189 && (DECL_CLASS_TEMPLATE_P (tmpl)
17190 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17191 type = make_template_placeholder (tmpl);
17192 else
17194 type = error_mark_node;
17195 if (!cp_parser_simulate_error (parser))
17196 cp_parser_name_lookup_error (parser, name, tmpl,
17197 NLE_TYPE, token->location);
17200 else
17201 type = error_mark_node;
17203 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17204 && !cp_parser_parse_definitely (parser))
17205 type = NULL_TREE;
17207 if (type && decl_specs)
17208 cp_parser_set_decl_spec_type (decl_specs, type,
17209 token,
17210 /*type_definition_p=*/false);
17213 /* If we didn't get a type-name, issue an error message. */
17214 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17216 cp_parser_error (parser, "expected type-name");
17217 return error_mark_node;
17220 if (type && type != error_mark_node)
17222 /* See if TYPE is an Objective-C type, and if so, parse and
17223 accept any protocol references following it. Do this before
17224 the cp_parser_check_for_invalid_template_id() call, because
17225 Objective-C types can be followed by '<...>' which would
17226 enclose protocol names rather than template arguments, and so
17227 everything is fine. */
17228 if (c_dialect_objc () && !parser->scope
17229 && (objc_is_id (type) || objc_is_class_name (type)))
17231 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17232 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17234 /* Clobber the "unqualified" type previously entered into
17235 DECL_SPECS with the new, improved protocol-qualified version. */
17236 if (decl_specs)
17237 decl_specs->type = qual_type;
17239 return qual_type;
17242 /* There is no valid C++ program where a non-template type is
17243 followed by a "<". That usually indicates that the user
17244 thought that the type was a template. */
17245 cp_parser_check_for_invalid_template_id (parser, type,
17246 none_type,
17247 token->location);
17250 return type;
17253 /* Parse a type-name.
17255 type-name:
17256 class-name
17257 enum-name
17258 typedef-name
17259 simple-template-id [in c++0x]
17261 enum-name:
17262 identifier
17264 typedef-name:
17265 identifier
17267 Concepts:
17269 type-name:
17270 concept-name
17271 partial-concept-id
17273 concept-name:
17274 identifier
17276 Returns a TYPE_DECL for the type. */
17278 static tree
17279 cp_parser_type_name (cp_parser* parser)
17281 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17284 /* See above. */
17285 static tree
17286 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17288 tree type_decl;
17290 /* We can't know yet whether it is a class-name or not. */
17291 cp_parser_parse_tentatively (parser);
17292 /* Try a class-name. */
17293 type_decl = cp_parser_class_name (parser,
17294 typename_keyword_p,
17295 /*template_keyword_p=*/false,
17296 none_type,
17297 /*check_dependency_p=*/true,
17298 /*class_head_p=*/false,
17299 /*is_declaration=*/false);
17300 /* If it's not a class-name, keep looking. */
17301 if (!cp_parser_parse_definitely (parser))
17303 if (cxx_dialect < cxx11)
17304 /* It must be a typedef-name or an enum-name. */
17305 return cp_parser_nonclass_name (parser);
17307 cp_parser_parse_tentatively (parser);
17308 /* It is either a simple-template-id representing an
17309 instantiation of an alias template... */
17310 type_decl = cp_parser_template_id (parser,
17311 /*template_keyword_p=*/false,
17312 /*check_dependency_p=*/true,
17313 none_type,
17314 /*is_declaration=*/false);
17315 /* Note that this must be an instantiation of an alias template
17316 because [temp.names]/6 says:
17318 A template-id that names an alias template specialization
17319 is a type-name.
17321 Whereas [temp.names]/7 says:
17323 A simple-template-id that names a class template
17324 specialization is a class-name.
17326 With concepts, this could also be a partial-concept-id that
17327 declares a non-type template parameter. */
17328 if (type_decl != NULL_TREE
17329 && TREE_CODE (type_decl) == TYPE_DECL
17330 && TYPE_DECL_ALIAS_P (type_decl))
17331 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17332 else if (is_constrained_parameter (type_decl))
17333 /* Don't do anything. */ ;
17334 else
17335 cp_parser_simulate_error (parser);
17337 if (!cp_parser_parse_definitely (parser))
17338 /* ... Or a typedef-name or an enum-name. */
17339 return cp_parser_nonclass_name (parser);
17342 return type_decl;
17345 /* Check if DECL and ARGS can form a constrained-type-specifier.
17346 If ARGS is non-null, we try to form a concept check of the
17347 form DECL<?, ARGS> where ? is a wildcard that matches any
17348 kind of template argument. If ARGS is NULL, then we try to
17349 form a concept check of the form DECL<?>. */
17351 static tree
17352 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17353 tree decl, tree args)
17355 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17357 /* If we a constrained-type-specifier cannot be deduced. */
17358 if (parser->prevent_constrained_type_specifiers)
17359 return NULL_TREE;
17361 /* A constrained type specifier can only be found in an
17362 overload set or as a reference to a template declaration.
17364 FIXME: This might be masking a bug. It's possible that
17365 that the deduction below is causing template specializations
17366 to be formed with the wildcard as an argument. */
17367 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17368 return NULL_TREE;
17370 /* Try to build a call expression that evaluates the
17371 concept. This can fail if the overload set refers
17372 only to non-templates. */
17373 tree placeholder = build_nt (WILDCARD_DECL);
17374 tree check = build_concept_check (decl, placeholder, args);
17375 if (check == error_mark_node)
17376 return NULL_TREE;
17378 /* Deduce the checked constraint and the prototype parameter.
17380 FIXME: In certain cases, failure to deduce should be a
17381 diagnosable error. */
17382 tree conc;
17383 tree proto;
17384 if (!deduce_constrained_parameter (check, conc, proto))
17385 return NULL_TREE;
17387 /* In template parameter scope, this results in a constrained
17388 parameter. Return a descriptor of that parm. */
17389 if (processing_template_parmlist)
17390 return build_constrained_parameter (conc, proto, args);
17392 /* In a parameter-declaration-clause, constrained-type
17393 specifiers result in invented template parameters. */
17394 if (parser->auto_is_implicit_function_template_parm_p)
17396 tree x = build_constrained_parameter (conc, proto, args);
17397 return synthesize_implicit_template_parm (parser, x);
17399 else
17401 /* Otherwise, we're in a context where the constrained
17402 type name is deduced and the constraint applies
17403 after deduction. */
17404 return make_constrained_auto (conc, args);
17407 return NULL_TREE;
17410 /* If DECL refers to a concept, return a TYPE_DECL representing
17411 the result of using the constrained type specifier in the
17412 current context. DECL refers to a concept if
17414 - it is an overload set containing a function concept taking a single
17415 type argument, or
17417 - it is a variable concept taking a single type argument. */
17419 static tree
17420 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17422 if (flag_concepts
17423 && (TREE_CODE (decl) == OVERLOAD
17424 || BASELINK_P (decl)
17425 || variable_concept_p (decl)))
17426 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17427 else
17428 return NULL_TREE;
17431 /* Check if DECL and ARGS form a partial-concept-id. If so,
17432 assign ID to the resulting constrained placeholder.
17434 Returns true if the partial-concept-id designates a placeholder
17435 and false otherwise. Note that *id is set to NULL_TREE in
17436 this case. */
17438 static tree
17439 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17441 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17444 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17445 or a concept-name.
17447 enum-name:
17448 identifier
17450 typedef-name:
17451 identifier
17453 concept-name:
17454 identifier
17456 Returns a TYPE_DECL for the type. */
17458 static tree
17459 cp_parser_nonclass_name (cp_parser* parser)
17461 tree type_decl;
17462 tree identifier;
17464 cp_token *token = cp_lexer_peek_token (parser->lexer);
17465 identifier = cp_parser_identifier (parser);
17466 if (identifier == error_mark_node)
17467 return error_mark_node;
17469 /* Look up the type-name. */
17470 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17472 type_decl = strip_using_decl (type_decl);
17474 /* If we found an overload set, then it may refer to a concept-name. */
17475 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17476 type_decl = decl;
17478 if (TREE_CODE (type_decl) != TYPE_DECL
17479 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17481 /* See if this is an Objective-C type. */
17482 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17483 tree type = objc_get_protocol_qualified_type (identifier, protos);
17484 if (type)
17485 type_decl = TYPE_NAME (type);
17488 /* Issue an error if we did not find a type-name. */
17489 if (TREE_CODE (type_decl) != TYPE_DECL
17490 /* In Objective-C, we have the complication that class names are
17491 normally type names and start declarations (eg, the
17492 "NSObject" in "NSObject *object;"), but can be used in an
17493 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17494 is an expression. So, a classname followed by a dot is not a
17495 valid type-name. */
17496 || (objc_is_class_name (TREE_TYPE (type_decl))
17497 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17499 if (!cp_parser_simulate_error (parser))
17500 cp_parser_name_lookup_error (parser, identifier, type_decl,
17501 NLE_TYPE, token->location);
17502 return error_mark_node;
17504 /* Remember that the name was used in the definition of the
17505 current class so that we can check later to see if the
17506 meaning would have been different after the class was
17507 entirely defined. */
17508 else if (type_decl != error_mark_node
17509 && !parser->scope)
17510 maybe_note_name_used_in_class (identifier, type_decl);
17512 return type_decl;
17515 /* Parse an elaborated-type-specifier. Note that the grammar given
17516 here incorporates the resolution to DR68.
17518 elaborated-type-specifier:
17519 class-key :: [opt] nested-name-specifier [opt] identifier
17520 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17521 enum-key :: [opt] nested-name-specifier [opt] identifier
17522 typename :: [opt] nested-name-specifier identifier
17523 typename :: [opt] nested-name-specifier template [opt]
17524 template-id
17526 GNU extension:
17528 elaborated-type-specifier:
17529 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17530 class-key attributes :: [opt] nested-name-specifier [opt]
17531 template [opt] template-id
17532 enum attributes :: [opt] nested-name-specifier [opt] identifier
17534 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17535 declared `friend'. If IS_DECLARATION is TRUE, then this
17536 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17537 something is being declared.
17539 Returns the TYPE specified. */
17541 static tree
17542 cp_parser_elaborated_type_specifier (cp_parser* parser,
17543 bool is_friend,
17544 bool is_declaration)
17546 enum tag_types tag_type;
17547 tree identifier;
17548 tree type = NULL_TREE;
17549 tree attributes = NULL_TREE;
17550 tree globalscope;
17551 cp_token *token = NULL;
17553 /* See if we're looking at the `enum' keyword. */
17554 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17556 /* Consume the `enum' token. */
17557 cp_lexer_consume_token (parser->lexer);
17558 /* Remember that it's an enumeration type. */
17559 tag_type = enum_type;
17560 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17561 enums) is used here. */
17562 cp_token *token = cp_lexer_peek_token (parser->lexer);
17563 if (cp_parser_is_keyword (token, RID_CLASS)
17564 || cp_parser_is_keyword (token, RID_STRUCT))
17566 gcc_rich_location richloc (token->location);
17567 richloc.add_range (input_location, false);
17568 richloc.add_fixit_remove ();
17569 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17570 "a scoped enum must not use the %qD keyword",
17571 token->u.value);
17572 /* Consume the `struct' or `class' and parse it anyway. */
17573 cp_lexer_consume_token (parser->lexer);
17575 /* Parse the attributes. */
17576 attributes = cp_parser_attributes_opt (parser);
17578 /* Or, it might be `typename'. */
17579 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17580 RID_TYPENAME))
17582 /* Consume the `typename' token. */
17583 cp_lexer_consume_token (parser->lexer);
17584 /* Remember that it's a `typename' type. */
17585 tag_type = typename_type;
17587 /* Otherwise it must be a class-key. */
17588 else
17590 tag_type = cp_parser_class_key (parser);
17591 if (tag_type == none_type)
17592 return error_mark_node;
17593 /* Parse the attributes. */
17594 attributes = cp_parser_attributes_opt (parser);
17597 /* Look for the `::' operator. */
17598 globalscope = cp_parser_global_scope_opt (parser,
17599 /*current_scope_valid_p=*/false);
17600 /* Look for the nested-name-specifier. */
17601 tree nested_name_specifier;
17602 if (tag_type == typename_type && !globalscope)
17604 nested_name_specifier
17605 = cp_parser_nested_name_specifier (parser,
17606 /*typename_keyword_p=*/true,
17607 /*check_dependency_p=*/true,
17608 /*type_p=*/true,
17609 is_declaration);
17610 if (!nested_name_specifier)
17611 return error_mark_node;
17613 else
17614 /* Even though `typename' is not present, the proposed resolution
17615 to Core Issue 180 says that in `class A<T>::B', `B' should be
17616 considered a type-name, even if `A<T>' is dependent. */
17617 nested_name_specifier
17618 = cp_parser_nested_name_specifier_opt (parser,
17619 /*typename_keyword_p=*/true,
17620 /*check_dependency_p=*/true,
17621 /*type_p=*/true,
17622 is_declaration);
17623 /* For everything but enumeration types, consider a template-id.
17624 For an enumeration type, consider only a plain identifier. */
17625 if (tag_type != enum_type)
17627 bool template_p = false;
17628 tree decl;
17630 /* Allow the `template' keyword. */
17631 template_p = cp_parser_optional_template_keyword (parser);
17632 /* If we didn't see `template', we don't know if there's a
17633 template-id or not. */
17634 if (!template_p)
17635 cp_parser_parse_tentatively (parser);
17636 /* Parse the template-id. */
17637 token = cp_lexer_peek_token (parser->lexer);
17638 decl = cp_parser_template_id (parser, template_p,
17639 /*check_dependency_p=*/true,
17640 tag_type,
17641 is_declaration);
17642 /* If we didn't find a template-id, look for an ordinary
17643 identifier. */
17644 if (!template_p && !cp_parser_parse_definitely (parser))
17646 /* We can get here when cp_parser_template_id, called by
17647 cp_parser_class_name with tag_type == none_type, succeeds
17648 and caches a BASELINK. Then, when called again here,
17649 instead of failing and returning an error_mark_node
17650 returns it (see template/typename17.C in C++11).
17651 ??? Could we diagnose this earlier? */
17652 else if (tag_type == typename_type && BASELINK_P (decl))
17654 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17655 type = error_mark_node;
17657 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17658 in effect, then we must assume that, upon instantiation, the
17659 template will correspond to a class. */
17660 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17661 && tag_type == typename_type)
17662 type = make_typename_type (parser->scope, decl,
17663 typename_type,
17664 /*complain=*/tf_error);
17665 /* If the `typename' keyword is in effect and DECL is not a type
17666 decl, then type is non existent. */
17667 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17669 else if (TREE_CODE (decl) == TYPE_DECL)
17671 type = check_elaborated_type_specifier (tag_type, decl,
17672 /*allow_template_p=*/true);
17674 /* If the next token is a semicolon, this must be a specialization,
17675 instantiation, or friend declaration. Check the scope while we
17676 still know whether or not we had a nested-name-specifier. */
17677 if (type != error_mark_node
17678 && !nested_name_specifier && !is_friend
17679 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17680 check_unqualified_spec_or_inst (type, token->location);
17682 else if (decl == error_mark_node)
17683 type = error_mark_node;
17686 if (!type)
17688 token = cp_lexer_peek_token (parser->lexer);
17689 identifier = cp_parser_identifier (parser);
17691 if (identifier == error_mark_node)
17693 parser->scope = NULL_TREE;
17694 return error_mark_node;
17697 /* For a `typename', we needn't call xref_tag. */
17698 if (tag_type == typename_type
17699 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17700 return cp_parser_make_typename_type (parser, identifier,
17701 token->location);
17703 /* Template parameter lists apply only if we are not within a
17704 function parameter list. */
17705 bool template_parm_lists_apply
17706 = parser->num_template_parameter_lists;
17707 if (template_parm_lists_apply)
17708 for (cp_binding_level *s = current_binding_level;
17709 s && s->kind != sk_template_parms;
17710 s = s->level_chain)
17711 if (s->kind == sk_function_parms)
17712 template_parm_lists_apply = false;
17714 /* Look up a qualified name in the usual way. */
17715 if (parser->scope)
17717 tree decl;
17718 tree ambiguous_decls;
17720 decl = cp_parser_lookup_name (parser, identifier,
17721 tag_type,
17722 /*is_template=*/false,
17723 /*is_namespace=*/false,
17724 /*check_dependency=*/true,
17725 &ambiguous_decls,
17726 token->location);
17728 /* If the lookup was ambiguous, an error will already have been
17729 issued. */
17730 if (ambiguous_decls)
17731 return error_mark_node;
17733 /* If we are parsing friend declaration, DECL may be a
17734 TEMPLATE_DECL tree node here. However, we need to check
17735 whether this TEMPLATE_DECL results in valid code. Consider
17736 the following example:
17738 namespace N {
17739 template <class T> class C {};
17741 class X {
17742 template <class T> friend class N::C; // #1, valid code
17744 template <class T> class Y {
17745 friend class N::C; // #2, invalid code
17748 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17749 name lookup of `N::C'. We see that friend declaration must
17750 be template for the code to be valid. Note that
17751 processing_template_decl does not work here since it is
17752 always 1 for the above two cases. */
17754 decl = (cp_parser_maybe_treat_template_as_class
17755 (decl, /*tag_name_p=*/is_friend
17756 && template_parm_lists_apply));
17758 if (TREE_CODE (decl) != TYPE_DECL)
17760 cp_parser_diagnose_invalid_type_name (parser,
17761 identifier,
17762 token->location);
17763 return error_mark_node;
17766 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17768 bool allow_template = (template_parm_lists_apply
17769 || DECL_SELF_REFERENCE_P (decl));
17770 type = check_elaborated_type_specifier (tag_type, decl,
17771 allow_template);
17773 if (type == error_mark_node)
17774 return error_mark_node;
17777 /* Forward declarations of nested types, such as
17779 class C1::C2;
17780 class C1::C2::C3;
17782 are invalid unless all components preceding the final '::'
17783 are complete. If all enclosing types are complete, these
17784 declarations become merely pointless.
17786 Invalid forward declarations of nested types are errors
17787 caught elsewhere in parsing. Those that are pointless arrive
17788 here. */
17790 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17791 && !is_friend && !processing_explicit_instantiation)
17792 warning (0, "declaration %qD does not declare anything", decl);
17794 type = TREE_TYPE (decl);
17796 else
17798 /* An elaborated-type-specifier sometimes introduces a new type and
17799 sometimes names an existing type. Normally, the rule is that it
17800 introduces a new type only if there is not an existing type of
17801 the same name already in scope. For example, given:
17803 struct S {};
17804 void f() { struct S s; }
17806 the `struct S' in the body of `f' is the same `struct S' as in
17807 the global scope; the existing definition is used. However, if
17808 there were no global declaration, this would introduce a new
17809 local class named `S'.
17811 An exception to this rule applies to the following code:
17813 namespace N { struct S; }
17815 Here, the elaborated-type-specifier names a new type
17816 unconditionally; even if there is already an `S' in the
17817 containing scope this declaration names a new type.
17818 This exception only applies if the elaborated-type-specifier
17819 forms the complete declaration:
17821 [class.name]
17823 A declaration consisting solely of `class-key identifier ;' is
17824 either a redeclaration of the name in the current scope or a
17825 forward declaration of the identifier as a class name. It
17826 introduces the name into the current scope.
17828 We are in this situation precisely when the next token is a `;'.
17830 An exception to the exception is that a `friend' declaration does
17831 *not* name a new type; i.e., given:
17833 struct S { friend struct T; };
17835 `T' is not a new type in the scope of `S'.
17837 Also, `new struct S' or `sizeof (struct S)' never results in the
17838 definition of a new type; a new type can only be declared in a
17839 declaration context. */
17841 tag_scope ts;
17842 bool template_p;
17844 if (is_friend)
17845 /* Friends have special name lookup rules. */
17846 ts = ts_within_enclosing_non_class;
17847 else if (is_declaration
17848 && cp_lexer_next_token_is (parser->lexer,
17849 CPP_SEMICOLON))
17850 /* This is a `class-key identifier ;' */
17851 ts = ts_current;
17852 else
17853 ts = ts_global;
17855 template_p =
17856 (template_parm_lists_apply
17857 && (cp_parser_next_token_starts_class_definition_p (parser)
17858 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17859 /* An unqualified name was used to reference this type, so
17860 there were no qualifying templates. */
17861 if (template_parm_lists_apply
17862 && !cp_parser_check_template_parameters (parser,
17863 /*num_templates=*/0,
17864 token->location,
17865 /*declarator=*/NULL))
17866 return error_mark_node;
17867 type = xref_tag (tag_type, identifier, ts, template_p);
17871 if (type == error_mark_node)
17872 return error_mark_node;
17874 /* Allow attributes on forward declarations of classes. */
17875 if (attributes)
17877 if (TREE_CODE (type) == TYPENAME_TYPE)
17878 warning (OPT_Wattributes,
17879 "attributes ignored on uninstantiated type");
17880 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17881 && ! processing_explicit_instantiation)
17882 warning (OPT_Wattributes,
17883 "attributes ignored on template instantiation");
17884 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17885 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17886 else
17887 warning (OPT_Wattributes,
17888 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17891 if (tag_type != enum_type)
17893 /* Indicate whether this class was declared as a `class' or as a
17894 `struct'. */
17895 if (CLASS_TYPE_P (type))
17896 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17897 cp_parser_check_class_key (tag_type, type);
17900 /* A "<" cannot follow an elaborated type specifier. If that
17901 happens, the user was probably trying to form a template-id. */
17902 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17903 token->location);
17905 return type;
17908 /* Parse an enum-specifier.
17910 enum-specifier:
17911 enum-head { enumerator-list [opt] }
17912 enum-head { enumerator-list , } [C++0x]
17914 enum-head:
17915 enum-key identifier [opt] enum-base [opt]
17916 enum-key nested-name-specifier identifier enum-base [opt]
17918 enum-key:
17919 enum
17920 enum class [C++0x]
17921 enum struct [C++0x]
17923 enum-base: [C++0x]
17924 : type-specifier-seq
17926 opaque-enum-specifier:
17927 enum-key identifier enum-base [opt] ;
17929 GNU Extensions:
17930 enum-key attributes[opt] identifier [opt] enum-base [opt]
17931 { enumerator-list [opt] }attributes[opt]
17932 enum-key attributes[opt] identifier [opt] enum-base [opt]
17933 { enumerator-list, }attributes[opt] [C++0x]
17935 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17936 if the token stream isn't an enum-specifier after all. */
17938 static tree
17939 cp_parser_enum_specifier (cp_parser* parser)
17941 tree identifier;
17942 tree type = NULL_TREE;
17943 tree prev_scope;
17944 tree nested_name_specifier = NULL_TREE;
17945 tree attributes;
17946 bool scoped_enum_p = false;
17947 bool has_underlying_type = false;
17948 bool nested_being_defined = false;
17949 bool new_value_list = false;
17950 bool is_new_type = false;
17951 bool is_unnamed = false;
17952 tree underlying_type = NULL_TREE;
17953 cp_token *type_start_token = NULL;
17954 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17956 parser->colon_corrects_to_scope_p = false;
17958 /* Parse tentatively so that we can back up if we don't find a
17959 enum-specifier. */
17960 cp_parser_parse_tentatively (parser);
17962 /* Caller guarantees that the current token is 'enum', an identifier
17963 possibly follows, and the token after that is an opening brace.
17964 If we don't have an identifier, fabricate an anonymous name for
17965 the enumeration being defined. */
17966 cp_lexer_consume_token (parser->lexer);
17968 /* Parse the "class" or "struct", which indicates a scoped
17969 enumeration type in C++0x. */
17970 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17971 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17973 if (cxx_dialect < cxx11)
17974 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17976 /* Consume the `struct' or `class' token. */
17977 cp_lexer_consume_token (parser->lexer);
17979 scoped_enum_p = true;
17982 attributes = cp_parser_attributes_opt (parser);
17984 /* Clear the qualification. */
17985 parser->scope = NULL_TREE;
17986 parser->qualifying_scope = NULL_TREE;
17987 parser->object_scope = NULL_TREE;
17989 /* Figure out in what scope the declaration is being placed. */
17990 prev_scope = current_scope ();
17992 type_start_token = cp_lexer_peek_token (parser->lexer);
17994 push_deferring_access_checks (dk_no_check);
17995 nested_name_specifier
17996 = cp_parser_nested_name_specifier_opt (parser,
17997 /*typename_keyword_p=*/true,
17998 /*check_dependency_p=*/false,
17999 /*type_p=*/false,
18000 /*is_declaration=*/false);
18002 if (nested_name_specifier)
18004 tree name;
18006 identifier = cp_parser_identifier (parser);
18007 name = cp_parser_lookup_name (parser, identifier,
18008 enum_type,
18009 /*is_template=*/false,
18010 /*is_namespace=*/false,
18011 /*check_dependency=*/true,
18012 /*ambiguous_decls=*/NULL,
18013 input_location);
18014 if (name && name != error_mark_node)
18016 type = TREE_TYPE (name);
18017 if (TREE_CODE (type) == TYPENAME_TYPE)
18019 /* Are template enums allowed in ISO? */
18020 if (template_parm_scope_p ())
18021 pedwarn (type_start_token->location, OPT_Wpedantic,
18022 "%qD is an enumeration template", name);
18023 /* ignore a typename reference, for it will be solved by name
18024 in start_enum. */
18025 type = NULL_TREE;
18028 else if (nested_name_specifier == error_mark_node)
18029 /* We already issued an error. */;
18030 else
18032 error_at (type_start_token->location,
18033 "%qD does not name an enumeration in %qT",
18034 identifier, nested_name_specifier);
18035 nested_name_specifier = error_mark_node;
18038 else
18040 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18041 identifier = cp_parser_identifier (parser);
18042 else
18044 identifier = make_anon_name ();
18045 is_unnamed = true;
18046 if (scoped_enum_p)
18047 error_at (type_start_token->location,
18048 "unnamed scoped enum is not allowed");
18051 pop_deferring_access_checks ();
18053 /* Check for the `:' that denotes a specified underlying type in C++0x.
18054 Note that a ':' could also indicate a bitfield width, however. */
18055 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18057 cp_decl_specifier_seq type_specifiers;
18059 /* Consume the `:'. */
18060 cp_lexer_consume_token (parser->lexer);
18062 /* Parse the type-specifier-seq. */
18063 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
18064 /*is_trailing_return=*/false,
18065 &type_specifiers);
18067 /* At this point this is surely not elaborated type specifier. */
18068 if (!cp_parser_parse_definitely (parser))
18069 return NULL_TREE;
18071 if (cxx_dialect < cxx11)
18072 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
18074 has_underlying_type = true;
18076 /* If that didn't work, stop. */
18077 if (type_specifiers.type != error_mark_node)
18079 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
18080 /*initialized=*/0, NULL);
18081 if (underlying_type == error_mark_node
18082 || check_for_bare_parameter_packs (underlying_type))
18083 underlying_type = NULL_TREE;
18087 /* Look for the `{' but don't consume it yet. */
18088 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18090 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
18092 cp_parser_error (parser, "expected %<{%>");
18093 if (has_underlying_type)
18095 type = NULL_TREE;
18096 goto out;
18099 /* An opaque-enum-specifier must have a ';' here. */
18100 if ((scoped_enum_p || underlying_type)
18101 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18103 cp_parser_error (parser, "expected %<;%> or %<{%>");
18104 if (has_underlying_type)
18106 type = NULL_TREE;
18107 goto out;
18112 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18113 return NULL_TREE;
18115 if (nested_name_specifier)
18117 if (CLASS_TYPE_P (nested_name_specifier))
18119 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18120 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18121 push_scope (nested_name_specifier);
18123 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18125 push_nested_namespace (nested_name_specifier);
18129 /* Issue an error message if type-definitions are forbidden here. */
18130 if (!cp_parser_check_type_definition (parser))
18131 type = error_mark_node;
18132 else
18133 /* Create the new type. We do this before consuming the opening
18134 brace so the enum will be recorded as being on the line of its
18135 tag (or the 'enum' keyword, if there is no tag). */
18136 type = start_enum (identifier, type, underlying_type,
18137 attributes, scoped_enum_p, &is_new_type);
18139 /* If the next token is not '{' it is an opaque-enum-specifier or an
18140 elaborated-type-specifier. */
18141 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18143 timevar_push (TV_PARSE_ENUM);
18144 if (nested_name_specifier
18145 && nested_name_specifier != error_mark_node)
18147 /* The following catches invalid code such as:
18148 enum class S<int>::E { A, B, C }; */
18149 if (!processing_specialization
18150 && CLASS_TYPE_P (nested_name_specifier)
18151 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18152 error_at (type_start_token->location, "cannot add an enumerator "
18153 "list to a template instantiation");
18155 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18157 error_at (type_start_token->location,
18158 "%<%T::%E%> has not been declared",
18159 TYPE_CONTEXT (nested_name_specifier),
18160 nested_name_specifier);
18161 type = error_mark_node;
18163 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18164 && !CLASS_TYPE_P (nested_name_specifier))
18166 error_at (type_start_token->location, "nested name specifier "
18167 "%qT for enum declaration does not name a class "
18168 "or namespace", nested_name_specifier);
18169 type = error_mark_node;
18171 /* If that scope does not contain the scope in which the
18172 class was originally declared, the program is invalid. */
18173 else if (prev_scope && !is_ancestor (prev_scope,
18174 nested_name_specifier))
18176 if (at_namespace_scope_p ())
18177 error_at (type_start_token->location,
18178 "declaration of %qD in namespace %qD which does not "
18179 "enclose %qD",
18180 type, prev_scope, nested_name_specifier);
18181 else
18182 error_at (type_start_token->location,
18183 "declaration of %qD in %qD which does not "
18184 "enclose %qD",
18185 type, prev_scope, nested_name_specifier);
18186 type = error_mark_node;
18188 /* If that scope is the scope where the declaration is being placed
18189 the program is invalid. */
18190 else if (CLASS_TYPE_P (nested_name_specifier)
18191 && CLASS_TYPE_P (prev_scope)
18192 && same_type_p (nested_name_specifier, prev_scope))
18194 permerror (type_start_token->location,
18195 "extra qualification not allowed");
18196 nested_name_specifier = NULL_TREE;
18200 if (scoped_enum_p)
18201 begin_scope (sk_scoped_enum, type);
18203 /* Consume the opening brace. */
18204 matching_braces braces;
18205 braces.consume_open (parser);
18207 if (type == error_mark_node)
18208 ; /* Nothing to add */
18209 else if (OPAQUE_ENUM_P (type)
18210 || (cxx_dialect > cxx98 && processing_specialization))
18212 new_value_list = true;
18213 SET_OPAQUE_ENUM_P (type, false);
18214 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18216 else
18218 error_at (type_start_token->location,
18219 "multiple definition of %q#T", type);
18220 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18221 "previous definition here");
18222 type = error_mark_node;
18225 if (type == error_mark_node)
18226 cp_parser_skip_to_end_of_block_or_statement (parser);
18227 /* If the next token is not '}', then there are some enumerators. */
18228 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18230 if (is_unnamed && !scoped_enum_p)
18231 pedwarn (type_start_token->location, OPT_Wpedantic,
18232 "ISO C++ forbids empty unnamed enum");
18234 else
18235 cp_parser_enumerator_list (parser, type);
18237 /* Consume the final '}'. */
18238 braces.require_close (parser);
18240 if (scoped_enum_p)
18241 finish_scope ();
18242 timevar_pop (TV_PARSE_ENUM);
18244 else
18246 /* If a ';' follows, then it is an opaque-enum-specifier
18247 and additional restrictions apply. */
18248 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18250 if (is_unnamed)
18251 error_at (type_start_token->location,
18252 "opaque-enum-specifier without name");
18253 else if (nested_name_specifier)
18254 error_at (type_start_token->location,
18255 "opaque-enum-specifier must use a simple identifier");
18259 /* Look for trailing attributes to apply to this enumeration, and
18260 apply them if appropriate. */
18261 if (cp_parser_allow_gnu_extensions_p (parser))
18263 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18264 cplus_decl_attributes (&type,
18265 trailing_attr,
18266 (int) ATTR_FLAG_TYPE_IN_PLACE);
18269 /* Finish up the enumeration. */
18270 if (type != error_mark_node)
18272 if (new_value_list)
18273 finish_enum_value_list (type);
18274 if (is_new_type)
18275 finish_enum (type);
18278 if (nested_name_specifier)
18280 if (CLASS_TYPE_P (nested_name_specifier))
18282 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18283 pop_scope (nested_name_specifier);
18285 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18287 pop_nested_namespace (nested_name_specifier);
18290 out:
18291 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18292 return type;
18295 /* Parse an enumerator-list. The enumerators all have the indicated
18296 TYPE.
18298 enumerator-list:
18299 enumerator-definition
18300 enumerator-list , enumerator-definition */
18302 static void
18303 cp_parser_enumerator_list (cp_parser* parser, tree type)
18305 while (true)
18307 /* Parse an enumerator-definition. */
18308 cp_parser_enumerator_definition (parser, type);
18310 /* If the next token is not a ',', we've reached the end of
18311 the list. */
18312 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18313 break;
18314 /* Otherwise, consume the `,' and keep going. */
18315 cp_lexer_consume_token (parser->lexer);
18316 /* If the next token is a `}', there is a trailing comma. */
18317 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18319 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18320 pedwarn (input_location, OPT_Wpedantic,
18321 "comma at end of enumerator list");
18322 break;
18327 /* Parse an enumerator-definition. The enumerator has the indicated
18328 TYPE.
18330 enumerator-definition:
18331 enumerator
18332 enumerator = constant-expression
18334 enumerator:
18335 identifier
18337 GNU Extensions:
18339 enumerator-definition:
18340 enumerator attributes [opt]
18341 enumerator attributes [opt] = constant-expression */
18343 static void
18344 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18346 tree identifier;
18347 tree value;
18348 location_t loc;
18350 /* Save the input location because we are interested in the location
18351 of the identifier and not the location of the explicit value. */
18352 loc = cp_lexer_peek_token (parser->lexer)->location;
18354 /* Look for the identifier. */
18355 identifier = cp_parser_identifier (parser);
18356 if (identifier == error_mark_node)
18357 return;
18359 /* Parse any specified attributes. */
18360 tree attrs = cp_parser_attributes_opt (parser);
18362 /* If the next token is an '=', then there is an explicit value. */
18363 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18365 /* Consume the `=' token. */
18366 cp_lexer_consume_token (parser->lexer);
18367 /* Parse the value. */
18368 value = cp_parser_constant_expression (parser);
18370 else
18371 value = NULL_TREE;
18373 /* If we are processing a template, make sure the initializer of the
18374 enumerator doesn't contain any bare template parameter pack. */
18375 if (check_for_bare_parameter_packs (value))
18376 value = error_mark_node;
18378 /* Create the enumerator. */
18379 build_enumerator (identifier, value, type, attrs, loc);
18382 /* Parse a namespace-name.
18384 namespace-name:
18385 original-namespace-name
18386 namespace-alias
18388 Returns the NAMESPACE_DECL for the namespace. */
18390 static tree
18391 cp_parser_namespace_name (cp_parser* parser)
18393 tree identifier;
18394 tree namespace_decl;
18396 cp_token *token = cp_lexer_peek_token (parser->lexer);
18398 /* Get the name of the namespace. */
18399 identifier = cp_parser_identifier (parser);
18400 if (identifier == error_mark_node)
18401 return error_mark_node;
18403 /* Look up the identifier in the currently active scope. Look only
18404 for namespaces, due to:
18406 [basic.lookup.udir]
18408 When looking up a namespace-name in a using-directive or alias
18409 definition, only namespace names are considered.
18411 And:
18413 [basic.lookup.qual]
18415 During the lookup of a name preceding the :: scope resolution
18416 operator, object, function, and enumerator names are ignored.
18418 (Note that cp_parser_qualifying_entity only calls this
18419 function if the token after the name is the scope resolution
18420 operator.) */
18421 namespace_decl = cp_parser_lookup_name (parser, identifier,
18422 none_type,
18423 /*is_template=*/false,
18424 /*is_namespace=*/true,
18425 /*check_dependency=*/true,
18426 /*ambiguous_decls=*/NULL,
18427 token->location);
18428 /* If it's not a namespace, issue an error. */
18429 if (namespace_decl == error_mark_node
18430 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18432 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18433 error_at (token->location, "%qD is not a namespace-name", identifier);
18434 cp_parser_error (parser, "expected namespace-name");
18435 namespace_decl = error_mark_node;
18438 return namespace_decl;
18441 /* Parse a namespace-definition.
18443 namespace-definition:
18444 named-namespace-definition
18445 unnamed-namespace-definition
18447 named-namespace-definition:
18448 original-namespace-definition
18449 extension-namespace-definition
18451 original-namespace-definition:
18452 namespace identifier { namespace-body }
18454 extension-namespace-definition:
18455 namespace original-namespace-name { namespace-body }
18457 unnamed-namespace-definition:
18458 namespace { namespace-body } */
18460 static void
18461 cp_parser_namespace_definition (cp_parser* parser)
18463 tree identifier;
18464 int nested_definition_count = 0;
18466 cp_ensure_no_omp_declare_simd (parser);
18467 cp_ensure_no_oacc_routine (parser);
18469 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18471 if (is_inline)
18473 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18474 cp_lexer_consume_token (parser->lexer);
18477 /* Look for the `namespace' keyword. */
18478 cp_token* token
18479 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18481 /* Parse any specified attributes before the identifier. */
18482 tree attribs = cp_parser_attributes_opt (parser);
18484 for (;;)
18486 identifier = NULL_TREE;
18488 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18490 identifier = cp_parser_identifier (parser);
18492 /* Parse any attributes specified after the identifier. */
18493 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18497 break;
18499 if (!nested_definition_count && cxx_dialect < cxx1z)
18500 pedwarn (input_location, OPT_Wpedantic,
18501 "nested namespace definitions only available with "
18502 "-std=c++1z or -std=gnu++1z");
18504 /* Nested namespace names can create new namespaces (unlike
18505 other qualified-ids). */
18506 if (int count = identifier ? push_namespace (identifier) : 0)
18507 nested_definition_count += count;
18508 else
18509 cp_parser_error (parser, "nested namespace name required");
18510 cp_lexer_consume_token (parser->lexer);
18513 if (nested_definition_count && !identifier)
18514 cp_parser_error (parser, "namespace name required");
18516 if (nested_definition_count && attribs)
18517 error_at (token->location,
18518 "a nested namespace definition cannot have attributes");
18519 if (nested_definition_count && is_inline)
18520 error_at (token->location,
18521 "a nested namespace definition cannot be inline");
18523 /* Start the namespace. */
18524 nested_definition_count += push_namespace (identifier, is_inline);
18526 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18528 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18530 /* Look for the `{' to validate starting the namespace. */
18531 matching_braces braces;
18532 if (braces.require_open (parser))
18534 /* Parse the body of the namespace. */
18535 cp_parser_namespace_body (parser);
18537 /* Look for the final `}'. */
18538 braces.require_close (parser);
18541 if (has_visibility)
18542 pop_visibility (1);
18544 /* Pop the nested namespace definitions. */
18545 while (nested_definition_count--)
18546 pop_namespace ();
18549 /* Parse a namespace-body.
18551 namespace-body:
18552 declaration-seq [opt] */
18554 static void
18555 cp_parser_namespace_body (cp_parser* parser)
18557 cp_parser_declaration_seq_opt (parser);
18560 /* Parse a namespace-alias-definition.
18562 namespace-alias-definition:
18563 namespace identifier = qualified-namespace-specifier ; */
18565 static void
18566 cp_parser_namespace_alias_definition (cp_parser* parser)
18568 tree identifier;
18569 tree namespace_specifier;
18571 cp_token *token = cp_lexer_peek_token (parser->lexer);
18573 /* Look for the `namespace' keyword. */
18574 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18575 /* Look for the identifier. */
18576 identifier = cp_parser_identifier (parser);
18577 if (identifier == error_mark_node)
18578 return;
18579 /* Look for the `=' token. */
18580 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18581 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18583 error_at (token->location, "%<namespace%> definition is not allowed here");
18584 /* Skip the definition. */
18585 cp_lexer_consume_token (parser->lexer);
18586 if (cp_parser_skip_to_closing_brace (parser))
18587 cp_lexer_consume_token (parser->lexer);
18588 return;
18590 cp_parser_require (parser, CPP_EQ, RT_EQ);
18591 /* Look for the qualified-namespace-specifier. */
18592 namespace_specifier
18593 = cp_parser_qualified_namespace_specifier (parser);
18594 /* Look for the `;' token. */
18595 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18597 /* Register the alias in the symbol table. */
18598 do_namespace_alias (identifier, namespace_specifier);
18601 /* Parse a qualified-namespace-specifier.
18603 qualified-namespace-specifier:
18604 :: [opt] nested-name-specifier [opt] namespace-name
18606 Returns a NAMESPACE_DECL corresponding to the specified
18607 namespace. */
18609 static tree
18610 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18612 /* Look for the optional `::'. */
18613 cp_parser_global_scope_opt (parser,
18614 /*current_scope_valid_p=*/false);
18616 /* Look for the optional nested-name-specifier. */
18617 cp_parser_nested_name_specifier_opt (parser,
18618 /*typename_keyword_p=*/false,
18619 /*check_dependency_p=*/true,
18620 /*type_p=*/false,
18621 /*is_declaration=*/true);
18623 return cp_parser_namespace_name (parser);
18626 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18627 access declaration.
18629 using-declaration:
18630 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18631 using :: unqualified-id ;
18633 access-declaration:
18634 qualified-id ;
18638 static bool
18639 cp_parser_using_declaration (cp_parser* parser,
18640 bool access_declaration_p)
18642 cp_token *token;
18643 bool typename_p = false;
18644 bool global_scope_p;
18645 tree decl;
18646 tree identifier;
18647 tree qscope;
18648 int oldcount = errorcount;
18649 cp_token *diag_token = NULL;
18651 if (access_declaration_p)
18653 diag_token = cp_lexer_peek_token (parser->lexer);
18654 cp_parser_parse_tentatively (parser);
18656 else
18658 /* Look for the `using' keyword. */
18659 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18661 again:
18662 /* Peek at the next token. */
18663 token = cp_lexer_peek_token (parser->lexer);
18664 /* See if it's `typename'. */
18665 if (token->keyword == RID_TYPENAME)
18667 /* Remember that we've seen it. */
18668 typename_p = true;
18669 /* Consume the `typename' token. */
18670 cp_lexer_consume_token (parser->lexer);
18674 /* Look for the optional global scope qualification. */
18675 global_scope_p
18676 = (cp_parser_global_scope_opt (parser,
18677 /*current_scope_valid_p=*/false)
18678 != NULL_TREE);
18680 /* If we saw `typename', or didn't see `::', then there must be a
18681 nested-name-specifier present. */
18682 if (typename_p || !global_scope_p)
18684 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18685 /*check_dependency_p=*/true,
18686 /*type_p=*/false,
18687 /*is_declaration=*/true);
18688 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18690 cp_parser_skip_to_end_of_block_or_statement (parser);
18691 return false;
18694 /* Otherwise, we could be in either of the two productions. In that
18695 case, treat the nested-name-specifier as optional. */
18696 else
18697 qscope = cp_parser_nested_name_specifier_opt (parser,
18698 /*typename_keyword_p=*/false,
18699 /*check_dependency_p=*/true,
18700 /*type_p=*/false,
18701 /*is_declaration=*/true);
18702 if (!qscope)
18703 qscope = global_namespace;
18704 else if (UNSCOPED_ENUM_P (qscope))
18705 qscope = CP_TYPE_CONTEXT (qscope);
18707 if (access_declaration_p && cp_parser_error_occurred (parser))
18708 /* Something has already gone wrong; there's no need to parse
18709 further. Since an error has occurred, the return value of
18710 cp_parser_parse_definitely will be false, as required. */
18711 return cp_parser_parse_definitely (parser);
18713 token = cp_lexer_peek_token (parser->lexer);
18714 /* Parse the unqualified-id. */
18715 identifier = cp_parser_unqualified_id (parser,
18716 /*template_keyword_p=*/false,
18717 /*check_dependency_p=*/true,
18718 /*declarator_p=*/true,
18719 /*optional_p=*/false);
18721 if (access_declaration_p)
18723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18724 cp_parser_simulate_error (parser);
18725 if (!cp_parser_parse_definitely (parser))
18726 return false;
18728 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18730 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18731 if (cxx_dialect < cxx1z
18732 && !in_system_header_at (ell->location))
18733 pedwarn (ell->location, 0,
18734 "pack expansion in using-declaration only available "
18735 "with -std=c++1z or -std=gnu++1z");
18736 qscope = make_pack_expansion (qscope);
18739 /* The function we call to handle a using-declaration is different
18740 depending on what scope we are in. */
18741 if (qscope == error_mark_node || identifier == error_mark_node)
18743 else if (!identifier_p (identifier)
18744 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18745 /* [namespace.udecl]
18747 A using declaration shall not name a template-id. */
18748 error_at (token->location,
18749 "a template-id may not appear in a using-declaration");
18750 else
18752 if (at_class_scope_p ())
18754 /* Create the USING_DECL. */
18755 decl = do_class_using_decl (qscope, identifier);
18757 if (decl && typename_p)
18758 USING_DECL_TYPENAME_P (decl) = 1;
18760 if (check_for_bare_parameter_packs (decl))
18762 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18763 return false;
18765 else
18766 /* Add it to the list of members in this class. */
18767 finish_member_declaration (decl);
18769 else
18771 decl = cp_parser_lookup_name_simple (parser,
18772 identifier,
18773 token->location);
18774 if (decl == error_mark_node)
18775 cp_parser_name_lookup_error (parser, identifier,
18776 decl, NLE_NULL,
18777 token->location);
18778 else if (check_for_bare_parameter_packs (decl))
18780 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18781 return false;
18783 else if (!at_namespace_scope_p ())
18784 finish_local_using_decl (decl, qscope, identifier);
18785 else
18786 finish_namespace_using_decl (decl, qscope, identifier);
18790 if (!access_declaration_p
18791 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18793 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18794 if (cxx_dialect < cxx1z)
18795 pedwarn (comma->location, 0,
18796 "comma-separated list in using-declaration only available "
18797 "with -std=c++1z or -std=gnu++1z");
18798 goto again;
18801 /* Look for the final `;'. */
18802 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18804 if (access_declaration_p && errorcount == oldcount)
18805 warning_at (diag_token->location, OPT_Wdeprecated,
18806 "access declarations are deprecated "
18807 "in favour of using-declarations; "
18808 "suggestion: add the %<using%> keyword");
18810 return true;
18813 /* Parse an alias-declaration.
18815 alias-declaration:
18816 using identifier attribute-specifier-seq [opt] = type-id */
18818 static tree
18819 cp_parser_alias_declaration (cp_parser* parser)
18821 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18822 location_t id_location;
18823 cp_declarator *declarator;
18824 cp_decl_specifier_seq decl_specs;
18825 bool member_p;
18826 const char *saved_message = NULL;
18828 /* Look for the `using' keyword. */
18829 cp_token *using_token
18830 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18831 if (using_token == NULL)
18832 return error_mark_node;
18834 id_location = cp_lexer_peek_token (parser->lexer)->location;
18835 id = cp_parser_identifier (parser);
18836 if (id == error_mark_node)
18837 return error_mark_node;
18839 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18840 attributes = cp_parser_attributes_opt (parser);
18841 if (attributes == error_mark_node)
18842 return error_mark_node;
18844 cp_parser_require (parser, CPP_EQ, RT_EQ);
18846 if (cp_parser_error_occurred (parser))
18847 return error_mark_node;
18849 cp_parser_commit_to_tentative_parse (parser);
18851 /* Now we are going to parse the type-id of the declaration. */
18854 [dcl.type]/3 says:
18856 "A type-specifier-seq shall not define a class or enumeration
18857 unless it appears in the type-id of an alias-declaration (7.1.3) that
18858 is not the declaration of a template-declaration."
18860 In other words, if we currently are in an alias template, the
18861 type-id should not define a type.
18863 So let's set parser->type_definition_forbidden_message in that
18864 case; cp_parser_check_type_definition (called by
18865 cp_parser_class_specifier) will then emit an error if a type is
18866 defined in the type-id. */
18867 if (parser->num_template_parameter_lists)
18869 saved_message = parser->type_definition_forbidden_message;
18870 parser->type_definition_forbidden_message =
18871 G_("types may not be defined in alias template declarations");
18874 type = cp_parser_type_id (parser);
18876 /* Restore the error message if need be. */
18877 if (parser->num_template_parameter_lists)
18878 parser->type_definition_forbidden_message = saved_message;
18880 if (type == error_mark_node
18881 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18883 cp_parser_skip_to_end_of_block_or_statement (parser);
18884 return error_mark_node;
18887 /* A typedef-name can also be introduced by an alias-declaration. The
18888 identifier following the using keyword becomes a typedef-name. It has
18889 the same semantics as if it were introduced by the typedef
18890 specifier. In particular, it does not define a new type and it shall
18891 not appear in the type-id. */
18893 clear_decl_specs (&decl_specs);
18894 decl_specs.type = type;
18895 if (attributes != NULL_TREE)
18897 decl_specs.attributes = attributes;
18898 set_and_check_decl_spec_loc (&decl_specs,
18899 ds_attribute,
18900 attrs_token);
18902 set_and_check_decl_spec_loc (&decl_specs,
18903 ds_typedef,
18904 using_token);
18905 set_and_check_decl_spec_loc (&decl_specs,
18906 ds_alias,
18907 using_token);
18909 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18910 declarator->id_loc = id_location;
18912 member_p = at_class_scope_p ();
18913 if (member_p)
18914 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18915 NULL_TREE, attributes);
18916 else
18917 decl = start_decl (declarator, &decl_specs, 0,
18918 attributes, NULL_TREE, &pushed_scope);
18919 if (decl == error_mark_node)
18920 return decl;
18922 // Attach constraints to the alias declaration.
18923 if (flag_concepts && current_template_parms)
18925 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18926 tree constr = build_constraints (reqs, NULL_TREE);
18927 set_constraints (decl, constr);
18930 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18932 if (pushed_scope)
18933 pop_scope (pushed_scope);
18935 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18936 added into the symbol table; otherwise, return the TYPE_DECL. */
18937 if (DECL_LANG_SPECIFIC (decl)
18938 && DECL_TEMPLATE_INFO (decl)
18939 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18941 decl = DECL_TI_TEMPLATE (decl);
18942 if (member_p)
18943 check_member_template (decl);
18946 return decl;
18949 /* Parse a using-directive.
18951 using-directive:
18952 using namespace :: [opt] nested-name-specifier [opt]
18953 namespace-name ; */
18955 static void
18956 cp_parser_using_directive (cp_parser* parser)
18958 tree namespace_decl;
18959 tree attribs;
18961 /* Look for the `using' keyword. */
18962 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18963 /* And the `namespace' keyword. */
18964 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18965 /* Look for the optional `::' operator. */
18966 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18967 /* And the optional nested-name-specifier. */
18968 cp_parser_nested_name_specifier_opt (parser,
18969 /*typename_keyword_p=*/false,
18970 /*check_dependency_p=*/true,
18971 /*type_p=*/false,
18972 /*is_declaration=*/true);
18973 /* Get the namespace being used. */
18974 namespace_decl = cp_parser_namespace_name (parser);
18975 /* And any specified attributes. */
18976 attribs = cp_parser_attributes_opt (parser);
18978 /* Update the symbol table. */
18979 if (namespace_bindings_p ())
18980 finish_namespace_using_directive (namespace_decl, attribs);
18981 else
18982 finish_local_using_directive (namespace_decl, attribs);
18984 /* Look for the final `;'. */
18985 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18988 /* Parse an asm-definition.
18990 asm-definition:
18991 asm ( string-literal ) ;
18993 GNU Extension:
18995 asm-definition:
18996 asm volatile [opt] ( string-literal ) ;
18997 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18998 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18999 : asm-operand-list [opt] ) ;
19000 asm volatile [opt] ( string-literal : asm-operand-list [opt]
19001 : asm-operand-list [opt]
19002 : asm-clobber-list [opt] ) ;
19003 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
19004 : asm-clobber-list [opt]
19005 : asm-goto-list ) ; */
19007 static void
19008 cp_parser_asm_definition (cp_parser* parser)
19010 tree string;
19011 tree outputs = NULL_TREE;
19012 tree inputs = NULL_TREE;
19013 tree clobbers = NULL_TREE;
19014 tree labels = NULL_TREE;
19015 tree asm_stmt;
19016 bool volatile_p = false;
19017 bool extended_p = false;
19018 bool invalid_inputs_p = false;
19019 bool invalid_outputs_p = false;
19020 bool goto_p = false;
19021 required_token missing = RT_NONE;
19023 /* Look for the `asm' keyword. */
19024 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
19026 if (parser->in_function_body
19027 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
19029 error ("%<asm%> in %<constexpr%> function");
19030 cp_function_chain->invalid_constexpr = true;
19033 /* See if the next token is `volatile'. */
19034 if (cp_parser_allow_gnu_extensions_p (parser)
19035 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
19037 /* Remember that we saw the `volatile' keyword. */
19038 volatile_p = true;
19039 /* Consume the token. */
19040 cp_lexer_consume_token (parser->lexer);
19042 if (cp_parser_allow_gnu_extensions_p (parser)
19043 && parser->in_function_body
19044 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
19046 /* Remember that we saw the `goto' keyword. */
19047 goto_p = true;
19048 /* Consume the token. */
19049 cp_lexer_consume_token (parser->lexer);
19051 /* Look for the opening `('. */
19052 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19053 return;
19054 /* Look for the string. */
19055 string = cp_parser_string_literal (parser, false, false);
19056 if (string == error_mark_node)
19058 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19059 /*consume_paren=*/true);
19060 return;
19063 /* If we're allowing GNU extensions, check for the extended assembly
19064 syntax. Unfortunately, the `:' tokens need not be separated by
19065 a space in C, and so, for compatibility, we tolerate that here
19066 too. Doing that means that we have to treat the `::' operator as
19067 two `:' tokens. */
19068 if (cp_parser_allow_gnu_extensions_p (parser)
19069 && parser->in_function_body
19070 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
19071 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
19073 bool inputs_p = false;
19074 bool clobbers_p = false;
19075 bool labels_p = false;
19077 /* The extended syntax was used. */
19078 extended_p = true;
19080 /* Look for outputs. */
19081 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19083 /* Consume the `:'. */
19084 cp_lexer_consume_token (parser->lexer);
19085 /* Parse the output-operands. */
19086 if (cp_lexer_next_token_is_not (parser->lexer,
19087 CPP_COLON)
19088 && cp_lexer_next_token_is_not (parser->lexer,
19089 CPP_SCOPE)
19090 && cp_lexer_next_token_is_not (parser->lexer,
19091 CPP_CLOSE_PAREN)
19092 && !goto_p)
19094 outputs = cp_parser_asm_operand_list (parser);
19095 if (outputs == error_mark_node)
19096 invalid_outputs_p = true;
19099 /* If the next token is `::', there are no outputs, and the
19100 next token is the beginning of the inputs. */
19101 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19102 /* The inputs are coming next. */
19103 inputs_p = true;
19105 /* Look for inputs. */
19106 if (inputs_p
19107 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19109 /* Consume the `:' or `::'. */
19110 cp_lexer_consume_token (parser->lexer);
19111 /* Parse the output-operands. */
19112 if (cp_lexer_next_token_is_not (parser->lexer,
19113 CPP_COLON)
19114 && cp_lexer_next_token_is_not (parser->lexer,
19115 CPP_SCOPE)
19116 && cp_lexer_next_token_is_not (parser->lexer,
19117 CPP_CLOSE_PAREN))
19119 inputs = cp_parser_asm_operand_list (parser);
19120 if (inputs == error_mark_node)
19121 invalid_inputs_p = true;
19124 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19125 /* The clobbers are coming next. */
19126 clobbers_p = true;
19128 /* Look for clobbers. */
19129 if (clobbers_p
19130 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19132 clobbers_p = true;
19133 /* Consume the `:' or `::'. */
19134 cp_lexer_consume_token (parser->lexer);
19135 /* Parse the clobbers. */
19136 if (cp_lexer_next_token_is_not (parser->lexer,
19137 CPP_COLON)
19138 && cp_lexer_next_token_is_not (parser->lexer,
19139 CPP_CLOSE_PAREN))
19140 clobbers = cp_parser_asm_clobber_list (parser);
19142 else if (goto_p
19143 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19144 /* The labels are coming next. */
19145 labels_p = true;
19147 /* Look for labels. */
19148 if (labels_p
19149 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19151 labels_p = true;
19152 /* Consume the `:' or `::'. */
19153 cp_lexer_consume_token (parser->lexer);
19154 /* Parse the labels. */
19155 labels = cp_parser_asm_label_list (parser);
19158 if (goto_p && !labels_p)
19159 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19161 else if (goto_p)
19162 missing = RT_COLON_SCOPE;
19164 /* Look for the closing `)'. */
19165 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19166 missing ? missing : RT_CLOSE_PAREN))
19167 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19168 /*consume_paren=*/true);
19169 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19171 if (!invalid_inputs_p && !invalid_outputs_p)
19173 /* Create the ASM_EXPR. */
19174 if (parser->in_function_body)
19176 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19177 inputs, clobbers, labels);
19178 /* If the extended syntax was not used, mark the ASM_EXPR. */
19179 if (!extended_p)
19181 tree temp = asm_stmt;
19182 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19183 temp = TREE_OPERAND (temp, 0);
19185 ASM_INPUT_P (temp) = 1;
19188 else
19189 symtab->finalize_toplevel_asm (string);
19193 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19194 type that comes from the decl-specifier-seq. */
19196 static tree
19197 strip_declarator_types (tree type, cp_declarator *declarator)
19199 for (cp_declarator *d = declarator; d;)
19200 switch (d->kind)
19202 case cdk_id:
19203 case cdk_decomp:
19204 case cdk_error:
19205 d = NULL;
19206 break;
19208 default:
19209 if (TYPE_PTRMEMFUNC_P (type))
19210 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19211 type = TREE_TYPE (type);
19212 d = d->declarator;
19213 break;
19216 return type;
19219 /* Declarators [gram.dcl.decl] */
19221 /* Parse an init-declarator.
19223 init-declarator:
19224 declarator initializer [opt]
19226 GNU Extension:
19228 init-declarator:
19229 declarator asm-specification [opt] attributes [opt] initializer [opt]
19231 function-definition:
19232 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19233 function-body
19234 decl-specifier-seq [opt] declarator function-try-block
19236 GNU Extension:
19238 function-definition:
19239 __extension__ function-definition
19241 TM Extension:
19243 function-definition:
19244 decl-specifier-seq [opt] declarator function-transaction-block
19246 The DECL_SPECIFIERS apply to this declarator. Returns a
19247 representation of the entity declared. If MEMBER_P is TRUE, then
19248 this declarator appears in a class scope. The new DECL created by
19249 this declarator is returned.
19251 The CHECKS are access checks that should be performed once we know
19252 what entity is being declared (and, therefore, what classes have
19253 befriended it).
19255 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19256 for a function-definition here as well. If the declarator is a
19257 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19258 be TRUE upon return. By that point, the function-definition will
19259 have been completely parsed.
19261 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19262 is FALSE.
19264 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19265 parsed declaration if it is an uninitialized single declarator not followed
19266 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19267 if present, will not be consumed. If returned, this declarator will be
19268 created with SD_INITIALIZED but will not call cp_finish_decl.
19270 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19271 and there is an initializer, the pointed location_t is set to the
19272 location of the '=' or `(', or '{' in C++11 token introducing the
19273 initializer. */
19275 static tree
19276 cp_parser_init_declarator (cp_parser* parser,
19277 cp_decl_specifier_seq *decl_specifiers,
19278 vec<deferred_access_check, va_gc> *checks,
19279 bool function_definition_allowed_p,
19280 bool member_p,
19281 int declares_class_or_enum,
19282 bool* function_definition_p,
19283 tree* maybe_range_for_decl,
19284 location_t* init_loc,
19285 tree* auto_result)
19287 cp_token *token = NULL, *asm_spec_start_token = NULL,
19288 *attributes_start_token = NULL;
19289 cp_declarator *declarator;
19290 tree prefix_attributes;
19291 tree attributes = NULL;
19292 tree asm_specification;
19293 tree initializer;
19294 tree decl = NULL_TREE;
19295 tree scope;
19296 int is_initialized;
19297 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19298 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19299 "(...)". */
19300 enum cpp_ttype initialization_kind;
19301 bool is_direct_init = false;
19302 bool is_non_constant_init;
19303 int ctor_dtor_or_conv_p;
19304 bool friend_p = cp_parser_friend_p (decl_specifiers);
19305 tree pushed_scope = NULL_TREE;
19306 bool range_for_decl_p = false;
19307 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19308 location_t tmp_init_loc = UNKNOWN_LOCATION;
19310 /* Gather the attributes that were provided with the
19311 decl-specifiers. */
19312 prefix_attributes = decl_specifiers->attributes;
19314 /* Assume that this is not the declarator for a function
19315 definition. */
19316 if (function_definition_p)
19317 *function_definition_p = false;
19319 /* Default arguments are only permitted for function parameters. */
19320 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19321 parser->default_arg_ok_p = false;
19323 /* Defer access checks while parsing the declarator; we cannot know
19324 what names are accessible until we know what is being
19325 declared. */
19326 resume_deferring_access_checks ();
19328 token = cp_lexer_peek_token (parser->lexer);
19330 /* Parse the declarator. */
19331 declarator
19332 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19333 &ctor_dtor_or_conv_p,
19334 /*parenthesized_p=*/NULL,
19335 member_p, friend_p);
19336 /* Gather up the deferred checks. */
19337 stop_deferring_access_checks ();
19339 parser->default_arg_ok_p = saved_default_arg_ok_p;
19341 /* If the DECLARATOR was erroneous, there's no need to go
19342 further. */
19343 if (declarator == cp_error_declarator)
19344 return error_mark_node;
19346 /* Check that the number of template-parameter-lists is OK. */
19347 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19348 token->location))
19349 return error_mark_node;
19351 if (declares_class_or_enum & 2)
19352 cp_parser_check_for_definition_in_return_type (declarator,
19353 decl_specifiers->type,
19354 decl_specifiers->locations[ds_type_spec]);
19356 /* Figure out what scope the entity declared by the DECLARATOR is
19357 located in. `grokdeclarator' sometimes changes the scope, so
19358 we compute it now. */
19359 scope = get_scope_of_declarator (declarator);
19361 /* Perform any lookups in the declared type which were thought to be
19362 dependent, but are not in the scope of the declarator. */
19363 decl_specifiers->type
19364 = maybe_update_decl_type (decl_specifiers->type, scope);
19366 /* If we're allowing GNU extensions, look for an
19367 asm-specification. */
19368 if (cp_parser_allow_gnu_extensions_p (parser))
19370 /* Look for an asm-specification. */
19371 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19372 asm_specification = cp_parser_asm_specification_opt (parser);
19374 else
19375 asm_specification = NULL_TREE;
19377 /* Look for attributes. */
19378 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19379 attributes = cp_parser_attributes_opt (parser);
19381 /* Peek at the next token. */
19382 token = cp_lexer_peek_token (parser->lexer);
19384 bool bogus_implicit_tmpl = false;
19386 if (function_declarator_p (declarator))
19388 /* Handle C++17 deduction guides. */
19389 if (!decl_specifiers->type
19390 && ctor_dtor_or_conv_p <= 0
19391 && cxx_dialect >= cxx1z)
19393 cp_declarator *id = get_id_declarator (declarator);
19394 tree name = id->u.id.unqualified_name;
19395 parser->scope = id->u.id.qualifying_scope;
19396 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19397 if (tmpl
19398 && (DECL_CLASS_TEMPLATE_P (tmpl)
19399 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19401 id->u.id.unqualified_name = dguide_name (tmpl);
19402 id->u.id.sfk = sfk_deduction_guide;
19403 ctor_dtor_or_conv_p = 1;
19407 /* Check to see if the token indicates the start of a
19408 function-definition. */
19409 if (cp_parser_token_starts_function_definition_p (token))
19411 if (!function_definition_allowed_p)
19413 /* If a function-definition should not appear here, issue an
19414 error message. */
19415 cp_parser_error (parser,
19416 "a function-definition is not allowed here");
19417 return error_mark_node;
19420 location_t func_brace_location
19421 = cp_lexer_peek_token (parser->lexer)->location;
19423 /* Neither attributes nor an asm-specification are allowed
19424 on a function-definition. */
19425 if (asm_specification)
19426 error_at (asm_spec_start_token->location,
19427 "an asm-specification is not allowed "
19428 "on a function-definition");
19429 if (attributes)
19430 error_at (attributes_start_token->location,
19431 "attributes are not allowed "
19432 "on a function-definition");
19433 /* This is a function-definition. */
19434 *function_definition_p = true;
19436 /* Parse the function definition. */
19437 if (member_p)
19438 decl = cp_parser_save_member_function_body (parser,
19439 decl_specifiers,
19440 declarator,
19441 prefix_attributes);
19442 else
19443 decl =
19444 (cp_parser_function_definition_from_specifiers_and_declarator
19445 (parser, decl_specifiers, prefix_attributes, declarator));
19447 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19449 /* This is where the prologue starts... */
19450 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19451 = func_brace_location;
19454 return decl;
19457 else if (parser->fully_implicit_function_template_p)
19459 /* A non-template declaration involving a function parameter list
19460 containing an implicit template parameter will be made into a
19461 template. If the resulting declaration is not going to be an
19462 actual function then finish the template scope here to prevent it.
19463 An error message will be issued once we have a decl to talk about.
19465 FIXME probably we should do type deduction rather than create an
19466 implicit template, but the standard currently doesn't allow it. */
19467 bogus_implicit_tmpl = true;
19468 finish_fully_implicit_template (parser, NULL_TREE);
19471 /* [dcl.dcl]
19473 Only in function declarations for constructors, destructors, type
19474 conversions, and deduction guides can the decl-specifier-seq be omitted.
19476 We explicitly postpone this check past the point where we handle
19477 function-definitions because we tolerate function-definitions
19478 that are missing their return types in some modes. */
19479 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19481 cp_parser_error (parser,
19482 "expected constructor, destructor, or type conversion");
19483 return error_mark_node;
19486 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19487 if (token->type == CPP_EQ
19488 || token->type == CPP_OPEN_PAREN
19489 || token->type == CPP_OPEN_BRACE)
19491 is_initialized = SD_INITIALIZED;
19492 initialization_kind = token->type;
19493 if (maybe_range_for_decl)
19494 *maybe_range_for_decl = error_mark_node;
19495 tmp_init_loc = token->location;
19496 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19497 *init_loc = tmp_init_loc;
19499 if (token->type == CPP_EQ
19500 && function_declarator_p (declarator))
19502 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19503 if (t2->keyword == RID_DEFAULT)
19504 is_initialized = SD_DEFAULTED;
19505 else if (t2->keyword == RID_DELETE)
19506 is_initialized = SD_DELETED;
19509 else
19511 /* If the init-declarator isn't initialized and isn't followed by a
19512 `,' or `;', it's not a valid init-declarator. */
19513 if (token->type != CPP_COMMA
19514 && token->type != CPP_SEMICOLON)
19516 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19517 range_for_decl_p = true;
19518 else
19520 if (!maybe_range_for_decl)
19521 cp_parser_error (parser, "expected initializer");
19522 return error_mark_node;
19525 is_initialized = SD_UNINITIALIZED;
19526 initialization_kind = CPP_EOF;
19529 /* Because start_decl has side-effects, we should only call it if we
19530 know we're going ahead. By this point, we know that we cannot
19531 possibly be looking at any other construct. */
19532 cp_parser_commit_to_tentative_parse (parser);
19534 /* Enter the newly declared entry in the symbol table. If we're
19535 processing a declaration in a class-specifier, we wait until
19536 after processing the initializer. */
19537 if (!member_p)
19539 if (parser->in_unbraced_linkage_specification_p)
19540 decl_specifiers->storage_class = sc_extern;
19541 decl = start_decl (declarator, decl_specifiers,
19542 range_for_decl_p? SD_INITIALIZED : is_initialized,
19543 attributes, prefix_attributes, &pushed_scope);
19544 cp_finalize_omp_declare_simd (parser, decl);
19545 cp_finalize_oacc_routine (parser, decl, false);
19546 /* Adjust location of decl if declarator->id_loc is more appropriate:
19547 set, and decl wasn't merged with another decl, in which case its
19548 location would be different from input_location, and more accurate. */
19549 if (DECL_P (decl)
19550 && declarator->id_loc != UNKNOWN_LOCATION
19551 && DECL_SOURCE_LOCATION (decl) == input_location)
19552 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19554 else if (scope)
19555 /* Enter the SCOPE. That way unqualified names appearing in the
19556 initializer will be looked up in SCOPE. */
19557 pushed_scope = push_scope (scope);
19559 /* Perform deferred access control checks, now that we know in which
19560 SCOPE the declared entity resides. */
19561 if (!member_p && decl)
19563 tree saved_current_function_decl = NULL_TREE;
19565 /* If the entity being declared is a function, pretend that we
19566 are in its scope. If it is a `friend', it may have access to
19567 things that would not otherwise be accessible. */
19568 if (TREE_CODE (decl) == FUNCTION_DECL)
19570 saved_current_function_decl = current_function_decl;
19571 current_function_decl = decl;
19574 /* Perform access checks for template parameters. */
19575 cp_parser_perform_template_parameter_access_checks (checks);
19577 /* Perform the access control checks for the declarator and the
19578 decl-specifiers. */
19579 perform_deferred_access_checks (tf_warning_or_error);
19581 /* Restore the saved value. */
19582 if (TREE_CODE (decl) == FUNCTION_DECL)
19583 current_function_decl = saved_current_function_decl;
19586 /* Parse the initializer. */
19587 initializer = NULL_TREE;
19588 is_direct_init = false;
19589 is_non_constant_init = true;
19590 if (is_initialized)
19592 if (function_declarator_p (declarator))
19594 if (initialization_kind == CPP_EQ)
19595 initializer = cp_parser_pure_specifier (parser);
19596 else
19598 /* If the declaration was erroneous, we don't really
19599 know what the user intended, so just silently
19600 consume the initializer. */
19601 if (decl != error_mark_node)
19602 error_at (tmp_init_loc, "initializer provided for function");
19603 cp_parser_skip_to_closing_parenthesis (parser,
19604 /*recovering=*/true,
19605 /*or_comma=*/false,
19606 /*consume_paren=*/true);
19609 else
19611 /* We want to record the extra mangling scope for in-class
19612 initializers of class members and initializers of static data
19613 member templates. The former involves deferring
19614 parsing of the initializer until end of class as with default
19615 arguments. So right here we only handle the latter. */
19616 if (!member_p && processing_template_decl)
19617 start_lambda_scope (decl);
19618 initializer = cp_parser_initializer (parser,
19619 &is_direct_init,
19620 &is_non_constant_init);
19621 if (!member_p && processing_template_decl)
19622 finish_lambda_scope ();
19623 if (initializer == error_mark_node)
19624 cp_parser_skip_to_end_of_statement (parser);
19628 /* The old parser allows attributes to appear after a parenthesized
19629 initializer. Mark Mitchell proposed removing this functionality
19630 on the GCC mailing lists on 2002-08-13. This parser accepts the
19631 attributes -- but ignores them. */
19632 if (cp_parser_allow_gnu_extensions_p (parser)
19633 && initialization_kind == CPP_OPEN_PAREN)
19634 if (cp_parser_attributes_opt (parser))
19635 warning (OPT_Wattributes,
19636 "attributes after parenthesized initializer ignored");
19638 /* And now complain about a non-function implicit template. */
19639 if (bogus_implicit_tmpl && decl != error_mark_node)
19640 error_at (DECL_SOURCE_LOCATION (decl),
19641 "non-function %qD declared as implicit template", decl);
19643 /* For an in-class declaration, use `grokfield' to create the
19644 declaration. */
19645 if (member_p)
19647 if (pushed_scope)
19649 pop_scope (pushed_scope);
19650 pushed_scope = NULL_TREE;
19652 decl = grokfield (declarator, decl_specifiers,
19653 initializer, !is_non_constant_init,
19654 /*asmspec=*/NULL_TREE,
19655 chainon (attributes, prefix_attributes));
19656 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19657 cp_parser_save_default_args (parser, decl);
19658 cp_finalize_omp_declare_simd (parser, decl);
19659 cp_finalize_oacc_routine (parser, decl, false);
19662 /* Finish processing the declaration. But, skip member
19663 declarations. */
19664 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19666 cp_finish_decl (decl,
19667 initializer, !is_non_constant_init,
19668 asm_specification,
19669 /* If the initializer is in parentheses, then this is
19670 a direct-initialization, which means that an
19671 `explicit' constructor is OK. Otherwise, an
19672 `explicit' constructor cannot be used. */
19673 ((is_direct_init || !is_initialized)
19674 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19676 else if ((cxx_dialect != cxx98) && friend_p
19677 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19678 /* Core issue #226 (C++0x only): A default template-argument
19679 shall not be specified in a friend class template
19680 declaration. */
19681 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19682 /*is_partial=*/false, /*is_friend_decl=*/1);
19684 if (!friend_p && pushed_scope)
19685 pop_scope (pushed_scope);
19687 if (function_declarator_p (declarator)
19688 && parser->fully_implicit_function_template_p)
19690 if (member_p)
19691 decl = finish_fully_implicit_template (parser, decl);
19692 else
19693 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19696 if (auto_result && is_initialized && decl_specifiers->type
19697 && type_uses_auto (decl_specifiers->type))
19698 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19700 return decl;
19703 /* Parse a declarator.
19705 declarator:
19706 direct-declarator
19707 ptr-operator declarator
19709 abstract-declarator:
19710 ptr-operator abstract-declarator [opt]
19711 direct-abstract-declarator
19713 GNU Extensions:
19715 declarator:
19716 attributes [opt] direct-declarator
19717 attributes [opt] ptr-operator declarator
19719 abstract-declarator:
19720 attributes [opt] ptr-operator abstract-declarator [opt]
19721 attributes [opt] direct-abstract-declarator
19723 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19724 detect constructors, destructors, deduction guides, or conversion operators.
19725 It is set to -1 if the declarator is a name, and +1 if it is a
19726 function. Otherwise it is set to zero. Usually you just want to
19727 test for >0, but internally the negative value is used.
19729 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19730 a decl-specifier-seq unless it declares a constructor, destructor,
19731 or conversion. It might seem that we could check this condition in
19732 semantic analysis, rather than parsing, but that makes it difficult
19733 to handle something like `f()'. We want to notice that there are
19734 no decl-specifiers, and therefore realize that this is an
19735 expression, not a declaration.)
19737 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19738 the declarator is a direct-declarator of the form "(...)".
19740 MEMBER_P is true iff this declarator is a member-declarator.
19742 FRIEND_P is true iff this declarator is a friend. */
19744 static cp_declarator *
19745 cp_parser_declarator (cp_parser* parser,
19746 cp_parser_declarator_kind dcl_kind,
19747 int* ctor_dtor_or_conv_p,
19748 bool* parenthesized_p,
19749 bool member_p, bool friend_p)
19751 cp_declarator *declarator;
19752 enum tree_code code;
19753 cp_cv_quals cv_quals;
19754 tree class_type;
19755 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19757 /* Assume this is not a constructor, destructor, or type-conversion
19758 operator. */
19759 if (ctor_dtor_or_conv_p)
19760 *ctor_dtor_or_conv_p = 0;
19762 if (cp_parser_allow_gnu_extensions_p (parser))
19763 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19765 /* Check for the ptr-operator production. */
19766 cp_parser_parse_tentatively (parser);
19767 /* Parse the ptr-operator. */
19768 code = cp_parser_ptr_operator (parser,
19769 &class_type,
19770 &cv_quals,
19771 &std_attributes);
19773 /* If that worked, then we have a ptr-operator. */
19774 if (cp_parser_parse_definitely (parser))
19776 /* If a ptr-operator was found, then this declarator was not
19777 parenthesized. */
19778 if (parenthesized_p)
19779 *parenthesized_p = true;
19780 /* The dependent declarator is optional if we are parsing an
19781 abstract-declarator. */
19782 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19783 cp_parser_parse_tentatively (parser);
19785 /* Parse the dependent declarator. */
19786 declarator = cp_parser_declarator (parser, dcl_kind,
19787 /*ctor_dtor_or_conv_p=*/NULL,
19788 /*parenthesized_p=*/NULL,
19789 /*member_p=*/false,
19790 friend_p);
19792 /* If we are parsing an abstract-declarator, we must handle the
19793 case where the dependent declarator is absent. */
19794 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19795 && !cp_parser_parse_definitely (parser))
19796 declarator = NULL;
19798 declarator = cp_parser_make_indirect_declarator
19799 (code, class_type, cv_quals, declarator, std_attributes);
19801 /* Everything else is a direct-declarator. */
19802 else
19804 if (parenthesized_p)
19805 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19806 CPP_OPEN_PAREN);
19807 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19808 ctor_dtor_or_conv_p,
19809 member_p, friend_p);
19812 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19813 declarator->attributes = gnu_attributes;
19814 return declarator;
19817 /* Parse a direct-declarator or direct-abstract-declarator.
19819 direct-declarator:
19820 declarator-id
19821 direct-declarator ( parameter-declaration-clause )
19822 cv-qualifier-seq [opt]
19823 ref-qualifier [opt]
19824 exception-specification [opt]
19825 direct-declarator [ constant-expression [opt] ]
19826 ( declarator )
19828 direct-abstract-declarator:
19829 direct-abstract-declarator [opt]
19830 ( parameter-declaration-clause )
19831 cv-qualifier-seq [opt]
19832 ref-qualifier [opt]
19833 exception-specification [opt]
19834 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19835 ( abstract-declarator )
19837 Returns a representation of the declarator. DCL_KIND is
19838 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19839 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19840 we are parsing a direct-declarator. It is
19841 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19842 of ambiguity we prefer an abstract declarator, as per
19843 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19844 as for cp_parser_declarator. */
19846 static cp_declarator *
19847 cp_parser_direct_declarator (cp_parser* parser,
19848 cp_parser_declarator_kind dcl_kind,
19849 int* ctor_dtor_or_conv_p,
19850 bool member_p, bool friend_p)
19852 cp_token *token;
19853 cp_declarator *declarator = NULL;
19854 tree scope = NULL_TREE;
19855 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19856 bool saved_in_declarator_p = parser->in_declarator_p;
19857 bool first = true;
19858 tree pushed_scope = NULL_TREE;
19860 while (true)
19862 /* Peek at the next token. */
19863 token = cp_lexer_peek_token (parser->lexer);
19864 if (token->type == CPP_OPEN_PAREN)
19866 /* This is either a parameter-declaration-clause, or a
19867 parenthesized declarator. When we know we are parsing a
19868 named declarator, it must be a parenthesized declarator
19869 if FIRST is true. For instance, `(int)' is a
19870 parameter-declaration-clause, with an omitted
19871 direct-abstract-declarator. But `((*))', is a
19872 parenthesized abstract declarator. Finally, when T is a
19873 template parameter `(T)' is a
19874 parameter-declaration-clause, and not a parenthesized
19875 named declarator.
19877 We first try and parse a parameter-declaration-clause,
19878 and then try a nested declarator (if FIRST is true).
19880 It is not an error for it not to be a
19881 parameter-declaration-clause, even when FIRST is
19882 false. Consider,
19884 int i (int);
19885 int i (3);
19887 The first is the declaration of a function while the
19888 second is the definition of a variable, including its
19889 initializer.
19891 Having seen only the parenthesis, we cannot know which of
19892 these two alternatives should be selected. Even more
19893 complex are examples like:
19895 int i (int (a));
19896 int i (int (3));
19898 The former is a function-declaration; the latter is a
19899 variable initialization.
19901 Thus again, we try a parameter-declaration-clause, and if
19902 that fails, we back out and return. */
19904 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19906 tree params;
19907 bool is_declarator = false;
19909 /* In a member-declarator, the only valid interpretation
19910 of a parenthesis is the start of a
19911 parameter-declaration-clause. (It is invalid to
19912 initialize a static data member with a parenthesized
19913 initializer; only the "=" form of initialization is
19914 permitted.) */
19915 if (!member_p)
19916 cp_parser_parse_tentatively (parser);
19918 /* Consume the `('. */
19919 matching_parens parens;
19920 parens.consume_open (parser);
19921 if (first)
19923 /* If this is going to be an abstract declarator, we're
19924 in a declarator and we can't have default args. */
19925 parser->default_arg_ok_p = false;
19926 parser->in_declarator_p = true;
19929 begin_scope (sk_function_parms, NULL_TREE);
19931 /* Parse the parameter-declaration-clause. */
19932 params = cp_parser_parameter_declaration_clause (parser);
19934 /* Consume the `)'. */
19935 parens.require_close (parser);
19937 /* If all went well, parse the cv-qualifier-seq,
19938 ref-qualifier and the exception-specification. */
19939 if (member_p || cp_parser_parse_definitely (parser))
19941 cp_cv_quals cv_quals;
19942 cp_virt_specifiers virt_specifiers;
19943 cp_ref_qualifier ref_qual;
19944 tree exception_specification;
19945 tree late_return;
19946 tree attrs;
19947 bool memfn = (member_p || (pushed_scope
19948 && CLASS_TYPE_P (pushed_scope)));
19950 is_declarator = true;
19952 if (ctor_dtor_or_conv_p)
19953 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19954 first = false;
19956 /* Parse the cv-qualifier-seq. */
19957 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19958 /* Parse the ref-qualifier. */
19959 ref_qual = cp_parser_ref_qualifier_opt (parser);
19960 /* Parse the tx-qualifier. */
19961 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19962 /* And the exception-specification. */
19963 exception_specification
19964 = cp_parser_exception_specification_opt (parser);
19966 attrs = cp_parser_std_attribute_spec_seq (parser);
19968 /* In here, we handle cases where attribute is used after
19969 the function declaration. For example:
19970 void func (int x) __attribute__((vector(..))); */
19971 tree gnu_attrs = NULL_TREE;
19972 if (flag_cilkplus
19973 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19975 cp_parser_parse_tentatively (parser);
19976 tree attr = cp_parser_gnu_attributes_opt (parser);
19977 if (cp_lexer_next_token_is_not (parser->lexer,
19978 CPP_SEMICOLON)
19979 && cp_lexer_next_token_is_not (parser->lexer,
19980 CPP_OPEN_BRACE))
19981 cp_parser_abort_tentative_parse (parser);
19982 else if (!cp_parser_parse_definitely (parser))
19984 else
19985 gnu_attrs = attr;
19987 tree requires_clause = NULL_TREE;
19988 late_return = (cp_parser_late_return_type_opt
19989 (parser, declarator, requires_clause,
19990 memfn ? cv_quals : -1));
19992 /* Parse the virt-specifier-seq. */
19993 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19995 /* Create the function-declarator. */
19996 declarator = make_call_declarator (declarator,
19997 params,
19998 cv_quals,
19999 virt_specifiers,
20000 ref_qual,
20001 tx_qual,
20002 exception_specification,
20003 late_return,
20004 requires_clause);
20005 declarator->std_attributes = attrs;
20006 declarator->attributes = gnu_attrs;
20007 /* Any subsequent parameter lists are to do with
20008 return type, so are not those of the declared
20009 function. */
20010 parser->default_arg_ok_p = false;
20013 /* Remove the function parms from scope. */
20014 pop_bindings_and_leave_scope ();
20016 if (is_declarator)
20017 /* Repeat the main loop. */
20018 continue;
20021 /* If this is the first, we can try a parenthesized
20022 declarator. */
20023 if (first)
20025 bool saved_in_type_id_in_expr_p;
20027 parser->default_arg_ok_p = saved_default_arg_ok_p;
20028 parser->in_declarator_p = saved_in_declarator_p;
20030 /* Consume the `('. */
20031 matching_parens parens;
20032 parens.consume_open (parser);
20033 /* Parse the nested declarator. */
20034 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20035 parser->in_type_id_in_expr_p = true;
20036 declarator
20037 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
20038 /*parenthesized_p=*/NULL,
20039 member_p, friend_p);
20040 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20041 first = false;
20042 /* Expect a `)'. */
20043 if (!parens.require_close (parser))
20044 declarator = cp_error_declarator;
20045 if (declarator == cp_error_declarator)
20046 break;
20048 goto handle_declarator;
20050 /* Otherwise, we must be done. */
20051 else
20052 break;
20054 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
20055 && token->type == CPP_OPEN_SQUARE
20056 && !cp_next_tokens_can_be_attribute_p (parser))
20058 /* Parse an array-declarator. */
20059 tree bounds, attrs;
20061 if (ctor_dtor_or_conv_p)
20062 *ctor_dtor_or_conv_p = 0;
20064 first = false;
20065 parser->default_arg_ok_p = false;
20066 parser->in_declarator_p = true;
20067 /* Consume the `['. */
20068 cp_lexer_consume_token (parser->lexer);
20069 /* Peek at the next token. */
20070 token = cp_lexer_peek_token (parser->lexer);
20071 /* If the next token is `]', then there is no
20072 constant-expression. */
20073 if (token->type != CPP_CLOSE_SQUARE)
20075 bool non_constant_p;
20076 bounds
20077 = cp_parser_constant_expression (parser,
20078 /*allow_non_constant=*/true,
20079 &non_constant_p);
20080 if (!non_constant_p)
20081 /* OK */;
20082 else if (error_operand_p (bounds))
20083 /* Already gave an error. */;
20084 else if (!parser->in_function_body
20085 || current_binding_level->kind == sk_function_parms)
20087 /* Normally, the array bound must be an integral constant
20088 expression. However, as an extension, we allow VLAs
20089 in function scopes as long as they aren't part of a
20090 parameter declaration. */
20091 cp_parser_error (parser,
20092 "array bound is not an integer constant");
20093 bounds = error_mark_node;
20095 else if (processing_template_decl
20096 && !type_dependent_expression_p (bounds))
20098 /* Remember this wasn't a constant-expression. */
20099 bounds = build_nop (TREE_TYPE (bounds), bounds);
20100 TREE_SIDE_EFFECTS (bounds) = 1;
20103 else
20104 bounds = NULL_TREE;
20105 /* Look for the closing `]'. */
20106 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
20108 declarator = cp_error_declarator;
20109 break;
20112 attrs = cp_parser_std_attribute_spec_seq (parser);
20113 declarator = make_array_declarator (declarator, bounds);
20114 declarator->std_attributes = attrs;
20116 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20119 tree qualifying_scope;
20120 tree unqualified_name;
20121 tree attrs;
20122 special_function_kind sfk;
20123 bool abstract_ok;
20124 bool pack_expansion_p = false;
20125 cp_token *declarator_id_start_token;
20127 /* Parse a declarator-id */
20128 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20129 if (abstract_ok)
20131 cp_parser_parse_tentatively (parser);
20133 /* If we see an ellipsis, we should be looking at a
20134 parameter pack. */
20135 if (token->type == CPP_ELLIPSIS)
20137 /* Consume the `...' */
20138 cp_lexer_consume_token (parser->lexer);
20140 pack_expansion_p = true;
20144 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20145 unqualified_name
20146 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20147 qualifying_scope = parser->scope;
20148 if (abstract_ok)
20150 bool okay = false;
20152 if (!unqualified_name && pack_expansion_p)
20154 /* Check whether an error occurred. */
20155 okay = !cp_parser_error_occurred (parser);
20157 /* We already consumed the ellipsis to mark a
20158 parameter pack, but we have no way to report it,
20159 so abort the tentative parse. We will be exiting
20160 immediately anyway. */
20161 cp_parser_abort_tentative_parse (parser);
20163 else
20164 okay = cp_parser_parse_definitely (parser);
20166 if (!okay)
20167 unqualified_name = error_mark_node;
20168 else if (unqualified_name
20169 && (qualifying_scope
20170 || (!identifier_p (unqualified_name))))
20172 cp_parser_error (parser, "expected unqualified-id");
20173 unqualified_name = error_mark_node;
20177 if (!unqualified_name)
20178 return NULL;
20179 if (unqualified_name == error_mark_node)
20181 declarator = cp_error_declarator;
20182 pack_expansion_p = false;
20183 declarator->parameter_pack_p = false;
20184 break;
20187 attrs = cp_parser_std_attribute_spec_seq (parser);
20189 if (qualifying_scope && at_namespace_scope_p ()
20190 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20192 /* In the declaration of a member of a template class
20193 outside of the class itself, the SCOPE will sometimes
20194 be a TYPENAME_TYPE. For example, given:
20196 template <typename T>
20197 int S<T>::R::i = 3;
20199 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20200 this context, we must resolve S<T>::R to an ordinary
20201 type, rather than a typename type.
20203 The reason we normally avoid resolving TYPENAME_TYPEs
20204 is that a specialization of `S' might render
20205 `S<T>::R' not a type. However, if `S' is
20206 specialized, then this `i' will not be used, so there
20207 is no harm in resolving the types here. */
20208 tree type;
20210 /* Resolve the TYPENAME_TYPE. */
20211 type = resolve_typename_type (qualifying_scope,
20212 /*only_current_p=*/false);
20213 /* If that failed, the declarator is invalid. */
20214 if (TREE_CODE (type) == TYPENAME_TYPE)
20216 if (typedef_variant_p (type))
20217 error_at (declarator_id_start_token->location,
20218 "cannot define member of dependent typedef "
20219 "%qT", type);
20220 else
20221 error_at (declarator_id_start_token->location,
20222 "%<%T::%E%> is not a type",
20223 TYPE_CONTEXT (qualifying_scope),
20224 TYPE_IDENTIFIER (qualifying_scope));
20226 qualifying_scope = type;
20229 sfk = sfk_none;
20231 if (unqualified_name)
20233 tree class_type;
20235 if (qualifying_scope
20236 && CLASS_TYPE_P (qualifying_scope))
20237 class_type = qualifying_scope;
20238 else
20239 class_type = current_class_type;
20241 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20243 tree name_type = TREE_TYPE (unqualified_name);
20245 if (!class_type || !same_type_p (name_type, class_type))
20247 /* We do not attempt to print the declarator
20248 here because we do not have enough
20249 information about its original syntactic
20250 form. */
20251 cp_parser_error (parser, "invalid declarator");
20252 declarator = cp_error_declarator;
20253 break;
20255 else if (qualifying_scope
20256 && CLASSTYPE_USE_TEMPLATE (name_type))
20258 error_at (declarator_id_start_token->location,
20259 "invalid use of constructor as a template");
20260 inform (declarator_id_start_token->location,
20261 "use %<%T::%D%> instead of %<%T::%D%> to "
20262 "name the constructor in a qualified name",
20263 class_type,
20264 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20265 class_type, name_type);
20266 declarator = cp_error_declarator;
20267 break;
20269 unqualified_name = constructor_name (class_type);
20272 if (class_type)
20274 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20275 sfk = sfk_destructor;
20276 else if (identifier_p (unqualified_name)
20277 && IDENTIFIER_CONV_OP_P (unqualified_name))
20278 sfk = sfk_conversion;
20279 else if (/* There's no way to declare a constructor
20280 for an unnamed type, even if the type
20281 got a name for linkage purposes. */
20282 !TYPE_WAS_UNNAMED (class_type)
20283 /* Handle correctly (c++/19200):
20285 struct S {
20286 struct T{};
20287 friend void S(T);
20290 and also:
20292 namespace N {
20293 void S();
20296 struct S {
20297 friend void N::S();
20298 }; */
20299 && (!friend_p || class_type == qualifying_scope)
20300 && constructor_name_p (unqualified_name,
20301 class_type))
20302 sfk = sfk_constructor;
20303 else if (is_overloaded_fn (unqualified_name)
20304 && DECL_CONSTRUCTOR_P (get_first_fn
20305 (unqualified_name)))
20306 sfk = sfk_constructor;
20308 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20309 *ctor_dtor_or_conv_p = -1;
20312 declarator = make_id_declarator (qualifying_scope,
20313 unqualified_name,
20314 sfk);
20315 declarator->std_attributes = attrs;
20316 declarator->id_loc = token->location;
20317 declarator->parameter_pack_p = pack_expansion_p;
20319 if (pack_expansion_p)
20320 maybe_warn_variadic_templates ();
20323 handle_declarator:;
20324 scope = get_scope_of_declarator (declarator);
20325 if (scope)
20327 /* Any names that appear after the declarator-id for a
20328 member are looked up in the containing scope. */
20329 if (at_function_scope_p ())
20331 /* But declarations with qualified-ids can't appear in a
20332 function. */
20333 cp_parser_error (parser, "qualified-id in declaration");
20334 declarator = cp_error_declarator;
20335 break;
20337 pushed_scope = push_scope (scope);
20339 parser->in_declarator_p = true;
20340 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20341 || (declarator && declarator->kind == cdk_id))
20342 /* Default args are only allowed on function
20343 declarations. */
20344 parser->default_arg_ok_p = saved_default_arg_ok_p;
20345 else
20346 parser->default_arg_ok_p = false;
20348 first = false;
20350 /* We're done. */
20351 else
20352 break;
20355 /* For an abstract declarator, we might wind up with nothing at this
20356 point. That's an error; the declarator is not optional. */
20357 if (!declarator)
20358 cp_parser_error (parser, "expected declarator");
20360 /* If we entered a scope, we must exit it now. */
20361 if (pushed_scope)
20362 pop_scope (pushed_scope);
20364 parser->default_arg_ok_p = saved_default_arg_ok_p;
20365 parser->in_declarator_p = saved_in_declarator_p;
20367 return declarator;
20370 /* Parse a ptr-operator.
20372 ptr-operator:
20373 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20374 * cv-qualifier-seq [opt]
20376 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20377 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20379 GNU Extension:
20381 ptr-operator:
20382 & cv-qualifier-seq [opt]
20384 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20385 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20386 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20387 filled in with the TYPE containing the member. *CV_QUALS is
20388 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20389 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20390 Note that the tree codes returned by this function have nothing
20391 to do with the types of trees that will be eventually be created
20392 to represent the pointer or reference type being parsed. They are
20393 just constants with suggestive names. */
20394 static enum tree_code
20395 cp_parser_ptr_operator (cp_parser* parser,
20396 tree* type,
20397 cp_cv_quals *cv_quals,
20398 tree *attributes)
20400 enum tree_code code = ERROR_MARK;
20401 cp_token *token;
20402 tree attrs = NULL_TREE;
20404 /* Assume that it's not a pointer-to-member. */
20405 *type = NULL_TREE;
20406 /* And that there are no cv-qualifiers. */
20407 *cv_quals = TYPE_UNQUALIFIED;
20409 /* Peek at the next token. */
20410 token = cp_lexer_peek_token (parser->lexer);
20412 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20413 if (token->type == CPP_MULT)
20414 code = INDIRECT_REF;
20415 else if (token->type == CPP_AND)
20416 code = ADDR_EXPR;
20417 else if ((cxx_dialect != cxx98) &&
20418 token->type == CPP_AND_AND) /* C++0x only */
20419 code = NON_LVALUE_EXPR;
20421 if (code != ERROR_MARK)
20423 /* Consume the `*', `&' or `&&'. */
20424 cp_lexer_consume_token (parser->lexer);
20426 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20427 `&', if we are allowing GNU extensions. (The only qualifier
20428 that can legally appear after `&' is `restrict', but that is
20429 enforced during semantic analysis. */
20430 if (code == INDIRECT_REF
20431 || cp_parser_allow_gnu_extensions_p (parser))
20432 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20434 attrs = cp_parser_std_attribute_spec_seq (parser);
20435 if (attributes != NULL)
20436 *attributes = attrs;
20438 else
20440 /* Try the pointer-to-member case. */
20441 cp_parser_parse_tentatively (parser);
20442 /* Look for the optional `::' operator. */
20443 cp_parser_global_scope_opt (parser,
20444 /*current_scope_valid_p=*/false);
20445 /* Look for the nested-name specifier. */
20446 token = cp_lexer_peek_token (parser->lexer);
20447 cp_parser_nested_name_specifier (parser,
20448 /*typename_keyword_p=*/false,
20449 /*check_dependency_p=*/true,
20450 /*type_p=*/false,
20451 /*is_declaration=*/false);
20452 /* If we found it, and the next token is a `*', then we are
20453 indeed looking at a pointer-to-member operator. */
20454 if (!cp_parser_error_occurred (parser)
20455 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20457 /* Indicate that the `*' operator was used. */
20458 code = INDIRECT_REF;
20460 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20461 error_at (token->location, "%qD is a namespace", parser->scope);
20462 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20463 error_at (token->location, "cannot form pointer to member of "
20464 "non-class %q#T", parser->scope);
20465 else
20467 /* The type of which the member is a member is given by the
20468 current SCOPE. */
20469 *type = parser->scope;
20470 /* The next name will not be qualified. */
20471 parser->scope = NULL_TREE;
20472 parser->qualifying_scope = NULL_TREE;
20473 parser->object_scope = NULL_TREE;
20474 /* Look for optional c++11 attributes. */
20475 attrs = cp_parser_std_attribute_spec_seq (parser);
20476 if (attributes != NULL)
20477 *attributes = attrs;
20478 /* Look for the optional cv-qualifier-seq. */
20479 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20482 /* If that didn't work we don't have a ptr-operator. */
20483 if (!cp_parser_parse_definitely (parser))
20484 cp_parser_error (parser, "expected ptr-operator");
20487 return code;
20490 /* Parse an (optional) cv-qualifier-seq.
20492 cv-qualifier-seq:
20493 cv-qualifier cv-qualifier-seq [opt]
20495 cv-qualifier:
20496 const
20497 volatile
20499 GNU Extension:
20501 cv-qualifier:
20502 __restrict__
20504 Returns a bitmask representing the cv-qualifiers. */
20506 static cp_cv_quals
20507 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20509 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20511 while (true)
20513 cp_token *token;
20514 cp_cv_quals cv_qualifier;
20516 /* Peek at the next token. */
20517 token = cp_lexer_peek_token (parser->lexer);
20518 /* See if it's a cv-qualifier. */
20519 switch (token->keyword)
20521 case RID_CONST:
20522 cv_qualifier = TYPE_QUAL_CONST;
20523 break;
20525 case RID_VOLATILE:
20526 cv_qualifier = TYPE_QUAL_VOLATILE;
20527 break;
20529 case RID_RESTRICT:
20530 cv_qualifier = TYPE_QUAL_RESTRICT;
20531 break;
20533 default:
20534 cv_qualifier = TYPE_UNQUALIFIED;
20535 break;
20538 if (!cv_qualifier)
20539 break;
20541 if (cv_quals & cv_qualifier)
20543 gcc_rich_location richloc (token->location);
20544 richloc.add_fixit_remove ();
20545 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20546 cp_lexer_purge_token (parser->lexer);
20548 else
20550 cp_lexer_consume_token (parser->lexer);
20551 cv_quals |= cv_qualifier;
20555 return cv_quals;
20558 /* Parse an (optional) ref-qualifier
20560 ref-qualifier:
20564 Returns cp_ref_qualifier representing ref-qualifier. */
20566 static cp_ref_qualifier
20567 cp_parser_ref_qualifier_opt (cp_parser* parser)
20569 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20571 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20572 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20573 return ref_qual;
20575 while (true)
20577 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20578 cp_token *token = cp_lexer_peek_token (parser->lexer);
20580 switch (token->type)
20582 case CPP_AND:
20583 curr_ref_qual = REF_QUAL_LVALUE;
20584 break;
20586 case CPP_AND_AND:
20587 curr_ref_qual = REF_QUAL_RVALUE;
20588 break;
20590 default:
20591 curr_ref_qual = REF_QUAL_NONE;
20592 break;
20595 if (!curr_ref_qual)
20596 break;
20597 else if (ref_qual)
20599 error_at (token->location, "multiple ref-qualifiers");
20600 cp_lexer_purge_token (parser->lexer);
20602 else
20604 ref_qual = curr_ref_qual;
20605 cp_lexer_consume_token (parser->lexer);
20609 return ref_qual;
20612 /* Parse an optional tx-qualifier.
20614 tx-qualifier:
20615 transaction_safe
20616 transaction_safe_dynamic */
20618 static tree
20619 cp_parser_tx_qualifier_opt (cp_parser *parser)
20621 cp_token *token = cp_lexer_peek_token (parser->lexer);
20622 if (token->type == CPP_NAME)
20624 tree name = token->u.value;
20625 const char *p = IDENTIFIER_POINTER (name);
20626 const int len = strlen ("transaction_safe");
20627 if (!strncmp (p, "transaction_safe", len))
20629 p += len;
20630 if (*p == '\0'
20631 || !strcmp (p, "_dynamic"))
20633 cp_lexer_consume_token (parser->lexer);
20634 if (!flag_tm)
20636 error ("%qE requires %<-fgnu-tm%>", name);
20637 return NULL_TREE;
20639 else
20640 return name;
20644 return NULL_TREE;
20647 /* Parse an (optional) virt-specifier-seq.
20649 virt-specifier-seq:
20650 virt-specifier virt-specifier-seq [opt]
20652 virt-specifier:
20653 override
20654 final
20656 Returns a bitmask representing the virt-specifiers. */
20658 static cp_virt_specifiers
20659 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20661 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20663 while (true)
20665 cp_token *token;
20666 cp_virt_specifiers virt_specifier;
20668 /* Peek at the next token. */
20669 token = cp_lexer_peek_token (parser->lexer);
20670 /* See if it's a virt-specifier-qualifier. */
20671 if (token->type != CPP_NAME)
20672 break;
20673 if (id_equal (token->u.value, "override"))
20675 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20676 virt_specifier = VIRT_SPEC_OVERRIDE;
20678 else if (id_equal (token->u.value, "final"))
20680 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20681 virt_specifier = VIRT_SPEC_FINAL;
20683 else if (id_equal (token->u.value, "__final"))
20685 virt_specifier = VIRT_SPEC_FINAL;
20687 else
20688 break;
20690 if (virt_specifiers & virt_specifier)
20692 gcc_rich_location richloc (token->location);
20693 richloc.add_fixit_remove ();
20694 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20695 cp_lexer_purge_token (parser->lexer);
20697 else
20699 cp_lexer_consume_token (parser->lexer);
20700 virt_specifiers |= virt_specifier;
20703 return virt_specifiers;
20706 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20707 is in scope even though it isn't real. */
20709 void
20710 inject_this_parameter (tree ctype, cp_cv_quals quals)
20712 tree this_parm;
20714 if (current_class_ptr)
20716 /* We don't clear this between NSDMIs. Is it already what we want? */
20717 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20718 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20719 && cp_type_quals (type) == quals)
20720 return;
20723 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20724 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20725 current_class_ptr = NULL_TREE;
20726 current_class_ref
20727 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20728 current_class_ptr = this_parm;
20731 /* Return true iff our current scope is a non-static data member
20732 initializer. */
20734 bool
20735 parsing_nsdmi (void)
20737 /* We recognize NSDMI context by the context-less 'this' pointer set up
20738 by the function above. */
20739 if (current_class_ptr
20740 && TREE_CODE (current_class_ptr) == PARM_DECL
20741 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20742 return true;
20743 return false;
20746 /* Return true iff our current scope is a default capturing generic lambda
20747 defined within a template. FIXME: This is part of a workaround (see
20748 semantics.c) to handle building lambda closure types correctly in templates
20749 which we ultimately want to defer to instantiation time. */
20751 bool
20752 parsing_default_capturing_generic_lambda_in_template (void)
20754 if (!processing_template_decl || !current_class_type)
20755 return false;
20757 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20758 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20759 return false;
20761 tree callop = lambda_function (lam);
20762 if (!callop)
20763 return false;
20765 return (DECL_TEMPLATE_INFO (callop)
20766 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20767 && ((current_nonlambda_class_type ()
20768 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20769 || ((current_nonlambda_function ()
20770 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20773 /* Parse a late-specified return type, if any. This is not a separate
20774 non-terminal, but part of a function declarator, which looks like
20776 -> trailing-type-specifier-seq abstract-declarator(opt)
20778 Returns the type indicated by the type-id.
20780 In addition to this, parse any queued up #pragma omp declare simd
20781 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20782 #pragma acc routine clauses.
20784 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20785 function. */
20787 static tree
20788 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20789 tree& requires_clause, cp_cv_quals quals)
20791 cp_token *token;
20792 tree type = NULL_TREE;
20793 bool declare_simd_p = (parser->omp_declare_simd
20794 && declarator
20795 && declarator->kind == cdk_id);
20797 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20798 && declarator && declarator->kind == cdk_id);
20800 bool oacc_routine_p = (parser->oacc_routine
20801 && declarator
20802 && declarator->kind == cdk_id);
20804 /* Peek at the next token. */
20805 token = cp_lexer_peek_token (parser->lexer);
20806 /* A late-specified return type is indicated by an initial '->'. */
20807 if (token->type != CPP_DEREF
20808 && token->keyword != RID_REQUIRES
20809 && !(token->type == CPP_NAME
20810 && token->u.value == ridpointers[RID_REQUIRES])
20811 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20812 return NULL_TREE;
20814 tree save_ccp = current_class_ptr;
20815 tree save_ccr = current_class_ref;
20816 if (quals >= 0)
20818 /* DR 1207: 'this' is in scope in the trailing return type. */
20819 inject_this_parameter (current_class_type, quals);
20822 if (token->type == CPP_DEREF)
20824 /* Consume the ->. */
20825 cp_lexer_consume_token (parser->lexer);
20827 type = cp_parser_trailing_type_id (parser);
20830 /* Function declarations may be followed by a trailing
20831 requires-clause. */
20832 requires_clause = cp_parser_requires_clause_opt (parser);
20834 if (cilk_simd_fn_vector_p)
20835 declarator->attributes
20836 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20837 declarator->attributes);
20838 if (declare_simd_p)
20839 declarator->attributes
20840 = cp_parser_late_parsing_omp_declare_simd (parser,
20841 declarator->attributes);
20842 if (oacc_routine_p)
20843 declarator->attributes
20844 = cp_parser_late_parsing_oacc_routine (parser,
20845 declarator->attributes);
20847 if (quals >= 0)
20849 current_class_ptr = save_ccp;
20850 current_class_ref = save_ccr;
20853 return type;
20856 /* Parse a declarator-id.
20858 declarator-id:
20859 id-expression
20860 :: [opt] nested-name-specifier [opt] type-name
20862 In the `id-expression' case, the value returned is as for
20863 cp_parser_id_expression if the id-expression was an unqualified-id.
20864 If the id-expression was a qualified-id, then a SCOPE_REF is
20865 returned. The first operand is the scope (either a NAMESPACE_DECL
20866 or TREE_TYPE), but the second is still just a representation of an
20867 unqualified-id. */
20869 static tree
20870 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20872 tree id;
20873 /* The expression must be an id-expression. Assume that qualified
20874 names are the names of types so that:
20876 template <class T>
20877 int S<T>::R::i = 3;
20879 will work; we must treat `S<T>::R' as the name of a type.
20880 Similarly, assume that qualified names are templates, where
20881 required, so that:
20883 template <class T>
20884 int S<T>::R<T>::i = 3;
20886 will work, too. */
20887 id = cp_parser_id_expression (parser,
20888 /*template_keyword_p=*/false,
20889 /*check_dependency_p=*/false,
20890 /*template_p=*/NULL,
20891 /*declarator_p=*/true,
20892 optional_p);
20893 if (id && BASELINK_P (id))
20894 id = BASELINK_FUNCTIONS (id);
20895 return id;
20898 /* Parse a type-id.
20900 type-id:
20901 type-specifier-seq abstract-declarator [opt]
20903 Returns the TYPE specified. */
20905 static tree
20906 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20907 bool is_trailing_return)
20909 cp_decl_specifier_seq type_specifier_seq;
20910 cp_declarator *abstract_declarator;
20912 /* Parse the type-specifier-seq. */
20913 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20914 is_trailing_return,
20915 &type_specifier_seq);
20916 if (is_template_arg && type_specifier_seq.type
20917 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20918 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20919 /* A bare template name as a template argument is a template template
20920 argument, not a placeholder, so fail parsing it as a type argument. */
20922 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20923 cp_parser_simulate_error (parser);
20924 return error_mark_node;
20926 if (type_specifier_seq.type == error_mark_node)
20927 return error_mark_node;
20929 /* There might or might not be an abstract declarator. */
20930 cp_parser_parse_tentatively (parser);
20931 /* Look for the declarator. */
20932 abstract_declarator
20933 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20934 /*parenthesized_p=*/NULL,
20935 /*member_p=*/false,
20936 /*friend_p=*/false);
20937 /* Check to see if there really was a declarator. */
20938 if (!cp_parser_parse_definitely (parser))
20939 abstract_declarator = NULL;
20941 if (type_specifier_seq.type
20942 /* The concepts TS allows 'auto' as a type-id. */
20943 && (!flag_concepts || parser->in_type_id_in_expr_p)
20944 /* None of the valid uses of 'auto' in C++14 involve the type-id
20945 nonterminal, but it is valid in a trailing-return-type. */
20946 && !(cxx_dialect >= cxx14 && is_trailing_return))
20947 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20949 /* A type-id with type 'auto' is only ok if the abstract declarator
20950 is a function declarator with a late-specified return type.
20952 A type-id with 'auto' is also valid in a trailing-return-type
20953 in a compound-requirement. */
20954 if (abstract_declarator
20955 && abstract_declarator->kind == cdk_function
20956 && abstract_declarator->u.function.late_return_type)
20957 /* OK */;
20958 else if (parser->in_result_type_constraint_p)
20959 /* OK */;
20960 else
20962 location_t loc = type_specifier_seq.locations[ds_type_spec];
20963 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20965 error_at (loc, "missing template arguments after %qT",
20966 auto_node);
20967 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20968 tmpl);
20970 else
20971 error_at (loc, "invalid use of %qT", auto_node);
20972 return error_mark_node;
20976 return groktypename (&type_specifier_seq, abstract_declarator,
20977 is_template_arg);
20980 static tree
20981 cp_parser_type_id (cp_parser *parser)
20983 return cp_parser_type_id_1 (parser, false, false);
20986 static tree
20987 cp_parser_template_type_arg (cp_parser *parser)
20989 tree r;
20990 const char *saved_message = parser->type_definition_forbidden_message;
20991 parser->type_definition_forbidden_message
20992 = G_("types may not be defined in template arguments");
20993 r = cp_parser_type_id_1 (parser, true, false);
20994 parser->type_definition_forbidden_message = saved_message;
20995 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20997 error ("invalid use of %<auto%> in template argument");
20998 r = error_mark_node;
21000 return r;
21003 static tree
21004 cp_parser_trailing_type_id (cp_parser *parser)
21006 return cp_parser_type_id_1 (parser, false, true);
21009 /* Parse a type-specifier-seq.
21011 type-specifier-seq:
21012 type-specifier type-specifier-seq [opt]
21014 GNU extension:
21016 type-specifier-seq:
21017 attributes type-specifier-seq [opt]
21019 If IS_DECLARATION is true, we are at the start of a "condition" or
21020 exception-declaration, so we might be followed by a declarator-id.
21022 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
21023 i.e. we've just seen "->".
21025 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
21027 static void
21028 cp_parser_type_specifier_seq (cp_parser* parser,
21029 bool is_declaration,
21030 bool is_trailing_return,
21031 cp_decl_specifier_seq *type_specifier_seq)
21033 bool seen_type_specifier = false;
21034 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
21035 cp_token *start_token = NULL;
21037 /* Clear the TYPE_SPECIFIER_SEQ. */
21038 clear_decl_specs (type_specifier_seq);
21040 /* In the context of a trailing return type, enum E { } is an
21041 elaborated-type-specifier followed by a function-body, not an
21042 enum-specifier. */
21043 if (is_trailing_return)
21044 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
21046 /* Parse the type-specifiers and attributes. */
21047 while (true)
21049 tree type_specifier;
21050 bool is_cv_qualifier;
21052 /* Check for attributes first. */
21053 if (cp_next_tokens_can_be_attribute_p (parser))
21055 type_specifier_seq->attributes =
21056 chainon (type_specifier_seq->attributes,
21057 cp_parser_attributes_opt (parser));
21058 continue;
21061 /* record the token of the beginning of the type specifier seq,
21062 for error reporting purposes*/
21063 if (!start_token)
21064 start_token = cp_lexer_peek_token (parser->lexer);
21066 /* Look for the type-specifier. */
21067 type_specifier = cp_parser_type_specifier (parser,
21068 flags,
21069 type_specifier_seq,
21070 /*is_declaration=*/false,
21071 NULL,
21072 &is_cv_qualifier);
21073 if (!type_specifier)
21075 /* If the first type-specifier could not be found, this is not a
21076 type-specifier-seq at all. */
21077 if (!seen_type_specifier)
21079 /* Set in_declarator_p to avoid skipping to the semicolon. */
21080 int in_decl = parser->in_declarator_p;
21081 parser->in_declarator_p = true;
21083 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
21084 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
21085 cp_parser_error (parser, "expected type-specifier");
21087 parser->in_declarator_p = in_decl;
21089 type_specifier_seq->type = error_mark_node;
21090 return;
21092 /* If subsequent type-specifiers could not be found, the
21093 type-specifier-seq is complete. */
21094 break;
21097 seen_type_specifier = true;
21098 /* The standard says that a condition can be:
21100 type-specifier-seq declarator = assignment-expression
21102 However, given:
21104 struct S {};
21105 if (int S = ...)
21107 we should treat the "S" as a declarator, not as a
21108 type-specifier. The standard doesn't say that explicitly for
21109 type-specifier-seq, but it does say that for
21110 decl-specifier-seq in an ordinary declaration. Perhaps it
21111 would be clearer just to allow a decl-specifier-seq here, and
21112 then add a semantic restriction that if any decl-specifiers
21113 that are not type-specifiers appear, the program is invalid. */
21114 if (is_declaration && !is_cv_qualifier)
21115 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21119 /* Return whether the function currently being declared has an associated
21120 template parameter list. */
21122 static bool
21123 function_being_declared_is_template_p (cp_parser* parser)
21125 if (!current_template_parms || processing_template_parmlist)
21126 return false;
21128 if (parser->implicit_template_scope)
21129 return true;
21131 if (at_class_scope_p ()
21132 && TYPE_BEING_DEFINED (current_class_type))
21133 return parser->num_template_parameter_lists != 0;
21135 return ((int) parser->num_template_parameter_lists > template_class_depth
21136 (current_class_type));
21139 /* Parse a parameter-declaration-clause.
21141 parameter-declaration-clause:
21142 parameter-declaration-list [opt] ... [opt]
21143 parameter-declaration-list , ...
21145 Returns a representation for the parameter declarations. A return
21146 value of NULL indicates a parameter-declaration-clause consisting
21147 only of an ellipsis. */
21149 static tree
21150 cp_parser_parameter_declaration_clause (cp_parser* parser)
21152 tree parameters;
21153 cp_token *token;
21154 bool ellipsis_p;
21155 bool is_error;
21157 struct cleanup {
21158 cp_parser* parser;
21159 int auto_is_implicit_function_template_parm_p;
21160 ~cleanup() {
21161 parser->auto_is_implicit_function_template_parm_p
21162 = auto_is_implicit_function_template_parm_p;
21164 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21166 (void) cleanup;
21168 if (!processing_specialization
21169 && !processing_template_parmlist
21170 && !processing_explicit_instantiation)
21171 if (!current_function_decl
21172 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21173 parser->auto_is_implicit_function_template_parm_p = true;
21175 /* Peek at the next token. */
21176 token = cp_lexer_peek_token (parser->lexer);
21177 /* Check for trivial parameter-declaration-clauses. */
21178 if (token->type == CPP_ELLIPSIS)
21180 /* Consume the `...' token. */
21181 cp_lexer_consume_token (parser->lexer);
21182 return NULL_TREE;
21184 else if (token->type == CPP_CLOSE_PAREN)
21185 /* There are no parameters. */
21187 #ifndef NO_IMPLICIT_EXTERN_C
21188 if (in_system_header_at (input_location)
21189 && current_class_type == NULL
21190 && current_lang_name == lang_name_c)
21191 return NULL_TREE;
21192 else
21193 #endif
21194 return void_list_node;
21196 /* Check for `(void)', too, which is a special case. */
21197 else if (token->keyword == RID_VOID
21198 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21199 == CPP_CLOSE_PAREN))
21201 /* Consume the `void' token. */
21202 cp_lexer_consume_token (parser->lexer);
21203 /* There are no parameters. */
21204 return void_list_node;
21207 /* Parse the parameter-declaration-list. */
21208 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21209 /* If a parse error occurred while parsing the
21210 parameter-declaration-list, then the entire
21211 parameter-declaration-clause is erroneous. */
21212 if (is_error)
21213 return NULL;
21215 /* Peek at the next token. */
21216 token = cp_lexer_peek_token (parser->lexer);
21217 /* If it's a `,', the clause should terminate with an ellipsis. */
21218 if (token->type == CPP_COMMA)
21220 /* Consume the `,'. */
21221 cp_lexer_consume_token (parser->lexer);
21222 /* Expect an ellipsis. */
21223 ellipsis_p
21224 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21226 /* It might also be `...' if the optional trailing `,' was
21227 omitted. */
21228 else if (token->type == CPP_ELLIPSIS)
21230 /* Consume the `...' token. */
21231 cp_lexer_consume_token (parser->lexer);
21232 /* And remember that we saw it. */
21233 ellipsis_p = true;
21235 else
21236 ellipsis_p = false;
21238 /* Finish the parameter list. */
21239 if (!ellipsis_p)
21240 parameters = chainon (parameters, void_list_node);
21242 return parameters;
21245 /* Parse a parameter-declaration-list.
21247 parameter-declaration-list:
21248 parameter-declaration
21249 parameter-declaration-list , parameter-declaration
21251 Returns a representation of the parameter-declaration-list, as for
21252 cp_parser_parameter_declaration_clause. However, the
21253 `void_list_node' is never appended to the list. Upon return,
21254 *IS_ERROR will be true iff an error occurred. */
21256 static tree
21257 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21259 tree parameters = NULL_TREE;
21260 tree *tail = &parameters;
21261 bool saved_in_unbraced_linkage_specification_p;
21262 int index = 0;
21264 /* Assume all will go well. */
21265 *is_error = false;
21266 /* The special considerations that apply to a function within an
21267 unbraced linkage specifications do not apply to the parameters
21268 to the function. */
21269 saved_in_unbraced_linkage_specification_p
21270 = parser->in_unbraced_linkage_specification_p;
21271 parser->in_unbraced_linkage_specification_p = false;
21273 /* Look for more parameters. */
21274 while (true)
21276 cp_parameter_declarator *parameter;
21277 tree decl = error_mark_node;
21278 bool parenthesized_p = false;
21279 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21280 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21281 (current_template_parms)) : 0);
21283 /* Parse the parameter. */
21284 parameter
21285 = cp_parser_parameter_declaration (parser,
21286 /*template_parm_p=*/false,
21287 &parenthesized_p);
21289 /* We don't know yet if the enclosing context is deprecated, so wait
21290 and warn in grokparms if appropriate. */
21291 deprecated_state = DEPRECATED_SUPPRESS;
21293 if (parameter)
21295 /* If a function parameter pack was specified and an implicit template
21296 parameter was introduced during cp_parser_parameter_declaration,
21297 change any implicit parameters introduced into packs. */
21298 if (parser->implicit_template_parms
21299 && parameter->declarator
21300 && parameter->declarator->parameter_pack_p)
21302 int latest_template_parm_idx = TREE_VEC_LENGTH
21303 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21305 if (latest_template_parm_idx != template_parm_idx)
21306 parameter->decl_specifiers.type = convert_generic_types_to_packs
21307 (parameter->decl_specifiers.type,
21308 template_parm_idx, latest_template_parm_idx);
21311 decl = grokdeclarator (parameter->declarator,
21312 &parameter->decl_specifiers,
21313 PARM,
21314 parameter->default_argument != NULL_TREE,
21315 &parameter->decl_specifiers.attributes);
21318 deprecated_state = DEPRECATED_NORMAL;
21320 /* If a parse error occurred parsing the parameter declaration,
21321 then the entire parameter-declaration-list is erroneous. */
21322 if (decl == error_mark_node)
21324 *is_error = true;
21325 parameters = error_mark_node;
21326 break;
21329 if (parameter->decl_specifiers.attributes)
21330 cplus_decl_attributes (&decl,
21331 parameter->decl_specifiers.attributes,
21333 if (DECL_NAME (decl))
21334 decl = pushdecl (decl);
21336 if (decl != error_mark_node)
21338 retrofit_lang_decl (decl);
21339 DECL_PARM_INDEX (decl) = ++index;
21340 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21343 /* Add the new parameter to the list. */
21344 *tail = build_tree_list (parameter->default_argument, decl);
21345 tail = &TREE_CHAIN (*tail);
21347 /* Peek at the next token. */
21348 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21349 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21350 /* These are for Objective-C++ */
21351 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21352 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21353 /* The parameter-declaration-list is complete. */
21354 break;
21355 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21357 cp_token *token;
21359 /* Peek at the next token. */
21360 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21361 /* If it's an ellipsis, then the list is complete. */
21362 if (token->type == CPP_ELLIPSIS)
21363 break;
21364 /* Otherwise, there must be more parameters. Consume the
21365 `,'. */
21366 cp_lexer_consume_token (parser->lexer);
21367 /* When parsing something like:
21369 int i(float f, double d)
21371 we can tell after seeing the declaration for "f" that we
21372 are not looking at an initialization of a variable "i",
21373 but rather at the declaration of a function "i".
21375 Due to the fact that the parsing of template arguments
21376 (as specified to a template-id) requires backtracking we
21377 cannot use this technique when inside a template argument
21378 list. */
21379 if (!parser->in_template_argument_list_p
21380 && !parser->in_type_id_in_expr_p
21381 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21382 /* However, a parameter-declaration of the form
21383 "float(f)" (which is a valid declaration of a
21384 parameter "f") can also be interpreted as an
21385 expression (the conversion of "f" to "float"). */
21386 && !parenthesized_p)
21387 cp_parser_commit_to_tentative_parse (parser);
21389 else
21391 cp_parser_error (parser, "expected %<,%> or %<...%>");
21392 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21393 cp_parser_skip_to_closing_parenthesis (parser,
21394 /*recovering=*/true,
21395 /*or_comma=*/false,
21396 /*consume_paren=*/false);
21397 break;
21401 parser->in_unbraced_linkage_specification_p
21402 = saved_in_unbraced_linkage_specification_p;
21404 /* Reset implicit_template_scope if we are about to leave the function
21405 parameter list that introduced it. Note that for out-of-line member
21406 definitions, there will be one or more class scopes before we get to
21407 the template parameter scope. */
21409 if (cp_binding_level *its = parser->implicit_template_scope)
21410 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21412 while (maybe_its->kind == sk_class)
21413 maybe_its = maybe_its->level_chain;
21414 if (maybe_its == its)
21416 parser->implicit_template_parms = 0;
21417 parser->implicit_template_scope = 0;
21421 return parameters;
21424 /* Parse a parameter declaration.
21426 parameter-declaration:
21427 decl-specifier-seq ... [opt] declarator
21428 decl-specifier-seq declarator = assignment-expression
21429 decl-specifier-seq ... [opt] abstract-declarator [opt]
21430 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21432 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21433 declares a template parameter. (In that case, a non-nested `>'
21434 token encountered during the parsing of the assignment-expression
21435 is not interpreted as a greater-than operator.)
21437 Returns a representation of the parameter, or NULL if an error
21438 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21439 true iff the declarator is of the form "(p)". */
21441 static cp_parameter_declarator *
21442 cp_parser_parameter_declaration (cp_parser *parser,
21443 bool template_parm_p,
21444 bool *parenthesized_p)
21446 int declares_class_or_enum;
21447 cp_decl_specifier_seq decl_specifiers;
21448 cp_declarator *declarator;
21449 tree default_argument;
21450 cp_token *token = NULL, *declarator_token_start = NULL;
21451 const char *saved_message;
21452 bool template_parameter_pack_p = false;
21454 /* In a template parameter, `>' is not an operator.
21456 [temp.param]
21458 When parsing a default template-argument for a non-type
21459 template-parameter, the first non-nested `>' is taken as the end
21460 of the template parameter-list rather than a greater-than
21461 operator. */
21463 /* Type definitions may not appear in parameter types. */
21464 saved_message = parser->type_definition_forbidden_message;
21465 parser->type_definition_forbidden_message
21466 = G_("types may not be defined in parameter types");
21468 /* Parse the declaration-specifiers. */
21469 cp_parser_decl_specifier_seq (parser,
21470 CP_PARSER_FLAGS_NONE,
21471 &decl_specifiers,
21472 &declares_class_or_enum);
21474 /* Complain about missing 'typename' or other invalid type names. */
21475 if (!decl_specifiers.any_type_specifiers_p
21476 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21477 decl_specifiers.type = error_mark_node;
21479 /* If an error occurred, there's no reason to attempt to parse the
21480 rest of the declaration. */
21481 if (cp_parser_error_occurred (parser))
21483 parser->type_definition_forbidden_message = saved_message;
21484 return NULL;
21487 /* Peek at the next token. */
21488 token = cp_lexer_peek_token (parser->lexer);
21490 /* If the next token is a `)', `,', `=', `>', or `...', then there
21491 is no declarator. However, when variadic templates are enabled,
21492 there may be a declarator following `...'. */
21493 if (token->type == CPP_CLOSE_PAREN
21494 || token->type == CPP_COMMA
21495 || token->type == CPP_EQ
21496 || token->type == CPP_GREATER)
21498 declarator = NULL;
21499 if (parenthesized_p)
21500 *parenthesized_p = false;
21502 /* Otherwise, there should be a declarator. */
21503 else
21505 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21506 parser->default_arg_ok_p = false;
21508 /* After seeing a decl-specifier-seq, if the next token is not a
21509 "(", there is no possibility that the code is a valid
21510 expression. Therefore, if parsing tentatively, we commit at
21511 this point. */
21512 if (!parser->in_template_argument_list_p
21513 /* In an expression context, having seen:
21515 (int((char ...
21517 we cannot be sure whether we are looking at a
21518 function-type (taking a "char" as a parameter) or a cast
21519 of some object of type "char" to "int". */
21520 && !parser->in_type_id_in_expr_p
21521 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21522 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21523 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21524 cp_parser_commit_to_tentative_parse (parser);
21525 /* Parse the declarator. */
21526 declarator_token_start = token;
21527 declarator = cp_parser_declarator (parser,
21528 CP_PARSER_DECLARATOR_EITHER,
21529 /*ctor_dtor_or_conv_p=*/NULL,
21530 parenthesized_p,
21531 /*member_p=*/false,
21532 /*friend_p=*/false);
21533 parser->default_arg_ok_p = saved_default_arg_ok_p;
21534 /* After the declarator, allow more attributes. */
21535 decl_specifiers.attributes
21536 = chainon (decl_specifiers.attributes,
21537 cp_parser_attributes_opt (parser));
21539 /* If the declarator is a template parameter pack, remember that and
21540 clear the flag in the declarator itself so we don't get errors
21541 from grokdeclarator. */
21542 if (template_parm_p && declarator && declarator->parameter_pack_p)
21544 declarator->parameter_pack_p = false;
21545 template_parameter_pack_p = true;
21549 /* If the next token is an ellipsis, and we have not seen a declarator
21550 name, and if either the type of the declarator contains parameter
21551 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21552 for, eg, abbreviated integral type names), then we actually have a
21553 parameter pack expansion expression. Otherwise, leave the ellipsis
21554 for a C-style variadic function. */
21555 token = cp_lexer_peek_token (parser->lexer);
21556 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21558 tree type = decl_specifiers.type;
21560 if (type && DECL_P (type))
21561 type = TREE_TYPE (type);
21563 if (((type
21564 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21565 && (template_parm_p || uses_parameter_packs (type)))
21566 || (!type && template_parm_p))
21567 && declarator_can_be_parameter_pack (declarator))
21569 /* Consume the `...'. */
21570 cp_lexer_consume_token (parser->lexer);
21571 maybe_warn_variadic_templates ();
21573 /* Build a pack expansion type */
21574 if (template_parm_p)
21575 template_parameter_pack_p = true;
21576 else if (declarator)
21577 declarator->parameter_pack_p = true;
21578 else
21579 decl_specifiers.type = make_pack_expansion (type);
21583 /* The restriction on defining new types applies only to the type
21584 of the parameter, not to the default argument. */
21585 parser->type_definition_forbidden_message = saved_message;
21587 /* If the next token is `=', then process a default argument. */
21588 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21590 tree type = decl_specifiers.type;
21591 token = cp_lexer_peek_token (parser->lexer);
21592 /* If we are defining a class, then the tokens that make up the
21593 default argument must be saved and processed later. */
21594 if (!template_parm_p && at_class_scope_p ()
21595 && TYPE_BEING_DEFINED (current_class_type)
21596 && !LAMBDA_TYPE_P (current_class_type))
21597 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21599 // A constrained-type-specifier may declare a type template-parameter.
21600 else if (declares_constrained_type_template_parameter (type))
21601 default_argument
21602 = cp_parser_default_type_template_argument (parser);
21604 // A constrained-type-specifier may declare a template-template-parameter.
21605 else if (declares_constrained_template_template_parameter (type))
21606 default_argument
21607 = cp_parser_default_template_template_argument (parser);
21609 /* Outside of a class definition, we can just parse the
21610 assignment-expression. */
21611 else
21612 default_argument
21613 = cp_parser_default_argument (parser, template_parm_p);
21615 if (!parser->default_arg_ok_p)
21617 permerror (token->location,
21618 "default arguments are only "
21619 "permitted for function parameters");
21621 else if ((declarator && declarator->parameter_pack_p)
21622 || template_parameter_pack_p
21623 || (decl_specifiers.type
21624 && PACK_EXPANSION_P (decl_specifiers.type)))
21626 /* Find the name of the parameter pack. */
21627 cp_declarator *id_declarator = declarator;
21628 while (id_declarator && id_declarator->kind != cdk_id)
21629 id_declarator = id_declarator->declarator;
21631 if (id_declarator && id_declarator->kind == cdk_id)
21632 error_at (declarator_token_start->location,
21633 template_parm_p
21634 ? G_("template parameter pack %qD "
21635 "cannot have a default argument")
21636 : G_("parameter pack %qD cannot have "
21637 "a default argument"),
21638 id_declarator->u.id.unqualified_name);
21639 else
21640 error_at (declarator_token_start->location,
21641 template_parm_p
21642 ? G_("template parameter pack cannot have "
21643 "a default argument")
21644 : G_("parameter pack cannot have a "
21645 "default argument"));
21647 default_argument = NULL_TREE;
21650 else
21651 default_argument = NULL_TREE;
21653 return make_parameter_declarator (&decl_specifiers,
21654 declarator,
21655 default_argument,
21656 template_parameter_pack_p);
21659 /* Parse a default argument and return it.
21661 TEMPLATE_PARM_P is true if this is a default argument for a
21662 non-type template parameter. */
21663 static tree
21664 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21666 tree default_argument = NULL_TREE;
21667 bool saved_greater_than_is_operator_p;
21668 bool saved_local_variables_forbidden_p;
21669 bool non_constant_p, is_direct_init;
21671 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21672 set correctly. */
21673 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21674 parser->greater_than_is_operator_p = !template_parm_p;
21675 /* Local variable names (and the `this' keyword) may not
21676 appear in a default argument. */
21677 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21678 parser->local_variables_forbidden_p = true;
21679 /* Parse the assignment-expression. */
21680 if (template_parm_p)
21681 push_deferring_access_checks (dk_no_deferred);
21682 tree saved_class_ptr = NULL_TREE;
21683 tree saved_class_ref = NULL_TREE;
21684 /* The "this" pointer is not valid in a default argument. */
21685 if (cfun)
21687 saved_class_ptr = current_class_ptr;
21688 cp_function_chain->x_current_class_ptr = NULL_TREE;
21689 saved_class_ref = current_class_ref;
21690 cp_function_chain->x_current_class_ref = NULL_TREE;
21692 default_argument
21693 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21694 /* Restore the "this" pointer. */
21695 if (cfun)
21697 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21698 cp_function_chain->x_current_class_ref = saved_class_ref;
21700 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21701 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21702 if (template_parm_p)
21703 pop_deferring_access_checks ();
21704 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21705 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21707 return default_argument;
21710 /* Parse a function-body.
21712 function-body:
21713 compound_statement */
21715 static void
21716 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21718 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21719 ? BCS_TRY_BLOCK : BCS_NORMAL),
21720 true);
21723 /* Parse a ctor-initializer-opt followed by a function-body. Return
21724 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21725 is true we are parsing a function-try-block. */
21727 static bool
21728 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21729 bool in_function_try_block)
21731 tree body, list;
21732 bool ctor_initializer_p;
21733 const bool check_body_p =
21734 DECL_CONSTRUCTOR_P (current_function_decl)
21735 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21736 tree last = NULL;
21738 /* Begin the function body. */
21739 body = begin_function_body ();
21740 /* Parse the optional ctor-initializer. */
21741 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21743 /* If we're parsing a constexpr constructor definition, we need
21744 to check that the constructor body is indeed empty. However,
21745 before we get to cp_parser_function_body lot of junk has been
21746 generated, so we can't just check that we have an empty block.
21747 Rather we take a snapshot of the outermost block, and check whether
21748 cp_parser_function_body changed its state. */
21749 if (check_body_p)
21751 list = cur_stmt_list;
21752 if (STATEMENT_LIST_TAIL (list))
21753 last = STATEMENT_LIST_TAIL (list)->stmt;
21755 /* Parse the function-body. */
21756 cp_parser_function_body (parser, in_function_try_block);
21757 if (check_body_p)
21758 check_constexpr_ctor_body (last, list, /*complain=*/true);
21759 /* Finish the function body. */
21760 finish_function_body (body);
21762 return ctor_initializer_p;
21765 /* Parse an initializer.
21767 initializer:
21768 = initializer-clause
21769 ( expression-list )
21771 Returns an expression representing the initializer. If no
21772 initializer is present, NULL_TREE is returned.
21774 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21775 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21776 set to TRUE if there is no initializer present. If there is an
21777 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21778 is set to true; otherwise it is set to false. */
21780 static tree
21781 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21782 bool* non_constant_p)
21784 cp_token *token;
21785 tree init;
21787 /* Peek at the next token. */
21788 token = cp_lexer_peek_token (parser->lexer);
21790 /* Let our caller know whether or not this initializer was
21791 parenthesized. */
21792 *is_direct_init = (token->type != CPP_EQ);
21793 /* Assume that the initializer is constant. */
21794 *non_constant_p = false;
21796 if (token->type == CPP_EQ)
21798 /* Consume the `='. */
21799 cp_lexer_consume_token (parser->lexer);
21800 /* Parse the initializer-clause. */
21801 init = cp_parser_initializer_clause (parser, non_constant_p);
21803 else if (token->type == CPP_OPEN_PAREN)
21805 vec<tree, va_gc> *vec;
21806 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21807 /*cast_p=*/false,
21808 /*allow_expansion_p=*/true,
21809 non_constant_p);
21810 if (vec == NULL)
21811 return error_mark_node;
21812 init = build_tree_list_vec (vec);
21813 release_tree_vector (vec);
21815 else if (token->type == CPP_OPEN_BRACE)
21817 cp_lexer_set_source_position (parser->lexer);
21818 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21819 init = cp_parser_braced_list (parser, non_constant_p);
21820 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21822 else
21824 /* Anything else is an error. */
21825 cp_parser_error (parser, "expected initializer");
21826 init = error_mark_node;
21829 if (check_for_bare_parameter_packs (init))
21830 init = error_mark_node;
21832 return init;
21835 /* Parse an initializer-clause.
21837 initializer-clause:
21838 assignment-expression
21839 braced-init-list
21841 Returns an expression representing the initializer.
21843 If the `assignment-expression' production is used the value
21844 returned is simply a representation for the expression.
21846 Otherwise, calls cp_parser_braced_list. */
21848 static cp_expr
21849 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21851 cp_expr initializer;
21853 /* Assume the expression is constant. */
21854 *non_constant_p = false;
21856 /* If it is not a `{', then we are looking at an
21857 assignment-expression. */
21858 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21860 initializer
21861 = cp_parser_constant_expression (parser,
21862 /*allow_non_constant_p=*/true,
21863 non_constant_p);
21865 else
21866 initializer = cp_parser_braced_list (parser, non_constant_p);
21868 return initializer;
21871 /* Parse a brace-enclosed initializer list.
21873 braced-init-list:
21874 { initializer-list , [opt] }
21877 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21878 the elements of the initializer-list (or NULL, if the last
21879 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21880 NULL_TREE. There is no way to detect whether or not the optional
21881 trailing `,' was provided. NON_CONSTANT_P is as for
21882 cp_parser_initializer. */
21884 static cp_expr
21885 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21887 tree initializer;
21888 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21890 /* Consume the `{' token. */
21891 matching_braces braces;
21892 braces.consume_open (parser);
21893 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21894 initializer = make_node (CONSTRUCTOR);
21895 /* If it's not a `}', then there is a non-trivial initializer. */
21896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21898 /* Parse the initializer list. */
21899 CONSTRUCTOR_ELTS (initializer)
21900 = cp_parser_initializer_list (parser, non_constant_p);
21901 /* A trailing `,' token is allowed. */
21902 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21903 cp_lexer_consume_token (parser->lexer);
21905 else
21906 *non_constant_p = false;
21907 /* Now, there should be a trailing `}'. */
21908 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21909 braces.require_close (parser);
21910 TREE_TYPE (initializer) = init_list_type_node;
21912 cp_expr result (initializer);
21913 /* Build a location of the form:
21914 { ... }
21915 ^~~~~~~
21916 with caret==start at the open brace, finish at the close brace. */
21917 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21918 result.set_location (combined_loc);
21919 return result;
21922 /* Consume tokens up to, and including, the next non-nested closing `]'.
21923 Returns true iff we found a closing `]'. */
21925 static bool
21926 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21928 unsigned square_depth = 0;
21930 while (true)
21932 cp_token * token = cp_lexer_peek_token (parser->lexer);
21934 switch (token->type)
21936 case CPP_EOF:
21937 case CPP_PRAGMA_EOL:
21938 /* If we've run out of tokens, then there is no closing `]'. */
21939 return false;
21941 case CPP_OPEN_SQUARE:
21942 ++square_depth;
21943 break;
21945 case CPP_CLOSE_SQUARE:
21946 if (!square_depth--)
21948 cp_lexer_consume_token (parser->lexer);
21949 return true;
21951 break;
21953 default:
21954 break;
21957 /* Consume the token. */
21958 cp_lexer_consume_token (parser->lexer);
21962 /* Return true if we are looking at an array-designator, false otherwise. */
21964 static bool
21965 cp_parser_array_designator_p (cp_parser *parser)
21967 /* Consume the `['. */
21968 cp_lexer_consume_token (parser->lexer);
21970 cp_lexer_save_tokens (parser->lexer);
21972 /* Skip tokens until the next token is a closing square bracket.
21973 If we find the closing `]', and the next token is a `=', then
21974 we are looking at an array designator. */
21975 bool array_designator_p
21976 = (cp_parser_skip_to_closing_square_bracket (parser)
21977 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21979 /* Roll back the tokens we skipped. */
21980 cp_lexer_rollback_tokens (parser->lexer);
21982 return array_designator_p;
21985 /* Parse an initializer-list.
21987 initializer-list:
21988 initializer-clause ... [opt]
21989 initializer-list , initializer-clause ... [opt]
21991 GNU Extension:
21993 initializer-list:
21994 designation initializer-clause ...[opt]
21995 initializer-list , designation initializer-clause ...[opt]
21997 designation:
21998 . identifier =
21999 identifier :
22000 [ constant-expression ] =
22002 Returns a vec of constructor_elt. The VALUE of each elt is an expression
22003 for the initializer. If the INDEX of the elt is non-NULL, it is the
22004 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
22005 as for cp_parser_initializer. */
22007 static vec<constructor_elt, va_gc> *
22008 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
22010 vec<constructor_elt, va_gc> *v = NULL;
22012 /* Assume all of the expressions are constant. */
22013 *non_constant_p = false;
22015 /* Parse the rest of the list. */
22016 while (true)
22018 cp_token *token;
22019 tree designator;
22020 tree initializer;
22021 bool clause_non_constant_p;
22023 /* If the next token is an identifier and the following one is a
22024 colon, we are looking at the GNU designated-initializer
22025 syntax. */
22026 if (cp_parser_allow_gnu_extensions_p (parser)
22027 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
22028 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
22030 /* Warn the user that they are using an extension. */
22031 pedwarn (input_location, OPT_Wpedantic,
22032 "ISO C++ does not allow designated initializers");
22033 /* Consume the identifier. */
22034 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22035 /* Consume the `:'. */
22036 cp_lexer_consume_token (parser->lexer);
22038 /* Also handle the C99 syntax, '. id ='. */
22039 else if (cp_parser_allow_gnu_extensions_p (parser)
22040 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
22041 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
22042 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
22044 /* Warn the user that they are using an extension. */
22045 pedwarn (input_location, OPT_Wpedantic,
22046 "ISO C++ does not allow C99 designated initializers");
22047 /* Consume the `.'. */
22048 cp_lexer_consume_token (parser->lexer);
22049 /* Consume the identifier. */
22050 designator = cp_lexer_consume_token (parser->lexer)->u.value;
22051 /* Consume the `='. */
22052 cp_lexer_consume_token (parser->lexer);
22054 /* Also handle C99 array designators, '[ const ] ='. */
22055 else if (cp_parser_allow_gnu_extensions_p (parser)
22056 && !c_dialect_objc ()
22057 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
22059 /* In C++11, [ could start a lambda-introducer. */
22060 bool non_const = false;
22062 cp_parser_parse_tentatively (parser);
22064 if (!cp_parser_array_designator_p (parser))
22066 cp_parser_simulate_error (parser);
22067 designator = NULL_TREE;
22069 else
22071 designator = cp_parser_constant_expression (parser, true,
22072 &non_const);
22073 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22074 cp_parser_require (parser, CPP_EQ, RT_EQ);
22077 if (!cp_parser_parse_definitely (parser))
22078 designator = NULL_TREE;
22079 else if (non_const)
22080 require_potential_rvalue_constant_expression (designator);
22082 else
22083 designator = NULL_TREE;
22085 /* Parse the initializer. */
22086 initializer = cp_parser_initializer_clause (parser,
22087 &clause_non_constant_p);
22088 /* If any clause is non-constant, so is the entire initializer. */
22089 if (clause_non_constant_p)
22090 *non_constant_p = true;
22092 /* If we have an ellipsis, this is an initializer pack
22093 expansion. */
22094 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22096 /* Consume the `...'. */
22097 cp_lexer_consume_token (parser->lexer);
22099 /* Turn the initializer into an initializer expansion. */
22100 initializer = make_pack_expansion (initializer);
22103 /* Add it to the vector. */
22104 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
22106 /* If the next token is not a comma, we have reached the end of
22107 the list. */
22108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
22109 break;
22111 /* Peek at the next token. */
22112 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22113 /* If the next token is a `}', then we're still done. An
22114 initializer-clause can have a trailing `,' after the
22115 initializer-list and before the closing `}'. */
22116 if (token->type == CPP_CLOSE_BRACE)
22117 break;
22119 /* Consume the `,' token. */
22120 cp_lexer_consume_token (parser->lexer);
22123 return v;
22126 /* Classes [gram.class] */
22128 /* Parse a class-name.
22130 class-name:
22131 identifier
22132 template-id
22134 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22135 to indicate that names looked up in dependent types should be
22136 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22137 keyword has been used to indicate that the name that appears next
22138 is a template. TAG_TYPE indicates the explicit tag given before
22139 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22140 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22141 is the class being defined in a class-head. If ENUM_OK is TRUE,
22142 enum-names are also accepted.
22144 Returns the TYPE_DECL representing the class. */
22146 static tree
22147 cp_parser_class_name (cp_parser *parser,
22148 bool typename_keyword_p,
22149 bool template_keyword_p,
22150 enum tag_types tag_type,
22151 bool check_dependency_p,
22152 bool class_head_p,
22153 bool is_declaration,
22154 bool enum_ok)
22156 tree decl;
22157 tree scope;
22158 bool typename_p;
22159 cp_token *token;
22160 tree identifier = NULL_TREE;
22162 /* All class-names start with an identifier. */
22163 token = cp_lexer_peek_token (parser->lexer);
22164 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22166 cp_parser_error (parser, "expected class-name");
22167 return error_mark_node;
22170 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22171 to a template-id, so we save it here. */
22172 scope = parser->scope;
22173 if (scope == error_mark_node)
22174 return error_mark_node;
22176 /* Any name names a type if we're following the `typename' keyword
22177 in a qualified name where the enclosing scope is type-dependent. */
22178 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22179 && dependent_type_p (scope));
22180 /* Handle the common case (an identifier, but not a template-id)
22181 efficiently. */
22182 if (token->type == CPP_NAME
22183 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22185 cp_token *identifier_token;
22186 bool ambiguous_p;
22188 /* Look for the identifier. */
22189 identifier_token = cp_lexer_peek_token (parser->lexer);
22190 ambiguous_p = identifier_token->error_reported;
22191 identifier = cp_parser_identifier (parser);
22192 /* If the next token isn't an identifier, we are certainly not
22193 looking at a class-name. */
22194 if (identifier == error_mark_node)
22195 decl = error_mark_node;
22196 /* If we know this is a type-name, there's no need to look it
22197 up. */
22198 else if (typename_p)
22199 decl = identifier;
22200 else
22202 tree ambiguous_decls;
22203 /* If we already know that this lookup is ambiguous, then
22204 we've already issued an error message; there's no reason
22205 to check again. */
22206 if (ambiguous_p)
22208 cp_parser_simulate_error (parser);
22209 return error_mark_node;
22211 /* If the next token is a `::', then the name must be a type
22212 name.
22214 [basic.lookup.qual]
22216 During the lookup for a name preceding the :: scope
22217 resolution operator, object, function, and enumerator
22218 names are ignored. */
22219 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22220 tag_type = scope_type;
22221 /* Look up the name. */
22222 decl = cp_parser_lookup_name (parser, identifier,
22223 tag_type,
22224 /*is_template=*/false,
22225 /*is_namespace=*/false,
22226 check_dependency_p,
22227 &ambiguous_decls,
22228 identifier_token->location);
22229 if (ambiguous_decls)
22231 if (cp_parser_parsing_tentatively (parser))
22232 cp_parser_simulate_error (parser);
22233 return error_mark_node;
22237 else
22239 /* Try a template-id. */
22240 decl = cp_parser_template_id (parser, template_keyword_p,
22241 check_dependency_p,
22242 tag_type,
22243 is_declaration);
22244 if (decl == error_mark_node)
22245 return error_mark_node;
22248 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22250 /* If this is a typename, create a TYPENAME_TYPE. */
22251 if (typename_p && decl != error_mark_node)
22253 decl = make_typename_type (scope, decl, typename_type,
22254 /*complain=*/tf_error);
22255 if (decl != error_mark_node)
22256 decl = TYPE_NAME (decl);
22259 decl = strip_using_decl (decl);
22261 /* Check to see that it is really the name of a class. */
22262 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22263 && identifier_p (TREE_OPERAND (decl, 0))
22264 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22265 /* Situations like this:
22267 template <typename T> struct A {
22268 typename T::template X<int>::I i;
22271 are problematic. Is `T::template X<int>' a class-name? The
22272 standard does not seem to be definitive, but there is no other
22273 valid interpretation of the following `::'. Therefore, those
22274 names are considered class-names. */
22276 decl = make_typename_type (scope, decl, tag_type, tf_error);
22277 if (decl != error_mark_node)
22278 decl = TYPE_NAME (decl);
22280 else if (TREE_CODE (decl) != TYPE_DECL
22281 || TREE_TYPE (decl) == error_mark_node
22282 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22283 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22284 /* In Objective-C 2.0, a classname followed by '.' starts a
22285 dot-syntax expression, and it's not a type-name. */
22286 || (c_dialect_objc ()
22287 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22288 && objc_is_class_name (decl)))
22289 decl = error_mark_node;
22291 if (decl == error_mark_node)
22292 cp_parser_error (parser, "expected class-name");
22293 else if (identifier && !parser->scope)
22294 maybe_note_name_used_in_class (identifier, decl);
22296 return decl;
22299 /* Parse a class-specifier.
22301 class-specifier:
22302 class-head { member-specification [opt] }
22304 Returns the TREE_TYPE representing the class. */
22306 static tree
22307 cp_parser_class_specifier_1 (cp_parser* parser)
22309 tree type;
22310 tree attributes = NULL_TREE;
22311 bool nested_name_specifier_p;
22312 unsigned saved_num_template_parameter_lists;
22313 bool saved_in_function_body;
22314 unsigned char in_statement;
22315 bool in_switch_statement_p;
22316 bool saved_in_unbraced_linkage_specification_p;
22317 tree old_scope = NULL_TREE;
22318 tree scope = NULL_TREE;
22319 cp_token *closing_brace;
22321 push_deferring_access_checks (dk_no_deferred);
22323 /* Parse the class-head. */
22324 type = cp_parser_class_head (parser,
22325 &nested_name_specifier_p);
22326 /* If the class-head was a semantic disaster, skip the entire body
22327 of the class. */
22328 if (!type)
22330 cp_parser_skip_to_end_of_block_or_statement (parser);
22331 pop_deferring_access_checks ();
22332 return error_mark_node;
22335 /* Look for the `{'. */
22336 matching_braces braces;
22337 if (!braces.require_open (parser))
22339 pop_deferring_access_checks ();
22340 return error_mark_node;
22343 cp_ensure_no_omp_declare_simd (parser);
22344 cp_ensure_no_oacc_routine (parser);
22346 /* Issue an error message if type-definitions are forbidden here. */
22347 cp_parser_check_type_definition (parser);
22348 /* Remember that we are defining one more class. */
22349 ++parser->num_classes_being_defined;
22350 /* Inside the class, surrounding template-parameter-lists do not
22351 apply. */
22352 saved_num_template_parameter_lists
22353 = parser->num_template_parameter_lists;
22354 parser->num_template_parameter_lists = 0;
22355 /* We are not in a function body. */
22356 saved_in_function_body = parser->in_function_body;
22357 parser->in_function_body = false;
22358 /* Or in a loop. */
22359 in_statement = parser->in_statement;
22360 parser->in_statement = 0;
22361 /* Or in a switch. */
22362 in_switch_statement_p = parser->in_switch_statement_p;
22363 parser->in_switch_statement_p = false;
22364 /* We are not immediately inside an extern "lang" block. */
22365 saved_in_unbraced_linkage_specification_p
22366 = parser->in_unbraced_linkage_specification_p;
22367 parser->in_unbraced_linkage_specification_p = false;
22369 // Associate constraints with the type.
22370 if (flag_concepts)
22371 type = associate_classtype_constraints (type);
22373 /* Start the class. */
22374 if (nested_name_specifier_p)
22376 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22377 old_scope = push_inner_scope (scope);
22379 type = begin_class_definition (type);
22381 if (type == error_mark_node)
22382 /* If the type is erroneous, skip the entire body of the class. */
22383 cp_parser_skip_to_closing_brace (parser);
22384 else
22385 /* Parse the member-specification. */
22386 cp_parser_member_specification_opt (parser);
22388 /* Look for the trailing `}'. */
22389 closing_brace = braces.require_close (parser);
22390 /* Look for trailing attributes to apply to this class. */
22391 if (cp_parser_allow_gnu_extensions_p (parser))
22392 attributes = cp_parser_gnu_attributes_opt (parser);
22393 if (type != error_mark_node)
22394 type = finish_struct (type, attributes);
22395 if (nested_name_specifier_p)
22396 pop_inner_scope (old_scope, scope);
22398 /* We've finished a type definition. Check for the common syntax
22399 error of forgetting a semicolon after the definition. We need to
22400 be careful, as we can't just check for not-a-semicolon and be done
22401 with it; the user might have typed:
22403 class X { } c = ...;
22404 class X { } *p = ...;
22406 and so forth. Instead, enumerate all the possible tokens that
22407 might follow this production; if we don't see one of them, then
22408 complain and silently insert the semicolon. */
22410 cp_token *token = cp_lexer_peek_token (parser->lexer);
22411 bool want_semicolon = true;
22413 if (cp_next_tokens_can_be_std_attribute_p (parser))
22414 /* Don't try to parse c++11 attributes here. As per the
22415 grammar, that should be a task for
22416 cp_parser_decl_specifier_seq. */
22417 want_semicolon = false;
22419 switch (token->type)
22421 case CPP_NAME:
22422 case CPP_SEMICOLON:
22423 case CPP_MULT:
22424 case CPP_AND:
22425 case CPP_OPEN_PAREN:
22426 case CPP_CLOSE_PAREN:
22427 case CPP_COMMA:
22428 want_semicolon = false;
22429 break;
22431 /* While it's legal for type qualifiers and storage class
22432 specifiers to follow type definitions in the grammar, only
22433 compiler testsuites contain code like that. Assume that if
22434 we see such code, then what we're really seeing is a case
22435 like:
22437 class X { }
22438 const <type> var = ...;
22442 class Y { }
22443 static <type> func (...) ...
22445 i.e. the qualifier or specifier applies to the next
22446 declaration. To do so, however, we need to look ahead one
22447 more token to see if *that* token is a type specifier.
22449 This code could be improved to handle:
22451 class Z { }
22452 static const <type> var = ...; */
22453 case CPP_KEYWORD:
22454 if (keyword_is_decl_specifier (token->keyword))
22456 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22458 /* Handling user-defined types here would be nice, but very
22459 tricky. */
22460 want_semicolon
22461 = (lookahead->type == CPP_KEYWORD
22462 && keyword_begins_type_specifier (lookahead->keyword));
22464 break;
22465 default:
22466 break;
22469 /* If we don't have a type, then something is very wrong and we
22470 shouldn't try to do anything clever. Likewise for not seeing the
22471 closing brace. */
22472 if (closing_brace && TYPE_P (type) && want_semicolon)
22474 /* Locate the closing brace. */
22475 cp_token_position prev
22476 = cp_lexer_previous_token_position (parser->lexer);
22477 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22478 location_t loc = prev_token->location;
22480 /* We want to suggest insertion of a ';' immediately *after* the
22481 closing brace, so, if we can, offset the location by 1 column. */
22482 location_t next_loc = loc;
22483 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22484 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22486 rich_location richloc (line_table, next_loc);
22488 /* If we successfully offset the location, suggest the fix-it. */
22489 if (next_loc != loc)
22490 richloc.add_fixit_insert_before (next_loc, ";");
22492 if (CLASSTYPE_DECLARED_CLASS (type))
22493 error_at_rich_loc (&richloc,
22494 "expected %<;%> after class definition");
22495 else if (TREE_CODE (type) == RECORD_TYPE)
22496 error_at_rich_loc (&richloc,
22497 "expected %<;%> after struct definition");
22498 else if (TREE_CODE (type) == UNION_TYPE)
22499 error_at_rich_loc (&richloc,
22500 "expected %<;%> after union definition");
22501 else
22502 gcc_unreachable ();
22504 /* Unget one token and smash it to look as though we encountered
22505 a semicolon in the input stream. */
22506 cp_lexer_set_token_position (parser->lexer, prev);
22507 token = cp_lexer_peek_token (parser->lexer);
22508 token->type = CPP_SEMICOLON;
22509 token->keyword = RID_MAX;
22513 /* If this class is not itself within the scope of another class,
22514 then we need to parse the bodies of all of the queued function
22515 definitions. Note that the queued functions defined in a class
22516 are not always processed immediately following the
22517 class-specifier for that class. Consider:
22519 struct A {
22520 struct B { void f() { sizeof (A); } };
22523 If `f' were processed before the processing of `A' were
22524 completed, there would be no way to compute the size of `A'.
22525 Note that the nesting we are interested in here is lexical --
22526 not the semantic nesting given by TYPE_CONTEXT. In particular,
22527 for:
22529 struct A { struct B; };
22530 struct A::B { void f() { } };
22532 there is no need to delay the parsing of `A::B::f'. */
22533 if (--parser->num_classes_being_defined == 0)
22535 tree decl;
22536 tree class_type = NULL_TREE;
22537 tree pushed_scope = NULL_TREE;
22538 unsigned ix;
22539 cp_default_arg_entry *e;
22540 tree save_ccp, save_ccr;
22542 /* In a first pass, parse default arguments to the functions.
22543 Then, in a second pass, parse the bodies of the functions.
22544 This two-phased approach handles cases like:
22546 struct S {
22547 void f() { g(); }
22548 void g(int i = 3);
22552 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22554 decl = e->decl;
22555 /* If there are default arguments that have not yet been processed,
22556 take care of them now. */
22557 if (class_type != e->class_type)
22559 if (pushed_scope)
22560 pop_scope (pushed_scope);
22561 class_type = e->class_type;
22562 pushed_scope = push_scope (class_type);
22564 /* Make sure that any template parameters are in scope. */
22565 maybe_begin_member_template_processing (decl);
22566 /* Parse the default argument expressions. */
22567 cp_parser_late_parsing_default_args (parser, decl);
22568 /* Remove any template parameters from the symbol table. */
22569 maybe_end_member_template_processing ();
22571 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22572 /* Now parse any NSDMIs. */
22573 save_ccp = current_class_ptr;
22574 save_ccr = current_class_ref;
22575 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22577 if (class_type != DECL_CONTEXT (decl))
22579 if (pushed_scope)
22580 pop_scope (pushed_scope);
22581 class_type = DECL_CONTEXT (decl);
22582 pushed_scope = push_scope (class_type);
22584 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22585 cp_parser_late_parsing_nsdmi (parser, decl);
22587 vec_safe_truncate (unparsed_nsdmis, 0);
22588 current_class_ptr = save_ccp;
22589 current_class_ref = save_ccr;
22590 if (pushed_scope)
22591 pop_scope (pushed_scope);
22593 /* Now do some post-NSDMI bookkeeping. */
22594 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22595 after_nsdmi_defaulted_late_checks (class_type);
22596 vec_safe_truncate (unparsed_classes, 0);
22597 after_nsdmi_defaulted_late_checks (type);
22599 /* Now parse the body of the functions. */
22600 if (flag_openmp)
22602 /* OpenMP UDRs need to be parsed before all other functions. */
22603 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22604 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22605 cp_parser_late_parsing_for_member (parser, decl);
22606 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22607 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22608 cp_parser_late_parsing_for_member (parser, decl);
22610 else
22611 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22612 cp_parser_late_parsing_for_member (parser, decl);
22613 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22615 else
22616 vec_safe_push (unparsed_classes, type);
22618 /* Put back any saved access checks. */
22619 pop_deferring_access_checks ();
22621 /* Restore saved state. */
22622 parser->in_switch_statement_p = in_switch_statement_p;
22623 parser->in_statement = in_statement;
22624 parser->in_function_body = saved_in_function_body;
22625 parser->num_template_parameter_lists
22626 = saved_num_template_parameter_lists;
22627 parser->in_unbraced_linkage_specification_p
22628 = saved_in_unbraced_linkage_specification_p;
22630 return type;
22633 static tree
22634 cp_parser_class_specifier (cp_parser* parser)
22636 tree ret;
22637 timevar_push (TV_PARSE_STRUCT);
22638 ret = cp_parser_class_specifier_1 (parser);
22639 timevar_pop (TV_PARSE_STRUCT);
22640 return ret;
22643 /* Parse a class-head.
22645 class-head:
22646 class-key identifier [opt] base-clause [opt]
22647 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22648 class-key nested-name-specifier [opt] template-id
22649 base-clause [opt]
22651 class-virt-specifier:
22652 final
22654 GNU Extensions:
22655 class-key attributes identifier [opt] base-clause [opt]
22656 class-key attributes nested-name-specifier identifier base-clause [opt]
22657 class-key attributes nested-name-specifier [opt] template-id
22658 base-clause [opt]
22660 Upon return BASES is initialized to the list of base classes (or
22661 NULL, if there are none) in the same form returned by
22662 cp_parser_base_clause.
22664 Returns the TYPE of the indicated class. Sets
22665 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22666 involving a nested-name-specifier was used, and FALSE otherwise.
22668 Returns error_mark_node if this is not a class-head.
22670 Returns NULL_TREE if the class-head is syntactically valid, but
22671 semantically invalid in a way that means we should skip the entire
22672 body of the class. */
22674 static tree
22675 cp_parser_class_head (cp_parser* parser,
22676 bool* nested_name_specifier_p)
22678 tree nested_name_specifier;
22679 enum tag_types class_key;
22680 tree id = NULL_TREE;
22681 tree type = NULL_TREE;
22682 tree attributes;
22683 tree bases;
22684 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22685 bool template_id_p = false;
22686 bool qualified_p = false;
22687 bool invalid_nested_name_p = false;
22688 bool invalid_explicit_specialization_p = false;
22689 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22690 tree pushed_scope = NULL_TREE;
22691 unsigned num_templates;
22692 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22693 /* Assume no nested-name-specifier will be present. */
22694 *nested_name_specifier_p = false;
22695 /* Assume no template parameter lists will be used in defining the
22696 type. */
22697 num_templates = 0;
22698 parser->colon_corrects_to_scope_p = false;
22700 /* Look for the class-key. */
22701 class_key = cp_parser_class_key (parser);
22702 if (class_key == none_type)
22703 return error_mark_node;
22705 location_t class_head_start_location = input_location;
22707 /* Parse the attributes. */
22708 attributes = cp_parser_attributes_opt (parser);
22710 /* If the next token is `::', that is invalid -- but sometimes
22711 people do try to write:
22713 struct ::S {};
22715 Handle this gracefully by accepting the extra qualifier, and then
22716 issuing an error about it later if this really is a
22717 class-head. If it turns out just to be an elaborated type
22718 specifier, remain silent. */
22719 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22720 qualified_p = true;
22722 push_deferring_access_checks (dk_no_check);
22724 /* Determine the name of the class. Begin by looking for an
22725 optional nested-name-specifier. */
22726 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22727 nested_name_specifier
22728 = cp_parser_nested_name_specifier_opt (parser,
22729 /*typename_keyword_p=*/false,
22730 /*check_dependency_p=*/false,
22731 /*type_p=*/true,
22732 /*is_declaration=*/false);
22733 /* If there was a nested-name-specifier, then there *must* be an
22734 identifier. */
22736 cp_token *bad_template_keyword = NULL;
22738 if (nested_name_specifier)
22740 type_start_token = cp_lexer_peek_token (parser->lexer);
22741 /* Although the grammar says `identifier', it really means
22742 `class-name' or `template-name'. You are only allowed to
22743 define a class that has already been declared with this
22744 syntax.
22746 The proposed resolution for Core Issue 180 says that wherever
22747 you see `class T::X' you should treat `X' as a type-name.
22749 It is OK to define an inaccessible class; for example:
22751 class A { class B; };
22752 class A::B {};
22754 We do not know if we will see a class-name, or a
22755 template-name. We look for a class-name first, in case the
22756 class-name is a template-id; if we looked for the
22757 template-name first we would stop after the template-name. */
22758 cp_parser_parse_tentatively (parser);
22759 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22760 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22761 type = cp_parser_class_name (parser,
22762 /*typename_keyword_p=*/false,
22763 /*template_keyword_p=*/false,
22764 class_type,
22765 /*check_dependency_p=*/false,
22766 /*class_head_p=*/true,
22767 /*is_declaration=*/false);
22768 /* If that didn't work, ignore the nested-name-specifier. */
22769 if (!cp_parser_parse_definitely (parser))
22771 invalid_nested_name_p = true;
22772 type_start_token = cp_lexer_peek_token (parser->lexer);
22773 id = cp_parser_identifier (parser);
22774 if (id == error_mark_node)
22775 id = NULL_TREE;
22777 /* If we could not find a corresponding TYPE, treat this
22778 declaration like an unqualified declaration. */
22779 if (type == error_mark_node)
22780 nested_name_specifier = NULL_TREE;
22781 /* Otherwise, count the number of templates used in TYPE and its
22782 containing scopes. */
22783 else
22785 tree scope;
22787 for (scope = TREE_TYPE (type);
22788 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22789 scope = get_containing_scope (scope))
22790 if (TYPE_P (scope)
22791 && CLASS_TYPE_P (scope)
22792 && CLASSTYPE_TEMPLATE_INFO (scope)
22793 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22794 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22795 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22796 ++num_templates;
22799 /* Otherwise, the identifier is optional. */
22800 else
22802 /* We don't know whether what comes next is a template-id,
22803 an identifier, or nothing at all. */
22804 cp_parser_parse_tentatively (parser);
22805 /* Check for a template-id. */
22806 type_start_token = cp_lexer_peek_token (parser->lexer);
22807 id = cp_parser_template_id (parser,
22808 /*template_keyword_p=*/false,
22809 /*check_dependency_p=*/true,
22810 class_key,
22811 /*is_declaration=*/true);
22812 /* If that didn't work, it could still be an identifier. */
22813 if (!cp_parser_parse_definitely (parser))
22815 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22817 type_start_token = cp_lexer_peek_token (parser->lexer);
22818 id = cp_parser_identifier (parser);
22820 else
22821 id = NULL_TREE;
22823 else
22825 template_id_p = true;
22826 ++num_templates;
22830 pop_deferring_access_checks ();
22832 if (id)
22834 cp_parser_check_for_invalid_template_id (parser, id,
22835 class_key,
22836 type_start_token->location);
22838 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22840 /* If it's not a `:' or a `{' then we can't really be looking at a
22841 class-head, since a class-head only appears as part of a
22842 class-specifier. We have to detect this situation before calling
22843 xref_tag, since that has irreversible side-effects. */
22844 if (!cp_parser_next_token_starts_class_definition_p (parser))
22846 cp_parser_error (parser, "expected %<{%> or %<:%>");
22847 type = error_mark_node;
22848 goto out;
22851 /* At this point, we're going ahead with the class-specifier, even
22852 if some other problem occurs. */
22853 cp_parser_commit_to_tentative_parse (parser);
22854 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22856 cp_parser_error (parser,
22857 "cannot specify %<override%> for a class");
22858 type = error_mark_node;
22859 goto out;
22861 /* Issue the error about the overly-qualified name now. */
22862 if (qualified_p)
22864 cp_parser_error (parser,
22865 "global qualification of class name is invalid");
22866 type = error_mark_node;
22867 goto out;
22869 else if (invalid_nested_name_p)
22871 cp_parser_error (parser,
22872 "qualified name does not name a class");
22873 type = error_mark_node;
22874 goto out;
22876 else if (nested_name_specifier)
22878 tree scope;
22880 if (bad_template_keyword)
22881 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22882 keyword template shall not appear at the top level. */
22883 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22884 "keyword %<template%> not allowed in class-head-name");
22886 /* Reject typedef-names in class heads. */
22887 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22889 error_at (type_start_token->location,
22890 "invalid class name in declaration of %qD",
22891 type);
22892 type = NULL_TREE;
22893 goto done;
22896 /* Figure out in what scope the declaration is being placed. */
22897 scope = current_scope ();
22898 /* If that scope does not contain the scope in which the
22899 class was originally declared, the program is invalid. */
22900 if (scope && !is_ancestor (scope, nested_name_specifier))
22902 if (at_namespace_scope_p ())
22903 error_at (type_start_token->location,
22904 "declaration of %qD in namespace %qD which does not "
22905 "enclose %qD",
22906 type, scope, nested_name_specifier);
22907 else
22908 error_at (type_start_token->location,
22909 "declaration of %qD in %qD which does not enclose %qD",
22910 type, scope, nested_name_specifier);
22911 type = NULL_TREE;
22912 goto done;
22914 /* [dcl.meaning]
22916 A declarator-id shall not be qualified except for the
22917 definition of a ... nested class outside of its class
22918 ... [or] the definition or explicit instantiation of a
22919 class member of a namespace outside of its namespace. */
22920 if (scope == nested_name_specifier)
22922 permerror (nested_name_specifier_token_start->location,
22923 "extra qualification not allowed");
22924 nested_name_specifier = NULL_TREE;
22925 num_templates = 0;
22928 /* An explicit-specialization must be preceded by "template <>". If
22929 it is not, try to recover gracefully. */
22930 if (at_namespace_scope_p ()
22931 && parser->num_template_parameter_lists == 0
22932 && !processing_template_parmlist
22933 && template_id_p)
22935 /* Build a location of this form:
22936 struct typename <ARGS>
22937 ^~~~~~~~~~~~~~~~~~~~~~
22938 with caret==start at the start token, and
22939 finishing at the end of the type. */
22940 location_t reported_loc
22941 = make_location (class_head_start_location,
22942 class_head_start_location,
22943 get_finish (type_start_token->location));
22944 rich_location richloc (line_table, reported_loc);
22945 richloc.add_fixit_insert_before (class_head_start_location,
22946 "template <> ");
22947 error_at_rich_loc
22948 (&richloc,
22949 "an explicit specialization must be preceded by %<template <>%>");
22950 invalid_explicit_specialization_p = true;
22951 /* Take the same action that would have been taken by
22952 cp_parser_explicit_specialization. */
22953 ++parser->num_template_parameter_lists;
22954 begin_specialization ();
22956 /* There must be no "return" statements between this point and the
22957 end of this function; set "type "to the correct return value and
22958 use "goto done;" to return. */
22959 /* Make sure that the right number of template parameters were
22960 present. */
22961 if (!cp_parser_check_template_parameters (parser, num_templates,
22962 type_start_token->location,
22963 /*declarator=*/NULL))
22965 /* If something went wrong, there is no point in even trying to
22966 process the class-definition. */
22967 type = NULL_TREE;
22968 goto done;
22971 /* Look up the type. */
22972 if (template_id_p)
22974 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22975 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22976 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22978 error_at (type_start_token->location,
22979 "function template %qD redeclared as a class template", id);
22980 type = error_mark_node;
22982 else
22984 type = TREE_TYPE (id);
22985 type = maybe_process_partial_specialization (type);
22987 /* Check the scope while we still know whether or not we had a
22988 nested-name-specifier. */
22989 if (type != error_mark_node)
22990 check_unqualified_spec_or_inst (type, type_start_token->location);
22992 if (nested_name_specifier)
22993 pushed_scope = push_scope (nested_name_specifier);
22995 else if (nested_name_specifier)
22997 tree class_type;
22999 /* Given:
23001 template <typename T> struct S { struct T };
23002 template <typename T> struct S<T>::T { };
23004 we will get a TYPENAME_TYPE when processing the definition of
23005 `S::T'. We need to resolve it to the actual type before we
23006 try to define it. */
23007 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
23009 class_type = resolve_typename_type (TREE_TYPE (type),
23010 /*only_current_p=*/false);
23011 if (TREE_CODE (class_type) != TYPENAME_TYPE)
23012 type = TYPE_NAME (class_type);
23013 else
23015 cp_parser_error (parser, "could not resolve typename type");
23016 type = error_mark_node;
23020 if (maybe_process_partial_specialization (TREE_TYPE (type))
23021 == error_mark_node)
23023 type = NULL_TREE;
23024 goto done;
23027 class_type = current_class_type;
23028 /* Enter the scope indicated by the nested-name-specifier. */
23029 pushed_scope = push_scope (nested_name_specifier);
23030 /* Get the canonical version of this type. */
23031 type = TYPE_MAIN_DECL (TREE_TYPE (type));
23032 /* Call push_template_decl if it seems like we should be defining a
23033 template either from the template headers or the type we're
23034 defining, so that we diagnose both extra and missing headers. */
23035 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
23036 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
23037 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
23039 type = push_template_decl (type);
23040 if (type == error_mark_node)
23042 type = NULL_TREE;
23043 goto done;
23047 type = TREE_TYPE (type);
23048 *nested_name_specifier_p = true;
23050 else /* The name is not a nested name. */
23052 /* If the class was unnamed, create a dummy name. */
23053 if (!id)
23054 id = make_anon_name ();
23055 tag_scope tag_scope = (parser->in_type_id_in_expr_p
23056 ? ts_within_enclosing_non_class
23057 : ts_current);
23058 type = xref_tag (class_key, id, tag_scope,
23059 parser->num_template_parameter_lists);
23062 /* Indicate whether this class was declared as a `class' or as a
23063 `struct'. */
23064 if (TREE_CODE (type) == RECORD_TYPE)
23065 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
23066 cp_parser_check_class_key (class_key, type);
23068 /* If this type was already complete, and we see another definition,
23069 that's an error. */
23070 if (type != error_mark_node && COMPLETE_TYPE_P (type))
23072 error_at (type_start_token->location, "redefinition of %q#T",
23073 type);
23074 inform (location_of (type), "previous definition of %q#T",
23075 type);
23076 type = NULL_TREE;
23077 goto done;
23079 else if (type == error_mark_node)
23080 type = NULL_TREE;
23082 if (type)
23084 /* Apply attributes now, before any use of the class as a template
23085 argument in its base list. */
23086 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
23087 fixup_attribute_variants (type);
23090 /* We will have entered the scope containing the class; the names of
23091 base classes should be looked up in that context. For example:
23093 struct A { struct B {}; struct C; };
23094 struct A::C : B {};
23096 is valid. */
23098 /* Get the list of base-classes, if there is one. */
23099 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
23101 /* PR59482: enter the class scope so that base-specifiers are looked
23102 up correctly. */
23103 if (type)
23104 pushclass (type);
23105 bases = cp_parser_base_clause (parser);
23106 /* PR59482: get out of the previously pushed class scope so that the
23107 subsequent pops pop the right thing. */
23108 if (type)
23109 popclass ();
23111 else
23112 bases = NULL_TREE;
23114 /* If we're really defining a class, process the base classes.
23115 If they're invalid, fail. */
23116 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23117 xref_basetypes (type, bases);
23119 done:
23120 /* Leave the scope given by the nested-name-specifier. We will
23121 enter the class scope itself while processing the members. */
23122 if (pushed_scope)
23123 pop_scope (pushed_scope);
23125 if (invalid_explicit_specialization_p)
23127 end_specialization ();
23128 --parser->num_template_parameter_lists;
23131 if (type)
23132 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23133 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23134 CLASSTYPE_FINAL (type) = 1;
23135 out:
23136 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23137 return type;
23140 /* Parse a class-key.
23142 class-key:
23143 class
23144 struct
23145 union
23147 Returns the kind of class-key specified, or none_type to indicate
23148 error. */
23150 static enum tag_types
23151 cp_parser_class_key (cp_parser* parser)
23153 cp_token *token;
23154 enum tag_types tag_type;
23156 /* Look for the class-key. */
23157 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23158 if (!token)
23159 return none_type;
23161 /* Check to see if the TOKEN is a class-key. */
23162 tag_type = cp_parser_token_is_class_key (token);
23163 if (!tag_type)
23164 cp_parser_error (parser, "expected class-key");
23165 return tag_type;
23168 /* Parse a type-parameter-key.
23170 type-parameter-key:
23171 class
23172 typename
23175 static void
23176 cp_parser_type_parameter_key (cp_parser* parser)
23178 /* Look for the type-parameter-key. */
23179 enum tag_types tag_type = none_type;
23180 cp_token *token = cp_lexer_peek_token (parser->lexer);
23181 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23183 cp_lexer_consume_token (parser->lexer);
23184 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
23185 /* typename is not allowed in a template template parameter
23186 by the standard until C++1Z. */
23187 pedwarn (token->location, OPT_Wpedantic,
23188 "ISO C++ forbids typename key in template template parameter;"
23189 " use -std=c++1z or -std=gnu++1z");
23191 else
23192 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23194 return;
23197 /* Parse an (optional) member-specification.
23199 member-specification:
23200 member-declaration member-specification [opt]
23201 access-specifier : member-specification [opt] */
23203 static void
23204 cp_parser_member_specification_opt (cp_parser* parser)
23206 while (true)
23208 cp_token *token;
23209 enum rid keyword;
23211 /* Peek at the next token. */
23212 token = cp_lexer_peek_token (parser->lexer);
23213 /* If it's a `}', or EOF then we've seen all the members. */
23214 if (token->type == CPP_CLOSE_BRACE
23215 || token->type == CPP_EOF
23216 || token->type == CPP_PRAGMA_EOL)
23217 break;
23219 /* See if this token is a keyword. */
23220 keyword = token->keyword;
23221 switch (keyword)
23223 case RID_PUBLIC:
23224 case RID_PROTECTED:
23225 case RID_PRIVATE:
23226 /* Consume the access-specifier. */
23227 cp_lexer_consume_token (parser->lexer);
23228 /* Remember which access-specifier is active. */
23229 current_access_specifier = token->u.value;
23230 /* Look for the `:'. */
23231 cp_parser_require (parser, CPP_COLON, RT_COLON);
23232 break;
23234 default:
23235 /* Accept #pragmas at class scope. */
23236 if (token->type == CPP_PRAGMA)
23238 cp_parser_pragma (parser, pragma_member, NULL);
23239 break;
23242 /* Otherwise, the next construction must be a
23243 member-declaration. */
23244 cp_parser_member_declaration (parser);
23249 /* Parse a member-declaration.
23251 member-declaration:
23252 decl-specifier-seq [opt] member-declarator-list [opt] ;
23253 function-definition ; [opt]
23254 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23255 using-declaration
23256 template-declaration
23257 alias-declaration
23259 member-declarator-list:
23260 member-declarator
23261 member-declarator-list , member-declarator
23263 member-declarator:
23264 declarator pure-specifier [opt]
23265 declarator constant-initializer [opt]
23266 identifier [opt] : constant-expression
23268 GNU Extensions:
23270 member-declaration:
23271 __extension__ member-declaration
23273 member-declarator:
23274 declarator attributes [opt] pure-specifier [opt]
23275 declarator attributes [opt] constant-initializer [opt]
23276 identifier [opt] attributes [opt] : constant-expression
23278 C++0x Extensions:
23280 member-declaration:
23281 static_assert-declaration */
23283 static void
23284 cp_parser_member_declaration (cp_parser* parser)
23286 cp_decl_specifier_seq decl_specifiers;
23287 tree prefix_attributes;
23288 tree decl;
23289 int declares_class_or_enum;
23290 bool friend_p;
23291 cp_token *token = NULL;
23292 cp_token *decl_spec_token_start = NULL;
23293 cp_token *initializer_token_start = NULL;
23294 int saved_pedantic;
23295 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23297 /* Check for the `__extension__' keyword. */
23298 if (cp_parser_extension_opt (parser, &saved_pedantic))
23300 /* Recurse. */
23301 cp_parser_member_declaration (parser);
23302 /* Restore the old value of the PEDANTIC flag. */
23303 pedantic = saved_pedantic;
23305 return;
23308 /* Check for a template-declaration. */
23309 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23311 /* An explicit specialization here is an error condition, and we
23312 expect the specialization handler to detect and report this. */
23313 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23314 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23315 cp_parser_explicit_specialization (parser);
23316 else
23317 cp_parser_template_declaration (parser, /*member_p=*/true);
23319 return;
23321 /* Check for a template introduction. */
23322 else if (cp_parser_template_declaration_after_export (parser, true))
23323 return;
23325 /* Check for a using-declaration. */
23326 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23328 if (cxx_dialect < cxx11)
23330 /* Parse the using-declaration. */
23331 cp_parser_using_declaration (parser,
23332 /*access_declaration_p=*/false);
23333 return;
23335 else
23337 tree decl;
23338 bool alias_decl_expected;
23339 cp_parser_parse_tentatively (parser);
23340 decl = cp_parser_alias_declaration (parser);
23341 /* Note that if we actually see the '=' token after the
23342 identifier, cp_parser_alias_declaration commits the
23343 tentative parse. In that case, we really expect an
23344 alias-declaration. Otherwise, we expect a using
23345 declaration. */
23346 alias_decl_expected =
23347 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23348 cp_parser_parse_definitely (parser);
23350 if (alias_decl_expected)
23351 finish_member_declaration (decl);
23352 else
23353 cp_parser_using_declaration (parser,
23354 /*access_declaration_p=*/false);
23355 return;
23359 /* Check for @defs. */
23360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23362 tree ivar, member;
23363 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23364 ivar = ivar_chains;
23365 while (ivar)
23367 member = ivar;
23368 ivar = TREE_CHAIN (member);
23369 TREE_CHAIN (member) = NULL_TREE;
23370 finish_member_declaration (member);
23372 return;
23375 /* If the next token is `static_assert' we have a static assertion. */
23376 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23378 cp_parser_static_assert (parser, /*member_p=*/true);
23379 return;
23382 parser->colon_corrects_to_scope_p = false;
23384 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23385 goto out;
23387 /* Parse the decl-specifier-seq. */
23388 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23389 cp_parser_decl_specifier_seq (parser,
23390 CP_PARSER_FLAGS_OPTIONAL,
23391 &decl_specifiers,
23392 &declares_class_or_enum);
23393 /* Check for an invalid type-name. */
23394 if (!decl_specifiers.any_type_specifiers_p
23395 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23396 goto out;
23397 /* If there is no declarator, then the decl-specifier-seq should
23398 specify a type. */
23399 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23401 /* If there was no decl-specifier-seq, and the next token is a
23402 `;', then we have something like:
23404 struct S { ; };
23406 [class.mem]
23408 Each member-declaration shall declare at least one member
23409 name of the class. */
23410 if (!decl_specifiers.any_specifiers_p)
23412 cp_token *token = cp_lexer_peek_token (parser->lexer);
23413 if (!in_system_header_at (token->location))
23415 gcc_rich_location richloc (token->location);
23416 richloc.add_fixit_remove ();
23417 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23420 else
23422 tree type;
23424 /* See if this declaration is a friend. */
23425 friend_p = cp_parser_friend_p (&decl_specifiers);
23426 /* If there were decl-specifiers, check to see if there was
23427 a class-declaration. */
23428 type = check_tag_decl (&decl_specifiers,
23429 /*explicit_type_instantiation_p=*/false);
23430 /* Nested classes have already been added to the class, but
23431 a `friend' needs to be explicitly registered. */
23432 if (friend_p)
23434 /* If the `friend' keyword was present, the friend must
23435 be introduced with a class-key. */
23436 if (!declares_class_or_enum && cxx_dialect < cxx11)
23437 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23438 "in C++03 a class-key must be used "
23439 "when declaring a friend");
23440 /* In this case:
23442 template <typename T> struct A {
23443 friend struct A<T>::B;
23446 A<T>::B will be represented by a TYPENAME_TYPE, and
23447 therefore not recognized by check_tag_decl. */
23448 if (!type)
23450 type = decl_specifiers.type;
23451 if (type && TREE_CODE (type) == TYPE_DECL)
23452 type = TREE_TYPE (type);
23454 if (!type || !TYPE_P (type))
23455 error_at (decl_spec_token_start->location,
23456 "friend declaration does not name a class or "
23457 "function");
23458 else
23459 make_friend_class (current_class_type, type,
23460 /*complain=*/true);
23462 /* If there is no TYPE, an error message will already have
23463 been issued. */
23464 else if (!type || type == error_mark_node)
23466 /* An anonymous aggregate has to be handled specially; such
23467 a declaration really declares a data member (with a
23468 particular type), as opposed to a nested class. */
23469 else if (ANON_AGGR_TYPE_P (type))
23471 /* C++11 9.5/6. */
23472 if (decl_specifiers.storage_class != sc_none)
23473 error_at (decl_spec_token_start->location,
23474 "a storage class on an anonymous aggregate "
23475 "in class scope is not allowed");
23477 /* Remove constructors and such from TYPE, now that we
23478 know it is an anonymous aggregate. */
23479 fixup_anonymous_aggr (type);
23480 /* And make the corresponding data member. */
23481 decl = build_decl (decl_spec_token_start->location,
23482 FIELD_DECL, NULL_TREE, type);
23483 /* Add it to the class. */
23484 finish_member_declaration (decl);
23486 else
23487 cp_parser_check_access_in_redeclaration
23488 (TYPE_NAME (type),
23489 decl_spec_token_start->location);
23492 else
23494 bool assume_semicolon = false;
23496 /* Clear attributes from the decl_specifiers but keep them
23497 around as prefix attributes that apply them to the entity
23498 being declared. */
23499 prefix_attributes = decl_specifiers.attributes;
23500 decl_specifiers.attributes = NULL_TREE;
23502 /* See if these declarations will be friends. */
23503 friend_p = cp_parser_friend_p (&decl_specifiers);
23505 /* Keep going until we hit the `;' at the end of the
23506 declaration. */
23507 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23509 tree attributes = NULL_TREE;
23510 tree first_attribute;
23512 /* Peek at the next token. */
23513 token = cp_lexer_peek_token (parser->lexer);
23515 /* Check for a bitfield declaration. */
23516 if (token->type == CPP_COLON
23517 || (token->type == CPP_NAME
23518 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23519 == CPP_COLON))
23521 tree identifier;
23522 tree width;
23524 /* Get the name of the bitfield. Note that we cannot just
23525 check TOKEN here because it may have been invalidated by
23526 the call to cp_lexer_peek_nth_token above. */
23527 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23528 identifier = cp_parser_identifier (parser);
23529 else
23530 identifier = NULL_TREE;
23532 /* Consume the `:' token. */
23533 cp_lexer_consume_token (parser->lexer);
23534 /* Get the width of the bitfield. */
23535 width
23536 = cp_parser_constant_expression (parser);
23538 /* Look for attributes that apply to the bitfield. */
23539 attributes = cp_parser_attributes_opt (parser);
23540 /* Remember which attributes are prefix attributes and
23541 which are not. */
23542 first_attribute = attributes;
23543 /* Combine the attributes. */
23544 attributes = chainon (prefix_attributes, attributes);
23546 /* Create the bitfield declaration. */
23547 decl = grokbitfield (identifier
23548 ? make_id_declarator (NULL_TREE,
23549 identifier,
23550 sfk_none)
23551 : NULL,
23552 &decl_specifiers,
23553 width,
23554 attributes);
23556 else
23558 cp_declarator *declarator;
23559 tree initializer;
23560 tree asm_specification;
23561 int ctor_dtor_or_conv_p;
23563 /* Parse the declarator. */
23564 declarator
23565 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23566 &ctor_dtor_or_conv_p,
23567 /*parenthesized_p=*/NULL,
23568 /*member_p=*/true,
23569 friend_p);
23571 /* If something went wrong parsing the declarator, make sure
23572 that we at least consume some tokens. */
23573 if (declarator == cp_error_declarator)
23575 /* Skip to the end of the statement. */
23576 cp_parser_skip_to_end_of_statement (parser);
23577 /* If the next token is not a semicolon, that is
23578 probably because we just skipped over the body of
23579 a function. So, we consume a semicolon if
23580 present, but do not issue an error message if it
23581 is not present. */
23582 if (cp_lexer_next_token_is (parser->lexer,
23583 CPP_SEMICOLON))
23584 cp_lexer_consume_token (parser->lexer);
23585 goto out;
23588 if (declares_class_or_enum & 2)
23589 cp_parser_check_for_definition_in_return_type
23590 (declarator, decl_specifiers.type,
23591 decl_specifiers.locations[ds_type_spec]);
23593 /* Look for an asm-specification. */
23594 asm_specification = cp_parser_asm_specification_opt (parser);
23595 /* Look for attributes that apply to the declaration. */
23596 attributes = cp_parser_attributes_opt (parser);
23597 /* Remember which attributes are prefix attributes and
23598 which are not. */
23599 first_attribute = attributes;
23600 /* Combine the attributes. */
23601 attributes = chainon (prefix_attributes, attributes);
23603 /* If it's an `=', then we have a constant-initializer or a
23604 pure-specifier. It is not correct to parse the
23605 initializer before registering the member declaration
23606 since the member declaration should be in scope while
23607 its initializer is processed. However, the rest of the
23608 front end does not yet provide an interface that allows
23609 us to handle this correctly. */
23610 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23612 /* In [class.mem]:
23614 A pure-specifier shall be used only in the declaration of
23615 a virtual function.
23617 A member-declarator can contain a constant-initializer
23618 only if it declares a static member of integral or
23619 enumeration type.
23621 Therefore, if the DECLARATOR is for a function, we look
23622 for a pure-specifier; otherwise, we look for a
23623 constant-initializer. When we call `grokfield', it will
23624 perform more stringent semantics checks. */
23625 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23626 if (function_declarator_p (declarator)
23627 || (decl_specifiers.type
23628 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23629 && declarator->kind == cdk_id
23630 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23631 == FUNCTION_TYPE)))
23632 initializer = cp_parser_pure_specifier (parser);
23633 else if (decl_specifiers.storage_class != sc_static)
23634 initializer = cp_parser_save_nsdmi (parser);
23635 else if (cxx_dialect >= cxx11)
23637 bool nonconst;
23638 /* Don't require a constant rvalue in C++11, since we
23639 might want a reference constant. We'll enforce
23640 constancy later. */
23641 cp_lexer_consume_token (parser->lexer);
23642 /* Parse the initializer. */
23643 initializer = cp_parser_initializer_clause (parser,
23644 &nonconst);
23646 else
23647 /* Parse the initializer. */
23648 initializer = cp_parser_constant_initializer (parser);
23650 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23651 && !function_declarator_p (declarator))
23653 bool x;
23654 if (decl_specifiers.storage_class != sc_static)
23655 initializer = cp_parser_save_nsdmi (parser);
23656 else
23657 initializer = cp_parser_initializer (parser, &x, &x);
23659 /* Otherwise, there is no initializer. */
23660 else
23661 initializer = NULL_TREE;
23663 /* See if we are probably looking at a function
23664 definition. We are certainly not looking at a
23665 member-declarator. Calling `grokfield' has
23666 side-effects, so we must not do it unless we are sure
23667 that we are looking at a member-declarator. */
23668 if (cp_parser_token_starts_function_definition_p
23669 (cp_lexer_peek_token (parser->lexer)))
23671 /* The grammar does not allow a pure-specifier to be
23672 used when a member function is defined. (It is
23673 possible that this fact is an oversight in the
23674 standard, since a pure function may be defined
23675 outside of the class-specifier. */
23676 if (initializer && initializer_token_start)
23677 error_at (initializer_token_start->location,
23678 "pure-specifier on function-definition");
23679 decl = cp_parser_save_member_function_body (parser,
23680 &decl_specifiers,
23681 declarator,
23682 attributes);
23683 if (parser->fully_implicit_function_template_p)
23684 decl = finish_fully_implicit_template (parser, decl);
23685 /* If the member was not a friend, declare it here. */
23686 if (!friend_p)
23687 finish_member_declaration (decl);
23688 /* Peek at the next token. */
23689 token = cp_lexer_peek_token (parser->lexer);
23690 /* If the next token is a semicolon, consume it. */
23691 if (token->type == CPP_SEMICOLON)
23693 location_t semicolon_loc
23694 = cp_lexer_consume_token (parser->lexer)->location;
23695 gcc_rich_location richloc (semicolon_loc);
23696 richloc.add_fixit_remove ();
23697 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23698 "extra %<;%> after in-class "
23699 "function definition");
23701 goto out;
23703 else
23704 if (declarator->kind == cdk_function)
23705 declarator->id_loc = token->location;
23706 /* Create the declaration. */
23707 decl = grokfield (declarator, &decl_specifiers,
23708 initializer, /*init_const_expr_p=*/true,
23709 asm_specification, attributes);
23710 if (parser->fully_implicit_function_template_p)
23712 if (friend_p)
23713 finish_fully_implicit_template (parser, 0);
23714 else
23715 decl = finish_fully_implicit_template (parser, decl);
23719 cp_finalize_omp_declare_simd (parser, decl);
23720 cp_finalize_oacc_routine (parser, decl, false);
23722 /* Reset PREFIX_ATTRIBUTES. */
23723 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23724 attributes = TREE_CHAIN (attributes);
23725 if (attributes)
23726 TREE_CHAIN (attributes) = NULL_TREE;
23728 /* If there is any qualification still in effect, clear it
23729 now; we will be starting fresh with the next declarator. */
23730 parser->scope = NULL_TREE;
23731 parser->qualifying_scope = NULL_TREE;
23732 parser->object_scope = NULL_TREE;
23733 /* If it's a `,', then there are more declarators. */
23734 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23736 cp_lexer_consume_token (parser->lexer);
23737 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23739 cp_token *token = cp_lexer_previous_token (parser->lexer);
23740 gcc_rich_location richloc (token->location);
23741 richloc.add_fixit_remove ();
23742 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23743 "member declaration");
23746 /* If the next token isn't a `;', then we have a parse error. */
23747 else if (cp_lexer_next_token_is_not (parser->lexer,
23748 CPP_SEMICOLON))
23750 /* The next token might be a ways away from where the
23751 actual semicolon is missing. Find the previous token
23752 and use that for our error position. */
23753 cp_token *token = cp_lexer_previous_token (parser->lexer);
23754 gcc_rich_location richloc (token->location);
23755 richloc.add_fixit_insert_after (";");
23756 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23757 "member declaration");
23759 /* Assume that the user meant to provide a semicolon. If
23760 we were to cp_parser_skip_to_end_of_statement, we might
23761 skip to a semicolon inside a member function definition
23762 and issue nonsensical error messages. */
23763 assume_semicolon = true;
23766 if (decl)
23768 /* Add DECL to the list of members. */
23769 if (!friend_p
23770 /* Explicitly include, eg, NSDMIs, for better error
23771 recovery (c++/58650). */
23772 || !DECL_DECLARES_FUNCTION_P (decl))
23773 finish_member_declaration (decl);
23775 if (TREE_CODE (decl) == FUNCTION_DECL)
23776 cp_parser_save_default_args (parser, decl);
23777 else if (TREE_CODE (decl) == FIELD_DECL
23778 && !DECL_C_BIT_FIELD (decl)
23779 && DECL_INITIAL (decl))
23780 /* Add DECL to the queue of NSDMI to be parsed later. */
23781 vec_safe_push (unparsed_nsdmis, decl);
23784 if (assume_semicolon)
23785 goto out;
23789 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23790 out:
23791 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23794 /* Parse a pure-specifier.
23796 pure-specifier:
23799 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23800 Otherwise, ERROR_MARK_NODE is returned. */
23802 static tree
23803 cp_parser_pure_specifier (cp_parser* parser)
23805 cp_token *token;
23807 /* Look for the `=' token. */
23808 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23809 return error_mark_node;
23810 /* Look for the `0' token. */
23811 token = cp_lexer_peek_token (parser->lexer);
23813 if (token->type == CPP_EOF
23814 || token->type == CPP_PRAGMA_EOL)
23815 return error_mark_node;
23817 cp_lexer_consume_token (parser->lexer);
23819 /* Accept = default or = delete in c++0x mode. */
23820 if (token->keyword == RID_DEFAULT
23821 || token->keyword == RID_DELETE)
23823 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23824 return token->u.value;
23827 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23828 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23830 cp_parser_error (parser,
23831 "invalid pure specifier (only %<= 0%> is allowed)");
23832 cp_parser_skip_to_end_of_statement (parser);
23833 return error_mark_node;
23835 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23837 error_at (token->location, "templates may not be %<virtual%>");
23838 return error_mark_node;
23841 return integer_zero_node;
23844 /* Parse a constant-initializer.
23846 constant-initializer:
23847 = constant-expression
23849 Returns a representation of the constant-expression. */
23851 static tree
23852 cp_parser_constant_initializer (cp_parser* parser)
23854 /* Look for the `=' token. */
23855 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23856 return error_mark_node;
23858 /* It is invalid to write:
23860 struct S { static const int i = { 7 }; };
23863 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23865 cp_parser_error (parser,
23866 "a brace-enclosed initializer is not allowed here");
23867 /* Consume the opening brace. */
23868 matching_braces braces;
23869 braces.consume_open (parser);
23870 /* Skip the initializer. */
23871 cp_parser_skip_to_closing_brace (parser);
23872 /* Look for the trailing `}'. */
23873 braces.require_close (parser);
23875 return error_mark_node;
23878 return cp_parser_constant_expression (parser);
23881 /* Derived classes [gram.class.derived] */
23883 /* Parse a base-clause.
23885 base-clause:
23886 : base-specifier-list
23888 base-specifier-list:
23889 base-specifier ... [opt]
23890 base-specifier-list , base-specifier ... [opt]
23892 Returns a TREE_LIST representing the base-classes, in the order in
23893 which they were declared. The representation of each node is as
23894 described by cp_parser_base_specifier.
23896 In the case that no bases are specified, this function will return
23897 NULL_TREE, not ERROR_MARK_NODE. */
23899 static tree
23900 cp_parser_base_clause (cp_parser* parser)
23902 tree bases = NULL_TREE;
23904 /* Look for the `:' that begins the list. */
23905 cp_parser_require (parser, CPP_COLON, RT_COLON);
23907 /* Scan the base-specifier-list. */
23908 while (true)
23910 cp_token *token;
23911 tree base;
23912 bool pack_expansion_p = false;
23914 /* Look for the base-specifier. */
23915 base = cp_parser_base_specifier (parser);
23916 /* Look for the (optional) ellipsis. */
23917 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23919 /* Consume the `...'. */
23920 cp_lexer_consume_token (parser->lexer);
23922 pack_expansion_p = true;
23925 /* Add BASE to the front of the list. */
23926 if (base && base != error_mark_node)
23928 if (pack_expansion_p)
23929 /* Make this a pack expansion type. */
23930 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23932 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23934 TREE_CHAIN (base) = bases;
23935 bases = base;
23938 /* Peek at the next token. */
23939 token = cp_lexer_peek_token (parser->lexer);
23940 /* If it's not a comma, then the list is complete. */
23941 if (token->type != CPP_COMMA)
23942 break;
23943 /* Consume the `,'. */
23944 cp_lexer_consume_token (parser->lexer);
23947 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23948 base class had a qualified name. However, the next name that
23949 appears is certainly not qualified. */
23950 parser->scope = NULL_TREE;
23951 parser->qualifying_scope = NULL_TREE;
23952 parser->object_scope = NULL_TREE;
23954 return nreverse (bases);
23957 /* Parse a base-specifier.
23959 base-specifier:
23960 :: [opt] nested-name-specifier [opt] class-name
23961 virtual access-specifier [opt] :: [opt] nested-name-specifier
23962 [opt] class-name
23963 access-specifier virtual [opt] :: [opt] nested-name-specifier
23964 [opt] class-name
23966 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23967 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23968 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23969 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23971 static tree
23972 cp_parser_base_specifier (cp_parser* parser)
23974 cp_token *token;
23975 bool done = false;
23976 bool virtual_p = false;
23977 bool duplicate_virtual_error_issued_p = false;
23978 bool duplicate_access_error_issued_p = false;
23979 bool class_scope_p, template_p;
23980 tree access = access_default_node;
23981 tree type;
23983 /* Process the optional `virtual' and `access-specifier'. */
23984 while (!done)
23986 /* Peek at the next token. */
23987 token = cp_lexer_peek_token (parser->lexer);
23988 /* Process `virtual'. */
23989 switch (token->keyword)
23991 case RID_VIRTUAL:
23992 /* If `virtual' appears more than once, issue an error. */
23993 if (virtual_p && !duplicate_virtual_error_issued_p)
23995 cp_parser_error (parser,
23996 "%<virtual%> specified more than once in base-specifier");
23997 duplicate_virtual_error_issued_p = true;
24000 virtual_p = true;
24002 /* Consume the `virtual' token. */
24003 cp_lexer_consume_token (parser->lexer);
24005 break;
24007 case RID_PUBLIC:
24008 case RID_PROTECTED:
24009 case RID_PRIVATE:
24010 /* If more than one access specifier appears, issue an
24011 error. */
24012 if (access != access_default_node
24013 && !duplicate_access_error_issued_p)
24015 cp_parser_error (parser,
24016 "more than one access specifier in base-specifier");
24017 duplicate_access_error_issued_p = true;
24020 access = ridpointers[(int) token->keyword];
24022 /* Consume the access-specifier. */
24023 cp_lexer_consume_token (parser->lexer);
24025 break;
24027 default:
24028 done = true;
24029 break;
24032 /* It is not uncommon to see programs mechanically, erroneously, use
24033 the 'typename' keyword to denote (dependent) qualified types
24034 as base classes. */
24035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
24037 token = cp_lexer_peek_token (parser->lexer);
24038 if (!processing_template_decl)
24039 error_at (token->location,
24040 "keyword %<typename%> not allowed outside of templates");
24041 else
24042 error_at (token->location,
24043 "keyword %<typename%> not allowed in this context "
24044 "(the base class is implicitly a type)");
24045 cp_lexer_consume_token (parser->lexer);
24048 /* Look for the optional `::' operator. */
24049 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
24050 /* Look for the nested-name-specifier. The simplest way to
24051 implement:
24053 [temp.res]
24055 The keyword `typename' is not permitted in a base-specifier or
24056 mem-initializer; in these contexts a qualified name that
24057 depends on a template-parameter is implicitly assumed to be a
24058 type name.
24060 is to pretend that we have seen the `typename' keyword at this
24061 point. */
24062 cp_parser_nested_name_specifier_opt (parser,
24063 /*typename_keyword_p=*/true,
24064 /*check_dependency_p=*/true,
24065 /*type_p=*/true,
24066 /*is_declaration=*/true);
24067 /* If the base class is given by a qualified name, assume that names
24068 we see are type names or templates, as appropriate. */
24069 class_scope_p = (parser->scope && TYPE_P (parser->scope));
24070 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
24072 if (!parser->scope
24073 && cp_lexer_next_token_is_decltype (parser->lexer))
24074 /* DR 950 allows decltype as a base-specifier. */
24075 type = cp_parser_decltype (parser);
24076 else
24078 /* Otherwise, look for the class-name. */
24079 type = cp_parser_class_name (parser,
24080 class_scope_p,
24081 template_p,
24082 typename_type,
24083 /*check_dependency_p=*/true,
24084 /*class_head_p=*/false,
24085 /*is_declaration=*/true);
24086 type = TREE_TYPE (type);
24089 if (type == error_mark_node)
24090 return error_mark_node;
24092 return finish_base_specifier (type, access, virtual_p);
24095 /* Exception handling [gram.exception] */
24097 /* Parse an (optional) noexcept-specification.
24099 noexcept-specification:
24100 noexcept ( constant-expression ) [opt]
24102 If no noexcept-specification is present, returns NULL_TREE.
24103 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
24104 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
24105 there are no parentheses. CONSUMED_EXPR will be set accordingly.
24106 Otherwise, returns a noexcept specification unless RETURN_COND is true,
24107 in which case a boolean condition is returned instead. */
24109 static tree
24110 cp_parser_noexcept_specification_opt (cp_parser* parser,
24111 bool require_constexpr,
24112 bool* consumed_expr,
24113 bool return_cond)
24115 cp_token *token;
24116 const char *saved_message;
24118 /* Peek at the next token. */
24119 token = cp_lexer_peek_token (parser->lexer);
24121 /* Is it a noexcept-specification? */
24122 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24124 tree expr;
24125 cp_lexer_consume_token (parser->lexer);
24127 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24129 matching_parens parens;
24130 parens.consume_open (parser);
24132 if (require_constexpr)
24134 /* Types may not be defined in an exception-specification. */
24135 saved_message = parser->type_definition_forbidden_message;
24136 parser->type_definition_forbidden_message
24137 = G_("types may not be defined in an exception-specification");
24139 expr = cp_parser_constant_expression (parser);
24141 /* Restore the saved message. */
24142 parser->type_definition_forbidden_message = saved_message;
24144 else
24146 expr = cp_parser_expression (parser);
24147 *consumed_expr = true;
24150 parens.require_close (parser);
24152 else
24154 expr = boolean_true_node;
24155 if (!require_constexpr)
24156 *consumed_expr = false;
24159 /* We cannot build a noexcept-spec right away because this will check
24160 that expr is a constexpr. */
24161 if (!return_cond)
24162 return build_noexcept_spec (expr, tf_warning_or_error);
24163 else
24164 return expr;
24166 else
24167 return NULL_TREE;
24170 /* Parse an (optional) exception-specification.
24172 exception-specification:
24173 throw ( type-id-list [opt] )
24175 Returns a TREE_LIST representing the exception-specification. The
24176 TREE_VALUE of each node is a type. */
24178 static tree
24179 cp_parser_exception_specification_opt (cp_parser* parser)
24181 cp_token *token;
24182 tree type_id_list;
24183 const char *saved_message;
24185 /* Peek at the next token. */
24186 token = cp_lexer_peek_token (parser->lexer);
24188 /* Is it a noexcept-specification? */
24189 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24190 false);
24191 if (type_id_list != NULL_TREE)
24192 return type_id_list;
24194 /* If it's not `throw', then there's no exception-specification. */
24195 if (!cp_parser_is_keyword (token, RID_THROW))
24196 return NULL_TREE;
24198 location_t loc = token->location;
24200 /* Consume the `throw'. */
24201 cp_lexer_consume_token (parser->lexer);
24203 /* Look for the `('. */
24204 matching_parens parens;
24205 parens.require_open (parser);
24207 /* Peek at the next token. */
24208 token = cp_lexer_peek_token (parser->lexer);
24209 /* If it's not a `)', then there is a type-id-list. */
24210 if (token->type != CPP_CLOSE_PAREN)
24212 /* Types may not be defined in an exception-specification. */
24213 saved_message = parser->type_definition_forbidden_message;
24214 parser->type_definition_forbidden_message
24215 = G_("types may not be defined in an exception-specification");
24216 /* Parse the type-id-list. */
24217 type_id_list = cp_parser_type_id_list (parser);
24218 /* Restore the saved message. */
24219 parser->type_definition_forbidden_message = saved_message;
24221 if (cxx_dialect >= cxx1z)
24223 error_at (loc, "ISO C++1z does not allow dynamic exception "
24224 "specifications");
24225 type_id_list = NULL_TREE;
24227 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24228 warning_at (loc, OPT_Wdeprecated,
24229 "dynamic exception specifications are deprecated in "
24230 "C++11");
24232 /* In C++17, throw() is equivalent to noexcept (true). throw()
24233 is deprecated in C++11 and above as well, but is still widely used,
24234 so don't warn about it yet. */
24235 else if (cxx_dialect >= cxx1z)
24236 type_id_list = noexcept_true_spec;
24237 else
24238 type_id_list = empty_except_spec;
24240 /* Look for the `)'. */
24241 parens.require_close (parser);
24243 return type_id_list;
24246 /* Parse an (optional) type-id-list.
24248 type-id-list:
24249 type-id ... [opt]
24250 type-id-list , type-id ... [opt]
24252 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24253 in the order that the types were presented. */
24255 static tree
24256 cp_parser_type_id_list (cp_parser* parser)
24258 tree types = NULL_TREE;
24260 while (true)
24262 cp_token *token;
24263 tree type;
24265 token = cp_lexer_peek_token (parser->lexer);
24267 /* Get the next type-id. */
24268 type = cp_parser_type_id (parser);
24269 /* Check for invalid 'auto'. */
24270 if (flag_concepts && type_uses_auto (type))
24272 error_at (token->location,
24273 "invalid use of %<auto%> in exception-specification");
24274 type = error_mark_node;
24276 /* Parse the optional ellipsis. */
24277 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24279 /* Consume the `...'. */
24280 cp_lexer_consume_token (parser->lexer);
24282 /* Turn the type into a pack expansion expression. */
24283 type = make_pack_expansion (type);
24285 /* Add it to the list. */
24286 types = add_exception_specifier (types, type, /*complain=*/1);
24287 /* Peek at the next token. */
24288 token = cp_lexer_peek_token (parser->lexer);
24289 /* If it is not a `,', we are done. */
24290 if (token->type != CPP_COMMA)
24291 break;
24292 /* Consume the `,'. */
24293 cp_lexer_consume_token (parser->lexer);
24296 return nreverse (types);
24299 /* Parse a try-block.
24301 try-block:
24302 try compound-statement handler-seq */
24304 static tree
24305 cp_parser_try_block (cp_parser* parser)
24307 tree try_block;
24309 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24310 if (parser->in_function_body
24311 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24312 error ("%<try%> in %<constexpr%> function");
24314 try_block = begin_try_block ();
24315 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24316 finish_try_block (try_block);
24317 cp_parser_handler_seq (parser);
24318 finish_handler_sequence (try_block);
24320 return try_block;
24323 /* Parse a function-try-block.
24325 function-try-block:
24326 try ctor-initializer [opt] function-body handler-seq */
24328 static bool
24329 cp_parser_function_try_block (cp_parser* parser)
24331 tree compound_stmt;
24332 tree try_block;
24333 bool ctor_initializer_p;
24335 /* Look for the `try' keyword. */
24336 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24337 return false;
24338 /* Let the rest of the front end know where we are. */
24339 try_block = begin_function_try_block (&compound_stmt);
24340 /* Parse the function-body. */
24341 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24342 (parser, /*in_function_try_block=*/true);
24343 /* We're done with the `try' part. */
24344 finish_function_try_block (try_block);
24345 /* Parse the handlers. */
24346 cp_parser_handler_seq (parser);
24347 /* We're done with the handlers. */
24348 finish_function_handler_sequence (try_block, compound_stmt);
24350 return ctor_initializer_p;
24353 /* Parse a handler-seq.
24355 handler-seq:
24356 handler handler-seq [opt] */
24358 static void
24359 cp_parser_handler_seq (cp_parser* parser)
24361 while (true)
24363 cp_token *token;
24365 /* Parse the handler. */
24366 cp_parser_handler (parser);
24367 /* Peek at the next token. */
24368 token = cp_lexer_peek_token (parser->lexer);
24369 /* If it's not `catch' then there are no more handlers. */
24370 if (!cp_parser_is_keyword (token, RID_CATCH))
24371 break;
24375 /* Parse a handler.
24377 handler:
24378 catch ( exception-declaration ) compound-statement */
24380 static void
24381 cp_parser_handler (cp_parser* parser)
24383 tree handler;
24384 tree declaration;
24386 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24387 handler = begin_handler ();
24388 matching_parens parens;
24389 parens.require_open (parser);
24390 declaration = cp_parser_exception_declaration (parser);
24391 finish_handler_parms (declaration, handler);
24392 parens.require_close (parser);
24393 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24394 finish_handler (handler);
24397 /* Parse an exception-declaration.
24399 exception-declaration:
24400 type-specifier-seq declarator
24401 type-specifier-seq abstract-declarator
24402 type-specifier-seq
24405 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24406 ellipsis variant is used. */
24408 static tree
24409 cp_parser_exception_declaration (cp_parser* parser)
24411 cp_decl_specifier_seq type_specifiers;
24412 cp_declarator *declarator;
24413 const char *saved_message;
24415 /* If it's an ellipsis, it's easy to handle. */
24416 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24418 /* Consume the `...' token. */
24419 cp_lexer_consume_token (parser->lexer);
24420 return NULL_TREE;
24423 /* Types may not be defined in exception-declarations. */
24424 saved_message = parser->type_definition_forbidden_message;
24425 parser->type_definition_forbidden_message
24426 = G_("types may not be defined in exception-declarations");
24428 /* Parse the type-specifier-seq. */
24429 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24430 /*is_trailing_return=*/false,
24431 &type_specifiers);
24432 /* If it's a `)', then there is no declarator. */
24433 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24434 declarator = NULL;
24435 else
24436 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24437 /*ctor_dtor_or_conv_p=*/NULL,
24438 /*parenthesized_p=*/NULL,
24439 /*member_p=*/false,
24440 /*friend_p=*/false);
24442 /* Restore the saved message. */
24443 parser->type_definition_forbidden_message = saved_message;
24445 if (!type_specifiers.any_specifiers_p)
24446 return error_mark_node;
24448 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24451 /* Parse a throw-expression.
24453 throw-expression:
24454 throw assignment-expression [opt]
24456 Returns a THROW_EXPR representing the throw-expression. */
24458 static tree
24459 cp_parser_throw_expression (cp_parser* parser)
24461 tree expression;
24462 cp_token* token;
24464 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24465 token = cp_lexer_peek_token (parser->lexer);
24466 /* Figure out whether or not there is an assignment-expression
24467 following the "throw" keyword. */
24468 if (token->type == CPP_COMMA
24469 || token->type == CPP_SEMICOLON
24470 || token->type == CPP_CLOSE_PAREN
24471 || token->type == CPP_CLOSE_SQUARE
24472 || token->type == CPP_CLOSE_BRACE
24473 || token->type == CPP_COLON)
24474 expression = NULL_TREE;
24475 else
24476 expression = cp_parser_assignment_expression (parser);
24478 return build_throw (expression);
24481 /* GNU Extensions */
24483 /* Parse an (optional) asm-specification.
24485 asm-specification:
24486 asm ( string-literal )
24488 If the asm-specification is present, returns a STRING_CST
24489 corresponding to the string-literal. Otherwise, returns
24490 NULL_TREE. */
24492 static tree
24493 cp_parser_asm_specification_opt (cp_parser* parser)
24495 cp_token *token;
24496 tree asm_specification;
24498 /* Peek at the next token. */
24499 token = cp_lexer_peek_token (parser->lexer);
24500 /* If the next token isn't the `asm' keyword, then there's no
24501 asm-specification. */
24502 if (!cp_parser_is_keyword (token, RID_ASM))
24503 return NULL_TREE;
24505 /* Consume the `asm' token. */
24506 cp_lexer_consume_token (parser->lexer);
24507 /* Look for the `('. */
24508 matching_parens parens;
24509 parens.require_open (parser);
24511 /* Look for the string-literal. */
24512 asm_specification = cp_parser_string_literal (parser, false, false);
24514 /* Look for the `)'. */
24515 parens.require_close (parser);
24517 return asm_specification;
24520 /* Parse an asm-operand-list.
24522 asm-operand-list:
24523 asm-operand
24524 asm-operand-list , asm-operand
24526 asm-operand:
24527 string-literal ( expression )
24528 [ string-literal ] string-literal ( expression )
24530 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24531 each node is the expression. The TREE_PURPOSE is itself a
24532 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24533 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24534 is a STRING_CST for the string literal before the parenthesis. Returns
24535 ERROR_MARK_NODE if any of the operands are invalid. */
24537 static tree
24538 cp_parser_asm_operand_list (cp_parser* parser)
24540 tree asm_operands = NULL_TREE;
24541 bool invalid_operands = false;
24543 while (true)
24545 tree string_literal;
24546 tree expression;
24547 tree name;
24549 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24551 /* Consume the `[' token. */
24552 cp_lexer_consume_token (parser->lexer);
24553 /* Read the operand name. */
24554 name = cp_parser_identifier (parser);
24555 if (name != error_mark_node)
24556 name = build_string (IDENTIFIER_LENGTH (name),
24557 IDENTIFIER_POINTER (name));
24558 /* Look for the closing `]'. */
24559 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24561 else
24562 name = NULL_TREE;
24563 /* Look for the string-literal. */
24564 string_literal = cp_parser_string_literal (parser, false, false);
24566 /* Look for the `('. */
24567 matching_parens parens;
24568 parens.require_open (parser);
24569 /* Parse the expression. */
24570 expression = cp_parser_expression (parser);
24571 /* Look for the `)'. */
24572 parens.require_close (parser);
24574 if (name == error_mark_node
24575 || string_literal == error_mark_node
24576 || expression == error_mark_node)
24577 invalid_operands = true;
24579 /* Add this operand to the list. */
24580 asm_operands = tree_cons (build_tree_list (name, string_literal),
24581 expression,
24582 asm_operands);
24583 /* If the next token is not a `,', there are no more
24584 operands. */
24585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24586 break;
24587 /* Consume the `,'. */
24588 cp_lexer_consume_token (parser->lexer);
24591 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24594 /* Parse an asm-clobber-list.
24596 asm-clobber-list:
24597 string-literal
24598 asm-clobber-list , string-literal
24600 Returns a TREE_LIST, indicating the clobbers in the order that they
24601 appeared. The TREE_VALUE of each node is a STRING_CST. */
24603 static tree
24604 cp_parser_asm_clobber_list (cp_parser* parser)
24606 tree clobbers = NULL_TREE;
24608 while (true)
24610 tree string_literal;
24612 /* Look for the string literal. */
24613 string_literal = cp_parser_string_literal (parser, false, false);
24614 /* Add it to the list. */
24615 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24616 /* If the next token is not a `,', then the list is
24617 complete. */
24618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24619 break;
24620 /* Consume the `,' token. */
24621 cp_lexer_consume_token (parser->lexer);
24624 return clobbers;
24627 /* Parse an asm-label-list.
24629 asm-label-list:
24630 identifier
24631 asm-label-list , identifier
24633 Returns a TREE_LIST, indicating the labels in the order that they
24634 appeared. The TREE_VALUE of each node is a label. */
24636 static tree
24637 cp_parser_asm_label_list (cp_parser* parser)
24639 tree labels = NULL_TREE;
24641 while (true)
24643 tree identifier, label, name;
24645 /* Look for the identifier. */
24646 identifier = cp_parser_identifier (parser);
24647 if (!error_operand_p (identifier))
24649 label = lookup_label (identifier);
24650 if (TREE_CODE (label) == LABEL_DECL)
24652 TREE_USED (label) = 1;
24653 check_goto (label);
24654 name = build_string (IDENTIFIER_LENGTH (identifier),
24655 IDENTIFIER_POINTER (identifier));
24656 labels = tree_cons (name, label, labels);
24659 /* If the next token is not a `,', then the list is
24660 complete. */
24661 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24662 break;
24663 /* Consume the `,' token. */
24664 cp_lexer_consume_token (parser->lexer);
24667 return nreverse (labels);
24670 /* Return TRUE iff the next tokens in the stream are possibly the
24671 beginning of a GNU extension attribute. */
24673 static bool
24674 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24676 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24679 /* Return TRUE iff the next tokens in the stream are possibly the
24680 beginning of a standard C++-11 attribute specifier. */
24682 static bool
24683 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24685 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24688 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24689 beginning of a standard C++-11 attribute specifier. */
24691 static bool
24692 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24694 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24696 return (cxx_dialect >= cxx11
24697 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24698 || (token->type == CPP_OPEN_SQUARE
24699 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24700 && token->type == CPP_OPEN_SQUARE)));
24703 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24704 beginning of a GNU extension attribute. */
24706 static bool
24707 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24709 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24711 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24714 /* Return true iff the next tokens can be the beginning of either a
24715 GNU attribute list, or a standard C++11 attribute sequence. */
24717 static bool
24718 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24720 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24721 || cp_next_tokens_can_be_std_attribute_p (parser));
24724 /* Return true iff the next Nth tokens can be the beginning of either
24725 a GNU attribute list, or a standard C++11 attribute sequence. */
24727 static bool
24728 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24730 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24731 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24734 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24735 of GNU attributes, or return NULL. */
24737 static tree
24738 cp_parser_attributes_opt (cp_parser *parser)
24740 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24741 return cp_parser_gnu_attributes_opt (parser);
24742 return cp_parser_std_attribute_spec_seq (parser);
24745 #define CILK_SIMD_FN_CLAUSE_MASK \
24746 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24747 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24748 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24749 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24750 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24752 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24753 vector [(<clauses>)] */
24755 static void
24756 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24758 bool first_p = parser->cilk_simd_fn_info == NULL;
24759 cp_token *token = v_token;
24760 if (first_p)
24762 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24763 parser->cilk_simd_fn_info->error_seen = false;
24764 parser->cilk_simd_fn_info->fndecl_seen = false;
24765 parser->cilk_simd_fn_info->tokens = vNULL;
24766 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24768 int paren_scope = 0;
24769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24771 cp_lexer_consume_token (parser->lexer);
24772 v_token = cp_lexer_peek_token (parser->lexer);
24773 paren_scope++;
24775 while (paren_scope > 0)
24777 token = cp_lexer_peek_token (parser->lexer);
24778 if (token->type == CPP_OPEN_PAREN)
24779 paren_scope++;
24780 else if (token->type == CPP_CLOSE_PAREN)
24781 paren_scope--;
24782 /* Do not push the last ')' */
24783 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24784 cp_lexer_consume_token (parser->lexer);
24787 token->type = CPP_PRAGMA_EOL;
24788 parser->lexer->next_token = token;
24789 cp_lexer_consume_token (parser->lexer);
24791 struct cp_token_cache *cp
24792 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24793 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24796 /* Parse an (optional) series of attributes.
24798 attributes:
24799 attributes attribute
24801 attribute:
24802 __attribute__ (( attribute-list [opt] ))
24804 The return value is as for cp_parser_gnu_attribute_list. */
24806 static tree
24807 cp_parser_gnu_attributes_opt (cp_parser* parser)
24809 tree attributes = NULL_TREE;
24811 while (true)
24813 cp_token *token;
24814 tree attribute_list;
24815 bool ok = true;
24817 /* Peek at the next token. */
24818 token = cp_lexer_peek_token (parser->lexer);
24819 /* If it's not `__attribute__', then we're done. */
24820 if (token->keyword != RID_ATTRIBUTE)
24821 break;
24823 /* Consume the `__attribute__' keyword. */
24824 cp_lexer_consume_token (parser->lexer);
24825 /* Look for the two `(' tokens. */
24826 matching_parens outer_parens;
24827 outer_parens.require_open (parser);
24828 matching_parens inner_parens;
24829 inner_parens.require_open (parser);
24831 /* Peek at the next token. */
24832 token = cp_lexer_peek_token (parser->lexer);
24833 if (token->type != CPP_CLOSE_PAREN)
24834 /* Parse the attribute-list. */
24835 attribute_list = cp_parser_gnu_attribute_list (parser);
24836 else
24837 /* If the next token is a `)', then there is no attribute
24838 list. */
24839 attribute_list = NULL;
24841 /* Look for the two `)' tokens. */
24842 if (!inner_parens.require_close (parser))
24843 ok = false;
24844 if (!outer_parens.require_close (parser))
24845 ok = false;
24846 if (!ok)
24847 cp_parser_skip_to_end_of_statement (parser);
24849 /* Add these new attributes to the list. */
24850 attributes = chainon (attributes, attribute_list);
24853 return attributes;
24856 /* Parse a GNU attribute-list.
24858 attribute-list:
24859 attribute
24860 attribute-list , attribute
24862 attribute:
24863 identifier
24864 identifier ( identifier )
24865 identifier ( identifier , expression-list )
24866 identifier ( expression-list )
24868 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24869 to an attribute. The TREE_PURPOSE of each node is the identifier
24870 indicating which attribute is in use. The TREE_VALUE represents
24871 the arguments, if any. */
24873 static tree
24874 cp_parser_gnu_attribute_list (cp_parser* parser)
24876 tree attribute_list = NULL_TREE;
24877 bool save_translate_strings_p = parser->translate_strings_p;
24879 parser->translate_strings_p = false;
24880 while (true)
24882 cp_token *token;
24883 tree identifier;
24884 tree attribute;
24886 /* Look for the identifier. We also allow keywords here; for
24887 example `__attribute__ ((const))' is legal. */
24888 token = cp_lexer_peek_token (parser->lexer);
24889 if (token->type == CPP_NAME
24890 || token->type == CPP_KEYWORD)
24892 tree arguments = NULL_TREE;
24894 /* Consume the token, but save it since we need it for the
24895 SIMD enabled function parsing. */
24896 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24898 /* Save away the identifier that indicates which attribute
24899 this is. */
24900 identifier = (token->type == CPP_KEYWORD)
24901 /* For keywords, use the canonical spelling, not the
24902 parsed identifier. */
24903 ? ridpointers[(int) token->keyword]
24904 : id_token->u.value;
24906 identifier = canonicalize_attr_name (identifier);
24907 attribute = build_tree_list (identifier, NULL_TREE);
24909 /* Peek at the next token. */
24910 token = cp_lexer_peek_token (parser->lexer);
24911 /* If it's an `(', then parse the attribute arguments. */
24912 if (token->type == CPP_OPEN_PAREN)
24914 vec<tree, va_gc> *vec;
24915 int attr_flag = (attribute_takes_identifier_p (identifier)
24916 ? id_attr : normal_attr);
24917 if (is_cilkplus_vector_p (identifier))
24919 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24920 continue;
24922 else
24923 vec = cp_parser_parenthesized_expression_list
24924 (parser, attr_flag, /*cast_p=*/false,
24925 /*allow_expansion_p=*/false,
24926 /*non_constant_p=*/NULL);
24927 if (vec == NULL)
24928 arguments = error_mark_node;
24929 else
24931 arguments = build_tree_list_vec (vec);
24932 release_tree_vector (vec);
24934 /* Save the arguments away. */
24935 TREE_VALUE (attribute) = arguments;
24937 else if (is_cilkplus_vector_p (identifier))
24939 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24940 continue;
24943 if (arguments != error_mark_node)
24945 /* Add this attribute to the list. */
24946 TREE_CHAIN (attribute) = attribute_list;
24947 attribute_list = attribute;
24950 token = cp_lexer_peek_token (parser->lexer);
24952 /* Now, look for more attributes. If the next token isn't a
24953 `,', we're done. */
24954 if (token->type != CPP_COMMA)
24955 break;
24957 /* Consume the comma and keep going. */
24958 cp_lexer_consume_token (parser->lexer);
24960 parser->translate_strings_p = save_translate_strings_p;
24962 /* We built up the list in reverse order. */
24963 return nreverse (attribute_list);
24966 /* Parse a standard C++11 attribute.
24968 The returned representation is a TREE_LIST which TREE_PURPOSE is
24969 the scoped name of the attribute, and the TREE_VALUE is its
24970 arguments list.
24972 Note that the scoped name of the attribute is itself a TREE_LIST
24973 which TREE_PURPOSE is the namespace of the attribute, and
24974 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24975 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24976 and which TREE_PURPOSE is directly the attribute name.
24978 Clients of the attribute code should use get_attribute_namespace
24979 and get_attribute_name to get the actual namespace and name of
24980 attributes, regardless of their being GNU or C++11 attributes.
24982 attribute:
24983 attribute-token attribute-argument-clause [opt]
24985 attribute-token:
24986 identifier
24987 attribute-scoped-token
24989 attribute-scoped-token:
24990 attribute-namespace :: identifier
24992 attribute-namespace:
24993 identifier
24995 attribute-argument-clause:
24996 ( balanced-token-seq )
24998 balanced-token-seq:
24999 balanced-token [opt]
25000 balanced-token-seq balanced-token
25002 balanced-token:
25003 ( balanced-token-seq )
25004 [ balanced-token-seq ]
25005 { balanced-token-seq }. */
25007 static tree
25008 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
25010 tree attribute, attr_id = NULL_TREE, arguments;
25011 cp_token *token;
25013 /* First, parse name of the attribute, a.k.a attribute-token. */
25015 token = cp_lexer_peek_token (parser->lexer);
25016 if (token->type == CPP_NAME)
25017 attr_id = token->u.value;
25018 else if (token->type == CPP_KEYWORD)
25019 attr_id = ridpointers[(int) token->keyword];
25020 else if (token->flags & NAMED_OP)
25021 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25023 if (attr_id == NULL_TREE)
25024 return NULL_TREE;
25026 cp_lexer_consume_token (parser->lexer);
25028 token = cp_lexer_peek_token (parser->lexer);
25029 if (token->type == CPP_SCOPE)
25031 /* We are seeing a scoped attribute token. */
25033 cp_lexer_consume_token (parser->lexer);
25034 if (attr_ns)
25035 error_at (token->location, "attribute using prefix used together "
25036 "with scoped attribute token");
25037 attr_ns = attr_id;
25039 token = cp_lexer_consume_token (parser->lexer);
25040 if (token->type == CPP_NAME)
25041 attr_id = token->u.value;
25042 else if (token->type == CPP_KEYWORD)
25043 attr_id = ridpointers[(int) token->keyword];
25044 else if (token->flags & NAMED_OP)
25045 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
25046 else
25048 error_at (token->location,
25049 "expected an identifier for the attribute name");
25050 return error_mark_node;
25053 attr_id = canonicalize_attr_name (attr_id);
25054 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25055 NULL_TREE);
25056 token = cp_lexer_peek_token (parser->lexer);
25058 else if (attr_ns)
25059 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
25060 NULL_TREE);
25061 else
25063 attr_id = canonicalize_attr_name (attr_id);
25064 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
25065 NULL_TREE);
25066 /* C++11 noreturn attribute is equivalent to GNU's. */
25067 if (is_attribute_p ("noreturn", attr_id))
25068 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25069 /* C++14 deprecated attribute is equivalent to GNU's. */
25070 else if (is_attribute_p ("deprecated", attr_id))
25071 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25072 /* C++17 fallthrough attribute is equivalent to GNU's. */
25073 else if (is_attribute_p ("fallthrough", attr_id))
25074 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
25075 /* Transactional Memory TS optimize_for_synchronized attribute is
25076 equivalent to GNU transaction_callable. */
25077 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
25078 TREE_PURPOSE (attribute)
25079 = get_identifier ("transaction_callable");
25080 /* Transactional Memory attributes are GNU attributes. */
25081 else if (tm_attr_to_mask (attr_id))
25082 TREE_PURPOSE (attribute) = attr_id;
25085 /* Now parse the optional argument clause of the attribute. */
25087 if (token->type != CPP_OPEN_PAREN)
25088 return attribute;
25091 vec<tree, va_gc> *vec;
25092 int attr_flag = normal_attr;
25094 if (attr_ns == get_identifier ("gnu")
25095 && attribute_takes_identifier_p (attr_id))
25096 /* A GNU attribute that takes an identifier in parameter. */
25097 attr_flag = id_attr;
25099 vec = cp_parser_parenthesized_expression_list
25100 (parser, attr_flag, /*cast_p=*/false,
25101 /*allow_expansion_p=*/true,
25102 /*non_constant_p=*/NULL);
25103 if (vec == NULL)
25104 arguments = error_mark_node;
25105 else
25107 arguments = build_tree_list_vec (vec);
25108 release_tree_vector (vec);
25111 if (arguments == error_mark_node)
25112 attribute = error_mark_node;
25113 else
25114 TREE_VALUE (attribute) = arguments;
25117 return attribute;
25120 /* Check that the attribute ATTRIBUTE appears at most once in the
25121 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
25122 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
25123 isn't implemented yet in GCC. */
25125 static void
25126 cp_parser_check_std_attribute (tree attributes, tree attribute)
25128 if (attributes)
25130 tree name = get_attribute_name (attribute);
25131 if (is_attribute_p ("noreturn", name)
25132 && lookup_attribute ("noreturn", attributes))
25133 error ("attribute %<noreturn%> can appear at most once "
25134 "in an attribute-list");
25135 else if (is_attribute_p ("deprecated", name)
25136 && lookup_attribute ("deprecated", attributes))
25137 error ("attribute %<deprecated%> can appear at most once "
25138 "in an attribute-list");
25142 /* Parse a list of standard C++-11 attributes.
25144 attribute-list:
25145 attribute [opt]
25146 attribute-list , attribute[opt]
25147 attribute ...
25148 attribute-list , attribute ...
25151 static tree
25152 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25154 tree attributes = NULL_TREE, attribute = NULL_TREE;
25155 cp_token *token = NULL;
25157 while (true)
25159 attribute = cp_parser_std_attribute (parser, attr_ns);
25160 if (attribute == error_mark_node)
25161 break;
25162 if (attribute != NULL_TREE)
25164 cp_parser_check_std_attribute (attributes, attribute);
25165 TREE_CHAIN (attribute) = attributes;
25166 attributes = attribute;
25168 token = cp_lexer_peek_token (parser->lexer);
25169 if (token->type == CPP_ELLIPSIS)
25171 cp_lexer_consume_token (parser->lexer);
25172 if (attribute == NULL_TREE)
25173 error_at (token->location,
25174 "expected attribute before %<...%>");
25175 else
25177 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25178 if (pack == error_mark_node)
25179 return error_mark_node;
25180 TREE_VALUE (attribute) = pack;
25182 token = cp_lexer_peek_token (parser->lexer);
25184 if (token->type != CPP_COMMA)
25185 break;
25186 cp_lexer_consume_token (parser->lexer);
25188 attributes = nreverse (attributes);
25189 return attributes;
25192 /* Parse a standard C++-11 attribute specifier.
25194 attribute-specifier:
25195 [ [ attribute-using-prefix [opt] attribute-list ] ]
25196 alignment-specifier
25198 attribute-using-prefix:
25199 using attribute-namespace :
25201 alignment-specifier:
25202 alignas ( type-id ... [opt] )
25203 alignas ( alignment-expression ... [opt] ). */
25205 static tree
25206 cp_parser_std_attribute_spec (cp_parser *parser)
25208 tree attributes = NULL_TREE;
25209 cp_token *token = cp_lexer_peek_token (parser->lexer);
25211 if (token->type == CPP_OPEN_SQUARE
25212 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25214 tree attr_ns = NULL_TREE;
25216 cp_lexer_consume_token (parser->lexer);
25217 cp_lexer_consume_token (parser->lexer);
25219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25221 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25222 if (token->type == CPP_NAME)
25223 attr_ns = token->u.value;
25224 else if (token->type == CPP_KEYWORD)
25225 attr_ns = ridpointers[(int) token->keyword];
25226 else if (token->flags & NAMED_OP)
25227 attr_ns = get_identifier (cpp_type2name (token->type,
25228 token->flags));
25229 if (attr_ns
25230 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25232 if (cxx_dialect < cxx1z
25233 && !in_system_header_at (input_location))
25234 pedwarn (input_location, 0,
25235 "attribute using prefix only available "
25236 "with -std=c++1z or -std=gnu++1z");
25238 cp_lexer_consume_token (parser->lexer);
25239 cp_lexer_consume_token (parser->lexer);
25240 cp_lexer_consume_token (parser->lexer);
25242 else
25243 attr_ns = NULL_TREE;
25246 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25248 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25249 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25250 cp_parser_skip_to_end_of_statement (parser);
25251 else
25252 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25253 when we are sure that we have actually parsed them. */
25254 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25256 else
25258 tree alignas_expr;
25260 /* Look for an alignment-specifier. */
25262 token = cp_lexer_peek_token (parser->lexer);
25264 if (token->type != CPP_KEYWORD
25265 || token->keyword != RID_ALIGNAS)
25266 return NULL_TREE;
25268 cp_lexer_consume_token (parser->lexer);
25269 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25271 matching_parens parens;
25272 if (!parens.require_open (parser))
25274 cp_parser_error (parser, "expected %<(%>");
25275 return error_mark_node;
25278 cp_parser_parse_tentatively (parser);
25279 alignas_expr = cp_parser_type_id (parser);
25281 if (!cp_parser_parse_definitely (parser))
25283 alignas_expr = cp_parser_assignment_expression (parser);
25284 if (alignas_expr == error_mark_node)
25285 cp_parser_skip_to_end_of_statement (parser);
25286 if (alignas_expr == NULL_TREE
25287 || alignas_expr == error_mark_node)
25288 return alignas_expr;
25291 alignas_expr = cxx_alignas_expr (alignas_expr);
25292 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25294 /* Handle alignas (pack...). */
25295 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25297 cp_lexer_consume_token (parser->lexer);
25298 alignas_expr = make_pack_expansion (alignas_expr);
25301 /* Something went wrong, so don't build the attribute. */
25302 if (alignas_expr == error_mark_node)
25303 return error_mark_node;
25305 if (!parens.require_close (parser))
25307 cp_parser_error (parser, "expected %<)%>");
25308 return error_mark_node;
25311 /* Build the C++-11 representation of an 'aligned'
25312 attribute. */
25313 attributes =
25314 build_tree_list (build_tree_list (get_identifier ("gnu"),
25315 get_identifier ("aligned")),
25316 alignas_expr);
25319 return attributes;
25322 /* Parse a standard C++-11 attribute-specifier-seq.
25324 attribute-specifier-seq:
25325 attribute-specifier-seq [opt] attribute-specifier
25328 static tree
25329 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25331 tree attr_specs = NULL_TREE;
25332 tree attr_last = NULL_TREE;
25334 while (true)
25336 tree attr_spec = cp_parser_std_attribute_spec (parser);
25337 if (attr_spec == NULL_TREE)
25338 break;
25339 if (attr_spec == error_mark_node)
25340 return error_mark_node;
25342 if (attr_last)
25343 TREE_CHAIN (attr_last) = attr_spec;
25344 else
25345 attr_specs = attr_last = attr_spec;
25346 attr_last = tree_last (attr_last);
25349 return attr_specs;
25352 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25353 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25354 current value of the PEDANTIC flag, regardless of whether or not
25355 the `__extension__' keyword is present. The caller is responsible
25356 for restoring the value of the PEDANTIC flag. */
25358 static bool
25359 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25361 /* Save the old value of the PEDANTIC flag. */
25362 *saved_pedantic = pedantic;
25364 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25366 /* Consume the `__extension__' token. */
25367 cp_lexer_consume_token (parser->lexer);
25368 /* We're not being pedantic while the `__extension__' keyword is
25369 in effect. */
25370 pedantic = 0;
25372 return true;
25375 return false;
25378 /* Parse a label declaration.
25380 label-declaration:
25381 __label__ label-declarator-seq ;
25383 label-declarator-seq:
25384 identifier , label-declarator-seq
25385 identifier */
25387 static void
25388 cp_parser_label_declaration (cp_parser* parser)
25390 /* Look for the `__label__' keyword. */
25391 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25393 while (true)
25395 tree identifier;
25397 /* Look for an identifier. */
25398 identifier = cp_parser_identifier (parser);
25399 /* If we failed, stop. */
25400 if (identifier == error_mark_node)
25401 break;
25402 /* Declare it as a label. */
25403 finish_label_decl (identifier);
25404 /* If the next token is a `;', stop. */
25405 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25406 break;
25407 /* Look for the `,' separating the label declarations. */
25408 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25411 /* Look for the final `;'. */
25412 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25415 // -------------------------------------------------------------------------- //
25416 // Requires Clause
25418 // Parse a requires clause.
25420 // requires-clause:
25421 // 'requires' logical-or-expression
25423 // The required logical-or-expression must be a constant expression. Note
25424 // that we don't check that the expression is constepxr here. We defer until
25425 // we analyze constraints and then, we only check atomic constraints.
25426 static tree
25427 cp_parser_requires_clause (cp_parser *parser)
25429 // Parse the requires clause so that it is not automatically folded.
25430 ++processing_template_decl;
25431 tree expr = cp_parser_binary_expression (parser, false, false,
25432 PREC_NOT_OPERATOR, NULL);
25433 if (check_for_bare_parameter_packs (expr))
25434 expr = error_mark_node;
25435 --processing_template_decl;
25436 return expr;
25439 // Optionally parse a requires clause:
25440 static tree
25441 cp_parser_requires_clause_opt (cp_parser *parser)
25443 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25444 if (tok->keyword != RID_REQUIRES)
25446 if (!flag_concepts && tok->type == CPP_NAME
25447 && tok->u.value == ridpointers[RID_REQUIRES])
25449 error_at (cp_lexer_peek_token (parser->lexer)->location,
25450 "%<requires%> only available with -fconcepts");
25451 /* Parse and discard the requires-clause. */
25452 cp_lexer_consume_token (parser->lexer);
25453 cp_parser_requires_clause (parser);
25455 return NULL_TREE;
25457 cp_lexer_consume_token (parser->lexer);
25458 return cp_parser_requires_clause (parser);
25462 /*---------------------------------------------------------------------------
25463 Requires expressions
25464 ---------------------------------------------------------------------------*/
25466 /* Parse a requires expression
25468 requirement-expression:
25469 'requires' requirement-parameter-list [opt] requirement-body */
25470 static tree
25471 cp_parser_requires_expression (cp_parser *parser)
25473 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25474 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25476 /* A requires-expression shall appear only within a concept
25477 definition or a requires-clause.
25479 TODO: Implement this diagnostic correctly. */
25480 if (!processing_template_decl)
25482 error_at (loc, "a requires expression cannot appear outside a template");
25483 cp_parser_skip_to_end_of_statement (parser);
25484 return error_mark_node;
25487 tree parms, reqs;
25489 /* Local parameters are delared as variables within the scope
25490 of the expression. They are not visible past the end of
25491 the expression. Expressions within the requires-expression
25492 are unevaluated. */
25493 struct scope_sentinel
25495 scope_sentinel ()
25497 ++cp_unevaluated_operand;
25498 begin_scope (sk_block, NULL_TREE);
25501 ~scope_sentinel ()
25503 pop_bindings_and_leave_scope ();
25504 --cp_unevaluated_operand;
25506 } s;
25508 /* Parse the optional parameter list. */
25509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25511 parms = cp_parser_requirement_parameter_list (parser);
25512 if (parms == error_mark_node)
25513 return error_mark_node;
25515 else
25516 parms = NULL_TREE;
25518 /* Parse the requirement body. */
25519 reqs = cp_parser_requirement_body (parser);
25520 if (reqs == error_mark_node)
25521 return error_mark_node;
25524 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25525 the parm chain. */
25526 grokparms (parms, &parms);
25527 return finish_requires_expr (parms, reqs);
25530 /* Parse a parameterized requirement.
25532 requirement-parameter-list:
25533 '(' parameter-declaration-clause ')' */
25534 static tree
25535 cp_parser_requirement_parameter_list (cp_parser *parser)
25537 matching_parens parens;
25538 if (!parens.require_open (parser))
25539 return error_mark_node;
25541 tree parms = cp_parser_parameter_declaration_clause (parser);
25543 if (!parens.require_close (parser))
25544 return error_mark_node;
25546 return parms;
25549 /* Parse the body of a requirement.
25551 requirement-body:
25552 '{' requirement-list '}' */
25553 static tree
25554 cp_parser_requirement_body (cp_parser *parser)
25556 matching_braces braces;
25557 if (!braces.require_open (parser))
25558 return error_mark_node;
25560 tree reqs = cp_parser_requirement_list (parser);
25562 if (!braces.require_close (parser))
25563 return error_mark_node;
25565 return reqs;
25568 /* Parse a list of requirements.
25570 requirement-list:
25571 requirement
25572 requirement-list ';' requirement[opt] */
25573 static tree
25574 cp_parser_requirement_list (cp_parser *parser)
25576 tree result = NULL_TREE;
25577 while (true)
25579 tree req = cp_parser_requirement (parser);
25580 if (req == error_mark_node)
25581 return error_mark_node;
25583 result = tree_cons (NULL_TREE, req, result);
25585 /* If we see a semi-colon, consume it. */
25586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25587 cp_lexer_consume_token (parser->lexer);
25589 /* Stop processing at the end of the list. */
25590 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25591 break;
25594 /* Reverse the order of requirements so they are analyzed in
25595 declaration order. */
25596 return nreverse (result);
25599 /* Parse a syntactic requirement or type requirement.
25601 requirement:
25602 simple-requirement
25603 compound-requirement
25604 type-requirement
25605 nested-requirement */
25606 static tree
25607 cp_parser_requirement (cp_parser *parser)
25609 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25610 return cp_parser_compound_requirement (parser);
25611 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25612 return cp_parser_type_requirement (parser);
25613 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25614 return cp_parser_nested_requirement (parser);
25615 else
25616 return cp_parser_simple_requirement (parser);
25619 /* Parse a simple requirement.
25621 simple-requirement:
25622 expression ';' */
25623 static tree
25624 cp_parser_simple_requirement (cp_parser *parser)
25626 tree expr = cp_parser_expression (parser, NULL, false, false);
25627 if (!expr || expr == error_mark_node)
25628 return error_mark_node;
25630 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25631 return error_mark_node;
25633 return finish_simple_requirement (expr);
25636 /* Parse a type requirement
25638 type-requirement
25639 nested-name-specifier [opt] required-type-name ';'
25641 required-type-name:
25642 type-name
25643 'template' [opt] simple-template-id */
25644 static tree
25645 cp_parser_type_requirement (cp_parser *parser)
25647 cp_lexer_consume_token (parser->lexer);
25649 // Save the scope before parsing name specifiers.
25650 tree saved_scope = parser->scope;
25651 tree saved_object_scope = parser->object_scope;
25652 tree saved_qualifying_scope = parser->qualifying_scope;
25653 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25654 cp_parser_nested_name_specifier_opt (parser,
25655 /*typename_keyword_p=*/true,
25656 /*check_dependency_p=*/false,
25657 /*type_p=*/true,
25658 /*is_declaration=*/false);
25660 tree type;
25661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25663 cp_lexer_consume_token (parser->lexer);
25664 type = cp_parser_template_id (parser,
25665 /*template_keyword_p=*/true,
25666 /*check_dependency=*/false,
25667 /*tag_type=*/none_type,
25668 /*is_declaration=*/false);
25669 type = make_typename_type (parser->scope, type, typename_type,
25670 /*complain=*/tf_error);
25672 else
25673 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25675 if (TREE_CODE (type) == TYPE_DECL)
25676 type = TREE_TYPE (type);
25678 parser->scope = saved_scope;
25679 parser->object_scope = saved_object_scope;
25680 parser->qualifying_scope = saved_qualifying_scope;
25682 if (type == error_mark_node)
25683 cp_parser_skip_to_end_of_statement (parser);
25685 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25686 return error_mark_node;
25687 if (type == error_mark_node)
25688 return error_mark_node;
25690 return finish_type_requirement (type);
25693 /* Parse a compound requirement
25695 compound-requirement:
25696 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25697 static tree
25698 cp_parser_compound_requirement (cp_parser *parser)
25700 /* Parse an expression enclosed in '{ }'s. */
25701 matching_braces braces;
25702 if (!braces.require_open (parser))
25703 return error_mark_node;
25705 tree expr = cp_parser_expression (parser, NULL, false, false);
25706 if (!expr || expr == error_mark_node)
25707 return error_mark_node;
25709 if (!braces.require_close (parser))
25710 return error_mark_node;
25712 /* Parse the optional noexcept. */
25713 bool noexcept_p = false;
25714 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25716 cp_lexer_consume_token (parser->lexer);
25717 noexcept_p = true;
25720 /* Parse the optional trailing return type. */
25721 tree type = NULL_TREE;
25722 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25724 cp_lexer_consume_token (parser->lexer);
25725 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25726 parser->in_result_type_constraint_p = true;
25727 type = cp_parser_trailing_type_id (parser);
25728 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25729 if (type == error_mark_node)
25730 return error_mark_node;
25733 return finish_compound_requirement (expr, type, noexcept_p);
25736 /* Parse a nested requirement. This is the same as a requires clause.
25738 nested-requirement:
25739 requires-clause */
25740 static tree
25741 cp_parser_nested_requirement (cp_parser *parser)
25743 cp_lexer_consume_token (parser->lexer);
25744 tree req = cp_parser_requires_clause (parser);
25745 if (req == error_mark_node)
25746 return error_mark_node;
25747 return finish_nested_requirement (req);
25750 /* Support Functions */
25752 /* Return the appropriate prefer_type argument for lookup_name_real based on
25753 tag_type and template_mem_access. */
25755 static inline int
25756 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25758 /* DR 141: When looking in the current enclosing context for a template-name
25759 after -> or ., only consider class templates. */
25760 if (template_mem_access)
25761 return 2;
25762 switch (tag_type)
25764 case none_type: return 0; // No preference.
25765 case scope_type: return 1; // Type or namespace.
25766 default: return 2; // Type only.
25770 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25771 NAME should have one of the representations used for an
25772 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25773 is returned. If PARSER->SCOPE is a dependent type, then a
25774 SCOPE_REF is returned.
25776 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25777 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25778 was formed. Abstractly, such entities should not be passed to this
25779 function, because they do not need to be looked up, but it is
25780 simpler to check for this special case here, rather than at the
25781 call-sites.
25783 In cases not explicitly covered above, this function returns a
25784 DECL, OVERLOAD, or baselink representing the result of the lookup.
25785 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25786 is returned.
25788 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25789 (e.g., "struct") that was used. In that case bindings that do not
25790 refer to types are ignored.
25792 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25793 ignored.
25795 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25796 are ignored.
25798 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25799 types.
25801 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25802 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25803 NULL_TREE otherwise. */
25805 static cp_expr
25806 cp_parser_lookup_name (cp_parser *parser, tree name,
25807 enum tag_types tag_type,
25808 bool is_template,
25809 bool is_namespace,
25810 bool check_dependency,
25811 tree *ambiguous_decls,
25812 location_t name_location)
25814 tree decl;
25815 tree object_type = parser->context->object_type;
25817 /* Assume that the lookup will be unambiguous. */
25818 if (ambiguous_decls)
25819 *ambiguous_decls = NULL_TREE;
25821 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25822 no longer valid. Note that if we are parsing tentatively, and
25823 the parse fails, OBJECT_TYPE will be automatically restored. */
25824 parser->context->object_type = NULL_TREE;
25826 if (name == error_mark_node)
25827 return error_mark_node;
25829 /* A template-id has already been resolved; there is no lookup to
25830 do. */
25831 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25832 return name;
25833 if (BASELINK_P (name))
25835 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25836 == TEMPLATE_ID_EXPR);
25837 return name;
25840 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25841 it should already have been checked to make sure that the name
25842 used matches the type being destroyed. */
25843 if (TREE_CODE (name) == BIT_NOT_EXPR)
25845 tree type;
25847 /* Figure out to which type this destructor applies. */
25848 if (parser->scope)
25849 type = parser->scope;
25850 else if (object_type)
25851 type = object_type;
25852 else
25853 type = current_class_type;
25854 /* If that's not a class type, there is no destructor. */
25855 if (!type || !CLASS_TYPE_P (type))
25856 return error_mark_node;
25858 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25859 lazily_declare_fn (sfk_destructor, type);
25861 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25862 return dtor;
25864 return error_mark_node;
25867 /* By this point, the NAME should be an ordinary identifier. If
25868 the id-expression was a qualified name, the qualifying scope is
25869 stored in PARSER->SCOPE at this point. */
25870 gcc_assert (identifier_p (name));
25872 /* Perform the lookup. */
25873 if (parser->scope)
25875 bool dependent_p;
25877 if (parser->scope == error_mark_node)
25878 return error_mark_node;
25880 /* If the SCOPE is dependent, the lookup must be deferred until
25881 the template is instantiated -- unless we are explicitly
25882 looking up names in uninstantiated templates. Even then, we
25883 cannot look up the name if the scope is not a class type; it
25884 might, for example, be a template type parameter. */
25885 dependent_p = (TYPE_P (parser->scope)
25886 && dependent_scope_p (parser->scope));
25887 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25888 && dependent_p)
25889 /* Defer lookup. */
25890 decl = error_mark_node;
25891 else
25893 tree pushed_scope = NULL_TREE;
25895 /* If PARSER->SCOPE is a dependent type, then it must be a
25896 class type, and we must not be checking dependencies;
25897 otherwise, we would have processed this lookup above. So
25898 that PARSER->SCOPE is not considered a dependent base by
25899 lookup_member, we must enter the scope here. */
25900 if (dependent_p)
25901 pushed_scope = push_scope (parser->scope);
25903 /* If the PARSER->SCOPE is a template specialization, it
25904 may be instantiated during name lookup. In that case,
25905 errors may be issued. Even if we rollback the current
25906 tentative parse, those errors are valid. */
25907 decl = lookup_qualified_name (parser->scope, name,
25908 prefer_type_arg (tag_type),
25909 /*complain=*/true);
25911 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25912 lookup result and the nested-name-specifier nominates a class C:
25913 * if the name specified after the nested-name-specifier, when
25914 looked up in C, is the injected-class-name of C (Clause 9), or
25915 * if the name specified after the nested-name-specifier is the
25916 same as the identifier or the simple-template-id's template-
25917 name in the last component of the nested-name-specifier,
25918 the name is instead considered to name the constructor of
25919 class C. [ Note: for example, the constructor is not an
25920 acceptable lookup result in an elaborated-type-specifier so
25921 the constructor would not be used in place of the
25922 injected-class-name. --end note ] Such a constructor name
25923 shall be used only in the declarator-id of a declaration that
25924 names a constructor or in a using-declaration. */
25925 if (tag_type == none_type
25926 && DECL_SELF_REFERENCE_P (decl)
25927 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25928 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25929 prefer_type_arg (tag_type),
25930 /*complain=*/true);
25932 /* If we have a single function from a using decl, pull it out. */
25933 if (TREE_CODE (decl) == OVERLOAD
25934 && !really_overloaded_fn (decl))
25935 decl = OVL_FUNCTION (decl);
25937 if (pushed_scope)
25938 pop_scope (pushed_scope);
25941 /* If the scope is a dependent type and either we deferred lookup or
25942 we did lookup but didn't find the name, rememeber the name. */
25943 if (decl == error_mark_node && TYPE_P (parser->scope)
25944 && dependent_type_p (parser->scope))
25946 if (tag_type)
25948 tree type;
25950 /* The resolution to Core Issue 180 says that `struct
25951 A::B' should be considered a type-name, even if `A'
25952 is dependent. */
25953 type = make_typename_type (parser->scope, name, tag_type,
25954 /*complain=*/tf_error);
25955 if (type != error_mark_node)
25956 decl = TYPE_NAME (type);
25958 else if (is_template
25959 && (cp_parser_next_token_ends_template_argument_p (parser)
25960 || cp_lexer_next_token_is (parser->lexer,
25961 CPP_CLOSE_PAREN)))
25962 decl = make_unbound_class_template (parser->scope,
25963 name, NULL_TREE,
25964 /*complain=*/tf_error);
25965 else
25966 decl = build_qualified_name (/*type=*/NULL_TREE,
25967 parser->scope, name,
25968 is_template);
25970 parser->qualifying_scope = parser->scope;
25971 parser->object_scope = NULL_TREE;
25973 else if (object_type)
25975 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25976 OBJECT_TYPE is not a class. */
25977 if (CLASS_TYPE_P (object_type))
25978 /* If the OBJECT_TYPE is a template specialization, it may
25979 be instantiated during name lookup. In that case, errors
25980 may be issued. Even if we rollback the current tentative
25981 parse, those errors are valid. */
25982 decl = lookup_member (object_type,
25983 name,
25984 /*protect=*/0,
25985 prefer_type_arg (tag_type),
25986 tf_warning_or_error);
25987 else
25988 decl = NULL_TREE;
25990 if (!decl)
25991 /* Look it up in the enclosing context. DR 141: When looking for a
25992 template-name after -> or ., only consider class templates. */
25993 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25994 /*nonclass=*/0,
25995 /*block_p=*/true, is_namespace, 0);
25996 if (object_type == unknown_type_node)
25997 /* The object is type-dependent, so we can't look anything up; we used
25998 this to get the DR 141 behavior. */
25999 object_type = NULL_TREE;
26000 parser->object_scope = object_type;
26001 parser->qualifying_scope = NULL_TREE;
26003 else
26005 decl = lookup_name_real (name, prefer_type_arg (tag_type),
26006 /*nonclass=*/0,
26007 /*block_p=*/true, is_namespace, 0);
26008 parser->qualifying_scope = NULL_TREE;
26009 parser->object_scope = NULL_TREE;
26012 /* If the lookup failed, let our caller know. */
26013 if (!decl || decl == error_mark_node)
26014 return error_mark_node;
26016 /* Pull out the template from an injected-class-name (or multiple). */
26017 if (is_template)
26018 decl = maybe_get_template_decl_from_type_decl (decl);
26020 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
26021 if (TREE_CODE (decl) == TREE_LIST)
26023 if (ambiguous_decls)
26024 *ambiguous_decls = decl;
26025 /* The error message we have to print is too complicated for
26026 cp_parser_error, so we incorporate its actions directly. */
26027 if (!cp_parser_simulate_error (parser))
26029 error_at (name_location, "reference to %qD is ambiguous",
26030 name);
26031 print_candidates (decl);
26033 return error_mark_node;
26036 gcc_assert (DECL_P (decl)
26037 || TREE_CODE (decl) == OVERLOAD
26038 || TREE_CODE (decl) == SCOPE_REF
26039 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
26040 || BASELINK_P (decl));
26042 /* If we have resolved the name of a member declaration, check to
26043 see if the declaration is accessible. When the name resolves to
26044 set of overloaded functions, accessibility is checked when
26045 overload resolution is done.
26047 During an explicit instantiation, access is not checked at all,
26048 as per [temp.explicit]. */
26049 if (DECL_P (decl))
26050 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
26052 maybe_record_typedef_use (decl);
26054 return cp_expr (decl, name_location);
26057 /* Like cp_parser_lookup_name, but for use in the typical case where
26058 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
26059 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
26061 static tree
26062 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
26064 return cp_parser_lookup_name (parser, name,
26065 none_type,
26066 /*is_template=*/false,
26067 /*is_namespace=*/false,
26068 /*check_dependency=*/true,
26069 /*ambiguous_decls=*/NULL,
26070 location);
26073 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
26074 the current context, return the TYPE_DECL. If TAG_NAME_P is
26075 true, the DECL indicates the class being defined in a class-head,
26076 or declared in an elaborated-type-specifier.
26078 Otherwise, return DECL. */
26080 static tree
26081 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
26083 /* If the TEMPLATE_DECL is being declared as part of a class-head,
26084 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
26086 struct A {
26087 template <typename T> struct B;
26090 template <typename T> struct A::B {};
26092 Similarly, in an elaborated-type-specifier:
26094 namespace N { struct X{}; }
26096 struct A {
26097 template <typename T> friend struct N::X;
26100 However, if the DECL refers to a class type, and we are in
26101 the scope of the class, then the name lookup automatically
26102 finds the TYPE_DECL created by build_self_reference rather
26103 than a TEMPLATE_DECL. For example, in:
26105 template <class T> struct S {
26106 S s;
26109 there is no need to handle such case. */
26111 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
26112 return DECL_TEMPLATE_RESULT (decl);
26114 return decl;
26117 /* If too many, or too few, template-parameter lists apply to the
26118 declarator, issue an error message. Returns TRUE if all went well,
26119 and FALSE otherwise. */
26121 static bool
26122 cp_parser_check_declarator_template_parameters (cp_parser* parser,
26123 cp_declarator *declarator,
26124 location_t declarator_location)
26126 switch (declarator->kind)
26128 case cdk_id:
26130 unsigned num_templates = 0;
26131 tree scope = declarator->u.id.qualifying_scope;
26133 if (scope)
26134 num_templates = num_template_headers_for_class (scope);
26135 else if (TREE_CODE (declarator->u.id.unqualified_name)
26136 == TEMPLATE_ID_EXPR)
26137 /* If the DECLARATOR has the form `X<y>' then it uses one
26138 additional level of template parameters. */
26139 ++num_templates;
26141 return cp_parser_check_template_parameters
26142 (parser, num_templates, declarator_location, declarator);
26145 case cdk_function:
26146 case cdk_array:
26147 case cdk_pointer:
26148 case cdk_reference:
26149 case cdk_ptrmem:
26150 return (cp_parser_check_declarator_template_parameters
26151 (parser, declarator->declarator, declarator_location));
26153 case cdk_decomp:
26154 case cdk_error:
26155 return true;
26157 default:
26158 gcc_unreachable ();
26160 return false;
26163 /* NUM_TEMPLATES were used in the current declaration. If that is
26164 invalid, return FALSE and issue an error messages. Otherwise,
26165 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26166 declarator and we can print more accurate diagnostics. */
26168 static bool
26169 cp_parser_check_template_parameters (cp_parser* parser,
26170 unsigned num_templates,
26171 location_t location,
26172 cp_declarator *declarator)
26174 /* If there are the same number of template classes and parameter
26175 lists, that's OK. */
26176 if (parser->num_template_parameter_lists == num_templates)
26177 return true;
26178 /* If there are more, but only one more, then we are referring to a
26179 member template. That's OK too. */
26180 if (parser->num_template_parameter_lists == num_templates + 1)
26181 return true;
26182 /* If there are more template classes than parameter lists, we have
26183 something like:
26185 template <class T> void S<T>::R<T>::f (); */
26186 if (parser->num_template_parameter_lists < num_templates)
26188 if (declarator && !current_function_decl)
26189 error_at (location, "specializing member %<%T::%E%> "
26190 "requires %<template<>%> syntax",
26191 declarator->u.id.qualifying_scope,
26192 declarator->u.id.unqualified_name);
26193 else if (declarator)
26194 error_at (location, "invalid declaration of %<%T::%E%>",
26195 declarator->u.id.qualifying_scope,
26196 declarator->u.id.unqualified_name);
26197 else
26198 error_at (location, "too few template-parameter-lists");
26199 return false;
26201 /* Otherwise, there are too many template parameter lists. We have
26202 something like:
26204 template <class T> template <class U> void S::f(); */
26205 error_at (location, "too many template-parameter-lists");
26206 return false;
26209 /* Parse an optional `::' token indicating that the following name is
26210 from the global namespace. If so, PARSER->SCOPE is set to the
26211 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26212 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26213 Returns the new value of PARSER->SCOPE, if the `::' token is
26214 present, and NULL_TREE otherwise. */
26216 static tree
26217 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26219 cp_token *token;
26221 /* Peek at the next token. */
26222 token = cp_lexer_peek_token (parser->lexer);
26223 /* If we're looking at a `::' token then we're starting from the
26224 global namespace, not our current location. */
26225 if (token->type == CPP_SCOPE)
26227 /* Consume the `::' token. */
26228 cp_lexer_consume_token (parser->lexer);
26229 /* Set the SCOPE so that we know where to start the lookup. */
26230 parser->scope = global_namespace;
26231 parser->qualifying_scope = global_namespace;
26232 parser->object_scope = NULL_TREE;
26234 return parser->scope;
26236 else if (!current_scope_valid_p)
26238 parser->scope = NULL_TREE;
26239 parser->qualifying_scope = NULL_TREE;
26240 parser->object_scope = NULL_TREE;
26243 return NULL_TREE;
26246 /* Returns TRUE if the upcoming token sequence is the start of a
26247 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26248 declarator is preceded by the `friend' specifier. */
26250 static bool
26251 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26253 bool constructor_p;
26254 bool outside_class_specifier_p;
26255 tree nested_name_specifier;
26256 cp_token *next_token;
26258 /* The common case is that this is not a constructor declarator, so
26259 try to avoid doing lots of work if at all possible. It's not
26260 valid declare a constructor at function scope. */
26261 if (parser->in_function_body)
26262 return false;
26263 /* And only certain tokens can begin a constructor declarator. */
26264 next_token = cp_lexer_peek_token (parser->lexer);
26265 if (next_token->type != CPP_NAME
26266 && next_token->type != CPP_SCOPE
26267 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26268 && next_token->type != CPP_TEMPLATE_ID)
26269 return false;
26271 /* Parse tentatively; we are going to roll back all of the tokens
26272 consumed here. */
26273 cp_parser_parse_tentatively (parser);
26274 /* Assume that we are looking at a constructor declarator. */
26275 constructor_p = true;
26277 /* Look for the optional `::' operator. */
26278 cp_parser_global_scope_opt (parser,
26279 /*current_scope_valid_p=*/false);
26280 /* Look for the nested-name-specifier. */
26281 nested_name_specifier
26282 = (cp_parser_nested_name_specifier_opt (parser,
26283 /*typename_keyword_p=*/false,
26284 /*check_dependency_p=*/false,
26285 /*type_p=*/false,
26286 /*is_declaration=*/false));
26288 outside_class_specifier_p = (!at_class_scope_p ()
26289 || !TYPE_BEING_DEFINED (current_class_type)
26290 || friend_p);
26292 /* Outside of a class-specifier, there must be a
26293 nested-name-specifier. Except in C++17 mode, where we
26294 might be declaring a guiding declaration. */
26295 if (!nested_name_specifier && outside_class_specifier_p
26296 && cxx_dialect < cxx1z)
26297 constructor_p = false;
26298 else if (nested_name_specifier == error_mark_node)
26299 constructor_p = false;
26301 /* If we have a class scope, this is easy; DR 147 says that S::S always
26302 names the constructor, and no other qualified name could. */
26303 if (constructor_p && nested_name_specifier
26304 && CLASS_TYPE_P (nested_name_specifier))
26306 tree id = cp_parser_unqualified_id (parser,
26307 /*template_keyword_p=*/false,
26308 /*check_dependency_p=*/false,
26309 /*declarator_p=*/true,
26310 /*optional_p=*/false);
26311 if (is_overloaded_fn (id))
26312 id = DECL_NAME (get_first_fn (id));
26313 if (!constructor_name_p (id, nested_name_specifier))
26314 constructor_p = false;
26316 /* If we still think that this might be a constructor-declarator,
26317 look for a class-name. */
26318 else if (constructor_p)
26320 /* If we have:
26322 template <typename T> struct S {
26323 S();
26326 we must recognize that the nested `S' names a class. */
26327 if (cxx_dialect >= cxx1z)
26328 cp_parser_parse_tentatively (parser);
26330 tree type_decl;
26331 type_decl = cp_parser_class_name (parser,
26332 /*typename_keyword_p=*/false,
26333 /*template_keyword_p=*/false,
26334 none_type,
26335 /*check_dependency_p=*/false,
26336 /*class_head_p=*/false,
26337 /*is_declaration=*/false);
26339 if (cxx_dialect >= cxx1z
26340 && !cp_parser_parse_definitely (parser))
26342 type_decl = NULL_TREE;
26343 tree tmpl = cp_parser_template_name (parser,
26344 /*template_keyword*/false,
26345 /*check_dependency_p*/false,
26346 /*is_declaration*/false,
26347 none_type,
26348 /*is_identifier*/NULL);
26349 if (DECL_CLASS_TEMPLATE_P (tmpl)
26350 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26351 /* It's a deduction guide, return true. */;
26352 else
26353 cp_parser_simulate_error (parser);
26356 /* If there was no class-name, then this is not a constructor.
26357 Otherwise, if we are in a class-specifier and we aren't
26358 handling a friend declaration, check that its type matches
26359 current_class_type (c++/38313). Note: error_mark_node
26360 is left alone for error recovery purposes. */
26361 constructor_p = (!cp_parser_error_occurred (parser)
26362 && (outside_class_specifier_p
26363 || type_decl == NULL_TREE
26364 || type_decl == error_mark_node
26365 || same_type_p (current_class_type,
26366 TREE_TYPE (type_decl))));
26368 /* If we're still considering a constructor, we have to see a `(',
26369 to begin the parameter-declaration-clause, followed by either a
26370 `)', an `...', or a decl-specifier. We need to check for a
26371 type-specifier to avoid being fooled into thinking that:
26373 S (f) (int);
26375 is a constructor. (It is actually a function named `f' that
26376 takes one parameter (of type `int') and returns a value of type
26377 `S'. */
26378 if (constructor_p
26379 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26380 constructor_p = false;
26382 if (constructor_p
26383 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26384 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26385 /* A parameter declaration begins with a decl-specifier,
26386 which is either the "attribute" keyword, a storage class
26387 specifier, or (usually) a type-specifier. */
26388 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26390 tree type;
26391 tree pushed_scope = NULL_TREE;
26392 unsigned saved_num_template_parameter_lists;
26394 /* Names appearing in the type-specifier should be looked up
26395 in the scope of the class. */
26396 if (current_class_type)
26397 type = NULL_TREE;
26398 else if (type_decl)
26400 type = TREE_TYPE (type_decl);
26401 if (TREE_CODE (type) == TYPENAME_TYPE)
26403 type = resolve_typename_type (type,
26404 /*only_current_p=*/false);
26405 if (TREE_CODE (type) == TYPENAME_TYPE)
26407 cp_parser_abort_tentative_parse (parser);
26408 return false;
26411 pushed_scope = push_scope (type);
26414 /* Inside the constructor parameter list, surrounding
26415 template-parameter-lists do not apply. */
26416 saved_num_template_parameter_lists
26417 = parser->num_template_parameter_lists;
26418 parser->num_template_parameter_lists = 0;
26420 /* Look for the type-specifier. */
26421 cp_parser_type_specifier (parser,
26422 CP_PARSER_FLAGS_NONE,
26423 /*decl_specs=*/NULL,
26424 /*is_declarator=*/true,
26425 /*declares_class_or_enum=*/NULL,
26426 /*is_cv_qualifier=*/NULL);
26428 parser->num_template_parameter_lists
26429 = saved_num_template_parameter_lists;
26431 /* Leave the scope of the class. */
26432 if (pushed_scope)
26433 pop_scope (pushed_scope);
26435 constructor_p = !cp_parser_error_occurred (parser);
26439 /* We did not really want to consume any tokens. */
26440 cp_parser_abort_tentative_parse (parser);
26442 return constructor_p;
26445 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26446 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26447 they must be performed once we are in the scope of the function.
26449 Returns the function defined. */
26451 static tree
26452 cp_parser_function_definition_from_specifiers_and_declarator
26453 (cp_parser* parser,
26454 cp_decl_specifier_seq *decl_specifiers,
26455 tree attributes,
26456 const cp_declarator *declarator)
26458 tree fn;
26459 bool success_p;
26461 /* Begin the function-definition. */
26462 success_p = start_function (decl_specifiers, declarator, attributes);
26464 /* The things we're about to see are not directly qualified by any
26465 template headers we've seen thus far. */
26466 reset_specialization ();
26468 /* If there were names looked up in the decl-specifier-seq that we
26469 did not check, check them now. We must wait until we are in the
26470 scope of the function to perform the checks, since the function
26471 might be a friend. */
26472 perform_deferred_access_checks (tf_warning_or_error);
26474 if (success_p)
26476 cp_finalize_omp_declare_simd (parser, current_function_decl);
26477 parser->omp_declare_simd = NULL;
26478 cp_finalize_oacc_routine (parser, current_function_decl, true);
26479 parser->oacc_routine = NULL;
26482 if (!success_p)
26484 /* Skip the entire function. */
26485 cp_parser_skip_to_end_of_block_or_statement (parser);
26486 fn = error_mark_node;
26488 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26490 /* Seen already, skip it. An error message has already been output. */
26491 cp_parser_skip_to_end_of_block_or_statement (parser);
26492 fn = current_function_decl;
26493 current_function_decl = NULL_TREE;
26494 /* If this is a function from a class, pop the nested class. */
26495 if (current_class_name)
26496 pop_nested_class ();
26498 else
26500 timevar_id_t tv;
26501 if (DECL_DECLARED_INLINE_P (current_function_decl))
26502 tv = TV_PARSE_INLINE;
26503 else
26504 tv = TV_PARSE_FUNC;
26505 timevar_push (tv);
26506 fn = cp_parser_function_definition_after_declarator (parser,
26507 /*inline_p=*/false);
26508 timevar_pop (tv);
26511 return fn;
26514 /* Parse the part of a function-definition that follows the
26515 declarator. INLINE_P is TRUE iff this function is an inline
26516 function defined within a class-specifier.
26518 Returns the function defined. */
26520 static tree
26521 cp_parser_function_definition_after_declarator (cp_parser* parser,
26522 bool inline_p)
26524 tree fn;
26525 bool ctor_initializer_p = false;
26526 bool saved_in_unbraced_linkage_specification_p;
26527 bool saved_in_function_body;
26528 unsigned saved_num_template_parameter_lists;
26529 cp_token *token;
26530 bool fully_implicit_function_template_p
26531 = parser->fully_implicit_function_template_p;
26532 parser->fully_implicit_function_template_p = false;
26533 tree implicit_template_parms
26534 = parser->implicit_template_parms;
26535 parser->implicit_template_parms = 0;
26536 cp_binding_level* implicit_template_scope
26537 = parser->implicit_template_scope;
26538 parser->implicit_template_scope = 0;
26540 saved_in_function_body = parser->in_function_body;
26541 parser->in_function_body = true;
26542 /* If the next token is `return', then the code may be trying to
26543 make use of the "named return value" extension that G++ used to
26544 support. */
26545 token = cp_lexer_peek_token (parser->lexer);
26546 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26548 /* Consume the `return' keyword. */
26549 cp_lexer_consume_token (parser->lexer);
26550 /* Look for the identifier that indicates what value is to be
26551 returned. */
26552 cp_parser_identifier (parser);
26553 /* Issue an error message. */
26554 error_at (token->location,
26555 "named return values are no longer supported");
26556 /* Skip tokens until we reach the start of the function body. */
26557 while (true)
26559 cp_token *token = cp_lexer_peek_token (parser->lexer);
26560 if (token->type == CPP_OPEN_BRACE
26561 || token->type == CPP_EOF
26562 || token->type == CPP_PRAGMA_EOL)
26563 break;
26564 cp_lexer_consume_token (parser->lexer);
26567 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26568 anything declared inside `f'. */
26569 saved_in_unbraced_linkage_specification_p
26570 = parser->in_unbraced_linkage_specification_p;
26571 parser->in_unbraced_linkage_specification_p = false;
26572 /* Inside the function, surrounding template-parameter-lists do not
26573 apply. */
26574 saved_num_template_parameter_lists
26575 = parser->num_template_parameter_lists;
26576 parser->num_template_parameter_lists = 0;
26578 start_lambda_scope (current_function_decl);
26580 /* If the next token is `try', `__transaction_atomic', or
26581 `__transaction_relaxed`, then we are looking at either function-try-block
26582 or function-transaction-block. Note that all of these include the
26583 function-body. */
26584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26585 ctor_initializer_p = cp_parser_function_transaction (parser,
26586 RID_TRANSACTION_ATOMIC);
26587 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26588 RID_TRANSACTION_RELAXED))
26589 ctor_initializer_p = cp_parser_function_transaction (parser,
26590 RID_TRANSACTION_RELAXED);
26591 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26592 ctor_initializer_p = cp_parser_function_try_block (parser);
26593 else
26594 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26595 (parser, /*in_function_try_block=*/false);
26597 finish_lambda_scope ();
26599 /* Finish the function. */
26600 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26601 (inline_p ? 2 : 0));
26602 /* Generate code for it, if necessary. */
26603 expand_or_defer_fn (fn);
26604 /* Restore the saved values. */
26605 parser->in_unbraced_linkage_specification_p
26606 = saved_in_unbraced_linkage_specification_p;
26607 parser->num_template_parameter_lists
26608 = saved_num_template_parameter_lists;
26609 parser->in_function_body = saved_in_function_body;
26611 parser->fully_implicit_function_template_p
26612 = fully_implicit_function_template_p;
26613 parser->implicit_template_parms
26614 = implicit_template_parms;
26615 parser->implicit_template_scope
26616 = implicit_template_scope;
26618 if (parser->fully_implicit_function_template_p)
26619 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26621 return fn;
26624 /* Parse a template-declaration body (following argument list). */
26626 static void
26627 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26628 tree parameter_list,
26629 bool member_p)
26631 tree decl = NULL_TREE;
26632 bool friend_p = false;
26634 /* We just processed one more parameter list. */
26635 ++parser->num_template_parameter_lists;
26637 /* Get the deferred access checks from the parameter list. These
26638 will be checked once we know what is being declared, as for a
26639 member template the checks must be performed in the scope of the
26640 class containing the member. */
26641 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26643 /* Tentatively parse for a new template parameter list, which can either be
26644 the template keyword or a template introduction. */
26645 if (cp_parser_template_declaration_after_export (parser, member_p))
26646 /* OK */;
26647 else if (cxx_dialect >= cxx11
26648 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26649 decl = cp_parser_alias_declaration (parser);
26650 else
26652 /* There are no access checks when parsing a template, as we do not
26653 know if a specialization will be a friend. */
26654 push_deferring_access_checks (dk_no_check);
26655 cp_token *token = cp_lexer_peek_token (parser->lexer);
26656 decl = cp_parser_single_declaration (parser,
26657 checks,
26658 member_p,
26659 /*explicit_specialization_p=*/false,
26660 &friend_p);
26661 pop_deferring_access_checks ();
26663 /* If this is a member template declaration, let the front
26664 end know. */
26665 if (member_p && !friend_p && decl)
26667 if (TREE_CODE (decl) == TYPE_DECL)
26668 cp_parser_check_access_in_redeclaration (decl, token->location);
26670 decl = finish_member_template_decl (decl);
26672 else if (friend_p && decl
26673 && DECL_DECLARES_TYPE_P (decl))
26674 make_friend_class (current_class_type, TREE_TYPE (decl),
26675 /*complain=*/true);
26677 /* We are done with the current parameter list. */
26678 --parser->num_template_parameter_lists;
26680 pop_deferring_access_checks ();
26682 /* Finish up. */
26683 finish_template_decl (parameter_list);
26685 /* Check the template arguments for a literal operator template. */
26686 if (decl
26687 && DECL_DECLARES_FUNCTION_P (decl)
26688 && UDLIT_OPER_P (DECL_NAME (decl)))
26690 bool ok = true;
26691 if (parameter_list == NULL_TREE)
26692 ok = false;
26693 else
26695 int num_parms = TREE_VEC_LENGTH (parameter_list);
26696 if (num_parms == 1)
26698 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26699 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26700 if (TREE_TYPE (parm) != char_type_node
26701 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26702 ok = false;
26704 else if (num_parms == 2 && cxx_dialect >= cxx14)
26706 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26707 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26708 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26709 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26710 if (parm == error_mark_node
26711 || TREE_TYPE (parm) != TREE_TYPE (type)
26712 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26713 ok = false;
26715 else
26716 ok = false;
26718 if (!ok)
26720 if (cxx_dialect >= cxx14)
26721 error ("literal operator template %qD has invalid parameter list."
26722 " Expected non-type template argument pack <char...>"
26723 " or <typename CharT, CharT...>",
26724 decl);
26725 else
26726 error ("literal operator template %qD has invalid parameter list."
26727 " Expected non-type template argument pack <char...>",
26728 decl);
26732 /* Register member declarations. */
26733 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26734 finish_member_declaration (decl);
26735 /* If DECL is a function template, we must return to parse it later.
26736 (Even though there is no definition, there might be default
26737 arguments that need handling.) */
26738 if (member_p && decl
26739 && DECL_DECLARES_FUNCTION_P (decl))
26740 vec_safe_push (unparsed_funs_with_definitions, decl);
26743 /* Parse a template introduction header for a template-declaration. Returns
26744 false if tentative parse fails. */
26746 static bool
26747 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26749 cp_parser_parse_tentatively (parser);
26751 tree saved_scope = parser->scope;
26752 tree saved_object_scope = parser->object_scope;
26753 tree saved_qualifying_scope = parser->qualifying_scope;
26755 /* Look for the optional `::' operator. */
26756 cp_parser_global_scope_opt (parser,
26757 /*current_scope_valid_p=*/false);
26758 /* Look for the nested-name-specifier. */
26759 cp_parser_nested_name_specifier_opt (parser,
26760 /*typename_keyword_p=*/false,
26761 /*check_dependency_p=*/true,
26762 /*type_p=*/false,
26763 /*is_declaration=*/false);
26765 cp_token *token = cp_lexer_peek_token (parser->lexer);
26766 tree concept_name = cp_parser_identifier (parser);
26768 /* Look up the concept for which we will be matching
26769 template parameters. */
26770 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26771 token->location);
26772 parser->scope = saved_scope;
26773 parser->object_scope = saved_object_scope;
26774 parser->qualifying_scope = saved_qualifying_scope;
26776 if (concept_name == error_mark_node)
26777 cp_parser_simulate_error (parser);
26779 /* Look for opening brace for introduction. */
26780 matching_braces braces;
26781 braces.require_open (parser);
26783 if (!cp_parser_parse_definitely (parser))
26784 return false;
26786 push_deferring_access_checks (dk_deferred);
26788 /* Build vector of placeholder parameters and grab
26789 matching identifiers. */
26790 tree introduction_list = cp_parser_introduction_list (parser);
26792 /* The introduction-list shall not be empty. */
26793 int nargs = TREE_VEC_LENGTH (introduction_list);
26794 if (nargs == 0)
26796 error ("empty introduction-list");
26797 return true;
26800 /* Look for closing brace for introduction. */
26801 if (!braces.require_close (parser))
26802 return true;
26804 if (tmpl_decl == error_mark_node)
26806 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26807 token->location);
26808 return true;
26811 /* Build and associate the constraint. */
26812 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26813 if (parms && parms != error_mark_node)
26815 cp_parser_template_declaration_after_parameters (parser, parms,
26816 member_p);
26817 return true;
26820 error_at (token->location, "no matching concept for template-introduction");
26821 return true;
26824 /* Parse a normal template-declaration following the template keyword. */
26826 static void
26827 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26829 tree parameter_list;
26830 bool need_lang_pop;
26831 location_t location = input_location;
26833 /* Look for the `<' token. */
26834 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26835 return;
26836 if (at_class_scope_p () && current_function_decl)
26838 /* 14.5.2.2 [temp.mem]
26840 A local class shall not have member templates. */
26841 error_at (location,
26842 "invalid declaration of member template in local class");
26843 cp_parser_skip_to_end_of_block_or_statement (parser);
26844 return;
26846 /* [temp]
26848 A template ... shall not have C linkage. */
26849 if (current_lang_name == lang_name_c)
26851 error_at (location, "template with C linkage");
26852 /* Give it C++ linkage to avoid confusing other parts of the
26853 front end. */
26854 push_lang_context (lang_name_cplusplus);
26855 need_lang_pop = true;
26857 else
26858 need_lang_pop = false;
26860 /* We cannot perform access checks on the template parameter
26861 declarations until we know what is being declared, just as we
26862 cannot check the decl-specifier list. */
26863 push_deferring_access_checks (dk_deferred);
26865 /* If the next token is `>', then we have an invalid
26866 specialization. Rather than complain about an invalid template
26867 parameter, issue an error message here. */
26868 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26870 cp_parser_error (parser, "invalid explicit specialization");
26871 begin_specialization ();
26872 parameter_list = NULL_TREE;
26874 else
26876 /* Parse the template parameters. */
26877 parameter_list = cp_parser_template_parameter_list (parser);
26880 /* Look for the `>'. */
26881 cp_parser_skip_to_end_of_template_parameter_list (parser);
26883 /* Manage template requirements */
26884 if (flag_concepts)
26886 tree reqs = get_shorthand_constraints (current_template_parms);
26887 if (tree r = cp_parser_requires_clause_opt (parser))
26888 reqs = conjoin_constraints (reqs, normalize_expression (r));
26889 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26892 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26893 member_p);
26895 /* For the erroneous case of a template with C linkage, we pushed an
26896 implicit C++ linkage scope; exit that scope now. */
26897 if (need_lang_pop)
26898 pop_lang_context ();
26901 /* Parse a template-declaration, assuming that the `export' (and
26902 `extern') keywords, if present, has already been scanned. MEMBER_P
26903 is as for cp_parser_template_declaration. */
26905 static bool
26906 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26908 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26910 cp_lexer_consume_token (parser->lexer);
26911 cp_parser_explicit_template_declaration (parser, member_p);
26912 return true;
26914 else if (flag_concepts)
26915 return cp_parser_template_introduction (parser, member_p);
26917 return false;
26920 /* Perform the deferred access checks from a template-parameter-list.
26921 CHECKS is a TREE_LIST of access checks, as returned by
26922 get_deferred_access_checks. */
26924 static void
26925 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26927 ++processing_template_parmlist;
26928 perform_access_checks (checks, tf_warning_or_error);
26929 --processing_template_parmlist;
26932 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26933 `function-definition' sequence that follows a template header.
26934 If MEMBER_P is true, this declaration appears in a class scope.
26936 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26937 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26939 static tree
26940 cp_parser_single_declaration (cp_parser* parser,
26941 vec<deferred_access_check, va_gc> *checks,
26942 bool member_p,
26943 bool explicit_specialization_p,
26944 bool* friend_p)
26946 int declares_class_or_enum;
26947 tree decl = NULL_TREE;
26948 cp_decl_specifier_seq decl_specifiers;
26949 bool function_definition_p = false;
26950 cp_token *decl_spec_token_start;
26952 /* This function is only used when processing a template
26953 declaration. */
26954 gcc_assert (innermost_scope_kind () == sk_template_parms
26955 || innermost_scope_kind () == sk_template_spec);
26957 /* Defer access checks until we know what is being declared. */
26958 push_deferring_access_checks (dk_deferred);
26960 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26961 alternative. */
26962 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26963 cp_parser_decl_specifier_seq (parser,
26964 CP_PARSER_FLAGS_OPTIONAL,
26965 &decl_specifiers,
26966 &declares_class_or_enum);
26967 if (friend_p)
26968 *friend_p = cp_parser_friend_p (&decl_specifiers);
26970 /* There are no template typedefs. */
26971 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26973 error_at (decl_spec_token_start->location,
26974 "template declaration of %<typedef%>");
26975 decl = error_mark_node;
26978 /* Gather up the access checks that occurred the
26979 decl-specifier-seq. */
26980 stop_deferring_access_checks ();
26982 /* Check for the declaration of a template class. */
26983 if (declares_class_or_enum)
26985 if (cp_parser_declares_only_class_p (parser)
26986 || (declares_class_or_enum & 2))
26988 // If this is a declaration, but not a definition, associate
26989 // any constraints with the type declaration. Constraints
26990 // are associated with definitions in cp_parser_class_specifier.
26991 if (declares_class_or_enum == 1)
26992 associate_classtype_constraints (decl_specifiers.type);
26994 decl = shadow_tag (&decl_specifiers);
26996 /* In this case:
26998 struct C {
26999 friend template <typename T> struct A<T>::B;
27002 A<T>::B will be represented by a TYPENAME_TYPE, and
27003 therefore not recognized by shadow_tag. */
27004 if (friend_p && *friend_p
27005 && !decl
27006 && decl_specifiers.type
27007 && TYPE_P (decl_specifiers.type))
27008 decl = decl_specifiers.type;
27010 if (decl && decl != error_mark_node)
27011 decl = TYPE_NAME (decl);
27012 else
27013 decl = error_mark_node;
27015 /* Perform access checks for template parameters. */
27016 cp_parser_perform_template_parameter_access_checks (checks);
27018 /* Give a helpful diagnostic for
27019 template <class T> struct A { } a;
27020 if we aren't already recovering from an error. */
27021 if (!cp_parser_declares_only_class_p (parser)
27022 && !seen_error ())
27024 error_at (cp_lexer_peek_token (parser->lexer)->location,
27025 "a class template declaration must not declare "
27026 "anything else");
27027 cp_parser_skip_to_end_of_block_or_statement (parser);
27028 goto out;
27033 /* Complain about missing 'typename' or other invalid type names. */
27034 if (!decl_specifiers.any_type_specifiers_p
27035 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
27037 /* cp_parser_parse_and_diagnose_invalid_type_name calls
27038 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
27039 the rest of this declaration. */
27040 decl = error_mark_node;
27041 goto out;
27044 /* If it's not a template class, try for a template function. If
27045 the next token is a `;', then this declaration does not declare
27046 anything. But, if there were errors in the decl-specifiers, then
27047 the error might well have come from an attempted class-specifier.
27048 In that case, there's no need to warn about a missing declarator. */
27049 if (!decl
27050 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
27051 || decl_specifiers.type != error_mark_node))
27053 decl = cp_parser_init_declarator (parser,
27054 &decl_specifiers,
27055 checks,
27056 /*function_definition_allowed_p=*/true,
27057 member_p,
27058 declares_class_or_enum,
27059 &function_definition_p,
27060 NULL, NULL, NULL);
27062 /* 7.1.1-1 [dcl.stc]
27064 A storage-class-specifier shall not be specified in an explicit
27065 specialization... */
27066 if (decl
27067 && explicit_specialization_p
27068 && decl_specifiers.storage_class != sc_none)
27070 error_at (decl_spec_token_start->location,
27071 "explicit template specialization cannot have a storage class");
27072 decl = error_mark_node;
27075 if (decl && VAR_P (decl))
27076 check_template_variable (decl);
27079 /* Look for a trailing `;' after the declaration. */
27080 if (!function_definition_p
27081 && (decl == error_mark_node
27082 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
27083 cp_parser_skip_to_end_of_block_or_statement (parser);
27085 out:
27086 pop_deferring_access_checks ();
27088 /* Clear any current qualification; whatever comes next is the start
27089 of something new. */
27090 parser->scope = NULL_TREE;
27091 parser->qualifying_scope = NULL_TREE;
27092 parser->object_scope = NULL_TREE;
27094 return decl;
27097 /* Parse a cast-expression that is not the operand of a unary "&". */
27099 static cp_expr
27100 cp_parser_simple_cast_expression (cp_parser *parser)
27102 return cp_parser_cast_expression (parser, /*address_p=*/false,
27103 /*cast_p=*/false, /*decltype*/false, NULL);
27106 /* Parse a functional cast to TYPE. Returns an expression
27107 representing the cast. */
27109 static cp_expr
27110 cp_parser_functional_cast (cp_parser* parser, tree type)
27112 vec<tree, va_gc> *vec;
27113 tree expression_list;
27114 cp_expr cast;
27115 bool nonconst_p;
27117 location_t start_loc = input_location;
27119 if (!type)
27120 type = error_mark_node;
27122 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
27124 cp_lexer_set_source_position (parser->lexer);
27125 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27126 expression_list = cp_parser_braced_list (parser, &nonconst_p);
27127 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
27128 if (TREE_CODE (type) == TYPE_DECL)
27129 type = TREE_TYPE (type);
27131 cast = finish_compound_literal (type, expression_list,
27132 tf_warning_or_error, fcl_functional);
27133 /* Create a location of the form:
27134 type_name{i, f}
27135 ^~~~~~~~~~~~~~~
27136 with caret == start at the start of the type name,
27137 finishing at the closing brace. */
27138 location_t finish_loc
27139 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27140 location_t combined_loc = make_location (start_loc, start_loc,
27141 finish_loc);
27142 cast.set_location (combined_loc);
27143 return cast;
27147 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27148 /*cast_p=*/true,
27149 /*allow_expansion_p=*/true,
27150 /*non_constant_p=*/NULL);
27151 if (vec == NULL)
27152 expression_list = error_mark_node;
27153 else
27155 expression_list = build_tree_list_vec (vec);
27156 release_tree_vector (vec);
27159 cast = build_functional_cast (type, expression_list,
27160 tf_warning_or_error);
27161 /* [expr.const]/1: In an integral constant expression "only type
27162 conversions to integral or enumeration type can be used". */
27163 if (TREE_CODE (type) == TYPE_DECL)
27164 type = TREE_TYPE (type);
27165 if (cast != error_mark_node
27166 && !cast_valid_in_integral_constant_expression_p (type)
27167 && cp_parser_non_integral_constant_expression (parser,
27168 NIC_CONSTRUCTOR))
27169 return error_mark_node;
27171 /* Create a location of the form:
27172 float(i)
27173 ^~~~~~~~
27174 with caret == start at the start of the type name,
27175 finishing at the closing paren. */
27176 location_t finish_loc
27177 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27178 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27179 cast.set_location (combined_loc);
27180 return cast;
27183 /* Save the tokens that make up the body of a member function defined
27184 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27185 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27186 specifiers applied to the declaration. Returns the FUNCTION_DECL
27187 for the member function. */
27189 static tree
27190 cp_parser_save_member_function_body (cp_parser* parser,
27191 cp_decl_specifier_seq *decl_specifiers,
27192 cp_declarator *declarator,
27193 tree attributes)
27195 cp_token *first;
27196 cp_token *last;
27197 tree fn;
27198 bool function_try_block = false;
27200 /* Create the FUNCTION_DECL. */
27201 fn = grokmethod (decl_specifiers, declarator, attributes);
27202 cp_finalize_omp_declare_simd (parser, fn);
27203 cp_finalize_oacc_routine (parser, fn, true);
27204 /* If something went badly wrong, bail out now. */
27205 if (fn == error_mark_node)
27207 /* If there's a function-body, skip it. */
27208 if (cp_parser_token_starts_function_definition_p
27209 (cp_lexer_peek_token (parser->lexer)))
27210 cp_parser_skip_to_end_of_block_or_statement (parser);
27211 return error_mark_node;
27214 /* Remember it, if there default args to post process. */
27215 cp_parser_save_default_args (parser, fn);
27217 /* Save away the tokens that make up the body of the
27218 function. */
27219 first = parser->lexer->next_token;
27221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27222 cp_lexer_consume_token (parser->lexer);
27223 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27224 RID_TRANSACTION_ATOMIC))
27226 cp_lexer_consume_token (parser->lexer);
27227 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27228 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27229 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27230 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27231 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27232 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27233 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27235 cp_lexer_consume_token (parser->lexer);
27236 cp_lexer_consume_token (parser->lexer);
27237 cp_lexer_consume_token (parser->lexer);
27238 cp_lexer_consume_token (parser->lexer);
27239 cp_lexer_consume_token (parser->lexer);
27241 else
27242 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27243 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27245 cp_lexer_consume_token (parser->lexer);
27246 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27247 break;
27251 /* Handle function try blocks. */
27252 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27254 cp_lexer_consume_token (parser->lexer);
27255 function_try_block = true;
27257 /* We can have braced-init-list mem-initializers before the fn body. */
27258 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27260 cp_lexer_consume_token (parser->lexer);
27261 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27263 /* cache_group will stop after an un-nested { } pair, too. */
27264 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27265 break;
27267 /* variadic mem-inits have ... after the ')'. */
27268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27269 cp_lexer_consume_token (parser->lexer);
27272 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27273 /* Handle function try blocks. */
27274 if (function_try_block)
27275 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27276 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27277 last = parser->lexer->next_token;
27279 /* Save away the inline definition; we will process it when the
27280 class is complete. */
27281 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27282 DECL_PENDING_INLINE_P (fn) = 1;
27284 /* We need to know that this was defined in the class, so that
27285 friend templates are handled correctly. */
27286 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27288 /* Add FN to the queue of functions to be parsed later. */
27289 vec_safe_push (unparsed_funs_with_definitions, fn);
27291 return fn;
27294 /* Save the tokens that make up the in-class initializer for a non-static
27295 data member. Returns a DEFAULT_ARG. */
27297 static tree
27298 cp_parser_save_nsdmi (cp_parser* parser)
27300 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27303 /* Parse a template-argument-list, as well as the trailing ">" (but
27304 not the opening "<"). See cp_parser_template_argument_list for the
27305 return value. */
27307 static tree
27308 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27310 tree arguments;
27311 tree saved_scope;
27312 tree saved_qualifying_scope;
27313 tree saved_object_scope;
27314 bool saved_greater_than_is_operator_p;
27315 int saved_unevaluated_operand;
27316 int saved_inhibit_evaluation_warnings;
27318 /* [temp.names]
27320 When parsing a template-id, the first non-nested `>' is taken as
27321 the end of the template-argument-list rather than a greater-than
27322 operator. */
27323 saved_greater_than_is_operator_p
27324 = parser->greater_than_is_operator_p;
27325 parser->greater_than_is_operator_p = false;
27326 /* Parsing the argument list may modify SCOPE, so we save it
27327 here. */
27328 saved_scope = parser->scope;
27329 saved_qualifying_scope = parser->qualifying_scope;
27330 saved_object_scope = parser->object_scope;
27331 /* We need to evaluate the template arguments, even though this
27332 template-id may be nested within a "sizeof". */
27333 saved_unevaluated_operand = cp_unevaluated_operand;
27334 cp_unevaluated_operand = 0;
27335 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27336 c_inhibit_evaluation_warnings = 0;
27337 /* Parse the template-argument-list itself. */
27338 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27339 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27340 arguments = NULL_TREE;
27341 else
27342 arguments = cp_parser_template_argument_list (parser);
27343 /* Look for the `>' that ends the template-argument-list. If we find
27344 a '>>' instead, it's probably just a typo. */
27345 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27347 if (cxx_dialect != cxx98)
27349 /* In C++0x, a `>>' in a template argument list or cast
27350 expression is considered to be two separate `>'
27351 tokens. So, change the current token to a `>', but don't
27352 consume it: it will be consumed later when the outer
27353 template argument list (or cast expression) is parsed.
27354 Note that this replacement of `>' for `>>' is necessary
27355 even if we are parsing tentatively: in the tentative
27356 case, after calling
27357 cp_parser_enclosed_template_argument_list we will always
27358 throw away all of the template arguments and the first
27359 closing `>', either because the template argument list
27360 was erroneous or because we are replacing those tokens
27361 with a CPP_TEMPLATE_ID token. The second `>' (which will
27362 not have been thrown away) is needed either to close an
27363 outer template argument list or to complete a new-style
27364 cast. */
27365 cp_token *token = cp_lexer_peek_token (parser->lexer);
27366 token->type = CPP_GREATER;
27368 else if (!saved_greater_than_is_operator_p)
27370 /* If we're in a nested template argument list, the '>>' has
27371 to be a typo for '> >'. We emit the error message, but we
27372 continue parsing and we push a '>' as next token, so that
27373 the argument list will be parsed correctly. Note that the
27374 global source location is still on the token before the
27375 '>>', so we need to say explicitly where we want it. */
27376 cp_token *token = cp_lexer_peek_token (parser->lexer);
27377 gcc_rich_location richloc (token->location);
27378 richloc.add_fixit_replace ("> >");
27379 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27380 "within a nested template argument list");
27382 token->type = CPP_GREATER;
27384 else
27386 /* If this is not a nested template argument list, the '>>'
27387 is a typo for '>'. Emit an error message and continue.
27388 Same deal about the token location, but here we can get it
27389 right by consuming the '>>' before issuing the diagnostic. */
27390 cp_token *token = cp_lexer_consume_token (parser->lexer);
27391 error_at (token->location,
27392 "spurious %<>>%>, use %<>%> to terminate "
27393 "a template argument list");
27396 else
27397 cp_parser_skip_to_end_of_template_parameter_list (parser);
27398 /* The `>' token might be a greater-than operator again now. */
27399 parser->greater_than_is_operator_p
27400 = saved_greater_than_is_operator_p;
27401 /* Restore the SAVED_SCOPE. */
27402 parser->scope = saved_scope;
27403 parser->qualifying_scope = saved_qualifying_scope;
27404 parser->object_scope = saved_object_scope;
27405 cp_unevaluated_operand = saved_unevaluated_operand;
27406 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27408 return arguments;
27411 /* MEMBER_FUNCTION is a member function, or a friend. If default
27412 arguments, or the body of the function have not yet been parsed,
27413 parse them now. */
27415 static void
27416 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27418 timevar_push (TV_PARSE_INMETH);
27419 /* If this member is a template, get the underlying
27420 FUNCTION_DECL. */
27421 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27422 member_function = DECL_TEMPLATE_RESULT (member_function);
27424 /* There should not be any class definitions in progress at this
27425 point; the bodies of members are only parsed outside of all class
27426 definitions. */
27427 gcc_assert (parser->num_classes_being_defined == 0);
27428 /* While we're parsing the member functions we might encounter more
27429 classes. We want to handle them right away, but we don't want
27430 them getting mixed up with functions that are currently in the
27431 queue. */
27432 push_unparsed_function_queues (parser);
27434 /* Make sure that any template parameters are in scope. */
27435 maybe_begin_member_template_processing (member_function);
27437 /* If the body of the function has not yet been parsed, parse it
27438 now. */
27439 if (DECL_PENDING_INLINE_P (member_function))
27441 tree function_scope;
27442 cp_token_cache *tokens;
27444 /* The function is no longer pending; we are processing it. */
27445 tokens = DECL_PENDING_INLINE_INFO (member_function);
27446 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27447 DECL_PENDING_INLINE_P (member_function) = 0;
27449 /* If this is a local class, enter the scope of the containing
27450 function. */
27451 function_scope = current_function_decl;
27452 if (function_scope)
27453 push_function_context ();
27455 /* Push the body of the function onto the lexer stack. */
27456 cp_parser_push_lexer_for_tokens (parser, tokens);
27458 /* Let the front end know that we going to be defining this
27459 function. */
27460 start_preparsed_function (member_function, NULL_TREE,
27461 SF_PRE_PARSED | SF_INCLASS_INLINE);
27463 /* Don't do access checking if it is a templated function. */
27464 if (processing_template_decl)
27465 push_deferring_access_checks (dk_no_check);
27467 /* #pragma omp declare reduction needs special parsing. */
27468 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27470 parser->lexer->in_pragma = true;
27471 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27472 finish_function (/*inline*/2);
27473 cp_check_omp_declare_reduction (member_function);
27475 else
27476 /* Now, parse the body of the function. */
27477 cp_parser_function_definition_after_declarator (parser,
27478 /*inline_p=*/true);
27480 if (processing_template_decl)
27481 pop_deferring_access_checks ();
27483 /* Leave the scope of the containing function. */
27484 if (function_scope)
27485 pop_function_context ();
27486 cp_parser_pop_lexer (parser);
27489 /* Remove any template parameters from the symbol table. */
27490 maybe_end_member_template_processing ();
27492 /* Restore the queue. */
27493 pop_unparsed_function_queues (parser);
27494 timevar_pop (TV_PARSE_INMETH);
27497 /* If DECL contains any default args, remember it on the unparsed
27498 functions queue. */
27500 static void
27501 cp_parser_save_default_args (cp_parser* parser, tree decl)
27503 tree probe;
27505 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27506 probe;
27507 probe = TREE_CHAIN (probe))
27508 if (TREE_PURPOSE (probe))
27510 cp_default_arg_entry entry = {current_class_type, decl};
27511 vec_safe_push (unparsed_funs_with_default_args, entry);
27512 break;
27516 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27517 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27518 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27519 from the parameter-type-list. */
27521 static tree
27522 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27523 tree default_arg, tree parmtype)
27525 cp_token_cache *tokens;
27526 tree parsed_arg;
27527 bool dummy;
27529 if (default_arg == error_mark_node)
27530 return error_mark_node;
27532 /* Push the saved tokens for the default argument onto the parser's
27533 lexer stack. */
27534 tokens = DEFARG_TOKENS (default_arg);
27535 cp_parser_push_lexer_for_tokens (parser, tokens);
27537 start_lambda_scope (decl);
27539 /* Parse the default argument. */
27540 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27541 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27542 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27544 finish_lambda_scope ();
27546 if (parsed_arg == error_mark_node)
27547 cp_parser_skip_to_end_of_statement (parser);
27549 if (!processing_template_decl)
27551 /* In a non-template class, check conversions now. In a template,
27552 we'll wait and instantiate these as needed. */
27553 if (TREE_CODE (decl) == PARM_DECL)
27554 parsed_arg = check_default_argument (parmtype, parsed_arg,
27555 tf_warning_or_error);
27556 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27557 parsed_arg = error_mark_node;
27558 else
27559 parsed_arg = digest_nsdmi_init (decl, parsed_arg, tf_warning_or_error);
27562 /* If the token stream has not been completely used up, then
27563 there was extra junk after the end of the default
27564 argument. */
27565 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27567 if (TREE_CODE (decl) == PARM_DECL)
27568 cp_parser_error (parser, "expected %<,%>");
27569 else
27570 cp_parser_error (parser, "expected %<;%>");
27573 /* Revert to the main lexer. */
27574 cp_parser_pop_lexer (parser);
27576 return parsed_arg;
27579 /* FIELD is a non-static data member with an initializer which we saved for
27580 later; parse it now. */
27582 static void
27583 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27585 tree def;
27587 maybe_begin_member_template_processing (field);
27589 push_unparsed_function_queues (parser);
27590 def = cp_parser_late_parse_one_default_arg (parser, field,
27591 DECL_INITIAL (field),
27592 NULL_TREE);
27593 pop_unparsed_function_queues (parser);
27595 maybe_end_member_template_processing ();
27597 DECL_INITIAL (field) = def;
27600 /* FN is a FUNCTION_DECL which may contains a parameter with an
27601 unparsed DEFAULT_ARG. Parse the default args now. This function
27602 assumes that the current scope is the scope in which the default
27603 argument should be processed. */
27605 static void
27606 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27608 bool saved_local_variables_forbidden_p;
27609 tree parm, parmdecl;
27611 /* While we're parsing the default args, we might (due to the
27612 statement expression extension) encounter more classes. We want
27613 to handle them right away, but we don't want them getting mixed
27614 up with default args that are currently in the queue. */
27615 push_unparsed_function_queues (parser);
27617 /* Local variable names (and the `this' keyword) may not appear
27618 in a default argument. */
27619 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27620 parser->local_variables_forbidden_p = true;
27622 push_defarg_context (fn);
27624 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27625 parmdecl = DECL_ARGUMENTS (fn);
27626 parm && parm != void_list_node;
27627 parm = TREE_CHAIN (parm),
27628 parmdecl = DECL_CHAIN (parmdecl))
27630 tree default_arg = TREE_PURPOSE (parm);
27631 tree parsed_arg;
27632 vec<tree, va_gc> *insts;
27633 tree copy;
27634 unsigned ix;
27636 if (!default_arg)
27637 continue;
27639 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27640 /* This can happen for a friend declaration for a function
27641 already declared with default arguments. */
27642 continue;
27644 parsed_arg
27645 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27646 default_arg,
27647 TREE_VALUE (parm));
27648 if (parsed_arg == error_mark_node)
27650 continue;
27653 TREE_PURPOSE (parm) = parsed_arg;
27655 /* Update any instantiations we've already created. */
27656 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27657 vec_safe_iterate (insts, ix, &copy); ix++)
27658 TREE_PURPOSE (copy) = parsed_arg;
27661 pop_defarg_context ();
27663 /* Make sure no default arg is missing. */
27664 check_default_args (fn);
27666 /* Restore the state of local_variables_forbidden_p. */
27667 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27669 /* Restore the queue. */
27670 pop_unparsed_function_queues (parser);
27673 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27675 sizeof ... ( identifier )
27677 where the 'sizeof' token has already been consumed. */
27679 static tree
27680 cp_parser_sizeof_pack (cp_parser *parser)
27682 /* Consume the `...'. */
27683 cp_lexer_consume_token (parser->lexer);
27684 maybe_warn_variadic_templates ();
27686 matching_parens parens;
27687 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27688 if (paren)
27689 parens.consume_open (parser);
27690 else
27691 permerror (cp_lexer_peek_token (parser->lexer)->location,
27692 "%<sizeof...%> argument must be surrounded by parentheses");
27694 cp_token *token = cp_lexer_peek_token (parser->lexer);
27695 tree name = cp_parser_identifier (parser);
27696 if (name == error_mark_node)
27697 return error_mark_node;
27698 /* The name is not qualified. */
27699 parser->scope = NULL_TREE;
27700 parser->qualifying_scope = NULL_TREE;
27701 parser->object_scope = NULL_TREE;
27702 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27703 if (expr == error_mark_node)
27704 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27705 token->location);
27706 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27707 expr = TREE_TYPE (expr);
27708 else if (TREE_CODE (expr) == CONST_DECL)
27709 expr = DECL_INITIAL (expr);
27710 expr = make_pack_expansion (expr);
27711 PACK_EXPANSION_SIZEOF_P (expr) = true;
27713 if (paren)
27714 parens.require_close (parser);
27716 return expr;
27719 /* Parse the operand of `sizeof' (or a similar operator). Returns
27720 either a TYPE or an expression, depending on the form of the
27721 input. The KEYWORD indicates which kind of expression we have
27722 encountered. */
27724 static tree
27725 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27727 tree expr = NULL_TREE;
27728 const char *saved_message;
27729 char *tmp;
27730 bool saved_integral_constant_expression_p;
27731 bool saved_non_integral_constant_expression_p;
27733 /* If it's a `...', then we are computing the length of a parameter
27734 pack. */
27735 if (keyword == RID_SIZEOF
27736 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27737 return cp_parser_sizeof_pack (parser);
27739 /* Types cannot be defined in a `sizeof' expression. Save away the
27740 old message. */
27741 saved_message = parser->type_definition_forbidden_message;
27742 /* And create the new one. */
27743 tmp = concat ("types may not be defined in %<",
27744 IDENTIFIER_POINTER (ridpointers[keyword]),
27745 "%> expressions", NULL);
27746 parser->type_definition_forbidden_message = tmp;
27748 /* The restrictions on constant-expressions do not apply inside
27749 sizeof expressions. */
27750 saved_integral_constant_expression_p
27751 = parser->integral_constant_expression_p;
27752 saved_non_integral_constant_expression_p
27753 = parser->non_integral_constant_expression_p;
27754 parser->integral_constant_expression_p = false;
27756 /* Do not actually evaluate the expression. */
27757 ++cp_unevaluated_operand;
27758 ++c_inhibit_evaluation_warnings;
27759 /* If it's a `(', then we might be looking at the type-id
27760 construction. */
27761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27763 tree type = NULL_TREE;
27765 /* We can't be sure yet whether we're looking at a type-id or an
27766 expression. */
27767 cp_parser_parse_tentatively (parser);
27769 matching_parens parens;
27770 parens.consume_open (parser);
27772 /* Note: as a GNU Extension, compound literals are considered
27773 postfix-expressions as they are in C99, so they are valid
27774 arguments to sizeof. See comment in cp_parser_cast_expression
27775 for details. */
27776 if (cp_parser_compound_literal_p (parser))
27777 cp_parser_simulate_error (parser);
27778 else
27780 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27781 parser->in_type_id_in_expr_p = true;
27782 /* Look for the type-id. */
27783 type = cp_parser_type_id (parser);
27784 /* Look for the closing `)'. */
27785 parens.require_close (parser);
27786 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27789 /* If all went well, then we're done. */
27790 if (cp_parser_parse_definitely (parser))
27792 cp_decl_specifier_seq decl_specs;
27794 /* Build a trivial decl-specifier-seq. */
27795 clear_decl_specs (&decl_specs);
27796 decl_specs.type = type;
27798 /* Call grokdeclarator to figure out what type this is. */
27799 expr = grokdeclarator (NULL,
27800 &decl_specs,
27801 TYPENAME,
27802 /*initialized=*/0,
27803 /*attrlist=*/NULL);
27807 /* If the type-id production did not work out, then we must be
27808 looking at the unary-expression production. */
27809 if (!expr)
27810 expr = cp_parser_unary_expression (parser);
27812 /* Go back to evaluating expressions. */
27813 --cp_unevaluated_operand;
27814 --c_inhibit_evaluation_warnings;
27816 /* Free the message we created. */
27817 free (tmp);
27818 /* And restore the old one. */
27819 parser->type_definition_forbidden_message = saved_message;
27820 parser->integral_constant_expression_p
27821 = saved_integral_constant_expression_p;
27822 parser->non_integral_constant_expression_p
27823 = saved_non_integral_constant_expression_p;
27825 return expr;
27828 /* If the current declaration has no declarator, return true. */
27830 static bool
27831 cp_parser_declares_only_class_p (cp_parser *parser)
27833 /* If the next token is a `;' or a `,' then there is no
27834 declarator. */
27835 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27836 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27839 /* Update the DECL_SPECS to reflect the storage class indicated by
27840 KEYWORD. */
27842 static void
27843 cp_parser_set_storage_class (cp_parser *parser,
27844 cp_decl_specifier_seq *decl_specs,
27845 enum rid keyword,
27846 cp_token *token)
27848 cp_storage_class storage_class;
27850 if (parser->in_unbraced_linkage_specification_p)
27852 error_at (token->location, "invalid use of %qD in linkage specification",
27853 ridpointers[keyword]);
27854 return;
27856 else if (decl_specs->storage_class != sc_none)
27858 decl_specs->conflicting_specifiers_p = true;
27859 return;
27862 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27863 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27864 && decl_specs->gnu_thread_keyword_p)
27866 pedwarn (decl_specs->locations[ds_thread], 0,
27867 "%<__thread%> before %qD", ridpointers[keyword]);
27870 switch (keyword)
27872 case RID_AUTO:
27873 storage_class = sc_auto;
27874 break;
27875 case RID_REGISTER:
27876 storage_class = sc_register;
27877 break;
27878 case RID_STATIC:
27879 storage_class = sc_static;
27880 break;
27881 case RID_EXTERN:
27882 storage_class = sc_extern;
27883 break;
27884 case RID_MUTABLE:
27885 storage_class = sc_mutable;
27886 break;
27887 default:
27888 gcc_unreachable ();
27890 decl_specs->storage_class = storage_class;
27891 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27893 /* A storage class specifier cannot be applied alongside a typedef
27894 specifier. If there is a typedef specifier present then set
27895 conflicting_specifiers_p which will trigger an error later
27896 on in grokdeclarator. */
27897 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27898 decl_specs->conflicting_specifiers_p = true;
27901 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27902 is true, the type is a class or enum definition. */
27904 static void
27905 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27906 tree type_spec,
27907 cp_token *token,
27908 bool type_definition_p)
27910 decl_specs->any_specifiers_p = true;
27912 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27913 (with, for example, in "typedef int wchar_t;") we remember that
27914 this is what happened. In system headers, we ignore these
27915 declarations so that G++ can work with system headers that are not
27916 C++-safe. */
27917 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27918 && !type_definition_p
27919 && (type_spec == boolean_type_node
27920 || type_spec == char16_type_node
27921 || type_spec == char32_type_node
27922 || type_spec == wchar_type_node)
27923 && (decl_specs->type
27924 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27925 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27926 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27927 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27929 decl_specs->redefined_builtin_type = type_spec;
27930 set_and_check_decl_spec_loc (decl_specs,
27931 ds_redefined_builtin_type_spec,
27932 token);
27933 if (!decl_specs->type)
27935 decl_specs->type = type_spec;
27936 decl_specs->type_definition_p = false;
27937 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27940 else if (decl_specs->type)
27941 decl_specs->multiple_types_p = true;
27942 else
27944 decl_specs->type = type_spec;
27945 decl_specs->type_definition_p = type_definition_p;
27946 decl_specs->redefined_builtin_type = NULL_TREE;
27947 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27951 /* True iff TOKEN is the GNU keyword __thread. */
27953 static bool
27954 token_is__thread (cp_token *token)
27956 gcc_assert (token->keyword == RID_THREAD);
27957 return id_equal (token->u.value, "__thread");
27960 /* Set the location for a declarator specifier and check if it is
27961 duplicated.
27963 DECL_SPECS is the sequence of declarator specifiers onto which to
27964 set the location.
27966 DS is the single declarator specifier to set which location is to
27967 be set onto the existing sequence of declarators.
27969 LOCATION is the location for the declarator specifier to
27970 consider. */
27972 static void
27973 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27974 cp_decl_spec ds, cp_token *token)
27976 gcc_assert (ds < ds_last);
27978 if (decl_specs == NULL)
27979 return;
27981 source_location location = token->location;
27983 if (decl_specs->locations[ds] == 0)
27985 decl_specs->locations[ds] = location;
27986 if (ds == ds_thread)
27987 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27989 else
27991 if (ds == ds_long)
27993 if (decl_specs->locations[ds_long_long] != 0)
27994 error_at (location,
27995 "%<long long long%> is too long for GCC");
27996 else
27998 decl_specs->locations[ds_long_long] = location;
27999 pedwarn_cxx98 (location,
28000 OPT_Wlong_long,
28001 "ISO C++ 1998 does not support %<long long%>");
28004 else if (ds == ds_thread)
28006 bool gnu = token_is__thread (token);
28007 if (gnu != decl_specs->gnu_thread_keyword_p)
28008 error_at (location,
28009 "both %<__thread%> and %<thread_local%> specified");
28010 else
28012 gcc_rich_location richloc (location);
28013 richloc.add_fixit_remove ();
28014 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
28017 else
28019 static const char *const decl_spec_names[] = {
28020 "signed",
28021 "unsigned",
28022 "short",
28023 "long",
28024 "const",
28025 "volatile",
28026 "restrict",
28027 "inline",
28028 "virtual",
28029 "explicit",
28030 "friend",
28031 "typedef",
28032 "using",
28033 "constexpr",
28034 "__complex"
28036 gcc_rich_location richloc (location);
28037 richloc.add_fixit_remove ();
28038 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
28043 /* Return true iff the declarator specifier DS is present in the
28044 sequence of declarator specifiers DECL_SPECS. */
28046 bool
28047 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
28048 cp_decl_spec ds)
28050 gcc_assert (ds < ds_last);
28052 if (decl_specs == NULL)
28053 return false;
28055 return decl_specs->locations[ds] != 0;
28058 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
28059 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
28061 static bool
28062 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
28064 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
28067 /* Get a description of the matching symbol to TOKEN_DESC e.g. "(" for
28068 RT_CLOSE_PAREN. */
28070 static const char *
28071 get_matching_symbol (required_token token_desc)
28073 switch (token_desc)
28075 default:
28076 gcc_unreachable ();
28077 return "";
28078 case RT_CLOSE_BRACE:
28079 return "{";
28080 case RT_CLOSE_PAREN:
28081 return "(";
28085 /* Issue an error message indicating that TOKEN_DESC was expected.
28086 If KEYWORD is true, it indicated this function is called by
28087 cp_parser_require_keword and the required token can only be
28088 a indicated keyword.
28090 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28091 within any error as the location of an "opening" token matching
28092 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28093 RT_CLOSE_PAREN). */
28095 static void
28096 cp_parser_required_error (cp_parser *parser,
28097 required_token token_desc,
28098 bool keyword,
28099 location_t matching_location)
28101 if (cp_parser_simulate_error (parser))
28102 return;
28104 const char *gmsgid = NULL;
28105 switch (token_desc)
28107 case RT_NEW:
28108 gmsgid = G_("expected %<new%>");
28109 break;
28110 case RT_DELETE:
28111 gmsgid = G_("expected %<delete%>");
28112 break;
28113 case RT_RETURN:
28114 gmsgid = G_("expected %<return%>");
28115 break;
28116 case RT_WHILE:
28117 gmsgid = G_("expected %<while%>");
28118 break;
28119 case RT_EXTERN:
28120 gmsgid = G_("expected %<extern%>");
28121 break;
28122 case RT_STATIC_ASSERT:
28123 gmsgid = G_("expected %<static_assert%>");
28124 break;
28125 case RT_DECLTYPE:
28126 gmsgid = G_("expected %<decltype%>");
28127 break;
28128 case RT_OPERATOR:
28129 gmsgid = G_("expected %<operator%>");
28130 break;
28131 case RT_CLASS:
28132 gmsgid = G_("expected %<class%>");
28133 break;
28134 case RT_TEMPLATE:
28135 gmsgid = G_("expected %<template%>");
28136 break;
28137 case RT_NAMESPACE:
28138 gmsgid = G_("expected %<namespace%>");
28139 break;
28140 case RT_USING:
28141 gmsgid = G_("expected %<using%>");
28142 break;
28143 case RT_ASM:
28144 gmsgid = G_("expected %<asm%>");
28145 break;
28146 case RT_TRY:
28147 gmsgid = G_("expected %<try%>");
28148 break;
28149 case RT_CATCH:
28150 gmsgid = G_("expected %<catch%>");
28151 break;
28152 case RT_THROW:
28153 gmsgid = G_("expected %<throw%>");
28154 break;
28155 case RT_LABEL:
28156 gmsgid = G_("expected %<__label__%>");
28157 break;
28158 case RT_AT_TRY:
28159 gmsgid = G_("expected %<@try%>");
28160 break;
28161 case RT_AT_SYNCHRONIZED:
28162 gmsgid = G_("expected %<@synchronized%>");
28163 break;
28164 case RT_AT_THROW:
28165 gmsgid = G_("expected %<@throw%>");
28166 break;
28167 case RT_TRANSACTION_ATOMIC:
28168 gmsgid = G_("expected %<__transaction_atomic%>");
28169 break;
28170 case RT_TRANSACTION_RELAXED:
28171 gmsgid = G_("expected %<__transaction_relaxed%>");
28172 break;
28173 default:
28174 break;
28177 if (!gmsgid && !keyword)
28179 switch (token_desc)
28181 case RT_SEMICOLON:
28182 gmsgid = G_("expected %<;%>");
28183 break;
28184 case RT_OPEN_PAREN:
28185 gmsgid = G_("expected %<(%>");
28186 break;
28187 case RT_CLOSE_BRACE:
28188 gmsgid = G_("expected %<}%>");
28189 break;
28190 case RT_OPEN_BRACE:
28191 gmsgid = G_("expected %<{%>");
28192 break;
28193 case RT_CLOSE_SQUARE:
28194 gmsgid = G_("expected %<]%>");
28195 break;
28196 case RT_OPEN_SQUARE:
28197 gmsgid = G_("expected %<[%>");
28198 break;
28199 case RT_COMMA:
28200 gmsgid = G_("expected %<,%>");
28201 break;
28202 case RT_SCOPE:
28203 gmsgid = G_("expected %<::%>");
28204 break;
28205 case RT_LESS:
28206 gmsgid = G_("expected %<<%>");
28207 break;
28208 case RT_GREATER:
28209 gmsgid = G_("expected %<>%>");
28210 break;
28211 case RT_EQ:
28212 gmsgid = G_("expected %<=%>");
28213 break;
28214 case RT_ELLIPSIS:
28215 gmsgid = G_("expected %<...%>");
28216 break;
28217 case RT_MULT:
28218 gmsgid = G_("expected %<*%>");
28219 break;
28220 case RT_COMPL:
28221 gmsgid = G_("expected %<~%>");
28222 break;
28223 case RT_COLON:
28224 gmsgid = G_("expected %<:%>");
28225 break;
28226 case RT_COLON_SCOPE:
28227 gmsgid = G_("expected %<:%> or %<::%>");
28228 break;
28229 case RT_CLOSE_PAREN:
28230 gmsgid = G_("expected %<)%>");
28231 break;
28232 case RT_COMMA_CLOSE_PAREN:
28233 gmsgid = G_("expected %<,%> or %<)%>");
28234 break;
28235 case RT_PRAGMA_EOL:
28236 gmsgid = G_("expected end of line");
28237 break;
28238 case RT_NAME:
28239 gmsgid = G_("expected identifier");
28240 break;
28241 case RT_SELECT:
28242 gmsgid = G_("expected selection-statement");
28243 break;
28244 case RT_ITERATION:
28245 gmsgid = G_("expected iteration-statement");
28246 break;
28247 case RT_JUMP:
28248 gmsgid = G_("expected jump-statement");
28249 break;
28250 case RT_CLASS_KEY:
28251 gmsgid = G_("expected class-key");
28252 break;
28253 case RT_CLASS_TYPENAME_TEMPLATE:
28254 gmsgid = G_("expected %<class%>, %<typename%>, or %<template%>");
28255 break;
28256 default:
28257 gcc_unreachable ();
28261 if (gmsgid)
28263 /* Emulate rest of cp_parser_error. */
28264 cp_token *token = cp_lexer_peek_token (parser->lexer);
28265 cp_lexer_set_source_position_from_token (token);
28267 gcc_rich_location richloc (input_location);
28269 /* If matching_location != UNKNOWN_LOCATION, highlight it.
28270 Attempt to consolidate diagnostics by printing it as a
28271 secondary range within the main diagnostic. */
28272 bool added_matching_location = false;
28273 if (matching_location != UNKNOWN_LOCATION)
28274 added_matching_location
28275 = richloc.add_location_if_nearby (matching_location);
28277 c_parse_error (gmsgid,
28278 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
28279 token->u.value, token->flags, &richloc);
28281 /* If we weren't able to consolidate matching_location, then
28282 print it as a secondary diagnostic. */
28283 if (matching_location != UNKNOWN_LOCATION && !added_matching_location)
28284 inform (matching_location, "to match this %qs",
28285 get_matching_symbol (token_desc));
28290 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28291 issue an error message indicating that TOKEN_DESC was expected.
28293 Returns the token consumed, if the token had the appropriate type.
28294 Otherwise, returns NULL.
28296 If MATCHING_LOCATION is not UNKNOWN_LOCATION, then highlight it
28297 within any error as the location of an "opening" token matching
28298 the close token TYPE (e.g. the location of the '(' when TOKEN_DESC is
28299 RT_CLOSE_PAREN). */
28301 static cp_token *
28302 cp_parser_require (cp_parser* parser,
28303 enum cpp_ttype type,
28304 required_token token_desc,
28305 location_t matching_location)
28307 if (cp_lexer_next_token_is (parser->lexer, type))
28308 return cp_lexer_consume_token (parser->lexer);
28309 else
28311 /* Output the MESSAGE -- unless we're parsing tentatively. */
28312 if (!cp_parser_simulate_error (parser))
28313 cp_parser_required_error (parser, token_desc, /*keyword=*/false,
28314 matching_location);
28315 return NULL;
28319 /* An error message is produced if the next token is not '>'.
28320 All further tokens are skipped until the desired token is
28321 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28323 static void
28324 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28326 /* Current level of '< ... >'. */
28327 unsigned level = 0;
28328 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28329 unsigned nesting_depth = 0;
28331 /* Are we ready, yet? If not, issue error message. */
28332 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28333 return;
28335 /* Skip tokens until the desired token is found. */
28336 while (true)
28338 /* Peek at the next token. */
28339 switch (cp_lexer_peek_token (parser->lexer)->type)
28341 case CPP_LESS:
28342 if (!nesting_depth)
28343 ++level;
28344 break;
28346 case CPP_RSHIFT:
28347 if (cxx_dialect == cxx98)
28348 /* C++0x views the `>>' operator as two `>' tokens, but
28349 C++98 does not. */
28350 break;
28351 else if (!nesting_depth && level-- == 0)
28353 /* We've hit a `>>' where the first `>' closes the
28354 template argument list, and the second `>' is
28355 spurious. Just consume the `>>' and stop; we've
28356 already produced at least one error. */
28357 cp_lexer_consume_token (parser->lexer);
28358 return;
28360 /* Fall through for C++0x, so we handle the second `>' in
28361 the `>>'. */
28362 gcc_fallthrough ();
28364 case CPP_GREATER:
28365 if (!nesting_depth && level-- == 0)
28367 /* We've reached the token we want, consume it and stop. */
28368 cp_lexer_consume_token (parser->lexer);
28369 return;
28371 break;
28373 case CPP_OPEN_PAREN:
28374 case CPP_OPEN_SQUARE:
28375 ++nesting_depth;
28376 break;
28378 case CPP_CLOSE_PAREN:
28379 case CPP_CLOSE_SQUARE:
28380 if (nesting_depth-- == 0)
28381 return;
28382 break;
28384 case CPP_EOF:
28385 case CPP_PRAGMA_EOL:
28386 case CPP_SEMICOLON:
28387 case CPP_OPEN_BRACE:
28388 case CPP_CLOSE_BRACE:
28389 /* The '>' was probably forgotten, don't look further. */
28390 return;
28392 default:
28393 break;
28396 /* Consume this token. */
28397 cp_lexer_consume_token (parser->lexer);
28401 /* If the next token is the indicated keyword, consume it. Otherwise,
28402 issue an error message indicating that TOKEN_DESC was expected.
28404 Returns the token consumed, if the token had the appropriate type.
28405 Otherwise, returns NULL. */
28407 static cp_token *
28408 cp_parser_require_keyword (cp_parser* parser,
28409 enum rid keyword,
28410 required_token token_desc)
28412 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28414 if (token && token->keyword != keyword)
28416 cp_parser_required_error (parser, token_desc, /*keyword=*/true,
28417 UNKNOWN_LOCATION);
28418 return NULL;
28421 return token;
28424 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28425 function-definition. */
28427 static bool
28428 cp_parser_token_starts_function_definition_p (cp_token* token)
28430 return (/* An ordinary function-body begins with an `{'. */
28431 token->type == CPP_OPEN_BRACE
28432 /* A ctor-initializer begins with a `:'. */
28433 || token->type == CPP_COLON
28434 /* A function-try-block begins with `try'. */
28435 || token->keyword == RID_TRY
28436 /* A function-transaction-block begins with `__transaction_atomic'
28437 or `__transaction_relaxed'. */
28438 || token->keyword == RID_TRANSACTION_ATOMIC
28439 || token->keyword == RID_TRANSACTION_RELAXED
28440 /* The named return value extension begins with `return'. */
28441 || token->keyword == RID_RETURN);
28444 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28445 definition. */
28447 static bool
28448 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28450 cp_token *token;
28452 token = cp_lexer_peek_token (parser->lexer);
28453 return (token->type == CPP_OPEN_BRACE
28454 || (token->type == CPP_COLON
28455 && !parser->colon_doesnt_start_class_def_p));
28458 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28459 C++0x) ending a template-argument. */
28461 static bool
28462 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28464 cp_token *token;
28466 token = cp_lexer_peek_token (parser->lexer);
28467 return (token->type == CPP_COMMA
28468 || token->type == CPP_GREATER
28469 || token->type == CPP_ELLIPSIS
28470 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28473 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28474 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28476 static bool
28477 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28478 size_t n)
28480 cp_token *token;
28482 token = cp_lexer_peek_nth_token (parser->lexer, n);
28483 if (token->type == CPP_LESS)
28484 return true;
28485 /* Check for the sequence `<::' in the original code. It would be lexed as
28486 `[:', where `[' is a digraph, and there is no whitespace before
28487 `:'. */
28488 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28490 cp_token *token2;
28491 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28492 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28493 return true;
28495 return false;
28498 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28499 or none_type otherwise. */
28501 static enum tag_types
28502 cp_parser_token_is_class_key (cp_token* token)
28504 switch (token->keyword)
28506 case RID_CLASS:
28507 return class_type;
28508 case RID_STRUCT:
28509 return record_type;
28510 case RID_UNION:
28511 return union_type;
28513 default:
28514 return none_type;
28518 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28519 or none_type otherwise or if the token is null. */
28521 static enum tag_types
28522 cp_parser_token_is_type_parameter_key (cp_token* token)
28524 if (!token)
28525 return none_type;
28527 switch (token->keyword)
28529 case RID_CLASS:
28530 return class_type;
28531 case RID_TYPENAME:
28532 return typename_type;
28534 default:
28535 return none_type;
28539 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28541 static void
28542 cp_parser_check_class_key (enum tag_types class_key, tree type)
28544 if (type == error_mark_node)
28545 return;
28546 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28548 if (permerror (input_location, "%qs tag used in naming %q#T",
28549 class_key == union_type ? "union"
28550 : class_key == record_type ? "struct" : "class",
28551 type))
28552 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28553 "%q#T was previously declared here", type);
28557 /* Issue an error message if DECL is redeclared with different
28558 access than its original declaration [class.access.spec/3].
28559 This applies to nested classes, nested class templates and
28560 enumerations [class.mem/1]. */
28562 static void
28563 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28565 if (!decl
28566 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28567 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28568 return;
28570 if ((TREE_PRIVATE (decl)
28571 != (current_access_specifier == access_private_node))
28572 || (TREE_PROTECTED (decl)
28573 != (current_access_specifier == access_protected_node)))
28574 error_at (location, "%qD redeclared with different access", decl);
28577 /* Look for the `template' keyword, as a syntactic disambiguator.
28578 Return TRUE iff it is present, in which case it will be
28579 consumed. */
28581 static bool
28582 cp_parser_optional_template_keyword (cp_parser *parser)
28584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28586 /* In C++98 the `template' keyword can only be used within templates;
28587 outside templates the parser can always figure out what is a
28588 template and what is not. In C++11, per the resolution of DR 468,
28589 `template' is allowed in cases where it is not strictly necessary. */
28590 if (!processing_template_decl
28591 && pedantic && cxx_dialect == cxx98)
28593 cp_token *token = cp_lexer_peek_token (parser->lexer);
28594 pedwarn (token->location, OPT_Wpedantic,
28595 "in C++98 %<template%> (as a disambiguator) is only "
28596 "allowed within templates");
28597 /* If this part of the token stream is rescanned, the same
28598 error message would be generated. So, we purge the token
28599 from the stream. */
28600 cp_lexer_purge_token (parser->lexer);
28601 return false;
28603 else
28605 /* Consume the `template' keyword. */
28606 cp_lexer_consume_token (parser->lexer);
28607 return true;
28610 return false;
28613 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28614 set PARSER->SCOPE, and perform other related actions. */
28616 static void
28617 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28619 struct tree_check *check_value;
28621 /* Get the stored value. */
28622 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28623 /* Set the scope from the stored value. */
28624 parser->scope = saved_checks_value (check_value);
28625 parser->qualifying_scope = check_value->qualifying_scope;
28626 parser->object_scope = NULL_TREE;
28629 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28630 encounter the end of a block before what we were looking for. */
28632 static bool
28633 cp_parser_cache_group (cp_parser *parser,
28634 enum cpp_ttype end,
28635 unsigned depth)
28637 while (true)
28639 cp_token *token = cp_lexer_peek_token (parser->lexer);
28641 /* Abort a parenthesized expression if we encounter a semicolon. */
28642 if ((end == CPP_CLOSE_PAREN || depth == 0)
28643 && token->type == CPP_SEMICOLON)
28644 return true;
28645 /* If we've reached the end of the file, stop. */
28646 if (token->type == CPP_EOF
28647 || (end != CPP_PRAGMA_EOL
28648 && token->type == CPP_PRAGMA_EOL))
28649 return true;
28650 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28651 /* We've hit the end of an enclosing block, so there's been some
28652 kind of syntax error. */
28653 return true;
28655 /* Consume the token. */
28656 cp_lexer_consume_token (parser->lexer);
28657 /* See if it starts a new group. */
28658 if (token->type == CPP_OPEN_BRACE)
28660 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28661 /* In theory this should probably check end == '}', but
28662 cp_parser_save_member_function_body needs it to exit
28663 after either '}' or ')' when called with ')'. */
28664 if (depth == 0)
28665 return false;
28667 else if (token->type == CPP_OPEN_PAREN)
28669 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28670 if (depth == 0 && end == CPP_CLOSE_PAREN)
28671 return false;
28673 else if (token->type == CPP_PRAGMA)
28674 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28675 else if (token->type == end)
28676 return false;
28680 /* Like above, for caching a default argument or NSDMI. Both of these are
28681 terminated by a non-nested comma, but it can be unclear whether or not a
28682 comma is nested in a template argument list unless we do more parsing.
28683 In order to handle this ambiguity, when we encounter a ',' after a '<'
28684 we try to parse what follows as a parameter-declaration-list (in the
28685 case of a default argument) or a member-declarator (in the case of an
28686 NSDMI). If that succeeds, then we stop caching. */
28688 static tree
28689 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28691 unsigned depth = 0;
28692 int maybe_template_id = 0;
28693 cp_token *first_token;
28694 cp_token *token;
28695 tree default_argument;
28697 /* Add tokens until we have processed the entire default
28698 argument. We add the range [first_token, token). */
28699 first_token = cp_lexer_peek_token (parser->lexer);
28700 if (first_token->type == CPP_OPEN_BRACE)
28702 /* For list-initialization, this is straightforward. */
28703 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28704 token = cp_lexer_peek_token (parser->lexer);
28706 else while (true)
28708 bool done = false;
28710 /* Peek at the next token. */
28711 token = cp_lexer_peek_token (parser->lexer);
28712 /* What we do depends on what token we have. */
28713 switch (token->type)
28715 /* In valid code, a default argument must be
28716 immediately followed by a `,' `)', or `...'. */
28717 case CPP_COMMA:
28718 if (depth == 0 && maybe_template_id)
28720 /* If we've seen a '<', we might be in a
28721 template-argument-list. Until Core issue 325 is
28722 resolved, we don't know how this situation ought
28723 to be handled, so try to DTRT. We check whether
28724 what comes after the comma is a valid parameter
28725 declaration list. If it is, then the comma ends
28726 the default argument; otherwise the default
28727 argument continues. */
28728 bool error = false;
28729 cp_token *peek;
28731 /* Set ITALP so cp_parser_parameter_declaration_list
28732 doesn't decide to commit to this parse. */
28733 bool saved_italp = parser->in_template_argument_list_p;
28734 parser->in_template_argument_list_p = true;
28736 cp_parser_parse_tentatively (parser);
28738 if (nsdmi)
28740 /* Parse declarators until we reach a non-comma or
28741 somthing that cannot be an initializer.
28742 Just checking whether we're looking at a single
28743 declarator is insufficient. Consider:
28744 int var = tuple<T,U>::x;
28745 The template parameter 'U' looks exactly like a
28746 declarator. */
28749 int ctor_dtor_or_conv_p;
28750 cp_lexer_consume_token (parser->lexer);
28751 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28752 &ctor_dtor_or_conv_p,
28753 /*parenthesized_p=*/NULL,
28754 /*member_p=*/true,
28755 /*friend_p=*/false);
28756 peek = cp_lexer_peek_token (parser->lexer);
28757 if (cp_parser_error_occurred (parser))
28758 break;
28760 while (peek->type == CPP_COMMA);
28761 /* If we met an '=' or ';' then the original comma
28762 was the end of the NSDMI. Otherwise assume
28763 we're still in the NSDMI. */
28764 error = (peek->type != CPP_EQ
28765 && peek->type != CPP_SEMICOLON);
28767 else
28769 cp_lexer_consume_token (parser->lexer);
28770 begin_scope (sk_function_parms, NULL_TREE);
28771 cp_parser_parameter_declaration_list (parser, &error);
28772 pop_bindings_and_leave_scope ();
28774 if (!cp_parser_error_occurred (parser) && !error)
28775 done = true;
28776 cp_parser_abort_tentative_parse (parser);
28778 parser->in_template_argument_list_p = saved_italp;
28779 break;
28781 /* FALLTHRU */
28782 case CPP_CLOSE_PAREN:
28783 case CPP_ELLIPSIS:
28784 /* If we run into a non-nested `;', `}', or `]',
28785 then the code is invalid -- but the default
28786 argument is certainly over. */
28787 case CPP_SEMICOLON:
28788 case CPP_CLOSE_BRACE:
28789 case CPP_CLOSE_SQUARE:
28790 if (depth == 0
28791 /* Handle correctly int n = sizeof ... ( p ); */
28792 && token->type != CPP_ELLIPSIS)
28793 done = true;
28794 /* Update DEPTH, if necessary. */
28795 else if (token->type == CPP_CLOSE_PAREN
28796 || token->type == CPP_CLOSE_BRACE
28797 || token->type == CPP_CLOSE_SQUARE)
28798 --depth;
28799 break;
28801 case CPP_OPEN_PAREN:
28802 case CPP_OPEN_SQUARE:
28803 case CPP_OPEN_BRACE:
28804 ++depth;
28805 break;
28807 case CPP_LESS:
28808 if (depth == 0)
28809 /* This might be the comparison operator, or it might
28810 start a template argument list. */
28811 ++maybe_template_id;
28812 break;
28814 case CPP_RSHIFT:
28815 if (cxx_dialect == cxx98)
28816 break;
28817 /* Fall through for C++0x, which treats the `>>'
28818 operator like two `>' tokens in certain
28819 cases. */
28820 gcc_fallthrough ();
28822 case CPP_GREATER:
28823 if (depth == 0)
28825 /* This might be an operator, or it might close a
28826 template argument list. But if a previous '<'
28827 started a template argument list, this will have
28828 closed it, so we can't be in one anymore. */
28829 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28830 if (maybe_template_id < 0)
28831 maybe_template_id = 0;
28833 break;
28835 /* If we run out of tokens, issue an error message. */
28836 case CPP_EOF:
28837 case CPP_PRAGMA_EOL:
28838 error_at (token->location, "file ends in default argument");
28839 return error_mark_node;
28841 case CPP_NAME:
28842 case CPP_SCOPE:
28843 /* In these cases, we should look for template-ids.
28844 For example, if the default argument is
28845 `X<int, double>()', we need to do name lookup to
28846 figure out whether or not `X' is a template; if
28847 so, the `,' does not end the default argument.
28849 That is not yet done. */
28850 break;
28852 default:
28853 break;
28856 /* If we've reached the end, stop. */
28857 if (done)
28858 break;
28860 /* Add the token to the token block. */
28861 token = cp_lexer_consume_token (parser->lexer);
28864 /* Create a DEFAULT_ARG to represent the unparsed default
28865 argument. */
28866 default_argument = make_node (DEFAULT_ARG);
28867 DEFARG_TOKENS (default_argument)
28868 = cp_token_cache_new (first_token, token);
28869 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28871 return default_argument;
28874 /* A location to use for diagnostics about an unparsed DEFAULT_ARG. */
28876 location_t
28877 defarg_location (tree default_argument)
28879 cp_token_cache *tokens = DEFARG_TOKENS (default_argument);
28880 location_t start = tokens->first->location;
28881 location_t end = tokens->last->location;
28882 return make_location (start, start, end);
28885 /* Begin parsing tentatively. We always save tokens while parsing
28886 tentatively so that if the tentative parsing fails we can restore the
28887 tokens. */
28889 static void
28890 cp_parser_parse_tentatively (cp_parser* parser)
28892 /* Enter a new parsing context. */
28893 parser->context = cp_parser_context_new (parser->context);
28894 /* Begin saving tokens. */
28895 cp_lexer_save_tokens (parser->lexer);
28896 /* In order to avoid repetitive access control error messages,
28897 access checks are queued up until we are no longer parsing
28898 tentatively. */
28899 push_deferring_access_checks (dk_deferred);
28902 /* Commit to the currently active tentative parse. */
28904 static void
28905 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28907 cp_parser_context *context;
28908 cp_lexer *lexer;
28910 /* Mark all of the levels as committed. */
28911 lexer = parser->lexer;
28912 for (context = parser->context; context->next; context = context->next)
28914 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28915 break;
28916 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28917 while (!cp_lexer_saving_tokens (lexer))
28918 lexer = lexer->next;
28919 cp_lexer_commit_tokens (lexer);
28923 /* Commit to the topmost currently active tentative parse.
28925 Note that this function shouldn't be called when there are
28926 irreversible side-effects while in a tentative state. For
28927 example, we shouldn't create a permanent entry in the symbol
28928 table, or issue an error message that might not apply if the
28929 tentative parse is aborted. */
28931 static void
28932 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28934 cp_parser_context *context = parser->context;
28935 cp_lexer *lexer = parser->lexer;
28937 if (context)
28939 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28940 return;
28941 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28943 while (!cp_lexer_saving_tokens (lexer))
28944 lexer = lexer->next;
28945 cp_lexer_commit_tokens (lexer);
28949 /* Abort the currently active tentative parse. All consumed tokens
28950 will be rolled back, and no diagnostics will be issued. */
28952 static void
28953 cp_parser_abort_tentative_parse (cp_parser* parser)
28955 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28956 || errorcount > 0);
28957 cp_parser_simulate_error (parser);
28958 /* Now, pretend that we want to see if the construct was
28959 successfully parsed. */
28960 cp_parser_parse_definitely (parser);
28963 /* Stop parsing tentatively. If a parse error has occurred, restore the
28964 token stream. Otherwise, commit to the tokens we have consumed.
28965 Returns true if no error occurred; false otherwise. */
28967 static bool
28968 cp_parser_parse_definitely (cp_parser* parser)
28970 bool error_occurred;
28971 cp_parser_context *context;
28973 /* Remember whether or not an error occurred, since we are about to
28974 destroy that information. */
28975 error_occurred = cp_parser_error_occurred (parser);
28976 /* Remove the topmost context from the stack. */
28977 context = parser->context;
28978 parser->context = context->next;
28979 /* If no parse errors occurred, commit to the tentative parse. */
28980 if (!error_occurred)
28982 /* Commit to the tokens read tentatively, unless that was
28983 already done. */
28984 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28985 cp_lexer_commit_tokens (parser->lexer);
28987 pop_to_parent_deferring_access_checks ();
28989 /* Otherwise, if errors occurred, roll back our state so that things
28990 are just as they were before we began the tentative parse. */
28991 else
28993 cp_lexer_rollback_tokens (parser->lexer);
28994 pop_deferring_access_checks ();
28996 /* Add the context to the front of the free list. */
28997 context->next = cp_parser_context_free_list;
28998 cp_parser_context_free_list = context;
29000 return !error_occurred;
29003 /* Returns true if we are parsing tentatively and are not committed to
29004 this tentative parse. */
29006 static bool
29007 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
29009 return (cp_parser_parsing_tentatively (parser)
29010 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
29013 /* Returns nonzero iff an error has occurred during the most recent
29014 tentative parse. */
29016 static bool
29017 cp_parser_error_occurred (cp_parser* parser)
29019 return (cp_parser_parsing_tentatively (parser)
29020 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
29023 /* Returns nonzero if GNU extensions are allowed. */
29025 static bool
29026 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
29028 return parser->allow_gnu_extensions_p;
29031 /* Objective-C++ Productions */
29034 /* Parse an Objective-C expression, which feeds into a primary-expression
29035 above.
29037 objc-expression:
29038 objc-message-expression
29039 objc-string-literal
29040 objc-encode-expression
29041 objc-protocol-expression
29042 objc-selector-expression
29044 Returns a tree representation of the expression. */
29046 static cp_expr
29047 cp_parser_objc_expression (cp_parser* parser)
29049 /* Try to figure out what kind of declaration is present. */
29050 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29052 switch (kwd->type)
29054 case CPP_OPEN_SQUARE:
29055 return cp_parser_objc_message_expression (parser);
29057 case CPP_OBJC_STRING:
29058 kwd = cp_lexer_consume_token (parser->lexer);
29059 return objc_build_string_object (kwd->u.value);
29061 case CPP_KEYWORD:
29062 switch (kwd->keyword)
29064 case RID_AT_ENCODE:
29065 return cp_parser_objc_encode_expression (parser);
29067 case RID_AT_PROTOCOL:
29068 return cp_parser_objc_protocol_expression (parser);
29070 case RID_AT_SELECTOR:
29071 return cp_parser_objc_selector_expression (parser);
29073 default:
29074 break;
29076 default:
29077 error_at (kwd->location,
29078 "misplaced %<@%D%> Objective-C++ construct",
29079 kwd->u.value);
29080 cp_parser_skip_to_end_of_block_or_statement (parser);
29083 return error_mark_node;
29086 /* Parse an Objective-C message expression.
29088 objc-message-expression:
29089 [ objc-message-receiver objc-message-args ]
29091 Returns a representation of an Objective-C message. */
29093 static tree
29094 cp_parser_objc_message_expression (cp_parser* parser)
29096 tree receiver, messageargs;
29098 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29099 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
29100 receiver = cp_parser_objc_message_receiver (parser);
29101 messageargs = cp_parser_objc_message_args (parser);
29102 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
29103 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
29105 tree result = objc_build_message_expr (receiver, messageargs);
29107 /* Construct a location e.g.
29108 [self func1:5]
29109 ^~~~~~~~~~~~~~
29110 ranging from the '[' to the ']', with the caret at the start. */
29111 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
29112 protected_set_expr_location (result, combined_loc);
29114 return result;
29117 /* Parse an objc-message-receiver.
29119 objc-message-receiver:
29120 expression
29121 simple-type-specifier
29123 Returns a representation of the type or expression. */
29125 static tree
29126 cp_parser_objc_message_receiver (cp_parser* parser)
29128 tree rcv;
29130 /* An Objective-C message receiver may be either (1) a type
29131 or (2) an expression. */
29132 cp_parser_parse_tentatively (parser);
29133 rcv = cp_parser_expression (parser);
29135 /* If that worked out, fine. */
29136 if (cp_parser_parse_definitely (parser))
29137 return rcv;
29139 cp_parser_parse_tentatively (parser);
29140 rcv = cp_parser_simple_type_specifier (parser,
29141 /*decl_specs=*/NULL,
29142 CP_PARSER_FLAGS_NONE);
29144 if (cp_parser_parse_definitely (parser))
29145 return objc_get_class_reference (rcv);
29147 cp_parser_error (parser, "objective-c++ message receiver expected");
29148 return error_mark_node;
29151 /* Parse the arguments and selectors comprising an Objective-C message.
29153 objc-message-args:
29154 objc-selector
29155 objc-selector-args
29156 objc-selector-args , objc-comma-args
29158 objc-selector-args:
29159 objc-selector [opt] : assignment-expression
29160 objc-selector-args objc-selector [opt] : assignment-expression
29162 objc-comma-args:
29163 assignment-expression
29164 objc-comma-args , assignment-expression
29166 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
29167 selector arguments and TREE_VALUE containing a list of comma
29168 arguments. */
29170 static tree
29171 cp_parser_objc_message_args (cp_parser* parser)
29173 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
29174 bool maybe_unary_selector_p = true;
29175 cp_token *token = cp_lexer_peek_token (parser->lexer);
29177 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29179 tree selector = NULL_TREE, arg;
29181 if (token->type != CPP_COLON)
29182 selector = cp_parser_objc_selector (parser);
29184 /* Detect if we have a unary selector. */
29185 if (maybe_unary_selector_p
29186 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29187 return build_tree_list (selector, NULL_TREE);
29189 maybe_unary_selector_p = false;
29190 cp_parser_require (parser, CPP_COLON, RT_COLON);
29191 arg = cp_parser_assignment_expression (parser);
29193 sel_args
29194 = chainon (sel_args,
29195 build_tree_list (selector, arg));
29197 token = cp_lexer_peek_token (parser->lexer);
29200 /* Handle non-selector arguments, if any. */
29201 while (token->type == CPP_COMMA)
29203 tree arg;
29205 cp_lexer_consume_token (parser->lexer);
29206 arg = cp_parser_assignment_expression (parser);
29208 addl_args
29209 = chainon (addl_args,
29210 build_tree_list (NULL_TREE, arg));
29212 token = cp_lexer_peek_token (parser->lexer);
29215 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29217 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29218 return build_tree_list (error_mark_node, error_mark_node);
29221 return build_tree_list (sel_args, addl_args);
29224 /* Parse an Objective-C encode expression.
29226 objc-encode-expression:
29227 @encode objc-typename
29229 Returns an encoded representation of the type argument. */
29231 static cp_expr
29232 cp_parser_objc_encode_expression (cp_parser* parser)
29234 tree type;
29235 cp_token *token;
29236 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29238 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29239 matching_parens parens;
29240 parens.require_open (parser);
29241 token = cp_lexer_peek_token (parser->lexer);
29242 type = complete_type (cp_parser_type_id (parser));
29243 parens.require_close (parser);
29245 if (!type)
29247 error_at (token->location,
29248 "%<@encode%> must specify a type as an argument");
29249 return error_mark_node;
29252 /* This happens if we find @encode(T) (where T is a template
29253 typename or something dependent on a template typename) when
29254 parsing a template. In that case, we can't compile it
29255 immediately, but we rather create an AT_ENCODE_EXPR which will
29256 need to be instantiated when the template is used.
29258 if (dependent_type_p (type))
29260 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29261 TREE_READONLY (value) = 1;
29262 return value;
29266 /* Build a location of the form:
29267 @encode(int)
29268 ^~~~~~~~~~~~
29269 with caret==start at the @ token, finishing at the close paren. */
29270 location_t combined_loc
29271 = make_location (start_loc, start_loc,
29272 cp_lexer_previous_token (parser->lexer)->location);
29274 return cp_expr (objc_build_encode_expr (type), combined_loc);
29277 /* Parse an Objective-C @defs expression. */
29279 static tree
29280 cp_parser_objc_defs_expression (cp_parser *parser)
29282 tree name;
29284 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29285 matching_parens parens;
29286 parens.require_open (parser);
29287 name = cp_parser_identifier (parser);
29288 parens.require_close (parser);
29290 return objc_get_class_ivars (name);
29293 /* Parse an Objective-C protocol expression.
29295 objc-protocol-expression:
29296 @protocol ( identifier )
29298 Returns a representation of the protocol expression. */
29300 static tree
29301 cp_parser_objc_protocol_expression (cp_parser* parser)
29303 tree proto;
29304 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29306 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29307 matching_parens parens;
29308 parens.require_open (parser);
29309 proto = cp_parser_identifier (parser);
29310 parens.require_close (parser);
29312 /* Build a location of the form:
29313 @protocol(prot)
29314 ^~~~~~~~~~~~~~~
29315 with caret==start at the @ token, finishing at the close paren. */
29316 location_t combined_loc
29317 = make_location (start_loc, start_loc,
29318 cp_lexer_previous_token (parser->lexer)->location);
29319 tree result = objc_build_protocol_expr (proto);
29320 protected_set_expr_location (result, combined_loc);
29321 return result;
29324 /* Parse an Objective-C selector expression.
29326 objc-selector-expression:
29327 @selector ( objc-method-signature )
29329 objc-method-signature:
29330 objc-selector
29331 objc-selector-seq
29333 objc-selector-seq:
29334 objc-selector :
29335 objc-selector-seq objc-selector :
29337 Returns a representation of the method selector. */
29339 static tree
29340 cp_parser_objc_selector_expression (cp_parser* parser)
29342 tree sel_seq = NULL_TREE;
29343 bool maybe_unary_selector_p = true;
29344 cp_token *token;
29345 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29347 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29348 matching_parens parens;
29349 parens.require_open (parser);
29350 token = cp_lexer_peek_token (parser->lexer);
29352 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29353 || token->type == CPP_SCOPE)
29355 tree selector = NULL_TREE;
29357 if (token->type != CPP_COLON
29358 || token->type == CPP_SCOPE)
29359 selector = cp_parser_objc_selector (parser);
29361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29362 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29364 /* Detect if we have a unary selector. */
29365 if (maybe_unary_selector_p)
29367 sel_seq = selector;
29368 goto finish_selector;
29370 else
29372 cp_parser_error (parser, "expected %<:%>");
29375 maybe_unary_selector_p = false;
29376 token = cp_lexer_consume_token (parser->lexer);
29378 if (token->type == CPP_SCOPE)
29380 sel_seq
29381 = chainon (sel_seq,
29382 build_tree_list (selector, NULL_TREE));
29383 sel_seq
29384 = chainon (sel_seq,
29385 build_tree_list (NULL_TREE, NULL_TREE));
29387 else
29388 sel_seq
29389 = chainon (sel_seq,
29390 build_tree_list (selector, NULL_TREE));
29392 token = cp_lexer_peek_token (parser->lexer);
29395 finish_selector:
29396 parens.require_close (parser);
29399 /* Build a location of the form:
29400 @selector(func)
29401 ^~~~~~~~~~~~~~~
29402 with caret==start at the @ token, finishing at the close paren. */
29403 location_t combined_loc
29404 = make_location (loc, loc,
29405 cp_lexer_previous_token (parser->lexer)->location);
29406 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29407 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29408 protected_set_expr_location (result, combined_loc);
29409 return result;
29412 /* Parse a list of identifiers.
29414 objc-identifier-list:
29415 identifier
29416 objc-identifier-list , identifier
29418 Returns a TREE_LIST of identifier nodes. */
29420 static tree
29421 cp_parser_objc_identifier_list (cp_parser* parser)
29423 tree identifier;
29424 tree list;
29425 cp_token *sep;
29427 identifier = cp_parser_identifier (parser);
29428 if (identifier == error_mark_node)
29429 return error_mark_node;
29431 list = build_tree_list (NULL_TREE, identifier);
29432 sep = cp_lexer_peek_token (parser->lexer);
29434 while (sep->type == CPP_COMMA)
29436 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29437 identifier = cp_parser_identifier (parser);
29438 if (identifier == error_mark_node)
29439 return list;
29441 list = chainon (list, build_tree_list (NULL_TREE,
29442 identifier));
29443 sep = cp_lexer_peek_token (parser->lexer);
29446 return list;
29449 /* Parse an Objective-C alias declaration.
29451 objc-alias-declaration:
29452 @compatibility_alias identifier identifier ;
29454 This function registers the alias mapping with the Objective-C front end.
29455 It returns nothing. */
29457 static void
29458 cp_parser_objc_alias_declaration (cp_parser* parser)
29460 tree alias, orig;
29462 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29463 alias = cp_parser_identifier (parser);
29464 orig = cp_parser_identifier (parser);
29465 objc_declare_alias (alias, orig);
29466 cp_parser_consume_semicolon_at_end_of_statement (parser);
29469 /* Parse an Objective-C class forward-declaration.
29471 objc-class-declaration:
29472 @class objc-identifier-list ;
29474 The function registers the forward declarations with the Objective-C
29475 front end. It returns nothing. */
29477 static void
29478 cp_parser_objc_class_declaration (cp_parser* parser)
29480 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29481 while (true)
29483 tree id;
29485 id = cp_parser_identifier (parser);
29486 if (id == error_mark_node)
29487 break;
29489 objc_declare_class (id);
29491 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29492 cp_lexer_consume_token (parser->lexer);
29493 else
29494 break;
29496 cp_parser_consume_semicolon_at_end_of_statement (parser);
29499 /* Parse a list of Objective-C protocol references.
29501 objc-protocol-refs-opt:
29502 objc-protocol-refs [opt]
29504 objc-protocol-refs:
29505 < objc-identifier-list >
29507 Returns a TREE_LIST of identifiers, if any. */
29509 static tree
29510 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29512 tree protorefs = NULL_TREE;
29514 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29516 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29517 protorefs = cp_parser_objc_identifier_list (parser);
29518 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29521 return protorefs;
29524 /* Parse a Objective-C visibility specification. */
29526 static void
29527 cp_parser_objc_visibility_spec (cp_parser* parser)
29529 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29531 switch (vis->keyword)
29533 case RID_AT_PRIVATE:
29534 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29535 break;
29536 case RID_AT_PROTECTED:
29537 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29538 break;
29539 case RID_AT_PUBLIC:
29540 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29541 break;
29542 case RID_AT_PACKAGE:
29543 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29544 break;
29545 default:
29546 return;
29549 /* Eat '@private'/'@protected'/'@public'. */
29550 cp_lexer_consume_token (parser->lexer);
29553 /* Parse an Objective-C method type. Return 'true' if it is a class
29554 (+) method, and 'false' if it is an instance (-) method. */
29556 static inline bool
29557 cp_parser_objc_method_type (cp_parser* parser)
29559 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29560 return true;
29561 else
29562 return false;
29565 /* Parse an Objective-C protocol qualifier. */
29567 static tree
29568 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29570 tree quals = NULL_TREE, node;
29571 cp_token *token = cp_lexer_peek_token (parser->lexer);
29573 node = token->u.value;
29575 while (node && identifier_p (node)
29576 && (node == ridpointers [(int) RID_IN]
29577 || node == ridpointers [(int) RID_OUT]
29578 || node == ridpointers [(int) RID_INOUT]
29579 || node == ridpointers [(int) RID_BYCOPY]
29580 || node == ridpointers [(int) RID_BYREF]
29581 || node == ridpointers [(int) RID_ONEWAY]))
29583 quals = tree_cons (NULL_TREE, node, quals);
29584 cp_lexer_consume_token (parser->lexer);
29585 token = cp_lexer_peek_token (parser->lexer);
29586 node = token->u.value;
29589 return quals;
29592 /* Parse an Objective-C typename. */
29594 static tree
29595 cp_parser_objc_typename (cp_parser* parser)
29597 tree type_name = NULL_TREE;
29599 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29601 tree proto_quals, cp_type = NULL_TREE;
29603 matching_parens parens;
29604 parens.consume_open (parser); /* Eat '('. */
29605 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29607 /* An ObjC type name may consist of just protocol qualifiers, in which
29608 case the type shall default to 'id'. */
29609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29611 cp_type = cp_parser_type_id (parser);
29613 /* If the type could not be parsed, an error has already
29614 been produced. For error recovery, behave as if it had
29615 not been specified, which will use the default type
29616 'id'. */
29617 if (cp_type == error_mark_node)
29619 cp_type = NULL_TREE;
29620 /* We need to skip to the closing parenthesis as
29621 cp_parser_type_id() does not seem to do it for
29622 us. */
29623 cp_parser_skip_to_closing_parenthesis (parser,
29624 /*recovering=*/true,
29625 /*or_comma=*/false,
29626 /*consume_paren=*/false);
29630 parens.require_close (parser);
29631 type_name = build_tree_list (proto_quals, cp_type);
29634 return type_name;
29637 /* Check to see if TYPE refers to an Objective-C selector name. */
29639 static bool
29640 cp_parser_objc_selector_p (enum cpp_ttype type)
29642 return (type == CPP_NAME || type == CPP_KEYWORD
29643 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29644 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29645 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29646 || type == CPP_XOR || type == CPP_XOR_EQ);
29649 /* Parse an Objective-C selector. */
29651 static tree
29652 cp_parser_objc_selector (cp_parser* parser)
29654 cp_token *token = cp_lexer_consume_token (parser->lexer);
29656 if (!cp_parser_objc_selector_p (token->type))
29658 error_at (token->location, "invalid Objective-C++ selector name");
29659 return error_mark_node;
29662 /* C++ operator names are allowed to appear in ObjC selectors. */
29663 switch (token->type)
29665 case CPP_AND_AND: return get_identifier ("and");
29666 case CPP_AND_EQ: return get_identifier ("and_eq");
29667 case CPP_AND: return get_identifier ("bitand");
29668 case CPP_OR: return get_identifier ("bitor");
29669 case CPP_COMPL: return get_identifier ("compl");
29670 case CPP_NOT: return get_identifier ("not");
29671 case CPP_NOT_EQ: return get_identifier ("not_eq");
29672 case CPP_OR_OR: return get_identifier ("or");
29673 case CPP_OR_EQ: return get_identifier ("or_eq");
29674 case CPP_XOR: return get_identifier ("xor");
29675 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29676 default: return token->u.value;
29680 /* Parse an Objective-C params list. */
29682 static tree
29683 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29685 tree params = NULL_TREE;
29686 bool maybe_unary_selector_p = true;
29687 cp_token *token = cp_lexer_peek_token (parser->lexer);
29689 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29691 tree selector = NULL_TREE, type_name, identifier;
29692 tree parm_attr = NULL_TREE;
29694 if (token->keyword == RID_ATTRIBUTE)
29695 break;
29697 if (token->type != CPP_COLON)
29698 selector = cp_parser_objc_selector (parser);
29700 /* Detect if we have a unary selector. */
29701 if (maybe_unary_selector_p
29702 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29704 params = selector; /* Might be followed by attributes. */
29705 break;
29708 maybe_unary_selector_p = false;
29709 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29711 /* Something went quite wrong. There should be a colon
29712 here, but there is not. Stop parsing parameters. */
29713 break;
29715 type_name = cp_parser_objc_typename (parser);
29716 /* New ObjC allows attributes on parameters too. */
29717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29718 parm_attr = cp_parser_attributes_opt (parser);
29719 identifier = cp_parser_identifier (parser);
29721 params
29722 = chainon (params,
29723 objc_build_keyword_decl (selector,
29724 type_name,
29725 identifier,
29726 parm_attr));
29728 token = cp_lexer_peek_token (parser->lexer);
29731 if (params == NULL_TREE)
29733 cp_parser_error (parser, "objective-c++ method declaration is expected");
29734 return error_mark_node;
29737 /* We allow tail attributes for the method. */
29738 if (token->keyword == RID_ATTRIBUTE)
29740 *attributes = cp_parser_attributes_opt (parser);
29741 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29742 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29743 return params;
29744 cp_parser_error (parser,
29745 "method attributes must be specified at the end");
29746 return error_mark_node;
29749 if (params == NULL_TREE)
29751 cp_parser_error (parser, "objective-c++ method declaration is expected");
29752 return error_mark_node;
29754 return params;
29757 /* Parse the non-keyword Objective-C params. */
29759 static tree
29760 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29761 tree* attributes)
29763 tree params = make_node (TREE_LIST);
29764 cp_token *token = cp_lexer_peek_token (parser->lexer);
29765 *ellipsisp = false; /* Initially, assume no ellipsis. */
29767 while (token->type == CPP_COMMA)
29769 cp_parameter_declarator *parmdecl;
29770 tree parm;
29772 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29773 token = cp_lexer_peek_token (parser->lexer);
29775 if (token->type == CPP_ELLIPSIS)
29777 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29778 *ellipsisp = true;
29779 token = cp_lexer_peek_token (parser->lexer);
29780 break;
29783 /* TODO: parse attributes for tail parameters. */
29784 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29785 parm = grokdeclarator (parmdecl->declarator,
29786 &parmdecl->decl_specifiers,
29787 PARM, /*initialized=*/0,
29788 /*attrlist=*/NULL);
29790 chainon (params, build_tree_list (NULL_TREE, parm));
29791 token = cp_lexer_peek_token (parser->lexer);
29794 /* We allow tail attributes for the method. */
29795 if (token->keyword == RID_ATTRIBUTE)
29797 if (*attributes == NULL_TREE)
29799 *attributes = cp_parser_attributes_opt (parser);
29800 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29801 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29802 return params;
29804 else
29805 /* We have an error, but parse the attributes, so that we can
29806 carry on. */
29807 *attributes = cp_parser_attributes_opt (parser);
29809 cp_parser_error (parser,
29810 "method attributes must be specified at the end");
29811 return error_mark_node;
29814 return params;
29817 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29819 static void
29820 cp_parser_objc_interstitial_code (cp_parser* parser)
29822 cp_token *token = cp_lexer_peek_token (parser->lexer);
29824 /* If the next token is `extern' and the following token is a string
29825 literal, then we have a linkage specification. */
29826 if (token->keyword == RID_EXTERN
29827 && cp_parser_is_pure_string_literal
29828 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29829 cp_parser_linkage_specification (parser);
29830 /* Handle #pragma, if any. */
29831 else if (token->type == CPP_PRAGMA)
29832 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29833 /* Allow stray semicolons. */
29834 else if (token->type == CPP_SEMICOLON)
29835 cp_lexer_consume_token (parser->lexer);
29836 /* Mark methods as optional or required, when building protocols. */
29837 else if (token->keyword == RID_AT_OPTIONAL)
29839 cp_lexer_consume_token (parser->lexer);
29840 objc_set_method_opt (true);
29842 else if (token->keyword == RID_AT_REQUIRED)
29844 cp_lexer_consume_token (parser->lexer);
29845 objc_set_method_opt (false);
29847 else if (token->keyword == RID_NAMESPACE)
29848 cp_parser_namespace_definition (parser);
29849 /* Other stray characters must generate errors. */
29850 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29852 cp_lexer_consume_token (parser->lexer);
29853 error ("stray %qs between Objective-C++ methods",
29854 token->type == CPP_OPEN_BRACE ? "{" : "}");
29856 /* Finally, try to parse a block-declaration, or a function-definition. */
29857 else
29858 cp_parser_block_declaration (parser, /*statement_p=*/false);
29861 /* Parse a method signature. */
29863 static tree
29864 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29866 tree rettype, kwdparms, optparms;
29867 bool ellipsis = false;
29868 bool is_class_method;
29870 is_class_method = cp_parser_objc_method_type (parser);
29871 rettype = cp_parser_objc_typename (parser);
29872 *attributes = NULL_TREE;
29873 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29874 if (kwdparms == error_mark_node)
29875 return error_mark_node;
29876 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29877 if (optparms == error_mark_node)
29878 return error_mark_node;
29880 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29883 static bool
29884 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29886 tree tattr;
29887 cp_lexer_save_tokens (parser->lexer);
29888 tattr = cp_parser_attributes_opt (parser);
29889 gcc_assert (tattr) ;
29891 /* If the attributes are followed by a method introducer, this is not allowed.
29892 Dump the attributes and flag the situation. */
29893 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29894 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29895 return true;
29897 /* Otherwise, the attributes introduce some interstitial code, possibly so
29898 rewind to allow that check. */
29899 cp_lexer_rollback_tokens (parser->lexer);
29900 return false;
29903 /* Parse an Objective-C method prototype list. */
29905 static void
29906 cp_parser_objc_method_prototype_list (cp_parser* parser)
29908 cp_token *token = cp_lexer_peek_token (parser->lexer);
29910 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29912 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29914 tree attributes, sig;
29915 bool is_class_method;
29916 if (token->type == CPP_PLUS)
29917 is_class_method = true;
29918 else
29919 is_class_method = false;
29920 sig = cp_parser_objc_method_signature (parser, &attributes);
29921 if (sig == error_mark_node)
29923 cp_parser_skip_to_end_of_block_or_statement (parser);
29924 token = cp_lexer_peek_token (parser->lexer);
29925 continue;
29927 objc_add_method_declaration (is_class_method, sig, attributes);
29928 cp_parser_consume_semicolon_at_end_of_statement (parser);
29930 else if (token->keyword == RID_AT_PROPERTY)
29931 cp_parser_objc_at_property_declaration (parser);
29932 else if (token->keyword == RID_ATTRIBUTE
29933 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29934 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29935 OPT_Wattributes,
29936 "prefix attributes are ignored for methods");
29937 else
29938 /* Allow for interspersed non-ObjC++ code. */
29939 cp_parser_objc_interstitial_code (parser);
29941 token = cp_lexer_peek_token (parser->lexer);
29944 if (token->type != CPP_EOF)
29945 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29946 else
29947 cp_parser_error (parser, "expected %<@end%>");
29949 objc_finish_interface ();
29952 /* Parse an Objective-C method definition list. */
29954 static void
29955 cp_parser_objc_method_definition_list (cp_parser* parser)
29957 cp_token *token = cp_lexer_peek_token (parser->lexer);
29959 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29961 tree meth;
29963 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29965 cp_token *ptk;
29966 tree sig, attribute;
29967 bool is_class_method;
29968 if (token->type == CPP_PLUS)
29969 is_class_method = true;
29970 else
29971 is_class_method = false;
29972 push_deferring_access_checks (dk_deferred);
29973 sig = cp_parser_objc_method_signature (parser, &attribute);
29974 if (sig == error_mark_node)
29976 cp_parser_skip_to_end_of_block_or_statement (parser);
29977 token = cp_lexer_peek_token (parser->lexer);
29978 continue;
29980 objc_start_method_definition (is_class_method, sig, attribute,
29981 NULL_TREE);
29983 /* For historical reasons, we accept an optional semicolon. */
29984 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29985 cp_lexer_consume_token (parser->lexer);
29987 ptk = cp_lexer_peek_token (parser->lexer);
29988 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29989 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29991 perform_deferred_access_checks (tf_warning_or_error);
29992 stop_deferring_access_checks ();
29993 meth = cp_parser_function_definition_after_declarator (parser,
29994 false);
29995 pop_deferring_access_checks ();
29996 objc_finish_method_definition (meth);
29999 /* The following case will be removed once @synthesize is
30000 completely implemented. */
30001 else if (token->keyword == RID_AT_PROPERTY)
30002 cp_parser_objc_at_property_declaration (parser);
30003 else if (token->keyword == RID_AT_SYNTHESIZE)
30004 cp_parser_objc_at_synthesize_declaration (parser);
30005 else if (token->keyword == RID_AT_DYNAMIC)
30006 cp_parser_objc_at_dynamic_declaration (parser);
30007 else if (token->keyword == RID_ATTRIBUTE
30008 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
30009 warning_at (token->location, OPT_Wattributes,
30010 "prefix attributes are ignored for methods");
30011 else
30012 /* Allow for interspersed non-ObjC++ code. */
30013 cp_parser_objc_interstitial_code (parser);
30015 token = cp_lexer_peek_token (parser->lexer);
30018 if (token->type != CPP_EOF)
30019 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30020 else
30021 cp_parser_error (parser, "expected %<@end%>");
30023 objc_finish_implementation ();
30026 /* Parse Objective-C ivars. */
30028 static void
30029 cp_parser_objc_class_ivars (cp_parser* parser)
30031 cp_token *token = cp_lexer_peek_token (parser->lexer);
30033 if (token->type != CPP_OPEN_BRACE)
30034 return; /* No ivars specified. */
30036 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
30037 token = cp_lexer_peek_token (parser->lexer);
30039 while (token->type != CPP_CLOSE_BRACE
30040 && token->keyword != RID_AT_END && token->type != CPP_EOF)
30042 cp_decl_specifier_seq declspecs;
30043 int decl_class_or_enum_p;
30044 tree prefix_attributes;
30046 cp_parser_objc_visibility_spec (parser);
30048 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
30049 break;
30051 cp_parser_decl_specifier_seq (parser,
30052 CP_PARSER_FLAGS_OPTIONAL,
30053 &declspecs,
30054 &decl_class_or_enum_p);
30056 /* auto, register, static, extern, mutable. */
30057 if (declspecs.storage_class != sc_none)
30059 cp_parser_error (parser, "invalid type for instance variable");
30060 declspecs.storage_class = sc_none;
30063 /* thread_local. */
30064 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30066 cp_parser_error (parser, "invalid type for instance variable");
30067 declspecs.locations[ds_thread] = 0;
30070 /* typedef. */
30071 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30073 cp_parser_error (parser, "invalid type for instance variable");
30074 declspecs.locations[ds_typedef] = 0;
30077 prefix_attributes = declspecs.attributes;
30078 declspecs.attributes = NULL_TREE;
30080 /* Keep going until we hit the `;' at the end of the
30081 declaration. */
30082 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30084 tree width = NULL_TREE, attributes, first_attribute, decl;
30085 cp_declarator *declarator = NULL;
30086 int ctor_dtor_or_conv_p;
30088 /* Check for a (possibly unnamed) bitfield declaration. */
30089 token = cp_lexer_peek_token (parser->lexer);
30090 if (token->type == CPP_COLON)
30091 goto eat_colon;
30093 if (token->type == CPP_NAME
30094 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
30095 == CPP_COLON))
30097 /* Get the name of the bitfield. */
30098 declarator = make_id_declarator (NULL_TREE,
30099 cp_parser_identifier (parser),
30100 sfk_none);
30102 eat_colon:
30103 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30104 /* Get the width of the bitfield. */
30105 width
30106 = cp_parser_constant_expression (parser);
30108 else
30110 /* Parse the declarator. */
30111 declarator
30112 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30113 &ctor_dtor_or_conv_p,
30114 /*parenthesized_p=*/NULL,
30115 /*member_p=*/false,
30116 /*friend_p=*/false);
30119 /* Look for attributes that apply to the ivar. */
30120 attributes = cp_parser_attributes_opt (parser);
30121 /* Remember which attributes are prefix attributes and
30122 which are not. */
30123 first_attribute = attributes;
30124 /* Combine the attributes. */
30125 attributes = chainon (prefix_attributes, attributes);
30127 if (width)
30128 /* Create the bitfield declaration. */
30129 decl = grokbitfield (declarator, &declspecs,
30130 width,
30131 attributes);
30132 else
30133 decl = grokfield (declarator, &declspecs,
30134 NULL_TREE, /*init_const_expr_p=*/false,
30135 NULL_TREE, attributes);
30137 /* Add the instance variable. */
30138 if (decl != error_mark_node && decl != NULL_TREE)
30139 objc_add_instance_variable (decl);
30141 /* Reset PREFIX_ATTRIBUTES. */
30142 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30143 attributes = TREE_CHAIN (attributes);
30144 if (attributes)
30145 TREE_CHAIN (attributes) = NULL_TREE;
30147 token = cp_lexer_peek_token (parser->lexer);
30149 if (token->type == CPP_COMMA)
30151 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30152 continue;
30154 break;
30157 cp_parser_consume_semicolon_at_end_of_statement (parser);
30158 token = cp_lexer_peek_token (parser->lexer);
30161 if (token->keyword == RID_AT_END)
30162 cp_parser_error (parser, "expected %<}%>");
30164 /* Do not consume the RID_AT_END, so it will be read again as terminating
30165 the @interface of @implementation. */
30166 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
30167 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
30169 /* For historical reasons, we accept an optional semicolon. */
30170 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30171 cp_lexer_consume_token (parser->lexer);
30174 /* Parse an Objective-C protocol declaration. */
30176 static void
30177 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
30179 tree proto, protorefs;
30180 cp_token *tok;
30182 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
30183 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
30185 tok = cp_lexer_peek_token (parser->lexer);
30186 error_at (tok->location, "identifier expected after %<@protocol%>");
30187 cp_parser_consume_semicolon_at_end_of_statement (parser);
30188 return;
30191 /* See if we have a forward declaration or a definition. */
30192 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
30194 /* Try a forward declaration first. */
30195 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
30197 while (true)
30199 tree id;
30201 id = cp_parser_identifier (parser);
30202 if (id == error_mark_node)
30203 break;
30205 objc_declare_protocol (id, attributes);
30207 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30208 cp_lexer_consume_token (parser->lexer);
30209 else
30210 break;
30212 cp_parser_consume_semicolon_at_end_of_statement (parser);
30215 /* Ok, we got a full-fledged definition (or at least should). */
30216 else
30218 proto = cp_parser_identifier (parser);
30219 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30220 objc_start_protocol (proto, protorefs, attributes);
30221 cp_parser_objc_method_prototype_list (parser);
30225 /* Parse an Objective-C superclass or category. */
30227 static void
30228 cp_parser_objc_superclass_or_category (cp_parser *parser,
30229 bool iface_p,
30230 tree *super,
30231 tree *categ, bool *is_class_extension)
30233 cp_token *next = cp_lexer_peek_token (parser->lexer);
30235 *super = *categ = NULL_TREE;
30236 *is_class_extension = false;
30237 if (next->type == CPP_COLON)
30239 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30240 *super = cp_parser_identifier (parser);
30242 else if (next->type == CPP_OPEN_PAREN)
30244 matching_parens parens;
30245 parens.consume_open (parser); /* Eat '('. */
30247 /* If there is no category name, and this is an @interface, we
30248 have a class extension. */
30249 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30251 *categ = NULL_TREE;
30252 *is_class_extension = true;
30254 else
30255 *categ = cp_parser_identifier (parser);
30257 parens.require_close (parser);
30261 /* Parse an Objective-C class interface. */
30263 static void
30264 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30266 tree name, super, categ, protos;
30267 bool is_class_extension;
30269 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30270 name = cp_parser_identifier (parser);
30271 if (name == error_mark_node)
30273 /* It's hard to recover because even if valid @interface stuff
30274 is to follow, we can't compile it (or validate it) if we
30275 don't even know which class it refers to. Let's assume this
30276 was a stray '@interface' token in the stream and skip it.
30278 return;
30280 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30281 &is_class_extension);
30282 protos = cp_parser_objc_protocol_refs_opt (parser);
30284 /* We have either a class or a category on our hands. */
30285 if (categ || is_class_extension)
30286 objc_start_category_interface (name, categ, protos, attributes);
30287 else
30289 objc_start_class_interface (name, super, protos, attributes);
30290 /* Handle instance variable declarations, if any. */
30291 cp_parser_objc_class_ivars (parser);
30292 objc_continue_interface ();
30295 cp_parser_objc_method_prototype_list (parser);
30298 /* Parse an Objective-C class implementation. */
30300 static void
30301 cp_parser_objc_class_implementation (cp_parser* parser)
30303 tree name, super, categ;
30304 bool is_class_extension;
30306 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30307 name = cp_parser_identifier (parser);
30308 if (name == error_mark_node)
30310 /* It's hard to recover because even if valid @implementation
30311 stuff is to follow, we can't compile it (or validate it) if
30312 we don't even know which class it refers to. Let's assume
30313 this was a stray '@implementation' token in the stream and
30314 skip it.
30316 return;
30318 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30319 &is_class_extension);
30321 /* We have either a class or a category on our hands. */
30322 if (categ)
30323 objc_start_category_implementation (name, categ);
30324 else
30326 objc_start_class_implementation (name, super);
30327 /* Handle instance variable declarations, if any. */
30328 cp_parser_objc_class_ivars (parser);
30329 objc_continue_implementation ();
30332 cp_parser_objc_method_definition_list (parser);
30335 /* Consume the @end token and finish off the implementation. */
30337 static void
30338 cp_parser_objc_end_implementation (cp_parser* parser)
30340 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30341 objc_finish_implementation ();
30344 /* Parse an Objective-C declaration. */
30346 static void
30347 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30349 /* Try to figure out what kind of declaration is present. */
30350 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30352 if (attributes)
30353 switch (kwd->keyword)
30355 case RID_AT_ALIAS:
30356 case RID_AT_CLASS:
30357 case RID_AT_END:
30358 error_at (kwd->location, "attributes may not be specified before"
30359 " the %<@%D%> Objective-C++ keyword",
30360 kwd->u.value);
30361 attributes = NULL;
30362 break;
30363 case RID_AT_IMPLEMENTATION:
30364 warning_at (kwd->location, OPT_Wattributes,
30365 "prefix attributes are ignored before %<@%D%>",
30366 kwd->u.value);
30367 attributes = NULL;
30368 default:
30369 break;
30372 switch (kwd->keyword)
30374 case RID_AT_ALIAS:
30375 cp_parser_objc_alias_declaration (parser);
30376 break;
30377 case RID_AT_CLASS:
30378 cp_parser_objc_class_declaration (parser);
30379 break;
30380 case RID_AT_PROTOCOL:
30381 cp_parser_objc_protocol_declaration (parser, attributes);
30382 break;
30383 case RID_AT_INTERFACE:
30384 cp_parser_objc_class_interface (parser, attributes);
30385 break;
30386 case RID_AT_IMPLEMENTATION:
30387 cp_parser_objc_class_implementation (parser);
30388 break;
30389 case RID_AT_END:
30390 cp_parser_objc_end_implementation (parser);
30391 break;
30392 default:
30393 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30394 kwd->u.value);
30395 cp_parser_skip_to_end_of_block_or_statement (parser);
30399 /* Parse an Objective-C try-catch-finally statement.
30401 objc-try-catch-finally-stmt:
30402 @try compound-statement objc-catch-clause-seq [opt]
30403 objc-finally-clause [opt]
30405 objc-catch-clause-seq:
30406 objc-catch-clause objc-catch-clause-seq [opt]
30408 objc-catch-clause:
30409 @catch ( objc-exception-declaration ) compound-statement
30411 objc-finally-clause:
30412 @finally compound-statement
30414 objc-exception-declaration:
30415 parameter-declaration
30416 '...'
30418 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30420 Returns NULL_TREE.
30422 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30423 for C. Keep them in sync. */
30425 static tree
30426 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30428 location_t location;
30429 tree stmt;
30431 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30432 location = cp_lexer_peek_token (parser->lexer)->location;
30433 objc_maybe_warn_exceptions (location);
30434 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30435 node, lest it get absorbed into the surrounding block. */
30436 stmt = push_stmt_list ();
30437 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30438 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30440 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30442 cp_parameter_declarator *parm;
30443 tree parameter_declaration = error_mark_node;
30444 bool seen_open_paren = false;
30445 matching_parens parens;
30447 cp_lexer_consume_token (parser->lexer);
30448 if (parens.require_open (parser))
30449 seen_open_paren = true;
30450 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30452 /* We have "@catch (...)" (where the '...' are literally
30453 what is in the code). Skip the '...'.
30454 parameter_declaration is set to NULL_TREE, and
30455 objc_being_catch_clauses() knows that that means
30456 '...'. */
30457 cp_lexer_consume_token (parser->lexer);
30458 parameter_declaration = NULL_TREE;
30460 else
30462 /* We have "@catch (NSException *exception)" or something
30463 like that. Parse the parameter declaration. */
30464 parm = cp_parser_parameter_declaration (parser, false, NULL);
30465 if (parm == NULL)
30466 parameter_declaration = error_mark_node;
30467 else
30468 parameter_declaration = grokdeclarator (parm->declarator,
30469 &parm->decl_specifiers,
30470 PARM, /*initialized=*/0,
30471 /*attrlist=*/NULL);
30473 if (seen_open_paren)
30474 parens.require_close (parser);
30475 else
30477 /* If there was no open parenthesis, we are recovering from
30478 an error, and we are trying to figure out what mistake
30479 the user has made. */
30481 /* If there is an immediate closing parenthesis, the user
30482 probably forgot the opening one (ie, they typed "@catch
30483 NSException *e)". Parse the closing parenthesis and keep
30484 going. */
30485 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30486 cp_lexer_consume_token (parser->lexer);
30488 /* If these is no immediate closing parenthesis, the user
30489 probably doesn't know that parenthesis are required at
30490 all (ie, they typed "@catch NSException *e"). So, just
30491 forget about the closing parenthesis and keep going. */
30493 objc_begin_catch_clause (parameter_declaration);
30494 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30495 objc_finish_catch_clause ();
30497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30499 cp_lexer_consume_token (parser->lexer);
30500 location = cp_lexer_peek_token (parser->lexer)->location;
30501 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30502 node, lest it get absorbed into the surrounding block. */
30503 stmt = push_stmt_list ();
30504 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30505 objc_build_finally_clause (location, pop_stmt_list (stmt));
30508 return objc_finish_try_stmt ();
30511 /* Parse an Objective-C synchronized statement.
30513 objc-synchronized-stmt:
30514 @synchronized ( expression ) compound-statement
30516 Returns NULL_TREE. */
30518 static tree
30519 cp_parser_objc_synchronized_statement (cp_parser *parser)
30521 location_t location;
30522 tree lock, stmt;
30524 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30526 location = cp_lexer_peek_token (parser->lexer)->location;
30527 objc_maybe_warn_exceptions (location);
30528 matching_parens parens;
30529 parens.require_open (parser);
30530 lock = cp_parser_expression (parser);
30531 parens.require_close (parser);
30533 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30534 node, lest it get absorbed into the surrounding block. */
30535 stmt = push_stmt_list ();
30536 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30538 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30541 /* Parse an Objective-C throw statement.
30543 objc-throw-stmt:
30544 @throw assignment-expression [opt] ;
30546 Returns a constructed '@throw' statement. */
30548 static tree
30549 cp_parser_objc_throw_statement (cp_parser *parser)
30551 tree expr = NULL_TREE;
30552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30554 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30557 expr = cp_parser_expression (parser);
30559 cp_parser_consume_semicolon_at_end_of_statement (parser);
30561 return objc_build_throw_stmt (loc, expr);
30564 /* Parse an Objective-C statement. */
30566 static tree
30567 cp_parser_objc_statement (cp_parser * parser)
30569 /* Try to figure out what kind of declaration is present. */
30570 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30572 switch (kwd->keyword)
30574 case RID_AT_TRY:
30575 return cp_parser_objc_try_catch_finally_statement (parser);
30576 case RID_AT_SYNCHRONIZED:
30577 return cp_parser_objc_synchronized_statement (parser);
30578 case RID_AT_THROW:
30579 return cp_parser_objc_throw_statement (parser);
30580 default:
30581 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30582 kwd->u.value);
30583 cp_parser_skip_to_end_of_block_or_statement (parser);
30586 return error_mark_node;
30589 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30590 look ahead to see if an objc keyword follows the attributes. This
30591 is to detect the use of prefix attributes on ObjC @interface and
30592 @protocol. */
30594 static bool
30595 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30597 cp_lexer_save_tokens (parser->lexer);
30598 *attrib = cp_parser_attributes_opt (parser);
30599 gcc_assert (*attrib);
30600 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30602 cp_lexer_commit_tokens (parser->lexer);
30603 return true;
30605 cp_lexer_rollback_tokens (parser->lexer);
30606 return false;
30609 /* This routine is a minimal replacement for
30610 c_parser_struct_declaration () used when parsing the list of
30611 types/names or ObjC++ properties. For example, when parsing the
30612 code
30614 @property (readonly) int a, b, c;
30616 this function is responsible for parsing "int a, int b, int c" and
30617 returning the declarations as CHAIN of DECLs.
30619 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30620 similar parsing. */
30621 static tree
30622 cp_parser_objc_struct_declaration (cp_parser *parser)
30624 tree decls = NULL_TREE;
30625 cp_decl_specifier_seq declspecs;
30626 int decl_class_or_enum_p;
30627 tree prefix_attributes;
30629 cp_parser_decl_specifier_seq (parser,
30630 CP_PARSER_FLAGS_NONE,
30631 &declspecs,
30632 &decl_class_or_enum_p);
30634 if (declspecs.type == error_mark_node)
30635 return error_mark_node;
30637 /* auto, register, static, extern, mutable. */
30638 if (declspecs.storage_class != sc_none)
30640 cp_parser_error (parser, "invalid type for property");
30641 declspecs.storage_class = sc_none;
30644 /* thread_local. */
30645 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30647 cp_parser_error (parser, "invalid type for property");
30648 declspecs.locations[ds_thread] = 0;
30651 /* typedef. */
30652 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30654 cp_parser_error (parser, "invalid type for property");
30655 declspecs.locations[ds_typedef] = 0;
30658 prefix_attributes = declspecs.attributes;
30659 declspecs.attributes = NULL_TREE;
30661 /* Keep going until we hit the `;' at the end of the declaration. */
30662 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30664 tree attributes, first_attribute, decl;
30665 cp_declarator *declarator;
30666 cp_token *token;
30668 /* Parse the declarator. */
30669 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30670 NULL, NULL, false, false);
30672 /* Look for attributes that apply to the ivar. */
30673 attributes = cp_parser_attributes_opt (parser);
30674 /* Remember which attributes are prefix attributes and
30675 which are not. */
30676 first_attribute = attributes;
30677 /* Combine the attributes. */
30678 attributes = chainon (prefix_attributes, attributes);
30680 decl = grokfield (declarator, &declspecs,
30681 NULL_TREE, /*init_const_expr_p=*/false,
30682 NULL_TREE, attributes);
30684 if (decl == error_mark_node || decl == NULL_TREE)
30685 return error_mark_node;
30687 /* Reset PREFIX_ATTRIBUTES. */
30688 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30689 attributes = TREE_CHAIN (attributes);
30690 if (attributes)
30691 TREE_CHAIN (attributes) = NULL_TREE;
30693 DECL_CHAIN (decl) = decls;
30694 decls = decl;
30696 token = cp_lexer_peek_token (parser->lexer);
30697 if (token->type == CPP_COMMA)
30699 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30700 continue;
30702 else
30703 break;
30705 return decls;
30708 /* Parse an Objective-C @property declaration. The syntax is:
30710 objc-property-declaration:
30711 '@property' objc-property-attributes[opt] struct-declaration ;
30713 objc-property-attributes:
30714 '(' objc-property-attribute-list ')'
30716 objc-property-attribute-list:
30717 objc-property-attribute
30718 objc-property-attribute-list, objc-property-attribute
30720 objc-property-attribute
30721 'getter' = identifier
30722 'setter' = identifier
30723 'readonly'
30724 'readwrite'
30725 'assign'
30726 'retain'
30727 'copy'
30728 'nonatomic'
30730 For example:
30731 @property NSString *name;
30732 @property (readonly) id object;
30733 @property (retain, nonatomic, getter=getTheName) id name;
30734 @property int a, b, c;
30736 PS: This function is identical to
30737 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30738 static void
30739 cp_parser_objc_at_property_declaration (cp_parser *parser)
30741 /* The following variables hold the attributes of the properties as
30742 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30743 seen. When we see an attribute, we set them to 'true' (if they
30744 are boolean properties) or to the identifier (if they have an
30745 argument, ie, for getter and setter). Note that here we only
30746 parse the list of attributes, check the syntax and accumulate the
30747 attributes that we find. objc_add_property_declaration() will
30748 then process the information. */
30749 bool property_assign = false;
30750 bool property_copy = false;
30751 tree property_getter_ident = NULL_TREE;
30752 bool property_nonatomic = false;
30753 bool property_readonly = false;
30754 bool property_readwrite = false;
30755 bool property_retain = false;
30756 tree property_setter_ident = NULL_TREE;
30758 /* 'properties' is the list of properties that we read. Usually a
30759 single one, but maybe more (eg, in "@property int a, b, c;" there
30760 are three). */
30761 tree properties;
30762 location_t loc;
30764 loc = cp_lexer_peek_token (parser->lexer)->location;
30766 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30768 /* Parse the optional attribute list... */
30769 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30771 /* Eat the '('. */
30772 matching_parens parens;
30773 parens.consume_open (parser);
30775 while (true)
30777 bool syntax_error = false;
30778 cp_token *token = cp_lexer_peek_token (parser->lexer);
30779 enum rid keyword;
30781 if (token->type != CPP_NAME)
30783 cp_parser_error (parser, "expected identifier");
30784 break;
30786 keyword = C_RID_CODE (token->u.value);
30787 cp_lexer_consume_token (parser->lexer);
30788 switch (keyword)
30790 case RID_ASSIGN: property_assign = true; break;
30791 case RID_COPY: property_copy = true; break;
30792 case RID_NONATOMIC: property_nonatomic = true; break;
30793 case RID_READONLY: property_readonly = true; break;
30794 case RID_READWRITE: property_readwrite = true; break;
30795 case RID_RETAIN: property_retain = true; break;
30797 case RID_GETTER:
30798 case RID_SETTER:
30799 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30801 if (keyword == RID_GETTER)
30802 cp_parser_error (parser,
30803 "missing %<=%> (after %<getter%> attribute)");
30804 else
30805 cp_parser_error (parser,
30806 "missing %<=%> (after %<setter%> attribute)");
30807 syntax_error = true;
30808 break;
30810 cp_lexer_consume_token (parser->lexer); /* eat the = */
30811 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30813 cp_parser_error (parser, "expected identifier");
30814 syntax_error = true;
30815 break;
30817 if (keyword == RID_SETTER)
30819 if (property_setter_ident != NULL_TREE)
30821 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30822 cp_lexer_consume_token (parser->lexer);
30824 else
30825 property_setter_ident = cp_parser_objc_selector (parser);
30826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30827 cp_parser_error (parser, "setter name must terminate with %<:%>");
30828 else
30829 cp_lexer_consume_token (parser->lexer);
30831 else
30833 if (property_getter_ident != NULL_TREE)
30835 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30836 cp_lexer_consume_token (parser->lexer);
30838 else
30839 property_getter_ident = cp_parser_objc_selector (parser);
30841 break;
30842 default:
30843 cp_parser_error (parser, "unknown property attribute");
30844 syntax_error = true;
30845 break;
30848 if (syntax_error)
30849 break;
30851 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30852 cp_lexer_consume_token (parser->lexer);
30853 else
30854 break;
30857 /* FIXME: "@property (setter, assign);" will generate a spurious
30858 "error: expected ‘)’ before ‘,’ token". This is because
30859 cp_parser_require, unlike the C counterpart, will produce an
30860 error even if we are in error recovery. */
30861 if (!parens.require_close (parser))
30863 cp_parser_skip_to_closing_parenthesis (parser,
30864 /*recovering=*/true,
30865 /*or_comma=*/false,
30866 /*consume_paren=*/true);
30870 /* ... and the property declaration(s). */
30871 properties = cp_parser_objc_struct_declaration (parser);
30873 if (properties == error_mark_node)
30875 cp_parser_skip_to_end_of_statement (parser);
30876 /* If the next token is now a `;', consume it. */
30877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30878 cp_lexer_consume_token (parser->lexer);
30879 return;
30882 if (properties == NULL_TREE)
30883 cp_parser_error (parser, "expected identifier");
30884 else
30886 /* Comma-separated properties are chained together in
30887 reverse order; add them one by one. */
30888 properties = nreverse (properties);
30890 for (; properties; properties = TREE_CHAIN (properties))
30891 objc_add_property_declaration (loc, copy_node (properties),
30892 property_readonly, property_readwrite,
30893 property_assign, property_retain,
30894 property_copy, property_nonatomic,
30895 property_getter_ident, property_setter_ident);
30898 cp_parser_consume_semicolon_at_end_of_statement (parser);
30901 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30903 objc-synthesize-declaration:
30904 @synthesize objc-synthesize-identifier-list ;
30906 objc-synthesize-identifier-list:
30907 objc-synthesize-identifier
30908 objc-synthesize-identifier-list, objc-synthesize-identifier
30910 objc-synthesize-identifier
30911 identifier
30912 identifier = identifier
30914 For example:
30915 @synthesize MyProperty;
30916 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30918 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30919 for C. Keep them in sync.
30921 static void
30922 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30924 tree list = NULL_TREE;
30925 location_t loc;
30926 loc = cp_lexer_peek_token (parser->lexer)->location;
30928 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30929 while (true)
30931 tree property, ivar;
30932 property = cp_parser_identifier (parser);
30933 if (property == error_mark_node)
30935 cp_parser_consume_semicolon_at_end_of_statement (parser);
30936 return;
30938 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30940 cp_lexer_consume_token (parser->lexer);
30941 ivar = cp_parser_identifier (parser);
30942 if (ivar == error_mark_node)
30944 cp_parser_consume_semicolon_at_end_of_statement (parser);
30945 return;
30948 else
30949 ivar = NULL_TREE;
30950 list = chainon (list, build_tree_list (ivar, property));
30951 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30952 cp_lexer_consume_token (parser->lexer);
30953 else
30954 break;
30956 cp_parser_consume_semicolon_at_end_of_statement (parser);
30957 objc_add_synthesize_declaration (loc, list);
30960 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30962 objc-dynamic-declaration:
30963 @dynamic identifier-list ;
30965 For example:
30966 @dynamic MyProperty;
30967 @dynamic MyProperty, AnotherProperty;
30969 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30970 for C. Keep them in sync.
30972 static void
30973 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30975 tree list = NULL_TREE;
30976 location_t loc;
30977 loc = cp_lexer_peek_token (parser->lexer)->location;
30979 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30980 while (true)
30982 tree property;
30983 property = cp_parser_identifier (parser);
30984 if (property == error_mark_node)
30986 cp_parser_consume_semicolon_at_end_of_statement (parser);
30987 return;
30989 list = chainon (list, build_tree_list (NULL, property));
30990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30991 cp_lexer_consume_token (parser->lexer);
30992 else
30993 break;
30995 cp_parser_consume_semicolon_at_end_of_statement (parser);
30996 objc_add_dynamic_declaration (loc, list);
31000 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
31002 /* Returns name of the next clause.
31003 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
31004 the token is not consumed. Otherwise appropriate pragma_omp_clause is
31005 returned and the token is consumed. */
31007 static pragma_omp_clause
31008 cp_parser_omp_clause_name (cp_parser *parser)
31010 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
31012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
31013 result = PRAGMA_OACC_CLAUSE_AUTO;
31014 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
31015 result = PRAGMA_OMP_CLAUSE_IF;
31016 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
31017 result = PRAGMA_OMP_CLAUSE_DEFAULT;
31018 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
31019 result = PRAGMA_OACC_CLAUSE_DELETE;
31020 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
31021 result = PRAGMA_OMP_CLAUSE_PRIVATE;
31022 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
31023 result = PRAGMA_OMP_CLAUSE_FOR;
31024 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31026 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31027 const char *p = IDENTIFIER_POINTER (id);
31029 switch (p[0])
31031 case 'a':
31032 if (!strcmp ("aligned", p))
31033 result = PRAGMA_OMP_CLAUSE_ALIGNED;
31034 else if (!strcmp ("async", p))
31035 result = PRAGMA_OACC_CLAUSE_ASYNC;
31036 break;
31037 case 'c':
31038 if (!strcmp ("collapse", p))
31039 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
31040 else if (!strcmp ("copy", p))
31041 result = PRAGMA_OACC_CLAUSE_COPY;
31042 else if (!strcmp ("copyin", p))
31043 result = PRAGMA_OMP_CLAUSE_COPYIN;
31044 else if (!strcmp ("copyout", p))
31045 result = PRAGMA_OACC_CLAUSE_COPYOUT;
31046 else if (!strcmp ("copyprivate", p))
31047 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
31048 else if (!strcmp ("create", p))
31049 result = PRAGMA_OACC_CLAUSE_CREATE;
31050 break;
31051 case 'd':
31052 if (!strcmp ("defaultmap", p))
31053 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
31054 else if (!strcmp ("depend", p))
31055 result = PRAGMA_OMP_CLAUSE_DEPEND;
31056 else if (!strcmp ("device", p))
31057 result = PRAGMA_OMP_CLAUSE_DEVICE;
31058 else if (!strcmp ("deviceptr", p))
31059 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
31060 else if (!strcmp ("device_resident", p))
31061 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
31062 else if (!strcmp ("dist_schedule", p))
31063 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
31064 break;
31065 case 'f':
31066 if (!strcmp ("final", p))
31067 result = PRAGMA_OMP_CLAUSE_FINAL;
31068 else if (!strcmp ("firstprivate", p))
31069 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
31070 else if (!strcmp ("from", p))
31071 result = PRAGMA_OMP_CLAUSE_FROM;
31072 break;
31073 case 'g':
31074 if (!strcmp ("gang", p))
31075 result = PRAGMA_OACC_CLAUSE_GANG;
31076 else if (!strcmp ("grainsize", p))
31077 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
31078 break;
31079 case 'h':
31080 if (!strcmp ("hint", p))
31081 result = PRAGMA_OMP_CLAUSE_HINT;
31082 else if (!strcmp ("host", p))
31083 result = PRAGMA_OACC_CLAUSE_HOST;
31084 break;
31085 case 'i':
31086 if (!strcmp ("inbranch", p))
31087 result = PRAGMA_OMP_CLAUSE_INBRANCH;
31088 else if (!strcmp ("independent", p))
31089 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
31090 else if (!strcmp ("is_device_ptr", p))
31091 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
31092 break;
31093 case 'l':
31094 if (!strcmp ("lastprivate", p))
31095 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
31096 else if (!strcmp ("linear", p))
31097 result = PRAGMA_OMP_CLAUSE_LINEAR;
31098 else if (!strcmp ("link", p))
31099 result = PRAGMA_OMP_CLAUSE_LINK;
31100 break;
31101 case 'm':
31102 if (!strcmp ("map", p))
31103 result = PRAGMA_OMP_CLAUSE_MAP;
31104 else if (!strcmp ("mergeable", p))
31105 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
31106 else if (flag_cilkplus && !strcmp ("mask", p))
31107 result = PRAGMA_CILK_CLAUSE_MASK;
31108 break;
31109 case 'n':
31110 if (!strcmp ("nogroup", p))
31111 result = PRAGMA_OMP_CLAUSE_NOGROUP;
31112 else if (!strcmp ("notinbranch", p))
31113 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
31114 else if (!strcmp ("nowait", p))
31115 result = PRAGMA_OMP_CLAUSE_NOWAIT;
31116 else if (flag_cilkplus && !strcmp ("nomask", p))
31117 result = PRAGMA_CILK_CLAUSE_NOMASK;
31118 else if (!strcmp ("num_gangs", p))
31119 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
31120 else if (!strcmp ("num_tasks", p))
31121 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
31122 else if (!strcmp ("num_teams", p))
31123 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
31124 else if (!strcmp ("num_threads", p))
31125 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
31126 else if (!strcmp ("num_workers", p))
31127 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
31128 break;
31129 case 'o':
31130 if (!strcmp ("ordered", p))
31131 result = PRAGMA_OMP_CLAUSE_ORDERED;
31132 break;
31133 case 'p':
31134 if (!strcmp ("parallel", p))
31135 result = PRAGMA_OMP_CLAUSE_PARALLEL;
31136 else if (!strcmp ("present", p))
31137 result = PRAGMA_OACC_CLAUSE_PRESENT;
31138 else if (!strcmp ("present_or_copy", p)
31139 || !strcmp ("pcopy", p))
31140 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
31141 else if (!strcmp ("present_or_copyin", p)
31142 || !strcmp ("pcopyin", p))
31143 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
31144 else if (!strcmp ("present_or_copyout", p)
31145 || !strcmp ("pcopyout", p))
31146 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
31147 else if (!strcmp ("present_or_create", p)
31148 || !strcmp ("pcreate", p))
31149 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
31150 else if (!strcmp ("priority", p))
31151 result = PRAGMA_OMP_CLAUSE_PRIORITY;
31152 else if (!strcmp ("proc_bind", p))
31153 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
31154 break;
31155 case 'r':
31156 if (!strcmp ("reduction", p))
31157 result = PRAGMA_OMP_CLAUSE_REDUCTION;
31158 break;
31159 case 's':
31160 if (!strcmp ("safelen", p))
31161 result = PRAGMA_OMP_CLAUSE_SAFELEN;
31162 else if (!strcmp ("schedule", p))
31163 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
31164 else if (!strcmp ("sections", p))
31165 result = PRAGMA_OMP_CLAUSE_SECTIONS;
31166 else if (!strcmp ("self", p))
31167 result = PRAGMA_OACC_CLAUSE_SELF;
31168 else if (!strcmp ("seq", p))
31169 result = PRAGMA_OACC_CLAUSE_SEQ;
31170 else if (!strcmp ("shared", p))
31171 result = PRAGMA_OMP_CLAUSE_SHARED;
31172 else if (!strcmp ("simd", p))
31173 result = PRAGMA_OMP_CLAUSE_SIMD;
31174 else if (!strcmp ("simdlen", p))
31175 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
31176 break;
31177 case 't':
31178 if (!strcmp ("taskgroup", p))
31179 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
31180 else if (!strcmp ("thread_limit", p))
31181 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
31182 else if (!strcmp ("threads", p))
31183 result = PRAGMA_OMP_CLAUSE_THREADS;
31184 else if (!strcmp ("tile", p))
31185 result = PRAGMA_OACC_CLAUSE_TILE;
31186 else if (!strcmp ("to", p))
31187 result = PRAGMA_OMP_CLAUSE_TO;
31188 break;
31189 case 'u':
31190 if (!strcmp ("uniform", p))
31191 result = PRAGMA_OMP_CLAUSE_UNIFORM;
31192 else if (!strcmp ("untied", p))
31193 result = PRAGMA_OMP_CLAUSE_UNTIED;
31194 else if (!strcmp ("use_device", p))
31195 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
31196 else if (!strcmp ("use_device_ptr", p))
31197 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
31198 break;
31199 case 'v':
31200 if (!strcmp ("vector", p))
31201 result = PRAGMA_OACC_CLAUSE_VECTOR;
31202 else if (!strcmp ("vector_length", p))
31203 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
31204 else if (flag_cilkplus && !strcmp ("vectorlength", p))
31205 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
31206 break;
31207 case 'w':
31208 if (!strcmp ("wait", p))
31209 result = PRAGMA_OACC_CLAUSE_WAIT;
31210 else if (!strcmp ("worker", p))
31211 result = PRAGMA_OACC_CLAUSE_WORKER;
31212 break;
31216 if (result != PRAGMA_OMP_CLAUSE_NONE)
31217 cp_lexer_consume_token (parser->lexer);
31219 return result;
31222 /* Validate that a clause of the given type does not already exist. */
31224 static void
31225 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31226 const char *name, location_t location)
31228 tree c;
31230 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31231 if (OMP_CLAUSE_CODE (c) == code)
31233 error_at (location, "too many %qs clauses", name);
31234 break;
31238 /* OpenMP 2.5:
31239 variable-list:
31240 identifier
31241 variable-list , identifier
31243 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31244 colon). An opening parenthesis will have been consumed by the caller.
31246 If KIND is nonzero, create the appropriate node and install the decl
31247 in OMP_CLAUSE_DECL and add the node to the head of the list.
31249 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31250 return the list created.
31252 COLON can be NULL if only closing parenthesis should end the list,
31253 or pointer to bool which will receive false if the list is terminated
31254 by closing parenthesis or true if the list is terminated by colon. */
31256 static tree
31257 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31258 tree list, bool *colon)
31260 cp_token *token;
31261 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31262 if (colon)
31264 parser->colon_corrects_to_scope_p = false;
31265 *colon = false;
31267 while (1)
31269 tree name, decl;
31271 token = cp_lexer_peek_token (parser->lexer);
31272 if (kind != 0
31273 && current_class_ptr
31274 && cp_parser_is_keyword (token, RID_THIS))
31276 decl = finish_this_expr ();
31277 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31278 || CONVERT_EXPR_P (decl))
31279 decl = TREE_OPERAND (decl, 0);
31280 cp_lexer_consume_token (parser->lexer);
31282 else
31284 name = cp_parser_id_expression (parser, /*template_p=*/false,
31285 /*check_dependency_p=*/true,
31286 /*template_p=*/NULL,
31287 /*declarator_p=*/false,
31288 /*optional_p=*/false);
31289 if (name == error_mark_node)
31290 goto skip_comma;
31292 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31293 if (decl == error_mark_node)
31294 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31295 token->location);
31297 if (decl == error_mark_node)
31299 else if (kind != 0)
31301 switch (kind)
31303 case OMP_CLAUSE__CACHE_:
31304 /* The OpenACC cache directive explicitly only allows "array
31305 elements or subarrays". */
31306 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31308 error_at (token->location, "expected %<[%>");
31309 decl = error_mark_node;
31310 break;
31312 /* FALLTHROUGH. */
31313 case OMP_CLAUSE_MAP:
31314 case OMP_CLAUSE_FROM:
31315 case OMP_CLAUSE_TO:
31316 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31318 location_t loc
31319 = cp_lexer_peek_token (parser->lexer)->location;
31320 cp_id_kind idk = CP_ID_KIND_NONE;
31321 cp_lexer_consume_token (parser->lexer);
31322 decl = convert_from_reference (decl);
31323 decl
31324 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31325 decl, false,
31326 &idk, loc);
31328 /* FALLTHROUGH. */
31329 case OMP_CLAUSE_DEPEND:
31330 case OMP_CLAUSE_REDUCTION:
31331 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31333 tree low_bound = NULL_TREE, length = NULL_TREE;
31335 parser->colon_corrects_to_scope_p = false;
31336 cp_lexer_consume_token (parser->lexer);
31337 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31338 low_bound = cp_parser_expression (parser);
31339 if (!colon)
31340 parser->colon_corrects_to_scope_p
31341 = saved_colon_corrects_to_scope_p;
31342 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31343 length = integer_one_node;
31344 else
31346 /* Look for `:'. */
31347 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31348 goto skip_comma;
31349 if (!cp_lexer_next_token_is (parser->lexer,
31350 CPP_CLOSE_SQUARE))
31351 length = cp_parser_expression (parser);
31353 /* Look for the closing `]'. */
31354 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31355 RT_CLOSE_SQUARE))
31356 goto skip_comma;
31358 decl = tree_cons (low_bound, length, decl);
31360 break;
31361 default:
31362 break;
31365 tree u = build_omp_clause (token->location, kind);
31366 OMP_CLAUSE_DECL (u) = decl;
31367 OMP_CLAUSE_CHAIN (u) = list;
31368 list = u;
31370 else
31371 list = tree_cons (decl, NULL_TREE, list);
31373 get_comma:
31374 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31375 break;
31376 cp_lexer_consume_token (parser->lexer);
31379 if (colon)
31380 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31382 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31384 *colon = true;
31385 cp_parser_require (parser, CPP_COLON, RT_COLON);
31386 return list;
31389 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31391 int ending;
31393 /* Try to resync to an unnested comma. Copied from
31394 cp_parser_parenthesized_expression_list. */
31395 skip_comma:
31396 if (colon)
31397 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31398 ending = cp_parser_skip_to_closing_parenthesis (parser,
31399 /*recovering=*/true,
31400 /*or_comma=*/true,
31401 /*consume_paren=*/true);
31402 if (ending < 0)
31403 goto get_comma;
31406 return list;
31409 /* Similarly, but expect leading and trailing parenthesis. This is a very
31410 common case for omp clauses. */
31412 static tree
31413 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31415 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31416 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31417 return list;
31420 /* OpenACC 2.0:
31421 copy ( variable-list )
31422 copyin ( variable-list )
31423 copyout ( variable-list )
31424 create ( variable-list )
31425 delete ( variable-list )
31426 present ( variable-list )
31427 present_or_copy ( variable-list )
31428 pcopy ( variable-list )
31429 present_or_copyin ( variable-list )
31430 pcopyin ( variable-list )
31431 present_or_copyout ( variable-list )
31432 pcopyout ( variable-list )
31433 present_or_create ( variable-list )
31434 pcreate ( variable-list ) */
31436 static tree
31437 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31438 tree list)
31440 enum gomp_map_kind kind;
31441 switch (c_kind)
31443 case PRAGMA_OACC_CLAUSE_COPY:
31444 kind = GOMP_MAP_FORCE_TOFROM;
31445 break;
31446 case PRAGMA_OACC_CLAUSE_COPYIN:
31447 kind = GOMP_MAP_FORCE_TO;
31448 break;
31449 case PRAGMA_OACC_CLAUSE_COPYOUT:
31450 kind = GOMP_MAP_FORCE_FROM;
31451 break;
31452 case PRAGMA_OACC_CLAUSE_CREATE:
31453 kind = GOMP_MAP_FORCE_ALLOC;
31454 break;
31455 case PRAGMA_OACC_CLAUSE_DELETE:
31456 kind = GOMP_MAP_DELETE;
31457 break;
31458 case PRAGMA_OACC_CLAUSE_DEVICE:
31459 kind = GOMP_MAP_FORCE_TO;
31460 break;
31461 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31462 kind = GOMP_MAP_DEVICE_RESIDENT;
31463 break;
31464 case PRAGMA_OACC_CLAUSE_HOST:
31465 case PRAGMA_OACC_CLAUSE_SELF:
31466 kind = GOMP_MAP_FORCE_FROM;
31467 break;
31468 case PRAGMA_OACC_CLAUSE_LINK:
31469 kind = GOMP_MAP_LINK;
31470 break;
31471 case PRAGMA_OACC_CLAUSE_PRESENT:
31472 kind = GOMP_MAP_FORCE_PRESENT;
31473 break;
31474 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31475 kind = GOMP_MAP_TOFROM;
31476 break;
31477 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31478 kind = GOMP_MAP_TO;
31479 break;
31480 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31481 kind = GOMP_MAP_FROM;
31482 break;
31483 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31484 kind = GOMP_MAP_ALLOC;
31485 break;
31486 default:
31487 gcc_unreachable ();
31489 tree nl, c;
31490 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31492 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31493 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31495 return nl;
31498 /* OpenACC 2.0:
31499 deviceptr ( variable-list ) */
31501 static tree
31502 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31504 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31505 tree vars, t;
31507 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31508 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31509 variable-list must only allow for pointer variables. */
31510 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31511 for (t = vars; t; t = TREE_CHAIN (t))
31513 tree v = TREE_PURPOSE (t);
31514 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31515 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31516 OMP_CLAUSE_DECL (u) = v;
31517 OMP_CLAUSE_CHAIN (u) = list;
31518 list = u;
31521 return list;
31524 /* OpenACC 2.0:
31525 auto
31526 independent
31527 nohost
31528 seq */
31530 static tree
31531 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31532 enum omp_clause_code code,
31533 tree list, location_t location)
31535 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31536 tree c = build_omp_clause (location, code);
31537 OMP_CLAUSE_CHAIN (c) = list;
31538 return c;
31541 /* OpenACC:
31542 num_gangs ( expression )
31543 num_workers ( expression )
31544 vector_length ( expression ) */
31546 static tree
31547 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31548 const char *str, tree list)
31550 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31552 matching_parens parens;
31553 if (!parens.require_open (parser))
31554 return list;
31556 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31558 if (t == error_mark_node
31559 || !parens.require_close (parser))
31561 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31562 /*or_comma=*/false,
31563 /*consume_paren=*/true);
31564 return list;
31567 check_no_duplicate_clause (list, code, str, loc);
31569 tree c = build_omp_clause (loc, code);
31570 OMP_CLAUSE_OPERAND (c, 0) = t;
31571 OMP_CLAUSE_CHAIN (c) = list;
31572 return c;
31575 /* OpenACC:
31577 gang [( gang-arg-list )]
31578 worker [( [num:] int-expr )]
31579 vector [( [length:] int-expr )]
31581 where gang-arg is one of:
31583 [num:] int-expr
31584 static: size-expr
31586 and size-expr may be:
31589 int-expr
31592 static tree
31593 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31594 const char *str, tree list)
31596 const char *id = "num";
31597 cp_lexer *lexer = parser->lexer;
31598 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31599 location_t loc = cp_lexer_peek_token (lexer)->location;
31601 if (kind == OMP_CLAUSE_VECTOR)
31602 id = "length";
31604 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31606 matching_parens parens;
31607 parens.consume_open (parser);
31611 cp_token *next = cp_lexer_peek_token (lexer);
31612 int idx = 0;
31614 /* Gang static argument. */
31615 if (kind == OMP_CLAUSE_GANG
31616 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31618 cp_lexer_consume_token (lexer);
31620 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31621 goto cleanup_error;
31623 idx = 1;
31624 if (ops[idx] != NULL)
31626 cp_parser_error (parser, "too many %<static%> arguments");
31627 goto cleanup_error;
31630 /* Check for the '*' argument. */
31631 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31632 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31633 || cp_lexer_nth_token_is (parser->lexer, 2,
31634 CPP_CLOSE_PAREN)))
31636 cp_lexer_consume_token (lexer);
31637 ops[idx] = integer_minus_one_node;
31639 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31641 cp_lexer_consume_token (lexer);
31642 continue;
31644 else break;
31647 /* Worker num: argument and vector length: arguments. */
31648 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31649 && id_equal (next->u.value, id)
31650 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31652 cp_lexer_consume_token (lexer); /* id */
31653 cp_lexer_consume_token (lexer); /* ':' */
31656 /* Now collect the actual argument. */
31657 if (ops[idx] != NULL_TREE)
31659 cp_parser_error (parser, "unexpected argument");
31660 goto cleanup_error;
31663 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31664 false);
31665 if (expr == error_mark_node)
31666 goto cleanup_error;
31668 mark_exp_read (expr);
31669 ops[idx] = expr;
31671 if (kind == OMP_CLAUSE_GANG
31672 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31674 cp_lexer_consume_token (lexer);
31675 continue;
31677 break;
31679 while (1);
31681 if (!parens.require_close (parser))
31682 goto cleanup_error;
31685 check_no_duplicate_clause (list, kind, str, loc);
31687 c = build_omp_clause (loc, kind);
31689 if (ops[1])
31690 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31692 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31693 OMP_CLAUSE_CHAIN (c) = list;
31695 return c;
31697 cleanup_error:
31698 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31699 return list;
31702 /* OpenACC 2.0:
31703 tile ( size-expr-list ) */
31705 static tree
31706 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31708 tree c, expr = error_mark_node;
31709 tree tile = NULL_TREE;
31711 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31712 so, but the spec authors never considered such a case and have
31713 differing opinions on what it might mean, including 'not
31714 allowed'.) */
31715 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31716 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31717 clause_loc);
31719 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31720 return list;
31724 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31725 return list;
31727 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31728 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31729 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31731 cp_lexer_consume_token (parser->lexer);
31732 expr = integer_zero_node;
31734 else
31735 expr = cp_parser_constant_expression (parser);
31737 tile = tree_cons (NULL_TREE, expr, tile);
31739 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31741 /* Consume the trailing ')'. */
31742 cp_lexer_consume_token (parser->lexer);
31744 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31745 tile = nreverse (tile);
31746 OMP_CLAUSE_TILE_LIST (c) = tile;
31747 OMP_CLAUSE_CHAIN (c) = list;
31748 return c;
31751 /* OpenACC 2.0
31752 Parse wait clause or directive parameters. */
31754 static tree
31755 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31757 vec<tree, va_gc> *args;
31758 tree t, args_tree;
31760 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31761 /*cast_p=*/false,
31762 /*allow_expansion_p=*/true,
31763 /*non_constant_p=*/NULL);
31765 if (args == NULL || args->length () == 0)
31767 cp_parser_error (parser, "expected integer expression before ')'");
31768 if (args != NULL)
31769 release_tree_vector (args);
31770 return list;
31773 args_tree = build_tree_list_vec (args);
31775 release_tree_vector (args);
31777 for (t = args_tree; t; t = TREE_CHAIN (t))
31779 tree targ = TREE_VALUE (t);
31781 if (targ != error_mark_node)
31783 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31784 error ("%<wait%> expression must be integral");
31785 else
31787 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31789 mark_rvalue_use (targ);
31790 OMP_CLAUSE_DECL (c) = targ;
31791 OMP_CLAUSE_CHAIN (c) = list;
31792 list = c;
31797 return list;
31800 /* OpenACC:
31801 wait ( int-expr-list ) */
31803 static tree
31804 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31806 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31808 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31809 return list;
31811 list = cp_parser_oacc_wait_list (parser, location, list);
31813 return list;
31816 /* OpenMP 3.0:
31817 collapse ( constant-expression ) */
31819 static tree
31820 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31822 tree c, num;
31823 location_t loc;
31824 HOST_WIDE_INT n;
31826 loc = cp_lexer_peek_token (parser->lexer)->location;
31827 matching_parens parens;
31828 if (!parens.require_open (parser))
31829 return list;
31831 num = cp_parser_constant_expression (parser);
31833 if (!parens.require_close (parser))
31834 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31835 /*or_comma=*/false,
31836 /*consume_paren=*/true);
31838 if (num == error_mark_node)
31839 return list;
31840 num = fold_non_dependent_expr (num);
31841 if (!tree_fits_shwi_p (num)
31842 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31843 || (n = tree_to_shwi (num)) <= 0
31844 || (int) n != n)
31846 error_at (loc, "collapse argument needs positive constant integer expression");
31847 return list;
31850 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31851 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31852 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31853 OMP_CLAUSE_CHAIN (c) = list;
31854 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31856 return c;
31859 /* OpenMP 2.5:
31860 default ( none | shared )
31862 OpenACC:
31863 default ( none | present ) */
31865 static tree
31866 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31867 location_t location, bool is_oacc)
31869 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31870 tree c;
31872 matching_parens parens;
31873 if (!parens.require_open (parser))
31874 return list;
31875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31877 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31878 const char *p = IDENTIFIER_POINTER (id);
31880 switch (p[0])
31882 case 'n':
31883 if (strcmp ("none", p) != 0)
31884 goto invalid_kind;
31885 kind = OMP_CLAUSE_DEFAULT_NONE;
31886 break;
31888 case 'p':
31889 if (strcmp ("present", p) != 0 || !is_oacc)
31890 goto invalid_kind;
31891 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31892 break;
31894 case 's':
31895 if (strcmp ("shared", p) != 0 || is_oacc)
31896 goto invalid_kind;
31897 kind = OMP_CLAUSE_DEFAULT_SHARED;
31898 break;
31900 default:
31901 goto invalid_kind;
31904 cp_lexer_consume_token (parser->lexer);
31906 else
31908 invalid_kind:
31909 if (is_oacc)
31910 cp_parser_error (parser, "expected %<none%> or %<present%>");
31911 else
31912 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31915 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31916 || !parens.require_close (parser))
31917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31918 /*or_comma=*/false,
31919 /*consume_paren=*/true);
31921 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31922 return list;
31924 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31925 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31926 OMP_CLAUSE_CHAIN (c) = list;
31927 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31929 return c;
31932 /* OpenMP 3.1:
31933 final ( expression ) */
31935 static tree
31936 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31938 tree t, c;
31940 matching_parens parens;
31941 if (!parens.require_open (parser))
31942 return list;
31944 t = cp_parser_condition (parser);
31946 if (t == error_mark_node
31947 || !parens.require_close (parser))
31948 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31949 /*or_comma=*/false,
31950 /*consume_paren=*/true);
31952 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31954 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31955 OMP_CLAUSE_FINAL_EXPR (c) = t;
31956 OMP_CLAUSE_CHAIN (c) = list;
31958 return c;
31961 /* OpenMP 2.5:
31962 if ( expression )
31964 OpenMP 4.5:
31965 if ( directive-name-modifier : expression )
31967 directive-name-modifier:
31968 parallel | task | taskloop | target data | target | target update
31969 | target enter data | target exit data */
31971 static tree
31972 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31973 bool is_omp)
31975 tree t, c;
31976 enum tree_code if_modifier = ERROR_MARK;
31978 matching_parens parens;
31979 if (!parens.require_open (parser))
31980 return list;
31982 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31984 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31985 const char *p = IDENTIFIER_POINTER (id);
31986 int n = 2;
31988 if (strcmp ("parallel", p) == 0)
31989 if_modifier = OMP_PARALLEL;
31990 else if (strcmp ("task", p) == 0)
31991 if_modifier = OMP_TASK;
31992 else if (strcmp ("taskloop", p) == 0)
31993 if_modifier = OMP_TASKLOOP;
31994 else if (strcmp ("target", p) == 0)
31996 if_modifier = OMP_TARGET;
31997 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31999 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
32000 p = IDENTIFIER_POINTER (id);
32001 if (strcmp ("data", p) == 0)
32002 if_modifier = OMP_TARGET_DATA;
32003 else if (strcmp ("update", p) == 0)
32004 if_modifier = OMP_TARGET_UPDATE;
32005 else if (strcmp ("enter", p) == 0)
32006 if_modifier = OMP_TARGET_ENTER_DATA;
32007 else if (strcmp ("exit", p) == 0)
32008 if_modifier = OMP_TARGET_EXIT_DATA;
32009 if (if_modifier != OMP_TARGET)
32010 n = 3;
32011 else
32013 location_t loc
32014 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
32015 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
32016 "or %<exit%>");
32017 if_modifier = ERROR_MARK;
32019 if (if_modifier == OMP_TARGET_ENTER_DATA
32020 || if_modifier == OMP_TARGET_EXIT_DATA)
32022 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
32024 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
32025 p = IDENTIFIER_POINTER (id);
32026 if (strcmp ("data", p) == 0)
32027 n = 4;
32029 if (n != 4)
32031 location_t loc
32032 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
32033 error_at (loc, "expected %<data%>");
32034 if_modifier = ERROR_MARK;
32039 if (if_modifier != ERROR_MARK)
32041 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
32043 while (n-- > 0)
32044 cp_lexer_consume_token (parser->lexer);
32046 else
32048 if (n > 2)
32050 location_t loc
32051 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
32052 error_at (loc, "expected %<:%>");
32054 if_modifier = ERROR_MARK;
32059 t = cp_parser_condition (parser);
32061 if (t == error_mark_node
32062 || !parens.require_close (parser))
32063 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32064 /*or_comma=*/false,
32065 /*consume_paren=*/true);
32067 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
32068 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
32070 if (if_modifier != ERROR_MARK
32071 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32073 const char *p = NULL;
32074 switch (if_modifier)
32076 case OMP_PARALLEL: p = "parallel"; break;
32077 case OMP_TASK: p = "task"; break;
32078 case OMP_TASKLOOP: p = "taskloop"; break;
32079 case OMP_TARGET_DATA: p = "target data"; break;
32080 case OMP_TARGET: p = "target"; break;
32081 case OMP_TARGET_UPDATE: p = "target update"; break;
32082 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
32083 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
32084 default: gcc_unreachable ();
32086 error_at (location, "too many %<if%> clauses with %qs modifier",
32088 return list;
32090 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
32092 if (!is_omp)
32093 error_at (location, "too many %<if%> clauses");
32094 else
32095 error_at (location, "too many %<if%> clauses without modifier");
32096 return list;
32098 else if (if_modifier == ERROR_MARK
32099 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
32101 error_at (location, "if any %<if%> clause has modifier, then all "
32102 "%<if%> clauses have to use modifier");
32103 return list;
32107 c = build_omp_clause (location, OMP_CLAUSE_IF);
32108 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
32109 OMP_CLAUSE_IF_EXPR (c) = t;
32110 OMP_CLAUSE_CHAIN (c) = list;
32112 return c;
32115 /* OpenMP 3.1:
32116 mergeable */
32118 static tree
32119 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
32120 tree list, location_t location)
32122 tree c;
32124 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
32125 location);
32127 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
32128 OMP_CLAUSE_CHAIN (c) = list;
32129 return c;
32132 /* OpenMP 2.5:
32133 nowait */
32135 static tree
32136 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
32137 tree list, location_t location)
32139 tree c;
32141 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
32143 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
32144 OMP_CLAUSE_CHAIN (c) = list;
32145 return c;
32148 /* OpenMP 2.5:
32149 num_threads ( expression ) */
32151 static tree
32152 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
32153 location_t location)
32155 tree t, c;
32157 matching_parens parens;
32158 if (!parens.require_open (parser))
32159 return list;
32161 t = cp_parser_expression (parser);
32163 if (t == error_mark_node
32164 || !parens.require_close (parser))
32165 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32166 /*or_comma=*/false,
32167 /*consume_paren=*/true);
32169 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
32170 "num_threads", location);
32172 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
32173 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
32174 OMP_CLAUSE_CHAIN (c) = list;
32176 return c;
32179 /* OpenMP 4.5:
32180 num_tasks ( expression ) */
32182 static tree
32183 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
32184 location_t location)
32186 tree t, c;
32188 matching_parens parens;
32189 if (!parens.require_open (parser))
32190 return list;
32192 t = cp_parser_expression (parser);
32194 if (t == error_mark_node
32195 || !parens.require_close (parser))
32196 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32197 /*or_comma=*/false,
32198 /*consume_paren=*/true);
32200 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
32201 "num_tasks", location);
32203 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
32204 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
32205 OMP_CLAUSE_CHAIN (c) = list;
32207 return c;
32210 /* OpenMP 4.5:
32211 grainsize ( expression ) */
32213 static tree
32214 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
32215 location_t location)
32217 tree t, c;
32219 matching_parens parens;
32220 if (!parens.require_open (parser))
32221 return list;
32223 t = cp_parser_expression (parser);
32225 if (t == error_mark_node
32226 || !parens.require_close (parser))
32227 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32228 /*or_comma=*/false,
32229 /*consume_paren=*/true);
32231 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32232 "grainsize", location);
32234 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32235 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32236 OMP_CLAUSE_CHAIN (c) = list;
32238 return c;
32241 /* OpenMP 4.5:
32242 priority ( expression ) */
32244 static tree
32245 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32246 location_t location)
32248 tree t, c;
32250 matching_parens parens;
32251 if (!parens.require_open (parser))
32252 return list;
32254 t = cp_parser_expression (parser);
32256 if (t == error_mark_node
32257 || !parens.require_close (parser))
32258 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32259 /*or_comma=*/false,
32260 /*consume_paren=*/true);
32262 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32263 "priority", location);
32265 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32266 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32267 OMP_CLAUSE_CHAIN (c) = list;
32269 return c;
32272 /* OpenMP 4.5:
32273 hint ( expression ) */
32275 static tree
32276 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32277 location_t location)
32279 tree t, c;
32281 matching_parens parens;
32282 if (!parens.require_open (parser))
32283 return list;
32285 t = cp_parser_expression (parser);
32287 if (t == error_mark_node
32288 || !parens.require_close (parser))
32289 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32290 /*or_comma=*/false,
32291 /*consume_paren=*/true);
32293 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32295 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32296 OMP_CLAUSE_HINT_EXPR (c) = t;
32297 OMP_CLAUSE_CHAIN (c) = list;
32299 return c;
32302 /* OpenMP 4.5:
32303 defaultmap ( tofrom : scalar ) */
32305 static tree
32306 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32307 location_t location)
32309 tree c, id;
32310 const char *p;
32312 matching_parens parens;
32313 if (!parens.require_open (parser))
32314 return list;
32316 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32318 cp_parser_error (parser, "expected %<tofrom%>");
32319 goto out_err;
32321 id = cp_lexer_peek_token (parser->lexer)->u.value;
32322 p = IDENTIFIER_POINTER (id);
32323 if (strcmp (p, "tofrom") != 0)
32325 cp_parser_error (parser, "expected %<tofrom%>");
32326 goto out_err;
32328 cp_lexer_consume_token (parser->lexer);
32329 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32330 goto out_err;
32332 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32334 cp_parser_error (parser, "expected %<scalar%>");
32335 goto out_err;
32337 id = cp_lexer_peek_token (parser->lexer)->u.value;
32338 p = IDENTIFIER_POINTER (id);
32339 if (strcmp (p, "scalar") != 0)
32341 cp_parser_error (parser, "expected %<scalar%>");
32342 goto out_err;
32344 cp_lexer_consume_token (parser->lexer);
32345 if (!parens.require_close (parser))
32346 goto out_err;
32348 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32349 location);
32351 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32352 OMP_CLAUSE_CHAIN (c) = list;
32353 return c;
32355 out_err:
32356 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32357 /*or_comma=*/false,
32358 /*consume_paren=*/true);
32359 return list;
32362 /* OpenMP 2.5:
32363 ordered
32365 OpenMP 4.5:
32366 ordered ( constant-expression ) */
32368 static tree
32369 cp_parser_omp_clause_ordered (cp_parser *parser,
32370 tree list, location_t location)
32372 tree c, num = NULL_TREE;
32373 HOST_WIDE_INT n;
32375 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32376 "ordered", location);
32378 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32380 matching_parens parens;
32381 parens.consume_open (parser);
32383 num = cp_parser_constant_expression (parser);
32385 if (!parens.require_close (parser))
32386 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32387 /*or_comma=*/false,
32388 /*consume_paren=*/true);
32390 if (num == error_mark_node)
32391 return list;
32392 num = fold_non_dependent_expr (num);
32393 if (!tree_fits_shwi_p (num)
32394 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32395 || (n = tree_to_shwi (num)) <= 0
32396 || (int) n != n)
32398 error_at (location,
32399 "ordered argument needs positive constant integer "
32400 "expression");
32401 return list;
32405 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32406 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32407 OMP_CLAUSE_CHAIN (c) = list;
32408 return c;
32411 /* OpenMP 2.5:
32412 reduction ( reduction-operator : variable-list )
32414 reduction-operator:
32415 One of: + * - & ^ | && ||
32417 OpenMP 3.1:
32419 reduction-operator:
32420 One of: + * - & ^ | && || min max
32422 OpenMP 4.0:
32424 reduction-operator:
32425 One of: + * - & ^ | && ||
32426 id-expression */
32428 static tree
32429 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32431 enum tree_code code = ERROR_MARK;
32432 tree nlist, c, id = NULL_TREE;
32434 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32435 return list;
32437 switch (cp_lexer_peek_token (parser->lexer)->type)
32439 case CPP_PLUS: code = PLUS_EXPR; break;
32440 case CPP_MULT: code = MULT_EXPR; break;
32441 case CPP_MINUS: code = MINUS_EXPR; break;
32442 case CPP_AND: code = BIT_AND_EXPR; break;
32443 case CPP_XOR: code = BIT_XOR_EXPR; break;
32444 case CPP_OR: code = BIT_IOR_EXPR; break;
32445 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32446 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32447 default: break;
32450 if (code != ERROR_MARK)
32451 cp_lexer_consume_token (parser->lexer);
32452 else
32454 bool saved_colon_corrects_to_scope_p;
32455 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32456 parser->colon_corrects_to_scope_p = false;
32457 id = cp_parser_id_expression (parser, /*template_p=*/false,
32458 /*check_dependency_p=*/true,
32459 /*template_p=*/NULL,
32460 /*declarator_p=*/false,
32461 /*optional_p=*/false);
32462 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32463 if (identifier_p (id))
32465 const char *p = IDENTIFIER_POINTER (id);
32467 if (strcmp (p, "min") == 0)
32468 code = MIN_EXPR;
32469 else if (strcmp (p, "max") == 0)
32470 code = MAX_EXPR;
32471 else if (id == cp_operator_id (PLUS_EXPR))
32472 code = PLUS_EXPR;
32473 else if (id == cp_operator_id (MULT_EXPR))
32474 code = MULT_EXPR;
32475 else if (id == cp_operator_id (MINUS_EXPR))
32476 code = MINUS_EXPR;
32477 else if (id == cp_operator_id (BIT_AND_EXPR))
32478 code = BIT_AND_EXPR;
32479 else if (id == cp_operator_id (BIT_IOR_EXPR))
32480 code = BIT_IOR_EXPR;
32481 else if (id == cp_operator_id (BIT_XOR_EXPR))
32482 code = BIT_XOR_EXPR;
32483 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32484 code = TRUTH_ANDIF_EXPR;
32485 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32486 code = TRUTH_ORIF_EXPR;
32487 id = omp_reduction_id (code, id, NULL_TREE);
32488 tree scope = parser->scope;
32489 if (scope)
32490 id = build_qualified_name (NULL_TREE, scope, id, false);
32491 parser->scope = NULL_TREE;
32492 parser->qualifying_scope = NULL_TREE;
32493 parser->object_scope = NULL_TREE;
32495 else
32497 error ("invalid reduction-identifier");
32498 resync_fail:
32499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32500 /*or_comma=*/false,
32501 /*consume_paren=*/true);
32502 return list;
32506 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32507 goto resync_fail;
32509 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32510 NULL);
32511 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32513 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32514 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32517 return nlist;
32520 /* OpenMP 2.5:
32521 schedule ( schedule-kind )
32522 schedule ( schedule-kind , expression )
32524 schedule-kind:
32525 static | dynamic | guided | runtime | auto
32527 OpenMP 4.5:
32528 schedule ( schedule-modifier : schedule-kind )
32529 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32531 schedule-modifier:
32532 simd
32533 monotonic
32534 nonmonotonic */
32536 static tree
32537 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32539 tree c, t;
32540 int modifiers = 0, nmodifiers = 0;
32542 matching_parens parens;
32543 if (!parens.require_open (parser))
32544 return list;
32546 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32548 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32550 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32551 const char *p = IDENTIFIER_POINTER (id);
32552 if (strcmp ("simd", p) == 0)
32553 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32554 else if (strcmp ("monotonic", p) == 0)
32555 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32556 else if (strcmp ("nonmonotonic", p) == 0)
32557 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32558 else
32559 break;
32560 cp_lexer_consume_token (parser->lexer);
32561 if (nmodifiers++ == 0
32562 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32563 cp_lexer_consume_token (parser->lexer);
32564 else
32566 cp_parser_require (parser, CPP_COLON, RT_COLON);
32567 break;
32571 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32574 const char *p = IDENTIFIER_POINTER (id);
32576 switch (p[0])
32578 case 'd':
32579 if (strcmp ("dynamic", p) != 0)
32580 goto invalid_kind;
32581 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32582 break;
32584 case 'g':
32585 if (strcmp ("guided", p) != 0)
32586 goto invalid_kind;
32587 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32588 break;
32590 case 'r':
32591 if (strcmp ("runtime", p) != 0)
32592 goto invalid_kind;
32593 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32594 break;
32596 default:
32597 goto invalid_kind;
32600 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32601 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32602 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32603 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32604 else
32605 goto invalid_kind;
32606 cp_lexer_consume_token (parser->lexer);
32608 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32609 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32610 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32611 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32613 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32614 "specified");
32615 modifiers = 0;
32618 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32620 cp_token *token;
32621 cp_lexer_consume_token (parser->lexer);
32623 token = cp_lexer_peek_token (parser->lexer);
32624 t = cp_parser_assignment_expression (parser);
32626 if (t == error_mark_node)
32627 goto resync_fail;
32628 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32629 error_at (token->location, "schedule %<runtime%> does not take "
32630 "a %<chunk_size%> parameter");
32631 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32632 error_at (token->location, "schedule %<auto%> does not take "
32633 "a %<chunk_size%> parameter");
32634 else
32635 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32637 if (!parens.require_close (parser))
32638 goto resync_fail;
32640 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32641 goto resync_fail;
32643 OMP_CLAUSE_SCHEDULE_KIND (c)
32644 = (enum omp_clause_schedule_kind)
32645 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32647 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32648 OMP_CLAUSE_CHAIN (c) = list;
32649 return c;
32651 invalid_kind:
32652 cp_parser_error (parser, "invalid schedule kind");
32653 resync_fail:
32654 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32655 /*or_comma=*/false,
32656 /*consume_paren=*/true);
32657 return list;
32660 /* OpenMP 3.0:
32661 untied */
32663 static tree
32664 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32665 tree list, location_t location)
32667 tree c;
32669 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32671 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32672 OMP_CLAUSE_CHAIN (c) = list;
32673 return c;
32676 /* OpenMP 4.0:
32677 inbranch
32678 notinbranch */
32680 static tree
32681 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32682 tree list, location_t location)
32684 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32685 tree c = build_omp_clause (location, code);
32686 OMP_CLAUSE_CHAIN (c) = list;
32687 return c;
32690 /* OpenMP 4.0:
32691 parallel
32693 sections
32694 taskgroup */
32696 static tree
32697 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32698 enum omp_clause_code code,
32699 tree list, location_t location)
32701 tree c = build_omp_clause (location, code);
32702 OMP_CLAUSE_CHAIN (c) = list;
32703 return c;
32706 /* OpenMP 4.5:
32707 nogroup */
32709 static tree
32710 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32711 tree list, location_t location)
32713 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32714 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32715 OMP_CLAUSE_CHAIN (c) = list;
32716 return c;
32719 /* OpenMP 4.5:
32720 simd
32721 threads */
32723 static tree
32724 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32725 enum omp_clause_code code,
32726 tree list, location_t location)
32728 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32729 tree c = build_omp_clause (location, code);
32730 OMP_CLAUSE_CHAIN (c) = list;
32731 return c;
32734 /* OpenMP 4.0:
32735 num_teams ( expression ) */
32737 static tree
32738 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32739 location_t location)
32741 tree t, c;
32743 matching_parens parens;
32744 if (!parens.require_open (parser))
32745 return list;
32747 t = cp_parser_expression (parser);
32749 if (t == error_mark_node
32750 || !parens.require_close (parser))
32751 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32752 /*or_comma=*/false,
32753 /*consume_paren=*/true);
32755 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32756 "num_teams", location);
32758 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32759 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32760 OMP_CLAUSE_CHAIN (c) = list;
32762 return c;
32765 /* OpenMP 4.0:
32766 thread_limit ( expression ) */
32768 static tree
32769 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32770 location_t location)
32772 tree t, c;
32774 matching_parens parens;
32775 if (!parens.require_open (parser))
32776 return list;
32778 t = cp_parser_expression (parser);
32780 if (t == error_mark_node
32781 || !parens.require_close (parser))
32782 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32783 /*or_comma=*/false,
32784 /*consume_paren=*/true);
32786 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32787 "thread_limit", location);
32789 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32790 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32791 OMP_CLAUSE_CHAIN (c) = list;
32793 return c;
32796 /* OpenMP 4.0:
32797 aligned ( variable-list )
32798 aligned ( variable-list : constant-expression ) */
32800 static tree
32801 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32803 tree nlist, c, alignment = NULL_TREE;
32804 bool colon;
32806 matching_parens parens;
32807 if (!parens.require_open (parser))
32808 return list;
32810 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32811 &colon);
32813 if (colon)
32815 alignment = cp_parser_constant_expression (parser);
32817 if (!parens.require_close (parser))
32818 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32819 /*or_comma=*/false,
32820 /*consume_paren=*/true);
32822 if (alignment == error_mark_node)
32823 alignment = NULL_TREE;
32826 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32827 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32829 return nlist;
32832 /* OpenMP 4.0:
32833 linear ( variable-list )
32834 linear ( variable-list : expression )
32836 OpenMP 4.5:
32837 linear ( modifier ( variable-list ) )
32838 linear ( modifier ( variable-list ) : expression ) */
32840 static tree
32841 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32842 bool is_cilk_simd_fn, bool declare_simd)
32844 tree nlist, c, step = integer_one_node;
32845 bool colon;
32846 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32848 matching_parens parens;
32849 if (!parens.require_open (parser))
32850 return list;
32852 if (!is_cilk_simd_fn
32853 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32855 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32856 const char *p = IDENTIFIER_POINTER (id);
32858 if (strcmp ("ref", p) == 0)
32859 kind = OMP_CLAUSE_LINEAR_REF;
32860 else if (strcmp ("val", p) == 0)
32861 kind = OMP_CLAUSE_LINEAR_VAL;
32862 else if (strcmp ("uval", p) == 0)
32863 kind = OMP_CLAUSE_LINEAR_UVAL;
32864 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32865 cp_lexer_consume_token (parser->lexer);
32866 else
32867 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32870 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32871 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32872 &colon);
32873 else
32875 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32876 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32877 if (colon)
32878 cp_parser_require (parser, CPP_COLON, RT_COLON);
32879 else if (!parens.require_close (parser))
32880 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32881 /*or_comma=*/false,
32882 /*consume_paren=*/true);
32885 if (colon)
32887 step = NULL_TREE;
32888 if (declare_simd
32889 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32890 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32892 cp_token *token = cp_lexer_peek_token (parser->lexer);
32893 cp_parser_parse_tentatively (parser);
32894 step = cp_parser_id_expression (parser, /*template_p=*/false,
32895 /*check_dependency_p=*/true,
32896 /*template_p=*/NULL,
32897 /*declarator_p=*/false,
32898 /*optional_p=*/false);
32899 if (step != error_mark_node)
32900 step = cp_parser_lookup_name_simple (parser, step, token->location);
32901 if (step == error_mark_node)
32903 step = NULL_TREE;
32904 cp_parser_abort_tentative_parse (parser);
32906 else if (!cp_parser_parse_definitely (parser))
32907 step = NULL_TREE;
32909 if (!step)
32910 step = cp_parser_expression (parser);
32912 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32914 sorry ("using parameters for %<linear%> step is not supported yet");
32915 step = integer_one_node;
32917 if (!parens.require_close (parser))
32918 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32919 /*or_comma=*/false,
32920 /*consume_paren=*/true);
32922 if (step == error_mark_node)
32923 return list;
32926 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32928 OMP_CLAUSE_LINEAR_STEP (c) = step;
32929 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32932 return nlist;
32935 /* OpenMP 4.0:
32936 safelen ( constant-expression ) */
32938 static tree
32939 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32940 location_t location)
32942 tree t, c;
32944 matching_parens parens;
32945 if (!parens.require_open (parser))
32946 return list;
32948 t = cp_parser_constant_expression (parser);
32950 if (t == error_mark_node
32951 || !parens.require_close (parser))
32952 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32953 /*or_comma=*/false,
32954 /*consume_paren=*/true);
32956 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32958 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32959 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32960 OMP_CLAUSE_CHAIN (c) = list;
32962 return c;
32965 /* OpenMP 4.0:
32966 simdlen ( constant-expression ) */
32968 static tree
32969 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32970 location_t location)
32972 tree t, c;
32974 matching_parens parens;
32975 if (!parens.require_open (parser))
32976 return list;
32978 t = cp_parser_constant_expression (parser);
32980 if (t == error_mark_node
32981 || !parens.require_close (parser))
32982 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32983 /*or_comma=*/false,
32984 /*consume_paren=*/true);
32986 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32988 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32989 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32990 OMP_CLAUSE_CHAIN (c) = list;
32992 return c;
32995 /* OpenMP 4.5:
32996 vec:
32997 identifier [+/- integer]
32998 vec , identifier [+/- integer]
33001 static tree
33002 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
33003 tree list)
33005 tree vec = NULL;
33007 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
33009 cp_parser_error (parser, "expected identifier");
33010 return list;
33013 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33015 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
33016 tree t, identifier = cp_parser_identifier (parser);
33017 tree addend = NULL;
33019 if (identifier == error_mark_node)
33020 t = error_mark_node;
33021 else
33023 t = cp_parser_lookup_name_simple
33024 (parser, identifier,
33025 cp_lexer_peek_token (parser->lexer)->location);
33026 if (t == error_mark_node)
33027 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
33028 id_loc);
33031 bool neg = false;
33032 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
33033 neg = true;
33034 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
33036 addend = integer_zero_node;
33037 goto add_to_vector;
33039 cp_lexer_consume_token (parser->lexer);
33041 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
33043 cp_parser_error (parser, "expected integer");
33044 return list;
33047 addend = cp_lexer_peek_token (parser->lexer)->u.value;
33048 if (TREE_CODE (addend) != INTEGER_CST)
33050 cp_parser_error (parser, "expected integer");
33051 return list;
33053 cp_lexer_consume_token (parser->lexer);
33055 add_to_vector:
33056 if (t != error_mark_node)
33058 vec = tree_cons (addend, t, vec);
33059 if (neg)
33060 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
33063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
33064 break;
33066 cp_lexer_consume_token (parser->lexer);
33069 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
33071 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
33072 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
33073 OMP_CLAUSE_DECL (u) = nreverse (vec);
33074 OMP_CLAUSE_CHAIN (u) = list;
33075 return u;
33077 return list;
33080 /* OpenMP 4.0:
33081 depend ( depend-kind : variable-list )
33083 depend-kind:
33084 in | out | inout
33086 OpenMP 4.5:
33087 depend ( source )
33089 depend ( sink : vec ) */
33091 static tree
33092 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
33094 tree nlist, c;
33095 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
33097 matching_parens parens;
33098 if (!parens.require_open (parser))
33099 return list;
33101 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33103 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33104 const char *p = IDENTIFIER_POINTER (id);
33106 if (strcmp ("in", p) == 0)
33107 kind = OMP_CLAUSE_DEPEND_IN;
33108 else if (strcmp ("inout", p) == 0)
33109 kind = OMP_CLAUSE_DEPEND_INOUT;
33110 else if (strcmp ("out", p) == 0)
33111 kind = OMP_CLAUSE_DEPEND_OUT;
33112 else if (strcmp ("source", p) == 0)
33113 kind = OMP_CLAUSE_DEPEND_SOURCE;
33114 else if (strcmp ("sink", p) == 0)
33115 kind = OMP_CLAUSE_DEPEND_SINK;
33116 else
33117 goto invalid_kind;
33119 else
33120 goto invalid_kind;
33122 cp_lexer_consume_token (parser->lexer);
33124 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
33126 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
33127 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33128 OMP_CLAUSE_DECL (c) = NULL_TREE;
33129 OMP_CLAUSE_CHAIN (c) = list;
33130 if (!parens.require_close (parser))
33131 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33132 /*or_comma=*/false,
33133 /*consume_paren=*/true);
33134 return c;
33137 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
33138 goto resync_fail;
33140 if (kind == OMP_CLAUSE_DEPEND_SINK)
33141 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
33142 else
33144 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
33145 list, NULL);
33147 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33148 OMP_CLAUSE_DEPEND_KIND (c) = kind;
33150 return nlist;
33152 invalid_kind:
33153 cp_parser_error (parser, "invalid depend kind");
33154 resync_fail:
33155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33156 /*or_comma=*/false,
33157 /*consume_paren=*/true);
33158 return list;
33161 /* OpenMP 4.0:
33162 map ( map-kind : variable-list )
33163 map ( variable-list )
33165 map-kind:
33166 alloc | to | from | tofrom
33168 OpenMP 4.5:
33169 map-kind:
33170 alloc | to | from | tofrom | release | delete
33172 map ( always [,] map-kind: variable-list ) */
33174 static tree
33175 cp_parser_omp_clause_map (cp_parser *parser, tree list)
33177 tree nlist, c;
33178 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
33179 bool always = false;
33181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33182 return list;
33184 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33186 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33187 const char *p = IDENTIFIER_POINTER (id);
33189 if (strcmp ("always", p) == 0)
33191 int nth = 2;
33192 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
33193 nth++;
33194 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
33195 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
33196 == RID_DELETE))
33197 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
33198 == CPP_COLON))
33200 always = true;
33201 cp_lexer_consume_token (parser->lexer);
33202 if (nth == 3)
33203 cp_lexer_consume_token (parser->lexer);
33208 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
33209 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33211 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33212 const char *p = IDENTIFIER_POINTER (id);
33214 if (strcmp ("alloc", p) == 0)
33215 kind = GOMP_MAP_ALLOC;
33216 else if (strcmp ("to", p) == 0)
33217 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
33218 else if (strcmp ("from", p) == 0)
33219 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
33220 else if (strcmp ("tofrom", p) == 0)
33221 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
33222 else if (strcmp ("release", p) == 0)
33223 kind = GOMP_MAP_RELEASE;
33224 else
33226 cp_parser_error (parser, "invalid map kind");
33227 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33228 /*or_comma=*/false,
33229 /*consume_paren=*/true);
33230 return list;
33232 cp_lexer_consume_token (parser->lexer);
33233 cp_lexer_consume_token (parser->lexer);
33235 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33236 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33238 kind = GOMP_MAP_DELETE;
33239 cp_lexer_consume_token (parser->lexer);
33240 cp_lexer_consume_token (parser->lexer);
33243 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33244 NULL);
33246 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33247 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33249 return nlist;
33252 /* OpenMP 4.0:
33253 device ( expression ) */
33255 static tree
33256 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33257 location_t location)
33259 tree t, c;
33261 matching_parens parens;
33262 if (!parens.require_open (parser))
33263 return list;
33265 t = cp_parser_expression (parser);
33267 if (t == error_mark_node
33268 || !parens.require_close (parser))
33269 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33270 /*or_comma=*/false,
33271 /*consume_paren=*/true);
33273 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33274 "device", location);
33276 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33277 OMP_CLAUSE_DEVICE_ID (c) = t;
33278 OMP_CLAUSE_CHAIN (c) = list;
33280 return c;
33283 /* OpenMP 4.0:
33284 dist_schedule ( static )
33285 dist_schedule ( static , expression ) */
33287 static tree
33288 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33289 location_t location)
33291 tree c, t;
33293 matching_parens parens;
33294 if (!parens.require_open (parser))
33295 return list;
33297 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33299 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33300 goto invalid_kind;
33301 cp_lexer_consume_token (parser->lexer);
33303 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33305 cp_lexer_consume_token (parser->lexer);
33307 t = cp_parser_assignment_expression (parser);
33309 if (t == error_mark_node)
33310 goto resync_fail;
33311 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33313 if (!parens.require_close (parser))
33314 goto resync_fail;
33316 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33317 goto resync_fail;
33319 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33320 location);
33321 OMP_CLAUSE_CHAIN (c) = list;
33322 return c;
33324 invalid_kind:
33325 cp_parser_error (parser, "invalid dist_schedule kind");
33326 resync_fail:
33327 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33328 /*or_comma=*/false,
33329 /*consume_paren=*/true);
33330 return list;
33333 /* OpenMP 4.0:
33334 proc_bind ( proc-bind-kind )
33336 proc-bind-kind:
33337 master | close | spread */
33339 static tree
33340 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33341 location_t location)
33343 tree c;
33344 enum omp_clause_proc_bind_kind kind;
33346 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33347 return list;
33349 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33351 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33352 const char *p = IDENTIFIER_POINTER (id);
33354 if (strcmp ("master", p) == 0)
33355 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33356 else if (strcmp ("close", p) == 0)
33357 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33358 else if (strcmp ("spread", p) == 0)
33359 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33360 else
33361 goto invalid_kind;
33363 else
33364 goto invalid_kind;
33366 cp_lexer_consume_token (parser->lexer);
33367 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33368 goto resync_fail;
33370 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33371 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33372 location);
33373 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33374 OMP_CLAUSE_CHAIN (c) = list;
33375 return c;
33377 invalid_kind:
33378 cp_parser_error (parser, "invalid depend kind");
33379 resync_fail:
33380 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33381 /*or_comma=*/false,
33382 /*consume_paren=*/true);
33383 return list;
33386 /* OpenACC:
33387 async [( int-expr )] */
33389 static tree
33390 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33392 tree c, t;
33393 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33395 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33397 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33399 matching_parens parens;
33400 parens.consume_open (parser);
33402 t = cp_parser_expression (parser);
33403 if (t == error_mark_node
33404 || !parens.require_close (parser))
33405 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33406 /*or_comma=*/false,
33407 /*consume_paren=*/true);
33410 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33412 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33413 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33414 OMP_CLAUSE_CHAIN (c) = list;
33415 list = c;
33417 return list;
33420 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33421 is a bitmask in MASK. Return the list of clauses found. */
33423 static tree
33424 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33425 const char *where, cp_token *pragma_tok,
33426 bool finish_p = true)
33428 tree clauses = NULL;
33429 bool first = true;
33431 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33433 location_t here;
33434 pragma_omp_clause c_kind;
33435 omp_clause_code code;
33436 const char *c_name;
33437 tree prev = clauses;
33439 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33440 cp_lexer_consume_token (parser->lexer);
33442 here = cp_lexer_peek_token (parser->lexer)->location;
33443 c_kind = cp_parser_omp_clause_name (parser);
33445 switch (c_kind)
33447 case PRAGMA_OACC_CLAUSE_ASYNC:
33448 clauses = cp_parser_oacc_clause_async (parser, clauses);
33449 c_name = "async";
33450 break;
33451 case PRAGMA_OACC_CLAUSE_AUTO:
33452 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33453 clauses, here);
33454 c_name = "auto";
33455 break;
33456 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33457 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33458 c_name = "collapse";
33459 break;
33460 case PRAGMA_OACC_CLAUSE_COPY:
33461 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33462 c_name = "copy";
33463 break;
33464 case PRAGMA_OACC_CLAUSE_COPYIN:
33465 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33466 c_name = "copyin";
33467 break;
33468 case PRAGMA_OACC_CLAUSE_COPYOUT:
33469 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33470 c_name = "copyout";
33471 break;
33472 case PRAGMA_OACC_CLAUSE_CREATE:
33473 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33474 c_name = "create";
33475 break;
33476 case PRAGMA_OACC_CLAUSE_DELETE:
33477 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33478 c_name = "delete";
33479 break;
33480 case PRAGMA_OMP_CLAUSE_DEFAULT:
33481 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33482 c_name = "default";
33483 break;
33484 case PRAGMA_OACC_CLAUSE_DEVICE:
33485 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33486 c_name = "device";
33487 break;
33488 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33489 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33490 c_name = "deviceptr";
33491 break;
33492 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33493 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33494 c_name = "device_resident";
33495 break;
33496 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33497 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33498 clauses);
33499 c_name = "firstprivate";
33500 break;
33501 case PRAGMA_OACC_CLAUSE_GANG:
33502 c_name = "gang";
33503 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33504 c_name, clauses);
33505 break;
33506 case PRAGMA_OACC_CLAUSE_HOST:
33507 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33508 c_name = "host";
33509 break;
33510 case PRAGMA_OACC_CLAUSE_IF:
33511 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33512 c_name = "if";
33513 break;
33514 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33515 clauses = cp_parser_oacc_simple_clause (parser,
33516 OMP_CLAUSE_INDEPENDENT,
33517 clauses, here);
33518 c_name = "independent";
33519 break;
33520 case PRAGMA_OACC_CLAUSE_LINK:
33521 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33522 c_name = "link";
33523 break;
33524 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33525 code = OMP_CLAUSE_NUM_GANGS;
33526 c_name = "num_gangs";
33527 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33528 clauses);
33529 break;
33530 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33531 c_name = "num_workers";
33532 code = OMP_CLAUSE_NUM_WORKERS;
33533 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33534 clauses);
33535 break;
33536 case PRAGMA_OACC_CLAUSE_PRESENT:
33537 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33538 c_name = "present";
33539 break;
33540 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33541 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33542 c_name = "present_or_copy";
33543 break;
33544 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33545 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33546 c_name = "present_or_copyin";
33547 break;
33548 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33549 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33550 c_name = "present_or_copyout";
33551 break;
33552 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33553 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33554 c_name = "present_or_create";
33555 break;
33556 case PRAGMA_OACC_CLAUSE_PRIVATE:
33557 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33558 clauses);
33559 c_name = "private";
33560 break;
33561 case PRAGMA_OACC_CLAUSE_REDUCTION:
33562 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33563 c_name = "reduction";
33564 break;
33565 case PRAGMA_OACC_CLAUSE_SELF:
33566 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33567 c_name = "self";
33568 break;
33569 case PRAGMA_OACC_CLAUSE_SEQ:
33570 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33571 clauses, here);
33572 c_name = "seq";
33573 break;
33574 case PRAGMA_OACC_CLAUSE_TILE:
33575 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33576 c_name = "tile";
33577 break;
33578 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33579 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33580 clauses);
33581 c_name = "use_device";
33582 break;
33583 case PRAGMA_OACC_CLAUSE_VECTOR:
33584 c_name = "vector";
33585 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33586 c_name, clauses);
33587 break;
33588 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33589 c_name = "vector_length";
33590 code = OMP_CLAUSE_VECTOR_LENGTH;
33591 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33592 clauses);
33593 break;
33594 case PRAGMA_OACC_CLAUSE_WAIT:
33595 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33596 c_name = "wait";
33597 break;
33598 case PRAGMA_OACC_CLAUSE_WORKER:
33599 c_name = "worker";
33600 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33601 c_name, clauses);
33602 break;
33603 default:
33604 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33605 goto saw_error;
33608 first = false;
33610 if (((mask >> c_kind) & 1) == 0)
33612 /* Remove the invalid clause(s) from the list to avoid
33613 confusing the rest of the compiler. */
33614 clauses = prev;
33615 error_at (here, "%qs is not valid for %qs", c_name, where);
33619 saw_error:
33620 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33622 if (finish_p)
33623 return finish_omp_clauses (clauses, C_ORT_ACC);
33625 return clauses;
33628 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33629 is a bitmask in MASK. Return the list of clauses found; the result
33630 of clause default goes in *pdefault. */
33632 static tree
33633 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33634 const char *where, cp_token *pragma_tok,
33635 bool finish_p = true)
33637 tree clauses = NULL;
33638 bool first = true;
33639 cp_token *token = NULL;
33641 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33643 pragma_omp_clause c_kind;
33644 const char *c_name;
33645 tree prev = clauses;
33647 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33648 cp_lexer_consume_token (parser->lexer);
33650 token = cp_lexer_peek_token (parser->lexer);
33651 c_kind = cp_parser_omp_clause_name (parser);
33653 switch (c_kind)
33655 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33656 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33657 token->location);
33658 c_name = "collapse";
33659 break;
33660 case PRAGMA_OMP_CLAUSE_COPYIN:
33661 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33662 c_name = "copyin";
33663 break;
33664 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33665 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33666 clauses);
33667 c_name = "copyprivate";
33668 break;
33669 case PRAGMA_OMP_CLAUSE_DEFAULT:
33670 clauses = cp_parser_omp_clause_default (parser, clauses,
33671 token->location, false);
33672 c_name = "default";
33673 break;
33674 case PRAGMA_OMP_CLAUSE_FINAL:
33675 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33676 c_name = "final";
33677 break;
33678 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33679 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33680 clauses);
33681 c_name = "firstprivate";
33682 break;
33683 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33684 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33685 token->location);
33686 c_name = "grainsize";
33687 break;
33688 case PRAGMA_OMP_CLAUSE_HINT:
33689 clauses = cp_parser_omp_clause_hint (parser, clauses,
33690 token->location);
33691 c_name = "hint";
33692 break;
33693 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33694 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33695 token->location);
33696 c_name = "defaultmap";
33697 break;
33698 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33699 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33700 clauses);
33701 c_name = "use_device_ptr";
33702 break;
33703 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33704 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33705 clauses);
33706 c_name = "is_device_ptr";
33707 break;
33708 case PRAGMA_OMP_CLAUSE_IF:
33709 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33710 true);
33711 c_name = "if";
33712 break;
33713 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33714 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33715 clauses);
33716 c_name = "lastprivate";
33717 break;
33718 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33719 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33720 token->location);
33721 c_name = "mergeable";
33722 break;
33723 case PRAGMA_OMP_CLAUSE_NOWAIT:
33724 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33725 c_name = "nowait";
33726 break;
33727 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33728 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33729 token->location);
33730 c_name = "num_tasks";
33731 break;
33732 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33733 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33734 token->location);
33735 c_name = "num_threads";
33736 break;
33737 case PRAGMA_OMP_CLAUSE_ORDERED:
33738 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33739 token->location);
33740 c_name = "ordered";
33741 break;
33742 case PRAGMA_OMP_CLAUSE_PRIORITY:
33743 clauses = cp_parser_omp_clause_priority (parser, clauses,
33744 token->location);
33745 c_name = "priority";
33746 break;
33747 case PRAGMA_OMP_CLAUSE_PRIVATE:
33748 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33749 clauses);
33750 c_name = "private";
33751 break;
33752 case PRAGMA_OMP_CLAUSE_REDUCTION:
33753 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33754 c_name = "reduction";
33755 break;
33756 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33757 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33758 token->location);
33759 c_name = "schedule";
33760 break;
33761 case PRAGMA_OMP_CLAUSE_SHARED:
33762 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33763 clauses);
33764 c_name = "shared";
33765 break;
33766 case PRAGMA_OMP_CLAUSE_UNTIED:
33767 clauses = cp_parser_omp_clause_untied (parser, clauses,
33768 token->location);
33769 c_name = "untied";
33770 break;
33771 case PRAGMA_OMP_CLAUSE_INBRANCH:
33772 case PRAGMA_CILK_CLAUSE_MASK:
33773 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33774 clauses, token->location);
33775 c_name = "inbranch";
33776 break;
33777 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33778 case PRAGMA_CILK_CLAUSE_NOMASK:
33779 clauses = cp_parser_omp_clause_branch (parser,
33780 OMP_CLAUSE_NOTINBRANCH,
33781 clauses, token->location);
33782 c_name = "notinbranch";
33783 break;
33784 case PRAGMA_OMP_CLAUSE_PARALLEL:
33785 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33786 clauses, token->location);
33787 c_name = "parallel";
33788 if (!first)
33790 clause_not_first:
33791 error_at (token->location, "%qs must be the first clause of %qs",
33792 c_name, where);
33793 clauses = prev;
33795 break;
33796 case PRAGMA_OMP_CLAUSE_FOR:
33797 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33798 clauses, token->location);
33799 c_name = "for";
33800 if (!first)
33801 goto clause_not_first;
33802 break;
33803 case PRAGMA_OMP_CLAUSE_SECTIONS:
33804 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33805 clauses, token->location);
33806 c_name = "sections";
33807 if (!first)
33808 goto clause_not_first;
33809 break;
33810 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33811 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33812 clauses, token->location);
33813 c_name = "taskgroup";
33814 if (!first)
33815 goto clause_not_first;
33816 break;
33817 case PRAGMA_OMP_CLAUSE_LINK:
33818 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33819 c_name = "to";
33820 break;
33821 case PRAGMA_OMP_CLAUSE_TO:
33822 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33823 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33824 clauses);
33825 else
33826 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33827 c_name = "to";
33828 break;
33829 case PRAGMA_OMP_CLAUSE_FROM:
33830 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33831 c_name = "from";
33832 break;
33833 case PRAGMA_OMP_CLAUSE_UNIFORM:
33834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33835 clauses);
33836 c_name = "uniform";
33837 break;
33838 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33839 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33840 token->location);
33841 c_name = "num_teams";
33842 break;
33843 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33844 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33845 token->location);
33846 c_name = "thread_limit";
33847 break;
33848 case PRAGMA_OMP_CLAUSE_ALIGNED:
33849 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33850 c_name = "aligned";
33851 break;
33852 case PRAGMA_OMP_CLAUSE_LINEAR:
33854 bool cilk_simd_fn = false, declare_simd = false;
33855 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33856 cilk_simd_fn = true;
33857 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33858 declare_simd = true;
33859 clauses = cp_parser_omp_clause_linear (parser, clauses,
33860 cilk_simd_fn, declare_simd);
33862 c_name = "linear";
33863 break;
33864 case PRAGMA_OMP_CLAUSE_DEPEND:
33865 clauses = cp_parser_omp_clause_depend (parser, clauses,
33866 token->location);
33867 c_name = "depend";
33868 break;
33869 case PRAGMA_OMP_CLAUSE_MAP:
33870 clauses = cp_parser_omp_clause_map (parser, clauses);
33871 c_name = "map";
33872 break;
33873 case PRAGMA_OMP_CLAUSE_DEVICE:
33874 clauses = cp_parser_omp_clause_device (parser, clauses,
33875 token->location);
33876 c_name = "device";
33877 break;
33878 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33879 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33880 token->location);
33881 c_name = "dist_schedule";
33882 break;
33883 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33884 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33885 token->location);
33886 c_name = "proc_bind";
33887 break;
33888 case PRAGMA_OMP_CLAUSE_SAFELEN:
33889 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33890 token->location);
33891 c_name = "safelen";
33892 break;
33893 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33894 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33895 token->location);
33896 c_name = "simdlen";
33897 break;
33898 case PRAGMA_OMP_CLAUSE_NOGROUP:
33899 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33900 token->location);
33901 c_name = "nogroup";
33902 break;
33903 case PRAGMA_OMP_CLAUSE_THREADS:
33904 clauses
33905 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33906 clauses, token->location);
33907 c_name = "threads";
33908 break;
33909 case PRAGMA_OMP_CLAUSE_SIMD:
33910 clauses
33911 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33912 clauses, token->location);
33913 c_name = "simd";
33914 break;
33915 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33916 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33917 c_name = "simdlen";
33918 break;
33919 default:
33920 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33921 goto saw_error;
33924 first = false;
33926 if (((mask >> c_kind) & 1) == 0)
33928 /* Remove the invalid clause(s) from the list to avoid
33929 confusing the rest of the compiler. */
33930 clauses = prev;
33931 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33934 saw_error:
33935 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33936 no reason to skip to the end. */
33937 if (!(flag_cilkplus && pragma_tok == NULL))
33938 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33939 if (finish_p)
33941 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33942 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33943 else
33944 return finish_omp_clauses (clauses, C_ORT_OMP);
33946 return clauses;
33949 /* OpenMP 2.5:
33950 structured-block:
33951 statement
33953 In practice, we're also interested in adding the statement to an
33954 outer node. So it is convenient if we work around the fact that
33955 cp_parser_statement calls add_stmt. */
33957 static unsigned
33958 cp_parser_begin_omp_structured_block (cp_parser *parser)
33960 unsigned save = parser->in_statement;
33962 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33963 This preserves the "not within loop or switch" style error messages
33964 for nonsense cases like
33965 void foo() {
33966 #pragma omp single
33967 break;
33970 if (parser->in_statement)
33971 parser->in_statement = IN_OMP_BLOCK;
33973 return save;
33976 static void
33977 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33979 parser->in_statement = save;
33982 static tree
33983 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33985 tree stmt = begin_omp_structured_block ();
33986 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33988 cp_parser_statement (parser, NULL_TREE, false, if_p);
33990 cp_parser_end_omp_structured_block (parser, save);
33991 return finish_omp_structured_block (stmt);
33994 /* OpenMP 2.5:
33995 # pragma omp atomic new-line
33996 expression-stmt
33998 expression-stmt:
33999 x binop= expr | x++ | ++x | x-- | --x
34000 binop:
34001 +, *, -, /, &, ^, |, <<, >>
34003 where x is an lvalue expression with scalar type.
34005 OpenMP 3.1:
34006 # pragma omp atomic new-line
34007 update-stmt
34009 # pragma omp atomic read new-line
34010 read-stmt
34012 # pragma omp atomic write new-line
34013 write-stmt
34015 # pragma omp atomic update new-line
34016 update-stmt
34018 # pragma omp atomic capture new-line
34019 capture-stmt
34021 # pragma omp atomic capture new-line
34022 capture-block
34024 read-stmt:
34025 v = x
34026 write-stmt:
34027 x = expr
34028 update-stmt:
34029 expression-stmt | x = x binop expr
34030 capture-stmt:
34031 v = expression-stmt
34032 capture-block:
34033 { v = x; update-stmt; } | { update-stmt; v = x; }
34035 OpenMP 4.0:
34036 update-stmt:
34037 expression-stmt | x = x binop expr | x = expr binop x
34038 capture-stmt:
34039 v = update-stmt
34040 capture-block:
34041 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
34043 where x and v are lvalue expressions with scalar type. */
34045 static void
34046 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
34048 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
34049 tree rhs1 = NULL_TREE, orig_lhs;
34050 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
34051 bool structured_block = false;
34052 bool seq_cst = false;
34054 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34056 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34057 const char *p = IDENTIFIER_POINTER (id);
34059 if (!strcmp (p, "seq_cst"))
34061 seq_cst = true;
34062 cp_lexer_consume_token (parser->lexer);
34063 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34064 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34065 cp_lexer_consume_token (parser->lexer);
34068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34070 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34071 const char *p = IDENTIFIER_POINTER (id);
34073 if (!strcmp (p, "read"))
34074 code = OMP_ATOMIC_READ;
34075 else if (!strcmp (p, "write"))
34076 code = NOP_EXPR;
34077 else if (!strcmp (p, "update"))
34078 code = OMP_ATOMIC;
34079 else if (!strcmp (p, "capture"))
34080 code = OMP_ATOMIC_CAPTURE_NEW;
34081 else
34082 p = NULL;
34083 if (p)
34084 cp_lexer_consume_token (parser->lexer);
34086 if (!seq_cst)
34088 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
34089 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
34090 cp_lexer_consume_token (parser->lexer);
34092 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34094 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34095 const char *p = IDENTIFIER_POINTER (id);
34097 if (!strcmp (p, "seq_cst"))
34099 seq_cst = true;
34100 cp_lexer_consume_token (parser->lexer);
34104 cp_parser_require_pragma_eol (parser, pragma_tok);
34106 switch (code)
34108 case OMP_ATOMIC_READ:
34109 case NOP_EXPR: /* atomic write */
34110 v = cp_parser_unary_expression (parser);
34111 if (v == error_mark_node)
34112 goto saw_error;
34113 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34114 goto saw_error;
34115 if (code == NOP_EXPR)
34116 lhs = cp_parser_expression (parser);
34117 else
34118 lhs = cp_parser_unary_expression (parser);
34119 if (lhs == error_mark_node)
34120 goto saw_error;
34121 if (code == NOP_EXPR)
34123 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
34124 opcode. */
34125 code = OMP_ATOMIC;
34126 rhs = lhs;
34127 lhs = v;
34128 v = NULL_TREE;
34130 goto done;
34131 case OMP_ATOMIC_CAPTURE_NEW:
34132 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34134 cp_lexer_consume_token (parser->lexer);
34135 structured_block = true;
34137 else
34139 v = cp_parser_unary_expression (parser);
34140 if (v == error_mark_node)
34141 goto saw_error;
34142 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34143 goto saw_error;
34145 default:
34146 break;
34149 restart:
34150 lhs = cp_parser_unary_expression (parser);
34151 orig_lhs = lhs;
34152 switch (TREE_CODE (lhs))
34154 case ERROR_MARK:
34155 goto saw_error;
34157 case POSTINCREMENT_EXPR:
34158 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34159 code = OMP_ATOMIC_CAPTURE_OLD;
34160 /* FALLTHROUGH */
34161 case PREINCREMENT_EXPR:
34162 lhs = TREE_OPERAND (lhs, 0);
34163 opcode = PLUS_EXPR;
34164 rhs = integer_one_node;
34165 break;
34167 case POSTDECREMENT_EXPR:
34168 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
34169 code = OMP_ATOMIC_CAPTURE_OLD;
34170 /* FALLTHROUGH */
34171 case PREDECREMENT_EXPR:
34172 lhs = TREE_OPERAND (lhs, 0);
34173 opcode = MINUS_EXPR;
34174 rhs = integer_one_node;
34175 break;
34177 case COMPOUND_EXPR:
34178 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
34179 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
34180 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
34181 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
34182 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
34183 (TREE_OPERAND (lhs, 1), 0), 0)))
34184 == BOOLEAN_TYPE)
34185 /* Undo effects of boolean_increment for post {in,de}crement. */
34186 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
34187 /* FALLTHRU */
34188 case MODIFY_EXPR:
34189 if (TREE_CODE (lhs) == MODIFY_EXPR
34190 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
34192 /* Undo effects of boolean_increment. */
34193 if (integer_onep (TREE_OPERAND (lhs, 1)))
34195 /* This is pre or post increment. */
34196 rhs = TREE_OPERAND (lhs, 1);
34197 lhs = TREE_OPERAND (lhs, 0);
34198 opcode = NOP_EXPR;
34199 if (code == OMP_ATOMIC_CAPTURE_NEW
34200 && !structured_block
34201 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
34202 code = OMP_ATOMIC_CAPTURE_OLD;
34203 break;
34206 /* FALLTHRU */
34207 default:
34208 switch (cp_lexer_peek_token (parser->lexer)->type)
34210 case CPP_MULT_EQ:
34211 opcode = MULT_EXPR;
34212 break;
34213 case CPP_DIV_EQ:
34214 opcode = TRUNC_DIV_EXPR;
34215 break;
34216 case CPP_PLUS_EQ:
34217 opcode = PLUS_EXPR;
34218 break;
34219 case CPP_MINUS_EQ:
34220 opcode = MINUS_EXPR;
34221 break;
34222 case CPP_LSHIFT_EQ:
34223 opcode = LSHIFT_EXPR;
34224 break;
34225 case CPP_RSHIFT_EQ:
34226 opcode = RSHIFT_EXPR;
34227 break;
34228 case CPP_AND_EQ:
34229 opcode = BIT_AND_EXPR;
34230 break;
34231 case CPP_OR_EQ:
34232 opcode = BIT_IOR_EXPR;
34233 break;
34234 case CPP_XOR_EQ:
34235 opcode = BIT_XOR_EXPR;
34236 break;
34237 case CPP_EQ:
34238 enum cp_parser_prec oprec;
34239 cp_token *token;
34240 cp_lexer_consume_token (parser->lexer);
34241 cp_parser_parse_tentatively (parser);
34242 rhs1 = cp_parser_simple_cast_expression (parser);
34243 if (rhs1 == error_mark_node)
34245 cp_parser_abort_tentative_parse (parser);
34246 cp_parser_simple_cast_expression (parser);
34247 goto saw_error;
34249 token = cp_lexer_peek_token (parser->lexer);
34250 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34252 cp_parser_abort_tentative_parse (parser);
34253 cp_parser_parse_tentatively (parser);
34254 rhs = cp_parser_binary_expression (parser, false, true,
34255 PREC_NOT_OPERATOR, NULL);
34256 if (rhs == error_mark_node)
34258 cp_parser_abort_tentative_parse (parser);
34259 cp_parser_binary_expression (parser, false, true,
34260 PREC_NOT_OPERATOR, NULL);
34261 goto saw_error;
34263 switch (TREE_CODE (rhs))
34265 case MULT_EXPR:
34266 case TRUNC_DIV_EXPR:
34267 case RDIV_EXPR:
34268 case PLUS_EXPR:
34269 case MINUS_EXPR:
34270 case LSHIFT_EXPR:
34271 case RSHIFT_EXPR:
34272 case BIT_AND_EXPR:
34273 case BIT_IOR_EXPR:
34274 case BIT_XOR_EXPR:
34275 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34277 if (cp_parser_parse_definitely (parser))
34279 opcode = TREE_CODE (rhs);
34280 rhs1 = TREE_OPERAND (rhs, 0);
34281 rhs = TREE_OPERAND (rhs, 1);
34282 goto stmt_done;
34284 else
34285 goto saw_error;
34287 break;
34288 default:
34289 break;
34291 cp_parser_abort_tentative_parse (parser);
34292 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34294 rhs = cp_parser_expression (parser);
34295 if (rhs == error_mark_node)
34296 goto saw_error;
34297 opcode = NOP_EXPR;
34298 rhs1 = NULL_TREE;
34299 goto stmt_done;
34301 cp_parser_error (parser,
34302 "invalid form of %<#pragma omp atomic%>");
34303 goto saw_error;
34305 if (!cp_parser_parse_definitely (parser))
34306 goto saw_error;
34307 switch (token->type)
34309 case CPP_SEMICOLON:
34310 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34312 code = OMP_ATOMIC_CAPTURE_OLD;
34313 v = lhs;
34314 lhs = NULL_TREE;
34315 lhs1 = rhs1;
34316 rhs1 = NULL_TREE;
34317 cp_lexer_consume_token (parser->lexer);
34318 goto restart;
34320 else if (structured_block)
34322 opcode = NOP_EXPR;
34323 rhs = rhs1;
34324 rhs1 = NULL_TREE;
34325 goto stmt_done;
34327 cp_parser_error (parser,
34328 "invalid form of %<#pragma omp atomic%>");
34329 goto saw_error;
34330 case CPP_MULT:
34331 opcode = MULT_EXPR;
34332 break;
34333 case CPP_DIV:
34334 opcode = TRUNC_DIV_EXPR;
34335 break;
34336 case CPP_PLUS:
34337 opcode = PLUS_EXPR;
34338 break;
34339 case CPP_MINUS:
34340 opcode = MINUS_EXPR;
34341 break;
34342 case CPP_LSHIFT:
34343 opcode = LSHIFT_EXPR;
34344 break;
34345 case CPP_RSHIFT:
34346 opcode = RSHIFT_EXPR;
34347 break;
34348 case CPP_AND:
34349 opcode = BIT_AND_EXPR;
34350 break;
34351 case CPP_OR:
34352 opcode = BIT_IOR_EXPR;
34353 break;
34354 case CPP_XOR:
34355 opcode = BIT_XOR_EXPR;
34356 break;
34357 default:
34358 cp_parser_error (parser,
34359 "invalid operator for %<#pragma omp atomic%>");
34360 goto saw_error;
34362 oprec = TOKEN_PRECEDENCE (token);
34363 gcc_assert (oprec != PREC_NOT_OPERATOR);
34364 if (commutative_tree_code (opcode))
34365 oprec = (enum cp_parser_prec) (oprec - 1);
34366 cp_lexer_consume_token (parser->lexer);
34367 rhs = cp_parser_binary_expression (parser, false, false,
34368 oprec, NULL);
34369 if (rhs == error_mark_node)
34370 goto saw_error;
34371 goto stmt_done;
34372 /* FALLTHROUGH */
34373 default:
34374 cp_parser_error (parser,
34375 "invalid operator for %<#pragma omp atomic%>");
34376 goto saw_error;
34378 cp_lexer_consume_token (parser->lexer);
34380 rhs = cp_parser_expression (parser);
34381 if (rhs == error_mark_node)
34382 goto saw_error;
34383 break;
34385 stmt_done:
34386 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34388 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34389 goto saw_error;
34390 v = cp_parser_unary_expression (parser);
34391 if (v == error_mark_node)
34392 goto saw_error;
34393 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34394 goto saw_error;
34395 lhs1 = cp_parser_unary_expression (parser);
34396 if (lhs1 == error_mark_node)
34397 goto saw_error;
34399 if (structured_block)
34401 cp_parser_consume_semicolon_at_end_of_statement (parser);
34402 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34404 done:
34405 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34406 if (!structured_block)
34407 cp_parser_consume_semicolon_at_end_of_statement (parser);
34408 return;
34410 saw_error:
34411 cp_parser_skip_to_end_of_block_or_statement (parser);
34412 if (structured_block)
34414 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34415 cp_lexer_consume_token (parser->lexer);
34416 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34418 cp_parser_skip_to_end_of_block_or_statement (parser);
34419 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34420 cp_lexer_consume_token (parser->lexer);
34426 /* OpenMP 2.5:
34427 # pragma omp barrier new-line */
34429 static void
34430 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34432 cp_parser_require_pragma_eol (parser, pragma_tok);
34433 finish_omp_barrier ();
34436 /* OpenMP 2.5:
34437 # pragma omp critical [(name)] new-line
34438 structured-block
34440 OpenMP 4.5:
34441 # pragma omp critical [(name) [hint(expression)]] new-line
34442 structured-block */
34444 #define OMP_CRITICAL_CLAUSE_MASK \
34445 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34447 static tree
34448 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34450 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34454 matching_parens parens;
34455 parens.consume_open (parser);
34457 name = cp_parser_identifier (parser);
34459 if (name == error_mark_node
34460 || !parens.require_close (parser))
34461 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34462 /*or_comma=*/false,
34463 /*consume_paren=*/true);
34464 if (name == error_mark_node)
34465 name = NULL;
34467 clauses = cp_parser_omp_all_clauses (parser,
34468 OMP_CRITICAL_CLAUSE_MASK,
34469 "#pragma omp critical", pragma_tok);
34471 else
34472 cp_parser_require_pragma_eol (parser, pragma_tok);
34474 stmt = cp_parser_omp_structured_block (parser, if_p);
34475 return c_finish_omp_critical (input_location, stmt, name, clauses);
34478 /* OpenMP 2.5:
34479 # pragma omp flush flush-vars[opt] new-line
34481 flush-vars:
34482 ( variable-list ) */
34484 static void
34485 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34487 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34488 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34489 cp_parser_require_pragma_eol (parser, pragma_tok);
34491 finish_omp_flush ();
34494 /* Helper function, to parse omp for increment expression. */
34496 static tree
34497 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34499 tree cond = cp_parser_binary_expression (parser, false, true,
34500 PREC_NOT_OPERATOR, NULL);
34501 if (cond == error_mark_node
34502 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34504 cp_parser_skip_to_end_of_statement (parser);
34505 return error_mark_node;
34508 switch (TREE_CODE (cond))
34510 case GT_EXPR:
34511 case GE_EXPR:
34512 case LT_EXPR:
34513 case LE_EXPR:
34514 break;
34515 case NE_EXPR:
34516 if (code == CILK_SIMD || code == CILK_FOR)
34517 break;
34518 /* Fall through: OpenMP disallows NE_EXPR. */
34519 gcc_fallthrough ();
34520 default:
34521 return error_mark_node;
34524 /* If decl is an iterator, preserve LHS and RHS of the relational
34525 expr until finish_omp_for. */
34526 if (decl
34527 && (type_dependent_expression_p (decl)
34528 || CLASS_TYPE_P (TREE_TYPE (decl))))
34529 return cond;
34531 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34532 TREE_CODE (cond),
34533 TREE_OPERAND (cond, 0), ERROR_MARK,
34534 TREE_OPERAND (cond, 1), ERROR_MARK,
34535 /*overload=*/NULL, tf_warning_or_error);
34538 /* Helper function, to parse omp for increment expression. */
34540 static tree
34541 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34543 cp_token *token = cp_lexer_peek_token (parser->lexer);
34544 enum tree_code op;
34545 tree lhs, rhs;
34546 cp_id_kind idk;
34547 bool decl_first;
34549 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34551 op = (token->type == CPP_PLUS_PLUS
34552 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34553 cp_lexer_consume_token (parser->lexer);
34554 lhs = cp_parser_simple_cast_expression (parser);
34555 if (lhs != decl
34556 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34557 return error_mark_node;
34558 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34561 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34562 if (lhs != decl
34563 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34564 return error_mark_node;
34566 token = cp_lexer_peek_token (parser->lexer);
34567 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34569 op = (token->type == CPP_PLUS_PLUS
34570 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34571 cp_lexer_consume_token (parser->lexer);
34572 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34575 op = cp_parser_assignment_operator_opt (parser);
34576 if (op == ERROR_MARK)
34577 return error_mark_node;
34579 if (op != NOP_EXPR)
34581 rhs = cp_parser_assignment_expression (parser);
34582 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34583 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34586 lhs = cp_parser_binary_expression (parser, false, false,
34587 PREC_ADDITIVE_EXPRESSION, NULL);
34588 token = cp_lexer_peek_token (parser->lexer);
34589 decl_first = (lhs == decl
34590 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34591 if (decl_first)
34592 lhs = NULL_TREE;
34593 if (token->type != CPP_PLUS
34594 && token->type != CPP_MINUS)
34595 return error_mark_node;
34599 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34600 cp_lexer_consume_token (parser->lexer);
34601 rhs = cp_parser_binary_expression (parser, false, false,
34602 PREC_ADDITIVE_EXPRESSION, NULL);
34603 token = cp_lexer_peek_token (parser->lexer);
34604 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34606 if (lhs == NULL_TREE)
34608 if (op == PLUS_EXPR)
34609 lhs = rhs;
34610 else
34611 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34612 tf_warning_or_error);
34614 else
34615 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34616 ERROR_MARK, NULL, tf_warning_or_error);
34619 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34621 if (!decl_first)
34623 if ((rhs != decl
34624 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34625 || op == MINUS_EXPR)
34626 return error_mark_node;
34627 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34629 else
34630 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34632 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34635 /* Parse the initialization statement of either an OpenMP for loop or
34636 a Cilk Plus for loop.
34638 Return true if the resulting construct should have an
34639 OMP_CLAUSE_PRIVATE added to it. */
34641 static tree
34642 cp_parser_omp_for_loop_init (cp_parser *parser,
34643 enum tree_code code,
34644 tree &this_pre_body,
34645 vec<tree, va_gc> *for_block,
34646 tree &init,
34647 tree &orig_init,
34648 tree &decl,
34649 tree &real_decl)
34651 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34652 return NULL_TREE;
34654 tree add_private_clause = NULL_TREE;
34656 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34658 init-expr:
34659 var = lb
34660 integer-type var = lb
34661 random-access-iterator-type var = lb
34662 pointer-type var = lb
34664 cp_decl_specifier_seq type_specifiers;
34666 /* First, try to parse as an initialized declaration. See
34667 cp_parser_condition, from whence the bulk of this is copied. */
34669 cp_parser_parse_tentatively (parser);
34670 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34671 /*is_trailing_return=*/false,
34672 &type_specifiers);
34673 if (cp_parser_parse_definitely (parser))
34675 /* If parsing a type specifier seq succeeded, then this
34676 MUST be a initialized declaration. */
34677 tree asm_specification, attributes;
34678 cp_declarator *declarator;
34680 declarator = cp_parser_declarator (parser,
34681 CP_PARSER_DECLARATOR_NAMED,
34682 /*ctor_dtor_or_conv_p=*/NULL,
34683 /*parenthesized_p=*/NULL,
34684 /*member_p=*/false,
34685 /*friend_p=*/false);
34686 attributes = cp_parser_attributes_opt (parser);
34687 asm_specification = cp_parser_asm_specification_opt (parser);
34689 if (declarator == cp_error_declarator)
34690 cp_parser_skip_to_end_of_statement (parser);
34692 else
34694 tree pushed_scope, auto_node;
34696 decl = start_decl (declarator, &type_specifiers,
34697 SD_INITIALIZED, attributes,
34698 /*prefix_attributes=*/NULL_TREE,
34699 &pushed_scope);
34701 auto_node = type_uses_auto (TREE_TYPE (decl));
34702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34704 if (cp_lexer_next_token_is (parser->lexer,
34705 CPP_OPEN_PAREN))
34707 if (code != CILK_SIMD && code != CILK_FOR)
34708 error ("parenthesized initialization is not allowed in "
34709 "OpenMP %<for%> loop");
34710 else
34711 error ("parenthesized initialization is "
34712 "not allowed in for-loop");
34714 else
34715 /* Trigger an error. */
34716 cp_parser_require (parser, CPP_EQ, RT_EQ);
34718 init = error_mark_node;
34719 cp_parser_skip_to_end_of_statement (parser);
34721 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34722 || type_dependent_expression_p (decl)
34723 || auto_node)
34725 bool is_direct_init, is_non_constant_init;
34727 init = cp_parser_initializer (parser,
34728 &is_direct_init,
34729 &is_non_constant_init);
34731 if (auto_node)
34733 TREE_TYPE (decl)
34734 = do_auto_deduction (TREE_TYPE (decl), init,
34735 auto_node);
34737 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34738 && !type_dependent_expression_p (decl))
34739 goto non_class;
34742 cp_finish_decl (decl, init, !is_non_constant_init,
34743 asm_specification,
34744 LOOKUP_ONLYCONVERTING);
34745 orig_init = init;
34746 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34748 vec_safe_push (for_block, this_pre_body);
34749 init = NULL_TREE;
34751 else
34753 init = pop_stmt_list (this_pre_body);
34754 if (init && TREE_CODE (init) == STATEMENT_LIST)
34756 tree_stmt_iterator i = tsi_start (init);
34757 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34758 while (!tsi_end_p (i))
34760 tree t = tsi_stmt (i);
34761 if (TREE_CODE (t) == DECL_EXPR
34762 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34764 tsi_delink (&i);
34765 vec_safe_push (for_block, t);
34766 continue;
34768 break;
34770 if (tsi_one_before_end_p (i))
34772 tree t = tsi_stmt (i);
34773 tsi_delink (&i);
34774 free_stmt_list (init);
34775 init = t;
34779 this_pre_body = NULL_TREE;
34781 else
34783 /* Consume '='. */
34784 cp_lexer_consume_token (parser->lexer);
34785 init = cp_parser_assignment_expression (parser);
34787 non_class:
34788 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34789 init = error_mark_node;
34790 else
34791 cp_finish_decl (decl, NULL_TREE,
34792 /*init_const_expr_p=*/false,
34793 asm_specification,
34794 LOOKUP_ONLYCONVERTING);
34797 if (pushed_scope)
34798 pop_scope (pushed_scope);
34801 else
34803 cp_id_kind idk;
34804 /* If parsing a type specifier sequence failed, then
34805 this MUST be a simple expression. */
34806 if (code == CILK_FOR)
34807 error ("%<_Cilk_for%> allows expression instead of declaration only "
34808 "in C, not in C++");
34809 cp_parser_parse_tentatively (parser);
34810 decl = cp_parser_primary_expression (parser, false, false,
34811 false, &idk);
34812 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34813 if (!cp_parser_error_occurred (parser)
34814 && decl
34815 && (TREE_CODE (decl) == COMPONENT_REF
34816 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34818 cp_parser_abort_tentative_parse (parser);
34819 cp_parser_parse_tentatively (parser);
34820 cp_token *token = cp_lexer_peek_token (parser->lexer);
34821 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34822 /*check_dependency_p=*/true,
34823 /*template_p=*/NULL,
34824 /*declarator_p=*/false,
34825 /*optional_p=*/false);
34826 if (name != error_mark_node
34827 && last_tok == cp_lexer_peek_token (parser->lexer))
34829 decl = cp_parser_lookup_name_simple (parser, name,
34830 token->location);
34831 if (TREE_CODE (decl) == FIELD_DECL)
34832 add_private_clause = omp_privatize_field (decl, false);
34834 cp_parser_abort_tentative_parse (parser);
34835 cp_parser_parse_tentatively (parser);
34836 decl = cp_parser_primary_expression (parser, false, false,
34837 false, &idk);
34839 if (!cp_parser_error_occurred (parser)
34840 && decl
34841 && DECL_P (decl)
34842 && CLASS_TYPE_P (TREE_TYPE (decl)))
34844 tree rhs;
34846 cp_parser_parse_definitely (parser);
34847 cp_parser_require (parser, CPP_EQ, RT_EQ);
34848 rhs = cp_parser_assignment_expression (parser);
34849 orig_init = rhs;
34850 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34851 decl, NOP_EXPR,
34852 rhs,
34853 tf_warning_or_error));
34854 if (!add_private_clause)
34855 add_private_clause = decl;
34857 else
34859 decl = NULL;
34860 cp_parser_abort_tentative_parse (parser);
34861 init = cp_parser_expression (parser);
34862 if (init)
34864 if (TREE_CODE (init) == MODIFY_EXPR
34865 || TREE_CODE (init) == MODOP_EXPR)
34866 real_decl = TREE_OPERAND (init, 0);
34870 return add_private_clause;
34873 /* Parse the restricted form of the for statement allowed by OpenMP. */
34875 static tree
34876 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34877 tree *cclauses, bool *if_p)
34879 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34880 tree real_decl, initv, condv, incrv, declv;
34881 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34882 location_t loc_first;
34883 bool collapse_err = false;
34884 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34885 vec<tree, va_gc> *for_block = make_tree_vector ();
34886 auto_vec<tree, 4> orig_inits;
34887 bool tiling = false;
34889 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34890 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34891 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34892 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34894 tiling = true;
34895 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34897 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34898 && OMP_CLAUSE_ORDERED_EXPR (cl))
34900 ordered_cl = cl;
34901 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34904 if (ordered && ordered < collapse)
34906 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34907 "%<ordered%> clause parameter is less than %<collapse%>");
34908 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34909 = build_int_cst (NULL_TREE, collapse);
34910 ordered = collapse;
34912 if (ordered)
34914 for (tree *pc = &clauses; *pc; )
34915 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34917 error_at (OMP_CLAUSE_LOCATION (*pc),
34918 "%<linear%> clause may not be specified together "
34919 "with %<ordered%> clause with a parameter");
34920 *pc = OMP_CLAUSE_CHAIN (*pc);
34922 else
34923 pc = &OMP_CLAUSE_CHAIN (*pc);
34926 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34927 count = ordered ? ordered : collapse;
34929 declv = make_tree_vec (count);
34930 initv = make_tree_vec (count);
34931 condv = make_tree_vec (count);
34932 incrv = make_tree_vec (count);
34934 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34936 for (i = 0; i < count; i++)
34938 int bracecount = 0;
34939 tree add_private_clause = NULL_TREE;
34940 location_t loc;
34942 if (code != CILK_FOR
34943 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34945 if (!collapse_err)
34946 cp_parser_error (parser, "for statement expected");
34947 return NULL;
34949 if (code == CILK_FOR
34950 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34952 if (!collapse_err)
34953 cp_parser_error (parser, "_Cilk_for statement expected");
34954 return NULL;
34956 loc = cp_lexer_consume_token (parser->lexer)->location;
34958 matching_parens parens;
34959 if (!parens.require_open (parser))
34960 return NULL;
34962 init = orig_init = decl = real_decl = NULL;
34963 this_pre_body = push_stmt_list ();
34965 add_private_clause
34966 = cp_parser_omp_for_loop_init (parser, code,
34967 this_pre_body, for_block,
34968 init, orig_init, decl, real_decl);
34970 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34971 if (this_pre_body)
34973 this_pre_body = pop_stmt_list (this_pre_body);
34974 if (pre_body)
34976 tree t = pre_body;
34977 pre_body = push_stmt_list ();
34978 add_stmt (t);
34979 add_stmt (this_pre_body);
34980 pre_body = pop_stmt_list (pre_body);
34982 else
34983 pre_body = this_pre_body;
34986 if (decl)
34987 real_decl = decl;
34988 if (cclauses != NULL
34989 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34990 && real_decl != NULL_TREE)
34992 tree *c;
34993 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34994 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34995 && OMP_CLAUSE_DECL (*c) == real_decl)
34997 error_at (loc, "iteration variable %qD"
34998 " should not be firstprivate", real_decl);
34999 *c = OMP_CLAUSE_CHAIN (*c);
35001 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
35002 && OMP_CLAUSE_DECL (*c) == real_decl)
35004 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
35005 tree l = *c;
35006 *c = OMP_CLAUSE_CHAIN (*c);
35007 if (code == OMP_SIMD)
35009 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35010 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
35012 else
35014 OMP_CLAUSE_CHAIN (l) = clauses;
35015 clauses = l;
35017 add_private_clause = NULL_TREE;
35019 else
35021 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
35022 && OMP_CLAUSE_DECL (*c) == real_decl)
35023 add_private_clause = NULL_TREE;
35024 c = &OMP_CLAUSE_CHAIN (*c);
35028 if (add_private_clause)
35030 tree c;
35031 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
35033 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
35034 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
35035 && OMP_CLAUSE_DECL (c) == decl)
35036 break;
35037 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
35038 && OMP_CLAUSE_DECL (c) == decl)
35039 error_at (loc, "iteration variable %qD "
35040 "should not be firstprivate",
35041 decl);
35042 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
35043 && OMP_CLAUSE_DECL (c) == decl)
35044 error_at (loc, "iteration variable %qD should not be reduction",
35045 decl);
35047 if (c == NULL)
35049 if (code != OMP_SIMD)
35050 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
35051 else if (collapse == 1)
35052 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
35053 else
35054 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
35055 OMP_CLAUSE_DECL (c) = add_private_clause;
35056 c = finish_omp_clauses (c, C_ORT_OMP);
35057 if (c)
35059 OMP_CLAUSE_CHAIN (c) = clauses;
35060 clauses = c;
35061 /* For linear, signal that we need to fill up
35062 the so far unknown linear step. */
35063 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
35064 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
35069 cond = NULL;
35070 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
35071 cond = cp_parser_omp_for_cond (parser, decl, code);
35072 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
35074 incr = NULL;
35075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
35077 /* If decl is an iterator, preserve the operator on decl
35078 until finish_omp_for. */
35079 if (real_decl
35080 && ((processing_template_decl
35081 && (TREE_TYPE (real_decl) == NULL_TREE
35082 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
35083 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
35084 incr = cp_parser_omp_for_incr (parser, real_decl);
35085 else
35086 incr = cp_parser_expression (parser);
35087 if (!EXPR_HAS_LOCATION (incr))
35088 protected_set_expr_location (incr, input_location);
35091 if (!parens.require_close (parser))
35092 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
35093 /*or_comma=*/false,
35094 /*consume_paren=*/true);
35096 TREE_VEC_ELT (declv, i) = decl;
35097 TREE_VEC_ELT (initv, i) = init;
35098 TREE_VEC_ELT (condv, i) = cond;
35099 TREE_VEC_ELT (incrv, i) = incr;
35100 if (orig_init)
35102 orig_inits.safe_grow_cleared (i + 1);
35103 orig_inits[i] = orig_init;
35106 if (i == count - 1)
35107 break;
35109 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
35110 in between the collapsed for loops to be still considered perfectly
35111 nested. Hopefully the final version clarifies this.
35112 For now handle (multiple) {'s and empty statements. */
35113 cp_parser_parse_tentatively (parser);
35114 for (;;)
35116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35117 break;
35118 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
35120 cp_lexer_consume_token (parser->lexer);
35121 bracecount++;
35123 else if (bracecount
35124 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35125 cp_lexer_consume_token (parser->lexer);
35126 else
35128 loc = cp_lexer_peek_token (parser->lexer)->location;
35129 error_at (loc, "not enough for loops to collapse");
35130 collapse_err = true;
35131 cp_parser_abort_tentative_parse (parser);
35132 declv = NULL_TREE;
35133 break;
35137 if (declv)
35139 cp_parser_parse_definitely (parser);
35140 nbraces += bracecount;
35144 if (nbraces)
35145 if_p = NULL;
35147 /* Note that we saved the original contents of this flag when we entered
35148 the structured block, and so we don't need to re-save it here. */
35149 if (code == CILK_SIMD || code == CILK_FOR)
35150 parser->in_statement = IN_CILK_SIMD_FOR;
35151 else
35152 parser->in_statement = IN_OMP_FOR;
35154 /* Note that the grammar doesn't call for a structured block here,
35155 though the loop as a whole is a structured block. */
35156 body = push_stmt_list ();
35157 cp_parser_statement (parser, NULL_TREE, false, if_p);
35158 body = pop_stmt_list (body);
35160 if (declv == NULL_TREE)
35161 ret = NULL_TREE;
35162 else
35163 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
35164 body, pre_body, &orig_inits, clauses);
35166 while (nbraces)
35168 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
35170 cp_lexer_consume_token (parser->lexer);
35171 nbraces--;
35173 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
35174 cp_lexer_consume_token (parser->lexer);
35175 else
35177 if (!collapse_err)
35179 error_at (cp_lexer_peek_token (parser->lexer)->location,
35180 "collapsed loops not perfectly nested");
35182 collapse_err = true;
35183 cp_parser_statement_seq_opt (parser, NULL);
35184 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
35185 break;
35189 while (!for_block->is_empty ())
35191 tree t = for_block->pop ();
35192 if (TREE_CODE (t) == STATEMENT_LIST)
35193 add_stmt (pop_stmt_list (t));
35194 else
35195 add_stmt (t);
35197 release_tree_vector (for_block);
35199 return ret;
35202 /* Helper function for OpenMP parsing, split clauses and call
35203 finish_omp_clauses on each of the set of clauses afterwards. */
35205 static void
35206 cp_omp_split_clauses (location_t loc, enum tree_code code,
35207 omp_clause_mask mask, tree clauses, tree *cclauses)
35209 int i;
35210 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
35211 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
35212 if (cclauses[i])
35213 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
35216 /* OpenMP 4.0:
35217 #pragma omp simd simd-clause[optseq] new-line
35218 for-loop */
35220 #define OMP_SIMD_CLAUSE_MASK \
35221 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35230 static tree
35231 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
35232 char *p_name, omp_clause_mask mask, tree *cclauses,
35233 bool *if_p)
35235 tree clauses, sb, ret;
35236 unsigned int save;
35237 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35239 strcat (p_name, " simd");
35240 mask |= OMP_SIMD_CLAUSE_MASK;
35242 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35243 cclauses == NULL);
35244 if (cclauses)
35246 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35247 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35248 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35249 OMP_CLAUSE_ORDERED);
35250 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35252 error_at (OMP_CLAUSE_LOCATION (c),
35253 "%<ordered%> clause with parameter may not be specified "
35254 "on %qs construct", p_name);
35255 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35259 sb = begin_omp_structured_block ();
35260 save = cp_parser_begin_omp_structured_block (parser);
35262 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35264 cp_parser_end_omp_structured_block (parser, save);
35265 add_stmt (finish_omp_structured_block (sb));
35267 return ret;
35270 /* OpenMP 2.5:
35271 #pragma omp for for-clause[optseq] new-line
35272 for-loop
35274 OpenMP 4.0:
35275 #pragma omp for simd for-simd-clause[optseq] new-line
35276 for-loop */
35278 #define OMP_FOR_CLAUSE_MASK \
35279 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35280 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35289 static tree
35290 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35291 char *p_name, omp_clause_mask mask, tree *cclauses,
35292 bool *if_p)
35294 tree clauses, sb, ret;
35295 unsigned int save;
35296 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35298 strcat (p_name, " for");
35299 mask |= OMP_FOR_CLAUSE_MASK;
35300 /* parallel for{, simd} disallows nowait clause, but for
35301 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35302 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35303 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35304 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35305 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35306 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35308 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35310 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35311 const char *p = IDENTIFIER_POINTER (id);
35313 if (strcmp (p, "simd") == 0)
35315 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35316 if (cclauses == NULL)
35317 cclauses = cclauses_buf;
35319 cp_lexer_consume_token (parser->lexer);
35320 if (!flag_openmp) /* flag_openmp_simd */
35321 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35322 cclauses, if_p);
35323 sb = begin_omp_structured_block ();
35324 save = cp_parser_begin_omp_structured_block (parser);
35325 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35326 cclauses, if_p);
35327 cp_parser_end_omp_structured_block (parser, save);
35328 tree body = finish_omp_structured_block (sb);
35329 if (ret == NULL)
35330 return ret;
35331 ret = make_node (OMP_FOR);
35332 TREE_TYPE (ret) = void_type_node;
35333 OMP_FOR_BODY (ret) = body;
35334 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35335 SET_EXPR_LOCATION (ret, loc);
35336 add_stmt (ret);
35337 return ret;
35340 if (!flag_openmp) /* flag_openmp_simd */
35342 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35343 return NULL_TREE;
35346 /* Composite distribute parallel for disallows linear clause. */
35347 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35348 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35350 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35351 cclauses == NULL);
35352 if (cclauses)
35354 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35355 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35358 sb = begin_omp_structured_block ();
35359 save = cp_parser_begin_omp_structured_block (parser);
35361 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35363 cp_parser_end_omp_structured_block (parser, save);
35364 add_stmt (finish_omp_structured_block (sb));
35366 return ret;
35369 /* OpenMP 2.5:
35370 # pragma omp master new-line
35371 structured-block */
35373 static tree
35374 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35376 cp_parser_require_pragma_eol (parser, pragma_tok);
35377 return c_finish_omp_master (input_location,
35378 cp_parser_omp_structured_block (parser, if_p));
35381 /* OpenMP 2.5:
35382 # pragma omp ordered new-line
35383 structured-block
35385 OpenMP 4.5:
35386 # pragma omp ordered ordered-clauses new-line
35387 structured-block */
35389 #define OMP_ORDERED_CLAUSE_MASK \
35390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35393 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35394 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35396 static bool
35397 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35398 enum pragma_context context, bool *if_p)
35400 location_t loc = pragma_tok->location;
35402 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35404 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35405 const char *p = IDENTIFIER_POINTER (id);
35407 if (strcmp (p, "depend") == 0)
35409 if (context == pragma_stmt)
35411 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35412 "%<depend%> clause may only be used in compound "
35413 "statements");
35414 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35415 return false;
35417 tree clauses
35418 = cp_parser_omp_all_clauses (parser,
35419 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35420 "#pragma omp ordered", pragma_tok);
35421 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35422 return false;
35426 tree clauses
35427 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35428 "#pragma omp ordered", pragma_tok);
35429 c_finish_omp_ordered (loc, clauses,
35430 cp_parser_omp_structured_block (parser, if_p));
35431 return true;
35434 /* OpenMP 2.5:
35436 section-scope:
35437 { section-sequence }
35439 section-sequence:
35440 section-directive[opt] structured-block
35441 section-sequence section-directive structured-block */
35443 static tree
35444 cp_parser_omp_sections_scope (cp_parser *parser)
35446 tree stmt, substmt;
35447 bool error_suppress = false;
35448 cp_token *tok;
35450 matching_braces braces;
35451 if (!braces.require_open (parser))
35452 return NULL_TREE;
35454 stmt = push_stmt_list ();
35456 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35457 != PRAGMA_OMP_SECTION)
35459 substmt = cp_parser_omp_structured_block (parser, NULL);
35460 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35461 add_stmt (substmt);
35464 while (1)
35466 tok = cp_lexer_peek_token (parser->lexer);
35467 if (tok->type == CPP_CLOSE_BRACE)
35468 break;
35469 if (tok->type == CPP_EOF)
35470 break;
35472 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35474 cp_lexer_consume_token (parser->lexer);
35475 cp_parser_require_pragma_eol (parser, tok);
35476 error_suppress = false;
35478 else if (!error_suppress)
35480 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35481 error_suppress = true;
35484 substmt = cp_parser_omp_structured_block (parser, NULL);
35485 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35486 add_stmt (substmt);
35488 braces.require_close (parser);
35490 substmt = pop_stmt_list (stmt);
35492 stmt = make_node (OMP_SECTIONS);
35493 TREE_TYPE (stmt) = void_type_node;
35494 OMP_SECTIONS_BODY (stmt) = substmt;
35496 add_stmt (stmt);
35497 return stmt;
35500 /* OpenMP 2.5:
35501 # pragma omp sections sections-clause[optseq] newline
35502 sections-scope */
35504 #define OMP_SECTIONS_CLAUSE_MASK \
35505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35511 static tree
35512 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35513 char *p_name, omp_clause_mask mask, tree *cclauses)
35515 tree clauses, ret;
35516 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35518 strcat (p_name, " sections");
35519 mask |= OMP_SECTIONS_CLAUSE_MASK;
35520 if (cclauses)
35521 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35523 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35524 cclauses == NULL);
35525 if (cclauses)
35527 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35528 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35531 ret = cp_parser_omp_sections_scope (parser);
35532 if (ret)
35533 OMP_SECTIONS_CLAUSES (ret) = clauses;
35535 return ret;
35538 /* OpenMP 2.5:
35539 # pragma omp parallel parallel-clause[optseq] new-line
35540 structured-block
35541 # pragma omp parallel for parallel-for-clause[optseq] new-line
35542 structured-block
35543 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35544 structured-block
35546 OpenMP 4.0:
35547 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35548 structured-block */
35550 #define OMP_PARALLEL_CLAUSE_MASK \
35551 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35554 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35555 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35556 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35557 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35561 static tree
35562 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35563 char *p_name, omp_clause_mask mask, tree *cclauses,
35564 bool *if_p)
35566 tree stmt, clauses, block;
35567 unsigned int save;
35568 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35570 strcat (p_name, " parallel");
35571 mask |= OMP_PARALLEL_CLAUSE_MASK;
35572 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35573 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35574 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35575 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35577 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35579 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35580 if (cclauses == NULL)
35581 cclauses = cclauses_buf;
35583 cp_lexer_consume_token (parser->lexer);
35584 if (!flag_openmp) /* flag_openmp_simd */
35585 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35586 if_p);
35587 block = begin_omp_parallel ();
35588 save = cp_parser_begin_omp_structured_block (parser);
35589 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35590 if_p);
35591 cp_parser_end_omp_structured_block (parser, save);
35592 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35593 block);
35594 if (ret == NULL_TREE)
35595 return ret;
35596 OMP_PARALLEL_COMBINED (stmt) = 1;
35597 return stmt;
35599 /* When combined with distribute, parallel has to be followed by for.
35600 #pragma omp target parallel is allowed though. */
35601 else if (cclauses
35602 && (mask & (OMP_CLAUSE_MASK_1
35603 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35605 error_at (loc, "expected %<for%> after %qs", p_name);
35606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35607 return NULL_TREE;
35609 else if (!flag_openmp) /* flag_openmp_simd */
35611 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35612 return NULL_TREE;
35614 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35616 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35617 const char *p = IDENTIFIER_POINTER (id);
35618 if (strcmp (p, "sections") == 0)
35620 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35621 cclauses = cclauses_buf;
35623 cp_lexer_consume_token (parser->lexer);
35624 block = begin_omp_parallel ();
35625 save = cp_parser_begin_omp_structured_block (parser);
35626 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35627 cp_parser_end_omp_structured_block (parser, save);
35628 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35629 block);
35630 OMP_PARALLEL_COMBINED (stmt) = 1;
35631 return stmt;
35635 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35636 cclauses == NULL);
35637 if (cclauses)
35639 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35640 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35643 block = begin_omp_parallel ();
35644 save = cp_parser_begin_omp_structured_block (parser);
35645 cp_parser_statement (parser, NULL_TREE, false, if_p);
35646 cp_parser_end_omp_structured_block (parser, save);
35647 stmt = finish_omp_parallel (clauses, block);
35648 return stmt;
35651 /* OpenMP 2.5:
35652 # pragma omp single single-clause[optseq] new-line
35653 structured-block */
35655 #define OMP_SINGLE_CLAUSE_MASK \
35656 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35657 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35658 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35659 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35661 static tree
35662 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35664 tree stmt = make_node (OMP_SINGLE);
35665 TREE_TYPE (stmt) = void_type_node;
35667 OMP_SINGLE_CLAUSES (stmt)
35668 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35669 "#pragma omp single", pragma_tok);
35670 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35672 return add_stmt (stmt);
35675 /* OpenMP 3.0:
35676 # pragma omp task task-clause[optseq] new-line
35677 structured-block */
35679 #define OMP_TASK_CLAUSE_MASK \
35680 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35681 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35682 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35683 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35684 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35691 static tree
35692 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35694 tree clauses, block;
35695 unsigned int save;
35697 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35698 "#pragma omp task", pragma_tok);
35699 block = begin_omp_task ();
35700 save = cp_parser_begin_omp_structured_block (parser);
35701 cp_parser_statement (parser, NULL_TREE, false, if_p);
35702 cp_parser_end_omp_structured_block (parser, save);
35703 return finish_omp_task (clauses, block);
35706 /* OpenMP 3.0:
35707 # pragma omp taskwait new-line */
35709 static void
35710 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35712 cp_parser_require_pragma_eol (parser, pragma_tok);
35713 finish_omp_taskwait ();
35716 /* OpenMP 3.1:
35717 # pragma omp taskyield new-line */
35719 static void
35720 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35722 cp_parser_require_pragma_eol (parser, pragma_tok);
35723 finish_omp_taskyield ();
35726 /* OpenMP 4.0:
35727 # pragma omp taskgroup new-line
35728 structured-block */
35730 static tree
35731 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35733 cp_parser_require_pragma_eol (parser, pragma_tok);
35734 return c_finish_omp_taskgroup (input_location,
35735 cp_parser_omp_structured_block (parser,
35736 if_p));
35740 /* OpenMP 2.5:
35741 # pragma omp threadprivate (variable-list) */
35743 static void
35744 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35746 tree vars;
35748 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35749 cp_parser_require_pragma_eol (parser, pragma_tok);
35751 finish_omp_threadprivate (vars);
35754 /* OpenMP 4.0:
35755 # pragma omp cancel cancel-clause[optseq] new-line */
35757 #define OMP_CANCEL_CLAUSE_MASK \
35758 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35764 static void
35765 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35767 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35768 "#pragma omp cancel", pragma_tok);
35769 finish_omp_cancel (clauses);
35772 /* OpenMP 4.0:
35773 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35775 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35776 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35777 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35778 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35779 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35781 static void
35782 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35783 enum pragma_context context)
35785 tree clauses;
35786 bool point_seen = false;
35788 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35790 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35791 const char *p = IDENTIFIER_POINTER (id);
35793 if (strcmp (p, "point") == 0)
35795 cp_lexer_consume_token (parser->lexer);
35796 point_seen = true;
35799 if (!point_seen)
35801 cp_parser_error (parser, "expected %<point%>");
35802 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35803 return;
35806 if (context != pragma_compound)
35808 if (context == pragma_stmt)
35809 error_at (pragma_tok->location,
35810 "%<#pragma %s%> may only be used in compound statements",
35811 "omp cancellation point");
35812 else
35813 cp_parser_error (parser, "expected declaration specifiers");
35814 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35815 return;
35818 clauses = cp_parser_omp_all_clauses (parser,
35819 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35820 "#pragma omp cancellation point",
35821 pragma_tok);
35822 finish_omp_cancellation_point (clauses);
35825 /* OpenMP 4.0:
35826 #pragma omp distribute distribute-clause[optseq] new-line
35827 for-loop */
35829 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35830 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35831 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35836 static tree
35837 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35838 char *p_name, omp_clause_mask mask, tree *cclauses,
35839 bool *if_p)
35841 tree clauses, sb, ret;
35842 unsigned int save;
35843 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35845 strcat (p_name, " distribute");
35846 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35851 const char *p = IDENTIFIER_POINTER (id);
35852 bool simd = false;
35853 bool parallel = false;
35855 if (strcmp (p, "simd") == 0)
35856 simd = true;
35857 else
35858 parallel = strcmp (p, "parallel") == 0;
35859 if (parallel || simd)
35861 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35862 if (cclauses == NULL)
35863 cclauses = cclauses_buf;
35864 cp_lexer_consume_token (parser->lexer);
35865 if (!flag_openmp) /* flag_openmp_simd */
35867 if (simd)
35868 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35869 cclauses, if_p);
35870 else
35871 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35872 cclauses, if_p);
35874 sb = begin_omp_structured_block ();
35875 save = cp_parser_begin_omp_structured_block (parser);
35876 if (simd)
35877 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35878 cclauses, if_p);
35879 else
35880 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35881 cclauses, if_p);
35882 cp_parser_end_omp_structured_block (parser, save);
35883 tree body = finish_omp_structured_block (sb);
35884 if (ret == NULL)
35885 return ret;
35886 ret = make_node (OMP_DISTRIBUTE);
35887 TREE_TYPE (ret) = void_type_node;
35888 OMP_FOR_BODY (ret) = body;
35889 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35890 SET_EXPR_LOCATION (ret, loc);
35891 add_stmt (ret);
35892 return ret;
35895 if (!flag_openmp) /* flag_openmp_simd */
35897 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35898 return NULL_TREE;
35901 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35902 cclauses == NULL);
35903 if (cclauses)
35905 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35906 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35909 sb = begin_omp_structured_block ();
35910 save = cp_parser_begin_omp_structured_block (parser);
35912 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35914 cp_parser_end_omp_structured_block (parser, save);
35915 add_stmt (finish_omp_structured_block (sb));
35917 return ret;
35920 /* OpenMP 4.0:
35921 # pragma omp teams teams-clause[optseq] new-line
35922 structured-block */
35924 #define OMP_TEAMS_CLAUSE_MASK \
35925 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35926 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35927 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35928 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35929 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35930 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35933 static tree
35934 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35935 char *p_name, omp_clause_mask mask, tree *cclauses,
35936 bool *if_p)
35938 tree clauses, sb, ret;
35939 unsigned int save;
35940 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35942 strcat (p_name, " teams");
35943 mask |= OMP_TEAMS_CLAUSE_MASK;
35945 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35947 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35948 const char *p = IDENTIFIER_POINTER (id);
35949 if (strcmp (p, "distribute") == 0)
35951 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35952 if (cclauses == NULL)
35953 cclauses = cclauses_buf;
35955 cp_lexer_consume_token (parser->lexer);
35956 if (!flag_openmp) /* flag_openmp_simd */
35957 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35958 cclauses, if_p);
35959 sb = begin_omp_structured_block ();
35960 save = cp_parser_begin_omp_structured_block (parser);
35961 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35962 cclauses, if_p);
35963 cp_parser_end_omp_structured_block (parser, save);
35964 tree body = finish_omp_structured_block (sb);
35965 if (ret == NULL)
35966 return ret;
35967 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35968 ret = make_node (OMP_TEAMS);
35969 TREE_TYPE (ret) = void_type_node;
35970 OMP_TEAMS_CLAUSES (ret) = clauses;
35971 OMP_TEAMS_BODY (ret) = body;
35972 OMP_TEAMS_COMBINED (ret) = 1;
35973 SET_EXPR_LOCATION (ret, loc);
35974 return add_stmt (ret);
35977 if (!flag_openmp) /* flag_openmp_simd */
35979 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35980 return NULL_TREE;
35983 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35984 cclauses == NULL);
35985 if (cclauses)
35987 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35988 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35991 tree stmt = make_node (OMP_TEAMS);
35992 TREE_TYPE (stmt) = void_type_node;
35993 OMP_TEAMS_CLAUSES (stmt) = clauses;
35994 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35995 SET_EXPR_LOCATION (stmt, loc);
35997 return add_stmt (stmt);
36000 /* OpenMP 4.0:
36001 # pragma omp target data target-data-clause[optseq] new-line
36002 structured-block */
36004 #define OMP_TARGET_DATA_CLAUSE_MASK \
36005 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
36010 static tree
36011 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36013 tree clauses
36014 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
36015 "#pragma omp target data", pragma_tok);
36016 int map_seen = 0;
36017 for (tree *pc = &clauses; *pc;)
36019 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36020 switch (OMP_CLAUSE_MAP_KIND (*pc))
36022 case GOMP_MAP_TO:
36023 case GOMP_MAP_ALWAYS_TO:
36024 case GOMP_MAP_FROM:
36025 case GOMP_MAP_ALWAYS_FROM:
36026 case GOMP_MAP_TOFROM:
36027 case GOMP_MAP_ALWAYS_TOFROM:
36028 case GOMP_MAP_ALLOC:
36029 map_seen = 3;
36030 break;
36031 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36032 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36033 case GOMP_MAP_ALWAYS_POINTER:
36034 break;
36035 default:
36036 map_seen |= 1;
36037 error_at (OMP_CLAUSE_LOCATION (*pc),
36038 "%<#pragma omp target data%> with map-type other "
36039 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36040 "on %<map%> clause");
36041 *pc = OMP_CLAUSE_CHAIN (*pc);
36042 continue;
36044 pc = &OMP_CLAUSE_CHAIN (*pc);
36047 if (map_seen != 3)
36049 if (map_seen == 0)
36050 error_at (pragma_tok->location,
36051 "%<#pragma omp target data%> must contain at least "
36052 "one %<map%> clause");
36053 return NULL_TREE;
36056 tree stmt = make_node (OMP_TARGET_DATA);
36057 TREE_TYPE (stmt) = void_type_node;
36058 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
36060 keep_next_level (true);
36061 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36063 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36064 return add_stmt (stmt);
36067 /* OpenMP 4.5:
36068 # pragma omp target enter data target-enter-data-clause[optseq] new-line
36069 structured-block */
36071 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
36072 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36073 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36074 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36075 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36076 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36078 static tree
36079 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
36080 enum pragma_context context)
36082 bool data_seen = false;
36083 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36086 const char *p = IDENTIFIER_POINTER (id);
36088 if (strcmp (p, "data") == 0)
36090 cp_lexer_consume_token (parser->lexer);
36091 data_seen = true;
36094 if (!data_seen)
36096 cp_parser_error (parser, "expected %<data%>");
36097 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36098 return NULL_TREE;
36101 if (context == pragma_stmt)
36103 error_at (pragma_tok->location,
36104 "%<#pragma %s%> may only be used in compound statements",
36105 "omp target enter data");
36106 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36107 return NULL_TREE;
36110 tree clauses
36111 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
36112 "#pragma omp target enter data", pragma_tok);
36113 int map_seen = 0;
36114 for (tree *pc = &clauses; *pc;)
36116 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36117 switch (OMP_CLAUSE_MAP_KIND (*pc))
36119 case GOMP_MAP_TO:
36120 case GOMP_MAP_ALWAYS_TO:
36121 case GOMP_MAP_ALLOC:
36122 map_seen = 3;
36123 break;
36124 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36125 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36126 case GOMP_MAP_ALWAYS_POINTER:
36127 break;
36128 default:
36129 map_seen |= 1;
36130 error_at (OMP_CLAUSE_LOCATION (*pc),
36131 "%<#pragma omp target enter data%> with map-type other "
36132 "than %<to%> or %<alloc%> on %<map%> clause");
36133 *pc = OMP_CLAUSE_CHAIN (*pc);
36134 continue;
36136 pc = &OMP_CLAUSE_CHAIN (*pc);
36139 if (map_seen != 3)
36141 if (map_seen == 0)
36142 error_at (pragma_tok->location,
36143 "%<#pragma omp target enter data%> must contain at least "
36144 "one %<map%> clause");
36145 return NULL_TREE;
36148 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
36149 TREE_TYPE (stmt) = void_type_node;
36150 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
36151 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36152 return add_stmt (stmt);
36155 /* OpenMP 4.5:
36156 # pragma omp target exit data target-enter-data-clause[optseq] new-line
36157 structured-block */
36159 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
36160 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36161 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36162 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36163 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36164 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36166 static tree
36167 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
36168 enum pragma_context context)
36170 bool data_seen = false;
36171 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36173 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36174 const char *p = IDENTIFIER_POINTER (id);
36176 if (strcmp (p, "data") == 0)
36178 cp_lexer_consume_token (parser->lexer);
36179 data_seen = true;
36182 if (!data_seen)
36184 cp_parser_error (parser, "expected %<data%>");
36185 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36186 return NULL_TREE;
36189 if (context == pragma_stmt)
36191 error_at (pragma_tok->location,
36192 "%<#pragma %s%> may only be used in compound statements",
36193 "omp target exit data");
36194 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36195 return NULL_TREE;
36198 tree clauses
36199 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
36200 "#pragma omp target exit data", pragma_tok);
36201 int map_seen = 0;
36202 for (tree *pc = &clauses; *pc;)
36204 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36205 switch (OMP_CLAUSE_MAP_KIND (*pc))
36207 case GOMP_MAP_FROM:
36208 case GOMP_MAP_ALWAYS_FROM:
36209 case GOMP_MAP_RELEASE:
36210 case GOMP_MAP_DELETE:
36211 map_seen = 3;
36212 break;
36213 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36214 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36215 case GOMP_MAP_ALWAYS_POINTER:
36216 break;
36217 default:
36218 map_seen |= 1;
36219 error_at (OMP_CLAUSE_LOCATION (*pc),
36220 "%<#pragma omp target exit data%> with map-type other "
36221 "than %<from%>, %<release%> or %<delete%> on %<map%>"
36222 " clause");
36223 *pc = OMP_CLAUSE_CHAIN (*pc);
36224 continue;
36226 pc = &OMP_CLAUSE_CHAIN (*pc);
36229 if (map_seen != 3)
36231 if (map_seen == 0)
36232 error_at (pragma_tok->location,
36233 "%<#pragma omp target exit data%> must contain at least "
36234 "one %<map%> clause");
36235 return NULL_TREE;
36238 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
36239 TREE_TYPE (stmt) = void_type_node;
36240 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36241 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36242 return add_stmt (stmt);
36245 /* OpenMP 4.0:
36246 # pragma omp target update target-update-clause[optseq] new-line */
36248 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36249 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36250 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36251 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36252 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36253 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36254 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36256 static bool
36257 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36258 enum pragma_context context)
36260 if (context == pragma_stmt)
36262 error_at (pragma_tok->location,
36263 "%<#pragma %s%> may only be used in compound statements",
36264 "omp target update");
36265 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36266 return false;
36269 tree clauses
36270 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36271 "#pragma omp target update", pragma_tok);
36272 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36273 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36275 error_at (pragma_tok->location,
36276 "%<#pragma omp target update%> must contain at least one "
36277 "%<from%> or %<to%> clauses");
36278 return false;
36281 tree stmt = make_node (OMP_TARGET_UPDATE);
36282 TREE_TYPE (stmt) = void_type_node;
36283 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36284 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36285 add_stmt (stmt);
36286 return false;
36289 /* OpenMP 4.0:
36290 # pragma omp target target-clause[optseq] new-line
36291 structured-block */
36293 #define OMP_TARGET_CLAUSE_MASK \
36294 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36304 static bool
36305 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36306 enum pragma_context context, bool *if_p)
36308 tree *pc = NULL, stmt;
36310 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36312 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36313 const char *p = IDENTIFIER_POINTER (id);
36314 enum tree_code ccode = ERROR_MARK;
36316 if (strcmp (p, "teams") == 0)
36317 ccode = OMP_TEAMS;
36318 else if (strcmp (p, "parallel") == 0)
36319 ccode = OMP_PARALLEL;
36320 else if (strcmp (p, "simd") == 0)
36321 ccode = OMP_SIMD;
36322 if (ccode != ERROR_MARK)
36324 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36325 char p_name[sizeof ("#pragma omp target teams distribute "
36326 "parallel for simd")];
36328 cp_lexer_consume_token (parser->lexer);
36329 strcpy (p_name, "#pragma omp target");
36330 if (!flag_openmp) /* flag_openmp_simd */
36332 tree stmt;
36333 switch (ccode)
36335 case OMP_TEAMS:
36336 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36337 OMP_TARGET_CLAUSE_MASK,
36338 cclauses, if_p);
36339 break;
36340 case OMP_PARALLEL:
36341 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36342 OMP_TARGET_CLAUSE_MASK,
36343 cclauses, if_p);
36344 break;
36345 case OMP_SIMD:
36346 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36347 OMP_TARGET_CLAUSE_MASK,
36348 cclauses, if_p);
36349 break;
36350 default:
36351 gcc_unreachable ();
36353 return stmt != NULL_TREE;
36355 keep_next_level (true);
36356 tree sb = begin_omp_structured_block (), ret;
36357 unsigned save = cp_parser_begin_omp_structured_block (parser);
36358 switch (ccode)
36360 case OMP_TEAMS:
36361 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36362 OMP_TARGET_CLAUSE_MASK, cclauses,
36363 if_p);
36364 break;
36365 case OMP_PARALLEL:
36366 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36367 OMP_TARGET_CLAUSE_MASK, cclauses,
36368 if_p);
36369 break;
36370 case OMP_SIMD:
36371 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36372 OMP_TARGET_CLAUSE_MASK, cclauses,
36373 if_p);
36374 break;
36375 default:
36376 gcc_unreachable ();
36378 cp_parser_end_omp_structured_block (parser, save);
36379 tree body = finish_omp_structured_block (sb);
36380 if (ret == NULL_TREE)
36381 return false;
36382 if (ccode == OMP_TEAMS && !processing_template_decl)
36384 /* For combined target teams, ensure the num_teams and
36385 thread_limit clause expressions are evaluated on the host,
36386 before entering the target construct. */
36387 tree c;
36388 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36389 c; c = OMP_CLAUSE_CHAIN (c))
36390 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36391 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36392 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36394 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36395 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36396 if (expr == error_mark_node)
36397 continue;
36398 tree tmp = TARGET_EXPR_SLOT (expr);
36399 add_stmt (expr);
36400 OMP_CLAUSE_OPERAND (c, 0) = expr;
36401 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36402 OMP_CLAUSE_FIRSTPRIVATE);
36403 OMP_CLAUSE_DECL (tc) = tmp;
36404 OMP_CLAUSE_CHAIN (tc)
36405 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36406 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36409 tree stmt = make_node (OMP_TARGET);
36410 TREE_TYPE (stmt) = void_type_node;
36411 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36412 OMP_TARGET_BODY (stmt) = body;
36413 OMP_TARGET_COMBINED (stmt) = 1;
36414 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36415 add_stmt (stmt);
36416 pc = &OMP_TARGET_CLAUSES (stmt);
36417 goto check_clauses;
36419 else if (!flag_openmp) /* flag_openmp_simd */
36421 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36422 return false;
36424 else if (strcmp (p, "data") == 0)
36426 cp_lexer_consume_token (parser->lexer);
36427 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36428 return true;
36430 else if (strcmp (p, "enter") == 0)
36432 cp_lexer_consume_token (parser->lexer);
36433 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36434 return false;
36436 else if (strcmp (p, "exit") == 0)
36438 cp_lexer_consume_token (parser->lexer);
36439 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36440 return false;
36442 else if (strcmp (p, "update") == 0)
36444 cp_lexer_consume_token (parser->lexer);
36445 return cp_parser_omp_target_update (parser, pragma_tok, context);
36448 if (!flag_openmp) /* flag_openmp_simd */
36450 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36451 return false;
36454 stmt = make_node (OMP_TARGET);
36455 TREE_TYPE (stmt) = void_type_node;
36457 OMP_TARGET_CLAUSES (stmt)
36458 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36459 "#pragma omp target", pragma_tok);
36460 pc = &OMP_TARGET_CLAUSES (stmt);
36461 keep_next_level (true);
36462 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36464 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36465 add_stmt (stmt);
36467 check_clauses:
36468 while (*pc)
36470 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36471 switch (OMP_CLAUSE_MAP_KIND (*pc))
36473 case GOMP_MAP_TO:
36474 case GOMP_MAP_ALWAYS_TO:
36475 case GOMP_MAP_FROM:
36476 case GOMP_MAP_ALWAYS_FROM:
36477 case GOMP_MAP_TOFROM:
36478 case GOMP_MAP_ALWAYS_TOFROM:
36479 case GOMP_MAP_ALLOC:
36480 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36481 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36482 case GOMP_MAP_ALWAYS_POINTER:
36483 break;
36484 default:
36485 error_at (OMP_CLAUSE_LOCATION (*pc),
36486 "%<#pragma omp target%> with map-type other "
36487 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36488 "on %<map%> clause");
36489 *pc = OMP_CLAUSE_CHAIN (*pc);
36490 continue;
36492 pc = &OMP_CLAUSE_CHAIN (*pc);
36494 return true;
36497 /* OpenACC 2.0:
36498 # pragma acc cache (variable-list) new-line
36501 static tree
36502 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36504 tree stmt, clauses;
36506 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36507 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36509 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36511 stmt = make_node (OACC_CACHE);
36512 TREE_TYPE (stmt) = void_type_node;
36513 OACC_CACHE_CLAUSES (stmt) = clauses;
36514 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36515 add_stmt (stmt);
36517 return stmt;
36520 /* OpenACC 2.0:
36521 # pragma acc data oacc-data-clause[optseq] new-line
36522 structured-block */
36524 #define OACC_DATA_CLAUSE_MASK \
36525 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36526 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36527 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36528 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36529 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36530 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36531 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36532 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36533 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36534 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36535 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36537 static tree
36538 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36540 tree stmt, clauses, block;
36541 unsigned int save;
36543 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36544 "#pragma acc data", pragma_tok);
36546 block = begin_omp_parallel ();
36547 save = cp_parser_begin_omp_structured_block (parser);
36548 cp_parser_statement (parser, NULL_TREE, false, if_p);
36549 cp_parser_end_omp_structured_block (parser, save);
36550 stmt = finish_oacc_data (clauses, block);
36551 return stmt;
36554 /* OpenACC 2.0:
36555 # pragma acc host_data <clauses> new-line
36556 structured-block */
36558 #define OACC_HOST_DATA_CLAUSE_MASK \
36559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36561 static tree
36562 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36564 tree stmt, clauses, block;
36565 unsigned int save;
36567 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36568 "#pragma acc host_data", pragma_tok);
36570 block = begin_omp_parallel ();
36571 save = cp_parser_begin_omp_structured_block (parser);
36572 cp_parser_statement (parser, NULL_TREE, false, if_p);
36573 cp_parser_end_omp_structured_block (parser, save);
36574 stmt = finish_oacc_host_data (clauses, block);
36575 return stmt;
36578 /* OpenACC 2.0:
36579 # pragma acc declare oacc-data-clause[optseq] new-line
36582 #define OACC_DECLARE_CLAUSE_MASK \
36583 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36596 static tree
36597 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36599 tree clauses, stmt;
36600 bool error = false;
36602 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36603 "#pragma acc declare", pragma_tok, true);
36606 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36608 error_at (pragma_tok->location,
36609 "no valid clauses specified in %<#pragma acc declare%>");
36610 return NULL_TREE;
36613 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36615 location_t loc = OMP_CLAUSE_LOCATION (t);
36616 tree decl = OMP_CLAUSE_DECL (t);
36617 if (!DECL_P (decl))
36619 error_at (loc, "array section in %<#pragma acc declare%>");
36620 error = true;
36621 continue;
36623 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36624 switch (OMP_CLAUSE_MAP_KIND (t))
36626 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36627 case GOMP_MAP_FORCE_ALLOC:
36628 case GOMP_MAP_FORCE_TO:
36629 case GOMP_MAP_FORCE_DEVICEPTR:
36630 case GOMP_MAP_DEVICE_RESIDENT:
36631 break;
36633 case GOMP_MAP_LINK:
36634 if (!global_bindings_p ()
36635 && (TREE_STATIC (decl)
36636 || !DECL_EXTERNAL (decl)))
36638 error_at (loc,
36639 "%qD must be a global variable in "
36640 "%<#pragma acc declare link%>",
36641 decl);
36642 error = true;
36643 continue;
36645 break;
36647 default:
36648 if (global_bindings_p ())
36650 error_at (loc, "invalid OpenACC clause at file scope");
36651 error = true;
36652 continue;
36654 if (DECL_EXTERNAL (decl))
36656 error_at (loc,
36657 "invalid use of %<extern%> variable %qD "
36658 "in %<#pragma acc declare%>", decl);
36659 error = true;
36660 continue;
36662 else if (TREE_PUBLIC (decl))
36664 error_at (loc,
36665 "invalid use of %<global%> variable %qD "
36666 "in %<#pragma acc declare%>", decl);
36667 error = true;
36668 continue;
36670 break;
36673 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36674 || lookup_attribute ("omp declare target link",
36675 DECL_ATTRIBUTES (decl)))
36677 error_at (loc, "variable %qD used more than once with "
36678 "%<#pragma acc declare%>", decl);
36679 error = true;
36680 continue;
36683 if (!error)
36685 tree id;
36687 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36688 id = get_identifier ("omp declare target link");
36689 else
36690 id = get_identifier ("omp declare target");
36692 DECL_ATTRIBUTES (decl)
36693 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36694 if (global_bindings_p ())
36696 symtab_node *node = symtab_node::get (decl);
36697 if (node != NULL)
36699 node->offloadable = 1;
36700 if (ENABLE_OFFLOADING)
36702 g->have_offload = true;
36703 if (is_a <varpool_node *> (node))
36704 vec_safe_push (offload_vars, decl);
36711 if (error || global_bindings_p ())
36712 return NULL_TREE;
36714 stmt = make_node (OACC_DECLARE);
36715 TREE_TYPE (stmt) = void_type_node;
36716 OACC_DECLARE_CLAUSES (stmt) = clauses;
36717 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36719 add_stmt (stmt);
36721 return NULL_TREE;
36724 /* OpenACC 2.0:
36725 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36729 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36731 LOC is the location of the #pragma token.
36734 #define OACC_ENTER_DATA_CLAUSE_MASK \
36735 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36736 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36737 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36738 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36739 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36740 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36741 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36743 #define OACC_EXIT_DATA_CLAUSE_MASK \
36744 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36745 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36746 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36747 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36748 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36750 static tree
36751 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36752 bool enter)
36754 location_t loc = pragma_tok->location;
36755 tree stmt, clauses;
36756 const char *p = "";
36758 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36759 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36761 if (strcmp (p, "data") != 0)
36763 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36764 enter ? "enter" : "exit");
36765 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36766 return NULL_TREE;
36769 cp_lexer_consume_token (parser->lexer);
36771 if (enter)
36772 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36773 "#pragma acc enter data", pragma_tok);
36774 else
36775 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36776 "#pragma acc exit data", pragma_tok);
36778 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36780 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36781 enter ? "enter" : "exit");
36782 return NULL_TREE;
36785 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36786 TREE_TYPE (stmt) = void_type_node;
36787 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36788 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36789 add_stmt (stmt);
36790 return stmt;
36793 /* OpenACC 2.0:
36794 # pragma acc loop oacc-loop-clause[optseq] new-line
36795 structured-block */
36797 #define OACC_LOOP_CLAUSE_MASK \
36798 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36799 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36800 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36801 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36802 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36803 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36804 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36805 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36806 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36807 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36809 static tree
36810 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36811 omp_clause_mask mask, tree *cclauses, bool *if_p)
36813 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36815 strcat (p_name, " loop");
36816 mask |= OACC_LOOP_CLAUSE_MASK;
36818 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36819 cclauses == NULL);
36820 if (cclauses)
36822 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36823 if (*cclauses)
36824 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36825 if (clauses)
36826 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36829 tree block = begin_omp_structured_block ();
36830 int save = cp_parser_begin_omp_structured_block (parser);
36831 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36832 cp_parser_end_omp_structured_block (parser, save);
36833 add_stmt (finish_omp_structured_block (block));
36835 return stmt;
36838 /* OpenACC 2.0:
36839 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36840 structured-block
36844 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36845 structured-block
36848 #define OACC_KERNELS_CLAUSE_MASK \
36849 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36850 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36851 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36852 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36853 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36854 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36855 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36856 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36857 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36858 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36859 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36860 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36861 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36862 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36863 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36864 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36865 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36867 #define OACC_PARALLEL_CLAUSE_MASK \
36868 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36869 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36875 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36876 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36877 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36878 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36879 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36880 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36881 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36882 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36883 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36884 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36885 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36886 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36887 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36889 static tree
36890 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36891 char *p_name, bool *if_p)
36893 omp_clause_mask mask;
36894 enum tree_code code;
36895 switch (cp_parser_pragma_kind (pragma_tok))
36897 case PRAGMA_OACC_KERNELS:
36898 strcat (p_name, " kernels");
36899 mask = OACC_KERNELS_CLAUSE_MASK;
36900 code = OACC_KERNELS;
36901 break;
36902 case PRAGMA_OACC_PARALLEL:
36903 strcat (p_name, " parallel");
36904 mask = OACC_PARALLEL_CLAUSE_MASK;
36905 code = OACC_PARALLEL;
36906 break;
36907 default:
36908 gcc_unreachable ();
36911 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36913 const char *p
36914 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36915 if (strcmp (p, "loop") == 0)
36917 cp_lexer_consume_token (parser->lexer);
36918 tree block = begin_omp_parallel ();
36919 tree clauses;
36920 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36921 if_p);
36922 return finish_omp_construct (code, block, clauses);
36926 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36928 tree block = begin_omp_parallel ();
36929 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36930 cp_parser_statement (parser, NULL_TREE, false, if_p);
36931 cp_parser_end_omp_structured_block (parser, save);
36932 return finish_omp_construct (code, block, clauses);
36935 /* OpenACC 2.0:
36936 # pragma acc update oacc-update-clause[optseq] new-line
36939 #define OACC_UPDATE_CLAUSE_MASK \
36940 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36941 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36942 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36943 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36945 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36947 static tree
36948 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36950 tree stmt, clauses;
36952 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36953 "#pragma acc update", pragma_tok);
36955 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36957 error_at (pragma_tok->location,
36958 "%<#pragma acc update%> must contain at least one "
36959 "%<device%> or %<host%> or %<self%> clause");
36960 return NULL_TREE;
36963 stmt = make_node (OACC_UPDATE);
36964 TREE_TYPE (stmt) = void_type_node;
36965 OACC_UPDATE_CLAUSES (stmt) = clauses;
36966 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36967 add_stmt (stmt);
36968 return stmt;
36971 /* OpenACC 2.0:
36972 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36974 LOC is the location of the #pragma token.
36977 #define OACC_WAIT_CLAUSE_MASK \
36978 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36980 static tree
36981 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36983 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36986 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36987 list = cp_parser_oacc_wait_list (parser, loc, list);
36989 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36990 "#pragma acc wait", pragma_tok);
36992 stmt = c_finish_oacc_wait (loc, list, clauses);
36993 stmt = finish_expr_stmt (stmt);
36995 return stmt;
36998 /* OpenMP 4.0:
36999 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
37001 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
37002 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
37003 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
37004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
37005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
37006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
37007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
37009 static void
37010 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
37011 enum pragma_context context)
37013 bool first_p = parser->omp_declare_simd == NULL;
37014 cp_omp_declare_simd_data data;
37015 if (first_p)
37017 data.error_seen = false;
37018 data.fndecl_seen = false;
37019 data.tokens = vNULL;
37020 data.clauses = NULL_TREE;
37021 /* It is safe to take the address of a local variable; it will only be
37022 used while this scope is live. */
37023 parser->omp_declare_simd = &data;
37026 /* Store away all pragma tokens. */
37027 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37028 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37029 cp_lexer_consume_token (parser->lexer);
37030 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37031 parser->omp_declare_simd->error_seen = true;
37032 cp_parser_require_pragma_eol (parser, pragma_tok);
37033 struct cp_token_cache *cp
37034 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37035 parser->omp_declare_simd->tokens.safe_push (cp);
37037 if (first_p)
37039 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37040 cp_parser_pragma (parser, context, NULL);
37041 switch (context)
37043 case pragma_external:
37044 cp_parser_declaration (parser);
37045 break;
37046 case pragma_member:
37047 cp_parser_member_declaration (parser);
37048 break;
37049 case pragma_objc_icode:
37050 cp_parser_block_declaration (parser, /*statement_p=*/false);
37051 break;
37052 default:
37053 cp_parser_declaration_statement (parser);
37054 break;
37056 if (parser->omp_declare_simd
37057 && !parser->omp_declare_simd->error_seen
37058 && !parser->omp_declare_simd->fndecl_seen)
37059 error_at (pragma_tok->location,
37060 "%<#pragma omp declare simd%> not immediately followed by "
37061 "function declaration or definition");
37062 data.tokens.release ();
37063 parser->omp_declare_simd = NULL;
37067 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
37068 This function is modelled similar to the late parsing of omp declare
37069 simd. */
37071 static tree
37072 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
37074 struct cp_token_cache *ce;
37075 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
37076 int ii = 0;
37078 if (parser->omp_declare_simd != NULL
37079 || lookup_attribute ("simd", attrs))
37081 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
37082 "used in the same function marked as a Cilk Plus SIMD-enabled "
37083 "function");
37084 parser->cilk_simd_fn_info->tokens.release ();
37085 XDELETE (parser->cilk_simd_fn_info);
37086 parser->cilk_simd_fn_info = NULL;
37087 return attrs;
37089 if (!info->error_seen && info->fndecl_seen)
37091 error ("vector attribute not immediately followed by a single function"
37092 " declaration or definition");
37093 info->error_seen = true;
37095 if (info->error_seen)
37096 return attrs;
37098 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
37100 tree c, cl;
37102 cp_parser_push_lexer_for_tokens (parser, ce);
37103 parser->lexer->in_pragma = true;
37104 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
37105 "SIMD-enabled functions attribute",
37106 NULL);
37107 cp_parser_pop_lexer (parser);
37108 if (cl)
37109 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37111 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
37112 TREE_CHAIN (c) = attrs;
37113 attrs = c;
37115 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37116 TREE_CHAIN (c) = attrs;
37117 if (processing_template_decl)
37118 ATTR_IS_DEPENDENT (c) = 1;
37119 attrs = c;
37121 info->fndecl_seen = true;
37122 parser->cilk_simd_fn_info->tokens.release ();
37123 XDELETE (parser->cilk_simd_fn_info);
37124 parser->cilk_simd_fn_info = NULL;
37125 return attrs;
37128 /* Finalize #pragma omp declare simd clauses after direct declarator has
37129 been parsed, and put that into "omp declare simd" attribute. */
37131 static tree
37132 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
37134 struct cp_token_cache *ce;
37135 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
37136 int i;
37138 if (!data->error_seen && data->fndecl_seen)
37140 error ("%<#pragma omp declare simd%> not immediately followed by "
37141 "a single function declaration or definition");
37142 data->error_seen = true;
37144 if (data->error_seen)
37145 return attrs;
37147 FOR_EACH_VEC_ELT (data->tokens, i, ce)
37149 tree c, cl;
37151 cp_parser_push_lexer_for_tokens (parser, ce);
37152 parser->lexer->in_pragma = true;
37153 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37154 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37155 cp_lexer_consume_token (parser->lexer);
37156 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
37157 "#pragma omp declare simd", pragma_tok);
37158 cp_parser_pop_lexer (parser);
37159 if (cl)
37160 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
37161 c = build_tree_list (get_identifier ("omp declare simd"), cl);
37162 TREE_CHAIN (c) = attrs;
37163 if (processing_template_decl)
37164 ATTR_IS_DEPENDENT (c) = 1;
37165 attrs = c;
37168 data->fndecl_seen = true;
37169 return attrs;
37173 /* OpenMP 4.0:
37174 # pragma omp declare target new-line
37175 declarations and definitions
37176 # pragma omp end declare target new-line
37178 OpenMP 4.5:
37179 # pragma omp declare target ( extended-list ) new-line
37181 # pragma omp declare target declare-target-clauses[seq] new-line */
37183 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
37184 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
37185 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
37187 static void
37188 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
37190 tree clauses = NULL_TREE;
37191 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37192 clauses
37193 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
37194 "#pragma omp declare target", pragma_tok);
37195 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37197 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
37198 clauses);
37199 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
37200 cp_parser_require_pragma_eol (parser, pragma_tok);
37202 else
37204 cp_parser_require_pragma_eol (parser, pragma_tok);
37205 scope_chain->omp_declare_target_attribute++;
37206 return;
37208 if (scope_chain->omp_declare_target_attribute)
37209 error_at (pragma_tok->location,
37210 "%<#pragma omp declare target%> with clauses in between "
37211 "%<#pragma omp declare target%> without clauses and "
37212 "%<#pragma omp end declare target%>");
37213 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
37215 tree t = OMP_CLAUSE_DECL (c), id;
37216 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
37217 tree at2 = lookup_attribute ("omp declare target link",
37218 DECL_ATTRIBUTES (t));
37219 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
37221 id = get_identifier ("omp declare target link");
37222 std::swap (at1, at2);
37224 else
37225 id = get_identifier ("omp declare target");
37226 if (at2)
37228 error_at (OMP_CLAUSE_LOCATION (c),
37229 "%qD specified both in declare target %<link%> and %<to%>"
37230 " clauses", t);
37231 continue;
37233 if (!at1)
37235 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
37236 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
37237 continue;
37239 symtab_node *node = symtab_node::get (t);
37240 if (node != NULL)
37242 node->offloadable = 1;
37243 if (ENABLE_OFFLOADING)
37245 g->have_offload = true;
37246 if (is_a <varpool_node *> (node))
37247 vec_safe_push (offload_vars, t);
37254 static void
37255 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37257 const char *p = "";
37258 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37260 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37261 p = IDENTIFIER_POINTER (id);
37263 if (strcmp (p, "declare") == 0)
37265 cp_lexer_consume_token (parser->lexer);
37266 p = "";
37267 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37269 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37270 p = IDENTIFIER_POINTER (id);
37272 if (strcmp (p, "target") == 0)
37273 cp_lexer_consume_token (parser->lexer);
37274 else
37276 cp_parser_error (parser, "expected %<target%>");
37277 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37278 return;
37281 else
37283 cp_parser_error (parser, "expected %<declare%>");
37284 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37285 return;
37287 cp_parser_require_pragma_eol (parser, pragma_tok);
37288 if (!scope_chain->omp_declare_target_attribute)
37289 error_at (pragma_tok->location,
37290 "%<#pragma omp end declare target%> without corresponding "
37291 "%<#pragma omp declare target%>");
37292 else
37293 scope_chain->omp_declare_target_attribute--;
37296 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37297 expression and optional initializer clause of
37298 #pragma omp declare reduction. We store the expression(s) as
37299 either 3, 6 or 7 special statements inside of the artificial function's
37300 body. The first two statements are DECL_EXPRs for the artificial
37301 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37302 expression that uses those variables.
37303 If there was any INITIALIZER clause, this is followed by further statements,
37304 the fourth and fifth statements are DECL_EXPRs for the artificial
37305 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37306 constructor variant (first token after open paren is not omp_priv),
37307 then the sixth statement is a statement with the function call expression
37308 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37309 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37310 to initialize the OMP_PRIV artificial variable and there is seventh
37311 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37313 static bool
37314 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37316 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37317 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37318 type = TREE_TYPE (type);
37319 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37320 DECL_ARTIFICIAL (omp_out) = 1;
37321 pushdecl (omp_out);
37322 add_decl_expr (omp_out);
37323 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37324 DECL_ARTIFICIAL (omp_in) = 1;
37325 pushdecl (omp_in);
37326 add_decl_expr (omp_in);
37327 tree combiner;
37328 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37330 keep_next_level (true);
37331 tree block = begin_omp_structured_block ();
37332 combiner = cp_parser_expression (parser);
37333 finish_expr_stmt (combiner);
37334 block = finish_omp_structured_block (block);
37335 add_stmt (block);
37337 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37338 return false;
37340 const char *p = "";
37341 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37343 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37344 p = IDENTIFIER_POINTER (id);
37347 if (strcmp (p, "initializer") == 0)
37349 cp_lexer_consume_token (parser->lexer);
37350 matching_parens parens;
37351 if (!parens.require_open (parser))
37352 return false;
37354 p = "";
37355 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37357 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37358 p = IDENTIFIER_POINTER (id);
37361 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37362 DECL_ARTIFICIAL (omp_priv) = 1;
37363 pushdecl (omp_priv);
37364 add_decl_expr (omp_priv);
37365 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37366 DECL_ARTIFICIAL (omp_orig) = 1;
37367 pushdecl (omp_orig);
37368 add_decl_expr (omp_orig);
37370 keep_next_level (true);
37371 block = begin_omp_structured_block ();
37373 bool ctor = false;
37374 if (strcmp (p, "omp_priv") == 0)
37376 bool is_direct_init, is_non_constant_init;
37377 ctor = true;
37378 cp_lexer_consume_token (parser->lexer);
37379 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37380 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37381 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37382 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37383 == CPP_CLOSE_PAREN
37384 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37385 == CPP_CLOSE_PAREN))
37387 finish_omp_structured_block (block);
37388 error ("invalid initializer clause");
37389 return false;
37391 initializer = cp_parser_initializer (parser, &is_direct_init,
37392 &is_non_constant_init);
37393 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37394 NULL_TREE, LOOKUP_ONLYCONVERTING);
37396 else
37398 cp_parser_parse_tentatively (parser);
37399 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37400 /*check_dependency_p=*/true,
37401 /*template_p=*/NULL,
37402 /*declarator_p=*/false,
37403 /*optional_p=*/false);
37404 vec<tree, va_gc> *args;
37405 if (fn_name == error_mark_node
37406 || cp_parser_error_occurred (parser)
37407 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37408 || ((args = cp_parser_parenthesized_expression_list
37409 (parser, non_attr, /*cast_p=*/false,
37410 /*allow_expansion_p=*/true,
37411 /*non_constant_p=*/NULL)),
37412 cp_parser_error_occurred (parser)))
37414 finish_omp_structured_block (block);
37415 cp_parser_abort_tentative_parse (parser);
37416 cp_parser_error (parser, "expected id-expression (arguments)");
37417 return false;
37419 unsigned int i;
37420 tree arg;
37421 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37422 if (arg == omp_priv
37423 || (TREE_CODE (arg) == ADDR_EXPR
37424 && TREE_OPERAND (arg, 0) == omp_priv))
37425 break;
37426 cp_parser_abort_tentative_parse (parser);
37427 if (arg == NULL_TREE)
37428 error ("one of the initializer call arguments should be %<omp_priv%>"
37429 " or %<&omp_priv%>");
37430 initializer = cp_parser_postfix_expression (parser, false, false, false,
37431 false, NULL);
37432 finish_expr_stmt (initializer);
37435 block = finish_omp_structured_block (block);
37436 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37437 add_stmt (block);
37439 if (ctor)
37440 add_decl_expr (omp_orig);
37442 if (!parens.require_close (parser))
37443 return false;
37446 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37447 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false,
37448 UNKNOWN_LOCATION);
37450 return true;
37453 /* OpenMP 4.0
37454 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37455 initializer-clause[opt] new-line
37457 initializer-clause:
37458 initializer (omp_priv initializer)
37459 initializer (function-name (argument-list)) */
37461 static void
37462 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37463 enum pragma_context)
37465 auto_vec<tree> types;
37466 enum tree_code reduc_code = ERROR_MARK;
37467 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37468 unsigned int i;
37469 cp_token *first_token;
37470 cp_token_cache *cp;
37471 int errs;
37472 void *p;
37474 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37475 p = obstack_alloc (&declarator_obstack, 0);
37477 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37478 goto fail;
37480 switch (cp_lexer_peek_token (parser->lexer)->type)
37482 case CPP_PLUS:
37483 reduc_code = PLUS_EXPR;
37484 break;
37485 case CPP_MULT:
37486 reduc_code = MULT_EXPR;
37487 break;
37488 case CPP_MINUS:
37489 reduc_code = MINUS_EXPR;
37490 break;
37491 case CPP_AND:
37492 reduc_code = BIT_AND_EXPR;
37493 break;
37494 case CPP_XOR:
37495 reduc_code = BIT_XOR_EXPR;
37496 break;
37497 case CPP_OR:
37498 reduc_code = BIT_IOR_EXPR;
37499 break;
37500 case CPP_AND_AND:
37501 reduc_code = TRUTH_ANDIF_EXPR;
37502 break;
37503 case CPP_OR_OR:
37504 reduc_code = TRUTH_ORIF_EXPR;
37505 break;
37506 case CPP_NAME:
37507 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37508 break;
37509 default:
37510 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37511 "%<|%>, %<&&%>, %<||%> or identifier");
37512 goto fail;
37515 if (reduc_code != ERROR_MARK)
37516 cp_lexer_consume_token (parser->lexer);
37518 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37519 if (reduc_id == error_mark_node)
37520 goto fail;
37522 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37523 goto fail;
37525 /* Types may not be defined in declare reduction type list. */
37526 const char *saved_message;
37527 saved_message = parser->type_definition_forbidden_message;
37528 parser->type_definition_forbidden_message
37529 = G_("types may not be defined in declare reduction type list");
37530 bool saved_colon_corrects_to_scope_p;
37531 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37532 parser->colon_corrects_to_scope_p = false;
37533 bool saved_colon_doesnt_start_class_def_p;
37534 saved_colon_doesnt_start_class_def_p
37535 = parser->colon_doesnt_start_class_def_p;
37536 parser->colon_doesnt_start_class_def_p = true;
37538 while (true)
37540 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37541 type = cp_parser_type_id (parser);
37542 if (type == error_mark_node)
37544 else if (ARITHMETIC_TYPE_P (type)
37545 && (orig_reduc_id == NULL_TREE
37546 || (TREE_CODE (type) != COMPLEX_TYPE
37547 && (id_equal (orig_reduc_id, "min")
37548 || id_equal (orig_reduc_id, "max")))))
37549 error_at (loc, "predeclared arithmetic type %qT in "
37550 "%<#pragma omp declare reduction%>", type);
37551 else if (TREE_CODE (type) == FUNCTION_TYPE
37552 || TREE_CODE (type) == METHOD_TYPE
37553 || TREE_CODE (type) == ARRAY_TYPE)
37554 error_at (loc, "function or array type %qT in "
37555 "%<#pragma omp declare reduction%>", type);
37556 else if (TREE_CODE (type) == REFERENCE_TYPE)
37557 error_at (loc, "reference type %qT in "
37558 "%<#pragma omp declare reduction%>", type);
37559 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37560 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37561 "%<#pragma omp declare reduction%>", type);
37562 else
37563 types.safe_push (type);
37565 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37566 cp_lexer_consume_token (parser->lexer);
37567 else
37568 break;
37571 /* Restore the saved message. */
37572 parser->type_definition_forbidden_message = saved_message;
37573 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37574 parser->colon_doesnt_start_class_def_p
37575 = saved_colon_doesnt_start_class_def_p;
37577 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37578 || types.is_empty ())
37580 fail:
37581 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37582 goto done;
37585 first_token = cp_lexer_peek_token (parser->lexer);
37586 cp = NULL;
37587 errs = errorcount;
37588 FOR_EACH_VEC_ELT (types, i, type)
37590 tree fntype
37591 = build_function_type_list (void_type_node,
37592 cp_build_reference_type (type, false),
37593 NULL_TREE);
37594 tree this_reduc_id = reduc_id;
37595 if (!dependent_type_p (type))
37596 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37597 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37598 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37599 DECL_ARTIFICIAL (fndecl) = 1;
37600 DECL_EXTERNAL (fndecl) = 1;
37601 DECL_DECLARED_INLINE_P (fndecl) = 1;
37602 DECL_IGNORED_P (fndecl) = 1;
37603 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37604 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37605 DECL_ATTRIBUTES (fndecl)
37606 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37607 DECL_ATTRIBUTES (fndecl));
37608 if (processing_template_decl)
37609 fndecl = push_template_decl (fndecl);
37610 bool block_scope = false;
37611 tree block = NULL_TREE;
37612 if (current_function_decl)
37614 block_scope = true;
37615 DECL_CONTEXT (fndecl) = global_namespace;
37616 if (!processing_template_decl)
37617 pushdecl (fndecl);
37619 else if (current_class_type)
37621 if (cp == NULL)
37623 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37624 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37625 cp_lexer_consume_token (parser->lexer);
37626 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37627 goto fail;
37628 cp = cp_token_cache_new (first_token,
37629 cp_lexer_peek_nth_token (parser->lexer,
37630 2));
37632 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37633 finish_member_declaration (fndecl);
37634 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37635 DECL_PENDING_INLINE_P (fndecl) = 1;
37636 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37637 continue;
37639 else
37641 DECL_CONTEXT (fndecl) = current_namespace;
37642 pushdecl (fndecl);
37644 if (!block_scope)
37645 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37646 else
37647 block = begin_omp_structured_block ();
37648 if (cp)
37650 cp_parser_push_lexer_for_tokens (parser, cp);
37651 parser->lexer->in_pragma = true;
37653 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37655 if (!block_scope)
37656 finish_function (0);
37657 else
37658 DECL_CONTEXT (fndecl) = current_function_decl;
37659 if (cp)
37660 cp_parser_pop_lexer (parser);
37661 goto fail;
37663 if (cp)
37664 cp_parser_pop_lexer (parser);
37665 if (!block_scope)
37666 finish_function (0);
37667 else
37669 DECL_CONTEXT (fndecl) = current_function_decl;
37670 block = finish_omp_structured_block (block);
37671 if (TREE_CODE (block) == BIND_EXPR)
37672 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37673 else if (TREE_CODE (block) == STATEMENT_LIST)
37674 DECL_SAVED_TREE (fndecl) = block;
37675 if (processing_template_decl)
37676 add_decl_expr (fndecl);
37678 cp_check_omp_declare_reduction (fndecl);
37679 if (cp == NULL && types.length () > 1)
37680 cp = cp_token_cache_new (first_token,
37681 cp_lexer_peek_nth_token (parser->lexer, 2));
37682 if (errs != errorcount)
37683 break;
37686 cp_parser_require_pragma_eol (parser, pragma_tok);
37688 done:
37689 /* Free any declarators allocated. */
37690 obstack_free (&declarator_obstack, p);
37693 /* OpenMP 4.0
37694 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37695 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37696 initializer-clause[opt] new-line
37697 #pragma omp declare target new-line */
37699 static void
37700 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37701 enum pragma_context context)
37703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37705 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37706 const char *p = IDENTIFIER_POINTER (id);
37708 if (strcmp (p, "simd") == 0)
37710 cp_lexer_consume_token (parser->lexer);
37711 cp_parser_omp_declare_simd (parser, pragma_tok,
37712 context);
37713 return;
37715 cp_ensure_no_omp_declare_simd (parser);
37716 if (strcmp (p, "reduction") == 0)
37718 cp_lexer_consume_token (parser->lexer);
37719 cp_parser_omp_declare_reduction (parser, pragma_tok,
37720 context);
37721 return;
37723 if (!flag_openmp) /* flag_openmp_simd */
37725 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37726 return;
37728 if (strcmp (p, "target") == 0)
37730 cp_lexer_consume_token (parser->lexer);
37731 cp_parser_omp_declare_target (parser, pragma_tok);
37732 return;
37735 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37736 "or %<target%>");
37737 cp_parser_require_pragma_eol (parser, pragma_tok);
37740 /* OpenMP 4.5:
37741 #pragma omp taskloop taskloop-clause[optseq] new-line
37742 for-loop
37744 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37745 for-loop */
37747 #define OMP_TASKLOOP_CLAUSE_MASK \
37748 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37749 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37750 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37751 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37752 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37753 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37754 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37755 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37756 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37757 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37758 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37759 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37760 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37761 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37763 static tree
37764 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37765 char *p_name, omp_clause_mask mask, tree *cclauses,
37766 bool *if_p)
37768 tree clauses, sb, ret;
37769 unsigned int save;
37770 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37772 strcat (p_name, " taskloop");
37773 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37775 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37777 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37778 const char *p = IDENTIFIER_POINTER (id);
37780 if (strcmp (p, "simd") == 0)
37782 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37783 if (cclauses == NULL)
37784 cclauses = cclauses_buf;
37786 cp_lexer_consume_token (parser->lexer);
37787 if (!flag_openmp) /* flag_openmp_simd */
37788 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37789 cclauses, if_p);
37790 sb = begin_omp_structured_block ();
37791 save = cp_parser_begin_omp_structured_block (parser);
37792 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37793 cclauses, if_p);
37794 cp_parser_end_omp_structured_block (parser, save);
37795 tree body = finish_omp_structured_block (sb);
37796 if (ret == NULL)
37797 return ret;
37798 ret = make_node (OMP_TASKLOOP);
37799 TREE_TYPE (ret) = void_type_node;
37800 OMP_FOR_BODY (ret) = body;
37801 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37802 SET_EXPR_LOCATION (ret, loc);
37803 add_stmt (ret);
37804 return ret;
37807 if (!flag_openmp) /* flag_openmp_simd */
37809 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37810 return NULL_TREE;
37813 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37814 cclauses == NULL);
37815 if (cclauses)
37817 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37818 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37821 sb = begin_omp_structured_block ();
37822 save = cp_parser_begin_omp_structured_block (parser);
37824 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37825 if_p);
37827 cp_parser_end_omp_structured_block (parser, save);
37828 add_stmt (finish_omp_structured_block (sb));
37830 return ret;
37834 /* OpenACC 2.0:
37835 # pragma acc routine oacc-routine-clause[optseq] new-line
37836 function-definition
37838 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37841 #define OACC_ROUTINE_CLAUSE_MASK \
37842 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37843 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37844 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37845 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37848 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37849 component, which must resolve to a declared namespace-scope
37850 function. The clauses are either processed directly (for a named
37851 function), or defered until the immediatley following declaration
37852 is parsed. */
37854 static void
37855 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37856 enum pragma_context context)
37858 gcc_checking_assert (context == pragma_external);
37859 /* The checking for "another pragma following this one" in the "no optional
37860 '( name )'" case makes sure that we dont re-enter. */
37861 gcc_checking_assert (parser->oacc_routine == NULL);
37863 cp_oacc_routine_data data;
37864 data.error_seen = false;
37865 data.fndecl_seen = false;
37866 data.tokens = vNULL;
37867 data.clauses = NULL_TREE;
37868 data.loc = pragma_tok->location;
37869 /* It is safe to take the address of a local variable; it will only be
37870 used while this scope is live. */
37871 parser->oacc_routine = &data;
37873 /* Look for optional '( name )'. */
37874 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37876 matching_parens parens;
37877 parens.consume_open (parser); /* '(' */
37879 /* We parse the name as an id-expression. If it resolves to
37880 anything other than a non-overloaded function at namespace
37881 scope, it's an error. */
37882 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37883 tree name = cp_parser_id_expression (parser,
37884 /*template_keyword_p=*/false,
37885 /*check_dependency_p=*/false,
37886 /*template_p=*/NULL,
37887 /*declarator_p=*/false,
37888 /*optional_p=*/false);
37889 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37890 if (name != error_mark_node && decl == error_mark_node)
37891 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37893 if (decl == error_mark_node
37894 || !parens.require_close (parser))
37896 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37897 parser->oacc_routine = NULL;
37898 return;
37901 data.clauses
37902 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37903 "#pragma acc routine",
37904 cp_lexer_peek_token (parser->lexer));
37906 if (decl && is_overloaded_fn (decl)
37907 && (TREE_CODE (decl) != FUNCTION_DECL
37908 || DECL_FUNCTION_TEMPLATE_P (decl)))
37910 error_at (name_loc,
37911 "%<#pragma acc routine%> names a set of overloads");
37912 parser->oacc_routine = NULL;
37913 return;
37916 /* Perhaps we should use the same rule as declarations in different
37917 namespaces? */
37918 if (!DECL_NAMESPACE_SCOPE_P (decl))
37920 error_at (name_loc,
37921 "%qD does not refer to a namespace scope function", decl);
37922 parser->oacc_routine = NULL;
37923 return;
37926 if (TREE_CODE (decl) != FUNCTION_DECL)
37928 error_at (name_loc, "%qD does not refer to a function", decl);
37929 parser->oacc_routine = NULL;
37930 return;
37933 cp_finalize_oacc_routine (parser, decl, false);
37934 parser->oacc_routine = NULL;
37936 else /* No optional '( name )'. */
37938 /* Store away all pragma tokens. */
37939 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37940 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37941 cp_lexer_consume_token (parser->lexer);
37942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37943 parser->oacc_routine->error_seen = true;
37944 cp_parser_require_pragma_eol (parser, pragma_tok);
37945 struct cp_token_cache *cp
37946 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37947 parser->oacc_routine->tokens.safe_push (cp);
37949 /* Emit a helpful diagnostic if there's another pragma following this
37950 one. */
37951 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37953 cp_ensure_no_oacc_routine (parser);
37954 data.tokens.release ();
37955 /* ..., and then just keep going. */
37956 return;
37959 /* We only have to consider the pragma_external case here. */
37960 cp_parser_declaration (parser);
37961 if (parser->oacc_routine
37962 && !parser->oacc_routine->fndecl_seen)
37963 cp_ensure_no_oacc_routine (parser);
37964 else
37965 parser->oacc_routine = NULL;
37966 data.tokens.release ();
37970 /* Finalize #pragma acc routine clauses after direct declarator has
37971 been parsed. */
37973 static tree
37974 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37976 struct cp_token_cache *ce;
37977 cp_oacc_routine_data *data = parser->oacc_routine;
37979 if (!data->error_seen && data->fndecl_seen)
37981 error_at (data->loc,
37982 "%<#pragma acc routine%> not immediately followed by "
37983 "a single function declaration or definition");
37984 data->error_seen = true;
37986 if (data->error_seen)
37987 return attrs;
37989 gcc_checking_assert (data->tokens.length () == 1);
37990 ce = data->tokens[0];
37992 cp_parser_push_lexer_for_tokens (parser, ce);
37993 parser->lexer->in_pragma = true;
37994 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37996 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37997 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37998 parser->oacc_routine->clauses
37999 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
38000 "#pragma acc routine", pragma_tok);
38001 cp_parser_pop_lexer (parser);
38002 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
38003 fndecl_seen. */
38005 return attrs;
38008 /* Apply any saved OpenACC routine clauses to a just-parsed
38009 declaration. */
38011 static void
38012 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
38014 if (__builtin_expect (parser->oacc_routine != NULL, 0))
38016 /* Keep going if we're in error reporting mode. */
38017 if (parser->oacc_routine->error_seen
38018 || fndecl == error_mark_node)
38019 return;
38021 if (parser->oacc_routine->fndecl_seen)
38023 error_at (parser->oacc_routine->loc,
38024 "%<#pragma acc routine%> not immediately followed by"
38025 " a single function declaration or definition");
38026 parser->oacc_routine = NULL;
38027 return;
38029 if (TREE_CODE (fndecl) != FUNCTION_DECL)
38031 cp_ensure_no_oacc_routine (parser);
38032 return;
38035 if (oacc_get_fn_attrib (fndecl))
38037 error_at (parser->oacc_routine->loc,
38038 "%<#pragma acc routine%> already applied to %qD", fndecl);
38039 parser->oacc_routine = NULL;
38040 return;
38043 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
38045 error_at (parser->oacc_routine->loc,
38046 TREE_USED (fndecl)
38047 ? G_("%<#pragma acc routine%> must be applied before use")
38048 : G_("%<#pragma acc routine%> must be applied before "
38049 "definition"));
38050 parser->oacc_routine = NULL;
38051 return;
38054 /* Process the routine's dimension clauses. */
38055 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
38056 oacc_replace_fn_attrib (fndecl, dims);
38058 /* Add an "omp declare target" attribute. */
38059 DECL_ATTRIBUTES (fndecl)
38060 = tree_cons (get_identifier ("omp declare target"),
38061 NULL_TREE, DECL_ATTRIBUTES (fndecl));
38063 /* Don't unset parser->oacc_routine here: we may still need it to
38064 diagnose wrong usage. But, remember that we've used this "#pragma acc
38065 routine". */
38066 parser->oacc_routine->fndecl_seen = true;
38070 /* Main entry point to OpenMP statement pragmas. */
38072 static void
38073 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38075 tree stmt;
38076 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
38077 omp_clause_mask mask (0);
38079 switch (cp_parser_pragma_kind (pragma_tok))
38081 case PRAGMA_OACC_ATOMIC:
38082 cp_parser_omp_atomic (parser, pragma_tok);
38083 return;
38084 case PRAGMA_OACC_CACHE:
38085 stmt = cp_parser_oacc_cache (parser, pragma_tok);
38086 break;
38087 case PRAGMA_OACC_DATA:
38088 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
38089 break;
38090 case PRAGMA_OACC_ENTER_DATA:
38091 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
38092 break;
38093 case PRAGMA_OACC_EXIT_DATA:
38094 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
38095 break;
38096 case PRAGMA_OACC_HOST_DATA:
38097 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
38098 break;
38099 case PRAGMA_OACC_KERNELS:
38100 case PRAGMA_OACC_PARALLEL:
38101 strcpy (p_name, "#pragma acc");
38102 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
38103 if_p);
38104 break;
38105 case PRAGMA_OACC_LOOP:
38106 strcpy (p_name, "#pragma acc");
38107 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
38108 if_p);
38109 break;
38110 case PRAGMA_OACC_UPDATE:
38111 stmt = cp_parser_oacc_update (parser, pragma_tok);
38112 break;
38113 case PRAGMA_OACC_WAIT:
38114 stmt = cp_parser_oacc_wait (parser, pragma_tok);
38115 break;
38116 case PRAGMA_OMP_ATOMIC:
38117 cp_parser_omp_atomic (parser, pragma_tok);
38118 return;
38119 case PRAGMA_OMP_CRITICAL:
38120 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
38121 break;
38122 case PRAGMA_OMP_DISTRIBUTE:
38123 strcpy (p_name, "#pragma omp");
38124 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
38125 if_p);
38126 break;
38127 case PRAGMA_OMP_FOR:
38128 strcpy (p_name, "#pragma omp");
38129 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
38130 if_p);
38131 break;
38132 case PRAGMA_OMP_MASTER:
38133 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
38134 break;
38135 case PRAGMA_OMP_PARALLEL:
38136 strcpy (p_name, "#pragma omp");
38137 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
38138 if_p);
38139 break;
38140 case PRAGMA_OMP_SECTIONS:
38141 strcpy (p_name, "#pragma omp");
38142 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
38143 break;
38144 case PRAGMA_OMP_SIMD:
38145 strcpy (p_name, "#pragma omp");
38146 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
38147 if_p);
38148 break;
38149 case PRAGMA_OMP_SINGLE:
38150 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
38151 break;
38152 case PRAGMA_OMP_TASK:
38153 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
38154 break;
38155 case PRAGMA_OMP_TASKGROUP:
38156 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
38157 break;
38158 case PRAGMA_OMP_TASKLOOP:
38159 strcpy (p_name, "#pragma omp");
38160 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
38161 if_p);
38162 break;
38163 case PRAGMA_OMP_TEAMS:
38164 strcpy (p_name, "#pragma omp");
38165 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
38166 if_p);
38167 break;
38168 default:
38169 gcc_unreachable ();
38172 protected_set_expr_location (stmt, pragma_tok->location);
38175 /* Transactional Memory parsing routines. */
38177 /* Parse a transaction attribute.
38179 txn-attribute:
38180 attribute
38181 [ [ identifier ] ]
38183 We use this instead of cp_parser_attributes_opt for transactions to avoid
38184 the pedwarn in C++98 mode. */
38186 static tree
38187 cp_parser_txn_attribute_opt (cp_parser *parser)
38189 cp_token *token;
38190 tree attr_name, attr = NULL;
38192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
38193 return cp_parser_attributes_opt (parser);
38195 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
38196 return NULL_TREE;
38197 cp_lexer_consume_token (parser->lexer);
38198 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
38199 goto error1;
38201 token = cp_lexer_peek_token (parser->lexer);
38202 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
38204 token = cp_lexer_consume_token (parser->lexer);
38206 attr_name = (token->type == CPP_KEYWORD
38207 /* For keywords, use the canonical spelling,
38208 not the parsed identifier. */
38209 ? ridpointers[(int) token->keyword]
38210 : token->u.value);
38211 attr = build_tree_list (attr_name, NULL_TREE);
38213 else
38214 cp_parser_error (parser, "expected identifier");
38216 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38217 error1:
38218 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
38219 return attr;
38222 /* Parse a __transaction_atomic or __transaction_relaxed statement.
38224 transaction-statement:
38225 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
38226 compound-statement
38227 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
38230 static tree
38231 cp_parser_transaction (cp_parser *parser, cp_token *token)
38233 unsigned char old_in = parser->in_transaction;
38234 unsigned char this_in = 1, new_in;
38235 enum rid keyword = token->keyword;
38236 tree stmt, attrs, noex;
38238 cp_lexer_consume_token (parser->lexer);
38240 if (keyword == RID_TRANSACTION_RELAXED
38241 || keyword == RID_SYNCHRONIZED)
38242 this_in |= TM_STMT_ATTR_RELAXED;
38243 else
38245 attrs = cp_parser_txn_attribute_opt (parser);
38246 if (attrs)
38247 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38250 /* Parse a noexcept specification. */
38251 if (keyword == RID_ATOMIC_NOEXCEPT)
38252 noex = boolean_true_node;
38253 else if (keyword == RID_ATOMIC_CANCEL)
38255 /* cancel-and-throw is unimplemented. */
38256 sorry ("atomic_cancel");
38257 noex = NULL_TREE;
38259 else
38260 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38262 /* Keep track if we're in the lexical scope of an outer transaction. */
38263 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38265 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38267 parser->in_transaction = new_in;
38268 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38269 parser->in_transaction = old_in;
38271 finish_transaction_stmt (stmt, NULL, this_in, noex);
38273 return stmt;
38276 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38278 transaction-expression:
38279 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38280 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38283 static tree
38284 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38286 unsigned char old_in = parser->in_transaction;
38287 unsigned char this_in = 1;
38288 cp_token *token;
38289 tree expr, noex;
38290 bool noex_expr;
38291 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38293 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38294 || keyword == RID_TRANSACTION_RELAXED);
38296 if (!flag_tm)
38297 error_at (loc,
38298 keyword == RID_TRANSACTION_RELAXED
38299 ? G_("%<__transaction_relaxed%> without transactional memory "
38300 "support enabled")
38301 : G_("%<__transaction_atomic%> without transactional memory "
38302 "support enabled"));
38304 token = cp_parser_require_keyword (parser, keyword,
38305 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38306 : RT_TRANSACTION_RELAXED));
38307 gcc_assert (token != NULL);
38309 if (keyword == RID_TRANSACTION_RELAXED)
38310 this_in |= TM_STMT_ATTR_RELAXED;
38312 /* Set this early. This might mean that we allow transaction_cancel in
38313 an expression that we find out later actually has to be a constexpr.
38314 However, we expect that cxx_constant_value will be able to deal with
38315 this; also, if the noexcept has no constexpr, then what we parse next
38316 really is a transaction's body. */
38317 parser->in_transaction = this_in;
38319 /* Parse a noexcept specification. */
38320 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38321 true);
38323 if (!noex || !noex_expr
38324 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38326 matching_parens parens;
38327 parens.require_open (parser);
38329 expr = cp_parser_expression (parser);
38330 expr = finish_parenthesized_expr (expr);
38332 parens.require_close (parser);
38334 else
38336 /* The only expression that is available got parsed for the noexcept
38337 already. noexcept is true then. */
38338 expr = noex;
38339 noex = boolean_true_node;
38342 expr = build_transaction_expr (token->location, expr, this_in, noex);
38343 parser->in_transaction = old_in;
38345 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38346 return error_mark_node;
38348 return (flag_tm ? expr : error_mark_node);
38351 /* Parse a function-transaction-block.
38353 function-transaction-block:
38354 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38355 function-body
38356 __transaction_atomic txn-attribute[opt] function-try-block
38357 __transaction_relaxed ctor-initializer[opt] function-body
38358 __transaction_relaxed function-try-block
38361 static bool
38362 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38364 unsigned char old_in = parser->in_transaction;
38365 unsigned char new_in = 1;
38366 tree compound_stmt, stmt, attrs;
38367 bool ctor_initializer_p;
38368 cp_token *token;
38370 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38371 || keyword == RID_TRANSACTION_RELAXED);
38372 token = cp_parser_require_keyword (parser, keyword,
38373 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38374 : RT_TRANSACTION_RELAXED));
38375 gcc_assert (token != NULL);
38377 if (keyword == RID_TRANSACTION_RELAXED)
38378 new_in |= TM_STMT_ATTR_RELAXED;
38379 else
38381 attrs = cp_parser_txn_attribute_opt (parser);
38382 if (attrs)
38383 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38386 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38388 parser->in_transaction = new_in;
38390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38391 ctor_initializer_p = cp_parser_function_try_block (parser);
38392 else
38393 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38394 (parser, /*in_function_try_block=*/false);
38396 parser->in_transaction = old_in;
38398 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38400 return ctor_initializer_p;
38403 /* Parse a __transaction_cancel statement.
38405 cancel-statement:
38406 __transaction_cancel txn-attribute[opt] ;
38407 __transaction_cancel txn-attribute[opt] throw-expression ;
38409 ??? Cancel and throw is not yet implemented. */
38411 static tree
38412 cp_parser_transaction_cancel (cp_parser *parser)
38414 cp_token *token;
38415 bool is_outer = false;
38416 tree stmt, attrs;
38418 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38419 RT_TRANSACTION_CANCEL);
38420 gcc_assert (token != NULL);
38422 attrs = cp_parser_txn_attribute_opt (parser);
38423 if (attrs)
38424 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38426 /* ??? Parse cancel-and-throw here. */
38428 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38430 if (!flag_tm)
38432 error_at (token->location, "%<__transaction_cancel%> without "
38433 "transactional memory support enabled");
38434 return error_mark_node;
38436 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38438 error_at (token->location, "%<__transaction_cancel%> within a "
38439 "%<__transaction_relaxed%>");
38440 return error_mark_node;
38442 else if (is_outer)
38444 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38445 && !is_tm_may_cancel_outer (current_function_decl))
38447 error_at (token->location, "outer %<__transaction_cancel%> not "
38448 "within outer %<__transaction_atomic%>");
38449 error_at (token->location,
38450 " or a %<transaction_may_cancel_outer%> function");
38451 return error_mark_node;
38454 else if (parser->in_transaction == 0)
38456 error_at (token->location, "%<__transaction_cancel%> not within "
38457 "%<__transaction_atomic%>");
38458 return error_mark_node;
38461 stmt = build_tm_abort_call (token->location, is_outer);
38462 add_stmt (stmt);
38464 return stmt;
38467 /* The parser. */
38469 static GTY (()) cp_parser *the_parser;
38472 /* Special handling for the first token or line in the file. The first
38473 thing in the file might be #pragma GCC pch_preprocess, which loads a
38474 PCH file, which is a GC collection point. So we need to handle this
38475 first pragma without benefit of an existing lexer structure.
38477 Always returns one token to the caller in *FIRST_TOKEN. This is
38478 either the true first token of the file, or the first token after
38479 the initial pragma. */
38481 static void
38482 cp_parser_initial_pragma (cp_token *first_token)
38484 tree name = NULL;
38486 cp_lexer_get_preprocessor_token (NULL, first_token);
38487 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38488 return;
38490 cp_lexer_get_preprocessor_token (NULL, first_token);
38491 if (first_token->type == CPP_STRING)
38493 name = first_token->u.value;
38495 cp_lexer_get_preprocessor_token (NULL, first_token);
38496 if (first_token->type != CPP_PRAGMA_EOL)
38497 error_at (first_token->location,
38498 "junk at end of %<#pragma GCC pch_preprocess%>");
38500 else
38501 error_at (first_token->location, "expected string literal");
38503 /* Skip to the end of the pragma. */
38504 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38505 cp_lexer_get_preprocessor_token (NULL, first_token);
38507 /* Now actually load the PCH file. */
38508 if (name)
38509 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38511 /* Read one more token to return to our caller. We have to do this
38512 after reading the PCH file in, since its pointers have to be
38513 live. */
38514 cp_lexer_get_preprocessor_token (NULL, first_token);
38517 /* Parses the grainsize pragma for the _Cilk_for statement.
38518 Syntax:
38519 #pragma cilk grainsize = <VALUE>. */
38521 static void
38522 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38524 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38526 tree exp = cp_parser_binary_expression (parser, false, false,
38527 PREC_NOT_OPERATOR, NULL);
38528 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38529 if (!exp || exp == error_mark_node)
38531 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38532 return;
38535 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38536 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38537 cp_parser_cilk_for (parser, exp, if_p);
38538 else
38539 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38540 "%<#pragma cilk grainsize%> is not followed by "
38541 "%<_Cilk_for%>");
38542 return;
38544 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38547 /* Normal parsing of a pragma token. Here we can (and must) use the
38548 regular lexer. */
38550 static bool
38551 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38553 cp_token *pragma_tok;
38554 unsigned int id;
38555 tree stmt;
38556 bool ret;
38558 pragma_tok = cp_lexer_consume_token (parser->lexer);
38559 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38560 parser->lexer->in_pragma = true;
38562 id = cp_parser_pragma_kind (pragma_tok);
38563 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38564 cp_ensure_no_omp_declare_simd (parser);
38565 switch (id)
38567 case PRAGMA_GCC_PCH_PREPROCESS:
38568 error_at (pragma_tok->location,
38569 "%<#pragma GCC pch_preprocess%> must be first");
38570 break;
38572 case PRAGMA_OMP_BARRIER:
38573 switch (context)
38575 case pragma_compound:
38576 cp_parser_omp_barrier (parser, pragma_tok);
38577 return false;
38578 case pragma_stmt:
38579 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38580 "used in compound statements", "omp barrier");
38581 break;
38582 default:
38583 goto bad_stmt;
38585 break;
38587 case PRAGMA_OMP_FLUSH:
38588 switch (context)
38590 case pragma_compound:
38591 cp_parser_omp_flush (parser, pragma_tok);
38592 return false;
38593 case pragma_stmt:
38594 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38595 "used in compound statements", "omp flush");
38596 break;
38597 default:
38598 goto bad_stmt;
38600 break;
38602 case PRAGMA_OMP_TASKWAIT:
38603 switch (context)
38605 case pragma_compound:
38606 cp_parser_omp_taskwait (parser, pragma_tok);
38607 return false;
38608 case pragma_stmt:
38609 error_at (pragma_tok->location,
38610 "%<#pragma %s%> may only be used in compound statements",
38611 "omp taskwait");
38612 break;
38613 default:
38614 goto bad_stmt;
38616 break;
38618 case PRAGMA_OMP_TASKYIELD:
38619 switch (context)
38621 case pragma_compound:
38622 cp_parser_omp_taskyield (parser, pragma_tok);
38623 return false;
38624 case pragma_stmt:
38625 error_at (pragma_tok->location,
38626 "%<#pragma %s%> may only be used in compound statements",
38627 "omp taskyield");
38628 break;
38629 default:
38630 goto bad_stmt;
38632 break;
38634 case PRAGMA_OMP_CANCEL:
38635 switch (context)
38637 case pragma_compound:
38638 cp_parser_omp_cancel (parser, pragma_tok);
38639 return false;
38640 case pragma_stmt:
38641 error_at (pragma_tok->location,
38642 "%<#pragma %s%> may only be used in compound statements",
38643 "omp cancel");
38644 break;
38645 default:
38646 goto bad_stmt;
38648 break;
38650 case PRAGMA_OMP_CANCELLATION_POINT:
38651 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38652 return false;
38654 case PRAGMA_OMP_THREADPRIVATE:
38655 cp_parser_omp_threadprivate (parser, pragma_tok);
38656 return false;
38658 case PRAGMA_OMP_DECLARE:
38659 cp_parser_omp_declare (parser, pragma_tok, context);
38660 return false;
38662 case PRAGMA_OACC_DECLARE:
38663 cp_parser_oacc_declare (parser, pragma_tok);
38664 return false;
38666 case PRAGMA_OACC_ENTER_DATA:
38667 if (context == pragma_stmt)
38669 error_at (pragma_tok->location,
38670 "%<#pragma %s%> may only be used in compound statements",
38671 "acc enter data");
38672 break;
38674 else if (context != pragma_compound)
38675 goto bad_stmt;
38676 cp_parser_omp_construct (parser, pragma_tok, if_p);
38677 return true;
38679 case PRAGMA_OACC_EXIT_DATA:
38680 if (context == pragma_stmt)
38682 error_at (pragma_tok->location,
38683 "%<#pragma %s%> may only be used in compound statements",
38684 "acc exit data");
38685 break;
38687 else if (context != pragma_compound)
38688 goto bad_stmt;
38689 cp_parser_omp_construct (parser, pragma_tok, if_p);
38690 return true;
38692 case PRAGMA_OACC_ROUTINE:
38693 if (context != pragma_external)
38695 error_at (pragma_tok->location,
38696 "%<#pragma acc routine%> must be at file scope");
38697 break;
38699 cp_parser_oacc_routine (parser, pragma_tok, context);
38700 return false;
38702 case PRAGMA_OACC_UPDATE:
38703 if (context == pragma_stmt)
38705 error_at (pragma_tok->location,
38706 "%<#pragma %s%> may only be used in compound statements",
38707 "acc update");
38708 break;
38710 else if (context != pragma_compound)
38711 goto bad_stmt;
38712 cp_parser_omp_construct (parser, pragma_tok, if_p);
38713 return true;
38715 case PRAGMA_OACC_WAIT:
38716 if (context == pragma_stmt)
38718 error_at (pragma_tok->location,
38719 "%<#pragma %s%> may only be used in compound statements",
38720 "acc wait");
38721 break;
38723 else if (context != pragma_compound)
38724 goto bad_stmt;
38725 cp_parser_omp_construct (parser, pragma_tok, if_p);
38726 return true;
38728 case PRAGMA_OACC_ATOMIC:
38729 case PRAGMA_OACC_CACHE:
38730 case PRAGMA_OACC_DATA:
38731 case PRAGMA_OACC_HOST_DATA:
38732 case PRAGMA_OACC_KERNELS:
38733 case PRAGMA_OACC_PARALLEL:
38734 case PRAGMA_OACC_LOOP:
38735 case PRAGMA_OMP_ATOMIC:
38736 case PRAGMA_OMP_CRITICAL:
38737 case PRAGMA_OMP_DISTRIBUTE:
38738 case PRAGMA_OMP_FOR:
38739 case PRAGMA_OMP_MASTER:
38740 case PRAGMA_OMP_PARALLEL:
38741 case PRAGMA_OMP_SECTIONS:
38742 case PRAGMA_OMP_SIMD:
38743 case PRAGMA_OMP_SINGLE:
38744 case PRAGMA_OMP_TASK:
38745 case PRAGMA_OMP_TASKGROUP:
38746 case PRAGMA_OMP_TASKLOOP:
38747 case PRAGMA_OMP_TEAMS:
38748 if (context != pragma_stmt && context != pragma_compound)
38749 goto bad_stmt;
38750 stmt = push_omp_privatization_clauses (false);
38751 cp_parser_omp_construct (parser, pragma_tok, if_p);
38752 pop_omp_privatization_clauses (stmt);
38753 return true;
38755 case PRAGMA_OMP_ORDERED:
38756 if (context != pragma_stmt && context != pragma_compound)
38757 goto bad_stmt;
38758 stmt = push_omp_privatization_clauses (false);
38759 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38760 pop_omp_privatization_clauses (stmt);
38761 return ret;
38763 case PRAGMA_OMP_TARGET:
38764 if (context != pragma_stmt && context != pragma_compound)
38765 goto bad_stmt;
38766 stmt = push_omp_privatization_clauses (false);
38767 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38768 pop_omp_privatization_clauses (stmt);
38769 return ret;
38771 case PRAGMA_OMP_END_DECLARE_TARGET:
38772 cp_parser_omp_end_declare_target (parser, pragma_tok);
38773 return false;
38775 case PRAGMA_OMP_SECTION:
38776 error_at (pragma_tok->location,
38777 "%<#pragma omp section%> may only be used in "
38778 "%<#pragma omp sections%> construct");
38779 break;
38781 case PRAGMA_IVDEP:
38783 if (context == pragma_external)
38785 error_at (pragma_tok->location,
38786 "%<#pragma GCC ivdep%> must be inside a function");
38787 break;
38789 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38790 cp_token *tok;
38791 tok = cp_lexer_peek_token (the_parser->lexer);
38792 if (tok->type != CPP_KEYWORD
38793 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38794 && tok->keyword != RID_DO))
38796 cp_parser_error (parser, "for, while or do statement expected");
38797 return false;
38799 cp_parser_iteration_statement (parser, if_p, true);
38800 return true;
38803 case PRAGMA_CILK_SIMD:
38804 if (context == pragma_external)
38806 error_at (pragma_tok->location,
38807 "%<#pragma simd%> must be inside a function");
38808 break;
38810 stmt = push_omp_privatization_clauses (false);
38811 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38812 pop_omp_privatization_clauses (stmt);
38813 return true;
38815 case PRAGMA_CILK_GRAINSIZE:
38816 if (context == pragma_external)
38818 error_at (pragma_tok->location,
38819 "%<#pragma cilk grainsize%> must be inside a function");
38820 break;
38823 /* Ignore the pragma if Cilk Plus is not enabled. */
38824 if (flag_cilkplus)
38826 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38827 return true;
38829 else
38831 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38832 "%<#pragma cilk grainsize%>");
38833 break;
38836 default:
38837 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38838 c_invoke_pragma_handler (id);
38839 break;
38841 bad_stmt:
38842 cp_parser_error (parser, "expected declaration specifiers");
38843 break;
38846 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38847 return false;
38850 /* The interface the pragma parsers have to the lexer. */
38852 enum cpp_ttype
38853 pragma_lex (tree *value, location_t *loc)
38855 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38856 enum cpp_ttype ret = tok->type;
38858 *value = tok->u.value;
38859 if (loc)
38860 *loc = tok->location;
38862 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38863 ret = CPP_EOF;
38864 else if (ret == CPP_STRING)
38865 *value = cp_parser_string_literal (the_parser, false, false);
38866 else
38868 if (ret == CPP_KEYWORD)
38869 ret = CPP_NAME;
38870 cp_lexer_consume_token (the_parser->lexer);
38873 return ret;
38877 /* External interface. */
38879 /* Parse one entire translation unit. */
38881 void
38882 c_parse_file (void)
38884 static bool already_called = false;
38886 if (already_called)
38887 fatal_error (input_location,
38888 "inter-module optimizations not implemented for C++");
38889 already_called = true;
38891 the_parser = cp_parser_new ();
38892 push_deferring_access_checks (flag_access_control
38893 ? dk_no_deferred : dk_no_check);
38894 cp_parser_translation_unit (the_parser);
38895 the_parser = NULL;
38898 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38899 vectorlength clause:
38900 Syntax:
38901 vectorlength ( constant-expression ) */
38903 static tree
38904 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38905 bool is_simd_fn)
38907 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38908 tree expr;
38909 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38910 safelen clause. Thus, vectorlength is represented as OMP 4.0
38911 safelen. For SIMD-enabled function it is represented by OMP 4.0
38912 simdlen. */
38913 if (!is_simd_fn)
38914 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38915 loc);
38916 else
38917 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38918 loc);
38920 matching_parens parens;
38921 if (!parens.require_open (parser))
38922 return error_mark_node;
38924 expr = cp_parser_constant_expression (parser);
38925 expr = maybe_constant_value (expr);
38927 /* If expr == error_mark_node, then don't emit any errors nor
38928 create a clause. if any of the above functions returns
38929 error mark node then they would have emitted an error message. */
38930 if (expr == error_mark_node)
38932 else if (!TREE_TYPE (expr)
38933 || !TREE_CONSTANT (expr)
38934 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38935 error_at (loc, "vectorlength must be an integer constant");
38936 else if (TREE_CONSTANT (expr)
38937 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38938 error_at (loc, "vectorlength must be a power of 2");
38939 else
38941 tree c;
38942 if (!is_simd_fn)
38944 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38945 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38946 OMP_CLAUSE_CHAIN (c) = clauses;
38947 clauses = c;
38949 else
38951 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38952 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38953 OMP_CLAUSE_CHAIN (c) = clauses;
38954 clauses = c;
38958 if (!parens.require_close (parser))
38959 return error_mark_node;
38960 return clauses;
38963 /* Handles the Cilk Plus #pragma simd linear clause.
38964 Syntax:
38965 linear ( simd-linear-variable-list )
38967 simd-linear-variable-list:
38968 simd-linear-variable
38969 simd-linear-variable-list , simd-linear-variable
38971 simd-linear-variable:
38972 id-expression
38973 id-expression : simd-linear-step
38975 simd-linear-step:
38976 conditional-expression */
38978 static tree
38979 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38981 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38983 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38984 return clauses;
38985 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38987 cp_parser_error (parser, "expected identifier");
38988 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38989 return error_mark_node;
38992 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38993 parser->colon_corrects_to_scope_p = false;
38994 while (1)
38996 cp_token *token = cp_lexer_peek_token (parser->lexer);
38997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38999 cp_parser_error (parser, "expected variable-name");
39000 clauses = error_mark_node;
39001 break;
39004 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
39005 false, false);
39006 tree decl = cp_parser_lookup_name_simple (parser, var_name,
39007 token->location);
39008 if (decl == error_mark_node)
39010 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
39011 token->location);
39012 clauses = error_mark_node;
39014 else
39016 tree e = NULL_TREE;
39017 tree step_size = integer_one_node;
39019 /* If present, parse the linear step. Otherwise, assume the default
39020 value of 1. */
39021 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
39023 cp_lexer_consume_token (parser->lexer);
39025 e = cp_parser_assignment_expression (parser);
39026 e = maybe_constant_value (e);
39028 if (e == error_mark_node)
39030 /* If an error has occurred, then the whole pragma is
39031 considered ill-formed. Thus, no reason to keep
39032 parsing. */
39033 clauses = error_mark_node;
39034 break;
39036 else if (type_dependent_expression_p (e)
39037 || value_dependent_expression_p (e)
39038 || (TREE_TYPE (e)
39039 && INTEGRAL_TYPE_P (TREE_TYPE (e))
39040 && (TREE_CONSTANT (e)
39041 || DECL_P (e))))
39042 step_size = e;
39043 else
39044 cp_parser_error (parser,
39045 "step size must be an integer constant "
39046 "expression or an integer variable");
39049 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
39050 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
39051 OMP_CLAUSE_DECL (l) = decl;
39052 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
39053 OMP_CLAUSE_CHAIN (l) = clauses;
39054 clauses = l;
39056 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
39057 cp_lexer_consume_token (parser->lexer);
39058 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
39059 break;
39060 else
39062 error_at (cp_lexer_peek_token (parser->lexer)->location,
39063 "expected %<,%> or %<)%> after %qE", decl);
39064 clauses = error_mark_node;
39065 break;
39068 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
39069 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
39070 return clauses;
39073 /* Returns the name of the next clause. If the clause is not
39074 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
39075 token is not consumed. Otherwise, the appropriate enum from the
39076 pragma_simd_clause is returned and the token is consumed. */
39078 static pragma_omp_clause
39079 cp_parser_cilk_simd_clause_name (cp_parser *parser)
39081 pragma_omp_clause clause_type;
39082 cp_token *token = cp_lexer_peek_token (parser->lexer);
39084 if (token->keyword == RID_PRIVATE)
39085 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
39086 else if (!token->u.value || token->type != CPP_NAME)
39087 return PRAGMA_CILK_CLAUSE_NONE;
39088 else if (id_equal (token->u.value, "vectorlength"))
39089 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
39090 else if (id_equal (token->u.value, "linear"))
39091 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
39092 else if (id_equal (token->u.value, "firstprivate"))
39093 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
39094 else if (id_equal (token->u.value, "lastprivate"))
39095 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
39096 else if (id_equal (token->u.value, "reduction"))
39097 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
39098 else
39099 return PRAGMA_CILK_CLAUSE_NONE;
39101 cp_lexer_consume_token (parser->lexer);
39102 return clause_type;
39105 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
39107 static tree
39108 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
39110 tree clauses = NULL_TREE;
39112 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
39113 && clauses != error_mark_node)
39115 pragma_omp_clause c_kind;
39116 c_kind = cp_parser_cilk_simd_clause_name (parser);
39117 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
39118 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
39119 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
39120 clauses = cp_parser_cilk_simd_linear (parser, clauses);
39121 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
39122 /* Use the OpenMP 4.0 equivalent function. */
39123 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
39124 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
39125 /* Use the OpenMP 4.0 equivalent function. */
39126 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
39127 clauses);
39128 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
39129 /* Use the OMP 4.0 equivalent function. */
39130 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
39131 clauses);
39132 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
39133 /* Use the OMP 4.0 equivalent function. */
39134 clauses = cp_parser_omp_clause_reduction (parser, clauses);
39135 else
39137 clauses = error_mark_node;
39138 cp_parser_error (parser, "expected %<#pragma simd%> clause");
39139 break;
39143 cp_parser_skip_to_pragma_eol (parser, pragma_token);
39145 if (clauses == error_mark_node)
39146 return error_mark_node;
39147 else
39148 return finish_omp_clauses (clauses, C_ORT_CILK);
39151 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
39153 static void
39154 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
39156 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
39158 if (clauses == error_mark_node)
39159 return;
39161 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
39163 error_at (cp_lexer_peek_token (parser->lexer)->location,
39164 "for statement expected");
39165 return;
39168 tree sb = begin_omp_structured_block ();
39169 int save = cp_parser_begin_omp_structured_block (parser);
39170 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
39171 if (ret)
39172 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
39173 cp_parser_end_omp_structured_block (parser, save);
39174 add_stmt (finish_omp_structured_block (sb));
39177 /* Main entry-point for parsing Cilk Plus _Cilk_for
39178 loops. The return value is error_mark_node
39179 when errors happen and CILK_FOR tree on success. */
39181 static tree
39182 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
39184 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
39185 gcc_unreachable ();
39187 tree sb = begin_omp_structured_block ();
39188 int save = cp_parser_begin_omp_structured_block (parser);
39190 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
39191 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
39192 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
39193 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
39195 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
39196 if (ret)
39197 cpp_validate_cilk_plus_loop (ret);
39198 else
39199 ret = error_mark_node;
39201 cp_parser_end_omp_structured_block (parser, save);
39202 add_stmt (finish_omp_structured_block (sb));
39203 return ret;
39206 /* Create an identifier for a generic parameter type (a synthesized
39207 template parameter implied by `auto' or a concept identifier). */
39209 static GTY(()) int generic_parm_count;
39210 static tree
39211 make_generic_type_name ()
39213 char buf[32];
39214 sprintf (buf, "auto:%d", ++generic_parm_count);
39215 return get_identifier (buf);
39218 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
39219 (creating a new template parameter list if necessary). Returns the newly
39220 created template type parm. */
39222 static tree
39223 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
39225 gcc_assert (current_binding_level->kind == sk_function_parms);
39227 /* Before committing to modifying any scope, if we're in an
39228 implicit template scope, and we're trying to synthesize a
39229 constrained parameter, try to find a previous parameter with
39230 the same name. This is the same-type rule for abbreviated
39231 function templates.
39233 NOTE: We can generate implicit parameters when tentatively
39234 parsing a nested name specifier, only to reject that parse
39235 later. However, matching the same template-id as part of a
39236 direct-declarator should generate an identical template
39237 parameter, so this rule will merge them. */
39238 if (parser->implicit_template_scope && constr)
39240 tree t = parser->implicit_template_parms;
39241 while (t)
39243 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
39245 tree d = TREE_VALUE (t);
39246 if (TREE_CODE (d) == PARM_DECL)
39247 /* Return the TEMPLATE_PARM_INDEX. */
39248 d = DECL_INITIAL (d);
39249 return d;
39251 t = TREE_CHAIN (t);
39255 /* We are either continuing a function template that already contains implicit
39256 template parameters, creating a new fully-implicit function template, or
39257 extending an existing explicit function template with implicit template
39258 parameters. */
39260 cp_binding_level *const entry_scope = current_binding_level;
39262 bool become_template = false;
39263 cp_binding_level *parent_scope = 0;
39265 if (parser->implicit_template_scope)
39267 gcc_assert (parser->implicit_template_parms);
39269 current_binding_level = parser->implicit_template_scope;
39271 else
39273 /* Roll back to the existing template parameter scope (in the case of
39274 extending an explicit function template) or introduce a new template
39275 parameter scope ahead of the function parameter scope (or class scope
39276 in the case of out-of-line member definitions). The function scope is
39277 added back after template parameter synthesis below. */
39279 cp_binding_level *scope = entry_scope;
39281 while (scope->kind == sk_function_parms)
39283 parent_scope = scope;
39284 scope = scope->level_chain;
39286 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39288 /* If not defining a class, then any class scope is a scope level in
39289 an out-of-line member definition. In this case simply wind back
39290 beyond the first such scope to inject the template parameter list.
39291 Otherwise wind back to the class being defined. The latter can
39292 occur in class member friend declarations such as:
39294 class A {
39295 void foo (auto);
39297 class B {
39298 friend void A::foo (auto);
39301 The template parameter list synthesized for the friend declaration
39302 must be injected in the scope of 'B'. This can also occur in
39303 erroneous cases such as:
39305 struct A {
39306 struct B {
39307 void foo (auto);
39309 void B::foo (auto) {}
39312 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39313 but, nevertheless, the template parameter list synthesized for the
39314 declarator should be injected into the scope of 'A' as if the
39315 ill-formed template was specified explicitly. */
39317 while (scope->kind == sk_class && !scope->defining_class_p)
39319 parent_scope = scope;
39320 scope = scope->level_chain;
39324 current_binding_level = scope;
39326 if (scope->kind != sk_template_parms
39327 || !function_being_declared_is_template_p (parser))
39329 /* Introduce a new template parameter list for implicit template
39330 parameters. */
39332 become_template = true;
39334 parser->implicit_template_scope
39335 = begin_scope (sk_template_parms, NULL);
39337 ++processing_template_decl;
39339 parser->fully_implicit_function_template_p = true;
39340 ++parser->num_template_parameter_lists;
39342 else
39344 /* Synthesize implicit template parameters at the end of the explicit
39345 template parameter list. */
39347 gcc_assert (current_template_parms);
39349 parser->implicit_template_scope = scope;
39351 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39352 parser->implicit_template_parms
39353 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39357 /* Synthesize a new template parameter and track the current template
39358 parameter chain with implicit_template_parms. */
39360 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39361 tree synth_id = make_generic_type_name ();
39362 tree synth_tmpl_parm;
39363 bool non_type = false;
39365 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39366 synth_tmpl_parm
39367 = finish_template_type_parm (class_type_node, synth_id);
39368 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39369 synth_tmpl_parm
39370 = finish_constrained_template_template_parm (proto, synth_id);
39371 else
39373 synth_tmpl_parm = copy_decl (proto);
39374 DECL_NAME (synth_tmpl_parm) = synth_id;
39375 non_type = true;
39378 // Attach the constraint to the parm before processing.
39379 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39380 TREE_TYPE (node) = constr;
39381 tree new_parm
39382 = process_template_parm (parser->implicit_template_parms,
39383 input_location,
39384 node,
39385 /*non_type=*/non_type,
39386 /*param_pack=*/false);
39388 // Chain the new parameter to the list of implicit parameters.
39389 if (parser->implicit_template_parms)
39390 parser->implicit_template_parms
39391 = TREE_CHAIN (parser->implicit_template_parms);
39392 else
39393 parser->implicit_template_parms = new_parm;
39395 tree new_decl = get_local_decls ();
39396 if (non_type)
39397 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39398 new_decl = DECL_INITIAL (new_decl);
39400 /* If creating a fully implicit function template, start the new implicit
39401 template parameter list with this synthesized type, otherwise grow the
39402 current template parameter list. */
39404 if (become_template)
39406 parent_scope->level_chain = current_binding_level;
39408 tree new_parms = make_tree_vec (1);
39409 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39410 current_template_parms = tree_cons (size_int (processing_template_decl),
39411 new_parms, current_template_parms);
39413 else
39415 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39416 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39417 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39418 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39421 // If the new parameter was constrained, we need to add that to the
39422 // constraints in the template parameter list.
39423 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39425 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39426 reqs = conjoin_constraints (reqs, req);
39427 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39430 current_binding_level = entry_scope;
39432 return new_decl;
39435 /* Finish the declaration of a fully implicit function template. Such a
39436 template has no explicit template parameter list so has not been through the
39437 normal template head and tail processing. synthesize_implicit_template_parm
39438 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39439 provided if the declaration is a class member such that its template
39440 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39441 form is returned. Otherwise NULL_TREE is returned. */
39443 static tree
39444 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39446 gcc_assert (parser->fully_implicit_function_template_p);
39448 if (member_decl_opt && member_decl_opt != error_mark_node
39449 && DECL_VIRTUAL_P (member_decl_opt))
39451 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39452 "implicit templates may not be %<virtual%>");
39453 DECL_VIRTUAL_P (member_decl_opt) = false;
39456 if (member_decl_opt)
39457 member_decl_opt = finish_member_template_decl (member_decl_opt);
39458 end_template_decl ();
39460 parser->fully_implicit_function_template_p = false;
39461 --parser->num_template_parameter_lists;
39463 return member_decl_opt;
39466 #include "gt-cp-parser.h"