PR c++/79512
[official-gcc.git] / gcc / cp / parser.c
blob060962db625328e4d068eb83a232768f39afd3b4
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_INTERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (C_IS_RESERVED_WORD (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value, 0);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
2555 static cp_token *cp_parser_require
2556 (cp_parser *, enum cpp_ttype, required_token);
2557 static cp_token *cp_parser_require_keyword
2558 (cp_parser *, enum rid, required_token);
2559 static bool cp_parser_token_starts_function_definition_p
2560 (cp_token *);
2561 static bool cp_parser_next_token_starts_class_definition_p
2562 (cp_parser *);
2563 static bool cp_parser_next_token_ends_template_argument_p
2564 (cp_parser *);
2565 static bool cp_parser_nth_token_starts_template_argument_list_p
2566 (cp_parser *, size_t);
2567 static enum tag_types cp_parser_token_is_class_key
2568 (cp_token *);
2569 static enum tag_types cp_parser_token_is_type_parameter_key
2570 (cp_token *);
2571 static void cp_parser_check_class_key
2572 (enum tag_types, tree type);
2573 static void cp_parser_check_access_in_redeclaration
2574 (tree type, location_t location);
2575 static bool cp_parser_optional_template_keyword
2576 (cp_parser *);
2577 static void cp_parser_pre_parsed_nested_name_specifier
2578 (cp_parser *);
2579 static bool cp_parser_cache_group
2580 (cp_parser *, enum cpp_ttype, unsigned);
2581 static tree cp_parser_cache_defarg
2582 (cp_parser *parser, bool nsdmi);
2583 static void cp_parser_parse_tentatively
2584 (cp_parser *);
2585 static void cp_parser_commit_to_tentative_parse
2586 (cp_parser *);
2587 static void cp_parser_commit_to_topmost_tentative_parse
2588 (cp_parser *);
2589 static void cp_parser_abort_tentative_parse
2590 (cp_parser *);
2591 static bool cp_parser_parse_definitely
2592 (cp_parser *);
2593 static inline bool cp_parser_parsing_tentatively
2594 (cp_parser *);
2595 static bool cp_parser_uncommitted_to_tentative_parse_p
2596 (cp_parser *);
2597 static void cp_parser_error
2598 (cp_parser *, const char *);
2599 static void cp_parser_name_lookup_error
2600 (cp_parser *, tree, tree, name_lookup_error, location_t);
2601 static bool cp_parser_simulate_error
2602 (cp_parser *);
2603 static bool cp_parser_check_type_definition
2604 (cp_parser *);
2605 static void cp_parser_check_for_definition_in_return_type
2606 (cp_declarator *, tree, location_t type_location);
2607 static void cp_parser_check_for_invalid_template_id
2608 (cp_parser *, tree, enum tag_types, location_t location);
2609 static bool cp_parser_non_integral_constant_expression
2610 (cp_parser *, non_integral_constant);
2611 static void cp_parser_diagnose_invalid_type_name
2612 (cp_parser *, tree, location_t);
2613 static bool cp_parser_parse_and_diagnose_invalid_type_name
2614 (cp_parser *);
2615 static int cp_parser_skip_to_closing_parenthesis
2616 (cp_parser *, bool, bool, bool);
2617 static void cp_parser_skip_to_end_of_statement
2618 (cp_parser *);
2619 static void cp_parser_consume_semicolon_at_end_of_statement
2620 (cp_parser *);
2621 static void cp_parser_skip_to_end_of_block_or_statement
2622 (cp_parser *);
2623 static bool cp_parser_skip_to_closing_brace
2624 (cp_parser *);
2625 static void cp_parser_skip_to_end_of_template_parameter_list
2626 (cp_parser *);
2627 static void cp_parser_skip_to_pragma_eol
2628 (cp_parser*, cp_token *);
2629 static bool cp_parser_error_occurred
2630 (cp_parser *);
2631 static bool cp_parser_allow_gnu_extensions_p
2632 (cp_parser *);
2633 static bool cp_parser_is_pure_string_literal
2634 (cp_token *);
2635 static bool cp_parser_is_string_literal
2636 (cp_token *);
2637 static bool cp_parser_is_keyword
2638 (cp_token *, enum rid);
2639 static tree cp_parser_make_typename_type
2640 (cp_parser *, tree, location_t location);
2641 static cp_declarator * cp_parser_make_indirect_declarator
2642 (enum tree_code, tree, cp_cv_quals, cp_declarator *, tree);
2643 static bool cp_parser_compound_literal_p
2644 (cp_parser *);
2645 static bool cp_parser_array_designator_p
2646 (cp_parser *);
2647 static bool cp_parser_init_statement_p
2648 (cp_parser *);
2649 static bool cp_parser_skip_to_closing_square_bracket
2650 (cp_parser *);
2652 /* Concept-related syntactic transformations */
2654 static tree cp_parser_maybe_concept_name (cp_parser *, tree);
2655 static tree cp_parser_maybe_partial_concept_id (cp_parser *, tree, tree);
2657 // -------------------------------------------------------------------------- //
2658 // Unevaluated Operand Guard
2660 // Implementation of an RAII helper for unevaluated operand parsing.
2661 cp_unevaluated::cp_unevaluated ()
2663 ++cp_unevaluated_operand;
2664 ++c_inhibit_evaluation_warnings;
2667 cp_unevaluated::~cp_unevaluated ()
2669 --c_inhibit_evaluation_warnings;
2670 --cp_unevaluated_operand;
2673 // -------------------------------------------------------------------------- //
2674 // Tentative Parsing
2676 /* Returns nonzero if we are parsing tentatively. */
2678 static inline bool
2679 cp_parser_parsing_tentatively (cp_parser* parser)
2681 return parser->context->next != NULL;
2684 /* Returns nonzero if TOKEN is a string literal. */
2686 static bool
2687 cp_parser_is_pure_string_literal (cp_token* token)
2689 return (token->type == CPP_STRING ||
2690 token->type == CPP_STRING16 ||
2691 token->type == CPP_STRING32 ||
2692 token->type == CPP_WSTRING ||
2693 token->type == CPP_UTF8STRING);
2696 /* Returns nonzero if TOKEN is a string literal
2697 of a user-defined string literal. */
2699 static bool
2700 cp_parser_is_string_literal (cp_token* token)
2702 return (cp_parser_is_pure_string_literal (token) ||
2703 token->type == CPP_STRING_USERDEF ||
2704 token->type == CPP_STRING16_USERDEF ||
2705 token->type == CPP_STRING32_USERDEF ||
2706 token->type == CPP_WSTRING_USERDEF ||
2707 token->type == CPP_UTF8STRING_USERDEF);
2710 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2712 static bool
2713 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2715 return token->keyword == keyword;
2718 /* Return TOKEN's pragma_kind if it is CPP_PRAGMA, otherwise
2719 PRAGMA_NONE. */
2721 static enum pragma_kind
2722 cp_parser_pragma_kind (cp_token *token)
2724 if (token->type != CPP_PRAGMA)
2725 return PRAGMA_NONE;
2726 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
2727 return (enum pragma_kind) TREE_INT_CST_LOW (token->u.value);
2730 /* Helper function for cp_parser_error.
2731 Having peeked a token of kind TOK1_KIND that might signify
2732 a conflict marker, peek successor tokens to determine
2733 if we actually do have a conflict marker.
2734 Specifically, we consider a run of 7 '<', '=' or '>' characters
2735 at the start of a line as a conflict marker.
2736 These come through the lexer as three pairs and a single,
2737 e.g. three CPP_LSHIFT tokens ("<<") and a CPP_LESS token ('<').
2738 If it returns true, *OUT_LOC is written to with the location/range
2739 of the marker. */
2741 static bool
2742 cp_lexer_peek_conflict_marker (cp_lexer *lexer, enum cpp_ttype tok1_kind,
2743 location_t *out_loc)
2745 cp_token *token2 = cp_lexer_peek_nth_token (lexer, 2);
2746 if (token2->type != tok1_kind)
2747 return false;
2748 cp_token *token3 = cp_lexer_peek_nth_token (lexer, 3);
2749 if (token3->type != tok1_kind)
2750 return false;
2751 cp_token *token4 = cp_lexer_peek_nth_token (lexer, 4);
2752 if (token4->type != conflict_marker_get_final_tok_kind (tok1_kind))
2753 return false;
2755 /* It must be at the start of the line. */
2756 location_t start_loc = cp_lexer_peek_token (lexer)->location;
2757 if (LOCATION_COLUMN (start_loc) != 1)
2758 return false;
2760 /* We have a conflict marker. Construct a location of the form:
2761 <<<<<<<
2762 ^~~~~~~
2763 with start == caret, finishing at the end of the marker. */
2764 location_t finish_loc = get_finish (token4->location);
2765 *out_loc = make_location (start_loc, start_loc, finish_loc);
2767 return true;
2770 /* If not parsing tentatively, issue a diagnostic of the form
2771 FILE:LINE: MESSAGE before TOKEN
2772 where TOKEN is the next token in the input stream. MESSAGE
2773 (specified by the caller) is usually of the form "expected
2774 OTHER-TOKEN". */
2776 static void
2777 cp_parser_error (cp_parser* parser, const char* gmsgid)
2779 if (!cp_parser_simulate_error (parser))
2781 cp_token *token = cp_lexer_peek_token (parser->lexer);
2782 /* This diagnostic makes more sense if it is tagged to the line
2783 of the token we just peeked at. */
2784 cp_lexer_set_source_position_from_token (token);
2786 if (token->type == CPP_PRAGMA)
2788 error_at (token->location,
2789 "%<#pragma%> is not allowed here");
2790 cp_parser_skip_to_pragma_eol (parser, token);
2791 return;
2794 /* If this is actually a conflict marker, report it as such. */
2795 if (token->type == CPP_LSHIFT
2796 || token->type == CPP_RSHIFT
2797 || token->type == CPP_EQ_EQ)
2799 location_t loc;
2800 if (cp_lexer_peek_conflict_marker (parser->lexer, token->type, &loc))
2802 error_at (loc, "version control conflict marker in file");
2803 return;
2807 c_parse_error (gmsgid,
2808 /* Because c_parser_error does not understand
2809 CPP_KEYWORD, keywords are treated like
2810 identifiers. */
2811 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2812 token->u.value, token->flags);
2816 /* Issue an error about name-lookup failing. NAME is the
2817 IDENTIFIER_NODE DECL is the result of
2818 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2819 the thing that we hoped to find. */
2821 static void
2822 cp_parser_name_lookup_error (cp_parser* parser,
2823 tree name,
2824 tree decl,
2825 name_lookup_error desired,
2826 location_t location)
2828 /* If name lookup completely failed, tell the user that NAME was not
2829 declared. */
2830 if (decl == error_mark_node)
2832 if (parser->scope && parser->scope != global_namespace)
2833 error_at (location, "%<%E::%E%> has not been declared",
2834 parser->scope, name);
2835 else if (parser->scope == global_namespace)
2836 error_at (location, "%<::%E%> has not been declared", name);
2837 else if (parser->object_scope
2838 && !CLASS_TYPE_P (parser->object_scope))
2839 error_at (location, "request for member %qE in non-class type %qT",
2840 name, parser->object_scope);
2841 else if (parser->object_scope)
2842 error_at (location, "%<%T::%E%> has not been declared",
2843 parser->object_scope, name);
2844 else
2845 error_at (location, "%qE has not been declared", name);
2847 else if (parser->scope && parser->scope != global_namespace)
2849 switch (desired)
2851 case NLE_TYPE:
2852 error_at (location, "%<%E::%E%> is not a type",
2853 parser->scope, name);
2854 break;
2855 case NLE_CXX98:
2856 error_at (location, "%<%E::%E%> is not a class or namespace",
2857 parser->scope, name);
2858 break;
2859 case NLE_NOT_CXX98:
2860 error_at (location,
2861 "%<%E::%E%> is not a class, namespace, or enumeration",
2862 parser->scope, name);
2863 break;
2864 default:
2865 gcc_unreachable ();
2869 else if (parser->scope == global_namespace)
2871 switch (desired)
2873 case NLE_TYPE:
2874 error_at (location, "%<::%E%> is not a type", name);
2875 break;
2876 case NLE_CXX98:
2877 error_at (location, "%<::%E%> is not a class or namespace", name);
2878 break;
2879 case NLE_NOT_CXX98:
2880 error_at (location,
2881 "%<::%E%> is not a class, namespace, or enumeration",
2882 name);
2883 break;
2884 default:
2885 gcc_unreachable ();
2888 else
2890 switch (desired)
2892 case NLE_TYPE:
2893 error_at (location, "%qE is not a type", name);
2894 break;
2895 case NLE_CXX98:
2896 error_at (location, "%qE is not a class or namespace", name);
2897 break;
2898 case NLE_NOT_CXX98:
2899 error_at (location,
2900 "%qE is not a class, namespace, or enumeration", name);
2901 break;
2902 default:
2903 gcc_unreachable ();
2908 /* If we are parsing tentatively, remember that an error has occurred
2909 during this tentative parse. Returns true if the error was
2910 simulated; false if a message should be issued by the caller. */
2912 static bool
2913 cp_parser_simulate_error (cp_parser* parser)
2915 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2917 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2918 return true;
2920 return false;
2923 /* This function is called when a type is defined. If type
2924 definitions are forbidden at this point, an error message is
2925 issued. */
2927 static bool
2928 cp_parser_check_type_definition (cp_parser* parser)
2930 /* If types are forbidden here, issue a message. */
2931 if (parser->type_definition_forbidden_message)
2933 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2934 in the message need to be interpreted. */
2935 error (parser->type_definition_forbidden_message);
2936 return false;
2938 return true;
2941 /* This function is called when the DECLARATOR is processed. The TYPE
2942 was a type defined in the decl-specifiers. If it is invalid to
2943 define a type in the decl-specifiers for DECLARATOR, an error is
2944 issued. TYPE_LOCATION is the location of TYPE and is used
2945 for error reporting. */
2947 static void
2948 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2949 tree type, location_t type_location)
2951 /* [dcl.fct] forbids type definitions in return types.
2952 Unfortunately, it's not easy to know whether or not we are
2953 processing a return type until after the fact. */
2954 while (declarator
2955 && (declarator->kind == cdk_pointer
2956 || declarator->kind == cdk_reference
2957 || declarator->kind == cdk_ptrmem))
2958 declarator = declarator->declarator;
2959 if (declarator
2960 && declarator->kind == cdk_function)
2962 error_at (type_location,
2963 "new types may not be defined in a return type");
2964 inform (type_location,
2965 "(perhaps a semicolon is missing after the definition of %qT)",
2966 type);
2970 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2971 "<" in any valid C++ program. If the next token is indeed "<",
2972 issue a message warning the user about what appears to be an
2973 invalid attempt to form a template-id. LOCATION is the location
2974 of the type-specifier (TYPE) */
2976 static void
2977 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2978 tree type,
2979 enum tag_types tag_type,
2980 location_t location)
2982 cp_token_position start = 0;
2984 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2986 if (TYPE_P (type))
2987 error_at (location, "%qT is not a template", type);
2988 else if (identifier_p (type))
2990 if (tag_type != none_type)
2991 error_at (location, "%qE is not a class template", type);
2992 else
2993 error_at (location, "%qE is not a template", type);
2995 else
2996 error_at (location, "invalid template-id");
2997 /* Remember the location of the invalid "<". */
2998 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2999 start = cp_lexer_token_position (parser->lexer, true);
3000 /* Consume the "<". */
3001 cp_lexer_consume_token (parser->lexer);
3002 /* Parse the template arguments. */
3003 cp_parser_enclosed_template_argument_list (parser);
3004 /* Permanently remove the invalid template arguments so that
3005 this error message is not issued again. */
3006 if (start)
3007 cp_lexer_purge_tokens_after (parser->lexer, start);
3011 /* If parsing an integral constant-expression, issue an error message
3012 about the fact that THING appeared and return true. Otherwise,
3013 return false. In either case, set
3014 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3016 static bool
3017 cp_parser_non_integral_constant_expression (cp_parser *parser,
3018 non_integral_constant thing)
3020 parser->non_integral_constant_expression_p = true;
3021 if (parser->integral_constant_expression_p)
3023 if (!parser->allow_non_integral_constant_expression_p)
3025 const char *msg = NULL;
3026 switch (thing)
3028 case NIC_FLOAT:
3029 pedwarn (input_location, OPT_Wpedantic,
3030 "ISO C++ forbids using a floating-point literal "
3031 "in a constant-expression");
3032 return true;
3033 case NIC_CAST:
3034 error ("a cast to a type other than an integral or "
3035 "enumeration type cannot appear in a "
3036 "constant-expression");
3037 return true;
3038 case NIC_TYPEID:
3039 error ("%<typeid%> operator "
3040 "cannot appear in a constant-expression");
3041 return true;
3042 case NIC_NCC:
3043 error ("non-constant compound literals "
3044 "cannot appear in a constant-expression");
3045 return true;
3046 case NIC_FUNC_CALL:
3047 error ("a function call "
3048 "cannot appear in a constant-expression");
3049 return true;
3050 case NIC_INC:
3051 error ("an increment "
3052 "cannot appear in a constant-expression");
3053 return true;
3054 case NIC_DEC:
3055 error ("an decrement "
3056 "cannot appear in a constant-expression");
3057 return true;
3058 case NIC_ARRAY_REF:
3059 error ("an array reference "
3060 "cannot appear in a constant-expression");
3061 return true;
3062 case NIC_ADDR_LABEL:
3063 error ("the address of a label "
3064 "cannot appear in a constant-expression");
3065 return true;
3066 case NIC_OVERLOADED:
3067 error ("calls to overloaded operators "
3068 "cannot appear in a constant-expression");
3069 return true;
3070 case NIC_ASSIGNMENT:
3071 error ("an assignment cannot appear in a constant-expression");
3072 return true;
3073 case NIC_COMMA:
3074 error ("a comma operator "
3075 "cannot appear in a constant-expression");
3076 return true;
3077 case NIC_CONSTRUCTOR:
3078 error ("a call to a constructor "
3079 "cannot appear in a constant-expression");
3080 return true;
3081 case NIC_TRANSACTION:
3082 error ("a transaction expression "
3083 "cannot appear in a constant-expression");
3084 return true;
3085 case NIC_THIS:
3086 msg = "this";
3087 break;
3088 case NIC_FUNC_NAME:
3089 msg = "__FUNCTION__";
3090 break;
3091 case NIC_PRETTY_FUNC:
3092 msg = "__PRETTY_FUNCTION__";
3093 break;
3094 case NIC_C99_FUNC:
3095 msg = "__func__";
3096 break;
3097 case NIC_VA_ARG:
3098 msg = "va_arg";
3099 break;
3100 case NIC_ARROW:
3101 msg = "->";
3102 break;
3103 case NIC_POINT:
3104 msg = ".";
3105 break;
3106 case NIC_STAR:
3107 msg = "*";
3108 break;
3109 case NIC_ADDR:
3110 msg = "&";
3111 break;
3112 case NIC_PREINCREMENT:
3113 msg = "++";
3114 break;
3115 case NIC_PREDECREMENT:
3116 msg = "--";
3117 break;
3118 case NIC_NEW:
3119 msg = "new";
3120 break;
3121 case NIC_DEL:
3122 msg = "delete";
3123 break;
3124 default:
3125 gcc_unreachable ();
3127 if (msg)
3128 error ("%qs cannot appear in a constant-expression", msg);
3129 return true;
3132 return false;
3135 /* Emit a diagnostic for an invalid type name. This function commits
3136 to the current active tentative parse, if any. (Otherwise, the
3137 problematic construct might be encountered again later, resulting
3138 in duplicate error messages.) LOCATION is the location of ID. */
3140 static void
3141 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3142 location_t location)
3144 tree decl, ambiguous_decls;
3145 cp_parser_commit_to_tentative_parse (parser);
3146 /* Try to lookup the identifier. */
3147 decl = cp_parser_lookup_name (parser, id, none_type,
3148 /*is_template=*/false,
3149 /*is_namespace=*/false,
3150 /*check_dependency=*/true,
3151 &ambiguous_decls, location);
3152 if (ambiguous_decls)
3153 /* If the lookup was ambiguous, an error will already have
3154 been issued. */
3155 return;
3156 /* If the lookup found a template-name, it means that the user forgot
3157 to specify an argument list. Emit a useful error message. */
3158 if (DECL_TYPE_TEMPLATE_P (decl))
3160 error_at (location,
3161 "invalid use of template-name %qE without an argument list",
3162 decl);
3163 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3164 inform (location, "class template argument deduction is only available "
3165 "with -std=c++1z or -std=gnu++1z");
3166 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3168 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3169 error_at (location, "invalid use of destructor %qD as a type", id);
3170 else if (TREE_CODE (decl) == TYPE_DECL)
3171 /* Something like 'unsigned A a;' */
3172 error_at (location, "invalid combination of multiple type-specifiers");
3173 else if (!parser->scope)
3175 /* Issue an error message. */
3176 const char *suggestion = NULL;
3177 if (TREE_CODE (id) == IDENTIFIER_NODE)
3178 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3179 if (suggestion)
3181 gcc_rich_location richloc (location);
3182 richloc.add_fixit_replace (suggestion);
3183 error_at_rich_loc (&richloc,
3184 "%qE does not name a type; did you mean %qs?",
3185 id, suggestion);
3187 else
3188 error_at (location, "%qE does not name a type", id);
3189 /* If we're in a template class, it's possible that the user was
3190 referring to a type from a base class. For example:
3192 template <typename T> struct A { typedef T X; };
3193 template <typename T> struct B : public A<T> { X x; };
3195 The user should have said "typename A<T>::X". */
3196 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3197 inform (location, "C++11 %<constexpr%> only available with "
3198 "-std=c++11 or -std=gnu++11");
3199 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3200 inform (location, "C++11 %<noexcept%> only available with "
3201 "-std=c++11 or -std=gnu++11");
3202 else if (cxx_dialect < cxx11
3203 && TREE_CODE (id) == IDENTIFIER_NODE
3204 && !strcmp (IDENTIFIER_POINTER (id), "thread_local"))
3205 inform (location, "C++11 %<thread_local%> only available with "
3206 "-std=c++11 or -std=gnu++11");
3207 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3208 inform (location, "%<concept%> only available with -fconcepts");
3209 else if (processing_template_decl && current_class_type
3210 && TYPE_BINFO (current_class_type))
3212 tree b;
3214 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3216 b = TREE_CHAIN (b))
3218 tree base_type = BINFO_TYPE (b);
3219 if (CLASS_TYPE_P (base_type)
3220 && dependent_type_p (base_type))
3222 tree field;
3223 /* Go from a particular instantiation of the
3224 template (which will have an empty TYPE_FIELDs),
3225 to the main version. */
3226 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3227 for (field = TYPE_FIELDS (base_type);
3228 field;
3229 field = DECL_CHAIN (field))
3230 if (TREE_CODE (field) == TYPE_DECL
3231 && DECL_NAME (field) == id)
3233 inform (location,
3234 "(perhaps %<typename %T::%E%> was intended)",
3235 BINFO_TYPE (b), id);
3236 break;
3238 if (field)
3239 break;
3244 /* Here we diagnose qualified-ids where the scope is actually correct,
3245 but the identifier does not resolve to a valid type name. */
3246 else if (parser->scope != error_mark_node)
3248 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3250 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3251 error_at (location_of (id),
3252 "%qE in namespace %qE does not name a template type",
3253 id, parser->scope);
3254 else
3255 error_at (location_of (id),
3256 "%qE in namespace %qE does not name a type",
3257 id, parser->scope);
3258 if (DECL_P (decl))
3259 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3261 else if (CLASS_TYPE_P (parser->scope)
3262 && constructor_name_p (id, parser->scope))
3264 /* A<T>::A<T>() */
3265 error_at (location, "%<%T::%E%> names the constructor, not"
3266 " the type", parser->scope, id);
3267 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3268 error_at (location, "and %qT has no template constructors",
3269 parser->scope);
3271 else if (TYPE_P (parser->scope)
3272 && dependent_scope_p (parser->scope))
3273 error_at (location, "need %<typename%> before %<%T::%E%> because "
3274 "%qT is a dependent scope",
3275 parser->scope, id, parser->scope);
3276 else if (TYPE_P (parser->scope))
3278 if (!COMPLETE_TYPE_P (parser->scope))
3279 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3280 parser->scope);
3281 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3282 error_at (location_of (id),
3283 "%qE in %q#T does not name a template type",
3284 id, parser->scope);
3285 else
3286 error_at (location_of (id),
3287 "%qE in %q#T does not name a type",
3288 id, parser->scope);
3289 if (DECL_P (decl))
3290 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3292 else
3293 gcc_unreachable ();
3297 /* Check for a common situation where a type-name should be present,
3298 but is not, and issue a sensible error message. Returns true if an
3299 invalid type-name was detected.
3301 The situation handled by this function are variable declarations of the
3302 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3303 Usually, `ID' should name a type, but if we got here it means that it
3304 does not. We try to emit the best possible error message depending on
3305 how exactly the id-expression looks like. */
3307 static bool
3308 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3310 tree id;
3311 cp_token *token = cp_lexer_peek_token (parser->lexer);
3313 /* Avoid duplicate error about ambiguous lookup. */
3314 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3316 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3317 if (next->type == CPP_NAME && next->error_reported)
3318 goto out;
3321 cp_parser_parse_tentatively (parser);
3322 id = cp_parser_id_expression (parser,
3323 /*template_keyword_p=*/false,
3324 /*check_dependency_p=*/true,
3325 /*template_p=*/NULL,
3326 /*declarator_p=*/true,
3327 /*optional_p=*/false);
3328 /* If the next token is a (, this is a function with no explicit return
3329 type, i.e. constructor, destructor or conversion op. */
3330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3331 || TREE_CODE (id) == TYPE_DECL)
3333 cp_parser_abort_tentative_parse (parser);
3334 return false;
3336 if (!cp_parser_parse_definitely (parser))
3337 return false;
3339 /* Emit a diagnostic for the invalid type. */
3340 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3341 out:
3342 /* If we aren't in the middle of a declarator (i.e. in a
3343 parameter-declaration-clause), skip to the end of the declaration;
3344 there's no point in trying to process it. */
3345 if (!parser->in_declarator_p)
3346 cp_parser_skip_to_end_of_block_or_statement (parser);
3347 return true;
3350 /* Consume tokens up to, and including, the next non-nested closing `)'.
3351 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3352 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3353 found an unnested token of that type. */
3355 static int
3356 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3357 bool recovering,
3358 cpp_ttype or_ttype,
3359 bool consume_paren)
3361 unsigned paren_depth = 0;
3362 unsigned brace_depth = 0;
3363 unsigned square_depth = 0;
3365 if (recovering && or_ttype == CPP_EOF
3366 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3367 return 0;
3369 while (true)
3371 cp_token * token = cp_lexer_peek_token (parser->lexer);
3373 /* Have we found what we're looking for before the closing paren? */
3374 if (token->type == or_ttype && or_ttype != CPP_EOF
3375 && !brace_depth && !paren_depth && !square_depth)
3376 return -1;
3378 switch (token->type)
3380 case CPP_EOF:
3381 case CPP_PRAGMA_EOL:
3382 /* If we've run out of tokens, then there is no closing `)'. */
3383 return 0;
3385 /* This is good for lambda expression capture-lists. */
3386 case CPP_OPEN_SQUARE:
3387 ++square_depth;
3388 break;
3389 case CPP_CLOSE_SQUARE:
3390 if (!square_depth--)
3391 return 0;
3392 break;
3394 case CPP_SEMICOLON:
3395 /* This matches the processing in skip_to_end_of_statement. */
3396 if (!brace_depth)
3397 return 0;
3398 break;
3400 case CPP_OPEN_BRACE:
3401 ++brace_depth;
3402 break;
3403 case CPP_CLOSE_BRACE:
3404 if (!brace_depth--)
3405 return 0;
3406 break;
3408 case CPP_OPEN_PAREN:
3409 if (!brace_depth)
3410 ++paren_depth;
3411 break;
3413 case CPP_CLOSE_PAREN:
3414 if (!brace_depth && !paren_depth--)
3416 if (consume_paren)
3417 cp_lexer_consume_token (parser->lexer);
3418 return 1;
3420 break;
3422 default:
3423 break;
3426 /* Consume the token. */
3427 cp_lexer_consume_token (parser->lexer);
3431 /* Consume tokens up to, and including, the next non-nested closing `)'.
3432 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3433 are doing error recovery. Returns -1 if OR_COMMA is true and we
3434 found an unnested token of that type. */
3436 static int
3437 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3438 bool recovering,
3439 bool or_comma,
3440 bool consume_paren)
3442 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3443 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3444 ttype, consume_paren);
3447 /* Consume tokens until we reach the end of the current statement.
3448 Normally, that will be just before consuming a `;'. However, if a
3449 non-nested `}' comes first, then we stop before consuming that. */
3451 static void
3452 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3454 unsigned nesting_depth = 0;
3456 /* Unwind generic function template scope if necessary. */
3457 if (parser->fully_implicit_function_template_p)
3458 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3460 while (true)
3462 cp_token *token = cp_lexer_peek_token (parser->lexer);
3464 switch (token->type)
3466 case CPP_EOF:
3467 case CPP_PRAGMA_EOL:
3468 /* If we've run out of tokens, stop. */
3469 return;
3471 case CPP_SEMICOLON:
3472 /* If the next token is a `;', we have reached the end of the
3473 statement. */
3474 if (!nesting_depth)
3475 return;
3476 break;
3478 case CPP_CLOSE_BRACE:
3479 /* If this is a non-nested '}', stop before consuming it.
3480 That way, when confronted with something like:
3482 { 3 + }
3484 we stop before consuming the closing '}', even though we
3485 have not yet reached a `;'. */
3486 if (nesting_depth == 0)
3487 return;
3489 /* If it is the closing '}' for a block that we have
3490 scanned, stop -- but only after consuming the token.
3491 That way given:
3493 void f g () { ... }
3494 typedef int I;
3496 we will stop after the body of the erroneously declared
3497 function, but before consuming the following `typedef'
3498 declaration. */
3499 if (--nesting_depth == 0)
3501 cp_lexer_consume_token (parser->lexer);
3502 return;
3504 break;
3506 case CPP_OPEN_BRACE:
3507 ++nesting_depth;
3508 break;
3510 default:
3511 break;
3514 /* Consume the token. */
3515 cp_lexer_consume_token (parser->lexer);
3519 /* This function is called at the end of a statement or declaration.
3520 If the next token is a semicolon, it is consumed; otherwise, error
3521 recovery is attempted. */
3523 static void
3524 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3526 /* Look for the trailing `;'. */
3527 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3529 /* If there is additional (erroneous) input, skip to the end of
3530 the statement. */
3531 cp_parser_skip_to_end_of_statement (parser);
3532 /* If the next token is now a `;', consume it. */
3533 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3534 cp_lexer_consume_token (parser->lexer);
3538 /* Skip tokens until we have consumed an entire block, or until we
3539 have consumed a non-nested `;'. */
3541 static void
3542 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3544 int nesting_depth = 0;
3546 /* Unwind generic function template scope if necessary. */
3547 if (parser->fully_implicit_function_template_p)
3548 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3550 while (nesting_depth >= 0)
3552 cp_token *token = cp_lexer_peek_token (parser->lexer);
3554 switch (token->type)
3556 case CPP_EOF:
3557 case CPP_PRAGMA_EOL:
3558 /* If we've run out of tokens, stop. */
3559 return;
3561 case CPP_SEMICOLON:
3562 /* Stop if this is an unnested ';'. */
3563 if (!nesting_depth)
3564 nesting_depth = -1;
3565 break;
3567 case CPP_CLOSE_BRACE:
3568 /* Stop if this is an unnested '}', or closes the outermost
3569 nesting level. */
3570 nesting_depth--;
3571 if (nesting_depth < 0)
3572 return;
3573 if (!nesting_depth)
3574 nesting_depth = -1;
3575 break;
3577 case CPP_OPEN_BRACE:
3578 /* Nest. */
3579 nesting_depth++;
3580 break;
3582 default:
3583 break;
3586 /* Consume the token. */
3587 cp_lexer_consume_token (parser->lexer);
3591 /* Skip tokens until a non-nested closing curly brace is the next
3592 token, or there are no more tokens. Return true in the first case,
3593 false otherwise. */
3595 static bool
3596 cp_parser_skip_to_closing_brace (cp_parser *parser)
3598 unsigned nesting_depth = 0;
3600 while (true)
3602 cp_token *token = cp_lexer_peek_token (parser->lexer);
3604 switch (token->type)
3606 case CPP_EOF:
3607 case CPP_PRAGMA_EOL:
3608 /* If we've run out of tokens, stop. */
3609 return false;
3611 case CPP_CLOSE_BRACE:
3612 /* If the next token is a non-nested `}', then we have reached
3613 the end of the current block. */
3614 if (nesting_depth-- == 0)
3615 return true;
3616 break;
3618 case CPP_OPEN_BRACE:
3619 /* If it the next token is a `{', then we are entering a new
3620 block. Consume the entire block. */
3621 ++nesting_depth;
3622 break;
3624 default:
3625 break;
3628 /* Consume the token. */
3629 cp_lexer_consume_token (parser->lexer);
3633 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3634 parameter is the PRAGMA token, allowing us to purge the entire pragma
3635 sequence. */
3637 static void
3638 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3640 cp_token *token;
3642 parser->lexer->in_pragma = false;
3645 token = cp_lexer_consume_token (parser->lexer);
3646 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3648 /* Ensure that the pragma is not parsed again. */
3649 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3652 /* Require pragma end of line, resyncing with it as necessary. The
3653 arguments are as for cp_parser_skip_to_pragma_eol. */
3655 static void
3656 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3658 parser->lexer->in_pragma = false;
3659 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3660 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3663 /* This is a simple wrapper around make_typename_type. When the id is
3664 an unresolved identifier node, we can provide a superior diagnostic
3665 using cp_parser_diagnose_invalid_type_name. */
3667 static tree
3668 cp_parser_make_typename_type (cp_parser *parser, tree id,
3669 location_t id_location)
3671 tree result;
3672 if (identifier_p (id))
3674 result = make_typename_type (parser->scope, id, typename_type,
3675 /*complain=*/tf_none);
3676 if (result == error_mark_node)
3677 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3678 return result;
3680 return make_typename_type (parser->scope, id, typename_type, tf_error);
3683 /* This is a wrapper around the
3684 make_{pointer,ptrmem,reference}_declarator functions that decides
3685 which one to call based on the CODE and CLASS_TYPE arguments. The
3686 CODE argument should be one of the values returned by
3687 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3688 appertain to the pointer or reference. */
3690 static cp_declarator *
3691 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3692 cp_cv_quals cv_qualifiers,
3693 cp_declarator *target,
3694 tree attributes)
3696 if (code == ERROR_MARK)
3697 return cp_error_declarator;
3699 if (code == INDIRECT_REF)
3700 if (class_type == NULL_TREE)
3701 return make_pointer_declarator (cv_qualifiers, target, attributes);
3702 else
3703 return make_ptrmem_declarator (cv_qualifiers, class_type,
3704 target, attributes);
3705 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3706 return make_reference_declarator (cv_qualifiers, target,
3707 false, attributes);
3708 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3709 return make_reference_declarator (cv_qualifiers, target,
3710 true, attributes);
3711 gcc_unreachable ();
3714 /* Create a new C++ parser. */
3716 static cp_parser *
3717 cp_parser_new (void)
3719 cp_parser *parser;
3720 cp_lexer *lexer;
3721 unsigned i;
3723 /* cp_lexer_new_main is called before doing GC allocation because
3724 cp_lexer_new_main might load a PCH file. */
3725 lexer = cp_lexer_new_main ();
3727 /* Initialize the binops_by_token so that we can get the tree
3728 directly from the token. */
3729 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3730 binops_by_token[binops[i].token_type] = binops[i];
3732 parser = ggc_cleared_alloc<cp_parser> ();
3733 parser->lexer = lexer;
3734 parser->context = cp_parser_context_new (NULL);
3736 /* For now, we always accept GNU extensions. */
3737 parser->allow_gnu_extensions_p = 1;
3739 /* The `>' token is a greater-than operator, not the end of a
3740 template-id. */
3741 parser->greater_than_is_operator_p = true;
3743 parser->default_arg_ok_p = true;
3745 /* We are not parsing a constant-expression. */
3746 parser->integral_constant_expression_p = false;
3747 parser->allow_non_integral_constant_expression_p = false;
3748 parser->non_integral_constant_expression_p = false;
3750 /* Local variable names are not forbidden. */
3751 parser->local_variables_forbidden_p = false;
3753 /* We are not processing an `extern "C"' declaration. */
3754 parser->in_unbraced_linkage_specification_p = false;
3756 /* We are not processing a declarator. */
3757 parser->in_declarator_p = false;
3759 /* We are not processing a template-argument-list. */
3760 parser->in_template_argument_list_p = false;
3762 /* We are not in an iteration statement. */
3763 parser->in_statement = 0;
3765 /* We are not in a switch statement. */
3766 parser->in_switch_statement_p = false;
3768 /* We are not parsing a type-id inside an expression. */
3769 parser->in_type_id_in_expr_p = false;
3771 /* Declarations aren't implicitly extern "C". */
3772 parser->implicit_extern_c = false;
3774 /* String literals should be translated to the execution character set. */
3775 parser->translate_strings_p = true;
3777 /* We are not parsing a function body. */
3778 parser->in_function_body = false;
3780 /* We can correct until told otherwise. */
3781 parser->colon_corrects_to_scope_p = true;
3783 /* The unparsed function queue is empty. */
3784 push_unparsed_function_queues (parser);
3786 /* There are no classes being defined. */
3787 parser->num_classes_being_defined = 0;
3789 /* No template parameters apply. */
3790 parser->num_template_parameter_lists = 0;
3792 /* Special parsing data structures. */
3793 parser->omp_declare_simd = NULL;
3794 parser->cilk_simd_fn_info = NULL;
3795 parser->oacc_routine = NULL;
3797 /* Not declaring an implicit function template. */
3798 parser->auto_is_implicit_function_template_parm_p = false;
3799 parser->fully_implicit_function_template_p = false;
3800 parser->implicit_template_parms = 0;
3801 parser->implicit_template_scope = 0;
3803 /* Allow constrained-type-specifiers. */
3804 parser->prevent_constrained_type_specifiers = 0;
3806 return parser;
3809 /* Create a cp_lexer structure which will emit the tokens in CACHE
3810 and push it onto the parser's lexer stack. This is used for delayed
3811 parsing of in-class method bodies and default arguments, and should
3812 not be confused with tentative parsing. */
3813 static void
3814 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3816 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3817 lexer->next = parser->lexer;
3818 parser->lexer = lexer;
3820 /* Move the current source position to that of the first token in the
3821 new lexer. */
3822 cp_lexer_set_source_position_from_token (lexer->next_token);
3825 /* Pop the top lexer off the parser stack. This is never used for the
3826 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3827 static void
3828 cp_parser_pop_lexer (cp_parser *parser)
3830 cp_lexer *lexer = parser->lexer;
3831 parser->lexer = lexer->next;
3832 cp_lexer_destroy (lexer);
3834 /* Put the current source position back where it was before this
3835 lexer was pushed. */
3836 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3839 /* Lexical conventions [gram.lex] */
3841 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3842 identifier. */
3844 static cp_expr
3845 cp_parser_identifier (cp_parser* parser)
3847 cp_token *token;
3849 /* Look for the identifier. */
3850 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3851 /* Return the value. */
3852 if (token)
3853 return cp_expr (token->u.value, token->location);
3854 else
3855 return error_mark_node;
3858 /* Parse a sequence of adjacent string constants. Returns a
3859 TREE_STRING representing the combined, nul-terminated string
3860 constant. If TRANSLATE is true, translate the string to the
3861 execution character set. If WIDE_OK is true, a wide string is
3862 invalid here.
3864 C++98 [lex.string] says that if a narrow string literal token is
3865 adjacent to a wide string literal token, the behavior is undefined.
3866 However, C99 6.4.5p4 says that this results in a wide string literal.
3867 We follow C99 here, for consistency with the C front end.
3869 This code is largely lifted from lex_string() in c-lex.c.
3871 FUTURE: ObjC++ will need to handle @-strings here. */
3872 static cp_expr
3873 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3874 bool lookup_udlit = true)
3876 tree value;
3877 size_t count;
3878 struct obstack str_ob;
3879 cpp_string str, istr, *strs;
3880 cp_token *tok;
3881 enum cpp_ttype type, curr_type;
3882 int have_suffix_p = 0;
3883 tree string_tree;
3884 tree suffix_id = NULL_TREE;
3885 bool curr_tok_is_userdef_p = false;
3887 tok = cp_lexer_peek_token (parser->lexer);
3888 if (!cp_parser_is_string_literal (tok))
3890 cp_parser_error (parser, "expected string-literal");
3891 return error_mark_node;
3894 location_t loc = tok->location;
3896 if (cpp_userdef_string_p (tok->type))
3898 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3899 curr_type = cpp_userdef_string_remove_type (tok->type);
3900 curr_tok_is_userdef_p = true;
3902 else
3904 string_tree = tok->u.value;
3905 curr_type = tok->type;
3907 type = curr_type;
3909 /* Try to avoid the overhead of creating and destroying an obstack
3910 for the common case of just one string. */
3911 if (!cp_parser_is_string_literal
3912 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3914 cp_lexer_consume_token (parser->lexer);
3916 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3917 str.len = TREE_STRING_LENGTH (string_tree);
3918 count = 1;
3920 if (curr_tok_is_userdef_p)
3922 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3923 have_suffix_p = 1;
3924 curr_type = cpp_userdef_string_remove_type (tok->type);
3926 else
3927 curr_type = tok->type;
3929 strs = &str;
3931 else
3933 location_t last_tok_loc = tok->location;
3934 gcc_obstack_init (&str_ob);
3935 count = 0;
3939 cp_lexer_consume_token (parser->lexer);
3940 count++;
3941 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3942 str.len = TREE_STRING_LENGTH (string_tree);
3944 if (curr_tok_is_userdef_p)
3946 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3947 if (have_suffix_p == 0)
3949 suffix_id = curr_suffix_id;
3950 have_suffix_p = 1;
3952 else if (have_suffix_p == 1
3953 && curr_suffix_id != suffix_id)
3955 error ("inconsistent user-defined literal suffixes"
3956 " %qD and %qD in string literal",
3957 suffix_id, curr_suffix_id);
3958 have_suffix_p = -1;
3960 curr_type = cpp_userdef_string_remove_type (tok->type);
3962 else
3963 curr_type = tok->type;
3965 if (type != curr_type)
3967 if (type == CPP_STRING)
3968 type = curr_type;
3969 else if (curr_type != CPP_STRING)
3971 rich_location rich_loc (line_table, tok->location);
3972 rich_loc.add_range (last_tok_loc, false);
3973 error_at_rich_loc (&rich_loc,
3974 "unsupported non-standard concatenation "
3975 "of string literals");
3979 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3981 last_tok_loc = tok->location;
3983 tok = cp_lexer_peek_token (parser->lexer);
3984 if (cpp_userdef_string_p (tok->type))
3986 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3987 curr_type = cpp_userdef_string_remove_type (tok->type);
3988 curr_tok_is_userdef_p = true;
3990 else
3992 string_tree = tok->u.value;
3993 curr_type = tok->type;
3994 curr_tok_is_userdef_p = false;
3997 while (cp_parser_is_string_literal (tok));
3999 /* A string literal built by concatenation has its caret=start at
4000 the start of the initial string, and its finish at the finish of
4001 the final string literal. */
4002 loc = make_location (loc, loc, get_finish (last_tok_loc));
4004 strs = (cpp_string *) obstack_finish (&str_ob);
4007 if (type != CPP_STRING && !wide_ok)
4009 cp_parser_error (parser, "a wide string is invalid in this context");
4010 type = CPP_STRING;
4013 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4014 (parse_in, strs, count, &istr, type))
4016 value = build_string (istr.len, (const char *)istr.text);
4017 free (CONST_CAST (unsigned char *, istr.text));
4019 switch (type)
4021 default:
4022 case CPP_STRING:
4023 case CPP_UTF8STRING:
4024 TREE_TYPE (value) = char_array_type_node;
4025 break;
4026 case CPP_STRING16:
4027 TREE_TYPE (value) = char16_array_type_node;
4028 break;
4029 case CPP_STRING32:
4030 TREE_TYPE (value) = char32_array_type_node;
4031 break;
4032 case CPP_WSTRING:
4033 TREE_TYPE (value) = wchar_array_type_node;
4034 break;
4037 value = fix_string_type (value);
4039 if (have_suffix_p)
4041 tree literal = build_userdef_literal (suffix_id, value,
4042 OT_NONE, NULL_TREE);
4043 if (lookup_udlit)
4044 value = cp_parser_userdef_string_literal (literal);
4045 else
4046 value = literal;
4049 else
4050 /* cpp_interpret_string has issued an error. */
4051 value = error_mark_node;
4053 if (count > 1)
4054 obstack_free (&str_ob, 0);
4056 return cp_expr (value, loc);
4059 /* Look up a literal operator with the name and the exact arguments. */
4061 static tree
4062 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4064 tree decl, fns;
4065 decl = lookup_name (name);
4066 if (!decl || !is_overloaded_fn (decl))
4067 return error_mark_node;
4069 for (fns = decl; fns; fns = OVL_NEXT (fns))
4071 unsigned int ix;
4072 bool found = true;
4073 tree fn = OVL_CURRENT (fns);
4074 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4075 if (parmtypes != NULL_TREE)
4077 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4078 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4080 tree tparm = TREE_VALUE (parmtypes);
4081 tree targ = TREE_TYPE ((*args)[ix]);
4082 bool ptr = TYPE_PTR_P (tparm);
4083 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4084 if ((ptr || arr || !same_type_p (tparm, targ))
4085 && (!ptr || !arr
4086 || !same_type_p (TREE_TYPE (tparm),
4087 TREE_TYPE (targ))))
4088 found = false;
4090 if (found
4091 && ix == vec_safe_length (args)
4092 /* May be this should be sufficient_parms_p instead,
4093 depending on how exactly should user-defined literals
4094 work in presence of default arguments on the literal
4095 operator parameters. */
4096 && parmtypes == void_list_node)
4097 return decl;
4101 return error_mark_node;
4104 /* Parse a user-defined char constant. Returns a call to a user-defined
4105 literal operator taking the character as an argument. */
4107 static cp_expr
4108 cp_parser_userdef_char_literal (cp_parser *parser)
4110 cp_token *token = cp_lexer_consume_token (parser->lexer);
4111 tree literal = token->u.value;
4112 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4113 tree value = USERDEF_LITERAL_VALUE (literal);
4114 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4115 tree decl, result;
4117 /* Build up a call to the user-defined operator */
4118 /* Lookup the name we got back from the id-expression. */
4119 vec<tree, va_gc> *args = make_tree_vector ();
4120 vec_safe_push (args, value);
4121 decl = lookup_literal_operator (name, args);
4122 if (!decl || decl == error_mark_node)
4124 error ("unable to find character literal operator %qD with %qT argument",
4125 name, TREE_TYPE (value));
4126 release_tree_vector (args);
4127 return error_mark_node;
4129 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4130 release_tree_vector (args);
4131 return result;
4134 /* A subroutine of cp_parser_userdef_numeric_literal to
4135 create a char... template parameter pack from a string node. */
4137 static tree
4138 make_char_string_pack (tree value)
4140 tree charvec;
4141 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4142 const char *str = TREE_STRING_POINTER (value);
4143 int i, len = TREE_STRING_LENGTH (value) - 1;
4144 tree argvec = make_tree_vec (1);
4146 /* Fill in CHARVEC with all of the parameters. */
4147 charvec = make_tree_vec (len);
4148 for (i = 0; i < len; ++i)
4149 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4151 /* Build the argument packs. */
4152 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4153 TREE_TYPE (argpack) = char_type_node;
4155 TREE_VEC_ELT (argvec, 0) = argpack;
4157 return argvec;
4160 /* A subroutine of cp_parser_userdef_numeric_literal to
4161 create a char... template parameter pack from a string node. */
4163 static tree
4164 make_string_pack (tree value)
4166 tree charvec;
4167 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4168 const unsigned char *str
4169 = (const unsigned char *) TREE_STRING_POINTER (value);
4170 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4171 int len = TREE_STRING_LENGTH (value) / sz - 1;
4172 tree argvec = make_tree_vec (2);
4174 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4175 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4177 /* First template parm is character type. */
4178 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4180 /* Fill in CHARVEC with all of the parameters. */
4181 charvec = make_tree_vec (len);
4182 for (int i = 0; i < len; ++i)
4183 TREE_VEC_ELT (charvec, i)
4184 = double_int_to_tree (str_char_type_node,
4185 double_int::from_buffer (str + i * sz, sz));
4187 /* Build the argument packs. */
4188 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4189 TREE_TYPE (argpack) = str_char_type_node;
4191 TREE_VEC_ELT (argvec, 1) = argpack;
4193 return argvec;
4196 /* Parse a user-defined numeric constant. returns a call to a user-defined
4197 literal operator. */
4199 static cp_expr
4200 cp_parser_userdef_numeric_literal (cp_parser *parser)
4202 cp_token *token = cp_lexer_consume_token (parser->lexer);
4203 tree literal = token->u.value;
4204 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4205 tree value = USERDEF_LITERAL_VALUE (literal);
4206 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4207 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4208 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4209 tree decl, result;
4210 vec<tree, va_gc> *args;
4212 /* Look for a literal operator taking the exact type of numeric argument
4213 as the literal value. */
4214 args = make_tree_vector ();
4215 vec_safe_push (args, value);
4216 decl = lookup_literal_operator (name, args);
4217 if (decl && decl != error_mark_node)
4219 result = finish_call_expr (decl, &args, false, true,
4220 tf_warning_or_error);
4222 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4224 warning_at (token->location, OPT_Woverflow,
4225 "integer literal exceeds range of %qT type",
4226 long_long_unsigned_type_node);
4228 else
4230 if (overflow > 0)
4231 warning_at (token->location, OPT_Woverflow,
4232 "floating literal exceeds range of %qT type",
4233 long_double_type_node);
4234 else if (overflow < 0)
4235 warning_at (token->location, OPT_Woverflow,
4236 "floating literal truncated to zero");
4239 release_tree_vector (args);
4240 return result;
4242 release_tree_vector (args);
4244 /* If the numeric argument didn't work, look for a raw literal
4245 operator taking a const char* argument consisting of the number
4246 in string format. */
4247 args = make_tree_vector ();
4248 vec_safe_push (args, num_string);
4249 decl = lookup_literal_operator (name, args);
4250 if (decl && decl != error_mark_node)
4252 result = finish_call_expr (decl, &args, false, true,
4253 tf_warning_or_error);
4254 release_tree_vector (args);
4255 return result;
4257 release_tree_vector (args);
4259 /* If the raw literal didn't work, look for a non-type template
4260 function with parameter pack char.... Call the function with
4261 template parameter characters representing the number. */
4262 args = make_tree_vector ();
4263 decl = lookup_literal_operator (name, args);
4264 if (decl && decl != error_mark_node)
4266 tree tmpl_args = make_char_string_pack (num_string);
4267 decl = lookup_template_function (decl, tmpl_args);
4268 result = finish_call_expr (decl, &args, false, true,
4269 tf_warning_or_error);
4270 release_tree_vector (args);
4271 return result;
4274 release_tree_vector (args);
4276 error ("unable to find numeric literal operator %qD", name);
4277 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4278 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4279 "to enable more built-in suffixes");
4280 return error_mark_node;
4283 /* Parse a user-defined string constant. Returns a call to a user-defined
4284 literal operator taking a character pointer and the length of the string
4285 as arguments. */
4287 static tree
4288 cp_parser_userdef_string_literal (tree literal)
4290 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4291 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4292 tree value = USERDEF_LITERAL_VALUE (literal);
4293 int len = TREE_STRING_LENGTH (value)
4294 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4295 tree decl, result;
4296 vec<tree, va_gc> *args;
4298 /* Build up a call to the user-defined operator. */
4299 /* Lookup the name we got back from the id-expression. */
4300 args = make_tree_vector ();
4301 vec_safe_push (args, value);
4302 vec_safe_push (args, build_int_cst (size_type_node, len));
4303 decl = lookup_literal_operator (name, args);
4305 if (decl && decl != error_mark_node)
4307 result = finish_call_expr (decl, &args, false, true,
4308 tf_warning_or_error);
4309 release_tree_vector (args);
4310 return result;
4312 release_tree_vector (args);
4314 /* Look for a template function with typename parameter CharT
4315 and parameter pack CharT... Call the function with
4316 template parameter characters representing the string. */
4317 args = make_tree_vector ();
4318 decl = lookup_literal_operator (name, args);
4319 if (decl && decl != error_mark_node)
4321 tree tmpl_args = make_string_pack (value);
4322 decl = lookup_template_function (decl, tmpl_args);
4323 result = finish_call_expr (decl, &args, false, true,
4324 tf_warning_or_error);
4325 release_tree_vector (args);
4326 return result;
4328 release_tree_vector (args);
4330 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4331 name, TREE_TYPE (value), size_type_node);
4332 return error_mark_node;
4336 /* Basic concepts [gram.basic] */
4338 /* Parse a translation-unit.
4340 translation-unit:
4341 declaration-seq [opt]
4343 Returns TRUE if all went well. */
4345 static bool
4346 cp_parser_translation_unit (cp_parser* parser)
4348 /* The address of the first non-permanent object on the declarator
4349 obstack. */
4350 static void *declarator_obstack_base;
4352 bool success;
4354 /* Create the declarator obstack, if necessary. */
4355 if (!cp_error_declarator)
4357 gcc_obstack_init (&declarator_obstack);
4358 /* Create the error declarator. */
4359 cp_error_declarator = make_declarator (cdk_error);
4360 /* Create the empty parameter list. */
4361 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4362 /* Remember where the base of the declarator obstack lies. */
4363 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4366 cp_parser_declaration_seq_opt (parser);
4368 /* If there are no tokens left then all went well. */
4369 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4371 /* Get rid of the token array; we don't need it any more. */
4372 cp_lexer_destroy (parser->lexer);
4373 parser->lexer = NULL;
4375 /* This file might have been a context that's implicitly extern
4376 "C". If so, pop the lang context. (Only relevant for PCH.) */
4377 if (parser->implicit_extern_c)
4379 pop_lang_context ();
4380 parser->implicit_extern_c = false;
4383 /* Finish up. */
4384 finish_translation_unit ();
4386 success = true;
4388 else
4390 cp_parser_error (parser, "expected declaration");
4391 success = false;
4394 /* Make sure the declarator obstack was fully cleaned up. */
4395 gcc_assert (obstack_next_free (&declarator_obstack)
4396 == declarator_obstack_base);
4398 /* All went well. */
4399 return success;
4402 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4403 decltype context. */
4405 static inline tsubst_flags_t
4406 complain_flags (bool decltype_p)
4408 tsubst_flags_t complain = tf_warning_or_error;
4409 if (decltype_p)
4410 complain |= tf_decltype;
4411 return complain;
4414 /* We're about to parse a collection of statements. If we're currently
4415 parsing tentatively, set up a firewall so that any nested
4416 cp_parser_commit_to_tentative_parse won't affect the current context. */
4418 static cp_token_position
4419 cp_parser_start_tentative_firewall (cp_parser *parser)
4421 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4422 return 0;
4424 cp_parser_parse_tentatively (parser);
4425 cp_parser_commit_to_topmost_tentative_parse (parser);
4426 return cp_lexer_token_position (parser->lexer, false);
4429 /* We've finished parsing the collection of statements. Wrap up the
4430 firewall and replace the relevant tokens with the parsed form. */
4432 static void
4433 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4434 tree expr)
4436 if (!start)
4437 return;
4439 /* Finish the firewall level. */
4440 cp_parser_parse_definitely (parser);
4441 /* And remember the result of the parse for when we try again. */
4442 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4443 token->type = CPP_PREPARSED_EXPR;
4444 token->u.value = expr;
4445 token->keyword = RID_MAX;
4446 cp_lexer_purge_tokens_after (parser->lexer, start);
4449 /* Like the above functions, but let the user modify the tokens. Used by
4450 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4451 later parses, so it makes sense to localize the effects of
4452 cp_parser_commit_to_tentative_parse. */
4454 struct tentative_firewall
4456 cp_parser *parser;
4457 bool set;
4459 tentative_firewall (cp_parser *p): parser(p)
4461 /* If we're currently parsing tentatively, start a committed level as a
4462 firewall and then an inner tentative parse. */
4463 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4465 cp_parser_parse_tentatively (parser);
4466 cp_parser_commit_to_topmost_tentative_parse (parser);
4467 cp_parser_parse_tentatively (parser);
4471 ~tentative_firewall()
4473 if (set)
4475 /* Finish the inner tentative parse and the firewall, propagating any
4476 uncommitted error state to the outer tentative parse. */
4477 bool err = cp_parser_error_occurred (parser);
4478 cp_parser_parse_definitely (parser);
4479 cp_parser_parse_definitely (parser);
4480 if (err)
4481 cp_parser_simulate_error (parser);
4486 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4487 enclosing parentheses. */
4489 static cp_expr
4490 cp_parser_statement_expr (cp_parser *parser)
4492 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4494 /* Consume the '('. */
4495 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4496 cp_lexer_consume_token (parser->lexer);
4497 /* Start the statement-expression. */
4498 tree expr = begin_stmt_expr ();
4499 /* Parse the compound-statement. */
4500 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4501 /* Finish up. */
4502 expr = finish_stmt_expr (expr, false);
4503 /* Consume the ')'. */
4504 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4505 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4506 cp_parser_skip_to_end_of_statement (parser);
4508 cp_parser_end_tentative_firewall (parser, start, expr);
4509 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4510 return cp_expr (expr, combined_loc);
4513 /* Expressions [gram.expr] */
4515 /* Parse a fold-operator.
4517 fold-operator:
4518 - * / % ^ & | = < > << >>
4519 = -= *= /= %= ^= &= |= <<= >>=
4520 == != <= >= && || , .* ->*
4522 This returns the tree code corresponding to the matched operator
4523 as an int. When the current token matches a compound assignment
4524 opertor, the resulting tree code is the negative value of the
4525 non-assignment operator. */
4527 static int
4528 cp_parser_fold_operator (cp_token *token)
4530 switch (token->type)
4532 case CPP_PLUS: return PLUS_EXPR;
4533 case CPP_MINUS: return MINUS_EXPR;
4534 case CPP_MULT: return MULT_EXPR;
4535 case CPP_DIV: return TRUNC_DIV_EXPR;
4536 case CPP_MOD: return TRUNC_MOD_EXPR;
4537 case CPP_XOR: return BIT_XOR_EXPR;
4538 case CPP_AND: return BIT_AND_EXPR;
4539 case CPP_OR: return BIT_IOR_EXPR;
4540 case CPP_LSHIFT: return LSHIFT_EXPR;
4541 case CPP_RSHIFT: return RSHIFT_EXPR;
4543 case CPP_EQ: return -NOP_EXPR;
4544 case CPP_PLUS_EQ: return -PLUS_EXPR;
4545 case CPP_MINUS_EQ: return -MINUS_EXPR;
4546 case CPP_MULT_EQ: return -MULT_EXPR;
4547 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4548 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4549 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4550 case CPP_AND_EQ: return -BIT_AND_EXPR;
4551 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4552 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4553 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4555 case CPP_EQ_EQ: return EQ_EXPR;
4556 case CPP_NOT_EQ: return NE_EXPR;
4557 case CPP_LESS: return LT_EXPR;
4558 case CPP_GREATER: return GT_EXPR;
4559 case CPP_LESS_EQ: return LE_EXPR;
4560 case CPP_GREATER_EQ: return GE_EXPR;
4562 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4563 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4565 case CPP_COMMA: return COMPOUND_EXPR;
4567 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4568 case CPP_DEREF_STAR: return MEMBER_REF;
4570 default: return ERROR_MARK;
4574 /* Returns true if CODE indicates a binary expression, which is not allowed in
4575 the LHS of a fold-expression. More codes will need to be added to use this
4576 function in other contexts. */
4578 static bool
4579 is_binary_op (tree_code code)
4581 switch (code)
4583 case PLUS_EXPR:
4584 case POINTER_PLUS_EXPR:
4585 case MINUS_EXPR:
4586 case MULT_EXPR:
4587 case TRUNC_DIV_EXPR:
4588 case TRUNC_MOD_EXPR:
4589 case BIT_XOR_EXPR:
4590 case BIT_AND_EXPR:
4591 case BIT_IOR_EXPR:
4592 case LSHIFT_EXPR:
4593 case RSHIFT_EXPR:
4595 case MODOP_EXPR:
4597 case EQ_EXPR:
4598 case NE_EXPR:
4599 case LE_EXPR:
4600 case GE_EXPR:
4601 case LT_EXPR:
4602 case GT_EXPR:
4604 case TRUTH_ANDIF_EXPR:
4605 case TRUTH_ORIF_EXPR:
4607 case COMPOUND_EXPR:
4609 case DOTSTAR_EXPR:
4610 case MEMBER_REF:
4611 return true;
4613 default:
4614 return false;
4618 /* If the next token is a suitable fold operator, consume it and return as
4619 the function above. */
4621 static int
4622 cp_parser_fold_operator (cp_parser *parser)
4624 cp_token* token = cp_lexer_peek_token (parser->lexer);
4625 int code = cp_parser_fold_operator (token);
4626 if (code != ERROR_MARK)
4627 cp_lexer_consume_token (parser->lexer);
4628 return code;
4631 /* Parse a fold-expression.
4633 fold-expression:
4634 ( ... folding-operator cast-expression)
4635 ( cast-expression folding-operator ... )
4636 ( cast-expression folding operator ... folding-operator cast-expression)
4638 Note that the '(' and ')' are matched in primary expression. */
4640 static cp_expr
4641 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4643 cp_id_kind pidk;
4645 // Left fold.
4646 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4648 cp_lexer_consume_token (parser->lexer);
4649 int op = cp_parser_fold_operator (parser);
4650 if (op == ERROR_MARK)
4652 cp_parser_error (parser, "expected binary operator");
4653 return error_mark_node;
4656 tree expr = cp_parser_cast_expression (parser, false, false,
4657 false, &pidk);
4658 if (expr == error_mark_node)
4659 return error_mark_node;
4660 return finish_left_unary_fold_expr (expr, op);
4663 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4664 int op = cp_parser_fold_operator (parser);
4665 if (op == ERROR_MARK)
4667 cp_parser_error (parser, "expected binary operator");
4668 return error_mark_node;
4671 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4673 cp_parser_error (parser, "expected ...");
4674 return error_mark_node;
4676 cp_lexer_consume_token (parser->lexer);
4678 /* The operands of a fold-expression are cast-expressions, so binary or
4679 conditional expressions are not allowed. We check this here to avoid
4680 tentative parsing. */
4681 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4682 /* OK, the expression was parenthesized. */;
4683 else if (is_binary_op (TREE_CODE (expr1)))
4684 error_at (location_of (expr1),
4685 "binary expression in operand of fold-expression");
4686 else if (TREE_CODE (expr1) == COND_EXPR)
4687 error_at (location_of (expr1),
4688 "conditional expression in operand of fold-expression");
4690 // Right fold.
4691 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4692 return finish_right_unary_fold_expr (expr1, op);
4694 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4696 cp_parser_error (parser, "mismatched operator in fold-expression");
4697 return error_mark_node;
4699 cp_lexer_consume_token (parser->lexer);
4701 // Binary left or right fold.
4702 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4703 if (expr2 == error_mark_node)
4704 return error_mark_node;
4705 return finish_binary_fold_expr (expr1, expr2, op);
4708 /* Parse a primary-expression.
4710 primary-expression:
4711 literal
4712 this
4713 ( expression )
4714 id-expression
4715 lambda-expression (C++11)
4717 GNU Extensions:
4719 primary-expression:
4720 ( compound-statement )
4721 __builtin_va_arg ( assignment-expression , type-id )
4722 __builtin_offsetof ( type-id , offsetof-expression )
4724 C++ Extensions:
4725 __has_nothrow_assign ( type-id )
4726 __has_nothrow_constructor ( type-id )
4727 __has_nothrow_copy ( type-id )
4728 __has_trivial_assign ( type-id )
4729 __has_trivial_constructor ( type-id )
4730 __has_trivial_copy ( type-id )
4731 __has_trivial_destructor ( type-id )
4732 __has_virtual_destructor ( type-id )
4733 __is_abstract ( type-id )
4734 __is_base_of ( type-id , type-id )
4735 __is_class ( type-id )
4736 __is_empty ( type-id )
4737 __is_enum ( type-id )
4738 __is_final ( type-id )
4739 __is_literal_type ( type-id )
4740 __is_pod ( type-id )
4741 __is_polymorphic ( type-id )
4742 __is_std_layout ( type-id )
4743 __is_trivial ( type-id )
4744 __is_union ( type-id )
4746 Objective-C++ Extension:
4748 primary-expression:
4749 objc-expression
4751 literal:
4752 __null
4754 ADDRESS_P is true iff this expression was immediately preceded by
4755 "&" and therefore might denote a pointer-to-member. CAST_P is true
4756 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4757 true iff this expression is a template argument.
4759 Returns a representation of the expression. Upon return, *IDK
4760 indicates what kind of id-expression (if any) was present. */
4762 static cp_expr
4763 cp_parser_primary_expression (cp_parser *parser,
4764 bool address_p,
4765 bool cast_p,
4766 bool template_arg_p,
4767 bool decltype_p,
4768 cp_id_kind *idk)
4770 cp_token *token = NULL;
4772 /* Assume the primary expression is not an id-expression. */
4773 *idk = CP_ID_KIND_NONE;
4775 /* Peek at the next token. */
4776 token = cp_lexer_peek_token (parser->lexer);
4777 switch ((int) token->type)
4779 /* literal:
4780 integer-literal
4781 character-literal
4782 floating-literal
4783 string-literal
4784 boolean-literal
4785 pointer-literal
4786 user-defined-literal */
4787 case CPP_CHAR:
4788 case CPP_CHAR16:
4789 case CPP_CHAR32:
4790 case CPP_WCHAR:
4791 case CPP_UTF8CHAR:
4792 case CPP_NUMBER:
4793 case CPP_PREPARSED_EXPR:
4794 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4795 return cp_parser_userdef_numeric_literal (parser);
4796 token = cp_lexer_consume_token (parser->lexer);
4797 if (TREE_CODE (token->u.value) == FIXED_CST)
4799 error_at (token->location,
4800 "fixed-point types not supported in C++");
4801 return error_mark_node;
4803 /* Floating-point literals are only allowed in an integral
4804 constant expression if they are cast to an integral or
4805 enumeration type. */
4806 if (TREE_CODE (token->u.value) == REAL_CST
4807 && parser->integral_constant_expression_p
4808 && pedantic)
4810 /* CAST_P will be set even in invalid code like "int(2.7 +
4811 ...)". Therefore, we have to check that the next token
4812 is sure to end the cast. */
4813 if (cast_p)
4815 cp_token *next_token;
4817 next_token = cp_lexer_peek_token (parser->lexer);
4818 if (/* The comma at the end of an
4819 enumerator-definition. */
4820 next_token->type != CPP_COMMA
4821 /* The curly brace at the end of an enum-specifier. */
4822 && next_token->type != CPP_CLOSE_BRACE
4823 /* The end of a statement. */
4824 && next_token->type != CPP_SEMICOLON
4825 /* The end of the cast-expression. */
4826 && next_token->type != CPP_CLOSE_PAREN
4827 /* The end of an array bound. */
4828 && next_token->type != CPP_CLOSE_SQUARE
4829 /* The closing ">" in a template-argument-list. */
4830 && (next_token->type != CPP_GREATER
4831 || parser->greater_than_is_operator_p)
4832 /* C++0x only: A ">>" treated like two ">" tokens,
4833 in a template-argument-list. */
4834 && (next_token->type != CPP_RSHIFT
4835 || (cxx_dialect == cxx98)
4836 || parser->greater_than_is_operator_p))
4837 cast_p = false;
4840 /* If we are within a cast, then the constraint that the
4841 cast is to an integral or enumeration type will be
4842 checked at that point. If we are not within a cast, then
4843 this code is invalid. */
4844 if (!cast_p)
4845 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4847 return cp_expr (token->u.value, token->location);
4849 case CPP_CHAR_USERDEF:
4850 case CPP_CHAR16_USERDEF:
4851 case CPP_CHAR32_USERDEF:
4852 case CPP_WCHAR_USERDEF:
4853 case CPP_UTF8CHAR_USERDEF:
4854 return cp_parser_userdef_char_literal (parser);
4856 case CPP_STRING:
4857 case CPP_STRING16:
4858 case CPP_STRING32:
4859 case CPP_WSTRING:
4860 case CPP_UTF8STRING:
4861 case CPP_STRING_USERDEF:
4862 case CPP_STRING16_USERDEF:
4863 case CPP_STRING32_USERDEF:
4864 case CPP_WSTRING_USERDEF:
4865 case CPP_UTF8STRING_USERDEF:
4866 /* ??? Should wide strings be allowed when parser->translate_strings_p
4867 is false (i.e. in attributes)? If not, we can kill the third
4868 argument to cp_parser_string_literal. */
4869 return cp_parser_string_literal (parser,
4870 parser->translate_strings_p,
4871 true);
4873 case CPP_OPEN_PAREN:
4874 /* If we see `( { ' then we are looking at the beginning of
4875 a GNU statement-expression. */
4876 if (cp_parser_allow_gnu_extensions_p (parser)
4877 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4879 /* Statement-expressions are not allowed by the standard. */
4880 pedwarn (token->location, OPT_Wpedantic,
4881 "ISO C++ forbids braced-groups within expressions");
4883 /* And they're not allowed outside of a function-body; you
4884 cannot, for example, write:
4886 int i = ({ int j = 3; j + 1; });
4888 at class or namespace scope. */
4889 if (!parser->in_function_body
4890 || parser->in_template_argument_list_p)
4892 error_at (token->location,
4893 "statement-expressions are not allowed outside "
4894 "functions nor in template-argument lists");
4895 cp_parser_skip_to_end_of_block_or_statement (parser);
4896 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4897 cp_lexer_consume_token (parser->lexer);
4898 return error_mark_node;
4900 else
4901 return cp_parser_statement_expr (parser);
4903 /* Otherwise it's a normal parenthesized expression. */
4905 cp_expr expr;
4906 bool saved_greater_than_is_operator_p;
4908 location_t open_paren_loc = token->location;
4910 /* Consume the `('. */
4911 cp_lexer_consume_token (parser->lexer);
4912 /* Within a parenthesized expression, a `>' token is always
4913 the greater-than operator. */
4914 saved_greater_than_is_operator_p
4915 = parser->greater_than_is_operator_p;
4916 parser->greater_than_is_operator_p = true;
4918 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4919 /* Left fold expression. */
4920 expr = NULL_TREE;
4921 else
4922 /* Parse the parenthesized expression. */
4923 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4925 token = cp_lexer_peek_token (parser->lexer);
4926 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4928 expr = cp_parser_fold_expression (parser, expr);
4929 if (expr != error_mark_node
4930 && cxx_dialect < cxx1z
4931 && !in_system_header_at (input_location))
4932 pedwarn (input_location, 0, "fold-expressions only available "
4933 "with -std=c++1z or -std=gnu++1z");
4935 else
4936 /* Let the front end know that this expression was
4937 enclosed in parentheses. This matters in case, for
4938 example, the expression is of the form `A::B', since
4939 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4940 not. */
4941 expr = finish_parenthesized_expr (expr);
4943 /* DR 705: Wrapping an unqualified name in parentheses
4944 suppresses arg-dependent lookup. We want to pass back
4945 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4946 (c++/37862), but none of the others. */
4947 if (*idk != CP_ID_KIND_QUALIFIED)
4948 *idk = CP_ID_KIND_NONE;
4950 /* The `>' token might be the end of a template-id or
4951 template-parameter-list now. */
4952 parser->greater_than_is_operator_p
4953 = saved_greater_than_is_operator_p;
4955 /* Consume the `)'. */
4956 token = cp_lexer_peek_token (parser->lexer);
4957 location_t close_paren_loc = token->location;
4958 expr.set_range (open_paren_loc, close_paren_loc);
4959 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4960 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4961 cp_parser_skip_to_end_of_statement (parser);
4963 return expr;
4966 case CPP_OPEN_SQUARE:
4968 if (c_dialect_objc ())
4970 /* We might have an Objective-C++ message. */
4971 cp_parser_parse_tentatively (parser);
4972 tree msg = cp_parser_objc_message_expression (parser);
4973 /* If that works out, we're done ... */
4974 if (cp_parser_parse_definitely (parser))
4975 return msg;
4976 /* ... else, fall though to see if it's a lambda. */
4978 cp_expr lam = cp_parser_lambda_expression (parser);
4979 /* Don't warn about a failed tentative parse. */
4980 if (cp_parser_error_occurred (parser))
4981 return error_mark_node;
4982 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4983 return lam;
4986 case CPP_OBJC_STRING:
4987 if (c_dialect_objc ())
4988 /* We have an Objective-C++ string literal. */
4989 return cp_parser_objc_expression (parser);
4990 cp_parser_error (parser, "expected primary-expression");
4991 return error_mark_node;
4993 case CPP_KEYWORD:
4994 switch (token->keyword)
4996 /* These two are the boolean literals. */
4997 case RID_TRUE:
4998 cp_lexer_consume_token (parser->lexer);
4999 return cp_expr (boolean_true_node, token->location);
5000 case RID_FALSE:
5001 cp_lexer_consume_token (parser->lexer);
5002 return cp_expr (boolean_false_node, token->location);
5004 /* The `__null' literal. */
5005 case RID_NULL:
5006 cp_lexer_consume_token (parser->lexer);
5007 return cp_expr (null_node, token->location);
5009 /* The `nullptr' literal. */
5010 case RID_NULLPTR:
5011 cp_lexer_consume_token (parser->lexer);
5012 return cp_expr (nullptr_node, token->location);
5014 /* Recognize the `this' keyword. */
5015 case RID_THIS:
5016 cp_lexer_consume_token (parser->lexer);
5017 if (parser->local_variables_forbidden_p)
5019 error_at (token->location,
5020 "%<this%> may not be used in this context");
5021 return error_mark_node;
5023 /* Pointers cannot appear in constant-expressions. */
5024 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5025 return error_mark_node;
5026 return cp_expr (finish_this_expr (), token->location);
5028 /* The `operator' keyword can be the beginning of an
5029 id-expression. */
5030 case RID_OPERATOR:
5031 goto id_expression;
5033 case RID_FUNCTION_NAME:
5034 case RID_PRETTY_FUNCTION_NAME:
5035 case RID_C99_FUNCTION_NAME:
5037 non_integral_constant name;
5039 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5040 __func__ are the names of variables -- but they are
5041 treated specially. Therefore, they are handled here,
5042 rather than relying on the generic id-expression logic
5043 below. Grammatically, these names are id-expressions.
5045 Consume the token. */
5046 token = cp_lexer_consume_token (parser->lexer);
5048 switch (token->keyword)
5050 case RID_FUNCTION_NAME:
5051 name = NIC_FUNC_NAME;
5052 break;
5053 case RID_PRETTY_FUNCTION_NAME:
5054 name = NIC_PRETTY_FUNC;
5055 break;
5056 case RID_C99_FUNCTION_NAME:
5057 name = NIC_C99_FUNC;
5058 break;
5059 default:
5060 gcc_unreachable ();
5063 if (cp_parser_non_integral_constant_expression (parser, name))
5064 return error_mark_node;
5066 /* Look up the name. */
5067 return finish_fname (token->u.value);
5070 case RID_VA_ARG:
5072 tree expression;
5073 tree type;
5074 source_location type_location;
5075 location_t start_loc
5076 = cp_lexer_peek_token (parser->lexer)->location;
5077 /* The `__builtin_va_arg' construct is used to handle
5078 `va_arg'. Consume the `__builtin_va_arg' token. */
5079 cp_lexer_consume_token (parser->lexer);
5080 /* Look for the opening `('. */
5081 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5082 /* Now, parse the assignment-expression. */
5083 expression = cp_parser_assignment_expression (parser);
5084 /* Look for the `,'. */
5085 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5086 type_location = cp_lexer_peek_token (parser->lexer)->location;
5087 /* Parse the type-id. */
5089 type_id_in_expr_sentinel s (parser);
5090 type = cp_parser_type_id (parser);
5092 /* Look for the closing `)'. */
5093 location_t finish_loc
5094 = cp_lexer_peek_token (parser->lexer)->location;
5095 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5096 /* Using `va_arg' in a constant-expression is not
5097 allowed. */
5098 if (cp_parser_non_integral_constant_expression (parser,
5099 NIC_VA_ARG))
5100 return error_mark_node;
5101 /* Construct a location of the form:
5102 __builtin_va_arg (v, int)
5103 ~~~~~~~~~~~~~~~~~~~~~^~~~
5104 with the caret at the type, ranging from the start of the
5105 "__builtin_va_arg" token to the close paren. */
5106 location_t combined_loc
5107 = make_location (type_location, start_loc, finish_loc);
5108 return build_x_va_arg (combined_loc, expression, type);
5111 case RID_OFFSETOF:
5112 return cp_parser_builtin_offsetof (parser);
5114 case RID_HAS_NOTHROW_ASSIGN:
5115 case RID_HAS_NOTHROW_CONSTRUCTOR:
5116 case RID_HAS_NOTHROW_COPY:
5117 case RID_HAS_TRIVIAL_ASSIGN:
5118 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5119 case RID_HAS_TRIVIAL_COPY:
5120 case RID_HAS_TRIVIAL_DESTRUCTOR:
5121 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5122 case RID_HAS_VIRTUAL_DESTRUCTOR:
5123 case RID_IS_ABSTRACT:
5124 case RID_IS_BASE_OF:
5125 case RID_IS_CLASS:
5126 case RID_IS_EMPTY:
5127 case RID_IS_ENUM:
5128 case RID_IS_FINAL:
5129 case RID_IS_LITERAL_TYPE:
5130 case RID_IS_POD:
5131 case RID_IS_POLYMORPHIC:
5132 case RID_IS_SAME_AS:
5133 case RID_IS_STD_LAYOUT:
5134 case RID_IS_TRIVIAL:
5135 case RID_IS_TRIVIALLY_ASSIGNABLE:
5136 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5137 case RID_IS_TRIVIALLY_COPYABLE:
5138 case RID_IS_UNION:
5139 return cp_parser_trait_expr (parser, token->keyword);
5141 // C++ concepts
5142 case RID_REQUIRES:
5143 return cp_parser_requires_expression (parser);
5145 /* Objective-C++ expressions. */
5146 case RID_AT_ENCODE:
5147 case RID_AT_PROTOCOL:
5148 case RID_AT_SELECTOR:
5149 return cp_parser_objc_expression (parser);
5151 case RID_TEMPLATE:
5152 if (parser->in_function_body
5153 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5154 == CPP_LESS))
5156 error_at (token->location,
5157 "a template declaration cannot appear at block scope");
5158 cp_parser_skip_to_end_of_block_or_statement (parser);
5159 return error_mark_node;
5161 /* FALLTHRU */
5162 default:
5163 cp_parser_error (parser, "expected primary-expression");
5164 return error_mark_node;
5167 /* An id-expression can start with either an identifier, a
5168 `::' as the beginning of a qualified-id, or the "operator"
5169 keyword. */
5170 case CPP_NAME:
5171 case CPP_SCOPE:
5172 case CPP_TEMPLATE_ID:
5173 case CPP_NESTED_NAME_SPECIFIER:
5175 id_expression:
5176 cp_expr id_expression;
5177 cp_expr decl;
5178 const char *error_msg;
5179 bool template_p;
5180 bool done;
5181 cp_token *id_expr_token;
5183 /* Parse the id-expression. */
5184 id_expression
5185 = cp_parser_id_expression (parser,
5186 /*template_keyword_p=*/false,
5187 /*check_dependency_p=*/true,
5188 &template_p,
5189 /*declarator_p=*/false,
5190 /*optional_p=*/false);
5191 if (id_expression == error_mark_node)
5192 return error_mark_node;
5193 id_expr_token = token;
5194 token = cp_lexer_peek_token (parser->lexer);
5195 done = (token->type != CPP_OPEN_SQUARE
5196 && token->type != CPP_OPEN_PAREN
5197 && token->type != CPP_DOT
5198 && token->type != CPP_DEREF
5199 && token->type != CPP_PLUS_PLUS
5200 && token->type != CPP_MINUS_MINUS);
5201 /* If we have a template-id, then no further lookup is
5202 required. If the template-id was for a template-class, we
5203 will sometimes have a TYPE_DECL at this point. */
5204 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5205 || TREE_CODE (id_expression) == TYPE_DECL)
5206 decl = id_expression;
5207 /* Look up the name. */
5208 else
5210 tree ambiguous_decls;
5212 /* If we already know that this lookup is ambiguous, then
5213 we've already issued an error message; there's no reason
5214 to check again. */
5215 if (id_expr_token->type == CPP_NAME
5216 && id_expr_token->error_reported)
5218 cp_parser_simulate_error (parser);
5219 return error_mark_node;
5222 decl = cp_parser_lookup_name (parser, id_expression,
5223 none_type,
5224 template_p,
5225 /*is_namespace=*/false,
5226 /*check_dependency=*/true,
5227 &ambiguous_decls,
5228 id_expr_token->location);
5229 /* If the lookup was ambiguous, an error will already have
5230 been issued. */
5231 if (ambiguous_decls)
5232 return error_mark_node;
5234 /* In Objective-C++, we may have an Objective-C 2.0
5235 dot-syntax for classes here. */
5236 if (c_dialect_objc ()
5237 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5238 && TREE_CODE (decl) == TYPE_DECL
5239 && objc_is_class_name (decl))
5241 tree component;
5242 cp_lexer_consume_token (parser->lexer);
5243 component = cp_parser_identifier (parser);
5244 if (component == error_mark_node)
5245 return error_mark_node;
5247 tree result = objc_build_class_component_ref (id_expression,
5248 component);
5249 /* Build a location of the form:
5250 expr.component
5251 ~~~~~^~~~~~~~~
5252 with caret at the start of the component name (at
5253 input_location), ranging from the start of the id_expression
5254 to the end of the component name. */
5255 location_t combined_loc
5256 = make_location (input_location, id_expression.get_start (),
5257 get_finish (input_location));
5258 protected_set_expr_location (result, combined_loc);
5259 return result;
5262 /* In Objective-C++, an instance variable (ivar) may be preferred
5263 to whatever cp_parser_lookup_name() found.
5264 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5265 rest of c-family, we have to do a little extra work to preserve
5266 any location information in cp_expr "decl". Given that
5267 objc_lookup_ivar is implemented in "c-family" and "objc", we
5268 have a trip through the pure "tree" type, rather than cp_expr.
5269 Naively copying it back to "decl" would implicitly give the
5270 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5271 store an EXPR_LOCATION. Hence we only update "decl" (and
5272 hence its location_t) if we get back a different tree node. */
5273 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5274 id_expression);
5275 if (decl_tree != decl.get_value ())
5276 decl = cp_expr (decl_tree);
5278 /* If name lookup gives us a SCOPE_REF, then the
5279 qualifying scope was dependent. */
5280 if (TREE_CODE (decl) == SCOPE_REF)
5282 /* At this point, we do not know if DECL is a valid
5283 integral constant expression. We assume that it is
5284 in fact such an expression, so that code like:
5286 template <int N> struct A {
5287 int a[B<N>::i];
5290 is accepted. At template-instantiation time, we
5291 will check that B<N>::i is actually a constant. */
5292 return decl;
5294 /* Check to see if DECL is a local variable in a context
5295 where that is forbidden. */
5296 if (parser->local_variables_forbidden_p
5297 && local_variable_p (decl))
5299 /* It might be that we only found DECL because we are
5300 trying to be generous with pre-ISO scoping rules.
5301 For example, consider:
5303 int i;
5304 void g() {
5305 for (int i = 0; i < 10; ++i) {}
5306 extern void f(int j = i);
5309 Here, name look up will originally find the out
5310 of scope `i'. We need to issue a warning message,
5311 but then use the global `i'. */
5312 decl = check_for_out_of_scope_variable (decl);
5313 if (local_variable_p (decl))
5315 error_at (id_expr_token->location,
5316 "local variable %qD may not appear in this context",
5317 decl.get_value ());
5318 return error_mark_node;
5323 decl = (finish_id_expression
5324 (id_expression, decl, parser->scope,
5325 idk,
5326 parser->integral_constant_expression_p,
5327 parser->allow_non_integral_constant_expression_p,
5328 &parser->non_integral_constant_expression_p,
5329 template_p, done, address_p,
5330 template_arg_p,
5331 &error_msg,
5332 id_expression.get_location ()));
5333 if (error_msg)
5334 cp_parser_error (parser, error_msg);
5335 decl.set_location (id_expr_token->location);
5336 return decl;
5339 /* Anything else is an error. */
5340 default:
5341 cp_parser_error (parser, "expected primary-expression");
5342 return error_mark_node;
5346 static inline cp_expr
5347 cp_parser_primary_expression (cp_parser *parser,
5348 bool address_p,
5349 bool cast_p,
5350 bool template_arg_p,
5351 cp_id_kind *idk)
5353 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5354 /*decltype*/false, idk);
5357 /* Parse an id-expression.
5359 id-expression:
5360 unqualified-id
5361 qualified-id
5363 qualified-id:
5364 :: [opt] nested-name-specifier template [opt] unqualified-id
5365 :: identifier
5366 :: operator-function-id
5367 :: template-id
5369 Return a representation of the unqualified portion of the
5370 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5371 a `::' or nested-name-specifier.
5373 Often, if the id-expression was a qualified-id, the caller will
5374 want to make a SCOPE_REF to represent the qualified-id. This
5375 function does not do this in order to avoid wastefully creating
5376 SCOPE_REFs when they are not required.
5378 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5379 `template' keyword.
5381 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5382 uninstantiated templates.
5384 If *TEMPLATE_P is non-NULL, it is set to true iff the
5385 `template' keyword is used to explicitly indicate that the entity
5386 named is a template.
5388 If DECLARATOR_P is true, the id-expression is appearing as part of
5389 a declarator, rather than as part of an expression. */
5391 static cp_expr
5392 cp_parser_id_expression (cp_parser *parser,
5393 bool template_keyword_p,
5394 bool check_dependency_p,
5395 bool *template_p,
5396 bool declarator_p,
5397 bool optional_p)
5399 bool global_scope_p;
5400 bool nested_name_specifier_p;
5402 /* Assume the `template' keyword was not used. */
5403 if (template_p)
5404 *template_p = template_keyword_p;
5406 /* Look for the optional `::' operator. */
5407 global_scope_p
5408 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5409 != NULL_TREE);
5410 /* Look for the optional nested-name-specifier. */
5411 nested_name_specifier_p
5412 = (cp_parser_nested_name_specifier_opt (parser,
5413 /*typename_keyword_p=*/false,
5414 check_dependency_p,
5415 /*type_p=*/false,
5416 declarator_p)
5417 != NULL_TREE);
5418 /* If there is a nested-name-specifier, then we are looking at
5419 the first qualified-id production. */
5420 if (nested_name_specifier_p)
5422 tree saved_scope;
5423 tree saved_object_scope;
5424 tree saved_qualifying_scope;
5425 cp_expr unqualified_id;
5426 bool is_template;
5428 /* See if the next token is the `template' keyword. */
5429 if (!template_p)
5430 template_p = &is_template;
5431 *template_p = cp_parser_optional_template_keyword (parser);
5432 /* Name lookup we do during the processing of the
5433 unqualified-id might obliterate SCOPE. */
5434 saved_scope = parser->scope;
5435 saved_object_scope = parser->object_scope;
5436 saved_qualifying_scope = parser->qualifying_scope;
5437 /* Process the final unqualified-id. */
5438 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5439 check_dependency_p,
5440 declarator_p,
5441 /*optional_p=*/false);
5442 /* Restore the SAVED_SCOPE for our caller. */
5443 parser->scope = saved_scope;
5444 parser->object_scope = saved_object_scope;
5445 parser->qualifying_scope = saved_qualifying_scope;
5447 return unqualified_id;
5449 /* Otherwise, if we are in global scope, then we are looking at one
5450 of the other qualified-id productions. */
5451 else if (global_scope_p)
5453 cp_token *token;
5454 tree id;
5456 /* Peek at the next token. */
5457 token = cp_lexer_peek_token (parser->lexer);
5459 /* If it's an identifier, and the next token is not a "<", then
5460 we can avoid the template-id case. This is an optimization
5461 for this common case. */
5462 if (token->type == CPP_NAME
5463 && !cp_parser_nth_token_starts_template_argument_list_p
5464 (parser, 2))
5465 return cp_parser_identifier (parser);
5467 cp_parser_parse_tentatively (parser);
5468 /* Try a template-id. */
5469 id = cp_parser_template_id (parser,
5470 /*template_keyword_p=*/false,
5471 /*check_dependency_p=*/true,
5472 none_type,
5473 declarator_p);
5474 /* If that worked, we're done. */
5475 if (cp_parser_parse_definitely (parser))
5476 return id;
5478 /* Peek at the next token. (Changes in the token buffer may
5479 have invalidated the pointer obtained above.) */
5480 token = cp_lexer_peek_token (parser->lexer);
5482 switch (token->type)
5484 case CPP_NAME:
5485 return cp_parser_identifier (parser);
5487 case CPP_KEYWORD:
5488 if (token->keyword == RID_OPERATOR)
5489 return cp_parser_operator_function_id (parser);
5490 /* Fall through. */
5492 default:
5493 cp_parser_error (parser, "expected id-expression");
5494 return error_mark_node;
5497 else
5498 return cp_parser_unqualified_id (parser, template_keyword_p,
5499 /*check_dependency_p=*/true,
5500 declarator_p,
5501 optional_p);
5504 /* Parse an unqualified-id.
5506 unqualified-id:
5507 identifier
5508 operator-function-id
5509 conversion-function-id
5510 ~ class-name
5511 template-id
5513 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5514 keyword, in a construct like `A::template ...'.
5516 Returns a representation of unqualified-id. For the `identifier'
5517 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5518 production a BIT_NOT_EXPR is returned; the operand of the
5519 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5520 other productions, see the documentation accompanying the
5521 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5522 names are looked up in uninstantiated templates. If DECLARATOR_P
5523 is true, the unqualified-id is appearing as part of a declarator,
5524 rather than as part of an expression. */
5526 static cp_expr
5527 cp_parser_unqualified_id (cp_parser* parser,
5528 bool template_keyword_p,
5529 bool check_dependency_p,
5530 bool declarator_p,
5531 bool optional_p)
5533 cp_token *token;
5535 /* Peek at the next token. */
5536 token = cp_lexer_peek_token (parser->lexer);
5538 switch ((int) token->type)
5540 case CPP_NAME:
5542 tree id;
5544 /* We don't know yet whether or not this will be a
5545 template-id. */
5546 cp_parser_parse_tentatively (parser);
5547 /* Try a template-id. */
5548 id = cp_parser_template_id (parser, template_keyword_p,
5549 check_dependency_p,
5550 none_type,
5551 declarator_p);
5552 /* If it worked, we're done. */
5553 if (cp_parser_parse_definitely (parser))
5554 return id;
5555 /* Otherwise, it's an ordinary identifier. */
5556 return cp_parser_identifier (parser);
5559 case CPP_TEMPLATE_ID:
5560 return cp_parser_template_id (parser, template_keyword_p,
5561 check_dependency_p,
5562 none_type,
5563 declarator_p);
5565 case CPP_COMPL:
5567 tree type_decl;
5568 tree qualifying_scope;
5569 tree object_scope;
5570 tree scope;
5571 bool done;
5573 /* Consume the `~' token. */
5574 cp_lexer_consume_token (parser->lexer);
5575 /* Parse the class-name. The standard, as written, seems to
5576 say that:
5578 template <typename T> struct S { ~S (); };
5579 template <typename T> S<T>::~S() {}
5581 is invalid, since `~' must be followed by a class-name, but
5582 `S<T>' is dependent, and so not known to be a class.
5583 That's not right; we need to look in uninstantiated
5584 templates. A further complication arises from:
5586 template <typename T> void f(T t) {
5587 t.T::~T();
5590 Here, it is not possible to look up `T' in the scope of `T'
5591 itself. We must look in both the current scope, and the
5592 scope of the containing complete expression.
5594 Yet another issue is:
5596 struct S {
5597 int S;
5598 ~S();
5601 S::~S() {}
5603 The standard does not seem to say that the `S' in `~S'
5604 should refer to the type `S' and not the data member
5605 `S::S'. */
5607 /* DR 244 says that we look up the name after the "~" in the
5608 same scope as we looked up the qualifying name. That idea
5609 isn't fully worked out; it's more complicated than that. */
5610 scope = parser->scope;
5611 object_scope = parser->object_scope;
5612 qualifying_scope = parser->qualifying_scope;
5614 /* Check for invalid scopes. */
5615 if (scope == error_mark_node)
5617 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5618 cp_lexer_consume_token (parser->lexer);
5619 return error_mark_node;
5621 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5623 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5624 error_at (token->location,
5625 "scope %qT before %<~%> is not a class-name",
5626 scope);
5627 cp_parser_simulate_error (parser);
5628 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5629 cp_lexer_consume_token (parser->lexer);
5630 return error_mark_node;
5632 gcc_assert (!scope || TYPE_P (scope));
5634 /* If the name is of the form "X::~X" it's OK even if X is a
5635 typedef. */
5636 token = cp_lexer_peek_token (parser->lexer);
5637 if (scope
5638 && token->type == CPP_NAME
5639 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5640 != CPP_LESS)
5641 && (token->u.value == TYPE_IDENTIFIER (scope)
5642 || (CLASS_TYPE_P (scope)
5643 && constructor_name_p (token->u.value, scope))))
5645 cp_lexer_consume_token (parser->lexer);
5646 return build_nt (BIT_NOT_EXPR, scope);
5649 /* ~auto means the destructor of whatever the object is. */
5650 if (cp_parser_is_keyword (token, RID_AUTO))
5652 if (cxx_dialect < cxx14)
5653 pedwarn (input_location, 0,
5654 "%<~auto%> only available with "
5655 "-std=c++14 or -std=gnu++14");
5656 cp_lexer_consume_token (parser->lexer);
5657 return build_nt (BIT_NOT_EXPR, make_auto ());
5660 /* If there was an explicit qualification (S::~T), first look
5661 in the scope given by the qualification (i.e., S).
5663 Note: in the calls to cp_parser_class_name below we pass
5664 typename_type so that lookup finds the injected-class-name
5665 rather than the constructor. */
5666 done = false;
5667 type_decl = NULL_TREE;
5668 if (scope)
5670 cp_parser_parse_tentatively (parser);
5671 type_decl = cp_parser_class_name (parser,
5672 /*typename_keyword_p=*/false,
5673 /*template_keyword_p=*/false,
5674 typename_type,
5675 /*check_dependency=*/false,
5676 /*class_head_p=*/false,
5677 declarator_p);
5678 if (cp_parser_parse_definitely (parser))
5679 done = true;
5681 /* In "N::S::~S", look in "N" as well. */
5682 if (!done && scope && qualifying_scope)
5684 cp_parser_parse_tentatively (parser);
5685 parser->scope = qualifying_scope;
5686 parser->object_scope = NULL_TREE;
5687 parser->qualifying_scope = NULL_TREE;
5688 type_decl
5689 = cp_parser_class_name (parser,
5690 /*typename_keyword_p=*/false,
5691 /*template_keyword_p=*/false,
5692 typename_type,
5693 /*check_dependency=*/false,
5694 /*class_head_p=*/false,
5695 declarator_p);
5696 if (cp_parser_parse_definitely (parser))
5697 done = true;
5699 /* In "p->S::~T", look in the scope given by "*p" as well. */
5700 else if (!done && object_scope)
5702 cp_parser_parse_tentatively (parser);
5703 parser->scope = object_scope;
5704 parser->object_scope = NULL_TREE;
5705 parser->qualifying_scope = NULL_TREE;
5706 type_decl
5707 = cp_parser_class_name (parser,
5708 /*typename_keyword_p=*/false,
5709 /*template_keyword_p=*/false,
5710 typename_type,
5711 /*check_dependency=*/false,
5712 /*class_head_p=*/false,
5713 declarator_p);
5714 if (cp_parser_parse_definitely (parser))
5715 done = true;
5717 /* Look in the surrounding context. */
5718 if (!done)
5720 parser->scope = NULL_TREE;
5721 parser->object_scope = NULL_TREE;
5722 parser->qualifying_scope = NULL_TREE;
5723 if (processing_template_decl)
5724 cp_parser_parse_tentatively (parser);
5725 type_decl
5726 = cp_parser_class_name (parser,
5727 /*typename_keyword_p=*/false,
5728 /*template_keyword_p=*/false,
5729 typename_type,
5730 /*check_dependency=*/false,
5731 /*class_head_p=*/false,
5732 declarator_p);
5733 if (processing_template_decl
5734 && ! cp_parser_parse_definitely (parser))
5736 /* We couldn't find a type with this name. If we're parsing
5737 tentatively, fail and try something else. */
5738 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5740 cp_parser_simulate_error (parser);
5741 return error_mark_node;
5743 /* Otherwise, accept it and check for a match at instantiation
5744 time. */
5745 type_decl = cp_parser_identifier (parser);
5746 if (type_decl != error_mark_node)
5747 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5748 return type_decl;
5751 /* If an error occurred, assume that the name of the
5752 destructor is the same as the name of the qualifying
5753 class. That allows us to keep parsing after running
5754 into ill-formed destructor names. */
5755 if (type_decl == error_mark_node && scope)
5756 return build_nt (BIT_NOT_EXPR, scope);
5757 else if (type_decl == error_mark_node)
5758 return error_mark_node;
5760 /* Check that destructor name and scope match. */
5761 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5763 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5764 error_at (token->location,
5765 "declaration of %<~%T%> as member of %qT",
5766 type_decl, scope);
5767 cp_parser_simulate_error (parser);
5768 return error_mark_node;
5771 /* [class.dtor]
5773 A typedef-name that names a class shall not be used as the
5774 identifier in the declarator for a destructor declaration. */
5775 if (declarator_p
5776 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5777 && !DECL_SELF_REFERENCE_P (type_decl)
5778 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5779 error_at (token->location,
5780 "typedef-name %qD used as destructor declarator",
5781 type_decl);
5783 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5786 case CPP_KEYWORD:
5787 if (token->keyword == RID_OPERATOR)
5789 cp_expr id;
5791 /* This could be a template-id, so we try that first. */
5792 cp_parser_parse_tentatively (parser);
5793 /* Try a template-id. */
5794 id = cp_parser_template_id (parser, template_keyword_p,
5795 /*check_dependency_p=*/true,
5796 none_type,
5797 declarator_p);
5798 /* If that worked, we're done. */
5799 if (cp_parser_parse_definitely (parser))
5800 return id;
5801 /* We still don't know whether we're looking at an
5802 operator-function-id or a conversion-function-id. */
5803 cp_parser_parse_tentatively (parser);
5804 /* Try an operator-function-id. */
5805 id = cp_parser_operator_function_id (parser);
5806 /* If that didn't work, try a conversion-function-id. */
5807 if (!cp_parser_parse_definitely (parser))
5808 id = cp_parser_conversion_function_id (parser);
5809 else if (UDLIT_OPER_P (id))
5811 /* 17.6.3.3.5 */
5812 const char *name = UDLIT_OP_SUFFIX (id);
5813 if (name[0] != '_' && !in_system_header_at (input_location)
5814 && declarator_p)
5815 warning (0, "literal operator suffixes not preceded by %<_%>"
5816 " are reserved for future standardization");
5819 return id;
5821 /* Fall through. */
5823 default:
5824 if (optional_p)
5825 return NULL_TREE;
5826 cp_parser_error (parser, "expected unqualified-id");
5827 return error_mark_node;
5831 /* Parse an (optional) nested-name-specifier.
5833 nested-name-specifier: [C++98]
5834 class-or-namespace-name :: nested-name-specifier [opt]
5835 class-or-namespace-name :: template nested-name-specifier [opt]
5837 nested-name-specifier: [C++0x]
5838 type-name ::
5839 namespace-name ::
5840 nested-name-specifier identifier ::
5841 nested-name-specifier template [opt] simple-template-id ::
5843 PARSER->SCOPE should be set appropriately before this function is
5844 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5845 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5846 in name lookups.
5848 Sets PARSER->SCOPE to the class (TYPE) or namespace
5849 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5850 it unchanged if there is no nested-name-specifier. Returns the new
5851 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5853 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5854 part of a declaration and/or decl-specifier. */
5856 static tree
5857 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5858 bool typename_keyword_p,
5859 bool check_dependency_p,
5860 bool type_p,
5861 bool is_declaration)
5863 bool success = false;
5864 cp_token_position start = 0;
5865 cp_token *token;
5867 /* Remember where the nested-name-specifier starts. */
5868 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5870 start = cp_lexer_token_position (parser->lexer, false);
5871 push_deferring_access_checks (dk_deferred);
5874 while (true)
5876 tree new_scope;
5877 tree old_scope;
5878 tree saved_qualifying_scope;
5879 bool template_keyword_p;
5881 /* Spot cases that cannot be the beginning of a
5882 nested-name-specifier. */
5883 token = cp_lexer_peek_token (parser->lexer);
5885 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5886 the already parsed nested-name-specifier. */
5887 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5889 /* Grab the nested-name-specifier and continue the loop. */
5890 cp_parser_pre_parsed_nested_name_specifier (parser);
5891 /* If we originally encountered this nested-name-specifier
5892 with IS_DECLARATION set to false, we will not have
5893 resolved TYPENAME_TYPEs, so we must do so here. */
5894 if (is_declaration
5895 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5897 new_scope = resolve_typename_type (parser->scope,
5898 /*only_current_p=*/false);
5899 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5900 parser->scope = new_scope;
5902 success = true;
5903 continue;
5906 /* Spot cases that cannot be the beginning of a
5907 nested-name-specifier. On the second and subsequent times
5908 through the loop, we look for the `template' keyword. */
5909 if (success && token->keyword == RID_TEMPLATE)
5911 /* A template-id can start a nested-name-specifier. */
5912 else if (token->type == CPP_TEMPLATE_ID)
5914 /* DR 743: decltype can be used in a nested-name-specifier. */
5915 else if (token_is_decltype (token))
5917 else
5919 /* If the next token is not an identifier, then it is
5920 definitely not a type-name or namespace-name. */
5921 if (token->type != CPP_NAME)
5922 break;
5923 /* If the following token is neither a `<' (to begin a
5924 template-id), nor a `::', then we are not looking at a
5925 nested-name-specifier. */
5926 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5928 if (token->type == CPP_COLON
5929 && parser->colon_corrects_to_scope_p
5930 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5932 error_at (token->location,
5933 "found %<:%> in nested-name-specifier, expected %<::%>");
5934 token->type = CPP_SCOPE;
5937 if (token->type != CPP_SCOPE
5938 && !cp_parser_nth_token_starts_template_argument_list_p
5939 (parser, 2))
5940 break;
5943 /* The nested-name-specifier is optional, so we parse
5944 tentatively. */
5945 cp_parser_parse_tentatively (parser);
5947 /* Look for the optional `template' keyword, if this isn't the
5948 first time through the loop. */
5949 if (success)
5950 template_keyword_p = cp_parser_optional_template_keyword (parser);
5951 else
5952 template_keyword_p = false;
5954 /* Save the old scope since the name lookup we are about to do
5955 might destroy it. */
5956 old_scope = parser->scope;
5957 saved_qualifying_scope = parser->qualifying_scope;
5958 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5959 look up names in "X<T>::I" in order to determine that "Y" is
5960 a template. So, if we have a typename at this point, we make
5961 an effort to look through it. */
5962 if (is_declaration
5963 && !typename_keyword_p
5964 && parser->scope
5965 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5966 parser->scope = resolve_typename_type (parser->scope,
5967 /*only_current_p=*/false);
5968 /* Parse the qualifying entity. */
5969 new_scope
5970 = cp_parser_qualifying_entity (parser,
5971 typename_keyword_p,
5972 template_keyword_p,
5973 check_dependency_p,
5974 type_p,
5975 is_declaration);
5976 /* Look for the `::' token. */
5977 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5979 /* If we found what we wanted, we keep going; otherwise, we're
5980 done. */
5981 if (!cp_parser_parse_definitely (parser))
5983 bool error_p = false;
5985 /* Restore the OLD_SCOPE since it was valid before the
5986 failed attempt at finding the last
5987 class-or-namespace-name. */
5988 parser->scope = old_scope;
5989 parser->qualifying_scope = saved_qualifying_scope;
5991 /* If the next token is a decltype, and the one after that is a
5992 `::', then the decltype has failed to resolve to a class or
5993 enumeration type. Give this error even when parsing
5994 tentatively since it can't possibly be valid--and we're going
5995 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5996 won't get another chance.*/
5997 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5998 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5999 == CPP_SCOPE))
6001 token = cp_lexer_consume_token (parser->lexer);
6002 error_at (token->location, "decltype evaluates to %qT, "
6003 "which is not a class or enumeration type",
6004 token->u.tree_check_value->value);
6005 parser->scope = error_mark_node;
6006 error_p = true;
6007 /* As below. */
6008 success = true;
6009 cp_lexer_consume_token (parser->lexer);
6012 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6013 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6015 /* If we have a non-type template-id followed by ::, it can't
6016 possibly be valid. */
6017 token = cp_lexer_peek_token (parser->lexer);
6018 tree tid = token->u.tree_check_value->value;
6019 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6020 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6022 tree tmpl = NULL_TREE;
6023 if (is_overloaded_fn (tid))
6025 tree fns = get_fns (tid);
6026 if (!OVL_CHAIN (fns))
6027 tmpl = OVL_CURRENT (fns);
6028 error_at (token->location, "function template-id %qD "
6029 "in nested-name-specifier", tid);
6031 else
6033 /* Variable template. */
6034 tmpl = TREE_OPERAND (tid, 0);
6035 gcc_assert (variable_template_p (tmpl));
6036 error_at (token->location, "variable template-id %qD "
6037 "in nested-name-specifier", tid);
6039 if (tmpl)
6040 inform (DECL_SOURCE_LOCATION (tmpl),
6041 "%qD declared here", tmpl);
6043 parser->scope = error_mark_node;
6044 error_p = true;
6045 /* As below. */
6046 success = true;
6047 cp_lexer_consume_token (parser->lexer);
6048 cp_lexer_consume_token (parser->lexer);
6052 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6053 break;
6054 /* If the next token is an identifier, and the one after
6055 that is a `::', then any valid interpretation would have
6056 found a class-or-namespace-name. */
6057 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6058 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6059 == CPP_SCOPE)
6060 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6061 != CPP_COMPL))
6063 token = cp_lexer_consume_token (parser->lexer);
6064 if (!error_p)
6066 if (!token->error_reported)
6068 tree decl;
6069 tree ambiguous_decls;
6071 decl = cp_parser_lookup_name (parser, token->u.value,
6072 none_type,
6073 /*is_template=*/false,
6074 /*is_namespace=*/false,
6075 /*check_dependency=*/true,
6076 &ambiguous_decls,
6077 token->location);
6078 if (TREE_CODE (decl) == TEMPLATE_DECL)
6079 error_at (token->location,
6080 "%qD used without template parameters",
6081 decl);
6082 else if (ambiguous_decls)
6084 // cp_parser_lookup_name has the same diagnostic,
6085 // thus make sure to emit it at most once.
6086 if (cp_parser_uncommitted_to_tentative_parse_p
6087 (parser))
6089 error_at (token->location,
6090 "reference to %qD is ambiguous",
6091 token->u.value);
6092 print_candidates (ambiguous_decls);
6094 decl = error_mark_node;
6096 else
6098 if (cxx_dialect != cxx98)
6099 cp_parser_name_lookup_error
6100 (parser, token->u.value, decl, NLE_NOT_CXX98,
6101 token->location);
6102 else
6103 cp_parser_name_lookup_error
6104 (parser, token->u.value, decl, NLE_CXX98,
6105 token->location);
6108 parser->scope = error_mark_node;
6109 error_p = true;
6110 /* Treat this as a successful nested-name-specifier
6111 due to:
6113 [basic.lookup.qual]
6115 If the name found is not a class-name (clause
6116 _class_) or namespace-name (_namespace.def_), the
6117 program is ill-formed. */
6118 success = true;
6120 cp_lexer_consume_token (parser->lexer);
6122 break;
6124 /* We've found one valid nested-name-specifier. */
6125 success = true;
6126 /* Name lookup always gives us a DECL. */
6127 if (TREE_CODE (new_scope) == TYPE_DECL)
6128 new_scope = TREE_TYPE (new_scope);
6129 /* Uses of "template" must be followed by actual templates. */
6130 if (template_keyword_p
6131 && !(CLASS_TYPE_P (new_scope)
6132 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6133 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6134 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6135 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6136 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6137 == TEMPLATE_ID_EXPR)))
6138 permerror (input_location, TYPE_P (new_scope)
6139 ? G_("%qT is not a template")
6140 : G_("%qD is not a template"),
6141 new_scope);
6142 /* If it is a class scope, try to complete it; we are about to
6143 be looking up names inside the class. */
6144 if (TYPE_P (new_scope)
6145 /* Since checking types for dependency can be expensive,
6146 avoid doing it if the type is already complete. */
6147 && !COMPLETE_TYPE_P (new_scope)
6148 /* Do not try to complete dependent types. */
6149 && !dependent_type_p (new_scope))
6151 new_scope = complete_type (new_scope);
6152 /* If it is a typedef to current class, use the current
6153 class instead, as the typedef won't have any names inside
6154 it yet. */
6155 if (!COMPLETE_TYPE_P (new_scope)
6156 && currently_open_class (new_scope))
6157 new_scope = TYPE_MAIN_VARIANT (new_scope);
6159 /* Make sure we look in the right scope the next time through
6160 the loop. */
6161 parser->scope = new_scope;
6164 /* If parsing tentatively, replace the sequence of tokens that makes
6165 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6166 token. That way, should we re-parse the token stream, we will
6167 not have to repeat the effort required to do the parse, nor will
6168 we issue duplicate error messages. */
6169 if (success && start)
6171 cp_token *token;
6173 token = cp_lexer_token_at (parser->lexer, start);
6174 /* Reset the contents of the START token. */
6175 token->type = CPP_NESTED_NAME_SPECIFIER;
6176 /* Retrieve any deferred checks. Do not pop this access checks yet
6177 so the memory will not be reclaimed during token replacing below. */
6178 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6179 token->u.tree_check_value->value = parser->scope;
6180 token->u.tree_check_value->checks = get_deferred_access_checks ();
6181 token->u.tree_check_value->qualifying_scope =
6182 parser->qualifying_scope;
6183 token->keyword = RID_MAX;
6185 /* Purge all subsequent tokens. */
6186 cp_lexer_purge_tokens_after (parser->lexer, start);
6189 if (start)
6190 pop_to_parent_deferring_access_checks ();
6192 return success ? parser->scope : NULL_TREE;
6195 /* Parse a nested-name-specifier. See
6196 cp_parser_nested_name_specifier_opt for details. This function
6197 behaves identically, except that it will an issue an error if no
6198 nested-name-specifier is present. */
6200 static tree
6201 cp_parser_nested_name_specifier (cp_parser *parser,
6202 bool typename_keyword_p,
6203 bool check_dependency_p,
6204 bool type_p,
6205 bool is_declaration)
6207 tree scope;
6209 /* Look for the nested-name-specifier. */
6210 scope = cp_parser_nested_name_specifier_opt (parser,
6211 typename_keyword_p,
6212 check_dependency_p,
6213 type_p,
6214 is_declaration);
6215 /* If it was not present, issue an error message. */
6216 if (!scope)
6218 cp_parser_error (parser, "expected nested-name-specifier");
6219 parser->scope = NULL_TREE;
6222 return scope;
6225 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6226 this is either a class-name or a namespace-name (which corresponds
6227 to the class-or-namespace-name production in the grammar). For
6228 C++0x, it can also be a type-name that refers to an enumeration
6229 type or a simple-template-id.
6231 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6232 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6233 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6234 TYPE_P is TRUE iff the next name should be taken as a class-name,
6235 even the same name is declared to be another entity in the same
6236 scope.
6238 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6239 specified by the class-or-namespace-name. If neither is found the
6240 ERROR_MARK_NODE is returned. */
6242 static tree
6243 cp_parser_qualifying_entity (cp_parser *parser,
6244 bool typename_keyword_p,
6245 bool template_keyword_p,
6246 bool check_dependency_p,
6247 bool type_p,
6248 bool is_declaration)
6250 tree saved_scope;
6251 tree saved_qualifying_scope;
6252 tree saved_object_scope;
6253 tree scope;
6254 bool only_class_p;
6255 bool successful_parse_p;
6257 /* DR 743: decltype can appear in a nested-name-specifier. */
6258 if (cp_lexer_next_token_is_decltype (parser->lexer))
6260 scope = cp_parser_decltype (parser);
6261 if (TREE_CODE (scope) != ENUMERAL_TYPE
6262 && !MAYBE_CLASS_TYPE_P (scope))
6264 cp_parser_simulate_error (parser);
6265 return error_mark_node;
6267 if (TYPE_NAME (scope))
6268 scope = TYPE_NAME (scope);
6269 return scope;
6272 /* Before we try to parse the class-name, we must save away the
6273 current PARSER->SCOPE since cp_parser_class_name will destroy
6274 it. */
6275 saved_scope = parser->scope;
6276 saved_qualifying_scope = parser->qualifying_scope;
6277 saved_object_scope = parser->object_scope;
6278 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6279 there is no need to look for a namespace-name. */
6280 only_class_p = template_keyword_p
6281 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6282 if (!only_class_p)
6283 cp_parser_parse_tentatively (parser);
6284 scope = cp_parser_class_name (parser,
6285 typename_keyword_p,
6286 template_keyword_p,
6287 type_p ? class_type : none_type,
6288 check_dependency_p,
6289 /*class_head_p=*/false,
6290 is_declaration,
6291 /*enum_ok=*/cxx_dialect > cxx98);
6292 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6293 /* If that didn't work, try for a namespace-name. */
6294 if (!only_class_p && !successful_parse_p)
6296 /* Restore the saved scope. */
6297 parser->scope = saved_scope;
6298 parser->qualifying_scope = saved_qualifying_scope;
6299 parser->object_scope = saved_object_scope;
6300 /* If we are not looking at an identifier followed by the scope
6301 resolution operator, then this is not part of a
6302 nested-name-specifier. (Note that this function is only used
6303 to parse the components of a nested-name-specifier.) */
6304 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6305 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6306 return error_mark_node;
6307 scope = cp_parser_namespace_name (parser);
6310 return scope;
6313 /* Return true if we are looking at a compound-literal, false otherwise. */
6315 static bool
6316 cp_parser_compound_literal_p (cp_parser *parser)
6318 /* Consume the `('. */
6319 cp_lexer_consume_token (parser->lexer);
6321 cp_lexer_save_tokens (parser->lexer);
6323 /* Skip tokens until the next token is a closing parenthesis.
6324 If we find the closing `)', and the next token is a `{', then
6325 we are looking at a compound-literal. */
6326 bool compound_literal_p
6327 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6328 /*consume_paren=*/true)
6329 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6331 /* Roll back the tokens we skipped. */
6332 cp_lexer_rollback_tokens (parser->lexer);
6334 return compound_literal_p;
6337 /* Parse a postfix-expression.
6339 postfix-expression:
6340 primary-expression
6341 postfix-expression [ expression ]
6342 postfix-expression ( expression-list [opt] )
6343 simple-type-specifier ( expression-list [opt] )
6344 typename :: [opt] nested-name-specifier identifier
6345 ( expression-list [opt] )
6346 typename :: [opt] nested-name-specifier template [opt] template-id
6347 ( expression-list [opt] )
6348 postfix-expression . template [opt] id-expression
6349 postfix-expression -> template [opt] id-expression
6350 postfix-expression . pseudo-destructor-name
6351 postfix-expression -> pseudo-destructor-name
6352 postfix-expression ++
6353 postfix-expression --
6354 dynamic_cast < type-id > ( expression )
6355 static_cast < type-id > ( expression )
6356 reinterpret_cast < type-id > ( expression )
6357 const_cast < type-id > ( expression )
6358 typeid ( expression )
6359 typeid ( type-id )
6361 GNU Extension:
6363 postfix-expression:
6364 ( type-id ) { initializer-list , [opt] }
6366 This extension is a GNU version of the C99 compound-literal
6367 construct. (The C99 grammar uses `type-name' instead of `type-id',
6368 but they are essentially the same concept.)
6370 If ADDRESS_P is true, the postfix expression is the operand of the
6371 `&' operator. CAST_P is true if this expression is the target of a
6372 cast.
6374 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6375 class member access expressions [expr.ref].
6377 Returns a representation of the expression. */
6379 static cp_expr
6380 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6381 bool member_access_only_p, bool decltype_p,
6382 cp_id_kind * pidk_return)
6384 cp_token *token;
6385 location_t loc;
6386 enum rid keyword;
6387 cp_id_kind idk = CP_ID_KIND_NONE;
6388 cp_expr postfix_expression = NULL_TREE;
6389 bool is_member_access = false;
6390 int saved_in_statement = -1;
6392 /* Peek at the next token. */
6393 token = cp_lexer_peek_token (parser->lexer);
6394 loc = token->location;
6395 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6397 /* Some of the productions are determined by keywords. */
6398 keyword = token->keyword;
6399 switch (keyword)
6401 case RID_DYNCAST:
6402 case RID_STATCAST:
6403 case RID_REINTCAST:
6404 case RID_CONSTCAST:
6406 tree type;
6407 cp_expr expression;
6408 const char *saved_message;
6409 bool saved_in_type_id_in_expr_p;
6411 /* All of these can be handled in the same way from the point
6412 of view of parsing. Begin by consuming the token
6413 identifying the cast. */
6414 cp_lexer_consume_token (parser->lexer);
6416 /* New types cannot be defined in the cast. */
6417 saved_message = parser->type_definition_forbidden_message;
6418 parser->type_definition_forbidden_message
6419 = G_("types may not be defined in casts");
6421 /* Look for the opening `<'. */
6422 cp_parser_require (parser, CPP_LESS, RT_LESS);
6423 /* Parse the type to which we are casting. */
6424 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6425 parser->in_type_id_in_expr_p = true;
6426 type = cp_parser_type_id (parser);
6427 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6428 /* Look for the closing `>'. */
6429 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6430 /* Restore the old message. */
6431 parser->type_definition_forbidden_message = saved_message;
6433 bool saved_greater_than_is_operator_p
6434 = parser->greater_than_is_operator_p;
6435 parser->greater_than_is_operator_p = true;
6437 /* And the expression which is being cast. */
6438 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6439 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6440 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6441 RT_CLOSE_PAREN);
6442 location_t end_loc = close_paren ?
6443 close_paren->location : UNKNOWN_LOCATION;
6445 parser->greater_than_is_operator_p
6446 = saved_greater_than_is_operator_p;
6448 /* Only type conversions to integral or enumeration types
6449 can be used in constant-expressions. */
6450 if (!cast_valid_in_integral_constant_expression_p (type)
6451 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6453 postfix_expression = error_mark_node;
6454 break;
6457 switch (keyword)
6459 case RID_DYNCAST:
6460 postfix_expression
6461 = build_dynamic_cast (type, expression, tf_warning_or_error);
6462 break;
6463 case RID_STATCAST:
6464 postfix_expression
6465 = build_static_cast (type, expression, tf_warning_or_error);
6466 break;
6467 case RID_REINTCAST:
6468 postfix_expression
6469 = build_reinterpret_cast (type, expression,
6470 tf_warning_or_error);
6471 break;
6472 case RID_CONSTCAST:
6473 postfix_expression
6474 = build_const_cast (type, expression, tf_warning_or_error);
6475 break;
6476 default:
6477 gcc_unreachable ();
6480 /* Construct a location e.g. :
6481 reinterpret_cast <int *> (expr)
6482 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6483 ranging from the start of the "*_cast" token to the final closing
6484 paren, with the caret at the start. */
6485 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6486 postfix_expression.set_location (cp_cast_loc);
6488 break;
6490 case RID_TYPEID:
6492 tree type;
6493 const char *saved_message;
6494 bool saved_in_type_id_in_expr_p;
6496 /* Consume the `typeid' token. */
6497 cp_lexer_consume_token (parser->lexer);
6498 /* Look for the `(' token. */
6499 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6500 /* Types cannot be defined in a `typeid' expression. */
6501 saved_message = parser->type_definition_forbidden_message;
6502 parser->type_definition_forbidden_message
6503 = G_("types may not be defined in a %<typeid%> expression");
6504 /* We can't be sure yet whether we're looking at a type-id or an
6505 expression. */
6506 cp_parser_parse_tentatively (parser);
6507 /* Try a type-id first. */
6508 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6509 parser->in_type_id_in_expr_p = true;
6510 type = cp_parser_type_id (parser);
6511 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6512 /* Look for the `)' token. Otherwise, we can't be sure that
6513 we're not looking at an expression: consider `typeid (int
6514 (3))', for example. */
6515 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6516 /* If all went well, simply lookup the type-id. */
6517 if (cp_parser_parse_definitely (parser))
6518 postfix_expression = get_typeid (type, tf_warning_or_error);
6519 /* Otherwise, fall back to the expression variant. */
6520 else
6522 tree expression;
6524 /* Look for an expression. */
6525 expression = cp_parser_expression (parser, & idk);
6526 /* Compute its typeid. */
6527 postfix_expression = build_typeid (expression, tf_warning_or_error);
6528 /* Look for the `)' token. */
6529 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6531 /* Restore the saved message. */
6532 parser->type_definition_forbidden_message = saved_message;
6533 /* `typeid' may not appear in an integral constant expression. */
6534 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6535 postfix_expression = error_mark_node;
6537 break;
6539 case RID_TYPENAME:
6541 tree type;
6542 /* The syntax permitted here is the same permitted for an
6543 elaborated-type-specifier. */
6544 ++parser->prevent_constrained_type_specifiers;
6545 type = cp_parser_elaborated_type_specifier (parser,
6546 /*is_friend=*/false,
6547 /*is_declaration=*/false);
6548 --parser->prevent_constrained_type_specifiers;
6549 postfix_expression = cp_parser_functional_cast (parser, type);
6551 break;
6553 case RID_CILK_SPAWN:
6555 location_t cilk_spawn_loc
6556 = cp_lexer_peek_token (parser->lexer)->location;
6557 cp_lexer_consume_token (parser->lexer);
6558 token = cp_lexer_peek_token (parser->lexer);
6559 if (token->type == CPP_SEMICOLON)
6561 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6562 "an expression");
6563 postfix_expression = error_mark_node;
6564 break;
6566 else if (!current_function_decl)
6568 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6569 "inside a function");
6570 postfix_expression = error_mark_node;
6571 break;
6573 else
6575 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6576 saved_in_statement = parser->in_statement;
6577 parser->in_statement |= IN_CILK_SPAWN;
6579 cfun->calls_cilk_spawn = 1;
6580 postfix_expression =
6581 cp_parser_postfix_expression (parser, false, false,
6582 false, false, &idk);
6583 if (!flag_cilkplus)
6585 error_at (token->location, "-fcilkplus must be enabled to use"
6586 " %<_Cilk_spawn%>");
6587 cfun->calls_cilk_spawn = 0;
6589 else if (saved_in_statement & IN_CILK_SPAWN)
6591 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6592 "are not permitted");
6593 postfix_expression = error_mark_node;
6594 cfun->calls_cilk_spawn = 0;
6596 else
6598 location_t loc = postfix_expression.get_location ();
6599 postfix_expression = build_cilk_spawn (token->location,
6600 postfix_expression);
6601 /* Build a location of the form:
6602 _Cilk_spawn expr
6603 ~~~~~~~~~~~~^~~~
6604 with caret at the expr, ranging from the start of the
6605 _Cilk_spawn token to the end of the expression. */
6606 location_t combined_loc =
6607 make_location (loc, cilk_spawn_loc, get_finish (loc));
6608 postfix_expression.set_location (combined_loc);
6609 if (postfix_expression != error_mark_node)
6610 SET_EXPR_LOCATION (postfix_expression, input_location);
6611 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6613 break;
6616 case RID_ADDRESSOF:
6617 case RID_BUILTIN_SHUFFLE:
6618 case RID_BUILTIN_LAUNDER:
6620 vec<tree, va_gc> *vec;
6621 unsigned int i;
6622 tree p;
6624 cp_lexer_consume_token (parser->lexer);
6625 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6626 /*cast_p=*/false, /*allow_expansion_p=*/true,
6627 /*non_constant_p=*/NULL);
6628 if (vec == NULL)
6630 postfix_expression = error_mark_node;
6631 break;
6634 FOR_EACH_VEC_ELT (*vec, i, p)
6635 mark_exp_read (p);
6637 switch (keyword)
6639 case RID_ADDRESSOF:
6640 if (vec->length () == 1)
6641 postfix_expression
6642 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6643 else
6645 error_at (loc, "wrong number of arguments to "
6646 "%<__builtin_addressof%>");
6647 postfix_expression = error_mark_node;
6649 break;
6651 case RID_BUILTIN_LAUNDER:
6652 if (vec->length () == 1)
6653 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6654 tf_warning_or_error);
6655 else
6657 error_at (loc, "wrong number of arguments to "
6658 "%<__builtin_launder%>");
6659 postfix_expression = error_mark_node;
6661 break;
6663 case RID_BUILTIN_SHUFFLE:
6664 if (vec->length () == 2)
6665 postfix_expression
6666 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6667 (*vec)[1], tf_warning_or_error);
6668 else if (vec->length () == 3)
6669 postfix_expression
6670 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6671 (*vec)[2], tf_warning_or_error);
6672 else
6674 error_at (loc, "wrong number of arguments to "
6675 "%<__builtin_shuffle%>");
6676 postfix_expression = error_mark_node;
6678 break;
6680 default:
6681 gcc_unreachable ();
6683 break;
6686 default:
6688 tree type;
6690 /* If the next thing is a simple-type-specifier, we may be
6691 looking at a functional cast. We could also be looking at
6692 an id-expression. So, we try the functional cast, and if
6693 that doesn't work we fall back to the primary-expression. */
6694 cp_parser_parse_tentatively (parser);
6695 /* Look for the simple-type-specifier. */
6696 ++parser->prevent_constrained_type_specifiers;
6697 type = cp_parser_simple_type_specifier (parser,
6698 /*decl_specs=*/NULL,
6699 CP_PARSER_FLAGS_NONE);
6700 --parser->prevent_constrained_type_specifiers;
6701 /* Parse the cast itself. */
6702 if (!cp_parser_error_occurred (parser))
6703 postfix_expression
6704 = cp_parser_functional_cast (parser, type);
6705 /* If that worked, we're done. */
6706 if (cp_parser_parse_definitely (parser))
6707 break;
6709 /* If the functional-cast didn't work out, try a
6710 compound-literal. */
6711 if (cp_parser_allow_gnu_extensions_p (parser)
6712 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6714 cp_expr initializer = NULL_TREE;
6716 cp_parser_parse_tentatively (parser);
6718 /* Avoid calling cp_parser_type_id pointlessly, see comment
6719 in cp_parser_cast_expression about c++/29234. */
6720 if (!cp_parser_compound_literal_p (parser))
6721 cp_parser_simulate_error (parser);
6722 else
6724 /* Parse the type. */
6725 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6726 parser->in_type_id_in_expr_p = true;
6727 type = cp_parser_type_id (parser);
6728 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6729 /* Look for the `)'. */
6730 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6733 /* If things aren't going well, there's no need to
6734 keep going. */
6735 if (!cp_parser_error_occurred (parser))
6737 bool non_constant_p;
6738 /* Parse the brace-enclosed initializer list. */
6739 initializer = cp_parser_braced_list (parser,
6740 &non_constant_p);
6742 /* If that worked, we're definitely looking at a
6743 compound-literal expression. */
6744 if (cp_parser_parse_definitely (parser))
6746 /* Warn the user that a compound literal is not
6747 allowed in standard C++. */
6748 pedwarn (input_location, OPT_Wpedantic,
6749 "ISO C++ forbids compound-literals");
6750 /* For simplicity, we disallow compound literals in
6751 constant-expressions. We could
6752 allow compound literals of integer type, whose
6753 initializer was a constant, in constant
6754 expressions. Permitting that usage, as a further
6755 extension, would not change the meaning of any
6756 currently accepted programs. (Of course, as
6757 compound literals are not part of ISO C++, the
6758 standard has nothing to say.) */
6759 if (cp_parser_non_integral_constant_expression (parser,
6760 NIC_NCC))
6762 postfix_expression = error_mark_node;
6763 break;
6765 /* Form the representation of the compound-literal. */
6766 postfix_expression
6767 = finish_compound_literal (type, initializer,
6768 tf_warning_or_error);
6769 postfix_expression.set_location (initializer.get_location ());
6770 break;
6774 /* It must be a primary-expression. */
6775 postfix_expression
6776 = cp_parser_primary_expression (parser, address_p, cast_p,
6777 /*template_arg_p=*/false,
6778 decltype_p,
6779 &idk);
6781 break;
6784 /* Note that we don't need to worry about calling build_cplus_new on a
6785 class-valued CALL_EXPR in decltype when it isn't the end of the
6786 postfix-expression; unary_complex_lvalue will take care of that for
6787 all these cases. */
6789 /* Keep looping until the postfix-expression is complete. */
6790 while (true)
6792 if (idk == CP_ID_KIND_UNQUALIFIED
6793 && identifier_p (postfix_expression)
6794 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6795 /* It is not a Koenig lookup function call. */
6796 postfix_expression
6797 = unqualified_name_lookup_error (postfix_expression);
6799 /* Peek at the next token. */
6800 token = cp_lexer_peek_token (parser->lexer);
6802 switch (token->type)
6804 case CPP_OPEN_SQUARE:
6805 if (cp_next_tokens_can_be_std_attribute_p (parser))
6807 cp_parser_error (parser,
6808 "two consecutive %<[%> shall "
6809 "only introduce an attribute");
6810 return error_mark_node;
6812 postfix_expression
6813 = cp_parser_postfix_open_square_expression (parser,
6814 postfix_expression,
6815 false,
6816 decltype_p);
6817 postfix_expression.set_range (start_loc,
6818 postfix_expression.get_location ());
6820 idk = CP_ID_KIND_NONE;
6821 is_member_access = false;
6822 break;
6824 case CPP_OPEN_PAREN:
6825 /* postfix-expression ( expression-list [opt] ) */
6827 bool koenig_p;
6828 bool is_builtin_constant_p;
6829 bool saved_integral_constant_expression_p = false;
6830 bool saved_non_integral_constant_expression_p = false;
6831 tsubst_flags_t complain = complain_flags (decltype_p);
6832 vec<tree, va_gc> *args;
6833 location_t close_paren_loc = UNKNOWN_LOCATION;
6835 is_member_access = false;
6837 is_builtin_constant_p
6838 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6839 if (is_builtin_constant_p)
6841 /* The whole point of __builtin_constant_p is to allow
6842 non-constant expressions to appear as arguments. */
6843 saved_integral_constant_expression_p
6844 = parser->integral_constant_expression_p;
6845 saved_non_integral_constant_expression_p
6846 = parser->non_integral_constant_expression_p;
6847 parser->integral_constant_expression_p = false;
6849 args = (cp_parser_parenthesized_expression_list
6850 (parser, non_attr,
6851 /*cast_p=*/false, /*allow_expansion_p=*/true,
6852 /*non_constant_p=*/NULL,
6853 /*close_paren_loc=*/&close_paren_loc));
6854 if (is_builtin_constant_p)
6856 parser->integral_constant_expression_p
6857 = saved_integral_constant_expression_p;
6858 parser->non_integral_constant_expression_p
6859 = saved_non_integral_constant_expression_p;
6862 if (args == NULL)
6864 postfix_expression = error_mark_node;
6865 break;
6868 /* Function calls are not permitted in
6869 constant-expressions. */
6870 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6871 && cp_parser_non_integral_constant_expression (parser,
6872 NIC_FUNC_CALL))
6874 postfix_expression = error_mark_node;
6875 release_tree_vector (args);
6876 break;
6879 koenig_p = false;
6880 if (idk == CP_ID_KIND_UNQUALIFIED
6881 || idk == CP_ID_KIND_TEMPLATE_ID)
6883 if (identifier_p (postfix_expression))
6885 if (!args->is_empty ())
6887 koenig_p = true;
6888 if (!any_type_dependent_arguments_p (args))
6889 postfix_expression
6890 = perform_koenig_lookup (postfix_expression, args,
6891 complain);
6893 else
6894 postfix_expression
6895 = unqualified_fn_lookup_error (postfix_expression);
6897 /* We do not perform argument-dependent lookup if
6898 normal lookup finds a non-function, in accordance
6899 with the expected resolution of DR 218. */
6900 else if (!args->is_empty ()
6901 && is_overloaded_fn (postfix_expression))
6903 tree fn = get_first_fn (postfix_expression);
6904 fn = STRIP_TEMPLATE (fn);
6906 /* Do not do argument dependent lookup if regular
6907 lookup finds a member function or a block-scope
6908 function declaration. [basic.lookup.argdep]/3 */
6909 if (!DECL_FUNCTION_MEMBER_P (fn)
6910 && !DECL_LOCAL_FUNCTION_P (fn))
6912 koenig_p = true;
6913 if (!any_type_dependent_arguments_p (args))
6914 postfix_expression
6915 = perform_koenig_lookup (postfix_expression, args,
6916 complain);
6921 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6922 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6923 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6924 && vec_safe_length (args) == 3)
6926 tree arg0 = (*args)[0];
6927 tree arg1 = (*args)[1];
6928 tree arg2 = (*args)[2];
6929 int literal_mask = ((!!integer_zerop (arg1) << 1)
6930 | (!!integer_zerop (arg2) << 2));
6931 if (TREE_CODE (arg2) == CONST_DECL)
6932 arg2 = DECL_INITIAL (arg2);
6933 warn_for_memset (input_location, arg0, arg2, literal_mask);
6936 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6937 && warn_restrict)
6939 unsigned i;
6940 tree arg;
6941 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6942 TREE_VISITED (arg) = 0;
6944 unsigned param_pos = 0;
6945 for (tree decl = DECL_ARGUMENTS (postfix_expression);
6946 decl != NULL_TREE;
6947 decl = DECL_CHAIN (decl), param_pos++)
6949 tree type = TREE_TYPE (decl);
6950 if (POINTER_TYPE_P (type) && TYPE_RESTRICT (type)
6951 && !TYPE_READONLY (TREE_TYPE (type)))
6952 warn_for_restrict (param_pos, args);
6955 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
6956 TREE_VISITED (arg) = 0;
6959 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6961 tree instance = TREE_OPERAND (postfix_expression, 0);
6962 tree fn = TREE_OPERAND (postfix_expression, 1);
6964 if (processing_template_decl
6965 && (type_dependent_object_expression_p (instance)
6966 || (!BASELINK_P (fn)
6967 && TREE_CODE (fn) != FIELD_DECL)
6968 || type_dependent_expression_p (fn)
6969 || any_type_dependent_arguments_p (args)))
6971 maybe_generic_this_capture (instance, fn);
6972 postfix_expression
6973 = build_nt_call_vec (postfix_expression, args);
6974 release_tree_vector (args);
6975 break;
6978 if (BASELINK_P (fn))
6980 postfix_expression
6981 = (build_new_method_call
6982 (instance, fn, &args, NULL_TREE,
6983 (idk == CP_ID_KIND_QUALIFIED
6984 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6985 : LOOKUP_NORMAL),
6986 /*fn_p=*/NULL,
6987 complain));
6989 else
6990 postfix_expression
6991 = finish_call_expr (postfix_expression, &args,
6992 /*disallow_virtual=*/false,
6993 /*koenig_p=*/false,
6994 complain);
6996 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6997 || TREE_CODE (postfix_expression) == MEMBER_REF
6998 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6999 postfix_expression = (build_offset_ref_call_from_tree
7000 (postfix_expression, &args,
7001 complain));
7002 else if (idk == CP_ID_KIND_QUALIFIED)
7003 /* A call to a static class member, or a namespace-scope
7004 function. */
7005 postfix_expression
7006 = finish_call_expr (postfix_expression, &args,
7007 /*disallow_virtual=*/true,
7008 koenig_p,
7009 complain);
7010 else
7011 /* All other function calls. */
7012 postfix_expression
7013 = finish_call_expr (postfix_expression, &args,
7014 /*disallow_virtual=*/false,
7015 koenig_p,
7016 complain);
7018 if (close_paren_loc != UNKNOWN_LOCATION)
7020 location_t combined_loc = make_location (token->location,
7021 start_loc,
7022 close_paren_loc);
7023 postfix_expression.set_location (combined_loc);
7026 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7027 idk = CP_ID_KIND_NONE;
7029 release_tree_vector (args);
7031 break;
7033 case CPP_DOT:
7034 case CPP_DEREF:
7035 /* postfix-expression . template [opt] id-expression
7036 postfix-expression . pseudo-destructor-name
7037 postfix-expression -> template [opt] id-expression
7038 postfix-expression -> pseudo-destructor-name */
7040 /* Consume the `.' or `->' operator. */
7041 cp_lexer_consume_token (parser->lexer);
7043 postfix_expression
7044 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7045 postfix_expression,
7046 false, &idk, loc);
7048 is_member_access = true;
7049 break;
7051 case CPP_PLUS_PLUS:
7052 /* postfix-expression ++ */
7053 /* Consume the `++' token. */
7054 cp_lexer_consume_token (parser->lexer);
7055 /* Generate a representation for the complete expression. */
7056 postfix_expression
7057 = finish_increment_expr (postfix_expression,
7058 POSTINCREMENT_EXPR);
7059 /* Increments may not appear in constant-expressions. */
7060 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7061 postfix_expression = error_mark_node;
7062 idk = CP_ID_KIND_NONE;
7063 is_member_access = false;
7064 break;
7066 case CPP_MINUS_MINUS:
7067 /* postfix-expression -- */
7068 /* Consume the `--' token. */
7069 cp_lexer_consume_token (parser->lexer);
7070 /* Generate a representation for the complete expression. */
7071 postfix_expression
7072 = finish_increment_expr (postfix_expression,
7073 POSTDECREMENT_EXPR);
7074 /* Decrements may not appear in constant-expressions. */
7075 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7076 postfix_expression = error_mark_node;
7077 idk = CP_ID_KIND_NONE;
7078 is_member_access = false;
7079 break;
7081 default:
7082 if (pidk_return != NULL)
7083 * pidk_return = idk;
7084 if (member_access_only_p)
7085 return is_member_access
7086 ? postfix_expression
7087 : cp_expr (error_mark_node);
7088 else
7089 return postfix_expression;
7093 /* We should never get here. */
7094 gcc_unreachable ();
7095 return error_mark_node;
7098 /* This function parses Cilk Plus array notations. If a normal array expr. is
7099 parsed then the array index is passed back to the caller through *INIT_INDEX
7100 and the function returns a NULL_TREE. If array notation expr. is parsed,
7101 then *INIT_INDEX is ignored by the caller and the function returns
7102 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7103 error_mark_node. */
7105 static tree
7106 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7107 tree array_value)
7109 cp_token *token = NULL;
7110 tree length_index, stride = NULL_TREE, value_tree, array_type;
7111 if (!array_value || array_value == error_mark_node)
7113 cp_parser_skip_to_end_of_statement (parser);
7114 return error_mark_node;
7117 array_type = TREE_TYPE (array_value);
7119 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7120 parser->colon_corrects_to_scope_p = false;
7121 token = cp_lexer_peek_token (parser->lexer);
7123 if (!token)
7125 cp_parser_error (parser, "expected %<:%> or numeral");
7126 return error_mark_node;
7128 else if (token->type == CPP_COLON)
7130 /* Consume the ':'. */
7131 cp_lexer_consume_token (parser->lexer);
7133 /* If we are here, then we have a case like this A[:]. */
7134 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7136 cp_parser_error (parser, "expected %<]%>");
7137 cp_parser_skip_to_end_of_statement (parser);
7138 return error_mark_node;
7140 *init_index = NULL_TREE;
7141 stride = NULL_TREE;
7142 length_index = NULL_TREE;
7144 else
7146 /* If we are here, then there are three valid possibilities:
7147 1. ARRAY [ EXP ]
7148 2. ARRAY [ EXP : EXP ]
7149 3. ARRAY [ EXP : EXP : EXP ] */
7151 *init_index = cp_parser_expression (parser);
7152 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7154 /* This indicates that we have a normal array expression. */
7155 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7156 return NULL_TREE;
7159 /* Consume the ':'. */
7160 cp_lexer_consume_token (parser->lexer);
7161 length_index = cp_parser_expression (parser);
7162 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7164 cp_lexer_consume_token (parser->lexer);
7165 stride = cp_parser_expression (parser);
7168 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7170 if (*init_index == error_mark_node || length_index == error_mark_node
7171 || stride == error_mark_node || array_type == error_mark_node)
7173 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7174 cp_lexer_consume_token (parser->lexer);
7175 return error_mark_node;
7177 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7179 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7180 length_index, stride, array_type);
7181 return value_tree;
7184 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7185 by cp_parser_builtin_offsetof. We're looking for
7187 postfix-expression [ expression ]
7188 postfix-expression [ braced-init-list ] (C++11)
7190 FOR_OFFSETOF is set if we're being called in that context, which
7191 changes how we deal with integer constant expressions. */
7193 static tree
7194 cp_parser_postfix_open_square_expression (cp_parser *parser,
7195 tree postfix_expression,
7196 bool for_offsetof,
7197 bool decltype_p)
7199 tree index = NULL_TREE;
7200 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7201 bool saved_greater_than_is_operator_p;
7203 /* Consume the `[' token. */
7204 cp_lexer_consume_token (parser->lexer);
7206 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7207 parser->greater_than_is_operator_p = true;
7209 /* Parse the index expression. */
7210 /* ??? For offsetof, there is a question of what to allow here. If
7211 offsetof is not being used in an integral constant expression context,
7212 then we *could* get the right answer by computing the value at runtime.
7213 If we are in an integral constant expression context, then we might
7214 could accept any constant expression; hard to say without analysis.
7215 Rather than open the barn door too wide right away, allow only integer
7216 constant expressions here. */
7217 if (for_offsetof)
7218 index = cp_parser_constant_expression (parser);
7219 else
7221 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7223 bool expr_nonconst_p;
7224 cp_lexer_set_source_position (parser->lexer);
7225 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7226 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7227 if (flag_cilkplus
7228 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7230 error_at (cp_lexer_peek_token (parser->lexer)->location,
7231 "braced list index is not allowed with array "
7232 "notation");
7233 cp_parser_skip_to_end_of_statement (parser);
7234 return error_mark_node;
7237 else if (flag_cilkplus)
7239 /* Here are have these two options:
7240 ARRAY[EXP : EXP] - Array notation expr with default
7241 stride of 1.
7242 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7243 stride. */
7244 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7245 postfix_expression);
7246 if (an_exp)
7247 return an_exp;
7249 else
7250 index = cp_parser_expression (parser);
7253 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7255 /* Look for the closing `]'. */
7256 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7258 /* Build the ARRAY_REF. */
7259 postfix_expression = grok_array_decl (loc, postfix_expression,
7260 index, decltype_p);
7262 /* When not doing offsetof, array references are not permitted in
7263 constant-expressions. */
7264 if (!for_offsetof
7265 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7266 postfix_expression = error_mark_node;
7268 return postfix_expression;
7271 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7272 by cp_parser_builtin_offsetof. We're looking for
7274 postfix-expression . template [opt] id-expression
7275 postfix-expression . pseudo-destructor-name
7276 postfix-expression -> template [opt] id-expression
7277 postfix-expression -> pseudo-destructor-name
7279 FOR_OFFSETOF is set if we're being called in that context. That sorta
7280 limits what of the above we'll actually accept, but nevermind.
7281 TOKEN_TYPE is the "." or "->" token, which will already have been
7282 removed from the stream. */
7284 static tree
7285 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7286 enum cpp_ttype token_type,
7287 cp_expr postfix_expression,
7288 bool for_offsetof, cp_id_kind *idk,
7289 location_t location)
7291 tree name;
7292 bool dependent_p;
7293 bool pseudo_destructor_p;
7294 tree scope = NULL_TREE;
7295 location_t start_loc = postfix_expression.get_start ();
7297 /* If this is a `->' operator, dereference the pointer. */
7298 if (token_type == CPP_DEREF)
7299 postfix_expression = build_x_arrow (location, postfix_expression,
7300 tf_warning_or_error);
7301 /* Check to see whether or not the expression is type-dependent and
7302 not the current instantiation. */
7303 dependent_p = type_dependent_object_expression_p (postfix_expression);
7304 /* The identifier following the `->' or `.' is not qualified. */
7305 parser->scope = NULL_TREE;
7306 parser->qualifying_scope = NULL_TREE;
7307 parser->object_scope = NULL_TREE;
7308 *idk = CP_ID_KIND_NONE;
7310 /* Enter the scope corresponding to the type of the object
7311 given by the POSTFIX_EXPRESSION. */
7312 if (!dependent_p)
7314 scope = TREE_TYPE (postfix_expression);
7315 /* According to the standard, no expression should ever have
7316 reference type. Unfortunately, we do not currently match
7317 the standard in this respect in that our internal representation
7318 of an expression may have reference type even when the standard
7319 says it does not. Therefore, we have to manually obtain the
7320 underlying type here. */
7321 scope = non_reference (scope);
7322 /* The type of the POSTFIX_EXPRESSION must be complete. */
7323 /* Unlike the object expression in other contexts, *this is not
7324 required to be of complete type for purposes of class member
7325 access (5.2.5) outside the member function body. */
7326 if (postfix_expression != current_class_ref
7327 && scope != error_mark_node
7328 && !(processing_template_decl
7329 && current_class_type
7330 && (same_type_ignoring_top_level_qualifiers_p
7331 (scope, current_class_type))))
7333 scope = complete_type (scope);
7334 if (!COMPLETE_TYPE_P (scope)
7335 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7336 && EXPR_P (postfix_expression))
7338 /* In a template, be permissive by treating an object expression
7339 of incomplete type as dependent (after a pedwarn). */
7340 diagnostic_t kind = (processing_template_decl
7341 ? DK_PEDWARN
7342 : DK_ERROR);
7343 cxx_incomplete_type_diagnostic
7344 (location_of (postfix_expression),
7345 postfix_expression, scope, kind);
7346 if (processing_template_decl)
7348 dependent_p = true;
7349 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7354 if (!dependent_p)
7356 /* Let the name lookup machinery know that we are processing a
7357 class member access expression. */
7358 parser->context->object_type = scope;
7359 /* If something went wrong, we want to be able to discern that case,
7360 as opposed to the case where there was no SCOPE due to the type
7361 of expression being dependent. */
7362 if (!scope)
7363 scope = error_mark_node;
7364 /* If the SCOPE was erroneous, make the various semantic analysis
7365 functions exit quickly -- and without issuing additional error
7366 messages. */
7367 if (scope == error_mark_node)
7368 postfix_expression = error_mark_node;
7372 if (dependent_p)
7373 /* Tell cp_parser_lookup_name that there was an object, even though it's
7374 type-dependent. */
7375 parser->context->object_type = unknown_type_node;
7377 /* Assume this expression is not a pseudo-destructor access. */
7378 pseudo_destructor_p = false;
7380 /* If the SCOPE is a scalar type, then, if this is a valid program,
7381 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7382 is type dependent, it can be pseudo-destructor-name or something else.
7383 Try to parse it as pseudo-destructor-name first. */
7384 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7386 tree s;
7387 tree type;
7389 cp_parser_parse_tentatively (parser);
7390 /* Parse the pseudo-destructor-name. */
7391 s = NULL_TREE;
7392 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7393 &s, &type);
7394 if (dependent_p
7395 && (cp_parser_error_occurred (parser)
7396 || !SCALAR_TYPE_P (type)))
7397 cp_parser_abort_tentative_parse (parser);
7398 else if (cp_parser_parse_definitely (parser))
7400 pseudo_destructor_p = true;
7401 postfix_expression
7402 = finish_pseudo_destructor_expr (postfix_expression,
7403 s, type, location);
7407 if (!pseudo_destructor_p)
7409 /* If the SCOPE is not a scalar type, we are looking at an
7410 ordinary class member access expression, rather than a
7411 pseudo-destructor-name. */
7412 bool template_p;
7413 cp_token *token = cp_lexer_peek_token (parser->lexer);
7414 /* Parse the id-expression. */
7415 name = (cp_parser_id_expression
7416 (parser,
7417 cp_parser_optional_template_keyword (parser),
7418 /*check_dependency_p=*/true,
7419 &template_p,
7420 /*declarator_p=*/false,
7421 /*optional_p=*/false));
7422 /* In general, build a SCOPE_REF if the member name is qualified.
7423 However, if the name was not dependent and has already been
7424 resolved; there is no need to build the SCOPE_REF. For example;
7426 struct X { void f(); };
7427 template <typename T> void f(T* t) { t->X::f(); }
7429 Even though "t" is dependent, "X::f" is not and has been resolved
7430 to a BASELINK; there is no need to include scope information. */
7432 /* But we do need to remember that there was an explicit scope for
7433 virtual function calls. */
7434 if (parser->scope)
7435 *idk = CP_ID_KIND_QUALIFIED;
7437 /* If the name is a template-id that names a type, we will get a
7438 TYPE_DECL here. That is invalid code. */
7439 if (TREE_CODE (name) == TYPE_DECL)
7441 error_at (token->location, "invalid use of %qD", name);
7442 postfix_expression = error_mark_node;
7444 else
7446 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7448 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7450 error_at (token->location, "%<%D::%D%> is not a class member",
7451 parser->scope, name);
7452 postfix_expression = error_mark_node;
7454 else
7455 name = build_qualified_name (/*type=*/NULL_TREE,
7456 parser->scope,
7457 name,
7458 template_p);
7459 parser->scope = NULL_TREE;
7460 parser->qualifying_scope = NULL_TREE;
7461 parser->object_scope = NULL_TREE;
7463 if (parser->scope && name && BASELINK_P (name))
7464 adjust_result_of_qualified_name_lookup
7465 (name, parser->scope, scope);
7466 postfix_expression
7467 = finish_class_member_access_expr (postfix_expression, name,
7468 template_p,
7469 tf_warning_or_error);
7470 /* Build a location e.g.:
7471 ptr->access_expr
7472 ~~~^~~~~~~~~~~~~
7473 where the caret is at the deref token, ranging from
7474 the start of postfix_expression to the end of the access expr. */
7475 location_t end_loc
7476 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7477 location_t combined_loc
7478 = make_location (input_location, start_loc, end_loc);
7479 protected_set_expr_location (postfix_expression, combined_loc);
7483 /* We no longer need to look up names in the scope of the object on
7484 the left-hand side of the `.' or `->' operator. */
7485 parser->context->object_type = NULL_TREE;
7487 /* Outside of offsetof, these operators may not appear in
7488 constant-expressions. */
7489 if (!for_offsetof
7490 && (cp_parser_non_integral_constant_expression
7491 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7492 postfix_expression = error_mark_node;
7494 return postfix_expression;
7497 /* Parse a parenthesized expression-list.
7499 expression-list:
7500 assignment-expression
7501 expression-list, assignment-expression
7503 attribute-list:
7504 expression-list
7505 identifier
7506 identifier, expression-list
7508 CAST_P is true if this expression is the target of a cast.
7510 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7511 argument pack.
7513 Returns a vector of trees. Each element is a representation of an
7514 assignment-expression. NULL is returned if the ( and or ) are
7515 missing. An empty, but allocated, vector is returned on no
7516 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7517 if we are parsing an attribute list for an attribute that wants a
7518 plain identifier argument, normal_attr for an attribute that wants
7519 an expression, or non_attr if we aren't parsing an attribute list. If
7520 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7521 not all of the expressions in the list were constant.
7522 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7523 will be written to with the location of the closing parenthesis. If
7524 an error occurs, it may or may not be written to. */
7526 static vec<tree, va_gc> *
7527 cp_parser_parenthesized_expression_list (cp_parser* parser,
7528 int is_attribute_list,
7529 bool cast_p,
7530 bool allow_expansion_p,
7531 bool *non_constant_p,
7532 location_t *close_paren_loc)
7534 vec<tree, va_gc> *expression_list;
7535 bool fold_expr_p = is_attribute_list != non_attr;
7536 tree identifier = NULL_TREE;
7537 bool saved_greater_than_is_operator_p;
7539 /* Assume all the expressions will be constant. */
7540 if (non_constant_p)
7541 *non_constant_p = false;
7543 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7544 return NULL;
7546 expression_list = make_tree_vector ();
7548 /* Within a parenthesized expression, a `>' token is always
7549 the greater-than operator. */
7550 saved_greater_than_is_operator_p
7551 = parser->greater_than_is_operator_p;
7552 parser->greater_than_is_operator_p = true;
7554 /* Consume expressions until there are no more. */
7555 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7556 while (true)
7558 tree expr;
7560 /* At the beginning of attribute lists, check to see if the
7561 next token is an identifier. */
7562 if (is_attribute_list == id_attr
7563 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7565 cp_token *token;
7567 /* Consume the identifier. */
7568 token = cp_lexer_consume_token (parser->lexer);
7569 /* Save the identifier. */
7570 identifier = token->u.value;
7572 else
7574 bool expr_non_constant_p;
7576 /* Parse the next assignment-expression. */
7577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7579 /* A braced-init-list. */
7580 cp_lexer_set_source_position (parser->lexer);
7581 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7582 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7583 if (non_constant_p && expr_non_constant_p)
7584 *non_constant_p = true;
7586 else if (non_constant_p)
7588 expr = (cp_parser_constant_expression
7589 (parser, /*allow_non_constant_p=*/true,
7590 &expr_non_constant_p));
7591 if (expr_non_constant_p)
7592 *non_constant_p = true;
7594 else
7595 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7596 cast_p);
7598 if (fold_expr_p)
7599 expr = instantiate_non_dependent_expr (expr);
7601 /* If we have an ellipsis, then this is an expression
7602 expansion. */
7603 if (allow_expansion_p
7604 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7606 /* Consume the `...'. */
7607 cp_lexer_consume_token (parser->lexer);
7609 /* Build the argument pack. */
7610 expr = make_pack_expansion (expr);
7613 /* Add it to the list. We add error_mark_node
7614 expressions to the list, so that we can still tell if
7615 the correct form for a parenthesized expression-list
7616 is found. That gives better errors. */
7617 vec_safe_push (expression_list, expr);
7619 if (expr == error_mark_node)
7620 goto skip_comma;
7623 /* After the first item, attribute lists look the same as
7624 expression lists. */
7625 is_attribute_list = non_attr;
7627 get_comma:;
7628 /* If the next token isn't a `,', then we are done. */
7629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7630 break;
7632 /* Otherwise, consume the `,' and keep going. */
7633 cp_lexer_consume_token (parser->lexer);
7636 if (close_paren_loc)
7637 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7639 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7641 int ending;
7643 skip_comma:;
7644 /* We try and resync to an unnested comma, as that will give the
7645 user better diagnostics. */
7646 ending = cp_parser_skip_to_closing_parenthesis (parser,
7647 /*recovering=*/true,
7648 /*or_comma=*/true,
7649 /*consume_paren=*/true);
7650 if (ending < 0)
7651 goto get_comma;
7652 if (!ending)
7654 parser->greater_than_is_operator_p
7655 = saved_greater_than_is_operator_p;
7656 return NULL;
7660 parser->greater_than_is_operator_p
7661 = saved_greater_than_is_operator_p;
7663 if (identifier)
7664 vec_safe_insert (expression_list, 0, identifier);
7666 return expression_list;
7669 /* Parse a pseudo-destructor-name.
7671 pseudo-destructor-name:
7672 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7673 :: [opt] nested-name-specifier template template-id :: ~ type-name
7674 :: [opt] nested-name-specifier [opt] ~ type-name
7676 If either of the first two productions is used, sets *SCOPE to the
7677 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7678 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7679 or ERROR_MARK_NODE if the parse fails. */
7681 static void
7682 cp_parser_pseudo_destructor_name (cp_parser* parser,
7683 tree object,
7684 tree* scope,
7685 tree* type)
7687 bool nested_name_specifier_p;
7689 /* Handle ~auto. */
7690 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7691 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7692 && !type_dependent_expression_p (object))
7694 if (cxx_dialect < cxx14)
7695 pedwarn (input_location, 0,
7696 "%<~auto%> only available with "
7697 "-std=c++14 or -std=gnu++14");
7698 cp_lexer_consume_token (parser->lexer);
7699 cp_lexer_consume_token (parser->lexer);
7700 *scope = NULL_TREE;
7701 *type = TREE_TYPE (object);
7702 return;
7705 /* Assume that things will not work out. */
7706 *type = error_mark_node;
7708 /* Look for the optional `::' operator. */
7709 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7710 /* Look for the optional nested-name-specifier. */
7711 nested_name_specifier_p
7712 = (cp_parser_nested_name_specifier_opt (parser,
7713 /*typename_keyword_p=*/false,
7714 /*check_dependency_p=*/true,
7715 /*type_p=*/false,
7716 /*is_declaration=*/false)
7717 != NULL_TREE);
7718 /* Now, if we saw a nested-name-specifier, we might be doing the
7719 second production. */
7720 if (nested_name_specifier_p
7721 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7723 /* Consume the `template' keyword. */
7724 cp_lexer_consume_token (parser->lexer);
7725 /* Parse the template-id. */
7726 cp_parser_template_id (parser,
7727 /*template_keyword_p=*/true,
7728 /*check_dependency_p=*/false,
7729 class_type,
7730 /*is_declaration=*/true);
7731 /* Look for the `::' token. */
7732 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7734 /* If the next token is not a `~', then there might be some
7735 additional qualification. */
7736 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7738 /* At this point, we're looking for "type-name :: ~". The type-name
7739 must not be a class-name, since this is a pseudo-destructor. So,
7740 it must be either an enum-name, or a typedef-name -- both of which
7741 are just identifiers. So, we peek ahead to check that the "::"
7742 and "~" tokens are present; if they are not, then we can avoid
7743 calling type_name. */
7744 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7745 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7746 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7748 cp_parser_error (parser, "non-scalar type");
7749 return;
7752 /* Look for the type-name. */
7753 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7754 if (*scope == error_mark_node)
7755 return;
7757 /* Look for the `::' token. */
7758 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7760 else
7761 *scope = NULL_TREE;
7763 /* Look for the `~'. */
7764 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7766 /* Once we see the ~, this has to be a pseudo-destructor. */
7767 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7768 cp_parser_commit_to_topmost_tentative_parse (parser);
7770 /* Look for the type-name again. We are not responsible for
7771 checking that it matches the first type-name. */
7772 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7775 /* Parse a unary-expression.
7777 unary-expression:
7778 postfix-expression
7779 ++ cast-expression
7780 -- cast-expression
7781 unary-operator cast-expression
7782 sizeof unary-expression
7783 sizeof ( type-id )
7784 alignof ( type-id ) [C++0x]
7785 new-expression
7786 delete-expression
7788 GNU Extensions:
7790 unary-expression:
7791 __extension__ cast-expression
7792 __alignof__ unary-expression
7793 __alignof__ ( type-id )
7794 alignof unary-expression [C++0x]
7795 __real__ cast-expression
7796 __imag__ cast-expression
7797 && identifier
7798 sizeof ( type-id ) { initializer-list , [opt] }
7799 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7800 __alignof__ ( type-id ) { initializer-list , [opt] }
7802 ADDRESS_P is true iff the unary-expression is appearing as the
7803 operand of the `&' operator. CAST_P is true if this expression is
7804 the target of a cast.
7806 Returns a representation of the expression. */
7808 static cp_expr
7809 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7810 bool address_p, bool cast_p, bool decltype_p)
7812 cp_token *token;
7813 enum tree_code unary_operator;
7815 /* Peek at the next token. */
7816 token = cp_lexer_peek_token (parser->lexer);
7817 /* Some keywords give away the kind of expression. */
7818 if (token->type == CPP_KEYWORD)
7820 enum rid keyword = token->keyword;
7822 switch (keyword)
7824 case RID_ALIGNOF:
7825 case RID_SIZEOF:
7827 tree operand, ret;
7828 enum tree_code op;
7829 location_t first_loc;
7831 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7832 /* Consume the token. */
7833 cp_lexer_consume_token (parser->lexer);
7834 first_loc = cp_lexer_peek_token (parser->lexer)->location;
7835 /* Parse the operand. */
7836 operand = cp_parser_sizeof_operand (parser, keyword);
7838 if (TYPE_P (operand))
7839 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7840 else
7842 /* ISO C++ defines alignof only with types, not with
7843 expressions. So pedwarn if alignof is used with a non-
7844 type expression. However, __alignof__ is ok. */
7845 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
7846 pedwarn (token->location, OPT_Wpedantic,
7847 "ISO C++ does not allow %<alignof%> "
7848 "with a non-type");
7850 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7852 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7853 SIZEOF_EXPR with the original operand. */
7854 if (op == SIZEOF_EXPR && ret != error_mark_node)
7856 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7858 if (!processing_template_decl && TYPE_P (operand))
7860 ret = build_min (SIZEOF_EXPR, size_type_node,
7861 build1 (NOP_EXPR, operand,
7862 error_mark_node));
7863 SIZEOF_EXPR_TYPE_P (ret) = 1;
7865 else
7866 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7867 TREE_SIDE_EFFECTS (ret) = 0;
7868 TREE_READONLY (ret) = 1;
7870 SET_EXPR_LOCATION (ret, first_loc);
7872 return ret;
7875 case RID_NEW:
7876 return cp_parser_new_expression (parser);
7878 case RID_DELETE:
7879 return cp_parser_delete_expression (parser);
7881 case RID_EXTENSION:
7883 /* The saved value of the PEDANTIC flag. */
7884 int saved_pedantic;
7885 tree expr;
7887 /* Save away the PEDANTIC flag. */
7888 cp_parser_extension_opt (parser, &saved_pedantic);
7889 /* Parse the cast-expression. */
7890 expr = cp_parser_simple_cast_expression (parser);
7891 /* Restore the PEDANTIC flag. */
7892 pedantic = saved_pedantic;
7894 return expr;
7897 case RID_REALPART:
7898 case RID_IMAGPART:
7900 tree expression;
7902 /* Consume the `__real__' or `__imag__' token. */
7903 cp_lexer_consume_token (parser->lexer);
7904 /* Parse the cast-expression. */
7905 expression = cp_parser_simple_cast_expression (parser);
7906 /* Create the complete representation. */
7907 return build_x_unary_op (token->location,
7908 (keyword == RID_REALPART
7909 ? REALPART_EXPR : IMAGPART_EXPR),
7910 expression,
7911 tf_warning_or_error);
7913 break;
7915 case RID_TRANSACTION_ATOMIC:
7916 case RID_TRANSACTION_RELAXED:
7917 return cp_parser_transaction_expression (parser, keyword);
7919 case RID_NOEXCEPT:
7921 tree expr;
7922 const char *saved_message;
7923 bool saved_integral_constant_expression_p;
7924 bool saved_non_integral_constant_expression_p;
7925 bool saved_greater_than_is_operator_p;
7927 cp_lexer_consume_token (parser->lexer);
7928 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7930 saved_message = parser->type_definition_forbidden_message;
7931 parser->type_definition_forbidden_message
7932 = G_("types may not be defined in %<noexcept%> expressions");
7934 saved_integral_constant_expression_p
7935 = parser->integral_constant_expression_p;
7936 saved_non_integral_constant_expression_p
7937 = parser->non_integral_constant_expression_p;
7938 parser->integral_constant_expression_p = false;
7940 saved_greater_than_is_operator_p
7941 = parser->greater_than_is_operator_p;
7942 parser->greater_than_is_operator_p = true;
7944 ++cp_unevaluated_operand;
7945 ++c_inhibit_evaluation_warnings;
7946 ++cp_noexcept_operand;
7947 expr = cp_parser_expression (parser);
7948 --cp_noexcept_operand;
7949 --c_inhibit_evaluation_warnings;
7950 --cp_unevaluated_operand;
7952 parser->greater_than_is_operator_p
7953 = saved_greater_than_is_operator_p;
7955 parser->integral_constant_expression_p
7956 = saved_integral_constant_expression_p;
7957 parser->non_integral_constant_expression_p
7958 = saved_non_integral_constant_expression_p;
7960 parser->type_definition_forbidden_message = saved_message;
7962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7963 return finish_noexcept_expr (expr, tf_warning_or_error);
7966 default:
7967 break;
7971 /* Look for the `:: new' and `:: delete', which also signal the
7972 beginning of a new-expression, or delete-expression,
7973 respectively. If the next token is `::', then it might be one of
7974 these. */
7975 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7977 enum rid keyword;
7979 /* See if the token after the `::' is one of the keywords in
7980 which we're interested. */
7981 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7982 /* If it's `new', we have a new-expression. */
7983 if (keyword == RID_NEW)
7984 return cp_parser_new_expression (parser);
7985 /* Similarly, for `delete'. */
7986 else if (keyword == RID_DELETE)
7987 return cp_parser_delete_expression (parser);
7990 /* Look for a unary operator. */
7991 unary_operator = cp_parser_unary_operator (token);
7992 /* The `++' and `--' operators can be handled similarly, even though
7993 they are not technically unary-operators in the grammar. */
7994 if (unary_operator == ERROR_MARK)
7996 if (token->type == CPP_PLUS_PLUS)
7997 unary_operator = PREINCREMENT_EXPR;
7998 else if (token->type == CPP_MINUS_MINUS)
7999 unary_operator = PREDECREMENT_EXPR;
8000 /* Handle the GNU address-of-label extension. */
8001 else if (cp_parser_allow_gnu_extensions_p (parser)
8002 && token->type == CPP_AND_AND)
8004 tree identifier;
8005 tree expression;
8006 location_t start_loc = token->location;
8008 /* Consume the '&&' token. */
8009 cp_lexer_consume_token (parser->lexer);
8010 /* Look for the identifier. */
8011 location_t finish_loc
8012 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8013 identifier = cp_parser_identifier (parser);
8014 /* Construct a location of the form:
8015 &&label
8016 ^~~~~~~
8017 with caret==start at the "&&", finish at the end of the label. */
8018 location_t combined_loc
8019 = make_location (start_loc, start_loc, finish_loc);
8020 /* Create an expression representing the address. */
8021 expression = finish_label_address_expr (identifier, combined_loc);
8022 if (cp_parser_non_integral_constant_expression (parser,
8023 NIC_ADDR_LABEL))
8024 expression = error_mark_node;
8025 return expression;
8028 if (unary_operator != ERROR_MARK)
8030 cp_expr cast_expression;
8031 cp_expr expression = error_mark_node;
8032 non_integral_constant non_constant_p = NIC_NONE;
8033 location_t loc = token->location;
8034 tsubst_flags_t complain = complain_flags (decltype_p);
8036 /* Consume the operator token. */
8037 token = cp_lexer_consume_token (parser->lexer);
8038 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8040 /* Parse the cast-expression. */
8041 cast_expression
8042 = cp_parser_cast_expression (parser,
8043 unary_operator == ADDR_EXPR,
8044 /*cast_p=*/false,
8045 /*decltype*/false,
8046 pidk);
8048 /* Make a location:
8049 OP_TOKEN CAST_EXPRESSION
8050 ^~~~~~~~~~~~~~~~~~~~~~~~~
8051 with start==caret at the operator token, and
8052 extending to the end of the cast_expression. */
8053 loc = make_location (loc, loc, cast_expression.get_finish ());
8055 /* Now, build an appropriate representation. */
8056 switch (unary_operator)
8058 case INDIRECT_REF:
8059 non_constant_p = NIC_STAR;
8060 expression = build_x_indirect_ref (loc, cast_expression,
8061 RO_UNARY_STAR,
8062 complain);
8063 /* TODO: build_x_indirect_ref does not always honor the
8064 location, so ensure it is set. */
8065 expression.set_location (loc);
8066 break;
8068 case ADDR_EXPR:
8069 non_constant_p = NIC_ADDR;
8070 /* Fall through. */
8071 case BIT_NOT_EXPR:
8072 expression = build_x_unary_op (loc, unary_operator,
8073 cast_expression,
8074 complain);
8075 /* TODO: build_x_unary_op does not always honor the location,
8076 so ensure it is set. */
8077 expression.set_location (loc);
8078 break;
8080 case PREINCREMENT_EXPR:
8081 case PREDECREMENT_EXPR:
8082 non_constant_p = unary_operator == PREINCREMENT_EXPR
8083 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8084 /* Fall through. */
8085 case NEGATE_EXPR:
8086 /* Immediately fold negation of a constant, unless the constant is 0
8087 (since -0 == 0) or it would overflow. */
8088 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8089 && CONSTANT_CLASS_P (cast_expression)
8090 && !integer_zerop (cast_expression)
8091 && !TREE_OVERFLOW (cast_expression))
8093 tree folded = fold_build1 (unary_operator,
8094 TREE_TYPE (cast_expression),
8095 cast_expression);
8096 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8098 expression = cp_expr (folded, loc);
8099 break;
8102 /* Fall through. */
8103 case UNARY_PLUS_EXPR:
8104 case TRUTH_NOT_EXPR:
8105 expression = finish_unary_op_expr (loc, unary_operator,
8106 cast_expression, complain);
8107 break;
8109 default:
8110 gcc_unreachable ();
8113 if (non_constant_p != NIC_NONE
8114 && cp_parser_non_integral_constant_expression (parser,
8115 non_constant_p))
8116 expression = error_mark_node;
8118 return expression;
8121 return cp_parser_postfix_expression (parser, address_p, cast_p,
8122 /*member_access_only_p=*/false,
8123 decltype_p,
8124 pidk);
8127 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8128 unary-operator, the corresponding tree code is returned. */
8130 static enum tree_code
8131 cp_parser_unary_operator (cp_token* token)
8133 switch (token->type)
8135 case CPP_MULT:
8136 return INDIRECT_REF;
8138 case CPP_AND:
8139 return ADDR_EXPR;
8141 case CPP_PLUS:
8142 return UNARY_PLUS_EXPR;
8144 case CPP_MINUS:
8145 return NEGATE_EXPR;
8147 case CPP_NOT:
8148 return TRUTH_NOT_EXPR;
8150 case CPP_COMPL:
8151 return BIT_NOT_EXPR;
8153 default:
8154 return ERROR_MARK;
8158 /* Parse a new-expression.
8160 new-expression:
8161 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8162 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8164 Returns a representation of the expression. */
8166 static tree
8167 cp_parser_new_expression (cp_parser* parser)
8169 bool global_scope_p;
8170 vec<tree, va_gc> *placement;
8171 tree type;
8172 vec<tree, va_gc> *initializer;
8173 tree nelts = NULL_TREE;
8174 tree ret;
8176 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8178 /* Look for the optional `::' operator. */
8179 global_scope_p
8180 = (cp_parser_global_scope_opt (parser,
8181 /*current_scope_valid_p=*/false)
8182 != NULL_TREE);
8183 /* Look for the `new' operator. */
8184 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8185 /* There's no easy way to tell a new-placement from the
8186 `( type-id )' construct. */
8187 cp_parser_parse_tentatively (parser);
8188 /* Look for a new-placement. */
8189 placement = cp_parser_new_placement (parser);
8190 /* If that didn't work out, there's no new-placement. */
8191 if (!cp_parser_parse_definitely (parser))
8193 if (placement != NULL)
8194 release_tree_vector (placement);
8195 placement = NULL;
8198 /* If the next token is a `(', then we have a parenthesized
8199 type-id. */
8200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8202 cp_token *token;
8203 const char *saved_message = parser->type_definition_forbidden_message;
8205 /* Consume the `('. */
8206 cp_lexer_consume_token (parser->lexer);
8208 /* Parse the type-id. */
8209 parser->type_definition_forbidden_message
8210 = G_("types may not be defined in a new-expression");
8212 type_id_in_expr_sentinel s (parser);
8213 type = cp_parser_type_id (parser);
8215 parser->type_definition_forbidden_message = saved_message;
8217 /* Look for the closing `)'. */
8218 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8219 token = cp_lexer_peek_token (parser->lexer);
8220 /* There should not be a direct-new-declarator in this production,
8221 but GCC used to allowed this, so we check and emit a sensible error
8222 message for this case. */
8223 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8225 error_at (token->location,
8226 "array bound forbidden after parenthesized type-id");
8227 inform (token->location,
8228 "try removing the parentheses around the type-id");
8229 cp_parser_direct_new_declarator (parser);
8232 /* Otherwise, there must be a new-type-id. */
8233 else
8234 type = cp_parser_new_type_id (parser, &nelts);
8236 /* If the next token is a `(' or '{', then we have a new-initializer. */
8237 cp_token *token = cp_lexer_peek_token (parser->lexer);
8238 if (token->type == CPP_OPEN_PAREN
8239 || token->type == CPP_OPEN_BRACE)
8240 initializer = cp_parser_new_initializer (parser);
8241 else
8242 initializer = NULL;
8244 /* A new-expression may not appear in an integral constant
8245 expression. */
8246 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8247 ret = error_mark_node;
8248 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8249 of a new-type-id or type-id of a new-expression, the new-expression shall
8250 contain a new-initializer of the form ( assignment-expression )".
8251 Additionally, consistently with the spirit of DR 1467, we want to accept
8252 'new auto { 2 }' too. */
8253 else if (type_uses_auto (type)
8254 && (vec_safe_length (initializer) != 1
8255 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8256 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8258 error_at (token->location,
8259 "initialization of new-expression for type %<auto%> "
8260 "requires exactly one element");
8261 ret = error_mark_node;
8263 else
8265 /* Construct a location e.g.:
8266 ptr = new int[100]
8267 ^~~~~~~~~~~~
8268 with caret == start at the start of the "new" token, and the end
8269 at the end of the final token we consumed. */
8270 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8271 location_t end_loc = get_finish (end_tok->location);
8272 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8274 /* Create a representation of the new-expression. */
8275 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8276 tf_warning_or_error);
8277 protected_set_expr_location (ret, combined_loc);
8280 if (placement != NULL)
8281 release_tree_vector (placement);
8282 if (initializer != NULL)
8283 release_tree_vector (initializer);
8285 return ret;
8288 /* Parse a new-placement.
8290 new-placement:
8291 ( expression-list )
8293 Returns the same representation as for an expression-list. */
8295 static vec<tree, va_gc> *
8296 cp_parser_new_placement (cp_parser* parser)
8298 vec<tree, va_gc> *expression_list;
8300 /* Parse the expression-list. */
8301 expression_list = (cp_parser_parenthesized_expression_list
8302 (parser, non_attr, /*cast_p=*/false,
8303 /*allow_expansion_p=*/true,
8304 /*non_constant_p=*/NULL));
8306 if (expression_list && expression_list->is_empty ())
8307 error ("expected expression-list or type-id");
8309 return expression_list;
8312 /* Parse a new-type-id.
8314 new-type-id:
8315 type-specifier-seq new-declarator [opt]
8317 Returns the TYPE allocated. If the new-type-id indicates an array
8318 type, *NELTS is set to the number of elements in the last array
8319 bound; the TYPE will not include the last array bound. */
8321 static tree
8322 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8324 cp_decl_specifier_seq type_specifier_seq;
8325 cp_declarator *new_declarator;
8326 cp_declarator *declarator;
8327 cp_declarator *outer_declarator;
8328 const char *saved_message;
8330 /* The type-specifier sequence must not contain type definitions.
8331 (It cannot contain declarations of new types either, but if they
8332 are not definitions we will catch that because they are not
8333 complete.) */
8334 saved_message = parser->type_definition_forbidden_message;
8335 parser->type_definition_forbidden_message
8336 = G_("types may not be defined in a new-type-id");
8337 /* Parse the type-specifier-seq. */
8338 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8339 /*is_trailing_return=*/false,
8340 &type_specifier_seq);
8341 /* Restore the old message. */
8342 parser->type_definition_forbidden_message = saved_message;
8344 if (type_specifier_seq.type == error_mark_node)
8345 return error_mark_node;
8347 /* Parse the new-declarator. */
8348 new_declarator = cp_parser_new_declarator_opt (parser);
8350 /* Determine the number of elements in the last array dimension, if
8351 any. */
8352 *nelts = NULL_TREE;
8353 /* Skip down to the last array dimension. */
8354 declarator = new_declarator;
8355 outer_declarator = NULL;
8356 while (declarator && (declarator->kind == cdk_pointer
8357 || declarator->kind == cdk_ptrmem))
8359 outer_declarator = declarator;
8360 declarator = declarator->declarator;
8362 while (declarator
8363 && declarator->kind == cdk_array
8364 && declarator->declarator
8365 && declarator->declarator->kind == cdk_array)
8367 outer_declarator = declarator;
8368 declarator = declarator->declarator;
8371 if (declarator && declarator->kind == cdk_array)
8373 *nelts = declarator->u.array.bounds;
8374 if (*nelts == error_mark_node)
8375 *nelts = integer_one_node;
8377 if (outer_declarator)
8378 outer_declarator->declarator = declarator->declarator;
8379 else
8380 new_declarator = NULL;
8383 return groktypename (&type_specifier_seq, new_declarator, false);
8386 /* Parse an (optional) new-declarator.
8388 new-declarator:
8389 ptr-operator new-declarator [opt]
8390 direct-new-declarator
8392 Returns the declarator. */
8394 static cp_declarator *
8395 cp_parser_new_declarator_opt (cp_parser* parser)
8397 enum tree_code code;
8398 tree type, std_attributes = NULL_TREE;
8399 cp_cv_quals cv_quals;
8401 /* We don't know if there's a ptr-operator next, or not. */
8402 cp_parser_parse_tentatively (parser);
8403 /* Look for a ptr-operator. */
8404 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8405 /* If that worked, look for more new-declarators. */
8406 if (cp_parser_parse_definitely (parser))
8408 cp_declarator *declarator;
8410 /* Parse another optional declarator. */
8411 declarator = cp_parser_new_declarator_opt (parser);
8413 declarator = cp_parser_make_indirect_declarator
8414 (code, type, cv_quals, declarator, std_attributes);
8416 return declarator;
8419 /* If the next token is a `[', there is a direct-new-declarator. */
8420 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8421 return cp_parser_direct_new_declarator (parser);
8423 return NULL;
8426 /* Parse a direct-new-declarator.
8428 direct-new-declarator:
8429 [ expression ]
8430 direct-new-declarator [constant-expression]
8434 static cp_declarator *
8435 cp_parser_direct_new_declarator (cp_parser* parser)
8437 cp_declarator *declarator = NULL;
8439 while (true)
8441 tree expression;
8442 cp_token *token;
8444 /* Look for the opening `['. */
8445 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8447 token = cp_lexer_peek_token (parser->lexer);
8448 expression = cp_parser_expression (parser);
8449 /* The standard requires that the expression have integral
8450 type. DR 74 adds enumeration types. We believe that the
8451 real intent is that these expressions be handled like the
8452 expression in a `switch' condition, which also allows
8453 classes with a single conversion to integral or
8454 enumeration type. */
8455 if (!processing_template_decl)
8457 expression
8458 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8459 expression,
8460 /*complain=*/true);
8461 if (!expression)
8463 error_at (token->location,
8464 "expression in new-declarator must have integral "
8465 "or enumeration type");
8466 expression = error_mark_node;
8470 /* Look for the closing `]'. */
8471 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8473 /* Add this bound to the declarator. */
8474 declarator = make_array_declarator (declarator, expression);
8476 /* If the next token is not a `[', then there are no more
8477 bounds. */
8478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8479 break;
8482 return declarator;
8485 /* Parse a new-initializer.
8487 new-initializer:
8488 ( expression-list [opt] )
8489 braced-init-list
8491 Returns a representation of the expression-list. */
8493 static vec<tree, va_gc> *
8494 cp_parser_new_initializer (cp_parser* parser)
8496 vec<tree, va_gc> *expression_list;
8498 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8500 tree t;
8501 bool expr_non_constant_p;
8502 cp_lexer_set_source_position (parser->lexer);
8503 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8504 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8505 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8506 expression_list = make_tree_vector_single (t);
8508 else
8509 expression_list = (cp_parser_parenthesized_expression_list
8510 (parser, non_attr, /*cast_p=*/false,
8511 /*allow_expansion_p=*/true,
8512 /*non_constant_p=*/NULL));
8514 return expression_list;
8517 /* Parse a delete-expression.
8519 delete-expression:
8520 :: [opt] delete cast-expression
8521 :: [opt] delete [ ] cast-expression
8523 Returns a representation of the expression. */
8525 static tree
8526 cp_parser_delete_expression (cp_parser* parser)
8528 bool global_scope_p;
8529 bool array_p;
8530 tree expression;
8532 /* Look for the optional `::' operator. */
8533 global_scope_p
8534 = (cp_parser_global_scope_opt (parser,
8535 /*current_scope_valid_p=*/false)
8536 != NULL_TREE);
8537 /* Look for the `delete' keyword. */
8538 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8539 /* See if the array syntax is in use. */
8540 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8542 /* Consume the `[' token. */
8543 cp_lexer_consume_token (parser->lexer);
8544 /* Look for the `]' token. */
8545 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8546 /* Remember that this is the `[]' construct. */
8547 array_p = true;
8549 else
8550 array_p = false;
8552 /* Parse the cast-expression. */
8553 expression = cp_parser_simple_cast_expression (parser);
8555 /* A delete-expression may not appear in an integral constant
8556 expression. */
8557 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8558 return error_mark_node;
8560 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8561 tf_warning_or_error);
8564 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8565 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8566 0 otherwise. */
8568 static int
8569 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8571 cp_token *token = cp_lexer_peek_token (parser->lexer);
8572 switch (token->type)
8574 case CPP_COMMA:
8575 case CPP_SEMICOLON:
8576 case CPP_QUERY:
8577 case CPP_COLON:
8578 case CPP_CLOSE_SQUARE:
8579 case CPP_CLOSE_PAREN:
8580 case CPP_CLOSE_BRACE:
8581 case CPP_OPEN_BRACE:
8582 case CPP_DOT:
8583 case CPP_DOT_STAR:
8584 case CPP_DEREF:
8585 case CPP_DEREF_STAR:
8586 case CPP_DIV:
8587 case CPP_MOD:
8588 case CPP_LSHIFT:
8589 case CPP_RSHIFT:
8590 case CPP_LESS:
8591 case CPP_GREATER:
8592 case CPP_LESS_EQ:
8593 case CPP_GREATER_EQ:
8594 case CPP_EQ_EQ:
8595 case CPP_NOT_EQ:
8596 case CPP_EQ:
8597 case CPP_MULT_EQ:
8598 case CPP_DIV_EQ:
8599 case CPP_MOD_EQ:
8600 case CPP_PLUS_EQ:
8601 case CPP_MINUS_EQ:
8602 case CPP_RSHIFT_EQ:
8603 case CPP_LSHIFT_EQ:
8604 case CPP_AND_EQ:
8605 case CPP_XOR_EQ:
8606 case CPP_OR_EQ:
8607 case CPP_XOR:
8608 case CPP_OR:
8609 case CPP_OR_OR:
8610 case CPP_EOF:
8611 case CPP_ELLIPSIS:
8612 return 0;
8614 case CPP_OPEN_PAREN:
8615 /* In ((type ()) () the last () isn't a valid cast-expression,
8616 so the whole must be parsed as postfix-expression. */
8617 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8618 != CPP_CLOSE_PAREN;
8620 case CPP_OPEN_SQUARE:
8621 /* '[' may start a primary-expression in obj-c++ and in C++11,
8622 as a lambda-expression, eg, '(void)[]{}'. */
8623 if (cxx_dialect >= cxx11)
8624 return -1;
8625 return c_dialect_objc ();
8627 case CPP_PLUS_PLUS:
8628 case CPP_MINUS_MINUS:
8629 /* '++' and '--' may or may not start a cast-expression:
8631 struct T { void operator++(int); };
8632 void f() { (T())++; }
8636 int a;
8637 (int)++a; */
8638 return -1;
8640 default:
8641 return 1;
8645 /* Parse a cast-expression.
8647 cast-expression:
8648 unary-expression
8649 ( type-id ) cast-expression
8651 ADDRESS_P is true iff the unary-expression is appearing as the
8652 operand of the `&' operator. CAST_P is true if this expression is
8653 the target of a cast.
8655 Returns a representation of the expression. */
8657 static cp_expr
8658 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8659 bool decltype_p, cp_id_kind * pidk)
8661 /* If it's a `(', then we might be looking at a cast. */
8662 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8664 tree type = NULL_TREE;
8665 cp_expr expr (NULL_TREE);
8666 int cast_expression = 0;
8667 const char *saved_message;
8669 /* There's no way to know yet whether or not this is a cast.
8670 For example, `(int (3))' is a unary-expression, while `(int)
8671 3' is a cast. So, we resort to parsing tentatively. */
8672 cp_parser_parse_tentatively (parser);
8673 /* Types may not be defined in a cast. */
8674 saved_message = parser->type_definition_forbidden_message;
8675 parser->type_definition_forbidden_message
8676 = G_("types may not be defined in casts");
8677 /* Consume the `('. */
8678 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8679 location_t open_paren_loc = open_paren->location;
8681 /* A very tricky bit is that `(struct S) { 3 }' is a
8682 compound-literal (which we permit in C++ as an extension).
8683 But, that construct is not a cast-expression -- it is a
8684 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8685 is legal; if the compound-literal were a cast-expression,
8686 you'd need an extra set of parentheses.) But, if we parse
8687 the type-id, and it happens to be a class-specifier, then we
8688 will commit to the parse at that point, because we cannot
8689 undo the action that is done when creating a new class. So,
8690 then we cannot back up and do a postfix-expression.
8692 Another tricky case is the following (c++/29234):
8694 struct S { void operator () (); };
8696 void foo ()
8698 ( S()() );
8701 As a type-id we parse the parenthesized S()() as a function
8702 returning a function, groktypename complains and we cannot
8703 back up in this case either.
8705 Therefore, we scan ahead to the closing `)', and check to see
8706 if the tokens after the `)' can start a cast-expression. Otherwise
8707 we are dealing with an unary-expression, a postfix-expression
8708 or something else.
8710 Yet another tricky case, in C++11, is the following (c++/54891):
8712 (void)[]{};
8714 The issue is that usually, besides the case of lambda-expressions,
8715 the parenthesized type-id cannot be followed by '[', and, eg, we
8716 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8717 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8718 we don't commit, we try a cast-expression, then an unary-expression.
8720 Save tokens so that we can put them back. */
8721 cp_lexer_save_tokens (parser->lexer);
8723 /* We may be looking at a cast-expression. */
8724 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8725 /*consume_paren=*/true))
8726 cast_expression
8727 = cp_parser_tokens_start_cast_expression (parser);
8729 /* Roll back the tokens we skipped. */
8730 cp_lexer_rollback_tokens (parser->lexer);
8731 /* If we aren't looking at a cast-expression, simulate an error so
8732 that the call to cp_parser_error_occurred below returns true. */
8733 if (!cast_expression)
8734 cp_parser_simulate_error (parser);
8735 else
8737 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8738 parser->in_type_id_in_expr_p = true;
8739 /* Look for the type-id. */
8740 type = cp_parser_type_id (parser);
8741 /* Look for the closing `)'. */
8742 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8743 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8746 /* Restore the saved message. */
8747 parser->type_definition_forbidden_message = saved_message;
8749 /* At this point this can only be either a cast or a
8750 parenthesized ctor such as `(T ())' that looks like a cast to
8751 function returning T. */
8752 if (!cp_parser_error_occurred (parser))
8754 /* Only commit if the cast-expression doesn't start with
8755 '++', '--', or '[' in C++11. */
8756 if (cast_expression > 0)
8757 cp_parser_commit_to_topmost_tentative_parse (parser);
8759 expr = cp_parser_cast_expression (parser,
8760 /*address_p=*/false,
8761 /*cast_p=*/true,
8762 /*decltype_p=*/false,
8763 pidk);
8765 if (cp_parser_parse_definitely (parser))
8767 /* Warn about old-style casts, if so requested. */
8768 if (warn_old_style_cast
8769 && !in_system_header_at (input_location)
8770 && !VOID_TYPE_P (type)
8771 && current_lang_name != lang_name_c)
8772 warning (OPT_Wold_style_cast, "use of old-style cast");
8774 /* Only type conversions to integral or enumeration types
8775 can be used in constant-expressions. */
8776 if (!cast_valid_in_integral_constant_expression_p (type)
8777 && cp_parser_non_integral_constant_expression (parser,
8778 NIC_CAST))
8779 return error_mark_node;
8781 /* Perform the cast. */
8782 /* Make a location:
8783 (TYPE) EXPR
8784 ^~~~~~~~~~~
8785 with start==caret at the open paren, extending to the
8786 end of "expr". */
8787 location_t cast_loc = make_location (open_paren_loc,
8788 open_paren_loc,
8789 expr.get_finish ());
8790 expr = build_c_cast (cast_loc, type, expr);
8791 return expr;
8794 else
8795 cp_parser_abort_tentative_parse (parser);
8798 /* If we get here, then it's not a cast, so it must be a
8799 unary-expression. */
8800 return cp_parser_unary_expression (parser, pidk, address_p,
8801 cast_p, decltype_p);
8804 /* Parse a binary expression of the general form:
8806 pm-expression:
8807 cast-expression
8808 pm-expression .* cast-expression
8809 pm-expression ->* cast-expression
8811 multiplicative-expression:
8812 pm-expression
8813 multiplicative-expression * pm-expression
8814 multiplicative-expression / pm-expression
8815 multiplicative-expression % pm-expression
8817 additive-expression:
8818 multiplicative-expression
8819 additive-expression + multiplicative-expression
8820 additive-expression - multiplicative-expression
8822 shift-expression:
8823 additive-expression
8824 shift-expression << additive-expression
8825 shift-expression >> additive-expression
8827 relational-expression:
8828 shift-expression
8829 relational-expression < shift-expression
8830 relational-expression > shift-expression
8831 relational-expression <= shift-expression
8832 relational-expression >= shift-expression
8834 GNU Extension:
8836 relational-expression:
8837 relational-expression <? shift-expression
8838 relational-expression >? shift-expression
8840 equality-expression:
8841 relational-expression
8842 equality-expression == relational-expression
8843 equality-expression != relational-expression
8845 and-expression:
8846 equality-expression
8847 and-expression & equality-expression
8849 exclusive-or-expression:
8850 and-expression
8851 exclusive-or-expression ^ and-expression
8853 inclusive-or-expression:
8854 exclusive-or-expression
8855 inclusive-or-expression | exclusive-or-expression
8857 logical-and-expression:
8858 inclusive-or-expression
8859 logical-and-expression && inclusive-or-expression
8861 logical-or-expression:
8862 logical-and-expression
8863 logical-or-expression || logical-and-expression
8865 All these are implemented with a single function like:
8867 binary-expression:
8868 simple-cast-expression
8869 binary-expression <token> binary-expression
8871 CAST_P is true if this expression is the target of a cast.
8873 The binops_by_token map is used to get the tree codes for each <token> type.
8874 binary-expressions are associated according to a precedence table. */
8876 #define TOKEN_PRECEDENCE(token) \
8877 (((token->type == CPP_GREATER \
8878 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8879 && !parser->greater_than_is_operator_p) \
8880 ? PREC_NOT_OPERATOR \
8881 : binops_by_token[token->type].prec)
8883 static cp_expr
8884 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8885 bool no_toplevel_fold_p,
8886 bool decltype_p,
8887 enum cp_parser_prec prec,
8888 cp_id_kind * pidk)
8890 cp_parser_expression_stack stack;
8891 cp_parser_expression_stack_entry *sp = &stack[0];
8892 cp_parser_expression_stack_entry current;
8893 cp_expr rhs;
8894 cp_token *token;
8895 enum tree_code rhs_type;
8896 enum cp_parser_prec new_prec, lookahead_prec;
8897 tree overload;
8899 /* Parse the first expression. */
8900 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8901 ? TRUTH_NOT_EXPR : ERROR_MARK);
8902 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
8903 cast_p, decltype_p, pidk);
8904 current.prec = prec;
8906 if (cp_parser_error_occurred (parser))
8907 return error_mark_node;
8909 for (;;)
8911 /* Get an operator token. */
8912 token = cp_lexer_peek_token (parser->lexer);
8914 if (warn_cxx11_compat
8915 && token->type == CPP_RSHIFT
8916 && !parser->greater_than_is_operator_p)
8918 if (warning_at (token->location, OPT_Wc__11_compat,
8919 "%<>>%> operator is treated"
8920 " as two right angle brackets in C++11"))
8921 inform (token->location,
8922 "suggest parentheses around %<>>%> expression");
8925 new_prec = TOKEN_PRECEDENCE (token);
8926 if (new_prec != PREC_NOT_OPERATOR
8927 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8928 /* This is a fold-expression; handle it later. */
8929 new_prec = PREC_NOT_OPERATOR;
8931 /* Popping an entry off the stack means we completed a subexpression:
8932 - either we found a token which is not an operator (`>' where it is not
8933 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
8934 will happen repeatedly;
8935 - or, we found an operator which has lower priority. This is the case
8936 where the recursive descent *ascends*, as in `3 * 4 + 5' after
8937 parsing `3 * 4'. */
8938 if (new_prec <= current.prec)
8940 if (sp == stack)
8941 break;
8942 else
8943 goto pop;
8946 get_rhs:
8947 current.tree_type = binops_by_token[token->type].tree_type;
8948 current.loc = token->location;
8950 /* We used the operator token. */
8951 cp_lexer_consume_token (parser->lexer);
8953 /* For "false && x" or "true || x", x will never be executed;
8954 disable warnings while evaluating it. */
8955 if (current.tree_type == TRUTH_ANDIF_EXPR)
8956 c_inhibit_evaluation_warnings +=
8957 cp_fully_fold (current.lhs) == truthvalue_false_node;
8958 else if (current.tree_type == TRUTH_ORIF_EXPR)
8959 c_inhibit_evaluation_warnings +=
8960 cp_fully_fold (current.lhs) == truthvalue_true_node;
8962 /* Extract another operand. It may be the RHS of this expression
8963 or the LHS of a new, higher priority expression. */
8964 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8965 ? TRUTH_NOT_EXPR : ERROR_MARK);
8966 rhs = cp_parser_simple_cast_expression (parser);
8968 /* Get another operator token. Look up its precedence to avoid
8969 building a useless (immediately popped) stack entry for common
8970 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
8971 token = cp_lexer_peek_token (parser->lexer);
8972 lookahead_prec = TOKEN_PRECEDENCE (token);
8973 if (lookahead_prec != PREC_NOT_OPERATOR
8974 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
8975 lookahead_prec = PREC_NOT_OPERATOR;
8976 if (lookahead_prec > new_prec)
8978 /* ... and prepare to parse the RHS of the new, higher priority
8979 expression. Since precedence levels on the stack are
8980 monotonically increasing, we do not have to care about
8981 stack overflows. */
8982 *sp = current;
8983 ++sp;
8984 current.lhs = rhs;
8985 current.lhs_type = rhs_type;
8986 current.prec = new_prec;
8987 new_prec = lookahead_prec;
8988 goto get_rhs;
8990 pop:
8991 lookahead_prec = new_prec;
8992 /* If the stack is not empty, we have parsed into LHS the right side
8993 (`4' in the example above) of an expression we had suspended.
8994 We can use the information on the stack to recover the LHS (`3')
8995 from the stack together with the tree code (`MULT_EXPR'), and
8996 the precedence of the higher level subexpression
8997 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
8998 which will be used to actually build the additive expression. */
8999 rhs = current.lhs;
9000 rhs_type = current.lhs_type;
9001 --sp;
9002 current = *sp;
9005 /* Undo the disabling of warnings done above. */
9006 if (current.tree_type == TRUTH_ANDIF_EXPR)
9007 c_inhibit_evaluation_warnings -=
9008 cp_fully_fold (current.lhs) == truthvalue_false_node;
9009 else if (current.tree_type == TRUTH_ORIF_EXPR)
9010 c_inhibit_evaluation_warnings -=
9011 cp_fully_fold (current.lhs) == truthvalue_true_node;
9013 if (warn_logical_not_paren
9014 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9015 && current.lhs_type == TRUTH_NOT_EXPR
9016 /* Avoid warning for !!x == y. */
9017 && (TREE_CODE (current.lhs) != NE_EXPR
9018 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9019 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9020 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9021 /* Avoid warning for !b == y where b is boolean. */
9022 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9023 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9024 != BOOLEAN_TYPE))))
9025 /* Avoid warning for !!b == y where b is boolean. */
9026 && (!DECL_P (current.lhs)
9027 || TREE_TYPE (current.lhs) == NULL_TREE
9028 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9029 warn_logical_not_parentheses (current.loc, current.tree_type,
9030 current.lhs, maybe_constant_value (rhs));
9032 overload = NULL;
9034 location_t combined_loc = make_location (current.loc,
9035 current.lhs.get_start (),
9036 rhs.get_finish ());
9038 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9039 ERROR_MARK for everything that is not a binary expression.
9040 This makes warn_about_parentheses miss some warnings that
9041 involve unary operators. For unary expressions we should
9042 pass the correct tree_code unless the unary expression was
9043 surrounded by parentheses.
9045 if (no_toplevel_fold_p
9046 && lookahead_prec <= current.prec
9047 && sp == stack)
9048 current.lhs = build2_loc (combined_loc,
9049 current.tree_type,
9050 TREE_CODE_CLASS (current.tree_type)
9051 == tcc_comparison
9052 ? boolean_type_node : TREE_TYPE (current.lhs),
9053 current.lhs, rhs);
9054 else
9056 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9057 current.lhs, current.lhs_type,
9058 rhs, rhs_type, &overload,
9059 complain_flags (decltype_p));
9060 /* TODO: build_x_binary_op doesn't always honor the location. */
9061 current.lhs.set_location (combined_loc);
9063 current.lhs_type = current.tree_type;
9065 /* If the binary operator required the use of an overloaded operator,
9066 then this expression cannot be an integral constant-expression.
9067 An overloaded operator can be used even if both operands are
9068 otherwise permissible in an integral constant-expression if at
9069 least one of the operands is of enumeration type. */
9071 if (overload
9072 && cp_parser_non_integral_constant_expression (parser,
9073 NIC_OVERLOADED))
9074 return error_mark_node;
9077 return current.lhs;
9080 static cp_expr
9081 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9082 bool no_toplevel_fold_p,
9083 enum cp_parser_prec prec,
9084 cp_id_kind * pidk)
9086 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9087 /*decltype*/false, prec, pidk);
9090 /* Parse the `? expression : assignment-expression' part of a
9091 conditional-expression. The LOGICAL_OR_EXPR is the
9092 logical-or-expression that started the conditional-expression.
9093 Returns a representation of the entire conditional-expression.
9095 This routine is used by cp_parser_assignment_expression.
9097 ? expression : assignment-expression
9099 GNU Extensions:
9101 ? : assignment-expression */
9103 static tree
9104 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9106 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9107 cp_expr assignment_expr;
9108 struct cp_token *token;
9109 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9111 /* Consume the `?' token. */
9112 cp_lexer_consume_token (parser->lexer);
9113 token = cp_lexer_peek_token (parser->lexer);
9114 if (cp_parser_allow_gnu_extensions_p (parser)
9115 && token->type == CPP_COLON)
9117 pedwarn (token->location, OPT_Wpedantic,
9118 "ISO C++ does not allow ?: with omitted middle operand");
9119 /* Implicit true clause. */
9120 expr = NULL_TREE;
9121 c_inhibit_evaluation_warnings +=
9122 folded_logical_or_expr == truthvalue_true_node;
9123 warn_for_omitted_condop (token->location, logical_or_expr);
9125 else
9127 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9128 parser->colon_corrects_to_scope_p = false;
9129 /* Parse the expression. */
9130 c_inhibit_evaluation_warnings +=
9131 folded_logical_or_expr == truthvalue_false_node;
9132 expr = cp_parser_expression (parser);
9133 c_inhibit_evaluation_warnings +=
9134 ((folded_logical_or_expr == truthvalue_true_node)
9135 - (folded_logical_or_expr == truthvalue_false_node));
9136 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9139 /* The next token should be a `:'. */
9140 cp_parser_require (parser, CPP_COLON, RT_COLON);
9141 /* Parse the assignment-expression. */
9142 assignment_expr = cp_parser_assignment_expression (parser);
9143 c_inhibit_evaluation_warnings -=
9144 folded_logical_or_expr == truthvalue_true_node;
9146 /* Make a location:
9147 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9148 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9149 with the caret at the "?", ranging from the start of
9150 the logical_or_expr to the end of the assignment_expr. */
9151 loc = make_location (loc,
9152 logical_or_expr.get_start (),
9153 assignment_expr.get_finish ());
9155 /* Build the conditional-expression. */
9156 return build_x_conditional_expr (loc, logical_or_expr,
9157 expr,
9158 assignment_expr,
9159 tf_warning_or_error);
9162 /* Parse an assignment-expression.
9164 assignment-expression:
9165 conditional-expression
9166 logical-or-expression assignment-operator assignment_expression
9167 throw-expression
9169 CAST_P is true if this expression is the target of a cast.
9170 DECLTYPE_P is true if this expression is the operand of decltype.
9172 Returns a representation for the expression. */
9174 static cp_expr
9175 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9176 bool cast_p, bool decltype_p)
9178 cp_expr expr;
9180 /* If the next token is the `throw' keyword, then we're looking at
9181 a throw-expression. */
9182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9183 expr = cp_parser_throw_expression (parser);
9184 /* Otherwise, it must be that we are looking at a
9185 logical-or-expression. */
9186 else
9188 /* Parse the binary expressions (logical-or-expression). */
9189 expr = cp_parser_binary_expression (parser, cast_p, false,
9190 decltype_p,
9191 PREC_NOT_OPERATOR, pidk);
9192 /* If the next token is a `?' then we're actually looking at a
9193 conditional-expression. */
9194 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9195 return cp_parser_question_colon_clause (parser, expr);
9196 else
9198 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9200 /* If it's an assignment-operator, we're using the second
9201 production. */
9202 enum tree_code assignment_operator
9203 = cp_parser_assignment_operator_opt (parser);
9204 if (assignment_operator != ERROR_MARK)
9206 bool non_constant_p;
9208 /* Parse the right-hand side of the assignment. */
9209 cp_expr rhs = cp_parser_initializer_clause (parser,
9210 &non_constant_p);
9212 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9213 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9215 /* An assignment may not appear in a
9216 constant-expression. */
9217 if (cp_parser_non_integral_constant_expression (parser,
9218 NIC_ASSIGNMENT))
9219 return error_mark_node;
9220 /* Build the assignment expression. Its default
9221 location:
9222 LHS = RHS
9223 ~~~~^~~~~
9224 is the location of the '=' token as the
9225 caret, ranging from the start of the lhs to the
9226 end of the rhs. */
9227 loc = make_location (loc,
9228 expr.get_start (),
9229 rhs.get_finish ());
9230 expr = build_x_modify_expr (loc, expr,
9231 assignment_operator,
9232 rhs,
9233 complain_flags (decltype_p));
9234 /* TODO: build_x_modify_expr doesn't honor the location,
9235 so we must set it here. */
9236 expr.set_location (loc);
9241 return expr;
9244 /* Parse an (optional) assignment-operator.
9246 assignment-operator: one of
9247 = *= /= %= += -= >>= <<= &= ^= |=
9249 GNU Extension:
9251 assignment-operator: one of
9252 <?= >?=
9254 If the next token is an assignment operator, the corresponding tree
9255 code is returned, and the token is consumed. For example, for
9256 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9257 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9258 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9259 operator, ERROR_MARK is returned. */
9261 static enum tree_code
9262 cp_parser_assignment_operator_opt (cp_parser* parser)
9264 enum tree_code op;
9265 cp_token *token;
9267 /* Peek at the next token. */
9268 token = cp_lexer_peek_token (parser->lexer);
9270 switch (token->type)
9272 case CPP_EQ:
9273 op = NOP_EXPR;
9274 break;
9276 case CPP_MULT_EQ:
9277 op = MULT_EXPR;
9278 break;
9280 case CPP_DIV_EQ:
9281 op = TRUNC_DIV_EXPR;
9282 break;
9284 case CPP_MOD_EQ:
9285 op = TRUNC_MOD_EXPR;
9286 break;
9288 case CPP_PLUS_EQ:
9289 op = PLUS_EXPR;
9290 break;
9292 case CPP_MINUS_EQ:
9293 op = MINUS_EXPR;
9294 break;
9296 case CPP_RSHIFT_EQ:
9297 op = RSHIFT_EXPR;
9298 break;
9300 case CPP_LSHIFT_EQ:
9301 op = LSHIFT_EXPR;
9302 break;
9304 case CPP_AND_EQ:
9305 op = BIT_AND_EXPR;
9306 break;
9308 case CPP_XOR_EQ:
9309 op = BIT_XOR_EXPR;
9310 break;
9312 case CPP_OR_EQ:
9313 op = BIT_IOR_EXPR;
9314 break;
9316 default:
9317 /* Nothing else is an assignment operator. */
9318 op = ERROR_MARK;
9321 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9322 if (op != ERROR_MARK
9323 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9324 op = ERROR_MARK;
9326 /* If it was an assignment operator, consume it. */
9327 if (op != ERROR_MARK)
9328 cp_lexer_consume_token (parser->lexer);
9330 return op;
9333 /* Parse an expression.
9335 expression:
9336 assignment-expression
9337 expression , assignment-expression
9339 CAST_P is true if this expression is the target of a cast.
9340 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9341 except possibly parenthesized or on the RHS of a comma (N3276).
9343 Returns a representation of the expression. */
9345 static cp_expr
9346 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9347 bool cast_p, bool decltype_p)
9349 cp_expr expression = NULL_TREE;
9350 location_t loc = UNKNOWN_LOCATION;
9352 while (true)
9354 cp_expr assignment_expression;
9356 /* Parse the next assignment-expression. */
9357 assignment_expression
9358 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9360 /* We don't create a temporary for a call that is the immediate operand
9361 of decltype or on the RHS of a comma. But when we see a comma, we
9362 need to create a temporary for a call on the LHS. */
9363 if (decltype_p && !processing_template_decl
9364 && TREE_CODE (assignment_expression) == CALL_EXPR
9365 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9366 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9367 assignment_expression
9368 = build_cplus_new (TREE_TYPE (assignment_expression),
9369 assignment_expression, tf_warning_or_error);
9371 /* If this is the first assignment-expression, we can just
9372 save it away. */
9373 if (!expression)
9374 expression = assignment_expression;
9375 else
9377 /* Create a location with caret at the comma, ranging
9378 from the start of the LHS to the end of the RHS. */
9379 loc = make_location (loc,
9380 expression.get_start (),
9381 assignment_expression.get_finish ());
9382 expression = build_x_compound_expr (loc, expression,
9383 assignment_expression,
9384 complain_flags (decltype_p));
9385 expression.set_location (loc);
9387 /* If the next token is not a comma, or we're in a fold-expression, then
9388 we are done with the expression. */
9389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9390 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9391 break;
9392 /* Consume the `,'. */
9393 loc = cp_lexer_peek_token (parser->lexer)->location;
9394 cp_lexer_consume_token (parser->lexer);
9395 /* A comma operator cannot appear in a constant-expression. */
9396 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9397 expression = error_mark_node;
9400 return expression;
9403 /* Parse a constant-expression.
9405 constant-expression:
9406 conditional-expression
9408 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9409 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9410 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9411 is false, NON_CONSTANT_P should be NULL. */
9413 static cp_expr
9414 cp_parser_constant_expression (cp_parser* parser,
9415 bool allow_non_constant_p,
9416 bool *non_constant_p)
9418 bool saved_integral_constant_expression_p;
9419 bool saved_allow_non_integral_constant_expression_p;
9420 bool saved_non_integral_constant_expression_p;
9421 cp_expr expression;
9423 /* It might seem that we could simply parse the
9424 conditional-expression, and then check to see if it were
9425 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9426 one that the compiler can figure out is constant, possibly after
9427 doing some simplifications or optimizations. The standard has a
9428 precise definition of constant-expression, and we must honor
9429 that, even though it is somewhat more restrictive.
9431 For example:
9433 int i[(2, 3)];
9435 is not a legal declaration, because `(2, 3)' is not a
9436 constant-expression. The `,' operator is forbidden in a
9437 constant-expression. However, GCC's constant-folding machinery
9438 will fold this operation to an INTEGER_CST for `3'. */
9440 /* Save the old settings. */
9441 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9442 saved_allow_non_integral_constant_expression_p
9443 = parser->allow_non_integral_constant_expression_p;
9444 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9445 /* We are now parsing a constant-expression. */
9446 parser->integral_constant_expression_p = true;
9447 parser->allow_non_integral_constant_expression_p
9448 = (allow_non_constant_p || cxx_dialect >= cxx11);
9449 parser->non_integral_constant_expression_p = false;
9450 /* Although the grammar says "conditional-expression", we parse an
9451 "assignment-expression", which also permits "throw-expression"
9452 and the use of assignment operators. In the case that
9453 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9454 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9455 actually essential that we look for an assignment-expression.
9456 For example, cp_parser_initializer_clauses uses this function to
9457 determine whether a particular assignment-expression is in fact
9458 constant. */
9459 expression = cp_parser_assignment_expression (parser);
9460 /* Restore the old settings. */
9461 parser->integral_constant_expression_p
9462 = saved_integral_constant_expression_p;
9463 parser->allow_non_integral_constant_expression_p
9464 = saved_allow_non_integral_constant_expression_p;
9465 if (cxx_dialect >= cxx11)
9467 /* Require an rvalue constant expression here; that's what our
9468 callers expect. Reference constant expressions are handled
9469 separately in e.g. cp_parser_template_argument. */
9470 bool is_const = potential_rvalue_constant_expression (expression);
9471 parser->non_integral_constant_expression_p = !is_const;
9472 if (!is_const && !allow_non_constant_p)
9473 require_potential_rvalue_constant_expression (expression);
9475 if (allow_non_constant_p)
9476 *non_constant_p = parser->non_integral_constant_expression_p;
9477 parser->non_integral_constant_expression_p
9478 = saved_non_integral_constant_expression_p;
9480 return expression;
9483 /* Parse __builtin_offsetof.
9485 offsetof-expression:
9486 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9488 offsetof-member-designator:
9489 id-expression
9490 | offsetof-member-designator "." id-expression
9491 | offsetof-member-designator "[" expression "]"
9492 | offsetof-member-designator "->" id-expression */
9494 static cp_expr
9495 cp_parser_builtin_offsetof (cp_parser *parser)
9497 int save_ice_p, save_non_ice_p;
9498 tree type;
9499 cp_expr expr;
9500 cp_id_kind dummy;
9501 cp_token *token;
9502 location_t finish_loc;
9504 /* We're about to accept non-integral-constant things, but will
9505 definitely yield an integral constant expression. Save and
9506 restore these values around our local parsing. */
9507 save_ice_p = parser->integral_constant_expression_p;
9508 save_non_ice_p = parser->non_integral_constant_expression_p;
9510 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9512 /* Consume the "__builtin_offsetof" token. */
9513 cp_lexer_consume_token (parser->lexer);
9514 /* Consume the opening `('. */
9515 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9516 /* Parse the type-id. */
9517 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9518 type = cp_parser_type_id (parser);
9519 /* Look for the `,'. */
9520 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9521 token = cp_lexer_peek_token (parser->lexer);
9523 /* Build the (type *)null that begins the traditional offsetof macro. */
9524 tree object_ptr
9525 = build_static_cast (build_pointer_type (type), null_pointer_node,
9526 tf_warning_or_error);
9528 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9529 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9530 true, &dummy, token->location);
9531 while (true)
9533 token = cp_lexer_peek_token (parser->lexer);
9534 switch (token->type)
9536 case CPP_OPEN_SQUARE:
9537 /* offsetof-member-designator "[" expression "]" */
9538 expr = cp_parser_postfix_open_square_expression (parser, expr,
9539 true, false);
9540 break;
9542 case CPP_DEREF:
9543 /* offsetof-member-designator "->" identifier */
9544 expr = grok_array_decl (token->location, expr,
9545 integer_zero_node, false);
9546 /* FALLTHRU */
9548 case CPP_DOT:
9549 /* offsetof-member-designator "." identifier */
9550 cp_lexer_consume_token (parser->lexer);
9551 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9552 expr, true, &dummy,
9553 token->location);
9554 break;
9556 case CPP_CLOSE_PAREN:
9557 /* Consume the ")" token. */
9558 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9559 cp_lexer_consume_token (parser->lexer);
9560 goto success;
9562 default:
9563 /* Error. We know the following require will fail, but
9564 that gives the proper error message. */
9565 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9566 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9567 expr = error_mark_node;
9568 goto failure;
9572 success:
9573 /* Make a location of the form:
9574 __builtin_offsetof (struct s, f)
9575 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9576 with caret at the type-id, ranging from the start of the
9577 "_builtin_offsetof" token to the close paren. */
9578 loc = make_location (loc, start_loc, finish_loc);
9579 /* The result will be an INTEGER_CST, so we need to explicitly
9580 preserve the location. */
9581 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9583 failure:
9584 parser->integral_constant_expression_p = save_ice_p;
9585 parser->non_integral_constant_expression_p = save_non_ice_p;
9587 return expr;
9590 /* Parse a trait expression.
9592 Returns a representation of the expression, the underlying type
9593 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9595 static tree
9596 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9598 cp_trait_kind kind;
9599 tree type1, type2 = NULL_TREE;
9600 bool binary = false;
9601 bool variadic = false;
9603 switch (keyword)
9605 case RID_HAS_NOTHROW_ASSIGN:
9606 kind = CPTK_HAS_NOTHROW_ASSIGN;
9607 break;
9608 case RID_HAS_NOTHROW_CONSTRUCTOR:
9609 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9610 break;
9611 case RID_HAS_NOTHROW_COPY:
9612 kind = CPTK_HAS_NOTHROW_COPY;
9613 break;
9614 case RID_HAS_TRIVIAL_ASSIGN:
9615 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9616 break;
9617 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9618 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9619 break;
9620 case RID_HAS_TRIVIAL_COPY:
9621 kind = CPTK_HAS_TRIVIAL_COPY;
9622 break;
9623 case RID_HAS_TRIVIAL_DESTRUCTOR:
9624 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9625 break;
9626 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9627 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9628 break;
9629 case RID_HAS_VIRTUAL_DESTRUCTOR:
9630 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9631 break;
9632 case RID_IS_ABSTRACT:
9633 kind = CPTK_IS_ABSTRACT;
9634 break;
9635 case RID_IS_BASE_OF:
9636 kind = CPTK_IS_BASE_OF;
9637 binary = true;
9638 break;
9639 case RID_IS_CLASS:
9640 kind = CPTK_IS_CLASS;
9641 break;
9642 case RID_IS_EMPTY:
9643 kind = CPTK_IS_EMPTY;
9644 break;
9645 case RID_IS_ENUM:
9646 kind = CPTK_IS_ENUM;
9647 break;
9648 case RID_IS_FINAL:
9649 kind = CPTK_IS_FINAL;
9650 break;
9651 case RID_IS_LITERAL_TYPE:
9652 kind = CPTK_IS_LITERAL_TYPE;
9653 break;
9654 case RID_IS_POD:
9655 kind = CPTK_IS_POD;
9656 break;
9657 case RID_IS_POLYMORPHIC:
9658 kind = CPTK_IS_POLYMORPHIC;
9659 break;
9660 case RID_IS_SAME_AS:
9661 kind = CPTK_IS_SAME_AS;
9662 binary = true;
9663 break;
9664 case RID_IS_STD_LAYOUT:
9665 kind = CPTK_IS_STD_LAYOUT;
9666 break;
9667 case RID_IS_TRIVIAL:
9668 kind = CPTK_IS_TRIVIAL;
9669 break;
9670 case RID_IS_TRIVIALLY_ASSIGNABLE:
9671 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9672 binary = true;
9673 break;
9674 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9675 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9676 variadic = true;
9677 break;
9678 case RID_IS_TRIVIALLY_COPYABLE:
9679 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9680 break;
9681 case RID_IS_UNION:
9682 kind = CPTK_IS_UNION;
9683 break;
9684 case RID_UNDERLYING_TYPE:
9685 kind = CPTK_UNDERLYING_TYPE;
9686 break;
9687 case RID_BASES:
9688 kind = CPTK_BASES;
9689 break;
9690 case RID_DIRECT_BASES:
9691 kind = CPTK_DIRECT_BASES;
9692 break;
9693 default:
9694 gcc_unreachable ();
9697 /* Consume the token. */
9698 cp_lexer_consume_token (parser->lexer);
9700 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9703 type_id_in_expr_sentinel s (parser);
9704 type1 = cp_parser_type_id (parser);
9707 if (type1 == error_mark_node)
9708 return error_mark_node;
9710 if (binary)
9712 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9715 type_id_in_expr_sentinel s (parser);
9716 type2 = cp_parser_type_id (parser);
9719 if (type2 == error_mark_node)
9720 return error_mark_node;
9722 else if (variadic)
9724 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9726 cp_lexer_consume_token (parser->lexer);
9727 tree elt = cp_parser_type_id (parser);
9728 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9730 cp_lexer_consume_token (parser->lexer);
9731 elt = make_pack_expansion (elt);
9733 if (elt == error_mark_node)
9734 return error_mark_node;
9735 type2 = tree_cons (NULL_TREE, elt, type2);
9739 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9741 /* Complete the trait expression, which may mean either processing
9742 the trait expr now or saving it for template instantiation. */
9743 switch (kind)
9745 case CPTK_UNDERLYING_TYPE:
9746 return finish_underlying_type (type1);
9747 case CPTK_BASES:
9748 return finish_bases (type1, false);
9749 case CPTK_DIRECT_BASES:
9750 return finish_bases (type1, true);
9751 default:
9752 return finish_trait_expr (kind, type1, type2);
9756 /* Lambdas that appear in variable initializer or default argument scope
9757 get that in their mangling, so we need to record it. We might as well
9758 use the count for function and namespace scopes as well. */
9759 static GTY(()) tree lambda_scope;
9760 static GTY(()) int lambda_count;
9761 struct GTY(()) tree_int
9763 tree t;
9764 int i;
9766 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9768 static void
9769 start_lambda_scope (tree decl)
9771 tree_int ti;
9772 gcc_assert (decl);
9773 /* Once we're inside a function, we ignore other scopes and just push
9774 the function again so that popping works properly. */
9775 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9776 decl = current_function_decl;
9777 ti.t = lambda_scope;
9778 ti.i = lambda_count;
9779 vec_safe_push (lambda_scope_stack, ti);
9780 if (lambda_scope != decl)
9782 /* Don't reset the count if we're still in the same function. */
9783 lambda_scope = decl;
9784 lambda_count = 0;
9788 static void
9789 record_lambda_scope (tree lambda)
9791 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9792 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9795 static void
9796 finish_lambda_scope (void)
9798 tree_int *p = &lambda_scope_stack->last ();
9799 if (lambda_scope != p->t)
9801 lambda_scope = p->t;
9802 lambda_count = p->i;
9804 lambda_scope_stack->pop ();
9807 /* Parse a lambda expression.
9809 lambda-expression:
9810 lambda-introducer lambda-declarator [opt] compound-statement
9812 Returns a representation of the expression. */
9814 static cp_expr
9815 cp_parser_lambda_expression (cp_parser* parser)
9817 tree lambda_expr = build_lambda_expr ();
9818 tree type;
9819 bool ok = true;
9820 cp_token *token = cp_lexer_peek_token (parser->lexer);
9821 cp_token_position start = 0;
9823 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9825 if (cp_unevaluated_operand)
9827 if (!token->error_reported)
9829 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9830 "lambda-expression in unevaluated context");
9831 token->error_reported = true;
9833 ok = false;
9835 else if (parser->in_template_argument_list_p)
9837 if (!token->error_reported)
9839 error_at (token->location, "lambda-expression in template-argument");
9840 token->error_reported = true;
9842 ok = false;
9845 /* We may be in the middle of deferred access check. Disable
9846 it now. */
9847 push_deferring_access_checks (dk_no_deferred);
9849 cp_parser_lambda_introducer (parser, lambda_expr);
9851 type = begin_lambda_type (lambda_expr);
9852 if (type == error_mark_node)
9853 return error_mark_node;
9855 record_lambda_scope (lambda_expr);
9857 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9858 determine_visibility (TYPE_NAME (type));
9860 /* Now that we've started the type, add the capture fields for any
9861 explicit captures. */
9862 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9865 /* Inside the class, surrounding template-parameter-lists do not apply. */
9866 unsigned int saved_num_template_parameter_lists
9867 = parser->num_template_parameter_lists;
9868 unsigned char in_statement = parser->in_statement;
9869 bool in_switch_statement_p = parser->in_switch_statement_p;
9870 bool fully_implicit_function_template_p
9871 = parser->fully_implicit_function_template_p;
9872 tree implicit_template_parms = parser->implicit_template_parms;
9873 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9874 bool auto_is_implicit_function_template_parm_p
9875 = parser->auto_is_implicit_function_template_parm_p;
9877 parser->num_template_parameter_lists = 0;
9878 parser->in_statement = 0;
9879 parser->in_switch_statement_p = false;
9880 parser->fully_implicit_function_template_p = false;
9881 parser->implicit_template_parms = 0;
9882 parser->implicit_template_scope = 0;
9883 parser->auto_is_implicit_function_template_parm_p = false;
9885 /* By virtue of defining a local class, a lambda expression has access to
9886 the private variables of enclosing classes. */
9888 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
9890 if (ok && cp_parser_error_occurred (parser))
9891 ok = false;
9893 if (ok)
9895 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
9896 && cp_parser_start_tentative_firewall (parser))
9897 start = token;
9898 cp_parser_lambda_body (parser, lambda_expr);
9900 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
9902 if (cp_parser_skip_to_closing_brace (parser))
9903 cp_lexer_consume_token (parser->lexer);
9906 /* The capture list was built up in reverse order; fix that now. */
9907 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
9908 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9910 if (ok)
9911 maybe_add_lambda_conv_op (type);
9913 type = finish_struct (type, /*attributes=*/NULL_TREE);
9915 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
9916 parser->in_statement = in_statement;
9917 parser->in_switch_statement_p = in_switch_statement_p;
9918 parser->fully_implicit_function_template_p
9919 = fully_implicit_function_template_p;
9920 parser->implicit_template_parms = implicit_template_parms;
9921 parser->implicit_template_scope = implicit_template_scope;
9922 parser->auto_is_implicit_function_template_parm_p
9923 = auto_is_implicit_function_template_parm_p;
9926 /* This field is only used during parsing of the lambda. */
9927 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
9929 /* This lambda shouldn't have any proxies left at this point. */
9930 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
9931 /* And now that we're done, push proxies for an enclosing lambda. */
9932 insert_pending_capture_proxies ();
9934 if (ok)
9935 lambda_expr = build_lambda_object (lambda_expr);
9936 else
9937 lambda_expr = error_mark_node;
9939 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
9941 pop_deferring_access_checks ();
9943 return lambda_expr;
9946 /* Parse the beginning of a lambda expression.
9948 lambda-introducer:
9949 [ lambda-capture [opt] ]
9951 LAMBDA_EXPR is the current representation of the lambda expression. */
9953 static void
9954 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
9956 /* Need commas after the first capture. */
9957 bool first = true;
9959 /* Eat the leading `['. */
9960 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
9962 /* Record default capture mode. "[&" "[=" "[&," "[=," */
9963 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
9964 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
9965 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
9966 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9967 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
9969 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
9971 cp_lexer_consume_token (parser->lexer);
9972 first = false;
9975 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
9977 cp_token* capture_token;
9978 tree capture_id;
9979 tree capture_init_expr;
9980 cp_id_kind idk = CP_ID_KIND_NONE;
9981 bool explicit_init_p = false;
9983 enum capture_kind_type
9985 BY_COPY,
9986 BY_REFERENCE
9988 enum capture_kind_type capture_kind = BY_COPY;
9990 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
9992 error ("expected end of capture-list");
9993 return;
9996 if (first)
9997 first = false;
9998 else
9999 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10001 /* Possibly capture `this'. */
10002 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10004 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10005 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10006 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10007 "with by-copy capture default");
10008 cp_lexer_consume_token (parser->lexer);
10009 add_capture (lambda_expr,
10010 /*id=*/this_identifier,
10011 /*initializer=*/finish_this_expr (),
10012 /*by_reference_p=*/true,
10013 explicit_init_p);
10014 continue;
10017 /* Possibly capture `*this'. */
10018 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10019 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10021 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10022 if (cxx_dialect < cxx1z)
10023 pedwarn (loc, 0, "%<*this%> capture only available with "
10024 "-std=c++1z or -std=gnu++1z");
10025 cp_lexer_consume_token (parser->lexer);
10026 cp_lexer_consume_token (parser->lexer);
10027 add_capture (lambda_expr,
10028 /*id=*/this_identifier,
10029 /*initializer=*/finish_this_expr (),
10030 /*by_reference_p=*/false,
10031 explicit_init_p);
10032 continue;
10035 /* Remember whether we want to capture as a reference or not. */
10036 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10038 capture_kind = BY_REFERENCE;
10039 cp_lexer_consume_token (parser->lexer);
10042 /* Get the identifier. */
10043 capture_token = cp_lexer_peek_token (parser->lexer);
10044 capture_id = cp_parser_identifier (parser);
10046 if (capture_id == error_mark_node)
10047 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10048 delimiters, but I modified this to stop on unnested ']' as well. It
10049 was already changed to stop on unnested '}', so the
10050 "closing_parenthesis" name is no more misleading with my change. */
10052 cp_parser_skip_to_closing_parenthesis (parser,
10053 /*recovering=*/true,
10054 /*or_comma=*/true,
10055 /*consume_paren=*/true);
10056 break;
10059 /* Find the initializer for this capture. */
10060 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10061 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10062 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10064 bool direct, non_constant;
10065 /* An explicit initializer exists. */
10066 if (cxx_dialect < cxx14)
10067 pedwarn (input_location, 0,
10068 "lambda capture initializers "
10069 "only available with -std=c++14 or -std=gnu++14");
10070 capture_init_expr = cp_parser_initializer (parser, &direct,
10071 &non_constant);
10072 explicit_init_p = true;
10073 if (capture_init_expr == NULL_TREE)
10075 error ("empty initializer for lambda init-capture");
10076 capture_init_expr = error_mark_node;
10079 else
10081 const char* error_msg;
10083 /* Turn the identifier into an id-expression. */
10084 capture_init_expr
10085 = cp_parser_lookup_name_simple (parser, capture_id,
10086 capture_token->location);
10088 if (capture_init_expr == error_mark_node)
10090 unqualified_name_lookup_error (capture_id);
10091 continue;
10093 else if (DECL_P (capture_init_expr)
10094 && (!VAR_P (capture_init_expr)
10095 && TREE_CODE (capture_init_expr) != PARM_DECL))
10097 error_at (capture_token->location,
10098 "capture of non-variable %qD ",
10099 capture_init_expr);
10100 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10101 "%q#D declared here", capture_init_expr);
10102 continue;
10104 if (VAR_P (capture_init_expr)
10105 && decl_storage_duration (capture_init_expr) != dk_auto)
10107 if (pedwarn (capture_token->location, 0, "capture of variable "
10108 "%qD with non-automatic storage duration",
10109 capture_init_expr))
10110 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10111 "%q#D declared here", capture_init_expr);
10112 continue;
10115 capture_init_expr
10116 = finish_id_expression
10117 (capture_id,
10118 capture_init_expr,
10119 parser->scope,
10120 &idk,
10121 /*integral_constant_expression_p=*/false,
10122 /*allow_non_integral_constant_expression_p=*/false,
10123 /*non_integral_constant_expression_p=*/NULL,
10124 /*template_p=*/false,
10125 /*done=*/true,
10126 /*address_p=*/false,
10127 /*template_arg_p=*/false,
10128 &error_msg,
10129 capture_token->location);
10131 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10133 cp_lexer_consume_token (parser->lexer);
10134 capture_init_expr = make_pack_expansion (capture_init_expr);
10136 else
10137 check_for_bare_parameter_packs (capture_init_expr);
10140 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10141 && !explicit_init_p)
10143 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10144 && capture_kind == BY_COPY)
10145 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10146 "of %qD redundant with by-copy capture default",
10147 capture_id);
10148 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10149 && capture_kind == BY_REFERENCE)
10150 pedwarn (capture_token->location, 0, "explicit by-reference "
10151 "capture of %qD redundant with by-reference capture "
10152 "default", capture_id);
10155 add_capture (lambda_expr,
10156 capture_id,
10157 capture_init_expr,
10158 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10159 explicit_init_p);
10162 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10165 /* Parse the (optional) middle of a lambda expression.
10167 lambda-declarator:
10168 < template-parameter-list [opt] >
10169 ( parameter-declaration-clause [opt] )
10170 attribute-specifier [opt]
10171 decl-specifier-seq [opt]
10172 exception-specification [opt]
10173 lambda-return-type-clause [opt]
10175 LAMBDA_EXPR is the current representation of the lambda expression. */
10177 static bool
10178 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10180 /* 5.1.1.4 of the standard says:
10181 If a lambda-expression does not include a lambda-declarator, it is as if
10182 the lambda-declarator were ().
10183 This means an empty parameter list, no attributes, and no exception
10184 specification. */
10185 tree param_list = void_list_node;
10186 tree attributes = NULL_TREE;
10187 tree exception_spec = NULL_TREE;
10188 tree template_param_list = NULL_TREE;
10189 tree tx_qual = NULL_TREE;
10190 cp_decl_specifier_seq lambda_specs;
10191 clear_decl_specs (&lambda_specs);
10193 /* The template-parameter-list is optional, but must begin with
10194 an opening angle if present. */
10195 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10197 if (cxx_dialect < cxx14)
10198 pedwarn (parser->lexer->next_token->location, 0,
10199 "lambda templates are only available with "
10200 "-std=c++14 or -std=gnu++14");
10201 else
10202 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10203 "ISO C++ does not support lambda templates");
10205 cp_lexer_consume_token (parser->lexer);
10207 template_param_list = cp_parser_template_parameter_list (parser);
10209 cp_parser_skip_to_end_of_template_parameter_list (parser);
10211 /* We just processed one more parameter list. */
10212 ++parser->num_template_parameter_lists;
10215 /* The parameter-declaration-clause is optional (unless
10216 template-parameter-list was given), but must begin with an
10217 opening parenthesis if present. */
10218 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10220 cp_lexer_consume_token (parser->lexer);
10222 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10224 /* Parse parameters. */
10225 param_list = cp_parser_parameter_declaration_clause (parser);
10227 /* Default arguments shall not be specified in the
10228 parameter-declaration-clause of a lambda-declarator. */
10229 if (cxx_dialect < cxx14)
10230 for (tree t = param_list; t; t = TREE_CHAIN (t))
10231 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10232 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10233 "default argument specified for lambda parameter");
10235 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10237 attributes = cp_parser_attributes_opt (parser);
10239 /* In the decl-specifier-seq of the lambda-declarator, each
10240 decl-specifier shall either be mutable or constexpr. */
10241 int declares_class_or_enum;
10242 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10243 cp_parser_decl_specifier_seq (parser,
10244 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10245 &lambda_specs, &declares_class_or_enum);
10246 if (lambda_specs.storage_class == sc_mutable)
10248 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10249 if (lambda_specs.conflicting_specifiers_p)
10250 error_at (lambda_specs.locations[ds_storage_class],
10251 "duplicate %<mutable%>");
10254 tx_qual = cp_parser_tx_qualifier_opt (parser);
10256 /* Parse optional exception specification. */
10257 exception_spec = cp_parser_exception_specification_opt (parser);
10259 /* Parse optional trailing return type. */
10260 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10262 cp_lexer_consume_token (parser->lexer);
10263 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10264 = cp_parser_trailing_type_id (parser);
10267 /* The function parameters must be in scope all the way until after the
10268 trailing-return-type in case of decltype. */
10269 pop_bindings_and_leave_scope ();
10271 else if (template_param_list != NULL_TREE) // generate diagnostic
10272 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10274 /* Create the function call operator.
10276 Messing with declarators like this is no uglier than building up the
10277 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10278 other code. */
10280 cp_decl_specifier_seq return_type_specs;
10281 cp_declarator* declarator;
10282 tree fco;
10283 int quals;
10284 void *p;
10286 clear_decl_specs (&return_type_specs);
10287 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10288 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10289 else
10290 /* Maybe we will deduce the return type later. */
10291 return_type_specs.type = make_auto ();
10293 if (lambda_specs.locations[ds_constexpr])
10295 if (cxx_dialect >= cxx1z)
10296 return_type_specs.locations[ds_constexpr]
10297 = lambda_specs.locations[ds_constexpr];
10298 else
10299 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10300 "lambda only available with -std=c++1z or -std=gnu++1z");
10303 p = obstack_alloc (&declarator_obstack, 0);
10305 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10306 sfk_none);
10308 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10309 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10310 declarator = make_call_declarator (declarator, param_list, quals,
10311 VIRT_SPEC_UNSPECIFIED,
10312 REF_QUAL_NONE,
10313 tx_qual,
10314 exception_spec,
10315 /*late_return_type=*/NULL_TREE,
10316 /*requires_clause*/NULL_TREE);
10317 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10319 fco = grokmethod (&return_type_specs,
10320 declarator,
10321 attributes);
10322 if (fco != error_mark_node)
10324 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10325 DECL_ARTIFICIAL (fco) = 1;
10326 /* Give the object parameter a different name. */
10327 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10328 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10329 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10331 if (template_param_list)
10333 fco = finish_member_template_decl (fco);
10334 finish_template_decl (template_param_list);
10335 --parser->num_template_parameter_lists;
10337 else if (parser->fully_implicit_function_template_p)
10338 fco = finish_fully_implicit_template (parser, fco);
10340 finish_member_declaration (fco);
10342 obstack_free (&declarator_obstack, p);
10344 return (fco != error_mark_node);
10348 /* Parse the body of a lambda expression, which is simply
10350 compound-statement
10352 but which requires special handling.
10353 LAMBDA_EXPR is the current representation of the lambda expression. */
10355 static void
10356 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10358 bool nested = (current_function_decl != NULL_TREE);
10359 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10360 if (nested)
10361 push_function_context ();
10362 else
10363 /* Still increment function_depth so that we don't GC in the
10364 middle of an expression. */
10365 ++function_depth;
10366 vec<tree> omp_privatization_save;
10367 save_omp_privatization_clauses (omp_privatization_save);
10368 /* Clear this in case we're in the middle of a default argument. */
10369 parser->local_variables_forbidden_p = false;
10371 /* Finish the function call operator
10372 - class_specifier
10373 + late_parsing_for_member
10374 + function_definition_after_declarator
10375 + ctor_initializer_opt_and_function_body */
10377 tree fco = lambda_function (lambda_expr);
10378 tree body;
10379 bool done = false;
10380 tree compound_stmt;
10381 tree cap;
10383 /* Let the front end know that we are going to be defining this
10384 function. */
10385 start_preparsed_function (fco,
10386 NULL_TREE,
10387 SF_PRE_PARSED | SF_INCLASS_INLINE);
10389 start_lambda_scope (fco);
10390 body = begin_function_body ();
10392 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10393 goto out;
10395 /* Push the proxies for any explicit captures. */
10396 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10397 cap = TREE_CHAIN (cap))
10398 build_capture_proxy (TREE_PURPOSE (cap));
10400 compound_stmt = begin_compound_stmt (0);
10402 /* 5.1.1.4 of the standard says:
10403 If a lambda-expression does not include a trailing-return-type, it
10404 is as if the trailing-return-type denotes the following type:
10405 * if the compound-statement is of the form
10406 { return attribute-specifier [opt] expression ; }
10407 the type of the returned expression after lvalue-to-rvalue
10408 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10409 (_conv.array_ 4.2), and function-to-pointer conversion
10410 (_conv.func_ 4.3);
10411 * otherwise, void. */
10413 /* In a lambda that has neither a lambda-return-type-clause
10414 nor a deducible form, errors should be reported for return statements
10415 in the body. Since we used void as the placeholder return type, parsing
10416 the body as usual will give such desired behavior. */
10417 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10418 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10419 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10421 tree expr = NULL_TREE;
10422 cp_id_kind idk = CP_ID_KIND_NONE;
10424 /* Parse tentatively in case there's more after the initial return
10425 statement. */
10426 cp_parser_parse_tentatively (parser);
10428 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10430 expr = cp_parser_expression (parser, &idk);
10432 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10433 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10435 if (cp_parser_parse_definitely (parser))
10437 if (!processing_template_decl)
10439 tree type = lambda_return_type (expr);
10440 apply_deduced_return_type (fco, type);
10441 if (type == error_mark_node)
10442 expr = error_mark_node;
10445 /* Will get error here if type not deduced yet. */
10446 finish_return_stmt (expr);
10448 done = true;
10452 if (!done)
10454 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10455 cp_parser_label_declaration (parser);
10456 cp_parser_statement_seq_opt (parser, NULL_TREE);
10457 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10460 finish_compound_stmt (compound_stmt);
10462 out:
10463 finish_function_body (body);
10464 finish_lambda_scope ();
10466 /* Finish the function and generate code for it if necessary. */
10467 tree fn = finish_function (/*inline*/2);
10469 /* Only expand if the call op is not a template. */
10470 if (!DECL_TEMPLATE_INFO (fco))
10471 expand_or_defer_fn (fn);
10474 restore_omp_privatization_clauses (omp_privatization_save);
10475 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10476 if (nested)
10477 pop_function_context();
10478 else
10479 --function_depth;
10482 /* Statements [gram.stmt.stmt] */
10484 /* Parse a statement.
10486 statement:
10487 labeled-statement
10488 expression-statement
10489 compound-statement
10490 selection-statement
10491 iteration-statement
10492 jump-statement
10493 declaration-statement
10494 try-block
10496 C++11:
10498 statement:
10499 labeled-statement
10500 attribute-specifier-seq (opt) expression-statement
10501 attribute-specifier-seq (opt) compound-statement
10502 attribute-specifier-seq (opt) selection-statement
10503 attribute-specifier-seq (opt) iteration-statement
10504 attribute-specifier-seq (opt) jump-statement
10505 declaration-statement
10506 attribute-specifier-seq (opt) try-block
10508 init-statement:
10509 expression-statement
10510 simple-declaration
10512 TM Extension:
10514 statement:
10515 atomic-statement
10517 IN_COMPOUND is true when the statement is nested inside a
10518 cp_parser_compound_statement; this matters for certain pragmas.
10520 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10521 is a (possibly labeled) if statement which is not enclosed in braces
10522 and has an else clause. This is used to implement -Wparentheses.
10524 CHAIN is a vector of if-else-if conditions. */
10526 static void
10527 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10528 bool in_compound, bool *if_p, vec<tree> *chain)
10530 tree statement, std_attrs = NULL_TREE;
10531 cp_token *token;
10532 location_t statement_location, attrs_location;
10534 restart:
10535 if (if_p != NULL)
10536 *if_p = false;
10537 /* There is no statement yet. */
10538 statement = NULL_TREE;
10540 saved_token_sentinel saved_tokens (parser->lexer);
10541 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10542 if (c_dialect_objc ())
10543 /* In obj-c++, seeing '[[' might be the either the beginning of
10544 c++11 attributes, or a nested objc-message-expression. So
10545 let's parse the c++11 attributes tentatively. */
10546 cp_parser_parse_tentatively (parser);
10547 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10548 if (c_dialect_objc ())
10550 if (!cp_parser_parse_definitely (parser))
10551 std_attrs = NULL_TREE;
10554 /* Peek at the next token. */
10555 token = cp_lexer_peek_token (parser->lexer);
10556 /* Remember the location of the first token in the statement. */
10557 statement_location = token->location;
10558 /* If this is a keyword, then that will often determine what kind of
10559 statement we have. */
10560 if (token->type == CPP_KEYWORD)
10562 enum rid keyword = token->keyword;
10564 switch (keyword)
10566 case RID_CASE:
10567 case RID_DEFAULT:
10568 /* Looks like a labeled-statement with a case label.
10569 Parse the label, and then use tail recursion to parse
10570 the statement. */
10571 cp_parser_label_for_labeled_statement (parser, std_attrs);
10572 in_compound = false;
10573 goto restart;
10575 case RID_IF:
10576 case RID_SWITCH:
10577 statement = cp_parser_selection_statement (parser, if_p, chain);
10578 break;
10580 case RID_WHILE:
10581 case RID_DO:
10582 case RID_FOR:
10583 statement = cp_parser_iteration_statement (parser, if_p, false);
10584 break;
10586 case RID_CILK_FOR:
10587 if (!flag_cilkplus)
10589 error_at (cp_lexer_peek_token (parser->lexer)->location,
10590 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10591 cp_lexer_consume_token (parser->lexer);
10592 statement = error_mark_node;
10594 else
10595 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10596 break;
10598 case RID_BREAK:
10599 case RID_CONTINUE:
10600 case RID_RETURN:
10601 case RID_GOTO:
10602 statement = cp_parser_jump_statement (parser);
10603 break;
10605 case RID_CILK_SYNC:
10606 cp_lexer_consume_token (parser->lexer);
10607 if (flag_cilkplus)
10609 tree sync_expr = build_cilk_sync ();
10610 SET_EXPR_LOCATION (sync_expr,
10611 token->location);
10612 statement = finish_expr_stmt (sync_expr);
10614 else
10616 error_at (token->location, "-fcilkplus must be enabled to use"
10617 " %<_Cilk_sync%>");
10618 statement = error_mark_node;
10620 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10621 break;
10623 /* Objective-C++ exception-handling constructs. */
10624 case RID_AT_TRY:
10625 case RID_AT_CATCH:
10626 case RID_AT_FINALLY:
10627 case RID_AT_SYNCHRONIZED:
10628 case RID_AT_THROW:
10629 statement = cp_parser_objc_statement (parser);
10630 break;
10632 case RID_TRY:
10633 statement = cp_parser_try_block (parser);
10634 break;
10636 case RID_NAMESPACE:
10637 /* This must be a namespace alias definition. */
10638 cp_parser_declaration_statement (parser);
10639 return;
10641 case RID_TRANSACTION_ATOMIC:
10642 case RID_TRANSACTION_RELAXED:
10643 case RID_SYNCHRONIZED:
10644 case RID_ATOMIC_NOEXCEPT:
10645 case RID_ATOMIC_CANCEL:
10646 statement = cp_parser_transaction (parser, token);
10647 break;
10648 case RID_TRANSACTION_CANCEL:
10649 statement = cp_parser_transaction_cancel (parser);
10650 break;
10652 default:
10653 /* It might be a keyword like `int' that can start a
10654 declaration-statement. */
10655 break;
10658 else if (token->type == CPP_NAME)
10660 /* If the next token is a `:', then we are looking at a
10661 labeled-statement. */
10662 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10663 if (token->type == CPP_COLON)
10665 /* Looks like a labeled-statement with an ordinary label.
10666 Parse the label, and then use tail recursion to parse
10667 the statement. */
10669 cp_parser_label_for_labeled_statement (parser, std_attrs);
10670 in_compound = false;
10671 goto restart;
10674 /* Anything that starts with a `{' must be a compound-statement. */
10675 else if (token->type == CPP_OPEN_BRACE)
10676 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10677 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10678 a statement all its own. */
10679 else if (token->type == CPP_PRAGMA)
10681 /* Only certain OpenMP pragmas are attached to statements, and thus
10682 are considered statements themselves. All others are not. In
10683 the context of a compound, accept the pragma as a "statement" and
10684 return so that we can check for a close brace. Otherwise we
10685 require a real statement and must go back and read one. */
10686 if (in_compound)
10687 cp_parser_pragma (parser, pragma_compound, if_p);
10688 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10689 goto restart;
10690 return;
10692 else if (token->type == CPP_EOF)
10694 cp_parser_error (parser, "expected statement");
10695 return;
10698 /* Everything else must be a declaration-statement or an
10699 expression-statement. Try for the declaration-statement
10700 first, unless we are looking at a `;', in which case we know that
10701 we have an expression-statement. */
10702 if (!statement)
10704 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10706 if (std_attrs != NULL_TREE)
10708 /* Attributes should be parsed as part of the the
10709 declaration, so let's un-parse them. */
10710 saved_tokens.rollback();
10711 std_attrs = NULL_TREE;
10714 cp_parser_parse_tentatively (parser);
10715 /* Try to parse the declaration-statement. */
10716 cp_parser_declaration_statement (parser);
10717 /* If that worked, we're done. */
10718 if (cp_parser_parse_definitely (parser))
10719 return;
10721 /* Look for an expression-statement instead. */
10722 statement = cp_parser_expression_statement (parser, in_statement_expr);
10724 /* Handle [[fallthrough]];. */
10725 if (attribute_fallthrough_p (std_attrs))
10727 /* The next token after the fallthrough attribute is ';'. */
10728 if (statement == NULL_TREE)
10730 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10731 statement = build_call_expr_internal_loc (statement_location,
10732 IFN_FALLTHROUGH,
10733 void_type_node, 0);
10734 finish_expr_stmt (statement);
10736 else
10737 warning_at (statement_location, OPT_Wattributes,
10738 "%<fallthrough%> attribute not followed by %<;%>");
10739 std_attrs = NULL_TREE;
10743 /* Set the line number for the statement. */
10744 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10745 SET_EXPR_LOCATION (statement, statement_location);
10747 /* Allow "[[fallthrough]];", but warn otherwise. */
10748 if (std_attrs != NULL_TREE)
10749 warning_at (attrs_location,
10750 OPT_Wattributes,
10751 "attributes at the beginning of statement are ignored");
10754 /* Parse the label for a labeled-statement, i.e.
10756 identifier :
10757 case constant-expression :
10758 default :
10760 GNU Extension:
10761 case constant-expression ... constant-expression : statement
10763 When a label is parsed without errors, the label is added to the
10764 parse tree by the finish_* functions, so this function doesn't
10765 have to return the label. */
10767 static void
10768 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10770 cp_token *token;
10771 tree label = NULL_TREE;
10772 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10774 /* The next token should be an identifier. */
10775 token = cp_lexer_peek_token (parser->lexer);
10776 if (token->type != CPP_NAME
10777 && token->type != CPP_KEYWORD)
10779 cp_parser_error (parser, "expected labeled-statement");
10780 return;
10783 /* Remember whether this case or a user-defined label is allowed to fall
10784 through to. */
10785 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10787 parser->colon_corrects_to_scope_p = false;
10788 switch (token->keyword)
10790 case RID_CASE:
10792 tree expr, expr_hi;
10793 cp_token *ellipsis;
10795 /* Consume the `case' token. */
10796 cp_lexer_consume_token (parser->lexer);
10797 /* Parse the constant-expression. */
10798 expr = cp_parser_constant_expression (parser);
10799 if (check_for_bare_parameter_packs (expr))
10800 expr = error_mark_node;
10802 ellipsis = cp_lexer_peek_token (parser->lexer);
10803 if (ellipsis->type == CPP_ELLIPSIS)
10805 /* Consume the `...' token. */
10806 cp_lexer_consume_token (parser->lexer);
10807 expr_hi = cp_parser_constant_expression (parser);
10808 if (check_for_bare_parameter_packs (expr_hi))
10809 expr_hi = error_mark_node;
10811 /* We don't need to emit warnings here, as the common code
10812 will do this for us. */
10814 else
10815 expr_hi = NULL_TREE;
10817 if (parser->in_switch_statement_p)
10819 tree l = finish_case_label (token->location, expr, expr_hi);
10820 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10821 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10823 else
10824 error_at (token->location,
10825 "case label %qE not within a switch statement",
10826 expr);
10828 break;
10830 case RID_DEFAULT:
10831 /* Consume the `default' token. */
10832 cp_lexer_consume_token (parser->lexer);
10834 if (parser->in_switch_statement_p)
10836 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10837 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10838 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10840 else
10841 error_at (token->location, "case label not within a switch statement");
10842 break;
10844 default:
10845 /* Anything else must be an ordinary label. */
10846 label = finish_label_stmt (cp_parser_identifier (parser));
10847 if (label && TREE_CODE (label) == LABEL_DECL)
10848 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10849 break;
10852 /* Require the `:' token. */
10853 cp_parser_require (parser, CPP_COLON, RT_COLON);
10855 /* An ordinary label may optionally be followed by attributes.
10856 However, this is only permitted if the attributes are then
10857 followed by a semicolon. This is because, for backward
10858 compatibility, when parsing
10859 lab: __attribute__ ((unused)) int i;
10860 we want the attribute to attach to "i", not "lab". */
10861 if (label != NULL_TREE
10862 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10864 tree attrs;
10865 cp_parser_parse_tentatively (parser);
10866 attrs = cp_parser_gnu_attributes_opt (parser);
10867 if (attrs == NULL_TREE
10868 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10869 cp_parser_abort_tentative_parse (parser);
10870 else if (!cp_parser_parse_definitely (parser))
10872 else
10873 attributes = chainon (attributes, attrs);
10876 if (attributes != NULL_TREE)
10877 cplus_decl_attributes (&label, attributes, 0);
10879 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10882 /* Parse an expression-statement.
10884 expression-statement:
10885 expression [opt] ;
10887 Returns the new EXPR_STMT -- or NULL_TREE if the expression
10888 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
10889 indicates whether this expression-statement is part of an
10890 expression statement. */
10892 static tree
10893 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
10895 tree statement = NULL_TREE;
10896 cp_token *token = cp_lexer_peek_token (parser->lexer);
10897 location_t loc = token->location;
10899 /* There might be attribute fallthrough. */
10900 tree attr = cp_parser_gnu_attributes_opt (parser);
10902 /* If the next token is a ';', then there is no expression
10903 statement. */
10904 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10906 statement = cp_parser_expression (parser);
10907 if (statement == error_mark_node
10908 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10910 cp_parser_skip_to_end_of_block_or_statement (parser);
10911 return error_mark_node;
10915 /* Handle [[fallthrough]];. */
10916 if (attribute_fallthrough_p (attr))
10918 /* The next token after the fallthrough attribute is ';'. */
10919 if (statement == NULL_TREE)
10920 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10921 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
10922 void_type_node, 0);
10923 else
10924 warning_at (loc, OPT_Wattributes,
10925 "%<fallthrough%> attribute not followed by %<;%>");
10926 attr = NULL_TREE;
10929 /* Allow "[[fallthrough]];", but warn otherwise. */
10930 if (attr != NULL_TREE)
10931 warning_at (loc, OPT_Wattributes,
10932 "attributes at the beginning of statement are ignored");
10934 /* Give a helpful message for "A<T>::type t;" and the like. */
10935 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
10936 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
10938 if (TREE_CODE (statement) == SCOPE_REF)
10939 error_at (token->location, "need %<typename%> before %qE because "
10940 "%qT is a dependent scope",
10941 statement, TREE_OPERAND (statement, 0));
10942 else if (is_overloaded_fn (statement)
10943 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
10945 /* A::A a; */
10946 tree fn = get_first_fn (statement);
10947 error_at (token->location,
10948 "%<%T::%D%> names the constructor, not the type",
10949 DECL_CONTEXT (fn), DECL_NAME (fn));
10953 /* Consume the final `;'. */
10954 cp_parser_consume_semicolon_at_end_of_statement (parser);
10956 if (in_statement_expr
10957 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10958 /* This is the final expression statement of a statement
10959 expression. */
10960 statement = finish_stmt_expr_expr (statement, in_statement_expr);
10961 else if (statement)
10962 statement = finish_expr_stmt (statement);
10964 return statement;
10967 /* Parse a compound-statement.
10969 compound-statement:
10970 { statement-seq [opt] }
10972 GNU extension:
10974 compound-statement:
10975 { label-declaration-seq [opt] statement-seq [opt] }
10977 label-declaration-seq:
10978 label-declaration
10979 label-declaration-seq label-declaration
10981 Returns a tree representing the statement. */
10983 static tree
10984 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
10985 int bcs_flags, bool function_body)
10987 tree compound_stmt;
10989 /* Consume the `{'. */
10990 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10991 return error_mark_node;
10992 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
10993 && !function_body && cxx_dialect < cxx14)
10994 pedwarn (input_location, OPT_Wpedantic,
10995 "compound-statement in constexpr function");
10996 /* Begin the compound-statement. */
10997 compound_stmt = begin_compound_stmt (bcs_flags);
10998 /* If the next keyword is `__label__' we have a label declaration. */
10999 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11000 cp_parser_label_declaration (parser);
11001 /* Parse an (optional) statement-seq. */
11002 cp_parser_statement_seq_opt (parser, in_statement_expr);
11003 /* Finish the compound-statement. */
11004 finish_compound_stmt (compound_stmt);
11005 /* Consume the `}'. */
11006 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11008 return compound_stmt;
11011 /* Parse an (optional) statement-seq.
11013 statement-seq:
11014 statement
11015 statement-seq [opt] statement */
11017 static void
11018 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11020 /* Scan statements until there aren't any more. */
11021 while (true)
11023 cp_token *token = cp_lexer_peek_token (parser->lexer);
11025 /* If we are looking at a `}', then we have run out of
11026 statements; the same is true if we have reached the end
11027 of file, or have stumbled upon a stray '@end'. */
11028 if (token->type == CPP_CLOSE_BRACE
11029 || token->type == CPP_EOF
11030 || token->type == CPP_PRAGMA_EOL
11031 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11032 break;
11034 /* If we are in a compound statement and find 'else' then
11035 something went wrong. */
11036 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11038 if (parser->in_statement & IN_IF_STMT)
11039 break;
11040 else
11042 token = cp_lexer_consume_token (parser->lexer);
11043 error_at (token->location, "%<else%> without a previous %<if%>");
11047 /* Parse the statement. */
11048 cp_parser_statement (parser, in_statement_expr, true, NULL);
11052 /* Return true if we're looking at (init; cond), false otherwise. */
11054 static bool
11055 cp_parser_init_statement_p (cp_parser *parser)
11057 /* Save tokens so that we can put them back. */
11058 cp_lexer_save_tokens (parser->lexer);
11060 /* Look for ';' that is not nested in () or {}. */
11061 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11062 /*recovering=*/false,
11063 CPP_SEMICOLON,
11064 /*consume_paren=*/false);
11066 /* Roll back the tokens we skipped. */
11067 cp_lexer_rollback_tokens (parser->lexer);
11069 return ret == -1;
11072 /* Parse a selection-statement.
11074 selection-statement:
11075 if ( init-statement [opt] condition ) statement
11076 if ( init-statement [opt] condition ) statement else statement
11077 switch ( init-statement [opt] condition ) statement
11079 Returns the new IF_STMT or SWITCH_STMT.
11081 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11082 is a (possibly labeled) if statement which is not enclosed in
11083 braces and has an else clause. This is used to implement
11084 -Wparentheses.
11086 CHAIN is a vector of if-else-if conditions. This is used to implement
11087 -Wduplicated-cond. */
11089 static tree
11090 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11091 vec<tree> *chain)
11093 cp_token *token;
11094 enum rid keyword;
11095 token_indent_info guard_tinfo;
11097 if (if_p != NULL)
11098 *if_p = false;
11100 /* Peek at the next token. */
11101 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11102 guard_tinfo = get_token_indent_info (token);
11104 /* See what kind of keyword it is. */
11105 keyword = token->keyword;
11106 switch (keyword)
11108 case RID_IF:
11109 case RID_SWITCH:
11111 tree statement;
11112 tree condition;
11114 bool cx = false;
11115 if (keyword == RID_IF
11116 && cp_lexer_next_token_is_keyword (parser->lexer,
11117 RID_CONSTEXPR))
11119 cx = true;
11120 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11121 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11122 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11123 "with -std=c++1z or -std=gnu++1z");
11126 /* Look for the `('. */
11127 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11129 cp_parser_skip_to_end_of_statement (parser);
11130 return error_mark_node;
11133 /* Begin the selection-statement. */
11134 if (keyword == RID_IF)
11136 statement = begin_if_stmt ();
11137 IF_STMT_CONSTEXPR_P (statement) = cx;
11139 else
11140 statement = begin_switch_stmt ();
11142 /* Parse the optional init-statement. */
11143 if (cp_parser_init_statement_p (parser))
11145 tree decl;
11146 if (cxx_dialect < cxx1z)
11147 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11148 "init-statement in selection statements only available "
11149 "with -std=c++1z or -std=gnu++1z");
11150 cp_parser_init_statement (parser, &decl);
11153 /* Parse the condition. */
11154 condition = cp_parser_condition (parser);
11155 /* Look for the `)'. */
11156 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11157 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11158 /*consume_paren=*/true);
11160 if (keyword == RID_IF)
11162 bool nested_if;
11163 unsigned char in_statement;
11165 /* Add the condition. */
11166 condition = finish_if_stmt_cond (condition, statement);
11168 if (warn_duplicated_cond)
11169 warn_duplicated_cond_add_or_warn (token->location, condition,
11170 &chain);
11172 /* Parse the then-clause. */
11173 in_statement = parser->in_statement;
11174 parser->in_statement |= IN_IF_STMT;
11176 /* Outside a template, the non-selected branch of a constexpr
11177 if is a 'discarded statement', i.e. unevaluated. */
11178 bool was_discarded = in_discarded_stmt;
11179 bool discard_then = (cx && !processing_template_decl
11180 && integer_zerop (condition));
11181 if (discard_then)
11183 in_discarded_stmt = true;
11184 ++c_inhibit_evaluation_warnings;
11187 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11188 guard_tinfo);
11190 parser->in_statement = in_statement;
11192 finish_then_clause (statement);
11194 if (discard_then)
11196 THEN_CLAUSE (statement) = NULL_TREE;
11197 in_discarded_stmt = was_discarded;
11198 --c_inhibit_evaluation_warnings;
11201 /* If the next token is `else', parse the else-clause. */
11202 if (cp_lexer_next_token_is_keyword (parser->lexer,
11203 RID_ELSE))
11205 bool discard_else = (cx && !processing_template_decl
11206 && integer_nonzerop (condition));
11207 if (discard_else)
11209 in_discarded_stmt = true;
11210 ++c_inhibit_evaluation_warnings;
11213 guard_tinfo
11214 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11215 /* Consume the `else' keyword. */
11216 cp_lexer_consume_token (parser->lexer);
11217 if (warn_duplicated_cond)
11219 if (cp_lexer_next_token_is_keyword (parser->lexer,
11220 RID_IF)
11221 && chain == NULL)
11223 /* We've got "if (COND) else if (COND2)". Start
11224 the condition chain and add COND as the first
11225 element. */
11226 chain = new vec<tree> ();
11227 if (!CONSTANT_CLASS_P (condition)
11228 && !TREE_SIDE_EFFECTS (condition))
11230 /* Wrap it in a NOP_EXPR so that we can set the
11231 location of the condition. */
11232 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11233 condition);
11234 SET_EXPR_LOCATION (e, token->location);
11235 chain->safe_push (e);
11238 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11239 RID_IF))
11241 /* This is if-else without subsequent if. Zap the
11242 condition chain; we would have already warned at
11243 this point. */
11244 delete chain;
11245 chain = NULL;
11248 begin_else_clause (statement);
11249 /* Parse the else-clause. */
11250 cp_parser_implicitly_scoped_statement (parser, NULL,
11251 guard_tinfo, chain);
11253 finish_else_clause (statement);
11255 /* If we are currently parsing a then-clause, then
11256 IF_P will not be NULL. We set it to true to
11257 indicate that this if statement has an else clause.
11258 This may trigger the Wparentheses warning below
11259 when we get back up to the parent if statement. */
11260 if (if_p != NULL)
11261 *if_p = true;
11263 if (discard_else)
11265 ELSE_CLAUSE (statement) = NULL_TREE;
11266 in_discarded_stmt = was_discarded;
11267 --c_inhibit_evaluation_warnings;
11270 else
11272 /* This if statement does not have an else clause. If
11273 NESTED_IF is true, then the then-clause has an if
11274 statement which does have an else clause. We warn
11275 about the potential ambiguity. */
11276 if (nested_if)
11277 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11278 "suggest explicit braces to avoid ambiguous"
11279 " %<else%>");
11280 if (warn_duplicated_cond)
11282 /* We don't need the condition chain anymore. */
11283 delete chain;
11284 chain = NULL;
11288 /* Now we're all done with the if-statement. */
11289 finish_if_stmt (statement);
11291 else
11293 bool in_switch_statement_p;
11294 unsigned char in_statement;
11296 /* Add the condition. */
11297 finish_switch_cond (condition, statement);
11299 /* Parse the body of the switch-statement. */
11300 in_switch_statement_p = parser->in_switch_statement_p;
11301 in_statement = parser->in_statement;
11302 parser->in_switch_statement_p = true;
11303 parser->in_statement |= IN_SWITCH_STMT;
11304 cp_parser_implicitly_scoped_statement (parser, if_p,
11305 guard_tinfo);
11306 parser->in_switch_statement_p = in_switch_statement_p;
11307 parser->in_statement = in_statement;
11309 /* Now we're all done with the switch-statement. */
11310 finish_switch_stmt (statement);
11313 return statement;
11315 break;
11317 default:
11318 cp_parser_error (parser, "expected selection-statement");
11319 return error_mark_node;
11323 /* Parse a condition.
11325 condition:
11326 expression
11327 type-specifier-seq declarator = initializer-clause
11328 type-specifier-seq declarator braced-init-list
11330 GNU Extension:
11332 condition:
11333 type-specifier-seq declarator asm-specification [opt]
11334 attributes [opt] = assignment-expression
11336 Returns the expression that should be tested. */
11338 static tree
11339 cp_parser_condition (cp_parser* parser)
11341 cp_decl_specifier_seq type_specifiers;
11342 const char *saved_message;
11343 int declares_class_or_enum;
11345 /* Try the declaration first. */
11346 cp_parser_parse_tentatively (parser);
11347 /* New types are not allowed in the type-specifier-seq for a
11348 condition. */
11349 saved_message = parser->type_definition_forbidden_message;
11350 parser->type_definition_forbidden_message
11351 = G_("types may not be defined in conditions");
11352 /* Parse the type-specifier-seq. */
11353 cp_parser_decl_specifier_seq (parser,
11354 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11355 &type_specifiers,
11356 &declares_class_or_enum);
11357 /* Restore the saved message. */
11358 parser->type_definition_forbidden_message = saved_message;
11359 /* If all is well, we might be looking at a declaration. */
11360 if (!cp_parser_error_occurred (parser))
11362 tree decl;
11363 tree asm_specification;
11364 tree attributes;
11365 cp_declarator *declarator;
11366 tree initializer = NULL_TREE;
11368 /* Parse the declarator. */
11369 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11370 /*ctor_dtor_or_conv_p=*/NULL,
11371 /*parenthesized_p=*/NULL,
11372 /*member_p=*/false,
11373 /*friend_p=*/false);
11374 /* Parse the attributes. */
11375 attributes = cp_parser_attributes_opt (parser);
11376 /* Parse the asm-specification. */
11377 asm_specification = cp_parser_asm_specification_opt (parser);
11378 /* If the next token is not an `=' or '{', then we might still be
11379 looking at an expression. For example:
11381 if (A(a).x)
11383 looks like a decl-specifier-seq and a declarator -- but then
11384 there is no `=', so this is an expression. */
11385 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11386 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11387 cp_parser_simulate_error (parser);
11389 /* If we did see an `=' or '{', then we are looking at a declaration
11390 for sure. */
11391 if (cp_parser_parse_definitely (parser))
11393 tree pushed_scope;
11394 bool non_constant_p;
11395 int flags = LOOKUP_ONLYCONVERTING;
11397 /* Create the declaration. */
11398 decl = start_decl (declarator, &type_specifiers,
11399 /*initialized_p=*/true,
11400 attributes, /*prefix_attributes=*/NULL_TREE,
11401 &pushed_scope);
11403 /* Parse the initializer. */
11404 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11406 initializer = cp_parser_braced_list (parser, &non_constant_p);
11407 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11408 flags = 0;
11410 else
11412 /* Consume the `='. */
11413 cp_parser_require (parser, CPP_EQ, RT_EQ);
11414 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11416 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11417 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11419 /* Process the initializer. */
11420 cp_finish_decl (decl,
11421 initializer, !non_constant_p,
11422 asm_specification,
11423 flags);
11425 if (pushed_scope)
11426 pop_scope (pushed_scope);
11428 return convert_from_reference (decl);
11431 /* If we didn't even get past the declarator successfully, we are
11432 definitely not looking at a declaration. */
11433 else
11434 cp_parser_abort_tentative_parse (parser);
11436 /* Otherwise, we are looking at an expression. */
11437 return cp_parser_expression (parser);
11440 /* Parses a for-statement or range-for-statement until the closing ')',
11441 not included. */
11443 static tree
11444 cp_parser_for (cp_parser *parser, bool ivdep)
11446 tree init, scope, decl;
11447 bool is_range_for;
11449 /* Begin the for-statement. */
11450 scope = begin_for_scope (&init);
11452 /* Parse the initialization. */
11453 is_range_for = cp_parser_init_statement (parser, &decl);
11455 if (is_range_for)
11456 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11457 else
11458 return cp_parser_c_for (parser, scope, init, ivdep);
11461 static tree
11462 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11464 /* Normal for loop */
11465 tree condition = NULL_TREE;
11466 tree expression = NULL_TREE;
11467 tree stmt;
11469 stmt = begin_for_stmt (scope, init);
11470 /* The init-statement has already been parsed in
11471 cp_parser_init_statement, so no work is needed here. */
11472 finish_init_stmt (stmt);
11474 /* If there's a condition, process it. */
11475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11476 condition = cp_parser_condition (parser);
11477 else if (ivdep)
11479 cp_parser_error (parser, "missing loop condition in loop with "
11480 "%<GCC ivdep%> pragma");
11481 condition = error_mark_node;
11483 finish_for_cond (condition, stmt, ivdep);
11484 /* Look for the `;'. */
11485 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11487 /* If there's an expression, process it. */
11488 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11489 expression = cp_parser_expression (parser);
11490 finish_for_expr (expression, stmt);
11492 return stmt;
11495 /* Tries to parse a range-based for-statement:
11497 range-based-for:
11498 decl-specifier-seq declarator : expression
11500 The decl-specifier-seq declarator and the `:' are already parsed by
11501 cp_parser_init_statement. If processing_template_decl it returns a
11502 newly created RANGE_FOR_STMT; if not, it is converted to a
11503 regular FOR_STMT. */
11505 static tree
11506 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11507 bool ivdep)
11509 tree stmt, range_expr;
11510 auto_vec <cxx_binding *, 16> bindings;
11511 auto_vec <tree, 16> names;
11512 tree decomp_first_name = NULL_TREE;
11513 unsigned int decomp_cnt = 0;
11515 /* Get the range declaration momentarily out of the way so that
11516 the range expression doesn't clash with it. */
11517 if (range_decl != error_mark_node)
11519 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11521 tree v = DECL_VALUE_EXPR (range_decl);
11522 /* For decomposition declaration get all of the corresponding
11523 declarations out of the way. */
11524 if (TREE_CODE (v) == ARRAY_REF
11525 && VAR_P (TREE_OPERAND (v, 0))
11526 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11528 tree d = range_decl;
11529 range_decl = TREE_OPERAND (v, 0);
11530 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11531 decomp_first_name = d;
11532 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11534 tree name = DECL_NAME (d);
11535 names.safe_push (name);
11536 bindings.safe_push (IDENTIFIER_BINDING (name));
11537 IDENTIFIER_BINDING (name)
11538 = IDENTIFIER_BINDING (name)->previous;
11542 if (names.is_empty ())
11544 tree name = DECL_NAME (range_decl);
11545 names.safe_push (name);
11546 bindings.safe_push (IDENTIFIER_BINDING (name));
11547 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11551 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11553 bool expr_non_constant_p;
11554 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11556 else
11557 range_expr = cp_parser_expression (parser);
11559 /* Put the range declaration(s) back into scope. */
11560 for (unsigned int i = 0; i < names.length (); i++)
11562 cxx_binding *binding = bindings[i];
11563 binding->previous = IDENTIFIER_BINDING (names[i]);
11564 IDENTIFIER_BINDING (names[i]) = binding;
11567 /* If in template, STMT is converted to a normal for-statement
11568 at instantiation. If not, it is done just ahead. */
11569 if (processing_template_decl)
11571 if (check_for_bare_parameter_packs (range_expr))
11572 range_expr = error_mark_node;
11573 stmt = begin_range_for_stmt (scope, init);
11574 if (ivdep)
11575 RANGE_FOR_IVDEP (stmt) = 1;
11576 finish_range_for_decl (stmt, range_decl, range_expr);
11577 if (!type_dependent_expression_p (range_expr)
11578 /* do_auto_deduction doesn't mess with template init-lists. */
11579 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11580 do_range_for_auto_deduction (range_decl, range_expr);
11582 else
11584 stmt = begin_for_stmt (scope, init);
11585 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11586 decomp_first_name, decomp_cnt, ivdep);
11588 return stmt;
11591 /* Subroutine of cp_convert_range_for: given the initializer expression,
11592 builds up the range temporary. */
11594 static tree
11595 build_range_temp (tree range_expr)
11597 tree range_type, range_temp;
11599 /* Find out the type deduced by the declaration
11600 `auto &&__range = range_expr'. */
11601 range_type = cp_build_reference_type (make_auto (), true);
11602 range_type = do_auto_deduction (range_type, range_expr,
11603 type_uses_auto (range_type));
11605 /* Create the __range variable. */
11606 range_temp = build_decl (input_location, VAR_DECL,
11607 get_identifier ("__for_range"), range_type);
11608 TREE_USED (range_temp) = 1;
11609 DECL_ARTIFICIAL (range_temp) = 1;
11611 return range_temp;
11614 /* Used by cp_parser_range_for in template context: we aren't going to
11615 do a full conversion yet, but we still need to resolve auto in the
11616 type of the for-range-declaration if present. This is basically
11617 a shortcut version of cp_convert_range_for. */
11619 static void
11620 do_range_for_auto_deduction (tree decl, tree range_expr)
11622 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11623 if (auto_node)
11625 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11626 range_temp = convert_from_reference (build_range_temp (range_expr));
11627 iter_type = (cp_parser_perform_range_for_lookup
11628 (range_temp, &begin_dummy, &end_dummy));
11629 if (iter_type)
11631 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11632 iter_type);
11633 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11634 tf_warning_or_error);
11635 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11636 iter_decl, auto_node);
11641 /* Converts a range-based for-statement into a normal
11642 for-statement, as per the definition.
11644 for (RANGE_DECL : RANGE_EXPR)
11645 BLOCK
11647 should be equivalent to:
11650 auto &&__range = RANGE_EXPR;
11651 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11652 __begin != __end;
11653 ++__begin)
11655 RANGE_DECL = *__begin;
11656 BLOCK
11660 If RANGE_EXPR is an array:
11661 BEGIN_EXPR = __range
11662 END_EXPR = __range + ARRAY_SIZE(__range)
11663 Else if RANGE_EXPR has a member 'begin' or 'end':
11664 BEGIN_EXPR = __range.begin()
11665 END_EXPR = __range.end()
11666 Else:
11667 BEGIN_EXPR = begin(__range)
11668 END_EXPR = end(__range);
11670 If __range has a member 'begin' but not 'end', or vice versa, we must
11671 still use the second alternative (it will surely fail, however).
11672 When calling begin()/end() in the third alternative we must use
11673 argument dependent lookup, but always considering 'std' as an associated
11674 namespace. */
11676 tree
11677 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11678 tree decomp_first_name, unsigned int decomp_cnt,
11679 bool ivdep)
11681 tree begin, end;
11682 tree iter_type, begin_expr, end_expr;
11683 tree condition, expression;
11685 if (range_decl == error_mark_node || range_expr == error_mark_node)
11686 /* If an error happened previously do nothing or else a lot of
11687 unhelpful errors would be issued. */
11688 begin_expr = end_expr = iter_type = error_mark_node;
11689 else
11691 tree range_temp;
11693 if (VAR_P (range_expr)
11694 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11695 /* Can't bind a reference to an array of runtime bound. */
11696 range_temp = range_expr;
11697 else
11699 range_temp = build_range_temp (range_expr);
11700 pushdecl (range_temp);
11701 cp_finish_decl (range_temp, range_expr,
11702 /*is_constant_init*/false, NULL_TREE,
11703 LOOKUP_ONLYCONVERTING);
11704 range_temp = convert_from_reference (range_temp);
11706 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11707 &begin_expr, &end_expr);
11710 /* The new for initialization statement. */
11711 begin = build_decl (input_location, VAR_DECL,
11712 get_identifier ("__for_begin"), iter_type);
11713 TREE_USED (begin) = 1;
11714 DECL_ARTIFICIAL (begin) = 1;
11715 pushdecl (begin);
11716 cp_finish_decl (begin, begin_expr,
11717 /*is_constant_init*/false, NULL_TREE,
11718 LOOKUP_ONLYCONVERTING);
11720 if (cxx_dialect >= cxx1z)
11721 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11722 end = build_decl (input_location, VAR_DECL,
11723 get_identifier ("__for_end"), iter_type);
11724 TREE_USED (end) = 1;
11725 DECL_ARTIFICIAL (end) = 1;
11726 pushdecl (end);
11727 cp_finish_decl (end, end_expr,
11728 /*is_constant_init*/false, NULL_TREE,
11729 LOOKUP_ONLYCONVERTING);
11731 finish_init_stmt (statement);
11733 /* The new for condition. */
11734 condition = build_x_binary_op (input_location, NE_EXPR,
11735 begin, ERROR_MARK,
11736 end, ERROR_MARK,
11737 NULL, tf_warning_or_error);
11738 finish_for_cond (condition, statement, ivdep);
11740 /* The new increment expression. */
11741 expression = finish_unary_op_expr (input_location,
11742 PREINCREMENT_EXPR, begin,
11743 tf_warning_or_error);
11744 finish_for_expr (expression, statement);
11746 /* The declaration is initialized with *__begin inside the loop body. */
11747 cp_finish_decl (range_decl,
11748 build_x_indirect_ref (input_location, begin, RO_NULL,
11749 tf_warning_or_error),
11750 /*is_constant_init*/false, NULL_TREE,
11751 LOOKUP_ONLYCONVERTING);
11752 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11753 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11755 return statement;
11758 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11759 We need to solve both at the same time because the method used
11760 depends on the existence of members begin or end.
11761 Returns the type deduced for the iterator expression. */
11763 static tree
11764 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11766 if (error_operand_p (range))
11768 *begin = *end = error_mark_node;
11769 return error_mark_node;
11772 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11774 error ("range-based %<for%> expression of type %qT "
11775 "has incomplete type", TREE_TYPE (range));
11776 *begin = *end = error_mark_node;
11777 return error_mark_node;
11779 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11781 /* If RANGE is an array, we will use pointer arithmetic. */
11782 *begin = decay_conversion (range, tf_warning_or_error);
11783 *end = build_binary_op (input_location, PLUS_EXPR,
11784 range,
11785 array_type_nelts_top (TREE_TYPE (range)),
11787 return TREE_TYPE (*begin);
11789 else
11791 /* If it is not an array, we must do a bit of magic. */
11792 tree id_begin, id_end;
11793 tree member_begin, member_end;
11795 *begin = *end = error_mark_node;
11797 id_begin = get_identifier ("begin");
11798 id_end = get_identifier ("end");
11799 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11800 /*protect=*/2, /*want_type=*/false,
11801 tf_warning_or_error);
11802 member_end = lookup_member (TREE_TYPE (range), id_end,
11803 /*protect=*/2, /*want_type=*/false,
11804 tf_warning_or_error);
11806 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11808 /* Use the member functions. */
11809 if (member_begin != NULL_TREE)
11810 *begin = cp_parser_range_for_member_function (range, id_begin);
11811 else
11812 error ("range-based %<for%> expression of type %qT has an "
11813 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11815 if (member_end != NULL_TREE)
11816 *end = cp_parser_range_for_member_function (range, id_end);
11817 else
11818 error ("range-based %<for%> expression of type %qT has a "
11819 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11821 else
11823 /* Use global functions with ADL. */
11824 vec<tree, va_gc> *vec;
11825 vec = make_tree_vector ();
11827 vec_safe_push (vec, range);
11829 member_begin = perform_koenig_lookup (id_begin, vec,
11830 tf_warning_or_error);
11831 *begin = finish_call_expr (member_begin, &vec, false, true,
11832 tf_warning_or_error);
11833 member_end = perform_koenig_lookup (id_end, vec,
11834 tf_warning_or_error);
11835 *end = finish_call_expr (member_end, &vec, false, true,
11836 tf_warning_or_error);
11838 release_tree_vector (vec);
11841 /* Last common checks. */
11842 if (*begin == error_mark_node || *end == error_mark_node)
11844 /* If one of the expressions is an error do no more checks. */
11845 *begin = *end = error_mark_node;
11846 return error_mark_node;
11848 else if (type_dependent_expression_p (*begin)
11849 || type_dependent_expression_p (*end))
11850 /* Can happen, when, eg, in a template context, Koenig lookup
11851 can't resolve begin/end (c++/58503). */
11852 return NULL_TREE;
11853 else
11855 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11856 /* The unqualified type of the __begin and __end temporaries should
11857 be the same, as required by the multiple auto declaration. */
11858 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11860 if (cxx_dialect >= cxx1z
11861 && (build_x_binary_op (input_location, NE_EXPR,
11862 *begin, ERROR_MARK,
11863 *end, ERROR_MARK,
11864 NULL, tf_none)
11865 != error_mark_node))
11866 /* P0184R0 allows __begin and __end to have different types,
11867 but make sure they are comparable so we can give a better
11868 diagnostic. */;
11869 else
11870 error ("inconsistent begin/end types in range-based %<for%> "
11871 "statement: %qT and %qT",
11872 TREE_TYPE (*begin), TREE_TYPE (*end));
11874 return iter_type;
11879 /* Helper function for cp_parser_perform_range_for_lookup.
11880 Builds a tree for RANGE.IDENTIFIER(). */
11882 static tree
11883 cp_parser_range_for_member_function (tree range, tree identifier)
11885 tree member, res;
11886 vec<tree, va_gc> *vec;
11888 member = finish_class_member_access_expr (range, identifier,
11889 false, tf_warning_or_error);
11890 if (member == error_mark_node)
11891 return error_mark_node;
11893 vec = make_tree_vector ();
11894 res = finish_call_expr (member, &vec,
11895 /*disallow_virtual=*/false,
11896 /*koenig_p=*/false,
11897 tf_warning_or_error);
11898 release_tree_vector (vec);
11899 return res;
11902 /* Parse an iteration-statement.
11904 iteration-statement:
11905 while ( condition ) statement
11906 do statement while ( expression ) ;
11907 for ( init-statement condition [opt] ; expression [opt] )
11908 statement
11910 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
11912 static tree
11913 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
11915 cp_token *token;
11916 enum rid keyword;
11917 tree statement;
11918 unsigned char in_statement;
11919 token_indent_info guard_tinfo;
11921 /* Peek at the next token. */
11922 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
11923 if (!token)
11924 return error_mark_node;
11926 guard_tinfo = get_token_indent_info (token);
11928 /* Remember whether or not we are already within an iteration
11929 statement. */
11930 in_statement = parser->in_statement;
11932 /* See what kind of keyword it is. */
11933 keyword = token->keyword;
11934 switch (keyword)
11936 case RID_WHILE:
11938 tree condition;
11940 /* Begin the while-statement. */
11941 statement = begin_while_stmt ();
11942 /* Look for the `('. */
11943 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11944 /* Parse the condition. */
11945 condition = cp_parser_condition (parser);
11946 finish_while_stmt_cond (condition, statement, ivdep);
11947 /* Look for the `)'. */
11948 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11949 /* Parse the dependent statement. */
11950 parser->in_statement = IN_ITERATION_STMT;
11951 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11952 parser->in_statement = in_statement;
11953 /* We're done with the while-statement. */
11954 finish_while_stmt (statement);
11956 break;
11958 case RID_DO:
11960 tree expression;
11962 /* Begin the do-statement. */
11963 statement = begin_do_stmt ();
11964 /* Parse the body of the do-statement. */
11965 parser->in_statement = IN_ITERATION_STMT;
11966 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
11967 parser->in_statement = in_statement;
11968 finish_do_body (statement);
11969 /* Look for the `while' keyword. */
11970 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
11971 /* Look for the `('. */
11972 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11973 /* Parse the expression. */
11974 expression = cp_parser_expression (parser);
11975 /* We're done with the do-statement. */
11976 finish_do_stmt (expression, statement, ivdep);
11977 /* Look for the `)'. */
11978 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11979 /* Look for the `;'. */
11980 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11982 break;
11984 case RID_FOR:
11986 /* Look for the `('. */
11987 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
11989 statement = cp_parser_for (parser, ivdep);
11991 /* Look for the `)'. */
11992 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11994 /* Parse the body of the for-statement. */
11995 parser->in_statement = IN_ITERATION_STMT;
11996 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
11997 parser->in_statement = in_statement;
11999 /* We're done with the for-statement. */
12000 finish_for_stmt (statement);
12002 break;
12004 default:
12005 cp_parser_error (parser, "expected iteration-statement");
12006 statement = error_mark_node;
12007 break;
12010 return statement;
12013 /* Parse a init-statement or the declarator of a range-based-for.
12014 Returns true if a range-based-for declaration is seen.
12016 init-statement:
12017 expression-statement
12018 simple-declaration */
12020 static bool
12021 cp_parser_init_statement (cp_parser* parser, tree *decl)
12023 /* If the next token is a `;', then we have an empty
12024 expression-statement. Grammatically, this is also a
12025 simple-declaration, but an invalid one, because it does not
12026 declare anything. Therefore, if we did not handle this case
12027 specially, we would issue an error message about an invalid
12028 declaration. */
12029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12031 bool is_range_for = false;
12032 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12034 /* A colon is used in range-based for. */
12035 parser->colon_corrects_to_scope_p = false;
12037 /* We're going to speculatively look for a declaration, falling back
12038 to an expression, if necessary. */
12039 cp_parser_parse_tentatively (parser);
12040 /* Parse the declaration. */
12041 cp_parser_simple_declaration (parser,
12042 /*function_definition_allowed_p=*/false,
12043 decl);
12044 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12045 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12047 /* It is a range-for, consume the ':' */
12048 cp_lexer_consume_token (parser->lexer);
12049 is_range_for = true;
12050 if (cxx_dialect < cxx11)
12052 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12053 "range-based %<for%> loops only available with "
12054 "-std=c++11 or -std=gnu++11");
12055 *decl = error_mark_node;
12058 else
12059 /* The ';' is not consumed yet because we told
12060 cp_parser_simple_declaration not to. */
12061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12063 if (cp_parser_parse_definitely (parser))
12064 return is_range_for;
12065 /* If the tentative parse failed, then we shall need to look for an
12066 expression-statement. */
12068 /* If we are here, it is an expression-statement. */
12069 cp_parser_expression_statement (parser, NULL_TREE);
12070 return false;
12073 /* Parse a jump-statement.
12075 jump-statement:
12076 break ;
12077 continue ;
12078 return expression [opt] ;
12079 return braced-init-list ;
12080 goto identifier ;
12082 GNU extension:
12084 jump-statement:
12085 goto * expression ;
12087 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12089 static tree
12090 cp_parser_jump_statement (cp_parser* parser)
12092 tree statement = error_mark_node;
12093 cp_token *token;
12094 enum rid keyword;
12095 unsigned char in_statement;
12097 /* Peek at the next token. */
12098 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12099 if (!token)
12100 return error_mark_node;
12102 /* See what kind of keyword it is. */
12103 keyword = token->keyword;
12104 switch (keyword)
12106 case RID_BREAK:
12107 in_statement = parser->in_statement & ~IN_IF_STMT;
12108 switch (in_statement)
12110 case 0:
12111 error_at (token->location, "break statement not within loop or switch");
12112 break;
12113 default:
12114 gcc_assert ((in_statement & IN_SWITCH_STMT)
12115 || in_statement == IN_ITERATION_STMT);
12116 statement = finish_break_stmt ();
12117 if (in_statement == IN_ITERATION_STMT)
12118 break_maybe_infinite_loop ();
12119 break;
12120 case IN_OMP_BLOCK:
12121 error_at (token->location, "invalid exit from OpenMP structured block");
12122 break;
12123 case IN_OMP_FOR:
12124 error_at (token->location, "break statement used with OpenMP for loop");
12125 break;
12126 case IN_CILK_SIMD_FOR:
12127 error_at (token->location, "break statement used with Cilk Plus for loop");
12128 break;
12130 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12131 break;
12133 case RID_CONTINUE:
12134 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12136 case 0:
12137 error_at (token->location, "continue statement not within a loop");
12138 break;
12139 case IN_CILK_SIMD_FOR:
12140 error_at (token->location,
12141 "continue statement within %<#pragma simd%> loop body");
12142 /* Fall through. */
12143 case IN_ITERATION_STMT:
12144 case IN_OMP_FOR:
12145 statement = finish_continue_stmt ();
12146 break;
12147 case IN_OMP_BLOCK:
12148 error_at (token->location, "invalid exit from OpenMP structured block");
12149 break;
12150 default:
12151 gcc_unreachable ();
12153 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12154 break;
12156 case RID_RETURN:
12158 tree expr;
12159 bool expr_non_constant_p;
12161 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12163 cp_lexer_set_source_position (parser->lexer);
12164 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12165 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12167 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12168 expr = cp_parser_expression (parser);
12169 else
12170 /* If the next token is a `;', then there is no
12171 expression. */
12172 expr = NULL_TREE;
12173 /* Build the return-statement. */
12174 if (current_function_auto_return_pattern && in_discarded_stmt)
12175 /* Don't deduce from a discarded return statement. */;
12176 else
12177 statement = finish_return_stmt (expr);
12178 /* Look for the final `;'. */
12179 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12181 break;
12183 case RID_GOTO:
12184 if (parser->in_function_body
12185 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12187 error ("%<goto%> in %<constexpr%> function");
12188 cp_function_chain->invalid_constexpr = true;
12191 /* Create the goto-statement. */
12192 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12194 /* Issue a warning about this use of a GNU extension. */
12195 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12196 /* Consume the '*' token. */
12197 cp_lexer_consume_token (parser->lexer);
12198 /* Parse the dependent expression. */
12199 finish_goto_stmt (cp_parser_expression (parser));
12201 else
12202 finish_goto_stmt (cp_parser_identifier (parser));
12203 /* Look for the final `;'. */
12204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12205 break;
12207 default:
12208 cp_parser_error (parser, "expected jump-statement");
12209 break;
12212 return statement;
12215 /* Parse a declaration-statement.
12217 declaration-statement:
12218 block-declaration */
12220 static void
12221 cp_parser_declaration_statement (cp_parser* parser)
12223 void *p;
12225 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12226 p = obstack_alloc (&declarator_obstack, 0);
12228 /* Parse the block-declaration. */
12229 cp_parser_block_declaration (parser, /*statement_p=*/true);
12231 /* Free any declarators allocated. */
12232 obstack_free (&declarator_obstack, p);
12235 /* Some dependent statements (like `if (cond) statement'), are
12236 implicitly in their own scope. In other words, if the statement is
12237 a single statement (as opposed to a compound-statement), it is
12238 none-the-less treated as if it were enclosed in braces. Any
12239 declarations appearing in the dependent statement are out of scope
12240 after control passes that point. This function parses a statement,
12241 but ensures that is in its own scope, even if it is not a
12242 compound-statement.
12244 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12245 is a (possibly labeled) if statement which is not enclosed in
12246 braces and has an else clause. This is used to implement
12247 -Wparentheses.
12249 CHAIN is a vector of if-else-if conditions. This is used to implement
12250 -Wduplicated-cond.
12252 Returns the new statement. */
12254 static tree
12255 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12256 const token_indent_info &guard_tinfo,
12257 vec<tree> *chain)
12259 tree statement;
12260 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12261 token_indent_info body_tinfo
12262 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12264 if (if_p != NULL)
12265 *if_p = false;
12267 /* Mark if () ; with a special NOP_EXPR. */
12268 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12270 cp_lexer_consume_token (parser->lexer);
12271 statement = add_stmt (build_empty_stmt (body_loc));
12273 if (guard_tinfo.keyword == RID_IF
12274 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12275 warning_at (body_loc, OPT_Wempty_body,
12276 "suggest braces around empty body in an %<if%> statement");
12277 else if (guard_tinfo.keyword == RID_ELSE)
12278 warning_at (body_loc, OPT_Wempty_body,
12279 "suggest braces around empty body in an %<else%> statement");
12281 /* if a compound is opened, we simply parse the statement directly. */
12282 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12283 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12284 /* If the token is not a `{', then we must take special action. */
12285 else
12287 /* Create a compound-statement. */
12288 statement = begin_compound_stmt (0);
12289 /* Parse the dependent-statement. */
12290 cp_parser_statement (parser, NULL_TREE, false, if_p, chain);
12291 /* Finish the dummy compound-statement. */
12292 finish_compound_stmt (statement);
12295 token_indent_info next_tinfo
12296 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12297 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12299 /* Return the statement. */
12300 return statement;
12303 /* For some dependent statements (like `while (cond) statement'), we
12304 have already created a scope. Therefore, even if the dependent
12305 statement is a compound-statement, we do not want to create another
12306 scope. */
12308 static void
12309 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12310 const token_indent_info &guard_tinfo)
12312 /* If the token is a `{', then we must take special action. */
12313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12315 token_indent_info body_tinfo
12316 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12318 cp_parser_statement (parser, NULL_TREE, false, if_p);
12319 token_indent_info next_tinfo
12320 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12321 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12323 else
12325 /* Avoid calling cp_parser_compound_statement, so that we
12326 don't create a new scope. Do everything else by hand. */
12327 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12328 /* If the next keyword is `__label__' we have a label declaration. */
12329 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12330 cp_parser_label_declaration (parser);
12331 /* Parse an (optional) statement-seq. */
12332 cp_parser_statement_seq_opt (parser, NULL_TREE);
12333 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12337 /* Declarations [gram.dcl.dcl] */
12339 /* Parse an optional declaration-sequence.
12341 declaration-seq:
12342 declaration
12343 declaration-seq declaration */
12345 static void
12346 cp_parser_declaration_seq_opt (cp_parser* parser)
12348 while (true)
12350 cp_token *token;
12352 token = cp_lexer_peek_token (parser->lexer);
12354 if (token->type == CPP_CLOSE_BRACE
12355 || token->type == CPP_EOF
12356 || token->type == CPP_PRAGMA_EOL)
12357 break;
12359 if (token->type == CPP_SEMICOLON)
12361 /* A declaration consisting of a single semicolon is
12362 invalid. Allow it unless we're being pedantic. */
12363 cp_lexer_consume_token (parser->lexer);
12364 if (!in_system_header_at (input_location))
12365 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12366 continue;
12369 /* If we're entering or exiting a region that's implicitly
12370 extern "C", modify the lang context appropriately. */
12371 if (!parser->implicit_extern_c && token->implicit_extern_c)
12373 push_lang_context (lang_name_c);
12374 parser->implicit_extern_c = true;
12376 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12378 pop_lang_context ();
12379 parser->implicit_extern_c = false;
12382 if (token->type == CPP_PRAGMA)
12384 /* A top-level declaration can consist solely of a #pragma.
12385 A nested declaration cannot, so this is done here and not
12386 in cp_parser_declaration. (A #pragma at block scope is
12387 handled in cp_parser_statement.) */
12388 cp_parser_pragma (parser, pragma_external, NULL);
12389 continue;
12392 /* Parse the declaration itself. */
12393 cp_parser_declaration (parser);
12397 /* Parse a declaration.
12399 declaration:
12400 block-declaration
12401 function-definition
12402 template-declaration
12403 explicit-instantiation
12404 explicit-specialization
12405 linkage-specification
12406 namespace-definition
12408 C++17:
12409 deduction-guide
12411 GNU extension:
12413 declaration:
12414 __extension__ declaration */
12416 static void
12417 cp_parser_declaration (cp_parser* parser)
12419 cp_token token1;
12420 cp_token token2;
12421 int saved_pedantic;
12422 void *p;
12423 tree attributes = NULL_TREE;
12425 /* Check for the `__extension__' keyword. */
12426 if (cp_parser_extension_opt (parser, &saved_pedantic))
12428 /* Parse the qualified declaration. */
12429 cp_parser_declaration (parser);
12430 /* Restore the PEDANTIC flag. */
12431 pedantic = saved_pedantic;
12433 return;
12436 /* Try to figure out what kind of declaration is present. */
12437 token1 = *cp_lexer_peek_token (parser->lexer);
12439 if (token1.type != CPP_EOF)
12440 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12441 else
12443 token2.type = CPP_EOF;
12444 token2.keyword = RID_MAX;
12447 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12448 p = obstack_alloc (&declarator_obstack, 0);
12450 /* If the next token is `extern' and the following token is a string
12451 literal, then we have a linkage specification. */
12452 if (token1.keyword == RID_EXTERN
12453 && cp_parser_is_pure_string_literal (&token2))
12454 cp_parser_linkage_specification (parser);
12455 /* If the next token is `template', then we have either a template
12456 declaration, an explicit instantiation, or an explicit
12457 specialization. */
12458 else if (token1.keyword == RID_TEMPLATE)
12460 /* `template <>' indicates a template specialization. */
12461 if (token2.type == CPP_LESS
12462 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12463 cp_parser_explicit_specialization (parser);
12464 /* `template <' indicates a template declaration. */
12465 else if (token2.type == CPP_LESS)
12466 cp_parser_template_declaration (parser, /*member_p=*/false);
12467 /* Anything else must be an explicit instantiation. */
12468 else
12469 cp_parser_explicit_instantiation (parser);
12471 /* If the next token is `export', then we have a template
12472 declaration. */
12473 else if (token1.keyword == RID_EXPORT)
12474 cp_parser_template_declaration (parser, /*member_p=*/false);
12475 /* If the next token is `extern', 'static' or 'inline' and the one
12476 after that is `template', we have a GNU extended explicit
12477 instantiation directive. */
12478 else if (cp_parser_allow_gnu_extensions_p (parser)
12479 && (token1.keyword == RID_EXTERN
12480 || token1.keyword == RID_STATIC
12481 || token1.keyword == RID_INLINE)
12482 && token2.keyword == RID_TEMPLATE)
12483 cp_parser_explicit_instantiation (parser);
12484 /* If the next token is `namespace', check for a named or unnamed
12485 namespace definition. */
12486 else if (token1.keyword == RID_NAMESPACE
12487 && (/* A named namespace definition. */
12488 (token2.type == CPP_NAME
12489 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12490 != CPP_EQ))
12491 || (token2.type == CPP_OPEN_SQUARE
12492 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12493 == CPP_OPEN_SQUARE)
12494 /* An unnamed namespace definition. */
12495 || token2.type == CPP_OPEN_BRACE
12496 || token2.keyword == RID_ATTRIBUTE))
12497 cp_parser_namespace_definition (parser);
12498 /* An inline (associated) namespace definition. */
12499 else if (token1.keyword == RID_INLINE
12500 && token2.keyword == RID_NAMESPACE)
12501 cp_parser_namespace_definition (parser);
12502 /* Objective-C++ declaration/definition. */
12503 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12504 cp_parser_objc_declaration (parser, NULL_TREE);
12505 else if (c_dialect_objc ()
12506 && token1.keyword == RID_ATTRIBUTE
12507 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12508 cp_parser_objc_declaration (parser, attributes);
12509 /* At this point we may have a template declared by a concept
12510 introduction. */
12511 else if (flag_concepts
12512 && cp_parser_template_declaration_after_export (parser,
12513 /*member_p=*/false))
12514 /* We did. */;
12515 else
12516 /* Try to parse a block-declaration, or a function-definition. */
12517 cp_parser_block_declaration (parser, /*statement_p=*/false);
12519 /* Free any declarators allocated. */
12520 obstack_free (&declarator_obstack, p);
12523 /* Parse a block-declaration.
12525 block-declaration:
12526 simple-declaration
12527 asm-definition
12528 namespace-alias-definition
12529 using-declaration
12530 using-directive
12532 GNU Extension:
12534 block-declaration:
12535 __extension__ block-declaration
12537 C++0x Extension:
12539 block-declaration:
12540 static_assert-declaration
12542 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12543 part of a declaration-statement. */
12545 static void
12546 cp_parser_block_declaration (cp_parser *parser,
12547 bool statement_p)
12549 cp_token *token1;
12550 int saved_pedantic;
12552 /* Check for the `__extension__' keyword. */
12553 if (cp_parser_extension_opt (parser, &saved_pedantic))
12555 /* Parse the qualified declaration. */
12556 cp_parser_block_declaration (parser, statement_p);
12557 /* Restore the PEDANTIC flag. */
12558 pedantic = saved_pedantic;
12560 return;
12563 /* Peek at the next token to figure out which kind of declaration is
12564 present. */
12565 token1 = cp_lexer_peek_token (parser->lexer);
12567 /* If the next keyword is `asm', we have an asm-definition. */
12568 if (token1->keyword == RID_ASM)
12570 if (statement_p)
12571 cp_parser_commit_to_tentative_parse (parser);
12572 cp_parser_asm_definition (parser);
12574 /* If the next keyword is `namespace', we have a
12575 namespace-alias-definition. */
12576 else if (token1->keyword == RID_NAMESPACE)
12577 cp_parser_namespace_alias_definition (parser);
12578 /* If the next keyword is `using', we have a
12579 using-declaration, a using-directive, or an alias-declaration. */
12580 else if (token1->keyword == RID_USING)
12582 cp_token *token2;
12584 if (statement_p)
12585 cp_parser_commit_to_tentative_parse (parser);
12586 /* If the token after `using' is `namespace', then we have a
12587 using-directive. */
12588 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12589 if (token2->keyword == RID_NAMESPACE)
12590 cp_parser_using_directive (parser);
12591 /* If the second token after 'using' is '=', then we have an
12592 alias-declaration. */
12593 else if (cxx_dialect >= cxx11
12594 && token2->type == CPP_NAME
12595 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12596 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12597 cp_parser_alias_declaration (parser);
12598 /* Otherwise, it's a using-declaration. */
12599 else
12600 cp_parser_using_declaration (parser,
12601 /*access_declaration_p=*/false);
12603 /* If the next keyword is `__label__' we have a misplaced label
12604 declaration. */
12605 else if (token1->keyword == RID_LABEL)
12607 cp_lexer_consume_token (parser->lexer);
12608 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12609 cp_parser_skip_to_end_of_statement (parser);
12610 /* If the next token is now a `;', consume it. */
12611 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12612 cp_lexer_consume_token (parser->lexer);
12614 /* If the next token is `static_assert' we have a static assertion. */
12615 else if (token1->keyword == RID_STATIC_ASSERT)
12616 cp_parser_static_assert (parser, /*member_p=*/false);
12617 /* Anything else must be a simple-declaration. */
12618 else
12619 cp_parser_simple_declaration (parser, !statement_p,
12620 /*maybe_range_for_decl*/NULL);
12623 /* Parse a simple-declaration.
12625 simple-declaration:
12626 decl-specifier-seq [opt] init-declarator-list [opt] ;
12627 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12628 brace-or-equal-initializer ;
12630 init-declarator-list:
12631 init-declarator
12632 init-declarator-list , init-declarator
12634 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12635 function-definition as a simple-declaration.
12637 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12638 parsed declaration if it is an uninitialized single declarator not followed
12639 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12640 if present, will not be consumed. */
12642 static void
12643 cp_parser_simple_declaration (cp_parser* parser,
12644 bool function_definition_allowed_p,
12645 tree *maybe_range_for_decl)
12647 cp_decl_specifier_seq decl_specifiers;
12648 int declares_class_or_enum;
12649 bool saw_declarator;
12650 location_t comma_loc = UNKNOWN_LOCATION;
12651 location_t init_loc = UNKNOWN_LOCATION;
12653 if (maybe_range_for_decl)
12654 *maybe_range_for_decl = NULL_TREE;
12656 /* Defer access checks until we know what is being declared; the
12657 checks for names appearing in the decl-specifier-seq should be
12658 done as if we were in the scope of the thing being declared. */
12659 push_deferring_access_checks (dk_deferred);
12661 /* Parse the decl-specifier-seq. We have to keep track of whether
12662 or not the decl-specifier-seq declares a named class or
12663 enumeration type, since that is the only case in which the
12664 init-declarator-list is allowed to be empty.
12666 [dcl.dcl]
12668 In a simple-declaration, the optional init-declarator-list can be
12669 omitted only when declaring a class or enumeration, that is when
12670 the decl-specifier-seq contains either a class-specifier, an
12671 elaborated-type-specifier, or an enum-specifier. */
12672 cp_parser_decl_specifier_seq (parser,
12673 CP_PARSER_FLAGS_OPTIONAL,
12674 &decl_specifiers,
12675 &declares_class_or_enum);
12676 /* We no longer need to defer access checks. */
12677 stop_deferring_access_checks ();
12679 /* In a block scope, a valid declaration must always have a
12680 decl-specifier-seq. By not trying to parse declarators, we can
12681 resolve the declaration/expression ambiguity more quickly. */
12682 if (!function_definition_allowed_p
12683 && !decl_specifiers.any_specifiers_p)
12685 cp_parser_error (parser, "expected declaration");
12686 goto done;
12689 /* If the next two tokens are both identifiers, the code is
12690 erroneous. The usual cause of this situation is code like:
12692 T t;
12694 where "T" should name a type -- but does not. */
12695 if (!decl_specifiers.any_type_specifiers_p
12696 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12698 /* If parsing tentatively, we should commit; we really are
12699 looking at a declaration. */
12700 cp_parser_commit_to_tentative_parse (parser);
12701 /* Give up. */
12702 goto done;
12705 /* If we have seen at least one decl-specifier, and the next token
12706 is not a parenthesis, then we must be looking at a declaration.
12707 (After "int (" we might be looking at a functional cast.) */
12708 if (decl_specifiers.any_specifiers_p
12709 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12710 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12711 && !cp_parser_error_occurred (parser))
12712 cp_parser_commit_to_tentative_parse (parser);
12714 /* Look for C++17 decomposition declaration. */
12715 for (size_t n = 1; ; n++)
12716 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12717 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12718 continue;
12719 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12720 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12721 && decl_specifiers.any_specifiers_p)
12723 tree decl
12724 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12725 maybe_range_for_decl,
12726 &init_loc);
12728 /* The next token should be either a `,' or a `;'. */
12729 cp_token *token = cp_lexer_peek_token (parser->lexer);
12730 /* If it's a `;', we are done. */
12731 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12732 goto finish;
12733 /* Anything else is an error. */
12734 else
12736 /* If we have already issued an error message we don't need
12737 to issue another one. */
12738 if ((decl != error_mark_node
12739 && DECL_INITIAL (decl) != error_mark_node)
12740 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12741 cp_parser_error (parser, "expected %<,%> or %<;%>");
12742 /* Skip tokens until we reach the end of the statement. */
12743 cp_parser_skip_to_end_of_statement (parser);
12744 /* If the next token is now a `;', consume it. */
12745 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12746 cp_lexer_consume_token (parser->lexer);
12747 goto done;
12750 else
12751 break;
12753 tree last_type;
12754 bool auto_specifier_p;
12755 /* NULL_TREE if both variable and function declaration are allowed,
12756 error_mark_node if function declaration are not allowed and
12757 a FUNCTION_DECL that should be diagnosed if it is followed by
12758 variable declarations. */
12759 tree auto_function_declaration;
12761 last_type = NULL_TREE;
12762 auto_specifier_p
12763 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12764 auto_function_declaration = NULL_TREE;
12766 /* Keep going until we hit the `;' at the end of the simple
12767 declaration. */
12768 saw_declarator = false;
12769 while (cp_lexer_next_token_is_not (parser->lexer,
12770 CPP_SEMICOLON))
12772 cp_token *token;
12773 bool function_definition_p;
12774 tree decl;
12775 tree auto_result = NULL_TREE;
12777 if (saw_declarator)
12779 /* If we are processing next declarator, comma is expected */
12780 token = cp_lexer_peek_token (parser->lexer);
12781 gcc_assert (token->type == CPP_COMMA);
12782 cp_lexer_consume_token (parser->lexer);
12783 if (maybe_range_for_decl)
12785 *maybe_range_for_decl = error_mark_node;
12786 if (comma_loc == UNKNOWN_LOCATION)
12787 comma_loc = token->location;
12790 else
12791 saw_declarator = true;
12793 /* Parse the init-declarator. */
12794 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12795 /*checks=*/NULL,
12796 function_definition_allowed_p,
12797 /*member_p=*/false,
12798 declares_class_or_enum,
12799 &function_definition_p,
12800 maybe_range_for_decl,
12801 &init_loc,
12802 &auto_result);
12803 /* If an error occurred while parsing tentatively, exit quickly.
12804 (That usually happens when in the body of a function; each
12805 statement is treated as a declaration-statement until proven
12806 otherwise.) */
12807 if (cp_parser_error_occurred (parser))
12808 goto done;
12810 if (auto_specifier_p && cxx_dialect >= cxx14)
12812 /* If the init-declarator-list contains more than one
12813 init-declarator, they shall all form declarations of
12814 variables. */
12815 if (auto_function_declaration == NULL_TREE)
12816 auto_function_declaration
12817 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12818 else if (TREE_CODE (decl) == FUNCTION_DECL
12819 || auto_function_declaration != error_mark_node)
12821 error_at (decl_specifiers.locations[ds_type_spec],
12822 "non-variable %qD in declaration with more than one "
12823 "declarator with placeholder type",
12824 TREE_CODE (decl) == FUNCTION_DECL
12825 ? decl : auto_function_declaration);
12826 auto_function_declaration = error_mark_node;
12830 if (auto_result
12831 && (!processing_template_decl || !type_uses_auto (auto_result)))
12833 if (last_type
12834 && last_type != error_mark_node
12835 && !same_type_p (auto_result, last_type))
12837 /* If the list of declarators contains more than one declarator,
12838 the type of each declared variable is determined as described
12839 above. If the type deduced for the template parameter U is not
12840 the same in each deduction, the program is ill-formed. */
12841 error_at (decl_specifiers.locations[ds_type_spec],
12842 "inconsistent deduction for %qT: %qT and then %qT",
12843 decl_specifiers.type, last_type, auto_result);
12844 last_type = error_mark_node;
12846 else
12847 last_type = auto_result;
12850 /* Handle function definitions specially. */
12851 if (function_definition_p)
12853 /* If the next token is a `,', then we are probably
12854 processing something like:
12856 void f() {}, *p;
12858 which is erroneous. */
12859 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12861 cp_token *token = cp_lexer_peek_token (parser->lexer);
12862 error_at (token->location,
12863 "mixing"
12864 " declarations and function-definitions is forbidden");
12866 /* Otherwise, we're done with the list of declarators. */
12867 else
12869 pop_deferring_access_checks ();
12870 return;
12873 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
12874 *maybe_range_for_decl = decl;
12875 /* The next token should be either a `,' or a `;'. */
12876 token = cp_lexer_peek_token (parser->lexer);
12877 /* If it's a `,', there are more declarators to come. */
12878 if (token->type == CPP_COMMA)
12879 /* will be consumed next time around */;
12880 /* If it's a `;', we are done. */
12881 else if (token->type == CPP_SEMICOLON)
12882 break;
12883 else if (maybe_range_for_decl)
12885 if (declares_class_or_enum && token->type == CPP_COLON)
12886 permerror (decl_specifiers.locations[ds_type_spec],
12887 "types may not be defined in a for-range-declaration");
12888 break;
12890 /* Anything else is an error. */
12891 else
12893 /* If we have already issued an error message we don't need
12894 to issue another one. */
12895 if ((decl != error_mark_node
12896 && DECL_INITIAL (decl) != error_mark_node)
12897 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12898 cp_parser_error (parser, "expected %<,%> or %<;%>");
12899 /* Skip tokens until we reach the end of the statement. */
12900 cp_parser_skip_to_end_of_statement (parser);
12901 /* If the next token is now a `;', consume it. */
12902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12903 cp_lexer_consume_token (parser->lexer);
12904 goto done;
12906 /* After the first time around, a function-definition is not
12907 allowed -- even if it was OK at first. For example:
12909 int i, f() {}
12911 is not valid. */
12912 function_definition_allowed_p = false;
12915 /* Issue an error message if no declarators are present, and the
12916 decl-specifier-seq does not itself declare a class or
12917 enumeration: [dcl.dcl]/3. */
12918 if (!saw_declarator)
12920 if (cp_parser_declares_only_class_p (parser))
12922 if (!declares_class_or_enum
12923 && decl_specifiers.type
12924 && OVERLOAD_TYPE_P (decl_specifiers.type))
12925 /* Ensure an error is issued anyway when finish_decltype_type,
12926 called via cp_parser_decl_specifier_seq, returns a class or
12927 an enumeration (c++/51786). */
12928 decl_specifiers.type = NULL_TREE;
12929 shadow_tag (&decl_specifiers);
12931 /* Perform any deferred access checks. */
12932 perform_deferred_access_checks (tf_warning_or_error);
12935 /* Consume the `;'. */
12936 finish:
12937 if (!maybe_range_for_decl)
12938 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12939 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12941 if (init_loc != UNKNOWN_LOCATION)
12942 error_at (init_loc, "initializer in range-based %<for%> loop");
12943 if (comma_loc != UNKNOWN_LOCATION)
12944 error_at (comma_loc,
12945 "multiple declarations in range-based %<for%> loop");
12948 done:
12949 pop_deferring_access_checks ();
12952 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
12953 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12954 initializer ; */
12956 static tree
12957 cp_parser_decomposition_declaration (cp_parser *parser,
12958 cp_decl_specifier_seq *decl_specifiers,
12959 tree *maybe_range_for_decl,
12960 location_t *init_loc)
12962 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
12963 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
12964 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
12966 /* Parse the identifier-list. */
12967 auto_vec<cp_expr, 10> v;
12968 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12969 while (true)
12971 cp_expr e = cp_parser_identifier (parser);
12972 if (e.get_value () == error_mark_node)
12973 break;
12974 v.safe_push (e);
12975 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12976 break;
12977 cp_lexer_consume_token (parser->lexer);
12980 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
12981 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
12983 end_loc = UNKNOWN_LOCATION;
12984 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
12985 false);
12986 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
12987 cp_lexer_consume_token (parser->lexer);
12988 else
12990 cp_parser_skip_to_end_of_statement (parser);
12991 return error_mark_node;
12995 if (cxx_dialect < cxx1z)
12996 pedwarn (loc, 0, "decomposition declaration only available with "
12997 "-std=c++1z or -std=gnu++1z");
12999 tree pushed_scope;
13000 cp_declarator *declarator = make_declarator (cdk_decomp);
13001 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13002 declarator->id_loc = loc;
13003 if (ref_qual != REF_QUAL_NONE)
13004 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13005 ref_qual == REF_QUAL_RVALUE,
13006 NULL_TREE);
13007 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13008 NULL_TREE, decl_specifiers->attributes,
13009 &pushed_scope);
13010 tree orig_decl = decl;
13012 unsigned int i;
13013 cp_expr e;
13014 cp_decl_specifier_seq decl_specs;
13015 clear_decl_specs (&decl_specs);
13016 decl_specs.type = make_auto ();
13017 tree prev = decl;
13018 FOR_EACH_VEC_ELT (v, i, e)
13020 if (i == 0)
13021 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13022 else
13023 declarator->u.id.unqualified_name = e.get_value ();
13024 declarator->id_loc = e.get_location ();
13025 tree elt_pushed_scope;
13026 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13027 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13028 if (decl2 == error_mark_node)
13029 decl = error_mark_node;
13030 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13032 /* Ensure we've diagnosed redeclaration if we aren't creating
13033 a new VAR_DECL. */
13034 gcc_assert (errorcount);
13035 decl = error_mark_node;
13037 else
13038 prev = decl2;
13039 if (elt_pushed_scope)
13040 pop_scope (elt_pushed_scope);
13043 if (v.is_empty ())
13045 error_at (loc, "empty decomposition declaration");
13046 decl = error_mark_node;
13049 if (maybe_range_for_decl == NULL
13050 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13052 bool non_constant_p = false, is_direct_init = false;
13053 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13054 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13055 &non_constant_p);
13057 if (decl != error_mark_node)
13059 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13060 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13061 cp_finish_decomp (decl, prev, v.length ());
13064 else if (decl != error_mark_node)
13066 *maybe_range_for_decl = prev;
13067 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13068 the underlying DECL. */
13069 cp_finish_decomp (decl, prev, v.length ());
13072 if (pushed_scope)
13073 pop_scope (pushed_scope);
13075 if (decl == error_mark_node && DECL_P (orig_decl))
13077 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13078 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13081 return decl;
13084 /* Parse a decl-specifier-seq.
13086 decl-specifier-seq:
13087 decl-specifier-seq [opt] decl-specifier
13088 decl-specifier attribute-specifier-seq [opt] (C++11)
13090 decl-specifier:
13091 storage-class-specifier
13092 type-specifier
13093 function-specifier
13094 friend
13095 typedef
13097 GNU Extension:
13099 decl-specifier:
13100 attributes
13102 Concepts Extension:
13104 decl-specifier:
13105 concept
13107 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13109 The parser flags FLAGS is used to control type-specifier parsing.
13111 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13112 flags:
13114 1: one of the decl-specifiers is an elaborated-type-specifier
13115 (i.e., a type declaration)
13116 2: one of the decl-specifiers is an enum-specifier or a
13117 class-specifier (i.e., a type definition)
13121 static void
13122 cp_parser_decl_specifier_seq (cp_parser* parser,
13123 cp_parser_flags flags,
13124 cp_decl_specifier_seq *decl_specs,
13125 int* declares_class_or_enum)
13127 bool constructor_possible_p = !parser->in_declarator_p;
13128 bool found_decl_spec = false;
13129 cp_token *start_token = NULL;
13130 cp_decl_spec ds;
13132 /* Clear DECL_SPECS. */
13133 clear_decl_specs (decl_specs);
13135 /* Assume no class or enumeration type is declared. */
13136 *declares_class_or_enum = 0;
13138 /* Keep reading specifiers until there are no more to read. */
13139 while (true)
13141 bool constructor_p;
13142 cp_token *token;
13143 ds = ds_last;
13145 /* Peek at the next token. */
13146 token = cp_lexer_peek_token (parser->lexer);
13148 /* Save the first token of the decl spec list for error
13149 reporting. */
13150 if (!start_token)
13151 start_token = token;
13152 /* Handle attributes. */
13153 if (cp_next_tokens_can_be_attribute_p (parser))
13155 /* Parse the attributes. */
13156 tree attrs = cp_parser_attributes_opt (parser);
13158 /* In a sequence of declaration specifiers, c++11 attributes
13159 appertain to the type that precede them. In that case
13160 [dcl.spec]/1 says:
13162 The attribute-specifier-seq affects the type only for
13163 the declaration it appears in, not other declarations
13164 involving the same type.
13166 But for now let's force the user to position the
13167 attribute either at the beginning of the declaration or
13168 after the declarator-id, which would clearly mean that it
13169 applies to the declarator. */
13170 if (cxx11_attribute_p (attrs))
13172 if (!found_decl_spec)
13173 /* The c++11 attribute is at the beginning of the
13174 declaration. It appertains to the entity being
13175 declared. */;
13176 else
13178 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13180 /* This is an attribute following a
13181 class-specifier. */
13182 if (decl_specs->type_definition_p)
13183 warn_misplaced_attr_for_class_type (token->location,
13184 decl_specs->type);
13185 attrs = NULL_TREE;
13187 else
13189 decl_specs->std_attributes
13190 = chainon (decl_specs->std_attributes,
13191 attrs);
13192 if (decl_specs->locations[ds_std_attribute] == 0)
13193 decl_specs->locations[ds_std_attribute] = token->location;
13195 continue;
13199 decl_specs->attributes
13200 = chainon (decl_specs->attributes,
13201 attrs);
13202 if (decl_specs->locations[ds_attribute] == 0)
13203 decl_specs->locations[ds_attribute] = token->location;
13204 continue;
13206 /* Assume we will find a decl-specifier keyword. */
13207 found_decl_spec = true;
13208 /* If the next token is an appropriate keyword, we can simply
13209 add it to the list. */
13210 switch (token->keyword)
13212 /* decl-specifier:
13213 friend
13214 constexpr */
13215 case RID_FRIEND:
13216 if (!at_class_scope_p ())
13218 error_at (token->location, "%<friend%> used outside of class");
13219 cp_lexer_purge_token (parser->lexer);
13221 else
13223 ds = ds_friend;
13224 /* Consume the token. */
13225 cp_lexer_consume_token (parser->lexer);
13227 break;
13229 case RID_CONSTEXPR:
13230 ds = ds_constexpr;
13231 cp_lexer_consume_token (parser->lexer);
13232 break;
13234 case RID_CONCEPT:
13235 ds = ds_concept;
13236 cp_lexer_consume_token (parser->lexer);
13237 break;
13239 /* function-specifier:
13240 inline
13241 virtual
13242 explicit */
13243 case RID_INLINE:
13244 case RID_VIRTUAL:
13245 case RID_EXPLICIT:
13246 cp_parser_function_specifier_opt (parser, decl_specs);
13247 break;
13249 /* decl-specifier:
13250 typedef */
13251 case RID_TYPEDEF:
13252 ds = ds_typedef;
13253 /* Consume the token. */
13254 cp_lexer_consume_token (parser->lexer);
13255 /* A constructor declarator cannot appear in a typedef. */
13256 constructor_possible_p = false;
13257 /* The "typedef" keyword can only occur in a declaration; we
13258 may as well commit at this point. */
13259 cp_parser_commit_to_tentative_parse (parser);
13261 if (decl_specs->storage_class != sc_none)
13262 decl_specs->conflicting_specifiers_p = true;
13263 break;
13265 /* storage-class-specifier:
13266 auto
13267 register
13268 static
13269 extern
13270 mutable
13272 GNU Extension:
13273 thread */
13274 case RID_AUTO:
13275 if (cxx_dialect == cxx98)
13277 /* Consume the token. */
13278 cp_lexer_consume_token (parser->lexer);
13280 /* Complain about `auto' as a storage specifier, if
13281 we're complaining about C++0x compatibility. */
13282 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13283 " changes meaning in C++11; please remove it");
13285 /* Set the storage class anyway. */
13286 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13287 token);
13289 else
13290 /* C++0x auto type-specifier. */
13291 found_decl_spec = false;
13292 break;
13294 case RID_REGISTER:
13295 case RID_STATIC:
13296 case RID_EXTERN:
13297 case RID_MUTABLE:
13298 /* Consume the token. */
13299 cp_lexer_consume_token (parser->lexer);
13300 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13301 token);
13302 break;
13303 case RID_THREAD:
13304 /* Consume the token. */
13305 ds = ds_thread;
13306 cp_lexer_consume_token (parser->lexer);
13307 break;
13309 default:
13310 /* We did not yet find a decl-specifier yet. */
13311 found_decl_spec = false;
13312 break;
13315 if (found_decl_spec
13316 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13317 && token->keyword != RID_CONSTEXPR)
13318 error ("decl-specifier invalid in condition");
13320 if (found_decl_spec
13321 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13322 && token->keyword != RID_MUTABLE
13323 && token->keyword != RID_CONSTEXPR)
13324 error_at (token->location, "%qD invalid in lambda",
13325 ridpointers[token->keyword]);
13327 if (ds != ds_last)
13328 set_and_check_decl_spec_loc (decl_specs, ds, token);
13330 /* Constructors are a special case. The `S' in `S()' is not a
13331 decl-specifier; it is the beginning of the declarator. */
13332 constructor_p
13333 = (!found_decl_spec
13334 && constructor_possible_p
13335 && (cp_parser_constructor_declarator_p
13336 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13338 /* If we don't have a DECL_SPEC yet, then we must be looking at
13339 a type-specifier. */
13340 if (!found_decl_spec && !constructor_p)
13342 int decl_spec_declares_class_or_enum;
13343 bool is_cv_qualifier;
13344 tree type_spec;
13346 type_spec
13347 = cp_parser_type_specifier (parser, flags,
13348 decl_specs,
13349 /*is_declaration=*/true,
13350 &decl_spec_declares_class_or_enum,
13351 &is_cv_qualifier);
13352 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13354 /* If this type-specifier referenced a user-defined type
13355 (a typedef, class-name, etc.), then we can't allow any
13356 more such type-specifiers henceforth.
13358 [dcl.spec]
13360 The longest sequence of decl-specifiers that could
13361 possibly be a type name is taken as the
13362 decl-specifier-seq of a declaration. The sequence shall
13363 be self-consistent as described below.
13365 [dcl.type]
13367 As a general rule, at most one type-specifier is allowed
13368 in the complete decl-specifier-seq of a declaration. The
13369 only exceptions are the following:
13371 -- const or volatile can be combined with any other
13372 type-specifier.
13374 -- signed or unsigned can be combined with char, long,
13375 short, or int.
13377 -- ..
13379 Example:
13381 typedef char* Pc;
13382 void g (const int Pc);
13384 Here, Pc is *not* part of the decl-specifier seq; it's
13385 the declarator. Therefore, once we see a type-specifier
13386 (other than a cv-qualifier), we forbid any additional
13387 user-defined types. We *do* still allow things like `int
13388 int' to be considered a decl-specifier-seq, and issue the
13389 error message later. */
13390 if (type_spec && !is_cv_qualifier)
13391 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13392 /* A constructor declarator cannot follow a type-specifier. */
13393 if (type_spec)
13395 constructor_possible_p = false;
13396 found_decl_spec = true;
13397 if (!is_cv_qualifier)
13398 decl_specs->any_type_specifiers_p = true;
13402 /* If we still do not have a DECL_SPEC, then there are no more
13403 decl-specifiers. */
13404 if (!found_decl_spec)
13405 break;
13407 decl_specs->any_specifiers_p = true;
13408 /* After we see one decl-specifier, further decl-specifiers are
13409 always optional. */
13410 flags |= CP_PARSER_FLAGS_OPTIONAL;
13413 /* Don't allow a friend specifier with a class definition. */
13414 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13415 && (*declares_class_or_enum & 2))
13416 error_at (decl_specs->locations[ds_friend],
13417 "class definition may not be declared a friend");
13420 /* Parse an (optional) storage-class-specifier.
13422 storage-class-specifier:
13423 auto
13424 register
13425 static
13426 extern
13427 mutable
13429 GNU Extension:
13431 storage-class-specifier:
13432 thread
13434 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13436 static tree
13437 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13439 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13441 case RID_AUTO:
13442 if (cxx_dialect != cxx98)
13443 return NULL_TREE;
13444 /* Fall through for C++98. */
13445 gcc_fallthrough ();
13447 case RID_REGISTER:
13448 case RID_STATIC:
13449 case RID_EXTERN:
13450 case RID_MUTABLE:
13451 case RID_THREAD:
13452 /* Consume the token. */
13453 return cp_lexer_consume_token (parser->lexer)->u.value;
13455 default:
13456 return NULL_TREE;
13460 /* Parse an (optional) function-specifier.
13462 function-specifier:
13463 inline
13464 virtual
13465 explicit
13467 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13468 Updates DECL_SPECS, if it is non-NULL. */
13470 static tree
13471 cp_parser_function_specifier_opt (cp_parser* parser,
13472 cp_decl_specifier_seq *decl_specs)
13474 cp_token *token = cp_lexer_peek_token (parser->lexer);
13475 switch (token->keyword)
13477 case RID_INLINE:
13478 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13479 break;
13481 case RID_VIRTUAL:
13482 /* 14.5.2.3 [temp.mem]
13484 A member function template shall not be virtual. */
13485 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13486 && current_class_type)
13487 error_at (token->location, "templates may not be %<virtual%>");
13488 else
13489 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13490 break;
13492 case RID_EXPLICIT:
13493 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13494 break;
13496 default:
13497 return NULL_TREE;
13500 /* Consume the token. */
13501 return cp_lexer_consume_token (parser->lexer)->u.value;
13504 /* Parse a linkage-specification.
13506 linkage-specification:
13507 extern string-literal { declaration-seq [opt] }
13508 extern string-literal declaration */
13510 static void
13511 cp_parser_linkage_specification (cp_parser* parser)
13513 tree linkage;
13515 /* Look for the `extern' keyword. */
13516 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13518 /* Look for the string-literal. */
13519 linkage = cp_parser_string_literal (parser, false, false);
13521 /* Transform the literal into an identifier. If the literal is a
13522 wide-character string, or contains embedded NULs, then we can't
13523 handle it as the user wants. */
13524 if (strlen (TREE_STRING_POINTER (linkage))
13525 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13527 cp_parser_error (parser, "invalid linkage-specification");
13528 /* Assume C++ linkage. */
13529 linkage = lang_name_cplusplus;
13531 else
13532 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13534 /* We're now using the new linkage. */
13535 push_lang_context (linkage);
13537 /* If the next token is a `{', then we're using the first
13538 production. */
13539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13541 cp_ensure_no_omp_declare_simd (parser);
13542 cp_ensure_no_oacc_routine (parser);
13544 /* Consume the `{' token. */
13545 cp_lexer_consume_token (parser->lexer);
13546 /* Parse the declarations. */
13547 cp_parser_declaration_seq_opt (parser);
13548 /* Look for the closing `}'. */
13549 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13551 /* Otherwise, there's just one declaration. */
13552 else
13554 bool saved_in_unbraced_linkage_specification_p;
13556 saved_in_unbraced_linkage_specification_p
13557 = parser->in_unbraced_linkage_specification_p;
13558 parser->in_unbraced_linkage_specification_p = true;
13559 cp_parser_declaration (parser);
13560 parser->in_unbraced_linkage_specification_p
13561 = saved_in_unbraced_linkage_specification_p;
13564 /* We're done with the linkage-specification. */
13565 pop_lang_context ();
13568 /* Parse a static_assert-declaration.
13570 static_assert-declaration:
13571 static_assert ( constant-expression , string-literal ) ;
13572 static_assert ( constant-expression ) ; (C++1Z)
13574 If MEMBER_P, this static_assert is a class member. */
13576 static void
13577 cp_parser_static_assert(cp_parser *parser, bool member_p)
13579 tree condition;
13580 tree message;
13581 cp_token *token;
13582 location_t saved_loc;
13583 bool dummy;
13585 /* Peek at the `static_assert' token so we can keep track of exactly
13586 where the static assertion started. */
13587 token = cp_lexer_peek_token (parser->lexer);
13588 saved_loc = token->location;
13590 /* Look for the `static_assert' keyword. */
13591 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13592 RT_STATIC_ASSERT))
13593 return;
13595 /* We know we are in a static assertion; commit to any tentative
13596 parse. */
13597 if (cp_parser_parsing_tentatively (parser))
13598 cp_parser_commit_to_tentative_parse (parser);
13600 /* Parse the `(' starting the static assertion condition. */
13601 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13603 /* Parse the constant-expression. Allow a non-constant expression
13604 here in order to give better diagnostics in finish_static_assert. */
13605 condition =
13606 cp_parser_constant_expression (parser,
13607 /*allow_non_constant_p=*/true,
13608 /*non_constant_p=*/&dummy);
13610 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13612 if (cxx_dialect < cxx1z)
13613 pedwarn (input_location, OPT_Wpedantic,
13614 "static_assert without a message "
13615 "only available with -std=c++1z or -std=gnu++1z");
13616 /* Eat the ')' */
13617 cp_lexer_consume_token (parser->lexer);
13618 message = build_string (1, "");
13619 TREE_TYPE (message) = char_array_type_node;
13620 fix_string_type (message);
13622 else
13624 /* Parse the separating `,'. */
13625 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13627 /* Parse the string-literal message. */
13628 message = cp_parser_string_literal (parser,
13629 /*translate=*/false,
13630 /*wide_ok=*/true);
13632 /* A `)' completes the static assertion. */
13633 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13634 cp_parser_skip_to_closing_parenthesis (parser,
13635 /*recovering=*/true,
13636 /*or_comma=*/false,
13637 /*consume_paren=*/true);
13640 /* A semicolon terminates the declaration. */
13641 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13643 /* Complete the static assertion, which may mean either processing
13644 the static assert now or saving it for template instantiation. */
13645 finish_static_assert (condition, message, saved_loc, member_p);
13648 /* Parse the expression in decltype ( expression ). */
13650 static tree
13651 cp_parser_decltype_expr (cp_parser *parser,
13652 bool &id_expression_or_member_access_p)
13654 cp_token *id_expr_start_token;
13655 tree expr;
13657 /* Since we're going to preserve any side-effects from this parse, set up a
13658 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13659 in the expression. */
13660 tentative_firewall firewall (parser);
13662 /* First, try parsing an id-expression. */
13663 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13664 cp_parser_parse_tentatively (parser);
13665 expr = cp_parser_id_expression (parser,
13666 /*template_keyword_p=*/false,
13667 /*check_dependency_p=*/true,
13668 /*template_p=*/NULL,
13669 /*declarator_p=*/false,
13670 /*optional_p=*/false);
13672 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13674 bool non_integral_constant_expression_p = false;
13675 tree id_expression = expr;
13676 cp_id_kind idk;
13677 const char *error_msg;
13679 if (identifier_p (expr))
13680 /* Lookup the name we got back from the id-expression. */
13681 expr = cp_parser_lookup_name_simple (parser, expr,
13682 id_expr_start_token->location);
13684 if (expr
13685 && expr != error_mark_node
13686 && TREE_CODE (expr) != TYPE_DECL
13687 && (TREE_CODE (expr) != BIT_NOT_EXPR
13688 || !TYPE_P (TREE_OPERAND (expr, 0)))
13689 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13691 /* Complete lookup of the id-expression. */
13692 expr = (finish_id_expression
13693 (id_expression, expr, parser->scope, &idk,
13694 /*integral_constant_expression_p=*/false,
13695 /*allow_non_integral_constant_expression_p=*/true,
13696 &non_integral_constant_expression_p,
13697 /*template_p=*/false,
13698 /*done=*/true,
13699 /*address_p=*/false,
13700 /*template_arg_p=*/false,
13701 &error_msg,
13702 id_expr_start_token->location));
13704 if (expr == error_mark_node)
13705 /* We found an id-expression, but it was something that we
13706 should not have found. This is an error, not something
13707 we can recover from, so note that we found an
13708 id-expression and we'll recover as gracefully as
13709 possible. */
13710 id_expression_or_member_access_p = true;
13713 if (expr
13714 && expr != error_mark_node
13715 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13716 /* We have an id-expression. */
13717 id_expression_or_member_access_p = true;
13720 if (!id_expression_or_member_access_p)
13722 /* Abort the id-expression parse. */
13723 cp_parser_abort_tentative_parse (parser);
13725 /* Parsing tentatively, again. */
13726 cp_parser_parse_tentatively (parser);
13728 /* Parse a class member access. */
13729 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13730 /*cast_p=*/false, /*decltype*/true,
13731 /*member_access_only_p=*/true, NULL);
13733 if (expr
13734 && expr != error_mark_node
13735 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13736 /* We have an id-expression. */
13737 id_expression_or_member_access_p = true;
13740 if (id_expression_or_member_access_p)
13741 /* We have parsed the complete id-expression or member access. */
13742 cp_parser_parse_definitely (parser);
13743 else
13745 /* Abort our attempt to parse an id-expression or member access
13746 expression. */
13747 cp_parser_abort_tentative_parse (parser);
13749 /* Parse a full expression. */
13750 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13751 /*decltype_p=*/true);
13754 return expr;
13757 /* Parse a `decltype' type. Returns the type.
13759 simple-type-specifier:
13760 decltype ( expression )
13761 C++14 proposal:
13762 decltype ( auto ) */
13764 static tree
13765 cp_parser_decltype (cp_parser *parser)
13767 tree expr;
13768 bool id_expression_or_member_access_p = false;
13769 const char *saved_message;
13770 bool saved_integral_constant_expression_p;
13771 bool saved_non_integral_constant_expression_p;
13772 bool saved_greater_than_is_operator_p;
13773 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13775 if (start_token->type == CPP_DECLTYPE)
13777 /* Already parsed. */
13778 cp_lexer_consume_token (parser->lexer);
13779 return saved_checks_value (start_token->u.tree_check_value);
13782 /* Look for the `decltype' token. */
13783 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13784 return error_mark_node;
13786 /* Parse the opening `('. */
13787 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13788 return error_mark_node;
13790 /* decltype (auto) */
13791 if (cxx_dialect >= cxx14
13792 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13794 cp_lexer_consume_token (parser->lexer);
13795 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13796 return error_mark_node;
13797 expr = make_decltype_auto ();
13798 AUTO_IS_DECLTYPE (expr) = true;
13799 goto rewrite;
13802 /* Types cannot be defined in a `decltype' expression. Save away the
13803 old message. */
13804 saved_message = parser->type_definition_forbidden_message;
13806 /* And create the new one. */
13807 parser->type_definition_forbidden_message
13808 = G_("types may not be defined in %<decltype%> expressions");
13810 /* The restrictions on constant-expressions do not apply inside
13811 decltype expressions. */
13812 saved_integral_constant_expression_p
13813 = parser->integral_constant_expression_p;
13814 saved_non_integral_constant_expression_p
13815 = parser->non_integral_constant_expression_p;
13816 parser->integral_constant_expression_p = false;
13818 /* Within a parenthesized expression, a `>' token is always
13819 the greater-than operator. */
13820 saved_greater_than_is_operator_p
13821 = parser->greater_than_is_operator_p;
13822 parser->greater_than_is_operator_p = true;
13824 /* Do not actually evaluate the expression. */
13825 ++cp_unevaluated_operand;
13827 /* Do not warn about problems with the expression. */
13828 ++c_inhibit_evaluation_warnings;
13830 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13832 /* Go back to evaluating expressions. */
13833 --cp_unevaluated_operand;
13834 --c_inhibit_evaluation_warnings;
13836 /* The `>' token might be the end of a template-id or
13837 template-parameter-list now. */
13838 parser->greater_than_is_operator_p
13839 = saved_greater_than_is_operator_p;
13841 /* Restore the old message and the integral constant expression
13842 flags. */
13843 parser->type_definition_forbidden_message = saved_message;
13844 parser->integral_constant_expression_p
13845 = saved_integral_constant_expression_p;
13846 parser->non_integral_constant_expression_p
13847 = saved_non_integral_constant_expression_p;
13849 /* Parse to the closing `)'. */
13850 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13852 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13853 /*consume_paren=*/true);
13854 return error_mark_node;
13857 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13858 tf_warning_or_error);
13860 rewrite:
13861 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13862 it again. */
13863 start_token->type = CPP_DECLTYPE;
13864 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13865 start_token->u.tree_check_value->value = expr;
13866 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13867 start_token->keyword = RID_MAX;
13868 cp_lexer_purge_tokens_after (parser->lexer, start_token);
13870 return expr;
13873 /* Special member functions [gram.special] */
13875 /* Parse a conversion-function-id.
13877 conversion-function-id:
13878 operator conversion-type-id
13880 Returns an IDENTIFIER_NODE representing the operator. */
13882 static tree
13883 cp_parser_conversion_function_id (cp_parser* parser)
13885 tree type;
13886 tree saved_scope;
13887 tree saved_qualifying_scope;
13888 tree saved_object_scope;
13889 tree pushed_scope = NULL_TREE;
13891 /* Look for the `operator' token. */
13892 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
13893 return error_mark_node;
13894 /* When we parse the conversion-type-id, the current scope will be
13895 reset. However, we need that information in able to look up the
13896 conversion function later, so we save it here. */
13897 saved_scope = parser->scope;
13898 saved_qualifying_scope = parser->qualifying_scope;
13899 saved_object_scope = parser->object_scope;
13900 /* We must enter the scope of the class so that the names of
13901 entities declared within the class are available in the
13902 conversion-type-id. For example, consider:
13904 struct S {
13905 typedef int I;
13906 operator I();
13909 S::operator I() { ... }
13911 In order to see that `I' is a type-name in the definition, we
13912 must be in the scope of `S'. */
13913 if (saved_scope)
13914 pushed_scope = push_scope (saved_scope);
13915 /* Parse the conversion-type-id. */
13916 type = cp_parser_conversion_type_id (parser);
13917 /* Leave the scope of the class, if any. */
13918 if (pushed_scope)
13919 pop_scope (pushed_scope);
13920 /* Restore the saved scope. */
13921 parser->scope = saved_scope;
13922 parser->qualifying_scope = saved_qualifying_scope;
13923 parser->object_scope = saved_object_scope;
13924 /* If the TYPE is invalid, indicate failure. */
13925 if (type == error_mark_node)
13926 return error_mark_node;
13927 return mangle_conv_op_name_for_type (type);
13930 /* Parse a conversion-type-id:
13932 conversion-type-id:
13933 type-specifier-seq conversion-declarator [opt]
13935 Returns the TYPE specified. */
13937 static tree
13938 cp_parser_conversion_type_id (cp_parser* parser)
13940 tree attributes;
13941 cp_decl_specifier_seq type_specifiers;
13942 cp_declarator *declarator;
13943 tree type_specified;
13944 const char *saved_message;
13946 /* Parse the attributes. */
13947 attributes = cp_parser_attributes_opt (parser);
13949 saved_message = parser->type_definition_forbidden_message;
13950 parser->type_definition_forbidden_message
13951 = G_("types may not be defined in a conversion-type-id");
13953 /* Parse the type-specifiers. */
13954 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13955 /*is_trailing_return=*/false,
13956 &type_specifiers);
13958 parser->type_definition_forbidden_message = saved_message;
13960 /* If that didn't work, stop. */
13961 if (type_specifiers.type == error_mark_node)
13962 return error_mark_node;
13963 /* Parse the conversion-declarator. */
13964 declarator = cp_parser_conversion_declarator_opt (parser);
13966 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
13967 /*initialized=*/0, &attributes);
13968 if (attributes)
13969 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
13971 /* Don't give this error when parsing tentatively. This happens to
13972 work because we always parse this definitively once. */
13973 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
13974 && type_uses_auto (type_specified))
13976 if (cxx_dialect < cxx14)
13978 error ("invalid use of %<auto%> in conversion operator");
13979 return error_mark_node;
13981 else if (template_parm_scope_p ())
13982 warning (0, "use of %<auto%> in member template "
13983 "conversion operator can never be deduced");
13986 return type_specified;
13989 /* Parse an (optional) conversion-declarator.
13991 conversion-declarator:
13992 ptr-operator conversion-declarator [opt]
13996 static cp_declarator *
13997 cp_parser_conversion_declarator_opt (cp_parser* parser)
13999 enum tree_code code;
14000 tree class_type, std_attributes = NULL_TREE;
14001 cp_cv_quals cv_quals;
14003 /* We don't know if there's a ptr-operator next, or not. */
14004 cp_parser_parse_tentatively (parser);
14005 /* Try the ptr-operator. */
14006 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14007 &std_attributes);
14008 /* If it worked, look for more conversion-declarators. */
14009 if (cp_parser_parse_definitely (parser))
14011 cp_declarator *declarator;
14013 /* Parse another optional declarator. */
14014 declarator = cp_parser_conversion_declarator_opt (parser);
14016 declarator = cp_parser_make_indirect_declarator
14017 (code, class_type, cv_quals, declarator, std_attributes);
14019 return declarator;
14022 return NULL;
14025 /* Parse an (optional) ctor-initializer.
14027 ctor-initializer:
14028 : mem-initializer-list
14030 Returns TRUE iff the ctor-initializer was actually present. */
14032 static bool
14033 cp_parser_ctor_initializer_opt (cp_parser* parser)
14035 /* If the next token is not a `:', then there is no
14036 ctor-initializer. */
14037 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14039 /* Do default initialization of any bases and members. */
14040 if (DECL_CONSTRUCTOR_P (current_function_decl))
14041 finish_mem_initializers (NULL_TREE);
14043 return false;
14046 /* Consume the `:' token. */
14047 cp_lexer_consume_token (parser->lexer);
14048 /* And the mem-initializer-list. */
14049 cp_parser_mem_initializer_list (parser);
14051 return true;
14054 /* Parse a mem-initializer-list.
14056 mem-initializer-list:
14057 mem-initializer ... [opt]
14058 mem-initializer ... [opt] , mem-initializer-list */
14060 static void
14061 cp_parser_mem_initializer_list (cp_parser* parser)
14063 tree mem_initializer_list = NULL_TREE;
14064 tree target_ctor = error_mark_node;
14065 cp_token *token = cp_lexer_peek_token (parser->lexer);
14067 /* Let the semantic analysis code know that we are starting the
14068 mem-initializer-list. */
14069 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14070 error_at (token->location,
14071 "only constructors take member initializers");
14073 /* Loop through the list. */
14074 while (true)
14076 tree mem_initializer;
14078 token = cp_lexer_peek_token (parser->lexer);
14079 /* Parse the mem-initializer. */
14080 mem_initializer = cp_parser_mem_initializer (parser);
14081 /* If the next token is a `...', we're expanding member initializers. */
14082 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14084 /* Consume the `...'. */
14085 cp_lexer_consume_token (parser->lexer);
14087 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14088 can be expanded but members cannot. */
14089 if (mem_initializer != error_mark_node
14090 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14092 error_at (token->location,
14093 "cannot expand initializer for member %<%D%>",
14094 TREE_PURPOSE (mem_initializer));
14095 mem_initializer = error_mark_node;
14098 /* Construct the pack expansion type. */
14099 if (mem_initializer != error_mark_node)
14100 mem_initializer = make_pack_expansion (mem_initializer);
14102 if (target_ctor != error_mark_node
14103 && mem_initializer != error_mark_node)
14105 error ("mem-initializer for %qD follows constructor delegation",
14106 TREE_PURPOSE (mem_initializer));
14107 mem_initializer = error_mark_node;
14109 /* Look for a target constructor. */
14110 if (mem_initializer != error_mark_node
14111 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14112 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14114 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14115 if (mem_initializer_list)
14117 error ("constructor delegation follows mem-initializer for %qD",
14118 TREE_PURPOSE (mem_initializer_list));
14119 mem_initializer = error_mark_node;
14121 target_ctor = mem_initializer;
14123 /* Add it to the list, unless it was erroneous. */
14124 if (mem_initializer != error_mark_node)
14126 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14127 mem_initializer_list = mem_initializer;
14129 /* If the next token is not a `,', we're done. */
14130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14131 break;
14132 /* Consume the `,' token. */
14133 cp_lexer_consume_token (parser->lexer);
14136 /* Perform semantic analysis. */
14137 if (DECL_CONSTRUCTOR_P (current_function_decl))
14138 finish_mem_initializers (mem_initializer_list);
14141 /* Parse a mem-initializer.
14143 mem-initializer:
14144 mem-initializer-id ( expression-list [opt] )
14145 mem-initializer-id braced-init-list
14147 GNU extension:
14149 mem-initializer:
14150 ( expression-list [opt] )
14152 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14153 class) or FIELD_DECL (for a non-static data member) to initialize;
14154 the TREE_VALUE is the expression-list. An empty initialization
14155 list is represented by void_list_node. */
14157 static tree
14158 cp_parser_mem_initializer (cp_parser* parser)
14160 tree mem_initializer_id;
14161 tree expression_list;
14162 tree member;
14163 cp_token *token = cp_lexer_peek_token (parser->lexer);
14165 /* Find out what is being initialized. */
14166 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14168 permerror (token->location,
14169 "anachronistic old-style base class initializer");
14170 mem_initializer_id = NULL_TREE;
14172 else
14174 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14175 if (mem_initializer_id == error_mark_node)
14176 return mem_initializer_id;
14178 member = expand_member_init (mem_initializer_id);
14179 if (member && !DECL_P (member))
14180 in_base_initializer = 1;
14182 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14184 bool expr_non_constant_p;
14185 cp_lexer_set_source_position (parser->lexer);
14186 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14187 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14188 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14189 expression_list = build_tree_list (NULL_TREE, expression_list);
14191 else
14193 vec<tree, va_gc> *vec;
14194 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14195 /*cast_p=*/false,
14196 /*allow_expansion_p=*/true,
14197 /*non_constant_p=*/NULL);
14198 if (vec == NULL)
14199 return error_mark_node;
14200 expression_list = build_tree_list_vec (vec);
14201 release_tree_vector (vec);
14204 if (expression_list == error_mark_node)
14205 return error_mark_node;
14206 if (!expression_list)
14207 expression_list = void_type_node;
14209 in_base_initializer = 0;
14211 return member ? build_tree_list (member, expression_list) : error_mark_node;
14214 /* Parse a mem-initializer-id.
14216 mem-initializer-id:
14217 :: [opt] nested-name-specifier [opt] class-name
14218 decltype-specifier (C++11)
14219 identifier
14221 Returns a TYPE indicating the class to be initialized for the first
14222 production (and the second in C++11). Returns an IDENTIFIER_NODE
14223 indicating the data member to be initialized for the last production. */
14225 static tree
14226 cp_parser_mem_initializer_id (cp_parser* parser)
14228 bool global_scope_p;
14229 bool nested_name_specifier_p;
14230 bool template_p = false;
14231 tree id;
14233 cp_token *token = cp_lexer_peek_token (parser->lexer);
14235 /* `typename' is not allowed in this context ([temp.res]). */
14236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14238 error_at (token->location,
14239 "keyword %<typename%> not allowed in this context (a qualified "
14240 "member initializer is implicitly a type)");
14241 cp_lexer_consume_token (parser->lexer);
14243 /* Look for the optional `::' operator. */
14244 global_scope_p
14245 = (cp_parser_global_scope_opt (parser,
14246 /*current_scope_valid_p=*/false)
14247 != NULL_TREE);
14248 /* Look for the optional nested-name-specifier. The simplest way to
14249 implement:
14251 [temp.res]
14253 The keyword `typename' is not permitted in a base-specifier or
14254 mem-initializer; in these contexts a qualified name that
14255 depends on a template-parameter is implicitly assumed to be a
14256 type name.
14258 is to assume that we have seen the `typename' keyword at this
14259 point. */
14260 nested_name_specifier_p
14261 = (cp_parser_nested_name_specifier_opt (parser,
14262 /*typename_keyword_p=*/true,
14263 /*check_dependency_p=*/true,
14264 /*type_p=*/true,
14265 /*is_declaration=*/true)
14266 != NULL_TREE);
14267 if (nested_name_specifier_p)
14268 template_p = cp_parser_optional_template_keyword (parser);
14269 /* If there is a `::' operator or a nested-name-specifier, then we
14270 are definitely looking for a class-name. */
14271 if (global_scope_p || nested_name_specifier_p)
14272 return cp_parser_class_name (parser,
14273 /*typename_keyword_p=*/true,
14274 /*template_keyword_p=*/template_p,
14275 typename_type,
14276 /*check_dependency_p=*/true,
14277 /*class_head_p=*/false,
14278 /*is_declaration=*/true);
14279 /* Otherwise, we could also be looking for an ordinary identifier. */
14280 cp_parser_parse_tentatively (parser);
14281 if (cp_lexer_next_token_is_decltype (parser->lexer))
14282 /* Try a decltype-specifier. */
14283 id = cp_parser_decltype (parser);
14284 else
14285 /* Otherwise, try a class-name. */
14286 id = cp_parser_class_name (parser,
14287 /*typename_keyword_p=*/true,
14288 /*template_keyword_p=*/false,
14289 none_type,
14290 /*check_dependency_p=*/true,
14291 /*class_head_p=*/false,
14292 /*is_declaration=*/true);
14293 /* If we found one, we're done. */
14294 if (cp_parser_parse_definitely (parser))
14295 return id;
14296 /* Otherwise, look for an ordinary identifier. */
14297 return cp_parser_identifier (parser);
14300 /* Overloading [gram.over] */
14302 /* Parse an operator-function-id.
14304 operator-function-id:
14305 operator operator
14307 Returns an IDENTIFIER_NODE for the operator which is a
14308 human-readable spelling of the identifier, e.g., `operator +'. */
14310 static cp_expr
14311 cp_parser_operator_function_id (cp_parser* parser)
14313 /* Look for the `operator' keyword. */
14314 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14315 return error_mark_node;
14316 /* And then the name of the operator itself. */
14317 return cp_parser_operator (parser);
14320 /* Return an identifier node for a user-defined literal operator.
14321 The suffix identifier is chained to the operator name identifier. */
14323 tree
14324 cp_literal_operator_id (const char* name)
14326 tree identifier;
14327 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14328 + strlen (name) + 10);
14329 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14330 identifier = get_identifier (buffer);
14332 return identifier;
14335 /* Parse an operator.
14337 operator:
14338 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14339 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14340 || ++ -- , ->* -> () []
14342 GNU Extensions:
14344 operator:
14345 <? >? <?= >?=
14347 Returns an IDENTIFIER_NODE for the operator which is a
14348 human-readable spelling of the identifier, e.g., `operator +'. */
14350 static cp_expr
14351 cp_parser_operator (cp_parser* parser)
14353 tree id = NULL_TREE;
14354 cp_token *token;
14355 bool utf8 = false;
14357 /* Peek at the next token. */
14358 token = cp_lexer_peek_token (parser->lexer);
14360 location_t start_loc = token->location;
14362 /* Figure out which operator we have. */
14363 switch (token->type)
14365 case CPP_KEYWORD:
14367 enum tree_code op;
14369 /* The keyword should be either `new' or `delete'. */
14370 if (token->keyword == RID_NEW)
14371 op = NEW_EXPR;
14372 else if (token->keyword == RID_DELETE)
14373 op = DELETE_EXPR;
14374 else
14375 break;
14377 /* Consume the `new' or `delete' token. */
14378 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14380 /* Peek at the next token. */
14381 token = cp_lexer_peek_token (parser->lexer);
14382 /* If it's a `[' token then this is the array variant of the
14383 operator. */
14384 if (token->type == CPP_OPEN_SQUARE)
14386 /* Consume the `[' token. */
14387 cp_lexer_consume_token (parser->lexer);
14388 /* Look for the `]' token. */
14389 if (cp_token *close_token
14390 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14391 end_loc = close_token->location;
14392 id = cp_operator_id (op == NEW_EXPR
14393 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14395 /* Otherwise, we have the non-array variant. */
14396 else
14397 id = cp_operator_id (op);
14399 location_t loc = make_location (start_loc, start_loc, end_loc);
14401 return cp_expr (id, loc);
14404 case CPP_PLUS:
14405 id = cp_operator_id (PLUS_EXPR);
14406 break;
14408 case CPP_MINUS:
14409 id = cp_operator_id (MINUS_EXPR);
14410 break;
14412 case CPP_MULT:
14413 id = cp_operator_id (MULT_EXPR);
14414 break;
14416 case CPP_DIV:
14417 id = cp_operator_id (TRUNC_DIV_EXPR);
14418 break;
14420 case CPP_MOD:
14421 id = cp_operator_id (TRUNC_MOD_EXPR);
14422 break;
14424 case CPP_XOR:
14425 id = cp_operator_id (BIT_XOR_EXPR);
14426 break;
14428 case CPP_AND:
14429 id = cp_operator_id (BIT_AND_EXPR);
14430 break;
14432 case CPP_OR:
14433 id = cp_operator_id (BIT_IOR_EXPR);
14434 break;
14436 case CPP_COMPL:
14437 id = cp_operator_id (BIT_NOT_EXPR);
14438 break;
14440 case CPP_NOT:
14441 id = cp_operator_id (TRUTH_NOT_EXPR);
14442 break;
14444 case CPP_EQ:
14445 id = cp_assignment_operator_id (NOP_EXPR);
14446 break;
14448 case CPP_LESS:
14449 id = cp_operator_id (LT_EXPR);
14450 break;
14452 case CPP_GREATER:
14453 id = cp_operator_id (GT_EXPR);
14454 break;
14456 case CPP_PLUS_EQ:
14457 id = cp_assignment_operator_id (PLUS_EXPR);
14458 break;
14460 case CPP_MINUS_EQ:
14461 id = cp_assignment_operator_id (MINUS_EXPR);
14462 break;
14464 case CPP_MULT_EQ:
14465 id = cp_assignment_operator_id (MULT_EXPR);
14466 break;
14468 case CPP_DIV_EQ:
14469 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14470 break;
14472 case CPP_MOD_EQ:
14473 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14474 break;
14476 case CPP_XOR_EQ:
14477 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14478 break;
14480 case CPP_AND_EQ:
14481 id = cp_assignment_operator_id (BIT_AND_EXPR);
14482 break;
14484 case CPP_OR_EQ:
14485 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14486 break;
14488 case CPP_LSHIFT:
14489 id = cp_operator_id (LSHIFT_EXPR);
14490 break;
14492 case CPP_RSHIFT:
14493 id = cp_operator_id (RSHIFT_EXPR);
14494 break;
14496 case CPP_LSHIFT_EQ:
14497 id = cp_assignment_operator_id (LSHIFT_EXPR);
14498 break;
14500 case CPP_RSHIFT_EQ:
14501 id = cp_assignment_operator_id (RSHIFT_EXPR);
14502 break;
14504 case CPP_EQ_EQ:
14505 id = cp_operator_id (EQ_EXPR);
14506 break;
14508 case CPP_NOT_EQ:
14509 id = cp_operator_id (NE_EXPR);
14510 break;
14512 case CPP_LESS_EQ:
14513 id = cp_operator_id (LE_EXPR);
14514 break;
14516 case CPP_GREATER_EQ:
14517 id = cp_operator_id (GE_EXPR);
14518 break;
14520 case CPP_AND_AND:
14521 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14522 break;
14524 case CPP_OR_OR:
14525 id = cp_operator_id (TRUTH_ORIF_EXPR);
14526 break;
14528 case CPP_PLUS_PLUS:
14529 id = cp_operator_id (POSTINCREMENT_EXPR);
14530 break;
14532 case CPP_MINUS_MINUS:
14533 id = cp_operator_id (PREDECREMENT_EXPR);
14534 break;
14536 case CPP_COMMA:
14537 id = cp_operator_id (COMPOUND_EXPR);
14538 break;
14540 case CPP_DEREF_STAR:
14541 id = cp_operator_id (MEMBER_REF);
14542 break;
14544 case CPP_DEREF:
14545 id = cp_operator_id (COMPONENT_REF);
14546 break;
14548 case CPP_OPEN_PAREN:
14549 /* Consume the `('. */
14550 cp_lexer_consume_token (parser->lexer);
14551 /* Look for the matching `)'. */
14552 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14553 return cp_operator_id (CALL_EXPR);
14555 case CPP_OPEN_SQUARE:
14556 /* Consume the `['. */
14557 cp_lexer_consume_token (parser->lexer);
14558 /* Look for the matching `]'. */
14559 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14560 return cp_operator_id (ARRAY_REF);
14562 case CPP_UTF8STRING:
14563 case CPP_UTF8STRING_USERDEF:
14564 utf8 = true;
14565 /* FALLTHRU */
14566 case CPP_STRING:
14567 case CPP_WSTRING:
14568 case CPP_STRING16:
14569 case CPP_STRING32:
14570 case CPP_STRING_USERDEF:
14571 case CPP_WSTRING_USERDEF:
14572 case CPP_STRING16_USERDEF:
14573 case CPP_STRING32_USERDEF:
14575 tree str, string_tree;
14576 int sz, len;
14578 if (cxx_dialect == cxx98)
14579 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14581 /* Consume the string. */
14582 str = cp_parser_string_literal (parser, /*translate=*/true,
14583 /*wide_ok=*/true, /*lookup_udlit=*/false);
14584 if (str == error_mark_node)
14585 return error_mark_node;
14586 else if (TREE_CODE (str) == USERDEF_LITERAL)
14588 string_tree = USERDEF_LITERAL_VALUE (str);
14589 id = USERDEF_LITERAL_SUFFIX_ID (str);
14591 else
14593 string_tree = str;
14594 /* Look for the suffix identifier. */
14595 token = cp_lexer_peek_token (parser->lexer);
14596 if (token->type == CPP_NAME)
14597 id = cp_parser_identifier (parser);
14598 else if (token->type == CPP_KEYWORD)
14600 error ("unexpected keyword;"
14601 " remove space between quotes and suffix identifier");
14602 return error_mark_node;
14604 else
14606 error ("expected suffix identifier");
14607 return error_mark_node;
14610 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14611 (TREE_TYPE (TREE_TYPE (string_tree))));
14612 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14613 if (len != 0)
14615 error ("expected empty string after %<operator%> keyword");
14616 return error_mark_node;
14618 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14619 != char_type_node)
14621 error ("invalid encoding prefix in literal operator");
14622 return error_mark_node;
14624 if (id != error_mark_node)
14626 const char *name = IDENTIFIER_POINTER (id);
14627 id = cp_literal_operator_id (name);
14629 return id;
14632 default:
14633 /* Anything else is an error. */
14634 break;
14637 /* If we have selected an identifier, we need to consume the
14638 operator token. */
14639 if (id)
14640 cp_lexer_consume_token (parser->lexer);
14641 /* Otherwise, no valid operator name was present. */
14642 else
14644 cp_parser_error (parser, "expected operator");
14645 id = error_mark_node;
14648 return cp_expr (id, start_loc);
14651 /* Parse a template-declaration.
14653 template-declaration:
14654 export [opt] template < template-parameter-list > declaration
14656 If MEMBER_P is TRUE, this template-declaration occurs within a
14657 class-specifier.
14659 The grammar rule given by the standard isn't correct. What
14660 is really meant is:
14662 template-declaration:
14663 export [opt] template-parameter-list-seq
14664 decl-specifier-seq [opt] init-declarator [opt] ;
14665 export [opt] template-parameter-list-seq
14666 function-definition
14668 template-parameter-list-seq:
14669 template-parameter-list-seq [opt]
14670 template < template-parameter-list >
14672 Concept Extensions:
14674 template-parameter-list-seq:
14675 template < template-parameter-list > requires-clause [opt]
14677 requires-clause:
14678 requires logical-or-expression */
14680 static void
14681 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14683 /* Check for `export'. */
14684 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14686 /* Consume the `export' token. */
14687 cp_lexer_consume_token (parser->lexer);
14688 /* Warn that we do not support `export'. */
14689 warning (0, "keyword %<export%> not implemented, and will be ignored");
14692 cp_parser_template_declaration_after_export (parser, member_p);
14695 /* Parse a template-parameter-list.
14697 template-parameter-list:
14698 template-parameter
14699 template-parameter-list , template-parameter
14701 Returns a TREE_LIST. Each node represents a template parameter.
14702 The nodes are connected via their TREE_CHAINs. */
14704 static tree
14705 cp_parser_template_parameter_list (cp_parser* parser)
14707 tree parameter_list = NULL_TREE;
14709 begin_template_parm_list ();
14711 /* The loop below parses the template parms. We first need to know
14712 the total number of template parms to be able to compute proper
14713 canonical types of each dependent type. So after the loop, when
14714 we know the total number of template parms,
14715 end_template_parm_list computes the proper canonical types and
14716 fixes up the dependent types accordingly. */
14717 while (true)
14719 tree parameter;
14720 bool is_non_type;
14721 bool is_parameter_pack;
14722 location_t parm_loc;
14724 /* Parse the template-parameter. */
14725 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14726 parameter = cp_parser_template_parameter (parser,
14727 &is_non_type,
14728 &is_parameter_pack);
14729 /* Add it to the list. */
14730 if (parameter != error_mark_node)
14731 parameter_list = process_template_parm (parameter_list,
14732 parm_loc,
14733 parameter,
14734 is_non_type,
14735 is_parameter_pack);
14736 else
14738 tree err_parm = build_tree_list (parameter, parameter);
14739 parameter_list = chainon (parameter_list, err_parm);
14742 /* If the next token is not a `,', we're done. */
14743 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14744 break;
14745 /* Otherwise, consume the `,' token. */
14746 cp_lexer_consume_token (parser->lexer);
14749 return end_template_parm_list (parameter_list);
14752 /* Parse a introduction-list.
14754 introduction-list:
14755 introduced-parameter
14756 introduction-list , introduced-parameter
14758 introduced-parameter:
14759 ...[opt] identifier
14761 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14762 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14763 WILDCARD_DECL will also have DECL_NAME set and token location in
14764 DECL_SOURCE_LOCATION. */
14766 static tree
14767 cp_parser_introduction_list (cp_parser *parser)
14769 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14771 while (true)
14773 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14774 if (is_pack)
14775 cp_lexer_consume_token (parser->lexer);
14777 /* Build placeholder. */
14778 tree parm = build_nt (WILDCARD_DECL);
14779 DECL_SOURCE_LOCATION (parm)
14780 = cp_lexer_peek_token (parser->lexer)->location;
14781 DECL_NAME (parm) = cp_parser_identifier (parser);
14782 WILDCARD_PACK_P (parm) = is_pack;
14783 vec_safe_push (introduction_vec, parm);
14785 /* If the next token is not a `,', we're done. */
14786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14787 break;
14788 /* Otherwise, consume the `,' token. */
14789 cp_lexer_consume_token (parser->lexer);
14792 /* Convert the vec into a TREE_VEC. */
14793 tree introduction_list = make_tree_vec (introduction_vec->length ());
14794 unsigned int n;
14795 tree parm;
14796 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14797 TREE_VEC_ELT (introduction_list, n) = parm;
14799 release_tree_vector (introduction_vec);
14800 return introduction_list;
14803 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14804 is an abstract declarator. */
14806 static inline cp_declarator*
14807 get_id_declarator (cp_declarator *declarator)
14809 cp_declarator *d = declarator;
14810 while (d && d->kind != cdk_id)
14811 d = d->declarator;
14812 return d;
14815 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14816 is an abstract declarator. */
14818 static inline tree
14819 get_unqualified_id (cp_declarator *declarator)
14821 declarator = get_id_declarator (declarator);
14822 if (declarator)
14823 return declarator->u.id.unqualified_name;
14824 else
14825 return NULL_TREE;
14828 /* Returns true if DECL represents a constrained-parameter. */
14830 static inline bool
14831 is_constrained_parameter (tree decl)
14833 return (decl
14834 && TREE_CODE (decl) == TYPE_DECL
14835 && CONSTRAINED_PARM_CONCEPT (decl)
14836 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14839 /* Returns true if PARM declares a constrained-parameter. */
14841 static inline bool
14842 is_constrained_parameter (cp_parameter_declarator *parm)
14844 return is_constrained_parameter (parm->decl_specifiers.type);
14847 /* Check that the type parameter is only a declarator-id, and that its
14848 type is not cv-qualified. */
14850 bool
14851 cp_parser_check_constrained_type_parm (cp_parser *parser,
14852 cp_parameter_declarator *parm)
14854 if (!parm->declarator)
14855 return true;
14857 if (parm->declarator->kind != cdk_id)
14859 cp_parser_error (parser, "invalid constrained type parameter");
14860 return false;
14863 /* Don't allow cv-qualified type parameters. */
14864 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14865 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14867 cp_parser_error (parser, "cv-qualified type parameter");
14868 return false;
14871 return true;
14874 /* Finish parsing/processing a template type parameter and checking
14875 various restrictions. */
14877 static inline tree
14878 cp_parser_constrained_type_template_parm (cp_parser *parser,
14879 tree id,
14880 cp_parameter_declarator* parmdecl)
14882 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
14883 return finish_template_type_parm (class_type_node, id);
14884 else
14885 return error_mark_node;
14888 static tree
14889 finish_constrained_template_template_parm (tree proto, tree id)
14891 /* FIXME: This should probably be copied, and we may need to adjust
14892 the template parameter depths. */
14893 tree saved_parms = current_template_parms;
14894 begin_template_parm_list ();
14895 current_template_parms = DECL_TEMPLATE_PARMS (proto);
14896 end_template_parm_list ();
14898 tree parm = finish_template_template_parm (class_type_node, id);
14899 current_template_parms = saved_parms;
14901 return parm;
14904 /* Finish parsing/processing a template template parameter by borrowing
14905 the template parameter list from the prototype parameter. */
14907 static tree
14908 cp_parser_constrained_template_template_parm (cp_parser *parser,
14909 tree proto,
14910 tree id,
14911 cp_parameter_declarator *parmdecl)
14913 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
14914 return error_mark_node;
14915 return finish_constrained_template_template_parm (proto, id);
14918 /* Create a new non-type template parameter from the given PARM
14919 declarator. */
14921 static tree
14922 constrained_non_type_template_parm (bool *is_non_type,
14923 cp_parameter_declarator *parm)
14925 *is_non_type = true;
14926 cp_declarator *decl = parm->declarator;
14927 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
14928 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
14929 return grokdeclarator (decl, specs, TPARM, 0, NULL);
14932 /* Build a constrained template parameter based on the PARMDECL
14933 declarator. The type of PARMDECL is the constrained type, which
14934 refers to the prototype template parameter that ultimately
14935 specifies the type of the declared parameter. */
14937 static tree
14938 finish_constrained_parameter (cp_parser *parser,
14939 cp_parameter_declarator *parmdecl,
14940 bool *is_non_type,
14941 bool *is_parameter_pack)
14943 tree decl = parmdecl->decl_specifiers.type;
14944 tree id = get_unqualified_id (parmdecl->declarator);
14945 tree def = parmdecl->default_argument;
14946 tree proto = DECL_INITIAL (decl);
14948 /* A template parameter constrained by a variadic concept shall also
14949 be declared as a template parameter pack. */
14950 bool is_variadic = template_parameter_pack_p (proto);
14951 if (is_variadic && !*is_parameter_pack)
14952 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
14954 /* Build the parameter. Return an error if the declarator was invalid. */
14955 tree parm;
14956 if (TREE_CODE (proto) == TYPE_DECL)
14957 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
14958 else if (TREE_CODE (proto) == TEMPLATE_DECL)
14959 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
14960 parmdecl);
14961 else
14962 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
14963 if (parm == error_mark_node)
14964 return error_mark_node;
14966 /* Finish the parameter decl and create a node attaching the
14967 default argument and constraint. */
14968 parm = build_tree_list (def, parm);
14969 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
14971 return parm;
14974 /* Returns true if the parsed type actually represents the declaration
14975 of a type template-parameter. */
14977 static inline bool
14978 declares_constrained_type_template_parameter (tree type)
14980 return (is_constrained_parameter (type)
14981 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
14985 /* Returns true if the parsed type actually represents the declaration of
14986 a template template-parameter. */
14988 static bool
14989 declares_constrained_template_template_parameter (tree type)
14991 return (is_constrained_parameter (type)
14992 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
14995 /* Parse a default argument for a type template-parameter.
14996 Note that diagnostics are handled in cp_parser_template_parameter. */
14998 static tree
14999 cp_parser_default_type_template_argument (cp_parser *parser)
15001 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15003 /* Consume the `=' token. */
15004 cp_lexer_consume_token (parser->lexer);
15006 cp_token *token = cp_lexer_peek_token (parser->lexer);
15008 /* Parse the default-argument. */
15009 push_deferring_access_checks (dk_no_deferred);
15010 tree default_argument = cp_parser_type_id (parser);
15011 pop_deferring_access_checks ();
15013 if (flag_concepts && type_uses_auto (default_argument))
15015 error_at (token->location,
15016 "invalid use of %<auto%> in default template argument");
15017 return error_mark_node;
15020 return default_argument;
15023 /* Parse a default argument for a template template-parameter. */
15025 static tree
15026 cp_parser_default_template_template_argument (cp_parser *parser)
15028 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15030 bool is_template;
15032 /* Consume the `='. */
15033 cp_lexer_consume_token (parser->lexer);
15034 /* Parse the id-expression. */
15035 push_deferring_access_checks (dk_no_deferred);
15036 /* save token before parsing the id-expression, for error
15037 reporting */
15038 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15039 tree default_argument
15040 = cp_parser_id_expression (parser,
15041 /*template_keyword_p=*/false,
15042 /*check_dependency_p=*/true,
15043 /*template_p=*/&is_template,
15044 /*declarator_p=*/false,
15045 /*optional_p=*/false);
15046 if (TREE_CODE (default_argument) == TYPE_DECL)
15047 /* If the id-expression was a template-id that refers to
15048 a template-class, we already have the declaration here,
15049 so no further lookup is needed. */
15051 else
15052 /* Look up the name. */
15053 default_argument
15054 = cp_parser_lookup_name (parser, default_argument,
15055 none_type,
15056 /*is_template=*/is_template,
15057 /*is_namespace=*/false,
15058 /*check_dependency=*/true,
15059 /*ambiguous_decls=*/NULL,
15060 token->location);
15061 /* See if the default argument is valid. */
15062 default_argument = check_template_template_default_arg (default_argument);
15063 pop_deferring_access_checks ();
15064 return default_argument;
15067 /* Parse a template-parameter.
15069 template-parameter:
15070 type-parameter
15071 parameter-declaration
15073 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15074 the parameter. The TREE_PURPOSE is the default value, if any.
15075 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15076 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15077 set to true iff this parameter is a parameter pack. */
15079 static tree
15080 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15081 bool *is_parameter_pack)
15083 cp_token *token;
15084 cp_parameter_declarator *parameter_declarator;
15085 tree parm;
15087 /* Assume it is a type parameter or a template parameter. */
15088 *is_non_type = false;
15089 /* Assume it not a parameter pack. */
15090 *is_parameter_pack = false;
15091 /* Peek at the next token. */
15092 token = cp_lexer_peek_token (parser->lexer);
15093 /* If it is `class' or `template', we have a type-parameter. */
15094 if (token->keyword == RID_TEMPLATE)
15095 return cp_parser_type_parameter (parser, is_parameter_pack);
15096 /* If it is `class' or `typename' we do not know yet whether it is a
15097 type parameter or a non-type parameter. Consider:
15099 template <typename T, typename T::X X> ...
15103 template <class C, class D*> ...
15105 Here, the first parameter is a type parameter, and the second is
15106 a non-type parameter. We can tell by looking at the token after
15107 the identifier -- if it is a `,', `=', or `>' then we have a type
15108 parameter. */
15109 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15111 /* Peek at the token after `class' or `typename'. */
15112 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15113 /* If it's an ellipsis, we have a template type parameter
15114 pack. */
15115 if (token->type == CPP_ELLIPSIS)
15116 return cp_parser_type_parameter (parser, is_parameter_pack);
15117 /* If it's an identifier, skip it. */
15118 if (token->type == CPP_NAME)
15119 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15120 /* Now, see if the token looks like the end of a template
15121 parameter. */
15122 if (token->type == CPP_COMMA
15123 || token->type == CPP_EQ
15124 || token->type == CPP_GREATER)
15125 return cp_parser_type_parameter (parser, is_parameter_pack);
15128 /* Otherwise, it is a non-type parameter or a constrained parameter.
15130 [temp.param]
15132 When parsing a default template-argument for a non-type
15133 template-parameter, the first non-nested `>' is taken as the end
15134 of the template parameter-list rather than a greater-than
15135 operator. */
15136 parameter_declarator
15137 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15138 /*parenthesized_p=*/NULL);
15140 if (!parameter_declarator)
15141 return error_mark_node;
15143 /* If the parameter declaration is marked as a parameter pack, set
15144 *IS_PARAMETER_PACK to notify the caller. */
15145 if (parameter_declarator->template_parameter_pack_p)
15146 *is_parameter_pack = true;
15148 if (parameter_declarator->default_argument)
15150 /* Can happen in some cases of erroneous input (c++/34892). */
15151 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15152 /* Consume the `...' for better error recovery. */
15153 cp_lexer_consume_token (parser->lexer);
15156 // The parameter may have been constrained.
15157 if (is_constrained_parameter (parameter_declarator))
15158 return finish_constrained_parameter (parser,
15159 parameter_declarator,
15160 is_non_type,
15161 is_parameter_pack);
15163 // Now we're sure that the parameter is a non-type parameter.
15164 *is_non_type = true;
15166 parm = grokdeclarator (parameter_declarator->declarator,
15167 &parameter_declarator->decl_specifiers,
15168 TPARM, /*initialized=*/0,
15169 /*attrlist=*/NULL);
15170 if (parm == error_mark_node)
15171 return error_mark_node;
15173 return build_tree_list (parameter_declarator->default_argument, parm);
15176 /* Parse a type-parameter.
15178 type-parameter:
15179 class identifier [opt]
15180 class identifier [opt] = type-id
15181 typename identifier [opt]
15182 typename identifier [opt] = type-id
15183 template < template-parameter-list > class identifier [opt]
15184 template < template-parameter-list > class identifier [opt]
15185 = id-expression
15187 GNU Extension (variadic templates):
15189 type-parameter:
15190 class ... identifier [opt]
15191 typename ... identifier [opt]
15193 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15194 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15195 the declaration of the parameter.
15197 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15199 static tree
15200 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15202 cp_token *token;
15203 tree parameter;
15205 /* Look for a keyword to tell us what kind of parameter this is. */
15206 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15207 if (!token)
15208 return error_mark_node;
15210 switch (token->keyword)
15212 case RID_CLASS:
15213 case RID_TYPENAME:
15215 tree identifier;
15216 tree default_argument;
15218 /* If the next token is an ellipsis, we have a template
15219 argument pack. */
15220 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15222 /* Consume the `...' token. */
15223 cp_lexer_consume_token (parser->lexer);
15224 maybe_warn_variadic_templates ();
15226 *is_parameter_pack = true;
15229 /* If the next token is an identifier, then it names the
15230 parameter. */
15231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15232 identifier = cp_parser_identifier (parser);
15233 else
15234 identifier = NULL_TREE;
15236 /* Create the parameter. */
15237 parameter = finish_template_type_parm (class_type_node, identifier);
15239 /* If the next token is an `=', we have a default argument. */
15240 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15242 default_argument
15243 = cp_parser_default_type_template_argument (parser);
15245 /* Template parameter packs cannot have default
15246 arguments. */
15247 if (*is_parameter_pack)
15249 if (identifier)
15250 error_at (token->location,
15251 "template parameter pack %qD cannot have a "
15252 "default argument", identifier);
15253 else
15254 error_at (token->location,
15255 "template parameter packs cannot have "
15256 "default arguments");
15257 default_argument = NULL_TREE;
15259 else if (check_for_bare_parameter_packs (default_argument))
15260 default_argument = error_mark_node;
15262 else
15263 default_argument = NULL_TREE;
15265 /* Create the combined representation of the parameter and the
15266 default argument. */
15267 parameter = build_tree_list (default_argument, parameter);
15269 break;
15271 case RID_TEMPLATE:
15273 tree identifier;
15274 tree default_argument;
15276 /* Look for the `<'. */
15277 cp_parser_require (parser, CPP_LESS, RT_LESS);
15278 /* Parse the template-parameter-list. */
15279 cp_parser_template_parameter_list (parser);
15280 /* Look for the `>'. */
15281 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15283 // If template requirements are present, parse them.
15284 if (flag_concepts)
15286 tree reqs = get_shorthand_constraints (current_template_parms);
15287 if (tree r = cp_parser_requires_clause_opt (parser))
15288 reqs = conjoin_constraints (reqs, normalize_expression (r));
15289 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15292 /* Look for the `class' or 'typename' keywords. */
15293 cp_parser_type_parameter_key (parser);
15294 /* If the next token is an ellipsis, we have a template
15295 argument pack. */
15296 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15298 /* Consume the `...' token. */
15299 cp_lexer_consume_token (parser->lexer);
15300 maybe_warn_variadic_templates ();
15302 *is_parameter_pack = true;
15304 /* If the next token is an `=', then there is a
15305 default-argument. If the next token is a `>', we are at
15306 the end of the parameter-list. If the next token is a `,',
15307 then we are at the end of this parameter. */
15308 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15309 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15310 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15312 identifier = cp_parser_identifier (parser);
15313 /* Treat invalid names as if the parameter were nameless. */
15314 if (identifier == error_mark_node)
15315 identifier = NULL_TREE;
15317 else
15318 identifier = NULL_TREE;
15320 /* Create the template parameter. */
15321 parameter = finish_template_template_parm (class_type_node,
15322 identifier);
15324 /* If the next token is an `=', then there is a
15325 default-argument. */
15326 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15328 default_argument
15329 = cp_parser_default_template_template_argument (parser);
15331 /* Template parameter packs cannot have default
15332 arguments. */
15333 if (*is_parameter_pack)
15335 if (identifier)
15336 error_at (token->location,
15337 "template parameter pack %qD cannot "
15338 "have a default argument",
15339 identifier);
15340 else
15341 error_at (token->location, "template parameter packs cannot "
15342 "have default arguments");
15343 default_argument = NULL_TREE;
15346 else
15347 default_argument = NULL_TREE;
15349 /* Create the combined representation of the parameter and the
15350 default argument. */
15351 parameter = build_tree_list (default_argument, parameter);
15353 break;
15355 default:
15356 gcc_unreachable ();
15357 break;
15360 return parameter;
15363 /* Parse a template-id.
15365 template-id:
15366 template-name < template-argument-list [opt] >
15368 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15369 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15370 returned. Otherwise, if the template-name names a function, or set
15371 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15372 names a class, returns a TYPE_DECL for the specialization.
15374 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15375 uninstantiated templates. */
15377 static tree
15378 cp_parser_template_id (cp_parser *parser,
15379 bool template_keyword_p,
15380 bool check_dependency_p,
15381 enum tag_types tag_type,
15382 bool is_declaration)
15384 tree templ;
15385 tree arguments;
15386 tree template_id;
15387 cp_token_position start_of_id = 0;
15388 cp_token *next_token = NULL, *next_token_2 = NULL;
15389 bool is_identifier;
15391 /* If the next token corresponds to a template-id, there is no need
15392 to reparse it. */
15393 next_token = cp_lexer_peek_token (parser->lexer);
15394 if (next_token->type == CPP_TEMPLATE_ID)
15396 cp_lexer_consume_token (parser->lexer);
15397 return saved_checks_value (next_token->u.tree_check_value);
15400 /* Avoid performing name lookup if there is no possibility of
15401 finding a template-id. */
15402 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15403 || (next_token->type == CPP_NAME
15404 && !cp_parser_nth_token_starts_template_argument_list_p
15405 (parser, 2)))
15407 cp_parser_error (parser, "expected template-id");
15408 return error_mark_node;
15411 /* Remember where the template-id starts. */
15412 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15413 start_of_id = cp_lexer_token_position (parser->lexer, false);
15415 push_deferring_access_checks (dk_deferred);
15417 /* Parse the template-name. */
15418 is_identifier = false;
15419 templ = cp_parser_template_name (parser, template_keyword_p,
15420 check_dependency_p,
15421 is_declaration,
15422 tag_type,
15423 &is_identifier);
15424 if (templ == error_mark_node || is_identifier)
15426 pop_deferring_access_checks ();
15427 return templ;
15430 /* Since we're going to preserve any side-effects from this parse, set up a
15431 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15432 in the template arguments. */
15433 tentative_firewall firewall (parser);
15435 /* If we find the sequence `[:' after a template-name, it's probably
15436 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15437 parse correctly the argument list. */
15438 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15439 == CPP_OPEN_SQUARE)
15440 && next_token->flags & DIGRAPH
15441 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15442 == CPP_COLON)
15443 && !(next_token_2->flags & PREV_WHITE))
15445 cp_parser_parse_tentatively (parser);
15446 /* Change `:' into `::'. */
15447 next_token_2->type = CPP_SCOPE;
15448 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15449 CPP_LESS. */
15450 cp_lexer_consume_token (parser->lexer);
15452 /* Parse the arguments. */
15453 arguments = cp_parser_enclosed_template_argument_list (parser);
15454 if (!cp_parser_parse_definitely (parser))
15456 /* If we couldn't parse an argument list, then we revert our changes
15457 and return simply an error. Maybe this is not a template-id
15458 after all. */
15459 next_token_2->type = CPP_COLON;
15460 cp_parser_error (parser, "expected %<<%>");
15461 pop_deferring_access_checks ();
15462 return error_mark_node;
15464 /* Otherwise, emit an error about the invalid digraph, but continue
15465 parsing because we got our argument list. */
15466 if (permerror (next_token->location,
15467 "%<<::%> cannot begin a template-argument list"))
15469 static bool hint = false;
15470 inform (next_token->location,
15471 "%<<:%> is an alternate spelling for %<[%>."
15472 " Insert whitespace between %<<%> and %<::%>");
15473 if (!hint && !flag_permissive)
15475 inform (next_token->location, "(if you use %<-fpermissive%> "
15476 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15477 "accept your code)");
15478 hint = true;
15482 else
15484 /* Look for the `<' that starts the template-argument-list. */
15485 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15487 pop_deferring_access_checks ();
15488 return error_mark_node;
15490 /* Parse the arguments. */
15491 arguments = cp_parser_enclosed_template_argument_list (parser);
15494 /* Build a representation of the specialization. */
15495 if (identifier_p (templ))
15496 template_id = build_min_nt_loc (next_token->location,
15497 TEMPLATE_ID_EXPR,
15498 templ, arguments);
15499 else if (DECL_TYPE_TEMPLATE_P (templ)
15500 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15502 bool entering_scope;
15503 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15504 template (rather than some instantiation thereof) only if
15505 is not nested within some other construct. For example, in
15506 "template <typename T> void f(T) { A<T>::", A<T> is just an
15507 instantiation of A. */
15508 entering_scope = (template_parm_scope_p ()
15509 && cp_lexer_next_token_is (parser->lexer,
15510 CPP_SCOPE));
15511 template_id
15512 = finish_template_type (templ, arguments, entering_scope);
15514 /* A template-like identifier may be a partial concept id. */
15515 else if (flag_concepts
15516 && (template_id = (cp_parser_maybe_partial_concept_id
15517 (parser, templ, arguments))))
15518 return template_id;
15519 else if (variable_template_p (templ))
15521 template_id = lookup_template_variable (templ, arguments);
15522 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15523 SET_EXPR_LOCATION (template_id, next_token->location);
15525 else
15527 /* If it's not a class-template or a template-template, it should be
15528 a function-template. */
15529 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15530 || TREE_CODE (templ) == OVERLOAD
15531 || BASELINK_P (templ)));
15533 template_id = lookup_template_function (templ, arguments);
15534 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15535 SET_EXPR_LOCATION (template_id, next_token->location);
15538 /* If parsing tentatively, replace the sequence of tokens that makes
15539 up the template-id with a CPP_TEMPLATE_ID token. That way,
15540 should we re-parse the token stream, we will not have to repeat
15541 the effort required to do the parse, nor will we issue duplicate
15542 error messages about problems during instantiation of the
15543 template. */
15544 if (start_of_id
15545 /* Don't do this if we had a parse error in a declarator; re-parsing
15546 might succeed if a name changes meaning (60361). */
15547 && !(cp_parser_error_occurred (parser)
15548 && cp_parser_parsing_tentatively (parser)
15549 && parser->in_declarator_p))
15551 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15553 /* Reset the contents of the START_OF_ID token. */
15554 token->type = CPP_TEMPLATE_ID;
15556 /* Update the location to be of the form:
15557 template-name < template-argument-list [opt] >
15558 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15559 with caret == start at the start of the template-name,
15560 ranging until the closing '>'. */
15561 location_t finish_loc
15562 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15563 location_t combined_loc
15564 = make_location (token->location, token->location, finish_loc);
15565 token->location = combined_loc;
15567 /* Retrieve any deferred checks. Do not pop this access checks yet
15568 so the memory will not be reclaimed during token replacing below. */
15569 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15570 token->u.tree_check_value->value = template_id;
15571 token->u.tree_check_value->checks = get_deferred_access_checks ();
15572 token->keyword = RID_MAX;
15574 /* Purge all subsequent tokens. */
15575 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15577 /* ??? Can we actually assume that, if template_id ==
15578 error_mark_node, we will have issued a diagnostic to the
15579 user, as opposed to simply marking the tentative parse as
15580 failed? */
15581 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15582 error_at (token->location, "parse error in template argument list");
15585 pop_to_parent_deferring_access_checks ();
15586 return template_id;
15589 /* Parse a template-name.
15591 template-name:
15592 identifier
15594 The standard should actually say:
15596 template-name:
15597 identifier
15598 operator-function-id
15600 A defect report has been filed about this issue.
15602 A conversion-function-id cannot be a template name because they cannot
15603 be part of a template-id. In fact, looking at this code:
15605 a.operator K<int>()
15607 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15608 It is impossible to call a templated conversion-function-id with an
15609 explicit argument list, since the only allowed template parameter is
15610 the type to which it is converting.
15612 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15613 `template' keyword, in a construction like:
15615 T::template f<3>()
15617 In that case `f' is taken to be a template-name, even though there
15618 is no way of knowing for sure.
15620 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15621 name refers to a set of overloaded functions, at least one of which
15622 is a template, or an IDENTIFIER_NODE with the name of the template,
15623 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15624 names are looked up inside uninstantiated templates. */
15626 static tree
15627 cp_parser_template_name (cp_parser* parser,
15628 bool template_keyword_p,
15629 bool check_dependency_p,
15630 bool is_declaration,
15631 enum tag_types tag_type,
15632 bool *is_identifier)
15634 tree identifier;
15635 tree decl;
15636 tree fns;
15637 cp_token *token = cp_lexer_peek_token (parser->lexer);
15639 /* If the next token is `operator', then we have either an
15640 operator-function-id or a conversion-function-id. */
15641 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15643 /* We don't know whether we're looking at an
15644 operator-function-id or a conversion-function-id. */
15645 cp_parser_parse_tentatively (parser);
15646 /* Try an operator-function-id. */
15647 identifier = cp_parser_operator_function_id (parser);
15648 /* If that didn't work, try a conversion-function-id. */
15649 if (!cp_parser_parse_definitely (parser))
15651 cp_parser_error (parser, "expected template-name");
15652 return error_mark_node;
15655 /* Look for the identifier. */
15656 else
15657 identifier = cp_parser_identifier (parser);
15659 /* If we didn't find an identifier, we don't have a template-id. */
15660 if (identifier == error_mark_node)
15661 return error_mark_node;
15663 /* If the name immediately followed the `template' keyword, then it
15664 is a template-name. However, if the next token is not `<', then
15665 we do not treat it as a template-name, since it is not being used
15666 as part of a template-id. This enables us to handle constructs
15667 like:
15669 template <typename T> struct S { S(); };
15670 template <typename T> S<T>::S();
15672 correctly. We would treat `S' as a template -- if it were `S<T>'
15673 -- but we do not if there is no `<'. */
15675 if (processing_template_decl
15676 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15678 /* In a declaration, in a dependent context, we pretend that the
15679 "template" keyword was present in order to improve error
15680 recovery. For example, given:
15682 template <typename T> void f(T::X<int>);
15684 we want to treat "X<int>" as a template-id. */
15685 if (is_declaration
15686 && !template_keyword_p
15687 && parser->scope && TYPE_P (parser->scope)
15688 && check_dependency_p
15689 && dependent_scope_p (parser->scope)
15690 /* Do not do this for dtors (or ctors), since they never
15691 need the template keyword before their name. */
15692 && !constructor_name_p (identifier, parser->scope))
15694 cp_token_position start = 0;
15696 /* Explain what went wrong. */
15697 error_at (token->location, "non-template %qD used as template",
15698 identifier);
15699 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15700 parser->scope, identifier);
15701 /* If parsing tentatively, find the location of the "<" token. */
15702 if (cp_parser_simulate_error (parser))
15703 start = cp_lexer_token_position (parser->lexer, true);
15704 /* Parse the template arguments so that we can issue error
15705 messages about them. */
15706 cp_lexer_consume_token (parser->lexer);
15707 cp_parser_enclosed_template_argument_list (parser);
15708 /* Skip tokens until we find a good place from which to
15709 continue parsing. */
15710 cp_parser_skip_to_closing_parenthesis (parser,
15711 /*recovering=*/true,
15712 /*or_comma=*/true,
15713 /*consume_paren=*/false);
15714 /* If parsing tentatively, permanently remove the
15715 template argument list. That will prevent duplicate
15716 error messages from being issued about the missing
15717 "template" keyword. */
15718 if (start)
15719 cp_lexer_purge_tokens_after (parser->lexer, start);
15720 if (is_identifier)
15721 *is_identifier = true;
15722 return identifier;
15725 /* If the "template" keyword is present, then there is generally
15726 no point in doing name-lookup, so we just return IDENTIFIER.
15727 But, if the qualifying scope is non-dependent then we can
15728 (and must) do name-lookup normally. */
15729 if (template_keyword_p
15730 && (!parser->scope
15731 || (TYPE_P (parser->scope)
15732 && dependent_type_p (parser->scope))))
15733 return identifier;
15736 /* Look up the name. */
15737 decl = cp_parser_lookup_name (parser, identifier,
15738 tag_type,
15739 /*is_template=*/true,
15740 /*is_namespace=*/false,
15741 check_dependency_p,
15742 /*ambiguous_decls=*/NULL,
15743 token->location);
15745 decl = strip_using_decl (decl);
15747 /* If DECL is a template, then the name was a template-name. */
15748 if (TREE_CODE (decl) == TEMPLATE_DECL)
15750 if (TREE_DEPRECATED (decl)
15751 && deprecated_state != DEPRECATED_SUPPRESS)
15752 warn_deprecated_use (decl, NULL_TREE);
15754 else
15756 tree fn = NULL_TREE;
15758 /* The standard does not explicitly indicate whether a name that
15759 names a set of overloaded declarations, some of which are
15760 templates, is a template-name. However, such a name should
15761 be a template-name; otherwise, there is no way to form a
15762 template-id for the overloaded templates. */
15763 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
15764 if (TREE_CODE (fns) == OVERLOAD)
15765 for (fn = fns; fn; fn = OVL_NEXT (fn))
15766 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
15767 break;
15769 if (!fn)
15771 /* The name does not name a template. */
15772 cp_parser_error (parser, "expected template-name");
15773 return error_mark_node;
15777 /* If DECL is dependent, and refers to a function, then just return
15778 its name; we will look it up again during template instantiation. */
15779 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15781 tree scope = ovl_scope (decl);
15782 if (TYPE_P (scope) && dependent_type_p (scope))
15783 return identifier;
15786 return decl;
15789 /* Parse a template-argument-list.
15791 template-argument-list:
15792 template-argument ... [opt]
15793 template-argument-list , template-argument ... [opt]
15795 Returns a TREE_VEC containing the arguments. */
15797 static tree
15798 cp_parser_template_argument_list (cp_parser* parser)
15800 tree fixed_args[10];
15801 unsigned n_args = 0;
15802 unsigned alloced = 10;
15803 tree *arg_ary = fixed_args;
15804 tree vec;
15805 bool saved_in_template_argument_list_p;
15806 bool saved_ice_p;
15807 bool saved_non_ice_p;
15809 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15810 parser->in_template_argument_list_p = true;
15811 /* Even if the template-id appears in an integral
15812 constant-expression, the contents of the argument list do
15813 not. */
15814 saved_ice_p = parser->integral_constant_expression_p;
15815 parser->integral_constant_expression_p = false;
15816 saved_non_ice_p = parser->non_integral_constant_expression_p;
15817 parser->non_integral_constant_expression_p = false;
15819 /* Parse the arguments. */
15822 tree argument;
15824 if (n_args)
15825 /* Consume the comma. */
15826 cp_lexer_consume_token (parser->lexer);
15828 /* Parse the template-argument. */
15829 argument = cp_parser_template_argument (parser);
15831 /* If the next token is an ellipsis, we're expanding a template
15832 argument pack. */
15833 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15835 if (argument == error_mark_node)
15837 cp_token *token = cp_lexer_peek_token (parser->lexer);
15838 error_at (token->location,
15839 "expected parameter pack before %<...%>");
15841 /* Consume the `...' token. */
15842 cp_lexer_consume_token (parser->lexer);
15844 /* Make the argument into a TYPE_PACK_EXPANSION or
15845 EXPR_PACK_EXPANSION. */
15846 argument = make_pack_expansion (argument);
15849 if (n_args == alloced)
15851 alloced *= 2;
15853 if (arg_ary == fixed_args)
15855 arg_ary = XNEWVEC (tree, alloced);
15856 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15858 else
15859 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
15861 arg_ary[n_args++] = argument;
15863 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15865 vec = make_tree_vec (n_args);
15867 while (n_args--)
15868 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
15870 if (arg_ary != fixed_args)
15871 free (arg_ary);
15872 parser->non_integral_constant_expression_p = saved_non_ice_p;
15873 parser->integral_constant_expression_p = saved_ice_p;
15874 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
15875 if (CHECKING_P)
15876 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
15877 return vec;
15880 /* Parse a template-argument.
15882 template-argument:
15883 assignment-expression
15884 type-id
15885 id-expression
15887 The representation is that of an assignment-expression, type-id, or
15888 id-expression -- except that the qualified id-expression is
15889 evaluated, so that the value returned is either a DECL or an
15890 OVERLOAD.
15892 Although the standard says "assignment-expression", it forbids
15893 throw-expressions or assignments in the template argument.
15894 Therefore, we use "conditional-expression" instead. */
15896 static tree
15897 cp_parser_template_argument (cp_parser* parser)
15899 tree argument;
15900 bool template_p;
15901 bool address_p;
15902 bool maybe_type_id = false;
15903 cp_token *token = NULL, *argument_start_token = NULL;
15904 location_t loc = 0;
15905 cp_id_kind idk;
15907 /* There's really no way to know what we're looking at, so we just
15908 try each alternative in order.
15910 [temp.arg]
15912 In a template-argument, an ambiguity between a type-id and an
15913 expression is resolved to a type-id, regardless of the form of
15914 the corresponding template-parameter.
15916 Therefore, we try a type-id first. */
15917 cp_parser_parse_tentatively (parser);
15918 argument = cp_parser_template_type_arg (parser);
15919 /* If there was no error parsing the type-id but the next token is a
15920 '>>', our behavior depends on which dialect of C++ we're
15921 parsing. In C++98, we probably found a typo for '> >'. But there
15922 are type-id which are also valid expressions. For instance:
15924 struct X { int operator >> (int); };
15925 template <int V> struct Foo {};
15926 Foo<X () >> 5> r;
15928 Here 'X()' is a valid type-id of a function type, but the user just
15929 wanted to write the expression "X() >> 5". Thus, we remember that we
15930 found a valid type-id, but we still try to parse the argument as an
15931 expression to see what happens.
15933 In C++0x, the '>>' will be considered two separate '>'
15934 tokens. */
15935 if (!cp_parser_error_occurred (parser)
15936 && cxx_dialect == cxx98
15937 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15939 maybe_type_id = true;
15940 cp_parser_abort_tentative_parse (parser);
15942 else
15944 /* If the next token isn't a `,' or a `>', then this argument wasn't
15945 really finished. This means that the argument is not a valid
15946 type-id. */
15947 if (!cp_parser_next_token_ends_template_argument_p (parser))
15948 cp_parser_error (parser, "expected template-argument");
15949 /* If that worked, we're done. */
15950 if (cp_parser_parse_definitely (parser))
15951 return argument;
15953 /* We're still not sure what the argument will be. */
15954 cp_parser_parse_tentatively (parser);
15955 /* Try a template. */
15956 argument_start_token = cp_lexer_peek_token (parser->lexer);
15957 argument = cp_parser_id_expression (parser,
15958 /*template_keyword_p=*/false,
15959 /*check_dependency_p=*/true,
15960 &template_p,
15961 /*declarator_p=*/false,
15962 /*optional_p=*/false);
15963 /* If the next token isn't a `,' or a `>', then this argument wasn't
15964 really finished. */
15965 if (!cp_parser_next_token_ends_template_argument_p (parser))
15966 cp_parser_error (parser, "expected template-argument");
15967 if (!cp_parser_error_occurred (parser))
15969 /* Figure out what is being referred to. If the id-expression
15970 was for a class template specialization, then we will have a
15971 TYPE_DECL at this point. There is no need to do name lookup
15972 at this point in that case. */
15973 if (TREE_CODE (argument) != TYPE_DECL)
15974 argument = cp_parser_lookup_name (parser, argument,
15975 none_type,
15976 /*is_template=*/template_p,
15977 /*is_namespace=*/false,
15978 /*check_dependency=*/true,
15979 /*ambiguous_decls=*/NULL,
15980 argument_start_token->location);
15981 /* Handle a constrained-type-specifier for a non-type template
15982 parameter. */
15983 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
15984 argument = decl;
15985 else if (TREE_CODE (argument) != TEMPLATE_DECL
15986 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
15987 cp_parser_error (parser, "expected template-name");
15989 if (cp_parser_parse_definitely (parser))
15991 if (TREE_DEPRECATED (argument))
15992 warn_deprecated_use (argument, NULL_TREE);
15993 return argument;
15995 /* It must be a non-type argument. In C++17 any constant-expression is
15996 allowed. */
15997 if (cxx_dialect > cxx14)
15998 goto general_expr;
16000 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16002 -- an integral constant-expression of integral or enumeration
16003 type; or
16005 -- the name of a non-type template-parameter; or
16007 -- the name of an object or function with external linkage...
16009 -- the address of an object or function with external linkage...
16011 -- a pointer to member... */
16012 /* Look for a non-type template parameter. */
16013 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16015 cp_parser_parse_tentatively (parser);
16016 argument = cp_parser_primary_expression (parser,
16017 /*address_p=*/false,
16018 /*cast_p=*/false,
16019 /*template_arg_p=*/true,
16020 &idk);
16021 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16022 || !cp_parser_next_token_ends_template_argument_p (parser))
16023 cp_parser_simulate_error (parser);
16024 if (cp_parser_parse_definitely (parser))
16025 return argument;
16028 /* If the next token is "&", the argument must be the address of an
16029 object or function with external linkage. */
16030 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16031 if (address_p)
16033 loc = cp_lexer_peek_token (parser->lexer)->location;
16034 cp_lexer_consume_token (parser->lexer);
16036 /* See if we might have an id-expression. */
16037 token = cp_lexer_peek_token (parser->lexer);
16038 if (token->type == CPP_NAME
16039 || token->keyword == RID_OPERATOR
16040 || token->type == CPP_SCOPE
16041 || token->type == CPP_TEMPLATE_ID
16042 || token->type == CPP_NESTED_NAME_SPECIFIER)
16044 cp_parser_parse_tentatively (parser);
16045 argument = cp_parser_primary_expression (parser,
16046 address_p,
16047 /*cast_p=*/false,
16048 /*template_arg_p=*/true,
16049 &idk);
16050 if (cp_parser_error_occurred (parser)
16051 || !cp_parser_next_token_ends_template_argument_p (parser))
16052 cp_parser_abort_tentative_parse (parser);
16053 else
16055 tree probe;
16057 if (INDIRECT_REF_P (argument))
16059 /* Strip the dereference temporarily. */
16060 gcc_assert (REFERENCE_REF_P (argument));
16061 argument = TREE_OPERAND (argument, 0);
16064 /* If we're in a template, we represent a qualified-id referring
16065 to a static data member as a SCOPE_REF even if the scope isn't
16066 dependent so that we can check access control later. */
16067 probe = argument;
16068 if (TREE_CODE (probe) == SCOPE_REF)
16069 probe = TREE_OPERAND (probe, 1);
16070 if (VAR_P (probe))
16072 /* A variable without external linkage might still be a
16073 valid constant-expression, so no error is issued here
16074 if the external-linkage check fails. */
16075 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16076 cp_parser_simulate_error (parser);
16078 else if (is_overloaded_fn (argument))
16079 /* All overloaded functions are allowed; if the external
16080 linkage test does not pass, an error will be issued
16081 later. */
16083 else if (address_p
16084 && (TREE_CODE (argument) == OFFSET_REF
16085 || TREE_CODE (argument) == SCOPE_REF))
16086 /* A pointer-to-member. */
16088 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16090 else
16091 cp_parser_simulate_error (parser);
16093 if (cp_parser_parse_definitely (parser))
16095 if (address_p)
16096 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16097 tf_warning_or_error);
16098 else
16099 argument = convert_from_reference (argument);
16100 return argument;
16104 /* If the argument started with "&", there are no other valid
16105 alternatives at this point. */
16106 if (address_p)
16108 cp_parser_error (parser, "invalid non-type template argument");
16109 return error_mark_node;
16112 general_expr:
16113 /* If the argument wasn't successfully parsed as a type-id followed
16114 by '>>', the argument can only be a constant expression now.
16115 Otherwise, we try parsing the constant-expression tentatively,
16116 because the argument could really be a type-id. */
16117 if (maybe_type_id)
16118 cp_parser_parse_tentatively (parser);
16120 if (cxx_dialect <= cxx14)
16121 argument = cp_parser_constant_expression (parser);
16122 else
16124 /* With C++17 generalized non-type template arguments we need to handle
16125 lvalue constant expressions, too. */
16126 argument = cp_parser_assignment_expression (parser);
16127 require_potential_constant_expression (argument);
16130 if (!maybe_type_id)
16131 return argument;
16132 if (!cp_parser_next_token_ends_template_argument_p (parser))
16133 cp_parser_error (parser, "expected template-argument");
16134 if (cp_parser_parse_definitely (parser))
16135 return argument;
16136 /* We did our best to parse the argument as a non type-id, but that
16137 was the only alternative that matched (albeit with a '>' after
16138 it). We can assume it's just a typo from the user, and a
16139 diagnostic will then be issued. */
16140 return cp_parser_template_type_arg (parser);
16143 /* Parse an explicit-instantiation.
16145 explicit-instantiation:
16146 template declaration
16148 Although the standard says `declaration', what it really means is:
16150 explicit-instantiation:
16151 template decl-specifier-seq [opt] declarator [opt] ;
16153 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16154 supposed to be allowed. A defect report has been filed about this
16155 issue.
16157 GNU Extension:
16159 explicit-instantiation:
16160 storage-class-specifier template
16161 decl-specifier-seq [opt] declarator [opt] ;
16162 function-specifier template
16163 decl-specifier-seq [opt] declarator [opt] ; */
16165 static void
16166 cp_parser_explicit_instantiation (cp_parser* parser)
16168 int declares_class_or_enum;
16169 cp_decl_specifier_seq decl_specifiers;
16170 tree extension_specifier = NULL_TREE;
16172 timevar_push (TV_TEMPLATE_INST);
16174 /* Look for an (optional) storage-class-specifier or
16175 function-specifier. */
16176 if (cp_parser_allow_gnu_extensions_p (parser))
16178 extension_specifier
16179 = cp_parser_storage_class_specifier_opt (parser);
16180 if (!extension_specifier)
16181 extension_specifier
16182 = cp_parser_function_specifier_opt (parser,
16183 /*decl_specs=*/NULL);
16186 /* Look for the `template' keyword. */
16187 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16188 /* Let the front end know that we are processing an explicit
16189 instantiation. */
16190 begin_explicit_instantiation ();
16191 /* [temp.explicit] says that we are supposed to ignore access
16192 control while processing explicit instantiation directives. */
16193 push_deferring_access_checks (dk_no_check);
16194 /* Parse a decl-specifier-seq. */
16195 cp_parser_decl_specifier_seq (parser,
16196 CP_PARSER_FLAGS_OPTIONAL,
16197 &decl_specifiers,
16198 &declares_class_or_enum);
16199 /* If there was exactly one decl-specifier, and it declared a class,
16200 and there's no declarator, then we have an explicit type
16201 instantiation. */
16202 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16204 tree type;
16206 type = check_tag_decl (&decl_specifiers,
16207 /*explicit_type_instantiation_p=*/true);
16208 /* Turn access control back on for names used during
16209 template instantiation. */
16210 pop_deferring_access_checks ();
16211 if (type)
16212 do_type_instantiation (type, extension_specifier,
16213 /*complain=*/tf_error);
16215 else
16217 cp_declarator *declarator;
16218 tree decl;
16220 /* Parse the declarator. */
16221 declarator
16222 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16223 /*ctor_dtor_or_conv_p=*/NULL,
16224 /*parenthesized_p=*/NULL,
16225 /*member_p=*/false,
16226 /*friend_p=*/false);
16227 if (declares_class_or_enum & 2)
16228 cp_parser_check_for_definition_in_return_type (declarator,
16229 decl_specifiers.type,
16230 decl_specifiers.locations[ds_type_spec]);
16231 if (declarator != cp_error_declarator)
16233 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16234 permerror (decl_specifiers.locations[ds_inline],
16235 "explicit instantiation shall not use"
16236 " %<inline%> specifier");
16237 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16238 permerror (decl_specifiers.locations[ds_constexpr],
16239 "explicit instantiation shall not use"
16240 " %<constexpr%> specifier");
16242 decl = grokdeclarator (declarator, &decl_specifiers,
16243 NORMAL, 0, &decl_specifiers.attributes);
16244 /* Turn access control back on for names used during
16245 template instantiation. */
16246 pop_deferring_access_checks ();
16247 /* Do the explicit instantiation. */
16248 do_decl_instantiation (decl, extension_specifier);
16250 else
16252 pop_deferring_access_checks ();
16253 /* Skip the body of the explicit instantiation. */
16254 cp_parser_skip_to_end_of_statement (parser);
16257 /* We're done with the instantiation. */
16258 end_explicit_instantiation ();
16260 cp_parser_consume_semicolon_at_end_of_statement (parser);
16262 timevar_pop (TV_TEMPLATE_INST);
16265 /* Parse an explicit-specialization.
16267 explicit-specialization:
16268 template < > declaration
16270 Although the standard says `declaration', what it really means is:
16272 explicit-specialization:
16273 template <> decl-specifier [opt] init-declarator [opt] ;
16274 template <> function-definition
16275 template <> explicit-specialization
16276 template <> template-declaration */
16278 static void
16279 cp_parser_explicit_specialization (cp_parser* parser)
16281 bool need_lang_pop;
16282 cp_token *token = cp_lexer_peek_token (parser->lexer);
16284 /* Look for the `template' keyword. */
16285 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16286 /* Look for the `<'. */
16287 cp_parser_require (parser, CPP_LESS, RT_LESS);
16288 /* Look for the `>'. */
16289 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16290 /* We have processed another parameter list. */
16291 ++parser->num_template_parameter_lists;
16292 /* [temp]
16294 A template ... explicit specialization ... shall not have C
16295 linkage. */
16296 if (current_lang_name == lang_name_c)
16298 error_at (token->location, "template specialization with C linkage");
16299 /* Give it C++ linkage to avoid confusing other parts of the
16300 front end. */
16301 push_lang_context (lang_name_cplusplus);
16302 need_lang_pop = true;
16304 else
16305 need_lang_pop = false;
16306 /* Let the front end know that we are beginning a specialization. */
16307 if (!begin_specialization ())
16309 end_specialization ();
16310 return;
16313 /* If the next keyword is `template', we need to figure out whether
16314 or not we're looking a template-declaration. */
16315 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16317 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16318 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16319 cp_parser_template_declaration_after_export (parser,
16320 /*member_p=*/false);
16321 else
16322 cp_parser_explicit_specialization (parser);
16324 else
16325 /* Parse the dependent declaration. */
16326 cp_parser_single_declaration (parser,
16327 /*checks=*/NULL,
16328 /*member_p=*/false,
16329 /*explicit_specialization_p=*/true,
16330 /*friend_p=*/NULL);
16331 /* We're done with the specialization. */
16332 end_specialization ();
16333 /* For the erroneous case of a template with C linkage, we pushed an
16334 implicit C++ linkage scope; exit that scope now. */
16335 if (need_lang_pop)
16336 pop_lang_context ();
16337 /* We're done with this parameter list. */
16338 --parser->num_template_parameter_lists;
16341 /* Parse a type-specifier.
16343 type-specifier:
16344 simple-type-specifier
16345 class-specifier
16346 enum-specifier
16347 elaborated-type-specifier
16348 cv-qualifier
16350 GNU Extension:
16352 type-specifier:
16353 __complex__
16355 Returns a representation of the type-specifier. For a
16356 class-specifier, enum-specifier, or elaborated-type-specifier, a
16357 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16359 The parser flags FLAGS is used to control type-specifier parsing.
16361 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16362 in a decl-specifier-seq.
16364 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16365 class-specifier, enum-specifier, or elaborated-type-specifier, then
16366 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16367 if a type is declared; 2 if it is defined. Otherwise, it is set to
16368 zero.
16370 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16371 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16372 is set to FALSE. */
16374 static tree
16375 cp_parser_type_specifier (cp_parser* parser,
16376 cp_parser_flags flags,
16377 cp_decl_specifier_seq *decl_specs,
16378 bool is_declaration,
16379 int* declares_class_or_enum,
16380 bool* is_cv_qualifier)
16382 tree type_spec = NULL_TREE;
16383 cp_token *token;
16384 enum rid keyword;
16385 cp_decl_spec ds = ds_last;
16387 /* Assume this type-specifier does not declare a new type. */
16388 if (declares_class_or_enum)
16389 *declares_class_or_enum = 0;
16390 /* And that it does not specify a cv-qualifier. */
16391 if (is_cv_qualifier)
16392 *is_cv_qualifier = false;
16393 /* Peek at the next token. */
16394 token = cp_lexer_peek_token (parser->lexer);
16396 /* If we're looking at a keyword, we can use that to guide the
16397 production we choose. */
16398 keyword = token->keyword;
16399 switch (keyword)
16401 case RID_ENUM:
16402 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16403 goto elaborated_type_specifier;
16405 /* Look for the enum-specifier. */
16406 type_spec = cp_parser_enum_specifier (parser);
16407 /* If that worked, we're done. */
16408 if (type_spec)
16410 if (declares_class_or_enum)
16411 *declares_class_or_enum = 2;
16412 if (decl_specs)
16413 cp_parser_set_decl_spec_type (decl_specs,
16414 type_spec,
16415 token,
16416 /*type_definition_p=*/true);
16417 return type_spec;
16419 else
16420 goto elaborated_type_specifier;
16422 /* Any of these indicate either a class-specifier, or an
16423 elaborated-type-specifier. */
16424 case RID_CLASS:
16425 case RID_STRUCT:
16426 case RID_UNION:
16427 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16428 goto elaborated_type_specifier;
16430 /* Parse tentatively so that we can back up if we don't find a
16431 class-specifier. */
16432 cp_parser_parse_tentatively (parser);
16433 /* Look for the class-specifier. */
16434 type_spec = cp_parser_class_specifier (parser);
16435 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16436 /* If that worked, we're done. */
16437 if (cp_parser_parse_definitely (parser))
16439 if (declares_class_or_enum)
16440 *declares_class_or_enum = 2;
16441 if (decl_specs)
16442 cp_parser_set_decl_spec_type (decl_specs,
16443 type_spec,
16444 token,
16445 /*type_definition_p=*/true);
16446 return type_spec;
16449 /* Fall through. */
16450 elaborated_type_specifier:
16451 /* We're declaring (not defining) a class or enum. */
16452 if (declares_class_or_enum)
16453 *declares_class_or_enum = 1;
16455 /* Fall through. */
16456 case RID_TYPENAME:
16457 /* Look for an elaborated-type-specifier. */
16458 type_spec
16459 = (cp_parser_elaborated_type_specifier
16460 (parser,
16461 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16462 is_declaration));
16463 if (decl_specs)
16464 cp_parser_set_decl_spec_type (decl_specs,
16465 type_spec,
16466 token,
16467 /*type_definition_p=*/false);
16468 return type_spec;
16470 case RID_CONST:
16471 ds = ds_const;
16472 if (is_cv_qualifier)
16473 *is_cv_qualifier = true;
16474 break;
16476 case RID_VOLATILE:
16477 ds = ds_volatile;
16478 if (is_cv_qualifier)
16479 *is_cv_qualifier = true;
16480 break;
16482 case RID_RESTRICT:
16483 ds = ds_restrict;
16484 if (is_cv_qualifier)
16485 *is_cv_qualifier = true;
16486 break;
16488 case RID_COMPLEX:
16489 /* The `__complex__' keyword is a GNU extension. */
16490 ds = ds_complex;
16491 break;
16493 default:
16494 break;
16497 /* Handle simple keywords. */
16498 if (ds != ds_last)
16500 if (decl_specs)
16502 set_and_check_decl_spec_loc (decl_specs, ds, token);
16503 decl_specs->any_specifiers_p = true;
16505 return cp_lexer_consume_token (parser->lexer)->u.value;
16508 /* If we do not already have a type-specifier, assume we are looking
16509 at a simple-type-specifier. */
16510 type_spec = cp_parser_simple_type_specifier (parser,
16511 decl_specs,
16512 flags);
16514 /* If we didn't find a type-specifier, and a type-specifier was not
16515 optional in this context, issue an error message. */
16516 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16518 cp_parser_error (parser, "expected type specifier");
16519 return error_mark_node;
16522 return type_spec;
16525 /* Parse a simple-type-specifier.
16527 simple-type-specifier:
16528 :: [opt] nested-name-specifier [opt] type-name
16529 :: [opt] nested-name-specifier template template-id
16530 char
16531 wchar_t
16532 bool
16533 short
16535 long
16536 signed
16537 unsigned
16538 float
16539 double
16540 void
16542 C++11 Extension:
16544 simple-type-specifier:
16545 auto
16546 decltype ( expression )
16547 char16_t
16548 char32_t
16549 __underlying_type ( type-id )
16551 C++17 extension:
16553 nested-name-specifier(opt) template-name
16555 GNU Extension:
16557 simple-type-specifier:
16558 __int128
16559 __typeof__ unary-expression
16560 __typeof__ ( type-id )
16561 __typeof__ ( type-id ) { initializer-list , [opt] }
16563 Concepts Extension:
16565 simple-type-specifier:
16566 constrained-type-specifier
16568 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16569 appropriately updated. */
16571 static tree
16572 cp_parser_simple_type_specifier (cp_parser* parser,
16573 cp_decl_specifier_seq *decl_specs,
16574 cp_parser_flags flags)
16576 tree type = NULL_TREE;
16577 cp_token *token;
16578 int idx;
16580 /* Peek at the next token. */
16581 token = cp_lexer_peek_token (parser->lexer);
16583 /* If we're looking at a keyword, things are easy. */
16584 switch (token->keyword)
16586 case RID_CHAR:
16587 if (decl_specs)
16588 decl_specs->explicit_char_p = true;
16589 type = char_type_node;
16590 break;
16591 case RID_CHAR16:
16592 type = char16_type_node;
16593 break;
16594 case RID_CHAR32:
16595 type = char32_type_node;
16596 break;
16597 case RID_WCHAR:
16598 type = wchar_type_node;
16599 break;
16600 case RID_BOOL:
16601 type = boolean_type_node;
16602 break;
16603 case RID_SHORT:
16604 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16605 type = short_integer_type_node;
16606 break;
16607 case RID_INT:
16608 if (decl_specs)
16609 decl_specs->explicit_int_p = true;
16610 type = integer_type_node;
16611 break;
16612 case RID_INT_N_0:
16613 case RID_INT_N_1:
16614 case RID_INT_N_2:
16615 case RID_INT_N_3:
16616 idx = token->keyword - RID_INT_N_0;
16617 if (! int_n_enabled_p [idx])
16618 break;
16619 if (decl_specs)
16621 decl_specs->explicit_intN_p = true;
16622 decl_specs->int_n_idx = idx;
16624 type = int_n_trees [idx].signed_type;
16625 break;
16626 case RID_LONG:
16627 if (decl_specs)
16628 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16629 type = long_integer_type_node;
16630 break;
16631 case RID_SIGNED:
16632 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16633 type = integer_type_node;
16634 break;
16635 case RID_UNSIGNED:
16636 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16637 type = unsigned_type_node;
16638 break;
16639 case RID_FLOAT:
16640 type = float_type_node;
16641 break;
16642 case RID_DOUBLE:
16643 type = double_type_node;
16644 break;
16645 case RID_VOID:
16646 type = void_type_node;
16647 break;
16649 case RID_AUTO:
16650 maybe_warn_cpp0x (CPP0X_AUTO);
16651 if (parser->auto_is_implicit_function_template_parm_p)
16653 /* The 'auto' might be the placeholder return type for a function decl
16654 with trailing return type. */
16655 bool have_trailing_return_fn_decl = false;
16657 cp_parser_parse_tentatively (parser);
16658 cp_lexer_consume_token (parser->lexer);
16659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16660 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16661 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16662 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16664 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16666 cp_lexer_consume_token (parser->lexer);
16667 cp_parser_skip_to_closing_parenthesis (parser,
16668 /*recovering*/false,
16669 /*or_comma*/false,
16670 /*consume_paren*/true);
16671 continue;
16674 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16676 have_trailing_return_fn_decl = true;
16677 break;
16680 cp_lexer_consume_token (parser->lexer);
16682 cp_parser_abort_tentative_parse (parser);
16684 if (have_trailing_return_fn_decl)
16686 type = make_auto ();
16687 break;
16690 if (cxx_dialect >= cxx14)
16692 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16693 type = TREE_TYPE (type);
16695 else
16696 type = error_mark_node;
16698 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16700 if (cxx_dialect < cxx14)
16701 error_at (token->location,
16702 "use of %<auto%> in lambda parameter declaration "
16703 "only available with "
16704 "-std=c++14 or -std=gnu++14");
16706 else if (cxx_dialect < cxx14)
16707 error_at (token->location,
16708 "use of %<auto%> in parameter declaration "
16709 "only available with "
16710 "-std=c++14 or -std=gnu++14");
16711 else if (!flag_concepts)
16712 pedwarn (token->location, OPT_Wpedantic,
16713 "ISO C++ forbids use of %<auto%> in parameter "
16714 "declaration");
16716 else
16717 type = make_auto ();
16718 break;
16720 case RID_DECLTYPE:
16721 /* Since DR 743, decltype can either be a simple-type-specifier by
16722 itself or begin a nested-name-specifier. Parsing it will replace
16723 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16724 handling below decide what to do. */
16725 cp_parser_decltype (parser);
16726 cp_lexer_set_token_position (parser->lexer, token);
16727 break;
16729 case RID_TYPEOF:
16730 /* Consume the `typeof' token. */
16731 cp_lexer_consume_token (parser->lexer);
16732 /* Parse the operand to `typeof'. */
16733 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16734 /* If it is not already a TYPE, take its type. */
16735 if (!TYPE_P (type))
16736 type = finish_typeof (type);
16738 if (decl_specs)
16739 cp_parser_set_decl_spec_type (decl_specs, type,
16740 token,
16741 /*type_definition_p=*/false);
16743 return type;
16745 case RID_UNDERLYING_TYPE:
16746 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16747 if (decl_specs)
16748 cp_parser_set_decl_spec_type (decl_specs, type,
16749 token,
16750 /*type_definition_p=*/false);
16752 return type;
16754 case RID_BASES:
16755 case RID_DIRECT_BASES:
16756 type = cp_parser_trait_expr (parser, token->keyword);
16757 if (decl_specs)
16758 cp_parser_set_decl_spec_type (decl_specs, type,
16759 token,
16760 /*type_definition_p=*/false);
16761 return type;
16762 default:
16763 break;
16766 /* If token is an already-parsed decltype not followed by ::,
16767 it's a simple-type-specifier. */
16768 if (token->type == CPP_DECLTYPE
16769 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16771 type = saved_checks_value (token->u.tree_check_value);
16772 if (decl_specs)
16774 cp_parser_set_decl_spec_type (decl_specs, type,
16775 token,
16776 /*type_definition_p=*/false);
16777 /* Remember that we are handling a decltype in order to
16778 implement the resolution of DR 1510 when the argument
16779 isn't instantiation dependent. */
16780 decl_specs->decltype_p = true;
16782 cp_lexer_consume_token (parser->lexer);
16783 return type;
16786 /* If the type-specifier was for a built-in type, we're done. */
16787 if (type)
16789 /* Record the type. */
16790 if (decl_specs
16791 && (token->keyword != RID_SIGNED
16792 && token->keyword != RID_UNSIGNED
16793 && token->keyword != RID_SHORT
16794 && token->keyword != RID_LONG))
16795 cp_parser_set_decl_spec_type (decl_specs,
16796 type,
16797 token,
16798 /*type_definition_p=*/false);
16799 if (decl_specs)
16800 decl_specs->any_specifiers_p = true;
16802 /* Consume the token. */
16803 cp_lexer_consume_token (parser->lexer);
16805 if (type == error_mark_node)
16806 return error_mark_node;
16808 /* There is no valid C++ program where a non-template type is
16809 followed by a "<". That usually indicates that the user thought
16810 that the type was a template. */
16811 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16812 token->location);
16814 return TYPE_NAME (type);
16817 /* The type-specifier must be a user-defined type. */
16818 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16820 bool qualified_p;
16821 bool global_p;
16823 /* Don't gobble tokens or issue error messages if this is an
16824 optional type-specifier. */
16825 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16826 cp_parser_parse_tentatively (parser);
16828 token = cp_lexer_peek_token (parser->lexer);
16830 /* Look for the optional `::' operator. */
16831 global_p
16832 = (cp_parser_global_scope_opt (parser,
16833 /*current_scope_valid_p=*/false)
16834 != NULL_TREE);
16835 /* Look for the nested-name specifier. */
16836 qualified_p
16837 = (cp_parser_nested_name_specifier_opt (parser,
16838 /*typename_keyword_p=*/false,
16839 /*check_dependency_p=*/true,
16840 /*type_p=*/false,
16841 /*is_declaration=*/false)
16842 != NULL_TREE);
16843 /* If we have seen a nested-name-specifier, and the next token
16844 is `template', then we are using the template-id production. */
16845 if (parser->scope
16846 && cp_parser_optional_template_keyword (parser))
16848 /* Look for the template-id. */
16849 type = cp_parser_template_id (parser,
16850 /*template_keyword_p=*/true,
16851 /*check_dependency_p=*/true,
16852 none_type,
16853 /*is_declaration=*/false);
16854 /* If the template-id did not name a type, we are out of
16855 luck. */
16856 if (TREE_CODE (type) != TYPE_DECL)
16858 cp_parser_error (parser, "expected template-id for type");
16859 type = NULL_TREE;
16862 /* Otherwise, look for a type-name. */
16863 else
16864 type = cp_parser_type_name (parser);
16865 /* Keep track of all name-lookups performed in class scopes. */
16866 if (type
16867 && !global_p
16868 && !qualified_p
16869 && TREE_CODE (type) == TYPE_DECL
16870 && identifier_p (DECL_NAME (type)))
16871 maybe_note_name_used_in_class (DECL_NAME (type), type);
16872 /* If it didn't work out, we don't have a TYPE. */
16873 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16874 && !cp_parser_parse_definitely (parser))
16875 type = NULL_TREE;
16876 if (!type && cxx_dialect >= cxx1z)
16878 if (flags & CP_PARSER_FLAGS_OPTIONAL)
16879 cp_parser_parse_tentatively (parser);
16881 cp_parser_global_scope_opt (parser,
16882 /*current_scope_valid_p=*/false);
16883 cp_parser_nested_name_specifier_opt (parser,
16884 /*typename_keyword_p=*/false,
16885 /*check_dependency_p=*/true,
16886 /*type_p=*/false,
16887 /*is_declaration=*/false);
16888 tree name = cp_parser_identifier (parser);
16889 if (name && TREE_CODE (name) == IDENTIFIER_NODE
16890 && parser->scope != error_mark_node)
16892 tree tmpl = cp_parser_lookup_name (parser, name,
16893 none_type,
16894 /*is_template=*/false,
16895 /*is_namespace=*/false,
16896 /*check_dependency=*/true,
16897 /*ambiguous_decls=*/NULL,
16898 token->location);
16899 if (tmpl && tmpl != error_mark_node
16900 && (DECL_CLASS_TEMPLATE_P (tmpl)
16901 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
16902 type = make_template_placeholder (tmpl);
16903 else
16905 type = error_mark_node;
16906 if (!cp_parser_simulate_error (parser))
16907 cp_parser_name_lookup_error (parser, name, tmpl,
16908 NLE_TYPE, token->location);
16911 else
16912 type = error_mark_node;
16914 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
16915 && !cp_parser_parse_definitely (parser))
16916 type = NULL_TREE;
16918 if (type && decl_specs)
16919 cp_parser_set_decl_spec_type (decl_specs, type,
16920 token,
16921 /*type_definition_p=*/false);
16924 /* If we didn't get a type-name, issue an error message. */
16925 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16927 cp_parser_error (parser, "expected type-name");
16928 return error_mark_node;
16931 if (type && type != error_mark_node)
16933 /* See if TYPE is an Objective-C type, and if so, parse and
16934 accept any protocol references following it. Do this before
16935 the cp_parser_check_for_invalid_template_id() call, because
16936 Objective-C types can be followed by '<...>' which would
16937 enclose protocol names rather than template arguments, and so
16938 everything is fine. */
16939 if (c_dialect_objc () && !parser->scope
16940 && (objc_is_id (type) || objc_is_class_name (type)))
16942 tree protos = cp_parser_objc_protocol_refs_opt (parser);
16943 tree qual_type = objc_get_protocol_qualified_type (type, protos);
16945 /* Clobber the "unqualified" type previously entered into
16946 DECL_SPECS with the new, improved protocol-qualified version. */
16947 if (decl_specs)
16948 decl_specs->type = qual_type;
16950 return qual_type;
16953 /* There is no valid C++ program where a non-template type is
16954 followed by a "<". That usually indicates that the user
16955 thought that the type was a template. */
16956 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
16957 none_type,
16958 token->location);
16961 return type;
16964 /* Parse a type-name.
16966 type-name:
16967 class-name
16968 enum-name
16969 typedef-name
16970 simple-template-id [in c++0x]
16972 enum-name:
16973 identifier
16975 typedef-name:
16976 identifier
16978 Concepts:
16980 type-name:
16981 concept-name
16982 partial-concept-id
16984 concept-name:
16985 identifier
16987 Returns a TYPE_DECL for the type. */
16989 static tree
16990 cp_parser_type_name (cp_parser* parser)
16992 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
16995 /* See above. */
16996 static tree
16997 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
16999 tree type_decl;
17001 /* We can't know yet whether it is a class-name or not. */
17002 cp_parser_parse_tentatively (parser);
17003 /* Try a class-name. */
17004 type_decl = cp_parser_class_name (parser,
17005 typename_keyword_p,
17006 /*template_keyword_p=*/false,
17007 none_type,
17008 /*check_dependency_p=*/true,
17009 /*class_head_p=*/false,
17010 /*is_declaration=*/false);
17011 /* If it's not a class-name, keep looking. */
17012 if (!cp_parser_parse_definitely (parser))
17014 if (cxx_dialect < cxx11)
17015 /* It must be a typedef-name or an enum-name. */
17016 return cp_parser_nonclass_name (parser);
17018 cp_parser_parse_tentatively (parser);
17019 /* It is either a simple-template-id representing an
17020 instantiation of an alias template... */
17021 type_decl = cp_parser_template_id (parser,
17022 /*template_keyword_p=*/false,
17023 /*check_dependency_p=*/true,
17024 none_type,
17025 /*is_declaration=*/false);
17026 /* Note that this must be an instantiation of an alias template
17027 because [temp.names]/6 says:
17029 A template-id that names an alias template specialization
17030 is a type-name.
17032 Whereas [temp.names]/7 says:
17034 A simple-template-id that names a class template
17035 specialization is a class-name.
17037 With concepts, this could also be a partial-concept-id that
17038 declares a non-type template parameter. */
17039 if (type_decl != NULL_TREE
17040 && TREE_CODE (type_decl) == TYPE_DECL
17041 && TYPE_DECL_ALIAS_P (type_decl))
17042 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17043 else if (is_constrained_parameter (type_decl))
17044 /* Don't do anything. */ ;
17045 else
17046 cp_parser_simulate_error (parser);
17048 if (!cp_parser_parse_definitely (parser))
17049 /* ... Or a typedef-name or an enum-name. */
17050 return cp_parser_nonclass_name (parser);
17053 return type_decl;
17056 /* Check if DECL and ARGS can form a constrained-type-specifier.
17057 If ARGS is non-null, we try to form a concept check of the
17058 form DECL<?, ARGS> where ? is a wildcard that matches any
17059 kind of template argument. If ARGS is NULL, then we try to
17060 form a concept check of the form DECL<?>. */
17062 static tree
17063 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17064 tree decl, tree args)
17066 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17068 /* If we a constrained-type-specifier cannot be deduced. */
17069 if (parser->prevent_constrained_type_specifiers)
17070 return NULL_TREE;
17072 /* A constrained type specifier can only be found in an
17073 overload set or as a reference to a template declaration.
17075 FIXME: This might be masking a bug. It's possible that
17076 that the deduction below is causing template specializations
17077 to be formed with the wildcard as an argument. */
17078 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17079 return NULL_TREE;
17081 /* Try to build a call expression that evaluates the
17082 concept. This can fail if the overload set refers
17083 only to non-templates. */
17084 tree placeholder = build_nt (WILDCARD_DECL);
17085 tree check = build_concept_check (decl, placeholder, args);
17086 if (check == error_mark_node)
17087 return NULL_TREE;
17089 /* Deduce the checked constraint and the prototype parameter.
17091 FIXME: In certain cases, failure to deduce should be a
17092 diagnosable error. */
17093 tree conc;
17094 tree proto;
17095 if (!deduce_constrained_parameter (check, conc, proto))
17096 return NULL_TREE;
17098 /* In template parameter scope, this results in a constrained
17099 parameter. Return a descriptor of that parm. */
17100 if (processing_template_parmlist)
17101 return build_constrained_parameter (conc, proto, args);
17103 /* In a parameter-declaration-clause, constrained-type
17104 specifiers result in invented template parameters. */
17105 if (parser->auto_is_implicit_function_template_parm_p)
17107 tree x = build_constrained_parameter (conc, proto, args);
17108 return synthesize_implicit_template_parm (parser, x);
17110 else
17112 /* Otherwise, we're in a context where the constrained
17113 type name is deduced and the constraint applies
17114 after deduction. */
17115 return make_constrained_auto (conc, args);
17118 return NULL_TREE;
17121 /* If DECL refers to a concept, return a TYPE_DECL representing
17122 the result of using the constrained type specifier in the
17123 current context. DECL refers to a concept if
17125 - it is an overload set containing a function concept taking a single
17126 type argument, or
17128 - it is a variable concept taking a single type argument. */
17130 static tree
17131 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17133 if (flag_concepts
17134 && (TREE_CODE (decl) == OVERLOAD
17135 || BASELINK_P (decl)
17136 || variable_concept_p (decl)))
17137 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17138 else
17139 return NULL_TREE;
17142 /* Check if DECL and ARGS form a partial-concept-id. If so,
17143 assign ID to the resulting constrained placeholder.
17145 Returns true if the partial-concept-id designates a placeholder
17146 and false otherwise. Note that *id is set to NULL_TREE in
17147 this case. */
17149 static tree
17150 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17152 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17155 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17156 or a concept-name.
17158 enum-name:
17159 identifier
17161 typedef-name:
17162 identifier
17164 concept-name:
17165 identifier
17167 Returns a TYPE_DECL for the type. */
17169 static tree
17170 cp_parser_nonclass_name (cp_parser* parser)
17172 tree type_decl;
17173 tree identifier;
17175 cp_token *token = cp_lexer_peek_token (parser->lexer);
17176 identifier = cp_parser_identifier (parser);
17177 if (identifier == error_mark_node)
17178 return error_mark_node;
17180 /* Look up the type-name. */
17181 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17183 type_decl = strip_using_decl (type_decl);
17185 /* If we found an overload set, then it may refer to a concept-name. */
17186 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17187 type_decl = decl;
17189 if (TREE_CODE (type_decl) != TYPE_DECL
17190 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17192 /* See if this is an Objective-C type. */
17193 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17194 tree type = objc_get_protocol_qualified_type (identifier, protos);
17195 if (type)
17196 type_decl = TYPE_NAME (type);
17199 /* Issue an error if we did not find a type-name. */
17200 if (TREE_CODE (type_decl) != TYPE_DECL
17201 /* In Objective-C, we have the complication that class names are
17202 normally type names and start declarations (eg, the
17203 "NSObject" in "NSObject *object;"), but can be used in an
17204 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17205 is an expression. So, a classname followed by a dot is not a
17206 valid type-name. */
17207 || (objc_is_class_name (TREE_TYPE (type_decl))
17208 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17210 if (!cp_parser_simulate_error (parser))
17211 cp_parser_name_lookup_error (parser, identifier, type_decl,
17212 NLE_TYPE, token->location);
17213 return error_mark_node;
17215 /* Remember that the name was used in the definition of the
17216 current class so that we can check later to see if the
17217 meaning would have been different after the class was
17218 entirely defined. */
17219 else if (type_decl != error_mark_node
17220 && !parser->scope)
17221 maybe_note_name_used_in_class (identifier, type_decl);
17223 return type_decl;
17226 /* Parse an elaborated-type-specifier. Note that the grammar given
17227 here incorporates the resolution to DR68.
17229 elaborated-type-specifier:
17230 class-key :: [opt] nested-name-specifier [opt] identifier
17231 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17232 enum-key :: [opt] nested-name-specifier [opt] identifier
17233 typename :: [opt] nested-name-specifier identifier
17234 typename :: [opt] nested-name-specifier template [opt]
17235 template-id
17237 GNU extension:
17239 elaborated-type-specifier:
17240 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17241 class-key attributes :: [opt] nested-name-specifier [opt]
17242 template [opt] template-id
17243 enum attributes :: [opt] nested-name-specifier [opt] identifier
17245 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17246 declared `friend'. If IS_DECLARATION is TRUE, then this
17247 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17248 something is being declared.
17250 Returns the TYPE specified. */
17252 static tree
17253 cp_parser_elaborated_type_specifier (cp_parser* parser,
17254 bool is_friend,
17255 bool is_declaration)
17257 enum tag_types tag_type;
17258 tree identifier;
17259 tree type = NULL_TREE;
17260 tree attributes = NULL_TREE;
17261 tree globalscope;
17262 cp_token *token = NULL;
17264 /* See if we're looking at the `enum' keyword. */
17265 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17267 /* Consume the `enum' token. */
17268 cp_lexer_consume_token (parser->lexer);
17269 /* Remember that it's an enumeration type. */
17270 tag_type = enum_type;
17271 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17272 enums) is used here. */
17273 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17274 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17276 pedwarn (input_location, 0, "elaborated-type-specifier "
17277 "for a scoped enum must not use the %<%D%> keyword",
17278 cp_lexer_peek_token (parser->lexer)->u.value);
17279 /* Consume the `struct' or `class' and parse it anyway. */
17280 cp_lexer_consume_token (parser->lexer);
17282 /* Parse the attributes. */
17283 attributes = cp_parser_attributes_opt (parser);
17285 /* Or, it might be `typename'. */
17286 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17287 RID_TYPENAME))
17289 /* Consume the `typename' token. */
17290 cp_lexer_consume_token (parser->lexer);
17291 /* Remember that it's a `typename' type. */
17292 tag_type = typename_type;
17294 /* Otherwise it must be a class-key. */
17295 else
17297 tag_type = cp_parser_class_key (parser);
17298 if (tag_type == none_type)
17299 return error_mark_node;
17300 /* Parse the attributes. */
17301 attributes = cp_parser_attributes_opt (parser);
17304 /* Look for the `::' operator. */
17305 globalscope = cp_parser_global_scope_opt (parser,
17306 /*current_scope_valid_p=*/false);
17307 /* Look for the nested-name-specifier. */
17308 tree nested_name_specifier;
17309 if (tag_type == typename_type && !globalscope)
17311 nested_name_specifier
17312 = cp_parser_nested_name_specifier (parser,
17313 /*typename_keyword_p=*/true,
17314 /*check_dependency_p=*/true,
17315 /*type_p=*/true,
17316 is_declaration);
17317 if (!nested_name_specifier)
17318 return error_mark_node;
17320 else
17321 /* Even though `typename' is not present, the proposed resolution
17322 to Core Issue 180 says that in `class A<T>::B', `B' should be
17323 considered a type-name, even if `A<T>' is dependent. */
17324 nested_name_specifier
17325 = cp_parser_nested_name_specifier_opt (parser,
17326 /*typename_keyword_p=*/true,
17327 /*check_dependency_p=*/true,
17328 /*type_p=*/true,
17329 is_declaration);
17330 /* For everything but enumeration types, consider a template-id.
17331 For an enumeration type, consider only a plain identifier. */
17332 if (tag_type != enum_type)
17334 bool template_p = false;
17335 tree decl;
17337 /* Allow the `template' keyword. */
17338 template_p = cp_parser_optional_template_keyword (parser);
17339 /* If we didn't see `template', we don't know if there's a
17340 template-id or not. */
17341 if (!template_p)
17342 cp_parser_parse_tentatively (parser);
17343 /* Parse the template-id. */
17344 token = cp_lexer_peek_token (parser->lexer);
17345 decl = cp_parser_template_id (parser, template_p,
17346 /*check_dependency_p=*/true,
17347 tag_type,
17348 is_declaration);
17349 /* If we didn't find a template-id, look for an ordinary
17350 identifier. */
17351 if (!template_p && !cp_parser_parse_definitely (parser))
17353 /* We can get here when cp_parser_template_id, called by
17354 cp_parser_class_name with tag_type == none_type, succeeds
17355 and caches a BASELINK. Then, when called again here,
17356 instead of failing and returning an error_mark_node
17357 returns it (see template/typename17.C in C++11).
17358 ??? Could we diagnose this earlier? */
17359 else if (tag_type == typename_type && BASELINK_P (decl))
17361 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17362 type = error_mark_node;
17364 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17365 in effect, then we must assume that, upon instantiation, the
17366 template will correspond to a class. */
17367 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17368 && tag_type == typename_type)
17369 type = make_typename_type (parser->scope, decl,
17370 typename_type,
17371 /*complain=*/tf_error);
17372 /* If the `typename' keyword is in effect and DECL is not a type
17373 decl, then type is non existent. */
17374 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17376 else if (TREE_CODE (decl) == TYPE_DECL)
17378 type = check_elaborated_type_specifier (tag_type, decl,
17379 /*allow_template_p=*/true);
17381 /* If the next token is a semicolon, this must be a specialization,
17382 instantiation, or friend declaration. Check the scope while we
17383 still know whether or not we had a nested-name-specifier. */
17384 if (type != error_mark_node
17385 && !nested_name_specifier && !is_friend
17386 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17387 check_unqualified_spec_or_inst (type, token->location);
17389 else if (decl == error_mark_node)
17390 type = error_mark_node;
17393 if (!type)
17395 token = cp_lexer_peek_token (parser->lexer);
17396 identifier = cp_parser_identifier (parser);
17398 if (identifier == error_mark_node)
17400 parser->scope = NULL_TREE;
17401 return error_mark_node;
17404 /* For a `typename', we needn't call xref_tag. */
17405 if (tag_type == typename_type
17406 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17407 return cp_parser_make_typename_type (parser, identifier,
17408 token->location);
17410 /* Template parameter lists apply only if we are not within a
17411 function parameter list. */
17412 bool template_parm_lists_apply
17413 = parser->num_template_parameter_lists;
17414 if (template_parm_lists_apply)
17415 for (cp_binding_level *s = current_binding_level;
17416 s && s->kind != sk_template_parms;
17417 s = s->level_chain)
17418 if (s->kind == sk_function_parms)
17419 template_parm_lists_apply = false;
17421 /* Look up a qualified name in the usual way. */
17422 if (parser->scope)
17424 tree decl;
17425 tree ambiguous_decls;
17427 decl = cp_parser_lookup_name (parser, identifier,
17428 tag_type,
17429 /*is_template=*/false,
17430 /*is_namespace=*/false,
17431 /*check_dependency=*/true,
17432 &ambiguous_decls,
17433 token->location);
17435 /* If the lookup was ambiguous, an error will already have been
17436 issued. */
17437 if (ambiguous_decls)
17438 return error_mark_node;
17440 /* If we are parsing friend declaration, DECL may be a
17441 TEMPLATE_DECL tree node here. However, we need to check
17442 whether this TEMPLATE_DECL results in valid code. Consider
17443 the following example:
17445 namespace N {
17446 template <class T> class C {};
17448 class X {
17449 template <class T> friend class N::C; // #1, valid code
17451 template <class T> class Y {
17452 friend class N::C; // #2, invalid code
17455 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17456 name lookup of `N::C'. We see that friend declaration must
17457 be template for the code to be valid. Note that
17458 processing_template_decl does not work here since it is
17459 always 1 for the above two cases. */
17461 decl = (cp_parser_maybe_treat_template_as_class
17462 (decl, /*tag_name_p=*/is_friend
17463 && template_parm_lists_apply));
17465 if (TREE_CODE (decl) != TYPE_DECL)
17467 cp_parser_diagnose_invalid_type_name (parser,
17468 identifier,
17469 token->location);
17470 return error_mark_node;
17473 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17475 bool allow_template = (template_parm_lists_apply
17476 || DECL_SELF_REFERENCE_P (decl));
17477 type = check_elaborated_type_specifier (tag_type, decl,
17478 allow_template);
17480 if (type == error_mark_node)
17481 return error_mark_node;
17484 /* Forward declarations of nested types, such as
17486 class C1::C2;
17487 class C1::C2::C3;
17489 are invalid unless all components preceding the final '::'
17490 are complete. If all enclosing types are complete, these
17491 declarations become merely pointless.
17493 Invalid forward declarations of nested types are errors
17494 caught elsewhere in parsing. Those that are pointless arrive
17495 here. */
17497 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17498 && !is_friend && !processing_explicit_instantiation)
17499 warning (0, "declaration %qD does not declare anything", decl);
17501 type = TREE_TYPE (decl);
17503 else
17505 /* An elaborated-type-specifier sometimes introduces a new type and
17506 sometimes names an existing type. Normally, the rule is that it
17507 introduces a new type only if there is not an existing type of
17508 the same name already in scope. For example, given:
17510 struct S {};
17511 void f() { struct S s; }
17513 the `struct S' in the body of `f' is the same `struct S' as in
17514 the global scope; the existing definition is used. However, if
17515 there were no global declaration, this would introduce a new
17516 local class named `S'.
17518 An exception to this rule applies to the following code:
17520 namespace N { struct S; }
17522 Here, the elaborated-type-specifier names a new type
17523 unconditionally; even if there is already an `S' in the
17524 containing scope this declaration names a new type.
17525 This exception only applies if the elaborated-type-specifier
17526 forms the complete declaration:
17528 [class.name]
17530 A declaration consisting solely of `class-key identifier ;' is
17531 either a redeclaration of the name in the current scope or a
17532 forward declaration of the identifier as a class name. It
17533 introduces the name into the current scope.
17535 We are in this situation precisely when the next token is a `;'.
17537 An exception to the exception is that a `friend' declaration does
17538 *not* name a new type; i.e., given:
17540 struct S { friend struct T; };
17542 `T' is not a new type in the scope of `S'.
17544 Also, `new struct S' or `sizeof (struct S)' never results in the
17545 definition of a new type; a new type can only be declared in a
17546 declaration context. */
17548 tag_scope ts;
17549 bool template_p;
17551 if (is_friend)
17552 /* Friends have special name lookup rules. */
17553 ts = ts_within_enclosing_non_class;
17554 else if (is_declaration
17555 && cp_lexer_next_token_is (parser->lexer,
17556 CPP_SEMICOLON))
17557 /* This is a `class-key identifier ;' */
17558 ts = ts_current;
17559 else
17560 ts = ts_global;
17562 template_p =
17563 (template_parm_lists_apply
17564 && (cp_parser_next_token_starts_class_definition_p (parser)
17565 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17566 /* An unqualified name was used to reference this type, so
17567 there were no qualifying templates. */
17568 if (template_parm_lists_apply
17569 && !cp_parser_check_template_parameters (parser,
17570 /*num_templates=*/0,
17571 token->location,
17572 /*declarator=*/NULL))
17573 return error_mark_node;
17574 type = xref_tag (tag_type, identifier, ts, template_p);
17578 if (type == error_mark_node)
17579 return error_mark_node;
17581 /* Allow attributes on forward declarations of classes. */
17582 if (attributes)
17584 if (TREE_CODE (type) == TYPENAME_TYPE)
17585 warning (OPT_Wattributes,
17586 "attributes ignored on uninstantiated type");
17587 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17588 && ! processing_explicit_instantiation)
17589 warning (OPT_Wattributes,
17590 "attributes ignored on template instantiation");
17591 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17592 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17593 else
17594 warning (OPT_Wattributes,
17595 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17598 if (tag_type != enum_type)
17600 /* Indicate whether this class was declared as a `class' or as a
17601 `struct'. */
17602 if (CLASS_TYPE_P (type))
17603 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17604 cp_parser_check_class_key (tag_type, type);
17607 /* A "<" cannot follow an elaborated type specifier. If that
17608 happens, the user was probably trying to form a template-id. */
17609 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17610 token->location);
17612 return type;
17615 /* Parse an enum-specifier.
17617 enum-specifier:
17618 enum-head { enumerator-list [opt] }
17619 enum-head { enumerator-list , } [C++0x]
17621 enum-head:
17622 enum-key identifier [opt] enum-base [opt]
17623 enum-key nested-name-specifier identifier enum-base [opt]
17625 enum-key:
17626 enum
17627 enum class [C++0x]
17628 enum struct [C++0x]
17630 enum-base: [C++0x]
17631 : type-specifier-seq
17633 opaque-enum-specifier:
17634 enum-key identifier enum-base [opt] ;
17636 GNU Extensions:
17637 enum-key attributes[opt] identifier [opt] enum-base [opt]
17638 { enumerator-list [opt] }attributes[opt]
17639 enum-key attributes[opt] identifier [opt] enum-base [opt]
17640 { enumerator-list, }attributes[opt] [C++0x]
17642 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17643 if the token stream isn't an enum-specifier after all. */
17645 static tree
17646 cp_parser_enum_specifier (cp_parser* parser)
17648 tree identifier;
17649 tree type = NULL_TREE;
17650 tree prev_scope;
17651 tree nested_name_specifier = NULL_TREE;
17652 tree attributes;
17653 bool scoped_enum_p = false;
17654 bool has_underlying_type = false;
17655 bool nested_being_defined = false;
17656 bool new_value_list = false;
17657 bool is_new_type = false;
17658 bool is_unnamed = false;
17659 tree underlying_type = NULL_TREE;
17660 cp_token *type_start_token = NULL;
17661 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17663 parser->colon_corrects_to_scope_p = false;
17665 /* Parse tentatively so that we can back up if we don't find a
17666 enum-specifier. */
17667 cp_parser_parse_tentatively (parser);
17669 /* Caller guarantees that the current token is 'enum', an identifier
17670 possibly follows, and the token after that is an opening brace.
17671 If we don't have an identifier, fabricate an anonymous name for
17672 the enumeration being defined. */
17673 cp_lexer_consume_token (parser->lexer);
17675 /* Parse the "class" or "struct", which indicates a scoped
17676 enumeration type in C++0x. */
17677 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17678 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17680 if (cxx_dialect < cxx11)
17681 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17683 /* Consume the `struct' or `class' token. */
17684 cp_lexer_consume_token (parser->lexer);
17686 scoped_enum_p = true;
17689 attributes = cp_parser_attributes_opt (parser);
17691 /* Clear the qualification. */
17692 parser->scope = NULL_TREE;
17693 parser->qualifying_scope = NULL_TREE;
17694 parser->object_scope = NULL_TREE;
17696 /* Figure out in what scope the declaration is being placed. */
17697 prev_scope = current_scope ();
17699 type_start_token = cp_lexer_peek_token (parser->lexer);
17701 push_deferring_access_checks (dk_no_check);
17702 nested_name_specifier
17703 = cp_parser_nested_name_specifier_opt (parser,
17704 /*typename_keyword_p=*/true,
17705 /*check_dependency_p=*/false,
17706 /*type_p=*/false,
17707 /*is_declaration=*/false);
17709 if (nested_name_specifier)
17711 tree name;
17713 identifier = cp_parser_identifier (parser);
17714 name = cp_parser_lookup_name (parser, identifier,
17715 enum_type,
17716 /*is_template=*/false,
17717 /*is_namespace=*/false,
17718 /*check_dependency=*/true,
17719 /*ambiguous_decls=*/NULL,
17720 input_location);
17721 if (name && name != error_mark_node)
17723 type = TREE_TYPE (name);
17724 if (TREE_CODE (type) == TYPENAME_TYPE)
17726 /* Are template enums allowed in ISO? */
17727 if (template_parm_scope_p ())
17728 pedwarn (type_start_token->location, OPT_Wpedantic,
17729 "%qD is an enumeration template", name);
17730 /* ignore a typename reference, for it will be solved by name
17731 in start_enum. */
17732 type = NULL_TREE;
17735 else if (nested_name_specifier == error_mark_node)
17736 /* We already issued an error. */;
17737 else
17739 error_at (type_start_token->location,
17740 "%qD does not name an enumeration in %qT",
17741 identifier, nested_name_specifier);
17742 nested_name_specifier = error_mark_node;
17745 else
17747 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17748 identifier = cp_parser_identifier (parser);
17749 else
17751 identifier = make_anon_name ();
17752 is_unnamed = true;
17753 if (scoped_enum_p)
17754 error_at (type_start_token->location,
17755 "unnamed scoped enum is not allowed");
17758 pop_deferring_access_checks ();
17760 /* Check for the `:' that denotes a specified underlying type in C++0x.
17761 Note that a ':' could also indicate a bitfield width, however. */
17762 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17764 cp_decl_specifier_seq type_specifiers;
17766 /* Consume the `:'. */
17767 cp_lexer_consume_token (parser->lexer);
17769 /* Parse the type-specifier-seq. */
17770 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17771 /*is_trailing_return=*/false,
17772 &type_specifiers);
17774 /* At this point this is surely not elaborated type specifier. */
17775 if (!cp_parser_parse_definitely (parser))
17776 return NULL_TREE;
17778 if (cxx_dialect < cxx11)
17779 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17781 has_underlying_type = true;
17783 /* If that didn't work, stop. */
17784 if (type_specifiers.type != error_mark_node)
17786 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17787 /*initialized=*/0, NULL);
17788 if (underlying_type == error_mark_node
17789 || check_for_bare_parameter_packs (underlying_type))
17790 underlying_type = NULL_TREE;
17794 /* Look for the `{' but don't consume it yet. */
17795 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17797 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17799 cp_parser_error (parser, "expected %<{%>");
17800 if (has_underlying_type)
17802 type = NULL_TREE;
17803 goto out;
17806 /* An opaque-enum-specifier must have a ';' here. */
17807 if ((scoped_enum_p || underlying_type)
17808 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17810 cp_parser_error (parser, "expected %<;%> or %<{%>");
17811 if (has_underlying_type)
17813 type = NULL_TREE;
17814 goto out;
17819 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17820 return NULL_TREE;
17822 if (nested_name_specifier)
17824 if (CLASS_TYPE_P (nested_name_specifier))
17826 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17827 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17828 push_scope (nested_name_specifier);
17830 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17832 push_nested_namespace (nested_name_specifier);
17836 /* Issue an error message if type-definitions are forbidden here. */
17837 if (!cp_parser_check_type_definition (parser))
17838 type = error_mark_node;
17839 else
17840 /* Create the new type. We do this before consuming the opening
17841 brace so the enum will be recorded as being on the line of its
17842 tag (or the 'enum' keyword, if there is no tag). */
17843 type = start_enum (identifier, type, underlying_type,
17844 attributes, scoped_enum_p, &is_new_type);
17846 /* If the next token is not '{' it is an opaque-enum-specifier or an
17847 elaborated-type-specifier. */
17848 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17850 timevar_push (TV_PARSE_ENUM);
17851 if (nested_name_specifier
17852 && nested_name_specifier != error_mark_node)
17854 /* The following catches invalid code such as:
17855 enum class S<int>::E { A, B, C }; */
17856 if (!processing_specialization
17857 && CLASS_TYPE_P (nested_name_specifier)
17858 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
17859 error_at (type_start_token->location, "cannot add an enumerator "
17860 "list to a template instantiation");
17862 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
17864 error_at (type_start_token->location,
17865 "%<%T::%E%> has not been declared",
17866 TYPE_CONTEXT (nested_name_specifier),
17867 nested_name_specifier);
17868 type = error_mark_node;
17870 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
17871 && !CLASS_TYPE_P (nested_name_specifier))
17873 error_at (type_start_token->location, "nested name specifier "
17874 "%qT for enum declaration does not name a class "
17875 "or namespace", nested_name_specifier);
17876 type = error_mark_node;
17878 /* If that scope does not contain the scope in which the
17879 class was originally declared, the program is invalid. */
17880 else if (prev_scope && !is_ancestor (prev_scope,
17881 nested_name_specifier))
17883 if (at_namespace_scope_p ())
17884 error_at (type_start_token->location,
17885 "declaration of %qD in namespace %qD which does not "
17886 "enclose %qD",
17887 type, prev_scope, nested_name_specifier);
17888 else
17889 error_at (type_start_token->location,
17890 "declaration of %qD in %qD which does not "
17891 "enclose %qD",
17892 type, prev_scope, nested_name_specifier);
17893 type = error_mark_node;
17895 /* If that scope is the scope where the declaration is being placed
17896 the program is invalid. */
17897 else if (CLASS_TYPE_P (nested_name_specifier)
17898 && CLASS_TYPE_P (prev_scope)
17899 && same_type_p (nested_name_specifier, prev_scope))
17901 permerror (type_start_token->location,
17902 "extra qualification not allowed");
17903 nested_name_specifier = NULL_TREE;
17907 if (scoped_enum_p)
17908 begin_scope (sk_scoped_enum, type);
17910 /* Consume the opening brace. */
17911 cp_lexer_consume_token (parser->lexer);
17913 if (type == error_mark_node)
17914 ; /* Nothing to add */
17915 else if (OPAQUE_ENUM_P (type)
17916 || (cxx_dialect > cxx98 && processing_specialization))
17918 new_value_list = true;
17919 SET_OPAQUE_ENUM_P (type, false);
17920 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17922 else
17924 error_at (type_start_token->location,
17925 "multiple definition of %q#T", type);
17926 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
17927 "previous definition here");
17928 type = error_mark_node;
17931 if (type == error_mark_node)
17932 cp_parser_skip_to_end_of_block_or_statement (parser);
17933 /* If the next token is not '}', then there are some enumerators. */
17934 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
17936 if (is_unnamed && !scoped_enum_p)
17937 pedwarn (type_start_token->location, OPT_Wpedantic,
17938 "ISO C++ forbids empty unnamed enum");
17940 else
17941 cp_parser_enumerator_list (parser, type);
17943 /* Consume the final '}'. */
17944 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17946 if (scoped_enum_p)
17947 finish_scope ();
17948 timevar_pop (TV_PARSE_ENUM);
17950 else
17952 /* If a ';' follows, then it is an opaque-enum-specifier
17953 and additional restrictions apply. */
17954 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17956 if (is_unnamed)
17957 error_at (type_start_token->location,
17958 "opaque-enum-specifier without name");
17959 else if (nested_name_specifier)
17960 error_at (type_start_token->location,
17961 "opaque-enum-specifier must use a simple identifier");
17965 /* Look for trailing attributes to apply to this enumeration, and
17966 apply them if appropriate. */
17967 if (cp_parser_allow_gnu_extensions_p (parser))
17969 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
17970 cplus_decl_attributes (&type,
17971 trailing_attr,
17972 (int) ATTR_FLAG_TYPE_IN_PLACE);
17975 /* Finish up the enumeration. */
17976 if (type != error_mark_node)
17978 if (new_value_list)
17979 finish_enum_value_list (type);
17980 if (is_new_type)
17981 finish_enum (type);
17984 if (nested_name_specifier)
17986 if (CLASS_TYPE_P (nested_name_specifier))
17988 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
17989 pop_scope (nested_name_specifier);
17991 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17993 pop_nested_namespace (nested_name_specifier);
17996 out:
17997 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17998 return type;
18001 /* Parse an enumerator-list. The enumerators all have the indicated
18002 TYPE.
18004 enumerator-list:
18005 enumerator-definition
18006 enumerator-list , enumerator-definition */
18008 static void
18009 cp_parser_enumerator_list (cp_parser* parser, tree type)
18011 while (true)
18013 /* Parse an enumerator-definition. */
18014 cp_parser_enumerator_definition (parser, type);
18016 /* If the next token is not a ',', we've reached the end of
18017 the list. */
18018 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18019 break;
18020 /* Otherwise, consume the `,' and keep going. */
18021 cp_lexer_consume_token (parser->lexer);
18022 /* If the next token is a `}', there is a trailing comma. */
18023 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18025 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18026 pedwarn (input_location, OPT_Wpedantic,
18027 "comma at end of enumerator list");
18028 break;
18033 /* Parse an enumerator-definition. The enumerator has the indicated
18034 TYPE.
18036 enumerator-definition:
18037 enumerator
18038 enumerator = constant-expression
18040 enumerator:
18041 identifier
18043 GNU Extensions:
18045 enumerator-definition:
18046 enumerator attributes [opt]
18047 enumerator attributes [opt] = constant-expression */
18049 static void
18050 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18052 tree identifier;
18053 tree value;
18054 location_t loc;
18056 /* Save the input location because we are interested in the location
18057 of the identifier and not the location of the explicit value. */
18058 loc = cp_lexer_peek_token (parser->lexer)->location;
18060 /* Look for the identifier. */
18061 identifier = cp_parser_identifier (parser);
18062 if (identifier == error_mark_node)
18063 return;
18065 /* Parse any specified attributes. */
18066 tree attrs = cp_parser_attributes_opt (parser);
18068 /* If the next token is an '=', then there is an explicit value. */
18069 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18071 /* Consume the `=' token. */
18072 cp_lexer_consume_token (parser->lexer);
18073 /* Parse the value. */
18074 value = cp_parser_constant_expression (parser);
18076 else
18077 value = NULL_TREE;
18079 /* If we are processing a template, make sure the initializer of the
18080 enumerator doesn't contain any bare template parameter pack. */
18081 if (check_for_bare_parameter_packs (value))
18082 value = error_mark_node;
18084 /* Create the enumerator. */
18085 build_enumerator (identifier, value, type, attrs, loc);
18088 /* Parse a namespace-name.
18090 namespace-name:
18091 original-namespace-name
18092 namespace-alias
18094 Returns the NAMESPACE_DECL for the namespace. */
18096 static tree
18097 cp_parser_namespace_name (cp_parser* parser)
18099 tree identifier;
18100 tree namespace_decl;
18102 cp_token *token = cp_lexer_peek_token (parser->lexer);
18104 /* Get the name of the namespace. */
18105 identifier = cp_parser_identifier (parser);
18106 if (identifier == error_mark_node)
18107 return error_mark_node;
18109 /* Look up the identifier in the currently active scope. Look only
18110 for namespaces, due to:
18112 [basic.lookup.udir]
18114 When looking up a namespace-name in a using-directive or alias
18115 definition, only namespace names are considered.
18117 And:
18119 [basic.lookup.qual]
18121 During the lookup of a name preceding the :: scope resolution
18122 operator, object, function, and enumerator names are ignored.
18124 (Note that cp_parser_qualifying_entity only calls this
18125 function if the token after the name is the scope resolution
18126 operator.) */
18127 namespace_decl = cp_parser_lookup_name (parser, identifier,
18128 none_type,
18129 /*is_template=*/false,
18130 /*is_namespace=*/true,
18131 /*check_dependency=*/true,
18132 /*ambiguous_decls=*/NULL,
18133 token->location);
18134 /* If it's not a namespace, issue an error. */
18135 if (namespace_decl == error_mark_node
18136 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18138 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18139 error_at (token->location, "%qD is not a namespace-name", identifier);
18140 cp_parser_error (parser, "expected namespace-name");
18141 namespace_decl = error_mark_node;
18144 return namespace_decl;
18147 /* Parse a namespace-definition.
18149 namespace-definition:
18150 named-namespace-definition
18151 unnamed-namespace-definition
18153 named-namespace-definition:
18154 original-namespace-definition
18155 extension-namespace-definition
18157 original-namespace-definition:
18158 namespace identifier { namespace-body }
18160 extension-namespace-definition:
18161 namespace original-namespace-name { namespace-body }
18163 unnamed-namespace-definition:
18164 namespace { namespace-body } */
18166 static void
18167 cp_parser_namespace_definition (cp_parser* parser)
18169 tree identifier, attribs;
18170 bool has_visibility;
18171 bool is_inline;
18172 cp_token* token;
18173 int nested_definition_count = 0;
18175 cp_ensure_no_omp_declare_simd (parser);
18176 cp_ensure_no_oacc_routine (parser);
18177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
18179 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18180 is_inline = true;
18181 cp_lexer_consume_token (parser->lexer);
18183 else
18184 is_inline = false;
18186 /* Look for the `namespace' keyword. */
18187 token = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18189 /* Parse any specified attributes before the identifier. */
18190 attribs = cp_parser_attributes_opt (parser);
18192 /* Get the name of the namespace. We do not attempt to distinguish
18193 between an original-namespace-definition and an
18194 extension-namespace-definition at this point. The semantic
18195 analysis routines are responsible for that. */
18196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18197 identifier = cp_parser_identifier (parser);
18198 else
18199 identifier = NULL_TREE;
18201 /* Parse any specified attributes after the identifier. */
18202 tree post_ident_attribs = cp_parser_attributes_opt (parser);
18203 if (post_ident_attribs)
18205 if (attribs)
18206 attribs = chainon (attribs, post_ident_attribs);
18207 else
18208 attribs = post_ident_attribs;
18211 /* Start the namespace. */
18212 bool ok = push_namespace (identifier);
18214 /* Parse any nested namespace definition. */
18215 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18217 if (attribs)
18218 error_at (token->location, "a nested namespace definition cannot have attributes");
18219 if (cxx_dialect < cxx1z)
18220 pedwarn (input_location, OPT_Wpedantic,
18221 "nested namespace definitions only available with "
18222 "-std=c++1z or -std=gnu++1z");
18223 if (is_inline)
18224 error_at (token->location, "a nested namespace definition cannot be inline");
18225 while (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18227 cp_lexer_consume_token (parser->lexer);
18228 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18229 identifier = cp_parser_identifier (parser);
18230 else
18232 cp_parser_error (parser, "nested identifier required");
18233 break;
18235 if (push_namespace (identifier))
18236 ++nested_definition_count;
18240 /* Look for the `{' to validate starting the namespace. */
18241 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18243 /* "inline namespace" is equivalent to a stub namespace definition
18244 followed by a strong using directive. */
18245 if (is_inline && ok)
18247 tree name_space = current_namespace;
18248 /* Set up namespace association. */
18249 DECL_NAMESPACE_ASSOCIATIONS (name_space)
18250 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
18251 DECL_NAMESPACE_ASSOCIATIONS (name_space));
18252 /* Import the contents of the inline namespace. */
18253 pop_namespace ();
18254 do_using_directive (name_space);
18255 push_namespace (identifier);
18258 has_visibility = handle_namespace_attrs (current_namespace, attribs);
18260 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18262 /* Parse the body of the namespace. */
18263 cp_parser_namespace_body (parser);
18265 if (has_visibility)
18266 pop_visibility (1);
18268 /* Finish the nested namespace definitions. */
18269 while (nested_definition_count--)
18270 pop_namespace ();
18272 /* Finish the namespace. */
18273 if (ok)
18274 pop_namespace ();
18275 /* Look for the final `}'. */
18276 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18279 /* Parse a namespace-body.
18281 namespace-body:
18282 declaration-seq [opt] */
18284 static void
18285 cp_parser_namespace_body (cp_parser* parser)
18287 cp_parser_declaration_seq_opt (parser);
18290 /* Parse a namespace-alias-definition.
18292 namespace-alias-definition:
18293 namespace identifier = qualified-namespace-specifier ; */
18295 static void
18296 cp_parser_namespace_alias_definition (cp_parser* parser)
18298 tree identifier;
18299 tree namespace_specifier;
18301 cp_token *token = cp_lexer_peek_token (parser->lexer);
18303 /* Look for the `namespace' keyword. */
18304 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18305 /* Look for the identifier. */
18306 identifier = cp_parser_identifier (parser);
18307 if (identifier == error_mark_node)
18308 return;
18309 /* Look for the `=' token. */
18310 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18311 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18313 error_at (token->location, "%<namespace%> definition is not allowed here");
18314 /* Skip the definition. */
18315 cp_lexer_consume_token (parser->lexer);
18316 if (cp_parser_skip_to_closing_brace (parser))
18317 cp_lexer_consume_token (parser->lexer);
18318 return;
18320 cp_parser_require (parser, CPP_EQ, RT_EQ);
18321 /* Look for the qualified-namespace-specifier. */
18322 namespace_specifier
18323 = cp_parser_qualified_namespace_specifier (parser);
18324 /* Look for the `;' token. */
18325 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18327 /* Register the alias in the symbol table. */
18328 do_namespace_alias (identifier, namespace_specifier);
18331 /* Parse a qualified-namespace-specifier.
18333 qualified-namespace-specifier:
18334 :: [opt] nested-name-specifier [opt] namespace-name
18336 Returns a NAMESPACE_DECL corresponding to the specified
18337 namespace. */
18339 static tree
18340 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18342 /* Look for the optional `::'. */
18343 cp_parser_global_scope_opt (parser,
18344 /*current_scope_valid_p=*/false);
18346 /* Look for the optional nested-name-specifier. */
18347 cp_parser_nested_name_specifier_opt (parser,
18348 /*typename_keyword_p=*/false,
18349 /*check_dependency_p=*/true,
18350 /*type_p=*/false,
18351 /*is_declaration=*/true);
18353 return cp_parser_namespace_name (parser);
18356 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18357 access declaration.
18359 using-declaration:
18360 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18361 using :: unqualified-id ;
18363 access-declaration:
18364 qualified-id ;
18368 static bool
18369 cp_parser_using_declaration (cp_parser* parser,
18370 bool access_declaration_p)
18372 cp_token *token;
18373 bool typename_p = false;
18374 bool global_scope_p;
18375 tree decl;
18376 tree identifier;
18377 tree qscope;
18378 int oldcount = errorcount;
18379 cp_token *diag_token = NULL;
18381 if (access_declaration_p)
18383 diag_token = cp_lexer_peek_token (parser->lexer);
18384 cp_parser_parse_tentatively (parser);
18386 else
18388 /* Look for the `using' keyword. */
18389 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18391 again:
18392 /* Peek at the next token. */
18393 token = cp_lexer_peek_token (parser->lexer);
18394 /* See if it's `typename'. */
18395 if (token->keyword == RID_TYPENAME)
18397 /* Remember that we've seen it. */
18398 typename_p = true;
18399 /* Consume the `typename' token. */
18400 cp_lexer_consume_token (parser->lexer);
18404 /* Look for the optional global scope qualification. */
18405 global_scope_p
18406 = (cp_parser_global_scope_opt (parser,
18407 /*current_scope_valid_p=*/false)
18408 != NULL_TREE);
18410 /* If we saw `typename', or didn't see `::', then there must be a
18411 nested-name-specifier present. */
18412 if (typename_p || !global_scope_p)
18414 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18415 /*check_dependency_p=*/true,
18416 /*type_p=*/false,
18417 /*is_declaration=*/true);
18418 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18420 cp_parser_skip_to_end_of_block_or_statement (parser);
18421 return false;
18424 /* Otherwise, we could be in either of the two productions. In that
18425 case, treat the nested-name-specifier as optional. */
18426 else
18427 qscope = cp_parser_nested_name_specifier_opt (parser,
18428 /*typename_keyword_p=*/false,
18429 /*check_dependency_p=*/true,
18430 /*type_p=*/false,
18431 /*is_declaration=*/true);
18432 if (!qscope)
18433 qscope = global_namespace;
18434 else if (UNSCOPED_ENUM_P (qscope))
18435 qscope = CP_TYPE_CONTEXT (qscope);
18437 if (access_declaration_p && cp_parser_error_occurred (parser))
18438 /* Something has already gone wrong; there's no need to parse
18439 further. Since an error has occurred, the return value of
18440 cp_parser_parse_definitely will be false, as required. */
18441 return cp_parser_parse_definitely (parser);
18443 token = cp_lexer_peek_token (parser->lexer);
18444 /* Parse the unqualified-id. */
18445 identifier = cp_parser_unqualified_id (parser,
18446 /*template_keyword_p=*/false,
18447 /*check_dependency_p=*/true,
18448 /*declarator_p=*/true,
18449 /*optional_p=*/false);
18451 if (access_declaration_p)
18453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18454 cp_parser_simulate_error (parser);
18455 if (!cp_parser_parse_definitely (parser))
18456 return false;
18458 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18460 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18461 if (cxx_dialect < cxx1z
18462 && !in_system_header_at (ell->location))
18463 pedwarn (ell->location, 0,
18464 "pack expansion in using-declaration only available "
18465 "with -std=c++1z or -std=gnu++1z");
18466 qscope = make_pack_expansion (qscope);
18469 /* The function we call to handle a using-declaration is different
18470 depending on what scope we are in. */
18471 if (qscope == error_mark_node || identifier == error_mark_node)
18473 else if (!identifier_p (identifier)
18474 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18475 /* [namespace.udecl]
18477 A using declaration shall not name a template-id. */
18478 error_at (token->location,
18479 "a template-id may not appear in a using-declaration");
18480 else
18482 if (at_class_scope_p ())
18484 /* Create the USING_DECL. */
18485 decl = do_class_using_decl (qscope, identifier);
18487 if (decl && typename_p)
18488 USING_DECL_TYPENAME_P (decl) = 1;
18490 if (check_for_bare_parameter_packs (decl))
18492 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18493 return false;
18495 else
18496 /* Add it to the list of members in this class. */
18497 finish_member_declaration (decl);
18499 else
18501 decl = cp_parser_lookup_name_simple (parser,
18502 identifier,
18503 token->location);
18504 if (decl == error_mark_node)
18505 cp_parser_name_lookup_error (parser, identifier,
18506 decl, NLE_NULL,
18507 token->location);
18508 else if (check_for_bare_parameter_packs (decl))
18510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18511 return false;
18513 else if (!at_namespace_scope_p ())
18514 do_local_using_decl (decl, qscope, identifier);
18515 else
18516 do_toplevel_using_decl (decl, qscope, identifier);
18520 if (!access_declaration_p
18521 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18523 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18524 if (cxx_dialect < cxx1z)
18525 pedwarn (comma->location, 0,
18526 "comma-separated list in using-declaration only available "
18527 "with -std=c++1z or -std=gnu++1z");
18528 goto again;
18531 /* Look for the final `;'. */
18532 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18534 if (access_declaration_p && errorcount == oldcount)
18535 warning_at (diag_token->location, OPT_Wdeprecated,
18536 "access declarations are deprecated "
18537 "in favour of using-declarations; "
18538 "suggestion: add the %<using%> keyword");
18540 return true;
18543 /* Parse an alias-declaration.
18545 alias-declaration:
18546 using identifier attribute-specifier-seq [opt] = type-id */
18548 static tree
18549 cp_parser_alias_declaration (cp_parser* parser)
18551 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18552 location_t id_location;
18553 cp_declarator *declarator;
18554 cp_decl_specifier_seq decl_specs;
18555 bool member_p;
18556 const char *saved_message = NULL;
18558 /* Look for the `using' keyword. */
18559 cp_token *using_token
18560 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18561 if (using_token == NULL)
18562 return error_mark_node;
18564 id_location = cp_lexer_peek_token (parser->lexer)->location;
18565 id = cp_parser_identifier (parser);
18566 if (id == error_mark_node)
18567 return error_mark_node;
18569 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18570 attributes = cp_parser_attributes_opt (parser);
18571 if (attributes == error_mark_node)
18572 return error_mark_node;
18574 cp_parser_require (parser, CPP_EQ, RT_EQ);
18576 if (cp_parser_error_occurred (parser))
18577 return error_mark_node;
18579 cp_parser_commit_to_tentative_parse (parser);
18581 /* Now we are going to parse the type-id of the declaration. */
18584 [dcl.type]/3 says:
18586 "A type-specifier-seq shall not define a class or enumeration
18587 unless it appears in the type-id of an alias-declaration (7.1.3) that
18588 is not the declaration of a template-declaration."
18590 In other words, if we currently are in an alias template, the
18591 type-id should not define a type.
18593 So let's set parser->type_definition_forbidden_message in that
18594 case; cp_parser_check_type_definition (called by
18595 cp_parser_class_specifier) will then emit an error if a type is
18596 defined in the type-id. */
18597 if (parser->num_template_parameter_lists)
18599 saved_message = parser->type_definition_forbidden_message;
18600 parser->type_definition_forbidden_message =
18601 G_("types may not be defined in alias template declarations");
18604 type = cp_parser_type_id (parser);
18606 /* Restore the error message if need be. */
18607 if (parser->num_template_parameter_lists)
18608 parser->type_definition_forbidden_message = saved_message;
18610 if (type == error_mark_node
18611 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18613 cp_parser_skip_to_end_of_block_or_statement (parser);
18614 return error_mark_node;
18617 /* A typedef-name can also be introduced by an alias-declaration. The
18618 identifier following the using keyword becomes a typedef-name. It has
18619 the same semantics as if it were introduced by the typedef
18620 specifier. In particular, it does not define a new type and it shall
18621 not appear in the type-id. */
18623 clear_decl_specs (&decl_specs);
18624 decl_specs.type = type;
18625 if (attributes != NULL_TREE)
18627 decl_specs.attributes = attributes;
18628 set_and_check_decl_spec_loc (&decl_specs,
18629 ds_attribute,
18630 attrs_token);
18632 set_and_check_decl_spec_loc (&decl_specs,
18633 ds_typedef,
18634 using_token);
18635 set_and_check_decl_spec_loc (&decl_specs,
18636 ds_alias,
18637 using_token);
18639 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18640 declarator->id_loc = id_location;
18642 member_p = at_class_scope_p ();
18643 if (member_p)
18644 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18645 NULL_TREE, attributes);
18646 else
18647 decl = start_decl (declarator, &decl_specs, 0,
18648 attributes, NULL_TREE, &pushed_scope);
18649 if (decl == error_mark_node)
18650 return decl;
18652 // Attach constraints to the alias declaration.
18653 if (flag_concepts && current_template_parms)
18655 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18656 tree constr = build_constraints (reqs, NULL_TREE);
18657 set_constraints (decl, constr);
18660 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18662 if (pushed_scope)
18663 pop_scope (pushed_scope);
18665 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18666 added into the symbol table; otherwise, return the TYPE_DECL. */
18667 if (DECL_LANG_SPECIFIC (decl)
18668 && DECL_TEMPLATE_INFO (decl)
18669 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18671 decl = DECL_TI_TEMPLATE (decl);
18672 if (member_p)
18673 check_member_template (decl);
18676 return decl;
18679 /* Parse a using-directive.
18681 using-directive:
18682 using namespace :: [opt] nested-name-specifier [opt]
18683 namespace-name ; */
18685 static void
18686 cp_parser_using_directive (cp_parser* parser)
18688 tree namespace_decl;
18689 tree attribs;
18691 /* Look for the `using' keyword. */
18692 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18693 /* And the `namespace' keyword. */
18694 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18695 /* Look for the optional `::' operator. */
18696 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18697 /* And the optional nested-name-specifier. */
18698 cp_parser_nested_name_specifier_opt (parser,
18699 /*typename_keyword_p=*/false,
18700 /*check_dependency_p=*/true,
18701 /*type_p=*/false,
18702 /*is_declaration=*/true);
18703 /* Get the namespace being used. */
18704 namespace_decl = cp_parser_namespace_name (parser);
18705 /* And any specified attributes. */
18706 attribs = cp_parser_attributes_opt (parser);
18707 /* Update the symbol table. */
18708 parse_using_directive (namespace_decl, attribs);
18709 /* Look for the final `;'. */
18710 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18713 /* Parse an asm-definition.
18715 asm-definition:
18716 asm ( string-literal ) ;
18718 GNU Extension:
18720 asm-definition:
18721 asm volatile [opt] ( string-literal ) ;
18722 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18723 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18724 : asm-operand-list [opt] ) ;
18725 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18726 : asm-operand-list [opt]
18727 : asm-clobber-list [opt] ) ;
18728 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18729 : asm-clobber-list [opt]
18730 : asm-goto-list ) ; */
18732 static void
18733 cp_parser_asm_definition (cp_parser* parser)
18735 tree string;
18736 tree outputs = NULL_TREE;
18737 tree inputs = NULL_TREE;
18738 tree clobbers = NULL_TREE;
18739 tree labels = NULL_TREE;
18740 tree asm_stmt;
18741 bool volatile_p = false;
18742 bool extended_p = false;
18743 bool invalid_inputs_p = false;
18744 bool invalid_outputs_p = false;
18745 bool goto_p = false;
18746 required_token missing = RT_NONE;
18748 /* Look for the `asm' keyword. */
18749 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18751 if (parser->in_function_body
18752 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18754 error ("%<asm%> in %<constexpr%> function");
18755 cp_function_chain->invalid_constexpr = true;
18758 /* See if the next token is `volatile'. */
18759 if (cp_parser_allow_gnu_extensions_p (parser)
18760 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18762 /* Remember that we saw the `volatile' keyword. */
18763 volatile_p = true;
18764 /* Consume the token. */
18765 cp_lexer_consume_token (parser->lexer);
18767 if (cp_parser_allow_gnu_extensions_p (parser)
18768 && parser->in_function_body
18769 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18771 /* Remember that we saw the `goto' keyword. */
18772 goto_p = true;
18773 /* Consume the token. */
18774 cp_lexer_consume_token (parser->lexer);
18776 /* Look for the opening `('. */
18777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18778 return;
18779 /* Look for the string. */
18780 string = cp_parser_string_literal (parser, false, false);
18781 if (string == error_mark_node)
18783 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18784 /*consume_paren=*/true);
18785 return;
18788 /* If we're allowing GNU extensions, check for the extended assembly
18789 syntax. Unfortunately, the `:' tokens need not be separated by
18790 a space in C, and so, for compatibility, we tolerate that here
18791 too. Doing that means that we have to treat the `::' operator as
18792 two `:' tokens. */
18793 if (cp_parser_allow_gnu_extensions_p (parser)
18794 && parser->in_function_body
18795 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18796 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18798 bool inputs_p = false;
18799 bool clobbers_p = false;
18800 bool labels_p = false;
18802 /* The extended syntax was used. */
18803 extended_p = true;
18805 /* Look for outputs. */
18806 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18808 /* Consume the `:'. */
18809 cp_lexer_consume_token (parser->lexer);
18810 /* Parse the output-operands. */
18811 if (cp_lexer_next_token_is_not (parser->lexer,
18812 CPP_COLON)
18813 && cp_lexer_next_token_is_not (parser->lexer,
18814 CPP_SCOPE)
18815 && cp_lexer_next_token_is_not (parser->lexer,
18816 CPP_CLOSE_PAREN)
18817 && !goto_p)
18819 outputs = cp_parser_asm_operand_list (parser);
18820 if (outputs == error_mark_node)
18821 invalid_outputs_p = true;
18824 /* If the next token is `::', there are no outputs, and the
18825 next token is the beginning of the inputs. */
18826 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18827 /* The inputs are coming next. */
18828 inputs_p = true;
18830 /* Look for inputs. */
18831 if (inputs_p
18832 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18834 /* Consume the `:' or `::'. */
18835 cp_lexer_consume_token (parser->lexer);
18836 /* Parse the output-operands. */
18837 if (cp_lexer_next_token_is_not (parser->lexer,
18838 CPP_COLON)
18839 && cp_lexer_next_token_is_not (parser->lexer,
18840 CPP_SCOPE)
18841 && cp_lexer_next_token_is_not (parser->lexer,
18842 CPP_CLOSE_PAREN))
18844 inputs = cp_parser_asm_operand_list (parser);
18845 if (inputs == error_mark_node)
18846 invalid_inputs_p = true;
18849 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18850 /* The clobbers are coming next. */
18851 clobbers_p = true;
18853 /* Look for clobbers. */
18854 if (clobbers_p
18855 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18857 clobbers_p = true;
18858 /* Consume the `:' or `::'. */
18859 cp_lexer_consume_token (parser->lexer);
18860 /* Parse the clobbers. */
18861 if (cp_lexer_next_token_is_not (parser->lexer,
18862 CPP_COLON)
18863 && cp_lexer_next_token_is_not (parser->lexer,
18864 CPP_CLOSE_PAREN))
18865 clobbers = cp_parser_asm_clobber_list (parser);
18867 else if (goto_p
18868 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18869 /* The labels are coming next. */
18870 labels_p = true;
18872 /* Look for labels. */
18873 if (labels_p
18874 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18876 labels_p = true;
18877 /* Consume the `:' or `::'. */
18878 cp_lexer_consume_token (parser->lexer);
18879 /* Parse the labels. */
18880 labels = cp_parser_asm_label_list (parser);
18883 if (goto_p && !labels_p)
18884 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
18886 else if (goto_p)
18887 missing = RT_COLON_SCOPE;
18889 /* Look for the closing `)'. */
18890 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
18891 missing ? missing : RT_CLOSE_PAREN))
18892 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18893 /*consume_paren=*/true);
18894 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18896 if (!invalid_inputs_p && !invalid_outputs_p)
18898 /* Create the ASM_EXPR. */
18899 if (parser->in_function_body)
18901 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
18902 inputs, clobbers, labels);
18903 /* If the extended syntax was not used, mark the ASM_EXPR. */
18904 if (!extended_p)
18906 tree temp = asm_stmt;
18907 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
18908 temp = TREE_OPERAND (temp, 0);
18910 ASM_INPUT_P (temp) = 1;
18913 else
18914 symtab->finalize_toplevel_asm (string);
18918 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
18919 type that comes from the decl-specifier-seq. */
18921 static tree
18922 strip_declarator_types (tree type, cp_declarator *declarator)
18924 for (cp_declarator *d = declarator; d;)
18925 switch (d->kind)
18927 case cdk_id:
18928 case cdk_decomp:
18929 case cdk_error:
18930 d = NULL;
18931 break;
18933 default:
18934 if (TYPE_PTRMEMFUNC_P (type))
18935 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
18936 type = TREE_TYPE (type);
18937 d = d->declarator;
18938 break;
18941 return type;
18944 /* Declarators [gram.dcl.decl] */
18946 /* Parse an init-declarator.
18948 init-declarator:
18949 declarator initializer [opt]
18951 GNU Extension:
18953 init-declarator:
18954 declarator asm-specification [opt] attributes [opt] initializer [opt]
18956 function-definition:
18957 decl-specifier-seq [opt] declarator ctor-initializer [opt]
18958 function-body
18959 decl-specifier-seq [opt] declarator function-try-block
18961 GNU Extension:
18963 function-definition:
18964 __extension__ function-definition
18966 TM Extension:
18968 function-definition:
18969 decl-specifier-seq [opt] declarator function-transaction-block
18971 The DECL_SPECIFIERS apply to this declarator. Returns a
18972 representation of the entity declared. If MEMBER_P is TRUE, then
18973 this declarator appears in a class scope. The new DECL created by
18974 this declarator is returned.
18976 The CHECKS are access checks that should be performed once we know
18977 what entity is being declared (and, therefore, what classes have
18978 befriended it).
18980 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
18981 for a function-definition here as well. If the declarator is a
18982 declarator for a function-definition, *FUNCTION_DEFINITION_P will
18983 be TRUE upon return. By that point, the function-definition will
18984 have been completely parsed.
18986 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
18987 is FALSE.
18989 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
18990 parsed declaration if it is an uninitialized single declarator not followed
18991 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
18992 if present, will not be consumed. If returned, this declarator will be
18993 created with SD_INITIALIZED but will not call cp_finish_decl.
18995 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
18996 and there is an initializer, the pointed location_t is set to the
18997 location of the '=' or `(', or '{' in C++11 token introducing the
18998 initializer. */
19000 static tree
19001 cp_parser_init_declarator (cp_parser* parser,
19002 cp_decl_specifier_seq *decl_specifiers,
19003 vec<deferred_access_check, va_gc> *checks,
19004 bool function_definition_allowed_p,
19005 bool member_p,
19006 int declares_class_or_enum,
19007 bool* function_definition_p,
19008 tree* maybe_range_for_decl,
19009 location_t* init_loc,
19010 tree* auto_result)
19012 cp_token *token = NULL, *asm_spec_start_token = NULL,
19013 *attributes_start_token = NULL;
19014 cp_declarator *declarator;
19015 tree prefix_attributes;
19016 tree attributes = NULL;
19017 tree asm_specification;
19018 tree initializer;
19019 tree decl = NULL_TREE;
19020 tree scope;
19021 int is_initialized;
19022 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19023 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19024 "(...)". */
19025 enum cpp_ttype initialization_kind;
19026 bool is_direct_init = false;
19027 bool is_non_constant_init;
19028 int ctor_dtor_or_conv_p;
19029 bool friend_p = cp_parser_friend_p (decl_specifiers);
19030 tree pushed_scope = NULL_TREE;
19031 bool range_for_decl_p = false;
19032 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19033 location_t tmp_init_loc = UNKNOWN_LOCATION;
19035 /* Gather the attributes that were provided with the
19036 decl-specifiers. */
19037 prefix_attributes = decl_specifiers->attributes;
19039 /* Assume that this is not the declarator for a function
19040 definition. */
19041 if (function_definition_p)
19042 *function_definition_p = false;
19044 /* Default arguments are only permitted for function parameters. */
19045 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19046 parser->default_arg_ok_p = false;
19048 /* Defer access checks while parsing the declarator; we cannot know
19049 what names are accessible until we know what is being
19050 declared. */
19051 resume_deferring_access_checks ();
19053 token = cp_lexer_peek_token (parser->lexer);
19055 /* Parse the declarator. */
19056 declarator
19057 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19058 &ctor_dtor_or_conv_p,
19059 /*parenthesized_p=*/NULL,
19060 member_p, friend_p);
19061 /* Gather up the deferred checks. */
19062 stop_deferring_access_checks ();
19064 parser->default_arg_ok_p = saved_default_arg_ok_p;
19066 /* If the DECLARATOR was erroneous, there's no need to go
19067 further. */
19068 if (declarator == cp_error_declarator)
19069 return error_mark_node;
19071 /* Check that the number of template-parameter-lists is OK. */
19072 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19073 token->location))
19074 return error_mark_node;
19076 if (declares_class_or_enum & 2)
19077 cp_parser_check_for_definition_in_return_type (declarator,
19078 decl_specifiers->type,
19079 decl_specifiers->locations[ds_type_spec]);
19081 /* Figure out what scope the entity declared by the DECLARATOR is
19082 located in. `grokdeclarator' sometimes changes the scope, so
19083 we compute it now. */
19084 scope = get_scope_of_declarator (declarator);
19086 /* Perform any lookups in the declared type which were thought to be
19087 dependent, but are not in the scope of the declarator. */
19088 decl_specifiers->type
19089 = maybe_update_decl_type (decl_specifiers->type, scope);
19091 /* If we're allowing GNU extensions, look for an
19092 asm-specification. */
19093 if (cp_parser_allow_gnu_extensions_p (parser))
19095 /* Look for an asm-specification. */
19096 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19097 asm_specification = cp_parser_asm_specification_opt (parser);
19099 else
19100 asm_specification = NULL_TREE;
19102 /* Look for attributes. */
19103 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19104 attributes = cp_parser_attributes_opt (parser);
19106 /* Peek at the next token. */
19107 token = cp_lexer_peek_token (parser->lexer);
19109 bool bogus_implicit_tmpl = false;
19111 if (function_declarator_p (declarator))
19113 /* Handle C++17 deduction guides. */
19114 if (!decl_specifiers->type
19115 && ctor_dtor_or_conv_p <= 0
19116 && cxx_dialect >= cxx1z)
19118 cp_declarator *id = get_id_declarator (declarator);
19119 tree name = id->u.id.unqualified_name;
19120 parser->scope = id->u.id.qualifying_scope;
19121 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19122 if (tmpl
19123 && (DECL_CLASS_TEMPLATE_P (tmpl)
19124 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19126 id->u.id.unqualified_name = dguide_name (tmpl);
19127 id->u.id.sfk = sfk_deduction_guide;
19128 ctor_dtor_or_conv_p = 1;
19132 /* Check to see if the token indicates the start of a
19133 function-definition. */
19134 if (cp_parser_token_starts_function_definition_p (token))
19136 if (!function_definition_allowed_p)
19138 /* If a function-definition should not appear here, issue an
19139 error message. */
19140 cp_parser_error (parser,
19141 "a function-definition is not allowed here");
19142 return error_mark_node;
19145 location_t func_brace_location
19146 = cp_lexer_peek_token (parser->lexer)->location;
19148 /* Neither attributes nor an asm-specification are allowed
19149 on a function-definition. */
19150 if (asm_specification)
19151 error_at (asm_spec_start_token->location,
19152 "an asm-specification is not allowed "
19153 "on a function-definition");
19154 if (attributes)
19155 error_at (attributes_start_token->location,
19156 "attributes are not allowed "
19157 "on a function-definition");
19158 /* This is a function-definition. */
19159 *function_definition_p = true;
19161 /* Parse the function definition. */
19162 if (member_p)
19163 decl = cp_parser_save_member_function_body (parser,
19164 decl_specifiers,
19165 declarator,
19166 prefix_attributes);
19167 else
19168 decl =
19169 (cp_parser_function_definition_from_specifiers_and_declarator
19170 (parser, decl_specifiers, prefix_attributes, declarator));
19172 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19174 /* This is where the prologue starts... */
19175 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19176 = func_brace_location;
19179 return decl;
19182 else if (parser->fully_implicit_function_template_p)
19184 /* A non-template declaration involving a function parameter list
19185 containing an implicit template parameter will be made into a
19186 template. If the resulting declaration is not going to be an
19187 actual function then finish the template scope here to prevent it.
19188 An error message will be issued once we have a decl to talk about.
19190 FIXME probably we should do type deduction rather than create an
19191 implicit template, but the standard currently doesn't allow it. */
19192 bogus_implicit_tmpl = true;
19193 finish_fully_implicit_template (parser, NULL_TREE);
19196 /* [dcl.dcl]
19198 Only in function declarations for constructors, destructors, type
19199 conversions, and deduction guides can the decl-specifier-seq be omitted.
19201 We explicitly postpone this check past the point where we handle
19202 function-definitions because we tolerate function-definitions
19203 that are missing their return types in some modes. */
19204 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19206 cp_parser_error (parser,
19207 "expected constructor, destructor, or type conversion");
19208 return error_mark_node;
19211 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19212 if (token->type == CPP_EQ
19213 || token->type == CPP_OPEN_PAREN
19214 || token->type == CPP_OPEN_BRACE)
19216 is_initialized = SD_INITIALIZED;
19217 initialization_kind = token->type;
19218 if (maybe_range_for_decl)
19219 *maybe_range_for_decl = error_mark_node;
19220 tmp_init_loc = token->location;
19221 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19222 *init_loc = tmp_init_loc;
19224 if (token->type == CPP_EQ
19225 && function_declarator_p (declarator))
19227 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19228 if (t2->keyword == RID_DEFAULT)
19229 is_initialized = SD_DEFAULTED;
19230 else if (t2->keyword == RID_DELETE)
19231 is_initialized = SD_DELETED;
19234 else
19236 /* If the init-declarator isn't initialized and isn't followed by a
19237 `,' or `;', it's not a valid init-declarator. */
19238 if (token->type != CPP_COMMA
19239 && token->type != CPP_SEMICOLON)
19241 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19242 range_for_decl_p = true;
19243 else
19245 if (!maybe_range_for_decl)
19246 cp_parser_error (parser, "expected initializer");
19247 return error_mark_node;
19250 is_initialized = SD_UNINITIALIZED;
19251 initialization_kind = CPP_EOF;
19254 /* Because start_decl has side-effects, we should only call it if we
19255 know we're going ahead. By this point, we know that we cannot
19256 possibly be looking at any other construct. */
19257 cp_parser_commit_to_tentative_parse (parser);
19259 /* Enter the newly declared entry in the symbol table. If we're
19260 processing a declaration in a class-specifier, we wait until
19261 after processing the initializer. */
19262 if (!member_p)
19264 if (parser->in_unbraced_linkage_specification_p)
19265 decl_specifiers->storage_class = sc_extern;
19266 decl = start_decl (declarator, decl_specifiers,
19267 range_for_decl_p? SD_INITIALIZED : is_initialized,
19268 attributes, prefix_attributes, &pushed_scope);
19269 cp_finalize_omp_declare_simd (parser, decl);
19270 cp_finalize_oacc_routine (parser, decl, false);
19271 /* Adjust location of decl if declarator->id_loc is more appropriate:
19272 set, and decl wasn't merged with another decl, in which case its
19273 location would be different from input_location, and more accurate. */
19274 if (DECL_P (decl)
19275 && declarator->id_loc != UNKNOWN_LOCATION
19276 && DECL_SOURCE_LOCATION (decl) == input_location)
19277 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19279 else if (scope)
19280 /* Enter the SCOPE. That way unqualified names appearing in the
19281 initializer will be looked up in SCOPE. */
19282 pushed_scope = push_scope (scope);
19284 /* Perform deferred access control checks, now that we know in which
19285 SCOPE the declared entity resides. */
19286 if (!member_p && decl)
19288 tree saved_current_function_decl = NULL_TREE;
19290 /* If the entity being declared is a function, pretend that we
19291 are in its scope. If it is a `friend', it may have access to
19292 things that would not otherwise be accessible. */
19293 if (TREE_CODE (decl) == FUNCTION_DECL)
19295 saved_current_function_decl = current_function_decl;
19296 current_function_decl = decl;
19299 /* Perform access checks for template parameters. */
19300 cp_parser_perform_template_parameter_access_checks (checks);
19302 /* Perform the access control checks for the declarator and the
19303 decl-specifiers. */
19304 perform_deferred_access_checks (tf_warning_or_error);
19306 /* Restore the saved value. */
19307 if (TREE_CODE (decl) == FUNCTION_DECL)
19308 current_function_decl = saved_current_function_decl;
19311 /* Parse the initializer. */
19312 initializer = NULL_TREE;
19313 is_direct_init = false;
19314 is_non_constant_init = true;
19315 if (is_initialized)
19317 if (function_declarator_p (declarator))
19319 if (initialization_kind == CPP_EQ)
19320 initializer = cp_parser_pure_specifier (parser);
19321 else
19323 /* If the declaration was erroneous, we don't really
19324 know what the user intended, so just silently
19325 consume the initializer. */
19326 if (decl != error_mark_node)
19327 error_at (tmp_init_loc, "initializer provided for function");
19328 cp_parser_skip_to_closing_parenthesis (parser,
19329 /*recovering=*/true,
19330 /*or_comma=*/false,
19331 /*consume_paren=*/true);
19334 else
19336 /* We want to record the extra mangling scope for in-class
19337 initializers of class members and initializers of static data
19338 member templates. The former involves deferring
19339 parsing of the initializer until end of class as with default
19340 arguments. So right here we only handle the latter. */
19341 if (!member_p && processing_template_decl)
19342 start_lambda_scope (decl);
19343 initializer = cp_parser_initializer (parser,
19344 &is_direct_init,
19345 &is_non_constant_init);
19346 if (!member_p && processing_template_decl)
19347 finish_lambda_scope ();
19348 if (initializer == error_mark_node)
19349 cp_parser_skip_to_end_of_statement (parser);
19353 /* The old parser allows attributes to appear after a parenthesized
19354 initializer. Mark Mitchell proposed removing this functionality
19355 on the GCC mailing lists on 2002-08-13. This parser accepts the
19356 attributes -- but ignores them. */
19357 if (cp_parser_allow_gnu_extensions_p (parser)
19358 && initialization_kind == CPP_OPEN_PAREN)
19359 if (cp_parser_attributes_opt (parser))
19360 warning (OPT_Wattributes,
19361 "attributes after parenthesized initializer ignored");
19363 /* And now complain about a non-function implicit template. */
19364 if (bogus_implicit_tmpl && decl != error_mark_node)
19365 error_at (DECL_SOURCE_LOCATION (decl),
19366 "non-function %qD declared as implicit template", decl);
19368 /* For an in-class declaration, use `grokfield' to create the
19369 declaration. */
19370 if (member_p)
19372 if (pushed_scope)
19374 pop_scope (pushed_scope);
19375 pushed_scope = NULL_TREE;
19377 decl = grokfield (declarator, decl_specifiers,
19378 initializer, !is_non_constant_init,
19379 /*asmspec=*/NULL_TREE,
19380 chainon (attributes, prefix_attributes));
19381 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19382 cp_parser_save_default_args (parser, decl);
19383 cp_finalize_omp_declare_simd (parser, decl);
19384 cp_finalize_oacc_routine (parser, decl, false);
19387 /* Finish processing the declaration. But, skip member
19388 declarations. */
19389 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19391 cp_finish_decl (decl,
19392 initializer, !is_non_constant_init,
19393 asm_specification,
19394 /* If the initializer is in parentheses, then this is
19395 a direct-initialization, which means that an
19396 `explicit' constructor is OK. Otherwise, an
19397 `explicit' constructor cannot be used. */
19398 ((is_direct_init || !is_initialized)
19399 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19401 else if ((cxx_dialect != cxx98) && friend_p
19402 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19403 /* Core issue #226 (C++0x only): A default template-argument
19404 shall not be specified in a friend class template
19405 declaration. */
19406 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19407 /*is_partial=*/false, /*is_friend_decl=*/1);
19409 if (!friend_p && pushed_scope)
19410 pop_scope (pushed_scope);
19412 if (function_declarator_p (declarator)
19413 && parser->fully_implicit_function_template_p)
19415 if (member_p)
19416 decl = finish_fully_implicit_template (parser, decl);
19417 else
19418 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19421 if (auto_result && is_initialized && decl_specifiers->type
19422 && type_uses_auto (decl_specifiers->type))
19423 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19425 return decl;
19428 /* Parse a declarator.
19430 declarator:
19431 direct-declarator
19432 ptr-operator declarator
19434 abstract-declarator:
19435 ptr-operator abstract-declarator [opt]
19436 direct-abstract-declarator
19438 GNU Extensions:
19440 declarator:
19441 attributes [opt] direct-declarator
19442 attributes [opt] ptr-operator declarator
19444 abstract-declarator:
19445 attributes [opt] ptr-operator abstract-declarator [opt]
19446 attributes [opt] direct-abstract-declarator
19448 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19449 detect constructors, destructors, deduction guides, or conversion operators.
19450 It is set to -1 if the declarator is a name, and +1 if it is a
19451 function. Otherwise it is set to zero. Usually you just want to
19452 test for >0, but internally the negative value is used.
19454 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19455 a decl-specifier-seq unless it declares a constructor, destructor,
19456 or conversion. It might seem that we could check this condition in
19457 semantic analysis, rather than parsing, but that makes it difficult
19458 to handle something like `f()'. We want to notice that there are
19459 no decl-specifiers, and therefore realize that this is an
19460 expression, not a declaration.)
19462 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19463 the declarator is a direct-declarator of the form "(...)".
19465 MEMBER_P is true iff this declarator is a member-declarator.
19467 FRIEND_P is true iff this declarator is a friend. */
19469 static cp_declarator *
19470 cp_parser_declarator (cp_parser* parser,
19471 cp_parser_declarator_kind dcl_kind,
19472 int* ctor_dtor_or_conv_p,
19473 bool* parenthesized_p,
19474 bool member_p, bool friend_p)
19476 cp_declarator *declarator;
19477 enum tree_code code;
19478 cp_cv_quals cv_quals;
19479 tree class_type;
19480 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19482 /* Assume this is not a constructor, destructor, or type-conversion
19483 operator. */
19484 if (ctor_dtor_or_conv_p)
19485 *ctor_dtor_or_conv_p = 0;
19487 if (cp_parser_allow_gnu_extensions_p (parser))
19488 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19490 /* Check for the ptr-operator production. */
19491 cp_parser_parse_tentatively (parser);
19492 /* Parse the ptr-operator. */
19493 code = cp_parser_ptr_operator (parser,
19494 &class_type,
19495 &cv_quals,
19496 &std_attributes);
19498 /* If that worked, then we have a ptr-operator. */
19499 if (cp_parser_parse_definitely (parser))
19501 /* If a ptr-operator was found, then this declarator was not
19502 parenthesized. */
19503 if (parenthesized_p)
19504 *parenthesized_p = true;
19505 /* The dependent declarator is optional if we are parsing an
19506 abstract-declarator. */
19507 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19508 cp_parser_parse_tentatively (parser);
19510 /* Parse the dependent declarator. */
19511 declarator = cp_parser_declarator (parser, dcl_kind,
19512 /*ctor_dtor_or_conv_p=*/NULL,
19513 /*parenthesized_p=*/NULL,
19514 /*member_p=*/false,
19515 friend_p);
19517 /* If we are parsing an abstract-declarator, we must handle the
19518 case where the dependent declarator is absent. */
19519 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19520 && !cp_parser_parse_definitely (parser))
19521 declarator = NULL;
19523 declarator = cp_parser_make_indirect_declarator
19524 (code, class_type, cv_quals, declarator, std_attributes);
19526 /* Everything else is a direct-declarator. */
19527 else
19529 if (parenthesized_p)
19530 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19531 CPP_OPEN_PAREN);
19532 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19533 ctor_dtor_or_conv_p,
19534 member_p, friend_p);
19537 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19538 declarator->attributes = gnu_attributes;
19539 return declarator;
19542 /* Parse a direct-declarator or direct-abstract-declarator.
19544 direct-declarator:
19545 declarator-id
19546 direct-declarator ( parameter-declaration-clause )
19547 cv-qualifier-seq [opt]
19548 ref-qualifier [opt]
19549 exception-specification [opt]
19550 direct-declarator [ constant-expression [opt] ]
19551 ( declarator )
19553 direct-abstract-declarator:
19554 direct-abstract-declarator [opt]
19555 ( parameter-declaration-clause )
19556 cv-qualifier-seq [opt]
19557 ref-qualifier [opt]
19558 exception-specification [opt]
19559 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19560 ( abstract-declarator )
19562 Returns a representation of the declarator. DCL_KIND is
19563 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19564 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19565 we are parsing a direct-declarator. It is
19566 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19567 of ambiguity we prefer an abstract declarator, as per
19568 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19569 as for cp_parser_declarator. */
19571 static cp_declarator *
19572 cp_parser_direct_declarator (cp_parser* parser,
19573 cp_parser_declarator_kind dcl_kind,
19574 int* ctor_dtor_or_conv_p,
19575 bool member_p, bool friend_p)
19577 cp_token *token;
19578 cp_declarator *declarator = NULL;
19579 tree scope = NULL_TREE;
19580 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19581 bool saved_in_declarator_p = parser->in_declarator_p;
19582 bool first = true;
19583 tree pushed_scope = NULL_TREE;
19585 while (true)
19587 /* Peek at the next token. */
19588 token = cp_lexer_peek_token (parser->lexer);
19589 if (token->type == CPP_OPEN_PAREN)
19591 /* This is either a parameter-declaration-clause, or a
19592 parenthesized declarator. When we know we are parsing a
19593 named declarator, it must be a parenthesized declarator
19594 if FIRST is true. For instance, `(int)' is a
19595 parameter-declaration-clause, with an omitted
19596 direct-abstract-declarator. But `((*))', is a
19597 parenthesized abstract declarator. Finally, when T is a
19598 template parameter `(T)' is a
19599 parameter-declaration-clause, and not a parenthesized
19600 named declarator.
19602 We first try and parse a parameter-declaration-clause,
19603 and then try a nested declarator (if FIRST is true).
19605 It is not an error for it not to be a
19606 parameter-declaration-clause, even when FIRST is
19607 false. Consider,
19609 int i (int);
19610 int i (3);
19612 The first is the declaration of a function while the
19613 second is the definition of a variable, including its
19614 initializer.
19616 Having seen only the parenthesis, we cannot know which of
19617 these two alternatives should be selected. Even more
19618 complex are examples like:
19620 int i (int (a));
19621 int i (int (3));
19623 The former is a function-declaration; the latter is a
19624 variable initialization.
19626 Thus again, we try a parameter-declaration-clause, and if
19627 that fails, we back out and return. */
19629 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19631 tree params;
19632 bool is_declarator = false;
19634 /* In a member-declarator, the only valid interpretation
19635 of a parenthesis is the start of a
19636 parameter-declaration-clause. (It is invalid to
19637 initialize a static data member with a parenthesized
19638 initializer; only the "=" form of initialization is
19639 permitted.) */
19640 if (!member_p)
19641 cp_parser_parse_tentatively (parser);
19643 /* Consume the `('. */
19644 cp_lexer_consume_token (parser->lexer);
19645 if (first)
19647 /* If this is going to be an abstract declarator, we're
19648 in a declarator and we can't have default args. */
19649 parser->default_arg_ok_p = false;
19650 parser->in_declarator_p = true;
19653 begin_scope (sk_function_parms, NULL_TREE);
19655 /* Parse the parameter-declaration-clause. */
19656 params = cp_parser_parameter_declaration_clause (parser);
19658 /* Consume the `)'. */
19659 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19661 /* If all went well, parse the cv-qualifier-seq,
19662 ref-qualifier and the exception-specification. */
19663 if (member_p || cp_parser_parse_definitely (parser))
19665 cp_cv_quals cv_quals;
19666 cp_virt_specifiers virt_specifiers;
19667 cp_ref_qualifier ref_qual;
19668 tree exception_specification;
19669 tree late_return;
19670 tree attrs;
19671 bool memfn = (member_p || (pushed_scope
19672 && CLASS_TYPE_P (pushed_scope)));
19674 is_declarator = true;
19676 if (ctor_dtor_or_conv_p)
19677 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19678 first = false;
19680 /* Parse the cv-qualifier-seq. */
19681 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19682 /* Parse the ref-qualifier. */
19683 ref_qual = cp_parser_ref_qualifier_opt (parser);
19684 /* Parse the tx-qualifier. */
19685 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19686 /* And the exception-specification. */
19687 exception_specification
19688 = cp_parser_exception_specification_opt (parser);
19690 attrs = cp_parser_std_attribute_spec_seq (parser);
19692 /* In here, we handle cases where attribute is used after
19693 the function declaration. For example:
19694 void func (int x) __attribute__((vector(..))); */
19695 tree gnu_attrs = NULL_TREE;
19696 if (flag_cilkplus
19697 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19699 cp_parser_parse_tentatively (parser);
19700 tree attr = cp_parser_gnu_attributes_opt (parser);
19701 if (cp_lexer_next_token_is_not (parser->lexer,
19702 CPP_SEMICOLON)
19703 && cp_lexer_next_token_is_not (parser->lexer,
19704 CPP_OPEN_BRACE))
19705 cp_parser_abort_tentative_parse (parser);
19706 else if (!cp_parser_parse_definitely (parser))
19708 else
19709 gnu_attrs = attr;
19711 tree requires_clause = NULL_TREE;
19712 late_return = (cp_parser_late_return_type_opt
19713 (parser, declarator, requires_clause,
19714 memfn ? cv_quals : -1));
19716 /* Parse the virt-specifier-seq. */
19717 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19719 /* Create the function-declarator. */
19720 declarator = make_call_declarator (declarator,
19721 params,
19722 cv_quals,
19723 virt_specifiers,
19724 ref_qual,
19725 tx_qual,
19726 exception_specification,
19727 late_return,
19728 requires_clause);
19729 declarator->std_attributes = attrs;
19730 declarator->attributes = gnu_attrs;
19731 /* Any subsequent parameter lists are to do with
19732 return type, so are not those of the declared
19733 function. */
19734 parser->default_arg_ok_p = false;
19737 /* Remove the function parms from scope. */
19738 pop_bindings_and_leave_scope ();
19740 if (is_declarator)
19741 /* Repeat the main loop. */
19742 continue;
19745 /* If this is the first, we can try a parenthesized
19746 declarator. */
19747 if (first)
19749 bool saved_in_type_id_in_expr_p;
19751 parser->default_arg_ok_p = saved_default_arg_ok_p;
19752 parser->in_declarator_p = saved_in_declarator_p;
19754 /* Consume the `('. */
19755 cp_lexer_consume_token (parser->lexer);
19756 /* Parse the nested declarator. */
19757 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19758 parser->in_type_id_in_expr_p = true;
19759 declarator
19760 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19761 /*parenthesized_p=*/NULL,
19762 member_p, friend_p);
19763 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19764 first = false;
19765 /* Expect a `)'. */
19766 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19767 declarator = cp_error_declarator;
19768 if (declarator == cp_error_declarator)
19769 break;
19771 goto handle_declarator;
19773 /* Otherwise, we must be done. */
19774 else
19775 break;
19777 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19778 && token->type == CPP_OPEN_SQUARE
19779 && !cp_next_tokens_can_be_attribute_p (parser))
19781 /* Parse an array-declarator. */
19782 tree bounds, attrs;
19784 if (ctor_dtor_or_conv_p)
19785 *ctor_dtor_or_conv_p = 0;
19787 first = false;
19788 parser->default_arg_ok_p = false;
19789 parser->in_declarator_p = true;
19790 /* Consume the `['. */
19791 cp_lexer_consume_token (parser->lexer);
19792 /* Peek at the next token. */
19793 token = cp_lexer_peek_token (parser->lexer);
19794 /* If the next token is `]', then there is no
19795 constant-expression. */
19796 if (token->type != CPP_CLOSE_SQUARE)
19798 bool non_constant_p;
19799 bounds
19800 = cp_parser_constant_expression (parser,
19801 /*allow_non_constant=*/true,
19802 &non_constant_p);
19803 if (!non_constant_p)
19804 /* OK */;
19805 else if (error_operand_p (bounds))
19806 /* Already gave an error. */;
19807 else if (!parser->in_function_body
19808 || current_binding_level->kind == sk_function_parms)
19810 /* Normally, the array bound must be an integral constant
19811 expression. However, as an extension, we allow VLAs
19812 in function scopes as long as they aren't part of a
19813 parameter declaration. */
19814 cp_parser_error (parser,
19815 "array bound is not an integer constant");
19816 bounds = error_mark_node;
19818 else if (processing_template_decl
19819 && !type_dependent_expression_p (bounds))
19821 /* Remember this wasn't a constant-expression. */
19822 bounds = build_nop (TREE_TYPE (bounds), bounds);
19823 TREE_SIDE_EFFECTS (bounds) = 1;
19826 else
19827 bounds = NULL_TREE;
19828 /* Look for the closing `]'. */
19829 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19831 declarator = cp_error_declarator;
19832 break;
19835 attrs = cp_parser_std_attribute_spec_seq (parser);
19836 declarator = make_array_declarator (declarator, bounds);
19837 declarator->std_attributes = attrs;
19839 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19842 tree qualifying_scope;
19843 tree unqualified_name;
19844 tree attrs;
19845 special_function_kind sfk;
19846 bool abstract_ok;
19847 bool pack_expansion_p = false;
19848 cp_token *declarator_id_start_token;
19850 /* Parse a declarator-id */
19851 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19852 if (abstract_ok)
19854 cp_parser_parse_tentatively (parser);
19856 /* If we see an ellipsis, we should be looking at a
19857 parameter pack. */
19858 if (token->type == CPP_ELLIPSIS)
19860 /* Consume the `...' */
19861 cp_lexer_consume_token (parser->lexer);
19863 pack_expansion_p = true;
19867 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19868 unqualified_name
19869 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19870 qualifying_scope = parser->scope;
19871 if (abstract_ok)
19873 bool okay = false;
19875 if (!unqualified_name && pack_expansion_p)
19877 /* Check whether an error occurred. */
19878 okay = !cp_parser_error_occurred (parser);
19880 /* We already consumed the ellipsis to mark a
19881 parameter pack, but we have no way to report it,
19882 so abort the tentative parse. We will be exiting
19883 immediately anyway. */
19884 cp_parser_abort_tentative_parse (parser);
19886 else
19887 okay = cp_parser_parse_definitely (parser);
19889 if (!okay)
19890 unqualified_name = error_mark_node;
19891 else if (unqualified_name
19892 && (qualifying_scope
19893 || (!identifier_p (unqualified_name))))
19895 cp_parser_error (parser, "expected unqualified-id");
19896 unqualified_name = error_mark_node;
19900 if (!unqualified_name)
19901 return NULL;
19902 if (unqualified_name == error_mark_node)
19904 declarator = cp_error_declarator;
19905 pack_expansion_p = false;
19906 declarator->parameter_pack_p = false;
19907 break;
19910 attrs = cp_parser_std_attribute_spec_seq (parser);
19912 if (qualifying_scope && at_namespace_scope_p ()
19913 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
19915 /* In the declaration of a member of a template class
19916 outside of the class itself, the SCOPE will sometimes
19917 be a TYPENAME_TYPE. For example, given:
19919 template <typename T>
19920 int S<T>::R::i = 3;
19922 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
19923 this context, we must resolve S<T>::R to an ordinary
19924 type, rather than a typename type.
19926 The reason we normally avoid resolving TYPENAME_TYPEs
19927 is that a specialization of `S' might render
19928 `S<T>::R' not a type. However, if `S' is
19929 specialized, then this `i' will not be used, so there
19930 is no harm in resolving the types here. */
19931 tree type;
19933 /* Resolve the TYPENAME_TYPE. */
19934 type = resolve_typename_type (qualifying_scope,
19935 /*only_current_p=*/false);
19936 /* If that failed, the declarator is invalid. */
19937 if (TREE_CODE (type) == TYPENAME_TYPE)
19939 if (typedef_variant_p (type))
19940 error_at (declarator_id_start_token->location,
19941 "cannot define member of dependent typedef "
19942 "%qT", type);
19943 else
19944 error_at (declarator_id_start_token->location,
19945 "%<%T::%E%> is not a type",
19946 TYPE_CONTEXT (qualifying_scope),
19947 TYPE_IDENTIFIER (qualifying_scope));
19949 qualifying_scope = type;
19952 sfk = sfk_none;
19954 if (unqualified_name)
19956 tree class_type;
19958 if (qualifying_scope
19959 && CLASS_TYPE_P (qualifying_scope))
19960 class_type = qualifying_scope;
19961 else
19962 class_type = current_class_type;
19964 if (TREE_CODE (unqualified_name) == TYPE_DECL)
19966 tree name_type = TREE_TYPE (unqualified_name);
19967 if (class_type && same_type_p (name_type, class_type))
19969 if (qualifying_scope
19970 && CLASSTYPE_USE_TEMPLATE (name_type))
19972 error_at (declarator_id_start_token->location,
19973 "invalid use of constructor as a template");
19974 inform (declarator_id_start_token->location,
19975 "use %<%T::%D%> instead of %<%T::%D%> to "
19976 "name the constructor in a qualified name",
19977 class_type,
19978 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
19979 class_type, name_type);
19980 declarator = cp_error_declarator;
19981 break;
19983 else
19984 unqualified_name = constructor_name (class_type);
19986 else
19988 /* We do not attempt to print the declarator
19989 here because we do not have enough
19990 information about its original syntactic
19991 form. */
19992 cp_parser_error (parser, "invalid declarator");
19993 declarator = cp_error_declarator;
19994 break;
19998 if (class_type)
20000 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20001 sfk = sfk_destructor;
20002 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
20003 sfk = sfk_conversion;
20004 else if (/* There's no way to declare a constructor
20005 for an unnamed type, even if the type
20006 got a name for linkage purposes. */
20007 !TYPE_WAS_UNNAMED (class_type)
20008 /* Handle correctly (c++/19200):
20010 struct S {
20011 struct T{};
20012 friend void S(T);
20015 and also:
20017 namespace N {
20018 void S();
20021 struct S {
20022 friend void N::S();
20023 }; */
20024 && !(friend_p
20025 && class_type != qualifying_scope)
20026 && constructor_name_p (unqualified_name,
20027 class_type))
20029 unqualified_name = constructor_name (class_type);
20030 sfk = sfk_constructor;
20032 else if (is_overloaded_fn (unqualified_name)
20033 && DECL_CONSTRUCTOR_P (get_first_fn
20034 (unqualified_name)))
20035 sfk = sfk_constructor;
20037 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20038 *ctor_dtor_or_conv_p = -1;
20041 declarator = make_id_declarator (qualifying_scope,
20042 unqualified_name,
20043 sfk);
20044 declarator->std_attributes = attrs;
20045 declarator->id_loc = token->location;
20046 declarator->parameter_pack_p = pack_expansion_p;
20048 if (pack_expansion_p)
20049 maybe_warn_variadic_templates ();
20052 handle_declarator:;
20053 scope = get_scope_of_declarator (declarator);
20054 if (scope)
20056 /* Any names that appear after the declarator-id for a
20057 member are looked up in the containing scope. */
20058 if (at_function_scope_p ())
20060 /* But declarations with qualified-ids can't appear in a
20061 function. */
20062 cp_parser_error (parser, "qualified-id in declaration");
20063 declarator = cp_error_declarator;
20064 break;
20066 pushed_scope = push_scope (scope);
20068 parser->in_declarator_p = true;
20069 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20070 || (declarator && declarator->kind == cdk_id))
20071 /* Default args are only allowed on function
20072 declarations. */
20073 parser->default_arg_ok_p = saved_default_arg_ok_p;
20074 else
20075 parser->default_arg_ok_p = false;
20077 first = false;
20079 /* We're done. */
20080 else
20081 break;
20084 /* For an abstract declarator, we might wind up with nothing at this
20085 point. That's an error; the declarator is not optional. */
20086 if (!declarator)
20087 cp_parser_error (parser, "expected declarator");
20089 /* If we entered a scope, we must exit it now. */
20090 if (pushed_scope)
20091 pop_scope (pushed_scope);
20093 parser->default_arg_ok_p = saved_default_arg_ok_p;
20094 parser->in_declarator_p = saved_in_declarator_p;
20096 return declarator;
20099 /* Parse a ptr-operator.
20101 ptr-operator:
20102 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20103 * cv-qualifier-seq [opt]
20105 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20106 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20108 GNU Extension:
20110 ptr-operator:
20111 & cv-qualifier-seq [opt]
20113 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20114 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20115 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20116 filled in with the TYPE containing the member. *CV_QUALS is
20117 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20118 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20119 Note that the tree codes returned by this function have nothing
20120 to do with the types of trees that will be eventually be created
20121 to represent the pointer or reference type being parsed. They are
20122 just constants with suggestive names. */
20123 static enum tree_code
20124 cp_parser_ptr_operator (cp_parser* parser,
20125 tree* type,
20126 cp_cv_quals *cv_quals,
20127 tree *attributes)
20129 enum tree_code code = ERROR_MARK;
20130 cp_token *token;
20131 tree attrs = NULL_TREE;
20133 /* Assume that it's not a pointer-to-member. */
20134 *type = NULL_TREE;
20135 /* And that there are no cv-qualifiers. */
20136 *cv_quals = TYPE_UNQUALIFIED;
20138 /* Peek at the next token. */
20139 token = cp_lexer_peek_token (parser->lexer);
20141 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20142 if (token->type == CPP_MULT)
20143 code = INDIRECT_REF;
20144 else if (token->type == CPP_AND)
20145 code = ADDR_EXPR;
20146 else if ((cxx_dialect != cxx98) &&
20147 token->type == CPP_AND_AND) /* C++0x only */
20148 code = NON_LVALUE_EXPR;
20150 if (code != ERROR_MARK)
20152 /* Consume the `*', `&' or `&&'. */
20153 cp_lexer_consume_token (parser->lexer);
20155 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20156 `&', if we are allowing GNU extensions. (The only qualifier
20157 that can legally appear after `&' is `restrict', but that is
20158 enforced during semantic analysis. */
20159 if (code == INDIRECT_REF
20160 || cp_parser_allow_gnu_extensions_p (parser))
20161 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20163 attrs = cp_parser_std_attribute_spec_seq (parser);
20164 if (attributes != NULL)
20165 *attributes = attrs;
20167 else
20169 /* Try the pointer-to-member case. */
20170 cp_parser_parse_tentatively (parser);
20171 /* Look for the optional `::' operator. */
20172 cp_parser_global_scope_opt (parser,
20173 /*current_scope_valid_p=*/false);
20174 /* Look for the nested-name specifier. */
20175 token = cp_lexer_peek_token (parser->lexer);
20176 cp_parser_nested_name_specifier (parser,
20177 /*typename_keyword_p=*/false,
20178 /*check_dependency_p=*/true,
20179 /*type_p=*/false,
20180 /*is_declaration=*/false);
20181 /* If we found it, and the next token is a `*', then we are
20182 indeed looking at a pointer-to-member operator. */
20183 if (!cp_parser_error_occurred (parser)
20184 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20186 /* Indicate that the `*' operator was used. */
20187 code = INDIRECT_REF;
20189 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20190 error_at (token->location, "%qD is a namespace", parser->scope);
20191 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20192 error_at (token->location, "cannot form pointer to member of "
20193 "non-class %q#T", parser->scope);
20194 else
20196 /* The type of which the member is a member is given by the
20197 current SCOPE. */
20198 *type = parser->scope;
20199 /* The next name will not be qualified. */
20200 parser->scope = NULL_TREE;
20201 parser->qualifying_scope = NULL_TREE;
20202 parser->object_scope = NULL_TREE;
20203 /* Look for optional c++11 attributes. */
20204 attrs = cp_parser_std_attribute_spec_seq (parser);
20205 if (attributes != NULL)
20206 *attributes = attrs;
20207 /* Look for the optional cv-qualifier-seq. */
20208 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20211 /* If that didn't work we don't have a ptr-operator. */
20212 if (!cp_parser_parse_definitely (parser))
20213 cp_parser_error (parser, "expected ptr-operator");
20216 return code;
20219 /* Parse an (optional) cv-qualifier-seq.
20221 cv-qualifier-seq:
20222 cv-qualifier cv-qualifier-seq [opt]
20224 cv-qualifier:
20225 const
20226 volatile
20228 GNU Extension:
20230 cv-qualifier:
20231 __restrict__
20233 Returns a bitmask representing the cv-qualifiers. */
20235 static cp_cv_quals
20236 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20238 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20240 while (true)
20242 cp_token *token;
20243 cp_cv_quals cv_qualifier;
20245 /* Peek at the next token. */
20246 token = cp_lexer_peek_token (parser->lexer);
20247 /* See if it's a cv-qualifier. */
20248 switch (token->keyword)
20250 case RID_CONST:
20251 cv_qualifier = TYPE_QUAL_CONST;
20252 break;
20254 case RID_VOLATILE:
20255 cv_qualifier = TYPE_QUAL_VOLATILE;
20256 break;
20258 case RID_RESTRICT:
20259 cv_qualifier = TYPE_QUAL_RESTRICT;
20260 break;
20262 default:
20263 cv_qualifier = TYPE_UNQUALIFIED;
20264 break;
20267 if (!cv_qualifier)
20268 break;
20270 if (cv_quals & cv_qualifier)
20272 error_at (token->location, "duplicate cv-qualifier");
20273 cp_lexer_purge_token (parser->lexer);
20275 else
20277 cp_lexer_consume_token (parser->lexer);
20278 cv_quals |= cv_qualifier;
20282 return cv_quals;
20285 /* Parse an (optional) ref-qualifier
20287 ref-qualifier:
20291 Returns cp_ref_qualifier representing ref-qualifier. */
20293 static cp_ref_qualifier
20294 cp_parser_ref_qualifier_opt (cp_parser* parser)
20296 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20298 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20299 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20300 return ref_qual;
20302 while (true)
20304 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20305 cp_token *token = cp_lexer_peek_token (parser->lexer);
20307 switch (token->type)
20309 case CPP_AND:
20310 curr_ref_qual = REF_QUAL_LVALUE;
20311 break;
20313 case CPP_AND_AND:
20314 curr_ref_qual = REF_QUAL_RVALUE;
20315 break;
20317 default:
20318 curr_ref_qual = REF_QUAL_NONE;
20319 break;
20322 if (!curr_ref_qual)
20323 break;
20324 else if (ref_qual)
20326 error_at (token->location, "multiple ref-qualifiers");
20327 cp_lexer_purge_token (parser->lexer);
20329 else
20331 ref_qual = curr_ref_qual;
20332 cp_lexer_consume_token (parser->lexer);
20336 return ref_qual;
20339 /* Parse an optional tx-qualifier.
20341 tx-qualifier:
20342 transaction_safe
20343 transaction_safe_dynamic */
20345 static tree
20346 cp_parser_tx_qualifier_opt (cp_parser *parser)
20348 cp_token *token = cp_lexer_peek_token (parser->lexer);
20349 if (token->type == CPP_NAME)
20351 tree name = token->u.value;
20352 const char *p = IDENTIFIER_POINTER (name);
20353 const int len = strlen ("transaction_safe");
20354 if (!strncmp (p, "transaction_safe", len))
20356 p += len;
20357 if (*p == '\0'
20358 || !strcmp (p, "_dynamic"))
20360 cp_lexer_consume_token (parser->lexer);
20361 if (!flag_tm)
20363 error ("%E requires %<-fgnu-tm%>", name);
20364 return NULL_TREE;
20366 else
20367 return name;
20371 return NULL_TREE;
20374 /* Parse an (optional) virt-specifier-seq.
20376 virt-specifier-seq:
20377 virt-specifier virt-specifier-seq [opt]
20379 virt-specifier:
20380 override
20381 final
20383 Returns a bitmask representing the virt-specifiers. */
20385 static cp_virt_specifiers
20386 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20388 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20390 while (true)
20392 cp_token *token;
20393 cp_virt_specifiers virt_specifier;
20395 /* Peek at the next token. */
20396 token = cp_lexer_peek_token (parser->lexer);
20397 /* See if it's a virt-specifier-qualifier. */
20398 if (token->type != CPP_NAME)
20399 break;
20400 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
20402 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20403 virt_specifier = VIRT_SPEC_OVERRIDE;
20405 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
20407 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20408 virt_specifier = VIRT_SPEC_FINAL;
20410 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
20412 virt_specifier = VIRT_SPEC_FINAL;
20414 else
20415 break;
20417 if (virt_specifiers & virt_specifier)
20419 error_at (token->location, "duplicate virt-specifier");
20420 cp_lexer_purge_token (parser->lexer);
20422 else
20424 cp_lexer_consume_token (parser->lexer);
20425 virt_specifiers |= virt_specifier;
20428 return virt_specifiers;
20431 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20432 is in scope even though it isn't real. */
20434 void
20435 inject_this_parameter (tree ctype, cp_cv_quals quals)
20437 tree this_parm;
20439 if (current_class_ptr)
20441 /* We don't clear this between NSDMIs. Is it already what we want? */
20442 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20443 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20444 && cp_type_quals (type) == quals)
20445 return;
20448 this_parm = build_this_parm (ctype, quals);
20449 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20450 current_class_ptr = NULL_TREE;
20451 current_class_ref
20452 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20453 current_class_ptr = this_parm;
20456 /* Return true iff our current scope is a non-static data member
20457 initializer. */
20459 bool
20460 parsing_nsdmi (void)
20462 /* We recognize NSDMI context by the context-less 'this' pointer set up
20463 by the function above. */
20464 if (current_class_ptr
20465 && TREE_CODE (current_class_ptr) == PARM_DECL
20466 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20467 return true;
20468 return false;
20471 /* Return true iff our current scope is a default capturing generic lambda
20472 defined within a template. FIXME: This is part of a workaround (see
20473 semantics.c) to handle building lambda closure types correctly in templates
20474 which we ultimately want to defer to instantiation time. */
20476 bool
20477 parsing_default_capturing_generic_lambda_in_template (void)
20479 if (!processing_template_decl || !current_class_type)
20480 return false;
20482 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20483 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20484 return false;
20486 tree callop = lambda_function (lam);
20487 if (!callop)
20488 return false;
20490 return (DECL_TEMPLATE_INFO (callop)
20491 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20492 && ((current_nonlambda_class_type ()
20493 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20494 || ((current_nonlambda_function ()
20495 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20498 /* Parse a late-specified return type, if any. This is not a separate
20499 non-terminal, but part of a function declarator, which looks like
20501 -> trailing-type-specifier-seq abstract-declarator(opt)
20503 Returns the type indicated by the type-id.
20505 In addition to this, parse any queued up #pragma omp declare simd
20506 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20507 #pragma acc routine clauses.
20509 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20510 function. */
20512 static tree
20513 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20514 tree& requires_clause, cp_cv_quals quals)
20516 cp_token *token;
20517 tree type = NULL_TREE;
20518 bool declare_simd_p = (parser->omp_declare_simd
20519 && declarator
20520 && declarator->kind == cdk_id);
20522 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20523 && declarator && declarator->kind == cdk_id);
20525 bool oacc_routine_p = (parser->oacc_routine
20526 && declarator
20527 && declarator->kind == cdk_id);
20529 /* Peek at the next token. */
20530 token = cp_lexer_peek_token (parser->lexer);
20531 /* A late-specified return type is indicated by an initial '->'. */
20532 if (token->type != CPP_DEREF
20533 && token->keyword != RID_REQUIRES
20534 && !(token->type == CPP_NAME
20535 && token->u.value == ridpointers[RID_REQUIRES])
20536 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20537 return NULL_TREE;
20539 tree save_ccp = current_class_ptr;
20540 tree save_ccr = current_class_ref;
20541 if (quals >= 0)
20543 /* DR 1207: 'this' is in scope in the trailing return type. */
20544 inject_this_parameter (current_class_type, quals);
20547 if (token->type == CPP_DEREF)
20549 /* Consume the ->. */
20550 cp_lexer_consume_token (parser->lexer);
20552 type = cp_parser_trailing_type_id (parser);
20555 /* Function declarations may be followed by a trailing
20556 requires-clause. */
20557 requires_clause = cp_parser_requires_clause_opt (parser);
20559 if (cilk_simd_fn_vector_p)
20560 declarator->attributes
20561 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20562 declarator->attributes);
20563 if (declare_simd_p)
20564 declarator->attributes
20565 = cp_parser_late_parsing_omp_declare_simd (parser,
20566 declarator->attributes);
20567 if (oacc_routine_p)
20568 declarator->attributes
20569 = cp_parser_late_parsing_oacc_routine (parser,
20570 declarator->attributes);
20572 if (quals >= 0)
20574 current_class_ptr = save_ccp;
20575 current_class_ref = save_ccr;
20578 return type;
20581 /* Parse a declarator-id.
20583 declarator-id:
20584 id-expression
20585 :: [opt] nested-name-specifier [opt] type-name
20587 In the `id-expression' case, the value returned is as for
20588 cp_parser_id_expression if the id-expression was an unqualified-id.
20589 If the id-expression was a qualified-id, then a SCOPE_REF is
20590 returned. The first operand is the scope (either a NAMESPACE_DECL
20591 or TREE_TYPE), but the second is still just a representation of an
20592 unqualified-id. */
20594 static tree
20595 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20597 tree id;
20598 /* The expression must be an id-expression. Assume that qualified
20599 names are the names of types so that:
20601 template <class T>
20602 int S<T>::R::i = 3;
20604 will work; we must treat `S<T>::R' as the name of a type.
20605 Similarly, assume that qualified names are templates, where
20606 required, so that:
20608 template <class T>
20609 int S<T>::R<T>::i = 3;
20611 will work, too. */
20612 id = cp_parser_id_expression (parser,
20613 /*template_keyword_p=*/false,
20614 /*check_dependency_p=*/false,
20615 /*template_p=*/NULL,
20616 /*declarator_p=*/true,
20617 optional_p);
20618 if (id && BASELINK_P (id))
20619 id = BASELINK_FUNCTIONS (id);
20620 return id;
20623 /* Parse a type-id.
20625 type-id:
20626 type-specifier-seq abstract-declarator [opt]
20628 Returns the TYPE specified. */
20630 static tree
20631 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20632 bool is_trailing_return)
20634 cp_decl_specifier_seq type_specifier_seq;
20635 cp_declarator *abstract_declarator;
20637 /* Parse the type-specifier-seq. */
20638 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20639 is_trailing_return,
20640 &type_specifier_seq);
20641 if (is_template_arg && type_specifier_seq.type
20642 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20643 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20644 /* A bare template name as a template argument is a template template
20645 argument, not a placeholder, so fail parsing it as a type argument. */
20647 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20648 cp_parser_simulate_error (parser);
20649 return error_mark_node;
20651 if (type_specifier_seq.type == error_mark_node)
20652 return error_mark_node;
20654 /* There might or might not be an abstract declarator. */
20655 cp_parser_parse_tentatively (parser);
20656 /* Look for the declarator. */
20657 abstract_declarator
20658 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20659 /*parenthesized_p=*/NULL,
20660 /*member_p=*/false,
20661 /*friend_p=*/false);
20662 /* Check to see if there really was a declarator. */
20663 if (!cp_parser_parse_definitely (parser))
20664 abstract_declarator = NULL;
20666 if (type_specifier_seq.type
20667 /* The concepts TS allows 'auto' as a type-id. */
20668 && (!flag_concepts || parser->in_type_id_in_expr_p)
20669 /* None of the valid uses of 'auto' in C++14 involve the type-id
20670 nonterminal, but it is valid in a trailing-return-type. */
20671 && !(cxx_dialect >= cxx14 && is_trailing_return))
20672 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20674 /* A type-id with type 'auto' is only ok if the abstract declarator
20675 is a function declarator with a late-specified return type.
20677 A type-id with 'auto' is also valid in a trailing-return-type
20678 in a compound-requirement. */
20679 if (abstract_declarator
20680 && abstract_declarator->kind == cdk_function
20681 && abstract_declarator->u.function.late_return_type)
20682 /* OK */;
20683 else if (parser->in_result_type_constraint_p)
20684 /* OK */;
20685 else
20687 location_t loc = type_specifier_seq.locations[ds_type_spec];
20688 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20690 error_at (loc, "missing template arguments after %qT",
20691 auto_node);
20692 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20693 tmpl);
20695 else
20696 error_at (loc, "invalid use of %qT", auto_node);
20697 return error_mark_node;
20701 return groktypename (&type_specifier_seq, abstract_declarator,
20702 is_template_arg);
20705 static tree
20706 cp_parser_type_id (cp_parser *parser)
20708 return cp_parser_type_id_1 (parser, false, false);
20711 static tree
20712 cp_parser_template_type_arg (cp_parser *parser)
20714 tree r;
20715 const char *saved_message = parser->type_definition_forbidden_message;
20716 parser->type_definition_forbidden_message
20717 = G_("types may not be defined in template arguments");
20718 r = cp_parser_type_id_1 (parser, true, false);
20719 parser->type_definition_forbidden_message = saved_message;
20720 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20722 error ("invalid use of %<auto%> in template argument");
20723 r = error_mark_node;
20725 return r;
20728 static tree
20729 cp_parser_trailing_type_id (cp_parser *parser)
20731 return cp_parser_type_id_1 (parser, false, true);
20734 /* Parse a type-specifier-seq.
20736 type-specifier-seq:
20737 type-specifier type-specifier-seq [opt]
20739 GNU extension:
20741 type-specifier-seq:
20742 attributes type-specifier-seq [opt]
20744 If IS_DECLARATION is true, we are at the start of a "condition" or
20745 exception-declaration, so we might be followed by a declarator-id.
20747 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20748 i.e. we've just seen "->".
20750 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20752 static void
20753 cp_parser_type_specifier_seq (cp_parser* parser,
20754 bool is_declaration,
20755 bool is_trailing_return,
20756 cp_decl_specifier_seq *type_specifier_seq)
20758 bool seen_type_specifier = false;
20759 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20760 cp_token *start_token = NULL;
20762 /* Clear the TYPE_SPECIFIER_SEQ. */
20763 clear_decl_specs (type_specifier_seq);
20765 /* In the context of a trailing return type, enum E { } is an
20766 elaborated-type-specifier followed by a function-body, not an
20767 enum-specifier. */
20768 if (is_trailing_return)
20769 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20771 /* Parse the type-specifiers and attributes. */
20772 while (true)
20774 tree type_specifier;
20775 bool is_cv_qualifier;
20777 /* Check for attributes first. */
20778 if (cp_next_tokens_can_be_attribute_p (parser))
20780 type_specifier_seq->attributes =
20781 chainon (type_specifier_seq->attributes,
20782 cp_parser_attributes_opt (parser));
20783 continue;
20786 /* record the token of the beginning of the type specifier seq,
20787 for error reporting purposes*/
20788 if (!start_token)
20789 start_token = cp_lexer_peek_token (parser->lexer);
20791 /* Look for the type-specifier. */
20792 type_specifier = cp_parser_type_specifier (parser,
20793 flags,
20794 type_specifier_seq,
20795 /*is_declaration=*/false,
20796 NULL,
20797 &is_cv_qualifier);
20798 if (!type_specifier)
20800 /* If the first type-specifier could not be found, this is not a
20801 type-specifier-seq at all. */
20802 if (!seen_type_specifier)
20804 /* Set in_declarator_p to avoid skipping to the semicolon. */
20805 int in_decl = parser->in_declarator_p;
20806 parser->in_declarator_p = true;
20808 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20809 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20810 cp_parser_error (parser, "expected type-specifier");
20812 parser->in_declarator_p = in_decl;
20814 type_specifier_seq->type = error_mark_node;
20815 return;
20817 /* If subsequent type-specifiers could not be found, the
20818 type-specifier-seq is complete. */
20819 break;
20822 seen_type_specifier = true;
20823 /* The standard says that a condition can be:
20825 type-specifier-seq declarator = assignment-expression
20827 However, given:
20829 struct S {};
20830 if (int S = ...)
20832 we should treat the "S" as a declarator, not as a
20833 type-specifier. The standard doesn't say that explicitly for
20834 type-specifier-seq, but it does say that for
20835 decl-specifier-seq in an ordinary declaration. Perhaps it
20836 would be clearer just to allow a decl-specifier-seq here, and
20837 then add a semantic restriction that if any decl-specifiers
20838 that are not type-specifiers appear, the program is invalid. */
20839 if (is_declaration && !is_cv_qualifier)
20840 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20844 /* Return whether the function currently being declared has an associated
20845 template parameter list. */
20847 static bool
20848 function_being_declared_is_template_p (cp_parser* parser)
20850 if (!current_template_parms || processing_template_parmlist)
20851 return false;
20853 if (parser->implicit_template_scope)
20854 return true;
20856 if (at_class_scope_p ()
20857 && TYPE_BEING_DEFINED (current_class_type))
20858 return parser->num_template_parameter_lists != 0;
20860 return ((int) parser->num_template_parameter_lists > template_class_depth
20861 (current_class_type));
20864 /* Parse a parameter-declaration-clause.
20866 parameter-declaration-clause:
20867 parameter-declaration-list [opt] ... [opt]
20868 parameter-declaration-list , ...
20870 Returns a representation for the parameter declarations. A return
20871 value of NULL indicates a parameter-declaration-clause consisting
20872 only of an ellipsis. */
20874 static tree
20875 cp_parser_parameter_declaration_clause (cp_parser* parser)
20877 tree parameters;
20878 cp_token *token;
20879 bool ellipsis_p;
20880 bool is_error;
20882 struct cleanup {
20883 cp_parser* parser;
20884 int auto_is_implicit_function_template_parm_p;
20885 ~cleanup() {
20886 parser->auto_is_implicit_function_template_parm_p
20887 = auto_is_implicit_function_template_parm_p;
20889 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
20891 (void) cleanup;
20893 if (!processing_specialization
20894 && !processing_template_parmlist
20895 && !processing_explicit_instantiation)
20896 if (!current_function_decl
20897 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
20898 parser->auto_is_implicit_function_template_parm_p = true;
20900 /* Peek at the next token. */
20901 token = cp_lexer_peek_token (parser->lexer);
20902 /* Check for trivial parameter-declaration-clauses. */
20903 if (token->type == CPP_ELLIPSIS)
20905 /* Consume the `...' token. */
20906 cp_lexer_consume_token (parser->lexer);
20907 return NULL_TREE;
20909 else if (token->type == CPP_CLOSE_PAREN)
20910 /* There are no parameters. */
20912 #ifndef NO_IMPLICIT_EXTERN_C
20913 if (in_system_header_at (input_location)
20914 && current_class_type == NULL
20915 && current_lang_name == lang_name_c)
20916 return NULL_TREE;
20917 else
20918 #endif
20919 return void_list_node;
20921 /* Check for `(void)', too, which is a special case. */
20922 else if (token->keyword == RID_VOID
20923 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20924 == CPP_CLOSE_PAREN))
20926 /* Consume the `void' token. */
20927 cp_lexer_consume_token (parser->lexer);
20928 /* There are no parameters. */
20929 return void_list_node;
20932 /* Parse the parameter-declaration-list. */
20933 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
20934 /* If a parse error occurred while parsing the
20935 parameter-declaration-list, then the entire
20936 parameter-declaration-clause is erroneous. */
20937 if (is_error)
20938 return NULL;
20940 /* Peek at the next token. */
20941 token = cp_lexer_peek_token (parser->lexer);
20942 /* If it's a `,', the clause should terminate with an ellipsis. */
20943 if (token->type == CPP_COMMA)
20945 /* Consume the `,'. */
20946 cp_lexer_consume_token (parser->lexer);
20947 /* Expect an ellipsis. */
20948 ellipsis_p
20949 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
20951 /* It might also be `...' if the optional trailing `,' was
20952 omitted. */
20953 else if (token->type == CPP_ELLIPSIS)
20955 /* Consume the `...' token. */
20956 cp_lexer_consume_token (parser->lexer);
20957 /* And remember that we saw it. */
20958 ellipsis_p = true;
20960 else
20961 ellipsis_p = false;
20963 /* Finish the parameter list. */
20964 if (!ellipsis_p)
20965 parameters = chainon (parameters, void_list_node);
20967 return parameters;
20970 /* Parse a parameter-declaration-list.
20972 parameter-declaration-list:
20973 parameter-declaration
20974 parameter-declaration-list , parameter-declaration
20976 Returns a representation of the parameter-declaration-list, as for
20977 cp_parser_parameter_declaration_clause. However, the
20978 `void_list_node' is never appended to the list. Upon return,
20979 *IS_ERROR will be true iff an error occurred. */
20981 static tree
20982 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
20984 tree parameters = NULL_TREE;
20985 tree *tail = &parameters;
20986 bool saved_in_unbraced_linkage_specification_p;
20987 int index = 0;
20989 /* Assume all will go well. */
20990 *is_error = false;
20991 /* The special considerations that apply to a function within an
20992 unbraced linkage specifications do not apply to the parameters
20993 to the function. */
20994 saved_in_unbraced_linkage_specification_p
20995 = parser->in_unbraced_linkage_specification_p;
20996 parser->in_unbraced_linkage_specification_p = false;
20998 /* Look for more parameters. */
20999 while (true)
21001 cp_parameter_declarator *parameter;
21002 tree decl = error_mark_node;
21003 bool parenthesized_p = false;
21004 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21005 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21006 (current_template_parms)) : 0);
21008 /* Parse the parameter. */
21009 parameter
21010 = cp_parser_parameter_declaration (parser,
21011 /*template_parm_p=*/false,
21012 &parenthesized_p);
21014 /* We don't know yet if the enclosing context is deprecated, so wait
21015 and warn in grokparms if appropriate. */
21016 deprecated_state = DEPRECATED_SUPPRESS;
21018 if (parameter)
21020 /* If a function parameter pack was specified and an implicit template
21021 parameter was introduced during cp_parser_parameter_declaration,
21022 change any implicit parameters introduced into packs. */
21023 if (parser->implicit_template_parms
21024 && parameter->declarator
21025 && parameter->declarator->parameter_pack_p)
21027 int latest_template_parm_idx = TREE_VEC_LENGTH
21028 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21030 if (latest_template_parm_idx != template_parm_idx)
21031 parameter->decl_specifiers.type = convert_generic_types_to_packs
21032 (parameter->decl_specifiers.type,
21033 template_parm_idx, latest_template_parm_idx);
21036 decl = grokdeclarator (parameter->declarator,
21037 &parameter->decl_specifiers,
21038 PARM,
21039 parameter->default_argument != NULL_TREE,
21040 &parameter->decl_specifiers.attributes);
21043 deprecated_state = DEPRECATED_NORMAL;
21045 /* If a parse error occurred parsing the parameter declaration,
21046 then the entire parameter-declaration-list is erroneous. */
21047 if (decl == error_mark_node)
21049 *is_error = true;
21050 parameters = error_mark_node;
21051 break;
21054 if (parameter->decl_specifiers.attributes)
21055 cplus_decl_attributes (&decl,
21056 parameter->decl_specifiers.attributes,
21058 if (DECL_NAME (decl))
21059 decl = pushdecl (decl);
21061 if (decl != error_mark_node)
21063 retrofit_lang_decl (decl);
21064 DECL_PARM_INDEX (decl) = ++index;
21065 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21068 /* Add the new parameter to the list. */
21069 *tail = build_tree_list (parameter->default_argument, decl);
21070 tail = &TREE_CHAIN (*tail);
21072 /* Peek at the next token. */
21073 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21074 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21075 /* These are for Objective-C++ */
21076 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21077 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21078 /* The parameter-declaration-list is complete. */
21079 break;
21080 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21082 cp_token *token;
21084 /* Peek at the next token. */
21085 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21086 /* If it's an ellipsis, then the list is complete. */
21087 if (token->type == CPP_ELLIPSIS)
21088 break;
21089 /* Otherwise, there must be more parameters. Consume the
21090 `,'. */
21091 cp_lexer_consume_token (parser->lexer);
21092 /* When parsing something like:
21094 int i(float f, double d)
21096 we can tell after seeing the declaration for "f" that we
21097 are not looking at an initialization of a variable "i",
21098 but rather at the declaration of a function "i".
21100 Due to the fact that the parsing of template arguments
21101 (as specified to a template-id) requires backtracking we
21102 cannot use this technique when inside a template argument
21103 list. */
21104 if (!parser->in_template_argument_list_p
21105 && !parser->in_type_id_in_expr_p
21106 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21107 /* However, a parameter-declaration of the form
21108 "float(f)" (which is a valid declaration of a
21109 parameter "f") can also be interpreted as an
21110 expression (the conversion of "f" to "float"). */
21111 && !parenthesized_p)
21112 cp_parser_commit_to_tentative_parse (parser);
21114 else
21116 cp_parser_error (parser, "expected %<,%> or %<...%>");
21117 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21118 cp_parser_skip_to_closing_parenthesis (parser,
21119 /*recovering=*/true,
21120 /*or_comma=*/false,
21121 /*consume_paren=*/false);
21122 break;
21126 parser->in_unbraced_linkage_specification_p
21127 = saved_in_unbraced_linkage_specification_p;
21129 /* Reset implicit_template_scope if we are about to leave the function
21130 parameter list that introduced it. Note that for out-of-line member
21131 definitions, there will be one or more class scopes before we get to
21132 the template parameter scope. */
21134 if (cp_binding_level *its = parser->implicit_template_scope)
21135 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21137 while (maybe_its->kind == sk_class)
21138 maybe_its = maybe_its->level_chain;
21139 if (maybe_its == its)
21141 parser->implicit_template_parms = 0;
21142 parser->implicit_template_scope = 0;
21146 return parameters;
21149 /* Parse a parameter declaration.
21151 parameter-declaration:
21152 decl-specifier-seq ... [opt] declarator
21153 decl-specifier-seq declarator = assignment-expression
21154 decl-specifier-seq ... [opt] abstract-declarator [opt]
21155 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21157 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21158 declares a template parameter. (In that case, a non-nested `>'
21159 token encountered during the parsing of the assignment-expression
21160 is not interpreted as a greater-than operator.)
21162 Returns a representation of the parameter, or NULL if an error
21163 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21164 true iff the declarator is of the form "(p)". */
21166 static cp_parameter_declarator *
21167 cp_parser_parameter_declaration (cp_parser *parser,
21168 bool template_parm_p,
21169 bool *parenthesized_p)
21171 int declares_class_or_enum;
21172 cp_decl_specifier_seq decl_specifiers;
21173 cp_declarator *declarator;
21174 tree default_argument;
21175 cp_token *token = NULL, *declarator_token_start = NULL;
21176 const char *saved_message;
21177 bool template_parameter_pack_p = false;
21179 /* In a template parameter, `>' is not an operator.
21181 [temp.param]
21183 When parsing a default template-argument for a non-type
21184 template-parameter, the first non-nested `>' is taken as the end
21185 of the template parameter-list rather than a greater-than
21186 operator. */
21188 /* Type definitions may not appear in parameter types. */
21189 saved_message = parser->type_definition_forbidden_message;
21190 parser->type_definition_forbidden_message
21191 = G_("types may not be defined in parameter types");
21193 /* Parse the declaration-specifiers. */
21194 cp_parser_decl_specifier_seq (parser,
21195 CP_PARSER_FLAGS_NONE,
21196 &decl_specifiers,
21197 &declares_class_or_enum);
21199 /* Complain about missing 'typename' or other invalid type names. */
21200 if (!decl_specifiers.any_type_specifiers_p
21201 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21202 decl_specifiers.type = error_mark_node;
21204 /* If an error occurred, there's no reason to attempt to parse the
21205 rest of the declaration. */
21206 if (cp_parser_error_occurred (parser))
21208 parser->type_definition_forbidden_message = saved_message;
21209 return NULL;
21212 /* Peek at the next token. */
21213 token = cp_lexer_peek_token (parser->lexer);
21215 /* If the next token is a `)', `,', `=', `>', or `...', then there
21216 is no declarator. However, when variadic templates are enabled,
21217 there may be a declarator following `...'. */
21218 if (token->type == CPP_CLOSE_PAREN
21219 || token->type == CPP_COMMA
21220 || token->type == CPP_EQ
21221 || token->type == CPP_GREATER)
21223 declarator = NULL;
21224 if (parenthesized_p)
21225 *parenthesized_p = false;
21227 /* Otherwise, there should be a declarator. */
21228 else
21230 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21231 parser->default_arg_ok_p = false;
21233 /* After seeing a decl-specifier-seq, if the next token is not a
21234 "(", there is no possibility that the code is a valid
21235 expression. Therefore, if parsing tentatively, we commit at
21236 this point. */
21237 if (!parser->in_template_argument_list_p
21238 /* In an expression context, having seen:
21240 (int((char ...
21242 we cannot be sure whether we are looking at a
21243 function-type (taking a "char" as a parameter) or a cast
21244 of some object of type "char" to "int". */
21245 && !parser->in_type_id_in_expr_p
21246 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21247 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21248 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21249 cp_parser_commit_to_tentative_parse (parser);
21250 /* Parse the declarator. */
21251 declarator_token_start = token;
21252 declarator = cp_parser_declarator (parser,
21253 CP_PARSER_DECLARATOR_EITHER,
21254 /*ctor_dtor_or_conv_p=*/NULL,
21255 parenthesized_p,
21256 /*member_p=*/false,
21257 /*friend_p=*/false);
21258 parser->default_arg_ok_p = saved_default_arg_ok_p;
21259 /* After the declarator, allow more attributes. */
21260 decl_specifiers.attributes
21261 = chainon (decl_specifiers.attributes,
21262 cp_parser_attributes_opt (parser));
21264 /* If the declarator is a template parameter pack, remember that and
21265 clear the flag in the declarator itself so we don't get errors
21266 from grokdeclarator. */
21267 if (template_parm_p && declarator && declarator->parameter_pack_p)
21269 declarator->parameter_pack_p = false;
21270 template_parameter_pack_p = true;
21274 /* If the next token is an ellipsis, and we have not seen a declarator
21275 name, and if either the type of the declarator contains parameter
21276 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21277 for, eg, abbreviated integral type names), then we actually have a
21278 parameter pack expansion expression. Otherwise, leave the ellipsis
21279 for a C-style variadic function. */
21280 token = cp_lexer_peek_token (parser->lexer);
21281 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21283 tree type = decl_specifiers.type;
21285 if (type && DECL_P (type))
21286 type = TREE_TYPE (type);
21288 if (((type
21289 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21290 && (template_parm_p || uses_parameter_packs (type)))
21291 || (!type && template_parm_p))
21292 && declarator_can_be_parameter_pack (declarator))
21294 /* Consume the `...'. */
21295 cp_lexer_consume_token (parser->lexer);
21296 maybe_warn_variadic_templates ();
21298 /* Build a pack expansion type */
21299 if (template_parm_p)
21300 template_parameter_pack_p = true;
21301 else if (declarator)
21302 declarator->parameter_pack_p = true;
21303 else
21304 decl_specifiers.type = make_pack_expansion (type);
21308 /* The restriction on defining new types applies only to the type
21309 of the parameter, not to the default argument. */
21310 parser->type_definition_forbidden_message = saved_message;
21312 /* If the next token is `=', then process a default argument. */
21313 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21315 tree type = decl_specifiers.type;
21316 token = cp_lexer_peek_token (parser->lexer);
21317 /* If we are defining a class, then the tokens that make up the
21318 default argument must be saved and processed later. */
21319 if (!template_parm_p && at_class_scope_p ()
21320 && TYPE_BEING_DEFINED (current_class_type)
21321 && !LAMBDA_TYPE_P (current_class_type))
21322 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21324 // A constrained-type-specifier may declare a type template-parameter.
21325 else if (declares_constrained_type_template_parameter (type))
21326 default_argument
21327 = cp_parser_default_type_template_argument (parser);
21329 // A constrained-type-specifier may declare a template-template-parameter.
21330 else if (declares_constrained_template_template_parameter (type))
21331 default_argument
21332 = cp_parser_default_template_template_argument (parser);
21334 /* Outside of a class definition, we can just parse the
21335 assignment-expression. */
21336 else
21337 default_argument
21338 = cp_parser_default_argument (parser, template_parm_p);
21340 if (!parser->default_arg_ok_p)
21342 permerror (token->location,
21343 "default arguments are only "
21344 "permitted for function parameters");
21346 else if ((declarator && declarator->parameter_pack_p)
21347 || template_parameter_pack_p
21348 || (decl_specifiers.type
21349 && PACK_EXPANSION_P (decl_specifiers.type)))
21351 /* Find the name of the parameter pack. */
21352 cp_declarator *id_declarator = declarator;
21353 while (id_declarator && id_declarator->kind != cdk_id)
21354 id_declarator = id_declarator->declarator;
21356 if (id_declarator && id_declarator->kind == cdk_id)
21357 error_at (declarator_token_start->location,
21358 template_parm_p
21359 ? G_("template parameter pack %qD "
21360 "cannot have a default argument")
21361 : G_("parameter pack %qD cannot have "
21362 "a default argument"),
21363 id_declarator->u.id.unqualified_name);
21364 else
21365 error_at (declarator_token_start->location,
21366 template_parm_p
21367 ? G_("template parameter pack cannot have "
21368 "a default argument")
21369 : G_("parameter pack cannot have a "
21370 "default argument"));
21372 default_argument = NULL_TREE;
21375 else
21376 default_argument = NULL_TREE;
21378 return make_parameter_declarator (&decl_specifiers,
21379 declarator,
21380 default_argument,
21381 template_parameter_pack_p);
21384 /* Parse a default argument and return it.
21386 TEMPLATE_PARM_P is true if this is a default argument for a
21387 non-type template parameter. */
21388 static tree
21389 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21391 tree default_argument = NULL_TREE;
21392 bool saved_greater_than_is_operator_p;
21393 bool saved_local_variables_forbidden_p;
21394 bool non_constant_p, is_direct_init;
21396 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21397 set correctly. */
21398 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21399 parser->greater_than_is_operator_p = !template_parm_p;
21400 /* Local variable names (and the `this' keyword) may not
21401 appear in a default argument. */
21402 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21403 parser->local_variables_forbidden_p = true;
21404 /* Parse the assignment-expression. */
21405 if (template_parm_p)
21406 push_deferring_access_checks (dk_no_deferred);
21407 tree saved_class_ptr = NULL_TREE;
21408 tree saved_class_ref = NULL_TREE;
21409 /* The "this" pointer is not valid in a default argument. */
21410 if (cfun)
21412 saved_class_ptr = current_class_ptr;
21413 cp_function_chain->x_current_class_ptr = NULL_TREE;
21414 saved_class_ref = current_class_ref;
21415 cp_function_chain->x_current_class_ref = NULL_TREE;
21417 default_argument
21418 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21419 /* Restore the "this" pointer. */
21420 if (cfun)
21422 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21423 cp_function_chain->x_current_class_ref = saved_class_ref;
21425 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21426 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21427 if (template_parm_p)
21428 pop_deferring_access_checks ();
21429 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21430 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21432 return default_argument;
21435 /* Parse a function-body.
21437 function-body:
21438 compound_statement */
21440 static void
21441 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21443 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21444 ? BCS_TRY_BLOCK : BCS_NORMAL),
21445 true);
21448 /* Parse a ctor-initializer-opt followed by a function-body. Return
21449 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21450 is true we are parsing a function-try-block. */
21452 static bool
21453 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21454 bool in_function_try_block)
21456 tree body, list;
21457 bool ctor_initializer_p;
21458 const bool check_body_p =
21459 DECL_CONSTRUCTOR_P (current_function_decl)
21460 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21461 tree last = NULL;
21463 /* Begin the function body. */
21464 body = begin_function_body ();
21465 /* Parse the optional ctor-initializer. */
21466 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21468 /* If we're parsing a constexpr constructor definition, we need
21469 to check that the constructor body is indeed empty. However,
21470 before we get to cp_parser_function_body lot of junk has been
21471 generated, so we can't just check that we have an empty block.
21472 Rather we take a snapshot of the outermost block, and check whether
21473 cp_parser_function_body changed its state. */
21474 if (check_body_p)
21476 list = cur_stmt_list;
21477 if (STATEMENT_LIST_TAIL (list))
21478 last = STATEMENT_LIST_TAIL (list)->stmt;
21480 /* Parse the function-body. */
21481 cp_parser_function_body (parser, in_function_try_block);
21482 if (check_body_p)
21483 check_constexpr_ctor_body (last, list, /*complain=*/true);
21484 /* Finish the function body. */
21485 finish_function_body (body);
21487 return ctor_initializer_p;
21490 /* Parse an initializer.
21492 initializer:
21493 = initializer-clause
21494 ( expression-list )
21496 Returns an expression representing the initializer. If no
21497 initializer is present, NULL_TREE is returned.
21499 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21500 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21501 set to TRUE if there is no initializer present. If there is an
21502 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21503 is set to true; otherwise it is set to false. */
21505 static tree
21506 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21507 bool* non_constant_p)
21509 cp_token *token;
21510 tree init;
21512 /* Peek at the next token. */
21513 token = cp_lexer_peek_token (parser->lexer);
21515 /* Let our caller know whether or not this initializer was
21516 parenthesized. */
21517 *is_direct_init = (token->type != CPP_EQ);
21518 /* Assume that the initializer is constant. */
21519 *non_constant_p = false;
21521 if (token->type == CPP_EQ)
21523 /* Consume the `='. */
21524 cp_lexer_consume_token (parser->lexer);
21525 /* Parse the initializer-clause. */
21526 init = cp_parser_initializer_clause (parser, non_constant_p);
21528 else if (token->type == CPP_OPEN_PAREN)
21530 vec<tree, va_gc> *vec;
21531 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21532 /*cast_p=*/false,
21533 /*allow_expansion_p=*/true,
21534 non_constant_p);
21535 if (vec == NULL)
21536 return error_mark_node;
21537 init = build_tree_list_vec (vec);
21538 release_tree_vector (vec);
21540 else if (token->type == CPP_OPEN_BRACE)
21542 cp_lexer_set_source_position (parser->lexer);
21543 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21544 init = cp_parser_braced_list (parser, non_constant_p);
21545 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21547 else
21549 /* Anything else is an error. */
21550 cp_parser_error (parser, "expected initializer");
21551 init = error_mark_node;
21554 if (check_for_bare_parameter_packs (init))
21555 init = error_mark_node;
21557 return init;
21560 /* Parse an initializer-clause.
21562 initializer-clause:
21563 assignment-expression
21564 braced-init-list
21566 Returns an expression representing the initializer.
21568 If the `assignment-expression' production is used the value
21569 returned is simply a representation for the expression.
21571 Otherwise, calls cp_parser_braced_list. */
21573 static cp_expr
21574 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21576 cp_expr initializer;
21578 /* Assume the expression is constant. */
21579 *non_constant_p = false;
21581 /* If it is not a `{', then we are looking at an
21582 assignment-expression. */
21583 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21585 initializer
21586 = cp_parser_constant_expression (parser,
21587 /*allow_non_constant_p=*/true,
21588 non_constant_p);
21590 else
21591 initializer = cp_parser_braced_list (parser, non_constant_p);
21593 return initializer;
21596 /* Parse a brace-enclosed initializer list.
21598 braced-init-list:
21599 { initializer-list , [opt] }
21602 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21603 the elements of the initializer-list (or NULL, if the last
21604 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21605 NULL_TREE. There is no way to detect whether or not the optional
21606 trailing `,' was provided. NON_CONSTANT_P is as for
21607 cp_parser_initializer. */
21609 static cp_expr
21610 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21612 tree initializer;
21613 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21615 /* Consume the `{' token. */
21616 cp_lexer_consume_token (parser->lexer);
21617 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21618 initializer = make_node (CONSTRUCTOR);
21619 /* If it's not a `}', then there is a non-trivial initializer. */
21620 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21622 /* Parse the initializer list. */
21623 CONSTRUCTOR_ELTS (initializer)
21624 = cp_parser_initializer_list (parser, non_constant_p);
21625 /* A trailing `,' token is allowed. */
21626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21627 cp_lexer_consume_token (parser->lexer);
21629 else
21630 *non_constant_p = false;
21631 /* Now, there should be a trailing `}'. */
21632 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21633 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21634 TREE_TYPE (initializer) = init_list_type_node;
21636 cp_expr result (initializer);
21637 /* Build a location of the form:
21638 { ... }
21639 ^~~~~~~
21640 with caret==start at the open brace, finish at the close brace. */
21641 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21642 result.set_location (combined_loc);
21643 return result;
21646 /* Consume tokens up to, and including, the next non-nested closing `]'.
21647 Returns true iff we found a closing `]'. */
21649 static bool
21650 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21652 unsigned square_depth = 0;
21654 while (true)
21656 cp_token * token = cp_lexer_peek_token (parser->lexer);
21658 switch (token->type)
21660 case CPP_EOF:
21661 case CPP_PRAGMA_EOL:
21662 /* If we've run out of tokens, then there is no closing `]'. */
21663 return false;
21665 case CPP_OPEN_SQUARE:
21666 ++square_depth;
21667 break;
21669 case CPP_CLOSE_SQUARE:
21670 if (!square_depth--)
21672 cp_lexer_consume_token (parser->lexer);
21673 return true;
21675 break;
21677 default:
21678 break;
21681 /* Consume the token. */
21682 cp_lexer_consume_token (parser->lexer);
21686 /* Return true if we are looking at an array-designator, false otherwise. */
21688 static bool
21689 cp_parser_array_designator_p (cp_parser *parser)
21691 /* Consume the `['. */
21692 cp_lexer_consume_token (parser->lexer);
21694 cp_lexer_save_tokens (parser->lexer);
21696 /* Skip tokens until the next token is a closing square bracket.
21697 If we find the closing `]', and the next token is a `=', then
21698 we are looking at an array designator. */
21699 bool array_designator_p
21700 = (cp_parser_skip_to_closing_square_bracket (parser)
21701 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21703 /* Roll back the tokens we skipped. */
21704 cp_lexer_rollback_tokens (parser->lexer);
21706 return array_designator_p;
21709 /* Parse an initializer-list.
21711 initializer-list:
21712 initializer-clause ... [opt]
21713 initializer-list , initializer-clause ... [opt]
21715 GNU Extension:
21717 initializer-list:
21718 designation initializer-clause ...[opt]
21719 initializer-list , designation initializer-clause ...[opt]
21721 designation:
21722 . identifier =
21723 identifier :
21724 [ constant-expression ] =
21726 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21727 for the initializer. If the INDEX of the elt is non-NULL, it is the
21728 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21729 as for cp_parser_initializer. */
21731 static vec<constructor_elt, va_gc> *
21732 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21734 vec<constructor_elt, va_gc> *v = NULL;
21736 /* Assume all of the expressions are constant. */
21737 *non_constant_p = false;
21739 /* Parse the rest of the list. */
21740 while (true)
21742 cp_token *token;
21743 tree designator;
21744 tree initializer;
21745 bool clause_non_constant_p;
21747 /* If the next token is an identifier and the following one is a
21748 colon, we are looking at the GNU designated-initializer
21749 syntax. */
21750 if (cp_parser_allow_gnu_extensions_p (parser)
21751 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21752 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21754 /* Warn the user that they are using an extension. */
21755 pedwarn (input_location, OPT_Wpedantic,
21756 "ISO C++ does not allow designated initializers");
21757 /* Consume the identifier. */
21758 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21759 /* Consume the `:'. */
21760 cp_lexer_consume_token (parser->lexer);
21762 /* Also handle the C99 syntax, '. id ='. */
21763 else if (cp_parser_allow_gnu_extensions_p (parser)
21764 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21765 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21766 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21768 /* Warn the user that they are using an extension. */
21769 pedwarn (input_location, OPT_Wpedantic,
21770 "ISO C++ does not allow C99 designated initializers");
21771 /* Consume the `.'. */
21772 cp_lexer_consume_token (parser->lexer);
21773 /* Consume the identifier. */
21774 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21775 /* Consume the `='. */
21776 cp_lexer_consume_token (parser->lexer);
21778 /* Also handle C99 array designators, '[ const ] ='. */
21779 else if (cp_parser_allow_gnu_extensions_p (parser)
21780 && !c_dialect_objc ()
21781 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21783 /* In C++11, [ could start a lambda-introducer. */
21784 bool non_const = false;
21786 cp_parser_parse_tentatively (parser);
21788 if (!cp_parser_array_designator_p (parser))
21790 cp_parser_simulate_error (parser);
21791 designator = NULL_TREE;
21793 else
21795 designator = cp_parser_constant_expression (parser, true,
21796 &non_const);
21797 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21798 cp_parser_require (parser, CPP_EQ, RT_EQ);
21801 if (!cp_parser_parse_definitely (parser))
21802 designator = NULL_TREE;
21803 else if (non_const)
21804 require_potential_rvalue_constant_expression (designator);
21806 else
21807 designator = NULL_TREE;
21809 /* Parse the initializer. */
21810 initializer = cp_parser_initializer_clause (parser,
21811 &clause_non_constant_p);
21812 /* If any clause is non-constant, so is the entire initializer. */
21813 if (clause_non_constant_p)
21814 *non_constant_p = true;
21816 /* If we have an ellipsis, this is an initializer pack
21817 expansion. */
21818 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21820 /* Consume the `...'. */
21821 cp_lexer_consume_token (parser->lexer);
21823 /* Turn the initializer into an initializer expansion. */
21824 initializer = make_pack_expansion (initializer);
21827 /* Add it to the vector. */
21828 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21830 /* If the next token is not a comma, we have reached the end of
21831 the list. */
21832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21833 break;
21835 /* Peek at the next token. */
21836 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21837 /* If the next token is a `}', then we're still done. An
21838 initializer-clause can have a trailing `,' after the
21839 initializer-list and before the closing `}'. */
21840 if (token->type == CPP_CLOSE_BRACE)
21841 break;
21843 /* Consume the `,' token. */
21844 cp_lexer_consume_token (parser->lexer);
21847 return v;
21850 /* Classes [gram.class] */
21852 /* Parse a class-name.
21854 class-name:
21855 identifier
21856 template-id
21858 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21859 to indicate that names looked up in dependent types should be
21860 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21861 keyword has been used to indicate that the name that appears next
21862 is a template. TAG_TYPE indicates the explicit tag given before
21863 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21864 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21865 is the class being defined in a class-head. If ENUM_OK is TRUE,
21866 enum-names are also accepted.
21868 Returns the TYPE_DECL representing the class. */
21870 static tree
21871 cp_parser_class_name (cp_parser *parser,
21872 bool typename_keyword_p,
21873 bool template_keyword_p,
21874 enum tag_types tag_type,
21875 bool check_dependency_p,
21876 bool class_head_p,
21877 bool is_declaration,
21878 bool enum_ok)
21880 tree decl;
21881 tree scope;
21882 bool typename_p;
21883 cp_token *token;
21884 tree identifier = NULL_TREE;
21886 /* All class-names start with an identifier. */
21887 token = cp_lexer_peek_token (parser->lexer);
21888 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
21890 cp_parser_error (parser, "expected class-name");
21891 return error_mark_node;
21894 /* PARSER->SCOPE can be cleared when parsing the template-arguments
21895 to a template-id, so we save it here. */
21896 scope = parser->scope;
21897 if (scope == error_mark_node)
21898 return error_mark_node;
21900 /* Any name names a type if we're following the `typename' keyword
21901 in a qualified name where the enclosing scope is type-dependent. */
21902 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
21903 && dependent_type_p (scope));
21904 /* Handle the common case (an identifier, but not a template-id)
21905 efficiently. */
21906 if (token->type == CPP_NAME
21907 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
21909 cp_token *identifier_token;
21910 bool ambiguous_p;
21912 /* Look for the identifier. */
21913 identifier_token = cp_lexer_peek_token (parser->lexer);
21914 ambiguous_p = identifier_token->error_reported;
21915 identifier = cp_parser_identifier (parser);
21916 /* If the next token isn't an identifier, we are certainly not
21917 looking at a class-name. */
21918 if (identifier == error_mark_node)
21919 decl = error_mark_node;
21920 /* If we know this is a type-name, there's no need to look it
21921 up. */
21922 else if (typename_p)
21923 decl = identifier;
21924 else
21926 tree ambiguous_decls;
21927 /* If we already know that this lookup is ambiguous, then
21928 we've already issued an error message; there's no reason
21929 to check again. */
21930 if (ambiguous_p)
21932 cp_parser_simulate_error (parser);
21933 return error_mark_node;
21935 /* If the next token is a `::', then the name must be a type
21936 name.
21938 [basic.lookup.qual]
21940 During the lookup for a name preceding the :: scope
21941 resolution operator, object, function, and enumerator
21942 names are ignored. */
21943 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21944 tag_type = scope_type;
21945 /* Look up the name. */
21946 decl = cp_parser_lookup_name (parser, identifier,
21947 tag_type,
21948 /*is_template=*/false,
21949 /*is_namespace=*/false,
21950 check_dependency_p,
21951 &ambiguous_decls,
21952 identifier_token->location);
21953 if (ambiguous_decls)
21955 if (cp_parser_parsing_tentatively (parser))
21956 cp_parser_simulate_error (parser);
21957 return error_mark_node;
21961 else
21963 /* Try a template-id. */
21964 decl = cp_parser_template_id (parser, template_keyword_p,
21965 check_dependency_p,
21966 tag_type,
21967 is_declaration);
21968 if (decl == error_mark_node)
21969 return error_mark_node;
21972 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
21974 /* If this is a typename, create a TYPENAME_TYPE. */
21975 if (typename_p && decl != error_mark_node)
21977 decl = make_typename_type (scope, decl, typename_type,
21978 /*complain=*/tf_error);
21979 if (decl != error_mark_node)
21980 decl = TYPE_NAME (decl);
21983 decl = strip_using_decl (decl);
21985 /* Check to see that it is really the name of a class. */
21986 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
21987 && identifier_p (TREE_OPERAND (decl, 0))
21988 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
21989 /* Situations like this:
21991 template <typename T> struct A {
21992 typename T::template X<int>::I i;
21995 are problematic. Is `T::template X<int>' a class-name? The
21996 standard does not seem to be definitive, but there is no other
21997 valid interpretation of the following `::'. Therefore, those
21998 names are considered class-names. */
22000 decl = make_typename_type (scope, decl, tag_type, tf_error);
22001 if (decl != error_mark_node)
22002 decl = TYPE_NAME (decl);
22004 else if (TREE_CODE (decl) != TYPE_DECL
22005 || TREE_TYPE (decl) == error_mark_node
22006 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22007 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22008 /* In Objective-C 2.0, a classname followed by '.' starts a
22009 dot-syntax expression, and it's not a type-name. */
22010 || (c_dialect_objc ()
22011 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22012 && objc_is_class_name (decl)))
22013 decl = error_mark_node;
22015 if (decl == error_mark_node)
22016 cp_parser_error (parser, "expected class-name");
22017 else if (identifier && !parser->scope)
22018 maybe_note_name_used_in_class (identifier, decl);
22020 return decl;
22023 /* Parse a class-specifier.
22025 class-specifier:
22026 class-head { member-specification [opt] }
22028 Returns the TREE_TYPE representing the class. */
22030 static tree
22031 cp_parser_class_specifier_1 (cp_parser* parser)
22033 tree type;
22034 tree attributes = NULL_TREE;
22035 bool nested_name_specifier_p;
22036 unsigned saved_num_template_parameter_lists;
22037 bool saved_in_function_body;
22038 unsigned char in_statement;
22039 bool in_switch_statement_p;
22040 bool saved_in_unbraced_linkage_specification_p;
22041 tree old_scope = NULL_TREE;
22042 tree scope = NULL_TREE;
22043 cp_token *closing_brace;
22045 push_deferring_access_checks (dk_no_deferred);
22047 /* Parse the class-head. */
22048 type = cp_parser_class_head (parser,
22049 &nested_name_specifier_p);
22050 /* If the class-head was a semantic disaster, skip the entire body
22051 of the class. */
22052 if (!type)
22054 cp_parser_skip_to_end_of_block_or_statement (parser);
22055 pop_deferring_access_checks ();
22056 return error_mark_node;
22059 /* Look for the `{'. */
22060 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22062 pop_deferring_access_checks ();
22063 return error_mark_node;
22066 cp_ensure_no_omp_declare_simd (parser);
22067 cp_ensure_no_oacc_routine (parser);
22069 /* Issue an error message if type-definitions are forbidden here. */
22070 cp_parser_check_type_definition (parser);
22071 /* Remember that we are defining one more class. */
22072 ++parser->num_classes_being_defined;
22073 /* Inside the class, surrounding template-parameter-lists do not
22074 apply. */
22075 saved_num_template_parameter_lists
22076 = parser->num_template_parameter_lists;
22077 parser->num_template_parameter_lists = 0;
22078 /* We are not in a function body. */
22079 saved_in_function_body = parser->in_function_body;
22080 parser->in_function_body = false;
22081 /* Or in a loop. */
22082 in_statement = parser->in_statement;
22083 parser->in_statement = 0;
22084 /* Or in a switch. */
22085 in_switch_statement_p = parser->in_switch_statement_p;
22086 parser->in_switch_statement_p = false;
22087 /* We are not immediately inside an extern "lang" block. */
22088 saved_in_unbraced_linkage_specification_p
22089 = parser->in_unbraced_linkage_specification_p;
22090 parser->in_unbraced_linkage_specification_p = false;
22092 // Associate constraints with the type.
22093 if (flag_concepts)
22094 type = associate_classtype_constraints (type);
22096 /* Start the class. */
22097 if (nested_name_specifier_p)
22099 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22100 old_scope = push_inner_scope (scope);
22102 type = begin_class_definition (type);
22104 if (type == error_mark_node)
22105 /* If the type is erroneous, skip the entire body of the class. */
22106 cp_parser_skip_to_closing_brace (parser);
22107 else
22108 /* Parse the member-specification. */
22109 cp_parser_member_specification_opt (parser);
22111 /* Look for the trailing `}'. */
22112 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22113 /* Look for trailing attributes to apply to this class. */
22114 if (cp_parser_allow_gnu_extensions_p (parser))
22115 attributes = cp_parser_gnu_attributes_opt (parser);
22116 if (type != error_mark_node)
22117 type = finish_struct (type, attributes);
22118 if (nested_name_specifier_p)
22119 pop_inner_scope (old_scope, scope);
22121 /* We've finished a type definition. Check for the common syntax
22122 error of forgetting a semicolon after the definition. We need to
22123 be careful, as we can't just check for not-a-semicolon and be done
22124 with it; the user might have typed:
22126 class X { } c = ...;
22127 class X { } *p = ...;
22129 and so forth. Instead, enumerate all the possible tokens that
22130 might follow this production; if we don't see one of them, then
22131 complain and silently insert the semicolon. */
22133 cp_token *token = cp_lexer_peek_token (parser->lexer);
22134 bool want_semicolon = true;
22136 if (cp_next_tokens_can_be_std_attribute_p (parser))
22137 /* Don't try to parse c++11 attributes here. As per the
22138 grammar, that should be a task for
22139 cp_parser_decl_specifier_seq. */
22140 want_semicolon = false;
22142 switch (token->type)
22144 case CPP_NAME:
22145 case CPP_SEMICOLON:
22146 case CPP_MULT:
22147 case CPP_AND:
22148 case CPP_OPEN_PAREN:
22149 case CPP_CLOSE_PAREN:
22150 case CPP_COMMA:
22151 want_semicolon = false;
22152 break;
22154 /* While it's legal for type qualifiers and storage class
22155 specifiers to follow type definitions in the grammar, only
22156 compiler testsuites contain code like that. Assume that if
22157 we see such code, then what we're really seeing is a case
22158 like:
22160 class X { }
22161 const <type> var = ...;
22165 class Y { }
22166 static <type> func (...) ...
22168 i.e. the qualifier or specifier applies to the next
22169 declaration. To do so, however, we need to look ahead one
22170 more token to see if *that* token is a type specifier.
22172 This code could be improved to handle:
22174 class Z { }
22175 static const <type> var = ...; */
22176 case CPP_KEYWORD:
22177 if (keyword_is_decl_specifier (token->keyword))
22179 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22181 /* Handling user-defined types here would be nice, but very
22182 tricky. */
22183 want_semicolon
22184 = (lookahead->type == CPP_KEYWORD
22185 && keyword_begins_type_specifier (lookahead->keyword));
22187 break;
22188 default:
22189 break;
22192 /* If we don't have a type, then something is very wrong and we
22193 shouldn't try to do anything clever. Likewise for not seeing the
22194 closing brace. */
22195 if (closing_brace && TYPE_P (type) && want_semicolon)
22197 /* Locate the closing brace. */
22198 cp_token_position prev
22199 = cp_lexer_previous_token_position (parser->lexer);
22200 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22201 location_t loc = prev_token->location;
22203 /* We want to suggest insertion of a ';' immediately *after* the
22204 closing brace, so, if we can, offset the location by 1 column. */
22205 location_t next_loc = loc;
22206 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22207 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22209 rich_location richloc (line_table, next_loc);
22211 /* If we successfully offset the location, suggest the fix-it. */
22212 if (next_loc != loc)
22213 richloc.add_fixit_insert_before (next_loc, ";");
22215 if (CLASSTYPE_DECLARED_CLASS (type))
22216 error_at_rich_loc (&richloc,
22217 "expected %<;%> after class definition");
22218 else if (TREE_CODE (type) == RECORD_TYPE)
22219 error_at_rich_loc (&richloc,
22220 "expected %<;%> after struct definition");
22221 else if (TREE_CODE (type) == UNION_TYPE)
22222 error_at_rich_loc (&richloc,
22223 "expected %<;%> after union definition");
22224 else
22225 gcc_unreachable ();
22227 /* Unget one token and smash it to look as though we encountered
22228 a semicolon in the input stream. */
22229 cp_lexer_set_token_position (parser->lexer, prev);
22230 token = cp_lexer_peek_token (parser->lexer);
22231 token->type = CPP_SEMICOLON;
22232 token->keyword = RID_MAX;
22236 /* If this class is not itself within the scope of another class,
22237 then we need to parse the bodies of all of the queued function
22238 definitions. Note that the queued functions defined in a class
22239 are not always processed immediately following the
22240 class-specifier for that class. Consider:
22242 struct A {
22243 struct B { void f() { sizeof (A); } };
22246 If `f' were processed before the processing of `A' were
22247 completed, there would be no way to compute the size of `A'.
22248 Note that the nesting we are interested in here is lexical --
22249 not the semantic nesting given by TYPE_CONTEXT. In particular,
22250 for:
22252 struct A { struct B; };
22253 struct A::B { void f() { } };
22255 there is no need to delay the parsing of `A::B::f'. */
22256 if (--parser->num_classes_being_defined == 0)
22258 tree decl;
22259 tree class_type = NULL_TREE;
22260 tree pushed_scope = NULL_TREE;
22261 unsigned ix;
22262 cp_default_arg_entry *e;
22263 tree save_ccp, save_ccr;
22265 /* In a first pass, parse default arguments to the functions.
22266 Then, in a second pass, parse the bodies of the functions.
22267 This two-phased approach handles cases like:
22269 struct S {
22270 void f() { g(); }
22271 void g(int i = 3);
22275 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22277 decl = e->decl;
22278 /* If there are default arguments that have not yet been processed,
22279 take care of them now. */
22280 if (class_type != e->class_type)
22282 if (pushed_scope)
22283 pop_scope (pushed_scope);
22284 class_type = e->class_type;
22285 pushed_scope = push_scope (class_type);
22287 /* Make sure that any template parameters are in scope. */
22288 maybe_begin_member_template_processing (decl);
22289 /* Parse the default argument expressions. */
22290 cp_parser_late_parsing_default_args (parser, decl);
22291 /* Remove any template parameters from the symbol table. */
22292 maybe_end_member_template_processing ();
22294 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22295 /* Now parse any NSDMIs. */
22296 save_ccp = current_class_ptr;
22297 save_ccr = current_class_ref;
22298 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22300 if (class_type != DECL_CONTEXT (decl))
22302 if (pushed_scope)
22303 pop_scope (pushed_scope);
22304 class_type = DECL_CONTEXT (decl);
22305 pushed_scope = push_scope (class_type);
22307 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22308 cp_parser_late_parsing_nsdmi (parser, decl);
22310 vec_safe_truncate (unparsed_nsdmis, 0);
22311 current_class_ptr = save_ccp;
22312 current_class_ref = save_ccr;
22313 if (pushed_scope)
22314 pop_scope (pushed_scope);
22316 /* Now do some post-NSDMI bookkeeping. */
22317 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22318 after_nsdmi_defaulted_late_checks (class_type);
22319 vec_safe_truncate (unparsed_classes, 0);
22320 after_nsdmi_defaulted_late_checks (type);
22322 /* Now parse the body of the functions. */
22323 if (flag_openmp)
22325 /* OpenMP UDRs need to be parsed before all other functions. */
22326 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22327 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22328 cp_parser_late_parsing_for_member (parser, decl);
22329 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22330 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22331 cp_parser_late_parsing_for_member (parser, decl);
22333 else
22334 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22335 cp_parser_late_parsing_for_member (parser, decl);
22336 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22338 else
22339 vec_safe_push (unparsed_classes, type);
22341 /* Put back any saved access checks. */
22342 pop_deferring_access_checks ();
22344 /* Restore saved state. */
22345 parser->in_switch_statement_p = in_switch_statement_p;
22346 parser->in_statement = in_statement;
22347 parser->in_function_body = saved_in_function_body;
22348 parser->num_template_parameter_lists
22349 = saved_num_template_parameter_lists;
22350 parser->in_unbraced_linkage_specification_p
22351 = saved_in_unbraced_linkage_specification_p;
22353 return type;
22356 static tree
22357 cp_parser_class_specifier (cp_parser* parser)
22359 tree ret;
22360 timevar_push (TV_PARSE_STRUCT);
22361 ret = cp_parser_class_specifier_1 (parser);
22362 timevar_pop (TV_PARSE_STRUCT);
22363 return ret;
22366 /* Parse a class-head.
22368 class-head:
22369 class-key identifier [opt] base-clause [opt]
22370 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22371 class-key nested-name-specifier [opt] template-id
22372 base-clause [opt]
22374 class-virt-specifier:
22375 final
22377 GNU Extensions:
22378 class-key attributes identifier [opt] base-clause [opt]
22379 class-key attributes nested-name-specifier identifier base-clause [opt]
22380 class-key attributes nested-name-specifier [opt] template-id
22381 base-clause [opt]
22383 Upon return BASES is initialized to the list of base classes (or
22384 NULL, if there are none) in the same form returned by
22385 cp_parser_base_clause.
22387 Returns the TYPE of the indicated class. Sets
22388 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22389 involving a nested-name-specifier was used, and FALSE otherwise.
22391 Returns error_mark_node if this is not a class-head.
22393 Returns NULL_TREE if the class-head is syntactically valid, but
22394 semantically invalid in a way that means we should skip the entire
22395 body of the class. */
22397 static tree
22398 cp_parser_class_head (cp_parser* parser,
22399 bool* nested_name_specifier_p)
22401 tree nested_name_specifier;
22402 enum tag_types class_key;
22403 tree id = NULL_TREE;
22404 tree type = NULL_TREE;
22405 tree attributes;
22406 tree bases;
22407 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22408 bool template_id_p = false;
22409 bool qualified_p = false;
22410 bool invalid_nested_name_p = false;
22411 bool invalid_explicit_specialization_p = false;
22412 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22413 tree pushed_scope = NULL_TREE;
22414 unsigned num_templates;
22415 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22416 /* Assume no nested-name-specifier will be present. */
22417 *nested_name_specifier_p = false;
22418 /* Assume no template parameter lists will be used in defining the
22419 type. */
22420 num_templates = 0;
22421 parser->colon_corrects_to_scope_p = false;
22423 /* Look for the class-key. */
22424 class_key = cp_parser_class_key (parser);
22425 if (class_key == none_type)
22426 return error_mark_node;
22428 location_t class_head_start_location = input_location;
22430 /* Parse the attributes. */
22431 attributes = cp_parser_attributes_opt (parser);
22433 /* If the next token is `::', that is invalid -- but sometimes
22434 people do try to write:
22436 struct ::S {};
22438 Handle this gracefully by accepting the extra qualifier, and then
22439 issuing an error about it later if this really is a
22440 class-head. If it turns out just to be an elaborated type
22441 specifier, remain silent. */
22442 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22443 qualified_p = true;
22445 push_deferring_access_checks (dk_no_check);
22447 /* Determine the name of the class. Begin by looking for an
22448 optional nested-name-specifier. */
22449 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22450 nested_name_specifier
22451 = cp_parser_nested_name_specifier_opt (parser,
22452 /*typename_keyword_p=*/false,
22453 /*check_dependency_p=*/false,
22454 /*type_p=*/true,
22455 /*is_declaration=*/false);
22456 /* If there was a nested-name-specifier, then there *must* be an
22457 identifier. */
22458 if (nested_name_specifier)
22460 type_start_token = cp_lexer_peek_token (parser->lexer);
22461 /* Although the grammar says `identifier', it really means
22462 `class-name' or `template-name'. You are only allowed to
22463 define a class that has already been declared with this
22464 syntax.
22466 The proposed resolution for Core Issue 180 says that wherever
22467 you see `class T::X' you should treat `X' as a type-name.
22469 It is OK to define an inaccessible class; for example:
22471 class A { class B; };
22472 class A::B {};
22474 We do not know if we will see a class-name, or a
22475 template-name. We look for a class-name first, in case the
22476 class-name is a template-id; if we looked for the
22477 template-name first we would stop after the template-name. */
22478 cp_parser_parse_tentatively (parser);
22479 type = cp_parser_class_name (parser,
22480 /*typename_keyword_p=*/false,
22481 /*template_keyword_p=*/false,
22482 class_type,
22483 /*check_dependency_p=*/false,
22484 /*class_head_p=*/true,
22485 /*is_declaration=*/false);
22486 /* If that didn't work, ignore the nested-name-specifier. */
22487 if (!cp_parser_parse_definitely (parser))
22489 invalid_nested_name_p = true;
22490 type_start_token = cp_lexer_peek_token (parser->lexer);
22491 id = cp_parser_identifier (parser);
22492 if (id == error_mark_node)
22493 id = NULL_TREE;
22495 /* If we could not find a corresponding TYPE, treat this
22496 declaration like an unqualified declaration. */
22497 if (type == error_mark_node)
22498 nested_name_specifier = NULL_TREE;
22499 /* Otherwise, count the number of templates used in TYPE and its
22500 containing scopes. */
22501 else
22503 tree scope;
22505 for (scope = TREE_TYPE (type);
22506 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22507 scope = get_containing_scope (scope))
22508 if (TYPE_P (scope)
22509 && CLASS_TYPE_P (scope)
22510 && CLASSTYPE_TEMPLATE_INFO (scope)
22511 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22512 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22513 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22514 ++num_templates;
22517 /* Otherwise, the identifier is optional. */
22518 else
22520 /* We don't know whether what comes next is a template-id,
22521 an identifier, or nothing at all. */
22522 cp_parser_parse_tentatively (parser);
22523 /* Check for a template-id. */
22524 type_start_token = cp_lexer_peek_token (parser->lexer);
22525 id = cp_parser_template_id (parser,
22526 /*template_keyword_p=*/false,
22527 /*check_dependency_p=*/true,
22528 class_key,
22529 /*is_declaration=*/true);
22530 /* If that didn't work, it could still be an identifier. */
22531 if (!cp_parser_parse_definitely (parser))
22533 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22535 type_start_token = cp_lexer_peek_token (parser->lexer);
22536 id = cp_parser_identifier (parser);
22538 else
22539 id = NULL_TREE;
22541 else
22543 template_id_p = true;
22544 ++num_templates;
22548 pop_deferring_access_checks ();
22550 if (id)
22552 cp_parser_check_for_invalid_template_id (parser, id,
22553 class_key,
22554 type_start_token->location);
22556 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22558 /* If it's not a `:' or a `{' then we can't really be looking at a
22559 class-head, since a class-head only appears as part of a
22560 class-specifier. We have to detect this situation before calling
22561 xref_tag, since that has irreversible side-effects. */
22562 if (!cp_parser_next_token_starts_class_definition_p (parser))
22564 cp_parser_error (parser, "expected %<{%> or %<:%>");
22565 type = error_mark_node;
22566 goto out;
22569 /* At this point, we're going ahead with the class-specifier, even
22570 if some other problem occurs. */
22571 cp_parser_commit_to_tentative_parse (parser);
22572 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22574 cp_parser_error (parser,
22575 "cannot specify %<override%> for a class");
22576 type = error_mark_node;
22577 goto out;
22579 /* Issue the error about the overly-qualified name now. */
22580 if (qualified_p)
22582 cp_parser_error (parser,
22583 "global qualification of class name is invalid");
22584 type = error_mark_node;
22585 goto out;
22587 else if (invalid_nested_name_p)
22589 cp_parser_error (parser,
22590 "qualified name does not name a class");
22591 type = error_mark_node;
22592 goto out;
22594 else if (nested_name_specifier)
22596 tree scope;
22598 /* Reject typedef-names in class heads. */
22599 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22601 error_at (type_start_token->location,
22602 "invalid class name in declaration of %qD",
22603 type);
22604 type = NULL_TREE;
22605 goto done;
22608 /* Figure out in what scope the declaration is being placed. */
22609 scope = current_scope ();
22610 /* If that scope does not contain the scope in which the
22611 class was originally declared, the program is invalid. */
22612 if (scope && !is_ancestor (scope, nested_name_specifier))
22614 if (at_namespace_scope_p ())
22615 error_at (type_start_token->location,
22616 "declaration of %qD in namespace %qD which does not "
22617 "enclose %qD",
22618 type, scope, nested_name_specifier);
22619 else
22620 error_at (type_start_token->location,
22621 "declaration of %qD in %qD which does not enclose %qD",
22622 type, scope, nested_name_specifier);
22623 type = NULL_TREE;
22624 goto done;
22626 /* [dcl.meaning]
22628 A declarator-id shall not be qualified except for the
22629 definition of a ... nested class outside of its class
22630 ... [or] the definition or explicit instantiation of a
22631 class member of a namespace outside of its namespace. */
22632 if (scope == nested_name_specifier)
22634 permerror (nested_name_specifier_token_start->location,
22635 "extra qualification not allowed");
22636 nested_name_specifier = NULL_TREE;
22637 num_templates = 0;
22640 /* An explicit-specialization must be preceded by "template <>". If
22641 it is not, try to recover gracefully. */
22642 if (at_namespace_scope_p ()
22643 && parser->num_template_parameter_lists == 0
22644 && !processing_template_parmlist
22645 && template_id_p)
22647 /* Build a location of this form:
22648 struct typename <ARGS>
22649 ^~~~~~~~~~~~~~~~~~~~~~
22650 with caret==start at the start token, and
22651 finishing at the end of the type. */
22652 location_t reported_loc
22653 = make_location (class_head_start_location,
22654 class_head_start_location,
22655 get_finish (type_start_token->location));
22656 rich_location richloc (line_table, reported_loc);
22657 richloc.add_fixit_insert_before (class_head_start_location,
22658 "template <> ");
22659 error_at_rich_loc
22660 (&richloc,
22661 "an explicit specialization must be preceded by %<template <>%>");
22662 invalid_explicit_specialization_p = true;
22663 /* Take the same action that would have been taken by
22664 cp_parser_explicit_specialization. */
22665 ++parser->num_template_parameter_lists;
22666 begin_specialization ();
22668 /* There must be no "return" statements between this point and the
22669 end of this function; set "type "to the correct return value and
22670 use "goto done;" to return. */
22671 /* Make sure that the right number of template parameters were
22672 present. */
22673 if (!cp_parser_check_template_parameters (parser, num_templates,
22674 type_start_token->location,
22675 /*declarator=*/NULL))
22677 /* If something went wrong, there is no point in even trying to
22678 process the class-definition. */
22679 type = NULL_TREE;
22680 goto done;
22683 /* Look up the type. */
22684 if (template_id_p)
22686 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22687 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22688 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22690 error_at (type_start_token->location,
22691 "function template %qD redeclared as a class template", id);
22692 type = error_mark_node;
22694 else
22696 type = TREE_TYPE (id);
22697 type = maybe_process_partial_specialization (type);
22699 /* Check the scope while we still know whether or not we had a
22700 nested-name-specifier. */
22701 if (type != error_mark_node)
22702 check_unqualified_spec_or_inst (type, type_start_token->location);
22704 if (nested_name_specifier)
22705 pushed_scope = push_scope (nested_name_specifier);
22707 else if (nested_name_specifier)
22709 tree class_type;
22711 /* Given:
22713 template <typename T> struct S { struct T };
22714 template <typename T> struct S<T>::T { };
22716 we will get a TYPENAME_TYPE when processing the definition of
22717 `S::T'. We need to resolve it to the actual type before we
22718 try to define it. */
22719 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22721 class_type = resolve_typename_type (TREE_TYPE (type),
22722 /*only_current_p=*/false);
22723 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22724 type = TYPE_NAME (class_type);
22725 else
22727 cp_parser_error (parser, "could not resolve typename type");
22728 type = error_mark_node;
22732 if (maybe_process_partial_specialization (TREE_TYPE (type))
22733 == error_mark_node)
22735 type = NULL_TREE;
22736 goto done;
22739 class_type = current_class_type;
22740 /* Enter the scope indicated by the nested-name-specifier. */
22741 pushed_scope = push_scope (nested_name_specifier);
22742 /* Get the canonical version of this type. */
22743 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22744 /* Call push_template_decl if it seems like we should be defining a
22745 template either from the template headers or the type we're
22746 defining, so that we diagnose both extra and missing headers. */
22747 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22748 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22749 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22751 type = push_template_decl (type);
22752 if (type == error_mark_node)
22754 type = NULL_TREE;
22755 goto done;
22759 type = TREE_TYPE (type);
22760 *nested_name_specifier_p = true;
22762 else /* The name is not a nested name. */
22764 /* If the class was unnamed, create a dummy name. */
22765 if (!id)
22766 id = make_anon_name ();
22767 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
22768 parser->num_template_parameter_lists);
22771 /* Indicate whether this class was declared as a `class' or as a
22772 `struct'. */
22773 if (TREE_CODE (type) == RECORD_TYPE)
22774 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22775 cp_parser_check_class_key (class_key, type);
22777 /* If this type was already complete, and we see another definition,
22778 that's an error. */
22779 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22781 error_at (type_start_token->location, "redefinition of %q#T",
22782 type);
22783 inform (location_of (type), "previous definition of %q#T",
22784 type);
22785 type = NULL_TREE;
22786 goto done;
22788 else if (type == error_mark_node)
22789 type = NULL_TREE;
22791 if (type)
22793 /* Apply attributes now, before any use of the class as a template
22794 argument in its base list. */
22795 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22796 fixup_attribute_variants (type);
22799 /* We will have entered the scope containing the class; the names of
22800 base classes should be looked up in that context. For example:
22802 struct A { struct B {}; struct C; };
22803 struct A::C : B {};
22805 is valid. */
22807 /* Get the list of base-classes, if there is one. */
22808 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22810 /* PR59482: enter the class scope so that base-specifiers are looked
22811 up correctly. */
22812 if (type)
22813 pushclass (type);
22814 bases = cp_parser_base_clause (parser);
22815 /* PR59482: get out of the previously pushed class scope so that the
22816 subsequent pops pop the right thing. */
22817 if (type)
22818 popclass ();
22820 else
22821 bases = NULL_TREE;
22823 /* If we're really defining a class, process the base classes.
22824 If they're invalid, fail. */
22825 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22826 xref_basetypes (type, bases);
22828 done:
22829 /* Leave the scope given by the nested-name-specifier. We will
22830 enter the class scope itself while processing the members. */
22831 if (pushed_scope)
22832 pop_scope (pushed_scope);
22834 if (invalid_explicit_specialization_p)
22836 end_specialization ();
22837 --parser->num_template_parameter_lists;
22840 if (type)
22841 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22842 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22843 CLASSTYPE_FINAL (type) = 1;
22844 out:
22845 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22846 return type;
22849 /* Parse a class-key.
22851 class-key:
22852 class
22853 struct
22854 union
22856 Returns the kind of class-key specified, or none_type to indicate
22857 error. */
22859 static enum tag_types
22860 cp_parser_class_key (cp_parser* parser)
22862 cp_token *token;
22863 enum tag_types tag_type;
22865 /* Look for the class-key. */
22866 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22867 if (!token)
22868 return none_type;
22870 /* Check to see if the TOKEN is a class-key. */
22871 tag_type = cp_parser_token_is_class_key (token);
22872 if (!tag_type)
22873 cp_parser_error (parser, "expected class-key");
22874 return tag_type;
22877 /* Parse a type-parameter-key.
22879 type-parameter-key:
22880 class
22881 typename
22884 static void
22885 cp_parser_type_parameter_key (cp_parser* parser)
22887 /* Look for the type-parameter-key. */
22888 enum tag_types tag_type = none_type;
22889 cp_token *token = cp_lexer_peek_token (parser->lexer);
22890 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
22892 cp_lexer_consume_token (parser->lexer);
22893 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
22894 /* typename is not allowed in a template template parameter
22895 by the standard until C++1Z. */
22896 pedwarn (token->location, OPT_Wpedantic,
22897 "ISO C++ forbids typename key in template template parameter;"
22898 " use -std=c++1z or -std=gnu++1z");
22900 else
22901 cp_parser_error (parser, "expected %<class%> or %<typename%>");
22903 return;
22906 /* Parse an (optional) member-specification.
22908 member-specification:
22909 member-declaration member-specification [opt]
22910 access-specifier : member-specification [opt] */
22912 static void
22913 cp_parser_member_specification_opt (cp_parser* parser)
22915 while (true)
22917 cp_token *token;
22918 enum rid keyword;
22920 /* Peek at the next token. */
22921 token = cp_lexer_peek_token (parser->lexer);
22922 /* If it's a `}', or EOF then we've seen all the members. */
22923 if (token->type == CPP_CLOSE_BRACE
22924 || token->type == CPP_EOF
22925 || token->type == CPP_PRAGMA_EOL)
22926 break;
22928 /* See if this token is a keyword. */
22929 keyword = token->keyword;
22930 switch (keyword)
22932 case RID_PUBLIC:
22933 case RID_PROTECTED:
22934 case RID_PRIVATE:
22935 /* Consume the access-specifier. */
22936 cp_lexer_consume_token (parser->lexer);
22937 /* Remember which access-specifier is active. */
22938 current_access_specifier = token->u.value;
22939 /* Look for the `:'. */
22940 cp_parser_require (parser, CPP_COLON, RT_COLON);
22941 break;
22943 default:
22944 /* Accept #pragmas at class scope. */
22945 if (token->type == CPP_PRAGMA)
22947 cp_parser_pragma (parser, pragma_member, NULL);
22948 break;
22951 /* Otherwise, the next construction must be a
22952 member-declaration. */
22953 cp_parser_member_declaration (parser);
22958 /* Parse a member-declaration.
22960 member-declaration:
22961 decl-specifier-seq [opt] member-declarator-list [opt] ;
22962 function-definition ; [opt]
22963 :: [opt] nested-name-specifier template [opt] unqualified-id ;
22964 using-declaration
22965 template-declaration
22966 alias-declaration
22968 member-declarator-list:
22969 member-declarator
22970 member-declarator-list , member-declarator
22972 member-declarator:
22973 declarator pure-specifier [opt]
22974 declarator constant-initializer [opt]
22975 identifier [opt] : constant-expression
22977 GNU Extensions:
22979 member-declaration:
22980 __extension__ member-declaration
22982 member-declarator:
22983 declarator attributes [opt] pure-specifier [opt]
22984 declarator attributes [opt] constant-initializer [opt]
22985 identifier [opt] attributes [opt] : constant-expression
22987 C++0x Extensions:
22989 member-declaration:
22990 static_assert-declaration */
22992 static void
22993 cp_parser_member_declaration (cp_parser* parser)
22995 cp_decl_specifier_seq decl_specifiers;
22996 tree prefix_attributes;
22997 tree decl;
22998 int declares_class_or_enum;
22999 bool friend_p;
23000 cp_token *token = NULL;
23001 cp_token *decl_spec_token_start = NULL;
23002 cp_token *initializer_token_start = NULL;
23003 int saved_pedantic;
23004 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23006 /* Check for the `__extension__' keyword. */
23007 if (cp_parser_extension_opt (parser, &saved_pedantic))
23009 /* Recurse. */
23010 cp_parser_member_declaration (parser);
23011 /* Restore the old value of the PEDANTIC flag. */
23012 pedantic = saved_pedantic;
23014 return;
23017 /* Check for a template-declaration. */
23018 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23020 /* An explicit specialization here is an error condition, and we
23021 expect the specialization handler to detect and report this. */
23022 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23023 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23024 cp_parser_explicit_specialization (parser);
23025 else
23026 cp_parser_template_declaration (parser, /*member_p=*/true);
23028 return;
23030 /* Check for a template introduction. */
23031 else if (cp_parser_template_declaration_after_export (parser, true))
23032 return;
23034 /* Check for a using-declaration. */
23035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23037 if (cxx_dialect < cxx11)
23039 /* Parse the using-declaration. */
23040 cp_parser_using_declaration (parser,
23041 /*access_declaration_p=*/false);
23042 return;
23044 else
23046 tree decl;
23047 bool alias_decl_expected;
23048 cp_parser_parse_tentatively (parser);
23049 decl = cp_parser_alias_declaration (parser);
23050 /* Note that if we actually see the '=' token after the
23051 identifier, cp_parser_alias_declaration commits the
23052 tentative parse. In that case, we really expect an
23053 alias-declaration. Otherwise, we expect a using
23054 declaration. */
23055 alias_decl_expected =
23056 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23057 cp_parser_parse_definitely (parser);
23059 if (alias_decl_expected)
23060 finish_member_declaration (decl);
23061 else
23062 cp_parser_using_declaration (parser,
23063 /*access_declaration_p=*/false);
23064 return;
23068 /* Check for @defs. */
23069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23071 tree ivar, member;
23072 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23073 ivar = ivar_chains;
23074 while (ivar)
23076 member = ivar;
23077 ivar = TREE_CHAIN (member);
23078 TREE_CHAIN (member) = NULL_TREE;
23079 finish_member_declaration (member);
23081 return;
23084 /* If the next token is `static_assert' we have a static assertion. */
23085 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23087 cp_parser_static_assert (parser, /*member_p=*/true);
23088 return;
23091 parser->colon_corrects_to_scope_p = false;
23093 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23094 goto out;
23096 /* Parse the decl-specifier-seq. */
23097 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23098 cp_parser_decl_specifier_seq (parser,
23099 CP_PARSER_FLAGS_OPTIONAL,
23100 &decl_specifiers,
23101 &declares_class_or_enum);
23102 /* Check for an invalid type-name. */
23103 if (!decl_specifiers.any_type_specifiers_p
23104 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23105 goto out;
23106 /* If there is no declarator, then the decl-specifier-seq should
23107 specify a type. */
23108 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23110 /* If there was no decl-specifier-seq, and the next token is a
23111 `;', then we have something like:
23113 struct S { ; };
23115 [class.mem]
23117 Each member-declaration shall declare at least one member
23118 name of the class. */
23119 if (!decl_specifiers.any_specifiers_p)
23121 cp_token *token = cp_lexer_peek_token (parser->lexer);
23122 if (!in_system_header_at (token->location))
23123 pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
23125 else
23127 tree type;
23129 /* See if this declaration is a friend. */
23130 friend_p = cp_parser_friend_p (&decl_specifiers);
23131 /* If there were decl-specifiers, check to see if there was
23132 a class-declaration. */
23133 type = check_tag_decl (&decl_specifiers,
23134 /*explicit_type_instantiation_p=*/false);
23135 /* Nested classes have already been added to the class, but
23136 a `friend' needs to be explicitly registered. */
23137 if (friend_p)
23139 /* If the `friend' keyword was present, the friend must
23140 be introduced with a class-key. */
23141 if (!declares_class_or_enum && cxx_dialect < cxx11)
23142 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23143 "in C++03 a class-key must be used "
23144 "when declaring a friend");
23145 /* In this case:
23147 template <typename T> struct A {
23148 friend struct A<T>::B;
23151 A<T>::B will be represented by a TYPENAME_TYPE, and
23152 therefore not recognized by check_tag_decl. */
23153 if (!type)
23155 type = decl_specifiers.type;
23156 if (type && TREE_CODE (type) == TYPE_DECL)
23157 type = TREE_TYPE (type);
23159 if (!type || !TYPE_P (type))
23160 error_at (decl_spec_token_start->location,
23161 "friend declaration does not name a class or "
23162 "function");
23163 else
23164 make_friend_class (current_class_type, type,
23165 /*complain=*/true);
23167 /* If there is no TYPE, an error message will already have
23168 been issued. */
23169 else if (!type || type == error_mark_node)
23171 /* An anonymous aggregate has to be handled specially; such
23172 a declaration really declares a data member (with a
23173 particular type), as opposed to a nested class. */
23174 else if (ANON_AGGR_TYPE_P (type))
23176 /* C++11 9.5/6. */
23177 if (decl_specifiers.storage_class != sc_none)
23178 error_at (decl_spec_token_start->location,
23179 "a storage class on an anonymous aggregate "
23180 "in class scope is not allowed");
23182 /* Remove constructors and such from TYPE, now that we
23183 know it is an anonymous aggregate. */
23184 fixup_anonymous_aggr (type);
23185 /* And make the corresponding data member. */
23186 decl = build_decl (decl_spec_token_start->location,
23187 FIELD_DECL, NULL_TREE, type);
23188 /* Add it to the class. */
23189 finish_member_declaration (decl);
23191 else
23192 cp_parser_check_access_in_redeclaration
23193 (TYPE_NAME (type),
23194 decl_spec_token_start->location);
23197 else
23199 bool assume_semicolon = false;
23201 /* Clear attributes from the decl_specifiers but keep them
23202 around as prefix attributes that apply them to the entity
23203 being declared. */
23204 prefix_attributes = decl_specifiers.attributes;
23205 decl_specifiers.attributes = NULL_TREE;
23207 /* See if these declarations will be friends. */
23208 friend_p = cp_parser_friend_p (&decl_specifiers);
23210 /* Keep going until we hit the `;' at the end of the
23211 declaration. */
23212 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23214 tree attributes = NULL_TREE;
23215 tree first_attribute;
23217 /* Peek at the next token. */
23218 token = cp_lexer_peek_token (parser->lexer);
23220 /* Check for a bitfield declaration. */
23221 if (token->type == CPP_COLON
23222 || (token->type == CPP_NAME
23223 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23224 == CPP_COLON))
23226 tree identifier;
23227 tree width;
23229 /* Get the name of the bitfield. Note that we cannot just
23230 check TOKEN here because it may have been invalidated by
23231 the call to cp_lexer_peek_nth_token above. */
23232 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23233 identifier = cp_parser_identifier (parser);
23234 else
23235 identifier = NULL_TREE;
23237 /* Consume the `:' token. */
23238 cp_lexer_consume_token (parser->lexer);
23239 /* Get the width of the bitfield. */
23240 width
23241 = cp_parser_constant_expression (parser);
23243 /* Look for attributes that apply to the bitfield. */
23244 attributes = cp_parser_attributes_opt (parser);
23245 /* Remember which attributes are prefix attributes and
23246 which are not. */
23247 first_attribute = attributes;
23248 /* Combine the attributes. */
23249 attributes = chainon (prefix_attributes, attributes);
23251 /* Create the bitfield declaration. */
23252 decl = grokbitfield (identifier
23253 ? make_id_declarator (NULL_TREE,
23254 identifier,
23255 sfk_none)
23256 : NULL,
23257 &decl_specifiers,
23258 width,
23259 attributes);
23261 else
23263 cp_declarator *declarator;
23264 tree initializer;
23265 tree asm_specification;
23266 int ctor_dtor_or_conv_p;
23268 /* Parse the declarator. */
23269 declarator
23270 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23271 &ctor_dtor_or_conv_p,
23272 /*parenthesized_p=*/NULL,
23273 /*member_p=*/true,
23274 friend_p);
23276 /* If something went wrong parsing the declarator, make sure
23277 that we at least consume some tokens. */
23278 if (declarator == cp_error_declarator)
23280 /* Skip to the end of the statement. */
23281 cp_parser_skip_to_end_of_statement (parser);
23282 /* If the next token is not a semicolon, that is
23283 probably because we just skipped over the body of
23284 a function. So, we consume a semicolon if
23285 present, but do not issue an error message if it
23286 is not present. */
23287 if (cp_lexer_next_token_is (parser->lexer,
23288 CPP_SEMICOLON))
23289 cp_lexer_consume_token (parser->lexer);
23290 goto out;
23293 if (declares_class_or_enum & 2)
23294 cp_parser_check_for_definition_in_return_type
23295 (declarator, decl_specifiers.type,
23296 decl_specifiers.locations[ds_type_spec]);
23298 /* Look for an asm-specification. */
23299 asm_specification = cp_parser_asm_specification_opt (parser);
23300 /* Look for attributes that apply to the declaration. */
23301 attributes = cp_parser_attributes_opt (parser);
23302 /* Remember which attributes are prefix attributes and
23303 which are not. */
23304 first_attribute = attributes;
23305 /* Combine the attributes. */
23306 attributes = chainon (prefix_attributes, attributes);
23308 /* If it's an `=', then we have a constant-initializer or a
23309 pure-specifier. It is not correct to parse the
23310 initializer before registering the member declaration
23311 since the member declaration should be in scope while
23312 its initializer is processed. However, the rest of the
23313 front end does not yet provide an interface that allows
23314 us to handle this correctly. */
23315 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23317 /* In [class.mem]:
23319 A pure-specifier shall be used only in the declaration of
23320 a virtual function.
23322 A member-declarator can contain a constant-initializer
23323 only if it declares a static member of integral or
23324 enumeration type.
23326 Therefore, if the DECLARATOR is for a function, we look
23327 for a pure-specifier; otherwise, we look for a
23328 constant-initializer. When we call `grokfield', it will
23329 perform more stringent semantics checks. */
23330 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23331 if (function_declarator_p (declarator)
23332 || (decl_specifiers.type
23333 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23334 && declarator->kind == cdk_id
23335 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23336 == FUNCTION_TYPE)))
23337 initializer = cp_parser_pure_specifier (parser);
23338 else if (decl_specifiers.storage_class != sc_static)
23339 initializer = cp_parser_save_nsdmi (parser);
23340 else if (cxx_dialect >= cxx11)
23342 bool nonconst;
23343 /* Don't require a constant rvalue in C++11, since we
23344 might want a reference constant. We'll enforce
23345 constancy later. */
23346 cp_lexer_consume_token (parser->lexer);
23347 /* Parse the initializer. */
23348 initializer = cp_parser_initializer_clause (parser,
23349 &nonconst);
23351 else
23352 /* Parse the initializer. */
23353 initializer = cp_parser_constant_initializer (parser);
23355 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23356 && !function_declarator_p (declarator))
23358 bool x;
23359 if (decl_specifiers.storage_class != sc_static)
23360 initializer = cp_parser_save_nsdmi (parser);
23361 else
23362 initializer = cp_parser_initializer (parser, &x, &x);
23364 /* Otherwise, there is no initializer. */
23365 else
23366 initializer = NULL_TREE;
23368 /* See if we are probably looking at a function
23369 definition. We are certainly not looking at a
23370 member-declarator. Calling `grokfield' has
23371 side-effects, so we must not do it unless we are sure
23372 that we are looking at a member-declarator. */
23373 if (cp_parser_token_starts_function_definition_p
23374 (cp_lexer_peek_token (parser->lexer)))
23376 /* The grammar does not allow a pure-specifier to be
23377 used when a member function is defined. (It is
23378 possible that this fact is an oversight in the
23379 standard, since a pure function may be defined
23380 outside of the class-specifier. */
23381 if (initializer && initializer_token_start)
23382 error_at (initializer_token_start->location,
23383 "pure-specifier on function-definition");
23384 decl = cp_parser_save_member_function_body (parser,
23385 &decl_specifiers,
23386 declarator,
23387 attributes);
23388 if (parser->fully_implicit_function_template_p)
23389 decl = finish_fully_implicit_template (parser, decl);
23390 /* If the member was not a friend, declare it here. */
23391 if (!friend_p)
23392 finish_member_declaration (decl);
23393 /* Peek at the next token. */
23394 token = cp_lexer_peek_token (parser->lexer);
23395 /* If the next token is a semicolon, consume it. */
23396 if (token->type == CPP_SEMICOLON)
23397 cp_lexer_consume_token (parser->lexer);
23398 goto out;
23400 else
23401 if (declarator->kind == cdk_function)
23402 declarator->id_loc = token->location;
23403 /* Create the declaration. */
23404 decl = grokfield (declarator, &decl_specifiers,
23405 initializer, /*init_const_expr_p=*/true,
23406 asm_specification, attributes);
23407 if (parser->fully_implicit_function_template_p)
23409 if (friend_p)
23410 finish_fully_implicit_template (parser, 0);
23411 else
23412 decl = finish_fully_implicit_template (parser, decl);
23416 cp_finalize_omp_declare_simd (parser, decl);
23417 cp_finalize_oacc_routine (parser, decl, false);
23419 /* Reset PREFIX_ATTRIBUTES. */
23420 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23421 attributes = TREE_CHAIN (attributes);
23422 if (attributes)
23423 TREE_CHAIN (attributes) = NULL_TREE;
23425 /* If there is any qualification still in effect, clear it
23426 now; we will be starting fresh with the next declarator. */
23427 parser->scope = NULL_TREE;
23428 parser->qualifying_scope = NULL_TREE;
23429 parser->object_scope = NULL_TREE;
23430 /* If it's a `,', then there are more declarators. */
23431 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23433 cp_lexer_consume_token (parser->lexer);
23434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23436 cp_token *token = cp_lexer_previous_token (parser->lexer);
23437 error_at (token->location,
23438 "stray %<,%> at end of member declaration");
23441 /* If the next token isn't a `;', then we have a parse error. */
23442 else if (cp_lexer_next_token_is_not (parser->lexer,
23443 CPP_SEMICOLON))
23445 /* The next token might be a ways away from where the
23446 actual semicolon is missing. Find the previous token
23447 and use that for our error position. */
23448 cp_token *token = cp_lexer_previous_token (parser->lexer);
23449 error_at (token->location,
23450 "expected %<;%> at end of member declaration");
23452 /* Assume that the user meant to provide a semicolon. If
23453 we were to cp_parser_skip_to_end_of_statement, we might
23454 skip to a semicolon inside a member function definition
23455 and issue nonsensical error messages. */
23456 assume_semicolon = true;
23459 if (decl)
23461 /* Add DECL to the list of members. */
23462 if (!friend_p
23463 /* Explicitly include, eg, NSDMIs, for better error
23464 recovery (c++/58650). */
23465 || !DECL_DECLARES_FUNCTION_P (decl))
23466 finish_member_declaration (decl);
23468 if (TREE_CODE (decl) == FUNCTION_DECL)
23469 cp_parser_save_default_args (parser, decl);
23470 else if (TREE_CODE (decl) == FIELD_DECL
23471 && !DECL_C_BIT_FIELD (decl)
23472 && DECL_INITIAL (decl))
23473 /* Add DECL to the queue of NSDMI to be parsed later. */
23474 vec_safe_push (unparsed_nsdmis, decl);
23477 if (assume_semicolon)
23478 goto out;
23482 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23483 out:
23484 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23487 /* Parse a pure-specifier.
23489 pure-specifier:
23492 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23493 Otherwise, ERROR_MARK_NODE is returned. */
23495 static tree
23496 cp_parser_pure_specifier (cp_parser* parser)
23498 cp_token *token;
23500 /* Look for the `=' token. */
23501 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23502 return error_mark_node;
23503 /* Look for the `0' token. */
23504 token = cp_lexer_peek_token (parser->lexer);
23506 if (token->type == CPP_EOF
23507 || token->type == CPP_PRAGMA_EOL)
23508 return error_mark_node;
23510 cp_lexer_consume_token (parser->lexer);
23512 /* Accept = default or = delete in c++0x mode. */
23513 if (token->keyword == RID_DEFAULT
23514 || token->keyword == RID_DELETE)
23516 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23517 return token->u.value;
23520 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23521 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23523 cp_parser_error (parser,
23524 "invalid pure specifier (only %<= 0%> is allowed)");
23525 cp_parser_skip_to_end_of_statement (parser);
23526 return error_mark_node;
23528 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23530 error_at (token->location, "templates may not be %<virtual%>");
23531 return error_mark_node;
23534 return integer_zero_node;
23537 /* Parse a constant-initializer.
23539 constant-initializer:
23540 = constant-expression
23542 Returns a representation of the constant-expression. */
23544 static tree
23545 cp_parser_constant_initializer (cp_parser* parser)
23547 /* Look for the `=' token. */
23548 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23549 return error_mark_node;
23551 /* It is invalid to write:
23553 struct S { static const int i = { 7 }; };
23556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23558 cp_parser_error (parser,
23559 "a brace-enclosed initializer is not allowed here");
23560 /* Consume the opening brace. */
23561 cp_lexer_consume_token (parser->lexer);
23562 /* Skip the initializer. */
23563 cp_parser_skip_to_closing_brace (parser);
23564 /* Look for the trailing `}'. */
23565 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23567 return error_mark_node;
23570 return cp_parser_constant_expression (parser);
23573 /* Derived classes [gram.class.derived] */
23575 /* Parse a base-clause.
23577 base-clause:
23578 : base-specifier-list
23580 base-specifier-list:
23581 base-specifier ... [opt]
23582 base-specifier-list , base-specifier ... [opt]
23584 Returns a TREE_LIST representing the base-classes, in the order in
23585 which they were declared. The representation of each node is as
23586 described by cp_parser_base_specifier.
23588 In the case that no bases are specified, this function will return
23589 NULL_TREE, not ERROR_MARK_NODE. */
23591 static tree
23592 cp_parser_base_clause (cp_parser* parser)
23594 tree bases = NULL_TREE;
23596 /* Look for the `:' that begins the list. */
23597 cp_parser_require (parser, CPP_COLON, RT_COLON);
23599 /* Scan the base-specifier-list. */
23600 while (true)
23602 cp_token *token;
23603 tree base;
23604 bool pack_expansion_p = false;
23606 /* Look for the base-specifier. */
23607 base = cp_parser_base_specifier (parser);
23608 /* Look for the (optional) ellipsis. */
23609 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23611 /* Consume the `...'. */
23612 cp_lexer_consume_token (parser->lexer);
23614 pack_expansion_p = true;
23617 /* Add BASE to the front of the list. */
23618 if (base && base != error_mark_node)
23620 if (pack_expansion_p)
23621 /* Make this a pack expansion type. */
23622 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23624 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23626 TREE_CHAIN (base) = bases;
23627 bases = base;
23630 /* Peek at the next token. */
23631 token = cp_lexer_peek_token (parser->lexer);
23632 /* If it's not a comma, then the list is complete. */
23633 if (token->type != CPP_COMMA)
23634 break;
23635 /* Consume the `,'. */
23636 cp_lexer_consume_token (parser->lexer);
23639 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23640 base class had a qualified name. However, the next name that
23641 appears is certainly not qualified. */
23642 parser->scope = NULL_TREE;
23643 parser->qualifying_scope = NULL_TREE;
23644 parser->object_scope = NULL_TREE;
23646 return nreverse (bases);
23649 /* Parse a base-specifier.
23651 base-specifier:
23652 :: [opt] nested-name-specifier [opt] class-name
23653 virtual access-specifier [opt] :: [opt] nested-name-specifier
23654 [opt] class-name
23655 access-specifier virtual [opt] :: [opt] nested-name-specifier
23656 [opt] class-name
23658 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23659 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23660 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23661 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23663 static tree
23664 cp_parser_base_specifier (cp_parser* parser)
23666 cp_token *token;
23667 bool done = false;
23668 bool virtual_p = false;
23669 bool duplicate_virtual_error_issued_p = false;
23670 bool duplicate_access_error_issued_p = false;
23671 bool class_scope_p, template_p;
23672 tree access = access_default_node;
23673 tree type;
23675 /* Process the optional `virtual' and `access-specifier'. */
23676 while (!done)
23678 /* Peek at the next token. */
23679 token = cp_lexer_peek_token (parser->lexer);
23680 /* Process `virtual'. */
23681 switch (token->keyword)
23683 case RID_VIRTUAL:
23684 /* If `virtual' appears more than once, issue an error. */
23685 if (virtual_p && !duplicate_virtual_error_issued_p)
23687 cp_parser_error (parser,
23688 "%<virtual%> specified more than once in base-specified");
23689 duplicate_virtual_error_issued_p = true;
23692 virtual_p = true;
23694 /* Consume the `virtual' token. */
23695 cp_lexer_consume_token (parser->lexer);
23697 break;
23699 case RID_PUBLIC:
23700 case RID_PROTECTED:
23701 case RID_PRIVATE:
23702 /* If more than one access specifier appears, issue an
23703 error. */
23704 if (access != access_default_node
23705 && !duplicate_access_error_issued_p)
23707 cp_parser_error (parser,
23708 "more than one access specifier in base-specified");
23709 duplicate_access_error_issued_p = true;
23712 access = ridpointers[(int) token->keyword];
23714 /* Consume the access-specifier. */
23715 cp_lexer_consume_token (parser->lexer);
23717 break;
23719 default:
23720 done = true;
23721 break;
23724 /* It is not uncommon to see programs mechanically, erroneously, use
23725 the 'typename' keyword to denote (dependent) qualified types
23726 as base classes. */
23727 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23729 token = cp_lexer_peek_token (parser->lexer);
23730 if (!processing_template_decl)
23731 error_at (token->location,
23732 "keyword %<typename%> not allowed outside of templates");
23733 else
23734 error_at (token->location,
23735 "keyword %<typename%> not allowed in this context "
23736 "(the base class is implicitly a type)");
23737 cp_lexer_consume_token (parser->lexer);
23740 /* Look for the optional `::' operator. */
23741 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23742 /* Look for the nested-name-specifier. The simplest way to
23743 implement:
23745 [temp.res]
23747 The keyword `typename' is not permitted in a base-specifier or
23748 mem-initializer; in these contexts a qualified name that
23749 depends on a template-parameter is implicitly assumed to be a
23750 type name.
23752 is to pretend that we have seen the `typename' keyword at this
23753 point. */
23754 cp_parser_nested_name_specifier_opt (parser,
23755 /*typename_keyword_p=*/true,
23756 /*check_dependency_p=*/true,
23757 /*type_p=*/true,
23758 /*is_declaration=*/true);
23759 /* If the base class is given by a qualified name, assume that names
23760 we see are type names or templates, as appropriate. */
23761 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23762 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23764 if (!parser->scope
23765 && cp_lexer_next_token_is_decltype (parser->lexer))
23766 /* DR 950 allows decltype as a base-specifier. */
23767 type = cp_parser_decltype (parser);
23768 else
23770 /* Otherwise, look for the class-name. */
23771 type = cp_parser_class_name (parser,
23772 class_scope_p,
23773 template_p,
23774 typename_type,
23775 /*check_dependency_p=*/true,
23776 /*class_head_p=*/false,
23777 /*is_declaration=*/true);
23778 type = TREE_TYPE (type);
23781 if (type == error_mark_node)
23782 return error_mark_node;
23784 return finish_base_specifier (type, access, virtual_p);
23787 /* Exception handling [gram.exception] */
23789 /* Parse an (optional) noexcept-specification.
23791 noexcept-specification:
23792 noexcept ( constant-expression ) [opt]
23794 If no noexcept-specification is present, returns NULL_TREE.
23795 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23796 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23797 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23798 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23799 in which case a boolean condition is returned instead. */
23801 static tree
23802 cp_parser_noexcept_specification_opt (cp_parser* parser,
23803 bool require_constexpr,
23804 bool* consumed_expr,
23805 bool return_cond)
23807 cp_token *token;
23808 const char *saved_message;
23810 /* Peek at the next token. */
23811 token = cp_lexer_peek_token (parser->lexer);
23813 /* Is it a noexcept-specification? */
23814 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23816 tree expr;
23817 cp_lexer_consume_token (parser->lexer);
23819 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23821 cp_lexer_consume_token (parser->lexer);
23823 if (require_constexpr)
23825 /* Types may not be defined in an exception-specification. */
23826 saved_message = parser->type_definition_forbidden_message;
23827 parser->type_definition_forbidden_message
23828 = G_("types may not be defined in an exception-specification");
23830 expr = cp_parser_constant_expression (parser);
23832 /* Restore the saved message. */
23833 parser->type_definition_forbidden_message = saved_message;
23835 else
23837 expr = cp_parser_expression (parser);
23838 *consumed_expr = true;
23841 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23843 else
23845 expr = boolean_true_node;
23846 if (!require_constexpr)
23847 *consumed_expr = false;
23850 /* We cannot build a noexcept-spec right away because this will check
23851 that expr is a constexpr. */
23852 if (!return_cond)
23853 return build_noexcept_spec (expr, tf_warning_or_error);
23854 else
23855 return expr;
23857 else
23858 return NULL_TREE;
23861 /* Parse an (optional) exception-specification.
23863 exception-specification:
23864 throw ( type-id-list [opt] )
23866 Returns a TREE_LIST representing the exception-specification. The
23867 TREE_VALUE of each node is a type. */
23869 static tree
23870 cp_parser_exception_specification_opt (cp_parser* parser)
23872 cp_token *token;
23873 tree type_id_list;
23874 const char *saved_message;
23876 /* Peek at the next token. */
23877 token = cp_lexer_peek_token (parser->lexer);
23879 /* Is it a noexcept-specification? */
23880 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
23881 false);
23882 if (type_id_list != NULL_TREE)
23883 return type_id_list;
23885 /* If it's not `throw', then there's no exception-specification. */
23886 if (!cp_parser_is_keyword (token, RID_THROW))
23887 return NULL_TREE;
23889 location_t loc = token->location;
23891 /* Consume the `throw'. */
23892 cp_lexer_consume_token (parser->lexer);
23894 /* Look for the `('. */
23895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23897 /* Peek at the next token. */
23898 token = cp_lexer_peek_token (parser->lexer);
23899 /* If it's not a `)', then there is a type-id-list. */
23900 if (token->type != CPP_CLOSE_PAREN)
23902 /* Types may not be defined in an exception-specification. */
23903 saved_message = parser->type_definition_forbidden_message;
23904 parser->type_definition_forbidden_message
23905 = G_("types may not be defined in an exception-specification");
23906 /* Parse the type-id-list. */
23907 type_id_list = cp_parser_type_id_list (parser);
23908 /* Restore the saved message. */
23909 parser->type_definition_forbidden_message = saved_message;
23911 if (cxx_dialect >= cxx1z)
23913 error_at (loc, "ISO C++1z does not allow dynamic exception "
23914 "specifications");
23915 type_id_list = NULL_TREE;
23917 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
23918 warning_at (loc, OPT_Wdeprecated,
23919 "dynamic exception specifications are deprecated in C++11;"
23920 " use %<noexcept%> instead");
23922 /* In C++17, throw() is equivalent to noexcept (true). throw()
23923 is deprecated in C++11 and above as well, but is still widely used,
23924 so don't warn about it yet. */
23925 else if (cxx_dialect >= cxx1z)
23926 type_id_list = noexcept_true_spec;
23927 else
23928 type_id_list = empty_except_spec;
23930 /* Look for the `)'. */
23931 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23933 return type_id_list;
23936 /* Parse an (optional) type-id-list.
23938 type-id-list:
23939 type-id ... [opt]
23940 type-id-list , type-id ... [opt]
23942 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
23943 in the order that the types were presented. */
23945 static tree
23946 cp_parser_type_id_list (cp_parser* parser)
23948 tree types = NULL_TREE;
23950 while (true)
23952 cp_token *token;
23953 tree type;
23955 token = cp_lexer_peek_token (parser->lexer);
23957 /* Get the next type-id. */
23958 type = cp_parser_type_id (parser);
23959 /* Check for invalid 'auto'. */
23960 if (flag_concepts && type_uses_auto (type))
23962 error_at (token->location,
23963 "invalid use of %<auto%> in exception-specification");
23964 type = error_mark_node;
23966 /* Parse the optional ellipsis. */
23967 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23969 /* Consume the `...'. */
23970 cp_lexer_consume_token (parser->lexer);
23972 /* Turn the type into a pack expansion expression. */
23973 type = make_pack_expansion (type);
23975 /* Add it to the list. */
23976 types = add_exception_specifier (types, type, /*complain=*/1);
23977 /* Peek at the next token. */
23978 token = cp_lexer_peek_token (parser->lexer);
23979 /* If it is not a `,', we are done. */
23980 if (token->type != CPP_COMMA)
23981 break;
23982 /* Consume the `,'. */
23983 cp_lexer_consume_token (parser->lexer);
23986 return nreverse (types);
23989 /* Parse a try-block.
23991 try-block:
23992 try compound-statement handler-seq */
23994 static tree
23995 cp_parser_try_block (cp_parser* parser)
23997 tree try_block;
23999 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24000 if (parser->in_function_body
24001 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24002 error ("%<try%> in %<constexpr%> function");
24004 try_block = begin_try_block ();
24005 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24006 finish_try_block (try_block);
24007 cp_parser_handler_seq (parser);
24008 finish_handler_sequence (try_block);
24010 return try_block;
24013 /* Parse a function-try-block.
24015 function-try-block:
24016 try ctor-initializer [opt] function-body handler-seq */
24018 static bool
24019 cp_parser_function_try_block (cp_parser* parser)
24021 tree compound_stmt;
24022 tree try_block;
24023 bool ctor_initializer_p;
24025 /* Look for the `try' keyword. */
24026 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24027 return false;
24028 /* Let the rest of the front end know where we are. */
24029 try_block = begin_function_try_block (&compound_stmt);
24030 /* Parse the function-body. */
24031 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24032 (parser, /*in_function_try_block=*/true);
24033 /* We're done with the `try' part. */
24034 finish_function_try_block (try_block);
24035 /* Parse the handlers. */
24036 cp_parser_handler_seq (parser);
24037 /* We're done with the handlers. */
24038 finish_function_handler_sequence (try_block, compound_stmt);
24040 return ctor_initializer_p;
24043 /* Parse a handler-seq.
24045 handler-seq:
24046 handler handler-seq [opt] */
24048 static void
24049 cp_parser_handler_seq (cp_parser* parser)
24051 while (true)
24053 cp_token *token;
24055 /* Parse the handler. */
24056 cp_parser_handler (parser);
24057 /* Peek at the next token. */
24058 token = cp_lexer_peek_token (parser->lexer);
24059 /* If it's not `catch' then there are no more handlers. */
24060 if (!cp_parser_is_keyword (token, RID_CATCH))
24061 break;
24065 /* Parse a handler.
24067 handler:
24068 catch ( exception-declaration ) compound-statement */
24070 static void
24071 cp_parser_handler (cp_parser* parser)
24073 tree handler;
24074 tree declaration;
24076 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24077 handler = begin_handler ();
24078 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24079 declaration = cp_parser_exception_declaration (parser);
24080 finish_handler_parms (declaration, handler);
24081 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24082 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24083 finish_handler (handler);
24086 /* Parse an exception-declaration.
24088 exception-declaration:
24089 type-specifier-seq declarator
24090 type-specifier-seq abstract-declarator
24091 type-specifier-seq
24094 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24095 ellipsis variant is used. */
24097 static tree
24098 cp_parser_exception_declaration (cp_parser* parser)
24100 cp_decl_specifier_seq type_specifiers;
24101 cp_declarator *declarator;
24102 const char *saved_message;
24104 /* If it's an ellipsis, it's easy to handle. */
24105 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24107 /* Consume the `...' token. */
24108 cp_lexer_consume_token (parser->lexer);
24109 return NULL_TREE;
24112 /* Types may not be defined in exception-declarations. */
24113 saved_message = parser->type_definition_forbidden_message;
24114 parser->type_definition_forbidden_message
24115 = G_("types may not be defined in exception-declarations");
24117 /* Parse the type-specifier-seq. */
24118 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24119 /*is_trailing_return=*/false,
24120 &type_specifiers);
24121 /* If it's a `)', then there is no declarator. */
24122 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24123 declarator = NULL;
24124 else
24125 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24126 /*ctor_dtor_or_conv_p=*/NULL,
24127 /*parenthesized_p=*/NULL,
24128 /*member_p=*/false,
24129 /*friend_p=*/false);
24131 /* Restore the saved message. */
24132 parser->type_definition_forbidden_message = saved_message;
24134 if (!type_specifiers.any_specifiers_p)
24135 return error_mark_node;
24137 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24140 /* Parse a throw-expression.
24142 throw-expression:
24143 throw assignment-expression [opt]
24145 Returns a THROW_EXPR representing the throw-expression. */
24147 static tree
24148 cp_parser_throw_expression (cp_parser* parser)
24150 tree expression;
24151 cp_token* token;
24153 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24154 token = cp_lexer_peek_token (parser->lexer);
24155 /* Figure out whether or not there is an assignment-expression
24156 following the "throw" keyword. */
24157 if (token->type == CPP_COMMA
24158 || token->type == CPP_SEMICOLON
24159 || token->type == CPP_CLOSE_PAREN
24160 || token->type == CPP_CLOSE_SQUARE
24161 || token->type == CPP_CLOSE_BRACE
24162 || token->type == CPP_COLON)
24163 expression = NULL_TREE;
24164 else
24165 expression = cp_parser_assignment_expression (parser);
24167 return build_throw (expression);
24170 /* GNU Extensions */
24172 /* Parse an (optional) asm-specification.
24174 asm-specification:
24175 asm ( string-literal )
24177 If the asm-specification is present, returns a STRING_CST
24178 corresponding to the string-literal. Otherwise, returns
24179 NULL_TREE. */
24181 static tree
24182 cp_parser_asm_specification_opt (cp_parser* parser)
24184 cp_token *token;
24185 tree asm_specification;
24187 /* Peek at the next token. */
24188 token = cp_lexer_peek_token (parser->lexer);
24189 /* If the next token isn't the `asm' keyword, then there's no
24190 asm-specification. */
24191 if (!cp_parser_is_keyword (token, RID_ASM))
24192 return NULL_TREE;
24194 /* Consume the `asm' token. */
24195 cp_lexer_consume_token (parser->lexer);
24196 /* Look for the `('. */
24197 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24199 /* Look for the string-literal. */
24200 asm_specification = cp_parser_string_literal (parser, false, false);
24202 /* Look for the `)'. */
24203 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24205 return asm_specification;
24208 /* Parse an asm-operand-list.
24210 asm-operand-list:
24211 asm-operand
24212 asm-operand-list , asm-operand
24214 asm-operand:
24215 string-literal ( expression )
24216 [ string-literal ] string-literal ( expression )
24218 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24219 each node is the expression. The TREE_PURPOSE is itself a
24220 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24221 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24222 is a STRING_CST for the string literal before the parenthesis. Returns
24223 ERROR_MARK_NODE if any of the operands are invalid. */
24225 static tree
24226 cp_parser_asm_operand_list (cp_parser* parser)
24228 tree asm_operands = NULL_TREE;
24229 bool invalid_operands = false;
24231 while (true)
24233 tree string_literal;
24234 tree expression;
24235 tree name;
24237 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24239 /* Consume the `[' token. */
24240 cp_lexer_consume_token (parser->lexer);
24241 /* Read the operand name. */
24242 name = cp_parser_identifier (parser);
24243 if (name != error_mark_node)
24244 name = build_string (IDENTIFIER_LENGTH (name),
24245 IDENTIFIER_POINTER (name));
24246 /* Look for the closing `]'. */
24247 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24249 else
24250 name = NULL_TREE;
24251 /* Look for the string-literal. */
24252 string_literal = cp_parser_string_literal (parser, false, false);
24254 /* Look for the `('. */
24255 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24256 /* Parse the expression. */
24257 expression = cp_parser_expression (parser);
24258 /* Look for the `)'. */
24259 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24261 if (name == error_mark_node
24262 || string_literal == error_mark_node
24263 || expression == error_mark_node)
24264 invalid_operands = true;
24266 /* Add this operand to the list. */
24267 asm_operands = tree_cons (build_tree_list (name, string_literal),
24268 expression,
24269 asm_operands);
24270 /* If the next token is not a `,', there are no more
24271 operands. */
24272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24273 break;
24274 /* Consume the `,'. */
24275 cp_lexer_consume_token (parser->lexer);
24278 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24281 /* Parse an asm-clobber-list.
24283 asm-clobber-list:
24284 string-literal
24285 asm-clobber-list , string-literal
24287 Returns a TREE_LIST, indicating the clobbers in the order that they
24288 appeared. The TREE_VALUE of each node is a STRING_CST. */
24290 static tree
24291 cp_parser_asm_clobber_list (cp_parser* parser)
24293 tree clobbers = NULL_TREE;
24295 while (true)
24297 tree string_literal;
24299 /* Look for the string literal. */
24300 string_literal = cp_parser_string_literal (parser, false, false);
24301 /* Add it to the list. */
24302 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24303 /* If the next token is not a `,', then the list is
24304 complete. */
24305 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24306 break;
24307 /* Consume the `,' token. */
24308 cp_lexer_consume_token (parser->lexer);
24311 return clobbers;
24314 /* Parse an asm-label-list.
24316 asm-label-list:
24317 identifier
24318 asm-label-list , identifier
24320 Returns a TREE_LIST, indicating the labels in the order that they
24321 appeared. The TREE_VALUE of each node is a label. */
24323 static tree
24324 cp_parser_asm_label_list (cp_parser* parser)
24326 tree labels = NULL_TREE;
24328 while (true)
24330 tree identifier, label, name;
24332 /* Look for the identifier. */
24333 identifier = cp_parser_identifier (parser);
24334 if (!error_operand_p (identifier))
24336 label = lookup_label (identifier);
24337 if (TREE_CODE (label) == LABEL_DECL)
24339 TREE_USED (label) = 1;
24340 check_goto (label);
24341 name = build_string (IDENTIFIER_LENGTH (identifier),
24342 IDENTIFIER_POINTER (identifier));
24343 labels = tree_cons (name, label, labels);
24346 /* If the next token is not a `,', then the list is
24347 complete. */
24348 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24349 break;
24350 /* Consume the `,' token. */
24351 cp_lexer_consume_token (parser->lexer);
24354 return nreverse (labels);
24357 /* Return TRUE iff the next tokens in the stream are possibly the
24358 beginning of a GNU extension attribute. */
24360 static bool
24361 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24363 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24366 /* Return TRUE iff the next tokens in the stream are possibly the
24367 beginning of a standard C++-11 attribute specifier. */
24369 static bool
24370 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24372 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24375 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24376 beginning of a standard C++-11 attribute specifier. */
24378 static bool
24379 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24381 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24383 return (cxx_dialect >= cxx11
24384 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24385 || (token->type == CPP_OPEN_SQUARE
24386 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24387 && token->type == CPP_OPEN_SQUARE)));
24390 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24391 beginning of a GNU extension attribute. */
24393 static bool
24394 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24396 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24398 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24401 /* Return true iff the next tokens can be the beginning of either a
24402 GNU attribute list, or a standard C++11 attribute sequence. */
24404 static bool
24405 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24407 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24408 || cp_next_tokens_can_be_std_attribute_p (parser));
24411 /* Return true iff the next Nth tokens can be the beginning of either
24412 a GNU attribute list, or a standard C++11 attribute sequence. */
24414 static bool
24415 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24417 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24418 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24421 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24422 of GNU attributes, or return NULL. */
24424 static tree
24425 cp_parser_attributes_opt (cp_parser *parser)
24427 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24428 return cp_parser_gnu_attributes_opt (parser);
24429 return cp_parser_std_attribute_spec_seq (parser);
24432 #define CILK_SIMD_FN_CLAUSE_MASK \
24433 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24434 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24435 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24436 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24437 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24439 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24440 vector [(<clauses>)] */
24442 static void
24443 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24445 bool first_p = parser->cilk_simd_fn_info == NULL;
24446 cp_token *token = v_token;
24447 if (first_p)
24449 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24450 parser->cilk_simd_fn_info->error_seen = false;
24451 parser->cilk_simd_fn_info->fndecl_seen = false;
24452 parser->cilk_simd_fn_info->tokens = vNULL;
24453 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24455 int paren_scope = 0;
24456 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24458 cp_lexer_consume_token (parser->lexer);
24459 v_token = cp_lexer_peek_token (parser->lexer);
24460 paren_scope++;
24462 while (paren_scope > 0)
24464 token = cp_lexer_peek_token (parser->lexer);
24465 if (token->type == CPP_OPEN_PAREN)
24466 paren_scope++;
24467 else if (token->type == CPP_CLOSE_PAREN)
24468 paren_scope--;
24469 /* Do not push the last ')' */
24470 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24471 cp_lexer_consume_token (parser->lexer);
24474 token->type = CPP_PRAGMA_EOL;
24475 parser->lexer->next_token = token;
24476 cp_lexer_consume_token (parser->lexer);
24478 struct cp_token_cache *cp
24479 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24480 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24483 /* Parse an (optional) series of attributes.
24485 attributes:
24486 attributes attribute
24488 attribute:
24489 __attribute__ (( attribute-list [opt] ))
24491 The return value is as for cp_parser_gnu_attribute_list. */
24493 static tree
24494 cp_parser_gnu_attributes_opt (cp_parser* parser)
24496 tree attributes = NULL_TREE;
24498 while (true)
24500 cp_token *token;
24501 tree attribute_list;
24502 bool ok = true;
24504 /* Peek at the next token. */
24505 token = cp_lexer_peek_token (parser->lexer);
24506 /* If it's not `__attribute__', then we're done. */
24507 if (token->keyword != RID_ATTRIBUTE)
24508 break;
24510 /* Consume the `__attribute__' keyword. */
24511 cp_lexer_consume_token (parser->lexer);
24512 /* Look for the two `(' tokens. */
24513 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24514 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24516 /* Peek at the next token. */
24517 token = cp_lexer_peek_token (parser->lexer);
24518 if (token->type != CPP_CLOSE_PAREN)
24519 /* Parse the attribute-list. */
24520 attribute_list = cp_parser_gnu_attribute_list (parser);
24521 else
24522 /* If the next token is a `)', then there is no attribute
24523 list. */
24524 attribute_list = NULL;
24526 /* Look for the two `)' tokens. */
24527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24528 ok = false;
24529 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24530 ok = false;
24531 if (!ok)
24532 cp_parser_skip_to_end_of_statement (parser);
24534 /* Add these new attributes to the list. */
24535 attributes = chainon (attributes, attribute_list);
24538 return attributes;
24541 /* Parse a GNU attribute-list.
24543 attribute-list:
24544 attribute
24545 attribute-list , attribute
24547 attribute:
24548 identifier
24549 identifier ( identifier )
24550 identifier ( identifier , expression-list )
24551 identifier ( expression-list )
24553 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24554 to an attribute. The TREE_PURPOSE of each node is the identifier
24555 indicating which attribute is in use. The TREE_VALUE represents
24556 the arguments, if any. */
24558 static tree
24559 cp_parser_gnu_attribute_list (cp_parser* parser)
24561 tree attribute_list = NULL_TREE;
24562 bool save_translate_strings_p = parser->translate_strings_p;
24564 parser->translate_strings_p = false;
24565 while (true)
24567 cp_token *token;
24568 tree identifier;
24569 tree attribute;
24571 /* Look for the identifier. We also allow keywords here; for
24572 example `__attribute__ ((const))' is legal. */
24573 token = cp_lexer_peek_token (parser->lexer);
24574 if (token->type == CPP_NAME
24575 || token->type == CPP_KEYWORD)
24577 tree arguments = NULL_TREE;
24579 /* Consume the token, but save it since we need it for the
24580 SIMD enabled function parsing. */
24581 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24583 /* Save away the identifier that indicates which attribute
24584 this is. */
24585 identifier = (token->type == CPP_KEYWORD)
24586 /* For keywords, use the canonical spelling, not the
24587 parsed identifier. */
24588 ? ridpointers[(int) token->keyword]
24589 : id_token->u.value;
24591 attribute = build_tree_list (identifier, NULL_TREE);
24593 /* Peek at the next token. */
24594 token = cp_lexer_peek_token (parser->lexer);
24595 /* If it's an `(', then parse the attribute arguments. */
24596 if (token->type == CPP_OPEN_PAREN)
24598 vec<tree, va_gc> *vec;
24599 int attr_flag = (attribute_takes_identifier_p (identifier)
24600 ? id_attr : normal_attr);
24601 if (is_cilkplus_vector_p (identifier))
24603 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24604 continue;
24606 else
24607 vec = cp_parser_parenthesized_expression_list
24608 (parser, attr_flag, /*cast_p=*/false,
24609 /*allow_expansion_p=*/false,
24610 /*non_constant_p=*/NULL);
24611 if (vec == NULL)
24612 arguments = error_mark_node;
24613 else
24615 arguments = build_tree_list_vec (vec);
24616 release_tree_vector (vec);
24618 /* Save the arguments away. */
24619 TREE_VALUE (attribute) = arguments;
24621 else if (is_cilkplus_vector_p (identifier))
24623 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24624 continue;
24627 if (arguments != error_mark_node)
24629 /* Add this attribute to the list. */
24630 TREE_CHAIN (attribute) = attribute_list;
24631 attribute_list = attribute;
24634 token = cp_lexer_peek_token (parser->lexer);
24636 /* Now, look for more attributes. If the next token isn't a
24637 `,', we're done. */
24638 if (token->type != CPP_COMMA)
24639 break;
24641 /* Consume the comma and keep going. */
24642 cp_lexer_consume_token (parser->lexer);
24644 parser->translate_strings_p = save_translate_strings_p;
24646 /* We built up the list in reverse order. */
24647 return nreverse (attribute_list);
24650 /* Parse a standard C++11 attribute.
24652 The returned representation is a TREE_LIST which TREE_PURPOSE is
24653 the scoped name of the attribute, and the TREE_VALUE is its
24654 arguments list.
24656 Note that the scoped name of the attribute is itself a TREE_LIST
24657 which TREE_PURPOSE is the namespace of the attribute, and
24658 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24659 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24660 and which TREE_PURPOSE is directly the attribute name.
24662 Clients of the attribute code should use get_attribute_namespace
24663 and get_attribute_name to get the actual namespace and name of
24664 attributes, regardless of their being GNU or C++11 attributes.
24666 attribute:
24667 attribute-token attribute-argument-clause [opt]
24669 attribute-token:
24670 identifier
24671 attribute-scoped-token
24673 attribute-scoped-token:
24674 attribute-namespace :: identifier
24676 attribute-namespace:
24677 identifier
24679 attribute-argument-clause:
24680 ( balanced-token-seq )
24682 balanced-token-seq:
24683 balanced-token [opt]
24684 balanced-token-seq balanced-token
24686 balanced-token:
24687 ( balanced-token-seq )
24688 [ balanced-token-seq ]
24689 { balanced-token-seq }. */
24691 static tree
24692 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24694 tree attribute, attr_id = NULL_TREE, arguments;
24695 cp_token *token;
24697 /* First, parse name of the attribute, a.k.a attribute-token. */
24699 token = cp_lexer_peek_token (parser->lexer);
24700 if (token->type == CPP_NAME)
24701 attr_id = token->u.value;
24702 else if (token->type == CPP_KEYWORD)
24703 attr_id = ridpointers[(int) token->keyword];
24704 else if (token->flags & NAMED_OP)
24705 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24707 if (attr_id == NULL_TREE)
24708 return NULL_TREE;
24710 cp_lexer_consume_token (parser->lexer);
24712 token = cp_lexer_peek_token (parser->lexer);
24713 if (token->type == CPP_SCOPE)
24715 /* We are seeing a scoped attribute token. */
24717 cp_lexer_consume_token (parser->lexer);
24718 if (attr_ns)
24719 error_at (token->location, "attribute using prefix used together "
24720 "with scoped attribute token");
24721 attr_ns = attr_id;
24723 token = cp_lexer_consume_token (parser->lexer);
24724 if (token->type == CPP_NAME)
24725 attr_id = token->u.value;
24726 else if (token->type == CPP_KEYWORD)
24727 attr_id = ridpointers[(int) token->keyword];
24728 else if (token->flags & NAMED_OP)
24729 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24730 else
24732 error_at (token->location,
24733 "expected an identifier for the attribute name");
24734 return error_mark_node;
24736 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24737 NULL_TREE);
24738 token = cp_lexer_peek_token (parser->lexer);
24740 else if (attr_ns)
24741 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24742 NULL_TREE);
24743 else
24745 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24746 NULL_TREE);
24747 /* C++11 noreturn attribute is equivalent to GNU's. */
24748 if (is_attribute_p ("noreturn", attr_id))
24749 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24750 /* C++14 deprecated attribute is equivalent to GNU's. */
24751 else if (is_attribute_p ("deprecated", attr_id))
24752 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24753 /* C++17 fallthrough attribute is equivalent to GNU's. */
24754 else if (is_attribute_p ("fallthrough", attr_id))
24755 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24756 /* Transactional Memory TS optimize_for_synchronized attribute is
24757 equivalent to GNU transaction_callable. */
24758 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24759 TREE_PURPOSE (attribute)
24760 = get_identifier ("transaction_callable");
24761 /* Transactional Memory attributes are GNU attributes. */
24762 else if (tm_attr_to_mask (attr_id))
24763 TREE_PURPOSE (attribute) = attr_id;
24766 /* Now parse the optional argument clause of the attribute. */
24768 if (token->type != CPP_OPEN_PAREN)
24769 return attribute;
24772 vec<tree, va_gc> *vec;
24773 int attr_flag = normal_attr;
24775 if (attr_ns == get_identifier ("gnu")
24776 && attribute_takes_identifier_p (attr_id))
24777 /* A GNU attribute that takes an identifier in parameter. */
24778 attr_flag = id_attr;
24780 vec = cp_parser_parenthesized_expression_list
24781 (parser, attr_flag, /*cast_p=*/false,
24782 /*allow_expansion_p=*/true,
24783 /*non_constant_p=*/NULL);
24784 if (vec == NULL)
24785 arguments = error_mark_node;
24786 else
24788 arguments = build_tree_list_vec (vec);
24789 release_tree_vector (vec);
24792 if (arguments == error_mark_node)
24793 attribute = error_mark_node;
24794 else
24795 TREE_VALUE (attribute) = arguments;
24798 return attribute;
24801 /* Check that the attribute ATTRIBUTE appears at most once in the
24802 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24803 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24804 isn't implemented yet in GCC. */
24806 static void
24807 cp_parser_check_std_attribute (tree attributes, tree attribute)
24809 if (attributes)
24811 tree name = get_attribute_name (attribute);
24812 if (is_attribute_p ("noreturn", name)
24813 && lookup_attribute ("noreturn", attributes))
24814 error ("attribute %<noreturn%> can appear at most once "
24815 "in an attribute-list");
24816 else if (is_attribute_p ("deprecated", name)
24817 && lookup_attribute ("deprecated", attributes))
24818 error ("attribute %<deprecated%> can appear at most once "
24819 "in an attribute-list");
24823 /* Parse a list of standard C++-11 attributes.
24825 attribute-list:
24826 attribute [opt]
24827 attribute-list , attribute[opt]
24828 attribute ...
24829 attribute-list , attribute ...
24832 static tree
24833 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24835 tree attributes = NULL_TREE, attribute = NULL_TREE;
24836 cp_token *token = NULL;
24838 while (true)
24840 attribute = cp_parser_std_attribute (parser, attr_ns);
24841 if (attribute == error_mark_node)
24842 break;
24843 if (attribute != NULL_TREE)
24845 cp_parser_check_std_attribute (attributes, attribute);
24846 TREE_CHAIN (attribute) = attributes;
24847 attributes = attribute;
24849 token = cp_lexer_peek_token (parser->lexer);
24850 if (token->type == CPP_ELLIPSIS)
24852 cp_lexer_consume_token (parser->lexer);
24853 if (attribute == NULL_TREE)
24854 error_at (token->location,
24855 "expected attribute before %<...%>");
24856 else
24857 TREE_VALUE (attribute)
24858 = make_pack_expansion (TREE_VALUE (attribute));
24859 token = cp_lexer_peek_token (parser->lexer);
24861 if (token->type != CPP_COMMA)
24862 break;
24863 cp_lexer_consume_token (parser->lexer);
24865 attributes = nreverse (attributes);
24866 return attributes;
24869 /* Parse a standard C++-11 attribute specifier.
24871 attribute-specifier:
24872 [ [ attribute-using-prefix [opt] attribute-list ] ]
24873 alignment-specifier
24875 attribute-using-prefix:
24876 using attribute-namespace :
24878 alignment-specifier:
24879 alignas ( type-id ... [opt] )
24880 alignas ( alignment-expression ... [opt] ). */
24882 static tree
24883 cp_parser_std_attribute_spec (cp_parser *parser)
24885 tree attributes = NULL_TREE;
24886 cp_token *token = cp_lexer_peek_token (parser->lexer);
24888 if (token->type == CPP_OPEN_SQUARE
24889 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
24891 tree attr_ns = NULL_TREE;
24893 cp_lexer_consume_token (parser->lexer);
24894 cp_lexer_consume_token (parser->lexer);
24896 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
24898 token = cp_lexer_peek_nth_token (parser->lexer, 2);
24899 if (token->type == CPP_NAME)
24900 attr_ns = token->u.value;
24901 else if (token->type == CPP_KEYWORD)
24902 attr_ns = ridpointers[(int) token->keyword];
24903 else if (token->flags & NAMED_OP)
24904 attr_ns = get_identifier (cpp_type2name (token->type,
24905 token->flags));
24906 if (attr_ns
24907 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
24909 if (cxx_dialect < cxx1z
24910 && !in_system_header_at (input_location))
24911 pedwarn (input_location, 0,
24912 "attribute using prefix only available "
24913 "with -std=c++1z or -std=gnu++1z");
24915 cp_lexer_consume_token (parser->lexer);
24916 cp_lexer_consume_token (parser->lexer);
24917 cp_lexer_consume_token (parser->lexer);
24919 else
24920 attr_ns = NULL_TREE;
24923 attributes = cp_parser_std_attribute_list (parser, attr_ns);
24925 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
24926 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
24927 cp_parser_skip_to_end_of_statement (parser);
24928 else
24929 /* Warn about parsing c++11 attribute in non-c++1 mode, only
24930 when we are sure that we have actually parsed them. */
24931 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24933 else
24935 tree alignas_expr;
24937 /* Look for an alignment-specifier. */
24939 token = cp_lexer_peek_token (parser->lexer);
24941 if (token->type != CPP_KEYWORD
24942 || token->keyword != RID_ALIGNAS)
24943 return NULL_TREE;
24945 cp_lexer_consume_token (parser->lexer);
24946 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
24948 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
24950 cp_parser_error (parser, "expected %<(%>");
24951 return error_mark_node;
24954 cp_parser_parse_tentatively (parser);
24955 alignas_expr = cp_parser_type_id (parser);
24957 if (!cp_parser_parse_definitely (parser))
24959 alignas_expr = cp_parser_assignment_expression (parser);
24960 if (alignas_expr == error_mark_node)
24961 cp_parser_skip_to_end_of_statement (parser);
24962 if (alignas_expr == NULL_TREE
24963 || alignas_expr == error_mark_node)
24964 return alignas_expr;
24967 alignas_expr = cxx_alignas_expr (alignas_expr);
24968 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
24970 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24972 cp_lexer_consume_token (parser->lexer);
24973 alignas_expr = make_pack_expansion (alignas_expr);
24976 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
24978 cp_parser_error (parser, "expected %<)%>");
24979 return error_mark_node;
24982 /* Build the C++-11 representation of an 'aligned'
24983 attribute. */
24984 attributes =
24985 build_tree_list (build_tree_list (get_identifier ("gnu"),
24986 get_identifier ("aligned")),
24987 alignas_expr);
24990 return attributes;
24993 /* Parse a standard C++-11 attribute-specifier-seq.
24995 attribute-specifier-seq:
24996 attribute-specifier-seq [opt] attribute-specifier
24999 static tree
25000 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25002 tree attr_specs = NULL_TREE;
25003 tree attr_last = NULL_TREE;
25005 while (true)
25007 tree attr_spec = cp_parser_std_attribute_spec (parser);
25008 if (attr_spec == NULL_TREE)
25009 break;
25010 if (attr_spec == error_mark_node)
25011 return error_mark_node;
25013 if (attr_last)
25014 TREE_CHAIN (attr_last) = attr_spec;
25015 else
25016 attr_specs = attr_last = attr_spec;
25017 attr_last = tree_last (attr_last);
25020 return attr_specs;
25023 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25024 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25025 current value of the PEDANTIC flag, regardless of whether or not
25026 the `__extension__' keyword is present. The caller is responsible
25027 for restoring the value of the PEDANTIC flag. */
25029 static bool
25030 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25032 /* Save the old value of the PEDANTIC flag. */
25033 *saved_pedantic = pedantic;
25035 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25037 /* Consume the `__extension__' token. */
25038 cp_lexer_consume_token (parser->lexer);
25039 /* We're not being pedantic while the `__extension__' keyword is
25040 in effect. */
25041 pedantic = 0;
25043 return true;
25046 return false;
25049 /* Parse a label declaration.
25051 label-declaration:
25052 __label__ label-declarator-seq ;
25054 label-declarator-seq:
25055 identifier , label-declarator-seq
25056 identifier */
25058 static void
25059 cp_parser_label_declaration (cp_parser* parser)
25061 /* Look for the `__label__' keyword. */
25062 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25064 while (true)
25066 tree identifier;
25068 /* Look for an identifier. */
25069 identifier = cp_parser_identifier (parser);
25070 /* If we failed, stop. */
25071 if (identifier == error_mark_node)
25072 break;
25073 /* Declare it as a label. */
25074 finish_label_decl (identifier);
25075 /* If the next token is a `;', stop. */
25076 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25077 break;
25078 /* Look for the `,' separating the label declarations. */
25079 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25082 /* Look for the final `;'. */
25083 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25086 // -------------------------------------------------------------------------- //
25087 // Requires Clause
25089 // Parse a requires clause.
25091 // requires-clause:
25092 // 'requires' logical-or-expression
25094 // The required logical-or-expression must be a constant expression. Note
25095 // that we don't check that the expression is constepxr here. We defer until
25096 // we analyze constraints and then, we only check atomic constraints.
25097 static tree
25098 cp_parser_requires_clause (cp_parser *parser)
25100 // Parse the requires clause so that it is not automatically folded.
25101 ++processing_template_decl;
25102 tree expr = cp_parser_binary_expression (parser, false, false,
25103 PREC_NOT_OPERATOR, NULL);
25104 if (check_for_bare_parameter_packs (expr))
25105 expr = error_mark_node;
25106 --processing_template_decl;
25107 return expr;
25110 // Optionally parse a requires clause:
25111 static tree
25112 cp_parser_requires_clause_opt (cp_parser *parser)
25114 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25115 if (tok->keyword != RID_REQUIRES)
25117 if (!flag_concepts && tok->type == CPP_NAME
25118 && tok->u.value == ridpointers[RID_REQUIRES])
25120 error_at (cp_lexer_peek_token (parser->lexer)->location,
25121 "%<requires%> only available with -fconcepts");
25122 /* Parse and discard the requires-clause. */
25123 cp_lexer_consume_token (parser->lexer);
25124 cp_parser_requires_clause (parser);
25126 return NULL_TREE;
25128 cp_lexer_consume_token (parser->lexer);
25129 return cp_parser_requires_clause (parser);
25133 /*---------------------------------------------------------------------------
25134 Requires expressions
25135 ---------------------------------------------------------------------------*/
25137 /* Parse a requires expression
25139 requirement-expression:
25140 'requires' requirement-parameter-list [opt] requirement-body */
25141 static tree
25142 cp_parser_requires_expression (cp_parser *parser)
25144 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25145 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25147 /* A requires-expression shall appear only within a concept
25148 definition or a requires-clause.
25150 TODO: Implement this diagnostic correctly. */
25151 if (!processing_template_decl)
25153 error_at (loc, "a requires expression cannot appear outside a template");
25154 cp_parser_skip_to_end_of_statement (parser);
25155 return error_mark_node;
25158 tree parms, reqs;
25160 /* Local parameters are delared as variables within the scope
25161 of the expression. They are not visible past the end of
25162 the expression. Expressions within the requires-expression
25163 are unevaluated. */
25164 struct scope_sentinel
25166 scope_sentinel ()
25168 ++cp_unevaluated_operand;
25169 begin_scope (sk_block, NULL_TREE);
25172 ~scope_sentinel ()
25174 pop_bindings_and_leave_scope ();
25175 --cp_unevaluated_operand;
25177 } s;
25179 /* Parse the optional parameter list. */
25180 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25182 parms = cp_parser_requirement_parameter_list (parser);
25183 if (parms == error_mark_node)
25184 return error_mark_node;
25186 else
25187 parms = NULL_TREE;
25189 /* Parse the requirement body. */
25190 reqs = cp_parser_requirement_body (parser);
25191 if (reqs == error_mark_node)
25192 return error_mark_node;
25195 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25196 the parm chain. */
25197 grokparms (parms, &parms);
25198 return finish_requires_expr (parms, reqs);
25201 /* Parse a parameterized requirement.
25203 requirement-parameter-list:
25204 '(' parameter-declaration-clause ')' */
25205 static tree
25206 cp_parser_requirement_parameter_list (cp_parser *parser)
25208 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25209 return error_mark_node;
25211 tree parms = cp_parser_parameter_declaration_clause (parser);
25213 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25214 return error_mark_node;
25216 return parms;
25219 /* Parse the body of a requirement.
25221 requirement-body:
25222 '{' requirement-list '}' */
25223 static tree
25224 cp_parser_requirement_body (cp_parser *parser)
25226 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25227 return error_mark_node;
25229 tree reqs = cp_parser_requirement_list (parser);
25231 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25232 return error_mark_node;
25234 return reqs;
25237 /* Parse a list of requirements.
25239 requirement-list:
25240 requirement
25241 requirement-list ';' requirement[opt] */
25242 static tree
25243 cp_parser_requirement_list (cp_parser *parser)
25245 tree result = NULL_TREE;
25246 while (true)
25248 tree req = cp_parser_requirement (parser);
25249 if (req == error_mark_node)
25250 return error_mark_node;
25252 result = tree_cons (NULL_TREE, req, result);
25254 /* If we see a semi-colon, consume it. */
25255 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25256 cp_lexer_consume_token (parser->lexer);
25258 /* Stop processing at the end of the list. */
25259 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25260 break;
25263 /* Reverse the order of requirements so they are analyzed in
25264 declaration order. */
25265 return nreverse (result);
25268 /* Parse a syntactic requirement or type requirement.
25270 requirement:
25271 simple-requirement
25272 compound-requirement
25273 type-requirement
25274 nested-requirement */
25275 static tree
25276 cp_parser_requirement (cp_parser *parser)
25278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25279 return cp_parser_compound_requirement (parser);
25280 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25281 return cp_parser_type_requirement (parser);
25282 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25283 return cp_parser_nested_requirement (parser);
25284 else
25285 return cp_parser_simple_requirement (parser);
25288 /* Parse a simple requirement.
25290 simple-requirement:
25291 expression ';' */
25292 static tree
25293 cp_parser_simple_requirement (cp_parser *parser)
25295 tree expr = cp_parser_expression (parser, NULL, false, false);
25296 if (!expr || expr == error_mark_node)
25297 return error_mark_node;
25299 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25300 return error_mark_node;
25302 return finish_simple_requirement (expr);
25305 /* Parse a type requirement
25307 type-requirement
25308 nested-name-specifier [opt] required-type-name ';'
25310 required-type-name:
25311 type-name
25312 'template' [opt] simple-template-id */
25313 static tree
25314 cp_parser_type_requirement (cp_parser *parser)
25316 cp_lexer_consume_token (parser->lexer);
25318 // Save the scope before parsing name specifiers.
25319 tree saved_scope = parser->scope;
25320 tree saved_object_scope = parser->object_scope;
25321 tree saved_qualifying_scope = parser->qualifying_scope;
25322 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25323 cp_parser_nested_name_specifier_opt (parser,
25324 /*typename_keyword_p=*/true,
25325 /*check_dependency_p=*/false,
25326 /*type_p=*/true,
25327 /*is_declaration=*/false);
25329 tree type;
25330 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25332 cp_lexer_consume_token (parser->lexer);
25333 type = cp_parser_template_id (parser,
25334 /*template_keyword_p=*/true,
25335 /*check_dependency=*/false,
25336 /*tag_type=*/none_type,
25337 /*is_declaration=*/false);
25338 type = make_typename_type (parser->scope, type, typename_type,
25339 /*complain=*/tf_error);
25341 else
25342 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25344 if (TREE_CODE (type) == TYPE_DECL)
25345 type = TREE_TYPE (type);
25347 parser->scope = saved_scope;
25348 parser->object_scope = saved_object_scope;
25349 parser->qualifying_scope = saved_qualifying_scope;
25351 if (type == error_mark_node)
25352 cp_parser_skip_to_end_of_statement (parser);
25354 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25355 return error_mark_node;
25356 if (type == error_mark_node)
25357 return error_mark_node;
25359 return finish_type_requirement (type);
25362 /* Parse a compound requirement
25364 compound-requirement:
25365 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25366 static tree
25367 cp_parser_compound_requirement (cp_parser *parser)
25369 /* Parse an expression enclosed in '{ }'s. */
25370 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25371 return error_mark_node;
25373 tree expr = cp_parser_expression (parser, NULL, false, false);
25374 if (!expr || expr == error_mark_node)
25375 return error_mark_node;
25377 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25378 return error_mark_node;
25380 /* Parse the optional noexcept. */
25381 bool noexcept_p = false;
25382 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25384 cp_lexer_consume_token (parser->lexer);
25385 noexcept_p = true;
25388 /* Parse the optional trailing return type. */
25389 tree type = NULL_TREE;
25390 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25392 cp_lexer_consume_token (parser->lexer);
25393 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25394 parser->in_result_type_constraint_p = true;
25395 type = cp_parser_trailing_type_id (parser);
25396 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25397 if (type == error_mark_node)
25398 return error_mark_node;
25401 return finish_compound_requirement (expr, type, noexcept_p);
25404 /* Parse a nested requirement. This is the same as a requires clause.
25406 nested-requirement:
25407 requires-clause */
25408 static tree
25409 cp_parser_nested_requirement (cp_parser *parser)
25411 cp_lexer_consume_token (parser->lexer);
25412 tree req = cp_parser_requires_clause (parser);
25413 if (req == error_mark_node)
25414 return error_mark_node;
25415 return finish_nested_requirement (req);
25418 /* Support Functions */
25420 /* Return the appropriate prefer_type argument for lookup_name_real based on
25421 tag_type and template_mem_access. */
25423 static inline int
25424 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25426 /* DR 141: When looking in the current enclosing context for a template-name
25427 after -> or ., only consider class templates. */
25428 if (template_mem_access)
25429 return 2;
25430 switch (tag_type)
25432 case none_type: return 0; // No preference.
25433 case scope_type: return 1; // Type or namespace.
25434 default: return 2; // Type only.
25438 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25439 NAME should have one of the representations used for an
25440 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25441 is returned. If PARSER->SCOPE is a dependent type, then a
25442 SCOPE_REF is returned.
25444 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25445 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25446 was formed. Abstractly, such entities should not be passed to this
25447 function, because they do not need to be looked up, but it is
25448 simpler to check for this special case here, rather than at the
25449 call-sites.
25451 In cases not explicitly covered above, this function returns a
25452 DECL, OVERLOAD, or baselink representing the result of the lookup.
25453 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25454 is returned.
25456 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25457 (e.g., "struct") that was used. In that case bindings that do not
25458 refer to types are ignored.
25460 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25461 ignored.
25463 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25464 are ignored.
25466 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25467 types.
25469 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25470 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25471 NULL_TREE otherwise. */
25473 static cp_expr
25474 cp_parser_lookup_name (cp_parser *parser, tree name,
25475 enum tag_types tag_type,
25476 bool is_template,
25477 bool is_namespace,
25478 bool check_dependency,
25479 tree *ambiguous_decls,
25480 location_t name_location)
25482 tree decl;
25483 tree object_type = parser->context->object_type;
25485 /* Assume that the lookup will be unambiguous. */
25486 if (ambiguous_decls)
25487 *ambiguous_decls = NULL_TREE;
25489 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25490 no longer valid. Note that if we are parsing tentatively, and
25491 the parse fails, OBJECT_TYPE will be automatically restored. */
25492 parser->context->object_type = NULL_TREE;
25494 if (name == error_mark_node)
25495 return error_mark_node;
25497 /* A template-id has already been resolved; there is no lookup to
25498 do. */
25499 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25500 return name;
25501 if (BASELINK_P (name))
25503 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25504 == TEMPLATE_ID_EXPR);
25505 return name;
25508 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25509 it should already have been checked to make sure that the name
25510 used matches the type being destroyed. */
25511 if (TREE_CODE (name) == BIT_NOT_EXPR)
25513 tree type;
25515 /* Figure out to which type this destructor applies. */
25516 if (parser->scope)
25517 type = parser->scope;
25518 else if (object_type)
25519 type = object_type;
25520 else
25521 type = current_class_type;
25522 /* If that's not a class type, there is no destructor. */
25523 if (!type || !CLASS_TYPE_P (type))
25524 return error_mark_node;
25525 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25526 lazily_declare_fn (sfk_destructor, type);
25527 if (!CLASSTYPE_DESTRUCTORS (type))
25528 return error_mark_node;
25529 /* If it was a class type, return the destructor. */
25530 return CLASSTYPE_DESTRUCTORS (type);
25533 /* By this point, the NAME should be an ordinary identifier. If
25534 the id-expression was a qualified name, the qualifying scope is
25535 stored in PARSER->SCOPE at this point. */
25536 gcc_assert (identifier_p (name));
25538 /* Perform the lookup. */
25539 if (parser->scope)
25541 bool dependent_p;
25543 if (parser->scope == error_mark_node)
25544 return error_mark_node;
25546 /* If the SCOPE is dependent, the lookup must be deferred until
25547 the template is instantiated -- unless we are explicitly
25548 looking up names in uninstantiated templates. Even then, we
25549 cannot look up the name if the scope is not a class type; it
25550 might, for example, be a template type parameter. */
25551 dependent_p = (TYPE_P (parser->scope)
25552 && dependent_scope_p (parser->scope));
25553 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25554 && dependent_p)
25555 /* Defer lookup. */
25556 decl = error_mark_node;
25557 else
25559 tree pushed_scope = NULL_TREE;
25561 /* If PARSER->SCOPE is a dependent type, then it must be a
25562 class type, and we must not be checking dependencies;
25563 otherwise, we would have processed this lookup above. So
25564 that PARSER->SCOPE is not considered a dependent base by
25565 lookup_member, we must enter the scope here. */
25566 if (dependent_p)
25567 pushed_scope = push_scope (parser->scope);
25569 /* If the PARSER->SCOPE is a template specialization, it
25570 may be instantiated during name lookup. In that case,
25571 errors may be issued. Even if we rollback the current
25572 tentative parse, those errors are valid. */
25573 decl = lookup_qualified_name (parser->scope, name,
25574 prefer_type_arg (tag_type),
25575 /*complain=*/true);
25577 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25578 lookup result and the nested-name-specifier nominates a class C:
25579 * if the name specified after the nested-name-specifier, when
25580 looked up in C, is the injected-class-name of C (Clause 9), or
25581 * if the name specified after the nested-name-specifier is the
25582 same as the identifier or the simple-template-id's template-
25583 name in the last component of the nested-name-specifier,
25584 the name is instead considered to name the constructor of
25585 class C. [ Note: for example, the constructor is not an
25586 acceptable lookup result in an elaborated-type-specifier so
25587 the constructor would not be used in place of the
25588 injected-class-name. --end note ] Such a constructor name
25589 shall be used only in the declarator-id of a declaration that
25590 names a constructor or in a using-declaration. */
25591 if (tag_type == none_type
25592 && DECL_SELF_REFERENCE_P (decl)
25593 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25594 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25595 prefer_type_arg (tag_type),
25596 /*complain=*/true);
25598 /* If we have a single function from a using decl, pull it out. */
25599 if (TREE_CODE (decl) == OVERLOAD
25600 && !really_overloaded_fn (decl))
25601 decl = OVL_FUNCTION (decl);
25603 if (pushed_scope)
25604 pop_scope (pushed_scope);
25607 /* If the scope is a dependent type and either we deferred lookup or
25608 we did lookup but didn't find the name, rememeber the name. */
25609 if (decl == error_mark_node && TYPE_P (parser->scope)
25610 && dependent_type_p (parser->scope))
25612 if (tag_type)
25614 tree type;
25616 /* The resolution to Core Issue 180 says that `struct
25617 A::B' should be considered a type-name, even if `A'
25618 is dependent. */
25619 type = make_typename_type (parser->scope, name, tag_type,
25620 /*complain=*/tf_error);
25621 if (type != error_mark_node)
25622 decl = TYPE_NAME (type);
25624 else if (is_template
25625 && (cp_parser_next_token_ends_template_argument_p (parser)
25626 || cp_lexer_next_token_is (parser->lexer,
25627 CPP_CLOSE_PAREN)))
25628 decl = make_unbound_class_template (parser->scope,
25629 name, NULL_TREE,
25630 /*complain=*/tf_error);
25631 else
25632 decl = build_qualified_name (/*type=*/NULL_TREE,
25633 parser->scope, name,
25634 is_template);
25636 parser->qualifying_scope = parser->scope;
25637 parser->object_scope = NULL_TREE;
25639 else if (object_type)
25641 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25642 OBJECT_TYPE is not a class. */
25643 if (CLASS_TYPE_P (object_type))
25644 /* If the OBJECT_TYPE is a template specialization, it may
25645 be instantiated during name lookup. In that case, errors
25646 may be issued. Even if we rollback the current tentative
25647 parse, those errors are valid. */
25648 decl = lookup_member (object_type,
25649 name,
25650 /*protect=*/0,
25651 prefer_type_arg (tag_type),
25652 tf_warning_or_error);
25653 else
25654 decl = NULL_TREE;
25656 if (!decl)
25657 /* Look it up in the enclosing context. DR 141: When looking for a
25658 template-name after -> or ., only consider class templates. */
25659 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25660 /*nonclass=*/0,
25661 /*block_p=*/true, is_namespace, 0);
25662 if (object_type == unknown_type_node)
25663 /* The object is type-dependent, so we can't look anything up; we used
25664 this to get the DR 141 behavior. */
25665 object_type = NULL_TREE;
25666 parser->object_scope = object_type;
25667 parser->qualifying_scope = NULL_TREE;
25669 else
25671 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25672 /*nonclass=*/0,
25673 /*block_p=*/true, is_namespace, 0);
25674 parser->qualifying_scope = NULL_TREE;
25675 parser->object_scope = NULL_TREE;
25678 /* If the lookup failed, let our caller know. */
25679 if (!decl || decl == error_mark_node)
25680 return error_mark_node;
25682 /* Pull out the template from an injected-class-name (or multiple). */
25683 if (is_template)
25684 decl = maybe_get_template_decl_from_type_decl (decl);
25686 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25687 if (TREE_CODE (decl) == TREE_LIST)
25689 if (ambiguous_decls)
25690 *ambiguous_decls = decl;
25691 /* The error message we have to print is too complicated for
25692 cp_parser_error, so we incorporate its actions directly. */
25693 if (!cp_parser_simulate_error (parser))
25695 error_at (name_location, "reference to %qD is ambiguous",
25696 name);
25697 print_candidates (decl);
25699 return error_mark_node;
25702 gcc_assert (DECL_P (decl)
25703 || TREE_CODE (decl) == OVERLOAD
25704 || TREE_CODE (decl) == SCOPE_REF
25705 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25706 || BASELINK_P (decl));
25708 /* If we have resolved the name of a member declaration, check to
25709 see if the declaration is accessible. When the name resolves to
25710 set of overloaded functions, accessibility is checked when
25711 overload resolution is done.
25713 During an explicit instantiation, access is not checked at all,
25714 as per [temp.explicit]. */
25715 if (DECL_P (decl))
25716 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25718 maybe_record_typedef_use (decl);
25720 return cp_expr (decl, name_location);
25723 /* Like cp_parser_lookup_name, but for use in the typical case where
25724 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25725 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25727 static tree
25728 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25730 return cp_parser_lookup_name (parser, name,
25731 none_type,
25732 /*is_template=*/false,
25733 /*is_namespace=*/false,
25734 /*check_dependency=*/true,
25735 /*ambiguous_decls=*/NULL,
25736 location);
25739 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25740 the current context, return the TYPE_DECL. If TAG_NAME_P is
25741 true, the DECL indicates the class being defined in a class-head,
25742 or declared in an elaborated-type-specifier.
25744 Otherwise, return DECL. */
25746 static tree
25747 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25749 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25750 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25752 struct A {
25753 template <typename T> struct B;
25756 template <typename T> struct A::B {};
25758 Similarly, in an elaborated-type-specifier:
25760 namespace N { struct X{}; }
25762 struct A {
25763 template <typename T> friend struct N::X;
25766 However, if the DECL refers to a class type, and we are in
25767 the scope of the class, then the name lookup automatically
25768 finds the TYPE_DECL created by build_self_reference rather
25769 than a TEMPLATE_DECL. For example, in:
25771 template <class T> struct S {
25772 S s;
25775 there is no need to handle such case. */
25777 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25778 return DECL_TEMPLATE_RESULT (decl);
25780 return decl;
25783 /* If too many, or too few, template-parameter lists apply to the
25784 declarator, issue an error message. Returns TRUE if all went well,
25785 and FALSE otherwise. */
25787 static bool
25788 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25789 cp_declarator *declarator,
25790 location_t declarator_location)
25792 switch (declarator->kind)
25794 case cdk_id:
25796 unsigned num_templates = 0;
25797 tree scope = declarator->u.id.qualifying_scope;
25799 if (scope)
25800 num_templates = num_template_headers_for_class (scope);
25801 else if (TREE_CODE (declarator->u.id.unqualified_name)
25802 == TEMPLATE_ID_EXPR)
25803 /* If the DECLARATOR has the form `X<y>' then it uses one
25804 additional level of template parameters. */
25805 ++num_templates;
25807 return cp_parser_check_template_parameters
25808 (parser, num_templates, declarator_location, declarator);
25811 case cdk_function:
25812 case cdk_array:
25813 case cdk_pointer:
25814 case cdk_reference:
25815 case cdk_ptrmem:
25816 return (cp_parser_check_declarator_template_parameters
25817 (parser, declarator->declarator, declarator_location));
25819 case cdk_decomp:
25820 case cdk_error:
25821 return true;
25823 default:
25824 gcc_unreachable ();
25826 return false;
25829 /* NUM_TEMPLATES were used in the current declaration. If that is
25830 invalid, return FALSE and issue an error messages. Otherwise,
25831 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25832 declarator and we can print more accurate diagnostics. */
25834 static bool
25835 cp_parser_check_template_parameters (cp_parser* parser,
25836 unsigned num_templates,
25837 location_t location,
25838 cp_declarator *declarator)
25840 /* If there are the same number of template classes and parameter
25841 lists, that's OK. */
25842 if (parser->num_template_parameter_lists == num_templates)
25843 return true;
25844 /* If there are more, but only one more, then we are referring to a
25845 member template. That's OK too. */
25846 if (parser->num_template_parameter_lists == num_templates + 1)
25847 return true;
25848 /* If there are more template classes than parameter lists, we have
25849 something like:
25851 template <class T> void S<T>::R<T>::f (); */
25852 if (parser->num_template_parameter_lists < num_templates)
25854 if (declarator && !current_function_decl)
25855 error_at (location, "specializing member %<%T::%E%> "
25856 "requires %<template<>%> syntax",
25857 declarator->u.id.qualifying_scope,
25858 declarator->u.id.unqualified_name);
25859 else if (declarator)
25860 error_at (location, "invalid declaration of %<%T::%E%>",
25861 declarator->u.id.qualifying_scope,
25862 declarator->u.id.unqualified_name);
25863 else
25864 error_at (location, "too few template-parameter-lists");
25865 return false;
25867 /* Otherwise, there are too many template parameter lists. We have
25868 something like:
25870 template <class T> template <class U> void S::f(); */
25871 error_at (location, "too many template-parameter-lists");
25872 return false;
25875 /* Parse an optional `::' token indicating that the following name is
25876 from the global namespace. If so, PARSER->SCOPE is set to the
25877 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
25878 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
25879 Returns the new value of PARSER->SCOPE, if the `::' token is
25880 present, and NULL_TREE otherwise. */
25882 static tree
25883 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
25885 cp_token *token;
25887 /* Peek at the next token. */
25888 token = cp_lexer_peek_token (parser->lexer);
25889 /* If we're looking at a `::' token then we're starting from the
25890 global namespace, not our current location. */
25891 if (token->type == CPP_SCOPE)
25893 /* Consume the `::' token. */
25894 cp_lexer_consume_token (parser->lexer);
25895 /* Set the SCOPE so that we know where to start the lookup. */
25896 parser->scope = global_namespace;
25897 parser->qualifying_scope = global_namespace;
25898 parser->object_scope = NULL_TREE;
25900 return parser->scope;
25902 else if (!current_scope_valid_p)
25904 parser->scope = NULL_TREE;
25905 parser->qualifying_scope = NULL_TREE;
25906 parser->object_scope = NULL_TREE;
25909 return NULL_TREE;
25912 /* Returns TRUE if the upcoming token sequence is the start of a
25913 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
25914 declarator is preceded by the `friend' specifier. */
25916 static bool
25917 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
25919 bool constructor_p;
25920 bool outside_class_specifier_p;
25921 tree nested_name_specifier;
25922 cp_token *next_token;
25924 /* The common case is that this is not a constructor declarator, so
25925 try to avoid doing lots of work if at all possible. It's not
25926 valid declare a constructor at function scope. */
25927 if (parser->in_function_body)
25928 return false;
25929 /* And only certain tokens can begin a constructor declarator. */
25930 next_token = cp_lexer_peek_token (parser->lexer);
25931 if (next_token->type != CPP_NAME
25932 && next_token->type != CPP_SCOPE
25933 && next_token->type != CPP_NESTED_NAME_SPECIFIER
25934 && next_token->type != CPP_TEMPLATE_ID)
25935 return false;
25937 /* Parse tentatively; we are going to roll back all of the tokens
25938 consumed here. */
25939 cp_parser_parse_tentatively (parser);
25940 /* Assume that we are looking at a constructor declarator. */
25941 constructor_p = true;
25943 /* Look for the optional `::' operator. */
25944 cp_parser_global_scope_opt (parser,
25945 /*current_scope_valid_p=*/false);
25946 /* Look for the nested-name-specifier. */
25947 nested_name_specifier
25948 = (cp_parser_nested_name_specifier_opt (parser,
25949 /*typename_keyword_p=*/false,
25950 /*check_dependency_p=*/false,
25951 /*type_p=*/false,
25952 /*is_declaration=*/false));
25954 outside_class_specifier_p = (!at_class_scope_p ()
25955 || !TYPE_BEING_DEFINED (current_class_type)
25956 || friend_p);
25958 /* Outside of a class-specifier, there must be a
25959 nested-name-specifier. Except in C++17 mode, where we
25960 might be declaring a guiding declaration. */
25961 if (!nested_name_specifier && outside_class_specifier_p
25962 && cxx_dialect < cxx1z)
25963 constructor_p = false;
25964 else if (nested_name_specifier == error_mark_node)
25965 constructor_p = false;
25967 /* If we have a class scope, this is easy; DR 147 says that S::S always
25968 names the constructor, and no other qualified name could. */
25969 if (constructor_p && nested_name_specifier
25970 && CLASS_TYPE_P (nested_name_specifier))
25972 tree id = cp_parser_unqualified_id (parser,
25973 /*template_keyword_p=*/false,
25974 /*check_dependency_p=*/false,
25975 /*declarator_p=*/true,
25976 /*optional_p=*/false);
25977 if (is_overloaded_fn (id))
25978 id = DECL_NAME (get_first_fn (id));
25979 if (!constructor_name_p (id, nested_name_specifier))
25980 constructor_p = false;
25982 /* If we still think that this might be a constructor-declarator,
25983 look for a class-name. */
25984 else if (constructor_p)
25986 /* If we have:
25988 template <typename T> struct S {
25989 S();
25992 we must recognize that the nested `S' names a class. */
25993 if (cxx_dialect >= cxx1z)
25994 cp_parser_parse_tentatively (parser);
25996 tree type_decl;
25997 type_decl = cp_parser_class_name (parser,
25998 /*typename_keyword_p=*/false,
25999 /*template_keyword_p=*/false,
26000 none_type,
26001 /*check_dependency_p=*/false,
26002 /*class_head_p=*/false,
26003 /*is_declaration=*/false);
26005 if (cxx_dialect >= cxx1z
26006 && !cp_parser_parse_definitely (parser))
26008 type_decl = NULL_TREE;
26009 tree tmpl = cp_parser_template_name (parser,
26010 /*template_keyword*/false,
26011 /*check_dependency_p*/false,
26012 /*is_declaration*/false,
26013 none_type,
26014 /*is_identifier*/NULL);
26015 if (DECL_CLASS_TEMPLATE_P (tmpl)
26016 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26017 /* It's a deduction guide, return true. */;
26018 else
26019 cp_parser_simulate_error (parser);
26022 /* If there was no class-name, then this is not a constructor.
26023 Otherwise, if we are in a class-specifier and we aren't
26024 handling a friend declaration, check that its type matches
26025 current_class_type (c++/38313). Note: error_mark_node
26026 is left alone for error recovery purposes. */
26027 constructor_p = (!cp_parser_error_occurred (parser)
26028 && (outside_class_specifier_p
26029 || type_decl == NULL_TREE
26030 || type_decl == error_mark_node
26031 || same_type_p (current_class_type,
26032 TREE_TYPE (type_decl))));
26034 /* If we're still considering a constructor, we have to see a `(',
26035 to begin the parameter-declaration-clause, followed by either a
26036 `)', an `...', or a decl-specifier. We need to check for a
26037 type-specifier to avoid being fooled into thinking that:
26039 S (f) (int);
26041 is a constructor. (It is actually a function named `f' that
26042 takes one parameter (of type `int') and returns a value of type
26043 `S'. */
26044 if (constructor_p
26045 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26046 constructor_p = false;
26048 if (constructor_p
26049 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26050 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26051 /* A parameter declaration begins with a decl-specifier,
26052 which is either the "attribute" keyword, a storage class
26053 specifier, or (usually) a type-specifier. */
26054 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26056 tree type;
26057 tree pushed_scope = NULL_TREE;
26058 unsigned saved_num_template_parameter_lists;
26060 /* Names appearing in the type-specifier should be looked up
26061 in the scope of the class. */
26062 if (current_class_type)
26063 type = NULL_TREE;
26064 else if (type_decl)
26066 type = TREE_TYPE (type_decl);
26067 if (TREE_CODE (type) == TYPENAME_TYPE)
26069 type = resolve_typename_type (type,
26070 /*only_current_p=*/false);
26071 if (TREE_CODE (type) == TYPENAME_TYPE)
26073 cp_parser_abort_tentative_parse (parser);
26074 return false;
26077 pushed_scope = push_scope (type);
26080 /* Inside the constructor parameter list, surrounding
26081 template-parameter-lists do not apply. */
26082 saved_num_template_parameter_lists
26083 = parser->num_template_parameter_lists;
26084 parser->num_template_parameter_lists = 0;
26086 /* Look for the type-specifier. */
26087 cp_parser_type_specifier (parser,
26088 CP_PARSER_FLAGS_NONE,
26089 /*decl_specs=*/NULL,
26090 /*is_declarator=*/true,
26091 /*declares_class_or_enum=*/NULL,
26092 /*is_cv_qualifier=*/NULL);
26094 parser->num_template_parameter_lists
26095 = saved_num_template_parameter_lists;
26097 /* Leave the scope of the class. */
26098 if (pushed_scope)
26099 pop_scope (pushed_scope);
26101 constructor_p = !cp_parser_error_occurred (parser);
26105 /* We did not really want to consume any tokens. */
26106 cp_parser_abort_tentative_parse (parser);
26108 return constructor_p;
26111 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26112 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26113 they must be performed once we are in the scope of the function.
26115 Returns the function defined. */
26117 static tree
26118 cp_parser_function_definition_from_specifiers_and_declarator
26119 (cp_parser* parser,
26120 cp_decl_specifier_seq *decl_specifiers,
26121 tree attributes,
26122 const cp_declarator *declarator)
26124 tree fn;
26125 bool success_p;
26127 /* Begin the function-definition. */
26128 success_p = start_function (decl_specifiers, declarator, attributes);
26130 /* The things we're about to see are not directly qualified by any
26131 template headers we've seen thus far. */
26132 reset_specialization ();
26134 /* If there were names looked up in the decl-specifier-seq that we
26135 did not check, check them now. We must wait until we are in the
26136 scope of the function to perform the checks, since the function
26137 might be a friend. */
26138 perform_deferred_access_checks (tf_warning_or_error);
26140 if (success_p)
26142 cp_finalize_omp_declare_simd (parser, current_function_decl);
26143 parser->omp_declare_simd = NULL;
26144 cp_finalize_oacc_routine (parser, current_function_decl, true);
26145 parser->oacc_routine = NULL;
26148 if (!success_p)
26150 /* Skip the entire function. */
26151 cp_parser_skip_to_end_of_block_or_statement (parser);
26152 fn = error_mark_node;
26154 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26156 /* Seen already, skip it. An error message has already been output. */
26157 cp_parser_skip_to_end_of_block_or_statement (parser);
26158 fn = current_function_decl;
26159 current_function_decl = NULL_TREE;
26160 /* If this is a function from a class, pop the nested class. */
26161 if (current_class_name)
26162 pop_nested_class ();
26164 else
26166 timevar_id_t tv;
26167 if (DECL_DECLARED_INLINE_P (current_function_decl))
26168 tv = TV_PARSE_INLINE;
26169 else
26170 tv = TV_PARSE_FUNC;
26171 timevar_push (tv);
26172 fn = cp_parser_function_definition_after_declarator (parser,
26173 /*inline_p=*/false);
26174 timevar_pop (tv);
26177 return fn;
26180 /* Parse the part of a function-definition that follows the
26181 declarator. INLINE_P is TRUE iff this function is an inline
26182 function defined within a class-specifier.
26184 Returns the function defined. */
26186 static tree
26187 cp_parser_function_definition_after_declarator (cp_parser* parser,
26188 bool inline_p)
26190 tree fn;
26191 bool ctor_initializer_p = false;
26192 bool saved_in_unbraced_linkage_specification_p;
26193 bool saved_in_function_body;
26194 unsigned saved_num_template_parameter_lists;
26195 cp_token *token;
26196 bool fully_implicit_function_template_p
26197 = parser->fully_implicit_function_template_p;
26198 parser->fully_implicit_function_template_p = false;
26199 tree implicit_template_parms
26200 = parser->implicit_template_parms;
26201 parser->implicit_template_parms = 0;
26202 cp_binding_level* implicit_template_scope
26203 = parser->implicit_template_scope;
26204 parser->implicit_template_scope = 0;
26206 saved_in_function_body = parser->in_function_body;
26207 parser->in_function_body = true;
26208 /* If the next token is `return', then the code may be trying to
26209 make use of the "named return value" extension that G++ used to
26210 support. */
26211 token = cp_lexer_peek_token (parser->lexer);
26212 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26214 /* Consume the `return' keyword. */
26215 cp_lexer_consume_token (parser->lexer);
26216 /* Look for the identifier that indicates what value is to be
26217 returned. */
26218 cp_parser_identifier (parser);
26219 /* Issue an error message. */
26220 error_at (token->location,
26221 "named return values are no longer supported");
26222 /* Skip tokens until we reach the start of the function body. */
26223 while (true)
26225 cp_token *token = cp_lexer_peek_token (parser->lexer);
26226 if (token->type == CPP_OPEN_BRACE
26227 || token->type == CPP_EOF
26228 || token->type == CPP_PRAGMA_EOL)
26229 break;
26230 cp_lexer_consume_token (parser->lexer);
26233 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26234 anything declared inside `f'. */
26235 saved_in_unbraced_linkage_specification_p
26236 = parser->in_unbraced_linkage_specification_p;
26237 parser->in_unbraced_linkage_specification_p = false;
26238 /* Inside the function, surrounding template-parameter-lists do not
26239 apply. */
26240 saved_num_template_parameter_lists
26241 = parser->num_template_parameter_lists;
26242 parser->num_template_parameter_lists = 0;
26244 start_lambda_scope (current_function_decl);
26246 /* If the next token is `try', `__transaction_atomic', or
26247 `__transaction_relaxed`, then we are looking at either function-try-block
26248 or function-transaction-block. Note that all of these include the
26249 function-body. */
26250 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26251 ctor_initializer_p = cp_parser_function_transaction (parser,
26252 RID_TRANSACTION_ATOMIC);
26253 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26254 RID_TRANSACTION_RELAXED))
26255 ctor_initializer_p = cp_parser_function_transaction (parser,
26256 RID_TRANSACTION_RELAXED);
26257 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26258 ctor_initializer_p = cp_parser_function_try_block (parser);
26259 else
26260 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26261 (parser, /*in_function_try_block=*/false);
26263 finish_lambda_scope ();
26265 /* Finish the function. */
26266 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26267 (inline_p ? 2 : 0));
26268 /* Generate code for it, if necessary. */
26269 expand_or_defer_fn (fn);
26270 /* Restore the saved values. */
26271 parser->in_unbraced_linkage_specification_p
26272 = saved_in_unbraced_linkage_specification_p;
26273 parser->num_template_parameter_lists
26274 = saved_num_template_parameter_lists;
26275 parser->in_function_body = saved_in_function_body;
26277 parser->fully_implicit_function_template_p
26278 = fully_implicit_function_template_p;
26279 parser->implicit_template_parms
26280 = implicit_template_parms;
26281 parser->implicit_template_scope
26282 = implicit_template_scope;
26284 if (parser->fully_implicit_function_template_p)
26285 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26287 return fn;
26290 /* Parse a template-declaration body (following argument list). */
26292 static void
26293 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26294 tree parameter_list,
26295 bool member_p)
26297 tree decl = NULL_TREE;
26298 bool friend_p = false;
26300 /* We just processed one more parameter list. */
26301 ++parser->num_template_parameter_lists;
26303 /* Get the deferred access checks from the parameter list. These
26304 will be checked once we know what is being declared, as for a
26305 member template the checks must be performed in the scope of the
26306 class containing the member. */
26307 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26309 /* Tentatively parse for a new template parameter list, which can either be
26310 the template keyword or a template introduction. */
26311 if (cp_parser_template_declaration_after_export (parser, member_p))
26312 /* OK */;
26313 else if (cxx_dialect >= cxx11
26314 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26315 decl = cp_parser_alias_declaration (parser);
26316 else
26318 /* There are no access checks when parsing a template, as we do not
26319 know if a specialization will be a friend. */
26320 push_deferring_access_checks (dk_no_check);
26321 cp_token *token = cp_lexer_peek_token (parser->lexer);
26322 decl = cp_parser_single_declaration (parser,
26323 checks,
26324 member_p,
26325 /*explicit_specialization_p=*/false,
26326 &friend_p);
26327 pop_deferring_access_checks ();
26329 /* If this is a member template declaration, let the front
26330 end know. */
26331 if (member_p && !friend_p && decl)
26333 if (TREE_CODE (decl) == TYPE_DECL)
26334 cp_parser_check_access_in_redeclaration (decl, token->location);
26336 decl = finish_member_template_decl (decl);
26338 else if (friend_p && decl
26339 && DECL_DECLARES_TYPE_P (decl))
26340 make_friend_class (current_class_type, TREE_TYPE (decl),
26341 /*complain=*/true);
26343 /* We are done with the current parameter list. */
26344 --parser->num_template_parameter_lists;
26346 pop_deferring_access_checks ();
26348 /* Finish up. */
26349 finish_template_decl (parameter_list);
26351 /* Check the template arguments for a literal operator template. */
26352 if (decl
26353 && DECL_DECLARES_FUNCTION_P (decl)
26354 && UDLIT_OPER_P (DECL_NAME (decl)))
26356 bool ok = true;
26357 if (parameter_list == NULL_TREE)
26358 ok = false;
26359 else
26361 int num_parms = TREE_VEC_LENGTH (parameter_list);
26362 if (num_parms == 1)
26364 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26365 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26366 if (TREE_TYPE (parm) != char_type_node
26367 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26368 ok = false;
26370 else if (num_parms == 2 && cxx_dialect >= cxx14)
26372 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26373 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26374 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26375 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26376 if (parm == error_mark_node
26377 || TREE_TYPE (parm) != TREE_TYPE (type)
26378 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26379 ok = false;
26381 else
26382 ok = false;
26384 if (!ok)
26386 if (cxx_dialect >= cxx14)
26387 error ("literal operator template %qD has invalid parameter list."
26388 " Expected non-type template argument pack <char...>"
26389 " or <typename CharT, CharT...>",
26390 decl);
26391 else
26392 error ("literal operator template %qD has invalid parameter list."
26393 " Expected non-type template argument pack <char...>",
26394 decl);
26398 /* Register member declarations. */
26399 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26400 finish_member_declaration (decl);
26401 /* If DECL is a function template, we must return to parse it later.
26402 (Even though there is no definition, there might be default
26403 arguments that need handling.) */
26404 if (member_p && decl
26405 && DECL_DECLARES_FUNCTION_P (decl))
26406 vec_safe_push (unparsed_funs_with_definitions, decl);
26409 /* Parse a template introduction header for a template-declaration. Returns
26410 false if tentative parse fails. */
26412 static bool
26413 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26415 cp_parser_parse_tentatively (parser);
26417 tree saved_scope = parser->scope;
26418 tree saved_object_scope = parser->object_scope;
26419 tree saved_qualifying_scope = parser->qualifying_scope;
26421 /* Look for the optional `::' operator. */
26422 cp_parser_global_scope_opt (parser,
26423 /*current_scope_valid_p=*/false);
26424 /* Look for the nested-name-specifier. */
26425 cp_parser_nested_name_specifier_opt (parser,
26426 /*typename_keyword_p=*/false,
26427 /*check_dependency_p=*/true,
26428 /*type_p=*/false,
26429 /*is_declaration=*/false);
26431 cp_token *token = cp_lexer_peek_token (parser->lexer);
26432 tree concept_name = cp_parser_identifier (parser);
26434 /* Look up the concept for which we will be matching
26435 template parameters. */
26436 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26437 token->location);
26438 parser->scope = saved_scope;
26439 parser->object_scope = saved_object_scope;
26440 parser->qualifying_scope = saved_qualifying_scope;
26442 if (concept_name == error_mark_node)
26443 cp_parser_simulate_error (parser);
26445 /* Look for opening brace for introduction. */
26446 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26448 if (!cp_parser_parse_definitely (parser))
26449 return false;
26451 push_deferring_access_checks (dk_deferred);
26453 /* Build vector of placeholder parameters and grab
26454 matching identifiers. */
26455 tree introduction_list = cp_parser_introduction_list (parser);
26457 /* The introduction-list shall not be empty. */
26458 int nargs = TREE_VEC_LENGTH (introduction_list);
26459 if (nargs == 0)
26461 error ("empty introduction-list");
26462 return true;
26465 /* Look for closing brace for introduction. */
26466 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26467 return true;
26469 if (tmpl_decl == error_mark_node)
26471 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26472 token->location);
26473 return true;
26476 /* Build and associate the constraint. */
26477 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26478 if (parms && parms != error_mark_node)
26480 cp_parser_template_declaration_after_parameters (parser, parms,
26481 member_p);
26482 return true;
26485 error_at (token->location, "no matching concept for template-introduction");
26486 return true;
26489 /* Parse a normal template-declaration following the template keyword. */
26491 static void
26492 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26494 tree parameter_list;
26495 bool need_lang_pop;
26496 location_t location = input_location;
26498 /* Look for the `<' token. */
26499 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26500 return;
26501 if (at_class_scope_p () && current_function_decl)
26503 /* 14.5.2.2 [temp.mem]
26505 A local class shall not have member templates. */
26506 error_at (location,
26507 "invalid declaration of member template in local class");
26508 cp_parser_skip_to_end_of_block_or_statement (parser);
26509 return;
26511 /* [temp]
26513 A template ... shall not have C linkage. */
26514 if (current_lang_name == lang_name_c)
26516 error_at (location, "template with C linkage");
26517 /* Give it C++ linkage to avoid confusing other parts of the
26518 front end. */
26519 push_lang_context (lang_name_cplusplus);
26520 need_lang_pop = true;
26522 else
26523 need_lang_pop = false;
26525 /* We cannot perform access checks on the template parameter
26526 declarations until we know what is being declared, just as we
26527 cannot check the decl-specifier list. */
26528 push_deferring_access_checks (dk_deferred);
26530 /* If the next token is `>', then we have an invalid
26531 specialization. Rather than complain about an invalid template
26532 parameter, issue an error message here. */
26533 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26535 cp_parser_error (parser, "invalid explicit specialization");
26536 begin_specialization ();
26537 parameter_list = NULL_TREE;
26539 else
26541 /* Parse the template parameters. */
26542 parameter_list = cp_parser_template_parameter_list (parser);
26545 /* Look for the `>'. */
26546 cp_parser_skip_to_end_of_template_parameter_list (parser);
26548 /* Manage template requirements */
26549 if (flag_concepts)
26551 tree reqs = get_shorthand_constraints (current_template_parms);
26552 if (tree r = cp_parser_requires_clause_opt (parser))
26553 reqs = conjoin_constraints (reqs, normalize_expression (r));
26554 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26557 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26558 member_p);
26560 /* For the erroneous case of a template with C linkage, we pushed an
26561 implicit C++ linkage scope; exit that scope now. */
26562 if (need_lang_pop)
26563 pop_lang_context ();
26566 /* Parse a template-declaration, assuming that the `export' (and
26567 `extern') keywords, if present, has already been scanned. MEMBER_P
26568 is as for cp_parser_template_declaration. */
26570 static bool
26571 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26575 cp_lexer_consume_token (parser->lexer);
26576 cp_parser_explicit_template_declaration (parser, member_p);
26577 return true;
26579 else if (flag_concepts)
26580 return cp_parser_template_introduction (parser, member_p);
26582 return false;
26585 /* Perform the deferred access checks from a template-parameter-list.
26586 CHECKS is a TREE_LIST of access checks, as returned by
26587 get_deferred_access_checks. */
26589 static void
26590 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26592 ++processing_template_parmlist;
26593 perform_access_checks (checks, tf_warning_or_error);
26594 --processing_template_parmlist;
26597 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26598 `function-definition' sequence that follows a template header.
26599 If MEMBER_P is true, this declaration appears in a class scope.
26601 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26602 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26604 static tree
26605 cp_parser_single_declaration (cp_parser* parser,
26606 vec<deferred_access_check, va_gc> *checks,
26607 bool member_p,
26608 bool explicit_specialization_p,
26609 bool* friend_p)
26611 int declares_class_or_enum;
26612 tree decl = NULL_TREE;
26613 cp_decl_specifier_seq decl_specifiers;
26614 bool function_definition_p = false;
26615 cp_token *decl_spec_token_start;
26617 /* This function is only used when processing a template
26618 declaration. */
26619 gcc_assert (innermost_scope_kind () == sk_template_parms
26620 || innermost_scope_kind () == sk_template_spec);
26622 /* Defer access checks until we know what is being declared. */
26623 push_deferring_access_checks (dk_deferred);
26625 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26626 alternative. */
26627 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26628 cp_parser_decl_specifier_seq (parser,
26629 CP_PARSER_FLAGS_OPTIONAL,
26630 &decl_specifiers,
26631 &declares_class_or_enum);
26632 if (friend_p)
26633 *friend_p = cp_parser_friend_p (&decl_specifiers);
26635 /* There are no template typedefs. */
26636 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26638 error_at (decl_spec_token_start->location,
26639 "template declaration of %<typedef%>");
26640 decl = error_mark_node;
26643 /* Gather up the access checks that occurred the
26644 decl-specifier-seq. */
26645 stop_deferring_access_checks ();
26647 /* Check for the declaration of a template class. */
26648 if (declares_class_or_enum)
26650 if (cp_parser_declares_only_class_p (parser)
26651 || (declares_class_or_enum & 2))
26653 // If this is a declaration, but not a definition, associate
26654 // any constraints with the type declaration. Constraints
26655 // are associated with definitions in cp_parser_class_specifier.
26656 if (declares_class_or_enum == 1)
26657 associate_classtype_constraints (decl_specifiers.type);
26659 decl = shadow_tag (&decl_specifiers);
26661 /* In this case:
26663 struct C {
26664 friend template <typename T> struct A<T>::B;
26667 A<T>::B will be represented by a TYPENAME_TYPE, and
26668 therefore not recognized by shadow_tag. */
26669 if (friend_p && *friend_p
26670 && !decl
26671 && decl_specifiers.type
26672 && TYPE_P (decl_specifiers.type))
26673 decl = decl_specifiers.type;
26675 if (decl && decl != error_mark_node)
26676 decl = TYPE_NAME (decl);
26677 else
26678 decl = error_mark_node;
26680 /* Perform access checks for template parameters. */
26681 cp_parser_perform_template_parameter_access_checks (checks);
26683 /* Give a helpful diagnostic for
26684 template <class T> struct A { } a;
26685 if we aren't already recovering from an error. */
26686 if (!cp_parser_declares_only_class_p (parser)
26687 && !seen_error ())
26689 error_at (cp_lexer_peek_token (parser->lexer)->location,
26690 "a class template declaration must not declare "
26691 "anything else");
26692 cp_parser_skip_to_end_of_block_or_statement (parser);
26693 goto out;
26698 /* Complain about missing 'typename' or other invalid type names. */
26699 if (!decl_specifiers.any_type_specifiers_p
26700 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26702 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26703 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26704 the rest of this declaration. */
26705 decl = error_mark_node;
26706 goto out;
26709 /* If it's not a template class, try for a template function. If
26710 the next token is a `;', then this declaration does not declare
26711 anything. But, if there were errors in the decl-specifiers, then
26712 the error might well have come from an attempted class-specifier.
26713 In that case, there's no need to warn about a missing declarator. */
26714 if (!decl
26715 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26716 || decl_specifiers.type != error_mark_node))
26718 decl = cp_parser_init_declarator (parser,
26719 &decl_specifiers,
26720 checks,
26721 /*function_definition_allowed_p=*/true,
26722 member_p,
26723 declares_class_or_enum,
26724 &function_definition_p,
26725 NULL, NULL, NULL);
26727 /* 7.1.1-1 [dcl.stc]
26729 A storage-class-specifier shall not be specified in an explicit
26730 specialization... */
26731 if (decl
26732 && explicit_specialization_p
26733 && decl_specifiers.storage_class != sc_none)
26735 error_at (decl_spec_token_start->location,
26736 "explicit template specialization cannot have a storage class");
26737 decl = error_mark_node;
26740 if (decl && VAR_P (decl))
26741 check_template_variable (decl);
26744 /* Look for a trailing `;' after the declaration. */
26745 if (!function_definition_p
26746 && (decl == error_mark_node
26747 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26748 cp_parser_skip_to_end_of_block_or_statement (parser);
26750 out:
26751 pop_deferring_access_checks ();
26753 /* Clear any current qualification; whatever comes next is the start
26754 of something new. */
26755 parser->scope = NULL_TREE;
26756 parser->qualifying_scope = NULL_TREE;
26757 parser->object_scope = NULL_TREE;
26759 return decl;
26762 /* Parse a cast-expression that is not the operand of a unary "&". */
26764 static cp_expr
26765 cp_parser_simple_cast_expression (cp_parser *parser)
26767 return cp_parser_cast_expression (parser, /*address_p=*/false,
26768 /*cast_p=*/false, /*decltype*/false, NULL);
26771 /* Parse a functional cast to TYPE. Returns an expression
26772 representing the cast. */
26774 static cp_expr
26775 cp_parser_functional_cast (cp_parser* parser, tree type)
26777 vec<tree, va_gc> *vec;
26778 tree expression_list;
26779 cp_expr cast;
26780 bool nonconst_p;
26782 location_t start_loc = input_location;
26784 if (!type)
26785 type = error_mark_node;
26787 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26789 cp_lexer_set_source_position (parser->lexer);
26790 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26791 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26792 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26793 if (TREE_CODE (type) == TYPE_DECL)
26794 type = TREE_TYPE (type);
26796 cast = finish_compound_literal (type, expression_list,
26797 tf_warning_or_error);
26798 /* Create a location of the form:
26799 type_name{i, f}
26800 ^~~~~~~~~~~~~~~
26801 with caret == start at the start of the type name,
26802 finishing at the closing brace. */
26803 location_t finish_loc
26804 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26805 location_t combined_loc = make_location (start_loc, start_loc,
26806 finish_loc);
26807 cast.set_location (combined_loc);
26808 return cast;
26812 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26813 /*cast_p=*/true,
26814 /*allow_expansion_p=*/true,
26815 /*non_constant_p=*/NULL);
26816 if (vec == NULL)
26817 expression_list = error_mark_node;
26818 else
26820 expression_list = build_tree_list_vec (vec);
26821 release_tree_vector (vec);
26824 cast = build_functional_cast (type, expression_list,
26825 tf_warning_or_error);
26826 /* [expr.const]/1: In an integral constant expression "only type
26827 conversions to integral or enumeration type can be used". */
26828 if (TREE_CODE (type) == TYPE_DECL)
26829 type = TREE_TYPE (type);
26830 if (cast != error_mark_node
26831 && !cast_valid_in_integral_constant_expression_p (type)
26832 && cp_parser_non_integral_constant_expression (parser,
26833 NIC_CONSTRUCTOR))
26834 return error_mark_node;
26836 /* Create a location of the form:
26837 float(i)
26838 ^~~~~~~~
26839 with caret == start at the start of the type name,
26840 finishing at the closing paren. */
26841 location_t finish_loc
26842 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26843 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
26844 cast.set_location (combined_loc);
26845 return cast;
26848 /* Save the tokens that make up the body of a member function defined
26849 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
26850 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
26851 specifiers applied to the declaration. Returns the FUNCTION_DECL
26852 for the member function. */
26854 static tree
26855 cp_parser_save_member_function_body (cp_parser* parser,
26856 cp_decl_specifier_seq *decl_specifiers,
26857 cp_declarator *declarator,
26858 tree attributes)
26860 cp_token *first;
26861 cp_token *last;
26862 tree fn;
26863 bool function_try_block = false;
26865 /* Create the FUNCTION_DECL. */
26866 fn = grokmethod (decl_specifiers, declarator, attributes);
26867 cp_finalize_omp_declare_simd (parser, fn);
26868 cp_finalize_oacc_routine (parser, fn, true);
26869 /* If something went badly wrong, bail out now. */
26870 if (fn == error_mark_node)
26872 /* If there's a function-body, skip it. */
26873 if (cp_parser_token_starts_function_definition_p
26874 (cp_lexer_peek_token (parser->lexer)))
26875 cp_parser_skip_to_end_of_block_or_statement (parser);
26876 return error_mark_node;
26879 /* Remember it, if there default args to post process. */
26880 cp_parser_save_default_args (parser, fn);
26882 /* Save away the tokens that make up the body of the
26883 function. */
26884 first = parser->lexer->next_token;
26886 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
26887 cp_lexer_consume_token (parser->lexer);
26888 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26889 RID_TRANSACTION_ATOMIC))
26891 cp_lexer_consume_token (parser->lexer);
26892 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
26893 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
26894 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
26895 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
26896 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
26897 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
26898 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
26900 cp_lexer_consume_token (parser->lexer);
26901 cp_lexer_consume_token (parser->lexer);
26902 cp_lexer_consume_token (parser->lexer);
26903 cp_lexer_consume_token (parser->lexer);
26904 cp_lexer_consume_token (parser->lexer);
26906 else
26907 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
26908 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
26910 cp_lexer_consume_token (parser->lexer);
26911 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26912 break;
26916 /* Handle function try blocks. */
26917 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26919 cp_lexer_consume_token (parser->lexer);
26920 function_try_block = true;
26922 /* We can have braced-init-list mem-initializers before the fn body. */
26923 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
26925 cp_lexer_consume_token (parser->lexer);
26926 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
26928 /* cache_group will stop after an un-nested { } pair, too. */
26929 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
26930 break;
26932 /* variadic mem-inits have ... after the ')'. */
26933 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
26934 cp_lexer_consume_token (parser->lexer);
26937 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26938 /* Handle function try blocks. */
26939 if (function_try_block)
26940 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
26941 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
26942 last = parser->lexer->next_token;
26944 /* Save away the inline definition; we will process it when the
26945 class is complete. */
26946 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
26947 DECL_PENDING_INLINE_P (fn) = 1;
26949 /* We need to know that this was defined in the class, so that
26950 friend templates are handled correctly. */
26951 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
26953 /* Add FN to the queue of functions to be parsed later. */
26954 vec_safe_push (unparsed_funs_with_definitions, fn);
26956 return fn;
26959 /* Save the tokens that make up the in-class initializer for a non-static
26960 data member. Returns a DEFAULT_ARG. */
26962 static tree
26963 cp_parser_save_nsdmi (cp_parser* parser)
26965 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
26968 /* Parse a template-argument-list, as well as the trailing ">" (but
26969 not the opening "<"). See cp_parser_template_argument_list for the
26970 return value. */
26972 static tree
26973 cp_parser_enclosed_template_argument_list (cp_parser* parser)
26975 tree arguments;
26976 tree saved_scope;
26977 tree saved_qualifying_scope;
26978 tree saved_object_scope;
26979 bool saved_greater_than_is_operator_p;
26980 int saved_unevaluated_operand;
26981 int saved_inhibit_evaluation_warnings;
26983 /* [temp.names]
26985 When parsing a template-id, the first non-nested `>' is taken as
26986 the end of the template-argument-list rather than a greater-than
26987 operator. */
26988 saved_greater_than_is_operator_p
26989 = parser->greater_than_is_operator_p;
26990 parser->greater_than_is_operator_p = false;
26991 /* Parsing the argument list may modify SCOPE, so we save it
26992 here. */
26993 saved_scope = parser->scope;
26994 saved_qualifying_scope = parser->qualifying_scope;
26995 saved_object_scope = parser->object_scope;
26996 /* We need to evaluate the template arguments, even though this
26997 template-id may be nested within a "sizeof". */
26998 saved_unevaluated_operand = cp_unevaluated_operand;
26999 cp_unevaluated_operand = 0;
27000 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27001 c_inhibit_evaluation_warnings = 0;
27002 /* Parse the template-argument-list itself. */
27003 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27004 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27005 arguments = NULL_TREE;
27006 else
27007 arguments = cp_parser_template_argument_list (parser);
27008 /* Look for the `>' that ends the template-argument-list. If we find
27009 a '>>' instead, it's probably just a typo. */
27010 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27012 if (cxx_dialect != cxx98)
27014 /* In C++0x, a `>>' in a template argument list or cast
27015 expression is considered to be two separate `>'
27016 tokens. So, change the current token to a `>', but don't
27017 consume it: it will be consumed later when the outer
27018 template argument list (or cast expression) is parsed.
27019 Note that this replacement of `>' for `>>' is necessary
27020 even if we are parsing tentatively: in the tentative
27021 case, after calling
27022 cp_parser_enclosed_template_argument_list we will always
27023 throw away all of the template arguments and the first
27024 closing `>', either because the template argument list
27025 was erroneous or because we are replacing those tokens
27026 with a CPP_TEMPLATE_ID token. The second `>' (which will
27027 not have been thrown away) is needed either to close an
27028 outer template argument list or to complete a new-style
27029 cast. */
27030 cp_token *token = cp_lexer_peek_token (parser->lexer);
27031 token->type = CPP_GREATER;
27033 else if (!saved_greater_than_is_operator_p)
27035 /* If we're in a nested template argument list, the '>>' has
27036 to be a typo for '> >'. We emit the error message, but we
27037 continue parsing and we push a '>' as next token, so that
27038 the argument list will be parsed correctly. Note that the
27039 global source location is still on the token before the
27040 '>>', so we need to say explicitly where we want it. */
27041 cp_token *token = cp_lexer_peek_token (parser->lexer);
27042 gcc_rich_location richloc (token->location);
27043 richloc.add_fixit_replace ("> >");
27044 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27045 "within a nested template argument list");
27047 token->type = CPP_GREATER;
27049 else
27051 /* If this is not a nested template argument list, the '>>'
27052 is a typo for '>'. Emit an error message and continue.
27053 Same deal about the token location, but here we can get it
27054 right by consuming the '>>' before issuing the diagnostic. */
27055 cp_token *token = cp_lexer_consume_token (parser->lexer);
27056 error_at (token->location,
27057 "spurious %<>>%>, use %<>%> to terminate "
27058 "a template argument list");
27061 else
27062 cp_parser_skip_to_end_of_template_parameter_list (parser);
27063 /* The `>' token might be a greater-than operator again now. */
27064 parser->greater_than_is_operator_p
27065 = saved_greater_than_is_operator_p;
27066 /* Restore the SAVED_SCOPE. */
27067 parser->scope = saved_scope;
27068 parser->qualifying_scope = saved_qualifying_scope;
27069 parser->object_scope = saved_object_scope;
27070 cp_unevaluated_operand = saved_unevaluated_operand;
27071 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27073 return arguments;
27076 /* MEMBER_FUNCTION is a member function, or a friend. If default
27077 arguments, or the body of the function have not yet been parsed,
27078 parse them now. */
27080 static void
27081 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27083 timevar_push (TV_PARSE_INMETH);
27084 /* If this member is a template, get the underlying
27085 FUNCTION_DECL. */
27086 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27087 member_function = DECL_TEMPLATE_RESULT (member_function);
27089 /* There should not be any class definitions in progress at this
27090 point; the bodies of members are only parsed outside of all class
27091 definitions. */
27092 gcc_assert (parser->num_classes_being_defined == 0);
27093 /* While we're parsing the member functions we might encounter more
27094 classes. We want to handle them right away, but we don't want
27095 them getting mixed up with functions that are currently in the
27096 queue. */
27097 push_unparsed_function_queues (parser);
27099 /* Make sure that any template parameters are in scope. */
27100 maybe_begin_member_template_processing (member_function);
27102 /* If the body of the function has not yet been parsed, parse it
27103 now. */
27104 if (DECL_PENDING_INLINE_P (member_function))
27106 tree function_scope;
27107 cp_token_cache *tokens;
27109 /* The function is no longer pending; we are processing it. */
27110 tokens = DECL_PENDING_INLINE_INFO (member_function);
27111 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27112 DECL_PENDING_INLINE_P (member_function) = 0;
27114 /* If this is a local class, enter the scope of the containing
27115 function. */
27116 function_scope = current_function_decl;
27117 if (function_scope)
27118 push_function_context ();
27120 /* Push the body of the function onto the lexer stack. */
27121 cp_parser_push_lexer_for_tokens (parser, tokens);
27123 /* Let the front end know that we going to be defining this
27124 function. */
27125 start_preparsed_function (member_function, NULL_TREE,
27126 SF_PRE_PARSED | SF_INCLASS_INLINE);
27128 /* Don't do access checking if it is a templated function. */
27129 if (processing_template_decl)
27130 push_deferring_access_checks (dk_no_check);
27132 /* #pragma omp declare reduction needs special parsing. */
27133 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27135 parser->lexer->in_pragma = true;
27136 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27137 finish_function (/*inline*/2);
27138 cp_check_omp_declare_reduction (member_function);
27140 else
27141 /* Now, parse the body of the function. */
27142 cp_parser_function_definition_after_declarator (parser,
27143 /*inline_p=*/true);
27145 if (processing_template_decl)
27146 pop_deferring_access_checks ();
27148 /* Leave the scope of the containing function. */
27149 if (function_scope)
27150 pop_function_context ();
27151 cp_parser_pop_lexer (parser);
27154 /* Remove any template parameters from the symbol table. */
27155 maybe_end_member_template_processing ();
27157 /* Restore the queue. */
27158 pop_unparsed_function_queues (parser);
27159 timevar_pop (TV_PARSE_INMETH);
27162 /* If DECL contains any default args, remember it on the unparsed
27163 functions queue. */
27165 static void
27166 cp_parser_save_default_args (cp_parser* parser, tree decl)
27168 tree probe;
27170 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27171 probe;
27172 probe = TREE_CHAIN (probe))
27173 if (TREE_PURPOSE (probe))
27175 cp_default_arg_entry entry = {current_class_type, decl};
27176 vec_safe_push (unparsed_funs_with_default_args, entry);
27177 break;
27181 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27182 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27183 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27184 from the parameter-type-list. */
27186 static tree
27187 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27188 tree default_arg, tree parmtype)
27190 cp_token_cache *tokens;
27191 tree parsed_arg;
27192 bool dummy;
27194 if (default_arg == error_mark_node)
27195 return error_mark_node;
27197 /* Push the saved tokens for the default argument onto the parser's
27198 lexer stack. */
27199 tokens = DEFARG_TOKENS (default_arg);
27200 cp_parser_push_lexer_for_tokens (parser, tokens);
27202 start_lambda_scope (decl);
27204 /* Parse the default argument. */
27205 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27206 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27207 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27209 finish_lambda_scope ();
27211 if (parsed_arg == error_mark_node)
27212 cp_parser_skip_to_end_of_statement (parser);
27214 if (!processing_template_decl)
27216 /* In a non-template class, check conversions now. In a template,
27217 we'll wait and instantiate these as needed. */
27218 if (TREE_CODE (decl) == PARM_DECL)
27219 parsed_arg = check_default_argument (parmtype, parsed_arg,
27220 tf_warning_or_error);
27221 else
27222 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27225 /* If the token stream has not been completely used up, then
27226 there was extra junk after the end of the default
27227 argument. */
27228 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27230 if (TREE_CODE (decl) == PARM_DECL)
27231 cp_parser_error (parser, "expected %<,%>");
27232 else
27233 cp_parser_error (parser, "expected %<;%>");
27236 /* Revert to the main lexer. */
27237 cp_parser_pop_lexer (parser);
27239 return parsed_arg;
27242 /* FIELD is a non-static data member with an initializer which we saved for
27243 later; parse it now. */
27245 static void
27246 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27248 tree def;
27250 maybe_begin_member_template_processing (field);
27252 push_unparsed_function_queues (parser);
27253 def = cp_parser_late_parse_one_default_arg (parser, field,
27254 DECL_INITIAL (field),
27255 NULL_TREE);
27256 pop_unparsed_function_queues (parser);
27258 maybe_end_member_template_processing ();
27260 DECL_INITIAL (field) = def;
27263 /* FN is a FUNCTION_DECL which may contains a parameter with an
27264 unparsed DEFAULT_ARG. Parse the default args now. This function
27265 assumes that the current scope is the scope in which the default
27266 argument should be processed. */
27268 static void
27269 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27271 bool saved_local_variables_forbidden_p;
27272 tree parm, parmdecl;
27274 /* While we're parsing the default args, we might (due to the
27275 statement expression extension) encounter more classes. We want
27276 to handle them right away, but we don't want them getting mixed
27277 up with default args that are currently in the queue. */
27278 push_unparsed_function_queues (parser);
27280 /* Local variable names (and the `this' keyword) may not appear
27281 in a default argument. */
27282 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27283 parser->local_variables_forbidden_p = true;
27285 push_defarg_context (fn);
27287 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27288 parmdecl = DECL_ARGUMENTS (fn);
27289 parm && parm != void_list_node;
27290 parm = TREE_CHAIN (parm),
27291 parmdecl = DECL_CHAIN (parmdecl))
27293 tree default_arg = TREE_PURPOSE (parm);
27294 tree parsed_arg;
27295 vec<tree, va_gc> *insts;
27296 tree copy;
27297 unsigned ix;
27299 if (!default_arg)
27300 continue;
27302 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27303 /* This can happen for a friend declaration for a function
27304 already declared with default arguments. */
27305 continue;
27307 parsed_arg
27308 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27309 default_arg,
27310 TREE_VALUE (parm));
27311 if (parsed_arg == error_mark_node)
27313 continue;
27316 TREE_PURPOSE (parm) = parsed_arg;
27318 /* Update any instantiations we've already created. */
27319 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27320 vec_safe_iterate (insts, ix, &copy); ix++)
27321 TREE_PURPOSE (copy) = parsed_arg;
27324 pop_defarg_context ();
27326 /* Make sure no default arg is missing. */
27327 check_default_args (fn);
27329 /* Restore the state of local_variables_forbidden_p. */
27330 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27332 /* Restore the queue. */
27333 pop_unparsed_function_queues (parser);
27336 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27338 sizeof ... ( identifier )
27340 where the 'sizeof' token has already been consumed. */
27342 static tree
27343 cp_parser_sizeof_pack (cp_parser *parser)
27345 /* Consume the `...'. */
27346 cp_lexer_consume_token (parser->lexer);
27347 maybe_warn_variadic_templates ();
27349 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27350 if (paren)
27351 cp_lexer_consume_token (parser->lexer);
27352 else
27353 permerror (cp_lexer_peek_token (parser->lexer)->location,
27354 "%<sizeof...%> argument must be surrounded by parentheses");
27356 cp_token *token = cp_lexer_peek_token (parser->lexer);
27357 tree name = cp_parser_identifier (parser);
27358 if (name == error_mark_node)
27359 return error_mark_node;
27360 /* The name is not qualified. */
27361 parser->scope = NULL_TREE;
27362 parser->qualifying_scope = NULL_TREE;
27363 parser->object_scope = NULL_TREE;
27364 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27365 if (expr == error_mark_node)
27366 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27367 token->location);
27368 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27369 expr = TREE_TYPE (expr);
27370 else if (TREE_CODE (expr) == CONST_DECL)
27371 expr = DECL_INITIAL (expr);
27372 expr = make_pack_expansion (expr);
27373 PACK_EXPANSION_SIZEOF_P (expr) = true;
27375 if (paren)
27376 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27378 return expr;
27381 /* Parse the operand of `sizeof' (or a similar operator). Returns
27382 either a TYPE or an expression, depending on the form of the
27383 input. The KEYWORD indicates which kind of expression we have
27384 encountered. */
27386 static tree
27387 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27389 tree expr = NULL_TREE;
27390 const char *saved_message;
27391 char *tmp;
27392 bool saved_integral_constant_expression_p;
27393 bool saved_non_integral_constant_expression_p;
27395 /* If it's a `...', then we are computing the length of a parameter
27396 pack. */
27397 if (keyword == RID_SIZEOF
27398 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27399 return cp_parser_sizeof_pack (parser);
27401 /* Types cannot be defined in a `sizeof' expression. Save away the
27402 old message. */
27403 saved_message = parser->type_definition_forbidden_message;
27404 /* And create the new one. */
27405 tmp = concat ("types may not be defined in %<",
27406 IDENTIFIER_POINTER (ridpointers[keyword]),
27407 "%> expressions", NULL);
27408 parser->type_definition_forbidden_message = tmp;
27410 /* The restrictions on constant-expressions do not apply inside
27411 sizeof expressions. */
27412 saved_integral_constant_expression_p
27413 = parser->integral_constant_expression_p;
27414 saved_non_integral_constant_expression_p
27415 = parser->non_integral_constant_expression_p;
27416 parser->integral_constant_expression_p = false;
27418 /* Do not actually evaluate the expression. */
27419 ++cp_unevaluated_operand;
27420 ++c_inhibit_evaluation_warnings;
27421 /* If it's a `(', then we might be looking at the type-id
27422 construction. */
27423 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27425 tree type = NULL_TREE;
27427 /* We can't be sure yet whether we're looking at a type-id or an
27428 expression. */
27429 cp_parser_parse_tentatively (parser);
27430 /* Note: as a GNU Extension, compound literals are considered
27431 postfix-expressions as they are in C99, so they are valid
27432 arguments to sizeof. See comment in cp_parser_cast_expression
27433 for details. */
27434 if (cp_parser_compound_literal_p (parser))
27435 cp_parser_simulate_error (parser);
27436 else
27438 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27439 parser->in_type_id_in_expr_p = true;
27440 /* Look for the type-id. */
27441 type = cp_parser_type_id (parser);
27442 /* Look for the closing `)'. */
27443 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27444 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27447 /* If all went well, then we're done. */
27448 if (cp_parser_parse_definitely (parser))
27450 cp_decl_specifier_seq decl_specs;
27452 /* Build a trivial decl-specifier-seq. */
27453 clear_decl_specs (&decl_specs);
27454 decl_specs.type = type;
27456 /* Call grokdeclarator to figure out what type this is. */
27457 expr = grokdeclarator (NULL,
27458 &decl_specs,
27459 TYPENAME,
27460 /*initialized=*/0,
27461 /*attrlist=*/NULL);
27465 /* If the type-id production did not work out, then we must be
27466 looking at the unary-expression production. */
27467 if (!expr)
27468 expr = cp_parser_unary_expression (parser);
27470 /* Go back to evaluating expressions. */
27471 --cp_unevaluated_operand;
27472 --c_inhibit_evaluation_warnings;
27474 /* Free the message we created. */
27475 free (tmp);
27476 /* And restore the old one. */
27477 parser->type_definition_forbidden_message = saved_message;
27478 parser->integral_constant_expression_p
27479 = saved_integral_constant_expression_p;
27480 parser->non_integral_constant_expression_p
27481 = saved_non_integral_constant_expression_p;
27483 return expr;
27486 /* If the current declaration has no declarator, return true. */
27488 static bool
27489 cp_parser_declares_only_class_p (cp_parser *parser)
27491 /* If the next token is a `;' or a `,' then there is no
27492 declarator. */
27493 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27494 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27497 /* Update the DECL_SPECS to reflect the storage class indicated by
27498 KEYWORD. */
27500 static void
27501 cp_parser_set_storage_class (cp_parser *parser,
27502 cp_decl_specifier_seq *decl_specs,
27503 enum rid keyword,
27504 cp_token *token)
27506 cp_storage_class storage_class;
27508 if (parser->in_unbraced_linkage_specification_p)
27510 error_at (token->location, "invalid use of %qD in linkage specification",
27511 ridpointers[keyword]);
27512 return;
27514 else if (decl_specs->storage_class != sc_none)
27516 decl_specs->conflicting_specifiers_p = true;
27517 return;
27520 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27521 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27522 && decl_specs->gnu_thread_keyword_p)
27524 pedwarn (decl_specs->locations[ds_thread], 0,
27525 "%<__thread%> before %qD", ridpointers[keyword]);
27528 switch (keyword)
27530 case RID_AUTO:
27531 storage_class = sc_auto;
27532 break;
27533 case RID_REGISTER:
27534 storage_class = sc_register;
27535 break;
27536 case RID_STATIC:
27537 storage_class = sc_static;
27538 break;
27539 case RID_EXTERN:
27540 storage_class = sc_extern;
27541 break;
27542 case RID_MUTABLE:
27543 storage_class = sc_mutable;
27544 break;
27545 default:
27546 gcc_unreachable ();
27548 decl_specs->storage_class = storage_class;
27549 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27551 /* A storage class specifier cannot be applied alongside a typedef
27552 specifier. If there is a typedef specifier present then set
27553 conflicting_specifiers_p which will trigger an error later
27554 on in grokdeclarator. */
27555 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27556 decl_specs->conflicting_specifiers_p = true;
27559 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27560 is true, the type is a class or enum definition. */
27562 static void
27563 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27564 tree type_spec,
27565 cp_token *token,
27566 bool type_definition_p)
27568 decl_specs->any_specifiers_p = true;
27570 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27571 (with, for example, in "typedef int wchar_t;") we remember that
27572 this is what happened. In system headers, we ignore these
27573 declarations so that G++ can work with system headers that are not
27574 C++-safe. */
27575 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27576 && !type_definition_p
27577 && (type_spec == boolean_type_node
27578 || type_spec == char16_type_node
27579 || type_spec == char32_type_node
27580 || type_spec == wchar_type_node)
27581 && (decl_specs->type
27582 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27583 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27584 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27585 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27587 decl_specs->redefined_builtin_type = type_spec;
27588 set_and_check_decl_spec_loc (decl_specs,
27589 ds_redefined_builtin_type_spec,
27590 token);
27591 if (!decl_specs->type)
27593 decl_specs->type = type_spec;
27594 decl_specs->type_definition_p = false;
27595 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27598 else if (decl_specs->type)
27599 decl_specs->multiple_types_p = true;
27600 else
27602 decl_specs->type = type_spec;
27603 decl_specs->type_definition_p = type_definition_p;
27604 decl_specs->redefined_builtin_type = NULL_TREE;
27605 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27609 /* True iff TOKEN is the GNU keyword __thread. */
27611 static bool
27612 token_is__thread (cp_token *token)
27614 gcc_assert (token->keyword == RID_THREAD);
27615 return !strcmp (IDENTIFIER_POINTER (token->u.value), "__thread");
27618 /* Set the location for a declarator specifier and check if it is
27619 duplicated.
27621 DECL_SPECS is the sequence of declarator specifiers onto which to
27622 set the location.
27624 DS is the single declarator specifier to set which location is to
27625 be set onto the existing sequence of declarators.
27627 LOCATION is the location for the declarator specifier to
27628 consider. */
27630 static void
27631 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27632 cp_decl_spec ds, cp_token *token)
27634 gcc_assert (ds < ds_last);
27636 if (decl_specs == NULL)
27637 return;
27639 source_location location = token->location;
27641 if (decl_specs->locations[ds] == 0)
27643 decl_specs->locations[ds] = location;
27644 if (ds == ds_thread)
27645 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27647 else
27649 if (ds == ds_long)
27651 if (decl_specs->locations[ds_long_long] != 0)
27652 error_at (location,
27653 "%<long long long%> is too long for GCC");
27654 else
27656 decl_specs->locations[ds_long_long] = location;
27657 pedwarn_cxx98 (location,
27658 OPT_Wlong_long,
27659 "ISO C++ 1998 does not support %<long long%>");
27662 else if (ds == ds_thread)
27664 bool gnu = token_is__thread (token);
27665 if (gnu != decl_specs->gnu_thread_keyword_p)
27666 error_at (location,
27667 "both %<__thread%> and %<thread_local%> specified");
27668 else
27669 error_at (location, "duplicate %qD", token->u.value);
27671 else
27673 static const char *const decl_spec_names[] = {
27674 "signed",
27675 "unsigned",
27676 "short",
27677 "long",
27678 "const",
27679 "volatile",
27680 "restrict",
27681 "inline",
27682 "virtual",
27683 "explicit",
27684 "friend",
27685 "typedef",
27686 "using",
27687 "constexpr",
27688 "__complex"
27690 error_at (location,
27691 "duplicate %qs", decl_spec_names[ds]);
27696 /* Return true iff the declarator specifier DS is present in the
27697 sequence of declarator specifiers DECL_SPECS. */
27699 bool
27700 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27701 cp_decl_spec ds)
27703 gcc_assert (ds < ds_last);
27705 if (decl_specs == NULL)
27706 return false;
27708 return decl_specs->locations[ds] != 0;
27711 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27712 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27714 static bool
27715 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27717 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27720 /* Issue an error message indicating that TOKEN_DESC was expected.
27721 If KEYWORD is true, it indicated this function is called by
27722 cp_parser_require_keword and the required token can only be
27723 a indicated keyword. */
27725 static void
27726 cp_parser_required_error (cp_parser *parser,
27727 required_token token_desc,
27728 bool keyword)
27730 switch (token_desc)
27732 case RT_NEW:
27733 cp_parser_error (parser, "expected %<new%>");
27734 return;
27735 case RT_DELETE:
27736 cp_parser_error (parser, "expected %<delete%>");
27737 return;
27738 case RT_RETURN:
27739 cp_parser_error (parser, "expected %<return%>");
27740 return;
27741 case RT_WHILE:
27742 cp_parser_error (parser, "expected %<while%>");
27743 return;
27744 case RT_EXTERN:
27745 cp_parser_error (parser, "expected %<extern%>");
27746 return;
27747 case RT_STATIC_ASSERT:
27748 cp_parser_error (parser, "expected %<static_assert%>");
27749 return;
27750 case RT_DECLTYPE:
27751 cp_parser_error (parser, "expected %<decltype%>");
27752 return;
27753 case RT_OPERATOR:
27754 cp_parser_error (parser, "expected %<operator%>");
27755 return;
27756 case RT_CLASS:
27757 cp_parser_error (parser, "expected %<class%>");
27758 return;
27759 case RT_TEMPLATE:
27760 cp_parser_error (parser, "expected %<template%>");
27761 return;
27762 case RT_NAMESPACE:
27763 cp_parser_error (parser, "expected %<namespace%>");
27764 return;
27765 case RT_USING:
27766 cp_parser_error (parser, "expected %<using%>");
27767 return;
27768 case RT_ASM:
27769 cp_parser_error (parser, "expected %<asm%>");
27770 return;
27771 case RT_TRY:
27772 cp_parser_error (parser, "expected %<try%>");
27773 return;
27774 case RT_CATCH:
27775 cp_parser_error (parser, "expected %<catch%>");
27776 return;
27777 case RT_THROW:
27778 cp_parser_error (parser, "expected %<throw%>");
27779 return;
27780 case RT_LABEL:
27781 cp_parser_error (parser, "expected %<__label__%>");
27782 return;
27783 case RT_AT_TRY:
27784 cp_parser_error (parser, "expected %<@try%>");
27785 return;
27786 case RT_AT_SYNCHRONIZED:
27787 cp_parser_error (parser, "expected %<@synchronized%>");
27788 return;
27789 case RT_AT_THROW:
27790 cp_parser_error (parser, "expected %<@throw%>");
27791 return;
27792 case RT_TRANSACTION_ATOMIC:
27793 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27794 return;
27795 case RT_TRANSACTION_RELAXED:
27796 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27797 return;
27798 default:
27799 break;
27801 if (!keyword)
27803 switch (token_desc)
27805 case RT_SEMICOLON:
27806 cp_parser_error (parser, "expected %<;%>");
27807 return;
27808 case RT_OPEN_PAREN:
27809 cp_parser_error (parser, "expected %<(%>");
27810 return;
27811 case RT_CLOSE_BRACE:
27812 cp_parser_error (parser, "expected %<}%>");
27813 return;
27814 case RT_OPEN_BRACE:
27815 cp_parser_error (parser, "expected %<{%>");
27816 return;
27817 case RT_CLOSE_SQUARE:
27818 cp_parser_error (parser, "expected %<]%>");
27819 return;
27820 case RT_OPEN_SQUARE:
27821 cp_parser_error (parser, "expected %<[%>");
27822 return;
27823 case RT_COMMA:
27824 cp_parser_error (parser, "expected %<,%>");
27825 return;
27826 case RT_SCOPE:
27827 cp_parser_error (parser, "expected %<::%>");
27828 return;
27829 case RT_LESS:
27830 cp_parser_error (parser, "expected %<<%>");
27831 return;
27832 case RT_GREATER:
27833 cp_parser_error (parser, "expected %<>%>");
27834 return;
27835 case RT_EQ:
27836 cp_parser_error (parser, "expected %<=%>");
27837 return;
27838 case RT_ELLIPSIS:
27839 cp_parser_error (parser, "expected %<...%>");
27840 return;
27841 case RT_MULT:
27842 cp_parser_error (parser, "expected %<*%>");
27843 return;
27844 case RT_COMPL:
27845 cp_parser_error (parser, "expected %<~%>");
27846 return;
27847 case RT_COLON:
27848 cp_parser_error (parser, "expected %<:%>");
27849 return;
27850 case RT_COLON_SCOPE:
27851 cp_parser_error (parser, "expected %<:%> or %<::%>");
27852 return;
27853 case RT_CLOSE_PAREN:
27854 cp_parser_error (parser, "expected %<)%>");
27855 return;
27856 case RT_COMMA_CLOSE_PAREN:
27857 cp_parser_error (parser, "expected %<,%> or %<)%>");
27858 return;
27859 case RT_PRAGMA_EOL:
27860 cp_parser_error (parser, "expected end of line");
27861 return;
27862 case RT_NAME:
27863 cp_parser_error (parser, "expected identifier");
27864 return;
27865 case RT_SELECT:
27866 cp_parser_error (parser, "expected selection-statement");
27867 return;
27868 case RT_INTERATION:
27869 cp_parser_error (parser, "expected iteration-statement");
27870 return;
27871 case RT_JUMP:
27872 cp_parser_error (parser, "expected jump-statement");
27873 return;
27874 case RT_CLASS_KEY:
27875 cp_parser_error (parser, "expected class-key");
27876 return;
27877 case RT_CLASS_TYPENAME_TEMPLATE:
27878 cp_parser_error (parser,
27879 "expected %<class%>, %<typename%>, or %<template%>");
27880 return;
27881 default:
27882 gcc_unreachable ();
27885 else
27886 gcc_unreachable ();
27891 /* If the next token is of the indicated TYPE, consume it. Otherwise,
27892 issue an error message indicating that TOKEN_DESC was expected.
27894 Returns the token consumed, if the token had the appropriate type.
27895 Otherwise, returns NULL. */
27897 static cp_token *
27898 cp_parser_require (cp_parser* parser,
27899 enum cpp_ttype type,
27900 required_token token_desc)
27902 if (cp_lexer_next_token_is (parser->lexer, type))
27903 return cp_lexer_consume_token (parser->lexer);
27904 else
27906 /* Output the MESSAGE -- unless we're parsing tentatively. */
27907 if (!cp_parser_simulate_error (parser))
27908 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
27909 return NULL;
27913 /* An error message is produced if the next token is not '>'.
27914 All further tokens are skipped until the desired token is
27915 found or '{', '}', ';' or an unbalanced ')' or ']'. */
27917 static void
27918 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
27920 /* Current level of '< ... >'. */
27921 unsigned level = 0;
27922 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
27923 unsigned nesting_depth = 0;
27925 /* Are we ready, yet? If not, issue error message. */
27926 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
27927 return;
27929 /* Skip tokens until the desired token is found. */
27930 while (true)
27932 /* Peek at the next token. */
27933 switch (cp_lexer_peek_token (parser->lexer)->type)
27935 case CPP_LESS:
27936 if (!nesting_depth)
27937 ++level;
27938 break;
27940 case CPP_RSHIFT:
27941 if (cxx_dialect == cxx98)
27942 /* C++0x views the `>>' operator as two `>' tokens, but
27943 C++98 does not. */
27944 break;
27945 else if (!nesting_depth && level-- == 0)
27947 /* We've hit a `>>' where the first `>' closes the
27948 template argument list, and the second `>' is
27949 spurious. Just consume the `>>' and stop; we've
27950 already produced at least one error. */
27951 cp_lexer_consume_token (parser->lexer);
27952 return;
27954 /* Fall through for C++0x, so we handle the second `>' in
27955 the `>>'. */
27956 gcc_fallthrough ();
27958 case CPP_GREATER:
27959 if (!nesting_depth && level-- == 0)
27961 /* We've reached the token we want, consume it and stop. */
27962 cp_lexer_consume_token (parser->lexer);
27963 return;
27965 break;
27967 case CPP_OPEN_PAREN:
27968 case CPP_OPEN_SQUARE:
27969 ++nesting_depth;
27970 break;
27972 case CPP_CLOSE_PAREN:
27973 case CPP_CLOSE_SQUARE:
27974 if (nesting_depth-- == 0)
27975 return;
27976 break;
27978 case CPP_EOF:
27979 case CPP_PRAGMA_EOL:
27980 case CPP_SEMICOLON:
27981 case CPP_OPEN_BRACE:
27982 case CPP_CLOSE_BRACE:
27983 /* The '>' was probably forgotten, don't look further. */
27984 return;
27986 default:
27987 break;
27990 /* Consume this token. */
27991 cp_lexer_consume_token (parser->lexer);
27995 /* If the next token is the indicated keyword, consume it. Otherwise,
27996 issue an error message indicating that TOKEN_DESC was expected.
27998 Returns the token consumed, if the token had the appropriate type.
27999 Otherwise, returns NULL. */
28001 static cp_token *
28002 cp_parser_require_keyword (cp_parser* parser,
28003 enum rid keyword,
28004 required_token token_desc)
28006 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28008 if (token && token->keyword != keyword)
28010 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28011 return NULL;
28014 return token;
28017 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28018 function-definition. */
28020 static bool
28021 cp_parser_token_starts_function_definition_p (cp_token* token)
28023 return (/* An ordinary function-body begins with an `{'. */
28024 token->type == CPP_OPEN_BRACE
28025 /* A ctor-initializer begins with a `:'. */
28026 || token->type == CPP_COLON
28027 /* A function-try-block begins with `try'. */
28028 || token->keyword == RID_TRY
28029 /* A function-transaction-block begins with `__transaction_atomic'
28030 or `__transaction_relaxed'. */
28031 || token->keyword == RID_TRANSACTION_ATOMIC
28032 || token->keyword == RID_TRANSACTION_RELAXED
28033 /* The named return value extension begins with `return'. */
28034 || token->keyword == RID_RETURN);
28037 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28038 definition. */
28040 static bool
28041 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28043 cp_token *token;
28045 token = cp_lexer_peek_token (parser->lexer);
28046 return (token->type == CPP_OPEN_BRACE
28047 || (token->type == CPP_COLON
28048 && !parser->colon_doesnt_start_class_def_p));
28051 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28052 C++0x) ending a template-argument. */
28054 static bool
28055 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28057 cp_token *token;
28059 token = cp_lexer_peek_token (parser->lexer);
28060 return (token->type == CPP_COMMA
28061 || token->type == CPP_GREATER
28062 || token->type == CPP_ELLIPSIS
28063 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28066 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28067 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28069 static bool
28070 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28071 size_t n)
28073 cp_token *token;
28075 token = cp_lexer_peek_nth_token (parser->lexer, n);
28076 if (token->type == CPP_LESS)
28077 return true;
28078 /* Check for the sequence `<::' in the original code. It would be lexed as
28079 `[:', where `[' is a digraph, and there is no whitespace before
28080 `:'. */
28081 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28083 cp_token *token2;
28084 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28085 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28086 return true;
28088 return false;
28091 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28092 or none_type otherwise. */
28094 static enum tag_types
28095 cp_parser_token_is_class_key (cp_token* token)
28097 switch (token->keyword)
28099 case RID_CLASS:
28100 return class_type;
28101 case RID_STRUCT:
28102 return record_type;
28103 case RID_UNION:
28104 return union_type;
28106 default:
28107 return none_type;
28111 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28112 or none_type otherwise or if the token is null. */
28114 static enum tag_types
28115 cp_parser_token_is_type_parameter_key (cp_token* token)
28117 if (!token)
28118 return none_type;
28120 switch (token->keyword)
28122 case RID_CLASS:
28123 return class_type;
28124 case RID_TYPENAME:
28125 return typename_type;
28127 default:
28128 return none_type;
28132 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28134 static void
28135 cp_parser_check_class_key (enum tag_types class_key, tree type)
28137 if (type == error_mark_node)
28138 return;
28139 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28141 if (permerror (input_location, "%qs tag used in naming %q#T",
28142 class_key == union_type ? "union"
28143 : class_key == record_type ? "struct" : "class",
28144 type))
28145 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28146 "%q#T was previously declared here", type);
28150 /* Issue an error message if DECL is redeclared with different
28151 access than its original declaration [class.access.spec/3].
28152 This applies to nested classes, nested class templates and
28153 enumerations [class.mem/1]. */
28155 static void
28156 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28158 if (!decl
28159 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28160 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28161 return;
28163 if ((TREE_PRIVATE (decl)
28164 != (current_access_specifier == access_private_node))
28165 || (TREE_PROTECTED (decl)
28166 != (current_access_specifier == access_protected_node)))
28167 error_at (location, "%qD redeclared with different access", decl);
28170 /* Look for the `template' keyword, as a syntactic disambiguator.
28171 Return TRUE iff it is present, in which case it will be
28172 consumed. */
28174 static bool
28175 cp_parser_optional_template_keyword (cp_parser *parser)
28177 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28179 /* In C++98 the `template' keyword can only be used within templates;
28180 outside templates the parser can always figure out what is a
28181 template and what is not. In C++11, per the resolution of DR 468,
28182 `template' is allowed in cases where it is not strictly necessary. */
28183 if (!processing_template_decl
28184 && pedantic && cxx_dialect == cxx98)
28186 cp_token *token = cp_lexer_peek_token (parser->lexer);
28187 pedwarn (token->location, OPT_Wpedantic,
28188 "in C++98 %<template%> (as a disambiguator) is only "
28189 "allowed within templates");
28190 /* If this part of the token stream is rescanned, the same
28191 error message would be generated. So, we purge the token
28192 from the stream. */
28193 cp_lexer_purge_token (parser->lexer);
28194 return false;
28196 else
28198 /* Consume the `template' keyword. */
28199 cp_lexer_consume_token (parser->lexer);
28200 return true;
28203 return false;
28206 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28207 set PARSER->SCOPE, and perform other related actions. */
28209 static void
28210 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28212 struct tree_check *check_value;
28214 /* Get the stored value. */
28215 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28216 /* Set the scope from the stored value. */
28217 parser->scope = saved_checks_value (check_value);
28218 parser->qualifying_scope = check_value->qualifying_scope;
28219 parser->object_scope = NULL_TREE;
28222 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28223 encounter the end of a block before what we were looking for. */
28225 static bool
28226 cp_parser_cache_group (cp_parser *parser,
28227 enum cpp_ttype end,
28228 unsigned depth)
28230 while (true)
28232 cp_token *token = cp_lexer_peek_token (parser->lexer);
28234 /* Abort a parenthesized expression if we encounter a semicolon. */
28235 if ((end == CPP_CLOSE_PAREN || depth == 0)
28236 && token->type == CPP_SEMICOLON)
28237 return true;
28238 /* If we've reached the end of the file, stop. */
28239 if (token->type == CPP_EOF
28240 || (end != CPP_PRAGMA_EOL
28241 && token->type == CPP_PRAGMA_EOL))
28242 return true;
28243 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28244 /* We've hit the end of an enclosing block, so there's been some
28245 kind of syntax error. */
28246 return true;
28248 /* Consume the token. */
28249 cp_lexer_consume_token (parser->lexer);
28250 /* See if it starts a new group. */
28251 if (token->type == CPP_OPEN_BRACE)
28253 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28254 /* In theory this should probably check end == '}', but
28255 cp_parser_save_member_function_body needs it to exit
28256 after either '}' or ')' when called with ')'. */
28257 if (depth == 0)
28258 return false;
28260 else if (token->type == CPP_OPEN_PAREN)
28262 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28263 if (depth == 0 && end == CPP_CLOSE_PAREN)
28264 return false;
28266 else if (token->type == CPP_PRAGMA)
28267 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28268 else if (token->type == end)
28269 return false;
28273 /* Like above, for caching a default argument or NSDMI. Both of these are
28274 terminated by a non-nested comma, but it can be unclear whether or not a
28275 comma is nested in a template argument list unless we do more parsing.
28276 In order to handle this ambiguity, when we encounter a ',' after a '<'
28277 we try to parse what follows as a parameter-declaration-list (in the
28278 case of a default argument) or a member-declarator (in the case of an
28279 NSDMI). If that succeeds, then we stop caching. */
28281 static tree
28282 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28284 unsigned depth = 0;
28285 int maybe_template_id = 0;
28286 cp_token *first_token;
28287 cp_token *token;
28288 tree default_argument;
28290 /* Add tokens until we have processed the entire default
28291 argument. We add the range [first_token, token). */
28292 first_token = cp_lexer_peek_token (parser->lexer);
28293 if (first_token->type == CPP_OPEN_BRACE)
28295 /* For list-initialization, this is straightforward. */
28296 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28297 token = cp_lexer_peek_token (parser->lexer);
28299 else while (true)
28301 bool done = false;
28303 /* Peek at the next token. */
28304 token = cp_lexer_peek_token (parser->lexer);
28305 /* What we do depends on what token we have. */
28306 switch (token->type)
28308 /* In valid code, a default argument must be
28309 immediately followed by a `,' `)', or `...'. */
28310 case CPP_COMMA:
28311 if (depth == 0 && maybe_template_id)
28313 /* If we've seen a '<', we might be in a
28314 template-argument-list. Until Core issue 325 is
28315 resolved, we don't know how this situation ought
28316 to be handled, so try to DTRT. We check whether
28317 what comes after the comma is a valid parameter
28318 declaration list. If it is, then the comma ends
28319 the default argument; otherwise the default
28320 argument continues. */
28321 bool error = false;
28322 cp_token *peek;
28324 /* Set ITALP so cp_parser_parameter_declaration_list
28325 doesn't decide to commit to this parse. */
28326 bool saved_italp = parser->in_template_argument_list_p;
28327 parser->in_template_argument_list_p = true;
28329 cp_parser_parse_tentatively (parser);
28331 if (nsdmi)
28333 /* Parse declarators until we reach a non-comma or
28334 somthing that cannot be an initializer.
28335 Just checking whether we're looking at a single
28336 declarator is insufficient. Consider:
28337 int var = tuple<T,U>::x;
28338 The template parameter 'U' looks exactly like a
28339 declarator. */
28342 int ctor_dtor_or_conv_p;
28343 cp_lexer_consume_token (parser->lexer);
28344 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28345 &ctor_dtor_or_conv_p,
28346 /*parenthesized_p=*/NULL,
28347 /*member_p=*/true,
28348 /*friend_p=*/false);
28349 peek = cp_lexer_peek_token (parser->lexer);
28350 if (cp_parser_error_occurred (parser))
28351 break;
28353 while (peek->type == CPP_COMMA);
28354 /* If we met an '=' or ';' then the original comma
28355 was the end of the NSDMI. Otherwise assume
28356 we're still in the NSDMI. */
28357 error = (peek->type != CPP_EQ
28358 && peek->type != CPP_SEMICOLON);
28360 else
28362 cp_lexer_consume_token (parser->lexer);
28363 begin_scope (sk_function_parms, NULL_TREE);
28364 cp_parser_parameter_declaration_list (parser, &error);
28365 pop_bindings_and_leave_scope ();
28367 if (!cp_parser_error_occurred (parser) && !error)
28368 done = true;
28369 cp_parser_abort_tentative_parse (parser);
28371 parser->in_template_argument_list_p = saved_italp;
28372 break;
28374 /* FALLTHRU */
28375 case CPP_CLOSE_PAREN:
28376 case CPP_ELLIPSIS:
28377 /* If we run into a non-nested `;', `}', or `]',
28378 then the code is invalid -- but the default
28379 argument is certainly over. */
28380 case CPP_SEMICOLON:
28381 case CPP_CLOSE_BRACE:
28382 case CPP_CLOSE_SQUARE:
28383 if (depth == 0
28384 /* Handle correctly int n = sizeof ... ( p ); */
28385 && token->type != CPP_ELLIPSIS)
28386 done = true;
28387 /* Update DEPTH, if necessary. */
28388 else if (token->type == CPP_CLOSE_PAREN
28389 || token->type == CPP_CLOSE_BRACE
28390 || token->type == CPP_CLOSE_SQUARE)
28391 --depth;
28392 break;
28394 case CPP_OPEN_PAREN:
28395 case CPP_OPEN_SQUARE:
28396 case CPP_OPEN_BRACE:
28397 ++depth;
28398 break;
28400 case CPP_LESS:
28401 if (depth == 0)
28402 /* This might be the comparison operator, or it might
28403 start a template argument list. */
28404 ++maybe_template_id;
28405 break;
28407 case CPP_RSHIFT:
28408 if (cxx_dialect == cxx98)
28409 break;
28410 /* Fall through for C++0x, which treats the `>>'
28411 operator like two `>' tokens in certain
28412 cases. */
28413 gcc_fallthrough ();
28415 case CPP_GREATER:
28416 if (depth == 0)
28418 /* This might be an operator, or it might close a
28419 template argument list. But if a previous '<'
28420 started a template argument list, this will have
28421 closed it, so we can't be in one anymore. */
28422 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28423 if (maybe_template_id < 0)
28424 maybe_template_id = 0;
28426 break;
28428 /* If we run out of tokens, issue an error message. */
28429 case CPP_EOF:
28430 case CPP_PRAGMA_EOL:
28431 error_at (token->location, "file ends in default argument");
28432 return error_mark_node;
28434 case CPP_NAME:
28435 case CPP_SCOPE:
28436 /* In these cases, we should look for template-ids.
28437 For example, if the default argument is
28438 `X<int, double>()', we need to do name lookup to
28439 figure out whether or not `X' is a template; if
28440 so, the `,' does not end the default argument.
28442 That is not yet done. */
28443 break;
28445 default:
28446 break;
28449 /* If we've reached the end, stop. */
28450 if (done)
28451 break;
28453 /* Add the token to the token block. */
28454 token = cp_lexer_consume_token (parser->lexer);
28457 /* Create a DEFAULT_ARG to represent the unparsed default
28458 argument. */
28459 default_argument = make_node (DEFAULT_ARG);
28460 DEFARG_TOKENS (default_argument)
28461 = cp_token_cache_new (first_token, token);
28462 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28464 return default_argument;
28467 /* Begin parsing tentatively. We always save tokens while parsing
28468 tentatively so that if the tentative parsing fails we can restore the
28469 tokens. */
28471 static void
28472 cp_parser_parse_tentatively (cp_parser* parser)
28474 /* Enter a new parsing context. */
28475 parser->context = cp_parser_context_new (parser->context);
28476 /* Begin saving tokens. */
28477 cp_lexer_save_tokens (parser->lexer);
28478 /* In order to avoid repetitive access control error messages,
28479 access checks are queued up until we are no longer parsing
28480 tentatively. */
28481 push_deferring_access_checks (dk_deferred);
28484 /* Commit to the currently active tentative parse. */
28486 static void
28487 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28489 cp_parser_context *context;
28490 cp_lexer *lexer;
28492 /* Mark all of the levels as committed. */
28493 lexer = parser->lexer;
28494 for (context = parser->context; context->next; context = context->next)
28496 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28497 break;
28498 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28499 while (!cp_lexer_saving_tokens (lexer))
28500 lexer = lexer->next;
28501 cp_lexer_commit_tokens (lexer);
28505 /* Commit to the topmost currently active tentative parse.
28507 Note that this function shouldn't be called when there are
28508 irreversible side-effects while in a tentative state. For
28509 example, we shouldn't create a permanent entry in the symbol
28510 table, or issue an error message that might not apply if the
28511 tentative parse is aborted. */
28513 static void
28514 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28516 cp_parser_context *context = parser->context;
28517 cp_lexer *lexer = parser->lexer;
28519 if (context)
28521 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28522 return;
28523 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28525 while (!cp_lexer_saving_tokens (lexer))
28526 lexer = lexer->next;
28527 cp_lexer_commit_tokens (lexer);
28531 /* Abort the currently active tentative parse. All consumed tokens
28532 will be rolled back, and no diagnostics will be issued. */
28534 static void
28535 cp_parser_abort_tentative_parse (cp_parser* parser)
28537 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28538 || errorcount > 0);
28539 cp_parser_simulate_error (parser);
28540 /* Now, pretend that we want to see if the construct was
28541 successfully parsed. */
28542 cp_parser_parse_definitely (parser);
28545 /* Stop parsing tentatively. If a parse error has occurred, restore the
28546 token stream. Otherwise, commit to the tokens we have consumed.
28547 Returns true if no error occurred; false otherwise. */
28549 static bool
28550 cp_parser_parse_definitely (cp_parser* parser)
28552 bool error_occurred;
28553 cp_parser_context *context;
28555 /* Remember whether or not an error occurred, since we are about to
28556 destroy that information. */
28557 error_occurred = cp_parser_error_occurred (parser);
28558 /* Remove the topmost context from the stack. */
28559 context = parser->context;
28560 parser->context = context->next;
28561 /* If no parse errors occurred, commit to the tentative parse. */
28562 if (!error_occurred)
28564 /* Commit to the tokens read tentatively, unless that was
28565 already done. */
28566 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28567 cp_lexer_commit_tokens (parser->lexer);
28569 pop_to_parent_deferring_access_checks ();
28571 /* Otherwise, if errors occurred, roll back our state so that things
28572 are just as they were before we began the tentative parse. */
28573 else
28575 cp_lexer_rollback_tokens (parser->lexer);
28576 pop_deferring_access_checks ();
28578 /* Add the context to the front of the free list. */
28579 context->next = cp_parser_context_free_list;
28580 cp_parser_context_free_list = context;
28582 return !error_occurred;
28585 /* Returns true if we are parsing tentatively and are not committed to
28586 this tentative parse. */
28588 static bool
28589 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28591 return (cp_parser_parsing_tentatively (parser)
28592 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28595 /* Returns nonzero iff an error has occurred during the most recent
28596 tentative parse. */
28598 static bool
28599 cp_parser_error_occurred (cp_parser* parser)
28601 return (cp_parser_parsing_tentatively (parser)
28602 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28605 /* Returns nonzero if GNU extensions are allowed. */
28607 static bool
28608 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28610 return parser->allow_gnu_extensions_p;
28613 /* Objective-C++ Productions */
28616 /* Parse an Objective-C expression, which feeds into a primary-expression
28617 above.
28619 objc-expression:
28620 objc-message-expression
28621 objc-string-literal
28622 objc-encode-expression
28623 objc-protocol-expression
28624 objc-selector-expression
28626 Returns a tree representation of the expression. */
28628 static cp_expr
28629 cp_parser_objc_expression (cp_parser* parser)
28631 /* Try to figure out what kind of declaration is present. */
28632 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28634 switch (kwd->type)
28636 case CPP_OPEN_SQUARE:
28637 return cp_parser_objc_message_expression (parser);
28639 case CPP_OBJC_STRING:
28640 kwd = cp_lexer_consume_token (parser->lexer);
28641 return objc_build_string_object (kwd->u.value);
28643 case CPP_KEYWORD:
28644 switch (kwd->keyword)
28646 case RID_AT_ENCODE:
28647 return cp_parser_objc_encode_expression (parser);
28649 case RID_AT_PROTOCOL:
28650 return cp_parser_objc_protocol_expression (parser);
28652 case RID_AT_SELECTOR:
28653 return cp_parser_objc_selector_expression (parser);
28655 default:
28656 break;
28658 default:
28659 error_at (kwd->location,
28660 "misplaced %<@%D%> Objective-C++ construct",
28661 kwd->u.value);
28662 cp_parser_skip_to_end_of_block_or_statement (parser);
28665 return error_mark_node;
28668 /* Parse an Objective-C message expression.
28670 objc-message-expression:
28671 [ objc-message-receiver objc-message-args ]
28673 Returns a representation of an Objective-C message. */
28675 static tree
28676 cp_parser_objc_message_expression (cp_parser* parser)
28678 tree receiver, messageargs;
28680 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28681 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28682 receiver = cp_parser_objc_message_receiver (parser);
28683 messageargs = cp_parser_objc_message_args (parser);
28684 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28685 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28687 tree result = objc_build_message_expr (receiver, messageargs);
28689 /* Construct a location e.g.
28690 [self func1:5]
28691 ^~~~~~~~~~~~~~
28692 ranging from the '[' to the ']', with the caret at the start. */
28693 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28694 protected_set_expr_location (result, combined_loc);
28696 return result;
28699 /* Parse an objc-message-receiver.
28701 objc-message-receiver:
28702 expression
28703 simple-type-specifier
28705 Returns a representation of the type or expression. */
28707 static tree
28708 cp_parser_objc_message_receiver (cp_parser* parser)
28710 tree rcv;
28712 /* An Objective-C message receiver may be either (1) a type
28713 or (2) an expression. */
28714 cp_parser_parse_tentatively (parser);
28715 rcv = cp_parser_expression (parser);
28717 /* If that worked out, fine. */
28718 if (cp_parser_parse_definitely (parser))
28719 return rcv;
28721 cp_parser_parse_tentatively (parser);
28722 rcv = cp_parser_simple_type_specifier (parser,
28723 /*decl_specs=*/NULL,
28724 CP_PARSER_FLAGS_NONE);
28726 if (cp_parser_parse_definitely (parser))
28727 return objc_get_class_reference (rcv);
28729 cp_parser_error (parser, "objective-c++ message receiver expected");
28730 return error_mark_node;
28733 /* Parse the arguments and selectors comprising an Objective-C message.
28735 objc-message-args:
28736 objc-selector
28737 objc-selector-args
28738 objc-selector-args , objc-comma-args
28740 objc-selector-args:
28741 objc-selector [opt] : assignment-expression
28742 objc-selector-args objc-selector [opt] : assignment-expression
28744 objc-comma-args:
28745 assignment-expression
28746 objc-comma-args , assignment-expression
28748 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28749 selector arguments and TREE_VALUE containing a list of comma
28750 arguments. */
28752 static tree
28753 cp_parser_objc_message_args (cp_parser* parser)
28755 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28756 bool maybe_unary_selector_p = true;
28757 cp_token *token = cp_lexer_peek_token (parser->lexer);
28759 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28761 tree selector = NULL_TREE, arg;
28763 if (token->type != CPP_COLON)
28764 selector = cp_parser_objc_selector (parser);
28766 /* Detect if we have a unary selector. */
28767 if (maybe_unary_selector_p
28768 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28769 return build_tree_list (selector, NULL_TREE);
28771 maybe_unary_selector_p = false;
28772 cp_parser_require (parser, CPP_COLON, RT_COLON);
28773 arg = cp_parser_assignment_expression (parser);
28775 sel_args
28776 = chainon (sel_args,
28777 build_tree_list (selector, arg));
28779 token = cp_lexer_peek_token (parser->lexer);
28782 /* Handle non-selector arguments, if any. */
28783 while (token->type == CPP_COMMA)
28785 tree arg;
28787 cp_lexer_consume_token (parser->lexer);
28788 arg = cp_parser_assignment_expression (parser);
28790 addl_args
28791 = chainon (addl_args,
28792 build_tree_list (NULL_TREE, arg));
28794 token = cp_lexer_peek_token (parser->lexer);
28797 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28799 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28800 return build_tree_list (error_mark_node, error_mark_node);
28803 return build_tree_list (sel_args, addl_args);
28806 /* Parse an Objective-C encode expression.
28808 objc-encode-expression:
28809 @encode objc-typename
28811 Returns an encoded representation of the type argument. */
28813 static cp_expr
28814 cp_parser_objc_encode_expression (cp_parser* parser)
28816 tree type;
28817 cp_token *token;
28818 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28820 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28821 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28822 token = cp_lexer_peek_token (parser->lexer);
28823 type = complete_type (cp_parser_type_id (parser));
28824 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28826 if (!type)
28828 error_at (token->location,
28829 "%<@encode%> must specify a type as an argument");
28830 return error_mark_node;
28833 /* This happens if we find @encode(T) (where T is a template
28834 typename or something dependent on a template typename) when
28835 parsing a template. In that case, we can't compile it
28836 immediately, but we rather create an AT_ENCODE_EXPR which will
28837 need to be instantiated when the template is used.
28839 if (dependent_type_p (type))
28841 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
28842 TREE_READONLY (value) = 1;
28843 return value;
28847 /* Build a location of the form:
28848 @encode(int)
28849 ^~~~~~~~~~~~
28850 with caret==start at the @ token, finishing at the close paren. */
28851 location_t combined_loc
28852 = make_location (start_loc, start_loc,
28853 cp_lexer_previous_token (parser->lexer)->location);
28855 return cp_expr (objc_build_encode_expr (type), combined_loc);
28858 /* Parse an Objective-C @defs expression. */
28860 static tree
28861 cp_parser_objc_defs_expression (cp_parser *parser)
28863 tree name;
28865 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
28866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28867 name = cp_parser_identifier (parser);
28868 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28870 return objc_get_class_ivars (name);
28873 /* Parse an Objective-C protocol expression.
28875 objc-protocol-expression:
28876 @protocol ( identifier )
28878 Returns a representation of the protocol expression. */
28880 static tree
28881 cp_parser_objc_protocol_expression (cp_parser* parser)
28883 tree proto;
28884 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28886 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
28887 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28888 proto = cp_parser_identifier (parser);
28889 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28891 /* Build a location of the form:
28892 @protocol(prot)
28893 ^~~~~~~~~~~~~~~
28894 with caret==start at the @ token, finishing at the close paren. */
28895 location_t combined_loc
28896 = make_location (start_loc, start_loc,
28897 cp_lexer_previous_token (parser->lexer)->location);
28898 tree result = objc_build_protocol_expr (proto);
28899 protected_set_expr_location (result, combined_loc);
28900 return result;
28903 /* Parse an Objective-C selector expression.
28905 objc-selector-expression:
28906 @selector ( objc-method-signature )
28908 objc-method-signature:
28909 objc-selector
28910 objc-selector-seq
28912 objc-selector-seq:
28913 objc-selector :
28914 objc-selector-seq objc-selector :
28916 Returns a representation of the method selector. */
28918 static tree
28919 cp_parser_objc_selector_expression (cp_parser* parser)
28921 tree sel_seq = NULL_TREE;
28922 bool maybe_unary_selector_p = true;
28923 cp_token *token;
28924 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
28926 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
28927 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28928 token = cp_lexer_peek_token (parser->lexer);
28930 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
28931 || token->type == CPP_SCOPE)
28933 tree selector = NULL_TREE;
28935 if (token->type != CPP_COLON
28936 || token->type == CPP_SCOPE)
28937 selector = cp_parser_objc_selector (parser);
28939 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
28940 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
28942 /* Detect if we have a unary selector. */
28943 if (maybe_unary_selector_p)
28945 sel_seq = selector;
28946 goto finish_selector;
28948 else
28950 cp_parser_error (parser, "expected %<:%>");
28953 maybe_unary_selector_p = false;
28954 token = cp_lexer_consume_token (parser->lexer);
28956 if (token->type == CPP_SCOPE)
28958 sel_seq
28959 = chainon (sel_seq,
28960 build_tree_list (selector, NULL_TREE));
28961 sel_seq
28962 = chainon (sel_seq,
28963 build_tree_list (NULL_TREE, NULL_TREE));
28965 else
28966 sel_seq
28967 = chainon (sel_seq,
28968 build_tree_list (selector, NULL_TREE));
28970 token = cp_lexer_peek_token (parser->lexer);
28973 finish_selector:
28974 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28977 /* Build a location of the form:
28978 @selector(func)
28979 ^~~~~~~~~~~~~~~
28980 with caret==start at the @ token, finishing at the close paren. */
28981 location_t combined_loc
28982 = make_location (loc, loc,
28983 cp_lexer_previous_token (parser->lexer)->location);
28984 tree result = objc_build_selector_expr (combined_loc, sel_seq);
28985 /* TODO: objc_build_selector_expr doesn't always honor the location. */
28986 protected_set_expr_location (result, combined_loc);
28987 return result;
28990 /* Parse a list of identifiers.
28992 objc-identifier-list:
28993 identifier
28994 objc-identifier-list , identifier
28996 Returns a TREE_LIST of identifier nodes. */
28998 static tree
28999 cp_parser_objc_identifier_list (cp_parser* parser)
29001 tree identifier;
29002 tree list;
29003 cp_token *sep;
29005 identifier = cp_parser_identifier (parser);
29006 if (identifier == error_mark_node)
29007 return error_mark_node;
29009 list = build_tree_list (NULL_TREE, identifier);
29010 sep = cp_lexer_peek_token (parser->lexer);
29012 while (sep->type == CPP_COMMA)
29014 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29015 identifier = cp_parser_identifier (parser);
29016 if (identifier == error_mark_node)
29017 return list;
29019 list = chainon (list, build_tree_list (NULL_TREE,
29020 identifier));
29021 sep = cp_lexer_peek_token (parser->lexer);
29024 return list;
29027 /* Parse an Objective-C alias declaration.
29029 objc-alias-declaration:
29030 @compatibility_alias identifier identifier ;
29032 This function registers the alias mapping with the Objective-C front end.
29033 It returns nothing. */
29035 static void
29036 cp_parser_objc_alias_declaration (cp_parser* parser)
29038 tree alias, orig;
29040 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29041 alias = cp_parser_identifier (parser);
29042 orig = cp_parser_identifier (parser);
29043 objc_declare_alias (alias, orig);
29044 cp_parser_consume_semicolon_at_end_of_statement (parser);
29047 /* Parse an Objective-C class forward-declaration.
29049 objc-class-declaration:
29050 @class objc-identifier-list ;
29052 The function registers the forward declarations with the Objective-C
29053 front end. It returns nothing. */
29055 static void
29056 cp_parser_objc_class_declaration (cp_parser* parser)
29058 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29059 while (true)
29061 tree id;
29063 id = cp_parser_identifier (parser);
29064 if (id == error_mark_node)
29065 break;
29067 objc_declare_class (id);
29069 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29070 cp_lexer_consume_token (parser->lexer);
29071 else
29072 break;
29074 cp_parser_consume_semicolon_at_end_of_statement (parser);
29077 /* Parse a list of Objective-C protocol references.
29079 objc-protocol-refs-opt:
29080 objc-protocol-refs [opt]
29082 objc-protocol-refs:
29083 < objc-identifier-list >
29085 Returns a TREE_LIST of identifiers, if any. */
29087 static tree
29088 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29090 tree protorefs = NULL_TREE;
29092 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29094 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29095 protorefs = cp_parser_objc_identifier_list (parser);
29096 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29099 return protorefs;
29102 /* Parse a Objective-C visibility specification. */
29104 static void
29105 cp_parser_objc_visibility_spec (cp_parser* parser)
29107 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29109 switch (vis->keyword)
29111 case RID_AT_PRIVATE:
29112 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29113 break;
29114 case RID_AT_PROTECTED:
29115 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29116 break;
29117 case RID_AT_PUBLIC:
29118 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29119 break;
29120 case RID_AT_PACKAGE:
29121 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29122 break;
29123 default:
29124 return;
29127 /* Eat '@private'/'@protected'/'@public'. */
29128 cp_lexer_consume_token (parser->lexer);
29131 /* Parse an Objective-C method type. Return 'true' if it is a class
29132 (+) method, and 'false' if it is an instance (-) method. */
29134 static inline bool
29135 cp_parser_objc_method_type (cp_parser* parser)
29137 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29138 return true;
29139 else
29140 return false;
29143 /* Parse an Objective-C protocol qualifier. */
29145 static tree
29146 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29148 tree quals = NULL_TREE, node;
29149 cp_token *token = cp_lexer_peek_token (parser->lexer);
29151 node = token->u.value;
29153 while (node && identifier_p (node)
29154 && (node == ridpointers [(int) RID_IN]
29155 || node == ridpointers [(int) RID_OUT]
29156 || node == ridpointers [(int) RID_INOUT]
29157 || node == ridpointers [(int) RID_BYCOPY]
29158 || node == ridpointers [(int) RID_BYREF]
29159 || node == ridpointers [(int) RID_ONEWAY]))
29161 quals = tree_cons (NULL_TREE, node, quals);
29162 cp_lexer_consume_token (parser->lexer);
29163 token = cp_lexer_peek_token (parser->lexer);
29164 node = token->u.value;
29167 return quals;
29170 /* Parse an Objective-C typename. */
29172 static tree
29173 cp_parser_objc_typename (cp_parser* parser)
29175 tree type_name = NULL_TREE;
29177 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29179 tree proto_quals, cp_type = NULL_TREE;
29181 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29182 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29184 /* An ObjC type name may consist of just protocol qualifiers, in which
29185 case the type shall default to 'id'. */
29186 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29188 cp_type = cp_parser_type_id (parser);
29190 /* If the type could not be parsed, an error has already
29191 been produced. For error recovery, behave as if it had
29192 not been specified, which will use the default type
29193 'id'. */
29194 if (cp_type == error_mark_node)
29196 cp_type = NULL_TREE;
29197 /* We need to skip to the closing parenthesis as
29198 cp_parser_type_id() does not seem to do it for
29199 us. */
29200 cp_parser_skip_to_closing_parenthesis (parser,
29201 /*recovering=*/true,
29202 /*or_comma=*/false,
29203 /*consume_paren=*/false);
29207 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29208 type_name = build_tree_list (proto_quals, cp_type);
29211 return type_name;
29214 /* Check to see if TYPE refers to an Objective-C selector name. */
29216 static bool
29217 cp_parser_objc_selector_p (enum cpp_ttype type)
29219 return (type == CPP_NAME || type == CPP_KEYWORD
29220 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29221 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29222 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29223 || type == CPP_XOR || type == CPP_XOR_EQ);
29226 /* Parse an Objective-C selector. */
29228 static tree
29229 cp_parser_objc_selector (cp_parser* parser)
29231 cp_token *token = cp_lexer_consume_token (parser->lexer);
29233 if (!cp_parser_objc_selector_p (token->type))
29235 error_at (token->location, "invalid Objective-C++ selector name");
29236 return error_mark_node;
29239 /* C++ operator names are allowed to appear in ObjC selectors. */
29240 switch (token->type)
29242 case CPP_AND_AND: return get_identifier ("and");
29243 case CPP_AND_EQ: return get_identifier ("and_eq");
29244 case CPP_AND: return get_identifier ("bitand");
29245 case CPP_OR: return get_identifier ("bitor");
29246 case CPP_COMPL: return get_identifier ("compl");
29247 case CPP_NOT: return get_identifier ("not");
29248 case CPP_NOT_EQ: return get_identifier ("not_eq");
29249 case CPP_OR_OR: return get_identifier ("or");
29250 case CPP_OR_EQ: return get_identifier ("or_eq");
29251 case CPP_XOR: return get_identifier ("xor");
29252 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29253 default: return token->u.value;
29257 /* Parse an Objective-C params list. */
29259 static tree
29260 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29262 tree params = NULL_TREE;
29263 bool maybe_unary_selector_p = true;
29264 cp_token *token = cp_lexer_peek_token (parser->lexer);
29266 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29268 tree selector = NULL_TREE, type_name, identifier;
29269 tree parm_attr = NULL_TREE;
29271 if (token->keyword == RID_ATTRIBUTE)
29272 break;
29274 if (token->type != CPP_COLON)
29275 selector = cp_parser_objc_selector (parser);
29277 /* Detect if we have a unary selector. */
29278 if (maybe_unary_selector_p
29279 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29281 params = selector; /* Might be followed by attributes. */
29282 break;
29285 maybe_unary_selector_p = false;
29286 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29288 /* Something went quite wrong. There should be a colon
29289 here, but there is not. Stop parsing parameters. */
29290 break;
29292 type_name = cp_parser_objc_typename (parser);
29293 /* New ObjC allows attributes on parameters too. */
29294 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29295 parm_attr = cp_parser_attributes_opt (parser);
29296 identifier = cp_parser_identifier (parser);
29298 params
29299 = chainon (params,
29300 objc_build_keyword_decl (selector,
29301 type_name,
29302 identifier,
29303 parm_attr));
29305 token = cp_lexer_peek_token (parser->lexer);
29308 if (params == NULL_TREE)
29310 cp_parser_error (parser, "objective-c++ method declaration is expected");
29311 return error_mark_node;
29314 /* We allow tail attributes for the method. */
29315 if (token->keyword == RID_ATTRIBUTE)
29317 *attributes = cp_parser_attributes_opt (parser);
29318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29319 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29320 return params;
29321 cp_parser_error (parser,
29322 "method attributes must be specified at the end");
29323 return error_mark_node;
29326 if (params == NULL_TREE)
29328 cp_parser_error (parser, "objective-c++ method declaration is expected");
29329 return error_mark_node;
29331 return params;
29334 /* Parse the non-keyword Objective-C params. */
29336 static tree
29337 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29338 tree* attributes)
29340 tree params = make_node (TREE_LIST);
29341 cp_token *token = cp_lexer_peek_token (parser->lexer);
29342 *ellipsisp = false; /* Initially, assume no ellipsis. */
29344 while (token->type == CPP_COMMA)
29346 cp_parameter_declarator *parmdecl;
29347 tree parm;
29349 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29350 token = cp_lexer_peek_token (parser->lexer);
29352 if (token->type == CPP_ELLIPSIS)
29354 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29355 *ellipsisp = true;
29356 token = cp_lexer_peek_token (parser->lexer);
29357 break;
29360 /* TODO: parse attributes for tail parameters. */
29361 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29362 parm = grokdeclarator (parmdecl->declarator,
29363 &parmdecl->decl_specifiers,
29364 PARM, /*initialized=*/0,
29365 /*attrlist=*/NULL);
29367 chainon (params, build_tree_list (NULL_TREE, parm));
29368 token = cp_lexer_peek_token (parser->lexer);
29371 /* We allow tail attributes for the method. */
29372 if (token->keyword == RID_ATTRIBUTE)
29374 if (*attributes == NULL_TREE)
29376 *attributes = cp_parser_attributes_opt (parser);
29377 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29378 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29379 return params;
29381 else
29382 /* We have an error, but parse the attributes, so that we can
29383 carry on. */
29384 *attributes = cp_parser_attributes_opt (parser);
29386 cp_parser_error (parser,
29387 "method attributes must be specified at the end");
29388 return error_mark_node;
29391 return params;
29394 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29396 static void
29397 cp_parser_objc_interstitial_code (cp_parser* parser)
29399 cp_token *token = cp_lexer_peek_token (parser->lexer);
29401 /* If the next token is `extern' and the following token is a string
29402 literal, then we have a linkage specification. */
29403 if (token->keyword == RID_EXTERN
29404 && cp_parser_is_pure_string_literal
29405 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29406 cp_parser_linkage_specification (parser);
29407 /* Handle #pragma, if any. */
29408 else if (token->type == CPP_PRAGMA)
29409 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29410 /* Allow stray semicolons. */
29411 else if (token->type == CPP_SEMICOLON)
29412 cp_lexer_consume_token (parser->lexer);
29413 /* Mark methods as optional or required, when building protocols. */
29414 else if (token->keyword == RID_AT_OPTIONAL)
29416 cp_lexer_consume_token (parser->lexer);
29417 objc_set_method_opt (true);
29419 else if (token->keyword == RID_AT_REQUIRED)
29421 cp_lexer_consume_token (parser->lexer);
29422 objc_set_method_opt (false);
29424 else if (token->keyword == RID_NAMESPACE)
29425 cp_parser_namespace_definition (parser);
29426 /* Other stray characters must generate errors. */
29427 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29429 cp_lexer_consume_token (parser->lexer);
29430 error ("stray %qs between Objective-C++ methods",
29431 token->type == CPP_OPEN_BRACE ? "{" : "}");
29433 /* Finally, try to parse a block-declaration, or a function-definition. */
29434 else
29435 cp_parser_block_declaration (parser, /*statement_p=*/false);
29438 /* Parse a method signature. */
29440 static tree
29441 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29443 tree rettype, kwdparms, optparms;
29444 bool ellipsis = false;
29445 bool is_class_method;
29447 is_class_method = cp_parser_objc_method_type (parser);
29448 rettype = cp_parser_objc_typename (parser);
29449 *attributes = NULL_TREE;
29450 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29451 if (kwdparms == error_mark_node)
29452 return error_mark_node;
29453 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29454 if (optparms == error_mark_node)
29455 return error_mark_node;
29457 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29460 static bool
29461 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29463 tree tattr;
29464 cp_lexer_save_tokens (parser->lexer);
29465 tattr = cp_parser_attributes_opt (parser);
29466 gcc_assert (tattr) ;
29468 /* If the attributes are followed by a method introducer, this is not allowed.
29469 Dump the attributes and flag the situation. */
29470 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29471 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29472 return true;
29474 /* Otherwise, the attributes introduce some interstitial code, possibly so
29475 rewind to allow that check. */
29476 cp_lexer_rollback_tokens (parser->lexer);
29477 return false;
29480 /* Parse an Objective-C method prototype list. */
29482 static void
29483 cp_parser_objc_method_prototype_list (cp_parser* parser)
29485 cp_token *token = cp_lexer_peek_token (parser->lexer);
29487 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29489 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29491 tree attributes, sig;
29492 bool is_class_method;
29493 if (token->type == CPP_PLUS)
29494 is_class_method = true;
29495 else
29496 is_class_method = false;
29497 sig = cp_parser_objc_method_signature (parser, &attributes);
29498 if (sig == error_mark_node)
29500 cp_parser_skip_to_end_of_block_or_statement (parser);
29501 token = cp_lexer_peek_token (parser->lexer);
29502 continue;
29504 objc_add_method_declaration (is_class_method, sig, attributes);
29505 cp_parser_consume_semicolon_at_end_of_statement (parser);
29507 else if (token->keyword == RID_AT_PROPERTY)
29508 cp_parser_objc_at_property_declaration (parser);
29509 else if (token->keyword == RID_ATTRIBUTE
29510 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29511 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29512 OPT_Wattributes,
29513 "prefix attributes are ignored for methods");
29514 else
29515 /* Allow for interspersed non-ObjC++ code. */
29516 cp_parser_objc_interstitial_code (parser);
29518 token = cp_lexer_peek_token (parser->lexer);
29521 if (token->type != CPP_EOF)
29522 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29523 else
29524 cp_parser_error (parser, "expected %<@end%>");
29526 objc_finish_interface ();
29529 /* Parse an Objective-C method definition list. */
29531 static void
29532 cp_parser_objc_method_definition_list (cp_parser* parser)
29534 cp_token *token = cp_lexer_peek_token (parser->lexer);
29536 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29538 tree meth;
29540 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29542 cp_token *ptk;
29543 tree sig, attribute;
29544 bool is_class_method;
29545 if (token->type == CPP_PLUS)
29546 is_class_method = true;
29547 else
29548 is_class_method = false;
29549 push_deferring_access_checks (dk_deferred);
29550 sig = cp_parser_objc_method_signature (parser, &attribute);
29551 if (sig == error_mark_node)
29553 cp_parser_skip_to_end_of_block_or_statement (parser);
29554 token = cp_lexer_peek_token (parser->lexer);
29555 continue;
29557 objc_start_method_definition (is_class_method, sig, attribute,
29558 NULL_TREE);
29560 /* For historical reasons, we accept an optional semicolon. */
29561 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29562 cp_lexer_consume_token (parser->lexer);
29564 ptk = cp_lexer_peek_token (parser->lexer);
29565 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29566 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29568 perform_deferred_access_checks (tf_warning_or_error);
29569 stop_deferring_access_checks ();
29570 meth = cp_parser_function_definition_after_declarator (parser,
29571 false);
29572 pop_deferring_access_checks ();
29573 objc_finish_method_definition (meth);
29576 /* The following case will be removed once @synthesize is
29577 completely implemented. */
29578 else if (token->keyword == RID_AT_PROPERTY)
29579 cp_parser_objc_at_property_declaration (parser);
29580 else if (token->keyword == RID_AT_SYNTHESIZE)
29581 cp_parser_objc_at_synthesize_declaration (parser);
29582 else if (token->keyword == RID_AT_DYNAMIC)
29583 cp_parser_objc_at_dynamic_declaration (parser);
29584 else if (token->keyword == RID_ATTRIBUTE
29585 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29586 warning_at (token->location, OPT_Wattributes,
29587 "prefix attributes are ignored for methods");
29588 else
29589 /* Allow for interspersed non-ObjC++ code. */
29590 cp_parser_objc_interstitial_code (parser);
29592 token = cp_lexer_peek_token (parser->lexer);
29595 if (token->type != CPP_EOF)
29596 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29597 else
29598 cp_parser_error (parser, "expected %<@end%>");
29600 objc_finish_implementation ();
29603 /* Parse Objective-C ivars. */
29605 static void
29606 cp_parser_objc_class_ivars (cp_parser* parser)
29608 cp_token *token = cp_lexer_peek_token (parser->lexer);
29610 if (token->type != CPP_OPEN_BRACE)
29611 return; /* No ivars specified. */
29613 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29614 token = cp_lexer_peek_token (parser->lexer);
29616 while (token->type != CPP_CLOSE_BRACE
29617 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29619 cp_decl_specifier_seq declspecs;
29620 int decl_class_or_enum_p;
29621 tree prefix_attributes;
29623 cp_parser_objc_visibility_spec (parser);
29625 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29626 break;
29628 cp_parser_decl_specifier_seq (parser,
29629 CP_PARSER_FLAGS_OPTIONAL,
29630 &declspecs,
29631 &decl_class_or_enum_p);
29633 /* auto, register, static, extern, mutable. */
29634 if (declspecs.storage_class != sc_none)
29636 cp_parser_error (parser, "invalid type for instance variable");
29637 declspecs.storage_class = sc_none;
29640 /* thread_local. */
29641 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29643 cp_parser_error (parser, "invalid type for instance variable");
29644 declspecs.locations[ds_thread] = 0;
29647 /* typedef. */
29648 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29650 cp_parser_error (parser, "invalid type for instance variable");
29651 declspecs.locations[ds_typedef] = 0;
29654 prefix_attributes = declspecs.attributes;
29655 declspecs.attributes = NULL_TREE;
29657 /* Keep going until we hit the `;' at the end of the
29658 declaration. */
29659 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29661 tree width = NULL_TREE, attributes, first_attribute, decl;
29662 cp_declarator *declarator = NULL;
29663 int ctor_dtor_or_conv_p;
29665 /* Check for a (possibly unnamed) bitfield declaration. */
29666 token = cp_lexer_peek_token (parser->lexer);
29667 if (token->type == CPP_COLON)
29668 goto eat_colon;
29670 if (token->type == CPP_NAME
29671 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29672 == CPP_COLON))
29674 /* Get the name of the bitfield. */
29675 declarator = make_id_declarator (NULL_TREE,
29676 cp_parser_identifier (parser),
29677 sfk_none);
29679 eat_colon:
29680 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29681 /* Get the width of the bitfield. */
29682 width
29683 = cp_parser_constant_expression (parser);
29685 else
29687 /* Parse the declarator. */
29688 declarator
29689 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29690 &ctor_dtor_or_conv_p,
29691 /*parenthesized_p=*/NULL,
29692 /*member_p=*/false,
29693 /*friend_p=*/false);
29696 /* Look for attributes that apply to the ivar. */
29697 attributes = cp_parser_attributes_opt (parser);
29698 /* Remember which attributes are prefix attributes and
29699 which are not. */
29700 first_attribute = attributes;
29701 /* Combine the attributes. */
29702 attributes = chainon (prefix_attributes, attributes);
29704 if (width)
29705 /* Create the bitfield declaration. */
29706 decl = grokbitfield (declarator, &declspecs,
29707 width,
29708 attributes);
29709 else
29710 decl = grokfield (declarator, &declspecs,
29711 NULL_TREE, /*init_const_expr_p=*/false,
29712 NULL_TREE, attributes);
29714 /* Add the instance variable. */
29715 if (decl != error_mark_node && decl != NULL_TREE)
29716 objc_add_instance_variable (decl);
29718 /* Reset PREFIX_ATTRIBUTES. */
29719 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29720 attributes = TREE_CHAIN (attributes);
29721 if (attributes)
29722 TREE_CHAIN (attributes) = NULL_TREE;
29724 token = cp_lexer_peek_token (parser->lexer);
29726 if (token->type == CPP_COMMA)
29728 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29729 continue;
29731 break;
29734 cp_parser_consume_semicolon_at_end_of_statement (parser);
29735 token = cp_lexer_peek_token (parser->lexer);
29738 if (token->keyword == RID_AT_END)
29739 cp_parser_error (parser, "expected %<}%>");
29741 /* Do not consume the RID_AT_END, so it will be read again as terminating
29742 the @interface of @implementation. */
29743 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29744 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29746 /* For historical reasons, we accept an optional semicolon. */
29747 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29748 cp_lexer_consume_token (parser->lexer);
29751 /* Parse an Objective-C protocol declaration. */
29753 static void
29754 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29756 tree proto, protorefs;
29757 cp_token *tok;
29759 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29760 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29762 tok = cp_lexer_peek_token (parser->lexer);
29763 error_at (tok->location, "identifier expected after %<@protocol%>");
29764 cp_parser_consume_semicolon_at_end_of_statement (parser);
29765 return;
29768 /* See if we have a forward declaration or a definition. */
29769 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29771 /* Try a forward declaration first. */
29772 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29774 while (true)
29776 tree id;
29778 id = cp_parser_identifier (parser);
29779 if (id == error_mark_node)
29780 break;
29782 objc_declare_protocol (id, attributes);
29784 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29785 cp_lexer_consume_token (parser->lexer);
29786 else
29787 break;
29789 cp_parser_consume_semicolon_at_end_of_statement (parser);
29792 /* Ok, we got a full-fledged definition (or at least should). */
29793 else
29795 proto = cp_parser_identifier (parser);
29796 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29797 objc_start_protocol (proto, protorefs, attributes);
29798 cp_parser_objc_method_prototype_list (parser);
29802 /* Parse an Objective-C superclass or category. */
29804 static void
29805 cp_parser_objc_superclass_or_category (cp_parser *parser,
29806 bool iface_p,
29807 tree *super,
29808 tree *categ, bool *is_class_extension)
29810 cp_token *next = cp_lexer_peek_token (parser->lexer);
29812 *super = *categ = NULL_TREE;
29813 *is_class_extension = false;
29814 if (next->type == CPP_COLON)
29816 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29817 *super = cp_parser_identifier (parser);
29819 else if (next->type == CPP_OPEN_PAREN)
29821 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29823 /* If there is no category name, and this is an @interface, we
29824 have a class extension. */
29825 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29827 *categ = NULL_TREE;
29828 *is_class_extension = true;
29830 else
29831 *categ = cp_parser_identifier (parser);
29833 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29837 /* Parse an Objective-C class interface. */
29839 static void
29840 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
29842 tree name, super, categ, protos;
29843 bool is_class_extension;
29845 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
29846 name = cp_parser_identifier (parser);
29847 if (name == error_mark_node)
29849 /* It's hard to recover because even if valid @interface stuff
29850 is to follow, we can't compile it (or validate it) if we
29851 don't even know which class it refers to. Let's assume this
29852 was a stray '@interface' token in the stream and skip it.
29854 return;
29856 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
29857 &is_class_extension);
29858 protos = cp_parser_objc_protocol_refs_opt (parser);
29860 /* We have either a class or a category on our hands. */
29861 if (categ || is_class_extension)
29862 objc_start_category_interface (name, categ, protos, attributes);
29863 else
29865 objc_start_class_interface (name, super, protos, attributes);
29866 /* Handle instance variable declarations, if any. */
29867 cp_parser_objc_class_ivars (parser);
29868 objc_continue_interface ();
29871 cp_parser_objc_method_prototype_list (parser);
29874 /* Parse an Objective-C class implementation. */
29876 static void
29877 cp_parser_objc_class_implementation (cp_parser* parser)
29879 tree name, super, categ;
29880 bool is_class_extension;
29882 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
29883 name = cp_parser_identifier (parser);
29884 if (name == error_mark_node)
29886 /* It's hard to recover because even if valid @implementation
29887 stuff is to follow, we can't compile it (or validate it) if
29888 we don't even know which class it refers to. Let's assume
29889 this was a stray '@implementation' token in the stream and
29890 skip it.
29892 return;
29894 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
29895 &is_class_extension);
29897 /* We have either a class or a category on our hands. */
29898 if (categ)
29899 objc_start_category_implementation (name, categ);
29900 else
29902 objc_start_class_implementation (name, super);
29903 /* Handle instance variable declarations, if any. */
29904 cp_parser_objc_class_ivars (parser);
29905 objc_continue_implementation ();
29908 cp_parser_objc_method_definition_list (parser);
29911 /* Consume the @end token and finish off the implementation. */
29913 static void
29914 cp_parser_objc_end_implementation (cp_parser* parser)
29916 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29917 objc_finish_implementation ();
29920 /* Parse an Objective-C declaration. */
29922 static void
29923 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
29925 /* Try to figure out what kind of declaration is present. */
29926 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
29928 if (attributes)
29929 switch (kwd->keyword)
29931 case RID_AT_ALIAS:
29932 case RID_AT_CLASS:
29933 case RID_AT_END:
29934 error_at (kwd->location, "attributes may not be specified before"
29935 " the %<@%D%> Objective-C++ keyword",
29936 kwd->u.value);
29937 attributes = NULL;
29938 break;
29939 case RID_AT_IMPLEMENTATION:
29940 warning_at (kwd->location, OPT_Wattributes,
29941 "prefix attributes are ignored before %<@%D%>",
29942 kwd->u.value);
29943 attributes = NULL;
29944 default:
29945 break;
29948 switch (kwd->keyword)
29950 case RID_AT_ALIAS:
29951 cp_parser_objc_alias_declaration (parser);
29952 break;
29953 case RID_AT_CLASS:
29954 cp_parser_objc_class_declaration (parser);
29955 break;
29956 case RID_AT_PROTOCOL:
29957 cp_parser_objc_protocol_declaration (parser, attributes);
29958 break;
29959 case RID_AT_INTERFACE:
29960 cp_parser_objc_class_interface (parser, attributes);
29961 break;
29962 case RID_AT_IMPLEMENTATION:
29963 cp_parser_objc_class_implementation (parser);
29964 break;
29965 case RID_AT_END:
29966 cp_parser_objc_end_implementation (parser);
29967 break;
29968 default:
29969 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
29970 kwd->u.value);
29971 cp_parser_skip_to_end_of_block_or_statement (parser);
29975 /* Parse an Objective-C try-catch-finally statement.
29977 objc-try-catch-finally-stmt:
29978 @try compound-statement objc-catch-clause-seq [opt]
29979 objc-finally-clause [opt]
29981 objc-catch-clause-seq:
29982 objc-catch-clause objc-catch-clause-seq [opt]
29984 objc-catch-clause:
29985 @catch ( objc-exception-declaration ) compound-statement
29987 objc-finally-clause:
29988 @finally compound-statement
29990 objc-exception-declaration:
29991 parameter-declaration
29992 '...'
29994 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
29996 Returns NULL_TREE.
29998 PS: This function is identical to c_parser_objc_try_catch_finally_statement
29999 for C. Keep them in sync. */
30001 static tree
30002 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30004 location_t location;
30005 tree stmt;
30007 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30008 location = cp_lexer_peek_token (parser->lexer)->location;
30009 objc_maybe_warn_exceptions (location);
30010 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30011 node, lest it get absorbed into the surrounding block. */
30012 stmt = push_stmt_list ();
30013 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30014 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30016 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30018 cp_parameter_declarator *parm;
30019 tree parameter_declaration = error_mark_node;
30020 bool seen_open_paren = false;
30022 cp_lexer_consume_token (parser->lexer);
30023 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30024 seen_open_paren = true;
30025 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30027 /* We have "@catch (...)" (where the '...' are literally
30028 what is in the code). Skip the '...'.
30029 parameter_declaration is set to NULL_TREE, and
30030 objc_being_catch_clauses() knows that that means
30031 '...'. */
30032 cp_lexer_consume_token (parser->lexer);
30033 parameter_declaration = NULL_TREE;
30035 else
30037 /* We have "@catch (NSException *exception)" or something
30038 like that. Parse the parameter declaration. */
30039 parm = cp_parser_parameter_declaration (parser, false, NULL);
30040 if (parm == NULL)
30041 parameter_declaration = error_mark_node;
30042 else
30043 parameter_declaration = grokdeclarator (parm->declarator,
30044 &parm->decl_specifiers,
30045 PARM, /*initialized=*/0,
30046 /*attrlist=*/NULL);
30048 if (seen_open_paren)
30049 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30050 else
30052 /* If there was no open parenthesis, we are recovering from
30053 an error, and we are trying to figure out what mistake
30054 the user has made. */
30056 /* If there is an immediate closing parenthesis, the user
30057 probably forgot the opening one (ie, they typed "@catch
30058 NSException *e)". Parse the closing parenthesis and keep
30059 going. */
30060 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30061 cp_lexer_consume_token (parser->lexer);
30063 /* If these is no immediate closing parenthesis, the user
30064 probably doesn't know that parenthesis are required at
30065 all (ie, they typed "@catch NSException *e"). So, just
30066 forget about the closing parenthesis and keep going. */
30068 objc_begin_catch_clause (parameter_declaration);
30069 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30070 objc_finish_catch_clause ();
30072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30074 cp_lexer_consume_token (parser->lexer);
30075 location = cp_lexer_peek_token (parser->lexer)->location;
30076 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30077 node, lest it get absorbed into the surrounding block. */
30078 stmt = push_stmt_list ();
30079 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30080 objc_build_finally_clause (location, pop_stmt_list (stmt));
30083 return objc_finish_try_stmt ();
30086 /* Parse an Objective-C synchronized statement.
30088 objc-synchronized-stmt:
30089 @synchronized ( expression ) compound-statement
30091 Returns NULL_TREE. */
30093 static tree
30094 cp_parser_objc_synchronized_statement (cp_parser *parser)
30096 location_t location;
30097 tree lock, stmt;
30099 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30101 location = cp_lexer_peek_token (parser->lexer)->location;
30102 objc_maybe_warn_exceptions (location);
30103 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30104 lock = cp_parser_expression (parser);
30105 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30107 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30108 node, lest it get absorbed into the surrounding block. */
30109 stmt = push_stmt_list ();
30110 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30112 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30115 /* Parse an Objective-C throw statement.
30117 objc-throw-stmt:
30118 @throw assignment-expression [opt] ;
30120 Returns a constructed '@throw' statement. */
30122 static tree
30123 cp_parser_objc_throw_statement (cp_parser *parser)
30125 tree expr = NULL_TREE;
30126 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30128 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30130 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30131 expr = cp_parser_expression (parser);
30133 cp_parser_consume_semicolon_at_end_of_statement (parser);
30135 return objc_build_throw_stmt (loc, expr);
30138 /* Parse an Objective-C statement. */
30140 static tree
30141 cp_parser_objc_statement (cp_parser * parser)
30143 /* Try to figure out what kind of declaration is present. */
30144 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30146 switch (kwd->keyword)
30148 case RID_AT_TRY:
30149 return cp_parser_objc_try_catch_finally_statement (parser);
30150 case RID_AT_SYNCHRONIZED:
30151 return cp_parser_objc_synchronized_statement (parser);
30152 case RID_AT_THROW:
30153 return cp_parser_objc_throw_statement (parser);
30154 default:
30155 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30156 kwd->u.value);
30157 cp_parser_skip_to_end_of_block_or_statement (parser);
30160 return error_mark_node;
30163 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30164 look ahead to see if an objc keyword follows the attributes. This
30165 is to detect the use of prefix attributes on ObjC @interface and
30166 @protocol. */
30168 static bool
30169 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30171 cp_lexer_save_tokens (parser->lexer);
30172 *attrib = cp_parser_attributes_opt (parser);
30173 gcc_assert (*attrib);
30174 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30176 cp_lexer_commit_tokens (parser->lexer);
30177 return true;
30179 cp_lexer_rollback_tokens (parser->lexer);
30180 return false;
30183 /* This routine is a minimal replacement for
30184 c_parser_struct_declaration () used when parsing the list of
30185 types/names or ObjC++ properties. For example, when parsing the
30186 code
30188 @property (readonly) int a, b, c;
30190 this function is responsible for parsing "int a, int b, int c" and
30191 returning the declarations as CHAIN of DECLs.
30193 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30194 similar parsing. */
30195 static tree
30196 cp_parser_objc_struct_declaration (cp_parser *parser)
30198 tree decls = NULL_TREE;
30199 cp_decl_specifier_seq declspecs;
30200 int decl_class_or_enum_p;
30201 tree prefix_attributes;
30203 cp_parser_decl_specifier_seq (parser,
30204 CP_PARSER_FLAGS_NONE,
30205 &declspecs,
30206 &decl_class_or_enum_p);
30208 if (declspecs.type == error_mark_node)
30209 return error_mark_node;
30211 /* auto, register, static, extern, mutable. */
30212 if (declspecs.storage_class != sc_none)
30214 cp_parser_error (parser, "invalid type for property");
30215 declspecs.storage_class = sc_none;
30218 /* thread_local. */
30219 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30221 cp_parser_error (parser, "invalid type for property");
30222 declspecs.locations[ds_thread] = 0;
30225 /* typedef. */
30226 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30228 cp_parser_error (parser, "invalid type for property");
30229 declspecs.locations[ds_typedef] = 0;
30232 prefix_attributes = declspecs.attributes;
30233 declspecs.attributes = NULL_TREE;
30235 /* Keep going until we hit the `;' at the end of the declaration. */
30236 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30238 tree attributes, first_attribute, decl;
30239 cp_declarator *declarator;
30240 cp_token *token;
30242 /* Parse the declarator. */
30243 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30244 NULL, NULL, false, false);
30246 /* Look for attributes that apply to the ivar. */
30247 attributes = cp_parser_attributes_opt (parser);
30248 /* Remember which attributes are prefix attributes and
30249 which are not. */
30250 first_attribute = attributes;
30251 /* Combine the attributes. */
30252 attributes = chainon (prefix_attributes, attributes);
30254 decl = grokfield (declarator, &declspecs,
30255 NULL_TREE, /*init_const_expr_p=*/false,
30256 NULL_TREE, attributes);
30258 if (decl == error_mark_node || decl == NULL_TREE)
30259 return error_mark_node;
30261 /* Reset PREFIX_ATTRIBUTES. */
30262 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30263 attributes = TREE_CHAIN (attributes);
30264 if (attributes)
30265 TREE_CHAIN (attributes) = NULL_TREE;
30267 DECL_CHAIN (decl) = decls;
30268 decls = decl;
30270 token = cp_lexer_peek_token (parser->lexer);
30271 if (token->type == CPP_COMMA)
30273 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30274 continue;
30276 else
30277 break;
30279 return decls;
30282 /* Parse an Objective-C @property declaration. The syntax is:
30284 objc-property-declaration:
30285 '@property' objc-property-attributes[opt] struct-declaration ;
30287 objc-property-attributes:
30288 '(' objc-property-attribute-list ')'
30290 objc-property-attribute-list:
30291 objc-property-attribute
30292 objc-property-attribute-list, objc-property-attribute
30294 objc-property-attribute
30295 'getter' = identifier
30296 'setter' = identifier
30297 'readonly'
30298 'readwrite'
30299 'assign'
30300 'retain'
30301 'copy'
30302 'nonatomic'
30304 For example:
30305 @property NSString *name;
30306 @property (readonly) id object;
30307 @property (retain, nonatomic, getter=getTheName) id name;
30308 @property int a, b, c;
30310 PS: This function is identical to
30311 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30312 static void
30313 cp_parser_objc_at_property_declaration (cp_parser *parser)
30315 /* The following variables hold the attributes of the properties as
30316 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30317 seen. When we see an attribute, we set them to 'true' (if they
30318 are boolean properties) or to the identifier (if they have an
30319 argument, ie, for getter and setter). Note that here we only
30320 parse the list of attributes, check the syntax and accumulate the
30321 attributes that we find. objc_add_property_declaration() will
30322 then process the information. */
30323 bool property_assign = false;
30324 bool property_copy = false;
30325 tree property_getter_ident = NULL_TREE;
30326 bool property_nonatomic = false;
30327 bool property_readonly = false;
30328 bool property_readwrite = false;
30329 bool property_retain = false;
30330 tree property_setter_ident = NULL_TREE;
30332 /* 'properties' is the list of properties that we read. Usually a
30333 single one, but maybe more (eg, in "@property int a, b, c;" there
30334 are three). */
30335 tree properties;
30336 location_t loc;
30338 loc = cp_lexer_peek_token (parser->lexer)->location;
30340 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30342 /* Parse the optional attribute list... */
30343 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30345 /* Eat the '('. */
30346 cp_lexer_consume_token (parser->lexer);
30348 while (true)
30350 bool syntax_error = false;
30351 cp_token *token = cp_lexer_peek_token (parser->lexer);
30352 enum rid keyword;
30354 if (token->type != CPP_NAME)
30356 cp_parser_error (parser, "expected identifier");
30357 break;
30359 keyword = C_RID_CODE (token->u.value);
30360 cp_lexer_consume_token (parser->lexer);
30361 switch (keyword)
30363 case RID_ASSIGN: property_assign = true; break;
30364 case RID_COPY: property_copy = true; break;
30365 case RID_NONATOMIC: property_nonatomic = true; break;
30366 case RID_READONLY: property_readonly = true; break;
30367 case RID_READWRITE: property_readwrite = true; break;
30368 case RID_RETAIN: property_retain = true; break;
30370 case RID_GETTER:
30371 case RID_SETTER:
30372 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30374 if (keyword == RID_GETTER)
30375 cp_parser_error (parser,
30376 "missing %<=%> (after %<getter%> attribute)");
30377 else
30378 cp_parser_error (parser,
30379 "missing %<=%> (after %<setter%> attribute)");
30380 syntax_error = true;
30381 break;
30383 cp_lexer_consume_token (parser->lexer); /* eat the = */
30384 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30386 cp_parser_error (parser, "expected identifier");
30387 syntax_error = true;
30388 break;
30390 if (keyword == RID_SETTER)
30392 if (property_setter_ident != NULL_TREE)
30394 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30395 cp_lexer_consume_token (parser->lexer);
30397 else
30398 property_setter_ident = cp_parser_objc_selector (parser);
30399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30400 cp_parser_error (parser, "setter name must terminate with %<:%>");
30401 else
30402 cp_lexer_consume_token (parser->lexer);
30404 else
30406 if (property_getter_ident != NULL_TREE)
30408 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30409 cp_lexer_consume_token (parser->lexer);
30411 else
30412 property_getter_ident = cp_parser_objc_selector (parser);
30414 break;
30415 default:
30416 cp_parser_error (parser, "unknown property attribute");
30417 syntax_error = true;
30418 break;
30421 if (syntax_error)
30422 break;
30424 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30425 cp_lexer_consume_token (parser->lexer);
30426 else
30427 break;
30430 /* FIXME: "@property (setter, assign);" will generate a spurious
30431 "error: expected ‘)’ before ‘,’ token". This is because
30432 cp_parser_require, unlike the C counterpart, will produce an
30433 error even if we are in error recovery. */
30434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30436 cp_parser_skip_to_closing_parenthesis (parser,
30437 /*recovering=*/true,
30438 /*or_comma=*/false,
30439 /*consume_paren=*/true);
30443 /* ... and the property declaration(s). */
30444 properties = cp_parser_objc_struct_declaration (parser);
30446 if (properties == error_mark_node)
30448 cp_parser_skip_to_end_of_statement (parser);
30449 /* If the next token is now a `;', consume it. */
30450 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30451 cp_lexer_consume_token (parser->lexer);
30452 return;
30455 if (properties == NULL_TREE)
30456 cp_parser_error (parser, "expected identifier");
30457 else
30459 /* Comma-separated properties are chained together in
30460 reverse order; add them one by one. */
30461 properties = nreverse (properties);
30463 for (; properties; properties = TREE_CHAIN (properties))
30464 objc_add_property_declaration (loc, copy_node (properties),
30465 property_readonly, property_readwrite,
30466 property_assign, property_retain,
30467 property_copy, property_nonatomic,
30468 property_getter_ident, property_setter_ident);
30471 cp_parser_consume_semicolon_at_end_of_statement (parser);
30474 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30476 objc-synthesize-declaration:
30477 @synthesize objc-synthesize-identifier-list ;
30479 objc-synthesize-identifier-list:
30480 objc-synthesize-identifier
30481 objc-synthesize-identifier-list, objc-synthesize-identifier
30483 objc-synthesize-identifier
30484 identifier
30485 identifier = identifier
30487 For example:
30488 @synthesize MyProperty;
30489 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30491 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30492 for C. Keep them in sync.
30494 static void
30495 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30497 tree list = NULL_TREE;
30498 location_t loc;
30499 loc = cp_lexer_peek_token (parser->lexer)->location;
30501 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30502 while (true)
30504 tree property, ivar;
30505 property = cp_parser_identifier (parser);
30506 if (property == error_mark_node)
30508 cp_parser_consume_semicolon_at_end_of_statement (parser);
30509 return;
30511 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30513 cp_lexer_consume_token (parser->lexer);
30514 ivar = cp_parser_identifier (parser);
30515 if (ivar == error_mark_node)
30517 cp_parser_consume_semicolon_at_end_of_statement (parser);
30518 return;
30521 else
30522 ivar = NULL_TREE;
30523 list = chainon (list, build_tree_list (ivar, property));
30524 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30525 cp_lexer_consume_token (parser->lexer);
30526 else
30527 break;
30529 cp_parser_consume_semicolon_at_end_of_statement (parser);
30530 objc_add_synthesize_declaration (loc, list);
30533 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30535 objc-dynamic-declaration:
30536 @dynamic identifier-list ;
30538 For example:
30539 @dynamic MyProperty;
30540 @dynamic MyProperty, AnotherProperty;
30542 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30543 for C. Keep them in sync.
30545 static void
30546 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30548 tree list = NULL_TREE;
30549 location_t loc;
30550 loc = cp_lexer_peek_token (parser->lexer)->location;
30552 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30553 while (true)
30555 tree property;
30556 property = cp_parser_identifier (parser);
30557 if (property == error_mark_node)
30559 cp_parser_consume_semicolon_at_end_of_statement (parser);
30560 return;
30562 list = chainon (list, build_tree_list (NULL, property));
30563 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30564 cp_lexer_consume_token (parser->lexer);
30565 else
30566 break;
30568 cp_parser_consume_semicolon_at_end_of_statement (parser);
30569 objc_add_dynamic_declaration (loc, list);
30573 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30575 /* Returns name of the next clause.
30576 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30577 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30578 returned and the token is consumed. */
30580 static pragma_omp_clause
30581 cp_parser_omp_clause_name (cp_parser *parser)
30583 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30585 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30586 result = PRAGMA_OACC_CLAUSE_AUTO;
30587 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30588 result = PRAGMA_OMP_CLAUSE_IF;
30589 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30590 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30591 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30592 result = PRAGMA_OACC_CLAUSE_DELETE;
30593 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30594 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30595 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30596 result = PRAGMA_OMP_CLAUSE_FOR;
30597 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30599 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30600 const char *p = IDENTIFIER_POINTER (id);
30602 switch (p[0])
30604 case 'a':
30605 if (!strcmp ("aligned", p))
30606 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30607 else if (!strcmp ("async", p))
30608 result = PRAGMA_OACC_CLAUSE_ASYNC;
30609 break;
30610 case 'c':
30611 if (!strcmp ("collapse", p))
30612 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30613 else if (!strcmp ("copy", p))
30614 result = PRAGMA_OACC_CLAUSE_COPY;
30615 else if (!strcmp ("copyin", p))
30616 result = PRAGMA_OMP_CLAUSE_COPYIN;
30617 else if (!strcmp ("copyout", p))
30618 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30619 else if (!strcmp ("copyprivate", p))
30620 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30621 else if (!strcmp ("create", p))
30622 result = PRAGMA_OACC_CLAUSE_CREATE;
30623 break;
30624 case 'd':
30625 if (!strcmp ("defaultmap", p))
30626 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30627 else if (!strcmp ("depend", p))
30628 result = PRAGMA_OMP_CLAUSE_DEPEND;
30629 else if (!strcmp ("device", p))
30630 result = PRAGMA_OMP_CLAUSE_DEVICE;
30631 else if (!strcmp ("deviceptr", p))
30632 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30633 else if (!strcmp ("device_resident", p))
30634 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30635 else if (!strcmp ("dist_schedule", p))
30636 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30637 break;
30638 case 'f':
30639 if (!strcmp ("final", p))
30640 result = PRAGMA_OMP_CLAUSE_FINAL;
30641 else if (!strcmp ("firstprivate", p))
30642 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30643 else if (!strcmp ("from", p))
30644 result = PRAGMA_OMP_CLAUSE_FROM;
30645 break;
30646 case 'g':
30647 if (!strcmp ("gang", p))
30648 result = PRAGMA_OACC_CLAUSE_GANG;
30649 else if (!strcmp ("grainsize", p))
30650 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30651 break;
30652 case 'h':
30653 if (!strcmp ("hint", p))
30654 result = PRAGMA_OMP_CLAUSE_HINT;
30655 else if (!strcmp ("host", p))
30656 result = PRAGMA_OACC_CLAUSE_HOST;
30657 break;
30658 case 'i':
30659 if (!strcmp ("inbranch", p))
30660 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30661 else if (!strcmp ("independent", p))
30662 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30663 else if (!strcmp ("is_device_ptr", p))
30664 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30665 break;
30666 case 'l':
30667 if (!strcmp ("lastprivate", p))
30668 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30669 else if (!strcmp ("linear", p))
30670 result = PRAGMA_OMP_CLAUSE_LINEAR;
30671 else if (!strcmp ("link", p))
30672 result = PRAGMA_OMP_CLAUSE_LINK;
30673 break;
30674 case 'm':
30675 if (!strcmp ("map", p))
30676 result = PRAGMA_OMP_CLAUSE_MAP;
30677 else if (!strcmp ("mergeable", p))
30678 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30679 else if (flag_cilkplus && !strcmp ("mask", p))
30680 result = PRAGMA_CILK_CLAUSE_MASK;
30681 break;
30682 case 'n':
30683 if (!strcmp ("nogroup", p))
30684 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30685 else if (!strcmp ("notinbranch", p))
30686 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30687 else if (!strcmp ("nowait", p))
30688 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30689 else if (flag_cilkplus && !strcmp ("nomask", p))
30690 result = PRAGMA_CILK_CLAUSE_NOMASK;
30691 else if (!strcmp ("num_gangs", p))
30692 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30693 else if (!strcmp ("num_tasks", p))
30694 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30695 else if (!strcmp ("num_teams", p))
30696 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30697 else if (!strcmp ("num_threads", p))
30698 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30699 else if (!strcmp ("num_workers", p))
30700 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30701 break;
30702 case 'o':
30703 if (!strcmp ("ordered", p))
30704 result = PRAGMA_OMP_CLAUSE_ORDERED;
30705 break;
30706 case 'p':
30707 if (!strcmp ("parallel", p))
30708 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30709 else if (!strcmp ("present", p))
30710 result = PRAGMA_OACC_CLAUSE_PRESENT;
30711 else if (!strcmp ("present_or_copy", p)
30712 || !strcmp ("pcopy", p))
30713 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30714 else if (!strcmp ("present_or_copyin", p)
30715 || !strcmp ("pcopyin", p))
30716 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30717 else if (!strcmp ("present_or_copyout", p)
30718 || !strcmp ("pcopyout", p))
30719 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30720 else if (!strcmp ("present_or_create", p)
30721 || !strcmp ("pcreate", p))
30722 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30723 else if (!strcmp ("priority", p))
30724 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30725 else if (!strcmp ("proc_bind", p))
30726 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30727 break;
30728 case 'r':
30729 if (!strcmp ("reduction", p))
30730 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30731 break;
30732 case 's':
30733 if (!strcmp ("safelen", p))
30734 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30735 else if (!strcmp ("schedule", p))
30736 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30737 else if (!strcmp ("sections", p))
30738 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30739 else if (!strcmp ("self", p))
30740 result = PRAGMA_OACC_CLAUSE_SELF;
30741 else if (!strcmp ("seq", p))
30742 result = PRAGMA_OACC_CLAUSE_SEQ;
30743 else if (!strcmp ("shared", p))
30744 result = PRAGMA_OMP_CLAUSE_SHARED;
30745 else if (!strcmp ("simd", p))
30746 result = PRAGMA_OMP_CLAUSE_SIMD;
30747 else if (!strcmp ("simdlen", p))
30748 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30749 break;
30750 case 't':
30751 if (!strcmp ("taskgroup", p))
30752 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30753 else if (!strcmp ("thread_limit", p))
30754 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30755 else if (!strcmp ("threads", p))
30756 result = PRAGMA_OMP_CLAUSE_THREADS;
30757 else if (!strcmp ("tile", p))
30758 result = PRAGMA_OACC_CLAUSE_TILE;
30759 else if (!strcmp ("to", p))
30760 result = PRAGMA_OMP_CLAUSE_TO;
30761 break;
30762 case 'u':
30763 if (!strcmp ("uniform", p))
30764 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30765 else if (!strcmp ("untied", p))
30766 result = PRAGMA_OMP_CLAUSE_UNTIED;
30767 else if (!strcmp ("use_device", p))
30768 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30769 else if (!strcmp ("use_device_ptr", p))
30770 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30771 break;
30772 case 'v':
30773 if (!strcmp ("vector", p))
30774 result = PRAGMA_OACC_CLAUSE_VECTOR;
30775 else if (!strcmp ("vector_length", p))
30776 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30777 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30778 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30779 break;
30780 case 'w':
30781 if (!strcmp ("wait", p))
30782 result = PRAGMA_OACC_CLAUSE_WAIT;
30783 else if (!strcmp ("worker", p))
30784 result = PRAGMA_OACC_CLAUSE_WORKER;
30785 break;
30789 if (result != PRAGMA_OMP_CLAUSE_NONE)
30790 cp_lexer_consume_token (parser->lexer);
30792 return result;
30795 /* Validate that a clause of the given type does not already exist. */
30797 static void
30798 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30799 const char *name, location_t location)
30801 tree c;
30803 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30804 if (OMP_CLAUSE_CODE (c) == code)
30806 error_at (location, "too many %qs clauses", name);
30807 break;
30811 /* OpenMP 2.5:
30812 variable-list:
30813 identifier
30814 variable-list , identifier
30816 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30817 colon). An opening parenthesis will have been consumed by the caller.
30819 If KIND is nonzero, create the appropriate node and install the decl
30820 in OMP_CLAUSE_DECL and add the node to the head of the list.
30822 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30823 return the list created.
30825 COLON can be NULL if only closing parenthesis should end the list,
30826 or pointer to bool which will receive false if the list is terminated
30827 by closing parenthesis or true if the list is terminated by colon. */
30829 static tree
30830 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30831 tree list, bool *colon)
30833 cp_token *token;
30834 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30835 if (colon)
30837 parser->colon_corrects_to_scope_p = false;
30838 *colon = false;
30840 while (1)
30842 tree name, decl;
30844 token = cp_lexer_peek_token (parser->lexer);
30845 if (kind != 0
30846 && current_class_ptr
30847 && cp_parser_is_keyword (token, RID_THIS))
30849 decl = finish_this_expr ();
30850 if (TREE_CODE (decl) == NON_LVALUE_EXPR
30851 || CONVERT_EXPR_P (decl))
30852 decl = TREE_OPERAND (decl, 0);
30853 cp_lexer_consume_token (parser->lexer);
30855 else
30857 name = cp_parser_id_expression (parser, /*template_p=*/false,
30858 /*check_dependency_p=*/true,
30859 /*template_p=*/NULL,
30860 /*declarator_p=*/false,
30861 /*optional_p=*/false);
30862 if (name == error_mark_node)
30863 goto skip_comma;
30865 decl = cp_parser_lookup_name_simple (parser, name, token->location);
30866 if (decl == error_mark_node)
30867 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
30868 token->location);
30870 if (decl == error_mark_node)
30872 else if (kind != 0)
30874 switch (kind)
30876 case OMP_CLAUSE__CACHE_:
30877 /* The OpenACC cache directive explicitly only allows "array
30878 elements or subarrays". */
30879 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
30881 error_at (token->location, "expected %<[%>");
30882 decl = error_mark_node;
30883 break;
30885 /* FALLTHROUGH. */
30886 case OMP_CLAUSE_MAP:
30887 case OMP_CLAUSE_FROM:
30888 case OMP_CLAUSE_TO:
30889 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
30891 location_t loc
30892 = cp_lexer_peek_token (parser->lexer)->location;
30893 cp_id_kind idk = CP_ID_KIND_NONE;
30894 cp_lexer_consume_token (parser->lexer);
30895 decl = convert_from_reference (decl);
30896 decl
30897 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
30898 decl, false,
30899 &idk, loc);
30901 /* FALLTHROUGH. */
30902 case OMP_CLAUSE_DEPEND:
30903 case OMP_CLAUSE_REDUCTION:
30904 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
30906 tree low_bound = NULL_TREE, length = NULL_TREE;
30908 parser->colon_corrects_to_scope_p = false;
30909 cp_lexer_consume_token (parser->lexer);
30910 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30911 low_bound = cp_parser_expression (parser);
30912 if (!colon)
30913 parser->colon_corrects_to_scope_p
30914 = saved_colon_corrects_to_scope_p;
30915 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
30916 length = integer_one_node;
30917 else
30919 /* Look for `:'. */
30920 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
30921 goto skip_comma;
30922 if (!cp_lexer_next_token_is (parser->lexer,
30923 CPP_CLOSE_SQUARE))
30924 length = cp_parser_expression (parser);
30926 /* Look for the closing `]'. */
30927 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
30928 RT_CLOSE_SQUARE))
30929 goto skip_comma;
30931 decl = tree_cons (low_bound, length, decl);
30933 break;
30934 default:
30935 break;
30938 tree u = build_omp_clause (token->location, kind);
30939 OMP_CLAUSE_DECL (u) = decl;
30940 OMP_CLAUSE_CHAIN (u) = list;
30941 list = u;
30943 else
30944 list = tree_cons (decl, NULL_TREE, list);
30946 get_comma:
30947 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
30948 break;
30949 cp_lexer_consume_token (parser->lexer);
30952 if (colon)
30953 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30955 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
30957 *colon = true;
30958 cp_parser_require (parser, CPP_COLON, RT_COLON);
30959 return list;
30962 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30964 int ending;
30966 /* Try to resync to an unnested comma. Copied from
30967 cp_parser_parenthesized_expression_list. */
30968 skip_comma:
30969 if (colon)
30970 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
30971 ending = cp_parser_skip_to_closing_parenthesis (parser,
30972 /*recovering=*/true,
30973 /*or_comma=*/true,
30974 /*consume_paren=*/true);
30975 if (ending < 0)
30976 goto get_comma;
30979 return list;
30982 /* Similarly, but expect leading and trailing parenthesis. This is a very
30983 common case for omp clauses. */
30985 static tree
30986 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
30988 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30989 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
30990 return list;
30993 /* OpenACC 2.0:
30994 copy ( variable-list )
30995 copyin ( variable-list )
30996 copyout ( variable-list )
30997 create ( variable-list )
30998 delete ( variable-list )
30999 present ( variable-list )
31000 present_or_copy ( variable-list )
31001 pcopy ( variable-list )
31002 present_or_copyin ( variable-list )
31003 pcopyin ( variable-list )
31004 present_or_copyout ( variable-list )
31005 pcopyout ( variable-list )
31006 present_or_create ( variable-list )
31007 pcreate ( variable-list ) */
31009 static tree
31010 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31011 tree list)
31013 enum gomp_map_kind kind;
31014 switch (c_kind)
31016 case PRAGMA_OACC_CLAUSE_COPY:
31017 kind = GOMP_MAP_FORCE_TOFROM;
31018 break;
31019 case PRAGMA_OACC_CLAUSE_COPYIN:
31020 kind = GOMP_MAP_FORCE_TO;
31021 break;
31022 case PRAGMA_OACC_CLAUSE_COPYOUT:
31023 kind = GOMP_MAP_FORCE_FROM;
31024 break;
31025 case PRAGMA_OACC_CLAUSE_CREATE:
31026 kind = GOMP_MAP_FORCE_ALLOC;
31027 break;
31028 case PRAGMA_OACC_CLAUSE_DELETE:
31029 kind = GOMP_MAP_DELETE;
31030 break;
31031 case PRAGMA_OACC_CLAUSE_DEVICE:
31032 kind = GOMP_MAP_FORCE_TO;
31033 break;
31034 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31035 kind = GOMP_MAP_DEVICE_RESIDENT;
31036 break;
31037 case PRAGMA_OACC_CLAUSE_HOST:
31038 case PRAGMA_OACC_CLAUSE_SELF:
31039 kind = GOMP_MAP_FORCE_FROM;
31040 break;
31041 case PRAGMA_OACC_CLAUSE_LINK:
31042 kind = GOMP_MAP_LINK;
31043 break;
31044 case PRAGMA_OACC_CLAUSE_PRESENT:
31045 kind = GOMP_MAP_FORCE_PRESENT;
31046 break;
31047 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31048 kind = GOMP_MAP_TOFROM;
31049 break;
31050 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31051 kind = GOMP_MAP_TO;
31052 break;
31053 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31054 kind = GOMP_MAP_FROM;
31055 break;
31056 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31057 kind = GOMP_MAP_ALLOC;
31058 break;
31059 default:
31060 gcc_unreachable ();
31062 tree nl, c;
31063 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31065 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31066 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31068 return nl;
31071 /* OpenACC 2.0:
31072 deviceptr ( variable-list ) */
31074 static tree
31075 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31077 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31078 tree vars, t;
31080 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31081 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31082 variable-list must only allow for pointer variables. */
31083 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31084 for (t = vars; t; t = TREE_CHAIN (t))
31086 tree v = TREE_PURPOSE (t);
31087 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31088 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31089 OMP_CLAUSE_DECL (u) = v;
31090 OMP_CLAUSE_CHAIN (u) = list;
31091 list = u;
31094 return list;
31097 /* OpenACC 2.0:
31098 auto
31099 independent
31100 nohost
31101 seq */
31103 static tree
31104 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31105 enum omp_clause_code code,
31106 tree list, location_t location)
31108 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31109 tree c = build_omp_clause (location, code);
31110 OMP_CLAUSE_CHAIN (c) = list;
31111 return c;
31114 /* OpenACC:
31115 num_gangs ( expression )
31116 num_workers ( expression )
31117 vector_length ( expression ) */
31119 static tree
31120 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31121 const char *str, tree list)
31123 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31125 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31126 return list;
31128 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31130 if (t == error_mark_node
31131 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31133 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31134 /*or_comma=*/false,
31135 /*consume_paren=*/true);
31136 return list;
31139 check_no_duplicate_clause (list, code, str, loc);
31141 tree c = build_omp_clause (loc, code);
31142 OMP_CLAUSE_OPERAND (c, 0) = t;
31143 OMP_CLAUSE_CHAIN (c) = list;
31144 return c;
31147 /* OpenACC:
31149 gang [( gang-arg-list )]
31150 worker [( [num:] int-expr )]
31151 vector [( [length:] int-expr )]
31153 where gang-arg is one of:
31155 [num:] int-expr
31156 static: size-expr
31158 and size-expr may be:
31161 int-expr
31164 static tree
31165 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31166 const char *str, tree list)
31168 const char *id = "num";
31169 cp_lexer *lexer = parser->lexer;
31170 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31171 location_t loc = cp_lexer_peek_token (lexer)->location;
31173 if (kind == OMP_CLAUSE_VECTOR)
31174 id = "length";
31176 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31178 cp_lexer_consume_token (lexer);
31182 cp_token *next = cp_lexer_peek_token (lexer);
31183 int idx = 0;
31185 /* Gang static argument. */
31186 if (kind == OMP_CLAUSE_GANG
31187 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31189 cp_lexer_consume_token (lexer);
31191 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31192 goto cleanup_error;
31194 idx = 1;
31195 if (ops[idx] != NULL)
31197 cp_parser_error (parser, "too many %<static%> arguments");
31198 goto cleanup_error;
31201 /* Check for the '*' argument. */
31202 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31203 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31204 || cp_lexer_nth_token_is (parser->lexer, 2,
31205 CPP_CLOSE_PAREN)))
31207 cp_lexer_consume_token (lexer);
31208 ops[idx] = integer_minus_one_node;
31210 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31212 cp_lexer_consume_token (lexer);
31213 continue;
31215 else break;
31218 /* Worker num: argument and vector length: arguments. */
31219 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31220 && strcmp (id, IDENTIFIER_POINTER (next->u.value)) == 0
31221 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31223 cp_lexer_consume_token (lexer); /* id */
31224 cp_lexer_consume_token (lexer); /* ':' */
31227 /* Now collect the actual argument. */
31228 if (ops[idx] != NULL_TREE)
31230 cp_parser_error (parser, "unexpected argument");
31231 goto cleanup_error;
31234 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31235 false);
31236 if (expr == error_mark_node)
31237 goto cleanup_error;
31239 mark_exp_read (expr);
31240 ops[idx] = expr;
31242 if (kind == OMP_CLAUSE_GANG
31243 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31245 cp_lexer_consume_token (lexer);
31246 continue;
31248 break;
31250 while (1);
31252 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31253 goto cleanup_error;
31256 check_no_duplicate_clause (list, kind, str, loc);
31258 c = build_omp_clause (loc, kind);
31260 if (ops[1])
31261 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31263 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31264 OMP_CLAUSE_CHAIN (c) = list;
31266 return c;
31268 cleanup_error:
31269 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31270 return list;
31273 /* OpenACC 2.0:
31274 tile ( size-expr-list ) */
31276 static tree
31277 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31279 tree c, expr = error_mark_node;
31280 tree tile = NULL_TREE;
31282 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31283 so, but the spec authors never considered such a case and have
31284 differing opinions on what it might mean, including 'not
31285 allowed'.) */
31286 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31287 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31288 clause_loc);
31290 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31291 return list;
31295 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31296 return list;
31298 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31299 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31300 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31302 cp_lexer_consume_token (parser->lexer);
31303 expr = integer_zero_node;
31305 else
31306 expr = cp_parser_constant_expression (parser);
31308 tile = tree_cons (NULL_TREE, expr, tile);
31310 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31312 /* Consume the trailing ')'. */
31313 cp_lexer_consume_token (parser->lexer);
31315 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31316 tile = nreverse (tile);
31317 OMP_CLAUSE_TILE_LIST (c) = tile;
31318 OMP_CLAUSE_CHAIN (c) = list;
31319 return c;
31322 /* OpenACC 2.0
31323 Parse wait clause or directive parameters. */
31325 static tree
31326 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31328 vec<tree, va_gc> *args;
31329 tree t, args_tree;
31331 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31332 /*cast_p=*/false,
31333 /*allow_expansion_p=*/true,
31334 /*non_constant_p=*/NULL);
31336 if (args == NULL || args->length () == 0)
31338 cp_parser_error (parser, "expected integer expression before ')'");
31339 if (args != NULL)
31340 release_tree_vector (args);
31341 return list;
31344 args_tree = build_tree_list_vec (args);
31346 release_tree_vector (args);
31348 for (t = args_tree; t; t = TREE_CHAIN (t))
31350 tree targ = TREE_VALUE (t);
31352 if (targ != error_mark_node)
31354 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31355 error ("%<wait%> expression must be integral");
31356 else
31358 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31360 mark_rvalue_use (targ);
31361 OMP_CLAUSE_DECL (c) = targ;
31362 OMP_CLAUSE_CHAIN (c) = list;
31363 list = c;
31368 return list;
31371 /* OpenACC:
31372 wait ( int-expr-list ) */
31374 static tree
31375 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31377 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31379 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31380 return list;
31382 list = cp_parser_oacc_wait_list (parser, location, list);
31384 return list;
31387 /* OpenMP 3.0:
31388 collapse ( constant-expression ) */
31390 static tree
31391 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31393 tree c, num;
31394 location_t loc;
31395 HOST_WIDE_INT n;
31397 loc = cp_lexer_peek_token (parser->lexer)->location;
31398 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31399 return list;
31401 num = cp_parser_constant_expression (parser);
31403 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31404 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31405 /*or_comma=*/false,
31406 /*consume_paren=*/true);
31408 if (num == error_mark_node)
31409 return list;
31410 num = fold_non_dependent_expr (num);
31411 if (!tree_fits_shwi_p (num)
31412 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31413 || (n = tree_to_shwi (num)) <= 0
31414 || (int) n != n)
31416 error_at (loc, "collapse argument needs positive constant integer expression");
31417 return list;
31420 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31421 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31422 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31423 OMP_CLAUSE_CHAIN (c) = list;
31424 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31426 return c;
31429 /* OpenMP 2.5:
31430 default ( shared | none )
31432 OpenACC 2.0
31433 default (none) */
31435 static tree
31436 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31437 location_t location, bool is_oacc)
31439 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31440 tree c;
31442 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31443 return list;
31444 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31446 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31447 const char *p = IDENTIFIER_POINTER (id);
31449 switch (p[0])
31451 case 'n':
31452 if (strcmp ("none", p) != 0)
31453 goto invalid_kind;
31454 kind = OMP_CLAUSE_DEFAULT_NONE;
31455 break;
31457 case 's':
31458 if (strcmp ("shared", p) != 0 || is_oacc)
31459 goto invalid_kind;
31460 kind = OMP_CLAUSE_DEFAULT_SHARED;
31461 break;
31463 default:
31464 goto invalid_kind;
31467 cp_lexer_consume_token (parser->lexer);
31469 else
31471 invalid_kind:
31472 if (is_oacc)
31473 cp_parser_error (parser, "expected %<none%>");
31474 else
31475 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31478 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31479 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31480 /*or_comma=*/false,
31481 /*consume_paren=*/true);
31483 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31484 return list;
31486 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31487 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31488 OMP_CLAUSE_CHAIN (c) = list;
31489 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31491 return c;
31494 /* OpenMP 3.1:
31495 final ( expression ) */
31497 static tree
31498 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31500 tree t, c;
31502 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31503 return list;
31505 t = cp_parser_condition (parser);
31507 if (t == error_mark_node
31508 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31509 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31510 /*or_comma=*/false,
31511 /*consume_paren=*/true);
31513 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31515 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31516 OMP_CLAUSE_FINAL_EXPR (c) = t;
31517 OMP_CLAUSE_CHAIN (c) = list;
31519 return c;
31522 /* OpenMP 2.5:
31523 if ( expression )
31525 OpenMP 4.5:
31526 if ( directive-name-modifier : expression )
31528 directive-name-modifier:
31529 parallel | task | taskloop | target data | target | target update
31530 | target enter data | target exit data */
31532 static tree
31533 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31534 bool is_omp)
31536 tree t, c;
31537 enum tree_code if_modifier = ERROR_MARK;
31539 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31540 return list;
31542 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31544 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31545 const char *p = IDENTIFIER_POINTER (id);
31546 int n = 2;
31548 if (strcmp ("parallel", p) == 0)
31549 if_modifier = OMP_PARALLEL;
31550 else if (strcmp ("task", p) == 0)
31551 if_modifier = OMP_TASK;
31552 else if (strcmp ("taskloop", p) == 0)
31553 if_modifier = OMP_TASKLOOP;
31554 else if (strcmp ("target", p) == 0)
31556 if_modifier = OMP_TARGET;
31557 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31559 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31560 p = IDENTIFIER_POINTER (id);
31561 if (strcmp ("data", p) == 0)
31562 if_modifier = OMP_TARGET_DATA;
31563 else if (strcmp ("update", p) == 0)
31564 if_modifier = OMP_TARGET_UPDATE;
31565 else if (strcmp ("enter", p) == 0)
31566 if_modifier = OMP_TARGET_ENTER_DATA;
31567 else if (strcmp ("exit", p) == 0)
31568 if_modifier = OMP_TARGET_EXIT_DATA;
31569 if (if_modifier != OMP_TARGET)
31570 n = 3;
31571 else
31573 location_t loc
31574 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31575 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31576 "or %<exit%>");
31577 if_modifier = ERROR_MARK;
31579 if (if_modifier == OMP_TARGET_ENTER_DATA
31580 || if_modifier == OMP_TARGET_EXIT_DATA)
31582 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31584 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31585 p = IDENTIFIER_POINTER (id);
31586 if (strcmp ("data", p) == 0)
31587 n = 4;
31589 if (n != 4)
31591 location_t loc
31592 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31593 error_at (loc, "expected %<data%>");
31594 if_modifier = ERROR_MARK;
31599 if (if_modifier != ERROR_MARK)
31601 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31603 while (n-- > 0)
31604 cp_lexer_consume_token (parser->lexer);
31606 else
31608 if (n > 2)
31610 location_t loc
31611 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31612 error_at (loc, "expected %<:%>");
31614 if_modifier = ERROR_MARK;
31619 t = cp_parser_condition (parser);
31621 if (t == error_mark_node
31622 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31623 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31624 /*or_comma=*/false,
31625 /*consume_paren=*/true);
31627 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31628 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31630 if (if_modifier != ERROR_MARK
31631 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31633 const char *p = NULL;
31634 switch (if_modifier)
31636 case OMP_PARALLEL: p = "parallel"; break;
31637 case OMP_TASK: p = "task"; break;
31638 case OMP_TASKLOOP: p = "taskloop"; break;
31639 case OMP_TARGET_DATA: p = "target data"; break;
31640 case OMP_TARGET: p = "target"; break;
31641 case OMP_TARGET_UPDATE: p = "target update"; break;
31642 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31643 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31644 default: gcc_unreachable ();
31646 error_at (location, "too many %<if%> clauses with %qs modifier",
31648 return list;
31650 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31652 if (!is_omp)
31653 error_at (location, "too many %<if%> clauses");
31654 else
31655 error_at (location, "too many %<if%> clauses without modifier");
31656 return list;
31658 else if (if_modifier == ERROR_MARK
31659 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31661 error_at (location, "if any %<if%> clause has modifier, then all "
31662 "%<if%> clauses have to use modifier");
31663 return list;
31667 c = build_omp_clause (location, OMP_CLAUSE_IF);
31668 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31669 OMP_CLAUSE_IF_EXPR (c) = t;
31670 OMP_CLAUSE_CHAIN (c) = list;
31672 return c;
31675 /* OpenMP 3.1:
31676 mergeable */
31678 static tree
31679 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31680 tree list, location_t location)
31682 tree c;
31684 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31685 location);
31687 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31688 OMP_CLAUSE_CHAIN (c) = list;
31689 return c;
31692 /* OpenMP 2.5:
31693 nowait */
31695 static tree
31696 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31697 tree list, location_t location)
31699 tree c;
31701 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31703 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31704 OMP_CLAUSE_CHAIN (c) = list;
31705 return c;
31708 /* OpenMP 2.5:
31709 num_threads ( expression ) */
31711 static tree
31712 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31713 location_t location)
31715 tree t, c;
31717 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31718 return list;
31720 t = cp_parser_expression (parser);
31722 if (t == error_mark_node
31723 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31724 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31725 /*or_comma=*/false,
31726 /*consume_paren=*/true);
31728 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31729 "num_threads", location);
31731 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31732 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31733 OMP_CLAUSE_CHAIN (c) = list;
31735 return c;
31738 /* OpenMP 4.5:
31739 num_tasks ( expression ) */
31741 static tree
31742 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31743 location_t location)
31745 tree t, c;
31747 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31748 return list;
31750 t = cp_parser_expression (parser);
31752 if (t == error_mark_node
31753 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31754 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31755 /*or_comma=*/false,
31756 /*consume_paren=*/true);
31758 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31759 "num_tasks", location);
31761 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31762 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31763 OMP_CLAUSE_CHAIN (c) = list;
31765 return c;
31768 /* OpenMP 4.5:
31769 grainsize ( expression ) */
31771 static tree
31772 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31773 location_t location)
31775 tree t, c;
31777 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31778 return list;
31780 t = cp_parser_expression (parser);
31782 if (t == error_mark_node
31783 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31784 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31785 /*or_comma=*/false,
31786 /*consume_paren=*/true);
31788 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31789 "grainsize", location);
31791 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31792 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31793 OMP_CLAUSE_CHAIN (c) = list;
31795 return c;
31798 /* OpenMP 4.5:
31799 priority ( expression ) */
31801 static tree
31802 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31803 location_t location)
31805 tree t, c;
31807 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31808 return list;
31810 t = cp_parser_expression (parser);
31812 if (t == error_mark_node
31813 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31814 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31815 /*or_comma=*/false,
31816 /*consume_paren=*/true);
31818 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31819 "priority", location);
31821 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31822 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31823 OMP_CLAUSE_CHAIN (c) = list;
31825 return c;
31828 /* OpenMP 4.5:
31829 hint ( expression ) */
31831 static tree
31832 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
31833 location_t location)
31835 tree t, c;
31837 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31838 return list;
31840 t = cp_parser_expression (parser);
31842 if (t == error_mark_node
31843 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31844 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31845 /*or_comma=*/false,
31846 /*consume_paren=*/true);
31848 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
31850 c = build_omp_clause (location, OMP_CLAUSE_HINT);
31851 OMP_CLAUSE_HINT_EXPR (c) = t;
31852 OMP_CLAUSE_CHAIN (c) = list;
31854 return c;
31857 /* OpenMP 4.5:
31858 defaultmap ( tofrom : scalar ) */
31860 static tree
31861 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
31862 location_t location)
31864 tree c, id;
31865 const char *p;
31867 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31868 return list;
31870 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31872 cp_parser_error (parser, "expected %<tofrom%>");
31873 goto out_err;
31875 id = cp_lexer_peek_token (parser->lexer)->u.value;
31876 p = IDENTIFIER_POINTER (id);
31877 if (strcmp (p, "tofrom") != 0)
31879 cp_parser_error (parser, "expected %<tofrom%>");
31880 goto out_err;
31882 cp_lexer_consume_token (parser->lexer);
31883 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31884 goto out_err;
31886 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31888 cp_parser_error (parser, "expected %<scalar%>");
31889 goto out_err;
31891 id = cp_lexer_peek_token (parser->lexer)->u.value;
31892 p = IDENTIFIER_POINTER (id);
31893 if (strcmp (p, "scalar") != 0)
31895 cp_parser_error (parser, "expected %<scalar%>");
31896 goto out_err;
31898 cp_lexer_consume_token (parser->lexer);
31899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31900 goto out_err;
31902 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
31903 location);
31905 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
31906 OMP_CLAUSE_CHAIN (c) = list;
31907 return c;
31909 out_err:
31910 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31911 /*or_comma=*/false,
31912 /*consume_paren=*/true);
31913 return list;
31916 /* OpenMP 2.5:
31917 ordered
31919 OpenMP 4.5:
31920 ordered ( constant-expression ) */
31922 static tree
31923 cp_parser_omp_clause_ordered (cp_parser *parser,
31924 tree list, location_t location)
31926 tree c, num = NULL_TREE;
31927 HOST_WIDE_INT n;
31929 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
31930 "ordered", location);
31932 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
31934 cp_lexer_consume_token (parser->lexer);
31936 num = cp_parser_constant_expression (parser);
31938 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31939 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31940 /*or_comma=*/false,
31941 /*consume_paren=*/true);
31943 if (num == error_mark_node)
31944 return list;
31945 num = fold_non_dependent_expr (num);
31946 if (!tree_fits_shwi_p (num)
31947 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31948 || (n = tree_to_shwi (num)) <= 0
31949 || (int) n != n)
31951 error_at (location,
31952 "ordered argument needs positive constant integer "
31953 "expression");
31954 return list;
31958 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
31959 OMP_CLAUSE_ORDERED_EXPR (c) = num;
31960 OMP_CLAUSE_CHAIN (c) = list;
31961 return c;
31964 /* OpenMP 2.5:
31965 reduction ( reduction-operator : variable-list )
31967 reduction-operator:
31968 One of: + * - & ^ | && ||
31970 OpenMP 3.1:
31972 reduction-operator:
31973 One of: + * - & ^ | && || min max
31975 OpenMP 4.0:
31977 reduction-operator:
31978 One of: + * - & ^ | && ||
31979 id-expression */
31981 static tree
31982 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
31984 enum tree_code code = ERROR_MARK;
31985 tree nlist, c, id = NULL_TREE;
31987 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31988 return list;
31990 switch (cp_lexer_peek_token (parser->lexer)->type)
31992 case CPP_PLUS: code = PLUS_EXPR; break;
31993 case CPP_MULT: code = MULT_EXPR; break;
31994 case CPP_MINUS: code = MINUS_EXPR; break;
31995 case CPP_AND: code = BIT_AND_EXPR; break;
31996 case CPP_XOR: code = BIT_XOR_EXPR; break;
31997 case CPP_OR: code = BIT_IOR_EXPR; break;
31998 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
31999 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32000 default: break;
32003 if (code != ERROR_MARK)
32004 cp_lexer_consume_token (parser->lexer);
32005 else
32007 bool saved_colon_corrects_to_scope_p;
32008 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32009 parser->colon_corrects_to_scope_p = false;
32010 id = cp_parser_id_expression (parser, /*template_p=*/false,
32011 /*check_dependency_p=*/true,
32012 /*template_p=*/NULL,
32013 /*declarator_p=*/false,
32014 /*optional_p=*/false);
32015 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32016 if (identifier_p (id))
32018 const char *p = IDENTIFIER_POINTER (id);
32020 if (strcmp (p, "min") == 0)
32021 code = MIN_EXPR;
32022 else if (strcmp (p, "max") == 0)
32023 code = MAX_EXPR;
32024 else if (id == cp_operator_id (PLUS_EXPR))
32025 code = PLUS_EXPR;
32026 else if (id == cp_operator_id (MULT_EXPR))
32027 code = MULT_EXPR;
32028 else if (id == cp_operator_id (MINUS_EXPR))
32029 code = MINUS_EXPR;
32030 else if (id == cp_operator_id (BIT_AND_EXPR))
32031 code = BIT_AND_EXPR;
32032 else if (id == cp_operator_id (BIT_IOR_EXPR))
32033 code = BIT_IOR_EXPR;
32034 else if (id == cp_operator_id (BIT_XOR_EXPR))
32035 code = BIT_XOR_EXPR;
32036 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32037 code = TRUTH_ANDIF_EXPR;
32038 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32039 code = TRUTH_ORIF_EXPR;
32040 id = omp_reduction_id (code, id, NULL_TREE);
32041 tree scope = parser->scope;
32042 if (scope)
32043 id = build_qualified_name (NULL_TREE, scope, id, false);
32044 parser->scope = NULL_TREE;
32045 parser->qualifying_scope = NULL_TREE;
32046 parser->object_scope = NULL_TREE;
32048 else
32050 error ("invalid reduction-identifier");
32051 resync_fail:
32052 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32053 /*or_comma=*/false,
32054 /*consume_paren=*/true);
32055 return list;
32059 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32060 goto resync_fail;
32062 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32063 NULL);
32064 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32066 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32067 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32070 return nlist;
32073 /* OpenMP 2.5:
32074 schedule ( schedule-kind )
32075 schedule ( schedule-kind , expression )
32077 schedule-kind:
32078 static | dynamic | guided | runtime | auto
32080 OpenMP 4.5:
32081 schedule ( schedule-modifier : schedule-kind )
32082 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32084 schedule-modifier:
32085 simd
32086 monotonic
32087 nonmonotonic */
32089 static tree
32090 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32092 tree c, t;
32093 int modifiers = 0, nmodifiers = 0;
32095 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32096 return list;
32098 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32100 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32102 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32103 const char *p = IDENTIFIER_POINTER (id);
32104 if (strcmp ("simd", p) == 0)
32105 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32106 else if (strcmp ("monotonic", p) == 0)
32107 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32108 else if (strcmp ("nonmonotonic", p) == 0)
32109 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32110 else
32111 break;
32112 cp_lexer_consume_token (parser->lexer);
32113 if (nmodifiers++ == 0
32114 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32115 cp_lexer_consume_token (parser->lexer);
32116 else
32118 cp_parser_require (parser, CPP_COLON, RT_COLON);
32119 break;
32123 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32125 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32126 const char *p = IDENTIFIER_POINTER (id);
32128 switch (p[0])
32130 case 'd':
32131 if (strcmp ("dynamic", p) != 0)
32132 goto invalid_kind;
32133 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32134 break;
32136 case 'g':
32137 if (strcmp ("guided", p) != 0)
32138 goto invalid_kind;
32139 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32140 break;
32142 case 'r':
32143 if (strcmp ("runtime", p) != 0)
32144 goto invalid_kind;
32145 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32146 break;
32148 default:
32149 goto invalid_kind;
32152 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32153 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32154 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32155 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32156 else
32157 goto invalid_kind;
32158 cp_lexer_consume_token (parser->lexer);
32160 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32161 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32162 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32163 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32165 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32166 "specified");
32167 modifiers = 0;
32170 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32172 cp_token *token;
32173 cp_lexer_consume_token (parser->lexer);
32175 token = cp_lexer_peek_token (parser->lexer);
32176 t = cp_parser_assignment_expression (parser);
32178 if (t == error_mark_node)
32179 goto resync_fail;
32180 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32181 error_at (token->location, "schedule %<runtime%> does not take "
32182 "a %<chunk_size%> parameter");
32183 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32184 error_at (token->location, "schedule %<auto%> does not take "
32185 "a %<chunk_size%> parameter");
32186 else
32187 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32189 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32190 goto resync_fail;
32192 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32193 goto resync_fail;
32195 OMP_CLAUSE_SCHEDULE_KIND (c)
32196 = (enum omp_clause_schedule_kind)
32197 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32199 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32200 OMP_CLAUSE_CHAIN (c) = list;
32201 return c;
32203 invalid_kind:
32204 cp_parser_error (parser, "invalid schedule kind");
32205 resync_fail:
32206 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32207 /*or_comma=*/false,
32208 /*consume_paren=*/true);
32209 return list;
32212 /* OpenMP 3.0:
32213 untied */
32215 static tree
32216 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32217 tree list, location_t location)
32219 tree c;
32221 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32223 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32224 OMP_CLAUSE_CHAIN (c) = list;
32225 return c;
32228 /* OpenMP 4.0:
32229 inbranch
32230 notinbranch */
32232 static tree
32233 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32234 tree list, location_t location)
32236 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32237 tree c = build_omp_clause (location, code);
32238 OMP_CLAUSE_CHAIN (c) = list;
32239 return c;
32242 /* OpenMP 4.0:
32243 parallel
32245 sections
32246 taskgroup */
32248 static tree
32249 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32250 enum omp_clause_code code,
32251 tree list, location_t location)
32253 tree c = build_omp_clause (location, code);
32254 OMP_CLAUSE_CHAIN (c) = list;
32255 return c;
32258 /* OpenMP 4.5:
32259 nogroup */
32261 static tree
32262 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32263 tree list, location_t location)
32265 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32266 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32267 OMP_CLAUSE_CHAIN (c) = list;
32268 return c;
32271 /* OpenMP 4.5:
32272 simd
32273 threads */
32275 static tree
32276 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32277 enum omp_clause_code code,
32278 tree list, location_t location)
32280 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32281 tree c = build_omp_clause (location, code);
32282 OMP_CLAUSE_CHAIN (c) = list;
32283 return c;
32286 /* OpenMP 4.0:
32287 num_teams ( expression ) */
32289 static tree
32290 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32291 location_t location)
32293 tree t, c;
32295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32296 return list;
32298 t = cp_parser_expression (parser);
32300 if (t == error_mark_node
32301 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32302 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32303 /*or_comma=*/false,
32304 /*consume_paren=*/true);
32306 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32307 "num_teams", location);
32309 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32310 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32311 OMP_CLAUSE_CHAIN (c) = list;
32313 return c;
32316 /* OpenMP 4.0:
32317 thread_limit ( expression ) */
32319 static tree
32320 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32321 location_t location)
32323 tree t, c;
32325 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32326 return list;
32328 t = cp_parser_expression (parser);
32330 if (t == error_mark_node
32331 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32332 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32333 /*or_comma=*/false,
32334 /*consume_paren=*/true);
32336 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32337 "thread_limit", location);
32339 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32340 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32341 OMP_CLAUSE_CHAIN (c) = list;
32343 return c;
32346 /* OpenMP 4.0:
32347 aligned ( variable-list )
32348 aligned ( variable-list : constant-expression ) */
32350 static tree
32351 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32353 tree nlist, c, alignment = NULL_TREE;
32354 bool colon;
32356 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32357 return list;
32359 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32360 &colon);
32362 if (colon)
32364 alignment = cp_parser_constant_expression (parser);
32366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32368 /*or_comma=*/false,
32369 /*consume_paren=*/true);
32371 if (alignment == error_mark_node)
32372 alignment = NULL_TREE;
32375 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32376 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32378 return nlist;
32381 /* OpenMP 4.0:
32382 linear ( variable-list )
32383 linear ( variable-list : expression )
32385 OpenMP 4.5:
32386 linear ( modifier ( variable-list ) )
32387 linear ( modifier ( variable-list ) : expression ) */
32389 static tree
32390 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32391 bool is_cilk_simd_fn, bool declare_simd)
32393 tree nlist, c, step = integer_one_node;
32394 bool colon;
32395 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32397 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32398 return list;
32400 if (!is_cilk_simd_fn
32401 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32403 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32404 const char *p = IDENTIFIER_POINTER (id);
32406 if (strcmp ("ref", p) == 0)
32407 kind = OMP_CLAUSE_LINEAR_REF;
32408 else if (strcmp ("val", p) == 0)
32409 kind = OMP_CLAUSE_LINEAR_VAL;
32410 else if (strcmp ("uval", p) == 0)
32411 kind = OMP_CLAUSE_LINEAR_UVAL;
32412 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32413 cp_lexer_consume_token (parser->lexer);
32414 else
32415 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32418 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32419 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32420 &colon);
32421 else
32423 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32424 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32425 if (colon)
32426 cp_parser_require (parser, CPP_COLON, RT_COLON);
32427 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32428 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32429 /*or_comma=*/false,
32430 /*consume_paren=*/true);
32433 if (colon)
32435 step = NULL_TREE;
32436 if (declare_simd
32437 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32438 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32440 cp_token *token = cp_lexer_peek_token (parser->lexer);
32441 cp_parser_parse_tentatively (parser);
32442 step = cp_parser_id_expression (parser, /*template_p=*/false,
32443 /*check_dependency_p=*/true,
32444 /*template_p=*/NULL,
32445 /*declarator_p=*/false,
32446 /*optional_p=*/false);
32447 if (step != error_mark_node)
32448 step = cp_parser_lookup_name_simple (parser, step, token->location);
32449 if (step == error_mark_node)
32451 step = NULL_TREE;
32452 cp_parser_abort_tentative_parse (parser);
32454 else if (!cp_parser_parse_definitely (parser))
32455 step = NULL_TREE;
32457 if (!step)
32458 step = cp_parser_expression (parser);
32460 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32462 sorry ("using parameters for %<linear%> step is not supported yet");
32463 step = integer_one_node;
32465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32466 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32467 /*or_comma=*/false,
32468 /*consume_paren=*/true);
32470 if (step == error_mark_node)
32471 return list;
32474 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32476 OMP_CLAUSE_LINEAR_STEP (c) = step;
32477 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32480 return nlist;
32483 /* OpenMP 4.0:
32484 safelen ( constant-expression ) */
32486 static tree
32487 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32488 location_t location)
32490 tree t, c;
32492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32493 return list;
32495 t = cp_parser_constant_expression (parser);
32497 if (t == error_mark_node
32498 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32499 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32500 /*or_comma=*/false,
32501 /*consume_paren=*/true);
32503 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32505 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32506 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32507 OMP_CLAUSE_CHAIN (c) = list;
32509 return c;
32512 /* OpenMP 4.0:
32513 simdlen ( constant-expression ) */
32515 static tree
32516 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32517 location_t location)
32519 tree t, c;
32521 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32522 return list;
32524 t = cp_parser_constant_expression (parser);
32526 if (t == error_mark_node
32527 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32528 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32529 /*or_comma=*/false,
32530 /*consume_paren=*/true);
32532 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32534 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32535 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32536 OMP_CLAUSE_CHAIN (c) = list;
32538 return c;
32541 /* OpenMP 4.5:
32542 vec:
32543 identifier [+/- integer]
32544 vec , identifier [+/- integer]
32547 static tree
32548 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32549 tree list)
32551 tree vec = NULL;
32553 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32555 cp_parser_error (parser, "expected identifier");
32556 return list;
32559 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32561 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32562 tree t, identifier = cp_parser_identifier (parser);
32563 tree addend = NULL;
32565 if (identifier == error_mark_node)
32566 t = error_mark_node;
32567 else
32569 t = cp_parser_lookup_name_simple
32570 (parser, identifier,
32571 cp_lexer_peek_token (parser->lexer)->location);
32572 if (t == error_mark_node)
32573 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32574 id_loc);
32577 bool neg = false;
32578 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32579 neg = true;
32580 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32582 addend = integer_zero_node;
32583 goto add_to_vector;
32585 cp_lexer_consume_token (parser->lexer);
32587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32589 cp_parser_error (parser, "expected integer");
32590 return list;
32593 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32594 if (TREE_CODE (addend) != INTEGER_CST)
32596 cp_parser_error (parser, "expected integer");
32597 return list;
32599 cp_lexer_consume_token (parser->lexer);
32601 add_to_vector:
32602 if (t != error_mark_node)
32604 vec = tree_cons (addend, t, vec);
32605 if (neg)
32606 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32609 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32610 break;
32612 cp_lexer_consume_token (parser->lexer);
32615 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32617 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32618 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32619 OMP_CLAUSE_DECL (u) = nreverse (vec);
32620 OMP_CLAUSE_CHAIN (u) = list;
32621 return u;
32623 return list;
32626 /* OpenMP 4.0:
32627 depend ( depend-kind : variable-list )
32629 depend-kind:
32630 in | out | inout
32632 OpenMP 4.5:
32633 depend ( source )
32635 depend ( sink : vec ) */
32637 static tree
32638 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32640 tree nlist, c;
32641 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32643 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32644 return list;
32646 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32648 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32649 const char *p = IDENTIFIER_POINTER (id);
32651 if (strcmp ("in", p) == 0)
32652 kind = OMP_CLAUSE_DEPEND_IN;
32653 else if (strcmp ("inout", p) == 0)
32654 kind = OMP_CLAUSE_DEPEND_INOUT;
32655 else if (strcmp ("out", p) == 0)
32656 kind = OMP_CLAUSE_DEPEND_OUT;
32657 else if (strcmp ("source", p) == 0)
32658 kind = OMP_CLAUSE_DEPEND_SOURCE;
32659 else if (strcmp ("sink", p) == 0)
32660 kind = OMP_CLAUSE_DEPEND_SINK;
32661 else
32662 goto invalid_kind;
32664 else
32665 goto invalid_kind;
32667 cp_lexer_consume_token (parser->lexer);
32669 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32671 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32672 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32673 OMP_CLAUSE_DECL (c) = NULL_TREE;
32674 OMP_CLAUSE_CHAIN (c) = list;
32675 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32677 /*or_comma=*/false,
32678 /*consume_paren=*/true);
32679 return c;
32682 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32683 goto resync_fail;
32685 if (kind == OMP_CLAUSE_DEPEND_SINK)
32686 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32687 else
32689 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32690 list, NULL);
32692 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32693 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32695 return nlist;
32697 invalid_kind:
32698 cp_parser_error (parser, "invalid depend kind");
32699 resync_fail:
32700 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32701 /*or_comma=*/false,
32702 /*consume_paren=*/true);
32703 return list;
32706 /* OpenMP 4.0:
32707 map ( map-kind : variable-list )
32708 map ( variable-list )
32710 map-kind:
32711 alloc | to | from | tofrom
32713 OpenMP 4.5:
32714 map-kind:
32715 alloc | to | from | tofrom | release | delete
32717 map ( always [,] map-kind: variable-list ) */
32719 static tree
32720 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32722 tree nlist, c;
32723 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32724 bool always = false;
32726 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32727 return list;
32729 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32731 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32732 const char *p = IDENTIFIER_POINTER (id);
32734 if (strcmp ("always", p) == 0)
32736 int nth = 2;
32737 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32738 nth++;
32739 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32740 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32741 == RID_DELETE))
32742 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32743 == CPP_COLON))
32745 always = true;
32746 cp_lexer_consume_token (parser->lexer);
32747 if (nth == 3)
32748 cp_lexer_consume_token (parser->lexer);
32753 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32754 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32756 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32757 const char *p = IDENTIFIER_POINTER (id);
32759 if (strcmp ("alloc", p) == 0)
32760 kind = GOMP_MAP_ALLOC;
32761 else if (strcmp ("to", p) == 0)
32762 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32763 else if (strcmp ("from", p) == 0)
32764 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32765 else if (strcmp ("tofrom", p) == 0)
32766 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32767 else if (strcmp ("release", p) == 0)
32768 kind = GOMP_MAP_RELEASE;
32769 else
32771 cp_parser_error (parser, "invalid map kind");
32772 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32773 /*or_comma=*/false,
32774 /*consume_paren=*/true);
32775 return list;
32777 cp_lexer_consume_token (parser->lexer);
32778 cp_lexer_consume_token (parser->lexer);
32780 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32781 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32783 kind = GOMP_MAP_DELETE;
32784 cp_lexer_consume_token (parser->lexer);
32785 cp_lexer_consume_token (parser->lexer);
32788 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32789 NULL);
32791 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32792 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32794 return nlist;
32797 /* OpenMP 4.0:
32798 device ( expression ) */
32800 static tree
32801 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32802 location_t location)
32804 tree t, c;
32806 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32807 return list;
32809 t = cp_parser_expression (parser);
32811 if (t == error_mark_node
32812 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32813 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32814 /*or_comma=*/false,
32815 /*consume_paren=*/true);
32817 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32818 "device", location);
32820 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32821 OMP_CLAUSE_DEVICE_ID (c) = t;
32822 OMP_CLAUSE_CHAIN (c) = list;
32824 return c;
32827 /* OpenMP 4.0:
32828 dist_schedule ( static )
32829 dist_schedule ( static , expression ) */
32831 static tree
32832 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
32833 location_t location)
32835 tree c, t;
32837 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32838 return list;
32840 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
32842 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32843 goto invalid_kind;
32844 cp_lexer_consume_token (parser->lexer);
32846 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32848 cp_lexer_consume_token (parser->lexer);
32850 t = cp_parser_assignment_expression (parser);
32852 if (t == error_mark_node)
32853 goto resync_fail;
32854 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
32856 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32857 goto resync_fail;
32859 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32860 goto resync_fail;
32862 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
32863 location);
32864 OMP_CLAUSE_CHAIN (c) = list;
32865 return c;
32867 invalid_kind:
32868 cp_parser_error (parser, "invalid dist_schedule kind");
32869 resync_fail:
32870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32871 /*or_comma=*/false,
32872 /*consume_paren=*/true);
32873 return list;
32876 /* OpenMP 4.0:
32877 proc_bind ( proc-bind-kind )
32879 proc-bind-kind:
32880 master | close | spread */
32882 static tree
32883 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
32884 location_t location)
32886 tree c;
32887 enum omp_clause_proc_bind_kind kind;
32889 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32890 return list;
32892 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32894 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32895 const char *p = IDENTIFIER_POINTER (id);
32897 if (strcmp ("master", p) == 0)
32898 kind = OMP_CLAUSE_PROC_BIND_MASTER;
32899 else if (strcmp ("close", p) == 0)
32900 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
32901 else if (strcmp ("spread", p) == 0)
32902 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
32903 else
32904 goto invalid_kind;
32906 else
32907 goto invalid_kind;
32909 cp_lexer_consume_token (parser->lexer);
32910 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32911 goto resync_fail;
32913 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
32914 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
32915 location);
32916 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
32917 OMP_CLAUSE_CHAIN (c) = list;
32918 return c;
32920 invalid_kind:
32921 cp_parser_error (parser, "invalid depend kind");
32922 resync_fail:
32923 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32924 /*or_comma=*/false,
32925 /*consume_paren=*/true);
32926 return list;
32929 /* OpenACC:
32930 async [( int-expr )] */
32932 static tree
32933 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
32935 tree c, t;
32936 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
32938 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
32940 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
32942 cp_lexer_consume_token (parser->lexer);
32944 t = cp_parser_expression (parser);
32945 if (t == error_mark_node
32946 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32947 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32948 /*or_comma=*/false,
32949 /*consume_paren=*/true);
32952 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
32954 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
32955 OMP_CLAUSE_ASYNC_EXPR (c) = t;
32956 OMP_CLAUSE_CHAIN (c) = list;
32957 list = c;
32959 return list;
32962 /* Parse all OpenACC clauses. The set clauses allowed by the directive
32963 is a bitmask in MASK. Return the list of clauses found. */
32965 static tree
32966 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
32967 const char *where, cp_token *pragma_tok,
32968 bool finish_p = true)
32970 tree clauses = NULL;
32971 bool first = true;
32973 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
32975 location_t here;
32976 pragma_omp_clause c_kind;
32977 omp_clause_code code;
32978 const char *c_name;
32979 tree prev = clauses;
32981 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32982 cp_lexer_consume_token (parser->lexer);
32984 here = cp_lexer_peek_token (parser->lexer)->location;
32985 c_kind = cp_parser_omp_clause_name (parser);
32987 switch (c_kind)
32989 case PRAGMA_OACC_CLAUSE_ASYNC:
32990 clauses = cp_parser_oacc_clause_async (parser, clauses);
32991 c_name = "async";
32992 break;
32993 case PRAGMA_OACC_CLAUSE_AUTO:
32994 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
32995 clauses, here);
32996 c_name = "auto";
32997 break;
32998 case PRAGMA_OACC_CLAUSE_COLLAPSE:
32999 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33000 c_name = "collapse";
33001 break;
33002 case PRAGMA_OACC_CLAUSE_COPY:
33003 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33004 c_name = "copy";
33005 break;
33006 case PRAGMA_OACC_CLAUSE_COPYIN:
33007 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33008 c_name = "copyin";
33009 break;
33010 case PRAGMA_OACC_CLAUSE_COPYOUT:
33011 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33012 c_name = "copyout";
33013 break;
33014 case PRAGMA_OACC_CLAUSE_CREATE:
33015 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33016 c_name = "create";
33017 break;
33018 case PRAGMA_OACC_CLAUSE_DELETE:
33019 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33020 c_name = "delete";
33021 break;
33022 case PRAGMA_OMP_CLAUSE_DEFAULT:
33023 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33024 c_name = "default";
33025 break;
33026 case PRAGMA_OACC_CLAUSE_DEVICE:
33027 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33028 c_name = "device";
33029 break;
33030 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33031 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33032 c_name = "deviceptr";
33033 break;
33034 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33035 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33036 c_name = "device_resident";
33037 break;
33038 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33039 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33040 clauses);
33041 c_name = "firstprivate";
33042 break;
33043 case PRAGMA_OACC_CLAUSE_GANG:
33044 c_name = "gang";
33045 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33046 c_name, clauses);
33047 break;
33048 case PRAGMA_OACC_CLAUSE_HOST:
33049 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33050 c_name = "host";
33051 break;
33052 case PRAGMA_OACC_CLAUSE_IF:
33053 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33054 c_name = "if";
33055 break;
33056 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33057 clauses = cp_parser_oacc_simple_clause (parser,
33058 OMP_CLAUSE_INDEPENDENT,
33059 clauses, here);
33060 c_name = "independent";
33061 break;
33062 case PRAGMA_OACC_CLAUSE_LINK:
33063 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33064 c_name = "link";
33065 break;
33066 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33067 code = OMP_CLAUSE_NUM_GANGS;
33068 c_name = "num_gangs";
33069 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33070 clauses);
33071 break;
33072 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33073 c_name = "num_workers";
33074 code = OMP_CLAUSE_NUM_WORKERS;
33075 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33076 clauses);
33077 break;
33078 case PRAGMA_OACC_CLAUSE_PRESENT:
33079 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33080 c_name = "present";
33081 break;
33082 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33083 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33084 c_name = "present_or_copy";
33085 break;
33086 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33087 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33088 c_name = "present_or_copyin";
33089 break;
33090 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33091 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33092 c_name = "present_or_copyout";
33093 break;
33094 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33095 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33096 c_name = "present_or_create";
33097 break;
33098 case PRAGMA_OACC_CLAUSE_PRIVATE:
33099 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33100 clauses);
33101 c_name = "private";
33102 break;
33103 case PRAGMA_OACC_CLAUSE_REDUCTION:
33104 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33105 c_name = "reduction";
33106 break;
33107 case PRAGMA_OACC_CLAUSE_SELF:
33108 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33109 c_name = "self";
33110 break;
33111 case PRAGMA_OACC_CLAUSE_SEQ:
33112 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33113 clauses, here);
33114 c_name = "seq";
33115 break;
33116 case PRAGMA_OACC_CLAUSE_TILE:
33117 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33118 c_name = "tile";
33119 break;
33120 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33121 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33122 clauses);
33123 c_name = "use_device";
33124 break;
33125 case PRAGMA_OACC_CLAUSE_VECTOR:
33126 c_name = "vector";
33127 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33128 c_name, clauses);
33129 break;
33130 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33131 c_name = "vector_length";
33132 code = OMP_CLAUSE_VECTOR_LENGTH;
33133 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33134 clauses);
33135 break;
33136 case PRAGMA_OACC_CLAUSE_WAIT:
33137 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33138 c_name = "wait";
33139 break;
33140 case PRAGMA_OACC_CLAUSE_WORKER:
33141 c_name = "worker";
33142 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33143 c_name, clauses);
33144 break;
33145 default:
33146 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33147 goto saw_error;
33150 first = false;
33152 if (((mask >> c_kind) & 1) == 0)
33154 /* Remove the invalid clause(s) from the list to avoid
33155 confusing the rest of the compiler. */
33156 clauses = prev;
33157 error_at (here, "%qs is not valid for %qs", c_name, where);
33161 saw_error:
33162 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33164 if (finish_p)
33165 return finish_omp_clauses (clauses, C_ORT_ACC);
33167 return clauses;
33170 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33171 is a bitmask in MASK. Return the list of clauses found; the result
33172 of clause default goes in *pdefault. */
33174 static tree
33175 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33176 const char *where, cp_token *pragma_tok,
33177 bool finish_p = true)
33179 tree clauses = NULL;
33180 bool first = true;
33181 cp_token *token = NULL;
33183 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33185 pragma_omp_clause c_kind;
33186 const char *c_name;
33187 tree prev = clauses;
33189 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33190 cp_lexer_consume_token (parser->lexer);
33192 token = cp_lexer_peek_token (parser->lexer);
33193 c_kind = cp_parser_omp_clause_name (parser);
33195 switch (c_kind)
33197 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33198 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33199 token->location);
33200 c_name = "collapse";
33201 break;
33202 case PRAGMA_OMP_CLAUSE_COPYIN:
33203 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33204 c_name = "copyin";
33205 break;
33206 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33207 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33208 clauses);
33209 c_name = "copyprivate";
33210 break;
33211 case PRAGMA_OMP_CLAUSE_DEFAULT:
33212 clauses = cp_parser_omp_clause_default (parser, clauses,
33213 token->location, false);
33214 c_name = "default";
33215 break;
33216 case PRAGMA_OMP_CLAUSE_FINAL:
33217 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33218 c_name = "final";
33219 break;
33220 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33221 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33222 clauses);
33223 c_name = "firstprivate";
33224 break;
33225 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33226 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33227 token->location);
33228 c_name = "grainsize";
33229 break;
33230 case PRAGMA_OMP_CLAUSE_HINT:
33231 clauses = cp_parser_omp_clause_hint (parser, clauses,
33232 token->location);
33233 c_name = "hint";
33234 break;
33235 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33236 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33237 token->location);
33238 c_name = "defaultmap";
33239 break;
33240 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33241 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33242 clauses);
33243 c_name = "use_device_ptr";
33244 break;
33245 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33246 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33247 clauses);
33248 c_name = "is_device_ptr";
33249 break;
33250 case PRAGMA_OMP_CLAUSE_IF:
33251 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33252 true);
33253 c_name = "if";
33254 break;
33255 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33256 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33257 clauses);
33258 c_name = "lastprivate";
33259 break;
33260 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33261 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33262 token->location);
33263 c_name = "mergeable";
33264 break;
33265 case PRAGMA_OMP_CLAUSE_NOWAIT:
33266 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33267 c_name = "nowait";
33268 break;
33269 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33270 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33271 token->location);
33272 c_name = "num_tasks";
33273 break;
33274 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33275 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33276 token->location);
33277 c_name = "num_threads";
33278 break;
33279 case PRAGMA_OMP_CLAUSE_ORDERED:
33280 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33281 token->location);
33282 c_name = "ordered";
33283 break;
33284 case PRAGMA_OMP_CLAUSE_PRIORITY:
33285 clauses = cp_parser_omp_clause_priority (parser, clauses,
33286 token->location);
33287 c_name = "priority";
33288 break;
33289 case PRAGMA_OMP_CLAUSE_PRIVATE:
33290 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33291 clauses);
33292 c_name = "private";
33293 break;
33294 case PRAGMA_OMP_CLAUSE_REDUCTION:
33295 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33296 c_name = "reduction";
33297 break;
33298 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33299 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33300 token->location);
33301 c_name = "schedule";
33302 break;
33303 case PRAGMA_OMP_CLAUSE_SHARED:
33304 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33305 clauses);
33306 c_name = "shared";
33307 break;
33308 case PRAGMA_OMP_CLAUSE_UNTIED:
33309 clauses = cp_parser_omp_clause_untied (parser, clauses,
33310 token->location);
33311 c_name = "untied";
33312 break;
33313 case PRAGMA_OMP_CLAUSE_INBRANCH:
33314 case PRAGMA_CILK_CLAUSE_MASK:
33315 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33316 clauses, token->location);
33317 c_name = "inbranch";
33318 break;
33319 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33320 case PRAGMA_CILK_CLAUSE_NOMASK:
33321 clauses = cp_parser_omp_clause_branch (parser,
33322 OMP_CLAUSE_NOTINBRANCH,
33323 clauses, token->location);
33324 c_name = "notinbranch";
33325 break;
33326 case PRAGMA_OMP_CLAUSE_PARALLEL:
33327 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33328 clauses, token->location);
33329 c_name = "parallel";
33330 if (!first)
33332 clause_not_first:
33333 error_at (token->location, "%qs must be the first clause of %qs",
33334 c_name, where);
33335 clauses = prev;
33337 break;
33338 case PRAGMA_OMP_CLAUSE_FOR:
33339 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33340 clauses, token->location);
33341 c_name = "for";
33342 if (!first)
33343 goto clause_not_first;
33344 break;
33345 case PRAGMA_OMP_CLAUSE_SECTIONS:
33346 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33347 clauses, token->location);
33348 c_name = "sections";
33349 if (!first)
33350 goto clause_not_first;
33351 break;
33352 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33353 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33354 clauses, token->location);
33355 c_name = "taskgroup";
33356 if (!first)
33357 goto clause_not_first;
33358 break;
33359 case PRAGMA_OMP_CLAUSE_LINK:
33360 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33361 c_name = "to";
33362 break;
33363 case PRAGMA_OMP_CLAUSE_TO:
33364 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33365 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33366 clauses);
33367 else
33368 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33369 c_name = "to";
33370 break;
33371 case PRAGMA_OMP_CLAUSE_FROM:
33372 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33373 c_name = "from";
33374 break;
33375 case PRAGMA_OMP_CLAUSE_UNIFORM:
33376 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33377 clauses);
33378 c_name = "uniform";
33379 break;
33380 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33381 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33382 token->location);
33383 c_name = "num_teams";
33384 break;
33385 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33386 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33387 token->location);
33388 c_name = "thread_limit";
33389 break;
33390 case PRAGMA_OMP_CLAUSE_ALIGNED:
33391 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33392 c_name = "aligned";
33393 break;
33394 case PRAGMA_OMP_CLAUSE_LINEAR:
33396 bool cilk_simd_fn = false, declare_simd = false;
33397 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33398 cilk_simd_fn = true;
33399 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33400 declare_simd = true;
33401 clauses = cp_parser_omp_clause_linear (parser, clauses,
33402 cilk_simd_fn, declare_simd);
33404 c_name = "linear";
33405 break;
33406 case PRAGMA_OMP_CLAUSE_DEPEND:
33407 clauses = cp_parser_omp_clause_depend (parser, clauses,
33408 token->location);
33409 c_name = "depend";
33410 break;
33411 case PRAGMA_OMP_CLAUSE_MAP:
33412 clauses = cp_parser_omp_clause_map (parser, clauses);
33413 c_name = "map";
33414 break;
33415 case PRAGMA_OMP_CLAUSE_DEVICE:
33416 clauses = cp_parser_omp_clause_device (parser, clauses,
33417 token->location);
33418 c_name = "device";
33419 break;
33420 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33421 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33422 token->location);
33423 c_name = "dist_schedule";
33424 break;
33425 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33426 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33427 token->location);
33428 c_name = "proc_bind";
33429 break;
33430 case PRAGMA_OMP_CLAUSE_SAFELEN:
33431 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33432 token->location);
33433 c_name = "safelen";
33434 break;
33435 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33436 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33437 token->location);
33438 c_name = "simdlen";
33439 break;
33440 case PRAGMA_OMP_CLAUSE_NOGROUP:
33441 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33442 token->location);
33443 c_name = "nogroup";
33444 break;
33445 case PRAGMA_OMP_CLAUSE_THREADS:
33446 clauses
33447 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33448 clauses, token->location);
33449 c_name = "threads";
33450 break;
33451 case PRAGMA_OMP_CLAUSE_SIMD:
33452 clauses
33453 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33454 clauses, token->location);
33455 c_name = "simd";
33456 break;
33457 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33458 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33459 c_name = "simdlen";
33460 break;
33461 default:
33462 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33463 goto saw_error;
33466 first = false;
33468 if (((mask >> c_kind) & 1) == 0)
33470 /* Remove the invalid clause(s) from the list to avoid
33471 confusing the rest of the compiler. */
33472 clauses = prev;
33473 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33476 saw_error:
33477 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33478 no reason to skip to the end. */
33479 if (!(flag_cilkplus && pragma_tok == NULL))
33480 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33481 if (finish_p)
33483 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33484 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33485 else
33486 return finish_omp_clauses (clauses, C_ORT_OMP);
33488 return clauses;
33491 /* OpenMP 2.5:
33492 structured-block:
33493 statement
33495 In practice, we're also interested in adding the statement to an
33496 outer node. So it is convenient if we work around the fact that
33497 cp_parser_statement calls add_stmt. */
33499 static unsigned
33500 cp_parser_begin_omp_structured_block (cp_parser *parser)
33502 unsigned save = parser->in_statement;
33504 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33505 This preserves the "not within loop or switch" style error messages
33506 for nonsense cases like
33507 void foo() {
33508 #pragma omp single
33509 break;
33512 if (parser->in_statement)
33513 parser->in_statement = IN_OMP_BLOCK;
33515 return save;
33518 static void
33519 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33521 parser->in_statement = save;
33524 static tree
33525 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33527 tree stmt = begin_omp_structured_block ();
33528 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33530 cp_parser_statement (parser, NULL_TREE, false, if_p);
33532 cp_parser_end_omp_structured_block (parser, save);
33533 return finish_omp_structured_block (stmt);
33536 /* OpenMP 2.5:
33537 # pragma omp atomic new-line
33538 expression-stmt
33540 expression-stmt:
33541 x binop= expr | x++ | ++x | x-- | --x
33542 binop:
33543 +, *, -, /, &, ^, |, <<, >>
33545 where x is an lvalue expression with scalar type.
33547 OpenMP 3.1:
33548 # pragma omp atomic new-line
33549 update-stmt
33551 # pragma omp atomic read new-line
33552 read-stmt
33554 # pragma omp atomic write new-line
33555 write-stmt
33557 # pragma omp atomic update new-line
33558 update-stmt
33560 # pragma omp atomic capture new-line
33561 capture-stmt
33563 # pragma omp atomic capture new-line
33564 capture-block
33566 read-stmt:
33567 v = x
33568 write-stmt:
33569 x = expr
33570 update-stmt:
33571 expression-stmt | x = x binop expr
33572 capture-stmt:
33573 v = expression-stmt
33574 capture-block:
33575 { v = x; update-stmt; } | { update-stmt; v = x; }
33577 OpenMP 4.0:
33578 update-stmt:
33579 expression-stmt | x = x binop expr | x = expr binop x
33580 capture-stmt:
33581 v = update-stmt
33582 capture-block:
33583 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33585 where x and v are lvalue expressions with scalar type. */
33587 static void
33588 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33590 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33591 tree rhs1 = NULL_TREE, orig_lhs;
33592 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33593 bool structured_block = false;
33594 bool seq_cst = false;
33596 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33598 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33599 const char *p = IDENTIFIER_POINTER (id);
33601 if (!strcmp (p, "seq_cst"))
33603 seq_cst = true;
33604 cp_lexer_consume_token (parser->lexer);
33605 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33606 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33607 cp_lexer_consume_token (parser->lexer);
33610 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33612 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33613 const char *p = IDENTIFIER_POINTER (id);
33615 if (!strcmp (p, "read"))
33616 code = OMP_ATOMIC_READ;
33617 else if (!strcmp (p, "write"))
33618 code = NOP_EXPR;
33619 else if (!strcmp (p, "update"))
33620 code = OMP_ATOMIC;
33621 else if (!strcmp (p, "capture"))
33622 code = OMP_ATOMIC_CAPTURE_NEW;
33623 else
33624 p = NULL;
33625 if (p)
33626 cp_lexer_consume_token (parser->lexer);
33628 if (!seq_cst)
33630 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33631 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33632 cp_lexer_consume_token (parser->lexer);
33634 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33636 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33637 const char *p = IDENTIFIER_POINTER (id);
33639 if (!strcmp (p, "seq_cst"))
33641 seq_cst = true;
33642 cp_lexer_consume_token (parser->lexer);
33646 cp_parser_require_pragma_eol (parser, pragma_tok);
33648 switch (code)
33650 case OMP_ATOMIC_READ:
33651 case NOP_EXPR: /* atomic write */
33652 v = cp_parser_unary_expression (parser);
33653 if (v == error_mark_node)
33654 goto saw_error;
33655 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33656 goto saw_error;
33657 if (code == NOP_EXPR)
33658 lhs = cp_parser_expression (parser);
33659 else
33660 lhs = cp_parser_unary_expression (parser);
33661 if (lhs == error_mark_node)
33662 goto saw_error;
33663 if (code == NOP_EXPR)
33665 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33666 opcode. */
33667 code = OMP_ATOMIC;
33668 rhs = lhs;
33669 lhs = v;
33670 v = NULL_TREE;
33672 goto done;
33673 case OMP_ATOMIC_CAPTURE_NEW:
33674 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33676 cp_lexer_consume_token (parser->lexer);
33677 structured_block = true;
33679 else
33681 v = cp_parser_unary_expression (parser);
33682 if (v == error_mark_node)
33683 goto saw_error;
33684 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33685 goto saw_error;
33687 default:
33688 break;
33691 restart:
33692 lhs = cp_parser_unary_expression (parser);
33693 orig_lhs = lhs;
33694 switch (TREE_CODE (lhs))
33696 case ERROR_MARK:
33697 goto saw_error;
33699 case POSTINCREMENT_EXPR:
33700 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33701 code = OMP_ATOMIC_CAPTURE_OLD;
33702 /* FALLTHROUGH */
33703 case PREINCREMENT_EXPR:
33704 lhs = TREE_OPERAND (lhs, 0);
33705 opcode = PLUS_EXPR;
33706 rhs = integer_one_node;
33707 break;
33709 case POSTDECREMENT_EXPR:
33710 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33711 code = OMP_ATOMIC_CAPTURE_OLD;
33712 /* FALLTHROUGH */
33713 case PREDECREMENT_EXPR:
33714 lhs = TREE_OPERAND (lhs, 0);
33715 opcode = MINUS_EXPR;
33716 rhs = integer_one_node;
33717 break;
33719 case COMPOUND_EXPR:
33720 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33721 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33722 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33723 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33724 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33725 (TREE_OPERAND (lhs, 1), 0), 0)))
33726 == BOOLEAN_TYPE)
33727 /* Undo effects of boolean_increment for post {in,de}crement. */
33728 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33729 /* FALLTHRU */
33730 case MODIFY_EXPR:
33731 if (TREE_CODE (lhs) == MODIFY_EXPR
33732 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33734 /* Undo effects of boolean_increment. */
33735 if (integer_onep (TREE_OPERAND (lhs, 1)))
33737 /* This is pre or post increment. */
33738 rhs = TREE_OPERAND (lhs, 1);
33739 lhs = TREE_OPERAND (lhs, 0);
33740 opcode = NOP_EXPR;
33741 if (code == OMP_ATOMIC_CAPTURE_NEW
33742 && !structured_block
33743 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33744 code = OMP_ATOMIC_CAPTURE_OLD;
33745 break;
33748 /* FALLTHRU */
33749 default:
33750 switch (cp_lexer_peek_token (parser->lexer)->type)
33752 case CPP_MULT_EQ:
33753 opcode = MULT_EXPR;
33754 break;
33755 case CPP_DIV_EQ:
33756 opcode = TRUNC_DIV_EXPR;
33757 break;
33758 case CPP_PLUS_EQ:
33759 opcode = PLUS_EXPR;
33760 break;
33761 case CPP_MINUS_EQ:
33762 opcode = MINUS_EXPR;
33763 break;
33764 case CPP_LSHIFT_EQ:
33765 opcode = LSHIFT_EXPR;
33766 break;
33767 case CPP_RSHIFT_EQ:
33768 opcode = RSHIFT_EXPR;
33769 break;
33770 case CPP_AND_EQ:
33771 opcode = BIT_AND_EXPR;
33772 break;
33773 case CPP_OR_EQ:
33774 opcode = BIT_IOR_EXPR;
33775 break;
33776 case CPP_XOR_EQ:
33777 opcode = BIT_XOR_EXPR;
33778 break;
33779 case CPP_EQ:
33780 enum cp_parser_prec oprec;
33781 cp_token *token;
33782 cp_lexer_consume_token (parser->lexer);
33783 cp_parser_parse_tentatively (parser);
33784 rhs1 = cp_parser_simple_cast_expression (parser);
33785 if (rhs1 == error_mark_node)
33787 cp_parser_abort_tentative_parse (parser);
33788 cp_parser_simple_cast_expression (parser);
33789 goto saw_error;
33791 token = cp_lexer_peek_token (parser->lexer);
33792 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33794 cp_parser_abort_tentative_parse (parser);
33795 cp_parser_parse_tentatively (parser);
33796 rhs = cp_parser_binary_expression (parser, false, true,
33797 PREC_NOT_OPERATOR, NULL);
33798 if (rhs == error_mark_node)
33800 cp_parser_abort_tentative_parse (parser);
33801 cp_parser_binary_expression (parser, false, true,
33802 PREC_NOT_OPERATOR, NULL);
33803 goto saw_error;
33805 switch (TREE_CODE (rhs))
33807 case MULT_EXPR:
33808 case TRUNC_DIV_EXPR:
33809 case RDIV_EXPR:
33810 case PLUS_EXPR:
33811 case MINUS_EXPR:
33812 case LSHIFT_EXPR:
33813 case RSHIFT_EXPR:
33814 case BIT_AND_EXPR:
33815 case BIT_IOR_EXPR:
33816 case BIT_XOR_EXPR:
33817 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33819 if (cp_parser_parse_definitely (parser))
33821 opcode = TREE_CODE (rhs);
33822 rhs1 = TREE_OPERAND (rhs, 0);
33823 rhs = TREE_OPERAND (rhs, 1);
33824 goto stmt_done;
33826 else
33827 goto saw_error;
33829 break;
33830 default:
33831 break;
33833 cp_parser_abort_tentative_parse (parser);
33834 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
33836 rhs = cp_parser_expression (parser);
33837 if (rhs == error_mark_node)
33838 goto saw_error;
33839 opcode = NOP_EXPR;
33840 rhs1 = NULL_TREE;
33841 goto stmt_done;
33843 cp_parser_error (parser,
33844 "invalid form of %<#pragma omp atomic%>");
33845 goto saw_error;
33847 if (!cp_parser_parse_definitely (parser))
33848 goto saw_error;
33849 switch (token->type)
33851 case CPP_SEMICOLON:
33852 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33854 code = OMP_ATOMIC_CAPTURE_OLD;
33855 v = lhs;
33856 lhs = NULL_TREE;
33857 lhs1 = rhs1;
33858 rhs1 = NULL_TREE;
33859 cp_lexer_consume_token (parser->lexer);
33860 goto restart;
33862 else if (structured_block)
33864 opcode = NOP_EXPR;
33865 rhs = rhs1;
33866 rhs1 = NULL_TREE;
33867 goto stmt_done;
33869 cp_parser_error (parser,
33870 "invalid form of %<#pragma omp atomic%>");
33871 goto saw_error;
33872 case CPP_MULT:
33873 opcode = MULT_EXPR;
33874 break;
33875 case CPP_DIV:
33876 opcode = TRUNC_DIV_EXPR;
33877 break;
33878 case CPP_PLUS:
33879 opcode = PLUS_EXPR;
33880 break;
33881 case CPP_MINUS:
33882 opcode = MINUS_EXPR;
33883 break;
33884 case CPP_LSHIFT:
33885 opcode = LSHIFT_EXPR;
33886 break;
33887 case CPP_RSHIFT:
33888 opcode = RSHIFT_EXPR;
33889 break;
33890 case CPP_AND:
33891 opcode = BIT_AND_EXPR;
33892 break;
33893 case CPP_OR:
33894 opcode = BIT_IOR_EXPR;
33895 break;
33896 case CPP_XOR:
33897 opcode = BIT_XOR_EXPR;
33898 break;
33899 default:
33900 cp_parser_error (parser,
33901 "invalid operator for %<#pragma omp atomic%>");
33902 goto saw_error;
33904 oprec = TOKEN_PRECEDENCE (token);
33905 gcc_assert (oprec != PREC_NOT_OPERATOR);
33906 if (commutative_tree_code (opcode))
33907 oprec = (enum cp_parser_prec) (oprec - 1);
33908 cp_lexer_consume_token (parser->lexer);
33909 rhs = cp_parser_binary_expression (parser, false, false,
33910 oprec, NULL);
33911 if (rhs == error_mark_node)
33912 goto saw_error;
33913 goto stmt_done;
33914 /* FALLTHROUGH */
33915 default:
33916 cp_parser_error (parser,
33917 "invalid operator for %<#pragma omp atomic%>");
33918 goto saw_error;
33920 cp_lexer_consume_token (parser->lexer);
33922 rhs = cp_parser_expression (parser);
33923 if (rhs == error_mark_node)
33924 goto saw_error;
33925 break;
33927 stmt_done:
33928 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
33930 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
33931 goto saw_error;
33932 v = cp_parser_unary_expression (parser);
33933 if (v == error_mark_node)
33934 goto saw_error;
33935 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33936 goto saw_error;
33937 lhs1 = cp_parser_unary_expression (parser);
33938 if (lhs1 == error_mark_node)
33939 goto saw_error;
33941 if (structured_block)
33943 cp_parser_consume_semicolon_at_end_of_statement (parser);
33944 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
33946 done:
33947 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
33948 if (!structured_block)
33949 cp_parser_consume_semicolon_at_end_of_statement (parser);
33950 return;
33952 saw_error:
33953 cp_parser_skip_to_end_of_block_or_statement (parser);
33954 if (structured_block)
33956 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33957 cp_lexer_consume_token (parser->lexer);
33958 else if (code == OMP_ATOMIC_CAPTURE_NEW)
33960 cp_parser_skip_to_end_of_block_or_statement (parser);
33961 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
33962 cp_lexer_consume_token (parser->lexer);
33968 /* OpenMP 2.5:
33969 # pragma omp barrier new-line */
33971 static void
33972 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
33974 cp_parser_require_pragma_eol (parser, pragma_tok);
33975 finish_omp_barrier ();
33978 /* OpenMP 2.5:
33979 # pragma omp critical [(name)] new-line
33980 structured-block
33982 OpenMP 4.5:
33983 # pragma omp critical [(name) [hint(expression)]] new-line
33984 structured-block */
33986 #define OMP_CRITICAL_CLAUSE_MASK \
33987 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
33989 static tree
33990 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
33992 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
33994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
33996 cp_lexer_consume_token (parser->lexer);
33998 name = cp_parser_identifier (parser);
34000 if (name == error_mark_node
34001 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34002 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34003 /*or_comma=*/false,
34004 /*consume_paren=*/true);
34005 if (name == error_mark_node)
34006 name = NULL;
34008 clauses = cp_parser_omp_all_clauses (parser,
34009 OMP_CRITICAL_CLAUSE_MASK,
34010 "#pragma omp critical", pragma_tok);
34012 else
34013 cp_parser_require_pragma_eol (parser, pragma_tok);
34015 stmt = cp_parser_omp_structured_block (parser, if_p);
34016 return c_finish_omp_critical (input_location, stmt, name, clauses);
34019 /* OpenMP 2.5:
34020 # pragma omp flush flush-vars[opt] new-line
34022 flush-vars:
34023 ( variable-list ) */
34025 static void
34026 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34028 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34029 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34030 cp_parser_require_pragma_eol (parser, pragma_tok);
34032 finish_omp_flush ();
34035 /* Helper function, to parse omp for increment expression. */
34037 static tree
34038 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34040 tree cond = cp_parser_binary_expression (parser, false, true,
34041 PREC_NOT_OPERATOR, NULL);
34042 if (cond == error_mark_node
34043 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34045 cp_parser_skip_to_end_of_statement (parser);
34046 return error_mark_node;
34049 switch (TREE_CODE (cond))
34051 case GT_EXPR:
34052 case GE_EXPR:
34053 case LT_EXPR:
34054 case LE_EXPR:
34055 break;
34056 case NE_EXPR:
34057 if (code == CILK_SIMD || code == CILK_FOR)
34058 break;
34059 /* Fall through: OpenMP disallows NE_EXPR. */
34060 gcc_fallthrough ();
34061 default:
34062 return error_mark_node;
34065 /* If decl is an iterator, preserve LHS and RHS of the relational
34066 expr until finish_omp_for. */
34067 if (decl
34068 && (type_dependent_expression_p (decl)
34069 || CLASS_TYPE_P (TREE_TYPE (decl))))
34070 return cond;
34072 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34073 TREE_CODE (cond),
34074 TREE_OPERAND (cond, 0), ERROR_MARK,
34075 TREE_OPERAND (cond, 1), ERROR_MARK,
34076 /*overload=*/NULL, tf_warning_or_error);
34079 /* Helper function, to parse omp for increment expression. */
34081 static tree
34082 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34084 cp_token *token = cp_lexer_peek_token (parser->lexer);
34085 enum tree_code op;
34086 tree lhs, rhs;
34087 cp_id_kind idk;
34088 bool decl_first;
34090 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34092 op = (token->type == CPP_PLUS_PLUS
34093 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34094 cp_lexer_consume_token (parser->lexer);
34095 lhs = cp_parser_simple_cast_expression (parser);
34096 if (lhs != decl
34097 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34098 return error_mark_node;
34099 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34102 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34103 if (lhs != decl
34104 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34105 return error_mark_node;
34107 token = cp_lexer_peek_token (parser->lexer);
34108 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34110 op = (token->type == CPP_PLUS_PLUS
34111 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34112 cp_lexer_consume_token (parser->lexer);
34113 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34116 op = cp_parser_assignment_operator_opt (parser);
34117 if (op == ERROR_MARK)
34118 return error_mark_node;
34120 if (op != NOP_EXPR)
34122 rhs = cp_parser_assignment_expression (parser);
34123 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34124 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34127 lhs = cp_parser_binary_expression (parser, false, false,
34128 PREC_ADDITIVE_EXPRESSION, NULL);
34129 token = cp_lexer_peek_token (parser->lexer);
34130 decl_first = (lhs == decl
34131 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34132 if (decl_first)
34133 lhs = NULL_TREE;
34134 if (token->type != CPP_PLUS
34135 && token->type != CPP_MINUS)
34136 return error_mark_node;
34140 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34141 cp_lexer_consume_token (parser->lexer);
34142 rhs = cp_parser_binary_expression (parser, false, false,
34143 PREC_ADDITIVE_EXPRESSION, NULL);
34144 token = cp_lexer_peek_token (parser->lexer);
34145 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34147 if (lhs == NULL_TREE)
34149 if (op == PLUS_EXPR)
34150 lhs = rhs;
34151 else
34152 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34153 tf_warning_or_error);
34155 else
34156 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34157 ERROR_MARK, NULL, tf_warning_or_error);
34160 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34162 if (!decl_first)
34164 if ((rhs != decl
34165 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34166 || op == MINUS_EXPR)
34167 return error_mark_node;
34168 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34170 else
34171 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34173 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34176 /* Parse the initialization statement of either an OpenMP for loop or
34177 a Cilk Plus for loop.
34179 Return true if the resulting construct should have an
34180 OMP_CLAUSE_PRIVATE added to it. */
34182 static tree
34183 cp_parser_omp_for_loop_init (cp_parser *parser,
34184 enum tree_code code,
34185 tree &this_pre_body,
34186 vec<tree, va_gc> *for_block,
34187 tree &init,
34188 tree &orig_init,
34189 tree &decl,
34190 tree &real_decl)
34192 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34193 return NULL_TREE;
34195 tree add_private_clause = NULL_TREE;
34197 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34199 init-expr:
34200 var = lb
34201 integer-type var = lb
34202 random-access-iterator-type var = lb
34203 pointer-type var = lb
34205 cp_decl_specifier_seq type_specifiers;
34207 /* First, try to parse as an initialized declaration. See
34208 cp_parser_condition, from whence the bulk of this is copied. */
34210 cp_parser_parse_tentatively (parser);
34211 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34212 /*is_trailing_return=*/false,
34213 &type_specifiers);
34214 if (cp_parser_parse_definitely (parser))
34216 /* If parsing a type specifier seq succeeded, then this
34217 MUST be a initialized declaration. */
34218 tree asm_specification, attributes;
34219 cp_declarator *declarator;
34221 declarator = cp_parser_declarator (parser,
34222 CP_PARSER_DECLARATOR_NAMED,
34223 /*ctor_dtor_or_conv_p=*/NULL,
34224 /*parenthesized_p=*/NULL,
34225 /*member_p=*/false,
34226 /*friend_p=*/false);
34227 attributes = cp_parser_attributes_opt (parser);
34228 asm_specification = cp_parser_asm_specification_opt (parser);
34230 if (declarator == cp_error_declarator)
34231 cp_parser_skip_to_end_of_statement (parser);
34233 else
34235 tree pushed_scope, auto_node;
34237 decl = start_decl (declarator, &type_specifiers,
34238 SD_INITIALIZED, attributes,
34239 /*prefix_attributes=*/NULL_TREE,
34240 &pushed_scope);
34242 auto_node = type_uses_auto (TREE_TYPE (decl));
34243 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34245 if (cp_lexer_next_token_is (parser->lexer,
34246 CPP_OPEN_PAREN))
34248 if (code != CILK_SIMD && code != CILK_FOR)
34249 error ("parenthesized initialization is not allowed in "
34250 "OpenMP %<for%> loop");
34251 else
34252 error ("parenthesized initialization is "
34253 "not allowed in for-loop");
34255 else
34256 /* Trigger an error. */
34257 cp_parser_require (parser, CPP_EQ, RT_EQ);
34259 init = error_mark_node;
34260 cp_parser_skip_to_end_of_statement (parser);
34262 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34263 || type_dependent_expression_p (decl)
34264 || auto_node)
34266 bool is_direct_init, is_non_constant_init;
34268 init = cp_parser_initializer (parser,
34269 &is_direct_init,
34270 &is_non_constant_init);
34272 if (auto_node)
34274 TREE_TYPE (decl)
34275 = do_auto_deduction (TREE_TYPE (decl), init,
34276 auto_node);
34278 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34279 && !type_dependent_expression_p (decl))
34280 goto non_class;
34283 cp_finish_decl (decl, init, !is_non_constant_init,
34284 asm_specification,
34285 LOOKUP_ONLYCONVERTING);
34286 orig_init = init;
34287 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34289 vec_safe_push (for_block, this_pre_body);
34290 init = NULL_TREE;
34292 else
34294 init = pop_stmt_list (this_pre_body);
34295 if (init && TREE_CODE (init) == STATEMENT_LIST)
34297 tree_stmt_iterator i = tsi_start (init);
34298 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34299 while (!tsi_end_p (i))
34301 tree t = tsi_stmt (i);
34302 if (TREE_CODE (t) == DECL_EXPR
34303 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34305 tsi_delink (&i);
34306 vec_safe_push (for_block, t);
34307 continue;
34309 break;
34311 if (tsi_one_before_end_p (i))
34313 tree t = tsi_stmt (i);
34314 tsi_delink (&i);
34315 free_stmt_list (init);
34316 init = t;
34320 this_pre_body = NULL_TREE;
34322 else
34324 /* Consume '='. */
34325 cp_lexer_consume_token (parser->lexer);
34326 init = cp_parser_assignment_expression (parser);
34328 non_class:
34329 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34330 init = error_mark_node;
34331 else
34332 cp_finish_decl (decl, NULL_TREE,
34333 /*init_const_expr_p=*/false,
34334 asm_specification,
34335 LOOKUP_ONLYCONVERTING);
34338 if (pushed_scope)
34339 pop_scope (pushed_scope);
34342 else
34344 cp_id_kind idk;
34345 /* If parsing a type specifier sequence failed, then
34346 this MUST be a simple expression. */
34347 if (code == CILK_FOR)
34348 error ("%<_Cilk_for%> allows expression instead of declaration only "
34349 "in C, not in C++");
34350 cp_parser_parse_tentatively (parser);
34351 decl = cp_parser_primary_expression (parser, false, false,
34352 false, &idk);
34353 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34354 if (!cp_parser_error_occurred (parser)
34355 && decl
34356 && (TREE_CODE (decl) == COMPONENT_REF
34357 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34359 cp_parser_abort_tentative_parse (parser);
34360 cp_parser_parse_tentatively (parser);
34361 cp_token *token = cp_lexer_peek_token (parser->lexer);
34362 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34363 /*check_dependency_p=*/true,
34364 /*template_p=*/NULL,
34365 /*declarator_p=*/false,
34366 /*optional_p=*/false);
34367 if (name != error_mark_node
34368 && last_tok == cp_lexer_peek_token (parser->lexer))
34370 decl = cp_parser_lookup_name_simple (parser, name,
34371 token->location);
34372 if (TREE_CODE (decl) == FIELD_DECL)
34373 add_private_clause = omp_privatize_field (decl, false);
34375 cp_parser_abort_tentative_parse (parser);
34376 cp_parser_parse_tentatively (parser);
34377 decl = cp_parser_primary_expression (parser, false, false,
34378 false, &idk);
34380 if (!cp_parser_error_occurred (parser)
34381 && decl
34382 && DECL_P (decl)
34383 && CLASS_TYPE_P (TREE_TYPE (decl)))
34385 tree rhs;
34387 cp_parser_parse_definitely (parser);
34388 cp_parser_require (parser, CPP_EQ, RT_EQ);
34389 rhs = cp_parser_assignment_expression (parser);
34390 orig_init = rhs;
34391 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34392 decl, NOP_EXPR,
34393 rhs,
34394 tf_warning_or_error));
34395 if (!add_private_clause)
34396 add_private_clause = decl;
34398 else
34400 decl = NULL;
34401 cp_parser_abort_tentative_parse (parser);
34402 init = cp_parser_expression (parser);
34403 if (init)
34405 if (TREE_CODE (init) == MODIFY_EXPR
34406 || TREE_CODE (init) == MODOP_EXPR)
34407 real_decl = TREE_OPERAND (init, 0);
34411 return add_private_clause;
34414 /* Parse the restricted form of the for statement allowed by OpenMP. */
34416 static tree
34417 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34418 tree *cclauses, bool *if_p)
34420 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34421 tree real_decl, initv, condv, incrv, declv;
34422 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34423 location_t loc_first;
34424 bool collapse_err = false;
34425 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34426 vec<tree, va_gc> *for_block = make_tree_vector ();
34427 auto_vec<tree, 4> orig_inits;
34428 bool tiling = false;
34430 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34431 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34432 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34433 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34435 tiling = true;
34436 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34438 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34439 && OMP_CLAUSE_ORDERED_EXPR (cl))
34441 ordered_cl = cl;
34442 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34445 if (ordered && ordered < collapse)
34447 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34448 "%<ordered%> clause parameter is less than %<collapse%>");
34449 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34450 = build_int_cst (NULL_TREE, collapse);
34451 ordered = collapse;
34453 if (ordered)
34455 for (tree *pc = &clauses; *pc; )
34456 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34458 error_at (OMP_CLAUSE_LOCATION (*pc),
34459 "%<linear%> clause may not be specified together "
34460 "with %<ordered%> clause with a parameter");
34461 *pc = OMP_CLAUSE_CHAIN (*pc);
34463 else
34464 pc = &OMP_CLAUSE_CHAIN (*pc);
34467 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34468 count = ordered ? ordered : collapse;
34470 declv = make_tree_vec (count);
34471 initv = make_tree_vec (count);
34472 condv = make_tree_vec (count);
34473 incrv = make_tree_vec (count);
34475 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34477 for (i = 0; i < count; i++)
34479 int bracecount = 0;
34480 tree add_private_clause = NULL_TREE;
34481 location_t loc;
34483 if (code != CILK_FOR
34484 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34486 if (!collapse_err)
34487 cp_parser_error (parser, "for statement expected");
34488 return NULL;
34490 if (code == CILK_FOR
34491 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34493 if (!collapse_err)
34494 cp_parser_error (parser, "_Cilk_for statement expected");
34495 return NULL;
34497 loc = cp_lexer_consume_token (parser->lexer)->location;
34499 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34500 return NULL;
34502 init = orig_init = decl = real_decl = NULL;
34503 this_pre_body = push_stmt_list ();
34505 add_private_clause
34506 = cp_parser_omp_for_loop_init (parser, code,
34507 this_pre_body, for_block,
34508 init, orig_init, decl, real_decl);
34510 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34511 if (this_pre_body)
34513 this_pre_body = pop_stmt_list (this_pre_body);
34514 if (pre_body)
34516 tree t = pre_body;
34517 pre_body = push_stmt_list ();
34518 add_stmt (t);
34519 add_stmt (this_pre_body);
34520 pre_body = pop_stmt_list (pre_body);
34522 else
34523 pre_body = this_pre_body;
34526 if (decl)
34527 real_decl = decl;
34528 if (cclauses != NULL
34529 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34530 && real_decl != NULL_TREE)
34532 tree *c;
34533 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34534 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34535 && OMP_CLAUSE_DECL (*c) == real_decl)
34537 error_at (loc, "iteration variable %qD"
34538 " should not be firstprivate", real_decl);
34539 *c = OMP_CLAUSE_CHAIN (*c);
34541 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34542 && OMP_CLAUSE_DECL (*c) == real_decl)
34544 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34545 tree l = *c;
34546 *c = OMP_CLAUSE_CHAIN (*c);
34547 if (code == OMP_SIMD)
34549 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34550 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34552 else
34554 OMP_CLAUSE_CHAIN (l) = clauses;
34555 clauses = l;
34557 add_private_clause = NULL_TREE;
34559 else
34561 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34562 && OMP_CLAUSE_DECL (*c) == real_decl)
34563 add_private_clause = NULL_TREE;
34564 c = &OMP_CLAUSE_CHAIN (*c);
34568 if (add_private_clause)
34570 tree c;
34571 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34573 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34574 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34575 && OMP_CLAUSE_DECL (c) == decl)
34576 break;
34577 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34578 && OMP_CLAUSE_DECL (c) == decl)
34579 error_at (loc, "iteration variable %qD "
34580 "should not be firstprivate",
34581 decl);
34582 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34583 && OMP_CLAUSE_DECL (c) == decl)
34584 error_at (loc, "iteration variable %qD should not be reduction",
34585 decl);
34587 if (c == NULL)
34589 if (code != OMP_SIMD)
34590 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34591 else if (collapse == 1)
34592 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34593 else
34594 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34595 OMP_CLAUSE_DECL (c) = add_private_clause;
34596 c = finish_omp_clauses (c, C_ORT_OMP);
34597 if (c)
34599 OMP_CLAUSE_CHAIN (c) = clauses;
34600 clauses = c;
34601 /* For linear, signal that we need to fill up
34602 the so far unknown linear step. */
34603 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34604 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34609 cond = NULL;
34610 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34611 cond = cp_parser_omp_for_cond (parser, decl, code);
34612 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34614 incr = NULL;
34615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34617 /* If decl is an iterator, preserve the operator on decl
34618 until finish_omp_for. */
34619 if (real_decl
34620 && ((processing_template_decl
34621 && (TREE_TYPE (real_decl) == NULL_TREE
34622 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34623 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34624 incr = cp_parser_omp_for_incr (parser, real_decl);
34625 else
34626 incr = cp_parser_expression (parser);
34627 if (!EXPR_HAS_LOCATION (incr))
34628 protected_set_expr_location (incr, input_location);
34631 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34632 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34633 /*or_comma=*/false,
34634 /*consume_paren=*/true);
34636 TREE_VEC_ELT (declv, i) = decl;
34637 TREE_VEC_ELT (initv, i) = init;
34638 TREE_VEC_ELT (condv, i) = cond;
34639 TREE_VEC_ELT (incrv, i) = incr;
34640 if (orig_init)
34642 orig_inits.safe_grow_cleared (i + 1);
34643 orig_inits[i] = orig_init;
34646 if (i == count - 1)
34647 break;
34649 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34650 in between the collapsed for loops to be still considered perfectly
34651 nested. Hopefully the final version clarifies this.
34652 For now handle (multiple) {'s and empty statements. */
34653 cp_parser_parse_tentatively (parser);
34654 for (;;)
34656 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34657 break;
34658 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34660 cp_lexer_consume_token (parser->lexer);
34661 bracecount++;
34663 else if (bracecount
34664 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34665 cp_lexer_consume_token (parser->lexer);
34666 else
34668 loc = cp_lexer_peek_token (parser->lexer)->location;
34669 error_at (loc, "not enough for loops to collapse");
34670 collapse_err = true;
34671 cp_parser_abort_tentative_parse (parser);
34672 declv = NULL_TREE;
34673 break;
34677 if (declv)
34679 cp_parser_parse_definitely (parser);
34680 nbraces += bracecount;
34684 if (nbraces)
34685 if_p = NULL;
34687 /* Note that we saved the original contents of this flag when we entered
34688 the structured block, and so we don't need to re-save it here. */
34689 if (code == CILK_SIMD || code == CILK_FOR)
34690 parser->in_statement = IN_CILK_SIMD_FOR;
34691 else
34692 parser->in_statement = IN_OMP_FOR;
34694 /* Note that the grammar doesn't call for a structured block here,
34695 though the loop as a whole is a structured block. */
34696 body = push_stmt_list ();
34697 cp_parser_statement (parser, NULL_TREE, false, if_p);
34698 body = pop_stmt_list (body);
34700 if (declv == NULL_TREE)
34701 ret = NULL_TREE;
34702 else
34703 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34704 body, pre_body, &orig_inits, clauses);
34706 while (nbraces)
34708 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34710 cp_lexer_consume_token (parser->lexer);
34711 nbraces--;
34713 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34714 cp_lexer_consume_token (parser->lexer);
34715 else
34717 if (!collapse_err)
34719 error_at (cp_lexer_peek_token (parser->lexer)->location,
34720 "collapsed loops not perfectly nested");
34722 collapse_err = true;
34723 cp_parser_statement_seq_opt (parser, NULL);
34724 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34725 break;
34729 while (!for_block->is_empty ())
34731 tree t = for_block->pop ();
34732 if (TREE_CODE (t) == STATEMENT_LIST)
34733 add_stmt (pop_stmt_list (t));
34734 else
34735 add_stmt (t);
34737 release_tree_vector (for_block);
34739 return ret;
34742 /* Helper function for OpenMP parsing, split clauses and call
34743 finish_omp_clauses on each of the set of clauses afterwards. */
34745 static void
34746 cp_omp_split_clauses (location_t loc, enum tree_code code,
34747 omp_clause_mask mask, tree clauses, tree *cclauses)
34749 int i;
34750 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34751 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34752 if (cclauses[i])
34753 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34756 /* OpenMP 4.0:
34757 #pragma omp simd simd-clause[optseq] new-line
34758 for-loop */
34760 #define OMP_SIMD_CLAUSE_MASK \
34761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34768 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34770 static tree
34771 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34772 char *p_name, omp_clause_mask mask, tree *cclauses,
34773 bool *if_p)
34775 tree clauses, sb, ret;
34776 unsigned int save;
34777 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34779 strcat (p_name, " simd");
34780 mask |= OMP_SIMD_CLAUSE_MASK;
34782 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34783 cclauses == NULL);
34784 if (cclauses)
34786 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34787 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34788 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34789 OMP_CLAUSE_ORDERED);
34790 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34792 error_at (OMP_CLAUSE_LOCATION (c),
34793 "%<ordered%> clause with parameter may not be specified "
34794 "on %qs construct", p_name);
34795 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34799 sb = begin_omp_structured_block ();
34800 save = cp_parser_begin_omp_structured_block (parser);
34802 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34804 cp_parser_end_omp_structured_block (parser, save);
34805 add_stmt (finish_omp_structured_block (sb));
34807 return ret;
34810 /* OpenMP 2.5:
34811 #pragma omp for for-clause[optseq] new-line
34812 for-loop
34814 OpenMP 4.0:
34815 #pragma omp for simd for-simd-clause[optseq] new-line
34816 for-loop */
34818 #define OMP_FOR_CLAUSE_MASK \
34819 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34820 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34821 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34822 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34823 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34824 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34825 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34826 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34827 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34829 static tree
34830 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
34831 char *p_name, omp_clause_mask mask, tree *cclauses,
34832 bool *if_p)
34834 tree clauses, sb, ret;
34835 unsigned int save;
34836 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34838 strcat (p_name, " for");
34839 mask |= OMP_FOR_CLAUSE_MASK;
34840 /* parallel for{, simd} disallows nowait clause, but for
34841 target {teams distribute ,}parallel for{, simd} it should be accepted. */
34842 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
34843 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
34844 /* Composite distribute parallel for{, simd} disallows ordered clause. */
34845 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34846 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
34848 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34850 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34851 const char *p = IDENTIFIER_POINTER (id);
34853 if (strcmp (p, "simd") == 0)
34855 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
34856 if (cclauses == NULL)
34857 cclauses = cclauses_buf;
34859 cp_lexer_consume_token (parser->lexer);
34860 if (!flag_openmp) /* flag_openmp_simd */
34861 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34862 cclauses, if_p);
34863 sb = begin_omp_structured_block ();
34864 save = cp_parser_begin_omp_structured_block (parser);
34865 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
34866 cclauses, if_p);
34867 cp_parser_end_omp_structured_block (parser, save);
34868 tree body = finish_omp_structured_block (sb);
34869 if (ret == NULL)
34870 return ret;
34871 ret = make_node (OMP_FOR);
34872 TREE_TYPE (ret) = void_type_node;
34873 OMP_FOR_BODY (ret) = body;
34874 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34875 SET_EXPR_LOCATION (ret, loc);
34876 add_stmt (ret);
34877 return ret;
34880 if (!flag_openmp) /* flag_openmp_simd */
34882 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34883 return NULL_TREE;
34886 /* Composite distribute parallel for disallows linear clause. */
34887 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
34888 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
34890 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34891 cclauses == NULL);
34892 if (cclauses)
34894 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
34895 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34898 sb = begin_omp_structured_block ();
34899 save = cp_parser_begin_omp_structured_block (parser);
34901 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
34903 cp_parser_end_omp_structured_block (parser, save);
34904 add_stmt (finish_omp_structured_block (sb));
34906 return ret;
34909 /* OpenMP 2.5:
34910 # pragma omp master new-line
34911 structured-block */
34913 static tree
34914 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34916 cp_parser_require_pragma_eol (parser, pragma_tok);
34917 return c_finish_omp_master (input_location,
34918 cp_parser_omp_structured_block (parser, if_p));
34921 /* OpenMP 2.5:
34922 # pragma omp ordered new-line
34923 structured-block
34925 OpenMP 4.5:
34926 # pragma omp ordered ordered-clauses new-line
34927 structured-block */
34929 #define OMP_ORDERED_CLAUSE_MASK \
34930 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
34931 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
34933 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
34934 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
34936 static bool
34937 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
34938 enum pragma_context context, bool *if_p)
34940 location_t loc = pragma_tok->location;
34942 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
34944 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
34945 const char *p = IDENTIFIER_POINTER (id);
34947 if (strcmp (p, "depend") == 0)
34949 if (context == pragma_stmt)
34951 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
34952 "%<depend%> clause may only be used in compound "
34953 "statements");
34954 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
34955 return false;
34957 tree clauses
34958 = cp_parser_omp_all_clauses (parser,
34959 OMP_ORDERED_DEPEND_CLAUSE_MASK,
34960 "#pragma omp ordered", pragma_tok);
34961 c_finish_omp_ordered (loc, clauses, NULL_TREE);
34962 return false;
34966 tree clauses
34967 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
34968 "#pragma omp ordered", pragma_tok);
34969 c_finish_omp_ordered (loc, clauses,
34970 cp_parser_omp_structured_block (parser, if_p));
34971 return true;
34974 /* OpenMP 2.5:
34976 section-scope:
34977 { section-sequence }
34979 section-sequence:
34980 section-directive[opt] structured-block
34981 section-sequence section-directive structured-block */
34983 static tree
34984 cp_parser_omp_sections_scope (cp_parser *parser)
34986 tree stmt, substmt;
34987 bool error_suppress = false;
34988 cp_token *tok;
34990 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
34991 return NULL_TREE;
34993 stmt = push_stmt_list ();
34995 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
34996 != PRAGMA_OMP_SECTION)
34998 substmt = cp_parser_omp_structured_block (parser, NULL);
34999 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35000 add_stmt (substmt);
35003 while (1)
35005 tok = cp_lexer_peek_token (parser->lexer);
35006 if (tok->type == CPP_CLOSE_BRACE)
35007 break;
35008 if (tok->type == CPP_EOF)
35009 break;
35011 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35013 cp_lexer_consume_token (parser->lexer);
35014 cp_parser_require_pragma_eol (parser, tok);
35015 error_suppress = false;
35017 else if (!error_suppress)
35019 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35020 error_suppress = true;
35023 substmt = cp_parser_omp_structured_block (parser, NULL);
35024 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35025 add_stmt (substmt);
35027 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35029 substmt = pop_stmt_list (stmt);
35031 stmt = make_node (OMP_SECTIONS);
35032 TREE_TYPE (stmt) = void_type_node;
35033 OMP_SECTIONS_BODY (stmt) = substmt;
35035 add_stmt (stmt);
35036 return stmt;
35039 /* OpenMP 2.5:
35040 # pragma omp sections sections-clause[optseq] newline
35041 sections-scope */
35043 #define OMP_SECTIONS_CLAUSE_MASK \
35044 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35048 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35050 static tree
35051 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35052 char *p_name, omp_clause_mask mask, tree *cclauses)
35054 tree clauses, ret;
35055 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35057 strcat (p_name, " sections");
35058 mask |= OMP_SECTIONS_CLAUSE_MASK;
35059 if (cclauses)
35060 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35062 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35063 cclauses == NULL);
35064 if (cclauses)
35066 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35067 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35070 ret = cp_parser_omp_sections_scope (parser);
35071 if (ret)
35072 OMP_SECTIONS_CLAUSES (ret) = clauses;
35074 return ret;
35077 /* OpenMP 2.5:
35078 # pragma omp parallel parallel-clause[optseq] new-line
35079 structured-block
35080 # pragma omp parallel for parallel-for-clause[optseq] new-line
35081 structured-block
35082 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35083 structured-block
35085 OpenMP 4.0:
35086 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35087 structured-block */
35089 #define OMP_PARALLEL_CLAUSE_MASK \
35090 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35091 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35092 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35093 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35094 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35095 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35096 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35097 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35098 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35100 static tree
35101 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35102 char *p_name, omp_clause_mask mask, tree *cclauses,
35103 bool *if_p)
35105 tree stmt, clauses, block;
35106 unsigned int save;
35107 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35109 strcat (p_name, " parallel");
35110 mask |= OMP_PARALLEL_CLAUSE_MASK;
35111 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35112 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35113 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35114 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35118 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35119 if (cclauses == NULL)
35120 cclauses = cclauses_buf;
35122 cp_lexer_consume_token (parser->lexer);
35123 if (!flag_openmp) /* flag_openmp_simd */
35124 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35125 if_p);
35126 block = begin_omp_parallel ();
35127 save = cp_parser_begin_omp_structured_block (parser);
35128 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35129 if_p);
35130 cp_parser_end_omp_structured_block (parser, save);
35131 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35132 block);
35133 if (ret == NULL_TREE)
35134 return ret;
35135 OMP_PARALLEL_COMBINED (stmt) = 1;
35136 return stmt;
35138 /* When combined with distribute, parallel has to be followed by for.
35139 #pragma omp target parallel is allowed though. */
35140 else if (cclauses
35141 && (mask & (OMP_CLAUSE_MASK_1
35142 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35144 error_at (loc, "expected %<for%> after %qs", p_name);
35145 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35146 return NULL_TREE;
35148 else if (!flag_openmp) /* flag_openmp_simd */
35150 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35151 return NULL_TREE;
35153 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35155 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35156 const char *p = IDENTIFIER_POINTER (id);
35157 if (strcmp (p, "sections") == 0)
35159 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35160 cclauses = cclauses_buf;
35162 cp_lexer_consume_token (parser->lexer);
35163 block = begin_omp_parallel ();
35164 save = cp_parser_begin_omp_structured_block (parser);
35165 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35166 cp_parser_end_omp_structured_block (parser, save);
35167 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35168 block);
35169 OMP_PARALLEL_COMBINED (stmt) = 1;
35170 return stmt;
35174 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35175 cclauses == NULL);
35176 if (cclauses)
35178 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35179 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35182 block = begin_omp_parallel ();
35183 save = cp_parser_begin_omp_structured_block (parser);
35184 cp_parser_statement (parser, NULL_TREE, false, if_p);
35185 cp_parser_end_omp_structured_block (parser, save);
35186 stmt = finish_omp_parallel (clauses, block);
35187 return stmt;
35190 /* OpenMP 2.5:
35191 # pragma omp single single-clause[optseq] new-line
35192 structured-block */
35194 #define OMP_SINGLE_CLAUSE_MASK \
35195 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35196 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35197 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35198 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35200 static tree
35201 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35203 tree stmt = make_node (OMP_SINGLE);
35204 TREE_TYPE (stmt) = void_type_node;
35206 OMP_SINGLE_CLAUSES (stmt)
35207 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35208 "#pragma omp single", pragma_tok);
35209 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35211 return add_stmt (stmt);
35214 /* OpenMP 3.0:
35215 # pragma omp task task-clause[optseq] new-line
35216 structured-block */
35218 #define OMP_TASK_CLAUSE_MASK \
35219 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35220 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35221 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35222 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35223 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35224 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35225 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35226 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35227 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35228 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35230 static tree
35231 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35233 tree clauses, block;
35234 unsigned int save;
35236 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35237 "#pragma omp task", pragma_tok);
35238 block = begin_omp_task ();
35239 save = cp_parser_begin_omp_structured_block (parser);
35240 cp_parser_statement (parser, NULL_TREE, false, if_p);
35241 cp_parser_end_omp_structured_block (parser, save);
35242 return finish_omp_task (clauses, block);
35245 /* OpenMP 3.0:
35246 # pragma omp taskwait new-line */
35248 static void
35249 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35251 cp_parser_require_pragma_eol (parser, pragma_tok);
35252 finish_omp_taskwait ();
35255 /* OpenMP 3.1:
35256 # pragma omp taskyield new-line */
35258 static void
35259 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35261 cp_parser_require_pragma_eol (parser, pragma_tok);
35262 finish_omp_taskyield ();
35265 /* OpenMP 4.0:
35266 # pragma omp taskgroup new-line
35267 structured-block */
35269 static tree
35270 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35272 cp_parser_require_pragma_eol (parser, pragma_tok);
35273 return c_finish_omp_taskgroup (input_location,
35274 cp_parser_omp_structured_block (parser,
35275 if_p));
35279 /* OpenMP 2.5:
35280 # pragma omp threadprivate (variable-list) */
35282 static void
35283 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35285 tree vars;
35287 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35288 cp_parser_require_pragma_eol (parser, pragma_tok);
35290 finish_omp_threadprivate (vars);
35293 /* OpenMP 4.0:
35294 # pragma omp cancel cancel-clause[optseq] new-line */
35296 #define OMP_CANCEL_CLAUSE_MASK \
35297 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35303 static void
35304 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35306 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35307 "#pragma omp cancel", pragma_tok);
35308 finish_omp_cancel (clauses);
35311 /* OpenMP 4.0:
35312 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35314 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35315 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35320 static void
35321 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35322 enum pragma_context context)
35324 tree clauses;
35325 bool point_seen = false;
35327 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35329 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35330 const char *p = IDENTIFIER_POINTER (id);
35332 if (strcmp (p, "point") == 0)
35334 cp_lexer_consume_token (parser->lexer);
35335 point_seen = true;
35338 if (!point_seen)
35340 cp_parser_error (parser, "expected %<point%>");
35341 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35342 return;
35345 if (context != pragma_compound)
35347 if (context == pragma_stmt)
35348 error_at (pragma_tok->location,
35349 "%<#pragma omp cancellation point%> may only be used in"
35350 " compound statements");
35351 else
35352 cp_parser_error (parser, "expected declaration specifiers");
35353 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35354 return;
35357 clauses = cp_parser_omp_all_clauses (parser,
35358 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35359 "#pragma omp cancellation point",
35360 pragma_tok);
35361 finish_omp_cancellation_point (clauses);
35364 /* OpenMP 4.0:
35365 #pragma omp distribute distribute-clause[optseq] new-line
35366 for-loop */
35368 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35369 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35370 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35371 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35372 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35373 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35375 static tree
35376 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35377 char *p_name, omp_clause_mask mask, tree *cclauses,
35378 bool *if_p)
35380 tree clauses, sb, ret;
35381 unsigned int save;
35382 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35384 strcat (p_name, " distribute");
35385 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35389 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35390 const char *p = IDENTIFIER_POINTER (id);
35391 bool simd = false;
35392 bool parallel = false;
35394 if (strcmp (p, "simd") == 0)
35395 simd = true;
35396 else
35397 parallel = strcmp (p, "parallel") == 0;
35398 if (parallel || simd)
35400 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35401 if (cclauses == NULL)
35402 cclauses = cclauses_buf;
35403 cp_lexer_consume_token (parser->lexer);
35404 if (!flag_openmp) /* flag_openmp_simd */
35406 if (simd)
35407 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35408 cclauses, if_p);
35409 else
35410 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35411 cclauses, if_p);
35413 sb = begin_omp_structured_block ();
35414 save = cp_parser_begin_omp_structured_block (parser);
35415 if (simd)
35416 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35417 cclauses, if_p);
35418 else
35419 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35420 cclauses, if_p);
35421 cp_parser_end_omp_structured_block (parser, save);
35422 tree body = finish_omp_structured_block (sb);
35423 if (ret == NULL)
35424 return ret;
35425 ret = make_node (OMP_DISTRIBUTE);
35426 TREE_TYPE (ret) = void_type_node;
35427 OMP_FOR_BODY (ret) = body;
35428 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35429 SET_EXPR_LOCATION (ret, loc);
35430 add_stmt (ret);
35431 return ret;
35434 if (!flag_openmp) /* flag_openmp_simd */
35436 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35437 return NULL_TREE;
35440 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35441 cclauses == NULL);
35442 if (cclauses)
35444 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35445 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35448 sb = begin_omp_structured_block ();
35449 save = cp_parser_begin_omp_structured_block (parser);
35451 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35453 cp_parser_end_omp_structured_block (parser, save);
35454 add_stmt (finish_omp_structured_block (sb));
35456 return ret;
35459 /* OpenMP 4.0:
35460 # pragma omp teams teams-clause[optseq] new-line
35461 structured-block */
35463 #define OMP_TEAMS_CLAUSE_MASK \
35464 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35472 static tree
35473 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35474 char *p_name, omp_clause_mask mask, tree *cclauses,
35475 bool *if_p)
35477 tree clauses, sb, ret;
35478 unsigned int save;
35479 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35481 strcat (p_name, " teams");
35482 mask |= OMP_TEAMS_CLAUSE_MASK;
35484 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35486 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35487 const char *p = IDENTIFIER_POINTER (id);
35488 if (strcmp (p, "distribute") == 0)
35490 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35491 if (cclauses == NULL)
35492 cclauses = cclauses_buf;
35494 cp_lexer_consume_token (parser->lexer);
35495 if (!flag_openmp) /* flag_openmp_simd */
35496 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35497 cclauses, if_p);
35498 sb = begin_omp_structured_block ();
35499 save = cp_parser_begin_omp_structured_block (parser);
35500 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35501 cclauses, if_p);
35502 cp_parser_end_omp_structured_block (parser, save);
35503 tree body = finish_omp_structured_block (sb);
35504 if (ret == NULL)
35505 return ret;
35506 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35507 ret = make_node (OMP_TEAMS);
35508 TREE_TYPE (ret) = void_type_node;
35509 OMP_TEAMS_CLAUSES (ret) = clauses;
35510 OMP_TEAMS_BODY (ret) = body;
35511 OMP_TEAMS_COMBINED (ret) = 1;
35512 return add_stmt (ret);
35515 if (!flag_openmp) /* flag_openmp_simd */
35517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35518 return NULL_TREE;
35521 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35522 cclauses == NULL);
35523 if (cclauses)
35525 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35526 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35529 tree stmt = make_node (OMP_TEAMS);
35530 TREE_TYPE (stmt) = void_type_node;
35531 OMP_TEAMS_CLAUSES (stmt) = clauses;
35532 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35534 return add_stmt (stmt);
35537 /* OpenMP 4.0:
35538 # pragma omp target data target-data-clause[optseq] new-line
35539 structured-block */
35541 #define OMP_TARGET_DATA_CLAUSE_MASK \
35542 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35545 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35547 static tree
35548 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35550 tree clauses
35551 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35552 "#pragma omp target data", pragma_tok);
35553 int map_seen = 0;
35554 for (tree *pc = &clauses; *pc;)
35556 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35557 switch (OMP_CLAUSE_MAP_KIND (*pc))
35559 case GOMP_MAP_TO:
35560 case GOMP_MAP_ALWAYS_TO:
35561 case GOMP_MAP_FROM:
35562 case GOMP_MAP_ALWAYS_FROM:
35563 case GOMP_MAP_TOFROM:
35564 case GOMP_MAP_ALWAYS_TOFROM:
35565 case GOMP_MAP_ALLOC:
35566 map_seen = 3;
35567 break;
35568 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35569 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35570 case GOMP_MAP_ALWAYS_POINTER:
35571 break;
35572 default:
35573 map_seen |= 1;
35574 error_at (OMP_CLAUSE_LOCATION (*pc),
35575 "%<#pragma omp target data%> with map-type other "
35576 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35577 "on %<map%> clause");
35578 *pc = OMP_CLAUSE_CHAIN (*pc);
35579 continue;
35581 pc = &OMP_CLAUSE_CHAIN (*pc);
35584 if (map_seen != 3)
35586 if (map_seen == 0)
35587 error_at (pragma_tok->location,
35588 "%<#pragma omp target data%> must contain at least "
35589 "one %<map%> clause");
35590 return NULL_TREE;
35593 tree stmt = make_node (OMP_TARGET_DATA);
35594 TREE_TYPE (stmt) = void_type_node;
35595 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35597 keep_next_level (true);
35598 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35600 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35601 return add_stmt (stmt);
35604 /* OpenMP 4.5:
35605 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35606 structured-block */
35608 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35609 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35615 static tree
35616 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35617 enum pragma_context context)
35619 bool data_seen = false;
35620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35622 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35623 const char *p = IDENTIFIER_POINTER (id);
35625 if (strcmp (p, "data") == 0)
35627 cp_lexer_consume_token (parser->lexer);
35628 data_seen = true;
35631 if (!data_seen)
35633 cp_parser_error (parser, "expected %<data%>");
35634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35635 return NULL_TREE;
35638 if (context == pragma_stmt)
35640 error_at (pragma_tok->location,
35641 "%<#pragma omp target enter data%> may only be "
35642 "used in compound statements");
35643 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35644 return NULL_TREE;
35647 tree clauses
35648 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35649 "#pragma omp target enter data", pragma_tok);
35650 int map_seen = 0;
35651 for (tree *pc = &clauses; *pc;)
35653 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35654 switch (OMP_CLAUSE_MAP_KIND (*pc))
35656 case GOMP_MAP_TO:
35657 case GOMP_MAP_ALWAYS_TO:
35658 case GOMP_MAP_ALLOC:
35659 map_seen = 3;
35660 break;
35661 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35662 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35663 case GOMP_MAP_ALWAYS_POINTER:
35664 break;
35665 default:
35666 map_seen |= 1;
35667 error_at (OMP_CLAUSE_LOCATION (*pc),
35668 "%<#pragma omp target enter data%> with map-type other "
35669 "than %<to%> or %<alloc%> on %<map%> clause");
35670 *pc = OMP_CLAUSE_CHAIN (*pc);
35671 continue;
35673 pc = &OMP_CLAUSE_CHAIN (*pc);
35676 if (map_seen != 3)
35678 if (map_seen == 0)
35679 error_at (pragma_tok->location,
35680 "%<#pragma omp target enter data%> must contain at least "
35681 "one %<map%> clause");
35682 return NULL_TREE;
35685 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35686 TREE_TYPE (stmt) = void_type_node;
35687 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35688 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35689 return add_stmt (stmt);
35692 /* OpenMP 4.5:
35693 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35694 structured-block */
35696 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35697 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35698 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35699 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35703 static tree
35704 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35705 enum pragma_context context)
35707 bool data_seen = false;
35708 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35710 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35711 const char *p = IDENTIFIER_POINTER (id);
35713 if (strcmp (p, "data") == 0)
35715 cp_lexer_consume_token (parser->lexer);
35716 data_seen = true;
35719 if (!data_seen)
35721 cp_parser_error (parser, "expected %<data%>");
35722 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35723 return NULL_TREE;
35726 if (context == pragma_stmt)
35728 error_at (pragma_tok->location,
35729 "%<#pragma omp target exit data%> may only be "
35730 "used in compound statements");
35731 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35732 return NULL_TREE;
35735 tree clauses
35736 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35737 "#pragma omp target exit data", pragma_tok);
35738 int map_seen = 0;
35739 for (tree *pc = &clauses; *pc;)
35741 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35742 switch (OMP_CLAUSE_MAP_KIND (*pc))
35744 case GOMP_MAP_FROM:
35745 case GOMP_MAP_ALWAYS_FROM:
35746 case GOMP_MAP_RELEASE:
35747 case GOMP_MAP_DELETE:
35748 map_seen = 3;
35749 break;
35750 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35751 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35752 case GOMP_MAP_ALWAYS_POINTER:
35753 break;
35754 default:
35755 map_seen |= 1;
35756 error_at (OMP_CLAUSE_LOCATION (*pc),
35757 "%<#pragma omp target exit data%> with map-type other "
35758 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35759 " clause");
35760 *pc = OMP_CLAUSE_CHAIN (*pc);
35761 continue;
35763 pc = &OMP_CLAUSE_CHAIN (*pc);
35766 if (map_seen != 3)
35768 if (map_seen == 0)
35769 error_at (pragma_tok->location,
35770 "%<#pragma omp target exit data%> must contain at least "
35771 "one %<map%> clause");
35772 return NULL_TREE;
35775 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35776 TREE_TYPE (stmt) = void_type_node;
35777 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35778 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35779 return add_stmt (stmt);
35782 /* OpenMP 4.0:
35783 # pragma omp target update target-update-clause[optseq] new-line */
35785 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35786 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35787 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35788 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35789 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35790 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35791 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35793 static bool
35794 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35795 enum pragma_context context)
35797 if (context == pragma_stmt)
35799 error_at (pragma_tok->location,
35800 "%<#pragma omp target update%> may only be "
35801 "used in compound statements");
35802 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35803 return false;
35806 tree clauses
35807 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35808 "#pragma omp target update", pragma_tok);
35809 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35810 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35812 error_at (pragma_tok->location,
35813 "%<#pragma omp target update%> must contain at least one "
35814 "%<from%> or %<to%> clauses");
35815 return false;
35818 tree stmt = make_node (OMP_TARGET_UPDATE);
35819 TREE_TYPE (stmt) = void_type_node;
35820 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35821 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35822 add_stmt (stmt);
35823 return false;
35826 /* OpenMP 4.0:
35827 # pragma omp target target-clause[optseq] new-line
35828 structured-block */
35830 #define OMP_TARGET_CLAUSE_MASK \
35831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35836 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35837 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35838 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
35839 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
35841 static bool
35842 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
35843 enum pragma_context context, bool *if_p)
35845 tree *pc = NULL, stmt;
35847 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35849 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35850 const char *p = IDENTIFIER_POINTER (id);
35851 enum tree_code ccode = ERROR_MARK;
35853 if (strcmp (p, "teams") == 0)
35854 ccode = OMP_TEAMS;
35855 else if (strcmp (p, "parallel") == 0)
35856 ccode = OMP_PARALLEL;
35857 else if (strcmp (p, "simd") == 0)
35858 ccode = OMP_SIMD;
35859 if (ccode != ERROR_MARK)
35861 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
35862 char p_name[sizeof ("#pragma omp target teams distribute "
35863 "parallel for simd")];
35865 cp_lexer_consume_token (parser->lexer);
35866 strcpy (p_name, "#pragma omp target");
35867 if (!flag_openmp) /* flag_openmp_simd */
35869 tree stmt;
35870 switch (ccode)
35872 case OMP_TEAMS:
35873 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
35874 OMP_TARGET_CLAUSE_MASK,
35875 cclauses, if_p);
35876 break;
35877 case OMP_PARALLEL:
35878 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35879 OMP_TARGET_CLAUSE_MASK,
35880 cclauses, if_p);
35881 break;
35882 case OMP_SIMD:
35883 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
35884 OMP_TARGET_CLAUSE_MASK,
35885 cclauses, if_p);
35886 break;
35887 default:
35888 gcc_unreachable ();
35890 return stmt != NULL_TREE;
35892 keep_next_level (true);
35893 tree sb = begin_omp_structured_block (), ret;
35894 unsigned save = cp_parser_begin_omp_structured_block (parser);
35895 switch (ccode)
35897 case OMP_TEAMS:
35898 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
35899 OMP_TARGET_CLAUSE_MASK, cclauses,
35900 if_p);
35901 break;
35902 case OMP_PARALLEL:
35903 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
35904 OMP_TARGET_CLAUSE_MASK, cclauses,
35905 if_p);
35906 break;
35907 case OMP_SIMD:
35908 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
35909 OMP_TARGET_CLAUSE_MASK, cclauses,
35910 if_p);
35911 break;
35912 default:
35913 gcc_unreachable ();
35915 cp_parser_end_omp_structured_block (parser, save);
35916 tree body = finish_omp_structured_block (sb);
35917 if (ret == NULL_TREE)
35918 return false;
35919 if (ccode == OMP_TEAMS && !processing_template_decl)
35921 /* For combined target teams, ensure the num_teams and
35922 thread_limit clause expressions are evaluated on the host,
35923 before entering the target construct. */
35924 tree c;
35925 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35926 c; c = OMP_CLAUSE_CHAIN (c))
35927 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
35928 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
35929 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
35931 tree expr = OMP_CLAUSE_OPERAND (c, 0);
35932 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
35933 if (expr == error_mark_node)
35934 continue;
35935 tree tmp = TARGET_EXPR_SLOT (expr);
35936 add_stmt (expr);
35937 OMP_CLAUSE_OPERAND (c, 0) = expr;
35938 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
35939 OMP_CLAUSE_FIRSTPRIVATE);
35940 OMP_CLAUSE_DECL (tc) = tmp;
35941 OMP_CLAUSE_CHAIN (tc)
35942 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35943 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
35946 tree stmt = make_node (OMP_TARGET);
35947 TREE_TYPE (stmt) = void_type_node;
35948 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
35949 OMP_TARGET_BODY (stmt) = body;
35950 OMP_TARGET_COMBINED (stmt) = 1;
35951 add_stmt (stmt);
35952 pc = &OMP_TARGET_CLAUSES (stmt);
35953 goto check_clauses;
35955 else if (!flag_openmp) /* flag_openmp_simd */
35957 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35958 return false;
35960 else if (strcmp (p, "data") == 0)
35962 cp_lexer_consume_token (parser->lexer);
35963 cp_parser_omp_target_data (parser, pragma_tok, if_p);
35964 return true;
35966 else if (strcmp (p, "enter") == 0)
35968 cp_lexer_consume_token (parser->lexer);
35969 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
35970 return false;
35972 else if (strcmp (p, "exit") == 0)
35974 cp_lexer_consume_token (parser->lexer);
35975 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
35976 return false;
35978 else if (strcmp (p, "update") == 0)
35980 cp_lexer_consume_token (parser->lexer);
35981 return cp_parser_omp_target_update (parser, pragma_tok, context);
35984 if (!flag_openmp) /* flag_openmp_simd */
35986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35987 return false;
35990 stmt = make_node (OMP_TARGET);
35991 TREE_TYPE (stmt) = void_type_node;
35993 OMP_TARGET_CLAUSES (stmt)
35994 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
35995 "#pragma omp target", pragma_tok);
35996 pc = &OMP_TARGET_CLAUSES (stmt);
35997 keep_next_level (true);
35998 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36000 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36001 add_stmt (stmt);
36003 check_clauses:
36004 while (*pc)
36006 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36007 switch (OMP_CLAUSE_MAP_KIND (*pc))
36009 case GOMP_MAP_TO:
36010 case GOMP_MAP_ALWAYS_TO:
36011 case GOMP_MAP_FROM:
36012 case GOMP_MAP_ALWAYS_FROM:
36013 case GOMP_MAP_TOFROM:
36014 case GOMP_MAP_ALWAYS_TOFROM:
36015 case GOMP_MAP_ALLOC:
36016 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36017 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36018 case GOMP_MAP_ALWAYS_POINTER:
36019 break;
36020 default:
36021 error_at (OMP_CLAUSE_LOCATION (*pc),
36022 "%<#pragma omp target%> with map-type other "
36023 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36024 "on %<map%> clause");
36025 *pc = OMP_CLAUSE_CHAIN (*pc);
36026 continue;
36028 pc = &OMP_CLAUSE_CHAIN (*pc);
36030 return true;
36033 /* OpenACC 2.0:
36034 # pragma acc cache (variable-list) new-line
36037 static tree
36038 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36040 tree stmt, clauses;
36042 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36043 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36045 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36047 stmt = make_node (OACC_CACHE);
36048 TREE_TYPE (stmt) = void_type_node;
36049 OACC_CACHE_CLAUSES (stmt) = clauses;
36050 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36051 add_stmt (stmt);
36053 return stmt;
36056 /* OpenACC 2.0:
36057 # pragma acc data oacc-data-clause[optseq] new-line
36058 structured-block */
36060 #define OACC_DATA_CLAUSE_MASK \
36061 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36062 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36063 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36064 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36065 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36066 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36067 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36068 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36069 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36070 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36071 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36073 static tree
36074 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36076 tree stmt, clauses, block;
36077 unsigned int save;
36079 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36080 "#pragma acc data", pragma_tok);
36082 block = begin_omp_parallel ();
36083 save = cp_parser_begin_omp_structured_block (parser);
36084 cp_parser_statement (parser, NULL_TREE, false, if_p);
36085 cp_parser_end_omp_structured_block (parser, save);
36086 stmt = finish_oacc_data (clauses, block);
36087 return stmt;
36090 /* OpenACC 2.0:
36091 # pragma acc host_data <clauses> new-line
36092 structured-block */
36094 #define OACC_HOST_DATA_CLAUSE_MASK \
36095 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36097 static tree
36098 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36100 tree stmt, clauses, block;
36101 unsigned int save;
36103 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36104 "#pragma acc host_data", pragma_tok);
36106 block = begin_omp_parallel ();
36107 save = cp_parser_begin_omp_structured_block (parser);
36108 cp_parser_statement (parser, NULL_TREE, false, if_p);
36109 cp_parser_end_omp_structured_block (parser, save);
36110 stmt = finish_oacc_host_data (clauses, block);
36111 return stmt;
36114 /* OpenACC 2.0:
36115 # pragma acc declare oacc-data-clause[optseq] new-line
36118 #define OACC_DECLARE_CLAUSE_MASK \
36119 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36120 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36121 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36122 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36123 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36124 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36125 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36126 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36127 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36128 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36129 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36130 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36132 static tree
36133 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36135 tree clauses, stmt;
36136 bool error = false;
36138 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36139 "#pragma acc declare", pragma_tok, true);
36142 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36144 error_at (pragma_tok->location,
36145 "no valid clauses specified in %<#pragma acc declare%>");
36146 return NULL_TREE;
36149 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36151 location_t loc = OMP_CLAUSE_LOCATION (t);
36152 tree decl = OMP_CLAUSE_DECL (t);
36153 if (!DECL_P (decl))
36155 error_at (loc, "array section in %<#pragma acc declare%>");
36156 error = true;
36157 continue;
36159 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36160 switch (OMP_CLAUSE_MAP_KIND (t))
36162 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36163 case GOMP_MAP_FORCE_ALLOC:
36164 case GOMP_MAP_FORCE_TO:
36165 case GOMP_MAP_FORCE_DEVICEPTR:
36166 case GOMP_MAP_DEVICE_RESIDENT:
36167 break;
36169 case GOMP_MAP_LINK:
36170 if (!global_bindings_p ()
36171 && (TREE_STATIC (decl)
36172 || !DECL_EXTERNAL (decl)))
36174 error_at (loc,
36175 "%qD must be a global variable in "
36176 "%<#pragma acc declare link%>",
36177 decl);
36178 error = true;
36179 continue;
36181 break;
36183 default:
36184 if (global_bindings_p ())
36186 error_at (loc, "invalid OpenACC clause at file scope");
36187 error = true;
36188 continue;
36190 if (DECL_EXTERNAL (decl))
36192 error_at (loc,
36193 "invalid use of %<extern%> variable %qD "
36194 "in %<#pragma acc declare%>", decl);
36195 error = true;
36196 continue;
36198 else if (TREE_PUBLIC (decl))
36200 error_at (loc,
36201 "invalid use of %<global%> variable %qD "
36202 "in %<#pragma acc declare%>", decl);
36203 error = true;
36204 continue;
36206 break;
36209 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36210 || lookup_attribute ("omp declare target link",
36211 DECL_ATTRIBUTES (decl)))
36213 error_at (loc, "variable %qD used more than once with "
36214 "%<#pragma acc declare%>", decl);
36215 error = true;
36216 continue;
36219 if (!error)
36221 tree id;
36223 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36224 id = get_identifier ("omp declare target link");
36225 else
36226 id = get_identifier ("omp declare target");
36228 DECL_ATTRIBUTES (decl)
36229 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36230 if (global_bindings_p ())
36232 symtab_node *node = symtab_node::get (decl);
36233 if (node != NULL)
36235 node->offloadable = 1;
36236 if (ENABLE_OFFLOADING)
36238 g->have_offload = true;
36239 if (is_a <varpool_node *> (node))
36240 vec_safe_push (offload_vars, decl);
36247 if (error || global_bindings_p ())
36248 return NULL_TREE;
36250 stmt = make_node (OACC_DECLARE);
36251 TREE_TYPE (stmt) = void_type_node;
36252 OACC_DECLARE_CLAUSES (stmt) = clauses;
36253 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36255 add_stmt (stmt);
36257 return NULL_TREE;
36260 /* OpenACC 2.0:
36261 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36265 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36267 LOC is the location of the #pragma token.
36270 #define OACC_ENTER_DATA_CLAUSE_MASK \
36271 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36272 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36273 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36274 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36275 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36276 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36277 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36279 #define OACC_EXIT_DATA_CLAUSE_MASK \
36280 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36281 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36282 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36286 static tree
36287 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36288 bool enter)
36290 location_t loc = pragma_tok->location;
36291 tree stmt, clauses;
36292 const char *p = "";
36294 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36295 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36297 if (strcmp (p, "data") != 0)
36299 error_at (loc, enter
36300 ? "expected %<data%> after %<#pragma acc enter%>"
36301 : "expected %<data%> after %<#pragma acc exit%>");
36302 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36303 return NULL_TREE;
36306 cp_lexer_consume_token (parser->lexer);
36308 if (enter)
36309 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36310 "#pragma acc enter data", pragma_tok);
36311 else
36312 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36313 "#pragma acc exit data", pragma_tok);
36315 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36317 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36318 enter ? "enter" : "exit");
36319 return NULL_TREE;
36322 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36323 TREE_TYPE (stmt) = void_type_node;
36324 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36325 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36326 add_stmt (stmt);
36327 return stmt;
36330 /* OpenACC 2.0:
36331 # pragma acc loop oacc-loop-clause[optseq] new-line
36332 structured-block */
36334 #define OACC_LOOP_CLAUSE_MASK \
36335 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36336 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36337 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36338 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36339 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36340 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36341 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36342 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36346 static tree
36347 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36348 omp_clause_mask mask, tree *cclauses, bool *if_p)
36350 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36352 strcat (p_name, " loop");
36353 mask |= OACC_LOOP_CLAUSE_MASK;
36355 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36356 cclauses == NULL);
36357 if (cclauses)
36359 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36360 if (*cclauses)
36361 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36362 if (clauses)
36363 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36366 tree block = begin_omp_structured_block ();
36367 int save = cp_parser_begin_omp_structured_block (parser);
36368 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36369 cp_parser_end_omp_structured_block (parser, save);
36370 add_stmt (finish_omp_structured_block (block));
36372 return stmt;
36375 /* OpenACC 2.0:
36376 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36377 structured-block
36381 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36382 structured-block
36385 #define OACC_KERNELS_CLAUSE_MASK \
36386 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36387 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36388 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36389 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36401 #define OACC_PARALLEL_CLAUSE_MASK \
36402 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36403 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36404 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36405 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36406 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36407 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36408 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36409 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36410 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36411 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36412 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36413 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36414 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36415 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36419 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36420 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36421 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36423 static tree
36424 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36425 char *p_name, bool *if_p)
36427 omp_clause_mask mask;
36428 enum tree_code code;
36429 switch (cp_parser_pragma_kind (pragma_tok))
36431 case PRAGMA_OACC_KERNELS:
36432 strcat (p_name, " kernels");
36433 mask = OACC_KERNELS_CLAUSE_MASK;
36434 code = OACC_KERNELS;
36435 break;
36436 case PRAGMA_OACC_PARALLEL:
36437 strcat (p_name, " parallel");
36438 mask = OACC_PARALLEL_CLAUSE_MASK;
36439 code = OACC_PARALLEL;
36440 break;
36441 default:
36442 gcc_unreachable ();
36445 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36447 const char *p
36448 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36449 if (strcmp (p, "loop") == 0)
36451 cp_lexer_consume_token (parser->lexer);
36452 tree block = begin_omp_parallel ();
36453 tree clauses;
36454 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36455 if_p);
36456 return finish_omp_construct (code, block, clauses);
36460 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36462 tree block = begin_omp_parallel ();
36463 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36464 cp_parser_statement (parser, NULL_TREE, false, if_p);
36465 cp_parser_end_omp_structured_block (parser, save);
36466 return finish_omp_construct (code, block, clauses);
36469 /* OpenACC 2.0:
36470 # pragma acc update oacc-update-clause[optseq] new-line
36473 #define OACC_UPDATE_CLAUSE_MASK \
36474 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36475 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36476 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36477 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36478 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36479 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36481 static tree
36482 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36484 tree stmt, clauses;
36486 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36487 "#pragma acc update", pragma_tok);
36489 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36491 error_at (pragma_tok->location,
36492 "%<#pragma acc update%> must contain at least one "
36493 "%<device%> or %<host%> or %<self%> clause");
36494 return NULL_TREE;
36497 stmt = make_node (OACC_UPDATE);
36498 TREE_TYPE (stmt) = void_type_node;
36499 OACC_UPDATE_CLAUSES (stmt) = clauses;
36500 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36501 add_stmt (stmt);
36502 return stmt;
36505 /* OpenACC 2.0:
36506 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36508 LOC is the location of the #pragma token.
36511 #define OACC_WAIT_CLAUSE_MASK \
36512 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36514 static tree
36515 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36517 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36518 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36520 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36521 list = cp_parser_oacc_wait_list (parser, loc, list);
36523 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36524 "#pragma acc wait", pragma_tok);
36526 stmt = c_finish_oacc_wait (loc, list, clauses);
36527 stmt = finish_expr_stmt (stmt);
36529 return stmt;
36532 /* OpenMP 4.0:
36533 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36535 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36536 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36539 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36543 static void
36544 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36545 enum pragma_context context)
36547 bool first_p = parser->omp_declare_simd == NULL;
36548 cp_omp_declare_simd_data data;
36549 if (first_p)
36551 data.error_seen = false;
36552 data.fndecl_seen = false;
36553 data.tokens = vNULL;
36554 data.clauses = NULL_TREE;
36555 /* It is safe to take the address of a local variable; it will only be
36556 used while this scope is live. */
36557 parser->omp_declare_simd = &data;
36560 /* Store away all pragma tokens. */
36561 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36562 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36563 cp_lexer_consume_token (parser->lexer);
36564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36565 parser->omp_declare_simd->error_seen = true;
36566 cp_parser_require_pragma_eol (parser, pragma_tok);
36567 struct cp_token_cache *cp
36568 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36569 parser->omp_declare_simd->tokens.safe_push (cp);
36571 if (first_p)
36573 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36574 cp_parser_pragma (parser, context, NULL);
36575 switch (context)
36577 case pragma_external:
36578 cp_parser_declaration (parser);
36579 break;
36580 case pragma_member:
36581 cp_parser_member_declaration (parser);
36582 break;
36583 case pragma_objc_icode:
36584 cp_parser_block_declaration (parser, /*statement_p=*/false);
36585 break;
36586 default:
36587 cp_parser_declaration_statement (parser);
36588 break;
36590 if (parser->omp_declare_simd
36591 && !parser->omp_declare_simd->error_seen
36592 && !parser->omp_declare_simd->fndecl_seen)
36593 error_at (pragma_tok->location,
36594 "%<#pragma omp declare simd%> not immediately followed by "
36595 "function declaration or definition");
36596 data.tokens.release ();
36597 parser->omp_declare_simd = NULL;
36601 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36602 This function is modelled similar to the late parsing of omp declare
36603 simd. */
36605 static tree
36606 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36608 struct cp_token_cache *ce;
36609 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36610 int ii = 0;
36612 if (parser->omp_declare_simd != NULL
36613 || lookup_attribute ("simd", attrs))
36615 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36616 "used in the same function marked as a Cilk Plus SIMD-enabled "
36617 "function");
36618 parser->cilk_simd_fn_info->tokens.release ();
36619 XDELETE (parser->cilk_simd_fn_info);
36620 parser->cilk_simd_fn_info = NULL;
36621 return attrs;
36623 if (!info->error_seen && info->fndecl_seen)
36625 error ("vector attribute not immediately followed by a single function"
36626 " declaration or definition");
36627 info->error_seen = true;
36629 if (info->error_seen)
36630 return attrs;
36632 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36634 tree c, cl;
36636 cp_parser_push_lexer_for_tokens (parser, ce);
36637 parser->lexer->in_pragma = true;
36638 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36639 "SIMD-enabled functions attribute",
36640 NULL);
36641 cp_parser_pop_lexer (parser);
36642 if (cl)
36643 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36645 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36646 TREE_CHAIN (c) = attrs;
36647 attrs = c;
36649 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36650 TREE_CHAIN (c) = attrs;
36651 if (processing_template_decl)
36652 ATTR_IS_DEPENDENT (c) = 1;
36653 attrs = c;
36655 info->fndecl_seen = true;
36656 parser->cilk_simd_fn_info->tokens.release ();
36657 XDELETE (parser->cilk_simd_fn_info);
36658 parser->cilk_simd_fn_info = NULL;
36659 return attrs;
36662 /* Finalize #pragma omp declare simd clauses after direct declarator has
36663 been parsed, and put that into "omp declare simd" attribute. */
36665 static tree
36666 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36668 struct cp_token_cache *ce;
36669 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36670 int i;
36672 if (!data->error_seen && data->fndecl_seen)
36674 error ("%<#pragma omp declare simd%> not immediately followed by "
36675 "a single function declaration or definition");
36676 data->error_seen = true;
36678 if (data->error_seen)
36679 return attrs;
36681 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36683 tree c, cl;
36685 cp_parser_push_lexer_for_tokens (parser, ce);
36686 parser->lexer->in_pragma = true;
36687 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36688 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36689 cp_lexer_consume_token (parser->lexer);
36690 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36691 "#pragma omp declare simd", pragma_tok);
36692 cp_parser_pop_lexer (parser);
36693 if (cl)
36694 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36695 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36696 TREE_CHAIN (c) = attrs;
36697 if (processing_template_decl)
36698 ATTR_IS_DEPENDENT (c) = 1;
36699 attrs = c;
36702 data->fndecl_seen = true;
36703 return attrs;
36707 /* OpenMP 4.0:
36708 # pragma omp declare target new-line
36709 declarations and definitions
36710 # pragma omp end declare target new-line
36712 OpenMP 4.5:
36713 # pragma omp declare target ( extended-list ) new-line
36715 # pragma omp declare target declare-target-clauses[seq] new-line */
36717 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36718 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36719 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36721 static void
36722 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36724 tree clauses = NULL_TREE;
36725 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36726 clauses
36727 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36728 "#pragma omp declare target", pragma_tok);
36729 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36731 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36732 clauses);
36733 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36734 cp_parser_require_pragma_eol (parser, pragma_tok);
36736 else
36738 cp_parser_require_pragma_eol (parser, pragma_tok);
36739 scope_chain->omp_declare_target_attribute++;
36740 return;
36742 if (scope_chain->omp_declare_target_attribute)
36743 error_at (pragma_tok->location,
36744 "%<#pragma omp declare target%> with clauses in between "
36745 "%<#pragma omp declare target%> without clauses and "
36746 "%<#pragma omp end declare target%>");
36747 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36749 tree t = OMP_CLAUSE_DECL (c), id;
36750 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36751 tree at2 = lookup_attribute ("omp declare target link",
36752 DECL_ATTRIBUTES (t));
36753 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36755 id = get_identifier ("omp declare target link");
36756 std::swap (at1, at2);
36758 else
36759 id = get_identifier ("omp declare target");
36760 if (at2)
36762 error_at (OMP_CLAUSE_LOCATION (c),
36763 "%qD specified both in declare target %<link%> and %<to%>"
36764 " clauses", t);
36765 continue;
36767 if (!at1)
36769 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36770 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36771 continue;
36773 symtab_node *node = symtab_node::get (t);
36774 if (node != NULL)
36776 node->offloadable = 1;
36777 if (ENABLE_OFFLOADING)
36779 g->have_offload = true;
36780 if (is_a <varpool_node *> (node))
36781 vec_safe_push (offload_vars, t);
36788 static void
36789 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36791 const char *p = "";
36792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36794 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36795 p = IDENTIFIER_POINTER (id);
36797 if (strcmp (p, "declare") == 0)
36799 cp_lexer_consume_token (parser->lexer);
36800 p = "";
36801 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36803 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36804 p = IDENTIFIER_POINTER (id);
36806 if (strcmp (p, "target") == 0)
36807 cp_lexer_consume_token (parser->lexer);
36808 else
36810 cp_parser_error (parser, "expected %<target%>");
36811 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36812 return;
36815 else
36817 cp_parser_error (parser, "expected %<declare%>");
36818 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36819 return;
36821 cp_parser_require_pragma_eol (parser, pragma_tok);
36822 if (!scope_chain->omp_declare_target_attribute)
36823 error_at (pragma_tok->location,
36824 "%<#pragma omp end declare target%> without corresponding "
36825 "%<#pragma omp declare target%>");
36826 else
36827 scope_chain->omp_declare_target_attribute--;
36830 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
36831 expression and optional initializer clause of
36832 #pragma omp declare reduction. We store the expression(s) as
36833 either 3, 6 or 7 special statements inside of the artificial function's
36834 body. The first two statements are DECL_EXPRs for the artificial
36835 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
36836 expression that uses those variables.
36837 If there was any INITIALIZER clause, this is followed by further statements,
36838 the fourth and fifth statements are DECL_EXPRs for the artificial
36839 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
36840 constructor variant (first token after open paren is not omp_priv),
36841 then the sixth statement is a statement with the function call expression
36842 that uses the OMP_PRIV and optionally OMP_ORIG variable.
36843 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
36844 to initialize the OMP_PRIV artificial variable and there is seventh
36845 statement, a DECL_EXPR of the OMP_PRIV statement again. */
36847 static bool
36848 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
36850 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
36851 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
36852 type = TREE_TYPE (type);
36853 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
36854 DECL_ARTIFICIAL (omp_out) = 1;
36855 pushdecl (omp_out);
36856 add_decl_expr (omp_out);
36857 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
36858 DECL_ARTIFICIAL (omp_in) = 1;
36859 pushdecl (omp_in);
36860 add_decl_expr (omp_in);
36861 tree combiner;
36862 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
36864 keep_next_level (true);
36865 tree block = begin_omp_structured_block ();
36866 combiner = cp_parser_expression (parser);
36867 finish_expr_stmt (combiner);
36868 block = finish_omp_structured_block (block);
36869 add_stmt (block);
36871 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36872 return false;
36874 const char *p = "";
36875 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36877 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36878 p = IDENTIFIER_POINTER (id);
36881 if (strcmp (p, "initializer") == 0)
36883 cp_lexer_consume_token (parser->lexer);
36884 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
36885 return false;
36887 p = "";
36888 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36891 p = IDENTIFIER_POINTER (id);
36894 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
36895 DECL_ARTIFICIAL (omp_priv) = 1;
36896 pushdecl (omp_priv);
36897 add_decl_expr (omp_priv);
36898 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
36899 DECL_ARTIFICIAL (omp_orig) = 1;
36900 pushdecl (omp_orig);
36901 add_decl_expr (omp_orig);
36903 keep_next_level (true);
36904 block = begin_omp_structured_block ();
36906 bool ctor = false;
36907 if (strcmp (p, "omp_priv") == 0)
36909 bool is_direct_init, is_non_constant_init;
36910 ctor = true;
36911 cp_lexer_consume_token (parser->lexer);
36912 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
36913 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
36914 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36915 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
36916 == CPP_CLOSE_PAREN
36917 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
36918 == CPP_CLOSE_PAREN))
36920 finish_omp_structured_block (block);
36921 error ("invalid initializer clause");
36922 return false;
36924 initializer = cp_parser_initializer (parser, &is_direct_init,
36925 &is_non_constant_init);
36926 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
36927 NULL_TREE, LOOKUP_ONLYCONVERTING);
36929 else
36931 cp_parser_parse_tentatively (parser);
36932 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
36933 /*check_dependency_p=*/true,
36934 /*template_p=*/NULL,
36935 /*declarator_p=*/false,
36936 /*optional_p=*/false);
36937 vec<tree, va_gc> *args;
36938 if (fn_name == error_mark_node
36939 || cp_parser_error_occurred (parser)
36940 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
36941 || ((args = cp_parser_parenthesized_expression_list
36942 (parser, non_attr, /*cast_p=*/false,
36943 /*allow_expansion_p=*/true,
36944 /*non_constant_p=*/NULL)),
36945 cp_parser_error_occurred (parser)))
36947 finish_omp_structured_block (block);
36948 cp_parser_abort_tentative_parse (parser);
36949 cp_parser_error (parser, "expected id-expression (arguments)");
36950 return false;
36952 unsigned int i;
36953 tree arg;
36954 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
36955 if (arg == omp_priv
36956 || (TREE_CODE (arg) == ADDR_EXPR
36957 && TREE_OPERAND (arg, 0) == omp_priv))
36958 break;
36959 cp_parser_abort_tentative_parse (parser);
36960 if (arg == NULL_TREE)
36961 error ("one of the initializer call arguments should be %<omp_priv%>"
36962 " or %<&omp_priv%>");
36963 initializer = cp_parser_postfix_expression (parser, false, false, false,
36964 false, NULL);
36965 finish_expr_stmt (initializer);
36968 block = finish_omp_structured_block (block);
36969 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
36970 add_stmt (block);
36972 if (ctor)
36973 add_decl_expr (omp_orig);
36975 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
36976 return false;
36979 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
36980 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
36982 return true;
36985 /* OpenMP 4.0
36986 #pragma omp declare reduction (reduction-id : typename-list : expression) \
36987 initializer-clause[opt] new-line
36989 initializer-clause:
36990 initializer (omp_priv initializer)
36991 initializer (function-name (argument-list)) */
36993 static void
36994 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
36995 enum pragma_context)
36997 auto_vec<tree> types;
36998 enum tree_code reduc_code = ERROR_MARK;
36999 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37000 unsigned int i;
37001 cp_token *first_token;
37002 cp_token_cache *cp;
37003 int errs;
37004 void *p;
37006 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37007 p = obstack_alloc (&declarator_obstack, 0);
37009 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37010 goto fail;
37012 switch (cp_lexer_peek_token (parser->lexer)->type)
37014 case CPP_PLUS:
37015 reduc_code = PLUS_EXPR;
37016 break;
37017 case CPP_MULT:
37018 reduc_code = MULT_EXPR;
37019 break;
37020 case CPP_MINUS:
37021 reduc_code = MINUS_EXPR;
37022 break;
37023 case CPP_AND:
37024 reduc_code = BIT_AND_EXPR;
37025 break;
37026 case CPP_XOR:
37027 reduc_code = BIT_XOR_EXPR;
37028 break;
37029 case CPP_OR:
37030 reduc_code = BIT_IOR_EXPR;
37031 break;
37032 case CPP_AND_AND:
37033 reduc_code = TRUTH_ANDIF_EXPR;
37034 break;
37035 case CPP_OR_OR:
37036 reduc_code = TRUTH_ORIF_EXPR;
37037 break;
37038 case CPP_NAME:
37039 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37040 break;
37041 default:
37042 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37043 "%<|%>, %<&&%>, %<||%> or identifier");
37044 goto fail;
37047 if (reduc_code != ERROR_MARK)
37048 cp_lexer_consume_token (parser->lexer);
37050 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37051 if (reduc_id == error_mark_node)
37052 goto fail;
37054 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37055 goto fail;
37057 /* Types may not be defined in declare reduction type list. */
37058 const char *saved_message;
37059 saved_message = parser->type_definition_forbidden_message;
37060 parser->type_definition_forbidden_message
37061 = G_("types may not be defined in declare reduction type list");
37062 bool saved_colon_corrects_to_scope_p;
37063 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37064 parser->colon_corrects_to_scope_p = false;
37065 bool saved_colon_doesnt_start_class_def_p;
37066 saved_colon_doesnt_start_class_def_p
37067 = parser->colon_doesnt_start_class_def_p;
37068 parser->colon_doesnt_start_class_def_p = true;
37070 while (true)
37072 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37073 type = cp_parser_type_id (parser);
37074 if (type == error_mark_node)
37076 else if (ARITHMETIC_TYPE_P (type)
37077 && (orig_reduc_id == NULL_TREE
37078 || (TREE_CODE (type) != COMPLEX_TYPE
37079 && (strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37080 "min") == 0
37081 || strcmp (IDENTIFIER_POINTER (orig_reduc_id),
37082 "max") == 0))))
37083 error_at (loc, "predeclared arithmetic type %qT in "
37084 "%<#pragma omp declare reduction%>", type);
37085 else if (TREE_CODE (type) == FUNCTION_TYPE
37086 || TREE_CODE (type) == METHOD_TYPE
37087 || TREE_CODE (type) == ARRAY_TYPE)
37088 error_at (loc, "function or array type %qT in "
37089 "%<#pragma omp declare reduction%>", type);
37090 else if (TREE_CODE (type) == REFERENCE_TYPE)
37091 error_at (loc, "reference type %qT in "
37092 "%<#pragma omp declare reduction%>", type);
37093 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37094 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37095 "%<#pragma omp declare reduction%>", type);
37096 else
37097 types.safe_push (type);
37099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37100 cp_lexer_consume_token (parser->lexer);
37101 else
37102 break;
37105 /* Restore the saved message. */
37106 parser->type_definition_forbidden_message = saved_message;
37107 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37108 parser->colon_doesnt_start_class_def_p
37109 = saved_colon_doesnt_start_class_def_p;
37111 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37112 || types.is_empty ())
37114 fail:
37115 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37116 goto done;
37119 first_token = cp_lexer_peek_token (parser->lexer);
37120 cp = NULL;
37121 errs = errorcount;
37122 FOR_EACH_VEC_ELT (types, i, type)
37124 tree fntype
37125 = build_function_type_list (void_type_node,
37126 cp_build_reference_type (type, false),
37127 NULL_TREE);
37128 tree this_reduc_id = reduc_id;
37129 if (!dependent_type_p (type))
37130 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37131 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37132 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37133 DECL_ARTIFICIAL (fndecl) = 1;
37134 DECL_EXTERNAL (fndecl) = 1;
37135 DECL_DECLARED_INLINE_P (fndecl) = 1;
37136 DECL_IGNORED_P (fndecl) = 1;
37137 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37138 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37139 DECL_ATTRIBUTES (fndecl)
37140 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37141 DECL_ATTRIBUTES (fndecl));
37142 if (processing_template_decl)
37143 fndecl = push_template_decl (fndecl);
37144 bool block_scope = false;
37145 tree block = NULL_TREE;
37146 if (current_function_decl)
37148 block_scope = true;
37149 DECL_CONTEXT (fndecl) = global_namespace;
37150 if (!processing_template_decl)
37151 pushdecl (fndecl);
37153 else if (current_class_type)
37155 if (cp == NULL)
37157 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37158 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37159 cp_lexer_consume_token (parser->lexer);
37160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37161 goto fail;
37162 cp = cp_token_cache_new (first_token,
37163 cp_lexer_peek_nth_token (parser->lexer,
37164 2));
37166 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37167 finish_member_declaration (fndecl);
37168 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37169 DECL_PENDING_INLINE_P (fndecl) = 1;
37170 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37171 continue;
37173 else
37175 DECL_CONTEXT (fndecl) = current_namespace;
37176 pushdecl (fndecl);
37178 if (!block_scope)
37179 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37180 else
37181 block = begin_omp_structured_block ();
37182 if (cp)
37184 cp_parser_push_lexer_for_tokens (parser, cp);
37185 parser->lexer->in_pragma = true;
37187 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37189 if (!block_scope)
37190 finish_function (0);
37191 else
37192 DECL_CONTEXT (fndecl) = current_function_decl;
37193 if (cp)
37194 cp_parser_pop_lexer (parser);
37195 goto fail;
37197 if (cp)
37198 cp_parser_pop_lexer (parser);
37199 if (!block_scope)
37200 finish_function (0);
37201 else
37203 DECL_CONTEXT (fndecl) = current_function_decl;
37204 block = finish_omp_structured_block (block);
37205 if (TREE_CODE (block) == BIND_EXPR)
37206 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37207 else if (TREE_CODE (block) == STATEMENT_LIST)
37208 DECL_SAVED_TREE (fndecl) = block;
37209 if (processing_template_decl)
37210 add_decl_expr (fndecl);
37212 cp_check_omp_declare_reduction (fndecl);
37213 if (cp == NULL && types.length () > 1)
37214 cp = cp_token_cache_new (first_token,
37215 cp_lexer_peek_nth_token (parser->lexer, 2));
37216 if (errs != errorcount)
37217 break;
37220 cp_parser_require_pragma_eol (parser, pragma_tok);
37222 done:
37223 /* Free any declarators allocated. */
37224 obstack_free (&declarator_obstack, p);
37227 /* OpenMP 4.0
37228 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37229 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37230 initializer-clause[opt] new-line
37231 #pragma omp declare target new-line */
37233 static void
37234 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37235 enum pragma_context context)
37237 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37239 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37240 const char *p = IDENTIFIER_POINTER (id);
37242 if (strcmp (p, "simd") == 0)
37244 cp_lexer_consume_token (parser->lexer);
37245 cp_parser_omp_declare_simd (parser, pragma_tok,
37246 context);
37247 return;
37249 cp_ensure_no_omp_declare_simd (parser);
37250 if (strcmp (p, "reduction") == 0)
37252 cp_lexer_consume_token (parser->lexer);
37253 cp_parser_omp_declare_reduction (parser, pragma_tok,
37254 context);
37255 return;
37257 if (!flag_openmp) /* flag_openmp_simd */
37259 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37260 return;
37262 if (strcmp (p, "target") == 0)
37264 cp_lexer_consume_token (parser->lexer);
37265 cp_parser_omp_declare_target (parser, pragma_tok);
37266 return;
37269 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37270 "or %<target%>");
37271 cp_parser_require_pragma_eol (parser, pragma_tok);
37274 /* OpenMP 4.5:
37275 #pragma omp taskloop taskloop-clause[optseq] new-line
37276 for-loop
37278 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37279 for-loop */
37281 #define OMP_TASKLOOP_CLAUSE_MASK \
37282 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37283 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37284 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37297 static tree
37298 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37299 char *p_name, omp_clause_mask mask, tree *cclauses,
37300 bool *if_p)
37302 tree clauses, sb, ret;
37303 unsigned int save;
37304 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37306 strcat (p_name, " taskloop");
37307 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37309 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37311 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37312 const char *p = IDENTIFIER_POINTER (id);
37314 if (strcmp (p, "simd") == 0)
37316 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37317 if (cclauses == NULL)
37318 cclauses = cclauses_buf;
37320 cp_lexer_consume_token (parser->lexer);
37321 if (!flag_openmp) /* flag_openmp_simd */
37322 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37323 cclauses, if_p);
37324 sb = begin_omp_structured_block ();
37325 save = cp_parser_begin_omp_structured_block (parser);
37326 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37327 cclauses, if_p);
37328 cp_parser_end_omp_structured_block (parser, save);
37329 tree body = finish_omp_structured_block (sb);
37330 if (ret == NULL)
37331 return ret;
37332 ret = make_node (OMP_TASKLOOP);
37333 TREE_TYPE (ret) = void_type_node;
37334 OMP_FOR_BODY (ret) = body;
37335 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37336 SET_EXPR_LOCATION (ret, loc);
37337 add_stmt (ret);
37338 return ret;
37341 if (!flag_openmp) /* flag_openmp_simd */
37343 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37344 return NULL_TREE;
37347 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37348 cclauses == NULL);
37349 if (cclauses)
37351 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37352 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37355 sb = begin_omp_structured_block ();
37356 save = cp_parser_begin_omp_structured_block (parser);
37358 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37359 if_p);
37361 cp_parser_end_omp_structured_block (parser, save);
37362 add_stmt (finish_omp_structured_block (sb));
37364 return ret;
37368 /* OpenACC 2.0:
37369 # pragma acc routine oacc-routine-clause[optseq] new-line
37370 function-definition
37372 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37375 #define OACC_ROUTINE_CLAUSE_MASK \
37376 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37377 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37378 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37379 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37382 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37383 component, which must resolve to a declared namespace-scope
37384 function. The clauses are either processed directly (for a named
37385 function), or defered until the immediatley following declaration
37386 is parsed. */
37388 static void
37389 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37390 enum pragma_context context)
37392 gcc_checking_assert (context == pragma_external);
37393 /* The checking for "another pragma following this one" in the "no optional
37394 '( name )'" case makes sure that we dont re-enter. */
37395 gcc_checking_assert (parser->oacc_routine == NULL);
37397 cp_oacc_routine_data data;
37398 data.error_seen = false;
37399 data.fndecl_seen = false;
37400 data.tokens = vNULL;
37401 data.clauses = NULL_TREE;
37402 data.loc = pragma_tok->location;
37403 /* It is safe to take the address of a local variable; it will only be
37404 used while this scope is live. */
37405 parser->oacc_routine = &data;
37407 /* Look for optional '( name )'. */
37408 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37410 cp_lexer_consume_token (parser->lexer); /* '(' */
37412 /* We parse the name as an id-expression. If it resolves to
37413 anything other than a non-overloaded function at namespace
37414 scope, it's an error. */
37415 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37416 tree name = cp_parser_id_expression (parser,
37417 /*template_keyword_p=*/false,
37418 /*check_dependency_p=*/false,
37419 /*template_p=*/NULL,
37420 /*declarator_p=*/false,
37421 /*optional_p=*/false);
37422 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37423 if (name != error_mark_node && decl == error_mark_node)
37424 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37426 if (decl == error_mark_node
37427 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37429 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37430 parser->oacc_routine = NULL;
37431 return;
37434 data.clauses
37435 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37436 "#pragma acc routine",
37437 cp_lexer_peek_token (parser->lexer));
37439 if (decl && is_overloaded_fn (decl)
37440 && (TREE_CODE (decl) != FUNCTION_DECL
37441 || DECL_FUNCTION_TEMPLATE_P (decl)))
37443 error_at (name_loc,
37444 "%<#pragma acc routine%> names a set of overloads");
37445 parser->oacc_routine = NULL;
37446 return;
37449 /* Perhaps we should use the same rule as declarations in different
37450 namespaces? */
37451 if (!DECL_NAMESPACE_SCOPE_P (decl))
37453 error_at (name_loc,
37454 "%qD does not refer to a namespace scope function", decl);
37455 parser->oacc_routine = NULL;
37456 return;
37459 if (TREE_CODE (decl) != FUNCTION_DECL)
37461 error_at (name_loc, "%qD does not refer to a function", decl);
37462 parser->oacc_routine = NULL;
37463 return;
37466 cp_finalize_oacc_routine (parser, decl, false);
37467 parser->oacc_routine = NULL;
37469 else /* No optional '( name )'. */
37471 /* Store away all pragma tokens. */
37472 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37473 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37474 cp_lexer_consume_token (parser->lexer);
37475 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37476 parser->oacc_routine->error_seen = true;
37477 cp_parser_require_pragma_eol (parser, pragma_tok);
37478 struct cp_token_cache *cp
37479 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37480 parser->oacc_routine->tokens.safe_push (cp);
37482 /* Emit a helpful diagnostic if there's another pragma following this
37483 one. */
37484 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37486 cp_ensure_no_oacc_routine (parser);
37487 data.tokens.release ();
37488 /* ..., and then just keep going. */
37489 return;
37492 /* We only have to consider the pragma_external case here. */
37493 cp_parser_declaration (parser);
37494 if (parser->oacc_routine
37495 && !parser->oacc_routine->fndecl_seen)
37496 cp_ensure_no_oacc_routine (parser);
37497 else
37498 parser->oacc_routine = NULL;
37499 data.tokens.release ();
37503 /* Finalize #pragma acc routine clauses after direct declarator has
37504 been parsed. */
37506 static tree
37507 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37509 struct cp_token_cache *ce;
37510 cp_oacc_routine_data *data = parser->oacc_routine;
37512 if (!data->error_seen && data->fndecl_seen)
37514 error_at (data->loc,
37515 "%<#pragma acc routine%> not immediately followed by "
37516 "a single function declaration or definition");
37517 data->error_seen = true;
37519 if (data->error_seen)
37520 return attrs;
37522 gcc_checking_assert (data->tokens.length () == 1);
37523 ce = data->tokens[0];
37525 cp_parser_push_lexer_for_tokens (parser, ce);
37526 parser->lexer->in_pragma = true;
37527 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37529 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37530 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37531 parser->oacc_routine->clauses
37532 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37533 "#pragma acc routine", pragma_tok);
37534 cp_parser_pop_lexer (parser);
37535 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37536 fndecl_seen. */
37538 return attrs;
37541 /* Apply any saved OpenACC routine clauses to a just-parsed
37542 declaration. */
37544 static void
37545 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37547 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37549 /* Keep going if we're in error reporting mode. */
37550 if (parser->oacc_routine->error_seen
37551 || fndecl == error_mark_node)
37552 return;
37554 if (parser->oacc_routine->fndecl_seen)
37556 error_at (parser->oacc_routine->loc,
37557 "%<#pragma acc routine%> not immediately followed by"
37558 " a single function declaration or definition");
37559 parser->oacc_routine = NULL;
37560 return;
37562 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37564 cp_ensure_no_oacc_routine (parser);
37565 return;
37568 if (oacc_get_fn_attrib (fndecl))
37570 error_at (parser->oacc_routine->loc,
37571 "%<#pragma acc routine%> already applied to %qD", fndecl);
37572 parser->oacc_routine = NULL;
37573 return;
37576 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37578 error_at (parser->oacc_routine->loc,
37579 "%<#pragma acc routine%> must be applied before %s",
37580 TREE_USED (fndecl) ? "use" : "definition");
37581 parser->oacc_routine = NULL;
37582 return;
37585 /* Process the routine's dimension clauses. */
37586 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37587 oacc_replace_fn_attrib (fndecl, dims);
37589 /* Add an "omp declare target" attribute. */
37590 DECL_ATTRIBUTES (fndecl)
37591 = tree_cons (get_identifier ("omp declare target"),
37592 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37594 /* Don't unset parser->oacc_routine here: we may still need it to
37595 diagnose wrong usage. But, remember that we've used this "#pragma acc
37596 routine". */
37597 parser->oacc_routine->fndecl_seen = true;
37601 /* Main entry point to OpenMP statement pragmas. */
37603 static void
37604 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37606 tree stmt;
37607 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37608 omp_clause_mask mask (0);
37610 switch (cp_parser_pragma_kind (pragma_tok))
37612 case PRAGMA_OACC_ATOMIC:
37613 cp_parser_omp_atomic (parser, pragma_tok);
37614 return;
37615 case PRAGMA_OACC_CACHE:
37616 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37617 break;
37618 case PRAGMA_OACC_DATA:
37619 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37620 break;
37621 case PRAGMA_OACC_ENTER_DATA:
37622 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37623 break;
37624 case PRAGMA_OACC_EXIT_DATA:
37625 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37626 break;
37627 case PRAGMA_OACC_HOST_DATA:
37628 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37629 break;
37630 case PRAGMA_OACC_KERNELS:
37631 case PRAGMA_OACC_PARALLEL:
37632 strcpy (p_name, "#pragma acc");
37633 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37634 if_p);
37635 break;
37636 case PRAGMA_OACC_LOOP:
37637 strcpy (p_name, "#pragma acc");
37638 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37639 if_p);
37640 break;
37641 case PRAGMA_OACC_UPDATE:
37642 stmt = cp_parser_oacc_update (parser, pragma_tok);
37643 break;
37644 case PRAGMA_OACC_WAIT:
37645 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37646 break;
37647 case PRAGMA_OMP_ATOMIC:
37648 cp_parser_omp_atomic (parser, pragma_tok);
37649 return;
37650 case PRAGMA_OMP_CRITICAL:
37651 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37652 break;
37653 case PRAGMA_OMP_DISTRIBUTE:
37654 strcpy (p_name, "#pragma omp");
37655 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37656 if_p);
37657 break;
37658 case PRAGMA_OMP_FOR:
37659 strcpy (p_name, "#pragma omp");
37660 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37661 if_p);
37662 break;
37663 case PRAGMA_OMP_MASTER:
37664 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37665 break;
37666 case PRAGMA_OMP_PARALLEL:
37667 strcpy (p_name, "#pragma omp");
37668 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37669 if_p);
37670 break;
37671 case PRAGMA_OMP_SECTIONS:
37672 strcpy (p_name, "#pragma omp");
37673 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37674 break;
37675 case PRAGMA_OMP_SIMD:
37676 strcpy (p_name, "#pragma omp");
37677 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37678 if_p);
37679 break;
37680 case PRAGMA_OMP_SINGLE:
37681 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37682 break;
37683 case PRAGMA_OMP_TASK:
37684 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37685 break;
37686 case PRAGMA_OMP_TASKGROUP:
37687 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37688 break;
37689 case PRAGMA_OMP_TASKLOOP:
37690 strcpy (p_name, "#pragma omp");
37691 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37692 if_p);
37693 break;
37694 case PRAGMA_OMP_TEAMS:
37695 strcpy (p_name, "#pragma omp");
37696 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37697 if_p);
37698 break;
37699 default:
37700 gcc_unreachable ();
37703 protected_set_expr_location (stmt, pragma_tok->location);
37706 /* Transactional Memory parsing routines. */
37708 /* Parse a transaction attribute.
37710 txn-attribute:
37711 attribute
37712 [ [ identifier ] ]
37714 We use this instead of cp_parser_attributes_opt for transactions to avoid
37715 the pedwarn in C++98 mode. */
37717 static tree
37718 cp_parser_txn_attribute_opt (cp_parser *parser)
37720 cp_token *token;
37721 tree attr_name, attr = NULL;
37723 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37724 return cp_parser_attributes_opt (parser);
37726 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37727 return NULL_TREE;
37728 cp_lexer_consume_token (parser->lexer);
37729 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37730 goto error1;
37732 token = cp_lexer_peek_token (parser->lexer);
37733 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37735 token = cp_lexer_consume_token (parser->lexer);
37737 attr_name = (token->type == CPP_KEYWORD
37738 /* For keywords, use the canonical spelling,
37739 not the parsed identifier. */
37740 ? ridpointers[(int) token->keyword]
37741 : token->u.value);
37742 attr = build_tree_list (attr_name, NULL_TREE);
37744 else
37745 cp_parser_error (parser, "expected identifier");
37747 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37748 error1:
37749 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37750 return attr;
37753 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37755 transaction-statement:
37756 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37757 compound-statement
37758 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37761 static tree
37762 cp_parser_transaction (cp_parser *parser, cp_token *token)
37764 unsigned char old_in = parser->in_transaction;
37765 unsigned char this_in = 1, new_in;
37766 enum rid keyword = token->keyword;
37767 tree stmt, attrs, noex;
37769 cp_lexer_consume_token (parser->lexer);
37771 if (keyword == RID_TRANSACTION_RELAXED
37772 || keyword == RID_SYNCHRONIZED)
37773 this_in |= TM_STMT_ATTR_RELAXED;
37774 else
37776 attrs = cp_parser_txn_attribute_opt (parser);
37777 if (attrs)
37778 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37781 /* Parse a noexcept specification. */
37782 if (keyword == RID_ATOMIC_NOEXCEPT)
37783 noex = boolean_true_node;
37784 else if (keyword == RID_ATOMIC_CANCEL)
37786 /* cancel-and-throw is unimplemented. */
37787 sorry ("atomic_cancel");
37788 noex = NULL_TREE;
37790 else
37791 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37793 /* Keep track if we're in the lexical scope of an outer transaction. */
37794 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37796 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37798 parser->in_transaction = new_in;
37799 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37800 parser->in_transaction = old_in;
37802 finish_transaction_stmt (stmt, NULL, this_in, noex);
37804 return stmt;
37807 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37809 transaction-expression:
37810 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37811 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37814 static tree
37815 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37817 unsigned char old_in = parser->in_transaction;
37818 unsigned char this_in = 1;
37819 cp_token *token;
37820 tree expr, noex;
37821 bool noex_expr;
37822 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37824 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37825 || keyword == RID_TRANSACTION_RELAXED);
37827 if (!flag_tm)
37828 error_at (loc,
37829 keyword == RID_TRANSACTION_RELAXED
37830 ? G_("%<__transaction_relaxed%> without transactional memory "
37831 "support enabled")
37832 : G_("%<__transaction_atomic%> without transactional memory "
37833 "support enabled"));
37835 token = cp_parser_require_keyword (parser, keyword,
37836 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37837 : RT_TRANSACTION_RELAXED));
37838 gcc_assert (token != NULL);
37840 if (keyword == RID_TRANSACTION_RELAXED)
37841 this_in |= TM_STMT_ATTR_RELAXED;
37843 /* Set this early. This might mean that we allow transaction_cancel in
37844 an expression that we find out later actually has to be a constexpr.
37845 However, we expect that cxx_constant_value will be able to deal with
37846 this; also, if the noexcept has no constexpr, then what we parse next
37847 really is a transaction's body. */
37848 parser->in_transaction = this_in;
37850 /* Parse a noexcept specification. */
37851 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
37852 true);
37854 if (!noex || !noex_expr
37855 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
37857 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
37859 expr = cp_parser_expression (parser);
37860 expr = finish_parenthesized_expr (expr);
37862 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
37864 else
37866 /* The only expression that is available got parsed for the noexcept
37867 already. noexcept is true then. */
37868 expr = noex;
37869 noex = boolean_true_node;
37872 expr = build_transaction_expr (token->location, expr, this_in, noex);
37873 parser->in_transaction = old_in;
37875 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
37876 return error_mark_node;
37878 return (flag_tm ? expr : error_mark_node);
37881 /* Parse a function-transaction-block.
37883 function-transaction-block:
37884 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
37885 function-body
37886 __transaction_atomic txn-attribute[opt] function-try-block
37887 __transaction_relaxed ctor-initializer[opt] function-body
37888 __transaction_relaxed function-try-block
37891 static bool
37892 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
37894 unsigned char old_in = parser->in_transaction;
37895 unsigned char new_in = 1;
37896 tree compound_stmt, stmt, attrs;
37897 bool ctor_initializer_p;
37898 cp_token *token;
37900 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
37901 || keyword == RID_TRANSACTION_RELAXED);
37902 token = cp_parser_require_keyword (parser, keyword,
37903 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
37904 : RT_TRANSACTION_RELAXED));
37905 gcc_assert (token != NULL);
37907 if (keyword == RID_TRANSACTION_RELAXED)
37908 new_in |= TM_STMT_ATTR_RELAXED;
37909 else
37911 attrs = cp_parser_txn_attribute_opt (parser);
37912 if (attrs)
37913 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37916 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
37918 parser->in_transaction = new_in;
37920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
37921 ctor_initializer_p = cp_parser_function_try_block (parser);
37922 else
37923 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
37924 (parser, /*in_function_try_block=*/false);
37926 parser->in_transaction = old_in;
37928 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
37930 return ctor_initializer_p;
37933 /* Parse a __transaction_cancel statement.
37935 cancel-statement:
37936 __transaction_cancel txn-attribute[opt] ;
37937 __transaction_cancel txn-attribute[opt] throw-expression ;
37939 ??? Cancel and throw is not yet implemented. */
37941 static tree
37942 cp_parser_transaction_cancel (cp_parser *parser)
37944 cp_token *token;
37945 bool is_outer = false;
37946 tree stmt, attrs;
37948 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
37949 RT_TRANSACTION_CANCEL);
37950 gcc_assert (token != NULL);
37952 attrs = cp_parser_txn_attribute_opt (parser);
37953 if (attrs)
37954 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
37956 /* ??? Parse cancel-and-throw here. */
37958 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
37960 if (!flag_tm)
37962 error_at (token->location, "%<__transaction_cancel%> without "
37963 "transactional memory support enabled");
37964 return error_mark_node;
37966 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
37968 error_at (token->location, "%<__transaction_cancel%> within a "
37969 "%<__transaction_relaxed%>");
37970 return error_mark_node;
37972 else if (is_outer)
37974 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
37975 && !is_tm_may_cancel_outer (current_function_decl))
37977 error_at (token->location, "outer %<__transaction_cancel%> not "
37978 "within outer %<__transaction_atomic%>");
37979 error_at (token->location,
37980 " or a %<transaction_may_cancel_outer%> function");
37981 return error_mark_node;
37984 else if (parser->in_transaction == 0)
37986 error_at (token->location, "%<__transaction_cancel%> not within "
37987 "%<__transaction_atomic%>");
37988 return error_mark_node;
37991 stmt = build_tm_abort_call (token->location, is_outer);
37992 add_stmt (stmt);
37994 return stmt;
37997 /* The parser. */
37999 static GTY (()) cp_parser *the_parser;
38002 /* Special handling for the first token or line in the file. The first
38003 thing in the file might be #pragma GCC pch_preprocess, which loads a
38004 PCH file, which is a GC collection point. So we need to handle this
38005 first pragma without benefit of an existing lexer structure.
38007 Always returns one token to the caller in *FIRST_TOKEN. This is
38008 either the true first token of the file, or the first token after
38009 the initial pragma. */
38011 static void
38012 cp_parser_initial_pragma (cp_token *first_token)
38014 tree name = NULL;
38016 cp_lexer_get_preprocessor_token (NULL, first_token);
38017 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38018 return;
38020 cp_lexer_get_preprocessor_token (NULL, first_token);
38021 if (first_token->type == CPP_STRING)
38023 name = first_token->u.value;
38025 cp_lexer_get_preprocessor_token (NULL, first_token);
38026 if (first_token->type != CPP_PRAGMA_EOL)
38027 error_at (first_token->location,
38028 "junk at end of %<#pragma GCC pch_preprocess%>");
38030 else
38031 error_at (first_token->location, "expected string literal");
38033 /* Skip to the end of the pragma. */
38034 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38035 cp_lexer_get_preprocessor_token (NULL, first_token);
38037 /* Now actually load the PCH file. */
38038 if (name)
38039 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38041 /* Read one more token to return to our caller. We have to do this
38042 after reading the PCH file in, since its pointers have to be
38043 live. */
38044 cp_lexer_get_preprocessor_token (NULL, first_token);
38047 /* Parses the grainsize pragma for the _Cilk_for statement.
38048 Syntax:
38049 #pragma cilk grainsize = <VALUE>. */
38051 static void
38052 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38054 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38056 tree exp = cp_parser_binary_expression (parser, false, false,
38057 PREC_NOT_OPERATOR, NULL);
38058 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38059 if (!exp || exp == error_mark_node)
38061 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38062 return;
38065 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38067 cp_parser_cilk_for (parser, exp, if_p);
38068 else
38069 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38070 "%<#pragma cilk grainsize%> is not followed by "
38071 "%<_Cilk_for%>");
38072 return;
38074 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38077 /* Normal parsing of a pragma token. Here we can (and must) use the
38078 regular lexer. */
38080 static bool
38081 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38083 cp_token *pragma_tok;
38084 unsigned int id;
38085 tree stmt;
38086 bool ret;
38088 pragma_tok = cp_lexer_consume_token (parser->lexer);
38089 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38090 parser->lexer->in_pragma = true;
38092 id = cp_parser_pragma_kind (pragma_tok);
38093 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38094 cp_ensure_no_omp_declare_simd (parser);
38095 switch (id)
38097 case PRAGMA_GCC_PCH_PREPROCESS:
38098 error_at (pragma_tok->location,
38099 "%<#pragma GCC pch_preprocess%> must be first");
38100 break;
38102 case PRAGMA_OMP_BARRIER:
38103 switch (context)
38105 case pragma_compound:
38106 cp_parser_omp_barrier (parser, pragma_tok);
38107 return false;
38108 case pragma_stmt:
38109 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
38110 "used in compound statements");
38111 break;
38112 default:
38113 goto bad_stmt;
38115 break;
38117 case PRAGMA_OMP_FLUSH:
38118 switch (context)
38120 case pragma_compound:
38121 cp_parser_omp_flush (parser, pragma_tok);
38122 return false;
38123 case pragma_stmt:
38124 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
38125 "used in compound statements");
38126 break;
38127 default:
38128 goto bad_stmt;
38130 break;
38132 case PRAGMA_OMP_TASKWAIT:
38133 switch (context)
38135 case pragma_compound:
38136 cp_parser_omp_taskwait (parser, pragma_tok);
38137 return false;
38138 case pragma_stmt:
38139 error_at (pragma_tok->location,
38140 "%<#pragma omp taskwait%> may only be "
38141 "used in compound statements");
38142 break;
38143 default:
38144 goto bad_stmt;
38146 break;
38148 case PRAGMA_OMP_TASKYIELD:
38149 switch (context)
38151 case pragma_compound:
38152 cp_parser_omp_taskyield (parser, pragma_tok);
38153 return false;
38154 case pragma_stmt:
38155 error_at (pragma_tok->location,
38156 "%<#pragma omp taskyield%> may only be "
38157 "used in compound statements");
38158 break;
38159 default:
38160 goto bad_stmt;
38162 break;
38164 case PRAGMA_OMP_CANCEL:
38165 switch (context)
38167 case pragma_compound:
38168 cp_parser_omp_cancel (parser, pragma_tok);
38169 return false;
38170 case pragma_stmt:
38171 error_at (pragma_tok->location,
38172 "%<#pragma omp cancel%> may only be "
38173 "used in compound statements");
38174 break;
38175 default:
38176 goto bad_stmt;
38178 break;
38180 case PRAGMA_OMP_CANCELLATION_POINT:
38181 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38182 return false;
38184 case PRAGMA_OMP_THREADPRIVATE:
38185 cp_parser_omp_threadprivate (parser, pragma_tok);
38186 return false;
38188 case PRAGMA_OMP_DECLARE:
38189 cp_parser_omp_declare (parser, pragma_tok, context);
38190 return false;
38192 case PRAGMA_OACC_DECLARE:
38193 cp_parser_oacc_declare (parser, pragma_tok);
38194 return false;
38196 case PRAGMA_OACC_ENTER_DATA:
38197 if (context == pragma_stmt)
38199 cp_parser_error (parser, "%<#pragma acc enter data%> may only be "
38200 "used in compound statements");
38201 break;
38203 else if (context != pragma_compound)
38204 goto bad_stmt;
38205 cp_parser_omp_construct (parser, pragma_tok, if_p);
38206 return true;
38208 case PRAGMA_OACC_EXIT_DATA:
38209 if (context == pragma_stmt)
38211 cp_parser_error (parser, "%<#pragma acc exit data%> may only be "
38212 "used in compound statements");
38213 break;
38215 else if (context != pragma_compound)
38216 goto bad_stmt;
38217 cp_parser_omp_construct (parser, pragma_tok, if_p);
38218 return true;
38220 case PRAGMA_OACC_ROUTINE:
38221 if (context != pragma_external)
38223 error_at (pragma_tok->location,
38224 "%<#pragma acc routine%> must be at file scope");
38225 break;
38227 cp_parser_oacc_routine (parser, pragma_tok, context);
38228 return false;
38230 case PRAGMA_OACC_UPDATE:
38231 if (context == pragma_stmt)
38233 cp_parser_error (parser, "%<#pragma acc update%> may only be "
38234 "used in compound statements");
38235 break;
38237 else if (context != pragma_compound)
38238 goto bad_stmt;
38239 cp_parser_omp_construct (parser, pragma_tok, if_p);
38240 return true;
38242 case PRAGMA_OACC_WAIT:
38243 if (context == pragma_stmt)
38245 cp_parser_error (parser, "%<#pragma acc wait%> may only be "
38246 "used in compound statements");
38247 break;
38249 else if (context != pragma_compound)
38250 goto bad_stmt;
38251 cp_parser_omp_construct (parser, pragma_tok, if_p);
38252 return true;
38254 case PRAGMA_OACC_ATOMIC:
38255 case PRAGMA_OACC_CACHE:
38256 case PRAGMA_OACC_DATA:
38257 case PRAGMA_OACC_HOST_DATA:
38258 case PRAGMA_OACC_KERNELS:
38259 case PRAGMA_OACC_PARALLEL:
38260 case PRAGMA_OACC_LOOP:
38261 case PRAGMA_OMP_ATOMIC:
38262 case PRAGMA_OMP_CRITICAL:
38263 case PRAGMA_OMP_DISTRIBUTE:
38264 case PRAGMA_OMP_FOR:
38265 case PRAGMA_OMP_MASTER:
38266 case PRAGMA_OMP_PARALLEL:
38267 case PRAGMA_OMP_SECTIONS:
38268 case PRAGMA_OMP_SIMD:
38269 case PRAGMA_OMP_SINGLE:
38270 case PRAGMA_OMP_TASK:
38271 case PRAGMA_OMP_TASKGROUP:
38272 case PRAGMA_OMP_TASKLOOP:
38273 case PRAGMA_OMP_TEAMS:
38274 if (context != pragma_stmt && context != pragma_compound)
38275 goto bad_stmt;
38276 stmt = push_omp_privatization_clauses (false);
38277 cp_parser_omp_construct (parser, pragma_tok, if_p);
38278 pop_omp_privatization_clauses (stmt);
38279 return true;
38281 case PRAGMA_OMP_ORDERED:
38282 if (context != pragma_stmt && context != pragma_compound)
38283 goto bad_stmt;
38284 stmt = push_omp_privatization_clauses (false);
38285 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38286 pop_omp_privatization_clauses (stmt);
38287 return ret;
38289 case PRAGMA_OMP_TARGET:
38290 if (context != pragma_stmt && context != pragma_compound)
38291 goto bad_stmt;
38292 stmt = push_omp_privatization_clauses (false);
38293 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38294 pop_omp_privatization_clauses (stmt);
38295 return ret;
38297 case PRAGMA_OMP_END_DECLARE_TARGET:
38298 cp_parser_omp_end_declare_target (parser, pragma_tok);
38299 return false;
38301 case PRAGMA_OMP_SECTION:
38302 error_at (pragma_tok->location,
38303 "%<#pragma omp section%> may only be used in "
38304 "%<#pragma omp sections%> construct");
38305 break;
38307 case PRAGMA_IVDEP:
38309 if (context == pragma_external)
38311 error_at (pragma_tok->location,
38312 "%<#pragma GCC ivdep%> must be inside a function");
38313 break;
38315 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38316 cp_token *tok;
38317 tok = cp_lexer_peek_token (the_parser->lexer);
38318 if (tok->type != CPP_KEYWORD
38319 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38320 && tok->keyword != RID_DO))
38322 cp_parser_error (parser, "for, while or do statement expected");
38323 return false;
38325 cp_parser_iteration_statement (parser, if_p, true);
38326 return true;
38329 case PRAGMA_CILK_SIMD:
38330 if (context == pragma_external)
38332 error_at (pragma_tok->location,
38333 "%<#pragma simd%> must be inside a function");
38334 break;
38336 stmt = push_omp_privatization_clauses (false);
38337 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38338 pop_omp_privatization_clauses (stmt);
38339 return true;
38341 case PRAGMA_CILK_GRAINSIZE:
38342 if (context == pragma_external)
38344 error_at (pragma_tok->location,
38345 "%<#pragma cilk grainsize%> must be inside a function");
38346 break;
38349 /* Ignore the pragma if Cilk Plus is not enabled. */
38350 if (flag_cilkplus)
38352 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38353 return true;
38355 else
38357 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38358 "%<#pragma cilk grainsize%>");
38359 break;
38362 default:
38363 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38364 c_invoke_pragma_handler (id);
38365 break;
38367 bad_stmt:
38368 cp_parser_error (parser, "expected declaration specifiers");
38369 break;
38372 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38373 return false;
38376 /* The interface the pragma parsers have to the lexer. */
38378 enum cpp_ttype
38379 pragma_lex (tree *value, location_t *loc)
38381 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38382 enum cpp_ttype ret = tok->type;
38384 *value = tok->u.value;
38385 if (loc)
38386 *loc = tok->location;
38388 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38389 ret = CPP_EOF;
38390 else if (ret == CPP_STRING)
38391 *value = cp_parser_string_literal (the_parser, false, false);
38392 else
38394 if (ret == CPP_KEYWORD)
38395 ret = CPP_NAME;
38396 cp_lexer_consume_token (the_parser->lexer);
38399 return ret;
38403 /* External interface. */
38405 /* Parse one entire translation unit. */
38407 void
38408 c_parse_file (void)
38410 static bool already_called = false;
38412 if (already_called)
38413 fatal_error (input_location,
38414 "inter-module optimizations not implemented for C++");
38415 already_called = true;
38417 the_parser = cp_parser_new ();
38418 push_deferring_access_checks (flag_access_control
38419 ? dk_no_deferred : dk_no_check);
38420 cp_parser_translation_unit (the_parser);
38421 the_parser = NULL;
38424 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38425 vectorlength clause:
38426 Syntax:
38427 vectorlength ( constant-expression ) */
38429 static tree
38430 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38431 bool is_simd_fn)
38433 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38434 tree expr;
38435 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38436 safelen clause. Thus, vectorlength is represented as OMP 4.0
38437 safelen. For SIMD-enabled function it is represented by OMP 4.0
38438 simdlen. */
38439 if (!is_simd_fn)
38440 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38441 loc);
38442 else
38443 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38444 loc);
38446 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38447 return error_mark_node;
38449 expr = cp_parser_constant_expression (parser);
38450 expr = maybe_constant_value (expr);
38452 /* If expr == error_mark_node, then don't emit any errors nor
38453 create a clause. if any of the above functions returns
38454 error mark node then they would have emitted an error message. */
38455 if (expr == error_mark_node)
38457 else if (!TREE_TYPE (expr)
38458 || !TREE_CONSTANT (expr)
38459 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38460 error_at (loc, "vectorlength must be an integer constant");
38461 else if (TREE_CONSTANT (expr)
38462 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38463 error_at (loc, "vectorlength must be a power of 2");
38464 else
38466 tree c;
38467 if (!is_simd_fn)
38469 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38470 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38471 OMP_CLAUSE_CHAIN (c) = clauses;
38472 clauses = c;
38474 else
38476 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38477 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38478 OMP_CLAUSE_CHAIN (c) = clauses;
38479 clauses = c;
38483 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38484 return error_mark_node;
38485 return clauses;
38488 /* Handles the Cilk Plus #pragma simd linear clause.
38489 Syntax:
38490 linear ( simd-linear-variable-list )
38492 simd-linear-variable-list:
38493 simd-linear-variable
38494 simd-linear-variable-list , simd-linear-variable
38496 simd-linear-variable:
38497 id-expression
38498 id-expression : simd-linear-step
38500 simd-linear-step:
38501 conditional-expression */
38503 static tree
38504 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38506 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38508 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38509 return clauses;
38510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38512 cp_parser_error (parser, "expected identifier");
38513 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38514 return error_mark_node;
38517 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38518 parser->colon_corrects_to_scope_p = false;
38519 while (1)
38521 cp_token *token = cp_lexer_peek_token (parser->lexer);
38522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38524 cp_parser_error (parser, "expected variable-name");
38525 clauses = error_mark_node;
38526 break;
38529 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38530 false, false);
38531 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38532 token->location);
38533 if (decl == error_mark_node)
38535 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38536 token->location);
38537 clauses = error_mark_node;
38539 else
38541 tree e = NULL_TREE;
38542 tree step_size = integer_one_node;
38544 /* If present, parse the linear step. Otherwise, assume the default
38545 value of 1. */
38546 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38548 cp_lexer_consume_token (parser->lexer);
38550 e = cp_parser_assignment_expression (parser);
38551 e = maybe_constant_value (e);
38553 if (e == error_mark_node)
38555 /* If an error has occurred, then the whole pragma is
38556 considered ill-formed. Thus, no reason to keep
38557 parsing. */
38558 clauses = error_mark_node;
38559 break;
38561 else if (type_dependent_expression_p (e)
38562 || value_dependent_expression_p (e)
38563 || (TREE_TYPE (e)
38564 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38565 && (TREE_CONSTANT (e)
38566 || DECL_P (e))))
38567 step_size = e;
38568 else
38569 cp_parser_error (parser,
38570 "step size must be an integer constant "
38571 "expression or an integer variable");
38574 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38575 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38576 OMP_CLAUSE_DECL (l) = decl;
38577 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38578 OMP_CLAUSE_CHAIN (l) = clauses;
38579 clauses = l;
38581 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38582 cp_lexer_consume_token (parser->lexer);
38583 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38584 break;
38585 else
38587 error_at (cp_lexer_peek_token (parser->lexer)->location,
38588 "expected %<,%> or %<)%> after %qE", decl);
38589 clauses = error_mark_node;
38590 break;
38593 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38594 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38595 return clauses;
38598 /* Returns the name of the next clause. If the clause is not
38599 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38600 token is not consumed. Otherwise, the appropriate enum from the
38601 pragma_simd_clause is returned and the token is consumed. */
38603 static pragma_omp_clause
38604 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38606 pragma_omp_clause clause_type;
38607 cp_token *token = cp_lexer_peek_token (parser->lexer);
38609 if (token->keyword == RID_PRIVATE)
38610 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38611 else if (!token->u.value || token->type != CPP_NAME)
38612 return PRAGMA_CILK_CLAUSE_NONE;
38613 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "vectorlength"))
38614 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38615 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "linear"))
38616 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38617 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "firstprivate"))
38618 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38619 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "lastprivate"))
38620 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38621 else if (!strcmp (IDENTIFIER_POINTER (token->u.value), "reduction"))
38622 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38623 else
38624 return PRAGMA_CILK_CLAUSE_NONE;
38626 cp_lexer_consume_token (parser->lexer);
38627 return clause_type;
38630 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38632 static tree
38633 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38635 tree clauses = NULL_TREE;
38637 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38638 && clauses != error_mark_node)
38640 pragma_omp_clause c_kind;
38641 c_kind = cp_parser_cilk_simd_clause_name (parser);
38642 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38643 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38644 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38645 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38646 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38647 /* Use the OpenMP 4.0 equivalent function. */
38648 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38649 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38650 /* Use the OpenMP 4.0 equivalent function. */
38651 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38652 clauses);
38653 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38654 /* Use the OMP 4.0 equivalent function. */
38655 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38656 clauses);
38657 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38658 /* Use the OMP 4.0 equivalent function. */
38659 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38660 else
38662 clauses = error_mark_node;
38663 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38664 break;
38668 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38670 if (clauses == error_mark_node)
38671 return error_mark_node;
38672 else
38673 return finish_omp_clauses (clauses, C_ORT_CILK);
38676 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38678 static void
38679 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38681 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38683 if (clauses == error_mark_node)
38684 return;
38686 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38688 error_at (cp_lexer_peek_token (parser->lexer)->location,
38689 "for statement expected");
38690 return;
38693 tree sb = begin_omp_structured_block ();
38694 int save = cp_parser_begin_omp_structured_block (parser);
38695 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38696 if (ret)
38697 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38698 cp_parser_end_omp_structured_block (parser, save);
38699 add_stmt (finish_omp_structured_block (sb));
38702 /* Main entry-point for parsing Cilk Plus _Cilk_for
38703 loops. The return value is error_mark_node
38704 when errors happen and CILK_FOR tree on success. */
38706 static tree
38707 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38709 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38710 gcc_unreachable ();
38712 tree sb = begin_omp_structured_block ();
38713 int save = cp_parser_begin_omp_structured_block (parser);
38715 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38716 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38717 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38718 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38720 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38721 if (ret)
38722 cpp_validate_cilk_plus_loop (ret);
38723 else
38724 ret = error_mark_node;
38726 cp_parser_end_omp_structured_block (parser, save);
38727 add_stmt (finish_omp_structured_block (sb));
38728 return ret;
38731 /* Create an identifier for a generic parameter type (a synthesized
38732 template parameter implied by `auto' or a concept identifier). */
38734 static GTY(()) int generic_parm_count;
38735 static tree
38736 make_generic_type_name ()
38738 char buf[32];
38739 sprintf (buf, "auto:%d", ++generic_parm_count);
38740 return get_identifier (buf);
38743 /* Predicate that behaves as is_auto_or_concept but matches the parent
38744 node of the generic type rather than the generic type itself. This
38745 allows for type transformation in add_implicit_template_parms. */
38747 static inline bool
38748 tree_type_is_auto_or_concept (const_tree t)
38750 return TREE_TYPE (t) && is_auto_or_concept (TREE_TYPE (t));
38753 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38754 (creating a new template parameter list if necessary). Returns the newly
38755 created template type parm. */
38757 static tree
38758 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38760 gcc_assert (current_binding_level->kind == sk_function_parms);
38762 /* Before committing to modifying any scope, if we're in an
38763 implicit template scope, and we're trying to synthesize a
38764 constrained parameter, try to find a previous parameter with
38765 the same name. This is the same-type rule for abbreviated
38766 function templates.
38768 NOTE: We can generate implicit parameters when tentatively
38769 parsing a nested name specifier, only to reject that parse
38770 later. However, matching the same template-id as part of a
38771 direct-declarator should generate an identical template
38772 parameter, so this rule will merge them. */
38773 if (parser->implicit_template_scope && constr)
38775 tree t = parser->implicit_template_parms;
38776 while (t)
38778 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38780 tree d = TREE_VALUE (t);
38781 if (TREE_CODE (d) == PARM_DECL)
38782 /* Return the TEMPLATE_PARM_INDEX. */
38783 d = DECL_INITIAL (d);
38784 return d;
38786 t = TREE_CHAIN (t);
38790 /* We are either continuing a function template that already contains implicit
38791 template parameters, creating a new fully-implicit function template, or
38792 extending an existing explicit function template with implicit template
38793 parameters. */
38795 cp_binding_level *const entry_scope = current_binding_level;
38797 bool become_template = false;
38798 cp_binding_level *parent_scope = 0;
38800 if (parser->implicit_template_scope)
38802 gcc_assert (parser->implicit_template_parms);
38804 current_binding_level = parser->implicit_template_scope;
38806 else
38808 /* Roll back to the existing template parameter scope (in the case of
38809 extending an explicit function template) or introduce a new template
38810 parameter scope ahead of the function parameter scope (or class scope
38811 in the case of out-of-line member definitions). The function scope is
38812 added back after template parameter synthesis below. */
38814 cp_binding_level *scope = entry_scope;
38816 while (scope->kind == sk_function_parms)
38818 parent_scope = scope;
38819 scope = scope->level_chain;
38821 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38823 /* If not defining a class, then any class scope is a scope level in
38824 an out-of-line member definition. In this case simply wind back
38825 beyond the first such scope to inject the template parameter list.
38826 Otherwise wind back to the class being defined. The latter can
38827 occur in class member friend declarations such as:
38829 class A {
38830 void foo (auto);
38832 class B {
38833 friend void A::foo (auto);
38836 The template parameter list synthesized for the friend declaration
38837 must be injected in the scope of 'B'. This can also occur in
38838 erroneous cases such as:
38840 struct A {
38841 struct B {
38842 void foo (auto);
38844 void B::foo (auto) {}
38847 Here the attempted definition of 'B::foo' within 'A' is ill-formed
38848 but, nevertheless, the template parameter list synthesized for the
38849 declarator should be injected into the scope of 'A' as if the
38850 ill-formed template was specified explicitly. */
38852 while (scope->kind == sk_class && !scope->defining_class_p)
38854 parent_scope = scope;
38855 scope = scope->level_chain;
38859 current_binding_level = scope;
38861 if (scope->kind != sk_template_parms
38862 || !function_being_declared_is_template_p (parser))
38864 /* Introduce a new template parameter list for implicit template
38865 parameters. */
38867 become_template = true;
38869 parser->implicit_template_scope
38870 = begin_scope (sk_template_parms, NULL);
38872 ++processing_template_decl;
38874 parser->fully_implicit_function_template_p = true;
38875 ++parser->num_template_parameter_lists;
38877 else
38879 /* Synthesize implicit template parameters at the end of the explicit
38880 template parameter list. */
38882 gcc_assert (current_template_parms);
38884 parser->implicit_template_scope = scope;
38886 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38887 parser->implicit_template_parms
38888 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
38892 /* Synthesize a new template parameter and track the current template
38893 parameter chain with implicit_template_parms. */
38895 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
38896 tree synth_id = make_generic_type_name ();
38897 tree synth_tmpl_parm;
38898 bool non_type = false;
38900 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
38901 synth_tmpl_parm
38902 = finish_template_type_parm (class_type_node, synth_id);
38903 else if (TREE_CODE (proto) == TEMPLATE_DECL)
38904 synth_tmpl_parm
38905 = finish_constrained_template_template_parm (proto, synth_id);
38906 else
38908 synth_tmpl_parm = copy_decl (proto);
38909 DECL_NAME (synth_tmpl_parm) = synth_id;
38910 non_type = true;
38913 // Attach the constraint to the parm before processing.
38914 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
38915 TREE_TYPE (node) = constr;
38916 tree new_parm
38917 = process_template_parm (parser->implicit_template_parms,
38918 input_location,
38919 node,
38920 /*non_type=*/non_type,
38921 /*param_pack=*/false);
38923 // Chain the new parameter to the list of implicit parameters.
38924 if (parser->implicit_template_parms)
38925 parser->implicit_template_parms
38926 = TREE_CHAIN (parser->implicit_template_parms);
38927 else
38928 parser->implicit_template_parms = new_parm;
38930 tree new_decl = getdecls ();
38931 if (non_type)
38932 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
38933 new_decl = DECL_INITIAL (new_decl);
38935 /* If creating a fully implicit function template, start the new implicit
38936 template parameter list with this synthesized type, otherwise grow the
38937 current template parameter list. */
38939 if (become_template)
38941 parent_scope->level_chain = current_binding_level;
38943 tree new_parms = make_tree_vec (1);
38944 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
38945 current_template_parms = tree_cons (size_int (processing_template_decl),
38946 new_parms, current_template_parms);
38948 else
38950 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
38951 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
38952 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
38953 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
38956 // If the new parameter was constrained, we need to add that to the
38957 // constraints in the template parameter list.
38958 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
38960 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
38961 reqs = conjoin_constraints (reqs, req);
38962 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
38965 current_binding_level = entry_scope;
38967 return new_decl;
38970 /* Finish the declaration of a fully implicit function template. Such a
38971 template has no explicit template parameter list so has not been through the
38972 normal template head and tail processing. synthesize_implicit_template_parm
38973 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
38974 provided if the declaration is a class member such that its template
38975 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
38976 form is returned. Otherwise NULL_TREE is returned. */
38978 static tree
38979 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
38981 gcc_assert (parser->fully_implicit_function_template_p);
38983 if (member_decl_opt && member_decl_opt != error_mark_node
38984 && DECL_VIRTUAL_P (member_decl_opt))
38986 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
38987 "implicit templates may not be %<virtual%>");
38988 DECL_VIRTUAL_P (member_decl_opt) = false;
38991 if (member_decl_opt)
38992 member_decl_opt = finish_member_template_decl (member_decl_opt);
38993 end_template_decl ();
38995 parser->fully_implicit_function_template_p = false;
38996 --parser->num_template_parameter_lists;
38998 return member_decl_opt;
39001 #include "gt-cp-parser.h"