PR bootstrap/81216
[official-gcc.git] / gcc / cp / parser.c
blob573b97a658f57075c499977cb4dda725c06e20d2
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 (IDENTIFIER_KEYWORD_P (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
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 (TREE_CODE (type) == TYPE_DECL)
2987 type = TREE_TYPE (type);
2988 if (TYPE_P (type) && !template_placeholder_p (type))
2989 error_at (location, "%qT is not a template", type);
2990 else if (identifier_p (type))
2992 if (tag_type != none_type)
2993 error_at (location, "%qE is not a class template", type);
2994 else
2995 error_at (location, "%qE is not a template", type);
2997 else
2998 error_at (location, "invalid template-id");
2999 /* Remember the location of the invalid "<". */
3000 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3001 start = cp_lexer_token_position (parser->lexer, true);
3002 /* Consume the "<". */
3003 cp_lexer_consume_token (parser->lexer);
3004 /* Parse the template arguments. */
3005 cp_parser_enclosed_template_argument_list (parser);
3006 /* Permanently remove the invalid template arguments so that
3007 this error message is not issued again. */
3008 if (start)
3009 cp_lexer_purge_tokens_after (parser->lexer, start);
3013 /* If parsing an integral constant-expression, issue an error message
3014 about the fact that THING appeared and return true. Otherwise,
3015 return false. In either case, set
3016 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
3018 static bool
3019 cp_parser_non_integral_constant_expression (cp_parser *parser,
3020 non_integral_constant thing)
3022 parser->non_integral_constant_expression_p = true;
3023 if (parser->integral_constant_expression_p)
3025 if (!parser->allow_non_integral_constant_expression_p)
3027 const char *msg = NULL;
3028 switch (thing)
3030 case NIC_FLOAT:
3031 pedwarn (input_location, OPT_Wpedantic,
3032 "ISO C++ forbids using a floating-point literal "
3033 "in a constant-expression");
3034 return true;
3035 case NIC_CAST:
3036 error ("a cast to a type other than an integral or "
3037 "enumeration type cannot appear in a "
3038 "constant-expression");
3039 return true;
3040 case NIC_TYPEID:
3041 error ("%<typeid%> operator "
3042 "cannot appear in a constant-expression");
3043 return true;
3044 case NIC_NCC:
3045 error ("non-constant compound literals "
3046 "cannot appear in a constant-expression");
3047 return true;
3048 case NIC_FUNC_CALL:
3049 error ("a function call "
3050 "cannot appear in a constant-expression");
3051 return true;
3052 case NIC_INC:
3053 error ("an increment "
3054 "cannot appear in a constant-expression");
3055 return true;
3056 case NIC_DEC:
3057 error ("an decrement "
3058 "cannot appear in a constant-expression");
3059 return true;
3060 case NIC_ARRAY_REF:
3061 error ("an array reference "
3062 "cannot appear in a constant-expression");
3063 return true;
3064 case NIC_ADDR_LABEL:
3065 error ("the address of a label "
3066 "cannot appear in a constant-expression");
3067 return true;
3068 case NIC_OVERLOADED:
3069 error ("calls to overloaded operators "
3070 "cannot appear in a constant-expression");
3071 return true;
3072 case NIC_ASSIGNMENT:
3073 error ("an assignment cannot appear in a constant-expression");
3074 return true;
3075 case NIC_COMMA:
3076 error ("a comma operator "
3077 "cannot appear in a constant-expression");
3078 return true;
3079 case NIC_CONSTRUCTOR:
3080 error ("a call to a constructor "
3081 "cannot appear in a constant-expression");
3082 return true;
3083 case NIC_TRANSACTION:
3084 error ("a transaction expression "
3085 "cannot appear in a constant-expression");
3086 return true;
3087 case NIC_THIS:
3088 msg = "this";
3089 break;
3090 case NIC_FUNC_NAME:
3091 msg = "__FUNCTION__";
3092 break;
3093 case NIC_PRETTY_FUNC:
3094 msg = "__PRETTY_FUNCTION__";
3095 break;
3096 case NIC_C99_FUNC:
3097 msg = "__func__";
3098 break;
3099 case NIC_VA_ARG:
3100 msg = "va_arg";
3101 break;
3102 case NIC_ARROW:
3103 msg = "->";
3104 break;
3105 case NIC_POINT:
3106 msg = ".";
3107 break;
3108 case NIC_STAR:
3109 msg = "*";
3110 break;
3111 case NIC_ADDR:
3112 msg = "&";
3113 break;
3114 case NIC_PREINCREMENT:
3115 msg = "++";
3116 break;
3117 case NIC_PREDECREMENT:
3118 msg = "--";
3119 break;
3120 case NIC_NEW:
3121 msg = "new";
3122 break;
3123 case NIC_DEL:
3124 msg = "delete";
3125 break;
3126 default:
3127 gcc_unreachable ();
3129 if (msg)
3130 error ("%qs cannot appear in a constant-expression", msg);
3131 return true;
3134 return false;
3137 /* Emit a diagnostic for an invalid type name. This function commits
3138 to the current active tentative parse, if any. (Otherwise, the
3139 problematic construct might be encountered again later, resulting
3140 in duplicate error messages.) LOCATION is the location of ID. */
3142 static void
3143 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree id,
3144 location_t location)
3146 tree decl, ambiguous_decls;
3147 cp_parser_commit_to_tentative_parse (parser);
3148 /* Try to lookup the identifier. */
3149 decl = cp_parser_lookup_name (parser, id, none_type,
3150 /*is_template=*/false,
3151 /*is_namespace=*/false,
3152 /*check_dependency=*/true,
3153 &ambiguous_decls, location);
3154 if (ambiguous_decls)
3155 /* If the lookup was ambiguous, an error will already have
3156 been issued. */
3157 return;
3158 /* If the lookup found a template-name, it means that the user forgot
3159 to specify an argument list. Emit a useful error message. */
3160 if (DECL_TYPE_TEMPLATE_P (decl))
3162 error_at (location,
3163 "invalid use of template-name %qE without an argument list",
3164 decl);
3165 if (DECL_CLASS_TEMPLATE_P (decl) && cxx_dialect < cxx1z)
3166 inform (location, "class template argument deduction is only available "
3167 "with -std=c++1z or -std=gnu++1z");
3168 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3170 else if (TREE_CODE (id) == BIT_NOT_EXPR)
3171 error_at (location, "invalid use of destructor %qD as a type", id);
3172 else if (TREE_CODE (decl) == TYPE_DECL)
3173 /* Something like 'unsigned A a;' */
3174 error_at (location, "invalid combination of multiple type-specifiers");
3175 else if (!parser->scope)
3177 /* Issue an error message. */
3178 const char *suggestion = NULL;
3179 if (TREE_CODE (id) == IDENTIFIER_NODE)
3180 suggestion = lookup_name_fuzzy (id, FUZZY_LOOKUP_TYPENAME);
3181 if (suggestion)
3183 gcc_rich_location richloc (location);
3184 richloc.add_fixit_replace (suggestion);
3185 error_at_rich_loc (&richloc,
3186 "%qE does not name a type; did you mean %qs?",
3187 id, suggestion);
3189 else
3190 error_at (location, "%qE does not name a type", id);
3191 /* If we're in a template class, it's possible that the user was
3192 referring to a type from a base class. For example:
3194 template <typename T> struct A { typedef T X; };
3195 template <typename T> struct B : public A<T> { X x; };
3197 The user should have said "typename A<T>::X". */
3198 if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_CONSTEXPR])
3199 inform (location, "C++11 %<constexpr%> only available with "
3200 "-std=c++11 or -std=gnu++11");
3201 else if (cxx_dialect < cxx11 && id == ridpointers[(int)RID_NOEXCEPT])
3202 inform (location, "C++11 %<noexcept%> only available with "
3203 "-std=c++11 or -std=gnu++11");
3204 else if (cxx_dialect < cxx11
3205 && TREE_CODE (id) == IDENTIFIER_NODE
3206 && id_equal (id, "thread_local"))
3207 inform (location, "C++11 %<thread_local%> only available with "
3208 "-std=c++11 or -std=gnu++11");
3209 else if (!flag_concepts && id == ridpointers[(int)RID_CONCEPT])
3210 inform (location, "%<concept%> only available with -fconcepts");
3211 else if (processing_template_decl && current_class_type
3212 && TYPE_BINFO (current_class_type))
3214 tree b;
3216 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
3218 b = TREE_CHAIN (b))
3220 tree base_type = BINFO_TYPE (b);
3221 if (CLASS_TYPE_P (base_type)
3222 && dependent_type_p (base_type))
3224 tree field;
3225 /* Go from a particular instantiation of the
3226 template (which will have an empty TYPE_FIELDs),
3227 to the main version. */
3228 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
3229 for (field = TYPE_FIELDS (base_type);
3230 field;
3231 field = DECL_CHAIN (field))
3232 if (TREE_CODE (field) == TYPE_DECL
3233 && DECL_NAME (field) == id)
3235 inform (location,
3236 "(perhaps %<typename %T::%E%> was intended)",
3237 BINFO_TYPE (b), id);
3238 break;
3240 if (field)
3241 break;
3246 /* Here we diagnose qualified-ids where the scope is actually correct,
3247 but the identifier does not resolve to a valid type name. */
3248 else if (parser->scope != error_mark_node)
3250 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
3252 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3253 error_at (location_of (id),
3254 "%qE in namespace %qE does not name a template type",
3255 id, parser->scope);
3256 else
3257 error_at (location_of (id),
3258 "%qE in namespace %qE does not name a type",
3259 id, parser->scope);
3260 if (DECL_P (decl))
3261 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3263 else if (CLASS_TYPE_P (parser->scope)
3264 && constructor_name_p (id, parser->scope))
3266 /* A<T>::A<T>() */
3267 error_at (location, "%<%T::%E%> names the constructor, not"
3268 " the type", parser->scope, id);
3269 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3270 error_at (location, "and %qT has no template constructors",
3271 parser->scope);
3273 else if (TYPE_P (parser->scope)
3274 && dependent_scope_p (parser->scope))
3276 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3277 error_at (location,
3278 "need %<typename%> before %<%T::%D::%E%> because "
3279 "%<%T::%D%> is a dependent scope",
3280 TYPE_CONTEXT (parser->scope),
3281 TYPENAME_TYPE_FULLNAME (parser->scope),
3283 TYPE_CONTEXT (parser->scope),
3284 TYPENAME_TYPE_FULLNAME (parser->scope));
3285 else
3286 error_at (location, "need %<typename%> before %<%T::%E%> because "
3287 "%qT is a dependent scope",
3288 parser->scope, id, parser->scope);
3290 else if (TYPE_P (parser->scope))
3292 if (!COMPLETE_TYPE_P (parser->scope))
3293 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3294 parser->scope);
3295 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3296 error_at (location_of (id),
3297 "%qE in %q#T does not name a template type",
3298 id, parser->scope);
3299 else
3300 error_at (location_of (id),
3301 "%qE in %q#T does not name a type",
3302 id, parser->scope);
3303 if (DECL_P (decl))
3304 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3306 else
3307 gcc_unreachable ();
3311 /* Check for a common situation where a type-name should be present,
3312 but is not, and issue a sensible error message. Returns true if an
3313 invalid type-name was detected.
3315 The situation handled by this function are variable declarations of the
3316 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3317 Usually, `ID' should name a type, but if we got here it means that it
3318 does not. We try to emit the best possible error message depending on
3319 how exactly the id-expression looks like. */
3321 static bool
3322 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3324 tree id;
3325 cp_token *token = cp_lexer_peek_token (parser->lexer);
3327 /* Avoid duplicate error about ambiguous lookup. */
3328 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3330 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3331 if (next->type == CPP_NAME && next->error_reported)
3332 goto out;
3335 cp_parser_parse_tentatively (parser);
3336 id = cp_parser_id_expression (parser,
3337 /*template_keyword_p=*/false,
3338 /*check_dependency_p=*/true,
3339 /*template_p=*/NULL,
3340 /*declarator_p=*/true,
3341 /*optional_p=*/false);
3342 /* If the next token is a (, this is a function with no explicit return
3343 type, i.e. constructor, destructor or conversion op. */
3344 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3345 || TREE_CODE (id) == TYPE_DECL)
3347 cp_parser_abort_tentative_parse (parser);
3348 return false;
3350 if (!cp_parser_parse_definitely (parser))
3351 return false;
3353 /* Emit a diagnostic for the invalid type. */
3354 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3355 out:
3356 /* If we aren't in the middle of a declarator (i.e. in a
3357 parameter-declaration-clause), skip to the end of the declaration;
3358 there's no point in trying to process it. */
3359 if (!parser->in_declarator_p)
3360 cp_parser_skip_to_end_of_block_or_statement (parser);
3361 return true;
3364 /* Consume tokens up to, and including, the next non-nested closing `)'.
3365 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3366 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3367 found an unnested token of that type. */
3369 static int
3370 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3371 bool recovering,
3372 cpp_ttype or_ttype,
3373 bool consume_paren)
3375 unsigned paren_depth = 0;
3376 unsigned brace_depth = 0;
3377 unsigned square_depth = 0;
3379 if (recovering && or_ttype == CPP_EOF
3380 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3381 return 0;
3383 while (true)
3385 cp_token * token = cp_lexer_peek_token (parser->lexer);
3387 /* Have we found what we're looking for before the closing paren? */
3388 if (token->type == or_ttype && or_ttype != CPP_EOF
3389 && !brace_depth && !paren_depth && !square_depth)
3390 return -1;
3392 switch (token->type)
3394 case CPP_EOF:
3395 case CPP_PRAGMA_EOL:
3396 /* If we've run out of tokens, then there is no closing `)'. */
3397 return 0;
3399 /* This is good for lambda expression capture-lists. */
3400 case CPP_OPEN_SQUARE:
3401 ++square_depth;
3402 break;
3403 case CPP_CLOSE_SQUARE:
3404 if (!square_depth--)
3405 return 0;
3406 break;
3408 case CPP_SEMICOLON:
3409 /* This matches the processing in skip_to_end_of_statement. */
3410 if (!brace_depth)
3411 return 0;
3412 break;
3414 case CPP_OPEN_BRACE:
3415 ++brace_depth;
3416 break;
3417 case CPP_CLOSE_BRACE:
3418 if (!brace_depth--)
3419 return 0;
3420 break;
3422 case CPP_OPEN_PAREN:
3423 if (!brace_depth)
3424 ++paren_depth;
3425 break;
3427 case CPP_CLOSE_PAREN:
3428 if (!brace_depth && !paren_depth--)
3430 if (consume_paren)
3431 cp_lexer_consume_token (parser->lexer);
3432 return 1;
3434 break;
3436 default:
3437 break;
3440 /* Consume the token. */
3441 cp_lexer_consume_token (parser->lexer);
3445 /* Consume tokens up to, and including, the next non-nested closing `)'.
3446 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3447 are doing error recovery. Returns -1 if OR_COMMA is true and we
3448 found an unnested token of that type. */
3450 static int
3451 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3452 bool recovering,
3453 bool or_comma,
3454 bool consume_paren)
3456 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3457 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3458 ttype, consume_paren);
3461 /* Consume tokens until we reach the end of the current statement.
3462 Normally, that will be just before consuming a `;'. However, if a
3463 non-nested `}' comes first, then we stop before consuming that. */
3465 static void
3466 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3468 unsigned nesting_depth = 0;
3470 /* Unwind generic function template scope if necessary. */
3471 if (parser->fully_implicit_function_template_p)
3472 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3474 while (true)
3476 cp_token *token = cp_lexer_peek_token (parser->lexer);
3478 switch (token->type)
3480 case CPP_EOF:
3481 case CPP_PRAGMA_EOL:
3482 /* If we've run out of tokens, stop. */
3483 return;
3485 case CPP_SEMICOLON:
3486 /* If the next token is a `;', we have reached the end of the
3487 statement. */
3488 if (!nesting_depth)
3489 return;
3490 break;
3492 case CPP_CLOSE_BRACE:
3493 /* If this is a non-nested '}', stop before consuming it.
3494 That way, when confronted with something like:
3496 { 3 + }
3498 we stop before consuming the closing '}', even though we
3499 have not yet reached a `;'. */
3500 if (nesting_depth == 0)
3501 return;
3503 /* If it is the closing '}' for a block that we have
3504 scanned, stop -- but only after consuming the token.
3505 That way given:
3507 void f g () { ... }
3508 typedef int I;
3510 we will stop after the body of the erroneously declared
3511 function, but before consuming the following `typedef'
3512 declaration. */
3513 if (--nesting_depth == 0)
3515 cp_lexer_consume_token (parser->lexer);
3516 return;
3518 break;
3520 case CPP_OPEN_BRACE:
3521 ++nesting_depth;
3522 break;
3524 default:
3525 break;
3528 /* Consume the token. */
3529 cp_lexer_consume_token (parser->lexer);
3533 /* This function is called at the end of a statement or declaration.
3534 If the next token is a semicolon, it is consumed; otherwise, error
3535 recovery is attempted. */
3537 static void
3538 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3540 /* Look for the trailing `;'. */
3541 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3543 /* If there is additional (erroneous) input, skip to the end of
3544 the statement. */
3545 cp_parser_skip_to_end_of_statement (parser);
3546 /* If the next token is now a `;', consume it. */
3547 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3548 cp_lexer_consume_token (parser->lexer);
3552 /* Skip tokens until we have consumed an entire block, or until we
3553 have consumed a non-nested `;'. */
3555 static void
3556 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3558 int nesting_depth = 0;
3560 /* Unwind generic function template scope if necessary. */
3561 if (parser->fully_implicit_function_template_p)
3562 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3564 while (nesting_depth >= 0)
3566 cp_token *token = cp_lexer_peek_token (parser->lexer);
3568 switch (token->type)
3570 case CPP_EOF:
3571 case CPP_PRAGMA_EOL:
3572 /* If we've run out of tokens, stop. */
3573 return;
3575 case CPP_SEMICOLON:
3576 /* Stop if this is an unnested ';'. */
3577 if (!nesting_depth)
3578 nesting_depth = -1;
3579 break;
3581 case CPP_CLOSE_BRACE:
3582 /* Stop if this is an unnested '}', or closes the outermost
3583 nesting level. */
3584 nesting_depth--;
3585 if (nesting_depth < 0)
3586 return;
3587 if (!nesting_depth)
3588 nesting_depth = -1;
3589 break;
3591 case CPP_OPEN_BRACE:
3592 /* Nest. */
3593 nesting_depth++;
3594 break;
3596 default:
3597 break;
3600 /* Consume the token. */
3601 cp_lexer_consume_token (parser->lexer);
3605 /* Skip tokens until a non-nested closing curly brace is the next
3606 token, or there are no more tokens. Return true in the first case,
3607 false otherwise. */
3609 static bool
3610 cp_parser_skip_to_closing_brace (cp_parser *parser)
3612 unsigned nesting_depth = 0;
3614 while (true)
3616 cp_token *token = cp_lexer_peek_token (parser->lexer);
3618 switch (token->type)
3620 case CPP_EOF:
3621 case CPP_PRAGMA_EOL:
3622 /* If we've run out of tokens, stop. */
3623 return false;
3625 case CPP_CLOSE_BRACE:
3626 /* If the next token is a non-nested `}', then we have reached
3627 the end of the current block. */
3628 if (nesting_depth-- == 0)
3629 return true;
3630 break;
3632 case CPP_OPEN_BRACE:
3633 /* If it the next token is a `{', then we are entering a new
3634 block. Consume the entire block. */
3635 ++nesting_depth;
3636 break;
3638 default:
3639 break;
3642 /* Consume the token. */
3643 cp_lexer_consume_token (parser->lexer);
3647 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3648 parameter is the PRAGMA token, allowing us to purge the entire pragma
3649 sequence. */
3651 static void
3652 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3654 cp_token *token;
3656 parser->lexer->in_pragma = false;
3659 token = cp_lexer_consume_token (parser->lexer);
3660 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3662 /* Ensure that the pragma is not parsed again. */
3663 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3666 /* Require pragma end of line, resyncing with it as necessary. The
3667 arguments are as for cp_parser_skip_to_pragma_eol. */
3669 static void
3670 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3672 parser->lexer->in_pragma = false;
3673 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3674 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3677 /* This is a simple wrapper around make_typename_type. When the id is
3678 an unresolved identifier node, we can provide a superior diagnostic
3679 using cp_parser_diagnose_invalid_type_name. */
3681 static tree
3682 cp_parser_make_typename_type (cp_parser *parser, tree id,
3683 location_t id_location)
3685 tree result;
3686 if (identifier_p (id))
3688 result = make_typename_type (parser->scope, id, typename_type,
3689 /*complain=*/tf_none);
3690 if (result == error_mark_node)
3691 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3692 return result;
3694 return make_typename_type (parser->scope, id, typename_type, tf_error);
3697 /* This is a wrapper around the
3698 make_{pointer,ptrmem,reference}_declarator functions that decides
3699 which one to call based on the CODE and CLASS_TYPE arguments. The
3700 CODE argument should be one of the values returned by
3701 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3702 appertain to the pointer or reference. */
3704 static cp_declarator *
3705 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3706 cp_cv_quals cv_qualifiers,
3707 cp_declarator *target,
3708 tree attributes)
3710 if (code == ERROR_MARK)
3711 return cp_error_declarator;
3713 if (code == INDIRECT_REF)
3714 if (class_type == NULL_TREE)
3715 return make_pointer_declarator (cv_qualifiers, target, attributes);
3716 else
3717 return make_ptrmem_declarator (cv_qualifiers, class_type,
3718 target, attributes);
3719 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3720 return make_reference_declarator (cv_qualifiers, target,
3721 false, attributes);
3722 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3723 return make_reference_declarator (cv_qualifiers, target,
3724 true, attributes);
3725 gcc_unreachable ();
3728 /* Create a new C++ parser. */
3730 static cp_parser *
3731 cp_parser_new (void)
3733 cp_parser *parser;
3734 cp_lexer *lexer;
3735 unsigned i;
3737 /* cp_lexer_new_main is called before doing GC allocation because
3738 cp_lexer_new_main might load a PCH file. */
3739 lexer = cp_lexer_new_main ();
3741 /* Initialize the binops_by_token so that we can get the tree
3742 directly from the token. */
3743 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3744 binops_by_token[binops[i].token_type] = binops[i];
3746 parser = ggc_cleared_alloc<cp_parser> ();
3747 parser->lexer = lexer;
3748 parser->context = cp_parser_context_new (NULL);
3750 /* For now, we always accept GNU extensions. */
3751 parser->allow_gnu_extensions_p = 1;
3753 /* The `>' token is a greater-than operator, not the end of a
3754 template-id. */
3755 parser->greater_than_is_operator_p = true;
3757 parser->default_arg_ok_p = true;
3759 /* We are not parsing a constant-expression. */
3760 parser->integral_constant_expression_p = false;
3761 parser->allow_non_integral_constant_expression_p = false;
3762 parser->non_integral_constant_expression_p = false;
3764 /* Local variable names are not forbidden. */
3765 parser->local_variables_forbidden_p = false;
3767 /* We are not processing an `extern "C"' declaration. */
3768 parser->in_unbraced_linkage_specification_p = false;
3770 /* We are not processing a declarator. */
3771 parser->in_declarator_p = false;
3773 /* We are not processing a template-argument-list. */
3774 parser->in_template_argument_list_p = false;
3776 /* We are not in an iteration statement. */
3777 parser->in_statement = 0;
3779 /* We are not in a switch statement. */
3780 parser->in_switch_statement_p = false;
3782 /* We are not parsing a type-id inside an expression. */
3783 parser->in_type_id_in_expr_p = false;
3785 /* Declarations aren't implicitly extern "C". */
3786 parser->implicit_extern_c = false;
3788 /* String literals should be translated to the execution character set. */
3789 parser->translate_strings_p = true;
3791 /* We are not parsing a function body. */
3792 parser->in_function_body = false;
3794 /* We can correct until told otherwise. */
3795 parser->colon_corrects_to_scope_p = true;
3797 /* The unparsed function queue is empty. */
3798 push_unparsed_function_queues (parser);
3800 /* There are no classes being defined. */
3801 parser->num_classes_being_defined = 0;
3803 /* No template parameters apply. */
3804 parser->num_template_parameter_lists = 0;
3806 /* Special parsing data structures. */
3807 parser->omp_declare_simd = NULL;
3808 parser->cilk_simd_fn_info = NULL;
3809 parser->oacc_routine = NULL;
3811 /* Not declaring an implicit function template. */
3812 parser->auto_is_implicit_function_template_parm_p = false;
3813 parser->fully_implicit_function_template_p = false;
3814 parser->implicit_template_parms = 0;
3815 parser->implicit_template_scope = 0;
3817 /* Allow constrained-type-specifiers. */
3818 parser->prevent_constrained_type_specifiers = 0;
3820 return parser;
3823 /* Create a cp_lexer structure which will emit the tokens in CACHE
3824 and push it onto the parser's lexer stack. This is used for delayed
3825 parsing of in-class method bodies and default arguments, and should
3826 not be confused with tentative parsing. */
3827 static void
3828 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3830 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3831 lexer->next = parser->lexer;
3832 parser->lexer = lexer;
3834 /* Move the current source position to that of the first token in the
3835 new lexer. */
3836 cp_lexer_set_source_position_from_token (lexer->next_token);
3839 /* Pop the top lexer off the parser stack. This is never used for the
3840 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3841 static void
3842 cp_parser_pop_lexer (cp_parser *parser)
3844 cp_lexer *lexer = parser->lexer;
3845 parser->lexer = lexer->next;
3846 cp_lexer_destroy (lexer);
3848 /* Put the current source position back where it was before this
3849 lexer was pushed. */
3850 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3853 /* Lexical conventions [gram.lex] */
3855 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3856 identifier. */
3858 static cp_expr
3859 cp_parser_identifier (cp_parser* parser)
3861 cp_token *token;
3863 /* Look for the identifier. */
3864 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3865 /* Return the value. */
3866 if (token)
3867 return cp_expr (token->u.value, token->location);
3868 else
3869 return error_mark_node;
3872 /* Parse a sequence of adjacent string constants. Returns a
3873 TREE_STRING representing the combined, nul-terminated string
3874 constant. If TRANSLATE is true, translate the string to the
3875 execution character set. If WIDE_OK is true, a wide string is
3876 invalid here.
3878 C++98 [lex.string] says that if a narrow string literal token is
3879 adjacent to a wide string literal token, the behavior is undefined.
3880 However, C99 6.4.5p4 says that this results in a wide string literal.
3881 We follow C99 here, for consistency with the C front end.
3883 This code is largely lifted from lex_string() in c-lex.c.
3885 FUTURE: ObjC++ will need to handle @-strings here. */
3886 static cp_expr
3887 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3888 bool lookup_udlit = true)
3890 tree value;
3891 size_t count;
3892 struct obstack str_ob;
3893 cpp_string str, istr, *strs;
3894 cp_token *tok;
3895 enum cpp_ttype type, curr_type;
3896 int have_suffix_p = 0;
3897 tree string_tree;
3898 tree suffix_id = NULL_TREE;
3899 bool curr_tok_is_userdef_p = false;
3901 tok = cp_lexer_peek_token (parser->lexer);
3902 if (!cp_parser_is_string_literal (tok))
3904 cp_parser_error (parser, "expected string-literal");
3905 return error_mark_node;
3908 location_t loc = tok->location;
3910 if (cpp_userdef_string_p (tok->type))
3912 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3913 curr_type = cpp_userdef_string_remove_type (tok->type);
3914 curr_tok_is_userdef_p = true;
3916 else
3918 string_tree = tok->u.value;
3919 curr_type = tok->type;
3921 type = curr_type;
3923 /* Try to avoid the overhead of creating and destroying an obstack
3924 for the common case of just one string. */
3925 if (!cp_parser_is_string_literal
3926 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3928 cp_lexer_consume_token (parser->lexer);
3930 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3931 str.len = TREE_STRING_LENGTH (string_tree);
3932 count = 1;
3934 if (curr_tok_is_userdef_p)
3936 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3937 have_suffix_p = 1;
3938 curr_type = cpp_userdef_string_remove_type (tok->type);
3940 else
3941 curr_type = tok->type;
3943 strs = &str;
3945 else
3947 location_t last_tok_loc = tok->location;
3948 gcc_obstack_init (&str_ob);
3949 count = 0;
3953 cp_lexer_consume_token (parser->lexer);
3954 count++;
3955 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3956 str.len = TREE_STRING_LENGTH (string_tree);
3958 if (curr_tok_is_userdef_p)
3960 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3961 if (have_suffix_p == 0)
3963 suffix_id = curr_suffix_id;
3964 have_suffix_p = 1;
3966 else if (have_suffix_p == 1
3967 && curr_suffix_id != suffix_id)
3969 error ("inconsistent user-defined literal suffixes"
3970 " %qD and %qD in string literal",
3971 suffix_id, curr_suffix_id);
3972 have_suffix_p = -1;
3974 curr_type = cpp_userdef_string_remove_type (tok->type);
3976 else
3977 curr_type = tok->type;
3979 if (type != curr_type)
3981 if (type == CPP_STRING)
3982 type = curr_type;
3983 else if (curr_type != CPP_STRING)
3985 rich_location rich_loc (line_table, tok->location);
3986 rich_loc.add_range (last_tok_loc, false);
3987 error_at_rich_loc (&rich_loc,
3988 "unsupported non-standard concatenation "
3989 "of string literals");
3993 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3995 last_tok_loc = tok->location;
3997 tok = cp_lexer_peek_token (parser->lexer);
3998 if (cpp_userdef_string_p (tok->type))
4000 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4001 curr_type = cpp_userdef_string_remove_type (tok->type);
4002 curr_tok_is_userdef_p = true;
4004 else
4006 string_tree = tok->u.value;
4007 curr_type = tok->type;
4008 curr_tok_is_userdef_p = false;
4011 while (cp_parser_is_string_literal (tok));
4013 /* A string literal built by concatenation has its caret=start at
4014 the start of the initial string, and its finish at the finish of
4015 the final string literal. */
4016 loc = make_location (loc, loc, get_finish (last_tok_loc));
4018 strs = (cpp_string *) obstack_finish (&str_ob);
4021 if (type != CPP_STRING && !wide_ok)
4023 cp_parser_error (parser, "a wide string is invalid in this context");
4024 type = CPP_STRING;
4027 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4028 (parse_in, strs, count, &istr, type))
4030 value = build_string (istr.len, (const char *)istr.text);
4031 free (CONST_CAST (unsigned char *, istr.text));
4033 switch (type)
4035 default:
4036 case CPP_STRING:
4037 case CPP_UTF8STRING:
4038 TREE_TYPE (value) = char_array_type_node;
4039 break;
4040 case CPP_STRING16:
4041 TREE_TYPE (value) = char16_array_type_node;
4042 break;
4043 case CPP_STRING32:
4044 TREE_TYPE (value) = char32_array_type_node;
4045 break;
4046 case CPP_WSTRING:
4047 TREE_TYPE (value) = wchar_array_type_node;
4048 break;
4051 value = fix_string_type (value);
4053 if (have_suffix_p)
4055 tree literal = build_userdef_literal (suffix_id, value,
4056 OT_NONE, NULL_TREE);
4057 if (lookup_udlit)
4058 value = cp_parser_userdef_string_literal (literal);
4059 else
4060 value = literal;
4063 else
4064 /* cpp_interpret_string has issued an error. */
4065 value = error_mark_node;
4067 if (count > 1)
4068 obstack_free (&str_ob, 0);
4070 return cp_expr (value, loc);
4073 /* Look up a literal operator with the name and the exact arguments. */
4075 static tree
4076 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4078 tree decl;
4079 decl = lookup_name (name);
4080 if (!decl || !is_overloaded_fn (decl))
4081 return error_mark_node;
4083 for (lkp_iterator iter (decl); iter; ++iter)
4085 unsigned int ix;
4086 bool found = true;
4087 tree fn = *iter;
4088 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4089 if (parmtypes != NULL_TREE)
4091 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4092 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4094 tree tparm = TREE_VALUE (parmtypes);
4095 tree targ = TREE_TYPE ((*args)[ix]);
4096 bool ptr = TYPE_PTR_P (tparm);
4097 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4098 if ((ptr || arr || !same_type_p (tparm, targ))
4099 && (!ptr || !arr
4100 || !same_type_p (TREE_TYPE (tparm),
4101 TREE_TYPE (targ))))
4102 found = false;
4104 if (found
4105 && ix == vec_safe_length (args)
4106 /* May be this should be sufficient_parms_p instead,
4107 depending on how exactly should user-defined literals
4108 work in presence of default arguments on the literal
4109 operator parameters. */
4110 && parmtypes == void_list_node)
4111 return decl;
4115 return error_mark_node;
4118 /* Parse a user-defined char constant. Returns a call to a user-defined
4119 literal operator taking the character as an argument. */
4121 static cp_expr
4122 cp_parser_userdef_char_literal (cp_parser *parser)
4124 cp_token *token = cp_lexer_consume_token (parser->lexer);
4125 tree literal = token->u.value;
4126 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4127 tree value = USERDEF_LITERAL_VALUE (literal);
4128 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4129 tree decl, result;
4131 /* Build up a call to the user-defined operator */
4132 /* Lookup the name we got back from the id-expression. */
4133 vec<tree, va_gc> *args = make_tree_vector ();
4134 vec_safe_push (args, value);
4135 decl = lookup_literal_operator (name, args);
4136 if (!decl || decl == error_mark_node)
4138 error ("unable to find character literal operator %qD with %qT argument",
4139 name, TREE_TYPE (value));
4140 release_tree_vector (args);
4141 return error_mark_node;
4143 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4144 release_tree_vector (args);
4145 return result;
4148 /* A subroutine of cp_parser_userdef_numeric_literal to
4149 create a char... template parameter pack from a string node. */
4151 static tree
4152 make_char_string_pack (tree value)
4154 tree charvec;
4155 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4156 const char *str = TREE_STRING_POINTER (value);
4157 int i, len = TREE_STRING_LENGTH (value) - 1;
4158 tree argvec = make_tree_vec (1);
4160 /* Fill in CHARVEC with all of the parameters. */
4161 charvec = make_tree_vec (len);
4162 for (i = 0; i < len; ++i)
4163 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4165 /* Build the argument packs. */
4166 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4168 TREE_VEC_ELT (argvec, 0) = argpack;
4170 return argvec;
4173 /* A subroutine of cp_parser_userdef_numeric_literal to
4174 create a char... template parameter pack from a string node. */
4176 static tree
4177 make_string_pack (tree value)
4179 tree charvec;
4180 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4181 const unsigned char *str
4182 = (const unsigned char *) TREE_STRING_POINTER (value);
4183 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4184 int len = TREE_STRING_LENGTH (value) / sz - 1;
4185 tree argvec = make_tree_vec (2);
4187 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4188 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4190 /* First template parm is character type. */
4191 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4193 /* Fill in CHARVEC with all of the parameters. */
4194 charvec = make_tree_vec (len);
4195 for (int i = 0; i < len; ++i)
4196 TREE_VEC_ELT (charvec, i)
4197 = double_int_to_tree (str_char_type_node,
4198 double_int::from_buffer (str + i * sz, sz));
4200 /* Build the argument packs. */
4201 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4203 TREE_VEC_ELT (argvec, 1) = argpack;
4205 return argvec;
4208 /* Parse a user-defined numeric constant. returns a call to a user-defined
4209 literal operator. */
4211 static cp_expr
4212 cp_parser_userdef_numeric_literal (cp_parser *parser)
4214 cp_token *token = cp_lexer_consume_token (parser->lexer);
4215 tree literal = token->u.value;
4216 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4217 tree value = USERDEF_LITERAL_VALUE (literal);
4218 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4219 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4220 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4221 tree decl, result;
4222 vec<tree, va_gc> *args;
4224 /* Look for a literal operator taking the exact type of numeric argument
4225 as the literal value. */
4226 args = make_tree_vector ();
4227 vec_safe_push (args, value);
4228 decl = lookup_literal_operator (name, args);
4229 if (decl && decl != error_mark_node)
4231 result = finish_call_expr (decl, &args, false, true,
4232 tf_warning_or_error);
4234 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4236 warning_at (token->location, OPT_Woverflow,
4237 "integer literal exceeds range of %qT type",
4238 long_long_unsigned_type_node);
4240 else
4242 if (overflow > 0)
4243 warning_at (token->location, OPT_Woverflow,
4244 "floating literal exceeds range of %qT type",
4245 long_double_type_node);
4246 else if (overflow < 0)
4247 warning_at (token->location, OPT_Woverflow,
4248 "floating literal truncated to zero");
4251 release_tree_vector (args);
4252 return result;
4254 release_tree_vector (args);
4256 /* If the numeric argument didn't work, look for a raw literal
4257 operator taking a const char* argument consisting of the number
4258 in string format. */
4259 args = make_tree_vector ();
4260 vec_safe_push (args, num_string);
4261 decl = lookup_literal_operator (name, args);
4262 if (decl && decl != error_mark_node)
4264 result = finish_call_expr (decl, &args, false, true,
4265 tf_warning_or_error);
4266 release_tree_vector (args);
4267 return result;
4269 release_tree_vector (args);
4271 /* If the raw literal didn't work, look for a non-type template
4272 function with parameter pack char.... Call the function with
4273 template parameter characters representing the number. */
4274 args = make_tree_vector ();
4275 decl = lookup_literal_operator (name, args);
4276 if (decl && decl != error_mark_node)
4278 tree tmpl_args = make_char_string_pack (num_string);
4279 decl = lookup_template_function (decl, tmpl_args);
4280 result = finish_call_expr (decl, &args, false, true,
4281 tf_warning_or_error);
4282 release_tree_vector (args);
4283 return result;
4286 release_tree_vector (args);
4288 error ("unable to find numeric literal operator %qD", name);
4289 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4290 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4291 "to enable more built-in suffixes");
4292 return error_mark_node;
4295 /* Parse a user-defined string constant. Returns a call to a user-defined
4296 literal operator taking a character pointer and the length of the string
4297 as arguments. */
4299 static tree
4300 cp_parser_userdef_string_literal (tree literal)
4302 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4303 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4304 tree value = USERDEF_LITERAL_VALUE (literal);
4305 int len = TREE_STRING_LENGTH (value)
4306 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4307 tree decl, result;
4308 vec<tree, va_gc> *args;
4310 /* Build up a call to the user-defined operator. */
4311 /* Lookup the name we got back from the id-expression. */
4312 args = make_tree_vector ();
4313 vec_safe_push (args, value);
4314 vec_safe_push (args, build_int_cst (size_type_node, len));
4315 decl = lookup_literal_operator (name, args);
4317 if (decl && decl != error_mark_node)
4319 result = finish_call_expr (decl, &args, false, true,
4320 tf_warning_or_error);
4321 release_tree_vector (args);
4322 return result;
4324 release_tree_vector (args);
4326 /* Look for a template function with typename parameter CharT
4327 and parameter pack CharT... Call the function with
4328 template parameter characters representing the string. */
4329 args = make_tree_vector ();
4330 decl = lookup_literal_operator (name, args);
4331 if (decl && decl != error_mark_node)
4333 tree tmpl_args = make_string_pack (value);
4334 decl = lookup_template_function (decl, tmpl_args);
4335 result = finish_call_expr (decl, &args, false, true,
4336 tf_warning_or_error);
4337 release_tree_vector (args);
4338 return result;
4340 release_tree_vector (args);
4342 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4343 name, TREE_TYPE (value), size_type_node);
4344 return error_mark_node;
4348 /* Basic concepts [gram.basic] */
4350 /* Parse a translation-unit.
4352 translation-unit:
4353 declaration-seq [opt]
4355 Returns TRUE if all went well. */
4357 static bool
4358 cp_parser_translation_unit (cp_parser* parser)
4360 /* The address of the first non-permanent object on the declarator
4361 obstack. */
4362 static void *declarator_obstack_base;
4364 bool success;
4366 /* Create the declarator obstack, if necessary. */
4367 if (!cp_error_declarator)
4369 gcc_obstack_init (&declarator_obstack);
4370 /* Create the error declarator. */
4371 cp_error_declarator = make_declarator (cdk_error);
4372 /* Create the empty parameter list. */
4373 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4374 /* Remember where the base of the declarator obstack lies. */
4375 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4378 cp_parser_declaration_seq_opt (parser);
4380 /* If there are no tokens left then all went well. */
4381 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4383 /* Get rid of the token array; we don't need it any more. */
4384 cp_lexer_destroy (parser->lexer);
4385 parser->lexer = NULL;
4387 /* This file might have been a context that's implicitly extern
4388 "C". If so, pop the lang context. (Only relevant for PCH.) */
4389 if (parser->implicit_extern_c)
4391 pop_lang_context ();
4392 parser->implicit_extern_c = false;
4395 /* Finish up. */
4396 finish_translation_unit ();
4398 success = true;
4400 else
4402 cp_parser_error (parser, "expected declaration");
4403 success = false;
4406 /* Make sure the declarator obstack was fully cleaned up. */
4407 gcc_assert (obstack_next_free (&declarator_obstack)
4408 == declarator_obstack_base);
4410 /* All went well. */
4411 return success;
4414 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4415 decltype context. */
4417 static inline tsubst_flags_t
4418 complain_flags (bool decltype_p)
4420 tsubst_flags_t complain = tf_warning_or_error;
4421 if (decltype_p)
4422 complain |= tf_decltype;
4423 return complain;
4426 /* We're about to parse a collection of statements. If we're currently
4427 parsing tentatively, set up a firewall so that any nested
4428 cp_parser_commit_to_tentative_parse won't affect the current context. */
4430 static cp_token_position
4431 cp_parser_start_tentative_firewall (cp_parser *parser)
4433 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4434 return 0;
4436 cp_parser_parse_tentatively (parser);
4437 cp_parser_commit_to_topmost_tentative_parse (parser);
4438 return cp_lexer_token_position (parser->lexer, false);
4441 /* We've finished parsing the collection of statements. Wrap up the
4442 firewall and replace the relevant tokens with the parsed form. */
4444 static void
4445 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4446 tree expr)
4448 if (!start)
4449 return;
4451 /* Finish the firewall level. */
4452 cp_parser_parse_definitely (parser);
4453 /* And remember the result of the parse for when we try again. */
4454 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4455 token->type = CPP_PREPARSED_EXPR;
4456 token->u.value = expr;
4457 token->keyword = RID_MAX;
4458 cp_lexer_purge_tokens_after (parser->lexer, start);
4461 /* Like the above functions, but let the user modify the tokens. Used by
4462 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4463 later parses, so it makes sense to localize the effects of
4464 cp_parser_commit_to_tentative_parse. */
4466 struct tentative_firewall
4468 cp_parser *parser;
4469 bool set;
4471 tentative_firewall (cp_parser *p): parser(p)
4473 /* If we're currently parsing tentatively, start a committed level as a
4474 firewall and then an inner tentative parse. */
4475 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4477 cp_parser_parse_tentatively (parser);
4478 cp_parser_commit_to_topmost_tentative_parse (parser);
4479 cp_parser_parse_tentatively (parser);
4483 ~tentative_firewall()
4485 if (set)
4487 /* Finish the inner tentative parse and the firewall, propagating any
4488 uncommitted error state to the outer tentative parse. */
4489 bool err = cp_parser_error_occurred (parser);
4490 cp_parser_parse_definitely (parser);
4491 cp_parser_parse_definitely (parser);
4492 if (err)
4493 cp_parser_simulate_error (parser);
4498 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4499 enclosing parentheses. */
4501 static cp_expr
4502 cp_parser_statement_expr (cp_parser *parser)
4504 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4506 /* Consume the '('. */
4507 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4508 cp_lexer_consume_token (parser->lexer);
4509 /* Start the statement-expression. */
4510 tree expr = begin_stmt_expr ();
4511 /* Parse the compound-statement. */
4512 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4513 /* Finish up. */
4514 expr = finish_stmt_expr (expr, false);
4515 /* Consume the ')'. */
4516 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4517 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4518 cp_parser_skip_to_end_of_statement (parser);
4520 cp_parser_end_tentative_firewall (parser, start, expr);
4521 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4522 return cp_expr (expr, combined_loc);
4525 /* Expressions [gram.expr] */
4527 /* Parse a fold-operator.
4529 fold-operator:
4530 - * / % ^ & | = < > << >>
4531 = -= *= /= %= ^= &= |= <<= >>=
4532 == != <= >= && || , .* ->*
4534 This returns the tree code corresponding to the matched operator
4535 as an int. When the current token matches a compound assignment
4536 opertor, the resulting tree code is the negative value of the
4537 non-assignment operator. */
4539 static int
4540 cp_parser_fold_operator (cp_token *token)
4542 switch (token->type)
4544 case CPP_PLUS: return PLUS_EXPR;
4545 case CPP_MINUS: return MINUS_EXPR;
4546 case CPP_MULT: return MULT_EXPR;
4547 case CPP_DIV: return TRUNC_DIV_EXPR;
4548 case CPP_MOD: return TRUNC_MOD_EXPR;
4549 case CPP_XOR: return BIT_XOR_EXPR;
4550 case CPP_AND: return BIT_AND_EXPR;
4551 case CPP_OR: return BIT_IOR_EXPR;
4552 case CPP_LSHIFT: return LSHIFT_EXPR;
4553 case CPP_RSHIFT: return RSHIFT_EXPR;
4555 case CPP_EQ: return -NOP_EXPR;
4556 case CPP_PLUS_EQ: return -PLUS_EXPR;
4557 case CPP_MINUS_EQ: return -MINUS_EXPR;
4558 case CPP_MULT_EQ: return -MULT_EXPR;
4559 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4560 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4561 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4562 case CPP_AND_EQ: return -BIT_AND_EXPR;
4563 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4564 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4565 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4567 case CPP_EQ_EQ: return EQ_EXPR;
4568 case CPP_NOT_EQ: return NE_EXPR;
4569 case CPP_LESS: return LT_EXPR;
4570 case CPP_GREATER: return GT_EXPR;
4571 case CPP_LESS_EQ: return LE_EXPR;
4572 case CPP_GREATER_EQ: return GE_EXPR;
4574 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4575 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4577 case CPP_COMMA: return COMPOUND_EXPR;
4579 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4580 case CPP_DEREF_STAR: return MEMBER_REF;
4582 default: return ERROR_MARK;
4586 /* Returns true if CODE indicates a binary expression, which is not allowed in
4587 the LHS of a fold-expression. More codes will need to be added to use this
4588 function in other contexts. */
4590 static bool
4591 is_binary_op (tree_code code)
4593 switch (code)
4595 case PLUS_EXPR:
4596 case POINTER_PLUS_EXPR:
4597 case MINUS_EXPR:
4598 case MULT_EXPR:
4599 case TRUNC_DIV_EXPR:
4600 case TRUNC_MOD_EXPR:
4601 case BIT_XOR_EXPR:
4602 case BIT_AND_EXPR:
4603 case BIT_IOR_EXPR:
4604 case LSHIFT_EXPR:
4605 case RSHIFT_EXPR:
4607 case MODOP_EXPR:
4609 case EQ_EXPR:
4610 case NE_EXPR:
4611 case LE_EXPR:
4612 case GE_EXPR:
4613 case LT_EXPR:
4614 case GT_EXPR:
4616 case TRUTH_ANDIF_EXPR:
4617 case TRUTH_ORIF_EXPR:
4619 case COMPOUND_EXPR:
4621 case DOTSTAR_EXPR:
4622 case MEMBER_REF:
4623 return true;
4625 default:
4626 return false;
4630 /* If the next token is a suitable fold operator, consume it and return as
4631 the function above. */
4633 static int
4634 cp_parser_fold_operator (cp_parser *parser)
4636 cp_token* token = cp_lexer_peek_token (parser->lexer);
4637 int code = cp_parser_fold_operator (token);
4638 if (code != ERROR_MARK)
4639 cp_lexer_consume_token (parser->lexer);
4640 return code;
4643 /* Parse a fold-expression.
4645 fold-expression:
4646 ( ... folding-operator cast-expression)
4647 ( cast-expression folding-operator ... )
4648 ( cast-expression folding operator ... folding-operator cast-expression)
4650 Note that the '(' and ')' are matched in primary expression. */
4652 static cp_expr
4653 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4655 cp_id_kind pidk;
4657 // Left fold.
4658 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4660 cp_lexer_consume_token (parser->lexer);
4661 int op = cp_parser_fold_operator (parser);
4662 if (op == ERROR_MARK)
4664 cp_parser_error (parser, "expected binary operator");
4665 return error_mark_node;
4668 tree expr = cp_parser_cast_expression (parser, false, false,
4669 false, &pidk);
4670 if (expr == error_mark_node)
4671 return error_mark_node;
4672 return finish_left_unary_fold_expr (expr, op);
4675 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4676 int op = cp_parser_fold_operator (parser);
4677 if (op == ERROR_MARK)
4679 cp_parser_error (parser, "expected binary operator");
4680 return error_mark_node;
4683 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4685 cp_parser_error (parser, "expected ...");
4686 return error_mark_node;
4688 cp_lexer_consume_token (parser->lexer);
4690 /* The operands of a fold-expression are cast-expressions, so binary or
4691 conditional expressions are not allowed. We check this here to avoid
4692 tentative parsing. */
4693 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4694 /* OK, the expression was parenthesized. */;
4695 else if (is_binary_op (TREE_CODE (expr1)))
4696 error_at (location_of (expr1),
4697 "binary expression in operand of fold-expression");
4698 else if (TREE_CODE (expr1) == COND_EXPR)
4699 error_at (location_of (expr1),
4700 "conditional expression in operand of fold-expression");
4702 // Right fold.
4703 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4704 return finish_right_unary_fold_expr (expr1, op);
4706 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4708 cp_parser_error (parser, "mismatched operator in fold-expression");
4709 return error_mark_node;
4711 cp_lexer_consume_token (parser->lexer);
4713 // Binary left or right fold.
4714 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4715 if (expr2 == error_mark_node)
4716 return error_mark_node;
4717 return finish_binary_fold_expr (expr1, expr2, op);
4720 /* Parse a primary-expression.
4722 primary-expression:
4723 literal
4724 this
4725 ( expression )
4726 id-expression
4727 lambda-expression (C++11)
4729 GNU Extensions:
4731 primary-expression:
4732 ( compound-statement )
4733 __builtin_va_arg ( assignment-expression , type-id )
4734 __builtin_offsetof ( type-id , offsetof-expression )
4736 C++ Extensions:
4737 __has_nothrow_assign ( type-id )
4738 __has_nothrow_constructor ( type-id )
4739 __has_nothrow_copy ( type-id )
4740 __has_trivial_assign ( type-id )
4741 __has_trivial_constructor ( type-id )
4742 __has_trivial_copy ( type-id )
4743 __has_trivial_destructor ( type-id )
4744 __has_virtual_destructor ( type-id )
4745 __is_abstract ( type-id )
4746 __is_base_of ( type-id , type-id )
4747 __is_class ( type-id )
4748 __is_empty ( type-id )
4749 __is_enum ( type-id )
4750 __is_final ( type-id )
4751 __is_literal_type ( type-id )
4752 __is_pod ( type-id )
4753 __is_polymorphic ( type-id )
4754 __is_std_layout ( type-id )
4755 __is_trivial ( type-id )
4756 __is_union ( type-id )
4758 Objective-C++ Extension:
4760 primary-expression:
4761 objc-expression
4763 literal:
4764 __null
4766 ADDRESS_P is true iff this expression was immediately preceded by
4767 "&" and therefore might denote a pointer-to-member. CAST_P is true
4768 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4769 true iff this expression is a template argument.
4771 Returns a representation of the expression. Upon return, *IDK
4772 indicates what kind of id-expression (if any) was present. */
4774 static cp_expr
4775 cp_parser_primary_expression (cp_parser *parser,
4776 bool address_p,
4777 bool cast_p,
4778 bool template_arg_p,
4779 bool decltype_p,
4780 cp_id_kind *idk)
4782 cp_token *token = NULL;
4784 /* Assume the primary expression is not an id-expression. */
4785 *idk = CP_ID_KIND_NONE;
4787 /* Peek at the next token. */
4788 token = cp_lexer_peek_token (parser->lexer);
4789 switch ((int) token->type)
4791 /* literal:
4792 integer-literal
4793 character-literal
4794 floating-literal
4795 string-literal
4796 boolean-literal
4797 pointer-literal
4798 user-defined-literal */
4799 case CPP_CHAR:
4800 case CPP_CHAR16:
4801 case CPP_CHAR32:
4802 case CPP_WCHAR:
4803 case CPP_UTF8CHAR:
4804 case CPP_NUMBER:
4805 case CPP_PREPARSED_EXPR:
4806 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4807 return cp_parser_userdef_numeric_literal (parser);
4808 token = cp_lexer_consume_token (parser->lexer);
4809 if (TREE_CODE (token->u.value) == FIXED_CST)
4811 error_at (token->location,
4812 "fixed-point types not supported in C++");
4813 return error_mark_node;
4815 /* Floating-point literals are only allowed in an integral
4816 constant expression if they are cast to an integral or
4817 enumeration type. */
4818 if (TREE_CODE (token->u.value) == REAL_CST
4819 && parser->integral_constant_expression_p
4820 && pedantic)
4822 /* CAST_P will be set even in invalid code like "int(2.7 +
4823 ...)". Therefore, we have to check that the next token
4824 is sure to end the cast. */
4825 if (cast_p)
4827 cp_token *next_token;
4829 next_token = cp_lexer_peek_token (parser->lexer);
4830 if (/* The comma at the end of an
4831 enumerator-definition. */
4832 next_token->type != CPP_COMMA
4833 /* The curly brace at the end of an enum-specifier. */
4834 && next_token->type != CPP_CLOSE_BRACE
4835 /* The end of a statement. */
4836 && next_token->type != CPP_SEMICOLON
4837 /* The end of the cast-expression. */
4838 && next_token->type != CPP_CLOSE_PAREN
4839 /* The end of an array bound. */
4840 && next_token->type != CPP_CLOSE_SQUARE
4841 /* The closing ">" in a template-argument-list. */
4842 && (next_token->type != CPP_GREATER
4843 || parser->greater_than_is_operator_p)
4844 /* C++0x only: A ">>" treated like two ">" tokens,
4845 in a template-argument-list. */
4846 && (next_token->type != CPP_RSHIFT
4847 || (cxx_dialect == cxx98)
4848 || parser->greater_than_is_operator_p))
4849 cast_p = false;
4852 /* If we are within a cast, then the constraint that the
4853 cast is to an integral or enumeration type will be
4854 checked at that point. If we are not within a cast, then
4855 this code is invalid. */
4856 if (!cast_p)
4857 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4859 return cp_expr (token->u.value, token->location);
4861 case CPP_CHAR_USERDEF:
4862 case CPP_CHAR16_USERDEF:
4863 case CPP_CHAR32_USERDEF:
4864 case CPP_WCHAR_USERDEF:
4865 case CPP_UTF8CHAR_USERDEF:
4866 return cp_parser_userdef_char_literal (parser);
4868 case CPP_STRING:
4869 case CPP_STRING16:
4870 case CPP_STRING32:
4871 case CPP_WSTRING:
4872 case CPP_UTF8STRING:
4873 case CPP_STRING_USERDEF:
4874 case CPP_STRING16_USERDEF:
4875 case CPP_STRING32_USERDEF:
4876 case CPP_WSTRING_USERDEF:
4877 case CPP_UTF8STRING_USERDEF:
4878 /* ??? Should wide strings be allowed when parser->translate_strings_p
4879 is false (i.e. in attributes)? If not, we can kill the third
4880 argument to cp_parser_string_literal. */
4881 return cp_parser_string_literal (parser,
4882 parser->translate_strings_p,
4883 true);
4885 case CPP_OPEN_PAREN:
4886 /* If we see `( { ' then we are looking at the beginning of
4887 a GNU statement-expression. */
4888 if (cp_parser_allow_gnu_extensions_p (parser)
4889 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4891 /* Statement-expressions are not allowed by the standard. */
4892 pedwarn (token->location, OPT_Wpedantic,
4893 "ISO C++ forbids braced-groups within expressions");
4895 /* And they're not allowed outside of a function-body; you
4896 cannot, for example, write:
4898 int i = ({ int j = 3; j + 1; });
4900 at class or namespace scope. */
4901 if (!parser->in_function_body
4902 || parser->in_template_argument_list_p)
4904 error_at (token->location,
4905 "statement-expressions are not allowed outside "
4906 "functions nor in template-argument lists");
4907 cp_parser_skip_to_end_of_block_or_statement (parser);
4908 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4909 cp_lexer_consume_token (parser->lexer);
4910 return error_mark_node;
4912 else
4913 return cp_parser_statement_expr (parser);
4915 /* Otherwise it's a normal parenthesized expression. */
4917 cp_expr expr;
4918 bool saved_greater_than_is_operator_p;
4920 location_t open_paren_loc = token->location;
4922 /* Consume the `('. */
4923 cp_lexer_consume_token (parser->lexer);
4924 /* Within a parenthesized expression, a `>' token is always
4925 the greater-than operator. */
4926 saved_greater_than_is_operator_p
4927 = parser->greater_than_is_operator_p;
4928 parser->greater_than_is_operator_p = true;
4930 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4931 /* Left fold expression. */
4932 expr = NULL_TREE;
4933 else
4934 /* Parse the parenthesized expression. */
4935 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4937 token = cp_lexer_peek_token (parser->lexer);
4938 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4940 expr = cp_parser_fold_expression (parser, expr);
4941 if (expr != error_mark_node
4942 && cxx_dialect < cxx1z
4943 && !in_system_header_at (input_location))
4944 pedwarn (input_location, 0, "fold-expressions only available "
4945 "with -std=c++1z or -std=gnu++1z");
4947 else
4948 /* Let the front end know that this expression was
4949 enclosed in parentheses. This matters in case, for
4950 example, the expression is of the form `A::B', since
4951 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4952 not. */
4953 expr = finish_parenthesized_expr (expr);
4955 /* DR 705: Wrapping an unqualified name in parentheses
4956 suppresses arg-dependent lookup. We want to pass back
4957 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4958 (c++/37862), but none of the others. */
4959 if (*idk != CP_ID_KIND_QUALIFIED)
4960 *idk = CP_ID_KIND_NONE;
4962 /* The `>' token might be the end of a template-id or
4963 template-parameter-list now. */
4964 parser->greater_than_is_operator_p
4965 = saved_greater_than_is_operator_p;
4967 /* Consume the `)'. */
4968 token = cp_lexer_peek_token (parser->lexer);
4969 location_t close_paren_loc = token->location;
4970 expr.set_range (open_paren_loc, close_paren_loc);
4971 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4972 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4973 cp_parser_skip_to_end_of_statement (parser);
4975 return expr;
4978 case CPP_OPEN_SQUARE:
4980 if (c_dialect_objc ())
4982 /* We might have an Objective-C++ message. */
4983 cp_parser_parse_tentatively (parser);
4984 tree msg = cp_parser_objc_message_expression (parser);
4985 /* If that works out, we're done ... */
4986 if (cp_parser_parse_definitely (parser))
4987 return msg;
4988 /* ... else, fall though to see if it's a lambda. */
4990 cp_expr lam = cp_parser_lambda_expression (parser);
4991 /* Don't warn about a failed tentative parse. */
4992 if (cp_parser_error_occurred (parser))
4993 return error_mark_node;
4994 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4995 return lam;
4998 case CPP_OBJC_STRING:
4999 if (c_dialect_objc ())
5000 /* We have an Objective-C++ string literal. */
5001 return cp_parser_objc_expression (parser);
5002 cp_parser_error (parser, "expected primary-expression");
5003 return error_mark_node;
5005 case CPP_KEYWORD:
5006 switch (token->keyword)
5008 /* These two are the boolean literals. */
5009 case RID_TRUE:
5010 cp_lexer_consume_token (parser->lexer);
5011 return cp_expr (boolean_true_node, token->location);
5012 case RID_FALSE:
5013 cp_lexer_consume_token (parser->lexer);
5014 return cp_expr (boolean_false_node, token->location);
5016 /* The `__null' literal. */
5017 case RID_NULL:
5018 cp_lexer_consume_token (parser->lexer);
5019 return cp_expr (null_node, token->location);
5021 /* The `nullptr' literal. */
5022 case RID_NULLPTR:
5023 cp_lexer_consume_token (parser->lexer);
5024 return cp_expr (nullptr_node, token->location);
5026 /* Recognize the `this' keyword. */
5027 case RID_THIS:
5028 cp_lexer_consume_token (parser->lexer);
5029 if (parser->local_variables_forbidden_p)
5031 error_at (token->location,
5032 "%<this%> may not be used in this context");
5033 return error_mark_node;
5035 /* Pointers cannot appear in constant-expressions. */
5036 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5037 return error_mark_node;
5038 return cp_expr (finish_this_expr (), token->location);
5040 /* The `operator' keyword can be the beginning of an
5041 id-expression. */
5042 case RID_OPERATOR:
5043 goto id_expression;
5045 case RID_FUNCTION_NAME:
5046 case RID_PRETTY_FUNCTION_NAME:
5047 case RID_C99_FUNCTION_NAME:
5049 non_integral_constant name;
5051 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5052 __func__ are the names of variables -- but they are
5053 treated specially. Therefore, they are handled here,
5054 rather than relying on the generic id-expression logic
5055 below. Grammatically, these names are id-expressions.
5057 Consume the token. */
5058 token = cp_lexer_consume_token (parser->lexer);
5060 switch (token->keyword)
5062 case RID_FUNCTION_NAME:
5063 name = NIC_FUNC_NAME;
5064 break;
5065 case RID_PRETTY_FUNCTION_NAME:
5066 name = NIC_PRETTY_FUNC;
5067 break;
5068 case RID_C99_FUNCTION_NAME:
5069 name = NIC_C99_FUNC;
5070 break;
5071 default:
5072 gcc_unreachable ();
5075 if (cp_parser_non_integral_constant_expression (parser, name))
5076 return error_mark_node;
5078 /* Look up the name. */
5079 return finish_fname (token->u.value);
5082 case RID_VA_ARG:
5084 tree expression;
5085 tree type;
5086 source_location type_location;
5087 location_t start_loc
5088 = cp_lexer_peek_token (parser->lexer)->location;
5089 /* The `__builtin_va_arg' construct is used to handle
5090 `va_arg'. Consume the `__builtin_va_arg' token. */
5091 cp_lexer_consume_token (parser->lexer);
5092 /* Look for the opening `('. */
5093 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5094 /* Now, parse the assignment-expression. */
5095 expression = cp_parser_assignment_expression (parser);
5096 /* Look for the `,'. */
5097 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5098 type_location = cp_lexer_peek_token (parser->lexer)->location;
5099 /* Parse the type-id. */
5101 type_id_in_expr_sentinel s (parser);
5102 type = cp_parser_type_id (parser);
5104 /* Look for the closing `)'. */
5105 location_t finish_loc
5106 = cp_lexer_peek_token (parser->lexer)->location;
5107 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5108 /* Using `va_arg' in a constant-expression is not
5109 allowed. */
5110 if (cp_parser_non_integral_constant_expression (parser,
5111 NIC_VA_ARG))
5112 return error_mark_node;
5113 /* Construct a location of the form:
5114 __builtin_va_arg (v, int)
5115 ~~~~~~~~~~~~~~~~~~~~~^~~~
5116 with the caret at the type, ranging from the start of the
5117 "__builtin_va_arg" token to the close paren. */
5118 location_t combined_loc
5119 = make_location (type_location, start_loc, finish_loc);
5120 return build_x_va_arg (combined_loc, expression, type);
5123 case RID_OFFSETOF:
5124 return cp_parser_builtin_offsetof (parser);
5126 case RID_HAS_NOTHROW_ASSIGN:
5127 case RID_HAS_NOTHROW_CONSTRUCTOR:
5128 case RID_HAS_NOTHROW_COPY:
5129 case RID_HAS_TRIVIAL_ASSIGN:
5130 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5131 case RID_HAS_TRIVIAL_COPY:
5132 case RID_HAS_TRIVIAL_DESTRUCTOR:
5133 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5134 case RID_HAS_VIRTUAL_DESTRUCTOR:
5135 case RID_IS_ABSTRACT:
5136 case RID_IS_AGGREGATE:
5137 case RID_IS_BASE_OF:
5138 case RID_IS_CLASS:
5139 case RID_IS_EMPTY:
5140 case RID_IS_ENUM:
5141 case RID_IS_FINAL:
5142 case RID_IS_LITERAL_TYPE:
5143 case RID_IS_POD:
5144 case RID_IS_POLYMORPHIC:
5145 case RID_IS_SAME_AS:
5146 case RID_IS_STD_LAYOUT:
5147 case RID_IS_TRIVIAL:
5148 case RID_IS_TRIVIALLY_ASSIGNABLE:
5149 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5150 case RID_IS_TRIVIALLY_COPYABLE:
5151 case RID_IS_UNION:
5152 case RID_IS_ASSIGNABLE:
5153 case RID_IS_CONSTRUCTIBLE:
5154 return cp_parser_trait_expr (parser, token->keyword);
5156 // C++ concepts
5157 case RID_REQUIRES:
5158 return cp_parser_requires_expression (parser);
5160 /* Objective-C++ expressions. */
5161 case RID_AT_ENCODE:
5162 case RID_AT_PROTOCOL:
5163 case RID_AT_SELECTOR:
5164 return cp_parser_objc_expression (parser);
5166 case RID_TEMPLATE:
5167 if (parser->in_function_body
5168 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5169 == CPP_LESS))
5171 error_at (token->location,
5172 "a template declaration cannot appear at block scope");
5173 cp_parser_skip_to_end_of_block_or_statement (parser);
5174 return error_mark_node;
5176 /* FALLTHRU */
5177 default:
5178 cp_parser_error (parser, "expected primary-expression");
5179 return error_mark_node;
5182 /* An id-expression can start with either an identifier, a
5183 `::' as the beginning of a qualified-id, or the "operator"
5184 keyword. */
5185 case CPP_NAME:
5186 case CPP_SCOPE:
5187 case CPP_TEMPLATE_ID:
5188 case CPP_NESTED_NAME_SPECIFIER:
5190 id_expression:
5191 cp_expr id_expression;
5192 cp_expr decl;
5193 const char *error_msg;
5194 bool template_p;
5195 bool done;
5196 cp_token *id_expr_token;
5198 /* Parse the id-expression. */
5199 id_expression
5200 = cp_parser_id_expression (parser,
5201 /*template_keyword_p=*/false,
5202 /*check_dependency_p=*/true,
5203 &template_p,
5204 /*declarator_p=*/false,
5205 /*optional_p=*/false);
5206 if (id_expression == error_mark_node)
5207 return error_mark_node;
5208 id_expr_token = token;
5209 token = cp_lexer_peek_token (parser->lexer);
5210 done = (token->type != CPP_OPEN_SQUARE
5211 && token->type != CPP_OPEN_PAREN
5212 && token->type != CPP_DOT
5213 && token->type != CPP_DEREF
5214 && token->type != CPP_PLUS_PLUS
5215 && token->type != CPP_MINUS_MINUS);
5216 /* If we have a template-id, then no further lookup is
5217 required. If the template-id was for a template-class, we
5218 will sometimes have a TYPE_DECL at this point. */
5219 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5220 || TREE_CODE (id_expression) == TYPE_DECL)
5221 decl = id_expression;
5222 /* Look up the name. */
5223 else
5225 tree ambiguous_decls;
5227 /* If we already know that this lookup is ambiguous, then
5228 we've already issued an error message; there's no reason
5229 to check again. */
5230 if (id_expr_token->type == CPP_NAME
5231 && id_expr_token->error_reported)
5233 cp_parser_simulate_error (parser);
5234 return error_mark_node;
5237 decl = cp_parser_lookup_name (parser, id_expression,
5238 none_type,
5239 template_p,
5240 /*is_namespace=*/false,
5241 /*check_dependency=*/true,
5242 &ambiguous_decls,
5243 id_expr_token->location);
5244 /* If the lookup was ambiguous, an error will already have
5245 been issued. */
5246 if (ambiguous_decls)
5247 return error_mark_node;
5249 /* In Objective-C++, we may have an Objective-C 2.0
5250 dot-syntax for classes here. */
5251 if (c_dialect_objc ()
5252 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5253 && TREE_CODE (decl) == TYPE_DECL
5254 && objc_is_class_name (decl))
5256 tree component;
5257 cp_lexer_consume_token (parser->lexer);
5258 component = cp_parser_identifier (parser);
5259 if (component == error_mark_node)
5260 return error_mark_node;
5262 tree result = objc_build_class_component_ref (id_expression,
5263 component);
5264 /* Build a location of the form:
5265 expr.component
5266 ~~~~~^~~~~~~~~
5267 with caret at the start of the component name (at
5268 input_location), ranging from the start of the id_expression
5269 to the end of the component name. */
5270 location_t combined_loc
5271 = make_location (input_location, id_expression.get_start (),
5272 get_finish (input_location));
5273 protected_set_expr_location (result, combined_loc);
5274 return result;
5277 /* In Objective-C++, an instance variable (ivar) may be preferred
5278 to whatever cp_parser_lookup_name() found.
5279 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5280 rest of c-family, we have to do a little extra work to preserve
5281 any location information in cp_expr "decl". Given that
5282 objc_lookup_ivar is implemented in "c-family" and "objc", we
5283 have a trip through the pure "tree" type, rather than cp_expr.
5284 Naively copying it back to "decl" would implicitly give the
5285 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5286 store an EXPR_LOCATION. Hence we only update "decl" (and
5287 hence its location_t) if we get back a different tree node. */
5288 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5289 id_expression);
5290 if (decl_tree != decl.get_value ())
5291 decl = cp_expr (decl_tree);
5293 /* If name lookup gives us a SCOPE_REF, then the
5294 qualifying scope was dependent. */
5295 if (TREE_CODE (decl) == SCOPE_REF)
5297 /* At this point, we do not know if DECL is a valid
5298 integral constant expression. We assume that it is
5299 in fact such an expression, so that code like:
5301 template <int N> struct A {
5302 int a[B<N>::i];
5305 is accepted. At template-instantiation time, we
5306 will check that B<N>::i is actually a constant. */
5307 return decl;
5309 /* Check to see if DECL is a local variable in a context
5310 where that is forbidden. */
5311 if (parser->local_variables_forbidden_p
5312 && local_variable_p (decl))
5314 /* It might be that we only found DECL because we are
5315 trying to be generous with pre-ISO scoping rules.
5316 For example, consider:
5318 int i;
5319 void g() {
5320 for (int i = 0; i < 10; ++i) {}
5321 extern void f(int j = i);
5324 Here, name look up will originally find the out
5325 of scope `i'. We need to issue a warning message,
5326 but then use the global `i'. */
5327 decl = check_for_out_of_scope_variable (decl);
5328 if (local_variable_p (decl))
5330 error_at (id_expr_token->location,
5331 "local variable %qD may not appear in this context",
5332 decl.get_value ());
5333 return error_mark_node;
5338 decl = (finish_id_expression
5339 (id_expression, decl, parser->scope,
5340 idk,
5341 parser->integral_constant_expression_p,
5342 parser->allow_non_integral_constant_expression_p,
5343 &parser->non_integral_constant_expression_p,
5344 template_p, done, address_p,
5345 template_arg_p,
5346 &error_msg,
5347 id_expression.get_location ()));
5348 if (error_msg)
5349 cp_parser_error (parser, error_msg);
5350 decl.set_location (id_expr_token->location);
5351 return decl;
5354 /* Anything else is an error. */
5355 default:
5356 cp_parser_error (parser, "expected primary-expression");
5357 return error_mark_node;
5361 static inline cp_expr
5362 cp_parser_primary_expression (cp_parser *parser,
5363 bool address_p,
5364 bool cast_p,
5365 bool template_arg_p,
5366 cp_id_kind *idk)
5368 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5369 /*decltype*/false, idk);
5372 /* Parse an id-expression.
5374 id-expression:
5375 unqualified-id
5376 qualified-id
5378 qualified-id:
5379 :: [opt] nested-name-specifier template [opt] unqualified-id
5380 :: identifier
5381 :: operator-function-id
5382 :: template-id
5384 Return a representation of the unqualified portion of the
5385 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5386 a `::' or nested-name-specifier.
5388 Often, if the id-expression was a qualified-id, the caller will
5389 want to make a SCOPE_REF to represent the qualified-id. This
5390 function does not do this in order to avoid wastefully creating
5391 SCOPE_REFs when they are not required.
5393 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5394 `template' keyword.
5396 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5397 uninstantiated templates.
5399 If *TEMPLATE_P is non-NULL, it is set to true iff the
5400 `template' keyword is used to explicitly indicate that the entity
5401 named is a template.
5403 If DECLARATOR_P is true, the id-expression is appearing as part of
5404 a declarator, rather than as part of an expression. */
5406 static cp_expr
5407 cp_parser_id_expression (cp_parser *parser,
5408 bool template_keyword_p,
5409 bool check_dependency_p,
5410 bool *template_p,
5411 bool declarator_p,
5412 bool optional_p)
5414 bool global_scope_p;
5415 bool nested_name_specifier_p;
5417 /* Assume the `template' keyword was not used. */
5418 if (template_p)
5419 *template_p = template_keyword_p;
5421 /* Look for the optional `::' operator. */
5422 global_scope_p
5423 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
5424 != NULL_TREE);
5425 /* Look for the optional nested-name-specifier. */
5426 nested_name_specifier_p
5427 = (cp_parser_nested_name_specifier_opt (parser,
5428 /*typename_keyword_p=*/false,
5429 check_dependency_p,
5430 /*type_p=*/false,
5431 declarator_p)
5432 != NULL_TREE);
5433 /* If there is a nested-name-specifier, then we are looking at
5434 the first qualified-id production. */
5435 if (nested_name_specifier_p)
5437 tree saved_scope;
5438 tree saved_object_scope;
5439 tree saved_qualifying_scope;
5440 cp_expr unqualified_id;
5441 bool is_template;
5443 /* See if the next token is the `template' keyword. */
5444 if (!template_p)
5445 template_p = &is_template;
5446 *template_p = cp_parser_optional_template_keyword (parser);
5447 /* Name lookup we do during the processing of the
5448 unqualified-id might obliterate SCOPE. */
5449 saved_scope = parser->scope;
5450 saved_object_scope = parser->object_scope;
5451 saved_qualifying_scope = parser->qualifying_scope;
5452 /* Process the final unqualified-id. */
5453 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5454 check_dependency_p,
5455 declarator_p,
5456 /*optional_p=*/false);
5457 /* Restore the SAVED_SCOPE for our caller. */
5458 parser->scope = saved_scope;
5459 parser->object_scope = saved_object_scope;
5460 parser->qualifying_scope = saved_qualifying_scope;
5462 return unqualified_id;
5464 /* Otherwise, if we are in global scope, then we are looking at one
5465 of the other qualified-id productions. */
5466 else if (global_scope_p)
5468 cp_token *token;
5469 tree id;
5471 /* Peek at the next token. */
5472 token = cp_lexer_peek_token (parser->lexer);
5474 /* If it's an identifier, and the next token is not a "<", then
5475 we can avoid the template-id case. This is an optimization
5476 for this common case. */
5477 if (token->type == CPP_NAME
5478 && !cp_parser_nth_token_starts_template_argument_list_p
5479 (parser, 2))
5480 return cp_parser_identifier (parser);
5482 cp_parser_parse_tentatively (parser);
5483 /* Try a template-id. */
5484 id = cp_parser_template_id (parser,
5485 /*template_keyword_p=*/false,
5486 /*check_dependency_p=*/true,
5487 none_type,
5488 declarator_p);
5489 /* If that worked, we're done. */
5490 if (cp_parser_parse_definitely (parser))
5491 return id;
5493 /* Peek at the next token. (Changes in the token buffer may
5494 have invalidated the pointer obtained above.) */
5495 token = cp_lexer_peek_token (parser->lexer);
5497 switch (token->type)
5499 case CPP_NAME:
5500 return cp_parser_identifier (parser);
5502 case CPP_KEYWORD:
5503 if (token->keyword == RID_OPERATOR)
5504 return cp_parser_operator_function_id (parser);
5505 /* Fall through. */
5507 default:
5508 cp_parser_error (parser, "expected id-expression");
5509 return error_mark_node;
5512 else
5513 return cp_parser_unqualified_id (parser, template_keyword_p,
5514 /*check_dependency_p=*/true,
5515 declarator_p,
5516 optional_p);
5519 /* Parse an unqualified-id.
5521 unqualified-id:
5522 identifier
5523 operator-function-id
5524 conversion-function-id
5525 ~ class-name
5526 template-id
5528 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5529 keyword, in a construct like `A::template ...'.
5531 Returns a representation of unqualified-id. For the `identifier'
5532 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5533 production a BIT_NOT_EXPR is returned; the operand of the
5534 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5535 other productions, see the documentation accompanying the
5536 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5537 names are looked up in uninstantiated templates. If DECLARATOR_P
5538 is true, the unqualified-id is appearing as part of a declarator,
5539 rather than as part of an expression. */
5541 static cp_expr
5542 cp_parser_unqualified_id (cp_parser* parser,
5543 bool template_keyword_p,
5544 bool check_dependency_p,
5545 bool declarator_p,
5546 bool optional_p)
5548 cp_token *token;
5550 /* Peek at the next token. */
5551 token = cp_lexer_peek_token (parser->lexer);
5553 switch ((int) token->type)
5555 case CPP_NAME:
5557 tree id;
5559 /* We don't know yet whether or not this will be a
5560 template-id. */
5561 cp_parser_parse_tentatively (parser);
5562 /* Try a template-id. */
5563 id = cp_parser_template_id (parser, template_keyword_p,
5564 check_dependency_p,
5565 none_type,
5566 declarator_p);
5567 /* If it worked, we're done. */
5568 if (cp_parser_parse_definitely (parser))
5569 return id;
5570 /* Otherwise, it's an ordinary identifier. */
5571 return cp_parser_identifier (parser);
5574 case CPP_TEMPLATE_ID:
5575 return cp_parser_template_id (parser, template_keyword_p,
5576 check_dependency_p,
5577 none_type,
5578 declarator_p);
5580 case CPP_COMPL:
5582 tree type_decl;
5583 tree qualifying_scope;
5584 tree object_scope;
5585 tree scope;
5586 bool done;
5588 /* Consume the `~' token. */
5589 cp_lexer_consume_token (parser->lexer);
5590 /* Parse the class-name. The standard, as written, seems to
5591 say that:
5593 template <typename T> struct S { ~S (); };
5594 template <typename T> S<T>::~S() {}
5596 is invalid, since `~' must be followed by a class-name, but
5597 `S<T>' is dependent, and so not known to be a class.
5598 That's not right; we need to look in uninstantiated
5599 templates. A further complication arises from:
5601 template <typename T> void f(T t) {
5602 t.T::~T();
5605 Here, it is not possible to look up `T' in the scope of `T'
5606 itself. We must look in both the current scope, and the
5607 scope of the containing complete expression.
5609 Yet another issue is:
5611 struct S {
5612 int S;
5613 ~S();
5616 S::~S() {}
5618 The standard does not seem to say that the `S' in `~S'
5619 should refer to the type `S' and not the data member
5620 `S::S'. */
5622 /* DR 244 says that we look up the name after the "~" in the
5623 same scope as we looked up the qualifying name. That idea
5624 isn't fully worked out; it's more complicated than that. */
5625 scope = parser->scope;
5626 object_scope = parser->object_scope;
5627 qualifying_scope = parser->qualifying_scope;
5629 /* Check for invalid scopes. */
5630 if (scope == error_mark_node)
5632 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5633 cp_lexer_consume_token (parser->lexer);
5634 return error_mark_node;
5636 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5638 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5639 error_at (token->location,
5640 "scope %qT before %<~%> is not a class-name",
5641 scope);
5642 cp_parser_simulate_error (parser);
5643 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5644 cp_lexer_consume_token (parser->lexer);
5645 return error_mark_node;
5647 gcc_assert (!scope || TYPE_P (scope));
5649 /* If the name is of the form "X::~X" it's OK even if X is a
5650 typedef. */
5651 token = cp_lexer_peek_token (parser->lexer);
5652 if (scope
5653 && token->type == CPP_NAME
5654 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5655 != CPP_LESS)
5656 && (token->u.value == TYPE_IDENTIFIER (scope)
5657 || (CLASS_TYPE_P (scope)
5658 && constructor_name_p (token->u.value, scope))))
5660 cp_lexer_consume_token (parser->lexer);
5661 return build_nt (BIT_NOT_EXPR, scope);
5664 /* ~auto means the destructor of whatever the object is. */
5665 if (cp_parser_is_keyword (token, RID_AUTO))
5667 if (cxx_dialect < cxx14)
5668 pedwarn (input_location, 0,
5669 "%<~auto%> only available with "
5670 "-std=c++14 or -std=gnu++14");
5671 cp_lexer_consume_token (parser->lexer);
5672 return build_nt (BIT_NOT_EXPR, make_auto ());
5675 /* If there was an explicit qualification (S::~T), first look
5676 in the scope given by the qualification (i.e., S).
5678 Note: in the calls to cp_parser_class_name below we pass
5679 typename_type so that lookup finds the injected-class-name
5680 rather than the constructor. */
5681 done = false;
5682 type_decl = NULL_TREE;
5683 if (scope)
5685 cp_parser_parse_tentatively (parser);
5686 type_decl = cp_parser_class_name (parser,
5687 /*typename_keyword_p=*/false,
5688 /*template_keyword_p=*/false,
5689 typename_type,
5690 /*check_dependency=*/false,
5691 /*class_head_p=*/false,
5692 declarator_p);
5693 if (cp_parser_parse_definitely (parser))
5694 done = true;
5696 /* In "N::S::~S", look in "N" as well. */
5697 if (!done && scope && qualifying_scope)
5699 cp_parser_parse_tentatively (parser);
5700 parser->scope = qualifying_scope;
5701 parser->object_scope = NULL_TREE;
5702 parser->qualifying_scope = NULL_TREE;
5703 type_decl
5704 = cp_parser_class_name (parser,
5705 /*typename_keyword_p=*/false,
5706 /*template_keyword_p=*/false,
5707 typename_type,
5708 /*check_dependency=*/false,
5709 /*class_head_p=*/false,
5710 declarator_p);
5711 if (cp_parser_parse_definitely (parser))
5712 done = true;
5714 /* In "p->S::~T", look in the scope given by "*p" as well. */
5715 else if (!done && object_scope)
5717 cp_parser_parse_tentatively (parser);
5718 parser->scope = object_scope;
5719 parser->object_scope = NULL_TREE;
5720 parser->qualifying_scope = NULL_TREE;
5721 type_decl
5722 = cp_parser_class_name (parser,
5723 /*typename_keyword_p=*/false,
5724 /*template_keyword_p=*/false,
5725 typename_type,
5726 /*check_dependency=*/false,
5727 /*class_head_p=*/false,
5728 declarator_p);
5729 if (cp_parser_parse_definitely (parser))
5730 done = true;
5732 /* Look in the surrounding context. */
5733 if (!done)
5735 parser->scope = NULL_TREE;
5736 parser->object_scope = NULL_TREE;
5737 parser->qualifying_scope = NULL_TREE;
5738 if (processing_template_decl)
5739 cp_parser_parse_tentatively (parser);
5740 type_decl
5741 = cp_parser_class_name (parser,
5742 /*typename_keyword_p=*/false,
5743 /*template_keyword_p=*/false,
5744 typename_type,
5745 /*check_dependency=*/false,
5746 /*class_head_p=*/false,
5747 declarator_p);
5748 if (processing_template_decl
5749 && ! cp_parser_parse_definitely (parser))
5751 /* We couldn't find a type with this name. If we're parsing
5752 tentatively, fail and try something else. */
5753 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5755 cp_parser_simulate_error (parser);
5756 return error_mark_node;
5758 /* Otherwise, accept it and check for a match at instantiation
5759 time. */
5760 type_decl = cp_parser_identifier (parser);
5761 if (type_decl != error_mark_node)
5762 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5763 return type_decl;
5766 /* If an error occurred, assume that the name of the
5767 destructor is the same as the name of the qualifying
5768 class. That allows us to keep parsing after running
5769 into ill-formed destructor names. */
5770 if (type_decl == error_mark_node && scope)
5771 return build_nt (BIT_NOT_EXPR, scope);
5772 else if (type_decl == error_mark_node)
5773 return error_mark_node;
5775 /* Check that destructor name and scope match. */
5776 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5778 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5779 error_at (token->location,
5780 "declaration of %<~%T%> as member of %qT",
5781 type_decl, scope);
5782 cp_parser_simulate_error (parser);
5783 return error_mark_node;
5786 /* [class.dtor]
5788 A typedef-name that names a class shall not be used as the
5789 identifier in the declarator for a destructor declaration. */
5790 if (declarator_p
5791 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5792 && !DECL_SELF_REFERENCE_P (type_decl)
5793 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5794 error_at (token->location,
5795 "typedef-name %qD used as destructor declarator",
5796 type_decl);
5798 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5801 case CPP_KEYWORD:
5802 if (token->keyword == RID_OPERATOR)
5804 cp_expr id;
5806 /* This could be a template-id, so we try that first. */
5807 cp_parser_parse_tentatively (parser);
5808 /* Try a template-id. */
5809 id = cp_parser_template_id (parser, template_keyword_p,
5810 /*check_dependency_p=*/true,
5811 none_type,
5812 declarator_p);
5813 /* If that worked, we're done. */
5814 if (cp_parser_parse_definitely (parser))
5815 return id;
5816 /* We still don't know whether we're looking at an
5817 operator-function-id or a conversion-function-id. */
5818 cp_parser_parse_tentatively (parser);
5819 /* Try an operator-function-id. */
5820 id = cp_parser_operator_function_id (parser);
5821 /* If that didn't work, try a conversion-function-id. */
5822 if (!cp_parser_parse_definitely (parser))
5823 id = cp_parser_conversion_function_id (parser);
5824 else if (UDLIT_OPER_P (id))
5826 /* 17.6.3.3.5 */
5827 const char *name = UDLIT_OP_SUFFIX (id);
5828 if (name[0] != '_' && !in_system_header_at (input_location)
5829 && declarator_p)
5830 warning (OPT_Wliteral_suffix,
5831 "literal operator suffixes not preceded by %<_%>"
5832 " are reserved for future standardization");
5835 return id;
5837 /* Fall through. */
5839 default:
5840 if (optional_p)
5841 return NULL_TREE;
5842 cp_parser_error (parser, "expected unqualified-id");
5843 return error_mark_node;
5847 /* Parse an (optional) nested-name-specifier.
5849 nested-name-specifier: [C++98]
5850 class-or-namespace-name :: nested-name-specifier [opt]
5851 class-or-namespace-name :: template nested-name-specifier [opt]
5853 nested-name-specifier: [C++0x]
5854 type-name ::
5855 namespace-name ::
5856 nested-name-specifier identifier ::
5857 nested-name-specifier template [opt] simple-template-id ::
5859 PARSER->SCOPE should be set appropriately before this function is
5860 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5861 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5862 in name lookups.
5864 Sets PARSER->SCOPE to the class (TYPE) or namespace
5865 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5866 it unchanged if there is no nested-name-specifier. Returns the new
5867 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5869 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5870 part of a declaration and/or decl-specifier. */
5872 static tree
5873 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5874 bool typename_keyword_p,
5875 bool check_dependency_p,
5876 bool type_p,
5877 bool is_declaration)
5879 bool success = false;
5880 cp_token_position start = 0;
5881 cp_token *token;
5883 /* Remember where the nested-name-specifier starts. */
5884 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5886 start = cp_lexer_token_position (parser->lexer, false);
5887 push_deferring_access_checks (dk_deferred);
5890 while (true)
5892 tree new_scope;
5893 tree old_scope;
5894 tree saved_qualifying_scope;
5895 bool template_keyword_p;
5897 /* Spot cases that cannot be the beginning of a
5898 nested-name-specifier. */
5899 token = cp_lexer_peek_token (parser->lexer);
5901 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5902 the already parsed nested-name-specifier. */
5903 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5905 /* Grab the nested-name-specifier and continue the loop. */
5906 cp_parser_pre_parsed_nested_name_specifier (parser);
5907 /* If we originally encountered this nested-name-specifier
5908 with IS_DECLARATION set to false, we will not have
5909 resolved TYPENAME_TYPEs, so we must do so here. */
5910 if (is_declaration
5911 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5913 new_scope = resolve_typename_type (parser->scope,
5914 /*only_current_p=*/false);
5915 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5916 parser->scope = new_scope;
5918 success = true;
5919 continue;
5922 /* Spot cases that cannot be the beginning of a
5923 nested-name-specifier. On the second and subsequent times
5924 through the loop, we look for the `template' keyword. */
5925 if (success && token->keyword == RID_TEMPLATE)
5927 /* A template-id can start a nested-name-specifier. */
5928 else if (token->type == CPP_TEMPLATE_ID)
5930 /* DR 743: decltype can be used in a nested-name-specifier. */
5931 else if (token_is_decltype (token))
5933 else
5935 /* If the next token is not an identifier, then it is
5936 definitely not a type-name or namespace-name. */
5937 if (token->type != CPP_NAME)
5938 break;
5939 /* If the following token is neither a `<' (to begin a
5940 template-id), nor a `::', then we are not looking at a
5941 nested-name-specifier. */
5942 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5944 if (token->type == CPP_COLON
5945 && parser->colon_corrects_to_scope_p
5946 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5948 gcc_rich_location richloc (token->location);
5949 richloc.add_fixit_replace ("::");
5950 error_at_rich_loc (&richloc,
5951 "found %<:%> in nested-name-specifier, "
5952 "expected %<::%>");
5953 token->type = CPP_SCOPE;
5956 if (token->type != CPP_SCOPE
5957 && !cp_parser_nth_token_starts_template_argument_list_p
5958 (parser, 2))
5959 break;
5962 /* The nested-name-specifier is optional, so we parse
5963 tentatively. */
5964 cp_parser_parse_tentatively (parser);
5966 /* Look for the optional `template' keyword, if this isn't the
5967 first time through the loop. */
5968 if (success)
5969 template_keyword_p = cp_parser_optional_template_keyword (parser);
5970 else
5971 template_keyword_p = false;
5973 /* Save the old scope since the name lookup we are about to do
5974 might destroy it. */
5975 old_scope = parser->scope;
5976 saved_qualifying_scope = parser->qualifying_scope;
5977 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5978 look up names in "X<T>::I" in order to determine that "Y" is
5979 a template. So, if we have a typename at this point, we make
5980 an effort to look through it. */
5981 if (is_declaration
5982 && !typename_keyword_p
5983 && parser->scope
5984 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5985 parser->scope = resolve_typename_type (parser->scope,
5986 /*only_current_p=*/false);
5987 /* Parse the qualifying entity. */
5988 new_scope
5989 = cp_parser_qualifying_entity (parser,
5990 typename_keyword_p,
5991 template_keyword_p,
5992 check_dependency_p,
5993 type_p,
5994 is_declaration);
5995 /* Look for the `::' token. */
5996 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5998 /* If we found what we wanted, we keep going; otherwise, we're
5999 done. */
6000 if (!cp_parser_parse_definitely (parser))
6002 bool error_p = false;
6004 /* Restore the OLD_SCOPE since it was valid before the
6005 failed attempt at finding the last
6006 class-or-namespace-name. */
6007 parser->scope = old_scope;
6008 parser->qualifying_scope = saved_qualifying_scope;
6010 /* If the next token is a decltype, and the one after that is a
6011 `::', then the decltype has failed to resolve to a class or
6012 enumeration type. Give this error even when parsing
6013 tentatively since it can't possibly be valid--and we're going
6014 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6015 won't get another chance.*/
6016 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6017 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6018 == CPP_SCOPE))
6020 token = cp_lexer_consume_token (parser->lexer);
6021 error_at (token->location, "decltype evaluates to %qT, "
6022 "which is not a class or enumeration type",
6023 token->u.tree_check_value->value);
6024 parser->scope = error_mark_node;
6025 error_p = true;
6026 /* As below. */
6027 success = true;
6028 cp_lexer_consume_token (parser->lexer);
6031 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6032 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6034 /* If we have a non-type template-id followed by ::, it can't
6035 possibly be valid. */
6036 token = cp_lexer_peek_token (parser->lexer);
6037 tree tid = token->u.tree_check_value->value;
6038 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6039 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6041 tree tmpl = NULL_TREE;
6042 if (is_overloaded_fn (tid))
6044 tree fns = get_fns (tid);
6045 if (OVL_SINGLE_P (fns))
6046 tmpl = OVL_FIRST (fns);
6047 error_at (token->location, "function template-id %qD "
6048 "in nested-name-specifier", tid);
6050 else
6052 /* Variable template. */
6053 tmpl = TREE_OPERAND (tid, 0);
6054 gcc_assert (variable_template_p (tmpl));
6055 error_at (token->location, "variable template-id %qD "
6056 "in nested-name-specifier", tid);
6058 if (tmpl)
6059 inform (DECL_SOURCE_LOCATION (tmpl),
6060 "%qD declared here", tmpl);
6062 parser->scope = error_mark_node;
6063 error_p = true;
6064 /* As below. */
6065 success = true;
6066 cp_lexer_consume_token (parser->lexer);
6067 cp_lexer_consume_token (parser->lexer);
6071 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6072 break;
6073 /* If the next token is an identifier, and the one after
6074 that is a `::', then any valid interpretation would have
6075 found a class-or-namespace-name. */
6076 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6077 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6078 == CPP_SCOPE)
6079 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6080 != CPP_COMPL))
6082 token = cp_lexer_consume_token (parser->lexer);
6083 if (!error_p)
6085 if (!token->error_reported)
6087 tree decl;
6088 tree ambiguous_decls;
6090 decl = cp_parser_lookup_name (parser, token->u.value,
6091 none_type,
6092 /*is_template=*/false,
6093 /*is_namespace=*/false,
6094 /*check_dependency=*/true,
6095 &ambiguous_decls,
6096 token->location);
6097 if (TREE_CODE (decl) == TEMPLATE_DECL)
6098 error_at (token->location,
6099 "%qD used without template parameters",
6100 decl);
6101 else if (ambiguous_decls)
6103 // cp_parser_lookup_name has the same diagnostic,
6104 // thus make sure to emit it at most once.
6105 if (cp_parser_uncommitted_to_tentative_parse_p
6106 (parser))
6108 error_at (token->location,
6109 "reference to %qD is ambiguous",
6110 token->u.value);
6111 print_candidates (ambiguous_decls);
6113 decl = error_mark_node;
6115 else
6117 if (cxx_dialect != cxx98)
6118 cp_parser_name_lookup_error
6119 (parser, token->u.value, decl, NLE_NOT_CXX98,
6120 token->location);
6121 else
6122 cp_parser_name_lookup_error
6123 (parser, token->u.value, decl, NLE_CXX98,
6124 token->location);
6127 parser->scope = error_mark_node;
6128 error_p = true;
6129 /* Treat this as a successful nested-name-specifier
6130 due to:
6132 [basic.lookup.qual]
6134 If the name found is not a class-name (clause
6135 _class_) or namespace-name (_namespace.def_), the
6136 program is ill-formed. */
6137 success = true;
6139 cp_lexer_consume_token (parser->lexer);
6141 break;
6143 /* We've found one valid nested-name-specifier. */
6144 success = true;
6145 /* Name lookup always gives us a DECL. */
6146 if (TREE_CODE (new_scope) == TYPE_DECL)
6147 new_scope = TREE_TYPE (new_scope);
6148 /* Uses of "template" must be followed by actual templates. */
6149 if (template_keyword_p
6150 && !(CLASS_TYPE_P (new_scope)
6151 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6152 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6153 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6154 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6155 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6156 == TEMPLATE_ID_EXPR)))
6157 permerror (input_location, TYPE_P (new_scope)
6158 ? G_("%qT is not a template")
6159 : G_("%qD is not a template"),
6160 new_scope);
6161 /* If it is a class scope, try to complete it; we are about to
6162 be looking up names inside the class. */
6163 if (TYPE_P (new_scope)
6164 /* Since checking types for dependency can be expensive,
6165 avoid doing it if the type is already complete. */
6166 && !COMPLETE_TYPE_P (new_scope)
6167 /* Do not try to complete dependent types. */
6168 && !dependent_type_p (new_scope))
6170 new_scope = complete_type (new_scope);
6171 /* If it is a typedef to current class, use the current
6172 class instead, as the typedef won't have any names inside
6173 it yet. */
6174 if (!COMPLETE_TYPE_P (new_scope)
6175 && currently_open_class (new_scope))
6176 new_scope = TYPE_MAIN_VARIANT (new_scope);
6178 /* Make sure we look in the right scope the next time through
6179 the loop. */
6180 parser->scope = new_scope;
6183 /* If parsing tentatively, replace the sequence of tokens that makes
6184 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6185 token. That way, should we re-parse the token stream, we will
6186 not have to repeat the effort required to do the parse, nor will
6187 we issue duplicate error messages. */
6188 if (success && start)
6190 cp_token *token;
6192 token = cp_lexer_token_at (parser->lexer, start);
6193 /* Reset the contents of the START token. */
6194 token->type = CPP_NESTED_NAME_SPECIFIER;
6195 /* Retrieve any deferred checks. Do not pop this access checks yet
6196 so the memory will not be reclaimed during token replacing below. */
6197 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6198 token->u.tree_check_value->value = parser->scope;
6199 token->u.tree_check_value->checks = get_deferred_access_checks ();
6200 token->u.tree_check_value->qualifying_scope =
6201 parser->qualifying_scope;
6202 token->keyword = RID_MAX;
6204 /* Purge all subsequent tokens. */
6205 cp_lexer_purge_tokens_after (parser->lexer, start);
6208 if (start)
6209 pop_to_parent_deferring_access_checks ();
6211 return success ? parser->scope : NULL_TREE;
6214 /* Parse a nested-name-specifier. See
6215 cp_parser_nested_name_specifier_opt for details. This function
6216 behaves identically, except that it will an issue an error if no
6217 nested-name-specifier is present. */
6219 static tree
6220 cp_parser_nested_name_specifier (cp_parser *parser,
6221 bool typename_keyword_p,
6222 bool check_dependency_p,
6223 bool type_p,
6224 bool is_declaration)
6226 tree scope;
6228 /* Look for the nested-name-specifier. */
6229 scope = cp_parser_nested_name_specifier_opt (parser,
6230 typename_keyword_p,
6231 check_dependency_p,
6232 type_p,
6233 is_declaration);
6234 /* If it was not present, issue an error message. */
6235 if (!scope)
6237 cp_parser_error (parser, "expected nested-name-specifier");
6238 parser->scope = NULL_TREE;
6241 return scope;
6244 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6245 this is either a class-name or a namespace-name (which corresponds
6246 to the class-or-namespace-name production in the grammar). For
6247 C++0x, it can also be a type-name that refers to an enumeration
6248 type or a simple-template-id.
6250 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6251 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6252 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6253 TYPE_P is TRUE iff the next name should be taken as a class-name,
6254 even the same name is declared to be another entity in the same
6255 scope.
6257 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6258 specified by the class-or-namespace-name. If neither is found the
6259 ERROR_MARK_NODE is returned. */
6261 static tree
6262 cp_parser_qualifying_entity (cp_parser *parser,
6263 bool typename_keyword_p,
6264 bool template_keyword_p,
6265 bool check_dependency_p,
6266 bool type_p,
6267 bool is_declaration)
6269 tree saved_scope;
6270 tree saved_qualifying_scope;
6271 tree saved_object_scope;
6272 tree scope;
6273 bool only_class_p;
6274 bool successful_parse_p;
6276 /* DR 743: decltype can appear in a nested-name-specifier. */
6277 if (cp_lexer_next_token_is_decltype (parser->lexer))
6279 scope = cp_parser_decltype (parser);
6280 if (TREE_CODE (scope) != ENUMERAL_TYPE
6281 && !MAYBE_CLASS_TYPE_P (scope))
6283 cp_parser_simulate_error (parser);
6284 return error_mark_node;
6286 if (TYPE_NAME (scope))
6287 scope = TYPE_NAME (scope);
6288 return scope;
6291 /* Before we try to parse the class-name, we must save away the
6292 current PARSER->SCOPE since cp_parser_class_name will destroy
6293 it. */
6294 saved_scope = parser->scope;
6295 saved_qualifying_scope = parser->qualifying_scope;
6296 saved_object_scope = parser->object_scope;
6297 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6298 there is no need to look for a namespace-name. */
6299 only_class_p = template_keyword_p
6300 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6301 if (!only_class_p)
6302 cp_parser_parse_tentatively (parser);
6303 scope = cp_parser_class_name (parser,
6304 typename_keyword_p,
6305 template_keyword_p,
6306 type_p ? class_type : none_type,
6307 check_dependency_p,
6308 /*class_head_p=*/false,
6309 is_declaration,
6310 /*enum_ok=*/cxx_dialect > cxx98);
6311 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6312 /* If that didn't work, try for a namespace-name. */
6313 if (!only_class_p && !successful_parse_p)
6315 /* Restore the saved scope. */
6316 parser->scope = saved_scope;
6317 parser->qualifying_scope = saved_qualifying_scope;
6318 parser->object_scope = saved_object_scope;
6319 /* If we are not looking at an identifier followed by the scope
6320 resolution operator, then this is not part of a
6321 nested-name-specifier. (Note that this function is only used
6322 to parse the components of a nested-name-specifier.) */
6323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6324 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6325 return error_mark_node;
6326 scope = cp_parser_namespace_name (parser);
6329 return scope;
6332 /* Return true if we are looking at a compound-literal, false otherwise. */
6334 static bool
6335 cp_parser_compound_literal_p (cp_parser *parser)
6337 /* Consume the `('. */
6338 cp_lexer_consume_token (parser->lexer);
6340 cp_lexer_save_tokens (parser->lexer);
6342 /* Skip tokens until the next token is a closing parenthesis.
6343 If we find the closing `)', and the next token is a `{', then
6344 we are looking at a compound-literal. */
6345 bool compound_literal_p
6346 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6347 /*consume_paren=*/true)
6348 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6350 /* Roll back the tokens we skipped. */
6351 cp_lexer_rollback_tokens (parser->lexer);
6353 return compound_literal_p;
6356 /* Parse a postfix-expression.
6358 postfix-expression:
6359 primary-expression
6360 postfix-expression [ expression ]
6361 postfix-expression ( expression-list [opt] )
6362 simple-type-specifier ( expression-list [opt] )
6363 typename :: [opt] nested-name-specifier identifier
6364 ( expression-list [opt] )
6365 typename :: [opt] nested-name-specifier template [opt] template-id
6366 ( expression-list [opt] )
6367 postfix-expression . template [opt] id-expression
6368 postfix-expression -> template [opt] id-expression
6369 postfix-expression . pseudo-destructor-name
6370 postfix-expression -> pseudo-destructor-name
6371 postfix-expression ++
6372 postfix-expression --
6373 dynamic_cast < type-id > ( expression )
6374 static_cast < type-id > ( expression )
6375 reinterpret_cast < type-id > ( expression )
6376 const_cast < type-id > ( expression )
6377 typeid ( expression )
6378 typeid ( type-id )
6380 GNU Extension:
6382 postfix-expression:
6383 ( type-id ) { initializer-list , [opt] }
6385 This extension is a GNU version of the C99 compound-literal
6386 construct. (The C99 grammar uses `type-name' instead of `type-id',
6387 but they are essentially the same concept.)
6389 If ADDRESS_P is true, the postfix expression is the operand of the
6390 `&' operator. CAST_P is true if this expression is the target of a
6391 cast.
6393 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6394 class member access expressions [expr.ref].
6396 Returns a representation of the expression. */
6398 static cp_expr
6399 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6400 bool member_access_only_p, bool decltype_p,
6401 cp_id_kind * pidk_return)
6403 cp_token *token;
6404 location_t loc;
6405 enum rid keyword;
6406 cp_id_kind idk = CP_ID_KIND_NONE;
6407 cp_expr postfix_expression = NULL_TREE;
6408 bool is_member_access = false;
6409 int saved_in_statement = -1;
6411 /* Peek at the next token. */
6412 token = cp_lexer_peek_token (parser->lexer);
6413 loc = token->location;
6414 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6416 /* Some of the productions are determined by keywords. */
6417 keyword = token->keyword;
6418 switch (keyword)
6420 case RID_DYNCAST:
6421 case RID_STATCAST:
6422 case RID_REINTCAST:
6423 case RID_CONSTCAST:
6425 tree type;
6426 cp_expr expression;
6427 const char *saved_message;
6428 bool saved_in_type_id_in_expr_p;
6430 /* All of these can be handled in the same way from the point
6431 of view of parsing. Begin by consuming the token
6432 identifying the cast. */
6433 cp_lexer_consume_token (parser->lexer);
6435 /* New types cannot be defined in the cast. */
6436 saved_message = parser->type_definition_forbidden_message;
6437 parser->type_definition_forbidden_message
6438 = G_("types may not be defined in casts");
6440 /* Look for the opening `<'. */
6441 cp_parser_require (parser, CPP_LESS, RT_LESS);
6442 /* Parse the type to which we are casting. */
6443 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6444 parser->in_type_id_in_expr_p = true;
6445 type = cp_parser_type_id (parser);
6446 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6447 /* Look for the closing `>'. */
6448 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6449 /* Restore the old message. */
6450 parser->type_definition_forbidden_message = saved_message;
6452 bool saved_greater_than_is_operator_p
6453 = parser->greater_than_is_operator_p;
6454 parser->greater_than_is_operator_p = true;
6456 /* And the expression which is being cast. */
6457 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6458 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6459 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6460 RT_CLOSE_PAREN);
6461 location_t end_loc = close_paren ?
6462 close_paren->location : UNKNOWN_LOCATION;
6464 parser->greater_than_is_operator_p
6465 = saved_greater_than_is_operator_p;
6467 /* Only type conversions to integral or enumeration types
6468 can be used in constant-expressions. */
6469 if (!cast_valid_in_integral_constant_expression_p (type)
6470 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6472 postfix_expression = error_mark_node;
6473 break;
6476 switch (keyword)
6478 case RID_DYNCAST:
6479 postfix_expression
6480 = build_dynamic_cast (type, expression, tf_warning_or_error);
6481 break;
6482 case RID_STATCAST:
6483 postfix_expression
6484 = build_static_cast (type, expression, tf_warning_or_error);
6485 break;
6486 case RID_REINTCAST:
6487 postfix_expression
6488 = build_reinterpret_cast (type, expression,
6489 tf_warning_or_error);
6490 break;
6491 case RID_CONSTCAST:
6492 postfix_expression
6493 = build_const_cast (type, expression, tf_warning_or_error);
6494 break;
6495 default:
6496 gcc_unreachable ();
6499 /* Construct a location e.g. :
6500 reinterpret_cast <int *> (expr)
6501 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6502 ranging from the start of the "*_cast" token to the final closing
6503 paren, with the caret at the start. */
6504 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6505 postfix_expression.set_location (cp_cast_loc);
6507 break;
6509 case RID_TYPEID:
6511 tree type;
6512 const char *saved_message;
6513 bool saved_in_type_id_in_expr_p;
6515 /* Consume the `typeid' token. */
6516 cp_lexer_consume_token (parser->lexer);
6517 /* Look for the `(' token. */
6518 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6519 /* Types cannot be defined in a `typeid' expression. */
6520 saved_message = parser->type_definition_forbidden_message;
6521 parser->type_definition_forbidden_message
6522 = G_("types may not be defined in a %<typeid%> expression");
6523 /* We can't be sure yet whether we're looking at a type-id or an
6524 expression. */
6525 cp_parser_parse_tentatively (parser);
6526 /* Try a type-id first. */
6527 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6528 parser->in_type_id_in_expr_p = true;
6529 type = cp_parser_type_id (parser);
6530 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6531 /* Look for the `)' token. Otherwise, we can't be sure that
6532 we're not looking at an expression: consider `typeid (int
6533 (3))', for example. */
6534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6535 /* If all went well, simply lookup the type-id. */
6536 if (cp_parser_parse_definitely (parser))
6537 postfix_expression = get_typeid (type, tf_warning_or_error);
6538 /* Otherwise, fall back to the expression variant. */
6539 else
6541 tree expression;
6543 /* Look for an expression. */
6544 expression = cp_parser_expression (parser, & idk);
6545 /* Compute its typeid. */
6546 postfix_expression = build_typeid (expression, tf_warning_or_error);
6547 /* Look for the `)' token. */
6548 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6550 /* Restore the saved message. */
6551 parser->type_definition_forbidden_message = saved_message;
6552 /* `typeid' may not appear in an integral constant expression. */
6553 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6554 postfix_expression = error_mark_node;
6556 break;
6558 case RID_TYPENAME:
6560 tree type;
6561 /* The syntax permitted here is the same permitted for an
6562 elaborated-type-specifier. */
6563 ++parser->prevent_constrained_type_specifiers;
6564 type = cp_parser_elaborated_type_specifier (parser,
6565 /*is_friend=*/false,
6566 /*is_declaration=*/false);
6567 --parser->prevent_constrained_type_specifiers;
6568 postfix_expression = cp_parser_functional_cast (parser, type);
6570 break;
6572 case RID_CILK_SPAWN:
6574 location_t cilk_spawn_loc
6575 = cp_lexer_peek_token (parser->lexer)->location;
6576 cp_lexer_consume_token (parser->lexer);
6577 token = cp_lexer_peek_token (parser->lexer);
6578 if (token->type == CPP_SEMICOLON)
6580 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6581 "an expression");
6582 postfix_expression = error_mark_node;
6583 break;
6585 else if (!current_function_decl)
6587 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6588 "inside a function");
6589 postfix_expression = error_mark_node;
6590 break;
6592 else
6594 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6595 saved_in_statement = parser->in_statement;
6596 parser->in_statement |= IN_CILK_SPAWN;
6598 cfun->calls_cilk_spawn = 1;
6599 postfix_expression =
6600 cp_parser_postfix_expression (parser, false, false,
6601 false, false, &idk);
6602 if (!flag_cilkplus)
6604 error_at (token->location, "-fcilkplus must be enabled to use"
6605 " %<_Cilk_spawn%>");
6606 cfun->calls_cilk_spawn = 0;
6608 else if (saved_in_statement & IN_CILK_SPAWN)
6610 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6611 "are not permitted");
6612 postfix_expression = error_mark_node;
6613 cfun->calls_cilk_spawn = 0;
6615 else
6617 location_t loc = postfix_expression.get_location ();
6618 postfix_expression = build_cilk_spawn (token->location,
6619 postfix_expression);
6620 /* Build a location of the form:
6621 _Cilk_spawn expr
6622 ~~~~~~~~~~~~^~~~
6623 with caret at the expr, ranging from the start of the
6624 _Cilk_spawn token to the end of the expression. */
6625 location_t combined_loc =
6626 make_location (loc, cilk_spawn_loc, get_finish (loc));
6627 postfix_expression.set_location (combined_loc);
6628 if (postfix_expression != error_mark_node)
6629 SET_EXPR_LOCATION (postfix_expression, input_location);
6630 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6632 break;
6635 case RID_ADDRESSOF:
6636 case RID_BUILTIN_SHUFFLE:
6637 case RID_BUILTIN_LAUNDER:
6639 vec<tree, va_gc> *vec;
6640 unsigned int i;
6641 tree p;
6643 cp_lexer_consume_token (parser->lexer);
6644 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6645 /*cast_p=*/false, /*allow_expansion_p=*/true,
6646 /*non_constant_p=*/NULL);
6647 if (vec == NULL)
6649 postfix_expression = error_mark_node;
6650 break;
6653 FOR_EACH_VEC_ELT (*vec, i, p)
6654 mark_exp_read (p);
6656 switch (keyword)
6658 case RID_ADDRESSOF:
6659 if (vec->length () == 1)
6660 postfix_expression
6661 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6662 else
6664 error_at (loc, "wrong number of arguments to "
6665 "%<__builtin_addressof%>");
6666 postfix_expression = error_mark_node;
6668 break;
6670 case RID_BUILTIN_LAUNDER:
6671 if (vec->length () == 1)
6672 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6673 tf_warning_or_error);
6674 else
6676 error_at (loc, "wrong number of arguments to "
6677 "%<__builtin_launder%>");
6678 postfix_expression = error_mark_node;
6680 break;
6682 case RID_BUILTIN_SHUFFLE:
6683 if (vec->length () == 2)
6684 postfix_expression
6685 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6686 (*vec)[1], tf_warning_or_error);
6687 else if (vec->length () == 3)
6688 postfix_expression
6689 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6690 (*vec)[2], tf_warning_or_error);
6691 else
6693 error_at (loc, "wrong number of arguments to "
6694 "%<__builtin_shuffle%>");
6695 postfix_expression = error_mark_node;
6697 break;
6699 default:
6700 gcc_unreachable ();
6702 break;
6705 default:
6707 tree type;
6709 /* If the next thing is a simple-type-specifier, we may be
6710 looking at a functional cast. We could also be looking at
6711 an id-expression. So, we try the functional cast, and if
6712 that doesn't work we fall back to the primary-expression. */
6713 cp_parser_parse_tentatively (parser);
6714 /* Look for the simple-type-specifier. */
6715 ++parser->prevent_constrained_type_specifiers;
6716 type = cp_parser_simple_type_specifier (parser,
6717 /*decl_specs=*/NULL,
6718 CP_PARSER_FLAGS_NONE);
6719 --parser->prevent_constrained_type_specifiers;
6720 /* Parse the cast itself. */
6721 if (!cp_parser_error_occurred (parser))
6722 postfix_expression
6723 = cp_parser_functional_cast (parser, type);
6724 /* If that worked, we're done. */
6725 if (cp_parser_parse_definitely (parser))
6726 break;
6728 /* If the functional-cast didn't work out, try a
6729 compound-literal. */
6730 if (cp_parser_allow_gnu_extensions_p (parser)
6731 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6733 cp_expr initializer = NULL_TREE;
6735 cp_parser_parse_tentatively (parser);
6737 /* Avoid calling cp_parser_type_id pointlessly, see comment
6738 in cp_parser_cast_expression about c++/29234. */
6739 if (!cp_parser_compound_literal_p (parser))
6740 cp_parser_simulate_error (parser);
6741 else
6743 /* Parse the type. */
6744 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6745 parser->in_type_id_in_expr_p = true;
6746 type = cp_parser_type_id (parser);
6747 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6748 /* Look for the `)'. */
6749 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6752 /* If things aren't going well, there's no need to
6753 keep going. */
6754 if (!cp_parser_error_occurred (parser))
6756 bool non_constant_p;
6757 /* Parse the brace-enclosed initializer list. */
6758 initializer = cp_parser_braced_list (parser,
6759 &non_constant_p);
6761 /* If that worked, we're definitely looking at a
6762 compound-literal expression. */
6763 if (cp_parser_parse_definitely (parser))
6765 /* Warn the user that a compound literal is not
6766 allowed in standard C++. */
6767 pedwarn (input_location, OPT_Wpedantic,
6768 "ISO C++ forbids compound-literals");
6769 /* For simplicity, we disallow compound literals in
6770 constant-expressions. We could
6771 allow compound literals of integer type, whose
6772 initializer was a constant, in constant
6773 expressions. Permitting that usage, as a further
6774 extension, would not change the meaning of any
6775 currently accepted programs. (Of course, as
6776 compound literals are not part of ISO C++, the
6777 standard has nothing to say.) */
6778 if (cp_parser_non_integral_constant_expression (parser,
6779 NIC_NCC))
6781 postfix_expression = error_mark_node;
6782 break;
6784 /* Form the representation of the compound-literal. */
6785 postfix_expression
6786 = finish_compound_literal (type, initializer,
6787 tf_warning_or_error, fcl_c99);
6788 postfix_expression.set_location (initializer.get_location ());
6789 break;
6793 /* It must be a primary-expression. */
6794 postfix_expression
6795 = cp_parser_primary_expression (parser, address_p, cast_p,
6796 /*template_arg_p=*/false,
6797 decltype_p,
6798 &idk);
6800 break;
6803 /* Note that we don't need to worry about calling build_cplus_new on a
6804 class-valued CALL_EXPR in decltype when it isn't the end of the
6805 postfix-expression; unary_complex_lvalue will take care of that for
6806 all these cases. */
6808 /* Keep looping until the postfix-expression is complete. */
6809 while (true)
6811 if (idk == CP_ID_KIND_UNQUALIFIED
6812 && identifier_p (postfix_expression)
6813 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6814 /* It is not a Koenig lookup function call. */
6815 postfix_expression
6816 = unqualified_name_lookup_error (postfix_expression);
6818 /* Peek at the next token. */
6819 token = cp_lexer_peek_token (parser->lexer);
6821 switch (token->type)
6823 case CPP_OPEN_SQUARE:
6824 if (cp_next_tokens_can_be_std_attribute_p (parser))
6826 cp_parser_error (parser,
6827 "two consecutive %<[%> shall "
6828 "only introduce an attribute");
6829 return error_mark_node;
6831 postfix_expression
6832 = cp_parser_postfix_open_square_expression (parser,
6833 postfix_expression,
6834 false,
6835 decltype_p);
6836 postfix_expression.set_range (start_loc,
6837 postfix_expression.get_location ());
6839 idk = CP_ID_KIND_NONE;
6840 is_member_access = false;
6841 break;
6843 case CPP_OPEN_PAREN:
6844 /* postfix-expression ( expression-list [opt] ) */
6846 bool koenig_p;
6847 bool is_builtin_constant_p;
6848 bool saved_integral_constant_expression_p = false;
6849 bool saved_non_integral_constant_expression_p = false;
6850 tsubst_flags_t complain = complain_flags (decltype_p);
6851 vec<tree, va_gc> *args;
6852 location_t close_paren_loc = UNKNOWN_LOCATION;
6854 is_member_access = false;
6856 is_builtin_constant_p
6857 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6858 if (is_builtin_constant_p)
6860 /* The whole point of __builtin_constant_p is to allow
6861 non-constant expressions to appear as arguments. */
6862 saved_integral_constant_expression_p
6863 = parser->integral_constant_expression_p;
6864 saved_non_integral_constant_expression_p
6865 = parser->non_integral_constant_expression_p;
6866 parser->integral_constant_expression_p = false;
6868 args = (cp_parser_parenthesized_expression_list
6869 (parser, non_attr,
6870 /*cast_p=*/false, /*allow_expansion_p=*/true,
6871 /*non_constant_p=*/NULL,
6872 /*close_paren_loc=*/&close_paren_loc));
6873 if (is_builtin_constant_p)
6875 parser->integral_constant_expression_p
6876 = saved_integral_constant_expression_p;
6877 parser->non_integral_constant_expression_p
6878 = saved_non_integral_constant_expression_p;
6881 if (args == NULL)
6883 postfix_expression = error_mark_node;
6884 break;
6887 /* Function calls are not permitted in
6888 constant-expressions. */
6889 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6890 && cp_parser_non_integral_constant_expression (parser,
6891 NIC_FUNC_CALL))
6893 postfix_expression = error_mark_node;
6894 release_tree_vector (args);
6895 break;
6898 koenig_p = false;
6899 if (idk == CP_ID_KIND_UNQUALIFIED
6900 || idk == CP_ID_KIND_TEMPLATE_ID)
6902 if (identifier_p (postfix_expression))
6904 if (!args->is_empty ())
6906 koenig_p = true;
6907 if (!any_type_dependent_arguments_p (args))
6908 postfix_expression
6909 = perform_koenig_lookup (postfix_expression, args,
6910 complain);
6912 else
6913 postfix_expression
6914 = unqualified_fn_lookup_error (postfix_expression);
6916 /* We do not perform argument-dependent lookup if
6917 normal lookup finds a non-function, in accordance
6918 with the expected resolution of DR 218. */
6919 else if (!args->is_empty ()
6920 && is_overloaded_fn (postfix_expression))
6922 tree fn = get_first_fn (postfix_expression);
6923 fn = STRIP_TEMPLATE (fn);
6925 /* Do not do argument dependent lookup if regular
6926 lookup finds a member function or a block-scope
6927 function declaration. [basic.lookup.argdep]/3 */
6928 if (!DECL_FUNCTION_MEMBER_P (fn)
6929 && !DECL_LOCAL_FUNCTION_P (fn))
6931 koenig_p = true;
6932 if (!any_type_dependent_arguments_p (args))
6933 postfix_expression
6934 = perform_koenig_lookup (postfix_expression, args,
6935 complain);
6940 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6941 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6942 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6943 && vec_safe_length (args) == 3)
6945 tree arg0 = (*args)[0];
6946 tree arg1 = (*args)[1];
6947 tree arg2 = (*args)[2];
6948 int literal_mask = ((!!integer_zerop (arg1) << 1)
6949 | (!!integer_zerop (arg2) << 2));
6950 if (TREE_CODE (arg2) == CONST_DECL)
6951 arg2 = DECL_INITIAL (arg2);
6952 warn_for_memset (input_location, arg0, arg2, literal_mask);
6955 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6957 tree instance = TREE_OPERAND (postfix_expression, 0);
6958 tree fn = TREE_OPERAND (postfix_expression, 1);
6960 if (processing_template_decl
6961 && (type_dependent_object_expression_p (instance)
6962 || (!BASELINK_P (fn)
6963 && TREE_CODE (fn) != FIELD_DECL)
6964 || type_dependent_expression_p (fn)
6965 || any_type_dependent_arguments_p (args)))
6967 maybe_generic_this_capture (instance, fn);
6968 postfix_expression
6969 = build_min_nt_call_vec (postfix_expression, args);
6970 release_tree_vector (args);
6971 break;
6974 if (BASELINK_P (fn))
6976 postfix_expression
6977 = (build_new_method_call
6978 (instance, fn, &args, NULL_TREE,
6979 (idk == CP_ID_KIND_QUALIFIED
6980 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
6981 : LOOKUP_NORMAL),
6982 /*fn_p=*/NULL,
6983 complain));
6985 else
6986 postfix_expression
6987 = finish_call_expr (postfix_expression, &args,
6988 /*disallow_virtual=*/false,
6989 /*koenig_p=*/false,
6990 complain);
6992 else if (TREE_CODE (postfix_expression) == OFFSET_REF
6993 || TREE_CODE (postfix_expression) == MEMBER_REF
6994 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
6995 postfix_expression = (build_offset_ref_call_from_tree
6996 (postfix_expression, &args,
6997 complain));
6998 else if (idk == CP_ID_KIND_QUALIFIED)
6999 /* A call to a static class member, or a namespace-scope
7000 function. */
7001 postfix_expression
7002 = finish_call_expr (postfix_expression, &args,
7003 /*disallow_virtual=*/true,
7004 koenig_p,
7005 complain);
7006 else
7007 /* All other function calls. */
7008 postfix_expression
7009 = finish_call_expr (postfix_expression, &args,
7010 /*disallow_virtual=*/false,
7011 koenig_p,
7012 complain);
7014 if (close_paren_loc != UNKNOWN_LOCATION)
7016 location_t combined_loc = make_location (token->location,
7017 start_loc,
7018 close_paren_loc);
7019 postfix_expression.set_location (combined_loc);
7022 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7023 idk = CP_ID_KIND_NONE;
7025 release_tree_vector (args);
7027 break;
7029 case CPP_DOT:
7030 case CPP_DEREF:
7031 /* postfix-expression . template [opt] id-expression
7032 postfix-expression . pseudo-destructor-name
7033 postfix-expression -> template [opt] id-expression
7034 postfix-expression -> pseudo-destructor-name */
7036 /* Consume the `.' or `->' operator. */
7037 cp_lexer_consume_token (parser->lexer);
7039 postfix_expression
7040 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7041 postfix_expression,
7042 false, &idk, loc);
7044 is_member_access = true;
7045 break;
7047 case CPP_PLUS_PLUS:
7048 /* postfix-expression ++ */
7049 /* Consume the `++' token. */
7050 cp_lexer_consume_token (parser->lexer);
7051 /* Generate a representation for the complete expression. */
7052 postfix_expression
7053 = finish_increment_expr (postfix_expression,
7054 POSTINCREMENT_EXPR);
7055 /* Increments may not appear in constant-expressions. */
7056 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7057 postfix_expression = error_mark_node;
7058 idk = CP_ID_KIND_NONE;
7059 is_member_access = false;
7060 break;
7062 case CPP_MINUS_MINUS:
7063 /* postfix-expression -- */
7064 /* Consume the `--' token. */
7065 cp_lexer_consume_token (parser->lexer);
7066 /* Generate a representation for the complete expression. */
7067 postfix_expression
7068 = finish_increment_expr (postfix_expression,
7069 POSTDECREMENT_EXPR);
7070 /* Decrements may not appear in constant-expressions. */
7071 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7072 postfix_expression = error_mark_node;
7073 idk = CP_ID_KIND_NONE;
7074 is_member_access = false;
7075 break;
7077 default:
7078 if (pidk_return != NULL)
7079 * pidk_return = idk;
7080 if (member_access_only_p)
7081 return is_member_access
7082 ? postfix_expression
7083 : cp_expr (error_mark_node);
7084 else
7085 return postfix_expression;
7089 /* We should never get here. */
7090 gcc_unreachable ();
7091 return error_mark_node;
7094 /* This function parses Cilk Plus array notations. If a normal array expr. is
7095 parsed then the array index is passed back to the caller through *INIT_INDEX
7096 and the function returns a NULL_TREE. If array notation expr. is parsed,
7097 then *INIT_INDEX is ignored by the caller and the function returns
7098 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7099 error_mark_node. */
7101 static tree
7102 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7103 tree array_value)
7105 cp_token *token = NULL;
7106 tree length_index, stride = NULL_TREE, value_tree, array_type;
7107 if (!array_value || array_value == error_mark_node)
7109 cp_parser_skip_to_end_of_statement (parser);
7110 return error_mark_node;
7113 array_type = TREE_TYPE (array_value);
7115 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7116 parser->colon_corrects_to_scope_p = false;
7117 token = cp_lexer_peek_token (parser->lexer);
7119 if (!token)
7121 cp_parser_error (parser, "expected %<:%> or numeral");
7122 return error_mark_node;
7124 else if (token->type == CPP_COLON)
7126 /* Consume the ':'. */
7127 cp_lexer_consume_token (parser->lexer);
7129 /* If we are here, then we have a case like this A[:]. */
7130 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7132 cp_parser_error (parser, "expected %<]%>");
7133 cp_parser_skip_to_end_of_statement (parser);
7134 return error_mark_node;
7136 *init_index = NULL_TREE;
7137 stride = NULL_TREE;
7138 length_index = NULL_TREE;
7140 else
7142 /* If we are here, then there are three valid possibilities:
7143 1. ARRAY [ EXP ]
7144 2. ARRAY [ EXP : EXP ]
7145 3. ARRAY [ EXP : EXP : EXP ] */
7147 *init_index = cp_parser_expression (parser);
7148 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7150 /* This indicates that we have a normal array expression. */
7151 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7152 return NULL_TREE;
7155 /* Consume the ':'. */
7156 cp_lexer_consume_token (parser->lexer);
7157 length_index = cp_parser_expression (parser);
7158 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7160 cp_lexer_consume_token (parser->lexer);
7161 stride = cp_parser_expression (parser);
7164 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7166 if (*init_index == error_mark_node || length_index == error_mark_node
7167 || stride == error_mark_node || array_type == error_mark_node)
7169 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7170 cp_lexer_consume_token (parser->lexer);
7171 return error_mark_node;
7173 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7175 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7176 length_index, stride, array_type);
7177 return value_tree;
7180 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7181 by cp_parser_builtin_offsetof. We're looking for
7183 postfix-expression [ expression ]
7184 postfix-expression [ braced-init-list ] (C++11)
7186 FOR_OFFSETOF is set if we're being called in that context, which
7187 changes how we deal with integer constant expressions. */
7189 static tree
7190 cp_parser_postfix_open_square_expression (cp_parser *parser,
7191 tree postfix_expression,
7192 bool for_offsetof,
7193 bool decltype_p)
7195 tree index = NULL_TREE;
7196 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7197 bool saved_greater_than_is_operator_p;
7199 /* Consume the `[' token. */
7200 cp_lexer_consume_token (parser->lexer);
7202 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7203 parser->greater_than_is_operator_p = true;
7205 /* Parse the index expression. */
7206 /* ??? For offsetof, there is a question of what to allow here. If
7207 offsetof is not being used in an integral constant expression context,
7208 then we *could* get the right answer by computing the value at runtime.
7209 If we are in an integral constant expression context, then we might
7210 could accept any constant expression; hard to say without analysis.
7211 Rather than open the barn door too wide right away, allow only integer
7212 constant expressions here. */
7213 if (for_offsetof)
7214 index = cp_parser_constant_expression (parser);
7215 else
7217 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7219 bool expr_nonconst_p;
7220 cp_lexer_set_source_position (parser->lexer);
7221 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7222 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7223 if (flag_cilkplus
7224 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7226 error_at (cp_lexer_peek_token (parser->lexer)->location,
7227 "braced list index is not allowed with array "
7228 "notation");
7229 cp_parser_skip_to_end_of_statement (parser);
7230 return error_mark_node;
7233 else if (flag_cilkplus)
7235 /* Here are have these two options:
7236 ARRAY[EXP : EXP] - Array notation expr with default
7237 stride of 1.
7238 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7239 stride. */
7240 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7241 postfix_expression);
7242 if (an_exp)
7243 return an_exp;
7245 else
7246 index = cp_parser_expression (parser);
7249 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7251 /* Look for the closing `]'. */
7252 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7254 /* Build the ARRAY_REF. */
7255 postfix_expression = grok_array_decl (loc, postfix_expression,
7256 index, decltype_p);
7258 /* When not doing offsetof, array references are not permitted in
7259 constant-expressions. */
7260 if (!for_offsetof
7261 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7262 postfix_expression = error_mark_node;
7264 return postfix_expression;
7267 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7268 by cp_parser_builtin_offsetof. We're looking for
7270 postfix-expression . template [opt] id-expression
7271 postfix-expression . pseudo-destructor-name
7272 postfix-expression -> template [opt] id-expression
7273 postfix-expression -> pseudo-destructor-name
7275 FOR_OFFSETOF is set if we're being called in that context. That sorta
7276 limits what of the above we'll actually accept, but nevermind.
7277 TOKEN_TYPE is the "." or "->" token, which will already have been
7278 removed from the stream. */
7280 static tree
7281 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7282 enum cpp_ttype token_type,
7283 cp_expr postfix_expression,
7284 bool for_offsetof, cp_id_kind *idk,
7285 location_t location)
7287 tree name;
7288 bool dependent_p;
7289 bool pseudo_destructor_p;
7290 tree scope = NULL_TREE;
7291 location_t start_loc = postfix_expression.get_start ();
7293 /* If this is a `->' operator, dereference the pointer. */
7294 if (token_type == CPP_DEREF)
7295 postfix_expression = build_x_arrow (location, postfix_expression,
7296 tf_warning_or_error);
7297 /* Check to see whether or not the expression is type-dependent and
7298 not the current instantiation. */
7299 dependent_p = type_dependent_object_expression_p (postfix_expression);
7300 /* The identifier following the `->' or `.' is not qualified. */
7301 parser->scope = NULL_TREE;
7302 parser->qualifying_scope = NULL_TREE;
7303 parser->object_scope = NULL_TREE;
7304 *idk = CP_ID_KIND_NONE;
7306 /* Enter the scope corresponding to the type of the object
7307 given by the POSTFIX_EXPRESSION. */
7308 if (!dependent_p)
7310 scope = TREE_TYPE (postfix_expression);
7311 /* According to the standard, no expression should ever have
7312 reference type. Unfortunately, we do not currently match
7313 the standard in this respect in that our internal representation
7314 of an expression may have reference type even when the standard
7315 says it does not. Therefore, we have to manually obtain the
7316 underlying type here. */
7317 scope = non_reference (scope);
7318 /* The type of the POSTFIX_EXPRESSION must be complete. */
7319 /* Unlike the object expression in other contexts, *this is not
7320 required to be of complete type for purposes of class member
7321 access (5.2.5) outside the member function body. */
7322 if (postfix_expression != current_class_ref
7323 && scope != error_mark_node
7324 && !(processing_template_decl
7325 && current_class_type
7326 && (same_type_ignoring_top_level_qualifiers_p
7327 (scope, current_class_type))))
7329 scope = complete_type (scope);
7330 if (!COMPLETE_TYPE_P (scope)
7331 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7332 && EXPR_P (postfix_expression))
7334 /* In a template, be permissive by treating an object expression
7335 of incomplete type as dependent (after a pedwarn). */
7336 diagnostic_t kind = (processing_template_decl
7337 ? DK_PEDWARN
7338 : DK_ERROR);
7339 cxx_incomplete_type_diagnostic
7340 (location_of (postfix_expression),
7341 postfix_expression, scope, kind);
7342 if (processing_template_decl)
7344 dependent_p = true;
7345 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7350 if (!dependent_p)
7352 /* Let the name lookup machinery know that we are processing a
7353 class member access expression. */
7354 parser->context->object_type = scope;
7355 /* If something went wrong, we want to be able to discern that case,
7356 as opposed to the case where there was no SCOPE due to the type
7357 of expression being dependent. */
7358 if (!scope)
7359 scope = error_mark_node;
7360 /* If the SCOPE was erroneous, make the various semantic analysis
7361 functions exit quickly -- and without issuing additional error
7362 messages. */
7363 if (scope == error_mark_node)
7364 postfix_expression = error_mark_node;
7368 if (dependent_p)
7369 /* Tell cp_parser_lookup_name that there was an object, even though it's
7370 type-dependent. */
7371 parser->context->object_type = unknown_type_node;
7373 /* Assume this expression is not a pseudo-destructor access. */
7374 pseudo_destructor_p = false;
7376 /* If the SCOPE is a scalar type, then, if this is a valid program,
7377 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7378 is type dependent, it can be pseudo-destructor-name or something else.
7379 Try to parse it as pseudo-destructor-name first. */
7380 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7382 tree s;
7383 tree type;
7385 cp_parser_parse_tentatively (parser);
7386 /* Parse the pseudo-destructor-name. */
7387 s = NULL_TREE;
7388 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7389 &s, &type);
7390 if (dependent_p
7391 && (cp_parser_error_occurred (parser)
7392 || !SCALAR_TYPE_P (type)))
7393 cp_parser_abort_tentative_parse (parser);
7394 else if (cp_parser_parse_definitely (parser))
7396 pseudo_destructor_p = true;
7397 postfix_expression
7398 = finish_pseudo_destructor_expr (postfix_expression,
7399 s, type, location);
7403 if (!pseudo_destructor_p)
7405 /* If the SCOPE is not a scalar type, we are looking at an
7406 ordinary class member access expression, rather than a
7407 pseudo-destructor-name. */
7408 bool template_p;
7409 cp_token *token = cp_lexer_peek_token (parser->lexer);
7410 /* Parse the id-expression. */
7411 name = (cp_parser_id_expression
7412 (parser,
7413 cp_parser_optional_template_keyword (parser),
7414 /*check_dependency_p=*/true,
7415 &template_p,
7416 /*declarator_p=*/false,
7417 /*optional_p=*/false));
7418 /* In general, build a SCOPE_REF if the member name is qualified.
7419 However, if the name was not dependent and has already been
7420 resolved; there is no need to build the SCOPE_REF. For example;
7422 struct X { void f(); };
7423 template <typename T> void f(T* t) { t->X::f(); }
7425 Even though "t" is dependent, "X::f" is not and has been resolved
7426 to a BASELINK; there is no need to include scope information. */
7428 /* But we do need to remember that there was an explicit scope for
7429 virtual function calls. */
7430 if (parser->scope)
7431 *idk = CP_ID_KIND_QUALIFIED;
7433 /* If the name is a template-id that names a type, we will get a
7434 TYPE_DECL here. That is invalid code. */
7435 if (TREE_CODE (name) == TYPE_DECL)
7437 error_at (token->location, "invalid use of %qD", name);
7438 postfix_expression = error_mark_node;
7440 else
7442 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7444 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7446 error_at (token->location, "%<%D::%D%> is not a class member",
7447 parser->scope, name);
7448 postfix_expression = error_mark_node;
7450 else
7451 name = build_qualified_name (/*type=*/NULL_TREE,
7452 parser->scope,
7453 name,
7454 template_p);
7455 parser->scope = NULL_TREE;
7456 parser->qualifying_scope = NULL_TREE;
7457 parser->object_scope = NULL_TREE;
7459 if (parser->scope && name && BASELINK_P (name))
7460 adjust_result_of_qualified_name_lookup
7461 (name, parser->scope, scope);
7462 postfix_expression
7463 = finish_class_member_access_expr (postfix_expression, name,
7464 template_p,
7465 tf_warning_or_error);
7466 /* Build a location e.g.:
7467 ptr->access_expr
7468 ~~~^~~~~~~~~~~~~
7469 where the caret is at the deref token, ranging from
7470 the start of postfix_expression to the end of the access expr. */
7471 location_t end_loc
7472 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7473 location_t combined_loc
7474 = make_location (input_location, start_loc, end_loc);
7475 protected_set_expr_location (postfix_expression, combined_loc);
7479 /* We no longer need to look up names in the scope of the object on
7480 the left-hand side of the `.' or `->' operator. */
7481 parser->context->object_type = NULL_TREE;
7483 /* Outside of offsetof, these operators may not appear in
7484 constant-expressions. */
7485 if (!for_offsetof
7486 && (cp_parser_non_integral_constant_expression
7487 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7488 postfix_expression = error_mark_node;
7490 return postfix_expression;
7493 /* Parse a parenthesized expression-list.
7495 expression-list:
7496 assignment-expression
7497 expression-list, assignment-expression
7499 attribute-list:
7500 expression-list
7501 identifier
7502 identifier, expression-list
7504 CAST_P is true if this expression is the target of a cast.
7506 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7507 argument pack.
7509 Returns a vector of trees. Each element is a representation of an
7510 assignment-expression. NULL is returned if the ( and or ) are
7511 missing. An empty, but allocated, vector is returned on no
7512 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7513 if we are parsing an attribute list for an attribute that wants a
7514 plain identifier argument, normal_attr for an attribute that wants
7515 an expression, or non_attr if we aren't parsing an attribute list. If
7516 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7517 not all of the expressions in the list were constant.
7518 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7519 will be written to with the location of the closing parenthesis. If
7520 an error occurs, it may or may not be written to. */
7522 static vec<tree, va_gc> *
7523 cp_parser_parenthesized_expression_list (cp_parser* parser,
7524 int is_attribute_list,
7525 bool cast_p,
7526 bool allow_expansion_p,
7527 bool *non_constant_p,
7528 location_t *close_paren_loc)
7530 vec<tree, va_gc> *expression_list;
7531 bool fold_expr_p = is_attribute_list != non_attr;
7532 tree identifier = NULL_TREE;
7533 bool saved_greater_than_is_operator_p;
7535 /* Assume all the expressions will be constant. */
7536 if (non_constant_p)
7537 *non_constant_p = false;
7539 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7540 return NULL;
7542 expression_list = make_tree_vector ();
7544 /* Within a parenthesized expression, a `>' token is always
7545 the greater-than operator. */
7546 saved_greater_than_is_operator_p
7547 = parser->greater_than_is_operator_p;
7548 parser->greater_than_is_operator_p = true;
7550 /* Consume expressions until there are no more. */
7551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7552 while (true)
7554 tree expr;
7556 /* At the beginning of attribute lists, check to see if the
7557 next token is an identifier. */
7558 if (is_attribute_list == id_attr
7559 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7561 cp_token *token;
7563 /* Consume the identifier. */
7564 token = cp_lexer_consume_token (parser->lexer);
7565 /* Save the identifier. */
7566 identifier = token->u.value;
7568 else
7570 bool expr_non_constant_p;
7572 /* Parse the next assignment-expression. */
7573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7575 /* A braced-init-list. */
7576 cp_lexer_set_source_position (parser->lexer);
7577 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7578 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7579 if (non_constant_p && expr_non_constant_p)
7580 *non_constant_p = true;
7582 else if (non_constant_p)
7584 expr = (cp_parser_constant_expression
7585 (parser, /*allow_non_constant_p=*/true,
7586 &expr_non_constant_p));
7587 if (expr_non_constant_p)
7588 *non_constant_p = true;
7590 else
7591 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7592 cast_p);
7594 if (fold_expr_p)
7595 expr = instantiate_non_dependent_expr (expr);
7597 /* If we have an ellipsis, then this is an expression
7598 expansion. */
7599 if (allow_expansion_p
7600 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7602 /* Consume the `...'. */
7603 cp_lexer_consume_token (parser->lexer);
7605 /* Build the argument pack. */
7606 expr = make_pack_expansion (expr);
7609 /* Add it to the list. We add error_mark_node
7610 expressions to the list, so that we can still tell if
7611 the correct form for a parenthesized expression-list
7612 is found. That gives better errors. */
7613 vec_safe_push (expression_list, expr);
7615 if (expr == error_mark_node)
7616 goto skip_comma;
7619 /* After the first item, attribute lists look the same as
7620 expression lists. */
7621 is_attribute_list = non_attr;
7623 get_comma:;
7624 /* If the next token isn't a `,', then we are done. */
7625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7626 break;
7628 /* Otherwise, consume the `,' and keep going. */
7629 cp_lexer_consume_token (parser->lexer);
7632 if (close_paren_loc)
7633 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7635 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7637 int ending;
7639 skip_comma:;
7640 /* We try and resync to an unnested comma, as that will give the
7641 user better diagnostics. */
7642 ending = cp_parser_skip_to_closing_parenthesis (parser,
7643 /*recovering=*/true,
7644 /*or_comma=*/true,
7645 /*consume_paren=*/true);
7646 if (ending < 0)
7647 goto get_comma;
7648 if (!ending)
7650 parser->greater_than_is_operator_p
7651 = saved_greater_than_is_operator_p;
7652 return NULL;
7656 parser->greater_than_is_operator_p
7657 = saved_greater_than_is_operator_p;
7659 if (identifier)
7660 vec_safe_insert (expression_list, 0, identifier);
7662 return expression_list;
7665 /* Parse a pseudo-destructor-name.
7667 pseudo-destructor-name:
7668 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7669 :: [opt] nested-name-specifier template template-id :: ~ type-name
7670 :: [opt] nested-name-specifier [opt] ~ type-name
7672 If either of the first two productions is used, sets *SCOPE to the
7673 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7674 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7675 or ERROR_MARK_NODE if the parse fails. */
7677 static void
7678 cp_parser_pseudo_destructor_name (cp_parser* parser,
7679 tree object,
7680 tree* scope,
7681 tree* type)
7683 bool nested_name_specifier_p;
7685 /* Handle ~auto. */
7686 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7687 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7688 && !type_dependent_expression_p (object))
7690 if (cxx_dialect < cxx14)
7691 pedwarn (input_location, 0,
7692 "%<~auto%> only available with "
7693 "-std=c++14 or -std=gnu++14");
7694 cp_lexer_consume_token (parser->lexer);
7695 cp_lexer_consume_token (parser->lexer);
7696 *scope = NULL_TREE;
7697 *type = TREE_TYPE (object);
7698 return;
7701 /* Assume that things will not work out. */
7702 *type = error_mark_node;
7704 /* Look for the optional `::' operator. */
7705 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7706 /* Look for the optional nested-name-specifier. */
7707 nested_name_specifier_p
7708 = (cp_parser_nested_name_specifier_opt (parser,
7709 /*typename_keyword_p=*/false,
7710 /*check_dependency_p=*/true,
7711 /*type_p=*/false,
7712 /*is_declaration=*/false)
7713 != NULL_TREE);
7714 /* Now, if we saw a nested-name-specifier, we might be doing the
7715 second production. */
7716 if (nested_name_specifier_p
7717 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7719 /* Consume the `template' keyword. */
7720 cp_lexer_consume_token (parser->lexer);
7721 /* Parse the template-id. */
7722 cp_parser_template_id (parser,
7723 /*template_keyword_p=*/true,
7724 /*check_dependency_p=*/false,
7725 class_type,
7726 /*is_declaration=*/true);
7727 /* Look for the `::' token. */
7728 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7730 /* If the next token is not a `~', then there might be some
7731 additional qualification. */
7732 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7734 /* At this point, we're looking for "type-name :: ~". The type-name
7735 must not be a class-name, since this is a pseudo-destructor. So,
7736 it must be either an enum-name, or a typedef-name -- both of which
7737 are just identifiers. So, we peek ahead to check that the "::"
7738 and "~" tokens are present; if they are not, then we can avoid
7739 calling type_name. */
7740 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7741 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7742 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7744 cp_parser_error (parser, "non-scalar type");
7745 return;
7748 /* Look for the type-name. */
7749 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7750 if (*scope == error_mark_node)
7751 return;
7753 /* Look for the `::' token. */
7754 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7756 else
7757 *scope = NULL_TREE;
7759 /* Look for the `~'. */
7760 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7762 /* Once we see the ~, this has to be a pseudo-destructor. */
7763 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7764 cp_parser_commit_to_topmost_tentative_parse (parser);
7766 /* Look for the type-name again. We are not responsible for
7767 checking that it matches the first type-name. */
7768 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7771 /* Parse a unary-expression.
7773 unary-expression:
7774 postfix-expression
7775 ++ cast-expression
7776 -- cast-expression
7777 unary-operator cast-expression
7778 sizeof unary-expression
7779 sizeof ( type-id )
7780 alignof ( type-id ) [C++0x]
7781 new-expression
7782 delete-expression
7784 GNU Extensions:
7786 unary-expression:
7787 __extension__ cast-expression
7788 __alignof__ unary-expression
7789 __alignof__ ( type-id )
7790 alignof unary-expression [C++0x]
7791 __real__ cast-expression
7792 __imag__ cast-expression
7793 && identifier
7794 sizeof ( type-id ) { initializer-list , [opt] }
7795 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7796 __alignof__ ( type-id ) { initializer-list , [opt] }
7798 ADDRESS_P is true iff the unary-expression is appearing as the
7799 operand of the `&' operator. CAST_P is true if this expression is
7800 the target of a cast.
7802 Returns a representation of the expression. */
7804 static cp_expr
7805 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7806 bool address_p, bool cast_p, bool decltype_p)
7808 cp_token *token;
7809 enum tree_code unary_operator;
7811 /* Peek at the next token. */
7812 token = cp_lexer_peek_token (parser->lexer);
7813 /* Some keywords give away the kind of expression. */
7814 if (token->type == CPP_KEYWORD)
7816 enum rid keyword = token->keyword;
7818 switch (keyword)
7820 case RID_ALIGNOF:
7821 case RID_SIZEOF:
7823 tree operand, ret;
7824 enum tree_code op;
7825 location_t start_loc = token->location;
7827 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7828 /* Consume the token. */
7829 cp_lexer_consume_token (parser->lexer);
7830 /* Parse the operand. */
7831 operand = cp_parser_sizeof_operand (parser, keyword);
7833 if (TYPE_P (operand))
7834 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7835 else
7837 /* ISO C++ defines alignof only with types, not with
7838 expressions. So pedwarn if alignof is used with a non-
7839 type expression. However, __alignof__ is ok. */
7840 if (id_equal (token->u.value, "alignof"))
7841 pedwarn (token->location, OPT_Wpedantic,
7842 "ISO C++ does not allow %<alignof%> "
7843 "with a non-type");
7845 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7847 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7848 SIZEOF_EXPR with the original operand. */
7849 if (op == SIZEOF_EXPR && ret != error_mark_node)
7851 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7853 if (!processing_template_decl && TYPE_P (operand))
7855 ret = build_min (SIZEOF_EXPR, size_type_node,
7856 build1 (NOP_EXPR, operand,
7857 error_mark_node));
7858 SIZEOF_EXPR_TYPE_P (ret) = 1;
7860 else
7861 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7862 TREE_SIDE_EFFECTS (ret) = 0;
7863 TREE_READONLY (ret) = 1;
7867 /* Construct a location e.g. :
7868 alignof (expr)
7869 ^~~~~~~~~~~~~~
7870 with start == caret at the start of the "alignof"/"sizeof"
7871 token, with the endpoint at the final closing paren. */
7872 location_t finish_loc
7873 = cp_lexer_previous_token (parser->lexer)->location;
7874 location_t compound_loc
7875 = make_location (start_loc, start_loc, finish_loc);
7877 cp_expr ret_expr (ret);
7878 ret_expr.set_location (compound_loc);
7879 return ret_expr;
7882 case RID_NEW:
7883 return cp_parser_new_expression (parser);
7885 case RID_DELETE:
7886 return cp_parser_delete_expression (parser);
7888 case RID_EXTENSION:
7890 /* The saved value of the PEDANTIC flag. */
7891 int saved_pedantic;
7892 tree expr;
7894 /* Save away the PEDANTIC flag. */
7895 cp_parser_extension_opt (parser, &saved_pedantic);
7896 /* Parse the cast-expression. */
7897 expr = cp_parser_simple_cast_expression (parser);
7898 /* Restore the PEDANTIC flag. */
7899 pedantic = saved_pedantic;
7901 return expr;
7904 case RID_REALPART:
7905 case RID_IMAGPART:
7907 tree expression;
7909 /* Consume the `__real__' or `__imag__' token. */
7910 cp_lexer_consume_token (parser->lexer);
7911 /* Parse the cast-expression. */
7912 expression = cp_parser_simple_cast_expression (parser);
7913 /* Create the complete representation. */
7914 return build_x_unary_op (token->location,
7915 (keyword == RID_REALPART
7916 ? REALPART_EXPR : IMAGPART_EXPR),
7917 expression,
7918 tf_warning_or_error);
7920 break;
7922 case RID_TRANSACTION_ATOMIC:
7923 case RID_TRANSACTION_RELAXED:
7924 return cp_parser_transaction_expression (parser, keyword);
7926 case RID_NOEXCEPT:
7928 tree expr;
7929 const char *saved_message;
7930 bool saved_integral_constant_expression_p;
7931 bool saved_non_integral_constant_expression_p;
7932 bool saved_greater_than_is_operator_p;
7934 cp_lexer_consume_token (parser->lexer);
7935 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7937 saved_message = parser->type_definition_forbidden_message;
7938 parser->type_definition_forbidden_message
7939 = G_("types may not be defined in %<noexcept%> expressions");
7941 saved_integral_constant_expression_p
7942 = parser->integral_constant_expression_p;
7943 saved_non_integral_constant_expression_p
7944 = parser->non_integral_constant_expression_p;
7945 parser->integral_constant_expression_p = false;
7947 saved_greater_than_is_operator_p
7948 = parser->greater_than_is_operator_p;
7949 parser->greater_than_is_operator_p = true;
7951 ++cp_unevaluated_operand;
7952 ++c_inhibit_evaluation_warnings;
7953 ++cp_noexcept_operand;
7954 expr = cp_parser_expression (parser);
7955 --cp_noexcept_operand;
7956 --c_inhibit_evaluation_warnings;
7957 --cp_unevaluated_operand;
7959 parser->greater_than_is_operator_p
7960 = saved_greater_than_is_operator_p;
7962 parser->integral_constant_expression_p
7963 = saved_integral_constant_expression_p;
7964 parser->non_integral_constant_expression_p
7965 = saved_non_integral_constant_expression_p;
7967 parser->type_definition_forbidden_message = saved_message;
7969 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7970 return finish_noexcept_expr (expr, tf_warning_or_error);
7973 default:
7974 break;
7978 /* Look for the `:: new' and `:: delete', which also signal the
7979 beginning of a new-expression, or delete-expression,
7980 respectively. If the next token is `::', then it might be one of
7981 these. */
7982 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
7984 enum rid keyword;
7986 /* See if the token after the `::' is one of the keywords in
7987 which we're interested. */
7988 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
7989 /* If it's `new', we have a new-expression. */
7990 if (keyword == RID_NEW)
7991 return cp_parser_new_expression (parser);
7992 /* Similarly, for `delete'. */
7993 else if (keyword == RID_DELETE)
7994 return cp_parser_delete_expression (parser);
7997 /* Look for a unary operator. */
7998 unary_operator = cp_parser_unary_operator (token);
7999 /* The `++' and `--' operators can be handled similarly, even though
8000 they are not technically unary-operators in the grammar. */
8001 if (unary_operator == ERROR_MARK)
8003 if (token->type == CPP_PLUS_PLUS)
8004 unary_operator = PREINCREMENT_EXPR;
8005 else if (token->type == CPP_MINUS_MINUS)
8006 unary_operator = PREDECREMENT_EXPR;
8007 /* Handle the GNU address-of-label extension. */
8008 else if (cp_parser_allow_gnu_extensions_p (parser)
8009 && token->type == CPP_AND_AND)
8011 tree identifier;
8012 tree expression;
8013 location_t start_loc = token->location;
8015 /* Consume the '&&' token. */
8016 cp_lexer_consume_token (parser->lexer);
8017 /* Look for the identifier. */
8018 location_t finish_loc
8019 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8020 identifier = cp_parser_identifier (parser);
8021 /* Construct a location of the form:
8022 &&label
8023 ^~~~~~~
8024 with caret==start at the "&&", finish at the end of the label. */
8025 location_t combined_loc
8026 = make_location (start_loc, start_loc, finish_loc);
8027 /* Create an expression representing the address. */
8028 expression = finish_label_address_expr (identifier, combined_loc);
8029 if (cp_parser_non_integral_constant_expression (parser,
8030 NIC_ADDR_LABEL))
8031 expression = error_mark_node;
8032 return expression;
8035 if (unary_operator != ERROR_MARK)
8037 cp_expr cast_expression;
8038 cp_expr expression = error_mark_node;
8039 non_integral_constant non_constant_p = NIC_NONE;
8040 location_t loc = token->location;
8041 tsubst_flags_t complain = complain_flags (decltype_p);
8043 /* Consume the operator token. */
8044 token = cp_lexer_consume_token (parser->lexer);
8045 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8047 /* Parse the cast-expression. */
8048 cast_expression
8049 = cp_parser_cast_expression (parser,
8050 unary_operator == ADDR_EXPR,
8051 /*cast_p=*/false,
8052 /*decltype*/false,
8053 pidk);
8055 /* Make a location:
8056 OP_TOKEN CAST_EXPRESSION
8057 ^~~~~~~~~~~~~~~~~~~~~~~~~
8058 with start==caret at the operator token, and
8059 extending to the end of the cast_expression. */
8060 loc = make_location (loc, loc, cast_expression.get_finish ());
8062 /* Now, build an appropriate representation. */
8063 switch (unary_operator)
8065 case INDIRECT_REF:
8066 non_constant_p = NIC_STAR;
8067 expression = build_x_indirect_ref (loc, cast_expression,
8068 RO_UNARY_STAR,
8069 complain);
8070 /* TODO: build_x_indirect_ref does not always honor the
8071 location, so ensure it is set. */
8072 expression.set_location (loc);
8073 break;
8075 case ADDR_EXPR:
8076 non_constant_p = NIC_ADDR;
8077 /* Fall through. */
8078 case BIT_NOT_EXPR:
8079 expression = build_x_unary_op (loc, unary_operator,
8080 cast_expression,
8081 complain);
8082 /* TODO: build_x_unary_op does not always honor the location,
8083 so ensure it is set. */
8084 expression.set_location (loc);
8085 break;
8087 case PREINCREMENT_EXPR:
8088 case PREDECREMENT_EXPR:
8089 non_constant_p = unary_operator == PREINCREMENT_EXPR
8090 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8091 /* Fall through. */
8092 case NEGATE_EXPR:
8093 /* Immediately fold negation of a constant, unless the constant is 0
8094 (since -0 == 0) or it would overflow. */
8095 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8096 && CONSTANT_CLASS_P (cast_expression)
8097 && !integer_zerop (cast_expression)
8098 && !TREE_OVERFLOW (cast_expression))
8100 tree folded = fold_build1 (unary_operator,
8101 TREE_TYPE (cast_expression),
8102 cast_expression);
8103 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8105 expression = cp_expr (folded, loc);
8106 break;
8109 /* Fall through. */
8110 case UNARY_PLUS_EXPR:
8111 case TRUTH_NOT_EXPR:
8112 expression = finish_unary_op_expr (loc, unary_operator,
8113 cast_expression, complain);
8114 break;
8116 default:
8117 gcc_unreachable ();
8120 if (non_constant_p != NIC_NONE
8121 && cp_parser_non_integral_constant_expression (parser,
8122 non_constant_p))
8123 expression = error_mark_node;
8125 return expression;
8128 return cp_parser_postfix_expression (parser, address_p, cast_p,
8129 /*member_access_only_p=*/false,
8130 decltype_p,
8131 pidk);
8134 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8135 unary-operator, the corresponding tree code is returned. */
8137 static enum tree_code
8138 cp_parser_unary_operator (cp_token* token)
8140 switch (token->type)
8142 case CPP_MULT:
8143 return INDIRECT_REF;
8145 case CPP_AND:
8146 return ADDR_EXPR;
8148 case CPP_PLUS:
8149 return UNARY_PLUS_EXPR;
8151 case CPP_MINUS:
8152 return NEGATE_EXPR;
8154 case CPP_NOT:
8155 return TRUTH_NOT_EXPR;
8157 case CPP_COMPL:
8158 return BIT_NOT_EXPR;
8160 default:
8161 return ERROR_MARK;
8165 /* Parse a new-expression.
8167 new-expression:
8168 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8169 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8171 Returns a representation of the expression. */
8173 static tree
8174 cp_parser_new_expression (cp_parser* parser)
8176 bool global_scope_p;
8177 vec<tree, va_gc> *placement;
8178 tree type;
8179 vec<tree, va_gc> *initializer;
8180 tree nelts = NULL_TREE;
8181 tree ret;
8183 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8185 /* Look for the optional `::' operator. */
8186 global_scope_p
8187 = (cp_parser_global_scope_opt (parser,
8188 /*current_scope_valid_p=*/false)
8189 != NULL_TREE);
8190 /* Look for the `new' operator. */
8191 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8192 /* There's no easy way to tell a new-placement from the
8193 `( type-id )' construct. */
8194 cp_parser_parse_tentatively (parser);
8195 /* Look for a new-placement. */
8196 placement = cp_parser_new_placement (parser);
8197 /* If that didn't work out, there's no new-placement. */
8198 if (!cp_parser_parse_definitely (parser))
8200 if (placement != NULL)
8201 release_tree_vector (placement);
8202 placement = NULL;
8205 /* If the next token is a `(', then we have a parenthesized
8206 type-id. */
8207 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8209 cp_token *token;
8210 const char *saved_message = parser->type_definition_forbidden_message;
8212 /* Consume the `('. */
8213 cp_lexer_consume_token (parser->lexer);
8215 /* Parse the type-id. */
8216 parser->type_definition_forbidden_message
8217 = G_("types may not be defined in a new-expression");
8219 type_id_in_expr_sentinel s (parser);
8220 type = cp_parser_type_id (parser);
8222 parser->type_definition_forbidden_message = saved_message;
8224 /* Look for the closing `)'. */
8225 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8226 token = cp_lexer_peek_token (parser->lexer);
8227 /* There should not be a direct-new-declarator in this production,
8228 but GCC used to allowed this, so we check and emit a sensible error
8229 message for this case. */
8230 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8232 error_at (token->location,
8233 "array bound forbidden after parenthesized type-id");
8234 inform (token->location,
8235 "try removing the parentheses around the type-id");
8236 cp_parser_direct_new_declarator (parser);
8239 /* Otherwise, there must be a new-type-id. */
8240 else
8241 type = cp_parser_new_type_id (parser, &nelts);
8243 /* If the next token is a `(' or '{', then we have a new-initializer. */
8244 cp_token *token = cp_lexer_peek_token (parser->lexer);
8245 if (token->type == CPP_OPEN_PAREN
8246 || token->type == CPP_OPEN_BRACE)
8247 initializer = cp_parser_new_initializer (parser);
8248 else
8249 initializer = NULL;
8251 /* A new-expression may not appear in an integral constant
8252 expression. */
8253 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8254 ret = error_mark_node;
8255 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8256 of a new-type-id or type-id of a new-expression, the new-expression shall
8257 contain a new-initializer of the form ( assignment-expression )".
8258 Additionally, consistently with the spirit of DR 1467, we want to accept
8259 'new auto { 2 }' too. */
8260 else if ((ret = type_uses_auto (type))
8261 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8262 && (vec_safe_length (initializer) != 1
8263 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8264 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8266 error_at (token->location,
8267 "initialization of new-expression for type %<auto%> "
8268 "requires exactly one element");
8269 ret = error_mark_node;
8271 else
8273 /* Construct a location e.g.:
8274 ptr = new int[100]
8275 ^~~~~~~~~~~~
8276 with caret == start at the start of the "new" token, and the end
8277 at the end of the final token we consumed. */
8278 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8279 location_t end_loc = get_finish (end_tok->location);
8280 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8282 /* Create a representation of the new-expression. */
8283 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8284 tf_warning_or_error);
8285 protected_set_expr_location (ret, combined_loc);
8288 if (placement != NULL)
8289 release_tree_vector (placement);
8290 if (initializer != NULL)
8291 release_tree_vector (initializer);
8293 return ret;
8296 /* Parse a new-placement.
8298 new-placement:
8299 ( expression-list )
8301 Returns the same representation as for an expression-list. */
8303 static vec<tree, va_gc> *
8304 cp_parser_new_placement (cp_parser* parser)
8306 vec<tree, va_gc> *expression_list;
8308 /* Parse the expression-list. */
8309 expression_list = (cp_parser_parenthesized_expression_list
8310 (parser, non_attr, /*cast_p=*/false,
8311 /*allow_expansion_p=*/true,
8312 /*non_constant_p=*/NULL));
8314 if (expression_list && expression_list->is_empty ())
8315 error ("expected expression-list or type-id");
8317 return expression_list;
8320 /* Parse a new-type-id.
8322 new-type-id:
8323 type-specifier-seq new-declarator [opt]
8325 Returns the TYPE allocated. If the new-type-id indicates an array
8326 type, *NELTS is set to the number of elements in the last array
8327 bound; the TYPE will not include the last array bound. */
8329 static tree
8330 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8332 cp_decl_specifier_seq type_specifier_seq;
8333 cp_declarator *new_declarator;
8334 cp_declarator *declarator;
8335 cp_declarator *outer_declarator;
8336 const char *saved_message;
8338 /* The type-specifier sequence must not contain type definitions.
8339 (It cannot contain declarations of new types either, but if they
8340 are not definitions we will catch that because they are not
8341 complete.) */
8342 saved_message = parser->type_definition_forbidden_message;
8343 parser->type_definition_forbidden_message
8344 = G_("types may not be defined in a new-type-id");
8345 /* Parse the type-specifier-seq. */
8346 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8347 /*is_trailing_return=*/false,
8348 &type_specifier_seq);
8349 /* Restore the old message. */
8350 parser->type_definition_forbidden_message = saved_message;
8352 if (type_specifier_seq.type == error_mark_node)
8353 return error_mark_node;
8355 /* Parse the new-declarator. */
8356 new_declarator = cp_parser_new_declarator_opt (parser);
8358 /* Determine the number of elements in the last array dimension, if
8359 any. */
8360 *nelts = NULL_TREE;
8361 /* Skip down to the last array dimension. */
8362 declarator = new_declarator;
8363 outer_declarator = NULL;
8364 while (declarator && (declarator->kind == cdk_pointer
8365 || declarator->kind == cdk_ptrmem))
8367 outer_declarator = declarator;
8368 declarator = declarator->declarator;
8370 while (declarator
8371 && declarator->kind == cdk_array
8372 && declarator->declarator
8373 && declarator->declarator->kind == cdk_array)
8375 outer_declarator = declarator;
8376 declarator = declarator->declarator;
8379 if (declarator && declarator->kind == cdk_array)
8381 *nelts = declarator->u.array.bounds;
8382 if (*nelts == error_mark_node)
8383 *nelts = integer_one_node;
8385 if (outer_declarator)
8386 outer_declarator->declarator = declarator->declarator;
8387 else
8388 new_declarator = NULL;
8391 return groktypename (&type_specifier_seq, new_declarator, false);
8394 /* Parse an (optional) new-declarator.
8396 new-declarator:
8397 ptr-operator new-declarator [opt]
8398 direct-new-declarator
8400 Returns the declarator. */
8402 static cp_declarator *
8403 cp_parser_new_declarator_opt (cp_parser* parser)
8405 enum tree_code code;
8406 tree type, std_attributes = NULL_TREE;
8407 cp_cv_quals cv_quals;
8409 /* We don't know if there's a ptr-operator next, or not. */
8410 cp_parser_parse_tentatively (parser);
8411 /* Look for a ptr-operator. */
8412 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8413 /* If that worked, look for more new-declarators. */
8414 if (cp_parser_parse_definitely (parser))
8416 cp_declarator *declarator;
8418 /* Parse another optional declarator. */
8419 declarator = cp_parser_new_declarator_opt (parser);
8421 declarator = cp_parser_make_indirect_declarator
8422 (code, type, cv_quals, declarator, std_attributes);
8424 return declarator;
8427 /* If the next token is a `[', there is a direct-new-declarator. */
8428 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8429 return cp_parser_direct_new_declarator (parser);
8431 return NULL;
8434 /* Parse a direct-new-declarator.
8436 direct-new-declarator:
8437 [ expression ]
8438 direct-new-declarator [constant-expression]
8442 static cp_declarator *
8443 cp_parser_direct_new_declarator (cp_parser* parser)
8445 cp_declarator *declarator = NULL;
8447 while (true)
8449 tree expression;
8450 cp_token *token;
8452 /* Look for the opening `['. */
8453 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8455 token = cp_lexer_peek_token (parser->lexer);
8456 expression = cp_parser_expression (parser);
8457 /* The standard requires that the expression have integral
8458 type. DR 74 adds enumeration types. We believe that the
8459 real intent is that these expressions be handled like the
8460 expression in a `switch' condition, which also allows
8461 classes with a single conversion to integral or
8462 enumeration type. */
8463 if (!processing_template_decl)
8465 expression
8466 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8467 expression,
8468 /*complain=*/true);
8469 if (!expression)
8471 error_at (token->location,
8472 "expression in new-declarator must have integral "
8473 "or enumeration type");
8474 expression = error_mark_node;
8478 /* Look for the closing `]'. */
8479 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8481 /* Add this bound to the declarator. */
8482 declarator = make_array_declarator (declarator, expression);
8484 /* If the next token is not a `[', then there are no more
8485 bounds. */
8486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8487 break;
8490 return declarator;
8493 /* Parse a new-initializer.
8495 new-initializer:
8496 ( expression-list [opt] )
8497 braced-init-list
8499 Returns a representation of the expression-list. */
8501 static vec<tree, va_gc> *
8502 cp_parser_new_initializer (cp_parser* parser)
8504 vec<tree, va_gc> *expression_list;
8506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8508 tree t;
8509 bool expr_non_constant_p;
8510 cp_lexer_set_source_position (parser->lexer);
8511 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8512 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8513 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8514 expression_list = make_tree_vector_single (t);
8516 else
8517 expression_list = (cp_parser_parenthesized_expression_list
8518 (parser, non_attr, /*cast_p=*/false,
8519 /*allow_expansion_p=*/true,
8520 /*non_constant_p=*/NULL));
8522 return expression_list;
8525 /* Parse a delete-expression.
8527 delete-expression:
8528 :: [opt] delete cast-expression
8529 :: [opt] delete [ ] cast-expression
8531 Returns a representation of the expression. */
8533 static tree
8534 cp_parser_delete_expression (cp_parser* parser)
8536 bool global_scope_p;
8537 bool array_p;
8538 tree expression;
8540 /* Look for the optional `::' operator. */
8541 global_scope_p
8542 = (cp_parser_global_scope_opt (parser,
8543 /*current_scope_valid_p=*/false)
8544 != NULL_TREE);
8545 /* Look for the `delete' keyword. */
8546 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8547 /* See if the array syntax is in use. */
8548 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8550 /* Consume the `[' token. */
8551 cp_lexer_consume_token (parser->lexer);
8552 /* Look for the `]' token. */
8553 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8554 /* Remember that this is the `[]' construct. */
8555 array_p = true;
8557 else
8558 array_p = false;
8560 /* Parse the cast-expression. */
8561 expression = cp_parser_simple_cast_expression (parser);
8563 /* A delete-expression may not appear in an integral constant
8564 expression. */
8565 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8566 return error_mark_node;
8568 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8569 tf_warning_or_error);
8572 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8573 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8574 0 otherwise. */
8576 static int
8577 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8579 cp_token *token = cp_lexer_peek_token (parser->lexer);
8580 switch (token->type)
8582 case CPP_COMMA:
8583 case CPP_SEMICOLON:
8584 case CPP_QUERY:
8585 case CPP_COLON:
8586 case CPP_CLOSE_SQUARE:
8587 case CPP_CLOSE_PAREN:
8588 case CPP_CLOSE_BRACE:
8589 case CPP_OPEN_BRACE:
8590 case CPP_DOT:
8591 case CPP_DOT_STAR:
8592 case CPP_DEREF:
8593 case CPP_DEREF_STAR:
8594 case CPP_DIV:
8595 case CPP_MOD:
8596 case CPP_LSHIFT:
8597 case CPP_RSHIFT:
8598 case CPP_LESS:
8599 case CPP_GREATER:
8600 case CPP_LESS_EQ:
8601 case CPP_GREATER_EQ:
8602 case CPP_EQ_EQ:
8603 case CPP_NOT_EQ:
8604 case CPP_EQ:
8605 case CPP_MULT_EQ:
8606 case CPP_DIV_EQ:
8607 case CPP_MOD_EQ:
8608 case CPP_PLUS_EQ:
8609 case CPP_MINUS_EQ:
8610 case CPP_RSHIFT_EQ:
8611 case CPP_LSHIFT_EQ:
8612 case CPP_AND_EQ:
8613 case CPP_XOR_EQ:
8614 case CPP_OR_EQ:
8615 case CPP_XOR:
8616 case CPP_OR:
8617 case CPP_OR_OR:
8618 case CPP_EOF:
8619 case CPP_ELLIPSIS:
8620 return 0;
8622 case CPP_OPEN_PAREN:
8623 /* In ((type ()) () the last () isn't a valid cast-expression,
8624 so the whole must be parsed as postfix-expression. */
8625 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8626 != CPP_CLOSE_PAREN;
8628 case CPP_OPEN_SQUARE:
8629 /* '[' may start a primary-expression in obj-c++ and in C++11,
8630 as a lambda-expression, eg, '(void)[]{}'. */
8631 if (cxx_dialect >= cxx11)
8632 return -1;
8633 return c_dialect_objc ();
8635 case CPP_PLUS_PLUS:
8636 case CPP_MINUS_MINUS:
8637 /* '++' and '--' may or may not start a cast-expression:
8639 struct T { void operator++(int); };
8640 void f() { (T())++; }
8644 int a;
8645 (int)++a; */
8646 return -1;
8648 default:
8649 return 1;
8653 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8654 in the order: const_cast, static_cast, reinterpret_cast.
8656 Don't suggest dynamic_cast.
8658 Return the first legal cast kind found, or NULL otherwise. */
8660 static const char *
8661 get_cast_suggestion (tree dst_type, tree orig_expr)
8663 tree trial;
8665 /* Reuse the parser logic by attempting to build the various kinds of
8666 cast, with "complain" disabled.
8667 Identify the first such cast that is valid. */
8669 /* Don't attempt to run such logic within template processing. */
8670 if (processing_template_decl)
8671 return NULL;
8673 /* First try const_cast. */
8674 trial = build_const_cast (dst_type, orig_expr, tf_none);
8675 if (trial != error_mark_node)
8676 return "const_cast";
8678 /* If that fails, try static_cast. */
8679 trial = build_static_cast (dst_type, orig_expr, tf_none);
8680 if (trial != error_mark_node)
8681 return "static_cast";
8683 /* Finally, try reinterpret_cast. */
8684 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8685 if (trial != error_mark_node)
8686 return "reinterpret_cast";
8688 /* No such cast possible. */
8689 return NULL;
8692 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8693 suggesting how to convert a C-style cast of the form:
8695 (DST_TYPE)ORIG_EXPR
8697 to a C++-style cast.
8699 The primary range of RICHLOC is asssumed to be that of the original
8700 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8701 of the parens in the C-style cast. */
8703 static void
8704 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8705 location_t close_paren_loc, tree orig_expr,
8706 tree dst_type)
8708 /* This function is non-trivial, so bail out now if the warning isn't
8709 going to be emitted. */
8710 if (!warn_old_style_cast)
8711 return;
8713 /* Try to find a legal C++ cast, trying them in order:
8714 const_cast, static_cast, reinterpret_cast. */
8715 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8716 if (!cast_suggestion)
8717 return;
8719 /* Replace the open paren with "CAST_SUGGESTION<". */
8720 pretty_printer pp;
8721 pp_printf (&pp, "%s<", cast_suggestion);
8722 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8724 /* Replace the close paren with "> (". */
8725 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8727 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8728 rich_loc->add_fixit_insert_after (")");
8732 /* Parse a cast-expression.
8734 cast-expression:
8735 unary-expression
8736 ( type-id ) cast-expression
8738 ADDRESS_P is true iff the unary-expression is appearing as the
8739 operand of the `&' operator. CAST_P is true if this expression is
8740 the target of a cast.
8742 Returns a representation of the expression. */
8744 static cp_expr
8745 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8746 bool decltype_p, cp_id_kind * pidk)
8748 /* If it's a `(', then we might be looking at a cast. */
8749 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8751 tree type = NULL_TREE;
8752 cp_expr expr (NULL_TREE);
8753 int cast_expression = 0;
8754 const char *saved_message;
8756 /* There's no way to know yet whether or not this is a cast.
8757 For example, `(int (3))' is a unary-expression, while `(int)
8758 3' is a cast. So, we resort to parsing tentatively. */
8759 cp_parser_parse_tentatively (parser);
8760 /* Types may not be defined in a cast. */
8761 saved_message = parser->type_definition_forbidden_message;
8762 parser->type_definition_forbidden_message
8763 = G_("types may not be defined in casts");
8764 /* Consume the `('. */
8765 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8766 location_t open_paren_loc = open_paren->location;
8767 location_t close_paren_loc = UNKNOWN_LOCATION;
8769 /* A very tricky bit is that `(struct S) { 3 }' is a
8770 compound-literal (which we permit in C++ as an extension).
8771 But, that construct is not a cast-expression -- it is a
8772 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8773 is legal; if the compound-literal were a cast-expression,
8774 you'd need an extra set of parentheses.) But, if we parse
8775 the type-id, and it happens to be a class-specifier, then we
8776 will commit to the parse at that point, because we cannot
8777 undo the action that is done when creating a new class. So,
8778 then we cannot back up and do a postfix-expression.
8780 Another tricky case is the following (c++/29234):
8782 struct S { void operator () (); };
8784 void foo ()
8786 ( S()() );
8789 As a type-id we parse the parenthesized S()() as a function
8790 returning a function, groktypename complains and we cannot
8791 back up in this case either.
8793 Therefore, we scan ahead to the closing `)', and check to see
8794 if the tokens after the `)' can start a cast-expression. Otherwise
8795 we are dealing with an unary-expression, a postfix-expression
8796 or something else.
8798 Yet another tricky case, in C++11, is the following (c++/54891):
8800 (void)[]{};
8802 The issue is that usually, besides the case of lambda-expressions,
8803 the parenthesized type-id cannot be followed by '[', and, eg, we
8804 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8805 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8806 we don't commit, we try a cast-expression, then an unary-expression.
8808 Save tokens so that we can put them back. */
8809 cp_lexer_save_tokens (parser->lexer);
8811 /* We may be looking at a cast-expression. */
8812 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8813 /*consume_paren=*/true))
8814 cast_expression
8815 = cp_parser_tokens_start_cast_expression (parser);
8817 /* Roll back the tokens we skipped. */
8818 cp_lexer_rollback_tokens (parser->lexer);
8819 /* If we aren't looking at a cast-expression, simulate an error so
8820 that the call to cp_parser_error_occurred below returns true. */
8821 if (!cast_expression)
8822 cp_parser_simulate_error (parser);
8823 else
8825 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8826 parser->in_type_id_in_expr_p = true;
8827 /* Look for the type-id. */
8828 type = cp_parser_type_id (parser);
8829 /* Look for the closing `)'. */
8830 cp_token *close_paren
8831 = cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8832 if (close_paren)
8833 close_paren_loc = close_paren->location;
8834 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8837 /* Restore the saved message. */
8838 parser->type_definition_forbidden_message = saved_message;
8840 /* At this point this can only be either a cast or a
8841 parenthesized ctor such as `(T ())' that looks like a cast to
8842 function returning T. */
8843 if (!cp_parser_error_occurred (parser))
8845 /* Only commit if the cast-expression doesn't start with
8846 '++', '--', or '[' in C++11. */
8847 if (cast_expression > 0)
8848 cp_parser_commit_to_topmost_tentative_parse (parser);
8850 expr = cp_parser_cast_expression (parser,
8851 /*address_p=*/false,
8852 /*cast_p=*/true,
8853 /*decltype_p=*/false,
8854 pidk);
8856 if (cp_parser_parse_definitely (parser))
8858 /* Warn about old-style casts, if so requested. */
8859 if (warn_old_style_cast
8860 && !in_system_header_at (input_location)
8861 && !VOID_TYPE_P (type)
8862 && current_lang_name != lang_name_c)
8864 gcc_rich_location rich_loc (input_location);
8865 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8866 expr, type);
8867 warning_at_rich_loc (&rich_loc, OPT_Wold_style_cast,
8868 "use of old-style cast to %qT", type);
8871 /* Only type conversions to integral or enumeration types
8872 can be used in constant-expressions. */
8873 if (!cast_valid_in_integral_constant_expression_p (type)
8874 && cp_parser_non_integral_constant_expression (parser,
8875 NIC_CAST))
8876 return error_mark_node;
8878 /* Perform the cast. */
8879 /* Make a location:
8880 (TYPE) EXPR
8881 ^~~~~~~~~~~
8882 with start==caret at the open paren, extending to the
8883 end of "expr". */
8884 location_t cast_loc = make_location (open_paren_loc,
8885 open_paren_loc,
8886 expr.get_finish ());
8887 expr = build_c_cast (cast_loc, type, expr);
8888 return expr;
8891 else
8892 cp_parser_abort_tentative_parse (parser);
8895 /* If we get here, then it's not a cast, so it must be a
8896 unary-expression. */
8897 return cp_parser_unary_expression (parser, pidk, address_p,
8898 cast_p, decltype_p);
8901 /* Parse a binary expression of the general form:
8903 pm-expression:
8904 cast-expression
8905 pm-expression .* cast-expression
8906 pm-expression ->* cast-expression
8908 multiplicative-expression:
8909 pm-expression
8910 multiplicative-expression * pm-expression
8911 multiplicative-expression / pm-expression
8912 multiplicative-expression % pm-expression
8914 additive-expression:
8915 multiplicative-expression
8916 additive-expression + multiplicative-expression
8917 additive-expression - multiplicative-expression
8919 shift-expression:
8920 additive-expression
8921 shift-expression << additive-expression
8922 shift-expression >> additive-expression
8924 relational-expression:
8925 shift-expression
8926 relational-expression < shift-expression
8927 relational-expression > shift-expression
8928 relational-expression <= shift-expression
8929 relational-expression >= shift-expression
8931 GNU Extension:
8933 relational-expression:
8934 relational-expression <? shift-expression
8935 relational-expression >? shift-expression
8937 equality-expression:
8938 relational-expression
8939 equality-expression == relational-expression
8940 equality-expression != relational-expression
8942 and-expression:
8943 equality-expression
8944 and-expression & equality-expression
8946 exclusive-or-expression:
8947 and-expression
8948 exclusive-or-expression ^ and-expression
8950 inclusive-or-expression:
8951 exclusive-or-expression
8952 inclusive-or-expression | exclusive-or-expression
8954 logical-and-expression:
8955 inclusive-or-expression
8956 logical-and-expression && inclusive-or-expression
8958 logical-or-expression:
8959 logical-and-expression
8960 logical-or-expression || logical-and-expression
8962 All these are implemented with a single function like:
8964 binary-expression:
8965 simple-cast-expression
8966 binary-expression <token> binary-expression
8968 CAST_P is true if this expression is the target of a cast.
8970 The binops_by_token map is used to get the tree codes for each <token> type.
8971 binary-expressions are associated according to a precedence table. */
8973 #define TOKEN_PRECEDENCE(token) \
8974 (((token->type == CPP_GREATER \
8975 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
8976 && !parser->greater_than_is_operator_p) \
8977 ? PREC_NOT_OPERATOR \
8978 : binops_by_token[token->type].prec)
8980 static cp_expr
8981 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
8982 bool no_toplevel_fold_p,
8983 bool decltype_p,
8984 enum cp_parser_prec prec,
8985 cp_id_kind * pidk)
8987 cp_parser_expression_stack stack;
8988 cp_parser_expression_stack_entry *sp = &stack[0];
8989 cp_parser_expression_stack_entry current;
8990 cp_expr rhs;
8991 cp_token *token;
8992 enum tree_code rhs_type;
8993 enum cp_parser_prec new_prec, lookahead_prec;
8994 tree overload;
8996 /* Parse the first expression. */
8997 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
8998 ? TRUTH_NOT_EXPR : ERROR_MARK);
8999 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9000 cast_p, decltype_p, pidk);
9001 current.prec = prec;
9003 if (cp_parser_error_occurred (parser))
9004 return error_mark_node;
9006 for (;;)
9008 /* Get an operator token. */
9009 token = cp_lexer_peek_token (parser->lexer);
9011 if (warn_cxx11_compat
9012 && token->type == CPP_RSHIFT
9013 && !parser->greater_than_is_operator_p)
9015 if (warning_at (token->location, OPT_Wc__11_compat,
9016 "%<>>%> operator is treated"
9017 " as two right angle brackets in C++11"))
9018 inform (token->location,
9019 "suggest parentheses around %<>>%> expression");
9022 new_prec = TOKEN_PRECEDENCE (token);
9023 if (new_prec != PREC_NOT_OPERATOR
9024 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9025 /* This is a fold-expression; handle it later. */
9026 new_prec = PREC_NOT_OPERATOR;
9028 /* Popping an entry off the stack means we completed a subexpression:
9029 - either we found a token which is not an operator (`>' where it is not
9030 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9031 will happen repeatedly;
9032 - or, we found an operator which has lower priority. This is the case
9033 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9034 parsing `3 * 4'. */
9035 if (new_prec <= current.prec)
9037 if (sp == stack)
9038 break;
9039 else
9040 goto pop;
9043 get_rhs:
9044 current.tree_type = binops_by_token[token->type].tree_type;
9045 current.loc = token->location;
9047 /* We used the operator token. */
9048 cp_lexer_consume_token (parser->lexer);
9050 /* For "false && x" or "true || x", x will never be executed;
9051 disable warnings while evaluating it. */
9052 if (current.tree_type == TRUTH_ANDIF_EXPR)
9053 c_inhibit_evaluation_warnings +=
9054 cp_fully_fold (current.lhs) == truthvalue_false_node;
9055 else if (current.tree_type == TRUTH_ORIF_EXPR)
9056 c_inhibit_evaluation_warnings +=
9057 cp_fully_fold (current.lhs) == truthvalue_true_node;
9059 /* Extract another operand. It may be the RHS of this expression
9060 or the LHS of a new, higher priority expression. */
9061 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9062 ? TRUTH_NOT_EXPR : ERROR_MARK);
9063 rhs = cp_parser_simple_cast_expression (parser);
9065 /* Get another operator token. Look up its precedence to avoid
9066 building a useless (immediately popped) stack entry for common
9067 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9068 token = cp_lexer_peek_token (parser->lexer);
9069 lookahead_prec = TOKEN_PRECEDENCE (token);
9070 if (lookahead_prec != PREC_NOT_OPERATOR
9071 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9072 lookahead_prec = PREC_NOT_OPERATOR;
9073 if (lookahead_prec > new_prec)
9075 /* ... and prepare to parse the RHS of the new, higher priority
9076 expression. Since precedence levels on the stack are
9077 monotonically increasing, we do not have to care about
9078 stack overflows. */
9079 *sp = current;
9080 ++sp;
9081 current.lhs = rhs;
9082 current.lhs_type = rhs_type;
9083 current.prec = new_prec;
9084 new_prec = lookahead_prec;
9085 goto get_rhs;
9087 pop:
9088 lookahead_prec = new_prec;
9089 /* If the stack is not empty, we have parsed into LHS the right side
9090 (`4' in the example above) of an expression we had suspended.
9091 We can use the information on the stack to recover the LHS (`3')
9092 from the stack together with the tree code (`MULT_EXPR'), and
9093 the precedence of the higher level subexpression
9094 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9095 which will be used to actually build the additive expression. */
9096 rhs = current.lhs;
9097 rhs_type = current.lhs_type;
9098 --sp;
9099 current = *sp;
9102 /* Undo the disabling of warnings done above. */
9103 if (current.tree_type == TRUTH_ANDIF_EXPR)
9104 c_inhibit_evaluation_warnings -=
9105 cp_fully_fold (current.lhs) == truthvalue_false_node;
9106 else if (current.tree_type == TRUTH_ORIF_EXPR)
9107 c_inhibit_evaluation_warnings -=
9108 cp_fully_fold (current.lhs) == truthvalue_true_node;
9110 if (warn_logical_not_paren
9111 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9112 && current.lhs_type == TRUTH_NOT_EXPR
9113 /* Avoid warning for !!x == y. */
9114 && (TREE_CODE (current.lhs) != NE_EXPR
9115 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9116 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9117 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9118 /* Avoid warning for !b == y where b is boolean. */
9119 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9120 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9121 != BOOLEAN_TYPE))))
9122 /* Avoid warning for !!b == y where b is boolean. */
9123 && (!DECL_P (current.lhs)
9124 || TREE_TYPE (current.lhs) == NULL_TREE
9125 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9126 warn_logical_not_parentheses (current.loc, current.tree_type,
9127 current.lhs, maybe_constant_value (rhs));
9129 overload = NULL;
9131 location_t combined_loc = make_location (current.loc,
9132 current.lhs.get_start (),
9133 rhs.get_finish ());
9135 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9136 ERROR_MARK for everything that is not a binary expression.
9137 This makes warn_about_parentheses miss some warnings that
9138 involve unary operators. For unary expressions we should
9139 pass the correct tree_code unless the unary expression was
9140 surrounded by parentheses.
9142 if (no_toplevel_fold_p
9143 && lookahead_prec <= current.prec
9144 && sp == stack)
9145 current.lhs = build2_loc (combined_loc,
9146 current.tree_type,
9147 TREE_CODE_CLASS (current.tree_type)
9148 == tcc_comparison
9149 ? boolean_type_node : TREE_TYPE (current.lhs),
9150 current.lhs, rhs);
9151 else
9153 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9154 current.lhs, current.lhs_type,
9155 rhs, rhs_type, &overload,
9156 complain_flags (decltype_p));
9157 /* TODO: build_x_binary_op doesn't always honor the location. */
9158 current.lhs.set_location (combined_loc);
9160 current.lhs_type = current.tree_type;
9162 /* If the binary operator required the use of an overloaded operator,
9163 then this expression cannot be an integral constant-expression.
9164 An overloaded operator can be used even if both operands are
9165 otherwise permissible in an integral constant-expression if at
9166 least one of the operands is of enumeration type. */
9168 if (overload
9169 && cp_parser_non_integral_constant_expression (parser,
9170 NIC_OVERLOADED))
9171 return error_mark_node;
9174 return current.lhs;
9177 static cp_expr
9178 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9179 bool no_toplevel_fold_p,
9180 enum cp_parser_prec prec,
9181 cp_id_kind * pidk)
9183 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9184 /*decltype*/false, prec, pidk);
9187 /* Parse the `? expression : assignment-expression' part of a
9188 conditional-expression. The LOGICAL_OR_EXPR is the
9189 logical-or-expression that started the conditional-expression.
9190 Returns a representation of the entire conditional-expression.
9192 This routine is used by cp_parser_assignment_expression.
9194 ? expression : assignment-expression
9196 GNU Extensions:
9198 ? : assignment-expression */
9200 static tree
9201 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9203 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9204 cp_expr assignment_expr;
9205 struct cp_token *token;
9206 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9208 /* Consume the `?' token. */
9209 cp_lexer_consume_token (parser->lexer);
9210 token = cp_lexer_peek_token (parser->lexer);
9211 if (cp_parser_allow_gnu_extensions_p (parser)
9212 && token->type == CPP_COLON)
9214 pedwarn (token->location, OPT_Wpedantic,
9215 "ISO C++ does not allow ?: with omitted middle operand");
9216 /* Implicit true clause. */
9217 expr = NULL_TREE;
9218 c_inhibit_evaluation_warnings +=
9219 folded_logical_or_expr == truthvalue_true_node;
9220 warn_for_omitted_condop (token->location, logical_or_expr);
9222 else
9224 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9225 parser->colon_corrects_to_scope_p = false;
9226 /* Parse the expression. */
9227 c_inhibit_evaluation_warnings +=
9228 folded_logical_or_expr == truthvalue_false_node;
9229 expr = cp_parser_expression (parser);
9230 c_inhibit_evaluation_warnings +=
9231 ((folded_logical_or_expr == truthvalue_true_node)
9232 - (folded_logical_or_expr == truthvalue_false_node));
9233 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9236 /* The next token should be a `:'. */
9237 cp_parser_require (parser, CPP_COLON, RT_COLON);
9238 /* Parse the assignment-expression. */
9239 assignment_expr = cp_parser_assignment_expression (parser);
9240 c_inhibit_evaluation_warnings -=
9241 folded_logical_or_expr == truthvalue_true_node;
9243 /* Make a location:
9244 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9245 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9246 with the caret at the "?", ranging from the start of
9247 the logical_or_expr to the end of the assignment_expr. */
9248 loc = make_location (loc,
9249 logical_or_expr.get_start (),
9250 assignment_expr.get_finish ());
9252 /* Build the conditional-expression. */
9253 return build_x_conditional_expr (loc, logical_or_expr,
9254 expr,
9255 assignment_expr,
9256 tf_warning_or_error);
9259 /* Parse an assignment-expression.
9261 assignment-expression:
9262 conditional-expression
9263 logical-or-expression assignment-operator assignment_expression
9264 throw-expression
9266 CAST_P is true if this expression is the target of a cast.
9267 DECLTYPE_P is true if this expression is the operand of decltype.
9269 Returns a representation for the expression. */
9271 static cp_expr
9272 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9273 bool cast_p, bool decltype_p)
9275 cp_expr expr;
9277 /* If the next token is the `throw' keyword, then we're looking at
9278 a throw-expression. */
9279 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9280 expr = cp_parser_throw_expression (parser);
9281 /* Otherwise, it must be that we are looking at a
9282 logical-or-expression. */
9283 else
9285 /* Parse the binary expressions (logical-or-expression). */
9286 expr = cp_parser_binary_expression (parser, cast_p, false,
9287 decltype_p,
9288 PREC_NOT_OPERATOR, pidk);
9289 /* If the next token is a `?' then we're actually looking at a
9290 conditional-expression. */
9291 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9292 return cp_parser_question_colon_clause (parser, expr);
9293 else
9295 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9297 /* If it's an assignment-operator, we're using the second
9298 production. */
9299 enum tree_code assignment_operator
9300 = cp_parser_assignment_operator_opt (parser);
9301 if (assignment_operator != ERROR_MARK)
9303 bool non_constant_p;
9305 /* Parse the right-hand side of the assignment. */
9306 cp_expr rhs = cp_parser_initializer_clause (parser,
9307 &non_constant_p);
9309 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9310 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9312 /* An assignment may not appear in a
9313 constant-expression. */
9314 if (cp_parser_non_integral_constant_expression (parser,
9315 NIC_ASSIGNMENT))
9316 return error_mark_node;
9317 /* Build the assignment expression. Its default
9318 location:
9319 LHS = RHS
9320 ~~~~^~~~~
9321 is the location of the '=' token as the
9322 caret, ranging from the start of the lhs to the
9323 end of the rhs. */
9324 loc = make_location (loc,
9325 expr.get_start (),
9326 rhs.get_finish ());
9327 expr = build_x_modify_expr (loc, expr,
9328 assignment_operator,
9329 rhs,
9330 complain_flags (decltype_p));
9331 /* TODO: build_x_modify_expr doesn't honor the location,
9332 so we must set it here. */
9333 expr.set_location (loc);
9338 return expr;
9341 /* Parse an (optional) assignment-operator.
9343 assignment-operator: one of
9344 = *= /= %= += -= >>= <<= &= ^= |=
9346 GNU Extension:
9348 assignment-operator: one of
9349 <?= >?=
9351 If the next token is an assignment operator, the corresponding tree
9352 code is returned, and the token is consumed. For example, for
9353 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9354 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9355 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9356 operator, ERROR_MARK is returned. */
9358 static enum tree_code
9359 cp_parser_assignment_operator_opt (cp_parser* parser)
9361 enum tree_code op;
9362 cp_token *token;
9364 /* Peek at the next token. */
9365 token = cp_lexer_peek_token (parser->lexer);
9367 switch (token->type)
9369 case CPP_EQ:
9370 op = NOP_EXPR;
9371 break;
9373 case CPP_MULT_EQ:
9374 op = MULT_EXPR;
9375 break;
9377 case CPP_DIV_EQ:
9378 op = TRUNC_DIV_EXPR;
9379 break;
9381 case CPP_MOD_EQ:
9382 op = TRUNC_MOD_EXPR;
9383 break;
9385 case CPP_PLUS_EQ:
9386 op = PLUS_EXPR;
9387 break;
9389 case CPP_MINUS_EQ:
9390 op = MINUS_EXPR;
9391 break;
9393 case CPP_RSHIFT_EQ:
9394 op = RSHIFT_EXPR;
9395 break;
9397 case CPP_LSHIFT_EQ:
9398 op = LSHIFT_EXPR;
9399 break;
9401 case CPP_AND_EQ:
9402 op = BIT_AND_EXPR;
9403 break;
9405 case CPP_XOR_EQ:
9406 op = BIT_XOR_EXPR;
9407 break;
9409 case CPP_OR_EQ:
9410 op = BIT_IOR_EXPR;
9411 break;
9413 default:
9414 /* Nothing else is an assignment operator. */
9415 op = ERROR_MARK;
9418 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9419 if (op != ERROR_MARK
9420 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9421 op = ERROR_MARK;
9423 /* If it was an assignment operator, consume it. */
9424 if (op != ERROR_MARK)
9425 cp_lexer_consume_token (parser->lexer);
9427 return op;
9430 /* Parse an expression.
9432 expression:
9433 assignment-expression
9434 expression , assignment-expression
9436 CAST_P is true if this expression is the target of a cast.
9437 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9438 except possibly parenthesized or on the RHS of a comma (N3276).
9440 Returns a representation of the expression. */
9442 static cp_expr
9443 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9444 bool cast_p, bool decltype_p)
9446 cp_expr expression = NULL_TREE;
9447 location_t loc = UNKNOWN_LOCATION;
9449 while (true)
9451 cp_expr assignment_expression;
9453 /* Parse the next assignment-expression. */
9454 assignment_expression
9455 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9457 /* We don't create a temporary for a call that is the immediate operand
9458 of decltype or on the RHS of a comma. But when we see a comma, we
9459 need to create a temporary for a call on the LHS. */
9460 if (decltype_p && !processing_template_decl
9461 && TREE_CODE (assignment_expression) == CALL_EXPR
9462 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9463 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9464 assignment_expression
9465 = build_cplus_new (TREE_TYPE (assignment_expression),
9466 assignment_expression, tf_warning_or_error);
9468 /* If this is the first assignment-expression, we can just
9469 save it away. */
9470 if (!expression)
9471 expression = assignment_expression;
9472 else
9474 /* Create a location with caret at the comma, ranging
9475 from the start of the LHS to the end of the RHS. */
9476 loc = make_location (loc,
9477 expression.get_start (),
9478 assignment_expression.get_finish ());
9479 expression = build_x_compound_expr (loc, expression,
9480 assignment_expression,
9481 complain_flags (decltype_p));
9482 expression.set_location (loc);
9484 /* If the next token is not a comma, or we're in a fold-expression, then
9485 we are done with the expression. */
9486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9487 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9488 break;
9489 /* Consume the `,'. */
9490 loc = cp_lexer_peek_token (parser->lexer)->location;
9491 cp_lexer_consume_token (parser->lexer);
9492 /* A comma operator cannot appear in a constant-expression. */
9493 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9494 expression = error_mark_node;
9497 return expression;
9500 /* Parse a constant-expression.
9502 constant-expression:
9503 conditional-expression
9505 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9506 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9507 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9508 is false, NON_CONSTANT_P should be NULL. */
9510 static cp_expr
9511 cp_parser_constant_expression (cp_parser* parser,
9512 bool allow_non_constant_p,
9513 bool *non_constant_p)
9515 bool saved_integral_constant_expression_p;
9516 bool saved_allow_non_integral_constant_expression_p;
9517 bool saved_non_integral_constant_expression_p;
9518 cp_expr expression;
9520 /* It might seem that we could simply parse the
9521 conditional-expression, and then check to see if it were
9522 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9523 one that the compiler can figure out is constant, possibly after
9524 doing some simplifications or optimizations. The standard has a
9525 precise definition of constant-expression, and we must honor
9526 that, even though it is somewhat more restrictive.
9528 For example:
9530 int i[(2, 3)];
9532 is not a legal declaration, because `(2, 3)' is not a
9533 constant-expression. The `,' operator is forbidden in a
9534 constant-expression. However, GCC's constant-folding machinery
9535 will fold this operation to an INTEGER_CST for `3'. */
9537 /* Save the old settings. */
9538 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9539 saved_allow_non_integral_constant_expression_p
9540 = parser->allow_non_integral_constant_expression_p;
9541 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9542 /* We are now parsing a constant-expression. */
9543 parser->integral_constant_expression_p = true;
9544 parser->allow_non_integral_constant_expression_p
9545 = (allow_non_constant_p || cxx_dialect >= cxx11);
9546 parser->non_integral_constant_expression_p = false;
9547 /* Although the grammar says "conditional-expression", we parse an
9548 "assignment-expression", which also permits "throw-expression"
9549 and the use of assignment operators. In the case that
9550 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9551 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9552 actually essential that we look for an assignment-expression.
9553 For example, cp_parser_initializer_clauses uses this function to
9554 determine whether a particular assignment-expression is in fact
9555 constant. */
9556 expression = cp_parser_assignment_expression (parser);
9557 /* Restore the old settings. */
9558 parser->integral_constant_expression_p
9559 = saved_integral_constant_expression_p;
9560 parser->allow_non_integral_constant_expression_p
9561 = saved_allow_non_integral_constant_expression_p;
9562 if (cxx_dialect >= cxx11)
9564 /* Require an rvalue constant expression here; that's what our
9565 callers expect. Reference constant expressions are handled
9566 separately in e.g. cp_parser_template_argument. */
9567 tree decay = expression;
9568 if (TREE_TYPE (expression)
9569 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9570 decay = build_address (expression);
9571 bool is_const = potential_rvalue_constant_expression (decay);
9572 parser->non_integral_constant_expression_p = !is_const;
9573 if (!is_const && !allow_non_constant_p)
9574 require_potential_rvalue_constant_expression (decay);
9576 if (allow_non_constant_p)
9577 *non_constant_p = parser->non_integral_constant_expression_p;
9578 parser->non_integral_constant_expression_p
9579 = saved_non_integral_constant_expression_p;
9581 return expression;
9584 /* Parse __builtin_offsetof.
9586 offsetof-expression:
9587 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9589 offsetof-member-designator:
9590 id-expression
9591 | offsetof-member-designator "." id-expression
9592 | offsetof-member-designator "[" expression "]"
9593 | offsetof-member-designator "->" id-expression */
9595 static cp_expr
9596 cp_parser_builtin_offsetof (cp_parser *parser)
9598 int save_ice_p, save_non_ice_p;
9599 tree type;
9600 cp_expr expr;
9601 cp_id_kind dummy;
9602 cp_token *token;
9603 location_t finish_loc;
9605 /* We're about to accept non-integral-constant things, but will
9606 definitely yield an integral constant expression. Save and
9607 restore these values around our local parsing. */
9608 save_ice_p = parser->integral_constant_expression_p;
9609 save_non_ice_p = parser->non_integral_constant_expression_p;
9611 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9613 /* Consume the "__builtin_offsetof" token. */
9614 cp_lexer_consume_token (parser->lexer);
9615 /* Consume the opening `('. */
9616 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9617 /* Parse the type-id. */
9618 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9619 type = cp_parser_type_id (parser);
9620 /* Look for the `,'. */
9621 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9622 token = cp_lexer_peek_token (parser->lexer);
9624 /* Build the (type *)null that begins the traditional offsetof macro. */
9625 tree object_ptr
9626 = build_static_cast (build_pointer_type (type), null_pointer_node,
9627 tf_warning_or_error);
9629 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9630 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9631 true, &dummy, token->location);
9632 while (true)
9634 token = cp_lexer_peek_token (parser->lexer);
9635 switch (token->type)
9637 case CPP_OPEN_SQUARE:
9638 /* offsetof-member-designator "[" expression "]" */
9639 expr = cp_parser_postfix_open_square_expression (parser, expr,
9640 true, false);
9641 break;
9643 case CPP_DEREF:
9644 /* offsetof-member-designator "->" identifier */
9645 expr = grok_array_decl (token->location, expr,
9646 integer_zero_node, false);
9647 /* FALLTHRU */
9649 case CPP_DOT:
9650 /* offsetof-member-designator "." identifier */
9651 cp_lexer_consume_token (parser->lexer);
9652 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9653 expr, true, &dummy,
9654 token->location);
9655 break;
9657 case CPP_CLOSE_PAREN:
9658 /* Consume the ")" token. */
9659 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9660 cp_lexer_consume_token (parser->lexer);
9661 goto success;
9663 default:
9664 /* Error. We know the following require will fail, but
9665 that gives the proper error message. */
9666 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9667 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9668 expr = error_mark_node;
9669 goto failure;
9673 success:
9674 /* Make a location of the form:
9675 __builtin_offsetof (struct s, f)
9676 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9677 with caret at the type-id, ranging from the start of the
9678 "_builtin_offsetof" token to the close paren. */
9679 loc = make_location (loc, start_loc, finish_loc);
9680 /* The result will be an INTEGER_CST, so we need to explicitly
9681 preserve the location. */
9682 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9684 failure:
9685 parser->integral_constant_expression_p = save_ice_p;
9686 parser->non_integral_constant_expression_p = save_non_ice_p;
9688 return expr;
9691 /* Parse a trait expression.
9693 Returns a representation of the expression, the underlying type
9694 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9696 static tree
9697 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9699 cp_trait_kind kind;
9700 tree type1, type2 = NULL_TREE;
9701 bool binary = false;
9702 bool variadic = false;
9704 switch (keyword)
9706 case RID_HAS_NOTHROW_ASSIGN:
9707 kind = CPTK_HAS_NOTHROW_ASSIGN;
9708 break;
9709 case RID_HAS_NOTHROW_CONSTRUCTOR:
9710 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9711 break;
9712 case RID_HAS_NOTHROW_COPY:
9713 kind = CPTK_HAS_NOTHROW_COPY;
9714 break;
9715 case RID_HAS_TRIVIAL_ASSIGN:
9716 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9717 break;
9718 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9719 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9720 break;
9721 case RID_HAS_TRIVIAL_COPY:
9722 kind = CPTK_HAS_TRIVIAL_COPY;
9723 break;
9724 case RID_HAS_TRIVIAL_DESTRUCTOR:
9725 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9726 break;
9727 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9728 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9729 break;
9730 case RID_HAS_VIRTUAL_DESTRUCTOR:
9731 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9732 break;
9733 case RID_IS_ABSTRACT:
9734 kind = CPTK_IS_ABSTRACT;
9735 break;
9736 case RID_IS_AGGREGATE:
9737 kind = CPTK_IS_AGGREGATE;
9738 break;
9739 case RID_IS_BASE_OF:
9740 kind = CPTK_IS_BASE_OF;
9741 binary = true;
9742 break;
9743 case RID_IS_CLASS:
9744 kind = CPTK_IS_CLASS;
9745 break;
9746 case RID_IS_EMPTY:
9747 kind = CPTK_IS_EMPTY;
9748 break;
9749 case RID_IS_ENUM:
9750 kind = CPTK_IS_ENUM;
9751 break;
9752 case RID_IS_FINAL:
9753 kind = CPTK_IS_FINAL;
9754 break;
9755 case RID_IS_LITERAL_TYPE:
9756 kind = CPTK_IS_LITERAL_TYPE;
9757 break;
9758 case RID_IS_POD:
9759 kind = CPTK_IS_POD;
9760 break;
9761 case RID_IS_POLYMORPHIC:
9762 kind = CPTK_IS_POLYMORPHIC;
9763 break;
9764 case RID_IS_SAME_AS:
9765 kind = CPTK_IS_SAME_AS;
9766 binary = true;
9767 break;
9768 case RID_IS_STD_LAYOUT:
9769 kind = CPTK_IS_STD_LAYOUT;
9770 break;
9771 case RID_IS_TRIVIAL:
9772 kind = CPTK_IS_TRIVIAL;
9773 break;
9774 case RID_IS_TRIVIALLY_ASSIGNABLE:
9775 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9776 binary = true;
9777 break;
9778 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9779 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9780 variadic = true;
9781 break;
9782 case RID_IS_TRIVIALLY_COPYABLE:
9783 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9784 break;
9785 case RID_IS_UNION:
9786 kind = CPTK_IS_UNION;
9787 break;
9788 case RID_UNDERLYING_TYPE:
9789 kind = CPTK_UNDERLYING_TYPE;
9790 break;
9791 case RID_BASES:
9792 kind = CPTK_BASES;
9793 break;
9794 case RID_DIRECT_BASES:
9795 kind = CPTK_DIRECT_BASES;
9796 break;
9797 case RID_IS_ASSIGNABLE:
9798 kind = CPTK_IS_ASSIGNABLE;
9799 binary = true;
9800 break;
9801 case RID_IS_CONSTRUCTIBLE:
9802 kind = CPTK_IS_CONSTRUCTIBLE;
9803 variadic = true;
9804 break;
9805 default:
9806 gcc_unreachable ();
9809 /* Consume the token. */
9810 cp_lexer_consume_token (parser->lexer);
9812 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9815 type_id_in_expr_sentinel s (parser);
9816 type1 = cp_parser_type_id (parser);
9819 if (type1 == error_mark_node)
9820 return error_mark_node;
9822 if (binary)
9824 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9827 type_id_in_expr_sentinel s (parser);
9828 type2 = cp_parser_type_id (parser);
9831 if (type2 == error_mark_node)
9832 return error_mark_node;
9834 else if (variadic)
9836 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9838 cp_lexer_consume_token (parser->lexer);
9839 tree elt = cp_parser_type_id (parser);
9840 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9842 cp_lexer_consume_token (parser->lexer);
9843 elt = make_pack_expansion (elt);
9845 if (elt == error_mark_node)
9846 return error_mark_node;
9847 type2 = tree_cons (NULL_TREE, elt, type2);
9851 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9853 /* Complete the trait expression, which may mean either processing
9854 the trait expr now or saving it for template instantiation. */
9855 switch (kind)
9857 case CPTK_UNDERLYING_TYPE:
9858 return finish_underlying_type (type1);
9859 case CPTK_BASES:
9860 return finish_bases (type1, false);
9861 case CPTK_DIRECT_BASES:
9862 return finish_bases (type1, true);
9863 default:
9864 return finish_trait_expr (kind, type1, type2);
9868 /* Lambdas that appear in variable initializer or default argument scope
9869 get that in their mangling, so we need to record it. We might as well
9870 use the count for function and namespace scopes as well. */
9871 static GTY(()) tree lambda_scope;
9872 static GTY(()) int lambda_count;
9873 struct GTY(()) tree_int
9875 tree t;
9876 int i;
9878 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9880 static void
9881 start_lambda_scope (tree decl)
9883 tree_int ti;
9884 gcc_assert (decl);
9885 /* Once we're inside a function, we ignore other scopes and just push
9886 the function again so that popping works properly. */
9887 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9888 decl = current_function_decl;
9889 ti.t = lambda_scope;
9890 ti.i = lambda_count;
9891 vec_safe_push (lambda_scope_stack, ti);
9892 if (lambda_scope != decl)
9894 /* Don't reset the count if we're still in the same function. */
9895 lambda_scope = decl;
9896 lambda_count = 0;
9900 static void
9901 record_lambda_scope (tree lambda)
9903 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9904 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9907 static void
9908 finish_lambda_scope (void)
9910 tree_int *p = &lambda_scope_stack->last ();
9911 if (lambda_scope != p->t)
9913 lambda_scope = p->t;
9914 lambda_count = p->i;
9916 lambda_scope_stack->pop ();
9919 /* Parse a lambda expression.
9921 lambda-expression:
9922 lambda-introducer lambda-declarator [opt] compound-statement
9924 Returns a representation of the expression. */
9926 static cp_expr
9927 cp_parser_lambda_expression (cp_parser* parser)
9929 tree lambda_expr = build_lambda_expr ();
9930 tree type;
9931 bool ok = true;
9932 cp_token *token = cp_lexer_peek_token (parser->lexer);
9933 cp_token_position start = 0;
9935 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9937 if (cp_unevaluated_operand)
9939 if (!token->error_reported)
9941 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9942 "lambda-expression in unevaluated context");
9943 token->error_reported = true;
9945 ok = false;
9947 else if (parser->in_template_argument_list_p)
9949 if (!token->error_reported)
9951 error_at (token->location, "lambda-expression in template-argument");
9952 token->error_reported = true;
9954 ok = false;
9957 /* We may be in the middle of deferred access check. Disable
9958 it now. */
9959 push_deferring_access_checks (dk_no_deferred);
9961 cp_parser_lambda_introducer (parser, lambda_expr);
9963 type = begin_lambda_type (lambda_expr);
9964 if (type == error_mark_node)
9965 return error_mark_node;
9967 record_lambda_scope (lambda_expr);
9969 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9970 determine_visibility (TYPE_NAME (type));
9972 /* Now that we've started the type, add the capture fields for any
9973 explicit captures. */
9974 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
9977 /* Inside the class, surrounding template-parameter-lists do not apply. */
9978 unsigned int saved_num_template_parameter_lists
9979 = parser->num_template_parameter_lists;
9980 unsigned char in_statement = parser->in_statement;
9981 bool in_switch_statement_p = parser->in_switch_statement_p;
9982 bool fully_implicit_function_template_p
9983 = parser->fully_implicit_function_template_p;
9984 tree implicit_template_parms = parser->implicit_template_parms;
9985 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
9986 bool auto_is_implicit_function_template_parm_p
9987 = parser->auto_is_implicit_function_template_parm_p;
9989 parser->num_template_parameter_lists = 0;
9990 parser->in_statement = 0;
9991 parser->in_switch_statement_p = false;
9992 parser->fully_implicit_function_template_p = false;
9993 parser->implicit_template_parms = 0;
9994 parser->implicit_template_scope = 0;
9995 parser->auto_is_implicit_function_template_parm_p = false;
9997 /* By virtue of defining a local class, a lambda expression has access to
9998 the private variables of enclosing classes. */
10000 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10002 if (ok && cp_parser_error_occurred (parser))
10003 ok = false;
10005 if (ok)
10007 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10008 && cp_parser_start_tentative_firewall (parser))
10009 start = token;
10010 cp_parser_lambda_body (parser, lambda_expr);
10012 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10014 if (cp_parser_skip_to_closing_brace (parser))
10015 cp_lexer_consume_token (parser->lexer);
10018 /* The capture list was built up in reverse order; fix that now. */
10019 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10020 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10022 if (ok)
10023 maybe_add_lambda_conv_op (type);
10025 type = finish_struct (type, /*attributes=*/NULL_TREE);
10027 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10028 parser->in_statement = in_statement;
10029 parser->in_switch_statement_p = in_switch_statement_p;
10030 parser->fully_implicit_function_template_p
10031 = fully_implicit_function_template_p;
10032 parser->implicit_template_parms = implicit_template_parms;
10033 parser->implicit_template_scope = implicit_template_scope;
10034 parser->auto_is_implicit_function_template_parm_p
10035 = auto_is_implicit_function_template_parm_p;
10038 /* This field is only used during parsing of the lambda. */
10039 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10041 /* This lambda shouldn't have any proxies left at this point. */
10042 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10043 /* And now that we're done, push proxies for an enclosing lambda. */
10044 insert_pending_capture_proxies ();
10046 if (ok)
10047 lambda_expr = build_lambda_object (lambda_expr);
10048 else
10049 lambda_expr = error_mark_node;
10051 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10053 pop_deferring_access_checks ();
10055 return lambda_expr;
10058 /* Parse the beginning of a lambda expression.
10060 lambda-introducer:
10061 [ lambda-capture [opt] ]
10063 LAMBDA_EXPR is the current representation of the lambda expression. */
10065 static void
10066 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10068 /* Need commas after the first capture. */
10069 bool first = true;
10071 /* Eat the leading `['. */
10072 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10074 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10075 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10076 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10077 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10078 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10079 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10081 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10083 cp_lexer_consume_token (parser->lexer);
10084 first = false;
10087 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10089 cp_token* capture_token;
10090 tree capture_id;
10091 tree capture_init_expr;
10092 cp_id_kind idk = CP_ID_KIND_NONE;
10093 bool explicit_init_p = false;
10095 enum capture_kind_type
10097 BY_COPY,
10098 BY_REFERENCE
10100 enum capture_kind_type capture_kind = BY_COPY;
10102 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10104 error ("expected end of capture-list");
10105 return;
10108 if (first)
10109 first = false;
10110 else
10111 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10113 /* Possibly capture `this'. */
10114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10116 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10117 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10118 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10119 "with by-copy capture default");
10120 cp_lexer_consume_token (parser->lexer);
10121 add_capture (lambda_expr,
10122 /*id=*/this_identifier,
10123 /*initializer=*/finish_this_expr (),
10124 /*by_reference_p=*/true,
10125 explicit_init_p);
10126 continue;
10129 /* Possibly capture `*this'. */
10130 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10131 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10133 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10134 if (cxx_dialect < cxx1z)
10135 pedwarn (loc, 0, "%<*this%> capture only available with "
10136 "-std=c++1z or -std=gnu++1z");
10137 cp_lexer_consume_token (parser->lexer);
10138 cp_lexer_consume_token (parser->lexer);
10139 add_capture (lambda_expr,
10140 /*id=*/this_identifier,
10141 /*initializer=*/finish_this_expr (),
10142 /*by_reference_p=*/false,
10143 explicit_init_p);
10144 continue;
10147 /* Remember whether we want to capture as a reference or not. */
10148 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10150 capture_kind = BY_REFERENCE;
10151 cp_lexer_consume_token (parser->lexer);
10154 /* Get the identifier. */
10155 capture_token = cp_lexer_peek_token (parser->lexer);
10156 capture_id = cp_parser_identifier (parser);
10158 if (capture_id == error_mark_node)
10159 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10160 delimiters, but I modified this to stop on unnested ']' as well. It
10161 was already changed to stop on unnested '}', so the
10162 "closing_parenthesis" name is no more misleading with my change. */
10164 cp_parser_skip_to_closing_parenthesis (parser,
10165 /*recovering=*/true,
10166 /*or_comma=*/true,
10167 /*consume_paren=*/true);
10168 break;
10171 /* Find the initializer for this capture. */
10172 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10173 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10174 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10176 bool direct, non_constant;
10177 /* An explicit initializer exists. */
10178 if (cxx_dialect < cxx14)
10179 pedwarn (input_location, 0,
10180 "lambda capture initializers "
10181 "only available with -std=c++14 or -std=gnu++14");
10182 capture_init_expr = cp_parser_initializer (parser, &direct,
10183 &non_constant);
10184 explicit_init_p = true;
10185 if (capture_init_expr == NULL_TREE)
10187 error ("empty initializer for lambda init-capture");
10188 capture_init_expr = error_mark_node;
10191 else
10193 const char* error_msg;
10195 /* Turn the identifier into an id-expression. */
10196 capture_init_expr
10197 = cp_parser_lookup_name_simple (parser, capture_id,
10198 capture_token->location);
10200 if (capture_init_expr == error_mark_node)
10202 unqualified_name_lookup_error (capture_id);
10203 continue;
10205 else if (DECL_P (capture_init_expr)
10206 && (!VAR_P (capture_init_expr)
10207 && TREE_CODE (capture_init_expr) != PARM_DECL))
10209 error_at (capture_token->location,
10210 "capture of non-variable %qD ",
10211 capture_init_expr);
10212 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10213 "%q#D declared here", capture_init_expr);
10214 continue;
10216 if (VAR_P (capture_init_expr)
10217 && decl_storage_duration (capture_init_expr) != dk_auto)
10219 if (pedwarn (capture_token->location, 0, "capture of variable "
10220 "%qD with non-automatic storage duration",
10221 capture_init_expr))
10222 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10223 "%q#D declared here", capture_init_expr);
10224 continue;
10227 capture_init_expr
10228 = finish_id_expression
10229 (capture_id,
10230 capture_init_expr,
10231 parser->scope,
10232 &idk,
10233 /*integral_constant_expression_p=*/false,
10234 /*allow_non_integral_constant_expression_p=*/false,
10235 /*non_integral_constant_expression_p=*/NULL,
10236 /*template_p=*/false,
10237 /*done=*/true,
10238 /*address_p=*/false,
10239 /*template_arg_p=*/false,
10240 &error_msg,
10241 capture_token->location);
10243 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10245 cp_lexer_consume_token (parser->lexer);
10246 capture_init_expr = make_pack_expansion (capture_init_expr);
10248 else
10249 check_for_bare_parameter_packs (capture_init_expr);
10252 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10253 && !explicit_init_p)
10255 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10256 && capture_kind == BY_COPY)
10257 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10258 "of %qD redundant with by-copy capture default",
10259 capture_id);
10260 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10261 && capture_kind == BY_REFERENCE)
10262 pedwarn (capture_token->location, 0, "explicit by-reference "
10263 "capture of %qD redundant with by-reference capture "
10264 "default", capture_id);
10267 add_capture (lambda_expr,
10268 capture_id,
10269 capture_init_expr,
10270 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10271 explicit_init_p);
10274 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10277 /* Parse the (optional) middle of a lambda expression.
10279 lambda-declarator:
10280 < template-parameter-list [opt] >
10281 ( parameter-declaration-clause [opt] )
10282 attribute-specifier [opt]
10283 decl-specifier-seq [opt]
10284 exception-specification [opt]
10285 lambda-return-type-clause [opt]
10287 LAMBDA_EXPR is the current representation of the lambda expression. */
10289 static bool
10290 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10292 /* 5.1.1.4 of the standard says:
10293 If a lambda-expression does not include a lambda-declarator, it is as if
10294 the lambda-declarator were ().
10295 This means an empty parameter list, no attributes, and no exception
10296 specification. */
10297 tree param_list = void_list_node;
10298 tree attributes = NULL_TREE;
10299 tree exception_spec = NULL_TREE;
10300 tree template_param_list = NULL_TREE;
10301 tree tx_qual = NULL_TREE;
10302 cp_decl_specifier_seq lambda_specs;
10303 clear_decl_specs (&lambda_specs);
10305 /* The template-parameter-list is optional, but must begin with
10306 an opening angle if present. */
10307 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10309 if (cxx_dialect < cxx14)
10310 pedwarn (parser->lexer->next_token->location, 0,
10311 "lambda templates are only available with "
10312 "-std=c++14 or -std=gnu++14");
10313 else
10314 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10315 "ISO C++ does not support lambda templates");
10317 cp_lexer_consume_token (parser->lexer);
10319 template_param_list = cp_parser_template_parameter_list (parser);
10321 cp_parser_skip_to_end_of_template_parameter_list (parser);
10323 /* We just processed one more parameter list. */
10324 ++parser->num_template_parameter_lists;
10327 /* The parameter-declaration-clause is optional (unless
10328 template-parameter-list was given), but must begin with an
10329 opening parenthesis if present. */
10330 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10332 cp_lexer_consume_token (parser->lexer);
10334 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10336 /* Parse parameters. */
10337 param_list = cp_parser_parameter_declaration_clause (parser);
10339 /* Default arguments shall not be specified in the
10340 parameter-declaration-clause of a lambda-declarator. */
10341 if (cxx_dialect < cxx14)
10342 for (tree t = param_list; t; t = TREE_CHAIN (t))
10343 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10344 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10345 "default argument specified for lambda parameter");
10347 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10349 attributes = cp_parser_attributes_opt (parser);
10351 /* In the decl-specifier-seq of the lambda-declarator, each
10352 decl-specifier shall either be mutable or constexpr. */
10353 int declares_class_or_enum;
10354 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10355 cp_parser_decl_specifier_seq (parser,
10356 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10357 &lambda_specs, &declares_class_or_enum);
10358 if (lambda_specs.storage_class == sc_mutable)
10360 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10361 if (lambda_specs.conflicting_specifiers_p)
10362 error_at (lambda_specs.locations[ds_storage_class],
10363 "duplicate %<mutable%>");
10366 tx_qual = cp_parser_tx_qualifier_opt (parser);
10368 /* Parse optional exception specification. */
10369 exception_spec = cp_parser_exception_specification_opt (parser);
10371 /* Parse optional trailing return type. */
10372 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10374 cp_lexer_consume_token (parser->lexer);
10375 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10376 = cp_parser_trailing_type_id (parser);
10379 /* The function parameters must be in scope all the way until after the
10380 trailing-return-type in case of decltype. */
10381 pop_bindings_and_leave_scope ();
10383 else if (template_param_list != NULL_TREE) // generate diagnostic
10384 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10386 /* Create the function call operator.
10388 Messing with declarators like this is no uglier than building up the
10389 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10390 other code. */
10392 cp_decl_specifier_seq return_type_specs;
10393 cp_declarator* declarator;
10394 tree fco;
10395 int quals;
10396 void *p;
10398 clear_decl_specs (&return_type_specs);
10399 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10400 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10401 else
10402 /* Maybe we will deduce the return type later. */
10403 return_type_specs.type = make_auto ();
10405 if (lambda_specs.locations[ds_constexpr])
10407 if (cxx_dialect >= cxx1z)
10408 return_type_specs.locations[ds_constexpr]
10409 = lambda_specs.locations[ds_constexpr];
10410 else
10411 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10412 "lambda only available with -std=c++1z or -std=gnu++1z");
10415 p = obstack_alloc (&declarator_obstack, 0);
10417 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10418 sfk_none);
10420 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10421 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10422 declarator = make_call_declarator (declarator, param_list, quals,
10423 VIRT_SPEC_UNSPECIFIED,
10424 REF_QUAL_NONE,
10425 tx_qual,
10426 exception_spec,
10427 /*late_return_type=*/NULL_TREE,
10428 /*requires_clause*/NULL_TREE);
10429 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10431 fco = grokmethod (&return_type_specs,
10432 declarator,
10433 attributes);
10434 if (fco != error_mark_node)
10436 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10437 DECL_ARTIFICIAL (fco) = 1;
10438 /* Give the object parameter a different name. */
10439 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10440 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10441 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10443 if (template_param_list)
10445 fco = finish_member_template_decl (fco);
10446 finish_template_decl (template_param_list);
10447 --parser->num_template_parameter_lists;
10449 else if (parser->fully_implicit_function_template_p)
10450 fco = finish_fully_implicit_template (parser, fco);
10452 finish_member_declaration (fco);
10454 obstack_free (&declarator_obstack, p);
10456 return (fco != error_mark_node);
10460 /* Parse the body of a lambda expression, which is simply
10462 compound-statement
10464 but which requires special handling.
10465 LAMBDA_EXPR is the current representation of the lambda expression. */
10467 static void
10468 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10470 bool nested = (current_function_decl != NULL_TREE);
10471 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10472 if (nested)
10473 push_function_context ();
10474 else
10475 /* Still increment function_depth so that we don't GC in the
10476 middle of an expression. */
10477 ++function_depth;
10478 vec<tree> omp_privatization_save;
10479 save_omp_privatization_clauses (omp_privatization_save);
10480 /* Clear this in case we're in the middle of a default argument. */
10481 parser->local_variables_forbidden_p = false;
10483 /* Finish the function call operator
10484 - class_specifier
10485 + late_parsing_for_member
10486 + function_definition_after_declarator
10487 + ctor_initializer_opt_and_function_body */
10489 tree fco = lambda_function (lambda_expr);
10490 tree body;
10491 bool done = false;
10492 tree compound_stmt;
10493 tree cap;
10495 /* Let the front end know that we are going to be defining this
10496 function. */
10497 start_preparsed_function (fco,
10498 NULL_TREE,
10499 SF_PRE_PARSED | SF_INCLASS_INLINE);
10501 start_lambda_scope (fco);
10502 body = begin_function_body ();
10504 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10505 goto out;
10507 /* Push the proxies for any explicit captures. */
10508 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10509 cap = TREE_CHAIN (cap))
10510 build_capture_proxy (TREE_PURPOSE (cap));
10512 compound_stmt = begin_compound_stmt (0);
10514 /* 5.1.1.4 of the standard says:
10515 If a lambda-expression does not include a trailing-return-type, it
10516 is as if the trailing-return-type denotes the following type:
10517 * if the compound-statement is of the form
10518 { return attribute-specifier [opt] expression ; }
10519 the type of the returned expression after lvalue-to-rvalue
10520 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10521 (_conv.array_ 4.2), and function-to-pointer conversion
10522 (_conv.func_ 4.3);
10523 * otherwise, void. */
10525 /* In a lambda that has neither a lambda-return-type-clause
10526 nor a deducible form, errors should be reported for return statements
10527 in the body. Since we used void as the placeholder return type, parsing
10528 the body as usual will give such desired behavior. */
10529 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10530 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10531 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10533 tree expr = NULL_TREE;
10534 cp_id_kind idk = CP_ID_KIND_NONE;
10536 /* Parse tentatively in case there's more after the initial return
10537 statement. */
10538 cp_parser_parse_tentatively (parser);
10540 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10542 expr = cp_parser_expression (parser, &idk);
10544 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10545 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10547 if (cp_parser_parse_definitely (parser))
10549 if (!processing_template_decl)
10551 tree type = lambda_return_type (expr);
10552 apply_deduced_return_type (fco, type);
10553 if (type == error_mark_node)
10554 expr = error_mark_node;
10557 /* Will get error here if type not deduced yet. */
10558 finish_return_stmt (expr);
10560 done = true;
10564 if (!done)
10566 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10567 cp_parser_label_declaration (parser);
10568 cp_parser_statement_seq_opt (parser, NULL_TREE);
10569 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10572 finish_compound_stmt (compound_stmt);
10574 out:
10575 finish_function_body (body);
10576 finish_lambda_scope ();
10578 /* Finish the function and generate code for it if necessary. */
10579 tree fn = finish_function (/*inline*/2);
10581 /* Only expand if the call op is not a template. */
10582 if (!DECL_TEMPLATE_INFO (fco))
10583 expand_or_defer_fn (fn);
10586 restore_omp_privatization_clauses (omp_privatization_save);
10587 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10588 if (nested)
10589 pop_function_context();
10590 else
10591 --function_depth;
10594 /* Statements [gram.stmt.stmt] */
10596 /* Parse a statement.
10598 statement:
10599 labeled-statement
10600 expression-statement
10601 compound-statement
10602 selection-statement
10603 iteration-statement
10604 jump-statement
10605 declaration-statement
10606 try-block
10608 C++11:
10610 statement:
10611 labeled-statement
10612 attribute-specifier-seq (opt) expression-statement
10613 attribute-specifier-seq (opt) compound-statement
10614 attribute-specifier-seq (opt) selection-statement
10615 attribute-specifier-seq (opt) iteration-statement
10616 attribute-specifier-seq (opt) jump-statement
10617 declaration-statement
10618 attribute-specifier-seq (opt) try-block
10620 init-statement:
10621 expression-statement
10622 simple-declaration
10624 TM Extension:
10626 statement:
10627 atomic-statement
10629 IN_COMPOUND is true when the statement is nested inside a
10630 cp_parser_compound_statement; this matters for certain pragmas.
10632 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10633 is a (possibly labeled) if statement which is not enclosed in braces
10634 and has an else clause. This is used to implement -Wparentheses.
10636 CHAIN is a vector of if-else-if conditions. */
10638 static void
10639 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10640 bool in_compound, bool *if_p, vec<tree> *chain,
10641 location_t *loc_after_labels)
10643 tree statement, std_attrs = NULL_TREE;
10644 cp_token *token;
10645 location_t statement_location, attrs_location;
10647 restart:
10648 if (if_p != NULL)
10649 *if_p = false;
10650 /* There is no statement yet. */
10651 statement = NULL_TREE;
10653 saved_token_sentinel saved_tokens (parser->lexer);
10654 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10655 if (c_dialect_objc ())
10656 /* In obj-c++, seeing '[[' might be the either the beginning of
10657 c++11 attributes, or a nested objc-message-expression. So
10658 let's parse the c++11 attributes tentatively. */
10659 cp_parser_parse_tentatively (parser);
10660 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10661 if (c_dialect_objc ())
10663 if (!cp_parser_parse_definitely (parser))
10664 std_attrs = NULL_TREE;
10667 /* Peek at the next token. */
10668 token = cp_lexer_peek_token (parser->lexer);
10669 /* Remember the location of the first token in the statement. */
10670 statement_location = token->location;
10671 /* If this is a keyword, then that will often determine what kind of
10672 statement we have. */
10673 if (token->type == CPP_KEYWORD)
10675 enum rid keyword = token->keyword;
10677 switch (keyword)
10679 case RID_CASE:
10680 case RID_DEFAULT:
10681 /* Looks like a labeled-statement with a case label.
10682 Parse the label, and then use tail recursion to parse
10683 the statement. */
10684 cp_parser_label_for_labeled_statement (parser, std_attrs);
10685 in_compound = false;
10686 goto restart;
10688 case RID_IF:
10689 case RID_SWITCH:
10690 statement = cp_parser_selection_statement (parser, if_p, chain);
10691 break;
10693 case RID_WHILE:
10694 case RID_DO:
10695 case RID_FOR:
10696 statement = cp_parser_iteration_statement (parser, if_p, false);
10697 break;
10699 case RID_CILK_FOR:
10700 if (!flag_cilkplus)
10702 error_at (cp_lexer_peek_token (parser->lexer)->location,
10703 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10704 cp_lexer_consume_token (parser->lexer);
10705 statement = error_mark_node;
10707 else
10708 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10709 break;
10711 case RID_BREAK:
10712 case RID_CONTINUE:
10713 case RID_RETURN:
10714 case RID_GOTO:
10715 statement = cp_parser_jump_statement (parser);
10716 break;
10718 case RID_CILK_SYNC:
10719 cp_lexer_consume_token (parser->lexer);
10720 if (flag_cilkplus)
10722 tree sync_expr = build_cilk_sync ();
10723 SET_EXPR_LOCATION (sync_expr,
10724 token->location);
10725 statement = finish_expr_stmt (sync_expr);
10727 else
10729 error_at (token->location, "-fcilkplus must be enabled to use"
10730 " %<_Cilk_sync%>");
10731 statement = error_mark_node;
10733 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10734 break;
10736 /* Objective-C++ exception-handling constructs. */
10737 case RID_AT_TRY:
10738 case RID_AT_CATCH:
10739 case RID_AT_FINALLY:
10740 case RID_AT_SYNCHRONIZED:
10741 case RID_AT_THROW:
10742 statement = cp_parser_objc_statement (parser);
10743 break;
10745 case RID_TRY:
10746 statement = cp_parser_try_block (parser);
10747 break;
10749 case RID_NAMESPACE:
10750 /* This must be a namespace alias definition. */
10751 cp_parser_declaration_statement (parser);
10752 return;
10754 case RID_TRANSACTION_ATOMIC:
10755 case RID_TRANSACTION_RELAXED:
10756 case RID_SYNCHRONIZED:
10757 case RID_ATOMIC_NOEXCEPT:
10758 case RID_ATOMIC_CANCEL:
10759 statement = cp_parser_transaction (parser, token);
10760 break;
10761 case RID_TRANSACTION_CANCEL:
10762 statement = cp_parser_transaction_cancel (parser);
10763 break;
10765 default:
10766 /* It might be a keyword like `int' that can start a
10767 declaration-statement. */
10768 break;
10771 else if (token->type == CPP_NAME)
10773 /* If the next token is a `:', then we are looking at a
10774 labeled-statement. */
10775 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10776 if (token->type == CPP_COLON)
10778 /* Looks like a labeled-statement with an ordinary label.
10779 Parse the label, and then use tail recursion to parse
10780 the statement. */
10782 cp_parser_label_for_labeled_statement (parser, std_attrs);
10783 in_compound = false;
10784 goto restart;
10787 /* Anything that starts with a `{' must be a compound-statement. */
10788 else if (token->type == CPP_OPEN_BRACE)
10789 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10790 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10791 a statement all its own. */
10792 else if (token->type == CPP_PRAGMA)
10794 /* Only certain OpenMP pragmas are attached to statements, and thus
10795 are considered statements themselves. All others are not. In
10796 the context of a compound, accept the pragma as a "statement" and
10797 return so that we can check for a close brace. Otherwise we
10798 require a real statement and must go back and read one. */
10799 if (in_compound)
10800 cp_parser_pragma (parser, pragma_compound, if_p);
10801 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10802 goto restart;
10803 return;
10805 else if (token->type == CPP_EOF)
10807 cp_parser_error (parser, "expected statement");
10808 return;
10811 /* Everything else must be a declaration-statement or an
10812 expression-statement. Try for the declaration-statement
10813 first, unless we are looking at a `;', in which case we know that
10814 we have an expression-statement. */
10815 if (!statement)
10817 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10819 if (std_attrs != NULL_TREE)
10821 /* Attributes should be parsed as part of the the
10822 declaration, so let's un-parse them. */
10823 saved_tokens.rollback();
10824 std_attrs = NULL_TREE;
10827 cp_parser_parse_tentatively (parser);
10828 /* Try to parse the declaration-statement. */
10829 cp_parser_declaration_statement (parser);
10830 /* If that worked, we're done. */
10831 if (cp_parser_parse_definitely (parser))
10832 return;
10834 /* All preceding labels have been parsed at this point. */
10835 if (loc_after_labels != NULL)
10836 *loc_after_labels = statement_location;
10838 /* Look for an expression-statement instead. */
10839 statement = cp_parser_expression_statement (parser, in_statement_expr);
10841 /* Handle [[fallthrough]];. */
10842 if (attribute_fallthrough_p (std_attrs))
10844 /* The next token after the fallthrough attribute is ';'. */
10845 if (statement == NULL_TREE)
10847 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10848 statement = build_call_expr_internal_loc (statement_location,
10849 IFN_FALLTHROUGH,
10850 void_type_node, 0);
10851 finish_expr_stmt (statement);
10853 else
10854 warning_at (statement_location, OPT_Wattributes,
10855 "%<fallthrough%> attribute not followed by %<;%>");
10856 std_attrs = NULL_TREE;
10860 /* Set the line number for the statement. */
10861 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10862 SET_EXPR_LOCATION (statement, statement_location);
10864 /* Allow "[[fallthrough]];", but warn otherwise. */
10865 if (std_attrs != NULL_TREE)
10866 warning_at (attrs_location,
10867 OPT_Wattributes,
10868 "attributes at the beginning of statement are ignored");
10871 /* Parse the label for a labeled-statement, i.e.
10873 identifier :
10874 case constant-expression :
10875 default :
10877 GNU Extension:
10878 case constant-expression ... constant-expression : statement
10880 When a label is parsed without errors, the label is added to the
10881 parse tree by the finish_* functions, so this function doesn't
10882 have to return the label. */
10884 static void
10885 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10887 cp_token *token;
10888 tree label = NULL_TREE;
10889 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10891 /* The next token should be an identifier. */
10892 token = cp_lexer_peek_token (parser->lexer);
10893 if (token->type != CPP_NAME
10894 && token->type != CPP_KEYWORD)
10896 cp_parser_error (parser, "expected labeled-statement");
10897 return;
10900 /* Remember whether this case or a user-defined label is allowed to fall
10901 through to. */
10902 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10904 parser->colon_corrects_to_scope_p = false;
10905 switch (token->keyword)
10907 case RID_CASE:
10909 tree expr, expr_hi;
10910 cp_token *ellipsis;
10912 /* Consume the `case' token. */
10913 cp_lexer_consume_token (parser->lexer);
10914 /* Parse the constant-expression. */
10915 expr = cp_parser_constant_expression (parser);
10916 if (check_for_bare_parameter_packs (expr))
10917 expr = error_mark_node;
10919 ellipsis = cp_lexer_peek_token (parser->lexer);
10920 if (ellipsis->type == CPP_ELLIPSIS)
10922 /* Consume the `...' token. */
10923 cp_lexer_consume_token (parser->lexer);
10924 expr_hi = cp_parser_constant_expression (parser);
10925 if (check_for_bare_parameter_packs (expr_hi))
10926 expr_hi = error_mark_node;
10928 /* We don't need to emit warnings here, as the common code
10929 will do this for us. */
10931 else
10932 expr_hi = NULL_TREE;
10934 if (parser->in_switch_statement_p)
10936 tree l = finish_case_label (token->location, expr, expr_hi);
10937 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10938 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10940 else
10941 error_at (token->location,
10942 "case label %qE not within a switch statement",
10943 expr);
10945 break;
10947 case RID_DEFAULT:
10948 /* Consume the `default' token. */
10949 cp_lexer_consume_token (parser->lexer);
10951 if (parser->in_switch_statement_p)
10953 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10954 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10955 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10957 else
10958 error_at (token->location, "case label not within a switch statement");
10959 break;
10961 default:
10962 /* Anything else must be an ordinary label. */
10963 label = finish_label_stmt (cp_parser_identifier (parser));
10964 if (label && TREE_CODE (label) == LABEL_DECL)
10965 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10966 break;
10969 /* Require the `:' token. */
10970 cp_parser_require (parser, CPP_COLON, RT_COLON);
10972 /* An ordinary label may optionally be followed by attributes.
10973 However, this is only permitted if the attributes are then
10974 followed by a semicolon. This is because, for backward
10975 compatibility, when parsing
10976 lab: __attribute__ ((unused)) int i;
10977 we want the attribute to attach to "i", not "lab". */
10978 if (label != NULL_TREE
10979 && cp_next_tokens_can_be_gnu_attribute_p (parser))
10981 tree attrs;
10982 cp_parser_parse_tentatively (parser);
10983 attrs = cp_parser_gnu_attributes_opt (parser);
10984 if (attrs == NULL_TREE
10985 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10986 cp_parser_abort_tentative_parse (parser);
10987 else if (!cp_parser_parse_definitely (parser))
10989 else
10990 attributes = chainon (attributes, attrs);
10993 if (attributes != NULL_TREE)
10994 cplus_decl_attributes (&label, attributes, 0);
10996 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
10999 /* Parse an expression-statement.
11001 expression-statement:
11002 expression [opt] ;
11004 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11005 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11006 indicates whether this expression-statement is part of an
11007 expression statement. */
11009 static tree
11010 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11012 tree statement = NULL_TREE;
11013 cp_token *token = cp_lexer_peek_token (parser->lexer);
11014 location_t loc = token->location;
11016 /* There might be attribute fallthrough. */
11017 tree attr = cp_parser_gnu_attributes_opt (parser);
11019 /* If the next token is a ';', then there is no expression
11020 statement. */
11021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11023 statement = cp_parser_expression (parser);
11024 if (statement == error_mark_node
11025 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11027 cp_parser_skip_to_end_of_block_or_statement (parser);
11028 return error_mark_node;
11032 /* Handle [[fallthrough]];. */
11033 if (attribute_fallthrough_p (attr))
11035 /* The next token after the fallthrough attribute is ';'. */
11036 if (statement == NULL_TREE)
11037 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11038 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11039 void_type_node, 0);
11040 else
11041 warning_at (loc, OPT_Wattributes,
11042 "%<fallthrough%> attribute not followed by %<;%>");
11043 attr = NULL_TREE;
11046 /* Allow "[[fallthrough]];", but warn otherwise. */
11047 if (attr != NULL_TREE)
11048 warning_at (loc, OPT_Wattributes,
11049 "attributes at the beginning of statement are ignored");
11051 /* Give a helpful message for "A<T>::type t;" and the like. */
11052 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11053 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11055 if (TREE_CODE (statement) == SCOPE_REF)
11056 error_at (token->location, "need %<typename%> before %qE because "
11057 "%qT is a dependent scope",
11058 statement, TREE_OPERAND (statement, 0));
11059 else if (is_overloaded_fn (statement)
11060 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11062 /* A::A a; */
11063 tree fn = get_first_fn (statement);
11064 error_at (token->location,
11065 "%<%T::%D%> names the constructor, not the type",
11066 DECL_CONTEXT (fn), DECL_NAME (fn));
11070 /* Consume the final `;'. */
11071 cp_parser_consume_semicolon_at_end_of_statement (parser);
11073 if (in_statement_expr
11074 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11075 /* This is the final expression statement of a statement
11076 expression. */
11077 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11078 else if (statement)
11079 statement = finish_expr_stmt (statement);
11081 return statement;
11084 /* Parse a compound-statement.
11086 compound-statement:
11087 { statement-seq [opt] }
11089 GNU extension:
11091 compound-statement:
11092 { label-declaration-seq [opt] statement-seq [opt] }
11094 label-declaration-seq:
11095 label-declaration
11096 label-declaration-seq label-declaration
11098 Returns a tree representing the statement. */
11100 static tree
11101 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11102 int bcs_flags, bool function_body)
11104 tree compound_stmt;
11106 /* Consume the `{'. */
11107 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11108 return error_mark_node;
11109 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11110 && !function_body && cxx_dialect < cxx14)
11111 pedwarn (input_location, OPT_Wpedantic,
11112 "compound-statement in constexpr function");
11113 /* Begin the compound-statement. */
11114 compound_stmt = begin_compound_stmt (bcs_flags);
11115 /* If the next keyword is `__label__' we have a label declaration. */
11116 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11117 cp_parser_label_declaration (parser);
11118 /* Parse an (optional) statement-seq. */
11119 cp_parser_statement_seq_opt (parser, in_statement_expr);
11120 /* Finish the compound-statement. */
11121 finish_compound_stmt (compound_stmt);
11122 /* Consume the `}'. */
11123 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11125 return compound_stmt;
11128 /* Parse an (optional) statement-seq.
11130 statement-seq:
11131 statement
11132 statement-seq [opt] statement */
11134 static void
11135 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11137 /* Scan statements until there aren't any more. */
11138 while (true)
11140 cp_token *token = cp_lexer_peek_token (parser->lexer);
11142 /* If we are looking at a `}', then we have run out of
11143 statements; the same is true if we have reached the end
11144 of file, or have stumbled upon a stray '@end'. */
11145 if (token->type == CPP_CLOSE_BRACE
11146 || token->type == CPP_EOF
11147 || token->type == CPP_PRAGMA_EOL
11148 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11149 break;
11151 /* If we are in a compound statement and find 'else' then
11152 something went wrong. */
11153 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11155 if (parser->in_statement & IN_IF_STMT)
11156 break;
11157 else
11159 token = cp_lexer_consume_token (parser->lexer);
11160 error_at (token->location, "%<else%> without a previous %<if%>");
11164 /* Parse the statement. */
11165 cp_parser_statement (parser, in_statement_expr, true, NULL);
11169 /* Return true if we're looking at (init; cond), false otherwise. */
11171 static bool
11172 cp_parser_init_statement_p (cp_parser *parser)
11174 /* Save tokens so that we can put them back. */
11175 cp_lexer_save_tokens (parser->lexer);
11177 /* Look for ';' that is not nested in () or {}. */
11178 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11179 /*recovering=*/false,
11180 CPP_SEMICOLON,
11181 /*consume_paren=*/false);
11183 /* Roll back the tokens we skipped. */
11184 cp_lexer_rollback_tokens (parser->lexer);
11186 return ret == -1;
11189 /* Parse a selection-statement.
11191 selection-statement:
11192 if ( init-statement [opt] condition ) statement
11193 if ( init-statement [opt] condition ) statement else statement
11194 switch ( init-statement [opt] condition ) statement
11196 Returns the new IF_STMT or SWITCH_STMT.
11198 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11199 is a (possibly labeled) if statement which is not enclosed in
11200 braces and has an else clause. This is used to implement
11201 -Wparentheses.
11203 CHAIN is a vector of if-else-if conditions. This is used to implement
11204 -Wduplicated-cond. */
11206 static tree
11207 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11208 vec<tree> *chain)
11210 cp_token *token;
11211 enum rid keyword;
11212 token_indent_info guard_tinfo;
11214 if (if_p != NULL)
11215 *if_p = false;
11217 /* Peek at the next token. */
11218 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11219 guard_tinfo = get_token_indent_info (token);
11221 /* See what kind of keyword it is. */
11222 keyword = token->keyword;
11223 switch (keyword)
11225 case RID_IF:
11226 case RID_SWITCH:
11228 tree statement;
11229 tree condition;
11231 bool cx = false;
11232 if (keyword == RID_IF
11233 && cp_lexer_next_token_is_keyword (parser->lexer,
11234 RID_CONSTEXPR))
11236 cx = true;
11237 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11238 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11239 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11240 "with -std=c++1z or -std=gnu++1z");
11243 /* Look for the `('. */
11244 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11246 cp_parser_skip_to_end_of_statement (parser);
11247 return error_mark_node;
11250 /* Begin the selection-statement. */
11251 if (keyword == RID_IF)
11253 statement = begin_if_stmt ();
11254 IF_STMT_CONSTEXPR_P (statement) = cx;
11256 else
11257 statement = begin_switch_stmt ();
11259 /* Parse the optional init-statement. */
11260 if (cp_parser_init_statement_p (parser))
11262 tree decl;
11263 if (cxx_dialect < cxx1z)
11264 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11265 "init-statement in selection statements only available "
11266 "with -std=c++1z or -std=gnu++1z");
11267 cp_parser_init_statement (parser, &decl);
11270 /* Parse the condition. */
11271 condition = cp_parser_condition (parser);
11272 /* Look for the `)'. */
11273 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11274 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11275 /*consume_paren=*/true);
11277 if (keyword == RID_IF)
11279 bool nested_if;
11280 unsigned char in_statement;
11282 /* Add the condition. */
11283 condition = finish_if_stmt_cond (condition, statement);
11285 if (warn_duplicated_cond)
11286 warn_duplicated_cond_add_or_warn (token->location, condition,
11287 &chain);
11289 /* Parse the then-clause. */
11290 in_statement = parser->in_statement;
11291 parser->in_statement |= IN_IF_STMT;
11293 /* Outside a template, the non-selected branch of a constexpr
11294 if is a 'discarded statement', i.e. unevaluated. */
11295 bool was_discarded = in_discarded_stmt;
11296 bool discard_then = (cx && !processing_template_decl
11297 && integer_zerop (condition));
11298 if (discard_then)
11300 in_discarded_stmt = true;
11301 ++c_inhibit_evaluation_warnings;
11304 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11305 guard_tinfo);
11307 parser->in_statement = in_statement;
11309 finish_then_clause (statement);
11311 if (discard_then)
11313 THEN_CLAUSE (statement) = NULL_TREE;
11314 in_discarded_stmt = was_discarded;
11315 --c_inhibit_evaluation_warnings;
11318 /* If the next token is `else', parse the else-clause. */
11319 if (cp_lexer_next_token_is_keyword (parser->lexer,
11320 RID_ELSE))
11322 bool discard_else = (cx && !processing_template_decl
11323 && integer_nonzerop (condition));
11324 if (discard_else)
11326 in_discarded_stmt = true;
11327 ++c_inhibit_evaluation_warnings;
11330 guard_tinfo
11331 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11332 /* Consume the `else' keyword. */
11333 cp_lexer_consume_token (parser->lexer);
11334 if (warn_duplicated_cond)
11336 if (cp_lexer_next_token_is_keyword (parser->lexer,
11337 RID_IF)
11338 && chain == NULL)
11340 /* We've got "if (COND) else if (COND2)". Start
11341 the condition chain and add COND as the first
11342 element. */
11343 chain = new vec<tree> ();
11344 if (!CONSTANT_CLASS_P (condition)
11345 && !TREE_SIDE_EFFECTS (condition))
11347 /* Wrap it in a NOP_EXPR so that we can set the
11348 location of the condition. */
11349 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11350 condition);
11351 SET_EXPR_LOCATION (e, token->location);
11352 chain->safe_push (e);
11355 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11356 RID_IF))
11358 /* This is if-else without subsequent if. Zap the
11359 condition chain; we would have already warned at
11360 this point. */
11361 delete chain;
11362 chain = NULL;
11365 begin_else_clause (statement);
11366 /* Parse the else-clause. */
11367 cp_parser_implicitly_scoped_statement (parser, NULL,
11368 guard_tinfo, chain);
11370 finish_else_clause (statement);
11372 /* If we are currently parsing a then-clause, then
11373 IF_P will not be NULL. We set it to true to
11374 indicate that this if statement has an else clause.
11375 This may trigger the Wparentheses warning below
11376 when we get back up to the parent if statement. */
11377 if (if_p != NULL)
11378 *if_p = true;
11380 if (discard_else)
11382 ELSE_CLAUSE (statement) = NULL_TREE;
11383 in_discarded_stmt = was_discarded;
11384 --c_inhibit_evaluation_warnings;
11387 else
11389 /* This if statement does not have an else clause. If
11390 NESTED_IF is true, then the then-clause has an if
11391 statement which does have an else clause. We warn
11392 about the potential ambiguity. */
11393 if (nested_if)
11394 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11395 "suggest explicit braces to avoid ambiguous"
11396 " %<else%>");
11397 if (warn_duplicated_cond)
11399 /* We don't need the condition chain anymore. */
11400 delete chain;
11401 chain = NULL;
11405 /* Now we're all done with the if-statement. */
11406 finish_if_stmt (statement);
11408 else
11410 bool in_switch_statement_p;
11411 unsigned char in_statement;
11413 /* Add the condition. */
11414 finish_switch_cond (condition, statement);
11416 /* Parse the body of the switch-statement. */
11417 in_switch_statement_p = parser->in_switch_statement_p;
11418 in_statement = parser->in_statement;
11419 parser->in_switch_statement_p = true;
11420 parser->in_statement |= IN_SWITCH_STMT;
11421 cp_parser_implicitly_scoped_statement (parser, if_p,
11422 guard_tinfo);
11423 parser->in_switch_statement_p = in_switch_statement_p;
11424 parser->in_statement = in_statement;
11426 /* Now we're all done with the switch-statement. */
11427 finish_switch_stmt (statement);
11430 return statement;
11432 break;
11434 default:
11435 cp_parser_error (parser, "expected selection-statement");
11436 return error_mark_node;
11440 /* Parse a condition.
11442 condition:
11443 expression
11444 type-specifier-seq declarator = initializer-clause
11445 type-specifier-seq declarator braced-init-list
11447 GNU Extension:
11449 condition:
11450 type-specifier-seq declarator asm-specification [opt]
11451 attributes [opt] = assignment-expression
11453 Returns the expression that should be tested. */
11455 static tree
11456 cp_parser_condition (cp_parser* parser)
11458 cp_decl_specifier_seq type_specifiers;
11459 const char *saved_message;
11460 int declares_class_or_enum;
11462 /* Try the declaration first. */
11463 cp_parser_parse_tentatively (parser);
11464 /* New types are not allowed in the type-specifier-seq for a
11465 condition. */
11466 saved_message = parser->type_definition_forbidden_message;
11467 parser->type_definition_forbidden_message
11468 = G_("types may not be defined in conditions");
11469 /* Parse the type-specifier-seq. */
11470 cp_parser_decl_specifier_seq (parser,
11471 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11472 &type_specifiers,
11473 &declares_class_or_enum);
11474 /* Restore the saved message. */
11475 parser->type_definition_forbidden_message = saved_message;
11476 /* If all is well, we might be looking at a declaration. */
11477 if (!cp_parser_error_occurred (parser))
11479 tree decl;
11480 tree asm_specification;
11481 tree attributes;
11482 cp_declarator *declarator;
11483 tree initializer = NULL_TREE;
11485 /* Parse the declarator. */
11486 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11487 /*ctor_dtor_or_conv_p=*/NULL,
11488 /*parenthesized_p=*/NULL,
11489 /*member_p=*/false,
11490 /*friend_p=*/false);
11491 /* Parse the attributes. */
11492 attributes = cp_parser_attributes_opt (parser);
11493 /* Parse the asm-specification. */
11494 asm_specification = cp_parser_asm_specification_opt (parser);
11495 /* If the next token is not an `=' or '{', then we might still be
11496 looking at an expression. For example:
11498 if (A(a).x)
11500 looks like a decl-specifier-seq and a declarator -- but then
11501 there is no `=', so this is an expression. */
11502 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11503 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11504 cp_parser_simulate_error (parser);
11506 /* If we did see an `=' or '{', then we are looking at a declaration
11507 for sure. */
11508 if (cp_parser_parse_definitely (parser))
11510 tree pushed_scope;
11511 bool non_constant_p;
11512 int flags = LOOKUP_ONLYCONVERTING;
11514 /* Create the declaration. */
11515 decl = start_decl (declarator, &type_specifiers,
11516 /*initialized_p=*/true,
11517 attributes, /*prefix_attributes=*/NULL_TREE,
11518 &pushed_scope);
11520 /* Parse the initializer. */
11521 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11523 initializer = cp_parser_braced_list (parser, &non_constant_p);
11524 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11525 flags = 0;
11527 else
11529 /* Consume the `='. */
11530 cp_parser_require (parser, CPP_EQ, RT_EQ);
11531 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11533 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11534 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11536 /* Process the initializer. */
11537 cp_finish_decl (decl,
11538 initializer, !non_constant_p,
11539 asm_specification,
11540 flags);
11542 if (pushed_scope)
11543 pop_scope (pushed_scope);
11545 return convert_from_reference (decl);
11548 /* If we didn't even get past the declarator successfully, we are
11549 definitely not looking at a declaration. */
11550 else
11551 cp_parser_abort_tentative_parse (parser);
11553 /* Otherwise, we are looking at an expression. */
11554 return cp_parser_expression (parser);
11557 /* Parses a for-statement or range-for-statement until the closing ')',
11558 not included. */
11560 static tree
11561 cp_parser_for (cp_parser *parser, bool ivdep)
11563 tree init, scope, decl;
11564 bool is_range_for;
11566 /* Begin the for-statement. */
11567 scope = begin_for_scope (&init);
11569 /* Parse the initialization. */
11570 is_range_for = cp_parser_init_statement (parser, &decl);
11572 if (is_range_for)
11573 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11574 else
11575 return cp_parser_c_for (parser, scope, init, ivdep);
11578 static tree
11579 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11581 /* Normal for loop */
11582 tree condition = NULL_TREE;
11583 tree expression = NULL_TREE;
11584 tree stmt;
11586 stmt = begin_for_stmt (scope, init);
11587 /* The init-statement has already been parsed in
11588 cp_parser_init_statement, so no work is needed here. */
11589 finish_init_stmt (stmt);
11591 /* If there's a condition, process it. */
11592 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11593 condition = cp_parser_condition (parser);
11594 else if (ivdep)
11596 cp_parser_error (parser, "missing loop condition in loop with "
11597 "%<GCC ivdep%> pragma");
11598 condition = error_mark_node;
11600 finish_for_cond (condition, stmt, ivdep);
11601 /* Look for the `;'. */
11602 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11604 /* If there's an expression, process it. */
11605 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11606 expression = cp_parser_expression (parser);
11607 finish_for_expr (expression, stmt);
11609 return stmt;
11612 /* Tries to parse a range-based for-statement:
11614 range-based-for:
11615 decl-specifier-seq declarator : expression
11617 The decl-specifier-seq declarator and the `:' are already parsed by
11618 cp_parser_init_statement. If processing_template_decl it returns a
11619 newly created RANGE_FOR_STMT; if not, it is converted to a
11620 regular FOR_STMT. */
11622 static tree
11623 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11624 bool ivdep)
11626 tree stmt, range_expr;
11627 auto_vec <cxx_binding *, 16> bindings;
11628 auto_vec <tree, 16> names;
11629 tree decomp_first_name = NULL_TREE;
11630 unsigned int decomp_cnt = 0;
11632 /* Get the range declaration momentarily out of the way so that
11633 the range expression doesn't clash with it. */
11634 if (range_decl != error_mark_node)
11636 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11638 tree v = DECL_VALUE_EXPR (range_decl);
11639 /* For decomposition declaration get all of the corresponding
11640 declarations out of the way. */
11641 if (TREE_CODE (v) == ARRAY_REF
11642 && VAR_P (TREE_OPERAND (v, 0))
11643 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11645 tree d = range_decl;
11646 range_decl = TREE_OPERAND (v, 0);
11647 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11648 decomp_first_name = d;
11649 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11651 tree name = DECL_NAME (d);
11652 names.safe_push (name);
11653 bindings.safe_push (IDENTIFIER_BINDING (name));
11654 IDENTIFIER_BINDING (name)
11655 = IDENTIFIER_BINDING (name)->previous;
11659 if (names.is_empty ())
11661 tree name = DECL_NAME (range_decl);
11662 names.safe_push (name);
11663 bindings.safe_push (IDENTIFIER_BINDING (name));
11664 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11670 bool expr_non_constant_p;
11671 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11673 else
11674 range_expr = cp_parser_expression (parser);
11676 /* Put the range declaration(s) back into scope. */
11677 for (unsigned int i = 0; i < names.length (); i++)
11679 cxx_binding *binding = bindings[i];
11680 binding->previous = IDENTIFIER_BINDING (names[i]);
11681 IDENTIFIER_BINDING (names[i]) = binding;
11684 /* If in template, STMT is converted to a normal for-statement
11685 at instantiation. If not, it is done just ahead. */
11686 if (processing_template_decl)
11688 if (check_for_bare_parameter_packs (range_expr))
11689 range_expr = error_mark_node;
11690 stmt = begin_range_for_stmt (scope, init);
11691 if (ivdep)
11692 RANGE_FOR_IVDEP (stmt) = 1;
11693 finish_range_for_decl (stmt, range_decl, range_expr);
11694 if (!type_dependent_expression_p (range_expr)
11695 /* do_auto_deduction doesn't mess with template init-lists. */
11696 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11697 do_range_for_auto_deduction (range_decl, range_expr);
11699 else
11701 stmt = begin_for_stmt (scope, init);
11702 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11703 decomp_first_name, decomp_cnt, ivdep);
11705 return stmt;
11708 /* Subroutine of cp_convert_range_for: given the initializer expression,
11709 builds up the range temporary. */
11711 static tree
11712 build_range_temp (tree range_expr)
11714 tree range_type, range_temp;
11716 /* Find out the type deduced by the declaration
11717 `auto &&__range = range_expr'. */
11718 range_type = cp_build_reference_type (make_auto (), true);
11719 range_type = do_auto_deduction (range_type, range_expr,
11720 type_uses_auto (range_type));
11722 /* Create the __range variable. */
11723 range_temp = build_decl (input_location, VAR_DECL,
11724 get_identifier ("__for_range"), range_type);
11725 TREE_USED (range_temp) = 1;
11726 DECL_ARTIFICIAL (range_temp) = 1;
11728 return range_temp;
11731 /* Used by cp_parser_range_for in template context: we aren't going to
11732 do a full conversion yet, but we still need to resolve auto in the
11733 type of the for-range-declaration if present. This is basically
11734 a shortcut version of cp_convert_range_for. */
11736 static void
11737 do_range_for_auto_deduction (tree decl, tree range_expr)
11739 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11740 if (auto_node)
11742 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11743 range_temp = convert_from_reference (build_range_temp (range_expr));
11744 iter_type = (cp_parser_perform_range_for_lookup
11745 (range_temp, &begin_dummy, &end_dummy));
11746 if (iter_type)
11748 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11749 iter_type);
11750 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11751 tf_warning_or_error);
11752 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11753 iter_decl, auto_node);
11758 /* Converts a range-based for-statement into a normal
11759 for-statement, as per the definition.
11761 for (RANGE_DECL : RANGE_EXPR)
11762 BLOCK
11764 should be equivalent to:
11767 auto &&__range = RANGE_EXPR;
11768 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11769 __begin != __end;
11770 ++__begin)
11772 RANGE_DECL = *__begin;
11773 BLOCK
11777 If RANGE_EXPR is an array:
11778 BEGIN_EXPR = __range
11779 END_EXPR = __range + ARRAY_SIZE(__range)
11780 Else if RANGE_EXPR has a member 'begin' or 'end':
11781 BEGIN_EXPR = __range.begin()
11782 END_EXPR = __range.end()
11783 Else:
11784 BEGIN_EXPR = begin(__range)
11785 END_EXPR = end(__range);
11787 If __range has a member 'begin' but not 'end', or vice versa, we must
11788 still use the second alternative (it will surely fail, however).
11789 When calling begin()/end() in the third alternative we must use
11790 argument dependent lookup, but always considering 'std' as an associated
11791 namespace. */
11793 tree
11794 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11795 tree decomp_first_name, unsigned int decomp_cnt,
11796 bool ivdep)
11798 tree begin, end;
11799 tree iter_type, begin_expr, end_expr;
11800 tree condition, expression;
11802 if (range_decl == error_mark_node || range_expr == error_mark_node)
11803 /* If an error happened previously do nothing or else a lot of
11804 unhelpful errors would be issued. */
11805 begin_expr = end_expr = iter_type = error_mark_node;
11806 else
11808 tree range_temp;
11810 if (VAR_P (range_expr)
11811 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11812 /* Can't bind a reference to an array of runtime bound. */
11813 range_temp = range_expr;
11814 else
11816 range_temp = build_range_temp (range_expr);
11817 pushdecl (range_temp);
11818 cp_finish_decl (range_temp, range_expr,
11819 /*is_constant_init*/false, NULL_TREE,
11820 LOOKUP_ONLYCONVERTING);
11821 range_temp = convert_from_reference (range_temp);
11823 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11824 &begin_expr, &end_expr);
11827 /* The new for initialization statement. */
11828 begin = build_decl (input_location, VAR_DECL,
11829 get_identifier ("__for_begin"), iter_type);
11830 TREE_USED (begin) = 1;
11831 DECL_ARTIFICIAL (begin) = 1;
11832 pushdecl (begin);
11833 cp_finish_decl (begin, begin_expr,
11834 /*is_constant_init*/false, NULL_TREE,
11835 LOOKUP_ONLYCONVERTING);
11837 if (cxx_dialect >= cxx1z)
11838 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11839 end = build_decl (input_location, VAR_DECL,
11840 get_identifier ("__for_end"), iter_type);
11841 TREE_USED (end) = 1;
11842 DECL_ARTIFICIAL (end) = 1;
11843 pushdecl (end);
11844 cp_finish_decl (end, end_expr,
11845 /*is_constant_init*/false, NULL_TREE,
11846 LOOKUP_ONLYCONVERTING);
11848 finish_init_stmt (statement);
11850 /* The new for condition. */
11851 condition = build_x_binary_op (input_location, NE_EXPR,
11852 begin, ERROR_MARK,
11853 end, ERROR_MARK,
11854 NULL, tf_warning_or_error);
11855 finish_for_cond (condition, statement, ivdep);
11857 /* The new increment expression. */
11858 expression = finish_unary_op_expr (input_location,
11859 PREINCREMENT_EXPR, begin,
11860 tf_warning_or_error);
11861 finish_for_expr (expression, statement);
11863 /* The declaration is initialized with *__begin inside the loop body. */
11864 cp_finish_decl (range_decl,
11865 build_x_indirect_ref (input_location, begin, RO_NULL,
11866 tf_warning_or_error),
11867 /*is_constant_init*/false, NULL_TREE,
11868 LOOKUP_ONLYCONVERTING);
11869 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11870 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11872 return statement;
11875 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11876 We need to solve both at the same time because the method used
11877 depends on the existence of members begin or end.
11878 Returns the type deduced for the iterator expression. */
11880 static tree
11881 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11883 if (error_operand_p (range))
11885 *begin = *end = error_mark_node;
11886 return error_mark_node;
11889 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11891 error ("range-based %<for%> expression of type %qT "
11892 "has incomplete type", TREE_TYPE (range));
11893 *begin = *end = error_mark_node;
11894 return error_mark_node;
11896 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11898 /* If RANGE is an array, we will use pointer arithmetic. */
11899 *begin = decay_conversion (range, tf_warning_or_error);
11900 *end = build_binary_op (input_location, PLUS_EXPR,
11901 range,
11902 array_type_nelts_top (TREE_TYPE (range)),
11904 return TREE_TYPE (*begin);
11906 else
11908 /* If it is not an array, we must do a bit of magic. */
11909 tree id_begin, id_end;
11910 tree member_begin, member_end;
11912 *begin = *end = error_mark_node;
11914 id_begin = get_identifier ("begin");
11915 id_end = get_identifier ("end");
11916 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11917 /*protect=*/2, /*want_type=*/false,
11918 tf_warning_or_error);
11919 member_end = lookup_member (TREE_TYPE (range), id_end,
11920 /*protect=*/2, /*want_type=*/false,
11921 tf_warning_or_error);
11923 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11925 /* Use the member functions. */
11926 if (member_begin != NULL_TREE)
11927 *begin = cp_parser_range_for_member_function (range, id_begin);
11928 else
11929 error ("range-based %<for%> expression of type %qT has an "
11930 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11932 if (member_end != NULL_TREE)
11933 *end = cp_parser_range_for_member_function (range, id_end);
11934 else
11935 error ("range-based %<for%> expression of type %qT has a "
11936 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11938 else
11940 /* Use global functions with ADL. */
11941 vec<tree, va_gc> *vec;
11942 vec = make_tree_vector ();
11944 vec_safe_push (vec, range);
11946 member_begin = perform_koenig_lookup (id_begin, vec,
11947 tf_warning_or_error);
11948 *begin = finish_call_expr (member_begin, &vec, false, true,
11949 tf_warning_or_error);
11950 member_end = perform_koenig_lookup (id_end, vec,
11951 tf_warning_or_error);
11952 *end = finish_call_expr (member_end, &vec, false, true,
11953 tf_warning_or_error);
11955 release_tree_vector (vec);
11958 /* Last common checks. */
11959 if (*begin == error_mark_node || *end == error_mark_node)
11961 /* If one of the expressions is an error do no more checks. */
11962 *begin = *end = error_mark_node;
11963 return error_mark_node;
11965 else if (type_dependent_expression_p (*begin)
11966 || type_dependent_expression_p (*end))
11967 /* Can happen, when, eg, in a template context, Koenig lookup
11968 can't resolve begin/end (c++/58503). */
11969 return NULL_TREE;
11970 else
11972 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11973 /* The unqualified type of the __begin and __end temporaries should
11974 be the same, as required by the multiple auto declaration. */
11975 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
11977 if (cxx_dialect >= cxx1z
11978 && (build_x_binary_op (input_location, NE_EXPR,
11979 *begin, ERROR_MARK,
11980 *end, ERROR_MARK,
11981 NULL, tf_none)
11982 != error_mark_node))
11983 /* P0184R0 allows __begin and __end to have different types,
11984 but make sure they are comparable so we can give a better
11985 diagnostic. */;
11986 else
11987 error ("inconsistent begin/end types in range-based %<for%> "
11988 "statement: %qT and %qT",
11989 TREE_TYPE (*begin), TREE_TYPE (*end));
11991 return iter_type;
11996 /* Helper function for cp_parser_perform_range_for_lookup.
11997 Builds a tree for RANGE.IDENTIFIER(). */
11999 static tree
12000 cp_parser_range_for_member_function (tree range, tree identifier)
12002 tree member, res;
12003 vec<tree, va_gc> *vec;
12005 member = finish_class_member_access_expr (range, identifier,
12006 false, tf_warning_or_error);
12007 if (member == error_mark_node)
12008 return error_mark_node;
12010 vec = make_tree_vector ();
12011 res = finish_call_expr (member, &vec,
12012 /*disallow_virtual=*/false,
12013 /*koenig_p=*/false,
12014 tf_warning_or_error);
12015 release_tree_vector (vec);
12016 return res;
12019 /* Parse an iteration-statement.
12021 iteration-statement:
12022 while ( condition ) statement
12023 do statement while ( expression ) ;
12024 for ( init-statement condition [opt] ; expression [opt] )
12025 statement
12027 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12029 static tree
12030 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12032 cp_token *token;
12033 enum rid keyword;
12034 tree statement;
12035 unsigned char in_statement;
12036 token_indent_info guard_tinfo;
12038 /* Peek at the next token. */
12039 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
12040 if (!token)
12041 return error_mark_node;
12043 guard_tinfo = get_token_indent_info (token);
12045 /* Remember whether or not we are already within an iteration
12046 statement. */
12047 in_statement = parser->in_statement;
12049 /* See what kind of keyword it is. */
12050 keyword = token->keyword;
12051 switch (keyword)
12053 case RID_WHILE:
12055 tree condition;
12057 /* Begin the while-statement. */
12058 statement = begin_while_stmt ();
12059 /* Look for the `('. */
12060 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12061 /* Parse the condition. */
12062 condition = cp_parser_condition (parser);
12063 finish_while_stmt_cond (condition, statement, ivdep);
12064 /* Look for the `)'. */
12065 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12066 /* Parse the dependent statement. */
12067 parser->in_statement = IN_ITERATION_STMT;
12068 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12069 parser->in_statement = in_statement;
12070 /* We're done with the while-statement. */
12071 finish_while_stmt (statement);
12073 break;
12075 case RID_DO:
12077 tree expression;
12079 /* Begin the do-statement. */
12080 statement = begin_do_stmt ();
12081 /* Parse the body of the do-statement. */
12082 parser->in_statement = IN_ITERATION_STMT;
12083 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12084 parser->in_statement = in_statement;
12085 finish_do_body (statement);
12086 /* Look for the `while' keyword. */
12087 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12088 /* Look for the `('. */
12089 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12090 /* Parse the expression. */
12091 expression = cp_parser_expression (parser);
12092 /* We're done with the do-statement. */
12093 finish_do_stmt (expression, statement, ivdep);
12094 /* Look for the `)'. */
12095 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12096 /* Look for the `;'. */
12097 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12099 break;
12101 case RID_FOR:
12103 /* Look for the `('. */
12104 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12106 statement = cp_parser_for (parser, ivdep);
12108 /* Look for the `)'. */
12109 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12111 /* Parse the body of the for-statement. */
12112 parser->in_statement = IN_ITERATION_STMT;
12113 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12114 parser->in_statement = in_statement;
12116 /* We're done with the for-statement. */
12117 finish_for_stmt (statement);
12119 break;
12121 default:
12122 cp_parser_error (parser, "expected iteration-statement");
12123 statement = error_mark_node;
12124 break;
12127 return statement;
12130 /* Parse a init-statement or the declarator of a range-based-for.
12131 Returns true if a range-based-for declaration is seen.
12133 init-statement:
12134 expression-statement
12135 simple-declaration */
12137 static bool
12138 cp_parser_init_statement (cp_parser* parser, tree *decl)
12140 /* If the next token is a `;', then we have an empty
12141 expression-statement. Grammatically, this is also a
12142 simple-declaration, but an invalid one, because it does not
12143 declare anything. Therefore, if we did not handle this case
12144 specially, we would issue an error message about an invalid
12145 declaration. */
12146 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12148 bool is_range_for = false;
12149 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12151 /* A colon is used in range-based for. */
12152 parser->colon_corrects_to_scope_p = false;
12154 /* We're going to speculatively look for a declaration, falling back
12155 to an expression, if necessary. */
12156 cp_parser_parse_tentatively (parser);
12157 /* Parse the declaration. */
12158 cp_parser_simple_declaration (parser,
12159 /*function_definition_allowed_p=*/false,
12160 decl);
12161 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12162 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12164 /* It is a range-for, consume the ':' */
12165 cp_lexer_consume_token (parser->lexer);
12166 is_range_for = true;
12167 if (cxx_dialect < cxx11)
12169 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12170 "range-based %<for%> loops only available with "
12171 "-std=c++11 or -std=gnu++11");
12172 *decl = error_mark_node;
12175 else
12176 /* The ';' is not consumed yet because we told
12177 cp_parser_simple_declaration not to. */
12178 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12180 if (cp_parser_parse_definitely (parser))
12181 return is_range_for;
12182 /* If the tentative parse failed, then we shall need to look for an
12183 expression-statement. */
12185 /* If we are here, it is an expression-statement. */
12186 cp_parser_expression_statement (parser, NULL_TREE);
12187 return false;
12190 /* Parse a jump-statement.
12192 jump-statement:
12193 break ;
12194 continue ;
12195 return expression [opt] ;
12196 return braced-init-list ;
12197 goto identifier ;
12199 GNU extension:
12201 jump-statement:
12202 goto * expression ;
12204 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12206 static tree
12207 cp_parser_jump_statement (cp_parser* parser)
12209 tree statement = error_mark_node;
12210 cp_token *token;
12211 enum rid keyword;
12212 unsigned char in_statement;
12214 /* Peek at the next token. */
12215 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12216 if (!token)
12217 return error_mark_node;
12219 /* See what kind of keyword it is. */
12220 keyword = token->keyword;
12221 switch (keyword)
12223 case RID_BREAK:
12224 in_statement = parser->in_statement & ~IN_IF_STMT;
12225 switch (in_statement)
12227 case 0:
12228 error_at (token->location, "break statement not within loop or switch");
12229 break;
12230 default:
12231 gcc_assert ((in_statement & IN_SWITCH_STMT)
12232 || in_statement == IN_ITERATION_STMT);
12233 statement = finish_break_stmt ();
12234 if (in_statement == IN_ITERATION_STMT)
12235 break_maybe_infinite_loop ();
12236 break;
12237 case IN_OMP_BLOCK:
12238 error_at (token->location, "invalid exit from OpenMP structured block");
12239 break;
12240 case IN_OMP_FOR:
12241 error_at (token->location, "break statement used with OpenMP for loop");
12242 break;
12243 case IN_CILK_SIMD_FOR:
12244 error_at (token->location, "break statement used with Cilk Plus for loop");
12245 break;
12247 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12248 break;
12250 case RID_CONTINUE:
12251 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12253 case 0:
12254 error_at (token->location, "continue statement not within a loop");
12255 break;
12256 case IN_CILK_SIMD_FOR:
12257 error_at (token->location,
12258 "continue statement within %<#pragma simd%> loop body");
12259 /* Fall through. */
12260 case IN_ITERATION_STMT:
12261 case IN_OMP_FOR:
12262 statement = finish_continue_stmt ();
12263 break;
12264 case IN_OMP_BLOCK:
12265 error_at (token->location, "invalid exit from OpenMP structured block");
12266 break;
12267 default:
12268 gcc_unreachable ();
12270 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12271 break;
12273 case RID_RETURN:
12275 tree expr;
12276 bool expr_non_constant_p;
12278 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12280 cp_lexer_set_source_position (parser->lexer);
12281 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12282 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12284 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12285 expr = cp_parser_expression (parser);
12286 else
12287 /* If the next token is a `;', then there is no
12288 expression. */
12289 expr = NULL_TREE;
12290 /* Build the return-statement. */
12291 if (current_function_auto_return_pattern && in_discarded_stmt)
12292 /* Don't deduce from a discarded return statement. */;
12293 else
12294 statement = finish_return_stmt (expr);
12295 /* Look for the final `;'. */
12296 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12298 break;
12300 case RID_GOTO:
12301 if (parser->in_function_body
12302 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12304 error ("%<goto%> in %<constexpr%> function");
12305 cp_function_chain->invalid_constexpr = true;
12308 /* Create the goto-statement. */
12309 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12311 /* Issue a warning about this use of a GNU extension. */
12312 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12313 /* Consume the '*' token. */
12314 cp_lexer_consume_token (parser->lexer);
12315 /* Parse the dependent expression. */
12316 finish_goto_stmt (cp_parser_expression (parser));
12318 else
12319 finish_goto_stmt (cp_parser_identifier (parser));
12320 /* Look for the final `;'. */
12321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12322 break;
12324 default:
12325 cp_parser_error (parser, "expected jump-statement");
12326 break;
12329 return statement;
12332 /* Parse a declaration-statement.
12334 declaration-statement:
12335 block-declaration */
12337 static void
12338 cp_parser_declaration_statement (cp_parser* parser)
12340 void *p;
12342 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12343 p = obstack_alloc (&declarator_obstack, 0);
12345 /* Parse the block-declaration. */
12346 cp_parser_block_declaration (parser, /*statement_p=*/true);
12348 /* Free any declarators allocated. */
12349 obstack_free (&declarator_obstack, p);
12352 /* Some dependent statements (like `if (cond) statement'), are
12353 implicitly in their own scope. In other words, if the statement is
12354 a single statement (as opposed to a compound-statement), it is
12355 none-the-less treated as if it were enclosed in braces. Any
12356 declarations appearing in the dependent statement are out of scope
12357 after control passes that point. This function parses a statement,
12358 but ensures that is in its own scope, even if it is not a
12359 compound-statement.
12361 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12362 is a (possibly labeled) if statement which is not enclosed in
12363 braces and has an else clause. This is used to implement
12364 -Wparentheses.
12366 CHAIN is a vector of if-else-if conditions. This is used to implement
12367 -Wduplicated-cond.
12369 Returns the new statement. */
12371 static tree
12372 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12373 const token_indent_info &guard_tinfo,
12374 vec<tree> *chain)
12376 tree statement;
12377 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12378 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12379 token_indent_info body_tinfo
12380 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12382 if (if_p != NULL)
12383 *if_p = false;
12385 /* Mark if () ; with a special NOP_EXPR. */
12386 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12388 cp_lexer_consume_token (parser->lexer);
12389 statement = add_stmt (build_empty_stmt (body_loc));
12391 if (guard_tinfo.keyword == RID_IF
12392 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12393 warning_at (body_loc, OPT_Wempty_body,
12394 "suggest braces around empty body in an %<if%> statement");
12395 else if (guard_tinfo.keyword == RID_ELSE)
12396 warning_at (body_loc, OPT_Wempty_body,
12397 "suggest braces around empty body in an %<else%> statement");
12399 /* if a compound is opened, we simply parse the statement directly. */
12400 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12401 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12402 /* If the token is not a `{', then we must take special action. */
12403 else
12405 /* Create a compound-statement. */
12406 statement = begin_compound_stmt (0);
12407 /* Parse the dependent-statement. */
12408 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12409 &body_loc_after_labels);
12410 /* Finish the dummy compound-statement. */
12411 finish_compound_stmt (statement);
12414 token_indent_info next_tinfo
12415 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12416 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12418 if (body_loc_after_labels != UNKNOWN_LOCATION
12419 && next_tinfo.type != CPP_SEMICOLON)
12420 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12421 guard_tinfo.location, guard_tinfo.keyword);
12423 /* Return the statement. */
12424 return statement;
12427 /* For some dependent statements (like `while (cond) statement'), we
12428 have already created a scope. Therefore, even if the dependent
12429 statement is a compound-statement, we do not want to create another
12430 scope. */
12432 static void
12433 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12434 const token_indent_info &guard_tinfo)
12436 /* If the token is a `{', then we must take special action. */
12437 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12439 token_indent_info body_tinfo
12440 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12441 location_t loc_after_labels = UNKNOWN_LOCATION;
12443 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12444 &loc_after_labels);
12445 token_indent_info next_tinfo
12446 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12447 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12449 if (loc_after_labels != UNKNOWN_LOCATION
12450 && next_tinfo.type != CPP_SEMICOLON)
12451 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12452 guard_tinfo.location,
12453 guard_tinfo.keyword);
12455 else
12457 /* Avoid calling cp_parser_compound_statement, so that we
12458 don't create a new scope. Do everything else by hand. */
12459 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12460 /* If the next keyword is `__label__' we have a label declaration. */
12461 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12462 cp_parser_label_declaration (parser);
12463 /* Parse an (optional) statement-seq. */
12464 cp_parser_statement_seq_opt (parser, NULL_TREE);
12465 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12469 /* Declarations [gram.dcl.dcl] */
12471 /* Parse an optional declaration-sequence.
12473 declaration-seq:
12474 declaration
12475 declaration-seq declaration */
12477 static void
12478 cp_parser_declaration_seq_opt (cp_parser* parser)
12480 while (true)
12482 cp_token *token;
12484 token = cp_lexer_peek_token (parser->lexer);
12486 if (token->type == CPP_CLOSE_BRACE
12487 || token->type == CPP_EOF
12488 || token->type == CPP_PRAGMA_EOL)
12489 break;
12491 if (token->type == CPP_SEMICOLON)
12493 /* A declaration consisting of a single semicolon is
12494 invalid. Allow it unless we're being pedantic. */
12495 cp_lexer_consume_token (parser->lexer);
12496 if (!in_system_header_at (input_location))
12497 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12498 continue;
12501 /* If we're entering or exiting a region that's implicitly
12502 extern "C", modify the lang context appropriately. */
12503 if (!parser->implicit_extern_c && token->implicit_extern_c)
12505 push_lang_context (lang_name_c);
12506 parser->implicit_extern_c = true;
12508 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12510 pop_lang_context ();
12511 parser->implicit_extern_c = false;
12514 if (token->type == CPP_PRAGMA)
12516 /* A top-level declaration can consist solely of a #pragma.
12517 A nested declaration cannot, so this is done here and not
12518 in cp_parser_declaration. (A #pragma at block scope is
12519 handled in cp_parser_statement.) */
12520 cp_parser_pragma (parser, pragma_external, NULL);
12521 continue;
12524 /* Parse the declaration itself. */
12525 cp_parser_declaration (parser);
12529 /* Parse a declaration.
12531 declaration:
12532 block-declaration
12533 function-definition
12534 template-declaration
12535 explicit-instantiation
12536 explicit-specialization
12537 linkage-specification
12538 namespace-definition
12540 C++17:
12541 deduction-guide
12543 GNU extension:
12545 declaration:
12546 __extension__ declaration */
12548 static void
12549 cp_parser_declaration (cp_parser* parser)
12551 cp_token token1;
12552 cp_token token2;
12553 int saved_pedantic;
12554 void *p;
12555 tree attributes = NULL_TREE;
12557 /* Check for the `__extension__' keyword. */
12558 if (cp_parser_extension_opt (parser, &saved_pedantic))
12560 /* Parse the qualified declaration. */
12561 cp_parser_declaration (parser);
12562 /* Restore the PEDANTIC flag. */
12563 pedantic = saved_pedantic;
12565 return;
12568 /* Try to figure out what kind of declaration is present. */
12569 token1 = *cp_lexer_peek_token (parser->lexer);
12571 if (token1.type != CPP_EOF)
12572 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12573 else
12575 token2.type = CPP_EOF;
12576 token2.keyword = RID_MAX;
12579 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12580 p = obstack_alloc (&declarator_obstack, 0);
12582 /* If the next token is `extern' and the following token is a string
12583 literal, then we have a linkage specification. */
12584 if (token1.keyword == RID_EXTERN
12585 && cp_parser_is_pure_string_literal (&token2))
12586 cp_parser_linkage_specification (parser);
12587 /* If the next token is `template', then we have either a template
12588 declaration, an explicit instantiation, or an explicit
12589 specialization. */
12590 else if (token1.keyword == RID_TEMPLATE)
12592 /* `template <>' indicates a template specialization. */
12593 if (token2.type == CPP_LESS
12594 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12595 cp_parser_explicit_specialization (parser);
12596 /* `template <' indicates a template declaration. */
12597 else if (token2.type == CPP_LESS)
12598 cp_parser_template_declaration (parser, /*member_p=*/false);
12599 /* Anything else must be an explicit instantiation. */
12600 else
12601 cp_parser_explicit_instantiation (parser);
12603 /* If the next token is `export', then we have a template
12604 declaration. */
12605 else if (token1.keyword == RID_EXPORT)
12606 cp_parser_template_declaration (parser, /*member_p=*/false);
12607 /* If the next token is `extern', 'static' or 'inline' and the one
12608 after that is `template', we have a GNU extended explicit
12609 instantiation directive. */
12610 else if (cp_parser_allow_gnu_extensions_p (parser)
12611 && (token1.keyword == RID_EXTERN
12612 || token1.keyword == RID_STATIC
12613 || token1.keyword == RID_INLINE)
12614 && token2.keyword == RID_TEMPLATE)
12615 cp_parser_explicit_instantiation (parser);
12616 /* If the next token is `namespace', check for a named or unnamed
12617 namespace definition. */
12618 else if (token1.keyword == RID_NAMESPACE
12619 && (/* A named namespace definition. */
12620 (token2.type == CPP_NAME
12621 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12622 != CPP_EQ))
12623 || (token2.type == CPP_OPEN_SQUARE
12624 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12625 == CPP_OPEN_SQUARE)
12626 /* An unnamed namespace definition. */
12627 || token2.type == CPP_OPEN_BRACE
12628 || token2.keyword == RID_ATTRIBUTE))
12629 cp_parser_namespace_definition (parser);
12630 /* An inline (associated) namespace definition. */
12631 else if (token1.keyword == RID_INLINE
12632 && token2.keyword == RID_NAMESPACE)
12633 cp_parser_namespace_definition (parser);
12634 /* Objective-C++ declaration/definition. */
12635 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12636 cp_parser_objc_declaration (parser, NULL_TREE);
12637 else if (c_dialect_objc ()
12638 && token1.keyword == RID_ATTRIBUTE
12639 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12640 cp_parser_objc_declaration (parser, attributes);
12641 /* At this point we may have a template declared by a concept
12642 introduction. */
12643 else if (flag_concepts
12644 && cp_parser_template_declaration_after_export (parser,
12645 /*member_p=*/false))
12646 /* We did. */;
12647 else
12648 /* Try to parse a block-declaration, or a function-definition. */
12649 cp_parser_block_declaration (parser, /*statement_p=*/false);
12651 /* Free any declarators allocated. */
12652 obstack_free (&declarator_obstack, p);
12655 /* Parse a block-declaration.
12657 block-declaration:
12658 simple-declaration
12659 asm-definition
12660 namespace-alias-definition
12661 using-declaration
12662 using-directive
12664 GNU Extension:
12666 block-declaration:
12667 __extension__ block-declaration
12669 C++0x Extension:
12671 block-declaration:
12672 static_assert-declaration
12674 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12675 part of a declaration-statement. */
12677 static void
12678 cp_parser_block_declaration (cp_parser *parser,
12679 bool statement_p)
12681 cp_token *token1;
12682 int saved_pedantic;
12684 /* Check for the `__extension__' keyword. */
12685 if (cp_parser_extension_opt (parser, &saved_pedantic))
12687 /* Parse the qualified declaration. */
12688 cp_parser_block_declaration (parser, statement_p);
12689 /* Restore the PEDANTIC flag. */
12690 pedantic = saved_pedantic;
12692 return;
12695 /* Peek at the next token to figure out which kind of declaration is
12696 present. */
12697 token1 = cp_lexer_peek_token (parser->lexer);
12699 /* If the next keyword is `asm', we have an asm-definition. */
12700 if (token1->keyword == RID_ASM)
12702 if (statement_p)
12703 cp_parser_commit_to_tentative_parse (parser);
12704 cp_parser_asm_definition (parser);
12706 /* If the next keyword is `namespace', we have a
12707 namespace-alias-definition. */
12708 else if (token1->keyword == RID_NAMESPACE)
12709 cp_parser_namespace_alias_definition (parser);
12710 /* If the next keyword is `using', we have a
12711 using-declaration, a using-directive, or an alias-declaration. */
12712 else if (token1->keyword == RID_USING)
12714 cp_token *token2;
12716 if (statement_p)
12717 cp_parser_commit_to_tentative_parse (parser);
12718 /* If the token after `using' is `namespace', then we have a
12719 using-directive. */
12720 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12721 if (token2->keyword == RID_NAMESPACE)
12722 cp_parser_using_directive (parser);
12723 /* If the second token after 'using' is '=', then we have an
12724 alias-declaration. */
12725 else if (cxx_dialect >= cxx11
12726 && token2->type == CPP_NAME
12727 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12728 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12729 cp_parser_alias_declaration (parser);
12730 /* Otherwise, it's a using-declaration. */
12731 else
12732 cp_parser_using_declaration (parser,
12733 /*access_declaration_p=*/false);
12735 /* If the next keyword is `__label__' we have a misplaced label
12736 declaration. */
12737 else if (token1->keyword == RID_LABEL)
12739 cp_lexer_consume_token (parser->lexer);
12740 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12741 cp_parser_skip_to_end_of_statement (parser);
12742 /* If the next token is now a `;', consume it. */
12743 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12744 cp_lexer_consume_token (parser->lexer);
12746 /* If the next token is `static_assert' we have a static assertion. */
12747 else if (token1->keyword == RID_STATIC_ASSERT)
12748 cp_parser_static_assert (parser, /*member_p=*/false);
12749 /* Anything else must be a simple-declaration. */
12750 else
12751 cp_parser_simple_declaration (parser, !statement_p,
12752 /*maybe_range_for_decl*/NULL);
12755 /* Parse a simple-declaration.
12757 simple-declaration:
12758 decl-specifier-seq [opt] init-declarator-list [opt] ;
12759 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12760 brace-or-equal-initializer ;
12762 init-declarator-list:
12763 init-declarator
12764 init-declarator-list , init-declarator
12766 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12767 function-definition as a simple-declaration.
12769 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12770 parsed declaration if it is an uninitialized single declarator not followed
12771 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12772 if present, will not be consumed. */
12774 static void
12775 cp_parser_simple_declaration (cp_parser* parser,
12776 bool function_definition_allowed_p,
12777 tree *maybe_range_for_decl)
12779 cp_decl_specifier_seq decl_specifiers;
12780 int declares_class_or_enum;
12781 bool saw_declarator;
12782 location_t comma_loc = UNKNOWN_LOCATION;
12783 location_t init_loc = UNKNOWN_LOCATION;
12785 if (maybe_range_for_decl)
12786 *maybe_range_for_decl = NULL_TREE;
12788 /* Defer access checks until we know what is being declared; the
12789 checks for names appearing in the decl-specifier-seq should be
12790 done as if we were in the scope of the thing being declared. */
12791 push_deferring_access_checks (dk_deferred);
12793 /* Parse the decl-specifier-seq. We have to keep track of whether
12794 or not the decl-specifier-seq declares a named class or
12795 enumeration type, since that is the only case in which the
12796 init-declarator-list is allowed to be empty.
12798 [dcl.dcl]
12800 In a simple-declaration, the optional init-declarator-list can be
12801 omitted only when declaring a class or enumeration, that is when
12802 the decl-specifier-seq contains either a class-specifier, an
12803 elaborated-type-specifier, or an enum-specifier. */
12804 cp_parser_decl_specifier_seq (parser,
12805 CP_PARSER_FLAGS_OPTIONAL,
12806 &decl_specifiers,
12807 &declares_class_or_enum);
12808 /* We no longer need to defer access checks. */
12809 stop_deferring_access_checks ();
12811 /* In a block scope, a valid declaration must always have a
12812 decl-specifier-seq. By not trying to parse declarators, we can
12813 resolve the declaration/expression ambiguity more quickly. */
12814 if (!function_definition_allowed_p
12815 && !decl_specifiers.any_specifiers_p)
12817 cp_parser_error (parser, "expected declaration");
12818 goto done;
12821 /* If the next two tokens are both identifiers, the code is
12822 erroneous. The usual cause of this situation is code like:
12824 T t;
12826 where "T" should name a type -- but does not. */
12827 if (!decl_specifiers.any_type_specifiers_p
12828 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12830 /* If parsing tentatively, we should commit; we really are
12831 looking at a declaration. */
12832 cp_parser_commit_to_tentative_parse (parser);
12833 /* Give up. */
12834 goto done;
12837 /* If we have seen at least one decl-specifier, and the next token
12838 is not a parenthesis, then we must be looking at a declaration.
12839 (After "int (" we might be looking at a functional cast.) */
12840 if (decl_specifiers.any_specifiers_p
12841 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12842 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12843 && !cp_parser_error_occurred (parser))
12844 cp_parser_commit_to_tentative_parse (parser);
12846 /* Look for C++17 decomposition declaration. */
12847 for (size_t n = 1; ; n++)
12848 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12849 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12850 continue;
12851 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12852 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12853 && decl_specifiers.any_specifiers_p)
12855 tree decl
12856 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12857 maybe_range_for_decl,
12858 &init_loc);
12860 /* The next token should be either a `,' or a `;'. */
12861 cp_token *token = cp_lexer_peek_token (parser->lexer);
12862 /* If it's a `;', we are done. */
12863 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12864 goto finish;
12865 /* Anything else is an error. */
12866 else
12868 /* If we have already issued an error message we don't need
12869 to issue another one. */
12870 if ((decl != error_mark_node
12871 && DECL_INITIAL (decl) != error_mark_node)
12872 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12873 cp_parser_error (parser, "expected %<,%> or %<;%>");
12874 /* Skip tokens until we reach the end of the statement. */
12875 cp_parser_skip_to_end_of_statement (parser);
12876 /* If the next token is now a `;', consume it. */
12877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12878 cp_lexer_consume_token (parser->lexer);
12879 goto done;
12882 else
12883 break;
12885 tree last_type;
12886 bool auto_specifier_p;
12887 /* NULL_TREE if both variable and function declaration are allowed,
12888 error_mark_node if function declaration are not allowed and
12889 a FUNCTION_DECL that should be diagnosed if it is followed by
12890 variable declarations. */
12891 tree auto_function_declaration;
12893 last_type = NULL_TREE;
12894 auto_specifier_p
12895 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12896 auto_function_declaration = NULL_TREE;
12898 /* Keep going until we hit the `;' at the end of the simple
12899 declaration. */
12900 saw_declarator = false;
12901 while (cp_lexer_next_token_is_not (parser->lexer,
12902 CPP_SEMICOLON))
12904 cp_token *token;
12905 bool function_definition_p;
12906 tree decl;
12907 tree auto_result = NULL_TREE;
12909 if (saw_declarator)
12911 /* If we are processing next declarator, comma is expected */
12912 token = cp_lexer_peek_token (parser->lexer);
12913 gcc_assert (token->type == CPP_COMMA);
12914 cp_lexer_consume_token (parser->lexer);
12915 if (maybe_range_for_decl)
12917 *maybe_range_for_decl = error_mark_node;
12918 if (comma_loc == UNKNOWN_LOCATION)
12919 comma_loc = token->location;
12922 else
12923 saw_declarator = true;
12925 /* Parse the init-declarator. */
12926 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12927 /*checks=*/NULL,
12928 function_definition_allowed_p,
12929 /*member_p=*/false,
12930 declares_class_or_enum,
12931 &function_definition_p,
12932 maybe_range_for_decl,
12933 &init_loc,
12934 &auto_result);
12935 /* If an error occurred while parsing tentatively, exit quickly.
12936 (That usually happens when in the body of a function; each
12937 statement is treated as a declaration-statement until proven
12938 otherwise.) */
12939 if (cp_parser_error_occurred (parser))
12940 goto done;
12942 if (auto_specifier_p && cxx_dialect >= cxx14)
12944 /* If the init-declarator-list contains more than one
12945 init-declarator, they shall all form declarations of
12946 variables. */
12947 if (auto_function_declaration == NULL_TREE)
12948 auto_function_declaration
12949 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12950 else if (TREE_CODE (decl) == FUNCTION_DECL
12951 || auto_function_declaration != error_mark_node)
12953 error_at (decl_specifiers.locations[ds_type_spec],
12954 "non-variable %qD in declaration with more than one "
12955 "declarator with placeholder type",
12956 TREE_CODE (decl) == FUNCTION_DECL
12957 ? decl : auto_function_declaration);
12958 auto_function_declaration = error_mark_node;
12962 if (auto_result
12963 && (!processing_template_decl || !type_uses_auto (auto_result)))
12965 if (last_type
12966 && last_type != error_mark_node
12967 && !same_type_p (auto_result, last_type))
12969 /* If the list of declarators contains more than one declarator,
12970 the type of each declared variable is determined as described
12971 above. If the type deduced for the template parameter U is not
12972 the same in each deduction, the program is ill-formed. */
12973 error_at (decl_specifiers.locations[ds_type_spec],
12974 "inconsistent deduction for %qT: %qT and then %qT",
12975 decl_specifiers.type, last_type, auto_result);
12976 last_type = error_mark_node;
12978 else
12979 last_type = auto_result;
12982 /* Handle function definitions specially. */
12983 if (function_definition_p)
12985 /* If the next token is a `,', then we are probably
12986 processing something like:
12988 void f() {}, *p;
12990 which is erroneous. */
12991 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12993 cp_token *token = cp_lexer_peek_token (parser->lexer);
12994 error_at (token->location,
12995 "mixing"
12996 " declarations and function-definitions is forbidden");
12998 /* Otherwise, we're done with the list of declarators. */
12999 else
13001 pop_deferring_access_checks ();
13002 return;
13005 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13006 *maybe_range_for_decl = decl;
13007 /* The next token should be either a `,' or a `;'. */
13008 token = cp_lexer_peek_token (parser->lexer);
13009 /* If it's a `,', there are more declarators to come. */
13010 if (token->type == CPP_COMMA)
13011 /* will be consumed next time around */;
13012 /* If it's a `;', we are done. */
13013 else if (token->type == CPP_SEMICOLON)
13014 break;
13015 else if (maybe_range_for_decl)
13017 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13018 permerror (decl_specifiers.locations[ds_type_spec],
13019 "types may not be defined in a for-range-declaration");
13020 break;
13022 /* Anything else is an error. */
13023 else
13025 /* If we have already issued an error message we don't need
13026 to issue another one. */
13027 if ((decl != error_mark_node
13028 && DECL_INITIAL (decl) != error_mark_node)
13029 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13030 cp_parser_error (parser, "expected %<,%> or %<;%>");
13031 /* Skip tokens until we reach the end of the statement. */
13032 cp_parser_skip_to_end_of_statement (parser);
13033 /* If the next token is now a `;', consume it. */
13034 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13035 cp_lexer_consume_token (parser->lexer);
13036 goto done;
13038 /* After the first time around, a function-definition is not
13039 allowed -- even if it was OK at first. For example:
13041 int i, f() {}
13043 is not valid. */
13044 function_definition_allowed_p = false;
13047 /* Issue an error message if no declarators are present, and the
13048 decl-specifier-seq does not itself declare a class or
13049 enumeration: [dcl.dcl]/3. */
13050 if (!saw_declarator)
13052 if (cp_parser_declares_only_class_p (parser))
13054 if (!declares_class_or_enum
13055 && decl_specifiers.type
13056 && OVERLOAD_TYPE_P (decl_specifiers.type))
13057 /* Ensure an error is issued anyway when finish_decltype_type,
13058 called via cp_parser_decl_specifier_seq, returns a class or
13059 an enumeration (c++/51786). */
13060 decl_specifiers.type = NULL_TREE;
13061 shadow_tag (&decl_specifiers);
13063 /* Perform any deferred access checks. */
13064 perform_deferred_access_checks (tf_warning_or_error);
13067 /* Consume the `;'. */
13068 finish:
13069 if (!maybe_range_for_decl)
13070 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13071 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13073 if (init_loc != UNKNOWN_LOCATION)
13074 error_at (init_loc, "initializer in range-based %<for%> loop");
13075 if (comma_loc != UNKNOWN_LOCATION)
13076 error_at (comma_loc,
13077 "multiple declarations in range-based %<for%> loop");
13080 done:
13081 pop_deferring_access_checks ();
13084 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13085 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13086 initializer ; */
13088 static tree
13089 cp_parser_decomposition_declaration (cp_parser *parser,
13090 cp_decl_specifier_seq *decl_specifiers,
13091 tree *maybe_range_for_decl,
13092 location_t *init_loc)
13094 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13095 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13096 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13098 /* Parse the identifier-list. */
13099 auto_vec<cp_expr, 10> v;
13100 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13101 while (true)
13103 cp_expr e = cp_parser_identifier (parser);
13104 if (e.get_value () == error_mark_node)
13105 break;
13106 v.safe_push (e);
13107 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13108 break;
13109 cp_lexer_consume_token (parser->lexer);
13112 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13113 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13115 end_loc = UNKNOWN_LOCATION;
13116 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13117 false);
13118 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13119 cp_lexer_consume_token (parser->lexer);
13120 else
13122 cp_parser_skip_to_end_of_statement (parser);
13123 return error_mark_node;
13127 if (cxx_dialect < cxx1z)
13128 pedwarn (loc, 0, "decomposition declaration only available with "
13129 "-std=c++1z or -std=gnu++1z");
13131 tree pushed_scope;
13132 cp_declarator *declarator = make_declarator (cdk_decomp);
13133 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13134 declarator->id_loc = loc;
13135 if (ref_qual != REF_QUAL_NONE)
13136 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13137 ref_qual == REF_QUAL_RVALUE,
13138 NULL_TREE);
13139 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13140 NULL_TREE, decl_specifiers->attributes,
13141 &pushed_scope);
13142 tree orig_decl = decl;
13144 unsigned int i;
13145 cp_expr e;
13146 cp_decl_specifier_seq decl_specs;
13147 clear_decl_specs (&decl_specs);
13148 decl_specs.type = make_auto ();
13149 tree prev = decl;
13150 FOR_EACH_VEC_ELT (v, i, e)
13152 if (i == 0)
13153 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13154 else
13155 declarator->u.id.unqualified_name = e.get_value ();
13156 declarator->id_loc = e.get_location ();
13157 tree elt_pushed_scope;
13158 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13159 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13160 if (decl2 == error_mark_node)
13161 decl = error_mark_node;
13162 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13164 /* Ensure we've diagnosed redeclaration if we aren't creating
13165 a new VAR_DECL. */
13166 gcc_assert (errorcount);
13167 decl = error_mark_node;
13169 else
13170 prev = decl2;
13171 if (elt_pushed_scope)
13172 pop_scope (elt_pushed_scope);
13175 if (v.is_empty ())
13177 error_at (loc, "empty decomposition declaration");
13178 decl = error_mark_node;
13181 if (maybe_range_for_decl == NULL
13182 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13184 bool non_constant_p = false, is_direct_init = false;
13185 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13186 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13187 &non_constant_p);
13189 if (decl != error_mark_node)
13191 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13192 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13193 cp_finish_decomp (decl, prev, v.length ());
13196 else if (decl != error_mark_node)
13198 *maybe_range_for_decl = prev;
13199 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13200 the underlying DECL. */
13201 cp_finish_decomp (decl, prev, v.length ());
13204 if (pushed_scope)
13205 pop_scope (pushed_scope);
13207 if (decl == error_mark_node && DECL_P (orig_decl))
13209 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13210 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13213 return decl;
13216 /* Parse a decl-specifier-seq.
13218 decl-specifier-seq:
13219 decl-specifier-seq [opt] decl-specifier
13220 decl-specifier attribute-specifier-seq [opt] (C++11)
13222 decl-specifier:
13223 storage-class-specifier
13224 type-specifier
13225 function-specifier
13226 friend
13227 typedef
13229 GNU Extension:
13231 decl-specifier:
13232 attributes
13234 Concepts Extension:
13236 decl-specifier:
13237 concept
13239 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13241 The parser flags FLAGS is used to control type-specifier parsing.
13243 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13244 flags:
13246 1: one of the decl-specifiers is an elaborated-type-specifier
13247 (i.e., a type declaration)
13248 2: one of the decl-specifiers is an enum-specifier or a
13249 class-specifier (i.e., a type definition)
13253 static void
13254 cp_parser_decl_specifier_seq (cp_parser* parser,
13255 cp_parser_flags flags,
13256 cp_decl_specifier_seq *decl_specs,
13257 int* declares_class_or_enum)
13259 bool constructor_possible_p = !parser->in_declarator_p;
13260 bool found_decl_spec = false;
13261 cp_token *start_token = NULL;
13262 cp_decl_spec ds;
13264 /* Clear DECL_SPECS. */
13265 clear_decl_specs (decl_specs);
13267 /* Assume no class or enumeration type is declared. */
13268 *declares_class_or_enum = 0;
13270 /* Keep reading specifiers until there are no more to read. */
13271 while (true)
13273 bool constructor_p;
13274 cp_token *token;
13275 ds = ds_last;
13277 /* Peek at the next token. */
13278 token = cp_lexer_peek_token (parser->lexer);
13280 /* Save the first token of the decl spec list for error
13281 reporting. */
13282 if (!start_token)
13283 start_token = token;
13284 /* Handle attributes. */
13285 if (cp_next_tokens_can_be_attribute_p (parser))
13287 /* Parse the attributes. */
13288 tree attrs = cp_parser_attributes_opt (parser);
13290 /* In a sequence of declaration specifiers, c++11 attributes
13291 appertain to the type that precede them. In that case
13292 [dcl.spec]/1 says:
13294 The attribute-specifier-seq affects the type only for
13295 the declaration it appears in, not other declarations
13296 involving the same type.
13298 But for now let's force the user to position the
13299 attribute either at the beginning of the declaration or
13300 after the declarator-id, which would clearly mean that it
13301 applies to the declarator. */
13302 if (cxx11_attribute_p (attrs))
13304 if (!found_decl_spec)
13305 /* The c++11 attribute is at the beginning of the
13306 declaration. It appertains to the entity being
13307 declared. */;
13308 else
13310 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13312 /* This is an attribute following a
13313 class-specifier. */
13314 if (decl_specs->type_definition_p)
13315 warn_misplaced_attr_for_class_type (token->location,
13316 decl_specs->type);
13317 attrs = NULL_TREE;
13319 else
13321 decl_specs->std_attributes
13322 = chainon (decl_specs->std_attributes,
13323 attrs);
13324 if (decl_specs->locations[ds_std_attribute] == 0)
13325 decl_specs->locations[ds_std_attribute] = token->location;
13327 continue;
13331 decl_specs->attributes
13332 = chainon (decl_specs->attributes,
13333 attrs);
13334 if (decl_specs->locations[ds_attribute] == 0)
13335 decl_specs->locations[ds_attribute] = token->location;
13336 continue;
13338 /* Assume we will find a decl-specifier keyword. */
13339 found_decl_spec = true;
13340 /* If the next token is an appropriate keyword, we can simply
13341 add it to the list. */
13342 switch (token->keyword)
13344 /* decl-specifier:
13345 friend
13346 constexpr */
13347 case RID_FRIEND:
13348 if (!at_class_scope_p ())
13350 error_at (token->location, "%<friend%> used outside of class");
13351 cp_lexer_purge_token (parser->lexer);
13353 else
13355 ds = ds_friend;
13356 /* Consume the token. */
13357 cp_lexer_consume_token (parser->lexer);
13359 break;
13361 case RID_CONSTEXPR:
13362 ds = ds_constexpr;
13363 cp_lexer_consume_token (parser->lexer);
13364 break;
13366 case RID_CONCEPT:
13367 ds = ds_concept;
13368 cp_lexer_consume_token (parser->lexer);
13369 break;
13371 /* function-specifier:
13372 inline
13373 virtual
13374 explicit */
13375 case RID_INLINE:
13376 case RID_VIRTUAL:
13377 case RID_EXPLICIT:
13378 cp_parser_function_specifier_opt (parser, decl_specs);
13379 break;
13381 /* decl-specifier:
13382 typedef */
13383 case RID_TYPEDEF:
13384 ds = ds_typedef;
13385 /* Consume the token. */
13386 cp_lexer_consume_token (parser->lexer);
13387 /* A constructor declarator cannot appear in a typedef. */
13388 constructor_possible_p = false;
13389 /* The "typedef" keyword can only occur in a declaration; we
13390 may as well commit at this point. */
13391 cp_parser_commit_to_tentative_parse (parser);
13393 if (decl_specs->storage_class != sc_none)
13394 decl_specs->conflicting_specifiers_p = true;
13395 break;
13397 /* storage-class-specifier:
13398 auto
13399 register
13400 static
13401 extern
13402 mutable
13404 GNU Extension:
13405 thread */
13406 case RID_AUTO:
13407 if (cxx_dialect == cxx98)
13409 /* Consume the token. */
13410 cp_lexer_consume_token (parser->lexer);
13412 /* Complain about `auto' as a storage specifier, if
13413 we're complaining about C++0x compatibility. */
13414 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13415 " changes meaning in C++11; please remove it");
13417 /* Set the storage class anyway. */
13418 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13419 token);
13421 else
13422 /* C++0x auto type-specifier. */
13423 found_decl_spec = false;
13424 break;
13426 case RID_REGISTER:
13427 case RID_STATIC:
13428 case RID_EXTERN:
13429 case RID_MUTABLE:
13430 /* Consume the token. */
13431 cp_lexer_consume_token (parser->lexer);
13432 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13433 token);
13434 break;
13435 case RID_THREAD:
13436 /* Consume the token. */
13437 ds = ds_thread;
13438 cp_lexer_consume_token (parser->lexer);
13439 break;
13441 default:
13442 /* We did not yet find a decl-specifier yet. */
13443 found_decl_spec = false;
13444 break;
13447 if (found_decl_spec
13448 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13449 && token->keyword != RID_CONSTEXPR)
13450 error ("decl-specifier invalid in condition");
13452 if (found_decl_spec
13453 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13454 && token->keyword != RID_MUTABLE
13455 && token->keyword != RID_CONSTEXPR)
13456 error_at (token->location, "%qD invalid in lambda",
13457 ridpointers[token->keyword]);
13459 if (ds != ds_last)
13460 set_and_check_decl_spec_loc (decl_specs, ds, token);
13462 /* Constructors are a special case. The `S' in `S()' is not a
13463 decl-specifier; it is the beginning of the declarator. */
13464 constructor_p
13465 = (!found_decl_spec
13466 && constructor_possible_p
13467 && (cp_parser_constructor_declarator_p
13468 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13470 /* If we don't have a DECL_SPEC yet, then we must be looking at
13471 a type-specifier. */
13472 if (!found_decl_spec && !constructor_p)
13474 int decl_spec_declares_class_or_enum;
13475 bool is_cv_qualifier;
13476 tree type_spec;
13478 type_spec
13479 = cp_parser_type_specifier (parser, flags,
13480 decl_specs,
13481 /*is_declaration=*/true,
13482 &decl_spec_declares_class_or_enum,
13483 &is_cv_qualifier);
13484 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13486 /* If this type-specifier referenced a user-defined type
13487 (a typedef, class-name, etc.), then we can't allow any
13488 more such type-specifiers henceforth.
13490 [dcl.spec]
13492 The longest sequence of decl-specifiers that could
13493 possibly be a type name is taken as the
13494 decl-specifier-seq of a declaration. The sequence shall
13495 be self-consistent as described below.
13497 [dcl.type]
13499 As a general rule, at most one type-specifier is allowed
13500 in the complete decl-specifier-seq of a declaration. The
13501 only exceptions are the following:
13503 -- const or volatile can be combined with any other
13504 type-specifier.
13506 -- signed or unsigned can be combined with char, long,
13507 short, or int.
13509 -- ..
13511 Example:
13513 typedef char* Pc;
13514 void g (const int Pc);
13516 Here, Pc is *not* part of the decl-specifier seq; it's
13517 the declarator. Therefore, once we see a type-specifier
13518 (other than a cv-qualifier), we forbid any additional
13519 user-defined types. We *do* still allow things like `int
13520 int' to be considered a decl-specifier-seq, and issue the
13521 error message later. */
13522 if (type_spec && !is_cv_qualifier)
13523 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13524 /* A constructor declarator cannot follow a type-specifier. */
13525 if (type_spec)
13527 constructor_possible_p = false;
13528 found_decl_spec = true;
13529 if (!is_cv_qualifier)
13530 decl_specs->any_type_specifiers_p = true;
13534 /* If we still do not have a DECL_SPEC, then there are no more
13535 decl-specifiers. */
13536 if (!found_decl_spec)
13537 break;
13539 decl_specs->any_specifiers_p = true;
13540 /* After we see one decl-specifier, further decl-specifiers are
13541 always optional. */
13542 flags |= CP_PARSER_FLAGS_OPTIONAL;
13545 /* Don't allow a friend specifier with a class definition. */
13546 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13547 && (*declares_class_or_enum & 2))
13548 error_at (decl_specs->locations[ds_friend],
13549 "class definition may not be declared a friend");
13552 /* Parse an (optional) storage-class-specifier.
13554 storage-class-specifier:
13555 auto
13556 register
13557 static
13558 extern
13559 mutable
13561 GNU Extension:
13563 storage-class-specifier:
13564 thread
13566 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13568 static tree
13569 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13571 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13573 case RID_AUTO:
13574 if (cxx_dialect != cxx98)
13575 return NULL_TREE;
13576 /* Fall through for C++98. */
13577 gcc_fallthrough ();
13579 case RID_REGISTER:
13580 case RID_STATIC:
13581 case RID_EXTERN:
13582 case RID_MUTABLE:
13583 case RID_THREAD:
13584 /* Consume the token. */
13585 return cp_lexer_consume_token (parser->lexer)->u.value;
13587 default:
13588 return NULL_TREE;
13592 /* Parse an (optional) function-specifier.
13594 function-specifier:
13595 inline
13596 virtual
13597 explicit
13599 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13600 Updates DECL_SPECS, if it is non-NULL. */
13602 static tree
13603 cp_parser_function_specifier_opt (cp_parser* parser,
13604 cp_decl_specifier_seq *decl_specs)
13606 cp_token *token = cp_lexer_peek_token (parser->lexer);
13607 switch (token->keyword)
13609 case RID_INLINE:
13610 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13611 break;
13613 case RID_VIRTUAL:
13614 /* 14.5.2.3 [temp.mem]
13616 A member function template shall not be virtual. */
13617 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13618 && current_class_type)
13619 error_at (token->location, "templates may not be %<virtual%>");
13620 else
13621 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13622 break;
13624 case RID_EXPLICIT:
13625 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13626 break;
13628 default:
13629 return NULL_TREE;
13632 /* Consume the token. */
13633 return cp_lexer_consume_token (parser->lexer)->u.value;
13636 /* Parse a linkage-specification.
13638 linkage-specification:
13639 extern string-literal { declaration-seq [opt] }
13640 extern string-literal declaration */
13642 static void
13643 cp_parser_linkage_specification (cp_parser* parser)
13645 tree linkage;
13647 /* Look for the `extern' keyword. */
13648 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13650 /* Look for the string-literal. */
13651 linkage = cp_parser_string_literal (parser, false, false);
13653 /* Transform the literal into an identifier. If the literal is a
13654 wide-character string, or contains embedded NULs, then we can't
13655 handle it as the user wants. */
13656 if (strlen (TREE_STRING_POINTER (linkage))
13657 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13659 cp_parser_error (parser, "invalid linkage-specification");
13660 /* Assume C++ linkage. */
13661 linkage = lang_name_cplusplus;
13663 else
13664 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13666 /* We're now using the new linkage. */
13667 push_lang_context (linkage);
13669 /* If the next token is a `{', then we're using the first
13670 production. */
13671 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13673 cp_ensure_no_omp_declare_simd (parser);
13674 cp_ensure_no_oacc_routine (parser);
13676 /* Consume the `{' token. */
13677 cp_lexer_consume_token (parser->lexer);
13678 /* Parse the declarations. */
13679 cp_parser_declaration_seq_opt (parser);
13680 /* Look for the closing `}'. */
13681 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13683 /* Otherwise, there's just one declaration. */
13684 else
13686 bool saved_in_unbraced_linkage_specification_p;
13688 saved_in_unbraced_linkage_specification_p
13689 = parser->in_unbraced_linkage_specification_p;
13690 parser->in_unbraced_linkage_specification_p = true;
13691 cp_parser_declaration (parser);
13692 parser->in_unbraced_linkage_specification_p
13693 = saved_in_unbraced_linkage_specification_p;
13696 /* We're done with the linkage-specification. */
13697 pop_lang_context ();
13700 /* Parse a static_assert-declaration.
13702 static_assert-declaration:
13703 static_assert ( constant-expression , string-literal ) ;
13704 static_assert ( constant-expression ) ; (C++1Z)
13706 If MEMBER_P, this static_assert is a class member. */
13708 static void
13709 cp_parser_static_assert(cp_parser *parser, bool member_p)
13711 tree condition;
13712 tree message;
13713 cp_token *token;
13714 location_t saved_loc;
13715 bool dummy;
13717 /* Peek at the `static_assert' token so we can keep track of exactly
13718 where the static assertion started. */
13719 token = cp_lexer_peek_token (parser->lexer);
13720 saved_loc = token->location;
13722 /* Look for the `static_assert' keyword. */
13723 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13724 RT_STATIC_ASSERT))
13725 return;
13727 /* We know we are in a static assertion; commit to any tentative
13728 parse. */
13729 if (cp_parser_parsing_tentatively (parser))
13730 cp_parser_commit_to_tentative_parse (parser);
13732 /* Parse the `(' starting the static assertion condition. */
13733 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13735 /* Parse the constant-expression. Allow a non-constant expression
13736 here in order to give better diagnostics in finish_static_assert. */
13737 condition =
13738 cp_parser_constant_expression (parser,
13739 /*allow_non_constant_p=*/true,
13740 /*non_constant_p=*/&dummy);
13742 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13744 if (cxx_dialect < cxx1z)
13745 pedwarn (input_location, OPT_Wpedantic,
13746 "static_assert without a message "
13747 "only available with -std=c++1z or -std=gnu++1z");
13748 /* Eat the ')' */
13749 cp_lexer_consume_token (parser->lexer);
13750 message = build_string (1, "");
13751 TREE_TYPE (message) = char_array_type_node;
13752 fix_string_type (message);
13754 else
13756 /* Parse the separating `,'. */
13757 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13759 /* Parse the string-literal message. */
13760 message = cp_parser_string_literal (parser,
13761 /*translate=*/false,
13762 /*wide_ok=*/true);
13764 /* A `)' completes the static assertion. */
13765 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13766 cp_parser_skip_to_closing_parenthesis (parser,
13767 /*recovering=*/true,
13768 /*or_comma=*/false,
13769 /*consume_paren=*/true);
13772 /* A semicolon terminates the declaration. */
13773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13775 /* Complete the static assertion, which may mean either processing
13776 the static assert now or saving it for template instantiation. */
13777 finish_static_assert (condition, message, saved_loc, member_p);
13780 /* Parse the expression in decltype ( expression ). */
13782 static tree
13783 cp_parser_decltype_expr (cp_parser *parser,
13784 bool &id_expression_or_member_access_p)
13786 cp_token *id_expr_start_token;
13787 tree expr;
13789 /* Since we're going to preserve any side-effects from this parse, set up a
13790 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13791 in the expression. */
13792 tentative_firewall firewall (parser);
13794 /* First, try parsing an id-expression. */
13795 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13796 cp_parser_parse_tentatively (parser);
13797 expr = cp_parser_id_expression (parser,
13798 /*template_keyword_p=*/false,
13799 /*check_dependency_p=*/true,
13800 /*template_p=*/NULL,
13801 /*declarator_p=*/false,
13802 /*optional_p=*/false);
13804 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13806 bool non_integral_constant_expression_p = false;
13807 tree id_expression = expr;
13808 cp_id_kind idk;
13809 const char *error_msg;
13811 if (identifier_p (expr))
13812 /* Lookup the name we got back from the id-expression. */
13813 expr = cp_parser_lookup_name_simple (parser, expr,
13814 id_expr_start_token->location);
13816 if (expr
13817 && expr != error_mark_node
13818 && TREE_CODE (expr) != TYPE_DECL
13819 && (TREE_CODE (expr) != BIT_NOT_EXPR
13820 || !TYPE_P (TREE_OPERAND (expr, 0)))
13821 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13823 /* Complete lookup of the id-expression. */
13824 expr = (finish_id_expression
13825 (id_expression, expr, parser->scope, &idk,
13826 /*integral_constant_expression_p=*/false,
13827 /*allow_non_integral_constant_expression_p=*/true,
13828 &non_integral_constant_expression_p,
13829 /*template_p=*/false,
13830 /*done=*/true,
13831 /*address_p=*/false,
13832 /*template_arg_p=*/false,
13833 &error_msg,
13834 id_expr_start_token->location));
13836 if (expr == error_mark_node)
13837 /* We found an id-expression, but it was something that we
13838 should not have found. This is an error, not something
13839 we can recover from, so note that we found an
13840 id-expression and we'll recover as gracefully as
13841 possible. */
13842 id_expression_or_member_access_p = true;
13845 if (expr
13846 && expr != error_mark_node
13847 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13848 /* We have an id-expression. */
13849 id_expression_or_member_access_p = true;
13852 if (!id_expression_or_member_access_p)
13854 /* Abort the id-expression parse. */
13855 cp_parser_abort_tentative_parse (parser);
13857 /* Parsing tentatively, again. */
13858 cp_parser_parse_tentatively (parser);
13860 /* Parse a class member access. */
13861 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13862 /*cast_p=*/false, /*decltype*/true,
13863 /*member_access_only_p=*/true, NULL);
13865 if (expr
13866 && expr != error_mark_node
13867 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13868 /* We have an id-expression. */
13869 id_expression_or_member_access_p = true;
13872 if (id_expression_or_member_access_p)
13873 /* We have parsed the complete id-expression or member access. */
13874 cp_parser_parse_definitely (parser);
13875 else
13877 /* Abort our attempt to parse an id-expression or member access
13878 expression. */
13879 cp_parser_abort_tentative_parse (parser);
13881 /* Parse a full expression. */
13882 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13883 /*decltype_p=*/true);
13886 return expr;
13889 /* Parse a `decltype' type. Returns the type.
13891 simple-type-specifier:
13892 decltype ( expression )
13893 C++14 proposal:
13894 decltype ( auto ) */
13896 static tree
13897 cp_parser_decltype (cp_parser *parser)
13899 tree expr;
13900 bool id_expression_or_member_access_p = false;
13901 const char *saved_message;
13902 bool saved_integral_constant_expression_p;
13903 bool saved_non_integral_constant_expression_p;
13904 bool saved_greater_than_is_operator_p;
13905 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13907 if (start_token->type == CPP_DECLTYPE)
13909 /* Already parsed. */
13910 cp_lexer_consume_token (parser->lexer);
13911 return saved_checks_value (start_token->u.tree_check_value);
13914 /* Look for the `decltype' token. */
13915 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13916 return error_mark_node;
13918 /* Parse the opening `('. */
13919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13920 return error_mark_node;
13922 /* decltype (auto) */
13923 if (cxx_dialect >= cxx14
13924 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13926 cp_lexer_consume_token (parser->lexer);
13927 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13928 return error_mark_node;
13929 expr = make_decltype_auto ();
13930 AUTO_IS_DECLTYPE (expr) = true;
13931 goto rewrite;
13934 /* Types cannot be defined in a `decltype' expression. Save away the
13935 old message. */
13936 saved_message = parser->type_definition_forbidden_message;
13938 /* And create the new one. */
13939 parser->type_definition_forbidden_message
13940 = G_("types may not be defined in %<decltype%> expressions");
13942 /* The restrictions on constant-expressions do not apply inside
13943 decltype expressions. */
13944 saved_integral_constant_expression_p
13945 = parser->integral_constant_expression_p;
13946 saved_non_integral_constant_expression_p
13947 = parser->non_integral_constant_expression_p;
13948 parser->integral_constant_expression_p = false;
13950 /* Within a parenthesized expression, a `>' token is always
13951 the greater-than operator. */
13952 saved_greater_than_is_operator_p
13953 = parser->greater_than_is_operator_p;
13954 parser->greater_than_is_operator_p = true;
13956 /* Do not actually evaluate the expression. */
13957 ++cp_unevaluated_operand;
13959 /* Do not warn about problems with the expression. */
13960 ++c_inhibit_evaluation_warnings;
13962 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13964 /* Go back to evaluating expressions. */
13965 --cp_unevaluated_operand;
13966 --c_inhibit_evaluation_warnings;
13968 /* The `>' token might be the end of a template-id or
13969 template-parameter-list now. */
13970 parser->greater_than_is_operator_p
13971 = saved_greater_than_is_operator_p;
13973 /* Restore the old message and the integral constant expression
13974 flags. */
13975 parser->type_definition_forbidden_message = saved_message;
13976 parser->integral_constant_expression_p
13977 = saved_integral_constant_expression_p;
13978 parser->non_integral_constant_expression_p
13979 = saved_non_integral_constant_expression_p;
13981 /* Parse to the closing `)'. */
13982 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13984 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13985 /*consume_paren=*/true);
13986 return error_mark_node;
13989 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13990 tf_warning_or_error);
13992 rewrite:
13993 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13994 it again. */
13995 start_token->type = CPP_DECLTYPE;
13996 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13997 start_token->u.tree_check_value->value = expr;
13998 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13999 start_token->keyword = RID_MAX;
14000 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14002 return expr;
14005 /* Special member functions [gram.special] */
14007 /* Parse a conversion-function-id.
14009 conversion-function-id:
14010 operator conversion-type-id
14012 Returns an IDENTIFIER_NODE representing the operator. */
14014 static tree
14015 cp_parser_conversion_function_id (cp_parser* parser)
14017 tree type;
14018 tree saved_scope;
14019 tree saved_qualifying_scope;
14020 tree saved_object_scope;
14021 tree pushed_scope = NULL_TREE;
14023 /* Look for the `operator' token. */
14024 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14025 return error_mark_node;
14026 /* When we parse the conversion-type-id, the current scope will be
14027 reset. However, we need that information in able to look up the
14028 conversion function later, so we save it here. */
14029 saved_scope = parser->scope;
14030 saved_qualifying_scope = parser->qualifying_scope;
14031 saved_object_scope = parser->object_scope;
14032 /* We must enter the scope of the class so that the names of
14033 entities declared within the class are available in the
14034 conversion-type-id. For example, consider:
14036 struct S {
14037 typedef int I;
14038 operator I();
14041 S::operator I() { ... }
14043 In order to see that `I' is a type-name in the definition, we
14044 must be in the scope of `S'. */
14045 if (saved_scope)
14046 pushed_scope = push_scope (saved_scope);
14047 /* Parse the conversion-type-id. */
14048 type = cp_parser_conversion_type_id (parser);
14049 /* Leave the scope of the class, if any. */
14050 if (pushed_scope)
14051 pop_scope (pushed_scope);
14052 /* Restore the saved scope. */
14053 parser->scope = saved_scope;
14054 parser->qualifying_scope = saved_qualifying_scope;
14055 parser->object_scope = saved_object_scope;
14056 /* If the TYPE is invalid, indicate failure. */
14057 if (type == error_mark_node)
14058 return error_mark_node;
14059 return mangle_conv_op_name_for_type (type);
14062 /* Parse a conversion-type-id:
14064 conversion-type-id:
14065 type-specifier-seq conversion-declarator [opt]
14067 Returns the TYPE specified. */
14069 static tree
14070 cp_parser_conversion_type_id (cp_parser* parser)
14072 tree attributes;
14073 cp_decl_specifier_seq type_specifiers;
14074 cp_declarator *declarator;
14075 tree type_specified;
14076 const char *saved_message;
14078 /* Parse the attributes. */
14079 attributes = cp_parser_attributes_opt (parser);
14081 saved_message = parser->type_definition_forbidden_message;
14082 parser->type_definition_forbidden_message
14083 = G_("types may not be defined in a conversion-type-id");
14085 /* Parse the type-specifiers. */
14086 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14087 /*is_trailing_return=*/false,
14088 &type_specifiers);
14090 parser->type_definition_forbidden_message = saved_message;
14092 /* If that didn't work, stop. */
14093 if (type_specifiers.type == error_mark_node)
14094 return error_mark_node;
14095 /* Parse the conversion-declarator. */
14096 declarator = cp_parser_conversion_declarator_opt (parser);
14098 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14099 /*initialized=*/0, &attributes);
14100 if (attributes)
14101 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14103 /* Don't give this error when parsing tentatively. This happens to
14104 work because we always parse this definitively once. */
14105 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14106 && type_uses_auto (type_specified))
14108 if (cxx_dialect < cxx14)
14110 error ("invalid use of %<auto%> in conversion operator");
14111 return error_mark_node;
14113 else if (template_parm_scope_p ())
14114 warning (0, "use of %<auto%> in member template "
14115 "conversion operator can never be deduced");
14118 return type_specified;
14121 /* Parse an (optional) conversion-declarator.
14123 conversion-declarator:
14124 ptr-operator conversion-declarator [opt]
14128 static cp_declarator *
14129 cp_parser_conversion_declarator_opt (cp_parser* parser)
14131 enum tree_code code;
14132 tree class_type, std_attributes = NULL_TREE;
14133 cp_cv_quals cv_quals;
14135 /* We don't know if there's a ptr-operator next, or not. */
14136 cp_parser_parse_tentatively (parser);
14137 /* Try the ptr-operator. */
14138 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14139 &std_attributes);
14140 /* If it worked, look for more conversion-declarators. */
14141 if (cp_parser_parse_definitely (parser))
14143 cp_declarator *declarator;
14145 /* Parse another optional declarator. */
14146 declarator = cp_parser_conversion_declarator_opt (parser);
14148 declarator = cp_parser_make_indirect_declarator
14149 (code, class_type, cv_quals, declarator, std_attributes);
14151 return declarator;
14154 return NULL;
14157 /* Parse an (optional) ctor-initializer.
14159 ctor-initializer:
14160 : mem-initializer-list
14162 Returns TRUE iff the ctor-initializer was actually present. */
14164 static bool
14165 cp_parser_ctor_initializer_opt (cp_parser* parser)
14167 /* If the next token is not a `:', then there is no
14168 ctor-initializer. */
14169 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14171 /* Do default initialization of any bases and members. */
14172 if (DECL_CONSTRUCTOR_P (current_function_decl))
14173 finish_mem_initializers (NULL_TREE);
14175 return false;
14178 /* Consume the `:' token. */
14179 cp_lexer_consume_token (parser->lexer);
14180 /* And the mem-initializer-list. */
14181 cp_parser_mem_initializer_list (parser);
14183 return true;
14186 /* Parse a mem-initializer-list.
14188 mem-initializer-list:
14189 mem-initializer ... [opt]
14190 mem-initializer ... [opt] , mem-initializer-list */
14192 static void
14193 cp_parser_mem_initializer_list (cp_parser* parser)
14195 tree mem_initializer_list = NULL_TREE;
14196 tree target_ctor = error_mark_node;
14197 cp_token *token = cp_lexer_peek_token (parser->lexer);
14199 /* Let the semantic analysis code know that we are starting the
14200 mem-initializer-list. */
14201 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14202 error_at (token->location,
14203 "only constructors take member initializers");
14205 /* Loop through the list. */
14206 while (true)
14208 tree mem_initializer;
14210 token = cp_lexer_peek_token (parser->lexer);
14211 /* Parse the mem-initializer. */
14212 mem_initializer = cp_parser_mem_initializer (parser);
14213 /* If the next token is a `...', we're expanding member initializers. */
14214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14216 /* Consume the `...'. */
14217 cp_lexer_consume_token (parser->lexer);
14219 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14220 can be expanded but members cannot. */
14221 if (mem_initializer != error_mark_node
14222 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14224 error_at (token->location,
14225 "cannot expand initializer for member %qD",
14226 TREE_PURPOSE (mem_initializer));
14227 mem_initializer = error_mark_node;
14230 /* Construct the pack expansion type. */
14231 if (mem_initializer != error_mark_node)
14232 mem_initializer = make_pack_expansion (mem_initializer);
14234 if (target_ctor != error_mark_node
14235 && mem_initializer != error_mark_node)
14237 error ("mem-initializer for %qD follows constructor delegation",
14238 TREE_PURPOSE (mem_initializer));
14239 mem_initializer = error_mark_node;
14241 /* Look for a target constructor. */
14242 if (mem_initializer != error_mark_node
14243 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14244 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14246 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14247 if (mem_initializer_list)
14249 error ("constructor delegation follows mem-initializer for %qD",
14250 TREE_PURPOSE (mem_initializer_list));
14251 mem_initializer = error_mark_node;
14253 target_ctor = mem_initializer;
14255 /* Add it to the list, unless it was erroneous. */
14256 if (mem_initializer != error_mark_node)
14258 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14259 mem_initializer_list = mem_initializer;
14261 /* If the next token is not a `,', we're done. */
14262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14263 break;
14264 /* Consume the `,' token. */
14265 cp_lexer_consume_token (parser->lexer);
14268 /* Perform semantic analysis. */
14269 if (DECL_CONSTRUCTOR_P (current_function_decl))
14270 finish_mem_initializers (mem_initializer_list);
14273 /* Parse a mem-initializer.
14275 mem-initializer:
14276 mem-initializer-id ( expression-list [opt] )
14277 mem-initializer-id braced-init-list
14279 GNU extension:
14281 mem-initializer:
14282 ( expression-list [opt] )
14284 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14285 class) or FIELD_DECL (for a non-static data member) to initialize;
14286 the TREE_VALUE is the expression-list. An empty initialization
14287 list is represented by void_list_node. */
14289 static tree
14290 cp_parser_mem_initializer (cp_parser* parser)
14292 tree mem_initializer_id;
14293 tree expression_list;
14294 tree member;
14295 cp_token *token = cp_lexer_peek_token (parser->lexer);
14297 /* Find out what is being initialized. */
14298 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14300 permerror (token->location,
14301 "anachronistic old-style base class initializer");
14302 mem_initializer_id = NULL_TREE;
14304 else
14306 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14307 if (mem_initializer_id == error_mark_node)
14308 return mem_initializer_id;
14310 member = expand_member_init (mem_initializer_id);
14311 if (member && !DECL_P (member))
14312 in_base_initializer = 1;
14314 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14316 bool expr_non_constant_p;
14317 cp_lexer_set_source_position (parser->lexer);
14318 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14319 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14320 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14321 expression_list = build_tree_list (NULL_TREE, expression_list);
14323 else
14325 vec<tree, va_gc> *vec;
14326 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14327 /*cast_p=*/false,
14328 /*allow_expansion_p=*/true,
14329 /*non_constant_p=*/NULL);
14330 if (vec == NULL)
14331 return error_mark_node;
14332 expression_list = build_tree_list_vec (vec);
14333 release_tree_vector (vec);
14336 if (expression_list == error_mark_node)
14337 return error_mark_node;
14338 if (!expression_list)
14339 expression_list = void_type_node;
14341 in_base_initializer = 0;
14343 return member ? build_tree_list (member, expression_list) : error_mark_node;
14346 /* Parse a mem-initializer-id.
14348 mem-initializer-id:
14349 :: [opt] nested-name-specifier [opt] class-name
14350 decltype-specifier (C++11)
14351 identifier
14353 Returns a TYPE indicating the class to be initialized for the first
14354 production (and the second in C++11). Returns an IDENTIFIER_NODE
14355 indicating the data member to be initialized for the last production. */
14357 static tree
14358 cp_parser_mem_initializer_id (cp_parser* parser)
14360 bool global_scope_p;
14361 bool nested_name_specifier_p;
14362 bool template_p = false;
14363 tree id;
14365 cp_token *token = cp_lexer_peek_token (parser->lexer);
14367 /* `typename' is not allowed in this context ([temp.res]). */
14368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14370 error_at (token->location,
14371 "keyword %<typename%> not allowed in this context (a qualified "
14372 "member initializer is implicitly a type)");
14373 cp_lexer_consume_token (parser->lexer);
14375 /* Look for the optional `::' operator. */
14376 global_scope_p
14377 = (cp_parser_global_scope_opt (parser,
14378 /*current_scope_valid_p=*/false)
14379 != NULL_TREE);
14380 /* Look for the optional nested-name-specifier. The simplest way to
14381 implement:
14383 [temp.res]
14385 The keyword `typename' is not permitted in a base-specifier or
14386 mem-initializer; in these contexts a qualified name that
14387 depends on a template-parameter is implicitly assumed to be a
14388 type name.
14390 is to assume that we have seen the `typename' keyword at this
14391 point. */
14392 nested_name_specifier_p
14393 = (cp_parser_nested_name_specifier_opt (parser,
14394 /*typename_keyword_p=*/true,
14395 /*check_dependency_p=*/true,
14396 /*type_p=*/true,
14397 /*is_declaration=*/true)
14398 != NULL_TREE);
14399 if (nested_name_specifier_p)
14400 template_p = cp_parser_optional_template_keyword (parser);
14401 /* If there is a `::' operator or a nested-name-specifier, then we
14402 are definitely looking for a class-name. */
14403 if (global_scope_p || nested_name_specifier_p)
14404 return cp_parser_class_name (parser,
14405 /*typename_keyword_p=*/true,
14406 /*template_keyword_p=*/template_p,
14407 typename_type,
14408 /*check_dependency_p=*/true,
14409 /*class_head_p=*/false,
14410 /*is_declaration=*/true);
14411 /* Otherwise, we could also be looking for an ordinary identifier. */
14412 cp_parser_parse_tentatively (parser);
14413 if (cp_lexer_next_token_is_decltype (parser->lexer))
14414 /* Try a decltype-specifier. */
14415 id = cp_parser_decltype (parser);
14416 else
14417 /* Otherwise, try a class-name. */
14418 id = cp_parser_class_name (parser,
14419 /*typename_keyword_p=*/true,
14420 /*template_keyword_p=*/false,
14421 none_type,
14422 /*check_dependency_p=*/true,
14423 /*class_head_p=*/false,
14424 /*is_declaration=*/true);
14425 /* If we found one, we're done. */
14426 if (cp_parser_parse_definitely (parser))
14427 return id;
14428 /* Otherwise, look for an ordinary identifier. */
14429 return cp_parser_identifier (parser);
14432 /* Overloading [gram.over] */
14434 /* Parse an operator-function-id.
14436 operator-function-id:
14437 operator operator
14439 Returns an IDENTIFIER_NODE for the operator which is a
14440 human-readable spelling of the identifier, e.g., `operator +'. */
14442 static cp_expr
14443 cp_parser_operator_function_id (cp_parser* parser)
14445 /* Look for the `operator' keyword. */
14446 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14447 return error_mark_node;
14448 /* And then the name of the operator itself. */
14449 return cp_parser_operator (parser);
14452 /* Return an identifier node for a user-defined literal operator.
14453 The suffix identifier is chained to the operator name identifier. */
14455 tree
14456 cp_literal_operator_id (const char* name)
14458 tree identifier;
14459 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14460 + strlen (name) + 10);
14461 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14462 identifier = get_identifier (buffer);
14464 return identifier;
14467 /* Parse an operator.
14469 operator:
14470 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14471 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14472 || ++ -- , ->* -> () []
14474 GNU Extensions:
14476 operator:
14477 <? >? <?= >?=
14479 Returns an IDENTIFIER_NODE for the operator which is a
14480 human-readable spelling of the identifier, e.g., `operator +'. */
14482 static cp_expr
14483 cp_parser_operator (cp_parser* parser)
14485 tree id = NULL_TREE;
14486 cp_token *token;
14487 bool utf8 = false;
14489 /* Peek at the next token. */
14490 token = cp_lexer_peek_token (parser->lexer);
14492 location_t start_loc = token->location;
14494 /* Figure out which operator we have. */
14495 switch (token->type)
14497 case CPP_KEYWORD:
14499 enum tree_code op;
14501 /* The keyword should be either `new' or `delete'. */
14502 if (token->keyword == RID_NEW)
14503 op = NEW_EXPR;
14504 else if (token->keyword == RID_DELETE)
14505 op = DELETE_EXPR;
14506 else
14507 break;
14509 /* Consume the `new' or `delete' token. */
14510 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14512 /* Peek at the next token. */
14513 token = cp_lexer_peek_token (parser->lexer);
14514 /* If it's a `[' token then this is the array variant of the
14515 operator. */
14516 if (token->type == CPP_OPEN_SQUARE)
14518 /* Consume the `[' token. */
14519 cp_lexer_consume_token (parser->lexer);
14520 /* Look for the `]' token. */
14521 if (cp_token *close_token
14522 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14523 end_loc = close_token->location;
14524 id = cp_operator_id (op == NEW_EXPR
14525 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14527 /* Otherwise, we have the non-array variant. */
14528 else
14529 id = cp_operator_id (op);
14531 location_t loc = make_location (start_loc, start_loc, end_loc);
14533 return cp_expr (id, loc);
14536 case CPP_PLUS:
14537 id = cp_operator_id (PLUS_EXPR);
14538 break;
14540 case CPP_MINUS:
14541 id = cp_operator_id (MINUS_EXPR);
14542 break;
14544 case CPP_MULT:
14545 id = cp_operator_id (MULT_EXPR);
14546 break;
14548 case CPP_DIV:
14549 id = cp_operator_id (TRUNC_DIV_EXPR);
14550 break;
14552 case CPP_MOD:
14553 id = cp_operator_id (TRUNC_MOD_EXPR);
14554 break;
14556 case CPP_XOR:
14557 id = cp_operator_id (BIT_XOR_EXPR);
14558 break;
14560 case CPP_AND:
14561 id = cp_operator_id (BIT_AND_EXPR);
14562 break;
14564 case CPP_OR:
14565 id = cp_operator_id (BIT_IOR_EXPR);
14566 break;
14568 case CPP_COMPL:
14569 id = cp_operator_id (BIT_NOT_EXPR);
14570 break;
14572 case CPP_NOT:
14573 id = cp_operator_id (TRUTH_NOT_EXPR);
14574 break;
14576 case CPP_EQ:
14577 id = cp_assignment_operator_id (NOP_EXPR);
14578 break;
14580 case CPP_LESS:
14581 id = cp_operator_id (LT_EXPR);
14582 break;
14584 case CPP_GREATER:
14585 id = cp_operator_id (GT_EXPR);
14586 break;
14588 case CPP_PLUS_EQ:
14589 id = cp_assignment_operator_id (PLUS_EXPR);
14590 break;
14592 case CPP_MINUS_EQ:
14593 id = cp_assignment_operator_id (MINUS_EXPR);
14594 break;
14596 case CPP_MULT_EQ:
14597 id = cp_assignment_operator_id (MULT_EXPR);
14598 break;
14600 case CPP_DIV_EQ:
14601 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14602 break;
14604 case CPP_MOD_EQ:
14605 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14606 break;
14608 case CPP_XOR_EQ:
14609 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14610 break;
14612 case CPP_AND_EQ:
14613 id = cp_assignment_operator_id (BIT_AND_EXPR);
14614 break;
14616 case CPP_OR_EQ:
14617 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14618 break;
14620 case CPP_LSHIFT:
14621 id = cp_operator_id (LSHIFT_EXPR);
14622 break;
14624 case CPP_RSHIFT:
14625 id = cp_operator_id (RSHIFT_EXPR);
14626 break;
14628 case CPP_LSHIFT_EQ:
14629 id = cp_assignment_operator_id (LSHIFT_EXPR);
14630 break;
14632 case CPP_RSHIFT_EQ:
14633 id = cp_assignment_operator_id (RSHIFT_EXPR);
14634 break;
14636 case CPP_EQ_EQ:
14637 id = cp_operator_id (EQ_EXPR);
14638 break;
14640 case CPP_NOT_EQ:
14641 id = cp_operator_id (NE_EXPR);
14642 break;
14644 case CPP_LESS_EQ:
14645 id = cp_operator_id (LE_EXPR);
14646 break;
14648 case CPP_GREATER_EQ:
14649 id = cp_operator_id (GE_EXPR);
14650 break;
14652 case CPP_AND_AND:
14653 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14654 break;
14656 case CPP_OR_OR:
14657 id = cp_operator_id (TRUTH_ORIF_EXPR);
14658 break;
14660 case CPP_PLUS_PLUS:
14661 id = cp_operator_id (POSTINCREMENT_EXPR);
14662 break;
14664 case CPP_MINUS_MINUS:
14665 id = cp_operator_id (PREDECREMENT_EXPR);
14666 break;
14668 case CPP_COMMA:
14669 id = cp_operator_id (COMPOUND_EXPR);
14670 break;
14672 case CPP_DEREF_STAR:
14673 id = cp_operator_id (MEMBER_REF);
14674 break;
14676 case CPP_DEREF:
14677 id = cp_operator_id (COMPONENT_REF);
14678 break;
14680 case CPP_OPEN_PAREN:
14681 /* Consume the `('. */
14682 cp_lexer_consume_token (parser->lexer);
14683 /* Look for the matching `)'. */
14684 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14685 return cp_operator_id (CALL_EXPR);
14687 case CPP_OPEN_SQUARE:
14688 /* Consume the `['. */
14689 cp_lexer_consume_token (parser->lexer);
14690 /* Look for the matching `]'. */
14691 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14692 return cp_operator_id (ARRAY_REF);
14694 case CPP_UTF8STRING:
14695 case CPP_UTF8STRING_USERDEF:
14696 utf8 = true;
14697 /* FALLTHRU */
14698 case CPP_STRING:
14699 case CPP_WSTRING:
14700 case CPP_STRING16:
14701 case CPP_STRING32:
14702 case CPP_STRING_USERDEF:
14703 case CPP_WSTRING_USERDEF:
14704 case CPP_STRING16_USERDEF:
14705 case CPP_STRING32_USERDEF:
14707 tree str, string_tree;
14708 int sz, len;
14710 if (cxx_dialect == cxx98)
14711 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14713 /* Consume the string. */
14714 str = cp_parser_string_literal (parser, /*translate=*/true,
14715 /*wide_ok=*/true, /*lookup_udlit=*/false);
14716 if (str == error_mark_node)
14717 return error_mark_node;
14718 else if (TREE_CODE (str) == USERDEF_LITERAL)
14720 string_tree = USERDEF_LITERAL_VALUE (str);
14721 id = USERDEF_LITERAL_SUFFIX_ID (str);
14723 else
14725 string_tree = str;
14726 /* Look for the suffix identifier. */
14727 token = cp_lexer_peek_token (parser->lexer);
14728 if (token->type == CPP_NAME)
14729 id = cp_parser_identifier (parser);
14730 else if (token->type == CPP_KEYWORD)
14732 error ("unexpected keyword;"
14733 " remove space between quotes and suffix identifier");
14734 return error_mark_node;
14736 else
14738 error ("expected suffix identifier");
14739 return error_mark_node;
14742 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14743 (TREE_TYPE (TREE_TYPE (string_tree))));
14744 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14745 if (len != 0)
14747 error ("expected empty string after %<operator%> keyword");
14748 return error_mark_node;
14750 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14751 != char_type_node)
14753 error ("invalid encoding prefix in literal operator");
14754 return error_mark_node;
14756 if (id != error_mark_node)
14758 const char *name = IDENTIFIER_POINTER (id);
14759 id = cp_literal_operator_id (name);
14761 return id;
14764 default:
14765 /* Anything else is an error. */
14766 break;
14769 /* If we have selected an identifier, we need to consume the
14770 operator token. */
14771 if (id)
14772 cp_lexer_consume_token (parser->lexer);
14773 /* Otherwise, no valid operator name was present. */
14774 else
14776 cp_parser_error (parser, "expected operator");
14777 id = error_mark_node;
14780 return cp_expr (id, start_loc);
14783 /* Parse a template-declaration.
14785 template-declaration:
14786 export [opt] template < template-parameter-list > declaration
14788 If MEMBER_P is TRUE, this template-declaration occurs within a
14789 class-specifier.
14791 The grammar rule given by the standard isn't correct. What
14792 is really meant is:
14794 template-declaration:
14795 export [opt] template-parameter-list-seq
14796 decl-specifier-seq [opt] init-declarator [opt] ;
14797 export [opt] template-parameter-list-seq
14798 function-definition
14800 template-parameter-list-seq:
14801 template-parameter-list-seq [opt]
14802 template < template-parameter-list >
14804 Concept Extensions:
14806 template-parameter-list-seq:
14807 template < template-parameter-list > requires-clause [opt]
14809 requires-clause:
14810 requires logical-or-expression */
14812 static void
14813 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14815 /* Check for `export'. */
14816 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14818 /* Consume the `export' token. */
14819 cp_lexer_consume_token (parser->lexer);
14820 /* Warn that we do not support `export'. */
14821 warning (0, "keyword %<export%> not implemented, and will be ignored");
14824 cp_parser_template_declaration_after_export (parser, member_p);
14827 /* Parse a template-parameter-list.
14829 template-parameter-list:
14830 template-parameter
14831 template-parameter-list , template-parameter
14833 Returns a TREE_LIST. Each node represents a template parameter.
14834 The nodes are connected via their TREE_CHAINs. */
14836 static tree
14837 cp_parser_template_parameter_list (cp_parser* parser)
14839 tree parameter_list = NULL_TREE;
14841 begin_template_parm_list ();
14843 /* The loop below parses the template parms. We first need to know
14844 the total number of template parms to be able to compute proper
14845 canonical types of each dependent type. So after the loop, when
14846 we know the total number of template parms,
14847 end_template_parm_list computes the proper canonical types and
14848 fixes up the dependent types accordingly. */
14849 while (true)
14851 tree parameter;
14852 bool is_non_type;
14853 bool is_parameter_pack;
14854 location_t parm_loc;
14856 /* Parse the template-parameter. */
14857 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14858 parameter = cp_parser_template_parameter (parser,
14859 &is_non_type,
14860 &is_parameter_pack);
14861 /* Add it to the list. */
14862 if (parameter != error_mark_node)
14863 parameter_list = process_template_parm (parameter_list,
14864 parm_loc,
14865 parameter,
14866 is_non_type,
14867 is_parameter_pack);
14868 else
14870 tree err_parm = build_tree_list (parameter, parameter);
14871 parameter_list = chainon (parameter_list, err_parm);
14874 /* If the next token is not a `,', we're done. */
14875 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14876 break;
14877 /* Otherwise, consume the `,' token. */
14878 cp_lexer_consume_token (parser->lexer);
14881 return end_template_parm_list (parameter_list);
14884 /* Parse a introduction-list.
14886 introduction-list:
14887 introduced-parameter
14888 introduction-list , introduced-parameter
14890 introduced-parameter:
14891 ...[opt] identifier
14893 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14894 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14895 WILDCARD_DECL will also have DECL_NAME set and token location in
14896 DECL_SOURCE_LOCATION. */
14898 static tree
14899 cp_parser_introduction_list (cp_parser *parser)
14901 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14903 while (true)
14905 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14906 if (is_pack)
14907 cp_lexer_consume_token (parser->lexer);
14909 /* Build placeholder. */
14910 tree parm = build_nt (WILDCARD_DECL);
14911 DECL_SOURCE_LOCATION (parm)
14912 = cp_lexer_peek_token (parser->lexer)->location;
14913 DECL_NAME (parm) = cp_parser_identifier (parser);
14914 WILDCARD_PACK_P (parm) = is_pack;
14915 vec_safe_push (introduction_vec, parm);
14917 /* If the next token is not a `,', we're done. */
14918 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14919 break;
14920 /* Otherwise, consume the `,' token. */
14921 cp_lexer_consume_token (parser->lexer);
14924 /* Convert the vec into a TREE_VEC. */
14925 tree introduction_list = make_tree_vec (introduction_vec->length ());
14926 unsigned int n;
14927 tree parm;
14928 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14929 TREE_VEC_ELT (introduction_list, n) = parm;
14931 release_tree_vector (introduction_vec);
14932 return introduction_list;
14935 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14936 is an abstract declarator. */
14938 static inline cp_declarator*
14939 get_id_declarator (cp_declarator *declarator)
14941 cp_declarator *d = declarator;
14942 while (d && d->kind != cdk_id)
14943 d = d->declarator;
14944 return d;
14947 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14948 is an abstract declarator. */
14950 static inline tree
14951 get_unqualified_id (cp_declarator *declarator)
14953 declarator = get_id_declarator (declarator);
14954 if (declarator)
14955 return declarator->u.id.unqualified_name;
14956 else
14957 return NULL_TREE;
14960 /* Returns true if DECL represents a constrained-parameter. */
14962 static inline bool
14963 is_constrained_parameter (tree decl)
14965 return (decl
14966 && TREE_CODE (decl) == TYPE_DECL
14967 && CONSTRAINED_PARM_CONCEPT (decl)
14968 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14971 /* Returns true if PARM declares a constrained-parameter. */
14973 static inline bool
14974 is_constrained_parameter (cp_parameter_declarator *parm)
14976 return is_constrained_parameter (parm->decl_specifiers.type);
14979 /* Check that the type parameter is only a declarator-id, and that its
14980 type is not cv-qualified. */
14982 bool
14983 cp_parser_check_constrained_type_parm (cp_parser *parser,
14984 cp_parameter_declarator *parm)
14986 if (!parm->declarator)
14987 return true;
14989 if (parm->declarator->kind != cdk_id)
14991 cp_parser_error (parser, "invalid constrained type parameter");
14992 return false;
14995 /* Don't allow cv-qualified type parameters. */
14996 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14997 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14999 cp_parser_error (parser, "cv-qualified type parameter");
15000 return false;
15003 return true;
15006 /* Finish parsing/processing a template type parameter and checking
15007 various restrictions. */
15009 static inline tree
15010 cp_parser_constrained_type_template_parm (cp_parser *parser,
15011 tree id,
15012 cp_parameter_declarator* parmdecl)
15014 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15015 return finish_template_type_parm (class_type_node, id);
15016 else
15017 return error_mark_node;
15020 static tree
15021 finish_constrained_template_template_parm (tree proto, tree id)
15023 /* FIXME: This should probably be copied, and we may need to adjust
15024 the template parameter depths. */
15025 tree saved_parms = current_template_parms;
15026 begin_template_parm_list ();
15027 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15028 end_template_parm_list ();
15030 tree parm = finish_template_template_parm (class_type_node, id);
15031 current_template_parms = saved_parms;
15033 return parm;
15036 /* Finish parsing/processing a template template parameter by borrowing
15037 the template parameter list from the prototype parameter. */
15039 static tree
15040 cp_parser_constrained_template_template_parm (cp_parser *parser,
15041 tree proto,
15042 tree id,
15043 cp_parameter_declarator *parmdecl)
15045 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15046 return error_mark_node;
15047 return finish_constrained_template_template_parm (proto, id);
15050 /* Create a new non-type template parameter from the given PARM
15051 declarator. */
15053 static tree
15054 constrained_non_type_template_parm (bool *is_non_type,
15055 cp_parameter_declarator *parm)
15057 *is_non_type = true;
15058 cp_declarator *decl = parm->declarator;
15059 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15060 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15061 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15064 /* Build a constrained template parameter based on the PARMDECL
15065 declarator. The type of PARMDECL is the constrained type, which
15066 refers to the prototype template parameter that ultimately
15067 specifies the type of the declared parameter. */
15069 static tree
15070 finish_constrained_parameter (cp_parser *parser,
15071 cp_parameter_declarator *parmdecl,
15072 bool *is_non_type,
15073 bool *is_parameter_pack)
15075 tree decl = parmdecl->decl_specifiers.type;
15076 tree id = get_unqualified_id (parmdecl->declarator);
15077 tree def = parmdecl->default_argument;
15078 tree proto = DECL_INITIAL (decl);
15080 /* A template parameter constrained by a variadic concept shall also
15081 be declared as a template parameter pack. */
15082 bool is_variadic = template_parameter_pack_p (proto);
15083 if (is_variadic && !*is_parameter_pack)
15084 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15086 /* Build the parameter. Return an error if the declarator was invalid. */
15087 tree parm;
15088 if (TREE_CODE (proto) == TYPE_DECL)
15089 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15090 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15091 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15092 parmdecl);
15093 else
15094 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15095 if (parm == error_mark_node)
15096 return error_mark_node;
15098 /* Finish the parameter decl and create a node attaching the
15099 default argument and constraint. */
15100 parm = build_tree_list (def, parm);
15101 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15103 return parm;
15106 /* Returns true if the parsed type actually represents the declaration
15107 of a type template-parameter. */
15109 static inline bool
15110 declares_constrained_type_template_parameter (tree type)
15112 return (is_constrained_parameter (type)
15113 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15117 /* Returns true if the parsed type actually represents the declaration of
15118 a template template-parameter. */
15120 static bool
15121 declares_constrained_template_template_parameter (tree type)
15123 return (is_constrained_parameter (type)
15124 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15127 /* Parse a default argument for a type template-parameter.
15128 Note that diagnostics are handled in cp_parser_template_parameter. */
15130 static tree
15131 cp_parser_default_type_template_argument (cp_parser *parser)
15133 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15135 /* Consume the `=' token. */
15136 cp_lexer_consume_token (parser->lexer);
15138 cp_token *token = cp_lexer_peek_token (parser->lexer);
15140 /* Parse the default-argument. */
15141 push_deferring_access_checks (dk_no_deferred);
15142 tree default_argument = cp_parser_type_id (parser);
15143 pop_deferring_access_checks ();
15145 if (flag_concepts && type_uses_auto (default_argument))
15147 error_at (token->location,
15148 "invalid use of %<auto%> in default template argument");
15149 return error_mark_node;
15152 return default_argument;
15155 /* Parse a default argument for a template template-parameter. */
15157 static tree
15158 cp_parser_default_template_template_argument (cp_parser *parser)
15160 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15162 bool is_template;
15164 /* Consume the `='. */
15165 cp_lexer_consume_token (parser->lexer);
15166 /* Parse the id-expression. */
15167 push_deferring_access_checks (dk_no_deferred);
15168 /* save token before parsing the id-expression, for error
15169 reporting */
15170 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15171 tree default_argument
15172 = cp_parser_id_expression (parser,
15173 /*template_keyword_p=*/false,
15174 /*check_dependency_p=*/true,
15175 /*template_p=*/&is_template,
15176 /*declarator_p=*/false,
15177 /*optional_p=*/false);
15178 if (TREE_CODE (default_argument) == TYPE_DECL)
15179 /* If the id-expression was a template-id that refers to
15180 a template-class, we already have the declaration here,
15181 so no further lookup is needed. */
15183 else
15184 /* Look up the name. */
15185 default_argument
15186 = cp_parser_lookup_name (parser, default_argument,
15187 none_type,
15188 /*is_template=*/is_template,
15189 /*is_namespace=*/false,
15190 /*check_dependency=*/true,
15191 /*ambiguous_decls=*/NULL,
15192 token->location);
15193 /* See if the default argument is valid. */
15194 default_argument = check_template_template_default_arg (default_argument);
15195 pop_deferring_access_checks ();
15196 return default_argument;
15199 /* Parse a template-parameter.
15201 template-parameter:
15202 type-parameter
15203 parameter-declaration
15205 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15206 the parameter. The TREE_PURPOSE is the default value, if any.
15207 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15208 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15209 set to true iff this parameter is a parameter pack. */
15211 static tree
15212 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15213 bool *is_parameter_pack)
15215 cp_token *token;
15216 cp_parameter_declarator *parameter_declarator;
15217 tree parm;
15219 /* Assume it is a type parameter or a template parameter. */
15220 *is_non_type = false;
15221 /* Assume it not a parameter pack. */
15222 *is_parameter_pack = false;
15223 /* Peek at the next token. */
15224 token = cp_lexer_peek_token (parser->lexer);
15225 /* If it is `template', we have a type-parameter. */
15226 if (token->keyword == RID_TEMPLATE)
15227 return cp_parser_type_parameter (parser, is_parameter_pack);
15228 /* If it is `class' or `typename' we do not know yet whether it is a
15229 type parameter or a non-type parameter. Consider:
15231 template <typename T, typename T::X X> ...
15235 template <class C, class D*> ...
15237 Here, the first parameter is a type parameter, and the second is
15238 a non-type parameter. We can tell by looking at the token after
15239 the identifier -- if it is a `,', `=', or `>' then we have a type
15240 parameter. */
15241 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15243 /* Peek at the token after `class' or `typename'. */
15244 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15245 /* If it's an ellipsis, we have a template type parameter
15246 pack. */
15247 if (token->type == CPP_ELLIPSIS)
15248 return cp_parser_type_parameter (parser, is_parameter_pack);
15249 /* If it's an identifier, skip it. */
15250 if (token->type == CPP_NAME)
15251 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15252 /* Now, see if the token looks like the end of a template
15253 parameter. */
15254 if (token->type == CPP_COMMA
15255 || token->type == CPP_EQ
15256 || token->type == CPP_GREATER)
15257 return cp_parser_type_parameter (parser, is_parameter_pack);
15260 /* Otherwise, it is a non-type parameter or a constrained parameter.
15262 [temp.param]
15264 When parsing a default template-argument for a non-type
15265 template-parameter, the first non-nested `>' is taken as the end
15266 of the template parameter-list rather than a greater-than
15267 operator. */
15268 parameter_declarator
15269 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15270 /*parenthesized_p=*/NULL);
15272 if (!parameter_declarator)
15273 return error_mark_node;
15275 /* If the parameter declaration is marked as a parameter pack, set
15276 *IS_PARAMETER_PACK to notify the caller. */
15277 if (parameter_declarator->template_parameter_pack_p)
15278 *is_parameter_pack = true;
15280 if (parameter_declarator->default_argument)
15282 /* Can happen in some cases of erroneous input (c++/34892). */
15283 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15284 /* Consume the `...' for better error recovery. */
15285 cp_lexer_consume_token (parser->lexer);
15288 // The parameter may have been constrained.
15289 if (is_constrained_parameter (parameter_declarator))
15290 return finish_constrained_parameter (parser,
15291 parameter_declarator,
15292 is_non_type,
15293 is_parameter_pack);
15295 // Now we're sure that the parameter is a non-type parameter.
15296 *is_non_type = true;
15298 parm = grokdeclarator (parameter_declarator->declarator,
15299 &parameter_declarator->decl_specifiers,
15300 TPARM, /*initialized=*/0,
15301 /*attrlist=*/NULL);
15302 if (parm == error_mark_node)
15303 return error_mark_node;
15305 return build_tree_list (parameter_declarator->default_argument, parm);
15308 /* Parse a type-parameter.
15310 type-parameter:
15311 class identifier [opt]
15312 class identifier [opt] = type-id
15313 typename identifier [opt]
15314 typename identifier [opt] = type-id
15315 template < template-parameter-list > class identifier [opt]
15316 template < template-parameter-list > class identifier [opt]
15317 = id-expression
15319 GNU Extension (variadic templates):
15321 type-parameter:
15322 class ... identifier [opt]
15323 typename ... identifier [opt]
15325 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15326 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15327 the declaration of the parameter.
15329 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15331 static tree
15332 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15334 cp_token *token;
15335 tree parameter;
15337 /* Look for a keyword to tell us what kind of parameter this is. */
15338 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15339 if (!token)
15340 return error_mark_node;
15342 switch (token->keyword)
15344 case RID_CLASS:
15345 case RID_TYPENAME:
15347 tree identifier;
15348 tree default_argument;
15350 /* If the next token is an ellipsis, we have a template
15351 argument pack. */
15352 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15354 /* Consume the `...' token. */
15355 cp_lexer_consume_token (parser->lexer);
15356 maybe_warn_variadic_templates ();
15358 *is_parameter_pack = true;
15361 /* If the next token is an identifier, then it names the
15362 parameter. */
15363 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15364 identifier = cp_parser_identifier (parser);
15365 else
15366 identifier = NULL_TREE;
15368 /* Create the parameter. */
15369 parameter = finish_template_type_parm (class_type_node, identifier);
15371 /* If the next token is an `=', we have a default argument. */
15372 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15374 default_argument
15375 = cp_parser_default_type_template_argument (parser);
15377 /* Template parameter packs cannot have default
15378 arguments. */
15379 if (*is_parameter_pack)
15381 if (identifier)
15382 error_at (token->location,
15383 "template parameter pack %qD cannot have a "
15384 "default argument", identifier);
15385 else
15386 error_at (token->location,
15387 "template parameter packs cannot have "
15388 "default arguments");
15389 default_argument = NULL_TREE;
15391 else if (check_for_bare_parameter_packs (default_argument))
15392 default_argument = error_mark_node;
15394 else
15395 default_argument = NULL_TREE;
15397 /* Create the combined representation of the parameter and the
15398 default argument. */
15399 parameter = build_tree_list (default_argument, parameter);
15401 break;
15403 case RID_TEMPLATE:
15405 tree identifier;
15406 tree default_argument;
15408 /* Look for the `<'. */
15409 cp_parser_require (parser, CPP_LESS, RT_LESS);
15410 /* Parse the template-parameter-list. */
15411 cp_parser_template_parameter_list (parser);
15412 /* Look for the `>'. */
15413 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15415 // If template requirements are present, parse them.
15416 if (flag_concepts)
15418 tree reqs = get_shorthand_constraints (current_template_parms);
15419 if (tree r = cp_parser_requires_clause_opt (parser))
15420 reqs = conjoin_constraints (reqs, normalize_expression (r));
15421 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15424 /* Look for the `class' or 'typename' keywords. */
15425 cp_parser_type_parameter_key (parser);
15426 /* If the next token is an ellipsis, we have a template
15427 argument pack. */
15428 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15430 /* Consume the `...' token. */
15431 cp_lexer_consume_token (parser->lexer);
15432 maybe_warn_variadic_templates ();
15434 *is_parameter_pack = true;
15436 /* If the next token is an `=', then there is a
15437 default-argument. If the next token is a `>', we are at
15438 the end of the parameter-list. If the next token is a `,',
15439 then we are at the end of this parameter. */
15440 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15441 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15442 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15444 identifier = cp_parser_identifier (parser);
15445 /* Treat invalid names as if the parameter were nameless. */
15446 if (identifier == error_mark_node)
15447 identifier = NULL_TREE;
15449 else
15450 identifier = NULL_TREE;
15452 /* Create the template parameter. */
15453 parameter = finish_template_template_parm (class_type_node,
15454 identifier);
15456 /* If the next token is an `=', then there is a
15457 default-argument. */
15458 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15460 default_argument
15461 = cp_parser_default_template_template_argument (parser);
15463 /* Template parameter packs cannot have default
15464 arguments. */
15465 if (*is_parameter_pack)
15467 if (identifier)
15468 error_at (token->location,
15469 "template parameter pack %qD cannot "
15470 "have a default argument",
15471 identifier);
15472 else
15473 error_at (token->location, "template parameter packs cannot "
15474 "have default arguments");
15475 default_argument = NULL_TREE;
15478 else
15479 default_argument = NULL_TREE;
15481 /* Create the combined representation of the parameter and the
15482 default argument. */
15483 parameter = build_tree_list (default_argument, parameter);
15485 break;
15487 default:
15488 gcc_unreachable ();
15489 break;
15492 return parameter;
15495 /* Parse a template-id.
15497 template-id:
15498 template-name < template-argument-list [opt] >
15500 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15501 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15502 returned. Otherwise, if the template-name names a function, or set
15503 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15504 names a class, returns a TYPE_DECL for the specialization.
15506 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15507 uninstantiated templates. */
15509 static tree
15510 cp_parser_template_id (cp_parser *parser,
15511 bool template_keyword_p,
15512 bool check_dependency_p,
15513 enum tag_types tag_type,
15514 bool is_declaration)
15516 tree templ;
15517 tree arguments;
15518 tree template_id;
15519 cp_token_position start_of_id = 0;
15520 cp_token *next_token = NULL, *next_token_2 = NULL;
15521 bool is_identifier;
15523 /* If the next token corresponds to a template-id, there is no need
15524 to reparse it. */
15525 next_token = cp_lexer_peek_token (parser->lexer);
15526 if (next_token->type == CPP_TEMPLATE_ID)
15528 cp_lexer_consume_token (parser->lexer);
15529 return saved_checks_value (next_token->u.tree_check_value);
15532 /* Avoid performing name lookup if there is no possibility of
15533 finding a template-id. */
15534 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15535 || (next_token->type == CPP_NAME
15536 && !cp_parser_nth_token_starts_template_argument_list_p
15537 (parser, 2)))
15539 cp_parser_error (parser, "expected template-id");
15540 return error_mark_node;
15543 /* Remember where the template-id starts. */
15544 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15545 start_of_id = cp_lexer_token_position (parser->lexer, false);
15547 push_deferring_access_checks (dk_deferred);
15549 /* Parse the template-name. */
15550 is_identifier = false;
15551 templ = cp_parser_template_name (parser, template_keyword_p,
15552 check_dependency_p,
15553 is_declaration,
15554 tag_type,
15555 &is_identifier);
15556 if (templ == error_mark_node || is_identifier)
15558 pop_deferring_access_checks ();
15559 return templ;
15562 /* Since we're going to preserve any side-effects from this parse, set up a
15563 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15564 in the template arguments. */
15565 tentative_firewall firewall (parser);
15567 /* If we find the sequence `[:' after a template-name, it's probably
15568 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15569 parse correctly the argument list. */
15570 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15571 == CPP_OPEN_SQUARE)
15572 && next_token->flags & DIGRAPH
15573 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15574 == CPP_COLON)
15575 && !(next_token_2->flags & PREV_WHITE))
15577 cp_parser_parse_tentatively (parser);
15578 /* Change `:' into `::'. */
15579 next_token_2->type = CPP_SCOPE;
15580 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15581 CPP_LESS. */
15582 cp_lexer_consume_token (parser->lexer);
15584 /* Parse the arguments. */
15585 arguments = cp_parser_enclosed_template_argument_list (parser);
15586 if (!cp_parser_parse_definitely (parser))
15588 /* If we couldn't parse an argument list, then we revert our changes
15589 and return simply an error. Maybe this is not a template-id
15590 after all. */
15591 next_token_2->type = CPP_COLON;
15592 cp_parser_error (parser, "expected %<<%>");
15593 pop_deferring_access_checks ();
15594 return error_mark_node;
15596 /* Otherwise, emit an error about the invalid digraph, but continue
15597 parsing because we got our argument list. */
15598 if (permerror (next_token->location,
15599 "%<<::%> cannot begin a template-argument list"))
15601 static bool hint = false;
15602 inform (next_token->location,
15603 "%<<:%> is an alternate spelling for %<[%>."
15604 " Insert whitespace between %<<%> and %<::%>");
15605 if (!hint && !flag_permissive)
15607 inform (next_token->location, "(if you use %<-fpermissive%> "
15608 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15609 "accept your code)");
15610 hint = true;
15614 else
15616 /* Look for the `<' that starts the template-argument-list. */
15617 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15619 pop_deferring_access_checks ();
15620 return error_mark_node;
15622 /* Parse the arguments. */
15623 arguments = cp_parser_enclosed_template_argument_list (parser);
15626 /* Build a representation of the specialization. */
15627 if (identifier_p (templ))
15628 template_id = build_min_nt_loc (next_token->location,
15629 TEMPLATE_ID_EXPR,
15630 templ, arguments);
15631 else if (DECL_TYPE_TEMPLATE_P (templ)
15632 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15634 bool entering_scope;
15635 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15636 template (rather than some instantiation thereof) only if
15637 is not nested within some other construct. For example, in
15638 "template <typename T> void f(T) { A<T>::", A<T> is just an
15639 instantiation of A. */
15640 entering_scope = (template_parm_scope_p ()
15641 && cp_lexer_next_token_is (parser->lexer,
15642 CPP_SCOPE));
15643 template_id
15644 = finish_template_type (templ, arguments, entering_scope);
15646 /* A template-like identifier may be a partial concept id. */
15647 else if (flag_concepts
15648 && (template_id = (cp_parser_maybe_partial_concept_id
15649 (parser, templ, arguments))))
15650 return template_id;
15651 else if (variable_template_p (templ))
15653 template_id = lookup_template_variable (templ, arguments);
15654 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15655 SET_EXPR_LOCATION (template_id, next_token->location);
15657 else
15659 /* If it's not a class-template or a template-template, it should be
15660 a function-template. */
15661 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15662 || TREE_CODE (templ) == OVERLOAD
15663 || BASELINK_P (templ)));
15665 template_id = lookup_template_function (templ, arguments);
15666 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15667 SET_EXPR_LOCATION (template_id, next_token->location);
15670 /* If parsing tentatively, replace the sequence of tokens that makes
15671 up the template-id with a CPP_TEMPLATE_ID token. That way,
15672 should we re-parse the token stream, we will not have to repeat
15673 the effort required to do the parse, nor will we issue duplicate
15674 error messages about problems during instantiation of the
15675 template. */
15676 if (start_of_id
15677 /* Don't do this if we had a parse error in a declarator; re-parsing
15678 might succeed if a name changes meaning (60361). */
15679 && !(cp_parser_error_occurred (parser)
15680 && cp_parser_parsing_tentatively (parser)
15681 && parser->in_declarator_p))
15683 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15685 /* Reset the contents of the START_OF_ID token. */
15686 token->type = CPP_TEMPLATE_ID;
15688 /* Update the location to be of the form:
15689 template-name < template-argument-list [opt] >
15690 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15691 with caret == start at the start of the template-name,
15692 ranging until the closing '>'. */
15693 location_t finish_loc
15694 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15695 location_t combined_loc
15696 = make_location (token->location, token->location, finish_loc);
15697 token->location = combined_loc;
15699 /* We must mark the lookup as kept, so we don't throw it away on
15700 the first parse. */
15701 if (is_overloaded_fn (template_id))
15702 lookup_keep (get_fns (template_id), true);
15704 /* Retrieve any deferred checks. Do not pop this access checks yet
15705 so the memory will not be reclaimed during token replacing below. */
15706 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15707 token->u.tree_check_value->value = template_id;
15708 token->u.tree_check_value->checks = get_deferred_access_checks ();
15709 token->keyword = RID_MAX;
15711 /* Purge all subsequent tokens. */
15712 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15714 /* ??? Can we actually assume that, if template_id ==
15715 error_mark_node, we will have issued a diagnostic to the
15716 user, as opposed to simply marking the tentative parse as
15717 failed? */
15718 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15719 error_at (token->location, "parse error in template argument list");
15722 pop_to_parent_deferring_access_checks ();
15723 return template_id;
15726 /* Parse a template-name.
15728 template-name:
15729 identifier
15731 The standard should actually say:
15733 template-name:
15734 identifier
15735 operator-function-id
15737 A defect report has been filed about this issue.
15739 A conversion-function-id cannot be a template name because they cannot
15740 be part of a template-id. In fact, looking at this code:
15742 a.operator K<int>()
15744 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15745 It is impossible to call a templated conversion-function-id with an
15746 explicit argument list, since the only allowed template parameter is
15747 the type to which it is converting.
15749 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15750 `template' keyword, in a construction like:
15752 T::template f<3>()
15754 In that case `f' is taken to be a template-name, even though there
15755 is no way of knowing for sure.
15757 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15758 name refers to a set of overloaded functions, at least one of which
15759 is a template, or an IDENTIFIER_NODE with the name of the template,
15760 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15761 names are looked up inside uninstantiated templates. */
15763 static tree
15764 cp_parser_template_name (cp_parser* parser,
15765 bool template_keyword_p,
15766 bool check_dependency_p,
15767 bool is_declaration,
15768 enum tag_types tag_type,
15769 bool *is_identifier)
15771 tree identifier;
15772 tree decl;
15773 cp_token *token = cp_lexer_peek_token (parser->lexer);
15775 /* If the next token is `operator', then we have either an
15776 operator-function-id or a conversion-function-id. */
15777 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15779 /* We don't know whether we're looking at an
15780 operator-function-id or a conversion-function-id. */
15781 cp_parser_parse_tentatively (parser);
15782 /* Try an operator-function-id. */
15783 identifier = cp_parser_operator_function_id (parser);
15784 /* If that didn't work, try a conversion-function-id. */
15785 if (!cp_parser_parse_definitely (parser))
15787 cp_parser_error (parser, "expected template-name");
15788 return error_mark_node;
15791 /* Look for the identifier. */
15792 else
15793 identifier = cp_parser_identifier (parser);
15795 /* If we didn't find an identifier, we don't have a template-id. */
15796 if (identifier == error_mark_node)
15797 return error_mark_node;
15799 /* If the name immediately followed the `template' keyword, then it
15800 is a template-name. However, if the next token is not `<', then
15801 we do not treat it as a template-name, since it is not being used
15802 as part of a template-id. This enables us to handle constructs
15803 like:
15805 template <typename T> struct S { S(); };
15806 template <typename T> S<T>::S();
15808 correctly. We would treat `S' as a template -- if it were `S<T>'
15809 -- but we do not if there is no `<'. */
15811 if (processing_template_decl
15812 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15814 /* In a declaration, in a dependent context, we pretend that the
15815 "template" keyword was present in order to improve error
15816 recovery. For example, given:
15818 template <typename T> void f(T::X<int>);
15820 we want to treat "X<int>" as a template-id. */
15821 if (is_declaration
15822 && !template_keyword_p
15823 && parser->scope && TYPE_P (parser->scope)
15824 && check_dependency_p
15825 && dependent_scope_p (parser->scope)
15826 /* Do not do this for dtors (or ctors), since they never
15827 need the template keyword before their name. */
15828 && !constructor_name_p (identifier, parser->scope))
15830 cp_token_position start = 0;
15832 /* Explain what went wrong. */
15833 error_at (token->location, "non-template %qD used as template",
15834 identifier);
15835 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15836 parser->scope, identifier);
15837 /* If parsing tentatively, find the location of the "<" token. */
15838 if (cp_parser_simulate_error (parser))
15839 start = cp_lexer_token_position (parser->lexer, true);
15840 /* Parse the template arguments so that we can issue error
15841 messages about them. */
15842 cp_lexer_consume_token (parser->lexer);
15843 cp_parser_enclosed_template_argument_list (parser);
15844 /* Skip tokens until we find a good place from which to
15845 continue parsing. */
15846 cp_parser_skip_to_closing_parenthesis (parser,
15847 /*recovering=*/true,
15848 /*or_comma=*/true,
15849 /*consume_paren=*/false);
15850 /* If parsing tentatively, permanently remove the
15851 template argument list. That will prevent duplicate
15852 error messages from being issued about the missing
15853 "template" keyword. */
15854 if (start)
15855 cp_lexer_purge_tokens_after (parser->lexer, start);
15856 if (is_identifier)
15857 *is_identifier = true;
15858 parser->context->object_type = NULL_TREE;
15859 return identifier;
15862 /* If the "template" keyword is present, then there is generally
15863 no point in doing name-lookup, so we just return IDENTIFIER.
15864 But, if the qualifying scope is non-dependent then we can
15865 (and must) do name-lookup normally. */
15866 if (template_keyword_p
15867 && (!parser->scope
15868 || (TYPE_P (parser->scope)
15869 && dependent_type_p (parser->scope))))
15871 /* We're optimizing away the call to cp_parser_lookup_name, but we
15872 still need to do this. */
15873 parser->context->object_type = NULL_TREE;
15874 return identifier;
15878 /* Look up the name. */
15879 decl = cp_parser_lookup_name (parser, identifier,
15880 tag_type,
15881 /*is_template=*/true,
15882 /*is_namespace=*/false,
15883 check_dependency_p,
15884 /*ambiguous_decls=*/NULL,
15885 token->location);
15887 decl = strip_using_decl (decl);
15889 /* If DECL is a template, then the name was a template-name. */
15890 if (TREE_CODE (decl) == TEMPLATE_DECL)
15892 if (TREE_DEPRECATED (decl)
15893 && deprecated_state != DEPRECATED_SUPPRESS)
15894 warn_deprecated_use (decl, NULL_TREE);
15896 else
15898 /* The standard does not explicitly indicate whether a name that
15899 names a set of overloaded declarations, some of which are
15900 templates, is a template-name. However, such a name should
15901 be a template-name; otherwise, there is no way to form a
15902 template-id for the overloaded templates. */
15903 bool found = false;
15905 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15906 !found && iter; ++iter)
15907 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15908 found = true;
15910 if (!found)
15912 /* The name does not name a template. */
15913 cp_parser_error (parser, "expected template-name");
15914 return error_mark_node;
15918 /* If DECL is dependent, and refers to a function, then just return
15919 its name; we will look it up again during template instantiation. */
15920 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15922 tree scope = ovl_scope (decl);
15923 if (TYPE_P (scope) && dependent_type_p (scope))
15924 return identifier;
15927 return decl;
15930 /* Parse a template-argument-list.
15932 template-argument-list:
15933 template-argument ... [opt]
15934 template-argument-list , template-argument ... [opt]
15936 Returns a TREE_VEC containing the arguments. */
15938 static tree
15939 cp_parser_template_argument_list (cp_parser* parser)
15941 tree fixed_args[10];
15942 unsigned n_args = 0;
15943 unsigned alloced = 10;
15944 tree *arg_ary = fixed_args;
15945 tree vec;
15946 bool saved_in_template_argument_list_p;
15947 bool saved_ice_p;
15948 bool saved_non_ice_p;
15950 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15951 parser->in_template_argument_list_p = true;
15952 /* Even if the template-id appears in an integral
15953 constant-expression, the contents of the argument list do
15954 not. */
15955 saved_ice_p = parser->integral_constant_expression_p;
15956 parser->integral_constant_expression_p = false;
15957 saved_non_ice_p = parser->non_integral_constant_expression_p;
15958 parser->non_integral_constant_expression_p = false;
15960 /* Parse the arguments. */
15963 tree argument;
15965 if (n_args)
15966 /* Consume the comma. */
15967 cp_lexer_consume_token (parser->lexer);
15969 /* Parse the template-argument. */
15970 argument = cp_parser_template_argument (parser);
15972 /* If the next token is an ellipsis, we're expanding a template
15973 argument pack. */
15974 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15976 if (argument == error_mark_node)
15978 cp_token *token = cp_lexer_peek_token (parser->lexer);
15979 error_at (token->location,
15980 "expected parameter pack before %<...%>");
15982 /* Consume the `...' token. */
15983 cp_lexer_consume_token (parser->lexer);
15985 /* Make the argument into a TYPE_PACK_EXPANSION or
15986 EXPR_PACK_EXPANSION. */
15987 argument = make_pack_expansion (argument);
15990 if (n_args == alloced)
15992 alloced *= 2;
15994 if (arg_ary == fixed_args)
15996 arg_ary = XNEWVEC (tree, alloced);
15997 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15999 else
16000 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16002 arg_ary[n_args++] = argument;
16004 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16006 vec = make_tree_vec (n_args);
16008 while (n_args--)
16009 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16011 if (arg_ary != fixed_args)
16012 free (arg_ary);
16013 parser->non_integral_constant_expression_p = saved_non_ice_p;
16014 parser->integral_constant_expression_p = saved_ice_p;
16015 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16016 if (CHECKING_P)
16017 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16018 return vec;
16021 /* Parse a template-argument.
16023 template-argument:
16024 assignment-expression
16025 type-id
16026 id-expression
16028 The representation is that of an assignment-expression, type-id, or
16029 id-expression -- except that the qualified id-expression is
16030 evaluated, so that the value returned is either a DECL or an
16031 OVERLOAD.
16033 Although the standard says "assignment-expression", it forbids
16034 throw-expressions or assignments in the template argument.
16035 Therefore, we use "conditional-expression" instead. */
16037 static tree
16038 cp_parser_template_argument (cp_parser* parser)
16040 tree argument;
16041 bool template_p;
16042 bool address_p;
16043 bool maybe_type_id = false;
16044 cp_token *token = NULL, *argument_start_token = NULL;
16045 location_t loc = 0;
16046 cp_id_kind idk;
16048 /* There's really no way to know what we're looking at, so we just
16049 try each alternative in order.
16051 [temp.arg]
16053 In a template-argument, an ambiguity between a type-id and an
16054 expression is resolved to a type-id, regardless of the form of
16055 the corresponding template-parameter.
16057 Therefore, we try a type-id first. */
16058 cp_parser_parse_tentatively (parser);
16059 argument = cp_parser_template_type_arg (parser);
16060 /* If there was no error parsing the type-id but the next token is a
16061 '>>', our behavior depends on which dialect of C++ we're
16062 parsing. In C++98, we probably found a typo for '> >'. But there
16063 are type-id which are also valid expressions. For instance:
16065 struct X { int operator >> (int); };
16066 template <int V> struct Foo {};
16067 Foo<X () >> 5> r;
16069 Here 'X()' is a valid type-id of a function type, but the user just
16070 wanted to write the expression "X() >> 5". Thus, we remember that we
16071 found a valid type-id, but we still try to parse the argument as an
16072 expression to see what happens.
16074 In C++0x, the '>>' will be considered two separate '>'
16075 tokens. */
16076 if (!cp_parser_error_occurred (parser)
16077 && cxx_dialect == cxx98
16078 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16080 maybe_type_id = true;
16081 cp_parser_abort_tentative_parse (parser);
16083 else
16085 /* If the next token isn't a `,' or a `>', then this argument wasn't
16086 really finished. This means that the argument is not a valid
16087 type-id. */
16088 if (!cp_parser_next_token_ends_template_argument_p (parser))
16089 cp_parser_error (parser, "expected template-argument");
16090 /* If that worked, we're done. */
16091 if (cp_parser_parse_definitely (parser))
16092 return argument;
16094 /* We're still not sure what the argument will be. */
16095 cp_parser_parse_tentatively (parser);
16096 /* Try a template. */
16097 argument_start_token = cp_lexer_peek_token (parser->lexer);
16098 argument = cp_parser_id_expression (parser,
16099 /*template_keyword_p=*/false,
16100 /*check_dependency_p=*/true,
16101 &template_p,
16102 /*declarator_p=*/false,
16103 /*optional_p=*/false);
16104 /* If the next token isn't a `,' or a `>', then this argument wasn't
16105 really finished. */
16106 if (!cp_parser_next_token_ends_template_argument_p (parser))
16107 cp_parser_error (parser, "expected template-argument");
16108 if (!cp_parser_error_occurred (parser))
16110 /* Figure out what is being referred to. If the id-expression
16111 was for a class template specialization, then we will have a
16112 TYPE_DECL at this point. There is no need to do name lookup
16113 at this point in that case. */
16114 if (TREE_CODE (argument) != TYPE_DECL)
16115 argument = cp_parser_lookup_name (parser, argument,
16116 none_type,
16117 /*is_template=*/template_p,
16118 /*is_namespace=*/false,
16119 /*check_dependency=*/true,
16120 /*ambiguous_decls=*/NULL,
16121 argument_start_token->location);
16122 /* Handle a constrained-type-specifier for a non-type template
16123 parameter. */
16124 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16125 argument = decl;
16126 else if (TREE_CODE (argument) != TEMPLATE_DECL
16127 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16128 cp_parser_error (parser, "expected template-name");
16130 if (cp_parser_parse_definitely (parser))
16132 if (TREE_DEPRECATED (argument))
16133 warn_deprecated_use (argument, NULL_TREE);
16134 return argument;
16136 /* It must be a non-type argument. In C++17 any constant-expression is
16137 allowed. */
16138 if (cxx_dialect > cxx14)
16139 goto general_expr;
16141 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16143 -- an integral constant-expression of integral or enumeration
16144 type; or
16146 -- the name of a non-type template-parameter; or
16148 -- the name of an object or function with external linkage...
16150 -- the address of an object or function with external linkage...
16152 -- a pointer to member... */
16153 /* Look for a non-type template parameter. */
16154 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16156 cp_parser_parse_tentatively (parser);
16157 argument = cp_parser_primary_expression (parser,
16158 /*address_p=*/false,
16159 /*cast_p=*/false,
16160 /*template_arg_p=*/true,
16161 &idk);
16162 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16163 || !cp_parser_next_token_ends_template_argument_p (parser))
16164 cp_parser_simulate_error (parser);
16165 if (cp_parser_parse_definitely (parser))
16166 return argument;
16169 /* If the next token is "&", the argument must be the address of an
16170 object or function with external linkage. */
16171 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16172 if (address_p)
16174 loc = cp_lexer_peek_token (parser->lexer)->location;
16175 cp_lexer_consume_token (parser->lexer);
16177 /* See if we might have an id-expression. */
16178 token = cp_lexer_peek_token (parser->lexer);
16179 if (token->type == CPP_NAME
16180 || token->keyword == RID_OPERATOR
16181 || token->type == CPP_SCOPE
16182 || token->type == CPP_TEMPLATE_ID
16183 || token->type == CPP_NESTED_NAME_SPECIFIER)
16185 cp_parser_parse_tentatively (parser);
16186 argument = cp_parser_primary_expression (parser,
16187 address_p,
16188 /*cast_p=*/false,
16189 /*template_arg_p=*/true,
16190 &idk);
16191 if (cp_parser_error_occurred (parser)
16192 || !cp_parser_next_token_ends_template_argument_p (parser))
16193 cp_parser_abort_tentative_parse (parser);
16194 else
16196 tree probe;
16198 if (INDIRECT_REF_P (argument))
16200 /* Strip the dereference temporarily. */
16201 gcc_assert (REFERENCE_REF_P (argument));
16202 argument = TREE_OPERAND (argument, 0);
16205 /* If we're in a template, we represent a qualified-id referring
16206 to a static data member as a SCOPE_REF even if the scope isn't
16207 dependent so that we can check access control later. */
16208 probe = argument;
16209 if (TREE_CODE (probe) == SCOPE_REF)
16210 probe = TREE_OPERAND (probe, 1);
16211 if (VAR_P (probe))
16213 /* A variable without external linkage might still be a
16214 valid constant-expression, so no error is issued here
16215 if the external-linkage check fails. */
16216 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16217 cp_parser_simulate_error (parser);
16219 else if (is_overloaded_fn (argument))
16220 /* All overloaded functions are allowed; if the external
16221 linkage test does not pass, an error will be issued
16222 later. */
16224 else if (address_p
16225 && (TREE_CODE (argument) == OFFSET_REF
16226 || TREE_CODE (argument) == SCOPE_REF))
16227 /* A pointer-to-member. */
16229 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16231 else
16232 cp_parser_simulate_error (parser);
16234 if (cp_parser_parse_definitely (parser))
16236 if (address_p)
16237 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16238 tf_warning_or_error);
16239 else
16240 argument = convert_from_reference (argument);
16241 return argument;
16245 /* If the argument started with "&", there are no other valid
16246 alternatives at this point. */
16247 if (address_p)
16249 cp_parser_error (parser, "invalid non-type template argument");
16250 return error_mark_node;
16253 general_expr:
16254 /* If the argument wasn't successfully parsed as a type-id followed
16255 by '>>', the argument can only be a constant expression now.
16256 Otherwise, we try parsing the constant-expression tentatively,
16257 because the argument could really be a type-id. */
16258 if (maybe_type_id)
16259 cp_parser_parse_tentatively (parser);
16261 if (cxx_dialect <= cxx14)
16262 argument = cp_parser_constant_expression (parser);
16263 else
16265 /* With C++17 generalized non-type template arguments we need to handle
16266 lvalue constant expressions, too. */
16267 argument = cp_parser_assignment_expression (parser);
16268 require_potential_constant_expression (argument);
16271 if (!maybe_type_id)
16272 return argument;
16273 if (!cp_parser_next_token_ends_template_argument_p (parser))
16274 cp_parser_error (parser, "expected template-argument");
16275 if (cp_parser_parse_definitely (parser))
16276 return argument;
16277 /* We did our best to parse the argument as a non type-id, but that
16278 was the only alternative that matched (albeit with a '>' after
16279 it). We can assume it's just a typo from the user, and a
16280 diagnostic will then be issued. */
16281 return cp_parser_template_type_arg (parser);
16284 /* Parse an explicit-instantiation.
16286 explicit-instantiation:
16287 template declaration
16289 Although the standard says `declaration', what it really means is:
16291 explicit-instantiation:
16292 template decl-specifier-seq [opt] declarator [opt] ;
16294 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16295 supposed to be allowed. A defect report has been filed about this
16296 issue.
16298 GNU Extension:
16300 explicit-instantiation:
16301 storage-class-specifier template
16302 decl-specifier-seq [opt] declarator [opt] ;
16303 function-specifier template
16304 decl-specifier-seq [opt] declarator [opt] ; */
16306 static void
16307 cp_parser_explicit_instantiation (cp_parser* parser)
16309 int declares_class_or_enum;
16310 cp_decl_specifier_seq decl_specifiers;
16311 tree extension_specifier = NULL_TREE;
16313 timevar_push (TV_TEMPLATE_INST);
16315 /* Look for an (optional) storage-class-specifier or
16316 function-specifier. */
16317 if (cp_parser_allow_gnu_extensions_p (parser))
16319 extension_specifier
16320 = cp_parser_storage_class_specifier_opt (parser);
16321 if (!extension_specifier)
16322 extension_specifier
16323 = cp_parser_function_specifier_opt (parser,
16324 /*decl_specs=*/NULL);
16327 /* Look for the `template' keyword. */
16328 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16329 /* Let the front end know that we are processing an explicit
16330 instantiation. */
16331 begin_explicit_instantiation ();
16332 /* [temp.explicit] says that we are supposed to ignore access
16333 control while processing explicit instantiation directives. */
16334 push_deferring_access_checks (dk_no_check);
16335 /* Parse a decl-specifier-seq. */
16336 cp_parser_decl_specifier_seq (parser,
16337 CP_PARSER_FLAGS_OPTIONAL,
16338 &decl_specifiers,
16339 &declares_class_or_enum);
16340 /* If there was exactly one decl-specifier, and it declared a class,
16341 and there's no declarator, then we have an explicit type
16342 instantiation. */
16343 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16345 tree type;
16347 type = check_tag_decl (&decl_specifiers,
16348 /*explicit_type_instantiation_p=*/true);
16349 /* Turn access control back on for names used during
16350 template instantiation. */
16351 pop_deferring_access_checks ();
16352 if (type)
16353 do_type_instantiation (type, extension_specifier,
16354 /*complain=*/tf_error);
16356 else
16358 cp_declarator *declarator;
16359 tree decl;
16361 /* Parse the declarator. */
16362 declarator
16363 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16364 /*ctor_dtor_or_conv_p=*/NULL,
16365 /*parenthesized_p=*/NULL,
16366 /*member_p=*/false,
16367 /*friend_p=*/false);
16368 if (declares_class_or_enum & 2)
16369 cp_parser_check_for_definition_in_return_type (declarator,
16370 decl_specifiers.type,
16371 decl_specifiers.locations[ds_type_spec]);
16372 if (declarator != cp_error_declarator)
16374 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16375 permerror (decl_specifiers.locations[ds_inline],
16376 "explicit instantiation shall not use"
16377 " %<inline%> specifier");
16378 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16379 permerror (decl_specifiers.locations[ds_constexpr],
16380 "explicit instantiation shall not use"
16381 " %<constexpr%> specifier");
16383 decl = grokdeclarator (declarator, &decl_specifiers,
16384 NORMAL, 0, &decl_specifiers.attributes);
16385 /* Turn access control back on for names used during
16386 template instantiation. */
16387 pop_deferring_access_checks ();
16388 /* Do the explicit instantiation. */
16389 do_decl_instantiation (decl, extension_specifier);
16391 else
16393 pop_deferring_access_checks ();
16394 /* Skip the body of the explicit instantiation. */
16395 cp_parser_skip_to_end_of_statement (parser);
16398 /* We're done with the instantiation. */
16399 end_explicit_instantiation ();
16401 cp_parser_consume_semicolon_at_end_of_statement (parser);
16403 timevar_pop (TV_TEMPLATE_INST);
16406 /* Parse an explicit-specialization.
16408 explicit-specialization:
16409 template < > declaration
16411 Although the standard says `declaration', what it really means is:
16413 explicit-specialization:
16414 template <> decl-specifier [opt] init-declarator [opt] ;
16415 template <> function-definition
16416 template <> explicit-specialization
16417 template <> template-declaration */
16419 static void
16420 cp_parser_explicit_specialization (cp_parser* parser)
16422 bool need_lang_pop;
16423 cp_token *token = cp_lexer_peek_token (parser->lexer);
16425 /* Look for the `template' keyword. */
16426 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16427 /* Look for the `<'. */
16428 cp_parser_require (parser, CPP_LESS, RT_LESS);
16429 /* Look for the `>'. */
16430 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16431 /* We have processed another parameter list. */
16432 ++parser->num_template_parameter_lists;
16433 /* [temp]
16435 A template ... explicit specialization ... shall not have C
16436 linkage. */
16437 if (current_lang_name == lang_name_c)
16439 error_at (token->location, "template specialization with C linkage");
16440 /* Give it C++ linkage to avoid confusing other parts of the
16441 front end. */
16442 push_lang_context (lang_name_cplusplus);
16443 need_lang_pop = true;
16445 else
16446 need_lang_pop = false;
16447 /* Let the front end know that we are beginning a specialization. */
16448 if (!begin_specialization ())
16450 end_specialization ();
16451 return;
16454 /* If the next keyword is `template', we need to figure out whether
16455 or not we're looking a template-declaration. */
16456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16458 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16459 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16460 cp_parser_template_declaration_after_export (parser,
16461 /*member_p=*/false);
16462 else
16463 cp_parser_explicit_specialization (parser);
16465 else
16466 /* Parse the dependent declaration. */
16467 cp_parser_single_declaration (parser,
16468 /*checks=*/NULL,
16469 /*member_p=*/false,
16470 /*explicit_specialization_p=*/true,
16471 /*friend_p=*/NULL);
16472 /* We're done with the specialization. */
16473 end_specialization ();
16474 /* For the erroneous case of a template with C linkage, we pushed an
16475 implicit C++ linkage scope; exit that scope now. */
16476 if (need_lang_pop)
16477 pop_lang_context ();
16478 /* We're done with this parameter list. */
16479 --parser->num_template_parameter_lists;
16482 /* Parse a type-specifier.
16484 type-specifier:
16485 simple-type-specifier
16486 class-specifier
16487 enum-specifier
16488 elaborated-type-specifier
16489 cv-qualifier
16491 GNU Extension:
16493 type-specifier:
16494 __complex__
16496 Returns a representation of the type-specifier. For a
16497 class-specifier, enum-specifier, or elaborated-type-specifier, a
16498 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16500 The parser flags FLAGS is used to control type-specifier parsing.
16502 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16503 in a decl-specifier-seq.
16505 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16506 class-specifier, enum-specifier, or elaborated-type-specifier, then
16507 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16508 if a type is declared; 2 if it is defined. Otherwise, it is set to
16509 zero.
16511 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16512 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16513 is set to FALSE. */
16515 static tree
16516 cp_parser_type_specifier (cp_parser* parser,
16517 cp_parser_flags flags,
16518 cp_decl_specifier_seq *decl_specs,
16519 bool is_declaration,
16520 int* declares_class_or_enum,
16521 bool* is_cv_qualifier)
16523 tree type_spec = NULL_TREE;
16524 cp_token *token;
16525 enum rid keyword;
16526 cp_decl_spec ds = ds_last;
16528 /* Assume this type-specifier does not declare a new type. */
16529 if (declares_class_or_enum)
16530 *declares_class_or_enum = 0;
16531 /* And that it does not specify a cv-qualifier. */
16532 if (is_cv_qualifier)
16533 *is_cv_qualifier = false;
16534 /* Peek at the next token. */
16535 token = cp_lexer_peek_token (parser->lexer);
16537 /* If we're looking at a keyword, we can use that to guide the
16538 production we choose. */
16539 keyword = token->keyword;
16540 switch (keyword)
16542 case RID_ENUM:
16543 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16544 goto elaborated_type_specifier;
16546 /* Look for the enum-specifier. */
16547 type_spec = cp_parser_enum_specifier (parser);
16548 /* If that worked, we're done. */
16549 if (type_spec)
16551 if (declares_class_or_enum)
16552 *declares_class_or_enum = 2;
16553 if (decl_specs)
16554 cp_parser_set_decl_spec_type (decl_specs,
16555 type_spec,
16556 token,
16557 /*type_definition_p=*/true);
16558 return type_spec;
16560 else
16561 goto elaborated_type_specifier;
16563 /* Any of these indicate either a class-specifier, or an
16564 elaborated-type-specifier. */
16565 case RID_CLASS:
16566 case RID_STRUCT:
16567 case RID_UNION:
16568 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16569 goto elaborated_type_specifier;
16571 /* Parse tentatively so that we can back up if we don't find a
16572 class-specifier. */
16573 cp_parser_parse_tentatively (parser);
16574 /* Look for the class-specifier. */
16575 type_spec = cp_parser_class_specifier (parser);
16576 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16577 /* If that worked, we're done. */
16578 if (cp_parser_parse_definitely (parser))
16580 if (declares_class_or_enum)
16581 *declares_class_or_enum = 2;
16582 if (decl_specs)
16583 cp_parser_set_decl_spec_type (decl_specs,
16584 type_spec,
16585 token,
16586 /*type_definition_p=*/true);
16587 return type_spec;
16590 /* Fall through. */
16591 elaborated_type_specifier:
16592 /* We're declaring (not defining) a class or enum. */
16593 if (declares_class_or_enum)
16594 *declares_class_or_enum = 1;
16596 /* Fall through. */
16597 case RID_TYPENAME:
16598 /* Look for an elaborated-type-specifier. */
16599 type_spec
16600 = (cp_parser_elaborated_type_specifier
16601 (parser,
16602 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16603 is_declaration));
16604 if (decl_specs)
16605 cp_parser_set_decl_spec_type (decl_specs,
16606 type_spec,
16607 token,
16608 /*type_definition_p=*/false);
16609 return type_spec;
16611 case RID_CONST:
16612 ds = ds_const;
16613 if (is_cv_qualifier)
16614 *is_cv_qualifier = true;
16615 break;
16617 case RID_VOLATILE:
16618 ds = ds_volatile;
16619 if (is_cv_qualifier)
16620 *is_cv_qualifier = true;
16621 break;
16623 case RID_RESTRICT:
16624 ds = ds_restrict;
16625 if (is_cv_qualifier)
16626 *is_cv_qualifier = true;
16627 break;
16629 case RID_COMPLEX:
16630 /* The `__complex__' keyword is a GNU extension. */
16631 ds = ds_complex;
16632 break;
16634 default:
16635 break;
16638 /* Handle simple keywords. */
16639 if (ds != ds_last)
16641 if (decl_specs)
16643 set_and_check_decl_spec_loc (decl_specs, ds, token);
16644 decl_specs->any_specifiers_p = true;
16646 return cp_lexer_consume_token (parser->lexer)->u.value;
16649 /* If we do not already have a type-specifier, assume we are looking
16650 at a simple-type-specifier. */
16651 type_spec = cp_parser_simple_type_specifier (parser,
16652 decl_specs,
16653 flags);
16655 /* If we didn't find a type-specifier, and a type-specifier was not
16656 optional in this context, issue an error message. */
16657 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16659 cp_parser_error (parser, "expected type specifier");
16660 return error_mark_node;
16663 return type_spec;
16666 /* Parse a simple-type-specifier.
16668 simple-type-specifier:
16669 :: [opt] nested-name-specifier [opt] type-name
16670 :: [opt] nested-name-specifier template template-id
16671 char
16672 wchar_t
16673 bool
16674 short
16676 long
16677 signed
16678 unsigned
16679 float
16680 double
16681 void
16683 C++11 Extension:
16685 simple-type-specifier:
16686 auto
16687 decltype ( expression )
16688 char16_t
16689 char32_t
16690 __underlying_type ( type-id )
16692 C++17 extension:
16694 nested-name-specifier(opt) template-name
16696 GNU Extension:
16698 simple-type-specifier:
16699 __int128
16700 __typeof__ unary-expression
16701 __typeof__ ( type-id )
16702 __typeof__ ( type-id ) { initializer-list , [opt] }
16704 Concepts Extension:
16706 simple-type-specifier:
16707 constrained-type-specifier
16709 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16710 appropriately updated. */
16712 static tree
16713 cp_parser_simple_type_specifier (cp_parser* parser,
16714 cp_decl_specifier_seq *decl_specs,
16715 cp_parser_flags flags)
16717 tree type = NULL_TREE;
16718 cp_token *token;
16719 int idx;
16721 /* Peek at the next token. */
16722 token = cp_lexer_peek_token (parser->lexer);
16724 /* If we're looking at a keyword, things are easy. */
16725 switch (token->keyword)
16727 case RID_CHAR:
16728 if (decl_specs)
16729 decl_specs->explicit_char_p = true;
16730 type = char_type_node;
16731 break;
16732 case RID_CHAR16:
16733 type = char16_type_node;
16734 break;
16735 case RID_CHAR32:
16736 type = char32_type_node;
16737 break;
16738 case RID_WCHAR:
16739 type = wchar_type_node;
16740 break;
16741 case RID_BOOL:
16742 type = boolean_type_node;
16743 break;
16744 case RID_SHORT:
16745 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16746 type = short_integer_type_node;
16747 break;
16748 case RID_INT:
16749 if (decl_specs)
16750 decl_specs->explicit_int_p = true;
16751 type = integer_type_node;
16752 break;
16753 case RID_INT_N_0:
16754 case RID_INT_N_1:
16755 case RID_INT_N_2:
16756 case RID_INT_N_3:
16757 idx = token->keyword - RID_INT_N_0;
16758 if (! int_n_enabled_p [idx])
16759 break;
16760 if (decl_specs)
16762 decl_specs->explicit_intN_p = true;
16763 decl_specs->int_n_idx = idx;
16765 type = int_n_trees [idx].signed_type;
16766 break;
16767 case RID_LONG:
16768 if (decl_specs)
16769 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16770 type = long_integer_type_node;
16771 break;
16772 case RID_SIGNED:
16773 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16774 type = integer_type_node;
16775 break;
16776 case RID_UNSIGNED:
16777 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16778 type = unsigned_type_node;
16779 break;
16780 case RID_FLOAT:
16781 type = float_type_node;
16782 break;
16783 case RID_DOUBLE:
16784 type = double_type_node;
16785 break;
16786 case RID_VOID:
16787 type = void_type_node;
16788 break;
16790 case RID_AUTO:
16791 maybe_warn_cpp0x (CPP0X_AUTO);
16792 if (parser->auto_is_implicit_function_template_parm_p)
16794 /* The 'auto' might be the placeholder return type for a function decl
16795 with trailing return type. */
16796 bool have_trailing_return_fn_decl = false;
16798 cp_parser_parse_tentatively (parser);
16799 cp_lexer_consume_token (parser->lexer);
16800 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16801 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16802 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16803 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16805 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16807 cp_lexer_consume_token (parser->lexer);
16808 cp_parser_skip_to_closing_parenthesis (parser,
16809 /*recovering*/false,
16810 /*or_comma*/false,
16811 /*consume_paren*/true);
16812 continue;
16815 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16817 have_trailing_return_fn_decl = true;
16818 break;
16821 cp_lexer_consume_token (parser->lexer);
16823 cp_parser_abort_tentative_parse (parser);
16825 if (have_trailing_return_fn_decl)
16827 type = make_auto ();
16828 break;
16831 if (cxx_dialect >= cxx14)
16833 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16834 type = TREE_TYPE (type);
16836 else
16837 type = error_mark_node;
16839 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16841 if (cxx_dialect < cxx14)
16842 error_at (token->location,
16843 "use of %<auto%> in lambda parameter declaration "
16844 "only available with "
16845 "-std=c++14 or -std=gnu++14");
16847 else if (cxx_dialect < cxx14)
16848 error_at (token->location,
16849 "use of %<auto%> in parameter declaration "
16850 "only available with "
16851 "-std=c++14 or -std=gnu++14");
16852 else if (!flag_concepts)
16853 pedwarn (token->location, OPT_Wpedantic,
16854 "ISO C++ forbids use of %<auto%> in parameter "
16855 "declaration");
16857 else
16858 type = make_auto ();
16859 break;
16861 case RID_DECLTYPE:
16862 /* Since DR 743, decltype can either be a simple-type-specifier by
16863 itself or begin a nested-name-specifier. Parsing it will replace
16864 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16865 handling below decide what to do. */
16866 cp_parser_decltype (parser);
16867 cp_lexer_set_token_position (parser->lexer, token);
16868 break;
16870 case RID_TYPEOF:
16871 /* Consume the `typeof' token. */
16872 cp_lexer_consume_token (parser->lexer);
16873 /* Parse the operand to `typeof'. */
16874 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16875 /* If it is not already a TYPE, take its type. */
16876 if (!TYPE_P (type))
16877 type = finish_typeof (type);
16879 if (decl_specs)
16880 cp_parser_set_decl_spec_type (decl_specs, type,
16881 token,
16882 /*type_definition_p=*/false);
16884 return type;
16886 case RID_UNDERLYING_TYPE:
16887 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16888 if (decl_specs)
16889 cp_parser_set_decl_spec_type (decl_specs, type,
16890 token,
16891 /*type_definition_p=*/false);
16893 return type;
16895 case RID_BASES:
16896 case RID_DIRECT_BASES:
16897 type = cp_parser_trait_expr (parser, token->keyword);
16898 if (decl_specs)
16899 cp_parser_set_decl_spec_type (decl_specs, type,
16900 token,
16901 /*type_definition_p=*/false);
16902 return type;
16903 default:
16904 break;
16907 /* If token is an already-parsed decltype not followed by ::,
16908 it's a simple-type-specifier. */
16909 if (token->type == CPP_DECLTYPE
16910 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16912 type = saved_checks_value (token->u.tree_check_value);
16913 if (decl_specs)
16915 cp_parser_set_decl_spec_type (decl_specs, type,
16916 token,
16917 /*type_definition_p=*/false);
16918 /* Remember that we are handling a decltype in order to
16919 implement the resolution of DR 1510 when the argument
16920 isn't instantiation dependent. */
16921 decl_specs->decltype_p = true;
16923 cp_lexer_consume_token (parser->lexer);
16924 return type;
16927 /* If the type-specifier was for a built-in type, we're done. */
16928 if (type)
16930 /* Record the type. */
16931 if (decl_specs
16932 && (token->keyword != RID_SIGNED
16933 && token->keyword != RID_UNSIGNED
16934 && token->keyword != RID_SHORT
16935 && token->keyword != RID_LONG))
16936 cp_parser_set_decl_spec_type (decl_specs,
16937 type,
16938 token,
16939 /*type_definition_p=*/false);
16940 if (decl_specs)
16941 decl_specs->any_specifiers_p = true;
16943 /* Consume the token. */
16944 cp_lexer_consume_token (parser->lexer);
16946 if (type == error_mark_node)
16947 return error_mark_node;
16949 /* There is no valid C++ program where a non-template type is
16950 followed by a "<". That usually indicates that the user thought
16951 that the type was a template. */
16952 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16953 token->location);
16955 return TYPE_NAME (type);
16958 /* The type-specifier must be a user-defined type. */
16959 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16961 bool qualified_p;
16962 bool global_p;
16964 /* Don't gobble tokens or issue error messages if this is an
16965 optional type-specifier. */
16966 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16967 cp_parser_parse_tentatively (parser);
16969 token = cp_lexer_peek_token (parser->lexer);
16971 /* Look for the optional `::' operator. */
16972 global_p
16973 = (cp_parser_global_scope_opt (parser,
16974 /*current_scope_valid_p=*/false)
16975 != NULL_TREE);
16976 /* Look for the nested-name specifier. */
16977 qualified_p
16978 = (cp_parser_nested_name_specifier_opt (parser,
16979 /*typename_keyword_p=*/false,
16980 /*check_dependency_p=*/true,
16981 /*type_p=*/false,
16982 /*is_declaration=*/false)
16983 != NULL_TREE);
16984 /* If we have seen a nested-name-specifier, and the next token
16985 is `template', then we are using the template-id production. */
16986 if (parser->scope
16987 && cp_parser_optional_template_keyword (parser))
16989 /* Look for the template-id. */
16990 type = cp_parser_template_id (parser,
16991 /*template_keyword_p=*/true,
16992 /*check_dependency_p=*/true,
16993 none_type,
16994 /*is_declaration=*/false);
16995 /* If the template-id did not name a type, we are out of
16996 luck. */
16997 if (TREE_CODE (type) != TYPE_DECL)
16999 cp_parser_error (parser, "expected template-id for type");
17000 type = NULL_TREE;
17003 /* Otherwise, look for a type-name. */
17004 else
17005 type = cp_parser_type_name (parser);
17006 /* Keep track of all name-lookups performed in class scopes. */
17007 if (type
17008 && !global_p
17009 && !qualified_p
17010 && TREE_CODE (type) == TYPE_DECL
17011 && identifier_p (DECL_NAME (type)))
17012 maybe_note_name_used_in_class (DECL_NAME (type), type);
17013 /* If it didn't work out, we don't have a TYPE. */
17014 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17015 && !cp_parser_parse_definitely (parser))
17016 type = NULL_TREE;
17017 if (!type && cxx_dialect >= cxx1z)
17019 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17020 cp_parser_parse_tentatively (parser);
17022 cp_parser_global_scope_opt (parser,
17023 /*current_scope_valid_p=*/false);
17024 cp_parser_nested_name_specifier_opt (parser,
17025 /*typename_keyword_p=*/false,
17026 /*check_dependency_p=*/true,
17027 /*type_p=*/false,
17028 /*is_declaration=*/false);
17029 tree name = cp_parser_identifier (parser);
17030 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17031 && parser->scope != error_mark_node)
17033 tree tmpl = cp_parser_lookup_name (parser, name,
17034 none_type,
17035 /*is_template=*/false,
17036 /*is_namespace=*/false,
17037 /*check_dependency=*/true,
17038 /*ambiguous_decls=*/NULL,
17039 token->location);
17040 if (tmpl && tmpl != error_mark_node
17041 && (DECL_CLASS_TEMPLATE_P (tmpl)
17042 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17043 type = make_template_placeholder (tmpl);
17044 else
17046 type = error_mark_node;
17047 if (!cp_parser_simulate_error (parser))
17048 cp_parser_name_lookup_error (parser, name, tmpl,
17049 NLE_TYPE, token->location);
17052 else
17053 type = error_mark_node;
17055 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17056 && !cp_parser_parse_definitely (parser))
17057 type = NULL_TREE;
17059 if (type && decl_specs)
17060 cp_parser_set_decl_spec_type (decl_specs, type,
17061 token,
17062 /*type_definition_p=*/false);
17065 /* If we didn't get a type-name, issue an error message. */
17066 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17068 cp_parser_error (parser, "expected type-name");
17069 return error_mark_node;
17072 if (type && type != error_mark_node)
17074 /* See if TYPE is an Objective-C type, and if so, parse and
17075 accept any protocol references following it. Do this before
17076 the cp_parser_check_for_invalid_template_id() call, because
17077 Objective-C types can be followed by '<...>' which would
17078 enclose protocol names rather than template arguments, and so
17079 everything is fine. */
17080 if (c_dialect_objc () && !parser->scope
17081 && (objc_is_id (type) || objc_is_class_name (type)))
17083 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17084 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17086 /* Clobber the "unqualified" type previously entered into
17087 DECL_SPECS with the new, improved protocol-qualified version. */
17088 if (decl_specs)
17089 decl_specs->type = qual_type;
17091 return qual_type;
17094 /* There is no valid C++ program where a non-template type is
17095 followed by a "<". That usually indicates that the user
17096 thought that the type was a template. */
17097 cp_parser_check_for_invalid_template_id (parser, type,
17098 none_type,
17099 token->location);
17102 return type;
17105 /* Parse a type-name.
17107 type-name:
17108 class-name
17109 enum-name
17110 typedef-name
17111 simple-template-id [in c++0x]
17113 enum-name:
17114 identifier
17116 typedef-name:
17117 identifier
17119 Concepts:
17121 type-name:
17122 concept-name
17123 partial-concept-id
17125 concept-name:
17126 identifier
17128 Returns a TYPE_DECL for the type. */
17130 static tree
17131 cp_parser_type_name (cp_parser* parser)
17133 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17136 /* See above. */
17137 static tree
17138 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17140 tree type_decl;
17142 /* We can't know yet whether it is a class-name or not. */
17143 cp_parser_parse_tentatively (parser);
17144 /* Try a class-name. */
17145 type_decl = cp_parser_class_name (parser,
17146 typename_keyword_p,
17147 /*template_keyword_p=*/false,
17148 none_type,
17149 /*check_dependency_p=*/true,
17150 /*class_head_p=*/false,
17151 /*is_declaration=*/false);
17152 /* If it's not a class-name, keep looking. */
17153 if (!cp_parser_parse_definitely (parser))
17155 if (cxx_dialect < cxx11)
17156 /* It must be a typedef-name or an enum-name. */
17157 return cp_parser_nonclass_name (parser);
17159 cp_parser_parse_tentatively (parser);
17160 /* It is either a simple-template-id representing an
17161 instantiation of an alias template... */
17162 type_decl = cp_parser_template_id (parser,
17163 /*template_keyword_p=*/false,
17164 /*check_dependency_p=*/true,
17165 none_type,
17166 /*is_declaration=*/false);
17167 /* Note that this must be an instantiation of an alias template
17168 because [temp.names]/6 says:
17170 A template-id that names an alias template specialization
17171 is a type-name.
17173 Whereas [temp.names]/7 says:
17175 A simple-template-id that names a class template
17176 specialization is a class-name.
17178 With concepts, this could also be a partial-concept-id that
17179 declares a non-type template parameter. */
17180 if (type_decl != NULL_TREE
17181 && TREE_CODE (type_decl) == TYPE_DECL
17182 && TYPE_DECL_ALIAS_P (type_decl))
17183 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17184 else if (is_constrained_parameter (type_decl))
17185 /* Don't do anything. */ ;
17186 else
17187 cp_parser_simulate_error (parser);
17189 if (!cp_parser_parse_definitely (parser))
17190 /* ... Or a typedef-name or an enum-name. */
17191 return cp_parser_nonclass_name (parser);
17194 return type_decl;
17197 /* Check if DECL and ARGS can form a constrained-type-specifier.
17198 If ARGS is non-null, we try to form a concept check of the
17199 form DECL<?, ARGS> where ? is a wildcard that matches any
17200 kind of template argument. If ARGS is NULL, then we try to
17201 form a concept check of the form DECL<?>. */
17203 static tree
17204 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17205 tree decl, tree args)
17207 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17209 /* If we a constrained-type-specifier cannot be deduced. */
17210 if (parser->prevent_constrained_type_specifiers)
17211 return NULL_TREE;
17213 /* A constrained type specifier can only be found in an
17214 overload set or as a reference to a template declaration.
17216 FIXME: This might be masking a bug. It's possible that
17217 that the deduction below is causing template specializations
17218 to be formed with the wildcard as an argument. */
17219 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17220 return NULL_TREE;
17222 /* Try to build a call expression that evaluates the
17223 concept. This can fail if the overload set refers
17224 only to non-templates. */
17225 tree placeholder = build_nt (WILDCARD_DECL);
17226 tree check = build_concept_check (decl, placeholder, args);
17227 if (check == error_mark_node)
17228 return NULL_TREE;
17230 /* Deduce the checked constraint and the prototype parameter.
17232 FIXME: In certain cases, failure to deduce should be a
17233 diagnosable error. */
17234 tree conc;
17235 tree proto;
17236 if (!deduce_constrained_parameter (check, conc, proto))
17237 return NULL_TREE;
17239 /* In template parameter scope, this results in a constrained
17240 parameter. Return a descriptor of that parm. */
17241 if (processing_template_parmlist)
17242 return build_constrained_parameter (conc, proto, args);
17244 /* In a parameter-declaration-clause, constrained-type
17245 specifiers result in invented template parameters. */
17246 if (parser->auto_is_implicit_function_template_parm_p)
17248 tree x = build_constrained_parameter (conc, proto, args);
17249 return synthesize_implicit_template_parm (parser, x);
17251 else
17253 /* Otherwise, we're in a context where the constrained
17254 type name is deduced and the constraint applies
17255 after deduction. */
17256 return make_constrained_auto (conc, args);
17259 return NULL_TREE;
17262 /* If DECL refers to a concept, return a TYPE_DECL representing
17263 the result of using the constrained type specifier in the
17264 current context. DECL refers to a concept if
17266 - it is an overload set containing a function concept taking a single
17267 type argument, or
17269 - it is a variable concept taking a single type argument. */
17271 static tree
17272 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17274 if (flag_concepts
17275 && (TREE_CODE (decl) == OVERLOAD
17276 || BASELINK_P (decl)
17277 || variable_concept_p (decl)))
17278 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17279 else
17280 return NULL_TREE;
17283 /* Check if DECL and ARGS form a partial-concept-id. If so,
17284 assign ID to the resulting constrained placeholder.
17286 Returns true if the partial-concept-id designates a placeholder
17287 and false otherwise. Note that *id is set to NULL_TREE in
17288 this case. */
17290 static tree
17291 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17293 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17296 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17297 or a concept-name.
17299 enum-name:
17300 identifier
17302 typedef-name:
17303 identifier
17305 concept-name:
17306 identifier
17308 Returns a TYPE_DECL for the type. */
17310 static tree
17311 cp_parser_nonclass_name (cp_parser* parser)
17313 tree type_decl;
17314 tree identifier;
17316 cp_token *token = cp_lexer_peek_token (parser->lexer);
17317 identifier = cp_parser_identifier (parser);
17318 if (identifier == error_mark_node)
17319 return error_mark_node;
17321 /* Look up the type-name. */
17322 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17324 type_decl = strip_using_decl (type_decl);
17326 /* If we found an overload set, then it may refer to a concept-name. */
17327 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17328 type_decl = decl;
17330 if (TREE_CODE (type_decl) != TYPE_DECL
17331 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17333 /* See if this is an Objective-C type. */
17334 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17335 tree type = objc_get_protocol_qualified_type (identifier, protos);
17336 if (type)
17337 type_decl = TYPE_NAME (type);
17340 /* Issue an error if we did not find a type-name. */
17341 if (TREE_CODE (type_decl) != TYPE_DECL
17342 /* In Objective-C, we have the complication that class names are
17343 normally type names and start declarations (eg, the
17344 "NSObject" in "NSObject *object;"), but can be used in an
17345 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17346 is an expression. So, a classname followed by a dot is not a
17347 valid type-name. */
17348 || (objc_is_class_name (TREE_TYPE (type_decl))
17349 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17351 if (!cp_parser_simulate_error (parser))
17352 cp_parser_name_lookup_error (parser, identifier, type_decl,
17353 NLE_TYPE, token->location);
17354 return error_mark_node;
17356 /* Remember that the name was used in the definition of the
17357 current class so that we can check later to see if the
17358 meaning would have been different after the class was
17359 entirely defined. */
17360 else if (type_decl != error_mark_node
17361 && !parser->scope)
17362 maybe_note_name_used_in_class (identifier, type_decl);
17364 return type_decl;
17367 /* Parse an elaborated-type-specifier. Note that the grammar given
17368 here incorporates the resolution to DR68.
17370 elaborated-type-specifier:
17371 class-key :: [opt] nested-name-specifier [opt] identifier
17372 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17373 enum-key :: [opt] nested-name-specifier [opt] identifier
17374 typename :: [opt] nested-name-specifier identifier
17375 typename :: [opt] nested-name-specifier template [opt]
17376 template-id
17378 GNU extension:
17380 elaborated-type-specifier:
17381 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17382 class-key attributes :: [opt] nested-name-specifier [opt]
17383 template [opt] template-id
17384 enum attributes :: [opt] nested-name-specifier [opt] identifier
17386 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17387 declared `friend'. If IS_DECLARATION is TRUE, then this
17388 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17389 something is being declared.
17391 Returns the TYPE specified. */
17393 static tree
17394 cp_parser_elaborated_type_specifier (cp_parser* parser,
17395 bool is_friend,
17396 bool is_declaration)
17398 enum tag_types tag_type;
17399 tree identifier;
17400 tree type = NULL_TREE;
17401 tree attributes = NULL_TREE;
17402 tree globalscope;
17403 cp_token *token = NULL;
17405 /* See if we're looking at the `enum' keyword. */
17406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17408 /* Consume the `enum' token. */
17409 cp_lexer_consume_token (parser->lexer);
17410 /* Remember that it's an enumeration type. */
17411 tag_type = enum_type;
17412 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17413 enums) is used here. */
17414 cp_token *token = cp_lexer_peek_token (parser->lexer);
17415 if (cp_parser_is_keyword (token, RID_CLASS)
17416 || cp_parser_is_keyword (token, RID_STRUCT))
17418 gcc_rich_location richloc (token->location);
17419 richloc.add_range (input_location, false);
17420 richloc.add_fixit_remove ();
17421 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17422 "a scoped enum must not use the %qD keyword",
17423 token->u.value);
17424 /* Consume the `struct' or `class' and parse it anyway. */
17425 cp_lexer_consume_token (parser->lexer);
17427 /* Parse the attributes. */
17428 attributes = cp_parser_attributes_opt (parser);
17430 /* Or, it might be `typename'. */
17431 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17432 RID_TYPENAME))
17434 /* Consume the `typename' token. */
17435 cp_lexer_consume_token (parser->lexer);
17436 /* Remember that it's a `typename' type. */
17437 tag_type = typename_type;
17439 /* Otherwise it must be a class-key. */
17440 else
17442 tag_type = cp_parser_class_key (parser);
17443 if (tag_type == none_type)
17444 return error_mark_node;
17445 /* Parse the attributes. */
17446 attributes = cp_parser_attributes_opt (parser);
17449 /* Look for the `::' operator. */
17450 globalscope = cp_parser_global_scope_opt (parser,
17451 /*current_scope_valid_p=*/false);
17452 /* Look for the nested-name-specifier. */
17453 tree nested_name_specifier;
17454 if (tag_type == typename_type && !globalscope)
17456 nested_name_specifier
17457 = cp_parser_nested_name_specifier (parser,
17458 /*typename_keyword_p=*/true,
17459 /*check_dependency_p=*/true,
17460 /*type_p=*/true,
17461 is_declaration);
17462 if (!nested_name_specifier)
17463 return error_mark_node;
17465 else
17466 /* Even though `typename' is not present, the proposed resolution
17467 to Core Issue 180 says that in `class A<T>::B', `B' should be
17468 considered a type-name, even if `A<T>' is dependent. */
17469 nested_name_specifier
17470 = cp_parser_nested_name_specifier_opt (parser,
17471 /*typename_keyword_p=*/true,
17472 /*check_dependency_p=*/true,
17473 /*type_p=*/true,
17474 is_declaration);
17475 /* For everything but enumeration types, consider a template-id.
17476 For an enumeration type, consider only a plain identifier. */
17477 if (tag_type != enum_type)
17479 bool template_p = false;
17480 tree decl;
17482 /* Allow the `template' keyword. */
17483 template_p = cp_parser_optional_template_keyword (parser);
17484 /* If we didn't see `template', we don't know if there's a
17485 template-id or not. */
17486 if (!template_p)
17487 cp_parser_parse_tentatively (parser);
17488 /* Parse the template-id. */
17489 token = cp_lexer_peek_token (parser->lexer);
17490 decl = cp_parser_template_id (parser, template_p,
17491 /*check_dependency_p=*/true,
17492 tag_type,
17493 is_declaration);
17494 /* If we didn't find a template-id, look for an ordinary
17495 identifier. */
17496 if (!template_p && !cp_parser_parse_definitely (parser))
17498 /* We can get here when cp_parser_template_id, called by
17499 cp_parser_class_name with tag_type == none_type, succeeds
17500 and caches a BASELINK. Then, when called again here,
17501 instead of failing and returning an error_mark_node
17502 returns it (see template/typename17.C in C++11).
17503 ??? Could we diagnose this earlier? */
17504 else if (tag_type == typename_type && BASELINK_P (decl))
17506 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17507 type = error_mark_node;
17509 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17510 in effect, then we must assume that, upon instantiation, the
17511 template will correspond to a class. */
17512 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17513 && tag_type == typename_type)
17514 type = make_typename_type (parser->scope, decl,
17515 typename_type,
17516 /*complain=*/tf_error);
17517 /* If the `typename' keyword is in effect and DECL is not a type
17518 decl, then type is non existent. */
17519 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17521 else if (TREE_CODE (decl) == TYPE_DECL)
17523 type = check_elaborated_type_specifier (tag_type, decl,
17524 /*allow_template_p=*/true);
17526 /* If the next token is a semicolon, this must be a specialization,
17527 instantiation, or friend declaration. Check the scope while we
17528 still know whether or not we had a nested-name-specifier. */
17529 if (type != error_mark_node
17530 && !nested_name_specifier && !is_friend
17531 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17532 check_unqualified_spec_or_inst (type, token->location);
17534 else if (decl == error_mark_node)
17535 type = error_mark_node;
17538 if (!type)
17540 token = cp_lexer_peek_token (parser->lexer);
17541 identifier = cp_parser_identifier (parser);
17543 if (identifier == error_mark_node)
17545 parser->scope = NULL_TREE;
17546 return error_mark_node;
17549 /* For a `typename', we needn't call xref_tag. */
17550 if (tag_type == typename_type
17551 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17552 return cp_parser_make_typename_type (parser, identifier,
17553 token->location);
17555 /* Template parameter lists apply only if we are not within a
17556 function parameter list. */
17557 bool template_parm_lists_apply
17558 = parser->num_template_parameter_lists;
17559 if (template_parm_lists_apply)
17560 for (cp_binding_level *s = current_binding_level;
17561 s && s->kind != sk_template_parms;
17562 s = s->level_chain)
17563 if (s->kind == sk_function_parms)
17564 template_parm_lists_apply = false;
17566 /* Look up a qualified name in the usual way. */
17567 if (parser->scope)
17569 tree decl;
17570 tree ambiguous_decls;
17572 decl = cp_parser_lookup_name (parser, identifier,
17573 tag_type,
17574 /*is_template=*/false,
17575 /*is_namespace=*/false,
17576 /*check_dependency=*/true,
17577 &ambiguous_decls,
17578 token->location);
17580 /* If the lookup was ambiguous, an error will already have been
17581 issued. */
17582 if (ambiguous_decls)
17583 return error_mark_node;
17585 /* If we are parsing friend declaration, DECL may be a
17586 TEMPLATE_DECL tree node here. However, we need to check
17587 whether this TEMPLATE_DECL results in valid code. Consider
17588 the following example:
17590 namespace N {
17591 template <class T> class C {};
17593 class X {
17594 template <class T> friend class N::C; // #1, valid code
17596 template <class T> class Y {
17597 friend class N::C; // #2, invalid code
17600 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17601 name lookup of `N::C'. We see that friend declaration must
17602 be template for the code to be valid. Note that
17603 processing_template_decl does not work here since it is
17604 always 1 for the above two cases. */
17606 decl = (cp_parser_maybe_treat_template_as_class
17607 (decl, /*tag_name_p=*/is_friend
17608 && template_parm_lists_apply));
17610 if (TREE_CODE (decl) != TYPE_DECL)
17612 cp_parser_diagnose_invalid_type_name (parser,
17613 identifier,
17614 token->location);
17615 return error_mark_node;
17618 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17620 bool allow_template = (template_parm_lists_apply
17621 || DECL_SELF_REFERENCE_P (decl));
17622 type = check_elaborated_type_specifier (tag_type, decl,
17623 allow_template);
17625 if (type == error_mark_node)
17626 return error_mark_node;
17629 /* Forward declarations of nested types, such as
17631 class C1::C2;
17632 class C1::C2::C3;
17634 are invalid unless all components preceding the final '::'
17635 are complete. If all enclosing types are complete, these
17636 declarations become merely pointless.
17638 Invalid forward declarations of nested types are errors
17639 caught elsewhere in parsing. Those that are pointless arrive
17640 here. */
17642 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17643 && !is_friend && !processing_explicit_instantiation)
17644 warning (0, "declaration %qD does not declare anything", decl);
17646 type = TREE_TYPE (decl);
17648 else
17650 /* An elaborated-type-specifier sometimes introduces a new type and
17651 sometimes names an existing type. Normally, the rule is that it
17652 introduces a new type only if there is not an existing type of
17653 the same name already in scope. For example, given:
17655 struct S {};
17656 void f() { struct S s; }
17658 the `struct S' in the body of `f' is the same `struct S' as in
17659 the global scope; the existing definition is used. However, if
17660 there were no global declaration, this would introduce a new
17661 local class named `S'.
17663 An exception to this rule applies to the following code:
17665 namespace N { struct S; }
17667 Here, the elaborated-type-specifier names a new type
17668 unconditionally; even if there is already an `S' in the
17669 containing scope this declaration names a new type.
17670 This exception only applies if the elaborated-type-specifier
17671 forms the complete declaration:
17673 [class.name]
17675 A declaration consisting solely of `class-key identifier ;' is
17676 either a redeclaration of the name in the current scope or a
17677 forward declaration of the identifier as a class name. It
17678 introduces the name into the current scope.
17680 We are in this situation precisely when the next token is a `;'.
17682 An exception to the exception is that a `friend' declaration does
17683 *not* name a new type; i.e., given:
17685 struct S { friend struct T; };
17687 `T' is not a new type in the scope of `S'.
17689 Also, `new struct S' or `sizeof (struct S)' never results in the
17690 definition of a new type; a new type can only be declared in a
17691 declaration context. */
17693 tag_scope ts;
17694 bool template_p;
17696 if (is_friend)
17697 /* Friends have special name lookup rules. */
17698 ts = ts_within_enclosing_non_class;
17699 else if (is_declaration
17700 && cp_lexer_next_token_is (parser->lexer,
17701 CPP_SEMICOLON))
17702 /* This is a `class-key identifier ;' */
17703 ts = ts_current;
17704 else
17705 ts = ts_global;
17707 template_p =
17708 (template_parm_lists_apply
17709 && (cp_parser_next_token_starts_class_definition_p (parser)
17710 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17711 /* An unqualified name was used to reference this type, so
17712 there were no qualifying templates. */
17713 if (template_parm_lists_apply
17714 && !cp_parser_check_template_parameters (parser,
17715 /*num_templates=*/0,
17716 token->location,
17717 /*declarator=*/NULL))
17718 return error_mark_node;
17719 type = xref_tag (tag_type, identifier, ts, template_p);
17723 if (type == error_mark_node)
17724 return error_mark_node;
17726 /* Allow attributes on forward declarations of classes. */
17727 if (attributes)
17729 if (TREE_CODE (type) == TYPENAME_TYPE)
17730 warning (OPT_Wattributes,
17731 "attributes ignored on uninstantiated type");
17732 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17733 && ! processing_explicit_instantiation)
17734 warning (OPT_Wattributes,
17735 "attributes ignored on template instantiation");
17736 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17737 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17738 else
17739 warning (OPT_Wattributes,
17740 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17743 if (tag_type != enum_type)
17745 /* Indicate whether this class was declared as a `class' or as a
17746 `struct'. */
17747 if (CLASS_TYPE_P (type))
17748 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17749 cp_parser_check_class_key (tag_type, type);
17752 /* A "<" cannot follow an elaborated type specifier. If that
17753 happens, the user was probably trying to form a template-id. */
17754 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17755 token->location);
17757 return type;
17760 /* Parse an enum-specifier.
17762 enum-specifier:
17763 enum-head { enumerator-list [opt] }
17764 enum-head { enumerator-list , } [C++0x]
17766 enum-head:
17767 enum-key identifier [opt] enum-base [opt]
17768 enum-key nested-name-specifier identifier enum-base [opt]
17770 enum-key:
17771 enum
17772 enum class [C++0x]
17773 enum struct [C++0x]
17775 enum-base: [C++0x]
17776 : type-specifier-seq
17778 opaque-enum-specifier:
17779 enum-key identifier enum-base [opt] ;
17781 GNU Extensions:
17782 enum-key attributes[opt] identifier [opt] enum-base [opt]
17783 { enumerator-list [opt] }attributes[opt]
17784 enum-key attributes[opt] identifier [opt] enum-base [opt]
17785 { enumerator-list, }attributes[opt] [C++0x]
17787 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17788 if the token stream isn't an enum-specifier after all. */
17790 static tree
17791 cp_parser_enum_specifier (cp_parser* parser)
17793 tree identifier;
17794 tree type = NULL_TREE;
17795 tree prev_scope;
17796 tree nested_name_specifier = NULL_TREE;
17797 tree attributes;
17798 bool scoped_enum_p = false;
17799 bool has_underlying_type = false;
17800 bool nested_being_defined = false;
17801 bool new_value_list = false;
17802 bool is_new_type = false;
17803 bool is_unnamed = false;
17804 tree underlying_type = NULL_TREE;
17805 cp_token *type_start_token = NULL;
17806 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17808 parser->colon_corrects_to_scope_p = false;
17810 /* Parse tentatively so that we can back up if we don't find a
17811 enum-specifier. */
17812 cp_parser_parse_tentatively (parser);
17814 /* Caller guarantees that the current token is 'enum', an identifier
17815 possibly follows, and the token after that is an opening brace.
17816 If we don't have an identifier, fabricate an anonymous name for
17817 the enumeration being defined. */
17818 cp_lexer_consume_token (parser->lexer);
17820 /* Parse the "class" or "struct", which indicates a scoped
17821 enumeration type in C++0x. */
17822 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17823 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17825 if (cxx_dialect < cxx11)
17826 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17828 /* Consume the `struct' or `class' token. */
17829 cp_lexer_consume_token (parser->lexer);
17831 scoped_enum_p = true;
17834 attributes = cp_parser_attributes_opt (parser);
17836 /* Clear the qualification. */
17837 parser->scope = NULL_TREE;
17838 parser->qualifying_scope = NULL_TREE;
17839 parser->object_scope = NULL_TREE;
17841 /* Figure out in what scope the declaration is being placed. */
17842 prev_scope = current_scope ();
17844 type_start_token = cp_lexer_peek_token (parser->lexer);
17846 push_deferring_access_checks (dk_no_check);
17847 nested_name_specifier
17848 = cp_parser_nested_name_specifier_opt (parser,
17849 /*typename_keyword_p=*/true,
17850 /*check_dependency_p=*/false,
17851 /*type_p=*/false,
17852 /*is_declaration=*/false);
17854 if (nested_name_specifier)
17856 tree name;
17858 identifier = cp_parser_identifier (parser);
17859 name = cp_parser_lookup_name (parser, identifier,
17860 enum_type,
17861 /*is_template=*/false,
17862 /*is_namespace=*/false,
17863 /*check_dependency=*/true,
17864 /*ambiguous_decls=*/NULL,
17865 input_location);
17866 if (name && name != error_mark_node)
17868 type = TREE_TYPE (name);
17869 if (TREE_CODE (type) == TYPENAME_TYPE)
17871 /* Are template enums allowed in ISO? */
17872 if (template_parm_scope_p ())
17873 pedwarn (type_start_token->location, OPT_Wpedantic,
17874 "%qD is an enumeration template", name);
17875 /* ignore a typename reference, for it will be solved by name
17876 in start_enum. */
17877 type = NULL_TREE;
17880 else if (nested_name_specifier == error_mark_node)
17881 /* We already issued an error. */;
17882 else
17884 error_at (type_start_token->location,
17885 "%qD does not name an enumeration in %qT",
17886 identifier, nested_name_specifier);
17887 nested_name_specifier = error_mark_node;
17890 else
17892 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17893 identifier = cp_parser_identifier (parser);
17894 else
17896 identifier = make_anon_name ();
17897 is_unnamed = true;
17898 if (scoped_enum_p)
17899 error_at (type_start_token->location,
17900 "unnamed scoped enum is not allowed");
17903 pop_deferring_access_checks ();
17905 /* Check for the `:' that denotes a specified underlying type in C++0x.
17906 Note that a ':' could also indicate a bitfield width, however. */
17907 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17909 cp_decl_specifier_seq type_specifiers;
17911 /* Consume the `:'. */
17912 cp_lexer_consume_token (parser->lexer);
17914 /* Parse the type-specifier-seq. */
17915 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17916 /*is_trailing_return=*/false,
17917 &type_specifiers);
17919 /* At this point this is surely not elaborated type specifier. */
17920 if (!cp_parser_parse_definitely (parser))
17921 return NULL_TREE;
17923 if (cxx_dialect < cxx11)
17924 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17926 has_underlying_type = true;
17928 /* If that didn't work, stop. */
17929 if (type_specifiers.type != error_mark_node)
17931 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17932 /*initialized=*/0, NULL);
17933 if (underlying_type == error_mark_node
17934 || check_for_bare_parameter_packs (underlying_type))
17935 underlying_type = NULL_TREE;
17939 /* Look for the `{' but don't consume it yet. */
17940 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17942 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17944 cp_parser_error (parser, "expected %<{%>");
17945 if (has_underlying_type)
17947 type = NULL_TREE;
17948 goto out;
17951 /* An opaque-enum-specifier must have a ';' here. */
17952 if ((scoped_enum_p || underlying_type)
17953 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17955 cp_parser_error (parser, "expected %<;%> or %<{%>");
17956 if (has_underlying_type)
17958 type = NULL_TREE;
17959 goto out;
17964 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17965 return NULL_TREE;
17967 if (nested_name_specifier)
17969 if (CLASS_TYPE_P (nested_name_specifier))
17971 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17972 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17973 push_scope (nested_name_specifier);
17975 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17977 push_nested_namespace (nested_name_specifier);
17981 /* Issue an error message if type-definitions are forbidden here. */
17982 if (!cp_parser_check_type_definition (parser))
17983 type = error_mark_node;
17984 else
17985 /* Create the new type. We do this before consuming the opening
17986 brace so the enum will be recorded as being on the line of its
17987 tag (or the 'enum' keyword, if there is no tag). */
17988 type = start_enum (identifier, type, underlying_type,
17989 attributes, scoped_enum_p, &is_new_type);
17991 /* If the next token is not '{' it is an opaque-enum-specifier or an
17992 elaborated-type-specifier. */
17993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17995 timevar_push (TV_PARSE_ENUM);
17996 if (nested_name_specifier
17997 && nested_name_specifier != error_mark_node)
17999 /* The following catches invalid code such as:
18000 enum class S<int>::E { A, B, C }; */
18001 if (!processing_specialization
18002 && CLASS_TYPE_P (nested_name_specifier)
18003 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18004 error_at (type_start_token->location, "cannot add an enumerator "
18005 "list to a template instantiation");
18007 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18009 error_at (type_start_token->location,
18010 "%<%T::%E%> has not been declared",
18011 TYPE_CONTEXT (nested_name_specifier),
18012 nested_name_specifier);
18013 type = error_mark_node;
18015 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18016 && !CLASS_TYPE_P (nested_name_specifier))
18018 error_at (type_start_token->location, "nested name specifier "
18019 "%qT for enum declaration does not name a class "
18020 "or namespace", nested_name_specifier);
18021 type = error_mark_node;
18023 /* If that scope does not contain the scope in which the
18024 class was originally declared, the program is invalid. */
18025 else if (prev_scope && !is_ancestor (prev_scope,
18026 nested_name_specifier))
18028 if (at_namespace_scope_p ())
18029 error_at (type_start_token->location,
18030 "declaration of %qD in namespace %qD which does not "
18031 "enclose %qD",
18032 type, prev_scope, nested_name_specifier);
18033 else
18034 error_at (type_start_token->location,
18035 "declaration of %qD in %qD which does not "
18036 "enclose %qD",
18037 type, prev_scope, nested_name_specifier);
18038 type = error_mark_node;
18040 /* If that scope is the scope where the declaration is being placed
18041 the program is invalid. */
18042 else if (CLASS_TYPE_P (nested_name_specifier)
18043 && CLASS_TYPE_P (prev_scope)
18044 && same_type_p (nested_name_specifier, prev_scope))
18046 permerror (type_start_token->location,
18047 "extra qualification not allowed");
18048 nested_name_specifier = NULL_TREE;
18052 if (scoped_enum_p)
18053 begin_scope (sk_scoped_enum, type);
18055 /* Consume the opening brace. */
18056 cp_lexer_consume_token (parser->lexer);
18058 if (type == error_mark_node)
18059 ; /* Nothing to add */
18060 else if (OPAQUE_ENUM_P (type)
18061 || (cxx_dialect > cxx98 && processing_specialization))
18063 new_value_list = true;
18064 SET_OPAQUE_ENUM_P (type, false);
18065 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18067 else
18069 error_at (type_start_token->location,
18070 "multiple definition of %q#T", type);
18071 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18072 "previous definition here");
18073 type = error_mark_node;
18076 if (type == error_mark_node)
18077 cp_parser_skip_to_end_of_block_or_statement (parser);
18078 /* If the next token is not '}', then there are some enumerators. */
18079 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18081 if (is_unnamed && !scoped_enum_p)
18082 pedwarn (type_start_token->location, OPT_Wpedantic,
18083 "ISO C++ forbids empty unnamed enum");
18085 else
18086 cp_parser_enumerator_list (parser, type);
18088 /* Consume the final '}'. */
18089 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18091 if (scoped_enum_p)
18092 finish_scope ();
18093 timevar_pop (TV_PARSE_ENUM);
18095 else
18097 /* If a ';' follows, then it is an opaque-enum-specifier
18098 and additional restrictions apply. */
18099 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18101 if (is_unnamed)
18102 error_at (type_start_token->location,
18103 "opaque-enum-specifier without name");
18104 else if (nested_name_specifier)
18105 error_at (type_start_token->location,
18106 "opaque-enum-specifier must use a simple identifier");
18110 /* Look for trailing attributes to apply to this enumeration, and
18111 apply them if appropriate. */
18112 if (cp_parser_allow_gnu_extensions_p (parser))
18114 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18115 cplus_decl_attributes (&type,
18116 trailing_attr,
18117 (int) ATTR_FLAG_TYPE_IN_PLACE);
18120 /* Finish up the enumeration. */
18121 if (type != error_mark_node)
18123 if (new_value_list)
18124 finish_enum_value_list (type);
18125 if (is_new_type)
18126 finish_enum (type);
18129 if (nested_name_specifier)
18131 if (CLASS_TYPE_P (nested_name_specifier))
18133 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18134 pop_scope (nested_name_specifier);
18136 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18138 pop_nested_namespace (nested_name_specifier);
18141 out:
18142 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18143 return type;
18146 /* Parse an enumerator-list. The enumerators all have the indicated
18147 TYPE.
18149 enumerator-list:
18150 enumerator-definition
18151 enumerator-list , enumerator-definition */
18153 static void
18154 cp_parser_enumerator_list (cp_parser* parser, tree type)
18156 while (true)
18158 /* Parse an enumerator-definition. */
18159 cp_parser_enumerator_definition (parser, type);
18161 /* If the next token is not a ',', we've reached the end of
18162 the list. */
18163 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18164 break;
18165 /* Otherwise, consume the `,' and keep going. */
18166 cp_lexer_consume_token (parser->lexer);
18167 /* If the next token is a `}', there is a trailing comma. */
18168 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18170 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18171 pedwarn (input_location, OPT_Wpedantic,
18172 "comma at end of enumerator list");
18173 break;
18178 /* Parse an enumerator-definition. The enumerator has the indicated
18179 TYPE.
18181 enumerator-definition:
18182 enumerator
18183 enumerator = constant-expression
18185 enumerator:
18186 identifier
18188 GNU Extensions:
18190 enumerator-definition:
18191 enumerator attributes [opt]
18192 enumerator attributes [opt] = constant-expression */
18194 static void
18195 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18197 tree identifier;
18198 tree value;
18199 location_t loc;
18201 /* Save the input location because we are interested in the location
18202 of the identifier and not the location of the explicit value. */
18203 loc = cp_lexer_peek_token (parser->lexer)->location;
18205 /* Look for the identifier. */
18206 identifier = cp_parser_identifier (parser);
18207 if (identifier == error_mark_node)
18208 return;
18210 /* Parse any specified attributes. */
18211 tree attrs = cp_parser_attributes_opt (parser);
18213 /* If the next token is an '=', then there is an explicit value. */
18214 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18216 /* Consume the `=' token. */
18217 cp_lexer_consume_token (parser->lexer);
18218 /* Parse the value. */
18219 value = cp_parser_constant_expression (parser);
18221 else
18222 value = NULL_TREE;
18224 /* If we are processing a template, make sure the initializer of the
18225 enumerator doesn't contain any bare template parameter pack. */
18226 if (check_for_bare_parameter_packs (value))
18227 value = error_mark_node;
18229 /* Create the enumerator. */
18230 build_enumerator (identifier, value, type, attrs, loc);
18233 /* Parse a namespace-name.
18235 namespace-name:
18236 original-namespace-name
18237 namespace-alias
18239 Returns the NAMESPACE_DECL for the namespace. */
18241 static tree
18242 cp_parser_namespace_name (cp_parser* parser)
18244 tree identifier;
18245 tree namespace_decl;
18247 cp_token *token = cp_lexer_peek_token (parser->lexer);
18249 /* Get the name of the namespace. */
18250 identifier = cp_parser_identifier (parser);
18251 if (identifier == error_mark_node)
18252 return error_mark_node;
18254 /* Look up the identifier in the currently active scope. Look only
18255 for namespaces, due to:
18257 [basic.lookup.udir]
18259 When looking up a namespace-name in a using-directive or alias
18260 definition, only namespace names are considered.
18262 And:
18264 [basic.lookup.qual]
18266 During the lookup of a name preceding the :: scope resolution
18267 operator, object, function, and enumerator names are ignored.
18269 (Note that cp_parser_qualifying_entity only calls this
18270 function if the token after the name is the scope resolution
18271 operator.) */
18272 namespace_decl = cp_parser_lookup_name (parser, identifier,
18273 none_type,
18274 /*is_template=*/false,
18275 /*is_namespace=*/true,
18276 /*check_dependency=*/true,
18277 /*ambiguous_decls=*/NULL,
18278 token->location);
18279 /* If it's not a namespace, issue an error. */
18280 if (namespace_decl == error_mark_node
18281 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18283 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18284 error_at (token->location, "%qD is not a namespace-name", identifier);
18285 cp_parser_error (parser, "expected namespace-name");
18286 namespace_decl = error_mark_node;
18289 return namespace_decl;
18292 /* Parse a namespace-definition.
18294 namespace-definition:
18295 named-namespace-definition
18296 unnamed-namespace-definition
18298 named-namespace-definition:
18299 original-namespace-definition
18300 extension-namespace-definition
18302 original-namespace-definition:
18303 namespace identifier { namespace-body }
18305 extension-namespace-definition:
18306 namespace original-namespace-name { namespace-body }
18308 unnamed-namespace-definition:
18309 namespace { namespace-body } */
18311 static void
18312 cp_parser_namespace_definition (cp_parser* parser)
18314 tree identifier;
18315 int nested_definition_count = 0;
18317 cp_ensure_no_omp_declare_simd (parser);
18318 cp_ensure_no_oacc_routine (parser);
18320 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18322 if (is_inline)
18324 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18325 cp_lexer_consume_token (parser->lexer);
18328 /* Look for the `namespace' keyword. */
18329 cp_token* token
18330 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18332 /* Parse any specified attributes before the identifier. */
18333 tree attribs = cp_parser_attributes_opt (parser);
18335 for (;;)
18337 identifier = NULL_TREE;
18339 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18341 identifier = cp_parser_identifier (parser);
18343 /* Parse any attributes specified after the identifier. */
18344 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18347 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18348 break;
18350 if (!nested_definition_count && cxx_dialect < cxx1z)
18351 pedwarn (input_location, OPT_Wpedantic,
18352 "nested namespace definitions only available with "
18353 "-std=c++1z or -std=gnu++1z");
18355 /* Nested namespace names can create new namespaces (unlike
18356 other qualified-ids). */
18357 if (int count = identifier ? push_namespace (identifier) : 0)
18358 nested_definition_count += count;
18359 else
18360 cp_parser_error (parser, "nested namespace name required");
18361 cp_lexer_consume_token (parser->lexer);
18364 if (nested_definition_count && !identifier)
18365 cp_parser_error (parser, "namespace name required");
18367 if (nested_definition_count && attribs)
18368 error_at (token->location,
18369 "a nested namespace definition cannot have attributes");
18370 if (nested_definition_count && is_inline)
18371 error_at (token->location,
18372 "a nested namespace definition cannot be inline");
18374 /* Start the namespace. */
18375 nested_definition_count += push_namespace (identifier, is_inline);
18377 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18379 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18381 /* Look for the `{' to validate starting the namespace. */
18382 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18384 /* Parse the body of the namespace. */
18385 cp_parser_namespace_body (parser);
18387 /* Look for the final `}'. */
18388 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18390 if (has_visibility)
18391 pop_visibility (1);
18393 /* Pop the nested namespace definitions. */
18394 while (nested_definition_count--)
18395 pop_namespace ();
18398 /* Parse a namespace-body.
18400 namespace-body:
18401 declaration-seq [opt] */
18403 static void
18404 cp_parser_namespace_body (cp_parser* parser)
18406 cp_parser_declaration_seq_opt (parser);
18409 /* Parse a namespace-alias-definition.
18411 namespace-alias-definition:
18412 namespace identifier = qualified-namespace-specifier ; */
18414 static void
18415 cp_parser_namespace_alias_definition (cp_parser* parser)
18417 tree identifier;
18418 tree namespace_specifier;
18420 cp_token *token = cp_lexer_peek_token (parser->lexer);
18422 /* Look for the `namespace' keyword. */
18423 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18424 /* Look for the identifier. */
18425 identifier = cp_parser_identifier (parser);
18426 if (identifier == error_mark_node)
18427 return;
18428 /* Look for the `=' token. */
18429 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18430 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18432 error_at (token->location, "%<namespace%> definition is not allowed here");
18433 /* Skip the definition. */
18434 cp_lexer_consume_token (parser->lexer);
18435 if (cp_parser_skip_to_closing_brace (parser))
18436 cp_lexer_consume_token (parser->lexer);
18437 return;
18439 cp_parser_require (parser, CPP_EQ, RT_EQ);
18440 /* Look for the qualified-namespace-specifier. */
18441 namespace_specifier
18442 = cp_parser_qualified_namespace_specifier (parser);
18443 /* Look for the `;' token. */
18444 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18446 /* Register the alias in the symbol table. */
18447 do_namespace_alias (identifier, namespace_specifier);
18450 /* Parse a qualified-namespace-specifier.
18452 qualified-namespace-specifier:
18453 :: [opt] nested-name-specifier [opt] namespace-name
18455 Returns a NAMESPACE_DECL corresponding to the specified
18456 namespace. */
18458 static tree
18459 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18461 /* Look for the optional `::'. */
18462 cp_parser_global_scope_opt (parser,
18463 /*current_scope_valid_p=*/false);
18465 /* Look for the optional nested-name-specifier. */
18466 cp_parser_nested_name_specifier_opt (parser,
18467 /*typename_keyword_p=*/false,
18468 /*check_dependency_p=*/true,
18469 /*type_p=*/false,
18470 /*is_declaration=*/true);
18472 return cp_parser_namespace_name (parser);
18475 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18476 access declaration.
18478 using-declaration:
18479 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18480 using :: unqualified-id ;
18482 access-declaration:
18483 qualified-id ;
18487 static bool
18488 cp_parser_using_declaration (cp_parser* parser,
18489 bool access_declaration_p)
18491 cp_token *token;
18492 bool typename_p = false;
18493 bool global_scope_p;
18494 tree decl;
18495 tree identifier;
18496 tree qscope;
18497 int oldcount = errorcount;
18498 cp_token *diag_token = NULL;
18500 if (access_declaration_p)
18502 diag_token = cp_lexer_peek_token (parser->lexer);
18503 cp_parser_parse_tentatively (parser);
18505 else
18507 /* Look for the `using' keyword. */
18508 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18510 again:
18511 /* Peek at the next token. */
18512 token = cp_lexer_peek_token (parser->lexer);
18513 /* See if it's `typename'. */
18514 if (token->keyword == RID_TYPENAME)
18516 /* Remember that we've seen it. */
18517 typename_p = true;
18518 /* Consume the `typename' token. */
18519 cp_lexer_consume_token (parser->lexer);
18523 /* Look for the optional global scope qualification. */
18524 global_scope_p
18525 = (cp_parser_global_scope_opt (parser,
18526 /*current_scope_valid_p=*/false)
18527 != NULL_TREE);
18529 /* If we saw `typename', or didn't see `::', then there must be a
18530 nested-name-specifier present. */
18531 if (typename_p || !global_scope_p)
18533 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18534 /*check_dependency_p=*/true,
18535 /*type_p=*/false,
18536 /*is_declaration=*/true);
18537 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18539 cp_parser_skip_to_end_of_block_or_statement (parser);
18540 return false;
18543 /* Otherwise, we could be in either of the two productions. In that
18544 case, treat the nested-name-specifier as optional. */
18545 else
18546 qscope = cp_parser_nested_name_specifier_opt (parser,
18547 /*typename_keyword_p=*/false,
18548 /*check_dependency_p=*/true,
18549 /*type_p=*/false,
18550 /*is_declaration=*/true);
18551 if (!qscope)
18552 qscope = global_namespace;
18553 else if (UNSCOPED_ENUM_P (qscope))
18554 qscope = CP_TYPE_CONTEXT (qscope);
18556 if (access_declaration_p && cp_parser_error_occurred (parser))
18557 /* Something has already gone wrong; there's no need to parse
18558 further. Since an error has occurred, the return value of
18559 cp_parser_parse_definitely will be false, as required. */
18560 return cp_parser_parse_definitely (parser);
18562 token = cp_lexer_peek_token (parser->lexer);
18563 /* Parse the unqualified-id. */
18564 identifier = cp_parser_unqualified_id (parser,
18565 /*template_keyword_p=*/false,
18566 /*check_dependency_p=*/true,
18567 /*declarator_p=*/true,
18568 /*optional_p=*/false);
18570 if (access_declaration_p)
18572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18573 cp_parser_simulate_error (parser);
18574 if (!cp_parser_parse_definitely (parser))
18575 return false;
18577 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18579 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18580 if (cxx_dialect < cxx1z
18581 && !in_system_header_at (ell->location))
18582 pedwarn (ell->location, 0,
18583 "pack expansion in using-declaration only available "
18584 "with -std=c++1z or -std=gnu++1z");
18585 qscope = make_pack_expansion (qscope);
18588 /* The function we call to handle a using-declaration is different
18589 depending on what scope we are in. */
18590 if (qscope == error_mark_node || identifier == error_mark_node)
18592 else if (!identifier_p (identifier)
18593 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18594 /* [namespace.udecl]
18596 A using declaration shall not name a template-id. */
18597 error_at (token->location,
18598 "a template-id may not appear in a using-declaration");
18599 else
18601 if (at_class_scope_p ())
18603 /* Create the USING_DECL. */
18604 decl = do_class_using_decl (qscope, identifier);
18606 if (decl && typename_p)
18607 USING_DECL_TYPENAME_P (decl) = 1;
18609 if (check_for_bare_parameter_packs (decl))
18611 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18612 return false;
18614 else
18615 /* Add it to the list of members in this class. */
18616 finish_member_declaration (decl);
18618 else
18620 decl = cp_parser_lookup_name_simple (parser,
18621 identifier,
18622 token->location);
18623 if (decl == error_mark_node)
18624 cp_parser_name_lookup_error (parser, identifier,
18625 decl, NLE_NULL,
18626 token->location);
18627 else if (check_for_bare_parameter_packs (decl))
18629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18630 return false;
18632 else if (!at_namespace_scope_p ())
18633 finish_local_using_decl (decl, qscope, identifier);
18634 else
18635 finish_namespace_using_decl (decl, qscope, identifier);
18639 if (!access_declaration_p
18640 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18642 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18643 if (cxx_dialect < cxx1z)
18644 pedwarn (comma->location, 0,
18645 "comma-separated list in using-declaration only available "
18646 "with -std=c++1z or -std=gnu++1z");
18647 goto again;
18650 /* Look for the final `;'. */
18651 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18653 if (access_declaration_p && errorcount == oldcount)
18654 warning_at (diag_token->location, OPT_Wdeprecated,
18655 "access declarations are deprecated "
18656 "in favour of using-declarations; "
18657 "suggestion: add the %<using%> keyword");
18659 return true;
18662 /* Parse an alias-declaration.
18664 alias-declaration:
18665 using identifier attribute-specifier-seq [opt] = type-id */
18667 static tree
18668 cp_parser_alias_declaration (cp_parser* parser)
18670 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18671 location_t id_location;
18672 cp_declarator *declarator;
18673 cp_decl_specifier_seq decl_specs;
18674 bool member_p;
18675 const char *saved_message = NULL;
18677 /* Look for the `using' keyword. */
18678 cp_token *using_token
18679 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18680 if (using_token == NULL)
18681 return error_mark_node;
18683 id_location = cp_lexer_peek_token (parser->lexer)->location;
18684 id = cp_parser_identifier (parser);
18685 if (id == error_mark_node)
18686 return error_mark_node;
18688 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18689 attributes = cp_parser_attributes_opt (parser);
18690 if (attributes == error_mark_node)
18691 return error_mark_node;
18693 cp_parser_require (parser, CPP_EQ, RT_EQ);
18695 if (cp_parser_error_occurred (parser))
18696 return error_mark_node;
18698 cp_parser_commit_to_tentative_parse (parser);
18700 /* Now we are going to parse the type-id of the declaration. */
18703 [dcl.type]/3 says:
18705 "A type-specifier-seq shall not define a class or enumeration
18706 unless it appears in the type-id of an alias-declaration (7.1.3) that
18707 is not the declaration of a template-declaration."
18709 In other words, if we currently are in an alias template, the
18710 type-id should not define a type.
18712 So let's set parser->type_definition_forbidden_message in that
18713 case; cp_parser_check_type_definition (called by
18714 cp_parser_class_specifier) will then emit an error if a type is
18715 defined in the type-id. */
18716 if (parser->num_template_parameter_lists)
18718 saved_message = parser->type_definition_forbidden_message;
18719 parser->type_definition_forbidden_message =
18720 G_("types may not be defined in alias template declarations");
18723 type = cp_parser_type_id (parser);
18725 /* Restore the error message if need be. */
18726 if (parser->num_template_parameter_lists)
18727 parser->type_definition_forbidden_message = saved_message;
18729 if (type == error_mark_node
18730 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18732 cp_parser_skip_to_end_of_block_or_statement (parser);
18733 return error_mark_node;
18736 /* A typedef-name can also be introduced by an alias-declaration. The
18737 identifier following the using keyword becomes a typedef-name. It has
18738 the same semantics as if it were introduced by the typedef
18739 specifier. In particular, it does not define a new type and it shall
18740 not appear in the type-id. */
18742 clear_decl_specs (&decl_specs);
18743 decl_specs.type = type;
18744 if (attributes != NULL_TREE)
18746 decl_specs.attributes = attributes;
18747 set_and_check_decl_spec_loc (&decl_specs,
18748 ds_attribute,
18749 attrs_token);
18751 set_and_check_decl_spec_loc (&decl_specs,
18752 ds_typedef,
18753 using_token);
18754 set_and_check_decl_spec_loc (&decl_specs,
18755 ds_alias,
18756 using_token);
18758 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18759 declarator->id_loc = id_location;
18761 member_p = at_class_scope_p ();
18762 if (member_p)
18763 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18764 NULL_TREE, attributes);
18765 else
18766 decl = start_decl (declarator, &decl_specs, 0,
18767 attributes, NULL_TREE, &pushed_scope);
18768 if (decl == error_mark_node)
18769 return decl;
18771 // Attach constraints to the alias declaration.
18772 if (flag_concepts && current_template_parms)
18774 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18775 tree constr = build_constraints (reqs, NULL_TREE);
18776 set_constraints (decl, constr);
18779 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18781 if (pushed_scope)
18782 pop_scope (pushed_scope);
18784 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18785 added into the symbol table; otherwise, return the TYPE_DECL. */
18786 if (DECL_LANG_SPECIFIC (decl)
18787 && DECL_TEMPLATE_INFO (decl)
18788 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18790 decl = DECL_TI_TEMPLATE (decl);
18791 if (member_p)
18792 check_member_template (decl);
18795 return decl;
18798 /* Parse a using-directive.
18800 using-directive:
18801 using namespace :: [opt] nested-name-specifier [opt]
18802 namespace-name ; */
18804 static void
18805 cp_parser_using_directive (cp_parser* parser)
18807 tree namespace_decl;
18808 tree attribs;
18810 /* Look for the `using' keyword. */
18811 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18812 /* And the `namespace' keyword. */
18813 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18814 /* Look for the optional `::' operator. */
18815 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18816 /* And the optional nested-name-specifier. */
18817 cp_parser_nested_name_specifier_opt (parser,
18818 /*typename_keyword_p=*/false,
18819 /*check_dependency_p=*/true,
18820 /*type_p=*/false,
18821 /*is_declaration=*/true);
18822 /* Get the namespace being used. */
18823 namespace_decl = cp_parser_namespace_name (parser);
18824 /* And any specified attributes. */
18825 attribs = cp_parser_attributes_opt (parser);
18827 /* Update the symbol table. */
18828 if (namespace_bindings_p ())
18829 finish_namespace_using_directive (namespace_decl, attribs);
18830 else
18831 finish_local_using_directive (namespace_decl, attribs);
18833 /* Look for the final `;'. */
18834 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18837 /* Parse an asm-definition.
18839 asm-definition:
18840 asm ( string-literal ) ;
18842 GNU Extension:
18844 asm-definition:
18845 asm volatile [opt] ( string-literal ) ;
18846 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18847 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18848 : asm-operand-list [opt] ) ;
18849 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18850 : asm-operand-list [opt]
18851 : asm-clobber-list [opt] ) ;
18852 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18853 : asm-clobber-list [opt]
18854 : asm-goto-list ) ; */
18856 static void
18857 cp_parser_asm_definition (cp_parser* parser)
18859 tree string;
18860 tree outputs = NULL_TREE;
18861 tree inputs = NULL_TREE;
18862 tree clobbers = NULL_TREE;
18863 tree labels = NULL_TREE;
18864 tree asm_stmt;
18865 bool volatile_p = false;
18866 bool extended_p = false;
18867 bool invalid_inputs_p = false;
18868 bool invalid_outputs_p = false;
18869 bool goto_p = false;
18870 required_token missing = RT_NONE;
18872 /* Look for the `asm' keyword. */
18873 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18875 if (parser->in_function_body
18876 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18878 error ("%<asm%> in %<constexpr%> function");
18879 cp_function_chain->invalid_constexpr = true;
18882 /* See if the next token is `volatile'. */
18883 if (cp_parser_allow_gnu_extensions_p (parser)
18884 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18886 /* Remember that we saw the `volatile' keyword. */
18887 volatile_p = true;
18888 /* Consume the token. */
18889 cp_lexer_consume_token (parser->lexer);
18891 if (cp_parser_allow_gnu_extensions_p (parser)
18892 && parser->in_function_body
18893 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18895 /* Remember that we saw the `goto' keyword. */
18896 goto_p = true;
18897 /* Consume the token. */
18898 cp_lexer_consume_token (parser->lexer);
18900 /* Look for the opening `('. */
18901 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18902 return;
18903 /* Look for the string. */
18904 string = cp_parser_string_literal (parser, false, false);
18905 if (string == error_mark_node)
18907 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18908 /*consume_paren=*/true);
18909 return;
18912 /* If we're allowing GNU extensions, check for the extended assembly
18913 syntax. Unfortunately, the `:' tokens need not be separated by
18914 a space in C, and so, for compatibility, we tolerate that here
18915 too. Doing that means that we have to treat the `::' operator as
18916 two `:' tokens. */
18917 if (cp_parser_allow_gnu_extensions_p (parser)
18918 && parser->in_function_body
18919 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18920 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18922 bool inputs_p = false;
18923 bool clobbers_p = false;
18924 bool labels_p = false;
18926 /* The extended syntax was used. */
18927 extended_p = true;
18929 /* Look for outputs. */
18930 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18932 /* Consume the `:'. */
18933 cp_lexer_consume_token (parser->lexer);
18934 /* Parse the output-operands. */
18935 if (cp_lexer_next_token_is_not (parser->lexer,
18936 CPP_COLON)
18937 && cp_lexer_next_token_is_not (parser->lexer,
18938 CPP_SCOPE)
18939 && cp_lexer_next_token_is_not (parser->lexer,
18940 CPP_CLOSE_PAREN)
18941 && !goto_p)
18943 outputs = cp_parser_asm_operand_list (parser);
18944 if (outputs == error_mark_node)
18945 invalid_outputs_p = true;
18948 /* If the next token is `::', there are no outputs, and the
18949 next token is the beginning of the inputs. */
18950 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18951 /* The inputs are coming next. */
18952 inputs_p = true;
18954 /* Look for inputs. */
18955 if (inputs_p
18956 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18958 /* Consume the `:' or `::'. */
18959 cp_lexer_consume_token (parser->lexer);
18960 /* Parse the output-operands. */
18961 if (cp_lexer_next_token_is_not (parser->lexer,
18962 CPP_COLON)
18963 && cp_lexer_next_token_is_not (parser->lexer,
18964 CPP_SCOPE)
18965 && cp_lexer_next_token_is_not (parser->lexer,
18966 CPP_CLOSE_PAREN))
18968 inputs = cp_parser_asm_operand_list (parser);
18969 if (inputs == error_mark_node)
18970 invalid_inputs_p = true;
18973 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18974 /* The clobbers are coming next. */
18975 clobbers_p = true;
18977 /* Look for clobbers. */
18978 if (clobbers_p
18979 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18981 clobbers_p = true;
18982 /* Consume the `:' or `::'. */
18983 cp_lexer_consume_token (parser->lexer);
18984 /* Parse the clobbers. */
18985 if (cp_lexer_next_token_is_not (parser->lexer,
18986 CPP_COLON)
18987 && cp_lexer_next_token_is_not (parser->lexer,
18988 CPP_CLOSE_PAREN))
18989 clobbers = cp_parser_asm_clobber_list (parser);
18991 else if (goto_p
18992 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18993 /* The labels are coming next. */
18994 labels_p = true;
18996 /* Look for labels. */
18997 if (labels_p
18998 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19000 labels_p = true;
19001 /* Consume the `:' or `::'. */
19002 cp_lexer_consume_token (parser->lexer);
19003 /* Parse the labels. */
19004 labels = cp_parser_asm_label_list (parser);
19007 if (goto_p && !labels_p)
19008 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19010 else if (goto_p)
19011 missing = RT_COLON_SCOPE;
19013 /* Look for the closing `)'. */
19014 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19015 missing ? missing : RT_CLOSE_PAREN))
19016 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19017 /*consume_paren=*/true);
19018 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19020 if (!invalid_inputs_p && !invalid_outputs_p)
19022 /* Create the ASM_EXPR. */
19023 if (parser->in_function_body)
19025 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19026 inputs, clobbers, labels);
19027 /* If the extended syntax was not used, mark the ASM_EXPR. */
19028 if (!extended_p)
19030 tree temp = asm_stmt;
19031 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19032 temp = TREE_OPERAND (temp, 0);
19034 ASM_INPUT_P (temp) = 1;
19037 else
19038 symtab->finalize_toplevel_asm (string);
19042 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19043 type that comes from the decl-specifier-seq. */
19045 static tree
19046 strip_declarator_types (tree type, cp_declarator *declarator)
19048 for (cp_declarator *d = declarator; d;)
19049 switch (d->kind)
19051 case cdk_id:
19052 case cdk_decomp:
19053 case cdk_error:
19054 d = NULL;
19055 break;
19057 default:
19058 if (TYPE_PTRMEMFUNC_P (type))
19059 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19060 type = TREE_TYPE (type);
19061 d = d->declarator;
19062 break;
19065 return type;
19068 /* Declarators [gram.dcl.decl] */
19070 /* Parse an init-declarator.
19072 init-declarator:
19073 declarator initializer [opt]
19075 GNU Extension:
19077 init-declarator:
19078 declarator asm-specification [opt] attributes [opt] initializer [opt]
19080 function-definition:
19081 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19082 function-body
19083 decl-specifier-seq [opt] declarator function-try-block
19085 GNU Extension:
19087 function-definition:
19088 __extension__ function-definition
19090 TM Extension:
19092 function-definition:
19093 decl-specifier-seq [opt] declarator function-transaction-block
19095 The DECL_SPECIFIERS apply to this declarator. Returns a
19096 representation of the entity declared. If MEMBER_P is TRUE, then
19097 this declarator appears in a class scope. The new DECL created by
19098 this declarator is returned.
19100 The CHECKS are access checks that should be performed once we know
19101 what entity is being declared (and, therefore, what classes have
19102 befriended it).
19104 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19105 for a function-definition here as well. If the declarator is a
19106 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19107 be TRUE upon return. By that point, the function-definition will
19108 have been completely parsed.
19110 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19111 is FALSE.
19113 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19114 parsed declaration if it is an uninitialized single declarator not followed
19115 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19116 if present, will not be consumed. If returned, this declarator will be
19117 created with SD_INITIALIZED but will not call cp_finish_decl.
19119 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19120 and there is an initializer, the pointed location_t is set to the
19121 location of the '=' or `(', or '{' in C++11 token introducing the
19122 initializer. */
19124 static tree
19125 cp_parser_init_declarator (cp_parser* parser,
19126 cp_decl_specifier_seq *decl_specifiers,
19127 vec<deferred_access_check, va_gc> *checks,
19128 bool function_definition_allowed_p,
19129 bool member_p,
19130 int declares_class_or_enum,
19131 bool* function_definition_p,
19132 tree* maybe_range_for_decl,
19133 location_t* init_loc,
19134 tree* auto_result)
19136 cp_token *token = NULL, *asm_spec_start_token = NULL,
19137 *attributes_start_token = NULL;
19138 cp_declarator *declarator;
19139 tree prefix_attributes;
19140 tree attributes = NULL;
19141 tree asm_specification;
19142 tree initializer;
19143 tree decl = NULL_TREE;
19144 tree scope;
19145 int is_initialized;
19146 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19147 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19148 "(...)". */
19149 enum cpp_ttype initialization_kind;
19150 bool is_direct_init = false;
19151 bool is_non_constant_init;
19152 int ctor_dtor_or_conv_p;
19153 bool friend_p = cp_parser_friend_p (decl_specifiers);
19154 tree pushed_scope = NULL_TREE;
19155 bool range_for_decl_p = false;
19156 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19157 location_t tmp_init_loc = UNKNOWN_LOCATION;
19159 /* Gather the attributes that were provided with the
19160 decl-specifiers. */
19161 prefix_attributes = decl_specifiers->attributes;
19163 /* Assume that this is not the declarator for a function
19164 definition. */
19165 if (function_definition_p)
19166 *function_definition_p = false;
19168 /* Default arguments are only permitted for function parameters. */
19169 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19170 parser->default_arg_ok_p = false;
19172 /* Defer access checks while parsing the declarator; we cannot know
19173 what names are accessible until we know what is being
19174 declared. */
19175 resume_deferring_access_checks ();
19177 token = cp_lexer_peek_token (parser->lexer);
19179 /* Parse the declarator. */
19180 declarator
19181 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19182 &ctor_dtor_or_conv_p,
19183 /*parenthesized_p=*/NULL,
19184 member_p, friend_p);
19185 /* Gather up the deferred checks. */
19186 stop_deferring_access_checks ();
19188 parser->default_arg_ok_p = saved_default_arg_ok_p;
19190 /* If the DECLARATOR was erroneous, there's no need to go
19191 further. */
19192 if (declarator == cp_error_declarator)
19193 return error_mark_node;
19195 /* Check that the number of template-parameter-lists is OK. */
19196 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19197 token->location))
19198 return error_mark_node;
19200 if (declares_class_or_enum & 2)
19201 cp_parser_check_for_definition_in_return_type (declarator,
19202 decl_specifiers->type,
19203 decl_specifiers->locations[ds_type_spec]);
19205 /* Figure out what scope the entity declared by the DECLARATOR is
19206 located in. `grokdeclarator' sometimes changes the scope, so
19207 we compute it now. */
19208 scope = get_scope_of_declarator (declarator);
19210 /* Perform any lookups in the declared type which were thought to be
19211 dependent, but are not in the scope of the declarator. */
19212 decl_specifiers->type
19213 = maybe_update_decl_type (decl_specifiers->type, scope);
19215 /* If we're allowing GNU extensions, look for an
19216 asm-specification. */
19217 if (cp_parser_allow_gnu_extensions_p (parser))
19219 /* Look for an asm-specification. */
19220 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19221 asm_specification = cp_parser_asm_specification_opt (parser);
19223 else
19224 asm_specification = NULL_TREE;
19226 /* Look for attributes. */
19227 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19228 attributes = cp_parser_attributes_opt (parser);
19230 /* Peek at the next token. */
19231 token = cp_lexer_peek_token (parser->lexer);
19233 bool bogus_implicit_tmpl = false;
19235 if (function_declarator_p (declarator))
19237 /* Handle C++17 deduction guides. */
19238 if (!decl_specifiers->type
19239 && ctor_dtor_or_conv_p <= 0
19240 && cxx_dialect >= cxx1z)
19242 cp_declarator *id = get_id_declarator (declarator);
19243 tree name = id->u.id.unqualified_name;
19244 parser->scope = id->u.id.qualifying_scope;
19245 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19246 if (tmpl
19247 && (DECL_CLASS_TEMPLATE_P (tmpl)
19248 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19250 id->u.id.unqualified_name = dguide_name (tmpl);
19251 id->u.id.sfk = sfk_deduction_guide;
19252 ctor_dtor_or_conv_p = 1;
19256 /* Check to see if the token indicates the start of a
19257 function-definition. */
19258 if (cp_parser_token_starts_function_definition_p (token))
19260 if (!function_definition_allowed_p)
19262 /* If a function-definition should not appear here, issue an
19263 error message. */
19264 cp_parser_error (parser,
19265 "a function-definition is not allowed here");
19266 return error_mark_node;
19269 location_t func_brace_location
19270 = cp_lexer_peek_token (parser->lexer)->location;
19272 /* Neither attributes nor an asm-specification are allowed
19273 on a function-definition. */
19274 if (asm_specification)
19275 error_at (asm_spec_start_token->location,
19276 "an asm-specification is not allowed "
19277 "on a function-definition");
19278 if (attributes)
19279 error_at (attributes_start_token->location,
19280 "attributes are not allowed "
19281 "on a function-definition");
19282 /* This is a function-definition. */
19283 *function_definition_p = true;
19285 /* Parse the function definition. */
19286 if (member_p)
19287 decl = cp_parser_save_member_function_body (parser,
19288 decl_specifiers,
19289 declarator,
19290 prefix_attributes);
19291 else
19292 decl =
19293 (cp_parser_function_definition_from_specifiers_and_declarator
19294 (parser, decl_specifiers, prefix_attributes, declarator));
19296 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19298 /* This is where the prologue starts... */
19299 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19300 = func_brace_location;
19303 return decl;
19306 else if (parser->fully_implicit_function_template_p)
19308 /* A non-template declaration involving a function parameter list
19309 containing an implicit template parameter will be made into a
19310 template. If the resulting declaration is not going to be an
19311 actual function then finish the template scope here to prevent it.
19312 An error message will be issued once we have a decl to talk about.
19314 FIXME probably we should do type deduction rather than create an
19315 implicit template, but the standard currently doesn't allow it. */
19316 bogus_implicit_tmpl = true;
19317 finish_fully_implicit_template (parser, NULL_TREE);
19320 /* [dcl.dcl]
19322 Only in function declarations for constructors, destructors, type
19323 conversions, and deduction guides can the decl-specifier-seq be omitted.
19325 We explicitly postpone this check past the point where we handle
19326 function-definitions because we tolerate function-definitions
19327 that are missing their return types in some modes. */
19328 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19330 cp_parser_error (parser,
19331 "expected constructor, destructor, or type conversion");
19332 return error_mark_node;
19335 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19336 if (token->type == CPP_EQ
19337 || token->type == CPP_OPEN_PAREN
19338 || token->type == CPP_OPEN_BRACE)
19340 is_initialized = SD_INITIALIZED;
19341 initialization_kind = token->type;
19342 if (maybe_range_for_decl)
19343 *maybe_range_for_decl = error_mark_node;
19344 tmp_init_loc = token->location;
19345 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19346 *init_loc = tmp_init_loc;
19348 if (token->type == CPP_EQ
19349 && function_declarator_p (declarator))
19351 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19352 if (t2->keyword == RID_DEFAULT)
19353 is_initialized = SD_DEFAULTED;
19354 else if (t2->keyword == RID_DELETE)
19355 is_initialized = SD_DELETED;
19358 else
19360 /* If the init-declarator isn't initialized and isn't followed by a
19361 `,' or `;', it's not a valid init-declarator. */
19362 if (token->type != CPP_COMMA
19363 && token->type != CPP_SEMICOLON)
19365 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19366 range_for_decl_p = true;
19367 else
19369 if (!maybe_range_for_decl)
19370 cp_parser_error (parser, "expected initializer");
19371 return error_mark_node;
19374 is_initialized = SD_UNINITIALIZED;
19375 initialization_kind = CPP_EOF;
19378 /* Because start_decl has side-effects, we should only call it if we
19379 know we're going ahead. By this point, we know that we cannot
19380 possibly be looking at any other construct. */
19381 cp_parser_commit_to_tentative_parse (parser);
19383 /* Enter the newly declared entry in the symbol table. If we're
19384 processing a declaration in a class-specifier, we wait until
19385 after processing the initializer. */
19386 if (!member_p)
19388 if (parser->in_unbraced_linkage_specification_p)
19389 decl_specifiers->storage_class = sc_extern;
19390 decl = start_decl (declarator, decl_specifiers,
19391 range_for_decl_p? SD_INITIALIZED : is_initialized,
19392 attributes, prefix_attributes, &pushed_scope);
19393 cp_finalize_omp_declare_simd (parser, decl);
19394 cp_finalize_oacc_routine (parser, decl, false);
19395 /* Adjust location of decl if declarator->id_loc is more appropriate:
19396 set, and decl wasn't merged with another decl, in which case its
19397 location would be different from input_location, and more accurate. */
19398 if (DECL_P (decl)
19399 && declarator->id_loc != UNKNOWN_LOCATION
19400 && DECL_SOURCE_LOCATION (decl) == input_location)
19401 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19403 else if (scope)
19404 /* Enter the SCOPE. That way unqualified names appearing in the
19405 initializer will be looked up in SCOPE. */
19406 pushed_scope = push_scope (scope);
19408 /* Perform deferred access control checks, now that we know in which
19409 SCOPE the declared entity resides. */
19410 if (!member_p && decl)
19412 tree saved_current_function_decl = NULL_TREE;
19414 /* If the entity being declared is a function, pretend that we
19415 are in its scope. If it is a `friend', it may have access to
19416 things that would not otherwise be accessible. */
19417 if (TREE_CODE (decl) == FUNCTION_DECL)
19419 saved_current_function_decl = current_function_decl;
19420 current_function_decl = decl;
19423 /* Perform access checks for template parameters. */
19424 cp_parser_perform_template_parameter_access_checks (checks);
19426 /* Perform the access control checks for the declarator and the
19427 decl-specifiers. */
19428 perform_deferred_access_checks (tf_warning_or_error);
19430 /* Restore the saved value. */
19431 if (TREE_CODE (decl) == FUNCTION_DECL)
19432 current_function_decl = saved_current_function_decl;
19435 /* Parse the initializer. */
19436 initializer = NULL_TREE;
19437 is_direct_init = false;
19438 is_non_constant_init = true;
19439 if (is_initialized)
19441 if (function_declarator_p (declarator))
19443 if (initialization_kind == CPP_EQ)
19444 initializer = cp_parser_pure_specifier (parser);
19445 else
19447 /* If the declaration was erroneous, we don't really
19448 know what the user intended, so just silently
19449 consume the initializer. */
19450 if (decl != error_mark_node)
19451 error_at (tmp_init_loc, "initializer provided for function");
19452 cp_parser_skip_to_closing_parenthesis (parser,
19453 /*recovering=*/true,
19454 /*or_comma=*/false,
19455 /*consume_paren=*/true);
19458 else
19460 /* We want to record the extra mangling scope for in-class
19461 initializers of class members and initializers of static data
19462 member templates. The former involves deferring
19463 parsing of the initializer until end of class as with default
19464 arguments. So right here we only handle the latter. */
19465 if (!member_p && processing_template_decl)
19466 start_lambda_scope (decl);
19467 initializer = cp_parser_initializer (parser,
19468 &is_direct_init,
19469 &is_non_constant_init);
19470 if (!member_p && processing_template_decl)
19471 finish_lambda_scope ();
19472 if (initializer == error_mark_node)
19473 cp_parser_skip_to_end_of_statement (parser);
19477 /* The old parser allows attributes to appear after a parenthesized
19478 initializer. Mark Mitchell proposed removing this functionality
19479 on the GCC mailing lists on 2002-08-13. This parser accepts the
19480 attributes -- but ignores them. */
19481 if (cp_parser_allow_gnu_extensions_p (parser)
19482 && initialization_kind == CPP_OPEN_PAREN)
19483 if (cp_parser_attributes_opt (parser))
19484 warning (OPT_Wattributes,
19485 "attributes after parenthesized initializer ignored");
19487 /* And now complain about a non-function implicit template. */
19488 if (bogus_implicit_tmpl && decl != error_mark_node)
19489 error_at (DECL_SOURCE_LOCATION (decl),
19490 "non-function %qD declared as implicit template", decl);
19492 /* For an in-class declaration, use `grokfield' to create the
19493 declaration. */
19494 if (member_p)
19496 if (pushed_scope)
19498 pop_scope (pushed_scope);
19499 pushed_scope = NULL_TREE;
19501 decl = grokfield (declarator, decl_specifiers,
19502 initializer, !is_non_constant_init,
19503 /*asmspec=*/NULL_TREE,
19504 chainon (attributes, prefix_attributes));
19505 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19506 cp_parser_save_default_args (parser, decl);
19507 cp_finalize_omp_declare_simd (parser, decl);
19508 cp_finalize_oacc_routine (parser, decl, false);
19511 /* Finish processing the declaration. But, skip member
19512 declarations. */
19513 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19515 cp_finish_decl (decl,
19516 initializer, !is_non_constant_init,
19517 asm_specification,
19518 /* If the initializer is in parentheses, then this is
19519 a direct-initialization, which means that an
19520 `explicit' constructor is OK. Otherwise, an
19521 `explicit' constructor cannot be used. */
19522 ((is_direct_init || !is_initialized)
19523 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19525 else if ((cxx_dialect != cxx98) && friend_p
19526 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19527 /* Core issue #226 (C++0x only): A default template-argument
19528 shall not be specified in a friend class template
19529 declaration. */
19530 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19531 /*is_partial=*/false, /*is_friend_decl=*/1);
19533 if (!friend_p && pushed_scope)
19534 pop_scope (pushed_scope);
19536 if (function_declarator_p (declarator)
19537 && parser->fully_implicit_function_template_p)
19539 if (member_p)
19540 decl = finish_fully_implicit_template (parser, decl);
19541 else
19542 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19545 if (auto_result && is_initialized && decl_specifiers->type
19546 && type_uses_auto (decl_specifiers->type))
19547 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19549 return decl;
19552 /* Parse a declarator.
19554 declarator:
19555 direct-declarator
19556 ptr-operator declarator
19558 abstract-declarator:
19559 ptr-operator abstract-declarator [opt]
19560 direct-abstract-declarator
19562 GNU Extensions:
19564 declarator:
19565 attributes [opt] direct-declarator
19566 attributes [opt] ptr-operator declarator
19568 abstract-declarator:
19569 attributes [opt] ptr-operator abstract-declarator [opt]
19570 attributes [opt] direct-abstract-declarator
19572 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19573 detect constructors, destructors, deduction guides, or conversion operators.
19574 It is set to -1 if the declarator is a name, and +1 if it is a
19575 function. Otherwise it is set to zero. Usually you just want to
19576 test for >0, but internally the negative value is used.
19578 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19579 a decl-specifier-seq unless it declares a constructor, destructor,
19580 or conversion. It might seem that we could check this condition in
19581 semantic analysis, rather than parsing, but that makes it difficult
19582 to handle something like `f()'. We want to notice that there are
19583 no decl-specifiers, and therefore realize that this is an
19584 expression, not a declaration.)
19586 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19587 the declarator is a direct-declarator of the form "(...)".
19589 MEMBER_P is true iff this declarator is a member-declarator.
19591 FRIEND_P is true iff this declarator is a friend. */
19593 static cp_declarator *
19594 cp_parser_declarator (cp_parser* parser,
19595 cp_parser_declarator_kind dcl_kind,
19596 int* ctor_dtor_or_conv_p,
19597 bool* parenthesized_p,
19598 bool member_p, bool friend_p)
19600 cp_declarator *declarator;
19601 enum tree_code code;
19602 cp_cv_quals cv_quals;
19603 tree class_type;
19604 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19606 /* Assume this is not a constructor, destructor, or type-conversion
19607 operator. */
19608 if (ctor_dtor_or_conv_p)
19609 *ctor_dtor_or_conv_p = 0;
19611 if (cp_parser_allow_gnu_extensions_p (parser))
19612 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19614 /* Check for the ptr-operator production. */
19615 cp_parser_parse_tentatively (parser);
19616 /* Parse the ptr-operator. */
19617 code = cp_parser_ptr_operator (parser,
19618 &class_type,
19619 &cv_quals,
19620 &std_attributes);
19622 /* If that worked, then we have a ptr-operator. */
19623 if (cp_parser_parse_definitely (parser))
19625 /* If a ptr-operator was found, then this declarator was not
19626 parenthesized. */
19627 if (parenthesized_p)
19628 *parenthesized_p = true;
19629 /* The dependent declarator is optional if we are parsing an
19630 abstract-declarator. */
19631 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19632 cp_parser_parse_tentatively (parser);
19634 /* Parse the dependent declarator. */
19635 declarator = cp_parser_declarator (parser, dcl_kind,
19636 /*ctor_dtor_or_conv_p=*/NULL,
19637 /*parenthesized_p=*/NULL,
19638 /*member_p=*/false,
19639 friend_p);
19641 /* If we are parsing an abstract-declarator, we must handle the
19642 case where the dependent declarator is absent. */
19643 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19644 && !cp_parser_parse_definitely (parser))
19645 declarator = NULL;
19647 declarator = cp_parser_make_indirect_declarator
19648 (code, class_type, cv_quals, declarator, std_attributes);
19650 /* Everything else is a direct-declarator. */
19651 else
19653 if (parenthesized_p)
19654 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19655 CPP_OPEN_PAREN);
19656 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19657 ctor_dtor_or_conv_p,
19658 member_p, friend_p);
19661 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19662 declarator->attributes = gnu_attributes;
19663 return declarator;
19666 /* Parse a direct-declarator or direct-abstract-declarator.
19668 direct-declarator:
19669 declarator-id
19670 direct-declarator ( parameter-declaration-clause )
19671 cv-qualifier-seq [opt]
19672 ref-qualifier [opt]
19673 exception-specification [opt]
19674 direct-declarator [ constant-expression [opt] ]
19675 ( declarator )
19677 direct-abstract-declarator:
19678 direct-abstract-declarator [opt]
19679 ( parameter-declaration-clause )
19680 cv-qualifier-seq [opt]
19681 ref-qualifier [opt]
19682 exception-specification [opt]
19683 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19684 ( abstract-declarator )
19686 Returns a representation of the declarator. DCL_KIND is
19687 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19688 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19689 we are parsing a direct-declarator. It is
19690 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19691 of ambiguity we prefer an abstract declarator, as per
19692 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19693 as for cp_parser_declarator. */
19695 static cp_declarator *
19696 cp_parser_direct_declarator (cp_parser* parser,
19697 cp_parser_declarator_kind dcl_kind,
19698 int* ctor_dtor_or_conv_p,
19699 bool member_p, bool friend_p)
19701 cp_token *token;
19702 cp_declarator *declarator = NULL;
19703 tree scope = NULL_TREE;
19704 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19705 bool saved_in_declarator_p = parser->in_declarator_p;
19706 bool first = true;
19707 tree pushed_scope = NULL_TREE;
19709 while (true)
19711 /* Peek at the next token. */
19712 token = cp_lexer_peek_token (parser->lexer);
19713 if (token->type == CPP_OPEN_PAREN)
19715 /* This is either a parameter-declaration-clause, or a
19716 parenthesized declarator. When we know we are parsing a
19717 named declarator, it must be a parenthesized declarator
19718 if FIRST is true. For instance, `(int)' is a
19719 parameter-declaration-clause, with an omitted
19720 direct-abstract-declarator. But `((*))', is a
19721 parenthesized abstract declarator. Finally, when T is a
19722 template parameter `(T)' is a
19723 parameter-declaration-clause, and not a parenthesized
19724 named declarator.
19726 We first try and parse a parameter-declaration-clause,
19727 and then try a nested declarator (if FIRST is true).
19729 It is not an error for it not to be a
19730 parameter-declaration-clause, even when FIRST is
19731 false. Consider,
19733 int i (int);
19734 int i (3);
19736 The first is the declaration of a function while the
19737 second is the definition of a variable, including its
19738 initializer.
19740 Having seen only the parenthesis, we cannot know which of
19741 these two alternatives should be selected. Even more
19742 complex are examples like:
19744 int i (int (a));
19745 int i (int (3));
19747 The former is a function-declaration; the latter is a
19748 variable initialization.
19750 Thus again, we try a parameter-declaration-clause, and if
19751 that fails, we back out and return. */
19753 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19755 tree params;
19756 bool is_declarator = false;
19758 /* In a member-declarator, the only valid interpretation
19759 of a parenthesis is the start of a
19760 parameter-declaration-clause. (It is invalid to
19761 initialize a static data member with a parenthesized
19762 initializer; only the "=" form of initialization is
19763 permitted.) */
19764 if (!member_p)
19765 cp_parser_parse_tentatively (parser);
19767 /* Consume the `('. */
19768 cp_lexer_consume_token (parser->lexer);
19769 if (first)
19771 /* If this is going to be an abstract declarator, we're
19772 in a declarator and we can't have default args. */
19773 parser->default_arg_ok_p = false;
19774 parser->in_declarator_p = true;
19777 begin_scope (sk_function_parms, NULL_TREE);
19779 /* Parse the parameter-declaration-clause. */
19780 params = cp_parser_parameter_declaration_clause (parser);
19782 /* Consume the `)'. */
19783 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19785 /* If all went well, parse the cv-qualifier-seq,
19786 ref-qualifier and the exception-specification. */
19787 if (member_p || cp_parser_parse_definitely (parser))
19789 cp_cv_quals cv_quals;
19790 cp_virt_specifiers virt_specifiers;
19791 cp_ref_qualifier ref_qual;
19792 tree exception_specification;
19793 tree late_return;
19794 tree attrs;
19795 bool memfn = (member_p || (pushed_scope
19796 && CLASS_TYPE_P (pushed_scope)));
19798 is_declarator = true;
19800 if (ctor_dtor_or_conv_p)
19801 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19802 first = false;
19804 /* Parse the cv-qualifier-seq. */
19805 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19806 /* Parse the ref-qualifier. */
19807 ref_qual = cp_parser_ref_qualifier_opt (parser);
19808 /* Parse the tx-qualifier. */
19809 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19810 /* And the exception-specification. */
19811 exception_specification
19812 = cp_parser_exception_specification_opt (parser);
19814 attrs = cp_parser_std_attribute_spec_seq (parser);
19816 /* In here, we handle cases where attribute is used after
19817 the function declaration. For example:
19818 void func (int x) __attribute__((vector(..))); */
19819 tree gnu_attrs = NULL_TREE;
19820 if (flag_cilkplus
19821 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19823 cp_parser_parse_tentatively (parser);
19824 tree attr = cp_parser_gnu_attributes_opt (parser);
19825 if (cp_lexer_next_token_is_not (parser->lexer,
19826 CPP_SEMICOLON)
19827 && cp_lexer_next_token_is_not (parser->lexer,
19828 CPP_OPEN_BRACE))
19829 cp_parser_abort_tentative_parse (parser);
19830 else if (!cp_parser_parse_definitely (parser))
19832 else
19833 gnu_attrs = attr;
19835 tree requires_clause = NULL_TREE;
19836 late_return = (cp_parser_late_return_type_opt
19837 (parser, declarator, requires_clause,
19838 memfn ? cv_quals : -1));
19840 /* Parse the virt-specifier-seq. */
19841 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19843 /* Create the function-declarator. */
19844 declarator = make_call_declarator (declarator,
19845 params,
19846 cv_quals,
19847 virt_specifiers,
19848 ref_qual,
19849 tx_qual,
19850 exception_specification,
19851 late_return,
19852 requires_clause);
19853 declarator->std_attributes = attrs;
19854 declarator->attributes = gnu_attrs;
19855 /* Any subsequent parameter lists are to do with
19856 return type, so are not those of the declared
19857 function. */
19858 parser->default_arg_ok_p = false;
19861 /* Remove the function parms from scope. */
19862 pop_bindings_and_leave_scope ();
19864 if (is_declarator)
19865 /* Repeat the main loop. */
19866 continue;
19869 /* If this is the first, we can try a parenthesized
19870 declarator. */
19871 if (first)
19873 bool saved_in_type_id_in_expr_p;
19875 parser->default_arg_ok_p = saved_default_arg_ok_p;
19876 parser->in_declarator_p = saved_in_declarator_p;
19878 /* Consume the `('. */
19879 cp_lexer_consume_token (parser->lexer);
19880 /* Parse the nested declarator. */
19881 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19882 parser->in_type_id_in_expr_p = true;
19883 declarator
19884 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19885 /*parenthesized_p=*/NULL,
19886 member_p, friend_p);
19887 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19888 first = false;
19889 /* Expect a `)'. */
19890 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19891 declarator = cp_error_declarator;
19892 if (declarator == cp_error_declarator)
19893 break;
19895 goto handle_declarator;
19897 /* Otherwise, we must be done. */
19898 else
19899 break;
19901 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19902 && token->type == CPP_OPEN_SQUARE
19903 && !cp_next_tokens_can_be_attribute_p (parser))
19905 /* Parse an array-declarator. */
19906 tree bounds, attrs;
19908 if (ctor_dtor_or_conv_p)
19909 *ctor_dtor_or_conv_p = 0;
19911 first = false;
19912 parser->default_arg_ok_p = false;
19913 parser->in_declarator_p = true;
19914 /* Consume the `['. */
19915 cp_lexer_consume_token (parser->lexer);
19916 /* Peek at the next token. */
19917 token = cp_lexer_peek_token (parser->lexer);
19918 /* If the next token is `]', then there is no
19919 constant-expression. */
19920 if (token->type != CPP_CLOSE_SQUARE)
19922 bool non_constant_p;
19923 bounds
19924 = cp_parser_constant_expression (parser,
19925 /*allow_non_constant=*/true,
19926 &non_constant_p);
19927 if (!non_constant_p)
19928 /* OK */;
19929 else if (error_operand_p (bounds))
19930 /* Already gave an error. */;
19931 else if (!parser->in_function_body
19932 || current_binding_level->kind == sk_function_parms)
19934 /* Normally, the array bound must be an integral constant
19935 expression. However, as an extension, we allow VLAs
19936 in function scopes as long as they aren't part of a
19937 parameter declaration. */
19938 cp_parser_error (parser,
19939 "array bound is not an integer constant");
19940 bounds = error_mark_node;
19942 else if (processing_template_decl
19943 && !type_dependent_expression_p (bounds))
19945 /* Remember this wasn't a constant-expression. */
19946 bounds = build_nop (TREE_TYPE (bounds), bounds);
19947 TREE_SIDE_EFFECTS (bounds) = 1;
19950 else
19951 bounds = NULL_TREE;
19952 /* Look for the closing `]'. */
19953 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19955 declarator = cp_error_declarator;
19956 break;
19959 attrs = cp_parser_std_attribute_spec_seq (parser);
19960 declarator = make_array_declarator (declarator, bounds);
19961 declarator->std_attributes = attrs;
19963 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19966 tree qualifying_scope;
19967 tree unqualified_name;
19968 tree attrs;
19969 special_function_kind sfk;
19970 bool abstract_ok;
19971 bool pack_expansion_p = false;
19972 cp_token *declarator_id_start_token;
19974 /* Parse a declarator-id */
19975 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19976 if (abstract_ok)
19978 cp_parser_parse_tentatively (parser);
19980 /* If we see an ellipsis, we should be looking at a
19981 parameter pack. */
19982 if (token->type == CPP_ELLIPSIS)
19984 /* Consume the `...' */
19985 cp_lexer_consume_token (parser->lexer);
19987 pack_expansion_p = true;
19991 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19992 unqualified_name
19993 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19994 qualifying_scope = parser->scope;
19995 if (abstract_ok)
19997 bool okay = false;
19999 if (!unqualified_name && pack_expansion_p)
20001 /* Check whether an error occurred. */
20002 okay = !cp_parser_error_occurred (parser);
20004 /* We already consumed the ellipsis to mark a
20005 parameter pack, but we have no way to report it,
20006 so abort the tentative parse. We will be exiting
20007 immediately anyway. */
20008 cp_parser_abort_tentative_parse (parser);
20010 else
20011 okay = cp_parser_parse_definitely (parser);
20013 if (!okay)
20014 unqualified_name = error_mark_node;
20015 else if (unqualified_name
20016 && (qualifying_scope
20017 || (!identifier_p (unqualified_name))))
20019 cp_parser_error (parser, "expected unqualified-id");
20020 unqualified_name = error_mark_node;
20024 if (!unqualified_name)
20025 return NULL;
20026 if (unqualified_name == error_mark_node)
20028 declarator = cp_error_declarator;
20029 pack_expansion_p = false;
20030 declarator->parameter_pack_p = false;
20031 break;
20034 attrs = cp_parser_std_attribute_spec_seq (parser);
20036 if (qualifying_scope && at_namespace_scope_p ()
20037 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20039 /* In the declaration of a member of a template class
20040 outside of the class itself, the SCOPE will sometimes
20041 be a TYPENAME_TYPE. For example, given:
20043 template <typename T>
20044 int S<T>::R::i = 3;
20046 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20047 this context, we must resolve S<T>::R to an ordinary
20048 type, rather than a typename type.
20050 The reason we normally avoid resolving TYPENAME_TYPEs
20051 is that a specialization of `S' might render
20052 `S<T>::R' not a type. However, if `S' is
20053 specialized, then this `i' will not be used, so there
20054 is no harm in resolving the types here. */
20055 tree type;
20057 /* Resolve the TYPENAME_TYPE. */
20058 type = resolve_typename_type (qualifying_scope,
20059 /*only_current_p=*/false);
20060 /* If that failed, the declarator is invalid. */
20061 if (TREE_CODE (type) == TYPENAME_TYPE)
20063 if (typedef_variant_p (type))
20064 error_at (declarator_id_start_token->location,
20065 "cannot define member of dependent typedef "
20066 "%qT", type);
20067 else
20068 error_at (declarator_id_start_token->location,
20069 "%<%T::%E%> is not a type",
20070 TYPE_CONTEXT (qualifying_scope),
20071 TYPE_IDENTIFIER (qualifying_scope));
20073 qualifying_scope = type;
20076 sfk = sfk_none;
20078 if (unqualified_name)
20080 tree class_type;
20082 if (qualifying_scope
20083 && CLASS_TYPE_P (qualifying_scope))
20084 class_type = qualifying_scope;
20085 else
20086 class_type = current_class_type;
20088 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20090 tree name_type = TREE_TYPE (unqualified_name);
20091 if (class_type && same_type_p (name_type, class_type))
20093 if (qualifying_scope
20094 && CLASSTYPE_USE_TEMPLATE (name_type))
20096 error_at (declarator_id_start_token->location,
20097 "invalid use of constructor as a template");
20098 inform (declarator_id_start_token->location,
20099 "use %<%T::%D%> instead of %<%T::%D%> to "
20100 "name the constructor in a qualified name",
20101 class_type,
20102 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20103 class_type, name_type);
20104 declarator = cp_error_declarator;
20105 break;
20107 else
20108 unqualified_name = constructor_name (class_type);
20110 else
20112 /* We do not attempt to print the declarator
20113 here because we do not have enough
20114 information about its original syntactic
20115 form. */
20116 cp_parser_error (parser, "invalid declarator");
20117 declarator = cp_error_declarator;
20118 break;
20122 if (class_type)
20124 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20125 sfk = sfk_destructor;
20126 else if (identifier_p (unqualified_name)
20127 && IDENTIFIER_CONV_OP_P (unqualified_name))
20128 sfk = sfk_conversion;
20129 else if (/* There's no way to declare a constructor
20130 for an unnamed type, even if the type
20131 got a name for linkage purposes. */
20132 !TYPE_WAS_UNNAMED (class_type)
20133 /* Handle correctly (c++/19200):
20135 struct S {
20136 struct T{};
20137 friend void S(T);
20140 and also:
20142 namespace N {
20143 void S();
20146 struct S {
20147 friend void N::S();
20148 }; */
20149 && !(friend_p
20150 && class_type != qualifying_scope)
20151 && constructor_name_p (unqualified_name,
20152 class_type))
20154 unqualified_name = constructor_name (class_type);
20155 sfk = sfk_constructor;
20157 else if (is_overloaded_fn (unqualified_name)
20158 && DECL_CONSTRUCTOR_P (get_first_fn
20159 (unqualified_name)))
20160 sfk = sfk_constructor;
20162 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20163 *ctor_dtor_or_conv_p = -1;
20166 declarator = make_id_declarator (qualifying_scope,
20167 unqualified_name,
20168 sfk);
20169 declarator->std_attributes = attrs;
20170 declarator->id_loc = token->location;
20171 declarator->parameter_pack_p = pack_expansion_p;
20173 if (pack_expansion_p)
20174 maybe_warn_variadic_templates ();
20177 handle_declarator:;
20178 scope = get_scope_of_declarator (declarator);
20179 if (scope)
20181 /* Any names that appear after the declarator-id for a
20182 member are looked up in the containing scope. */
20183 if (at_function_scope_p ())
20185 /* But declarations with qualified-ids can't appear in a
20186 function. */
20187 cp_parser_error (parser, "qualified-id in declaration");
20188 declarator = cp_error_declarator;
20189 break;
20191 pushed_scope = push_scope (scope);
20193 parser->in_declarator_p = true;
20194 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20195 || (declarator && declarator->kind == cdk_id))
20196 /* Default args are only allowed on function
20197 declarations. */
20198 parser->default_arg_ok_p = saved_default_arg_ok_p;
20199 else
20200 parser->default_arg_ok_p = false;
20202 first = false;
20204 /* We're done. */
20205 else
20206 break;
20209 /* For an abstract declarator, we might wind up with nothing at this
20210 point. That's an error; the declarator is not optional. */
20211 if (!declarator)
20212 cp_parser_error (parser, "expected declarator");
20214 /* If we entered a scope, we must exit it now. */
20215 if (pushed_scope)
20216 pop_scope (pushed_scope);
20218 parser->default_arg_ok_p = saved_default_arg_ok_p;
20219 parser->in_declarator_p = saved_in_declarator_p;
20221 return declarator;
20224 /* Parse a ptr-operator.
20226 ptr-operator:
20227 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20228 * cv-qualifier-seq [opt]
20230 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20231 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20233 GNU Extension:
20235 ptr-operator:
20236 & cv-qualifier-seq [opt]
20238 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20239 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20240 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20241 filled in with the TYPE containing the member. *CV_QUALS is
20242 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20243 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20244 Note that the tree codes returned by this function have nothing
20245 to do with the types of trees that will be eventually be created
20246 to represent the pointer or reference type being parsed. They are
20247 just constants with suggestive names. */
20248 static enum tree_code
20249 cp_parser_ptr_operator (cp_parser* parser,
20250 tree* type,
20251 cp_cv_quals *cv_quals,
20252 tree *attributes)
20254 enum tree_code code = ERROR_MARK;
20255 cp_token *token;
20256 tree attrs = NULL_TREE;
20258 /* Assume that it's not a pointer-to-member. */
20259 *type = NULL_TREE;
20260 /* And that there are no cv-qualifiers. */
20261 *cv_quals = TYPE_UNQUALIFIED;
20263 /* Peek at the next token. */
20264 token = cp_lexer_peek_token (parser->lexer);
20266 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20267 if (token->type == CPP_MULT)
20268 code = INDIRECT_REF;
20269 else if (token->type == CPP_AND)
20270 code = ADDR_EXPR;
20271 else if ((cxx_dialect != cxx98) &&
20272 token->type == CPP_AND_AND) /* C++0x only */
20273 code = NON_LVALUE_EXPR;
20275 if (code != ERROR_MARK)
20277 /* Consume the `*', `&' or `&&'. */
20278 cp_lexer_consume_token (parser->lexer);
20280 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20281 `&', if we are allowing GNU extensions. (The only qualifier
20282 that can legally appear after `&' is `restrict', but that is
20283 enforced during semantic analysis. */
20284 if (code == INDIRECT_REF
20285 || cp_parser_allow_gnu_extensions_p (parser))
20286 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20288 attrs = cp_parser_std_attribute_spec_seq (parser);
20289 if (attributes != NULL)
20290 *attributes = attrs;
20292 else
20294 /* Try the pointer-to-member case. */
20295 cp_parser_parse_tentatively (parser);
20296 /* Look for the optional `::' operator. */
20297 cp_parser_global_scope_opt (parser,
20298 /*current_scope_valid_p=*/false);
20299 /* Look for the nested-name specifier. */
20300 token = cp_lexer_peek_token (parser->lexer);
20301 cp_parser_nested_name_specifier (parser,
20302 /*typename_keyword_p=*/false,
20303 /*check_dependency_p=*/true,
20304 /*type_p=*/false,
20305 /*is_declaration=*/false);
20306 /* If we found it, and the next token is a `*', then we are
20307 indeed looking at a pointer-to-member operator. */
20308 if (!cp_parser_error_occurred (parser)
20309 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20311 /* Indicate that the `*' operator was used. */
20312 code = INDIRECT_REF;
20314 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20315 error_at (token->location, "%qD is a namespace", parser->scope);
20316 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20317 error_at (token->location, "cannot form pointer to member of "
20318 "non-class %q#T", parser->scope);
20319 else
20321 /* The type of which the member is a member is given by the
20322 current SCOPE. */
20323 *type = parser->scope;
20324 /* The next name will not be qualified. */
20325 parser->scope = NULL_TREE;
20326 parser->qualifying_scope = NULL_TREE;
20327 parser->object_scope = NULL_TREE;
20328 /* Look for optional c++11 attributes. */
20329 attrs = cp_parser_std_attribute_spec_seq (parser);
20330 if (attributes != NULL)
20331 *attributes = attrs;
20332 /* Look for the optional cv-qualifier-seq. */
20333 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20336 /* If that didn't work we don't have a ptr-operator. */
20337 if (!cp_parser_parse_definitely (parser))
20338 cp_parser_error (parser, "expected ptr-operator");
20341 return code;
20344 /* Parse an (optional) cv-qualifier-seq.
20346 cv-qualifier-seq:
20347 cv-qualifier cv-qualifier-seq [opt]
20349 cv-qualifier:
20350 const
20351 volatile
20353 GNU Extension:
20355 cv-qualifier:
20356 __restrict__
20358 Returns a bitmask representing the cv-qualifiers. */
20360 static cp_cv_quals
20361 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20363 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20365 while (true)
20367 cp_token *token;
20368 cp_cv_quals cv_qualifier;
20370 /* Peek at the next token. */
20371 token = cp_lexer_peek_token (parser->lexer);
20372 /* See if it's a cv-qualifier. */
20373 switch (token->keyword)
20375 case RID_CONST:
20376 cv_qualifier = TYPE_QUAL_CONST;
20377 break;
20379 case RID_VOLATILE:
20380 cv_qualifier = TYPE_QUAL_VOLATILE;
20381 break;
20383 case RID_RESTRICT:
20384 cv_qualifier = TYPE_QUAL_RESTRICT;
20385 break;
20387 default:
20388 cv_qualifier = TYPE_UNQUALIFIED;
20389 break;
20392 if (!cv_qualifier)
20393 break;
20395 if (cv_quals & cv_qualifier)
20397 gcc_rich_location richloc (token->location);
20398 richloc.add_fixit_remove ();
20399 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20400 cp_lexer_purge_token (parser->lexer);
20402 else
20404 cp_lexer_consume_token (parser->lexer);
20405 cv_quals |= cv_qualifier;
20409 return cv_quals;
20412 /* Parse an (optional) ref-qualifier
20414 ref-qualifier:
20418 Returns cp_ref_qualifier representing ref-qualifier. */
20420 static cp_ref_qualifier
20421 cp_parser_ref_qualifier_opt (cp_parser* parser)
20423 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20425 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20426 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20427 return ref_qual;
20429 while (true)
20431 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20432 cp_token *token = cp_lexer_peek_token (parser->lexer);
20434 switch (token->type)
20436 case CPP_AND:
20437 curr_ref_qual = REF_QUAL_LVALUE;
20438 break;
20440 case CPP_AND_AND:
20441 curr_ref_qual = REF_QUAL_RVALUE;
20442 break;
20444 default:
20445 curr_ref_qual = REF_QUAL_NONE;
20446 break;
20449 if (!curr_ref_qual)
20450 break;
20451 else if (ref_qual)
20453 error_at (token->location, "multiple ref-qualifiers");
20454 cp_lexer_purge_token (parser->lexer);
20456 else
20458 ref_qual = curr_ref_qual;
20459 cp_lexer_consume_token (parser->lexer);
20463 return ref_qual;
20466 /* Parse an optional tx-qualifier.
20468 tx-qualifier:
20469 transaction_safe
20470 transaction_safe_dynamic */
20472 static tree
20473 cp_parser_tx_qualifier_opt (cp_parser *parser)
20475 cp_token *token = cp_lexer_peek_token (parser->lexer);
20476 if (token->type == CPP_NAME)
20478 tree name = token->u.value;
20479 const char *p = IDENTIFIER_POINTER (name);
20480 const int len = strlen ("transaction_safe");
20481 if (!strncmp (p, "transaction_safe", len))
20483 p += len;
20484 if (*p == '\0'
20485 || !strcmp (p, "_dynamic"))
20487 cp_lexer_consume_token (parser->lexer);
20488 if (!flag_tm)
20490 error ("%qE requires %<-fgnu-tm%>", name);
20491 return NULL_TREE;
20493 else
20494 return name;
20498 return NULL_TREE;
20501 /* Parse an (optional) virt-specifier-seq.
20503 virt-specifier-seq:
20504 virt-specifier virt-specifier-seq [opt]
20506 virt-specifier:
20507 override
20508 final
20510 Returns a bitmask representing the virt-specifiers. */
20512 static cp_virt_specifiers
20513 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20515 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20517 while (true)
20519 cp_token *token;
20520 cp_virt_specifiers virt_specifier;
20522 /* Peek at the next token. */
20523 token = cp_lexer_peek_token (parser->lexer);
20524 /* See if it's a virt-specifier-qualifier. */
20525 if (token->type != CPP_NAME)
20526 break;
20527 if (id_equal (token->u.value, "override"))
20529 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20530 virt_specifier = VIRT_SPEC_OVERRIDE;
20532 else if (id_equal (token->u.value, "final"))
20534 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20535 virt_specifier = VIRT_SPEC_FINAL;
20537 else if (id_equal (token->u.value, "__final"))
20539 virt_specifier = VIRT_SPEC_FINAL;
20541 else
20542 break;
20544 if (virt_specifiers & virt_specifier)
20546 gcc_rich_location richloc (token->location);
20547 richloc.add_fixit_remove ();
20548 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20549 cp_lexer_purge_token (parser->lexer);
20551 else
20553 cp_lexer_consume_token (parser->lexer);
20554 virt_specifiers |= virt_specifier;
20557 return virt_specifiers;
20560 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20561 is in scope even though it isn't real. */
20563 void
20564 inject_this_parameter (tree ctype, cp_cv_quals quals)
20566 tree this_parm;
20568 if (current_class_ptr)
20570 /* We don't clear this between NSDMIs. Is it already what we want? */
20571 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20572 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20573 && cp_type_quals (type) == quals)
20574 return;
20577 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20578 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20579 current_class_ptr = NULL_TREE;
20580 current_class_ref
20581 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20582 current_class_ptr = this_parm;
20585 /* Return true iff our current scope is a non-static data member
20586 initializer. */
20588 bool
20589 parsing_nsdmi (void)
20591 /* We recognize NSDMI context by the context-less 'this' pointer set up
20592 by the function above. */
20593 if (current_class_ptr
20594 && TREE_CODE (current_class_ptr) == PARM_DECL
20595 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20596 return true;
20597 return false;
20600 /* Return true iff our current scope is a default capturing generic lambda
20601 defined within a template. FIXME: This is part of a workaround (see
20602 semantics.c) to handle building lambda closure types correctly in templates
20603 which we ultimately want to defer to instantiation time. */
20605 bool
20606 parsing_default_capturing_generic_lambda_in_template (void)
20608 if (!processing_template_decl || !current_class_type)
20609 return false;
20611 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20612 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20613 return false;
20615 tree callop = lambda_function (lam);
20616 if (!callop)
20617 return false;
20619 return (DECL_TEMPLATE_INFO (callop)
20620 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20621 && ((current_nonlambda_class_type ()
20622 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20623 || ((current_nonlambda_function ()
20624 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20627 /* Parse a late-specified return type, if any. This is not a separate
20628 non-terminal, but part of a function declarator, which looks like
20630 -> trailing-type-specifier-seq abstract-declarator(opt)
20632 Returns the type indicated by the type-id.
20634 In addition to this, parse any queued up #pragma omp declare simd
20635 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20636 #pragma acc routine clauses.
20638 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20639 function. */
20641 static tree
20642 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20643 tree& requires_clause, cp_cv_quals quals)
20645 cp_token *token;
20646 tree type = NULL_TREE;
20647 bool declare_simd_p = (parser->omp_declare_simd
20648 && declarator
20649 && declarator->kind == cdk_id);
20651 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20652 && declarator && declarator->kind == cdk_id);
20654 bool oacc_routine_p = (parser->oacc_routine
20655 && declarator
20656 && declarator->kind == cdk_id);
20658 /* Peek at the next token. */
20659 token = cp_lexer_peek_token (parser->lexer);
20660 /* A late-specified return type is indicated by an initial '->'. */
20661 if (token->type != CPP_DEREF
20662 && token->keyword != RID_REQUIRES
20663 && !(token->type == CPP_NAME
20664 && token->u.value == ridpointers[RID_REQUIRES])
20665 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20666 return NULL_TREE;
20668 tree save_ccp = current_class_ptr;
20669 tree save_ccr = current_class_ref;
20670 if (quals >= 0)
20672 /* DR 1207: 'this' is in scope in the trailing return type. */
20673 inject_this_parameter (current_class_type, quals);
20676 if (token->type == CPP_DEREF)
20678 /* Consume the ->. */
20679 cp_lexer_consume_token (parser->lexer);
20681 type = cp_parser_trailing_type_id (parser);
20684 /* Function declarations may be followed by a trailing
20685 requires-clause. */
20686 requires_clause = cp_parser_requires_clause_opt (parser);
20688 if (cilk_simd_fn_vector_p)
20689 declarator->attributes
20690 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20691 declarator->attributes);
20692 if (declare_simd_p)
20693 declarator->attributes
20694 = cp_parser_late_parsing_omp_declare_simd (parser,
20695 declarator->attributes);
20696 if (oacc_routine_p)
20697 declarator->attributes
20698 = cp_parser_late_parsing_oacc_routine (parser,
20699 declarator->attributes);
20701 if (quals >= 0)
20703 current_class_ptr = save_ccp;
20704 current_class_ref = save_ccr;
20707 return type;
20710 /* Parse a declarator-id.
20712 declarator-id:
20713 id-expression
20714 :: [opt] nested-name-specifier [opt] type-name
20716 In the `id-expression' case, the value returned is as for
20717 cp_parser_id_expression if the id-expression was an unqualified-id.
20718 If the id-expression was a qualified-id, then a SCOPE_REF is
20719 returned. The first operand is the scope (either a NAMESPACE_DECL
20720 or TREE_TYPE), but the second is still just a representation of an
20721 unqualified-id. */
20723 static tree
20724 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20726 tree id;
20727 /* The expression must be an id-expression. Assume that qualified
20728 names are the names of types so that:
20730 template <class T>
20731 int S<T>::R::i = 3;
20733 will work; we must treat `S<T>::R' as the name of a type.
20734 Similarly, assume that qualified names are templates, where
20735 required, so that:
20737 template <class T>
20738 int S<T>::R<T>::i = 3;
20740 will work, too. */
20741 id = cp_parser_id_expression (parser,
20742 /*template_keyword_p=*/false,
20743 /*check_dependency_p=*/false,
20744 /*template_p=*/NULL,
20745 /*declarator_p=*/true,
20746 optional_p);
20747 if (id && BASELINK_P (id))
20748 id = BASELINK_FUNCTIONS (id);
20749 return id;
20752 /* Parse a type-id.
20754 type-id:
20755 type-specifier-seq abstract-declarator [opt]
20757 Returns the TYPE specified. */
20759 static tree
20760 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20761 bool is_trailing_return)
20763 cp_decl_specifier_seq type_specifier_seq;
20764 cp_declarator *abstract_declarator;
20766 /* Parse the type-specifier-seq. */
20767 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20768 is_trailing_return,
20769 &type_specifier_seq);
20770 if (is_template_arg && type_specifier_seq.type
20771 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20772 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20773 /* A bare template name as a template argument is a template template
20774 argument, not a placeholder, so fail parsing it as a type argument. */
20776 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20777 cp_parser_simulate_error (parser);
20778 return error_mark_node;
20780 if (type_specifier_seq.type == error_mark_node)
20781 return error_mark_node;
20783 /* There might or might not be an abstract declarator. */
20784 cp_parser_parse_tentatively (parser);
20785 /* Look for the declarator. */
20786 abstract_declarator
20787 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20788 /*parenthesized_p=*/NULL,
20789 /*member_p=*/false,
20790 /*friend_p=*/false);
20791 /* Check to see if there really was a declarator. */
20792 if (!cp_parser_parse_definitely (parser))
20793 abstract_declarator = NULL;
20795 if (type_specifier_seq.type
20796 /* The concepts TS allows 'auto' as a type-id. */
20797 && (!flag_concepts || parser->in_type_id_in_expr_p)
20798 /* None of the valid uses of 'auto' in C++14 involve the type-id
20799 nonterminal, but it is valid in a trailing-return-type. */
20800 && !(cxx_dialect >= cxx14 && is_trailing_return))
20801 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20803 /* A type-id with type 'auto' is only ok if the abstract declarator
20804 is a function declarator with a late-specified return type.
20806 A type-id with 'auto' is also valid in a trailing-return-type
20807 in a compound-requirement. */
20808 if (abstract_declarator
20809 && abstract_declarator->kind == cdk_function
20810 && abstract_declarator->u.function.late_return_type)
20811 /* OK */;
20812 else if (parser->in_result_type_constraint_p)
20813 /* OK */;
20814 else
20816 location_t loc = type_specifier_seq.locations[ds_type_spec];
20817 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20819 error_at (loc, "missing template arguments after %qT",
20820 auto_node);
20821 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20822 tmpl);
20824 else
20825 error_at (loc, "invalid use of %qT", auto_node);
20826 return error_mark_node;
20830 return groktypename (&type_specifier_seq, abstract_declarator,
20831 is_template_arg);
20834 static tree
20835 cp_parser_type_id (cp_parser *parser)
20837 return cp_parser_type_id_1 (parser, false, false);
20840 static tree
20841 cp_parser_template_type_arg (cp_parser *parser)
20843 tree r;
20844 const char *saved_message = parser->type_definition_forbidden_message;
20845 parser->type_definition_forbidden_message
20846 = G_("types may not be defined in template arguments");
20847 r = cp_parser_type_id_1 (parser, true, false);
20848 parser->type_definition_forbidden_message = saved_message;
20849 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20851 error ("invalid use of %<auto%> in template argument");
20852 r = error_mark_node;
20854 return r;
20857 static tree
20858 cp_parser_trailing_type_id (cp_parser *parser)
20860 return cp_parser_type_id_1 (parser, false, true);
20863 /* Parse a type-specifier-seq.
20865 type-specifier-seq:
20866 type-specifier type-specifier-seq [opt]
20868 GNU extension:
20870 type-specifier-seq:
20871 attributes type-specifier-seq [opt]
20873 If IS_DECLARATION is true, we are at the start of a "condition" or
20874 exception-declaration, so we might be followed by a declarator-id.
20876 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20877 i.e. we've just seen "->".
20879 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20881 static void
20882 cp_parser_type_specifier_seq (cp_parser* parser,
20883 bool is_declaration,
20884 bool is_trailing_return,
20885 cp_decl_specifier_seq *type_specifier_seq)
20887 bool seen_type_specifier = false;
20888 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20889 cp_token *start_token = NULL;
20891 /* Clear the TYPE_SPECIFIER_SEQ. */
20892 clear_decl_specs (type_specifier_seq);
20894 /* In the context of a trailing return type, enum E { } is an
20895 elaborated-type-specifier followed by a function-body, not an
20896 enum-specifier. */
20897 if (is_trailing_return)
20898 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20900 /* Parse the type-specifiers and attributes. */
20901 while (true)
20903 tree type_specifier;
20904 bool is_cv_qualifier;
20906 /* Check for attributes first. */
20907 if (cp_next_tokens_can_be_attribute_p (parser))
20909 type_specifier_seq->attributes =
20910 chainon (type_specifier_seq->attributes,
20911 cp_parser_attributes_opt (parser));
20912 continue;
20915 /* record the token of the beginning of the type specifier seq,
20916 for error reporting purposes*/
20917 if (!start_token)
20918 start_token = cp_lexer_peek_token (parser->lexer);
20920 /* Look for the type-specifier. */
20921 type_specifier = cp_parser_type_specifier (parser,
20922 flags,
20923 type_specifier_seq,
20924 /*is_declaration=*/false,
20925 NULL,
20926 &is_cv_qualifier);
20927 if (!type_specifier)
20929 /* If the first type-specifier could not be found, this is not a
20930 type-specifier-seq at all. */
20931 if (!seen_type_specifier)
20933 /* Set in_declarator_p to avoid skipping to the semicolon. */
20934 int in_decl = parser->in_declarator_p;
20935 parser->in_declarator_p = true;
20937 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20938 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20939 cp_parser_error (parser, "expected type-specifier");
20941 parser->in_declarator_p = in_decl;
20943 type_specifier_seq->type = error_mark_node;
20944 return;
20946 /* If subsequent type-specifiers could not be found, the
20947 type-specifier-seq is complete. */
20948 break;
20951 seen_type_specifier = true;
20952 /* The standard says that a condition can be:
20954 type-specifier-seq declarator = assignment-expression
20956 However, given:
20958 struct S {};
20959 if (int S = ...)
20961 we should treat the "S" as a declarator, not as a
20962 type-specifier. The standard doesn't say that explicitly for
20963 type-specifier-seq, but it does say that for
20964 decl-specifier-seq in an ordinary declaration. Perhaps it
20965 would be clearer just to allow a decl-specifier-seq here, and
20966 then add a semantic restriction that if any decl-specifiers
20967 that are not type-specifiers appear, the program is invalid. */
20968 if (is_declaration && !is_cv_qualifier)
20969 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20973 /* Return whether the function currently being declared has an associated
20974 template parameter list. */
20976 static bool
20977 function_being_declared_is_template_p (cp_parser* parser)
20979 if (!current_template_parms || processing_template_parmlist)
20980 return false;
20982 if (parser->implicit_template_scope)
20983 return true;
20985 if (at_class_scope_p ()
20986 && TYPE_BEING_DEFINED (current_class_type))
20987 return parser->num_template_parameter_lists != 0;
20989 return ((int) parser->num_template_parameter_lists > template_class_depth
20990 (current_class_type));
20993 /* Parse a parameter-declaration-clause.
20995 parameter-declaration-clause:
20996 parameter-declaration-list [opt] ... [opt]
20997 parameter-declaration-list , ...
20999 Returns a representation for the parameter declarations. A return
21000 value of NULL indicates a parameter-declaration-clause consisting
21001 only of an ellipsis. */
21003 static tree
21004 cp_parser_parameter_declaration_clause (cp_parser* parser)
21006 tree parameters;
21007 cp_token *token;
21008 bool ellipsis_p;
21009 bool is_error;
21011 struct cleanup {
21012 cp_parser* parser;
21013 int auto_is_implicit_function_template_parm_p;
21014 ~cleanup() {
21015 parser->auto_is_implicit_function_template_parm_p
21016 = auto_is_implicit_function_template_parm_p;
21018 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21020 (void) cleanup;
21022 if (!processing_specialization
21023 && !processing_template_parmlist
21024 && !processing_explicit_instantiation)
21025 if (!current_function_decl
21026 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21027 parser->auto_is_implicit_function_template_parm_p = true;
21029 /* Peek at the next token. */
21030 token = cp_lexer_peek_token (parser->lexer);
21031 /* Check for trivial parameter-declaration-clauses. */
21032 if (token->type == CPP_ELLIPSIS)
21034 /* Consume the `...' token. */
21035 cp_lexer_consume_token (parser->lexer);
21036 return NULL_TREE;
21038 else if (token->type == CPP_CLOSE_PAREN)
21039 /* There are no parameters. */
21041 #ifndef NO_IMPLICIT_EXTERN_C
21042 if (in_system_header_at (input_location)
21043 && current_class_type == NULL
21044 && current_lang_name == lang_name_c)
21045 return NULL_TREE;
21046 else
21047 #endif
21048 return void_list_node;
21050 /* Check for `(void)', too, which is a special case. */
21051 else if (token->keyword == RID_VOID
21052 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21053 == CPP_CLOSE_PAREN))
21055 /* Consume the `void' token. */
21056 cp_lexer_consume_token (parser->lexer);
21057 /* There are no parameters. */
21058 return void_list_node;
21061 /* Parse the parameter-declaration-list. */
21062 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21063 /* If a parse error occurred while parsing the
21064 parameter-declaration-list, then the entire
21065 parameter-declaration-clause is erroneous. */
21066 if (is_error)
21067 return NULL;
21069 /* Peek at the next token. */
21070 token = cp_lexer_peek_token (parser->lexer);
21071 /* If it's a `,', the clause should terminate with an ellipsis. */
21072 if (token->type == CPP_COMMA)
21074 /* Consume the `,'. */
21075 cp_lexer_consume_token (parser->lexer);
21076 /* Expect an ellipsis. */
21077 ellipsis_p
21078 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21080 /* It might also be `...' if the optional trailing `,' was
21081 omitted. */
21082 else if (token->type == CPP_ELLIPSIS)
21084 /* Consume the `...' token. */
21085 cp_lexer_consume_token (parser->lexer);
21086 /* And remember that we saw it. */
21087 ellipsis_p = true;
21089 else
21090 ellipsis_p = false;
21092 /* Finish the parameter list. */
21093 if (!ellipsis_p)
21094 parameters = chainon (parameters, void_list_node);
21096 return parameters;
21099 /* Parse a parameter-declaration-list.
21101 parameter-declaration-list:
21102 parameter-declaration
21103 parameter-declaration-list , parameter-declaration
21105 Returns a representation of the parameter-declaration-list, as for
21106 cp_parser_parameter_declaration_clause. However, the
21107 `void_list_node' is never appended to the list. Upon return,
21108 *IS_ERROR will be true iff an error occurred. */
21110 static tree
21111 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21113 tree parameters = NULL_TREE;
21114 tree *tail = &parameters;
21115 bool saved_in_unbraced_linkage_specification_p;
21116 int index = 0;
21118 /* Assume all will go well. */
21119 *is_error = false;
21120 /* The special considerations that apply to a function within an
21121 unbraced linkage specifications do not apply to the parameters
21122 to the function. */
21123 saved_in_unbraced_linkage_specification_p
21124 = parser->in_unbraced_linkage_specification_p;
21125 parser->in_unbraced_linkage_specification_p = false;
21127 /* Look for more parameters. */
21128 while (true)
21130 cp_parameter_declarator *parameter;
21131 tree decl = error_mark_node;
21132 bool parenthesized_p = false;
21133 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21134 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21135 (current_template_parms)) : 0);
21137 /* Parse the parameter. */
21138 parameter
21139 = cp_parser_parameter_declaration (parser,
21140 /*template_parm_p=*/false,
21141 &parenthesized_p);
21143 /* We don't know yet if the enclosing context is deprecated, so wait
21144 and warn in grokparms if appropriate. */
21145 deprecated_state = DEPRECATED_SUPPRESS;
21147 if (parameter)
21149 /* If a function parameter pack was specified and an implicit template
21150 parameter was introduced during cp_parser_parameter_declaration,
21151 change any implicit parameters introduced into packs. */
21152 if (parser->implicit_template_parms
21153 && parameter->declarator
21154 && parameter->declarator->parameter_pack_p)
21156 int latest_template_parm_idx = TREE_VEC_LENGTH
21157 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21159 if (latest_template_parm_idx != template_parm_idx)
21160 parameter->decl_specifiers.type = convert_generic_types_to_packs
21161 (parameter->decl_specifiers.type,
21162 template_parm_idx, latest_template_parm_idx);
21165 decl = grokdeclarator (parameter->declarator,
21166 &parameter->decl_specifiers,
21167 PARM,
21168 parameter->default_argument != NULL_TREE,
21169 &parameter->decl_specifiers.attributes);
21172 deprecated_state = DEPRECATED_NORMAL;
21174 /* If a parse error occurred parsing the parameter declaration,
21175 then the entire parameter-declaration-list is erroneous. */
21176 if (decl == error_mark_node)
21178 *is_error = true;
21179 parameters = error_mark_node;
21180 break;
21183 if (parameter->decl_specifiers.attributes)
21184 cplus_decl_attributes (&decl,
21185 parameter->decl_specifiers.attributes,
21187 if (DECL_NAME (decl))
21188 decl = pushdecl (decl);
21190 if (decl != error_mark_node)
21192 retrofit_lang_decl (decl);
21193 DECL_PARM_INDEX (decl) = ++index;
21194 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21197 /* Add the new parameter to the list. */
21198 *tail = build_tree_list (parameter->default_argument, decl);
21199 tail = &TREE_CHAIN (*tail);
21201 /* Peek at the next token. */
21202 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21203 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21204 /* These are for Objective-C++ */
21205 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21206 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21207 /* The parameter-declaration-list is complete. */
21208 break;
21209 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21211 cp_token *token;
21213 /* Peek at the next token. */
21214 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21215 /* If it's an ellipsis, then the list is complete. */
21216 if (token->type == CPP_ELLIPSIS)
21217 break;
21218 /* Otherwise, there must be more parameters. Consume the
21219 `,'. */
21220 cp_lexer_consume_token (parser->lexer);
21221 /* When parsing something like:
21223 int i(float f, double d)
21225 we can tell after seeing the declaration for "f" that we
21226 are not looking at an initialization of a variable "i",
21227 but rather at the declaration of a function "i".
21229 Due to the fact that the parsing of template arguments
21230 (as specified to a template-id) requires backtracking we
21231 cannot use this technique when inside a template argument
21232 list. */
21233 if (!parser->in_template_argument_list_p
21234 && !parser->in_type_id_in_expr_p
21235 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21236 /* However, a parameter-declaration of the form
21237 "float(f)" (which is a valid declaration of a
21238 parameter "f") can also be interpreted as an
21239 expression (the conversion of "f" to "float"). */
21240 && !parenthesized_p)
21241 cp_parser_commit_to_tentative_parse (parser);
21243 else
21245 cp_parser_error (parser, "expected %<,%> or %<...%>");
21246 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21247 cp_parser_skip_to_closing_parenthesis (parser,
21248 /*recovering=*/true,
21249 /*or_comma=*/false,
21250 /*consume_paren=*/false);
21251 break;
21255 parser->in_unbraced_linkage_specification_p
21256 = saved_in_unbraced_linkage_specification_p;
21258 /* Reset implicit_template_scope if we are about to leave the function
21259 parameter list that introduced it. Note that for out-of-line member
21260 definitions, there will be one or more class scopes before we get to
21261 the template parameter scope. */
21263 if (cp_binding_level *its = parser->implicit_template_scope)
21264 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21266 while (maybe_its->kind == sk_class)
21267 maybe_its = maybe_its->level_chain;
21268 if (maybe_its == its)
21270 parser->implicit_template_parms = 0;
21271 parser->implicit_template_scope = 0;
21275 return parameters;
21278 /* Parse a parameter declaration.
21280 parameter-declaration:
21281 decl-specifier-seq ... [opt] declarator
21282 decl-specifier-seq declarator = assignment-expression
21283 decl-specifier-seq ... [opt] abstract-declarator [opt]
21284 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21286 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21287 declares a template parameter. (In that case, a non-nested `>'
21288 token encountered during the parsing of the assignment-expression
21289 is not interpreted as a greater-than operator.)
21291 Returns a representation of the parameter, or NULL if an error
21292 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21293 true iff the declarator is of the form "(p)". */
21295 static cp_parameter_declarator *
21296 cp_parser_parameter_declaration (cp_parser *parser,
21297 bool template_parm_p,
21298 bool *parenthesized_p)
21300 int declares_class_or_enum;
21301 cp_decl_specifier_seq decl_specifiers;
21302 cp_declarator *declarator;
21303 tree default_argument;
21304 cp_token *token = NULL, *declarator_token_start = NULL;
21305 const char *saved_message;
21306 bool template_parameter_pack_p = false;
21308 /* In a template parameter, `>' is not an operator.
21310 [temp.param]
21312 When parsing a default template-argument for a non-type
21313 template-parameter, the first non-nested `>' is taken as the end
21314 of the template parameter-list rather than a greater-than
21315 operator. */
21317 /* Type definitions may not appear in parameter types. */
21318 saved_message = parser->type_definition_forbidden_message;
21319 parser->type_definition_forbidden_message
21320 = G_("types may not be defined in parameter types");
21322 /* Parse the declaration-specifiers. */
21323 cp_parser_decl_specifier_seq (parser,
21324 CP_PARSER_FLAGS_NONE,
21325 &decl_specifiers,
21326 &declares_class_or_enum);
21328 /* Complain about missing 'typename' or other invalid type names. */
21329 if (!decl_specifiers.any_type_specifiers_p
21330 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21331 decl_specifiers.type = error_mark_node;
21333 /* If an error occurred, there's no reason to attempt to parse the
21334 rest of the declaration. */
21335 if (cp_parser_error_occurred (parser))
21337 parser->type_definition_forbidden_message = saved_message;
21338 return NULL;
21341 /* Peek at the next token. */
21342 token = cp_lexer_peek_token (parser->lexer);
21344 /* If the next token is a `)', `,', `=', `>', or `...', then there
21345 is no declarator. However, when variadic templates are enabled,
21346 there may be a declarator following `...'. */
21347 if (token->type == CPP_CLOSE_PAREN
21348 || token->type == CPP_COMMA
21349 || token->type == CPP_EQ
21350 || token->type == CPP_GREATER)
21352 declarator = NULL;
21353 if (parenthesized_p)
21354 *parenthesized_p = false;
21356 /* Otherwise, there should be a declarator. */
21357 else
21359 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21360 parser->default_arg_ok_p = false;
21362 /* After seeing a decl-specifier-seq, if the next token is not a
21363 "(", there is no possibility that the code is a valid
21364 expression. Therefore, if parsing tentatively, we commit at
21365 this point. */
21366 if (!parser->in_template_argument_list_p
21367 /* In an expression context, having seen:
21369 (int((char ...
21371 we cannot be sure whether we are looking at a
21372 function-type (taking a "char" as a parameter) or a cast
21373 of some object of type "char" to "int". */
21374 && !parser->in_type_id_in_expr_p
21375 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21376 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21377 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21378 cp_parser_commit_to_tentative_parse (parser);
21379 /* Parse the declarator. */
21380 declarator_token_start = token;
21381 declarator = cp_parser_declarator (parser,
21382 CP_PARSER_DECLARATOR_EITHER,
21383 /*ctor_dtor_or_conv_p=*/NULL,
21384 parenthesized_p,
21385 /*member_p=*/false,
21386 /*friend_p=*/false);
21387 parser->default_arg_ok_p = saved_default_arg_ok_p;
21388 /* After the declarator, allow more attributes. */
21389 decl_specifiers.attributes
21390 = chainon (decl_specifiers.attributes,
21391 cp_parser_attributes_opt (parser));
21393 /* If the declarator is a template parameter pack, remember that and
21394 clear the flag in the declarator itself so we don't get errors
21395 from grokdeclarator. */
21396 if (template_parm_p && declarator && declarator->parameter_pack_p)
21398 declarator->parameter_pack_p = false;
21399 template_parameter_pack_p = true;
21403 /* If the next token is an ellipsis, and we have not seen a declarator
21404 name, and if either the type of the declarator contains parameter
21405 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21406 for, eg, abbreviated integral type names), then we actually have a
21407 parameter pack expansion expression. Otherwise, leave the ellipsis
21408 for a C-style variadic function. */
21409 token = cp_lexer_peek_token (parser->lexer);
21410 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21412 tree type = decl_specifiers.type;
21414 if (type && DECL_P (type))
21415 type = TREE_TYPE (type);
21417 if (((type
21418 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21419 && (template_parm_p || uses_parameter_packs (type)))
21420 || (!type && template_parm_p))
21421 && declarator_can_be_parameter_pack (declarator))
21423 /* Consume the `...'. */
21424 cp_lexer_consume_token (parser->lexer);
21425 maybe_warn_variadic_templates ();
21427 /* Build a pack expansion type */
21428 if (template_parm_p)
21429 template_parameter_pack_p = true;
21430 else if (declarator)
21431 declarator->parameter_pack_p = true;
21432 else
21433 decl_specifiers.type = make_pack_expansion (type);
21437 /* The restriction on defining new types applies only to the type
21438 of the parameter, not to the default argument. */
21439 parser->type_definition_forbidden_message = saved_message;
21441 /* If the next token is `=', then process a default argument. */
21442 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21444 tree type = decl_specifiers.type;
21445 token = cp_lexer_peek_token (parser->lexer);
21446 /* If we are defining a class, then the tokens that make up the
21447 default argument must be saved and processed later. */
21448 if (!template_parm_p && at_class_scope_p ()
21449 && TYPE_BEING_DEFINED (current_class_type)
21450 && !LAMBDA_TYPE_P (current_class_type))
21451 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21453 // A constrained-type-specifier may declare a type template-parameter.
21454 else if (declares_constrained_type_template_parameter (type))
21455 default_argument
21456 = cp_parser_default_type_template_argument (parser);
21458 // A constrained-type-specifier may declare a template-template-parameter.
21459 else if (declares_constrained_template_template_parameter (type))
21460 default_argument
21461 = cp_parser_default_template_template_argument (parser);
21463 /* Outside of a class definition, we can just parse the
21464 assignment-expression. */
21465 else
21466 default_argument
21467 = cp_parser_default_argument (parser, template_parm_p);
21469 if (!parser->default_arg_ok_p)
21471 permerror (token->location,
21472 "default arguments are only "
21473 "permitted for function parameters");
21475 else if ((declarator && declarator->parameter_pack_p)
21476 || template_parameter_pack_p
21477 || (decl_specifiers.type
21478 && PACK_EXPANSION_P (decl_specifiers.type)))
21480 /* Find the name of the parameter pack. */
21481 cp_declarator *id_declarator = declarator;
21482 while (id_declarator && id_declarator->kind != cdk_id)
21483 id_declarator = id_declarator->declarator;
21485 if (id_declarator && id_declarator->kind == cdk_id)
21486 error_at (declarator_token_start->location,
21487 template_parm_p
21488 ? G_("template parameter pack %qD "
21489 "cannot have a default argument")
21490 : G_("parameter pack %qD cannot have "
21491 "a default argument"),
21492 id_declarator->u.id.unqualified_name);
21493 else
21494 error_at (declarator_token_start->location,
21495 template_parm_p
21496 ? G_("template parameter pack cannot have "
21497 "a default argument")
21498 : G_("parameter pack cannot have a "
21499 "default argument"));
21501 default_argument = NULL_TREE;
21504 else
21505 default_argument = NULL_TREE;
21507 return make_parameter_declarator (&decl_specifiers,
21508 declarator,
21509 default_argument,
21510 template_parameter_pack_p);
21513 /* Parse a default argument and return it.
21515 TEMPLATE_PARM_P is true if this is a default argument for a
21516 non-type template parameter. */
21517 static tree
21518 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21520 tree default_argument = NULL_TREE;
21521 bool saved_greater_than_is_operator_p;
21522 bool saved_local_variables_forbidden_p;
21523 bool non_constant_p, is_direct_init;
21525 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21526 set correctly. */
21527 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21528 parser->greater_than_is_operator_p = !template_parm_p;
21529 /* Local variable names (and the `this' keyword) may not
21530 appear in a default argument. */
21531 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21532 parser->local_variables_forbidden_p = true;
21533 /* Parse the assignment-expression. */
21534 if (template_parm_p)
21535 push_deferring_access_checks (dk_no_deferred);
21536 tree saved_class_ptr = NULL_TREE;
21537 tree saved_class_ref = NULL_TREE;
21538 /* The "this" pointer is not valid in a default argument. */
21539 if (cfun)
21541 saved_class_ptr = current_class_ptr;
21542 cp_function_chain->x_current_class_ptr = NULL_TREE;
21543 saved_class_ref = current_class_ref;
21544 cp_function_chain->x_current_class_ref = NULL_TREE;
21546 default_argument
21547 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21548 /* Restore the "this" pointer. */
21549 if (cfun)
21551 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21552 cp_function_chain->x_current_class_ref = saved_class_ref;
21554 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21555 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21556 if (template_parm_p)
21557 pop_deferring_access_checks ();
21558 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21559 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21561 return default_argument;
21564 /* Parse a function-body.
21566 function-body:
21567 compound_statement */
21569 static void
21570 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21572 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21573 ? BCS_TRY_BLOCK : BCS_NORMAL),
21574 true);
21577 /* Parse a ctor-initializer-opt followed by a function-body. Return
21578 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21579 is true we are parsing a function-try-block. */
21581 static bool
21582 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21583 bool in_function_try_block)
21585 tree body, list;
21586 bool ctor_initializer_p;
21587 const bool check_body_p =
21588 DECL_CONSTRUCTOR_P (current_function_decl)
21589 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21590 tree last = NULL;
21592 /* Begin the function body. */
21593 body = begin_function_body ();
21594 /* Parse the optional ctor-initializer. */
21595 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21597 /* If we're parsing a constexpr constructor definition, we need
21598 to check that the constructor body is indeed empty. However,
21599 before we get to cp_parser_function_body lot of junk has been
21600 generated, so we can't just check that we have an empty block.
21601 Rather we take a snapshot of the outermost block, and check whether
21602 cp_parser_function_body changed its state. */
21603 if (check_body_p)
21605 list = cur_stmt_list;
21606 if (STATEMENT_LIST_TAIL (list))
21607 last = STATEMENT_LIST_TAIL (list)->stmt;
21609 /* Parse the function-body. */
21610 cp_parser_function_body (parser, in_function_try_block);
21611 if (check_body_p)
21612 check_constexpr_ctor_body (last, list, /*complain=*/true);
21613 /* Finish the function body. */
21614 finish_function_body (body);
21616 return ctor_initializer_p;
21619 /* Parse an initializer.
21621 initializer:
21622 = initializer-clause
21623 ( expression-list )
21625 Returns an expression representing the initializer. If no
21626 initializer is present, NULL_TREE is returned.
21628 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21629 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21630 set to TRUE if there is no initializer present. If there is an
21631 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21632 is set to true; otherwise it is set to false. */
21634 static tree
21635 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21636 bool* non_constant_p)
21638 cp_token *token;
21639 tree init;
21641 /* Peek at the next token. */
21642 token = cp_lexer_peek_token (parser->lexer);
21644 /* Let our caller know whether or not this initializer was
21645 parenthesized. */
21646 *is_direct_init = (token->type != CPP_EQ);
21647 /* Assume that the initializer is constant. */
21648 *non_constant_p = false;
21650 if (token->type == CPP_EQ)
21652 /* Consume the `='. */
21653 cp_lexer_consume_token (parser->lexer);
21654 /* Parse the initializer-clause. */
21655 init = cp_parser_initializer_clause (parser, non_constant_p);
21657 else if (token->type == CPP_OPEN_PAREN)
21659 vec<tree, va_gc> *vec;
21660 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21661 /*cast_p=*/false,
21662 /*allow_expansion_p=*/true,
21663 non_constant_p);
21664 if (vec == NULL)
21665 return error_mark_node;
21666 init = build_tree_list_vec (vec);
21667 release_tree_vector (vec);
21669 else if (token->type == CPP_OPEN_BRACE)
21671 cp_lexer_set_source_position (parser->lexer);
21672 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21673 init = cp_parser_braced_list (parser, non_constant_p);
21674 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21676 else
21678 /* Anything else is an error. */
21679 cp_parser_error (parser, "expected initializer");
21680 init = error_mark_node;
21683 if (check_for_bare_parameter_packs (init))
21684 init = error_mark_node;
21686 return init;
21689 /* Parse an initializer-clause.
21691 initializer-clause:
21692 assignment-expression
21693 braced-init-list
21695 Returns an expression representing the initializer.
21697 If the `assignment-expression' production is used the value
21698 returned is simply a representation for the expression.
21700 Otherwise, calls cp_parser_braced_list. */
21702 static cp_expr
21703 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21705 cp_expr initializer;
21707 /* Assume the expression is constant. */
21708 *non_constant_p = false;
21710 /* If it is not a `{', then we are looking at an
21711 assignment-expression. */
21712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21714 initializer
21715 = cp_parser_constant_expression (parser,
21716 /*allow_non_constant_p=*/true,
21717 non_constant_p);
21719 else
21720 initializer = cp_parser_braced_list (parser, non_constant_p);
21722 return initializer;
21725 /* Parse a brace-enclosed initializer list.
21727 braced-init-list:
21728 { initializer-list , [opt] }
21731 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21732 the elements of the initializer-list (or NULL, if the last
21733 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21734 NULL_TREE. There is no way to detect whether or not the optional
21735 trailing `,' was provided. NON_CONSTANT_P is as for
21736 cp_parser_initializer. */
21738 static cp_expr
21739 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21741 tree initializer;
21742 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21744 /* Consume the `{' token. */
21745 cp_lexer_consume_token (parser->lexer);
21746 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21747 initializer = make_node (CONSTRUCTOR);
21748 /* If it's not a `}', then there is a non-trivial initializer. */
21749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21751 /* Parse the initializer list. */
21752 CONSTRUCTOR_ELTS (initializer)
21753 = cp_parser_initializer_list (parser, non_constant_p);
21754 /* A trailing `,' token is allowed. */
21755 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21756 cp_lexer_consume_token (parser->lexer);
21758 else
21759 *non_constant_p = false;
21760 /* Now, there should be a trailing `}'. */
21761 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21762 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21763 TREE_TYPE (initializer) = init_list_type_node;
21765 cp_expr result (initializer);
21766 /* Build a location of the form:
21767 { ... }
21768 ^~~~~~~
21769 with caret==start at the open brace, finish at the close brace. */
21770 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21771 result.set_location (combined_loc);
21772 return result;
21775 /* Consume tokens up to, and including, the next non-nested closing `]'.
21776 Returns true iff we found a closing `]'. */
21778 static bool
21779 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21781 unsigned square_depth = 0;
21783 while (true)
21785 cp_token * token = cp_lexer_peek_token (parser->lexer);
21787 switch (token->type)
21789 case CPP_EOF:
21790 case CPP_PRAGMA_EOL:
21791 /* If we've run out of tokens, then there is no closing `]'. */
21792 return false;
21794 case CPP_OPEN_SQUARE:
21795 ++square_depth;
21796 break;
21798 case CPP_CLOSE_SQUARE:
21799 if (!square_depth--)
21801 cp_lexer_consume_token (parser->lexer);
21802 return true;
21804 break;
21806 default:
21807 break;
21810 /* Consume the token. */
21811 cp_lexer_consume_token (parser->lexer);
21815 /* Return true if we are looking at an array-designator, false otherwise. */
21817 static bool
21818 cp_parser_array_designator_p (cp_parser *parser)
21820 /* Consume the `['. */
21821 cp_lexer_consume_token (parser->lexer);
21823 cp_lexer_save_tokens (parser->lexer);
21825 /* Skip tokens until the next token is a closing square bracket.
21826 If we find the closing `]', and the next token is a `=', then
21827 we are looking at an array designator. */
21828 bool array_designator_p
21829 = (cp_parser_skip_to_closing_square_bracket (parser)
21830 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21832 /* Roll back the tokens we skipped. */
21833 cp_lexer_rollback_tokens (parser->lexer);
21835 return array_designator_p;
21838 /* Parse an initializer-list.
21840 initializer-list:
21841 initializer-clause ... [opt]
21842 initializer-list , initializer-clause ... [opt]
21844 GNU Extension:
21846 initializer-list:
21847 designation initializer-clause ...[opt]
21848 initializer-list , designation initializer-clause ...[opt]
21850 designation:
21851 . identifier =
21852 identifier :
21853 [ constant-expression ] =
21855 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21856 for the initializer. If the INDEX of the elt is non-NULL, it is the
21857 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21858 as for cp_parser_initializer. */
21860 static vec<constructor_elt, va_gc> *
21861 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21863 vec<constructor_elt, va_gc> *v = NULL;
21865 /* Assume all of the expressions are constant. */
21866 *non_constant_p = false;
21868 /* Parse the rest of the list. */
21869 while (true)
21871 cp_token *token;
21872 tree designator;
21873 tree initializer;
21874 bool clause_non_constant_p;
21876 /* If the next token is an identifier and the following one is a
21877 colon, we are looking at the GNU designated-initializer
21878 syntax. */
21879 if (cp_parser_allow_gnu_extensions_p (parser)
21880 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21881 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21883 /* Warn the user that they are using an extension. */
21884 pedwarn (input_location, OPT_Wpedantic,
21885 "ISO C++ does not allow designated initializers");
21886 /* Consume the identifier. */
21887 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21888 /* Consume the `:'. */
21889 cp_lexer_consume_token (parser->lexer);
21891 /* Also handle the C99 syntax, '. id ='. */
21892 else if (cp_parser_allow_gnu_extensions_p (parser)
21893 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21894 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21895 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21897 /* Warn the user that they are using an extension. */
21898 pedwarn (input_location, OPT_Wpedantic,
21899 "ISO C++ does not allow C99 designated initializers");
21900 /* Consume the `.'. */
21901 cp_lexer_consume_token (parser->lexer);
21902 /* Consume the identifier. */
21903 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21904 /* Consume the `='. */
21905 cp_lexer_consume_token (parser->lexer);
21907 /* Also handle C99 array designators, '[ const ] ='. */
21908 else if (cp_parser_allow_gnu_extensions_p (parser)
21909 && !c_dialect_objc ()
21910 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21912 /* In C++11, [ could start a lambda-introducer. */
21913 bool non_const = false;
21915 cp_parser_parse_tentatively (parser);
21917 if (!cp_parser_array_designator_p (parser))
21919 cp_parser_simulate_error (parser);
21920 designator = NULL_TREE;
21922 else
21924 designator = cp_parser_constant_expression (parser, true,
21925 &non_const);
21926 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21927 cp_parser_require (parser, CPP_EQ, RT_EQ);
21930 if (!cp_parser_parse_definitely (parser))
21931 designator = NULL_TREE;
21932 else if (non_const)
21933 require_potential_rvalue_constant_expression (designator);
21935 else
21936 designator = NULL_TREE;
21938 /* Parse the initializer. */
21939 initializer = cp_parser_initializer_clause (parser,
21940 &clause_non_constant_p);
21941 /* If any clause is non-constant, so is the entire initializer. */
21942 if (clause_non_constant_p)
21943 *non_constant_p = true;
21945 /* If we have an ellipsis, this is an initializer pack
21946 expansion. */
21947 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21949 /* Consume the `...'. */
21950 cp_lexer_consume_token (parser->lexer);
21952 /* Turn the initializer into an initializer expansion. */
21953 initializer = make_pack_expansion (initializer);
21956 /* Add it to the vector. */
21957 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21959 /* If the next token is not a comma, we have reached the end of
21960 the list. */
21961 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21962 break;
21964 /* Peek at the next token. */
21965 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21966 /* If the next token is a `}', then we're still done. An
21967 initializer-clause can have a trailing `,' after the
21968 initializer-list and before the closing `}'. */
21969 if (token->type == CPP_CLOSE_BRACE)
21970 break;
21972 /* Consume the `,' token. */
21973 cp_lexer_consume_token (parser->lexer);
21976 return v;
21979 /* Classes [gram.class] */
21981 /* Parse a class-name.
21983 class-name:
21984 identifier
21985 template-id
21987 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21988 to indicate that names looked up in dependent types should be
21989 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21990 keyword has been used to indicate that the name that appears next
21991 is a template. TAG_TYPE indicates the explicit tag given before
21992 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21993 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21994 is the class being defined in a class-head. If ENUM_OK is TRUE,
21995 enum-names are also accepted.
21997 Returns the TYPE_DECL representing the class. */
21999 static tree
22000 cp_parser_class_name (cp_parser *parser,
22001 bool typename_keyword_p,
22002 bool template_keyword_p,
22003 enum tag_types tag_type,
22004 bool check_dependency_p,
22005 bool class_head_p,
22006 bool is_declaration,
22007 bool enum_ok)
22009 tree decl;
22010 tree scope;
22011 bool typename_p;
22012 cp_token *token;
22013 tree identifier = NULL_TREE;
22015 /* All class-names start with an identifier. */
22016 token = cp_lexer_peek_token (parser->lexer);
22017 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22019 cp_parser_error (parser, "expected class-name");
22020 return error_mark_node;
22023 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22024 to a template-id, so we save it here. */
22025 scope = parser->scope;
22026 if (scope == error_mark_node)
22027 return error_mark_node;
22029 /* Any name names a type if we're following the `typename' keyword
22030 in a qualified name where the enclosing scope is type-dependent. */
22031 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22032 && dependent_type_p (scope));
22033 /* Handle the common case (an identifier, but not a template-id)
22034 efficiently. */
22035 if (token->type == CPP_NAME
22036 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22038 cp_token *identifier_token;
22039 bool ambiguous_p;
22041 /* Look for the identifier. */
22042 identifier_token = cp_lexer_peek_token (parser->lexer);
22043 ambiguous_p = identifier_token->error_reported;
22044 identifier = cp_parser_identifier (parser);
22045 /* If the next token isn't an identifier, we are certainly not
22046 looking at a class-name. */
22047 if (identifier == error_mark_node)
22048 decl = error_mark_node;
22049 /* If we know this is a type-name, there's no need to look it
22050 up. */
22051 else if (typename_p)
22052 decl = identifier;
22053 else
22055 tree ambiguous_decls;
22056 /* If we already know that this lookup is ambiguous, then
22057 we've already issued an error message; there's no reason
22058 to check again. */
22059 if (ambiguous_p)
22061 cp_parser_simulate_error (parser);
22062 return error_mark_node;
22064 /* If the next token is a `::', then the name must be a type
22065 name.
22067 [basic.lookup.qual]
22069 During the lookup for a name preceding the :: scope
22070 resolution operator, object, function, and enumerator
22071 names are ignored. */
22072 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22073 tag_type = scope_type;
22074 /* Look up the name. */
22075 decl = cp_parser_lookup_name (parser, identifier,
22076 tag_type,
22077 /*is_template=*/false,
22078 /*is_namespace=*/false,
22079 check_dependency_p,
22080 &ambiguous_decls,
22081 identifier_token->location);
22082 if (ambiguous_decls)
22084 if (cp_parser_parsing_tentatively (parser))
22085 cp_parser_simulate_error (parser);
22086 return error_mark_node;
22090 else
22092 /* Try a template-id. */
22093 decl = cp_parser_template_id (parser, template_keyword_p,
22094 check_dependency_p,
22095 tag_type,
22096 is_declaration);
22097 if (decl == error_mark_node)
22098 return error_mark_node;
22101 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22103 /* If this is a typename, create a TYPENAME_TYPE. */
22104 if (typename_p && decl != error_mark_node)
22106 decl = make_typename_type (scope, decl, typename_type,
22107 /*complain=*/tf_error);
22108 if (decl != error_mark_node)
22109 decl = TYPE_NAME (decl);
22112 decl = strip_using_decl (decl);
22114 /* Check to see that it is really the name of a class. */
22115 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22116 && identifier_p (TREE_OPERAND (decl, 0))
22117 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22118 /* Situations like this:
22120 template <typename T> struct A {
22121 typename T::template X<int>::I i;
22124 are problematic. Is `T::template X<int>' a class-name? The
22125 standard does not seem to be definitive, but there is no other
22126 valid interpretation of the following `::'. Therefore, those
22127 names are considered class-names. */
22129 decl = make_typename_type (scope, decl, tag_type, tf_error);
22130 if (decl != error_mark_node)
22131 decl = TYPE_NAME (decl);
22133 else if (TREE_CODE (decl) != TYPE_DECL
22134 || TREE_TYPE (decl) == error_mark_node
22135 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22136 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22137 /* In Objective-C 2.0, a classname followed by '.' starts a
22138 dot-syntax expression, and it's not a type-name. */
22139 || (c_dialect_objc ()
22140 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22141 && objc_is_class_name (decl)))
22142 decl = error_mark_node;
22144 if (decl == error_mark_node)
22145 cp_parser_error (parser, "expected class-name");
22146 else if (identifier && !parser->scope)
22147 maybe_note_name_used_in_class (identifier, decl);
22149 return decl;
22152 /* Parse a class-specifier.
22154 class-specifier:
22155 class-head { member-specification [opt] }
22157 Returns the TREE_TYPE representing the class. */
22159 static tree
22160 cp_parser_class_specifier_1 (cp_parser* parser)
22162 tree type;
22163 tree attributes = NULL_TREE;
22164 bool nested_name_specifier_p;
22165 unsigned saved_num_template_parameter_lists;
22166 bool saved_in_function_body;
22167 unsigned char in_statement;
22168 bool in_switch_statement_p;
22169 bool saved_in_unbraced_linkage_specification_p;
22170 tree old_scope = NULL_TREE;
22171 tree scope = NULL_TREE;
22172 cp_token *closing_brace;
22174 push_deferring_access_checks (dk_no_deferred);
22176 /* Parse the class-head. */
22177 type = cp_parser_class_head (parser,
22178 &nested_name_specifier_p);
22179 /* If the class-head was a semantic disaster, skip the entire body
22180 of the class. */
22181 if (!type)
22183 cp_parser_skip_to_end_of_block_or_statement (parser);
22184 pop_deferring_access_checks ();
22185 return error_mark_node;
22188 /* Look for the `{'. */
22189 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22191 pop_deferring_access_checks ();
22192 return error_mark_node;
22195 cp_ensure_no_omp_declare_simd (parser);
22196 cp_ensure_no_oacc_routine (parser);
22198 /* Issue an error message if type-definitions are forbidden here. */
22199 cp_parser_check_type_definition (parser);
22200 /* Remember that we are defining one more class. */
22201 ++parser->num_classes_being_defined;
22202 /* Inside the class, surrounding template-parameter-lists do not
22203 apply. */
22204 saved_num_template_parameter_lists
22205 = parser->num_template_parameter_lists;
22206 parser->num_template_parameter_lists = 0;
22207 /* We are not in a function body. */
22208 saved_in_function_body = parser->in_function_body;
22209 parser->in_function_body = false;
22210 /* Or in a loop. */
22211 in_statement = parser->in_statement;
22212 parser->in_statement = 0;
22213 /* Or in a switch. */
22214 in_switch_statement_p = parser->in_switch_statement_p;
22215 parser->in_switch_statement_p = false;
22216 /* We are not immediately inside an extern "lang" block. */
22217 saved_in_unbraced_linkage_specification_p
22218 = parser->in_unbraced_linkage_specification_p;
22219 parser->in_unbraced_linkage_specification_p = false;
22221 // Associate constraints with the type.
22222 if (flag_concepts)
22223 type = associate_classtype_constraints (type);
22225 /* Start the class. */
22226 if (nested_name_specifier_p)
22228 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22229 old_scope = push_inner_scope (scope);
22231 type = begin_class_definition (type);
22233 if (type == error_mark_node)
22234 /* If the type is erroneous, skip the entire body of the class. */
22235 cp_parser_skip_to_closing_brace (parser);
22236 else
22237 /* Parse the member-specification. */
22238 cp_parser_member_specification_opt (parser);
22240 /* Look for the trailing `}'. */
22241 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22242 /* Look for trailing attributes to apply to this class. */
22243 if (cp_parser_allow_gnu_extensions_p (parser))
22244 attributes = cp_parser_gnu_attributes_opt (parser);
22245 if (type != error_mark_node)
22246 type = finish_struct (type, attributes);
22247 if (nested_name_specifier_p)
22248 pop_inner_scope (old_scope, scope);
22250 /* We've finished a type definition. Check for the common syntax
22251 error of forgetting a semicolon after the definition. We need to
22252 be careful, as we can't just check for not-a-semicolon and be done
22253 with it; the user might have typed:
22255 class X { } c = ...;
22256 class X { } *p = ...;
22258 and so forth. Instead, enumerate all the possible tokens that
22259 might follow this production; if we don't see one of them, then
22260 complain and silently insert the semicolon. */
22262 cp_token *token = cp_lexer_peek_token (parser->lexer);
22263 bool want_semicolon = true;
22265 if (cp_next_tokens_can_be_std_attribute_p (parser))
22266 /* Don't try to parse c++11 attributes here. As per the
22267 grammar, that should be a task for
22268 cp_parser_decl_specifier_seq. */
22269 want_semicolon = false;
22271 switch (token->type)
22273 case CPP_NAME:
22274 case CPP_SEMICOLON:
22275 case CPP_MULT:
22276 case CPP_AND:
22277 case CPP_OPEN_PAREN:
22278 case CPP_CLOSE_PAREN:
22279 case CPP_COMMA:
22280 want_semicolon = false;
22281 break;
22283 /* While it's legal for type qualifiers and storage class
22284 specifiers to follow type definitions in the grammar, only
22285 compiler testsuites contain code like that. Assume that if
22286 we see such code, then what we're really seeing is a case
22287 like:
22289 class X { }
22290 const <type> var = ...;
22294 class Y { }
22295 static <type> func (...) ...
22297 i.e. the qualifier or specifier applies to the next
22298 declaration. To do so, however, we need to look ahead one
22299 more token to see if *that* token is a type specifier.
22301 This code could be improved to handle:
22303 class Z { }
22304 static const <type> var = ...; */
22305 case CPP_KEYWORD:
22306 if (keyword_is_decl_specifier (token->keyword))
22308 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22310 /* Handling user-defined types here would be nice, but very
22311 tricky. */
22312 want_semicolon
22313 = (lookahead->type == CPP_KEYWORD
22314 && keyword_begins_type_specifier (lookahead->keyword));
22316 break;
22317 default:
22318 break;
22321 /* If we don't have a type, then something is very wrong and we
22322 shouldn't try to do anything clever. Likewise for not seeing the
22323 closing brace. */
22324 if (closing_brace && TYPE_P (type) && want_semicolon)
22326 /* Locate the closing brace. */
22327 cp_token_position prev
22328 = cp_lexer_previous_token_position (parser->lexer);
22329 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22330 location_t loc = prev_token->location;
22332 /* We want to suggest insertion of a ';' immediately *after* the
22333 closing brace, so, if we can, offset the location by 1 column. */
22334 location_t next_loc = loc;
22335 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22336 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22338 rich_location richloc (line_table, next_loc);
22340 /* If we successfully offset the location, suggest the fix-it. */
22341 if (next_loc != loc)
22342 richloc.add_fixit_insert_before (next_loc, ";");
22344 if (CLASSTYPE_DECLARED_CLASS (type))
22345 error_at_rich_loc (&richloc,
22346 "expected %<;%> after class definition");
22347 else if (TREE_CODE (type) == RECORD_TYPE)
22348 error_at_rich_loc (&richloc,
22349 "expected %<;%> after struct definition");
22350 else if (TREE_CODE (type) == UNION_TYPE)
22351 error_at_rich_loc (&richloc,
22352 "expected %<;%> after union definition");
22353 else
22354 gcc_unreachable ();
22356 /* Unget one token and smash it to look as though we encountered
22357 a semicolon in the input stream. */
22358 cp_lexer_set_token_position (parser->lexer, prev);
22359 token = cp_lexer_peek_token (parser->lexer);
22360 token->type = CPP_SEMICOLON;
22361 token->keyword = RID_MAX;
22365 /* If this class is not itself within the scope of another class,
22366 then we need to parse the bodies of all of the queued function
22367 definitions. Note that the queued functions defined in a class
22368 are not always processed immediately following the
22369 class-specifier for that class. Consider:
22371 struct A {
22372 struct B { void f() { sizeof (A); } };
22375 If `f' were processed before the processing of `A' were
22376 completed, there would be no way to compute the size of `A'.
22377 Note that the nesting we are interested in here is lexical --
22378 not the semantic nesting given by TYPE_CONTEXT. In particular,
22379 for:
22381 struct A { struct B; };
22382 struct A::B { void f() { } };
22384 there is no need to delay the parsing of `A::B::f'. */
22385 if (--parser->num_classes_being_defined == 0)
22387 tree decl;
22388 tree class_type = NULL_TREE;
22389 tree pushed_scope = NULL_TREE;
22390 unsigned ix;
22391 cp_default_arg_entry *e;
22392 tree save_ccp, save_ccr;
22394 /* In a first pass, parse default arguments to the functions.
22395 Then, in a second pass, parse the bodies of the functions.
22396 This two-phased approach handles cases like:
22398 struct S {
22399 void f() { g(); }
22400 void g(int i = 3);
22404 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22406 decl = e->decl;
22407 /* If there are default arguments that have not yet been processed,
22408 take care of them now. */
22409 if (class_type != e->class_type)
22411 if (pushed_scope)
22412 pop_scope (pushed_scope);
22413 class_type = e->class_type;
22414 pushed_scope = push_scope (class_type);
22416 /* Make sure that any template parameters are in scope. */
22417 maybe_begin_member_template_processing (decl);
22418 /* Parse the default argument expressions. */
22419 cp_parser_late_parsing_default_args (parser, decl);
22420 /* Remove any template parameters from the symbol table. */
22421 maybe_end_member_template_processing ();
22423 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22424 /* Now parse any NSDMIs. */
22425 save_ccp = current_class_ptr;
22426 save_ccr = current_class_ref;
22427 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22429 if (class_type != DECL_CONTEXT (decl))
22431 if (pushed_scope)
22432 pop_scope (pushed_scope);
22433 class_type = DECL_CONTEXT (decl);
22434 pushed_scope = push_scope (class_type);
22436 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22437 cp_parser_late_parsing_nsdmi (parser, decl);
22439 vec_safe_truncate (unparsed_nsdmis, 0);
22440 current_class_ptr = save_ccp;
22441 current_class_ref = save_ccr;
22442 if (pushed_scope)
22443 pop_scope (pushed_scope);
22445 /* Now do some post-NSDMI bookkeeping. */
22446 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22447 after_nsdmi_defaulted_late_checks (class_type);
22448 vec_safe_truncate (unparsed_classes, 0);
22449 after_nsdmi_defaulted_late_checks (type);
22451 /* Now parse the body of the functions. */
22452 if (flag_openmp)
22454 /* OpenMP UDRs need to be parsed before all other functions. */
22455 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22456 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22457 cp_parser_late_parsing_for_member (parser, decl);
22458 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22459 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22460 cp_parser_late_parsing_for_member (parser, decl);
22462 else
22463 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22464 cp_parser_late_parsing_for_member (parser, decl);
22465 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22467 else
22468 vec_safe_push (unparsed_classes, type);
22470 /* Put back any saved access checks. */
22471 pop_deferring_access_checks ();
22473 /* Restore saved state. */
22474 parser->in_switch_statement_p = in_switch_statement_p;
22475 parser->in_statement = in_statement;
22476 parser->in_function_body = saved_in_function_body;
22477 parser->num_template_parameter_lists
22478 = saved_num_template_parameter_lists;
22479 parser->in_unbraced_linkage_specification_p
22480 = saved_in_unbraced_linkage_specification_p;
22482 return type;
22485 static tree
22486 cp_parser_class_specifier (cp_parser* parser)
22488 tree ret;
22489 timevar_push (TV_PARSE_STRUCT);
22490 ret = cp_parser_class_specifier_1 (parser);
22491 timevar_pop (TV_PARSE_STRUCT);
22492 return ret;
22495 /* Parse a class-head.
22497 class-head:
22498 class-key identifier [opt] base-clause [opt]
22499 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22500 class-key nested-name-specifier [opt] template-id
22501 base-clause [opt]
22503 class-virt-specifier:
22504 final
22506 GNU Extensions:
22507 class-key attributes identifier [opt] base-clause [opt]
22508 class-key attributes nested-name-specifier identifier base-clause [opt]
22509 class-key attributes nested-name-specifier [opt] template-id
22510 base-clause [opt]
22512 Upon return BASES is initialized to the list of base classes (or
22513 NULL, if there are none) in the same form returned by
22514 cp_parser_base_clause.
22516 Returns the TYPE of the indicated class. Sets
22517 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22518 involving a nested-name-specifier was used, and FALSE otherwise.
22520 Returns error_mark_node if this is not a class-head.
22522 Returns NULL_TREE if the class-head is syntactically valid, but
22523 semantically invalid in a way that means we should skip the entire
22524 body of the class. */
22526 static tree
22527 cp_parser_class_head (cp_parser* parser,
22528 bool* nested_name_specifier_p)
22530 tree nested_name_specifier;
22531 enum tag_types class_key;
22532 tree id = NULL_TREE;
22533 tree type = NULL_TREE;
22534 tree attributes;
22535 tree bases;
22536 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22537 bool template_id_p = false;
22538 bool qualified_p = false;
22539 bool invalid_nested_name_p = false;
22540 bool invalid_explicit_specialization_p = false;
22541 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22542 tree pushed_scope = NULL_TREE;
22543 unsigned num_templates;
22544 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22545 /* Assume no nested-name-specifier will be present. */
22546 *nested_name_specifier_p = false;
22547 /* Assume no template parameter lists will be used in defining the
22548 type. */
22549 num_templates = 0;
22550 parser->colon_corrects_to_scope_p = false;
22552 /* Look for the class-key. */
22553 class_key = cp_parser_class_key (parser);
22554 if (class_key == none_type)
22555 return error_mark_node;
22557 location_t class_head_start_location = input_location;
22559 /* Parse the attributes. */
22560 attributes = cp_parser_attributes_opt (parser);
22562 /* If the next token is `::', that is invalid -- but sometimes
22563 people do try to write:
22565 struct ::S {};
22567 Handle this gracefully by accepting the extra qualifier, and then
22568 issuing an error about it later if this really is a
22569 class-head. If it turns out just to be an elaborated type
22570 specifier, remain silent. */
22571 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22572 qualified_p = true;
22574 push_deferring_access_checks (dk_no_check);
22576 /* Determine the name of the class. Begin by looking for an
22577 optional nested-name-specifier. */
22578 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22579 nested_name_specifier
22580 = cp_parser_nested_name_specifier_opt (parser,
22581 /*typename_keyword_p=*/false,
22582 /*check_dependency_p=*/false,
22583 /*type_p=*/true,
22584 /*is_declaration=*/false);
22585 /* If there was a nested-name-specifier, then there *must* be an
22586 identifier. */
22587 if (nested_name_specifier)
22589 type_start_token = cp_lexer_peek_token (parser->lexer);
22590 /* Although the grammar says `identifier', it really means
22591 `class-name' or `template-name'. You are only allowed to
22592 define a class that has already been declared with this
22593 syntax.
22595 The proposed resolution for Core Issue 180 says that wherever
22596 you see `class T::X' you should treat `X' as a type-name.
22598 It is OK to define an inaccessible class; for example:
22600 class A { class B; };
22601 class A::B {};
22603 We do not know if we will see a class-name, or a
22604 template-name. We look for a class-name first, in case the
22605 class-name is a template-id; if we looked for the
22606 template-name first we would stop after the template-name. */
22607 cp_parser_parse_tentatively (parser);
22608 type = cp_parser_class_name (parser,
22609 /*typename_keyword_p=*/false,
22610 /*template_keyword_p=*/false,
22611 class_type,
22612 /*check_dependency_p=*/false,
22613 /*class_head_p=*/true,
22614 /*is_declaration=*/false);
22615 /* If that didn't work, ignore the nested-name-specifier. */
22616 if (!cp_parser_parse_definitely (parser))
22618 invalid_nested_name_p = true;
22619 type_start_token = cp_lexer_peek_token (parser->lexer);
22620 id = cp_parser_identifier (parser);
22621 if (id == error_mark_node)
22622 id = NULL_TREE;
22624 /* If we could not find a corresponding TYPE, treat this
22625 declaration like an unqualified declaration. */
22626 if (type == error_mark_node)
22627 nested_name_specifier = NULL_TREE;
22628 /* Otherwise, count the number of templates used in TYPE and its
22629 containing scopes. */
22630 else
22632 tree scope;
22634 for (scope = TREE_TYPE (type);
22635 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22636 scope = get_containing_scope (scope))
22637 if (TYPE_P (scope)
22638 && CLASS_TYPE_P (scope)
22639 && CLASSTYPE_TEMPLATE_INFO (scope)
22640 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22641 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22642 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22643 ++num_templates;
22646 /* Otherwise, the identifier is optional. */
22647 else
22649 /* We don't know whether what comes next is a template-id,
22650 an identifier, or nothing at all. */
22651 cp_parser_parse_tentatively (parser);
22652 /* Check for a template-id. */
22653 type_start_token = cp_lexer_peek_token (parser->lexer);
22654 id = cp_parser_template_id (parser,
22655 /*template_keyword_p=*/false,
22656 /*check_dependency_p=*/true,
22657 class_key,
22658 /*is_declaration=*/true);
22659 /* If that didn't work, it could still be an identifier. */
22660 if (!cp_parser_parse_definitely (parser))
22662 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22664 type_start_token = cp_lexer_peek_token (parser->lexer);
22665 id = cp_parser_identifier (parser);
22667 else
22668 id = NULL_TREE;
22670 else
22672 template_id_p = true;
22673 ++num_templates;
22677 pop_deferring_access_checks ();
22679 if (id)
22681 cp_parser_check_for_invalid_template_id (parser, id,
22682 class_key,
22683 type_start_token->location);
22685 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22687 /* If it's not a `:' or a `{' then we can't really be looking at a
22688 class-head, since a class-head only appears as part of a
22689 class-specifier. We have to detect this situation before calling
22690 xref_tag, since that has irreversible side-effects. */
22691 if (!cp_parser_next_token_starts_class_definition_p (parser))
22693 cp_parser_error (parser, "expected %<{%> or %<:%>");
22694 type = error_mark_node;
22695 goto out;
22698 /* At this point, we're going ahead with the class-specifier, even
22699 if some other problem occurs. */
22700 cp_parser_commit_to_tentative_parse (parser);
22701 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22703 cp_parser_error (parser,
22704 "cannot specify %<override%> for a class");
22705 type = error_mark_node;
22706 goto out;
22708 /* Issue the error about the overly-qualified name now. */
22709 if (qualified_p)
22711 cp_parser_error (parser,
22712 "global qualification of class name is invalid");
22713 type = error_mark_node;
22714 goto out;
22716 else if (invalid_nested_name_p)
22718 cp_parser_error (parser,
22719 "qualified name does not name a class");
22720 type = error_mark_node;
22721 goto out;
22723 else if (nested_name_specifier)
22725 tree scope;
22727 /* Reject typedef-names in class heads. */
22728 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22730 error_at (type_start_token->location,
22731 "invalid class name in declaration of %qD",
22732 type);
22733 type = NULL_TREE;
22734 goto done;
22737 /* Figure out in what scope the declaration is being placed. */
22738 scope = current_scope ();
22739 /* If that scope does not contain the scope in which the
22740 class was originally declared, the program is invalid. */
22741 if (scope && !is_ancestor (scope, nested_name_specifier))
22743 if (at_namespace_scope_p ())
22744 error_at (type_start_token->location,
22745 "declaration of %qD in namespace %qD which does not "
22746 "enclose %qD",
22747 type, scope, nested_name_specifier);
22748 else
22749 error_at (type_start_token->location,
22750 "declaration of %qD in %qD which does not enclose %qD",
22751 type, scope, nested_name_specifier);
22752 type = NULL_TREE;
22753 goto done;
22755 /* [dcl.meaning]
22757 A declarator-id shall not be qualified except for the
22758 definition of a ... nested class outside of its class
22759 ... [or] the definition or explicit instantiation of a
22760 class member of a namespace outside of its namespace. */
22761 if (scope == nested_name_specifier)
22763 permerror (nested_name_specifier_token_start->location,
22764 "extra qualification not allowed");
22765 nested_name_specifier = NULL_TREE;
22766 num_templates = 0;
22769 /* An explicit-specialization must be preceded by "template <>". If
22770 it is not, try to recover gracefully. */
22771 if (at_namespace_scope_p ()
22772 && parser->num_template_parameter_lists == 0
22773 && !processing_template_parmlist
22774 && template_id_p)
22776 /* Build a location of this form:
22777 struct typename <ARGS>
22778 ^~~~~~~~~~~~~~~~~~~~~~
22779 with caret==start at the start token, and
22780 finishing at the end of the type. */
22781 location_t reported_loc
22782 = make_location (class_head_start_location,
22783 class_head_start_location,
22784 get_finish (type_start_token->location));
22785 rich_location richloc (line_table, reported_loc);
22786 richloc.add_fixit_insert_before (class_head_start_location,
22787 "template <> ");
22788 error_at_rich_loc
22789 (&richloc,
22790 "an explicit specialization must be preceded by %<template <>%>");
22791 invalid_explicit_specialization_p = true;
22792 /* Take the same action that would have been taken by
22793 cp_parser_explicit_specialization. */
22794 ++parser->num_template_parameter_lists;
22795 begin_specialization ();
22797 /* There must be no "return" statements between this point and the
22798 end of this function; set "type "to the correct return value and
22799 use "goto done;" to return. */
22800 /* Make sure that the right number of template parameters were
22801 present. */
22802 if (!cp_parser_check_template_parameters (parser, num_templates,
22803 type_start_token->location,
22804 /*declarator=*/NULL))
22806 /* If something went wrong, there is no point in even trying to
22807 process the class-definition. */
22808 type = NULL_TREE;
22809 goto done;
22812 /* Look up the type. */
22813 if (template_id_p)
22815 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22816 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22817 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22819 error_at (type_start_token->location,
22820 "function template %qD redeclared as a class template", id);
22821 type = error_mark_node;
22823 else
22825 type = TREE_TYPE (id);
22826 type = maybe_process_partial_specialization (type);
22828 /* Check the scope while we still know whether or not we had a
22829 nested-name-specifier. */
22830 if (type != error_mark_node)
22831 check_unqualified_spec_or_inst (type, type_start_token->location);
22833 if (nested_name_specifier)
22834 pushed_scope = push_scope (nested_name_specifier);
22836 else if (nested_name_specifier)
22838 tree class_type;
22840 /* Given:
22842 template <typename T> struct S { struct T };
22843 template <typename T> struct S<T>::T { };
22845 we will get a TYPENAME_TYPE when processing the definition of
22846 `S::T'. We need to resolve it to the actual type before we
22847 try to define it. */
22848 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22850 class_type = resolve_typename_type (TREE_TYPE (type),
22851 /*only_current_p=*/false);
22852 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22853 type = TYPE_NAME (class_type);
22854 else
22856 cp_parser_error (parser, "could not resolve typename type");
22857 type = error_mark_node;
22861 if (maybe_process_partial_specialization (TREE_TYPE (type))
22862 == error_mark_node)
22864 type = NULL_TREE;
22865 goto done;
22868 class_type = current_class_type;
22869 /* Enter the scope indicated by the nested-name-specifier. */
22870 pushed_scope = push_scope (nested_name_specifier);
22871 /* Get the canonical version of this type. */
22872 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22873 /* Call push_template_decl if it seems like we should be defining a
22874 template either from the template headers or the type we're
22875 defining, so that we diagnose both extra and missing headers. */
22876 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22877 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22878 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22880 type = push_template_decl (type);
22881 if (type == error_mark_node)
22883 type = NULL_TREE;
22884 goto done;
22888 type = TREE_TYPE (type);
22889 *nested_name_specifier_p = true;
22891 else /* The name is not a nested name. */
22893 /* If the class was unnamed, create a dummy name. */
22894 if (!id)
22895 id = make_anon_name ();
22896 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22897 ? ts_within_enclosing_non_class
22898 : ts_current);
22899 type = xref_tag (class_key, id, tag_scope,
22900 parser->num_template_parameter_lists);
22903 /* Indicate whether this class was declared as a `class' or as a
22904 `struct'. */
22905 if (TREE_CODE (type) == RECORD_TYPE)
22906 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22907 cp_parser_check_class_key (class_key, type);
22909 /* If this type was already complete, and we see another definition,
22910 that's an error. */
22911 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22913 error_at (type_start_token->location, "redefinition of %q#T",
22914 type);
22915 inform (location_of (type), "previous definition of %q#T",
22916 type);
22917 type = NULL_TREE;
22918 goto done;
22920 else if (type == error_mark_node)
22921 type = NULL_TREE;
22923 if (type)
22925 /* Apply attributes now, before any use of the class as a template
22926 argument in its base list. */
22927 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22928 fixup_attribute_variants (type);
22931 /* We will have entered the scope containing the class; the names of
22932 base classes should be looked up in that context. For example:
22934 struct A { struct B {}; struct C; };
22935 struct A::C : B {};
22937 is valid. */
22939 /* Get the list of base-classes, if there is one. */
22940 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22942 /* PR59482: enter the class scope so that base-specifiers are looked
22943 up correctly. */
22944 if (type)
22945 pushclass (type);
22946 bases = cp_parser_base_clause (parser);
22947 /* PR59482: get out of the previously pushed class scope so that the
22948 subsequent pops pop the right thing. */
22949 if (type)
22950 popclass ();
22952 else
22953 bases = NULL_TREE;
22955 /* If we're really defining a class, process the base classes.
22956 If they're invalid, fail. */
22957 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22958 xref_basetypes (type, bases);
22960 done:
22961 /* Leave the scope given by the nested-name-specifier. We will
22962 enter the class scope itself while processing the members. */
22963 if (pushed_scope)
22964 pop_scope (pushed_scope);
22966 if (invalid_explicit_specialization_p)
22968 end_specialization ();
22969 --parser->num_template_parameter_lists;
22972 if (type)
22973 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22974 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22975 CLASSTYPE_FINAL (type) = 1;
22976 out:
22977 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22978 return type;
22981 /* Parse a class-key.
22983 class-key:
22984 class
22985 struct
22986 union
22988 Returns the kind of class-key specified, or none_type to indicate
22989 error. */
22991 static enum tag_types
22992 cp_parser_class_key (cp_parser* parser)
22994 cp_token *token;
22995 enum tag_types tag_type;
22997 /* Look for the class-key. */
22998 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22999 if (!token)
23000 return none_type;
23002 /* Check to see if the TOKEN is a class-key. */
23003 tag_type = cp_parser_token_is_class_key (token);
23004 if (!tag_type)
23005 cp_parser_error (parser, "expected class-key");
23006 return tag_type;
23009 /* Parse a type-parameter-key.
23011 type-parameter-key:
23012 class
23013 typename
23016 static void
23017 cp_parser_type_parameter_key (cp_parser* parser)
23019 /* Look for the type-parameter-key. */
23020 enum tag_types tag_type = none_type;
23021 cp_token *token = cp_lexer_peek_token (parser->lexer);
23022 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23024 cp_lexer_consume_token (parser->lexer);
23025 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
23026 /* typename is not allowed in a template template parameter
23027 by the standard until C++1Z. */
23028 pedwarn (token->location, OPT_Wpedantic,
23029 "ISO C++ forbids typename key in template template parameter;"
23030 " use -std=c++1z or -std=gnu++1z");
23032 else
23033 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23035 return;
23038 /* Parse an (optional) member-specification.
23040 member-specification:
23041 member-declaration member-specification [opt]
23042 access-specifier : member-specification [opt] */
23044 static void
23045 cp_parser_member_specification_opt (cp_parser* parser)
23047 while (true)
23049 cp_token *token;
23050 enum rid keyword;
23052 /* Peek at the next token. */
23053 token = cp_lexer_peek_token (parser->lexer);
23054 /* If it's a `}', or EOF then we've seen all the members. */
23055 if (token->type == CPP_CLOSE_BRACE
23056 || token->type == CPP_EOF
23057 || token->type == CPP_PRAGMA_EOL)
23058 break;
23060 /* See if this token is a keyword. */
23061 keyword = token->keyword;
23062 switch (keyword)
23064 case RID_PUBLIC:
23065 case RID_PROTECTED:
23066 case RID_PRIVATE:
23067 /* Consume the access-specifier. */
23068 cp_lexer_consume_token (parser->lexer);
23069 /* Remember which access-specifier is active. */
23070 current_access_specifier = token->u.value;
23071 /* Look for the `:'. */
23072 cp_parser_require (parser, CPP_COLON, RT_COLON);
23073 break;
23075 default:
23076 /* Accept #pragmas at class scope. */
23077 if (token->type == CPP_PRAGMA)
23079 cp_parser_pragma (parser, pragma_member, NULL);
23080 break;
23083 /* Otherwise, the next construction must be a
23084 member-declaration. */
23085 cp_parser_member_declaration (parser);
23090 /* Parse a member-declaration.
23092 member-declaration:
23093 decl-specifier-seq [opt] member-declarator-list [opt] ;
23094 function-definition ; [opt]
23095 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23096 using-declaration
23097 template-declaration
23098 alias-declaration
23100 member-declarator-list:
23101 member-declarator
23102 member-declarator-list , member-declarator
23104 member-declarator:
23105 declarator pure-specifier [opt]
23106 declarator constant-initializer [opt]
23107 identifier [opt] : constant-expression
23109 GNU Extensions:
23111 member-declaration:
23112 __extension__ member-declaration
23114 member-declarator:
23115 declarator attributes [opt] pure-specifier [opt]
23116 declarator attributes [opt] constant-initializer [opt]
23117 identifier [opt] attributes [opt] : constant-expression
23119 C++0x Extensions:
23121 member-declaration:
23122 static_assert-declaration */
23124 static void
23125 cp_parser_member_declaration (cp_parser* parser)
23127 cp_decl_specifier_seq decl_specifiers;
23128 tree prefix_attributes;
23129 tree decl;
23130 int declares_class_or_enum;
23131 bool friend_p;
23132 cp_token *token = NULL;
23133 cp_token *decl_spec_token_start = NULL;
23134 cp_token *initializer_token_start = NULL;
23135 int saved_pedantic;
23136 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23138 /* Check for the `__extension__' keyword. */
23139 if (cp_parser_extension_opt (parser, &saved_pedantic))
23141 /* Recurse. */
23142 cp_parser_member_declaration (parser);
23143 /* Restore the old value of the PEDANTIC flag. */
23144 pedantic = saved_pedantic;
23146 return;
23149 /* Check for a template-declaration. */
23150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23152 /* An explicit specialization here is an error condition, and we
23153 expect the specialization handler to detect and report this. */
23154 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23155 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23156 cp_parser_explicit_specialization (parser);
23157 else
23158 cp_parser_template_declaration (parser, /*member_p=*/true);
23160 return;
23162 /* Check for a template introduction. */
23163 else if (cp_parser_template_declaration_after_export (parser, true))
23164 return;
23166 /* Check for a using-declaration. */
23167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23169 if (cxx_dialect < cxx11)
23171 /* Parse the using-declaration. */
23172 cp_parser_using_declaration (parser,
23173 /*access_declaration_p=*/false);
23174 return;
23176 else
23178 tree decl;
23179 bool alias_decl_expected;
23180 cp_parser_parse_tentatively (parser);
23181 decl = cp_parser_alias_declaration (parser);
23182 /* Note that if we actually see the '=' token after the
23183 identifier, cp_parser_alias_declaration commits the
23184 tentative parse. In that case, we really expect an
23185 alias-declaration. Otherwise, we expect a using
23186 declaration. */
23187 alias_decl_expected =
23188 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23189 cp_parser_parse_definitely (parser);
23191 if (alias_decl_expected)
23192 finish_member_declaration (decl);
23193 else
23194 cp_parser_using_declaration (parser,
23195 /*access_declaration_p=*/false);
23196 return;
23200 /* Check for @defs. */
23201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23203 tree ivar, member;
23204 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23205 ivar = ivar_chains;
23206 while (ivar)
23208 member = ivar;
23209 ivar = TREE_CHAIN (member);
23210 TREE_CHAIN (member) = NULL_TREE;
23211 finish_member_declaration (member);
23213 return;
23216 /* If the next token is `static_assert' we have a static assertion. */
23217 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23219 cp_parser_static_assert (parser, /*member_p=*/true);
23220 return;
23223 parser->colon_corrects_to_scope_p = false;
23225 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23226 goto out;
23228 /* Parse the decl-specifier-seq. */
23229 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23230 cp_parser_decl_specifier_seq (parser,
23231 CP_PARSER_FLAGS_OPTIONAL,
23232 &decl_specifiers,
23233 &declares_class_or_enum);
23234 /* Check for an invalid type-name. */
23235 if (!decl_specifiers.any_type_specifiers_p
23236 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23237 goto out;
23238 /* If there is no declarator, then the decl-specifier-seq should
23239 specify a type. */
23240 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23242 /* If there was no decl-specifier-seq, and the next token is a
23243 `;', then we have something like:
23245 struct S { ; };
23247 [class.mem]
23249 Each member-declaration shall declare at least one member
23250 name of the class. */
23251 if (!decl_specifiers.any_specifiers_p)
23253 cp_token *token = cp_lexer_peek_token (parser->lexer);
23254 if (!in_system_header_at (token->location))
23256 gcc_rich_location richloc (token->location);
23257 richloc.add_fixit_remove ();
23258 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23261 else
23263 tree type;
23265 /* See if this declaration is a friend. */
23266 friend_p = cp_parser_friend_p (&decl_specifiers);
23267 /* If there were decl-specifiers, check to see if there was
23268 a class-declaration. */
23269 type = check_tag_decl (&decl_specifiers,
23270 /*explicit_type_instantiation_p=*/false);
23271 /* Nested classes have already been added to the class, but
23272 a `friend' needs to be explicitly registered. */
23273 if (friend_p)
23275 /* If the `friend' keyword was present, the friend must
23276 be introduced with a class-key. */
23277 if (!declares_class_or_enum && cxx_dialect < cxx11)
23278 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23279 "in C++03 a class-key must be used "
23280 "when declaring a friend");
23281 /* In this case:
23283 template <typename T> struct A {
23284 friend struct A<T>::B;
23287 A<T>::B will be represented by a TYPENAME_TYPE, and
23288 therefore not recognized by check_tag_decl. */
23289 if (!type)
23291 type = decl_specifiers.type;
23292 if (type && TREE_CODE (type) == TYPE_DECL)
23293 type = TREE_TYPE (type);
23295 if (!type || !TYPE_P (type))
23296 error_at (decl_spec_token_start->location,
23297 "friend declaration does not name a class or "
23298 "function");
23299 else
23300 make_friend_class (current_class_type, type,
23301 /*complain=*/true);
23303 /* If there is no TYPE, an error message will already have
23304 been issued. */
23305 else if (!type || type == error_mark_node)
23307 /* An anonymous aggregate has to be handled specially; such
23308 a declaration really declares a data member (with a
23309 particular type), as opposed to a nested class. */
23310 else if (ANON_AGGR_TYPE_P (type))
23312 /* C++11 9.5/6. */
23313 if (decl_specifiers.storage_class != sc_none)
23314 error_at (decl_spec_token_start->location,
23315 "a storage class on an anonymous aggregate "
23316 "in class scope is not allowed");
23318 /* Remove constructors and such from TYPE, now that we
23319 know it is an anonymous aggregate. */
23320 fixup_anonymous_aggr (type);
23321 /* And make the corresponding data member. */
23322 decl = build_decl (decl_spec_token_start->location,
23323 FIELD_DECL, NULL_TREE, type);
23324 /* Add it to the class. */
23325 finish_member_declaration (decl);
23327 else
23328 cp_parser_check_access_in_redeclaration
23329 (TYPE_NAME (type),
23330 decl_spec_token_start->location);
23333 else
23335 bool assume_semicolon = false;
23337 /* Clear attributes from the decl_specifiers but keep them
23338 around as prefix attributes that apply them to the entity
23339 being declared. */
23340 prefix_attributes = decl_specifiers.attributes;
23341 decl_specifiers.attributes = NULL_TREE;
23343 /* See if these declarations will be friends. */
23344 friend_p = cp_parser_friend_p (&decl_specifiers);
23346 /* Keep going until we hit the `;' at the end of the
23347 declaration. */
23348 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23350 tree attributes = NULL_TREE;
23351 tree first_attribute;
23353 /* Peek at the next token. */
23354 token = cp_lexer_peek_token (parser->lexer);
23356 /* Check for a bitfield declaration. */
23357 if (token->type == CPP_COLON
23358 || (token->type == CPP_NAME
23359 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23360 == CPP_COLON))
23362 tree identifier;
23363 tree width;
23365 /* Get the name of the bitfield. Note that we cannot just
23366 check TOKEN here because it may have been invalidated by
23367 the call to cp_lexer_peek_nth_token above. */
23368 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23369 identifier = cp_parser_identifier (parser);
23370 else
23371 identifier = NULL_TREE;
23373 /* Consume the `:' token. */
23374 cp_lexer_consume_token (parser->lexer);
23375 /* Get the width of the bitfield. */
23376 width
23377 = cp_parser_constant_expression (parser);
23379 /* Look for attributes that apply to the bitfield. */
23380 attributes = cp_parser_attributes_opt (parser);
23381 /* Remember which attributes are prefix attributes and
23382 which are not. */
23383 first_attribute = attributes;
23384 /* Combine the attributes. */
23385 attributes = chainon (prefix_attributes, attributes);
23387 /* Create the bitfield declaration. */
23388 decl = grokbitfield (identifier
23389 ? make_id_declarator (NULL_TREE,
23390 identifier,
23391 sfk_none)
23392 : NULL,
23393 &decl_specifiers,
23394 width,
23395 attributes);
23397 else
23399 cp_declarator *declarator;
23400 tree initializer;
23401 tree asm_specification;
23402 int ctor_dtor_or_conv_p;
23404 /* Parse the declarator. */
23405 declarator
23406 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23407 &ctor_dtor_or_conv_p,
23408 /*parenthesized_p=*/NULL,
23409 /*member_p=*/true,
23410 friend_p);
23412 /* If something went wrong parsing the declarator, make sure
23413 that we at least consume some tokens. */
23414 if (declarator == cp_error_declarator)
23416 /* Skip to the end of the statement. */
23417 cp_parser_skip_to_end_of_statement (parser);
23418 /* If the next token is not a semicolon, that is
23419 probably because we just skipped over the body of
23420 a function. So, we consume a semicolon if
23421 present, but do not issue an error message if it
23422 is not present. */
23423 if (cp_lexer_next_token_is (parser->lexer,
23424 CPP_SEMICOLON))
23425 cp_lexer_consume_token (parser->lexer);
23426 goto out;
23429 if (declares_class_or_enum & 2)
23430 cp_parser_check_for_definition_in_return_type
23431 (declarator, decl_specifiers.type,
23432 decl_specifiers.locations[ds_type_spec]);
23434 /* Look for an asm-specification. */
23435 asm_specification = cp_parser_asm_specification_opt (parser);
23436 /* Look for attributes that apply to the declaration. */
23437 attributes = cp_parser_attributes_opt (parser);
23438 /* Remember which attributes are prefix attributes and
23439 which are not. */
23440 first_attribute = attributes;
23441 /* Combine the attributes. */
23442 attributes = chainon (prefix_attributes, attributes);
23444 /* If it's an `=', then we have a constant-initializer or a
23445 pure-specifier. It is not correct to parse the
23446 initializer before registering the member declaration
23447 since the member declaration should be in scope while
23448 its initializer is processed. However, the rest of the
23449 front end does not yet provide an interface that allows
23450 us to handle this correctly. */
23451 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23453 /* In [class.mem]:
23455 A pure-specifier shall be used only in the declaration of
23456 a virtual function.
23458 A member-declarator can contain a constant-initializer
23459 only if it declares a static member of integral or
23460 enumeration type.
23462 Therefore, if the DECLARATOR is for a function, we look
23463 for a pure-specifier; otherwise, we look for a
23464 constant-initializer. When we call `grokfield', it will
23465 perform more stringent semantics checks. */
23466 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23467 if (function_declarator_p (declarator)
23468 || (decl_specifiers.type
23469 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23470 && declarator->kind == cdk_id
23471 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23472 == FUNCTION_TYPE)))
23473 initializer = cp_parser_pure_specifier (parser);
23474 else if (decl_specifiers.storage_class != sc_static)
23475 initializer = cp_parser_save_nsdmi (parser);
23476 else if (cxx_dialect >= cxx11)
23478 bool nonconst;
23479 /* Don't require a constant rvalue in C++11, since we
23480 might want a reference constant. We'll enforce
23481 constancy later. */
23482 cp_lexer_consume_token (parser->lexer);
23483 /* Parse the initializer. */
23484 initializer = cp_parser_initializer_clause (parser,
23485 &nonconst);
23487 else
23488 /* Parse the initializer. */
23489 initializer = cp_parser_constant_initializer (parser);
23491 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23492 && !function_declarator_p (declarator))
23494 bool x;
23495 if (decl_specifiers.storage_class != sc_static)
23496 initializer = cp_parser_save_nsdmi (parser);
23497 else
23498 initializer = cp_parser_initializer (parser, &x, &x);
23500 /* Otherwise, there is no initializer. */
23501 else
23502 initializer = NULL_TREE;
23504 /* See if we are probably looking at a function
23505 definition. We are certainly not looking at a
23506 member-declarator. Calling `grokfield' has
23507 side-effects, so we must not do it unless we are sure
23508 that we are looking at a member-declarator. */
23509 if (cp_parser_token_starts_function_definition_p
23510 (cp_lexer_peek_token (parser->lexer)))
23512 /* The grammar does not allow a pure-specifier to be
23513 used when a member function is defined. (It is
23514 possible that this fact is an oversight in the
23515 standard, since a pure function may be defined
23516 outside of the class-specifier. */
23517 if (initializer && initializer_token_start)
23518 error_at (initializer_token_start->location,
23519 "pure-specifier on function-definition");
23520 decl = cp_parser_save_member_function_body (parser,
23521 &decl_specifiers,
23522 declarator,
23523 attributes);
23524 if (parser->fully_implicit_function_template_p)
23525 decl = finish_fully_implicit_template (parser, decl);
23526 /* If the member was not a friend, declare it here. */
23527 if (!friend_p)
23528 finish_member_declaration (decl);
23529 /* Peek at the next token. */
23530 token = cp_lexer_peek_token (parser->lexer);
23531 /* If the next token is a semicolon, consume it. */
23532 if (token->type == CPP_SEMICOLON)
23534 location_t semicolon_loc
23535 = cp_lexer_consume_token (parser->lexer)->location;
23536 gcc_rich_location richloc (semicolon_loc);
23537 richloc.add_fixit_remove ();
23538 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23539 "extra %<;%> after in-class "
23540 "function definition");
23542 goto out;
23544 else
23545 if (declarator->kind == cdk_function)
23546 declarator->id_loc = token->location;
23547 /* Create the declaration. */
23548 decl = grokfield (declarator, &decl_specifiers,
23549 initializer, /*init_const_expr_p=*/true,
23550 asm_specification, attributes);
23551 if (parser->fully_implicit_function_template_p)
23553 if (friend_p)
23554 finish_fully_implicit_template (parser, 0);
23555 else
23556 decl = finish_fully_implicit_template (parser, decl);
23560 cp_finalize_omp_declare_simd (parser, decl);
23561 cp_finalize_oacc_routine (parser, decl, false);
23563 /* Reset PREFIX_ATTRIBUTES. */
23564 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23565 attributes = TREE_CHAIN (attributes);
23566 if (attributes)
23567 TREE_CHAIN (attributes) = NULL_TREE;
23569 /* If there is any qualification still in effect, clear it
23570 now; we will be starting fresh with the next declarator. */
23571 parser->scope = NULL_TREE;
23572 parser->qualifying_scope = NULL_TREE;
23573 parser->object_scope = NULL_TREE;
23574 /* If it's a `,', then there are more declarators. */
23575 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23577 cp_lexer_consume_token (parser->lexer);
23578 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23580 cp_token *token = cp_lexer_previous_token (parser->lexer);
23581 gcc_rich_location richloc (token->location);
23582 richloc.add_fixit_remove ();
23583 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23584 "member declaration");
23587 /* If the next token isn't a `;', then we have a parse error. */
23588 else if (cp_lexer_next_token_is_not (parser->lexer,
23589 CPP_SEMICOLON))
23591 /* The next token might be a ways away from where the
23592 actual semicolon is missing. Find the previous token
23593 and use that for our error position. */
23594 cp_token *token = cp_lexer_previous_token (parser->lexer);
23595 gcc_rich_location richloc (token->location);
23596 richloc.add_fixit_insert_after (";");
23597 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23598 "member declaration");
23600 /* Assume that the user meant to provide a semicolon. If
23601 we were to cp_parser_skip_to_end_of_statement, we might
23602 skip to a semicolon inside a member function definition
23603 and issue nonsensical error messages. */
23604 assume_semicolon = true;
23607 if (decl)
23609 /* Add DECL to the list of members. */
23610 if (!friend_p
23611 /* Explicitly include, eg, NSDMIs, for better error
23612 recovery (c++/58650). */
23613 || !DECL_DECLARES_FUNCTION_P (decl))
23614 finish_member_declaration (decl);
23616 if (TREE_CODE (decl) == FUNCTION_DECL)
23617 cp_parser_save_default_args (parser, decl);
23618 else if (TREE_CODE (decl) == FIELD_DECL
23619 && !DECL_C_BIT_FIELD (decl)
23620 && DECL_INITIAL (decl))
23621 /* Add DECL to the queue of NSDMI to be parsed later. */
23622 vec_safe_push (unparsed_nsdmis, decl);
23625 if (assume_semicolon)
23626 goto out;
23630 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23631 out:
23632 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23635 /* Parse a pure-specifier.
23637 pure-specifier:
23640 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23641 Otherwise, ERROR_MARK_NODE is returned. */
23643 static tree
23644 cp_parser_pure_specifier (cp_parser* parser)
23646 cp_token *token;
23648 /* Look for the `=' token. */
23649 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23650 return error_mark_node;
23651 /* Look for the `0' token. */
23652 token = cp_lexer_peek_token (parser->lexer);
23654 if (token->type == CPP_EOF
23655 || token->type == CPP_PRAGMA_EOL)
23656 return error_mark_node;
23658 cp_lexer_consume_token (parser->lexer);
23660 /* Accept = default or = delete in c++0x mode. */
23661 if (token->keyword == RID_DEFAULT
23662 || token->keyword == RID_DELETE)
23664 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23665 return token->u.value;
23668 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23669 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23671 cp_parser_error (parser,
23672 "invalid pure specifier (only %<= 0%> is allowed)");
23673 cp_parser_skip_to_end_of_statement (parser);
23674 return error_mark_node;
23676 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23678 error_at (token->location, "templates may not be %<virtual%>");
23679 return error_mark_node;
23682 return integer_zero_node;
23685 /* Parse a constant-initializer.
23687 constant-initializer:
23688 = constant-expression
23690 Returns a representation of the constant-expression. */
23692 static tree
23693 cp_parser_constant_initializer (cp_parser* parser)
23695 /* Look for the `=' token. */
23696 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23697 return error_mark_node;
23699 /* It is invalid to write:
23701 struct S { static const int i = { 7 }; };
23704 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23706 cp_parser_error (parser,
23707 "a brace-enclosed initializer is not allowed here");
23708 /* Consume the opening brace. */
23709 cp_lexer_consume_token (parser->lexer);
23710 /* Skip the initializer. */
23711 cp_parser_skip_to_closing_brace (parser);
23712 /* Look for the trailing `}'. */
23713 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23715 return error_mark_node;
23718 return cp_parser_constant_expression (parser);
23721 /* Derived classes [gram.class.derived] */
23723 /* Parse a base-clause.
23725 base-clause:
23726 : base-specifier-list
23728 base-specifier-list:
23729 base-specifier ... [opt]
23730 base-specifier-list , base-specifier ... [opt]
23732 Returns a TREE_LIST representing the base-classes, in the order in
23733 which they were declared. The representation of each node is as
23734 described by cp_parser_base_specifier.
23736 In the case that no bases are specified, this function will return
23737 NULL_TREE, not ERROR_MARK_NODE. */
23739 static tree
23740 cp_parser_base_clause (cp_parser* parser)
23742 tree bases = NULL_TREE;
23744 /* Look for the `:' that begins the list. */
23745 cp_parser_require (parser, CPP_COLON, RT_COLON);
23747 /* Scan the base-specifier-list. */
23748 while (true)
23750 cp_token *token;
23751 tree base;
23752 bool pack_expansion_p = false;
23754 /* Look for the base-specifier. */
23755 base = cp_parser_base_specifier (parser);
23756 /* Look for the (optional) ellipsis. */
23757 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23759 /* Consume the `...'. */
23760 cp_lexer_consume_token (parser->lexer);
23762 pack_expansion_p = true;
23765 /* Add BASE to the front of the list. */
23766 if (base && base != error_mark_node)
23768 if (pack_expansion_p)
23769 /* Make this a pack expansion type. */
23770 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23772 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23774 TREE_CHAIN (base) = bases;
23775 bases = base;
23778 /* Peek at the next token. */
23779 token = cp_lexer_peek_token (parser->lexer);
23780 /* If it's not a comma, then the list is complete. */
23781 if (token->type != CPP_COMMA)
23782 break;
23783 /* Consume the `,'. */
23784 cp_lexer_consume_token (parser->lexer);
23787 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23788 base class had a qualified name. However, the next name that
23789 appears is certainly not qualified. */
23790 parser->scope = NULL_TREE;
23791 parser->qualifying_scope = NULL_TREE;
23792 parser->object_scope = NULL_TREE;
23794 return nreverse (bases);
23797 /* Parse a base-specifier.
23799 base-specifier:
23800 :: [opt] nested-name-specifier [opt] class-name
23801 virtual access-specifier [opt] :: [opt] nested-name-specifier
23802 [opt] class-name
23803 access-specifier virtual [opt] :: [opt] nested-name-specifier
23804 [opt] class-name
23806 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23807 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23808 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23809 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23811 static tree
23812 cp_parser_base_specifier (cp_parser* parser)
23814 cp_token *token;
23815 bool done = false;
23816 bool virtual_p = false;
23817 bool duplicate_virtual_error_issued_p = false;
23818 bool duplicate_access_error_issued_p = false;
23819 bool class_scope_p, template_p;
23820 tree access = access_default_node;
23821 tree type;
23823 /* Process the optional `virtual' and `access-specifier'. */
23824 while (!done)
23826 /* Peek at the next token. */
23827 token = cp_lexer_peek_token (parser->lexer);
23828 /* Process `virtual'. */
23829 switch (token->keyword)
23831 case RID_VIRTUAL:
23832 /* If `virtual' appears more than once, issue an error. */
23833 if (virtual_p && !duplicate_virtual_error_issued_p)
23835 cp_parser_error (parser,
23836 "%<virtual%> specified more than once in base-specifier");
23837 duplicate_virtual_error_issued_p = true;
23840 virtual_p = true;
23842 /* Consume the `virtual' token. */
23843 cp_lexer_consume_token (parser->lexer);
23845 break;
23847 case RID_PUBLIC:
23848 case RID_PROTECTED:
23849 case RID_PRIVATE:
23850 /* If more than one access specifier appears, issue an
23851 error. */
23852 if (access != access_default_node
23853 && !duplicate_access_error_issued_p)
23855 cp_parser_error (parser,
23856 "more than one access specifier in base-specifier");
23857 duplicate_access_error_issued_p = true;
23860 access = ridpointers[(int) token->keyword];
23862 /* Consume the access-specifier. */
23863 cp_lexer_consume_token (parser->lexer);
23865 break;
23867 default:
23868 done = true;
23869 break;
23872 /* It is not uncommon to see programs mechanically, erroneously, use
23873 the 'typename' keyword to denote (dependent) qualified types
23874 as base classes. */
23875 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23877 token = cp_lexer_peek_token (parser->lexer);
23878 if (!processing_template_decl)
23879 error_at (token->location,
23880 "keyword %<typename%> not allowed outside of templates");
23881 else
23882 error_at (token->location,
23883 "keyword %<typename%> not allowed in this context "
23884 "(the base class is implicitly a type)");
23885 cp_lexer_consume_token (parser->lexer);
23888 /* Look for the optional `::' operator. */
23889 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23890 /* Look for the nested-name-specifier. The simplest way to
23891 implement:
23893 [temp.res]
23895 The keyword `typename' is not permitted in a base-specifier or
23896 mem-initializer; in these contexts a qualified name that
23897 depends on a template-parameter is implicitly assumed to be a
23898 type name.
23900 is to pretend that we have seen the `typename' keyword at this
23901 point. */
23902 cp_parser_nested_name_specifier_opt (parser,
23903 /*typename_keyword_p=*/true,
23904 /*check_dependency_p=*/true,
23905 /*type_p=*/true,
23906 /*is_declaration=*/true);
23907 /* If the base class is given by a qualified name, assume that names
23908 we see are type names or templates, as appropriate. */
23909 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23910 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23912 if (!parser->scope
23913 && cp_lexer_next_token_is_decltype (parser->lexer))
23914 /* DR 950 allows decltype as a base-specifier. */
23915 type = cp_parser_decltype (parser);
23916 else
23918 /* Otherwise, look for the class-name. */
23919 type = cp_parser_class_name (parser,
23920 class_scope_p,
23921 template_p,
23922 typename_type,
23923 /*check_dependency_p=*/true,
23924 /*class_head_p=*/false,
23925 /*is_declaration=*/true);
23926 type = TREE_TYPE (type);
23929 if (type == error_mark_node)
23930 return error_mark_node;
23932 return finish_base_specifier (type, access, virtual_p);
23935 /* Exception handling [gram.exception] */
23937 /* Parse an (optional) noexcept-specification.
23939 noexcept-specification:
23940 noexcept ( constant-expression ) [opt]
23942 If no noexcept-specification is present, returns NULL_TREE.
23943 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23944 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23945 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23946 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23947 in which case a boolean condition is returned instead. */
23949 static tree
23950 cp_parser_noexcept_specification_opt (cp_parser* parser,
23951 bool require_constexpr,
23952 bool* consumed_expr,
23953 bool return_cond)
23955 cp_token *token;
23956 const char *saved_message;
23958 /* Peek at the next token. */
23959 token = cp_lexer_peek_token (parser->lexer);
23961 /* Is it a noexcept-specification? */
23962 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23964 tree expr;
23965 cp_lexer_consume_token (parser->lexer);
23967 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23969 cp_lexer_consume_token (parser->lexer);
23971 if (require_constexpr)
23973 /* Types may not be defined in an exception-specification. */
23974 saved_message = parser->type_definition_forbidden_message;
23975 parser->type_definition_forbidden_message
23976 = G_("types may not be defined in an exception-specification");
23978 expr = cp_parser_constant_expression (parser);
23980 /* Restore the saved message. */
23981 parser->type_definition_forbidden_message = saved_message;
23983 else
23985 expr = cp_parser_expression (parser);
23986 *consumed_expr = true;
23989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23991 else
23993 expr = boolean_true_node;
23994 if (!require_constexpr)
23995 *consumed_expr = false;
23998 /* We cannot build a noexcept-spec right away because this will check
23999 that expr is a constexpr. */
24000 if (!return_cond)
24001 return build_noexcept_spec (expr, tf_warning_or_error);
24002 else
24003 return expr;
24005 else
24006 return NULL_TREE;
24009 /* Parse an (optional) exception-specification.
24011 exception-specification:
24012 throw ( type-id-list [opt] )
24014 Returns a TREE_LIST representing the exception-specification. The
24015 TREE_VALUE of each node is a type. */
24017 static tree
24018 cp_parser_exception_specification_opt (cp_parser* parser)
24020 cp_token *token;
24021 tree type_id_list;
24022 const char *saved_message;
24024 /* Peek at the next token. */
24025 token = cp_lexer_peek_token (parser->lexer);
24027 /* Is it a noexcept-specification? */
24028 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24029 false);
24030 if (type_id_list != NULL_TREE)
24031 return type_id_list;
24033 /* If it's not `throw', then there's no exception-specification. */
24034 if (!cp_parser_is_keyword (token, RID_THROW))
24035 return NULL_TREE;
24037 location_t loc = token->location;
24039 /* Consume the `throw'. */
24040 cp_lexer_consume_token (parser->lexer);
24042 /* Look for the `('. */
24043 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24045 /* Peek at the next token. */
24046 token = cp_lexer_peek_token (parser->lexer);
24047 /* If it's not a `)', then there is a type-id-list. */
24048 if (token->type != CPP_CLOSE_PAREN)
24050 /* Types may not be defined in an exception-specification. */
24051 saved_message = parser->type_definition_forbidden_message;
24052 parser->type_definition_forbidden_message
24053 = G_("types may not be defined in an exception-specification");
24054 /* Parse the type-id-list. */
24055 type_id_list = cp_parser_type_id_list (parser);
24056 /* Restore the saved message. */
24057 parser->type_definition_forbidden_message = saved_message;
24059 if (cxx_dialect >= cxx1z)
24061 error_at (loc, "ISO C++1z does not allow dynamic exception "
24062 "specifications");
24063 type_id_list = NULL_TREE;
24065 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24066 warning_at (loc, OPT_Wdeprecated,
24067 "dynamic exception specifications are deprecated in "
24068 "C++11");
24070 /* In C++17, throw() is equivalent to noexcept (true). throw()
24071 is deprecated in C++11 and above as well, but is still widely used,
24072 so don't warn about it yet. */
24073 else if (cxx_dialect >= cxx1z)
24074 type_id_list = noexcept_true_spec;
24075 else
24076 type_id_list = empty_except_spec;
24078 /* Look for the `)'. */
24079 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24081 return type_id_list;
24084 /* Parse an (optional) type-id-list.
24086 type-id-list:
24087 type-id ... [opt]
24088 type-id-list , type-id ... [opt]
24090 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24091 in the order that the types were presented. */
24093 static tree
24094 cp_parser_type_id_list (cp_parser* parser)
24096 tree types = NULL_TREE;
24098 while (true)
24100 cp_token *token;
24101 tree type;
24103 token = cp_lexer_peek_token (parser->lexer);
24105 /* Get the next type-id. */
24106 type = cp_parser_type_id (parser);
24107 /* Check for invalid 'auto'. */
24108 if (flag_concepts && type_uses_auto (type))
24110 error_at (token->location,
24111 "invalid use of %<auto%> in exception-specification");
24112 type = error_mark_node;
24114 /* Parse the optional ellipsis. */
24115 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24117 /* Consume the `...'. */
24118 cp_lexer_consume_token (parser->lexer);
24120 /* Turn the type into a pack expansion expression. */
24121 type = make_pack_expansion (type);
24123 /* Add it to the list. */
24124 types = add_exception_specifier (types, type, /*complain=*/1);
24125 /* Peek at the next token. */
24126 token = cp_lexer_peek_token (parser->lexer);
24127 /* If it is not a `,', we are done. */
24128 if (token->type != CPP_COMMA)
24129 break;
24130 /* Consume the `,'. */
24131 cp_lexer_consume_token (parser->lexer);
24134 return nreverse (types);
24137 /* Parse a try-block.
24139 try-block:
24140 try compound-statement handler-seq */
24142 static tree
24143 cp_parser_try_block (cp_parser* parser)
24145 tree try_block;
24147 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24148 if (parser->in_function_body
24149 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24150 error ("%<try%> in %<constexpr%> function");
24152 try_block = begin_try_block ();
24153 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24154 finish_try_block (try_block);
24155 cp_parser_handler_seq (parser);
24156 finish_handler_sequence (try_block);
24158 return try_block;
24161 /* Parse a function-try-block.
24163 function-try-block:
24164 try ctor-initializer [opt] function-body handler-seq */
24166 static bool
24167 cp_parser_function_try_block (cp_parser* parser)
24169 tree compound_stmt;
24170 tree try_block;
24171 bool ctor_initializer_p;
24173 /* Look for the `try' keyword. */
24174 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24175 return false;
24176 /* Let the rest of the front end know where we are. */
24177 try_block = begin_function_try_block (&compound_stmt);
24178 /* Parse the function-body. */
24179 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24180 (parser, /*in_function_try_block=*/true);
24181 /* We're done with the `try' part. */
24182 finish_function_try_block (try_block);
24183 /* Parse the handlers. */
24184 cp_parser_handler_seq (parser);
24185 /* We're done with the handlers. */
24186 finish_function_handler_sequence (try_block, compound_stmt);
24188 return ctor_initializer_p;
24191 /* Parse a handler-seq.
24193 handler-seq:
24194 handler handler-seq [opt] */
24196 static void
24197 cp_parser_handler_seq (cp_parser* parser)
24199 while (true)
24201 cp_token *token;
24203 /* Parse the handler. */
24204 cp_parser_handler (parser);
24205 /* Peek at the next token. */
24206 token = cp_lexer_peek_token (parser->lexer);
24207 /* If it's not `catch' then there are no more handlers. */
24208 if (!cp_parser_is_keyword (token, RID_CATCH))
24209 break;
24213 /* Parse a handler.
24215 handler:
24216 catch ( exception-declaration ) compound-statement */
24218 static void
24219 cp_parser_handler (cp_parser* parser)
24221 tree handler;
24222 tree declaration;
24224 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24225 handler = begin_handler ();
24226 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24227 declaration = cp_parser_exception_declaration (parser);
24228 finish_handler_parms (declaration, handler);
24229 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24230 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24231 finish_handler (handler);
24234 /* Parse an exception-declaration.
24236 exception-declaration:
24237 type-specifier-seq declarator
24238 type-specifier-seq abstract-declarator
24239 type-specifier-seq
24242 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24243 ellipsis variant is used. */
24245 static tree
24246 cp_parser_exception_declaration (cp_parser* parser)
24248 cp_decl_specifier_seq type_specifiers;
24249 cp_declarator *declarator;
24250 const char *saved_message;
24252 /* If it's an ellipsis, it's easy to handle. */
24253 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24255 /* Consume the `...' token. */
24256 cp_lexer_consume_token (parser->lexer);
24257 return NULL_TREE;
24260 /* Types may not be defined in exception-declarations. */
24261 saved_message = parser->type_definition_forbidden_message;
24262 parser->type_definition_forbidden_message
24263 = G_("types may not be defined in exception-declarations");
24265 /* Parse the type-specifier-seq. */
24266 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24267 /*is_trailing_return=*/false,
24268 &type_specifiers);
24269 /* If it's a `)', then there is no declarator. */
24270 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24271 declarator = NULL;
24272 else
24273 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24274 /*ctor_dtor_or_conv_p=*/NULL,
24275 /*parenthesized_p=*/NULL,
24276 /*member_p=*/false,
24277 /*friend_p=*/false);
24279 /* Restore the saved message. */
24280 parser->type_definition_forbidden_message = saved_message;
24282 if (!type_specifiers.any_specifiers_p)
24283 return error_mark_node;
24285 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24288 /* Parse a throw-expression.
24290 throw-expression:
24291 throw assignment-expression [opt]
24293 Returns a THROW_EXPR representing the throw-expression. */
24295 static tree
24296 cp_parser_throw_expression (cp_parser* parser)
24298 tree expression;
24299 cp_token* token;
24301 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24302 token = cp_lexer_peek_token (parser->lexer);
24303 /* Figure out whether or not there is an assignment-expression
24304 following the "throw" keyword. */
24305 if (token->type == CPP_COMMA
24306 || token->type == CPP_SEMICOLON
24307 || token->type == CPP_CLOSE_PAREN
24308 || token->type == CPP_CLOSE_SQUARE
24309 || token->type == CPP_CLOSE_BRACE
24310 || token->type == CPP_COLON)
24311 expression = NULL_TREE;
24312 else
24313 expression = cp_parser_assignment_expression (parser);
24315 return build_throw (expression);
24318 /* GNU Extensions */
24320 /* Parse an (optional) asm-specification.
24322 asm-specification:
24323 asm ( string-literal )
24325 If the asm-specification is present, returns a STRING_CST
24326 corresponding to the string-literal. Otherwise, returns
24327 NULL_TREE. */
24329 static tree
24330 cp_parser_asm_specification_opt (cp_parser* parser)
24332 cp_token *token;
24333 tree asm_specification;
24335 /* Peek at the next token. */
24336 token = cp_lexer_peek_token (parser->lexer);
24337 /* If the next token isn't the `asm' keyword, then there's no
24338 asm-specification. */
24339 if (!cp_parser_is_keyword (token, RID_ASM))
24340 return NULL_TREE;
24342 /* Consume the `asm' token. */
24343 cp_lexer_consume_token (parser->lexer);
24344 /* Look for the `('. */
24345 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24347 /* Look for the string-literal. */
24348 asm_specification = cp_parser_string_literal (parser, false, false);
24350 /* Look for the `)'. */
24351 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24353 return asm_specification;
24356 /* Parse an asm-operand-list.
24358 asm-operand-list:
24359 asm-operand
24360 asm-operand-list , asm-operand
24362 asm-operand:
24363 string-literal ( expression )
24364 [ string-literal ] string-literal ( expression )
24366 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24367 each node is the expression. The TREE_PURPOSE is itself a
24368 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24369 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24370 is a STRING_CST for the string literal before the parenthesis. Returns
24371 ERROR_MARK_NODE if any of the operands are invalid. */
24373 static tree
24374 cp_parser_asm_operand_list (cp_parser* parser)
24376 tree asm_operands = NULL_TREE;
24377 bool invalid_operands = false;
24379 while (true)
24381 tree string_literal;
24382 tree expression;
24383 tree name;
24385 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24387 /* Consume the `[' token. */
24388 cp_lexer_consume_token (parser->lexer);
24389 /* Read the operand name. */
24390 name = cp_parser_identifier (parser);
24391 if (name != error_mark_node)
24392 name = build_string (IDENTIFIER_LENGTH (name),
24393 IDENTIFIER_POINTER (name));
24394 /* Look for the closing `]'. */
24395 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24397 else
24398 name = NULL_TREE;
24399 /* Look for the string-literal. */
24400 string_literal = cp_parser_string_literal (parser, false, false);
24402 /* Look for the `('. */
24403 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24404 /* Parse the expression. */
24405 expression = cp_parser_expression (parser);
24406 /* Look for the `)'. */
24407 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24409 if (name == error_mark_node
24410 || string_literal == error_mark_node
24411 || expression == error_mark_node)
24412 invalid_operands = true;
24414 /* Add this operand to the list. */
24415 asm_operands = tree_cons (build_tree_list (name, string_literal),
24416 expression,
24417 asm_operands);
24418 /* If the next token is not a `,', there are no more
24419 operands. */
24420 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24421 break;
24422 /* Consume the `,'. */
24423 cp_lexer_consume_token (parser->lexer);
24426 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24429 /* Parse an asm-clobber-list.
24431 asm-clobber-list:
24432 string-literal
24433 asm-clobber-list , string-literal
24435 Returns a TREE_LIST, indicating the clobbers in the order that they
24436 appeared. The TREE_VALUE of each node is a STRING_CST. */
24438 static tree
24439 cp_parser_asm_clobber_list (cp_parser* parser)
24441 tree clobbers = NULL_TREE;
24443 while (true)
24445 tree string_literal;
24447 /* Look for the string literal. */
24448 string_literal = cp_parser_string_literal (parser, false, false);
24449 /* Add it to the list. */
24450 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24451 /* If the next token is not a `,', then the list is
24452 complete. */
24453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24454 break;
24455 /* Consume the `,' token. */
24456 cp_lexer_consume_token (parser->lexer);
24459 return clobbers;
24462 /* Parse an asm-label-list.
24464 asm-label-list:
24465 identifier
24466 asm-label-list , identifier
24468 Returns a TREE_LIST, indicating the labels in the order that they
24469 appeared. The TREE_VALUE of each node is a label. */
24471 static tree
24472 cp_parser_asm_label_list (cp_parser* parser)
24474 tree labels = NULL_TREE;
24476 while (true)
24478 tree identifier, label, name;
24480 /* Look for the identifier. */
24481 identifier = cp_parser_identifier (parser);
24482 if (!error_operand_p (identifier))
24484 label = lookup_label (identifier);
24485 if (TREE_CODE (label) == LABEL_DECL)
24487 TREE_USED (label) = 1;
24488 check_goto (label);
24489 name = build_string (IDENTIFIER_LENGTH (identifier),
24490 IDENTIFIER_POINTER (identifier));
24491 labels = tree_cons (name, label, labels);
24494 /* If the next token is not a `,', then the list is
24495 complete. */
24496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24497 break;
24498 /* Consume the `,' token. */
24499 cp_lexer_consume_token (parser->lexer);
24502 return nreverse (labels);
24505 /* Return TRUE iff the next tokens in the stream are possibly the
24506 beginning of a GNU extension attribute. */
24508 static bool
24509 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24511 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24514 /* Return TRUE iff the next tokens in the stream are possibly the
24515 beginning of a standard C++-11 attribute specifier. */
24517 static bool
24518 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24520 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24523 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24524 beginning of a standard C++-11 attribute specifier. */
24526 static bool
24527 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24529 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24531 return (cxx_dialect >= cxx11
24532 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24533 || (token->type == CPP_OPEN_SQUARE
24534 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24535 && token->type == CPP_OPEN_SQUARE)));
24538 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24539 beginning of a GNU extension attribute. */
24541 static bool
24542 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24544 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24546 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24549 /* Return true iff the next tokens can be the beginning of either a
24550 GNU attribute list, or a standard C++11 attribute sequence. */
24552 static bool
24553 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24555 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24556 || cp_next_tokens_can_be_std_attribute_p (parser));
24559 /* Return true iff the next Nth tokens can be the beginning of either
24560 a GNU attribute list, or a standard C++11 attribute sequence. */
24562 static bool
24563 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24565 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24566 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24569 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24570 of GNU attributes, or return NULL. */
24572 static tree
24573 cp_parser_attributes_opt (cp_parser *parser)
24575 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24576 return cp_parser_gnu_attributes_opt (parser);
24577 return cp_parser_std_attribute_spec_seq (parser);
24580 #define CILK_SIMD_FN_CLAUSE_MASK \
24581 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24582 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24583 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24584 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24585 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24587 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24588 vector [(<clauses>)] */
24590 static void
24591 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24593 bool first_p = parser->cilk_simd_fn_info == NULL;
24594 cp_token *token = v_token;
24595 if (first_p)
24597 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24598 parser->cilk_simd_fn_info->error_seen = false;
24599 parser->cilk_simd_fn_info->fndecl_seen = false;
24600 parser->cilk_simd_fn_info->tokens = vNULL;
24601 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24603 int paren_scope = 0;
24604 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24606 cp_lexer_consume_token (parser->lexer);
24607 v_token = cp_lexer_peek_token (parser->lexer);
24608 paren_scope++;
24610 while (paren_scope > 0)
24612 token = cp_lexer_peek_token (parser->lexer);
24613 if (token->type == CPP_OPEN_PAREN)
24614 paren_scope++;
24615 else if (token->type == CPP_CLOSE_PAREN)
24616 paren_scope--;
24617 /* Do not push the last ')' */
24618 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24619 cp_lexer_consume_token (parser->lexer);
24622 token->type = CPP_PRAGMA_EOL;
24623 parser->lexer->next_token = token;
24624 cp_lexer_consume_token (parser->lexer);
24626 struct cp_token_cache *cp
24627 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24628 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24631 /* Parse an (optional) series of attributes.
24633 attributes:
24634 attributes attribute
24636 attribute:
24637 __attribute__ (( attribute-list [opt] ))
24639 The return value is as for cp_parser_gnu_attribute_list. */
24641 static tree
24642 cp_parser_gnu_attributes_opt (cp_parser* parser)
24644 tree attributes = NULL_TREE;
24646 while (true)
24648 cp_token *token;
24649 tree attribute_list;
24650 bool ok = true;
24652 /* Peek at the next token. */
24653 token = cp_lexer_peek_token (parser->lexer);
24654 /* If it's not `__attribute__', then we're done. */
24655 if (token->keyword != RID_ATTRIBUTE)
24656 break;
24658 /* Consume the `__attribute__' keyword. */
24659 cp_lexer_consume_token (parser->lexer);
24660 /* Look for the two `(' tokens. */
24661 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24662 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24664 /* Peek at the next token. */
24665 token = cp_lexer_peek_token (parser->lexer);
24666 if (token->type != CPP_CLOSE_PAREN)
24667 /* Parse the attribute-list. */
24668 attribute_list = cp_parser_gnu_attribute_list (parser);
24669 else
24670 /* If the next token is a `)', then there is no attribute
24671 list. */
24672 attribute_list = NULL;
24674 /* Look for the two `)' tokens. */
24675 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24676 ok = false;
24677 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24678 ok = false;
24679 if (!ok)
24680 cp_parser_skip_to_end_of_statement (parser);
24682 /* Add these new attributes to the list. */
24683 attributes = chainon (attributes, attribute_list);
24686 return attributes;
24689 /* Parse a GNU attribute-list.
24691 attribute-list:
24692 attribute
24693 attribute-list , attribute
24695 attribute:
24696 identifier
24697 identifier ( identifier )
24698 identifier ( identifier , expression-list )
24699 identifier ( expression-list )
24701 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24702 to an attribute. The TREE_PURPOSE of each node is the identifier
24703 indicating which attribute is in use. The TREE_VALUE represents
24704 the arguments, if any. */
24706 static tree
24707 cp_parser_gnu_attribute_list (cp_parser* parser)
24709 tree attribute_list = NULL_TREE;
24710 bool save_translate_strings_p = parser->translate_strings_p;
24712 parser->translate_strings_p = false;
24713 while (true)
24715 cp_token *token;
24716 tree identifier;
24717 tree attribute;
24719 /* Look for the identifier. We also allow keywords here; for
24720 example `__attribute__ ((const))' is legal. */
24721 token = cp_lexer_peek_token (parser->lexer);
24722 if (token->type == CPP_NAME
24723 || token->type == CPP_KEYWORD)
24725 tree arguments = NULL_TREE;
24727 /* Consume the token, but save it since we need it for the
24728 SIMD enabled function parsing. */
24729 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24731 /* Save away the identifier that indicates which attribute
24732 this is. */
24733 identifier = (token->type == CPP_KEYWORD)
24734 /* For keywords, use the canonical spelling, not the
24735 parsed identifier. */
24736 ? ridpointers[(int) token->keyword]
24737 : id_token->u.value;
24739 attribute = build_tree_list (identifier, NULL_TREE);
24741 /* Peek at the next token. */
24742 token = cp_lexer_peek_token (parser->lexer);
24743 /* If it's an `(', then parse the attribute arguments. */
24744 if (token->type == CPP_OPEN_PAREN)
24746 vec<tree, va_gc> *vec;
24747 int attr_flag = (attribute_takes_identifier_p (identifier)
24748 ? id_attr : normal_attr);
24749 if (is_cilkplus_vector_p (identifier))
24751 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24752 continue;
24754 else
24755 vec = cp_parser_parenthesized_expression_list
24756 (parser, attr_flag, /*cast_p=*/false,
24757 /*allow_expansion_p=*/false,
24758 /*non_constant_p=*/NULL);
24759 if (vec == NULL)
24760 arguments = error_mark_node;
24761 else
24763 arguments = build_tree_list_vec (vec);
24764 release_tree_vector (vec);
24766 /* Save the arguments away. */
24767 TREE_VALUE (attribute) = arguments;
24769 else if (is_cilkplus_vector_p (identifier))
24771 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24772 continue;
24775 if (arguments != error_mark_node)
24777 /* Add this attribute to the list. */
24778 TREE_CHAIN (attribute) = attribute_list;
24779 attribute_list = attribute;
24782 token = cp_lexer_peek_token (parser->lexer);
24784 /* Now, look for more attributes. If the next token isn't a
24785 `,', we're done. */
24786 if (token->type != CPP_COMMA)
24787 break;
24789 /* Consume the comma and keep going. */
24790 cp_lexer_consume_token (parser->lexer);
24792 parser->translate_strings_p = save_translate_strings_p;
24794 /* We built up the list in reverse order. */
24795 return nreverse (attribute_list);
24798 /* Parse a standard C++11 attribute.
24800 The returned representation is a TREE_LIST which TREE_PURPOSE is
24801 the scoped name of the attribute, and the TREE_VALUE is its
24802 arguments list.
24804 Note that the scoped name of the attribute is itself a TREE_LIST
24805 which TREE_PURPOSE is the namespace of the attribute, and
24806 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24807 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24808 and which TREE_PURPOSE is directly the attribute name.
24810 Clients of the attribute code should use get_attribute_namespace
24811 and get_attribute_name to get the actual namespace and name of
24812 attributes, regardless of their being GNU or C++11 attributes.
24814 attribute:
24815 attribute-token attribute-argument-clause [opt]
24817 attribute-token:
24818 identifier
24819 attribute-scoped-token
24821 attribute-scoped-token:
24822 attribute-namespace :: identifier
24824 attribute-namespace:
24825 identifier
24827 attribute-argument-clause:
24828 ( balanced-token-seq )
24830 balanced-token-seq:
24831 balanced-token [opt]
24832 balanced-token-seq balanced-token
24834 balanced-token:
24835 ( balanced-token-seq )
24836 [ balanced-token-seq ]
24837 { balanced-token-seq }. */
24839 static tree
24840 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24842 tree attribute, attr_id = NULL_TREE, arguments;
24843 cp_token *token;
24845 /* First, parse name of the attribute, a.k.a attribute-token. */
24847 token = cp_lexer_peek_token (parser->lexer);
24848 if (token->type == CPP_NAME)
24849 attr_id = token->u.value;
24850 else if (token->type == CPP_KEYWORD)
24851 attr_id = ridpointers[(int) token->keyword];
24852 else if (token->flags & NAMED_OP)
24853 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24855 if (attr_id == NULL_TREE)
24856 return NULL_TREE;
24858 cp_lexer_consume_token (parser->lexer);
24860 token = cp_lexer_peek_token (parser->lexer);
24861 if (token->type == CPP_SCOPE)
24863 /* We are seeing a scoped attribute token. */
24865 cp_lexer_consume_token (parser->lexer);
24866 if (attr_ns)
24867 error_at (token->location, "attribute using prefix used together "
24868 "with scoped attribute token");
24869 attr_ns = attr_id;
24871 token = cp_lexer_consume_token (parser->lexer);
24872 if (token->type == CPP_NAME)
24873 attr_id = token->u.value;
24874 else if (token->type == CPP_KEYWORD)
24875 attr_id = ridpointers[(int) token->keyword];
24876 else if (token->flags & NAMED_OP)
24877 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24878 else
24880 error_at (token->location,
24881 "expected an identifier for the attribute name");
24882 return error_mark_node;
24884 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24885 NULL_TREE);
24886 token = cp_lexer_peek_token (parser->lexer);
24888 else if (attr_ns)
24889 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24890 NULL_TREE);
24891 else
24893 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24894 NULL_TREE);
24895 /* C++11 noreturn attribute is equivalent to GNU's. */
24896 if (is_attribute_p ("noreturn", attr_id))
24897 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24898 /* C++14 deprecated attribute is equivalent to GNU's. */
24899 else if (is_attribute_p ("deprecated", attr_id))
24900 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24901 /* C++17 fallthrough attribute is equivalent to GNU's. */
24902 else if (is_attribute_p ("fallthrough", attr_id))
24903 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24904 /* Transactional Memory TS optimize_for_synchronized attribute is
24905 equivalent to GNU transaction_callable. */
24906 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24907 TREE_PURPOSE (attribute)
24908 = get_identifier ("transaction_callable");
24909 /* Transactional Memory attributes are GNU attributes. */
24910 else if (tm_attr_to_mask (attr_id))
24911 TREE_PURPOSE (attribute) = attr_id;
24914 /* Now parse the optional argument clause of the attribute. */
24916 if (token->type != CPP_OPEN_PAREN)
24917 return attribute;
24920 vec<tree, va_gc> *vec;
24921 int attr_flag = normal_attr;
24923 if (attr_ns == get_identifier ("gnu")
24924 && attribute_takes_identifier_p (attr_id))
24925 /* A GNU attribute that takes an identifier in parameter. */
24926 attr_flag = id_attr;
24928 vec = cp_parser_parenthesized_expression_list
24929 (parser, attr_flag, /*cast_p=*/false,
24930 /*allow_expansion_p=*/true,
24931 /*non_constant_p=*/NULL);
24932 if (vec == NULL)
24933 arguments = error_mark_node;
24934 else
24936 arguments = build_tree_list_vec (vec);
24937 release_tree_vector (vec);
24940 if (arguments == error_mark_node)
24941 attribute = error_mark_node;
24942 else
24943 TREE_VALUE (attribute) = arguments;
24946 return attribute;
24949 /* Check that the attribute ATTRIBUTE appears at most once in the
24950 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24951 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24952 isn't implemented yet in GCC. */
24954 static void
24955 cp_parser_check_std_attribute (tree attributes, tree attribute)
24957 if (attributes)
24959 tree name = get_attribute_name (attribute);
24960 if (is_attribute_p ("noreturn", name)
24961 && lookup_attribute ("noreturn", attributes))
24962 error ("attribute %<noreturn%> can appear at most once "
24963 "in an attribute-list");
24964 else if (is_attribute_p ("deprecated", name)
24965 && lookup_attribute ("deprecated", attributes))
24966 error ("attribute %<deprecated%> can appear at most once "
24967 "in an attribute-list");
24971 /* Parse a list of standard C++-11 attributes.
24973 attribute-list:
24974 attribute [opt]
24975 attribute-list , attribute[opt]
24976 attribute ...
24977 attribute-list , attribute ...
24980 static tree
24981 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24983 tree attributes = NULL_TREE, attribute = NULL_TREE;
24984 cp_token *token = NULL;
24986 while (true)
24988 attribute = cp_parser_std_attribute (parser, attr_ns);
24989 if (attribute == error_mark_node)
24990 break;
24991 if (attribute != NULL_TREE)
24993 cp_parser_check_std_attribute (attributes, attribute);
24994 TREE_CHAIN (attribute) = attributes;
24995 attributes = attribute;
24997 token = cp_lexer_peek_token (parser->lexer);
24998 if (token->type == CPP_ELLIPSIS)
25000 cp_lexer_consume_token (parser->lexer);
25001 if (attribute == NULL_TREE)
25002 error_at (token->location,
25003 "expected attribute before %<...%>");
25004 else
25006 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25007 if (pack == error_mark_node)
25008 return error_mark_node;
25009 TREE_VALUE (attribute) = pack;
25011 token = cp_lexer_peek_token (parser->lexer);
25013 if (token->type != CPP_COMMA)
25014 break;
25015 cp_lexer_consume_token (parser->lexer);
25017 attributes = nreverse (attributes);
25018 return attributes;
25021 /* Parse a standard C++-11 attribute specifier.
25023 attribute-specifier:
25024 [ [ attribute-using-prefix [opt] attribute-list ] ]
25025 alignment-specifier
25027 attribute-using-prefix:
25028 using attribute-namespace :
25030 alignment-specifier:
25031 alignas ( type-id ... [opt] )
25032 alignas ( alignment-expression ... [opt] ). */
25034 static tree
25035 cp_parser_std_attribute_spec (cp_parser *parser)
25037 tree attributes = NULL_TREE;
25038 cp_token *token = cp_lexer_peek_token (parser->lexer);
25040 if (token->type == CPP_OPEN_SQUARE
25041 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25043 tree attr_ns = NULL_TREE;
25045 cp_lexer_consume_token (parser->lexer);
25046 cp_lexer_consume_token (parser->lexer);
25048 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25050 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25051 if (token->type == CPP_NAME)
25052 attr_ns = token->u.value;
25053 else if (token->type == CPP_KEYWORD)
25054 attr_ns = ridpointers[(int) token->keyword];
25055 else if (token->flags & NAMED_OP)
25056 attr_ns = get_identifier (cpp_type2name (token->type,
25057 token->flags));
25058 if (attr_ns
25059 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25061 if (cxx_dialect < cxx1z
25062 && !in_system_header_at (input_location))
25063 pedwarn (input_location, 0,
25064 "attribute using prefix only available "
25065 "with -std=c++1z or -std=gnu++1z");
25067 cp_lexer_consume_token (parser->lexer);
25068 cp_lexer_consume_token (parser->lexer);
25069 cp_lexer_consume_token (parser->lexer);
25071 else
25072 attr_ns = NULL_TREE;
25075 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25077 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25078 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25079 cp_parser_skip_to_end_of_statement (parser);
25080 else
25081 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25082 when we are sure that we have actually parsed them. */
25083 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25085 else
25087 tree alignas_expr;
25089 /* Look for an alignment-specifier. */
25091 token = cp_lexer_peek_token (parser->lexer);
25093 if (token->type != CPP_KEYWORD
25094 || token->keyword != RID_ALIGNAS)
25095 return NULL_TREE;
25097 cp_lexer_consume_token (parser->lexer);
25098 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25100 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
25102 cp_parser_error (parser, "expected %<(%>");
25103 return error_mark_node;
25106 cp_parser_parse_tentatively (parser);
25107 alignas_expr = cp_parser_type_id (parser);
25109 if (!cp_parser_parse_definitely (parser))
25111 alignas_expr = cp_parser_assignment_expression (parser);
25112 if (alignas_expr == error_mark_node)
25113 cp_parser_skip_to_end_of_statement (parser);
25114 if (alignas_expr == NULL_TREE
25115 || alignas_expr == error_mark_node)
25116 return alignas_expr;
25119 alignas_expr = cxx_alignas_expr (alignas_expr);
25120 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25122 /* Handle alignas (pack...). */
25123 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25125 cp_lexer_consume_token (parser->lexer);
25126 alignas_expr = make_pack_expansion (alignas_expr);
25129 /* Something went wrong, so don't build the attribute. */
25130 if (alignas_expr == error_mark_node)
25131 return error_mark_node;
25133 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25135 cp_parser_error (parser, "expected %<)%>");
25136 return error_mark_node;
25139 /* Build the C++-11 representation of an 'aligned'
25140 attribute. */
25141 attributes =
25142 build_tree_list (build_tree_list (get_identifier ("gnu"),
25143 get_identifier ("aligned")),
25144 alignas_expr);
25147 return attributes;
25150 /* Parse a standard C++-11 attribute-specifier-seq.
25152 attribute-specifier-seq:
25153 attribute-specifier-seq [opt] attribute-specifier
25156 static tree
25157 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25159 tree attr_specs = NULL_TREE;
25160 tree attr_last = NULL_TREE;
25162 while (true)
25164 tree attr_spec = cp_parser_std_attribute_spec (parser);
25165 if (attr_spec == NULL_TREE)
25166 break;
25167 if (attr_spec == error_mark_node)
25168 return error_mark_node;
25170 if (attr_last)
25171 TREE_CHAIN (attr_last) = attr_spec;
25172 else
25173 attr_specs = attr_last = attr_spec;
25174 attr_last = tree_last (attr_last);
25177 return attr_specs;
25180 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25181 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25182 current value of the PEDANTIC flag, regardless of whether or not
25183 the `__extension__' keyword is present. The caller is responsible
25184 for restoring the value of the PEDANTIC flag. */
25186 static bool
25187 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25189 /* Save the old value of the PEDANTIC flag. */
25190 *saved_pedantic = pedantic;
25192 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25194 /* Consume the `__extension__' token. */
25195 cp_lexer_consume_token (parser->lexer);
25196 /* We're not being pedantic while the `__extension__' keyword is
25197 in effect. */
25198 pedantic = 0;
25200 return true;
25203 return false;
25206 /* Parse a label declaration.
25208 label-declaration:
25209 __label__ label-declarator-seq ;
25211 label-declarator-seq:
25212 identifier , label-declarator-seq
25213 identifier */
25215 static void
25216 cp_parser_label_declaration (cp_parser* parser)
25218 /* Look for the `__label__' keyword. */
25219 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25221 while (true)
25223 tree identifier;
25225 /* Look for an identifier. */
25226 identifier = cp_parser_identifier (parser);
25227 /* If we failed, stop. */
25228 if (identifier == error_mark_node)
25229 break;
25230 /* Declare it as a label. */
25231 finish_label_decl (identifier);
25232 /* If the next token is a `;', stop. */
25233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25234 break;
25235 /* Look for the `,' separating the label declarations. */
25236 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25239 /* Look for the final `;'. */
25240 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25243 // -------------------------------------------------------------------------- //
25244 // Requires Clause
25246 // Parse a requires clause.
25248 // requires-clause:
25249 // 'requires' logical-or-expression
25251 // The required logical-or-expression must be a constant expression. Note
25252 // that we don't check that the expression is constepxr here. We defer until
25253 // we analyze constraints and then, we only check atomic constraints.
25254 static tree
25255 cp_parser_requires_clause (cp_parser *parser)
25257 // Parse the requires clause so that it is not automatically folded.
25258 ++processing_template_decl;
25259 tree expr = cp_parser_binary_expression (parser, false, false,
25260 PREC_NOT_OPERATOR, NULL);
25261 if (check_for_bare_parameter_packs (expr))
25262 expr = error_mark_node;
25263 --processing_template_decl;
25264 return expr;
25267 // Optionally parse a requires clause:
25268 static tree
25269 cp_parser_requires_clause_opt (cp_parser *parser)
25271 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25272 if (tok->keyword != RID_REQUIRES)
25274 if (!flag_concepts && tok->type == CPP_NAME
25275 && tok->u.value == ridpointers[RID_REQUIRES])
25277 error_at (cp_lexer_peek_token (parser->lexer)->location,
25278 "%<requires%> only available with -fconcepts");
25279 /* Parse and discard the requires-clause. */
25280 cp_lexer_consume_token (parser->lexer);
25281 cp_parser_requires_clause (parser);
25283 return NULL_TREE;
25285 cp_lexer_consume_token (parser->lexer);
25286 return cp_parser_requires_clause (parser);
25290 /*---------------------------------------------------------------------------
25291 Requires expressions
25292 ---------------------------------------------------------------------------*/
25294 /* Parse a requires expression
25296 requirement-expression:
25297 'requires' requirement-parameter-list [opt] requirement-body */
25298 static tree
25299 cp_parser_requires_expression (cp_parser *parser)
25301 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25302 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25304 /* A requires-expression shall appear only within a concept
25305 definition or a requires-clause.
25307 TODO: Implement this diagnostic correctly. */
25308 if (!processing_template_decl)
25310 error_at (loc, "a requires expression cannot appear outside a template");
25311 cp_parser_skip_to_end_of_statement (parser);
25312 return error_mark_node;
25315 tree parms, reqs;
25317 /* Local parameters are delared as variables within the scope
25318 of the expression. They are not visible past the end of
25319 the expression. Expressions within the requires-expression
25320 are unevaluated. */
25321 struct scope_sentinel
25323 scope_sentinel ()
25325 ++cp_unevaluated_operand;
25326 begin_scope (sk_block, NULL_TREE);
25329 ~scope_sentinel ()
25331 pop_bindings_and_leave_scope ();
25332 --cp_unevaluated_operand;
25334 } s;
25336 /* Parse the optional parameter list. */
25337 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25339 parms = cp_parser_requirement_parameter_list (parser);
25340 if (parms == error_mark_node)
25341 return error_mark_node;
25343 else
25344 parms = NULL_TREE;
25346 /* Parse the requirement body. */
25347 reqs = cp_parser_requirement_body (parser);
25348 if (reqs == error_mark_node)
25349 return error_mark_node;
25352 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25353 the parm chain. */
25354 grokparms (parms, &parms);
25355 return finish_requires_expr (parms, reqs);
25358 /* Parse a parameterized requirement.
25360 requirement-parameter-list:
25361 '(' parameter-declaration-clause ')' */
25362 static tree
25363 cp_parser_requirement_parameter_list (cp_parser *parser)
25365 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25366 return error_mark_node;
25368 tree parms = cp_parser_parameter_declaration_clause (parser);
25370 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25371 return error_mark_node;
25373 return parms;
25376 /* Parse the body of a requirement.
25378 requirement-body:
25379 '{' requirement-list '}' */
25380 static tree
25381 cp_parser_requirement_body (cp_parser *parser)
25383 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25384 return error_mark_node;
25386 tree reqs = cp_parser_requirement_list (parser);
25388 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25389 return error_mark_node;
25391 return reqs;
25394 /* Parse a list of requirements.
25396 requirement-list:
25397 requirement
25398 requirement-list ';' requirement[opt] */
25399 static tree
25400 cp_parser_requirement_list (cp_parser *parser)
25402 tree result = NULL_TREE;
25403 while (true)
25405 tree req = cp_parser_requirement (parser);
25406 if (req == error_mark_node)
25407 return error_mark_node;
25409 result = tree_cons (NULL_TREE, req, result);
25411 /* If we see a semi-colon, consume it. */
25412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25413 cp_lexer_consume_token (parser->lexer);
25415 /* Stop processing at the end of the list. */
25416 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25417 break;
25420 /* Reverse the order of requirements so they are analyzed in
25421 declaration order. */
25422 return nreverse (result);
25425 /* Parse a syntactic requirement or type requirement.
25427 requirement:
25428 simple-requirement
25429 compound-requirement
25430 type-requirement
25431 nested-requirement */
25432 static tree
25433 cp_parser_requirement (cp_parser *parser)
25435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25436 return cp_parser_compound_requirement (parser);
25437 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25438 return cp_parser_type_requirement (parser);
25439 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25440 return cp_parser_nested_requirement (parser);
25441 else
25442 return cp_parser_simple_requirement (parser);
25445 /* Parse a simple requirement.
25447 simple-requirement:
25448 expression ';' */
25449 static tree
25450 cp_parser_simple_requirement (cp_parser *parser)
25452 tree expr = cp_parser_expression (parser, NULL, false, false);
25453 if (!expr || expr == error_mark_node)
25454 return error_mark_node;
25456 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25457 return error_mark_node;
25459 return finish_simple_requirement (expr);
25462 /* Parse a type requirement
25464 type-requirement
25465 nested-name-specifier [opt] required-type-name ';'
25467 required-type-name:
25468 type-name
25469 'template' [opt] simple-template-id */
25470 static tree
25471 cp_parser_type_requirement (cp_parser *parser)
25473 cp_lexer_consume_token (parser->lexer);
25475 // Save the scope before parsing name specifiers.
25476 tree saved_scope = parser->scope;
25477 tree saved_object_scope = parser->object_scope;
25478 tree saved_qualifying_scope = parser->qualifying_scope;
25479 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25480 cp_parser_nested_name_specifier_opt (parser,
25481 /*typename_keyword_p=*/true,
25482 /*check_dependency_p=*/false,
25483 /*type_p=*/true,
25484 /*is_declaration=*/false);
25486 tree type;
25487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25489 cp_lexer_consume_token (parser->lexer);
25490 type = cp_parser_template_id (parser,
25491 /*template_keyword_p=*/true,
25492 /*check_dependency=*/false,
25493 /*tag_type=*/none_type,
25494 /*is_declaration=*/false);
25495 type = make_typename_type (parser->scope, type, typename_type,
25496 /*complain=*/tf_error);
25498 else
25499 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25501 if (TREE_CODE (type) == TYPE_DECL)
25502 type = TREE_TYPE (type);
25504 parser->scope = saved_scope;
25505 parser->object_scope = saved_object_scope;
25506 parser->qualifying_scope = saved_qualifying_scope;
25508 if (type == error_mark_node)
25509 cp_parser_skip_to_end_of_statement (parser);
25511 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25512 return error_mark_node;
25513 if (type == error_mark_node)
25514 return error_mark_node;
25516 return finish_type_requirement (type);
25519 /* Parse a compound requirement
25521 compound-requirement:
25522 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25523 static tree
25524 cp_parser_compound_requirement (cp_parser *parser)
25526 /* Parse an expression enclosed in '{ }'s. */
25527 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25528 return error_mark_node;
25530 tree expr = cp_parser_expression (parser, NULL, false, false);
25531 if (!expr || expr == error_mark_node)
25532 return error_mark_node;
25534 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25535 return error_mark_node;
25537 /* Parse the optional noexcept. */
25538 bool noexcept_p = false;
25539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25541 cp_lexer_consume_token (parser->lexer);
25542 noexcept_p = true;
25545 /* Parse the optional trailing return type. */
25546 tree type = NULL_TREE;
25547 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25549 cp_lexer_consume_token (parser->lexer);
25550 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25551 parser->in_result_type_constraint_p = true;
25552 type = cp_parser_trailing_type_id (parser);
25553 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25554 if (type == error_mark_node)
25555 return error_mark_node;
25558 return finish_compound_requirement (expr, type, noexcept_p);
25561 /* Parse a nested requirement. This is the same as a requires clause.
25563 nested-requirement:
25564 requires-clause */
25565 static tree
25566 cp_parser_nested_requirement (cp_parser *parser)
25568 cp_lexer_consume_token (parser->lexer);
25569 tree req = cp_parser_requires_clause (parser);
25570 if (req == error_mark_node)
25571 return error_mark_node;
25572 return finish_nested_requirement (req);
25575 /* Support Functions */
25577 /* Return the appropriate prefer_type argument for lookup_name_real based on
25578 tag_type and template_mem_access. */
25580 static inline int
25581 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25583 /* DR 141: When looking in the current enclosing context for a template-name
25584 after -> or ., only consider class templates. */
25585 if (template_mem_access)
25586 return 2;
25587 switch (tag_type)
25589 case none_type: return 0; // No preference.
25590 case scope_type: return 1; // Type or namespace.
25591 default: return 2; // Type only.
25595 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25596 NAME should have one of the representations used for an
25597 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25598 is returned. If PARSER->SCOPE is a dependent type, then a
25599 SCOPE_REF is returned.
25601 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25602 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25603 was formed. Abstractly, such entities should not be passed to this
25604 function, because they do not need to be looked up, but it is
25605 simpler to check for this special case here, rather than at the
25606 call-sites.
25608 In cases not explicitly covered above, this function returns a
25609 DECL, OVERLOAD, or baselink representing the result of the lookup.
25610 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25611 is returned.
25613 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25614 (e.g., "struct") that was used. In that case bindings that do not
25615 refer to types are ignored.
25617 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25618 ignored.
25620 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25621 are ignored.
25623 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25624 types.
25626 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25627 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25628 NULL_TREE otherwise. */
25630 static cp_expr
25631 cp_parser_lookup_name (cp_parser *parser, tree name,
25632 enum tag_types tag_type,
25633 bool is_template,
25634 bool is_namespace,
25635 bool check_dependency,
25636 tree *ambiguous_decls,
25637 location_t name_location)
25639 tree decl;
25640 tree object_type = parser->context->object_type;
25642 /* Assume that the lookup will be unambiguous. */
25643 if (ambiguous_decls)
25644 *ambiguous_decls = NULL_TREE;
25646 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25647 no longer valid. Note that if we are parsing tentatively, and
25648 the parse fails, OBJECT_TYPE will be automatically restored. */
25649 parser->context->object_type = NULL_TREE;
25651 if (name == error_mark_node)
25652 return error_mark_node;
25654 /* A template-id has already been resolved; there is no lookup to
25655 do. */
25656 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25657 return name;
25658 if (BASELINK_P (name))
25660 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25661 == TEMPLATE_ID_EXPR);
25662 return name;
25665 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25666 it should already have been checked to make sure that the name
25667 used matches the type being destroyed. */
25668 if (TREE_CODE (name) == BIT_NOT_EXPR)
25670 tree type;
25672 /* Figure out to which type this destructor applies. */
25673 if (parser->scope)
25674 type = parser->scope;
25675 else if (object_type)
25676 type = object_type;
25677 else
25678 type = current_class_type;
25679 /* If that's not a class type, there is no destructor. */
25680 if (!type || !CLASS_TYPE_P (type))
25681 return error_mark_node;
25682 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25683 lazily_declare_fn (sfk_destructor, type);
25684 if (!CLASSTYPE_DESTRUCTORS (type))
25685 return error_mark_node;
25686 /* If it was a class type, return the destructor. */
25687 return CLASSTYPE_DESTRUCTORS (type);
25690 /* By this point, the NAME should be an ordinary identifier. If
25691 the id-expression was a qualified name, the qualifying scope is
25692 stored in PARSER->SCOPE at this point. */
25693 gcc_assert (identifier_p (name));
25695 /* Perform the lookup. */
25696 if (parser->scope)
25698 bool dependent_p;
25700 if (parser->scope == error_mark_node)
25701 return error_mark_node;
25703 /* If the SCOPE is dependent, the lookup must be deferred until
25704 the template is instantiated -- unless we are explicitly
25705 looking up names in uninstantiated templates. Even then, we
25706 cannot look up the name if the scope is not a class type; it
25707 might, for example, be a template type parameter. */
25708 dependent_p = (TYPE_P (parser->scope)
25709 && dependent_scope_p (parser->scope));
25710 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25711 && dependent_p)
25712 /* Defer lookup. */
25713 decl = error_mark_node;
25714 else
25716 tree pushed_scope = NULL_TREE;
25718 /* If PARSER->SCOPE is a dependent type, then it must be a
25719 class type, and we must not be checking dependencies;
25720 otherwise, we would have processed this lookup above. So
25721 that PARSER->SCOPE is not considered a dependent base by
25722 lookup_member, we must enter the scope here. */
25723 if (dependent_p)
25724 pushed_scope = push_scope (parser->scope);
25726 /* If the PARSER->SCOPE is a template specialization, it
25727 may be instantiated during name lookup. In that case,
25728 errors may be issued. Even if we rollback the current
25729 tentative parse, those errors are valid. */
25730 decl = lookup_qualified_name (parser->scope, name,
25731 prefer_type_arg (tag_type),
25732 /*complain=*/true);
25734 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25735 lookup result and the nested-name-specifier nominates a class C:
25736 * if the name specified after the nested-name-specifier, when
25737 looked up in C, is the injected-class-name of C (Clause 9), or
25738 * if the name specified after the nested-name-specifier is the
25739 same as the identifier or the simple-template-id's template-
25740 name in the last component of the nested-name-specifier,
25741 the name is instead considered to name the constructor of
25742 class C. [ Note: for example, the constructor is not an
25743 acceptable lookup result in an elaborated-type-specifier so
25744 the constructor would not be used in place of the
25745 injected-class-name. --end note ] Such a constructor name
25746 shall be used only in the declarator-id of a declaration that
25747 names a constructor or in a using-declaration. */
25748 if (tag_type == none_type
25749 && DECL_SELF_REFERENCE_P (decl)
25750 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25751 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25752 prefer_type_arg (tag_type),
25753 /*complain=*/true);
25755 /* If we have a single function from a using decl, pull it out. */
25756 if (TREE_CODE (decl) == OVERLOAD
25757 && !really_overloaded_fn (decl))
25758 decl = OVL_FUNCTION (decl);
25760 if (pushed_scope)
25761 pop_scope (pushed_scope);
25764 /* If the scope is a dependent type and either we deferred lookup or
25765 we did lookup but didn't find the name, rememeber the name. */
25766 if (decl == error_mark_node && TYPE_P (parser->scope)
25767 && dependent_type_p (parser->scope))
25769 if (tag_type)
25771 tree type;
25773 /* The resolution to Core Issue 180 says that `struct
25774 A::B' should be considered a type-name, even if `A'
25775 is dependent. */
25776 type = make_typename_type (parser->scope, name, tag_type,
25777 /*complain=*/tf_error);
25778 if (type != error_mark_node)
25779 decl = TYPE_NAME (type);
25781 else if (is_template
25782 && (cp_parser_next_token_ends_template_argument_p (parser)
25783 || cp_lexer_next_token_is (parser->lexer,
25784 CPP_CLOSE_PAREN)))
25785 decl = make_unbound_class_template (parser->scope,
25786 name, NULL_TREE,
25787 /*complain=*/tf_error);
25788 else
25789 decl = build_qualified_name (/*type=*/NULL_TREE,
25790 parser->scope, name,
25791 is_template);
25793 parser->qualifying_scope = parser->scope;
25794 parser->object_scope = NULL_TREE;
25796 else if (object_type)
25798 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25799 OBJECT_TYPE is not a class. */
25800 if (CLASS_TYPE_P (object_type))
25801 /* If the OBJECT_TYPE is a template specialization, it may
25802 be instantiated during name lookup. In that case, errors
25803 may be issued. Even if we rollback the current tentative
25804 parse, those errors are valid. */
25805 decl = lookup_member (object_type,
25806 name,
25807 /*protect=*/0,
25808 prefer_type_arg (tag_type),
25809 tf_warning_or_error);
25810 else
25811 decl = NULL_TREE;
25813 if (!decl)
25814 /* Look it up in the enclosing context. DR 141: When looking for a
25815 template-name after -> or ., only consider class templates. */
25816 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25817 /*nonclass=*/0,
25818 /*block_p=*/true, is_namespace, 0);
25819 if (object_type == unknown_type_node)
25820 /* The object is type-dependent, so we can't look anything up; we used
25821 this to get the DR 141 behavior. */
25822 object_type = NULL_TREE;
25823 parser->object_scope = object_type;
25824 parser->qualifying_scope = NULL_TREE;
25826 else
25828 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25829 /*nonclass=*/0,
25830 /*block_p=*/true, is_namespace, 0);
25831 parser->qualifying_scope = NULL_TREE;
25832 parser->object_scope = NULL_TREE;
25835 /* If the lookup failed, let our caller know. */
25836 if (!decl || decl == error_mark_node)
25837 return error_mark_node;
25839 /* Pull out the template from an injected-class-name (or multiple). */
25840 if (is_template)
25841 decl = maybe_get_template_decl_from_type_decl (decl);
25843 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25844 if (TREE_CODE (decl) == TREE_LIST)
25846 if (ambiguous_decls)
25847 *ambiguous_decls = decl;
25848 /* The error message we have to print is too complicated for
25849 cp_parser_error, so we incorporate its actions directly. */
25850 if (!cp_parser_simulate_error (parser))
25852 error_at (name_location, "reference to %qD is ambiguous",
25853 name);
25854 print_candidates (decl);
25856 return error_mark_node;
25859 gcc_assert (DECL_P (decl)
25860 || TREE_CODE (decl) == OVERLOAD
25861 || TREE_CODE (decl) == SCOPE_REF
25862 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25863 || BASELINK_P (decl));
25865 /* If we have resolved the name of a member declaration, check to
25866 see if the declaration is accessible. When the name resolves to
25867 set of overloaded functions, accessibility is checked when
25868 overload resolution is done.
25870 During an explicit instantiation, access is not checked at all,
25871 as per [temp.explicit]. */
25872 if (DECL_P (decl))
25873 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25875 maybe_record_typedef_use (decl);
25877 return cp_expr (decl, name_location);
25880 /* Like cp_parser_lookup_name, but for use in the typical case where
25881 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25882 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25884 static tree
25885 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25887 return cp_parser_lookup_name (parser, name,
25888 none_type,
25889 /*is_template=*/false,
25890 /*is_namespace=*/false,
25891 /*check_dependency=*/true,
25892 /*ambiguous_decls=*/NULL,
25893 location);
25896 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25897 the current context, return the TYPE_DECL. If TAG_NAME_P is
25898 true, the DECL indicates the class being defined in a class-head,
25899 or declared in an elaborated-type-specifier.
25901 Otherwise, return DECL. */
25903 static tree
25904 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25906 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25907 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25909 struct A {
25910 template <typename T> struct B;
25913 template <typename T> struct A::B {};
25915 Similarly, in an elaborated-type-specifier:
25917 namespace N { struct X{}; }
25919 struct A {
25920 template <typename T> friend struct N::X;
25923 However, if the DECL refers to a class type, and we are in
25924 the scope of the class, then the name lookup automatically
25925 finds the TYPE_DECL created by build_self_reference rather
25926 than a TEMPLATE_DECL. For example, in:
25928 template <class T> struct S {
25929 S s;
25932 there is no need to handle such case. */
25934 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25935 return DECL_TEMPLATE_RESULT (decl);
25937 return decl;
25940 /* If too many, or too few, template-parameter lists apply to the
25941 declarator, issue an error message. Returns TRUE if all went well,
25942 and FALSE otherwise. */
25944 static bool
25945 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25946 cp_declarator *declarator,
25947 location_t declarator_location)
25949 switch (declarator->kind)
25951 case cdk_id:
25953 unsigned num_templates = 0;
25954 tree scope = declarator->u.id.qualifying_scope;
25956 if (scope)
25957 num_templates = num_template_headers_for_class (scope);
25958 else if (TREE_CODE (declarator->u.id.unqualified_name)
25959 == TEMPLATE_ID_EXPR)
25960 /* If the DECLARATOR has the form `X<y>' then it uses one
25961 additional level of template parameters. */
25962 ++num_templates;
25964 return cp_parser_check_template_parameters
25965 (parser, num_templates, declarator_location, declarator);
25968 case cdk_function:
25969 case cdk_array:
25970 case cdk_pointer:
25971 case cdk_reference:
25972 case cdk_ptrmem:
25973 return (cp_parser_check_declarator_template_parameters
25974 (parser, declarator->declarator, declarator_location));
25976 case cdk_decomp:
25977 case cdk_error:
25978 return true;
25980 default:
25981 gcc_unreachable ();
25983 return false;
25986 /* NUM_TEMPLATES were used in the current declaration. If that is
25987 invalid, return FALSE and issue an error messages. Otherwise,
25988 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25989 declarator and we can print more accurate diagnostics. */
25991 static bool
25992 cp_parser_check_template_parameters (cp_parser* parser,
25993 unsigned num_templates,
25994 location_t location,
25995 cp_declarator *declarator)
25997 /* If there are the same number of template classes and parameter
25998 lists, that's OK. */
25999 if (parser->num_template_parameter_lists == num_templates)
26000 return true;
26001 /* If there are more, but only one more, then we are referring to a
26002 member template. That's OK too. */
26003 if (parser->num_template_parameter_lists == num_templates + 1)
26004 return true;
26005 /* If there are more template classes than parameter lists, we have
26006 something like:
26008 template <class T> void S<T>::R<T>::f (); */
26009 if (parser->num_template_parameter_lists < num_templates)
26011 if (declarator && !current_function_decl)
26012 error_at (location, "specializing member %<%T::%E%> "
26013 "requires %<template<>%> syntax",
26014 declarator->u.id.qualifying_scope,
26015 declarator->u.id.unqualified_name);
26016 else if (declarator)
26017 error_at (location, "invalid declaration of %<%T::%E%>",
26018 declarator->u.id.qualifying_scope,
26019 declarator->u.id.unqualified_name);
26020 else
26021 error_at (location, "too few template-parameter-lists");
26022 return false;
26024 /* Otherwise, there are too many template parameter lists. We have
26025 something like:
26027 template <class T> template <class U> void S::f(); */
26028 error_at (location, "too many template-parameter-lists");
26029 return false;
26032 /* Parse an optional `::' token indicating that the following name is
26033 from the global namespace. If so, PARSER->SCOPE is set to the
26034 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26035 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26036 Returns the new value of PARSER->SCOPE, if the `::' token is
26037 present, and NULL_TREE otherwise. */
26039 static tree
26040 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26042 cp_token *token;
26044 /* Peek at the next token. */
26045 token = cp_lexer_peek_token (parser->lexer);
26046 /* If we're looking at a `::' token then we're starting from the
26047 global namespace, not our current location. */
26048 if (token->type == CPP_SCOPE)
26050 /* Consume the `::' token. */
26051 cp_lexer_consume_token (parser->lexer);
26052 /* Set the SCOPE so that we know where to start the lookup. */
26053 parser->scope = global_namespace;
26054 parser->qualifying_scope = global_namespace;
26055 parser->object_scope = NULL_TREE;
26057 return parser->scope;
26059 else if (!current_scope_valid_p)
26061 parser->scope = NULL_TREE;
26062 parser->qualifying_scope = NULL_TREE;
26063 parser->object_scope = NULL_TREE;
26066 return NULL_TREE;
26069 /* Returns TRUE if the upcoming token sequence is the start of a
26070 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26071 declarator is preceded by the `friend' specifier. */
26073 static bool
26074 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26076 bool constructor_p;
26077 bool outside_class_specifier_p;
26078 tree nested_name_specifier;
26079 cp_token *next_token;
26081 /* The common case is that this is not a constructor declarator, so
26082 try to avoid doing lots of work if at all possible. It's not
26083 valid declare a constructor at function scope. */
26084 if (parser->in_function_body)
26085 return false;
26086 /* And only certain tokens can begin a constructor declarator. */
26087 next_token = cp_lexer_peek_token (parser->lexer);
26088 if (next_token->type != CPP_NAME
26089 && next_token->type != CPP_SCOPE
26090 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26091 && next_token->type != CPP_TEMPLATE_ID)
26092 return false;
26094 /* Parse tentatively; we are going to roll back all of the tokens
26095 consumed here. */
26096 cp_parser_parse_tentatively (parser);
26097 /* Assume that we are looking at a constructor declarator. */
26098 constructor_p = true;
26100 /* Look for the optional `::' operator. */
26101 cp_parser_global_scope_opt (parser,
26102 /*current_scope_valid_p=*/false);
26103 /* Look for the nested-name-specifier. */
26104 nested_name_specifier
26105 = (cp_parser_nested_name_specifier_opt (parser,
26106 /*typename_keyword_p=*/false,
26107 /*check_dependency_p=*/false,
26108 /*type_p=*/false,
26109 /*is_declaration=*/false));
26111 outside_class_specifier_p = (!at_class_scope_p ()
26112 || !TYPE_BEING_DEFINED (current_class_type)
26113 || friend_p);
26115 /* Outside of a class-specifier, there must be a
26116 nested-name-specifier. Except in C++17 mode, where we
26117 might be declaring a guiding declaration. */
26118 if (!nested_name_specifier && outside_class_specifier_p
26119 && cxx_dialect < cxx1z)
26120 constructor_p = false;
26121 else if (nested_name_specifier == error_mark_node)
26122 constructor_p = false;
26124 /* If we have a class scope, this is easy; DR 147 says that S::S always
26125 names the constructor, and no other qualified name could. */
26126 if (constructor_p && nested_name_specifier
26127 && CLASS_TYPE_P (nested_name_specifier))
26129 tree id = cp_parser_unqualified_id (parser,
26130 /*template_keyword_p=*/false,
26131 /*check_dependency_p=*/false,
26132 /*declarator_p=*/true,
26133 /*optional_p=*/false);
26134 if (is_overloaded_fn (id))
26135 id = DECL_NAME (get_first_fn (id));
26136 if (!constructor_name_p (id, nested_name_specifier))
26137 constructor_p = false;
26139 /* If we still think that this might be a constructor-declarator,
26140 look for a class-name. */
26141 else if (constructor_p)
26143 /* If we have:
26145 template <typename T> struct S {
26146 S();
26149 we must recognize that the nested `S' names a class. */
26150 if (cxx_dialect >= cxx1z)
26151 cp_parser_parse_tentatively (parser);
26153 tree type_decl;
26154 type_decl = cp_parser_class_name (parser,
26155 /*typename_keyword_p=*/false,
26156 /*template_keyword_p=*/false,
26157 none_type,
26158 /*check_dependency_p=*/false,
26159 /*class_head_p=*/false,
26160 /*is_declaration=*/false);
26162 if (cxx_dialect >= cxx1z
26163 && !cp_parser_parse_definitely (parser))
26165 type_decl = NULL_TREE;
26166 tree tmpl = cp_parser_template_name (parser,
26167 /*template_keyword*/false,
26168 /*check_dependency_p*/false,
26169 /*is_declaration*/false,
26170 none_type,
26171 /*is_identifier*/NULL);
26172 if (DECL_CLASS_TEMPLATE_P (tmpl)
26173 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26174 /* It's a deduction guide, return true. */;
26175 else
26176 cp_parser_simulate_error (parser);
26179 /* If there was no class-name, then this is not a constructor.
26180 Otherwise, if we are in a class-specifier and we aren't
26181 handling a friend declaration, check that its type matches
26182 current_class_type (c++/38313). Note: error_mark_node
26183 is left alone for error recovery purposes. */
26184 constructor_p = (!cp_parser_error_occurred (parser)
26185 && (outside_class_specifier_p
26186 || type_decl == NULL_TREE
26187 || type_decl == error_mark_node
26188 || same_type_p (current_class_type,
26189 TREE_TYPE (type_decl))));
26191 /* If we're still considering a constructor, we have to see a `(',
26192 to begin the parameter-declaration-clause, followed by either a
26193 `)', an `...', or a decl-specifier. We need to check for a
26194 type-specifier to avoid being fooled into thinking that:
26196 S (f) (int);
26198 is a constructor. (It is actually a function named `f' that
26199 takes one parameter (of type `int') and returns a value of type
26200 `S'. */
26201 if (constructor_p
26202 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26203 constructor_p = false;
26205 if (constructor_p
26206 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26207 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26208 /* A parameter declaration begins with a decl-specifier,
26209 which is either the "attribute" keyword, a storage class
26210 specifier, or (usually) a type-specifier. */
26211 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26213 tree type;
26214 tree pushed_scope = NULL_TREE;
26215 unsigned saved_num_template_parameter_lists;
26217 /* Names appearing in the type-specifier should be looked up
26218 in the scope of the class. */
26219 if (current_class_type)
26220 type = NULL_TREE;
26221 else if (type_decl)
26223 type = TREE_TYPE (type_decl);
26224 if (TREE_CODE (type) == TYPENAME_TYPE)
26226 type = resolve_typename_type (type,
26227 /*only_current_p=*/false);
26228 if (TREE_CODE (type) == TYPENAME_TYPE)
26230 cp_parser_abort_tentative_parse (parser);
26231 return false;
26234 pushed_scope = push_scope (type);
26237 /* Inside the constructor parameter list, surrounding
26238 template-parameter-lists do not apply. */
26239 saved_num_template_parameter_lists
26240 = parser->num_template_parameter_lists;
26241 parser->num_template_parameter_lists = 0;
26243 /* Look for the type-specifier. */
26244 cp_parser_type_specifier (parser,
26245 CP_PARSER_FLAGS_NONE,
26246 /*decl_specs=*/NULL,
26247 /*is_declarator=*/true,
26248 /*declares_class_or_enum=*/NULL,
26249 /*is_cv_qualifier=*/NULL);
26251 parser->num_template_parameter_lists
26252 = saved_num_template_parameter_lists;
26254 /* Leave the scope of the class. */
26255 if (pushed_scope)
26256 pop_scope (pushed_scope);
26258 constructor_p = !cp_parser_error_occurred (parser);
26262 /* We did not really want to consume any tokens. */
26263 cp_parser_abort_tentative_parse (parser);
26265 return constructor_p;
26268 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26269 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26270 they must be performed once we are in the scope of the function.
26272 Returns the function defined. */
26274 static tree
26275 cp_parser_function_definition_from_specifiers_and_declarator
26276 (cp_parser* parser,
26277 cp_decl_specifier_seq *decl_specifiers,
26278 tree attributes,
26279 const cp_declarator *declarator)
26281 tree fn;
26282 bool success_p;
26284 /* Begin the function-definition. */
26285 success_p = start_function (decl_specifiers, declarator, attributes);
26287 /* The things we're about to see are not directly qualified by any
26288 template headers we've seen thus far. */
26289 reset_specialization ();
26291 /* If there were names looked up in the decl-specifier-seq that we
26292 did not check, check them now. We must wait until we are in the
26293 scope of the function to perform the checks, since the function
26294 might be a friend. */
26295 perform_deferred_access_checks (tf_warning_or_error);
26297 if (success_p)
26299 cp_finalize_omp_declare_simd (parser, current_function_decl);
26300 parser->omp_declare_simd = NULL;
26301 cp_finalize_oacc_routine (parser, current_function_decl, true);
26302 parser->oacc_routine = NULL;
26305 if (!success_p)
26307 /* Skip the entire function. */
26308 cp_parser_skip_to_end_of_block_or_statement (parser);
26309 fn = error_mark_node;
26311 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26313 /* Seen already, skip it. An error message has already been output. */
26314 cp_parser_skip_to_end_of_block_or_statement (parser);
26315 fn = current_function_decl;
26316 current_function_decl = NULL_TREE;
26317 /* If this is a function from a class, pop the nested class. */
26318 if (current_class_name)
26319 pop_nested_class ();
26321 else
26323 timevar_id_t tv;
26324 if (DECL_DECLARED_INLINE_P (current_function_decl))
26325 tv = TV_PARSE_INLINE;
26326 else
26327 tv = TV_PARSE_FUNC;
26328 timevar_push (tv);
26329 fn = cp_parser_function_definition_after_declarator (parser,
26330 /*inline_p=*/false);
26331 timevar_pop (tv);
26334 return fn;
26337 /* Parse the part of a function-definition that follows the
26338 declarator. INLINE_P is TRUE iff this function is an inline
26339 function defined within a class-specifier.
26341 Returns the function defined. */
26343 static tree
26344 cp_parser_function_definition_after_declarator (cp_parser* parser,
26345 bool inline_p)
26347 tree fn;
26348 bool ctor_initializer_p = false;
26349 bool saved_in_unbraced_linkage_specification_p;
26350 bool saved_in_function_body;
26351 unsigned saved_num_template_parameter_lists;
26352 cp_token *token;
26353 bool fully_implicit_function_template_p
26354 = parser->fully_implicit_function_template_p;
26355 parser->fully_implicit_function_template_p = false;
26356 tree implicit_template_parms
26357 = parser->implicit_template_parms;
26358 parser->implicit_template_parms = 0;
26359 cp_binding_level* implicit_template_scope
26360 = parser->implicit_template_scope;
26361 parser->implicit_template_scope = 0;
26363 saved_in_function_body = parser->in_function_body;
26364 parser->in_function_body = true;
26365 /* If the next token is `return', then the code may be trying to
26366 make use of the "named return value" extension that G++ used to
26367 support. */
26368 token = cp_lexer_peek_token (parser->lexer);
26369 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26371 /* Consume the `return' keyword. */
26372 cp_lexer_consume_token (parser->lexer);
26373 /* Look for the identifier that indicates what value is to be
26374 returned. */
26375 cp_parser_identifier (parser);
26376 /* Issue an error message. */
26377 error_at (token->location,
26378 "named return values are no longer supported");
26379 /* Skip tokens until we reach the start of the function body. */
26380 while (true)
26382 cp_token *token = cp_lexer_peek_token (parser->lexer);
26383 if (token->type == CPP_OPEN_BRACE
26384 || token->type == CPP_EOF
26385 || token->type == CPP_PRAGMA_EOL)
26386 break;
26387 cp_lexer_consume_token (parser->lexer);
26390 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26391 anything declared inside `f'. */
26392 saved_in_unbraced_linkage_specification_p
26393 = parser->in_unbraced_linkage_specification_p;
26394 parser->in_unbraced_linkage_specification_p = false;
26395 /* Inside the function, surrounding template-parameter-lists do not
26396 apply. */
26397 saved_num_template_parameter_lists
26398 = parser->num_template_parameter_lists;
26399 parser->num_template_parameter_lists = 0;
26401 start_lambda_scope (current_function_decl);
26403 /* If the next token is `try', `__transaction_atomic', or
26404 `__transaction_relaxed`, then we are looking at either function-try-block
26405 or function-transaction-block. Note that all of these include the
26406 function-body. */
26407 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26408 ctor_initializer_p = cp_parser_function_transaction (parser,
26409 RID_TRANSACTION_ATOMIC);
26410 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26411 RID_TRANSACTION_RELAXED))
26412 ctor_initializer_p = cp_parser_function_transaction (parser,
26413 RID_TRANSACTION_RELAXED);
26414 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26415 ctor_initializer_p = cp_parser_function_try_block (parser);
26416 else
26417 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26418 (parser, /*in_function_try_block=*/false);
26420 finish_lambda_scope ();
26422 /* Finish the function. */
26423 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26424 (inline_p ? 2 : 0));
26425 /* Generate code for it, if necessary. */
26426 expand_or_defer_fn (fn);
26427 /* Restore the saved values. */
26428 parser->in_unbraced_linkage_specification_p
26429 = saved_in_unbraced_linkage_specification_p;
26430 parser->num_template_parameter_lists
26431 = saved_num_template_parameter_lists;
26432 parser->in_function_body = saved_in_function_body;
26434 parser->fully_implicit_function_template_p
26435 = fully_implicit_function_template_p;
26436 parser->implicit_template_parms
26437 = implicit_template_parms;
26438 parser->implicit_template_scope
26439 = implicit_template_scope;
26441 if (parser->fully_implicit_function_template_p)
26442 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26444 return fn;
26447 /* Parse a template-declaration body (following argument list). */
26449 static void
26450 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26451 tree parameter_list,
26452 bool member_p)
26454 tree decl = NULL_TREE;
26455 bool friend_p = false;
26457 /* We just processed one more parameter list. */
26458 ++parser->num_template_parameter_lists;
26460 /* Get the deferred access checks from the parameter list. These
26461 will be checked once we know what is being declared, as for a
26462 member template the checks must be performed in the scope of the
26463 class containing the member. */
26464 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26466 /* Tentatively parse for a new template parameter list, which can either be
26467 the template keyword or a template introduction. */
26468 if (cp_parser_template_declaration_after_export (parser, member_p))
26469 /* OK */;
26470 else if (cxx_dialect >= cxx11
26471 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26472 decl = cp_parser_alias_declaration (parser);
26473 else
26475 /* There are no access checks when parsing a template, as we do not
26476 know if a specialization will be a friend. */
26477 push_deferring_access_checks (dk_no_check);
26478 cp_token *token = cp_lexer_peek_token (parser->lexer);
26479 decl = cp_parser_single_declaration (parser,
26480 checks,
26481 member_p,
26482 /*explicit_specialization_p=*/false,
26483 &friend_p);
26484 pop_deferring_access_checks ();
26486 /* If this is a member template declaration, let the front
26487 end know. */
26488 if (member_p && !friend_p && decl)
26490 if (TREE_CODE (decl) == TYPE_DECL)
26491 cp_parser_check_access_in_redeclaration (decl, token->location);
26493 decl = finish_member_template_decl (decl);
26495 else if (friend_p && decl
26496 && DECL_DECLARES_TYPE_P (decl))
26497 make_friend_class (current_class_type, TREE_TYPE (decl),
26498 /*complain=*/true);
26500 /* We are done with the current parameter list. */
26501 --parser->num_template_parameter_lists;
26503 pop_deferring_access_checks ();
26505 /* Finish up. */
26506 finish_template_decl (parameter_list);
26508 /* Check the template arguments for a literal operator template. */
26509 if (decl
26510 && DECL_DECLARES_FUNCTION_P (decl)
26511 && UDLIT_OPER_P (DECL_NAME (decl)))
26513 bool ok = true;
26514 if (parameter_list == NULL_TREE)
26515 ok = false;
26516 else
26518 int num_parms = TREE_VEC_LENGTH (parameter_list);
26519 if (num_parms == 1)
26521 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26522 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26523 if (TREE_TYPE (parm) != char_type_node
26524 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26525 ok = false;
26527 else if (num_parms == 2 && cxx_dialect >= cxx14)
26529 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26530 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26531 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26532 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26533 if (parm == error_mark_node
26534 || TREE_TYPE (parm) != TREE_TYPE (type)
26535 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26536 ok = false;
26538 else
26539 ok = false;
26541 if (!ok)
26543 if (cxx_dialect >= cxx14)
26544 error ("literal operator template %qD has invalid parameter list."
26545 " Expected non-type template argument pack <char...>"
26546 " or <typename CharT, CharT...>",
26547 decl);
26548 else
26549 error ("literal operator template %qD has invalid parameter list."
26550 " Expected non-type template argument pack <char...>",
26551 decl);
26555 /* Register member declarations. */
26556 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26557 finish_member_declaration (decl);
26558 /* If DECL is a function template, we must return to parse it later.
26559 (Even though there is no definition, there might be default
26560 arguments that need handling.) */
26561 if (member_p && decl
26562 && DECL_DECLARES_FUNCTION_P (decl))
26563 vec_safe_push (unparsed_funs_with_definitions, decl);
26566 /* Parse a template introduction header for a template-declaration. Returns
26567 false if tentative parse fails. */
26569 static bool
26570 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26572 cp_parser_parse_tentatively (parser);
26574 tree saved_scope = parser->scope;
26575 tree saved_object_scope = parser->object_scope;
26576 tree saved_qualifying_scope = parser->qualifying_scope;
26578 /* Look for the optional `::' operator. */
26579 cp_parser_global_scope_opt (parser,
26580 /*current_scope_valid_p=*/false);
26581 /* Look for the nested-name-specifier. */
26582 cp_parser_nested_name_specifier_opt (parser,
26583 /*typename_keyword_p=*/false,
26584 /*check_dependency_p=*/true,
26585 /*type_p=*/false,
26586 /*is_declaration=*/false);
26588 cp_token *token = cp_lexer_peek_token (parser->lexer);
26589 tree concept_name = cp_parser_identifier (parser);
26591 /* Look up the concept for which we will be matching
26592 template parameters. */
26593 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26594 token->location);
26595 parser->scope = saved_scope;
26596 parser->object_scope = saved_object_scope;
26597 parser->qualifying_scope = saved_qualifying_scope;
26599 if (concept_name == error_mark_node)
26600 cp_parser_simulate_error (parser);
26602 /* Look for opening brace for introduction. */
26603 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26605 if (!cp_parser_parse_definitely (parser))
26606 return false;
26608 push_deferring_access_checks (dk_deferred);
26610 /* Build vector of placeholder parameters and grab
26611 matching identifiers. */
26612 tree introduction_list = cp_parser_introduction_list (parser);
26614 /* The introduction-list shall not be empty. */
26615 int nargs = TREE_VEC_LENGTH (introduction_list);
26616 if (nargs == 0)
26618 error ("empty introduction-list");
26619 return true;
26622 /* Look for closing brace for introduction. */
26623 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26624 return true;
26626 if (tmpl_decl == error_mark_node)
26628 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26629 token->location);
26630 return true;
26633 /* Build and associate the constraint. */
26634 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26635 if (parms && parms != error_mark_node)
26637 cp_parser_template_declaration_after_parameters (parser, parms,
26638 member_p);
26639 return true;
26642 error_at (token->location, "no matching concept for template-introduction");
26643 return true;
26646 /* Parse a normal template-declaration following the template keyword. */
26648 static void
26649 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26651 tree parameter_list;
26652 bool need_lang_pop;
26653 location_t location = input_location;
26655 /* Look for the `<' token. */
26656 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26657 return;
26658 if (at_class_scope_p () && current_function_decl)
26660 /* 14.5.2.2 [temp.mem]
26662 A local class shall not have member templates. */
26663 error_at (location,
26664 "invalid declaration of member template in local class");
26665 cp_parser_skip_to_end_of_block_or_statement (parser);
26666 return;
26668 /* [temp]
26670 A template ... shall not have C linkage. */
26671 if (current_lang_name == lang_name_c)
26673 error_at (location, "template with C linkage");
26674 /* Give it C++ linkage to avoid confusing other parts of the
26675 front end. */
26676 push_lang_context (lang_name_cplusplus);
26677 need_lang_pop = true;
26679 else
26680 need_lang_pop = false;
26682 /* We cannot perform access checks on the template parameter
26683 declarations until we know what is being declared, just as we
26684 cannot check the decl-specifier list. */
26685 push_deferring_access_checks (dk_deferred);
26687 /* If the next token is `>', then we have an invalid
26688 specialization. Rather than complain about an invalid template
26689 parameter, issue an error message here. */
26690 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26692 cp_parser_error (parser, "invalid explicit specialization");
26693 begin_specialization ();
26694 parameter_list = NULL_TREE;
26696 else
26698 /* Parse the template parameters. */
26699 parameter_list = cp_parser_template_parameter_list (parser);
26702 /* Look for the `>'. */
26703 cp_parser_skip_to_end_of_template_parameter_list (parser);
26705 /* Manage template requirements */
26706 if (flag_concepts)
26708 tree reqs = get_shorthand_constraints (current_template_parms);
26709 if (tree r = cp_parser_requires_clause_opt (parser))
26710 reqs = conjoin_constraints (reqs, normalize_expression (r));
26711 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26714 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26715 member_p);
26717 /* For the erroneous case of a template with C linkage, we pushed an
26718 implicit C++ linkage scope; exit that scope now. */
26719 if (need_lang_pop)
26720 pop_lang_context ();
26723 /* Parse a template-declaration, assuming that the `export' (and
26724 `extern') keywords, if present, has already been scanned. MEMBER_P
26725 is as for cp_parser_template_declaration. */
26727 static bool
26728 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26730 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26732 cp_lexer_consume_token (parser->lexer);
26733 cp_parser_explicit_template_declaration (parser, member_p);
26734 return true;
26736 else if (flag_concepts)
26737 return cp_parser_template_introduction (parser, member_p);
26739 return false;
26742 /* Perform the deferred access checks from a template-parameter-list.
26743 CHECKS is a TREE_LIST of access checks, as returned by
26744 get_deferred_access_checks. */
26746 static void
26747 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26749 ++processing_template_parmlist;
26750 perform_access_checks (checks, tf_warning_or_error);
26751 --processing_template_parmlist;
26754 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26755 `function-definition' sequence that follows a template header.
26756 If MEMBER_P is true, this declaration appears in a class scope.
26758 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26759 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26761 static tree
26762 cp_parser_single_declaration (cp_parser* parser,
26763 vec<deferred_access_check, va_gc> *checks,
26764 bool member_p,
26765 bool explicit_specialization_p,
26766 bool* friend_p)
26768 int declares_class_or_enum;
26769 tree decl = NULL_TREE;
26770 cp_decl_specifier_seq decl_specifiers;
26771 bool function_definition_p = false;
26772 cp_token *decl_spec_token_start;
26774 /* This function is only used when processing a template
26775 declaration. */
26776 gcc_assert (innermost_scope_kind () == sk_template_parms
26777 || innermost_scope_kind () == sk_template_spec);
26779 /* Defer access checks until we know what is being declared. */
26780 push_deferring_access_checks (dk_deferred);
26782 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26783 alternative. */
26784 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26785 cp_parser_decl_specifier_seq (parser,
26786 CP_PARSER_FLAGS_OPTIONAL,
26787 &decl_specifiers,
26788 &declares_class_or_enum);
26789 if (friend_p)
26790 *friend_p = cp_parser_friend_p (&decl_specifiers);
26792 /* There are no template typedefs. */
26793 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26795 error_at (decl_spec_token_start->location,
26796 "template declaration of %<typedef%>");
26797 decl = error_mark_node;
26800 /* Gather up the access checks that occurred the
26801 decl-specifier-seq. */
26802 stop_deferring_access_checks ();
26804 /* Check for the declaration of a template class. */
26805 if (declares_class_or_enum)
26807 if (cp_parser_declares_only_class_p (parser)
26808 || (declares_class_or_enum & 2))
26810 // If this is a declaration, but not a definition, associate
26811 // any constraints with the type declaration. Constraints
26812 // are associated with definitions in cp_parser_class_specifier.
26813 if (declares_class_or_enum == 1)
26814 associate_classtype_constraints (decl_specifiers.type);
26816 decl = shadow_tag (&decl_specifiers);
26818 /* In this case:
26820 struct C {
26821 friend template <typename T> struct A<T>::B;
26824 A<T>::B will be represented by a TYPENAME_TYPE, and
26825 therefore not recognized by shadow_tag. */
26826 if (friend_p && *friend_p
26827 && !decl
26828 && decl_specifiers.type
26829 && TYPE_P (decl_specifiers.type))
26830 decl = decl_specifiers.type;
26832 if (decl && decl != error_mark_node)
26833 decl = TYPE_NAME (decl);
26834 else
26835 decl = error_mark_node;
26837 /* Perform access checks for template parameters. */
26838 cp_parser_perform_template_parameter_access_checks (checks);
26840 /* Give a helpful diagnostic for
26841 template <class T> struct A { } a;
26842 if we aren't already recovering from an error. */
26843 if (!cp_parser_declares_only_class_p (parser)
26844 && !seen_error ())
26846 error_at (cp_lexer_peek_token (parser->lexer)->location,
26847 "a class template declaration must not declare "
26848 "anything else");
26849 cp_parser_skip_to_end_of_block_or_statement (parser);
26850 goto out;
26855 /* Complain about missing 'typename' or other invalid type names. */
26856 if (!decl_specifiers.any_type_specifiers_p
26857 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26859 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26860 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26861 the rest of this declaration. */
26862 decl = error_mark_node;
26863 goto out;
26866 /* If it's not a template class, try for a template function. If
26867 the next token is a `;', then this declaration does not declare
26868 anything. But, if there were errors in the decl-specifiers, then
26869 the error might well have come from an attempted class-specifier.
26870 In that case, there's no need to warn about a missing declarator. */
26871 if (!decl
26872 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26873 || decl_specifiers.type != error_mark_node))
26875 decl = cp_parser_init_declarator (parser,
26876 &decl_specifiers,
26877 checks,
26878 /*function_definition_allowed_p=*/true,
26879 member_p,
26880 declares_class_or_enum,
26881 &function_definition_p,
26882 NULL, NULL, NULL);
26884 /* 7.1.1-1 [dcl.stc]
26886 A storage-class-specifier shall not be specified in an explicit
26887 specialization... */
26888 if (decl
26889 && explicit_specialization_p
26890 && decl_specifiers.storage_class != sc_none)
26892 error_at (decl_spec_token_start->location,
26893 "explicit template specialization cannot have a storage class");
26894 decl = error_mark_node;
26897 if (decl && VAR_P (decl))
26898 check_template_variable (decl);
26901 /* Look for a trailing `;' after the declaration. */
26902 if (!function_definition_p
26903 && (decl == error_mark_node
26904 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26905 cp_parser_skip_to_end_of_block_or_statement (parser);
26907 out:
26908 pop_deferring_access_checks ();
26910 /* Clear any current qualification; whatever comes next is the start
26911 of something new. */
26912 parser->scope = NULL_TREE;
26913 parser->qualifying_scope = NULL_TREE;
26914 parser->object_scope = NULL_TREE;
26916 return decl;
26919 /* Parse a cast-expression that is not the operand of a unary "&". */
26921 static cp_expr
26922 cp_parser_simple_cast_expression (cp_parser *parser)
26924 return cp_parser_cast_expression (parser, /*address_p=*/false,
26925 /*cast_p=*/false, /*decltype*/false, NULL);
26928 /* Parse a functional cast to TYPE. Returns an expression
26929 representing the cast. */
26931 static cp_expr
26932 cp_parser_functional_cast (cp_parser* parser, tree type)
26934 vec<tree, va_gc> *vec;
26935 tree expression_list;
26936 cp_expr cast;
26937 bool nonconst_p;
26939 location_t start_loc = input_location;
26941 if (!type)
26942 type = error_mark_node;
26944 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26946 cp_lexer_set_source_position (parser->lexer);
26947 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26948 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26949 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26950 if (TREE_CODE (type) == TYPE_DECL)
26951 type = TREE_TYPE (type);
26953 cast = finish_compound_literal (type, expression_list,
26954 tf_warning_or_error, fcl_functional);
26955 /* Create a location of the form:
26956 type_name{i, f}
26957 ^~~~~~~~~~~~~~~
26958 with caret == start at the start of the type name,
26959 finishing at the closing brace. */
26960 location_t finish_loc
26961 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26962 location_t combined_loc = make_location (start_loc, start_loc,
26963 finish_loc);
26964 cast.set_location (combined_loc);
26965 return cast;
26969 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26970 /*cast_p=*/true,
26971 /*allow_expansion_p=*/true,
26972 /*non_constant_p=*/NULL);
26973 if (vec == NULL)
26974 expression_list = error_mark_node;
26975 else
26977 expression_list = build_tree_list_vec (vec);
26978 release_tree_vector (vec);
26981 cast = build_functional_cast (type, expression_list,
26982 tf_warning_or_error);
26983 /* [expr.const]/1: In an integral constant expression "only type
26984 conversions to integral or enumeration type can be used". */
26985 if (TREE_CODE (type) == TYPE_DECL)
26986 type = TREE_TYPE (type);
26987 if (cast != error_mark_node
26988 && !cast_valid_in_integral_constant_expression_p (type)
26989 && cp_parser_non_integral_constant_expression (parser,
26990 NIC_CONSTRUCTOR))
26991 return error_mark_node;
26993 /* Create a location of the form:
26994 float(i)
26995 ^~~~~~~~
26996 with caret == start at the start of the type name,
26997 finishing at the closing paren. */
26998 location_t finish_loc
26999 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27000 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27001 cast.set_location (combined_loc);
27002 return cast;
27005 /* Save the tokens that make up the body of a member function defined
27006 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27007 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27008 specifiers applied to the declaration. Returns the FUNCTION_DECL
27009 for the member function. */
27011 static tree
27012 cp_parser_save_member_function_body (cp_parser* parser,
27013 cp_decl_specifier_seq *decl_specifiers,
27014 cp_declarator *declarator,
27015 tree attributes)
27017 cp_token *first;
27018 cp_token *last;
27019 tree fn;
27020 bool function_try_block = false;
27022 /* Create the FUNCTION_DECL. */
27023 fn = grokmethod (decl_specifiers, declarator, attributes);
27024 cp_finalize_omp_declare_simd (parser, fn);
27025 cp_finalize_oacc_routine (parser, fn, true);
27026 /* If something went badly wrong, bail out now. */
27027 if (fn == error_mark_node)
27029 /* If there's a function-body, skip it. */
27030 if (cp_parser_token_starts_function_definition_p
27031 (cp_lexer_peek_token (parser->lexer)))
27032 cp_parser_skip_to_end_of_block_or_statement (parser);
27033 return error_mark_node;
27036 /* Remember it, if there default args to post process. */
27037 cp_parser_save_default_args (parser, fn);
27039 /* Save away the tokens that make up the body of the
27040 function. */
27041 first = parser->lexer->next_token;
27043 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27044 cp_lexer_consume_token (parser->lexer);
27045 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27046 RID_TRANSACTION_ATOMIC))
27048 cp_lexer_consume_token (parser->lexer);
27049 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27050 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27051 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27052 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27053 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27054 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27055 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27057 cp_lexer_consume_token (parser->lexer);
27058 cp_lexer_consume_token (parser->lexer);
27059 cp_lexer_consume_token (parser->lexer);
27060 cp_lexer_consume_token (parser->lexer);
27061 cp_lexer_consume_token (parser->lexer);
27063 else
27064 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27065 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27067 cp_lexer_consume_token (parser->lexer);
27068 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27069 break;
27073 /* Handle function try blocks. */
27074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27076 cp_lexer_consume_token (parser->lexer);
27077 function_try_block = true;
27079 /* We can have braced-init-list mem-initializers before the fn body. */
27080 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27082 cp_lexer_consume_token (parser->lexer);
27083 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27085 /* cache_group will stop after an un-nested { } pair, too. */
27086 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27087 break;
27089 /* variadic mem-inits have ... after the ')'. */
27090 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27091 cp_lexer_consume_token (parser->lexer);
27094 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27095 /* Handle function try blocks. */
27096 if (function_try_block)
27097 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27098 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27099 last = parser->lexer->next_token;
27101 /* Save away the inline definition; we will process it when the
27102 class is complete. */
27103 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27104 DECL_PENDING_INLINE_P (fn) = 1;
27106 /* We need to know that this was defined in the class, so that
27107 friend templates are handled correctly. */
27108 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27110 /* Add FN to the queue of functions to be parsed later. */
27111 vec_safe_push (unparsed_funs_with_definitions, fn);
27113 return fn;
27116 /* Save the tokens that make up the in-class initializer for a non-static
27117 data member. Returns a DEFAULT_ARG. */
27119 static tree
27120 cp_parser_save_nsdmi (cp_parser* parser)
27122 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27125 /* Parse a template-argument-list, as well as the trailing ">" (but
27126 not the opening "<"). See cp_parser_template_argument_list for the
27127 return value. */
27129 static tree
27130 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27132 tree arguments;
27133 tree saved_scope;
27134 tree saved_qualifying_scope;
27135 tree saved_object_scope;
27136 bool saved_greater_than_is_operator_p;
27137 int saved_unevaluated_operand;
27138 int saved_inhibit_evaluation_warnings;
27140 /* [temp.names]
27142 When parsing a template-id, the first non-nested `>' is taken as
27143 the end of the template-argument-list rather than a greater-than
27144 operator. */
27145 saved_greater_than_is_operator_p
27146 = parser->greater_than_is_operator_p;
27147 parser->greater_than_is_operator_p = false;
27148 /* Parsing the argument list may modify SCOPE, so we save it
27149 here. */
27150 saved_scope = parser->scope;
27151 saved_qualifying_scope = parser->qualifying_scope;
27152 saved_object_scope = parser->object_scope;
27153 /* We need to evaluate the template arguments, even though this
27154 template-id may be nested within a "sizeof". */
27155 saved_unevaluated_operand = cp_unevaluated_operand;
27156 cp_unevaluated_operand = 0;
27157 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27158 c_inhibit_evaluation_warnings = 0;
27159 /* Parse the template-argument-list itself. */
27160 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27161 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27162 arguments = NULL_TREE;
27163 else
27164 arguments = cp_parser_template_argument_list (parser);
27165 /* Look for the `>' that ends the template-argument-list. If we find
27166 a '>>' instead, it's probably just a typo. */
27167 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27169 if (cxx_dialect != cxx98)
27171 /* In C++0x, a `>>' in a template argument list or cast
27172 expression is considered to be two separate `>'
27173 tokens. So, change the current token to a `>', but don't
27174 consume it: it will be consumed later when the outer
27175 template argument list (or cast expression) is parsed.
27176 Note that this replacement of `>' for `>>' is necessary
27177 even if we are parsing tentatively: in the tentative
27178 case, after calling
27179 cp_parser_enclosed_template_argument_list we will always
27180 throw away all of the template arguments and the first
27181 closing `>', either because the template argument list
27182 was erroneous or because we are replacing those tokens
27183 with a CPP_TEMPLATE_ID token. The second `>' (which will
27184 not have been thrown away) is needed either to close an
27185 outer template argument list or to complete a new-style
27186 cast. */
27187 cp_token *token = cp_lexer_peek_token (parser->lexer);
27188 token->type = CPP_GREATER;
27190 else if (!saved_greater_than_is_operator_p)
27192 /* If we're in a nested template argument list, the '>>' has
27193 to be a typo for '> >'. We emit the error message, but we
27194 continue parsing and we push a '>' as next token, so that
27195 the argument list will be parsed correctly. Note that the
27196 global source location is still on the token before the
27197 '>>', so we need to say explicitly where we want it. */
27198 cp_token *token = cp_lexer_peek_token (parser->lexer);
27199 gcc_rich_location richloc (token->location);
27200 richloc.add_fixit_replace ("> >");
27201 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27202 "within a nested template argument list");
27204 token->type = CPP_GREATER;
27206 else
27208 /* If this is not a nested template argument list, the '>>'
27209 is a typo for '>'. Emit an error message and continue.
27210 Same deal about the token location, but here we can get it
27211 right by consuming the '>>' before issuing the diagnostic. */
27212 cp_token *token = cp_lexer_consume_token (parser->lexer);
27213 error_at (token->location,
27214 "spurious %<>>%>, use %<>%> to terminate "
27215 "a template argument list");
27218 else
27219 cp_parser_skip_to_end_of_template_parameter_list (parser);
27220 /* The `>' token might be a greater-than operator again now. */
27221 parser->greater_than_is_operator_p
27222 = saved_greater_than_is_operator_p;
27223 /* Restore the SAVED_SCOPE. */
27224 parser->scope = saved_scope;
27225 parser->qualifying_scope = saved_qualifying_scope;
27226 parser->object_scope = saved_object_scope;
27227 cp_unevaluated_operand = saved_unevaluated_operand;
27228 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27230 return arguments;
27233 /* MEMBER_FUNCTION is a member function, or a friend. If default
27234 arguments, or the body of the function have not yet been parsed,
27235 parse them now. */
27237 static void
27238 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27240 timevar_push (TV_PARSE_INMETH);
27241 /* If this member is a template, get the underlying
27242 FUNCTION_DECL. */
27243 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27244 member_function = DECL_TEMPLATE_RESULT (member_function);
27246 /* There should not be any class definitions in progress at this
27247 point; the bodies of members are only parsed outside of all class
27248 definitions. */
27249 gcc_assert (parser->num_classes_being_defined == 0);
27250 /* While we're parsing the member functions we might encounter more
27251 classes. We want to handle them right away, but we don't want
27252 them getting mixed up with functions that are currently in the
27253 queue. */
27254 push_unparsed_function_queues (parser);
27256 /* Make sure that any template parameters are in scope. */
27257 maybe_begin_member_template_processing (member_function);
27259 /* If the body of the function has not yet been parsed, parse it
27260 now. */
27261 if (DECL_PENDING_INLINE_P (member_function))
27263 tree function_scope;
27264 cp_token_cache *tokens;
27266 /* The function is no longer pending; we are processing it. */
27267 tokens = DECL_PENDING_INLINE_INFO (member_function);
27268 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27269 DECL_PENDING_INLINE_P (member_function) = 0;
27271 /* If this is a local class, enter the scope of the containing
27272 function. */
27273 function_scope = current_function_decl;
27274 if (function_scope)
27275 push_function_context ();
27277 /* Push the body of the function onto the lexer stack. */
27278 cp_parser_push_lexer_for_tokens (parser, tokens);
27280 /* Let the front end know that we going to be defining this
27281 function. */
27282 start_preparsed_function (member_function, NULL_TREE,
27283 SF_PRE_PARSED | SF_INCLASS_INLINE);
27285 /* Don't do access checking if it is a templated function. */
27286 if (processing_template_decl)
27287 push_deferring_access_checks (dk_no_check);
27289 /* #pragma omp declare reduction needs special parsing. */
27290 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27292 parser->lexer->in_pragma = true;
27293 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27294 finish_function (/*inline*/2);
27295 cp_check_omp_declare_reduction (member_function);
27297 else
27298 /* Now, parse the body of the function. */
27299 cp_parser_function_definition_after_declarator (parser,
27300 /*inline_p=*/true);
27302 if (processing_template_decl)
27303 pop_deferring_access_checks ();
27305 /* Leave the scope of the containing function. */
27306 if (function_scope)
27307 pop_function_context ();
27308 cp_parser_pop_lexer (parser);
27311 /* Remove any template parameters from the symbol table. */
27312 maybe_end_member_template_processing ();
27314 /* Restore the queue. */
27315 pop_unparsed_function_queues (parser);
27316 timevar_pop (TV_PARSE_INMETH);
27319 /* If DECL contains any default args, remember it on the unparsed
27320 functions queue. */
27322 static void
27323 cp_parser_save_default_args (cp_parser* parser, tree decl)
27325 tree probe;
27327 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27328 probe;
27329 probe = TREE_CHAIN (probe))
27330 if (TREE_PURPOSE (probe))
27332 cp_default_arg_entry entry = {current_class_type, decl};
27333 vec_safe_push (unparsed_funs_with_default_args, entry);
27334 break;
27338 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27339 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27340 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27341 from the parameter-type-list. */
27343 static tree
27344 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27345 tree default_arg, tree parmtype)
27347 cp_token_cache *tokens;
27348 tree parsed_arg;
27349 bool dummy;
27351 if (default_arg == error_mark_node)
27352 return error_mark_node;
27354 /* Push the saved tokens for the default argument onto the parser's
27355 lexer stack. */
27356 tokens = DEFARG_TOKENS (default_arg);
27357 cp_parser_push_lexer_for_tokens (parser, tokens);
27359 start_lambda_scope (decl);
27361 /* Parse the default argument. */
27362 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27363 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27364 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27366 finish_lambda_scope ();
27368 if (parsed_arg == error_mark_node)
27369 cp_parser_skip_to_end_of_statement (parser);
27371 if (!processing_template_decl)
27373 /* In a non-template class, check conversions now. In a template,
27374 we'll wait and instantiate these as needed. */
27375 if (TREE_CODE (decl) == PARM_DECL)
27376 parsed_arg = check_default_argument (parmtype, parsed_arg,
27377 tf_warning_or_error);
27378 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27379 parsed_arg = error_mark_node;
27380 else
27381 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27384 /* If the token stream has not been completely used up, then
27385 there was extra junk after the end of the default
27386 argument. */
27387 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27389 if (TREE_CODE (decl) == PARM_DECL)
27390 cp_parser_error (parser, "expected %<,%>");
27391 else
27392 cp_parser_error (parser, "expected %<;%>");
27395 /* Revert to the main lexer. */
27396 cp_parser_pop_lexer (parser);
27398 return parsed_arg;
27401 /* FIELD is a non-static data member with an initializer which we saved for
27402 later; parse it now. */
27404 static void
27405 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27407 tree def;
27409 maybe_begin_member_template_processing (field);
27411 push_unparsed_function_queues (parser);
27412 def = cp_parser_late_parse_one_default_arg (parser, field,
27413 DECL_INITIAL (field),
27414 NULL_TREE);
27415 pop_unparsed_function_queues (parser);
27417 maybe_end_member_template_processing ();
27419 DECL_INITIAL (field) = def;
27422 /* FN is a FUNCTION_DECL which may contains a parameter with an
27423 unparsed DEFAULT_ARG. Parse the default args now. This function
27424 assumes that the current scope is the scope in which the default
27425 argument should be processed. */
27427 static void
27428 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27430 bool saved_local_variables_forbidden_p;
27431 tree parm, parmdecl;
27433 /* While we're parsing the default args, we might (due to the
27434 statement expression extension) encounter more classes. We want
27435 to handle them right away, but we don't want them getting mixed
27436 up with default args that are currently in the queue. */
27437 push_unparsed_function_queues (parser);
27439 /* Local variable names (and the `this' keyword) may not appear
27440 in a default argument. */
27441 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27442 parser->local_variables_forbidden_p = true;
27444 push_defarg_context (fn);
27446 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27447 parmdecl = DECL_ARGUMENTS (fn);
27448 parm && parm != void_list_node;
27449 parm = TREE_CHAIN (parm),
27450 parmdecl = DECL_CHAIN (parmdecl))
27452 tree default_arg = TREE_PURPOSE (parm);
27453 tree parsed_arg;
27454 vec<tree, va_gc> *insts;
27455 tree copy;
27456 unsigned ix;
27458 if (!default_arg)
27459 continue;
27461 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27462 /* This can happen for a friend declaration for a function
27463 already declared with default arguments. */
27464 continue;
27466 parsed_arg
27467 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27468 default_arg,
27469 TREE_VALUE (parm));
27470 if (parsed_arg == error_mark_node)
27472 continue;
27475 TREE_PURPOSE (parm) = parsed_arg;
27477 /* Update any instantiations we've already created. */
27478 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27479 vec_safe_iterate (insts, ix, &copy); ix++)
27480 TREE_PURPOSE (copy) = parsed_arg;
27483 pop_defarg_context ();
27485 /* Make sure no default arg is missing. */
27486 check_default_args (fn);
27488 /* Restore the state of local_variables_forbidden_p. */
27489 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27491 /* Restore the queue. */
27492 pop_unparsed_function_queues (parser);
27495 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27497 sizeof ... ( identifier )
27499 where the 'sizeof' token has already been consumed. */
27501 static tree
27502 cp_parser_sizeof_pack (cp_parser *parser)
27504 /* Consume the `...'. */
27505 cp_lexer_consume_token (parser->lexer);
27506 maybe_warn_variadic_templates ();
27508 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27509 if (paren)
27510 cp_lexer_consume_token (parser->lexer);
27511 else
27512 permerror (cp_lexer_peek_token (parser->lexer)->location,
27513 "%<sizeof...%> argument must be surrounded by parentheses");
27515 cp_token *token = cp_lexer_peek_token (parser->lexer);
27516 tree name = cp_parser_identifier (parser);
27517 if (name == error_mark_node)
27518 return error_mark_node;
27519 /* The name is not qualified. */
27520 parser->scope = NULL_TREE;
27521 parser->qualifying_scope = NULL_TREE;
27522 parser->object_scope = NULL_TREE;
27523 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27524 if (expr == error_mark_node)
27525 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27526 token->location);
27527 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27528 expr = TREE_TYPE (expr);
27529 else if (TREE_CODE (expr) == CONST_DECL)
27530 expr = DECL_INITIAL (expr);
27531 expr = make_pack_expansion (expr);
27532 PACK_EXPANSION_SIZEOF_P (expr) = true;
27534 if (paren)
27535 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27537 return expr;
27540 /* Parse the operand of `sizeof' (or a similar operator). Returns
27541 either a TYPE or an expression, depending on the form of the
27542 input. The KEYWORD indicates which kind of expression we have
27543 encountered. */
27545 static tree
27546 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27548 tree expr = NULL_TREE;
27549 const char *saved_message;
27550 char *tmp;
27551 bool saved_integral_constant_expression_p;
27552 bool saved_non_integral_constant_expression_p;
27554 /* If it's a `...', then we are computing the length of a parameter
27555 pack. */
27556 if (keyword == RID_SIZEOF
27557 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27558 return cp_parser_sizeof_pack (parser);
27560 /* Types cannot be defined in a `sizeof' expression. Save away the
27561 old message. */
27562 saved_message = parser->type_definition_forbidden_message;
27563 /* And create the new one. */
27564 tmp = concat ("types may not be defined in %<",
27565 IDENTIFIER_POINTER (ridpointers[keyword]),
27566 "%> expressions", NULL);
27567 parser->type_definition_forbidden_message = tmp;
27569 /* The restrictions on constant-expressions do not apply inside
27570 sizeof expressions. */
27571 saved_integral_constant_expression_p
27572 = parser->integral_constant_expression_p;
27573 saved_non_integral_constant_expression_p
27574 = parser->non_integral_constant_expression_p;
27575 parser->integral_constant_expression_p = false;
27577 /* Do not actually evaluate the expression. */
27578 ++cp_unevaluated_operand;
27579 ++c_inhibit_evaluation_warnings;
27580 /* If it's a `(', then we might be looking at the type-id
27581 construction. */
27582 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27584 tree type = NULL_TREE;
27586 /* We can't be sure yet whether we're looking at a type-id or an
27587 expression. */
27588 cp_parser_parse_tentatively (parser);
27589 /* Note: as a GNU Extension, compound literals are considered
27590 postfix-expressions as they are in C99, so they are valid
27591 arguments to sizeof. See comment in cp_parser_cast_expression
27592 for details. */
27593 if (cp_parser_compound_literal_p (parser))
27594 cp_parser_simulate_error (parser);
27595 else
27597 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27598 parser->in_type_id_in_expr_p = true;
27599 /* Look for the type-id. */
27600 type = cp_parser_type_id (parser);
27601 /* Look for the closing `)'. */
27602 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27603 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27606 /* If all went well, then we're done. */
27607 if (cp_parser_parse_definitely (parser))
27609 cp_decl_specifier_seq decl_specs;
27611 /* Build a trivial decl-specifier-seq. */
27612 clear_decl_specs (&decl_specs);
27613 decl_specs.type = type;
27615 /* Call grokdeclarator to figure out what type this is. */
27616 expr = grokdeclarator (NULL,
27617 &decl_specs,
27618 TYPENAME,
27619 /*initialized=*/0,
27620 /*attrlist=*/NULL);
27624 /* If the type-id production did not work out, then we must be
27625 looking at the unary-expression production. */
27626 if (!expr)
27627 expr = cp_parser_unary_expression (parser);
27629 /* Go back to evaluating expressions. */
27630 --cp_unevaluated_operand;
27631 --c_inhibit_evaluation_warnings;
27633 /* Free the message we created. */
27634 free (tmp);
27635 /* And restore the old one. */
27636 parser->type_definition_forbidden_message = saved_message;
27637 parser->integral_constant_expression_p
27638 = saved_integral_constant_expression_p;
27639 parser->non_integral_constant_expression_p
27640 = saved_non_integral_constant_expression_p;
27642 return expr;
27645 /* If the current declaration has no declarator, return true. */
27647 static bool
27648 cp_parser_declares_only_class_p (cp_parser *parser)
27650 /* If the next token is a `;' or a `,' then there is no
27651 declarator. */
27652 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27653 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27656 /* Update the DECL_SPECS to reflect the storage class indicated by
27657 KEYWORD. */
27659 static void
27660 cp_parser_set_storage_class (cp_parser *parser,
27661 cp_decl_specifier_seq *decl_specs,
27662 enum rid keyword,
27663 cp_token *token)
27665 cp_storage_class storage_class;
27667 if (parser->in_unbraced_linkage_specification_p)
27669 error_at (token->location, "invalid use of %qD in linkage specification",
27670 ridpointers[keyword]);
27671 return;
27673 else if (decl_specs->storage_class != sc_none)
27675 decl_specs->conflicting_specifiers_p = true;
27676 return;
27679 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27680 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27681 && decl_specs->gnu_thread_keyword_p)
27683 pedwarn (decl_specs->locations[ds_thread], 0,
27684 "%<__thread%> before %qD", ridpointers[keyword]);
27687 switch (keyword)
27689 case RID_AUTO:
27690 storage_class = sc_auto;
27691 break;
27692 case RID_REGISTER:
27693 storage_class = sc_register;
27694 break;
27695 case RID_STATIC:
27696 storage_class = sc_static;
27697 break;
27698 case RID_EXTERN:
27699 storage_class = sc_extern;
27700 break;
27701 case RID_MUTABLE:
27702 storage_class = sc_mutable;
27703 break;
27704 default:
27705 gcc_unreachable ();
27707 decl_specs->storage_class = storage_class;
27708 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27710 /* A storage class specifier cannot be applied alongside a typedef
27711 specifier. If there is a typedef specifier present then set
27712 conflicting_specifiers_p which will trigger an error later
27713 on in grokdeclarator. */
27714 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27715 decl_specs->conflicting_specifiers_p = true;
27718 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27719 is true, the type is a class or enum definition. */
27721 static void
27722 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27723 tree type_spec,
27724 cp_token *token,
27725 bool type_definition_p)
27727 decl_specs->any_specifiers_p = true;
27729 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27730 (with, for example, in "typedef int wchar_t;") we remember that
27731 this is what happened. In system headers, we ignore these
27732 declarations so that G++ can work with system headers that are not
27733 C++-safe. */
27734 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27735 && !type_definition_p
27736 && (type_spec == boolean_type_node
27737 || type_spec == char16_type_node
27738 || type_spec == char32_type_node
27739 || type_spec == wchar_type_node)
27740 && (decl_specs->type
27741 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27742 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27743 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27744 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27746 decl_specs->redefined_builtin_type = type_spec;
27747 set_and_check_decl_spec_loc (decl_specs,
27748 ds_redefined_builtin_type_spec,
27749 token);
27750 if (!decl_specs->type)
27752 decl_specs->type = type_spec;
27753 decl_specs->type_definition_p = false;
27754 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27757 else if (decl_specs->type)
27758 decl_specs->multiple_types_p = true;
27759 else
27761 decl_specs->type = type_spec;
27762 decl_specs->type_definition_p = type_definition_p;
27763 decl_specs->redefined_builtin_type = NULL_TREE;
27764 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27768 /* True iff TOKEN is the GNU keyword __thread. */
27770 static bool
27771 token_is__thread (cp_token *token)
27773 gcc_assert (token->keyword == RID_THREAD);
27774 return id_equal (token->u.value, "__thread");
27777 /* Set the location for a declarator specifier and check if it is
27778 duplicated.
27780 DECL_SPECS is the sequence of declarator specifiers onto which to
27781 set the location.
27783 DS is the single declarator specifier to set which location is to
27784 be set onto the existing sequence of declarators.
27786 LOCATION is the location for the declarator specifier to
27787 consider. */
27789 static void
27790 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27791 cp_decl_spec ds, cp_token *token)
27793 gcc_assert (ds < ds_last);
27795 if (decl_specs == NULL)
27796 return;
27798 source_location location = token->location;
27800 if (decl_specs->locations[ds] == 0)
27802 decl_specs->locations[ds] = location;
27803 if (ds == ds_thread)
27804 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27806 else
27808 if (ds == ds_long)
27810 if (decl_specs->locations[ds_long_long] != 0)
27811 error_at (location,
27812 "%<long long long%> is too long for GCC");
27813 else
27815 decl_specs->locations[ds_long_long] = location;
27816 pedwarn_cxx98 (location,
27817 OPT_Wlong_long,
27818 "ISO C++ 1998 does not support %<long long%>");
27821 else if (ds == ds_thread)
27823 bool gnu = token_is__thread (token);
27824 if (gnu != decl_specs->gnu_thread_keyword_p)
27825 error_at (location,
27826 "both %<__thread%> and %<thread_local%> specified");
27827 else
27829 gcc_rich_location richloc (location);
27830 richloc.add_fixit_remove ();
27831 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27834 else
27836 static const char *const decl_spec_names[] = {
27837 "signed",
27838 "unsigned",
27839 "short",
27840 "long",
27841 "const",
27842 "volatile",
27843 "restrict",
27844 "inline",
27845 "virtual",
27846 "explicit",
27847 "friend",
27848 "typedef",
27849 "using",
27850 "constexpr",
27851 "__complex"
27853 gcc_rich_location richloc (location);
27854 richloc.add_fixit_remove ();
27855 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27860 /* Return true iff the declarator specifier DS is present in the
27861 sequence of declarator specifiers DECL_SPECS. */
27863 bool
27864 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27865 cp_decl_spec ds)
27867 gcc_assert (ds < ds_last);
27869 if (decl_specs == NULL)
27870 return false;
27872 return decl_specs->locations[ds] != 0;
27875 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27876 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27878 static bool
27879 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27881 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27884 /* Issue an error message indicating that TOKEN_DESC was expected.
27885 If KEYWORD is true, it indicated this function is called by
27886 cp_parser_require_keword and the required token can only be
27887 a indicated keyword. */
27889 static void
27890 cp_parser_required_error (cp_parser *parser,
27891 required_token token_desc,
27892 bool keyword)
27894 switch (token_desc)
27896 case RT_NEW:
27897 cp_parser_error (parser, "expected %<new%>");
27898 return;
27899 case RT_DELETE:
27900 cp_parser_error (parser, "expected %<delete%>");
27901 return;
27902 case RT_RETURN:
27903 cp_parser_error (parser, "expected %<return%>");
27904 return;
27905 case RT_WHILE:
27906 cp_parser_error (parser, "expected %<while%>");
27907 return;
27908 case RT_EXTERN:
27909 cp_parser_error (parser, "expected %<extern%>");
27910 return;
27911 case RT_STATIC_ASSERT:
27912 cp_parser_error (parser, "expected %<static_assert%>");
27913 return;
27914 case RT_DECLTYPE:
27915 cp_parser_error (parser, "expected %<decltype%>");
27916 return;
27917 case RT_OPERATOR:
27918 cp_parser_error (parser, "expected %<operator%>");
27919 return;
27920 case RT_CLASS:
27921 cp_parser_error (parser, "expected %<class%>");
27922 return;
27923 case RT_TEMPLATE:
27924 cp_parser_error (parser, "expected %<template%>");
27925 return;
27926 case RT_NAMESPACE:
27927 cp_parser_error (parser, "expected %<namespace%>");
27928 return;
27929 case RT_USING:
27930 cp_parser_error (parser, "expected %<using%>");
27931 return;
27932 case RT_ASM:
27933 cp_parser_error (parser, "expected %<asm%>");
27934 return;
27935 case RT_TRY:
27936 cp_parser_error (parser, "expected %<try%>");
27937 return;
27938 case RT_CATCH:
27939 cp_parser_error (parser, "expected %<catch%>");
27940 return;
27941 case RT_THROW:
27942 cp_parser_error (parser, "expected %<throw%>");
27943 return;
27944 case RT_LABEL:
27945 cp_parser_error (parser, "expected %<__label__%>");
27946 return;
27947 case RT_AT_TRY:
27948 cp_parser_error (parser, "expected %<@try%>");
27949 return;
27950 case RT_AT_SYNCHRONIZED:
27951 cp_parser_error (parser, "expected %<@synchronized%>");
27952 return;
27953 case RT_AT_THROW:
27954 cp_parser_error (parser, "expected %<@throw%>");
27955 return;
27956 case RT_TRANSACTION_ATOMIC:
27957 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27958 return;
27959 case RT_TRANSACTION_RELAXED:
27960 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27961 return;
27962 default:
27963 break;
27965 if (!keyword)
27967 switch (token_desc)
27969 case RT_SEMICOLON:
27970 cp_parser_error (parser, "expected %<;%>");
27971 return;
27972 case RT_OPEN_PAREN:
27973 cp_parser_error (parser, "expected %<(%>");
27974 return;
27975 case RT_CLOSE_BRACE:
27976 cp_parser_error (parser, "expected %<}%>");
27977 return;
27978 case RT_OPEN_BRACE:
27979 cp_parser_error (parser, "expected %<{%>");
27980 return;
27981 case RT_CLOSE_SQUARE:
27982 cp_parser_error (parser, "expected %<]%>");
27983 return;
27984 case RT_OPEN_SQUARE:
27985 cp_parser_error (parser, "expected %<[%>");
27986 return;
27987 case RT_COMMA:
27988 cp_parser_error (parser, "expected %<,%>");
27989 return;
27990 case RT_SCOPE:
27991 cp_parser_error (parser, "expected %<::%>");
27992 return;
27993 case RT_LESS:
27994 cp_parser_error (parser, "expected %<<%>");
27995 return;
27996 case RT_GREATER:
27997 cp_parser_error (parser, "expected %<>%>");
27998 return;
27999 case RT_EQ:
28000 cp_parser_error (parser, "expected %<=%>");
28001 return;
28002 case RT_ELLIPSIS:
28003 cp_parser_error (parser, "expected %<...%>");
28004 return;
28005 case RT_MULT:
28006 cp_parser_error (parser, "expected %<*%>");
28007 return;
28008 case RT_COMPL:
28009 cp_parser_error (parser, "expected %<~%>");
28010 return;
28011 case RT_COLON:
28012 cp_parser_error (parser, "expected %<:%>");
28013 return;
28014 case RT_COLON_SCOPE:
28015 cp_parser_error (parser, "expected %<:%> or %<::%>");
28016 return;
28017 case RT_CLOSE_PAREN:
28018 cp_parser_error (parser, "expected %<)%>");
28019 return;
28020 case RT_COMMA_CLOSE_PAREN:
28021 cp_parser_error (parser, "expected %<,%> or %<)%>");
28022 return;
28023 case RT_PRAGMA_EOL:
28024 cp_parser_error (parser, "expected end of line");
28025 return;
28026 case RT_NAME:
28027 cp_parser_error (parser, "expected identifier");
28028 return;
28029 case RT_SELECT:
28030 cp_parser_error (parser, "expected selection-statement");
28031 return;
28032 case RT_INTERATION:
28033 cp_parser_error (parser, "expected iteration-statement");
28034 return;
28035 case RT_JUMP:
28036 cp_parser_error (parser, "expected jump-statement");
28037 return;
28038 case RT_CLASS_KEY:
28039 cp_parser_error (parser, "expected class-key");
28040 return;
28041 case RT_CLASS_TYPENAME_TEMPLATE:
28042 cp_parser_error (parser,
28043 "expected %<class%>, %<typename%>, or %<template%>");
28044 return;
28045 default:
28046 gcc_unreachable ();
28049 else
28050 gcc_unreachable ();
28055 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28056 issue an error message indicating that TOKEN_DESC was expected.
28058 Returns the token consumed, if the token had the appropriate type.
28059 Otherwise, returns NULL. */
28061 static cp_token *
28062 cp_parser_require (cp_parser* parser,
28063 enum cpp_ttype type,
28064 required_token token_desc)
28066 if (cp_lexer_next_token_is (parser->lexer, type))
28067 return cp_lexer_consume_token (parser->lexer);
28068 else
28070 /* Output the MESSAGE -- unless we're parsing tentatively. */
28071 if (!cp_parser_simulate_error (parser))
28072 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
28073 return NULL;
28077 /* An error message is produced if the next token is not '>'.
28078 All further tokens are skipped until the desired token is
28079 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28081 static void
28082 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28084 /* Current level of '< ... >'. */
28085 unsigned level = 0;
28086 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28087 unsigned nesting_depth = 0;
28089 /* Are we ready, yet? If not, issue error message. */
28090 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28091 return;
28093 /* Skip tokens until the desired token is found. */
28094 while (true)
28096 /* Peek at the next token. */
28097 switch (cp_lexer_peek_token (parser->lexer)->type)
28099 case CPP_LESS:
28100 if (!nesting_depth)
28101 ++level;
28102 break;
28104 case CPP_RSHIFT:
28105 if (cxx_dialect == cxx98)
28106 /* C++0x views the `>>' operator as two `>' tokens, but
28107 C++98 does not. */
28108 break;
28109 else if (!nesting_depth && level-- == 0)
28111 /* We've hit a `>>' where the first `>' closes the
28112 template argument list, and the second `>' is
28113 spurious. Just consume the `>>' and stop; we've
28114 already produced at least one error. */
28115 cp_lexer_consume_token (parser->lexer);
28116 return;
28118 /* Fall through for C++0x, so we handle the second `>' in
28119 the `>>'. */
28120 gcc_fallthrough ();
28122 case CPP_GREATER:
28123 if (!nesting_depth && level-- == 0)
28125 /* We've reached the token we want, consume it and stop. */
28126 cp_lexer_consume_token (parser->lexer);
28127 return;
28129 break;
28131 case CPP_OPEN_PAREN:
28132 case CPP_OPEN_SQUARE:
28133 ++nesting_depth;
28134 break;
28136 case CPP_CLOSE_PAREN:
28137 case CPP_CLOSE_SQUARE:
28138 if (nesting_depth-- == 0)
28139 return;
28140 break;
28142 case CPP_EOF:
28143 case CPP_PRAGMA_EOL:
28144 case CPP_SEMICOLON:
28145 case CPP_OPEN_BRACE:
28146 case CPP_CLOSE_BRACE:
28147 /* The '>' was probably forgotten, don't look further. */
28148 return;
28150 default:
28151 break;
28154 /* Consume this token. */
28155 cp_lexer_consume_token (parser->lexer);
28159 /* If the next token is the indicated keyword, consume it. Otherwise,
28160 issue an error message indicating that TOKEN_DESC was expected.
28162 Returns the token consumed, if the token had the appropriate type.
28163 Otherwise, returns NULL. */
28165 static cp_token *
28166 cp_parser_require_keyword (cp_parser* parser,
28167 enum rid keyword,
28168 required_token token_desc)
28170 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28172 if (token && token->keyword != keyword)
28174 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28175 return NULL;
28178 return token;
28181 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28182 function-definition. */
28184 static bool
28185 cp_parser_token_starts_function_definition_p (cp_token* token)
28187 return (/* An ordinary function-body begins with an `{'. */
28188 token->type == CPP_OPEN_BRACE
28189 /* A ctor-initializer begins with a `:'. */
28190 || token->type == CPP_COLON
28191 /* A function-try-block begins with `try'. */
28192 || token->keyword == RID_TRY
28193 /* A function-transaction-block begins with `__transaction_atomic'
28194 or `__transaction_relaxed'. */
28195 || token->keyword == RID_TRANSACTION_ATOMIC
28196 || token->keyword == RID_TRANSACTION_RELAXED
28197 /* The named return value extension begins with `return'. */
28198 || token->keyword == RID_RETURN);
28201 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28202 definition. */
28204 static bool
28205 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28207 cp_token *token;
28209 token = cp_lexer_peek_token (parser->lexer);
28210 return (token->type == CPP_OPEN_BRACE
28211 || (token->type == CPP_COLON
28212 && !parser->colon_doesnt_start_class_def_p));
28215 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28216 C++0x) ending a template-argument. */
28218 static bool
28219 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28221 cp_token *token;
28223 token = cp_lexer_peek_token (parser->lexer);
28224 return (token->type == CPP_COMMA
28225 || token->type == CPP_GREATER
28226 || token->type == CPP_ELLIPSIS
28227 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28230 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28231 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28233 static bool
28234 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28235 size_t n)
28237 cp_token *token;
28239 token = cp_lexer_peek_nth_token (parser->lexer, n);
28240 if (token->type == CPP_LESS)
28241 return true;
28242 /* Check for the sequence `<::' in the original code. It would be lexed as
28243 `[:', where `[' is a digraph, and there is no whitespace before
28244 `:'. */
28245 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28247 cp_token *token2;
28248 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28249 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28250 return true;
28252 return false;
28255 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28256 or none_type otherwise. */
28258 static enum tag_types
28259 cp_parser_token_is_class_key (cp_token* token)
28261 switch (token->keyword)
28263 case RID_CLASS:
28264 return class_type;
28265 case RID_STRUCT:
28266 return record_type;
28267 case RID_UNION:
28268 return union_type;
28270 default:
28271 return none_type;
28275 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28276 or none_type otherwise or if the token is null. */
28278 static enum tag_types
28279 cp_parser_token_is_type_parameter_key (cp_token* token)
28281 if (!token)
28282 return none_type;
28284 switch (token->keyword)
28286 case RID_CLASS:
28287 return class_type;
28288 case RID_TYPENAME:
28289 return typename_type;
28291 default:
28292 return none_type;
28296 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28298 static void
28299 cp_parser_check_class_key (enum tag_types class_key, tree type)
28301 if (type == error_mark_node)
28302 return;
28303 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28305 if (permerror (input_location, "%qs tag used in naming %q#T",
28306 class_key == union_type ? "union"
28307 : class_key == record_type ? "struct" : "class",
28308 type))
28309 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28310 "%q#T was previously declared here", type);
28314 /* Issue an error message if DECL is redeclared with different
28315 access than its original declaration [class.access.spec/3].
28316 This applies to nested classes, nested class templates and
28317 enumerations [class.mem/1]. */
28319 static void
28320 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28322 if (!decl
28323 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28324 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28325 return;
28327 if ((TREE_PRIVATE (decl)
28328 != (current_access_specifier == access_private_node))
28329 || (TREE_PROTECTED (decl)
28330 != (current_access_specifier == access_protected_node)))
28331 error_at (location, "%qD redeclared with different access", decl);
28334 /* Look for the `template' keyword, as a syntactic disambiguator.
28335 Return TRUE iff it is present, in which case it will be
28336 consumed. */
28338 static bool
28339 cp_parser_optional_template_keyword (cp_parser *parser)
28341 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28343 /* In C++98 the `template' keyword can only be used within templates;
28344 outside templates the parser can always figure out what is a
28345 template and what is not. In C++11, per the resolution of DR 468,
28346 `template' is allowed in cases where it is not strictly necessary. */
28347 if (!processing_template_decl
28348 && pedantic && cxx_dialect == cxx98)
28350 cp_token *token = cp_lexer_peek_token (parser->lexer);
28351 pedwarn (token->location, OPT_Wpedantic,
28352 "in C++98 %<template%> (as a disambiguator) is only "
28353 "allowed within templates");
28354 /* If this part of the token stream is rescanned, the same
28355 error message would be generated. So, we purge the token
28356 from the stream. */
28357 cp_lexer_purge_token (parser->lexer);
28358 return false;
28360 else
28362 /* Consume the `template' keyword. */
28363 cp_lexer_consume_token (parser->lexer);
28364 return true;
28367 return false;
28370 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28371 set PARSER->SCOPE, and perform other related actions. */
28373 static void
28374 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28376 struct tree_check *check_value;
28378 /* Get the stored value. */
28379 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28380 /* Set the scope from the stored value. */
28381 parser->scope = saved_checks_value (check_value);
28382 parser->qualifying_scope = check_value->qualifying_scope;
28383 parser->object_scope = NULL_TREE;
28386 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28387 encounter the end of a block before what we were looking for. */
28389 static bool
28390 cp_parser_cache_group (cp_parser *parser,
28391 enum cpp_ttype end,
28392 unsigned depth)
28394 while (true)
28396 cp_token *token = cp_lexer_peek_token (parser->lexer);
28398 /* Abort a parenthesized expression if we encounter a semicolon. */
28399 if ((end == CPP_CLOSE_PAREN || depth == 0)
28400 && token->type == CPP_SEMICOLON)
28401 return true;
28402 /* If we've reached the end of the file, stop. */
28403 if (token->type == CPP_EOF
28404 || (end != CPP_PRAGMA_EOL
28405 && token->type == CPP_PRAGMA_EOL))
28406 return true;
28407 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28408 /* We've hit the end of an enclosing block, so there's been some
28409 kind of syntax error. */
28410 return true;
28412 /* Consume the token. */
28413 cp_lexer_consume_token (parser->lexer);
28414 /* See if it starts a new group. */
28415 if (token->type == CPP_OPEN_BRACE)
28417 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28418 /* In theory this should probably check end == '}', but
28419 cp_parser_save_member_function_body needs it to exit
28420 after either '}' or ')' when called with ')'. */
28421 if (depth == 0)
28422 return false;
28424 else if (token->type == CPP_OPEN_PAREN)
28426 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28427 if (depth == 0 && end == CPP_CLOSE_PAREN)
28428 return false;
28430 else if (token->type == CPP_PRAGMA)
28431 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28432 else if (token->type == end)
28433 return false;
28437 /* Like above, for caching a default argument or NSDMI. Both of these are
28438 terminated by a non-nested comma, but it can be unclear whether or not a
28439 comma is nested in a template argument list unless we do more parsing.
28440 In order to handle this ambiguity, when we encounter a ',' after a '<'
28441 we try to parse what follows as a parameter-declaration-list (in the
28442 case of a default argument) or a member-declarator (in the case of an
28443 NSDMI). If that succeeds, then we stop caching. */
28445 static tree
28446 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28448 unsigned depth = 0;
28449 int maybe_template_id = 0;
28450 cp_token *first_token;
28451 cp_token *token;
28452 tree default_argument;
28454 /* Add tokens until we have processed the entire default
28455 argument. We add the range [first_token, token). */
28456 first_token = cp_lexer_peek_token (parser->lexer);
28457 if (first_token->type == CPP_OPEN_BRACE)
28459 /* For list-initialization, this is straightforward. */
28460 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28461 token = cp_lexer_peek_token (parser->lexer);
28463 else while (true)
28465 bool done = false;
28467 /* Peek at the next token. */
28468 token = cp_lexer_peek_token (parser->lexer);
28469 /* What we do depends on what token we have. */
28470 switch (token->type)
28472 /* In valid code, a default argument must be
28473 immediately followed by a `,' `)', or `...'. */
28474 case CPP_COMMA:
28475 if (depth == 0 && maybe_template_id)
28477 /* If we've seen a '<', we might be in a
28478 template-argument-list. Until Core issue 325 is
28479 resolved, we don't know how this situation ought
28480 to be handled, so try to DTRT. We check whether
28481 what comes after the comma is a valid parameter
28482 declaration list. If it is, then the comma ends
28483 the default argument; otherwise the default
28484 argument continues. */
28485 bool error = false;
28486 cp_token *peek;
28488 /* Set ITALP so cp_parser_parameter_declaration_list
28489 doesn't decide to commit to this parse. */
28490 bool saved_italp = parser->in_template_argument_list_p;
28491 parser->in_template_argument_list_p = true;
28493 cp_parser_parse_tentatively (parser);
28495 if (nsdmi)
28497 /* Parse declarators until we reach a non-comma or
28498 somthing that cannot be an initializer.
28499 Just checking whether we're looking at a single
28500 declarator is insufficient. Consider:
28501 int var = tuple<T,U>::x;
28502 The template parameter 'U' looks exactly like a
28503 declarator. */
28506 int ctor_dtor_or_conv_p;
28507 cp_lexer_consume_token (parser->lexer);
28508 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28509 &ctor_dtor_or_conv_p,
28510 /*parenthesized_p=*/NULL,
28511 /*member_p=*/true,
28512 /*friend_p=*/false);
28513 peek = cp_lexer_peek_token (parser->lexer);
28514 if (cp_parser_error_occurred (parser))
28515 break;
28517 while (peek->type == CPP_COMMA);
28518 /* If we met an '=' or ';' then the original comma
28519 was the end of the NSDMI. Otherwise assume
28520 we're still in the NSDMI. */
28521 error = (peek->type != CPP_EQ
28522 && peek->type != CPP_SEMICOLON);
28524 else
28526 cp_lexer_consume_token (parser->lexer);
28527 begin_scope (sk_function_parms, NULL_TREE);
28528 cp_parser_parameter_declaration_list (parser, &error);
28529 pop_bindings_and_leave_scope ();
28531 if (!cp_parser_error_occurred (parser) && !error)
28532 done = true;
28533 cp_parser_abort_tentative_parse (parser);
28535 parser->in_template_argument_list_p = saved_italp;
28536 break;
28538 /* FALLTHRU */
28539 case CPP_CLOSE_PAREN:
28540 case CPP_ELLIPSIS:
28541 /* If we run into a non-nested `;', `}', or `]',
28542 then the code is invalid -- but the default
28543 argument is certainly over. */
28544 case CPP_SEMICOLON:
28545 case CPP_CLOSE_BRACE:
28546 case CPP_CLOSE_SQUARE:
28547 if (depth == 0
28548 /* Handle correctly int n = sizeof ... ( p ); */
28549 && token->type != CPP_ELLIPSIS)
28550 done = true;
28551 /* Update DEPTH, if necessary. */
28552 else if (token->type == CPP_CLOSE_PAREN
28553 || token->type == CPP_CLOSE_BRACE
28554 || token->type == CPP_CLOSE_SQUARE)
28555 --depth;
28556 break;
28558 case CPP_OPEN_PAREN:
28559 case CPP_OPEN_SQUARE:
28560 case CPP_OPEN_BRACE:
28561 ++depth;
28562 break;
28564 case CPP_LESS:
28565 if (depth == 0)
28566 /* This might be the comparison operator, or it might
28567 start a template argument list. */
28568 ++maybe_template_id;
28569 break;
28571 case CPP_RSHIFT:
28572 if (cxx_dialect == cxx98)
28573 break;
28574 /* Fall through for C++0x, which treats the `>>'
28575 operator like two `>' tokens in certain
28576 cases. */
28577 gcc_fallthrough ();
28579 case CPP_GREATER:
28580 if (depth == 0)
28582 /* This might be an operator, or it might close a
28583 template argument list. But if a previous '<'
28584 started a template argument list, this will have
28585 closed it, so we can't be in one anymore. */
28586 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28587 if (maybe_template_id < 0)
28588 maybe_template_id = 0;
28590 break;
28592 /* If we run out of tokens, issue an error message. */
28593 case CPP_EOF:
28594 case CPP_PRAGMA_EOL:
28595 error_at (token->location, "file ends in default argument");
28596 return error_mark_node;
28598 case CPP_NAME:
28599 case CPP_SCOPE:
28600 /* In these cases, we should look for template-ids.
28601 For example, if the default argument is
28602 `X<int, double>()', we need to do name lookup to
28603 figure out whether or not `X' is a template; if
28604 so, the `,' does not end the default argument.
28606 That is not yet done. */
28607 break;
28609 default:
28610 break;
28613 /* If we've reached the end, stop. */
28614 if (done)
28615 break;
28617 /* Add the token to the token block. */
28618 token = cp_lexer_consume_token (parser->lexer);
28621 /* Create a DEFAULT_ARG to represent the unparsed default
28622 argument. */
28623 default_argument = make_node (DEFAULT_ARG);
28624 DEFARG_TOKENS (default_argument)
28625 = cp_token_cache_new (first_token, token);
28626 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28628 return default_argument;
28631 /* Begin parsing tentatively. We always save tokens while parsing
28632 tentatively so that if the tentative parsing fails we can restore the
28633 tokens. */
28635 static void
28636 cp_parser_parse_tentatively (cp_parser* parser)
28638 /* Enter a new parsing context. */
28639 parser->context = cp_parser_context_new (parser->context);
28640 /* Begin saving tokens. */
28641 cp_lexer_save_tokens (parser->lexer);
28642 /* In order to avoid repetitive access control error messages,
28643 access checks are queued up until we are no longer parsing
28644 tentatively. */
28645 push_deferring_access_checks (dk_deferred);
28648 /* Commit to the currently active tentative parse. */
28650 static void
28651 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28653 cp_parser_context *context;
28654 cp_lexer *lexer;
28656 /* Mark all of the levels as committed. */
28657 lexer = parser->lexer;
28658 for (context = parser->context; context->next; context = context->next)
28660 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28661 break;
28662 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28663 while (!cp_lexer_saving_tokens (lexer))
28664 lexer = lexer->next;
28665 cp_lexer_commit_tokens (lexer);
28669 /* Commit to the topmost currently active tentative parse.
28671 Note that this function shouldn't be called when there are
28672 irreversible side-effects while in a tentative state. For
28673 example, we shouldn't create a permanent entry in the symbol
28674 table, or issue an error message that might not apply if the
28675 tentative parse is aborted. */
28677 static void
28678 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28680 cp_parser_context *context = parser->context;
28681 cp_lexer *lexer = parser->lexer;
28683 if (context)
28685 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28686 return;
28687 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28689 while (!cp_lexer_saving_tokens (lexer))
28690 lexer = lexer->next;
28691 cp_lexer_commit_tokens (lexer);
28695 /* Abort the currently active tentative parse. All consumed tokens
28696 will be rolled back, and no diagnostics will be issued. */
28698 static void
28699 cp_parser_abort_tentative_parse (cp_parser* parser)
28701 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28702 || errorcount > 0);
28703 cp_parser_simulate_error (parser);
28704 /* Now, pretend that we want to see if the construct was
28705 successfully parsed. */
28706 cp_parser_parse_definitely (parser);
28709 /* Stop parsing tentatively. If a parse error has occurred, restore the
28710 token stream. Otherwise, commit to the tokens we have consumed.
28711 Returns true if no error occurred; false otherwise. */
28713 static bool
28714 cp_parser_parse_definitely (cp_parser* parser)
28716 bool error_occurred;
28717 cp_parser_context *context;
28719 /* Remember whether or not an error occurred, since we are about to
28720 destroy that information. */
28721 error_occurred = cp_parser_error_occurred (parser);
28722 /* Remove the topmost context from the stack. */
28723 context = parser->context;
28724 parser->context = context->next;
28725 /* If no parse errors occurred, commit to the tentative parse. */
28726 if (!error_occurred)
28728 /* Commit to the tokens read tentatively, unless that was
28729 already done. */
28730 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28731 cp_lexer_commit_tokens (parser->lexer);
28733 pop_to_parent_deferring_access_checks ();
28735 /* Otherwise, if errors occurred, roll back our state so that things
28736 are just as they were before we began the tentative parse. */
28737 else
28739 cp_lexer_rollback_tokens (parser->lexer);
28740 pop_deferring_access_checks ();
28742 /* Add the context to the front of the free list. */
28743 context->next = cp_parser_context_free_list;
28744 cp_parser_context_free_list = context;
28746 return !error_occurred;
28749 /* Returns true if we are parsing tentatively and are not committed to
28750 this tentative parse. */
28752 static bool
28753 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28755 return (cp_parser_parsing_tentatively (parser)
28756 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28759 /* Returns nonzero iff an error has occurred during the most recent
28760 tentative parse. */
28762 static bool
28763 cp_parser_error_occurred (cp_parser* parser)
28765 return (cp_parser_parsing_tentatively (parser)
28766 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28769 /* Returns nonzero if GNU extensions are allowed. */
28771 static bool
28772 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28774 return parser->allow_gnu_extensions_p;
28777 /* Objective-C++ Productions */
28780 /* Parse an Objective-C expression, which feeds into a primary-expression
28781 above.
28783 objc-expression:
28784 objc-message-expression
28785 objc-string-literal
28786 objc-encode-expression
28787 objc-protocol-expression
28788 objc-selector-expression
28790 Returns a tree representation of the expression. */
28792 static cp_expr
28793 cp_parser_objc_expression (cp_parser* parser)
28795 /* Try to figure out what kind of declaration is present. */
28796 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28798 switch (kwd->type)
28800 case CPP_OPEN_SQUARE:
28801 return cp_parser_objc_message_expression (parser);
28803 case CPP_OBJC_STRING:
28804 kwd = cp_lexer_consume_token (parser->lexer);
28805 return objc_build_string_object (kwd->u.value);
28807 case CPP_KEYWORD:
28808 switch (kwd->keyword)
28810 case RID_AT_ENCODE:
28811 return cp_parser_objc_encode_expression (parser);
28813 case RID_AT_PROTOCOL:
28814 return cp_parser_objc_protocol_expression (parser);
28816 case RID_AT_SELECTOR:
28817 return cp_parser_objc_selector_expression (parser);
28819 default:
28820 break;
28822 default:
28823 error_at (kwd->location,
28824 "misplaced %<@%D%> Objective-C++ construct",
28825 kwd->u.value);
28826 cp_parser_skip_to_end_of_block_or_statement (parser);
28829 return error_mark_node;
28832 /* Parse an Objective-C message expression.
28834 objc-message-expression:
28835 [ objc-message-receiver objc-message-args ]
28837 Returns a representation of an Objective-C message. */
28839 static tree
28840 cp_parser_objc_message_expression (cp_parser* parser)
28842 tree receiver, messageargs;
28844 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28845 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28846 receiver = cp_parser_objc_message_receiver (parser);
28847 messageargs = cp_parser_objc_message_args (parser);
28848 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28849 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28851 tree result = objc_build_message_expr (receiver, messageargs);
28853 /* Construct a location e.g.
28854 [self func1:5]
28855 ^~~~~~~~~~~~~~
28856 ranging from the '[' to the ']', with the caret at the start. */
28857 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28858 protected_set_expr_location (result, combined_loc);
28860 return result;
28863 /* Parse an objc-message-receiver.
28865 objc-message-receiver:
28866 expression
28867 simple-type-specifier
28869 Returns a representation of the type or expression. */
28871 static tree
28872 cp_parser_objc_message_receiver (cp_parser* parser)
28874 tree rcv;
28876 /* An Objective-C message receiver may be either (1) a type
28877 or (2) an expression. */
28878 cp_parser_parse_tentatively (parser);
28879 rcv = cp_parser_expression (parser);
28881 /* If that worked out, fine. */
28882 if (cp_parser_parse_definitely (parser))
28883 return rcv;
28885 cp_parser_parse_tentatively (parser);
28886 rcv = cp_parser_simple_type_specifier (parser,
28887 /*decl_specs=*/NULL,
28888 CP_PARSER_FLAGS_NONE);
28890 if (cp_parser_parse_definitely (parser))
28891 return objc_get_class_reference (rcv);
28893 cp_parser_error (parser, "objective-c++ message receiver expected");
28894 return error_mark_node;
28897 /* Parse the arguments and selectors comprising an Objective-C message.
28899 objc-message-args:
28900 objc-selector
28901 objc-selector-args
28902 objc-selector-args , objc-comma-args
28904 objc-selector-args:
28905 objc-selector [opt] : assignment-expression
28906 objc-selector-args objc-selector [opt] : assignment-expression
28908 objc-comma-args:
28909 assignment-expression
28910 objc-comma-args , assignment-expression
28912 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28913 selector arguments and TREE_VALUE containing a list of comma
28914 arguments. */
28916 static tree
28917 cp_parser_objc_message_args (cp_parser* parser)
28919 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28920 bool maybe_unary_selector_p = true;
28921 cp_token *token = cp_lexer_peek_token (parser->lexer);
28923 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28925 tree selector = NULL_TREE, arg;
28927 if (token->type != CPP_COLON)
28928 selector = cp_parser_objc_selector (parser);
28930 /* Detect if we have a unary selector. */
28931 if (maybe_unary_selector_p
28932 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28933 return build_tree_list (selector, NULL_TREE);
28935 maybe_unary_selector_p = false;
28936 cp_parser_require (parser, CPP_COLON, RT_COLON);
28937 arg = cp_parser_assignment_expression (parser);
28939 sel_args
28940 = chainon (sel_args,
28941 build_tree_list (selector, arg));
28943 token = cp_lexer_peek_token (parser->lexer);
28946 /* Handle non-selector arguments, if any. */
28947 while (token->type == CPP_COMMA)
28949 tree arg;
28951 cp_lexer_consume_token (parser->lexer);
28952 arg = cp_parser_assignment_expression (parser);
28954 addl_args
28955 = chainon (addl_args,
28956 build_tree_list (NULL_TREE, arg));
28958 token = cp_lexer_peek_token (parser->lexer);
28961 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28963 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28964 return build_tree_list (error_mark_node, error_mark_node);
28967 return build_tree_list (sel_args, addl_args);
28970 /* Parse an Objective-C encode expression.
28972 objc-encode-expression:
28973 @encode objc-typename
28975 Returns an encoded representation of the type argument. */
28977 static cp_expr
28978 cp_parser_objc_encode_expression (cp_parser* parser)
28980 tree type;
28981 cp_token *token;
28982 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28984 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28985 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28986 token = cp_lexer_peek_token (parser->lexer);
28987 type = complete_type (cp_parser_type_id (parser));
28988 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28990 if (!type)
28992 error_at (token->location,
28993 "%<@encode%> must specify a type as an argument");
28994 return error_mark_node;
28997 /* This happens if we find @encode(T) (where T is a template
28998 typename or something dependent on a template typename) when
28999 parsing a template. In that case, we can't compile it
29000 immediately, but we rather create an AT_ENCODE_EXPR which will
29001 need to be instantiated when the template is used.
29003 if (dependent_type_p (type))
29005 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29006 TREE_READONLY (value) = 1;
29007 return value;
29011 /* Build a location of the form:
29012 @encode(int)
29013 ^~~~~~~~~~~~
29014 with caret==start at the @ token, finishing at the close paren. */
29015 location_t combined_loc
29016 = make_location (start_loc, start_loc,
29017 cp_lexer_previous_token (parser->lexer)->location);
29019 return cp_expr (objc_build_encode_expr (type), combined_loc);
29022 /* Parse an Objective-C @defs expression. */
29024 static tree
29025 cp_parser_objc_defs_expression (cp_parser *parser)
29027 tree name;
29029 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29030 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29031 name = cp_parser_identifier (parser);
29032 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29034 return objc_get_class_ivars (name);
29037 /* Parse an Objective-C protocol expression.
29039 objc-protocol-expression:
29040 @protocol ( identifier )
29042 Returns a representation of the protocol expression. */
29044 static tree
29045 cp_parser_objc_protocol_expression (cp_parser* parser)
29047 tree proto;
29048 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29050 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29051 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29052 proto = cp_parser_identifier (parser);
29053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29055 /* Build a location of the form:
29056 @protocol(prot)
29057 ^~~~~~~~~~~~~~~
29058 with caret==start at the @ token, finishing at the close paren. */
29059 location_t combined_loc
29060 = make_location (start_loc, start_loc,
29061 cp_lexer_previous_token (parser->lexer)->location);
29062 tree result = objc_build_protocol_expr (proto);
29063 protected_set_expr_location (result, combined_loc);
29064 return result;
29067 /* Parse an Objective-C selector expression.
29069 objc-selector-expression:
29070 @selector ( objc-method-signature )
29072 objc-method-signature:
29073 objc-selector
29074 objc-selector-seq
29076 objc-selector-seq:
29077 objc-selector :
29078 objc-selector-seq objc-selector :
29080 Returns a representation of the method selector. */
29082 static tree
29083 cp_parser_objc_selector_expression (cp_parser* parser)
29085 tree sel_seq = NULL_TREE;
29086 bool maybe_unary_selector_p = true;
29087 cp_token *token;
29088 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29090 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29091 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29092 token = cp_lexer_peek_token (parser->lexer);
29094 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29095 || token->type == CPP_SCOPE)
29097 tree selector = NULL_TREE;
29099 if (token->type != CPP_COLON
29100 || token->type == CPP_SCOPE)
29101 selector = cp_parser_objc_selector (parser);
29103 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29104 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29106 /* Detect if we have a unary selector. */
29107 if (maybe_unary_selector_p)
29109 sel_seq = selector;
29110 goto finish_selector;
29112 else
29114 cp_parser_error (parser, "expected %<:%>");
29117 maybe_unary_selector_p = false;
29118 token = cp_lexer_consume_token (parser->lexer);
29120 if (token->type == CPP_SCOPE)
29122 sel_seq
29123 = chainon (sel_seq,
29124 build_tree_list (selector, NULL_TREE));
29125 sel_seq
29126 = chainon (sel_seq,
29127 build_tree_list (NULL_TREE, NULL_TREE));
29129 else
29130 sel_seq
29131 = chainon (sel_seq,
29132 build_tree_list (selector, NULL_TREE));
29134 token = cp_lexer_peek_token (parser->lexer);
29137 finish_selector:
29138 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29141 /* Build a location of the form:
29142 @selector(func)
29143 ^~~~~~~~~~~~~~~
29144 with caret==start at the @ token, finishing at the close paren. */
29145 location_t combined_loc
29146 = make_location (loc, loc,
29147 cp_lexer_previous_token (parser->lexer)->location);
29148 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29149 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29150 protected_set_expr_location (result, combined_loc);
29151 return result;
29154 /* Parse a list of identifiers.
29156 objc-identifier-list:
29157 identifier
29158 objc-identifier-list , identifier
29160 Returns a TREE_LIST of identifier nodes. */
29162 static tree
29163 cp_parser_objc_identifier_list (cp_parser* parser)
29165 tree identifier;
29166 tree list;
29167 cp_token *sep;
29169 identifier = cp_parser_identifier (parser);
29170 if (identifier == error_mark_node)
29171 return error_mark_node;
29173 list = build_tree_list (NULL_TREE, identifier);
29174 sep = cp_lexer_peek_token (parser->lexer);
29176 while (sep->type == CPP_COMMA)
29178 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29179 identifier = cp_parser_identifier (parser);
29180 if (identifier == error_mark_node)
29181 return list;
29183 list = chainon (list, build_tree_list (NULL_TREE,
29184 identifier));
29185 sep = cp_lexer_peek_token (parser->lexer);
29188 return list;
29191 /* Parse an Objective-C alias declaration.
29193 objc-alias-declaration:
29194 @compatibility_alias identifier identifier ;
29196 This function registers the alias mapping with the Objective-C front end.
29197 It returns nothing. */
29199 static void
29200 cp_parser_objc_alias_declaration (cp_parser* parser)
29202 tree alias, orig;
29204 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29205 alias = cp_parser_identifier (parser);
29206 orig = cp_parser_identifier (parser);
29207 objc_declare_alias (alias, orig);
29208 cp_parser_consume_semicolon_at_end_of_statement (parser);
29211 /* Parse an Objective-C class forward-declaration.
29213 objc-class-declaration:
29214 @class objc-identifier-list ;
29216 The function registers the forward declarations with the Objective-C
29217 front end. It returns nothing. */
29219 static void
29220 cp_parser_objc_class_declaration (cp_parser* parser)
29222 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29223 while (true)
29225 tree id;
29227 id = cp_parser_identifier (parser);
29228 if (id == error_mark_node)
29229 break;
29231 objc_declare_class (id);
29233 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29234 cp_lexer_consume_token (parser->lexer);
29235 else
29236 break;
29238 cp_parser_consume_semicolon_at_end_of_statement (parser);
29241 /* Parse a list of Objective-C protocol references.
29243 objc-protocol-refs-opt:
29244 objc-protocol-refs [opt]
29246 objc-protocol-refs:
29247 < objc-identifier-list >
29249 Returns a TREE_LIST of identifiers, if any. */
29251 static tree
29252 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29254 tree protorefs = NULL_TREE;
29256 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29258 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29259 protorefs = cp_parser_objc_identifier_list (parser);
29260 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29263 return protorefs;
29266 /* Parse a Objective-C visibility specification. */
29268 static void
29269 cp_parser_objc_visibility_spec (cp_parser* parser)
29271 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29273 switch (vis->keyword)
29275 case RID_AT_PRIVATE:
29276 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29277 break;
29278 case RID_AT_PROTECTED:
29279 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29280 break;
29281 case RID_AT_PUBLIC:
29282 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29283 break;
29284 case RID_AT_PACKAGE:
29285 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29286 break;
29287 default:
29288 return;
29291 /* Eat '@private'/'@protected'/'@public'. */
29292 cp_lexer_consume_token (parser->lexer);
29295 /* Parse an Objective-C method type. Return 'true' if it is a class
29296 (+) method, and 'false' if it is an instance (-) method. */
29298 static inline bool
29299 cp_parser_objc_method_type (cp_parser* parser)
29301 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29302 return true;
29303 else
29304 return false;
29307 /* Parse an Objective-C protocol qualifier. */
29309 static tree
29310 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29312 tree quals = NULL_TREE, node;
29313 cp_token *token = cp_lexer_peek_token (parser->lexer);
29315 node = token->u.value;
29317 while (node && identifier_p (node)
29318 && (node == ridpointers [(int) RID_IN]
29319 || node == ridpointers [(int) RID_OUT]
29320 || node == ridpointers [(int) RID_INOUT]
29321 || node == ridpointers [(int) RID_BYCOPY]
29322 || node == ridpointers [(int) RID_BYREF]
29323 || node == ridpointers [(int) RID_ONEWAY]))
29325 quals = tree_cons (NULL_TREE, node, quals);
29326 cp_lexer_consume_token (parser->lexer);
29327 token = cp_lexer_peek_token (parser->lexer);
29328 node = token->u.value;
29331 return quals;
29334 /* Parse an Objective-C typename. */
29336 static tree
29337 cp_parser_objc_typename (cp_parser* parser)
29339 tree type_name = NULL_TREE;
29341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29343 tree proto_quals, cp_type = NULL_TREE;
29345 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29346 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29348 /* An ObjC type name may consist of just protocol qualifiers, in which
29349 case the type shall default to 'id'. */
29350 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29352 cp_type = cp_parser_type_id (parser);
29354 /* If the type could not be parsed, an error has already
29355 been produced. For error recovery, behave as if it had
29356 not been specified, which will use the default type
29357 'id'. */
29358 if (cp_type == error_mark_node)
29360 cp_type = NULL_TREE;
29361 /* We need to skip to the closing parenthesis as
29362 cp_parser_type_id() does not seem to do it for
29363 us. */
29364 cp_parser_skip_to_closing_parenthesis (parser,
29365 /*recovering=*/true,
29366 /*or_comma=*/false,
29367 /*consume_paren=*/false);
29371 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29372 type_name = build_tree_list (proto_quals, cp_type);
29375 return type_name;
29378 /* Check to see if TYPE refers to an Objective-C selector name. */
29380 static bool
29381 cp_parser_objc_selector_p (enum cpp_ttype type)
29383 return (type == CPP_NAME || type == CPP_KEYWORD
29384 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29385 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29386 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29387 || type == CPP_XOR || type == CPP_XOR_EQ);
29390 /* Parse an Objective-C selector. */
29392 static tree
29393 cp_parser_objc_selector (cp_parser* parser)
29395 cp_token *token = cp_lexer_consume_token (parser->lexer);
29397 if (!cp_parser_objc_selector_p (token->type))
29399 error_at (token->location, "invalid Objective-C++ selector name");
29400 return error_mark_node;
29403 /* C++ operator names are allowed to appear in ObjC selectors. */
29404 switch (token->type)
29406 case CPP_AND_AND: return get_identifier ("and");
29407 case CPP_AND_EQ: return get_identifier ("and_eq");
29408 case CPP_AND: return get_identifier ("bitand");
29409 case CPP_OR: return get_identifier ("bitor");
29410 case CPP_COMPL: return get_identifier ("compl");
29411 case CPP_NOT: return get_identifier ("not");
29412 case CPP_NOT_EQ: return get_identifier ("not_eq");
29413 case CPP_OR_OR: return get_identifier ("or");
29414 case CPP_OR_EQ: return get_identifier ("or_eq");
29415 case CPP_XOR: return get_identifier ("xor");
29416 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29417 default: return token->u.value;
29421 /* Parse an Objective-C params list. */
29423 static tree
29424 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29426 tree params = NULL_TREE;
29427 bool maybe_unary_selector_p = true;
29428 cp_token *token = cp_lexer_peek_token (parser->lexer);
29430 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29432 tree selector = NULL_TREE, type_name, identifier;
29433 tree parm_attr = NULL_TREE;
29435 if (token->keyword == RID_ATTRIBUTE)
29436 break;
29438 if (token->type != CPP_COLON)
29439 selector = cp_parser_objc_selector (parser);
29441 /* Detect if we have a unary selector. */
29442 if (maybe_unary_selector_p
29443 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29445 params = selector; /* Might be followed by attributes. */
29446 break;
29449 maybe_unary_selector_p = false;
29450 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29452 /* Something went quite wrong. There should be a colon
29453 here, but there is not. Stop parsing parameters. */
29454 break;
29456 type_name = cp_parser_objc_typename (parser);
29457 /* New ObjC allows attributes on parameters too. */
29458 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29459 parm_attr = cp_parser_attributes_opt (parser);
29460 identifier = cp_parser_identifier (parser);
29462 params
29463 = chainon (params,
29464 objc_build_keyword_decl (selector,
29465 type_name,
29466 identifier,
29467 parm_attr));
29469 token = cp_lexer_peek_token (parser->lexer);
29472 if (params == NULL_TREE)
29474 cp_parser_error (parser, "objective-c++ method declaration is expected");
29475 return error_mark_node;
29478 /* We allow tail attributes for the method. */
29479 if (token->keyword == RID_ATTRIBUTE)
29481 *attributes = cp_parser_attributes_opt (parser);
29482 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29483 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29484 return params;
29485 cp_parser_error (parser,
29486 "method attributes must be specified at the end");
29487 return error_mark_node;
29490 if (params == NULL_TREE)
29492 cp_parser_error (parser, "objective-c++ method declaration is expected");
29493 return error_mark_node;
29495 return params;
29498 /* Parse the non-keyword Objective-C params. */
29500 static tree
29501 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29502 tree* attributes)
29504 tree params = make_node (TREE_LIST);
29505 cp_token *token = cp_lexer_peek_token (parser->lexer);
29506 *ellipsisp = false; /* Initially, assume no ellipsis. */
29508 while (token->type == CPP_COMMA)
29510 cp_parameter_declarator *parmdecl;
29511 tree parm;
29513 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29514 token = cp_lexer_peek_token (parser->lexer);
29516 if (token->type == CPP_ELLIPSIS)
29518 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29519 *ellipsisp = true;
29520 token = cp_lexer_peek_token (parser->lexer);
29521 break;
29524 /* TODO: parse attributes for tail parameters. */
29525 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29526 parm = grokdeclarator (parmdecl->declarator,
29527 &parmdecl->decl_specifiers,
29528 PARM, /*initialized=*/0,
29529 /*attrlist=*/NULL);
29531 chainon (params, build_tree_list (NULL_TREE, parm));
29532 token = cp_lexer_peek_token (parser->lexer);
29535 /* We allow tail attributes for the method. */
29536 if (token->keyword == RID_ATTRIBUTE)
29538 if (*attributes == NULL_TREE)
29540 *attributes = cp_parser_attributes_opt (parser);
29541 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29542 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29543 return params;
29545 else
29546 /* We have an error, but parse the attributes, so that we can
29547 carry on. */
29548 *attributes = cp_parser_attributes_opt (parser);
29550 cp_parser_error (parser,
29551 "method attributes must be specified at the end");
29552 return error_mark_node;
29555 return params;
29558 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29560 static void
29561 cp_parser_objc_interstitial_code (cp_parser* parser)
29563 cp_token *token = cp_lexer_peek_token (parser->lexer);
29565 /* If the next token is `extern' and the following token is a string
29566 literal, then we have a linkage specification. */
29567 if (token->keyword == RID_EXTERN
29568 && cp_parser_is_pure_string_literal
29569 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29570 cp_parser_linkage_specification (parser);
29571 /* Handle #pragma, if any. */
29572 else if (token->type == CPP_PRAGMA)
29573 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29574 /* Allow stray semicolons. */
29575 else if (token->type == CPP_SEMICOLON)
29576 cp_lexer_consume_token (parser->lexer);
29577 /* Mark methods as optional or required, when building protocols. */
29578 else if (token->keyword == RID_AT_OPTIONAL)
29580 cp_lexer_consume_token (parser->lexer);
29581 objc_set_method_opt (true);
29583 else if (token->keyword == RID_AT_REQUIRED)
29585 cp_lexer_consume_token (parser->lexer);
29586 objc_set_method_opt (false);
29588 else if (token->keyword == RID_NAMESPACE)
29589 cp_parser_namespace_definition (parser);
29590 /* Other stray characters must generate errors. */
29591 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29593 cp_lexer_consume_token (parser->lexer);
29594 error ("stray %qs between Objective-C++ methods",
29595 token->type == CPP_OPEN_BRACE ? "{" : "}");
29597 /* Finally, try to parse a block-declaration, or a function-definition. */
29598 else
29599 cp_parser_block_declaration (parser, /*statement_p=*/false);
29602 /* Parse a method signature. */
29604 static tree
29605 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29607 tree rettype, kwdparms, optparms;
29608 bool ellipsis = false;
29609 bool is_class_method;
29611 is_class_method = cp_parser_objc_method_type (parser);
29612 rettype = cp_parser_objc_typename (parser);
29613 *attributes = NULL_TREE;
29614 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29615 if (kwdparms == error_mark_node)
29616 return error_mark_node;
29617 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29618 if (optparms == error_mark_node)
29619 return error_mark_node;
29621 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29624 static bool
29625 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29627 tree tattr;
29628 cp_lexer_save_tokens (parser->lexer);
29629 tattr = cp_parser_attributes_opt (parser);
29630 gcc_assert (tattr) ;
29632 /* If the attributes are followed by a method introducer, this is not allowed.
29633 Dump the attributes and flag the situation. */
29634 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29635 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29636 return true;
29638 /* Otherwise, the attributes introduce some interstitial code, possibly so
29639 rewind to allow that check. */
29640 cp_lexer_rollback_tokens (parser->lexer);
29641 return false;
29644 /* Parse an Objective-C method prototype list. */
29646 static void
29647 cp_parser_objc_method_prototype_list (cp_parser* parser)
29649 cp_token *token = cp_lexer_peek_token (parser->lexer);
29651 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29653 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29655 tree attributes, sig;
29656 bool is_class_method;
29657 if (token->type == CPP_PLUS)
29658 is_class_method = true;
29659 else
29660 is_class_method = false;
29661 sig = cp_parser_objc_method_signature (parser, &attributes);
29662 if (sig == error_mark_node)
29664 cp_parser_skip_to_end_of_block_or_statement (parser);
29665 token = cp_lexer_peek_token (parser->lexer);
29666 continue;
29668 objc_add_method_declaration (is_class_method, sig, attributes);
29669 cp_parser_consume_semicolon_at_end_of_statement (parser);
29671 else if (token->keyword == RID_AT_PROPERTY)
29672 cp_parser_objc_at_property_declaration (parser);
29673 else if (token->keyword == RID_ATTRIBUTE
29674 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29675 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29676 OPT_Wattributes,
29677 "prefix attributes are ignored for methods");
29678 else
29679 /* Allow for interspersed non-ObjC++ code. */
29680 cp_parser_objc_interstitial_code (parser);
29682 token = cp_lexer_peek_token (parser->lexer);
29685 if (token->type != CPP_EOF)
29686 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29687 else
29688 cp_parser_error (parser, "expected %<@end%>");
29690 objc_finish_interface ();
29693 /* Parse an Objective-C method definition list. */
29695 static void
29696 cp_parser_objc_method_definition_list (cp_parser* parser)
29698 cp_token *token = cp_lexer_peek_token (parser->lexer);
29700 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29702 tree meth;
29704 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29706 cp_token *ptk;
29707 tree sig, attribute;
29708 bool is_class_method;
29709 if (token->type == CPP_PLUS)
29710 is_class_method = true;
29711 else
29712 is_class_method = false;
29713 push_deferring_access_checks (dk_deferred);
29714 sig = cp_parser_objc_method_signature (parser, &attribute);
29715 if (sig == error_mark_node)
29717 cp_parser_skip_to_end_of_block_or_statement (parser);
29718 token = cp_lexer_peek_token (parser->lexer);
29719 continue;
29721 objc_start_method_definition (is_class_method, sig, attribute,
29722 NULL_TREE);
29724 /* For historical reasons, we accept an optional semicolon. */
29725 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29726 cp_lexer_consume_token (parser->lexer);
29728 ptk = cp_lexer_peek_token (parser->lexer);
29729 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29730 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29732 perform_deferred_access_checks (tf_warning_or_error);
29733 stop_deferring_access_checks ();
29734 meth = cp_parser_function_definition_after_declarator (parser,
29735 false);
29736 pop_deferring_access_checks ();
29737 objc_finish_method_definition (meth);
29740 /* The following case will be removed once @synthesize is
29741 completely implemented. */
29742 else if (token->keyword == RID_AT_PROPERTY)
29743 cp_parser_objc_at_property_declaration (parser);
29744 else if (token->keyword == RID_AT_SYNTHESIZE)
29745 cp_parser_objc_at_synthesize_declaration (parser);
29746 else if (token->keyword == RID_AT_DYNAMIC)
29747 cp_parser_objc_at_dynamic_declaration (parser);
29748 else if (token->keyword == RID_ATTRIBUTE
29749 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29750 warning_at (token->location, OPT_Wattributes,
29751 "prefix attributes are ignored for methods");
29752 else
29753 /* Allow for interspersed non-ObjC++ code. */
29754 cp_parser_objc_interstitial_code (parser);
29756 token = cp_lexer_peek_token (parser->lexer);
29759 if (token->type != CPP_EOF)
29760 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29761 else
29762 cp_parser_error (parser, "expected %<@end%>");
29764 objc_finish_implementation ();
29767 /* Parse Objective-C ivars. */
29769 static void
29770 cp_parser_objc_class_ivars (cp_parser* parser)
29772 cp_token *token = cp_lexer_peek_token (parser->lexer);
29774 if (token->type != CPP_OPEN_BRACE)
29775 return; /* No ivars specified. */
29777 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29778 token = cp_lexer_peek_token (parser->lexer);
29780 while (token->type != CPP_CLOSE_BRACE
29781 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29783 cp_decl_specifier_seq declspecs;
29784 int decl_class_or_enum_p;
29785 tree prefix_attributes;
29787 cp_parser_objc_visibility_spec (parser);
29789 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29790 break;
29792 cp_parser_decl_specifier_seq (parser,
29793 CP_PARSER_FLAGS_OPTIONAL,
29794 &declspecs,
29795 &decl_class_or_enum_p);
29797 /* auto, register, static, extern, mutable. */
29798 if (declspecs.storage_class != sc_none)
29800 cp_parser_error (parser, "invalid type for instance variable");
29801 declspecs.storage_class = sc_none;
29804 /* thread_local. */
29805 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29807 cp_parser_error (parser, "invalid type for instance variable");
29808 declspecs.locations[ds_thread] = 0;
29811 /* typedef. */
29812 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29814 cp_parser_error (parser, "invalid type for instance variable");
29815 declspecs.locations[ds_typedef] = 0;
29818 prefix_attributes = declspecs.attributes;
29819 declspecs.attributes = NULL_TREE;
29821 /* Keep going until we hit the `;' at the end of the
29822 declaration. */
29823 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29825 tree width = NULL_TREE, attributes, first_attribute, decl;
29826 cp_declarator *declarator = NULL;
29827 int ctor_dtor_or_conv_p;
29829 /* Check for a (possibly unnamed) bitfield declaration. */
29830 token = cp_lexer_peek_token (parser->lexer);
29831 if (token->type == CPP_COLON)
29832 goto eat_colon;
29834 if (token->type == CPP_NAME
29835 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29836 == CPP_COLON))
29838 /* Get the name of the bitfield. */
29839 declarator = make_id_declarator (NULL_TREE,
29840 cp_parser_identifier (parser),
29841 sfk_none);
29843 eat_colon:
29844 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29845 /* Get the width of the bitfield. */
29846 width
29847 = cp_parser_constant_expression (parser);
29849 else
29851 /* Parse the declarator. */
29852 declarator
29853 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29854 &ctor_dtor_or_conv_p,
29855 /*parenthesized_p=*/NULL,
29856 /*member_p=*/false,
29857 /*friend_p=*/false);
29860 /* Look for attributes that apply to the ivar. */
29861 attributes = cp_parser_attributes_opt (parser);
29862 /* Remember which attributes are prefix attributes and
29863 which are not. */
29864 first_attribute = attributes;
29865 /* Combine the attributes. */
29866 attributes = chainon (prefix_attributes, attributes);
29868 if (width)
29869 /* Create the bitfield declaration. */
29870 decl = grokbitfield (declarator, &declspecs,
29871 width,
29872 attributes);
29873 else
29874 decl = grokfield (declarator, &declspecs,
29875 NULL_TREE, /*init_const_expr_p=*/false,
29876 NULL_TREE, attributes);
29878 /* Add the instance variable. */
29879 if (decl != error_mark_node && decl != NULL_TREE)
29880 objc_add_instance_variable (decl);
29882 /* Reset PREFIX_ATTRIBUTES. */
29883 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29884 attributes = TREE_CHAIN (attributes);
29885 if (attributes)
29886 TREE_CHAIN (attributes) = NULL_TREE;
29888 token = cp_lexer_peek_token (parser->lexer);
29890 if (token->type == CPP_COMMA)
29892 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29893 continue;
29895 break;
29898 cp_parser_consume_semicolon_at_end_of_statement (parser);
29899 token = cp_lexer_peek_token (parser->lexer);
29902 if (token->keyword == RID_AT_END)
29903 cp_parser_error (parser, "expected %<}%>");
29905 /* Do not consume the RID_AT_END, so it will be read again as terminating
29906 the @interface of @implementation. */
29907 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29908 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29910 /* For historical reasons, we accept an optional semicolon. */
29911 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29912 cp_lexer_consume_token (parser->lexer);
29915 /* Parse an Objective-C protocol declaration. */
29917 static void
29918 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29920 tree proto, protorefs;
29921 cp_token *tok;
29923 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29924 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29926 tok = cp_lexer_peek_token (parser->lexer);
29927 error_at (tok->location, "identifier expected after %<@protocol%>");
29928 cp_parser_consume_semicolon_at_end_of_statement (parser);
29929 return;
29932 /* See if we have a forward declaration or a definition. */
29933 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29935 /* Try a forward declaration first. */
29936 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29938 while (true)
29940 tree id;
29942 id = cp_parser_identifier (parser);
29943 if (id == error_mark_node)
29944 break;
29946 objc_declare_protocol (id, attributes);
29948 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29949 cp_lexer_consume_token (parser->lexer);
29950 else
29951 break;
29953 cp_parser_consume_semicolon_at_end_of_statement (parser);
29956 /* Ok, we got a full-fledged definition (or at least should). */
29957 else
29959 proto = cp_parser_identifier (parser);
29960 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29961 objc_start_protocol (proto, protorefs, attributes);
29962 cp_parser_objc_method_prototype_list (parser);
29966 /* Parse an Objective-C superclass or category. */
29968 static void
29969 cp_parser_objc_superclass_or_category (cp_parser *parser,
29970 bool iface_p,
29971 tree *super,
29972 tree *categ, bool *is_class_extension)
29974 cp_token *next = cp_lexer_peek_token (parser->lexer);
29976 *super = *categ = NULL_TREE;
29977 *is_class_extension = false;
29978 if (next->type == CPP_COLON)
29980 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29981 *super = cp_parser_identifier (parser);
29983 else if (next->type == CPP_OPEN_PAREN)
29985 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29987 /* If there is no category name, and this is an @interface, we
29988 have a class extension. */
29989 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29991 *categ = NULL_TREE;
29992 *is_class_extension = true;
29994 else
29995 *categ = cp_parser_identifier (parser);
29997 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30001 /* Parse an Objective-C class interface. */
30003 static void
30004 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30006 tree name, super, categ, protos;
30007 bool is_class_extension;
30009 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30010 name = cp_parser_identifier (parser);
30011 if (name == error_mark_node)
30013 /* It's hard to recover because even if valid @interface stuff
30014 is to follow, we can't compile it (or validate it) if we
30015 don't even know which class it refers to. Let's assume this
30016 was a stray '@interface' token in the stream and skip it.
30018 return;
30020 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30021 &is_class_extension);
30022 protos = cp_parser_objc_protocol_refs_opt (parser);
30024 /* We have either a class or a category on our hands. */
30025 if (categ || is_class_extension)
30026 objc_start_category_interface (name, categ, protos, attributes);
30027 else
30029 objc_start_class_interface (name, super, protos, attributes);
30030 /* Handle instance variable declarations, if any. */
30031 cp_parser_objc_class_ivars (parser);
30032 objc_continue_interface ();
30035 cp_parser_objc_method_prototype_list (parser);
30038 /* Parse an Objective-C class implementation. */
30040 static void
30041 cp_parser_objc_class_implementation (cp_parser* parser)
30043 tree name, super, categ;
30044 bool is_class_extension;
30046 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30047 name = cp_parser_identifier (parser);
30048 if (name == error_mark_node)
30050 /* It's hard to recover because even if valid @implementation
30051 stuff is to follow, we can't compile it (or validate it) if
30052 we don't even know which class it refers to. Let's assume
30053 this was a stray '@implementation' token in the stream and
30054 skip it.
30056 return;
30058 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30059 &is_class_extension);
30061 /* We have either a class or a category on our hands. */
30062 if (categ)
30063 objc_start_category_implementation (name, categ);
30064 else
30066 objc_start_class_implementation (name, super);
30067 /* Handle instance variable declarations, if any. */
30068 cp_parser_objc_class_ivars (parser);
30069 objc_continue_implementation ();
30072 cp_parser_objc_method_definition_list (parser);
30075 /* Consume the @end token and finish off the implementation. */
30077 static void
30078 cp_parser_objc_end_implementation (cp_parser* parser)
30080 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30081 objc_finish_implementation ();
30084 /* Parse an Objective-C declaration. */
30086 static void
30087 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30089 /* Try to figure out what kind of declaration is present. */
30090 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30092 if (attributes)
30093 switch (kwd->keyword)
30095 case RID_AT_ALIAS:
30096 case RID_AT_CLASS:
30097 case RID_AT_END:
30098 error_at (kwd->location, "attributes may not be specified before"
30099 " the %<@%D%> Objective-C++ keyword",
30100 kwd->u.value);
30101 attributes = NULL;
30102 break;
30103 case RID_AT_IMPLEMENTATION:
30104 warning_at (kwd->location, OPT_Wattributes,
30105 "prefix attributes are ignored before %<@%D%>",
30106 kwd->u.value);
30107 attributes = NULL;
30108 default:
30109 break;
30112 switch (kwd->keyword)
30114 case RID_AT_ALIAS:
30115 cp_parser_objc_alias_declaration (parser);
30116 break;
30117 case RID_AT_CLASS:
30118 cp_parser_objc_class_declaration (parser);
30119 break;
30120 case RID_AT_PROTOCOL:
30121 cp_parser_objc_protocol_declaration (parser, attributes);
30122 break;
30123 case RID_AT_INTERFACE:
30124 cp_parser_objc_class_interface (parser, attributes);
30125 break;
30126 case RID_AT_IMPLEMENTATION:
30127 cp_parser_objc_class_implementation (parser);
30128 break;
30129 case RID_AT_END:
30130 cp_parser_objc_end_implementation (parser);
30131 break;
30132 default:
30133 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30134 kwd->u.value);
30135 cp_parser_skip_to_end_of_block_or_statement (parser);
30139 /* Parse an Objective-C try-catch-finally statement.
30141 objc-try-catch-finally-stmt:
30142 @try compound-statement objc-catch-clause-seq [opt]
30143 objc-finally-clause [opt]
30145 objc-catch-clause-seq:
30146 objc-catch-clause objc-catch-clause-seq [opt]
30148 objc-catch-clause:
30149 @catch ( objc-exception-declaration ) compound-statement
30151 objc-finally-clause:
30152 @finally compound-statement
30154 objc-exception-declaration:
30155 parameter-declaration
30156 '...'
30158 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30160 Returns NULL_TREE.
30162 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30163 for C. Keep them in sync. */
30165 static tree
30166 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30168 location_t location;
30169 tree stmt;
30171 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30172 location = cp_lexer_peek_token (parser->lexer)->location;
30173 objc_maybe_warn_exceptions (location);
30174 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30175 node, lest it get absorbed into the surrounding block. */
30176 stmt = push_stmt_list ();
30177 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30178 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30180 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30182 cp_parameter_declarator *parm;
30183 tree parameter_declaration = error_mark_node;
30184 bool seen_open_paren = false;
30186 cp_lexer_consume_token (parser->lexer);
30187 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30188 seen_open_paren = true;
30189 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30191 /* We have "@catch (...)" (where the '...' are literally
30192 what is in the code). Skip the '...'.
30193 parameter_declaration is set to NULL_TREE, and
30194 objc_being_catch_clauses() knows that that means
30195 '...'. */
30196 cp_lexer_consume_token (parser->lexer);
30197 parameter_declaration = NULL_TREE;
30199 else
30201 /* We have "@catch (NSException *exception)" or something
30202 like that. Parse the parameter declaration. */
30203 parm = cp_parser_parameter_declaration (parser, false, NULL);
30204 if (parm == NULL)
30205 parameter_declaration = error_mark_node;
30206 else
30207 parameter_declaration = grokdeclarator (parm->declarator,
30208 &parm->decl_specifiers,
30209 PARM, /*initialized=*/0,
30210 /*attrlist=*/NULL);
30212 if (seen_open_paren)
30213 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30214 else
30216 /* If there was no open parenthesis, we are recovering from
30217 an error, and we are trying to figure out what mistake
30218 the user has made. */
30220 /* If there is an immediate closing parenthesis, the user
30221 probably forgot the opening one (ie, they typed "@catch
30222 NSException *e)". Parse the closing parenthesis and keep
30223 going. */
30224 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30225 cp_lexer_consume_token (parser->lexer);
30227 /* If these is no immediate closing parenthesis, the user
30228 probably doesn't know that parenthesis are required at
30229 all (ie, they typed "@catch NSException *e"). So, just
30230 forget about the closing parenthesis and keep going. */
30232 objc_begin_catch_clause (parameter_declaration);
30233 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30234 objc_finish_catch_clause ();
30236 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30238 cp_lexer_consume_token (parser->lexer);
30239 location = cp_lexer_peek_token (parser->lexer)->location;
30240 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30241 node, lest it get absorbed into the surrounding block. */
30242 stmt = push_stmt_list ();
30243 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30244 objc_build_finally_clause (location, pop_stmt_list (stmt));
30247 return objc_finish_try_stmt ();
30250 /* Parse an Objective-C synchronized statement.
30252 objc-synchronized-stmt:
30253 @synchronized ( expression ) compound-statement
30255 Returns NULL_TREE. */
30257 static tree
30258 cp_parser_objc_synchronized_statement (cp_parser *parser)
30260 location_t location;
30261 tree lock, stmt;
30263 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30265 location = cp_lexer_peek_token (parser->lexer)->location;
30266 objc_maybe_warn_exceptions (location);
30267 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30268 lock = cp_parser_expression (parser);
30269 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30271 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30272 node, lest it get absorbed into the surrounding block. */
30273 stmt = push_stmt_list ();
30274 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30276 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30279 /* Parse an Objective-C throw statement.
30281 objc-throw-stmt:
30282 @throw assignment-expression [opt] ;
30284 Returns a constructed '@throw' statement. */
30286 static tree
30287 cp_parser_objc_throw_statement (cp_parser *parser)
30289 tree expr = NULL_TREE;
30290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30292 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30294 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30295 expr = cp_parser_expression (parser);
30297 cp_parser_consume_semicolon_at_end_of_statement (parser);
30299 return objc_build_throw_stmt (loc, expr);
30302 /* Parse an Objective-C statement. */
30304 static tree
30305 cp_parser_objc_statement (cp_parser * parser)
30307 /* Try to figure out what kind of declaration is present. */
30308 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30310 switch (kwd->keyword)
30312 case RID_AT_TRY:
30313 return cp_parser_objc_try_catch_finally_statement (parser);
30314 case RID_AT_SYNCHRONIZED:
30315 return cp_parser_objc_synchronized_statement (parser);
30316 case RID_AT_THROW:
30317 return cp_parser_objc_throw_statement (parser);
30318 default:
30319 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30320 kwd->u.value);
30321 cp_parser_skip_to_end_of_block_or_statement (parser);
30324 return error_mark_node;
30327 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30328 look ahead to see if an objc keyword follows the attributes. This
30329 is to detect the use of prefix attributes on ObjC @interface and
30330 @protocol. */
30332 static bool
30333 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30335 cp_lexer_save_tokens (parser->lexer);
30336 *attrib = cp_parser_attributes_opt (parser);
30337 gcc_assert (*attrib);
30338 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30340 cp_lexer_commit_tokens (parser->lexer);
30341 return true;
30343 cp_lexer_rollback_tokens (parser->lexer);
30344 return false;
30347 /* This routine is a minimal replacement for
30348 c_parser_struct_declaration () used when parsing the list of
30349 types/names or ObjC++ properties. For example, when parsing the
30350 code
30352 @property (readonly) int a, b, c;
30354 this function is responsible for parsing "int a, int b, int c" and
30355 returning the declarations as CHAIN of DECLs.
30357 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30358 similar parsing. */
30359 static tree
30360 cp_parser_objc_struct_declaration (cp_parser *parser)
30362 tree decls = NULL_TREE;
30363 cp_decl_specifier_seq declspecs;
30364 int decl_class_or_enum_p;
30365 tree prefix_attributes;
30367 cp_parser_decl_specifier_seq (parser,
30368 CP_PARSER_FLAGS_NONE,
30369 &declspecs,
30370 &decl_class_or_enum_p);
30372 if (declspecs.type == error_mark_node)
30373 return error_mark_node;
30375 /* auto, register, static, extern, mutable. */
30376 if (declspecs.storage_class != sc_none)
30378 cp_parser_error (parser, "invalid type for property");
30379 declspecs.storage_class = sc_none;
30382 /* thread_local. */
30383 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30385 cp_parser_error (parser, "invalid type for property");
30386 declspecs.locations[ds_thread] = 0;
30389 /* typedef. */
30390 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30392 cp_parser_error (parser, "invalid type for property");
30393 declspecs.locations[ds_typedef] = 0;
30396 prefix_attributes = declspecs.attributes;
30397 declspecs.attributes = NULL_TREE;
30399 /* Keep going until we hit the `;' at the end of the declaration. */
30400 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30402 tree attributes, first_attribute, decl;
30403 cp_declarator *declarator;
30404 cp_token *token;
30406 /* Parse the declarator. */
30407 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30408 NULL, NULL, false, false);
30410 /* Look for attributes that apply to the ivar. */
30411 attributes = cp_parser_attributes_opt (parser);
30412 /* Remember which attributes are prefix attributes and
30413 which are not. */
30414 first_attribute = attributes;
30415 /* Combine the attributes. */
30416 attributes = chainon (prefix_attributes, attributes);
30418 decl = grokfield (declarator, &declspecs,
30419 NULL_TREE, /*init_const_expr_p=*/false,
30420 NULL_TREE, attributes);
30422 if (decl == error_mark_node || decl == NULL_TREE)
30423 return error_mark_node;
30425 /* Reset PREFIX_ATTRIBUTES. */
30426 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30427 attributes = TREE_CHAIN (attributes);
30428 if (attributes)
30429 TREE_CHAIN (attributes) = NULL_TREE;
30431 DECL_CHAIN (decl) = decls;
30432 decls = decl;
30434 token = cp_lexer_peek_token (parser->lexer);
30435 if (token->type == CPP_COMMA)
30437 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30438 continue;
30440 else
30441 break;
30443 return decls;
30446 /* Parse an Objective-C @property declaration. The syntax is:
30448 objc-property-declaration:
30449 '@property' objc-property-attributes[opt] struct-declaration ;
30451 objc-property-attributes:
30452 '(' objc-property-attribute-list ')'
30454 objc-property-attribute-list:
30455 objc-property-attribute
30456 objc-property-attribute-list, objc-property-attribute
30458 objc-property-attribute
30459 'getter' = identifier
30460 'setter' = identifier
30461 'readonly'
30462 'readwrite'
30463 'assign'
30464 'retain'
30465 'copy'
30466 'nonatomic'
30468 For example:
30469 @property NSString *name;
30470 @property (readonly) id object;
30471 @property (retain, nonatomic, getter=getTheName) id name;
30472 @property int a, b, c;
30474 PS: This function is identical to
30475 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30476 static void
30477 cp_parser_objc_at_property_declaration (cp_parser *parser)
30479 /* The following variables hold the attributes of the properties as
30480 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30481 seen. When we see an attribute, we set them to 'true' (if they
30482 are boolean properties) or to the identifier (if they have an
30483 argument, ie, for getter and setter). Note that here we only
30484 parse the list of attributes, check the syntax and accumulate the
30485 attributes that we find. objc_add_property_declaration() will
30486 then process the information. */
30487 bool property_assign = false;
30488 bool property_copy = false;
30489 tree property_getter_ident = NULL_TREE;
30490 bool property_nonatomic = false;
30491 bool property_readonly = false;
30492 bool property_readwrite = false;
30493 bool property_retain = false;
30494 tree property_setter_ident = NULL_TREE;
30496 /* 'properties' is the list of properties that we read. Usually a
30497 single one, but maybe more (eg, in "@property int a, b, c;" there
30498 are three). */
30499 tree properties;
30500 location_t loc;
30502 loc = cp_lexer_peek_token (parser->lexer)->location;
30504 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30506 /* Parse the optional attribute list... */
30507 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30509 /* Eat the '('. */
30510 cp_lexer_consume_token (parser->lexer);
30512 while (true)
30514 bool syntax_error = false;
30515 cp_token *token = cp_lexer_peek_token (parser->lexer);
30516 enum rid keyword;
30518 if (token->type != CPP_NAME)
30520 cp_parser_error (parser, "expected identifier");
30521 break;
30523 keyword = C_RID_CODE (token->u.value);
30524 cp_lexer_consume_token (parser->lexer);
30525 switch (keyword)
30527 case RID_ASSIGN: property_assign = true; break;
30528 case RID_COPY: property_copy = true; break;
30529 case RID_NONATOMIC: property_nonatomic = true; break;
30530 case RID_READONLY: property_readonly = true; break;
30531 case RID_READWRITE: property_readwrite = true; break;
30532 case RID_RETAIN: property_retain = true; break;
30534 case RID_GETTER:
30535 case RID_SETTER:
30536 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30538 if (keyword == RID_GETTER)
30539 cp_parser_error (parser,
30540 "missing %<=%> (after %<getter%> attribute)");
30541 else
30542 cp_parser_error (parser,
30543 "missing %<=%> (after %<setter%> attribute)");
30544 syntax_error = true;
30545 break;
30547 cp_lexer_consume_token (parser->lexer); /* eat the = */
30548 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30550 cp_parser_error (parser, "expected identifier");
30551 syntax_error = true;
30552 break;
30554 if (keyword == RID_SETTER)
30556 if (property_setter_ident != NULL_TREE)
30558 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30559 cp_lexer_consume_token (parser->lexer);
30561 else
30562 property_setter_ident = cp_parser_objc_selector (parser);
30563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30564 cp_parser_error (parser, "setter name must terminate with %<:%>");
30565 else
30566 cp_lexer_consume_token (parser->lexer);
30568 else
30570 if (property_getter_ident != NULL_TREE)
30572 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30573 cp_lexer_consume_token (parser->lexer);
30575 else
30576 property_getter_ident = cp_parser_objc_selector (parser);
30578 break;
30579 default:
30580 cp_parser_error (parser, "unknown property attribute");
30581 syntax_error = true;
30582 break;
30585 if (syntax_error)
30586 break;
30588 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30589 cp_lexer_consume_token (parser->lexer);
30590 else
30591 break;
30594 /* FIXME: "@property (setter, assign);" will generate a spurious
30595 "error: expected ‘)’ before ‘,’ token". This is because
30596 cp_parser_require, unlike the C counterpart, will produce an
30597 error even if we are in error recovery. */
30598 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30600 cp_parser_skip_to_closing_parenthesis (parser,
30601 /*recovering=*/true,
30602 /*or_comma=*/false,
30603 /*consume_paren=*/true);
30607 /* ... and the property declaration(s). */
30608 properties = cp_parser_objc_struct_declaration (parser);
30610 if (properties == error_mark_node)
30612 cp_parser_skip_to_end_of_statement (parser);
30613 /* If the next token is now a `;', consume it. */
30614 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30615 cp_lexer_consume_token (parser->lexer);
30616 return;
30619 if (properties == NULL_TREE)
30620 cp_parser_error (parser, "expected identifier");
30621 else
30623 /* Comma-separated properties are chained together in
30624 reverse order; add them one by one. */
30625 properties = nreverse (properties);
30627 for (; properties; properties = TREE_CHAIN (properties))
30628 objc_add_property_declaration (loc, copy_node (properties),
30629 property_readonly, property_readwrite,
30630 property_assign, property_retain,
30631 property_copy, property_nonatomic,
30632 property_getter_ident, property_setter_ident);
30635 cp_parser_consume_semicolon_at_end_of_statement (parser);
30638 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30640 objc-synthesize-declaration:
30641 @synthesize objc-synthesize-identifier-list ;
30643 objc-synthesize-identifier-list:
30644 objc-synthesize-identifier
30645 objc-synthesize-identifier-list, objc-synthesize-identifier
30647 objc-synthesize-identifier
30648 identifier
30649 identifier = identifier
30651 For example:
30652 @synthesize MyProperty;
30653 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30655 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30656 for C. Keep them in sync.
30658 static void
30659 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30661 tree list = NULL_TREE;
30662 location_t loc;
30663 loc = cp_lexer_peek_token (parser->lexer)->location;
30665 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30666 while (true)
30668 tree property, ivar;
30669 property = cp_parser_identifier (parser);
30670 if (property == error_mark_node)
30672 cp_parser_consume_semicolon_at_end_of_statement (parser);
30673 return;
30675 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30677 cp_lexer_consume_token (parser->lexer);
30678 ivar = cp_parser_identifier (parser);
30679 if (ivar == error_mark_node)
30681 cp_parser_consume_semicolon_at_end_of_statement (parser);
30682 return;
30685 else
30686 ivar = NULL_TREE;
30687 list = chainon (list, build_tree_list (ivar, property));
30688 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30689 cp_lexer_consume_token (parser->lexer);
30690 else
30691 break;
30693 cp_parser_consume_semicolon_at_end_of_statement (parser);
30694 objc_add_synthesize_declaration (loc, list);
30697 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30699 objc-dynamic-declaration:
30700 @dynamic identifier-list ;
30702 For example:
30703 @dynamic MyProperty;
30704 @dynamic MyProperty, AnotherProperty;
30706 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30707 for C. Keep them in sync.
30709 static void
30710 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30712 tree list = NULL_TREE;
30713 location_t loc;
30714 loc = cp_lexer_peek_token (parser->lexer)->location;
30716 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30717 while (true)
30719 tree property;
30720 property = cp_parser_identifier (parser);
30721 if (property == error_mark_node)
30723 cp_parser_consume_semicolon_at_end_of_statement (parser);
30724 return;
30726 list = chainon (list, build_tree_list (NULL, property));
30727 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30728 cp_lexer_consume_token (parser->lexer);
30729 else
30730 break;
30732 cp_parser_consume_semicolon_at_end_of_statement (parser);
30733 objc_add_dynamic_declaration (loc, list);
30737 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30739 /* Returns name of the next clause.
30740 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30741 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30742 returned and the token is consumed. */
30744 static pragma_omp_clause
30745 cp_parser_omp_clause_name (cp_parser *parser)
30747 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30750 result = PRAGMA_OACC_CLAUSE_AUTO;
30751 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30752 result = PRAGMA_OMP_CLAUSE_IF;
30753 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30754 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30755 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30756 result = PRAGMA_OACC_CLAUSE_DELETE;
30757 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30758 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30759 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30760 result = PRAGMA_OMP_CLAUSE_FOR;
30761 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30763 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30764 const char *p = IDENTIFIER_POINTER (id);
30766 switch (p[0])
30768 case 'a':
30769 if (!strcmp ("aligned", p))
30770 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30771 else if (!strcmp ("async", p))
30772 result = PRAGMA_OACC_CLAUSE_ASYNC;
30773 break;
30774 case 'c':
30775 if (!strcmp ("collapse", p))
30776 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30777 else if (!strcmp ("copy", p))
30778 result = PRAGMA_OACC_CLAUSE_COPY;
30779 else if (!strcmp ("copyin", p))
30780 result = PRAGMA_OMP_CLAUSE_COPYIN;
30781 else if (!strcmp ("copyout", p))
30782 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30783 else if (!strcmp ("copyprivate", p))
30784 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30785 else if (!strcmp ("create", p))
30786 result = PRAGMA_OACC_CLAUSE_CREATE;
30787 break;
30788 case 'd':
30789 if (!strcmp ("defaultmap", p))
30790 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30791 else if (!strcmp ("depend", p))
30792 result = PRAGMA_OMP_CLAUSE_DEPEND;
30793 else if (!strcmp ("device", p))
30794 result = PRAGMA_OMP_CLAUSE_DEVICE;
30795 else if (!strcmp ("deviceptr", p))
30796 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30797 else if (!strcmp ("device_resident", p))
30798 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30799 else if (!strcmp ("dist_schedule", p))
30800 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30801 break;
30802 case 'f':
30803 if (!strcmp ("final", p))
30804 result = PRAGMA_OMP_CLAUSE_FINAL;
30805 else if (!strcmp ("firstprivate", p))
30806 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30807 else if (!strcmp ("from", p))
30808 result = PRAGMA_OMP_CLAUSE_FROM;
30809 break;
30810 case 'g':
30811 if (!strcmp ("gang", p))
30812 result = PRAGMA_OACC_CLAUSE_GANG;
30813 else if (!strcmp ("grainsize", p))
30814 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30815 break;
30816 case 'h':
30817 if (!strcmp ("hint", p))
30818 result = PRAGMA_OMP_CLAUSE_HINT;
30819 else if (!strcmp ("host", p))
30820 result = PRAGMA_OACC_CLAUSE_HOST;
30821 break;
30822 case 'i':
30823 if (!strcmp ("inbranch", p))
30824 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30825 else if (!strcmp ("independent", p))
30826 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30827 else if (!strcmp ("is_device_ptr", p))
30828 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30829 break;
30830 case 'l':
30831 if (!strcmp ("lastprivate", p))
30832 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30833 else if (!strcmp ("linear", p))
30834 result = PRAGMA_OMP_CLAUSE_LINEAR;
30835 else if (!strcmp ("link", p))
30836 result = PRAGMA_OMP_CLAUSE_LINK;
30837 break;
30838 case 'm':
30839 if (!strcmp ("map", p))
30840 result = PRAGMA_OMP_CLAUSE_MAP;
30841 else if (!strcmp ("mergeable", p))
30842 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30843 else if (flag_cilkplus && !strcmp ("mask", p))
30844 result = PRAGMA_CILK_CLAUSE_MASK;
30845 break;
30846 case 'n':
30847 if (!strcmp ("nogroup", p))
30848 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30849 else if (!strcmp ("notinbranch", p))
30850 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30851 else if (!strcmp ("nowait", p))
30852 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30853 else if (flag_cilkplus && !strcmp ("nomask", p))
30854 result = PRAGMA_CILK_CLAUSE_NOMASK;
30855 else if (!strcmp ("num_gangs", p))
30856 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30857 else if (!strcmp ("num_tasks", p))
30858 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30859 else if (!strcmp ("num_teams", p))
30860 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30861 else if (!strcmp ("num_threads", p))
30862 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30863 else if (!strcmp ("num_workers", p))
30864 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30865 break;
30866 case 'o':
30867 if (!strcmp ("ordered", p))
30868 result = PRAGMA_OMP_CLAUSE_ORDERED;
30869 break;
30870 case 'p':
30871 if (!strcmp ("parallel", p))
30872 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30873 else if (!strcmp ("present", p))
30874 result = PRAGMA_OACC_CLAUSE_PRESENT;
30875 else if (!strcmp ("present_or_copy", p)
30876 || !strcmp ("pcopy", p))
30877 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30878 else if (!strcmp ("present_or_copyin", p)
30879 || !strcmp ("pcopyin", p))
30880 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30881 else if (!strcmp ("present_or_copyout", p)
30882 || !strcmp ("pcopyout", p))
30883 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30884 else if (!strcmp ("present_or_create", p)
30885 || !strcmp ("pcreate", p))
30886 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30887 else if (!strcmp ("priority", p))
30888 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30889 else if (!strcmp ("proc_bind", p))
30890 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30891 break;
30892 case 'r':
30893 if (!strcmp ("reduction", p))
30894 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30895 break;
30896 case 's':
30897 if (!strcmp ("safelen", p))
30898 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30899 else if (!strcmp ("schedule", p))
30900 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30901 else if (!strcmp ("sections", p))
30902 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30903 else if (!strcmp ("self", p))
30904 result = PRAGMA_OACC_CLAUSE_SELF;
30905 else if (!strcmp ("seq", p))
30906 result = PRAGMA_OACC_CLAUSE_SEQ;
30907 else if (!strcmp ("shared", p))
30908 result = PRAGMA_OMP_CLAUSE_SHARED;
30909 else if (!strcmp ("simd", p))
30910 result = PRAGMA_OMP_CLAUSE_SIMD;
30911 else if (!strcmp ("simdlen", p))
30912 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30913 break;
30914 case 't':
30915 if (!strcmp ("taskgroup", p))
30916 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30917 else if (!strcmp ("thread_limit", p))
30918 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30919 else if (!strcmp ("threads", p))
30920 result = PRAGMA_OMP_CLAUSE_THREADS;
30921 else if (!strcmp ("tile", p))
30922 result = PRAGMA_OACC_CLAUSE_TILE;
30923 else if (!strcmp ("to", p))
30924 result = PRAGMA_OMP_CLAUSE_TO;
30925 break;
30926 case 'u':
30927 if (!strcmp ("uniform", p))
30928 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30929 else if (!strcmp ("untied", p))
30930 result = PRAGMA_OMP_CLAUSE_UNTIED;
30931 else if (!strcmp ("use_device", p))
30932 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30933 else if (!strcmp ("use_device_ptr", p))
30934 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30935 break;
30936 case 'v':
30937 if (!strcmp ("vector", p))
30938 result = PRAGMA_OACC_CLAUSE_VECTOR;
30939 else if (!strcmp ("vector_length", p))
30940 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30941 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30942 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30943 break;
30944 case 'w':
30945 if (!strcmp ("wait", p))
30946 result = PRAGMA_OACC_CLAUSE_WAIT;
30947 else if (!strcmp ("worker", p))
30948 result = PRAGMA_OACC_CLAUSE_WORKER;
30949 break;
30953 if (result != PRAGMA_OMP_CLAUSE_NONE)
30954 cp_lexer_consume_token (parser->lexer);
30956 return result;
30959 /* Validate that a clause of the given type does not already exist. */
30961 static void
30962 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30963 const char *name, location_t location)
30965 tree c;
30967 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30968 if (OMP_CLAUSE_CODE (c) == code)
30970 error_at (location, "too many %qs clauses", name);
30971 break;
30975 /* OpenMP 2.5:
30976 variable-list:
30977 identifier
30978 variable-list , identifier
30980 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30981 colon). An opening parenthesis will have been consumed by the caller.
30983 If KIND is nonzero, create the appropriate node and install the decl
30984 in OMP_CLAUSE_DECL and add the node to the head of the list.
30986 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30987 return the list created.
30989 COLON can be NULL if only closing parenthesis should end the list,
30990 or pointer to bool which will receive false if the list is terminated
30991 by closing parenthesis or true if the list is terminated by colon. */
30993 static tree
30994 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30995 tree list, bool *colon)
30997 cp_token *token;
30998 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30999 if (colon)
31001 parser->colon_corrects_to_scope_p = false;
31002 *colon = false;
31004 while (1)
31006 tree name, decl;
31008 token = cp_lexer_peek_token (parser->lexer);
31009 if (kind != 0
31010 && current_class_ptr
31011 && cp_parser_is_keyword (token, RID_THIS))
31013 decl = finish_this_expr ();
31014 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31015 || CONVERT_EXPR_P (decl))
31016 decl = TREE_OPERAND (decl, 0);
31017 cp_lexer_consume_token (parser->lexer);
31019 else
31021 name = cp_parser_id_expression (parser, /*template_p=*/false,
31022 /*check_dependency_p=*/true,
31023 /*template_p=*/NULL,
31024 /*declarator_p=*/false,
31025 /*optional_p=*/false);
31026 if (name == error_mark_node)
31027 goto skip_comma;
31029 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31030 if (decl == error_mark_node)
31031 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31032 token->location);
31034 if (decl == error_mark_node)
31036 else if (kind != 0)
31038 switch (kind)
31040 case OMP_CLAUSE__CACHE_:
31041 /* The OpenACC cache directive explicitly only allows "array
31042 elements or subarrays". */
31043 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31045 error_at (token->location, "expected %<[%>");
31046 decl = error_mark_node;
31047 break;
31049 /* FALLTHROUGH. */
31050 case OMP_CLAUSE_MAP:
31051 case OMP_CLAUSE_FROM:
31052 case OMP_CLAUSE_TO:
31053 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31055 location_t loc
31056 = cp_lexer_peek_token (parser->lexer)->location;
31057 cp_id_kind idk = CP_ID_KIND_NONE;
31058 cp_lexer_consume_token (parser->lexer);
31059 decl = convert_from_reference (decl);
31060 decl
31061 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31062 decl, false,
31063 &idk, loc);
31065 /* FALLTHROUGH. */
31066 case OMP_CLAUSE_DEPEND:
31067 case OMP_CLAUSE_REDUCTION:
31068 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31070 tree low_bound = NULL_TREE, length = NULL_TREE;
31072 parser->colon_corrects_to_scope_p = false;
31073 cp_lexer_consume_token (parser->lexer);
31074 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31075 low_bound = cp_parser_expression (parser);
31076 if (!colon)
31077 parser->colon_corrects_to_scope_p
31078 = saved_colon_corrects_to_scope_p;
31079 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31080 length = integer_one_node;
31081 else
31083 /* Look for `:'. */
31084 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31085 goto skip_comma;
31086 if (!cp_lexer_next_token_is (parser->lexer,
31087 CPP_CLOSE_SQUARE))
31088 length = cp_parser_expression (parser);
31090 /* Look for the closing `]'. */
31091 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31092 RT_CLOSE_SQUARE))
31093 goto skip_comma;
31095 decl = tree_cons (low_bound, length, decl);
31097 break;
31098 default:
31099 break;
31102 tree u = build_omp_clause (token->location, kind);
31103 OMP_CLAUSE_DECL (u) = decl;
31104 OMP_CLAUSE_CHAIN (u) = list;
31105 list = u;
31107 else
31108 list = tree_cons (decl, NULL_TREE, list);
31110 get_comma:
31111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31112 break;
31113 cp_lexer_consume_token (parser->lexer);
31116 if (colon)
31117 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31119 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31121 *colon = true;
31122 cp_parser_require (parser, CPP_COLON, RT_COLON);
31123 return list;
31126 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31128 int ending;
31130 /* Try to resync to an unnested comma. Copied from
31131 cp_parser_parenthesized_expression_list. */
31132 skip_comma:
31133 if (colon)
31134 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31135 ending = cp_parser_skip_to_closing_parenthesis (parser,
31136 /*recovering=*/true,
31137 /*or_comma=*/true,
31138 /*consume_paren=*/true);
31139 if (ending < 0)
31140 goto get_comma;
31143 return list;
31146 /* Similarly, but expect leading and trailing parenthesis. This is a very
31147 common case for omp clauses. */
31149 static tree
31150 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31152 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31153 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31154 return list;
31157 /* OpenACC 2.0:
31158 copy ( variable-list )
31159 copyin ( variable-list )
31160 copyout ( variable-list )
31161 create ( variable-list )
31162 delete ( variable-list )
31163 present ( variable-list )
31164 present_or_copy ( variable-list )
31165 pcopy ( variable-list )
31166 present_or_copyin ( variable-list )
31167 pcopyin ( variable-list )
31168 present_or_copyout ( variable-list )
31169 pcopyout ( variable-list )
31170 present_or_create ( variable-list )
31171 pcreate ( variable-list ) */
31173 static tree
31174 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31175 tree list)
31177 enum gomp_map_kind kind;
31178 switch (c_kind)
31180 case PRAGMA_OACC_CLAUSE_COPY:
31181 kind = GOMP_MAP_FORCE_TOFROM;
31182 break;
31183 case PRAGMA_OACC_CLAUSE_COPYIN:
31184 kind = GOMP_MAP_FORCE_TO;
31185 break;
31186 case PRAGMA_OACC_CLAUSE_COPYOUT:
31187 kind = GOMP_MAP_FORCE_FROM;
31188 break;
31189 case PRAGMA_OACC_CLAUSE_CREATE:
31190 kind = GOMP_MAP_FORCE_ALLOC;
31191 break;
31192 case PRAGMA_OACC_CLAUSE_DELETE:
31193 kind = GOMP_MAP_DELETE;
31194 break;
31195 case PRAGMA_OACC_CLAUSE_DEVICE:
31196 kind = GOMP_MAP_FORCE_TO;
31197 break;
31198 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31199 kind = GOMP_MAP_DEVICE_RESIDENT;
31200 break;
31201 case PRAGMA_OACC_CLAUSE_HOST:
31202 case PRAGMA_OACC_CLAUSE_SELF:
31203 kind = GOMP_MAP_FORCE_FROM;
31204 break;
31205 case PRAGMA_OACC_CLAUSE_LINK:
31206 kind = GOMP_MAP_LINK;
31207 break;
31208 case PRAGMA_OACC_CLAUSE_PRESENT:
31209 kind = GOMP_MAP_FORCE_PRESENT;
31210 break;
31211 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31212 kind = GOMP_MAP_TOFROM;
31213 break;
31214 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31215 kind = GOMP_MAP_TO;
31216 break;
31217 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31218 kind = GOMP_MAP_FROM;
31219 break;
31220 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31221 kind = GOMP_MAP_ALLOC;
31222 break;
31223 default:
31224 gcc_unreachable ();
31226 tree nl, c;
31227 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31229 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31230 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31232 return nl;
31235 /* OpenACC 2.0:
31236 deviceptr ( variable-list ) */
31238 static tree
31239 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31241 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31242 tree vars, t;
31244 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31245 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31246 variable-list must only allow for pointer variables. */
31247 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31248 for (t = vars; t; t = TREE_CHAIN (t))
31250 tree v = TREE_PURPOSE (t);
31251 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31252 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31253 OMP_CLAUSE_DECL (u) = v;
31254 OMP_CLAUSE_CHAIN (u) = list;
31255 list = u;
31258 return list;
31261 /* OpenACC 2.0:
31262 auto
31263 independent
31264 nohost
31265 seq */
31267 static tree
31268 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31269 enum omp_clause_code code,
31270 tree list, location_t location)
31272 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31273 tree c = build_omp_clause (location, code);
31274 OMP_CLAUSE_CHAIN (c) = list;
31275 return c;
31278 /* OpenACC:
31279 num_gangs ( expression )
31280 num_workers ( expression )
31281 vector_length ( expression ) */
31283 static tree
31284 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31285 const char *str, tree list)
31287 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31290 return list;
31292 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31294 if (t == error_mark_node
31295 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31297 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31298 /*or_comma=*/false,
31299 /*consume_paren=*/true);
31300 return list;
31303 check_no_duplicate_clause (list, code, str, loc);
31305 tree c = build_omp_clause (loc, code);
31306 OMP_CLAUSE_OPERAND (c, 0) = t;
31307 OMP_CLAUSE_CHAIN (c) = list;
31308 return c;
31311 /* OpenACC:
31313 gang [( gang-arg-list )]
31314 worker [( [num:] int-expr )]
31315 vector [( [length:] int-expr )]
31317 where gang-arg is one of:
31319 [num:] int-expr
31320 static: size-expr
31322 and size-expr may be:
31325 int-expr
31328 static tree
31329 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31330 const char *str, tree list)
31332 const char *id = "num";
31333 cp_lexer *lexer = parser->lexer;
31334 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31335 location_t loc = cp_lexer_peek_token (lexer)->location;
31337 if (kind == OMP_CLAUSE_VECTOR)
31338 id = "length";
31340 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31342 cp_lexer_consume_token (lexer);
31346 cp_token *next = cp_lexer_peek_token (lexer);
31347 int idx = 0;
31349 /* Gang static argument. */
31350 if (kind == OMP_CLAUSE_GANG
31351 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31353 cp_lexer_consume_token (lexer);
31355 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31356 goto cleanup_error;
31358 idx = 1;
31359 if (ops[idx] != NULL)
31361 cp_parser_error (parser, "too many %<static%> arguments");
31362 goto cleanup_error;
31365 /* Check for the '*' argument. */
31366 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31367 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31368 || cp_lexer_nth_token_is (parser->lexer, 2,
31369 CPP_CLOSE_PAREN)))
31371 cp_lexer_consume_token (lexer);
31372 ops[idx] = integer_minus_one_node;
31374 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31376 cp_lexer_consume_token (lexer);
31377 continue;
31379 else break;
31382 /* Worker num: argument and vector length: arguments. */
31383 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31384 && id_equal (next->u.value, id)
31385 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31387 cp_lexer_consume_token (lexer); /* id */
31388 cp_lexer_consume_token (lexer); /* ':' */
31391 /* Now collect the actual argument. */
31392 if (ops[idx] != NULL_TREE)
31394 cp_parser_error (parser, "unexpected argument");
31395 goto cleanup_error;
31398 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31399 false);
31400 if (expr == error_mark_node)
31401 goto cleanup_error;
31403 mark_exp_read (expr);
31404 ops[idx] = expr;
31406 if (kind == OMP_CLAUSE_GANG
31407 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31409 cp_lexer_consume_token (lexer);
31410 continue;
31412 break;
31414 while (1);
31416 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31417 goto cleanup_error;
31420 check_no_duplicate_clause (list, kind, str, loc);
31422 c = build_omp_clause (loc, kind);
31424 if (ops[1])
31425 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31427 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31428 OMP_CLAUSE_CHAIN (c) = list;
31430 return c;
31432 cleanup_error:
31433 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31434 return list;
31437 /* OpenACC 2.0:
31438 tile ( size-expr-list ) */
31440 static tree
31441 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31443 tree c, expr = error_mark_node;
31444 tree tile = NULL_TREE;
31446 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31447 so, but the spec authors never considered such a case and have
31448 differing opinions on what it might mean, including 'not
31449 allowed'.) */
31450 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31451 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31452 clause_loc);
31454 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31455 return list;
31459 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31460 return list;
31462 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31463 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31464 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31466 cp_lexer_consume_token (parser->lexer);
31467 expr = integer_zero_node;
31469 else
31470 expr = cp_parser_constant_expression (parser);
31472 tile = tree_cons (NULL_TREE, expr, tile);
31474 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31476 /* Consume the trailing ')'. */
31477 cp_lexer_consume_token (parser->lexer);
31479 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31480 tile = nreverse (tile);
31481 OMP_CLAUSE_TILE_LIST (c) = tile;
31482 OMP_CLAUSE_CHAIN (c) = list;
31483 return c;
31486 /* OpenACC 2.0
31487 Parse wait clause or directive parameters. */
31489 static tree
31490 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31492 vec<tree, va_gc> *args;
31493 tree t, args_tree;
31495 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31496 /*cast_p=*/false,
31497 /*allow_expansion_p=*/true,
31498 /*non_constant_p=*/NULL);
31500 if (args == NULL || args->length () == 0)
31502 cp_parser_error (parser, "expected integer expression before ')'");
31503 if (args != NULL)
31504 release_tree_vector (args);
31505 return list;
31508 args_tree = build_tree_list_vec (args);
31510 release_tree_vector (args);
31512 for (t = args_tree; t; t = TREE_CHAIN (t))
31514 tree targ = TREE_VALUE (t);
31516 if (targ != error_mark_node)
31518 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31519 error ("%<wait%> expression must be integral");
31520 else
31522 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31524 mark_rvalue_use (targ);
31525 OMP_CLAUSE_DECL (c) = targ;
31526 OMP_CLAUSE_CHAIN (c) = list;
31527 list = c;
31532 return list;
31535 /* OpenACC:
31536 wait ( int-expr-list ) */
31538 static tree
31539 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31541 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31543 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31544 return list;
31546 list = cp_parser_oacc_wait_list (parser, location, list);
31548 return list;
31551 /* OpenMP 3.0:
31552 collapse ( constant-expression ) */
31554 static tree
31555 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31557 tree c, num;
31558 location_t loc;
31559 HOST_WIDE_INT n;
31561 loc = cp_lexer_peek_token (parser->lexer)->location;
31562 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31563 return list;
31565 num = cp_parser_constant_expression (parser);
31567 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31568 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31569 /*or_comma=*/false,
31570 /*consume_paren=*/true);
31572 if (num == error_mark_node)
31573 return list;
31574 num = fold_non_dependent_expr (num);
31575 if (!tree_fits_shwi_p (num)
31576 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31577 || (n = tree_to_shwi (num)) <= 0
31578 || (int) n != n)
31580 error_at (loc, "collapse argument needs positive constant integer expression");
31581 return list;
31584 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31585 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31586 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31587 OMP_CLAUSE_CHAIN (c) = list;
31588 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31590 return c;
31593 /* OpenMP 2.5:
31594 default ( none | shared )
31596 OpenACC:
31597 default ( none | present ) */
31599 static tree
31600 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31601 location_t location, bool is_oacc)
31603 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31604 tree c;
31606 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31607 return list;
31608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31610 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31611 const char *p = IDENTIFIER_POINTER (id);
31613 switch (p[0])
31615 case 'n':
31616 if (strcmp ("none", p) != 0)
31617 goto invalid_kind;
31618 kind = OMP_CLAUSE_DEFAULT_NONE;
31619 break;
31621 case 'p':
31622 if (strcmp ("present", p) != 0 || !is_oacc)
31623 goto invalid_kind;
31624 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31625 break;
31627 case 's':
31628 if (strcmp ("shared", p) != 0 || is_oacc)
31629 goto invalid_kind;
31630 kind = OMP_CLAUSE_DEFAULT_SHARED;
31631 break;
31633 default:
31634 goto invalid_kind;
31637 cp_lexer_consume_token (parser->lexer);
31639 else
31641 invalid_kind:
31642 if (is_oacc)
31643 cp_parser_error (parser, "expected %<none%> or %<present%>");
31644 else
31645 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31648 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31649 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31650 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31651 /*or_comma=*/false,
31652 /*consume_paren=*/true);
31654 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31655 return list;
31657 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31658 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31659 OMP_CLAUSE_CHAIN (c) = list;
31660 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31662 return c;
31665 /* OpenMP 3.1:
31666 final ( expression ) */
31668 static tree
31669 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31671 tree t, c;
31673 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31674 return list;
31676 t = cp_parser_condition (parser);
31678 if (t == error_mark_node
31679 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31680 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31681 /*or_comma=*/false,
31682 /*consume_paren=*/true);
31684 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31686 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31687 OMP_CLAUSE_FINAL_EXPR (c) = t;
31688 OMP_CLAUSE_CHAIN (c) = list;
31690 return c;
31693 /* OpenMP 2.5:
31694 if ( expression )
31696 OpenMP 4.5:
31697 if ( directive-name-modifier : expression )
31699 directive-name-modifier:
31700 parallel | task | taskloop | target data | target | target update
31701 | target enter data | target exit data */
31703 static tree
31704 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31705 bool is_omp)
31707 tree t, c;
31708 enum tree_code if_modifier = ERROR_MARK;
31710 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31711 return list;
31713 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31715 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31716 const char *p = IDENTIFIER_POINTER (id);
31717 int n = 2;
31719 if (strcmp ("parallel", p) == 0)
31720 if_modifier = OMP_PARALLEL;
31721 else if (strcmp ("task", p) == 0)
31722 if_modifier = OMP_TASK;
31723 else if (strcmp ("taskloop", p) == 0)
31724 if_modifier = OMP_TASKLOOP;
31725 else if (strcmp ("target", p) == 0)
31727 if_modifier = OMP_TARGET;
31728 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31730 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31731 p = IDENTIFIER_POINTER (id);
31732 if (strcmp ("data", p) == 0)
31733 if_modifier = OMP_TARGET_DATA;
31734 else if (strcmp ("update", p) == 0)
31735 if_modifier = OMP_TARGET_UPDATE;
31736 else if (strcmp ("enter", p) == 0)
31737 if_modifier = OMP_TARGET_ENTER_DATA;
31738 else if (strcmp ("exit", p) == 0)
31739 if_modifier = OMP_TARGET_EXIT_DATA;
31740 if (if_modifier != OMP_TARGET)
31741 n = 3;
31742 else
31744 location_t loc
31745 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31746 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31747 "or %<exit%>");
31748 if_modifier = ERROR_MARK;
31750 if (if_modifier == OMP_TARGET_ENTER_DATA
31751 || if_modifier == OMP_TARGET_EXIT_DATA)
31753 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31755 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31756 p = IDENTIFIER_POINTER (id);
31757 if (strcmp ("data", p) == 0)
31758 n = 4;
31760 if (n != 4)
31762 location_t loc
31763 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31764 error_at (loc, "expected %<data%>");
31765 if_modifier = ERROR_MARK;
31770 if (if_modifier != ERROR_MARK)
31772 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31774 while (n-- > 0)
31775 cp_lexer_consume_token (parser->lexer);
31777 else
31779 if (n > 2)
31781 location_t loc
31782 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31783 error_at (loc, "expected %<:%>");
31785 if_modifier = ERROR_MARK;
31790 t = cp_parser_condition (parser);
31792 if (t == error_mark_node
31793 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31794 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31795 /*or_comma=*/false,
31796 /*consume_paren=*/true);
31798 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31799 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31801 if (if_modifier != ERROR_MARK
31802 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31804 const char *p = NULL;
31805 switch (if_modifier)
31807 case OMP_PARALLEL: p = "parallel"; break;
31808 case OMP_TASK: p = "task"; break;
31809 case OMP_TASKLOOP: p = "taskloop"; break;
31810 case OMP_TARGET_DATA: p = "target data"; break;
31811 case OMP_TARGET: p = "target"; break;
31812 case OMP_TARGET_UPDATE: p = "target update"; break;
31813 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31814 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31815 default: gcc_unreachable ();
31817 error_at (location, "too many %<if%> clauses with %qs modifier",
31819 return list;
31821 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31823 if (!is_omp)
31824 error_at (location, "too many %<if%> clauses");
31825 else
31826 error_at (location, "too many %<if%> clauses without modifier");
31827 return list;
31829 else if (if_modifier == ERROR_MARK
31830 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31832 error_at (location, "if any %<if%> clause has modifier, then all "
31833 "%<if%> clauses have to use modifier");
31834 return list;
31838 c = build_omp_clause (location, OMP_CLAUSE_IF);
31839 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31840 OMP_CLAUSE_IF_EXPR (c) = t;
31841 OMP_CLAUSE_CHAIN (c) = list;
31843 return c;
31846 /* OpenMP 3.1:
31847 mergeable */
31849 static tree
31850 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31851 tree list, location_t location)
31853 tree c;
31855 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31856 location);
31858 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31859 OMP_CLAUSE_CHAIN (c) = list;
31860 return c;
31863 /* OpenMP 2.5:
31864 nowait */
31866 static tree
31867 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31868 tree list, location_t location)
31870 tree c;
31872 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31874 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31875 OMP_CLAUSE_CHAIN (c) = list;
31876 return c;
31879 /* OpenMP 2.5:
31880 num_threads ( expression ) */
31882 static tree
31883 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31884 location_t location)
31886 tree t, c;
31888 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31889 return list;
31891 t = cp_parser_expression (parser);
31893 if (t == error_mark_node
31894 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31895 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31896 /*or_comma=*/false,
31897 /*consume_paren=*/true);
31899 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31900 "num_threads", location);
31902 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31903 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31904 OMP_CLAUSE_CHAIN (c) = list;
31906 return c;
31909 /* OpenMP 4.5:
31910 num_tasks ( expression ) */
31912 static tree
31913 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31914 location_t location)
31916 tree t, c;
31918 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31919 return list;
31921 t = cp_parser_expression (parser);
31923 if (t == error_mark_node
31924 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31925 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31926 /*or_comma=*/false,
31927 /*consume_paren=*/true);
31929 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31930 "num_tasks", location);
31932 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31933 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31934 OMP_CLAUSE_CHAIN (c) = list;
31936 return c;
31939 /* OpenMP 4.5:
31940 grainsize ( expression ) */
31942 static tree
31943 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31944 location_t location)
31946 tree t, c;
31948 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31949 return list;
31951 t = cp_parser_expression (parser);
31953 if (t == error_mark_node
31954 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31956 /*or_comma=*/false,
31957 /*consume_paren=*/true);
31959 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31960 "grainsize", location);
31962 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31963 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31964 OMP_CLAUSE_CHAIN (c) = list;
31966 return c;
31969 /* OpenMP 4.5:
31970 priority ( expression ) */
31972 static tree
31973 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31974 location_t location)
31976 tree t, c;
31978 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31979 return list;
31981 t = cp_parser_expression (parser);
31983 if (t == error_mark_node
31984 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31985 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31986 /*or_comma=*/false,
31987 /*consume_paren=*/true);
31989 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31990 "priority", location);
31992 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31993 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31994 OMP_CLAUSE_CHAIN (c) = list;
31996 return c;
31999 /* OpenMP 4.5:
32000 hint ( expression ) */
32002 static tree
32003 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32004 location_t location)
32006 tree t, c;
32008 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32009 return list;
32011 t = cp_parser_expression (parser);
32013 if (t == error_mark_node
32014 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32015 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32016 /*or_comma=*/false,
32017 /*consume_paren=*/true);
32019 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32021 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32022 OMP_CLAUSE_HINT_EXPR (c) = t;
32023 OMP_CLAUSE_CHAIN (c) = list;
32025 return c;
32028 /* OpenMP 4.5:
32029 defaultmap ( tofrom : scalar ) */
32031 static tree
32032 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32033 location_t location)
32035 tree c, id;
32036 const char *p;
32038 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32039 return list;
32041 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32043 cp_parser_error (parser, "expected %<tofrom%>");
32044 goto out_err;
32046 id = cp_lexer_peek_token (parser->lexer)->u.value;
32047 p = IDENTIFIER_POINTER (id);
32048 if (strcmp (p, "tofrom") != 0)
32050 cp_parser_error (parser, "expected %<tofrom%>");
32051 goto out_err;
32053 cp_lexer_consume_token (parser->lexer);
32054 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32055 goto out_err;
32057 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32059 cp_parser_error (parser, "expected %<scalar%>");
32060 goto out_err;
32062 id = cp_lexer_peek_token (parser->lexer)->u.value;
32063 p = IDENTIFIER_POINTER (id);
32064 if (strcmp (p, "scalar") != 0)
32066 cp_parser_error (parser, "expected %<scalar%>");
32067 goto out_err;
32069 cp_lexer_consume_token (parser->lexer);
32070 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32071 goto out_err;
32073 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32074 location);
32076 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32077 OMP_CLAUSE_CHAIN (c) = list;
32078 return c;
32080 out_err:
32081 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32082 /*or_comma=*/false,
32083 /*consume_paren=*/true);
32084 return list;
32087 /* OpenMP 2.5:
32088 ordered
32090 OpenMP 4.5:
32091 ordered ( constant-expression ) */
32093 static tree
32094 cp_parser_omp_clause_ordered (cp_parser *parser,
32095 tree list, location_t location)
32097 tree c, num = NULL_TREE;
32098 HOST_WIDE_INT n;
32100 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32101 "ordered", location);
32103 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32105 cp_lexer_consume_token (parser->lexer);
32107 num = cp_parser_constant_expression (parser);
32109 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32110 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32111 /*or_comma=*/false,
32112 /*consume_paren=*/true);
32114 if (num == error_mark_node)
32115 return list;
32116 num = fold_non_dependent_expr (num);
32117 if (!tree_fits_shwi_p (num)
32118 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32119 || (n = tree_to_shwi (num)) <= 0
32120 || (int) n != n)
32122 error_at (location,
32123 "ordered argument needs positive constant integer "
32124 "expression");
32125 return list;
32129 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32130 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32131 OMP_CLAUSE_CHAIN (c) = list;
32132 return c;
32135 /* OpenMP 2.5:
32136 reduction ( reduction-operator : variable-list )
32138 reduction-operator:
32139 One of: + * - & ^ | && ||
32141 OpenMP 3.1:
32143 reduction-operator:
32144 One of: + * - & ^ | && || min max
32146 OpenMP 4.0:
32148 reduction-operator:
32149 One of: + * - & ^ | && ||
32150 id-expression */
32152 static tree
32153 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32155 enum tree_code code = ERROR_MARK;
32156 tree nlist, c, id = NULL_TREE;
32158 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32159 return list;
32161 switch (cp_lexer_peek_token (parser->lexer)->type)
32163 case CPP_PLUS: code = PLUS_EXPR; break;
32164 case CPP_MULT: code = MULT_EXPR; break;
32165 case CPP_MINUS: code = MINUS_EXPR; break;
32166 case CPP_AND: code = BIT_AND_EXPR; break;
32167 case CPP_XOR: code = BIT_XOR_EXPR; break;
32168 case CPP_OR: code = BIT_IOR_EXPR; break;
32169 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32170 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32171 default: break;
32174 if (code != ERROR_MARK)
32175 cp_lexer_consume_token (parser->lexer);
32176 else
32178 bool saved_colon_corrects_to_scope_p;
32179 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32180 parser->colon_corrects_to_scope_p = false;
32181 id = cp_parser_id_expression (parser, /*template_p=*/false,
32182 /*check_dependency_p=*/true,
32183 /*template_p=*/NULL,
32184 /*declarator_p=*/false,
32185 /*optional_p=*/false);
32186 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32187 if (identifier_p (id))
32189 const char *p = IDENTIFIER_POINTER (id);
32191 if (strcmp (p, "min") == 0)
32192 code = MIN_EXPR;
32193 else if (strcmp (p, "max") == 0)
32194 code = MAX_EXPR;
32195 else if (id == cp_operator_id (PLUS_EXPR))
32196 code = PLUS_EXPR;
32197 else if (id == cp_operator_id (MULT_EXPR))
32198 code = MULT_EXPR;
32199 else if (id == cp_operator_id (MINUS_EXPR))
32200 code = MINUS_EXPR;
32201 else if (id == cp_operator_id (BIT_AND_EXPR))
32202 code = BIT_AND_EXPR;
32203 else if (id == cp_operator_id (BIT_IOR_EXPR))
32204 code = BIT_IOR_EXPR;
32205 else if (id == cp_operator_id (BIT_XOR_EXPR))
32206 code = BIT_XOR_EXPR;
32207 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32208 code = TRUTH_ANDIF_EXPR;
32209 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32210 code = TRUTH_ORIF_EXPR;
32211 id = omp_reduction_id (code, id, NULL_TREE);
32212 tree scope = parser->scope;
32213 if (scope)
32214 id = build_qualified_name (NULL_TREE, scope, id, false);
32215 parser->scope = NULL_TREE;
32216 parser->qualifying_scope = NULL_TREE;
32217 parser->object_scope = NULL_TREE;
32219 else
32221 error ("invalid reduction-identifier");
32222 resync_fail:
32223 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32224 /*or_comma=*/false,
32225 /*consume_paren=*/true);
32226 return list;
32230 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32231 goto resync_fail;
32233 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32234 NULL);
32235 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32237 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32238 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32241 return nlist;
32244 /* OpenMP 2.5:
32245 schedule ( schedule-kind )
32246 schedule ( schedule-kind , expression )
32248 schedule-kind:
32249 static | dynamic | guided | runtime | auto
32251 OpenMP 4.5:
32252 schedule ( schedule-modifier : schedule-kind )
32253 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32255 schedule-modifier:
32256 simd
32257 monotonic
32258 nonmonotonic */
32260 static tree
32261 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32263 tree c, t;
32264 int modifiers = 0, nmodifiers = 0;
32266 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32267 return list;
32269 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32271 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32273 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32274 const char *p = IDENTIFIER_POINTER (id);
32275 if (strcmp ("simd", p) == 0)
32276 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32277 else if (strcmp ("monotonic", p) == 0)
32278 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32279 else if (strcmp ("nonmonotonic", p) == 0)
32280 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32281 else
32282 break;
32283 cp_lexer_consume_token (parser->lexer);
32284 if (nmodifiers++ == 0
32285 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32286 cp_lexer_consume_token (parser->lexer);
32287 else
32289 cp_parser_require (parser, CPP_COLON, RT_COLON);
32290 break;
32294 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32297 const char *p = IDENTIFIER_POINTER (id);
32299 switch (p[0])
32301 case 'd':
32302 if (strcmp ("dynamic", p) != 0)
32303 goto invalid_kind;
32304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32305 break;
32307 case 'g':
32308 if (strcmp ("guided", p) != 0)
32309 goto invalid_kind;
32310 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32311 break;
32313 case 'r':
32314 if (strcmp ("runtime", p) != 0)
32315 goto invalid_kind;
32316 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32317 break;
32319 default:
32320 goto invalid_kind;
32323 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32324 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32325 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32326 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32327 else
32328 goto invalid_kind;
32329 cp_lexer_consume_token (parser->lexer);
32331 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32332 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32333 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32334 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32336 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32337 "specified");
32338 modifiers = 0;
32341 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32343 cp_token *token;
32344 cp_lexer_consume_token (parser->lexer);
32346 token = cp_lexer_peek_token (parser->lexer);
32347 t = cp_parser_assignment_expression (parser);
32349 if (t == error_mark_node)
32350 goto resync_fail;
32351 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32352 error_at (token->location, "schedule %<runtime%> does not take "
32353 "a %<chunk_size%> parameter");
32354 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32355 error_at (token->location, "schedule %<auto%> does not take "
32356 "a %<chunk_size%> parameter");
32357 else
32358 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32361 goto resync_fail;
32363 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32364 goto resync_fail;
32366 OMP_CLAUSE_SCHEDULE_KIND (c)
32367 = (enum omp_clause_schedule_kind)
32368 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32370 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32371 OMP_CLAUSE_CHAIN (c) = list;
32372 return c;
32374 invalid_kind:
32375 cp_parser_error (parser, "invalid schedule kind");
32376 resync_fail:
32377 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32378 /*or_comma=*/false,
32379 /*consume_paren=*/true);
32380 return list;
32383 /* OpenMP 3.0:
32384 untied */
32386 static tree
32387 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32388 tree list, location_t location)
32390 tree c;
32392 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32394 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32395 OMP_CLAUSE_CHAIN (c) = list;
32396 return c;
32399 /* OpenMP 4.0:
32400 inbranch
32401 notinbranch */
32403 static tree
32404 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32405 tree list, location_t location)
32407 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32408 tree c = build_omp_clause (location, code);
32409 OMP_CLAUSE_CHAIN (c) = list;
32410 return c;
32413 /* OpenMP 4.0:
32414 parallel
32416 sections
32417 taskgroup */
32419 static tree
32420 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32421 enum omp_clause_code code,
32422 tree list, location_t location)
32424 tree c = build_omp_clause (location, code);
32425 OMP_CLAUSE_CHAIN (c) = list;
32426 return c;
32429 /* OpenMP 4.5:
32430 nogroup */
32432 static tree
32433 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32434 tree list, location_t location)
32436 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32437 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32438 OMP_CLAUSE_CHAIN (c) = list;
32439 return c;
32442 /* OpenMP 4.5:
32443 simd
32444 threads */
32446 static tree
32447 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32448 enum omp_clause_code code,
32449 tree list, location_t location)
32451 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32452 tree c = build_omp_clause (location, code);
32453 OMP_CLAUSE_CHAIN (c) = list;
32454 return c;
32457 /* OpenMP 4.0:
32458 num_teams ( expression ) */
32460 static tree
32461 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32462 location_t location)
32464 tree t, c;
32466 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32467 return list;
32469 t = cp_parser_expression (parser);
32471 if (t == error_mark_node
32472 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32473 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32474 /*or_comma=*/false,
32475 /*consume_paren=*/true);
32477 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32478 "num_teams", location);
32480 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32481 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32482 OMP_CLAUSE_CHAIN (c) = list;
32484 return c;
32487 /* OpenMP 4.0:
32488 thread_limit ( expression ) */
32490 static tree
32491 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32492 location_t location)
32494 tree t, c;
32496 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32497 return list;
32499 t = cp_parser_expression (parser);
32501 if (t == error_mark_node
32502 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32503 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32504 /*or_comma=*/false,
32505 /*consume_paren=*/true);
32507 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32508 "thread_limit", location);
32510 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32511 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32512 OMP_CLAUSE_CHAIN (c) = list;
32514 return c;
32517 /* OpenMP 4.0:
32518 aligned ( variable-list )
32519 aligned ( variable-list : constant-expression ) */
32521 static tree
32522 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32524 tree nlist, c, alignment = NULL_TREE;
32525 bool colon;
32527 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32528 return list;
32530 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32531 &colon);
32533 if (colon)
32535 alignment = cp_parser_constant_expression (parser);
32537 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32538 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32539 /*or_comma=*/false,
32540 /*consume_paren=*/true);
32542 if (alignment == error_mark_node)
32543 alignment = NULL_TREE;
32546 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32547 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32549 return nlist;
32552 /* OpenMP 4.0:
32553 linear ( variable-list )
32554 linear ( variable-list : expression )
32556 OpenMP 4.5:
32557 linear ( modifier ( variable-list ) )
32558 linear ( modifier ( variable-list ) : expression ) */
32560 static tree
32561 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32562 bool is_cilk_simd_fn, bool declare_simd)
32564 tree nlist, c, step = integer_one_node;
32565 bool colon;
32566 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32568 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32569 return list;
32571 if (!is_cilk_simd_fn
32572 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32574 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32575 const char *p = IDENTIFIER_POINTER (id);
32577 if (strcmp ("ref", p) == 0)
32578 kind = OMP_CLAUSE_LINEAR_REF;
32579 else if (strcmp ("val", p) == 0)
32580 kind = OMP_CLAUSE_LINEAR_VAL;
32581 else if (strcmp ("uval", p) == 0)
32582 kind = OMP_CLAUSE_LINEAR_UVAL;
32583 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32584 cp_lexer_consume_token (parser->lexer);
32585 else
32586 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32589 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32590 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32591 &colon);
32592 else
32594 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32595 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32596 if (colon)
32597 cp_parser_require (parser, CPP_COLON, RT_COLON);
32598 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32599 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32600 /*or_comma=*/false,
32601 /*consume_paren=*/true);
32604 if (colon)
32606 step = NULL_TREE;
32607 if (declare_simd
32608 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32609 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32611 cp_token *token = cp_lexer_peek_token (parser->lexer);
32612 cp_parser_parse_tentatively (parser);
32613 step = cp_parser_id_expression (parser, /*template_p=*/false,
32614 /*check_dependency_p=*/true,
32615 /*template_p=*/NULL,
32616 /*declarator_p=*/false,
32617 /*optional_p=*/false);
32618 if (step != error_mark_node)
32619 step = cp_parser_lookup_name_simple (parser, step, token->location);
32620 if (step == error_mark_node)
32622 step = NULL_TREE;
32623 cp_parser_abort_tentative_parse (parser);
32625 else if (!cp_parser_parse_definitely (parser))
32626 step = NULL_TREE;
32628 if (!step)
32629 step = cp_parser_expression (parser);
32631 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32633 sorry ("using parameters for %<linear%> step is not supported yet");
32634 step = integer_one_node;
32636 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32637 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32638 /*or_comma=*/false,
32639 /*consume_paren=*/true);
32641 if (step == error_mark_node)
32642 return list;
32645 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32647 OMP_CLAUSE_LINEAR_STEP (c) = step;
32648 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32651 return nlist;
32654 /* OpenMP 4.0:
32655 safelen ( constant-expression ) */
32657 static tree
32658 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32659 location_t location)
32661 tree t, c;
32663 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32664 return list;
32666 t = cp_parser_constant_expression (parser);
32668 if (t == error_mark_node
32669 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32670 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32671 /*or_comma=*/false,
32672 /*consume_paren=*/true);
32674 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32676 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32677 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32678 OMP_CLAUSE_CHAIN (c) = list;
32680 return c;
32683 /* OpenMP 4.0:
32684 simdlen ( constant-expression ) */
32686 static tree
32687 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32688 location_t location)
32690 tree t, c;
32692 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32693 return list;
32695 t = cp_parser_constant_expression (parser);
32697 if (t == error_mark_node
32698 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32700 /*or_comma=*/false,
32701 /*consume_paren=*/true);
32703 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32705 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32706 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32707 OMP_CLAUSE_CHAIN (c) = list;
32709 return c;
32712 /* OpenMP 4.5:
32713 vec:
32714 identifier [+/- integer]
32715 vec , identifier [+/- integer]
32718 static tree
32719 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32720 tree list)
32722 tree vec = NULL;
32724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32726 cp_parser_error (parser, "expected identifier");
32727 return list;
32730 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32732 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32733 tree t, identifier = cp_parser_identifier (parser);
32734 tree addend = NULL;
32736 if (identifier == error_mark_node)
32737 t = error_mark_node;
32738 else
32740 t = cp_parser_lookup_name_simple
32741 (parser, identifier,
32742 cp_lexer_peek_token (parser->lexer)->location);
32743 if (t == error_mark_node)
32744 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32745 id_loc);
32748 bool neg = false;
32749 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32750 neg = true;
32751 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32753 addend = integer_zero_node;
32754 goto add_to_vector;
32756 cp_lexer_consume_token (parser->lexer);
32758 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32760 cp_parser_error (parser, "expected integer");
32761 return list;
32764 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32765 if (TREE_CODE (addend) != INTEGER_CST)
32767 cp_parser_error (parser, "expected integer");
32768 return list;
32770 cp_lexer_consume_token (parser->lexer);
32772 add_to_vector:
32773 if (t != error_mark_node)
32775 vec = tree_cons (addend, t, vec);
32776 if (neg)
32777 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32781 break;
32783 cp_lexer_consume_token (parser->lexer);
32786 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32788 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32789 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32790 OMP_CLAUSE_DECL (u) = nreverse (vec);
32791 OMP_CLAUSE_CHAIN (u) = list;
32792 return u;
32794 return list;
32797 /* OpenMP 4.0:
32798 depend ( depend-kind : variable-list )
32800 depend-kind:
32801 in | out | inout
32803 OpenMP 4.5:
32804 depend ( source )
32806 depend ( sink : vec ) */
32808 static tree
32809 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32811 tree nlist, c;
32812 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32814 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32815 return list;
32817 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32819 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32820 const char *p = IDENTIFIER_POINTER (id);
32822 if (strcmp ("in", p) == 0)
32823 kind = OMP_CLAUSE_DEPEND_IN;
32824 else if (strcmp ("inout", p) == 0)
32825 kind = OMP_CLAUSE_DEPEND_INOUT;
32826 else if (strcmp ("out", p) == 0)
32827 kind = OMP_CLAUSE_DEPEND_OUT;
32828 else if (strcmp ("source", p) == 0)
32829 kind = OMP_CLAUSE_DEPEND_SOURCE;
32830 else if (strcmp ("sink", p) == 0)
32831 kind = OMP_CLAUSE_DEPEND_SINK;
32832 else
32833 goto invalid_kind;
32835 else
32836 goto invalid_kind;
32838 cp_lexer_consume_token (parser->lexer);
32840 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32842 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32843 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32844 OMP_CLAUSE_DECL (c) = NULL_TREE;
32845 OMP_CLAUSE_CHAIN (c) = list;
32846 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32847 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32848 /*or_comma=*/false,
32849 /*consume_paren=*/true);
32850 return c;
32853 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32854 goto resync_fail;
32856 if (kind == OMP_CLAUSE_DEPEND_SINK)
32857 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32858 else
32860 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32861 list, NULL);
32863 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32864 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32866 return nlist;
32868 invalid_kind:
32869 cp_parser_error (parser, "invalid depend kind");
32870 resync_fail:
32871 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32872 /*or_comma=*/false,
32873 /*consume_paren=*/true);
32874 return list;
32877 /* OpenMP 4.0:
32878 map ( map-kind : variable-list )
32879 map ( variable-list )
32881 map-kind:
32882 alloc | to | from | tofrom
32884 OpenMP 4.5:
32885 map-kind:
32886 alloc | to | from | tofrom | release | delete
32888 map ( always [,] map-kind: variable-list ) */
32890 static tree
32891 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32893 tree nlist, c;
32894 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32895 bool always = false;
32897 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32898 return list;
32900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32902 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32903 const char *p = IDENTIFIER_POINTER (id);
32905 if (strcmp ("always", p) == 0)
32907 int nth = 2;
32908 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32909 nth++;
32910 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32911 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32912 == RID_DELETE))
32913 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32914 == CPP_COLON))
32916 always = true;
32917 cp_lexer_consume_token (parser->lexer);
32918 if (nth == 3)
32919 cp_lexer_consume_token (parser->lexer);
32924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32925 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32927 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32928 const char *p = IDENTIFIER_POINTER (id);
32930 if (strcmp ("alloc", p) == 0)
32931 kind = GOMP_MAP_ALLOC;
32932 else if (strcmp ("to", p) == 0)
32933 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32934 else if (strcmp ("from", p) == 0)
32935 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32936 else if (strcmp ("tofrom", p) == 0)
32937 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32938 else if (strcmp ("release", p) == 0)
32939 kind = GOMP_MAP_RELEASE;
32940 else
32942 cp_parser_error (parser, "invalid map kind");
32943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32944 /*or_comma=*/false,
32945 /*consume_paren=*/true);
32946 return list;
32948 cp_lexer_consume_token (parser->lexer);
32949 cp_lexer_consume_token (parser->lexer);
32951 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32952 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32954 kind = GOMP_MAP_DELETE;
32955 cp_lexer_consume_token (parser->lexer);
32956 cp_lexer_consume_token (parser->lexer);
32959 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32960 NULL);
32962 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32963 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32965 return nlist;
32968 /* OpenMP 4.0:
32969 device ( expression ) */
32971 static tree
32972 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32973 location_t location)
32975 tree t, c;
32977 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32978 return list;
32980 t = cp_parser_expression (parser);
32982 if (t == error_mark_node
32983 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32985 /*or_comma=*/false,
32986 /*consume_paren=*/true);
32988 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32989 "device", location);
32991 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32992 OMP_CLAUSE_DEVICE_ID (c) = t;
32993 OMP_CLAUSE_CHAIN (c) = list;
32995 return c;
32998 /* OpenMP 4.0:
32999 dist_schedule ( static )
33000 dist_schedule ( static , expression ) */
33002 static tree
33003 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33004 location_t location)
33006 tree c, t;
33008 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33009 return list;
33011 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33013 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33014 goto invalid_kind;
33015 cp_lexer_consume_token (parser->lexer);
33017 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33019 cp_lexer_consume_token (parser->lexer);
33021 t = cp_parser_assignment_expression (parser);
33023 if (t == error_mark_node)
33024 goto resync_fail;
33025 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33027 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33028 goto resync_fail;
33030 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33031 goto resync_fail;
33033 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33034 location);
33035 OMP_CLAUSE_CHAIN (c) = list;
33036 return c;
33038 invalid_kind:
33039 cp_parser_error (parser, "invalid dist_schedule kind");
33040 resync_fail:
33041 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33042 /*or_comma=*/false,
33043 /*consume_paren=*/true);
33044 return list;
33047 /* OpenMP 4.0:
33048 proc_bind ( proc-bind-kind )
33050 proc-bind-kind:
33051 master | close | spread */
33053 static tree
33054 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33055 location_t location)
33057 tree c;
33058 enum omp_clause_proc_bind_kind kind;
33060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33061 return list;
33063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33066 const char *p = IDENTIFIER_POINTER (id);
33068 if (strcmp ("master", p) == 0)
33069 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33070 else if (strcmp ("close", p) == 0)
33071 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33072 else if (strcmp ("spread", p) == 0)
33073 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33074 else
33075 goto invalid_kind;
33077 else
33078 goto invalid_kind;
33080 cp_lexer_consume_token (parser->lexer);
33081 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33082 goto resync_fail;
33084 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33085 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33086 location);
33087 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33088 OMP_CLAUSE_CHAIN (c) = list;
33089 return c;
33091 invalid_kind:
33092 cp_parser_error (parser, "invalid depend kind");
33093 resync_fail:
33094 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33095 /*or_comma=*/false,
33096 /*consume_paren=*/true);
33097 return list;
33100 /* OpenACC:
33101 async [( int-expr )] */
33103 static tree
33104 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33106 tree c, t;
33107 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33109 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33111 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33113 cp_lexer_consume_token (parser->lexer);
33115 t = cp_parser_expression (parser);
33116 if (t == error_mark_node
33117 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33118 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33119 /*or_comma=*/false,
33120 /*consume_paren=*/true);
33123 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33125 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33126 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33127 OMP_CLAUSE_CHAIN (c) = list;
33128 list = c;
33130 return list;
33133 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33134 is a bitmask in MASK. Return the list of clauses found. */
33136 static tree
33137 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33138 const char *where, cp_token *pragma_tok,
33139 bool finish_p = true)
33141 tree clauses = NULL;
33142 bool first = true;
33144 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33146 location_t here;
33147 pragma_omp_clause c_kind;
33148 omp_clause_code code;
33149 const char *c_name;
33150 tree prev = clauses;
33152 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33153 cp_lexer_consume_token (parser->lexer);
33155 here = cp_lexer_peek_token (parser->lexer)->location;
33156 c_kind = cp_parser_omp_clause_name (parser);
33158 switch (c_kind)
33160 case PRAGMA_OACC_CLAUSE_ASYNC:
33161 clauses = cp_parser_oacc_clause_async (parser, clauses);
33162 c_name = "async";
33163 break;
33164 case PRAGMA_OACC_CLAUSE_AUTO:
33165 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33166 clauses, here);
33167 c_name = "auto";
33168 break;
33169 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33170 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33171 c_name = "collapse";
33172 break;
33173 case PRAGMA_OACC_CLAUSE_COPY:
33174 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33175 c_name = "copy";
33176 break;
33177 case PRAGMA_OACC_CLAUSE_COPYIN:
33178 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33179 c_name = "copyin";
33180 break;
33181 case PRAGMA_OACC_CLAUSE_COPYOUT:
33182 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33183 c_name = "copyout";
33184 break;
33185 case PRAGMA_OACC_CLAUSE_CREATE:
33186 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33187 c_name = "create";
33188 break;
33189 case PRAGMA_OACC_CLAUSE_DELETE:
33190 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33191 c_name = "delete";
33192 break;
33193 case PRAGMA_OMP_CLAUSE_DEFAULT:
33194 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33195 c_name = "default";
33196 break;
33197 case PRAGMA_OACC_CLAUSE_DEVICE:
33198 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33199 c_name = "device";
33200 break;
33201 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33202 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33203 c_name = "deviceptr";
33204 break;
33205 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33206 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33207 c_name = "device_resident";
33208 break;
33209 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33210 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33211 clauses);
33212 c_name = "firstprivate";
33213 break;
33214 case PRAGMA_OACC_CLAUSE_GANG:
33215 c_name = "gang";
33216 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33217 c_name, clauses);
33218 break;
33219 case PRAGMA_OACC_CLAUSE_HOST:
33220 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33221 c_name = "host";
33222 break;
33223 case PRAGMA_OACC_CLAUSE_IF:
33224 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33225 c_name = "if";
33226 break;
33227 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33228 clauses = cp_parser_oacc_simple_clause (parser,
33229 OMP_CLAUSE_INDEPENDENT,
33230 clauses, here);
33231 c_name = "independent";
33232 break;
33233 case PRAGMA_OACC_CLAUSE_LINK:
33234 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33235 c_name = "link";
33236 break;
33237 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33238 code = OMP_CLAUSE_NUM_GANGS;
33239 c_name = "num_gangs";
33240 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33241 clauses);
33242 break;
33243 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33244 c_name = "num_workers";
33245 code = OMP_CLAUSE_NUM_WORKERS;
33246 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33247 clauses);
33248 break;
33249 case PRAGMA_OACC_CLAUSE_PRESENT:
33250 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33251 c_name = "present";
33252 break;
33253 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33254 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33255 c_name = "present_or_copy";
33256 break;
33257 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33258 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33259 c_name = "present_or_copyin";
33260 break;
33261 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33262 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33263 c_name = "present_or_copyout";
33264 break;
33265 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33266 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33267 c_name = "present_or_create";
33268 break;
33269 case PRAGMA_OACC_CLAUSE_PRIVATE:
33270 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33271 clauses);
33272 c_name = "private";
33273 break;
33274 case PRAGMA_OACC_CLAUSE_REDUCTION:
33275 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33276 c_name = "reduction";
33277 break;
33278 case PRAGMA_OACC_CLAUSE_SELF:
33279 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33280 c_name = "self";
33281 break;
33282 case PRAGMA_OACC_CLAUSE_SEQ:
33283 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33284 clauses, here);
33285 c_name = "seq";
33286 break;
33287 case PRAGMA_OACC_CLAUSE_TILE:
33288 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33289 c_name = "tile";
33290 break;
33291 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33292 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33293 clauses);
33294 c_name = "use_device";
33295 break;
33296 case PRAGMA_OACC_CLAUSE_VECTOR:
33297 c_name = "vector";
33298 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33299 c_name, clauses);
33300 break;
33301 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33302 c_name = "vector_length";
33303 code = OMP_CLAUSE_VECTOR_LENGTH;
33304 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33305 clauses);
33306 break;
33307 case PRAGMA_OACC_CLAUSE_WAIT:
33308 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33309 c_name = "wait";
33310 break;
33311 case PRAGMA_OACC_CLAUSE_WORKER:
33312 c_name = "worker";
33313 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33314 c_name, clauses);
33315 break;
33316 default:
33317 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33318 goto saw_error;
33321 first = false;
33323 if (((mask >> c_kind) & 1) == 0)
33325 /* Remove the invalid clause(s) from the list to avoid
33326 confusing the rest of the compiler. */
33327 clauses = prev;
33328 error_at (here, "%qs is not valid for %qs", c_name, where);
33332 saw_error:
33333 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33335 if (finish_p)
33336 return finish_omp_clauses (clauses, C_ORT_ACC);
33338 return clauses;
33341 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33342 is a bitmask in MASK. Return the list of clauses found; the result
33343 of clause default goes in *pdefault. */
33345 static tree
33346 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33347 const char *where, cp_token *pragma_tok,
33348 bool finish_p = true)
33350 tree clauses = NULL;
33351 bool first = true;
33352 cp_token *token = NULL;
33354 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33356 pragma_omp_clause c_kind;
33357 const char *c_name;
33358 tree prev = clauses;
33360 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33361 cp_lexer_consume_token (parser->lexer);
33363 token = cp_lexer_peek_token (parser->lexer);
33364 c_kind = cp_parser_omp_clause_name (parser);
33366 switch (c_kind)
33368 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33369 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33370 token->location);
33371 c_name = "collapse";
33372 break;
33373 case PRAGMA_OMP_CLAUSE_COPYIN:
33374 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33375 c_name = "copyin";
33376 break;
33377 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33378 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33379 clauses);
33380 c_name = "copyprivate";
33381 break;
33382 case PRAGMA_OMP_CLAUSE_DEFAULT:
33383 clauses = cp_parser_omp_clause_default (parser, clauses,
33384 token->location, false);
33385 c_name = "default";
33386 break;
33387 case PRAGMA_OMP_CLAUSE_FINAL:
33388 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33389 c_name = "final";
33390 break;
33391 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33392 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33393 clauses);
33394 c_name = "firstprivate";
33395 break;
33396 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33397 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33398 token->location);
33399 c_name = "grainsize";
33400 break;
33401 case PRAGMA_OMP_CLAUSE_HINT:
33402 clauses = cp_parser_omp_clause_hint (parser, clauses,
33403 token->location);
33404 c_name = "hint";
33405 break;
33406 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33407 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33408 token->location);
33409 c_name = "defaultmap";
33410 break;
33411 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33412 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33413 clauses);
33414 c_name = "use_device_ptr";
33415 break;
33416 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33417 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33418 clauses);
33419 c_name = "is_device_ptr";
33420 break;
33421 case PRAGMA_OMP_CLAUSE_IF:
33422 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33423 true);
33424 c_name = "if";
33425 break;
33426 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33428 clauses);
33429 c_name = "lastprivate";
33430 break;
33431 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33432 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33433 token->location);
33434 c_name = "mergeable";
33435 break;
33436 case PRAGMA_OMP_CLAUSE_NOWAIT:
33437 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33438 c_name = "nowait";
33439 break;
33440 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33441 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33442 token->location);
33443 c_name = "num_tasks";
33444 break;
33445 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33446 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33447 token->location);
33448 c_name = "num_threads";
33449 break;
33450 case PRAGMA_OMP_CLAUSE_ORDERED:
33451 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33452 token->location);
33453 c_name = "ordered";
33454 break;
33455 case PRAGMA_OMP_CLAUSE_PRIORITY:
33456 clauses = cp_parser_omp_clause_priority (parser, clauses,
33457 token->location);
33458 c_name = "priority";
33459 break;
33460 case PRAGMA_OMP_CLAUSE_PRIVATE:
33461 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33462 clauses);
33463 c_name = "private";
33464 break;
33465 case PRAGMA_OMP_CLAUSE_REDUCTION:
33466 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33467 c_name = "reduction";
33468 break;
33469 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33470 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33471 token->location);
33472 c_name = "schedule";
33473 break;
33474 case PRAGMA_OMP_CLAUSE_SHARED:
33475 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33476 clauses);
33477 c_name = "shared";
33478 break;
33479 case PRAGMA_OMP_CLAUSE_UNTIED:
33480 clauses = cp_parser_omp_clause_untied (parser, clauses,
33481 token->location);
33482 c_name = "untied";
33483 break;
33484 case PRAGMA_OMP_CLAUSE_INBRANCH:
33485 case PRAGMA_CILK_CLAUSE_MASK:
33486 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33487 clauses, token->location);
33488 c_name = "inbranch";
33489 break;
33490 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33491 case PRAGMA_CILK_CLAUSE_NOMASK:
33492 clauses = cp_parser_omp_clause_branch (parser,
33493 OMP_CLAUSE_NOTINBRANCH,
33494 clauses, token->location);
33495 c_name = "notinbranch";
33496 break;
33497 case PRAGMA_OMP_CLAUSE_PARALLEL:
33498 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33499 clauses, token->location);
33500 c_name = "parallel";
33501 if (!first)
33503 clause_not_first:
33504 error_at (token->location, "%qs must be the first clause of %qs",
33505 c_name, where);
33506 clauses = prev;
33508 break;
33509 case PRAGMA_OMP_CLAUSE_FOR:
33510 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33511 clauses, token->location);
33512 c_name = "for";
33513 if (!first)
33514 goto clause_not_first;
33515 break;
33516 case PRAGMA_OMP_CLAUSE_SECTIONS:
33517 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33518 clauses, token->location);
33519 c_name = "sections";
33520 if (!first)
33521 goto clause_not_first;
33522 break;
33523 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33524 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33525 clauses, token->location);
33526 c_name = "taskgroup";
33527 if (!first)
33528 goto clause_not_first;
33529 break;
33530 case PRAGMA_OMP_CLAUSE_LINK:
33531 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33532 c_name = "to";
33533 break;
33534 case PRAGMA_OMP_CLAUSE_TO:
33535 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33536 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33537 clauses);
33538 else
33539 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33540 c_name = "to";
33541 break;
33542 case PRAGMA_OMP_CLAUSE_FROM:
33543 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33544 c_name = "from";
33545 break;
33546 case PRAGMA_OMP_CLAUSE_UNIFORM:
33547 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33548 clauses);
33549 c_name = "uniform";
33550 break;
33551 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33552 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33553 token->location);
33554 c_name = "num_teams";
33555 break;
33556 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33557 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33558 token->location);
33559 c_name = "thread_limit";
33560 break;
33561 case PRAGMA_OMP_CLAUSE_ALIGNED:
33562 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33563 c_name = "aligned";
33564 break;
33565 case PRAGMA_OMP_CLAUSE_LINEAR:
33567 bool cilk_simd_fn = false, declare_simd = false;
33568 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33569 cilk_simd_fn = true;
33570 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33571 declare_simd = true;
33572 clauses = cp_parser_omp_clause_linear (parser, clauses,
33573 cilk_simd_fn, declare_simd);
33575 c_name = "linear";
33576 break;
33577 case PRAGMA_OMP_CLAUSE_DEPEND:
33578 clauses = cp_parser_omp_clause_depend (parser, clauses,
33579 token->location);
33580 c_name = "depend";
33581 break;
33582 case PRAGMA_OMP_CLAUSE_MAP:
33583 clauses = cp_parser_omp_clause_map (parser, clauses);
33584 c_name = "map";
33585 break;
33586 case PRAGMA_OMP_CLAUSE_DEVICE:
33587 clauses = cp_parser_omp_clause_device (parser, clauses,
33588 token->location);
33589 c_name = "device";
33590 break;
33591 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33592 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33593 token->location);
33594 c_name = "dist_schedule";
33595 break;
33596 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33597 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33598 token->location);
33599 c_name = "proc_bind";
33600 break;
33601 case PRAGMA_OMP_CLAUSE_SAFELEN:
33602 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33603 token->location);
33604 c_name = "safelen";
33605 break;
33606 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33607 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33608 token->location);
33609 c_name = "simdlen";
33610 break;
33611 case PRAGMA_OMP_CLAUSE_NOGROUP:
33612 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33613 token->location);
33614 c_name = "nogroup";
33615 break;
33616 case PRAGMA_OMP_CLAUSE_THREADS:
33617 clauses
33618 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33619 clauses, token->location);
33620 c_name = "threads";
33621 break;
33622 case PRAGMA_OMP_CLAUSE_SIMD:
33623 clauses
33624 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33625 clauses, token->location);
33626 c_name = "simd";
33627 break;
33628 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33629 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33630 c_name = "simdlen";
33631 break;
33632 default:
33633 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33634 goto saw_error;
33637 first = false;
33639 if (((mask >> c_kind) & 1) == 0)
33641 /* Remove the invalid clause(s) from the list to avoid
33642 confusing the rest of the compiler. */
33643 clauses = prev;
33644 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33647 saw_error:
33648 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33649 no reason to skip to the end. */
33650 if (!(flag_cilkplus && pragma_tok == NULL))
33651 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33652 if (finish_p)
33654 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33655 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33656 else
33657 return finish_omp_clauses (clauses, C_ORT_OMP);
33659 return clauses;
33662 /* OpenMP 2.5:
33663 structured-block:
33664 statement
33666 In practice, we're also interested in adding the statement to an
33667 outer node. So it is convenient if we work around the fact that
33668 cp_parser_statement calls add_stmt. */
33670 static unsigned
33671 cp_parser_begin_omp_structured_block (cp_parser *parser)
33673 unsigned save = parser->in_statement;
33675 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33676 This preserves the "not within loop or switch" style error messages
33677 for nonsense cases like
33678 void foo() {
33679 #pragma omp single
33680 break;
33683 if (parser->in_statement)
33684 parser->in_statement = IN_OMP_BLOCK;
33686 return save;
33689 static void
33690 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33692 parser->in_statement = save;
33695 static tree
33696 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33698 tree stmt = begin_omp_structured_block ();
33699 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33701 cp_parser_statement (parser, NULL_TREE, false, if_p);
33703 cp_parser_end_omp_structured_block (parser, save);
33704 return finish_omp_structured_block (stmt);
33707 /* OpenMP 2.5:
33708 # pragma omp atomic new-line
33709 expression-stmt
33711 expression-stmt:
33712 x binop= expr | x++ | ++x | x-- | --x
33713 binop:
33714 +, *, -, /, &, ^, |, <<, >>
33716 where x is an lvalue expression with scalar type.
33718 OpenMP 3.1:
33719 # pragma omp atomic new-line
33720 update-stmt
33722 # pragma omp atomic read new-line
33723 read-stmt
33725 # pragma omp atomic write new-line
33726 write-stmt
33728 # pragma omp atomic update new-line
33729 update-stmt
33731 # pragma omp atomic capture new-line
33732 capture-stmt
33734 # pragma omp atomic capture new-line
33735 capture-block
33737 read-stmt:
33738 v = x
33739 write-stmt:
33740 x = expr
33741 update-stmt:
33742 expression-stmt | x = x binop expr
33743 capture-stmt:
33744 v = expression-stmt
33745 capture-block:
33746 { v = x; update-stmt; } | { update-stmt; v = x; }
33748 OpenMP 4.0:
33749 update-stmt:
33750 expression-stmt | x = x binop expr | x = expr binop x
33751 capture-stmt:
33752 v = update-stmt
33753 capture-block:
33754 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33756 where x and v are lvalue expressions with scalar type. */
33758 static void
33759 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33761 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33762 tree rhs1 = NULL_TREE, orig_lhs;
33763 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33764 bool structured_block = false;
33765 bool seq_cst = false;
33767 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33769 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33770 const char *p = IDENTIFIER_POINTER (id);
33772 if (!strcmp (p, "seq_cst"))
33774 seq_cst = true;
33775 cp_lexer_consume_token (parser->lexer);
33776 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33777 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33778 cp_lexer_consume_token (parser->lexer);
33781 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33783 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33784 const char *p = IDENTIFIER_POINTER (id);
33786 if (!strcmp (p, "read"))
33787 code = OMP_ATOMIC_READ;
33788 else if (!strcmp (p, "write"))
33789 code = NOP_EXPR;
33790 else if (!strcmp (p, "update"))
33791 code = OMP_ATOMIC;
33792 else if (!strcmp (p, "capture"))
33793 code = OMP_ATOMIC_CAPTURE_NEW;
33794 else
33795 p = NULL;
33796 if (p)
33797 cp_lexer_consume_token (parser->lexer);
33799 if (!seq_cst)
33801 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33802 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33803 cp_lexer_consume_token (parser->lexer);
33805 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33807 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33808 const char *p = IDENTIFIER_POINTER (id);
33810 if (!strcmp (p, "seq_cst"))
33812 seq_cst = true;
33813 cp_lexer_consume_token (parser->lexer);
33817 cp_parser_require_pragma_eol (parser, pragma_tok);
33819 switch (code)
33821 case OMP_ATOMIC_READ:
33822 case NOP_EXPR: /* atomic write */
33823 v = cp_parser_unary_expression (parser);
33824 if (v == error_mark_node)
33825 goto saw_error;
33826 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33827 goto saw_error;
33828 if (code == NOP_EXPR)
33829 lhs = cp_parser_expression (parser);
33830 else
33831 lhs = cp_parser_unary_expression (parser);
33832 if (lhs == error_mark_node)
33833 goto saw_error;
33834 if (code == NOP_EXPR)
33836 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33837 opcode. */
33838 code = OMP_ATOMIC;
33839 rhs = lhs;
33840 lhs = v;
33841 v = NULL_TREE;
33843 goto done;
33844 case OMP_ATOMIC_CAPTURE_NEW:
33845 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33847 cp_lexer_consume_token (parser->lexer);
33848 structured_block = true;
33850 else
33852 v = cp_parser_unary_expression (parser);
33853 if (v == error_mark_node)
33854 goto saw_error;
33855 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33856 goto saw_error;
33858 default:
33859 break;
33862 restart:
33863 lhs = cp_parser_unary_expression (parser);
33864 orig_lhs = lhs;
33865 switch (TREE_CODE (lhs))
33867 case ERROR_MARK:
33868 goto saw_error;
33870 case POSTINCREMENT_EXPR:
33871 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33872 code = OMP_ATOMIC_CAPTURE_OLD;
33873 /* FALLTHROUGH */
33874 case PREINCREMENT_EXPR:
33875 lhs = TREE_OPERAND (lhs, 0);
33876 opcode = PLUS_EXPR;
33877 rhs = integer_one_node;
33878 break;
33880 case POSTDECREMENT_EXPR:
33881 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33882 code = OMP_ATOMIC_CAPTURE_OLD;
33883 /* FALLTHROUGH */
33884 case PREDECREMENT_EXPR:
33885 lhs = TREE_OPERAND (lhs, 0);
33886 opcode = MINUS_EXPR;
33887 rhs = integer_one_node;
33888 break;
33890 case COMPOUND_EXPR:
33891 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33892 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33893 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33894 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33895 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33896 (TREE_OPERAND (lhs, 1), 0), 0)))
33897 == BOOLEAN_TYPE)
33898 /* Undo effects of boolean_increment for post {in,de}crement. */
33899 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33900 /* FALLTHRU */
33901 case MODIFY_EXPR:
33902 if (TREE_CODE (lhs) == MODIFY_EXPR
33903 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33905 /* Undo effects of boolean_increment. */
33906 if (integer_onep (TREE_OPERAND (lhs, 1)))
33908 /* This is pre or post increment. */
33909 rhs = TREE_OPERAND (lhs, 1);
33910 lhs = TREE_OPERAND (lhs, 0);
33911 opcode = NOP_EXPR;
33912 if (code == OMP_ATOMIC_CAPTURE_NEW
33913 && !structured_block
33914 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33915 code = OMP_ATOMIC_CAPTURE_OLD;
33916 break;
33919 /* FALLTHRU */
33920 default:
33921 switch (cp_lexer_peek_token (parser->lexer)->type)
33923 case CPP_MULT_EQ:
33924 opcode = MULT_EXPR;
33925 break;
33926 case CPP_DIV_EQ:
33927 opcode = TRUNC_DIV_EXPR;
33928 break;
33929 case CPP_PLUS_EQ:
33930 opcode = PLUS_EXPR;
33931 break;
33932 case CPP_MINUS_EQ:
33933 opcode = MINUS_EXPR;
33934 break;
33935 case CPP_LSHIFT_EQ:
33936 opcode = LSHIFT_EXPR;
33937 break;
33938 case CPP_RSHIFT_EQ:
33939 opcode = RSHIFT_EXPR;
33940 break;
33941 case CPP_AND_EQ:
33942 opcode = BIT_AND_EXPR;
33943 break;
33944 case CPP_OR_EQ:
33945 opcode = BIT_IOR_EXPR;
33946 break;
33947 case CPP_XOR_EQ:
33948 opcode = BIT_XOR_EXPR;
33949 break;
33950 case CPP_EQ:
33951 enum cp_parser_prec oprec;
33952 cp_token *token;
33953 cp_lexer_consume_token (parser->lexer);
33954 cp_parser_parse_tentatively (parser);
33955 rhs1 = cp_parser_simple_cast_expression (parser);
33956 if (rhs1 == error_mark_node)
33958 cp_parser_abort_tentative_parse (parser);
33959 cp_parser_simple_cast_expression (parser);
33960 goto saw_error;
33962 token = cp_lexer_peek_token (parser->lexer);
33963 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33965 cp_parser_abort_tentative_parse (parser);
33966 cp_parser_parse_tentatively (parser);
33967 rhs = cp_parser_binary_expression (parser, false, true,
33968 PREC_NOT_OPERATOR, NULL);
33969 if (rhs == error_mark_node)
33971 cp_parser_abort_tentative_parse (parser);
33972 cp_parser_binary_expression (parser, false, true,
33973 PREC_NOT_OPERATOR, NULL);
33974 goto saw_error;
33976 switch (TREE_CODE (rhs))
33978 case MULT_EXPR:
33979 case TRUNC_DIV_EXPR:
33980 case RDIV_EXPR:
33981 case PLUS_EXPR:
33982 case MINUS_EXPR:
33983 case LSHIFT_EXPR:
33984 case RSHIFT_EXPR:
33985 case BIT_AND_EXPR:
33986 case BIT_IOR_EXPR:
33987 case BIT_XOR_EXPR:
33988 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33990 if (cp_parser_parse_definitely (parser))
33992 opcode = TREE_CODE (rhs);
33993 rhs1 = TREE_OPERAND (rhs, 0);
33994 rhs = TREE_OPERAND (rhs, 1);
33995 goto stmt_done;
33997 else
33998 goto saw_error;
34000 break;
34001 default:
34002 break;
34004 cp_parser_abort_tentative_parse (parser);
34005 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34007 rhs = cp_parser_expression (parser);
34008 if (rhs == error_mark_node)
34009 goto saw_error;
34010 opcode = NOP_EXPR;
34011 rhs1 = NULL_TREE;
34012 goto stmt_done;
34014 cp_parser_error (parser,
34015 "invalid form of %<#pragma omp atomic%>");
34016 goto saw_error;
34018 if (!cp_parser_parse_definitely (parser))
34019 goto saw_error;
34020 switch (token->type)
34022 case CPP_SEMICOLON:
34023 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34025 code = OMP_ATOMIC_CAPTURE_OLD;
34026 v = lhs;
34027 lhs = NULL_TREE;
34028 lhs1 = rhs1;
34029 rhs1 = NULL_TREE;
34030 cp_lexer_consume_token (parser->lexer);
34031 goto restart;
34033 else if (structured_block)
34035 opcode = NOP_EXPR;
34036 rhs = rhs1;
34037 rhs1 = NULL_TREE;
34038 goto stmt_done;
34040 cp_parser_error (parser,
34041 "invalid form of %<#pragma omp atomic%>");
34042 goto saw_error;
34043 case CPP_MULT:
34044 opcode = MULT_EXPR;
34045 break;
34046 case CPP_DIV:
34047 opcode = TRUNC_DIV_EXPR;
34048 break;
34049 case CPP_PLUS:
34050 opcode = PLUS_EXPR;
34051 break;
34052 case CPP_MINUS:
34053 opcode = MINUS_EXPR;
34054 break;
34055 case CPP_LSHIFT:
34056 opcode = LSHIFT_EXPR;
34057 break;
34058 case CPP_RSHIFT:
34059 opcode = RSHIFT_EXPR;
34060 break;
34061 case CPP_AND:
34062 opcode = BIT_AND_EXPR;
34063 break;
34064 case CPP_OR:
34065 opcode = BIT_IOR_EXPR;
34066 break;
34067 case CPP_XOR:
34068 opcode = BIT_XOR_EXPR;
34069 break;
34070 default:
34071 cp_parser_error (parser,
34072 "invalid operator for %<#pragma omp atomic%>");
34073 goto saw_error;
34075 oprec = TOKEN_PRECEDENCE (token);
34076 gcc_assert (oprec != PREC_NOT_OPERATOR);
34077 if (commutative_tree_code (opcode))
34078 oprec = (enum cp_parser_prec) (oprec - 1);
34079 cp_lexer_consume_token (parser->lexer);
34080 rhs = cp_parser_binary_expression (parser, false, false,
34081 oprec, NULL);
34082 if (rhs == error_mark_node)
34083 goto saw_error;
34084 goto stmt_done;
34085 /* FALLTHROUGH */
34086 default:
34087 cp_parser_error (parser,
34088 "invalid operator for %<#pragma omp atomic%>");
34089 goto saw_error;
34091 cp_lexer_consume_token (parser->lexer);
34093 rhs = cp_parser_expression (parser);
34094 if (rhs == error_mark_node)
34095 goto saw_error;
34096 break;
34098 stmt_done:
34099 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34101 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34102 goto saw_error;
34103 v = cp_parser_unary_expression (parser);
34104 if (v == error_mark_node)
34105 goto saw_error;
34106 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34107 goto saw_error;
34108 lhs1 = cp_parser_unary_expression (parser);
34109 if (lhs1 == error_mark_node)
34110 goto saw_error;
34112 if (structured_block)
34114 cp_parser_consume_semicolon_at_end_of_statement (parser);
34115 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34117 done:
34118 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34119 if (!structured_block)
34120 cp_parser_consume_semicolon_at_end_of_statement (parser);
34121 return;
34123 saw_error:
34124 cp_parser_skip_to_end_of_block_or_statement (parser);
34125 if (structured_block)
34127 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34128 cp_lexer_consume_token (parser->lexer);
34129 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34131 cp_parser_skip_to_end_of_block_or_statement (parser);
34132 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34133 cp_lexer_consume_token (parser->lexer);
34139 /* OpenMP 2.5:
34140 # pragma omp barrier new-line */
34142 static void
34143 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34145 cp_parser_require_pragma_eol (parser, pragma_tok);
34146 finish_omp_barrier ();
34149 /* OpenMP 2.5:
34150 # pragma omp critical [(name)] new-line
34151 structured-block
34153 OpenMP 4.5:
34154 # pragma omp critical [(name) [hint(expression)]] new-line
34155 structured-block */
34157 #define OMP_CRITICAL_CLAUSE_MASK \
34158 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34160 static tree
34161 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34163 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34167 cp_lexer_consume_token (parser->lexer);
34169 name = cp_parser_identifier (parser);
34171 if (name == error_mark_node
34172 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34173 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34174 /*or_comma=*/false,
34175 /*consume_paren=*/true);
34176 if (name == error_mark_node)
34177 name = NULL;
34179 clauses = cp_parser_omp_all_clauses (parser,
34180 OMP_CRITICAL_CLAUSE_MASK,
34181 "#pragma omp critical", pragma_tok);
34183 else
34184 cp_parser_require_pragma_eol (parser, pragma_tok);
34186 stmt = cp_parser_omp_structured_block (parser, if_p);
34187 return c_finish_omp_critical (input_location, stmt, name, clauses);
34190 /* OpenMP 2.5:
34191 # pragma omp flush flush-vars[opt] new-line
34193 flush-vars:
34194 ( variable-list ) */
34196 static void
34197 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34199 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34200 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34201 cp_parser_require_pragma_eol (parser, pragma_tok);
34203 finish_omp_flush ();
34206 /* Helper function, to parse omp for increment expression. */
34208 static tree
34209 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34211 tree cond = cp_parser_binary_expression (parser, false, true,
34212 PREC_NOT_OPERATOR, NULL);
34213 if (cond == error_mark_node
34214 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34216 cp_parser_skip_to_end_of_statement (parser);
34217 return error_mark_node;
34220 switch (TREE_CODE (cond))
34222 case GT_EXPR:
34223 case GE_EXPR:
34224 case LT_EXPR:
34225 case LE_EXPR:
34226 break;
34227 case NE_EXPR:
34228 if (code == CILK_SIMD || code == CILK_FOR)
34229 break;
34230 /* Fall through: OpenMP disallows NE_EXPR. */
34231 gcc_fallthrough ();
34232 default:
34233 return error_mark_node;
34236 /* If decl is an iterator, preserve LHS and RHS of the relational
34237 expr until finish_omp_for. */
34238 if (decl
34239 && (type_dependent_expression_p (decl)
34240 || CLASS_TYPE_P (TREE_TYPE (decl))))
34241 return cond;
34243 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34244 TREE_CODE (cond),
34245 TREE_OPERAND (cond, 0), ERROR_MARK,
34246 TREE_OPERAND (cond, 1), ERROR_MARK,
34247 /*overload=*/NULL, tf_warning_or_error);
34250 /* Helper function, to parse omp for increment expression. */
34252 static tree
34253 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34255 cp_token *token = cp_lexer_peek_token (parser->lexer);
34256 enum tree_code op;
34257 tree lhs, rhs;
34258 cp_id_kind idk;
34259 bool decl_first;
34261 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34263 op = (token->type == CPP_PLUS_PLUS
34264 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34265 cp_lexer_consume_token (parser->lexer);
34266 lhs = cp_parser_simple_cast_expression (parser);
34267 if (lhs != decl
34268 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34269 return error_mark_node;
34270 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34273 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34274 if (lhs != decl
34275 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34276 return error_mark_node;
34278 token = cp_lexer_peek_token (parser->lexer);
34279 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34281 op = (token->type == CPP_PLUS_PLUS
34282 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34283 cp_lexer_consume_token (parser->lexer);
34284 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34287 op = cp_parser_assignment_operator_opt (parser);
34288 if (op == ERROR_MARK)
34289 return error_mark_node;
34291 if (op != NOP_EXPR)
34293 rhs = cp_parser_assignment_expression (parser);
34294 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34295 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34298 lhs = cp_parser_binary_expression (parser, false, false,
34299 PREC_ADDITIVE_EXPRESSION, NULL);
34300 token = cp_lexer_peek_token (parser->lexer);
34301 decl_first = (lhs == decl
34302 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34303 if (decl_first)
34304 lhs = NULL_TREE;
34305 if (token->type != CPP_PLUS
34306 && token->type != CPP_MINUS)
34307 return error_mark_node;
34311 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34312 cp_lexer_consume_token (parser->lexer);
34313 rhs = cp_parser_binary_expression (parser, false, false,
34314 PREC_ADDITIVE_EXPRESSION, NULL);
34315 token = cp_lexer_peek_token (parser->lexer);
34316 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34318 if (lhs == NULL_TREE)
34320 if (op == PLUS_EXPR)
34321 lhs = rhs;
34322 else
34323 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34324 tf_warning_or_error);
34326 else
34327 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34328 ERROR_MARK, NULL, tf_warning_or_error);
34331 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34333 if (!decl_first)
34335 if ((rhs != decl
34336 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34337 || op == MINUS_EXPR)
34338 return error_mark_node;
34339 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34341 else
34342 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34344 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34347 /* Parse the initialization statement of either an OpenMP for loop or
34348 a Cilk Plus for loop.
34350 Return true if the resulting construct should have an
34351 OMP_CLAUSE_PRIVATE added to it. */
34353 static tree
34354 cp_parser_omp_for_loop_init (cp_parser *parser,
34355 enum tree_code code,
34356 tree &this_pre_body,
34357 vec<tree, va_gc> *for_block,
34358 tree &init,
34359 tree &orig_init,
34360 tree &decl,
34361 tree &real_decl)
34363 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34364 return NULL_TREE;
34366 tree add_private_clause = NULL_TREE;
34368 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34370 init-expr:
34371 var = lb
34372 integer-type var = lb
34373 random-access-iterator-type var = lb
34374 pointer-type var = lb
34376 cp_decl_specifier_seq type_specifiers;
34378 /* First, try to parse as an initialized declaration. See
34379 cp_parser_condition, from whence the bulk of this is copied. */
34381 cp_parser_parse_tentatively (parser);
34382 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34383 /*is_trailing_return=*/false,
34384 &type_specifiers);
34385 if (cp_parser_parse_definitely (parser))
34387 /* If parsing a type specifier seq succeeded, then this
34388 MUST be a initialized declaration. */
34389 tree asm_specification, attributes;
34390 cp_declarator *declarator;
34392 declarator = cp_parser_declarator (parser,
34393 CP_PARSER_DECLARATOR_NAMED,
34394 /*ctor_dtor_or_conv_p=*/NULL,
34395 /*parenthesized_p=*/NULL,
34396 /*member_p=*/false,
34397 /*friend_p=*/false);
34398 attributes = cp_parser_attributes_opt (parser);
34399 asm_specification = cp_parser_asm_specification_opt (parser);
34401 if (declarator == cp_error_declarator)
34402 cp_parser_skip_to_end_of_statement (parser);
34404 else
34406 tree pushed_scope, auto_node;
34408 decl = start_decl (declarator, &type_specifiers,
34409 SD_INITIALIZED, attributes,
34410 /*prefix_attributes=*/NULL_TREE,
34411 &pushed_scope);
34413 auto_node = type_uses_auto (TREE_TYPE (decl));
34414 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34416 if (cp_lexer_next_token_is (parser->lexer,
34417 CPP_OPEN_PAREN))
34419 if (code != CILK_SIMD && code != CILK_FOR)
34420 error ("parenthesized initialization is not allowed in "
34421 "OpenMP %<for%> loop");
34422 else
34423 error ("parenthesized initialization is "
34424 "not allowed in for-loop");
34426 else
34427 /* Trigger an error. */
34428 cp_parser_require (parser, CPP_EQ, RT_EQ);
34430 init = error_mark_node;
34431 cp_parser_skip_to_end_of_statement (parser);
34433 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34434 || type_dependent_expression_p (decl)
34435 || auto_node)
34437 bool is_direct_init, is_non_constant_init;
34439 init = cp_parser_initializer (parser,
34440 &is_direct_init,
34441 &is_non_constant_init);
34443 if (auto_node)
34445 TREE_TYPE (decl)
34446 = do_auto_deduction (TREE_TYPE (decl), init,
34447 auto_node);
34449 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34450 && !type_dependent_expression_p (decl))
34451 goto non_class;
34454 cp_finish_decl (decl, init, !is_non_constant_init,
34455 asm_specification,
34456 LOOKUP_ONLYCONVERTING);
34457 orig_init = init;
34458 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34460 vec_safe_push (for_block, this_pre_body);
34461 init = NULL_TREE;
34463 else
34465 init = pop_stmt_list (this_pre_body);
34466 if (init && TREE_CODE (init) == STATEMENT_LIST)
34468 tree_stmt_iterator i = tsi_start (init);
34469 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34470 while (!tsi_end_p (i))
34472 tree t = tsi_stmt (i);
34473 if (TREE_CODE (t) == DECL_EXPR
34474 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34476 tsi_delink (&i);
34477 vec_safe_push (for_block, t);
34478 continue;
34480 break;
34482 if (tsi_one_before_end_p (i))
34484 tree t = tsi_stmt (i);
34485 tsi_delink (&i);
34486 free_stmt_list (init);
34487 init = t;
34491 this_pre_body = NULL_TREE;
34493 else
34495 /* Consume '='. */
34496 cp_lexer_consume_token (parser->lexer);
34497 init = cp_parser_assignment_expression (parser);
34499 non_class:
34500 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34501 init = error_mark_node;
34502 else
34503 cp_finish_decl (decl, NULL_TREE,
34504 /*init_const_expr_p=*/false,
34505 asm_specification,
34506 LOOKUP_ONLYCONVERTING);
34509 if (pushed_scope)
34510 pop_scope (pushed_scope);
34513 else
34515 cp_id_kind idk;
34516 /* If parsing a type specifier sequence failed, then
34517 this MUST be a simple expression. */
34518 if (code == CILK_FOR)
34519 error ("%<_Cilk_for%> allows expression instead of declaration only "
34520 "in C, not in C++");
34521 cp_parser_parse_tentatively (parser);
34522 decl = cp_parser_primary_expression (parser, false, false,
34523 false, &idk);
34524 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34525 if (!cp_parser_error_occurred (parser)
34526 && decl
34527 && (TREE_CODE (decl) == COMPONENT_REF
34528 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34530 cp_parser_abort_tentative_parse (parser);
34531 cp_parser_parse_tentatively (parser);
34532 cp_token *token = cp_lexer_peek_token (parser->lexer);
34533 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34534 /*check_dependency_p=*/true,
34535 /*template_p=*/NULL,
34536 /*declarator_p=*/false,
34537 /*optional_p=*/false);
34538 if (name != error_mark_node
34539 && last_tok == cp_lexer_peek_token (parser->lexer))
34541 decl = cp_parser_lookup_name_simple (parser, name,
34542 token->location);
34543 if (TREE_CODE (decl) == FIELD_DECL)
34544 add_private_clause = omp_privatize_field (decl, false);
34546 cp_parser_abort_tentative_parse (parser);
34547 cp_parser_parse_tentatively (parser);
34548 decl = cp_parser_primary_expression (parser, false, false,
34549 false, &idk);
34551 if (!cp_parser_error_occurred (parser)
34552 && decl
34553 && DECL_P (decl)
34554 && CLASS_TYPE_P (TREE_TYPE (decl)))
34556 tree rhs;
34558 cp_parser_parse_definitely (parser);
34559 cp_parser_require (parser, CPP_EQ, RT_EQ);
34560 rhs = cp_parser_assignment_expression (parser);
34561 orig_init = rhs;
34562 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34563 decl, NOP_EXPR,
34564 rhs,
34565 tf_warning_or_error));
34566 if (!add_private_clause)
34567 add_private_clause = decl;
34569 else
34571 decl = NULL;
34572 cp_parser_abort_tentative_parse (parser);
34573 init = cp_parser_expression (parser);
34574 if (init)
34576 if (TREE_CODE (init) == MODIFY_EXPR
34577 || TREE_CODE (init) == MODOP_EXPR)
34578 real_decl = TREE_OPERAND (init, 0);
34582 return add_private_clause;
34585 /* Parse the restricted form of the for statement allowed by OpenMP. */
34587 static tree
34588 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34589 tree *cclauses, bool *if_p)
34591 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34592 tree real_decl, initv, condv, incrv, declv;
34593 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34594 location_t loc_first;
34595 bool collapse_err = false;
34596 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34597 vec<tree, va_gc> *for_block = make_tree_vector ();
34598 auto_vec<tree, 4> orig_inits;
34599 bool tiling = false;
34601 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34602 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34603 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34604 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34606 tiling = true;
34607 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34609 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34610 && OMP_CLAUSE_ORDERED_EXPR (cl))
34612 ordered_cl = cl;
34613 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34616 if (ordered && ordered < collapse)
34618 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34619 "%<ordered%> clause parameter is less than %<collapse%>");
34620 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34621 = build_int_cst (NULL_TREE, collapse);
34622 ordered = collapse;
34624 if (ordered)
34626 for (tree *pc = &clauses; *pc; )
34627 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34629 error_at (OMP_CLAUSE_LOCATION (*pc),
34630 "%<linear%> clause may not be specified together "
34631 "with %<ordered%> clause with a parameter");
34632 *pc = OMP_CLAUSE_CHAIN (*pc);
34634 else
34635 pc = &OMP_CLAUSE_CHAIN (*pc);
34638 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34639 count = ordered ? ordered : collapse;
34641 declv = make_tree_vec (count);
34642 initv = make_tree_vec (count);
34643 condv = make_tree_vec (count);
34644 incrv = make_tree_vec (count);
34646 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34648 for (i = 0; i < count; i++)
34650 int bracecount = 0;
34651 tree add_private_clause = NULL_TREE;
34652 location_t loc;
34654 if (code != CILK_FOR
34655 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34657 if (!collapse_err)
34658 cp_parser_error (parser, "for statement expected");
34659 return NULL;
34661 if (code == CILK_FOR
34662 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34664 if (!collapse_err)
34665 cp_parser_error (parser, "_Cilk_for statement expected");
34666 return NULL;
34668 loc = cp_lexer_consume_token (parser->lexer)->location;
34670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34671 return NULL;
34673 init = orig_init = decl = real_decl = NULL;
34674 this_pre_body = push_stmt_list ();
34676 add_private_clause
34677 = cp_parser_omp_for_loop_init (parser, code,
34678 this_pre_body, for_block,
34679 init, orig_init, decl, real_decl);
34681 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34682 if (this_pre_body)
34684 this_pre_body = pop_stmt_list (this_pre_body);
34685 if (pre_body)
34687 tree t = pre_body;
34688 pre_body = push_stmt_list ();
34689 add_stmt (t);
34690 add_stmt (this_pre_body);
34691 pre_body = pop_stmt_list (pre_body);
34693 else
34694 pre_body = this_pre_body;
34697 if (decl)
34698 real_decl = decl;
34699 if (cclauses != NULL
34700 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34701 && real_decl != NULL_TREE)
34703 tree *c;
34704 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34705 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34706 && OMP_CLAUSE_DECL (*c) == real_decl)
34708 error_at (loc, "iteration variable %qD"
34709 " should not be firstprivate", real_decl);
34710 *c = OMP_CLAUSE_CHAIN (*c);
34712 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34713 && OMP_CLAUSE_DECL (*c) == real_decl)
34715 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34716 tree l = *c;
34717 *c = OMP_CLAUSE_CHAIN (*c);
34718 if (code == OMP_SIMD)
34720 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34721 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34723 else
34725 OMP_CLAUSE_CHAIN (l) = clauses;
34726 clauses = l;
34728 add_private_clause = NULL_TREE;
34730 else
34732 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34733 && OMP_CLAUSE_DECL (*c) == real_decl)
34734 add_private_clause = NULL_TREE;
34735 c = &OMP_CLAUSE_CHAIN (*c);
34739 if (add_private_clause)
34741 tree c;
34742 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34744 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34745 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34746 && OMP_CLAUSE_DECL (c) == decl)
34747 break;
34748 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34749 && OMP_CLAUSE_DECL (c) == decl)
34750 error_at (loc, "iteration variable %qD "
34751 "should not be firstprivate",
34752 decl);
34753 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34754 && OMP_CLAUSE_DECL (c) == decl)
34755 error_at (loc, "iteration variable %qD should not be reduction",
34756 decl);
34758 if (c == NULL)
34760 if (code != OMP_SIMD)
34761 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34762 else if (collapse == 1)
34763 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34764 else
34765 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34766 OMP_CLAUSE_DECL (c) = add_private_clause;
34767 c = finish_omp_clauses (c, C_ORT_OMP);
34768 if (c)
34770 OMP_CLAUSE_CHAIN (c) = clauses;
34771 clauses = c;
34772 /* For linear, signal that we need to fill up
34773 the so far unknown linear step. */
34774 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34775 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34780 cond = NULL;
34781 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34782 cond = cp_parser_omp_for_cond (parser, decl, code);
34783 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34785 incr = NULL;
34786 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34788 /* If decl is an iterator, preserve the operator on decl
34789 until finish_omp_for. */
34790 if (real_decl
34791 && ((processing_template_decl
34792 && (TREE_TYPE (real_decl) == NULL_TREE
34793 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34794 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34795 incr = cp_parser_omp_for_incr (parser, real_decl);
34796 else
34797 incr = cp_parser_expression (parser);
34798 if (!EXPR_HAS_LOCATION (incr))
34799 protected_set_expr_location (incr, input_location);
34802 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34803 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34804 /*or_comma=*/false,
34805 /*consume_paren=*/true);
34807 TREE_VEC_ELT (declv, i) = decl;
34808 TREE_VEC_ELT (initv, i) = init;
34809 TREE_VEC_ELT (condv, i) = cond;
34810 TREE_VEC_ELT (incrv, i) = incr;
34811 if (orig_init)
34813 orig_inits.safe_grow_cleared (i + 1);
34814 orig_inits[i] = orig_init;
34817 if (i == count - 1)
34818 break;
34820 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34821 in between the collapsed for loops to be still considered perfectly
34822 nested. Hopefully the final version clarifies this.
34823 For now handle (multiple) {'s and empty statements. */
34824 cp_parser_parse_tentatively (parser);
34825 for (;;)
34827 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34828 break;
34829 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34831 cp_lexer_consume_token (parser->lexer);
34832 bracecount++;
34834 else if (bracecount
34835 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34836 cp_lexer_consume_token (parser->lexer);
34837 else
34839 loc = cp_lexer_peek_token (parser->lexer)->location;
34840 error_at (loc, "not enough for loops to collapse");
34841 collapse_err = true;
34842 cp_parser_abort_tentative_parse (parser);
34843 declv = NULL_TREE;
34844 break;
34848 if (declv)
34850 cp_parser_parse_definitely (parser);
34851 nbraces += bracecount;
34855 if (nbraces)
34856 if_p = NULL;
34858 /* Note that we saved the original contents of this flag when we entered
34859 the structured block, and so we don't need to re-save it here. */
34860 if (code == CILK_SIMD || code == CILK_FOR)
34861 parser->in_statement = IN_CILK_SIMD_FOR;
34862 else
34863 parser->in_statement = IN_OMP_FOR;
34865 /* Note that the grammar doesn't call for a structured block here,
34866 though the loop as a whole is a structured block. */
34867 body = push_stmt_list ();
34868 cp_parser_statement (parser, NULL_TREE, false, if_p);
34869 body = pop_stmt_list (body);
34871 if (declv == NULL_TREE)
34872 ret = NULL_TREE;
34873 else
34874 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34875 body, pre_body, &orig_inits, clauses);
34877 while (nbraces)
34879 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34881 cp_lexer_consume_token (parser->lexer);
34882 nbraces--;
34884 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34885 cp_lexer_consume_token (parser->lexer);
34886 else
34888 if (!collapse_err)
34890 error_at (cp_lexer_peek_token (parser->lexer)->location,
34891 "collapsed loops not perfectly nested");
34893 collapse_err = true;
34894 cp_parser_statement_seq_opt (parser, NULL);
34895 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34896 break;
34900 while (!for_block->is_empty ())
34902 tree t = for_block->pop ();
34903 if (TREE_CODE (t) == STATEMENT_LIST)
34904 add_stmt (pop_stmt_list (t));
34905 else
34906 add_stmt (t);
34908 release_tree_vector (for_block);
34910 return ret;
34913 /* Helper function for OpenMP parsing, split clauses and call
34914 finish_omp_clauses on each of the set of clauses afterwards. */
34916 static void
34917 cp_omp_split_clauses (location_t loc, enum tree_code code,
34918 omp_clause_mask mask, tree clauses, tree *cclauses)
34920 int i;
34921 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34922 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34923 if (cclauses[i])
34924 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34927 /* OpenMP 4.0:
34928 #pragma omp simd simd-clause[optseq] new-line
34929 for-loop */
34931 #define OMP_SIMD_CLAUSE_MASK \
34932 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34939 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34941 static tree
34942 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34943 char *p_name, omp_clause_mask mask, tree *cclauses,
34944 bool *if_p)
34946 tree clauses, sb, ret;
34947 unsigned int save;
34948 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34950 strcat (p_name, " simd");
34951 mask |= OMP_SIMD_CLAUSE_MASK;
34953 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34954 cclauses == NULL);
34955 if (cclauses)
34957 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34958 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34959 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34960 OMP_CLAUSE_ORDERED);
34961 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34963 error_at (OMP_CLAUSE_LOCATION (c),
34964 "%<ordered%> clause with parameter may not be specified "
34965 "on %qs construct", p_name);
34966 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34970 sb = begin_omp_structured_block ();
34971 save = cp_parser_begin_omp_structured_block (parser);
34973 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34975 cp_parser_end_omp_structured_block (parser, save);
34976 add_stmt (finish_omp_structured_block (sb));
34978 return ret;
34981 /* OpenMP 2.5:
34982 #pragma omp for for-clause[optseq] new-line
34983 for-loop
34985 OpenMP 4.0:
34986 #pragma omp for simd for-simd-clause[optseq] new-line
34987 for-loop */
34989 #define OMP_FOR_CLAUSE_MASK \
34990 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34998 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35000 static tree
35001 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35002 char *p_name, omp_clause_mask mask, tree *cclauses,
35003 bool *if_p)
35005 tree clauses, sb, ret;
35006 unsigned int save;
35007 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35009 strcat (p_name, " for");
35010 mask |= OMP_FOR_CLAUSE_MASK;
35011 /* parallel for{, simd} disallows nowait clause, but for
35012 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35013 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35014 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35015 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35016 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35017 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35022 const char *p = IDENTIFIER_POINTER (id);
35024 if (strcmp (p, "simd") == 0)
35026 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35027 if (cclauses == NULL)
35028 cclauses = cclauses_buf;
35030 cp_lexer_consume_token (parser->lexer);
35031 if (!flag_openmp) /* flag_openmp_simd */
35032 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35033 cclauses, if_p);
35034 sb = begin_omp_structured_block ();
35035 save = cp_parser_begin_omp_structured_block (parser);
35036 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35037 cclauses, if_p);
35038 cp_parser_end_omp_structured_block (parser, save);
35039 tree body = finish_omp_structured_block (sb);
35040 if (ret == NULL)
35041 return ret;
35042 ret = make_node (OMP_FOR);
35043 TREE_TYPE (ret) = void_type_node;
35044 OMP_FOR_BODY (ret) = body;
35045 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35046 SET_EXPR_LOCATION (ret, loc);
35047 add_stmt (ret);
35048 return ret;
35051 if (!flag_openmp) /* flag_openmp_simd */
35053 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35054 return NULL_TREE;
35057 /* Composite distribute parallel for disallows linear clause. */
35058 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35059 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35061 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35062 cclauses == NULL);
35063 if (cclauses)
35065 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35066 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35069 sb = begin_omp_structured_block ();
35070 save = cp_parser_begin_omp_structured_block (parser);
35072 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35074 cp_parser_end_omp_structured_block (parser, save);
35075 add_stmt (finish_omp_structured_block (sb));
35077 return ret;
35080 /* OpenMP 2.5:
35081 # pragma omp master new-line
35082 structured-block */
35084 static tree
35085 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35087 cp_parser_require_pragma_eol (parser, pragma_tok);
35088 return c_finish_omp_master (input_location,
35089 cp_parser_omp_structured_block (parser, if_p));
35092 /* OpenMP 2.5:
35093 # pragma omp ordered new-line
35094 structured-block
35096 OpenMP 4.5:
35097 # pragma omp ordered ordered-clauses new-line
35098 structured-block */
35100 #define OMP_ORDERED_CLAUSE_MASK \
35101 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35102 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35104 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35105 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35107 static bool
35108 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35109 enum pragma_context context, bool *if_p)
35111 location_t loc = pragma_tok->location;
35113 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35116 const char *p = IDENTIFIER_POINTER (id);
35118 if (strcmp (p, "depend") == 0)
35120 if (context == pragma_stmt)
35122 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35123 "%<depend%> clause may only be used in compound "
35124 "statements");
35125 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35126 return false;
35128 tree clauses
35129 = cp_parser_omp_all_clauses (parser,
35130 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35131 "#pragma omp ordered", pragma_tok);
35132 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35133 return false;
35137 tree clauses
35138 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35139 "#pragma omp ordered", pragma_tok);
35140 c_finish_omp_ordered (loc, clauses,
35141 cp_parser_omp_structured_block (parser, if_p));
35142 return true;
35145 /* OpenMP 2.5:
35147 section-scope:
35148 { section-sequence }
35150 section-sequence:
35151 section-directive[opt] structured-block
35152 section-sequence section-directive structured-block */
35154 static tree
35155 cp_parser_omp_sections_scope (cp_parser *parser)
35157 tree stmt, substmt;
35158 bool error_suppress = false;
35159 cp_token *tok;
35161 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35162 return NULL_TREE;
35164 stmt = push_stmt_list ();
35166 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35167 != PRAGMA_OMP_SECTION)
35169 substmt = cp_parser_omp_structured_block (parser, NULL);
35170 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35171 add_stmt (substmt);
35174 while (1)
35176 tok = cp_lexer_peek_token (parser->lexer);
35177 if (tok->type == CPP_CLOSE_BRACE)
35178 break;
35179 if (tok->type == CPP_EOF)
35180 break;
35182 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35184 cp_lexer_consume_token (parser->lexer);
35185 cp_parser_require_pragma_eol (parser, tok);
35186 error_suppress = false;
35188 else if (!error_suppress)
35190 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35191 error_suppress = true;
35194 substmt = cp_parser_omp_structured_block (parser, NULL);
35195 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35196 add_stmt (substmt);
35198 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35200 substmt = pop_stmt_list (stmt);
35202 stmt = make_node (OMP_SECTIONS);
35203 TREE_TYPE (stmt) = void_type_node;
35204 OMP_SECTIONS_BODY (stmt) = substmt;
35206 add_stmt (stmt);
35207 return stmt;
35210 /* OpenMP 2.5:
35211 # pragma omp sections sections-clause[optseq] newline
35212 sections-scope */
35214 #define OMP_SECTIONS_CLAUSE_MASK \
35215 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35219 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35221 static tree
35222 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35223 char *p_name, omp_clause_mask mask, tree *cclauses)
35225 tree clauses, ret;
35226 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35228 strcat (p_name, " sections");
35229 mask |= OMP_SECTIONS_CLAUSE_MASK;
35230 if (cclauses)
35231 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35233 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35234 cclauses == NULL);
35235 if (cclauses)
35237 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35238 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35241 ret = cp_parser_omp_sections_scope (parser);
35242 if (ret)
35243 OMP_SECTIONS_CLAUSES (ret) = clauses;
35245 return ret;
35248 /* OpenMP 2.5:
35249 # pragma omp parallel parallel-clause[optseq] new-line
35250 structured-block
35251 # pragma omp parallel for parallel-for-clause[optseq] new-line
35252 structured-block
35253 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35254 structured-block
35256 OpenMP 4.0:
35257 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35258 structured-block */
35260 #define OMP_PARALLEL_CLAUSE_MASK \
35261 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35269 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35271 static tree
35272 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35273 char *p_name, omp_clause_mask mask, tree *cclauses,
35274 bool *if_p)
35276 tree stmt, clauses, block;
35277 unsigned int save;
35278 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35280 strcat (p_name, " parallel");
35281 mask |= OMP_PARALLEL_CLAUSE_MASK;
35282 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35283 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35284 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35285 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35287 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35289 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35290 if (cclauses == NULL)
35291 cclauses = cclauses_buf;
35293 cp_lexer_consume_token (parser->lexer);
35294 if (!flag_openmp) /* flag_openmp_simd */
35295 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35296 if_p);
35297 block = begin_omp_parallel ();
35298 save = cp_parser_begin_omp_structured_block (parser);
35299 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35300 if_p);
35301 cp_parser_end_omp_structured_block (parser, save);
35302 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35303 block);
35304 if (ret == NULL_TREE)
35305 return ret;
35306 OMP_PARALLEL_COMBINED (stmt) = 1;
35307 return stmt;
35309 /* When combined with distribute, parallel has to be followed by for.
35310 #pragma omp target parallel is allowed though. */
35311 else if (cclauses
35312 && (mask & (OMP_CLAUSE_MASK_1
35313 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35315 error_at (loc, "expected %<for%> after %qs", p_name);
35316 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35317 return NULL_TREE;
35319 else if (!flag_openmp) /* flag_openmp_simd */
35321 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35322 return NULL_TREE;
35324 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35326 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35327 const char *p = IDENTIFIER_POINTER (id);
35328 if (strcmp (p, "sections") == 0)
35330 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35331 cclauses = cclauses_buf;
35333 cp_lexer_consume_token (parser->lexer);
35334 block = begin_omp_parallel ();
35335 save = cp_parser_begin_omp_structured_block (parser);
35336 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35337 cp_parser_end_omp_structured_block (parser, save);
35338 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35339 block);
35340 OMP_PARALLEL_COMBINED (stmt) = 1;
35341 return stmt;
35345 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35346 cclauses == NULL);
35347 if (cclauses)
35349 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35350 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35353 block = begin_omp_parallel ();
35354 save = cp_parser_begin_omp_structured_block (parser);
35355 cp_parser_statement (parser, NULL_TREE, false, if_p);
35356 cp_parser_end_omp_structured_block (parser, save);
35357 stmt = finish_omp_parallel (clauses, block);
35358 return stmt;
35361 /* OpenMP 2.5:
35362 # pragma omp single single-clause[optseq] new-line
35363 structured-block */
35365 #define OMP_SINGLE_CLAUSE_MASK \
35366 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35369 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35371 static tree
35372 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35374 tree stmt = make_node (OMP_SINGLE);
35375 TREE_TYPE (stmt) = void_type_node;
35377 OMP_SINGLE_CLAUSES (stmt)
35378 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35379 "#pragma omp single", pragma_tok);
35380 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35382 return add_stmt (stmt);
35385 /* OpenMP 3.0:
35386 # pragma omp task task-clause[optseq] new-line
35387 structured-block */
35389 #define OMP_TASK_CLAUSE_MASK \
35390 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35399 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35401 static tree
35402 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35404 tree clauses, block;
35405 unsigned int save;
35407 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35408 "#pragma omp task", pragma_tok);
35409 block = begin_omp_task ();
35410 save = cp_parser_begin_omp_structured_block (parser);
35411 cp_parser_statement (parser, NULL_TREE, false, if_p);
35412 cp_parser_end_omp_structured_block (parser, save);
35413 return finish_omp_task (clauses, block);
35416 /* OpenMP 3.0:
35417 # pragma omp taskwait new-line */
35419 static void
35420 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35422 cp_parser_require_pragma_eol (parser, pragma_tok);
35423 finish_omp_taskwait ();
35426 /* OpenMP 3.1:
35427 # pragma omp taskyield new-line */
35429 static void
35430 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35432 cp_parser_require_pragma_eol (parser, pragma_tok);
35433 finish_omp_taskyield ();
35436 /* OpenMP 4.0:
35437 # pragma omp taskgroup new-line
35438 structured-block */
35440 static tree
35441 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35443 cp_parser_require_pragma_eol (parser, pragma_tok);
35444 return c_finish_omp_taskgroup (input_location,
35445 cp_parser_omp_structured_block (parser,
35446 if_p));
35450 /* OpenMP 2.5:
35451 # pragma omp threadprivate (variable-list) */
35453 static void
35454 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35456 tree vars;
35458 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35459 cp_parser_require_pragma_eol (parser, pragma_tok);
35461 finish_omp_threadprivate (vars);
35464 /* OpenMP 4.0:
35465 # pragma omp cancel cancel-clause[optseq] new-line */
35467 #define OMP_CANCEL_CLAUSE_MASK \
35468 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35472 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35474 static void
35475 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35477 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35478 "#pragma omp cancel", pragma_tok);
35479 finish_omp_cancel (clauses);
35482 /* OpenMP 4.0:
35483 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35485 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35486 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35489 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35491 static void
35492 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35493 enum pragma_context context)
35495 tree clauses;
35496 bool point_seen = false;
35498 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35500 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35501 const char *p = IDENTIFIER_POINTER (id);
35503 if (strcmp (p, "point") == 0)
35505 cp_lexer_consume_token (parser->lexer);
35506 point_seen = true;
35509 if (!point_seen)
35511 cp_parser_error (parser, "expected %<point%>");
35512 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35513 return;
35516 if (context != pragma_compound)
35518 if (context == pragma_stmt)
35519 error_at (pragma_tok->location,
35520 "%<#pragma %s%> may only be used in compound statements",
35521 "omp cancellation point");
35522 else
35523 cp_parser_error (parser, "expected declaration specifiers");
35524 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35525 return;
35528 clauses = cp_parser_omp_all_clauses (parser,
35529 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35530 "#pragma omp cancellation point",
35531 pragma_tok);
35532 finish_omp_cancellation_point (clauses);
35535 /* OpenMP 4.0:
35536 #pragma omp distribute distribute-clause[optseq] new-line
35537 for-loop */
35539 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35540 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35544 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35546 static tree
35547 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35548 char *p_name, omp_clause_mask mask, tree *cclauses,
35549 bool *if_p)
35551 tree clauses, sb, ret;
35552 unsigned int save;
35553 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35555 strcat (p_name, " distribute");
35556 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35558 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35560 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35561 const char *p = IDENTIFIER_POINTER (id);
35562 bool simd = false;
35563 bool parallel = false;
35565 if (strcmp (p, "simd") == 0)
35566 simd = true;
35567 else
35568 parallel = strcmp (p, "parallel") == 0;
35569 if (parallel || simd)
35571 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35572 if (cclauses == NULL)
35573 cclauses = cclauses_buf;
35574 cp_lexer_consume_token (parser->lexer);
35575 if (!flag_openmp) /* flag_openmp_simd */
35577 if (simd)
35578 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35579 cclauses, if_p);
35580 else
35581 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35582 cclauses, if_p);
35584 sb = begin_omp_structured_block ();
35585 save = cp_parser_begin_omp_structured_block (parser);
35586 if (simd)
35587 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35588 cclauses, if_p);
35589 else
35590 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35591 cclauses, if_p);
35592 cp_parser_end_omp_structured_block (parser, save);
35593 tree body = finish_omp_structured_block (sb);
35594 if (ret == NULL)
35595 return ret;
35596 ret = make_node (OMP_DISTRIBUTE);
35597 TREE_TYPE (ret) = void_type_node;
35598 OMP_FOR_BODY (ret) = body;
35599 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35600 SET_EXPR_LOCATION (ret, loc);
35601 add_stmt (ret);
35602 return ret;
35605 if (!flag_openmp) /* flag_openmp_simd */
35607 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35608 return NULL_TREE;
35611 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35612 cclauses == NULL);
35613 if (cclauses)
35615 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35616 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35619 sb = begin_omp_structured_block ();
35620 save = cp_parser_begin_omp_structured_block (parser);
35622 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35624 cp_parser_end_omp_structured_block (parser, save);
35625 add_stmt (finish_omp_structured_block (sb));
35627 return ret;
35630 /* OpenMP 4.0:
35631 # pragma omp teams teams-clause[optseq] new-line
35632 structured-block */
35634 #define OMP_TEAMS_CLAUSE_MASK \
35635 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35643 static tree
35644 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35645 char *p_name, omp_clause_mask mask, tree *cclauses,
35646 bool *if_p)
35648 tree clauses, sb, ret;
35649 unsigned int save;
35650 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35652 strcat (p_name, " teams");
35653 mask |= OMP_TEAMS_CLAUSE_MASK;
35655 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35657 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35658 const char *p = IDENTIFIER_POINTER (id);
35659 if (strcmp (p, "distribute") == 0)
35661 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35662 if (cclauses == NULL)
35663 cclauses = cclauses_buf;
35665 cp_lexer_consume_token (parser->lexer);
35666 if (!flag_openmp) /* flag_openmp_simd */
35667 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35668 cclauses, if_p);
35669 sb = begin_omp_structured_block ();
35670 save = cp_parser_begin_omp_structured_block (parser);
35671 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35672 cclauses, if_p);
35673 cp_parser_end_omp_structured_block (parser, save);
35674 tree body = finish_omp_structured_block (sb);
35675 if (ret == NULL)
35676 return ret;
35677 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35678 ret = make_node (OMP_TEAMS);
35679 TREE_TYPE (ret) = void_type_node;
35680 OMP_TEAMS_CLAUSES (ret) = clauses;
35681 OMP_TEAMS_BODY (ret) = body;
35682 OMP_TEAMS_COMBINED (ret) = 1;
35683 SET_EXPR_LOCATION (ret, loc);
35684 return add_stmt (ret);
35687 if (!flag_openmp) /* flag_openmp_simd */
35689 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35690 return NULL_TREE;
35693 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35694 cclauses == NULL);
35695 if (cclauses)
35697 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35698 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35701 tree stmt = make_node (OMP_TEAMS);
35702 TREE_TYPE (stmt) = void_type_node;
35703 OMP_TEAMS_CLAUSES (stmt) = clauses;
35704 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35705 SET_EXPR_LOCATION (stmt, loc);
35707 return add_stmt (stmt);
35710 /* OpenMP 4.0:
35711 # pragma omp target data target-data-clause[optseq] new-line
35712 structured-block */
35714 #define OMP_TARGET_DATA_CLAUSE_MASK \
35715 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35718 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35720 static tree
35721 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35723 tree clauses
35724 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35725 "#pragma omp target data", pragma_tok);
35726 int map_seen = 0;
35727 for (tree *pc = &clauses; *pc;)
35729 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35730 switch (OMP_CLAUSE_MAP_KIND (*pc))
35732 case GOMP_MAP_TO:
35733 case GOMP_MAP_ALWAYS_TO:
35734 case GOMP_MAP_FROM:
35735 case GOMP_MAP_ALWAYS_FROM:
35736 case GOMP_MAP_TOFROM:
35737 case GOMP_MAP_ALWAYS_TOFROM:
35738 case GOMP_MAP_ALLOC:
35739 map_seen = 3;
35740 break;
35741 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35742 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35743 case GOMP_MAP_ALWAYS_POINTER:
35744 break;
35745 default:
35746 map_seen |= 1;
35747 error_at (OMP_CLAUSE_LOCATION (*pc),
35748 "%<#pragma omp target data%> with map-type other "
35749 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35750 "on %<map%> clause");
35751 *pc = OMP_CLAUSE_CHAIN (*pc);
35752 continue;
35754 pc = &OMP_CLAUSE_CHAIN (*pc);
35757 if (map_seen != 3)
35759 if (map_seen == 0)
35760 error_at (pragma_tok->location,
35761 "%<#pragma omp target data%> must contain at least "
35762 "one %<map%> clause");
35763 return NULL_TREE;
35766 tree stmt = make_node (OMP_TARGET_DATA);
35767 TREE_TYPE (stmt) = void_type_node;
35768 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35770 keep_next_level (true);
35771 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35773 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35774 return add_stmt (stmt);
35777 /* OpenMP 4.5:
35778 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35779 structured-block */
35781 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35782 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35786 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35788 static tree
35789 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35790 enum pragma_context context)
35792 bool data_seen = false;
35793 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35795 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35796 const char *p = IDENTIFIER_POINTER (id);
35798 if (strcmp (p, "data") == 0)
35800 cp_lexer_consume_token (parser->lexer);
35801 data_seen = true;
35804 if (!data_seen)
35806 cp_parser_error (parser, "expected %<data%>");
35807 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35808 return NULL_TREE;
35811 if (context == pragma_stmt)
35813 error_at (pragma_tok->location,
35814 "%<#pragma %s%> may only be used in compound statements",
35815 "omp target enter data");
35816 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35817 return NULL_TREE;
35820 tree clauses
35821 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35822 "#pragma omp target enter data", pragma_tok);
35823 int map_seen = 0;
35824 for (tree *pc = &clauses; *pc;)
35826 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35827 switch (OMP_CLAUSE_MAP_KIND (*pc))
35829 case GOMP_MAP_TO:
35830 case GOMP_MAP_ALWAYS_TO:
35831 case GOMP_MAP_ALLOC:
35832 map_seen = 3;
35833 break;
35834 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35835 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35836 case GOMP_MAP_ALWAYS_POINTER:
35837 break;
35838 default:
35839 map_seen |= 1;
35840 error_at (OMP_CLAUSE_LOCATION (*pc),
35841 "%<#pragma omp target enter data%> with map-type other "
35842 "than %<to%> or %<alloc%> on %<map%> clause");
35843 *pc = OMP_CLAUSE_CHAIN (*pc);
35844 continue;
35846 pc = &OMP_CLAUSE_CHAIN (*pc);
35849 if (map_seen != 3)
35851 if (map_seen == 0)
35852 error_at (pragma_tok->location,
35853 "%<#pragma omp target enter data%> must contain at least "
35854 "one %<map%> clause");
35855 return NULL_TREE;
35858 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35859 TREE_TYPE (stmt) = void_type_node;
35860 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35861 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35862 return add_stmt (stmt);
35865 /* OpenMP 4.5:
35866 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35867 structured-block */
35869 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35870 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35874 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35876 static tree
35877 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35878 enum pragma_context context)
35880 bool data_seen = false;
35881 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35883 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35884 const char *p = IDENTIFIER_POINTER (id);
35886 if (strcmp (p, "data") == 0)
35888 cp_lexer_consume_token (parser->lexer);
35889 data_seen = true;
35892 if (!data_seen)
35894 cp_parser_error (parser, "expected %<data%>");
35895 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35896 return NULL_TREE;
35899 if (context == pragma_stmt)
35901 error_at (pragma_tok->location,
35902 "%<#pragma %s%> may only be used in compound statements",
35903 "omp target exit data");
35904 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35905 return NULL_TREE;
35908 tree clauses
35909 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35910 "#pragma omp target exit data", pragma_tok);
35911 int map_seen = 0;
35912 for (tree *pc = &clauses; *pc;)
35914 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35915 switch (OMP_CLAUSE_MAP_KIND (*pc))
35917 case GOMP_MAP_FROM:
35918 case GOMP_MAP_ALWAYS_FROM:
35919 case GOMP_MAP_RELEASE:
35920 case GOMP_MAP_DELETE:
35921 map_seen = 3;
35922 break;
35923 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35924 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35925 case GOMP_MAP_ALWAYS_POINTER:
35926 break;
35927 default:
35928 map_seen |= 1;
35929 error_at (OMP_CLAUSE_LOCATION (*pc),
35930 "%<#pragma omp target exit data%> with map-type other "
35931 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35932 " clause");
35933 *pc = OMP_CLAUSE_CHAIN (*pc);
35934 continue;
35936 pc = &OMP_CLAUSE_CHAIN (*pc);
35939 if (map_seen != 3)
35941 if (map_seen == 0)
35942 error_at (pragma_tok->location,
35943 "%<#pragma omp target exit data%> must contain at least "
35944 "one %<map%> clause");
35945 return NULL_TREE;
35948 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35949 TREE_TYPE (stmt) = void_type_node;
35950 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35951 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35952 return add_stmt (stmt);
35955 /* OpenMP 4.0:
35956 # pragma omp target update target-update-clause[optseq] new-line */
35958 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35959 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35964 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35966 static bool
35967 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35968 enum pragma_context context)
35970 if (context == pragma_stmt)
35972 error_at (pragma_tok->location,
35973 "%<#pragma %s%> may only be used in compound statements",
35974 "omp target update");
35975 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35976 return false;
35979 tree clauses
35980 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35981 "#pragma omp target update", pragma_tok);
35982 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35983 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35985 error_at (pragma_tok->location,
35986 "%<#pragma omp target update%> must contain at least one "
35987 "%<from%> or %<to%> clauses");
35988 return false;
35991 tree stmt = make_node (OMP_TARGET_UPDATE);
35992 TREE_TYPE (stmt) = void_type_node;
35993 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35994 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35995 add_stmt (stmt);
35996 return false;
35999 /* OpenMP 4.0:
36000 # pragma omp target target-clause[optseq] new-line
36001 structured-block */
36003 #define OMP_TARGET_CLAUSE_MASK \
36004 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36014 static bool
36015 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36016 enum pragma_context context, bool *if_p)
36018 tree *pc = NULL, stmt;
36020 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36023 const char *p = IDENTIFIER_POINTER (id);
36024 enum tree_code ccode = ERROR_MARK;
36026 if (strcmp (p, "teams") == 0)
36027 ccode = OMP_TEAMS;
36028 else if (strcmp (p, "parallel") == 0)
36029 ccode = OMP_PARALLEL;
36030 else if (strcmp (p, "simd") == 0)
36031 ccode = OMP_SIMD;
36032 if (ccode != ERROR_MARK)
36034 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36035 char p_name[sizeof ("#pragma omp target teams distribute "
36036 "parallel for simd")];
36038 cp_lexer_consume_token (parser->lexer);
36039 strcpy (p_name, "#pragma omp target");
36040 if (!flag_openmp) /* flag_openmp_simd */
36042 tree stmt;
36043 switch (ccode)
36045 case OMP_TEAMS:
36046 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36047 OMP_TARGET_CLAUSE_MASK,
36048 cclauses, if_p);
36049 break;
36050 case OMP_PARALLEL:
36051 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36052 OMP_TARGET_CLAUSE_MASK,
36053 cclauses, if_p);
36054 break;
36055 case OMP_SIMD:
36056 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36057 OMP_TARGET_CLAUSE_MASK,
36058 cclauses, if_p);
36059 break;
36060 default:
36061 gcc_unreachable ();
36063 return stmt != NULL_TREE;
36065 keep_next_level (true);
36066 tree sb = begin_omp_structured_block (), ret;
36067 unsigned save = cp_parser_begin_omp_structured_block (parser);
36068 switch (ccode)
36070 case OMP_TEAMS:
36071 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36072 OMP_TARGET_CLAUSE_MASK, cclauses,
36073 if_p);
36074 break;
36075 case OMP_PARALLEL:
36076 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36077 OMP_TARGET_CLAUSE_MASK, cclauses,
36078 if_p);
36079 break;
36080 case OMP_SIMD:
36081 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36082 OMP_TARGET_CLAUSE_MASK, cclauses,
36083 if_p);
36084 break;
36085 default:
36086 gcc_unreachable ();
36088 cp_parser_end_omp_structured_block (parser, save);
36089 tree body = finish_omp_structured_block (sb);
36090 if (ret == NULL_TREE)
36091 return false;
36092 if (ccode == OMP_TEAMS && !processing_template_decl)
36094 /* For combined target teams, ensure the num_teams and
36095 thread_limit clause expressions are evaluated on the host,
36096 before entering the target construct. */
36097 tree c;
36098 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36099 c; c = OMP_CLAUSE_CHAIN (c))
36100 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36101 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36102 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36104 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36105 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36106 if (expr == error_mark_node)
36107 continue;
36108 tree tmp = TARGET_EXPR_SLOT (expr);
36109 add_stmt (expr);
36110 OMP_CLAUSE_OPERAND (c, 0) = expr;
36111 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36112 OMP_CLAUSE_FIRSTPRIVATE);
36113 OMP_CLAUSE_DECL (tc) = tmp;
36114 OMP_CLAUSE_CHAIN (tc)
36115 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36116 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36119 tree stmt = make_node (OMP_TARGET);
36120 TREE_TYPE (stmt) = void_type_node;
36121 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36122 OMP_TARGET_BODY (stmt) = body;
36123 OMP_TARGET_COMBINED (stmt) = 1;
36124 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36125 add_stmt (stmt);
36126 pc = &OMP_TARGET_CLAUSES (stmt);
36127 goto check_clauses;
36129 else if (!flag_openmp) /* flag_openmp_simd */
36131 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36132 return false;
36134 else if (strcmp (p, "data") == 0)
36136 cp_lexer_consume_token (parser->lexer);
36137 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36138 return true;
36140 else if (strcmp (p, "enter") == 0)
36142 cp_lexer_consume_token (parser->lexer);
36143 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36144 return false;
36146 else if (strcmp (p, "exit") == 0)
36148 cp_lexer_consume_token (parser->lexer);
36149 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36150 return false;
36152 else if (strcmp (p, "update") == 0)
36154 cp_lexer_consume_token (parser->lexer);
36155 return cp_parser_omp_target_update (parser, pragma_tok, context);
36158 if (!flag_openmp) /* flag_openmp_simd */
36160 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36161 return false;
36164 stmt = make_node (OMP_TARGET);
36165 TREE_TYPE (stmt) = void_type_node;
36167 OMP_TARGET_CLAUSES (stmt)
36168 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36169 "#pragma omp target", pragma_tok);
36170 pc = &OMP_TARGET_CLAUSES (stmt);
36171 keep_next_level (true);
36172 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36174 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36175 add_stmt (stmt);
36177 check_clauses:
36178 while (*pc)
36180 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36181 switch (OMP_CLAUSE_MAP_KIND (*pc))
36183 case GOMP_MAP_TO:
36184 case GOMP_MAP_ALWAYS_TO:
36185 case GOMP_MAP_FROM:
36186 case GOMP_MAP_ALWAYS_FROM:
36187 case GOMP_MAP_TOFROM:
36188 case GOMP_MAP_ALWAYS_TOFROM:
36189 case GOMP_MAP_ALLOC:
36190 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36191 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36192 case GOMP_MAP_ALWAYS_POINTER:
36193 break;
36194 default:
36195 error_at (OMP_CLAUSE_LOCATION (*pc),
36196 "%<#pragma omp target%> with map-type other "
36197 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36198 "on %<map%> clause");
36199 *pc = OMP_CLAUSE_CHAIN (*pc);
36200 continue;
36202 pc = &OMP_CLAUSE_CHAIN (*pc);
36204 return true;
36207 /* OpenACC 2.0:
36208 # pragma acc cache (variable-list) new-line
36211 static tree
36212 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36214 tree stmt, clauses;
36216 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36217 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36219 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36221 stmt = make_node (OACC_CACHE);
36222 TREE_TYPE (stmt) = void_type_node;
36223 OACC_CACHE_CLAUSES (stmt) = clauses;
36224 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36225 add_stmt (stmt);
36227 return stmt;
36230 /* OpenACC 2.0:
36231 # pragma acc data oacc-data-clause[optseq] new-line
36232 structured-block */
36234 #define OACC_DATA_CLAUSE_MASK \
36235 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36245 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36247 static tree
36248 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36250 tree stmt, clauses, block;
36251 unsigned int save;
36253 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36254 "#pragma acc data", pragma_tok);
36256 block = begin_omp_parallel ();
36257 save = cp_parser_begin_omp_structured_block (parser);
36258 cp_parser_statement (parser, NULL_TREE, false, if_p);
36259 cp_parser_end_omp_structured_block (parser, save);
36260 stmt = finish_oacc_data (clauses, block);
36261 return stmt;
36264 /* OpenACC 2.0:
36265 # pragma acc host_data <clauses> new-line
36266 structured-block */
36268 #define OACC_HOST_DATA_CLAUSE_MASK \
36269 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36271 static tree
36272 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36274 tree stmt, clauses, block;
36275 unsigned int save;
36277 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36278 "#pragma acc host_data", pragma_tok);
36280 block = begin_omp_parallel ();
36281 save = cp_parser_begin_omp_structured_block (parser);
36282 cp_parser_statement (parser, NULL_TREE, false, if_p);
36283 cp_parser_end_omp_structured_block (parser, save);
36284 stmt = finish_oacc_host_data (clauses, block);
36285 return stmt;
36288 /* OpenACC 2.0:
36289 # pragma acc declare oacc-data-clause[optseq] new-line
36292 #define OACC_DECLARE_CLAUSE_MASK \
36293 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36304 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36306 static tree
36307 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36309 tree clauses, stmt;
36310 bool error = false;
36312 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36313 "#pragma acc declare", pragma_tok, true);
36316 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36318 error_at (pragma_tok->location,
36319 "no valid clauses specified in %<#pragma acc declare%>");
36320 return NULL_TREE;
36323 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36325 location_t loc = OMP_CLAUSE_LOCATION (t);
36326 tree decl = OMP_CLAUSE_DECL (t);
36327 if (!DECL_P (decl))
36329 error_at (loc, "array section in %<#pragma acc declare%>");
36330 error = true;
36331 continue;
36333 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36334 switch (OMP_CLAUSE_MAP_KIND (t))
36336 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36337 case GOMP_MAP_FORCE_ALLOC:
36338 case GOMP_MAP_FORCE_TO:
36339 case GOMP_MAP_FORCE_DEVICEPTR:
36340 case GOMP_MAP_DEVICE_RESIDENT:
36341 break;
36343 case GOMP_MAP_LINK:
36344 if (!global_bindings_p ()
36345 && (TREE_STATIC (decl)
36346 || !DECL_EXTERNAL (decl)))
36348 error_at (loc,
36349 "%qD must be a global variable in "
36350 "%<#pragma acc declare link%>",
36351 decl);
36352 error = true;
36353 continue;
36355 break;
36357 default:
36358 if (global_bindings_p ())
36360 error_at (loc, "invalid OpenACC clause at file scope");
36361 error = true;
36362 continue;
36364 if (DECL_EXTERNAL (decl))
36366 error_at (loc,
36367 "invalid use of %<extern%> variable %qD "
36368 "in %<#pragma acc declare%>", decl);
36369 error = true;
36370 continue;
36372 else if (TREE_PUBLIC (decl))
36374 error_at (loc,
36375 "invalid use of %<global%> variable %qD "
36376 "in %<#pragma acc declare%>", decl);
36377 error = true;
36378 continue;
36380 break;
36383 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36384 || lookup_attribute ("omp declare target link",
36385 DECL_ATTRIBUTES (decl)))
36387 error_at (loc, "variable %qD used more than once with "
36388 "%<#pragma acc declare%>", decl);
36389 error = true;
36390 continue;
36393 if (!error)
36395 tree id;
36397 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36398 id = get_identifier ("omp declare target link");
36399 else
36400 id = get_identifier ("omp declare target");
36402 DECL_ATTRIBUTES (decl)
36403 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36404 if (global_bindings_p ())
36406 symtab_node *node = symtab_node::get (decl);
36407 if (node != NULL)
36409 node->offloadable = 1;
36410 if (ENABLE_OFFLOADING)
36412 g->have_offload = true;
36413 if (is_a <varpool_node *> (node))
36414 vec_safe_push (offload_vars, decl);
36421 if (error || global_bindings_p ())
36422 return NULL_TREE;
36424 stmt = make_node (OACC_DECLARE);
36425 TREE_TYPE (stmt) = void_type_node;
36426 OACC_DECLARE_CLAUSES (stmt) = clauses;
36427 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36429 add_stmt (stmt);
36431 return NULL_TREE;
36434 /* OpenACC 2.0:
36435 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36439 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36441 LOC is the location of the #pragma token.
36444 #define OACC_ENTER_DATA_CLAUSE_MASK \
36445 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36451 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36453 #define OACC_EXIT_DATA_CLAUSE_MASK \
36454 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36460 static tree
36461 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36462 bool enter)
36464 location_t loc = pragma_tok->location;
36465 tree stmt, clauses;
36466 const char *p = "";
36468 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36469 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36471 if (strcmp (p, "data") != 0)
36473 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36474 enter ? "enter" : "exit");
36475 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36476 return NULL_TREE;
36479 cp_lexer_consume_token (parser->lexer);
36481 if (enter)
36482 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36483 "#pragma acc enter data", pragma_tok);
36484 else
36485 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36486 "#pragma acc exit data", pragma_tok);
36488 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36490 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36491 enter ? "enter" : "exit");
36492 return NULL_TREE;
36495 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36496 TREE_TYPE (stmt) = void_type_node;
36497 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36498 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36499 add_stmt (stmt);
36500 return stmt;
36503 /* OpenACC 2.0:
36504 # pragma acc loop oacc-loop-clause[optseq] new-line
36505 structured-block */
36507 #define OACC_LOOP_CLAUSE_MASK \
36508 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36519 static tree
36520 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36521 omp_clause_mask mask, tree *cclauses, bool *if_p)
36523 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36525 strcat (p_name, " loop");
36526 mask |= OACC_LOOP_CLAUSE_MASK;
36528 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36529 cclauses == NULL);
36530 if (cclauses)
36532 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36533 if (*cclauses)
36534 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36535 if (clauses)
36536 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36539 tree block = begin_omp_structured_block ();
36540 int save = cp_parser_begin_omp_structured_block (parser);
36541 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36542 cp_parser_end_omp_structured_block (parser, save);
36543 add_stmt (finish_omp_structured_block (block));
36545 return stmt;
36548 /* OpenACC 2.0:
36549 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36550 structured-block
36554 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36555 structured-block
36558 #define OACC_KERNELS_CLAUSE_MASK \
36559 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36575 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36577 #define OACC_PARALLEL_CLAUSE_MASK \
36578 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36597 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36599 static tree
36600 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36601 char *p_name, bool *if_p)
36603 omp_clause_mask mask;
36604 enum tree_code code;
36605 switch (cp_parser_pragma_kind (pragma_tok))
36607 case PRAGMA_OACC_KERNELS:
36608 strcat (p_name, " kernels");
36609 mask = OACC_KERNELS_CLAUSE_MASK;
36610 code = OACC_KERNELS;
36611 break;
36612 case PRAGMA_OACC_PARALLEL:
36613 strcat (p_name, " parallel");
36614 mask = OACC_PARALLEL_CLAUSE_MASK;
36615 code = OACC_PARALLEL;
36616 break;
36617 default:
36618 gcc_unreachable ();
36621 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36623 const char *p
36624 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36625 if (strcmp (p, "loop") == 0)
36627 cp_lexer_consume_token (parser->lexer);
36628 tree block = begin_omp_parallel ();
36629 tree clauses;
36630 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36631 if_p);
36632 return finish_omp_construct (code, block, clauses);
36636 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36638 tree block = begin_omp_parallel ();
36639 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36640 cp_parser_statement (parser, NULL_TREE, false, if_p);
36641 cp_parser_end_omp_structured_block (parser, save);
36642 return finish_omp_construct (code, block, clauses);
36645 /* OpenACC 2.0:
36646 # pragma acc update oacc-update-clause[optseq] new-line
36649 #define OACC_UPDATE_CLAUSE_MASK \
36650 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36655 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36657 static tree
36658 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36660 tree stmt, clauses;
36662 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36663 "#pragma acc update", pragma_tok);
36665 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36667 error_at (pragma_tok->location,
36668 "%<#pragma acc update%> must contain at least one "
36669 "%<device%> or %<host%> or %<self%> clause");
36670 return NULL_TREE;
36673 stmt = make_node (OACC_UPDATE);
36674 TREE_TYPE (stmt) = void_type_node;
36675 OACC_UPDATE_CLAUSES (stmt) = clauses;
36676 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36677 add_stmt (stmt);
36678 return stmt;
36681 /* OpenACC 2.0:
36682 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36684 LOC is the location of the #pragma token.
36687 #define OACC_WAIT_CLAUSE_MASK \
36688 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36690 static tree
36691 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36693 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36694 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36696 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36697 list = cp_parser_oacc_wait_list (parser, loc, list);
36699 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36700 "#pragma acc wait", pragma_tok);
36702 stmt = c_finish_oacc_wait (loc, list, clauses);
36703 stmt = finish_expr_stmt (stmt);
36705 return stmt;
36708 /* OpenMP 4.0:
36709 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36711 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36712 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36719 static void
36720 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36721 enum pragma_context context)
36723 bool first_p = parser->omp_declare_simd == NULL;
36724 cp_omp_declare_simd_data data;
36725 if (first_p)
36727 data.error_seen = false;
36728 data.fndecl_seen = false;
36729 data.tokens = vNULL;
36730 data.clauses = NULL_TREE;
36731 /* It is safe to take the address of a local variable; it will only be
36732 used while this scope is live. */
36733 parser->omp_declare_simd = &data;
36736 /* Store away all pragma tokens. */
36737 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36738 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36739 cp_lexer_consume_token (parser->lexer);
36740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36741 parser->omp_declare_simd->error_seen = true;
36742 cp_parser_require_pragma_eol (parser, pragma_tok);
36743 struct cp_token_cache *cp
36744 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36745 parser->omp_declare_simd->tokens.safe_push (cp);
36747 if (first_p)
36749 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36750 cp_parser_pragma (parser, context, NULL);
36751 switch (context)
36753 case pragma_external:
36754 cp_parser_declaration (parser);
36755 break;
36756 case pragma_member:
36757 cp_parser_member_declaration (parser);
36758 break;
36759 case pragma_objc_icode:
36760 cp_parser_block_declaration (parser, /*statement_p=*/false);
36761 break;
36762 default:
36763 cp_parser_declaration_statement (parser);
36764 break;
36766 if (parser->omp_declare_simd
36767 && !parser->omp_declare_simd->error_seen
36768 && !parser->omp_declare_simd->fndecl_seen)
36769 error_at (pragma_tok->location,
36770 "%<#pragma omp declare simd%> not immediately followed by "
36771 "function declaration or definition");
36772 data.tokens.release ();
36773 parser->omp_declare_simd = NULL;
36777 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36778 This function is modelled similar to the late parsing of omp declare
36779 simd. */
36781 static tree
36782 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36784 struct cp_token_cache *ce;
36785 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36786 int ii = 0;
36788 if (parser->omp_declare_simd != NULL
36789 || lookup_attribute ("simd", attrs))
36791 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36792 "used in the same function marked as a Cilk Plus SIMD-enabled "
36793 "function");
36794 parser->cilk_simd_fn_info->tokens.release ();
36795 XDELETE (parser->cilk_simd_fn_info);
36796 parser->cilk_simd_fn_info = NULL;
36797 return attrs;
36799 if (!info->error_seen && info->fndecl_seen)
36801 error ("vector attribute not immediately followed by a single function"
36802 " declaration or definition");
36803 info->error_seen = true;
36805 if (info->error_seen)
36806 return attrs;
36808 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36810 tree c, cl;
36812 cp_parser_push_lexer_for_tokens (parser, ce);
36813 parser->lexer->in_pragma = true;
36814 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36815 "SIMD-enabled functions attribute",
36816 NULL);
36817 cp_parser_pop_lexer (parser);
36818 if (cl)
36819 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36821 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36822 TREE_CHAIN (c) = attrs;
36823 attrs = c;
36825 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36826 TREE_CHAIN (c) = attrs;
36827 if (processing_template_decl)
36828 ATTR_IS_DEPENDENT (c) = 1;
36829 attrs = c;
36831 info->fndecl_seen = true;
36832 parser->cilk_simd_fn_info->tokens.release ();
36833 XDELETE (parser->cilk_simd_fn_info);
36834 parser->cilk_simd_fn_info = NULL;
36835 return attrs;
36838 /* Finalize #pragma omp declare simd clauses after direct declarator has
36839 been parsed, and put that into "omp declare simd" attribute. */
36841 static tree
36842 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36844 struct cp_token_cache *ce;
36845 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36846 int i;
36848 if (!data->error_seen && data->fndecl_seen)
36850 error ("%<#pragma omp declare simd%> not immediately followed by "
36851 "a single function declaration or definition");
36852 data->error_seen = true;
36854 if (data->error_seen)
36855 return attrs;
36857 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36859 tree c, cl;
36861 cp_parser_push_lexer_for_tokens (parser, ce);
36862 parser->lexer->in_pragma = true;
36863 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36864 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36865 cp_lexer_consume_token (parser->lexer);
36866 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36867 "#pragma omp declare simd", pragma_tok);
36868 cp_parser_pop_lexer (parser);
36869 if (cl)
36870 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36871 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36872 TREE_CHAIN (c) = attrs;
36873 if (processing_template_decl)
36874 ATTR_IS_DEPENDENT (c) = 1;
36875 attrs = c;
36878 data->fndecl_seen = true;
36879 return attrs;
36883 /* OpenMP 4.0:
36884 # pragma omp declare target new-line
36885 declarations and definitions
36886 # pragma omp end declare target new-line
36888 OpenMP 4.5:
36889 # pragma omp declare target ( extended-list ) new-line
36891 # pragma omp declare target declare-target-clauses[seq] new-line */
36893 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36894 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36895 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36897 static void
36898 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36900 tree clauses = NULL_TREE;
36901 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36902 clauses
36903 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36904 "#pragma omp declare target", pragma_tok);
36905 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36907 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36908 clauses);
36909 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36910 cp_parser_require_pragma_eol (parser, pragma_tok);
36912 else
36914 cp_parser_require_pragma_eol (parser, pragma_tok);
36915 scope_chain->omp_declare_target_attribute++;
36916 return;
36918 if (scope_chain->omp_declare_target_attribute)
36919 error_at (pragma_tok->location,
36920 "%<#pragma omp declare target%> with clauses in between "
36921 "%<#pragma omp declare target%> without clauses and "
36922 "%<#pragma omp end declare target%>");
36923 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36925 tree t = OMP_CLAUSE_DECL (c), id;
36926 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36927 tree at2 = lookup_attribute ("omp declare target link",
36928 DECL_ATTRIBUTES (t));
36929 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36931 id = get_identifier ("omp declare target link");
36932 std::swap (at1, at2);
36934 else
36935 id = get_identifier ("omp declare target");
36936 if (at2)
36938 error_at (OMP_CLAUSE_LOCATION (c),
36939 "%qD specified both in declare target %<link%> and %<to%>"
36940 " clauses", t);
36941 continue;
36943 if (!at1)
36945 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36946 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36947 continue;
36949 symtab_node *node = symtab_node::get (t);
36950 if (node != NULL)
36952 node->offloadable = 1;
36953 if (ENABLE_OFFLOADING)
36955 g->have_offload = true;
36956 if (is_a <varpool_node *> (node))
36957 vec_safe_push (offload_vars, t);
36964 static void
36965 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36967 const char *p = "";
36968 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36970 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36971 p = IDENTIFIER_POINTER (id);
36973 if (strcmp (p, "declare") == 0)
36975 cp_lexer_consume_token (parser->lexer);
36976 p = "";
36977 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36979 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36980 p = IDENTIFIER_POINTER (id);
36982 if (strcmp (p, "target") == 0)
36983 cp_lexer_consume_token (parser->lexer);
36984 else
36986 cp_parser_error (parser, "expected %<target%>");
36987 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36988 return;
36991 else
36993 cp_parser_error (parser, "expected %<declare%>");
36994 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36995 return;
36997 cp_parser_require_pragma_eol (parser, pragma_tok);
36998 if (!scope_chain->omp_declare_target_attribute)
36999 error_at (pragma_tok->location,
37000 "%<#pragma omp end declare target%> without corresponding "
37001 "%<#pragma omp declare target%>");
37002 else
37003 scope_chain->omp_declare_target_attribute--;
37006 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37007 expression and optional initializer clause of
37008 #pragma omp declare reduction. We store the expression(s) as
37009 either 3, 6 or 7 special statements inside of the artificial function's
37010 body. The first two statements are DECL_EXPRs for the artificial
37011 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37012 expression that uses those variables.
37013 If there was any INITIALIZER clause, this is followed by further statements,
37014 the fourth and fifth statements are DECL_EXPRs for the artificial
37015 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37016 constructor variant (first token after open paren is not omp_priv),
37017 then the sixth statement is a statement with the function call expression
37018 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37019 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37020 to initialize the OMP_PRIV artificial variable and there is seventh
37021 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37023 static bool
37024 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37026 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37027 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37028 type = TREE_TYPE (type);
37029 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37030 DECL_ARTIFICIAL (omp_out) = 1;
37031 pushdecl (omp_out);
37032 add_decl_expr (omp_out);
37033 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37034 DECL_ARTIFICIAL (omp_in) = 1;
37035 pushdecl (omp_in);
37036 add_decl_expr (omp_in);
37037 tree combiner;
37038 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37040 keep_next_level (true);
37041 tree block = begin_omp_structured_block ();
37042 combiner = cp_parser_expression (parser);
37043 finish_expr_stmt (combiner);
37044 block = finish_omp_structured_block (block);
37045 add_stmt (block);
37047 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37048 return false;
37050 const char *p = "";
37051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37053 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37054 p = IDENTIFIER_POINTER (id);
37057 if (strcmp (p, "initializer") == 0)
37059 cp_lexer_consume_token (parser->lexer);
37060 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37061 return false;
37063 p = "";
37064 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37066 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37067 p = IDENTIFIER_POINTER (id);
37070 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37071 DECL_ARTIFICIAL (omp_priv) = 1;
37072 pushdecl (omp_priv);
37073 add_decl_expr (omp_priv);
37074 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37075 DECL_ARTIFICIAL (omp_orig) = 1;
37076 pushdecl (omp_orig);
37077 add_decl_expr (omp_orig);
37079 keep_next_level (true);
37080 block = begin_omp_structured_block ();
37082 bool ctor = false;
37083 if (strcmp (p, "omp_priv") == 0)
37085 bool is_direct_init, is_non_constant_init;
37086 ctor = true;
37087 cp_lexer_consume_token (parser->lexer);
37088 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37089 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37090 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37091 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37092 == CPP_CLOSE_PAREN
37093 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37094 == CPP_CLOSE_PAREN))
37096 finish_omp_structured_block (block);
37097 error ("invalid initializer clause");
37098 return false;
37100 initializer = cp_parser_initializer (parser, &is_direct_init,
37101 &is_non_constant_init);
37102 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37103 NULL_TREE, LOOKUP_ONLYCONVERTING);
37105 else
37107 cp_parser_parse_tentatively (parser);
37108 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37109 /*check_dependency_p=*/true,
37110 /*template_p=*/NULL,
37111 /*declarator_p=*/false,
37112 /*optional_p=*/false);
37113 vec<tree, va_gc> *args;
37114 if (fn_name == error_mark_node
37115 || cp_parser_error_occurred (parser)
37116 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37117 || ((args = cp_parser_parenthesized_expression_list
37118 (parser, non_attr, /*cast_p=*/false,
37119 /*allow_expansion_p=*/true,
37120 /*non_constant_p=*/NULL)),
37121 cp_parser_error_occurred (parser)))
37123 finish_omp_structured_block (block);
37124 cp_parser_abort_tentative_parse (parser);
37125 cp_parser_error (parser, "expected id-expression (arguments)");
37126 return false;
37128 unsigned int i;
37129 tree arg;
37130 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37131 if (arg == omp_priv
37132 || (TREE_CODE (arg) == ADDR_EXPR
37133 && TREE_OPERAND (arg, 0) == omp_priv))
37134 break;
37135 cp_parser_abort_tentative_parse (parser);
37136 if (arg == NULL_TREE)
37137 error ("one of the initializer call arguments should be %<omp_priv%>"
37138 " or %<&omp_priv%>");
37139 initializer = cp_parser_postfix_expression (parser, false, false, false,
37140 false, NULL);
37141 finish_expr_stmt (initializer);
37144 block = finish_omp_structured_block (block);
37145 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37146 add_stmt (block);
37148 if (ctor)
37149 add_decl_expr (omp_orig);
37151 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37152 return false;
37155 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37156 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37158 return true;
37161 /* OpenMP 4.0
37162 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37163 initializer-clause[opt] new-line
37165 initializer-clause:
37166 initializer (omp_priv initializer)
37167 initializer (function-name (argument-list)) */
37169 static void
37170 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37171 enum pragma_context)
37173 auto_vec<tree> types;
37174 enum tree_code reduc_code = ERROR_MARK;
37175 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37176 unsigned int i;
37177 cp_token *first_token;
37178 cp_token_cache *cp;
37179 int errs;
37180 void *p;
37182 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37183 p = obstack_alloc (&declarator_obstack, 0);
37185 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37186 goto fail;
37188 switch (cp_lexer_peek_token (parser->lexer)->type)
37190 case CPP_PLUS:
37191 reduc_code = PLUS_EXPR;
37192 break;
37193 case CPP_MULT:
37194 reduc_code = MULT_EXPR;
37195 break;
37196 case CPP_MINUS:
37197 reduc_code = MINUS_EXPR;
37198 break;
37199 case CPP_AND:
37200 reduc_code = BIT_AND_EXPR;
37201 break;
37202 case CPP_XOR:
37203 reduc_code = BIT_XOR_EXPR;
37204 break;
37205 case CPP_OR:
37206 reduc_code = BIT_IOR_EXPR;
37207 break;
37208 case CPP_AND_AND:
37209 reduc_code = TRUTH_ANDIF_EXPR;
37210 break;
37211 case CPP_OR_OR:
37212 reduc_code = TRUTH_ORIF_EXPR;
37213 break;
37214 case CPP_NAME:
37215 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37216 break;
37217 default:
37218 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37219 "%<|%>, %<&&%>, %<||%> or identifier");
37220 goto fail;
37223 if (reduc_code != ERROR_MARK)
37224 cp_lexer_consume_token (parser->lexer);
37226 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37227 if (reduc_id == error_mark_node)
37228 goto fail;
37230 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37231 goto fail;
37233 /* Types may not be defined in declare reduction type list. */
37234 const char *saved_message;
37235 saved_message = parser->type_definition_forbidden_message;
37236 parser->type_definition_forbidden_message
37237 = G_("types may not be defined in declare reduction type list");
37238 bool saved_colon_corrects_to_scope_p;
37239 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37240 parser->colon_corrects_to_scope_p = false;
37241 bool saved_colon_doesnt_start_class_def_p;
37242 saved_colon_doesnt_start_class_def_p
37243 = parser->colon_doesnt_start_class_def_p;
37244 parser->colon_doesnt_start_class_def_p = true;
37246 while (true)
37248 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37249 type = cp_parser_type_id (parser);
37250 if (type == error_mark_node)
37252 else if (ARITHMETIC_TYPE_P (type)
37253 && (orig_reduc_id == NULL_TREE
37254 || (TREE_CODE (type) != COMPLEX_TYPE
37255 && (id_equal (orig_reduc_id, "min")
37256 || id_equal (orig_reduc_id, "max")))))
37257 error_at (loc, "predeclared arithmetic type %qT in "
37258 "%<#pragma omp declare reduction%>", type);
37259 else if (TREE_CODE (type) == FUNCTION_TYPE
37260 || TREE_CODE (type) == METHOD_TYPE
37261 || TREE_CODE (type) == ARRAY_TYPE)
37262 error_at (loc, "function or array type %qT in "
37263 "%<#pragma omp declare reduction%>", type);
37264 else if (TREE_CODE (type) == REFERENCE_TYPE)
37265 error_at (loc, "reference type %qT in "
37266 "%<#pragma omp declare reduction%>", type);
37267 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37268 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37269 "%<#pragma omp declare reduction%>", type);
37270 else
37271 types.safe_push (type);
37273 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37274 cp_lexer_consume_token (parser->lexer);
37275 else
37276 break;
37279 /* Restore the saved message. */
37280 parser->type_definition_forbidden_message = saved_message;
37281 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37282 parser->colon_doesnt_start_class_def_p
37283 = saved_colon_doesnt_start_class_def_p;
37285 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37286 || types.is_empty ())
37288 fail:
37289 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37290 goto done;
37293 first_token = cp_lexer_peek_token (parser->lexer);
37294 cp = NULL;
37295 errs = errorcount;
37296 FOR_EACH_VEC_ELT (types, i, type)
37298 tree fntype
37299 = build_function_type_list (void_type_node,
37300 cp_build_reference_type (type, false),
37301 NULL_TREE);
37302 tree this_reduc_id = reduc_id;
37303 if (!dependent_type_p (type))
37304 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37305 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37306 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37307 DECL_ARTIFICIAL (fndecl) = 1;
37308 DECL_EXTERNAL (fndecl) = 1;
37309 DECL_DECLARED_INLINE_P (fndecl) = 1;
37310 DECL_IGNORED_P (fndecl) = 1;
37311 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37312 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37313 DECL_ATTRIBUTES (fndecl)
37314 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37315 DECL_ATTRIBUTES (fndecl));
37316 if (processing_template_decl)
37317 fndecl = push_template_decl (fndecl);
37318 bool block_scope = false;
37319 tree block = NULL_TREE;
37320 if (current_function_decl)
37322 block_scope = true;
37323 DECL_CONTEXT (fndecl) = global_namespace;
37324 if (!processing_template_decl)
37325 pushdecl (fndecl);
37327 else if (current_class_type)
37329 if (cp == NULL)
37331 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37332 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37333 cp_lexer_consume_token (parser->lexer);
37334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37335 goto fail;
37336 cp = cp_token_cache_new (first_token,
37337 cp_lexer_peek_nth_token (parser->lexer,
37338 2));
37340 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37341 finish_member_declaration (fndecl);
37342 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37343 DECL_PENDING_INLINE_P (fndecl) = 1;
37344 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37345 continue;
37347 else
37349 DECL_CONTEXT (fndecl) = current_namespace;
37350 pushdecl (fndecl);
37352 if (!block_scope)
37353 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37354 else
37355 block = begin_omp_structured_block ();
37356 if (cp)
37358 cp_parser_push_lexer_for_tokens (parser, cp);
37359 parser->lexer->in_pragma = true;
37361 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37363 if (!block_scope)
37364 finish_function (0);
37365 else
37366 DECL_CONTEXT (fndecl) = current_function_decl;
37367 if (cp)
37368 cp_parser_pop_lexer (parser);
37369 goto fail;
37371 if (cp)
37372 cp_parser_pop_lexer (parser);
37373 if (!block_scope)
37374 finish_function (0);
37375 else
37377 DECL_CONTEXT (fndecl) = current_function_decl;
37378 block = finish_omp_structured_block (block);
37379 if (TREE_CODE (block) == BIND_EXPR)
37380 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37381 else if (TREE_CODE (block) == STATEMENT_LIST)
37382 DECL_SAVED_TREE (fndecl) = block;
37383 if (processing_template_decl)
37384 add_decl_expr (fndecl);
37386 cp_check_omp_declare_reduction (fndecl);
37387 if (cp == NULL && types.length () > 1)
37388 cp = cp_token_cache_new (first_token,
37389 cp_lexer_peek_nth_token (parser->lexer, 2));
37390 if (errs != errorcount)
37391 break;
37394 cp_parser_require_pragma_eol (parser, pragma_tok);
37396 done:
37397 /* Free any declarators allocated. */
37398 obstack_free (&declarator_obstack, p);
37401 /* OpenMP 4.0
37402 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37403 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37404 initializer-clause[opt] new-line
37405 #pragma omp declare target new-line */
37407 static void
37408 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37409 enum pragma_context context)
37411 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37413 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37414 const char *p = IDENTIFIER_POINTER (id);
37416 if (strcmp (p, "simd") == 0)
37418 cp_lexer_consume_token (parser->lexer);
37419 cp_parser_omp_declare_simd (parser, pragma_tok,
37420 context);
37421 return;
37423 cp_ensure_no_omp_declare_simd (parser);
37424 if (strcmp (p, "reduction") == 0)
37426 cp_lexer_consume_token (parser->lexer);
37427 cp_parser_omp_declare_reduction (parser, pragma_tok,
37428 context);
37429 return;
37431 if (!flag_openmp) /* flag_openmp_simd */
37433 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37434 return;
37436 if (strcmp (p, "target") == 0)
37438 cp_lexer_consume_token (parser->lexer);
37439 cp_parser_omp_declare_target (parser, pragma_tok);
37440 return;
37443 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37444 "or %<target%>");
37445 cp_parser_require_pragma_eol (parser, pragma_tok);
37448 /* OpenMP 4.5:
37449 #pragma omp taskloop taskloop-clause[optseq] new-line
37450 for-loop
37452 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37453 for-loop */
37455 #define OMP_TASKLOOP_CLAUSE_MASK \
37456 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37471 static tree
37472 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37473 char *p_name, omp_clause_mask mask, tree *cclauses,
37474 bool *if_p)
37476 tree clauses, sb, ret;
37477 unsigned int save;
37478 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37480 strcat (p_name, " taskloop");
37481 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37483 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37485 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37486 const char *p = IDENTIFIER_POINTER (id);
37488 if (strcmp (p, "simd") == 0)
37490 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37491 if (cclauses == NULL)
37492 cclauses = cclauses_buf;
37494 cp_lexer_consume_token (parser->lexer);
37495 if (!flag_openmp) /* flag_openmp_simd */
37496 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37497 cclauses, if_p);
37498 sb = begin_omp_structured_block ();
37499 save = cp_parser_begin_omp_structured_block (parser);
37500 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37501 cclauses, if_p);
37502 cp_parser_end_omp_structured_block (parser, save);
37503 tree body = finish_omp_structured_block (sb);
37504 if (ret == NULL)
37505 return ret;
37506 ret = make_node (OMP_TASKLOOP);
37507 TREE_TYPE (ret) = void_type_node;
37508 OMP_FOR_BODY (ret) = body;
37509 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37510 SET_EXPR_LOCATION (ret, loc);
37511 add_stmt (ret);
37512 return ret;
37515 if (!flag_openmp) /* flag_openmp_simd */
37517 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37518 return NULL_TREE;
37521 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37522 cclauses == NULL);
37523 if (cclauses)
37525 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37526 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37529 sb = begin_omp_structured_block ();
37530 save = cp_parser_begin_omp_structured_block (parser);
37532 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37533 if_p);
37535 cp_parser_end_omp_structured_block (parser, save);
37536 add_stmt (finish_omp_structured_block (sb));
37538 return ret;
37542 /* OpenACC 2.0:
37543 # pragma acc routine oacc-routine-clause[optseq] new-line
37544 function-definition
37546 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37549 #define OACC_ROUTINE_CLAUSE_MASK \
37550 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37553 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37556 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37557 component, which must resolve to a declared namespace-scope
37558 function. The clauses are either processed directly (for a named
37559 function), or defered until the immediatley following declaration
37560 is parsed. */
37562 static void
37563 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37564 enum pragma_context context)
37566 gcc_checking_assert (context == pragma_external);
37567 /* The checking for "another pragma following this one" in the "no optional
37568 '( name )'" case makes sure that we dont re-enter. */
37569 gcc_checking_assert (parser->oacc_routine == NULL);
37571 cp_oacc_routine_data data;
37572 data.error_seen = false;
37573 data.fndecl_seen = false;
37574 data.tokens = vNULL;
37575 data.clauses = NULL_TREE;
37576 data.loc = pragma_tok->location;
37577 /* It is safe to take the address of a local variable; it will only be
37578 used while this scope is live. */
37579 parser->oacc_routine = &data;
37581 /* Look for optional '( name )'. */
37582 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37584 cp_lexer_consume_token (parser->lexer); /* '(' */
37586 /* We parse the name as an id-expression. If it resolves to
37587 anything other than a non-overloaded function at namespace
37588 scope, it's an error. */
37589 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37590 tree name = cp_parser_id_expression (parser,
37591 /*template_keyword_p=*/false,
37592 /*check_dependency_p=*/false,
37593 /*template_p=*/NULL,
37594 /*declarator_p=*/false,
37595 /*optional_p=*/false);
37596 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37597 if (name != error_mark_node && decl == error_mark_node)
37598 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37600 if (decl == error_mark_node
37601 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37603 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37604 parser->oacc_routine = NULL;
37605 return;
37608 data.clauses
37609 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37610 "#pragma acc routine",
37611 cp_lexer_peek_token (parser->lexer));
37613 if (decl && is_overloaded_fn (decl)
37614 && (TREE_CODE (decl) != FUNCTION_DECL
37615 || DECL_FUNCTION_TEMPLATE_P (decl)))
37617 error_at (name_loc,
37618 "%<#pragma acc routine%> names a set of overloads");
37619 parser->oacc_routine = NULL;
37620 return;
37623 /* Perhaps we should use the same rule as declarations in different
37624 namespaces? */
37625 if (!DECL_NAMESPACE_SCOPE_P (decl))
37627 error_at (name_loc,
37628 "%qD does not refer to a namespace scope function", decl);
37629 parser->oacc_routine = NULL;
37630 return;
37633 if (TREE_CODE (decl) != FUNCTION_DECL)
37635 error_at (name_loc, "%qD does not refer to a function", decl);
37636 parser->oacc_routine = NULL;
37637 return;
37640 cp_finalize_oacc_routine (parser, decl, false);
37641 parser->oacc_routine = NULL;
37643 else /* No optional '( name )'. */
37645 /* Store away all pragma tokens. */
37646 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37647 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37648 cp_lexer_consume_token (parser->lexer);
37649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37650 parser->oacc_routine->error_seen = true;
37651 cp_parser_require_pragma_eol (parser, pragma_tok);
37652 struct cp_token_cache *cp
37653 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37654 parser->oacc_routine->tokens.safe_push (cp);
37656 /* Emit a helpful diagnostic if there's another pragma following this
37657 one. */
37658 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37660 cp_ensure_no_oacc_routine (parser);
37661 data.tokens.release ();
37662 /* ..., and then just keep going. */
37663 return;
37666 /* We only have to consider the pragma_external case here. */
37667 cp_parser_declaration (parser);
37668 if (parser->oacc_routine
37669 && !parser->oacc_routine->fndecl_seen)
37670 cp_ensure_no_oacc_routine (parser);
37671 else
37672 parser->oacc_routine = NULL;
37673 data.tokens.release ();
37677 /* Finalize #pragma acc routine clauses after direct declarator has
37678 been parsed. */
37680 static tree
37681 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37683 struct cp_token_cache *ce;
37684 cp_oacc_routine_data *data = parser->oacc_routine;
37686 if (!data->error_seen && data->fndecl_seen)
37688 error_at (data->loc,
37689 "%<#pragma acc routine%> not immediately followed by "
37690 "a single function declaration or definition");
37691 data->error_seen = true;
37693 if (data->error_seen)
37694 return attrs;
37696 gcc_checking_assert (data->tokens.length () == 1);
37697 ce = data->tokens[0];
37699 cp_parser_push_lexer_for_tokens (parser, ce);
37700 parser->lexer->in_pragma = true;
37701 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37703 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37704 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37705 parser->oacc_routine->clauses
37706 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37707 "#pragma acc routine", pragma_tok);
37708 cp_parser_pop_lexer (parser);
37709 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37710 fndecl_seen. */
37712 return attrs;
37715 /* Apply any saved OpenACC routine clauses to a just-parsed
37716 declaration. */
37718 static void
37719 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37721 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37723 /* Keep going if we're in error reporting mode. */
37724 if (parser->oacc_routine->error_seen
37725 || fndecl == error_mark_node)
37726 return;
37728 if (parser->oacc_routine->fndecl_seen)
37730 error_at (parser->oacc_routine->loc,
37731 "%<#pragma acc routine%> not immediately followed by"
37732 " a single function declaration or definition");
37733 parser->oacc_routine = NULL;
37734 return;
37736 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37738 cp_ensure_no_oacc_routine (parser);
37739 return;
37742 if (oacc_get_fn_attrib (fndecl))
37744 error_at (parser->oacc_routine->loc,
37745 "%<#pragma acc routine%> already applied to %qD", fndecl);
37746 parser->oacc_routine = NULL;
37747 return;
37750 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37752 error_at (parser->oacc_routine->loc,
37753 TREE_USED (fndecl)
37754 ? G_("%<#pragma acc routine%> must be applied before use")
37755 : G_("%<#pragma acc routine%> must be applied before "
37756 "definition"));
37757 parser->oacc_routine = NULL;
37758 return;
37761 /* Process the routine's dimension clauses. */
37762 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37763 oacc_replace_fn_attrib (fndecl, dims);
37765 /* Add an "omp declare target" attribute. */
37766 DECL_ATTRIBUTES (fndecl)
37767 = tree_cons (get_identifier ("omp declare target"),
37768 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37770 /* Don't unset parser->oacc_routine here: we may still need it to
37771 diagnose wrong usage. But, remember that we've used this "#pragma acc
37772 routine". */
37773 parser->oacc_routine->fndecl_seen = true;
37777 /* Main entry point to OpenMP statement pragmas. */
37779 static void
37780 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37782 tree stmt;
37783 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37784 omp_clause_mask mask (0);
37786 switch (cp_parser_pragma_kind (pragma_tok))
37788 case PRAGMA_OACC_ATOMIC:
37789 cp_parser_omp_atomic (parser, pragma_tok);
37790 return;
37791 case PRAGMA_OACC_CACHE:
37792 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37793 break;
37794 case PRAGMA_OACC_DATA:
37795 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37796 break;
37797 case PRAGMA_OACC_ENTER_DATA:
37798 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37799 break;
37800 case PRAGMA_OACC_EXIT_DATA:
37801 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37802 break;
37803 case PRAGMA_OACC_HOST_DATA:
37804 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37805 break;
37806 case PRAGMA_OACC_KERNELS:
37807 case PRAGMA_OACC_PARALLEL:
37808 strcpy (p_name, "#pragma acc");
37809 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37810 if_p);
37811 break;
37812 case PRAGMA_OACC_LOOP:
37813 strcpy (p_name, "#pragma acc");
37814 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37815 if_p);
37816 break;
37817 case PRAGMA_OACC_UPDATE:
37818 stmt = cp_parser_oacc_update (parser, pragma_tok);
37819 break;
37820 case PRAGMA_OACC_WAIT:
37821 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37822 break;
37823 case PRAGMA_OMP_ATOMIC:
37824 cp_parser_omp_atomic (parser, pragma_tok);
37825 return;
37826 case PRAGMA_OMP_CRITICAL:
37827 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37828 break;
37829 case PRAGMA_OMP_DISTRIBUTE:
37830 strcpy (p_name, "#pragma omp");
37831 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37832 if_p);
37833 break;
37834 case PRAGMA_OMP_FOR:
37835 strcpy (p_name, "#pragma omp");
37836 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37837 if_p);
37838 break;
37839 case PRAGMA_OMP_MASTER:
37840 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37841 break;
37842 case PRAGMA_OMP_PARALLEL:
37843 strcpy (p_name, "#pragma omp");
37844 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37845 if_p);
37846 break;
37847 case PRAGMA_OMP_SECTIONS:
37848 strcpy (p_name, "#pragma omp");
37849 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37850 break;
37851 case PRAGMA_OMP_SIMD:
37852 strcpy (p_name, "#pragma omp");
37853 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37854 if_p);
37855 break;
37856 case PRAGMA_OMP_SINGLE:
37857 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37858 break;
37859 case PRAGMA_OMP_TASK:
37860 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37861 break;
37862 case PRAGMA_OMP_TASKGROUP:
37863 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37864 break;
37865 case PRAGMA_OMP_TASKLOOP:
37866 strcpy (p_name, "#pragma omp");
37867 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37868 if_p);
37869 break;
37870 case PRAGMA_OMP_TEAMS:
37871 strcpy (p_name, "#pragma omp");
37872 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37873 if_p);
37874 break;
37875 default:
37876 gcc_unreachable ();
37879 protected_set_expr_location (stmt, pragma_tok->location);
37882 /* Transactional Memory parsing routines. */
37884 /* Parse a transaction attribute.
37886 txn-attribute:
37887 attribute
37888 [ [ identifier ] ]
37890 We use this instead of cp_parser_attributes_opt for transactions to avoid
37891 the pedwarn in C++98 mode. */
37893 static tree
37894 cp_parser_txn_attribute_opt (cp_parser *parser)
37896 cp_token *token;
37897 tree attr_name, attr = NULL;
37899 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37900 return cp_parser_attributes_opt (parser);
37902 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37903 return NULL_TREE;
37904 cp_lexer_consume_token (parser->lexer);
37905 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37906 goto error1;
37908 token = cp_lexer_peek_token (parser->lexer);
37909 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37911 token = cp_lexer_consume_token (parser->lexer);
37913 attr_name = (token->type == CPP_KEYWORD
37914 /* For keywords, use the canonical spelling,
37915 not the parsed identifier. */
37916 ? ridpointers[(int) token->keyword]
37917 : token->u.value);
37918 attr = build_tree_list (attr_name, NULL_TREE);
37920 else
37921 cp_parser_error (parser, "expected identifier");
37923 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37924 error1:
37925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37926 return attr;
37929 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37931 transaction-statement:
37932 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37933 compound-statement
37934 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37937 static tree
37938 cp_parser_transaction (cp_parser *parser, cp_token *token)
37940 unsigned char old_in = parser->in_transaction;
37941 unsigned char this_in = 1, new_in;
37942 enum rid keyword = token->keyword;
37943 tree stmt, attrs, noex;
37945 cp_lexer_consume_token (parser->lexer);
37947 if (keyword == RID_TRANSACTION_RELAXED
37948 || keyword == RID_SYNCHRONIZED)
37949 this_in |= TM_STMT_ATTR_RELAXED;
37950 else
37952 attrs = cp_parser_txn_attribute_opt (parser);
37953 if (attrs)
37954 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37957 /* Parse a noexcept specification. */
37958 if (keyword == RID_ATOMIC_NOEXCEPT)
37959 noex = boolean_true_node;
37960 else if (keyword == RID_ATOMIC_CANCEL)
37962 /* cancel-and-throw is unimplemented. */
37963 sorry ("atomic_cancel");
37964 noex = NULL_TREE;
37966 else
37967 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37969 /* Keep track if we're in the lexical scope of an outer transaction. */
37970 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37972 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37974 parser->in_transaction = new_in;
37975 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37976 parser->in_transaction = old_in;
37978 finish_transaction_stmt (stmt, NULL, this_in, noex);
37980 return stmt;
37983 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37985 transaction-expression:
37986 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37987 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37990 static tree
37991 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37993 unsigned char old_in = parser->in_transaction;
37994 unsigned char this_in = 1;
37995 cp_token *token;
37996 tree expr, noex;
37997 bool noex_expr;
37998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38000 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38001 || keyword == RID_TRANSACTION_RELAXED);
38003 if (!flag_tm)
38004 error_at (loc,
38005 keyword == RID_TRANSACTION_RELAXED
38006 ? G_("%<__transaction_relaxed%> without transactional memory "
38007 "support enabled")
38008 : G_("%<__transaction_atomic%> without transactional memory "
38009 "support enabled"));
38011 token = cp_parser_require_keyword (parser, keyword,
38012 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38013 : RT_TRANSACTION_RELAXED));
38014 gcc_assert (token != NULL);
38016 if (keyword == RID_TRANSACTION_RELAXED)
38017 this_in |= TM_STMT_ATTR_RELAXED;
38019 /* Set this early. This might mean that we allow transaction_cancel in
38020 an expression that we find out later actually has to be a constexpr.
38021 However, we expect that cxx_constant_value will be able to deal with
38022 this; also, if the noexcept has no constexpr, then what we parse next
38023 really is a transaction's body. */
38024 parser->in_transaction = this_in;
38026 /* Parse a noexcept specification. */
38027 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38028 true);
38030 if (!noex || !noex_expr
38031 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38033 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
38035 expr = cp_parser_expression (parser);
38036 expr = finish_parenthesized_expr (expr);
38038 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
38040 else
38042 /* The only expression that is available got parsed for the noexcept
38043 already. noexcept is true then. */
38044 expr = noex;
38045 noex = boolean_true_node;
38048 expr = build_transaction_expr (token->location, expr, this_in, noex);
38049 parser->in_transaction = old_in;
38051 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38052 return error_mark_node;
38054 return (flag_tm ? expr : error_mark_node);
38057 /* Parse a function-transaction-block.
38059 function-transaction-block:
38060 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38061 function-body
38062 __transaction_atomic txn-attribute[opt] function-try-block
38063 __transaction_relaxed ctor-initializer[opt] function-body
38064 __transaction_relaxed function-try-block
38067 static bool
38068 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38070 unsigned char old_in = parser->in_transaction;
38071 unsigned char new_in = 1;
38072 tree compound_stmt, stmt, attrs;
38073 bool ctor_initializer_p;
38074 cp_token *token;
38076 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38077 || keyword == RID_TRANSACTION_RELAXED);
38078 token = cp_parser_require_keyword (parser, keyword,
38079 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38080 : RT_TRANSACTION_RELAXED));
38081 gcc_assert (token != NULL);
38083 if (keyword == RID_TRANSACTION_RELAXED)
38084 new_in |= TM_STMT_ATTR_RELAXED;
38085 else
38087 attrs = cp_parser_txn_attribute_opt (parser);
38088 if (attrs)
38089 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38092 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38094 parser->in_transaction = new_in;
38096 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38097 ctor_initializer_p = cp_parser_function_try_block (parser);
38098 else
38099 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38100 (parser, /*in_function_try_block=*/false);
38102 parser->in_transaction = old_in;
38104 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38106 return ctor_initializer_p;
38109 /* Parse a __transaction_cancel statement.
38111 cancel-statement:
38112 __transaction_cancel txn-attribute[opt] ;
38113 __transaction_cancel txn-attribute[opt] throw-expression ;
38115 ??? Cancel and throw is not yet implemented. */
38117 static tree
38118 cp_parser_transaction_cancel (cp_parser *parser)
38120 cp_token *token;
38121 bool is_outer = false;
38122 tree stmt, attrs;
38124 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38125 RT_TRANSACTION_CANCEL);
38126 gcc_assert (token != NULL);
38128 attrs = cp_parser_txn_attribute_opt (parser);
38129 if (attrs)
38130 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38132 /* ??? Parse cancel-and-throw here. */
38134 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38136 if (!flag_tm)
38138 error_at (token->location, "%<__transaction_cancel%> without "
38139 "transactional memory support enabled");
38140 return error_mark_node;
38142 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38144 error_at (token->location, "%<__transaction_cancel%> within a "
38145 "%<__transaction_relaxed%>");
38146 return error_mark_node;
38148 else if (is_outer)
38150 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38151 && !is_tm_may_cancel_outer (current_function_decl))
38153 error_at (token->location, "outer %<__transaction_cancel%> not "
38154 "within outer %<__transaction_atomic%>");
38155 error_at (token->location,
38156 " or a %<transaction_may_cancel_outer%> function");
38157 return error_mark_node;
38160 else if (parser->in_transaction == 0)
38162 error_at (token->location, "%<__transaction_cancel%> not within "
38163 "%<__transaction_atomic%>");
38164 return error_mark_node;
38167 stmt = build_tm_abort_call (token->location, is_outer);
38168 add_stmt (stmt);
38170 return stmt;
38173 /* The parser. */
38175 static GTY (()) cp_parser *the_parser;
38178 /* Special handling for the first token or line in the file. The first
38179 thing in the file might be #pragma GCC pch_preprocess, which loads a
38180 PCH file, which is a GC collection point. So we need to handle this
38181 first pragma without benefit of an existing lexer structure.
38183 Always returns one token to the caller in *FIRST_TOKEN. This is
38184 either the true first token of the file, or the first token after
38185 the initial pragma. */
38187 static void
38188 cp_parser_initial_pragma (cp_token *first_token)
38190 tree name = NULL;
38192 cp_lexer_get_preprocessor_token (NULL, first_token);
38193 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38194 return;
38196 cp_lexer_get_preprocessor_token (NULL, first_token);
38197 if (first_token->type == CPP_STRING)
38199 name = first_token->u.value;
38201 cp_lexer_get_preprocessor_token (NULL, first_token);
38202 if (first_token->type != CPP_PRAGMA_EOL)
38203 error_at (first_token->location,
38204 "junk at end of %<#pragma GCC pch_preprocess%>");
38206 else
38207 error_at (first_token->location, "expected string literal");
38209 /* Skip to the end of the pragma. */
38210 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38211 cp_lexer_get_preprocessor_token (NULL, first_token);
38213 /* Now actually load the PCH file. */
38214 if (name)
38215 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38217 /* Read one more token to return to our caller. We have to do this
38218 after reading the PCH file in, since its pointers have to be
38219 live. */
38220 cp_lexer_get_preprocessor_token (NULL, first_token);
38223 /* Parses the grainsize pragma for the _Cilk_for statement.
38224 Syntax:
38225 #pragma cilk grainsize = <VALUE>. */
38227 static void
38228 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38230 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38232 tree exp = cp_parser_binary_expression (parser, false, false,
38233 PREC_NOT_OPERATOR, NULL);
38234 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38235 if (!exp || exp == error_mark_node)
38237 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38238 return;
38241 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38242 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38243 cp_parser_cilk_for (parser, exp, if_p);
38244 else
38245 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38246 "%<#pragma cilk grainsize%> is not followed by "
38247 "%<_Cilk_for%>");
38248 return;
38250 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38253 /* Normal parsing of a pragma token. Here we can (and must) use the
38254 regular lexer. */
38256 static bool
38257 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38259 cp_token *pragma_tok;
38260 unsigned int id;
38261 tree stmt;
38262 bool ret;
38264 pragma_tok = cp_lexer_consume_token (parser->lexer);
38265 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38266 parser->lexer->in_pragma = true;
38268 id = cp_parser_pragma_kind (pragma_tok);
38269 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38270 cp_ensure_no_omp_declare_simd (parser);
38271 switch (id)
38273 case PRAGMA_GCC_PCH_PREPROCESS:
38274 error_at (pragma_tok->location,
38275 "%<#pragma GCC pch_preprocess%> must be first");
38276 break;
38278 case PRAGMA_OMP_BARRIER:
38279 switch (context)
38281 case pragma_compound:
38282 cp_parser_omp_barrier (parser, pragma_tok);
38283 return false;
38284 case pragma_stmt:
38285 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38286 "used in compound statements", "omp barrier");
38287 break;
38288 default:
38289 goto bad_stmt;
38291 break;
38293 case PRAGMA_OMP_FLUSH:
38294 switch (context)
38296 case pragma_compound:
38297 cp_parser_omp_flush (parser, pragma_tok);
38298 return false;
38299 case pragma_stmt:
38300 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38301 "used in compound statements", "omp flush");
38302 break;
38303 default:
38304 goto bad_stmt;
38306 break;
38308 case PRAGMA_OMP_TASKWAIT:
38309 switch (context)
38311 case pragma_compound:
38312 cp_parser_omp_taskwait (parser, pragma_tok);
38313 return false;
38314 case pragma_stmt:
38315 error_at (pragma_tok->location,
38316 "%<#pragma %s%> may only be used in compound statements",
38317 "omp taskwait");
38318 break;
38319 default:
38320 goto bad_stmt;
38322 break;
38324 case PRAGMA_OMP_TASKYIELD:
38325 switch (context)
38327 case pragma_compound:
38328 cp_parser_omp_taskyield (parser, pragma_tok);
38329 return false;
38330 case pragma_stmt:
38331 error_at (pragma_tok->location,
38332 "%<#pragma %s%> may only be used in compound statements",
38333 "omp taskyield");
38334 break;
38335 default:
38336 goto bad_stmt;
38338 break;
38340 case PRAGMA_OMP_CANCEL:
38341 switch (context)
38343 case pragma_compound:
38344 cp_parser_omp_cancel (parser, pragma_tok);
38345 return false;
38346 case pragma_stmt:
38347 error_at (pragma_tok->location,
38348 "%<#pragma %s%> may only be used in compound statements",
38349 "omp cancel");
38350 break;
38351 default:
38352 goto bad_stmt;
38354 break;
38356 case PRAGMA_OMP_CANCELLATION_POINT:
38357 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38358 return false;
38360 case PRAGMA_OMP_THREADPRIVATE:
38361 cp_parser_omp_threadprivate (parser, pragma_tok);
38362 return false;
38364 case PRAGMA_OMP_DECLARE:
38365 cp_parser_omp_declare (parser, pragma_tok, context);
38366 return false;
38368 case PRAGMA_OACC_DECLARE:
38369 cp_parser_oacc_declare (parser, pragma_tok);
38370 return false;
38372 case PRAGMA_OACC_ENTER_DATA:
38373 if (context == pragma_stmt)
38375 error_at (pragma_tok->location,
38376 "%<#pragma %s%> may only be used in compound statements",
38377 "acc enter data");
38378 break;
38380 else if (context != pragma_compound)
38381 goto bad_stmt;
38382 cp_parser_omp_construct (parser, pragma_tok, if_p);
38383 return true;
38385 case PRAGMA_OACC_EXIT_DATA:
38386 if (context == pragma_stmt)
38388 error_at (pragma_tok->location,
38389 "%<#pragma %s%> may only be used in compound statements",
38390 "acc exit data");
38391 break;
38393 else if (context != pragma_compound)
38394 goto bad_stmt;
38395 cp_parser_omp_construct (parser, pragma_tok, if_p);
38396 return true;
38398 case PRAGMA_OACC_ROUTINE:
38399 if (context != pragma_external)
38401 error_at (pragma_tok->location,
38402 "%<#pragma acc routine%> must be at file scope");
38403 break;
38405 cp_parser_oacc_routine (parser, pragma_tok, context);
38406 return false;
38408 case PRAGMA_OACC_UPDATE:
38409 if (context == pragma_stmt)
38411 error_at (pragma_tok->location,
38412 "%<#pragma %s%> may only be used in compound statements",
38413 "acc update");
38414 break;
38416 else if (context != pragma_compound)
38417 goto bad_stmt;
38418 cp_parser_omp_construct (parser, pragma_tok, if_p);
38419 return true;
38421 case PRAGMA_OACC_WAIT:
38422 if (context == pragma_stmt)
38424 error_at (pragma_tok->location,
38425 "%<#pragma %s%> may only be used in compound statements",
38426 "acc wait");
38427 break;
38429 else if (context != pragma_compound)
38430 goto bad_stmt;
38431 cp_parser_omp_construct (parser, pragma_tok, if_p);
38432 return true;
38434 case PRAGMA_OACC_ATOMIC:
38435 case PRAGMA_OACC_CACHE:
38436 case PRAGMA_OACC_DATA:
38437 case PRAGMA_OACC_HOST_DATA:
38438 case PRAGMA_OACC_KERNELS:
38439 case PRAGMA_OACC_PARALLEL:
38440 case PRAGMA_OACC_LOOP:
38441 case PRAGMA_OMP_ATOMIC:
38442 case PRAGMA_OMP_CRITICAL:
38443 case PRAGMA_OMP_DISTRIBUTE:
38444 case PRAGMA_OMP_FOR:
38445 case PRAGMA_OMP_MASTER:
38446 case PRAGMA_OMP_PARALLEL:
38447 case PRAGMA_OMP_SECTIONS:
38448 case PRAGMA_OMP_SIMD:
38449 case PRAGMA_OMP_SINGLE:
38450 case PRAGMA_OMP_TASK:
38451 case PRAGMA_OMP_TASKGROUP:
38452 case PRAGMA_OMP_TASKLOOP:
38453 case PRAGMA_OMP_TEAMS:
38454 if (context != pragma_stmt && context != pragma_compound)
38455 goto bad_stmt;
38456 stmt = push_omp_privatization_clauses (false);
38457 cp_parser_omp_construct (parser, pragma_tok, if_p);
38458 pop_omp_privatization_clauses (stmt);
38459 return true;
38461 case PRAGMA_OMP_ORDERED:
38462 if (context != pragma_stmt && context != pragma_compound)
38463 goto bad_stmt;
38464 stmt = push_omp_privatization_clauses (false);
38465 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38466 pop_omp_privatization_clauses (stmt);
38467 return ret;
38469 case PRAGMA_OMP_TARGET:
38470 if (context != pragma_stmt && context != pragma_compound)
38471 goto bad_stmt;
38472 stmt = push_omp_privatization_clauses (false);
38473 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38474 pop_omp_privatization_clauses (stmt);
38475 return ret;
38477 case PRAGMA_OMP_END_DECLARE_TARGET:
38478 cp_parser_omp_end_declare_target (parser, pragma_tok);
38479 return false;
38481 case PRAGMA_OMP_SECTION:
38482 error_at (pragma_tok->location,
38483 "%<#pragma omp section%> may only be used in "
38484 "%<#pragma omp sections%> construct");
38485 break;
38487 case PRAGMA_IVDEP:
38489 if (context == pragma_external)
38491 error_at (pragma_tok->location,
38492 "%<#pragma GCC ivdep%> must be inside a function");
38493 break;
38495 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38496 cp_token *tok;
38497 tok = cp_lexer_peek_token (the_parser->lexer);
38498 if (tok->type != CPP_KEYWORD
38499 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38500 && tok->keyword != RID_DO))
38502 cp_parser_error (parser, "for, while or do statement expected");
38503 return false;
38505 cp_parser_iteration_statement (parser, if_p, true);
38506 return true;
38509 case PRAGMA_CILK_SIMD:
38510 if (context == pragma_external)
38512 error_at (pragma_tok->location,
38513 "%<#pragma simd%> must be inside a function");
38514 break;
38516 stmt = push_omp_privatization_clauses (false);
38517 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38518 pop_omp_privatization_clauses (stmt);
38519 return true;
38521 case PRAGMA_CILK_GRAINSIZE:
38522 if (context == pragma_external)
38524 error_at (pragma_tok->location,
38525 "%<#pragma cilk grainsize%> must be inside a function");
38526 break;
38529 /* Ignore the pragma if Cilk Plus is not enabled. */
38530 if (flag_cilkplus)
38532 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38533 return true;
38535 else
38537 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38538 "%<#pragma cilk grainsize%>");
38539 break;
38542 default:
38543 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38544 c_invoke_pragma_handler (id);
38545 break;
38547 bad_stmt:
38548 cp_parser_error (parser, "expected declaration specifiers");
38549 break;
38552 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38553 return false;
38556 /* The interface the pragma parsers have to the lexer. */
38558 enum cpp_ttype
38559 pragma_lex (tree *value, location_t *loc)
38561 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38562 enum cpp_ttype ret = tok->type;
38564 *value = tok->u.value;
38565 if (loc)
38566 *loc = tok->location;
38568 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38569 ret = CPP_EOF;
38570 else if (ret == CPP_STRING)
38571 *value = cp_parser_string_literal (the_parser, false, false);
38572 else
38574 if (ret == CPP_KEYWORD)
38575 ret = CPP_NAME;
38576 cp_lexer_consume_token (the_parser->lexer);
38579 return ret;
38583 /* External interface. */
38585 /* Parse one entire translation unit. */
38587 void
38588 c_parse_file (void)
38590 static bool already_called = false;
38592 if (already_called)
38593 fatal_error (input_location,
38594 "inter-module optimizations not implemented for C++");
38595 already_called = true;
38597 the_parser = cp_parser_new ();
38598 push_deferring_access_checks (flag_access_control
38599 ? dk_no_deferred : dk_no_check);
38600 cp_parser_translation_unit (the_parser);
38601 the_parser = NULL;
38604 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38605 vectorlength clause:
38606 Syntax:
38607 vectorlength ( constant-expression ) */
38609 static tree
38610 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38611 bool is_simd_fn)
38613 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38614 tree expr;
38615 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38616 safelen clause. Thus, vectorlength is represented as OMP 4.0
38617 safelen. For SIMD-enabled function it is represented by OMP 4.0
38618 simdlen. */
38619 if (!is_simd_fn)
38620 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38621 loc);
38622 else
38623 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38624 loc);
38626 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38627 return error_mark_node;
38629 expr = cp_parser_constant_expression (parser);
38630 expr = maybe_constant_value (expr);
38632 /* If expr == error_mark_node, then don't emit any errors nor
38633 create a clause. if any of the above functions returns
38634 error mark node then they would have emitted an error message. */
38635 if (expr == error_mark_node)
38637 else if (!TREE_TYPE (expr)
38638 || !TREE_CONSTANT (expr)
38639 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38640 error_at (loc, "vectorlength must be an integer constant");
38641 else if (TREE_CONSTANT (expr)
38642 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38643 error_at (loc, "vectorlength must be a power of 2");
38644 else
38646 tree c;
38647 if (!is_simd_fn)
38649 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38650 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38651 OMP_CLAUSE_CHAIN (c) = clauses;
38652 clauses = c;
38654 else
38656 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38657 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38658 OMP_CLAUSE_CHAIN (c) = clauses;
38659 clauses = c;
38663 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38664 return error_mark_node;
38665 return clauses;
38668 /* Handles the Cilk Plus #pragma simd linear clause.
38669 Syntax:
38670 linear ( simd-linear-variable-list )
38672 simd-linear-variable-list:
38673 simd-linear-variable
38674 simd-linear-variable-list , simd-linear-variable
38676 simd-linear-variable:
38677 id-expression
38678 id-expression : simd-linear-step
38680 simd-linear-step:
38681 conditional-expression */
38683 static tree
38684 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38686 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38688 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38689 return clauses;
38690 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38692 cp_parser_error (parser, "expected identifier");
38693 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38694 return error_mark_node;
38697 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38698 parser->colon_corrects_to_scope_p = false;
38699 while (1)
38701 cp_token *token = cp_lexer_peek_token (parser->lexer);
38702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38704 cp_parser_error (parser, "expected variable-name");
38705 clauses = error_mark_node;
38706 break;
38709 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38710 false, false);
38711 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38712 token->location);
38713 if (decl == error_mark_node)
38715 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38716 token->location);
38717 clauses = error_mark_node;
38719 else
38721 tree e = NULL_TREE;
38722 tree step_size = integer_one_node;
38724 /* If present, parse the linear step. Otherwise, assume the default
38725 value of 1. */
38726 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38728 cp_lexer_consume_token (parser->lexer);
38730 e = cp_parser_assignment_expression (parser);
38731 e = maybe_constant_value (e);
38733 if (e == error_mark_node)
38735 /* If an error has occurred, then the whole pragma is
38736 considered ill-formed. Thus, no reason to keep
38737 parsing. */
38738 clauses = error_mark_node;
38739 break;
38741 else if (type_dependent_expression_p (e)
38742 || value_dependent_expression_p (e)
38743 || (TREE_TYPE (e)
38744 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38745 && (TREE_CONSTANT (e)
38746 || DECL_P (e))))
38747 step_size = e;
38748 else
38749 cp_parser_error (parser,
38750 "step size must be an integer constant "
38751 "expression or an integer variable");
38754 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38755 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38756 OMP_CLAUSE_DECL (l) = decl;
38757 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38758 OMP_CLAUSE_CHAIN (l) = clauses;
38759 clauses = l;
38761 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38762 cp_lexer_consume_token (parser->lexer);
38763 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38764 break;
38765 else
38767 error_at (cp_lexer_peek_token (parser->lexer)->location,
38768 "expected %<,%> or %<)%> after %qE", decl);
38769 clauses = error_mark_node;
38770 break;
38773 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38774 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38775 return clauses;
38778 /* Returns the name of the next clause. If the clause is not
38779 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38780 token is not consumed. Otherwise, the appropriate enum from the
38781 pragma_simd_clause is returned and the token is consumed. */
38783 static pragma_omp_clause
38784 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38786 pragma_omp_clause clause_type;
38787 cp_token *token = cp_lexer_peek_token (parser->lexer);
38789 if (token->keyword == RID_PRIVATE)
38790 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38791 else if (!token->u.value || token->type != CPP_NAME)
38792 return PRAGMA_CILK_CLAUSE_NONE;
38793 else if (id_equal (token->u.value, "vectorlength"))
38794 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38795 else if (id_equal (token->u.value, "linear"))
38796 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38797 else if (id_equal (token->u.value, "firstprivate"))
38798 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38799 else if (id_equal (token->u.value, "lastprivate"))
38800 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38801 else if (id_equal (token->u.value, "reduction"))
38802 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38803 else
38804 return PRAGMA_CILK_CLAUSE_NONE;
38806 cp_lexer_consume_token (parser->lexer);
38807 return clause_type;
38810 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38812 static tree
38813 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38815 tree clauses = NULL_TREE;
38817 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38818 && clauses != error_mark_node)
38820 pragma_omp_clause c_kind;
38821 c_kind = cp_parser_cilk_simd_clause_name (parser);
38822 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38823 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38824 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38825 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38826 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38827 /* Use the OpenMP 4.0 equivalent function. */
38828 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38829 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38830 /* Use the OpenMP 4.0 equivalent function. */
38831 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38832 clauses);
38833 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38834 /* Use the OMP 4.0 equivalent function. */
38835 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38836 clauses);
38837 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38838 /* Use the OMP 4.0 equivalent function. */
38839 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38840 else
38842 clauses = error_mark_node;
38843 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38844 break;
38848 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38850 if (clauses == error_mark_node)
38851 return error_mark_node;
38852 else
38853 return finish_omp_clauses (clauses, C_ORT_CILK);
38856 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38858 static void
38859 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38861 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38863 if (clauses == error_mark_node)
38864 return;
38866 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38868 error_at (cp_lexer_peek_token (parser->lexer)->location,
38869 "for statement expected");
38870 return;
38873 tree sb = begin_omp_structured_block ();
38874 int save = cp_parser_begin_omp_structured_block (parser);
38875 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38876 if (ret)
38877 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38878 cp_parser_end_omp_structured_block (parser, save);
38879 add_stmt (finish_omp_structured_block (sb));
38882 /* Main entry-point for parsing Cilk Plus _Cilk_for
38883 loops. The return value is error_mark_node
38884 when errors happen and CILK_FOR tree on success. */
38886 static tree
38887 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38889 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38890 gcc_unreachable ();
38892 tree sb = begin_omp_structured_block ();
38893 int save = cp_parser_begin_omp_structured_block (parser);
38895 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38896 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38897 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38898 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38900 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38901 if (ret)
38902 cpp_validate_cilk_plus_loop (ret);
38903 else
38904 ret = error_mark_node;
38906 cp_parser_end_omp_structured_block (parser, save);
38907 add_stmt (finish_omp_structured_block (sb));
38908 return ret;
38911 /* Create an identifier for a generic parameter type (a synthesized
38912 template parameter implied by `auto' or a concept identifier). */
38914 static GTY(()) int generic_parm_count;
38915 static tree
38916 make_generic_type_name ()
38918 char buf[32];
38919 sprintf (buf, "auto:%d", ++generic_parm_count);
38920 return get_identifier (buf);
38923 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38924 (creating a new template parameter list if necessary). Returns the newly
38925 created template type parm. */
38927 static tree
38928 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38930 gcc_assert (current_binding_level->kind == sk_function_parms);
38932 /* Before committing to modifying any scope, if we're in an
38933 implicit template scope, and we're trying to synthesize a
38934 constrained parameter, try to find a previous parameter with
38935 the same name. This is the same-type rule for abbreviated
38936 function templates.
38938 NOTE: We can generate implicit parameters when tentatively
38939 parsing a nested name specifier, only to reject that parse
38940 later. However, matching the same template-id as part of a
38941 direct-declarator should generate an identical template
38942 parameter, so this rule will merge them. */
38943 if (parser->implicit_template_scope && constr)
38945 tree t = parser->implicit_template_parms;
38946 while (t)
38948 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38950 tree d = TREE_VALUE (t);
38951 if (TREE_CODE (d) == PARM_DECL)
38952 /* Return the TEMPLATE_PARM_INDEX. */
38953 d = DECL_INITIAL (d);
38954 return d;
38956 t = TREE_CHAIN (t);
38960 /* We are either continuing a function template that already contains implicit
38961 template parameters, creating a new fully-implicit function template, or
38962 extending an existing explicit function template with implicit template
38963 parameters. */
38965 cp_binding_level *const entry_scope = current_binding_level;
38967 bool become_template = false;
38968 cp_binding_level *parent_scope = 0;
38970 if (parser->implicit_template_scope)
38972 gcc_assert (parser->implicit_template_parms);
38974 current_binding_level = parser->implicit_template_scope;
38976 else
38978 /* Roll back to the existing template parameter scope (in the case of
38979 extending an explicit function template) or introduce a new template
38980 parameter scope ahead of the function parameter scope (or class scope
38981 in the case of out-of-line member definitions). The function scope is
38982 added back after template parameter synthesis below. */
38984 cp_binding_level *scope = entry_scope;
38986 while (scope->kind == sk_function_parms)
38988 parent_scope = scope;
38989 scope = scope->level_chain;
38991 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38993 /* If not defining a class, then any class scope is a scope level in
38994 an out-of-line member definition. In this case simply wind back
38995 beyond the first such scope to inject the template parameter list.
38996 Otherwise wind back to the class being defined. The latter can
38997 occur in class member friend declarations such as:
38999 class A {
39000 void foo (auto);
39002 class B {
39003 friend void A::foo (auto);
39006 The template parameter list synthesized for the friend declaration
39007 must be injected in the scope of 'B'. This can also occur in
39008 erroneous cases such as:
39010 struct A {
39011 struct B {
39012 void foo (auto);
39014 void B::foo (auto) {}
39017 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39018 but, nevertheless, the template parameter list synthesized for the
39019 declarator should be injected into the scope of 'A' as if the
39020 ill-formed template was specified explicitly. */
39022 while (scope->kind == sk_class && !scope->defining_class_p)
39024 parent_scope = scope;
39025 scope = scope->level_chain;
39029 current_binding_level = scope;
39031 if (scope->kind != sk_template_parms
39032 || !function_being_declared_is_template_p (parser))
39034 /* Introduce a new template parameter list for implicit template
39035 parameters. */
39037 become_template = true;
39039 parser->implicit_template_scope
39040 = begin_scope (sk_template_parms, NULL);
39042 ++processing_template_decl;
39044 parser->fully_implicit_function_template_p = true;
39045 ++parser->num_template_parameter_lists;
39047 else
39049 /* Synthesize implicit template parameters at the end of the explicit
39050 template parameter list. */
39052 gcc_assert (current_template_parms);
39054 parser->implicit_template_scope = scope;
39056 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39057 parser->implicit_template_parms
39058 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39062 /* Synthesize a new template parameter and track the current template
39063 parameter chain with implicit_template_parms. */
39065 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39066 tree synth_id = make_generic_type_name ();
39067 tree synth_tmpl_parm;
39068 bool non_type = false;
39070 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39071 synth_tmpl_parm
39072 = finish_template_type_parm (class_type_node, synth_id);
39073 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39074 synth_tmpl_parm
39075 = finish_constrained_template_template_parm (proto, synth_id);
39076 else
39078 synth_tmpl_parm = copy_decl (proto);
39079 DECL_NAME (synth_tmpl_parm) = synth_id;
39080 non_type = true;
39083 // Attach the constraint to the parm before processing.
39084 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39085 TREE_TYPE (node) = constr;
39086 tree new_parm
39087 = process_template_parm (parser->implicit_template_parms,
39088 input_location,
39089 node,
39090 /*non_type=*/non_type,
39091 /*param_pack=*/false);
39093 // Chain the new parameter to the list of implicit parameters.
39094 if (parser->implicit_template_parms)
39095 parser->implicit_template_parms
39096 = TREE_CHAIN (parser->implicit_template_parms);
39097 else
39098 parser->implicit_template_parms = new_parm;
39100 tree new_decl = get_local_decls ();
39101 if (non_type)
39102 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39103 new_decl = DECL_INITIAL (new_decl);
39105 /* If creating a fully implicit function template, start the new implicit
39106 template parameter list with this synthesized type, otherwise grow the
39107 current template parameter list. */
39109 if (become_template)
39111 parent_scope->level_chain = current_binding_level;
39113 tree new_parms = make_tree_vec (1);
39114 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39115 current_template_parms = tree_cons (size_int (processing_template_decl),
39116 new_parms, current_template_parms);
39118 else
39120 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39121 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39122 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39123 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39126 // If the new parameter was constrained, we need to add that to the
39127 // constraints in the template parameter list.
39128 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39130 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39131 reqs = conjoin_constraints (reqs, req);
39132 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39135 current_binding_level = entry_scope;
39137 return new_decl;
39140 /* Finish the declaration of a fully implicit function template. Such a
39141 template has no explicit template parameter list so has not been through the
39142 normal template head and tail processing. synthesize_implicit_template_parm
39143 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39144 provided if the declaration is a class member such that its template
39145 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39146 form is returned. Otherwise NULL_TREE is returned. */
39148 static tree
39149 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39151 gcc_assert (parser->fully_implicit_function_template_p);
39153 if (member_decl_opt && member_decl_opt != error_mark_node
39154 && DECL_VIRTUAL_P (member_decl_opt))
39156 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39157 "implicit templates may not be %<virtual%>");
39158 DECL_VIRTUAL_P (member_decl_opt) = false;
39161 if (member_decl_opt)
39162 member_decl_opt = finish_member_template_decl (member_decl_opt);
39163 end_template_decl ();
39165 parser->fully_implicit_function_template_p = false;
39166 --parser->num_template_parameter_lists;
39168 return member_decl_opt;
39171 #include "gt-cp-parser.h"