libgo: add misc/cgo files
[official-gcc.git] / gcc / cp / parser.c
blobc405fe5b77611fd1041b342bd105a9e6027f1e45
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;
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 (next_tinfo.type != CPP_SEMICOLON)
12450 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12451 guard_tinfo.location,
12452 guard_tinfo.keyword);
12454 else
12456 /* Avoid calling cp_parser_compound_statement, so that we
12457 don't create a new scope. Do everything else by hand. */
12458 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12459 /* If the next keyword is `__label__' we have a label declaration. */
12460 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12461 cp_parser_label_declaration (parser);
12462 /* Parse an (optional) statement-seq. */
12463 cp_parser_statement_seq_opt (parser, NULL_TREE);
12464 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12468 /* Declarations [gram.dcl.dcl] */
12470 /* Parse an optional declaration-sequence.
12472 declaration-seq:
12473 declaration
12474 declaration-seq declaration */
12476 static void
12477 cp_parser_declaration_seq_opt (cp_parser* parser)
12479 while (true)
12481 cp_token *token;
12483 token = cp_lexer_peek_token (parser->lexer);
12485 if (token->type == CPP_CLOSE_BRACE
12486 || token->type == CPP_EOF
12487 || token->type == CPP_PRAGMA_EOL)
12488 break;
12490 if (token->type == CPP_SEMICOLON)
12492 /* A declaration consisting of a single semicolon is
12493 invalid. Allow it unless we're being pedantic. */
12494 cp_lexer_consume_token (parser->lexer);
12495 if (!in_system_header_at (input_location))
12496 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12497 continue;
12500 /* If we're entering or exiting a region that's implicitly
12501 extern "C", modify the lang context appropriately. */
12502 if (!parser->implicit_extern_c && token->implicit_extern_c)
12504 push_lang_context (lang_name_c);
12505 parser->implicit_extern_c = true;
12507 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12509 pop_lang_context ();
12510 parser->implicit_extern_c = false;
12513 if (token->type == CPP_PRAGMA)
12515 /* A top-level declaration can consist solely of a #pragma.
12516 A nested declaration cannot, so this is done here and not
12517 in cp_parser_declaration. (A #pragma at block scope is
12518 handled in cp_parser_statement.) */
12519 cp_parser_pragma (parser, pragma_external, NULL);
12520 continue;
12523 /* Parse the declaration itself. */
12524 cp_parser_declaration (parser);
12528 /* Parse a declaration.
12530 declaration:
12531 block-declaration
12532 function-definition
12533 template-declaration
12534 explicit-instantiation
12535 explicit-specialization
12536 linkage-specification
12537 namespace-definition
12539 C++17:
12540 deduction-guide
12542 GNU extension:
12544 declaration:
12545 __extension__ declaration */
12547 static void
12548 cp_parser_declaration (cp_parser* parser)
12550 cp_token token1;
12551 cp_token token2;
12552 int saved_pedantic;
12553 void *p;
12554 tree attributes = NULL_TREE;
12556 /* Check for the `__extension__' keyword. */
12557 if (cp_parser_extension_opt (parser, &saved_pedantic))
12559 /* Parse the qualified declaration. */
12560 cp_parser_declaration (parser);
12561 /* Restore the PEDANTIC flag. */
12562 pedantic = saved_pedantic;
12564 return;
12567 /* Try to figure out what kind of declaration is present. */
12568 token1 = *cp_lexer_peek_token (parser->lexer);
12570 if (token1.type != CPP_EOF)
12571 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12572 else
12574 token2.type = CPP_EOF;
12575 token2.keyword = RID_MAX;
12578 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12579 p = obstack_alloc (&declarator_obstack, 0);
12581 /* If the next token is `extern' and the following token is a string
12582 literal, then we have a linkage specification. */
12583 if (token1.keyword == RID_EXTERN
12584 && cp_parser_is_pure_string_literal (&token2))
12585 cp_parser_linkage_specification (parser);
12586 /* If the next token is `template', then we have either a template
12587 declaration, an explicit instantiation, or an explicit
12588 specialization. */
12589 else if (token1.keyword == RID_TEMPLATE)
12591 /* `template <>' indicates a template specialization. */
12592 if (token2.type == CPP_LESS
12593 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12594 cp_parser_explicit_specialization (parser);
12595 /* `template <' indicates a template declaration. */
12596 else if (token2.type == CPP_LESS)
12597 cp_parser_template_declaration (parser, /*member_p=*/false);
12598 /* Anything else must be an explicit instantiation. */
12599 else
12600 cp_parser_explicit_instantiation (parser);
12602 /* If the next token is `export', then we have a template
12603 declaration. */
12604 else if (token1.keyword == RID_EXPORT)
12605 cp_parser_template_declaration (parser, /*member_p=*/false);
12606 /* If the next token is `extern', 'static' or 'inline' and the one
12607 after that is `template', we have a GNU extended explicit
12608 instantiation directive. */
12609 else if (cp_parser_allow_gnu_extensions_p (parser)
12610 && (token1.keyword == RID_EXTERN
12611 || token1.keyword == RID_STATIC
12612 || token1.keyword == RID_INLINE)
12613 && token2.keyword == RID_TEMPLATE)
12614 cp_parser_explicit_instantiation (parser);
12615 /* If the next token is `namespace', check for a named or unnamed
12616 namespace definition. */
12617 else if (token1.keyword == RID_NAMESPACE
12618 && (/* A named namespace definition. */
12619 (token2.type == CPP_NAME
12620 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12621 != CPP_EQ))
12622 || (token2.type == CPP_OPEN_SQUARE
12623 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12624 == CPP_OPEN_SQUARE)
12625 /* An unnamed namespace definition. */
12626 || token2.type == CPP_OPEN_BRACE
12627 || token2.keyword == RID_ATTRIBUTE))
12628 cp_parser_namespace_definition (parser);
12629 /* An inline (associated) namespace definition. */
12630 else if (token1.keyword == RID_INLINE
12631 && token2.keyword == RID_NAMESPACE)
12632 cp_parser_namespace_definition (parser);
12633 /* Objective-C++ declaration/definition. */
12634 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12635 cp_parser_objc_declaration (parser, NULL_TREE);
12636 else if (c_dialect_objc ()
12637 && token1.keyword == RID_ATTRIBUTE
12638 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12639 cp_parser_objc_declaration (parser, attributes);
12640 /* At this point we may have a template declared by a concept
12641 introduction. */
12642 else if (flag_concepts
12643 && cp_parser_template_declaration_after_export (parser,
12644 /*member_p=*/false))
12645 /* We did. */;
12646 else
12647 /* Try to parse a block-declaration, or a function-definition. */
12648 cp_parser_block_declaration (parser, /*statement_p=*/false);
12650 /* Free any declarators allocated. */
12651 obstack_free (&declarator_obstack, p);
12654 /* Parse a block-declaration.
12656 block-declaration:
12657 simple-declaration
12658 asm-definition
12659 namespace-alias-definition
12660 using-declaration
12661 using-directive
12663 GNU Extension:
12665 block-declaration:
12666 __extension__ block-declaration
12668 C++0x Extension:
12670 block-declaration:
12671 static_assert-declaration
12673 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12674 part of a declaration-statement. */
12676 static void
12677 cp_parser_block_declaration (cp_parser *parser,
12678 bool statement_p)
12680 cp_token *token1;
12681 int saved_pedantic;
12683 /* Check for the `__extension__' keyword. */
12684 if (cp_parser_extension_opt (parser, &saved_pedantic))
12686 /* Parse the qualified declaration. */
12687 cp_parser_block_declaration (parser, statement_p);
12688 /* Restore the PEDANTIC flag. */
12689 pedantic = saved_pedantic;
12691 return;
12694 /* Peek at the next token to figure out which kind of declaration is
12695 present. */
12696 token1 = cp_lexer_peek_token (parser->lexer);
12698 /* If the next keyword is `asm', we have an asm-definition. */
12699 if (token1->keyword == RID_ASM)
12701 if (statement_p)
12702 cp_parser_commit_to_tentative_parse (parser);
12703 cp_parser_asm_definition (parser);
12705 /* If the next keyword is `namespace', we have a
12706 namespace-alias-definition. */
12707 else if (token1->keyword == RID_NAMESPACE)
12708 cp_parser_namespace_alias_definition (parser);
12709 /* If the next keyword is `using', we have a
12710 using-declaration, a using-directive, or an alias-declaration. */
12711 else if (token1->keyword == RID_USING)
12713 cp_token *token2;
12715 if (statement_p)
12716 cp_parser_commit_to_tentative_parse (parser);
12717 /* If the token after `using' is `namespace', then we have a
12718 using-directive. */
12719 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12720 if (token2->keyword == RID_NAMESPACE)
12721 cp_parser_using_directive (parser);
12722 /* If the second token after 'using' is '=', then we have an
12723 alias-declaration. */
12724 else if (cxx_dialect >= cxx11
12725 && token2->type == CPP_NAME
12726 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12727 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12728 cp_parser_alias_declaration (parser);
12729 /* Otherwise, it's a using-declaration. */
12730 else
12731 cp_parser_using_declaration (parser,
12732 /*access_declaration_p=*/false);
12734 /* If the next keyword is `__label__' we have a misplaced label
12735 declaration. */
12736 else if (token1->keyword == RID_LABEL)
12738 cp_lexer_consume_token (parser->lexer);
12739 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12740 cp_parser_skip_to_end_of_statement (parser);
12741 /* If the next token is now a `;', consume it. */
12742 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12743 cp_lexer_consume_token (parser->lexer);
12745 /* If the next token is `static_assert' we have a static assertion. */
12746 else if (token1->keyword == RID_STATIC_ASSERT)
12747 cp_parser_static_assert (parser, /*member_p=*/false);
12748 /* Anything else must be a simple-declaration. */
12749 else
12750 cp_parser_simple_declaration (parser, !statement_p,
12751 /*maybe_range_for_decl*/NULL);
12754 /* Parse a simple-declaration.
12756 simple-declaration:
12757 decl-specifier-seq [opt] init-declarator-list [opt] ;
12758 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12759 brace-or-equal-initializer ;
12761 init-declarator-list:
12762 init-declarator
12763 init-declarator-list , init-declarator
12765 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12766 function-definition as a simple-declaration.
12768 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12769 parsed declaration if it is an uninitialized single declarator not followed
12770 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12771 if present, will not be consumed. */
12773 static void
12774 cp_parser_simple_declaration (cp_parser* parser,
12775 bool function_definition_allowed_p,
12776 tree *maybe_range_for_decl)
12778 cp_decl_specifier_seq decl_specifiers;
12779 int declares_class_or_enum;
12780 bool saw_declarator;
12781 location_t comma_loc = UNKNOWN_LOCATION;
12782 location_t init_loc = UNKNOWN_LOCATION;
12784 if (maybe_range_for_decl)
12785 *maybe_range_for_decl = NULL_TREE;
12787 /* Defer access checks until we know what is being declared; the
12788 checks for names appearing in the decl-specifier-seq should be
12789 done as if we were in the scope of the thing being declared. */
12790 push_deferring_access_checks (dk_deferred);
12792 /* Parse the decl-specifier-seq. We have to keep track of whether
12793 or not the decl-specifier-seq declares a named class or
12794 enumeration type, since that is the only case in which the
12795 init-declarator-list is allowed to be empty.
12797 [dcl.dcl]
12799 In a simple-declaration, the optional init-declarator-list can be
12800 omitted only when declaring a class or enumeration, that is when
12801 the decl-specifier-seq contains either a class-specifier, an
12802 elaborated-type-specifier, or an enum-specifier. */
12803 cp_parser_decl_specifier_seq (parser,
12804 CP_PARSER_FLAGS_OPTIONAL,
12805 &decl_specifiers,
12806 &declares_class_or_enum);
12807 /* We no longer need to defer access checks. */
12808 stop_deferring_access_checks ();
12810 /* In a block scope, a valid declaration must always have a
12811 decl-specifier-seq. By not trying to parse declarators, we can
12812 resolve the declaration/expression ambiguity more quickly. */
12813 if (!function_definition_allowed_p
12814 && !decl_specifiers.any_specifiers_p)
12816 cp_parser_error (parser, "expected declaration");
12817 goto done;
12820 /* If the next two tokens are both identifiers, the code is
12821 erroneous. The usual cause of this situation is code like:
12823 T t;
12825 where "T" should name a type -- but does not. */
12826 if (!decl_specifiers.any_type_specifiers_p
12827 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12829 /* If parsing tentatively, we should commit; we really are
12830 looking at a declaration. */
12831 cp_parser_commit_to_tentative_parse (parser);
12832 /* Give up. */
12833 goto done;
12836 /* If we have seen at least one decl-specifier, and the next token
12837 is not a parenthesis, then we must be looking at a declaration.
12838 (After "int (" we might be looking at a functional cast.) */
12839 if (decl_specifiers.any_specifiers_p
12840 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12841 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12842 && !cp_parser_error_occurred (parser))
12843 cp_parser_commit_to_tentative_parse (parser);
12845 /* Look for C++17 decomposition declaration. */
12846 for (size_t n = 1; ; n++)
12847 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12848 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12849 continue;
12850 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12851 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12852 && decl_specifiers.any_specifiers_p)
12854 tree decl
12855 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12856 maybe_range_for_decl,
12857 &init_loc);
12859 /* The next token should be either a `,' or a `;'. */
12860 cp_token *token = cp_lexer_peek_token (parser->lexer);
12861 /* If it's a `;', we are done. */
12862 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12863 goto finish;
12864 /* Anything else is an error. */
12865 else
12867 /* If we have already issued an error message we don't need
12868 to issue another one. */
12869 if ((decl != error_mark_node
12870 && DECL_INITIAL (decl) != error_mark_node)
12871 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12872 cp_parser_error (parser, "expected %<,%> or %<;%>");
12873 /* Skip tokens until we reach the end of the statement. */
12874 cp_parser_skip_to_end_of_statement (parser);
12875 /* If the next token is now a `;', consume it. */
12876 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12877 cp_lexer_consume_token (parser->lexer);
12878 goto done;
12881 else
12882 break;
12884 tree last_type;
12885 bool auto_specifier_p;
12886 /* NULL_TREE if both variable and function declaration are allowed,
12887 error_mark_node if function declaration are not allowed and
12888 a FUNCTION_DECL that should be diagnosed if it is followed by
12889 variable declarations. */
12890 tree auto_function_declaration;
12892 last_type = NULL_TREE;
12893 auto_specifier_p
12894 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12895 auto_function_declaration = NULL_TREE;
12897 /* Keep going until we hit the `;' at the end of the simple
12898 declaration. */
12899 saw_declarator = false;
12900 while (cp_lexer_next_token_is_not (parser->lexer,
12901 CPP_SEMICOLON))
12903 cp_token *token;
12904 bool function_definition_p;
12905 tree decl;
12906 tree auto_result = NULL_TREE;
12908 if (saw_declarator)
12910 /* If we are processing next declarator, comma is expected */
12911 token = cp_lexer_peek_token (parser->lexer);
12912 gcc_assert (token->type == CPP_COMMA);
12913 cp_lexer_consume_token (parser->lexer);
12914 if (maybe_range_for_decl)
12916 *maybe_range_for_decl = error_mark_node;
12917 if (comma_loc == UNKNOWN_LOCATION)
12918 comma_loc = token->location;
12921 else
12922 saw_declarator = true;
12924 /* Parse the init-declarator. */
12925 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12926 /*checks=*/NULL,
12927 function_definition_allowed_p,
12928 /*member_p=*/false,
12929 declares_class_or_enum,
12930 &function_definition_p,
12931 maybe_range_for_decl,
12932 &init_loc,
12933 &auto_result);
12934 /* If an error occurred while parsing tentatively, exit quickly.
12935 (That usually happens when in the body of a function; each
12936 statement is treated as a declaration-statement until proven
12937 otherwise.) */
12938 if (cp_parser_error_occurred (parser))
12939 goto done;
12941 if (auto_specifier_p && cxx_dialect >= cxx14)
12943 /* If the init-declarator-list contains more than one
12944 init-declarator, they shall all form declarations of
12945 variables. */
12946 if (auto_function_declaration == NULL_TREE)
12947 auto_function_declaration
12948 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12949 else if (TREE_CODE (decl) == FUNCTION_DECL
12950 || auto_function_declaration != error_mark_node)
12952 error_at (decl_specifiers.locations[ds_type_spec],
12953 "non-variable %qD in declaration with more than one "
12954 "declarator with placeholder type",
12955 TREE_CODE (decl) == FUNCTION_DECL
12956 ? decl : auto_function_declaration);
12957 auto_function_declaration = error_mark_node;
12961 if (auto_result
12962 && (!processing_template_decl || !type_uses_auto (auto_result)))
12964 if (last_type
12965 && last_type != error_mark_node
12966 && !same_type_p (auto_result, last_type))
12968 /* If the list of declarators contains more than one declarator,
12969 the type of each declared variable is determined as described
12970 above. If the type deduced for the template parameter U is not
12971 the same in each deduction, the program is ill-formed. */
12972 error_at (decl_specifiers.locations[ds_type_spec],
12973 "inconsistent deduction for %qT: %qT and then %qT",
12974 decl_specifiers.type, last_type, auto_result);
12975 last_type = error_mark_node;
12977 else
12978 last_type = auto_result;
12981 /* Handle function definitions specially. */
12982 if (function_definition_p)
12984 /* If the next token is a `,', then we are probably
12985 processing something like:
12987 void f() {}, *p;
12989 which is erroneous. */
12990 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12992 cp_token *token = cp_lexer_peek_token (parser->lexer);
12993 error_at (token->location,
12994 "mixing"
12995 " declarations and function-definitions is forbidden");
12997 /* Otherwise, we're done with the list of declarators. */
12998 else
13000 pop_deferring_access_checks ();
13001 return;
13004 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13005 *maybe_range_for_decl = decl;
13006 /* The next token should be either a `,' or a `;'. */
13007 token = cp_lexer_peek_token (parser->lexer);
13008 /* If it's a `,', there are more declarators to come. */
13009 if (token->type == CPP_COMMA)
13010 /* will be consumed next time around */;
13011 /* If it's a `;', we are done. */
13012 else if (token->type == CPP_SEMICOLON)
13013 break;
13014 else if (maybe_range_for_decl)
13016 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13017 permerror (decl_specifiers.locations[ds_type_spec],
13018 "types may not be defined in a for-range-declaration");
13019 break;
13021 /* Anything else is an error. */
13022 else
13024 /* If we have already issued an error message we don't need
13025 to issue another one. */
13026 if ((decl != error_mark_node
13027 && DECL_INITIAL (decl) != error_mark_node)
13028 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13029 cp_parser_error (parser, "expected %<,%> or %<;%>");
13030 /* Skip tokens until we reach the end of the statement. */
13031 cp_parser_skip_to_end_of_statement (parser);
13032 /* If the next token is now a `;', consume it. */
13033 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13034 cp_lexer_consume_token (parser->lexer);
13035 goto done;
13037 /* After the first time around, a function-definition is not
13038 allowed -- even if it was OK at first. For example:
13040 int i, f() {}
13042 is not valid. */
13043 function_definition_allowed_p = false;
13046 /* Issue an error message if no declarators are present, and the
13047 decl-specifier-seq does not itself declare a class or
13048 enumeration: [dcl.dcl]/3. */
13049 if (!saw_declarator)
13051 if (cp_parser_declares_only_class_p (parser))
13053 if (!declares_class_or_enum
13054 && decl_specifiers.type
13055 && OVERLOAD_TYPE_P (decl_specifiers.type))
13056 /* Ensure an error is issued anyway when finish_decltype_type,
13057 called via cp_parser_decl_specifier_seq, returns a class or
13058 an enumeration (c++/51786). */
13059 decl_specifiers.type = NULL_TREE;
13060 shadow_tag (&decl_specifiers);
13062 /* Perform any deferred access checks. */
13063 perform_deferred_access_checks (tf_warning_or_error);
13066 /* Consume the `;'. */
13067 finish:
13068 if (!maybe_range_for_decl)
13069 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13070 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13072 if (init_loc != UNKNOWN_LOCATION)
13073 error_at (init_loc, "initializer in range-based %<for%> loop");
13074 if (comma_loc != UNKNOWN_LOCATION)
13075 error_at (comma_loc,
13076 "multiple declarations in range-based %<for%> loop");
13079 done:
13080 pop_deferring_access_checks ();
13083 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13084 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13085 initializer ; */
13087 static tree
13088 cp_parser_decomposition_declaration (cp_parser *parser,
13089 cp_decl_specifier_seq *decl_specifiers,
13090 tree *maybe_range_for_decl,
13091 location_t *init_loc)
13093 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13094 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13095 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13097 /* Parse the identifier-list. */
13098 auto_vec<cp_expr, 10> v;
13099 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13100 while (true)
13102 cp_expr e = cp_parser_identifier (parser);
13103 if (e.get_value () == error_mark_node)
13104 break;
13105 v.safe_push (e);
13106 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13107 break;
13108 cp_lexer_consume_token (parser->lexer);
13111 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13112 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13114 end_loc = UNKNOWN_LOCATION;
13115 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13116 false);
13117 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13118 cp_lexer_consume_token (parser->lexer);
13119 else
13121 cp_parser_skip_to_end_of_statement (parser);
13122 return error_mark_node;
13126 if (cxx_dialect < cxx1z)
13127 pedwarn (loc, 0, "decomposition declaration only available with "
13128 "-std=c++1z or -std=gnu++1z");
13130 tree pushed_scope;
13131 cp_declarator *declarator = make_declarator (cdk_decomp);
13132 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13133 declarator->id_loc = loc;
13134 if (ref_qual != REF_QUAL_NONE)
13135 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13136 ref_qual == REF_QUAL_RVALUE,
13137 NULL_TREE);
13138 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13139 NULL_TREE, decl_specifiers->attributes,
13140 &pushed_scope);
13141 tree orig_decl = decl;
13143 unsigned int i;
13144 cp_expr e;
13145 cp_decl_specifier_seq decl_specs;
13146 clear_decl_specs (&decl_specs);
13147 decl_specs.type = make_auto ();
13148 tree prev = decl;
13149 FOR_EACH_VEC_ELT (v, i, e)
13151 if (i == 0)
13152 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13153 else
13154 declarator->u.id.unqualified_name = e.get_value ();
13155 declarator->id_loc = e.get_location ();
13156 tree elt_pushed_scope;
13157 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13158 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13159 if (decl2 == error_mark_node)
13160 decl = error_mark_node;
13161 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13163 /* Ensure we've diagnosed redeclaration if we aren't creating
13164 a new VAR_DECL. */
13165 gcc_assert (errorcount);
13166 decl = error_mark_node;
13168 else
13169 prev = decl2;
13170 if (elt_pushed_scope)
13171 pop_scope (elt_pushed_scope);
13174 if (v.is_empty ())
13176 error_at (loc, "empty decomposition declaration");
13177 decl = error_mark_node;
13180 if (maybe_range_for_decl == NULL
13181 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13183 bool non_constant_p = false, is_direct_init = false;
13184 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13185 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13186 &non_constant_p);
13188 if (decl != error_mark_node)
13190 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13191 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13192 cp_finish_decomp (decl, prev, v.length ());
13195 else if (decl != error_mark_node)
13197 *maybe_range_for_decl = prev;
13198 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13199 the underlying DECL. */
13200 cp_finish_decomp (decl, prev, v.length ());
13203 if (pushed_scope)
13204 pop_scope (pushed_scope);
13206 if (decl == error_mark_node && DECL_P (orig_decl))
13208 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13209 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13212 return decl;
13215 /* Parse a decl-specifier-seq.
13217 decl-specifier-seq:
13218 decl-specifier-seq [opt] decl-specifier
13219 decl-specifier attribute-specifier-seq [opt] (C++11)
13221 decl-specifier:
13222 storage-class-specifier
13223 type-specifier
13224 function-specifier
13225 friend
13226 typedef
13228 GNU Extension:
13230 decl-specifier:
13231 attributes
13233 Concepts Extension:
13235 decl-specifier:
13236 concept
13238 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13240 The parser flags FLAGS is used to control type-specifier parsing.
13242 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13243 flags:
13245 1: one of the decl-specifiers is an elaborated-type-specifier
13246 (i.e., a type declaration)
13247 2: one of the decl-specifiers is an enum-specifier or a
13248 class-specifier (i.e., a type definition)
13252 static void
13253 cp_parser_decl_specifier_seq (cp_parser* parser,
13254 cp_parser_flags flags,
13255 cp_decl_specifier_seq *decl_specs,
13256 int* declares_class_or_enum)
13258 bool constructor_possible_p = !parser->in_declarator_p;
13259 bool found_decl_spec = false;
13260 cp_token *start_token = NULL;
13261 cp_decl_spec ds;
13263 /* Clear DECL_SPECS. */
13264 clear_decl_specs (decl_specs);
13266 /* Assume no class or enumeration type is declared. */
13267 *declares_class_or_enum = 0;
13269 /* Keep reading specifiers until there are no more to read. */
13270 while (true)
13272 bool constructor_p;
13273 cp_token *token;
13274 ds = ds_last;
13276 /* Peek at the next token. */
13277 token = cp_lexer_peek_token (parser->lexer);
13279 /* Save the first token of the decl spec list for error
13280 reporting. */
13281 if (!start_token)
13282 start_token = token;
13283 /* Handle attributes. */
13284 if (cp_next_tokens_can_be_attribute_p (parser))
13286 /* Parse the attributes. */
13287 tree attrs = cp_parser_attributes_opt (parser);
13289 /* In a sequence of declaration specifiers, c++11 attributes
13290 appertain to the type that precede them. In that case
13291 [dcl.spec]/1 says:
13293 The attribute-specifier-seq affects the type only for
13294 the declaration it appears in, not other declarations
13295 involving the same type.
13297 But for now let's force the user to position the
13298 attribute either at the beginning of the declaration or
13299 after the declarator-id, which would clearly mean that it
13300 applies to the declarator. */
13301 if (cxx11_attribute_p (attrs))
13303 if (!found_decl_spec)
13304 /* The c++11 attribute is at the beginning of the
13305 declaration. It appertains to the entity being
13306 declared. */;
13307 else
13309 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13311 /* This is an attribute following a
13312 class-specifier. */
13313 if (decl_specs->type_definition_p)
13314 warn_misplaced_attr_for_class_type (token->location,
13315 decl_specs->type);
13316 attrs = NULL_TREE;
13318 else
13320 decl_specs->std_attributes
13321 = chainon (decl_specs->std_attributes,
13322 attrs);
13323 if (decl_specs->locations[ds_std_attribute] == 0)
13324 decl_specs->locations[ds_std_attribute] = token->location;
13326 continue;
13330 decl_specs->attributes
13331 = chainon (decl_specs->attributes,
13332 attrs);
13333 if (decl_specs->locations[ds_attribute] == 0)
13334 decl_specs->locations[ds_attribute] = token->location;
13335 continue;
13337 /* Assume we will find a decl-specifier keyword. */
13338 found_decl_spec = true;
13339 /* If the next token is an appropriate keyword, we can simply
13340 add it to the list. */
13341 switch (token->keyword)
13343 /* decl-specifier:
13344 friend
13345 constexpr */
13346 case RID_FRIEND:
13347 if (!at_class_scope_p ())
13349 error_at (token->location, "%<friend%> used outside of class");
13350 cp_lexer_purge_token (parser->lexer);
13352 else
13354 ds = ds_friend;
13355 /* Consume the token. */
13356 cp_lexer_consume_token (parser->lexer);
13358 break;
13360 case RID_CONSTEXPR:
13361 ds = ds_constexpr;
13362 cp_lexer_consume_token (parser->lexer);
13363 break;
13365 case RID_CONCEPT:
13366 ds = ds_concept;
13367 cp_lexer_consume_token (parser->lexer);
13368 break;
13370 /* function-specifier:
13371 inline
13372 virtual
13373 explicit */
13374 case RID_INLINE:
13375 case RID_VIRTUAL:
13376 case RID_EXPLICIT:
13377 cp_parser_function_specifier_opt (parser, decl_specs);
13378 break;
13380 /* decl-specifier:
13381 typedef */
13382 case RID_TYPEDEF:
13383 ds = ds_typedef;
13384 /* Consume the token. */
13385 cp_lexer_consume_token (parser->lexer);
13386 /* A constructor declarator cannot appear in a typedef. */
13387 constructor_possible_p = false;
13388 /* The "typedef" keyword can only occur in a declaration; we
13389 may as well commit at this point. */
13390 cp_parser_commit_to_tentative_parse (parser);
13392 if (decl_specs->storage_class != sc_none)
13393 decl_specs->conflicting_specifiers_p = true;
13394 break;
13396 /* storage-class-specifier:
13397 auto
13398 register
13399 static
13400 extern
13401 mutable
13403 GNU Extension:
13404 thread */
13405 case RID_AUTO:
13406 if (cxx_dialect == cxx98)
13408 /* Consume the token. */
13409 cp_lexer_consume_token (parser->lexer);
13411 /* Complain about `auto' as a storage specifier, if
13412 we're complaining about C++0x compatibility. */
13413 warning_at (token->location, OPT_Wc__11_compat, "%<auto%>"
13414 " changes meaning in C++11; please remove it");
13416 /* Set the storage class anyway. */
13417 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13418 token);
13420 else
13421 /* C++0x auto type-specifier. */
13422 found_decl_spec = false;
13423 break;
13425 case RID_REGISTER:
13426 case RID_STATIC:
13427 case RID_EXTERN:
13428 case RID_MUTABLE:
13429 /* Consume the token. */
13430 cp_lexer_consume_token (parser->lexer);
13431 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13432 token);
13433 break;
13434 case RID_THREAD:
13435 /* Consume the token. */
13436 ds = ds_thread;
13437 cp_lexer_consume_token (parser->lexer);
13438 break;
13440 default:
13441 /* We did not yet find a decl-specifier yet. */
13442 found_decl_spec = false;
13443 break;
13446 if (found_decl_spec
13447 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13448 && token->keyword != RID_CONSTEXPR)
13449 error ("decl-specifier invalid in condition");
13451 if (found_decl_spec
13452 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13453 && token->keyword != RID_MUTABLE
13454 && token->keyword != RID_CONSTEXPR)
13455 error_at (token->location, "%qD invalid in lambda",
13456 ridpointers[token->keyword]);
13458 if (ds != ds_last)
13459 set_and_check_decl_spec_loc (decl_specs, ds, token);
13461 /* Constructors are a special case. The `S' in `S()' is not a
13462 decl-specifier; it is the beginning of the declarator. */
13463 constructor_p
13464 = (!found_decl_spec
13465 && constructor_possible_p
13466 && (cp_parser_constructor_declarator_p
13467 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13469 /* If we don't have a DECL_SPEC yet, then we must be looking at
13470 a type-specifier. */
13471 if (!found_decl_spec && !constructor_p)
13473 int decl_spec_declares_class_or_enum;
13474 bool is_cv_qualifier;
13475 tree type_spec;
13477 type_spec
13478 = cp_parser_type_specifier (parser, flags,
13479 decl_specs,
13480 /*is_declaration=*/true,
13481 &decl_spec_declares_class_or_enum,
13482 &is_cv_qualifier);
13483 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13485 /* If this type-specifier referenced a user-defined type
13486 (a typedef, class-name, etc.), then we can't allow any
13487 more such type-specifiers henceforth.
13489 [dcl.spec]
13491 The longest sequence of decl-specifiers that could
13492 possibly be a type name is taken as the
13493 decl-specifier-seq of a declaration. The sequence shall
13494 be self-consistent as described below.
13496 [dcl.type]
13498 As a general rule, at most one type-specifier is allowed
13499 in the complete decl-specifier-seq of a declaration. The
13500 only exceptions are the following:
13502 -- const or volatile can be combined with any other
13503 type-specifier.
13505 -- signed or unsigned can be combined with char, long,
13506 short, or int.
13508 -- ..
13510 Example:
13512 typedef char* Pc;
13513 void g (const int Pc);
13515 Here, Pc is *not* part of the decl-specifier seq; it's
13516 the declarator. Therefore, once we see a type-specifier
13517 (other than a cv-qualifier), we forbid any additional
13518 user-defined types. We *do* still allow things like `int
13519 int' to be considered a decl-specifier-seq, and issue the
13520 error message later. */
13521 if (type_spec && !is_cv_qualifier)
13522 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13523 /* A constructor declarator cannot follow a type-specifier. */
13524 if (type_spec)
13526 constructor_possible_p = false;
13527 found_decl_spec = true;
13528 if (!is_cv_qualifier)
13529 decl_specs->any_type_specifiers_p = true;
13533 /* If we still do not have a DECL_SPEC, then there are no more
13534 decl-specifiers. */
13535 if (!found_decl_spec)
13536 break;
13538 decl_specs->any_specifiers_p = true;
13539 /* After we see one decl-specifier, further decl-specifiers are
13540 always optional. */
13541 flags |= CP_PARSER_FLAGS_OPTIONAL;
13544 /* Don't allow a friend specifier with a class definition. */
13545 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13546 && (*declares_class_or_enum & 2))
13547 error_at (decl_specs->locations[ds_friend],
13548 "class definition may not be declared a friend");
13551 /* Parse an (optional) storage-class-specifier.
13553 storage-class-specifier:
13554 auto
13555 register
13556 static
13557 extern
13558 mutable
13560 GNU Extension:
13562 storage-class-specifier:
13563 thread
13565 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13567 static tree
13568 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13570 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13572 case RID_AUTO:
13573 if (cxx_dialect != cxx98)
13574 return NULL_TREE;
13575 /* Fall through for C++98. */
13576 gcc_fallthrough ();
13578 case RID_REGISTER:
13579 case RID_STATIC:
13580 case RID_EXTERN:
13581 case RID_MUTABLE:
13582 case RID_THREAD:
13583 /* Consume the token. */
13584 return cp_lexer_consume_token (parser->lexer)->u.value;
13586 default:
13587 return NULL_TREE;
13591 /* Parse an (optional) function-specifier.
13593 function-specifier:
13594 inline
13595 virtual
13596 explicit
13598 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13599 Updates DECL_SPECS, if it is non-NULL. */
13601 static tree
13602 cp_parser_function_specifier_opt (cp_parser* parser,
13603 cp_decl_specifier_seq *decl_specs)
13605 cp_token *token = cp_lexer_peek_token (parser->lexer);
13606 switch (token->keyword)
13608 case RID_INLINE:
13609 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13610 break;
13612 case RID_VIRTUAL:
13613 /* 14.5.2.3 [temp.mem]
13615 A member function template shall not be virtual. */
13616 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13617 && current_class_type)
13618 error_at (token->location, "templates may not be %<virtual%>");
13619 else
13620 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13621 break;
13623 case RID_EXPLICIT:
13624 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13625 break;
13627 default:
13628 return NULL_TREE;
13631 /* Consume the token. */
13632 return cp_lexer_consume_token (parser->lexer)->u.value;
13635 /* Parse a linkage-specification.
13637 linkage-specification:
13638 extern string-literal { declaration-seq [opt] }
13639 extern string-literal declaration */
13641 static void
13642 cp_parser_linkage_specification (cp_parser* parser)
13644 tree linkage;
13646 /* Look for the `extern' keyword. */
13647 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13649 /* Look for the string-literal. */
13650 linkage = cp_parser_string_literal (parser, false, false);
13652 /* Transform the literal into an identifier. If the literal is a
13653 wide-character string, or contains embedded NULs, then we can't
13654 handle it as the user wants. */
13655 if (strlen (TREE_STRING_POINTER (linkage))
13656 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13658 cp_parser_error (parser, "invalid linkage-specification");
13659 /* Assume C++ linkage. */
13660 linkage = lang_name_cplusplus;
13662 else
13663 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13665 /* We're now using the new linkage. */
13666 push_lang_context (linkage);
13668 /* If the next token is a `{', then we're using the first
13669 production. */
13670 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13672 cp_ensure_no_omp_declare_simd (parser);
13673 cp_ensure_no_oacc_routine (parser);
13675 /* Consume the `{' token. */
13676 cp_lexer_consume_token (parser->lexer);
13677 /* Parse the declarations. */
13678 cp_parser_declaration_seq_opt (parser);
13679 /* Look for the closing `}'. */
13680 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13682 /* Otherwise, there's just one declaration. */
13683 else
13685 bool saved_in_unbraced_linkage_specification_p;
13687 saved_in_unbraced_linkage_specification_p
13688 = parser->in_unbraced_linkage_specification_p;
13689 parser->in_unbraced_linkage_specification_p = true;
13690 cp_parser_declaration (parser);
13691 parser->in_unbraced_linkage_specification_p
13692 = saved_in_unbraced_linkage_specification_p;
13695 /* We're done with the linkage-specification. */
13696 pop_lang_context ();
13699 /* Parse a static_assert-declaration.
13701 static_assert-declaration:
13702 static_assert ( constant-expression , string-literal ) ;
13703 static_assert ( constant-expression ) ; (C++1Z)
13705 If MEMBER_P, this static_assert is a class member. */
13707 static void
13708 cp_parser_static_assert(cp_parser *parser, bool member_p)
13710 tree condition;
13711 tree message;
13712 cp_token *token;
13713 location_t saved_loc;
13714 bool dummy;
13716 /* Peek at the `static_assert' token so we can keep track of exactly
13717 where the static assertion started. */
13718 token = cp_lexer_peek_token (parser->lexer);
13719 saved_loc = token->location;
13721 /* Look for the `static_assert' keyword. */
13722 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13723 RT_STATIC_ASSERT))
13724 return;
13726 /* We know we are in a static assertion; commit to any tentative
13727 parse. */
13728 if (cp_parser_parsing_tentatively (parser))
13729 cp_parser_commit_to_tentative_parse (parser);
13731 /* Parse the `(' starting the static assertion condition. */
13732 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13734 /* Parse the constant-expression. Allow a non-constant expression
13735 here in order to give better diagnostics in finish_static_assert. */
13736 condition =
13737 cp_parser_constant_expression (parser,
13738 /*allow_non_constant_p=*/true,
13739 /*non_constant_p=*/&dummy);
13741 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13743 if (cxx_dialect < cxx1z)
13744 pedwarn (input_location, OPT_Wpedantic,
13745 "static_assert without a message "
13746 "only available with -std=c++1z or -std=gnu++1z");
13747 /* Eat the ')' */
13748 cp_lexer_consume_token (parser->lexer);
13749 message = build_string (1, "");
13750 TREE_TYPE (message) = char_array_type_node;
13751 fix_string_type (message);
13753 else
13755 /* Parse the separating `,'. */
13756 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13758 /* Parse the string-literal message. */
13759 message = cp_parser_string_literal (parser,
13760 /*translate=*/false,
13761 /*wide_ok=*/true);
13763 /* A `)' completes the static assertion. */
13764 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13765 cp_parser_skip_to_closing_parenthesis (parser,
13766 /*recovering=*/true,
13767 /*or_comma=*/false,
13768 /*consume_paren=*/true);
13771 /* A semicolon terminates the declaration. */
13772 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13774 /* Complete the static assertion, which may mean either processing
13775 the static assert now or saving it for template instantiation. */
13776 finish_static_assert (condition, message, saved_loc, member_p);
13779 /* Parse the expression in decltype ( expression ). */
13781 static tree
13782 cp_parser_decltype_expr (cp_parser *parser,
13783 bool &id_expression_or_member_access_p)
13785 cp_token *id_expr_start_token;
13786 tree expr;
13788 /* Since we're going to preserve any side-effects from this parse, set up a
13789 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13790 in the expression. */
13791 tentative_firewall firewall (parser);
13793 /* First, try parsing an id-expression. */
13794 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13795 cp_parser_parse_tentatively (parser);
13796 expr = cp_parser_id_expression (parser,
13797 /*template_keyword_p=*/false,
13798 /*check_dependency_p=*/true,
13799 /*template_p=*/NULL,
13800 /*declarator_p=*/false,
13801 /*optional_p=*/false);
13803 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13805 bool non_integral_constant_expression_p = false;
13806 tree id_expression = expr;
13807 cp_id_kind idk;
13808 const char *error_msg;
13810 if (identifier_p (expr))
13811 /* Lookup the name we got back from the id-expression. */
13812 expr = cp_parser_lookup_name_simple (parser, expr,
13813 id_expr_start_token->location);
13815 if (expr
13816 && expr != error_mark_node
13817 && TREE_CODE (expr) != TYPE_DECL
13818 && (TREE_CODE (expr) != BIT_NOT_EXPR
13819 || !TYPE_P (TREE_OPERAND (expr, 0)))
13820 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13822 /* Complete lookup of the id-expression. */
13823 expr = (finish_id_expression
13824 (id_expression, expr, parser->scope, &idk,
13825 /*integral_constant_expression_p=*/false,
13826 /*allow_non_integral_constant_expression_p=*/true,
13827 &non_integral_constant_expression_p,
13828 /*template_p=*/false,
13829 /*done=*/true,
13830 /*address_p=*/false,
13831 /*template_arg_p=*/false,
13832 &error_msg,
13833 id_expr_start_token->location));
13835 if (expr == error_mark_node)
13836 /* We found an id-expression, but it was something that we
13837 should not have found. This is an error, not something
13838 we can recover from, so note that we found an
13839 id-expression and we'll recover as gracefully as
13840 possible. */
13841 id_expression_or_member_access_p = true;
13844 if (expr
13845 && expr != error_mark_node
13846 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13847 /* We have an id-expression. */
13848 id_expression_or_member_access_p = true;
13851 if (!id_expression_or_member_access_p)
13853 /* Abort the id-expression parse. */
13854 cp_parser_abort_tentative_parse (parser);
13856 /* Parsing tentatively, again. */
13857 cp_parser_parse_tentatively (parser);
13859 /* Parse a class member access. */
13860 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13861 /*cast_p=*/false, /*decltype*/true,
13862 /*member_access_only_p=*/true, NULL);
13864 if (expr
13865 && expr != error_mark_node
13866 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13867 /* We have an id-expression. */
13868 id_expression_or_member_access_p = true;
13871 if (id_expression_or_member_access_p)
13872 /* We have parsed the complete id-expression or member access. */
13873 cp_parser_parse_definitely (parser);
13874 else
13876 /* Abort our attempt to parse an id-expression or member access
13877 expression. */
13878 cp_parser_abort_tentative_parse (parser);
13880 /* Parse a full expression. */
13881 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13882 /*decltype_p=*/true);
13885 return expr;
13888 /* Parse a `decltype' type. Returns the type.
13890 simple-type-specifier:
13891 decltype ( expression )
13892 C++14 proposal:
13893 decltype ( auto ) */
13895 static tree
13896 cp_parser_decltype (cp_parser *parser)
13898 tree expr;
13899 bool id_expression_or_member_access_p = false;
13900 const char *saved_message;
13901 bool saved_integral_constant_expression_p;
13902 bool saved_non_integral_constant_expression_p;
13903 bool saved_greater_than_is_operator_p;
13904 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13906 if (start_token->type == CPP_DECLTYPE)
13908 /* Already parsed. */
13909 cp_lexer_consume_token (parser->lexer);
13910 return saved_checks_value (start_token->u.tree_check_value);
13913 /* Look for the `decltype' token. */
13914 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13915 return error_mark_node;
13917 /* Parse the opening `('. */
13918 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13919 return error_mark_node;
13921 /* decltype (auto) */
13922 if (cxx_dialect >= cxx14
13923 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13925 cp_lexer_consume_token (parser->lexer);
13926 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13927 return error_mark_node;
13928 expr = make_decltype_auto ();
13929 AUTO_IS_DECLTYPE (expr) = true;
13930 goto rewrite;
13933 /* Types cannot be defined in a `decltype' expression. Save away the
13934 old message. */
13935 saved_message = parser->type_definition_forbidden_message;
13937 /* And create the new one. */
13938 parser->type_definition_forbidden_message
13939 = G_("types may not be defined in %<decltype%> expressions");
13941 /* The restrictions on constant-expressions do not apply inside
13942 decltype expressions. */
13943 saved_integral_constant_expression_p
13944 = parser->integral_constant_expression_p;
13945 saved_non_integral_constant_expression_p
13946 = parser->non_integral_constant_expression_p;
13947 parser->integral_constant_expression_p = false;
13949 /* Within a parenthesized expression, a `>' token is always
13950 the greater-than operator. */
13951 saved_greater_than_is_operator_p
13952 = parser->greater_than_is_operator_p;
13953 parser->greater_than_is_operator_p = true;
13955 /* Do not actually evaluate the expression. */
13956 ++cp_unevaluated_operand;
13958 /* Do not warn about problems with the expression. */
13959 ++c_inhibit_evaluation_warnings;
13961 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
13963 /* Go back to evaluating expressions. */
13964 --cp_unevaluated_operand;
13965 --c_inhibit_evaluation_warnings;
13967 /* The `>' token might be the end of a template-id or
13968 template-parameter-list now. */
13969 parser->greater_than_is_operator_p
13970 = saved_greater_than_is_operator_p;
13972 /* Restore the old message and the integral constant expression
13973 flags. */
13974 parser->type_definition_forbidden_message = saved_message;
13975 parser->integral_constant_expression_p
13976 = saved_integral_constant_expression_p;
13977 parser->non_integral_constant_expression_p
13978 = saved_non_integral_constant_expression_p;
13980 /* Parse to the closing `)'. */
13981 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13983 cp_parser_skip_to_closing_parenthesis (parser, true, false,
13984 /*consume_paren=*/true);
13985 return error_mark_node;
13988 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
13989 tf_warning_or_error);
13991 rewrite:
13992 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
13993 it again. */
13994 start_token->type = CPP_DECLTYPE;
13995 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
13996 start_token->u.tree_check_value->value = expr;
13997 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
13998 start_token->keyword = RID_MAX;
13999 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14001 return expr;
14004 /* Special member functions [gram.special] */
14006 /* Parse a conversion-function-id.
14008 conversion-function-id:
14009 operator conversion-type-id
14011 Returns an IDENTIFIER_NODE representing the operator. */
14013 static tree
14014 cp_parser_conversion_function_id (cp_parser* parser)
14016 tree type;
14017 tree saved_scope;
14018 tree saved_qualifying_scope;
14019 tree saved_object_scope;
14020 tree pushed_scope = NULL_TREE;
14022 /* Look for the `operator' token. */
14023 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14024 return error_mark_node;
14025 /* When we parse the conversion-type-id, the current scope will be
14026 reset. However, we need that information in able to look up the
14027 conversion function later, so we save it here. */
14028 saved_scope = parser->scope;
14029 saved_qualifying_scope = parser->qualifying_scope;
14030 saved_object_scope = parser->object_scope;
14031 /* We must enter the scope of the class so that the names of
14032 entities declared within the class are available in the
14033 conversion-type-id. For example, consider:
14035 struct S {
14036 typedef int I;
14037 operator I();
14040 S::operator I() { ... }
14042 In order to see that `I' is a type-name in the definition, we
14043 must be in the scope of `S'. */
14044 if (saved_scope)
14045 pushed_scope = push_scope (saved_scope);
14046 /* Parse the conversion-type-id. */
14047 type = cp_parser_conversion_type_id (parser);
14048 /* Leave the scope of the class, if any. */
14049 if (pushed_scope)
14050 pop_scope (pushed_scope);
14051 /* Restore the saved scope. */
14052 parser->scope = saved_scope;
14053 parser->qualifying_scope = saved_qualifying_scope;
14054 parser->object_scope = saved_object_scope;
14055 /* If the TYPE is invalid, indicate failure. */
14056 if (type == error_mark_node)
14057 return error_mark_node;
14058 return mangle_conv_op_name_for_type (type);
14061 /* Parse a conversion-type-id:
14063 conversion-type-id:
14064 type-specifier-seq conversion-declarator [opt]
14066 Returns the TYPE specified. */
14068 static tree
14069 cp_parser_conversion_type_id (cp_parser* parser)
14071 tree attributes;
14072 cp_decl_specifier_seq type_specifiers;
14073 cp_declarator *declarator;
14074 tree type_specified;
14075 const char *saved_message;
14077 /* Parse the attributes. */
14078 attributes = cp_parser_attributes_opt (parser);
14080 saved_message = parser->type_definition_forbidden_message;
14081 parser->type_definition_forbidden_message
14082 = G_("types may not be defined in a conversion-type-id");
14084 /* Parse the type-specifiers. */
14085 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14086 /*is_trailing_return=*/false,
14087 &type_specifiers);
14089 parser->type_definition_forbidden_message = saved_message;
14091 /* If that didn't work, stop. */
14092 if (type_specifiers.type == error_mark_node)
14093 return error_mark_node;
14094 /* Parse the conversion-declarator. */
14095 declarator = cp_parser_conversion_declarator_opt (parser);
14097 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14098 /*initialized=*/0, &attributes);
14099 if (attributes)
14100 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14102 /* Don't give this error when parsing tentatively. This happens to
14103 work because we always parse this definitively once. */
14104 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14105 && type_uses_auto (type_specified))
14107 if (cxx_dialect < cxx14)
14109 error ("invalid use of %<auto%> in conversion operator");
14110 return error_mark_node;
14112 else if (template_parm_scope_p ())
14113 warning (0, "use of %<auto%> in member template "
14114 "conversion operator can never be deduced");
14117 return type_specified;
14120 /* Parse an (optional) conversion-declarator.
14122 conversion-declarator:
14123 ptr-operator conversion-declarator [opt]
14127 static cp_declarator *
14128 cp_parser_conversion_declarator_opt (cp_parser* parser)
14130 enum tree_code code;
14131 tree class_type, std_attributes = NULL_TREE;
14132 cp_cv_quals cv_quals;
14134 /* We don't know if there's a ptr-operator next, or not. */
14135 cp_parser_parse_tentatively (parser);
14136 /* Try the ptr-operator. */
14137 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14138 &std_attributes);
14139 /* If it worked, look for more conversion-declarators. */
14140 if (cp_parser_parse_definitely (parser))
14142 cp_declarator *declarator;
14144 /* Parse another optional declarator. */
14145 declarator = cp_parser_conversion_declarator_opt (parser);
14147 declarator = cp_parser_make_indirect_declarator
14148 (code, class_type, cv_quals, declarator, std_attributes);
14150 return declarator;
14153 return NULL;
14156 /* Parse an (optional) ctor-initializer.
14158 ctor-initializer:
14159 : mem-initializer-list
14161 Returns TRUE iff the ctor-initializer was actually present. */
14163 static bool
14164 cp_parser_ctor_initializer_opt (cp_parser* parser)
14166 /* If the next token is not a `:', then there is no
14167 ctor-initializer. */
14168 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14170 /* Do default initialization of any bases and members. */
14171 if (DECL_CONSTRUCTOR_P (current_function_decl))
14172 finish_mem_initializers (NULL_TREE);
14174 return false;
14177 /* Consume the `:' token. */
14178 cp_lexer_consume_token (parser->lexer);
14179 /* And the mem-initializer-list. */
14180 cp_parser_mem_initializer_list (parser);
14182 return true;
14185 /* Parse a mem-initializer-list.
14187 mem-initializer-list:
14188 mem-initializer ... [opt]
14189 mem-initializer ... [opt] , mem-initializer-list */
14191 static void
14192 cp_parser_mem_initializer_list (cp_parser* parser)
14194 tree mem_initializer_list = NULL_TREE;
14195 tree target_ctor = error_mark_node;
14196 cp_token *token = cp_lexer_peek_token (parser->lexer);
14198 /* Let the semantic analysis code know that we are starting the
14199 mem-initializer-list. */
14200 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14201 error_at (token->location,
14202 "only constructors take member initializers");
14204 /* Loop through the list. */
14205 while (true)
14207 tree mem_initializer;
14209 token = cp_lexer_peek_token (parser->lexer);
14210 /* Parse the mem-initializer. */
14211 mem_initializer = cp_parser_mem_initializer (parser);
14212 /* If the next token is a `...', we're expanding member initializers. */
14213 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14215 /* Consume the `...'. */
14216 cp_lexer_consume_token (parser->lexer);
14218 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14219 can be expanded but members cannot. */
14220 if (mem_initializer != error_mark_node
14221 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14223 error_at (token->location,
14224 "cannot expand initializer for member %qD",
14225 TREE_PURPOSE (mem_initializer));
14226 mem_initializer = error_mark_node;
14229 /* Construct the pack expansion type. */
14230 if (mem_initializer != error_mark_node)
14231 mem_initializer = make_pack_expansion (mem_initializer);
14233 if (target_ctor != error_mark_node
14234 && mem_initializer != error_mark_node)
14236 error ("mem-initializer for %qD follows constructor delegation",
14237 TREE_PURPOSE (mem_initializer));
14238 mem_initializer = error_mark_node;
14240 /* Look for a target constructor. */
14241 if (mem_initializer != error_mark_node
14242 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14243 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14245 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14246 if (mem_initializer_list)
14248 error ("constructor delegation follows mem-initializer for %qD",
14249 TREE_PURPOSE (mem_initializer_list));
14250 mem_initializer = error_mark_node;
14252 target_ctor = mem_initializer;
14254 /* Add it to the list, unless it was erroneous. */
14255 if (mem_initializer != error_mark_node)
14257 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14258 mem_initializer_list = mem_initializer;
14260 /* If the next token is not a `,', we're done. */
14261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14262 break;
14263 /* Consume the `,' token. */
14264 cp_lexer_consume_token (parser->lexer);
14267 /* Perform semantic analysis. */
14268 if (DECL_CONSTRUCTOR_P (current_function_decl))
14269 finish_mem_initializers (mem_initializer_list);
14272 /* Parse a mem-initializer.
14274 mem-initializer:
14275 mem-initializer-id ( expression-list [opt] )
14276 mem-initializer-id braced-init-list
14278 GNU extension:
14280 mem-initializer:
14281 ( expression-list [opt] )
14283 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14284 class) or FIELD_DECL (for a non-static data member) to initialize;
14285 the TREE_VALUE is the expression-list. An empty initialization
14286 list is represented by void_list_node. */
14288 static tree
14289 cp_parser_mem_initializer (cp_parser* parser)
14291 tree mem_initializer_id;
14292 tree expression_list;
14293 tree member;
14294 cp_token *token = cp_lexer_peek_token (parser->lexer);
14296 /* Find out what is being initialized. */
14297 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14299 permerror (token->location,
14300 "anachronistic old-style base class initializer");
14301 mem_initializer_id = NULL_TREE;
14303 else
14305 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14306 if (mem_initializer_id == error_mark_node)
14307 return mem_initializer_id;
14309 member = expand_member_init (mem_initializer_id);
14310 if (member && !DECL_P (member))
14311 in_base_initializer = 1;
14313 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14315 bool expr_non_constant_p;
14316 cp_lexer_set_source_position (parser->lexer);
14317 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14318 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14319 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14320 expression_list = build_tree_list (NULL_TREE, expression_list);
14322 else
14324 vec<tree, va_gc> *vec;
14325 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14326 /*cast_p=*/false,
14327 /*allow_expansion_p=*/true,
14328 /*non_constant_p=*/NULL);
14329 if (vec == NULL)
14330 return error_mark_node;
14331 expression_list = build_tree_list_vec (vec);
14332 release_tree_vector (vec);
14335 if (expression_list == error_mark_node)
14336 return error_mark_node;
14337 if (!expression_list)
14338 expression_list = void_type_node;
14340 in_base_initializer = 0;
14342 return member ? build_tree_list (member, expression_list) : error_mark_node;
14345 /* Parse a mem-initializer-id.
14347 mem-initializer-id:
14348 :: [opt] nested-name-specifier [opt] class-name
14349 decltype-specifier (C++11)
14350 identifier
14352 Returns a TYPE indicating the class to be initialized for the first
14353 production (and the second in C++11). Returns an IDENTIFIER_NODE
14354 indicating the data member to be initialized for the last production. */
14356 static tree
14357 cp_parser_mem_initializer_id (cp_parser* parser)
14359 bool global_scope_p;
14360 bool nested_name_specifier_p;
14361 bool template_p = false;
14362 tree id;
14364 cp_token *token = cp_lexer_peek_token (parser->lexer);
14366 /* `typename' is not allowed in this context ([temp.res]). */
14367 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14369 error_at (token->location,
14370 "keyword %<typename%> not allowed in this context (a qualified "
14371 "member initializer is implicitly a type)");
14372 cp_lexer_consume_token (parser->lexer);
14374 /* Look for the optional `::' operator. */
14375 global_scope_p
14376 = (cp_parser_global_scope_opt (parser,
14377 /*current_scope_valid_p=*/false)
14378 != NULL_TREE);
14379 /* Look for the optional nested-name-specifier. The simplest way to
14380 implement:
14382 [temp.res]
14384 The keyword `typename' is not permitted in a base-specifier or
14385 mem-initializer; in these contexts a qualified name that
14386 depends on a template-parameter is implicitly assumed to be a
14387 type name.
14389 is to assume that we have seen the `typename' keyword at this
14390 point. */
14391 nested_name_specifier_p
14392 = (cp_parser_nested_name_specifier_opt (parser,
14393 /*typename_keyword_p=*/true,
14394 /*check_dependency_p=*/true,
14395 /*type_p=*/true,
14396 /*is_declaration=*/true)
14397 != NULL_TREE);
14398 if (nested_name_specifier_p)
14399 template_p = cp_parser_optional_template_keyword (parser);
14400 /* If there is a `::' operator or a nested-name-specifier, then we
14401 are definitely looking for a class-name. */
14402 if (global_scope_p || nested_name_specifier_p)
14403 return cp_parser_class_name (parser,
14404 /*typename_keyword_p=*/true,
14405 /*template_keyword_p=*/template_p,
14406 typename_type,
14407 /*check_dependency_p=*/true,
14408 /*class_head_p=*/false,
14409 /*is_declaration=*/true);
14410 /* Otherwise, we could also be looking for an ordinary identifier. */
14411 cp_parser_parse_tentatively (parser);
14412 if (cp_lexer_next_token_is_decltype (parser->lexer))
14413 /* Try a decltype-specifier. */
14414 id = cp_parser_decltype (parser);
14415 else
14416 /* Otherwise, try a class-name. */
14417 id = cp_parser_class_name (parser,
14418 /*typename_keyword_p=*/true,
14419 /*template_keyword_p=*/false,
14420 none_type,
14421 /*check_dependency_p=*/true,
14422 /*class_head_p=*/false,
14423 /*is_declaration=*/true);
14424 /* If we found one, we're done. */
14425 if (cp_parser_parse_definitely (parser))
14426 return id;
14427 /* Otherwise, look for an ordinary identifier. */
14428 return cp_parser_identifier (parser);
14431 /* Overloading [gram.over] */
14433 /* Parse an operator-function-id.
14435 operator-function-id:
14436 operator operator
14438 Returns an IDENTIFIER_NODE for the operator which is a
14439 human-readable spelling of the identifier, e.g., `operator +'. */
14441 static cp_expr
14442 cp_parser_operator_function_id (cp_parser* parser)
14444 /* Look for the `operator' keyword. */
14445 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14446 return error_mark_node;
14447 /* And then the name of the operator itself. */
14448 return cp_parser_operator (parser);
14451 /* Return an identifier node for a user-defined literal operator.
14452 The suffix identifier is chained to the operator name identifier. */
14454 tree
14455 cp_literal_operator_id (const char* name)
14457 tree identifier;
14458 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14459 + strlen (name) + 10);
14460 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14461 identifier = get_identifier (buffer);
14463 return identifier;
14466 /* Parse an operator.
14468 operator:
14469 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14470 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14471 || ++ -- , ->* -> () []
14473 GNU Extensions:
14475 operator:
14476 <? >? <?= >?=
14478 Returns an IDENTIFIER_NODE for the operator which is a
14479 human-readable spelling of the identifier, e.g., `operator +'. */
14481 static cp_expr
14482 cp_parser_operator (cp_parser* parser)
14484 tree id = NULL_TREE;
14485 cp_token *token;
14486 bool utf8 = false;
14488 /* Peek at the next token. */
14489 token = cp_lexer_peek_token (parser->lexer);
14491 location_t start_loc = token->location;
14493 /* Figure out which operator we have. */
14494 switch (token->type)
14496 case CPP_KEYWORD:
14498 enum tree_code op;
14500 /* The keyword should be either `new' or `delete'. */
14501 if (token->keyword == RID_NEW)
14502 op = NEW_EXPR;
14503 else if (token->keyword == RID_DELETE)
14504 op = DELETE_EXPR;
14505 else
14506 break;
14508 /* Consume the `new' or `delete' token. */
14509 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14511 /* Peek at the next token. */
14512 token = cp_lexer_peek_token (parser->lexer);
14513 /* If it's a `[' token then this is the array variant of the
14514 operator. */
14515 if (token->type == CPP_OPEN_SQUARE)
14517 /* Consume the `[' token. */
14518 cp_lexer_consume_token (parser->lexer);
14519 /* Look for the `]' token. */
14520 if (cp_token *close_token
14521 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14522 end_loc = close_token->location;
14523 id = cp_operator_id (op == NEW_EXPR
14524 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14526 /* Otherwise, we have the non-array variant. */
14527 else
14528 id = cp_operator_id (op);
14530 location_t loc = make_location (start_loc, start_loc, end_loc);
14532 return cp_expr (id, loc);
14535 case CPP_PLUS:
14536 id = cp_operator_id (PLUS_EXPR);
14537 break;
14539 case CPP_MINUS:
14540 id = cp_operator_id (MINUS_EXPR);
14541 break;
14543 case CPP_MULT:
14544 id = cp_operator_id (MULT_EXPR);
14545 break;
14547 case CPP_DIV:
14548 id = cp_operator_id (TRUNC_DIV_EXPR);
14549 break;
14551 case CPP_MOD:
14552 id = cp_operator_id (TRUNC_MOD_EXPR);
14553 break;
14555 case CPP_XOR:
14556 id = cp_operator_id (BIT_XOR_EXPR);
14557 break;
14559 case CPP_AND:
14560 id = cp_operator_id (BIT_AND_EXPR);
14561 break;
14563 case CPP_OR:
14564 id = cp_operator_id (BIT_IOR_EXPR);
14565 break;
14567 case CPP_COMPL:
14568 id = cp_operator_id (BIT_NOT_EXPR);
14569 break;
14571 case CPP_NOT:
14572 id = cp_operator_id (TRUTH_NOT_EXPR);
14573 break;
14575 case CPP_EQ:
14576 id = cp_assignment_operator_id (NOP_EXPR);
14577 break;
14579 case CPP_LESS:
14580 id = cp_operator_id (LT_EXPR);
14581 break;
14583 case CPP_GREATER:
14584 id = cp_operator_id (GT_EXPR);
14585 break;
14587 case CPP_PLUS_EQ:
14588 id = cp_assignment_operator_id (PLUS_EXPR);
14589 break;
14591 case CPP_MINUS_EQ:
14592 id = cp_assignment_operator_id (MINUS_EXPR);
14593 break;
14595 case CPP_MULT_EQ:
14596 id = cp_assignment_operator_id (MULT_EXPR);
14597 break;
14599 case CPP_DIV_EQ:
14600 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14601 break;
14603 case CPP_MOD_EQ:
14604 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14605 break;
14607 case CPP_XOR_EQ:
14608 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14609 break;
14611 case CPP_AND_EQ:
14612 id = cp_assignment_operator_id (BIT_AND_EXPR);
14613 break;
14615 case CPP_OR_EQ:
14616 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14617 break;
14619 case CPP_LSHIFT:
14620 id = cp_operator_id (LSHIFT_EXPR);
14621 break;
14623 case CPP_RSHIFT:
14624 id = cp_operator_id (RSHIFT_EXPR);
14625 break;
14627 case CPP_LSHIFT_EQ:
14628 id = cp_assignment_operator_id (LSHIFT_EXPR);
14629 break;
14631 case CPP_RSHIFT_EQ:
14632 id = cp_assignment_operator_id (RSHIFT_EXPR);
14633 break;
14635 case CPP_EQ_EQ:
14636 id = cp_operator_id (EQ_EXPR);
14637 break;
14639 case CPP_NOT_EQ:
14640 id = cp_operator_id (NE_EXPR);
14641 break;
14643 case CPP_LESS_EQ:
14644 id = cp_operator_id (LE_EXPR);
14645 break;
14647 case CPP_GREATER_EQ:
14648 id = cp_operator_id (GE_EXPR);
14649 break;
14651 case CPP_AND_AND:
14652 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14653 break;
14655 case CPP_OR_OR:
14656 id = cp_operator_id (TRUTH_ORIF_EXPR);
14657 break;
14659 case CPP_PLUS_PLUS:
14660 id = cp_operator_id (POSTINCREMENT_EXPR);
14661 break;
14663 case CPP_MINUS_MINUS:
14664 id = cp_operator_id (PREDECREMENT_EXPR);
14665 break;
14667 case CPP_COMMA:
14668 id = cp_operator_id (COMPOUND_EXPR);
14669 break;
14671 case CPP_DEREF_STAR:
14672 id = cp_operator_id (MEMBER_REF);
14673 break;
14675 case CPP_DEREF:
14676 id = cp_operator_id (COMPONENT_REF);
14677 break;
14679 case CPP_OPEN_PAREN:
14680 /* Consume the `('. */
14681 cp_lexer_consume_token (parser->lexer);
14682 /* Look for the matching `)'. */
14683 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14684 return cp_operator_id (CALL_EXPR);
14686 case CPP_OPEN_SQUARE:
14687 /* Consume the `['. */
14688 cp_lexer_consume_token (parser->lexer);
14689 /* Look for the matching `]'. */
14690 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14691 return cp_operator_id (ARRAY_REF);
14693 case CPP_UTF8STRING:
14694 case CPP_UTF8STRING_USERDEF:
14695 utf8 = true;
14696 /* FALLTHRU */
14697 case CPP_STRING:
14698 case CPP_WSTRING:
14699 case CPP_STRING16:
14700 case CPP_STRING32:
14701 case CPP_STRING_USERDEF:
14702 case CPP_WSTRING_USERDEF:
14703 case CPP_STRING16_USERDEF:
14704 case CPP_STRING32_USERDEF:
14706 tree str, string_tree;
14707 int sz, len;
14709 if (cxx_dialect == cxx98)
14710 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14712 /* Consume the string. */
14713 str = cp_parser_string_literal (parser, /*translate=*/true,
14714 /*wide_ok=*/true, /*lookup_udlit=*/false);
14715 if (str == error_mark_node)
14716 return error_mark_node;
14717 else if (TREE_CODE (str) == USERDEF_LITERAL)
14719 string_tree = USERDEF_LITERAL_VALUE (str);
14720 id = USERDEF_LITERAL_SUFFIX_ID (str);
14722 else
14724 string_tree = str;
14725 /* Look for the suffix identifier. */
14726 token = cp_lexer_peek_token (parser->lexer);
14727 if (token->type == CPP_NAME)
14728 id = cp_parser_identifier (parser);
14729 else if (token->type == CPP_KEYWORD)
14731 error ("unexpected keyword;"
14732 " remove space between quotes and suffix identifier");
14733 return error_mark_node;
14735 else
14737 error ("expected suffix identifier");
14738 return error_mark_node;
14741 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14742 (TREE_TYPE (TREE_TYPE (string_tree))));
14743 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14744 if (len != 0)
14746 error ("expected empty string after %<operator%> keyword");
14747 return error_mark_node;
14749 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14750 != char_type_node)
14752 error ("invalid encoding prefix in literal operator");
14753 return error_mark_node;
14755 if (id != error_mark_node)
14757 const char *name = IDENTIFIER_POINTER (id);
14758 id = cp_literal_operator_id (name);
14760 return id;
14763 default:
14764 /* Anything else is an error. */
14765 break;
14768 /* If we have selected an identifier, we need to consume the
14769 operator token. */
14770 if (id)
14771 cp_lexer_consume_token (parser->lexer);
14772 /* Otherwise, no valid operator name was present. */
14773 else
14775 cp_parser_error (parser, "expected operator");
14776 id = error_mark_node;
14779 return cp_expr (id, start_loc);
14782 /* Parse a template-declaration.
14784 template-declaration:
14785 export [opt] template < template-parameter-list > declaration
14787 If MEMBER_P is TRUE, this template-declaration occurs within a
14788 class-specifier.
14790 The grammar rule given by the standard isn't correct. What
14791 is really meant is:
14793 template-declaration:
14794 export [opt] template-parameter-list-seq
14795 decl-specifier-seq [opt] init-declarator [opt] ;
14796 export [opt] template-parameter-list-seq
14797 function-definition
14799 template-parameter-list-seq:
14800 template-parameter-list-seq [opt]
14801 template < template-parameter-list >
14803 Concept Extensions:
14805 template-parameter-list-seq:
14806 template < template-parameter-list > requires-clause [opt]
14808 requires-clause:
14809 requires logical-or-expression */
14811 static void
14812 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14814 /* Check for `export'. */
14815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14817 /* Consume the `export' token. */
14818 cp_lexer_consume_token (parser->lexer);
14819 /* Warn that we do not support `export'. */
14820 warning (0, "keyword %<export%> not implemented, and will be ignored");
14823 cp_parser_template_declaration_after_export (parser, member_p);
14826 /* Parse a template-parameter-list.
14828 template-parameter-list:
14829 template-parameter
14830 template-parameter-list , template-parameter
14832 Returns a TREE_LIST. Each node represents a template parameter.
14833 The nodes are connected via their TREE_CHAINs. */
14835 static tree
14836 cp_parser_template_parameter_list (cp_parser* parser)
14838 tree parameter_list = NULL_TREE;
14840 begin_template_parm_list ();
14842 /* The loop below parses the template parms. We first need to know
14843 the total number of template parms to be able to compute proper
14844 canonical types of each dependent type. So after the loop, when
14845 we know the total number of template parms,
14846 end_template_parm_list computes the proper canonical types and
14847 fixes up the dependent types accordingly. */
14848 while (true)
14850 tree parameter;
14851 bool is_non_type;
14852 bool is_parameter_pack;
14853 location_t parm_loc;
14855 /* Parse the template-parameter. */
14856 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14857 parameter = cp_parser_template_parameter (parser,
14858 &is_non_type,
14859 &is_parameter_pack);
14860 /* Add it to the list. */
14861 if (parameter != error_mark_node)
14862 parameter_list = process_template_parm (parameter_list,
14863 parm_loc,
14864 parameter,
14865 is_non_type,
14866 is_parameter_pack);
14867 else
14869 tree err_parm = build_tree_list (parameter, parameter);
14870 parameter_list = chainon (parameter_list, err_parm);
14873 /* If the next token is not a `,', we're done. */
14874 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14875 break;
14876 /* Otherwise, consume the `,' token. */
14877 cp_lexer_consume_token (parser->lexer);
14880 return end_template_parm_list (parameter_list);
14883 /* Parse a introduction-list.
14885 introduction-list:
14886 introduced-parameter
14887 introduction-list , introduced-parameter
14889 introduced-parameter:
14890 ...[opt] identifier
14892 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14893 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14894 WILDCARD_DECL will also have DECL_NAME set and token location in
14895 DECL_SOURCE_LOCATION. */
14897 static tree
14898 cp_parser_introduction_list (cp_parser *parser)
14900 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14902 while (true)
14904 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14905 if (is_pack)
14906 cp_lexer_consume_token (parser->lexer);
14908 /* Build placeholder. */
14909 tree parm = build_nt (WILDCARD_DECL);
14910 DECL_SOURCE_LOCATION (parm)
14911 = cp_lexer_peek_token (parser->lexer)->location;
14912 DECL_NAME (parm) = cp_parser_identifier (parser);
14913 WILDCARD_PACK_P (parm) = is_pack;
14914 vec_safe_push (introduction_vec, parm);
14916 /* If the next token is not a `,', we're done. */
14917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14918 break;
14919 /* Otherwise, consume the `,' token. */
14920 cp_lexer_consume_token (parser->lexer);
14923 /* Convert the vec into a TREE_VEC. */
14924 tree introduction_list = make_tree_vec (introduction_vec->length ());
14925 unsigned int n;
14926 tree parm;
14927 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14928 TREE_VEC_ELT (introduction_list, n) = parm;
14930 release_tree_vector (introduction_vec);
14931 return introduction_list;
14934 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14935 is an abstract declarator. */
14937 static inline cp_declarator*
14938 get_id_declarator (cp_declarator *declarator)
14940 cp_declarator *d = declarator;
14941 while (d && d->kind != cdk_id)
14942 d = d->declarator;
14943 return d;
14946 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14947 is an abstract declarator. */
14949 static inline tree
14950 get_unqualified_id (cp_declarator *declarator)
14952 declarator = get_id_declarator (declarator);
14953 if (declarator)
14954 return declarator->u.id.unqualified_name;
14955 else
14956 return NULL_TREE;
14959 /* Returns true if DECL represents a constrained-parameter. */
14961 static inline bool
14962 is_constrained_parameter (tree decl)
14964 return (decl
14965 && TREE_CODE (decl) == TYPE_DECL
14966 && CONSTRAINED_PARM_CONCEPT (decl)
14967 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
14970 /* Returns true if PARM declares a constrained-parameter. */
14972 static inline bool
14973 is_constrained_parameter (cp_parameter_declarator *parm)
14975 return is_constrained_parameter (parm->decl_specifiers.type);
14978 /* Check that the type parameter is only a declarator-id, and that its
14979 type is not cv-qualified. */
14981 bool
14982 cp_parser_check_constrained_type_parm (cp_parser *parser,
14983 cp_parameter_declarator *parm)
14985 if (!parm->declarator)
14986 return true;
14988 if (parm->declarator->kind != cdk_id)
14990 cp_parser_error (parser, "invalid constrained type parameter");
14991 return false;
14994 /* Don't allow cv-qualified type parameters. */
14995 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
14996 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
14998 cp_parser_error (parser, "cv-qualified type parameter");
14999 return false;
15002 return true;
15005 /* Finish parsing/processing a template type parameter and checking
15006 various restrictions. */
15008 static inline tree
15009 cp_parser_constrained_type_template_parm (cp_parser *parser,
15010 tree id,
15011 cp_parameter_declarator* parmdecl)
15013 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15014 return finish_template_type_parm (class_type_node, id);
15015 else
15016 return error_mark_node;
15019 static tree
15020 finish_constrained_template_template_parm (tree proto, tree id)
15022 /* FIXME: This should probably be copied, and we may need to adjust
15023 the template parameter depths. */
15024 tree saved_parms = current_template_parms;
15025 begin_template_parm_list ();
15026 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15027 end_template_parm_list ();
15029 tree parm = finish_template_template_parm (class_type_node, id);
15030 current_template_parms = saved_parms;
15032 return parm;
15035 /* Finish parsing/processing a template template parameter by borrowing
15036 the template parameter list from the prototype parameter. */
15038 static tree
15039 cp_parser_constrained_template_template_parm (cp_parser *parser,
15040 tree proto,
15041 tree id,
15042 cp_parameter_declarator *parmdecl)
15044 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15045 return error_mark_node;
15046 return finish_constrained_template_template_parm (proto, id);
15049 /* Create a new non-type template parameter from the given PARM
15050 declarator. */
15052 static tree
15053 constrained_non_type_template_parm (bool *is_non_type,
15054 cp_parameter_declarator *parm)
15056 *is_non_type = true;
15057 cp_declarator *decl = parm->declarator;
15058 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15059 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15060 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15063 /* Build a constrained template parameter based on the PARMDECL
15064 declarator. The type of PARMDECL is the constrained type, which
15065 refers to the prototype template parameter that ultimately
15066 specifies the type of the declared parameter. */
15068 static tree
15069 finish_constrained_parameter (cp_parser *parser,
15070 cp_parameter_declarator *parmdecl,
15071 bool *is_non_type,
15072 bool *is_parameter_pack)
15074 tree decl = parmdecl->decl_specifiers.type;
15075 tree id = get_unqualified_id (parmdecl->declarator);
15076 tree def = parmdecl->default_argument;
15077 tree proto = DECL_INITIAL (decl);
15079 /* A template parameter constrained by a variadic concept shall also
15080 be declared as a template parameter pack. */
15081 bool is_variadic = template_parameter_pack_p (proto);
15082 if (is_variadic && !*is_parameter_pack)
15083 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15085 /* Build the parameter. Return an error if the declarator was invalid. */
15086 tree parm;
15087 if (TREE_CODE (proto) == TYPE_DECL)
15088 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15089 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15090 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15091 parmdecl);
15092 else
15093 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15094 if (parm == error_mark_node)
15095 return error_mark_node;
15097 /* Finish the parameter decl and create a node attaching the
15098 default argument and constraint. */
15099 parm = build_tree_list (def, parm);
15100 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15102 return parm;
15105 /* Returns true if the parsed type actually represents the declaration
15106 of a type template-parameter. */
15108 static inline bool
15109 declares_constrained_type_template_parameter (tree type)
15111 return (is_constrained_parameter (type)
15112 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15116 /* Returns true if the parsed type actually represents the declaration of
15117 a template template-parameter. */
15119 static bool
15120 declares_constrained_template_template_parameter (tree type)
15122 return (is_constrained_parameter (type)
15123 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15126 /* Parse a default argument for a type template-parameter.
15127 Note that diagnostics are handled in cp_parser_template_parameter. */
15129 static tree
15130 cp_parser_default_type_template_argument (cp_parser *parser)
15132 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15134 /* Consume the `=' token. */
15135 cp_lexer_consume_token (parser->lexer);
15137 cp_token *token = cp_lexer_peek_token (parser->lexer);
15139 /* Parse the default-argument. */
15140 push_deferring_access_checks (dk_no_deferred);
15141 tree default_argument = cp_parser_type_id (parser);
15142 pop_deferring_access_checks ();
15144 if (flag_concepts && type_uses_auto (default_argument))
15146 error_at (token->location,
15147 "invalid use of %<auto%> in default template argument");
15148 return error_mark_node;
15151 return default_argument;
15154 /* Parse a default argument for a template template-parameter. */
15156 static tree
15157 cp_parser_default_template_template_argument (cp_parser *parser)
15159 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15161 bool is_template;
15163 /* Consume the `='. */
15164 cp_lexer_consume_token (parser->lexer);
15165 /* Parse the id-expression. */
15166 push_deferring_access_checks (dk_no_deferred);
15167 /* save token before parsing the id-expression, for error
15168 reporting */
15169 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15170 tree default_argument
15171 = cp_parser_id_expression (parser,
15172 /*template_keyword_p=*/false,
15173 /*check_dependency_p=*/true,
15174 /*template_p=*/&is_template,
15175 /*declarator_p=*/false,
15176 /*optional_p=*/false);
15177 if (TREE_CODE (default_argument) == TYPE_DECL)
15178 /* If the id-expression was a template-id that refers to
15179 a template-class, we already have the declaration here,
15180 so no further lookup is needed. */
15182 else
15183 /* Look up the name. */
15184 default_argument
15185 = cp_parser_lookup_name (parser, default_argument,
15186 none_type,
15187 /*is_template=*/is_template,
15188 /*is_namespace=*/false,
15189 /*check_dependency=*/true,
15190 /*ambiguous_decls=*/NULL,
15191 token->location);
15192 /* See if the default argument is valid. */
15193 default_argument = check_template_template_default_arg (default_argument);
15194 pop_deferring_access_checks ();
15195 return default_argument;
15198 /* Parse a template-parameter.
15200 template-parameter:
15201 type-parameter
15202 parameter-declaration
15204 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15205 the parameter. The TREE_PURPOSE is the default value, if any.
15206 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15207 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15208 set to true iff this parameter is a parameter pack. */
15210 static tree
15211 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15212 bool *is_parameter_pack)
15214 cp_token *token;
15215 cp_parameter_declarator *parameter_declarator;
15216 tree parm;
15218 /* Assume it is a type parameter or a template parameter. */
15219 *is_non_type = false;
15220 /* Assume it not a parameter pack. */
15221 *is_parameter_pack = false;
15222 /* Peek at the next token. */
15223 token = cp_lexer_peek_token (parser->lexer);
15224 /* If it is `template', we have a type-parameter. */
15225 if (token->keyword == RID_TEMPLATE)
15226 return cp_parser_type_parameter (parser, is_parameter_pack);
15227 /* If it is `class' or `typename' we do not know yet whether it is a
15228 type parameter or a non-type parameter. Consider:
15230 template <typename T, typename T::X X> ...
15234 template <class C, class D*> ...
15236 Here, the first parameter is a type parameter, and the second is
15237 a non-type parameter. We can tell by looking at the token after
15238 the identifier -- if it is a `,', `=', or `>' then we have a type
15239 parameter. */
15240 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15242 /* Peek at the token after `class' or `typename'. */
15243 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15244 /* If it's an ellipsis, we have a template type parameter
15245 pack. */
15246 if (token->type == CPP_ELLIPSIS)
15247 return cp_parser_type_parameter (parser, is_parameter_pack);
15248 /* If it's an identifier, skip it. */
15249 if (token->type == CPP_NAME)
15250 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15251 /* Now, see if the token looks like the end of a template
15252 parameter. */
15253 if (token->type == CPP_COMMA
15254 || token->type == CPP_EQ
15255 || token->type == CPP_GREATER)
15256 return cp_parser_type_parameter (parser, is_parameter_pack);
15259 /* Otherwise, it is a non-type parameter or a constrained parameter.
15261 [temp.param]
15263 When parsing a default template-argument for a non-type
15264 template-parameter, the first non-nested `>' is taken as the end
15265 of the template parameter-list rather than a greater-than
15266 operator. */
15267 parameter_declarator
15268 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15269 /*parenthesized_p=*/NULL);
15271 if (!parameter_declarator)
15272 return error_mark_node;
15274 /* If the parameter declaration is marked as a parameter pack, set
15275 *IS_PARAMETER_PACK to notify the caller. */
15276 if (parameter_declarator->template_parameter_pack_p)
15277 *is_parameter_pack = true;
15279 if (parameter_declarator->default_argument)
15281 /* Can happen in some cases of erroneous input (c++/34892). */
15282 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15283 /* Consume the `...' for better error recovery. */
15284 cp_lexer_consume_token (parser->lexer);
15287 // The parameter may have been constrained.
15288 if (is_constrained_parameter (parameter_declarator))
15289 return finish_constrained_parameter (parser,
15290 parameter_declarator,
15291 is_non_type,
15292 is_parameter_pack);
15294 // Now we're sure that the parameter is a non-type parameter.
15295 *is_non_type = true;
15297 parm = grokdeclarator (parameter_declarator->declarator,
15298 &parameter_declarator->decl_specifiers,
15299 TPARM, /*initialized=*/0,
15300 /*attrlist=*/NULL);
15301 if (parm == error_mark_node)
15302 return error_mark_node;
15304 return build_tree_list (parameter_declarator->default_argument, parm);
15307 /* Parse a type-parameter.
15309 type-parameter:
15310 class identifier [opt]
15311 class identifier [opt] = type-id
15312 typename identifier [opt]
15313 typename identifier [opt] = type-id
15314 template < template-parameter-list > class identifier [opt]
15315 template < template-parameter-list > class identifier [opt]
15316 = id-expression
15318 GNU Extension (variadic templates):
15320 type-parameter:
15321 class ... identifier [opt]
15322 typename ... identifier [opt]
15324 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15325 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15326 the declaration of the parameter.
15328 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15330 static tree
15331 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15333 cp_token *token;
15334 tree parameter;
15336 /* Look for a keyword to tell us what kind of parameter this is. */
15337 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15338 if (!token)
15339 return error_mark_node;
15341 switch (token->keyword)
15343 case RID_CLASS:
15344 case RID_TYPENAME:
15346 tree identifier;
15347 tree default_argument;
15349 /* If the next token is an ellipsis, we have a template
15350 argument pack. */
15351 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15353 /* Consume the `...' token. */
15354 cp_lexer_consume_token (parser->lexer);
15355 maybe_warn_variadic_templates ();
15357 *is_parameter_pack = true;
15360 /* If the next token is an identifier, then it names the
15361 parameter. */
15362 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15363 identifier = cp_parser_identifier (parser);
15364 else
15365 identifier = NULL_TREE;
15367 /* Create the parameter. */
15368 parameter = finish_template_type_parm (class_type_node, identifier);
15370 /* If the next token is an `=', we have a default argument. */
15371 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15373 default_argument
15374 = cp_parser_default_type_template_argument (parser);
15376 /* Template parameter packs cannot have default
15377 arguments. */
15378 if (*is_parameter_pack)
15380 if (identifier)
15381 error_at (token->location,
15382 "template parameter pack %qD cannot have a "
15383 "default argument", identifier);
15384 else
15385 error_at (token->location,
15386 "template parameter packs cannot have "
15387 "default arguments");
15388 default_argument = NULL_TREE;
15390 else if (check_for_bare_parameter_packs (default_argument))
15391 default_argument = error_mark_node;
15393 else
15394 default_argument = NULL_TREE;
15396 /* Create the combined representation of the parameter and the
15397 default argument. */
15398 parameter = build_tree_list (default_argument, parameter);
15400 break;
15402 case RID_TEMPLATE:
15404 tree identifier;
15405 tree default_argument;
15407 /* Look for the `<'. */
15408 cp_parser_require (parser, CPP_LESS, RT_LESS);
15409 /* Parse the template-parameter-list. */
15410 cp_parser_template_parameter_list (parser);
15411 /* Look for the `>'. */
15412 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15414 // If template requirements are present, parse them.
15415 if (flag_concepts)
15417 tree reqs = get_shorthand_constraints (current_template_parms);
15418 if (tree r = cp_parser_requires_clause_opt (parser))
15419 reqs = conjoin_constraints (reqs, normalize_expression (r));
15420 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15423 /* Look for the `class' or 'typename' keywords. */
15424 cp_parser_type_parameter_key (parser);
15425 /* If the next token is an ellipsis, we have a template
15426 argument pack. */
15427 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15429 /* Consume the `...' token. */
15430 cp_lexer_consume_token (parser->lexer);
15431 maybe_warn_variadic_templates ();
15433 *is_parameter_pack = true;
15435 /* If the next token is an `=', then there is a
15436 default-argument. If the next token is a `>', we are at
15437 the end of the parameter-list. If the next token is a `,',
15438 then we are at the end of this parameter. */
15439 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15440 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15441 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15443 identifier = cp_parser_identifier (parser);
15444 /* Treat invalid names as if the parameter were nameless. */
15445 if (identifier == error_mark_node)
15446 identifier = NULL_TREE;
15448 else
15449 identifier = NULL_TREE;
15451 /* Create the template parameter. */
15452 parameter = finish_template_template_parm (class_type_node,
15453 identifier);
15455 /* If the next token is an `=', then there is a
15456 default-argument. */
15457 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15459 default_argument
15460 = cp_parser_default_template_template_argument (parser);
15462 /* Template parameter packs cannot have default
15463 arguments. */
15464 if (*is_parameter_pack)
15466 if (identifier)
15467 error_at (token->location,
15468 "template parameter pack %qD cannot "
15469 "have a default argument",
15470 identifier);
15471 else
15472 error_at (token->location, "template parameter packs cannot "
15473 "have default arguments");
15474 default_argument = NULL_TREE;
15477 else
15478 default_argument = NULL_TREE;
15480 /* Create the combined representation of the parameter and the
15481 default argument. */
15482 parameter = build_tree_list (default_argument, parameter);
15484 break;
15486 default:
15487 gcc_unreachable ();
15488 break;
15491 return parameter;
15494 /* Parse a template-id.
15496 template-id:
15497 template-name < template-argument-list [opt] >
15499 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15500 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15501 returned. Otherwise, if the template-name names a function, or set
15502 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15503 names a class, returns a TYPE_DECL for the specialization.
15505 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15506 uninstantiated templates. */
15508 static tree
15509 cp_parser_template_id (cp_parser *parser,
15510 bool template_keyword_p,
15511 bool check_dependency_p,
15512 enum tag_types tag_type,
15513 bool is_declaration)
15515 tree templ;
15516 tree arguments;
15517 tree template_id;
15518 cp_token_position start_of_id = 0;
15519 cp_token *next_token = NULL, *next_token_2 = NULL;
15520 bool is_identifier;
15522 /* If the next token corresponds to a template-id, there is no need
15523 to reparse it. */
15524 next_token = cp_lexer_peek_token (parser->lexer);
15525 if (next_token->type == CPP_TEMPLATE_ID)
15527 cp_lexer_consume_token (parser->lexer);
15528 return saved_checks_value (next_token->u.tree_check_value);
15531 /* Avoid performing name lookup if there is no possibility of
15532 finding a template-id. */
15533 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
15534 || (next_token->type == CPP_NAME
15535 && !cp_parser_nth_token_starts_template_argument_list_p
15536 (parser, 2)))
15538 cp_parser_error (parser, "expected template-id");
15539 return error_mark_node;
15542 /* Remember where the template-id starts. */
15543 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15544 start_of_id = cp_lexer_token_position (parser->lexer, false);
15546 push_deferring_access_checks (dk_deferred);
15548 /* Parse the template-name. */
15549 is_identifier = false;
15550 templ = cp_parser_template_name (parser, template_keyword_p,
15551 check_dependency_p,
15552 is_declaration,
15553 tag_type,
15554 &is_identifier);
15555 if (templ == error_mark_node || is_identifier)
15557 pop_deferring_access_checks ();
15558 return templ;
15561 /* Since we're going to preserve any side-effects from this parse, set up a
15562 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15563 in the template arguments. */
15564 tentative_firewall firewall (parser);
15566 /* If we find the sequence `[:' after a template-name, it's probably
15567 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15568 parse correctly the argument list. */
15569 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15570 == CPP_OPEN_SQUARE)
15571 && next_token->flags & DIGRAPH
15572 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15573 == CPP_COLON)
15574 && !(next_token_2->flags & PREV_WHITE))
15576 cp_parser_parse_tentatively (parser);
15577 /* Change `:' into `::'. */
15578 next_token_2->type = CPP_SCOPE;
15579 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15580 CPP_LESS. */
15581 cp_lexer_consume_token (parser->lexer);
15583 /* Parse the arguments. */
15584 arguments = cp_parser_enclosed_template_argument_list (parser);
15585 if (!cp_parser_parse_definitely (parser))
15587 /* If we couldn't parse an argument list, then we revert our changes
15588 and return simply an error. Maybe this is not a template-id
15589 after all. */
15590 next_token_2->type = CPP_COLON;
15591 cp_parser_error (parser, "expected %<<%>");
15592 pop_deferring_access_checks ();
15593 return error_mark_node;
15595 /* Otherwise, emit an error about the invalid digraph, but continue
15596 parsing because we got our argument list. */
15597 if (permerror (next_token->location,
15598 "%<<::%> cannot begin a template-argument list"))
15600 static bool hint = false;
15601 inform (next_token->location,
15602 "%<<:%> is an alternate spelling for %<[%>."
15603 " Insert whitespace between %<<%> and %<::%>");
15604 if (!hint && !flag_permissive)
15606 inform (next_token->location, "(if you use %<-fpermissive%> "
15607 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15608 "accept your code)");
15609 hint = true;
15613 else
15615 /* Look for the `<' that starts the template-argument-list. */
15616 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15618 pop_deferring_access_checks ();
15619 return error_mark_node;
15621 /* Parse the arguments. */
15622 arguments = cp_parser_enclosed_template_argument_list (parser);
15625 /* Build a representation of the specialization. */
15626 if (identifier_p (templ))
15627 template_id = build_min_nt_loc (next_token->location,
15628 TEMPLATE_ID_EXPR,
15629 templ, arguments);
15630 else if (DECL_TYPE_TEMPLATE_P (templ)
15631 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15633 bool entering_scope;
15634 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15635 template (rather than some instantiation thereof) only if
15636 is not nested within some other construct. For example, in
15637 "template <typename T> void f(T) { A<T>::", A<T> is just an
15638 instantiation of A. */
15639 entering_scope = (template_parm_scope_p ()
15640 && cp_lexer_next_token_is (parser->lexer,
15641 CPP_SCOPE));
15642 template_id
15643 = finish_template_type (templ, arguments, entering_scope);
15645 /* A template-like identifier may be a partial concept id. */
15646 else if (flag_concepts
15647 && (template_id = (cp_parser_maybe_partial_concept_id
15648 (parser, templ, arguments))))
15649 return template_id;
15650 else if (variable_template_p (templ))
15652 template_id = lookup_template_variable (templ, arguments);
15653 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15654 SET_EXPR_LOCATION (template_id, next_token->location);
15656 else
15658 /* If it's not a class-template or a template-template, it should be
15659 a function-template. */
15660 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15661 || TREE_CODE (templ) == OVERLOAD
15662 || BASELINK_P (templ)));
15664 template_id = lookup_template_function (templ, arguments);
15665 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15666 SET_EXPR_LOCATION (template_id, next_token->location);
15669 /* If parsing tentatively, replace the sequence of tokens that makes
15670 up the template-id with a CPP_TEMPLATE_ID token. That way,
15671 should we re-parse the token stream, we will not have to repeat
15672 the effort required to do the parse, nor will we issue duplicate
15673 error messages about problems during instantiation of the
15674 template. */
15675 if (start_of_id
15676 /* Don't do this if we had a parse error in a declarator; re-parsing
15677 might succeed if a name changes meaning (60361). */
15678 && !(cp_parser_error_occurred (parser)
15679 && cp_parser_parsing_tentatively (parser)
15680 && parser->in_declarator_p))
15682 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
15684 /* Reset the contents of the START_OF_ID token. */
15685 token->type = CPP_TEMPLATE_ID;
15687 /* Update the location to be of the form:
15688 template-name < template-argument-list [opt] >
15689 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15690 with caret == start at the start of the template-name,
15691 ranging until the closing '>'. */
15692 location_t finish_loc
15693 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15694 location_t combined_loc
15695 = make_location (token->location, token->location, finish_loc);
15696 token->location = combined_loc;
15698 /* We must mark the lookup as kept, so we don't throw it away on
15699 the first parse. */
15700 if (is_overloaded_fn (template_id))
15701 lookup_keep (get_fns (template_id), true);
15703 /* Retrieve any deferred checks. Do not pop this access checks yet
15704 so the memory will not be reclaimed during token replacing below. */
15705 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15706 token->u.tree_check_value->value = template_id;
15707 token->u.tree_check_value->checks = get_deferred_access_checks ();
15708 token->keyword = RID_MAX;
15710 /* Purge all subsequent tokens. */
15711 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15713 /* ??? Can we actually assume that, if template_id ==
15714 error_mark_node, we will have issued a diagnostic to the
15715 user, as opposed to simply marking the tentative parse as
15716 failed? */
15717 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15718 error_at (token->location, "parse error in template argument list");
15721 pop_to_parent_deferring_access_checks ();
15722 return template_id;
15725 /* Parse a template-name.
15727 template-name:
15728 identifier
15730 The standard should actually say:
15732 template-name:
15733 identifier
15734 operator-function-id
15736 A defect report has been filed about this issue.
15738 A conversion-function-id cannot be a template name because they cannot
15739 be part of a template-id. In fact, looking at this code:
15741 a.operator K<int>()
15743 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15744 It is impossible to call a templated conversion-function-id with an
15745 explicit argument list, since the only allowed template parameter is
15746 the type to which it is converting.
15748 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15749 `template' keyword, in a construction like:
15751 T::template f<3>()
15753 In that case `f' is taken to be a template-name, even though there
15754 is no way of knowing for sure.
15756 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15757 name refers to a set of overloaded functions, at least one of which
15758 is a template, or an IDENTIFIER_NODE with the name of the template,
15759 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15760 names are looked up inside uninstantiated templates. */
15762 static tree
15763 cp_parser_template_name (cp_parser* parser,
15764 bool template_keyword_p,
15765 bool check_dependency_p,
15766 bool is_declaration,
15767 enum tag_types tag_type,
15768 bool *is_identifier)
15770 tree identifier;
15771 tree decl;
15772 cp_token *token = cp_lexer_peek_token (parser->lexer);
15774 /* If the next token is `operator', then we have either an
15775 operator-function-id or a conversion-function-id. */
15776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15778 /* We don't know whether we're looking at an
15779 operator-function-id or a conversion-function-id. */
15780 cp_parser_parse_tentatively (parser);
15781 /* Try an operator-function-id. */
15782 identifier = cp_parser_operator_function_id (parser);
15783 /* If that didn't work, try a conversion-function-id. */
15784 if (!cp_parser_parse_definitely (parser))
15786 cp_parser_error (parser, "expected template-name");
15787 return error_mark_node;
15790 /* Look for the identifier. */
15791 else
15792 identifier = cp_parser_identifier (parser);
15794 /* If we didn't find an identifier, we don't have a template-id. */
15795 if (identifier == error_mark_node)
15796 return error_mark_node;
15798 /* If the name immediately followed the `template' keyword, then it
15799 is a template-name. However, if the next token is not `<', then
15800 we do not treat it as a template-name, since it is not being used
15801 as part of a template-id. This enables us to handle constructs
15802 like:
15804 template <typename T> struct S { S(); };
15805 template <typename T> S<T>::S();
15807 correctly. We would treat `S' as a template -- if it were `S<T>'
15808 -- but we do not if there is no `<'. */
15810 if (processing_template_decl
15811 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15813 /* In a declaration, in a dependent context, we pretend that the
15814 "template" keyword was present in order to improve error
15815 recovery. For example, given:
15817 template <typename T> void f(T::X<int>);
15819 we want to treat "X<int>" as a template-id. */
15820 if (is_declaration
15821 && !template_keyword_p
15822 && parser->scope && TYPE_P (parser->scope)
15823 && check_dependency_p
15824 && dependent_scope_p (parser->scope)
15825 /* Do not do this for dtors (or ctors), since they never
15826 need the template keyword before their name. */
15827 && !constructor_name_p (identifier, parser->scope))
15829 cp_token_position start = 0;
15831 /* Explain what went wrong. */
15832 error_at (token->location, "non-template %qD used as template",
15833 identifier);
15834 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15835 parser->scope, identifier);
15836 /* If parsing tentatively, find the location of the "<" token. */
15837 if (cp_parser_simulate_error (parser))
15838 start = cp_lexer_token_position (parser->lexer, true);
15839 /* Parse the template arguments so that we can issue error
15840 messages about them. */
15841 cp_lexer_consume_token (parser->lexer);
15842 cp_parser_enclosed_template_argument_list (parser);
15843 /* Skip tokens until we find a good place from which to
15844 continue parsing. */
15845 cp_parser_skip_to_closing_parenthesis (parser,
15846 /*recovering=*/true,
15847 /*or_comma=*/true,
15848 /*consume_paren=*/false);
15849 /* If parsing tentatively, permanently remove the
15850 template argument list. That will prevent duplicate
15851 error messages from being issued about the missing
15852 "template" keyword. */
15853 if (start)
15854 cp_lexer_purge_tokens_after (parser->lexer, start);
15855 if (is_identifier)
15856 *is_identifier = true;
15857 parser->context->object_type = NULL_TREE;
15858 return identifier;
15861 /* If the "template" keyword is present, then there is generally
15862 no point in doing name-lookup, so we just return IDENTIFIER.
15863 But, if the qualifying scope is non-dependent then we can
15864 (and must) do name-lookup normally. */
15865 if (template_keyword_p
15866 && (!parser->scope
15867 || (TYPE_P (parser->scope)
15868 && dependent_type_p (parser->scope))))
15870 /* We're optimizing away the call to cp_parser_lookup_name, but we
15871 still need to do this. */
15872 parser->context->object_type = NULL_TREE;
15873 return identifier;
15877 /* Look up the name. */
15878 decl = cp_parser_lookup_name (parser, identifier,
15879 tag_type,
15880 /*is_template=*/true,
15881 /*is_namespace=*/false,
15882 check_dependency_p,
15883 /*ambiguous_decls=*/NULL,
15884 token->location);
15886 decl = strip_using_decl (decl);
15888 /* If DECL is a template, then the name was a template-name. */
15889 if (TREE_CODE (decl) == TEMPLATE_DECL)
15891 if (TREE_DEPRECATED (decl)
15892 && deprecated_state != DEPRECATED_SUPPRESS)
15893 warn_deprecated_use (decl, NULL_TREE);
15895 else
15897 /* The standard does not explicitly indicate whether a name that
15898 names a set of overloaded declarations, some of which are
15899 templates, is a template-name. However, such a name should
15900 be a template-name; otherwise, there is no way to form a
15901 template-id for the overloaded templates. */
15902 bool found = false;
15904 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15905 !found && iter; ++iter)
15906 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15907 found = true;
15909 if (!found)
15911 /* The name does not name a template. */
15912 cp_parser_error (parser, "expected template-name");
15913 return error_mark_node;
15917 /* If DECL is dependent, and refers to a function, then just return
15918 its name; we will look it up again during template instantiation. */
15919 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15921 tree scope = ovl_scope (decl);
15922 if (TYPE_P (scope) && dependent_type_p (scope))
15923 return identifier;
15926 return decl;
15929 /* Parse a template-argument-list.
15931 template-argument-list:
15932 template-argument ... [opt]
15933 template-argument-list , template-argument ... [opt]
15935 Returns a TREE_VEC containing the arguments. */
15937 static tree
15938 cp_parser_template_argument_list (cp_parser* parser)
15940 tree fixed_args[10];
15941 unsigned n_args = 0;
15942 unsigned alloced = 10;
15943 tree *arg_ary = fixed_args;
15944 tree vec;
15945 bool saved_in_template_argument_list_p;
15946 bool saved_ice_p;
15947 bool saved_non_ice_p;
15949 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15950 parser->in_template_argument_list_p = true;
15951 /* Even if the template-id appears in an integral
15952 constant-expression, the contents of the argument list do
15953 not. */
15954 saved_ice_p = parser->integral_constant_expression_p;
15955 parser->integral_constant_expression_p = false;
15956 saved_non_ice_p = parser->non_integral_constant_expression_p;
15957 parser->non_integral_constant_expression_p = false;
15959 /* Parse the arguments. */
15962 tree argument;
15964 if (n_args)
15965 /* Consume the comma. */
15966 cp_lexer_consume_token (parser->lexer);
15968 /* Parse the template-argument. */
15969 argument = cp_parser_template_argument (parser);
15971 /* If the next token is an ellipsis, we're expanding a template
15972 argument pack. */
15973 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15975 if (argument == error_mark_node)
15977 cp_token *token = cp_lexer_peek_token (parser->lexer);
15978 error_at (token->location,
15979 "expected parameter pack before %<...%>");
15981 /* Consume the `...' token. */
15982 cp_lexer_consume_token (parser->lexer);
15984 /* Make the argument into a TYPE_PACK_EXPANSION or
15985 EXPR_PACK_EXPANSION. */
15986 argument = make_pack_expansion (argument);
15989 if (n_args == alloced)
15991 alloced *= 2;
15993 if (arg_ary == fixed_args)
15995 arg_ary = XNEWVEC (tree, alloced);
15996 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
15998 else
15999 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16001 arg_ary[n_args++] = argument;
16003 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16005 vec = make_tree_vec (n_args);
16007 while (n_args--)
16008 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16010 if (arg_ary != fixed_args)
16011 free (arg_ary);
16012 parser->non_integral_constant_expression_p = saved_non_ice_p;
16013 parser->integral_constant_expression_p = saved_ice_p;
16014 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16015 if (CHECKING_P)
16016 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16017 return vec;
16020 /* Parse a template-argument.
16022 template-argument:
16023 assignment-expression
16024 type-id
16025 id-expression
16027 The representation is that of an assignment-expression, type-id, or
16028 id-expression -- except that the qualified id-expression is
16029 evaluated, so that the value returned is either a DECL or an
16030 OVERLOAD.
16032 Although the standard says "assignment-expression", it forbids
16033 throw-expressions or assignments in the template argument.
16034 Therefore, we use "conditional-expression" instead. */
16036 static tree
16037 cp_parser_template_argument (cp_parser* parser)
16039 tree argument;
16040 bool template_p;
16041 bool address_p;
16042 bool maybe_type_id = false;
16043 cp_token *token = NULL, *argument_start_token = NULL;
16044 location_t loc = 0;
16045 cp_id_kind idk;
16047 /* There's really no way to know what we're looking at, so we just
16048 try each alternative in order.
16050 [temp.arg]
16052 In a template-argument, an ambiguity between a type-id and an
16053 expression is resolved to a type-id, regardless of the form of
16054 the corresponding template-parameter.
16056 Therefore, we try a type-id first. */
16057 cp_parser_parse_tentatively (parser);
16058 argument = cp_parser_template_type_arg (parser);
16059 /* If there was no error parsing the type-id but the next token is a
16060 '>>', our behavior depends on which dialect of C++ we're
16061 parsing. In C++98, we probably found a typo for '> >'. But there
16062 are type-id which are also valid expressions. For instance:
16064 struct X { int operator >> (int); };
16065 template <int V> struct Foo {};
16066 Foo<X () >> 5> r;
16068 Here 'X()' is a valid type-id of a function type, but the user just
16069 wanted to write the expression "X() >> 5". Thus, we remember that we
16070 found a valid type-id, but we still try to parse the argument as an
16071 expression to see what happens.
16073 In C++0x, the '>>' will be considered two separate '>'
16074 tokens. */
16075 if (!cp_parser_error_occurred (parser)
16076 && cxx_dialect == cxx98
16077 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16079 maybe_type_id = true;
16080 cp_parser_abort_tentative_parse (parser);
16082 else
16084 /* If the next token isn't a `,' or a `>', then this argument wasn't
16085 really finished. This means that the argument is not a valid
16086 type-id. */
16087 if (!cp_parser_next_token_ends_template_argument_p (parser))
16088 cp_parser_error (parser, "expected template-argument");
16089 /* If that worked, we're done. */
16090 if (cp_parser_parse_definitely (parser))
16091 return argument;
16093 /* We're still not sure what the argument will be. */
16094 cp_parser_parse_tentatively (parser);
16095 /* Try a template. */
16096 argument_start_token = cp_lexer_peek_token (parser->lexer);
16097 argument = cp_parser_id_expression (parser,
16098 /*template_keyword_p=*/false,
16099 /*check_dependency_p=*/true,
16100 &template_p,
16101 /*declarator_p=*/false,
16102 /*optional_p=*/false);
16103 /* If the next token isn't a `,' or a `>', then this argument wasn't
16104 really finished. */
16105 if (!cp_parser_next_token_ends_template_argument_p (parser))
16106 cp_parser_error (parser, "expected template-argument");
16107 if (!cp_parser_error_occurred (parser))
16109 /* Figure out what is being referred to. If the id-expression
16110 was for a class template specialization, then we will have a
16111 TYPE_DECL at this point. There is no need to do name lookup
16112 at this point in that case. */
16113 if (TREE_CODE (argument) != TYPE_DECL)
16114 argument = cp_parser_lookup_name (parser, argument,
16115 none_type,
16116 /*is_template=*/template_p,
16117 /*is_namespace=*/false,
16118 /*check_dependency=*/true,
16119 /*ambiguous_decls=*/NULL,
16120 argument_start_token->location);
16121 /* Handle a constrained-type-specifier for a non-type template
16122 parameter. */
16123 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16124 argument = decl;
16125 else if (TREE_CODE (argument) != TEMPLATE_DECL
16126 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16127 cp_parser_error (parser, "expected template-name");
16129 if (cp_parser_parse_definitely (parser))
16131 if (TREE_DEPRECATED (argument))
16132 warn_deprecated_use (argument, NULL_TREE);
16133 return argument;
16135 /* It must be a non-type argument. In C++17 any constant-expression is
16136 allowed. */
16137 if (cxx_dialect > cxx14)
16138 goto general_expr;
16140 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16142 -- an integral constant-expression of integral or enumeration
16143 type; or
16145 -- the name of a non-type template-parameter; or
16147 -- the name of an object or function with external linkage...
16149 -- the address of an object or function with external linkage...
16151 -- a pointer to member... */
16152 /* Look for a non-type template parameter. */
16153 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16155 cp_parser_parse_tentatively (parser);
16156 argument = cp_parser_primary_expression (parser,
16157 /*address_p=*/false,
16158 /*cast_p=*/false,
16159 /*template_arg_p=*/true,
16160 &idk);
16161 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16162 || !cp_parser_next_token_ends_template_argument_p (parser))
16163 cp_parser_simulate_error (parser);
16164 if (cp_parser_parse_definitely (parser))
16165 return argument;
16168 /* If the next token is "&", the argument must be the address of an
16169 object or function with external linkage. */
16170 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16171 if (address_p)
16173 loc = cp_lexer_peek_token (parser->lexer)->location;
16174 cp_lexer_consume_token (parser->lexer);
16176 /* See if we might have an id-expression. */
16177 token = cp_lexer_peek_token (parser->lexer);
16178 if (token->type == CPP_NAME
16179 || token->keyword == RID_OPERATOR
16180 || token->type == CPP_SCOPE
16181 || token->type == CPP_TEMPLATE_ID
16182 || token->type == CPP_NESTED_NAME_SPECIFIER)
16184 cp_parser_parse_tentatively (parser);
16185 argument = cp_parser_primary_expression (parser,
16186 address_p,
16187 /*cast_p=*/false,
16188 /*template_arg_p=*/true,
16189 &idk);
16190 if (cp_parser_error_occurred (parser)
16191 || !cp_parser_next_token_ends_template_argument_p (parser))
16192 cp_parser_abort_tentative_parse (parser);
16193 else
16195 tree probe;
16197 if (INDIRECT_REF_P (argument))
16199 /* Strip the dereference temporarily. */
16200 gcc_assert (REFERENCE_REF_P (argument));
16201 argument = TREE_OPERAND (argument, 0);
16204 /* If we're in a template, we represent a qualified-id referring
16205 to a static data member as a SCOPE_REF even if the scope isn't
16206 dependent so that we can check access control later. */
16207 probe = argument;
16208 if (TREE_CODE (probe) == SCOPE_REF)
16209 probe = TREE_OPERAND (probe, 1);
16210 if (VAR_P (probe))
16212 /* A variable without external linkage might still be a
16213 valid constant-expression, so no error is issued here
16214 if the external-linkage check fails. */
16215 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16216 cp_parser_simulate_error (parser);
16218 else if (is_overloaded_fn (argument))
16219 /* All overloaded functions are allowed; if the external
16220 linkage test does not pass, an error will be issued
16221 later. */
16223 else if (address_p
16224 && (TREE_CODE (argument) == OFFSET_REF
16225 || TREE_CODE (argument) == SCOPE_REF))
16226 /* A pointer-to-member. */
16228 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16230 else
16231 cp_parser_simulate_error (parser);
16233 if (cp_parser_parse_definitely (parser))
16235 if (address_p)
16236 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16237 tf_warning_or_error);
16238 else
16239 argument = convert_from_reference (argument);
16240 return argument;
16244 /* If the argument started with "&", there are no other valid
16245 alternatives at this point. */
16246 if (address_p)
16248 cp_parser_error (parser, "invalid non-type template argument");
16249 return error_mark_node;
16252 general_expr:
16253 /* If the argument wasn't successfully parsed as a type-id followed
16254 by '>>', the argument can only be a constant expression now.
16255 Otherwise, we try parsing the constant-expression tentatively,
16256 because the argument could really be a type-id. */
16257 if (maybe_type_id)
16258 cp_parser_parse_tentatively (parser);
16260 if (cxx_dialect <= cxx14)
16261 argument = cp_parser_constant_expression (parser);
16262 else
16264 /* With C++17 generalized non-type template arguments we need to handle
16265 lvalue constant expressions, too. */
16266 argument = cp_parser_assignment_expression (parser);
16267 require_potential_constant_expression (argument);
16270 if (!maybe_type_id)
16271 return argument;
16272 if (!cp_parser_next_token_ends_template_argument_p (parser))
16273 cp_parser_error (parser, "expected template-argument");
16274 if (cp_parser_parse_definitely (parser))
16275 return argument;
16276 /* We did our best to parse the argument as a non type-id, but that
16277 was the only alternative that matched (albeit with a '>' after
16278 it). We can assume it's just a typo from the user, and a
16279 diagnostic will then be issued. */
16280 return cp_parser_template_type_arg (parser);
16283 /* Parse an explicit-instantiation.
16285 explicit-instantiation:
16286 template declaration
16288 Although the standard says `declaration', what it really means is:
16290 explicit-instantiation:
16291 template decl-specifier-seq [opt] declarator [opt] ;
16293 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16294 supposed to be allowed. A defect report has been filed about this
16295 issue.
16297 GNU Extension:
16299 explicit-instantiation:
16300 storage-class-specifier template
16301 decl-specifier-seq [opt] declarator [opt] ;
16302 function-specifier template
16303 decl-specifier-seq [opt] declarator [opt] ; */
16305 static void
16306 cp_parser_explicit_instantiation (cp_parser* parser)
16308 int declares_class_or_enum;
16309 cp_decl_specifier_seq decl_specifiers;
16310 tree extension_specifier = NULL_TREE;
16312 timevar_push (TV_TEMPLATE_INST);
16314 /* Look for an (optional) storage-class-specifier or
16315 function-specifier. */
16316 if (cp_parser_allow_gnu_extensions_p (parser))
16318 extension_specifier
16319 = cp_parser_storage_class_specifier_opt (parser);
16320 if (!extension_specifier)
16321 extension_specifier
16322 = cp_parser_function_specifier_opt (parser,
16323 /*decl_specs=*/NULL);
16326 /* Look for the `template' keyword. */
16327 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16328 /* Let the front end know that we are processing an explicit
16329 instantiation. */
16330 begin_explicit_instantiation ();
16331 /* [temp.explicit] says that we are supposed to ignore access
16332 control while processing explicit instantiation directives. */
16333 push_deferring_access_checks (dk_no_check);
16334 /* Parse a decl-specifier-seq. */
16335 cp_parser_decl_specifier_seq (parser,
16336 CP_PARSER_FLAGS_OPTIONAL,
16337 &decl_specifiers,
16338 &declares_class_or_enum);
16339 /* If there was exactly one decl-specifier, and it declared a class,
16340 and there's no declarator, then we have an explicit type
16341 instantiation. */
16342 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16344 tree type;
16346 type = check_tag_decl (&decl_specifiers,
16347 /*explicit_type_instantiation_p=*/true);
16348 /* Turn access control back on for names used during
16349 template instantiation. */
16350 pop_deferring_access_checks ();
16351 if (type)
16352 do_type_instantiation (type, extension_specifier,
16353 /*complain=*/tf_error);
16355 else
16357 cp_declarator *declarator;
16358 tree decl;
16360 /* Parse the declarator. */
16361 declarator
16362 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16363 /*ctor_dtor_or_conv_p=*/NULL,
16364 /*parenthesized_p=*/NULL,
16365 /*member_p=*/false,
16366 /*friend_p=*/false);
16367 if (declares_class_or_enum & 2)
16368 cp_parser_check_for_definition_in_return_type (declarator,
16369 decl_specifiers.type,
16370 decl_specifiers.locations[ds_type_spec]);
16371 if (declarator != cp_error_declarator)
16373 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16374 permerror (decl_specifiers.locations[ds_inline],
16375 "explicit instantiation shall not use"
16376 " %<inline%> specifier");
16377 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16378 permerror (decl_specifiers.locations[ds_constexpr],
16379 "explicit instantiation shall not use"
16380 " %<constexpr%> specifier");
16382 decl = grokdeclarator (declarator, &decl_specifiers,
16383 NORMAL, 0, &decl_specifiers.attributes);
16384 /* Turn access control back on for names used during
16385 template instantiation. */
16386 pop_deferring_access_checks ();
16387 /* Do the explicit instantiation. */
16388 do_decl_instantiation (decl, extension_specifier);
16390 else
16392 pop_deferring_access_checks ();
16393 /* Skip the body of the explicit instantiation. */
16394 cp_parser_skip_to_end_of_statement (parser);
16397 /* We're done with the instantiation. */
16398 end_explicit_instantiation ();
16400 cp_parser_consume_semicolon_at_end_of_statement (parser);
16402 timevar_pop (TV_TEMPLATE_INST);
16405 /* Parse an explicit-specialization.
16407 explicit-specialization:
16408 template < > declaration
16410 Although the standard says `declaration', what it really means is:
16412 explicit-specialization:
16413 template <> decl-specifier [opt] init-declarator [opt] ;
16414 template <> function-definition
16415 template <> explicit-specialization
16416 template <> template-declaration */
16418 static void
16419 cp_parser_explicit_specialization (cp_parser* parser)
16421 bool need_lang_pop;
16422 cp_token *token = cp_lexer_peek_token (parser->lexer);
16424 /* Look for the `template' keyword. */
16425 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16426 /* Look for the `<'. */
16427 cp_parser_require (parser, CPP_LESS, RT_LESS);
16428 /* Look for the `>'. */
16429 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16430 /* We have processed another parameter list. */
16431 ++parser->num_template_parameter_lists;
16432 /* [temp]
16434 A template ... explicit specialization ... shall not have C
16435 linkage. */
16436 if (current_lang_name == lang_name_c)
16438 error_at (token->location, "template specialization with C linkage");
16439 /* Give it C++ linkage to avoid confusing other parts of the
16440 front end. */
16441 push_lang_context (lang_name_cplusplus);
16442 need_lang_pop = true;
16444 else
16445 need_lang_pop = false;
16446 /* Let the front end know that we are beginning a specialization. */
16447 if (!begin_specialization ())
16449 end_specialization ();
16450 return;
16453 /* If the next keyword is `template', we need to figure out whether
16454 or not we're looking a template-declaration. */
16455 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16457 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16458 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16459 cp_parser_template_declaration_after_export (parser,
16460 /*member_p=*/false);
16461 else
16462 cp_parser_explicit_specialization (parser);
16464 else
16465 /* Parse the dependent declaration. */
16466 cp_parser_single_declaration (parser,
16467 /*checks=*/NULL,
16468 /*member_p=*/false,
16469 /*explicit_specialization_p=*/true,
16470 /*friend_p=*/NULL);
16471 /* We're done with the specialization. */
16472 end_specialization ();
16473 /* For the erroneous case of a template with C linkage, we pushed an
16474 implicit C++ linkage scope; exit that scope now. */
16475 if (need_lang_pop)
16476 pop_lang_context ();
16477 /* We're done with this parameter list. */
16478 --parser->num_template_parameter_lists;
16481 /* Parse a type-specifier.
16483 type-specifier:
16484 simple-type-specifier
16485 class-specifier
16486 enum-specifier
16487 elaborated-type-specifier
16488 cv-qualifier
16490 GNU Extension:
16492 type-specifier:
16493 __complex__
16495 Returns a representation of the type-specifier. For a
16496 class-specifier, enum-specifier, or elaborated-type-specifier, a
16497 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16499 The parser flags FLAGS is used to control type-specifier parsing.
16501 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16502 in a decl-specifier-seq.
16504 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16505 class-specifier, enum-specifier, or elaborated-type-specifier, then
16506 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16507 if a type is declared; 2 if it is defined. Otherwise, it is set to
16508 zero.
16510 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16511 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16512 is set to FALSE. */
16514 static tree
16515 cp_parser_type_specifier (cp_parser* parser,
16516 cp_parser_flags flags,
16517 cp_decl_specifier_seq *decl_specs,
16518 bool is_declaration,
16519 int* declares_class_or_enum,
16520 bool* is_cv_qualifier)
16522 tree type_spec = NULL_TREE;
16523 cp_token *token;
16524 enum rid keyword;
16525 cp_decl_spec ds = ds_last;
16527 /* Assume this type-specifier does not declare a new type. */
16528 if (declares_class_or_enum)
16529 *declares_class_or_enum = 0;
16530 /* And that it does not specify a cv-qualifier. */
16531 if (is_cv_qualifier)
16532 *is_cv_qualifier = false;
16533 /* Peek at the next token. */
16534 token = cp_lexer_peek_token (parser->lexer);
16536 /* If we're looking at a keyword, we can use that to guide the
16537 production we choose. */
16538 keyword = token->keyword;
16539 switch (keyword)
16541 case RID_ENUM:
16542 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16543 goto elaborated_type_specifier;
16545 /* Look for the enum-specifier. */
16546 type_spec = cp_parser_enum_specifier (parser);
16547 /* If that worked, we're done. */
16548 if (type_spec)
16550 if (declares_class_or_enum)
16551 *declares_class_or_enum = 2;
16552 if (decl_specs)
16553 cp_parser_set_decl_spec_type (decl_specs,
16554 type_spec,
16555 token,
16556 /*type_definition_p=*/true);
16557 return type_spec;
16559 else
16560 goto elaborated_type_specifier;
16562 /* Any of these indicate either a class-specifier, or an
16563 elaborated-type-specifier. */
16564 case RID_CLASS:
16565 case RID_STRUCT:
16566 case RID_UNION:
16567 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16568 goto elaborated_type_specifier;
16570 /* Parse tentatively so that we can back up if we don't find a
16571 class-specifier. */
16572 cp_parser_parse_tentatively (parser);
16573 /* Look for the class-specifier. */
16574 type_spec = cp_parser_class_specifier (parser);
16575 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16576 /* If that worked, we're done. */
16577 if (cp_parser_parse_definitely (parser))
16579 if (declares_class_or_enum)
16580 *declares_class_or_enum = 2;
16581 if (decl_specs)
16582 cp_parser_set_decl_spec_type (decl_specs,
16583 type_spec,
16584 token,
16585 /*type_definition_p=*/true);
16586 return type_spec;
16589 /* Fall through. */
16590 elaborated_type_specifier:
16591 /* We're declaring (not defining) a class or enum. */
16592 if (declares_class_or_enum)
16593 *declares_class_or_enum = 1;
16595 /* Fall through. */
16596 case RID_TYPENAME:
16597 /* Look for an elaborated-type-specifier. */
16598 type_spec
16599 = (cp_parser_elaborated_type_specifier
16600 (parser,
16601 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16602 is_declaration));
16603 if (decl_specs)
16604 cp_parser_set_decl_spec_type (decl_specs,
16605 type_spec,
16606 token,
16607 /*type_definition_p=*/false);
16608 return type_spec;
16610 case RID_CONST:
16611 ds = ds_const;
16612 if (is_cv_qualifier)
16613 *is_cv_qualifier = true;
16614 break;
16616 case RID_VOLATILE:
16617 ds = ds_volatile;
16618 if (is_cv_qualifier)
16619 *is_cv_qualifier = true;
16620 break;
16622 case RID_RESTRICT:
16623 ds = ds_restrict;
16624 if (is_cv_qualifier)
16625 *is_cv_qualifier = true;
16626 break;
16628 case RID_COMPLEX:
16629 /* The `__complex__' keyword is a GNU extension. */
16630 ds = ds_complex;
16631 break;
16633 default:
16634 break;
16637 /* Handle simple keywords. */
16638 if (ds != ds_last)
16640 if (decl_specs)
16642 set_and_check_decl_spec_loc (decl_specs, ds, token);
16643 decl_specs->any_specifiers_p = true;
16645 return cp_lexer_consume_token (parser->lexer)->u.value;
16648 /* If we do not already have a type-specifier, assume we are looking
16649 at a simple-type-specifier. */
16650 type_spec = cp_parser_simple_type_specifier (parser,
16651 decl_specs,
16652 flags);
16654 /* If we didn't find a type-specifier, and a type-specifier was not
16655 optional in this context, issue an error message. */
16656 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16658 cp_parser_error (parser, "expected type specifier");
16659 return error_mark_node;
16662 return type_spec;
16665 /* Parse a simple-type-specifier.
16667 simple-type-specifier:
16668 :: [opt] nested-name-specifier [opt] type-name
16669 :: [opt] nested-name-specifier template template-id
16670 char
16671 wchar_t
16672 bool
16673 short
16675 long
16676 signed
16677 unsigned
16678 float
16679 double
16680 void
16682 C++11 Extension:
16684 simple-type-specifier:
16685 auto
16686 decltype ( expression )
16687 char16_t
16688 char32_t
16689 __underlying_type ( type-id )
16691 C++17 extension:
16693 nested-name-specifier(opt) template-name
16695 GNU Extension:
16697 simple-type-specifier:
16698 __int128
16699 __typeof__ unary-expression
16700 __typeof__ ( type-id )
16701 __typeof__ ( type-id ) { initializer-list , [opt] }
16703 Concepts Extension:
16705 simple-type-specifier:
16706 constrained-type-specifier
16708 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16709 appropriately updated. */
16711 static tree
16712 cp_parser_simple_type_specifier (cp_parser* parser,
16713 cp_decl_specifier_seq *decl_specs,
16714 cp_parser_flags flags)
16716 tree type = NULL_TREE;
16717 cp_token *token;
16718 int idx;
16720 /* Peek at the next token. */
16721 token = cp_lexer_peek_token (parser->lexer);
16723 /* If we're looking at a keyword, things are easy. */
16724 switch (token->keyword)
16726 case RID_CHAR:
16727 if (decl_specs)
16728 decl_specs->explicit_char_p = true;
16729 type = char_type_node;
16730 break;
16731 case RID_CHAR16:
16732 type = char16_type_node;
16733 break;
16734 case RID_CHAR32:
16735 type = char32_type_node;
16736 break;
16737 case RID_WCHAR:
16738 type = wchar_type_node;
16739 break;
16740 case RID_BOOL:
16741 type = boolean_type_node;
16742 break;
16743 case RID_SHORT:
16744 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16745 type = short_integer_type_node;
16746 break;
16747 case RID_INT:
16748 if (decl_specs)
16749 decl_specs->explicit_int_p = true;
16750 type = integer_type_node;
16751 break;
16752 case RID_INT_N_0:
16753 case RID_INT_N_1:
16754 case RID_INT_N_2:
16755 case RID_INT_N_3:
16756 idx = token->keyword - RID_INT_N_0;
16757 if (! int_n_enabled_p [idx])
16758 break;
16759 if (decl_specs)
16761 decl_specs->explicit_intN_p = true;
16762 decl_specs->int_n_idx = idx;
16764 type = int_n_trees [idx].signed_type;
16765 break;
16766 case RID_LONG:
16767 if (decl_specs)
16768 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16769 type = long_integer_type_node;
16770 break;
16771 case RID_SIGNED:
16772 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16773 type = integer_type_node;
16774 break;
16775 case RID_UNSIGNED:
16776 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16777 type = unsigned_type_node;
16778 break;
16779 case RID_FLOAT:
16780 type = float_type_node;
16781 break;
16782 case RID_DOUBLE:
16783 type = double_type_node;
16784 break;
16785 case RID_VOID:
16786 type = void_type_node;
16787 break;
16789 case RID_AUTO:
16790 maybe_warn_cpp0x (CPP0X_AUTO);
16791 if (parser->auto_is_implicit_function_template_parm_p)
16793 /* The 'auto' might be the placeholder return type for a function decl
16794 with trailing return type. */
16795 bool have_trailing_return_fn_decl = false;
16797 cp_parser_parse_tentatively (parser);
16798 cp_lexer_consume_token (parser->lexer);
16799 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16800 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16801 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16802 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16804 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16806 cp_lexer_consume_token (parser->lexer);
16807 cp_parser_skip_to_closing_parenthesis (parser,
16808 /*recovering*/false,
16809 /*or_comma*/false,
16810 /*consume_paren*/true);
16811 continue;
16814 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16816 have_trailing_return_fn_decl = true;
16817 break;
16820 cp_lexer_consume_token (parser->lexer);
16822 cp_parser_abort_tentative_parse (parser);
16824 if (have_trailing_return_fn_decl)
16826 type = make_auto ();
16827 break;
16830 if (cxx_dialect >= cxx14)
16832 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16833 type = TREE_TYPE (type);
16835 else
16836 type = error_mark_node;
16838 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16840 if (cxx_dialect < cxx14)
16841 error_at (token->location,
16842 "use of %<auto%> in lambda parameter declaration "
16843 "only available with "
16844 "-std=c++14 or -std=gnu++14");
16846 else if (cxx_dialect < cxx14)
16847 error_at (token->location,
16848 "use of %<auto%> in parameter declaration "
16849 "only available with "
16850 "-std=c++14 or -std=gnu++14");
16851 else if (!flag_concepts)
16852 pedwarn (token->location, OPT_Wpedantic,
16853 "ISO C++ forbids use of %<auto%> in parameter "
16854 "declaration");
16856 else
16857 type = make_auto ();
16858 break;
16860 case RID_DECLTYPE:
16861 /* Since DR 743, decltype can either be a simple-type-specifier by
16862 itself or begin a nested-name-specifier. Parsing it will replace
16863 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16864 handling below decide what to do. */
16865 cp_parser_decltype (parser);
16866 cp_lexer_set_token_position (parser->lexer, token);
16867 break;
16869 case RID_TYPEOF:
16870 /* Consume the `typeof' token. */
16871 cp_lexer_consume_token (parser->lexer);
16872 /* Parse the operand to `typeof'. */
16873 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16874 /* If it is not already a TYPE, take its type. */
16875 if (!TYPE_P (type))
16876 type = finish_typeof (type);
16878 if (decl_specs)
16879 cp_parser_set_decl_spec_type (decl_specs, type,
16880 token,
16881 /*type_definition_p=*/false);
16883 return type;
16885 case RID_UNDERLYING_TYPE:
16886 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16887 if (decl_specs)
16888 cp_parser_set_decl_spec_type (decl_specs, type,
16889 token,
16890 /*type_definition_p=*/false);
16892 return type;
16894 case RID_BASES:
16895 case RID_DIRECT_BASES:
16896 type = cp_parser_trait_expr (parser, token->keyword);
16897 if (decl_specs)
16898 cp_parser_set_decl_spec_type (decl_specs, type,
16899 token,
16900 /*type_definition_p=*/false);
16901 return type;
16902 default:
16903 break;
16906 /* If token is an already-parsed decltype not followed by ::,
16907 it's a simple-type-specifier. */
16908 if (token->type == CPP_DECLTYPE
16909 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16911 type = saved_checks_value (token->u.tree_check_value);
16912 if (decl_specs)
16914 cp_parser_set_decl_spec_type (decl_specs, type,
16915 token,
16916 /*type_definition_p=*/false);
16917 /* Remember that we are handling a decltype in order to
16918 implement the resolution of DR 1510 when the argument
16919 isn't instantiation dependent. */
16920 decl_specs->decltype_p = true;
16922 cp_lexer_consume_token (parser->lexer);
16923 return type;
16926 /* If the type-specifier was for a built-in type, we're done. */
16927 if (type)
16929 /* Record the type. */
16930 if (decl_specs
16931 && (token->keyword != RID_SIGNED
16932 && token->keyword != RID_UNSIGNED
16933 && token->keyword != RID_SHORT
16934 && token->keyword != RID_LONG))
16935 cp_parser_set_decl_spec_type (decl_specs,
16936 type,
16937 token,
16938 /*type_definition_p=*/false);
16939 if (decl_specs)
16940 decl_specs->any_specifiers_p = true;
16942 /* Consume the token. */
16943 cp_lexer_consume_token (parser->lexer);
16945 if (type == error_mark_node)
16946 return error_mark_node;
16948 /* There is no valid C++ program where a non-template type is
16949 followed by a "<". That usually indicates that the user thought
16950 that the type was a template. */
16951 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16952 token->location);
16954 return TYPE_NAME (type);
16957 /* The type-specifier must be a user-defined type. */
16958 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
16960 bool qualified_p;
16961 bool global_p;
16963 /* Don't gobble tokens or issue error messages if this is an
16964 optional type-specifier. */
16965 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
16966 cp_parser_parse_tentatively (parser);
16968 token = cp_lexer_peek_token (parser->lexer);
16970 /* Look for the optional `::' operator. */
16971 global_p
16972 = (cp_parser_global_scope_opt (parser,
16973 /*current_scope_valid_p=*/false)
16974 != NULL_TREE);
16975 /* Look for the nested-name specifier. */
16976 qualified_p
16977 = (cp_parser_nested_name_specifier_opt (parser,
16978 /*typename_keyword_p=*/false,
16979 /*check_dependency_p=*/true,
16980 /*type_p=*/false,
16981 /*is_declaration=*/false)
16982 != NULL_TREE);
16983 /* If we have seen a nested-name-specifier, and the next token
16984 is `template', then we are using the template-id production. */
16985 if (parser->scope
16986 && cp_parser_optional_template_keyword (parser))
16988 /* Look for the template-id. */
16989 type = cp_parser_template_id (parser,
16990 /*template_keyword_p=*/true,
16991 /*check_dependency_p=*/true,
16992 none_type,
16993 /*is_declaration=*/false);
16994 /* If the template-id did not name a type, we are out of
16995 luck. */
16996 if (TREE_CODE (type) != TYPE_DECL)
16998 cp_parser_error (parser, "expected template-id for type");
16999 type = NULL_TREE;
17002 /* Otherwise, look for a type-name. */
17003 else
17004 type = cp_parser_type_name (parser);
17005 /* Keep track of all name-lookups performed in class scopes. */
17006 if (type
17007 && !global_p
17008 && !qualified_p
17009 && TREE_CODE (type) == TYPE_DECL
17010 && identifier_p (DECL_NAME (type)))
17011 maybe_note_name_used_in_class (DECL_NAME (type), type);
17012 /* If it didn't work out, we don't have a TYPE. */
17013 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17014 && !cp_parser_parse_definitely (parser))
17015 type = NULL_TREE;
17016 if (!type && cxx_dialect >= cxx1z)
17018 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17019 cp_parser_parse_tentatively (parser);
17021 cp_parser_global_scope_opt (parser,
17022 /*current_scope_valid_p=*/false);
17023 cp_parser_nested_name_specifier_opt (parser,
17024 /*typename_keyword_p=*/false,
17025 /*check_dependency_p=*/true,
17026 /*type_p=*/false,
17027 /*is_declaration=*/false);
17028 tree name = cp_parser_identifier (parser);
17029 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17030 && parser->scope != error_mark_node)
17032 tree tmpl = cp_parser_lookup_name (parser, name,
17033 none_type,
17034 /*is_template=*/false,
17035 /*is_namespace=*/false,
17036 /*check_dependency=*/true,
17037 /*ambiguous_decls=*/NULL,
17038 token->location);
17039 if (tmpl && tmpl != error_mark_node
17040 && (DECL_CLASS_TEMPLATE_P (tmpl)
17041 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17042 type = make_template_placeholder (tmpl);
17043 else
17045 type = error_mark_node;
17046 if (!cp_parser_simulate_error (parser))
17047 cp_parser_name_lookup_error (parser, name, tmpl,
17048 NLE_TYPE, token->location);
17051 else
17052 type = error_mark_node;
17054 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17055 && !cp_parser_parse_definitely (parser))
17056 type = NULL_TREE;
17058 if (type && decl_specs)
17059 cp_parser_set_decl_spec_type (decl_specs, type,
17060 token,
17061 /*type_definition_p=*/false);
17064 /* If we didn't get a type-name, issue an error message. */
17065 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17067 cp_parser_error (parser, "expected type-name");
17068 return error_mark_node;
17071 if (type && type != error_mark_node)
17073 /* See if TYPE is an Objective-C type, and if so, parse and
17074 accept any protocol references following it. Do this before
17075 the cp_parser_check_for_invalid_template_id() call, because
17076 Objective-C types can be followed by '<...>' which would
17077 enclose protocol names rather than template arguments, and so
17078 everything is fine. */
17079 if (c_dialect_objc () && !parser->scope
17080 && (objc_is_id (type) || objc_is_class_name (type)))
17082 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17083 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17085 /* Clobber the "unqualified" type previously entered into
17086 DECL_SPECS with the new, improved protocol-qualified version. */
17087 if (decl_specs)
17088 decl_specs->type = qual_type;
17090 return qual_type;
17093 /* There is no valid C++ program where a non-template type is
17094 followed by a "<". That usually indicates that the user
17095 thought that the type was a template. */
17096 cp_parser_check_for_invalid_template_id (parser, type,
17097 none_type,
17098 token->location);
17101 return type;
17104 /* Parse a type-name.
17106 type-name:
17107 class-name
17108 enum-name
17109 typedef-name
17110 simple-template-id [in c++0x]
17112 enum-name:
17113 identifier
17115 typedef-name:
17116 identifier
17118 Concepts:
17120 type-name:
17121 concept-name
17122 partial-concept-id
17124 concept-name:
17125 identifier
17127 Returns a TYPE_DECL for the type. */
17129 static tree
17130 cp_parser_type_name (cp_parser* parser)
17132 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17135 /* See above. */
17136 static tree
17137 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17139 tree type_decl;
17141 /* We can't know yet whether it is a class-name or not. */
17142 cp_parser_parse_tentatively (parser);
17143 /* Try a class-name. */
17144 type_decl = cp_parser_class_name (parser,
17145 typename_keyword_p,
17146 /*template_keyword_p=*/false,
17147 none_type,
17148 /*check_dependency_p=*/true,
17149 /*class_head_p=*/false,
17150 /*is_declaration=*/false);
17151 /* If it's not a class-name, keep looking. */
17152 if (!cp_parser_parse_definitely (parser))
17154 if (cxx_dialect < cxx11)
17155 /* It must be a typedef-name or an enum-name. */
17156 return cp_parser_nonclass_name (parser);
17158 cp_parser_parse_tentatively (parser);
17159 /* It is either a simple-template-id representing an
17160 instantiation of an alias template... */
17161 type_decl = cp_parser_template_id (parser,
17162 /*template_keyword_p=*/false,
17163 /*check_dependency_p=*/true,
17164 none_type,
17165 /*is_declaration=*/false);
17166 /* Note that this must be an instantiation of an alias template
17167 because [temp.names]/6 says:
17169 A template-id that names an alias template specialization
17170 is a type-name.
17172 Whereas [temp.names]/7 says:
17174 A simple-template-id that names a class template
17175 specialization is a class-name.
17177 With concepts, this could also be a partial-concept-id that
17178 declares a non-type template parameter. */
17179 if (type_decl != NULL_TREE
17180 && TREE_CODE (type_decl) == TYPE_DECL
17181 && TYPE_DECL_ALIAS_P (type_decl))
17182 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17183 else if (is_constrained_parameter (type_decl))
17184 /* Don't do anything. */ ;
17185 else
17186 cp_parser_simulate_error (parser);
17188 if (!cp_parser_parse_definitely (parser))
17189 /* ... Or a typedef-name or an enum-name. */
17190 return cp_parser_nonclass_name (parser);
17193 return type_decl;
17196 /* Check if DECL and ARGS can form a constrained-type-specifier.
17197 If ARGS is non-null, we try to form a concept check of the
17198 form DECL<?, ARGS> where ? is a wildcard that matches any
17199 kind of template argument. If ARGS is NULL, then we try to
17200 form a concept check of the form DECL<?>. */
17202 static tree
17203 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17204 tree decl, tree args)
17206 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17208 /* If we a constrained-type-specifier cannot be deduced. */
17209 if (parser->prevent_constrained_type_specifiers)
17210 return NULL_TREE;
17212 /* A constrained type specifier can only be found in an
17213 overload set or as a reference to a template declaration.
17215 FIXME: This might be masking a bug. It's possible that
17216 that the deduction below is causing template specializations
17217 to be formed with the wildcard as an argument. */
17218 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17219 return NULL_TREE;
17221 /* Try to build a call expression that evaluates the
17222 concept. This can fail if the overload set refers
17223 only to non-templates. */
17224 tree placeholder = build_nt (WILDCARD_DECL);
17225 tree check = build_concept_check (decl, placeholder, args);
17226 if (check == error_mark_node)
17227 return NULL_TREE;
17229 /* Deduce the checked constraint and the prototype parameter.
17231 FIXME: In certain cases, failure to deduce should be a
17232 diagnosable error. */
17233 tree conc;
17234 tree proto;
17235 if (!deduce_constrained_parameter (check, conc, proto))
17236 return NULL_TREE;
17238 /* In template parameter scope, this results in a constrained
17239 parameter. Return a descriptor of that parm. */
17240 if (processing_template_parmlist)
17241 return build_constrained_parameter (conc, proto, args);
17243 /* In a parameter-declaration-clause, constrained-type
17244 specifiers result in invented template parameters. */
17245 if (parser->auto_is_implicit_function_template_parm_p)
17247 tree x = build_constrained_parameter (conc, proto, args);
17248 return synthesize_implicit_template_parm (parser, x);
17250 else
17252 /* Otherwise, we're in a context where the constrained
17253 type name is deduced and the constraint applies
17254 after deduction. */
17255 return make_constrained_auto (conc, args);
17258 return NULL_TREE;
17261 /* If DECL refers to a concept, return a TYPE_DECL representing
17262 the result of using the constrained type specifier in the
17263 current context. DECL refers to a concept if
17265 - it is an overload set containing a function concept taking a single
17266 type argument, or
17268 - it is a variable concept taking a single type argument. */
17270 static tree
17271 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17273 if (flag_concepts
17274 && (TREE_CODE (decl) == OVERLOAD
17275 || BASELINK_P (decl)
17276 || variable_concept_p (decl)))
17277 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17278 else
17279 return NULL_TREE;
17282 /* Check if DECL and ARGS form a partial-concept-id. If so,
17283 assign ID to the resulting constrained placeholder.
17285 Returns true if the partial-concept-id designates a placeholder
17286 and false otherwise. Note that *id is set to NULL_TREE in
17287 this case. */
17289 static tree
17290 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17292 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17295 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17296 or a concept-name.
17298 enum-name:
17299 identifier
17301 typedef-name:
17302 identifier
17304 concept-name:
17305 identifier
17307 Returns a TYPE_DECL for the type. */
17309 static tree
17310 cp_parser_nonclass_name (cp_parser* parser)
17312 tree type_decl;
17313 tree identifier;
17315 cp_token *token = cp_lexer_peek_token (parser->lexer);
17316 identifier = cp_parser_identifier (parser);
17317 if (identifier == error_mark_node)
17318 return error_mark_node;
17320 /* Look up the type-name. */
17321 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17323 type_decl = strip_using_decl (type_decl);
17325 /* If we found an overload set, then it may refer to a concept-name. */
17326 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17327 type_decl = decl;
17329 if (TREE_CODE (type_decl) != TYPE_DECL
17330 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17332 /* See if this is an Objective-C type. */
17333 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17334 tree type = objc_get_protocol_qualified_type (identifier, protos);
17335 if (type)
17336 type_decl = TYPE_NAME (type);
17339 /* Issue an error if we did not find a type-name. */
17340 if (TREE_CODE (type_decl) != TYPE_DECL
17341 /* In Objective-C, we have the complication that class names are
17342 normally type names and start declarations (eg, the
17343 "NSObject" in "NSObject *object;"), but can be used in an
17344 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17345 is an expression. So, a classname followed by a dot is not a
17346 valid type-name. */
17347 || (objc_is_class_name (TREE_TYPE (type_decl))
17348 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17350 if (!cp_parser_simulate_error (parser))
17351 cp_parser_name_lookup_error (parser, identifier, type_decl,
17352 NLE_TYPE, token->location);
17353 return error_mark_node;
17355 /* Remember that the name was used in the definition of the
17356 current class so that we can check later to see if the
17357 meaning would have been different after the class was
17358 entirely defined. */
17359 else if (type_decl != error_mark_node
17360 && !parser->scope)
17361 maybe_note_name_used_in_class (identifier, type_decl);
17363 return type_decl;
17366 /* Parse an elaborated-type-specifier. Note that the grammar given
17367 here incorporates the resolution to DR68.
17369 elaborated-type-specifier:
17370 class-key :: [opt] nested-name-specifier [opt] identifier
17371 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17372 enum-key :: [opt] nested-name-specifier [opt] identifier
17373 typename :: [opt] nested-name-specifier identifier
17374 typename :: [opt] nested-name-specifier template [opt]
17375 template-id
17377 GNU extension:
17379 elaborated-type-specifier:
17380 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17381 class-key attributes :: [opt] nested-name-specifier [opt]
17382 template [opt] template-id
17383 enum attributes :: [opt] nested-name-specifier [opt] identifier
17385 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17386 declared `friend'. If IS_DECLARATION is TRUE, then this
17387 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17388 something is being declared.
17390 Returns the TYPE specified. */
17392 static tree
17393 cp_parser_elaborated_type_specifier (cp_parser* parser,
17394 bool is_friend,
17395 bool is_declaration)
17397 enum tag_types tag_type;
17398 tree identifier;
17399 tree type = NULL_TREE;
17400 tree attributes = NULL_TREE;
17401 tree globalscope;
17402 cp_token *token = NULL;
17404 /* See if we're looking at the `enum' keyword. */
17405 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17407 /* Consume the `enum' token. */
17408 cp_lexer_consume_token (parser->lexer);
17409 /* Remember that it's an enumeration type. */
17410 tag_type = enum_type;
17411 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17412 enums) is used here. */
17413 cp_token *token = cp_lexer_peek_token (parser->lexer);
17414 if (cp_parser_is_keyword (token, RID_CLASS)
17415 || cp_parser_is_keyword (token, RID_STRUCT))
17417 gcc_rich_location richloc (token->location);
17418 richloc.add_range (input_location, false);
17419 richloc.add_fixit_remove ();
17420 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17421 "a scoped enum must not use the %qD keyword",
17422 token->u.value);
17423 /* Consume the `struct' or `class' and parse it anyway. */
17424 cp_lexer_consume_token (parser->lexer);
17426 /* Parse the attributes. */
17427 attributes = cp_parser_attributes_opt (parser);
17429 /* Or, it might be `typename'. */
17430 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17431 RID_TYPENAME))
17433 /* Consume the `typename' token. */
17434 cp_lexer_consume_token (parser->lexer);
17435 /* Remember that it's a `typename' type. */
17436 tag_type = typename_type;
17438 /* Otherwise it must be a class-key. */
17439 else
17441 tag_type = cp_parser_class_key (parser);
17442 if (tag_type == none_type)
17443 return error_mark_node;
17444 /* Parse the attributes. */
17445 attributes = cp_parser_attributes_opt (parser);
17448 /* Look for the `::' operator. */
17449 globalscope = cp_parser_global_scope_opt (parser,
17450 /*current_scope_valid_p=*/false);
17451 /* Look for the nested-name-specifier. */
17452 tree nested_name_specifier;
17453 if (tag_type == typename_type && !globalscope)
17455 nested_name_specifier
17456 = cp_parser_nested_name_specifier (parser,
17457 /*typename_keyword_p=*/true,
17458 /*check_dependency_p=*/true,
17459 /*type_p=*/true,
17460 is_declaration);
17461 if (!nested_name_specifier)
17462 return error_mark_node;
17464 else
17465 /* Even though `typename' is not present, the proposed resolution
17466 to Core Issue 180 says that in `class A<T>::B', `B' should be
17467 considered a type-name, even if `A<T>' is dependent. */
17468 nested_name_specifier
17469 = cp_parser_nested_name_specifier_opt (parser,
17470 /*typename_keyword_p=*/true,
17471 /*check_dependency_p=*/true,
17472 /*type_p=*/true,
17473 is_declaration);
17474 /* For everything but enumeration types, consider a template-id.
17475 For an enumeration type, consider only a plain identifier. */
17476 if (tag_type != enum_type)
17478 bool template_p = false;
17479 tree decl;
17481 /* Allow the `template' keyword. */
17482 template_p = cp_parser_optional_template_keyword (parser);
17483 /* If we didn't see `template', we don't know if there's a
17484 template-id or not. */
17485 if (!template_p)
17486 cp_parser_parse_tentatively (parser);
17487 /* Parse the template-id. */
17488 token = cp_lexer_peek_token (parser->lexer);
17489 decl = cp_parser_template_id (parser, template_p,
17490 /*check_dependency_p=*/true,
17491 tag_type,
17492 is_declaration);
17493 /* If we didn't find a template-id, look for an ordinary
17494 identifier. */
17495 if (!template_p && !cp_parser_parse_definitely (parser))
17497 /* We can get here when cp_parser_template_id, called by
17498 cp_parser_class_name with tag_type == none_type, succeeds
17499 and caches a BASELINK. Then, when called again here,
17500 instead of failing and returning an error_mark_node
17501 returns it (see template/typename17.C in C++11).
17502 ??? Could we diagnose this earlier? */
17503 else if (tag_type == typename_type && BASELINK_P (decl))
17505 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17506 type = error_mark_node;
17508 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17509 in effect, then we must assume that, upon instantiation, the
17510 template will correspond to a class. */
17511 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17512 && tag_type == typename_type)
17513 type = make_typename_type (parser->scope, decl,
17514 typename_type,
17515 /*complain=*/tf_error);
17516 /* If the `typename' keyword is in effect and DECL is not a type
17517 decl, then type is non existent. */
17518 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17520 else if (TREE_CODE (decl) == TYPE_DECL)
17522 type = check_elaborated_type_specifier (tag_type, decl,
17523 /*allow_template_p=*/true);
17525 /* If the next token is a semicolon, this must be a specialization,
17526 instantiation, or friend declaration. Check the scope while we
17527 still know whether or not we had a nested-name-specifier. */
17528 if (type != error_mark_node
17529 && !nested_name_specifier && !is_friend
17530 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17531 check_unqualified_spec_or_inst (type, token->location);
17533 else if (decl == error_mark_node)
17534 type = error_mark_node;
17537 if (!type)
17539 token = cp_lexer_peek_token (parser->lexer);
17540 identifier = cp_parser_identifier (parser);
17542 if (identifier == error_mark_node)
17544 parser->scope = NULL_TREE;
17545 return error_mark_node;
17548 /* For a `typename', we needn't call xref_tag. */
17549 if (tag_type == typename_type
17550 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17551 return cp_parser_make_typename_type (parser, identifier,
17552 token->location);
17554 /* Template parameter lists apply only if we are not within a
17555 function parameter list. */
17556 bool template_parm_lists_apply
17557 = parser->num_template_parameter_lists;
17558 if (template_parm_lists_apply)
17559 for (cp_binding_level *s = current_binding_level;
17560 s && s->kind != sk_template_parms;
17561 s = s->level_chain)
17562 if (s->kind == sk_function_parms)
17563 template_parm_lists_apply = false;
17565 /* Look up a qualified name in the usual way. */
17566 if (parser->scope)
17568 tree decl;
17569 tree ambiguous_decls;
17571 decl = cp_parser_lookup_name (parser, identifier,
17572 tag_type,
17573 /*is_template=*/false,
17574 /*is_namespace=*/false,
17575 /*check_dependency=*/true,
17576 &ambiguous_decls,
17577 token->location);
17579 /* If the lookup was ambiguous, an error will already have been
17580 issued. */
17581 if (ambiguous_decls)
17582 return error_mark_node;
17584 /* If we are parsing friend declaration, DECL may be a
17585 TEMPLATE_DECL tree node here. However, we need to check
17586 whether this TEMPLATE_DECL results in valid code. Consider
17587 the following example:
17589 namespace N {
17590 template <class T> class C {};
17592 class X {
17593 template <class T> friend class N::C; // #1, valid code
17595 template <class T> class Y {
17596 friend class N::C; // #2, invalid code
17599 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17600 name lookup of `N::C'. We see that friend declaration must
17601 be template for the code to be valid. Note that
17602 processing_template_decl does not work here since it is
17603 always 1 for the above two cases. */
17605 decl = (cp_parser_maybe_treat_template_as_class
17606 (decl, /*tag_name_p=*/is_friend
17607 && template_parm_lists_apply));
17609 if (TREE_CODE (decl) != TYPE_DECL)
17611 cp_parser_diagnose_invalid_type_name (parser,
17612 identifier,
17613 token->location);
17614 return error_mark_node;
17617 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17619 bool allow_template = (template_parm_lists_apply
17620 || DECL_SELF_REFERENCE_P (decl));
17621 type = check_elaborated_type_specifier (tag_type, decl,
17622 allow_template);
17624 if (type == error_mark_node)
17625 return error_mark_node;
17628 /* Forward declarations of nested types, such as
17630 class C1::C2;
17631 class C1::C2::C3;
17633 are invalid unless all components preceding the final '::'
17634 are complete. If all enclosing types are complete, these
17635 declarations become merely pointless.
17637 Invalid forward declarations of nested types are errors
17638 caught elsewhere in parsing. Those that are pointless arrive
17639 here. */
17641 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17642 && !is_friend && !processing_explicit_instantiation)
17643 warning (0, "declaration %qD does not declare anything", decl);
17645 type = TREE_TYPE (decl);
17647 else
17649 /* An elaborated-type-specifier sometimes introduces a new type and
17650 sometimes names an existing type. Normally, the rule is that it
17651 introduces a new type only if there is not an existing type of
17652 the same name already in scope. For example, given:
17654 struct S {};
17655 void f() { struct S s; }
17657 the `struct S' in the body of `f' is the same `struct S' as in
17658 the global scope; the existing definition is used. However, if
17659 there were no global declaration, this would introduce a new
17660 local class named `S'.
17662 An exception to this rule applies to the following code:
17664 namespace N { struct S; }
17666 Here, the elaborated-type-specifier names a new type
17667 unconditionally; even if there is already an `S' in the
17668 containing scope this declaration names a new type.
17669 This exception only applies if the elaborated-type-specifier
17670 forms the complete declaration:
17672 [class.name]
17674 A declaration consisting solely of `class-key identifier ;' is
17675 either a redeclaration of the name in the current scope or a
17676 forward declaration of the identifier as a class name. It
17677 introduces the name into the current scope.
17679 We are in this situation precisely when the next token is a `;'.
17681 An exception to the exception is that a `friend' declaration does
17682 *not* name a new type; i.e., given:
17684 struct S { friend struct T; };
17686 `T' is not a new type in the scope of `S'.
17688 Also, `new struct S' or `sizeof (struct S)' never results in the
17689 definition of a new type; a new type can only be declared in a
17690 declaration context. */
17692 tag_scope ts;
17693 bool template_p;
17695 if (is_friend)
17696 /* Friends have special name lookup rules. */
17697 ts = ts_within_enclosing_non_class;
17698 else if (is_declaration
17699 && cp_lexer_next_token_is (parser->lexer,
17700 CPP_SEMICOLON))
17701 /* This is a `class-key identifier ;' */
17702 ts = ts_current;
17703 else
17704 ts = ts_global;
17706 template_p =
17707 (template_parm_lists_apply
17708 && (cp_parser_next_token_starts_class_definition_p (parser)
17709 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17710 /* An unqualified name was used to reference this type, so
17711 there were no qualifying templates. */
17712 if (template_parm_lists_apply
17713 && !cp_parser_check_template_parameters (parser,
17714 /*num_templates=*/0,
17715 token->location,
17716 /*declarator=*/NULL))
17717 return error_mark_node;
17718 type = xref_tag (tag_type, identifier, ts, template_p);
17722 if (type == error_mark_node)
17723 return error_mark_node;
17725 /* Allow attributes on forward declarations of classes. */
17726 if (attributes)
17728 if (TREE_CODE (type) == TYPENAME_TYPE)
17729 warning (OPT_Wattributes,
17730 "attributes ignored on uninstantiated type");
17731 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17732 && ! processing_explicit_instantiation)
17733 warning (OPT_Wattributes,
17734 "attributes ignored on template instantiation");
17735 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17736 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17737 else
17738 warning (OPT_Wattributes,
17739 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17742 if (tag_type != enum_type)
17744 /* Indicate whether this class was declared as a `class' or as a
17745 `struct'. */
17746 if (CLASS_TYPE_P (type))
17747 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17748 cp_parser_check_class_key (tag_type, type);
17751 /* A "<" cannot follow an elaborated type specifier. If that
17752 happens, the user was probably trying to form a template-id. */
17753 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17754 token->location);
17756 return type;
17759 /* Parse an enum-specifier.
17761 enum-specifier:
17762 enum-head { enumerator-list [opt] }
17763 enum-head { enumerator-list , } [C++0x]
17765 enum-head:
17766 enum-key identifier [opt] enum-base [opt]
17767 enum-key nested-name-specifier identifier enum-base [opt]
17769 enum-key:
17770 enum
17771 enum class [C++0x]
17772 enum struct [C++0x]
17774 enum-base: [C++0x]
17775 : type-specifier-seq
17777 opaque-enum-specifier:
17778 enum-key identifier enum-base [opt] ;
17780 GNU Extensions:
17781 enum-key attributes[opt] identifier [opt] enum-base [opt]
17782 { enumerator-list [opt] }attributes[opt]
17783 enum-key attributes[opt] identifier [opt] enum-base [opt]
17784 { enumerator-list, }attributes[opt] [C++0x]
17786 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17787 if the token stream isn't an enum-specifier after all. */
17789 static tree
17790 cp_parser_enum_specifier (cp_parser* parser)
17792 tree identifier;
17793 tree type = NULL_TREE;
17794 tree prev_scope;
17795 tree nested_name_specifier = NULL_TREE;
17796 tree attributes;
17797 bool scoped_enum_p = false;
17798 bool has_underlying_type = false;
17799 bool nested_being_defined = false;
17800 bool new_value_list = false;
17801 bool is_new_type = false;
17802 bool is_unnamed = false;
17803 tree underlying_type = NULL_TREE;
17804 cp_token *type_start_token = NULL;
17805 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17807 parser->colon_corrects_to_scope_p = false;
17809 /* Parse tentatively so that we can back up if we don't find a
17810 enum-specifier. */
17811 cp_parser_parse_tentatively (parser);
17813 /* Caller guarantees that the current token is 'enum', an identifier
17814 possibly follows, and the token after that is an opening brace.
17815 If we don't have an identifier, fabricate an anonymous name for
17816 the enumeration being defined. */
17817 cp_lexer_consume_token (parser->lexer);
17819 /* Parse the "class" or "struct", which indicates a scoped
17820 enumeration type in C++0x. */
17821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17822 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17824 if (cxx_dialect < cxx11)
17825 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17827 /* Consume the `struct' or `class' token. */
17828 cp_lexer_consume_token (parser->lexer);
17830 scoped_enum_p = true;
17833 attributes = cp_parser_attributes_opt (parser);
17835 /* Clear the qualification. */
17836 parser->scope = NULL_TREE;
17837 parser->qualifying_scope = NULL_TREE;
17838 parser->object_scope = NULL_TREE;
17840 /* Figure out in what scope the declaration is being placed. */
17841 prev_scope = current_scope ();
17843 type_start_token = cp_lexer_peek_token (parser->lexer);
17845 push_deferring_access_checks (dk_no_check);
17846 nested_name_specifier
17847 = cp_parser_nested_name_specifier_opt (parser,
17848 /*typename_keyword_p=*/true,
17849 /*check_dependency_p=*/false,
17850 /*type_p=*/false,
17851 /*is_declaration=*/false);
17853 if (nested_name_specifier)
17855 tree name;
17857 identifier = cp_parser_identifier (parser);
17858 name = cp_parser_lookup_name (parser, identifier,
17859 enum_type,
17860 /*is_template=*/false,
17861 /*is_namespace=*/false,
17862 /*check_dependency=*/true,
17863 /*ambiguous_decls=*/NULL,
17864 input_location);
17865 if (name && name != error_mark_node)
17867 type = TREE_TYPE (name);
17868 if (TREE_CODE (type) == TYPENAME_TYPE)
17870 /* Are template enums allowed in ISO? */
17871 if (template_parm_scope_p ())
17872 pedwarn (type_start_token->location, OPT_Wpedantic,
17873 "%qD is an enumeration template", name);
17874 /* ignore a typename reference, for it will be solved by name
17875 in start_enum. */
17876 type = NULL_TREE;
17879 else if (nested_name_specifier == error_mark_node)
17880 /* We already issued an error. */;
17881 else
17883 error_at (type_start_token->location,
17884 "%qD does not name an enumeration in %qT",
17885 identifier, nested_name_specifier);
17886 nested_name_specifier = error_mark_node;
17889 else
17891 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17892 identifier = cp_parser_identifier (parser);
17893 else
17895 identifier = make_anon_name ();
17896 is_unnamed = true;
17897 if (scoped_enum_p)
17898 error_at (type_start_token->location,
17899 "unnamed scoped enum is not allowed");
17902 pop_deferring_access_checks ();
17904 /* Check for the `:' that denotes a specified underlying type in C++0x.
17905 Note that a ':' could also indicate a bitfield width, however. */
17906 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17908 cp_decl_specifier_seq type_specifiers;
17910 /* Consume the `:'. */
17911 cp_lexer_consume_token (parser->lexer);
17913 /* Parse the type-specifier-seq. */
17914 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17915 /*is_trailing_return=*/false,
17916 &type_specifiers);
17918 /* At this point this is surely not elaborated type specifier. */
17919 if (!cp_parser_parse_definitely (parser))
17920 return NULL_TREE;
17922 if (cxx_dialect < cxx11)
17923 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17925 has_underlying_type = true;
17927 /* If that didn't work, stop. */
17928 if (type_specifiers.type != error_mark_node)
17930 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17931 /*initialized=*/0, NULL);
17932 if (underlying_type == error_mark_node
17933 || check_for_bare_parameter_packs (underlying_type))
17934 underlying_type = NULL_TREE;
17938 /* Look for the `{' but don't consume it yet. */
17939 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17941 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17943 cp_parser_error (parser, "expected %<{%>");
17944 if (has_underlying_type)
17946 type = NULL_TREE;
17947 goto out;
17950 /* An opaque-enum-specifier must have a ';' here. */
17951 if ((scoped_enum_p || underlying_type)
17952 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17954 cp_parser_error (parser, "expected %<;%> or %<{%>");
17955 if (has_underlying_type)
17957 type = NULL_TREE;
17958 goto out;
17963 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
17964 return NULL_TREE;
17966 if (nested_name_specifier)
17968 if (CLASS_TYPE_P (nested_name_specifier))
17970 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
17971 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
17972 push_scope (nested_name_specifier);
17974 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
17976 push_nested_namespace (nested_name_specifier);
17980 /* Issue an error message if type-definitions are forbidden here. */
17981 if (!cp_parser_check_type_definition (parser))
17982 type = error_mark_node;
17983 else
17984 /* Create the new type. We do this before consuming the opening
17985 brace so the enum will be recorded as being on the line of its
17986 tag (or the 'enum' keyword, if there is no tag). */
17987 type = start_enum (identifier, type, underlying_type,
17988 attributes, scoped_enum_p, &is_new_type);
17990 /* If the next token is not '{' it is an opaque-enum-specifier or an
17991 elaborated-type-specifier. */
17992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17994 timevar_push (TV_PARSE_ENUM);
17995 if (nested_name_specifier
17996 && nested_name_specifier != error_mark_node)
17998 /* The following catches invalid code such as:
17999 enum class S<int>::E { A, B, C }; */
18000 if (!processing_specialization
18001 && CLASS_TYPE_P (nested_name_specifier)
18002 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18003 error_at (type_start_token->location, "cannot add an enumerator "
18004 "list to a template instantiation");
18006 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18008 error_at (type_start_token->location,
18009 "%<%T::%E%> has not been declared",
18010 TYPE_CONTEXT (nested_name_specifier),
18011 nested_name_specifier);
18012 type = error_mark_node;
18014 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18015 && !CLASS_TYPE_P (nested_name_specifier))
18017 error_at (type_start_token->location, "nested name specifier "
18018 "%qT for enum declaration does not name a class "
18019 "or namespace", nested_name_specifier);
18020 type = error_mark_node;
18022 /* If that scope does not contain the scope in which the
18023 class was originally declared, the program is invalid. */
18024 else if (prev_scope && !is_ancestor (prev_scope,
18025 nested_name_specifier))
18027 if (at_namespace_scope_p ())
18028 error_at (type_start_token->location,
18029 "declaration of %qD in namespace %qD which does not "
18030 "enclose %qD",
18031 type, prev_scope, nested_name_specifier);
18032 else
18033 error_at (type_start_token->location,
18034 "declaration of %qD in %qD which does not "
18035 "enclose %qD",
18036 type, prev_scope, nested_name_specifier);
18037 type = error_mark_node;
18039 /* If that scope is the scope where the declaration is being placed
18040 the program is invalid. */
18041 else if (CLASS_TYPE_P (nested_name_specifier)
18042 && CLASS_TYPE_P (prev_scope)
18043 && same_type_p (nested_name_specifier, prev_scope))
18045 permerror (type_start_token->location,
18046 "extra qualification not allowed");
18047 nested_name_specifier = NULL_TREE;
18051 if (scoped_enum_p)
18052 begin_scope (sk_scoped_enum, type);
18054 /* Consume the opening brace. */
18055 cp_lexer_consume_token (parser->lexer);
18057 if (type == error_mark_node)
18058 ; /* Nothing to add */
18059 else if (OPAQUE_ENUM_P (type)
18060 || (cxx_dialect > cxx98 && processing_specialization))
18062 new_value_list = true;
18063 SET_OPAQUE_ENUM_P (type, false);
18064 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18066 else
18068 error_at (type_start_token->location,
18069 "multiple definition of %q#T", type);
18070 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18071 "previous definition here");
18072 type = error_mark_node;
18075 if (type == error_mark_node)
18076 cp_parser_skip_to_end_of_block_or_statement (parser);
18077 /* If the next token is not '}', then there are some enumerators. */
18078 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18080 if (is_unnamed && !scoped_enum_p)
18081 pedwarn (type_start_token->location, OPT_Wpedantic,
18082 "ISO C++ forbids empty unnamed enum");
18084 else
18085 cp_parser_enumerator_list (parser, type);
18087 /* Consume the final '}'. */
18088 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18090 if (scoped_enum_p)
18091 finish_scope ();
18092 timevar_pop (TV_PARSE_ENUM);
18094 else
18096 /* If a ';' follows, then it is an opaque-enum-specifier
18097 and additional restrictions apply. */
18098 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18100 if (is_unnamed)
18101 error_at (type_start_token->location,
18102 "opaque-enum-specifier without name");
18103 else if (nested_name_specifier)
18104 error_at (type_start_token->location,
18105 "opaque-enum-specifier must use a simple identifier");
18109 /* Look for trailing attributes to apply to this enumeration, and
18110 apply them if appropriate. */
18111 if (cp_parser_allow_gnu_extensions_p (parser))
18113 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18114 cplus_decl_attributes (&type,
18115 trailing_attr,
18116 (int) ATTR_FLAG_TYPE_IN_PLACE);
18119 /* Finish up the enumeration. */
18120 if (type != error_mark_node)
18122 if (new_value_list)
18123 finish_enum_value_list (type);
18124 if (is_new_type)
18125 finish_enum (type);
18128 if (nested_name_specifier)
18130 if (CLASS_TYPE_P (nested_name_specifier))
18132 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18133 pop_scope (nested_name_specifier);
18135 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18137 pop_nested_namespace (nested_name_specifier);
18140 out:
18141 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18142 return type;
18145 /* Parse an enumerator-list. The enumerators all have the indicated
18146 TYPE.
18148 enumerator-list:
18149 enumerator-definition
18150 enumerator-list , enumerator-definition */
18152 static void
18153 cp_parser_enumerator_list (cp_parser* parser, tree type)
18155 while (true)
18157 /* Parse an enumerator-definition. */
18158 cp_parser_enumerator_definition (parser, type);
18160 /* If the next token is not a ',', we've reached the end of
18161 the list. */
18162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18163 break;
18164 /* Otherwise, consume the `,' and keep going. */
18165 cp_lexer_consume_token (parser->lexer);
18166 /* If the next token is a `}', there is a trailing comma. */
18167 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18169 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18170 pedwarn (input_location, OPT_Wpedantic,
18171 "comma at end of enumerator list");
18172 break;
18177 /* Parse an enumerator-definition. The enumerator has the indicated
18178 TYPE.
18180 enumerator-definition:
18181 enumerator
18182 enumerator = constant-expression
18184 enumerator:
18185 identifier
18187 GNU Extensions:
18189 enumerator-definition:
18190 enumerator attributes [opt]
18191 enumerator attributes [opt] = constant-expression */
18193 static void
18194 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18196 tree identifier;
18197 tree value;
18198 location_t loc;
18200 /* Save the input location because we are interested in the location
18201 of the identifier and not the location of the explicit value. */
18202 loc = cp_lexer_peek_token (parser->lexer)->location;
18204 /* Look for the identifier. */
18205 identifier = cp_parser_identifier (parser);
18206 if (identifier == error_mark_node)
18207 return;
18209 /* Parse any specified attributes. */
18210 tree attrs = cp_parser_attributes_opt (parser);
18212 /* If the next token is an '=', then there is an explicit value. */
18213 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18215 /* Consume the `=' token. */
18216 cp_lexer_consume_token (parser->lexer);
18217 /* Parse the value. */
18218 value = cp_parser_constant_expression (parser);
18220 else
18221 value = NULL_TREE;
18223 /* If we are processing a template, make sure the initializer of the
18224 enumerator doesn't contain any bare template parameter pack. */
18225 if (check_for_bare_parameter_packs (value))
18226 value = error_mark_node;
18228 /* Create the enumerator. */
18229 build_enumerator (identifier, value, type, attrs, loc);
18232 /* Parse a namespace-name.
18234 namespace-name:
18235 original-namespace-name
18236 namespace-alias
18238 Returns the NAMESPACE_DECL for the namespace. */
18240 static tree
18241 cp_parser_namespace_name (cp_parser* parser)
18243 tree identifier;
18244 tree namespace_decl;
18246 cp_token *token = cp_lexer_peek_token (parser->lexer);
18248 /* Get the name of the namespace. */
18249 identifier = cp_parser_identifier (parser);
18250 if (identifier == error_mark_node)
18251 return error_mark_node;
18253 /* Look up the identifier in the currently active scope. Look only
18254 for namespaces, due to:
18256 [basic.lookup.udir]
18258 When looking up a namespace-name in a using-directive or alias
18259 definition, only namespace names are considered.
18261 And:
18263 [basic.lookup.qual]
18265 During the lookup of a name preceding the :: scope resolution
18266 operator, object, function, and enumerator names are ignored.
18268 (Note that cp_parser_qualifying_entity only calls this
18269 function if the token after the name is the scope resolution
18270 operator.) */
18271 namespace_decl = cp_parser_lookup_name (parser, identifier,
18272 none_type,
18273 /*is_template=*/false,
18274 /*is_namespace=*/true,
18275 /*check_dependency=*/true,
18276 /*ambiguous_decls=*/NULL,
18277 token->location);
18278 /* If it's not a namespace, issue an error. */
18279 if (namespace_decl == error_mark_node
18280 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18282 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18283 error_at (token->location, "%qD is not a namespace-name", identifier);
18284 cp_parser_error (parser, "expected namespace-name");
18285 namespace_decl = error_mark_node;
18288 return namespace_decl;
18291 /* Parse a namespace-definition.
18293 namespace-definition:
18294 named-namespace-definition
18295 unnamed-namespace-definition
18297 named-namespace-definition:
18298 original-namespace-definition
18299 extension-namespace-definition
18301 original-namespace-definition:
18302 namespace identifier { namespace-body }
18304 extension-namespace-definition:
18305 namespace original-namespace-name { namespace-body }
18307 unnamed-namespace-definition:
18308 namespace { namespace-body } */
18310 static void
18311 cp_parser_namespace_definition (cp_parser* parser)
18313 tree identifier;
18314 int nested_definition_count = 0;
18316 cp_ensure_no_omp_declare_simd (parser);
18317 cp_ensure_no_oacc_routine (parser);
18319 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18321 if (is_inline)
18323 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18324 cp_lexer_consume_token (parser->lexer);
18327 /* Look for the `namespace' keyword. */
18328 cp_token* token
18329 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18331 /* Parse any specified attributes before the identifier. */
18332 tree attribs = cp_parser_attributes_opt (parser);
18334 for (;;)
18336 identifier = NULL_TREE;
18338 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18340 identifier = cp_parser_identifier (parser);
18342 /* Parse any attributes specified after the identifier. */
18343 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18346 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18347 break;
18349 if (!nested_definition_count && cxx_dialect < cxx1z)
18350 pedwarn (input_location, OPT_Wpedantic,
18351 "nested namespace definitions only available with "
18352 "-std=c++1z or -std=gnu++1z");
18354 /* Nested namespace names can create new namespaces (unlike
18355 other qualified-ids). */
18356 if (int count = identifier ? push_namespace (identifier) : 0)
18357 nested_definition_count += count;
18358 else
18359 cp_parser_error (parser, "nested namespace name required");
18360 cp_lexer_consume_token (parser->lexer);
18363 if (nested_definition_count && !identifier)
18364 cp_parser_error (parser, "namespace name required");
18366 if (nested_definition_count && attribs)
18367 error_at (token->location,
18368 "a nested namespace definition cannot have attributes");
18369 if (nested_definition_count && is_inline)
18370 error_at (token->location,
18371 "a nested namespace definition cannot be inline");
18373 /* Start the namespace. */
18374 nested_definition_count += push_namespace (identifier, is_inline);
18376 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18378 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18380 /* Look for the `{' to validate starting the namespace. */
18381 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
18383 /* Parse the body of the namespace. */
18384 cp_parser_namespace_body (parser);
18386 /* Look for the final `}'. */
18387 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18389 if (has_visibility)
18390 pop_visibility (1);
18392 /* Pop the nested namespace definitions. */
18393 while (nested_definition_count--)
18394 pop_namespace ();
18397 /* Parse a namespace-body.
18399 namespace-body:
18400 declaration-seq [opt] */
18402 static void
18403 cp_parser_namespace_body (cp_parser* parser)
18405 cp_parser_declaration_seq_opt (parser);
18408 /* Parse a namespace-alias-definition.
18410 namespace-alias-definition:
18411 namespace identifier = qualified-namespace-specifier ; */
18413 static void
18414 cp_parser_namespace_alias_definition (cp_parser* parser)
18416 tree identifier;
18417 tree namespace_specifier;
18419 cp_token *token = cp_lexer_peek_token (parser->lexer);
18421 /* Look for the `namespace' keyword. */
18422 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18423 /* Look for the identifier. */
18424 identifier = cp_parser_identifier (parser);
18425 if (identifier == error_mark_node)
18426 return;
18427 /* Look for the `=' token. */
18428 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18429 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18431 error_at (token->location, "%<namespace%> definition is not allowed here");
18432 /* Skip the definition. */
18433 cp_lexer_consume_token (parser->lexer);
18434 if (cp_parser_skip_to_closing_brace (parser))
18435 cp_lexer_consume_token (parser->lexer);
18436 return;
18438 cp_parser_require (parser, CPP_EQ, RT_EQ);
18439 /* Look for the qualified-namespace-specifier. */
18440 namespace_specifier
18441 = cp_parser_qualified_namespace_specifier (parser);
18442 /* Look for the `;' token. */
18443 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18445 /* Register the alias in the symbol table. */
18446 do_namespace_alias (identifier, namespace_specifier);
18449 /* Parse a qualified-namespace-specifier.
18451 qualified-namespace-specifier:
18452 :: [opt] nested-name-specifier [opt] namespace-name
18454 Returns a NAMESPACE_DECL corresponding to the specified
18455 namespace. */
18457 static tree
18458 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18460 /* Look for the optional `::'. */
18461 cp_parser_global_scope_opt (parser,
18462 /*current_scope_valid_p=*/false);
18464 /* Look for the optional nested-name-specifier. */
18465 cp_parser_nested_name_specifier_opt (parser,
18466 /*typename_keyword_p=*/false,
18467 /*check_dependency_p=*/true,
18468 /*type_p=*/false,
18469 /*is_declaration=*/true);
18471 return cp_parser_namespace_name (parser);
18474 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18475 access declaration.
18477 using-declaration:
18478 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18479 using :: unqualified-id ;
18481 access-declaration:
18482 qualified-id ;
18486 static bool
18487 cp_parser_using_declaration (cp_parser* parser,
18488 bool access_declaration_p)
18490 cp_token *token;
18491 bool typename_p = false;
18492 bool global_scope_p;
18493 tree decl;
18494 tree identifier;
18495 tree qscope;
18496 int oldcount = errorcount;
18497 cp_token *diag_token = NULL;
18499 if (access_declaration_p)
18501 diag_token = cp_lexer_peek_token (parser->lexer);
18502 cp_parser_parse_tentatively (parser);
18504 else
18506 /* Look for the `using' keyword. */
18507 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18509 again:
18510 /* Peek at the next token. */
18511 token = cp_lexer_peek_token (parser->lexer);
18512 /* See if it's `typename'. */
18513 if (token->keyword == RID_TYPENAME)
18515 /* Remember that we've seen it. */
18516 typename_p = true;
18517 /* Consume the `typename' token. */
18518 cp_lexer_consume_token (parser->lexer);
18522 /* Look for the optional global scope qualification. */
18523 global_scope_p
18524 = (cp_parser_global_scope_opt (parser,
18525 /*current_scope_valid_p=*/false)
18526 != NULL_TREE);
18528 /* If we saw `typename', or didn't see `::', then there must be a
18529 nested-name-specifier present. */
18530 if (typename_p || !global_scope_p)
18532 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18533 /*check_dependency_p=*/true,
18534 /*type_p=*/false,
18535 /*is_declaration=*/true);
18536 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18538 cp_parser_skip_to_end_of_block_or_statement (parser);
18539 return false;
18542 /* Otherwise, we could be in either of the two productions. In that
18543 case, treat the nested-name-specifier as optional. */
18544 else
18545 qscope = cp_parser_nested_name_specifier_opt (parser,
18546 /*typename_keyword_p=*/false,
18547 /*check_dependency_p=*/true,
18548 /*type_p=*/false,
18549 /*is_declaration=*/true);
18550 if (!qscope)
18551 qscope = global_namespace;
18552 else if (UNSCOPED_ENUM_P (qscope))
18553 qscope = CP_TYPE_CONTEXT (qscope);
18555 if (access_declaration_p && cp_parser_error_occurred (parser))
18556 /* Something has already gone wrong; there's no need to parse
18557 further. Since an error has occurred, the return value of
18558 cp_parser_parse_definitely will be false, as required. */
18559 return cp_parser_parse_definitely (parser);
18561 token = cp_lexer_peek_token (parser->lexer);
18562 /* Parse the unqualified-id. */
18563 identifier = cp_parser_unqualified_id (parser,
18564 /*template_keyword_p=*/false,
18565 /*check_dependency_p=*/true,
18566 /*declarator_p=*/true,
18567 /*optional_p=*/false);
18569 if (access_declaration_p)
18571 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18572 cp_parser_simulate_error (parser);
18573 if (!cp_parser_parse_definitely (parser))
18574 return false;
18576 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18578 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18579 if (cxx_dialect < cxx1z
18580 && !in_system_header_at (ell->location))
18581 pedwarn (ell->location, 0,
18582 "pack expansion in using-declaration only available "
18583 "with -std=c++1z or -std=gnu++1z");
18584 qscope = make_pack_expansion (qscope);
18587 /* The function we call to handle a using-declaration is different
18588 depending on what scope we are in. */
18589 if (qscope == error_mark_node || identifier == error_mark_node)
18591 else if (!identifier_p (identifier)
18592 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18593 /* [namespace.udecl]
18595 A using declaration shall not name a template-id. */
18596 error_at (token->location,
18597 "a template-id may not appear in a using-declaration");
18598 else
18600 if (at_class_scope_p ())
18602 /* Create the USING_DECL. */
18603 decl = do_class_using_decl (qscope, identifier);
18605 if (decl && typename_p)
18606 USING_DECL_TYPENAME_P (decl) = 1;
18608 if (check_for_bare_parameter_packs (decl))
18610 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18611 return false;
18613 else
18614 /* Add it to the list of members in this class. */
18615 finish_member_declaration (decl);
18617 else
18619 decl = cp_parser_lookup_name_simple (parser,
18620 identifier,
18621 token->location);
18622 if (decl == error_mark_node)
18623 cp_parser_name_lookup_error (parser, identifier,
18624 decl, NLE_NULL,
18625 token->location);
18626 else if (check_for_bare_parameter_packs (decl))
18628 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18629 return false;
18631 else if (!at_namespace_scope_p ())
18632 finish_local_using_decl (decl, qscope, identifier);
18633 else
18634 finish_namespace_using_decl (decl, qscope, identifier);
18638 if (!access_declaration_p
18639 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18641 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18642 if (cxx_dialect < cxx1z)
18643 pedwarn (comma->location, 0,
18644 "comma-separated list in using-declaration only available "
18645 "with -std=c++1z or -std=gnu++1z");
18646 goto again;
18649 /* Look for the final `;'. */
18650 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18652 if (access_declaration_p && errorcount == oldcount)
18653 warning_at (diag_token->location, OPT_Wdeprecated,
18654 "access declarations are deprecated "
18655 "in favour of using-declarations; "
18656 "suggestion: add the %<using%> keyword");
18658 return true;
18661 /* Parse an alias-declaration.
18663 alias-declaration:
18664 using identifier attribute-specifier-seq [opt] = type-id */
18666 static tree
18667 cp_parser_alias_declaration (cp_parser* parser)
18669 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18670 location_t id_location;
18671 cp_declarator *declarator;
18672 cp_decl_specifier_seq decl_specs;
18673 bool member_p;
18674 const char *saved_message = NULL;
18676 /* Look for the `using' keyword. */
18677 cp_token *using_token
18678 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18679 if (using_token == NULL)
18680 return error_mark_node;
18682 id_location = cp_lexer_peek_token (parser->lexer)->location;
18683 id = cp_parser_identifier (parser);
18684 if (id == error_mark_node)
18685 return error_mark_node;
18687 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18688 attributes = cp_parser_attributes_opt (parser);
18689 if (attributes == error_mark_node)
18690 return error_mark_node;
18692 cp_parser_require (parser, CPP_EQ, RT_EQ);
18694 if (cp_parser_error_occurred (parser))
18695 return error_mark_node;
18697 cp_parser_commit_to_tentative_parse (parser);
18699 /* Now we are going to parse the type-id of the declaration. */
18702 [dcl.type]/3 says:
18704 "A type-specifier-seq shall not define a class or enumeration
18705 unless it appears in the type-id of an alias-declaration (7.1.3) that
18706 is not the declaration of a template-declaration."
18708 In other words, if we currently are in an alias template, the
18709 type-id should not define a type.
18711 So let's set parser->type_definition_forbidden_message in that
18712 case; cp_parser_check_type_definition (called by
18713 cp_parser_class_specifier) will then emit an error if a type is
18714 defined in the type-id. */
18715 if (parser->num_template_parameter_lists)
18717 saved_message = parser->type_definition_forbidden_message;
18718 parser->type_definition_forbidden_message =
18719 G_("types may not be defined in alias template declarations");
18722 type = cp_parser_type_id (parser);
18724 /* Restore the error message if need be. */
18725 if (parser->num_template_parameter_lists)
18726 parser->type_definition_forbidden_message = saved_message;
18728 if (type == error_mark_node
18729 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18731 cp_parser_skip_to_end_of_block_or_statement (parser);
18732 return error_mark_node;
18735 /* A typedef-name can also be introduced by an alias-declaration. The
18736 identifier following the using keyword becomes a typedef-name. It has
18737 the same semantics as if it were introduced by the typedef
18738 specifier. In particular, it does not define a new type and it shall
18739 not appear in the type-id. */
18741 clear_decl_specs (&decl_specs);
18742 decl_specs.type = type;
18743 if (attributes != NULL_TREE)
18745 decl_specs.attributes = attributes;
18746 set_and_check_decl_spec_loc (&decl_specs,
18747 ds_attribute,
18748 attrs_token);
18750 set_and_check_decl_spec_loc (&decl_specs,
18751 ds_typedef,
18752 using_token);
18753 set_and_check_decl_spec_loc (&decl_specs,
18754 ds_alias,
18755 using_token);
18757 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18758 declarator->id_loc = id_location;
18760 member_p = at_class_scope_p ();
18761 if (member_p)
18762 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18763 NULL_TREE, attributes);
18764 else
18765 decl = start_decl (declarator, &decl_specs, 0,
18766 attributes, NULL_TREE, &pushed_scope);
18767 if (decl == error_mark_node)
18768 return decl;
18770 // Attach constraints to the alias declaration.
18771 if (flag_concepts && current_template_parms)
18773 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18774 tree constr = build_constraints (reqs, NULL_TREE);
18775 set_constraints (decl, constr);
18778 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18780 if (pushed_scope)
18781 pop_scope (pushed_scope);
18783 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18784 added into the symbol table; otherwise, return the TYPE_DECL. */
18785 if (DECL_LANG_SPECIFIC (decl)
18786 && DECL_TEMPLATE_INFO (decl)
18787 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18789 decl = DECL_TI_TEMPLATE (decl);
18790 if (member_p)
18791 check_member_template (decl);
18794 return decl;
18797 /* Parse a using-directive.
18799 using-directive:
18800 using namespace :: [opt] nested-name-specifier [opt]
18801 namespace-name ; */
18803 static void
18804 cp_parser_using_directive (cp_parser* parser)
18806 tree namespace_decl;
18807 tree attribs;
18809 /* Look for the `using' keyword. */
18810 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18811 /* And the `namespace' keyword. */
18812 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18813 /* Look for the optional `::' operator. */
18814 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18815 /* And the optional nested-name-specifier. */
18816 cp_parser_nested_name_specifier_opt (parser,
18817 /*typename_keyword_p=*/false,
18818 /*check_dependency_p=*/true,
18819 /*type_p=*/false,
18820 /*is_declaration=*/true);
18821 /* Get the namespace being used. */
18822 namespace_decl = cp_parser_namespace_name (parser);
18823 /* And any specified attributes. */
18824 attribs = cp_parser_attributes_opt (parser);
18826 /* Update the symbol table. */
18827 if (namespace_bindings_p ())
18828 finish_namespace_using_directive (namespace_decl, attribs);
18829 else
18830 finish_local_using_directive (namespace_decl, attribs);
18832 /* Look for the final `;'. */
18833 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18836 /* Parse an asm-definition.
18838 asm-definition:
18839 asm ( string-literal ) ;
18841 GNU Extension:
18843 asm-definition:
18844 asm volatile [opt] ( string-literal ) ;
18845 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18846 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18847 : asm-operand-list [opt] ) ;
18848 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18849 : asm-operand-list [opt]
18850 : asm-clobber-list [opt] ) ;
18851 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18852 : asm-clobber-list [opt]
18853 : asm-goto-list ) ; */
18855 static void
18856 cp_parser_asm_definition (cp_parser* parser)
18858 tree string;
18859 tree outputs = NULL_TREE;
18860 tree inputs = NULL_TREE;
18861 tree clobbers = NULL_TREE;
18862 tree labels = NULL_TREE;
18863 tree asm_stmt;
18864 bool volatile_p = false;
18865 bool extended_p = false;
18866 bool invalid_inputs_p = false;
18867 bool invalid_outputs_p = false;
18868 bool goto_p = false;
18869 required_token missing = RT_NONE;
18871 /* Look for the `asm' keyword. */
18872 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18874 if (parser->in_function_body
18875 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18877 error ("%<asm%> in %<constexpr%> function");
18878 cp_function_chain->invalid_constexpr = true;
18881 /* See if the next token is `volatile'. */
18882 if (cp_parser_allow_gnu_extensions_p (parser)
18883 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18885 /* Remember that we saw the `volatile' keyword. */
18886 volatile_p = true;
18887 /* Consume the token. */
18888 cp_lexer_consume_token (parser->lexer);
18890 if (cp_parser_allow_gnu_extensions_p (parser)
18891 && parser->in_function_body
18892 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18894 /* Remember that we saw the `goto' keyword. */
18895 goto_p = true;
18896 /* Consume the token. */
18897 cp_lexer_consume_token (parser->lexer);
18899 /* Look for the opening `('. */
18900 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18901 return;
18902 /* Look for the string. */
18903 string = cp_parser_string_literal (parser, false, false);
18904 if (string == error_mark_node)
18906 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18907 /*consume_paren=*/true);
18908 return;
18911 /* If we're allowing GNU extensions, check for the extended assembly
18912 syntax. Unfortunately, the `:' tokens need not be separated by
18913 a space in C, and so, for compatibility, we tolerate that here
18914 too. Doing that means that we have to treat the `::' operator as
18915 two `:' tokens. */
18916 if (cp_parser_allow_gnu_extensions_p (parser)
18917 && parser->in_function_body
18918 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18919 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18921 bool inputs_p = false;
18922 bool clobbers_p = false;
18923 bool labels_p = false;
18925 /* The extended syntax was used. */
18926 extended_p = true;
18928 /* Look for outputs. */
18929 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18931 /* Consume the `:'. */
18932 cp_lexer_consume_token (parser->lexer);
18933 /* Parse the output-operands. */
18934 if (cp_lexer_next_token_is_not (parser->lexer,
18935 CPP_COLON)
18936 && cp_lexer_next_token_is_not (parser->lexer,
18937 CPP_SCOPE)
18938 && cp_lexer_next_token_is_not (parser->lexer,
18939 CPP_CLOSE_PAREN)
18940 && !goto_p)
18942 outputs = cp_parser_asm_operand_list (parser);
18943 if (outputs == error_mark_node)
18944 invalid_outputs_p = true;
18947 /* If the next token is `::', there are no outputs, and the
18948 next token is the beginning of the inputs. */
18949 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18950 /* The inputs are coming next. */
18951 inputs_p = true;
18953 /* Look for inputs. */
18954 if (inputs_p
18955 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18957 /* Consume the `:' or `::'. */
18958 cp_lexer_consume_token (parser->lexer);
18959 /* Parse the output-operands. */
18960 if (cp_lexer_next_token_is_not (parser->lexer,
18961 CPP_COLON)
18962 && cp_lexer_next_token_is_not (parser->lexer,
18963 CPP_SCOPE)
18964 && cp_lexer_next_token_is_not (parser->lexer,
18965 CPP_CLOSE_PAREN))
18967 inputs = cp_parser_asm_operand_list (parser);
18968 if (inputs == error_mark_node)
18969 invalid_inputs_p = true;
18972 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18973 /* The clobbers are coming next. */
18974 clobbers_p = true;
18976 /* Look for clobbers. */
18977 if (clobbers_p
18978 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18980 clobbers_p = true;
18981 /* Consume the `:' or `::'. */
18982 cp_lexer_consume_token (parser->lexer);
18983 /* Parse the clobbers. */
18984 if (cp_lexer_next_token_is_not (parser->lexer,
18985 CPP_COLON)
18986 && cp_lexer_next_token_is_not (parser->lexer,
18987 CPP_CLOSE_PAREN))
18988 clobbers = cp_parser_asm_clobber_list (parser);
18990 else if (goto_p
18991 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18992 /* The labels are coming next. */
18993 labels_p = true;
18995 /* Look for labels. */
18996 if (labels_p
18997 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
18999 labels_p = true;
19000 /* Consume the `:' or `::'. */
19001 cp_lexer_consume_token (parser->lexer);
19002 /* Parse the labels. */
19003 labels = cp_parser_asm_label_list (parser);
19006 if (goto_p && !labels_p)
19007 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19009 else if (goto_p)
19010 missing = RT_COLON_SCOPE;
19012 /* Look for the closing `)'. */
19013 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19014 missing ? missing : RT_CLOSE_PAREN))
19015 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19016 /*consume_paren=*/true);
19017 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19019 if (!invalid_inputs_p && !invalid_outputs_p)
19021 /* Create the ASM_EXPR. */
19022 if (parser->in_function_body)
19024 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19025 inputs, clobbers, labels);
19026 /* If the extended syntax was not used, mark the ASM_EXPR. */
19027 if (!extended_p)
19029 tree temp = asm_stmt;
19030 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19031 temp = TREE_OPERAND (temp, 0);
19033 ASM_INPUT_P (temp) = 1;
19036 else
19037 symtab->finalize_toplevel_asm (string);
19041 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19042 type that comes from the decl-specifier-seq. */
19044 static tree
19045 strip_declarator_types (tree type, cp_declarator *declarator)
19047 for (cp_declarator *d = declarator; d;)
19048 switch (d->kind)
19050 case cdk_id:
19051 case cdk_decomp:
19052 case cdk_error:
19053 d = NULL;
19054 break;
19056 default:
19057 if (TYPE_PTRMEMFUNC_P (type))
19058 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19059 type = TREE_TYPE (type);
19060 d = d->declarator;
19061 break;
19064 return type;
19067 /* Declarators [gram.dcl.decl] */
19069 /* Parse an init-declarator.
19071 init-declarator:
19072 declarator initializer [opt]
19074 GNU Extension:
19076 init-declarator:
19077 declarator asm-specification [opt] attributes [opt] initializer [opt]
19079 function-definition:
19080 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19081 function-body
19082 decl-specifier-seq [opt] declarator function-try-block
19084 GNU Extension:
19086 function-definition:
19087 __extension__ function-definition
19089 TM Extension:
19091 function-definition:
19092 decl-specifier-seq [opt] declarator function-transaction-block
19094 The DECL_SPECIFIERS apply to this declarator. Returns a
19095 representation of the entity declared. If MEMBER_P is TRUE, then
19096 this declarator appears in a class scope. The new DECL created by
19097 this declarator is returned.
19099 The CHECKS are access checks that should be performed once we know
19100 what entity is being declared (and, therefore, what classes have
19101 befriended it).
19103 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19104 for a function-definition here as well. If the declarator is a
19105 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19106 be TRUE upon return. By that point, the function-definition will
19107 have been completely parsed.
19109 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19110 is FALSE.
19112 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19113 parsed declaration if it is an uninitialized single declarator not followed
19114 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19115 if present, will not be consumed. If returned, this declarator will be
19116 created with SD_INITIALIZED but will not call cp_finish_decl.
19118 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19119 and there is an initializer, the pointed location_t is set to the
19120 location of the '=' or `(', or '{' in C++11 token introducing the
19121 initializer. */
19123 static tree
19124 cp_parser_init_declarator (cp_parser* parser,
19125 cp_decl_specifier_seq *decl_specifiers,
19126 vec<deferred_access_check, va_gc> *checks,
19127 bool function_definition_allowed_p,
19128 bool member_p,
19129 int declares_class_or_enum,
19130 bool* function_definition_p,
19131 tree* maybe_range_for_decl,
19132 location_t* init_loc,
19133 tree* auto_result)
19135 cp_token *token = NULL, *asm_spec_start_token = NULL,
19136 *attributes_start_token = NULL;
19137 cp_declarator *declarator;
19138 tree prefix_attributes;
19139 tree attributes = NULL;
19140 tree asm_specification;
19141 tree initializer;
19142 tree decl = NULL_TREE;
19143 tree scope;
19144 int is_initialized;
19145 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19146 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19147 "(...)". */
19148 enum cpp_ttype initialization_kind;
19149 bool is_direct_init = false;
19150 bool is_non_constant_init;
19151 int ctor_dtor_or_conv_p;
19152 bool friend_p = cp_parser_friend_p (decl_specifiers);
19153 tree pushed_scope = NULL_TREE;
19154 bool range_for_decl_p = false;
19155 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19156 location_t tmp_init_loc = UNKNOWN_LOCATION;
19158 /* Gather the attributes that were provided with the
19159 decl-specifiers. */
19160 prefix_attributes = decl_specifiers->attributes;
19162 /* Assume that this is not the declarator for a function
19163 definition. */
19164 if (function_definition_p)
19165 *function_definition_p = false;
19167 /* Default arguments are only permitted for function parameters. */
19168 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19169 parser->default_arg_ok_p = false;
19171 /* Defer access checks while parsing the declarator; we cannot know
19172 what names are accessible until we know what is being
19173 declared. */
19174 resume_deferring_access_checks ();
19176 token = cp_lexer_peek_token (parser->lexer);
19178 /* Parse the declarator. */
19179 declarator
19180 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19181 &ctor_dtor_or_conv_p,
19182 /*parenthesized_p=*/NULL,
19183 member_p, friend_p);
19184 /* Gather up the deferred checks. */
19185 stop_deferring_access_checks ();
19187 parser->default_arg_ok_p = saved_default_arg_ok_p;
19189 /* If the DECLARATOR was erroneous, there's no need to go
19190 further. */
19191 if (declarator == cp_error_declarator)
19192 return error_mark_node;
19194 /* Check that the number of template-parameter-lists is OK. */
19195 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19196 token->location))
19197 return error_mark_node;
19199 if (declares_class_or_enum & 2)
19200 cp_parser_check_for_definition_in_return_type (declarator,
19201 decl_specifiers->type,
19202 decl_specifiers->locations[ds_type_spec]);
19204 /* Figure out what scope the entity declared by the DECLARATOR is
19205 located in. `grokdeclarator' sometimes changes the scope, so
19206 we compute it now. */
19207 scope = get_scope_of_declarator (declarator);
19209 /* Perform any lookups in the declared type which were thought to be
19210 dependent, but are not in the scope of the declarator. */
19211 decl_specifiers->type
19212 = maybe_update_decl_type (decl_specifiers->type, scope);
19214 /* If we're allowing GNU extensions, look for an
19215 asm-specification. */
19216 if (cp_parser_allow_gnu_extensions_p (parser))
19218 /* Look for an asm-specification. */
19219 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19220 asm_specification = cp_parser_asm_specification_opt (parser);
19222 else
19223 asm_specification = NULL_TREE;
19225 /* Look for attributes. */
19226 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19227 attributes = cp_parser_attributes_opt (parser);
19229 /* Peek at the next token. */
19230 token = cp_lexer_peek_token (parser->lexer);
19232 bool bogus_implicit_tmpl = false;
19234 if (function_declarator_p (declarator))
19236 /* Handle C++17 deduction guides. */
19237 if (!decl_specifiers->type
19238 && ctor_dtor_or_conv_p <= 0
19239 && cxx_dialect >= cxx1z)
19241 cp_declarator *id = get_id_declarator (declarator);
19242 tree name = id->u.id.unqualified_name;
19243 parser->scope = id->u.id.qualifying_scope;
19244 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19245 if (tmpl
19246 && (DECL_CLASS_TEMPLATE_P (tmpl)
19247 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19249 id->u.id.unqualified_name = dguide_name (tmpl);
19250 id->u.id.sfk = sfk_deduction_guide;
19251 ctor_dtor_or_conv_p = 1;
19255 /* Check to see if the token indicates the start of a
19256 function-definition. */
19257 if (cp_parser_token_starts_function_definition_p (token))
19259 if (!function_definition_allowed_p)
19261 /* If a function-definition should not appear here, issue an
19262 error message. */
19263 cp_parser_error (parser,
19264 "a function-definition is not allowed here");
19265 return error_mark_node;
19268 location_t func_brace_location
19269 = cp_lexer_peek_token (parser->lexer)->location;
19271 /* Neither attributes nor an asm-specification are allowed
19272 on a function-definition. */
19273 if (asm_specification)
19274 error_at (asm_spec_start_token->location,
19275 "an asm-specification is not allowed "
19276 "on a function-definition");
19277 if (attributes)
19278 error_at (attributes_start_token->location,
19279 "attributes are not allowed "
19280 "on a function-definition");
19281 /* This is a function-definition. */
19282 *function_definition_p = true;
19284 /* Parse the function definition. */
19285 if (member_p)
19286 decl = cp_parser_save_member_function_body (parser,
19287 decl_specifiers,
19288 declarator,
19289 prefix_attributes);
19290 else
19291 decl =
19292 (cp_parser_function_definition_from_specifiers_and_declarator
19293 (parser, decl_specifiers, prefix_attributes, declarator));
19295 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19297 /* This is where the prologue starts... */
19298 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19299 = func_brace_location;
19302 return decl;
19305 else if (parser->fully_implicit_function_template_p)
19307 /* A non-template declaration involving a function parameter list
19308 containing an implicit template parameter will be made into a
19309 template. If the resulting declaration is not going to be an
19310 actual function then finish the template scope here to prevent it.
19311 An error message will be issued once we have a decl to talk about.
19313 FIXME probably we should do type deduction rather than create an
19314 implicit template, but the standard currently doesn't allow it. */
19315 bogus_implicit_tmpl = true;
19316 finish_fully_implicit_template (parser, NULL_TREE);
19319 /* [dcl.dcl]
19321 Only in function declarations for constructors, destructors, type
19322 conversions, and deduction guides can the decl-specifier-seq be omitted.
19324 We explicitly postpone this check past the point where we handle
19325 function-definitions because we tolerate function-definitions
19326 that are missing their return types in some modes. */
19327 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19329 cp_parser_error (parser,
19330 "expected constructor, destructor, or type conversion");
19331 return error_mark_node;
19334 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19335 if (token->type == CPP_EQ
19336 || token->type == CPP_OPEN_PAREN
19337 || token->type == CPP_OPEN_BRACE)
19339 is_initialized = SD_INITIALIZED;
19340 initialization_kind = token->type;
19341 if (maybe_range_for_decl)
19342 *maybe_range_for_decl = error_mark_node;
19343 tmp_init_loc = token->location;
19344 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19345 *init_loc = tmp_init_loc;
19347 if (token->type == CPP_EQ
19348 && function_declarator_p (declarator))
19350 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19351 if (t2->keyword == RID_DEFAULT)
19352 is_initialized = SD_DEFAULTED;
19353 else if (t2->keyword == RID_DELETE)
19354 is_initialized = SD_DELETED;
19357 else
19359 /* If the init-declarator isn't initialized and isn't followed by a
19360 `,' or `;', it's not a valid init-declarator. */
19361 if (token->type != CPP_COMMA
19362 && token->type != CPP_SEMICOLON)
19364 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19365 range_for_decl_p = true;
19366 else
19368 if (!maybe_range_for_decl)
19369 cp_parser_error (parser, "expected initializer");
19370 return error_mark_node;
19373 is_initialized = SD_UNINITIALIZED;
19374 initialization_kind = CPP_EOF;
19377 /* Because start_decl has side-effects, we should only call it if we
19378 know we're going ahead. By this point, we know that we cannot
19379 possibly be looking at any other construct. */
19380 cp_parser_commit_to_tentative_parse (parser);
19382 /* Enter the newly declared entry in the symbol table. If we're
19383 processing a declaration in a class-specifier, we wait until
19384 after processing the initializer. */
19385 if (!member_p)
19387 if (parser->in_unbraced_linkage_specification_p)
19388 decl_specifiers->storage_class = sc_extern;
19389 decl = start_decl (declarator, decl_specifiers,
19390 range_for_decl_p? SD_INITIALIZED : is_initialized,
19391 attributes, prefix_attributes, &pushed_scope);
19392 cp_finalize_omp_declare_simd (parser, decl);
19393 cp_finalize_oacc_routine (parser, decl, false);
19394 /* Adjust location of decl if declarator->id_loc is more appropriate:
19395 set, and decl wasn't merged with another decl, in which case its
19396 location would be different from input_location, and more accurate. */
19397 if (DECL_P (decl)
19398 && declarator->id_loc != UNKNOWN_LOCATION
19399 && DECL_SOURCE_LOCATION (decl) == input_location)
19400 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19402 else if (scope)
19403 /* Enter the SCOPE. That way unqualified names appearing in the
19404 initializer will be looked up in SCOPE. */
19405 pushed_scope = push_scope (scope);
19407 /* Perform deferred access control checks, now that we know in which
19408 SCOPE the declared entity resides. */
19409 if (!member_p && decl)
19411 tree saved_current_function_decl = NULL_TREE;
19413 /* If the entity being declared is a function, pretend that we
19414 are in its scope. If it is a `friend', it may have access to
19415 things that would not otherwise be accessible. */
19416 if (TREE_CODE (decl) == FUNCTION_DECL)
19418 saved_current_function_decl = current_function_decl;
19419 current_function_decl = decl;
19422 /* Perform access checks for template parameters. */
19423 cp_parser_perform_template_parameter_access_checks (checks);
19425 /* Perform the access control checks for the declarator and the
19426 decl-specifiers. */
19427 perform_deferred_access_checks (tf_warning_or_error);
19429 /* Restore the saved value. */
19430 if (TREE_CODE (decl) == FUNCTION_DECL)
19431 current_function_decl = saved_current_function_decl;
19434 /* Parse the initializer. */
19435 initializer = NULL_TREE;
19436 is_direct_init = false;
19437 is_non_constant_init = true;
19438 if (is_initialized)
19440 if (function_declarator_p (declarator))
19442 if (initialization_kind == CPP_EQ)
19443 initializer = cp_parser_pure_specifier (parser);
19444 else
19446 /* If the declaration was erroneous, we don't really
19447 know what the user intended, so just silently
19448 consume the initializer. */
19449 if (decl != error_mark_node)
19450 error_at (tmp_init_loc, "initializer provided for function");
19451 cp_parser_skip_to_closing_parenthesis (parser,
19452 /*recovering=*/true,
19453 /*or_comma=*/false,
19454 /*consume_paren=*/true);
19457 else
19459 /* We want to record the extra mangling scope for in-class
19460 initializers of class members and initializers of static data
19461 member templates. The former involves deferring
19462 parsing of the initializer until end of class as with default
19463 arguments. So right here we only handle the latter. */
19464 if (!member_p && processing_template_decl)
19465 start_lambda_scope (decl);
19466 initializer = cp_parser_initializer (parser,
19467 &is_direct_init,
19468 &is_non_constant_init);
19469 if (!member_p && processing_template_decl)
19470 finish_lambda_scope ();
19471 if (initializer == error_mark_node)
19472 cp_parser_skip_to_end_of_statement (parser);
19476 /* The old parser allows attributes to appear after a parenthesized
19477 initializer. Mark Mitchell proposed removing this functionality
19478 on the GCC mailing lists on 2002-08-13. This parser accepts the
19479 attributes -- but ignores them. */
19480 if (cp_parser_allow_gnu_extensions_p (parser)
19481 && initialization_kind == CPP_OPEN_PAREN)
19482 if (cp_parser_attributes_opt (parser))
19483 warning (OPT_Wattributes,
19484 "attributes after parenthesized initializer ignored");
19486 /* And now complain about a non-function implicit template. */
19487 if (bogus_implicit_tmpl && decl != error_mark_node)
19488 error_at (DECL_SOURCE_LOCATION (decl),
19489 "non-function %qD declared as implicit template", decl);
19491 /* For an in-class declaration, use `grokfield' to create the
19492 declaration. */
19493 if (member_p)
19495 if (pushed_scope)
19497 pop_scope (pushed_scope);
19498 pushed_scope = NULL_TREE;
19500 decl = grokfield (declarator, decl_specifiers,
19501 initializer, !is_non_constant_init,
19502 /*asmspec=*/NULL_TREE,
19503 chainon (attributes, prefix_attributes));
19504 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19505 cp_parser_save_default_args (parser, decl);
19506 cp_finalize_omp_declare_simd (parser, decl);
19507 cp_finalize_oacc_routine (parser, decl, false);
19510 /* Finish processing the declaration. But, skip member
19511 declarations. */
19512 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19514 cp_finish_decl (decl,
19515 initializer, !is_non_constant_init,
19516 asm_specification,
19517 /* If the initializer is in parentheses, then this is
19518 a direct-initialization, which means that an
19519 `explicit' constructor is OK. Otherwise, an
19520 `explicit' constructor cannot be used. */
19521 ((is_direct_init || !is_initialized)
19522 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19524 else if ((cxx_dialect != cxx98) && friend_p
19525 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19526 /* Core issue #226 (C++0x only): A default template-argument
19527 shall not be specified in a friend class template
19528 declaration. */
19529 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19530 /*is_partial=*/false, /*is_friend_decl=*/1);
19532 if (!friend_p && pushed_scope)
19533 pop_scope (pushed_scope);
19535 if (function_declarator_p (declarator)
19536 && parser->fully_implicit_function_template_p)
19538 if (member_p)
19539 decl = finish_fully_implicit_template (parser, decl);
19540 else
19541 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19544 if (auto_result && is_initialized && decl_specifiers->type
19545 && type_uses_auto (decl_specifiers->type))
19546 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19548 return decl;
19551 /* Parse a declarator.
19553 declarator:
19554 direct-declarator
19555 ptr-operator declarator
19557 abstract-declarator:
19558 ptr-operator abstract-declarator [opt]
19559 direct-abstract-declarator
19561 GNU Extensions:
19563 declarator:
19564 attributes [opt] direct-declarator
19565 attributes [opt] ptr-operator declarator
19567 abstract-declarator:
19568 attributes [opt] ptr-operator abstract-declarator [opt]
19569 attributes [opt] direct-abstract-declarator
19571 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19572 detect constructors, destructors, deduction guides, or conversion operators.
19573 It is set to -1 if the declarator is a name, and +1 if it is a
19574 function. Otherwise it is set to zero. Usually you just want to
19575 test for >0, but internally the negative value is used.
19577 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19578 a decl-specifier-seq unless it declares a constructor, destructor,
19579 or conversion. It might seem that we could check this condition in
19580 semantic analysis, rather than parsing, but that makes it difficult
19581 to handle something like `f()'. We want to notice that there are
19582 no decl-specifiers, and therefore realize that this is an
19583 expression, not a declaration.)
19585 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19586 the declarator is a direct-declarator of the form "(...)".
19588 MEMBER_P is true iff this declarator is a member-declarator.
19590 FRIEND_P is true iff this declarator is a friend. */
19592 static cp_declarator *
19593 cp_parser_declarator (cp_parser* parser,
19594 cp_parser_declarator_kind dcl_kind,
19595 int* ctor_dtor_or_conv_p,
19596 bool* parenthesized_p,
19597 bool member_p, bool friend_p)
19599 cp_declarator *declarator;
19600 enum tree_code code;
19601 cp_cv_quals cv_quals;
19602 tree class_type;
19603 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19605 /* Assume this is not a constructor, destructor, or type-conversion
19606 operator. */
19607 if (ctor_dtor_or_conv_p)
19608 *ctor_dtor_or_conv_p = 0;
19610 if (cp_parser_allow_gnu_extensions_p (parser))
19611 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19613 /* Check for the ptr-operator production. */
19614 cp_parser_parse_tentatively (parser);
19615 /* Parse the ptr-operator. */
19616 code = cp_parser_ptr_operator (parser,
19617 &class_type,
19618 &cv_quals,
19619 &std_attributes);
19621 /* If that worked, then we have a ptr-operator. */
19622 if (cp_parser_parse_definitely (parser))
19624 /* If a ptr-operator was found, then this declarator was not
19625 parenthesized. */
19626 if (parenthesized_p)
19627 *parenthesized_p = true;
19628 /* The dependent declarator is optional if we are parsing an
19629 abstract-declarator. */
19630 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19631 cp_parser_parse_tentatively (parser);
19633 /* Parse the dependent declarator. */
19634 declarator = cp_parser_declarator (parser, dcl_kind,
19635 /*ctor_dtor_or_conv_p=*/NULL,
19636 /*parenthesized_p=*/NULL,
19637 /*member_p=*/false,
19638 friend_p);
19640 /* If we are parsing an abstract-declarator, we must handle the
19641 case where the dependent declarator is absent. */
19642 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19643 && !cp_parser_parse_definitely (parser))
19644 declarator = NULL;
19646 declarator = cp_parser_make_indirect_declarator
19647 (code, class_type, cv_quals, declarator, std_attributes);
19649 /* Everything else is a direct-declarator. */
19650 else
19652 if (parenthesized_p)
19653 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19654 CPP_OPEN_PAREN);
19655 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19656 ctor_dtor_or_conv_p,
19657 member_p, friend_p);
19660 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19661 declarator->attributes = gnu_attributes;
19662 return declarator;
19665 /* Parse a direct-declarator or direct-abstract-declarator.
19667 direct-declarator:
19668 declarator-id
19669 direct-declarator ( parameter-declaration-clause )
19670 cv-qualifier-seq [opt]
19671 ref-qualifier [opt]
19672 exception-specification [opt]
19673 direct-declarator [ constant-expression [opt] ]
19674 ( declarator )
19676 direct-abstract-declarator:
19677 direct-abstract-declarator [opt]
19678 ( parameter-declaration-clause )
19679 cv-qualifier-seq [opt]
19680 ref-qualifier [opt]
19681 exception-specification [opt]
19682 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19683 ( abstract-declarator )
19685 Returns a representation of the declarator. DCL_KIND is
19686 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19687 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19688 we are parsing a direct-declarator. It is
19689 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19690 of ambiguity we prefer an abstract declarator, as per
19691 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19692 as for cp_parser_declarator. */
19694 static cp_declarator *
19695 cp_parser_direct_declarator (cp_parser* parser,
19696 cp_parser_declarator_kind dcl_kind,
19697 int* ctor_dtor_or_conv_p,
19698 bool member_p, bool friend_p)
19700 cp_token *token;
19701 cp_declarator *declarator = NULL;
19702 tree scope = NULL_TREE;
19703 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19704 bool saved_in_declarator_p = parser->in_declarator_p;
19705 bool first = true;
19706 tree pushed_scope = NULL_TREE;
19708 while (true)
19710 /* Peek at the next token. */
19711 token = cp_lexer_peek_token (parser->lexer);
19712 if (token->type == CPP_OPEN_PAREN)
19714 /* This is either a parameter-declaration-clause, or a
19715 parenthesized declarator. When we know we are parsing a
19716 named declarator, it must be a parenthesized declarator
19717 if FIRST is true. For instance, `(int)' is a
19718 parameter-declaration-clause, with an omitted
19719 direct-abstract-declarator. But `((*))', is a
19720 parenthesized abstract declarator. Finally, when T is a
19721 template parameter `(T)' is a
19722 parameter-declaration-clause, and not a parenthesized
19723 named declarator.
19725 We first try and parse a parameter-declaration-clause,
19726 and then try a nested declarator (if FIRST is true).
19728 It is not an error for it not to be a
19729 parameter-declaration-clause, even when FIRST is
19730 false. Consider,
19732 int i (int);
19733 int i (3);
19735 The first is the declaration of a function while the
19736 second is the definition of a variable, including its
19737 initializer.
19739 Having seen only the parenthesis, we cannot know which of
19740 these two alternatives should be selected. Even more
19741 complex are examples like:
19743 int i (int (a));
19744 int i (int (3));
19746 The former is a function-declaration; the latter is a
19747 variable initialization.
19749 Thus again, we try a parameter-declaration-clause, and if
19750 that fails, we back out and return. */
19752 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19754 tree params;
19755 bool is_declarator = false;
19757 /* In a member-declarator, the only valid interpretation
19758 of a parenthesis is the start of a
19759 parameter-declaration-clause. (It is invalid to
19760 initialize a static data member with a parenthesized
19761 initializer; only the "=" form of initialization is
19762 permitted.) */
19763 if (!member_p)
19764 cp_parser_parse_tentatively (parser);
19766 /* Consume the `('. */
19767 cp_lexer_consume_token (parser->lexer);
19768 if (first)
19770 /* If this is going to be an abstract declarator, we're
19771 in a declarator and we can't have default args. */
19772 parser->default_arg_ok_p = false;
19773 parser->in_declarator_p = true;
19776 begin_scope (sk_function_parms, NULL_TREE);
19778 /* Parse the parameter-declaration-clause. */
19779 params = cp_parser_parameter_declaration_clause (parser);
19781 /* Consume the `)'. */
19782 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19784 /* If all went well, parse the cv-qualifier-seq,
19785 ref-qualifier and the exception-specification. */
19786 if (member_p || cp_parser_parse_definitely (parser))
19788 cp_cv_quals cv_quals;
19789 cp_virt_specifiers virt_specifiers;
19790 cp_ref_qualifier ref_qual;
19791 tree exception_specification;
19792 tree late_return;
19793 tree attrs;
19794 bool memfn = (member_p || (pushed_scope
19795 && CLASS_TYPE_P (pushed_scope)));
19797 is_declarator = true;
19799 if (ctor_dtor_or_conv_p)
19800 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19801 first = false;
19803 /* Parse the cv-qualifier-seq. */
19804 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19805 /* Parse the ref-qualifier. */
19806 ref_qual = cp_parser_ref_qualifier_opt (parser);
19807 /* Parse the tx-qualifier. */
19808 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19809 /* And the exception-specification. */
19810 exception_specification
19811 = cp_parser_exception_specification_opt (parser);
19813 attrs = cp_parser_std_attribute_spec_seq (parser);
19815 /* In here, we handle cases where attribute is used after
19816 the function declaration. For example:
19817 void func (int x) __attribute__((vector(..))); */
19818 tree gnu_attrs = NULL_TREE;
19819 if (flag_cilkplus
19820 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19822 cp_parser_parse_tentatively (parser);
19823 tree attr = cp_parser_gnu_attributes_opt (parser);
19824 if (cp_lexer_next_token_is_not (parser->lexer,
19825 CPP_SEMICOLON)
19826 && cp_lexer_next_token_is_not (parser->lexer,
19827 CPP_OPEN_BRACE))
19828 cp_parser_abort_tentative_parse (parser);
19829 else if (!cp_parser_parse_definitely (parser))
19831 else
19832 gnu_attrs = attr;
19834 tree requires_clause = NULL_TREE;
19835 late_return = (cp_parser_late_return_type_opt
19836 (parser, declarator, requires_clause,
19837 memfn ? cv_quals : -1));
19839 /* Parse the virt-specifier-seq. */
19840 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19842 /* Create the function-declarator. */
19843 declarator = make_call_declarator (declarator,
19844 params,
19845 cv_quals,
19846 virt_specifiers,
19847 ref_qual,
19848 tx_qual,
19849 exception_specification,
19850 late_return,
19851 requires_clause);
19852 declarator->std_attributes = attrs;
19853 declarator->attributes = gnu_attrs;
19854 /* Any subsequent parameter lists are to do with
19855 return type, so are not those of the declared
19856 function. */
19857 parser->default_arg_ok_p = false;
19860 /* Remove the function parms from scope. */
19861 pop_bindings_and_leave_scope ();
19863 if (is_declarator)
19864 /* Repeat the main loop. */
19865 continue;
19868 /* If this is the first, we can try a parenthesized
19869 declarator. */
19870 if (first)
19872 bool saved_in_type_id_in_expr_p;
19874 parser->default_arg_ok_p = saved_default_arg_ok_p;
19875 parser->in_declarator_p = saved_in_declarator_p;
19877 /* Consume the `('. */
19878 cp_lexer_consume_token (parser->lexer);
19879 /* Parse the nested declarator. */
19880 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19881 parser->in_type_id_in_expr_p = true;
19882 declarator
19883 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19884 /*parenthesized_p=*/NULL,
19885 member_p, friend_p);
19886 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19887 first = false;
19888 /* Expect a `)'. */
19889 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19890 declarator = cp_error_declarator;
19891 if (declarator == cp_error_declarator)
19892 break;
19894 goto handle_declarator;
19896 /* Otherwise, we must be done. */
19897 else
19898 break;
19900 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19901 && token->type == CPP_OPEN_SQUARE
19902 && !cp_next_tokens_can_be_attribute_p (parser))
19904 /* Parse an array-declarator. */
19905 tree bounds, attrs;
19907 if (ctor_dtor_or_conv_p)
19908 *ctor_dtor_or_conv_p = 0;
19910 first = false;
19911 parser->default_arg_ok_p = false;
19912 parser->in_declarator_p = true;
19913 /* Consume the `['. */
19914 cp_lexer_consume_token (parser->lexer);
19915 /* Peek at the next token. */
19916 token = cp_lexer_peek_token (parser->lexer);
19917 /* If the next token is `]', then there is no
19918 constant-expression. */
19919 if (token->type != CPP_CLOSE_SQUARE)
19921 bool non_constant_p;
19922 bounds
19923 = cp_parser_constant_expression (parser,
19924 /*allow_non_constant=*/true,
19925 &non_constant_p);
19926 if (!non_constant_p)
19927 /* OK */;
19928 else if (error_operand_p (bounds))
19929 /* Already gave an error. */;
19930 else if (!parser->in_function_body
19931 || current_binding_level->kind == sk_function_parms)
19933 /* Normally, the array bound must be an integral constant
19934 expression. However, as an extension, we allow VLAs
19935 in function scopes as long as they aren't part of a
19936 parameter declaration. */
19937 cp_parser_error (parser,
19938 "array bound is not an integer constant");
19939 bounds = error_mark_node;
19941 else if (processing_template_decl
19942 && !type_dependent_expression_p (bounds))
19944 /* Remember this wasn't a constant-expression. */
19945 bounds = build_nop (TREE_TYPE (bounds), bounds);
19946 TREE_SIDE_EFFECTS (bounds) = 1;
19949 else
19950 bounds = NULL_TREE;
19951 /* Look for the closing `]'. */
19952 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19954 declarator = cp_error_declarator;
19955 break;
19958 attrs = cp_parser_std_attribute_spec_seq (parser);
19959 declarator = make_array_declarator (declarator, bounds);
19960 declarator->std_attributes = attrs;
19962 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
19965 tree qualifying_scope;
19966 tree unqualified_name;
19967 tree attrs;
19968 special_function_kind sfk;
19969 bool abstract_ok;
19970 bool pack_expansion_p = false;
19971 cp_token *declarator_id_start_token;
19973 /* Parse a declarator-id */
19974 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
19975 if (abstract_ok)
19977 cp_parser_parse_tentatively (parser);
19979 /* If we see an ellipsis, we should be looking at a
19980 parameter pack. */
19981 if (token->type == CPP_ELLIPSIS)
19983 /* Consume the `...' */
19984 cp_lexer_consume_token (parser->lexer);
19986 pack_expansion_p = true;
19990 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
19991 unqualified_name
19992 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
19993 qualifying_scope = parser->scope;
19994 if (abstract_ok)
19996 bool okay = false;
19998 if (!unqualified_name && pack_expansion_p)
20000 /* Check whether an error occurred. */
20001 okay = !cp_parser_error_occurred (parser);
20003 /* We already consumed the ellipsis to mark a
20004 parameter pack, but we have no way to report it,
20005 so abort the tentative parse. We will be exiting
20006 immediately anyway. */
20007 cp_parser_abort_tentative_parse (parser);
20009 else
20010 okay = cp_parser_parse_definitely (parser);
20012 if (!okay)
20013 unqualified_name = error_mark_node;
20014 else if (unqualified_name
20015 && (qualifying_scope
20016 || (!identifier_p (unqualified_name))))
20018 cp_parser_error (parser, "expected unqualified-id");
20019 unqualified_name = error_mark_node;
20023 if (!unqualified_name)
20024 return NULL;
20025 if (unqualified_name == error_mark_node)
20027 declarator = cp_error_declarator;
20028 pack_expansion_p = false;
20029 declarator->parameter_pack_p = false;
20030 break;
20033 attrs = cp_parser_std_attribute_spec_seq (parser);
20035 if (qualifying_scope && at_namespace_scope_p ()
20036 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20038 /* In the declaration of a member of a template class
20039 outside of the class itself, the SCOPE will sometimes
20040 be a TYPENAME_TYPE. For example, given:
20042 template <typename T>
20043 int S<T>::R::i = 3;
20045 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20046 this context, we must resolve S<T>::R to an ordinary
20047 type, rather than a typename type.
20049 The reason we normally avoid resolving TYPENAME_TYPEs
20050 is that a specialization of `S' might render
20051 `S<T>::R' not a type. However, if `S' is
20052 specialized, then this `i' will not be used, so there
20053 is no harm in resolving the types here. */
20054 tree type;
20056 /* Resolve the TYPENAME_TYPE. */
20057 type = resolve_typename_type (qualifying_scope,
20058 /*only_current_p=*/false);
20059 /* If that failed, the declarator is invalid. */
20060 if (TREE_CODE (type) == TYPENAME_TYPE)
20062 if (typedef_variant_p (type))
20063 error_at (declarator_id_start_token->location,
20064 "cannot define member of dependent typedef "
20065 "%qT", type);
20066 else
20067 error_at (declarator_id_start_token->location,
20068 "%<%T::%E%> is not a type",
20069 TYPE_CONTEXT (qualifying_scope),
20070 TYPE_IDENTIFIER (qualifying_scope));
20072 qualifying_scope = type;
20075 sfk = sfk_none;
20077 if (unqualified_name)
20079 tree class_type;
20081 if (qualifying_scope
20082 && CLASS_TYPE_P (qualifying_scope))
20083 class_type = qualifying_scope;
20084 else
20085 class_type = current_class_type;
20087 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20089 tree name_type = TREE_TYPE (unqualified_name);
20090 if (class_type && same_type_p (name_type, class_type))
20092 if (qualifying_scope
20093 && CLASSTYPE_USE_TEMPLATE (name_type))
20095 error_at (declarator_id_start_token->location,
20096 "invalid use of constructor as a template");
20097 inform (declarator_id_start_token->location,
20098 "use %<%T::%D%> instead of %<%T::%D%> to "
20099 "name the constructor in a qualified name",
20100 class_type,
20101 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20102 class_type, name_type);
20103 declarator = cp_error_declarator;
20104 break;
20106 else
20107 unqualified_name = constructor_name (class_type);
20109 else
20111 /* We do not attempt to print the declarator
20112 here because we do not have enough
20113 information about its original syntactic
20114 form. */
20115 cp_parser_error (parser, "invalid declarator");
20116 declarator = cp_error_declarator;
20117 break;
20121 if (class_type)
20123 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20124 sfk = sfk_destructor;
20125 else if (identifier_p (unqualified_name)
20126 && IDENTIFIER_CONV_OP_P (unqualified_name))
20127 sfk = sfk_conversion;
20128 else if (/* There's no way to declare a constructor
20129 for an unnamed type, even if the type
20130 got a name for linkage purposes. */
20131 !TYPE_WAS_UNNAMED (class_type)
20132 /* Handle correctly (c++/19200):
20134 struct S {
20135 struct T{};
20136 friend void S(T);
20139 and also:
20141 namespace N {
20142 void S();
20145 struct S {
20146 friend void N::S();
20147 }; */
20148 && !(friend_p
20149 && class_type != qualifying_scope)
20150 && constructor_name_p (unqualified_name,
20151 class_type))
20153 unqualified_name = constructor_name (class_type);
20154 sfk = sfk_constructor;
20156 else if (is_overloaded_fn (unqualified_name)
20157 && DECL_CONSTRUCTOR_P (get_first_fn
20158 (unqualified_name)))
20159 sfk = sfk_constructor;
20161 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20162 *ctor_dtor_or_conv_p = -1;
20165 declarator = make_id_declarator (qualifying_scope,
20166 unqualified_name,
20167 sfk);
20168 declarator->std_attributes = attrs;
20169 declarator->id_loc = token->location;
20170 declarator->parameter_pack_p = pack_expansion_p;
20172 if (pack_expansion_p)
20173 maybe_warn_variadic_templates ();
20176 handle_declarator:;
20177 scope = get_scope_of_declarator (declarator);
20178 if (scope)
20180 /* Any names that appear after the declarator-id for a
20181 member are looked up in the containing scope. */
20182 if (at_function_scope_p ())
20184 /* But declarations with qualified-ids can't appear in a
20185 function. */
20186 cp_parser_error (parser, "qualified-id in declaration");
20187 declarator = cp_error_declarator;
20188 break;
20190 pushed_scope = push_scope (scope);
20192 parser->in_declarator_p = true;
20193 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20194 || (declarator && declarator->kind == cdk_id))
20195 /* Default args are only allowed on function
20196 declarations. */
20197 parser->default_arg_ok_p = saved_default_arg_ok_p;
20198 else
20199 parser->default_arg_ok_p = false;
20201 first = false;
20203 /* We're done. */
20204 else
20205 break;
20208 /* For an abstract declarator, we might wind up with nothing at this
20209 point. That's an error; the declarator is not optional. */
20210 if (!declarator)
20211 cp_parser_error (parser, "expected declarator");
20213 /* If we entered a scope, we must exit it now. */
20214 if (pushed_scope)
20215 pop_scope (pushed_scope);
20217 parser->default_arg_ok_p = saved_default_arg_ok_p;
20218 parser->in_declarator_p = saved_in_declarator_p;
20220 return declarator;
20223 /* Parse a ptr-operator.
20225 ptr-operator:
20226 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20227 * cv-qualifier-seq [opt]
20229 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20230 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20232 GNU Extension:
20234 ptr-operator:
20235 & cv-qualifier-seq [opt]
20237 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20238 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20239 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20240 filled in with the TYPE containing the member. *CV_QUALS is
20241 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20242 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20243 Note that the tree codes returned by this function have nothing
20244 to do with the types of trees that will be eventually be created
20245 to represent the pointer or reference type being parsed. They are
20246 just constants with suggestive names. */
20247 static enum tree_code
20248 cp_parser_ptr_operator (cp_parser* parser,
20249 tree* type,
20250 cp_cv_quals *cv_quals,
20251 tree *attributes)
20253 enum tree_code code = ERROR_MARK;
20254 cp_token *token;
20255 tree attrs = NULL_TREE;
20257 /* Assume that it's not a pointer-to-member. */
20258 *type = NULL_TREE;
20259 /* And that there are no cv-qualifiers. */
20260 *cv_quals = TYPE_UNQUALIFIED;
20262 /* Peek at the next token. */
20263 token = cp_lexer_peek_token (parser->lexer);
20265 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20266 if (token->type == CPP_MULT)
20267 code = INDIRECT_REF;
20268 else if (token->type == CPP_AND)
20269 code = ADDR_EXPR;
20270 else if ((cxx_dialect != cxx98) &&
20271 token->type == CPP_AND_AND) /* C++0x only */
20272 code = NON_LVALUE_EXPR;
20274 if (code != ERROR_MARK)
20276 /* Consume the `*', `&' or `&&'. */
20277 cp_lexer_consume_token (parser->lexer);
20279 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20280 `&', if we are allowing GNU extensions. (The only qualifier
20281 that can legally appear after `&' is `restrict', but that is
20282 enforced during semantic analysis. */
20283 if (code == INDIRECT_REF
20284 || cp_parser_allow_gnu_extensions_p (parser))
20285 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20287 attrs = cp_parser_std_attribute_spec_seq (parser);
20288 if (attributes != NULL)
20289 *attributes = attrs;
20291 else
20293 /* Try the pointer-to-member case. */
20294 cp_parser_parse_tentatively (parser);
20295 /* Look for the optional `::' operator. */
20296 cp_parser_global_scope_opt (parser,
20297 /*current_scope_valid_p=*/false);
20298 /* Look for the nested-name specifier. */
20299 token = cp_lexer_peek_token (parser->lexer);
20300 cp_parser_nested_name_specifier (parser,
20301 /*typename_keyword_p=*/false,
20302 /*check_dependency_p=*/true,
20303 /*type_p=*/false,
20304 /*is_declaration=*/false);
20305 /* If we found it, and the next token is a `*', then we are
20306 indeed looking at a pointer-to-member operator. */
20307 if (!cp_parser_error_occurred (parser)
20308 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20310 /* Indicate that the `*' operator was used. */
20311 code = INDIRECT_REF;
20313 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20314 error_at (token->location, "%qD is a namespace", parser->scope);
20315 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20316 error_at (token->location, "cannot form pointer to member of "
20317 "non-class %q#T", parser->scope);
20318 else
20320 /* The type of which the member is a member is given by the
20321 current SCOPE. */
20322 *type = parser->scope;
20323 /* The next name will not be qualified. */
20324 parser->scope = NULL_TREE;
20325 parser->qualifying_scope = NULL_TREE;
20326 parser->object_scope = NULL_TREE;
20327 /* Look for optional c++11 attributes. */
20328 attrs = cp_parser_std_attribute_spec_seq (parser);
20329 if (attributes != NULL)
20330 *attributes = attrs;
20331 /* Look for the optional cv-qualifier-seq. */
20332 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20335 /* If that didn't work we don't have a ptr-operator. */
20336 if (!cp_parser_parse_definitely (parser))
20337 cp_parser_error (parser, "expected ptr-operator");
20340 return code;
20343 /* Parse an (optional) cv-qualifier-seq.
20345 cv-qualifier-seq:
20346 cv-qualifier cv-qualifier-seq [opt]
20348 cv-qualifier:
20349 const
20350 volatile
20352 GNU Extension:
20354 cv-qualifier:
20355 __restrict__
20357 Returns a bitmask representing the cv-qualifiers. */
20359 static cp_cv_quals
20360 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20362 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20364 while (true)
20366 cp_token *token;
20367 cp_cv_quals cv_qualifier;
20369 /* Peek at the next token. */
20370 token = cp_lexer_peek_token (parser->lexer);
20371 /* See if it's a cv-qualifier. */
20372 switch (token->keyword)
20374 case RID_CONST:
20375 cv_qualifier = TYPE_QUAL_CONST;
20376 break;
20378 case RID_VOLATILE:
20379 cv_qualifier = TYPE_QUAL_VOLATILE;
20380 break;
20382 case RID_RESTRICT:
20383 cv_qualifier = TYPE_QUAL_RESTRICT;
20384 break;
20386 default:
20387 cv_qualifier = TYPE_UNQUALIFIED;
20388 break;
20391 if (!cv_qualifier)
20392 break;
20394 if (cv_quals & cv_qualifier)
20396 gcc_rich_location richloc (token->location);
20397 richloc.add_fixit_remove ();
20398 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20399 cp_lexer_purge_token (parser->lexer);
20401 else
20403 cp_lexer_consume_token (parser->lexer);
20404 cv_quals |= cv_qualifier;
20408 return cv_quals;
20411 /* Parse an (optional) ref-qualifier
20413 ref-qualifier:
20417 Returns cp_ref_qualifier representing ref-qualifier. */
20419 static cp_ref_qualifier
20420 cp_parser_ref_qualifier_opt (cp_parser* parser)
20422 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20424 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20425 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20426 return ref_qual;
20428 while (true)
20430 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20431 cp_token *token = cp_lexer_peek_token (parser->lexer);
20433 switch (token->type)
20435 case CPP_AND:
20436 curr_ref_qual = REF_QUAL_LVALUE;
20437 break;
20439 case CPP_AND_AND:
20440 curr_ref_qual = REF_QUAL_RVALUE;
20441 break;
20443 default:
20444 curr_ref_qual = REF_QUAL_NONE;
20445 break;
20448 if (!curr_ref_qual)
20449 break;
20450 else if (ref_qual)
20452 error_at (token->location, "multiple ref-qualifiers");
20453 cp_lexer_purge_token (parser->lexer);
20455 else
20457 ref_qual = curr_ref_qual;
20458 cp_lexer_consume_token (parser->lexer);
20462 return ref_qual;
20465 /* Parse an optional tx-qualifier.
20467 tx-qualifier:
20468 transaction_safe
20469 transaction_safe_dynamic */
20471 static tree
20472 cp_parser_tx_qualifier_opt (cp_parser *parser)
20474 cp_token *token = cp_lexer_peek_token (parser->lexer);
20475 if (token->type == CPP_NAME)
20477 tree name = token->u.value;
20478 const char *p = IDENTIFIER_POINTER (name);
20479 const int len = strlen ("transaction_safe");
20480 if (!strncmp (p, "transaction_safe", len))
20482 p += len;
20483 if (*p == '\0'
20484 || !strcmp (p, "_dynamic"))
20486 cp_lexer_consume_token (parser->lexer);
20487 if (!flag_tm)
20489 error ("%qE requires %<-fgnu-tm%>", name);
20490 return NULL_TREE;
20492 else
20493 return name;
20497 return NULL_TREE;
20500 /* Parse an (optional) virt-specifier-seq.
20502 virt-specifier-seq:
20503 virt-specifier virt-specifier-seq [opt]
20505 virt-specifier:
20506 override
20507 final
20509 Returns a bitmask representing the virt-specifiers. */
20511 static cp_virt_specifiers
20512 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20514 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20516 while (true)
20518 cp_token *token;
20519 cp_virt_specifiers virt_specifier;
20521 /* Peek at the next token. */
20522 token = cp_lexer_peek_token (parser->lexer);
20523 /* See if it's a virt-specifier-qualifier. */
20524 if (token->type != CPP_NAME)
20525 break;
20526 if (id_equal (token->u.value, "override"))
20528 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20529 virt_specifier = VIRT_SPEC_OVERRIDE;
20531 else if (id_equal (token->u.value, "final"))
20533 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20534 virt_specifier = VIRT_SPEC_FINAL;
20536 else if (id_equal (token->u.value, "__final"))
20538 virt_specifier = VIRT_SPEC_FINAL;
20540 else
20541 break;
20543 if (virt_specifiers & virt_specifier)
20545 gcc_rich_location richloc (token->location);
20546 richloc.add_fixit_remove ();
20547 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20548 cp_lexer_purge_token (parser->lexer);
20550 else
20552 cp_lexer_consume_token (parser->lexer);
20553 virt_specifiers |= virt_specifier;
20556 return virt_specifiers;
20559 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20560 is in scope even though it isn't real. */
20562 void
20563 inject_this_parameter (tree ctype, cp_cv_quals quals)
20565 tree this_parm;
20567 if (current_class_ptr)
20569 /* We don't clear this between NSDMIs. Is it already what we want? */
20570 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20571 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20572 && cp_type_quals (type) == quals)
20573 return;
20576 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20577 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20578 current_class_ptr = NULL_TREE;
20579 current_class_ref
20580 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20581 current_class_ptr = this_parm;
20584 /* Return true iff our current scope is a non-static data member
20585 initializer. */
20587 bool
20588 parsing_nsdmi (void)
20590 /* We recognize NSDMI context by the context-less 'this' pointer set up
20591 by the function above. */
20592 if (current_class_ptr
20593 && TREE_CODE (current_class_ptr) == PARM_DECL
20594 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20595 return true;
20596 return false;
20599 /* Return true iff our current scope is a default capturing generic lambda
20600 defined within a template. FIXME: This is part of a workaround (see
20601 semantics.c) to handle building lambda closure types correctly in templates
20602 which we ultimately want to defer to instantiation time. */
20604 bool
20605 parsing_default_capturing_generic_lambda_in_template (void)
20607 if (!processing_template_decl || !current_class_type)
20608 return false;
20610 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20611 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20612 return false;
20614 tree callop = lambda_function (lam);
20615 if (!callop)
20616 return false;
20618 return (DECL_TEMPLATE_INFO (callop)
20619 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20620 && ((current_nonlambda_class_type ()
20621 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20622 || ((current_nonlambda_function ()
20623 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20626 /* Parse a late-specified return type, if any. This is not a separate
20627 non-terminal, but part of a function declarator, which looks like
20629 -> trailing-type-specifier-seq abstract-declarator(opt)
20631 Returns the type indicated by the type-id.
20633 In addition to this, parse any queued up #pragma omp declare simd
20634 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20635 #pragma acc routine clauses.
20637 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20638 function. */
20640 static tree
20641 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20642 tree& requires_clause, cp_cv_quals quals)
20644 cp_token *token;
20645 tree type = NULL_TREE;
20646 bool declare_simd_p = (parser->omp_declare_simd
20647 && declarator
20648 && declarator->kind == cdk_id);
20650 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20651 && declarator && declarator->kind == cdk_id);
20653 bool oacc_routine_p = (parser->oacc_routine
20654 && declarator
20655 && declarator->kind == cdk_id);
20657 /* Peek at the next token. */
20658 token = cp_lexer_peek_token (parser->lexer);
20659 /* A late-specified return type is indicated by an initial '->'. */
20660 if (token->type != CPP_DEREF
20661 && token->keyword != RID_REQUIRES
20662 && !(token->type == CPP_NAME
20663 && token->u.value == ridpointers[RID_REQUIRES])
20664 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20665 return NULL_TREE;
20667 tree save_ccp = current_class_ptr;
20668 tree save_ccr = current_class_ref;
20669 if (quals >= 0)
20671 /* DR 1207: 'this' is in scope in the trailing return type. */
20672 inject_this_parameter (current_class_type, quals);
20675 if (token->type == CPP_DEREF)
20677 /* Consume the ->. */
20678 cp_lexer_consume_token (parser->lexer);
20680 type = cp_parser_trailing_type_id (parser);
20683 /* Function declarations may be followed by a trailing
20684 requires-clause. */
20685 requires_clause = cp_parser_requires_clause_opt (parser);
20687 if (cilk_simd_fn_vector_p)
20688 declarator->attributes
20689 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20690 declarator->attributes);
20691 if (declare_simd_p)
20692 declarator->attributes
20693 = cp_parser_late_parsing_omp_declare_simd (parser,
20694 declarator->attributes);
20695 if (oacc_routine_p)
20696 declarator->attributes
20697 = cp_parser_late_parsing_oacc_routine (parser,
20698 declarator->attributes);
20700 if (quals >= 0)
20702 current_class_ptr = save_ccp;
20703 current_class_ref = save_ccr;
20706 return type;
20709 /* Parse a declarator-id.
20711 declarator-id:
20712 id-expression
20713 :: [opt] nested-name-specifier [opt] type-name
20715 In the `id-expression' case, the value returned is as for
20716 cp_parser_id_expression if the id-expression was an unqualified-id.
20717 If the id-expression was a qualified-id, then a SCOPE_REF is
20718 returned. The first operand is the scope (either a NAMESPACE_DECL
20719 or TREE_TYPE), but the second is still just a representation of an
20720 unqualified-id. */
20722 static tree
20723 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20725 tree id;
20726 /* The expression must be an id-expression. Assume that qualified
20727 names are the names of types so that:
20729 template <class T>
20730 int S<T>::R::i = 3;
20732 will work; we must treat `S<T>::R' as the name of a type.
20733 Similarly, assume that qualified names are templates, where
20734 required, so that:
20736 template <class T>
20737 int S<T>::R<T>::i = 3;
20739 will work, too. */
20740 id = cp_parser_id_expression (parser,
20741 /*template_keyword_p=*/false,
20742 /*check_dependency_p=*/false,
20743 /*template_p=*/NULL,
20744 /*declarator_p=*/true,
20745 optional_p);
20746 if (id && BASELINK_P (id))
20747 id = BASELINK_FUNCTIONS (id);
20748 return id;
20751 /* Parse a type-id.
20753 type-id:
20754 type-specifier-seq abstract-declarator [opt]
20756 Returns the TYPE specified. */
20758 static tree
20759 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20760 bool is_trailing_return)
20762 cp_decl_specifier_seq type_specifier_seq;
20763 cp_declarator *abstract_declarator;
20765 /* Parse the type-specifier-seq. */
20766 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20767 is_trailing_return,
20768 &type_specifier_seq);
20769 if (is_template_arg && type_specifier_seq.type
20770 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20771 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20772 /* A bare template name as a template argument is a template template
20773 argument, not a placeholder, so fail parsing it as a type argument. */
20775 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20776 cp_parser_simulate_error (parser);
20777 return error_mark_node;
20779 if (type_specifier_seq.type == error_mark_node)
20780 return error_mark_node;
20782 /* There might or might not be an abstract declarator. */
20783 cp_parser_parse_tentatively (parser);
20784 /* Look for the declarator. */
20785 abstract_declarator
20786 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20787 /*parenthesized_p=*/NULL,
20788 /*member_p=*/false,
20789 /*friend_p=*/false);
20790 /* Check to see if there really was a declarator. */
20791 if (!cp_parser_parse_definitely (parser))
20792 abstract_declarator = NULL;
20794 if (type_specifier_seq.type
20795 /* The concepts TS allows 'auto' as a type-id. */
20796 && (!flag_concepts || parser->in_type_id_in_expr_p)
20797 /* None of the valid uses of 'auto' in C++14 involve the type-id
20798 nonterminal, but it is valid in a trailing-return-type. */
20799 && !(cxx_dialect >= cxx14 && is_trailing_return))
20800 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20802 /* A type-id with type 'auto' is only ok if the abstract declarator
20803 is a function declarator with a late-specified return type.
20805 A type-id with 'auto' is also valid in a trailing-return-type
20806 in a compound-requirement. */
20807 if (abstract_declarator
20808 && abstract_declarator->kind == cdk_function
20809 && abstract_declarator->u.function.late_return_type)
20810 /* OK */;
20811 else if (parser->in_result_type_constraint_p)
20812 /* OK */;
20813 else
20815 location_t loc = type_specifier_seq.locations[ds_type_spec];
20816 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20818 error_at (loc, "missing template arguments after %qT",
20819 auto_node);
20820 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20821 tmpl);
20823 else
20824 error_at (loc, "invalid use of %qT", auto_node);
20825 return error_mark_node;
20829 return groktypename (&type_specifier_seq, abstract_declarator,
20830 is_template_arg);
20833 static tree
20834 cp_parser_type_id (cp_parser *parser)
20836 return cp_parser_type_id_1 (parser, false, false);
20839 static tree
20840 cp_parser_template_type_arg (cp_parser *parser)
20842 tree r;
20843 const char *saved_message = parser->type_definition_forbidden_message;
20844 parser->type_definition_forbidden_message
20845 = G_("types may not be defined in template arguments");
20846 r = cp_parser_type_id_1 (parser, true, false);
20847 parser->type_definition_forbidden_message = saved_message;
20848 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20850 error ("invalid use of %<auto%> in template argument");
20851 r = error_mark_node;
20853 return r;
20856 static tree
20857 cp_parser_trailing_type_id (cp_parser *parser)
20859 return cp_parser_type_id_1 (parser, false, true);
20862 /* Parse a type-specifier-seq.
20864 type-specifier-seq:
20865 type-specifier type-specifier-seq [opt]
20867 GNU extension:
20869 type-specifier-seq:
20870 attributes type-specifier-seq [opt]
20872 If IS_DECLARATION is true, we are at the start of a "condition" or
20873 exception-declaration, so we might be followed by a declarator-id.
20875 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20876 i.e. we've just seen "->".
20878 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20880 static void
20881 cp_parser_type_specifier_seq (cp_parser* parser,
20882 bool is_declaration,
20883 bool is_trailing_return,
20884 cp_decl_specifier_seq *type_specifier_seq)
20886 bool seen_type_specifier = false;
20887 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20888 cp_token *start_token = NULL;
20890 /* Clear the TYPE_SPECIFIER_SEQ. */
20891 clear_decl_specs (type_specifier_seq);
20893 /* In the context of a trailing return type, enum E { } is an
20894 elaborated-type-specifier followed by a function-body, not an
20895 enum-specifier. */
20896 if (is_trailing_return)
20897 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20899 /* Parse the type-specifiers and attributes. */
20900 while (true)
20902 tree type_specifier;
20903 bool is_cv_qualifier;
20905 /* Check for attributes first. */
20906 if (cp_next_tokens_can_be_attribute_p (parser))
20908 type_specifier_seq->attributes =
20909 chainon (type_specifier_seq->attributes,
20910 cp_parser_attributes_opt (parser));
20911 continue;
20914 /* record the token of the beginning of the type specifier seq,
20915 for error reporting purposes*/
20916 if (!start_token)
20917 start_token = cp_lexer_peek_token (parser->lexer);
20919 /* Look for the type-specifier. */
20920 type_specifier = cp_parser_type_specifier (parser,
20921 flags,
20922 type_specifier_seq,
20923 /*is_declaration=*/false,
20924 NULL,
20925 &is_cv_qualifier);
20926 if (!type_specifier)
20928 /* If the first type-specifier could not be found, this is not a
20929 type-specifier-seq at all. */
20930 if (!seen_type_specifier)
20932 /* Set in_declarator_p to avoid skipping to the semicolon. */
20933 int in_decl = parser->in_declarator_p;
20934 parser->in_declarator_p = true;
20936 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20937 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20938 cp_parser_error (parser, "expected type-specifier");
20940 parser->in_declarator_p = in_decl;
20942 type_specifier_seq->type = error_mark_node;
20943 return;
20945 /* If subsequent type-specifiers could not be found, the
20946 type-specifier-seq is complete. */
20947 break;
20950 seen_type_specifier = true;
20951 /* The standard says that a condition can be:
20953 type-specifier-seq declarator = assignment-expression
20955 However, given:
20957 struct S {};
20958 if (int S = ...)
20960 we should treat the "S" as a declarator, not as a
20961 type-specifier. The standard doesn't say that explicitly for
20962 type-specifier-seq, but it does say that for
20963 decl-specifier-seq in an ordinary declaration. Perhaps it
20964 would be clearer just to allow a decl-specifier-seq here, and
20965 then add a semantic restriction that if any decl-specifiers
20966 that are not type-specifiers appear, the program is invalid. */
20967 if (is_declaration && !is_cv_qualifier)
20968 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
20972 /* Return whether the function currently being declared has an associated
20973 template parameter list. */
20975 static bool
20976 function_being_declared_is_template_p (cp_parser* parser)
20978 if (!current_template_parms || processing_template_parmlist)
20979 return false;
20981 if (parser->implicit_template_scope)
20982 return true;
20984 if (at_class_scope_p ()
20985 && TYPE_BEING_DEFINED (current_class_type))
20986 return parser->num_template_parameter_lists != 0;
20988 return ((int) parser->num_template_parameter_lists > template_class_depth
20989 (current_class_type));
20992 /* Parse a parameter-declaration-clause.
20994 parameter-declaration-clause:
20995 parameter-declaration-list [opt] ... [opt]
20996 parameter-declaration-list , ...
20998 Returns a representation for the parameter declarations. A return
20999 value of NULL indicates a parameter-declaration-clause consisting
21000 only of an ellipsis. */
21002 static tree
21003 cp_parser_parameter_declaration_clause (cp_parser* parser)
21005 tree parameters;
21006 cp_token *token;
21007 bool ellipsis_p;
21008 bool is_error;
21010 struct cleanup {
21011 cp_parser* parser;
21012 int auto_is_implicit_function_template_parm_p;
21013 ~cleanup() {
21014 parser->auto_is_implicit_function_template_parm_p
21015 = auto_is_implicit_function_template_parm_p;
21017 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21019 (void) cleanup;
21021 if (!processing_specialization
21022 && !processing_template_parmlist
21023 && !processing_explicit_instantiation)
21024 if (!current_function_decl
21025 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21026 parser->auto_is_implicit_function_template_parm_p = true;
21028 /* Peek at the next token. */
21029 token = cp_lexer_peek_token (parser->lexer);
21030 /* Check for trivial parameter-declaration-clauses. */
21031 if (token->type == CPP_ELLIPSIS)
21033 /* Consume the `...' token. */
21034 cp_lexer_consume_token (parser->lexer);
21035 return NULL_TREE;
21037 else if (token->type == CPP_CLOSE_PAREN)
21038 /* There are no parameters. */
21040 #ifndef NO_IMPLICIT_EXTERN_C
21041 if (in_system_header_at (input_location)
21042 && current_class_type == NULL
21043 && current_lang_name == lang_name_c)
21044 return NULL_TREE;
21045 else
21046 #endif
21047 return void_list_node;
21049 /* Check for `(void)', too, which is a special case. */
21050 else if (token->keyword == RID_VOID
21051 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21052 == CPP_CLOSE_PAREN))
21054 /* Consume the `void' token. */
21055 cp_lexer_consume_token (parser->lexer);
21056 /* There are no parameters. */
21057 return void_list_node;
21060 /* Parse the parameter-declaration-list. */
21061 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21062 /* If a parse error occurred while parsing the
21063 parameter-declaration-list, then the entire
21064 parameter-declaration-clause is erroneous. */
21065 if (is_error)
21066 return NULL;
21068 /* Peek at the next token. */
21069 token = cp_lexer_peek_token (parser->lexer);
21070 /* If it's a `,', the clause should terminate with an ellipsis. */
21071 if (token->type == CPP_COMMA)
21073 /* Consume the `,'. */
21074 cp_lexer_consume_token (parser->lexer);
21075 /* Expect an ellipsis. */
21076 ellipsis_p
21077 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21079 /* It might also be `...' if the optional trailing `,' was
21080 omitted. */
21081 else if (token->type == CPP_ELLIPSIS)
21083 /* Consume the `...' token. */
21084 cp_lexer_consume_token (parser->lexer);
21085 /* And remember that we saw it. */
21086 ellipsis_p = true;
21088 else
21089 ellipsis_p = false;
21091 /* Finish the parameter list. */
21092 if (!ellipsis_p)
21093 parameters = chainon (parameters, void_list_node);
21095 return parameters;
21098 /* Parse a parameter-declaration-list.
21100 parameter-declaration-list:
21101 parameter-declaration
21102 parameter-declaration-list , parameter-declaration
21104 Returns a representation of the parameter-declaration-list, as for
21105 cp_parser_parameter_declaration_clause. However, the
21106 `void_list_node' is never appended to the list. Upon return,
21107 *IS_ERROR will be true iff an error occurred. */
21109 static tree
21110 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21112 tree parameters = NULL_TREE;
21113 tree *tail = &parameters;
21114 bool saved_in_unbraced_linkage_specification_p;
21115 int index = 0;
21117 /* Assume all will go well. */
21118 *is_error = false;
21119 /* The special considerations that apply to a function within an
21120 unbraced linkage specifications do not apply to the parameters
21121 to the function. */
21122 saved_in_unbraced_linkage_specification_p
21123 = parser->in_unbraced_linkage_specification_p;
21124 parser->in_unbraced_linkage_specification_p = false;
21126 /* Look for more parameters. */
21127 while (true)
21129 cp_parameter_declarator *parameter;
21130 tree decl = error_mark_node;
21131 bool parenthesized_p = false;
21132 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21133 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21134 (current_template_parms)) : 0);
21136 /* Parse the parameter. */
21137 parameter
21138 = cp_parser_parameter_declaration (parser,
21139 /*template_parm_p=*/false,
21140 &parenthesized_p);
21142 /* We don't know yet if the enclosing context is deprecated, so wait
21143 and warn in grokparms if appropriate. */
21144 deprecated_state = DEPRECATED_SUPPRESS;
21146 if (parameter)
21148 /* If a function parameter pack was specified and an implicit template
21149 parameter was introduced during cp_parser_parameter_declaration,
21150 change any implicit parameters introduced into packs. */
21151 if (parser->implicit_template_parms
21152 && parameter->declarator
21153 && parameter->declarator->parameter_pack_p)
21155 int latest_template_parm_idx = TREE_VEC_LENGTH
21156 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21158 if (latest_template_parm_idx != template_parm_idx)
21159 parameter->decl_specifiers.type = convert_generic_types_to_packs
21160 (parameter->decl_specifiers.type,
21161 template_parm_idx, latest_template_parm_idx);
21164 decl = grokdeclarator (parameter->declarator,
21165 &parameter->decl_specifiers,
21166 PARM,
21167 parameter->default_argument != NULL_TREE,
21168 &parameter->decl_specifiers.attributes);
21171 deprecated_state = DEPRECATED_NORMAL;
21173 /* If a parse error occurred parsing the parameter declaration,
21174 then the entire parameter-declaration-list is erroneous. */
21175 if (decl == error_mark_node)
21177 *is_error = true;
21178 parameters = error_mark_node;
21179 break;
21182 if (parameter->decl_specifiers.attributes)
21183 cplus_decl_attributes (&decl,
21184 parameter->decl_specifiers.attributes,
21186 if (DECL_NAME (decl))
21187 decl = pushdecl (decl);
21189 if (decl != error_mark_node)
21191 retrofit_lang_decl (decl);
21192 DECL_PARM_INDEX (decl) = ++index;
21193 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21196 /* Add the new parameter to the list. */
21197 *tail = build_tree_list (parameter->default_argument, decl);
21198 tail = &TREE_CHAIN (*tail);
21200 /* Peek at the next token. */
21201 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21202 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21203 /* These are for Objective-C++ */
21204 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21205 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21206 /* The parameter-declaration-list is complete. */
21207 break;
21208 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21210 cp_token *token;
21212 /* Peek at the next token. */
21213 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21214 /* If it's an ellipsis, then the list is complete. */
21215 if (token->type == CPP_ELLIPSIS)
21216 break;
21217 /* Otherwise, there must be more parameters. Consume the
21218 `,'. */
21219 cp_lexer_consume_token (parser->lexer);
21220 /* When parsing something like:
21222 int i(float f, double d)
21224 we can tell after seeing the declaration for "f" that we
21225 are not looking at an initialization of a variable "i",
21226 but rather at the declaration of a function "i".
21228 Due to the fact that the parsing of template arguments
21229 (as specified to a template-id) requires backtracking we
21230 cannot use this technique when inside a template argument
21231 list. */
21232 if (!parser->in_template_argument_list_p
21233 && !parser->in_type_id_in_expr_p
21234 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21235 /* However, a parameter-declaration of the form
21236 "float(f)" (which is a valid declaration of a
21237 parameter "f") can also be interpreted as an
21238 expression (the conversion of "f" to "float"). */
21239 && !parenthesized_p)
21240 cp_parser_commit_to_tentative_parse (parser);
21242 else
21244 cp_parser_error (parser, "expected %<,%> or %<...%>");
21245 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21246 cp_parser_skip_to_closing_parenthesis (parser,
21247 /*recovering=*/true,
21248 /*or_comma=*/false,
21249 /*consume_paren=*/false);
21250 break;
21254 parser->in_unbraced_linkage_specification_p
21255 = saved_in_unbraced_linkage_specification_p;
21257 /* Reset implicit_template_scope if we are about to leave the function
21258 parameter list that introduced it. Note that for out-of-line member
21259 definitions, there will be one or more class scopes before we get to
21260 the template parameter scope. */
21262 if (cp_binding_level *its = parser->implicit_template_scope)
21263 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21265 while (maybe_its->kind == sk_class)
21266 maybe_its = maybe_its->level_chain;
21267 if (maybe_its == its)
21269 parser->implicit_template_parms = 0;
21270 parser->implicit_template_scope = 0;
21274 return parameters;
21277 /* Parse a parameter declaration.
21279 parameter-declaration:
21280 decl-specifier-seq ... [opt] declarator
21281 decl-specifier-seq declarator = assignment-expression
21282 decl-specifier-seq ... [opt] abstract-declarator [opt]
21283 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21285 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21286 declares a template parameter. (In that case, a non-nested `>'
21287 token encountered during the parsing of the assignment-expression
21288 is not interpreted as a greater-than operator.)
21290 Returns a representation of the parameter, or NULL if an error
21291 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21292 true iff the declarator is of the form "(p)". */
21294 static cp_parameter_declarator *
21295 cp_parser_parameter_declaration (cp_parser *parser,
21296 bool template_parm_p,
21297 bool *parenthesized_p)
21299 int declares_class_or_enum;
21300 cp_decl_specifier_seq decl_specifiers;
21301 cp_declarator *declarator;
21302 tree default_argument;
21303 cp_token *token = NULL, *declarator_token_start = NULL;
21304 const char *saved_message;
21305 bool template_parameter_pack_p = false;
21307 /* In a template parameter, `>' is not an operator.
21309 [temp.param]
21311 When parsing a default template-argument for a non-type
21312 template-parameter, the first non-nested `>' is taken as the end
21313 of the template parameter-list rather than a greater-than
21314 operator. */
21316 /* Type definitions may not appear in parameter types. */
21317 saved_message = parser->type_definition_forbidden_message;
21318 parser->type_definition_forbidden_message
21319 = G_("types may not be defined in parameter types");
21321 /* Parse the declaration-specifiers. */
21322 cp_parser_decl_specifier_seq (parser,
21323 CP_PARSER_FLAGS_NONE,
21324 &decl_specifiers,
21325 &declares_class_or_enum);
21327 /* Complain about missing 'typename' or other invalid type names. */
21328 if (!decl_specifiers.any_type_specifiers_p
21329 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21330 decl_specifiers.type = error_mark_node;
21332 /* If an error occurred, there's no reason to attempt to parse the
21333 rest of the declaration. */
21334 if (cp_parser_error_occurred (parser))
21336 parser->type_definition_forbidden_message = saved_message;
21337 return NULL;
21340 /* Peek at the next token. */
21341 token = cp_lexer_peek_token (parser->lexer);
21343 /* If the next token is a `)', `,', `=', `>', or `...', then there
21344 is no declarator. However, when variadic templates are enabled,
21345 there may be a declarator following `...'. */
21346 if (token->type == CPP_CLOSE_PAREN
21347 || token->type == CPP_COMMA
21348 || token->type == CPP_EQ
21349 || token->type == CPP_GREATER)
21351 declarator = NULL;
21352 if (parenthesized_p)
21353 *parenthesized_p = false;
21355 /* Otherwise, there should be a declarator. */
21356 else
21358 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21359 parser->default_arg_ok_p = false;
21361 /* After seeing a decl-specifier-seq, if the next token is not a
21362 "(", there is no possibility that the code is a valid
21363 expression. Therefore, if parsing tentatively, we commit at
21364 this point. */
21365 if (!parser->in_template_argument_list_p
21366 /* In an expression context, having seen:
21368 (int((char ...
21370 we cannot be sure whether we are looking at a
21371 function-type (taking a "char" as a parameter) or a cast
21372 of some object of type "char" to "int". */
21373 && !parser->in_type_id_in_expr_p
21374 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21375 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21376 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21377 cp_parser_commit_to_tentative_parse (parser);
21378 /* Parse the declarator. */
21379 declarator_token_start = token;
21380 declarator = cp_parser_declarator (parser,
21381 CP_PARSER_DECLARATOR_EITHER,
21382 /*ctor_dtor_or_conv_p=*/NULL,
21383 parenthesized_p,
21384 /*member_p=*/false,
21385 /*friend_p=*/false);
21386 parser->default_arg_ok_p = saved_default_arg_ok_p;
21387 /* After the declarator, allow more attributes. */
21388 decl_specifiers.attributes
21389 = chainon (decl_specifiers.attributes,
21390 cp_parser_attributes_opt (parser));
21392 /* If the declarator is a template parameter pack, remember that and
21393 clear the flag in the declarator itself so we don't get errors
21394 from grokdeclarator. */
21395 if (template_parm_p && declarator && declarator->parameter_pack_p)
21397 declarator->parameter_pack_p = false;
21398 template_parameter_pack_p = true;
21402 /* If the next token is an ellipsis, and we have not seen a declarator
21403 name, and if either the type of the declarator contains parameter
21404 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21405 for, eg, abbreviated integral type names), then we actually have a
21406 parameter pack expansion expression. Otherwise, leave the ellipsis
21407 for a C-style variadic function. */
21408 token = cp_lexer_peek_token (parser->lexer);
21409 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21411 tree type = decl_specifiers.type;
21413 if (type && DECL_P (type))
21414 type = TREE_TYPE (type);
21416 if (((type
21417 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21418 && (template_parm_p || uses_parameter_packs (type)))
21419 || (!type && template_parm_p))
21420 && declarator_can_be_parameter_pack (declarator))
21422 /* Consume the `...'. */
21423 cp_lexer_consume_token (parser->lexer);
21424 maybe_warn_variadic_templates ();
21426 /* Build a pack expansion type */
21427 if (template_parm_p)
21428 template_parameter_pack_p = true;
21429 else if (declarator)
21430 declarator->parameter_pack_p = true;
21431 else
21432 decl_specifiers.type = make_pack_expansion (type);
21436 /* The restriction on defining new types applies only to the type
21437 of the parameter, not to the default argument. */
21438 parser->type_definition_forbidden_message = saved_message;
21440 /* If the next token is `=', then process a default argument. */
21441 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21443 tree type = decl_specifiers.type;
21444 token = cp_lexer_peek_token (parser->lexer);
21445 /* If we are defining a class, then the tokens that make up the
21446 default argument must be saved and processed later. */
21447 if (!template_parm_p && at_class_scope_p ()
21448 && TYPE_BEING_DEFINED (current_class_type)
21449 && !LAMBDA_TYPE_P (current_class_type))
21450 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21452 // A constrained-type-specifier may declare a type template-parameter.
21453 else if (declares_constrained_type_template_parameter (type))
21454 default_argument
21455 = cp_parser_default_type_template_argument (parser);
21457 // A constrained-type-specifier may declare a template-template-parameter.
21458 else if (declares_constrained_template_template_parameter (type))
21459 default_argument
21460 = cp_parser_default_template_template_argument (parser);
21462 /* Outside of a class definition, we can just parse the
21463 assignment-expression. */
21464 else
21465 default_argument
21466 = cp_parser_default_argument (parser, template_parm_p);
21468 if (!parser->default_arg_ok_p)
21470 permerror (token->location,
21471 "default arguments are only "
21472 "permitted for function parameters");
21474 else if ((declarator && declarator->parameter_pack_p)
21475 || template_parameter_pack_p
21476 || (decl_specifiers.type
21477 && PACK_EXPANSION_P (decl_specifiers.type)))
21479 /* Find the name of the parameter pack. */
21480 cp_declarator *id_declarator = declarator;
21481 while (id_declarator && id_declarator->kind != cdk_id)
21482 id_declarator = id_declarator->declarator;
21484 if (id_declarator && id_declarator->kind == cdk_id)
21485 error_at (declarator_token_start->location,
21486 template_parm_p
21487 ? G_("template parameter pack %qD "
21488 "cannot have a default argument")
21489 : G_("parameter pack %qD cannot have "
21490 "a default argument"),
21491 id_declarator->u.id.unqualified_name);
21492 else
21493 error_at (declarator_token_start->location,
21494 template_parm_p
21495 ? G_("template parameter pack cannot have "
21496 "a default argument")
21497 : G_("parameter pack cannot have a "
21498 "default argument"));
21500 default_argument = NULL_TREE;
21503 else
21504 default_argument = NULL_TREE;
21506 return make_parameter_declarator (&decl_specifiers,
21507 declarator,
21508 default_argument,
21509 template_parameter_pack_p);
21512 /* Parse a default argument and return it.
21514 TEMPLATE_PARM_P is true if this is a default argument for a
21515 non-type template parameter. */
21516 static tree
21517 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21519 tree default_argument = NULL_TREE;
21520 bool saved_greater_than_is_operator_p;
21521 bool saved_local_variables_forbidden_p;
21522 bool non_constant_p, is_direct_init;
21524 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21525 set correctly. */
21526 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21527 parser->greater_than_is_operator_p = !template_parm_p;
21528 /* Local variable names (and the `this' keyword) may not
21529 appear in a default argument. */
21530 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21531 parser->local_variables_forbidden_p = true;
21532 /* Parse the assignment-expression. */
21533 if (template_parm_p)
21534 push_deferring_access_checks (dk_no_deferred);
21535 tree saved_class_ptr = NULL_TREE;
21536 tree saved_class_ref = NULL_TREE;
21537 /* The "this" pointer is not valid in a default argument. */
21538 if (cfun)
21540 saved_class_ptr = current_class_ptr;
21541 cp_function_chain->x_current_class_ptr = NULL_TREE;
21542 saved_class_ref = current_class_ref;
21543 cp_function_chain->x_current_class_ref = NULL_TREE;
21545 default_argument
21546 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21547 /* Restore the "this" pointer. */
21548 if (cfun)
21550 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21551 cp_function_chain->x_current_class_ref = saved_class_ref;
21553 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21554 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21555 if (template_parm_p)
21556 pop_deferring_access_checks ();
21557 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21558 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21560 return default_argument;
21563 /* Parse a function-body.
21565 function-body:
21566 compound_statement */
21568 static void
21569 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21571 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21572 ? BCS_TRY_BLOCK : BCS_NORMAL),
21573 true);
21576 /* Parse a ctor-initializer-opt followed by a function-body. Return
21577 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21578 is true we are parsing a function-try-block. */
21580 static bool
21581 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21582 bool in_function_try_block)
21584 tree body, list;
21585 bool ctor_initializer_p;
21586 const bool check_body_p =
21587 DECL_CONSTRUCTOR_P (current_function_decl)
21588 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21589 tree last = NULL;
21591 /* Begin the function body. */
21592 body = begin_function_body ();
21593 /* Parse the optional ctor-initializer. */
21594 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21596 /* If we're parsing a constexpr constructor definition, we need
21597 to check that the constructor body is indeed empty. However,
21598 before we get to cp_parser_function_body lot of junk has been
21599 generated, so we can't just check that we have an empty block.
21600 Rather we take a snapshot of the outermost block, and check whether
21601 cp_parser_function_body changed its state. */
21602 if (check_body_p)
21604 list = cur_stmt_list;
21605 if (STATEMENT_LIST_TAIL (list))
21606 last = STATEMENT_LIST_TAIL (list)->stmt;
21608 /* Parse the function-body. */
21609 cp_parser_function_body (parser, in_function_try_block);
21610 if (check_body_p)
21611 check_constexpr_ctor_body (last, list, /*complain=*/true);
21612 /* Finish the function body. */
21613 finish_function_body (body);
21615 return ctor_initializer_p;
21618 /* Parse an initializer.
21620 initializer:
21621 = initializer-clause
21622 ( expression-list )
21624 Returns an expression representing the initializer. If no
21625 initializer is present, NULL_TREE is returned.
21627 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21628 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21629 set to TRUE if there is no initializer present. If there is an
21630 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21631 is set to true; otherwise it is set to false. */
21633 static tree
21634 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21635 bool* non_constant_p)
21637 cp_token *token;
21638 tree init;
21640 /* Peek at the next token. */
21641 token = cp_lexer_peek_token (parser->lexer);
21643 /* Let our caller know whether or not this initializer was
21644 parenthesized. */
21645 *is_direct_init = (token->type != CPP_EQ);
21646 /* Assume that the initializer is constant. */
21647 *non_constant_p = false;
21649 if (token->type == CPP_EQ)
21651 /* Consume the `='. */
21652 cp_lexer_consume_token (parser->lexer);
21653 /* Parse the initializer-clause. */
21654 init = cp_parser_initializer_clause (parser, non_constant_p);
21656 else if (token->type == CPP_OPEN_PAREN)
21658 vec<tree, va_gc> *vec;
21659 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21660 /*cast_p=*/false,
21661 /*allow_expansion_p=*/true,
21662 non_constant_p);
21663 if (vec == NULL)
21664 return error_mark_node;
21665 init = build_tree_list_vec (vec);
21666 release_tree_vector (vec);
21668 else if (token->type == CPP_OPEN_BRACE)
21670 cp_lexer_set_source_position (parser->lexer);
21671 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21672 init = cp_parser_braced_list (parser, non_constant_p);
21673 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21675 else
21677 /* Anything else is an error. */
21678 cp_parser_error (parser, "expected initializer");
21679 init = error_mark_node;
21682 if (check_for_bare_parameter_packs (init))
21683 init = error_mark_node;
21685 return init;
21688 /* Parse an initializer-clause.
21690 initializer-clause:
21691 assignment-expression
21692 braced-init-list
21694 Returns an expression representing the initializer.
21696 If the `assignment-expression' production is used the value
21697 returned is simply a representation for the expression.
21699 Otherwise, calls cp_parser_braced_list. */
21701 static cp_expr
21702 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21704 cp_expr initializer;
21706 /* Assume the expression is constant. */
21707 *non_constant_p = false;
21709 /* If it is not a `{', then we are looking at an
21710 assignment-expression. */
21711 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21713 initializer
21714 = cp_parser_constant_expression (parser,
21715 /*allow_non_constant_p=*/true,
21716 non_constant_p);
21718 else
21719 initializer = cp_parser_braced_list (parser, non_constant_p);
21721 return initializer;
21724 /* Parse a brace-enclosed initializer list.
21726 braced-init-list:
21727 { initializer-list , [opt] }
21730 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21731 the elements of the initializer-list (or NULL, if the last
21732 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21733 NULL_TREE. There is no way to detect whether or not the optional
21734 trailing `,' was provided. NON_CONSTANT_P is as for
21735 cp_parser_initializer. */
21737 static cp_expr
21738 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21740 tree initializer;
21741 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21743 /* Consume the `{' token. */
21744 cp_lexer_consume_token (parser->lexer);
21745 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21746 initializer = make_node (CONSTRUCTOR);
21747 /* If it's not a `}', then there is a non-trivial initializer. */
21748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21750 /* Parse the initializer list. */
21751 CONSTRUCTOR_ELTS (initializer)
21752 = cp_parser_initializer_list (parser, non_constant_p);
21753 /* A trailing `,' token is allowed. */
21754 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21755 cp_lexer_consume_token (parser->lexer);
21757 else
21758 *non_constant_p = false;
21759 /* Now, there should be a trailing `}'. */
21760 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21761 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21762 TREE_TYPE (initializer) = init_list_type_node;
21764 cp_expr result (initializer);
21765 /* Build a location of the form:
21766 { ... }
21767 ^~~~~~~
21768 with caret==start at the open brace, finish at the close brace. */
21769 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21770 result.set_location (combined_loc);
21771 return result;
21774 /* Consume tokens up to, and including, the next non-nested closing `]'.
21775 Returns true iff we found a closing `]'. */
21777 static bool
21778 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21780 unsigned square_depth = 0;
21782 while (true)
21784 cp_token * token = cp_lexer_peek_token (parser->lexer);
21786 switch (token->type)
21788 case CPP_EOF:
21789 case CPP_PRAGMA_EOL:
21790 /* If we've run out of tokens, then there is no closing `]'. */
21791 return false;
21793 case CPP_OPEN_SQUARE:
21794 ++square_depth;
21795 break;
21797 case CPP_CLOSE_SQUARE:
21798 if (!square_depth--)
21800 cp_lexer_consume_token (parser->lexer);
21801 return true;
21803 break;
21805 default:
21806 break;
21809 /* Consume the token. */
21810 cp_lexer_consume_token (parser->lexer);
21814 /* Return true if we are looking at an array-designator, false otherwise. */
21816 static bool
21817 cp_parser_array_designator_p (cp_parser *parser)
21819 /* Consume the `['. */
21820 cp_lexer_consume_token (parser->lexer);
21822 cp_lexer_save_tokens (parser->lexer);
21824 /* Skip tokens until the next token is a closing square bracket.
21825 If we find the closing `]', and the next token is a `=', then
21826 we are looking at an array designator. */
21827 bool array_designator_p
21828 = (cp_parser_skip_to_closing_square_bracket (parser)
21829 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21831 /* Roll back the tokens we skipped. */
21832 cp_lexer_rollback_tokens (parser->lexer);
21834 return array_designator_p;
21837 /* Parse an initializer-list.
21839 initializer-list:
21840 initializer-clause ... [opt]
21841 initializer-list , initializer-clause ... [opt]
21843 GNU Extension:
21845 initializer-list:
21846 designation initializer-clause ...[opt]
21847 initializer-list , designation initializer-clause ...[opt]
21849 designation:
21850 . identifier =
21851 identifier :
21852 [ constant-expression ] =
21854 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21855 for the initializer. If the INDEX of the elt is non-NULL, it is the
21856 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21857 as for cp_parser_initializer. */
21859 static vec<constructor_elt, va_gc> *
21860 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21862 vec<constructor_elt, va_gc> *v = NULL;
21864 /* Assume all of the expressions are constant. */
21865 *non_constant_p = false;
21867 /* Parse the rest of the list. */
21868 while (true)
21870 cp_token *token;
21871 tree designator;
21872 tree initializer;
21873 bool clause_non_constant_p;
21875 /* If the next token is an identifier and the following one is a
21876 colon, we are looking at the GNU designated-initializer
21877 syntax. */
21878 if (cp_parser_allow_gnu_extensions_p (parser)
21879 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21880 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21882 /* Warn the user that they are using an extension. */
21883 pedwarn (input_location, OPT_Wpedantic,
21884 "ISO C++ does not allow designated initializers");
21885 /* Consume the identifier. */
21886 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21887 /* Consume the `:'. */
21888 cp_lexer_consume_token (parser->lexer);
21890 /* Also handle the C99 syntax, '. id ='. */
21891 else if (cp_parser_allow_gnu_extensions_p (parser)
21892 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21893 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21894 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21896 /* Warn the user that they are using an extension. */
21897 pedwarn (input_location, OPT_Wpedantic,
21898 "ISO C++ does not allow C99 designated initializers");
21899 /* Consume the `.'. */
21900 cp_lexer_consume_token (parser->lexer);
21901 /* Consume the identifier. */
21902 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21903 /* Consume the `='. */
21904 cp_lexer_consume_token (parser->lexer);
21906 /* Also handle C99 array designators, '[ const ] ='. */
21907 else if (cp_parser_allow_gnu_extensions_p (parser)
21908 && !c_dialect_objc ()
21909 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21911 /* In C++11, [ could start a lambda-introducer. */
21912 bool non_const = false;
21914 cp_parser_parse_tentatively (parser);
21916 if (!cp_parser_array_designator_p (parser))
21918 cp_parser_simulate_error (parser);
21919 designator = NULL_TREE;
21921 else
21923 designator = cp_parser_constant_expression (parser, true,
21924 &non_const);
21925 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21926 cp_parser_require (parser, CPP_EQ, RT_EQ);
21929 if (!cp_parser_parse_definitely (parser))
21930 designator = NULL_TREE;
21931 else if (non_const)
21932 require_potential_rvalue_constant_expression (designator);
21934 else
21935 designator = NULL_TREE;
21937 /* Parse the initializer. */
21938 initializer = cp_parser_initializer_clause (parser,
21939 &clause_non_constant_p);
21940 /* If any clause is non-constant, so is the entire initializer. */
21941 if (clause_non_constant_p)
21942 *non_constant_p = true;
21944 /* If we have an ellipsis, this is an initializer pack
21945 expansion. */
21946 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21948 /* Consume the `...'. */
21949 cp_lexer_consume_token (parser->lexer);
21951 /* Turn the initializer into an initializer expansion. */
21952 initializer = make_pack_expansion (initializer);
21955 /* Add it to the vector. */
21956 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21958 /* If the next token is not a comma, we have reached the end of
21959 the list. */
21960 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21961 break;
21963 /* Peek at the next token. */
21964 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21965 /* If the next token is a `}', then we're still done. An
21966 initializer-clause can have a trailing `,' after the
21967 initializer-list and before the closing `}'. */
21968 if (token->type == CPP_CLOSE_BRACE)
21969 break;
21971 /* Consume the `,' token. */
21972 cp_lexer_consume_token (parser->lexer);
21975 return v;
21978 /* Classes [gram.class] */
21980 /* Parse a class-name.
21982 class-name:
21983 identifier
21984 template-id
21986 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
21987 to indicate that names looked up in dependent types should be
21988 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
21989 keyword has been used to indicate that the name that appears next
21990 is a template. TAG_TYPE indicates the explicit tag given before
21991 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
21992 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
21993 is the class being defined in a class-head. If ENUM_OK is TRUE,
21994 enum-names are also accepted.
21996 Returns the TYPE_DECL representing the class. */
21998 static tree
21999 cp_parser_class_name (cp_parser *parser,
22000 bool typename_keyword_p,
22001 bool template_keyword_p,
22002 enum tag_types tag_type,
22003 bool check_dependency_p,
22004 bool class_head_p,
22005 bool is_declaration,
22006 bool enum_ok)
22008 tree decl;
22009 tree scope;
22010 bool typename_p;
22011 cp_token *token;
22012 tree identifier = NULL_TREE;
22014 /* All class-names start with an identifier. */
22015 token = cp_lexer_peek_token (parser->lexer);
22016 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22018 cp_parser_error (parser, "expected class-name");
22019 return error_mark_node;
22022 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22023 to a template-id, so we save it here. */
22024 scope = parser->scope;
22025 if (scope == error_mark_node)
22026 return error_mark_node;
22028 /* Any name names a type if we're following the `typename' keyword
22029 in a qualified name where the enclosing scope is type-dependent. */
22030 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22031 && dependent_type_p (scope));
22032 /* Handle the common case (an identifier, but not a template-id)
22033 efficiently. */
22034 if (token->type == CPP_NAME
22035 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22037 cp_token *identifier_token;
22038 bool ambiguous_p;
22040 /* Look for the identifier. */
22041 identifier_token = cp_lexer_peek_token (parser->lexer);
22042 ambiguous_p = identifier_token->error_reported;
22043 identifier = cp_parser_identifier (parser);
22044 /* If the next token isn't an identifier, we are certainly not
22045 looking at a class-name. */
22046 if (identifier == error_mark_node)
22047 decl = error_mark_node;
22048 /* If we know this is a type-name, there's no need to look it
22049 up. */
22050 else if (typename_p)
22051 decl = identifier;
22052 else
22054 tree ambiguous_decls;
22055 /* If we already know that this lookup is ambiguous, then
22056 we've already issued an error message; there's no reason
22057 to check again. */
22058 if (ambiguous_p)
22060 cp_parser_simulate_error (parser);
22061 return error_mark_node;
22063 /* If the next token is a `::', then the name must be a type
22064 name.
22066 [basic.lookup.qual]
22068 During the lookup for a name preceding the :: scope
22069 resolution operator, object, function, and enumerator
22070 names are ignored. */
22071 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22072 tag_type = scope_type;
22073 /* Look up the name. */
22074 decl = cp_parser_lookup_name (parser, identifier,
22075 tag_type,
22076 /*is_template=*/false,
22077 /*is_namespace=*/false,
22078 check_dependency_p,
22079 &ambiguous_decls,
22080 identifier_token->location);
22081 if (ambiguous_decls)
22083 if (cp_parser_parsing_tentatively (parser))
22084 cp_parser_simulate_error (parser);
22085 return error_mark_node;
22089 else
22091 /* Try a template-id. */
22092 decl = cp_parser_template_id (parser, template_keyword_p,
22093 check_dependency_p,
22094 tag_type,
22095 is_declaration);
22096 if (decl == error_mark_node)
22097 return error_mark_node;
22100 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22102 /* If this is a typename, create a TYPENAME_TYPE. */
22103 if (typename_p && decl != error_mark_node)
22105 decl = make_typename_type (scope, decl, typename_type,
22106 /*complain=*/tf_error);
22107 if (decl != error_mark_node)
22108 decl = TYPE_NAME (decl);
22111 decl = strip_using_decl (decl);
22113 /* Check to see that it is really the name of a class. */
22114 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22115 && identifier_p (TREE_OPERAND (decl, 0))
22116 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22117 /* Situations like this:
22119 template <typename T> struct A {
22120 typename T::template X<int>::I i;
22123 are problematic. Is `T::template X<int>' a class-name? The
22124 standard does not seem to be definitive, but there is no other
22125 valid interpretation of the following `::'. Therefore, those
22126 names are considered class-names. */
22128 decl = make_typename_type (scope, decl, tag_type, tf_error);
22129 if (decl != error_mark_node)
22130 decl = TYPE_NAME (decl);
22132 else if (TREE_CODE (decl) != TYPE_DECL
22133 || TREE_TYPE (decl) == error_mark_node
22134 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22135 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22136 /* In Objective-C 2.0, a classname followed by '.' starts a
22137 dot-syntax expression, and it's not a type-name. */
22138 || (c_dialect_objc ()
22139 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22140 && objc_is_class_name (decl)))
22141 decl = error_mark_node;
22143 if (decl == error_mark_node)
22144 cp_parser_error (parser, "expected class-name");
22145 else if (identifier && !parser->scope)
22146 maybe_note_name_used_in_class (identifier, decl);
22148 return decl;
22151 /* Parse a class-specifier.
22153 class-specifier:
22154 class-head { member-specification [opt] }
22156 Returns the TREE_TYPE representing the class. */
22158 static tree
22159 cp_parser_class_specifier_1 (cp_parser* parser)
22161 tree type;
22162 tree attributes = NULL_TREE;
22163 bool nested_name_specifier_p;
22164 unsigned saved_num_template_parameter_lists;
22165 bool saved_in_function_body;
22166 unsigned char in_statement;
22167 bool in_switch_statement_p;
22168 bool saved_in_unbraced_linkage_specification_p;
22169 tree old_scope = NULL_TREE;
22170 tree scope = NULL_TREE;
22171 cp_token *closing_brace;
22173 push_deferring_access_checks (dk_no_deferred);
22175 /* Parse the class-head. */
22176 type = cp_parser_class_head (parser,
22177 &nested_name_specifier_p);
22178 /* If the class-head was a semantic disaster, skip the entire body
22179 of the class. */
22180 if (!type)
22182 cp_parser_skip_to_end_of_block_or_statement (parser);
22183 pop_deferring_access_checks ();
22184 return error_mark_node;
22187 /* Look for the `{'. */
22188 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22190 pop_deferring_access_checks ();
22191 return error_mark_node;
22194 cp_ensure_no_omp_declare_simd (parser);
22195 cp_ensure_no_oacc_routine (parser);
22197 /* Issue an error message if type-definitions are forbidden here. */
22198 cp_parser_check_type_definition (parser);
22199 /* Remember that we are defining one more class. */
22200 ++parser->num_classes_being_defined;
22201 /* Inside the class, surrounding template-parameter-lists do not
22202 apply. */
22203 saved_num_template_parameter_lists
22204 = parser->num_template_parameter_lists;
22205 parser->num_template_parameter_lists = 0;
22206 /* We are not in a function body. */
22207 saved_in_function_body = parser->in_function_body;
22208 parser->in_function_body = false;
22209 /* Or in a loop. */
22210 in_statement = parser->in_statement;
22211 parser->in_statement = 0;
22212 /* Or in a switch. */
22213 in_switch_statement_p = parser->in_switch_statement_p;
22214 parser->in_switch_statement_p = false;
22215 /* We are not immediately inside an extern "lang" block. */
22216 saved_in_unbraced_linkage_specification_p
22217 = parser->in_unbraced_linkage_specification_p;
22218 parser->in_unbraced_linkage_specification_p = false;
22220 // Associate constraints with the type.
22221 if (flag_concepts)
22222 type = associate_classtype_constraints (type);
22224 /* Start the class. */
22225 if (nested_name_specifier_p)
22227 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22228 old_scope = push_inner_scope (scope);
22230 type = begin_class_definition (type);
22232 if (type == error_mark_node)
22233 /* If the type is erroneous, skip the entire body of the class. */
22234 cp_parser_skip_to_closing_brace (parser);
22235 else
22236 /* Parse the member-specification. */
22237 cp_parser_member_specification_opt (parser);
22239 /* Look for the trailing `}'. */
22240 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22241 /* Look for trailing attributes to apply to this class. */
22242 if (cp_parser_allow_gnu_extensions_p (parser))
22243 attributes = cp_parser_gnu_attributes_opt (parser);
22244 if (type != error_mark_node)
22245 type = finish_struct (type, attributes);
22246 if (nested_name_specifier_p)
22247 pop_inner_scope (old_scope, scope);
22249 /* We've finished a type definition. Check for the common syntax
22250 error of forgetting a semicolon after the definition. We need to
22251 be careful, as we can't just check for not-a-semicolon and be done
22252 with it; the user might have typed:
22254 class X { } c = ...;
22255 class X { } *p = ...;
22257 and so forth. Instead, enumerate all the possible tokens that
22258 might follow this production; if we don't see one of them, then
22259 complain and silently insert the semicolon. */
22261 cp_token *token = cp_lexer_peek_token (parser->lexer);
22262 bool want_semicolon = true;
22264 if (cp_next_tokens_can_be_std_attribute_p (parser))
22265 /* Don't try to parse c++11 attributes here. As per the
22266 grammar, that should be a task for
22267 cp_parser_decl_specifier_seq. */
22268 want_semicolon = false;
22270 switch (token->type)
22272 case CPP_NAME:
22273 case CPP_SEMICOLON:
22274 case CPP_MULT:
22275 case CPP_AND:
22276 case CPP_OPEN_PAREN:
22277 case CPP_CLOSE_PAREN:
22278 case CPP_COMMA:
22279 want_semicolon = false;
22280 break;
22282 /* While it's legal for type qualifiers and storage class
22283 specifiers to follow type definitions in the grammar, only
22284 compiler testsuites contain code like that. Assume that if
22285 we see such code, then what we're really seeing is a case
22286 like:
22288 class X { }
22289 const <type> var = ...;
22293 class Y { }
22294 static <type> func (...) ...
22296 i.e. the qualifier or specifier applies to the next
22297 declaration. To do so, however, we need to look ahead one
22298 more token to see if *that* token is a type specifier.
22300 This code could be improved to handle:
22302 class Z { }
22303 static const <type> var = ...; */
22304 case CPP_KEYWORD:
22305 if (keyword_is_decl_specifier (token->keyword))
22307 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22309 /* Handling user-defined types here would be nice, but very
22310 tricky. */
22311 want_semicolon
22312 = (lookahead->type == CPP_KEYWORD
22313 && keyword_begins_type_specifier (lookahead->keyword));
22315 break;
22316 default:
22317 break;
22320 /* If we don't have a type, then something is very wrong and we
22321 shouldn't try to do anything clever. Likewise for not seeing the
22322 closing brace. */
22323 if (closing_brace && TYPE_P (type) && want_semicolon)
22325 /* Locate the closing brace. */
22326 cp_token_position prev
22327 = cp_lexer_previous_token_position (parser->lexer);
22328 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22329 location_t loc = prev_token->location;
22331 /* We want to suggest insertion of a ';' immediately *after* the
22332 closing brace, so, if we can, offset the location by 1 column. */
22333 location_t next_loc = loc;
22334 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22335 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22337 rich_location richloc (line_table, next_loc);
22339 /* If we successfully offset the location, suggest the fix-it. */
22340 if (next_loc != loc)
22341 richloc.add_fixit_insert_before (next_loc, ";");
22343 if (CLASSTYPE_DECLARED_CLASS (type))
22344 error_at_rich_loc (&richloc,
22345 "expected %<;%> after class definition");
22346 else if (TREE_CODE (type) == RECORD_TYPE)
22347 error_at_rich_loc (&richloc,
22348 "expected %<;%> after struct definition");
22349 else if (TREE_CODE (type) == UNION_TYPE)
22350 error_at_rich_loc (&richloc,
22351 "expected %<;%> after union definition");
22352 else
22353 gcc_unreachable ();
22355 /* Unget one token and smash it to look as though we encountered
22356 a semicolon in the input stream. */
22357 cp_lexer_set_token_position (parser->lexer, prev);
22358 token = cp_lexer_peek_token (parser->lexer);
22359 token->type = CPP_SEMICOLON;
22360 token->keyword = RID_MAX;
22364 /* If this class is not itself within the scope of another class,
22365 then we need to parse the bodies of all of the queued function
22366 definitions. Note that the queued functions defined in a class
22367 are not always processed immediately following the
22368 class-specifier for that class. Consider:
22370 struct A {
22371 struct B { void f() { sizeof (A); } };
22374 If `f' were processed before the processing of `A' were
22375 completed, there would be no way to compute the size of `A'.
22376 Note that the nesting we are interested in here is lexical --
22377 not the semantic nesting given by TYPE_CONTEXT. In particular,
22378 for:
22380 struct A { struct B; };
22381 struct A::B { void f() { } };
22383 there is no need to delay the parsing of `A::B::f'. */
22384 if (--parser->num_classes_being_defined == 0)
22386 tree decl;
22387 tree class_type = NULL_TREE;
22388 tree pushed_scope = NULL_TREE;
22389 unsigned ix;
22390 cp_default_arg_entry *e;
22391 tree save_ccp, save_ccr;
22393 /* In a first pass, parse default arguments to the functions.
22394 Then, in a second pass, parse the bodies of the functions.
22395 This two-phased approach handles cases like:
22397 struct S {
22398 void f() { g(); }
22399 void g(int i = 3);
22403 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22405 decl = e->decl;
22406 /* If there are default arguments that have not yet been processed,
22407 take care of them now. */
22408 if (class_type != e->class_type)
22410 if (pushed_scope)
22411 pop_scope (pushed_scope);
22412 class_type = e->class_type;
22413 pushed_scope = push_scope (class_type);
22415 /* Make sure that any template parameters are in scope. */
22416 maybe_begin_member_template_processing (decl);
22417 /* Parse the default argument expressions. */
22418 cp_parser_late_parsing_default_args (parser, decl);
22419 /* Remove any template parameters from the symbol table. */
22420 maybe_end_member_template_processing ();
22422 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22423 /* Now parse any NSDMIs. */
22424 save_ccp = current_class_ptr;
22425 save_ccr = current_class_ref;
22426 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22428 if (class_type != DECL_CONTEXT (decl))
22430 if (pushed_scope)
22431 pop_scope (pushed_scope);
22432 class_type = DECL_CONTEXT (decl);
22433 pushed_scope = push_scope (class_type);
22435 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22436 cp_parser_late_parsing_nsdmi (parser, decl);
22438 vec_safe_truncate (unparsed_nsdmis, 0);
22439 current_class_ptr = save_ccp;
22440 current_class_ref = save_ccr;
22441 if (pushed_scope)
22442 pop_scope (pushed_scope);
22444 /* Now do some post-NSDMI bookkeeping. */
22445 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22446 after_nsdmi_defaulted_late_checks (class_type);
22447 vec_safe_truncate (unparsed_classes, 0);
22448 after_nsdmi_defaulted_late_checks (type);
22450 /* Now parse the body of the functions. */
22451 if (flag_openmp)
22453 /* OpenMP UDRs need to be parsed before all other functions. */
22454 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22455 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22456 cp_parser_late_parsing_for_member (parser, decl);
22457 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22458 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22459 cp_parser_late_parsing_for_member (parser, decl);
22461 else
22462 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22463 cp_parser_late_parsing_for_member (parser, decl);
22464 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22466 else
22467 vec_safe_push (unparsed_classes, type);
22469 /* Put back any saved access checks. */
22470 pop_deferring_access_checks ();
22472 /* Restore saved state. */
22473 parser->in_switch_statement_p = in_switch_statement_p;
22474 parser->in_statement = in_statement;
22475 parser->in_function_body = saved_in_function_body;
22476 parser->num_template_parameter_lists
22477 = saved_num_template_parameter_lists;
22478 parser->in_unbraced_linkage_specification_p
22479 = saved_in_unbraced_linkage_specification_p;
22481 return type;
22484 static tree
22485 cp_parser_class_specifier (cp_parser* parser)
22487 tree ret;
22488 timevar_push (TV_PARSE_STRUCT);
22489 ret = cp_parser_class_specifier_1 (parser);
22490 timevar_pop (TV_PARSE_STRUCT);
22491 return ret;
22494 /* Parse a class-head.
22496 class-head:
22497 class-key identifier [opt] base-clause [opt]
22498 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22499 class-key nested-name-specifier [opt] template-id
22500 base-clause [opt]
22502 class-virt-specifier:
22503 final
22505 GNU Extensions:
22506 class-key attributes identifier [opt] base-clause [opt]
22507 class-key attributes nested-name-specifier identifier base-clause [opt]
22508 class-key attributes nested-name-specifier [opt] template-id
22509 base-clause [opt]
22511 Upon return BASES is initialized to the list of base classes (or
22512 NULL, if there are none) in the same form returned by
22513 cp_parser_base_clause.
22515 Returns the TYPE of the indicated class. Sets
22516 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22517 involving a nested-name-specifier was used, and FALSE otherwise.
22519 Returns error_mark_node if this is not a class-head.
22521 Returns NULL_TREE if the class-head is syntactically valid, but
22522 semantically invalid in a way that means we should skip the entire
22523 body of the class. */
22525 static tree
22526 cp_parser_class_head (cp_parser* parser,
22527 bool* nested_name_specifier_p)
22529 tree nested_name_specifier;
22530 enum tag_types class_key;
22531 tree id = NULL_TREE;
22532 tree type = NULL_TREE;
22533 tree attributes;
22534 tree bases;
22535 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22536 bool template_id_p = false;
22537 bool qualified_p = false;
22538 bool invalid_nested_name_p = false;
22539 bool invalid_explicit_specialization_p = false;
22540 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22541 tree pushed_scope = NULL_TREE;
22542 unsigned num_templates;
22543 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22544 /* Assume no nested-name-specifier will be present. */
22545 *nested_name_specifier_p = false;
22546 /* Assume no template parameter lists will be used in defining the
22547 type. */
22548 num_templates = 0;
22549 parser->colon_corrects_to_scope_p = false;
22551 /* Look for the class-key. */
22552 class_key = cp_parser_class_key (parser);
22553 if (class_key == none_type)
22554 return error_mark_node;
22556 location_t class_head_start_location = input_location;
22558 /* Parse the attributes. */
22559 attributes = cp_parser_attributes_opt (parser);
22561 /* If the next token is `::', that is invalid -- but sometimes
22562 people do try to write:
22564 struct ::S {};
22566 Handle this gracefully by accepting the extra qualifier, and then
22567 issuing an error about it later if this really is a
22568 class-head. If it turns out just to be an elaborated type
22569 specifier, remain silent. */
22570 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22571 qualified_p = true;
22573 push_deferring_access_checks (dk_no_check);
22575 /* Determine the name of the class. Begin by looking for an
22576 optional nested-name-specifier. */
22577 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22578 nested_name_specifier
22579 = cp_parser_nested_name_specifier_opt (parser,
22580 /*typename_keyword_p=*/false,
22581 /*check_dependency_p=*/false,
22582 /*type_p=*/true,
22583 /*is_declaration=*/false);
22584 /* If there was a nested-name-specifier, then there *must* be an
22585 identifier. */
22586 if (nested_name_specifier)
22588 type_start_token = cp_lexer_peek_token (parser->lexer);
22589 /* Although the grammar says `identifier', it really means
22590 `class-name' or `template-name'. You are only allowed to
22591 define a class that has already been declared with this
22592 syntax.
22594 The proposed resolution for Core Issue 180 says that wherever
22595 you see `class T::X' you should treat `X' as a type-name.
22597 It is OK to define an inaccessible class; for example:
22599 class A { class B; };
22600 class A::B {};
22602 We do not know if we will see a class-name, or a
22603 template-name. We look for a class-name first, in case the
22604 class-name is a template-id; if we looked for the
22605 template-name first we would stop after the template-name. */
22606 cp_parser_parse_tentatively (parser);
22607 type = cp_parser_class_name (parser,
22608 /*typename_keyword_p=*/false,
22609 /*template_keyword_p=*/false,
22610 class_type,
22611 /*check_dependency_p=*/false,
22612 /*class_head_p=*/true,
22613 /*is_declaration=*/false);
22614 /* If that didn't work, ignore the nested-name-specifier. */
22615 if (!cp_parser_parse_definitely (parser))
22617 invalid_nested_name_p = true;
22618 type_start_token = cp_lexer_peek_token (parser->lexer);
22619 id = cp_parser_identifier (parser);
22620 if (id == error_mark_node)
22621 id = NULL_TREE;
22623 /* If we could not find a corresponding TYPE, treat this
22624 declaration like an unqualified declaration. */
22625 if (type == error_mark_node)
22626 nested_name_specifier = NULL_TREE;
22627 /* Otherwise, count the number of templates used in TYPE and its
22628 containing scopes. */
22629 else
22631 tree scope;
22633 for (scope = TREE_TYPE (type);
22634 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22635 scope = get_containing_scope (scope))
22636 if (TYPE_P (scope)
22637 && CLASS_TYPE_P (scope)
22638 && CLASSTYPE_TEMPLATE_INFO (scope)
22639 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22640 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22641 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22642 ++num_templates;
22645 /* Otherwise, the identifier is optional. */
22646 else
22648 /* We don't know whether what comes next is a template-id,
22649 an identifier, or nothing at all. */
22650 cp_parser_parse_tentatively (parser);
22651 /* Check for a template-id. */
22652 type_start_token = cp_lexer_peek_token (parser->lexer);
22653 id = cp_parser_template_id (parser,
22654 /*template_keyword_p=*/false,
22655 /*check_dependency_p=*/true,
22656 class_key,
22657 /*is_declaration=*/true);
22658 /* If that didn't work, it could still be an identifier. */
22659 if (!cp_parser_parse_definitely (parser))
22661 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22663 type_start_token = cp_lexer_peek_token (parser->lexer);
22664 id = cp_parser_identifier (parser);
22666 else
22667 id = NULL_TREE;
22669 else
22671 template_id_p = true;
22672 ++num_templates;
22676 pop_deferring_access_checks ();
22678 if (id)
22680 cp_parser_check_for_invalid_template_id (parser, id,
22681 class_key,
22682 type_start_token->location);
22684 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22686 /* If it's not a `:' or a `{' then we can't really be looking at a
22687 class-head, since a class-head only appears as part of a
22688 class-specifier. We have to detect this situation before calling
22689 xref_tag, since that has irreversible side-effects. */
22690 if (!cp_parser_next_token_starts_class_definition_p (parser))
22692 cp_parser_error (parser, "expected %<{%> or %<:%>");
22693 type = error_mark_node;
22694 goto out;
22697 /* At this point, we're going ahead with the class-specifier, even
22698 if some other problem occurs. */
22699 cp_parser_commit_to_tentative_parse (parser);
22700 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22702 cp_parser_error (parser,
22703 "cannot specify %<override%> for a class");
22704 type = error_mark_node;
22705 goto out;
22707 /* Issue the error about the overly-qualified name now. */
22708 if (qualified_p)
22710 cp_parser_error (parser,
22711 "global qualification of class name is invalid");
22712 type = error_mark_node;
22713 goto out;
22715 else if (invalid_nested_name_p)
22717 cp_parser_error (parser,
22718 "qualified name does not name a class");
22719 type = error_mark_node;
22720 goto out;
22722 else if (nested_name_specifier)
22724 tree scope;
22726 /* Reject typedef-names in class heads. */
22727 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22729 error_at (type_start_token->location,
22730 "invalid class name in declaration of %qD",
22731 type);
22732 type = NULL_TREE;
22733 goto done;
22736 /* Figure out in what scope the declaration is being placed. */
22737 scope = current_scope ();
22738 /* If that scope does not contain the scope in which the
22739 class was originally declared, the program is invalid. */
22740 if (scope && !is_ancestor (scope, nested_name_specifier))
22742 if (at_namespace_scope_p ())
22743 error_at (type_start_token->location,
22744 "declaration of %qD in namespace %qD which does not "
22745 "enclose %qD",
22746 type, scope, nested_name_specifier);
22747 else
22748 error_at (type_start_token->location,
22749 "declaration of %qD in %qD which does not enclose %qD",
22750 type, scope, nested_name_specifier);
22751 type = NULL_TREE;
22752 goto done;
22754 /* [dcl.meaning]
22756 A declarator-id shall not be qualified except for the
22757 definition of a ... nested class outside of its class
22758 ... [or] the definition or explicit instantiation of a
22759 class member of a namespace outside of its namespace. */
22760 if (scope == nested_name_specifier)
22762 permerror (nested_name_specifier_token_start->location,
22763 "extra qualification not allowed");
22764 nested_name_specifier = NULL_TREE;
22765 num_templates = 0;
22768 /* An explicit-specialization must be preceded by "template <>". If
22769 it is not, try to recover gracefully. */
22770 if (at_namespace_scope_p ()
22771 && parser->num_template_parameter_lists == 0
22772 && !processing_template_parmlist
22773 && template_id_p)
22775 /* Build a location of this form:
22776 struct typename <ARGS>
22777 ^~~~~~~~~~~~~~~~~~~~~~
22778 with caret==start at the start token, and
22779 finishing at the end of the type. */
22780 location_t reported_loc
22781 = make_location (class_head_start_location,
22782 class_head_start_location,
22783 get_finish (type_start_token->location));
22784 rich_location richloc (line_table, reported_loc);
22785 richloc.add_fixit_insert_before (class_head_start_location,
22786 "template <> ");
22787 error_at_rich_loc
22788 (&richloc,
22789 "an explicit specialization must be preceded by %<template <>%>");
22790 invalid_explicit_specialization_p = true;
22791 /* Take the same action that would have been taken by
22792 cp_parser_explicit_specialization. */
22793 ++parser->num_template_parameter_lists;
22794 begin_specialization ();
22796 /* There must be no "return" statements between this point and the
22797 end of this function; set "type "to the correct return value and
22798 use "goto done;" to return. */
22799 /* Make sure that the right number of template parameters were
22800 present. */
22801 if (!cp_parser_check_template_parameters (parser, num_templates,
22802 type_start_token->location,
22803 /*declarator=*/NULL))
22805 /* If something went wrong, there is no point in even trying to
22806 process the class-definition. */
22807 type = NULL_TREE;
22808 goto done;
22811 /* Look up the type. */
22812 if (template_id_p)
22814 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22815 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22816 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22818 error_at (type_start_token->location,
22819 "function template %qD redeclared as a class template", id);
22820 type = error_mark_node;
22822 else
22824 type = TREE_TYPE (id);
22825 type = maybe_process_partial_specialization (type);
22827 /* Check the scope while we still know whether or not we had a
22828 nested-name-specifier. */
22829 if (type != error_mark_node)
22830 check_unqualified_spec_or_inst (type, type_start_token->location);
22832 if (nested_name_specifier)
22833 pushed_scope = push_scope (nested_name_specifier);
22835 else if (nested_name_specifier)
22837 tree class_type;
22839 /* Given:
22841 template <typename T> struct S { struct T };
22842 template <typename T> struct S<T>::T { };
22844 we will get a TYPENAME_TYPE when processing the definition of
22845 `S::T'. We need to resolve it to the actual type before we
22846 try to define it. */
22847 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22849 class_type = resolve_typename_type (TREE_TYPE (type),
22850 /*only_current_p=*/false);
22851 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22852 type = TYPE_NAME (class_type);
22853 else
22855 cp_parser_error (parser, "could not resolve typename type");
22856 type = error_mark_node;
22860 if (maybe_process_partial_specialization (TREE_TYPE (type))
22861 == error_mark_node)
22863 type = NULL_TREE;
22864 goto done;
22867 class_type = current_class_type;
22868 /* Enter the scope indicated by the nested-name-specifier. */
22869 pushed_scope = push_scope (nested_name_specifier);
22870 /* Get the canonical version of this type. */
22871 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22872 /* Call push_template_decl if it seems like we should be defining a
22873 template either from the template headers or the type we're
22874 defining, so that we diagnose both extra and missing headers. */
22875 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22876 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22877 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22879 type = push_template_decl (type);
22880 if (type == error_mark_node)
22882 type = NULL_TREE;
22883 goto done;
22887 type = TREE_TYPE (type);
22888 *nested_name_specifier_p = true;
22890 else /* The name is not a nested name. */
22892 /* If the class was unnamed, create a dummy name. */
22893 if (!id)
22894 id = make_anon_name ();
22895 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22896 ? ts_within_enclosing_non_class
22897 : ts_current);
22898 type = xref_tag (class_key, id, tag_scope,
22899 parser->num_template_parameter_lists);
22902 /* Indicate whether this class was declared as a `class' or as a
22903 `struct'. */
22904 if (TREE_CODE (type) == RECORD_TYPE)
22905 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22906 cp_parser_check_class_key (class_key, type);
22908 /* If this type was already complete, and we see another definition,
22909 that's an error. */
22910 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22912 error_at (type_start_token->location, "redefinition of %q#T",
22913 type);
22914 inform (location_of (type), "previous definition of %q#T",
22915 type);
22916 type = NULL_TREE;
22917 goto done;
22919 else if (type == error_mark_node)
22920 type = NULL_TREE;
22922 if (type)
22924 /* Apply attributes now, before any use of the class as a template
22925 argument in its base list. */
22926 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22927 fixup_attribute_variants (type);
22930 /* We will have entered the scope containing the class; the names of
22931 base classes should be looked up in that context. For example:
22933 struct A { struct B {}; struct C; };
22934 struct A::C : B {};
22936 is valid. */
22938 /* Get the list of base-classes, if there is one. */
22939 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22941 /* PR59482: enter the class scope so that base-specifiers are looked
22942 up correctly. */
22943 if (type)
22944 pushclass (type);
22945 bases = cp_parser_base_clause (parser);
22946 /* PR59482: get out of the previously pushed class scope so that the
22947 subsequent pops pop the right thing. */
22948 if (type)
22949 popclass ();
22951 else
22952 bases = NULL_TREE;
22954 /* If we're really defining a class, process the base classes.
22955 If they're invalid, fail. */
22956 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22957 xref_basetypes (type, bases);
22959 done:
22960 /* Leave the scope given by the nested-name-specifier. We will
22961 enter the class scope itself while processing the members. */
22962 if (pushed_scope)
22963 pop_scope (pushed_scope);
22965 if (invalid_explicit_specialization_p)
22967 end_specialization ();
22968 --parser->num_template_parameter_lists;
22971 if (type)
22972 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
22973 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
22974 CLASSTYPE_FINAL (type) = 1;
22975 out:
22976 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
22977 return type;
22980 /* Parse a class-key.
22982 class-key:
22983 class
22984 struct
22985 union
22987 Returns the kind of class-key specified, or none_type to indicate
22988 error. */
22990 static enum tag_types
22991 cp_parser_class_key (cp_parser* parser)
22993 cp_token *token;
22994 enum tag_types tag_type;
22996 /* Look for the class-key. */
22997 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
22998 if (!token)
22999 return none_type;
23001 /* Check to see if the TOKEN is a class-key. */
23002 tag_type = cp_parser_token_is_class_key (token);
23003 if (!tag_type)
23004 cp_parser_error (parser, "expected class-key");
23005 return tag_type;
23008 /* Parse a type-parameter-key.
23010 type-parameter-key:
23011 class
23012 typename
23015 static void
23016 cp_parser_type_parameter_key (cp_parser* parser)
23018 /* Look for the type-parameter-key. */
23019 enum tag_types tag_type = none_type;
23020 cp_token *token = cp_lexer_peek_token (parser->lexer);
23021 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23023 cp_lexer_consume_token (parser->lexer);
23024 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
23025 /* typename is not allowed in a template template parameter
23026 by the standard until C++1Z. */
23027 pedwarn (token->location, OPT_Wpedantic,
23028 "ISO C++ forbids typename key in template template parameter;"
23029 " use -std=c++1z or -std=gnu++1z");
23031 else
23032 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23034 return;
23037 /* Parse an (optional) member-specification.
23039 member-specification:
23040 member-declaration member-specification [opt]
23041 access-specifier : member-specification [opt] */
23043 static void
23044 cp_parser_member_specification_opt (cp_parser* parser)
23046 while (true)
23048 cp_token *token;
23049 enum rid keyword;
23051 /* Peek at the next token. */
23052 token = cp_lexer_peek_token (parser->lexer);
23053 /* If it's a `}', or EOF then we've seen all the members. */
23054 if (token->type == CPP_CLOSE_BRACE
23055 || token->type == CPP_EOF
23056 || token->type == CPP_PRAGMA_EOL)
23057 break;
23059 /* See if this token is a keyword. */
23060 keyword = token->keyword;
23061 switch (keyword)
23063 case RID_PUBLIC:
23064 case RID_PROTECTED:
23065 case RID_PRIVATE:
23066 /* Consume the access-specifier. */
23067 cp_lexer_consume_token (parser->lexer);
23068 /* Remember which access-specifier is active. */
23069 current_access_specifier = token->u.value;
23070 /* Look for the `:'. */
23071 cp_parser_require (parser, CPP_COLON, RT_COLON);
23072 break;
23074 default:
23075 /* Accept #pragmas at class scope. */
23076 if (token->type == CPP_PRAGMA)
23078 cp_parser_pragma (parser, pragma_member, NULL);
23079 break;
23082 /* Otherwise, the next construction must be a
23083 member-declaration. */
23084 cp_parser_member_declaration (parser);
23089 /* Parse a member-declaration.
23091 member-declaration:
23092 decl-specifier-seq [opt] member-declarator-list [opt] ;
23093 function-definition ; [opt]
23094 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23095 using-declaration
23096 template-declaration
23097 alias-declaration
23099 member-declarator-list:
23100 member-declarator
23101 member-declarator-list , member-declarator
23103 member-declarator:
23104 declarator pure-specifier [opt]
23105 declarator constant-initializer [opt]
23106 identifier [opt] : constant-expression
23108 GNU Extensions:
23110 member-declaration:
23111 __extension__ member-declaration
23113 member-declarator:
23114 declarator attributes [opt] pure-specifier [opt]
23115 declarator attributes [opt] constant-initializer [opt]
23116 identifier [opt] attributes [opt] : constant-expression
23118 C++0x Extensions:
23120 member-declaration:
23121 static_assert-declaration */
23123 static void
23124 cp_parser_member_declaration (cp_parser* parser)
23126 cp_decl_specifier_seq decl_specifiers;
23127 tree prefix_attributes;
23128 tree decl;
23129 int declares_class_or_enum;
23130 bool friend_p;
23131 cp_token *token = NULL;
23132 cp_token *decl_spec_token_start = NULL;
23133 cp_token *initializer_token_start = NULL;
23134 int saved_pedantic;
23135 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23137 /* Check for the `__extension__' keyword. */
23138 if (cp_parser_extension_opt (parser, &saved_pedantic))
23140 /* Recurse. */
23141 cp_parser_member_declaration (parser);
23142 /* Restore the old value of the PEDANTIC flag. */
23143 pedantic = saved_pedantic;
23145 return;
23148 /* Check for a template-declaration. */
23149 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23151 /* An explicit specialization here is an error condition, and we
23152 expect the specialization handler to detect and report this. */
23153 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23154 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23155 cp_parser_explicit_specialization (parser);
23156 else
23157 cp_parser_template_declaration (parser, /*member_p=*/true);
23159 return;
23161 /* Check for a template introduction. */
23162 else if (cp_parser_template_declaration_after_export (parser, true))
23163 return;
23165 /* Check for a using-declaration. */
23166 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23168 if (cxx_dialect < cxx11)
23170 /* Parse the using-declaration. */
23171 cp_parser_using_declaration (parser,
23172 /*access_declaration_p=*/false);
23173 return;
23175 else
23177 tree decl;
23178 bool alias_decl_expected;
23179 cp_parser_parse_tentatively (parser);
23180 decl = cp_parser_alias_declaration (parser);
23181 /* Note that if we actually see the '=' token after the
23182 identifier, cp_parser_alias_declaration commits the
23183 tentative parse. In that case, we really expect an
23184 alias-declaration. Otherwise, we expect a using
23185 declaration. */
23186 alias_decl_expected =
23187 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23188 cp_parser_parse_definitely (parser);
23190 if (alias_decl_expected)
23191 finish_member_declaration (decl);
23192 else
23193 cp_parser_using_declaration (parser,
23194 /*access_declaration_p=*/false);
23195 return;
23199 /* Check for @defs. */
23200 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23202 tree ivar, member;
23203 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23204 ivar = ivar_chains;
23205 while (ivar)
23207 member = ivar;
23208 ivar = TREE_CHAIN (member);
23209 TREE_CHAIN (member) = NULL_TREE;
23210 finish_member_declaration (member);
23212 return;
23215 /* If the next token is `static_assert' we have a static assertion. */
23216 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23218 cp_parser_static_assert (parser, /*member_p=*/true);
23219 return;
23222 parser->colon_corrects_to_scope_p = false;
23224 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23225 goto out;
23227 /* Parse the decl-specifier-seq. */
23228 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23229 cp_parser_decl_specifier_seq (parser,
23230 CP_PARSER_FLAGS_OPTIONAL,
23231 &decl_specifiers,
23232 &declares_class_or_enum);
23233 /* Check for an invalid type-name. */
23234 if (!decl_specifiers.any_type_specifiers_p
23235 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23236 goto out;
23237 /* If there is no declarator, then the decl-specifier-seq should
23238 specify a type. */
23239 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23241 /* If there was no decl-specifier-seq, and the next token is a
23242 `;', then we have something like:
23244 struct S { ; };
23246 [class.mem]
23248 Each member-declaration shall declare at least one member
23249 name of the class. */
23250 if (!decl_specifiers.any_specifiers_p)
23252 cp_token *token = cp_lexer_peek_token (parser->lexer);
23253 if (!in_system_header_at (token->location))
23255 gcc_rich_location richloc (token->location);
23256 richloc.add_fixit_remove ();
23257 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23260 else
23262 tree type;
23264 /* See if this declaration is a friend. */
23265 friend_p = cp_parser_friend_p (&decl_specifiers);
23266 /* If there were decl-specifiers, check to see if there was
23267 a class-declaration. */
23268 type = check_tag_decl (&decl_specifiers,
23269 /*explicit_type_instantiation_p=*/false);
23270 /* Nested classes have already been added to the class, but
23271 a `friend' needs to be explicitly registered. */
23272 if (friend_p)
23274 /* If the `friend' keyword was present, the friend must
23275 be introduced with a class-key. */
23276 if (!declares_class_or_enum && cxx_dialect < cxx11)
23277 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23278 "in C++03 a class-key must be used "
23279 "when declaring a friend");
23280 /* In this case:
23282 template <typename T> struct A {
23283 friend struct A<T>::B;
23286 A<T>::B will be represented by a TYPENAME_TYPE, and
23287 therefore not recognized by check_tag_decl. */
23288 if (!type)
23290 type = decl_specifiers.type;
23291 if (type && TREE_CODE (type) == TYPE_DECL)
23292 type = TREE_TYPE (type);
23294 if (!type || !TYPE_P (type))
23295 error_at (decl_spec_token_start->location,
23296 "friend declaration does not name a class or "
23297 "function");
23298 else
23299 make_friend_class (current_class_type, type,
23300 /*complain=*/true);
23302 /* If there is no TYPE, an error message will already have
23303 been issued. */
23304 else if (!type || type == error_mark_node)
23306 /* An anonymous aggregate has to be handled specially; such
23307 a declaration really declares a data member (with a
23308 particular type), as opposed to a nested class. */
23309 else if (ANON_AGGR_TYPE_P (type))
23311 /* C++11 9.5/6. */
23312 if (decl_specifiers.storage_class != sc_none)
23313 error_at (decl_spec_token_start->location,
23314 "a storage class on an anonymous aggregate "
23315 "in class scope is not allowed");
23317 /* Remove constructors and such from TYPE, now that we
23318 know it is an anonymous aggregate. */
23319 fixup_anonymous_aggr (type);
23320 /* And make the corresponding data member. */
23321 decl = build_decl (decl_spec_token_start->location,
23322 FIELD_DECL, NULL_TREE, type);
23323 /* Add it to the class. */
23324 finish_member_declaration (decl);
23326 else
23327 cp_parser_check_access_in_redeclaration
23328 (TYPE_NAME (type),
23329 decl_spec_token_start->location);
23332 else
23334 bool assume_semicolon = false;
23336 /* Clear attributes from the decl_specifiers but keep them
23337 around as prefix attributes that apply them to the entity
23338 being declared. */
23339 prefix_attributes = decl_specifiers.attributes;
23340 decl_specifiers.attributes = NULL_TREE;
23342 /* See if these declarations will be friends. */
23343 friend_p = cp_parser_friend_p (&decl_specifiers);
23345 /* Keep going until we hit the `;' at the end of the
23346 declaration. */
23347 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23349 tree attributes = NULL_TREE;
23350 tree first_attribute;
23352 /* Peek at the next token. */
23353 token = cp_lexer_peek_token (parser->lexer);
23355 /* Check for a bitfield declaration. */
23356 if (token->type == CPP_COLON
23357 || (token->type == CPP_NAME
23358 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23359 == CPP_COLON))
23361 tree identifier;
23362 tree width;
23364 /* Get the name of the bitfield. Note that we cannot just
23365 check TOKEN here because it may have been invalidated by
23366 the call to cp_lexer_peek_nth_token above. */
23367 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23368 identifier = cp_parser_identifier (parser);
23369 else
23370 identifier = NULL_TREE;
23372 /* Consume the `:' token. */
23373 cp_lexer_consume_token (parser->lexer);
23374 /* Get the width of the bitfield. */
23375 width
23376 = cp_parser_constant_expression (parser);
23378 /* Look for attributes that apply to the bitfield. */
23379 attributes = cp_parser_attributes_opt (parser);
23380 /* Remember which attributes are prefix attributes and
23381 which are not. */
23382 first_attribute = attributes;
23383 /* Combine the attributes. */
23384 attributes = chainon (prefix_attributes, attributes);
23386 /* Create the bitfield declaration. */
23387 decl = grokbitfield (identifier
23388 ? make_id_declarator (NULL_TREE,
23389 identifier,
23390 sfk_none)
23391 : NULL,
23392 &decl_specifiers,
23393 width,
23394 attributes);
23396 else
23398 cp_declarator *declarator;
23399 tree initializer;
23400 tree asm_specification;
23401 int ctor_dtor_or_conv_p;
23403 /* Parse the declarator. */
23404 declarator
23405 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23406 &ctor_dtor_or_conv_p,
23407 /*parenthesized_p=*/NULL,
23408 /*member_p=*/true,
23409 friend_p);
23411 /* If something went wrong parsing the declarator, make sure
23412 that we at least consume some tokens. */
23413 if (declarator == cp_error_declarator)
23415 /* Skip to the end of the statement. */
23416 cp_parser_skip_to_end_of_statement (parser);
23417 /* If the next token is not a semicolon, that is
23418 probably because we just skipped over the body of
23419 a function. So, we consume a semicolon if
23420 present, but do not issue an error message if it
23421 is not present. */
23422 if (cp_lexer_next_token_is (parser->lexer,
23423 CPP_SEMICOLON))
23424 cp_lexer_consume_token (parser->lexer);
23425 goto out;
23428 if (declares_class_or_enum & 2)
23429 cp_parser_check_for_definition_in_return_type
23430 (declarator, decl_specifiers.type,
23431 decl_specifiers.locations[ds_type_spec]);
23433 /* Look for an asm-specification. */
23434 asm_specification = cp_parser_asm_specification_opt (parser);
23435 /* Look for attributes that apply to the declaration. */
23436 attributes = cp_parser_attributes_opt (parser);
23437 /* Remember which attributes are prefix attributes and
23438 which are not. */
23439 first_attribute = attributes;
23440 /* Combine the attributes. */
23441 attributes = chainon (prefix_attributes, attributes);
23443 /* If it's an `=', then we have a constant-initializer or a
23444 pure-specifier. It is not correct to parse the
23445 initializer before registering the member declaration
23446 since the member declaration should be in scope while
23447 its initializer is processed. However, the rest of the
23448 front end does not yet provide an interface that allows
23449 us to handle this correctly. */
23450 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23452 /* In [class.mem]:
23454 A pure-specifier shall be used only in the declaration of
23455 a virtual function.
23457 A member-declarator can contain a constant-initializer
23458 only if it declares a static member of integral or
23459 enumeration type.
23461 Therefore, if the DECLARATOR is for a function, we look
23462 for a pure-specifier; otherwise, we look for a
23463 constant-initializer. When we call `grokfield', it will
23464 perform more stringent semantics checks. */
23465 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23466 if (function_declarator_p (declarator)
23467 || (decl_specifiers.type
23468 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23469 && declarator->kind == cdk_id
23470 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23471 == FUNCTION_TYPE)))
23472 initializer = cp_parser_pure_specifier (parser);
23473 else if (decl_specifiers.storage_class != sc_static)
23474 initializer = cp_parser_save_nsdmi (parser);
23475 else if (cxx_dialect >= cxx11)
23477 bool nonconst;
23478 /* Don't require a constant rvalue in C++11, since we
23479 might want a reference constant. We'll enforce
23480 constancy later. */
23481 cp_lexer_consume_token (parser->lexer);
23482 /* Parse the initializer. */
23483 initializer = cp_parser_initializer_clause (parser,
23484 &nonconst);
23486 else
23487 /* Parse the initializer. */
23488 initializer = cp_parser_constant_initializer (parser);
23490 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23491 && !function_declarator_p (declarator))
23493 bool x;
23494 if (decl_specifiers.storage_class != sc_static)
23495 initializer = cp_parser_save_nsdmi (parser);
23496 else
23497 initializer = cp_parser_initializer (parser, &x, &x);
23499 /* Otherwise, there is no initializer. */
23500 else
23501 initializer = NULL_TREE;
23503 /* See if we are probably looking at a function
23504 definition. We are certainly not looking at a
23505 member-declarator. Calling `grokfield' has
23506 side-effects, so we must not do it unless we are sure
23507 that we are looking at a member-declarator. */
23508 if (cp_parser_token_starts_function_definition_p
23509 (cp_lexer_peek_token (parser->lexer)))
23511 /* The grammar does not allow a pure-specifier to be
23512 used when a member function is defined. (It is
23513 possible that this fact is an oversight in the
23514 standard, since a pure function may be defined
23515 outside of the class-specifier. */
23516 if (initializer && initializer_token_start)
23517 error_at (initializer_token_start->location,
23518 "pure-specifier on function-definition");
23519 decl = cp_parser_save_member_function_body (parser,
23520 &decl_specifiers,
23521 declarator,
23522 attributes);
23523 if (parser->fully_implicit_function_template_p)
23524 decl = finish_fully_implicit_template (parser, decl);
23525 /* If the member was not a friend, declare it here. */
23526 if (!friend_p)
23527 finish_member_declaration (decl);
23528 /* Peek at the next token. */
23529 token = cp_lexer_peek_token (parser->lexer);
23530 /* If the next token is a semicolon, consume it. */
23531 if (token->type == CPP_SEMICOLON)
23533 location_t semicolon_loc
23534 = cp_lexer_consume_token (parser->lexer)->location;
23535 gcc_rich_location richloc (semicolon_loc);
23536 richloc.add_fixit_remove ();
23537 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23538 "extra %<;%> after in-class "
23539 "function definition");
23541 goto out;
23543 else
23544 if (declarator->kind == cdk_function)
23545 declarator->id_loc = token->location;
23546 /* Create the declaration. */
23547 decl = grokfield (declarator, &decl_specifiers,
23548 initializer, /*init_const_expr_p=*/true,
23549 asm_specification, attributes);
23550 if (parser->fully_implicit_function_template_p)
23552 if (friend_p)
23553 finish_fully_implicit_template (parser, 0);
23554 else
23555 decl = finish_fully_implicit_template (parser, decl);
23559 cp_finalize_omp_declare_simd (parser, decl);
23560 cp_finalize_oacc_routine (parser, decl, false);
23562 /* Reset PREFIX_ATTRIBUTES. */
23563 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23564 attributes = TREE_CHAIN (attributes);
23565 if (attributes)
23566 TREE_CHAIN (attributes) = NULL_TREE;
23568 /* If there is any qualification still in effect, clear it
23569 now; we will be starting fresh with the next declarator. */
23570 parser->scope = NULL_TREE;
23571 parser->qualifying_scope = NULL_TREE;
23572 parser->object_scope = NULL_TREE;
23573 /* If it's a `,', then there are more declarators. */
23574 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23576 cp_lexer_consume_token (parser->lexer);
23577 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23579 cp_token *token = cp_lexer_previous_token (parser->lexer);
23580 gcc_rich_location richloc (token->location);
23581 richloc.add_fixit_remove ();
23582 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23583 "member declaration");
23586 /* If the next token isn't a `;', then we have a parse error. */
23587 else if (cp_lexer_next_token_is_not (parser->lexer,
23588 CPP_SEMICOLON))
23590 /* The next token might be a ways away from where the
23591 actual semicolon is missing. Find the previous token
23592 and use that for our error position. */
23593 cp_token *token = cp_lexer_previous_token (parser->lexer);
23594 gcc_rich_location richloc (token->location);
23595 richloc.add_fixit_insert_after (";");
23596 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23597 "member declaration");
23599 /* Assume that the user meant to provide a semicolon. If
23600 we were to cp_parser_skip_to_end_of_statement, we might
23601 skip to a semicolon inside a member function definition
23602 and issue nonsensical error messages. */
23603 assume_semicolon = true;
23606 if (decl)
23608 /* Add DECL to the list of members. */
23609 if (!friend_p
23610 /* Explicitly include, eg, NSDMIs, for better error
23611 recovery (c++/58650). */
23612 || !DECL_DECLARES_FUNCTION_P (decl))
23613 finish_member_declaration (decl);
23615 if (TREE_CODE (decl) == FUNCTION_DECL)
23616 cp_parser_save_default_args (parser, decl);
23617 else if (TREE_CODE (decl) == FIELD_DECL
23618 && !DECL_C_BIT_FIELD (decl)
23619 && DECL_INITIAL (decl))
23620 /* Add DECL to the queue of NSDMI to be parsed later. */
23621 vec_safe_push (unparsed_nsdmis, decl);
23624 if (assume_semicolon)
23625 goto out;
23629 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23630 out:
23631 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23634 /* Parse a pure-specifier.
23636 pure-specifier:
23639 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23640 Otherwise, ERROR_MARK_NODE is returned. */
23642 static tree
23643 cp_parser_pure_specifier (cp_parser* parser)
23645 cp_token *token;
23647 /* Look for the `=' token. */
23648 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23649 return error_mark_node;
23650 /* Look for the `0' token. */
23651 token = cp_lexer_peek_token (parser->lexer);
23653 if (token->type == CPP_EOF
23654 || token->type == CPP_PRAGMA_EOL)
23655 return error_mark_node;
23657 cp_lexer_consume_token (parser->lexer);
23659 /* Accept = default or = delete in c++0x mode. */
23660 if (token->keyword == RID_DEFAULT
23661 || token->keyword == RID_DELETE)
23663 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23664 return token->u.value;
23667 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23668 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23670 cp_parser_error (parser,
23671 "invalid pure specifier (only %<= 0%> is allowed)");
23672 cp_parser_skip_to_end_of_statement (parser);
23673 return error_mark_node;
23675 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23677 error_at (token->location, "templates may not be %<virtual%>");
23678 return error_mark_node;
23681 return integer_zero_node;
23684 /* Parse a constant-initializer.
23686 constant-initializer:
23687 = constant-expression
23689 Returns a representation of the constant-expression. */
23691 static tree
23692 cp_parser_constant_initializer (cp_parser* parser)
23694 /* Look for the `=' token. */
23695 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23696 return error_mark_node;
23698 /* It is invalid to write:
23700 struct S { static const int i = { 7 }; };
23703 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23705 cp_parser_error (parser,
23706 "a brace-enclosed initializer is not allowed here");
23707 /* Consume the opening brace. */
23708 cp_lexer_consume_token (parser->lexer);
23709 /* Skip the initializer. */
23710 cp_parser_skip_to_closing_brace (parser);
23711 /* Look for the trailing `}'. */
23712 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23714 return error_mark_node;
23717 return cp_parser_constant_expression (parser);
23720 /* Derived classes [gram.class.derived] */
23722 /* Parse a base-clause.
23724 base-clause:
23725 : base-specifier-list
23727 base-specifier-list:
23728 base-specifier ... [opt]
23729 base-specifier-list , base-specifier ... [opt]
23731 Returns a TREE_LIST representing the base-classes, in the order in
23732 which they were declared. The representation of each node is as
23733 described by cp_parser_base_specifier.
23735 In the case that no bases are specified, this function will return
23736 NULL_TREE, not ERROR_MARK_NODE. */
23738 static tree
23739 cp_parser_base_clause (cp_parser* parser)
23741 tree bases = NULL_TREE;
23743 /* Look for the `:' that begins the list. */
23744 cp_parser_require (parser, CPP_COLON, RT_COLON);
23746 /* Scan the base-specifier-list. */
23747 while (true)
23749 cp_token *token;
23750 tree base;
23751 bool pack_expansion_p = false;
23753 /* Look for the base-specifier. */
23754 base = cp_parser_base_specifier (parser);
23755 /* Look for the (optional) ellipsis. */
23756 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23758 /* Consume the `...'. */
23759 cp_lexer_consume_token (parser->lexer);
23761 pack_expansion_p = true;
23764 /* Add BASE to the front of the list. */
23765 if (base && base != error_mark_node)
23767 if (pack_expansion_p)
23768 /* Make this a pack expansion type. */
23769 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23771 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23773 TREE_CHAIN (base) = bases;
23774 bases = base;
23777 /* Peek at the next token. */
23778 token = cp_lexer_peek_token (parser->lexer);
23779 /* If it's not a comma, then the list is complete. */
23780 if (token->type != CPP_COMMA)
23781 break;
23782 /* Consume the `,'. */
23783 cp_lexer_consume_token (parser->lexer);
23786 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23787 base class had a qualified name. However, the next name that
23788 appears is certainly not qualified. */
23789 parser->scope = NULL_TREE;
23790 parser->qualifying_scope = NULL_TREE;
23791 parser->object_scope = NULL_TREE;
23793 return nreverse (bases);
23796 /* Parse a base-specifier.
23798 base-specifier:
23799 :: [opt] nested-name-specifier [opt] class-name
23800 virtual access-specifier [opt] :: [opt] nested-name-specifier
23801 [opt] class-name
23802 access-specifier virtual [opt] :: [opt] nested-name-specifier
23803 [opt] class-name
23805 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23806 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23807 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23808 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23810 static tree
23811 cp_parser_base_specifier (cp_parser* parser)
23813 cp_token *token;
23814 bool done = false;
23815 bool virtual_p = false;
23816 bool duplicate_virtual_error_issued_p = false;
23817 bool duplicate_access_error_issued_p = false;
23818 bool class_scope_p, template_p;
23819 tree access = access_default_node;
23820 tree type;
23822 /* Process the optional `virtual' and `access-specifier'. */
23823 while (!done)
23825 /* Peek at the next token. */
23826 token = cp_lexer_peek_token (parser->lexer);
23827 /* Process `virtual'. */
23828 switch (token->keyword)
23830 case RID_VIRTUAL:
23831 /* If `virtual' appears more than once, issue an error. */
23832 if (virtual_p && !duplicate_virtual_error_issued_p)
23834 cp_parser_error (parser,
23835 "%<virtual%> specified more than once in base-specifier");
23836 duplicate_virtual_error_issued_p = true;
23839 virtual_p = true;
23841 /* Consume the `virtual' token. */
23842 cp_lexer_consume_token (parser->lexer);
23844 break;
23846 case RID_PUBLIC:
23847 case RID_PROTECTED:
23848 case RID_PRIVATE:
23849 /* If more than one access specifier appears, issue an
23850 error. */
23851 if (access != access_default_node
23852 && !duplicate_access_error_issued_p)
23854 cp_parser_error (parser,
23855 "more than one access specifier in base-specifier");
23856 duplicate_access_error_issued_p = true;
23859 access = ridpointers[(int) token->keyword];
23861 /* Consume the access-specifier. */
23862 cp_lexer_consume_token (parser->lexer);
23864 break;
23866 default:
23867 done = true;
23868 break;
23871 /* It is not uncommon to see programs mechanically, erroneously, use
23872 the 'typename' keyword to denote (dependent) qualified types
23873 as base classes. */
23874 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23876 token = cp_lexer_peek_token (parser->lexer);
23877 if (!processing_template_decl)
23878 error_at (token->location,
23879 "keyword %<typename%> not allowed outside of templates");
23880 else
23881 error_at (token->location,
23882 "keyword %<typename%> not allowed in this context "
23883 "(the base class is implicitly a type)");
23884 cp_lexer_consume_token (parser->lexer);
23887 /* Look for the optional `::' operator. */
23888 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23889 /* Look for the nested-name-specifier. The simplest way to
23890 implement:
23892 [temp.res]
23894 The keyword `typename' is not permitted in a base-specifier or
23895 mem-initializer; in these contexts a qualified name that
23896 depends on a template-parameter is implicitly assumed to be a
23897 type name.
23899 is to pretend that we have seen the `typename' keyword at this
23900 point. */
23901 cp_parser_nested_name_specifier_opt (parser,
23902 /*typename_keyword_p=*/true,
23903 /*check_dependency_p=*/true,
23904 /*type_p=*/true,
23905 /*is_declaration=*/true);
23906 /* If the base class is given by a qualified name, assume that names
23907 we see are type names or templates, as appropriate. */
23908 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23909 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23911 if (!parser->scope
23912 && cp_lexer_next_token_is_decltype (parser->lexer))
23913 /* DR 950 allows decltype as a base-specifier. */
23914 type = cp_parser_decltype (parser);
23915 else
23917 /* Otherwise, look for the class-name. */
23918 type = cp_parser_class_name (parser,
23919 class_scope_p,
23920 template_p,
23921 typename_type,
23922 /*check_dependency_p=*/true,
23923 /*class_head_p=*/false,
23924 /*is_declaration=*/true);
23925 type = TREE_TYPE (type);
23928 if (type == error_mark_node)
23929 return error_mark_node;
23931 return finish_base_specifier (type, access, virtual_p);
23934 /* Exception handling [gram.exception] */
23936 /* Parse an (optional) noexcept-specification.
23938 noexcept-specification:
23939 noexcept ( constant-expression ) [opt]
23941 If no noexcept-specification is present, returns NULL_TREE.
23942 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23943 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23944 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23945 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23946 in which case a boolean condition is returned instead. */
23948 static tree
23949 cp_parser_noexcept_specification_opt (cp_parser* parser,
23950 bool require_constexpr,
23951 bool* consumed_expr,
23952 bool return_cond)
23954 cp_token *token;
23955 const char *saved_message;
23957 /* Peek at the next token. */
23958 token = cp_lexer_peek_token (parser->lexer);
23960 /* Is it a noexcept-specification? */
23961 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
23963 tree expr;
23964 cp_lexer_consume_token (parser->lexer);
23966 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
23968 cp_lexer_consume_token (parser->lexer);
23970 if (require_constexpr)
23972 /* Types may not be defined in an exception-specification. */
23973 saved_message = parser->type_definition_forbidden_message;
23974 parser->type_definition_forbidden_message
23975 = G_("types may not be defined in an exception-specification");
23977 expr = cp_parser_constant_expression (parser);
23979 /* Restore the saved message. */
23980 parser->type_definition_forbidden_message = saved_message;
23982 else
23984 expr = cp_parser_expression (parser);
23985 *consumed_expr = true;
23988 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23990 else
23992 expr = boolean_true_node;
23993 if (!require_constexpr)
23994 *consumed_expr = false;
23997 /* We cannot build a noexcept-spec right away because this will check
23998 that expr is a constexpr. */
23999 if (!return_cond)
24000 return build_noexcept_spec (expr, tf_warning_or_error);
24001 else
24002 return expr;
24004 else
24005 return NULL_TREE;
24008 /* Parse an (optional) exception-specification.
24010 exception-specification:
24011 throw ( type-id-list [opt] )
24013 Returns a TREE_LIST representing the exception-specification. The
24014 TREE_VALUE of each node is a type. */
24016 static tree
24017 cp_parser_exception_specification_opt (cp_parser* parser)
24019 cp_token *token;
24020 tree type_id_list;
24021 const char *saved_message;
24023 /* Peek at the next token. */
24024 token = cp_lexer_peek_token (parser->lexer);
24026 /* Is it a noexcept-specification? */
24027 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24028 false);
24029 if (type_id_list != NULL_TREE)
24030 return type_id_list;
24032 /* If it's not `throw', then there's no exception-specification. */
24033 if (!cp_parser_is_keyword (token, RID_THROW))
24034 return NULL_TREE;
24036 location_t loc = token->location;
24038 /* Consume the `throw'. */
24039 cp_lexer_consume_token (parser->lexer);
24041 /* Look for the `('. */
24042 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24044 /* Peek at the next token. */
24045 token = cp_lexer_peek_token (parser->lexer);
24046 /* If it's not a `)', then there is a type-id-list. */
24047 if (token->type != CPP_CLOSE_PAREN)
24049 /* Types may not be defined in an exception-specification. */
24050 saved_message = parser->type_definition_forbidden_message;
24051 parser->type_definition_forbidden_message
24052 = G_("types may not be defined in an exception-specification");
24053 /* Parse the type-id-list. */
24054 type_id_list = cp_parser_type_id_list (parser);
24055 /* Restore the saved message. */
24056 parser->type_definition_forbidden_message = saved_message;
24058 if (cxx_dialect >= cxx1z)
24060 error_at (loc, "ISO C++1z does not allow dynamic exception "
24061 "specifications");
24062 type_id_list = NULL_TREE;
24064 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24065 warning_at (loc, OPT_Wdeprecated,
24066 "dynamic exception specifications are deprecated in "
24067 "C++11");
24069 /* In C++17, throw() is equivalent to noexcept (true). throw()
24070 is deprecated in C++11 and above as well, but is still widely used,
24071 so don't warn about it yet. */
24072 else if (cxx_dialect >= cxx1z)
24073 type_id_list = noexcept_true_spec;
24074 else
24075 type_id_list = empty_except_spec;
24077 /* Look for the `)'. */
24078 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24080 return type_id_list;
24083 /* Parse an (optional) type-id-list.
24085 type-id-list:
24086 type-id ... [opt]
24087 type-id-list , type-id ... [opt]
24089 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24090 in the order that the types were presented. */
24092 static tree
24093 cp_parser_type_id_list (cp_parser* parser)
24095 tree types = NULL_TREE;
24097 while (true)
24099 cp_token *token;
24100 tree type;
24102 token = cp_lexer_peek_token (parser->lexer);
24104 /* Get the next type-id. */
24105 type = cp_parser_type_id (parser);
24106 /* Check for invalid 'auto'. */
24107 if (flag_concepts && type_uses_auto (type))
24109 error_at (token->location,
24110 "invalid use of %<auto%> in exception-specification");
24111 type = error_mark_node;
24113 /* Parse the optional ellipsis. */
24114 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24116 /* Consume the `...'. */
24117 cp_lexer_consume_token (parser->lexer);
24119 /* Turn the type into a pack expansion expression. */
24120 type = make_pack_expansion (type);
24122 /* Add it to the list. */
24123 types = add_exception_specifier (types, type, /*complain=*/1);
24124 /* Peek at the next token. */
24125 token = cp_lexer_peek_token (parser->lexer);
24126 /* If it is not a `,', we are done. */
24127 if (token->type != CPP_COMMA)
24128 break;
24129 /* Consume the `,'. */
24130 cp_lexer_consume_token (parser->lexer);
24133 return nreverse (types);
24136 /* Parse a try-block.
24138 try-block:
24139 try compound-statement handler-seq */
24141 static tree
24142 cp_parser_try_block (cp_parser* parser)
24144 tree try_block;
24146 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24147 if (parser->in_function_body
24148 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24149 error ("%<try%> in %<constexpr%> function");
24151 try_block = begin_try_block ();
24152 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24153 finish_try_block (try_block);
24154 cp_parser_handler_seq (parser);
24155 finish_handler_sequence (try_block);
24157 return try_block;
24160 /* Parse a function-try-block.
24162 function-try-block:
24163 try ctor-initializer [opt] function-body handler-seq */
24165 static bool
24166 cp_parser_function_try_block (cp_parser* parser)
24168 tree compound_stmt;
24169 tree try_block;
24170 bool ctor_initializer_p;
24172 /* Look for the `try' keyword. */
24173 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24174 return false;
24175 /* Let the rest of the front end know where we are. */
24176 try_block = begin_function_try_block (&compound_stmt);
24177 /* Parse the function-body. */
24178 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24179 (parser, /*in_function_try_block=*/true);
24180 /* We're done with the `try' part. */
24181 finish_function_try_block (try_block);
24182 /* Parse the handlers. */
24183 cp_parser_handler_seq (parser);
24184 /* We're done with the handlers. */
24185 finish_function_handler_sequence (try_block, compound_stmt);
24187 return ctor_initializer_p;
24190 /* Parse a handler-seq.
24192 handler-seq:
24193 handler handler-seq [opt] */
24195 static void
24196 cp_parser_handler_seq (cp_parser* parser)
24198 while (true)
24200 cp_token *token;
24202 /* Parse the handler. */
24203 cp_parser_handler (parser);
24204 /* Peek at the next token. */
24205 token = cp_lexer_peek_token (parser->lexer);
24206 /* If it's not `catch' then there are no more handlers. */
24207 if (!cp_parser_is_keyword (token, RID_CATCH))
24208 break;
24212 /* Parse a handler.
24214 handler:
24215 catch ( exception-declaration ) compound-statement */
24217 static void
24218 cp_parser_handler (cp_parser* parser)
24220 tree handler;
24221 tree declaration;
24223 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24224 handler = begin_handler ();
24225 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24226 declaration = cp_parser_exception_declaration (parser);
24227 finish_handler_parms (declaration, handler);
24228 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24229 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24230 finish_handler (handler);
24233 /* Parse an exception-declaration.
24235 exception-declaration:
24236 type-specifier-seq declarator
24237 type-specifier-seq abstract-declarator
24238 type-specifier-seq
24241 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24242 ellipsis variant is used. */
24244 static tree
24245 cp_parser_exception_declaration (cp_parser* parser)
24247 cp_decl_specifier_seq type_specifiers;
24248 cp_declarator *declarator;
24249 const char *saved_message;
24251 /* If it's an ellipsis, it's easy to handle. */
24252 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24254 /* Consume the `...' token. */
24255 cp_lexer_consume_token (parser->lexer);
24256 return NULL_TREE;
24259 /* Types may not be defined in exception-declarations. */
24260 saved_message = parser->type_definition_forbidden_message;
24261 parser->type_definition_forbidden_message
24262 = G_("types may not be defined in exception-declarations");
24264 /* Parse the type-specifier-seq. */
24265 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24266 /*is_trailing_return=*/false,
24267 &type_specifiers);
24268 /* If it's a `)', then there is no declarator. */
24269 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24270 declarator = NULL;
24271 else
24272 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24273 /*ctor_dtor_or_conv_p=*/NULL,
24274 /*parenthesized_p=*/NULL,
24275 /*member_p=*/false,
24276 /*friend_p=*/false);
24278 /* Restore the saved message. */
24279 parser->type_definition_forbidden_message = saved_message;
24281 if (!type_specifiers.any_specifiers_p)
24282 return error_mark_node;
24284 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24287 /* Parse a throw-expression.
24289 throw-expression:
24290 throw assignment-expression [opt]
24292 Returns a THROW_EXPR representing the throw-expression. */
24294 static tree
24295 cp_parser_throw_expression (cp_parser* parser)
24297 tree expression;
24298 cp_token* token;
24300 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24301 token = cp_lexer_peek_token (parser->lexer);
24302 /* Figure out whether or not there is an assignment-expression
24303 following the "throw" keyword. */
24304 if (token->type == CPP_COMMA
24305 || token->type == CPP_SEMICOLON
24306 || token->type == CPP_CLOSE_PAREN
24307 || token->type == CPP_CLOSE_SQUARE
24308 || token->type == CPP_CLOSE_BRACE
24309 || token->type == CPP_COLON)
24310 expression = NULL_TREE;
24311 else
24312 expression = cp_parser_assignment_expression (parser);
24314 return build_throw (expression);
24317 /* GNU Extensions */
24319 /* Parse an (optional) asm-specification.
24321 asm-specification:
24322 asm ( string-literal )
24324 If the asm-specification is present, returns a STRING_CST
24325 corresponding to the string-literal. Otherwise, returns
24326 NULL_TREE. */
24328 static tree
24329 cp_parser_asm_specification_opt (cp_parser* parser)
24331 cp_token *token;
24332 tree asm_specification;
24334 /* Peek at the next token. */
24335 token = cp_lexer_peek_token (parser->lexer);
24336 /* If the next token isn't the `asm' keyword, then there's no
24337 asm-specification. */
24338 if (!cp_parser_is_keyword (token, RID_ASM))
24339 return NULL_TREE;
24341 /* Consume the `asm' token. */
24342 cp_lexer_consume_token (parser->lexer);
24343 /* Look for the `('. */
24344 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24346 /* Look for the string-literal. */
24347 asm_specification = cp_parser_string_literal (parser, false, false);
24349 /* Look for the `)'. */
24350 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24352 return asm_specification;
24355 /* Parse an asm-operand-list.
24357 asm-operand-list:
24358 asm-operand
24359 asm-operand-list , asm-operand
24361 asm-operand:
24362 string-literal ( expression )
24363 [ string-literal ] string-literal ( expression )
24365 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24366 each node is the expression. The TREE_PURPOSE is itself a
24367 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24368 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24369 is a STRING_CST for the string literal before the parenthesis. Returns
24370 ERROR_MARK_NODE if any of the operands are invalid. */
24372 static tree
24373 cp_parser_asm_operand_list (cp_parser* parser)
24375 tree asm_operands = NULL_TREE;
24376 bool invalid_operands = false;
24378 while (true)
24380 tree string_literal;
24381 tree expression;
24382 tree name;
24384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24386 /* Consume the `[' token. */
24387 cp_lexer_consume_token (parser->lexer);
24388 /* Read the operand name. */
24389 name = cp_parser_identifier (parser);
24390 if (name != error_mark_node)
24391 name = build_string (IDENTIFIER_LENGTH (name),
24392 IDENTIFIER_POINTER (name));
24393 /* Look for the closing `]'. */
24394 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24396 else
24397 name = NULL_TREE;
24398 /* Look for the string-literal. */
24399 string_literal = cp_parser_string_literal (parser, false, false);
24401 /* Look for the `('. */
24402 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24403 /* Parse the expression. */
24404 expression = cp_parser_expression (parser);
24405 /* Look for the `)'. */
24406 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24408 if (name == error_mark_node
24409 || string_literal == error_mark_node
24410 || expression == error_mark_node)
24411 invalid_operands = true;
24413 /* Add this operand to the list. */
24414 asm_operands = tree_cons (build_tree_list (name, string_literal),
24415 expression,
24416 asm_operands);
24417 /* If the next token is not a `,', there are no more
24418 operands. */
24419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24420 break;
24421 /* Consume the `,'. */
24422 cp_lexer_consume_token (parser->lexer);
24425 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24428 /* Parse an asm-clobber-list.
24430 asm-clobber-list:
24431 string-literal
24432 asm-clobber-list , string-literal
24434 Returns a TREE_LIST, indicating the clobbers in the order that they
24435 appeared. The TREE_VALUE of each node is a STRING_CST. */
24437 static tree
24438 cp_parser_asm_clobber_list (cp_parser* parser)
24440 tree clobbers = NULL_TREE;
24442 while (true)
24444 tree string_literal;
24446 /* Look for the string literal. */
24447 string_literal = cp_parser_string_literal (parser, false, false);
24448 /* Add it to the list. */
24449 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24450 /* If the next token is not a `,', then the list is
24451 complete. */
24452 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24453 break;
24454 /* Consume the `,' token. */
24455 cp_lexer_consume_token (parser->lexer);
24458 return clobbers;
24461 /* Parse an asm-label-list.
24463 asm-label-list:
24464 identifier
24465 asm-label-list , identifier
24467 Returns a TREE_LIST, indicating the labels in the order that they
24468 appeared. The TREE_VALUE of each node is a label. */
24470 static tree
24471 cp_parser_asm_label_list (cp_parser* parser)
24473 tree labels = NULL_TREE;
24475 while (true)
24477 tree identifier, label, name;
24479 /* Look for the identifier. */
24480 identifier = cp_parser_identifier (parser);
24481 if (!error_operand_p (identifier))
24483 label = lookup_label (identifier);
24484 if (TREE_CODE (label) == LABEL_DECL)
24486 TREE_USED (label) = 1;
24487 check_goto (label);
24488 name = build_string (IDENTIFIER_LENGTH (identifier),
24489 IDENTIFIER_POINTER (identifier));
24490 labels = tree_cons (name, label, labels);
24493 /* If the next token is not a `,', then the list is
24494 complete. */
24495 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24496 break;
24497 /* Consume the `,' token. */
24498 cp_lexer_consume_token (parser->lexer);
24501 return nreverse (labels);
24504 /* Return TRUE iff the next tokens in the stream are possibly the
24505 beginning of a GNU extension attribute. */
24507 static bool
24508 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24510 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24513 /* Return TRUE iff the next tokens in the stream are possibly the
24514 beginning of a standard C++-11 attribute specifier. */
24516 static bool
24517 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24519 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24522 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24523 beginning of a standard C++-11 attribute specifier. */
24525 static bool
24526 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24528 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24530 return (cxx_dialect >= cxx11
24531 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24532 || (token->type == CPP_OPEN_SQUARE
24533 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24534 && token->type == CPP_OPEN_SQUARE)));
24537 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24538 beginning of a GNU extension attribute. */
24540 static bool
24541 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24543 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24545 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24548 /* Return true iff the next tokens can be the beginning of either a
24549 GNU attribute list, or a standard C++11 attribute sequence. */
24551 static bool
24552 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24554 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24555 || cp_next_tokens_can_be_std_attribute_p (parser));
24558 /* Return true iff the next Nth tokens can be the beginning of either
24559 a GNU attribute list, or a standard C++11 attribute sequence. */
24561 static bool
24562 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24564 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24565 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24568 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24569 of GNU attributes, or return NULL. */
24571 static tree
24572 cp_parser_attributes_opt (cp_parser *parser)
24574 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24575 return cp_parser_gnu_attributes_opt (parser);
24576 return cp_parser_std_attribute_spec_seq (parser);
24579 #define CILK_SIMD_FN_CLAUSE_MASK \
24580 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24581 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24582 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24583 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24584 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24586 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24587 vector [(<clauses>)] */
24589 static void
24590 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24592 bool first_p = parser->cilk_simd_fn_info == NULL;
24593 cp_token *token = v_token;
24594 if (first_p)
24596 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24597 parser->cilk_simd_fn_info->error_seen = false;
24598 parser->cilk_simd_fn_info->fndecl_seen = false;
24599 parser->cilk_simd_fn_info->tokens = vNULL;
24600 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24602 int paren_scope = 0;
24603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24605 cp_lexer_consume_token (parser->lexer);
24606 v_token = cp_lexer_peek_token (parser->lexer);
24607 paren_scope++;
24609 while (paren_scope > 0)
24611 token = cp_lexer_peek_token (parser->lexer);
24612 if (token->type == CPP_OPEN_PAREN)
24613 paren_scope++;
24614 else if (token->type == CPP_CLOSE_PAREN)
24615 paren_scope--;
24616 /* Do not push the last ')' */
24617 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24618 cp_lexer_consume_token (parser->lexer);
24621 token->type = CPP_PRAGMA_EOL;
24622 parser->lexer->next_token = token;
24623 cp_lexer_consume_token (parser->lexer);
24625 struct cp_token_cache *cp
24626 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24627 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24630 /* Parse an (optional) series of attributes.
24632 attributes:
24633 attributes attribute
24635 attribute:
24636 __attribute__ (( attribute-list [opt] ))
24638 The return value is as for cp_parser_gnu_attribute_list. */
24640 static tree
24641 cp_parser_gnu_attributes_opt (cp_parser* parser)
24643 tree attributes = NULL_TREE;
24645 while (true)
24647 cp_token *token;
24648 tree attribute_list;
24649 bool ok = true;
24651 /* Peek at the next token. */
24652 token = cp_lexer_peek_token (parser->lexer);
24653 /* If it's not `__attribute__', then we're done. */
24654 if (token->keyword != RID_ATTRIBUTE)
24655 break;
24657 /* Consume the `__attribute__' keyword. */
24658 cp_lexer_consume_token (parser->lexer);
24659 /* Look for the two `(' tokens. */
24660 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24661 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24663 /* Peek at the next token. */
24664 token = cp_lexer_peek_token (parser->lexer);
24665 if (token->type != CPP_CLOSE_PAREN)
24666 /* Parse the attribute-list. */
24667 attribute_list = cp_parser_gnu_attribute_list (parser);
24668 else
24669 /* If the next token is a `)', then there is no attribute
24670 list. */
24671 attribute_list = NULL;
24673 /* Look for the two `)' tokens. */
24674 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24675 ok = false;
24676 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24677 ok = false;
24678 if (!ok)
24679 cp_parser_skip_to_end_of_statement (parser);
24681 /* Add these new attributes to the list. */
24682 attributes = chainon (attributes, attribute_list);
24685 return attributes;
24688 /* Parse a GNU attribute-list.
24690 attribute-list:
24691 attribute
24692 attribute-list , attribute
24694 attribute:
24695 identifier
24696 identifier ( identifier )
24697 identifier ( identifier , expression-list )
24698 identifier ( expression-list )
24700 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24701 to an attribute. The TREE_PURPOSE of each node is the identifier
24702 indicating which attribute is in use. The TREE_VALUE represents
24703 the arguments, if any. */
24705 static tree
24706 cp_parser_gnu_attribute_list (cp_parser* parser)
24708 tree attribute_list = NULL_TREE;
24709 bool save_translate_strings_p = parser->translate_strings_p;
24711 parser->translate_strings_p = false;
24712 while (true)
24714 cp_token *token;
24715 tree identifier;
24716 tree attribute;
24718 /* Look for the identifier. We also allow keywords here; for
24719 example `__attribute__ ((const))' is legal. */
24720 token = cp_lexer_peek_token (parser->lexer);
24721 if (token->type == CPP_NAME
24722 || token->type == CPP_KEYWORD)
24724 tree arguments = NULL_TREE;
24726 /* Consume the token, but save it since we need it for the
24727 SIMD enabled function parsing. */
24728 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24730 /* Save away the identifier that indicates which attribute
24731 this is. */
24732 identifier = (token->type == CPP_KEYWORD)
24733 /* For keywords, use the canonical spelling, not the
24734 parsed identifier. */
24735 ? ridpointers[(int) token->keyword]
24736 : id_token->u.value;
24738 attribute = build_tree_list (identifier, NULL_TREE);
24740 /* Peek at the next token. */
24741 token = cp_lexer_peek_token (parser->lexer);
24742 /* If it's an `(', then parse the attribute arguments. */
24743 if (token->type == CPP_OPEN_PAREN)
24745 vec<tree, va_gc> *vec;
24746 int attr_flag = (attribute_takes_identifier_p (identifier)
24747 ? id_attr : normal_attr);
24748 if (is_cilkplus_vector_p (identifier))
24750 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24751 continue;
24753 else
24754 vec = cp_parser_parenthesized_expression_list
24755 (parser, attr_flag, /*cast_p=*/false,
24756 /*allow_expansion_p=*/false,
24757 /*non_constant_p=*/NULL);
24758 if (vec == NULL)
24759 arguments = error_mark_node;
24760 else
24762 arguments = build_tree_list_vec (vec);
24763 release_tree_vector (vec);
24765 /* Save the arguments away. */
24766 TREE_VALUE (attribute) = arguments;
24768 else if (is_cilkplus_vector_p (identifier))
24770 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24771 continue;
24774 if (arguments != error_mark_node)
24776 /* Add this attribute to the list. */
24777 TREE_CHAIN (attribute) = attribute_list;
24778 attribute_list = attribute;
24781 token = cp_lexer_peek_token (parser->lexer);
24783 /* Now, look for more attributes. If the next token isn't a
24784 `,', we're done. */
24785 if (token->type != CPP_COMMA)
24786 break;
24788 /* Consume the comma and keep going. */
24789 cp_lexer_consume_token (parser->lexer);
24791 parser->translate_strings_p = save_translate_strings_p;
24793 /* We built up the list in reverse order. */
24794 return nreverse (attribute_list);
24797 /* Parse a standard C++11 attribute.
24799 The returned representation is a TREE_LIST which TREE_PURPOSE is
24800 the scoped name of the attribute, and the TREE_VALUE is its
24801 arguments list.
24803 Note that the scoped name of the attribute is itself a TREE_LIST
24804 which TREE_PURPOSE is the namespace of the attribute, and
24805 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24806 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24807 and which TREE_PURPOSE is directly the attribute name.
24809 Clients of the attribute code should use get_attribute_namespace
24810 and get_attribute_name to get the actual namespace and name of
24811 attributes, regardless of their being GNU or C++11 attributes.
24813 attribute:
24814 attribute-token attribute-argument-clause [opt]
24816 attribute-token:
24817 identifier
24818 attribute-scoped-token
24820 attribute-scoped-token:
24821 attribute-namespace :: identifier
24823 attribute-namespace:
24824 identifier
24826 attribute-argument-clause:
24827 ( balanced-token-seq )
24829 balanced-token-seq:
24830 balanced-token [opt]
24831 balanced-token-seq balanced-token
24833 balanced-token:
24834 ( balanced-token-seq )
24835 [ balanced-token-seq ]
24836 { balanced-token-seq }. */
24838 static tree
24839 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24841 tree attribute, attr_id = NULL_TREE, arguments;
24842 cp_token *token;
24844 /* First, parse name of the attribute, a.k.a attribute-token. */
24846 token = cp_lexer_peek_token (parser->lexer);
24847 if (token->type == CPP_NAME)
24848 attr_id = token->u.value;
24849 else if (token->type == CPP_KEYWORD)
24850 attr_id = ridpointers[(int) token->keyword];
24851 else if (token->flags & NAMED_OP)
24852 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24854 if (attr_id == NULL_TREE)
24855 return NULL_TREE;
24857 cp_lexer_consume_token (parser->lexer);
24859 token = cp_lexer_peek_token (parser->lexer);
24860 if (token->type == CPP_SCOPE)
24862 /* We are seeing a scoped attribute token. */
24864 cp_lexer_consume_token (parser->lexer);
24865 if (attr_ns)
24866 error_at (token->location, "attribute using prefix used together "
24867 "with scoped attribute token");
24868 attr_ns = attr_id;
24870 token = cp_lexer_consume_token (parser->lexer);
24871 if (token->type == CPP_NAME)
24872 attr_id = token->u.value;
24873 else if (token->type == CPP_KEYWORD)
24874 attr_id = ridpointers[(int) token->keyword];
24875 else if (token->flags & NAMED_OP)
24876 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24877 else
24879 error_at (token->location,
24880 "expected an identifier for the attribute name");
24881 return error_mark_node;
24883 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24884 NULL_TREE);
24885 token = cp_lexer_peek_token (parser->lexer);
24887 else if (attr_ns)
24888 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24889 NULL_TREE);
24890 else
24892 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24893 NULL_TREE);
24894 /* C++11 noreturn attribute is equivalent to GNU's. */
24895 if (is_attribute_p ("noreturn", attr_id))
24896 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24897 /* C++14 deprecated attribute is equivalent to GNU's. */
24898 else if (is_attribute_p ("deprecated", attr_id))
24899 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24900 /* C++17 fallthrough attribute is equivalent to GNU's. */
24901 else if (is_attribute_p ("fallthrough", attr_id))
24902 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24903 /* Transactional Memory TS optimize_for_synchronized attribute is
24904 equivalent to GNU transaction_callable. */
24905 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24906 TREE_PURPOSE (attribute)
24907 = get_identifier ("transaction_callable");
24908 /* Transactional Memory attributes are GNU attributes. */
24909 else if (tm_attr_to_mask (attr_id))
24910 TREE_PURPOSE (attribute) = attr_id;
24913 /* Now parse the optional argument clause of the attribute. */
24915 if (token->type != CPP_OPEN_PAREN)
24916 return attribute;
24919 vec<tree, va_gc> *vec;
24920 int attr_flag = normal_attr;
24922 if (attr_ns == get_identifier ("gnu")
24923 && attribute_takes_identifier_p (attr_id))
24924 /* A GNU attribute that takes an identifier in parameter. */
24925 attr_flag = id_attr;
24927 vec = cp_parser_parenthesized_expression_list
24928 (parser, attr_flag, /*cast_p=*/false,
24929 /*allow_expansion_p=*/true,
24930 /*non_constant_p=*/NULL);
24931 if (vec == NULL)
24932 arguments = error_mark_node;
24933 else
24935 arguments = build_tree_list_vec (vec);
24936 release_tree_vector (vec);
24939 if (arguments == error_mark_node)
24940 attribute = error_mark_node;
24941 else
24942 TREE_VALUE (attribute) = arguments;
24945 return attribute;
24948 /* Check that the attribute ATTRIBUTE appears at most once in the
24949 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24950 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24951 isn't implemented yet in GCC. */
24953 static void
24954 cp_parser_check_std_attribute (tree attributes, tree attribute)
24956 if (attributes)
24958 tree name = get_attribute_name (attribute);
24959 if (is_attribute_p ("noreturn", name)
24960 && lookup_attribute ("noreturn", attributes))
24961 error ("attribute %<noreturn%> can appear at most once "
24962 "in an attribute-list");
24963 else if (is_attribute_p ("deprecated", name)
24964 && lookup_attribute ("deprecated", attributes))
24965 error ("attribute %<deprecated%> can appear at most once "
24966 "in an attribute-list");
24970 /* Parse a list of standard C++-11 attributes.
24972 attribute-list:
24973 attribute [opt]
24974 attribute-list , attribute[opt]
24975 attribute ...
24976 attribute-list , attribute ...
24979 static tree
24980 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
24982 tree attributes = NULL_TREE, attribute = NULL_TREE;
24983 cp_token *token = NULL;
24985 while (true)
24987 attribute = cp_parser_std_attribute (parser, attr_ns);
24988 if (attribute == error_mark_node)
24989 break;
24990 if (attribute != NULL_TREE)
24992 cp_parser_check_std_attribute (attributes, attribute);
24993 TREE_CHAIN (attribute) = attributes;
24994 attributes = attribute;
24996 token = cp_lexer_peek_token (parser->lexer);
24997 if (token->type == CPP_ELLIPSIS)
24999 cp_lexer_consume_token (parser->lexer);
25000 if (attribute == NULL_TREE)
25001 error_at (token->location,
25002 "expected attribute before %<...%>");
25003 else
25005 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25006 if (pack == error_mark_node)
25007 return error_mark_node;
25008 TREE_VALUE (attribute) = pack;
25010 token = cp_lexer_peek_token (parser->lexer);
25012 if (token->type != CPP_COMMA)
25013 break;
25014 cp_lexer_consume_token (parser->lexer);
25016 attributes = nreverse (attributes);
25017 return attributes;
25020 /* Parse a standard C++-11 attribute specifier.
25022 attribute-specifier:
25023 [ [ attribute-using-prefix [opt] attribute-list ] ]
25024 alignment-specifier
25026 attribute-using-prefix:
25027 using attribute-namespace :
25029 alignment-specifier:
25030 alignas ( type-id ... [opt] )
25031 alignas ( alignment-expression ... [opt] ). */
25033 static tree
25034 cp_parser_std_attribute_spec (cp_parser *parser)
25036 tree attributes = NULL_TREE;
25037 cp_token *token = cp_lexer_peek_token (parser->lexer);
25039 if (token->type == CPP_OPEN_SQUARE
25040 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25042 tree attr_ns = NULL_TREE;
25044 cp_lexer_consume_token (parser->lexer);
25045 cp_lexer_consume_token (parser->lexer);
25047 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25049 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25050 if (token->type == CPP_NAME)
25051 attr_ns = token->u.value;
25052 else if (token->type == CPP_KEYWORD)
25053 attr_ns = ridpointers[(int) token->keyword];
25054 else if (token->flags & NAMED_OP)
25055 attr_ns = get_identifier (cpp_type2name (token->type,
25056 token->flags));
25057 if (attr_ns
25058 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25060 if (cxx_dialect < cxx1z
25061 && !in_system_header_at (input_location))
25062 pedwarn (input_location, 0,
25063 "attribute using prefix only available "
25064 "with -std=c++1z or -std=gnu++1z");
25066 cp_lexer_consume_token (parser->lexer);
25067 cp_lexer_consume_token (parser->lexer);
25068 cp_lexer_consume_token (parser->lexer);
25070 else
25071 attr_ns = NULL_TREE;
25074 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25076 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25077 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25078 cp_parser_skip_to_end_of_statement (parser);
25079 else
25080 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25081 when we are sure that we have actually parsed them. */
25082 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25084 else
25086 tree alignas_expr;
25088 /* Look for an alignment-specifier. */
25090 token = cp_lexer_peek_token (parser->lexer);
25092 if (token->type != CPP_KEYWORD
25093 || token->keyword != RID_ALIGNAS)
25094 return NULL_TREE;
25096 cp_lexer_consume_token (parser->lexer);
25097 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25099 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
25101 cp_parser_error (parser, "expected %<(%>");
25102 return error_mark_node;
25105 cp_parser_parse_tentatively (parser);
25106 alignas_expr = cp_parser_type_id (parser);
25108 if (!cp_parser_parse_definitely (parser))
25110 alignas_expr = cp_parser_assignment_expression (parser);
25111 if (alignas_expr == error_mark_node)
25112 cp_parser_skip_to_end_of_statement (parser);
25113 if (alignas_expr == NULL_TREE
25114 || alignas_expr == error_mark_node)
25115 return alignas_expr;
25118 alignas_expr = cxx_alignas_expr (alignas_expr);
25119 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25121 /* Handle alignas (pack...). */
25122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25124 cp_lexer_consume_token (parser->lexer);
25125 alignas_expr = make_pack_expansion (alignas_expr);
25128 /* Something went wrong, so don't build the attribute. */
25129 if (alignas_expr == error_mark_node)
25130 return error_mark_node;
25132 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25134 cp_parser_error (parser, "expected %<)%>");
25135 return error_mark_node;
25138 /* Build the C++-11 representation of an 'aligned'
25139 attribute. */
25140 attributes =
25141 build_tree_list (build_tree_list (get_identifier ("gnu"),
25142 get_identifier ("aligned")),
25143 alignas_expr);
25146 return attributes;
25149 /* Parse a standard C++-11 attribute-specifier-seq.
25151 attribute-specifier-seq:
25152 attribute-specifier-seq [opt] attribute-specifier
25155 static tree
25156 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25158 tree attr_specs = NULL_TREE;
25159 tree attr_last = NULL_TREE;
25161 while (true)
25163 tree attr_spec = cp_parser_std_attribute_spec (parser);
25164 if (attr_spec == NULL_TREE)
25165 break;
25166 if (attr_spec == error_mark_node)
25167 return error_mark_node;
25169 if (attr_last)
25170 TREE_CHAIN (attr_last) = attr_spec;
25171 else
25172 attr_specs = attr_last = attr_spec;
25173 attr_last = tree_last (attr_last);
25176 return attr_specs;
25179 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25180 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25181 current value of the PEDANTIC flag, regardless of whether or not
25182 the `__extension__' keyword is present. The caller is responsible
25183 for restoring the value of the PEDANTIC flag. */
25185 static bool
25186 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25188 /* Save the old value of the PEDANTIC flag. */
25189 *saved_pedantic = pedantic;
25191 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25193 /* Consume the `__extension__' token. */
25194 cp_lexer_consume_token (parser->lexer);
25195 /* We're not being pedantic while the `__extension__' keyword is
25196 in effect. */
25197 pedantic = 0;
25199 return true;
25202 return false;
25205 /* Parse a label declaration.
25207 label-declaration:
25208 __label__ label-declarator-seq ;
25210 label-declarator-seq:
25211 identifier , label-declarator-seq
25212 identifier */
25214 static void
25215 cp_parser_label_declaration (cp_parser* parser)
25217 /* Look for the `__label__' keyword. */
25218 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25220 while (true)
25222 tree identifier;
25224 /* Look for an identifier. */
25225 identifier = cp_parser_identifier (parser);
25226 /* If we failed, stop. */
25227 if (identifier == error_mark_node)
25228 break;
25229 /* Declare it as a label. */
25230 finish_label_decl (identifier);
25231 /* If the next token is a `;', stop. */
25232 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25233 break;
25234 /* Look for the `,' separating the label declarations. */
25235 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25238 /* Look for the final `;'. */
25239 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25242 // -------------------------------------------------------------------------- //
25243 // Requires Clause
25245 // Parse a requires clause.
25247 // requires-clause:
25248 // 'requires' logical-or-expression
25250 // The required logical-or-expression must be a constant expression. Note
25251 // that we don't check that the expression is constepxr here. We defer until
25252 // we analyze constraints and then, we only check atomic constraints.
25253 static tree
25254 cp_parser_requires_clause (cp_parser *parser)
25256 // Parse the requires clause so that it is not automatically folded.
25257 ++processing_template_decl;
25258 tree expr = cp_parser_binary_expression (parser, false, false,
25259 PREC_NOT_OPERATOR, NULL);
25260 if (check_for_bare_parameter_packs (expr))
25261 expr = error_mark_node;
25262 --processing_template_decl;
25263 return expr;
25266 // Optionally parse a requires clause:
25267 static tree
25268 cp_parser_requires_clause_opt (cp_parser *parser)
25270 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25271 if (tok->keyword != RID_REQUIRES)
25273 if (!flag_concepts && tok->type == CPP_NAME
25274 && tok->u.value == ridpointers[RID_REQUIRES])
25276 error_at (cp_lexer_peek_token (parser->lexer)->location,
25277 "%<requires%> only available with -fconcepts");
25278 /* Parse and discard the requires-clause. */
25279 cp_lexer_consume_token (parser->lexer);
25280 cp_parser_requires_clause (parser);
25282 return NULL_TREE;
25284 cp_lexer_consume_token (parser->lexer);
25285 return cp_parser_requires_clause (parser);
25289 /*---------------------------------------------------------------------------
25290 Requires expressions
25291 ---------------------------------------------------------------------------*/
25293 /* Parse a requires expression
25295 requirement-expression:
25296 'requires' requirement-parameter-list [opt] requirement-body */
25297 static tree
25298 cp_parser_requires_expression (cp_parser *parser)
25300 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25301 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25303 /* A requires-expression shall appear only within a concept
25304 definition or a requires-clause.
25306 TODO: Implement this diagnostic correctly. */
25307 if (!processing_template_decl)
25309 error_at (loc, "a requires expression cannot appear outside a template");
25310 cp_parser_skip_to_end_of_statement (parser);
25311 return error_mark_node;
25314 tree parms, reqs;
25316 /* Local parameters are delared as variables within the scope
25317 of the expression. They are not visible past the end of
25318 the expression. Expressions within the requires-expression
25319 are unevaluated. */
25320 struct scope_sentinel
25322 scope_sentinel ()
25324 ++cp_unevaluated_operand;
25325 begin_scope (sk_block, NULL_TREE);
25328 ~scope_sentinel ()
25330 pop_bindings_and_leave_scope ();
25331 --cp_unevaluated_operand;
25333 } s;
25335 /* Parse the optional parameter list. */
25336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25338 parms = cp_parser_requirement_parameter_list (parser);
25339 if (parms == error_mark_node)
25340 return error_mark_node;
25342 else
25343 parms = NULL_TREE;
25345 /* Parse the requirement body. */
25346 reqs = cp_parser_requirement_body (parser);
25347 if (reqs == error_mark_node)
25348 return error_mark_node;
25351 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25352 the parm chain. */
25353 grokparms (parms, &parms);
25354 return finish_requires_expr (parms, reqs);
25357 /* Parse a parameterized requirement.
25359 requirement-parameter-list:
25360 '(' parameter-declaration-clause ')' */
25361 static tree
25362 cp_parser_requirement_parameter_list (cp_parser *parser)
25364 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25365 return error_mark_node;
25367 tree parms = cp_parser_parameter_declaration_clause (parser);
25369 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25370 return error_mark_node;
25372 return parms;
25375 /* Parse the body of a requirement.
25377 requirement-body:
25378 '{' requirement-list '}' */
25379 static tree
25380 cp_parser_requirement_body (cp_parser *parser)
25382 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25383 return error_mark_node;
25385 tree reqs = cp_parser_requirement_list (parser);
25387 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25388 return error_mark_node;
25390 return reqs;
25393 /* Parse a list of requirements.
25395 requirement-list:
25396 requirement
25397 requirement-list ';' requirement[opt] */
25398 static tree
25399 cp_parser_requirement_list (cp_parser *parser)
25401 tree result = NULL_TREE;
25402 while (true)
25404 tree req = cp_parser_requirement (parser);
25405 if (req == error_mark_node)
25406 return error_mark_node;
25408 result = tree_cons (NULL_TREE, req, result);
25410 /* If we see a semi-colon, consume it. */
25411 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25412 cp_lexer_consume_token (parser->lexer);
25414 /* Stop processing at the end of the list. */
25415 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25416 break;
25419 /* Reverse the order of requirements so they are analyzed in
25420 declaration order. */
25421 return nreverse (result);
25424 /* Parse a syntactic requirement or type requirement.
25426 requirement:
25427 simple-requirement
25428 compound-requirement
25429 type-requirement
25430 nested-requirement */
25431 static tree
25432 cp_parser_requirement (cp_parser *parser)
25434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25435 return cp_parser_compound_requirement (parser);
25436 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25437 return cp_parser_type_requirement (parser);
25438 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25439 return cp_parser_nested_requirement (parser);
25440 else
25441 return cp_parser_simple_requirement (parser);
25444 /* Parse a simple requirement.
25446 simple-requirement:
25447 expression ';' */
25448 static tree
25449 cp_parser_simple_requirement (cp_parser *parser)
25451 tree expr = cp_parser_expression (parser, NULL, false, false);
25452 if (!expr || expr == error_mark_node)
25453 return error_mark_node;
25455 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25456 return error_mark_node;
25458 return finish_simple_requirement (expr);
25461 /* Parse a type requirement
25463 type-requirement
25464 nested-name-specifier [opt] required-type-name ';'
25466 required-type-name:
25467 type-name
25468 'template' [opt] simple-template-id */
25469 static tree
25470 cp_parser_type_requirement (cp_parser *parser)
25472 cp_lexer_consume_token (parser->lexer);
25474 // Save the scope before parsing name specifiers.
25475 tree saved_scope = parser->scope;
25476 tree saved_object_scope = parser->object_scope;
25477 tree saved_qualifying_scope = parser->qualifying_scope;
25478 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25479 cp_parser_nested_name_specifier_opt (parser,
25480 /*typename_keyword_p=*/true,
25481 /*check_dependency_p=*/false,
25482 /*type_p=*/true,
25483 /*is_declaration=*/false);
25485 tree type;
25486 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25488 cp_lexer_consume_token (parser->lexer);
25489 type = cp_parser_template_id (parser,
25490 /*template_keyword_p=*/true,
25491 /*check_dependency=*/false,
25492 /*tag_type=*/none_type,
25493 /*is_declaration=*/false);
25494 type = make_typename_type (parser->scope, type, typename_type,
25495 /*complain=*/tf_error);
25497 else
25498 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25500 if (TREE_CODE (type) == TYPE_DECL)
25501 type = TREE_TYPE (type);
25503 parser->scope = saved_scope;
25504 parser->object_scope = saved_object_scope;
25505 parser->qualifying_scope = saved_qualifying_scope;
25507 if (type == error_mark_node)
25508 cp_parser_skip_to_end_of_statement (parser);
25510 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25511 return error_mark_node;
25512 if (type == error_mark_node)
25513 return error_mark_node;
25515 return finish_type_requirement (type);
25518 /* Parse a compound requirement
25520 compound-requirement:
25521 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25522 static tree
25523 cp_parser_compound_requirement (cp_parser *parser)
25525 /* Parse an expression enclosed in '{ }'s. */
25526 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25527 return error_mark_node;
25529 tree expr = cp_parser_expression (parser, NULL, false, false);
25530 if (!expr || expr == error_mark_node)
25531 return error_mark_node;
25533 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25534 return error_mark_node;
25536 /* Parse the optional noexcept. */
25537 bool noexcept_p = false;
25538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25540 cp_lexer_consume_token (parser->lexer);
25541 noexcept_p = true;
25544 /* Parse the optional trailing return type. */
25545 tree type = NULL_TREE;
25546 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25548 cp_lexer_consume_token (parser->lexer);
25549 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25550 parser->in_result_type_constraint_p = true;
25551 type = cp_parser_trailing_type_id (parser);
25552 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25553 if (type == error_mark_node)
25554 return error_mark_node;
25557 return finish_compound_requirement (expr, type, noexcept_p);
25560 /* Parse a nested requirement. This is the same as a requires clause.
25562 nested-requirement:
25563 requires-clause */
25564 static tree
25565 cp_parser_nested_requirement (cp_parser *parser)
25567 cp_lexer_consume_token (parser->lexer);
25568 tree req = cp_parser_requires_clause (parser);
25569 if (req == error_mark_node)
25570 return error_mark_node;
25571 return finish_nested_requirement (req);
25574 /* Support Functions */
25576 /* Return the appropriate prefer_type argument for lookup_name_real based on
25577 tag_type and template_mem_access. */
25579 static inline int
25580 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25582 /* DR 141: When looking in the current enclosing context for a template-name
25583 after -> or ., only consider class templates. */
25584 if (template_mem_access)
25585 return 2;
25586 switch (tag_type)
25588 case none_type: return 0; // No preference.
25589 case scope_type: return 1; // Type or namespace.
25590 default: return 2; // Type only.
25594 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25595 NAME should have one of the representations used for an
25596 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25597 is returned. If PARSER->SCOPE is a dependent type, then a
25598 SCOPE_REF is returned.
25600 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25601 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25602 was formed. Abstractly, such entities should not be passed to this
25603 function, because they do not need to be looked up, but it is
25604 simpler to check for this special case here, rather than at the
25605 call-sites.
25607 In cases not explicitly covered above, this function returns a
25608 DECL, OVERLOAD, or baselink representing the result of the lookup.
25609 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25610 is returned.
25612 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25613 (e.g., "struct") that was used. In that case bindings that do not
25614 refer to types are ignored.
25616 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25617 ignored.
25619 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25620 are ignored.
25622 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25623 types.
25625 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25626 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25627 NULL_TREE otherwise. */
25629 static cp_expr
25630 cp_parser_lookup_name (cp_parser *parser, tree name,
25631 enum tag_types tag_type,
25632 bool is_template,
25633 bool is_namespace,
25634 bool check_dependency,
25635 tree *ambiguous_decls,
25636 location_t name_location)
25638 tree decl;
25639 tree object_type = parser->context->object_type;
25641 /* Assume that the lookup will be unambiguous. */
25642 if (ambiguous_decls)
25643 *ambiguous_decls = NULL_TREE;
25645 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25646 no longer valid. Note that if we are parsing tentatively, and
25647 the parse fails, OBJECT_TYPE will be automatically restored. */
25648 parser->context->object_type = NULL_TREE;
25650 if (name == error_mark_node)
25651 return error_mark_node;
25653 /* A template-id has already been resolved; there is no lookup to
25654 do. */
25655 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25656 return name;
25657 if (BASELINK_P (name))
25659 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25660 == TEMPLATE_ID_EXPR);
25661 return name;
25664 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25665 it should already have been checked to make sure that the name
25666 used matches the type being destroyed. */
25667 if (TREE_CODE (name) == BIT_NOT_EXPR)
25669 tree type;
25671 /* Figure out to which type this destructor applies. */
25672 if (parser->scope)
25673 type = parser->scope;
25674 else if (object_type)
25675 type = object_type;
25676 else
25677 type = current_class_type;
25678 /* If that's not a class type, there is no destructor. */
25679 if (!type || !CLASS_TYPE_P (type))
25680 return error_mark_node;
25681 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25682 lazily_declare_fn (sfk_destructor, type);
25683 if (!CLASSTYPE_DESTRUCTORS (type))
25684 return error_mark_node;
25685 /* If it was a class type, return the destructor. */
25686 return CLASSTYPE_DESTRUCTORS (type);
25689 /* By this point, the NAME should be an ordinary identifier. If
25690 the id-expression was a qualified name, the qualifying scope is
25691 stored in PARSER->SCOPE at this point. */
25692 gcc_assert (identifier_p (name));
25694 /* Perform the lookup. */
25695 if (parser->scope)
25697 bool dependent_p;
25699 if (parser->scope == error_mark_node)
25700 return error_mark_node;
25702 /* If the SCOPE is dependent, the lookup must be deferred until
25703 the template is instantiated -- unless we are explicitly
25704 looking up names in uninstantiated templates. Even then, we
25705 cannot look up the name if the scope is not a class type; it
25706 might, for example, be a template type parameter. */
25707 dependent_p = (TYPE_P (parser->scope)
25708 && dependent_scope_p (parser->scope));
25709 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25710 && dependent_p)
25711 /* Defer lookup. */
25712 decl = error_mark_node;
25713 else
25715 tree pushed_scope = NULL_TREE;
25717 /* If PARSER->SCOPE is a dependent type, then it must be a
25718 class type, and we must not be checking dependencies;
25719 otherwise, we would have processed this lookup above. So
25720 that PARSER->SCOPE is not considered a dependent base by
25721 lookup_member, we must enter the scope here. */
25722 if (dependent_p)
25723 pushed_scope = push_scope (parser->scope);
25725 /* If the PARSER->SCOPE is a template specialization, it
25726 may be instantiated during name lookup. In that case,
25727 errors may be issued. Even if we rollback the current
25728 tentative parse, those errors are valid. */
25729 decl = lookup_qualified_name (parser->scope, name,
25730 prefer_type_arg (tag_type),
25731 /*complain=*/true);
25733 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25734 lookup result and the nested-name-specifier nominates a class C:
25735 * if the name specified after the nested-name-specifier, when
25736 looked up in C, is the injected-class-name of C (Clause 9), or
25737 * if the name specified after the nested-name-specifier is the
25738 same as the identifier or the simple-template-id's template-
25739 name in the last component of the nested-name-specifier,
25740 the name is instead considered to name the constructor of
25741 class C. [ Note: for example, the constructor is not an
25742 acceptable lookup result in an elaborated-type-specifier so
25743 the constructor would not be used in place of the
25744 injected-class-name. --end note ] Such a constructor name
25745 shall be used only in the declarator-id of a declaration that
25746 names a constructor or in a using-declaration. */
25747 if (tag_type == none_type
25748 && DECL_SELF_REFERENCE_P (decl)
25749 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25750 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25751 prefer_type_arg (tag_type),
25752 /*complain=*/true);
25754 /* If we have a single function from a using decl, pull it out. */
25755 if (TREE_CODE (decl) == OVERLOAD
25756 && !really_overloaded_fn (decl))
25757 decl = OVL_FUNCTION (decl);
25759 if (pushed_scope)
25760 pop_scope (pushed_scope);
25763 /* If the scope is a dependent type and either we deferred lookup or
25764 we did lookup but didn't find the name, rememeber the name. */
25765 if (decl == error_mark_node && TYPE_P (parser->scope)
25766 && dependent_type_p (parser->scope))
25768 if (tag_type)
25770 tree type;
25772 /* The resolution to Core Issue 180 says that `struct
25773 A::B' should be considered a type-name, even if `A'
25774 is dependent. */
25775 type = make_typename_type (parser->scope, name, tag_type,
25776 /*complain=*/tf_error);
25777 if (type != error_mark_node)
25778 decl = TYPE_NAME (type);
25780 else if (is_template
25781 && (cp_parser_next_token_ends_template_argument_p (parser)
25782 || cp_lexer_next_token_is (parser->lexer,
25783 CPP_CLOSE_PAREN)))
25784 decl = make_unbound_class_template (parser->scope,
25785 name, NULL_TREE,
25786 /*complain=*/tf_error);
25787 else
25788 decl = build_qualified_name (/*type=*/NULL_TREE,
25789 parser->scope, name,
25790 is_template);
25792 parser->qualifying_scope = parser->scope;
25793 parser->object_scope = NULL_TREE;
25795 else if (object_type)
25797 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25798 OBJECT_TYPE is not a class. */
25799 if (CLASS_TYPE_P (object_type))
25800 /* If the OBJECT_TYPE is a template specialization, it may
25801 be instantiated during name lookup. In that case, errors
25802 may be issued. Even if we rollback the current tentative
25803 parse, those errors are valid. */
25804 decl = lookup_member (object_type,
25805 name,
25806 /*protect=*/0,
25807 prefer_type_arg (tag_type),
25808 tf_warning_or_error);
25809 else
25810 decl = NULL_TREE;
25812 if (!decl)
25813 /* Look it up in the enclosing context. DR 141: When looking for a
25814 template-name after -> or ., only consider class templates. */
25815 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25816 /*nonclass=*/0,
25817 /*block_p=*/true, is_namespace, 0);
25818 if (object_type == unknown_type_node)
25819 /* The object is type-dependent, so we can't look anything up; we used
25820 this to get the DR 141 behavior. */
25821 object_type = NULL_TREE;
25822 parser->object_scope = object_type;
25823 parser->qualifying_scope = NULL_TREE;
25825 else
25827 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25828 /*nonclass=*/0,
25829 /*block_p=*/true, is_namespace, 0);
25830 parser->qualifying_scope = NULL_TREE;
25831 parser->object_scope = NULL_TREE;
25834 /* If the lookup failed, let our caller know. */
25835 if (!decl || decl == error_mark_node)
25836 return error_mark_node;
25838 /* Pull out the template from an injected-class-name (or multiple). */
25839 if (is_template)
25840 decl = maybe_get_template_decl_from_type_decl (decl);
25842 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25843 if (TREE_CODE (decl) == TREE_LIST)
25845 if (ambiguous_decls)
25846 *ambiguous_decls = decl;
25847 /* The error message we have to print is too complicated for
25848 cp_parser_error, so we incorporate its actions directly. */
25849 if (!cp_parser_simulate_error (parser))
25851 error_at (name_location, "reference to %qD is ambiguous",
25852 name);
25853 print_candidates (decl);
25855 return error_mark_node;
25858 gcc_assert (DECL_P (decl)
25859 || TREE_CODE (decl) == OVERLOAD
25860 || TREE_CODE (decl) == SCOPE_REF
25861 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25862 || BASELINK_P (decl));
25864 /* If we have resolved the name of a member declaration, check to
25865 see if the declaration is accessible. When the name resolves to
25866 set of overloaded functions, accessibility is checked when
25867 overload resolution is done.
25869 During an explicit instantiation, access is not checked at all,
25870 as per [temp.explicit]. */
25871 if (DECL_P (decl))
25872 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25874 maybe_record_typedef_use (decl);
25876 return cp_expr (decl, name_location);
25879 /* Like cp_parser_lookup_name, but for use in the typical case where
25880 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25881 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25883 static tree
25884 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25886 return cp_parser_lookup_name (parser, name,
25887 none_type,
25888 /*is_template=*/false,
25889 /*is_namespace=*/false,
25890 /*check_dependency=*/true,
25891 /*ambiguous_decls=*/NULL,
25892 location);
25895 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25896 the current context, return the TYPE_DECL. If TAG_NAME_P is
25897 true, the DECL indicates the class being defined in a class-head,
25898 or declared in an elaborated-type-specifier.
25900 Otherwise, return DECL. */
25902 static tree
25903 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25905 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25906 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25908 struct A {
25909 template <typename T> struct B;
25912 template <typename T> struct A::B {};
25914 Similarly, in an elaborated-type-specifier:
25916 namespace N { struct X{}; }
25918 struct A {
25919 template <typename T> friend struct N::X;
25922 However, if the DECL refers to a class type, and we are in
25923 the scope of the class, then the name lookup automatically
25924 finds the TYPE_DECL created by build_self_reference rather
25925 than a TEMPLATE_DECL. For example, in:
25927 template <class T> struct S {
25928 S s;
25931 there is no need to handle such case. */
25933 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25934 return DECL_TEMPLATE_RESULT (decl);
25936 return decl;
25939 /* If too many, or too few, template-parameter lists apply to the
25940 declarator, issue an error message. Returns TRUE if all went well,
25941 and FALSE otherwise. */
25943 static bool
25944 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25945 cp_declarator *declarator,
25946 location_t declarator_location)
25948 switch (declarator->kind)
25950 case cdk_id:
25952 unsigned num_templates = 0;
25953 tree scope = declarator->u.id.qualifying_scope;
25955 if (scope)
25956 num_templates = num_template_headers_for_class (scope);
25957 else if (TREE_CODE (declarator->u.id.unqualified_name)
25958 == TEMPLATE_ID_EXPR)
25959 /* If the DECLARATOR has the form `X<y>' then it uses one
25960 additional level of template parameters. */
25961 ++num_templates;
25963 return cp_parser_check_template_parameters
25964 (parser, num_templates, declarator_location, declarator);
25967 case cdk_function:
25968 case cdk_array:
25969 case cdk_pointer:
25970 case cdk_reference:
25971 case cdk_ptrmem:
25972 return (cp_parser_check_declarator_template_parameters
25973 (parser, declarator->declarator, declarator_location));
25975 case cdk_decomp:
25976 case cdk_error:
25977 return true;
25979 default:
25980 gcc_unreachable ();
25982 return false;
25985 /* NUM_TEMPLATES were used in the current declaration. If that is
25986 invalid, return FALSE and issue an error messages. Otherwise,
25987 return TRUE. If DECLARATOR is non-NULL, then we are checking a
25988 declarator and we can print more accurate diagnostics. */
25990 static bool
25991 cp_parser_check_template_parameters (cp_parser* parser,
25992 unsigned num_templates,
25993 location_t location,
25994 cp_declarator *declarator)
25996 /* If there are the same number of template classes and parameter
25997 lists, that's OK. */
25998 if (parser->num_template_parameter_lists == num_templates)
25999 return true;
26000 /* If there are more, but only one more, then we are referring to a
26001 member template. That's OK too. */
26002 if (parser->num_template_parameter_lists == num_templates + 1)
26003 return true;
26004 /* If there are more template classes than parameter lists, we have
26005 something like:
26007 template <class T> void S<T>::R<T>::f (); */
26008 if (parser->num_template_parameter_lists < num_templates)
26010 if (declarator && !current_function_decl)
26011 error_at (location, "specializing member %<%T::%E%> "
26012 "requires %<template<>%> syntax",
26013 declarator->u.id.qualifying_scope,
26014 declarator->u.id.unqualified_name);
26015 else if (declarator)
26016 error_at (location, "invalid declaration of %<%T::%E%>",
26017 declarator->u.id.qualifying_scope,
26018 declarator->u.id.unqualified_name);
26019 else
26020 error_at (location, "too few template-parameter-lists");
26021 return false;
26023 /* Otherwise, there are too many template parameter lists. We have
26024 something like:
26026 template <class T> template <class U> void S::f(); */
26027 error_at (location, "too many template-parameter-lists");
26028 return false;
26031 /* Parse an optional `::' token indicating that the following name is
26032 from the global namespace. If so, PARSER->SCOPE is set to the
26033 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26034 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26035 Returns the new value of PARSER->SCOPE, if the `::' token is
26036 present, and NULL_TREE otherwise. */
26038 static tree
26039 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26041 cp_token *token;
26043 /* Peek at the next token. */
26044 token = cp_lexer_peek_token (parser->lexer);
26045 /* If we're looking at a `::' token then we're starting from the
26046 global namespace, not our current location. */
26047 if (token->type == CPP_SCOPE)
26049 /* Consume the `::' token. */
26050 cp_lexer_consume_token (parser->lexer);
26051 /* Set the SCOPE so that we know where to start the lookup. */
26052 parser->scope = global_namespace;
26053 parser->qualifying_scope = global_namespace;
26054 parser->object_scope = NULL_TREE;
26056 return parser->scope;
26058 else if (!current_scope_valid_p)
26060 parser->scope = NULL_TREE;
26061 parser->qualifying_scope = NULL_TREE;
26062 parser->object_scope = NULL_TREE;
26065 return NULL_TREE;
26068 /* Returns TRUE if the upcoming token sequence is the start of a
26069 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26070 declarator is preceded by the `friend' specifier. */
26072 static bool
26073 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26075 bool constructor_p;
26076 bool outside_class_specifier_p;
26077 tree nested_name_specifier;
26078 cp_token *next_token;
26080 /* The common case is that this is not a constructor declarator, so
26081 try to avoid doing lots of work if at all possible. It's not
26082 valid declare a constructor at function scope. */
26083 if (parser->in_function_body)
26084 return false;
26085 /* And only certain tokens can begin a constructor declarator. */
26086 next_token = cp_lexer_peek_token (parser->lexer);
26087 if (next_token->type != CPP_NAME
26088 && next_token->type != CPP_SCOPE
26089 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26090 && next_token->type != CPP_TEMPLATE_ID)
26091 return false;
26093 /* Parse tentatively; we are going to roll back all of the tokens
26094 consumed here. */
26095 cp_parser_parse_tentatively (parser);
26096 /* Assume that we are looking at a constructor declarator. */
26097 constructor_p = true;
26099 /* Look for the optional `::' operator. */
26100 cp_parser_global_scope_opt (parser,
26101 /*current_scope_valid_p=*/false);
26102 /* Look for the nested-name-specifier. */
26103 nested_name_specifier
26104 = (cp_parser_nested_name_specifier_opt (parser,
26105 /*typename_keyword_p=*/false,
26106 /*check_dependency_p=*/false,
26107 /*type_p=*/false,
26108 /*is_declaration=*/false));
26110 outside_class_specifier_p = (!at_class_scope_p ()
26111 || !TYPE_BEING_DEFINED (current_class_type)
26112 || friend_p);
26114 /* Outside of a class-specifier, there must be a
26115 nested-name-specifier. Except in C++17 mode, where we
26116 might be declaring a guiding declaration. */
26117 if (!nested_name_specifier && outside_class_specifier_p
26118 && cxx_dialect < cxx1z)
26119 constructor_p = false;
26120 else if (nested_name_specifier == error_mark_node)
26121 constructor_p = false;
26123 /* If we have a class scope, this is easy; DR 147 says that S::S always
26124 names the constructor, and no other qualified name could. */
26125 if (constructor_p && nested_name_specifier
26126 && CLASS_TYPE_P (nested_name_specifier))
26128 tree id = cp_parser_unqualified_id (parser,
26129 /*template_keyword_p=*/false,
26130 /*check_dependency_p=*/false,
26131 /*declarator_p=*/true,
26132 /*optional_p=*/false);
26133 if (is_overloaded_fn (id))
26134 id = DECL_NAME (get_first_fn (id));
26135 if (!constructor_name_p (id, nested_name_specifier))
26136 constructor_p = false;
26138 /* If we still think that this might be a constructor-declarator,
26139 look for a class-name. */
26140 else if (constructor_p)
26142 /* If we have:
26144 template <typename T> struct S {
26145 S();
26148 we must recognize that the nested `S' names a class. */
26149 if (cxx_dialect >= cxx1z)
26150 cp_parser_parse_tentatively (parser);
26152 tree type_decl;
26153 type_decl = cp_parser_class_name (parser,
26154 /*typename_keyword_p=*/false,
26155 /*template_keyword_p=*/false,
26156 none_type,
26157 /*check_dependency_p=*/false,
26158 /*class_head_p=*/false,
26159 /*is_declaration=*/false);
26161 if (cxx_dialect >= cxx1z
26162 && !cp_parser_parse_definitely (parser))
26164 type_decl = NULL_TREE;
26165 tree tmpl = cp_parser_template_name (parser,
26166 /*template_keyword*/false,
26167 /*check_dependency_p*/false,
26168 /*is_declaration*/false,
26169 none_type,
26170 /*is_identifier*/NULL);
26171 if (DECL_CLASS_TEMPLATE_P (tmpl)
26172 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26173 /* It's a deduction guide, return true. */;
26174 else
26175 cp_parser_simulate_error (parser);
26178 /* If there was no class-name, then this is not a constructor.
26179 Otherwise, if we are in a class-specifier and we aren't
26180 handling a friend declaration, check that its type matches
26181 current_class_type (c++/38313). Note: error_mark_node
26182 is left alone for error recovery purposes. */
26183 constructor_p = (!cp_parser_error_occurred (parser)
26184 && (outside_class_specifier_p
26185 || type_decl == NULL_TREE
26186 || type_decl == error_mark_node
26187 || same_type_p (current_class_type,
26188 TREE_TYPE (type_decl))));
26190 /* If we're still considering a constructor, we have to see a `(',
26191 to begin the parameter-declaration-clause, followed by either a
26192 `)', an `...', or a decl-specifier. We need to check for a
26193 type-specifier to avoid being fooled into thinking that:
26195 S (f) (int);
26197 is a constructor. (It is actually a function named `f' that
26198 takes one parameter (of type `int') and returns a value of type
26199 `S'. */
26200 if (constructor_p
26201 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26202 constructor_p = false;
26204 if (constructor_p
26205 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26206 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26207 /* A parameter declaration begins with a decl-specifier,
26208 which is either the "attribute" keyword, a storage class
26209 specifier, or (usually) a type-specifier. */
26210 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26212 tree type;
26213 tree pushed_scope = NULL_TREE;
26214 unsigned saved_num_template_parameter_lists;
26216 /* Names appearing in the type-specifier should be looked up
26217 in the scope of the class. */
26218 if (current_class_type)
26219 type = NULL_TREE;
26220 else if (type_decl)
26222 type = TREE_TYPE (type_decl);
26223 if (TREE_CODE (type) == TYPENAME_TYPE)
26225 type = resolve_typename_type (type,
26226 /*only_current_p=*/false);
26227 if (TREE_CODE (type) == TYPENAME_TYPE)
26229 cp_parser_abort_tentative_parse (parser);
26230 return false;
26233 pushed_scope = push_scope (type);
26236 /* Inside the constructor parameter list, surrounding
26237 template-parameter-lists do not apply. */
26238 saved_num_template_parameter_lists
26239 = parser->num_template_parameter_lists;
26240 parser->num_template_parameter_lists = 0;
26242 /* Look for the type-specifier. */
26243 cp_parser_type_specifier (parser,
26244 CP_PARSER_FLAGS_NONE,
26245 /*decl_specs=*/NULL,
26246 /*is_declarator=*/true,
26247 /*declares_class_or_enum=*/NULL,
26248 /*is_cv_qualifier=*/NULL);
26250 parser->num_template_parameter_lists
26251 = saved_num_template_parameter_lists;
26253 /* Leave the scope of the class. */
26254 if (pushed_scope)
26255 pop_scope (pushed_scope);
26257 constructor_p = !cp_parser_error_occurred (parser);
26261 /* We did not really want to consume any tokens. */
26262 cp_parser_abort_tentative_parse (parser);
26264 return constructor_p;
26267 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26268 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26269 they must be performed once we are in the scope of the function.
26271 Returns the function defined. */
26273 static tree
26274 cp_parser_function_definition_from_specifiers_and_declarator
26275 (cp_parser* parser,
26276 cp_decl_specifier_seq *decl_specifiers,
26277 tree attributes,
26278 const cp_declarator *declarator)
26280 tree fn;
26281 bool success_p;
26283 /* Begin the function-definition. */
26284 success_p = start_function (decl_specifiers, declarator, attributes);
26286 /* The things we're about to see are not directly qualified by any
26287 template headers we've seen thus far. */
26288 reset_specialization ();
26290 /* If there were names looked up in the decl-specifier-seq that we
26291 did not check, check them now. We must wait until we are in the
26292 scope of the function to perform the checks, since the function
26293 might be a friend. */
26294 perform_deferred_access_checks (tf_warning_or_error);
26296 if (success_p)
26298 cp_finalize_omp_declare_simd (parser, current_function_decl);
26299 parser->omp_declare_simd = NULL;
26300 cp_finalize_oacc_routine (parser, current_function_decl, true);
26301 parser->oacc_routine = NULL;
26304 if (!success_p)
26306 /* Skip the entire function. */
26307 cp_parser_skip_to_end_of_block_or_statement (parser);
26308 fn = error_mark_node;
26310 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26312 /* Seen already, skip it. An error message has already been output. */
26313 cp_parser_skip_to_end_of_block_or_statement (parser);
26314 fn = current_function_decl;
26315 current_function_decl = NULL_TREE;
26316 /* If this is a function from a class, pop the nested class. */
26317 if (current_class_name)
26318 pop_nested_class ();
26320 else
26322 timevar_id_t tv;
26323 if (DECL_DECLARED_INLINE_P (current_function_decl))
26324 tv = TV_PARSE_INLINE;
26325 else
26326 tv = TV_PARSE_FUNC;
26327 timevar_push (tv);
26328 fn = cp_parser_function_definition_after_declarator (parser,
26329 /*inline_p=*/false);
26330 timevar_pop (tv);
26333 return fn;
26336 /* Parse the part of a function-definition that follows the
26337 declarator. INLINE_P is TRUE iff this function is an inline
26338 function defined within a class-specifier.
26340 Returns the function defined. */
26342 static tree
26343 cp_parser_function_definition_after_declarator (cp_parser* parser,
26344 bool inline_p)
26346 tree fn;
26347 bool ctor_initializer_p = false;
26348 bool saved_in_unbraced_linkage_specification_p;
26349 bool saved_in_function_body;
26350 unsigned saved_num_template_parameter_lists;
26351 cp_token *token;
26352 bool fully_implicit_function_template_p
26353 = parser->fully_implicit_function_template_p;
26354 parser->fully_implicit_function_template_p = false;
26355 tree implicit_template_parms
26356 = parser->implicit_template_parms;
26357 parser->implicit_template_parms = 0;
26358 cp_binding_level* implicit_template_scope
26359 = parser->implicit_template_scope;
26360 parser->implicit_template_scope = 0;
26362 saved_in_function_body = parser->in_function_body;
26363 parser->in_function_body = true;
26364 /* If the next token is `return', then the code may be trying to
26365 make use of the "named return value" extension that G++ used to
26366 support. */
26367 token = cp_lexer_peek_token (parser->lexer);
26368 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26370 /* Consume the `return' keyword. */
26371 cp_lexer_consume_token (parser->lexer);
26372 /* Look for the identifier that indicates what value is to be
26373 returned. */
26374 cp_parser_identifier (parser);
26375 /* Issue an error message. */
26376 error_at (token->location,
26377 "named return values are no longer supported");
26378 /* Skip tokens until we reach the start of the function body. */
26379 while (true)
26381 cp_token *token = cp_lexer_peek_token (parser->lexer);
26382 if (token->type == CPP_OPEN_BRACE
26383 || token->type == CPP_EOF
26384 || token->type == CPP_PRAGMA_EOL)
26385 break;
26386 cp_lexer_consume_token (parser->lexer);
26389 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26390 anything declared inside `f'. */
26391 saved_in_unbraced_linkage_specification_p
26392 = parser->in_unbraced_linkage_specification_p;
26393 parser->in_unbraced_linkage_specification_p = false;
26394 /* Inside the function, surrounding template-parameter-lists do not
26395 apply. */
26396 saved_num_template_parameter_lists
26397 = parser->num_template_parameter_lists;
26398 parser->num_template_parameter_lists = 0;
26400 start_lambda_scope (current_function_decl);
26402 /* If the next token is `try', `__transaction_atomic', or
26403 `__transaction_relaxed`, then we are looking at either function-try-block
26404 or function-transaction-block. Note that all of these include the
26405 function-body. */
26406 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26407 ctor_initializer_p = cp_parser_function_transaction (parser,
26408 RID_TRANSACTION_ATOMIC);
26409 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26410 RID_TRANSACTION_RELAXED))
26411 ctor_initializer_p = cp_parser_function_transaction (parser,
26412 RID_TRANSACTION_RELAXED);
26413 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26414 ctor_initializer_p = cp_parser_function_try_block (parser);
26415 else
26416 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26417 (parser, /*in_function_try_block=*/false);
26419 finish_lambda_scope ();
26421 /* Finish the function. */
26422 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26423 (inline_p ? 2 : 0));
26424 /* Generate code for it, if necessary. */
26425 expand_or_defer_fn (fn);
26426 /* Restore the saved values. */
26427 parser->in_unbraced_linkage_specification_p
26428 = saved_in_unbraced_linkage_specification_p;
26429 parser->num_template_parameter_lists
26430 = saved_num_template_parameter_lists;
26431 parser->in_function_body = saved_in_function_body;
26433 parser->fully_implicit_function_template_p
26434 = fully_implicit_function_template_p;
26435 parser->implicit_template_parms
26436 = implicit_template_parms;
26437 parser->implicit_template_scope
26438 = implicit_template_scope;
26440 if (parser->fully_implicit_function_template_p)
26441 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26443 return fn;
26446 /* Parse a template-declaration body (following argument list). */
26448 static void
26449 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26450 tree parameter_list,
26451 bool member_p)
26453 tree decl = NULL_TREE;
26454 bool friend_p = false;
26456 /* We just processed one more parameter list. */
26457 ++parser->num_template_parameter_lists;
26459 /* Get the deferred access checks from the parameter list. These
26460 will be checked once we know what is being declared, as for a
26461 member template the checks must be performed in the scope of the
26462 class containing the member. */
26463 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26465 /* Tentatively parse for a new template parameter list, which can either be
26466 the template keyword or a template introduction. */
26467 if (cp_parser_template_declaration_after_export (parser, member_p))
26468 /* OK */;
26469 else if (cxx_dialect >= cxx11
26470 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26471 decl = cp_parser_alias_declaration (parser);
26472 else
26474 /* There are no access checks when parsing a template, as we do not
26475 know if a specialization will be a friend. */
26476 push_deferring_access_checks (dk_no_check);
26477 cp_token *token = cp_lexer_peek_token (parser->lexer);
26478 decl = cp_parser_single_declaration (parser,
26479 checks,
26480 member_p,
26481 /*explicit_specialization_p=*/false,
26482 &friend_p);
26483 pop_deferring_access_checks ();
26485 /* If this is a member template declaration, let the front
26486 end know. */
26487 if (member_p && !friend_p && decl)
26489 if (TREE_CODE (decl) == TYPE_DECL)
26490 cp_parser_check_access_in_redeclaration (decl, token->location);
26492 decl = finish_member_template_decl (decl);
26494 else if (friend_p && decl
26495 && DECL_DECLARES_TYPE_P (decl))
26496 make_friend_class (current_class_type, TREE_TYPE (decl),
26497 /*complain=*/true);
26499 /* We are done with the current parameter list. */
26500 --parser->num_template_parameter_lists;
26502 pop_deferring_access_checks ();
26504 /* Finish up. */
26505 finish_template_decl (parameter_list);
26507 /* Check the template arguments for a literal operator template. */
26508 if (decl
26509 && DECL_DECLARES_FUNCTION_P (decl)
26510 && UDLIT_OPER_P (DECL_NAME (decl)))
26512 bool ok = true;
26513 if (parameter_list == NULL_TREE)
26514 ok = false;
26515 else
26517 int num_parms = TREE_VEC_LENGTH (parameter_list);
26518 if (num_parms == 1)
26520 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26521 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26522 if (TREE_TYPE (parm) != char_type_node
26523 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26524 ok = false;
26526 else if (num_parms == 2 && cxx_dialect >= cxx14)
26528 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26529 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26530 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26531 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26532 if (parm == error_mark_node
26533 || TREE_TYPE (parm) != TREE_TYPE (type)
26534 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26535 ok = false;
26537 else
26538 ok = false;
26540 if (!ok)
26542 if (cxx_dialect >= cxx14)
26543 error ("literal operator template %qD has invalid parameter list."
26544 " Expected non-type template argument pack <char...>"
26545 " or <typename CharT, CharT...>",
26546 decl);
26547 else
26548 error ("literal operator template %qD has invalid parameter list."
26549 " Expected non-type template argument pack <char...>",
26550 decl);
26554 /* Register member declarations. */
26555 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26556 finish_member_declaration (decl);
26557 /* If DECL is a function template, we must return to parse it later.
26558 (Even though there is no definition, there might be default
26559 arguments that need handling.) */
26560 if (member_p && decl
26561 && DECL_DECLARES_FUNCTION_P (decl))
26562 vec_safe_push (unparsed_funs_with_definitions, decl);
26565 /* Parse a template introduction header for a template-declaration. Returns
26566 false if tentative parse fails. */
26568 static bool
26569 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26571 cp_parser_parse_tentatively (parser);
26573 tree saved_scope = parser->scope;
26574 tree saved_object_scope = parser->object_scope;
26575 tree saved_qualifying_scope = parser->qualifying_scope;
26577 /* Look for the optional `::' operator. */
26578 cp_parser_global_scope_opt (parser,
26579 /*current_scope_valid_p=*/false);
26580 /* Look for the nested-name-specifier. */
26581 cp_parser_nested_name_specifier_opt (parser,
26582 /*typename_keyword_p=*/false,
26583 /*check_dependency_p=*/true,
26584 /*type_p=*/false,
26585 /*is_declaration=*/false);
26587 cp_token *token = cp_lexer_peek_token (parser->lexer);
26588 tree concept_name = cp_parser_identifier (parser);
26590 /* Look up the concept for which we will be matching
26591 template parameters. */
26592 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26593 token->location);
26594 parser->scope = saved_scope;
26595 parser->object_scope = saved_object_scope;
26596 parser->qualifying_scope = saved_qualifying_scope;
26598 if (concept_name == error_mark_node)
26599 cp_parser_simulate_error (parser);
26601 /* Look for opening brace for introduction. */
26602 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26604 if (!cp_parser_parse_definitely (parser))
26605 return false;
26607 push_deferring_access_checks (dk_deferred);
26609 /* Build vector of placeholder parameters and grab
26610 matching identifiers. */
26611 tree introduction_list = cp_parser_introduction_list (parser);
26613 /* The introduction-list shall not be empty. */
26614 int nargs = TREE_VEC_LENGTH (introduction_list);
26615 if (nargs == 0)
26617 error ("empty introduction-list");
26618 return true;
26621 /* Look for closing brace for introduction. */
26622 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26623 return true;
26625 if (tmpl_decl == error_mark_node)
26627 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26628 token->location);
26629 return true;
26632 /* Build and associate the constraint. */
26633 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26634 if (parms && parms != error_mark_node)
26636 cp_parser_template_declaration_after_parameters (parser, parms,
26637 member_p);
26638 return true;
26641 error_at (token->location, "no matching concept for template-introduction");
26642 return true;
26645 /* Parse a normal template-declaration following the template keyword. */
26647 static void
26648 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26650 tree parameter_list;
26651 bool need_lang_pop;
26652 location_t location = input_location;
26654 /* Look for the `<' token. */
26655 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26656 return;
26657 if (at_class_scope_p () && current_function_decl)
26659 /* 14.5.2.2 [temp.mem]
26661 A local class shall not have member templates. */
26662 error_at (location,
26663 "invalid declaration of member template in local class");
26664 cp_parser_skip_to_end_of_block_or_statement (parser);
26665 return;
26667 /* [temp]
26669 A template ... shall not have C linkage. */
26670 if (current_lang_name == lang_name_c)
26672 error_at (location, "template with C linkage");
26673 /* Give it C++ linkage to avoid confusing other parts of the
26674 front end. */
26675 push_lang_context (lang_name_cplusplus);
26676 need_lang_pop = true;
26678 else
26679 need_lang_pop = false;
26681 /* We cannot perform access checks on the template parameter
26682 declarations until we know what is being declared, just as we
26683 cannot check the decl-specifier list. */
26684 push_deferring_access_checks (dk_deferred);
26686 /* If the next token is `>', then we have an invalid
26687 specialization. Rather than complain about an invalid template
26688 parameter, issue an error message here. */
26689 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26691 cp_parser_error (parser, "invalid explicit specialization");
26692 begin_specialization ();
26693 parameter_list = NULL_TREE;
26695 else
26697 /* Parse the template parameters. */
26698 parameter_list = cp_parser_template_parameter_list (parser);
26701 /* Look for the `>'. */
26702 cp_parser_skip_to_end_of_template_parameter_list (parser);
26704 /* Manage template requirements */
26705 if (flag_concepts)
26707 tree reqs = get_shorthand_constraints (current_template_parms);
26708 if (tree r = cp_parser_requires_clause_opt (parser))
26709 reqs = conjoin_constraints (reqs, normalize_expression (r));
26710 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26713 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26714 member_p);
26716 /* For the erroneous case of a template with C linkage, we pushed an
26717 implicit C++ linkage scope; exit that scope now. */
26718 if (need_lang_pop)
26719 pop_lang_context ();
26722 /* Parse a template-declaration, assuming that the `export' (and
26723 `extern') keywords, if present, has already been scanned. MEMBER_P
26724 is as for cp_parser_template_declaration. */
26726 static bool
26727 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26731 cp_lexer_consume_token (parser->lexer);
26732 cp_parser_explicit_template_declaration (parser, member_p);
26733 return true;
26735 else if (flag_concepts)
26736 return cp_parser_template_introduction (parser, member_p);
26738 return false;
26741 /* Perform the deferred access checks from a template-parameter-list.
26742 CHECKS is a TREE_LIST of access checks, as returned by
26743 get_deferred_access_checks. */
26745 static void
26746 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26748 ++processing_template_parmlist;
26749 perform_access_checks (checks, tf_warning_or_error);
26750 --processing_template_parmlist;
26753 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26754 `function-definition' sequence that follows a template header.
26755 If MEMBER_P is true, this declaration appears in a class scope.
26757 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26758 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26760 static tree
26761 cp_parser_single_declaration (cp_parser* parser,
26762 vec<deferred_access_check, va_gc> *checks,
26763 bool member_p,
26764 bool explicit_specialization_p,
26765 bool* friend_p)
26767 int declares_class_or_enum;
26768 tree decl = NULL_TREE;
26769 cp_decl_specifier_seq decl_specifiers;
26770 bool function_definition_p = false;
26771 cp_token *decl_spec_token_start;
26773 /* This function is only used when processing a template
26774 declaration. */
26775 gcc_assert (innermost_scope_kind () == sk_template_parms
26776 || innermost_scope_kind () == sk_template_spec);
26778 /* Defer access checks until we know what is being declared. */
26779 push_deferring_access_checks (dk_deferred);
26781 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26782 alternative. */
26783 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26784 cp_parser_decl_specifier_seq (parser,
26785 CP_PARSER_FLAGS_OPTIONAL,
26786 &decl_specifiers,
26787 &declares_class_or_enum);
26788 if (friend_p)
26789 *friend_p = cp_parser_friend_p (&decl_specifiers);
26791 /* There are no template typedefs. */
26792 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26794 error_at (decl_spec_token_start->location,
26795 "template declaration of %<typedef%>");
26796 decl = error_mark_node;
26799 /* Gather up the access checks that occurred the
26800 decl-specifier-seq. */
26801 stop_deferring_access_checks ();
26803 /* Check for the declaration of a template class. */
26804 if (declares_class_or_enum)
26806 if (cp_parser_declares_only_class_p (parser)
26807 || (declares_class_or_enum & 2))
26809 // If this is a declaration, but not a definition, associate
26810 // any constraints with the type declaration. Constraints
26811 // are associated with definitions in cp_parser_class_specifier.
26812 if (declares_class_or_enum == 1)
26813 associate_classtype_constraints (decl_specifiers.type);
26815 decl = shadow_tag (&decl_specifiers);
26817 /* In this case:
26819 struct C {
26820 friend template <typename T> struct A<T>::B;
26823 A<T>::B will be represented by a TYPENAME_TYPE, and
26824 therefore not recognized by shadow_tag. */
26825 if (friend_p && *friend_p
26826 && !decl
26827 && decl_specifiers.type
26828 && TYPE_P (decl_specifiers.type))
26829 decl = decl_specifiers.type;
26831 if (decl && decl != error_mark_node)
26832 decl = TYPE_NAME (decl);
26833 else
26834 decl = error_mark_node;
26836 /* Perform access checks for template parameters. */
26837 cp_parser_perform_template_parameter_access_checks (checks);
26839 /* Give a helpful diagnostic for
26840 template <class T> struct A { } a;
26841 if we aren't already recovering from an error. */
26842 if (!cp_parser_declares_only_class_p (parser)
26843 && !seen_error ())
26845 error_at (cp_lexer_peek_token (parser->lexer)->location,
26846 "a class template declaration must not declare "
26847 "anything else");
26848 cp_parser_skip_to_end_of_block_or_statement (parser);
26849 goto out;
26854 /* Complain about missing 'typename' or other invalid type names. */
26855 if (!decl_specifiers.any_type_specifiers_p
26856 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26858 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26859 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26860 the rest of this declaration. */
26861 decl = error_mark_node;
26862 goto out;
26865 /* If it's not a template class, try for a template function. If
26866 the next token is a `;', then this declaration does not declare
26867 anything. But, if there were errors in the decl-specifiers, then
26868 the error might well have come from an attempted class-specifier.
26869 In that case, there's no need to warn about a missing declarator. */
26870 if (!decl
26871 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26872 || decl_specifiers.type != error_mark_node))
26874 decl = cp_parser_init_declarator (parser,
26875 &decl_specifiers,
26876 checks,
26877 /*function_definition_allowed_p=*/true,
26878 member_p,
26879 declares_class_or_enum,
26880 &function_definition_p,
26881 NULL, NULL, NULL);
26883 /* 7.1.1-1 [dcl.stc]
26885 A storage-class-specifier shall not be specified in an explicit
26886 specialization... */
26887 if (decl
26888 && explicit_specialization_p
26889 && decl_specifiers.storage_class != sc_none)
26891 error_at (decl_spec_token_start->location,
26892 "explicit template specialization cannot have a storage class");
26893 decl = error_mark_node;
26896 if (decl && VAR_P (decl))
26897 check_template_variable (decl);
26900 /* Look for a trailing `;' after the declaration. */
26901 if (!function_definition_p
26902 && (decl == error_mark_node
26903 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26904 cp_parser_skip_to_end_of_block_or_statement (parser);
26906 out:
26907 pop_deferring_access_checks ();
26909 /* Clear any current qualification; whatever comes next is the start
26910 of something new. */
26911 parser->scope = NULL_TREE;
26912 parser->qualifying_scope = NULL_TREE;
26913 parser->object_scope = NULL_TREE;
26915 return decl;
26918 /* Parse a cast-expression that is not the operand of a unary "&". */
26920 static cp_expr
26921 cp_parser_simple_cast_expression (cp_parser *parser)
26923 return cp_parser_cast_expression (parser, /*address_p=*/false,
26924 /*cast_p=*/false, /*decltype*/false, NULL);
26927 /* Parse a functional cast to TYPE. Returns an expression
26928 representing the cast. */
26930 static cp_expr
26931 cp_parser_functional_cast (cp_parser* parser, tree type)
26933 vec<tree, va_gc> *vec;
26934 tree expression_list;
26935 cp_expr cast;
26936 bool nonconst_p;
26938 location_t start_loc = input_location;
26940 if (!type)
26941 type = error_mark_node;
26943 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26945 cp_lexer_set_source_position (parser->lexer);
26946 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26947 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26948 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26949 if (TREE_CODE (type) == TYPE_DECL)
26950 type = TREE_TYPE (type);
26952 cast = finish_compound_literal (type, expression_list,
26953 tf_warning_or_error, fcl_functional);
26954 /* Create a location of the form:
26955 type_name{i, f}
26956 ^~~~~~~~~~~~~~~
26957 with caret == start at the start of the type name,
26958 finishing at the closing brace. */
26959 location_t finish_loc
26960 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26961 location_t combined_loc = make_location (start_loc, start_loc,
26962 finish_loc);
26963 cast.set_location (combined_loc);
26964 return cast;
26968 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
26969 /*cast_p=*/true,
26970 /*allow_expansion_p=*/true,
26971 /*non_constant_p=*/NULL);
26972 if (vec == NULL)
26973 expression_list = error_mark_node;
26974 else
26976 expression_list = build_tree_list_vec (vec);
26977 release_tree_vector (vec);
26980 cast = build_functional_cast (type, expression_list,
26981 tf_warning_or_error);
26982 /* [expr.const]/1: In an integral constant expression "only type
26983 conversions to integral or enumeration type can be used". */
26984 if (TREE_CODE (type) == TYPE_DECL)
26985 type = TREE_TYPE (type);
26986 if (cast != error_mark_node
26987 && !cast_valid_in_integral_constant_expression_p (type)
26988 && cp_parser_non_integral_constant_expression (parser,
26989 NIC_CONSTRUCTOR))
26990 return error_mark_node;
26992 /* Create a location of the form:
26993 float(i)
26994 ^~~~~~~~
26995 with caret == start at the start of the type name,
26996 finishing at the closing paren. */
26997 location_t finish_loc
26998 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
26999 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27000 cast.set_location (combined_loc);
27001 return cast;
27004 /* Save the tokens that make up the body of a member function defined
27005 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27006 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27007 specifiers applied to the declaration. Returns the FUNCTION_DECL
27008 for the member function. */
27010 static tree
27011 cp_parser_save_member_function_body (cp_parser* parser,
27012 cp_decl_specifier_seq *decl_specifiers,
27013 cp_declarator *declarator,
27014 tree attributes)
27016 cp_token *first;
27017 cp_token *last;
27018 tree fn;
27019 bool function_try_block = false;
27021 /* Create the FUNCTION_DECL. */
27022 fn = grokmethod (decl_specifiers, declarator, attributes);
27023 cp_finalize_omp_declare_simd (parser, fn);
27024 cp_finalize_oacc_routine (parser, fn, true);
27025 /* If something went badly wrong, bail out now. */
27026 if (fn == error_mark_node)
27028 /* If there's a function-body, skip it. */
27029 if (cp_parser_token_starts_function_definition_p
27030 (cp_lexer_peek_token (parser->lexer)))
27031 cp_parser_skip_to_end_of_block_or_statement (parser);
27032 return error_mark_node;
27035 /* Remember it, if there default args to post process. */
27036 cp_parser_save_default_args (parser, fn);
27038 /* Save away the tokens that make up the body of the
27039 function. */
27040 first = parser->lexer->next_token;
27042 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27043 cp_lexer_consume_token (parser->lexer);
27044 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27045 RID_TRANSACTION_ATOMIC))
27047 cp_lexer_consume_token (parser->lexer);
27048 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27049 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27050 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27051 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27052 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27053 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27054 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27056 cp_lexer_consume_token (parser->lexer);
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);
27062 else
27063 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27064 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27066 cp_lexer_consume_token (parser->lexer);
27067 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27068 break;
27072 /* Handle function try blocks. */
27073 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27075 cp_lexer_consume_token (parser->lexer);
27076 function_try_block = true;
27078 /* We can have braced-init-list mem-initializers before the fn body. */
27079 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27081 cp_lexer_consume_token (parser->lexer);
27082 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27084 /* cache_group will stop after an un-nested { } pair, too. */
27085 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27086 break;
27088 /* variadic mem-inits have ... after the ')'. */
27089 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27090 cp_lexer_consume_token (parser->lexer);
27093 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27094 /* Handle function try blocks. */
27095 if (function_try_block)
27096 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27097 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27098 last = parser->lexer->next_token;
27100 /* Save away the inline definition; we will process it when the
27101 class is complete. */
27102 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27103 DECL_PENDING_INLINE_P (fn) = 1;
27105 /* We need to know that this was defined in the class, so that
27106 friend templates are handled correctly. */
27107 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27109 /* Add FN to the queue of functions to be parsed later. */
27110 vec_safe_push (unparsed_funs_with_definitions, fn);
27112 return fn;
27115 /* Save the tokens that make up the in-class initializer for a non-static
27116 data member. Returns a DEFAULT_ARG. */
27118 static tree
27119 cp_parser_save_nsdmi (cp_parser* parser)
27121 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27124 /* Parse a template-argument-list, as well as the trailing ">" (but
27125 not the opening "<"). See cp_parser_template_argument_list for the
27126 return value. */
27128 static tree
27129 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27131 tree arguments;
27132 tree saved_scope;
27133 tree saved_qualifying_scope;
27134 tree saved_object_scope;
27135 bool saved_greater_than_is_operator_p;
27136 int saved_unevaluated_operand;
27137 int saved_inhibit_evaluation_warnings;
27139 /* [temp.names]
27141 When parsing a template-id, the first non-nested `>' is taken as
27142 the end of the template-argument-list rather than a greater-than
27143 operator. */
27144 saved_greater_than_is_operator_p
27145 = parser->greater_than_is_operator_p;
27146 parser->greater_than_is_operator_p = false;
27147 /* Parsing the argument list may modify SCOPE, so we save it
27148 here. */
27149 saved_scope = parser->scope;
27150 saved_qualifying_scope = parser->qualifying_scope;
27151 saved_object_scope = parser->object_scope;
27152 /* We need to evaluate the template arguments, even though this
27153 template-id may be nested within a "sizeof". */
27154 saved_unevaluated_operand = cp_unevaluated_operand;
27155 cp_unevaluated_operand = 0;
27156 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27157 c_inhibit_evaluation_warnings = 0;
27158 /* Parse the template-argument-list itself. */
27159 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27160 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27161 arguments = NULL_TREE;
27162 else
27163 arguments = cp_parser_template_argument_list (parser);
27164 /* Look for the `>' that ends the template-argument-list. If we find
27165 a '>>' instead, it's probably just a typo. */
27166 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27168 if (cxx_dialect != cxx98)
27170 /* In C++0x, a `>>' in a template argument list or cast
27171 expression is considered to be two separate `>'
27172 tokens. So, change the current token to a `>', but don't
27173 consume it: it will be consumed later when the outer
27174 template argument list (or cast expression) is parsed.
27175 Note that this replacement of `>' for `>>' is necessary
27176 even if we are parsing tentatively: in the tentative
27177 case, after calling
27178 cp_parser_enclosed_template_argument_list we will always
27179 throw away all of the template arguments and the first
27180 closing `>', either because the template argument list
27181 was erroneous or because we are replacing those tokens
27182 with a CPP_TEMPLATE_ID token. The second `>' (which will
27183 not have been thrown away) is needed either to close an
27184 outer template argument list or to complete a new-style
27185 cast. */
27186 cp_token *token = cp_lexer_peek_token (parser->lexer);
27187 token->type = CPP_GREATER;
27189 else if (!saved_greater_than_is_operator_p)
27191 /* If we're in a nested template argument list, the '>>' has
27192 to be a typo for '> >'. We emit the error message, but we
27193 continue parsing and we push a '>' as next token, so that
27194 the argument list will be parsed correctly. Note that the
27195 global source location is still on the token before the
27196 '>>', so we need to say explicitly where we want it. */
27197 cp_token *token = cp_lexer_peek_token (parser->lexer);
27198 gcc_rich_location richloc (token->location);
27199 richloc.add_fixit_replace ("> >");
27200 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27201 "within a nested template argument list");
27203 token->type = CPP_GREATER;
27205 else
27207 /* If this is not a nested template argument list, the '>>'
27208 is a typo for '>'. Emit an error message and continue.
27209 Same deal about the token location, but here we can get it
27210 right by consuming the '>>' before issuing the diagnostic. */
27211 cp_token *token = cp_lexer_consume_token (parser->lexer);
27212 error_at (token->location,
27213 "spurious %<>>%>, use %<>%> to terminate "
27214 "a template argument list");
27217 else
27218 cp_parser_skip_to_end_of_template_parameter_list (parser);
27219 /* The `>' token might be a greater-than operator again now. */
27220 parser->greater_than_is_operator_p
27221 = saved_greater_than_is_operator_p;
27222 /* Restore the SAVED_SCOPE. */
27223 parser->scope = saved_scope;
27224 parser->qualifying_scope = saved_qualifying_scope;
27225 parser->object_scope = saved_object_scope;
27226 cp_unevaluated_operand = saved_unevaluated_operand;
27227 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27229 return arguments;
27232 /* MEMBER_FUNCTION is a member function, or a friend. If default
27233 arguments, or the body of the function have not yet been parsed,
27234 parse them now. */
27236 static void
27237 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27239 timevar_push (TV_PARSE_INMETH);
27240 /* If this member is a template, get the underlying
27241 FUNCTION_DECL. */
27242 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27243 member_function = DECL_TEMPLATE_RESULT (member_function);
27245 /* There should not be any class definitions in progress at this
27246 point; the bodies of members are only parsed outside of all class
27247 definitions. */
27248 gcc_assert (parser->num_classes_being_defined == 0);
27249 /* While we're parsing the member functions we might encounter more
27250 classes. We want to handle them right away, but we don't want
27251 them getting mixed up with functions that are currently in the
27252 queue. */
27253 push_unparsed_function_queues (parser);
27255 /* Make sure that any template parameters are in scope. */
27256 maybe_begin_member_template_processing (member_function);
27258 /* If the body of the function has not yet been parsed, parse it
27259 now. */
27260 if (DECL_PENDING_INLINE_P (member_function))
27262 tree function_scope;
27263 cp_token_cache *tokens;
27265 /* The function is no longer pending; we are processing it. */
27266 tokens = DECL_PENDING_INLINE_INFO (member_function);
27267 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27268 DECL_PENDING_INLINE_P (member_function) = 0;
27270 /* If this is a local class, enter the scope of the containing
27271 function. */
27272 function_scope = current_function_decl;
27273 if (function_scope)
27274 push_function_context ();
27276 /* Push the body of the function onto the lexer stack. */
27277 cp_parser_push_lexer_for_tokens (parser, tokens);
27279 /* Let the front end know that we going to be defining this
27280 function. */
27281 start_preparsed_function (member_function, NULL_TREE,
27282 SF_PRE_PARSED | SF_INCLASS_INLINE);
27284 /* Don't do access checking if it is a templated function. */
27285 if (processing_template_decl)
27286 push_deferring_access_checks (dk_no_check);
27288 /* #pragma omp declare reduction needs special parsing. */
27289 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27291 parser->lexer->in_pragma = true;
27292 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27293 finish_function (/*inline*/2);
27294 cp_check_omp_declare_reduction (member_function);
27296 else
27297 /* Now, parse the body of the function. */
27298 cp_parser_function_definition_after_declarator (parser,
27299 /*inline_p=*/true);
27301 if (processing_template_decl)
27302 pop_deferring_access_checks ();
27304 /* Leave the scope of the containing function. */
27305 if (function_scope)
27306 pop_function_context ();
27307 cp_parser_pop_lexer (parser);
27310 /* Remove any template parameters from the symbol table. */
27311 maybe_end_member_template_processing ();
27313 /* Restore the queue. */
27314 pop_unparsed_function_queues (parser);
27315 timevar_pop (TV_PARSE_INMETH);
27318 /* If DECL contains any default args, remember it on the unparsed
27319 functions queue. */
27321 static void
27322 cp_parser_save_default_args (cp_parser* parser, tree decl)
27324 tree probe;
27326 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27327 probe;
27328 probe = TREE_CHAIN (probe))
27329 if (TREE_PURPOSE (probe))
27331 cp_default_arg_entry entry = {current_class_type, decl};
27332 vec_safe_push (unparsed_funs_with_default_args, entry);
27333 break;
27337 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27338 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27339 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27340 from the parameter-type-list. */
27342 static tree
27343 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27344 tree default_arg, tree parmtype)
27346 cp_token_cache *tokens;
27347 tree parsed_arg;
27348 bool dummy;
27350 if (default_arg == error_mark_node)
27351 return error_mark_node;
27353 /* Push the saved tokens for the default argument onto the parser's
27354 lexer stack. */
27355 tokens = DEFARG_TOKENS (default_arg);
27356 cp_parser_push_lexer_for_tokens (parser, tokens);
27358 start_lambda_scope (decl);
27360 /* Parse the default argument. */
27361 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27362 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27363 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27365 finish_lambda_scope ();
27367 if (parsed_arg == error_mark_node)
27368 cp_parser_skip_to_end_of_statement (parser);
27370 if (!processing_template_decl)
27372 /* In a non-template class, check conversions now. In a template,
27373 we'll wait and instantiate these as needed. */
27374 if (TREE_CODE (decl) == PARM_DECL)
27375 parsed_arg = check_default_argument (parmtype, parsed_arg,
27376 tf_warning_or_error);
27377 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27378 parsed_arg = error_mark_node;
27379 else
27380 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27383 /* If the token stream has not been completely used up, then
27384 there was extra junk after the end of the default
27385 argument. */
27386 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27388 if (TREE_CODE (decl) == PARM_DECL)
27389 cp_parser_error (parser, "expected %<,%>");
27390 else
27391 cp_parser_error (parser, "expected %<;%>");
27394 /* Revert to the main lexer. */
27395 cp_parser_pop_lexer (parser);
27397 return parsed_arg;
27400 /* FIELD is a non-static data member with an initializer which we saved for
27401 later; parse it now. */
27403 static void
27404 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27406 tree def;
27408 maybe_begin_member_template_processing (field);
27410 push_unparsed_function_queues (parser);
27411 def = cp_parser_late_parse_one_default_arg (parser, field,
27412 DECL_INITIAL (field),
27413 NULL_TREE);
27414 pop_unparsed_function_queues (parser);
27416 maybe_end_member_template_processing ();
27418 DECL_INITIAL (field) = def;
27421 /* FN is a FUNCTION_DECL which may contains a parameter with an
27422 unparsed DEFAULT_ARG. Parse the default args now. This function
27423 assumes that the current scope is the scope in which the default
27424 argument should be processed. */
27426 static void
27427 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27429 bool saved_local_variables_forbidden_p;
27430 tree parm, parmdecl;
27432 /* While we're parsing the default args, we might (due to the
27433 statement expression extension) encounter more classes. We want
27434 to handle them right away, but we don't want them getting mixed
27435 up with default args that are currently in the queue. */
27436 push_unparsed_function_queues (parser);
27438 /* Local variable names (and the `this' keyword) may not appear
27439 in a default argument. */
27440 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27441 parser->local_variables_forbidden_p = true;
27443 push_defarg_context (fn);
27445 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27446 parmdecl = DECL_ARGUMENTS (fn);
27447 parm && parm != void_list_node;
27448 parm = TREE_CHAIN (parm),
27449 parmdecl = DECL_CHAIN (parmdecl))
27451 tree default_arg = TREE_PURPOSE (parm);
27452 tree parsed_arg;
27453 vec<tree, va_gc> *insts;
27454 tree copy;
27455 unsigned ix;
27457 if (!default_arg)
27458 continue;
27460 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27461 /* This can happen for a friend declaration for a function
27462 already declared with default arguments. */
27463 continue;
27465 parsed_arg
27466 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27467 default_arg,
27468 TREE_VALUE (parm));
27469 if (parsed_arg == error_mark_node)
27471 continue;
27474 TREE_PURPOSE (parm) = parsed_arg;
27476 /* Update any instantiations we've already created. */
27477 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27478 vec_safe_iterate (insts, ix, &copy); ix++)
27479 TREE_PURPOSE (copy) = parsed_arg;
27482 pop_defarg_context ();
27484 /* Make sure no default arg is missing. */
27485 check_default_args (fn);
27487 /* Restore the state of local_variables_forbidden_p. */
27488 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27490 /* Restore the queue. */
27491 pop_unparsed_function_queues (parser);
27494 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27496 sizeof ... ( identifier )
27498 where the 'sizeof' token has already been consumed. */
27500 static tree
27501 cp_parser_sizeof_pack (cp_parser *parser)
27503 /* Consume the `...'. */
27504 cp_lexer_consume_token (parser->lexer);
27505 maybe_warn_variadic_templates ();
27507 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27508 if (paren)
27509 cp_lexer_consume_token (parser->lexer);
27510 else
27511 permerror (cp_lexer_peek_token (parser->lexer)->location,
27512 "%<sizeof...%> argument must be surrounded by parentheses");
27514 cp_token *token = cp_lexer_peek_token (parser->lexer);
27515 tree name = cp_parser_identifier (parser);
27516 if (name == error_mark_node)
27517 return error_mark_node;
27518 /* The name is not qualified. */
27519 parser->scope = NULL_TREE;
27520 parser->qualifying_scope = NULL_TREE;
27521 parser->object_scope = NULL_TREE;
27522 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27523 if (expr == error_mark_node)
27524 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27525 token->location);
27526 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27527 expr = TREE_TYPE (expr);
27528 else if (TREE_CODE (expr) == CONST_DECL)
27529 expr = DECL_INITIAL (expr);
27530 expr = make_pack_expansion (expr);
27531 PACK_EXPANSION_SIZEOF_P (expr) = true;
27533 if (paren)
27534 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27536 return expr;
27539 /* Parse the operand of `sizeof' (or a similar operator). Returns
27540 either a TYPE or an expression, depending on the form of the
27541 input. The KEYWORD indicates which kind of expression we have
27542 encountered. */
27544 static tree
27545 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27547 tree expr = NULL_TREE;
27548 const char *saved_message;
27549 char *tmp;
27550 bool saved_integral_constant_expression_p;
27551 bool saved_non_integral_constant_expression_p;
27553 /* If it's a `...', then we are computing the length of a parameter
27554 pack. */
27555 if (keyword == RID_SIZEOF
27556 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27557 return cp_parser_sizeof_pack (parser);
27559 /* Types cannot be defined in a `sizeof' expression. Save away the
27560 old message. */
27561 saved_message = parser->type_definition_forbidden_message;
27562 /* And create the new one. */
27563 tmp = concat ("types may not be defined in %<",
27564 IDENTIFIER_POINTER (ridpointers[keyword]),
27565 "%> expressions", NULL);
27566 parser->type_definition_forbidden_message = tmp;
27568 /* The restrictions on constant-expressions do not apply inside
27569 sizeof expressions. */
27570 saved_integral_constant_expression_p
27571 = parser->integral_constant_expression_p;
27572 saved_non_integral_constant_expression_p
27573 = parser->non_integral_constant_expression_p;
27574 parser->integral_constant_expression_p = false;
27576 /* Do not actually evaluate the expression. */
27577 ++cp_unevaluated_operand;
27578 ++c_inhibit_evaluation_warnings;
27579 /* If it's a `(', then we might be looking at the type-id
27580 construction. */
27581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27583 tree type = NULL_TREE;
27585 /* We can't be sure yet whether we're looking at a type-id or an
27586 expression. */
27587 cp_parser_parse_tentatively (parser);
27588 /* Note: as a GNU Extension, compound literals are considered
27589 postfix-expressions as they are in C99, so they are valid
27590 arguments to sizeof. See comment in cp_parser_cast_expression
27591 for details. */
27592 if (cp_parser_compound_literal_p (parser))
27593 cp_parser_simulate_error (parser);
27594 else
27596 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27597 parser->in_type_id_in_expr_p = true;
27598 /* Look for the type-id. */
27599 type = cp_parser_type_id (parser);
27600 /* Look for the closing `)'. */
27601 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27602 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27605 /* If all went well, then we're done. */
27606 if (cp_parser_parse_definitely (parser))
27608 cp_decl_specifier_seq decl_specs;
27610 /* Build a trivial decl-specifier-seq. */
27611 clear_decl_specs (&decl_specs);
27612 decl_specs.type = type;
27614 /* Call grokdeclarator to figure out what type this is. */
27615 expr = grokdeclarator (NULL,
27616 &decl_specs,
27617 TYPENAME,
27618 /*initialized=*/0,
27619 /*attrlist=*/NULL);
27623 /* If the type-id production did not work out, then we must be
27624 looking at the unary-expression production. */
27625 if (!expr)
27626 expr = cp_parser_unary_expression (parser);
27628 /* Go back to evaluating expressions. */
27629 --cp_unevaluated_operand;
27630 --c_inhibit_evaluation_warnings;
27632 /* Free the message we created. */
27633 free (tmp);
27634 /* And restore the old one. */
27635 parser->type_definition_forbidden_message = saved_message;
27636 parser->integral_constant_expression_p
27637 = saved_integral_constant_expression_p;
27638 parser->non_integral_constant_expression_p
27639 = saved_non_integral_constant_expression_p;
27641 return expr;
27644 /* If the current declaration has no declarator, return true. */
27646 static bool
27647 cp_parser_declares_only_class_p (cp_parser *parser)
27649 /* If the next token is a `;' or a `,' then there is no
27650 declarator. */
27651 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27652 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27655 /* Update the DECL_SPECS to reflect the storage class indicated by
27656 KEYWORD. */
27658 static void
27659 cp_parser_set_storage_class (cp_parser *parser,
27660 cp_decl_specifier_seq *decl_specs,
27661 enum rid keyword,
27662 cp_token *token)
27664 cp_storage_class storage_class;
27666 if (parser->in_unbraced_linkage_specification_p)
27668 error_at (token->location, "invalid use of %qD in linkage specification",
27669 ridpointers[keyword]);
27670 return;
27672 else if (decl_specs->storage_class != sc_none)
27674 decl_specs->conflicting_specifiers_p = true;
27675 return;
27678 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27679 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27680 && decl_specs->gnu_thread_keyword_p)
27682 pedwarn (decl_specs->locations[ds_thread], 0,
27683 "%<__thread%> before %qD", ridpointers[keyword]);
27686 switch (keyword)
27688 case RID_AUTO:
27689 storage_class = sc_auto;
27690 break;
27691 case RID_REGISTER:
27692 storage_class = sc_register;
27693 break;
27694 case RID_STATIC:
27695 storage_class = sc_static;
27696 break;
27697 case RID_EXTERN:
27698 storage_class = sc_extern;
27699 break;
27700 case RID_MUTABLE:
27701 storage_class = sc_mutable;
27702 break;
27703 default:
27704 gcc_unreachable ();
27706 decl_specs->storage_class = storage_class;
27707 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27709 /* A storage class specifier cannot be applied alongside a typedef
27710 specifier. If there is a typedef specifier present then set
27711 conflicting_specifiers_p which will trigger an error later
27712 on in grokdeclarator. */
27713 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27714 decl_specs->conflicting_specifiers_p = true;
27717 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27718 is true, the type is a class or enum definition. */
27720 static void
27721 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27722 tree type_spec,
27723 cp_token *token,
27724 bool type_definition_p)
27726 decl_specs->any_specifiers_p = true;
27728 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27729 (with, for example, in "typedef int wchar_t;") we remember that
27730 this is what happened. In system headers, we ignore these
27731 declarations so that G++ can work with system headers that are not
27732 C++-safe. */
27733 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27734 && !type_definition_p
27735 && (type_spec == boolean_type_node
27736 || type_spec == char16_type_node
27737 || type_spec == char32_type_node
27738 || type_spec == wchar_type_node)
27739 && (decl_specs->type
27740 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27741 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27742 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27743 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27745 decl_specs->redefined_builtin_type = type_spec;
27746 set_and_check_decl_spec_loc (decl_specs,
27747 ds_redefined_builtin_type_spec,
27748 token);
27749 if (!decl_specs->type)
27751 decl_specs->type = type_spec;
27752 decl_specs->type_definition_p = false;
27753 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27756 else if (decl_specs->type)
27757 decl_specs->multiple_types_p = true;
27758 else
27760 decl_specs->type = type_spec;
27761 decl_specs->type_definition_p = type_definition_p;
27762 decl_specs->redefined_builtin_type = NULL_TREE;
27763 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27767 /* True iff TOKEN is the GNU keyword __thread. */
27769 static bool
27770 token_is__thread (cp_token *token)
27772 gcc_assert (token->keyword == RID_THREAD);
27773 return id_equal (token->u.value, "__thread");
27776 /* Set the location for a declarator specifier and check if it is
27777 duplicated.
27779 DECL_SPECS is the sequence of declarator specifiers onto which to
27780 set the location.
27782 DS is the single declarator specifier to set which location is to
27783 be set onto the existing sequence of declarators.
27785 LOCATION is the location for the declarator specifier to
27786 consider. */
27788 static void
27789 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27790 cp_decl_spec ds, cp_token *token)
27792 gcc_assert (ds < ds_last);
27794 if (decl_specs == NULL)
27795 return;
27797 source_location location = token->location;
27799 if (decl_specs->locations[ds] == 0)
27801 decl_specs->locations[ds] = location;
27802 if (ds == ds_thread)
27803 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27805 else
27807 if (ds == ds_long)
27809 if (decl_specs->locations[ds_long_long] != 0)
27810 error_at (location,
27811 "%<long long long%> is too long for GCC");
27812 else
27814 decl_specs->locations[ds_long_long] = location;
27815 pedwarn_cxx98 (location,
27816 OPT_Wlong_long,
27817 "ISO C++ 1998 does not support %<long long%>");
27820 else if (ds == ds_thread)
27822 bool gnu = token_is__thread (token);
27823 if (gnu != decl_specs->gnu_thread_keyword_p)
27824 error_at (location,
27825 "both %<__thread%> and %<thread_local%> specified");
27826 else
27828 gcc_rich_location richloc (location);
27829 richloc.add_fixit_remove ();
27830 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27833 else
27835 static const char *const decl_spec_names[] = {
27836 "signed",
27837 "unsigned",
27838 "short",
27839 "long",
27840 "const",
27841 "volatile",
27842 "restrict",
27843 "inline",
27844 "virtual",
27845 "explicit",
27846 "friend",
27847 "typedef",
27848 "using",
27849 "constexpr",
27850 "__complex"
27852 gcc_rich_location richloc (location);
27853 richloc.add_fixit_remove ();
27854 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27859 /* Return true iff the declarator specifier DS is present in the
27860 sequence of declarator specifiers DECL_SPECS. */
27862 bool
27863 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27864 cp_decl_spec ds)
27866 gcc_assert (ds < ds_last);
27868 if (decl_specs == NULL)
27869 return false;
27871 return decl_specs->locations[ds] != 0;
27874 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27875 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27877 static bool
27878 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27880 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27883 /* Issue an error message indicating that TOKEN_DESC was expected.
27884 If KEYWORD is true, it indicated this function is called by
27885 cp_parser_require_keword and the required token can only be
27886 a indicated keyword. */
27888 static void
27889 cp_parser_required_error (cp_parser *parser,
27890 required_token token_desc,
27891 bool keyword)
27893 switch (token_desc)
27895 case RT_NEW:
27896 cp_parser_error (parser, "expected %<new%>");
27897 return;
27898 case RT_DELETE:
27899 cp_parser_error (parser, "expected %<delete%>");
27900 return;
27901 case RT_RETURN:
27902 cp_parser_error (parser, "expected %<return%>");
27903 return;
27904 case RT_WHILE:
27905 cp_parser_error (parser, "expected %<while%>");
27906 return;
27907 case RT_EXTERN:
27908 cp_parser_error (parser, "expected %<extern%>");
27909 return;
27910 case RT_STATIC_ASSERT:
27911 cp_parser_error (parser, "expected %<static_assert%>");
27912 return;
27913 case RT_DECLTYPE:
27914 cp_parser_error (parser, "expected %<decltype%>");
27915 return;
27916 case RT_OPERATOR:
27917 cp_parser_error (parser, "expected %<operator%>");
27918 return;
27919 case RT_CLASS:
27920 cp_parser_error (parser, "expected %<class%>");
27921 return;
27922 case RT_TEMPLATE:
27923 cp_parser_error (parser, "expected %<template%>");
27924 return;
27925 case RT_NAMESPACE:
27926 cp_parser_error (parser, "expected %<namespace%>");
27927 return;
27928 case RT_USING:
27929 cp_parser_error (parser, "expected %<using%>");
27930 return;
27931 case RT_ASM:
27932 cp_parser_error (parser, "expected %<asm%>");
27933 return;
27934 case RT_TRY:
27935 cp_parser_error (parser, "expected %<try%>");
27936 return;
27937 case RT_CATCH:
27938 cp_parser_error (parser, "expected %<catch%>");
27939 return;
27940 case RT_THROW:
27941 cp_parser_error (parser, "expected %<throw%>");
27942 return;
27943 case RT_LABEL:
27944 cp_parser_error (parser, "expected %<__label__%>");
27945 return;
27946 case RT_AT_TRY:
27947 cp_parser_error (parser, "expected %<@try%>");
27948 return;
27949 case RT_AT_SYNCHRONIZED:
27950 cp_parser_error (parser, "expected %<@synchronized%>");
27951 return;
27952 case RT_AT_THROW:
27953 cp_parser_error (parser, "expected %<@throw%>");
27954 return;
27955 case RT_TRANSACTION_ATOMIC:
27956 cp_parser_error (parser, "expected %<__transaction_atomic%>");
27957 return;
27958 case RT_TRANSACTION_RELAXED:
27959 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
27960 return;
27961 default:
27962 break;
27964 if (!keyword)
27966 switch (token_desc)
27968 case RT_SEMICOLON:
27969 cp_parser_error (parser, "expected %<;%>");
27970 return;
27971 case RT_OPEN_PAREN:
27972 cp_parser_error (parser, "expected %<(%>");
27973 return;
27974 case RT_CLOSE_BRACE:
27975 cp_parser_error (parser, "expected %<}%>");
27976 return;
27977 case RT_OPEN_BRACE:
27978 cp_parser_error (parser, "expected %<{%>");
27979 return;
27980 case RT_CLOSE_SQUARE:
27981 cp_parser_error (parser, "expected %<]%>");
27982 return;
27983 case RT_OPEN_SQUARE:
27984 cp_parser_error (parser, "expected %<[%>");
27985 return;
27986 case RT_COMMA:
27987 cp_parser_error (parser, "expected %<,%>");
27988 return;
27989 case RT_SCOPE:
27990 cp_parser_error (parser, "expected %<::%>");
27991 return;
27992 case RT_LESS:
27993 cp_parser_error (parser, "expected %<<%>");
27994 return;
27995 case RT_GREATER:
27996 cp_parser_error (parser, "expected %<>%>");
27997 return;
27998 case RT_EQ:
27999 cp_parser_error (parser, "expected %<=%>");
28000 return;
28001 case RT_ELLIPSIS:
28002 cp_parser_error (parser, "expected %<...%>");
28003 return;
28004 case RT_MULT:
28005 cp_parser_error (parser, "expected %<*%>");
28006 return;
28007 case RT_COMPL:
28008 cp_parser_error (parser, "expected %<~%>");
28009 return;
28010 case RT_COLON:
28011 cp_parser_error (parser, "expected %<:%>");
28012 return;
28013 case RT_COLON_SCOPE:
28014 cp_parser_error (parser, "expected %<:%> or %<::%>");
28015 return;
28016 case RT_CLOSE_PAREN:
28017 cp_parser_error (parser, "expected %<)%>");
28018 return;
28019 case RT_COMMA_CLOSE_PAREN:
28020 cp_parser_error (parser, "expected %<,%> or %<)%>");
28021 return;
28022 case RT_PRAGMA_EOL:
28023 cp_parser_error (parser, "expected end of line");
28024 return;
28025 case RT_NAME:
28026 cp_parser_error (parser, "expected identifier");
28027 return;
28028 case RT_SELECT:
28029 cp_parser_error (parser, "expected selection-statement");
28030 return;
28031 case RT_INTERATION:
28032 cp_parser_error (parser, "expected iteration-statement");
28033 return;
28034 case RT_JUMP:
28035 cp_parser_error (parser, "expected jump-statement");
28036 return;
28037 case RT_CLASS_KEY:
28038 cp_parser_error (parser, "expected class-key");
28039 return;
28040 case RT_CLASS_TYPENAME_TEMPLATE:
28041 cp_parser_error (parser,
28042 "expected %<class%>, %<typename%>, or %<template%>");
28043 return;
28044 default:
28045 gcc_unreachable ();
28048 else
28049 gcc_unreachable ();
28054 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28055 issue an error message indicating that TOKEN_DESC was expected.
28057 Returns the token consumed, if the token had the appropriate type.
28058 Otherwise, returns NULL. */
28060 static cp_token *
28061 cp_parser_require (cp_parser* parser,
28062 enum cpp_ttype type,
28063 required_token token_desc)
28065 if (cp_lexer_next_token_is (parser->lexer, type))
28066 return cp_lexer_consume_token (parser->lexer);
28067 else
28069 /* Output the MESSAGE -- unless we're parsing tentatively. */
28070 if (!cp_parser_simulate_error (parser))
28071 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
28072 return NULL;
28076 /* An error message is produced if the next token is not '>'.
28077 All further tokens are skipped until the desired token is
28078 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28080 static void
28081 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28083 /* Current level of '< ... >'. */
28084 unsigned level = 0;
28085 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28086 unsigned nesting_depth = 0;
28088 /* Are we ready, yet? If not, issue error message. */
28089 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28090 return;
28092 /* Skip tokens until the desired token is found. */
28093 while (true)
28095 /* Peek at the next token. */
28096 switch (cp_lexer_peek_token (parser->lexer)->type)
28098 case CPP_LESS:
28099 if (!nesting_depth)
28100 ++level;
28101 break;
28103 case CPP_RSHIFT:
28104 if (cxx_dialect == cxx98)
28105 /* C++0x views the `>>' operator as two `>' tokens, but
28106 C++98 does not. */
28107 break;
28108 else if (!nesting_depth && level-- == 0)
28110 /* We've hit a `>>' where the first `>' closes the
28111 template argument list, and the second `>' is
28112 spurious. Just consume the `>>' and stop; we've
28113 already produced at least one error. */
28114 cp_lexer_consume_token (parser->lexer);
28115 return;
28117 /* Fall through for C++0x, so we handle the second `>' in
28118 the `>>'. */
28119 gcc_fallthrough ();
28121 case CPP_GREATER:
28122 if (!nesting_depth && level-- == 0)
28124 /* We've reached the token we want, consume it and stop. */
28125 cp_lexer_consume_token (parser->lexer);
28126 return;
28128 break;
28130 case CPP_OPEN_PAREN:
28131 case CPP_OPEN_SQUARE:
28132 ++nesting_depth;
28133 break;
28135 case CPP_CLOSE_PAREN:
28136 case CPP_CLOSE_SQUARE:
28137 if (nesting_depth-- == 0)
28138 return;
28139 break;
28141 case CPP_EOF:
28142 case CPP_PRAGMA_EOL:
28143 case CPP_SEMICOLON:
28144 case CPP_OPEN_BRACE:
28145 case CPP_CLOSE_BRACE:
28146 /* The '>' was probably forgotten, don't look further. */
28147 return;
28149 default:
28150 break;
28153 /* Consume this token. */
28154 cp_lexer_consume_token (parser->lexer);
28158 /* If the next token is the indicated keyword, consume it. Otherwise,
28159 issue an error message indicating that TOKEN_DESC was expected.
28161 Returns the token consumed, if the token had the appropriate type.
28162 Otherwise, returns NULL. */
28164 static cp_token *
28165 cp_parser_require_keyword (cp_parser* parser,
28166 enum rid keyword,
28167 required_token token_desc)
28169 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28171 if (token && token->keyword != keyword)
28173 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28174 return NULL;
28177 return token;
28180 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28181 function-definition. */
28183 static bool
28184 cp_parser_token_starts_function_definition_p (cp_token* token)
28186 return (/* An ordinary function-body begins with an `{'. */
28187 token->type == CPP_OPEN_BRACE
28188 /* A ctor-initializer begins with a `:'. */
28189 || token->type == CPP_COLON
28190 /* A function-try-block begins with `try'. */
28191 || token->keyword == RID_TRY
28192 /* A function-transaction-block begins with `__transaction_atomic'
28193 or `__transaction_relaxed'. */
28194 || token->keyword == RID_TRANSACTION_ATOMIC
28195 || token->keyword == RID_TRANSACTION_RELAXED
28196 /* The named return value extension begins with `return'. */
28197 || token->keyword == RID_RETURN);
28200 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28201 definition. */
28203 static bool
28204 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28206 cp_token *token;
28208 token = cp_lexer_peek_token (parser->lexer);
28209 return (token->type == CPP_OPEN_BRACE
28210 || (token->type == CPP_COLON
28211 && !parser->colon_doesnt_start_class_def_p));
28214 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28215 C++0x) ending a template-argument. */
28217 static bool
28218 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28220 cp_token *token;
28222 token = cp_lexer_peek_token (parser->lexer);
28223 return (token->type == CPP_COMMA
28224 || token->type == CPP_GREATER
28225 || token->type == CPP_ELLIPSIS
28226 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28229 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28230 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28232 static bool
28233 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28234 size_t n)
28236 cp_token *token;
28238 token = cp_lexer_peek_nth_token (parser->lexer, n);
28239 if (token->type == CPP_LESS)
28240 return true;
28241 /* Check for the sequence `<::' in the original code. It would be lexed as
28242 `[:', where `[' is a digraph, and there is no whitespace before
28243 `:'. */
28244 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28246 cp_token *token2;
28247 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28248 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28249 return true;
28251 return false;
28254 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28255 or none_type otherwise. */
28257 static enum tag_types
28258 cp_parser_token_is_class_key (cp_token* token)
28260 switch (token->keyword)
28262 case RID_CLASS:
28263 return class_type;
28264 case RID_STRUCT:
28265 return record_type;
28266 case RID_UNION:
28267 return union_type;
28269 default:
28270 return none_type;
28274 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28275 or none_type otherwise or if the token is null. */
28277 static enum tag_types
28278 cp_parser_token_is_type_parameter_key (cp_token* token)
28280 if (!token)
28281 return none_type;
28283 switch (token->keyword)
28285 case RID_CLASS:
28286 return class_type;
28287 case RID_TYPENAME:
28288 return typename_type;
28290 default:
28291 return none_type;
28295 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28297 static void
28298 cp_parser_check_class_key (enum tag_types class_key, tree type)
28300 if (type == error_mark_node)
28301 return;
28302 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28304 if (permerror (input_location, "%qs tag used in naming %q#T",
28305 class_key == union_type ? "union"
28306 : class_key == record_type ? "struct" : "class",
28307 type))
28308 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28309 "%q#T was previously declared here", type);
28313 /* Issue an error message if DECL is redeclared with different
28314 access than its original declaration [class.access.spec/3].
28315 This applies to nested classes, nested class templates and
28316 enumerations [class.mem/1]. */
28318 static void
28319 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28321 if (!decl
28322 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28323 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28324 return;
28326 if ((TREE_PRIVATE (decl)
28327 != (current_access_specifier == access_private_node))
28328 || (TREE_PROTECTED (decl)
28329 != (current_access_specifier == access_protected_node)))
28330 error_at (location, "%qD redeclared with different access", decl);
28333 /* Look for the `template' keyword, as a syntactic disambiguator.
28334 Return TRUE iff it is present, in which case it will be
28335 consumed. */
28337 static bool
28338 cp_parser_optional_template_keyword (cp_parser *parser)
28340 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28342 /* In C++98 the `template' keyword can only be used within templates;
28343 outside templates the parser can always figure out what is a
28344 template and what is not. In C++11, per the resolution of DR 468,
28345 `template' is allowed in cases where it is not strictly necessary. */
28346 if (!processing_template_decl
28347 && pedantic && cxx_dialect == cxx98)
28349 cp_token *token = cp_lexer_peek_token (parser->lexer);
28350 pedwarn (token->location, OPT_Wpedantic,
28351 "in C++98 %<template%> (as a disambiguator) is only "
28352 "allowed within templates");
28353 /* If this part of the token stream is rescanned, the same
28354 error message would be generated. So, we purge the token
28355 from the stream. */
28356 cp_lexer_purge_token (parser->lexer);
28357 return false;
28359 else
28361 /* Consume the `template' keyword. */
28362 cp_lexer_consume_token (parser->lexer);
28363 return true;
28366 return false;
28369 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28370 set PARSER->SCOPE, and perform other related actions. */
28372 static void
28373 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28375 struct tree_check *check_value;
28377 /* Get the stored value. */
28378 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28379 /* Set the scope from the stored value. */
28380 parser->scope = saved_checks_value (check_value);
28381 parser->qualifying_scope = check_value->qualifying_scope;
28382 parser->object_scope = NULL_TREE;
28385 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28386 encounter the end of a block before what we were looking for. */
28388 static bool
28389 cp_parser_cache_group (cp_parser *parser,
28390 enum cpp_ttype end,
28391 unsigned depth)
28393 while (true)
28395 cp_token *token = cp_lexer_peek_token (parser->lexer);
28397 /* Abort a parenthesized expression if we encounter a semicolon. */
28398 if ((end == CPP_CLOSE_PAREN || depth == 0)
28399 && token->type == CPP_SEMICOLON)
28400 return true;
28401 /* If we've reached the end of the file, stop. */
28402 if (token->type == CPP_EOF
28403 || (end != CPP_PRAGMA_EOL
28404 && token->type == CPP_PRAGMA_EOL))
28405 return true;
28406 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28407 /* We've hit the end of an enclosing block, so there's been some
28408 kind of syntax error. */
28409 return true;
28411 /* Consume the token. */
28412 cp_lexer_consume_token (parser->lexer);
28413 /* See if it starts a new group. */
28414 if (token->type == CPP_OPEN_BRACE)
28416 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28417 /* In theory this should probably check end == '}', but
28418 cp_parser_save_member_function_body needs it to exit
28419 after either '}' or ')' when called with ')'. */
28420 if (depth == 0)
28421 return false;
28423 else if (token->type == CPP_OPEN_PAREN)
28425 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28426 if (depth == 0 && end == CPP_CLOSE_PAREN)
28427 return false;
28429 else if (token->type == CPP_PRAGMA)
28430 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28431 else if (token->type == end)
28432 return false;
28436 /* Like above, for caching a default argument or NSDMI. Both of these are
28437 terminated by a non-nested comma, but it can be unclear whether or not a
28438 comma is nested in a template argument list unless we do more parsing.
28439 In order to handle this ambiguity, when we encounter a ',' after a '<'
28440 we try to parse what follows as a parameter-declaration-list (in the
28441 case of a default argument) or a member-declarator (in the case of an
28442 NSDMI). If that succeeds, then we stop caching. */
28444 static tree
28445 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28447 unsigned depth = 0;
28448 int maybe_template_id = 0;
28449 cp_token *first_token;
28450 cp_token *token;
28451 tree default_argument;
28453 /* Add tokens until we have processed the entire default
28454 argument. We add the range [first_token, token). */
28455 first_token = cp_lexer_peek_token (parser->lexer);
28456 if (first_token->type == CPP_OPEN_BRACE)
28458 /* For list-initialization, this is straightforward. */
28459 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28460 token = cp_lexer_peek_token (parser->lexer);
28462 else while (true)
28464 bool done = false;
28466 /* Peek at the next token. */
28467 token = cp_lexer_peek_token (parser->lexer);
28468 /* What we do depends on what token we have. */
28469 switch (token->type)
28471 /* In valid code, a default argument must be
28472 immediately followed by a `,' `)', or `...'. */
28473 case CPP_COMMA:
28474 if (depth == 0 && maybe_template_id)
28476 /* If we've seen a '<', we might be in a
28477 template-argument-list. Until Core issue 325 is
28478 resolved, we don't know how this situation ought
28479 to be handled, so try to DTRT. We check whether
28480 what comes after the comma is a valid parameter
28481 declaration list. If it is, then the comma ends
28482 the default argument; otherwise the default
28483 argument continues. */
28484 bool error = false;
28485 cp_token *peek;
28487 /* Set ITALP so cp_parser_parameter_declaration_list
28488 doesn't decide to commit to this parse. */
28489 bool saved_italp = parser->in_template_argument_list_p;
28490 parser->in_template_argument_list_p = true;
28492 cp_parser_parse_tentatively (parser);
28494 if (nsdmi)
28496 /* Parse declarators until we reach a non-comma or
28497 somthing that cannot be an initializer.
28498 Just checking whether we're looking at a single
28499 declarator is insufficient. Consider:
28500 int var = tuple<T,U>::x;
28501 The template parameter 'U' looks exactly like a
28502 declarator. */
28505 int ctor_dtor_or_conv_p;
28506 cp_lexer_consume_token (parser->lexer);
28507 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28508 &ctor_dtor_or_conv_p,
28509 /*parenthesized_p=*/NULL,
28510 /*member_p=*/true,
28511 /*friend_p=*/false);
28512 peek = cp_lexer_peek_token (parser->lexer);
28513 if (cp_parser_error_occurred (parser))
28514 break;
28516 while (peek->type == CPP_COMMA);
28517 /* If we met an '=' or ';' then the original comma
28518 was the end of the NSDMI. Otherwise assume
28519 we're still in the NSDMI. */
28520 error = (peek->type != CPP_EQ
28521 && peek->type != CPP_SEMICOLON);
28523 else
28525 cp_lexer_consume_token (parser->lexer);
28526 begin_scope (sk_function_parms, NULL_TREE);
28527 cp_parser_parameter_declaration_list (parser, &error);
28528 pop_bindings_and_leave_scope ();
28530 if (!cp_parser_error_occurred (parser) && !error)
28531 done = true;
28532 cp_parser_abort_tentative_parse (parser);
28534 parser->in_template_argument_list_p = saved_italp;
28535 break;
28537 /* FALLTHRU */
28538 case CPP_CLOSE_PAREN:
28539 case CPP_ELLIPSIS:
28540 /* If we run into a non-nested `;', `}', or `]',
28541 then the code is invalid -- but the default
28542 argument is certainly over. */
28543 case CPP_SEMICOLON:
28544 case CPP_CLOSE_BRACE:
28545 case CPP_CLOSE_SQUARE:
28546 if (depth == 0
28547 /* Handle correctly int n = sizeof ... ( p ); */
28548 && token->type != CPP_ELLIPSIS)
28549 done = true;
28550 /* Update DEPTH, if necessary. */
28551 else if (token->type == CPP_CLOSE_PAREN
28552 || token->type == CPP_CLOSE_BRACE
28553 || token->type == CPP_CLOSE_SQUARE)
28554 --depth;
28555 break;
28557 case CPP_OPEN_PAREN:
28558 case CPP_OPEN_SQUARE:
28559 case CPP_OPEN_BRACE:
28560 ++depth;
28561 break;
28563 case CPP_LESS:
28564 if (depth == 0)
28565 /* This might be the comparison operator, or it might
28566 start a template argument list. */
28567 ++maybe_template_id;
28568 break;
28570 case CPP_RSHIFT:
28571 if (cxx_dialect == cxx98)
28572 break;
28573 /* Fall through for C++0x, which treats the `>>'
28574 operator like two `>' tokens in certain
28575 cases. */
28576 gcc_fallthrough ();
28578 case CPP_GREATER:
28579 if (depth == 0)
28581 /* This might be an operator, or it might close a
28582 template argument list. But if a previous '<'
28583 started a template argument list, this will have
28584 closed it, so we can't be in one anymore. */
28585 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28586 if (maybe_template_id < 0)
28587 maybe_template_id = 0;
28589 break;
28591 /* If we run out of tokens, issue an error message. */
28592 case CPP_EOF:
28593 case CPP_PRAGMA_EOL:
28594 error_at (token->location, "file ends in default argument");
28595 return error_mark_node;
28597 case CPP_NAME:
28598 case CPP_SCOPE:
28599 /* In these cases, we should look for template-ids.
28600 For example, if the default argument is
28601 `X<int, double>()', we need to do name lookup to
28602 figure out whether or not `X' is a template; if
28603 so, the `,' does not end the default argument.
28605 That is not yet done. */
28606 break;
28608 default:
28609 break;
28612 /* If we've reached the end, stop. */
28613 if (done)
28614 break;
28616 /* Add the token to the token block. */
28617 token = cp_lexer_consume_token (parser->lexer);
28620 /* Create a DEFAULT_ARG to represent the unparsed default
28621 argument. */
28622 default_argument = make_node (DEFAULT_ARG);
28623 DEFARG_TOKENS (default_argument)
28624 = cp_token_cache_new (first_token, token);
28625 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28627 return default_argument;
28630 /* Begin parsing tentatively. We always save tokens while parsing
28631 tentatively so that if the tentative parsing fails we can restore the
28632 tokens. */
28634 static void
28635 cp_parser_parse_tentatively (cp_parser* parser)
28637 /* Enter a new parsing context. */
28638 parser->context = cp_parser_context_new (parser->context);
28639 /* Begin saving tokens. */
28640 cp_lexer_save_tokens (parser->lexer);
28641 /* In order to avoid repetitive access control error messages,
28642 access checks are queued up until we are no longer parsing
28643 tentatively. */
28644 push_deferring_access_checks (dk_deferred);
28647 /* Commit to the currently active tentative parse. */
28649 static void
28650 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28652 cp_parser_context *context;
28653 cp_lexer *lexer;
28655 /* Mark all of the levels as committed. */
28656 lexer = parser->lexer;
28657 for (context = parser->context; context->next; context = context->next)
28659 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28660 break;
28661 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28662 while (!cp_lexer_saving_tokens (lexer))
28663 lexer = lexer->next;
28664 cp_lexer_commit_tokens (lexer);
28668 /* Commit to the topmost currently active tentative parse.
28670 Note that this function shouldn't be called when there are
28671 irreversible side-effects while in a tentative state. For
28672 example, we shouldn't create a permanent entry in the symbol
28673 table, or issue an error message that might not apply if the
28674 tentative parse is aborted. */
28676 static void
28677 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28679 cp_parser_context *context = parser->context;
28680 cp_lexer *lexer = parser->lexer;
28682 if (context)
28684 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28685 return;
28686 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28688 while (!cp_lexer_saving_tokens (lexer))
28689 lexer = lexer->next;
28690 cp_lexer_commit_tokens (lexer);
28694 /* Abort the currently active tentative parse. All consumed tokens
28695 will be rolled back, and no diagnostics will be issued. */
28697 static void
28698 cp_parser_abort_tentative_parse (cp_parser* parser)
28700 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28701 || errorcount > 0);
28702 cp_parser_simulate_error (parser);
28703 /* Now, pretend that we want to see if the construct was
28704 successfully parsed. */
28705 cp_parser_parse_definitely (parser);
28708 /* Stop parsing tentatively. If a parse error has occurred, restore the
28709 token stream. Otherwise, commit to the tokens we have consumed.
28710 Returns true if no error occurred; false otherwise. */
28712 static bool
28713 cp_parser_parse_definitely (cp_parser* parser)
28715 bool error_occurred;
28716 cp_parser_context *context;
28718 /* Remember whether or not an error occurred, since we are about to
28719 destroy that information. */
28720 error_occurred = cp_parser_error_occurred (parser);
28721 /* Remove the topmost context from the stack. */
28722 context = parser->context;
28723 parser->context = context->next;
28724 /* If no parse errors occurred, commit to the tentative parse. */
28725 if (!error_occurred)
28727 /* Commit to the tokens read tentatively, unless that was
28728 already done. */
28729 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28730 cp_lexer_commit_tokens (parser->lexer);
28732 pop_to_parent_deferring_access_checks ();
28734 /* Otherwise, if errors occurred, roll back our state so that things
28735 are just as they were before we began the tentative parse. */
28736 else
28738 cp_lexer_rollback_tokens (parser->lexer);
28739 pop_deferring_access_checks ();
28741 /* Add the context to the front of the free list. */
28742 context->next = cp_parser_context_free_list;
28743 cp_parser_context_free_list = context;
28745 return !error_occurred;
28748 /* Returns true if we are parsing tentatively and are not committed to
28749 this tentative parse. */
28751 static bool
28752 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28754 return (cp_parser_parsing_tentatively (parser)
28755 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28758 /* Returns nonzero iff an error has occurred during the most recent
28759 tentative parse. */
28761 static bool
28762 cp_parser_error_occurred (cp_parser* parser)
28764 return (cp_parser_parsing_tentatively (parser)
28765 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28768 /* Returns nonzero if GNU extensions are allowed. */
28770 static bool
28771 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28773 return parser->allow_gnu_extensions_p;
28776 /* Objective-C++ Productions */
28779 /* Parse an Objective-C expression, which feeds into a primary-expression
28780 above.
28782 objc-expression:
28783 objc-message-expression
28784 objc-string-literal
28785 objc-encode-expression
28786 objc-protocol-expression
28787 objc-selector-expression
28789 Returns a tree representation of the expression. */
28791 static cp_expr
28792 cp_parser_objc_expression (cp_parser* parser)
28794 /* Try to figure out what kind of declaration is present. */
28795 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28797 switch (kwd->type)
28799 case CPP_OPEN_SQUARE:
28800 return cp_parser_objc_message_expression (parser);
28802 case CPP_OBJC_STRING:
28803 kwd = cp_lexer_consume_token (parser->lexer);
28804 return objc_build_string_object (kwd->u.value);
28806 case CPP_KEYWORD:
28807 switch (kwd->keyword)
28809 case RID_AT_ENCODE:
28810 return cp_parser_objc_encode_expression (parser);
28812 case RID_AT_PROTOCOL:
28813 return cp_parser_objc_protocol_expression (parser);
28815 case RID_AT_SELECTOR:
28816 return cp_parser_objc_selector_expression (parser);
28818 default:
28819 break;
28821 default:
28822 error_at (kwd->location,
28823 "misplaced %<@%D%> Objective-C++ construct",
28824 kwd->u.value);
28825 cp_parser_skip_to_end_of_block_or_statement (parser);
28828 return error_mark_node;
28831 /* Parse an Objective-C message expression.
28833 objc-message-expression:
28834 [ objc-message-receiver objc-message-args ]
28836 Returns a representation of an Objective-C message. */
28838 static tree
28839 cp_parser_objc_message_expression (cp_parser* parser)
28841 tree receiver, messageargs;
28843 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28844 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28845 receiver = cp_parser_objc_message_receiver (parser);
28846 messageargs = cp_parser_objc_message_args (parser);
28847 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28848 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28850 tree result = objc_build_message_expr (receiver, messageargs);
28852 /* Construct a location e.g.
28853 [self func1:5]
28854 ^~~~~~~~~~~~~~
28855 ranging from the '[' to the ']', with the caret at the start. */
28856 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28857 protected_set_expr_location (result, combined_loc);
28859 return result;
28862 /* Parse an objc-message-receiver.
28864 objc-message-receiver:
28865 expression
28866 simple-type-specifier
28868 Returns a representation of the type or expression. */
28870 static tree
28871 cp_parser_objc_message_receiver (cp_parser* parser)
28873 tree rcv;
28875 /* An Objective-C message receiver may be either (1) a type
28876 or (2) an expression. */
28877 cp_parser_parse_tentatively (parser);
28878 rcv = cp_parser_expression (parser);
28880 /* If that worked out, fine. */
28881 if (cp_parser_parse_definitely (parser))
28882 return rcv;
28884 cp_parser_parse_tentatively (parser);
28885 rcv = cp_parser_simple_type_specifier (parser,
28886 /*decl_specs=*/NULL,
28887 CP_PARSER_FLAGS_NONE);
28889 if (cp_parser_parse_definitely (parser))
28890 return objc_get_class_reference (rcv);
28892 cp_parser_error (parser, "objective-c++ message receiver expected");
28893 return error_mark_node;
28896 /* Parse the arguments and selectors comprising an Objective-C message.
28898 objc-message-args:
28899 objc-selector
28900 objc-selector-args
28901 objc-selector-args , objc-comma-args
28903 objc-selector-args:
28904 objc-selector [opt] : assignment-expression
28905 objc-selector-args objc-selector [opt] : assignment-expression
28907 objc-comma-args:
28908 assignment-expression
28909 objc-comma-args , assignment-expression
28911 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28912 selector arguments and TREE_VALUE containing a list of comma
28913 arguments. */
28915 static tree
28916 cp_parser_objc_message_args (cp_parser* parser)
28918 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28919 bool maybe_unary_selector_p = true;
28920 cp_token *token = cp_lexer_peek_token (parser->lexer);
28922 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28924 tree selector = NULL_TREE, arg;
28926 if (token->type != CPP_COLON)
28927 selector = cp_parser_objc_selector (parser);
28929 /* Detect if we have a unary selector. */
28930 if (maybe_unary_selector_p
28931 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28932 return build_tree_list (selector, NULL_TREE);
28934 maybe_unary_selector_p = false;
28935 cp_parser_require (parser, CPP_COLON, RT_COLON);
28936 arg = cp_parser_assignment_expression (parser);
28938 sel_args
28939 = chainon (sel_args,
28940 build_tree_list (selector, arg));
28942 token = cp_lexer_peek_token (parser->lexer);
28945 /* Handle non-selector arguments, if any. */
28946 while (token->type == CPP_COMMA)
28948 tree arg;
28950 cp_lexer_consume_token (parser->lexer);
28951 arg = cp_parser_assignment_expression (parser);
28953 addl_args
28954 = chainon (addl_args,
28955 build_tree_list (NULL_TREE, arg));
28957 token = cp_lexer_peek_token (parser->lexer);
28960 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
28962 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
28963 return build_tree_list (error_mark_node, error_mark_node);
28966 return build_tree_list (sel_args, addl_args);
28969 /* Parse an Objective-C encode expression.
28971 objc-encode-expression:
28972 @encode objc-typename
28974 Returns an encoded representation of the type argument. */
28976 static cp_expr
28977 cp_parser_objc_encode_expression (cp_parser* parser)
28979 tree type;
28980 cp_token *token;
28981 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28983 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
28984 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
28985 token = cp_lexer_peek_token (parser->lexer);
28986 type = complete_type (cp_parser_type_id (parser));
28987 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
28989 if (!type)
28991 error_at (token->location,
28992 "%<@encode%> must specify a type as an argument");
28993 return error_mark_node;
28996 /* This happens if we find @encode(T) (where T is a template
28997 typename or something dependent on a template typename) when
28998 parsing a template. In that case, we can't compile it
28999 immediately, but we rather create an AT_ENCODE_EXPR which will
29000 need to be instantiated when the template is used.
29002 if (dependent_type_p (type))
29004 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29005 TREE_READONLY (value) = 1;
29006 return value;
29010 /* Build a location of the form:
29011 @encode(int)
29012 ^~~~~~~~~~~~
29013 with caret==start at the @ token, finishing at the close paren. */
29014 location_t combined_loc
29015 = make_location (start_loc, start_loc,
29016 cp_lexer_previous_token (parser->lexer)->location);
29018 return cp_expr (objc_build_encode_expr (type), combined_loc);
29021 /* Parse an Objective-C @defs expression. */
29023 static tree
29024 cp_parser_objc_defs_expression (cp_parser *parser)
29026 tree name;
29028 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29029 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29030 name = cp_parser_identifier (parser);
29031 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29033 return objc_get_class_ivars (name);
29036 /* Parse an Objective-C protocol expression.
29038 objc-protocol-expression:
29039 @protocol ( identifier )
29041 Returns a representation of the protocol expression. */
29043 static tree
29044 cp_parser_objc_protocol_expression (cp_parser* parser)
29046 tree proto;
29047 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29049 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29050 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29051 proto = cp_parser_identifier (parser);
29052 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29054 /* Build a location of the form:
29055 @protocol(prot)
29056 ^~~~~~~~~~~~~~~
29057 with caret==start at the @ token, finishing at the close paren. */
29058 location_t combined_loc
29059 = make_location (start_loc, start_loc,
29060 cp_lexer_previous_token (parser->lexer)->location);
29061 tree result = objc_build_protocol_expr (proto);
29062 protected_set_expr_location (result, combined_loc);
29063 return result;
29066 /* Parse an Objective-C selector expression.
29068 objc-selector-expression:
29069 @selector ( objc-method-signature )
29071 objc-method-signature:
29072 objc-selector
29073 objc-selector-seq
29075 objc-selector-seq:
29076 objc-selector :
29077 objc-selector-seq objc-selector :
29079 Returns a representation of the method selector. */
29081 static tree
29082 cp_parser_objc_selector_expression (cp_parser* parser)
29084 tree sel_seq = NULL_TREE;
29085 bool maybe_unary_selector_p = true;
29086 cp_token *token;
29087 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29089 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29090 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29091 token = cp_lexer_peek_token (parser->lexer);
29093 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29094 || token->type == CPP_SCOPE)
29096 tree selector = NULL_TREE;
29098 if (token->type != CPP_COLON
29099 || token->type == CPP_SCOPE)
29100 selector = cp_parser_objc_selector (parser);
29102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29103 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29105 /* Detect if we have a unary selector. */
29106 if (maybe_unary_selector_p)
29108 sel_seq = selector;
29109 goto finish_selector;
29111 else
29113 cp_parser_error (parser, "expected %<:%>");
29116 maybe_unary_selector_p = false;
29117 token = cp_lexer_consume_token (parser->lexer);
29119 if (token->type == CPP_SCOPE)
29121 sel_seq
29122 = chainon (sel_seq,
29123 build_tree_list (selector, NULL_TREE));
29124 sel_seq
29125 = chainon (sel_seq,
29126 build_tree_list (NULL_TREE, NULL_TREE));
29128 else
29129 sel_seq
29130 = chainon (sel_seq,
29131 build_tree_list (selector, NULL_TREE));
29133 token = cp_lexer_peek_token (parser->lexer);
29136 finish_selector:
29137 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29140 /* Build a location of the form:
29141 @selector(func)
29142 ^~~~~~~~~~~~~~~
29143 with caret==start at the @ token, finishing at the close paren. */
29144 location_t combined_loc
29145 = make_location (loc, loc,
29146 cp_lexer_previous_token (parser->lexer)->location);
29147 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29148 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29149 protected_set_expr_location (result, combined_loc);
29150 return result;
29153 /* Parse a list of identifiers.
29155 objc-identifier-list:
29156 identifier
29157 objc-identifier-list , identifier
29159 Returns a TREE_LIST of identifier nodes. */
29161 static tree
29162 cp_parser_objc_identifier_list (cp_parser* parser)
29164 tree identifier;
29165 tree list;
29166 cp_token *sep;
29168 identifier = cp_parser_identifier (parser);
29169 if (identifier == error_mark_node)
29170 return error_mark_node;
29172 list = build_tree_list (NULL_TREE, identifier);
29173 sep = cp_lexer_peek_token (parser->lexer);
29175 while (sep->type == CPP_COMMA)
29177 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29178 identifier = cp_parser_identifier (parser);
29179 if (identifier == error_mark_node)
29180 return list;
29182 list = chainon (list, build_tree_list (NULL_TREE,
29183 identifier));
29184 sep = cp_lexer_peek_token (parser->lexer);
29187 return list;
29190 /* Parse an Objective-C alias declaration.
29192 objc-alias-declaration:
29193 @compatibility_alias identifier identifier ;
29195 This function registers the alias mapping with the Objective-C front end.
29196 It returns nothing. */
29198 static void
29199 cp_parser_objc_alias_declaration (cp_parser* parser)
29201 tree alias, orig;
29203 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29204 alias = cp_parser_identifier (parser);
29205 orig = cp_parser_identifier (parser);
29206 objc_declare_alias (alias, orig);
29207 cp_parser_consume_semicolon_at_end_of_statement (parser);
29210 /* Parse an Objective-C class forward-declaration.
29212 objc-class-declaration:
29213 @class objc-identifier-list ;
29215 The function registers the forward declarations with the Objective-C
29216 front end. It returns nothing. */
29218 static void
29219 cp_parser_objc_class_declaration (cp_parser* parser)
29221 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29222 while (true)
29224 tree id;
29226 id = cp_parser_identifier (parser);
29227 if (id == error_mark_node)
29228 break;
29230 objc_declare_class (id);
29232 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29233 cp_lexer_consume_token (parser->lexer);
29234 else
29235 break;
29237 cp_parser_consume_semicolon_at_end_of_statement (parser);
29240 /* Parse a list of Objective-C protocol references.
29242 objc-protocol-refs-opt:
29243 objc-protocol-refs [opt]
29245 objc-protocol-refs:
29246 < objc-identifier-list >
29248 Returns a TREE_LIST of identifiers, if any. */
29250 static tree
29251 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29253 tree protorefs = NULL_TREE;
29255 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29257 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29258 protorefs = cp_parser_objc_identifier_list (parser);
29259 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29262 return protorefs;
29265 /* Parse a Objective-C visibility specification. */
29267 static void
29268 cp_parser_objc_visibility_spec (cp_parser* parser)
29270 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29272 switch (vis->keyword)
29274 case RID_AT_PRIVATE:
29275 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29276 break;
29277 case RID_AT_PROTECTED:
29278 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29279 break;
29280 case RID_AT_PUBLIC:
29281 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29282 break;
29283 case RID_AT_PACKAGE:
29284 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29285 break;
29286 default:
29287 return;
29290 /* Eat '@private'/'@protected'/'@public'. */
29291 cp_lexer_consume_token (parser->lexer);
29294 /* Parse an Objective-C method type. Return 'true' if it is a class
29295 (+) method, and 'false' if it is an instance (-) method. */
29297 static inline bool
29298 cp_parser_objc_method_type (cp_parser* parser)
29300 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29301 return true;
29302 else
29303 return false;
29306 /* Parse an Objective-C protocol qualifier. */
29308 static tree
29309 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29311 tree quals = NULL_TREE, node;
29312 cp_token *token = cp_lexer_peek_token (parser->lexer);
29314 node = token->u.value;
29316 while (node && identifier_p (node)
29317 && (node == ridpointers [(int) RID_IN]
29318 || node == ridpointers [(int) RID_OUT]
29319 || node == ridpointers [(int) RID_INOUT]
29320 || node == ridpointers [(int) RID_BYCOPY]
29321 || node == ridpointers [(int) RID_BYREF]
29322 || node == ridpointers [(int) RID_ONEWAY]))
29324 quals = tree_cons (NULL_TREE, node, quals);
29325 cp_lexer_consume_token (parser->lexer);
29326 token = cp_lexer_peek_token (parser->lexer);
29327 node = token->u.value;
29330 return quals;
29333 /* Parse an Objective-C typename. */
29335 static tree
29336 cp_parser_objc_typename (cp_parser* parser)
29338 tree type_name = NULL_TREE;
29340 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29342 tree proto_quals, cp_type = NULL_TREE;
29344 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29345 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29347 /* An ObjC type name may consist of just protocol qualifiers, in which
29348 case the type shall default to 'id'. */
29349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29351 cp_type = cp_parser_type_id (parser);
29353 /* If the type could not be parsed, an error has already
29354 been produced. For error recovery, behave as if it had
29355 not been specified, which will use the default type
29356 'id'. */
29357 if (cp_type == error_mark_node)
29359 cp_type = NULL_TREE;
29360 /* We need to skip to the closing parenthesis as
29361 cp_parser_type_id() does not seem to do it for
29362 us. */
29363 cp_parser_skip_to_closing_parenthesis (parser,
29364 /*recovering=*/true,
29365 /*or_comma=*/false,
29366 /*consume_paren=*/false);
29370 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29371 type_name = build_tree_list (proto_quals, cp_type);
29374 return type_name;
29377 /* Check to see if TYPE refers to an Objective-C selector name. */
29379 static bool
29380 cp_parser_objc_selector_p (enum cpp_ttype type)
29382 return (type == CPP_NAME || type == CPP_KEYWORD
29383 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29384 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29385 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29386 || type == CPP_XOR || type == CPP_XOR_EQ);
29389 /* Parse an Objective-C selector. */
29391 static tree
29392 cp_parser_objc_selector (cp_parser* parser)
29394 cp_token *token = cp_lexer_consume_token (parser->lexer);
29396 if (!cp_parser_objc_selector_p (token->type))
29398 error_at (token->location, "invalid Objective-C++ selector name");
29399 return error_mark_node;
29402 /* C++ operator names are allowed to appear in ObjC selectors. */
29403 switch (token->type)
29405 case CPP_AND_AND: return get_identifier ("and");
29406 case CPP_AND_EQ: return get_identifier ("and_eq");
29407 case CPP_AND: return get_identifier ("bitand");
29408 case CPP_OR: return get_identifier ("bitor");
29409 case CPP_COMPL: return get_identifier ("compl");
29410 case CPP_NOT: return get_identifier ("not");
29411 case CPP_NOT_EQ: return get_identifier ("not_eq");
29412 case CPP_OR_OR: return get_identifier ("or");
29413 case CPP_OR_EQ: return get_identifier ("or_eq");
29414 case CPP_XOR: return get_identifier ("xor");
29415 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29416 default: return token->u.value;
29420 /* Parse an Objective-C params list. */
29422 static tree
29423 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29425 tree params = NULL_TREE;
29426 bool maybe_unary_selector_p = true;
29427 cp_token *token = cp_lexer_peek_token (parser->lexer);
29429 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29431 tree selector = NULL_TREE, type_name, identifier;
29432 tree parm_attr = NULL_TREE;
29434 if (token->keyword == RID_ATTRIBUTE)
29435 break;
29437 if (token->type != CPP_COLON)
29438 selector = cp_parser_objc_selector (parser);
29440 /* Detect if we have a unary selector. */
29441 if (maybe_unary_selector_p
29442 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29444 params = selector; /* Might be followed by attributes. */
29445 break;
29448 maybe_unary_selector_p = false;
29449 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29451 /* Something went quite wrong. There should be a colon
29452 here, but there is not. Stop parsing parameters. */
29453 break;
29455 type_name = cp_parser_objc_typename (parser);
29456 /* New ObjC allows attributes on parameters too. */
29457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29458 parm_attr = cp_parser_attributes_opt (parser);
29459 identifier = cp_parser_identifier (parser);
29461 params
29462 = chainon (params,
29463 objc_build_keyword_decl (selector,
29464 type_name,
29465 identifier,
29466 parm_attr));
29468 token = cp_lexer_peek_token (parser->lexer);
29471 if (params == NULL_TREE)
29473 cp_parser_error (parser, "objective-c++ method declaration is expected");
29474 return error_mark_node;
29477 /* We allow tail attributes for the method. */
29478 if (token->keyword == RID_ATTRIBUTE)
29480 *attributes = cp_parser_attributes_opt (parser);
29481 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29482 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29483 return params;
29484 cp_parser_error (parser,
29485 "method attributes must be specified at the end");
29486 return error_mark_node;
29489 if (params == NULL_TREE)
29491 cp_parser_error (parser, "objective-c++ method declaration is expected");
29492 return error_mark_node;
29494 return params;
29497 /* Parse the non-keyword Objective-C params. */
29499 static tree
29500 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29501 tree* attributes)
29503 tree params = make_node (TREE_LIST);
29504 cp_token *token = cp_lexer_peek_token (parser->lexer);
29505 *ellipsisp = false; /* Initially, assume no ellipsis. */
29507 while (token->type == CPP_COMMA)
29509 cp_parameter_declarator *parmdecl;
29510 tree parm;
29512 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29513 token = cp_lexer_peek_token (parser->lexer);
29515 if (token->type == CPP_ELLIPSIS)
29517 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29518 *ellipsisp = true;
29519 token = cp_lexer_peek_token (parser->lexer);
29520 break;
29523 /* TODO: parse attributes for tail parameters. */
29524 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29525 parm = grokdeclarator (parmdecl->declarator,
29526 &parmdecl->decl_specifiers,
29527 PARM, /*initialized=*/0,
29528 /*attrlist=*/NULL);
29530 chainon (params, build_tree_list (NULL_TREE, parm));
29531 token = cp_lexer_peek_token (parser->lexer);
29534 /* We allow tail attributes for the method. */
29535 if (token->keyword == RID_ATTRIBUTE)
29537 if (*attributes == NULL_TREE)
29539 *attributes = cp_parser_attributes_opt (parser);
29540 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29541 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29542 return params;
29544 else
29545 /* We have an error, but parse the attributes, so that we can
29546 carry on. */
29547 *attributes = cp_parser_attributes_opt (parser);
29549 cp_parser_error (parser,
29550 "method attributes must be specified at the end");
29551 return error_mark_node;
29554 return params;
29557 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29559 static void
29560 cp_parser_objc_interstitial_code (cp_parser* parser)
29562 cp_token *token = cp_lexer_peek_token (parser->lexer);
29564 /* If the next token is `extern' and the following token is a string
29565 literal, then we have a linkage specification. */
29566 if (token->keyword == RID_EXTERN
29567 && cp_parser_is_pure_string_literal
29568 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29569 cp_parser_linkage_specification (parser);
29570 /* Handle #pragma, if any. */
29571 else if (token->type == CPP_PRAGMA)
29572 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29573 /* Allow stray semicolons. */
29574 else if (token->type == CPP_SEMICOLON)
29575 cp_lexer_consume_token (parser->lexer);
29576 /* Mark methods as optional or required, when building protocols. */
29577 else if (token->keyword == RID_AT_OPTIONAL)
29579 cp_lexer_consume_token (parser->lexer);
29580 objc_set_method_opt (true);
29582 else if (token->keyword == RID_AT_REQUIRED)
29584 cp_lexer_consume_token (parser->lexer);
29585 objc_set_method_opt (false);
29587 else if (token->keyword == RID_NAMESPACE)
29588 cp_parser_namespace_definition (parser);
29589 /* Other stray characters must generate errors. */
29590 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29592 cp_lexer_consume_token (parser->lexer);
29593 error ("stray %qs between Objective-C++ methods",
29594 token->type == CPP_OPEN_BRACE ? "{" : "}");
29596 /* Finally, try to parse a block-declaration, or a function-definition. */
29597 else
29598 cp_parser_block_declaration (parser, /*statement_p=*/false);
29601 /* Parse a method signature. */
29603 static tree
29604 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29606 tree rettype, kwdparms, optparms;
29607 bool ellipsis = false;
29608 bool is_class_method;
29610 is_class_method = cp_parser_objc_method_type (parser);
29611 rettype = cp_parser_objc_typename (parser);
29612 *attributes = NULL_TREE;
29613 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29614 if (kwdparms == error_mark_node)
29615 return error_mark_node;
29616 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29617 if (optparms == error_mark_node)
29618 return error_mark_node;
29620 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29623 static bool
29624 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29626 tree tattr;
29627 cp_lexer_save_tokens (parser->lexer);
29628 tattr = cp_parser_attributes_opt (parser);
29629 gcc_assert (tattr) ;
29631 /* If the attributes are followed by a method introducer, this is not allowed.
29632 Dump the attributes and flag the situation. */
29633 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29634 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29635 return true;
29637 /* Otherwise, the attributes introduce some interstitial code, possibly so
29638 rewind to allow that check. */
29639 cp_lexer_rollback_tokens (parser->lexer);
29640 return false;
29643 /* Parse an Objective-C method prototype list. */
29645 static void
29646 cp_parser_objc_method_prototype_list (cp_parser* parser)
29648 cp_token *token = cp_lexer_peek_token (parser->lexer);
29650 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29652 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29654 tree attributes, sig;
29655 bool is_class_method;
29656 if (token->type == CPP_PLUS)
29657 is_class_method = true;
29658 else
29659 is_class_method = false;
29660 sig = cp_parser_objc_method_signature (parser, &attributes);
29661 if (sig == error_mark_node)
29663 cp_parser_skip_to_end_of_block_or_statement (parser);
29664 token = cp_lexer_peek_token (parser->lexer);
29665 continue;
29667 objc_add_method_declaration (is_class_method, sig, attributes);
29668 cp_parser_consume_semicolon_at_end_of_statement (parser);
29670 else if (token->keyword == RID_AT_PROPERTY)
29671 cp_parser_objc_at_property_declaration (parser);
29672 else if (token->keyword == RID_ATTRIBUTE
29673 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29674 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29675 OPT_Wattributes,
29676 "prefix attributes are ignored for methods");
29677 else
29678 /* Allow for interspersed non-ObjC++ code. */
29679 cp_parser_objc_interstitial_code (parser);
29681 token = cp_lexer_peek_token (parser->lexer);
29684 if (token->type != CPP_EOF)
29685 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29686 else
29687 cp_parser_error (parser, "expected %<@end%>");
29689 objc_finish_interface ();
29692 /* Parse an Objective-C method definition list. */
29694 static void
29695 cp_parser_objc_method_definition_list (cp_parser* parser)
29697 cp_token *token = cp_lexer_peek_token (parser->lexer);
29699 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29701 tree meth;
29703 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29705 cp_token *ptk;
29706 tree sig, attribute;
29707 bool is_class_method;
29708 if (token->type == CPP_PLUS)
29709 is_class_method = true;
29710 else
29711 is_class_method = false;
29712 push_deferring_access_checks (dk_deferred);
29713 sig = cp_parser_objc_method_signature (parser, &attribute);
29714 if (sig == error_mark_node)
29716 cp_parser_skip_to_end_of_block_or_statement (parser);
29717 token = cp_lexer_peek_token (parser->lexer);
29718 continue;
29720 objc_start_method_definition (is_class_method, sig, attribute,
29721 NULL_TREE);
29723 /* For historical reasons, we accept an optional semicolon. */
29724 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29725 cp_lexer_consume_token (parser->lexer);
29727 ptk = cp_lexer_peek_token (parser->lexer);
29728 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29729 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29731 perform_deferred_access_checks (tf_warning_or_error);
29732 stop_deferring_access_checks ();
29733 meth = cp_parser_function_definition_after_declarator (parser,
29734 false);
29735 pop_deferring_access_checks ();
29736 objc_finish_method_definition (meth);
29739 /* The following case will be removed once @synthesize is
29740 completely implemented. */
29741 else if (token->keyword == RID_AT_PROPERTY)
29742 cp_parser_objc_at_property_declaration (parser);
29743 else if (token->keyword == RID_AT_SYNTHESIZE)
29744 cp_parser_objc_at_synthesize_declaration (parser);
29745 else if (token->keyword == RID_AT_DYNAMIC)
29746 cp_parser_objc_at_dynamic_declaration (parser);
29747 else if (token->keyword == RID_ATTRIBUTE
29748 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29749 warning_at (token->location, OPT_Wattributes,
29750 "prefix attributes are ignored for methods");
29751 else
29752 /* Allow for interspersed non-ObjC++ code. */
29753 cp_parser_objc_interstitial_code (parser);
29755 token = cp_lexer_peek_token (parser->lexer);
29758 if (token->type != CPP_EOF)
29759 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29760 else
29761 cp_parser_error (parser, "expected %<@end%>");
29763 objc_finish_implementation ();
29766 /* Parse Objective-C ivars. */
29768 static void
29769 cp_parser_objc_class_ivars (cp_parser* parser)
29771 cp_token *token = cp_lexer_peek_token (parser->lexer);
29773 if (token->type != CPP_OPEN_BRACE)
29774 return; /* No ivars specified. */
29776 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29777 token = cp_lexer_peek_token (parser->lexer);
29779 while (token->type != CPP_CLOSE_BRACE
29780 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29782 cp_decl_specifier_seq declspecs;
29783 int decl_class_or_enum_p;
29784 tree prefix_attributes;
29786 cp_parser_objc_visibility_spec (parser);
29788 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29789 break;
29791 cp_parser_decl_specifier_seq (parser,
29792 CP_PARSER_FLAGS_OPTIONAL,
29793 &declspecs,
29794 &decl_class_or_enum_p);
29796 /* auto, register, static, extern, mutable. */
29797 if (declspecs.storage_class != sc_none)
29799 cp_parser_error (parser, "invalid type for instance variable");
29800 declspecs.storage_class = sc_none;
29803 /* thread_local. */
29804 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29806 cp_parser_error (parser, "invalid type for instance variable");
29807 declspecs.locations[ds_thread] = 0;
29810 /* typedef. */
29811 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29813 cp_parser_error (parser, "invalid type for instance variable");
29814 declspecs.locations[ds_typedef] = 0;
29817 prefix_attributes = declspecs.attributes;
29818 declspecs.attributes = NULL_TREE;
29820 /* Keep going until we hit the `;' at the end of the
29821 declaration. */
29822 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29824 tree width = NULL_TREE, attributes, first_attribute, decl;
29825 cp_declarator *declarator = NULL;
29826 int ctor_dtor_or_conv_p;
29828 /* Check for a (possibly unnamed) bitfield declaration. */
29829 token = cp_lexer_peek_token (parser->lexer);
29830 if (token->type == CPP_COLON)
29831 goto eat_colon;
29833 if (token->type == CPP_NAME
29834 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29835 == CPP_COLON))
29837 /* Get the name of the bitfield. */
29838 declarator = make_id_declarator (NULL_TREE,
29839 cp_parser_identifier (parser),
29840 sfk_none);
29842 eat_colon:
29843 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29844 /* Get the width of the bitfield. */
29845 width
29846 = cp_parser_constant_expression (parser);
29848 else
29850 /* Parse the declarator. */
29851 declarator
29852 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29853 &ctor_dtor_or_conv_p,
29854 /*parenthesized_p=*/NULL,
29855 /*member_p=*/false,
29856 /*friend_p=*/false);
29859 /* Look for attributes that apply to the ivar. */
29860 attributes = cp_parser_attributes_opt (parser);
29861 /* Remember which attributes are prefix attributes and
29862 which are not. */
29863 first_attribute = attributes;
29864 /* Combine the attributes. */
29865 attributes = chainon (prefix_attributes, attributes);
29867 if (width)
29868 /* Create the bitfield declaration. */
29869 decl = grokbitfield (declarator, &declspecs,
29870 width,
29871 attributes);
29872 else
29873 decl = grokfield (declarator, &declspecs,
29874 NULL_TREE, /*init_const_expr_p=*/false,
29875 NULL_TREE, attributes);
29877 /* Add the instance variable. */
29878 if (decl != error_mark_node && decl != NULL_TREE)
29879 objc_add_instance_variable (decl);
29881 /* Reset PREFIX_ATTRIBUTES. */
29882 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29883 attributes = TREE_CHAIN (attributes);
29884 if (attributes)
29885 TREE_CHAIN (attributes) = NULL_TREE;
29887 token = cp_lexer_peek_token (parser->lexer);
29889 if (token->type == CPP_COMMA)
29891 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29892 continue;
29894 break;
29897 cp_parser_consume_semicolon_at_end_of_statement (parser);
29898 token = cp_lexer_peek_token (parser->lexer);
29901 if (token->keyword == RID_AT_END)
29902 cp_parser_error (parser, "expected %<}%>");
29904 /* Do not consume the RID_AT_END, so it will be read again as terminating
29905 the @interface of @implementation. */
29906 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29907 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29909 /* For historical reasons, we accept an optional semicolon. */
29910 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29911 cp_lexer_consume_token (parser->lexer);
29914 /* Parse an Objective-C protocol declaration. */
29916 static void
29917 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29919 tree proto, protorefs;
29920 cp_token *tok;
29922 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29925 tok = cp_lexer_peek_token (parser->lexer);
29926 error_at (tok->location, "identifier expected after %<@protocol%>");
29927 cp_parser_consume_semicolon_at_end_of_statement (parser);
29928 return;
29931 /* See if we have a forward declaration or a definition. */
29932 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29934 /* Try a forward declaration first. */
29935 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29937 while (true)
29939 tree id;
29941 id = cp_parser_identifier (parser);
29942 if (id == error_mark_node)
29943 break;
29945 objc_declare_protocol (id, attributes);
29947 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29948 cp_lexer_consume_token (parser->lexer);
29949 else
29950 break;
29952 cp_parser_consume_semicolon_at_end_of_statement (parser);
29955 /* Ok, we got a full-fledged definition (or at least should). */
29956 else
29958 proto = cp_parser_identifier (parser);
29959 protorefs = cp_parser_objc_protocol_refs_opt (parser);
29960 objc_start_protocol (proto, protorefs, attributes);
29961 cp_parser_objc_method_prototype_list (parser);
29965 /* Parse an Objective-C superclass or category. */
29967 static void
29968 cp_parser_objc_superclass_or_category (cp_parser *parser,
29969 bool iface_p,
29970 tree *super,
29971 tree *categ, bool *is_class_extension)
29973 cp_token *next = cp_lexer_peek_token (parser->lexer);
29975 *super = *categ = NULL_TREE;
29976 *is_class_extension = false;
29977 if (next->type == CPP_COLON)
29979 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29980 *super = cp_parser_identifier (parser);
29982 else if (next->type == CPP_OPEN_PAREN)
29984 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29986 /* If there is no category name, and this is an @interface, we
29987 have a class extension. */
29988 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
29990 *categ = NULL_TREE;
29991 *is_class_extension = true;
29993 else
29994 *categ = cp_parser_identifier (parser);
29996 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30000 /* Parse an Objective-C class interface. */
30002 static void
30003 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30005 tree name, super, categ, protos;
30006 bool is_class_extension;
30008 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30009 name = cp_parser_identifier (parser);
30010 if (name == error_mark_node)
30012 /* It's hard to recover because even if valid @interface stuff
30013 is to follow, we can't compile it (or validate it) if we
30014 don't even know which class it refers to. Let's assume this
30015 was a stray '@interface' token in the stream and skip it.
30017 return;
30019 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30020 &is_class_extension);
30021 protos = cp_parser_objc_protocol_refs_opt (parser);
30023 /* We have either a class or a category on our hands. */
30024 if (categ || is_class_extension)
30025 objc_start_category_interface (name, categ, protos, attributes);
30026 else
30028 objc_start_class_interface (name, super, protos, attributes);
30029 /* Handle instance variable declarations, if any. */
30030 cp_parser_objc_class_ivars (parser);
30031 objc_continue_interface ();
30034 cp_parser_objc_method_prototype_list (parser);
30037 /* Parse an Objective-C class implementation. */
30039 static void
30040 cp_parser_objc_class_implementation (cp_parser* parser)
30042 tree name, super, categ;
30043 bool is_class_extension;
30045 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30046 name = cp_parser_identifier (parser);
30047 if (name == error_mark_node)
30049 /* It's hard to recover because even if valid @implementation
30050 stuff is to follow, we can't compile it (or validate it) if
30051 we don't even know which class it refers to. Let's assume
30052 this was a stray '@implementation' token in the stream and
30053 skip it.
30055 return;
30057 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30058 &is_class_extension);
30060 /* We have either a class or a category on our hands. */
30061 if (categ)
30062 objc_start_category_implementation (name, categ);
30063 else
30065 objc_start_class_implementation (name, super);
30066 /* Handle instance variable declarations, if any. */
30067 cp_parser_objc_class_ivars (parser);
30068 objc_continue_implementation ();
30071 cp_parser_objc_method_definition_list (parser);
30074 /* Consume the @end token and finish off the implementation. */
30076 static void
30077 cp_parser_objc_end_implementation (cp_parser* parser)
30079 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30080 objc_finish_implementation ();
30083 /* Parse an Objective-C declaration. */
30085 static void
30086 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30088 /* Try to figure out what kind of declaration is present. */
30089 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30091 if (attributes)
30092 switch (kwd->keyword)
30094 case RID_AT_ALIAS:
30095 case RID_AT_CLASS:
30096 case RID_AT_END:
30097 error_at (kwd->location, "attributes may not be specified before"
30098 " the %<@%D%> Objective-C++ keyword",
30099 kwd->u.value);
30100 attributes = NULL;
30101 break;
30102 case RID_AT_IMPLEMENTATION:
30103 warning_at (kwd->location, OPT_Wattributes,
30104 "prefix attributes are ignored before %<@%D%>",
30105 kwd->u.value);
30106 attributes = NULL;
30107 default:
30108 break;
30111 switch (kwd->keyword)
30113 case RID_AT_ALIAS:
30114 cp_parser_objc_alias_declaration (parser);
30115 break;
30116 case RID_AT_CLASS:
30117 cp_parser_objc_class_declaration (parser);
30118 break;
30119 case RID_AT_PROTOCOL:
30120 cp_parser_objc_protocol_declaration (parser, attributes);
30121 break;
30122 case RID_AT_INTERFACE:
30123 cp_parser_objc_class_interface (parser, attributes);
30124 break;
30125 case RID_AT_IMPLEMENTATION:
30126 cp_parser_objc_class_implementation (parser);
30127 break;
30128 case RID_AT_END:
30129 cp_parser_objc_end_implementation (parser);
30130 break;
30131 default:
30132 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30133 kwd->u.value);
30134 cp_parser_skip_to_end_of_block_or_statement (parser);
30138 /* Parse an Objective-C try-catch-finally statement.
30140 objc-try-catch-finally-stmt:
30141 @try compound-statement objc-catch-clause-seq [opt]
30142 objc-finally-clause [opt]
30144 objc-catch-clause-seq:
30145 objc-catch-clause objc-catch-clause-seq [opt]
30147 objc-catch-clause:
30148 @catch ( objc-exception-declaration ) compound-statement
30150 objc-finally-clause:
30151 @finally compound-statement
30153 objc-exception-declaration:
30154 parameter-declaration
30155 '...'
30157 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30159 Returns NULL_TREE.
30161 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30162 for C. Keep them in sync. */
30164 static tree
30165 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30167 location_t location;
30168 tree stmt;
30170 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30171 location = cp_lexer_peek_token (parser->lexer)->location;
30172 objc_maybe_warn_exceptions (location);
30173 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30174 node, lest it get absorbed into the surrounding block. */
30175 stmt = push_stmt_list ();
30176 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30177 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30179 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30181 cp_parameter_declarator *parm;
30182 tree parameter_declaration = error_mark_node;
30183 bool seen_open_paren = false;
30185 cp_lexer_consume_token (parser->lexer);
30186 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30187 seen_open_paren = true;
30188 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30190 /* We have "@catch (...)" (where the '...' are literally
30191 what is in the code). Skip the '...'.
30192 parameter_declaration is set to NULL_TREE, and
30193 objc_being_catch_clauses() knows that that means
30194 '...'. */
30195 cp_lexer_consume_token (parser->lexer);
30196 parameter_declaration = NULL_TREE;
30198 else
30200 /* We have "@catch (NSException *exception)" or something
30201 like that. Parse the parameter declaration. */
30202 parm = cp_parser_parameter_declaration (parser, false, NULL);
30203 if (parm == NULL)
30204 parameter_declaration = error_mark_node;
30205 else
30206 parameter_declaration = grokdeclarator (parm->declarator,
30207 &parm->decl_specifiers,
30208 PARM, /*initialized=*/0,
30209 /*attrlist=*/NULL);
30211 if (seen_open_paren)
30212 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30213 else
30215 /* If there was no open parenthesis, we are recovering from
30216 an error, and we are trying to figure out what mistake
30217 the user has made. */
30219 /* If there is an immediate closing parenthesis, the user
30220 probably forgot the opening one (ie, they typed "@catch
30221 NSException *e)". Parse the closing parenthesis and keep
30222 going. */
30223 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30224 cp_lexer_consume_token (parser->lexer);
30226 /* If these is no immediate closing parenthesis, the user
30227 probably doesn't know that parenthesis are required at
30228 all (ie, they typed "@catch NSException *e"). So, just
30229 forget about the closing parenthesis and keep going. */
30231 objc_begin_catch_clause (parameter_declaration);
30232 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30233 objc_finish_catch_clause ();
30235 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30237 cp_lexer_consume_token (parser->lexer);
30238 location = cp_lexer_peek_token (parser->lexer)->location;
30239 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30240 node, lest it get absorbed into the surrounding block. */
30241 stmt = push_stmt_list ();
30242 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30243 objc_build_finally_clause (location, pop_stmt_list (stmt));
30246 return objc_finish_try_stmt ();
30249 /* Parse an Objective-C synchronized statement.
30251 objc-synchronized-stmt:
30252 @synchronized ( expression ) compound-statement
30254 Returns NULL_TREE. */
30256 static tree
30257 cp_parser_objc_synchronized_statement (cp_parser *parser)
30259 location_t location;
30260 tree lock, stmt;
30262 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30264 location = cp_lexer_peek_token (parser->lexer)->location;
30265 objc_maybe_warn_exceptions (location);
30266 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30267 lock = cp_parser_expression (parser);
30268 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30270 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30271 node, lest it get absorbed into the surrounding block. */
30272 stmt = push_stmt_list ();
30273 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30275 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30278 /* Parse an Objective-C throw statement.
30280 objc-throw-stmt:
30281 @throw assignment-expression [opt] ;
30283 Returns a constructed '@throw' statement. */
30285 static tree
30286 cp_parser_objc_throw_statement (cp_parser *parser)
30288 tree expr = NULL_TREE;
30289 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30291 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30293 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30294 expr = cp_parser_expression (parser);
30296 cp_parser_consume_semicolon_at_end_of_statement (parser);
30298 return objc_build_throw_stmt (loc, expr);
30301 /* Parse an Objective-C statement. */
30303 static tree
30304 cp_parser_objc_statement (cp_parser * parser)
30306 /* Try to figure out what kind of declaration is present. */
30307 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30309 switch (kwd->keyword)
30311 case RID_AT_TRY:
30312 return cp_parser_objc_try_catch_finally_statement (parser);
30313 case RID_AT_SYNCHRONIZED:
30314 return cp_parser_objc_synchronized_statement (parser);
30315 case RID_AT_THROW:
30316 return cp_parser_objc_throw_statement (parser);
30317 default:
30318 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30319 kwd->u.value);
30320 cp_parser_skip_to_end_of_block_or_statement (parser);
30323 return error_mark_node;
30326 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30327 look ahead to see if an objc keyword follows the attributes. This
30328 is to detect the use of prefix attributes on ObjC @interface and
30329 @protocol. */
30331 static bool
30332 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30334 cp_lexer_save_tokens (parser->lexer);
30335 *attrib = cp_parser_attributes_opt (parser);
30336 gcc_assert (*attrib);
30337 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30339 cp_lexer_commit_tokens (parser->lexer);
30340 return true;
30342 cp_lexer_rollback_tokens (parser->lexer);
30343 return false;
30346 /* This routine is a minimal replacement for
30347 c_parser_struct_declaration () used when parsing the list of
30348 types/names or ObjC++ properties. For example, when parsing the
30349 code
30351 @property (readonly) int a, b, c;
30353 this function is responsible for parsing "int a, int b, int c" and
30354 returning the declarations as CHAIN of DECLs.
30356 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30357 similar parsing. */
30358 static tree
30359 cp_parser_objc_struct_declaration (cp_parser *parser)
30361 tree decls = NULL_TREE;
30362 cp_decl_specifier_seq declspecs;
30363 int decl_class_or_enum_p;
30364 tree prefix_attributes;
30366 cp_parser_decl_specifier_seq (parser,
30367 CP_PARSER_FLAGS_NONE,
30368 &declspecs,
30369 &decl_class_or_enum_p);
30371 if (declspecs.type == error_mark_node)
30372 return error_mark_node;
30374 /* auto, register, static, extern, mutable. */
30375 if (declspecs.storage_class != sc_none)
30377 cp_parser_error (parser, "invalid type for property");
30378 declspecs.storage_class = sc_none;
30381 /* thread_local. */
30382 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30384 cp_parser_error (parser, "invalid type for property");
30385 declspecs.locations[ds_thread] = 0;
30388 /* typedef. */
30389 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30391 cp_parser_error (parser, "invalid type for property");
30392 declspecs.locations[ds_typedef] = 0;
30395 prefix_attributes = declspecs.attributes;
30396 declspecs.attributes = NULL_TREE;
30398 /* Keep going until we hit the `;' at the end of the declaration. */
30399 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30401 tree attributes, first_attribute, decl;
30402 cp_declarator *declarator;
30403 cp_token *token;
30405 /* Parse the declarator. */
30406 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30407 NULL, NULL, false, false);
30409 /* Look for attributes that apply to the ivar. */
30410 attributes = cp_parser_attributes_opt (parser);
30411 /* Remember which attributes are prefix attributes and
30412 which are not. */
30413 first_attribute = attributes;
30414 /* Combine the attributes. */
30415 attributes = chainon (prefix_attributes, attributes);
30417 decl = grokfield (declarator, &declspecs,
30418 NULL_TREE, /*init_const_expr_p=*/false,
30419 NULL_TREE, attributes);
30421 if (decl == error_mark_node || decl == NULL_TREE)
30422 return error_mark_node;
30424 /* Reset PREFIX_ATTRIBUTES. */
30425 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30426 attributes = TREE_CHAIN (attributes);
30427 if (attributes)
30428 TREE_CHAIN (attributes) = NULL_TREE;
30430 DECL_CHAIN (decl) = decls;
30431 decls = decl;
30433 token = cp_lexer_peek_token (parser->lexer);
30434 if (token->type == CPP_COMMA)
30436 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30437 continue;
30439 else
30440 break;
30442 return decls;
30445 /* Parse an Objective-C @property declaration. The syntax is:
30447 objc-property-declaration:
30448 '@property' objc-property-attributes[opt] struct-declaration ;
30450 objc-property-attributes:
30451 '(' objc-property-attribute-list ')'
30453 objc-property-attribute-list:
30454 objc-property-attribute
30455 objc-property-attribute-list, objc-property-attribute
30457 objc-property-attribute
30458 'getter' = identifier
30459 'setter' = identifier
30460 'readonly'
30461 'readwrite'
30462 'assign'
30463 'retain'
30464 'copy'
30465 'nonatomic'
30467 For example:
30468 @property NSString *name;
30469 @property (readonly) id object;
30470 @property (retain, nonatomic, getter=getTheName) id name;
30471 @property int a, b, c;
30473 PS: This function is identical to
30474 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30475 static void
30476 cp_parser_objc_at_property_declaration (cp_parser *parser)
30478 /* The following variables hold the attributes of the properties as
30479 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30480 seen. When we see an attribute, we set them to 'true' (if they
30481 are boolean properties) or to the identifier (if they have an
30482 argument, ie, for getter and setter). Note that here we only
30483 parse the list of attributes, check the syntax and accumulate the
30484 attributes that we find. objc_add_property_declaration() will
30485 then process the information. */
30486 bool property_assign = false;
30487 bool property_copy = false;
30488 tree property_getter_ident = NULL_TREE;
30489 bool property_nonatomic = false;
30490 bool property_readonly = false;
30491 bool property_readwrite = false;
30492 bool property_retain = false;
30493 tree property_setter_ident = NULL_TREE;
30495 /* 'properties' is the list of properties that we read. Usually a
30496 single one, but maybe more (eg, in "@property int a, b, c;" there
30497 are three). */
30498 tree properties;
30499 location_t loc;
30501 loc = cp_lexer_peek_token (parser->lexer)->location;
30503 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30505 /* Parse the optional attribute list... */
30506 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30508 /* Eat the '('. */
30509 cp_lexer_consume_token (parser->lexer);
30511 while (true)
30513 bool syntax_error = false;
30514 cp_token *token = cp_lexer_peek_token (parser->lexer);
30515 enum rid keyword;
30517 if (token->type != CPP_NAME)
30519 cp_parser_error (parser, "expected identifier");
30520 break;
30522 keyword = C_RID_CODE (token->u.value);
30523 cp_lexer_consume_token (parser->lexer);
30524 switch (keyword)
30526 case RID_ASSIGN: property_assign = true; break;
30527 case RID_COPY: property_copy = true; break;
30528 case RID_NONATOMIC: property_nonatomic = true; break;
30529 case RID_READONLY: property_readonly = true; break;
30530 case RID_READWRITE: property_readwrite = true; break;
30531 case RID_RETAIN: property_retain = true; break;
30533 case RID_GETTER:
30534 case RID_SETTER:
30535 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30537 if (keyword == RID_GETTER)
30538 cp_parser_error (parser,
30539 "missing %<=%> (after %<getter%> attribute)");
30540 else
30541 cp_parser_error (parser,
30542 "missing %<=%> (after %<setter%> attribute)");
30543 syntax_error = true;
30544 break;
30546 cp_lexer_consume_token (parser->lexer); /* eat the = */
30547 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30549 cp_parser_error (parser, "expected identifier");
30550 syntax_error = true;
30551 break;
30553 if (keyword == RID_SETTER)
30555 if (property_setter_ident != NULL_TREE)
30557 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30558 cp_lexer_consume_token (parser->lexer);
30560 else
30561 property_setter_ident = cp_parser_objc_selector (parser);
30562 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30563 cp_parser_error (parser, "setter name must terminate with %<:%>");
30564 else
30565 cp_lexer_consume_token (parser->lexer);
30567 else
30569 if (property_getter_ident != NULL_TREE)
30571 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30572 cp_lexer_consume_token (parser->lexer);
30574 else
30575 property_getter_ident = cp_parser_objc_selector (parser);
30577 break;
30578 default:
30579 cp_parser_error (parser, "unknown property attribute");
30580 syntax_error = true;
30581 break;
30584 if (syntax_error)
30585 break;
30587 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30588 cp_lexer_consume_token (parser->lexer);
30589 else
30590 break;
30593 /* FIXME: "@property (setter, assign);" will generate a spurious
30594 "error: expected ‘)’ before ‘,’ token". This is because
30595 cp_parser_require, unlike the C counterpart, will produce an
30596 error even if we are in error recovery. */
30597 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30599 cp_parser_skip_to_closing_parenthesis (parser,
30600 /*recovering=*/true,
30601 /*or_comma=*/false,
30602 /*consume_paren=*/true);
30606 /* ... and the property declaration(s). */
30607 properties = cp_parser_objc_struct_declaration (parser);
30609 if (properties == error_mark_node)
30611 cp_parser_skip_to_end_of_statement (parser);
30612 /* If the next token is now a `;', consume it. */
30613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30614 cp_lexer_consume_token (parser->lexer);
30615 return;
30618 if (properties == NULL_TREE)
30619 cp_parser_error (parser, "expected identifier");
30620 else
30622 /* Comma-separated properties are chained together in
30623 reverse order; add them one by one. */
30624 properties = nreverse (properties);
30626 for (; properties; properties = TREE_CHAIN (properties))
30627 objc_add_property_declaration (loc, copy_node (properties),
30628 property_readonly, property_readwrite,
30629 property_assign, property_retain,
30630 property_copy, property_nonatomic,
30631 property_getter_ident, property_setter_ident);
30634 cp_parser_consume_semicolon_at_end_of_statement (parser);
30637 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30639 objc-synthesize-declaration:
30640 @synthesize objc-synthesize-identifier-list ;
30642 objc-synthesize-identifier-list:
30643 objc-synthesize-identifier
30644 objc-synthesize-identifier-list, objc-synthesize-identifier
30646 objc-synthesize-identifier
30647 identifier
30648 identifier = identifier
30650 For example:
30651 @synthesize MyProperty;
30652 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30654 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30655 for C. Keep them in sync.
30657 static void
30658 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30660 tree list = NULL_TREE;
30661 location_t loc;
30662 loc = cp_lexer_peek_token (parser->lexer)->location;
30664 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30665 while (true)
30667 tree property, ivar;
30668 property = cp_parser_identifier (parser);
30669 if (property == error_mark_node)
30671 cp_parser_consume_semicolon_at_end_of_statement (parser);
30672 return;
30674 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30676 cp_lexer_consume_token (parser->lexer);
30677 ivar = cp_parser_identifier (parser);
30678 if (ivar == error_mark_node)
30680 cp_parser_consume_semicolon_at_end_of_statement (parser);
30681 return;
30684 else
30685 ivar = NULL_TREE;
30686 list = chainon (list, build_tree_list (ivar, property));
30687 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30688 cp_lexer_consume_token (parser->lexer);
30689 else
30690 break;
30692 cp_parser_consume_semicolon_at_end_of_statement (parser);
30693 objc_add_synthesize_declaration (loc, list);
30696 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30698 objc-dynamic-declaration:
30699 @dynamic identifier-list ;
30701 For example:
30702 @dynamic MyProperty;
30703 @dynamic MyProperty, AnotherProperty;
30705 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30706 for C. Keep them in sync.
30708 static void
30709 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30711 tree list = NULL_TREE;
30712 location_t loc;
30713 loc = cp_lexer_peek_token (parser->lexer)->location;
30715 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30716 while (true)
30718 tree property;
30719 property = cp_parser_identifier (parser);
30720 if (property == error_mark_node)
30722 cp_parser_consume_semicolon_at_end_of_statement (parser);
30723 return;
30725 list = chainon (list, build_tree_list (NULL, property));
30726 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30727 cp_lexer_consume_token (parser->lexer);
30728 else
30729 break;
30731 cp_parser_consume_semicolon_at_end_of_statement (parser);
30732 objc_add_dynamic_declaration (loc, list);
30736 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30738 /* Returns name of the next clause.
30739 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30740 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30741 returned and the token is consumed. */
30743 static pragma_omp_clause
30744 cp_parser_omp_clause_name (cp_parser *parser)
30746 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30749 result = PRAGMA_OACC_CLAUSE_AUTO;
30750 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30751 result = PRAGMA_OMP_CLAUSE_IF;
30752 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30753 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30754 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30755 result = PRAGMA_OACC_CLAUSE_DELETE;
30756 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30757 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30758 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30759 result = PRAGMA_OMP_CLAUSE_FOR;
30760 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30762 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30763 const char *p = IDENTIFIER_POINTER (id);
30765 switch (p[0])
30767 case 'a':
30768 if (!strcmp ("aligned", p))
30769 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30770 else if (!strcmp ("async", p))
30771 result = PRAGMA_OACC_CLAUSE_ASYNC;
30772 break;
30773 case 'c':
30774 if (!strcmp ("collapse", p))
30775 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30776 else if (!strcmp ("copy", p))
30777 result = PRAGMA_OACC_CLAUSE_COPY;
30778 else if (!strcmp ("copyin", p))
30779 result = PRAGMA_OMP_CLAUSE_COPYIN;
30780 else if (!strcmp ("copyout", p))
30781 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30782 else if (!strcmp ("copyprivate", p))
30783 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30784 else if (!strcmp ("create", p))
30785 result = PRAGMA_OACC_CLAUSE_CREATE;
30786 break;
30787 case 'd':
30788 if (!strcmp ("defaultmap", p))
30789 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30790 else if (!strcmp ("depend", p))
30791 result = PRAGMA_OMP_CLAUSE_DEPEND;
30792 else if (!strcmp ("device", p))
30793 result = PRAGMA_OMP_CLAUSE_DEVICE;
30794 else if (!strcmp ("deviceptr", p))
30795 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30796 else if (!strcmp ("device_resident", p))
30797 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30798 else if (!strcmp ("dist_schedule", p))
30799 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30800 break;
30801 case 'f':
30802 if (!strcmp ("final", p))
30803 result = PRAGMA_OMP_CLAUSE_FINAL;
30804 else if (!strcmp ("firstprivate", p))
30805 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30806 else if (!strcmp ("from", p))
30807 result = PRAGMA_OMP_CLAUSE_FROM;
30808 break;
30809 case 'g':
30810 if (!strcmp ("gang", p))
30811 result = PRAGMA_OACC_CLAUSE_GANG;
30812 else if (!strcmp ("grainsize", p))
30813 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30814 break;
30815 case 'h':
30816 if (!strcmp ("hint", p))
30817 result = PRAGMA_OMP_CLAUSE_HINT;
30818 else if (!strcmp ("host", p))
30819 result = PRAGMA_OACC_CLAUSE_HOST;
30820 break;
30821 case 'i':
30822 if (!strcmp ("inbranch", p))
30823 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30824 else if (!strcmp ("independent", p))
30825 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30826 else if (!strcmp ("is_device_ptr", p))
30827 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30828 break;
30829 case 'l':
30830 if (!strcmp ("lastprivate", p))
30831 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30832 else if (!strcmp ("linear", p))
30833 result = PRAGMA_OMP_CLAUSE_LINEAR;
30834 else if (!strcmp ("link", p))
30835 result = PRAGMA_OMP_CLAUSE_LINK;
30836 break;
30837 case 'm':
30838 if (!strcmp ("map", p))
30839 result = PRAGMA_OMP_CLAUSE_MAP;
30840 else if (!strcmp ("mergeable", p))
30841 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30842 else if (flag_cilkplus && !strcmp ("mask", p))
30843 result = PRAGMA_CILK_CLAUSE_MASK;
30844 break;
30845 case 'n':
30846 if (!strcmp ("nogroup", p))
30847 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30848 else if (!strcmp ("notinbranch", p))
30849 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30850 else if (!strcmp ("nowait", p))
30851 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30852 else if (flag_cilkplus && !strcmp ("nomask", p))
30853 result = PRAGMA_CILK_CLAUSE_NOMASK;
30854 else if (!strcmp ("num_gangs", p))
30855 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30856 else if (!strcmp ("num_tasks", p))
30857 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30858 else if (!strcmp ("num_teams", p))
30859 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30860 else if (!strcmp ("num_threads", p))
30861 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30862 else if (!strcmp ("num_workers", p))
30863 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30864 break;
30865 case 'o':
30866 if (!strcmp ("ordered", p))
30867 result = PRAGMA_OMP_CLAUSE_ORDERED;
30868 break;
30869 case 'p':
30870 if (!strcmp ("parallel", p))
30871 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30872 else if (!strcmp ("present", p))
30873 result = PRAGMA_OACC_CLAUSE_PRESENT;
30874 else if (!strcmp ("present_or_copy", p)
30875 || !strcmp ("pcopy", p))
30876 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30877 else if (!strcmp ("present_or_copyin", p)
30878 || !strcmp ("pcopyin", p))
30879 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30880 else if (!strcmp ("present_or_copyout", p)
30881 || !strcmp ("pcopyout", p))
30882 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30883 else if (!strcmp ("present_or_create", p)
30884 || !strcmp ("pcreate", p))
30885 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30886 else if (!strcmp ("priority", p))
30887 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30888 else if (!strcmp ("proc_bind", p))
30889 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30890 break;
30891 case 'r':
30892 if (!strcmp ("reduction", p))
30893 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30894 break;
30895 case 's':
30896 if (!strcmp ("safelen", p))
30897 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30898 else if (!strcmp ("schedule", p))
30899 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30900 else if (!strcmp ("sections", p))
30901 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30902 else if (!strcmp ("self", p))
30903 result = PRAGMA_OACC_CLAUSE_SELF;
30904 else if (!strcmp ("seq", p))
30905 result = PRAGMA_OACC_CLAUSE_SEQ;
30906 else if (!strcmp ("shared", p))
30907 result = PRAGMA_OMP_CLAUSE_SHARED;
30908 else if (!strcmp ("simd", p))
30909 result = PRAGMA_OMP_CLAUSE_SIMD;
30910 else if (!strcmp ("simdlen", p))
30911 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30912 break;
30913 case 't':
30914 if (!strcmp ("taskgroup", p))
30915 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30916 else if (!strcmp ("thread_limit", p))
30917 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30918 else if (!strcmp ("threads", p))
30919 result = PRAGMA_OMP_CLAUSE_THREADS;
30920 else if (!strcmp ("tile", p))
30921 result = PRAGMA_OACC_CLAUSE_TILE;
30922 else if (!strcmp ("to", p))
30923 result = PRAGMA_OMP_CLAUSE_TO;
30924 break;
30925 case 'u':
30926 if (!strcmp ("uniform", p))
30927 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30928 else if (!strcmp ("untied", p))
30929 result = PRAGMA_OMP_CLAUSE_UNTIED;
30930 else if (!strcmp ("use_device", p))
30931 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30932 else if (!strcmp ("use_device_ptr", p))
30933 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30934 break;
30935 case 'v':
30936 if (!strcmp ("vector", p))
30937 result = PRAGMA_OACC_CLAUSE_VECTOR;
30938 else if (!strcmp ("vector_length", p))
30939 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30940 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30941 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30942 break;
30943 case 'w':
30944 if (!strcmp ("wait", p))
30945 result = PRAGMA_OACC_CLAUSE_WAIT;
30946 else if (!strcmp ("worker", p))
30947 result = PRAGMA_OACC_CLAUSE_WORKER;
30948 break;
30952 if (result != PRAGMA_OMP_CLAUSE_NONE)
30953 cp_lexer_consume_token (parser->lexer);
30955 return result;
30958 /* Validate that a clause of the given type does not already exist. */
30960 static void
30961 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
30962 const char *name, location_t location)
30964 tree c;
30966 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
30967 if (OMP_CLAUSE_CODE (c) == code)
30969 error_at (location, "too many %qs clauses", name);
30970 break;
30974 /* OpenMP 2.5:
30975 variable-list:
30976 identifier
30977 variable-list , identifier
30979 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
30980 colon). An opening parenthesis will have been consumed by the caller.
30982 If KIND is nonzero, create the appropriate node and install the decl
30983 in OMP_CLAUSE_DECL and add the node to the head of the list.
30985 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
30986 return the list created.
30988 COLON can be NULL if only closing parenthesis should end the list,
30989 or pointer to bool which will receive false if the list is terminated
30990 by closing parenthesis or true if the list is terminated by colon. */
30992 static tree
30993 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
30994 tree list, bool *colon)
30996 cp_token *token;
30997 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
30998 if (colon)
31000 parser->colon_corrects_to_scope_p = false;
31001 *colon = false;
31003 while (1)
31005 tree name, decl;
31007 token = cp_lexer_peek_token (parser->lexer);
31008 if (kind != 0
31009 && current_class_ptr
31010 && cp_parser_is_keyword (token, RID_THIS))
31012 decl = finish_this_expr ();
31013 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31014 || CONVERT_EXPR_P (decl))
31015 decl = TREE_OPERAND (decl, 0);
31016 cp_lexer_consume_token (parser->lexer);
31018 else
31020 name = cp_parser_id_expression (parser, /*template_p=*/false,
31021 /*check_dependency_p=*/true,
31022 /*template_p=*/NULL,
31023 /*declarator_p=*/false,
31024 /*optional_p=*/false);
31025 if (name == error_mark_node)
31026 goto skip_comma;
31028 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31029 if (decl == error_mark_node)
31030 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31031 token->location);
31033 if (decl == error_mark_node)
31035 else if (kind != 0)
31037 switch (kind)
31039 case OMP_CLAUSE__CACHE_:
31040 /* The OpenACC cache directive explicitly only allows "array
31041 elements or subarrays". */
31042 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31044 error_at (token->location, "expected %<[%>");
31045 decl = error_mark_node;
31046 break;
31048 /* FALLTHROUGH. */
31049 case OMP_CLAUSE_MAP:
31050 case OMP_CLAUSE_FROM:
31051 case OMP_CLAUSE_TO:
31052 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31054 location_t loc
31055 = cp_lexer_peek_token (parser->lexer)->location;
31056 cp_id_kind idk = CP_ID_KIND_NONE;
31057 cp_lexer_consume_token (parser->lexer);
31058 decl = convert_from_reference (decl);
31059 decl
31060 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31061 decl, false,
31062 &idk, loc);
31064 /* FALLTHROUGH. */
31065 case OMP_CLAUSE_DEPEND:
31066 case OMP_CLAUSE_REDUCTION:
31067 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31069 tree low_bound = NULL_TREE, length = NULL_TREE;
31071 parser->colon_corrects_to_scope_p = false;
31072 cp_lexer_consume_token (parser->lexer);
31073 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31074 low_bound = cp_parser_expression (parser);
31075 if (!colon)
31076 parser->colon_corrects_to_scope_p
31077 = saved_colon_corrects_to_scope_p;
31078 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31079 length = integer_one_node;
31080 else
31082 /* Look for `:'. */
31083 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31084 goto skip_comma;
31085 if (!cp_lexer_next_token_is (parser->lexer,
31086 CPP_CLOSE_SQUARE))
31087 length = cp_parser_expression (parser);
31089 /* Look for the closing `]'. */
31090 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31091 RT_CLOSE_SQUARE))
31092 goto skip_comma;
31094 decl = tree_cons (low_bound, length, decl);
31096 break;
31097 default:
31098 break;
31101 tree u = build_omp_clause (token->location, kind);
31102 OMP_CLAUSE_DECL (u) = decl;
31103 OMP_CLAUSE_CHAIN (u) = list;
31104 list = u;
31106 else
31107 list = tree_cons (decl, NULL_TREE, list);
31109 get_comma:
31110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31111 break;
31112 cp_lexer_consume_token (parser->lexer);
31115 if (colon)
31116 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31118 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31120 *colon = true;
31121 cp_parser_require (parser, CPP_COLON, RT_COLON);
31122 return list;
31125 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31127 int ending;
31129 /* Try to resync to an unnested comma. Copied from
31130 cp_parser_parenthesized_expression_list. */
31131 skip_comma:
31132 if (colon)
31133 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31134 ending = cp_parser_skip_to_closing_parenthesis (parser,
31135 /*recovering=*/true,
31136 /*or_comma=*/true,
31137 /*consume_paren=*/true);
31138 if (ending < 0)
31139 goto get_comma;
31142 return list;
31145 /* Similarly, but expect leading and trailing parenthesis. This is a very
31146 common case for omp clauses. */
31148 static tree
31149 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31151 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31152 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31153 return list;
31156 /* OpenACC 2.0:
31157 copy ( variable-list )
31158 copyin ( variable-list )
31159 copyout ( variable-list )
31160 create ( variable-list )
31161 delete ( variable-list )
31162 present ( variable-list )
31163 present_or_copy ( variable-list )
31164 pcopy ( variable-list )
31165 present_or_copyin ( variable-list )
31166 pcopyin ( variable-list )
31167 present_or_copyout ( variable-list )
31168 pcopyout ( variable-list )
31169 present_or_create ( variable-list )
31170 pcreate ( variable-list ) */
31172 static tree
31173 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31174 tree list)
31176 enum gomp_map_kind kind;
31177 switch (c_kind)
31179 case PRAGMA_OACC_CLAUSE_COPY:
31180 kind = GOMP_MAP_FORCE_TOFROM;
31181 break;
31182 case PRAGMA_OACC_CLAUSE_COPYIN:
31183 kind = GOMP_MAP_FORCE_TO;
31184 break;
31185 case PRAGMA_OACC_CLAUSE_COPYOUT:
31186 kind = GOMP_MAP_FORCE_FROM;
31187 break;
31188 case PRAGMA_OACC_CLAUSE_CREATE:
31189 kind = GOMP_MAP_FORCE_ALLOC;
31190 break;
31191 case PRAGMA_OACC_CLAUSE_DELETE:
31192 kind = GOMP_MAP_DELETE;
31193 break;
31194 case PRAGMA_OACC_CLAUSE_DEVICE:
31195 kind = GOMP_MAP_FORCE_TO;
31196 break;
31197 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31198 kind = GOMP_MAP_DEVICE_RESIDENT;
31199 break;
31200 case PRAGMA_OACC_CLAUSE_HOST:
31201 case PRAGMA_OACC_CLAUSE_SELF:
31202 kind = GOMP_MAP_FORCE_FROM;
31203 break;
31204 case PRAGMA_OACC_CLAUSE_LINK:
31205 kind = GOMP_MAP_LINK;
31206 break;
31207 case PRAGMA_OACC_CLAUSE_PRESENT:
31208 kind = GOMP_MAP_FORCE_PRESENT;
31209 break;
31210 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31211 kind = GOMP_MAP_TOFROM;
31212 break;
31213 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31214 kind = GOMP_MAP_TO;
31215 break;
31216 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31217 kind = GOMP_MAP_FROM;
31218 break;
31219 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31220 kind = GOMP_MAP_ALLOC;
31221 break;
31222 default:
31223 gcc_unreachable ();
31225 tree nl, c;
31226 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31228 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31229 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31231 return nl;
31234 /* OpenACC 2.0:
31235 deviceptr ( variable-list ) */
31237 static tree
31238 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31240 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31241 tree vars, t;
31243 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31244 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31245 variable-list must only allow for pointer variables. */
31246 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31247 for (t = vars; t; t = TREE_CHAIN (t))
31249 tree v = TREE_PURPOSE (t);
31250 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31251 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31252 OMP_CLAUSE_DECL (u) = v;
31253 OMP_CLAUSE_CHAIN (u) = list;
31254 list = u;
31257 return list;
31260 /* OpenACC 2.0:
31261 auto
31262 independent
31263 nohost
31264 seq */
31266 static tree
31267 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31268 enum omp_clause_code code,
31269 tree list, location_t location)
31271 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31272 tree c = build_omp_clause (location, code);
31273 OMP_CLAUSE_CHAIN (c) = list;
31274 return c;
31277 /* OpenACC:
31278 num_gangs ( expression )
31279 num_workers ( expression )
31280 vector_length ( expression ) */
31282 static tree
31283 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31284 const char *str, tree list)
31286 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31288 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31289 return list;
31291 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31293 if (t == error_mark_node
31294 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31296 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31297 /*or_comma=*/false,
31298 /*consume_paren=*/true);
31299 return list;
31302 check_no_duplicate_clause (list, code, str, loc);
31304 tree c = build_omp_clause (loc, code);
31305 OMP_CLAUSE_OPERAND (c, 0) = t;
31306 OMP_CLAUSE_CHAIN (c) = list;
31307 return c;
31310 /* OpenACC:
31312 gang [( gang-arg-list )]
31313 worker [( [num:] int-expr )]
31314 vector [( [length:] int-expr )]
31316 where gang-arg is one of:
31318 [num:] int-expr
31319 static: size-expr
31321 and size-expr may be:
31324 int-expr
31327 static tree
31328 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31329 const char *str, tree list)
31331 const char *id = "num";
31332 cp_lexer *lexer = parser->lexer;
31333 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31334 location_t loc = cp_lexer_peek_token (lexer)->location;
31336 if (kind == OMP_CLAUSE_VECTOR)
31337 id = "length";
31339 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31341 cp_lexer_consume_token (lexer);
31345 cp_token *next = cp_lexer_peek_token (lexer);
31346 int idx = 0;
31348 /* Gang static argument. */
31349 if (kind == OMP_CLAUSE_GANG
31350 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31352 cp_lexer_consume_token (lexer);
31354 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31355 goto cleanup_error;
31357 idx = 1;
31358 if (ops[idx] != NULL)
31360 cp_parser_error (parser, "too many %<static%> arguments");
31361 goto cleanup_error;
31364 /* Check for the '*' argument. */
31365 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31366 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31367 || cp_lexer_nth_token_is (parser->lexer, 2,
31368 CPP_CLOSE_PAREN)))
31370 cp_lexer_consume_token (lexer);
31371 ops[idx] = integer_minus_one_node;
31373 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31375 cp_lexer_consume_token (lexer);
31376 continue;
31378 else break;
31381 /* Worker num: argument and vector length: arguments. */
31382 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31383 && id_equal (next->u.value, id)
31384 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31386 cp_lexer_consume_token (lexer); /* id */
31387 cp_lexer_consume_token (lexer); /* ':' */
31390 /* Now collect the actual argument. */
31391 if (ops[idx] != NULL_TREE)
31393 cp_parser_error (parser, "unexpected argument");
31394 goto cleanup_error;
31397 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31398 false);
31399 if (expr == error_mark_node)
31400 goto cleanup_error;
31402 mark_exp_read (expr);
31403 ops[idx] = expr;
31405 if (kind == OMP_CLAUSE_GANG
31406 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31408 cp_lexer_consume_token (lexer);
31409 continue;
31411 break;
31413 while (1);
31415 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31416 goto cleanup_error;
31419 check_no_duplicate_clause (list, kind, str, loc);
31421 c = build_omp_clause (loc, kind);
31423 if (ops[1])
31424 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31426 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31427 OMP_CLAUSE_CHAIN (c) = list;
31429 return c;
31431 cleanup_error:
31432 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31433 return list;
31436 /* OpenACC 2.0:
31437 tile ( size-expr-list ) */
31439 static tree
31440 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31442 tree c, expr = error_mark_node;
31443 tree tile = NULL_TREE;
31445 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31446 so, but the spec authors never considered such a case and have
31447 differing opinions on what it might mean, including 'not
31448 allowed'.) */
31449 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31450 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31451 clause_loc);
31453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31454 return list;
31458 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31459 return list;
31461 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31462 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31463 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31465 cp_lexer_consume_token (parser->lexer);
31466 expr = integer_zero_node;
31468 else
31469 expr = cp_parser_constant_expression (parser);
31471 tile = tree_cons (NULL_TREE, expr, tile);
31473 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31475 /* Consume the trailing ')'. */
31476 cp_lexer_consume_token (parser->lexer);
31478 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31479 tile = nreverse (tile);
31480 OMP_CLAUSE_TILE_LIST (c) = tile;
31481 OMP_CLAUSE_CHAIN (c) = list;
31482 return c;
31485 /* OpenACC 2.0
31486 Parse wait clause or directive parameters. */
31488 static tree
31489 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31491 vec<tree, va_gc> *args;
31492 tree t, args_tree;
31494 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31495 /*cast_p=*/false,
31496 /*allow_expansion_p=*/true,
31497 /*non_constant_p=*/NULL);
31499 if (args == NULL || args->length () == 0)
31501 cp_parser_error (parser, "expected integer expression before ')'");
31502 if (args != NULL)
31503 release_tree_vector (args);
31504 return list;
31507 args_tree = build_tree_list_vec (args);
31509 release_tree_vector (args);
31511 for (t = args_tree; t; t = TREE_CHAIN (t))
31513 tree targ = TREE_VALUE (t);
31515 if (targ != error_mark_node)
31517 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31518 error ("%<wait%> expression must be integral");
31519 else
31521 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31523 mark_rvalue_use (targ);
31524 OMP_CLAUSE_DECL (c) = targ;
31525 OMP_CLAUSE_CHAIN (c) = list;
31526 list = c;
31531 return list;
31534 /* OpenACC:
31535 wait ( int-expr-list ) */
31537 static tree
31538 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31540 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31542 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31543 return list;
31545 list = cp_parser_oacc_wait_list (parser, location, list);
31547 return list;
31550 /* OpenMP 3.0:
31551 collapse ( constant-expression ) */
31553 static tree
31554 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31556 tree c, num;
31557 location_t loc;
31558 HOST_WIDE_INT n;
31560 loc = cp_lexer_peek_token (parser->lexer)->location;
31561 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31562 return list;
31564 num = cp_parser_constant_expression (parser);
31566 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31567 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31568 /*or_comma=*/false,
31569 /*consume_paren=*/true);
31571 if (num == error_mark_node)
31572 return list;
31573 num = fold_non_dependent_expr (num);
31574 if (!tree_fits_shwi_p (num)
31575 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31576 || (n = tree_to_shwi (num)) <= 0
31577 || (int) n != n)
31579 error_at (loc, "collapse argument needs positive constant integer expression");
31580 return list;
31583 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31584 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31585 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31586 OMP_CLAUSE_CHAIN (c) = list;
31587 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31589 return c;
31592 /* OpenMP 2.5:
31593 default ( none | shared )
31595 OpenACC:
31596 default ( none | present ) */
31598 static tree
31599 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31600 location_t location, bool is_oacc)
31602 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31603 tree c;
31605 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31606 return list;
31607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31610 const char *p = IDENTIFIER_POINTER (id);
31612 switch (p[0])
31614 case 'n':
31615 if (strcmp ("none", p) != 0)
31616 goto invalid_kind;
31617 kind = OMP_CLAUSE_DEFAULT_NONE;
31618 break;
31620 case 'p':
31621 if (strcmp ("present", p) != 0 || !is_oacc)
31622 goto invalid_kind;
31623 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31624 break;
31626 case 's':
31627 if (strcmp ("shared", p) != 0 || is_oacc)
31628 goto invalid_kind;
31629 kind = OMP_CLAUSE_DEFAULT_SHARED;
31630 break;
31632 default:
31633 goto invalid_kind;
31636 cp_lexer_consume_token (parser->lexer);
31638 else
31640 invalid_kind:
31641 if (is_oacc)
31642 cp_parser_error (parser, "expected %<none%> or %<present%>");
31643 else
31644 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31647 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31648 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31649 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31650 /*or_comma=*/false,
31651 /*consume_paren=*/true);
31653 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31654 return list;
31656 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31657 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31658 OMP_CLAUSE_CHAIN (c) = list;
31659 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31661 return c;
31664 /* OpenMP 3.1:
31665 final ( expression ) */
31667 static tree
31668 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31670 tree t, c;
31672 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31673 return list;
31675 t = cp_parser_condition (parser);
31677 if (t == error_mark_node
31678 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31679 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31680 /*or_comma=*/false,
31681 /*consume_paren=*/true);
31683 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31685 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31686 OMP_CLAUSE_FINAL_EXPR (c) = t;
31687 OMP_CLAUSE_CHAIN (c) = list;
31689 return c;
31692 /* OpenMP 2.5:
31693 if ( expression )
31695 OpenMP 4.5:
31696 if ( directive-name-modifier : expression )
31698 directive-name-modifier:
31699 parallel | task | taskloop | target data | target | target update
31700 | target enter data | target exit data */
31702 static tree
31703 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31704 bool is_omp)
31706 tree t, c;
31707 enum tree_code if_modifier = ERROR_MARK;
31709 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31710 return list;
31712 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31714 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31715 const char *p = IDENTIFIER_POINTER (id);
31716 int n = 2;
31718 if (strcmp ("parallel", p) == 0)
31719 if_modifier = OMP_PARALLEL;
31720 else if (strcmp ("task", p) == 0)
31721 if_modifier = OMP_TASK;
31722 else if (strcmp ("taskloop", p) == 0)
31723 if_modifier = OMP_TASKLOOP;
31724 else if (strcmp ("target", p) == 0)
31726 if_modifier = OMP_TARGET;
31727 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31729 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31730 p = IDENTIFIER_POINTER (id);
31731 if (strcmp ("data", p) == 0)
31732 if_modifier = OMP_TARGET_DATA;
31733 else if (strcmp ("update", p) == 0)
31734 if_modifier = OMP_TARGET_UPDATE;
31735 else if (strcmp ("enter", p) == 0)
31736 if_modifier = OMP_TARGET_ENTER_DATA;
31737 else if (strcmp ("exit", p) == 0)
31738 if_modifier = OMP_TARGET_EXIT_DATA;
31739 if (if_modifier != OMP_TARGET)
31740 n = 3;
31741 else
31743 location_t loc
31744 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31745 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31746 "or %<exit%>");
31747 if_modifier = ERROR_MARK;
31749 if (if_modifier == OMP_TARGET_ENTER_DATA
31750 || if_modifier == OMP_TARGET_EXIT_DATA)
31752 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31754 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31755 p = IDENTIFIER_POINTER (id);
31756 if (strcmp ("data", p) == 0)
31757 n = 4;
31759 if (n != 4)
31761 location_t loc
31762 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31763 error_at (loc, "expected %<data%>");
31764 if_modifier = ERROR_MARK;
31769 if (if_modifier != ERROR_MARK)
31771 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31773 while (n-- > 0)
31774 cp_lexer_consume_token (parser->lexer);
31776 else
31778 if (n > 2)
31780 location_t loc
31781 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31782 error_at (loc, "expected %<:%>");
31784 if_modifier = ERROR_MARK;
31789 t = cp_parser_condition (parser);
31791 if (t == error_mark_node
31792 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31793 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31794 /*or_comma=*/false,
31795 /*consume_paren=*/true);
31797 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31798 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31800 if (if_modifier != ERROR_MARK
31801 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31803 const char *p = NULL;
31804 switch (if_modifier)
31806 case OMP_PARALLEL: p = "parallel"; break;
31807 case OMP_TASK: p = "task"; break;
31808 case OMP_TASKLOOP: p = "taskloop"; break;
31809 case OMP_TARGET_DATA: p = "target data"; break;
31810 case OMP_TARGET: p = "target"; break;
31811 case OMP_TARGET_UPDATE: p = "target update"; break;
31812 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31813 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31814 default: gcc_unreachable ();
31816 error_at (location, "too many %<if%> clauses with %qs modifier",
31818 return list;
31820 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31822 if (!is_omp)
31823 error_at (location, "too many %<if%> clauses");
31824 else
31825 error_at (location, "too many %<if%> clauses without modifier");
31826 return list;
31828 else if (if_modifier == ERROR_MARK
31829 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31831 error_at (location, "if any %<if%> clause has modifier, then all "
31832 "%<if%> clauses have to use modifier");
31833 return list;
31837 c = build_omp_clause (location, OMP_CLAUSE_IF);
31838 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31839 OMP_CLAUSE_IF_EXPR (c) = t;
31840 OMP_CLAUSE_CHAIN (c) = list;
31842 return c;
31845 /* OpenMP 3.1:
31846 mergeable */
31848 static tree
31849 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31850 tree list, location_t location)
31852 tree c;
31854 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31855 location);
31857 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31858 OMP_CLAUSE_CHAIN (c) = list;
31859 return c;
31862 /* OpenMP 2.5:
31863 nowait */
31865 static tree
31866 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31867 tree list, location_t location)
31869 tree c;
31871 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31873 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31874 OMP_CLAUSE_CHAIN (c) = list;
31875 return c;
31878 /* OpenMP 2.5:
31879 num_threads ( expression ) */
31881 static tree
31882 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31883 location_t location)
31885 tree t, c;
31887 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31888 return list;
31890 t = cp_parser_expression (parser);
31892 if (t == error_mark_node
31893 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31895 /*or_comma=*/false,
31896 /*consume_paren=*/true);
31898 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31899 "num_threads", location);
31901 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31902 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31903 OMP_CLAUSE_CHAIN (c) = list;
31905 return c;
31908 /* OpenMP 4.5:
31909 num_tasks ( expression ) */
31911 static tree
31912 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31913 location_t location)
31915 tree t, c;
31917 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31918 return list;
31920 t = cp_parser_expression (parser);
31922 if (t == error_mark_node
31923 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31924 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31925 /*or_comma=*/false,
31926 /*consume_paren=*/true);
31928 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31929 "num_tasks", location);
31931 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31932 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31933 OMP_CLAUSE_CHAIN (c) = list;
31935 return c;
31938 /* OpenMP 4.5:
31939 grainsize ( expression ) */
31941 static tree
31942 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31943 location_t location)
31945 tree t, c;
31947 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31948 return list;
31950 t = cp_parser_expression (parser);
31952 if (t == error_mark_node
31953 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31954 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31955 /*or_comma=*/false,
31956 /*consume_paren=*/true);
31958 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
31959 "grainsize", location);
31961 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
31962 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
31963 OMP_CLAUSE_CHAIN (c) = list;
31965 return c;
31968 /* OpenMP 4.5:
31969 priority ( expression ) */
31971 static tree
31972 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
31973 location_t location)
31975 tree t, c;
31977 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31978 return list;
31980 t = cp_parser_expression (parser);
31982 if (t == error_mark_node
31983 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31984 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31985 /*or_comma=*/false,
31986 /*consume_paren=*/true);
31988 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
31989 "priority", location);
31991 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
31992 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
31993 OMP_CLAUSE_CHAIN (c) = list;
31995 return c;
31998 /* OpenMP 4.5:
31999 hint ( expression ) */
32001 static tree
32002 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32003 location_t location)
32005 tree t, c;
32007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32008 return list;
32010 t = cp_parser_expression (parser);
32012 if (t == error_mark_node
32013 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32014 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32015 /*or_comma=*/false,
32016 /*consume_paren=*/true);
32018 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32020 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32021 OMP_CLAUSE_HINT_EXPR (c) = t;
32022 OMP_CLAUSE_CHAIN (c) = list;
32024 return c;
32027 /* OpenMP 4.5:
32028 defaultmap ( tofrom : scalar ) */
32030 static tree
32031 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32032 location_t location)
32034 tree c, id;
32035 const char *p;
32037 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32038 return list;
32040 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32042 cp_parser_error (parser, "expected %<tofrom%>");
32043 goto out_err;
32045 id = cp_lexer_peek_token (parser->lexer)->u.value;
32046 p = IDENTIFIER_POINTER (id);
32047 if (strcmp (p, "tofrom") != 0)
32049 cp_parser_error (parser, "expected %<tofrom%>");
32050 goto out_err;
32052 cp_lexer_consume_token (parser->lexer);
32053 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32054 goto out_err;
32056 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32058 cp_parser_error (parser, "expected %<scalar%>");
32059 goto out_err;
32061 id = cp_lexer_peek_token (parser->lexer)->u.value;
32062 p = IDENTIFIER_POINTER (id);
32063 if (strcmp (p, "scalar") != 0)
32065 cp_parser_error (parser, "expected %<scalar%>");
32066 goto out_err;
32068 cp_lexer_consume_token (parser->lexer);
32069 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32070 goto out_err;
32072 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32073 location);
32075 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32076 OMP_CLAUSE_CHAIN (c) = list;
32077 return c;
32079 out_err:
32080 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32081 /*or_comma=*/false,
32082 /*consume_paren=*/true);
32083 return list;
32086 /* OpenMP 2.5:
32087 ordered
32089 OpenMP 4.5:
32090 ordered ( constant-expression ) */
32092 static tree
32093 cp_parser_omp_clause_ordered (cp_parser *parser,
32094 tree list, location_t location)
32096 tree c, num = NULL_TREE;
32097 HOST_WIDE_INT n;
32099 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32100 "ordered", location);
32102 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32104 cp_lexer_consume_token (parser->lexer);
32106 num = cp_parser_constant_expression (parser);
32108 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32109 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32110 /*or_comma=*/false,
32111 /*consume_paren=*/true);
32113 if (num == error_mark_node)
32114 return list;
32115 num = fold_non_dependent_expr (num);
32116 if (!tree_fits_shwi_p (num)
32117 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32118 || (n = tree_to_shwi (num)) <= 0
32119 || (int) n != n)
32121 error_at (location,
32122 "ordered argument needs positive constant integer "
32123 "expression");
32124 return list;
32128 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32129 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32130 OMP_CLAUSE_CHAIN (c) = list;
32131 return c;
32134 /* OpenMP 2.5:
32135 reduction ( reduction-operator : variable-list )
32137 reduction-operator:
32138 One of: + * - & ^ | && ||
32140 OpenMP 3.1:
32142 reduction-operator:
32143 One of: + * - & ^ | && || min max
32145 OpenMP 4.0:
32147 reduction-operator:
32148 One of: + * - & ^ | && ||
32149 id-expression */
32151 static tree
32152 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32154 enum tree_code code = ERROR_MARK;
32155 tree nlist, c, id = NULL_TREE;
32157 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32158 return list;
32160 switch (cp_lexer_peek_token (parser->lexer)->type)
32162 case CPP_PLUS: code = PLUS_EXPR; break;
32163 case CPP_MULT: code = MULT_EXPR; break;
32164 case CPP_MINUS: code = MINUS_EXPR; break;
32165 case CPP_AND: code = BIT_AND_EXPR; break;
32166 case CPP_XOR: code = BIT_XOR_EXPR; break;
32167 case CPP_OR: code = BIT_IOR_EXPR; break;
32168 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32169 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32170 default: break;
32173 if (code != ERROR_MARK)
32174 cp_lexer_consume_token (parser->lexer);
32175 else
32177 bool saved_colon_corrects_to_scope_p;
32178 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32179 parser->colon_corrects_to_scope_p = false;
32180 id = cp_parser_id_expression (parser, /*template_p=*/false,
32181 /*check_dependency_p=*/true,
32182 /*template_p=*/NULL,
32183 /*declarator_p=*/false,
32184 /*optional_p=*/false);
32185 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32186 if (identifier_p (id))
32188 const char *p = IDENTIFIER_POINTER (id);
32190 if (strcmp (p, "min") == 0)
32191 code = MIN_EXPR;
32192 else if (strcmp (p, "max") == 0)
32193 code = MAX_EXPR;
32194 else if (id == cp_operator_id (PLUS_EXPR))
32195 code = PLUS_EXPR;
32196 else if (id == cp_operator_id (MULT_EXPR))
32197 code = MULT_EXPR;
32198 else if (id == cp_operator_id (MINUS_EXPR))
32199 code = MINUS_EXPR;
32200 else if (id == cp_operator_id (BIT_AND_EXPR))
32201 code = BIT_AND_EXPR;
32202 else if (id == cp_operator_id (BIT_IOR_EXPR))
32203 code = BIT_IOR_EXPR;
32204 else if (id == cp_operator_id (BIT_XOR_EXPR))
32205 code = BIT_XOR_EXPR;
32206 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32207 code = TRUTH_ANDIF_EXPR;
32208 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32209 code = TRUTH_ORIF_EXPR;
32210 id = omp_reduction_id (code, id, NULL_TREE);
32211 tree scope = parser->scope;
32212 if (scope)
32213 id = build_qualified_name (NULL_TREE, scope, id, false);
32214 parser->scope = NULL_TREE;
32215 parser->qualifying_scope = NULL_TREE;
32216 parser->object_scope = NULL_TREE;
32218 else
32220 error ("invalid reduction-identifier");
32221 resync_fail:
32222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32223 /*or_comma=*/false,
32224 /*consume_paren=*/true);
32225 return list;
32229 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32230 goto resync_fail;
32232 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32233 NULL);
32234 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32236 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32237 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32240 return nlist;
32243 /* OpenMP 2.5:
32244 schedule ( schedule-kind )
32245 schedule ( schedule-kind , expression )
32247 schedule-kind:
32248 static | dynamic | guided | runtime | auto
32250 OpenMP 4.5:
32251 schedule ( schedule-modifier : schedule-kind )
32252 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32254 schedule-modifier:
32255 simd
32256 monotonic
32257 nonmonotonic */
32259 static tree
32260 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32262 tree c, t;
32263 int modifiers = 0, nmodifiers = 0;
32265 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32266 return list;
32268 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32270 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32272 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32273 const char *p = IDENTIFIER_POINTER (id);
32274 if (strcmp ("simd", p) == 0)
32275 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32276 else if (strcmp ("monotonic", p) == 0)
32277 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32278 else if (strcmp ("nonmonotonic", p) == 0)
32279 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32280 else
32281 break;
32282 cp_lexer_consume_token (parser->lexer);
32283 if (nmodifiers++ == 0
32284 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32285 cp_lexer_consume_token (parser->lexer);
32286 else
32288 cp_parser_require (parser, CPP_COLON, RT_COLON);
32289 break;
32293 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32295 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32296 const char *p = IDENTIFIER_POINTER (id);
32298 switch (p[0])
32300 case 'd':
32301 if (strcmp ("dynamic", p) != 0)
32302 goto invalid_kind;
32303 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32304 break;
32306 case 'g':
32307 if (strcmp ("guided", p) != 0)
32308 goto invalid_kind;
32309 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32310 break;
32312 case 'r':
32313 if (strcmp ("runtime", p) != 0)
32314 goto invalid_kind;
32315 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32316 break;
32318 default:
32319 goto invalid_kind;
32322 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32323 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32324 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32325 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32326 else
32327 goto invalid_kind;
32328 cp_lexer_consume_token (parser->lexer);
32330 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32331 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32332 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32333 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32335 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32336 "specified");
32337 modifiers = 0;
32340 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32342 cp_token *token;
32343 cp_lexer_consume_token (parser->lexer);
32345 token = cp_lexer_peek_token (parser->lexer);
32346 t = cp_parser_assignment_expression (parser);
32348 if (t == error_mark_node)
32349 goto resync_fail;
32350 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32351 error_at (token->location, "schedule %<runtime%> does not take "
32352 "a %<chunk_size%> parameter");
32353 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32354 error_at (token->location, "schedule %<auto%> does not take "
32355 "a %<chunk_size%> parameter");
32356 else
32357 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32359 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32360 goto resync_fail;
32362 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32363 goto resync_fail;
32365 OMP_CLAUSE_SCHEDULE_KIND (c)
32366 = (enum omp_clause_schedule_kind)
32367 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32369 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32370 OMP_CLAUSE_CHAIN (c) = list;
32371 return c;
32373 invalid_kind:
32374 cp_parser_error (parser, "invalid schedule kind");
32375 resync_fail:
32376 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32377 /*or_comma=*/false,
32378 /*consume_paren=*/true);
32379 return list;
32382 /* OpenMP 3.0:
32383 untied */
32385 static tree
32386 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32387 tree list, location_t location)
32389 tree c;
32391 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32393 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32394 OMP_CLAUSE_CHAIN (c) = list;
32395 return c;
32398 /* OpenMP 4.0:
32399 inbranch
32400 notinbranch */
32402 static tree
32403 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32404 tree list, location_t location)
32406 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32407 tree c = build_omp_clause (location, code);
32408 OMP_CLAUSE_CHAIN (c) = list;
32409 return c;
32412 /* OpenMP 4.0:
32413 parallel
32415 sections
32416 taskgroup */
32418 static tree
32419 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32420 enum omp_clause_code code,
32421 tree list, location_t location)
32423 tree c = build_omp_clause (location, code);
32424 OMP_CLAUSE_CHAIN (c) = list;
32425 return c;
32428 /* OpenMP 4.5:
32429 nogroup */
32431 static tree
32432 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32433 tree list, location_t location)
32435 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32436 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32437 OMP_CLAUSE_CHAIN (c) = list;
32438 return c;
32441 /* OpenMP 4.5:
32442 simd
32443 threads */
32445 static tree
32446 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32447 enum omp_clause_code code,
32448 tree list, location_t location)
32450 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32451 tree c = build_omp_clause (location, code);
32452 OMP_CLAUSE_CHAIN (c) = list;
32453 return c;
32456 /* OpenMP 4.0:
32457 num_teams ( expression ) */
32459 static tree
32460 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32461 location_t location)
32463 tree t, c;
32465 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32466 return list;
32468 t = cp_parser_expression (parser);
32470 if (t == error_mark_node
32471 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32472 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32473 /*or_comma=*/false,
32474 /*consume_paren=*/true);
32476 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32477 "num_teams", location);
32479 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32480 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32481 OMP_CLAUSE_CHAIN (c) = list;
32483 return c;
32486 /* OpenMP 4.0:
32487 thread_limit ( expression ) */
32489 static tree
32490 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32491 location_t location)
32493 tree t, c;
32495 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32496 return list;
32498 t = cp_parser_expression (parser);
32500 if (t == error_mark_node
32501 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32502 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32503 /*or_comma=*/false,
32504 /*consume_paren=*/true);
32506 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32507 "thread_limit", location);
32509 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32510 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32511 OMP_CLAUSE_CHAIN (c) = list;
32513 return c;
32516 /* OpenMP 4.0:
32517 aligned ( variable-list )
32518 aligned ( variable-list : constant-expression ) */
32520 static tree
32521 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32523 tree nlist, c, alignment = NULL_TREE;
32524 bool colon;
32526 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32527 return list;
32529 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32530 &colon);
32532 if (colon)
32534 alignment = cp_parser_constant_expression (parser);
32536 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32537 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32538 /*or_comma=*/false,
32539 /*consume_paren=*/true);
32541 if (alignment == error_mark_node)
32542 alignment = NULL_TREE;
32545 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32546 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32548 return nlist;
32551 /* OpenMP 4.0:
32552 linear ( variable-list )
32553 linear ( variable-list : expression )
32555 OpenMP 4.5:
32556 linear ( modifier ( variable-list ) )
32557 linear ( modifier ( variable-list ) : expression ) */
32559 static tree
32560 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32561 bool is_cilk_simd_fn, bool declare_simd)
32563 tree nlist, c, step = integer_one_node;
32564 bool colon;
32565 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32568 return list;
32570 if (!is_cilk_simd_fn
32571 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32573 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32574 const char *p = IDENTIFIER_POINTER (id);
32576 if (strcmp ("ref", p) == 0)
32577 kind = OMP_CLAUSE_LINEAR_REF;
32578 else if (strcmp ("val", p) == 0)
32579 kind = OMP_CLAUSE_LINEAR_VAL;
32580 else if (strcmp ("uval", p) == 0)
32581 kind = OMP_CLAUSE_LINEAR_UVAL;
32582 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32583 cp_lexer_consume_token (parser->lexer);
32584 else
32585 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32588 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32589 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32590 &colon);
32591 else
32593 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32594 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32595 if (colon)
32596 cp_parser_require (parser, CPP_COLON, RT_COLON);
32597 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32598 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32599 /*or_comma=*/false,
32600 /*consume_paren=*/true);
32603 if (colon)
32605 step = NULL_TREE;
32606 if (declare_simd
32607 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32608 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32610 cp_token *token = cp_lexer_peek_token (parser->lexer);
32611 cp_parser_parse_tentatively (parser);
32612 step = cp_parser_id_expression (parser, /*template_p=*/false,
32613 /*check_dependency_p=*/true,
32614 /*template_p=*/NULL,
32615 /*declarator_p=*/false,
32616 /*optional_p=*/false);
32617 if (step != error_mark_node)
32618 step = cp_parser_lookup_name_simple (parser, step, token->location);
32619 if (step == error_mark_node)
32621 step = NULL_TREE;
32622 cp_parser_abort_tentative_parse (parser);
32624 else if (!cp_parser_parse_definitely (parser))
32625 step = NULL_TREE;
32627 if (!step)
32628 step = cp_parser_expression (parser);
32630 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32632 sorry ("using parameters for %<linear%> step is not supported yet");
32633 step = integer_one_node;
32635 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32636 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32637 /*or_comma=*/false,
32638 /*consume_paren=*/true);
32640 if (step == error_mark_node)
32641 return list;
32644 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32646 OMP_CLAUSE_LINEAR_STEP (c) = step;
32647 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32650 return nlist;
32653 /* OpenMP 4.0:
32654 safelen ( constant-expression ) */
32656 static tree
32657 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32658 location_t location)
32660 tree t, c;
32662 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32663 return list;
32665 t = cp_parser_constant_expression (parser);
32667 if (t == error_mark_node
32668 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32669 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32670 /*or_comma=*/false,
32671 /*consume_paren=*/true);
32673 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32675 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32676 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32677 OMP_CLAUSE_CHAIN (c) = list;
32679 return c;
32682 /* OpenMP 4.0:
32683 simdlen ( constant-expression ) */
32685 static tree
32686 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32687 location_t location)
32689 tree t, c;
32691 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32692 return list;
32694 t = cp_parser_constant_expression (parser);
32696 if (t == error_mark_node
32697 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32698 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32699 /*or_comma=*/false,
32700 /*consume_paren=*/true);
32702 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32704 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32705 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32706 OMP_CLAUSE_CHAIN (c) = list;
32708 return c;
32711 /* OpenMP 4.5:
32712 vec:
32713 identifier [+/- integer]
32714 vec , identifier [+/- integer]
32717 static tree
32718 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32719 tree list)
32721 tree vec = NULL;
32723 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32725 cp_parser_error (parser, "expected identifier");
32726 return list;
32729 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32731 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32732 tree t, identifier = cp_parser_identifier (parser);
32733 tree addend = NULL;
32735 if (identifier == error_mark_node)
32736 t = error_mark_node;
32737 else
32739 t = cp_parser_lookup_name_simple
32740 (parser, identifier,
32741 cp_lexer_peek_token (parser->lexer)->location);
32742 if (t == error_mark_node)
32743 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32744 id_loc);
32747 bool neg = false;
32748 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32749 neg = true;
32750 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32752 addend = integer_zero_node;
32753 goto add_to_vector;
32755 cp_lexer_consume_token (parser->lexer);
32757 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32759 cp_parser_error (parser, "expected integer");
32760 return list;
32763 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32764 if (TREE_CODE (addend) != INTEGER_CST)
32766 cp_parser_error (parser, "expected integer");
32767 return list;
32769 cp_lexer_consume_token (parser->lexer);
32771 add_to_vector:
32772 if (t != error_mark_node)
32774 vec = tree_cons (addend, t, vec);
32775 if (neg)
32776 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32780 break;
32782 cp_lexer_consume_token (parser->lexer);
32785 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32787 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32788 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32789 OMP_CLAUSE_DECL (u) = nreverse (vec);
32790 OMP_CLAUSE_CHAIN (u) = list;
32791 return u;
32793 return list;
32796 /* OpenMP 4.0:
32797 depend ( depend-kind : variable-list )
32799 depend-kind:
32800 in | out | inout
32802 OpenMP 4.5:
32803 depend ( source )
32805 depend ( sink : vec ) */
32807 static tree
32808 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32810 tree nlist, c;
32811 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32813 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32814 return list;
32816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32819 const char *p = IDENTIFIER_POINTER (id);
32821 if (strcmp ("in", p) == 0)
32822 kind = OMP_CLAUSE_DEPEND_IN;
32823 else if (strcmp ("inout", p) == 0)
32824 kind = OMP_CLAUSE_DEPEND_INOUT;
32825 else if (strcmp ("out", p) == 0)
32826 kind = OMP_CLAUSE_DEPEND_OUT;
32827 else if (strcmp ("source", p) == 0)
32828 kind = OMP_CLAUSE_DEPEND_SOURCE;
32829 else if (strcmp ("sink", p) == 0)
32830 kind = OMP_CLAUSE_DEPEND_SINK;
32831 else
32832 goto invalid_kind;
32834 else
32835 goto invalid_kind;
32837 cp_lexer_consume_token (parser->lexer);
32839 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32841 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32842 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32843 OMP_CLAUSE_DECL (c) = NULL_TREE;
32844 OMP_CLAUSE_CHAIN (c) = list;
32845 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32846 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32847 /*or_comma=*/false,
32848 /*consume_paren=*/true);
32849 return c;
32852 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32853 goto resync_fail;
32855 if (kind == OMP_CLAUSE_DEPEND_SINK)
32856 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32857 else
32859 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32860 list, NULL);
32862 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32863 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32865 return nlist;
32867 invalid_kind:
32868 cp_parser_error (parser, "invalid depend kind");
32869 resync_fail:
32870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32871 /*or_comma=*/false,
32872 /*consume_paren=*/true);
32873 return list;
32876 /* OpenMP 4.0:
32877 map ( map-kind : variable-list )
32878 map ( variable-list )
32880 map-kind:
32881 alloc | to | from | tofrom
32883 OpenMP 4.5:
32884 map-kind:
32885 alloc | to | from | tofrom | release | delete
32887 map ( always [,] map-kind: variable-list ) */
32889 static tree
32890 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32892 tree nlist, c;
32893 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32894 bool always = false;
32896 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32897 return list;
32899 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32901 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32902 const char *p = IDENTIFIER_POINTER (id);
32904 if (strcmp ("always", p) == 0)
32906 int nth = 2;
32907 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32908 nth++;
32909 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32910 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32911 == RID_DELETE))
32912 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32913 == CPP_COLON))
32915 always = true;
32916 cp_lexer_consume_token (parser->lexer);
32917 if (nth == 3)
32918 cp_lexer_consume_token (parser->lexer);
32923 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32924 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32927 const char *p = IDENTIFIER_POINTER (id);
32929 if (strcmp ("alloc", p) == 0)
32930 kind = GOMP_MAP_ALLOC;
32931 else if (strcmp ("to", p) == 0)
32932 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32933 else if (strcmp ("from", p) == 0)
32934 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32935 else if (strcmp ("tofrom", p) == 0)
32936 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32937 else if (strcmp ("release", p) == 0)
32938 kind = GOMP_MAP_RELEASE;
32939 else
32941 cp_parser_error (parser, "invalid map kind");
32942 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32943 /*or_comma=*/false,
32944 /*consume_paren=*/true);
32945 return list;
32947 cp_lexer_consume_token (parser->lexer);
32948 cp_lexer_consume_token (parser->lexer);
32950 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
32951 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32953 kind = GOMP_MAP_DELETE;
32954 cp_lexer_consume_token (parser->lexer);
32955 cp_lexer_consume_token (parser->lexer);
32958 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
32959 NULL);
32961 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32962 OMP_CLAUSE_SET_MAP_KIND (c, kind);
32964 return nlist;
32967 /* OpenMP 4.0:
32968 device ( expression ) */
32970 static tree
32971 cp_parser_omp_clause_device (cp_parser *parser, tree list,
32972 location_t location)
32974 tree t, c;
32976 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32977 return list;
32979 t = cp_parser_expression (parser);
32981 if (t == error_mark_node
32982 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32983 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32984 /*or_comma=*/false,
32985 /*consume_paren=*/true);
32987 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
32988 "device", location);
32990 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
32991 OMP_CLAUSE_DEVICE_ID (c) = t;
32992 OMP_CLAUSE_CHAIN (c) = list;
32994 return c;
32997 /* OpenMP 4.0:
32998 dist_schedule ( static )
32999 dist_schedule ( static , expression ) */
33001 static tree
33002 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33003 location_t location)
33005 tree c, t;
33007 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33008 return list;
33010 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33012 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33013 goto invalid_kind;
33014 cp_lexer_consume_token (parser->lexer);
33016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33018 cp_lexer_consume_token (parser->lexer);
33020 t = cp_parser_assignment_expression (parser);
33022 if (t == error_mark_node)
33023 goto resync_fail;
33024 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33026 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33027 goto resync_fail;
33029 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33030 goto resync_fail;
33032 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33033 location);
33034 OMP_CLAUSE_CHAIN (c) = list;
33035 return c;
33037 invalid_kind:
33038 cp_parser_error (parser, "invalid dist_schedule kind");
33039 resync_fail:
33040 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33041 /*or_comma=*/false,
33042 /*consume_paren=*/true);
33043 return list;
33046 /* OpenMP 4.0:
33047 proc_bind ( proc-bind-kind )
33049 proc-bind-kind:
33050 master | close | spread */
33052 static tree
33053 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33054 location_t location)
33056 tree c;
33057 enum omp_clause_proc_bind_kind kind;
33059 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33060 return list;
33062 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33064 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33065 const char *p = IDENTIFIER_POINTER (id);
33067 if (strcmp ("master", p) == 0)
33068 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33069 else if (strcmp ("close", p) == 0)
33070 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33071 else if (strcmp ("spread", p) == 0)
33072 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33073 else
33074 goto invalid_kind;
33076 else
33077 goto invalid_kind;
33079 cp_lexer_consume_token (parser->lexer);
33080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33081 goto resync_fail;
33083 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33084 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33085 location);
33086 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33087 OMP_CLAUSE_CHAIN (c) = list;
33088 return c;
33090 invalid_kind:
33091 cp_parser_error (parser, "invalid depend kind");
33092 resync_fail:
33093 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33094 /*or_comma=*/false,
33095 /*consume_paren=*/true);
33096 return list;
33099 /* OpenACC:
33100 async [( int-expr )] */
33102 static tree
33103 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33105 tree c, t;
33106 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33108 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33110 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33112 cp_lexer_consume_token (parser->lexer);
33114 t = cp_parser_expression (parser);
33115 if (t == error_mark_node
33116 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33117 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33118 /*or_comma=*/false,
33119 /*consume_paren=*/true);
33122 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33124 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33125 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33126 OMP_CLAUSE_CHAIN (c) = list;
33127 list = c;
33129 return list;
33132 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33133 is a bitmask in MASK. Return the list of clauses found. */
33135 static tree
33136 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33137 const char *where, cp_token *pragma_tok,
33138 bool finish_p = true)
33140 tree clauses = NULL;
33141 bool first = true;
33143 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33145 location_t here;
33146 pragma_omp_clause c_kind;
33147 omp_clause_code code;
33148 const char *c_name;
33149 tree prev = clauses;
33151 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33152 cp_lexer_consume_token (parser->lexer);
33154 here = cp_lexer_peek_token (parser->lexer)->location;
33155 c_kind = cp_parser_omp_clause_name (parser);
33157 switch (c_kind)
33159 case PRAGMA_OACC_CLAUSE_ASYNC:
33160 clauses = cp_parser_oacc_clause_async (parser, clauses);
33161 c_name = "async";
33162 break;
33163 case PRAGMA_OACC_CLAUSE_AUTO:
33164 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33165 clauses, here);
33166 c_name = "auto";
33167 break;
33168 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33169 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33170 c_name = "collapse";
33171 break;
33172 case PRAGMA_OACC_CLAUSE_COPY:
33173 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33174 c_name = "copy";
33175 break;
33176 case PRAGMA_OACC_CLAUSE_COPYIN:
33177 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33178 c_name = "copyin";
33179 break;
33180 case PRAGMA_OACC_CLAUSE_COPYOUT:
33181 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33182 c_name = "copyout";
33183 break;
33184 case PRAGMA_OACC_CLAUSE_CREATE:
33185 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33186 c_name = "create";
33187 break;
33188 case PRAGMA_OACC_CLAUSE_DELETE:
33189 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33190 c_name = "delete";
33191 break;
33192 case PRAGMA_OMP_CLAUSE_DEFAULT:
33193 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33194 c_name = "default";
33195 break;
33196 case PRAGMA_OACC_CLAUSE_DEVICE:
33197 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33198 c_name = "device";
33199 break;
33200 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33201 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33202 c_name = "deviceptr";
33203 break;
33204 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33205 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33206 c_name = "device_resident";
33207 break;
33208 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33209 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33210 clauses);
33211 c_name = "firstprivate";
33212 break;
33213 case PRAGMA_OACC_CLAUSE_GANG:
33214 c_name = "gang";
33215 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33216 c_name, clauses);
33217 break;
33218 case PRAGMA_OACC_CLAUSE_HOST:
33219 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33220 c_name = "host";
33221 break;
33222 case PRAGMA_OACC_CLAUSE_IF:
33223 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33224 c_name = "if";
33225 break;
33226 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33227 clauses = cp_parser_oacc_simple_clause (parser,
33228 OMP_CLAUSE_INDEPENDENT,
33229 clauses, here);
33230 c_name = "independent";
33231 break;
33232 case PRAGMA_OACC_CLAUSE_LINK:
33233 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33234 c_name = "link";
33235 break;
33236 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33237 code = OMP_CLAUSE_NUM_GANGS;
33238 c_name = "num_gangs";
33239 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33240 clauses);
33241 break;
33242 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33243 c_name = "num_workers";
33244 code = OMP_CLAUSE_NUM_WORKERS;
33245 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33246 clauses);
33247 break;
33248 case PRAGMA_OACC_CLAUSE_PRESENT:
33249 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33250 c_name = "present";
33251 break;
33252 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33253 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33254 c_name = "present_or_copy";
33255 break;
33256 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33257 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33258 c_name = "present_or_copyin";
33259 break;
33260 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33261 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33262 c_name = "present_or_copyout";
33263 break;
33264 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33265 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33266 c_name = "present_or_create";
33267 break;
33268 case PRAGMA_OACC_CLAUSE_PRIVATE:
33269 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33270 clauses);
33271 c_name = "private";
33272 break;
33273 case PRAGMA_OACC_CLAUSE_REDUCTION:
33274 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33275 c_name = "reduction";
33276 break;
33277 case PRAGMA_OACC_CLAUSE_SELF:
33278 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33279 c_name = "self";
33280 break;
33281 case PRAGMA_OACC_CLAUSE_SEQ:
33282 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33283 clauses, here);
33284 c_name = "seq";
33285 break;
33286 case PRAGMA_OACC_CLAUSE_TILE:
33287 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33288 c_name = "tile";
33289 break;
33290 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33291 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33292 clauses);
33293 c_name = "use_device";
33294 break;
33295 case PRAGMA_OACC_CLAUSE_VECTOR:
33296 c_name = "vector";
33297 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33298 c_name, clauses);
33299 break;
33300 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33301 c_name = "vector_length";
33302 code = OMP_CLAUSE_VECTOR_LENGTH;
33303 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33304 clauses);
33305 break;
33306 case PRAGMA_OACC_CLAUSE_WAIT:
33307 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33308 c_name = "wait";
33309 break;
33310 case PRAGMA_OACC_CLAUSE_WORKER:
33311 c_name = "worker";
33312 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33313 c_name, clauses);
33314 break;
33315 default:
33316 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33317 goto saw_error;
33320 first = false;
33322 if (((mask >> c_kind) & 1) == 0)
33324 /* Remove the invalid clause(s) from the list to avoid
33325 confusing the rest of the compiler. */
33326 clauses = prev;
33327 error_at (here, "%qs is not valid for %qs", c_name, where);
33331 saw_error:
33332 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33334 if (finish_p)
33335 return finish_omp_clauses (clauses, C_ORT_ACC);
33337 return clauses;
33340 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33341 is a bitmask in MASK. Return the list of clauses found; the result
33342 of clause default goes in *pdefault. */
33344 static tree
33345 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33346 const char *where, cp_token *pragma_tok,
33347 bool finish_p = true)
33349 tree clauses = NULL;
33350 bool first = true;
33351 cp_token *token = NULL;
33353 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33355 pragma_omp_clause c_kind;
33356 const char *c_name;
33357 tree prev = clauses;
33359 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33360 cp_lexer_consume_token (parser->lexer);
33362 token = cp_lexer_peek_token (parser->lexer);
33363 c_kind = cp_parser_omp_clause_name (parser);
33365 switch (c_kind)
33367 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33368 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33369 token->location);
33370 c_name = "collapse";
33371 break;
33372 case PRAGMA_OMP_CLAUSE_COPYIN:
33373 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33374 c_name = "copyin";
33375 break;
33376 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33377 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33378 clauses);
33379 c_name = "copyprivate";
33380 break;
33381 case PRAGMA_OMP_CLAUSE_DEFAULT:
33382 clauses = cp_parser_omp_clause_default (parser, clauses,
33383 token->location, false);
33384 c_name = "default";
33385 break;
33386 case PRAGMA_OMP_CLAUSE_FINAL:
33387 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33388 c_name = "final";
33389 break;
33390 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33392 clauses);
33393 c_name = "firstprivate";
33394 break;
33395 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33396 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33397 token->location);
33398 c_name = "grainsize";
33399 break;
33400 case PRAGMA_OMP_CLAUSE_HINT:
33401 clauses = cp_parser_omp_clause_hint (parser, clauses,
33402 token->location);
33403 c_name = "hint";
33404 break;
33405 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33406 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33407 token->location);
33408 c_name = "defaultmap";
33409 break;
33410 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33411 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33412 clauses);
33413 c_name = "use_device_ptr";
33414 break;
33415 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33416 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33417 clauses);
33418 c_name = "is_device_ptr";
33419 break;
33420 case PRAGMA_OMP_CLAUSE_IF:
33421 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33422 true);
33423 c_name = "if";
33424 break;
33425 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33426 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33427 clauses);
33428 c_name = "lastprivate";
33429 break;
33430 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33431 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33432 token->location);
33433 c_name = "mergeable";
33434 break;
33435 case PRAGMA_OMP_CLAUSE_NOWAIT:
33436 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33437 c_name = "nowait";
33438 break;
33439 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33440 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33441 token->location);
33442 c_name = "num_tasks";
33443 break;
33444 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33445 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33446 token->location);
33447 c_name = "num_threads";
33448 break;
33449 case PRAGMA_OMP_CLAUSE_ORDERED:
33450 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33451 token->location);
33452 c_name = "ordered";
33453 break;
33454 case PRAGMA_OMP_CLAUSE_PRIORITY:
33455 clauses = cp_parser_omp_clause_priority (parser, clauses,
33456 token->location);
33457 c_name = "priority";
33458 break;
33459 case PRAGMA_OMP_CLAUSE_PRIVATE:
33460 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33461 clauses);
33462 c_name = "private";
33463 break;
33464 case PRAGMA_OMP_CLAUSE_REDUCTION:
33465 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33466 c_name = "reduction";
33467 break;
33468 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33469 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33470 token->location);
33471 c_name = "schedule";
33472 break;
33473 case PRAGMA_OMP_CLAUSE_SHARED:
33474 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33475 clauses);
33476 c_name = "shared";
33477 break;
33478 case PRAGMA_OMP_CLAUSE_UNTIED:
33479 clauses = cp_parser_omp_clause_untied (parser, clauses,
33480 token->location);
33481 c_name = "untied";
33482 break;
33483 case PRAGMA_OMP_CLAUSE_INBRANCH:
33484 case PRAGMA_CILK_CLAUSE_MASK:
33485 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33486 clauses, token->location);
33487 c_name = "inbranch";
33488 break;
33489 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33490 case PRAGMA_CILK_CLAUSE_NOMASK:
33491 clauses = cp_parser_omp_clause_branch (parser,
33492 OMP_CLAUSE_NOTINBRANCH,
33493 clauses, token->location);
33494 c_name = "notinbranch";
33495 break;
33496 case PRAGMA_OMP_CLAUSE_PARALLEL:
33497 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33498 clauses, token->location);
33499 c_name = "parallel";
33500 if (!first)
33502 clause_not_first:
33503 error_at (token->location, "%qs must be the first clause of %qs",
33504 c_name, where);
33505 clauses = prev;
33507 break;
33508 case PRAGMA_OMP_CLAUSE_FOR:
33509 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33510 clauses, token->location);
33511 c_name = "for";
33512 if (!first)
33513 goto clause_not_first;
33514 break;
33515 case PRAGMA_OMP_CLAUSE_SECTIONS:
33516 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33517 clauses, token->location);
33518 c_name = "sections";
33519 if (!first)
33520 goto clause_not_first;
33521 break;
33522 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33523 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33524 clauses, token->location);
33525 c_name = "taskgroup";
33526 if (!first)
33527 goto clause_not_first;
33528 break;
33529 case PRAGMA_OMP_CLAUSE_LINK:
33530 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33531 c_name = "to";
33532 break;
33533 case PRAGMA_OMP_CLAUSE_TO:
33534 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33535 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33536 clauses);
33537 else
33538 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33539 c_name = "to";
33540 break;
33541 case PRAGMA_OMP_CLAUSE_FROM:
33542 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33543 c_name = "from";
33544 break;
33545 case PRAGMA_OMP_CLAUSE_UNIFORM:
33546 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33547 clauses);
33548 c_name = "uniform";
33549 break;
33550 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33551 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33552 token->location);
33553 c_name = "num_teams";
33554 break;
33555 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33556 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33557 token->location);
33558 c_name = "thread_limit";
33559 break;
33560 case PRAGMA_OMP_CLAUSE_ALIGNED:
33561 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33562 c_name = "aligned";
33563 break;
33564 case PRAGMA_OMP_CLAUSE_LINEAR:
33566 bool cilk_simd_fn = false, declare_simd = false;
33567 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33568 cilk_simd_fn = true;
33569 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33570 declare_simd = true;
33571 clauses = cp_parser_omp_clause_linear (parser, clauses,
33572 cilk_simd_fn, declare_simd);
33574 c_name = "linear";
33575 break;
33576 case PRAGMA_OMP_CLAUSE_DEPEND:
33577 clauses = cp_parser_omp_clause_depend (parser, clauses,
33578 token->location);
33579 c_name = "depend";
33580 break;
33581 case PRAGMA_OMP_CLAUSE_MAP:
33582 clauses = cp_parser_omp_clause_map (parser, clauses);
33583 c_name = "map";
33584 break;
33585 case PRAGMA_OMP_CLAUSE_DEVICE:
33586 clauses = cp_parser_omp_clause_device (parser, clauses,
33587 token->location);
33588 c_name = "device";
33589 break;
33590 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33591 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33592 token->location);
33593 c_name = "dist_schedule";
33594 break;
33595 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33596 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33597 token->location);
33598 c_name = "proc_bind";
33599 break;
33600 case PRAGMA_OMP_CLAUSE_SAFELEN:
33601 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33602 token->location);
33603 c_name = "safelen";
33604 break;
33605 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33606 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33607 token->location);
33608 c_name = "simdlen";
33609 break;
33610 case PRAGMA_OMP_CLAUSE_NOGROUP:
33611 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33612 token->location);
33613 c_name = "nogroup";
33614 break;
33615 case PRAGMA_OMP_CLAUSE_THREADS:
33616 clauses
33617 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33618 clauses, token->location);
33619 c_name = "threads";
33620 break;
33621 case PRAGMA_OMP_CLAUSE_SIMD:
33622 clauses
33623 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33624 clauses, token->location);
33625 c_name = "simd";
33626 break;
33627 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33628 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33629 c_name = "simdlen";
33630 break;
33631 default:
33632 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33633 goto saw_error;
33636 first = false;
33638 if (((mask >> c_kind) & 1) == 0)
33640 /* Remove the invalid clause(s) from the list to avoid
33641 confusing the rest of the compiler. */
33642 clauses = prev;
33643 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33646 saw_error:
33647 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33648 no reason to skip to the end. */
33649 if (!(flag_cilkplus && pragma_tok == NULL))
33650 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33651 if (finish_p)
33653 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33654 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33655 else
33656 return finish_omp_clauses (clauses, C_ORT_OMP);
33658 return clauses;
33661 /* OpenMP 2.5:
33662 structured-block:
33663 statement
33665 In practice, we're also interested in adding the statement to an
33666 outer node. So it is convenient if we work around the fact that
33667 cp_parser_statement calls add_stmt. */
33669 static unsigned
33670 cp_parser_begin_omp_structured_block (cp_parser *parser)
33672 unsigned save = parser->in_statement;
33674 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33675 This preserves the "not within loop or switch" style error messages
33676 for nonsense cases like
33677 void foo() {
33678 #pragma omp single
33679 break;
33682 if (parser->in_statement)
33683 parser->in_statement = IN_OMP_BLOCK;
33685 return save;
33688 static void
33689 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33691 parser->in_statement = save;
33694 static tree
33695 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33697 tree stmt = begin_omp_structured_block ();
33698 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33700 cp_parser_statement (parser, NULL_TREE, false, if_p);
33702 cp_parser_end_omp_structured_block (parser, save);
33703 return finish_omp_structured_block (stmt);
33706 /* OpenMP 2.5:
33707 # pragma omp atomic new-line
33708 expression-stmt
33710 expression-stmt:
33711 x binop= expr | x++ | ++x | x-- | --x
33712 binop:
33713 +, *, -, /, &, ^, |, <<, >>
33715 where x is an lvalue expression with scalar type.
33717 OpenMP 3.1:
33718 # pragma omp atomic new-line
33719 update-stmt
33721 # pragma omp atomic read new-line
33722 read-stmt
33724 # pragma omp atomic write new-line
33725 write-stmt
33727 # pragma omp atomic update new-line
33728 update-stmt
33730 # pragma omp atomic capture new-line
33731 capture-stmt
33733 # pragma omp atomic capture new-line
33734 capture-block
33736 read-stmt:
33737 v = x
33738 write-stmt:
33739 x = expr
33740 update-stmt:
33741 expression-stmt | x = x binop expr
33742 capture-stmt:
33743 v = expression-stmt
33744 capture-block:
33745 { v = x; update-stmt; } | { update-stmt; v = x; }
33747 OpenMP 4.0:
33748 update-stmt:
33749 expression-stmt | x = x binop expr | x = expr binop x
33750 capture-stmt:
33751 v = update-stmt
33752 capture-block:
33753 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33755 where x and v are lvalue expressions with scalar type. */
33757 static void
33758 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33760 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33761 tree rhs1 = NULL_TREE, orig_lhs;
33762 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33763 bool structured_block = false;
33764 bool seq_cst = false;
33766 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33768 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33769 const char *p = IDENTIFIER_POINTER (id);
33771 if (!strcmp (p, "seq_cst"))
33773 seq_cst = true;
33774 cp_lexer_consume_token (parser->lexer);
33775 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33776 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33777 cp_lexer_consume_token (parser->lexer);
33780 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33782 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33783 const char *p = IDENTIFIER_POINTER (id);
33785 if (!strcmp (p, "read"))
33786 code = OMP_ATOMIC_READ;
33787 else if (!strcmp (p, "write"))
33788 code = NOP_EXPR;
33789 else if (!strcmp (p, "update"))
33790 code = OMP_ATOMIC;
33791 else if (!strcmp (p, "capture"))
33792 code = OMP_ATOMIC_CAPTURE_NEW;
33793 else
33794 p = NULL;
33795 if (p)
33796 cp_lexer_consume_token (parser->lexer);
33798 if (!seq_cst)
33800 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33801 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33802 cp_lexer_consume_token (parser->lexer);
33804 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33806 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33807 const char *p = IDENTIFIER_POINTER (id);
33809 if (!strcmp (p, "seq_cst"))
33811 seq_cst = true;
33812 cp_lexer_consume_token (parser->lexer);
33816 cp_parser_require_pragma_eol (parser, pragma_tok);
33818 switch (code)
33820 case OMP_ATOMIC_READ:
33821 case NOP_EXPR: /* atomic write */
33822 v = cp_parser_unary_expression (parser);
33823 if (v == error_mark_node)
33824 goto saw_error;
33825 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33826 goto saw_error;
33827 if (code == NOP_EXPR)
33828 lhs = cp_parser_expression (parser);
33829 else
33830 lhs = cp_parser_unary_expression (parser);
33831 if (lhs == error_mark_node)
33832 goto saw_error;
33833 if (code == NOP_EXPR)
33835 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33836 opcode. */
33837 code = OMP_ATOMIC;
33838 rhs = lhs;
33839 lhs = v;
33840 v = NULL_TREE;
33842 goto done;
33843 case OMP_ATOMIC_CAPTURE_NEW:
33844 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33846 cp_lexer_consume_token (parser->lexer);
33847 structured_block = true;
33849 else
33851 v = cp_parser_unary_expression (parser);
33852 if (v == error_mark_node)
33853 goto saw_error;
33854 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33855 goto saw_error;
33857 default:
33858 break;
33861 restart:
33862 lhs = cp_parser_unary_expression (parser);
33863 orig_lhs = lhs;
33864 switch (TREE_CODE (lhs))
33866 case ERROR_MARK:
33867 goto saw_error;
33869 case POSTINCREMENT_EXPR:
33870 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33871 code = OMP_ATOMIC_CAPTURE_OLD;
33872 /* FALLTHROUGH */
33873 case PREINCREMENT_EXPR:
33874 lhs = TREE_OPERAND (lhs, 0);
33875 opcode = PLUS_EXPR;
33876 rhs = integer_one_node;
33877 break;
33879 case POSTDECREMENT_EXPR:
33880 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33881 code = OMP_ATOMIC_CAPTURE_OLD;
33882 /* FALLTHROUGH */
33883 case PREDECREMENT_EXPR:
33884 lhs = TREE_OPERAND (lhs, 0);
33885 opcode = MINUS_EXPR;
33886 rhs = integer_one_node;
33887 break;
33889 case COMPOUND_EXPR:
33890 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33891 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33892 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33893 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33894 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33895 (TREE_OPERAND (lhs, 1), 0), 0)))
33896 == BOOLEAN_TYPE)
33897 /* Undo effects of boolean_increment for post {in,de}crement. */
33898 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33899 /* FALLTHRU */
33900 case MODIFY_EXPR:
33901 if (TREE_CODE (lhs) == MODIFY_EXPR
33902 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33904 /* Undo effects of boolean_increment. */
33905 if (integer_onep (TREE_OPERAND (lhs, 1)))
33907 /* This is pre or post increment. */
33908 rhs = TREE_OPERAND (lhs, 1);
33909 lhs = TREE_OPERAND (lhs, 0);
33910 opcode = NOP_EXPR;
33911 if (code == OMP_ATOMIC_CAPTURE_NEW
33912 && !structured_block
33913 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33914 code = OMP_ATOMIC_CAPTURE_OLD;
33915 break;
33918 /* FALLTHRU */
33919 default:
33920 switch (cp_lexer_peek_token (parser->lexer)->type)
33922 case CPP_MULT_EQ:
33923 opcode = MULT_EXPR;
33924 break;
33925 case CPP_DIV_EQ:
33926 opcode = TRUNC_DIV_EXPR;
33927 break;
33928 case CPP_PLUS_EQ:
33929 opcode = PLUS_EXPR;
33930 break;
33931 case CPP_MINUS_EQ:
33932 opcode = MINUS_EXPR;
33933 break;
33934 case CPP_LSHIFT_EQ:
33935 opcode = LSHIFT_EXPR;
33936 break;
33937 case CPP_RSHIFT_EQ:
33938 opcode = RSHIFT_EXPR;
33939 break;
33940 case CPP_AND_EQ:
33941 opcode = BIT_AND_EXPR;
33942 break;
33943 case CPP_OR_EQ:
33944 opcode = BIT_IOR_EXPR;
33945 break;
33946 case CPP_XOR_EQ:
33947 opcode = BIT_XOR_EXPR;
33948 break;
33949 case CPP_EQ:
33950 enum cp_parser_prec oprec;
33951 cp_token *token;
33952 cp_lexer_consume_token (parser->lexer);
33953 cp_parser_parse_tentatively (parser);
33954 rhs1 = cp_parser_simple_cast_expression (parser);
33955 if (rhs1 == error_mark_node)
33957 cp_parser_abort_tentative_parse (parser);
33958 cp_parser_simple_cast_expression (parser);
33959 goto saw_error;
33961 token = cp_lexer_peek_token (parser->lexer);
33962 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
33964 cp_parser_abort_tentative_parse (parser);
33965 cp_parser_parse_tentatively (parser);
33966 rhs = cp_parser_binary_expression (parser, false, true,
33967 PREC_NOT_OPERATOR, NULL);
33968 if (rhs == error_mark_node)
33970 cp_parser_abort_tentative_parse (parser);
33971 cp_parser_binary_expression (parser, false, true,
33972 PREC_NOT_OPERATOR, NULL);
33973 goto saw_error;
33975 switch (TREE_CODE (rhs))
33977 case MULT_EXPR:
33978 case TRUNC_DIV_EXPR:
33979 case RDIV_EXPR:
33980 case PLUS_EXPR:
33981 case MINUS_EXPR:
33982 case LSHIFT_EXPR:
33983 case RSHIFT_EXPR:
33984 case BIT_AND_EXPR:
33985 case BIT_IOR_EXPR:
33986 case BIT_XOR_EXPR:
33987 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
33989 if (cp_parser_parse_definitely (parser))
33991 opcode = TREE_CODE (rhs);
33992 rhs1 = TREE_OPERAND (rhs, 0);
33993 rhs = TREE_OPERAND (rhs, 1);
33994 goto stmt_done;
33996 else
33997 goto saw_error;
33999 break;
34000 default:
34001 break;
34003 cp_parser_abort_tentative_parse (parser);
34004 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34006 rhs = cp_parser_expression (parser);
34007 if (rhs == error_mark_node)
34008 goto saw_error;
34009 opcode = NOP_EXPR;
34010 rhs1 = NULL_TREE;
34011 goto stmt_done;
34013 cp_parser_error (parser,
34014 "invalid form of %<#pragma omp atomic%>");
34015 goto saw_error;
34017 if (!cp_parser_parse_definitely (parser))
34018 goto saw_error;
34019 switch (token->type)
34021 case CPP_SEMICOLON:
34022 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34024 code = OMP_ATOMIC_CAPTURE_OLD;
34025 v = lhs;
34026 lhs = NULL_TREE;
34027 lhs1 = rhs1;
34028 rhs1 = NULL_TREE;
34029 cp_lexer_consume_token (parser->lexer);
34030 goto restart;
34032 else if (structured_block)
34034 opcode = NOP_EXPR;
34035 rhs = rhs1;
34036 rhs1 = NULL_TREE;
34037 goto stmt_done;
34039 cp_parser_error (parser,
34040 "invalid form of %<#pragma omp atomic%>");
34041 goto saw_error;
34042 case CPP_MULT:
34043 opcode = MULT_EXPR;
34044 break;
34045 case CPP_DIV:
34046 opcode = TRUNC_DIV_EXPR;
34047 break;
34048 case CPP_PLUS:
34049 opcode = PLUS_EXPR;
34050 break;
34051 case CPP_MINUS:
34052 opcode = MINUS_EXPR;
34053 break;
34054 case CPP_LSHIFT:
34055 opcode = LSHIFT_EXPR;
34056 break;
34057 case CPP_RSHIFT:
34058 opcode = RSHIFT_EXPR;
34059 break;
34060 case CPP_AND:
34061 opcode = BIT_AND_EXPR;
34062 break;
34063 case CPP_OR:
34064 opcode = BIT_IOR_EXPR;
34065 break;
34066 case CPP_XOR:
34067 opcode = BIT_XOR_EXPR;
34068 break;
34069 default:
34070 cp_parser_error (parser,
34071 "invalid operator for %<#pragma omp atomic%>");
34072 goto saw_error;
34074 oprec = TOKEN_PRECEDENCE (token);
34075 gcc_assert (oprec != PREC_NOT_OPERATOR);
34076 if (commutative_tree_code (opcode))
34077 oprec = (enum cp_parser_prec) (oprec - 1);
34078 cp_lexer_consume_token (parser->lexer);
34079 rhs = cp_parser_binary_expression (parser, false, false,
34080 oprec, NULL);
34081 if (rhs == error_mark_node)
34082 goto saw_error;
34083 goto stmt_done;
34084 /* FALLTHROUGH */
34085 default:
34086 cp_parser_error (parser,
34087 "invalid operator for %<#pragma omp atomic%>");
34088 goto saw_error;
34090 cp_lexer_consume_token (parser->lexer);
34092 rhs = cp_parser_expression (parser);
34093 if (rhs == error_mark_node)
34094 goto saw_error;
34095 break;
34097 stmt_done:
34098 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34100 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34101 goto saw_error;
34102 v = cp_parser_unary_expression (parser);
34103 if (v == error_mark_node)
34104 goto saw_error;
34105 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34106 goto saw_error;
34107 lhs1 = cp_parser_unary_expression (parser);
34108 if (lhs1 == error_mark_node)
34109 goto saw_error;
34111 if (structured_block)
34113 cp_parser_consume_semicolon_at_end_of_statement (parser);
34114 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34116 done:
34117 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34118 if (!structured_block)
34119 cp_parser_consume_semicolon_at_end_of_statement (parser);
34120 return;
34122 saw_error:
34123 cp_parser_skip_to_end_of_block_or_statement (parser);
34124 if (structured_block)
34126 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34127 cp_lexer_consume_token (parser->lexer);
34128 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34130 cp_parser_skip_to_end_of_block_or_statement (parser);
34131 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34132 cp_lexer_consume_token (parser->lexer);
34138 /* OpenMP 2.5:
34139 # pragma omp barrier new-line */
34141 static void
34142 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34144 cp_parser_require_pragma_eol (parser, pragma_tok);
34145 finish_omp_barrier ();
34148 /* OpenMP 2.5:
34149 # pragma omp critical [(name)] new-line
34150 structured-block
34152 OpenMP 4.5:
34153 # pragma omp critical [(name) [hint(expression)]] new-line
34154 structured-block */
34156 #define OMP_CRITICAL_CLAUSE_MASK \
34157 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34159 static tree
34160 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34162 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34164 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34166 cp_lexer_consume_token (parser->lexer);
34168 name = cp_parser_identifier (parser);
34170 if (name == error_mark_node
34171 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34172 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34173 /*or_comma=*/false,
34174 /*consume_paren=*/true);
34175 if (name == error_mark_node)
34176 name = NULL;
34178 clauses = cp_parser_omp_all_clauses (parser,
34179 OMP_CRITICAL_CLAUSE_MASK,
34180 "#pragma omp critical", pragma_tok);
34182 else
34183 cp_parser_require_pragma_eol (parser, pragma_tok);
34185 stmt = cp_parser_omp_structured_block (parser, if_p);
34186 return c_finish_omp_critical (input_location, stmt, name, clauses);
34189 /* OpenMP 2.5:
34190 # pragma omp flush flush-vars[opt] new-line
34192 flush-vars:
34193 ( variable-list ) */
34195 static void
34196 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34199 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34200 cp_parser_require_pragma_eol (parser, pragma_tok);
34202 finish_omp_flush ();
34205 /* Helper function, to parse omp for increment expression. */
34207 static tree
34208 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34210 tree cond = cp_parser_binary_expression (parser, false, true,
34211 PREC_NOT_OPERATOR, NULL);
34212 if (cond == error_mark_node
34213 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34215 cp_parser_skip_to_end_of_statement (parser);
34216 return error_mark_node;
34219 switch (TREE_CODE (cond))
34221 case GT_EXPR:
34222 case GE_EXPR:
34223 case LT_EXPR:
34224 case LE_EXPR:
34225 break;
34226 case NE_EXPR:
34227 if (code == CILK_SIMD || code == CILK_FOR)
34228 break;
34229 /* Fall through: OpenMP disallows NE_EXPR. */
34230 gcc_fallthrough ();
34231 default:
34232 return error_mark_node;
34235 /* If decl is an iterator, preserve LHS and RHS of the relational
34236 expr until finish_omp_for. */
34237 if (decl
34238 && (type_dependent_expression_p (decl)
34239 || CLASS_TYPE_P (TREE_TYPE (decl))))
34240 return cond;
34242 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34243 TREE_CODE (cond),
34244 TREE_OPERAND (cond, 0), ERROR_MARK,
34245 TREE_OPERAND (cond, 1), ERROR_MARK,
34246 /*overload=*/NULL, tf_warning_or_error);
34249 /* Helper function, to parse omp for increment expression. */
34251 static tree
34252 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34254 cp_token *token = cp_lexer_peek_token (parser->lexer);
34255 enum tree_code op;
34256 tree lhs, rhs;
34257 cp_id_kind idk;
34258 bool decl_first;
34260 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34262 op = (token->type == CPP_PLUS_PLUS
34263 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34264 cp_lexer_consume_token (parser->lexer);
34265 lhs = cp_parser_simple_cast_expression (parser);
34266 if (lhs != decl
34267 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34268 return error_mark_node;
34269 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34272 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34273 if (lhs != decl
34274 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34275 return error_mark_node;
34277 token = cp_lexer_peek_token (parser->lexer);
34278 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34280 op = (token->type == CPP_PLUS_PLUS
34281 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34282 cp_lexer_consume_token (parser->lexer);
34283 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34286 op = cp_parser_assignment_operator_opt (parser);
34287 if (op == ERROR_MARK)
34288 return error_mark_node;
34290 if (op != NOP_EXPR)
34292 rhs = cp_parser_assignment_expression (parser);
34293 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34294 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34297 lhs = cp_parser_binary_expression (parser, false, false,
34298 PREC_ADDITIVE_EXPRESSION, NULL);
34299 token = cp_lexer_peek_token (parser->lexer);
34300 decl_first = (lhs == decl
34301 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34302 if (decl_first)
34303 lhs = NULL_TREE;
34304 if (token->type != CPP_PLUS
34305 && token->type != CPP_MINUS)
34306 return error_mark_node;
34310 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34311 cp_lexer_consume_token (parser->lexer);
34312 rhs = cp_parser_binary_expression (parser, false, false,
34313 PREC_ADDITIVE_EXPRESSION, NULL);
34314 token = cp_lexer_peek_token (parser->lexer);
34315 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34317 if (lhs == NULL_TREE)
34319 if (op == PLUS_EXPR)
34320 lhs = rhs;
34321 else
34322 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34323 tf_warning_or_error);
34325 else
34326 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34327 ERROR_MARK, NULL, tf_warning_or_error);
34330 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34332 if (!decl_first)
34334 if ((rhs != decl
34335 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34336 || op == MINUS_EXPR)
34337 return error_mark_node;
34338 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34340 else
34341 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34343 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34346 /* Parse the initialization statement of either an OpenMP for loop or
34347 a Cilk Plus for loop.
34349 Return true if the resulting construct should have an
34350 OMP_CLAUSE_PRIVATE added to it. */
34352 static tree
34353 cp_parser_omp_for_loop_init (cp_parser *parser,
34354 enum tree_code code,
34355 tree &this_pre_body,
34356 vec<tree, va_gc> *for_block,
34357 tree &init,
34358 tree &orig_init,
34359 tree &decl,
34360 tree &real_decl)
34362 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34363 return NULL_TREE;
34365 tree add_private_clause = NULL_TREE;
34367 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34369 init-expr:
34370 var = lb
34371 integer-type var = lb
34372 random-access-iterator-type var = lb
34373 pointer-type var = lb
34375 cp_decl_specifier_seq type_specifiers;
34377 /* First, try to parse as an initialized declaration. See
34378 cp_parser_condition, from whence the bulk of this is copied. */
34380 cp_parser_parse_tentatively (parser);
34381 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34382 /*is_trailing_return=*/false,
34383 &type_specifiers);
34384 if (cp_parser_parse_definitely (parser))
34386 /* If parsing a type specifier seq succeeded, then this
34387 MUST be a initialized declaration. */
34388 tree asm_specification, attributes;
34389 cp_declarator *declarator;
34391 declarator = cp_parser_declarator (parser,
34392 CP_PARSER_DECLARATOR_NAMED,
34393 /*ctor_dtor_or_conv_p=*/NULL,
34394 /*parenthesized_p=*/NULL,
34395 /*member_p=*/false,
34396 /*friend_p=*/false);
34397 attributes = cp_parser_attributes_opt (parser);
34398 asm_specification = cp_parser_asm_specification_opt (parser);
34400 if (declarator == cp_error_declarator)
34401 cp_parser_skip_to_end_of_statement (parser);
34403 else
34405 tree pushed_scope, auto_node;
34407 decl = start_decl (declarator, &type_specifiers,
34408 SD_INITIALIZED, attributes,
34409 /*prefix_attributes=*/NULL_TREE,
34410 &pushed_scope);
34412 auto_node = type_uses_auto (TREE_TYPE (decl));
34413 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34415 if (cp_lexer_next_token_is (parser->lexer,
34416 CPP_OPEN_PAREN))
34418 if (code != CILK_SIMD && code != CILK_FOR)
34419 error ("parenthesized initialization is not allowed in "
34420 "OpenMP %<for%> loop");
34421 else
34422 error ("parenthesized initialization is "
34423 "not allowed in for-loop");
34425 else
34426 /* Trigger an error. */
34427 cp_parser_require (parser, CPP_EQ, RT_EQ);
34429 init = error_mark_node;
34430 cp_parser_skip_to_end_of_statement (parser);
34432 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34433 || type_dependent_expression_p (decl)
34434 || auto_node)
34436 bool is_direct_init, is_non_constant_init;
34438 init = cp_parser_initializer (parser,
34439 &is_direct_init,
34440 &is_non_constant_init);
34442 if (auto_node)
34444 TREE_TYPE (decl)
34445 = do_auto_deduction (TREE_TYPE (decl), init,
34446 auto_node);
34448 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34449 && !type_dependent_expression_p (decl))
34450 goto non_class;
34453 cp_finish_decl (decl, init, !is_non_constant_init,
34454 asm_specification,
34455 LOOKUP_ONLYCONVERTING);
34456 orig_init = init;
34457 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34459 vec_safe_push (for_block, this_pre_body);
34460 init = NULL_TREE;
34462 else
34464 init = pop_stmt_list (this_pre_body);
34465 if (init && TREE_CODE (init) == STATEMENT_LIST)
34467 tree_stmt_iterator i = tsi_start (init);
34468 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34469 while (!tsi_end_p (i))
34471 tree t = tsi_stmt (i);
34472 if (TREE_CODE (t) == DECL_EXPR
34473 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34475 tsi_delink (&i);
34476 vec_safe_push (for_block, t);
34477 continue;
34479 break;
34481 if (tsi_one_before_end_p (i))
34483 tree t = tsi_stmt (i);
34484 tsi_delink (&i);
34485 free_stmt_list (init);
34486 init = t;
34490 this_pre_body = NULL_TREE;
34492 else
34494 /* Consume '='. */
34495 cp_lexer_consume_token (parser->lexer);
34496 init = cp_parser_assignment_expression (parser);
34498 non_class:
34499 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34500 init = error_mark_node;
34501 else
34502 cp_finish_decl (decl, NULL_TREE,
34503 /*init_const_expr_p=*/false,
34504 asm_specification,
34505 LOOKUP_ONLYCONVERTING);
34508 if (pushed_scope)
34509 pop_scope (pushed_scope);
34512 else
34514 cp_id_kind idk;
34515 /* If parsing a type specifier sequence failed, then
34516 this MUST be a simple expression. */
34517 if (code == CILK_FOR)
34518 error ("%<_Cilk_for%> allows expression instead of declaration only "
34519 "in C, not in C++");
34520 cp_parser_parse_tentatively (parser);
34521 decl = cp_parser_primary_expression (parser, false, false,
34522 false, &idk);
34523 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34524 if (!cp_parser_error_occurred (parser)
34525 && decl
34526 && (TREE_CODE (decl) == COMPONENT_REF
34527 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34529 cp_parser_abort_tentative_parse (parser);
34530 cp_parser_parse_tentatively (parser);
34531 cp_token *token = cp_lexer_peek_token (parser->lexer);
34532 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34533 /*check_dependency_p=*/true,
34534 /*template_p=*/NULL,
34535 /*declarator_p=*/false,
34536 /*optional_p=*/false);
34537 if (name != error_mark_node
34538 && last_tok == cp_lexer_peek_token (parser->lexer))
34540 decl = cp_parser_lookup_name_simple (parser, name,
34541 token->location);
34542 if (TREE_CODE (decl) == FIELD_DECL)
34543 add_private_clause = omp_privatize_field (decl, false);
34545 cp_parser_abort_tentative_parse (parser);
34546 cp_parser_parse_tentatively (parser);
34547 decl = cp_parser_primary_expression (parser, false, false,
34548 false, &idk);
34550 if (!cp_parser_error_occurred (parser)
34551 && decl
34552 && DECL_P (decl)
34553 && CLASS_TYPE_P (TREE_TYPE (decl)))
34555 tree rhs;
34557 cp_parser_parse_definitely (parser);
34558 cp_parser_require (parser, CPP_EQ, RT_EQ);
34559 rhs = cp_parser_assignment_expression (parser);
34560 orig_init = rhs;
34561 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34562 decl, NOP_EXPR,
34563 rhs,
34564 tf_warning_or_error));
34565 if (!add_private_clause)
34566 add_private_clause = decl;
34568 else
34570 decl = NULL;
34571 cp_parser_abort_tentative_parse (parser);
34572 init = cp_parser_expression (parser);
34573 if (init)
34575 if (TREE_CODE (init) == MODIFY_EXPR
34576 || TREE_CODE (init) == MODOP_EXPR)
34577 real_decl = TREE_OPERAND (init, 0);
34581 return add_private_clause;
34584 /* Parse the restricted form of the for statement allowed by OpenMP. */
34586 static tree
34587 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34588 tree *cclauses, bool *if_p)
34590 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34591 tree real_decl, initv, condv, incrv, declv;
34592 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34593 location_t loc_first;
34594 bool collapse_err = false;
34595 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34596 vec<tree, va_gc> *for_block = make_tree_vector ();
34597 auto_vec<tree, 4> orig_inits;
34598 bool tiling = false;
34600 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34601 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34602 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34603 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34605 tiling = true;
34606 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34608 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34609 && OMP_CLAUSE_ORDERED_EXPR (cl))
34611 ordered_cl = cl;
34612 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34615 if (ordered && ordered < collapse)
34617 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34618 "%<ordered%> clause parameter is less than %<collapse%>");
34619 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34620 = build_int_cst (NULL_TREE, collapse);
34621 ordered = collapse;
34623 if (ordered)
34625 for (tree *pc = &clauses; *pc; )
34626 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34628 error_at (OMP_CLAUSE_LOCATION (*pc),
34629 "%<linear%> clause may not be specified together "
34630 "with %<ordered%> clause with a parameter");
34631 *pc = OMP_CLAUSE_CHAIN (*pc);
34633 else
34634 pc = &OMP_CLAUSE_CHAIN (*pc);
34637 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34638 count = ordered ? ordered : collapse;
34640 declv = make_tree_vec (count);
34641 initv = make_tree_vec (count);
34642 condv = make_tree_vec (count);
34643 incrv = make_tree_vec (count);
34645 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34647 for (i = 0; i < count; i++)
34649 int bracecount = 0;
34650 tree add_private_clause = NULL_TREE;
34651 location_t loc;
34653 if (code != CILK_FOR
34654 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34656 if (!collapse_err)
34657 cp_parser_error (parser, "for statement expected");
34658 return NULL;
34660 if (code == CILK_FOR
34661 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34663 if (!collapse_err)
34664 cp_parser_error (parser, "_Cilk_for statement expected");
34665 return NULL;
34667 loc = cp_lexer_consume_token (parser->lexer)->location;
34669 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34670 return NULL;
34672 init = orig_init = decl = real_decl = NULL;
34673 this_pre_body = push_stmt_list ();
34675 add_private_clause
34676 = cp_parser_omp_for_loop_init (parser, code,
34677 this_pre_body, for_block,
34678 init, orig_init, decl, real_decl);
34680 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34681 if (this_pre_body)
34683 this_pre_body = pop_stmt_list (this_pre_body);
34684 if (pre_body)
34686 tree t = pre_body;
34687 pre_body = push_stmt_list ();
34688 add_stmt (t);
34689 add_stmt (this_pre_body);
34690 pre_body = pop_stmt_list (pre_body);
34692 else
34693 pre_body = this_pre_body;
34696 if (decl)
34697 real_decl = decl;
34698 if (cclauses != NULL
34699 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34700 && real_decl != NULL_TREE)
34702 tree *c;
34703 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34704 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34705 && OMP_CLAUSE_DECL (*c) == real_decl)
34707 error_at (loc, "iteration variable %qD"
34708 " should not be firstprivate", real_decl);
34709 *c = OMP_CLAUSE_CHAIN (*c);
34711 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34712 && OMP_CLAUSE_DECL (*c) == real_decl)
34714 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34715 tree l = *c;
34716 *c = OMP_CLAUSE_CHAIN (*c);
34717 if (code == OMP_SIMD)
34719 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34720 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34722 else
34724 OMP_CLAUSE_CHAIN (l) = clauses;
34725 clauses = l;
34727 add_private_clause = NULL_TREE;
34729 else
34731 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34732 && OMP_CLAUSE_DECL (*c) == real_decl)
34733 add_private_clause = NULL_TREE;
34734 c = &OMP_CLAUSE_CHAIN (*c);
34738 if (add_private_clause)
34740 tree c;
34741 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34743 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34744 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34745 && OMP_CLAUSE_DECL (c) == decl)
34746 break;
34747 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34748 && OMP_CLAUSE_DECL (c) == decl)
34749 error_at (loc, "iteration variable %qD "
34750 "should not be firstprivate",
34751 decl);
34752 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34753 && OMP_CLAUSE_DECL (c) == decl)
34754 error_at (loc, "iteration variable %qD should not be reduction",
34755 decl);
34757 if (c == NULL)
34759 if (code != OMP_SIMD)
34760 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34761 else if (collapse == 1)
34762 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34763 else
34764 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34765 OMP_CLAUSE_DECL (c) = add_private_clause;
34766 c = finish_omp_clauses (c, C_ORT_OMP);
34767 if (c)
34769 OMP_CLAUSE_CHAIN (c) = clauses;
34770 clauses = c;
34771 /* For linear, signal that we need to fill up
34772 the so far unknown linear step. */
34773 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34774 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34779 cond = NULL;
34780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34781 cond = cp_parser_omp_for_cond (parser, decl, code);
34782 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34784 incr = NULL;
34785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34787 /* If decl is an iterator, preserve the operator on decl
34788 until finish_omp_for. */
34789 if (real_decl
34790 && ((processing_template_decl
34791 && (TREE_TYPE (real_decl) == NULL_TREE
34792 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34793 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34794 incr = cp_parser_omp_for_incr (parser, real_decl);
34795 else
34796 incr = cp_parser_expression (parser);
34797 if (!EXPR_HAS_LOCATION (incr))
34798 protected_set_expr_location (incr, input_location);
34801 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34802 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34803 /*or_comma=*/false,
34804 /*consume_paren=*/true);
34806 TREE_VEC_ELT (declv, i) = decl;
34807 TREE_VEC_ELT (initv, i) = init;
34808 TREE_VEC_ELT (condv, i) = cond;
34809 TREE_VEC_ELT (incrv, i) = incr;
34810 if (orig_init)
34812 orig_inits.safe_grow_cleared (i + 1);
34813 orig_inits[i] = orig_init;
34816 if (i == count - 1)
34817 break;
34819 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34820 in between the collapsed for loops to be still considered perfectly
34821 nested. Hopefully the final version clarifies this.
34822 For now handle (multiple) {'s and empty statements. */
34823 cp_parser_parse_tentatively (parser);
34824 for (;;)
34826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34827 break;
34828 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34830 cp_lexer_consume_token (parser->lexer);
34831 bracecount++;
34833 else if (bracecount
34834 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34835 cp_lexer_consume_token (parser->lexer);
34836 else
34838 loc = cp_lexer_peek_token (parser->lexer)->location;
34839 error_at (loc, "not enough for loops to collapse");
34840 collapse_err = true;
34841 cp_parser_abort_tentative_parse (parser);
34842 declv = NULL_TREE;
34843 break;
34847 if (declv)
34849 cp_parser_parse_definitely (parser);
34850 nbraces += bracecount;
34854 if (nbraces)
34855 if_p = NULL;
34857 /* Note that we saved the original contents of this flag when we entered
34858 the structured block, and so we don't need to re-save it here. */
34859 if (code == CILK_SIMD || code == CILK_FOR)
34860 parser->in_statement = IN_CILK_SIMD_FOR;
34861 else
34862 parser->in_statement = IN_OMP_FOR;
34864 /* Note that the grammar doesn't call for a structured block here,
34865 though the loop as a whole is a structured block. */
34866 body = push_stmt_list ();
34867 cp_parser_statement (parser, NULL_TREE, false, if_p);
34868 body = pop_stmt_list (body);
34870 if (declv == NULL_TREE)
34871 ret = NULL_TREE;
34872 else
34873 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34874 body, pre_body, &orig_inits, clauses);
34876 while (nbraces)
34878 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34880 cp_lexer_consume_token (parser->lexer);
34881 nbraces--;
34883 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34884 cp_lexer_consume_token (parser->lexer);
34885 else
34887 if (!collapse_err)
34889 error_at (cp_lexer_peek_token (parser->lexer)->location,
34890 "collapsed loops not perfectly nested");
34892 collapse_err = true;
34893 cp_parser_statement_seq_opt (parser, NULL);
34894 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34895 break;
34899 while (!for_block->is_empty ())
34901 tree t = for_block->pop ();
34902 if (TREE_CODE (t) == STATEMENT_LIST)
34903 add_stmt (pop_stmt_list (t));
34904 else
34905 add_stmt (t);
34907 release_tree_vector (for_block);
34909 return ret;
34912 /* Helper function for OpenMP parsing, split clauses and call
34913 finish_omp_clauses on each of the set of clauses afterwards. */
34915 static void
34916 cp_omp_split_clauses (location_t loc, enum tree_code code,
34917 omp_clause_mask mask, tree clauses, tree *cclauses)
34919 int i;
34920 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34921 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34922 if (cclauses[i])
34923 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34926 /* OpenMP 4.0:
34927 #pragma omp simd simd-clause[optseq] new-line
34928 for-loop */
34930 #define OMP_SIMD_CLAUSE_MASK \
34931 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34932 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34933 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34934 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34935 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34936 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34937 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34938 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34940 static tree
34941 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34942 char *p_name, omp_clause_mask mask, tree *cclauses,
34943 bool *if_p)
34945 tree clauses, sb, ret;
34946 unsigned int save;
34947 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34949 strcat (p_name, " simd");
34950 mask |= OMP_SIMD_CLAUSE_MASK;
34952 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
34953 cclauses == NULL);
34954 if (cclauses)
34956 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
34957 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
34958 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
34959 OMP_CLAUSE_ORDERED);
34960 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
34962 error_at (OMP_CLAUSE_LOCATION (c),
34963 "%<ordered%> clause with parameter may not be specified "
34964 "on %qs construct", p_name);
34965 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
34969 sb = begin_omp_structured_block ();
34970 save = cp_parser_begin_omp_structured_block (parser);
34972 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
34974 cp_parser_end_omp_structured_block (parser, save);
34975 add_stmt (finish_omp_structured_block (sb));
34977 return ret;
34980 /* OpenMP 2.5:
34981 #pragma omp for for-clause[optseq] new-line
34982 for-loop
34984 OpenMP 4.0:
34985 #pragma omp for simd for-simd-clause[optseq] new-line
34986 for-loop */
34988 #define OMP_FOR_CLAUSE_MASK \
34989 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34990 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
34991 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34992 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34993 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34994 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
34995 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
34996 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
34997 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34999 static tree
35000 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35001 char *p_name, omp_clause_mask mask, tree *cclauses,
35002 bool *if_p)
35004 tree clauses, sb, ret;
35005 unsigned int save;
35006 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35008 strcat (p_name, " for");
35009 mask |= OMP_FOR_CLAUSE_MASK;
35010 /* parallel for{, simd} disallows nowait clause, but for
35011 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35012 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35013 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35014 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35015 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35016 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35018 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35020 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35021 const char *p = IDENTIFIER_POINTER (id);
35023 if (strcmp (p, "simd") == 0)
35025 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35026 if (cclauses == NULL)
35027 cclauses = cclauses_buf;
35029 cp_lexer_consume_token (parser->lexer);
35030 if (!flag_openmp) /* flag_openmp_simd */
35031 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35032 cclauses, if_p);
35033 sb = begin_omp_structured_block ();
35034 save = cp_parser_begin_omp_structured_block (parser);
35035 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35036 cclauses, if_p);
35037 cp_parser_end_omp_structured_block (parser, save);
35038 tree body = finish_omp_structured_block (sb);
35039 if (ret == NULL)
35040 return ret;
35041 ret = make_node (OMP_FOR);
35042 TREE_TYPE (ret) = void_type_node;
35043 OMP_FOR_BODY (ret) = body;
35044 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35045 SET_EXPR_LOCATION (ret, loc);
35046 add_stmt (ret);
35047 return ret;
35050 if (!flag_openmp) /* flag_openmp_simd */
35052 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35053 return NULL_TREE;
35056 /* Composite distribute parallel for disallows linear clause. */
35057 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35058 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35060 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35061 cclauses == NULL);
35062 if (cclauses)
35064 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35065 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35068 sb = begin_omp_structured_block ();
35069 save = cp_parser_begin_omp_structured_block (parser);
35071 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35073 cp_parser_end_omp_structured_block (parser, save);
35074 add_stmt (finish_omp_structured_block (sb));
35076 return ret;
35079 /* OpenMP 2.5:
35080 # pragma omp master new-line
35081 structured-block */
35083 static tree
35084 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35086 cp_parser_require_pragma_eol (parser, pragma_tok);
35087 return c_finish_omp_master (input_location,
35088 cp_parser_omp_structured_block (parser, if_p));
35091 /* OpenMP 2.5:
35092 # pragma omp ordered new-line
35093 structured-block
35095 OpenMP 4.5:
35096 # pragma omp ordered ordered-clauses new-line
35097 structured-block */
35099 #define OMP_ORDERED_CLAUSE_MASK \
35100 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35101 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35103 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35104 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35106 static bool
35107 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35108 enum pragma_context context, bool *if_p)
35110 location_t loc = pragma_tok->location;
35112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35114 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35115 const char *p = IDENTIFIER_POINTER (id);
35117 if (strcmp (p, "depend") == 0)
35119 if (context == pragma_stmt)
35121 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35122 "%<depend%> clause may only be used in compound "
35123 "statements");
35124 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35125 return false;
35127 tree clauses
35128 = cp_parser_omp_all_clauses (parser,
35129 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35130 "#pragma omp ordered", pragma_tok);
35131 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35132 return false;
35136 tree clauses
35137 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35138 "#pragma omp ordered", pragma_tok);
35139 c_finish_omp_ordered (loc, clauses,
35140 cp_parser_omp_structured_block (parser, if_p));
35141 return true;
35144 /* OpenMP 2.5:
35146 section-scope:
35147 { section-sequence }
35149 section-sequence:
35150 section-directive[opt] structured-block
35151 section-sequence section-directive structured-block */
35153 static tree
35154 cp_parser_omp_sections_scope (cp_parser *parser)
35156 tree stmt, substmt;
35157 bool error_suppress = false;
35158 cp_token *tok;
35160 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35161 return NULL_TREE;
35163 stmt = push_stmt_list ();
35165 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35166 != PRAGMA_OMP_SECTION)
35168 substmt = cp_parser_omp_structured_block (parser, NULL);
35169 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35170 add_stmt (substmt);
35173 while (1)
35175 tok = cp_lexer_peek_token (parser->lexer);
35176 if (tok->type == CPP_CLOSE_BRACE)
35177 break;
35178 if (tok->type == CPP_EOF)
35179 break;
35181 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35183 cp_lexer_consume_token (parser->lexer);
35184 cp_parser_require_pragma_eol (parser, tok);
35185 error_suppress = false;
35187 else if (!error_suppress)
35189 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35190 error_suppress = true;
35193 substmt = cp_parser_omp_structured_block (parser, NULL);
35194 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35195 add_stmt (substmt);
35197 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35199 substmt = pop_stmt_list (stmt);
35201 stmt = make_node (OMP_SECTIONS);
35202 TREE_TYPE (stmt) = void_type_node;
35203 OMP_SECTIONS_BODY (stmt) = substmt;
35205 add_stmt (stmt);
35206 return stmt;
35209 /* OpenMP 2.5:
35210 # pragma omp sections sections-clause[optseq] newline
35211 sections-scope */
35213 #define OMP_SECTIONS_CLAUSE_MASK \
35214 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35215 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35216 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35217 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35218 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35220 static tree
35221 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35222 char *p_name, omp_clause_mask mask, tree *cclauses)
35224 tree clauses, ret;
35225 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35227 strcat (p_name, " sections");
35228 mask |= OMP_SECTIONS_CLAUSE_MASK;
35229 if (cclauses)
35230 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35232 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35233 cclauses == NULL);
35234 if (cclauses)
35236 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35237 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35240 ret = cp_parser_omp_sections_scope (parser);
35241 if (ret)
35242 OMP_SECTIONS_CLAUSES (ret) = clauses;
35244 return ret;
35247 /* OpenMP 2.5:
35248 # pragma omp parallel parallel-clause[optseq] new-line
35249 structured-block
35250 # pragma omp parallel for parallel-for-clause[optseq] new-line
35251 structured-block
35252 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35253 structured-block
35255 OpenMP 4.0:
35256 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35257 structured-block */
35259 #define OMP_PARALLEL_CLAUSE_MASK \
35260 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35261 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35262 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35263 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35264 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35270 static tree
35271 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35272 char *p_name, omp_clause_mask mask, tree *cclauses,
35273 bool *if_p)
35275 tree stmt, clauses, block;
35276 unsigned int save;
35277 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35279 strcat (p_name, " parallel");
35280 mask |= OMP_PARALLEL_CLAUSE_MASK;
35281 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35282 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35283 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35284 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35286 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35288 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35289 if (cclauses == NULL)
35290 cclauses = cclauses_buf;
35292 cp_lexer_consume_token (parser->lexer);
35293 if (!flag_openmp) /* flag_openmp_simd */
35294 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35295 if_p);
35296 block = begin_omp_parallel ();
35297 save = cp_parser_begin_omp_structured_block (parser);
35298 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35299 if_p);
35300 cp_parser_end_omp_structured_block (parser, save);
35301 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35302 block);
35303 if (ret == NULL_TREE)
35304 return ret;
35305 OMP_PARALLEL_COMBINED (stmt) = 1;
35306 return stmt;
35308 /* When combined with distribute, parallel has to be followed by for.
35309 #pragma omp target parallel is allowed though. */
35310 else if (cclauses
35311 && (mask & (OMP_CLAUSE_MASK_1
35312 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35314 error_at (loc, "expected %<for%> after %qs", p_name);
35315 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35316 return NULL_TREE;
35318 else if (!flag_openmp) /* flag_openmp_simd */
35320 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35321 return NULL_TREE;
35323 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35325 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35326 const char *p = IDENTIFIER_POINTER (id);
35327 if (strcmp (p, "sections") == 0)
35329 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35330 cclauses = cclauses_buf;
35332 cp_lexer_consume_token (parser->lexer);
35333 block = begin_omp_parallel ();
35334 save = cp_parser_begin_omp_structured_block (parser);
35335 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35336 cp_parser_end_omp_structured_block (parser, save);
35337 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35338 block);
35339 OMP_PARALLEL_COMBINED (stmt) = 1;
35340 return stmt;
35344 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35345 cclauses == NULL);
35346 if (cclauses)
35348 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35349 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35352 block = begin_omp_parallel ();
35353 save = cp_parser_begin_omp_structured_block (parser);
35354 cp_parser_statement (parser, NULL_TREE, false, if_p);
35355 cp_parser_end_omp_structured_block (parser, save);
35356 stmt = finish_omp_parallel (clauses, block);
35357 return stmt;
35360 /* OpenMP 2.5:
35361 # pragma omp single single-clause[optseq] new-line
35362 structured-block */
35364 #define OMP_SINGLE_CLAUSE_MASK \
35365 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35366 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35367 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35368 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35370 static tree
35371 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35373 tree stmt = make_node (OMP_SINGLE);
35374 TREE_TYPE (stmt) = void_type_node;
35376 OMP_SINGLE_CLAUSES (stmt)
35377 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35378 "#pragma omp single", pragma_tok);
35379 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35381 return add_stmt (stmt);
35384 /* OpenMP 3.0:
35385 # pragma omp task task-clause[optseq] new-line
35386 structured-block */
35388 #define OMP_TASK_CLAUSE_MASK \
35389 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35390 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35391 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35392 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35393 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35394 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35395 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35396 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35397 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35398 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35400 static tree
35401 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35403 tree clauses, block;
35404 unsigned int save;
35406 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35407 "#pragma omp task", pragma_tok);
35408 block = begin_omp_task ();
35409 save = cp_parser_begin_omp_structured_block (parser);
35410 cp_parser_statement (parser, NULL_TREE, false, if_p);
35411 cp_parser_end_omp_structured_block (parser, save);
35412 return finish_omp_task (clauses, block);
35415 /* OpenMP 3.0:
35416 # pragma omp taskwait new-line */
35418 static void
35419 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35421 cp_parser_require_pragma_eol (parser, pragma_tok);
35422 finish_omp_taskwait ();
35425 /* OpenMP 3.1:
35426 # pragma omp taskyield new-line */
35428 static void
35429 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35431 cp_parser_require_pragma_eol (parser, pragma_tok);
35432 finish_omp_taskyield ();
35435 /* OpenMP 4.0:
35436 # pragma omp taskgroup new-line
35437 structured-block */
35439 static tree
35440 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35442 cp_parser_require_pragma_eol (parser, pragma_tok);
35443 return c_finish_omp_taskgroup (input_location,
35444 cp_parser_omp_structured_block (parser,
35445 if_p));
35449 /* OpenMP 2.5:
35450 # pragma omp threadprivate (variable-list) */
35452 static void
35453 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35455 tree vars;
35457 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35458 cp_parser_require_pragma_eol (parser, pragma_tok);
35460 finish_omp_threadprivate (vars);
35463 /* OpenMP 4.0:
35464 # pragma omp cancel cancel-clause[optseq] new-line */
35466 #define OMP_CANCEL_CLAUSE_MASK \
35467 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35469 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35470 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35471 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35473 static void
35474 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35476 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35477 "#pragma omp cancel", pragma_tok);
35478 finish_omp_cancel (clauses);
35481 /* OpenMP 4.0:
35482 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35484 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35485 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35486 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35487 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35488 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35490 static void
35491 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35492 enum pragma_context context)
35494 tree clauses;
35495 bool point_seen = false;
35497 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35499 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35500 const char *p = IDENTIFIER_POINTER (id);
35502 if (strcmp (p, "point") == 0)
35504 cp_lexer_consume_token (parser->lexer);
35505 point_seen = true;
35508 if (!point_seen)
35510 cp_parser_error (parser, "expected %<point%>");
35511 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35512 return;
35515 if (context != pragma_compound)
35517 if (context == pragma_stmt)
35518 error_at (pragma_tok->location,
35519 "%<#pragma %s%> may only be used in compound statements",
35520 "omp cancellation point");
35521 else
35522 cp_parser_error (parser, "expected declaration specifiers");
35523 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35524 return;
35527 clauses = cp_parser_omp_all_clauses (parser,
35528 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35529 "#pragma omp cancellation point",
35530 pragma_tok);
35531 finish_omp_cancellation_point (clauses);
35534 /* OpenMP 4.0:
35535 #pragma omp distribute distribute-clause[optseq] new-line
35536 for-loop */
35538 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35539 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35540 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35541 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35542 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35543 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35545 static tree
35546 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35547 char *p_name, omp_clause_mask mask, tree *cclauses,
35548 bool *if_p)
35550 tree clauses, sb, ret;
35551 unsigned int save;
35552 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35554 strcat (p_name, " distribute");
35555 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35557 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35559 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35560 const char *p = IDENTIFIER_POINTER (id);
35561 bool simd = false;
35562 bool parallel = false;
35564 if (strcmp (p, "simd") == 0)
35565 simd = true;
35566 else
35567 parallel = strcmp (p, "parallel") == 0;
35568 if (parallel || simd)
35570 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35571 if (cclauses == NULL)
35572 cclauses = cclauses_buf;
35573 cp_lexer_consume_token (parser->lexer);
35574 if (!flag_openmp) /* flag_openmp_simd */
35576 if (simd)
35577 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35578 cclauses, if_p);
35579 else
35580 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35581 cclauses, if_p);
35583 sb = begin_omp_structured_block ();
35584 save = cp_parser_begin_omp_structured_block (parser);
35585 if (simd)
35586 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35587 cclauses, if_p);
35588 else
35589 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35590 cclauses, if_p);
35591 cp_parser_end_omp_structured_block (parser, save);
35592 tree body = finish_omp_structured_block (sb);
35593 if (ret == NULL)
35594 return ret;
35595 ret = make_node (OMP_DISTRIBUTE);
35596 TREE_TYPE (ret) = void_type_node;
35597 OMP_FOR_BODY (ret) = body;
35598 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35599 SET_EXPR_LOCATION (ret, loc);
35600 add_stmt (ret);
35601 return ret;
35604 if (!flag_openmp) /* flag_openmp_simd */
35606 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35607 return NULL_TREE;
35610 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35611 cclauses == NULL);
35612 if (cclauses)
35614 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35615 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35618 sb = begin_omp_structured_block ();
35619 save = cp_parser_begin_omp_structured_block (parser);
35621 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35623 cp_parser_end_omp_structured_block (parser, save);
35624 add_stmt (finish_omp_structured_block (sb));
35626 return ret;
35629 /* OpenMP 4.0:
35630 # pragma omp teams teams-clause[optseq] new-line
35631 structured-block */
35633 #define OMP_TEAMS_CLAUSE_MASK \
35634 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35642 static tree
35643 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35644 char *p_name, omp_clause_mask mask, tree *cclauses,
35645 bool *if_p)
35647 tree clauses, sb, ret;
35648 unsigned int save;
35649 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35651 strcat (p_name, " teams");
35652 mask |= OMP_TEAMS_CLAUSE_MASK;
35654 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35657 const char *p = IDENTIFIER_POINTER (id);
35658 if (strcmp (p, "distribute") == 0)
35660 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35661 if (cclauses == NULL)
35662 cclauses = cclauses_buf;
35664 cp_lexer_consume_token (parser->lexer);
35665 if (!flag_openmp) /* flag_openmp_simd */
35666 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35667 cclauses, if_p);
35668 sb = begin_omp_structured_block ();
35669 save = cp_parser_begin_omp_structured_block (parser);
35670 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35671 cclauses, if_p);
35672 cp_parser_end_omp_structured_block (parser, save);
35673 tree body = finish_omp_structured_block (sb);
35674 if (ret == NULL)
35675 return ret;
35676 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35677 ret = make_node (OMP_TEAMS);
35678 TREE_TYPE (ret) = void_type_node;
35679 OMP_TEAMS_CLAUSES (ret) = clauses;
35680 OMP_TEAMS_BODY (ret) = body;
35681 OMP_TEAMS_COMBINED (ret) = 1;
35682 SET_EXPR_LOCATION (ret, loc);
35683 return add_stmt (ret);
35686 if (!flag_openmp) /* flag_openmp_simd */
35688 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35689 return NULL_TREE;
35692 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35693 cclauses == NULL);
35694 if (cclauses)
35696 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35697 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35700 tree stmt = make_node (OMP_TEAMS);
35701 TREE_TYPE (stmt) = void_type_node;
35702 OMP_TEAMS_CLAUSES (stmt) = clauses;
35703 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35704 SET_EXPR_LOCATION (stmt, loc);
35706 return add_stmt (stmt);
35709 /* OpenMP 4.0:
35710 # pragma omp target data target-data-clause[optseq] new-line
35711 structured-block */
35713 #define OMP_TARGET_DATA_CLAUSE_MASK \
35714 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35717 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35719 static tree
35720 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35722 tree clauses
35723 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35724 "#pragma omp target data", pragma_tok);
35725 int map_seen = 0;
35726 for (tree *pc = &clauses; *pc;)
35728 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35729 switch (OMP_CLAUSE_MAP_KIND (*pc))
35731 case GOMP_MAP_TO:
35732 case GOMP_MAP_ALWAYS_TO:
35733 case GOMP_MAP_FROM:
35734 case GOMP_MAP_ALWAYS_FROM:
35735 case GOMP_MAP_TOFROM:
35736 case GOMP_MAP_ALWAYS_TOFROM:
35737 case GOMP_MAP_ALLOC:
35738 map_seen = 3;
35739 break;
35740 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35741 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35742 case GOMP_MAP_ALWAYS_POINTER:
35743 break;
35744 default:
35745 map_seen |= 1;
35746 error_at (OMP_CLAUSE_LOCATION (*pc),
35747 "%<#pragma omp target data%> with map-type other "
35748 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35749 "on %<map%> clause");
35750 *pc = OMP_CLAUSE_CHAIN (*pc);
35751 continue;
35753 pc = &OMP_CLAUSE_CHAIN (*pc);
35756 if (map_seen != 3)
35758 if (map_seen == 0)
35759 error_at (pragma_tok->location,
35760 "%<#pragma omp target data%> must contain at least "
35761 "one %<map%> clause");
35762 return NULL_TREE;
35765 tree stmt = make_node (OMP_TARGET_DATA);
35766 TREE_TYPE (stmt) = void_type_node;
35767 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35769 keep_next_level (true);
35770 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35772 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35773 return add_stmt (stmt);
35776 /* OpenMP 4.5:
35777 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35778 structured-block */
35780 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35781 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35782 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35783 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35784 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35785 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35787 static tree
35788 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35789 enum pragma_context context)
35791 bool data_seen = false;
35792 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35794 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35795 const char *p = IDENTIFIER_POINTER (id);
35797 if (strcmp (p, "data") == 0)
35799 cp_lexer_consume_token (parser->lexer);
35800 data_seen = true;
35803 if (!data_seen)
35805 cp_parser_error (parser, "expected %<data%>");
35806 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35807 return NULL_TREE;
35810 if (context == pragma_stmt)
35812 error_at (pragma_tok->location,
35813 "%<#pragma %s%> may only be used in compound statements",
35814 "omp target enter data");
35815 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35816 return NULL_TREE;
35819 tree clauses
35820 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35821 "#pragma omp target enter data", pragma_tok);
35822 int map_seen = 0;
35823 for (tree *pc = &clauses; *pc;)
35825 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35826 switch (OMP_CLAUSE_MAP_KIND (*pc))
35828 case GOMP_MAP_TO:
35829 case GOMP_MAP_ALWAYS_TO:
35830 case GOMP_MAP_ALLOC:
35831 map_seen = 3;
35832 break;
35833 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35834 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35835 case GOMP_MAP_ALWAYS_POINTER:
35836 break;
35837 default:
35838 map_seen |= 1;
35839 error_at (OMP_CLAUSE_LOCATION (*pc),
35840 "%<#pragma omp target enter data%> with map-type other "
35841 "than %<to%> or %<alloc%> on %<map%> clause");
35842 *pc = OMP_CLAUSE_CHAIN (*pc);
35843 continue;
35845 pc = &OMP_CLAUSE_CHAIN (*pc);
35848 if (map_seen != 3)
35850 if (map_seen == 0)
35851 error_at (pragma_tok->location,
35852 "%<#pragma omp target enter data%> must contain at least "
35853 "one %<map%> clause");
35854 return NULL_TREE;
35857 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35858 TREE_TYPE (stmt) = void_type_node;
35859 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35860 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35861 return add_stmt (stmt);
35864 /* OpenMP 4.5:
35865 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35866 structured-block */
35868 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35869 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35870 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35871 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35872 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35873 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35875 static tree
35876 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35877 enum pragma_context context)
35879 bool data_seen = false;
35880 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35882 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35883 const char *p = IDENTIFIER_POINTER (id);
35885 if (strcmp (p, "data") == 0)
35887 cp_lexer_consume_token (parser->lexer);
35888 data_seen = true;
35891 if (!data_seen)
35893 cp_parser_error (parser, "expected %<data%>");
35894 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35895 return NULL_TREE;
35898 if (context == pragma_stmt)
35900 error_at (pragma_tok->location,
35901 "%<#pragma %s%> may only be used in compound statements",
35902 "omp target exit data");
35903 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35904 return NULL_TREE;
35907 tree clauses
35908 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35909 "#pragma omp target exit data", pragma_tok);
35910 int map_seen = 0;
35911 for (tree *pc = &clauses; *pc;)
35913 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35914 switch (OMP_CLAUSE_MAP_KIND (*pc))
35916 case GOMP_MAP_FROM:
35917 case GOMP_MAP_ALWAYS_FROM:
35918 case GOMP_MAP_RELEASE:
35919 case GOMP_MAP_DELETE:
35920 map_seen = 3;
35921 break;
35922 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35923 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35924 case GOMP_MAP_ALWAYS_POINTER:
35925 break;
35926 default:
35927 map_seen |= 1;
35928 error_at (OMP_CLAUSE_LOCATION (*pc),
35929 "%<#pragma omp target exit data%> with map-type other "
35930 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35931 " clause");
35932 *pc = OMP_CLAUSE_CHAIN (*pc);
35933 continue;
35935 pc = &OMP_CLAUSE_CHAIN (*pc);
35938 if (map_seen != 3)
35940 if (map_seen == 0)
35941 error_at (pragma_tok->location,
35942 "%<#pragma omp target exit data%> must contain at least "
35943 "one %<map%> clause");
35944 return NULL_TREE;
35947 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35948 TREE_TYPE (stmt) = void_type_node;
35949 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
35950 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35951 return add_stmt (stmt);
35954 /* OpenMP 4.0:
35955 # pragma omp target update target-update-clause[optseq] new-line */
35957 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
35958 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
35959 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
35960 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35961 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35962 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35963 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35965 static bool
35966 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
35967 enum pragma_context context)
35969 if (context == pragma_stmt)
35971 error_at (pragma_tok->location,
35972 "%<#pragma %s%> may only be used in compound statements",
35973 "omp target update");
35974 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35975 return false;
35978 tree clauses
35979 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
35980 "#pragma omp target update", pragma_tok);
35981 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
35982 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
35984 error_at (pragma_tok->location,
35985 "%<#pragma omp target update%> must contain at least one "
35986 "%<from%> or %<to%> clauses");
35987 return false;
35990 tree stmt = make_node (OMP_TARGET_UPDATE);
35991 TREE_TYPE (stmt) = void_type_node;
35992 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
35993 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35994 add_stmt (stmt);
35995 return false;
35998 /* OpenMP 4.0:
35999 # pragma omp target target-clause[optseq] new-line
36000 structured-block */
36002 #define OMP_TARGET_CLAUSE_MASK \
36003 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36004 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36005 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36006 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36007 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36008 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36013 static bool
36014 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36015 enum pragma_context context, bool *if_p)
36017 tree *pc = NULL, stmt;
36019 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36021 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36022 const char *p = IDENTIFIER_POINTER (id);
36023 enum tree_code ccode = ERROR_MARK;
36025 if (strcmp (p, "teams") == 0)
36026 ccode = OMP_TEAMS;
36027 else if (strcmp (p, "parallel") == 0)
36028 ccode = OMP_PARALLEL;
36029 else if (strcmp (p, "simd") == 0)
36030 ccode = OMP_SIMD;
36031 if (ccode != ERROR_MARK)
36033 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36034 char p_name[sizeof ("#pragma omp target teams distribute "
36035 "parallel for simd")];
36037 cp_lexer_consume_token (parser->lexer);
36038 strcpy (p_name, "#pragma omp target");
36039 if (!flag_openmp) /* flag_openmp_simd */
36041 tree stmt;
36042 switch (ccode)
36044 case OMP_TEAMS:
36045 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36046 OMP_TARGET_CLAUSE_MASK,
36047 cclauses, if_p);
36048 break;
36049 case OMP_PARALLEL:
36050 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36051 OMP_TARGET_CLAUSE_MASK,
36052 cclauses, if_p);
36053 break;
36054 case OMP_SIMD:
36055 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36056 OMP_TARGET_CLAUSE_MASK,
36057 cclauses, if_p);
36058 break;
36059 default:
36060 gcc_unreachable ();
36062 return stmt != NULL_TREE;
36064 keep_next_level (true);
36065 tree sb = begin_omp_structured_block (), ret;
36066 unsigned save = cp_parser_begin_omp_structured_block (parser);
36067 switch (ccode)
36069 case OMP_TEAMS:
36070 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36071 OMP_TARGET_CLAUSE_MASK, cclauses,
36072 if_p);
36073 break;
36074 case OMP_PARALLEL:
36075 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36076 OMP_TARGET_CLAUSE_MASK, cclauses,
36077 if_p);
36078 break;
36079 case OMP_SIMD:
36080 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36081 OMP_TARGET_CLAUSE_MASK, cclauses,
36082 if_p);
36083 break;
36084 default:
36085 gcc_unreachable ();
36087 cp_parser_end_omp_structured_block (parser, save);
36088 tree body = finish_omp_structured_block (sb);
36089 if (ret == NULL_TREE)
36090 return false;
36091 if (ccode == OMP_TEAMS && !processing_template_decl)
36093 /* For combined target teams, ensure the num_teams and
36094 thread_limit clause expressions are evaluated on the host,
36095 before entering the target construct. */
36096 tree c;
36097 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36098 c; c = OMP_CLAUSE_CHAIN (c))
36099 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36100 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36101 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36103 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36104 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36105 if (expr == error_mark_node)
36106 continue;
36107 tree tmp = TARGET_EXPR_SLOT (expr);
36108 add_stmt (expr);
36109 OMP_CLAUSE_OPERAND (c, 0) = expr;
36110 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36111 OMP_CLAUSE_FIRSTPRIVATE);
36112 OMP_CLAUSE_DECL (tc) = tmp;
36113 OMP_CLAUSE_CHAIN (tc)
36114 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36115 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36118 tree stmt = make_node (OMP_TARGET);
36119 TREE_TYPE (stmt) = void_type_node;
36120 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36121 OMP_TARGET_BODY (stmt) = body;
36122 OMP_TARGET_COMBINED (stmt) = 1;
36123 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36124 add_stmt (stmt);
36125 pc = &OMP_TARGET_CLAUSES (stmt);
36126 goto check_clauses;
36128 else if (!flag_openmp) /* flag_openmp_simd */
36130 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36131 return false;
36133 else if (strcmp (p, "data") == 0)
36135 cp_lexer_consume_token (parser->lexer);
36136 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36137 return true;
36139 else if (strcmp (p, "enter") == 0)
36141 cp_lexer_consume_token (parser->lexer);
36142 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36143 return false;
36145 else if (strcmp (p, "exit") == 0)
36147 cp_lexer_consume_token (parser->lexer);
36148 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36149 return false;
36151 else if (strcmp (p, "update") == 0)
36153 cp_lexer_consume_token (parser->lexer);
36154 return cp_parser_omp_target_update (parser, pragma_tok, context);
36157 if (!flag_openmp) /* flag_openmp_simd */
36159 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36160 return false;
36163 stmt = make_node (OMP_TARGET);
36164 TREE_TYPE (stmt) = void_type_node;
36166 OMP_TARGET_CLAUSES (stmt)
36167 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36168 "#pragma omp target", pragma_tok);
36169 pc = &OMP_TARGET_CLAUSES (stmt);
36170 keep_next_level (true);
36171 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36173 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36174 add_stmt (stmt);
36176 check_clauses:
36177 while (*pc)
36179 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36180 switch (OMP_CLAUSE_MAP_KIND (*pc))
36182 case GOMP_MAP_TO:
36183 case GOMP_MAP_ALWAYS_TO:
36184 case GOMP_MAP_FROM:
36185 case GOMP_MAP_ALWAYS_FROM:
36186 case GOMP_MAP_TOFROM:
36187 case GOMP_MAP_ALWAYS_TOFROM:
36188 case GOMP_MAP_ALLOC:
36189 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36190 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36191 case GOMP_MAP_ALWAYS_POINTER:
36192 break;
36193 default:
36194 error_at (OMP_CLAUSE_LOCATION (*pc),
36195 "%<#pragma omp target%> with map-type other "
36196 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36197 "on %<map%> clause");
36198 *pc = OMP_CLAUSE_CHAIN (*pc);
36199 continue;
36201 pc = &OMP_CLAUSE_CHAIN (*pc);
36203 return true;
36206 /* OpenACC 2.0:
36207 # pragma acc cache (variable-list) new-line
36210 static tree
36211 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36213 tree stmt, clauses;
36215 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36216 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36218 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36220 stmt = make_node (OACC_CACHE);
36221 TREE_TYPE (stmt) = void_type_node;
36222 OACC_CACHE_CLAUSES (stmt) = clauses;
36223 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36224 add_stmt (stmt);
36226 return stmt;
36229 /* OpenACC 2.0:
36230 # pragma acc data oacc-data-clause[optseq] new-line
36231 structured-block */
36233 #define OACC_DATA_CLAUSE_MASK \
36234 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36235 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36236 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36237 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36238 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36239 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36240 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36241 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36242 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36243 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36244 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36246 static tree
36247 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36249 tree stmt, clauses, block;
36250 unsigned int save;
36252 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36253 "#pragma acc data", pragma_tok);
36255 block = begin_omp_parallel ();
36256 save = cp_parser_begin_omp_structured_block (parser);
36257 cp_parser_statement (parser, NULL_TREE, false, if_p);
36258 cp_parser_end_omp_structured_block (parser, save);
36259 stmt = finish_oacc_data (clauses, block);
36260 return stmt;
36263 /* OpenACC 2.0:
36264 # pragma acc host_data <clauses> new-line
36265 structured-block */
36267 #define OACC_HOST_DATA_CLAUSE_MASK \
36268 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36270 static tree
36271 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36273 tree stmt, clauses, block;
36274 unsigned int save;
36276 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36277 "#pragma acc host_data", pragma_tok);
36279 block = begin_omp_parallel ();
36280 save = cp_parser_begin_omp_structured_block (parser);
36281 cp_parser_statement (parser, NULL_TREE, false, if_p);
36282 cp_parser_end_omp_structured_block (parser, save);
36283 stmt = finish_oacc_host_data (clauses, block);
36284 return stmt;
36287 /* OpenACC 2.0:
36288 # pragma acc declare oacc-data-clause[optseq] new-line
36291 #define OACC_DECLARE_CLAUSE_MASK \
36292 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36295 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36296 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36297 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36298 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36299 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36300 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36301 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36302 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36303 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36305 static tree
36306 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36308 tree clauses, stmt;
36309 bool error = false;
36311 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36312 "#pragma acc declare", pragma_tok, true);
36315 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36317 error_at (pragma_tok->location,
36318 "no valid clauses specified in %<#pragma acc declare%>");
36319 return NULL_TREE;
36322 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36324 location_t loc = OMP_CLAUSE_LOCATION (t);
36325 tree decl = OMP_CLAUSE_DECL (t);
36326 if (!DECL_P (decl))
36328 error_at (loc, "array section in %<#pragma acc declare%>");
36329 error = true;
36330 continue;
36332 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36333 switch (OMP_CLAUSE_MAP_KIND (t))
36335 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36336 case GOMP_MAP_FORCE_ALLOC:
36337 case GOMP_MAP_FORCE_TO:
36338 case GOMP_MAP_FORCE_DEVICEPTR:
36339 case GOMP_MAP_DEVICE_RESIDENT:
36340 break;
36342 case GOMP_MAP_LINK:
36343 if (!global_bindings_p ()
36344 && (TREE_STATIC (decl)
36345 || !DECL_EXTERNAL (decl)))
36347 error_at (loc,
36348 "%qD must be a global variable in "
36349 "%<#pragma acc declare link%>",
36350 decl);
36351 error = true;
36352 continue;
36354 break;
36356 default:
36357 if (global_bindings_p ())
36359 error_at (loc, "invalid OpenACC clause at file scope");
36360 error = true;
36361 continue;
36363 if (DECL_EXTERNAL (decl))
36365 error_at (loc,
36366 "invalid use of %<extern%> variable %qD "
36367 "in %<#pragma acc declare%>", decl);
36368 error = true;
36369 continue;
36371 else if (TREE_PUBLIC (decl))
36373 error_at (loc,
36374 "invalid use of %<global%> variable %qD "
36375 "in %<#pragma acc declare%>", decl);
36376 error = true;
36377 continue;
36379 break;
36382 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36383 || lookup_attribute ("omp declare target link",
36384 DECL_ATTRIBUTES (decl)))
36386 error_at (loc, "variable %qD used more than once with "
36387 "%<#pragma acc declare%>", decl);
36388 error = true;
36389 continue;
36392 if (!error)
36394 tree id;
36396 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36397 id = get_identifier ("omp declare target link");
36398 else
36399 id = get_identifier ("omp declare target");
36401 DECL_ATTRIBUTES (decl)
36402 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36403 if (global_bindings_p ())
36405 symtab_node *node = symtab_node::get (decl);
36406 if (node != NULL)
36408 node->offloadable = 1;
36409 if (ENABLE_OFFLOADING)
36411 g->have_offload = true;
36412 if (is_a <varpool_node *> (node))
36413 vec_safe_push (offload_vars, decl);
36420 if (error || global_bindings_p ())
36421 return NULL_TREE;
36423 stmt = make_node (OACC_DECLARE);
36424 TREE_TYPE (stmt) = void_type_node;
36425 OACC_DECLARE_CLAUSES (stmt) = clauses;
36426 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36428 add_stmt (stmt);
36430 return NULL_TREE;
36433 /* OpenACC 2.0:
36434 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36438 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36440 LOC is the location of the #pragma token.
36443 #define OACC_ENTER_DATA_CLAUSE_MASK \
36444 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36449 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36450 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36452 #define OACC_EXIT_DATA_CLAUSE_MASK \
36453 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36454 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36455 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36459 static tree
36460 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36461 bool enter)
36463 location_t loc = pragma_tok->location;
36464 tree stmt, clauses;
36465 const char *p = "";
36467 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36468 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36470 if (strcmp (p, "data") != 0)
36472 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36473 enter ? "enter" : "exit");
36474 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36475 return NULL_TREE;
36478 cp_lexer_consume_token (parser->lexer);
36480 if (enter)
36481 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36482 "#pragma acc enter data", pragma_tok);
36483 else
36484 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36485 "#pragma acc exit data", pragma_tok);
36487 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36489 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36490 enter ? "enter" : "exit");
36491 return NULL_TREE;
36494 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36495 TREE_TYPE (stmt) = void_type_node;
36496 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36497 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36498 add_stmt (stmt);
36499 return stmt;
36502 /* OpenACC 2.0:
36503 # pragma acc loop oacc-loop-clause[optseq] new-line
36504 structured-block */
36506 #define OACC_LOOP_CLAUSE_MASK \
36507 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36518 static tree
36519 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36520 omp_clause_mask mask, tree *cclauses, bool *if_p)
36522 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36524 strcat (p_name, " loop");
36525 mask |= OACC_LOOP_CLAUSE_MASK;
36527 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36528 cclauses == NULL);
36529 if (cclauses)
36531 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36532 if (*cclauses)
36533 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36534 if (clauses)
36535 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36538 tree block = begin_omp_structured_block ();
36539 int save = cp_parser_begin_omp_structured_block (parser);
36540 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36541 cp_parser_end_omp_structured_block (parser, save);
36542 add_stmt (finish_omp_structured_block (block));
36544 return stmt;
36547 /* OpenACC 2.0:
36548 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36549 structured-block
36553 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36554 structured-block
36557 #define OACC_KERNELS_CLAUSE_MASK \
36558 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36567 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36568 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36569 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36570 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36571 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36572 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36573 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36574 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36576 #define OACC_PARALLEL_CLAUSE_MASK \
36577 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36578 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36579 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36580 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36581 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36582 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36583 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36584 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36585 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36586 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36587 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36588 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36589 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36594 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36595 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36596 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36598 static tree
36599 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36600 char *p_name, bool *if_p)
36602 omp_clause_mask mask;
36603 enum tree_code code;
36604 switch (cp_parser_pragma_kind (pragma_tok))
36606 case PRAGMA_OACC_KERNELS:
36607 strcat (p_name, " kernels");
36608 mask = OACC_KERNELS_CLAUSE_MASK;
36609 code = OACC_KERNELS;
36610 break;
36611 case PRAGMA_OACC_PARALLEL:
36612 strcat (p_name, " parallel");
36613 mask = OACC_PARALLEL_CLAUSE_MASK;
36614 code = OACC_PARALLEL;
36615 break;
36616 default:
36617 gcc_unreachable ();
36620 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36622 const char *p
36623 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36624 if (strcmp (p, "loop") == 0)
36626 cp_lexer_consume_token (parser->lexer);
36627 tree block = begin_omp_parallel ();
36628 tree clauses;
36629 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36630 if_p);
36631 return finish_omp_construct (code, block, clauses);
36635 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36637 tree block = begin_omp_parallel ();
36638 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36639 cp_parser_statement (parser, NULL_TREE, false, if_p);
36640 cp_parser_end_omp_structured_block (parser, save);
36641 return finish_omp_construct (code, block, clauses);
36644 /* OpenACC 2.0:
36645 # pragma acc update oacc-update-clause[optseq] new-line
36648 #define OACC_UPDATE_CLAUSE_MASK \
36649 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36650 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36651 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36652 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36653 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36654 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36656 static tree
36657 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36659 tree stmt, clauses;
36661 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36662 "#pragma acc update", pragma_tok);
36664 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36666 error_at (pragma_tok->location,
36667 "%<#pragma acc update%> must contain at least one "
36668 "%<device%> or %<host%> or %<self%> clause");
36669 return NULL_TREE;
36672 stmt = make_node (OACC_UPDATE);
36673 TREE_TYPE (stmt) = void_type_node;
36674 OACC_UPDATE_CLAUSES (stmt) = clauses;
36675 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36676 add_stmt (stmt);
36677 return stmt;
36680 /* OpenACC 2.0:
36681 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36683 LOC is the location of the #pragma token.
36686 #define OACC_WAIT_CLAUSE_MASK \
36687 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36689 static tree
36690 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36692 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36693 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36695 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36696 list = cp_parser_oacc_wait_list (parser, loc, list);
36698 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36699 "#pragma acc wait", pragma_tok);
36701 stmt = c_finish_oacc_wait (loc, list, clauses);
36702 stmt = finish_expr_stmt (stmt);
36704 return stmt;
36707 /* OpenMP 4.0:
36708 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36710 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36711 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36712 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36713 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36714 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36715 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36716 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36718 static void
36719 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36720 enum pragma_context context)
36722 bool first_p = parser->omp_declare_simd == NULL;
36723 cp_omp_declare_simd_data data;
36724 if (first_p)
36726 data.error_seen = false;
36727 data.fndecl_seen = false;
36728 data.tokens = vNULL;
36729 data.clauses = NULL_TREE;
36730 /* It is safe to take the address of a local variable; it will only be
36731 used while this scope is live. */
36732 parser->omp_declare_simd = &data;
36735 /* Store away all pragma tokens. */
36736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36737 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36738 cp_lexer_consume_token (parser->lexer);
36739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36740 parser->omp_declare_simd->error_seen = true;
36741 cp_parser_require_pragma_eol (parser, pragma_tok);
36742 struct cp_token_cache *cp
36743 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36744 parser->omp_declare_simd->tokens.safe_push (cp);
36746 if (first_p)
36748 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36749 cp_parser_pragma (parser, context, NULL);
36750 switch (context)
36752 case pragma_external:
36753 cp_parser_declaration (parser);
36754 break;
36755 case pragma_member:
36756 cp_parser_member_declaration (parser);
36757 break;
36758 case pragma_objc_icode:
36759 cp_parser_block_declaration (parser, /*statement_p=*/false);
36760 break;
36761 default:
36762 cp_parser_declaration_statement (parser);
36763 break;
36765 if (parser->omp_declare_simd
36766 && !parser->omp_declare_simd->error_seen
36767 && !parser->omp_declare_simd->fndecl_seen)
36768 error_at (pragma_tok->location,
36769 "%<#pragma omp declare simd%> not immediately followed by "
36770 "function declaration or definition");
36771 data.tokens.release ();
36772 parser->omp_declare_simd = NULL;
36776 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36777 This function is modelled similar to the late parsing of omp declare
36778 simd. */
36780 static tree
36781 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36783 struct cp_token_cache *ce;
36784 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36785 int ii = 0;
36787 if (parser->omp_declare_simd != NULL
36788 || lookup_attribute ("simd", attrs))
36790 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36791 "used in the same function marked as a Cilk Plus SIMD-enabled "
36792 "function");
36793 parser->cilk_simd_fn_info->tokens.release ();
36794 XDELETE (parser->cilk_simd_fn_info);
36795 parser->cilk_simd_fn_info = NULL;
36796 return attrs;
36798 if (!info->error_seen && info->fndecl_seen)
36800 error ("vector attribute not immediately followed by a single function"
36801 " declaration or definition");
36802 info->error_seen = true;
36804 if (info->error_seen)
36805 return attrs;
36807 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36809 tree c, cl;
36811 cp_parser_push_lexer_for_tokens (parser, ce);
36812 parser->lexer->in_pragma = true;
36813 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36814 "SIMD-enabled functions attribute",
36815 NULL);
36816 cp_parser_pop_lexer (parser);
36817 if (cl)
36818 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36820 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36821 TREE_CHAIN (c) = attrs;
36822 attrs = c;
36824 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36825 TREE_CHAIN (c) = attrs;
36826 if (processing_template_decl)
36827 ATTR_IS_DEPENDENT (c) = 1;
36828 attrs = c;
36830 info->fndecl_seen = true;
36831 parser->cilk_simd_fn_info->tokens.release ();
36832 XDELETE (parser->cilk_simd_fn_info);
36833 parser->cilk_simd_fn_info = NULL;
36834 return attrs;
36837 /* Finalize #pragma omp declare simd clauses after direct declarator has
36838 been parsed, and put that into "omp declare simd" attribute. */
36840 static tree
36841 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36843 struct cp_token_cache *ce;
36844 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36845 int i;
36847 if (!data->error_seen && data->fndecl_seen)
36849 error ("%<#pragma omp declare simd%> not immediately followed by "
36850 "a single function declaration or definition");
36851 data->error_seen = true;
36853 if (data->error_seen)
36854 return attrs;
36856 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36858 tree c, cl;
36860 cp_parser_push_lexer_for_tokens (parser, ce);
36861 parser->lexer->in_pragma = true;
36862 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36863 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36864 cp_lexer_consume_token (parser->lexer);
36865 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36866 "#pragma omp declare simd", pragma_tok);
36867 cp_parser_pop_lexer (parser);
36868 if (cl)
36869 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36870 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36871 TREE_CHAIN (c) = attrs;
36872 if (processing_template_decl)
36873 ATTR_IS_DEPENDENT (c) = 1;
36874 attrs = c;
36877 data->fndecl_seen = true;
36878 return attrs;
36882 /* OpenMP 4.0:
36883 # pragma omp declare target new-line
36884 declarations and definitions
36885 # pragma omp end declare target new-line
36887 OpenMP 4.5:
36888 # pragma omp declare target ( extended-list ) new-line
36890 # pragma omp declare target declare-target-clauses[seq] new-line */
36892 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36893 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36894 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36896 static void
36897 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36899 tree clauses = NULL_TREE;
36900 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36901 clauses
36902 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36903 "#pragma omp declare target", pragma_tok);
36904 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36906 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36907 clauses);
36908 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36909 cp_parser_require_pragma_eol (parser, pragma_tok);
36911 else
36913 cp_parser_require_pragma_eol (parser, pragma_tok);
36914 scope_chain->omp_declare_target_attribute++;
36915 return;
36917 if (scope_chain->omp_declare_target_attribute)
36918 error_at (pragma_tok->location,
36919 "%<#pragma omp declare target%> with clauses in between "
36920 "%<#pragma omp declare target%> without clauses and "
36921 "%<#pragma omp end declare target%>");
36922 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36924 tree t = OMP_CLAUSE_DECL (c), id;
36925 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36926 tree at2 = lookup_attribute ("omp declare target link",
36927 DECL_ATTRIBUTES (t));
36928 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36930 id = get_identifier ("omp declare target link");
36931 std::swap (at1, at2);
36933 else
36934 id = get_identifier ("omp declare target");
36935 if (at2)
36937 error_at (OMP_CLAUSE_LOCATION (c),
36938 "%qD specified both in declare target %<link%> and %<to%>"
36939 " clauses", t);
36940 continue;
36942 if (!at1)
36944 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36945 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36946 continue;
36948 symtab_node *node = symtab_node::get (t);
36949 if (node != NULL)
36951 node->offloadable = 1;
36952 if (ENABLE_OFFLOADING)
36954 g->have_offload = true;
36955 if (is_a <varpool_node *> (node))
36956 vec_safe_push (offload_vars, t);
36963 static void
36964 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
36966 const char *p = "";
36967 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36969 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36970 p = IDENTIFIER_POINTER (id);
36972 if (strcmp (p, "declare") == 0)
36974 cp_lexer_consume_token (parser->lexer);
36975 p = "";
36976 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36978 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36979 p = IDENTIFIER_POINTER (id);
36981 if (strcmp (p, "target") == 0)
36982 cp_lexer_consume_token (parser->lexer);
36983 else
36985 cp_parser_error (parser, "expected %<target%>");
36986 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36987 return;
36990 else
36992 cp_parser_error (parser, "expected %<declare%>");
36993 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36994 return;
36996 cp_parser_require_pragma_eol (parser, pragma_tok);
36997 if (!scope_chain->omp_declare_target_attribute)
36998 error_at (pragma_tok->location,
36999 "%<#pragma omp end declare target%> without corresponding "
37000 "%<#pragma omp declare target%>");
37001 else
37002 scope_chain->omp_declare_target_attribute--;
37005 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37006 expression and optional initializer clause of
37007 #pragma omp declare reduction. We store the expression(s) as
37008 either 3, 6 or 7 special statements inside of the artificial function's
37009 body. The first two statements are DECL_EXPRs for the artificial
37010 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37011 expression that uses those variables.
37012 If there was any INITIALIZER clause, this is followed by further statements,
37013 the fourth and fifth statements are DECL_EXPRs for the artificial
37014 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37015 constructor variant (first token after open paren is not omp_priv),
37016 then the sixth statement is a statement with the function call expression
37017 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37018 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37019 to initialize the OMP_PRIV artificial variable and there is seventh
37020 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37022 static bool
37023 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37025 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37026 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37027 type = TREE_TYPE (type);
37028 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37029 DECL_ARTIFICIAL (omp_out) = 1;
37030 pushdecl (omp_out);
37031 add_decl_expr (omp_out);
37032 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37033 DECL_ARTIFICIAL (omp_in) = 1;
37034 pushdecl (omp_in);
37035 add_decl_expr (omp_in);
37036 tree combiner;
37037 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37039 keep_next_level (true);
37040 tree block = begin_omp_structured_block ();
37041 combiner = cp_parser_expression (parser);
37042 finish_expr_stmt (combiner);
37043 block = finish_omp_structured_block (block);
37044 add_stmt (block);
37046 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37047 return false;
37049 const char *p = "";
37050 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37052 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37053 p = IDENTIFIER_POINTER (id);
37056 if (strcmp (p, "initializer") == 0)
37058 cp_lexer_consume_token (parser->lexer);
37059 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37060 return false;
37062 p = "";
37063 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37065 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37066 p = IDENTIFIER_POINTER (id);
37069 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37070 DECL_ARTIFICIAL (omp_priv) = 1;
37071 pushdecl (omp_priv);
37072 add_decl_expr (omp_priv);
37073 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37074 DECL_ARTIFICIAL (omp_orig) = 1;
37075 pushdecl (omp_orig);
37076 add_decl_expr (omp_orig);
37078 keep_next_level (true);
37079 block = begin_omp_structured_block ();
37081 bool ctor = false;
37082 if (strcmp (p, "omp_priv") == 0)
37084 bool is_direct_init, is_non_constant_init;
37085 ctor = true;
37086 cp_lexer_consume_token (parser->lexer);
37087 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37088 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37089 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37090 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37091 == CPP_CLOSE_PAREN
37092 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37093 == CPP_CLOSE_PAREN))
37095 finish_omp_structured_block (block);
37096 error ("invalid initializer clause");
37097 return false;
37099 initializer = cp_parser_initializer (parser, &is_direct_init,
37100 &is_non_constant_init);
37101 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37102 NULL_TREE, LOOKUP_ONLYCONVERTING);
37104 else
37106 cp_parser_parse_tentatively (parser);
37107 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37108 /*check_dependency_p=*/true,
37109 /*template_p=*/NULL,
37110 /*declarator_p=*/false,
37111 /*optional_p=*/false);
37112 vec<tree, va_gc> *args;
37113 if (fn_name == error_mark_node
37114 || cp_parser_error_occurred (parser)
37115 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37116 || ((args = cp_parser_parenthesized_expression_list
37117 (parser, non_attr, /*cast_p=*/false,
37118 /*allow_expansion_p=*/true,
37119 /*non_constant_p=*/NULL)),
37120 cp_parser_error_occurred (parser)))
37122 finish_omp_structured_block (block);
37123 cp_parser_abort_tentative_parse (parser);
37124 cp_parser_error (parser, "expected id-expression (arguments)");
37125 return false;
37127 unsigned int i;
37128 tree arg;
37129 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37130 if (arg == omp_priv
37131 || (TREE_CODE (arg) == ADDR_EXPR
37132 && TREE_OPERAND (arg, 0) == omp_priv))
37133 break;
37134 cp_parser_abort_tentative_parse (parser);
37135 if (arg == NULL_TREE)
37136 error ("one of the initializer call arguments should be %<omp_priv%>"
37137 " or %<&omp_priv%>");
37138 initializer = cp_parser_postfix_expression (parser, false, false, false,
37139 false, NULL);
37140 finish_expr_stmt (initializer);
37143 block = finish_omp_structured_block (block);
37144 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37145 add_stmt (block);
37147 if (ctor)
37148 add_decl_expr (omp_orig);
37150 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37151 return false;
37154 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37155 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37157 return true;
37160 /* OpenMP 4.0
37161 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37162 initializer-clause[opt] new-line
37164 initializer-clause:
37165 initializer (omp_priv initializer)
37166 initializer (function-name (argument-list)) */
37168 static void
37169 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37170 enum pragma_context)
37172 auto_vec<tree> types;
37173 enum tree_code reduc_code = ERROR_MARK;
37174 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37175 unsigned int i;
37176 cp_token *first_token;
37177 cp_token_cache *cp;
37178 int errs;
37179 void *p;
37181 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37182 p = obstack_alloc (&declarator_obstack, 0);
37184 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37185 goto fail;
37187 switch (cp_lexer_peek_token (parser->lexer)->type)
37189 case CPP_PLUS:
37190 reduc_code = PLUS_EXPR;
37191 break;
37192 case CPP_MULT:
37193 reduc_code = MULT_EXPR;
37194 break;
37195 case CPP_MINUS:
37196 reduc_code = MINUS_EXPR;
37197 break;
37198 case CPP_AND:
37199 reduc_code = BIT_AND_EXPR;
37200 break;
37201 case CPP_XOR:
37202 reduc_code = BIT_XOR_EXPR;
37203 break;
37204 case CPP_OR:
37205 reduc_code = BIT_IOR_EXPR;
37206 break;
37207 case CPP_AND_AND:
37208 reduc_code = TRUTH_ANDIF_EXPR;
37209 break;
37210 case CPP_OR_OR:
37211 reduc_code = TRUTH_ORIF_EXPR;
37212 break;
37213 case CPP_NAME:
37214 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37215 break;
37216 default:
37217 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37218 "%<|%>, %<&&%>, %<||%> or identifier");
37219 goto fail;
37222 if (reduc_code != ERROR_MARK)
37223 cp_lexer_consume_token (parser->lexer);
37225 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37226 if (reduc_id == error_mark_node)
37227 goto fail;
37229 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37230 goto fail;
37232 /* Types may not be defined in declare reduction type list. */
37233 const char *saved_message;
37234 saved_message = parser->type_definition_forbidden_message;
37235 parser->type_definition_forbidden_message
37236 = G_("types may not be defined in declare reduction type list");
37237 bool saved_colon_corrects_to_scope_p;
37238 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37239 parser->colon_corrects_to_scope_p = false;
37240 bool saved_colon_doesnt_start_class_def_p;
37241 saved_colon_doesnt_start_class_def_p
37242 = parser->colon_doesnt_start_class_def_p;
37243 parser->colon_doesnt_start_class_def_p = true;
37245 while (true)
37247 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37248 type = cp_parser_type_id (parser);
37249 if (type == error_mark_node)
37251 else if (ARITHMETIC_TYPE_P (type)
37252 && (orig_reduc_id == NULL_TREE
37253 || (TREE_CODE (type) != COMPLEX_TYPE
37254 && (id_equal (orig_reduc_id, "min")
37255 || id_equal (orig_reduc_id, "max")))))
37256 error_at (loc, "predeclared arithmetic type %qT in "
37257 "%<#pragma omp declare reduction%>", type);
37258 else if (TREE_CODE (type) == FUNCTION_TYPE
37259 || TREE_CODE (type) == METHOD_TYPE
37260 || TREE_CODE (type) == ARRAY_TYPE)
37261 error_at (loc, "function or array type %qT in "
37262 "%<#pragma omp declare reduction%>", type);
37263 else if (TREE_CODE (type) == REFERENCE_TYPE)
37264 error_at (loc, "reference type %qT in "
37265 "%<#pragma omp declare reduction%>", type);
37266 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37267 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37268 "%<#pragma omp declare reduction%>", type);
37269 else
37270 types.safe_push (type);
37272 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37273 cp_lexer_consume_token (parser->lexer);
37274 else
37275 break;
37278 /* Restore the saved message. */
37279 parser->type_definition_forbidden_message = saved_message;
37280 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37281 parser->colon_doesnt_start_class_def_p
37282 = saved_colon_doesnt_start_class_def_p;
37284 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37285 || types.is_empty ())
37287 fail:
37288 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37289 goto done;
37292 first_token = cp_lexer_peek_token (parser->lexer);
37293 cp = NULL;
37294 errs = errorcount;
37295 FOR_EACH_VEC_ELT (types, i, type)
37297 tree fntype
37298 = build_function_type_list (void_type_node,
37299 cp_build_reference_type (type, false),
37300 NULL_TREE);
37301 tree this_reduc_id = reduc_id;
37302 if (!dependent_type_p (type))
37303 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37304 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37305 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37306 DECL_ARTIFICIAL (fndecl) = 1;
37307 DECL_EXTERNAL (fndecl) = 1;
37308 DECL_DECLARED_INLINE_P (fndecl) = 1;
37309 DECL_IGNORED_P (fndecl) = 1;
37310 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37311 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37312 DECL_ATTRIBUTES (fndecl)
37313 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37314 DECL_ATTRIBUTES (fndecl));
37315 if (processing_template_decl)
37316 fndecl = push_template_decl (fndecl);
37317 bool block_scope = false;
37318 tree block = NULL_TREE;
37319 if (current_function_decl)
37321 block_scope = true;
37322 DECL_CONTEXT (fndecl) = global_namespace;
37323 if (!processing_template_decl)
37324 pushdecl (fndecl);
37326 else if (current_class_type)
37328 if (cp == NULL)
37330 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37331 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37332 cp_lexer_consume_token (parser->lexer);
37333 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37334 goto fail;
37335 cp = cp_token_cache_new (first_token,
37336 cp_lexer_peek_nth_token (parser->lexer,
37337 2));
37339 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37340 finish_member_declaration (fndecl);
37341 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37342 DECL_PENDING_INLINE_P (fndecl) = 1;
37343 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37344 continue;
37346 else
37348 DECL_CONTEXT (fndecl) = current_namespace;
37349 pushdecl (fndecl);
37351 if (!block_scope)
37352 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37353 else
37354 block = begin_omp_structured_block ();
37355 if (cp)
37357 cp_parser_push_lexer_for_tokens (parser, cp);
37358 parser->lexer->in_pragma = true;
37360 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37362 if (!block_scope)
37363 finish_function (0);
37364 else
37365 DECL_CONTEXT (fndecl) = current_function_decl;
37366 if (cp)
37367 cp_parser_pop_lexer (parser);
37368 goto fail;
37370 if (cp)
37371 cp_parser_pop_lexer (parser);
37372 if (!block_scope)
37373 finish_function (0);
37374 else
37376 DECL_CONTEXT (fndecl) = current_function_decl;
37377 block = finish_omp_structured_block (block);
37378 if (TREE_CODE (block) == BIND_EXPR)
37379 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37380 else if (TREE_CODE (block) == STATEMENT_LIST)
37381 DECL_SAVED_TREE (fndecl) = block;
37382 if (processing_template_decl)
37383 add_decl_expr (fndecl);
37385 cp_check_omp_declare_reduction (fndecl);
37386 if (cp == NULL && types.length () > 1)
37387 cp = cp_token_cache_new (first_token,
37388 cp_lexer_peek_nth_token (parser->lexer, 2));
37389 if (errs != errorcount)
37390 break;
37393 cp_parser_require_pragma_eol (parser, pragma_tok);
37395 done:
37396 /* Free any declarators allocated. */
37397 obstack_free (&declarator_obstack, p);
37400 /* OpenMP 4.0
37401 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37402 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37403 initializer-clause[opt] new-line
37404 #pragma omp declare target new-line */
37406 static void
37407 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37408 enum pragma_context context)
37410 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37412 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37413 const char *p = IDENTIFIER_POINTER (id);
37415 if (strcmp (p, "simd") == 0)
37417 cp_lexer_consume_token (parser->lexer);
37418 cp_parser_omp_declare_simd (parser, pragma_tok,
37419 context);
37420 return;
37422 cp_ensure_no_omp_declare_simd (parser);
37423 if (strcmp (p, "reduction") == 0)
37425 cp_lexer_consume_token (parser->lexer);
37426 cp_parser_omp_declare_reduction (parser, pragma_tok,
37427 context);
37428 return;
37430 if (!flag_openmp) /* flag_openmp_simd */
37432 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37433 return;
37435 if (strcmp (p, "target") == 0)
37437 cp_lexer_consume_token (parser->lexer);
37438 cp_parser_omp_declare_target (parser, pragma_tok);
37439 return;
37442 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37443 "or %<target%>");
37444 cp_parser_require_pragma_eol (parser, pragma_tok);
37447 /* OpenMP 4.5:
37448 #pragma omp taskloop taskloop-clause[optseq] new-line
37449 for-loop
37451 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37452 for-loop */
37454 #define OMP_TASKLOOP_CLAUSE_MASK \
37455 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37456 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37457 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37458 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37459 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37460 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37461 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37462 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37463 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37464 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37465 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37466 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37467 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37468 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37470 static tree
37471 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37472 char *p_name, omp_clause_mask mask, tree *cclauses,
37473 bool *if_p)
37475 tree clauses, sb, ret;
37476 unsigned int save;
37477 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37479 strcat (p_name, " taskloop");
37480 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37482 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37484 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37485 const char *p = IDENTIFIER_POINTER (id);
37487 if (strcmp (p, "simd") == 0)
37489 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37490 if (cclauses == NULL)
37491 cclauses = cclauses_buf;
37493 cp_lexer_consume_token (parser->lexer);
37494 if (!flag_openmp) /* flag_openmp_simd */
37495 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37496 cclauses, if_p);
37497 sb = begin_omp_structured_block ();
37498 save = cp_parser_begin_omp_structured_block (parser);
37499 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37500 cclauses, if_p);
37501 cp_parser_end_omp_structured_block (parser, save);
37502 tree body = finish_omp_structured_block (sb);
37503 if (ret == NULL)
37504 return ret;
37505 ret = make_node (OMP_TASKLOOP);
37506 TREE_TYPE (ret) = void_type_node;
37507 OMP_FOR_BODY (ret) = body;
37508 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37509 SET_EXPR_LOCATION (ret, loc);
37510 add_stmt (ret);
37511 return ret;
37514 if (!flag_openmp) /* flag_openmp_simd */
37516 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37517 return NULL_TREE;
37520 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37521 cclauses == NULL);
37522 if (cclauses)
37524 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37525 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37528 sb = begin_omp_structured_block ();
37529 save = cp_parser_begin_omp_structured_block (parser);
37531 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37532 if_p);
37534 cp_parser_end_omp_structured_block (parser, save);
37535 add_stmt (finish_omp_structured_block (sb));
37537 return ret;
37541 /* OpenACC 2.0:
37542 # pragma acc routine oacc-routine-clause[optseq] new-line
37543 function-definition
37545 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37548 #define OACC_ROUTINE_CLAUSE_MASK \
37549 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37550 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37551 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37552 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37555 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37556 component, which must resolve to a declared namespace-scope
37557 function. The clauses are either processed directly (for a named
37558 function), or defered until the immediatley following declaration
37559 is parsed. */
37561 static void
37562 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37563 enum pragma_context context)
37565 gcc_checking_assert (context == pragma_external);
37566 /* The checking for "another pragma following this one" in the "no optional
37567 '( name )'" case makes sure that we dont re-enter. */
37568 gcc_checking_assert (parser->oacc_routine == NULL);
37570 cp_oacc_routine_data data;
37571 data.error_seen = false;
37572 data.fndecl_seen = false;
37573 data.tokens = vNULL;
37574 data.clauses = NULL_TREE;
37575 data.loc = pragma_tok->location;
37576 /* It is safe to take the address of a local variable; it will only be
37577 used while this scope is live. */
37578 parser->oacc_routine = &data;
37580 /* Look for optional '( name )'. */
37581 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37583 cp_lexer_consume_token (parser->lexer); /* '(' */
37585 /* We parse the name as an id-expression. If it resolves to
37586 anything other than a non-overloaded function at namespace
37587 scope, it's an error. */
37588 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37589 tree name = cp_parser_id_expression (parser,
37590 /*template_keyword_p=*/false,
37591 /*check_dependency_p=*/false,
37592 /*template_p=*/NULL,
37593 /*declarator_p=*/false,
37594 /*optional_p=*/false);
37595 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37596 if (name != error_mark_node && decl == error_mark_node)
37597 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37599 if (decl == error_mark_node
37600 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37602 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37603 parser->oacc_routine = NULL;
37604 return;
37607 data.clauses
37608 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37609 "#pragma acc routine",
37610 cp_lexer_peek_token (parser->lexer));
37612 if (decl && is_overloaded_fn (decl)
37613 && (TREE_CODE (decl) != FUNCTION_DECL
37614 || DECL_FUNCTION_TEMPLATE_P (decl)))
37616 error_at (name_loc,
37617 "%<#pragma acc routine%> names a set of overloads");
37618 parser->oacc_routine = NULL;
37619 return;
37622 /* Perhaps we should use the same rule as declarations in different
37623 namespaces? */
37624 if (!DECL_NAMESPACE_SCOPE_P (decl))
37626 error_at (name_loc,
37627 "%qD does not refer to a namespace scope function", decl);
37628 parser->oacc_routine = NULL;
37629 return;
37632 if (TREE_CODE (decl) != FUNCTION_DECL)
37634 error_at (name_loc, "%qD does not refer to a function", decl);
37635 parser->oacc_routine = NULL;
37636 return;
37639 cp_finalize_oacc_routine (parser, decl, false);
37640 parser->oacc_routine = NULL;
37642 else /* No optional '( name )'. */
37644 /* Store away all pragma tokens. */
37645 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37646 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37647 cp_lexer_consume_token (parser->lexer);
37648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37649 parser->oacc_routine->error_seen = true;
37650 cp_parser_require_pragma_eol (parser, pragma_tok);
37651 struct cp_token_cache *cp
37652 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37653 parser->oacc_routine->tokens.safe_push (cp);
37655 /* Emit a helpful diagnostic if there's another pragma following this
37656 one. */
37657 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37659 cp_ensure_no_oacc_routine (parser);
37660 data.tokens.release ();
37661 /* ..., and then just keep going. */
37662 return;
37665 /* We only have to consider the pragma_external case here. */
37666 cp_parser_declaration (parser);
37667 if (parser->oacc_routine
37668 && !parser->oacc_routine->fndecl_seen)
37669 cp_ensure_no_oacc_routine (parser);
37670 else
37671 parser->oacc_routine = NULL;
37672 data.tokens.release ();
37676 /* Finalize #pragma acc routine clauses after direct declarator has
37677 been parsed. */
37679 static tree
37680 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37682 struct cp_token_cache *ce;
37683 cp_oacc_routine_data *data = parser->oacc_routine;
37685 if (!data->error_seen && data->fndecl_seen)
37687 error_at (data->loc,
37688 "%<#pragma acc routine%> not immediately followed by "
37689 "a single function declaration or definition");
37690 data->error_seen = true;
37692 if (data->error_seen)
37693 return attrs;
37695 gcc_checking_assert (data->tokens.length () == 1);
37696 ce = data->tokens[0];
37698 cp_parser_push_lexer_for_tokens (parser, ce);
37699 parser->lexer->in_pragma = true;
37700 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37702 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37703 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37704 parser->oacc_routine->clauses
37705 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37706 "#pragma acc routine", pragma_tok);
37707 cp_parser_pop_lexer (parser);
37708 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37709 fndecl_seen. */
37711 return attrs;
37714 /* Apply any saved OpenACC routine clauses to a just-parsed
37715 declaration. */
37717 static void
37718 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37720 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37722 /* Keep going if we're in error reporting mode. */
37723 if (parser->oacc_routine->error_seen
37724 || fndecl == error_mark_node)
37725 return;
37727 if (parser->oacc_routine->fndecl_seen)
37729 error_at (parser->oacc_routine->loc,
37730 "%<#pragma acc routine%> not immediately followed by"
37731 " a single function declaration or definition");
37732 parser->oacc_routine = NULL;
37733 return;
37735 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37737 cp_ensure_no_oacc_routine (parser);
37738 return;
37741 if (oacc_get_fn_attrib (fndecl))
37743 error_at (parser->oacc_routine->loc,
37744 "%<#pragma acc routine%> already applied to %qD", fndecl);
37745 parser->oacc_routine = NULL;
37746 return;
37749 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37751 error_at (parser->oacc_routine->loc,
37752 TREE_USED (fndecl)
37753 ? G_("%<#pragma acc routine%> must be applied before use")
37754 : G_("%<#pragma acc routine%> must be applied before "
37755 "definition"));
37756 parser->oacc_routine = NULL;
37757 return;
37760 /* Process the routine's dimension clauses. */
37761 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37762 oacc_replace_fn_attrib (fndecl, dims);
37764 /* Add an "omp declare target" attribute. */
37765 DECL_ATTRIBUTES (fndecl)
37766 = tree_cons (get_identifier ("omp declare target"),
37767 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37769 /* Don't unset parser->oacc_routine here: we may still need it to
37770 diagnose wrong usage. But, remember that we've used this "#pragma acc
37771 routine". */
37772 parser->oacc_routine->fndecl_seen = true;
37776 /* Main entry point to OpenMP statement pragmas. */
37778 static void
37779 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37781 tree stmt;
37782 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37783 omp_clause_mask mask (0);
37785 switch (cp_parser_pragma_kind (pragma_tok))
37787 case PRAGMA_OACC_ATOMIC:
37788 cp_parser_omp_atomic (parser, pragma_tok);
37789 return;
37790 case PRAGMA_OACC_CACHE:
37791 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37792 break;
37793 case PRAGMA_OACC_DATA:
37794 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37795 break;
37796 case PRAGMA_OACC_ENTER_DATA:
37797 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37798 break;
37799 case PRAGMA_OACC_EXIT_DATA:
37800 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37801 break;
37802 case PRAGMA_OACC_HOST_DATA:
37803 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37804 break;
37805 case PRAGMA_OACC_KERNELS:
37806 case PRAGMA_OACC_PARALLEL:
37807 strcpy (p_name, "#pragma acc");
37808 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37809 if_p);
37810 break;
37811 case PRAGMA_OACC_LOOP:
37812 strcpy (p_name, "#pragma acc");
37813 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37814 if_p);
37815 break;
37816 case PRAGMA_OACC_UPDATE:
37817 stmt = cp_parser_oacc_update (parser, pragma_tok);
37818 break;
37819 case PRAGMA_OACC_WAIT:
37820 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37821 break;
37822 case PRAGMA_OMP_ATOMIC:
37823 cp_parser_omp_atomic (parser, pragma_tok);
37824 return;
37825 case PRAGMA_OMP_CRITICAL:
37826 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37827 break;
37828 case PRAGMA_OMP_DISTRIBUTE:
37829 strcpy (p_name, "#pragma omp");
37830 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37831 if_p);
37832 break;
37833 case PRAGMA_OMP_FOR:
37834 strcpy (p_name, "#pragma omp");
37835 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37836 if_p);
37837 break;
37838 case PRAGMA_OMP_MASTER:
37839 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37840 break;
37841 case PRAGMA_OMP_PARALLEL:
37842 strcpy (p_name, "#pragma omp");
37843 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37844 if_p);
37845 break;
37846 case PRAGMA_OMP_SECTIONS:
37847 strcpy (p_name, "#pragma omp");
37848 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37849 break;
37850 case PRAGMA_OMP_SIMD:
37851 strcpy (p_name, "#pragma omp");
37852 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37853 if_p);
37854 break;
37855 case PRAGMA_OMP_SINGLE:
37856 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37857 break;
37858 case PRAGMA_OMP_TASK:
37859 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37860 break;
37861 case PRAGMA_OMP_TASKGROUP:
37862 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37863 break;
37864 case PRAGMA_OMP_TASKLOOP:
37865 strcpy (p_name, "#pragma omp");
37866 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37867 if_p);
37868 break;
37869 case PRAGMA_OMP_TEAMS:
37870 strcpy (p_name, "#pragma omp");
37871 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37872 if_p);
37873 break;
37874 default:
37875 gcc_unreachable ();
37878 protected_set_expr_location (stmt, pragma_tok->location);
37881 /* Transactional Memory parsing routines. */
37883 /* Parse a transaction attribute.
37885 txn-attribute:
37886 attribute
37887 [ [ identifier ] ]
37889 We use this instead of cp_parser_attributes_opt for transactions to avoid
37890 the pedwarn in C++98 mode. */
37892 static tree
37893 cp_parser_txn_attribute_opt (cp_parser *parser)
37895 cp_token *token;
37896 tree attr_name, attr = NULL;
37898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37899 return cp_parser_attributes_opt (parser);
37901 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37902 return NULL_TREE;
37903 cp_lexer_consume_token (parser->lexer);
37904 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37905 goto error1;
37907 token = cp_lexer_peek_token (parser->lexer);
37908 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37910 token = cp_lexer_consume_token (parser->lexer);
37912 attr_name = (token->type == CPP_KEYWORD
37913 /* For keywords, use the canonical spelling,
37914 not the parsed identifier. */
37915 ? ridpointers[(int) token->keyword]
37916 : token->u.value);
37917 attr = build_tree_list (attr_name, NULL_TREE);
37919 else
37920 cp_parser_error (parser, "expected identifier");
37922 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37923 error1:
37924 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37925 return attr;
37928 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37930 transaction-statement:
37931 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37932 compound-statement
37933 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37936 static tree
37937 cp_parser_transaction (cp_parser *parser, cp_token *token)
37939 unsigned char old_in = parser->in_transaction;
37940 unsigned char this_in = 1, new_in;
37941 enum rid keyword = token->keyword;
37942 tree stmt, attrs, noex;
37944 cp_lexer_consume_token (parser->lexer);
37946 if (keyword == RID_TRANSACTION_RELAXED
37947 || keyword == RID_SYNCHRONIZED)
37948 this_in |= TM_STMT_ATTR_RELAXED;
37949 else
37951 attrs = cp_parser_txn_attribute_opt (parser);
37952 if (attrs)
37953 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
37956 /* Parse a noexcept specification. */
37957 if (keyword == RID_ATOMIC_NOEXCEPT)
37958 noex = boolean_true_node;
37959 else if (keyword == RID_ATOMIC_CANCEL)
37961 /* cancel-and-throw is unimplemented. */
37962 sorry ("atomic_cancel");
37963 noex = NULL_TREE;
37965 else
37966 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
37968 /* Keep track if we're in the lexical scope of an outer transaction. */
37969 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
37971 stmt = begin_transaction_stmt (token->location, NULL, this_in);
37973 parser->in_transaction = new_in;
37974 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
37975 parser->in_transaction = old_in;
37977 finish_transaction_stmt (stmt, NULL, this_in, noex);
37979 return stmt;
37982 /* Parse a __transaction_atomic or __transaction_relaxed expression.
37984 transaction-expression:
37985 __transaction_atomic txn-noexcept-spec[opt] ( expression )
37986 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
37989 static tree
37990 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
37992 unsigned char old_in = parser->in_transaction;
37993 unsigned char this_in = 1;
37994 cp_token *token;
37995 tree expr, noex;
37996 bool noex_expr;
37997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37999 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38000 || keyword == RID_TRANSACTION_RELAXED);
38002 if (!flag_tm)
38003 error_at (loc,
38004 keyword == RID_TRANSACTION_RELAXED
38005 ? G_("%<__transaction_relaxed%> without transactional memory "
38006 "support enabled")
38007 : G_("%<__transaction_atomic%> without transactional memory "
38008 "support enabled"));
38010 token = cp_parser_require_keyword (parser, keyword,
38011 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38012 : RT_TRANSACTION_RELAXED));
38013 gcc_assert (token != NULL);
38015 if (keyword == RID_TRANSACTION_RELAXED)
38016 this_in |= TM_STMT_ATTR_RELAXED;
38018 /* Set this early. This might mean that we allow transaction_cancel in
38019 an expression that we find out later actually has to be a constexpr.
38020 However, we expect that cxx_constant_value will be able to deal with
38021 this; also, if the noexcept has no constexpr, then what we parse next
38022 really is a transaction's body. */
38023 parser->in_transaction = this_in;
38025 /* Parse a noexcept specification. */
38026 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38027 true);
38029 if (!noex || !noex_expr
38030 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38032 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
38034 expr = cp_parser_expression (parser);
38035 expr = finish_parenthesized_expr (expr);
38037 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
38039 else
38041 /* The only expression that is available got parsed for the noexcept
38042 already. noexcept is true then. */
38043 expr = noex;
38044 noex = boolean_true_node;
38047 expr = build_transaction_expr (token->location, expr, this_in, noex);
38048 parser->in_transaction = old_in;
38050 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38051 return error_mark_node;
38053 return (flag_tm ? expr : error_mark_node);
38056 /* Parse a function-transaction-block.
38058 function-transaction-block:
38059 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38060 function-body
38061 __transaction_atomic txn-attribute[opt] function-try-block
38062 __transaction_relaxed ctor-initializer[opt] function-body
38063 __transaction_relaxed function-try-block
38066 static bool
38067 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38069 unsigned char old_in = parser->in_transaction;
38070 unsigned char new_in = 1;
38071 tree compound_stmt, stmt, attrs;
38072 bool ctor_initializer_p;
38073 cp_token *token;
38075 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38076 || keyword == RID_TRANSACTION_RELAXED);
38077 token = cp_parser_require_keyword (parser, keyword,
38078 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38079 : RT_TRANSACTION_RELAXED));
38080 gcc_assert (token != NULL);
38082 if (keyword == RID_TRANSACTION_RELAXED)
38083 new_in |= TM_STMT_ATTR_RELAXED;
38084 else
38086 attrs = cp_parser_txn_attribute_opt (parser);
38087 if (attrs)
38088 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38091 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38093 parser->in_transaction = new_in;
38095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38096 ctor_initializer_p = cp_parser_function_try_block (parser);
38097 else
38098 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38099 (parser, /*in_function_try_block=*/false);
38101 parser->in_transaction = old_in;
38103 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38105 return ctor_initializer_p;
38108 /* Parse a __transaction_cancel statement.
38110 cancel-statement:
38111 __transaction_cancel txn-attribute[opt] ;
38112 __transaction_cancel txn-attribute[opt] throw-expression ;
38114 ??? Cancel and throw is not yet implemented. */
38116 static tree
38117 cp_parser_transaction_cancel (cp_parser *parser)
38119 cp_token *token;
38120 bool is_outer = false;
38121 tree stmt, attrs;
38123 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38124 RT_TRANSACTION_CANCEL);
38125 gcc_assert (token != NULL);
38127 attrs = cp_parser_txn_attribute_opt (parser);
38128 if (attrs)
38129 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38131 /* ??? Parse cancel-and-throw here. */
38133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38135 if (!flag_tm)
38137 error_at (token->location, "%<__transaction_cancel%> without "
38138 "transactional memory support enabled");
38139 return error_mark_node;
38141 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38143 error_at (token->location, "%<__transaction_cancel%> within a "
38144 "%<__transaction_relaxed%>");
38145 return error_mark_node;
38147 else if (is_outer)
38149 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38150 && !is_tm_may_cancel_outer (current_function_decl))
38152 error_at (token->location, "outer %<__transaction_cancel%> not "
38153 "within outer %<__transaction_atomic%>");
38154 error_at (token->location,
38155 " or a %<transaction_may_cancel_outer%> function");
38156 return error_mark_node;
38159 else if (parser->in_transaction == 0)
38161 error_at (token->location, "%<__transaction_cancel%> not within "
38162 "%<__transaction_atomic%>");
38163 return error_mark_node;
38166 stmt = build_tm_abort_call (token->location, is_outer);
38167 add_stmt (stmt);
38169 return stmt;
38172 /* The parser. */
38174 static GTY (()) cp_parser *the_parser;
38177 /* Special handling for the first token or line in the file. The first
38178 thing in the file might be #pragma GCC pch_preprocess, which loads a
38179 PCH file, which is a GC collection point. So we need to handle this
38180 first pragma without benefit of an existing lexer structure.
38182 Always returns one token to the caller in *FIRST_TOKEN. This is
38183 either the true first token of the file, or the first token after
38184 the initial pragma. */
38186 static void
38187 cp_parser_initial_pragma (cp_token *first_token)
38189 tree name = NULL;
38191 cp_lexer_get_preprocessor_token (NULL, first_token);
38192 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38193 return;
38195 cp_lexer_get_preprocessor_token (NULL, first_token);
38196 if (first_token->type == CPP_STRING)
38198 name = first_token->u.value;
38200 cp_lexer_get_preprocessor_token (NULL, first_token);
38201 if (first_token->type != CPP_PRAGMA_EOL)
38202 error_at (first_token->location,
38203 "junk at end of %<#pragma GCC pch_preprocess%>");
38205 else
38206 error_at (first_token->location, "expected string literal");
38208 /* Skip to the end of the pragma. */
38209 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38210 cp_lexer_get_preprocessor_token (NULL, first_token);
38212 /* Now actually load the PCH file. */
38213 if (name)
38214 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38216 /* Read one more token to return to our caller. We have to do this
38217 after reading the PCH file in, since its pointers have to be
38218 live. */
38219 cp_lexer_get_preprocessor_token (NULL, first_token);
38222 /* Parses the grainsize pragma for the _Cilk_for statement.
38223 Syntax:
38224 #pragma cilk grainsize = <VALUE>. */
38226 static void
38227 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38229 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38231 tree exp = cp_parser_binary_expression (parser, false, false,
38232 PREC_NOT_OPERATOR, NULL);
38233 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38234 if (!exp || exp == error_mark_node)
38236 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38237 return;
38240 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38241 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38242 cp_parser_cilk_for (parser, exp, if_p);
38243 else
38244 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38245 "%<#pragma cilk grainsize%> is not followed by "
38246 "%<_Cilk_for%>");
38247 return;
38249 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38252 /* Normal parsing of a pragma token. Here we can (and must) use the
38253 regular lexer. */
38255 static bool
38256 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38258 cp_token *pragma_tok;
38259 unsigned int id;
38260 tree stmt;
38261 bool ret;
38263 pragma_tok = cp_lexer_consume_token (parser->lexer);
38264 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38265 parser->lexer->in_pragma = true;
38267 id = cp_parser_pragma_kind (pragma_tok);
38268 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38269 cp_ensure_no_omp_declare_simd (parser);
38270 switch (id)
38272 case PRAGMA_GCC_PCH_PREPROCESS:
38273 error_at (pragma_tok->location,
38274 "%<#pragma GCC pch_preprocess%> must be first");
38275 break;
38277 case PRAGMA_OMP_BARRIER:
38278 switch (context)
38280 case pragma_compound:
38281 cp_parser_omp_barrier (parser, pragma_tok);
38282 return false;
38283 case pragma_stmt:
38284 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38285 "used in compound statements", "omp barrier");
38286 break;
38287 default:
38288 goto bad_stmt;
38290 break;
38292 case PRAGMA_OMP_FLUSH:
38293 switch (context)
38295 case pragma_compound:
38296 cp_parser_omp_flush (parser, pragma_tok);
38297 return false;
38298 case pragma_stmt:
38299 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38300 "used in compound statements", "omp flush");
38301 break;
38302 default:
38303 goto bad_stmt;
38305 break;
38307 case PRAGMA_OMP_TASKWAIT:
38308 switch (context)
38310 case pragma_compound:
38311 cp_parser_omp_taskwait (parser, pragma_tok);
38312 return false;
38313 case pragma_stmt:
38314 error_at (pragma_tok->location,
38315 "%<#pragma %s%> may only be used in compound statements",
38316 "omp taskwait");
38317 break;
38318 default:
38319 goto bad_stmt;
38321 break;
38323 case PRAGMA_OMP_TASKYIELD:
38324 switch (context)
38326 case pragma_compound:
38327 cp_parser_omp_taskyield (parser, pragma_tok);
38328 return false;
38329 case pragma_stmt:
38330 error_at (pragma_tok->location,
38331 "%<#pragma %s%> may only be used in compound statements",
38332 "omp taskyield");
38333 break;
38334 default:
38335 goto bad_stmt;
38337 break;
38339 case PRAGMA_OMP_CANCEL:
38340 switch (context)
38342 case pragma_compound:
38343 cp_parser_omp_cancel (parser, pragma_tok);
38344 return false;
38345 case pragma_stmt:
38346 error_at (pragma_tok->location,
38347 "%<#pragma %s%> may only be used in compound statements",
38348 "omp cancel");
38349 break;
38350 default:
38351 goto bad_stmt;
38353 break;
38355 case PRAGMA_OMP_CANCELLATION_POINT:
38356 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38357 return false;
38359 case PRAGMA_OMP_THREADPRIVATE:
38360 cp_parser_omp_threadprivate (parser, pragma_tok);
38361 return false;
38363 case PRAGMA_OMP_DECLARE:
38364 cp_parser_omp_declare (parser, pragma_tok, context);
38365 return false;
38367 case PRAGMA_OACC_DECLARE:
38368 cp_parser_oacc_declare (parser, pragma_tok);
38369 return false;
38371 case PRAGMA_OACC_ENTER_DATA:
38372 if (context == pragma_stmt)
38374 error_at (pragma_tok->location,
38375 "%<#pragma %s%> may only be used in compound statements",
38376 "acc enter data");
38377 break;
38379 else if (context != pragma_compound)
38380 goto bad_stmt;
38381 cp_parser_omp_construct (parser, pragma_tok, if_p);
38382 return true;
38384 case PRAGMA_OACC_EXIT_DATA:
38385 if (context == pragma_stmt)
38387 error_at (pragma_tok->location,
38388 "%<#pragma %s%> may only be used in compound statements",
38389 "acc exit data");
38390 break;
38392 else if (context != pragma_compound)
38393 goto bad_stmt;
38394 cp_parser_omp_construct (parser, pragma_tok, if_p);
38395 return true;
38397 case PRAGMA_OACC_ROUTINE:
38398 if (context != pragma_external)
38400 error_at (pragma_tok->location,
38401 "%<#pragma acc routine%> must be at file scope");
38402 break;
38404 cp_parser_oacc_routine (parser, pragma_tok, context);
38405 return false;
38407 case PRAGMA_OACC_UPDATE:
38408 if (context == pragma_stmt)
38410 error_at (pragma_tok->location,
38411 "%<#pragma %s%> may only be used in compound statements",
38412 "acc update");
38413 break;
38415 else if (context != pragma_compound)
38416 goto bad_stmt;
38417 cp_parser_omp_construct (parser, pragma_tok, if_p);
38418 return true;
38420 case PRAGMA_OACC_WAIT:
38421 if (context == pragma_stmt)
38423 error_at (pragma_tok->location,
38424 "%<#pragma %s%> may only be used in compound statements",
38425 "acc wait");
38426 break;
38428 else if (context != pragma_compound)
38429 goto bad_stmt;
38430 cp_parser_omp_construct (parser, pragma_tok, if_p);
38431 return true;
38433 case PRAGMA_OACC_ATOMIC:
38434 case PRAGMA_OACC_CACHE:
38435 case PRAGMA_OACC_DATA:
38436 case PRAGMA_OACC_HOST_DATA:
38437 case PRAGMA_OACC_KERNELS:
38438 case PRAGMA_OACC_PARALLEL:
38439 case PRAGMA_OACC_LOOP:
38440 case PRAGMA_OMP_ATOMIC:
38441 case PRAGMA_OMP_CRITICAL:
38442 case PRAGMA_OMP_DISTRIBUTE:
38443 case PRAGMA_OMP_FOR:
38444 case PRAGMA_OMP_MASTER:
38445 case PRAGMA_OMP_PARALLEL:
38446 case PRAGMA_OMP_SECTIONS:
38447 case PRAGMA_OMP_SIMD:
38448 case PRAGMA_OMP_SINGLE:
38449 case PRAGMA_OMP_TASK:
38450 case PRAGMA_OMP_TASKGROUP:
38451 case PRAGMA_OMP_TASKLOOP:
38452 case PRAGMA_OMP_TEAMS:
38453 if (context != pragma_stmt && context != pragma_compound)
38454 goto bad_stmt;
38455 stmt = push_omp_privatization_clauses (false);
38456 cp_parser_omp_construct (parser, pragma_tok, if_p);
38457 pop_omp_privatization_clauses (stmt);
38458 return true;
38460 case PRAGMA_OMP_ORDERED:
38461 if (context != pragma_stmt && context != pragma_compound)
38462 goto bad_stmt;
38463 stmt = push_omp_privatization_clauses (false);
38464 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38465 pop_omp_privatization_clauses (stmt);
38466 return ret;
38468 case PRAGMA_OMP_TARGET:
38469 if (context != pragma_stmt && context != pragma_compound)
38470 goto bad_stmt;
38471 stmt = push_omp_privatization_clauses (false);
38472 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38473 pop_omp_privatization_clauses (stmt);
38474 return ret;
38476 case PRAGMA_OMP_END_DECLARE_TARGET:
38477 cp_parser_omp_end_declare_target (parser, pragma_tok);
38478 return false;
38480 case PRAGMA_OMP_SECTION:
38481 error_at (pragma_tok->location,
38482 "%<#pragma omp section%> may only be used in "
38483 "%<#pragma omp sections%> construct");
38484 break;
38486 case PRAGMA_IVDEP:
38488 if (context == pragma_external)
38490 error_at (pragma_tok->location,
38491 "%<#pragma GCC ivdep%> must be inside a function");
38492 break;
38494 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38495 cp_token *tok;
38496 tok = cp_lexer_peek_token (the_parser->lexer);
38497 if (tok->type != CPP_KEYWORD
38498 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38499 && tok->keyword != RID_DO))
38501 cp_parser_error (parser, "for, while or do statement expected");
38502 return false;
38504 cp_parser_iteration_statement (parser, if_p, true);
38505 return true;
38508 case PRAGMA_CILK_SIMD:
38509 if (context == pragma_external)
38511 error_at (pragma_tok->location,
38512 "%<#pragma simd%> must be inside a function");
38513 break;
38515 stmt = push_omp_privatization_clauses (false);
38516 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38517 pop_omp_privatization_clauses (stmt);
38518 return true;
38520 case PRAGMA_CILK_GRAINSIZE:
38521 if (context == pragma_external)
38523 error_at (pragma_tok->location,
38524 "%<#pragma cilk grainsize%> must be inside a function");
38525 break;
38528 /* Ignore the pragma if Cilk Plus is not enabled. */
38529 if (flag_cilkplus)
38531 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38532 return true;
38534 else
38536 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38537 "%<#pragma cilk grainsize%>");
38538 break;
38541 default:
38542 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38543 c_invoke_pragma_handler (id);
38544 break;
38546 bad_stmt:
38547 cp_parser_error (parser, "expected declaration specifiers");
38548 break;
38551 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38552 return false;
38555 /* The interface the pragma parsers have to the lexer. */
38557 enum cpp_ttype
38558 pragma_lex (tree *value, location_t *loc)
38560 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38561 enum cpp_ttype ret = tok->type;
38563 *value = tok->u.value;
38564 if (loc)
38565 *loc = tok->location;
38567 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38568 ret = CPP_EOF;
38569 else if (ret == CPP_STRING)
38570 *value = cp_parser_string_literal (the_parser, false, false);
38571 else
38573 if (ret == CPP_KEYWORD)
38574 ret = CPP_NAME;
38575 cp_lexer_consume_token (the_parser->lexer);
38578 return ret;
38582 /* External interface. */
38584 /* Parse one entire translation unit. */
38586 void
38587 c_parse_file (void)
38589 static bool already_called = false;
38591 if (already_called)
38592 fatal_error (input_location,
38593 "inter-module optimizations not implemented for C++");
38594 already_called = true;
38596 the_parser = cp_parser_new ();
38597 push_deferring_access_checks (flag_access_control
38598 ? dk_no_deferred : dk_no_check);
38599 cp_parser_translation_unit (the_parser);
38600 the_parser = NULL;
38603 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38604 vectorlength clause:
38605 Syntax:
38606 vectorlength ( constant-expression ) */
38608 static tree
38609 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38610 bool is_simd_fn)
38612 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38613 tree expr;
38614 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38615 safelen clause. Thus, vectorlength is represented as OMP 4.0
38616 safelen. For SIMD-enabled function it is represented by OMP 4.0
38617 simdlen. */
38618 if (!is_simd_fn)
38619 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38620 loc);
38621 else
38622 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38623 loc);
38625 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38626 return error_mark_node;
38628 expr = cp_parser_constant_expression (parser);
38629 expr = maybe_constant_value (expr);
38631 /* If expr == error_mark_node, then don't emit any errors nor
38632 create a clause. if any of the above functions returns
38633 error mark node then they would have emitted an error message. */
38634 if (expr == error_mark_node)
38636 else if (!TREE_TYPE (expr)
38637 || !TREE_CONSTANT (expr)
38638 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38639 error_at (loc, "vectorlength must be an integer constant");
38640 else if (TREE_CONSTANT (expr)
38641 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38642 error_at (loc, "vectorlength must be a power of 2");
38643 else
38645 tree c;
38646 if (!is_simd_fn)
38648 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38649 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38650 OMP_CLAUSE_CHAIN (c) = clauses;
38651 clauses = c;
38653 else
38655 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38656 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38657 OMP_CLAUSE_CHAIN (c) = clauses;
38658 clauses = c;
38662 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38663 return error_mark_node;
38664 return clauses;
38667 /* Handles the Cilk Plus #pragma simd linear clause.
38668 Syntax:
38669 linear ( simd-linear-variable-list )
38671 simd-linear-variable-list:
38672 simd-linear-variable
38673 simd-linear-variable-list , simd-linear-variable
38675 simd-linear-variable:
38676 id-expression
38677 id-expression : simd-linear-step
38679 simd-linear-step:
38680 conditional-expression */
38682 static tree
38683 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38685 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38687 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38688 return clauses;
38689 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38691 cp_parser_error (parser, "expected identifier");
38692 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38693 return error_mark_node;
38696 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38697 parser->colon_corrects_to_scope_p = false;
38698 while (1)
38700 cp_token *token = cp_lexer_peek_token (parser->lexer);
38701 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38703 cp_parser_error (parser, "expected variable-name");
38704 clauses = error_mark_node;
38705 break;
38708 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38709 false, false);
38710 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38711 token->location);
38712 if (decl == error_mark_node)
38714 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38715 token->location);
38716 clauses = error_mark_node;
38718 else
38720 tree e = NULL_TREE;
38721 tree step_size = integer_one_node;
38723 /* If present, parse the linear step. Otherwise, assume the default
38724 value of 1. */
38725 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38727 cp_lexer_consume_token (parser->lexer);
38729 e = cp_parser_assignment_expression (parser);
38730 e = maybe_constant_value (e);
38732 if (e == error_mark_node)
38734 /* If an error has occurred, then the whole pragma is
38735 considered ill-formed. Thus, no reason to keep
38736 parsing. */
38737 clauses = error_mark_node;
38738 break;
38740 else if (type_dependent_expression_p (e)
38741 || value_dependent_expression_p (e)
38742 || (TREE_TYPE (e)
38743 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38744 && (TREE_CONSTANT (e)
38745 || DECL_P (e))))
38746 step_size = e;
38747 else
38748 cp_parser_error (parser,
38749 "step size must be an integer constant "
38750 "expression or an integer variable");
38753 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38754 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38755 OMP_CLAUSE_DECL (l) = decl;
38756 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38757 OMP_CLAUSE_CHAIN (l) = clauses;
38758 clauses = l;
38760 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38761 cp_lexer_consume_token (parser->lexer);
38762 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38763 break;
38764 else
38766 error_at (cp_lexer_peek_token (parser->lexer)->location,
38767 "expected %<,%> or %<)%> after %qE", decl);
38768 clauses = error_mark_node;
38769 break;
38772 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38773 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38774 return clauses;
38777 /* Returns the name of the next clause. If the clause is not
38778 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38779 token is not consumed. Otherwise, the appropriate enum from the
38780 pragma_simd_clause is returned and the token is consumed. */
38782 static pragma_omp_clause
38783 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38785 pragma_omp_clause clause_type;
38786 cp_token *token = cp_lexer_peek_token (parser->lexer);
38788 if (token->keyword == RID_PRIVATE)
38789 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38790 else if (!token->u.value || token->type != CPP_NAME)
38791 return PRAGMA_CILK_CLAUSE_NONE;
38792 else if (id_equal (token->u.value, "vectorlength"))
38793 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38794 else if (id_equal (token->u.value, "linear"))
38795 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38796 else if (id_equal (token->u.value, "firstprivate"))
38797 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38798 else if (id_equal (token->u.value, "lastprivate"))
38799 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38800 else if (id_equal (token->u.value, "reduction"))
38801 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38802 else
38803 return PRAGMA_CILK_CLAUSE_NONE;
38805 cp_lexer_consume_token (parser->lexer);
38806 return clause_type;
38809 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38811 static tree
38812 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38814 tree clauses = NULL_TREE;
38816 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38817 && clauses != error_mark_node)
38819 pragma_omp_clause c_kind;
38820 c_kind = cp_parser_cilk_simd_clause_name (parser);
38821 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38822 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38823 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38824 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38825 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38826 /* Use the OpenMP 4.0 equivalent function. */
38827 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38828 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38829 /* Use the OpenMP 4.0 equivalent function. */
38830 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38831 clauses);
38832 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38833 /* Use the OMP 4.0 equivalent function. */
38834 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38835 clauses);
38836 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38837 /* Use the OMP 4.0 equivalent function. */
38838 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38839 else
38841 clauses = error_mark_node;
38842 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38843 break;
38847 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38849 if (clauses == error_mark_node)
38850 return error_mark_node;
38851 else
38852 return finish_omp_clauses (clauses, C_ORT_CILK);
38855 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38857 static void
38858 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38860 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38862 if (clauses == error_mark_node)
38863 return;
38865 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38867 error_at (cp_lexer_peek_token (parser->lexer)->location,
38868 "for statement expected");
38869 return;
38872 tree sb = begin_omp_structured_block ();
38873 int save = cp_parser_begin_omp_structured_block (parser);
38874 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38875 if (ret)
38876 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38877 cp_parser_end_omp_structured_block (parser, save);
38878 add_stmt (finish_omp_structured_block (sb));
38881 /* Main entry-point for parsing Cilk Plus _Cilk_for
38882 loops. The return value is error_mark_node
38883 when errors happen and CILK_FOR tree on success. */
38885 static tree
38886 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38888 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38889 gcc_unreachable ();
38891 tree sb = begin_omp_structured_block ();
38892 int save = cp_parser_begin_omp_structured_block (parser);
38894 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38895 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38896 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38897 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38899 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38900 if (ret)
38901 cpp_validate_cilk_plus_loop (ret);
38902 else
38903 ret = error_mark_node;
38905 cp_parser_end_omp_structured_block (parser, save);
38906 add_stmt (finish_omp_structured_block (sb));
38907 return ret;
38910 /* Create an identifier for a generic parameter type (a synthesized
38911 template parameter implied by `auto' or a concept identifier). */
38913 static GTY(()) int generic_parm_count;
38914 static tree
38915 make_generic_type_name ()
38917 char buf[32];
38918 sprintf (buf, "auto:%d", ++generic_parm_count);
38919 return get_identifier (buf);
38922 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38923 (creating a new template parameter list if necessary). Returns the newly
38924 created template type parm. */
38926 static tree
38927 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38929 gcc_assert (current_binding_level->kind == sk_function_parms);
38931 /* Before committing to modifying any scope, if we're in an
38932 implicit template scope, and we're trying to synthesize a
38933 constrained parameter, try to find a previous parameter with
38934 the same name. This is the same-type rule for abbreviated
38935 function templates.
38937 NOTE: We can generate implicit parameters when tentatively
38938 parsing a nested name specifier, only to reject that parse
38939 later. However, matching the same template-id as part of a
38940 direct-declarator should generate an identical template
38941 parameter, so this rule will merge them. */
38942 if (parser->implicit_template_scope && constr)
38944 tree t = parser->implicit_template_parms;
38945 while (t)
38947 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38949 tree d = TREE_VALUE (t);
38950 if (TREE_CODE (d) == PARM_DECL)
38951 /* Return the TEMPLATE_PARM_INDEX. */
38952 d = DECL_INITIAL (d);
38953 return d;
38955 t = TREE_CHAIN (t);
38959 /* We are either continuing a function template that already contains implicit
38960 template parameters, creating a new fully-implicit function template, or
38961 extending an existing explicit function template with implicit template
38962 parameters. */
38964 cp_binding_level *const entry_scope = current_binding_level;
38966 bool become_template = false;
38967 cp_binding_level *parent_scope = 0;
38969 if (parser->implicit_template_scope)
38971 gcc_assert (parser->implicit_template_parms);
38973 current_binding_level = parser->implicit_template_scope;
38975 else
38977 /* Roll back to the existing template parameter scope (in the case of
38978 extending an explicit function template) or introduce a new template
38979 parameter scope ahead of the function parameter scope (or class scope
38980 in the case of out-of-line member definitions). The function scope is
38981 added back after template parameter synthesis below. */
38983 cp_binding_level *scope = entry_scope;
38985 while (scope->kind == sk_function_parms)
38987 parent_scope = scope;
38988 scope = scope->level_chain;
38990 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
38992 /* If not defining a class, then any class scope is a scope level in
38993 an out-of-line member definition. In this case simply wind back
38994 beyond the first such scope to inject the template parameter list.
38995 Otherwise wind back to the class being defined. The latter can
38996 occur in class member friend declarations such as:
38998 class A {
38999 void foo (auto);
39001 class B {
39002 friend void A::foo (auto);
39005 The template parameter list synthesized for the friend declaration
39006 must be injected in the scope of 'B'. This can also occur in
39007 erroneous cases such as:
39009 struct A {
39010 struct B {
39011 void foo (auto);
39013 void B::foo (auto) {}
39016 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39017 but, nevertheless, the template parameter list synthesized for the
39018 declarator should be injected into the scope of 'A' as if the
39019 ill-formed template was specified explicitly. */
39021 while (scope->kind == sk_class && !scope->defining_class_p)
39023 parent_scope = scope;
39024 scope = scope->level_chain;
39028 current_binding_level = scope;
39030 if (scope->kind != sk_template_parms
39031 || !function_being_declared_is_template_p (parser))
39033 /* Introduce a new template parameter list for implicit template
39034 parameters. */
39036 become_template = true;
39038 parser->implicit_template_scope
39039 = begin_scope (sk_template_parms, NULL);
39041 ++processing_template_decl;
39043 parser->fully_implicit_function_template_p = true;
39044 ++parser->num_template_parameter_lists;
39046 else
39048 /* Synthesize implicit template parameters at the end of the explicit
39049 template parameter list. */
39051 gcc_assert (current_template_parms);
39053 parser->implicit_template_scope = scope;
39055 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39056 parser->implicit_template_parms
39057 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39061 /* Synthesize a new template parameter and track the current template
39062 parameter chain with implicit_template_parms. */
39064 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39065 tree synth_id = make_generic_type_name ();
39066 tree synth_tmpl_parm;
39067 bool non_type = false;
39069 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39070 synth_tmpl_parm
39071 = finish_template_type_parm (class_type_node, synth_id);
39072 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39073 synth_tmpl_parm
39074 = finish_constrained_template_template_parm (proto, synth_id);
39075 else
39077 synth_tmpl_parm = copy_decl (proto);
39078 DECL_NAME (synth_tmpl_parm) = synth_id;
39079 non_type = true;
39082 // Attach the constraint to the parm before processing.
39083 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39084 TREE_TYPE (node) = constr;
39085 tree new_parm
39086 = process_template_parm (parser->implicit_template_parms,
39087 input_location,
39088 node,
39089 /*non_type=*/non_type,
39090 /*param_pack=*/false);
39092 // Chain the new parameter to the list of implicit parameters.
39093 if (parser->implicit_template_parms)
39094 parser->implicit_template_parms
39095 = TREE_CHAIN (parser->implicit_template_parms);
39096 else
39097 parser->implicit_template_parms = new_parm;
39099 tree new_decl = get_local_decls ();
39100 if (non_type)
39101 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39102 new_decl = DECL_INITIAL (new_decl);
39104 /* If creating a fully implicit function template, start the new implicit
39105 template parameter list with this synthesized type, otherwise grow the
39106 current template parameter list. */
39108 if (become_template)
39110 parent_scope->level_chain = current_binding_level;
39112 tree new_parms = make_tree_vec (1);
39113 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39114 current_template_parms = tree_cons (size_int (processing_template_decl),
39115 new_parms, current_template_parms);
39117 else
39119 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39120 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39121 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39122 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39125 // If the new parameter was constrained, we need to add that to the
39126 // constraints in the template parameter list.
39127 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39129 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39130 reqs = conjoin_constraints (reqs, req);
39131 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39134 current_binding_level = entry_scope;
39136 return new_decl;
39139 /* Finish the declaration of a fully implicit function template. Such a
39140 template has no explicit template parameter list so has not been through the
39141 normal template head and tail processing. synthesize_implicit_template_parm
39142 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39143 provided if the declaration is a class member such that its template
39144 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39145 form is returned. Otherwise NULL_TREE is returned. */
39147 static tree
39148 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39150 gcc_assert (parser->fully_implicit_function_template_p);
39152 if (member_decl_opt && member_decl_opt != error_mark_node
39153 && DECL_VIRTUAL_P (member_decl_opt))
39155 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39156 "implicit templates may not be %<virtual%>");
39157 DECL_VIRTUAL_P (member_decl_opt) = false;
39160 if (member_decl_opt)
39161 member_decl_opt = finish_member_template_decl (member_decl_opt);
39162 end_template_decl ();
39164 parser->fully_implicit_function_template_p = false;
39165 --parser->num_template_parameter_lists;
39167 return member_decl_opt;
39170 #include "gt-cp-parser.h"