re PR c++/79790 ([C++17] ICE class template argument deduction failed)
[official-gcc.git] / gcc / cp / parser.c
blob88d0b2b250d649ed2e4d5e0cc81ca480f15de124
1 /* -*- C++ -*- Parser.
2 Copyright (C) 2000-2017 Free Software Foundation, Inc.
3 Written by Mark Mitchell <mark@codesourcery.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "cp-tree.h"
25 #include "c-family/c-common.h"
26 #include "timevar.h"
27 #include "stringpool.h"
28 #include "cgraph.h"
29 #include "print-tree.h"
30 #include "attribs.h"
31 #include "trans-mem.h"
32 #include "intl.h"
33 #include "decl.h"
34 #include "c-family/c-objc.h"
35 #include "plugin.h"
36 #include "tree-pretty-print.h"
37 #include "parser.h"
38 #include "gomp-constants.h"
39 #include "omp-general.h"
40 #include "omp-offload.h"
41 #include "c-family/c-indentation.h"
42 #include "context.h"
43 #include "cp-cilkplus.h"
44 #include "gcc-rich-location.h"
45 #include "tree-iterator.h"
48 /* The lexer. */
50 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
51 and c-lex.c) and the C++ parser. */
53 static cp_token eof_token =
55 CPP_EOF, RID_MAX, 0, false, false, false, 0, { NULL }
58 /* The various kinds of non integral constant we encounter. */
59 enum non_integral_constant {
60 NIC_NONE,
61 /* floating-point literal */
62 NIC_FLOAT,
63 /* %<this%> */
64 NIC_THIS,
65 /* %<__FUNCTION__%> */
66 NIC_FUNC_NAME,
67 /* %<__PRETTY_FUNCTION__%> */
68 NIC_PRETTY_FUNC,
69 /* %<__func__%> */
70 NIC_C99_FUNC,
71 /* "%<va_arg%> */
72 NIC_VA_ARG,
73 /* a cast */
74 NIC_CAST,
75 /* %<typeid%> operator */
76 NIC_TYPEID,
77 /* non-constant compound literals */
78 NIC_NCC,
79 /* a function call */
80 NIC_FUNC_CALL,
81 /* an increment */
82 NIC_INC,
83 /* an decrement */
84 NIC_DEC,
85 /* an array reference */
86 NIC_ARRAY_REF,
87 /* %<->%> */
88 NIC_ARROW,
89 /* %<.%> */
90 NIC_POINT,
91 /* the address of a label */
92 NIC_ADDR_LABEL,
93 /* %<*%> */
94 NIC_STAR,
95 /* %<&%> */
96 NIC_ADDR,
97 /* %<++%> */
98 NIC_PREINCREMENT,
99 /* %<--%> */
100 NIC_PREDECREMENT,
101 /* %<new%> */
102 NIC_NEW,
103 /* %<delete%> */
104 NIC_DEL,
105 /* calls to overloaded operators */
106 NIC_OVERLOADED,
107 /* an assignment */
108 NIC_ASSIGNMENT,
109 /* a comma operator */
110 NIC_COMMA,
111 /* a call to a constructor */
112 NIC_CONSTRUCTOR,
113 /* a transaction expression */
114 NIC_TRANSACTION
117 /* The various kinds of errors about name-lookup failing. */
118 enum name_lookup_error {
119 /* NULL */
120 NLE_NULL,
121 /* is not a type */
122 NLE_TYPE,
123 /* is not a class or namespace */
124 NLE_CXX98,
125 /* is not a class, namespace, or enumeration */
126 NLE_NOT_CXX98
129 /* The various kinds of required token */
130 enum required_token {
131 RT_NONE,
132 RT_SEMICOLON, /* ';' */
133 RT_OPEN_PAREN, /* '(' */
134 RT_CLOSE_BRACE, /* '}' */
135 RT_OPEN_BRACE, /* '{' */
136 RT_CLOSE_SQUARE, /* ']' */
137 RT_OPEN_SQUARE, /* '[' */
138 RT_COMMA, /* ',' */
139 RT_SCOPE, /* '::' */
140 RT_LESS, /* '<' */
141 RT_GREATER, /* '>' */
142 RT_EQ, /* '=' */
143 RT_ELLIPSIS, /* '...' */
144 RT_MULT, /* '*' */
145 RT_COMPL, /* '~' */
146 RT_COLON, /* ':' */
147 RT_COLON_SCOPE, /* ':' or '::' */
148 RT_CLOSE_PAREN, /* ')' */
149 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
150 RT_PRAGMA_EOL, /* end of line */
151 RT_NAME, /* identifier */
153 /* The type is CPP_KEYWORD */
154 RT_NEW, /* new */
155 RT_DELETE, /* delete */
156 RT_RETURN, /* return */
157 RT_WHILE, /* while */
158 RT_EXTERN, /* extern */
159 RT_STATIC_ASSERT, /* static_assert */
160 RT_DECLTYPE, /* decltype */
161 RT_OPERATOR, /* operator */
162 RT_CLASS, /* class */
163 RT_TEMPLATE, /* template */
164 RT_NAMESPACE, /* namespace */
165 RT_USING, /* using */
166 RT_ASM, /* asm */
167 RT_TRY, /* try */
168 RT_CATCH, /* catch */
169 RT_THROW, /* throw */
170 RT_LABEL, /* __label__ */
171 RT_AT_TRY, /* @try */
172 RT_AT_SYNCHRONIZED, /* @synchronized */
173 RT_AT_THROW, /* @throw */
175 RT_SELECT, /* selection-statement */
176 RT_ITERATION, /* iteration-statement */
177 RT_JUMP, /* jump-statement */
178 RT_CLASS_KEY, /* class-key */
179 RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
180 RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
181 RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
182 RT_TRANSACTION_CANCEL /* __transaction_cancel */
185 /* RAII wrapper for parser->in_type_id_in_expr_p, setting it on creation and
186 reverting it on destruction. */
188 class type_id_in_expr_sentinel
190 cp_parser *parser;
191 bool saved;
192 public:
193 type_id_in_expr_sentinel (cp_parser *parser, bool set = true)
194 : parser (parser),
195 saved (parser->in_type_id_in_expr_p)
196 { parser->in_type_id_in_expr_p = set; }
197 ~type_id_in_expr_sentinel ()
198 { parser->in_type_id_in_expr_p = saved; }
201 /* Prototypes. */
203 static cp_lexer *cp_lexer_new_main
204 (void);
205 static cp_lexer *cp_lexer_new_from_tokens
206 (cp_token_cache *tokens);
207 static void cp_lexer_destroy
208 (cp_lexer *);
209 static int cp_lexer_saving_tokens
210 (const cp_lexer *);
211 static cp_token *cp_lexer_token_at
212 (cp_lexer *, cp_token_position);
213 static void cp_lexer_get_preprocessor_token
214 (cp_lexer *, cp_token *);
215 static inline cp_token *cp_lexer_peek_token
216 (cp_lexer *);
217 static cp_token *cp_lexer_peek_nth_token
218 (cp_lexer *, size_t);
219 static inline bool cp_lexer_next_token_is
220 (cp_lexer *, enum cpp_ttype);
221 static bool cp_lexer_next_token_is_not
222 (cp_lexer *, enum cpp_ttype);
223 static bool cp_lexer_next_token_is_keyword
224 (cp_lexer *, enum rid);
225 static cp_token *cp_lexer_consume_token
226 (cp_lexer *);
227 static void cp_lexer_purge_token
228 (cp_lexer *);
229 static void cp_lexer_purge_tokens_after
230 (cp_lexer *, cp_token_position);
231 static void cp_lexer_save_tokens
232 (cp_lexer *);
233 static void cp_lexer_commit_tokens
234 (cp_lexer *);
235 static void cp_lexer_rollback_tokens
236 (cp_lexer *);
237 static void cp_lexer_print_token
238 (FILE *, cp_token *);
239 static inline bool cp_lexer_debugging_p
240 (cp_lexer *);
241 static void cp_lexer_start_debugging
242 (cp_lexer *) ATTRIBUTE_UNUSED;
243 static void cp_lexer_stop_debugging
244 (cp_lexer *) ATTRIBUTE_UNUSED;
246 static cp_token_cache *cp_token_cache_new
247 (cp_token *, cp_token *);
249 static void cp_parser_initial_pragma
250 (cp_token *);
252 static void cp_parser_cilk_simd
253 (cp_parser *, cp_token *, bool *);
254 static tree cp_parser_cilk_for
255 (cp_parser *, tree, bool *);
256 static bool cp_parser_omp_declare_reduction_exprs
257 (tree, cp_parser *);
258 static tree cp_parser_cilk_simd_vectorlength
259 (cp_parser *, tree, bool);
260 static void cp_finalize_oacc_routine
261 (cp_parser *, tree, bool);
263 /* Manifest constants. */
264 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
265 #define CP_SAVED_TOKEN_STACK 5
267 /* Variables. */
269 /* The stream to which debugging output should be written. */
270 static FILE *cp_lexer_debug_stream;
272 /* Nonzero if we are parsing an unevaluated operand: an operand to
273 sizeof, typeof, or alignof. */
274 int cp_unevaluated_operand;
276 /* Dump up to NUM tokens in BUFFER to FILE starting with token
277 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
278 first token in BUFFER. If NUM is 0, dump all the tokens. If
279 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
280 highlighted by surrounding it in [[ ]]. */
282 static void
283 cp_lexer_dump_tokens (FILE *file, vec<cp_token, va_gc> *buffer,
284 cp_token *start_token, unsigned num,
285 cp_token *curr_token)
287 unsigned i, nprinted;
288 cp_token *token;
289 bool do_print;
291 fprintf (file, "%u tokens\n", vec_safe_length (buffer));
293 if (buffer == NULL)
294 return;
296 if (num == 0)
297 num = buffer->length ();
299 if (start_token == NULL)
300 start_token = buffer->address ();
302 if (start_token > buffer->address ())
304 cp_lexer_print_token (file, &(*buffer)[0]);
305 fprintf (file, " ... ");
308 do_print = false;
309 nprinted = 0;
310 for (i = 0; buffer->iterate (i, &token) && nprinted < num; i++)
312 if (token == start_token)
313 do_print = true;
315 if (!do_print)
316 continue;
318 nprinted++;
319 if (token == curr_token)
320 fprintf (file, "[[");
322 cp_lexer_print_token (file, token);
324 if (token == curr_token)
325 fprintf (file, "]]");
327 switch (token->type)
329 case CPP_SEMICOLON:
330 case CPP_OPEN_BRACE:
331 case CPP_CLOSE_BRACE:
332 case CPP_EOF:
333 fputc ('\n', file);
334 break;
336 default:
337 fputc (' ', file);
341 if (i == num && i < buffer->length ())
343 fprintf (file, " ... ");
344 cp_lexer_print_token (file, &buffer->last ());
347 fprintf (file, "\n");
351 /* Dump all tokens in BUFFER to stderr. */
353 void
354 cp_lexer_debug_tokens (vec<cp_token, va_gc> *buffer)
356 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
359 DEBUG_FUNCTION void
360 debug (vec<cp_token, va_gc> &ref)
362 cp_lexer_dump_tokens (stderr, &ref, NULL, 0, NULL);
365 DEBUG_FUNCTION void
366 debug (vec<cp_token, va_gc> *ptr)
368 if (ptr)
369 debug (*ptr);
370 else
371 fprintf (stderr, "<nil>\n");
375 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
376 description for T. */
378 static void
379 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
381 if (t)
383 fprintf (file, "%s: ", desc);
384 print_node_brief (file, "", t, 0);
389 /* Dump parser context C to FILE. */
391 static void
392 cp_debug_print_context (FILE *file, cp_parser_context *c)
394 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
395 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
396 print_node_brief (file, "", c->object_type, 0);
397 fprintf (file, "}\n");
401 /* Print the stack of parsing contexts to FILE starting with FIRST. */
403 static void
404 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
406 unsigned i;
407 cp_parser_context *c;
409 fprintf (file, "Parsing context stack:\n");
410 for (i = 0, c = first; c; c = c->next, i++)
412 fprintf (file, "\t#%u: ", i);
413 cp_debug_print_context (file, c);
418 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
420 static void
421 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
423 if (flag)
424 fprintf (file, "%s: true\n", desc);
428 /* Print an unparsed function entry UF to FILE. */
430 static void
431 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
433 unsigned i;
434 cp_default_arg_entry *default_arg_fn;
435 tree fn;
437 fprintf (file, "\tFunctions with default args:\n");
438 for (i = 0;
439 vec_safe_iterate (uf->funs_with_default_args, i, &default_arg_fn);
440 i++)
442 fprintf (file, "\t\tClass type: ");
443 print_node_brief (file, "", default_arg_fn->class_type, 0);
444 fprintf (file, "\t\tDeclaration: ");
445 print_node_brief (file, "", default_arg_fn->decl, 0);
446 fprintf (file, "\n");
449 fprintf (file, "\n\tFunctions with definitions that require "
450 "post-processing\n\t\t");
451 for (i = 0; vec_safe_iterate (uf->funs_with_definitions, i, &fn); i++)
453 print_node_brief (file, "", fn, 0);
454 fprintf (file, " ");
456 fprintf (file, "\n");
458 fprintf (file, "\n\tNon-static data members with initializers that require "
459 "post-processing\n\t\t");
460 for (i = 0; vec_safe_iterate (uf->nsdmis, i, &fn); i++)
462 print_node_brief (file, "", fn, 0);
463 fprintf (file, " ");
465 fprintf (file, "\n");
469 /* Print the stack of unparsed member functions S to FILE. */
471 static void
472 cp_debug_print_unparsed_queues (FILE *file,
473 vec<cp_unparsed_functions_entry, va_gc> *s)
475 unsigned i;
476 cp_unparsed_functions_entry *uf;
478 fprintf (file, "Unparsed functions\n");
479 for (i = 0; vec_safe_iterate (s, i, &uf); i++)
481 fprintf (file, "#%u:\n", i);
482 cp_debug_print_unparsed_function (file, uf);
487 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
488 the given PARSER. If FILE is NULL, the output is printed on stderr. */
490 static void
491 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
493 cp_token *next_token, *first_token, *start_token;
495 if (file == NULL)
496 file = stderr;
498 next_token = parser->lexer->next_token;
499 first_token = parser->lexer->buffer->address ();
500 start_token = (next_token > first_token + window_size / 2)
501 ? next_token - window_size / 2
502 : first_token;
503 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
504 next_token);
508 /* Dump debugging information for the given PARSER. If FILE is NULL,
509 the output is printed on stderr. */
511 void
512 cp_debug_parser (FILE *file, cp_parser *parser)
514 const size_t window_size = 20;
515 cp_token *token;
516 expanded_location eloc;
518 if (file == NULL)
519 file = stderr;
521 fprintf (file, "Parser state\n\n");
522 fprintf (file, "Number of tokens: %u\n",
523 vec_safe_length (parser->lexer->buffer));
524 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
525 cp_debug_print_tree_if_set (file, "Object scope",
526 parser->object_scope);
527 cp_debug_print_tree_if_set (file, "Qualifying scope",
528 parser->qualifying_scope);
529 cp_debug_print_context_stack (file, parser->context);
530 cp_debug_print_flag (file, "Allow GNU extensions",
531 parser->allow_gnu_extensions_p);
532 cp_debug_print_flag (file, "'>' token is greater-than",
533 parser->greater_than_is_operator_p);
534 cp_debug_print_flag (file, "Default args allowed in current "
535 "parameter list", parser->default_arg_ok_p);
536 cp_debug_print_flag (file, "Parsing integral constant-expression",
537 parser->integral_constant_expression_p);
538 cp_debug_print_flag (file, "Allow non-constant expression in current "
539 "constant-expression",
540 parser->allow_non_integral_constant_expression_p);
541 cp_debug_print_flag (file, "Seen non-constant expression",
542 parser->non_integral_constant_expression_p);
543 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
544 "current context",
545 parser->local_variables_forbidden_p);
546 cp_debug_print_flag (file, "In unbraced linkage specification",
547 parser->in_unbraced_linkage_specification_p);
548 cp_debug_print_flag (file, "Parsing a declarator",
549 parser->in_declarator_p);
550 cp_debug_print_flag (file, "In template argument list",
551 parser->in_template_argument_list_p);
552 cp_debug_print_flag (file, "Parsing an iteration statement",
553 parser->in_statement & IN_ITERATION_STMT);
554 cp_debug_print_flag (file, "Parsing a switch statement",
555 parser->in_statement & IN_SWITCH_STMT);
556 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
557 parser->in_statement & IN_OMP_BLOCK);
558 cp_debug_print_flag (file, "Parsing a Cilk Plus for loop",
559 parser->in_statement & IN_CILK_SIMD_FOR);
560 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
561 parser->in_statement & IN_OMP_FOR);
562 cp_debug_print_flag (file, "Parsing an if statement",
563 parser->in_statement & IN_IF_STMT);
564 cp_debug_print_flag (file, "Parsing a type-id in an expression "
565 "context", parser->in_type_id_in_expr_p);
566 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
567 parser->implicit_extern_c);
568 cp_debug_print_flag (file, "String expressions should be translated "
569 "to execution character set",
570 parser->translate_strings_p);
571 cp_debug_print_flag (file, "Parsing function body outside of a "
572 "local class", parser->in_function_body);
573 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
574 parser->colon_corrects_to_scope_p);
575 cp_debug_print_flag (file, "Colon doesn't start a class definition",
576 parser->colon_doesnt_start_class_def_p);
577 if (parser->type_definition_forbidden_message)
578 fprintf (file, "Error message for forbidden type definitions: %s\n",
579 parser->type_definition_forbidden_message);
580 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
581 fprintf (file, "Number of class definitions in progress: %u\n",
582 parser->num_classes_being_defined);
583 fprintf (file, "Number of template parameter lists for the current "
584 "declaration: %u\n", parser->num_template_parameter_lists);
585 cp_debug_parser_tokens (file, parser, window_size);
586 token = parser->lexer->next_token;
587 fprintf (file, "Next token to parse:\n");
588 fprintf (file, "\tToken: ");
589 cp_lexer_print_token (file, token);
590 eloc = expand_location (token->location);
591 fprintf (file, "\n\tFile: %s\n", eloc.file);
592 fprintf (file, "\tLine: %d\n", eloc.line);
593 fprintf (file, "\tColumn: %d\n", eloc.column);
596 DEBUG_FUNCTION void
597 debug (cp_parser &ref)
599 cp_debug_parser (stderr, &ref);
602 DEBUG_FUNCTION void
603 debug (cp_parser *ptr)
605 if (ptr)
606 debug (*ptr);
607 else
608 fprintf (stderr, "<nil>\n");
611 /* Allocate memory for a new lexer object and return it. */
613 static cp_lexer *
614 cp_lexer_alloc (void)
616 cp_lexer *lexer;
618 c_common_no_more_pch ();
620 /* Allocate the memory. */
621 lexer = ggc_cleared_alloc<cp_lexer> ();
623 /* Initially we are not debugging. */
624 lexer->debugging_p = false;
626 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
628 /* Create the buffer. */
629 vec_alloc (lexer->buffer, CP_LEXER_BUFFER_SIZE);
631 return lexer;
635 /* Create a new main C++ lexer, the lexer that gets tokens from the
636 preprocessor. */
638 static cp_lexer *
639 cp_lexer_new_main (void)
641 cp_lexer *lexer;
642 cp_token token;
644 /* It's possible that parsing the first pragma will load a PCH file,
645 which is a GC collection point. So we have to do that before
646 allocating any memory. */
647 cp_parser_initial_pragma (&token);
649 lexer = cp_lexer_alloc ();
651 /* Put the first token in the buffer. */
652 lexer->buffer->quick_push (token);
654 /* Get the remaining tokens from the preprocessor. */
655 while (token.type != CPP_EOF)
657 cp_lexer_get_preprocessor_token (lexer, &token);
658 vec_safe_push (lexer->buffer, token);
661 lexer->last_token = lexer->buffer->address ()
662 + lexer->buffer->length ()
663 - 1;
664 lexer->next_token = lexer->buffer->length ()
665 ? lexer->buffer->address ()
666 : &eof_token;
668 /* Subsequent preprocessor diagnostics should use compiler
669 diagnostic functions to get the compiler source location. */
670 done_lexing = true;
672 gcc_assert (!lexer->next_token->purged_p);
673 return lexer;
676 /* Create a new lexer whose token stream is primed with the tokens in
677 CACHE. When these tokens are exhausted, no new tokens will be read. */
679 static cp_lexer *
680 cp_lexer_new_from_tokens (cp_token_cache *cache)
682 cp_token *first = cache->first;
683 cp_token *last = cache->last;
684 cp_lexer *lexer = ggc_cleared_alloc<cp_lexer> ();
686 /* We do not own the buffer. */
687 lexer->buffer = NULL;
688 lexer->next_token = first == last ? &eof_token : first;
689 lexer->last_token = last;
691 lexer->saved_tokens.create (CP_SAVED_TOKEN_STACK);
693 /* Initially we are not debugging. */
694 lexer->debugging_p = false;
696 gcc_assert (!lexer->next_token->purged_p);
697 return lexer;
700 /* Frees all resources associated with LEXER. */
702 static void
703 cp_lexer_destroy (cp_lexer *lexer)
705 vec_free (lexer->buffer);
706 lexer->saved_tokens.release ();
707 ggc_free (lexer);
710 /* This needs to be set to TRUE before the lexer-debugging infrastructure can
711 be used. The point of this flag is to help the compiler to fold away calls
712 to cp_lexer_debugging_p within this source file at compile time, when the
713 lexer is not being debugged. */
715 #define LEXER_DEBUGGING_ENABLED_P false
717 /* Returns nonzero if debugging information should be output. */
719 static inline bool
720 cp_lexer_debugging_p (cp_lexer *lexer)
722 if (!LEXER_DEBUGGING_ENABLED_P)
723 return false;
725 return lexer->debugging_p;
729 static inline cp_token_position
730 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
732 gcc_assert (!previous_p || lexer->next_token != &eof_token);
734 return lexer->next_token - previous_p;
737 static inline cp_token *
738 cp_lexer_token_at (cp_lexer * /*lexer*/, cp_token_position pos)
740 return pos;
743 static inline void
744 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
746 lexer->next_token = cp_lexer_token_at (lexer, pos);
749 static inline cp_token_position
750 cp_lexer_previous_token_position (cp_lexer *lexer)
752 if (lexer->next_token == &eof_token)
753 return lexer->last_token - 1;
754 else
755 return cp_lexer_token_position (lexer, true);
758 static inline cp_token *
759 cp_lexer_previous_token (cp_lexer *lexer)
761 cp_token_position tp = cp_lexer_previous_token_position (lexer);
763 /* Skip past purged tokens. */
764 while (tp->purged_p)
766 gcc_assert (tp != vec_safe_address (lexer->buffer));
767 tp--;
770 return cp_lexer_token_at (lexer, tp);
773 /* nonzero if we are presently saving tokens. */
775 static inline int
776 cp_lexer_saving_tokens (const cp_lexer* lexer)
778 return lexer->saved_tokens.length () != 0;
781 /* Store the next token from the preprocessor in *TOKEN. Return true
782 if we reach EOF. If LEXER is NULL, assume we are handling an
783 initial #pragma pch_preprocess, and thus want the lexer to return
784 processed strings. */
786 static void
787 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
789 static int is_extern_c = 0;
791 /* Get a new token from the preprocessor. */
792 token->type
793 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
794 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
795 token->keyword = RID_MAX;
796 token->purged_p = false;
797 token->error_reported = false;
799 /* On some systems, some header files are surrounded by an
800 implicit extern "C" block. Set a flag in the token if it
801 comes from such a header. */
802 is_extern_c += pending_lang_change;
803 pending_lang_change = 0;
804 token->implicit_extern_c = is_extern_c > 0;
806 /* Check to see if this token is a keyword. */
807 if (token->type == CPP_NAME)
809 if (IDENTIFIER_KEYWORD_P (token->u.value))
811 /* Mark this token as a keyword. */
812 token->type = CPP_KEYWORD;
813 /* Record which keyword. */
814 token->keyword = C_RID_CODE (token->u.value);
816 else
818 if (warn_cxx11_compat
819 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX11
820 && C_RID_CODE (token->u.value) <= RID_LAST_CXX11)
822 /* Warn about the C++0x keyword (but still treat it as
823 an identifier). */
824 warning (OPT_Wc__11_compat,
825 "identifier %qE is a keyword in C++11",
826 token->u.value);
828 /* Clear out the C_RID_CODE so we don't warn about this
829 particular identifier-turned-keyword again. */
830 C_SET_RID_CODE (token->u.value, RID_MAX);
833 token->keyword = RID_MAX;
836 else if (token->type == CPP_AT_NAME)
838 /* This only happens in Objective-C++; it must be a keyword. */
839 token->type = CPP_KEYWORD;
840 switch (C_RID_CODE (token->u.value))
842 /* Replace 'class' with '@class', 'private' with '@private',
843 etc. This prevents confusion with the C++ keyword
844 'class', and makes the tokens consistent with other
845 Objective-C 'AT' keywords. For example '@class' is
846 reported as RID_AT_CLASS which is consistent with
847 '@synchronized', which is reported as
848 RID_AT_SYNCHRONIZED.
850 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
851 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
852 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
853 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
854 case RID_THROW: token->keyword = RID_AT_THROW; break;
855 case RID_TRY: token->keyword = RID_AT_TRY; break;
856 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
857 case RID_SYNCHRONIZED: token->keyword = RID_AT_SYNCHRONIZED; break;
858 default: token->keyword = C_RID_CODE (token->u.value);
863 /* Update the globals input_location and the input file stack from TOKEN. */
864 static inline void
865 cp_lexer_set_source_position_from_token (cp_token *token)
867 if (token->type != CPP_EOF)
869 input_location = token->location;
873 /* Update the globals input_location and the input file stack from LEXER. */
874 static inline void
875 cp_lexer_set_source_position (cp_lexer *lexer)
877 cp_token *token = cp_lexer_peek_token (lexer);
878 cp_lexer_set_source_position_from_token (token);
881 /* Return a pointer to the next token in the token stream, but do not
882 consume it. */
884 static inline cp_token *
885 cp_lexer_peek_token (cp_lexer *lexer)
887 if (cp_lexer_debugging_p (lexer))
889 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
890 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
891 putc ('\n', cp_lexer_debug_stream);
893 return lexer->next_token;
896 /* Return true if the next token has the indicated TYPE. */
898 static inline bool
899 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
901 return cp_lexer_peek_token (lexer)->type == type;
904 /* Return true if the next token does not have the indicated TYPE. */
906 static inline bool
907 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
909 return !cp_lexer_next_token_is (lexer, type);
912 /* Return true if the next token is the indicated KEYWORD. */
914 static inline bool
915 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
917 return cp_lexer_peek_token (lexer)->keyword == keyword;
920 static inline bool
921 cp_lexer_nth_token_is (cp_lexer* lexer, size_t n, enum cpp_ttype type)
923 return cp_lexer_peek_nth_token (lexer, n)->type == type;
926 static inline bool
927 cp_lexer_nth_token_is_keyword (cp_lexer* lexer, size_t n, enum rid keyword)
929 return cp_lexer_peek_nth_token (lexer, n)->keyword == keyword;
932 /* Return true if the next token is not the indicated KEYWORD. */
934 static inline bool
935 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
937 return cp_lexer_peek_token (lexer)->keyword != keyword;
940 /* Return true if KEYWORD can start a decl-specifier. */
942 bool
943 cp_keyword_starts_decl_specifier_p (enum rid keyword)
945 switch (keyword)
947 /* auto specifier: storage-class-specifier in C++,
948 simple-type-specifier in C++0x. */
949 case RID_AUTO:
950 /* Storage classes. */
951 case RID_REGISTER:
952 case RID_STATIC:
953 case RID_EXTERN:
954 case RID_MUTABLE:
955 case RID_THREAD:
956 /* Elaborated type specifiers. */
957 case RID_ENUM:
958 case RID_CLASS:
959 case RID_STRUCT:
960 case RID_UNION:
961 case RID_TYPENAME:
962 /* Simple type specifiers. */
963 case RID_CHAR:
964 case RID_CHAR16:
965 case RID_CHAR32:
966 case RID_WCHAR:
967 case RID_BOOL:
968 case RID_SHORT:
969 case RID_INT:
970 case RID_LONG:
971 case RID_SIGNED:
972 case RID_UNSIGNED:
973 case RID_FLOAT:
974 case RID_DOUBLE:
975 case RID_VOID:
976 /* GNU extensions. */
977 case RID_ATTRIBUTE:
978 case RID_TYPEOF:
979 /* C++0x extensions. */
980 case RID_DECLTYPE:
981 case RID_UNDERLYING_TYPE:
982 case RID_CONSTEXPR:
983 return true;
985 default:
986 if (keyword >= RID_FIRST_INT_N
987 && keyword < RID_FIRST_INT_N + NUM_INT_N_ENTS
988 && int_n_enabled_p[keyword - RID_FIRST_INT_N])
989 return true;
990 return false;
994 /* Return true if the next token is a keyword for a decl-specifier. */
996 static bool
997 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
999 cp_token *token;
1001 token = cp_lexer_peek_token (lexer);
1002 return cp_keyword_starts_decl_specifier_p (token->keyword);
1005 /* Returns TRUE iff the token T begins a decltype type. */
1007 static bool
1008 token_is_decltype (cp_token *t)
1010 return (t->keyword == RID_DECLTYPE
1011 || t->type == CPP_DECLTYPE);
1014 /* Returns TRUE iff the next token begins a decltype type. */
1016 static bool
1017 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
1019 cp_token *t = cp_lexer_peek_token (lexer);
1020 return token_is_decltype (t);
1023 /* Called when processing a token with tree_check_value; perform or defer the
1024 associated checks and return the value. */
1026 static tree
1027 saved_checks_value (struct tree_check *check_value)
1029 /* Perform any access checks that were deferred. */
1030 vec<deferred_access_check, va_gc> *checks;
1031 deferred_access_check *chk;
1032 checks = check_value->checks;
1033 if (checks)
1035 int i;
1036 FOR_EACH_VEC_SAFE_ELT (checks, i, chk)
1037 perform_or_defer_access_check (chk->binfo,
1038 chk->decl,
1039 chk->diag_decl, tf_warning_or_error);
1041 /* Return the stored value. */
1042 return check_value->value;
1045 /* Return a pointer to the Nth token in the token stream. If N is 1,
1046 then this is precisely equivalent to cp_lexer_peek_token (except
1047 that it is not inline). One would like to disallow that case, but
1048 there is one case (cp_parser_nth_token_starts_template_id) where
1049 the caller passes a variable for N and it might be 1. */
1051 static cp_token *
1052 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
1054 cp_token *token;
1056 /* N is 1-based, not zero-based. */
1057 gcc_assert (n > 0);
1059 if (cp_lexer_debugging_p (lexer))
1060 fprintf (cp_lexer_debug_stream,
1061 "cp_lexer: peeking ahead %ld at token: ", (long)n);
1063 --n;
1064 token = lexer->next_token;
1065 gcc_assert (!n || token != &eof_token);
1066 while (n != 0)
1068 ++token;
1069 if (token == lexer->last_token)
1071 token = &eof_token;
1072 break;
1075 if (!token->purged_p)
1076 --n;
1079 if (cp_lexer_debugging_p (lexer))
1081 cp_lexer_print_token (cp_lexer_debug_stream, token);
1082 putc ('\n', cp_lexer_debug_stream);
1085 return token;
1088 /* Return the next token, and advance the lexer's next_token pointer
1089 to point to the next non-purged token. */
1091 static cp_token *
1092 cp_lexer_consume_token (cp_lexer* lexer)
1094 cp_token *token = lexer->next_token;
1096 gcc_assert (token != &eof_token);
1097 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
1101 lexer->next_token++;
1102 if (lexer->next_token == lexer->last_token)
1104 lexer->next_token = &eof_token;
1105 break;
1109 while (lexer->next_token->purged_p);
1111 cp_lexer_set_source_position_from_token (token);
1113 /* Provide debugging output. */
1114 if (cp_lexer_debugging_p (lexer))
1116 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
1117 cp_lexer_print_token (cp_lexer_debug_stream, token);
1118 putc ('\n', cp_lexer_debug_stream);
1121 return token;
1124 /* Permanently remove the next token from the token stream, and
1125 advance the next_token pointer to refer to the next non-purged
1126 token. */
1128 static void
1129 cp_lexer_purge_token (cp_lexer *lexer)
1131 cp_token *tok = lexer->next_token;
1133 gcc_assert (tok != &eof_token);
1134 tok->purged_p = true;
1135 tok->location = UNKNOWN_LOCATION;
1136 tok->u.value = NULL_TREE;
1137 tok->keyword = RID_MAX;
1141 tok++;
1142 if (tok == lexer->last_token)
1144 tok = &eof_token;
1145 break;
1148 while (tok->purged_p);
1149 lexer->next_token = tok;
1152 /* Permanently remove all tokens after TOK, up to, but not
1153 including, the token that will be returned next by
1154 cp_lexer_peek_token. */
1156 static void
1157 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1159 cp_token *peek = lexer->next_token;
1161 if (peek == &eof_token)
1162 peek = lexer->last_token;
1164 gcc_assert (tok < peek);
1166 for ( tok += 1; tok != peek; tok += 1)
1168 tok->purged_p = true;
1169 tok->location = UNKNOWN_LOCATION;
1170 tok->u.value = NULL_TREE;
1171 tok->keyword = RID_MAX;
1175 /* Begin saving tokens. All tokens consumed after this point will be
1176 preserved. */
1178 static void
1179 cp_lexer_save_tokens (cp_lexer* lexer)
1181 /* Provide debugging output. */
1182 if (cp_lexer_debugging_p (lexer))
1183 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1185 lexer->saved_tokens.safe_push (lexer->next_token);
1188 /* Commit to the portion of the token stream most recently saved. */
1190 static void
1191 cp_lexer_commit_tokens (cp_lexer* lexer)
1193 /* Provide debugging output. */
1194 if (cp_lexer_debugging_p (lexer))
1195 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1197 lexer->saved_tokens.pop ();
1200 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1201 to the token stream. Stop saving tokens. */
1203 static void
1204 cp_lexer_rollback_tokens (cp_lexer* lexer)
1206 /* Provide debugging output. */
1207 if (cp_lexer_debugging_p (lexer))
1208 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1210 lexer->next_token = lexer->saved_tokens.pop ();
1213 /* RAII wrapper around the above functions, with sanity checking. Creating
1214 a variable saves tokens, which are committed when the variable is
1215 destroyed unless they are explicitly rolled back by calling the rollback
1216 member function. */
1218 struct saved_token_sentinel
1220 cp_lexer *lexer;
1221 unsigned len;
1222 bool commit;
1223 saved_token_sentinel(cp_lexer *lexer): lexer(lexer), commit(true)
1225 len = lexer->saved_tokens.length ();
1226 cp_lexer_save_tokens (lexer);
1228 void rollback ()
1230 cp_lexer_rollback_tokens (lexer);
1231 commit = false;
1233 ~saved_token_sentinel()
1235 if (commit)
1236 cp_lexer_commit_tokens (lexer);
1237 gcc_assert (lexer->saved_tokens.length () == len);
1241 /* Print a representation of the TOKEN on the STREAM. */
1243 static void
1244 cp_lexer_print_token (FILE * stream, cp_token *token)
1246 /* We don't use cpp_type2name here because the parser defines
1247 a few tokens of its own. */
1248 static const char *const token_names[] = {
1249 /* cpplib-defined token types */
1250 #define OP(e, s) #e,
1251 #define TK(e, s) #e,
1252 TTYPE_TABLE
1253 #undef OP
1254 #undef TK
1255 /* C++ parser token types - see "Manifest constants", above. */
1256 "KEYWORD",
1257 "TEMPLATE_ID",
1258 "NESTED_NAME_SPECIFIER",
1261 /* For some tokens, print the associated data. */
1262 switch (token->type)
1264 case CPP_KEYWORD:
1265 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1266 For example, `struct' is mapped to an INTEGER_CST. */
1267 if (!identifier_p (token->u.value))
1268 break;
1269 /* fall through */
1270 case CPP_NAME:
1271 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1272 break;
1274 case CPP_STRING:
1275 case CPP_STRING16:
1276 case CPP_STRING32:
1277 case CPP_WSTRING:
1278 case CPP_UTF8STRING:
1279 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1280 break;
1282 case CPP_NUMBER:
1283 print_generic_expr (stream, token->u.value);
1284 break;
1286 default:
1287 /* If we have a name for the token, print it out. Otherwise, we
1288 simply give the numeric code. */
1289 if (token->type < ARRAY_SIZE(token_names))
1290 fputs (token_names[token->type], stream);
1291 else
1292 fprintf (stream, "[%d]", token->type);
1293 break;
1297 DEBUG_FUNCTION void
1298 debug (cp_token &ref)
1300 cp_lexer_print_token (stderr, &ref);
1301 fprintf (stderr, "\n");
1304 DEBUG_FUNCTION void
1305 debug (cp_token *ptr)
1307 if (ptr)
1308 debug (*ptr);
1309 else
1310 fprintf (stderr, "<nil>\n");
1314 /* Start emitting debugging information. */
1316 static void
1317 cp_lexer_start_debugging (cp_lexer* lexer)
1319 if (!LEXER_DEBUGGING_ENABLED_P)
1320 fatal_error (input_location,
1321 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1323 lexer->debugging_p = true;
1324 cp_lexer_debug_stream = stderr;
1327 /* Stop emitting debugging information. */
1329 static void
1330 cp_lexer_stop_debugging (cp_lexer* lexer)
1332 if (!LEXER_DEBUGGING_ENABLED_P)
1333 fatal_error (input_location,
1334 "LEXER_DEBUGGING_ENABLED_P is not set to true");
1336 lexer->debugging_p = false;
1337 cp_lexer_debug_stream = NULL;
1340 /* Create a new cp_token_cache, representing a range of tokens. */
1342 static cp_token_cache *
1343 cp_token_cache_new (cp_token *first, cp_token *last)
1345 cp_token_cache *cache = ggc_alloc<cp_token_cache> ();
1346 cache->first = first;
1347 cache->last = last;
1348 return cache;
1351 /* Diagnose if #pragma omp declare simd isn't followed immediately
1352 by function declaration or definition. */
1354 static inline void
1355 cp_ensure_no_omp_declare_simd (cp_parser *parser)
1357 if (parser->omp_declare_simd && !parser->omp_declare_simd->error_seen)
1359 error ("%<#pragma omp declare simd%> not immediately followed by "
1360 "function declaration or definition");
1361 parser->omp_declare_simd = NULL;
1365 /* Finalize #pragma omp declare simd clauses after FNDECL has been parsed,
1366 and put that into "omp declare simd" attribute. */
1368 static inline void
1369 cp_finalize_omp_declare_simd (cp_parser *parser, tree fndecl)
1371 if (__builtin_expect (parser->omp_declare_simd != NULL, 0))
1373 if (fndecl == error_mark_node)
1375 parser->omp_declare_simd = NULL;
1376 return;
1378 if (TREE_CODE (fndecl) != FUNCTION_DECL)
1380 cp_ensure_no_omp_declare_simd (parser);
1381 return;
1386 /* Diagnose if #pragma acc routine isn't followed immediately by function
1387 declaration or definition. */
1389 static inline void
1390 cp_ensure_no_oacc_routine (cp_parser *parser)
1392 if (parser->oacc_routine && !parser->oacc_routine->error_seen)
1394 error_at (parser->oacc_routine->loc,
1395 "%<#pragma acc routine%> not immediately followed by "
1396 "function declaration or definition");
1397 parser->oacc_routine = NULL;
1401 /* Decl-specifiers. */
1403 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1405 static void
1406 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1408 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1411 /* Declarators. */
1413 /* Nothing other than the parser should be creating declarators;
1414 declarators are a semi-syntactic representation of C++ entities.
1415 Other parts of the front end that need to create entities (like
1416 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1418 static cp_declarator *make_call_declarator
1419 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, cp_ref_qualifier, tree, tree, tree, tree);
1420 static cp_declarator *make_array_declarator
1421 (cp_declarator *, tree);
1422 static cp_declarator *make_pointer_declarator
1423 (cp_cv_quals, cp_declarator *, tree);
1424 static cp_declarator *make_reference_declarator
1425 (cp_cv_quals, cp_declarator *, bool, tree);
1426 static cp_declarator *make_ptrmem_declarator
1427 (cp_cv_quals, tree, cp_declarator *, tree);
1429 /* An erroneous declarator. */
1430 static cp_declarator *cp_error_declarator;
1432 /* The obstack on which declarators and related data structures are
1433 allocated. */
1434 static struct obstack declarator_obstack;
1436 /* Alloc BYTES from the declarator memory pool. */
1438 static inline void *
1439 alloc_declarator (size_t bytes)
1441 return obstack_alloc (&declarator_obstack, bytes);
1444 /* Allocate a declarator of the indicated KIND. Clear fields that are
1445 common to all declarators. */
1447 static cp_declarator *
1448 make_declarator (cp_declarator_kind kind)
1450 cp_declarator *declarator;
1452 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1453 declarator->kind = kind;
1454 declarator->attributes = NULL_TREE;
1455 declarator->std_attributes = NULL_TREE;
1456 declarator->declarator = NULL;
1457 declarator->parameter_pack_p = false;
1458 declarator->id_loc = UNKNOWN_LOCATION;
1460 return declarator;
1463 /* Make a declarator for a generalized identifier. If
1464 QUALIFYING_SCOPE is non-NULL, the identifier is
1465 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1466 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1467 is, if any. */
1469 static cp_declarator *
1470 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1471 special_function_kind sfk)
1473 cp_declarator *declarator;
1475 /* It is valid to write:
1477 class C { void f(); };
1478 typedef C D;
1479 void D::f();
1481 The standard is not clear about whether `typedef const C D' is
1482 legal; as of 2002-09-15 the committee is considering that
1483 question. EDG 3.0 allows that syntax. Therefore, we do as
1484 well. */
1485 if (qualifying_scope && TYPE_P (qualifying_scope))
1486 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1488 gcc_assert (identifier_p (unqualified_name)
1489 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1490 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1492 declarator = make_declarator (cdk_id);
1493 declarator->u.id.qualifying_scope = qualifying_scope;
1494 declarator->u.id.unqualified_name = unqualified_name;
1495 declarator->u.id.sfk = sfk;
1497 return declarator;
1500 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1501 of modifiers such as const or volatile to apply to the pointer
1502 type, represented as identifiers. ATTRIBUTES represent the attributes that
1503 appertain to the pointer or reference. */
1505 cp_declarator *
1506 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1507 tree attributes)
1509 cp_declarator *declarator;
1511 declarator = make_declarator (cdk_pointer);
1512 declarator->declarator = target;
1513 declarator->u.pointer.qualifiers = cv_qualifiers;
1514 declarator->u.pointer.class_type = NULL_TREE;
1515 if (target)
1517 declarator->id_loc = target->id_loc;
1518 declarator->parameter_pack_p = target->parameter_pack_p;
1519 target->parameter_pack_p = false;
1521 else
1522 declarator->parameter_pack_p = false;
1524 declarator->std_attributes = attributes;
1526 return declarator;
1529 /* Like make_pointer_declarator -- but for references. ATTRIBUTES
1530 represent the attributes that appertain to the pointer or
1531 reference. */
1533 cp_declarator *
1534 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1535 bool rvalue_ref, tree attributes)
1537 cp_declarator *declarator;
1539 declarator = make_declarator (cdk_reference);
1540 declarator->declarator = target;
1541 declarator->u.reference.qualifiers = cv_qualifiers;
1542 declarator->u.reference.rvalue_ref = rvalue_ref;
1543 if (target)
1545 declarator->id_loc = target->id_loc;
1546 declarator->parameter_pack_p = target->parameter_pack_p;
1547 target->parameter_pack_p = false;
1549 else
1550 declarator->parameter_pack_p = false;
1552 declarator->std_attributes = attributes;
1554 return declarator;
1557 /* Like make_pointer_declarator -- but for a pointer to a non-static
1558 member of CLASS_TYPE. ATTRIBUTES represent the attributes that
1559 appertain to the pointer or reference. */
1561 cp_declarator *
1562 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1563 cp_declarator *pointee,
1564 tree attributes)
1566 cp_declarator *declarator;
1568 declarator = make_declarator (cdk_ptrmem);
1569 declarator->declarator = pointee;
1570 declarator->u.pointer.qualifiers = cv_qualifiers;
1571 declarator->u.pointer.class_type = class_type;
1573 if (pointee)
1575 declarator->parameter_pack_p = pointee->parameter_pack_p;
1576 pointee->parameter_pack_p = false;
1578 else
1579 declarator->parameter_pack_p = false;
1581 declarator->std_attributes = attributes;
1583 return declarator;
1586 /* Make a declarator for the function given by TARGET, with the
1587 indicated PARMS. The CV_QUALIFIERS apply to the function, as in
1588 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1589 indicates what exceptions can be thrown. */
1591 cp_declarator *
1592 make_call_declarator (cp_declarator *target,
1593 tree parms,
1594 cp_cv_quals cv_qualifiers,
1595 cp_virt_specifiers virt_specifiers,
1596 cp_ref_qualifier ref_qualifier,
1597 tree tx_qualifier,
1598 tree exception_specification,
1599 tree late_return_type,
1600 tree requires_clause)
1602 cp_declarator *declarator;
1604 declarator = make_declarator (cdk_function);
1605 declarator->declarator = target;
1606 declarator->u.function.parameters = parms;
1607 declarator->u.function.qualifiers = cv_qualifiers;
1608 declarator->u.function.virt_specifiers = virt_specifiers;
1609 declarator->u.function.ref_qualifier = ref_qualifier;
1610 declarator->u.function.tx_qualifier = tx_qualifier;
1611 declarator->u.function.exception_specification = exception_specification;
1612 declarator->u.function.late_return_type = late_return_type;
1613 declarator->u.function.requires_clause = requires_clause;
1614 if (target)
1616 declarator->id_loc = target->id_loc;
1617 declarator->parameter_pack_p = target->parameter_pack_p;
1618 target->parameter_pack_p = false;
1620 else
1621 declarator->parameter_pack_p = false;
1623 return declarator;
1626 /* Make a declarator for an array of BOUNDS elements, each of which is
1627 defined by ELEMENT. */
1629 cp_declarator *
1630 make_array_declarator (cp_declarator *element, tree bounds)
1632 cp_declarator *declarator;
1634 declarator = make_declarator (cdk_array);
1635 declarator->declarator = element;
1636 declarator->u.array.bounds = bounds;
1637 if (element)
1639 declarator->id_loc = element->id_loc;
1640 declarator->parameter_pack_p = element->parameter_pack_p;
1641 element->parameter_pack_p = false;
1643 else
1644 declarator->parameter_pack_p = false;
1646 return declarator;
1649 /* Determine whether the declarator we've seen so far can be a
1650 parameter pack, when followed by an ellipsis. */
1651 static bool
1652 declarator_can_be_parameter_pack (cp_declarator *declarator)
1654 if (declarator && declarator->parameter_pack_p)
1655 /* We already saw an ellipsis. */
1656 return false;
1658 /* Search for a declarator name, or any other declarator that goes
1659 after the point where the ellipsis could appear in a parameter
1660 pack. If we find any of these, then this declarator can not be
1661 made into a parameter pack. */
1662 bool found = false;
1663 while (declarator && !found)
1665 switch ((int)declarator->kind)
1667 case cdk_id:
1668 case cdk_array:
1669 case cdk_decomp:
1670 found = true;
1671 break;
1673 case cdk_error:
1674 return true;
1676 default:
1677 declarator = declarator->declarator;
1678 break;
1682 return !found;
1685 cp_parameter_declarator *no_parameters;
1687 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1688 DECLARATOR and DEFAULT_ARGUMENT. */
1690 cp_parameter_declarator *
1691 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1692 cp_declarator *declarator,
1693 tree default_argument,
1694 bool template_parameter_pack_p = false)
1696 cp_parameter_declarator *parameter;
1698 parameter = ((cp_parameter_declarator *)
1699 alloc_declarator (sizeof (cp_parameter_declarator)));
1700 parameter->next = NULL;
1701 if (decl_specifiers)
1702 parameter->decl_specifiers = *decl_specifiers;
1703 else
1704 clear_decl_specs (&parameter->decl_specifiers);
1705 parameter->declarator = declarator;
1706 parameter->default_argument = default_argument;
1707 parameter->template_parameter_pack_p = template_parameter_pack_p;
1709 return parameter;
1712 /* Returns true iff DECLARATOR is a declaration for a function. */
1714 static bool
1715 function_declarator_p (const cp_declarator *declarator)
1717 while (declarator)
1719 if (declarator->kind == cdk_function
1720 && declarator->declarator->kind == cdk_id)
1721 return true;
1722 if (declarator->kind == cdk_id
1723 || declarator->kind == cdk_decomp
1724 || declarator->kind == cdk_error)
1725 return false;
1726 declarator = declarator->declarator;
1728 return false;
1731 /* The parser. */
1733 /* Overview
1734 --------
1736 A cp_parser parses the token stream as specified by the C++
1737 grammar. Its job is purely parsing, not semantic analysis. For
1738 example, the parser breaks the token stream into declarators,
1739 expressions, statements, and other similar syntactic constructs.
1740 It does not check that the types of the expressions on either side
1741 of an assignment-statement are compatible, or that a function is
1742 not declared with a parameter of type `void'.
1744 The parser invokes routines elsewhere in the compiler to perform
1745 semantic analysis and to build up the abstract syntax tree for the
1746 code processed.
1748 The parser (and the template instantiation code, which is, in a
1749 way, a close relative of parsing) are the only parts of the
1750 compiler that should be calling push_scope and pop_scope, or
1751 related functions. The parser (and template instantiation code)
1752 keeps track of what scope is presently active; everything else
1753 should simply honor that. (The code that generates static
1754 initializers may also need to set the scope, in order to check
1755 access control correctly when emitting the initializers.)
1757 Methodology
1758 -----------
1760 The parser is of the standard recursive-descent variety. Upcoming
1761 tokens in the token stream are examined in order to determine which
1762 production to use when parsing a non-terminal. Some C++ constructs
1763 require arbitrary look ahead to disambiguate. For example, it is
1764 impossible, in the general case, to tell whether a statement is an
1765 expression or declaration without scanning the entire statement.
1766 Therefore, the parser is capable of "parsing tentatively." When the
1767 parser is not sure what construct comes next, it enters this mode.
1768 Then, while we attempt to parse the construct, the parser queues up
1769 error messages, rather than issuing them immediately, and saves the
1770 tokens it consumes. If the construct is parsed successfully, the
1771 parser "commits", i.e., it issues any queued error messages and
1772 the tokens that were being preserved are permanently discarded.
1773 If, however, the construct is not parsed successfully, the parser
1774 rolls back its state completely so that it can resume parsing using
1775 a different alternative.
1777 Future Improvements
1778 -------------------
1780 The performance of the parser could probably be improved substantially.
1781 We could often eliminate the need to parse tentatively by looking ahead
1782 a little bit. In some places, this approach might not entirely eliminate
1783 the need to parse tentatively, but it might still speed up the average
1784 case. */
1786 /* Flags that are passed to some parsing functions. These values can
1787 be bitwise-ored together. */
1789 enum
1791 /* No flags. */
1792 CP_PARSER_FLAGS_NONE = 0x0,
1793 /* The construct is optional. If it is not present, then no error
1794 should be issued. */
1795 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1796 /* When parsing a type-specifier, treat user-defined type-names
1797 as non-type identifiers. */
1798 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1799 /* When parsing a type-specifier, do not try to parse a class-specifier
1800 or enum-specifier. */
1801 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1802 /* When parsing a decl-specifier-seq, only allow type-specifier or
1803 constexpr. */
1804 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8,
1805 /* When parsing a decl-specifier-seq, only allow mutable or constexpr. */
1806 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR = 0x10
1809 /* This type is used for parameters and variables which hold
1810 combinations of the above flags. */
1811 typedef int cp_parser_flags;
1813 /* The different kinds of declarators we want to parse. */
1815 enum cp_parser_declarator_kind
1817 /* We want an abstract declarator. */
1818 CP_PARSER_DECLARATOR_ABSTRACT,
1819 /* We want a named declarator. */
1820 CP_PARSER_DECLARATOR_NAMED,
1821 /* We don't mind, but the name must be an unqualified-id. */
1822 CP_PARSER_DECLARATOR_EITHER
1825 /* The precedence values used to parse binary expressions. The minimum value
1826 of PREC must be 1, because zero is reserved to quickly discriminate
1827 binary operators from other tokens. */
1829 enum cp_parser_prec
1831 PREC_NOT_OPERATOR,
1832 PREC_LOGICAL_OR_EXPRESSION,
1833 PREC_LOGICAL_AND_EXPRESSION,
1834 PREC_INCLUSIVE_OR_EXPRESSION,
1835 PREC_EXCLUSIVE_OR_EXPRESSION,
1836 PREC_AND_EXPRESSION,
1837 PREC_EQUALITY_EXPRESSION,
1838 PREC_RELATIONAL_EXPRESSION,
1839 PREC_SHIFT_EXPRESSION,
1840 PREC_ADDITIVE_EXPRESSION,
1841 PREC_MULTIPLICATIVE_EXPRESSION,
1842 PREC_PM_EXPRESSION,
1843 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1846 /* A mapping from a token type to a corresponding tree node type, with a
1847 precedence value. */
1849 struct cp_parser_binary_operations_map_node
1851 /* The token type. */
1852 enum cpp_ttype token_type;
1853 /* The corresponding tree code. */
1854 enum tree_code tree_type;
1855 /* The precedence of this operator. */
1856 enum cp_parser_prec prec;
1859 struct cp_parser_expression_stack_entry
1861 /* Left hand side of the binary operation we are currently
1862 parsing. */
1863 cp_expr lhs;
1864 /* Original tree code for left hand side, if it was a binary
1865 expression itself (used for -Wparentheses). */
1866 enum tree_code lhs_type;
1867 /* Tree code for the binary operation we are parsing. */
1868 enum tree_code tree_type;
1869 /* Precedence of the binary operation we are parsing. */
1870 enum cp_parser_prec prec;
1871 /* Location of the binary operation we are parsing. */
1872 location_t loc;
1875 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1876 entries because precedence levels on the stack are monotonically
1877 increasing. */
1878 typedef struct cp_parser_expression_stack_entry
1879 cp_parser_expression_stack[NUM_PREC_VALUES];
1881 /* Prototypes. */
1883 /* Constructors and destructors. */
1885 static cp_parser_context *cp_parser_context_new
1886 (cp_parser_context *);
1888 /* Class variables. */
1890 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1892 /* The operator-precedence table used by cp_parser_binary_expression.
1893 Transformed into an associative array (binops_by_token) by
1894 cp_parser_new. */
1896 static const cp_parser_binary_operations_map_node binops[] = {
1897 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1898 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1900 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1901 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1902 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1904 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1905 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1907 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1908 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1910 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1911 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1912 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1913 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1915 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1916 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1918 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1920 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1922 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1924 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1926 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1929 /* The same as binops, but initialized by cp_parser_new so that
1930 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1931 for speed. */
1932 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1934 /* Constructors and destructors. */
1936 /* Construct a new context. The context below this one on the stack
1937 is given by NEXT. */
1939 static cp_parser_context *
1940 cp_parser_context_new (cp_parser_context* next)
1942 cp_parser_context *context;
1944 /* Allocate the storage. */
1945 if (cp_parser_context_free_list != NULL)
1947 /* Pull the first entry from the free list. */
1948 context = cp_parser_context_free_list;
1949 cp_parser_context_free_list = context->next;
1950 memset (context, 0, sizeof (*context));
1952 else
1953 context = ggc_cleared_alloc<cp_parser_context> ();
1955 /* No errors have occurred yet in this context. */
1956 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1957 /* If this is not the bottommost context, copy information that we
1958 need from the previous context. */
1959 if (next)
1961 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1962 expression, then we are parsing one in this context, too. */
1963 context->object_type = next->object_type;
1964 /* Thread the stack. */
1965 context->next = next;
1968 return context;
1971 /* Managing the unparsed function queues. */
1973 #define unparsed_funs_with_default_args \
1974 parser->unparsed_queues->last ().funs_with_default_args
1975 #define unparsed_funs_with_definitions \
1976 parser->unparsed_queues->last ().funs_with_definitions
1977 #define unparsed_nsdmis \
1978 parser->unparsed_queues->last ().nsdmis
1979 #define unparsed_classes \
1980 parser->unparsed_queues->last ().classes
1982 static void
1983 push_unparsed_function_queues (cp_parser *parser)
1985 cp_unparsed_functions_entry e = {NULL, make_tree_vector (), NULL, NULL};
1986 vec_safe_push (parser->unparsed_queues, e);
1989 static void
1990 pop_unparsed_function_queues (cp_parser *parser)
1992 release_tree_vector (unparsed_funs_with_definitions);
1993 parser->unparsed_queues->pop ();
1996 /* Prototypes. */
1998 /* Constructors and destructors. */
2000 static cp_parser *cp_parser_new
2001 (void);
2003 /* Routines to parse various constructs.
2005 Those that return `tree' will return the error_mark_node (rather
2006 than NULL_TREE) if a parse error occurs, unless otherwise noted.
2007 Sometimes, they will return an ordinary node if error-recovery was
2008 attempted, even though a parse error occurred. So, to check
2009 whether or not a parse error occurred, you should always use
2010 cp_parser_error_occurred. If the construct is optional (indicated
2011 either by an `_opt' in the name of the function that does the
2012 parsing or via a FLAGS parameter), then NULL_TREE is returned if
2013 the construct is not present. */
2015 /* Lexical conventions [gram.lex] */
2017 static cp_expr cp_parser_identifier
2018 (cp_parser *);
2019 static cp_expr cp_parser_string_literal
2020 (cp_parser *, bool, bool, bool);
2021 static cp_expr cp_parser_userdef_char_literal
2022 (cp_parser *);
2023 static tree cp_parser_userdef_string_literal
2024 (tree);
2025 static cp_expr cp_parser_userdef_numeric_literal
2026 (cp_parser *);
2028 /* Basic concepts [gram.basic] */
2030 static bool cp_parser_translation_unit
2031 (cp_parser *);
2033 /* Expressions [gram.expr] */
2035 static cp_expr cp_parser_primary_expression
2036 (cp_parser *, bool, bool, bool, cp_id_kind *);
2037 static cp_expr cp_parser_id_expression
2038 (cp_parser *, bool, bool, bool *, bool, bool);
2039 static cp_expr cp_parser_unqualified_id
2040 (cp_parser *, bool, bool, bool, bool);
2041 static tree cp_parser_nested_name_specifier_opt
2042 (cp_parser *, bool, bool, bool, bool, bool = false);
2043 static tree cp_parser_nested_name_specifier
2044 (cp_parser *, bool, bool, bool, bool);
2045 static tree cp_parser_qualifying_entity
2046 (cp_parser *, bool, bool, bool, bool, bool);
2047 static cp_expr cp_parser_postfix_expression
2048 (cp_parser *, bool, bool, bool, bool, cp_id_kind *);
2049 static tree cp_parser_postfix_open_square_expression
2050 (cp_parser *, tree, bool, bool);
2051 static tree cp_parser_postfix_dot_deref_expression
2052 (cp_parser *, enum cpp_ttype, cp_expr, bool, cp_id_kind *, location_t);
2053 static vec<tree, va_gc> *cp_parser_parenthesized_expression_list
2054 (cp_parser *, int, bool, bool, bool *, location_t * = NULL);
2055 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
2056 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
2057 static void cp_parser_pseudo_destructor_name
2058 (cp_parser *, tree, tree *, tree *);
2059 static cp_expr cp_parser_unary_expression
2060 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
2061 static enum tree_code cp_parser_unary_operator
2062 (cp_token *);
2063 static tree cp_parser_new_expression
2064 (cp_parser *);
2065 static vec<tree, va_gc> *cp_parser_new_placement
2066 (cp_parser *);
2067 static tree cp_parser_new_type_id
2068 (cp_parser *, tree *);
2069 static cp_declarator *cp_parser_new_declarator_opt
2070 (cp_parser *);
2071 static cp_declarator *cp_parser_direct_new_declarator
2072 (cp_parser *);
2073 static vec<tree, va_gc> *cp_parser_new_initializer
2074 (cp_parser *);
2075 static tree cp_parser_delete_expression
2076 (cp_parser *);
2077 static cp_expr cp_parser_cast_expression
2078 (cp_parser *, bool, bool, bool, cp_id_kind *);
2079 static cp_expr cp_parser_binary_expression
2080 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
2081 static tree cp_parser_question_colon_clause
2082 (cp_parser *, cp_expr);
2083 static cp_expr cp_parser_assignment_expression
2084 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2085 static enum tree_code cp_parser_assignment_operator_opt
2086 (cp_parser *);
2087 static cp_expr cp_parser_expression
2088 (cp_parser *, cp_id_kind * = NULL, bool = false, bool = false);
2089 static cp_expr cp_parser_constant_expression
2090 (cp_parser *, bool = false, bool * = NULL);
2091 static cp_expr cp_parser_builtin_offsetof
2092 (cp_parser *);
2093 static cp_expr cp_parser_lambda_expression
2094 (cp_parser *);
2095 static void cp_parser_lambda_introducer
2096 (cp_parser *, tree);
2097 static bool cp_parser_lambda_declarator_opt
2098 (cp_parser *, tree);
2099 static void cp_parser_lambda_body
2100 (cp_parser *, tree);
2102 /* Statements [gram.stmt.stmt] */
2104 static void cp_parser_statement
2105 (cp_parser *, tree, bool, bool *, vec<tree> * = NULL, location_t * = NULL);
2106 static void cp_parser_label_for_labeled_statement
2107 (cp_parser *, tree);
2108 static tree cp_parser_expression_statement
2109 (cp_parser *, tree);
2110 static tree cp_parser_compound_statement
2111 (cp_parser *, tree, int, bool);
2112 static void cp_parser_statement_seq_opt
2113 (cp_parser *, tree);
2114 static tree cp_parser_selection_statement
2115 (cp_parser *, bool *, vec<tree> *);
2116 static tree cp_parser_condition
2117 (cp_parser *);
2118 static tree cp_parser_iteration_statement
2119 (cp_parser *, bool *, bool);
2120 static bool cp_parser_init_statement
2121 (cp_parser *, tree *decl);
2122 static tree cp_parser_for
2123 (cp_parser *, bool);
2124 static tree cp_parser_c_for
2125 (cp_parser *, tree, tree, bool);
2126 static tree cp_parser_range_for
2127 (cp_parser *, tree, tree, tree, bool);
2128 static void do_range_for_auto_deduction
2129 (tree, tree);
2130 static tree cp_parser_perform_range_for_lookup
2131 (tree, tree *, tree *);
2132 static tree cp_parser_range_for_member_function
2133 (tree, tree);
2134 static tree cp_parser_jump_statement
2135 (cp_parser *);
2136 static void cp_parser_declaration_statement
2137 (cp_parser *);
2139 static tree cp_parser_implicitly_scoped_statement
2140 (cp_parser *, bool *, const token_indent_info &, vec<tree> * = NULL);
2141 static void cp_parser_already_scoped_statement
2142 (cp_parser *, bool *, const token_indent_info &);
2144 /* Declarations [gram.dcl.dcl] */
2146 static void cp_parser_declaration_seq_opt
2147 (cp_parser *);
2148 static void cp_parser_declaration
2149 (cp_parser *);
2150 static void cp_parser_block_declaration
2151 (cp_parser *, bool);
2152 static void cp_parser_simple_declaration
2153 (cp_parser *, bool, tree *);
2154 static void cp_parser_decl_specifier_seq
2155 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
2156 static tree cp_parser_storage_class_specifier_opt
2157 (cp_parser *);
2158 static tree cp_parser_function_specifier_opt
2159 (cp_parser *, cp_decl_specifier_seq *);
2160 static tree cp_parser_type_specifier
2161 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
2162 int *, bool *);
2163 static tree cp_parser_simple_type_specifier
2164 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
2165 static tree cp_parser_type_name
2166 (cp_parser *, bool);
2167 static tree cp_parser_type_name
2168 (cp_parser *);
2169 static tree cp_parser_nonclass_name
2170 (cp_parser* parser);
2171 static tree cp_parser_elaborated_type_specifier
2172 (cp_parser *, bool, bool);
2173 static tree cp_parser_enum_specifier
2174 (cp_parser *);
2175 static void cp_parser_enumerator_list
2176 (cp_parser *, tree);
2177 static void cp_parser_enumerator_definition
2178 (cp_parser *, tree);
2179 static tree cp_parser_namespace_name
2180 (cp_parser *);
2181 static void cp_parser_namespace_definition
2182 (cp_parser *);
2183 static void cp_parser_namespace_body
2184 (cp_parser *);
2185 static tree cp_parser_qualified_namespace_specifier
2186 (cp_parser *);
2187 static void cp_parser_namespace_alias_definition
2188 (cp_parser *);
2189 static bool cp_parser_using_declaration
2190 (cp_parser *, bool);
2191 static void cp_parser_using_directive
2192 (cp_parser *);
2193 static tree cp_parser_alias_declaration
2194 (cp_parser *);
2195 static void cp_parser_asm_definition
2196 (cp_parser *);
2197 static void cp_parser_linkage_specification
2198 (cp_parser *);
2199 static void cp_parser_static_assert
2200 (cp_parser *, bool);
2201 static tree cp_parser_decltype
2202 (cp_parser *);
2203 static tree cp_parser_decomposition_declaration
2204 (cp_parser *, cp_decl_specifier_seq *, tree *, location_t *);
2206 /* Declarators [gram.dcl.decl] */
2208 static tree cp_parser_init_declarator
2209 (cp_parser *, cp_decl_specifier_seq *, vec<deferred_access_check, va_gc> *,
2210 bool, bool, int, bool *, tree *, location_t *, tree *);
2211 static cp_declarator *cp_parser_declarator
2212 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool, bool);
2213 static cp_declarator *cp_parser_direct_declarator
2214 (cp_parser *, cp_parser_declarator_kind, int *, bool, bool);
2215 static enum tree_code cp_parser_ptr_operator
2216 (cp_parser *, tree *, cp_cv_quals *, tree *);
2217 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
2218 (cp_parser *);
2219 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
2220 (cp_parser *);
2221 static cp_ref_qualifier cp_parser_ref_qualifier_opt
2222 (cp_parser *);
2223 static tree cp_parser_tx_qualifier_opt
2224 (cp_parser *);
2225 static tree cp_parser_late_return_type_opt
2226 (cp_parser *, cp_declarator *, tree &, cp_cv_quals);
2227 static tree cp_parser_declarator_id
2228 (cp_parser *, bool);
2229 static tree cp_parser_type_id
2230 (cp_parser *);
2231 static tree cp_parser_template_type_arg
2232 (cp_parser *);
2233 static tree cp_parser_trailing_type_id (cp_parser *);
2234 static tree cp_parser_type_id_1
2235 (cp_parser *, bool, bool);
2236 static void cp_parser_type_specifier_seq
2237 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
2238 static tree cp_parser_parameter_declaration_clause
2239 (cp_parser *);
2240 static tree cp_parser_parameter_declaration_list
2241 (cp_parser *, bool *);
2242 static cp_parameter_declarator *cp_parser_parameter_declaration
2243 (cp_parser *, bool, bool *);
2244 static tree cp_parser_default_argument
2245 (cp_parser *, bool);
2246 static void cp_parser_function_body
2247 (cp_parser *, bool);
2248 static tree cp_parser_initializer
2249 (cp_parser *, bool *, bool *);
2250 static cp_expr cp_parser_initializer_clause
2251 (cp_parser *, bool *);
2252 static cp_expr cp_parser_braced_list
2253 (cp_parser*, bool*);
2254 static vec<constructor_elt, va_gc> *cp_parser_initializer_list
2255 (cp_parser *, bool *);
2257 static bool cp_parser_ctor_initializer_opt_and_function_body
2258 (cp_parser *, bool);
2260 static tree cp_parser_late_parsing_omp_declare_simd
2261 (cp_parser *, tree);
2263 static tree cp_parser_late_parsing_cilk_simd_fn_info
2264 (cp_parser *, tree);
2266 static tree cp_parser_late_parsing_oacc_routine
2267 (cp_parser *, tree);
2269 static tree synthesize_implicit_template_parm
2270 (cp_parser *, tree);
2271 static tree finish_fully_implicit_template
2272 (cp_parser *, tree);
2274 /* Classes [gram.class] */
2276 static tree cp_parser_class_name
2277 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool, bool = false);
2278 static tree cp_parser_class_specifier
2279 (cp_parser *);
2280 static tree cp_parser_class_head
2281 (cp_parser *, bool *);
2282 static enum tag_types cp_parser_class_key
2283 (cp_parser *);
2284 static void cp_parser_type_parameter_key
2285 (cp_parser* parser);
2286 static void cp_parser_member_specification_opt
2287 (cp_parser *);
2288 static void cp_parser_member_declaration
2289 (cp_parser *);
2290 static tree cp_parser_pure_specifier
2291 (cp_parser *);
2292 static tree cp_parser_constant_initializer
2293 (cp_parser *);
2295 /* Derived classes [gram.class.derived] */
2297 static tree cp_parser_base_clause
2298 (cp_parser *);
2299 static tree cp_parser_base_specifier
2300 (cp_parser *);
2302 /* Special member functions [gram.special] */
2304 static tree cp_parser_conversion_function_id
2305 (cp_parser *);
2306 static tree cp_parser_conversion_type_id
2307 (cp_parser *);
2308 static cp_declarator *cp_parser_conversion_declarator_opt
2309 (cp_parser *);
2310 static bool cp_parser_ctor_initializer_opt
2311 (cp_parser *);
2312 static void cp_parser_mem_initializer_list
2313 (cp_parser *);
2314 static tree cp_parser_mem_initializer
2315 (cp_parser *);
2316 static tree cp_parser_mem_initializer_id
2317 (cp_parser *);
2319 /* Overloading [gram.over] */
2321 static cp_expr cp_parser_operator_function_id
2322 (cp_parser *);
2323 static cp_expr cp_parser_operator
2324 (cp_parser *);
2326 /* Templates [gram.temp] */
2328 static void cp_parser_template_declaration
2329 (cp_parser *, bool);
2330 static tree cp_parser_template_parameter_list
2331 (cp_parser *);
2332 static tree cp_parser_template_parameter
2333 (cp_parser *, bool *, bool *);
2334 static tree cp_parser_type_parameter
2335 (cp_parser *, bool *);
2336 static tree cp_parser_template_id
2337 (cp_parser *, bool, bool, enum tag_types, bool);
2338 static tree cp_parser_template_name
2339 (cp_parser *, bool, bool, bool, enum tag_types, bool *);
2340 static tree cp_parser_template_argument_list
2341 (cp_parser *);
2342 static tree cp_parser_template_argument
2343 (cp_parser *);
2344 static void cp_parser_explicit_instantiation
2345 (cp_parser *);
2346 static void cp_parser_explicit_specialization
2347 (cp_parser *);
2349 /* Exception handling [gram.exception] */
2351 static tree cp_parser_try_block
2352 (cp_parser *);
2353 static bool cp_parser_function_try_block
2354 (cp_parser *);
2355 static void cp_parser_handler_seq
2356 (cp_parser *);
2357 static void cp_parser_handler
2358 (cp_parser *);
2359 static tree cp_parser_exception_declaration
2360 (cp_parser *);
2361 static tree cp_parser_throw_expression
2362 (cp_parser *);
2363 static tree cp_parser_exception_specification_opt
2364 (cp_parser *);
2365 static tree cp_parser_type_id_list
2366 (cp_parser *);
2368 /* GNU Extensions */
2370 static tree cp_parser_asm_specification_opt
2371 (cp_parser *);
2372 static tree cp_parser_asm_operand_list
2373 (cp_parser *);
2374 static tree cp_parser_asm_clobber_list
2375 (cp_parser *);
2376 static tree cp_parser_asm_label_list
2377 (cp_parser *);
2378 static bool cp_next_tokens_can_be_attribute_p
2379 (cp_parser *);
2380 static bool cp_next_tokens_can_be_gnu_attribute_p
2381 (cp_parser *);
2382 static bool cp_next_tokens_can_be_std_attribute_p
2383 (cp_parser *);
2384 static bool cp_nth_tokens_can_be_std_attribute_p
2385 (cp_parser *, size_t);
2386 static bool cp_nth_tokens_can_be_gnu_attribute_p
2387 (cp_parser *, size_t);
2388 static bool cp_nth_tokens_can_be_attribute_p
2389 (cp_parser *, size_t);
2390 static tree cp_parser_attributes_opt
2391 (cp_parser *);
2392 static tree cp_parser_gnu_attributes_opt
2393 (cp_parser *);
2394 static tree cp_parser_gnu_attribute_list
2395 (cp_parser *);
2396 static tree cp_parser_std_attribute
2397 (cp_parser *, tree);
2398 static tree cp_parser_std_attribute_spec
2399 (cp_parser *);
2400 static tree cp_parser_std_attribute_spec_seq
2401 (cp_parser *);
2402 static bool cp_parser_extension_opt
2403 (cp_parser *, int *);
2404 static void cp_parser_label_declaration
2405 (cp_parser *);
2407 /* Concept Extensions */
2409 static tree cp_parser_requires_clause
2410 (cp_parser *);
2411 static tree cp_parser_requires_clause_opt
2412 (cp_parser *);
2413 static tree cp_parser_requires_expression
2414 (cp_parser *);
2415 static tree cp_parser_requirement_parameter_list
2416 (cp_parser *);
2417 static tree cp_parser_requirement_body
2418 (cp_parser *);
2419 static tree cp_parser_requirement_list
2420 (cp_parser *);
2421 static tree cp_parser_requirement
2422 (cp_parser *);
2423 static tree cp_parser_simple_requirement
2424 (cp_parser *);
2425 static tree cp_parser_compound_requirement
2426 (cp_parser *);
2427 static tree cp_parser_type_requirement
2428 (cp_parser *);
2429 static tree cp_parser_nested_requirement
2430 (cp_parser *);
2432 /* Transactional Memory Extensions */
2434 static tree cp_parser_transaction
2435 (cp_parser *, cp_token *);
2436 static tree cp_parser_transaction_expression
2437 (cp_parser *, enum rid);
2438 static bool cp_parser_function_transaction
2439 (cp_parser *, enum rid);
2440 static tree cp_parser_transaction_cancel
2441 (cp_parser *);
2443 enum pragma_context {
2444 pragma_external,
2445 pragma_member,
2446 pragma_objc_icode,
2447 pragma_stmt,
2448 pragma_compound
2450 static bool cp_parser_pragma
2451 (cp_parser *, enum pragma_context, bool *);
2453 /* Objective-C++ Productions */
2455 static tree cp_parser_objc_message_receiver
2456 (cp_parser *);
2457 static tree cp_parser_objc_message_args
2458 (cp_parser *);
2459 static tree cp_parser_objc_message_expression
2460 (cp_parser *);
2461 static cp_expr cp_parser_objc_encode_expression
2462 (cp_parser *);
2463 static tree cp_parser_objc_defs_expression
2464 (cp_parser *);
2465 static tree cp_parser_objc_protocol_expression
2466 (cp_parser *);
2467 static tree cp_parser_objc_selector_expression
2468 (cp_parser *);
2469 static cp_expr cp_parser_objc_expression
2470 (cp_parser *);
2471 static bool cp_parser_objc_selector_p
2472 (enum cpp_ttype);
2473 static tree cp_parser_objc_selector
2474 (cp_parser *);
2475 static tree cp_parser_objc_protocol_refs_opt
2476 (cp_parser *);
2477 static void cp_parser_objc_declaration
2478 (cp_parser *, tree);
2479 static tree cp_parser_objc_statement
2480 (cp_parser *);
2481 static bool cp_parser_objc_valid_prefix_attributes
2482 (cp_parser *, tree *);
2483 static void cp_parser_objc_at_property_declaration
2484 (cp_parser *) ;
2485 static void cp_parser_objc_at_synthesize_declaration
2486 (cp_parser *) ;
2487 static void cp_parser_objc_at_dynamic_declaration
2488 (cp_parser *) ;
2489 static tree cp_parser_objc_struct_declaration
2490 (cp_parser *) ;
2492 /* Utility Routines */
2494 static cp_expr cp_parser_lookup_name
2495 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2496 static tree cp_parser_lookup_name_simple
2497 (cp_parser *, tree, location_t);
2498 static tree cp_parser_maybe_treat_template_as_class
2499 (tree, bool);
2500 static bool cp_parser_check_declarator_template_parameters
2501 (cp_parser *, cp_declarator *, location_t);
2502 static bool cp_parser_check_template_parameters
2503 (cp_parser *, unsigned, location_t, cp_declarator *);
2504 static cp_expr cp_parser_simple_cast_expression
2505 (cp_parser *);
2506 static tree cp_parser_global_scope_opt
2507 (cp_parser *, bool);
2508 static bool cp_parser_constructor_declarator_p
2509 (cp_parser *, bool);
2510 static tree cp_parser_function_definition_from_specifiers_and_declarator
2511 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2512 static tree cp_parser_function_definition_after_declarator
2513 (cp_parser *, bool);
2514 static bool cp_parser_template_declaration_after_export
2515 (cp_parser *, bool);
2516 static void cp_parser_perform_template_parameter_access_checks
2517 (vec<deferred_access_check, va_gc> *);
2518 static tree cp_parser_single_declaration
2519 (cp_parser *, vec<deferred_access_check, va_gc> *, bool, bool, bool *);
2520 static cp_expr cp_parser_functional_cast
2521 (cp_parser *, tree);
2522 static tree cp_parser_save_member_function_body
2523 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2524 static tree cp_parser_save_nsdmi
2525 (cp_parser *);
2526 static tree cp_parser_enclosed_template_argument_list
2527 (cp_parser *);
2528 static void cp_parser_save_default_args
2529 (cp_parser *, tree);
2530 static void cp_parser_late_parsing_for_member
2531 (cp_parser *, tree);
2532 static tree cp_parser_late_parse_one_default_arg
2533 (cp_parser *, tree, tree, tree);
2534 static void cp_parser_late_parsing_nsdmi
2535 (cp_parser *, tree);
2536 static void cp_parser_late_parsing_default_args
2537 (cp_parser *, tree);
2538 static tree cp_parser_sizeof_operand
2539 (cp_parser *, enum rid);
2540 static tree cp_parser_trait_expr
2541 (cp_parser *, enum rid);
2542 static bool cp_parser_declares_only_class_p
2543 (cp_parser *);
2544 static void cp_parser_set_storage_class
2545 (cp_parser *, cp_decl_specifier_seq *, enum rid, cp_token *);
2546 static void cp_parser_set_decl_spec_type
2547 (cp_decl_specifier_seq *, tree, cp_token *, bool);
2548 static void set_and_check_decl_spec_loc
2549 (cp_decl_specifier_seq *decl_specs,
2550 cp_decl_spec ds, cp_token *);
2551 static bool cp_parser_friend_p
2552 (const cp_decl_specifier_seq *);
2553 static void cp_parser_required_error
2554 (cp_parser *, required_token, bool);
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 if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3257 error_at (location_of (id),
3258 "%qE in namespace %qE does not name a template type",
3259 TREE_OPERAND (id, 0), parser->scope);
3260 else
3261 error_at (location_of (id),
3262 "%qE in namespace %qE does not name a type",
3263 id, parser->scope);
3264 if (DECL_P (decl))
3265 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3267 else if (CLASS_TYPE_P (parser->scope)
3268 && constructor_name_p (id, parser->scope))
3270 /* A<T>::A<T>() */
3271 error_at (location, "%<%T::%E%> names the constructor, not"
3272 " the type", parser->scope, id);
3273 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3274 error_at (location, "and %qT has no template constructors",
3275 parser->scope);
3277 else if (TYPE_P (parser->scope)
3278 && dependent_scope_p (parser->scope))
3280 if (TREE_CODE (parser->scope) == TYPENAME_TYPE)
3281 error_at (location,
3282 "need %<typename%> before %<%T::%D::%E%> because "
3283 "%<%T::%D%> is a dependent scope",
3284 TYPE_CONTEXT (parser->scope),
3285 TYPENAME_TYPE_FULLNAME (parser->scope),
3287 TYPE_CONTEXT (parser->scope),
3288 TYPENAME_TYPE_FULLNAME (parser->scope));
3289 else
3290 error_at (location, "need %<typename%> before %<%T::%E%> because "
3291 "%qT is a dependent scope",
3292 parser->scope, id, parser->scope);
3294 else if (TYPE_P (parser->scope))
3296 if (!COMPLETE_TYPE_P (parser->scope))
3297 cxx_incomplete_type_error (location_of (id), NULL_TREE,
3298 parser->scope);
3299 else if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
3300 error_at (location_of (id),
3301 "%qE in %q#T does not name a template type",
3302 id, parser->scope);
3303 else if (TREE_CODE (id) == TEMPLATE_ID_EXPR)
3304 error_at (location_of (id),
3305 "%qE in %q#T does not name a template type",
3306 TREE_OPERAND (id, 0), parser->scope);
3307 else
3308 error_at (location_of (id),
3309 "%qE in %q#T does not name a type",
3310 id, parser->scope);
3311 if (DECL_P (decl))
3312 inform (DECL_SOURCE_LOCATION (decl), "%qD declared here", decl);
3314 else
3315 gcc_unreachable ();
3319 /* Check for a common situation where a type-name should be present,
3320 but is not, and issue a sensible error message. Returns true if an
3321 invalid type-name was detected.
3323 The situation handled by this function are variable declarations of the
3324 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
3325 Usually, `ID' should name a type, but if we got here it means that it
3326 does not. We try to emit the best possible error message depending on
3327 how exactly the id-expression looks like. */
3329 static bool
3330 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
3332 tree id;
3333 cp_token *token = cp_lexer_peek_token (parser->lexer);
3335 /* Avoid duplicate error about ambiguous lookup. */
3336 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3338 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
3339 if (next->type == CPP_NAME && next->error_reported)
3340 goto out;
3343 cp_parser_parse_tentatively (parser);
3344 id = cp_parser_id_expression (parser,
3345 /*template_keyword_p=*/false,
3346 /*check_dependency_p=*/true,
3347 /*template_p=*/NULL,
3348 /*declarator_p=*/true,
3349 /*optional_p=*/false);
3350 /* If the next token is a (, this is a function with no explicit return
3351 type, i.e. constructor, destructor or conversion op. */
3352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
3353 || TREE_CODE (id) == TYPE_DECL)
3355 cp_parser_abort_tentative_parse (parser);
3356 return false;
3358 if (!cp_parser_parse_definitely (parser))
3359 return false;
3361 /* Emit a diagnostic for the invalid type. */
3362 cp_parser_diagnose_invalid_type_name (parser, id, token->location);
3363 out:
3364 /* If we aren't in the middle of a declarator (i.e. in a
3365 parameter-declaration-clause), skip to the end of the declaration;
3366 there's no point in trying to process it. */
3367 if (!parser->in_declarator_p)
3368 cp_parser_skip_to_end_of_block_or_statement (parser);
3369 return true;
3372 /* Consume tokens up to, and including, the next non-nested closing `)'.
3373 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3374 are doing error recovery. Returns -1 if OR_TTYPE is not CPP_EOF and we
3375 found an unnested token of that type. */
3377 static int
3378 cp_parser_skip_to_closing_parenthesis_1 (cp_parser *parser,
3379 bool recovering,
3380 cpp_ttype or_ttype,
3381 bool consume_paren)
3383 unsigned paren_depth = 0;
3384 unsigned brace_depth = 0;
3385 unsigned square_depth = 0;
3387 if (recovering && or_ttype == CPP_EOF
3388 && cp_parser_uncommitted_to_tentative_parse_p (parser))
3389 return 0;
3391 while (true)
3393 cp_token * token = cp_lexer_peek_token (parser->lexer);
3395 /* Have we found what we're looking for before the closing paren? */
3396 if (token->type == or_ttype && or_ttype != CPP_EOF
3397 && !brace_depth && !paren_depth && !square_depth)
3398 return -1;
3400 switch (token->type)
3402 case CPP_EOF:
3403 case CPP_PRAGMA_EOL:
3404 /* If we've run out of tokens, then there is no closing `)'. */
3405 return 0;
3407 /* This is good for lambda expression capture-lists. */
3408 case CPP_OPEN_SQUARE:
3409 ++square_depth;
3410 break;
3411 case CPP_CLOSE_SQUARE:
3412 if (!square_depth--)
3413 return 0;
3414 break;
3416 case CPP_SEMICOLON:
3417 /* This matches the processing in skip_to_end_of_statement. */
3418 if (!brace_depth)
3419 return 0;
3420 break;
3422 case CPP_OPEN_BRACE:
3423 ++brace_depth;
3424 break;
3425 case CPP_CLOSE_BRACE:
3426 if (!brace_depth--)
3427 return 0;
3428 break;
3430 case CPP_OPEN_PAREN:
3431 if (!brace_depth)
3432 ++paren_depth;
3433 break;
3435 case CPP_CLOSE_PAREN:
3436 if (!brace_depth && !paren_depth--)
3438 if (consume_paren)
3439 cp_lexer_consume_token (parser->lexer);
3440 return 1;
3442 break;
3444 default:
3445 break;
3448 /* Consume the token. */
3449 cp_lexer_consume_token (parser->lexer);
3453 /* Consume tokens up to, and including, the next non-nested closing `)'.
3454 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
3455 are doing error recovery. Returns -1 if OR_COMMA is true and we
3456 found an unnested token of that type. */
3458 static int
3459 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
3460 bool recovering,
3461 bool or_comma,
3462 bool consume_paren)
3464 cpp_ttype ttype = or_comma ? CPP_COMMA : CPP_EOF;
3465 return cp_parser_skip_to_closing_parenthesis_1 (parser, recovering,
3466 ttype, consume_paren);
3469 /* Consume tokens until we reach the end of the current statement.
3470 Normally, that will be just before consuming a `;'. However, if a
3471 non-nested `}' comes first, then we stop before consuming that. */
3473 static void
3474 cp_parser_skip_to_end_of_statement (cp_parser* parser)
3476 unsigned nesting_depth = 0;
3478 /* Unwind generic function template scope if necessary. */
3479 if (parser->fully_implicit_function_template_p)
3480 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3482 while (true)
3484 cp_token *token = cp_lexer_peek_token (parser->lexer);
3486 switch (token->type)
3488 case CPP_EOF:
3489 case CPP_PRAGMA_EOL:
3490 /* If we've run out of tokens, stop. */
3491 return;
3493 case CPP_SEMICOLON:
3494 /* If the next token is a `;', we have reached the end of the
3495 statement. */
3496 if (!nesting_depth)
3497 return;
3498 break;
3500 case CPP_CLOSE_BRACE:
3501 /* If this is a non-nested '}', stop before consuming it.
3502 That way, when confronted with something like:
3504 { 3 + }
3506 we stop before consuming the closing '}', even though we
3507 have not yet reached a `;'. */
3508 if (nesting_depth == 0)
3509 return;
3511 /* If it is the closing '}' for a block that we have
3512 scanned, stop -- but only after consuming the token.
3513 That way given:
3515 void f g () { ... }
3516 typedef int I;
3518 we will stop after the body of the erroneously declared
3519 function, but before consuming the following `typedef'
3520 declaration. */
3521 if (--nesting_depth == 0)
3523 cp_lexer_consume_token (parser->lexer);
3524 return;
3526 break;
3528 case CPP_OPEN_BRACE:
3529 ++nesting_depth;
3530 break;
3532 default:
3533 break;
3536 /* Consume the token. */
3537 cp_lexer_consume_token (parser->lexer);
3541 /* This function is called at the end of a statement or declaration.
3542 If the next token is a semicolon, it is consumed; otherwise, error
3543 recovery is attempted. */
3545 static void
3546 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3548 /* Look for the trailing `;'. */
3549 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3551 /* If there is additional (erroneous) input, skip to the end of
3552 the statement. */
3553 cp_parser_skip_to_end_of_statement (parser);
3554 /* If the next token is now a `;', consume it. */
3555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3556 cp_lexer_consume_token (parser->lexer);
3560 /* Skip tokens until we have consumed an entire block, or until we
3561 have consumed a non-nested `;'. */
3563 static void
3564 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3566 int nesting_depth = 0;
3568 /* Unwind generic function template scope if necessary. */
3569 if (parser->fully_implicit_function_template_p)
3570 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
3572 while (nesting_depth >= 0)
3574 cp_token *token = cp_lexer_peek_token (parser->lexer);
3576 switch (token->type)
3578 case CPP_EOF:
3579 case CPP_PRAGMA_EOL:
3580 /* If we've run out of tokens, stop. */
3581 return;
3583 case CPP_SEMICOLON:
3584 /* Stop if this is an unnested ';'. */
3585 if (!nesting_depth)
3586 nesting_depth = -1;
3587 break;
3589 case CPP_CLOSE_BRACE:
3590 /* Stop if this is an unnested '}', or closes the outermost
3591 nesting level. */
3592 nesting_depth--;
3593 if (nesting_depth < 0)
3594 return;
3595 if (!nesting_depth)
3596 nesting_depth = -1;
3597 break;
3599 case CPP_OPEN_BRACE:
3600 /* Nest. */
3601 nesting_depth++;
3602 break;
3604 default:
3605 break;
3608 /* Consume the token. */
3609 cp_lexer_consume_token (parser->lexer);
3613 /* Skip tokens until a non-nested closing curly brace is the next
3614 token, or there are no more tokens. Return true in the first case,
3615 false otherwise. */
3617 static bool
3618 cp_parser_skip_to_closing_brace (cp_parser *parser)
3620 unsigned nesting_depth = 0;
3622 while (true)
3624 cp_token *token = cp_lexer_peek_token (parser->lexer);
3626 switch (token->type)
3628 case CPP_EOF:
3629 case CPP_PRAGMA_EOL:
3630 /* If we've run out of tokens, stop. */
3631 return false;
3633 case CPP_CLOSE_BRACE:
3634 /* If the next token is a non-nested `}', then we have reached
3635 the end of the current block. */
3636 if (nesting_depth-- == 0)
3637 return true;
3638 break;
3640 case CPP_OPEN_BRACE:
3641 /* If it the next token is a `{', then we are entering a new
3642 block. Consume the entire block. */
3643 ++nesting_depth;
3644 break;
3646 default:
3647 break;
3650 /* Consume the token. */
3651 cp_lexer_consume_token (parser->lexer);
3655 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3656 parameter is the PRAGMA token, allowing us to purge the entire pragma
3657 sequence. */
3659 static void
3660 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3662 cp_token *token;
3664 parser->lexer->in_pragma = false;
3667 token = cp_lexer_consume_token (parser->lexer);
3668 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3670 /* Ensure that the pragma is not parsed again. */
3671 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3674 /* Require pragma end of line, resyncing with it as necessary. The
3675 arguments are as for cp_parser_skip_to_pragma_eol. */
3677 static void
3678 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3680 parser->lexer->in_pragma = false;
3681 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3682 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3685 /* This is a simple wrapper around make_typename_type. When the id is
3686 an unresolved identifier node, we can provide a superior diagnostic
3687 using cp_parser_diagnose_invalid_type_name. */
3689 static tree
3690 cp_parser_make_typename_type (cp_parser *parser, tree id,
3691 location_t id_location)
3693 tree result;
3694 if (identifier_p (id))
3696 result = make_typename_type (parser->scope, id, typename_type,
3697 /*complain=*/tf_none);
3698 if (result == error_mark_node)
3699 cp_parser_diagnose_invalid_type_name (parser, id, id_location);
3700 return result;
3702 return make_typename_type (parser->scope, id, typename_type, tf_error);
3705 /* This is a wrapper around the
3706 make_{pointer,ptrmem,reference}_declarator functions that decides
3707 which one to call based on the CODE and CLASS_TYPE arguments. The
3708 CODE argument should be one of the values returned by
3709 cp_parser_ptr_operator. ATTRIBUTES represent the attributes that
3710 appertain to the pointer or reference. */
3712 static cp_declarator *
3713 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3714 cp_cv_quals cv_qualifiers,
3715 cp_declarator *target,
3716 tree attributes)
3718 if (code == ERROR_MARK)
3719 return cp_error_declarator;
3721 if (code == INDIRECT_REF)
3722 if (class_type == NULL_TREE)
3723 return make_pointer_declarator (cv_qualifiers, target, attributes);
3724 else
3725 return make_ptrmem_declarator (cv_qualifiers, class_type,
3726 target, attributes);
3727 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3728 return make_reference_declarator (cv_qualifiers, target,
3729 false, attributes);
3730 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3731 return make_reference_declarator (cv_qualifiers, target,
3732 true, attributes);
3733 gcc_unreachable ();
3736 /* Create a new C++ parser. */
3738 static cp_parser *
3739 cp_parser_new (void)
3741 cp_parser *parser;
3742 cp_lexer *lexer;
3743 unsigned i;
3745 /* cp_lexer_new_main is called before doing GC allocation because
3746 cp_lexer_new_main might load a PCH file. */
3747 lexer = cp_lexer_new_main ();
3749 /* Initialize the binops_by_token so that we can get the tree
3750 directly from the token. */
3751 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3752 binops_by_token[binops[i].token_type] = binops[i];
3754 parser = ggc_cleared_alloc<cp_parser> ();
3755 parser->lexer = lexer;
3756 parser->context = cp_parser_context_new (NULL);
3758 /* For now, we always accept GNU extensions. */
3759 parser->allow_gnu_extensions_p = 1;
3761 /* The `>' token is a greater-than operator, not the end of a
3762 template-id. */
3763 parser->greater_than_is_operator_p = true;
3765 parser->default_arg_ok_p = true;
3767 /* We are not parsing a constant-expression. */
3768 parser->integral_constant_expression_p = false;
3769 parser->allow_non_integral_constant_expression_p = false;
3770 parser->non_integral_constant_expression_p = false;
3772 /* Local variable names are not forbidden. */
3773 parser->local_variables_forbidden_p = false;
3775 /* We are not processing an `extern "C"' declaration. */
3776 parser->in_unbraced_linkage_specification_p = false;
3778 /* We are not processing a declarator. */
3779 parser->in_declarator_p = false;
3781 /* We are not processing a template-argument-list. */
3782 parser->in_template_argument_list_p = false;
3784 /* We are not in an iteration statement. */
3785 parser->in_statement = 0;
3787 /* We are not in a switch statement. */
3788 parser->in_switch_statement_p = false;
3790 /* We are not parsing a type-id inside an expression. */
3791 parser->in_type_id_in_expr_p = false;
3793 /* Declarations aren't implicitly extern "C". */
3794 parser->implicit_extern_c = false;
3796 /* String literals should be translated to the execution character set. */
3797 parser->translate_strings_p = true;
3799 /* We are not parsing a function body. */
3800 parser->in_function_body = false;
3802 /* We can correct until told otherwise. */
3803 parser->colon_corrects_to_scope_p = true;
3805 /* The unparsed function queue is empty. */
3806 push_unparsed_function_queues (parser);
3808 /* There are no classes being defined. */
3809 parser->num_classes_being_defined = 0;
3811 /* No template parameters apply. */
3812 parser->num_template_parameter_lists = 0;
3814 /* Special parsing data structures. */
3815 parser->omp_declare_simd = NULL;
3816 parser->cilk_simd_fn_info = NULL;
3817 parser->oacc_routine = NULL;
3819 /* Not declaring an implicit function template. */
3820 parser->auto_is_implicit_function_template_parm_p = false;
3821 parser->fully_implicit_function_template_p = false;
3822 parser->implicit_template_parms = 0;
3823 parser->implicit_template_scope = 0;
3825 /* Allow constrained-type-specifiers. */
3826 parser->prevent_constrained_type_specifiers = 0;
3828 return parser;
3831 /* Create a cp_lexer structure which will emit the tokens in CACHE
3832 and push it onto the parser's lexer stack. This is used for delayed
3833 parsing of in-class method bodies and default arguments, and should
3834 not be confused with tentative parsing. */
3835 static void
3836 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3838 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3839 lexer->next = parser->lexer;
3840 parser->lexer = lexer;
3842 /* Move the current source position to that of the first token in the
3843 new lexer. */
3844 cp_lexer_set_source_position_from_token (lexer->next_token);
3847 /* Pop the top lexer off the parser stack. This is never used for the
3848 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3849 static void
3850 cp_parser_pop_lexer (cp_parser *parser)
3852 cp_lexer *lexer = parser->lexer;
3853 parser->lexer = lexer->next;
3854 cp_lexer_destroy (lexer);
3856 /* Put the current source position back where it was before this
3857 lexer was pushed. */
3858 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3861 /* Lexical conventions [gram.lex] */
3863 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3864 identifier. */
3866 static cp_expr
3867 cp_parser_identifier (cp_parser* parser)
3869 cp_token *token;
3871 /* Look for the identifier. */
3872 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3873 /* Return the value. */
3874 if (token)
3875 return cp_expr (token->u.value, token->location);
3876 else
3877 return error_mark_node;
3880 /* Parse a sequence of adjacent string constants. Returns a
3881 TREE_STRING representing the combined, nul-terminated string
3882 constant. If TRANSLATE is true, translate the string to the
3883 execution character set. If WIDE_OK is true, a wide string is
3884 invalid here.
3886 C++98 [lex.string] says that if a narrow string literal token is
3887 adjacent to a wide string literal token, the behavior is undefined.
3888 However, C99 6.4.5p4 says that this results in a wide string literal.
3889 We follow C99 here, for consistency with the C front end.
3891 This code is largely lifted from lex_string() in c-lex.c.
3893 FUTURE: ObjC++ will need to handle @-strings here. */
3894 static cp_expr
3895 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok,
3896 bool lookup_udlit = true)
3898 tree value;
3899 size_t count;
3900 struct obstack str_ob;
3901 cpp_string str, istr, *strs;
3902 cp_token *tok;
3903 enum cpp_ttype type, curr_type;
3904 int have_suffix_p = 0;
3905 tree string_tree;
3906 tree suffix_id = NULL_TREE;
3907 bool curr_tok_is_userdef_p = false;
3909 tok = cp_lexer_peek_token (parser->lexer);
3910 if (!cp_parser_is_string_literal (tok))
3912 cp_parser_error (parser, "expected string-literal");
3913 return error_mark_node;
3916 location_t loc = tok->location;
3918 if (cpp_userdef_string_p (tok->type))
3920 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3921 curr_type = cpp_userdef_string_remove_type (tok->type);
3922 curr_tok_is_userdef_p = true;
3924 else
3926 string_tree = tok->u.value;
3927 curr_type = tok->type;
3929 type = curr_type;
3931 /* Try to avoid the overhead of creating and destroying an obstack
3932 for the common case of just one string. */
3933 if (!cp_parser_is_string_literal
3934 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3936 cp_lexer_consume_token (parser->lexer);
3938 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3939 str.len = TREE_STRING_LENGTH (string_tree);
3940 count = 1;
3942 if (curr_tok_is_userdef_p)
3944 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3945 have_suffix_p = 1;
3946 curr_type = cpp_userdef_string_remove_type (tok->type);
3948 else
3949 curr_type = tok->type;
3951 strs = &str;
3953 else
3955 location_t last_tok_loc = tok->location;
3956 gcc_obstack_init (&str_ob);
3957 count = 0;
3961 cp_lexer_consume_token (parser->lexer);
3962 count++;
3963 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3964 str.len = TREE_STRING_LENGTH (string_tree);
3966 if (curr_tok_is_userdef_p)
3968 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3969 if (have_suffix_p == 0)
3971 suffix_id = curr_suffix_id;
3972 have_suffix_p = 1;
3974 else if (have_suffix_p == 1
3975 && curr_suffix_id != suffix_id)
3977 error ("inconsistent user-defined literal suffixes"
3978 " %qD and %qD in string literal",
3979 suffix_id, curr_suffix_id);
3980 have_suffix_p = -1;
3982 curr_type = cpp_userdef_string_remove_type (tok->type);
3984 else
3985 curr_type = tok->type;
3987 if (type != curr_type)
3989 if (type == CPP_STRING)
3990 type = curr_type;
3991 else if (curr_type != CPP_STRING)
3993 rich_location rich_loc (line_table, tok->location);
3994 rich_loc.add_range (last_tok_loc, false);
3995 error_at_rich_loc (&rich_loc,
3996 "unsupported non-standard concatenation "
3997 "of string literals");
4001 obstack_grow (&str_ob, &str, sizeof (cpp_string));
4003 last_tok_loc = tok->location;
4005 tok = cp_lexer_peek_token (parser->lexer);
4006 if (cpp_userdef_string_p (tok->type))
4008 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
4009 curr_type = cpp_userdef_string_remove_type (tok->type);
4010 curr_tok_is_userdef_p = true;
4012 else
4014 string_tree = tok->u.value;
4015 curr_type = tok->type;
4016 curr_tok_is_userdef_p = false;
4019 while (cp_parser_is_string_literal (tok));
4021 /* A string literal built by concatenation has its caret=start at
4022 the start of the initial string, and its finish at the finish of
4023 the final string literal. */
4024 loc = make_location (loc, loc, get_finish (last_tok_loc));
4026 strs = (cpp_string *) obstack_finish (&str_ob);
4029 if (type != CPP_STRING && !wide_ok)
4031 cp_parser_error (parser, "a wide string is invalid in this context");
4032 type = CPP_STRING;
4035 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
4036 (parse_in, strs, count, &istr, type))
4038 value = build_string (istr.len, (const char *)istr.text);
4039 free (CONST_CAST (unsigned char *, istr.text));
4041 switch (type)
4043 default:
4044 case CPP_STRING:
4045 case CPP_UTF8STRING:
4046 TREE_TYPE (value) = char_array_type_node;
4047 break;
4048 case CPP_STRING16:
4049 TREE_TYPE (value) = char16_array_type_node;
4050 break;
4051 case CPP_STRING32:
4052 TREE_TYPE (value) = char32_array_type_node;
4053 break;
4054 case CPP_WSTRING:
4055 TREE_TYPE (value) = wchar_array_type_node;
4056 break;
4059 value = fix_string_type (value);
4061 if (have_suffix_p)
4063 tree literal = build_userdef_literal (suffix_id, value,
4064 OT_NONE, NULL_TREE);
4065 if (lookup_udlit)
4066 value = cp_parser_userdef_string_literal (literal);
4067 else
4068 value = literal;
4071 else
4072 /* cpp_interpret_string has issued an error. */
4073 value = error_mark_node;
4075 if (count > 1)
4076 obstack_free (&str_ob, 0);
4078 return cp_expr (value, loc);
4081 /* Look up a literal operator with the name and the exact arguments. */
4083 static tree
4084 lookup_literal_operator (tree name, vec<tree, va_gc> *args)
4086 tree decl;
4087 decl = lookup_name (name);
4088 if (!decl || !is_overloaded_fn (decl))
4089 return error_mark_node;
4091 for (lkp_iterator iter (decl); iter; ++iter)
4093 unsigned int ix;
4094 bool found = true;
4095 tree fn = *iter;
4096 tree parmtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
4097 if (parmtypes != NULL_TREE)
4099 for (ix = 0; ix < vec_safe_length (args) && parmtypes != NULL_TREE;
4100 ++ix, parmtypes = TREE_CHAIN (parmtypes))
4102 tree tparm = TREE_VALUE (parmtypes);
4103 tree targ = TREE_TYPE ((*args)[ix]);
4104 bool ptr = TYPE_PTR_P (tparm);
4105 bool arr = TREE_CODE (targ) == ARRAY_TYPE;
4106 if ((ptr || arr || !same_type_p (tparm, targ))
4107 && (!ptr || !arr
4108 || !same_type_p (TREE_TYPE (tparm),
4109 TREE_TYPE (targ))))
4110 found = false;
4112 if (found
4113 && ix == vec_safe_length (args)
4114 /* May be this should be sufficient_parms_p instead,
4115 depending on how exactly should user-defined literals
4116 work in presence of default arguments on the literal
4117 operator parameters. */
4118 && parmtypes == void_list_node)
4119 return decl;
4123 return error_mark_node;
4126 /* Parse a user-defined char constant. Returns a call to a user-defined
4127 literal operator taking the character as an argument. */
4129 static cp_expr
4130 cp_parser_userdef_char_literal (cp_parser *parser)
4132 cp_token *token = cp_lexer_consume_token (parser->lexer);
4133 tree literal = token->u.value;
4134 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4135 tree value = USERDEF_LITERAL_VALUE (literal);
4136 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4137 tree decl, result;
4139 /* Build up a call to the user-defined operator */
4140 /* Lookup the name we got back from the id-expression. */
4141 vec<tree, va_gc> *args = make_tree_vector ();
4142 vec_safe_push (args, value);
4143 decl = lookup_literal_operator (name, args);
4144 if (!decl || decl == error_mark_node)
4146 error ("unable to find character literal operator %qD with %qT argument",
4147 name, TREE_TYPE (value));
4148 release_tree_vector (args);
4149 return error_mark_node;
4151 result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
4152 release_tree_vector (args);
4153 return result;
4156 /* A subroutine of cp_parser_userdef_numeric_literal to
4157 create a char... template parameter pack from a string node. */
4159 static tree
4160 make_char_string_pack (tree value)
4162 tree charvec;
4163 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4164 const char *str = TREE_STRING_POINTER (value);
4165 int i, len = TREE_STRING_LENGTH (value) - 1;
4166 tree argvec = make_tree_vec (1);
4168 /* Fill in CHARVEC with all of the parameters. */
4169 charvec = make_tree_vec (len);
4170 for (i = 0; i < len; ++i)
4171 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
4173 /* Build the argument packs. */
4174 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4176 TREE_VEC_ELT (argvec, 0) = argpack;
4178 return argvec;
4181 /* A subroutine of cp_parser_userdef_numeric_literal to
4182 create a char... template parameter pack from a string node. */
4184 static tree
4185 make_string_pack (tree value)
4187 tree charvec;
4188 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
4189 const unsigned char *str
4190 = (const unsigned char *) TREE_STRING_POINTER (value);
4191 int sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value))));
4192 int len = TREE_STRING_LENGTH (value) / sz - 1;
4193 tree argvec = make_tree_vec (2);
4195 tree str_char_type_node = TREE_TYPE (TREE_TYPE (value));
4196 str_char_type_node = TYPE_MAIN_VARIANT (str_char_type_node);
4198 /* First template parm is character type. */
4199 TREE_VEC_ELT (argvec, 0) = str_char_type_node;
4201 /* Fill in CHARVEC with all of the parameters. */
4202 charvec = make_tree_vec (len);
4203 for (int i = 0; i < len; ++i)
4204 TREE_VEC_ELT (charvec, i)
4205 = double_int_to_tree (str_char_type_node,
4206 double_int::from_buffer (str + i * sz, sz));
4208 /* Build the argument packs. */
4209 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
4211 TREE_VEC_ELT (argvec, 1) = argpack;
4213 return argvec;
4216 /* Parse a user-defined numeric constant. returns a call to a user-defined
4217 literal operator. */
4219 static cp_expr
4220 cp_parser_userdef_numeric_literal (cp_parser *parser)
4222 cp_token *token = cp_lexer_consume_token (parser->lexer);
4223 tree literal = token->u.value;
4224 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4225 tree value = USERDEF_LITERAL_VALUE (literal);
4226 int overflow = USERDEF_LITERAL_OVERFLOW (literal);
4227 tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
4228 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4229 tree decl, result;
4230 vec<tree, va_gc> *args;
4232 /* Look for a literal operator taking the exact type of numeric argument
4233 as the literal value. */
4234 args = make_tree_vector ();
4235 vec_safe_push (args, value);
4236 decl = lookup_literal_operator (name, args);
4237 if (decl && decl != error_mark_node)
4239 result = finish_call_expr (decl, &args, false, true,
4240 tf_warning_or_error);
4242 if (TREE_CODE (TREE_TYPE (value)) == INTEGER_TYPE && overflow > 0)
4244 warning_at (token->location, OPT_Woverflow,
4245 "integer literal exceeds range of %qT type",
4246 long_long_unsigned_type_node);
4248 else
4250 if (overflow > 0)
4251 warning_at (token->location, OPT_Woverflow,
4252 "floating literal exceeds range of %qT type",
4253 long_double_type_node);
4254 else if (overflow < 0)
4255 warning_at (token->location, OPT_Woverflow,
4256 "floating literal truncated to zero");
4259 release_tree_vector (args);
4260 return result;
4262 release_tree_vector (args);
4264 /* If the numeric argument didn't work, look for a raw literal
4265 operator taking a const char* argument consisting of the number
4266 in string format. */
4267 args = make_tree_vector ();
4268 vec_safe_push (args, num_string);
4269 decl = lookup_literal_operator (name, args);
4270 if (decl && decl != error_mark_node)
4272 result = finish_call_expr (decl, &args, false, true,
4273 tf_warning_or_error);
4274 release_tree_vector (args);
4275 return result;
4277 release_tree_vector (args);
4279 /* If the raw literal didn't work, look for a non-type template
4280 function with parameter pack char.... Call the function with
4281 template parameter characters representing the number. */
4282 args = make_tree_vector ();
4283 decl = lookup_literal_operator (name, args);
4284 if (decl && decl != error_mark_node)
4286 tree tmpl_args = make_char_string_pack (num_string);
4287 decl = lookup_template_function (decl, tmpl_args);
4288 result = finish_call_expr (decl, &args, false, true,
4289 tf_warning_or_error);
4290 release_tree_vector (args);
4291 return result;
4294 release_tree_vector (args);
4296 error ("unable to find numeric literal operator %qD", name);
4297 if (!cpp_get_options (parse_in)->ext_numeric_literals)
4298 inform (token->location, "use -std=gnu++11 or -fext-numeric-literals "
4299 "to enable more built-in suffixes");
4300 return error_mark_node;
4303 /* Parse a user-defined string constant. Returns a call to a user-defined
4304 literal operator taking a character pointer and the length of the string
4305 as arguments. */
4307 static tree
4308 cp_parser_userdef_string_literal (tree literal)
4310 tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
4311 tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
4312 tree value = USERDEF_LITERAL_VALUE (literal);
4313 int len = TREE_STRING_LENGTH (value)
4314 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
4315 tree decl, result;
4316 vec<tree, va_gc> *args;
4318 /* Build up a call to the user-defined operator. */
4319 /* Lookup the name we got back from the id-expression. */
4320 args = make_tree_vector ();
4321 vec_safe_push (args, value);
4322 vec_safe_push (args, build_int_cst (size_type_node, len));
4323 decl = lookup_literal_operator (name, args);
4325 if (decl && decl != error_mark_node)
4327 result = finish_call_expr (decl, &args, false, true,
4328 tf_warning_or_error);
4329 release_tree_vector (args);
4330 return result;
4332 release_tree_vector (args);
4334 /* Look for a template function with typename parameter CharT
4335 and parameter pack CharT... Call the function with
4336 template parameter characters representing the string. */
4337 args = make_tree_vector ();
4338 decl = lookup_literal_operator (name, args);
4339 if (decl && decl != error_mark_node)
4341 tree tmpl_args = make_string_pack (value);
4342 decl = lookup_template_function (decl, tmpl_args);
4343 result = finish_call_expr (decl, &args, false, true,
4344 tf_warning_or_error);
4345 release_tree_vector (args);
4346 return result;
4348 release_tree_vector (args);
4350 error ("unable to find string literal operator %qD with %qT, %qT arguments",
4351 name, TREE_TYPE (value), size_type_node);
4352 return error_mark_node;
4356 /* Basic concepts [gram.basic] */
4358 /* Parse a translation-unit.
4360 translation-unit:
4361 declaration-seq [opt]
4363 Returns TRUE if all went well. */
4365 static bool
4366 cp_parser_translation_unit (cp_parser* parser)
4368 /* The address of the first non-permanent object on the declarator
4369 obstack. */
4370 static void *declarator_obstack_base;
4372 bool success;
4374 /* Create the declarator obstack, if necessary. */
4375 if (!cp_error_declarator)
4377 gcc_obstack_init (&declarator_obstack);
4378 /* Create the error declarator. */
4379 cp_error_declarator = make_declarator (cdk_error);
4380 /* Create the empty parameter list. */
4381 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
4382 /* Remember where the base of the declarator obstack lies. */
4383 declarator_obstack_base = obstack_next_free (&declarator_obstack);
4386 cp_parser_declaration_seq_opt (parser);
4388 /* If there are no tokens left then all went well. */
4389 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
4391 /* Get rid of the token array; we don't need it any more. */
4392 cp_lexer_destroy (parser->lexer);
4393 parser->lexer = NULL;
4395 /* This file might have been a context that's implicitly extern
4396 "C". If so, pop the lang context. (Only relevant for PCH.) */
4397 if (parser->implicit_extern_c)
4399 pop_lang_context ();
4400 parser->implicit_extern_c = false;
4403 /* Finish up. */
4404 finish_translation_unit ();
4406 success = true;
4408 else
4410 cp_parser_error (parser, "expected declaration");
4411 success = false;
4414 /* Make sure the declarator obstack was fully cleaned up. */
4415 gcc_assert (obstack_next_free (&declarator_obstack)
4416 == declarator_obstack_base);
4418 /* All went well. */
4419 return success;
4422 /* Return the appropriate tsubst flags for parsing, possibly in N3276
4423 decltype context. */
4425 static inline tsubst_flags_t
4426 complain_flags (bool decltype_p)
4428 tsubst_flags_t complain = tf_warning_or_error;
4429 if (decltype_p)
4430 complain |= tf_decltype;
4431 return complain;
4434 /* We're about to parse a collection of statements. If we're currently
4435 parsing tentatively, set up a firewall so that any nested
4436 cp_parser_commit_to_tentative_parse won't affect the current context. */
4438 static cp_token_position
4439 cp_parser_start_tentative_firewall (cp_parser *parser)
4441 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4442 return 0;
4444 cp_parser_parse_tentatively (parser);
4445 cp_parser_commit_to_topmost_tentative_parse (parser);
4446 return cp_lexer_token_position (parser->lexer, false);
4449 /* We've finished parsing the collection of statements. Wrap up the
4450 firewall and replace the relevant tokens with the parsed form. */
4452 static void
4453 cp_parser_end_tentative_firewall (cp_parser *parser, cp_token_position start,
4454 tree expr)
4456 if (!start)
4457 return;
4459 /* Finish the firewall level. */
4460 cp_parser_parse_definitely (parser);
4461 /* And remember the result of the parse for when we try again. */
4462 cp_token *token = cp_lexer_token_at (parser->lexer, start);
4463 token->type = CPP_PREPARSED_EXPR;
4464 token->u.value = expr;
4465 token->keyword = RID_MAX;
4466 cp_lexer_purge_tokens_after (parser->lexer, start);
4469 /* Like the above functions, but let the user modify the tokens. Used by
4470 CPP_DECLTYPE and CPP_TEMPLATE_ID, where we are saving the side-effects for
4471 later parses, so it makes sense to localize the effects of
4472 cp_parser_commit_to_tentative_parse. */
4474 struct tentative_firewall
4476 cp_parser *parser;
4477 bool set;
4479 tentative_firewall (cp_parser *p): parser(p)
4481 /* If we're currently parsing tentatively, start a committed level as a
4482 firewall and then an inner tentative parse. */
4483 if ((set = cp_parser_uncommitted_to_tentative_parse_p (parser)))
4485 cp_parser_parse_tentatively (parser);
4486 cp_parser_commit_to_topmost_tentative_parse (parser);
4487 cp_parser_parse_tentatively (parser);
4491 ~tentative_firewall()
4493 if (set)
4495 /* Finish the inner tentative parse and the firewall, propagating any
4496 uncommitted error state to the outer tentative parse. */
4497 bool err = cp_parser_error_occurred (parser);
4498 cp_parser_parse_definitely (parser);
4499 cp_parser_parse_definitely (parser);
4500 if (err)
4501 cp_parser_simulate_error (parser);
4506 /* Parse a GNU statement-expression, i.e. ({ stmts }), except for the
4507 enclosing parentheses. */
4509 static cp_expr
4510 cp_parser_statement_expr (cp_parser *parser)
4512 cp_token_position start = cp_parser_start_tentative_firewall (parser);
4514 /* Consume the '('. */
4515 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
4516 cp_lexer_consume_token (parser->lexer);
4517 /* Start the statement-expression. */
4518 tree expr = begin_stmt_expr ();
4519 /* Parse the compound-statement. */
4520 cp_parser_compound_statement (parser, expr, BCS_NORMAL, false);
4521 /* Finish up. */
4522 expr = finish_stmt_expr (expr, false);
4523 /* Consume the ')'. */
4524 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
4525 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4526 cp_parser_skip_to_end_of_statement (parser);
4528 cp_parser_end_tentative_firewall (parser, start, expr);
4529 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
4530 return cp_expr (expr, combined_loc);
4533 /* Expressions [gram.expr] */
4535 /* Parse a fold-operator.
4537 fold-operator:
4538 - * / % ^ & | = < > << >>
4539 = -= *= /= %= ^= &= |= <<= >>=
4540 == != <= >= && || , .* ->*
4542 This returns the tree code corresponding to the matched operator
4543 as an int. When the current token matches a compound assignment
4544 opertor, the resulting tree code is the negative value of the
4545 non-assignment operator. */
4547 static int
4548 cp_parser_fold_operator (cp_token *token)
4550 switch (token->type)
4552 case CPP_PLUS: return PLUS_EXPR;
4553 case CPP_MINUS: return MINUS_EXPR;
4554 case CPP_MULT: return MULT_EXPR;
4555 case CPP_DIV: return TRUNC_DIV_EXPR;
4556 case CPP_MOD: return TRUNC_MOD_EXPR;
4557 case CPP_XOR: return BIT_XOR_EXPR;
4558 case CPP_AND: return BIT_AND_EXPR;
4559 case CPP_OR: return BIT_IOR_EXPR;
4560 case CPP_LSHIFT: return LSHIFT_EXPR;
4561 case CPP_RSHIFT: return RSHIFT_EXPR;
4563 case CPP_EQ: return -NOP_EXPR;
4564 case CPP_PLUS_EQ: return -PLUS_EXPR;
4565 case CPP_MINUS_EQ: return -MINUS_EXPR;
4566 case CPP_MULT_EQ: return -MULT_EXPR;
4567 case CPP_DIV_EQ: return -TRUNC_DIV_EXPR;
4568 case CPP_MOD_EQ: return -TRUNC_MOD_EXPR;
4569 case CPP_XOR_EQ: return -BIT_XOR_EXPR;
4570 case CPP_AND_EQ: return -BIT_AND_EXPR;
4571 case CPP_OR_EQ: return -BIT_IOR_EXPR;
4572 case CPP_LSHIFT_EQ: return -LSHIFT_EXPR;
4573 case CPP_RSHIFT_EQ: return -RSHIFT_EXPR;
4575 case CPP_EQ_EQ: return EQ_EXPR;
4576 case CPP_NOT_EQ: return NE_EXPR;
4577 case CPP_LESS: return LT_EXPR;
4578 case CPP_GREATER: return GT_EXPR;
4579 case CPP_LESS_EQ: return LE_EXPR;
4580 case CPP_GREATER_EQ: return GE_EXPR;
4582 case CPP_AND_AND: return TRUTH_ANDIF_EXPR;
4583 case CPP_OR_OR: return TRUTH_ORIF_EXPR;
4585 case CPP_COMMA: return COMPOUND_EXPR;
4587 case CPP_DOT_STAR: return DOTSTAR_EXPR;
4588 case CPP_DEREF_STAR: return MEMBER_REF;
4590 default: return ERROR_MARK;
4594 /* Returns true if CODE indicates a binary expression, which is not allowed in
4595 the LHS of a fold-expression. More codes will need to be added to use this
4596 function in other contexts. */
4598 static bool
4599 is_binary_op (tree_code code)
4601 switch (code)
4603 case PLUS_EXPR:
4604 case POINTER_PLUS_EXPR:
4605 case MINUS_EXPR:
4606 case MULT_EXPR:
4607 case TRUNC_DIV_EXPR:
4608 case TRUNC_MOD_EXPR:
4609 case BIT_XOR_EXPR:
4610 case BIT_AND_EXPR:
4611 case BIT_IOR_EXPR:
4612 case LSHIFT_EXPR:
4613 case RSHIFT_EXPR:
4615 case MODOP_EXPR:
4617 case EQ_EXPR:
4618 case NE_EXPR:
4619 case LE_EXPR:
4620 case GE_EXPR:
4621 case LT_EXPR:
4622 case GT_EXPR:
4624 case TRUTH_ANDIF_EXPR:
4625 case TRUTH_ORIF_EXPR:
4627 case COMPOUND_EXPR:
4629 case DOTSTAR_EXPR:
4630 case MEMBER_REF:
4631 return true;
4633 default:
4634 return false;
4638 /* If the next token is a suitable fold operator, consume it and return as
4639 the function above. */
4641 static int
4642 cp_parser_fold_operator (cp_parser *parser)
4644 cp_token* token = cp_lexer_peek_token (parser->lexer);
4645 int code = cp_parser_fold_operator (token);
4646 if (code != ERROR_MARK)
4647 cp_lexer_consume_token (parser->lexer);
4648 return code;
4651 /* Parse a fold-expression.
4653 fold-expression:
4654 ( ... folding-operator cast-expression)
4655 ( cast-expression folding-operator ... )
4656 ( cast-expression folding operator ... folding-operator cast-expression)
4658 Note that the '(' and ')' are matched in primary expression. */
4660 static cp_expr
4661 cp_parser_fold_expression (cp_parser *parser, tree expr1)
4663 cp_id_kind pidk;
4665 // Left fold.
4666 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4668 cp_lexer_consume_token (parser->lexer);
4669 int op = cp_parser_fold_operator (parser);
4670 if (op == ERROR_MARK)
4672 cp_parser_error (parser, "expected binary operator");
4673 return error_mark_node;
4676 tree expr = cp_parser_cast_expression (parser, false, false,
4677 false, &pidk);
4678 if (expr == error_mark_node)
4679 return error_mark_node;
4680 return finish_left_unary_fold_expr (expr, op);
4683 const cp_token* token = cp_lexer_peek_token (parser->lexer);
4684 int op = cp_parser_fold_operator (parser);
4685 if (op == ERROR_MARK)
4687 cp_parser_error (parser, "expected binary operator");
4688 return error_mark_node;
4691 if (cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS))
4693 cp_parser_error (parser, "expected ...");
4694 return error_mark_node;
4696 cp_lexer_consume_token (parser->lexer);
4698 /* The operands of a fold-expression are cast-expressions, so binary or
4699 conditional expressions are not allowed. We check this here to avoid
4700 tentative parsing. */
4701 if (EXPR_P (expr1) && TREE_NO_WARNING (expr1))
4702 /* OK, the expression was parenthesized. */;
4703 else if (is_binary_op (TREE_CODE (expr1)))
4704 error_at (location_of (expr1),
4705 "binary expression in operand of fold-expression");
4706 else if (TREE_CODE (expr1) == COND_EXPR)
4707 error_at (location_of (expr1),
4708 "conditional expression in operand of fold-expression");
4710 // Right fold.
4711 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4712 return finish_right_unary_fold_expr (expr1, op);
4714 if (cp_lexer_next_token_is_not (parser->lexer, token->type))
4716 cp_parser_error (parser, "mismatched operator in fold-expression");
4717 return error_mark_node;
4719 cp_lexer_consume_token (parser->lexer);
4721 // Binary left or right fold.
4722 tree expr2 = cp_parser_cast_expression (parser, false, false, false, &pidk);
4723 if (expr2 == error_mark_node)
4724 return error_mark_node;
4725 return finish_binary_fold_expr (expr1, expr2, op);
4728 /* Parse a primary-expression.
4730 primary-expression:
4731 literal
4732 this
4733 ( expression )
4734 id-expression
4735 lambda-expression (C++11)
4737 GNU Extensions:
4739 primary-expression:
4740 ( compound-statement )
4741 __builtin_va_arg ( assignment-expression , type-id )
4742 __builtin_offsetof ( type-id , offsetof-expression )
4744 C++ Extensions:
4745 __has_nothrow_assign ( type-id )
4746 __has_nothrow_constructor ( type-id )
4747 __has_nothrow_copy ( type-id )
4748 __has_trivial_assign ( type-id )
4749 __has_trivial_constructor ( type-id )
4750 __has_trivial_copy ( type-id )
4751 __has_trivial_destructor ( type-id )
4752 __has_virtual_destructor ( type-id )
4753 __is_abstract ( type-id )
4754 __is_base_of ( type-id , type-id )
4755 __is_class ( type-id )
4756 __is_empty ( type-id )
4757 __is_enum ( type-id )
4758 __is_final ( type-id )
4759 __is_literal_type ( type-id )
4760 __is_pod ( type-id )
4761 __is_polymorphic ( type-id )
4762 __is_std_layout ( type-id )
4763 __is_trivial ( type-id )
4764 __is_union ( type-id )
4766 Objective-C++ Extension:
4768 primary-expression:
4769 objc-expression
4771 literal:
4772 __null
4774 ADDRESS_P is true iff this expression was immediately preceded by
4775 "&" and therefore might denote a pointer-to-member. CAST_P is true
4776 iff this expression is the target of a cast. TEMPLATE_ARG_P is
4777 true iff this expression is a template argument.
4779 Returns a representation of the expression. Upon return, *IDK
4780 indicates what kind of id-expression (if any) was present. */
4782 static cp_expr
4783 cp_parser_primary_expression (cp_parser *parser,
4784 bool address_p,
4785 bool cast_p,
4786 bool template_arg_p,
4787 bool decltype_p,
4788 cp_id_kind *idk)
4790 cp_token *token = NULL;
4792 /* Assume the primary expression is not an id-expression. */
4793 *idk = CP_ID_KIND_NONE;
4795 /* Peek at the next token. */
4796 token = cp_lexer_peek_token (parser->lexer);
4797 switch ((int) token->type)
4799 /* literal:
4800 integer-literal
4801 character-literal
4802 floating-literal
4803 string-literal
4804 boolean-literal
4805 pointer-literal
4806 user-defined-literal */
4807 case CPP_CHAR:
4808 case CPP_CHAR16:
4809 case CPP_CHAR32:
4810 case CPP_WCHAR:
4811 case CPP_UTF8CHAR:
4812 case CPP_NUMBER:
4813 case CPP_PREPARSED_EXPR:
4814 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
4815 return cp_parser_userdef_numeric_literal (parser);
4816 token = cp_lexer_consume_token (parser->lexer);
4817 if (TREE_CODE (token->u.value) == FIXED_CST)
4819 error_at (token->location,
4820 "fixed-point types not supported in C++");
4821 return error_mark_node;
4823 /* Floating-point literals are only allowed in an integral
4824 constant expression if they are cast to an integral or
4825 enumeration type. */
4826 if (TREE_CODE (token->u.value) == REAL_CST
4827 && parser->integral_constant_expression_p
4828 && pedantic)
4830 /* CAST_P will be set even in invalid code like "int(2.7 +
4831 ...)". Therefore, we have to check that the next token
4832 is sure to end the cast. */
4833 if (cast_p)
4835 cp_token *next_token;
4837 next_token = cp_lexer_peek_token (parser->lexer);
4838 if (/* The comma at the end of an
4839 enumerator-definition. */
4840 next_token->type != CPP_COMMA
4841 /* The curly brace at the end of an enum-specifier. */
4842 && next_token->type != CPP_CLOSE_BRACE
4843 /* The end of a statement. */
4844 && next_token->type != CPP_SEMICOLON
4845 /* The end of the cast-expression. */
4846 && next_token->type != CPP_CLOSE_PAREN
4847 /* The end of an array bound. */
4848 && next_token->type != CPP_CLOSE_SQUARE
4849 /* The closing ">" in a template-argument-list. */
4850 && (next_token->type != CPP_GREATER
4851 || parser->greater_than_is_operator_p)
4852 /* C++0x only: A ">>" treated like two ">" tokens,
4853 in a template-argument-list. */
4854 && (next_token->type != CPP_RSHIFT
4855 || (cxx_dialect == cxx98)
4856 || parser->greater_than_is_operator_p))
4857 cast_p = false;
4860 /* If we are within a cast, then the constraint that the
4861 cast is to an integral or enumeration type will be
4862 checked at that point. If we are not within a cast, then
4863 this code is invalid. */
4864 if (!cast_p)
4865 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
4867 return cp_expr (token->u.value, token->location);
4869 case CPP_CHAR_USERDEF:
4870 case CPP_CHAR16_USERDEF:
4871 case CPP_CHAR32_USERDEF:
4872 case CPP_WCHAR_USERDEF:
4873 case CPP_UTF8CHAR_USERDEF:
4874 return cp_parser_userdef_char_literal (parser);
4876 case CPP_STRING:
4877 case CPP_STRING16:
4878 case CPP_STRING32:
4879 case CPP_WSTRING:
4880 case CPP_UTF8STRING:
4881 case CPP_STRING_USERDEF:
4882 case CPP_STRING16_USERDEF:
4883 case CPP_STRING32_USERDEF:
4884 case CPP_WSTRING_USERDEF:
4885 case CPP_UTF8STRING_USERDEF:
4886 /* ??? Should wide strings be allowed when parser->translate_strings_p
4887 is false (i.e. in attributes)? If not, we can kill the third
4888 argument to cp_parser_string_literal. */
4889 return cp_parser_string_literal (parser,
4890 parser->translate_strings_p,
4891 true);
4893 case CPP_OPEN_PAREN:
4894 /* If we see `( { ' then we are looking at the beginning of
4895 a GNU statement-expression. */
4896 if (cp_parser_allow_gnu_extensions_p (parser)
4897 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_BRACE))
4899 /* Statement-expressions are not allowed by the standard. */
4900 pedwarn (token->location, OPT_Wpedantic,
4901 "ISO C++ forbids braced-groups within expressions");
4903 /* And they're not allowed outside of a function-body; you
4904 cannot, for example, write:
4906 int i = ({ int j = 3; j + 1; });
4908 at class or namespace scope. */
4909 if (!parser->in_function_body
4910 || parser->in_template_argument_list_p)
4912 error_at (token->location,
4913 "statement-expressions are not allowed outside "
4914 "functions nor in template-argument lists");
4915 cp_parser_skip_to_end_of_block_or_statement (parser);
4916 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
4917 cp_lexer_consume_token (parser->lexer);
4918 return error_mark_node;
4920 else
4921 return cp_parser_statement_expr (parser);
4923 /* Otherwise it's a normal parenthesized expression. */
4925 cp_expr expr;
4926 bool saved_greater_than_is_operator_p;
4928 location_t open_paren_loc = token->location;
4930 /* Consume the `('. */
4931 cp_lexer_consume_token (parser->lexer);
4932 /* Within a parenthesized expression, a `>' token is always
4933 the greater-than operator. */
4934 saved_greater_than_is_operator_p
4935 = parser->greater_than_is_operator_p;
4936 parser->greater_than_is_operator_p = true;
4938 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4939 /* Left fold expression. */
4940 expr = NULL_TREE;
4941 else
4942 /* Parse the parenthesized expression. */
4943 expr = cp_parser_expression (parser, idk, cast_p, decltype_p);
4945 token = cp_lexer_peek_token (parser->lexer);
4946 if (token->type == CPP_ELLIPSIS || cp_parser_fold_operator (token))
4948 expr = cp_parser_fold_expression (parser, expr);
4949 if (expr != error_mark_node
4950 && cxx_dialect < cxx1z
4951 && !in_system_header_at (input_location))
4952 pedwarn (input_location, 0, "fold-expressions only available "
4953 "with -std=c++1z or -std=gnu++1z");
4955 else
4956 /* Let the front end know that this expression was
4957 enclosed in parentheses. This matters in case, for
4958 example, the expression is of the form `A::B', since
4959 `&A::B' might be a pointer-to-member, but `&(A::B)' is
4960 not. */
4961 expr = finish_parenthesized_expr (expr);
4963 /* DR 705: Wrapping an unqualified name in parentheses
4964 suppresses arg-dependent lookup. We want to pass back
4965 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4966 (c++/37862), but none of the others. */
4967 if (*idk != CP_ID_KIND_QUALIFIED)
4968 *idk = CP_ID_KIND_NONE;
4970 /* The `>' token might be the end of a template-id or
4971 template-parameter-list now. */
4972 parser->greater_than_is_operator_p
4973 = saved_greater_than_is_operator_p;
4975 /* Consume the `)'. */
4976 token = cp_lexer_peek_token (parser->lexer);
4977 location_t close_paren_loc = token->location;
4978 expr.set_range (open_paren_loc, close_paren_loc);
4979 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN)
4980 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4981 cp_parser_skip_to_end_of_statement (parser);
4983 return expr;
4986 case CPP_OPEN_SQUARE:
4988 if (c_dialect_objc ())
4990 /* We might have an Objective-C++ message. */
4991 cp_parser_parse_tentatively (parser);
4992 tree msg = cp_parser_objc_message_expression (parser);
4993 /* If that works out, we're done ... */
4994 if (cp_parser_parse_definitely (parser))
4995 return msg;
4996 /* ... else, fall though to see if it's a lambda. */
4998 cp_expr lam = cp_parser_lambda_expression (parser);
4999 /* Don't warn about a failed tentative parse. */
5000 if (cp_parser_error_occurred (parser))
5001 return error_mark_node;
5002 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
5003 return lam;
5006 case CPP_OBJC_STRING:
5007 if (c_dialect_objc ())
5008 /* We have an Objective-C++ string literal. */
5009 return cp_parser_objc_expression (parser);
5010 cp_parser_error (parser, "expected primary-expression");
5011 return error_mark_node;
5013 case CPP_KEYWORD:
5014 switch (token->keyword)
5016 /* These two are the boolean literals. */
5017 case RID_TRUE:
5018 cp_lexer_consume_token (parser->lexer);
5019 return cp_expr (boolean_true_node, token->location);
5020 case RID_FALSE:
5021 cp_lexer_consume_token (parser->lexer);
5022 return cp_expr (boolean_false_node, token->location);
5024 /* The `__null' literal. */
5025 case RID_NULL:
5026 cp_lexer_consume_token (parser->lexer);
5027 return cp_expr (null_node, token->location);
5029 /* The `nullptr' literal. */
5030 case RID_NULLPTR:
5031 cp_lexer_consume_token (parser->lexer);
5032 return cp_expr (nullptr_node, token->location);
5034 /* Recognize the `this' keyword. */
5035 case RID_THIS:
5036 cp_lexer_consume_token (parser->lexer);
5037 if (parser->local_variables_forbidden_p)
5039 error_at (token->location,
5040 "%<this%> may not be used in this context");
5041 return error_mark_node;
5043 /* Pointers cannot appear in constant-expressions. */
5044 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
5045 return error_mark_node;
5046 return cp_expr (finish_this_expr (), token->location);
5048 /* The `operator' keyword can be the beginning of an
5049 id-expression. */
5050 case RID_OPERATOR:
5051 goto id_expression;
5053 case RID_FUNCTION_NAME:
5054 case RID_PRETTY_FUNCTION_NAME:
5055 case RID_C99_FUNCTION_NAME:
5057 non_integral_constant name;
5059 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
5060 __func__ are the names of variables -- but they are
5061 treated specially. Therefore, they are handled here,
5062 rather than relying on the generic id-expression logic
5063 below. Grammatically, these names are id-expressions.
5065 Consume the token. */
5066 token = cp_lexer_consume_token (parser->lexer);
5068 switch (token->keyword)
5070 case RID_FUNCTION_NAME:
5071 name = NIC_FUNC_NAME;
5072 break;
5073 case RID_PRETTY_FUNCTION_NAME:
5074 name = NIC_PRETTY_FUNC;
5075 break;
5076 case RID_C99_FUNCTION_NAME:
5077 name = NIC_C99_FUNC;
5078 break;
5079 default:
5080 gcc_unreachable ();
5083 if (cp_parser_non_integral_constant_expression (parser, name))
5084 return error_mark_node;
5086 /* Look up the name. */
5087 return finish_fname (token->u.value);
5090 case RID_VA_ARG:
5092 tree expression;
5093 tree type;
5094 source_location type_location;
5095 location_t start_loc
5096 = cp_lexer_peek_token (parser->lexer)->location;
5097 /* The `__builtin_va_arg' construct is used to handle
5098 `va_arg'. Consume the `__builtin_va_arg' token. */
5099 cp_lexer_consume_token (parser->lexer);
5100 /* Look for the opening `('. */
5101 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5102 /* Now, parse the assignment-expression. */
5103 expression = cp_parser_assignment_expression (parser);
5104 /* Look for the `,'. */
5105 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
5106 type_location = cp_lexer_peek_token (parser->lexer)->location;
5107 /* Parse the type-id. */
5109 type_id_in_expr_sentinel s (parser);
5110 type = cp_parser_type_id (parser);
5112 /* Look for the closing `)'. */
5113 location_t finish_loc
5114 = cp_lexer_peek_token (parser->lexer)->location;
5115 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5116 /* Using `va_arg' in a constant-expression is not
5117 allowed. */
5118 if (cp_parser_non_integral_constant_expression (parser,
5119 NIC_VA_ARG))
5120 return error_mark_node;
5121 /* Construct a location of the form:
5122 __builtin_va_arg (v, int)
5123 ~~~~~~~~~~~~~~~~~~~~~^~~~
5124 with the caret at the type, ranging from the start of the
5125 "__builtin_va_arg" token to the close paren. */
5126 location_t combined_loc
5127 = make_location (type_location, start_loc, finish_loc);
5128 return build_x_va_arg (combined_loc, expression, type);
5131 case RID_OFFSETOF:
5132 return cp_parser_builtin_offsetof (parser);
5134 case RID_HAS_NOTHROW_ASSIGN:
5135 case RID_HAS_NOTHROW_CONSTRUCTOR:
5136 case RID_HAS_NOTHROW_COPY:
5137 case RID_HAS_TRIVIAL_ASSIGN:
5138 case RID_HAS_TRIVIAL_CONSTRUCTOR:
5139 case RID_HAS_TRIVIAL_COPY:
5140 case RID_HAS_TRIVIAL_DESTRUCTOR:
5141 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
5142 case RID_HAS_VIRTUAL_DESTRUCTOR:
5143 case RID_IS_ABSTRACT:
5144 case RID_IS_AGGREGATE:
5145 case RID_IS_BASE_OF:
5146 case RID_IS_CLASS:
5147 case RID_IS_EMPTY:
5148 case RID_IS_ENUM:
5149 case RID_IS_FINAL:
5150 case RID_IS_LITERAL_TYPE:
5151 case RID_IS_POD:
5152 case RID_IS_POLYMORPHIC:
5153 case RID_IS_SAME_AS:
5154 case RID_IS_STD_LAYOUT:
5155 case RID_IS_TRIVIAL:
5156 case RID_IS_TRIVIALLY_ASSIGNABLE:
5157 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
5158 case RID_IS_TRIVIALLY_COPYABLE:
5159 case RID_IS_UNION:
5160 case RID_IS_ASSIGNABLE:
5161 case RID_IS_CONSTRUCTIBLE:
5162 return cp_parser_trait_expr (parser, token->keyword);
5164 // C++ concepts
5165 case RID_REQUIRES:
5166 return cp_parser_requires_expression (parser);
5168 /* Objective-C++ expressions. */
5169 case RID_AT_ENCODE:
5170 case RID_AT_PROTOCOL:
5171 case RID_AT_SELECTOR:
5172 return cp_parser_objc_expression (parser);
5174 case RID_TEMPLATE:
5175 if (parser->in_function_body
5176 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5177 == CPP_LESS))
5179 error_at (token->location,
5180 "a template declaration cannot appear at block scope");
5181 cp_parser_skip_to_end_of_block_or_statement (parser);
5182 return error_mark_node;
5184 /* FALLTHRU */
5185 default:
5186 cp_parser_error (parser, "expected primary-expression");
5187 return error_mark_node;
5190 /* An id-expression can start with either an identifier, a
5191 `::' as the beginning of a qualified-id, or the "operator"
5192 keyword. */
5193 case CPP_NAME:
5194 case CPP_SCOPE:
5195 case CPP_TEMPLATE_ID:
5196 case CPP_NESTED_NAME_SPECIFIER:
5198 id_expression:
5199 cp_expr id_expression;
5200 cp_expr decl;
5201 const char *error_msg;
5202 bool template_p;
5203 bool done;
5204 cp_token *id_expr_token;
5206 /* Parse the id-expression. */
5207 id_expression
5208 = cp_parser_id_expression (parser,
5209 /*template_keyword_p=*/false,
5210 /*check_dependency_p=*/true,
5211 &template_p,
5212 /*declarator_p=*/false,
5213 /*optional_p=*/false);
5214 if (id_expression == error_mark_node)
5215 return error_mark_node;
5216 id_expr_token = token;
5217 token = cp_lexer_peek_token (parser->lexer);
5218 done = (token->type != CPP_OPEN_SQUARE
5219 && token->type != CPP_OPEN_PAREN
5220 && token->type != CPP_DOT
5221 && token->type != CPP_DEREF
5222 && token->type != CPP_PLUS_PLUS
5223 && token->type != CPP_MINUS_MINUS);
5224 /* If we have a template-id, then no further lookup is
5225 required. If the template-id was for a template-class, we
5226 will sometimes have a TYPE_DECL at this point. */
5227 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
5228 || TREE_CODE (id_expression) == TYPE_DECL)
5229 decl = id_expression;
5230 /* Look up the name. */
5231 else
5233 tree ambiguous_decls;
5235 /* If we already know that this lookup is ambiguous, then
5236 we've already issued an error message; there's no reason
5237 to check again. */
5238 if (id_expr_token->type == CPP_NAME
5239 && id_expr_token->error_reported)
5241 cp_parser_simulate_error (parser);
5242 return error_mark_node;
5245 decl = cp_parser_lookup_name (parser, id_expression,
5246 none_type,
5247 template_p,
5248 /*is_namespace=*/false,
5249 /*check_dependency=*/true,
5250 &ambiguous_decls,
5251 id_expr_token->location);
5252 /* If the lookup was ambiguous, an error will already have
5253 been issued. */
5254 if (ambiguous_decls)
5255 return error_mark_node;
5257 /* In Objective-C++, we may have an Objective-C 2.0
5258 dot-syntax for classes here. */
5259 if (c_dialect_objc ()
5260 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
5261 && TREE_CODE (decl) == TYPE_DECL
5262 && objc_is_class_name (decl))
5264 tree component;
5265 cp_lexer_consume_token (parser->lexer);
5266 component = cp_parser_identifier (parser);
5267 if (component == error_mark_node)
5268 return error_mark_node;
5270 tree result = objc_build_class_component_ref (id_expression,
5271 component);
5272 /* Build a location of the form:
5273 expr.component
5274 ~~~~~^~~~~~~~~
5275 with caret at the start of the component name (at
5276 input_location), ranging from the start of the id_expression
5277 to the end of the component name. */
5278 location_t combined_loc
5279 = make_location (input_location, id_expression.get_start (),
5280 get_finish (input_location));
5281 protected_set_expr_location (result, combined_loc);
5282 return result;
5285 /* In Objective-C++, an instance variable (ivar) may be preferred
5286 to whatever cp_parser_lookup_name() found.
5287 Call objc_lookup_ivar. To avoid exposing cp_expr to the
5288 rest of c-family, we have to do a little extra work to preserve
5289 any location information in cp_expr "decl". Given that
5290 objc_lookup_ivar is implemented in "c-family" and "objc", we
5291 have a trip through the pure "tree" type, rather than cp_expr.
5292 Naively copying it back to "decl" would implicitly give the
5293 new cp_expr value an UNKNOWN_LOCATION for nodes that don't
5294 store an EXPR_LOCATION. Hence we only update "decl" (and
5295 hence its location_t) if we get back a different tree node. */
5296 tree decl_tree = objc_lookup_ivar (decl.get_value (),
5297 id_expression);
5298 if (decl_tree != decl.get_value ())
5299 decl = cp_expr (decl_tree);
5301 /* If name lookup gives us a SCOPE_REF, then the
5302 qualifying scope was dependent. */
5303 if (TREE_CODE (decl) == SCOPE_REF)
5305 /* At this point, we do not know if DECL is a valid
5306 integral constant expression. We assume that it is
5307 in fact such an expression, so that code like:
5309 template <int N> struct A {
5310 int a[B<N>::i];
5313 is accepted. At template-instantiation time, we
5314 will check that B<N>::i is actually a constant. */
5315 return decl;
5317 /* Check to see if DECL is a local variable in a context
5318 where that is forbidden. */
5319 if (parser->local_variables_forbidden_p
5320 && local_variable_p (decl))
5322 /* It might be that we only found DECL because we are
5323 trying to be generous with pre-ISO scoping rules.
5324 For example, consider:
5326 int i;
5327 void g() {
5328 for (int i = 0; i < 10; ++i) {}
5329 extern void f(int j = i);
5332 Here, name look up will originally find the out
5333 of scope `i'. We need to issue a warning message,
5334 but then use the global `i'. */
5335 decl = check_for_out_of_scope_variable (decl);
5336 if (local_variable_p (decl))
5338 error_at (id_expr_token->location,
5339 "local variable %qD may not appear in this context",
5340 decl.get_value ());
5341 return error_mark_node;
5346 decl = (finish_id_expression
5347 (id_expression, decl, parser->scope,
5348 idk,
5349 parser->integral_constant_expression_p,
5350 parser->allow_non_integral_constant_expression_p,
5351 &parser->non_integral_constant_expression_p,
5352 template_p, done, address_p,
5353 template_arg_p,
5354 &error_msg,
5355 id_expression.get_location ()));
5356 if (error_msg)
5357 cp_parser_error (parser, error_msg);
5358 decl.set_location (id_expr_token->location);
5359 return decl;
5362 /* Anything else is an error. */
5363 default:
5364 cp_parser_error (parser, "expected primary-expression");
5365 return error_mark_node;
5369 static inline cp_expr
5370 cp_parser_primary_expression (cp_parser *parser,
5371 bool address_p,
5372 bool cast_p,
5373 bool template_arg_p,
5374 cp_id_kind *idk)
5376 return cp_parser_primary_expression (parser, address_p, cast_p, template_arg_p,
5377 /*decltype*/false, idk);
5380 /* Parse an id-expression.
5382 id-expression:
5383 unqualified-id
5384 qualified-id
5386 qualified-id:
5387 :: [opt] nested-name-specifier template [opt] unqualified-id
5388 :: identifier
5389 :: operator-function-id
5390 :: template-id
5392 Return a representation of the unqualified portion of the
5393 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
5394 a `::' or nested-name-specifier.
5396 Often, if the id-expression was a qualified-id, the caller will
5397 want to make a SCOPE_REF to represent the qualified-id. This
5398 function does not do this in order to avoid wastefully creating
5399 SCOPE_REFs when they are not required.
5401 If TEMPLATE_KEYWORD_P is true, then we have just seen the
5402 `template' keyword.
5404 If CHECK_DEPENDENCY_P is false, then names are looked up inside
5405 uninstantiated templates.
5407 If *TEMPLATE_P is non-NULL, it is set to true iff the
5408 `template' keyword is used to explicitly indicate that the entity
5409 named is a template.
5411 If DECLARATOR_P is true, the id-expression is appearing as part of
5412 a declarator, rather than as part of an expression. */
5414 static cp_expr
5415 cp_parser_id_expression (cp_parser *parser,
5416 bool template_keyword_p,
5417 bool check_dependency_p,
5418 bool *template_p,
5419 bool declarator_p,
5420 bool optional_p)
5422 bool global_scope_p;
5423 bool nested_name_specifier_p;
5425 /* Assume the `template' keyword was not used. */
5426 if (template_p)
5427 *template_p = template_keyword_p;
5429 /* Look for the optional `::' operator. */
5430 global_scope_p
5431 = (!template_keyword_p
5432 && (cp_parser_global_scope_opt (parser,
5433 /*current_scope_valid_p=*/false)
5434 != NULL_TREE));
5436 /* Look for the optional nested-name-specifier. */
5437 nested_name_specifier_p
5438 = (cp_parser_nested_name_specifier_opt (parser,
5439 /*typename_keyword_p=*/false,
5440 check_dependency_p,
5441 /*type_p=*/false,
5442 declarator_p,
5443 template_keyword_p)
5444 != NULL_TREE);
5446 /* If there is a nested-name-specifier, then we are looking at
5447 the first qualified-id production. */
5448 if (nested_name_specifier_p)
5450 tree saved_scope;
5451 tree saved_object_scope;
5452 tree saved_qualifying_scope;
5453 cp_expr unqualified_id;
5454 bool is_template;
5456 /* See if the next token is the `template' keyword. */
5457 if (!template_p)
5458 template_p = &is_template;
5459 *template_p = cp_parser_optional_template_keyword (parser);
5460 /* Name lookup we do during the processing of the
5461 unqualified-id might obliterate SCOPE. */
5462 saved_scope = parser->scope;
5463 saved_object_scope = parser->object_scope;
5464 saved_qualifying_scope = parser->qualifying_scope;
5465 /* Process the final unqualified-id. */
5466 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
5467 check_dependency_p,
5468 declarator_p,
5469 /*optional_p=*/false);
5470 /* Restore the SAVED_SCOPE for our caller. */
5471 parser->scope = saved_scope;
5472 parser->object_scope = saved_object_scope;
5473 parser->qualifying_scope = saved_qualifying_scope;
5475 return unqualified_id;
5477 /* Otherwise, if we are in global scope, then we are looking at one
5478 of the other qualified-id productions. */
5479 else if (global_scope_p)
5481 cp_token *token;
5482 tree id;
5484 /* Peek at the next token. */
5485 token = cp_lexer_peek_token (parser->lexer);
5487 /* If it's an identifier, and the next token is not a "<", then
5488 we can avoid the template-id case. This is an optimization
5489 for this common case. */
5490 if (token->type == CPP_NAME
5491 && !cp_parser_nth_token_starts_template_argument_list_p
5492 (parser, 2))
5493 return cp_parser_identifier (parser);
5495 cp_parser_parse_tentatively (parser);
5496 /* Try a template-id. */
5497 id = cp_parser_template_id (parser,
5498 /*template_keyword_p=*/false,
5499 /*check_dependency_p=*/true,
5500 none_type,
5501 declarator_p);
5502 /* If that worked, we're done. */
5503 if (cp_parser_parse_definitely (parser))
5504 return id;
5506 /* Peek at the next token. (Changes in the token buffer may
5507 have invalidated the pointer obtained above.) */
5508 token = cp_lexer_peek_token (parser->lexer);
5510 switch (token->type)
5512 case CPP_NAME:
5513 return cp_parser_identifier (parser);
5515 case CPP_KEYWORD:
5516 if (token->keyword == RID_OPERATOR)
5517 return cp_parser_operator_function_id (parser);
5518 /* Fall through. */
5520 default:
5521 cp_parser_error (parser, "expected id-expression");
5522 return error_mark_node;
5525 else
5526 return cp_parser_unqualified_id (parser, template_keyword_p,
5527 /*check_dependency_p=*/true,
5528 declarator_p,
5529 optional_p);
5532 /* Parse an unqualified-id.
5534 unqualified-id:
5535 identifier
5536 operator-function-id
5537 conversion-function-id
5538 ~ class-name
5539 template-id
5541 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
5542 keyword, in a construct like `A::template ...'.
5544 Returns a representation of unqualified-id. For the `identifier'
5545 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
5546 production a BIT_NOT_EXPR is returned; the operand of the
5547 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
5548 other productions, see the documentation accompanying the
5549 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
5550 names are looked up in uninstantiated templates. If DECLARATOR_P
5551 is true, the unqualified-id is appearing as part of a declarator,
5552 rather than as part of an expression. */
5554 static cp_expr
5555 cp_parser_unqualified_id (cp_parser* parser,
5556 bool template_keyword_p,
5557 bool check_dependency_p,
5558 bool declarator_p,
5559 bool optional_p)
5561 cp_token *token;
5563 /* Peek at the next token. */
5564 token = cp_lexer_peek_token (parser->lexer);
5566 switch ((int) token->type)
5568 case CPP_NAME:
5570 tree id;
5572 /* We don't know yet whether or not this will be a
5573 template-id. */
5574 cp_parser_parse_tentatively (parser);
5575 /* Try a template-id. */
5576 id = cp_parser_template_id (parser, template_keyword_p,
5577 check_dependency_p,
5578 none_type,
5579 declarator_p);
5580 /* If it worked, we're done. */
5581 if (cp_parser_parse_definitely (parser))
5582 return id;
5583 /* Otherwise, it's an ordinary identifier. */
5584 return cp_parser_identifier (parser);
5587 case CPP_TEMPLATE_ID:
5588 return cp_parser_template_id (parser, template_keyword_p,
5589 check_dependency_p,
5590 none_type,
5591 declarator_p);
5593 case CPP_COMPL:
5595 tree type_decl;
5596 tree qualifying_scope;
5597 tree object_scope;
5598 tree scope;
5599 bool done;
5601 /* Consume the `~' token. */
5602 cp_lexer_consume_token (parser->lexer);
5603 /* Parse the class-name. The standard, as written, seems to
5604 say that:
5606 template <typename T> struct S { ~S (); };
5607 template <typename T> S<T>::~S() {}
5609 is invalid, since `~' must be followed by a class-name, but
5610 `S<T>' is dependent, and so not known to be a class.
5611 That's not right; we need to look in uninstantiated
5612 templates. A further complication arises from:
5614 template <typename T> void f(T t) {
5615 t.T::~T();
5618 Here, it is not possible to look up `T' in the scope of `T'
5619 itself. We must look in both the current scope, and the
5620 scope of the containing complete expression.
5622 Yet another issue is:
5624 struct S {
5625 int S;
5626 ~S();
5629 S::~S() {}
5631 The standard does not seem to say that the `S' in `~S'
5632 should refer to the type `S' and not the data member
5633 `S::S'. */
5635 /* DR 244 says that we look up the name after the "~" in the
5636 same scope as we looked up the qualifying name. That idea
5637 isn't fully worked out; it's more complicated than that. */
5638 scope = parser->scope;
5639 object_scope = parser->object_scope;
5640 qualifying_scope = parser->qualifying_scope;
5642 /* Check for invalid scopes. */
5643 if (scope == error_mark_node)
5645 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5646 cp_lexer_consume_token (parser->lexer);
5647 return error_mark_node;
5649 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
5651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5652 error_at (token->location,
5653 "scope %qT before %<~%> is not a class-name",
5654 scope);
5655 cp_parser_simulate_error (parser);
5656 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
5657 cp_lexer_consume_token (parser->lexer);
5658 return error_mark_node;
5660 gcc_assert (!scope || TYPE_P (scope));
5662 /* If the name is of the form "X::~X" it's OK even if X is a
5663 typedef. */
5664 token = cp_lexer_peek_token (parser->lexer);
5665 if (scope
5666 && token->type == CPP_NAME
5667 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5668 != CPP_LESS)
5669 && (token->u.value == TYPE_IDENTIFIER (scope)
5670 || (CLASS_TYPE_P (scope)
5671 && constructor_name_p (token->u.value, scope))))
5673 cp_lexer_consume_token (parser->lexer);
5674 return build_nt (BIT_NOT_EXPR, scope);
5677 /* ~auto means the destructor of whatever the object is. */
5678 if (cp_parser_is_keyword (token, RID_AUTO))
5680 if (cxx_dialect < cxx14)
5681 pedwarn (input_location, 0,
5682 "%<~auto%> only available with "
5683 "-std=c++14 or -std=gnu++14");
5684 cp_lexer_consume_token (parser->lexer);
5685 return build_nt (BIT_NOT_EXPR, make_auto ());
5688 /* If there was an explicit qualification (S::~T), first look
5689 in the scope given by the qualification (i.e., S).
5691 Note: in the calls to cp_parser_class_name below we pass
5692 typename_type so that lookup finds the injected-class-name
5693 rather than the constructor. */
5694 done = false;
5695 type_decl = NULL_TREE;
5696 if (scope)
5698 cp_parser_parse_tentatively (parser);
5699 type_decl = cp_parser_class_name (parser,
5700 /*typename_keyword_p=*/false,
5701 /*template_keyword_p=*/false,
5702 typename_type,
5703 /*check_dependency=*/false,
5704 /*class_head_p=*/false,
5705 declarator_p);
5706 if (cp_parser_parse_definitely (parser))
5707 done = true;
5709 /* In "N::S::~S", look in "N" as well. */
5710 if (!done && scope && qualifying_scope)
5712 cp_parser_parse_tentatively (parser);
5713 parser->scope = qualifying_scope;
5714 parser->object_scope = NULL_TREE;
5715 parser->qualifying_scope = NULL_TREE;
5716 type_decl
5717 = cp_parser_class_name (parser,
5718 /*typename_keyword_p=*/false,
5719 /*template_keyword_p=*/false,
5720 typename_type,
5721 /*check_dependency=*/false,
5722 /*class_head_p=*/false,
5723 declarator_p);
5724 if (cp_parser_parse_definitely (parser))
5725 done = true;
5727 /* In "p->S::~T", look in the scope given by "*p" as well. */
5728 else if (!done && object_scope)
5730 cp_parser_parse_tentatively (parser);
5731 parser->scope = object_scope;
5732 parser->object_scope = NULL_TREE;
5733 parser->qualifying_scope = NULL_TREE;
5734 type_decl
5735 = cp_parser_class_name (parser,
5736 /*typename_keyword_p=*/false,
5737 /*template_keyword_p=*/false,
5738 typename_type,
5739 /*check_dependency=*/false,
5740 /*class_head_p=*/false,
5741 declarator_p);
5742 if (cp_parser_parse_definitely (parser))
5743 done = true;
5745 /* Look in the surrounding context. */
5746 if (!done)
5748 parser->scope = NULL_TREE;
5749 parser->object_scope = NULL_TREE;
5750 parser->qualifying_scope = NULL_TREE;
5751 if (processing_template_decl)
5752 cp_parser_parse_tentatively (parser);
5753 type_decl
5754 = cp_parser_class_name (parser,
5755 /*typename_keyword_p=*/false,
5756 /*template_keyword_p=*/false,
5757 typename_type,
5758 /*check_dependency=*/false,
5759 /*class_head_p=*/false,
5760 declarator_p);
5761 if (processing_template_decl
5762 && ! cp_parser_parse_definitely (parser))
5764 /* We couldn't find a type with this name. If we're parsing
5765 tentatively, fail and try something else. */
5766 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5768 cp_parser_simulate_error (parser);
5769 return error_mark_node;
5771 /* Otherwise, accept it and check for a match at instantiation
5772 time. */
5773 type_decl = cp_parser_identifier (parser);
5774 if (type_decl != error_mark_node)
5775 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
5776 return type_decl;
5779 /* If an error occurred, assume that the name of the
5780 destructor is the same as the name of the qualifying
5781 class. That allows us to keep parsing after running
5782 into ill-formed destructor names. */
5783 if (type_decl == error_mark_node && scope)
5784 return build_nt (BIT_NOT_EXPR, scope);
5785 else if (type_decl == error_mark_node)
5786 return error_mark_node;
5788 /* Check that destructor name and scope match. */
5789 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
5791 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
5792 error_at (token->location,
5793 "declaration of %<~%T%> as member of %qT",
5794 type_decl, scope);
5795 cp_parser_simulate_error (parser);
5796 return error_mark_node;
5799 /* [class.dtor]
5801 A typedef-name that names a class shall not be used as the
5802 identifier in the declarator for a destructor declaration. */
5803 if (declarator_p
5804 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
5805 && !DECL_SELF_REFERENCE_P (type_decl)
5806 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
5807 error_at (token->location,
5808 "typedef-name %qD used as destructor declarator",
5809 type_decl);
5811 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
5814 case CPP_KEYWORD:
5815 if (token->keyword == RID_OPERATOR)
5817 cp_expr id;
5819 /* This could be a template-id, so we try that first. */
5820 cp_parser_parse_tentatively (parser);
5821 /* Try a template-id. */
5822 id = cp_parser_template_id (parser, template_keyword_p,
5823 /*check_dependency_p=*/true,
5824 none_type,
5825 declarator_p);
5826 /* If that worked, we're done. */
5827 if (cp_parser_parse_definitely (parser))
5828 return id;
5829 /* We still don't know whether we're looking at an
5830 operator-function-id or a conversion-function-id. */
5831 cp_parser_parse_tentatively (parser);
5832 /* Try an operator-function-id. */
5833 id = cp_parser_operator_function_id (parser);
5834 /* If that didn't work, try a conversion-function-id. */
5835 if (!cp_parser_parse_definitely (parser))
5836 id = cp_parser_conversion_function_id (parser);
5837 else if (UDLIT_OPER_P (id))
5839 /* 17.6.3.3.5 */
5840 const char *name = UDLIT_OP_SUFFIX (id);
5841 if (name[0] != '_' && !in_system_header_at (input_location)
5842 && declarator_p)
5843 warning (OPT_Wliteral_suffix,
5844 "literal operator suffixes not preceded by %<_%>"
5845 " are reserved for future standardization");
5848 return id;
5850 /* Fall through. */
5852 default:
5853 if (optional_p)
5854 return NULL_TREE;
5855 cp_parser_error (parser, "expected unqualified-id");
5856 return error_mark_node;
5860 /* Parse an (optional) nested-name-specifier.
5862 nested-name-specifier: [C++98]
5863 class-or-namespace-name :: nested-name-specifier [opt]
5864 class-or-namespace-name :: template nested-name-specifier [opt]
5866 nested-name-specifier: [C++0x]
5867 type-name ::
5868 namespace-name ::
5869 nested-name-specifier identifier ::
5870 nested-name-specifier template [opt] simple-template-id ::
5872 PARSER->SCOPE should be set appropriately before this function is
5873 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
5874 effect. TYPE_P is TRUE if we non-type bindings should be ignored
5875 in name lookups.
5877 Sets PARSER->SCOPE to the class (TYPE) or namespace
5878 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
5879 it unchanged if there is no nested-name-specifier. Returns the new
5880 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
5882 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
5883 part of a declaration and/or decl-specifier. */
5885 static tree
5886 cp_parser_nested_name_specifier_opt (cp_parser *parser,
5887 bool typename_keyword_p,
5888 bool check_dependency_p,
5889 bool type_p,
5890 bool is_declaration,
5891 bool template_keyword_p /* = false */)
5893 bool success = false;
5894 cp_token_position start = 0;
5895 cp_token *token;
5897 /* Remember where the nested-name-specifier starts. */
5898 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5900 start = cp_lexer_token_position (parser->lexer, false);
5901 push_deferring_access_checks (dk_deferred);
5904 while (true)
5906 tree new_scope;
5907 tree old_scope;
5908 tree saved_qualifying_scope;
5910 /* Spot cases that cannot be the beginning of a
5911 nested-name-specifier. */
5912 token = cp_lexer_peek_token (parser->lexer);
5914 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
5915 the already parsed nested-name-specifier. */
5916 if (token->type == CPP_NESTED_NAME_SPECIFIER)
5918 /* Grab the nested-name-specifier and continue the loop. */
5919 cp_parser_pre_parsed_nested_name_specifier (parser);
5920 /* If we originally encountered this nested-name-specifier
5921 with IS_DECLARATION set to false, we will not have
5922 resolved TYPENAME_TYPEs, so we must do so here. */
5923 if (is_declaration
5924 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5926 new_scope = resolve_typename_type (parser->scope,
5927 /*only_current_p=*/false);
5928 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
5929 parser->scope = new_scope;
5931 success = true;
5932 continue;
5935 /* Spot cases that cannot be the beginning of a
5936 nested-name-specifier. On the second and subsequent times
5937 through the loop, we look for the `template' keyword. */
5938 if (success && token->keyword == RID_TEMPLATE)
5940 /* A template-id can start a nested-name-specifier. */
5941 else if (token->type == CPP_TEMPLATE_ID)
5943 /* DR 743: decltype can be used in a nested-name-specifier. */
5944 else if (token_is_decltype (token))
5946 else
5948 /* If the next token is not an identifier, then it is
5949 definitely not a type-name or namespace-name. */
5950 if (token->type != CPP_NAME)
5951 break;
5952 /* If the following token is neither a `<' (to begin a
5953 template-id), nor a `::', then we are not looking at a
5954 nested-name-specifier. */
5955 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5957 if (token->type == CPP_COLON
5958 && parser->colon_corrects_to_scope_p
5959 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
5961 gcc_rich_location richloc (token->location);
5962 richloc.add_fixit_replace ("::");
5963 error_at_rich_loc (&richloc,
5964 "found %<:%> in nested-name-specifier, "
5965 "expected %<::%>");
5966 token->type = CPP_SCOPE;
5969 if (token->type != CPP_SCOPE
5970 && !cp_parser_nth_token_starts_template_argument_list_p
5971 (parser, 2))
5972 break;
5975 /* The nested-name-specifier is optional, so we parse
5976 tentatively. */
5977 cp_parser_parse_tentatively (parser);
5979 /* Look for the optional `template' keyword, if this isn't the
5980 first time through the loop. */
5981 if (success)
5982 template_keyword_p = cp_parser_optional_template_keyword (parser);
5984 /* Save the old scope since the name lookup we are about to do
5985 might destroy it. */
5986 old_scope = parser->scope;
5987 saved_qualifying_scope = parser->qualifying_scope;
5988 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
5989 look up names in "X<T>::I" in order to determine that "Y" is
5990 a template. So, if we have a typename at this point, we make
5991 an effort to look through it. */
5992 if (is_declaration
5993 && !typename_keyword_p
5994 && parser->scope
5995 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
5996 parser->scope = resolve_typename_type (parser->scope,
5997 /*only_current_p=*/false);
5998 /* Parse the qualifying entity. */
5999 new_scope
6000 = cp_parser_qualifying_entity (parser,
6001 typename_keyword_p,
6002 template_keyword_p,
6003 check_dependency_p,
6004 type_p,
6005 is_declaration);
6006 /* Look for the `::' token. */
6007 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6009 /* If we found what we wanted, we keep going; otherwise, we're
6010 done. */
6011 if (!cp_parser_parse_definitely (parser))
6013 bool error_p = false;
6015 /* Restore the OLD_SCOPE since it was valid before the
6016 failed attempt at finding the last
6017 class-or-namespace-name. */
6018 parser->scope = old_scope;
6019 parser->qualifying_scope = saved_qualifying_scope;
6021 /* If the next token is a decltype, and the one after that is a
6022 `::', then the decltype has failed to resolve to a class or
6023 enumeration type. Give this error even when parsing
6024 tentatively since it can't possibly be valid--and we're going
6025 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
6026 won't get another chance.*/
6027 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
6028 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6029 == CPP_SCOPE))
6031 token = cp_lexer_consume_token (parser->lexer);
6032 error_at (token->location, "decltype evaluates to %qT, "
6033 "which is not a class or enumeration type",
6034 token->u.tree_check_value->value);
6035 parser->scope = error_mark_node;
6036 error_p = true;
6037 /* As below. */
6038 success = true;
6039 cp_lexer_consume_token (parser->lexer);
6042 if (cp_lexer_next_token_is (parser->lexer, CPP_TEMPLATE_ID)
6043 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_SCOPE))
6045 /* If we have a non-type template-id followed by ::, it can't
6046 possibly be valid. */
6047 token = cp_lexer_peek_token (parser->lexer);
6048 tree tid = token->u.tree_check_value->value;
6049 if (TREE_CODE (tid) == TEMPLATE_ID_EXPR
6050 && TREE_CODE (TREE_OPERAND (tid, 0)) != IDENTIFIER_NODE)
6052 tree tmpl = NULL_TREE;
6053 if (is_overloaded_fn (tid))
6055 tree fns = get_fns (tid);
6056 if (OVL_SINGLE_P (fns))
6057 tmpl = OVL_FIRST (fns);
6058 error_at (token->location, "function template-id %qD "
6059 "in nested-name-specifier", tid);
6061 else
6063 /* Variable template. */
6064 tmpl = TREE_OPERAND (tid, 0);
6065 gcc_assert (variable_template_p (tmpl));
6066 error_at (token->location, "variable template-id %qD "
6067 "in nested-name-specifier", tid);
6069 if (tmpl)
6070 inform (DECL_SOURCE_LOCATION (tmpl),
6071 "%qD declared here", tmpl);
6073 parser->scope = error_mark_node;
6074 error_p = true;
6075 /* As below. */
6076 success = true;
6077 cp_lexer_consume_token (parser->lexer);
6078 cp_lexer_consume_token (parser->lexer);
6082 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
6083 break;
6084 /* If the next token is an identifier, and the one after
6085 that is a `::', then any valid interpretation would have
6086 found a class-or-namespace-name. */
6087 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
6088 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
6089 == CPP_SCOPE)
6090 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6091 != CPP_COMPL))
6093 token = cp_lexer_consume_token (parser->lexer);
6094 if (!error_p)
6096 if (!token->error_reported)
6098 tree decl;
6099 tree ambiguous_decls;
6101 decl = cp_parser_lookup_name (parser, token->u.value,
6102 none_type,
6103 /*is_template=*/false,
6104 /*is_namespace=*/false,
6105 /*check_dependency=*/true,
6106 &ambiguous_decls,
6107 token->location);
6108 if (TREE_CODE (decl) == TEMPLATE_DECL)
6109 error_at (token->location,
6110 "%qD used without template parameters",
6111 decl);
6112 else if (ambiguous_decls)
6114 // cp_parser_lookup_name has the same diagnostic,
6115 // thus make sure to emit it at most once.
6116 if (cp_parser_uncommitted_to_tentative_parse_p
6117 (parser))
6119 error_at (token->location,
6120 "reference to %qD is ambiguous",
6121 token->u.value);
6122 print_candidates (ambiguous_decls);
6124 decl = error_mark_node;
6126 else
6128 if (cxx_dialect != cxx98)
6129 cp_parser_name_lookup_error
6130 (parser, token->u.value, decl, NLE_NOT_CXX98,
6131 token->location);
6132 else
6133 cp_parser_name_lookup_error
6134 (parser, token->u.value, decl, NLE_CXX98,
6135 token->location);
6138 parser->scope = error_mark_node;
6139 error_p = true;
6140 /* Treat this as a successful nested-name-specifier
6141 due to:
6143 [basic.lookup.qual]
6145 If the name found is not a class-name (clause
6146 _class_) or namespace-name (_namespace.def_), the
6147 program is ill-formed. */
6148 success = true;
6150 cp_lexer_consume_token (parser->lexer);
6152 break;
6154 /* We've found one valid nested-name-specifier. */
6155 success = true;
6156 /* Name lookup always gives us a DECL. */
6157 if (TREE_CODE (new_scope) == TYPE_DECL)
6158 new_scope = TREE_TYPE (new_scope);
6159 /* Uses of "template" must be followed by actual templates. */
6160 if (template_keyword_p
6161 && !(CLASS_TYPE_P (new_scope)
6162 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
6163 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
6164 || CLASSTYPE_IS_TEMPLATE (new_scope)))
6165 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
6166 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
6167 == TEMPLATE_ID_EXPR)))
6168 permerror (input_location, TYPE_P (new_scope)
6169 ? G_("%qT is not a template")
6170 : G_("%qD is not a template"),
6171 new_scope);
6172 /* If it is a class scope, try to complete it; we are about to
6173 be looking up names inside the class. */
6174 if (TYPE_P (new_scope)
6175 /* Since checking types for dependency can be expensive,
6176 avoid doing it if the type is already complete. */
6177 && !COMPLETE_TYPE_P (new_scope)
6178 /* Do not try to complete dependent types. */
6179 && !dependent_type_p (new_scope))
6181 new_scope = complete_type (new_scope);
6182 /* If it is a typedef to current class, use the current
6183 class instead, as the typedef won't have any names inside
6184 it yet. */
6185 if (!COMPLETE_TYPE_P (new_scope)
6186 && currently_open_class (new_scope))
6187 new_scope = TYPE_MAIN_VARIANT (new_scope);
6189 /* Make sure we look in the right scope the next time through
6190 the loop. */
6191 parser->scope = new_scope;
6194 /* If parsing tentatively, replace the sequence of tokens that makes
6195 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
6196 token. That way, should we re-parse the token stream, we will
6197 not have to repeat the effort required to do the parse, nor will
6198 we issue duplicate error messages. */
6199 if (success && start)
6201 cp_token *token;
6203 token = cp_lexer_token_at (parser->lexer, start);
6204 /* Reset the contents of the START token. */
6205 token->type = CPP_NESTED_NAME_SPECIFIER;
6206 /* Retrieve any deferred checks. Do not pop this access checks yet
6207 so the memory will not be reclaimed during token replacing below. */
6208 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
6209 token->u.tree_check_value->value = parser->scope;
6210 token->u.tree_check_value->checks = get_deferred_access_checks ();
6211 token->u.tree_check_value->qualifying_scope =
6212 parser->qualifying_scope;
6213 token->keyword = RID_MAX;
6215 /* Purge all subsequent tokens. */
6216 cp_lexer_purge_tokens_after (parser->lexer, start);
6219 if (start)
6220 pop_to_parent_deferring_access_checks ();
6222 return success ? parser->scope : NULL_TREE;
6225 /* Parse a nested-name-specifier. See
6226 cp_parser_nested_name_specifier_opt for details. This function
6227 behaves identically, except that it will an issue an error if no
6228 nested-name-specifier is present. */
6230 static tree
6231 cp_parser_nested_name_specifier (cp_parser *parser,
6232 bool typename_keyword_p,
6233 bool check_dependency_p,
6234 bool type_p,
6235 bool is_declaration)
6237 tree scope;
6239 /* Look for the nested-name-specifier. */
6240 scope = cp_parser_nested_name_specifier_opt (parser,
6241 typename_keyword_p,
6242 check_dependency_p,
6243 type_p,
6244 is_declaration);
6245 /* If it was not present, issue an error message. */
6246 if (!scope)
6248 cp_parser_error (parser, "expected nested-name-specifier");
6249 parser->scope = NULL_TREE;
6252 return scope;
6255 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
6256 this is either a class-name or a namespace-name (which corresponds
6257 to the class-or-namespace-name production in the grammar). For
6258 C++0x, it can also be a type-name that refers to an enumeration
6259 type or a simple-template-id.
6261 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
6262 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
6263 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
6264 TYPE_P is TRUE iff the next name should be taken as a class-name,
6265 even the same name is declared to be another entity in the same
6266 scope.
6268 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
6269 specified by the class-or-namespace-name. If neither is found the
6270 ERROR_MARK_NODE is returned. */
6272 static tree
6273 cp_parser_qualifying_entity (cp_parser *parser,
6274 bool typename_keyword_p,
6275 bool template_keyword_p,
6276 bool check_dependency_p,
6277 bool type_p,
6278 bool is_declaration)
6280 tree saved_scope;
6281 tree saved_qualifying_scope;
6282 tree saved_object_scope;
6283 tree scope;
6284 bool only_class_p;
6285 bool successful_parse_p;
6287 /* DR 743: decltype can appear in a nested-name-specifier. */
6288 if (cp_lexer_next_token_is_decltype (parser->lexer))
6290 scope = cp_parser_decltype (parser);
6291 if (TREE_CODE (scope) != ENUMERAL_TYPE
6292 && !MAYBE_CLASS_TYPE_P (scope))
6294 cp_parser_simulate_error (parser);
6295 return error_mark_node;
6297 if (TYPE_NAME (scope))
6298 scope = TYPE_NAME (scope);
6299 return scope;
6302 /* Before we try to parse the class-name, we must save away the
6303 current PARSER->SCOPE since cp_parser_class_name will destroy
6304 it. */
6305 saved_scope = parser->scope;
6306 saved_qualifying_scope = parser->qualifying_scope;
6307 saved_object_scope = parser->object_scope;
6308 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
6309 there is no need to look for a namespace-name. */
6310 only_class_p = template_keyword_p
6311 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
6312 if (!only_class_p)
6313 cp_parser_parse_tentatively (parser);
6314 scope = cp_parser_class_name (parser,
6315 typename_keyword_p,
6316 template_keyword_p,
6317 type_p ? class_type : none_type,
6318 check_dependency_p,
6319 /*class_head_p=*/false,
6320 is_declaration,
6321 /*enum_ok=*/cxx_dialect > cxx98);
6322 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
6323 /* If that didn't work, try for a namespace-name. */
6324 if (!only_class_p && !successful_parse_p)
6326 /* Restore the saved scope. */
6327 parser->scope = saved_scope;
6328 parser->qualifying_scope = saved_qualifying_scope;
6329 parser->object_scope = saved_object_scope;
6330 /* If we are not looking at an identifier followed by the scope
6331 resolution operator, then this is not part of a
6332 nested-name-specifier. (Note that this function is only used
6333 to parse the components of a nested-name-specifier.) */
6334 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
6335 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
6336 return error_mark_node;
6337 scope = cp_parser_namespace_name (parser);
6340 return scope;
6343 /* Return true if we are looking at a compound-literal, false otherwise. */
6345 static bool
6346 cp_parser_compound_literal_p (cp_parser *parser)
6348 /* Consume the `('. */
6349 cp_lexer_consume_token (parser->lexer);
6351 cp_lexer_save_tokens (parser->lexer);
6353 /* Skip tokens until the next token is a closing parenthesis.
6354 If we find the closing `)', and the next token is a `{', then
6355 we are looking at a compound-literal. */
6356 bool compound_literal_p
6357 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6358 /*consume_paren=*/true)
6359 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6361 /* Roll back the tokens we skipped. */
6362 cp_lexer_rollback_tokens (parser->lexer);
6364 return compound_literal_p;
6367 /* Parse a postfix-expression.
6369 postfix-expression:
6370 primary-expression
6371 postfix-expression [ expression ]
6372 postfix-expression ( expression-list [opt] )
6373 simple-type-specifier ( expression-list [opt] )
6374 typename :: [opt] nested-name-specifier identifier
6375 ( expression-list [opt] )
6376 typename :: [opt] nested-name-specifier template [opt] template-id
6377 ( expression-list [opt] )
6378 postfix-expression . template [opt] id-expression
6379 postfix-expression -> template [opt] id-expression
6380 postfix-expression . pseudo-destructor-name
6381 postfix-expression -> pseudo-destructor-name
6382 postfix-expression ++
6383 postfix-expression --
6384 dynamic_cast < type-id > ( expression )
6385 static_cast < type-id > ( expression )
6386 reinterpret_cast < type-id > ( expression )
6387 const_cast < type-id > ( expression )
6388 typeid ( expression )
6389 typeid ( type-id )
6391 GNU Extension:
6393 postfix-expression:
6394 ( type-id ) { initializer-list , [opt] }
6396 This extension is a GNU version of the C99 compound-literal
6397 construct. (The C99 grammar uses `type-name' instead of `type-id',
6398 but they are essentially the same concept.)
6400 If ADDRESS_P is true, the postfix expression is the operand of the
6401 `&' operator. CAST_P is true if this expression is the target of a
6402 cast.
6404 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
6405 class member access expressions [expr.ref].
6407 Returns a representation of the expression. */
6409 static cp_expr
6410 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
6411 bool member_access_only_p, bool decltype_p,
6412 cp_id_kind * pidk_return)
6414 cp_token *token;
6415 location_t loc;
6416 enum rid keyword;
6417 cp_id_kind idk = CP_ID_KIND_NONE;
6418 cp_expr postfix_expression = NULL_TREE;
6419 bool is_member_access = false;
6420 int saved_in_statement = -1;
6422 /* Peek at the next token. */
6423 token = cp_lexer_peek_token (parser->lexer);
6424 loc = token->location;
6425 location_t start_loc = get_range_from_loc (line_table, loc).m_start;
6427 /* Some of the productions are determined by keywords. */
6428 keyword = token->keyword;
6429 switch (keyword)
6431 case RID_DYNCAST:
6432 case RID_STATCAST:
6433 case RID_REINTCAST:
6434 case RID_CONSTCAST:
6436 tree type;
6437 cp_expr expression;
6438 const char *saved_message;
6439 bool saved_in_type_id_in_expr_p;
6441 /* All of these can be handled in the same way from the point
6442 of view of parsing. Begin by consuming the token
6443 identifying the cast. */
6444 cp_lexer_consume_token (parser->lexer);
6446 /* New types cannot be defined in the cast. */
6447 saved_message = parser->type_definition_forbidden_message;
6448 parser->type_definition_forbidden_message
6449 = G_("types may not be defined in casts");
6451 /* Look for the opening `<'. */
6452 cp_parser_require (parser, CPP_LESS, RT_LESS);
6453 /* Parse the type to which we are casting. */
6454 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6455 parser->in_type_id_in_expr_p = true;
6456 type = cp_parser_type_id (parser);
6457 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6458 /* Look for the closing `>'. */
6459 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
6460 /* Restore the old message. */
6461 parser->type_definition_forbidden_message = saved_message;
6463 bool saved_greater_than_is_operator_p
6464 = parser->greater_than_is_operator_p;
6465 parser->greater_than_is_operator_p = true;
6467 /* And the expression which is being cast. */
6468 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6469 expression = cp_parser_expression (parser, & idk, /*cast_p=*/true);
6470 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6471 RT_CLOSE_PAREN);
6472 location_t end_loc = close_paren ?
6473 close_paren->location : UNKNOWN_LOCATION;
6475 parser->greater_than_is_operator_p
6476 = saved_greater_than_is_operator_p;
6478 /* Only type conversions to integral or enumeration types
6479 can be used in constant-expressions. */
6480 if (!cast_valid_in_integral_constant_expression_p (type)
6481 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
6483 postfix_expression = error_mark_node;
6484 break;
6487 switch (keyword)
6489 case RID_DYNCAST:
6490 postfix_expression
6491 = build_dynamic_cast (type, expression, tf_warning_or_error);
6492 break;
6493 case RID_STATCAST:
6494 postfix_expression
6495 = build_static_cast (type, expression, tf_warning_or_error);
6496 break;
6497 case RID_REINTCAST:
6498 postfix_expression
6499 = build_reinterpret_cast (type, expression,
6500 tf_warning_or_error);
6501 break;
6502 case RID_CONSTCAST:
6503 postfix_expression
6504 = build_const_cast (type, expression, tf_warning_or_error);
6505 break;
6506 default:
6507 gcc_unreachable ();
6510 /* Construct a location e.g. :
6511 reinterpret_cast <int *> (expr)
6512 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6513 ranging from the start of the "*_cast" token to the final closing
6514 paren, with the caret at the start. */
6515 location_t cp_cast_loc = make_location (start_loc, start_loc, end_loc);
6516 postfix_expression.set_location (cp_cast_loc);
6518 break;
6520 case RID_TYPEID:
6522 tree type;
6523 const char *saved_message;
6524 bool saved_in_type_id_in_expr_p;
6526 /* Consume the `typeid' token. */
6527 cp_lexer_consume_token (parser->lexer);
6528 /* Look for the `(' token. */
6529 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6530 /* Types cannot be defined in a `typeid' expression. */
6531 saved_message = parser->type_definition_forbidden_message;
6532 parser->type_definition_forbidden_message
6533 = G_("types may not be defined in a %<typeid%> expression");
6534 /* We can't be sure yet whether we're looking at a type-id or an
6535 expression. */
6536 cp_parser_parse_tentatively (parser);
6537 /* Try a type-id first. */
6538 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6539 parser->in_type_id_in_expr_p = true;
6540 type = cp_parser_type_id (parser);
6541 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6542 /* Look for the `)' token. Otherwise, we can't be sure that
6543 we're not looking at an expression: consider `typeid (int
6544 (3))', for example. */
6545 cp_token *close_paren = cp_parser_require (parser, CPP_CLOSE_PAREN,
6546 RT_CLOSE_PAREN);
6547 /* If all went well, simply lookup the type-id. */
6548 if (cp_parser_parse_definitely (parser))
6549 postfix_expression = get_typeid (type, tf_warning_or_error);
6550 /* Otherwise, fall back to the expression variant. */
6551 else
6553 tree expression;
6555 /* Look for an expression. */
6556 expression = cp_parser_expression (parser, & idk);
6557 /* Compute its typeid. */
6558 postfix_expression = build_typeid (expression, tf_warning_or_error);
6559 /* Look for the `)' token. */
6560 close_paren
6561 = cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6563 /* Restore the saved message. */
6564 parser->type_definition_forbidden_message = saved_message;
6565 /* `typeid' may not appear in an integral constant expression. */
6566 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
6567 postfix_expression = error_mark_node;
6569 /* Construct a location e.g. :
6570 typeid (expr)
6571 ^~~~~~~~~~~~~
6572 ranging from the start of the "typeid" token to the final closing
6573 paren, with the caret at the start. */
6574 if (close_paren)
6576 location_t typeid_loc
6577 = make_location (start_loc, start_loc, close_paren->location);
6578 postfix_expression.set_location (typeid_loc);
6581 break;
6583 case RID_TYPENAME:
6585 tree type;
6586 /* The syntax permitted here is the same permitted for an
6587 elaborated-type-specifier. */
6588 ++parser->prevent_constrained_type_specifiers;
6589 type = cp_parser_elaborated_type_specifier (parser,
6590 /*is_friend=*/false,
6591 /*is_declaration=*/false);
6592 --parser->prevent_constrained_type_specifiers;
6593 postfix_expression = cp_parser_functional_cast (parser, type);
6595 break;
6597 case RID_CILK_SPAWN:
6599 location_t cilk_spawn_loc
6600 = cp_lexer_peek_token (parser->lexer)->location;
6601 cp_lexer_consume_token (parser->lexer);
6602 token = cp_lexer_peek_token (parser->lexer);
6603 if (token->type == CPP_SEMICOLON)
6605 error_at (token->location, "%<_Cilk_spawn%> must be followed by "
6606 "an expression");
6607 postfix_expression = error_mark_node;
6608 break;
6610 else if (!current_function_decl)
6612 error_at (token->location, "%<_Cilk_spawn%> may only be used "
6613 "inside a function");
6614 postfix_expression = error_mark_node;
6615 break;
6617 else
6619 /* Consecutive _Cilk_spawns are not allowed in a statement. */
6620 saved_in_statement = parser->in_statement;
6621 parser->in_statement |= IN_CILK_SPAWN;
6623 cfun->calls_cilk_spawn = 1;
6624 postfix_expression =
6625 cp_parser_postfix_expression (parser, false, false,
6626 false, false, &idk);
6627 if (!flag_cilkplus)
6629 error_at (token->location, "-fcilkplus must be enabled to use"
6630 " %<_Cilk_spawn%>");
6631 cfun->calls_cilk_spawn = 0;
6633 else if (saved_in_statement & IN_CILK_SPAWN)
6635 error_at (token->location, "consecutive %<_Cilk_spawn%> keywords "
6636 "are not permitted");
6637 postfix_expression = error_mark_node;
6638 cfun->calls_cilk_spawn = 0;
6640 else
6642 location_t loc = postfix_expression.get_location ();
6643 postfix_expression = build_cilk_spawn (token->location,
6644 postfix_expression);
6645 /* Build a location of the form:
6646 _Cilk_spawn expr
6647 ~~~~~~~~~~~~^~~~
6648 with caret at the expr, ranging from the start of the
6649 _Cilk_spawn token to the end of the expression. */
6650 location_t combined_loc =
6651 make_location (loc, cilk_spawn_loc, get_finish (loc));
6652 postfix_expression.set_location (combined_loc);
6653 if (postfix_expression != error_mark_node)
6654 SET_EXPR_LOCATION (postfix_expression, input_location);
6655 parser->in_statement = parser->in_statement & ~IN_CILK_SPAWN;
6657 break;
6660 case RID_ADDRESSOF:
6661 case RID_BUILTIN_SHUFFLE:
6662 case RID_BUILTIN_LAUNDER:
6664 vec<tree, va_gc> *vec;
6665 unsigned int i;
6666 tree p;
6668 cp_lexer_consume_token (parser->lexer);
6669 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
6670 /*cast_p=*/false, /*allow_expansion_p=*/true,
6671 /*non_constant_p=*/NULL);
6672 if (vec == NULL)
6674 postfix_expression = error_mark_node;
6675 break;
6678 FOR_EACH_VEC_ELT (*vec, i, p)
6679 mark_exp_read (p);
6681 switch (keyword)
6683 case RID_ADDRESSOF:
6684 if (vec->length () == 1)
6685 postfix_expression
6686 = cp_build_addressof (loc, (*vec)[0], tf_warning_or_error);
6687 else
6689 error_at (loc, "wrong number of arguments to "
6690 "%<__builtin_addressof%>");
6691 postfix_expression = error_mark_node;
6693 break;
6695 case RID_BUILTIN_LAUNDER:
6696 if (vec->length () == 1)
6697 postfix_expression = finish_builtin_launder (loc, (*vec)[0],
6698 tf_warning_or_error);
6699 else
6701 error_at (loc, "wrong number of arguments to "
6702 "%<__builtin_launder%>");
6703 postfix_expression = error_mark_node;
6705 break;
6707 case RID_BUILTIN_SHUFFLE:
6708 if (vec->length () == 2)
6709 postfix_expression
6710 = build_x_vec_perm_expr (loc, (*vec)[0], NULL_TREE,
6711 (*vec)[1], tf_warning_or_error);
6712 else if (vec->length () == 3)
6713 postfix_expression
6714 = build_x_vec_perm_expr (loc, (*vec)[0], (*vec)[1],
6715 (*vec)[2], tf_warning_or_error);
6716 else
6718 error_at (loc, "wrong number of arguments to "
6719 "%<__builtin_shuffle%>");
6720 postfix_expression = error_mark_node;
6722 break;
6724 default:
6725 gcc_unreachable ();
6727 break;
6730 default:
6732 tree type;
6734 /* If the next thing is a simple-type-specifier, we may be
6735 looking at a functional cast. We could also be looking at
6736 an id-expression. So, we try the functional cast, and if
6737 that doesn't work we fall back to the primary-expression. */
6738 cp_parser_parse_tentatively (parser);
6739 /* Look for the simple-type-specifier. */
6740 ++parser->prevent_constrained_type_specifiers;
6741 type = cp_parser_simple_type_specifier (parser,
6742 /*decl_specs=*/NULL,
6743 CP_PARSER_FLAGS_NONE);
6744 --parser->prevent_constrained_type_specifiers;
6745 /* Parse the cast itself. */
6746 if (!cp_parser_error_occurred (parser))
6747 postfix_expression
6748 = cp_parser_functional_cast (parser, type);
6749 /* If that worked, we're done. */
6750 if (cp_parser_parse_definitely (parser))
6751 break;
6753 /* If the functional-cast didn't work out, try a
6754 compound-literal. */
6755 if (cp_parser_allow_gnu_extensions_p (parser)
6756 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6758 cp_expr initializer = NULL_TREE;
6760 cp_parser_parse_tentatively (parser);
6762 /* Avoid calling cp_parser_type_id pointlessly, see comment
6763 in cp_parser_cast_expression about c++/29234. */
6764 if (!cp_parser_compound_literal_p (parser))
6765 cp_parser_simulate_error (parser);
6766 else
6768 /* Parse the type. */
6769 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6770 parser->in_type_id_in_expr_p = true;
6771 type = cp_parser_type_id (parser);
6772 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6773 /* Look for the `)'. */
6774 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6777 /* If things aren't going well, there's no need to
6778 keep going. */
6779 if (!cp_parser_error_occurred (parser))
6781 bool non_constant_p;
6782 /* Parse the brace-enclosed initializer list. */
6783 initializer = cp_parser_braced_list (parser,
6784 &non_constant_p);
6786 /* If that worked, we're definitely looking at a
6787 compound-literal expression. */
6788 if (cp_parser_parse_definitely (parser))
6790 /* Warn the user that a compound literal is not
6791 allowed in standard C++. */
6792 pedwarn (input_location, OPT_Wpedantic,
6793 "ISO C++ forbids compound-literals");
6794 /* For simplicity, we disallow compound literals in
6795 constant-expressions. We could
6796 allow compound literals of integer type, whose
6797 initializer was a constant, in constant
6798 expressions. Permitting that usage, as a further
6799 extension, would not change the meaning of any
6800 currently accepted programs. (Of course, as
6801 compound literals are not part of ISO C++, the
6802 standard has nothing to say.) */
6803 if (cp_parser_non_integral_constant_expression (parser,
6804 NIC_NCC))
6806 postfix_expression = error_mark_node;
6807 break;
6809 /* Form the representation of the compound-literal. */
6810 postfix_expression
6811 = finish_compound_literal (type, initializer,
6812 tf_warning_or_error, fcl_c99);
6813 postfix_expression.set_location (initializer.get_location ());
6814 break;
6818 /* It must be a primary-expression. */
6819 postfix_expression
6820 = cp_parser_primary_expression (parser, address_p, cast_p,
6821 /*template_arg_p=*/false,
6822 decltype_p,
6823 &idk);
6825 break;
6828 /* Note that we don't need to worry about calling build_cplus_new on a
6829 class-valued CALL_EXPR in decltype when it isn't the end of the
6830 postfix-expression; unary_complex_lvalue will take care of that for
6831 all these cases. */
6833 /* Keep looping until the postfix-expression is complete. */
6834 while (true)
6836 if (idk == CP_ID_KIND_UNQUALIFIED
6837 && identifier_p (postfix_expression)
6838 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6839 /* It is not a Koenig lookup function call. */
6840 postfix_expression
6841 = unqualified_name_lookup_error (postfix_expression);
6843 /* Peek at the next token. */
6844 token = cp_lexer_peek_token (parser->lexer);
6846 switch (token->type)
6848 case CPP_OPEN_SQUARE:
6849 if (cp_next_tokens_can_be_std_attribute_p (parser))
6851 cp_parser_error (parser,
6852 "two consecutive %<[%> shall "
6853 "only introduce an attribute");
6854 return error_mark_node;
6856 postfix_expression
6857 = cp_parser_postfix_open_square_expression (parser,
6858 postfix_expression,
6859 false,
6860 decltype_p);
6861 postfix_expression.set_range (start_loc,
6862 postfix_expression.get_location ());
6864 idk = CP_ID_KIND_NONE;
6865 is_member_access = false;
6866 break;
6868 case CPP_OPEN_PAREN:
6869 /* postfix-expression ( expression-list [opt] ) */
6871 bool koenig_p;
6872 bool is_builtin_constant_p;
6873 bool saved_integral_constant_expression_p = false;
6874 bool saved_non_integral_constant_expression_p = false;
6875 tsubst_flags_t complain = complain_flags (decltype_p);
6876 vec<tree, va_gc> *args;
6877 location_t close_paren_loc = UNKNOWN_LOCATION;
6879 is_member_access = false;
6881 is_builtin_constant_p
6882 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
6883 if (is_builtin_constant_p)
6885 /* The whole point of __builtin_constant_p is to allow
6886 non-constant expressions to appear as arguments. */
6887 saved_integral_constant_expression_p
6888 = parser->integral_constant_expression_p;
6889 saved_non_integral_constant_expression_p
6890 = parser->non_integral_constant_expression_p;
6891 parser->integral_constant_expression_p = false;
6893 args = (cp_parser_parenthesized_expression_list
6894 (parser, non_attr,
6895 /*cast_p=*/false, /*allow_expansion_p=*/true,
6896 /*non_constant_p=*/NULL,
6897 /*close_paren_loc=*/&close_paren_loc));
6898 if (is_builtin_constant_p)
6900 parser->integral_constant_expression_p
6901 = saved_integral_constant_expression_p;
6902 parser->non_integral_constant_expression_p
6903 = saved_non_integral_constant_expression_p;
6906 if (args == NULL)
6908 postfix_expression = error_mark_node;
6909 break;
6912 /* Function calls are not permitted in
6913 constant-expressions. */
6914 if (! builtin_valid_in_constant_expr_p (postfix_expression)
6915 && cp_parser_non_integral_constant_expression (parser,
6916 NIC_FUNC_CALL))
6918 postfix_expression = error_mark_node;
6919 release_tree_vector (args);
6920 break;
6923 koenig_p = false;
6924 if (idk == CP_ID_KIND_UNQUALIFIED
6925 || idk == CP_ID_KIND_TEMPLATE_ID)
6927 if (identifier_p (postfix_expression))
6929 if (!args->is_empty ())
6931 koenig_p = true;
6932 if (!any_type_dependent_arguments_p (args))
6933 postfix_expression
6934 = perform_koenig_lookup (postfix_expression, args,
6935 complain);
6937 else
6938 postfix_expression
6939 = unqualified_fn_lookup_error (postfix_expression);
6941 /* We do not perform argument-dependent lookup if
6942 normal lookup finds a non-function, in accordance
6943 with the expected resolution of DR 218. */
6944 else if (!args->is_empty ()
6945 && is_overloaded_fn (postfix_expression))
6947 tree fn = get_first_fn (postfix_expression);
6948 fn = STRIP_TEMPLATE (fn);
6950 /* Do not do argument dependent lookup if regular
6951 lookup finds a member function or a block-scope
6952 function declaration. [basic.lookup.argdep]/3 */
6953 if (!DECL_FUNCTION_MEMBER_P (fn)
6954 && !DECL_LOCAL_FUNCTION_P (fn))
6956 koenig_p = true;
6957 if (!any_type_dependent_arguments_p (args))
6958 postfix_expression
6959 = perform_koenig_lookup (postfix_expression, args,
6960 complain);
6965 if (TREE_CODE (postfix_expression) == FUNCTION_DECL
6966 && DECL_BUILT_IN_CLASS (postfix_expression) == BUILT_IN_NORMAL
6967 && DECL_FUNCTION_CODE (postfix_expression) == BUILT_IN_MEMSET
6968 && vec_safe_length (args) == 3)
6970 tree arg0 = (*args)[0];
6971 tree arg1 = (*args)[1];
6972 tree arg2 = (*args)[2];
6973 int literal_mask = ((!!integer_zerop (arg1) << 1)
6974 | (!!integer_zerop (arg2) << 2));
6975 if (TREE_CODE (arg2) == CONST_DECL)
6976 arg2 = DECL_INITIAL (arg2);
6977 warn_for_memset (input_location, arg0, arg2, literal_mask);
6980 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
6982 tree instance = TREE_OPERAND (postfix_expression, 0);
6983 tree fn = TREE_OPERAND (postfix_expression, 1);
6985 if (processing_template_decl
6986 && (type_dependent_object_expression_p (instance)
6987 || (!BASELINK_P (fn)
6988 && TREE_CODE (fn) != FIELD_DECL)
6989 || type_dependent_expression_p (fn)
6990 || any_type_dependent_arguments_p (args)))
6992 maybe_generic_this_capture (instance, fn);
6993 postfix_expression
6994 = build_min_nt_call_vec (postfix_expression, args);
6995 release_tree_vector (args);
6996 break;
6999 if (BASELINK_P (fn))
7001 postfix_expression
7002 = (build_new_method_call
7003 (instance, fn, &args, NULL_TREE,
7004 (idk == CP_ID_KIND_QUALIFIED
7005 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
7006 : LOOKUP_NORMAL),
7007 /*fn_p=*/NULL,
7008 complain));
7010 else
7011 postfix_expression
7012 = finish_call_expr (postfix_expression, &args,
7013 /*disallow_virtual=*/false,
7014 /*koenig_p=*/false,
7015 complain);
7017 else if (TREE_CODE (postfix_expression) == OFFSET_REF
7018 || TREE_CODE (postfix_expression) == MEMBER_REF
7019 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
7020 postfix_expression = (build_offset_ref_call_from_tree
7021 (postfix_expression, &args,
7022 complain));
7023 else if (idk == CP_ID_KIND_QUALIFIED)
7024 /* A call to a static class member, or a namespace-scope
7025 function. */
7026 postfix_expression
7027 = finish_call_expr (postfix_expression, &args,
7028 /*disallow_virtual=*/true,
7029 koenig_p,
7030 complain);
7031 else
7032 /* All other function calls. */
7033 postfix_expression
7034 = finish_call_expr (postfix_expression, &args,
7035 /*disallow_virtual=*/false,
7036 koenig_p,
7037 complain);
7039 if (close_paren_loc != UNKNOWN_LOCATION)
7041 location_t combined_loc = make_location (token->location,
7042 start_loc,
7043 close_paren_loc);
7044 postfix_expression.set_location (combined_loc);
7047 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
7048 idk = CP_ID_KIND_NONE;
7050 release_tree_vector (args);
7052 break;
7054 case CPP_DOT:
7055 case CPP_DEREF:
7056 /* postfix-expression . template [opt] id-expression
7057 postfix-expression . pseudo-destructor-name
7058 postfix-expression -> template [opt] id-expression
7059 postfix-expression -> pseudo-destructor-name */
7061 /* Consume the `.' or `->' operator. */
7062 cp_lexer_consume_token (parser->lexer);
7064 postfix_expression
7065 = cp_parser_postfix_dot_deref_expression (parser, token->type,
7066 postfix_expression,
7067 false, &idk, loc);
7069 is_member_access = true;
7070 break;
7072 case CPP_PLUS_PLUS:
7073 /* postfix-expression ++ */
7074 /* Consume the `++' token. */
7075 cp_lexer_consume_token (parser->lexer);
7076 /* Generate a representation for the complete expression. */
7077 postfix_expression
7078 = finish_increment_expr (postfix_expression,
7079 POSTINCREMENT_EXPR);
7080 /* Increments may not appear in constant-expressions. */
7081 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
7082 postfix_expression = error_mark_node;
7083 idk = CP_ID_KIND_NONE;
7084 is_member_access = false;
7085 break;
7087 case CPP_MINUS_MINUS:
7088 /* postfix-expression -- */
7089 /* Consume the `--' token. */
7090 cp_lexer_consume_token (parser->lexer);
7091 /* Generate a representation for the complete expression. */
7092 postfix_expression
7093 = finish_increment_expr (postfix_expression,
7094 POSTDECREMENT_EXPR);
7095 /* Decrements may not appear in constant-expressions. */
7096 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
7097 postfix_expression = error_mark_node;
7098 idk = CP_ID_KIND_NONE;
7099 is_member_access = false;
7100 break;
7102 default:
7103 if (pidk_return != NULL)
7104 * pidk_return = idk;
7105 if (member_access_only_p)
7106 return is_member_access
7107 ? postfix_expression
7108 : cp_expr (error_mark_node);
7109 else
7110 return postfix_expression;
7114 /* We should never get here. */
7115 gcc_unreachable ();
7116 return error_mark_node;
7119 /* This function parses Cilk Plus array notations. If a normal array expr. is
7120 parsed then the array index is passed back to the caller through *INIT_INDEX
7121 and the function returns a NULL_TREE. If array notation expr. is parsed,
7122 then *INIT_INDEX is ignored by the caller and the function returns
7123 a tree of type ARRAY_NOTATION_REF. If some error occurred it returns
7124 error_mark_node. */
7126 static tree
7127 cp_parser_array_notation (location_t loc, cp_parser *parser, tree *init_index,
7128 tree array_value)
7130 cp_token *token = NULL;
7131 tree length_index, stride = NULL_TREE, value_tree, array_type;
7132 if (!array_value || array_value == error_mark_node)
7134 cp_parser_skip_to_end_of_statement (parser);
7135 return error_mark_node;
7138 array_type = TREE_TYPE (array_value);
7140 bool saved_colon_corrects = parser->colon_corrects_to_scope_p;
7141 parser->colon_corrects_to_scope_p = false;
7142 token = cp_lexer_peek_token (parser->lexer);
7144 if (!token)
7146 cp_parser_error (parser, "expected %<:%> or numeral");
7147 return error_mark_node;
7149 else if (token->type == CPP_COLON)
7151 /* Consume the ':'. */
7152 cp_lexer_consume_token (parser->lexer);
7154 /* If we are here, then we have a case like this A[:]. */
7155 if (cp_lexer_peek_token (parser->lexer)->type != CPP_CLOSE_SQUARE)
7157 cp_parser_error (parser, "expected %<]%>");
7158 cp_parser_skip_to_end_of_statement (parser);
7159 return error_mark_node;
7161 *init_index = NULL_TREE;
7162 stride = NULL_TREE;
7163 length_index = NULL_TREE;
7165 else
7167 /* If we are here, then there are three valid possibilities:
7168 1. ARRAY [ EXP ]
7169 2. ARRAY [ EXP : EXP ]
7170 3. ARRAY [ EXP : EXP : EXP ] */
7172 *init_index = cp_parser_expression (parser);
7173 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
7175 /* This indicates that we have a normal array expression. */
7176 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7177 return NULL_TREE;
7180 /* Consume the ':'. */
7181 cp_lexer_consume_token (parser->lexer);
7182 length_index = cp_parser_expression (parser);
7183 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7185 cp_lexer_consume_token (parser->lexer);
7186 stride = cp_parser_expression (parser);
7189 parser->colon_corrects_to_scope_p = saved_colon_corrects;
7191 if (*init_index == error_mark_node || length_index == error_mark_node
7192 || stride == error_mark_node || array_type == error_mark_node)
7194 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_SQUARE)
7195 cp_lexer_consume_token (parser->lexer);
7196 return error_mark_node;
7198 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7200 value_tree = build_array_notation_ref (loc, array_value, *init_index,
7201 length_index, stride, array_type);
7202 return value_tree;
7205 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7206 by cp_parser_builtin_offsetof. We're looking for
7208 postfix-expression [ expression ]
7209 postfix-expression [ braced-init-list ] (C++11)
7211 FOR_OFFSETOF is set if we're being called in that context, which
7212 changes how we deal with integer constant expressions. */
7214 static tree
7215 cp_parser_postfix_open_square_expression (cp_parser *parser,
7216 tree postfix_expression,
7217 bool for_offsetof,
7218 bool decltype_p)
7220 tree index = NULL_TREE;
7221 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7222 bool saved_greater_than_is_operator_p;
7224 /* Consume the `[' token. */
7225 cp_lexer_consume_token (parser->lexer);
7227 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
7228 parser->greater_than_is_operator_p = true;
7230 /* Parse the index expression. */
7231 /* ??? For offsetof, there is a question of what to allow here. If
7232 offsetof is not being used in an integral constant expression context,
7233 then we *could* get the right answer by computing the value at runtime.
7234 If we are in an integral constant expression context, then we might
7235 could accept any constant expression; hard to say without analysis.
7236 Rather than open the barn door too wide right away, allow only integer
7237 constant expressions here. */
7238 if (for_offsetof)
7239 index = cp_parser_constant_expression (parser);
7240 else
7242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7244 bool expr_nonconst_p;
7245 cp_lexer_set_source_position (parser->lexer);
7246 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7247 index = cp_parser_braced_list (parser, &expr_nonconst_p);
7248 if (flag_cilkplus
7249 && cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
7251 error_at (cp_lexer_peek_token (parser->lexer)->location,
7252 "braced list index is not allowed with array "
7253 "notation");
7254 cp_parser_skip_to_end_of_statement (parser);
7255 return error_mark_node;
7258 else if (flag_cilkplus)
7260 /* Here are have these two options:
7261 ARRAY[EXP : EXP] - Array notation expr with default
7262 stride of 1.
7263 ARRAY[EXP : EXP : EXP] - Array Notation with user-defined
7264 stride. */
7265 tree an_exp = cp_parser_array_notation (loc, parser, &index,
7266 postfix_expression);
7267 if (an_exp)
7268 return an_exp;
7270 else
7271 index = cp_parser_expression (parser);
7274 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
7276 /* Look for the closing `]'. */
7277 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7279 /* Build the ARRAY_REF. */
7280 postfix_expression = grok_array_decl (loc, postfix_expression,
7281 index, decltype_p);
7283 /* When not doing offsetof, array references are not permitted in
7284 constant-expressions. */
7285 if (!for_offsetof
7286 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
7287 postfix_expression = error_mark_node;
7289 return postfix_expression;
7292 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
7293 by cp_parser_builtin_offsetof. We're looking for
7295 postfix-expression . template [opt] id-expression
7296 postfix-expression . pseudo-destructor-name
7297 postfix-expression -> template [opt] id-expression
7298 postfix-expression -> pseudo-destructor-name
7300 FOR_OFFSETOF is set if we're being called in that context. That sorta
7301 limits what of the above we'll actually accept, but nevermind.
7302 TOKEN_TYPE is the "." or "->" token, which will already have been
7303 removed from the stream. */
7305 static tree
7306 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
7307 enum cpp_ttype token_type,
7308 cp_expr postfix_expression,
7309 bool for_offsetof, cp_id_kind *idk,
7310 location_t location)
7312 tree name;
7313 bool dependent_p;
7314 bool pseudo_destructor_p;
7315 tree scope = NULL_TREE;
7316 location_t start_loc = postfix_expression.get_start ();
7318 /* If this is a `->' operator, dereference the pointer. */
7319 if (token_type == CPP_DEREF)
7320 postfix_expression = build_x_arrow (location, postfix_expression,
7321 tf_warning_or_error);
7322 /* Check to see whether or not the expression is type-dependent and
7323 not the current instantiation. */
7324 dependent_p = type_dependent_object_expression_p (postfix_expression);
7325 /* The identifier following the `->' or `.' is not qualified. */
7326 parser->scope = NULL_TREE;
7327 parser->qualifying_scope = NULL_TREE;
7328 parser->object_scope = NULL_TREE;
7329 *idk = CP_ID_KIND_NONE;
7331 /* Enter the scope corresponding to the type of the object
7332 given by the POSTFIX_EXPRESSION. */
7333 if (!dependent_p)
7335 scope = TREE_TYPE (postfix_expression);
7336 /* According to the standard, no expression should ever have
7337 reference type. Unfortunately, we do not currently match
7338 the standard in this respect in that our internal representation
7339 of an expression may have reference type even when the standard
7340 says it does not. Therefore, we have to manually obtain the
7341 underlying type here. */
7342 scope = non_reference (scope);
7343 /* The type of the POSTFIX_EXPRESSION must be complete. */
7344 /* Unlike the object expression in other contexts, *this is not
7345 required to be of complete type for purposes of class member
7346 access (5.2.5) outside the member function body. */
7347 if (postfix_expression != current_class_ref
7348 && scope != error_mark_node
7349 && !(processing_template_decl
7350 && current_class_type
7351 && (same_type_ignoring_top_level_qualifiers_p
7352 (scope, current_class_type))))
7354 scope = complete_type (scope);
7355 if (!COMPLETE_TYPE_P (scope)
7356 /* Avoid clobbering e.g. OVERLOADs or DECLs. */
7357 && EXPR_P (postfix_expression))
7359 /* In a template, be permissive by treating an object expression
7360 of incomplete type as dependent (after a pedwarn). */
7361 diagnostic_t kind = (processing_template_decl
7362 ? DK_PEDWARN
7363 : DK_ERROR);
7364 cxx_incomplete_type_diagnostic
7365 (location_of (postfix_expression),
7366 postfix_expression, scope, kind);
7367 if (processing_template_decl)
7369 dependent_p = true;
7370 scope = TREE_TYPE (postfix_expression) = NULL_TREE;
7375 if (!dependent_p)
7377 /* Let the name lookup machinery know that we are processing a
7378 class member access expression. */
7379 parser->context->object_type = scope;
7380 /* If something went wrong, we want to be able to discern that case,
7381 as opposed to the case where there was no SCOPE due to the type
7382 of expression being dependent. */
7383 if (!scope)
7384 scope = error_mark_node;
7385 /* If the SCOPE was erroneous, make the various semantic analysis
7386 functions exit quickly -- and without issuing additional error
7387 messages. */
7388 if (scope == error_mark_node)
7389 postfix_expression = error_mark_node;
7393 if (dependent_p)
7394 /* Tell cp_parser_lookup_name that there was an object, even though it's
7395 type-dependent. */
7396 parser->context->object_type = unknown_type_node;
7398 /* Assume this expression is not a pseudo-destructor access. */
7399 pseudo_destructor_p = false;
7401 /* If the SCOPE is a scalar type, then, if this is a valid program,
7402 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
7403 is type dependent, it can be pseudo-destructor-name or something else.
7404 Try to parse it as pseudo-destructor-name first. */
7405 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
7407 tree s;
7408 tree type;
7410 cp_parser_parse_tentatively (parser);
7411 /* Parse the pseudo-destructor-name. */
7412 s = NULL_TREE;
7413 cp_parser_pseudo_destructor_name (parser, postfix_expression,
7414 &s, &type);
7415 if (dependent_p
7416 && (cp_parser_error_occurred (parser)
7417 || !SCALAR_TYPE_P (type)))
7418 cp_parser_abort_tentative_parse (parser);
7419 else if (cp_parser_parse_definitely (parser))
7421 pseudo_destructor_p = true;
7422 postfix_expression
7423 = finish_pseudo_destructor_expr (postfix_expression,
7424 s, type, location);
7428 if (!pseudo_destructor_p)
7430 /* If the SCOPE is not a scalar type, we are looking at an
7431 ordinary class member access expression, rather than a
7432 pseudo-destructor-name. */
7433 bool template_p;
7434 cp_token *token = cp_lexer_peek_token (parser->lexer);
7435 /* Parse the id-expression. */
7436 name = (cp_parser_id_expression
7437 (parser,
7438 cp_parser_optional_template_keyword (parser),
7439 /*check_dependency_p=*/true,
7440 &template_p,
7441 /*declarator_p=*/false,
7442 /*optional_p=*/false));
7443 /* In general, build a SCOPE_REF if the member name is qualified.
7444 However, if the name was not dependent and has already been
7445 resolved; there is no need to build the SCOPE_REF. For example;
7447 struct X { void f(); };
7448 template <typename T> void f(T* t) { t->X::f(); }
7450 Even though "t" is dependent, "X::f" is not and has been resolved
7451 to a BASELINK; there is no need to include scope information. */
7453 /* But we do need to remember that there was an explicit scope for
7454 virtual function calls. */
7455 if (parser->scope)
7456 *idk = CP_ID_KIND_QUALIFIED;
7458 /* If the name is a template-id that names a type, we will get a
7459 TYPE_DECL here. That is invalid code. */
7460 if (TREE_CODE (name) == TYPE_DECL)
7462 error_at (token->location, "invalid use of %qD", name);
7463 postfix_expression = error_mark_node;
7465 else
7467 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
7469 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
7471 error_at (token->location, "%<%D::%D%> is not a class member",
7472 parser->scope, name);
7473 postfix_expression = error_mark_node;
7475 else
7476 name = build_qualified_name (/*type=*/NULL_TREE,
7477 parser->scope,
7478 name,
7479 template_p);
7480 parser->scope = NULL_TREE;
7481 parser->qualifying_scope = NULL_TREE;
7482 parser->object_scope = NULL_TREE;
7484 if (parser->scope && name && BASELINK_P (name))
7485 adjust_result_of_qualified_name_lookup
7486 (name, parser->scope, scope);
7487 postfix_expression
7488 = finish_class_member_access_expr (postfix_expression, name,
7489 template_p,
7490 tf_warning_or_error);
7491 /* Build a location e.g.:
7492 ptr->access_expr
7493 ~~~^~~~~~~~~~~~~
7494 where the caret is at the deref token, ranging from
7495 the start of postfix_expression to the end of the access expr. */
7496 location_t end_loc
7497 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
7498 location_t combined_loc
7499 = make_location (input_location, start_loc, end_loc);
7500 protected_set_expr_location (postfix_expression, combined_loc);
7504 /* We no longer need to look up names in the scope of the object on
7505 the left-hand side of the `.' or `->' operator. */
7506 parser->context->object_type = NULL_TREE;
7508 /* Outside of offsetof, these operators may not appear in
7509 constant-expressions. */
7510 if (!for_offsetof
7511 && (cp_parser_non_integral_constant_expression
7512 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
7513 postfix_expression = error_mark_node;
7515 return postfix_expression;
7518 /* Parse a parenthesized expression-list.
7520 expression-list:
7521 assignment-expression
7522 expression-list, assignment-expression
7524 attribute-list:
7525 expression-list
7526 identifier
7527 identifier, expression-list
7529 CAST_P is true if this expression is the target of a cast.
7531 ALLOW_EXPANSION_P is true if this expression allows expansion of an
7532 argument pack.
7534 Returns a vector of trees. Each element is a representation of an
7535 assignment-expression. NULL is returned if the ( and or ) are
7536 missing. An empty, but allocated, vector is returned on no
7537 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
7538 if we are parsing an attribute list for an attribute that wants a
7539 plain identifier argument, normal_attr for an attribute that wants
7540 an expression, or non_attr if we aren't parsing an attribute list. If
7541 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
7542 not all of the expressions in the list were constant.
7543 If CLOSE_PAREN_LOC is non-NULL, and no errors occur, then *CLOSE_PAREN_LOC
7544 will be written to with the location of the closing parenthesis. If
7545 an error occurs, it may or may not be written to. */
7547 static vec<tree, va_gc> *
7548 cp_parser_parenthesized_expression_list (cp_parser* parser,
7549 int is_attribute_list,
7550 bool cast_p,
7551 bool allow_expansion_p,
7552 bool *non_constant_p,
7553 location_t *close_paren_loc)
7555 vec<tree, va_gc> *expression_list;
7556 bool fold_expr_p = is_attribute_list != non_attr;
7557 tree identifier = NULL_TREE;
7558 bool saved_greater_than_is_operator_p;
7560 /* Assume all the expressions will be constant. */
7561 if (non_constant_p)
7562 *non_constant_p = false;
7564 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
7565 return NULL;
7567 expression_list = make_tree_vector ();
7569 /* Within a parenthesized expression, a `>' token is always
7570 the greater-than operator. */
7571 saved_greater_than_is_operator_p
7572 = parser->greater_than_is_operator_p;
7573 parser->greater_than_is_operator_p = true;
7575 /* Consume expressions until there are no more. */
7576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7577 while (true)
7579 tree expr;
7581 /* At the beginning of attribute lists, check to see if the
7582 next token is an identifier. */
7583 if (is_attribute_list == id_attr
7584 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
7586 cp_token *token;
7588 /* Consume the identifier. */
7589 token = cp_lexer_consume_token (parser->lexer);
7590 /* Save the identifier. */
7591 identifier = token->u.value;
7593 else
7595 bool expr_non_constant_p;
7597 /* Parse the next assignment-expression. */
7598 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7600 /* A braced-init-list. */
7601 cp_lexer_set_source_position (parser->lexer);
7602 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7603 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7604 if (non_constant_p && expr_non_constant_p)
7605 *non_constant_p = true;
7607 else if (non_constant_p)
7609 expr = (cp_parser_constant_expression
7610 (parser, /*allow_non_constant_p=*/true,
7611 &expr_non_constant_p));
7612 if (expr_non_constant_p)
7613 *non_constant_p = true;
7615 else
7616 expr = cp_parser_assignment_expression (parser, /*pidk=*/NULL,
7617 cast_p);
7619 if (fold_expr_p)
7620 expr = instantiate_non_dependent_expr (expr);
7622 /* If we have an ellipsis, then this is an expression
7623 expansion. */
7624 if (allow_expansion_p
7625 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
7627 /* Consume the `...'. */
7628 cp_lexer_consume_token (parser->lexer);
7630 /* Build the argument pack. */
7631 expr = make_pack_expansion (expr);
7634 /* Add it to the list. We add error_mark_node
7635 expressions to the list, so that we can still tell if
7636 the correct form for a parenthesized expression-list
7637 is found. That gives better errors. */
7638 vec_safe_push (expression_list, expr);
7640 if (expr == error_mark_node)
7641 goto skip_comma;
7644 /* After the first item, attribute lists look the same as
7645 expression lists. */
7646 is_attribute_list = non_attr;
7648 get_comma:;
7649 /* If the next token isn't a `,', then we are done. */
7650 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7651 break;
7653 /* Otherwise, consume the `,' and keep going. */
7654 cp_lexer_consume_token (parser->lexer);
7657 if (close_paren_loc)
7658 *close_paren_loc = cp_lexer_peek_token (parser->lexer)->location;
7660 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
7662 int ending;
7664 skip_comma:;
7665 /* We try and resync to an unnested comma, as that will give the
7666 user better diagnostics. */
7667 ending = cp_parser_skip_to_closing_parenthesis (parser,
7668 /*recovering=*/true,
7669 /*or_comma=*/true,
7670 /*consume_paren=*/true);
7671 if (ending < 0)
7672 goto get_comma;
7673 if (!ending)
7675 parser->greater_than_is_operator_p
7676 = saved_greater_than_is_operator_p;
7677 return NULL;
7681 parser->greater_than_is_operator_p
7682 = saved_greater_than_is_operator_p;
7684 if (identifier)
7685 vec_safe_insert (expression_list, 0, identifier);
7687 return expression_list;
7690 /* Parse a pseudo-destructor-name.
7692 pseudo-destructor-name:
7693 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
7694 :: [opt] nested-name-specifier template template-id :: ~ type-name
7695 :: [opt] nested-name-specifier [opt] ~ type-name
7697 If either of the first two productions is used, sets *SCOPE to the
7698 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
7699 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
7700 or ERROR_MARK_NODE if the parse fails. */
7702 static void
7703 cp_parser_pseudo_destructor_name (cp_parser* parser,
7704 tree object,
7705 tree* scope,
7706 tree* type)
7708 bool nested_name_specifier_p;
7710 /* Handle ~auto. */
7711 if (cp_lexer_next_token_is (parser->lexer, CPP_COMPL)
7712 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_AUTO)
7713 && !type_dependent_expression_p (object))
7715 if (cxx_dialect < cxx14)
7716 pedwarn (input_location, 0,
7717 "%<~auto%> only available with "
7718 "-std=c++14 or -std=gnu++14");
7719 cp_lexer_consume_token (parser->lexer);
7720 cp_lexer_consume_token (parser->lexer);
7721 *scope = NULL_TREE;
7722 *type = TREE_TYPE (object);
7723 return;
7726 /* Assume that things will not work out. */
7727 *type = error_mark_node;
7729 /* Look for the optional `::' operator. */
7730 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
7731 /* Look for the optional nested-name-specifier. */
7732 nested_name_specifier_p
7733 = (cp_parser_nested_name_specifier_opt (parser,
7734 /*typename_keyword_p=*/false,
7735 /*check_dependency_p=*/true,
7736 /*type_p=*/false,
7737 /*is_declaration=*/false)
7738 != NULL_TREE);
7739 /* Now, if we saw a nested-name-specifier, we might be doing the
7740 second production. */
7741 if (nested_name_specifier_p
7742 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
7744 /* Consume the `template' keyword. */
7745 cp_lexer_consume_token (parser->lexer);
7746 /* Parse the template-id. */
7747 cp_parser_template_id (parser,
7748 /*template_keyword_p=*/true,
7749 /*check_dependency_p=*/false,
7750 class_type,
7751 /*is_declaration=*/true);
7752 /* Look for the `::' token. */
7753 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7755 /* If the next token is not a `~', then there might be some
7756 additional qualification. */
7757 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
7759 /* At this point, we're looking for "type-name :: ~". The type-name
7760 must not be a class-name, since this is a pseudo-destructor. So,
7761 it must be either an enum-name, or a typedef-name -- both of which
7762 are just identifiers. So, we peek ahead to check that the "::"
7763 and "~" tokens are present; if they are not, then we can avoid
7764 calling type_name. */
7765 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
7766 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
7767 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
7769 cp_parser_error (parser, "non-scalar type");
7770 return;
7773 /* Look for the type-name. */
7774 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
7775 if (*scope == error_mark_node)
7776 return;
7778 /* Look for the `::' token. */
7779 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
7781 else
7782 *scope = NULL_TREE;
7784 /* Look for the `~'. */
7785 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
7787 /* Once we see the ~, this has to be a pseudo-destructor. */
7788 if (!processing_template_decl && !cp_parser_error_occurred (parser))
7789 cp_parser_commit_to_topmost_tentative_parse (parser);
7791 /* Look for the type-name again. We are not responsible for
7792 checking that it matches the first type-name. */
7793 *type = TREE_TYPE (cp_parser_nonclass_name (parser));
7796 /* Parse a unary-expression.
7798 unary-expression:
7799 postfix-expression
7800 ++ cast-expression
7801 -- cast-expression
7802 unary-operator cast-expression
7803 sizeof unary-expression
7804 sizeof ( type-id )
7805 alignof ( type-id ) [C++0x]
7806 new-expression
7807 delete-expression
7809 GNU Extensions:
7811 unary-expression:
7812 __extension__ cast-expression
7813 __alignof__ unary-expression
7814 __alignof__ ( type-id )
7815 alignof unary-expression [C++0x]
7816 __real__ cast-expression
7817 __imag__ cast-expression
7818 && identifier
7819 sizeof ( type-id ) { initializer-list , [opt] }
7820 alignof ( type-id ) { initializer-list , [opt] } [C++0x]
7821 __alignof__ ( type-id ) { initializer-list , [opt] }
7823 ADDRESS_P is true iff the unary-expression is appearing as the
7824 operand of the `&' operator. CAST_P is true if this expression is
7825 the target of a cast.
7827 Returns a representation of the expression. */
7829 static cp_expr
7830 cp_parser_unary_expression (cp_parser *parser, cp_id_kind * pidk,
7831 bool address_p, bool cast_p, bool decltype_p)
7833 cp_token *token;
7834 enum tree_code unary_operator;
7836 /* Peek at the next token. */
7837 token = cp_lexer_peek_token (parser->lexer);
7838 /* Some keywords give away the kind of expression. */
7839 if (token->type == CPP_KEYWORD)
7841 enum rid keyword = token->keyword;
7843 switch (keyword)
7845 case RID_ALIGNOF:
7846 case RID_SIZEOF:
7848 tree operand, ret;
7849 enum tree_code op;
7850 location_t start_loc = token->location;
7852 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
7853 /* Consume the token. */
7854 cp_lexer_consume_token (parser->lexer);
7855 /* Parse the operand. */
7856 operand = cp_parser_sizeof_operand (parser, keyword);
7858 if (TYPE_P (operand))
7859 ret = cxx_sizeof_or_alignof_type (operand, op, true);
7860 else
7862 /* ISO C++ defines alignof only with types, not with
7863 expressions. So pedwarn if alignof is used with a non-
7864 type expression. However, __alignof__ is ok. */
7865 if (id_equal (token->u.value, "alignof"))
7866 pedwarn (token->location, OPT_Wpedantic,
7867 "ISO C++ does not allow %<alignof%> "
7868 "with a non-type");
7870 ret = cxx_sizeof_or_alignof_expr (operand, op, true);
7872 /* For SIZEOF_EXPR, just issue diagnostics, but keep
7873 SIZEOF_EXPR with the original operand. */
7874 if (op == SIZEOF_EXPR && ret != error_mark_node)
7876 if (TREE_CODE (ret) != SIZEOF_EXPR || TYPE_P (operand))
7878 if (!processing_template_decl && TYPE_P (operand))
7880 ret = build_min (SIZEOF_EXPR, size_type_node,
7881 build1 (NOP_EXPR, operand,
7882 error_mark_node));
7883 SIZEOF_EXPR_TYPE_P (ret) = 1;
7885 else
7886 ret = build_min (SIZEOF_EXPR, size_type_node, operand);
7887 TREE_SIDE_EFFECTS (ret) = 0;
7888 TREE_READONLY (ret) = 1;
7892 /* Construct a location e.g. :
7893 alignof (expr)
7894 ^~~~~~~~~~~~~~
7895 with start == caret at the start of the "alignof"/"sizeof"
7896 token, with the endpoint at the final closing paren. */
7897 location_t finish_loc
7898 = cp_lexer_previous_token (parser->lexer)->location;
7899 location_t compound_loc
7900 = make_location (start_loc, start_loc, finish_loc);
7902 cp_expr ret_expr (ret);
7903 ret_expr.set_location (compound_loc);
7904 return ret_expr;
7907 case RID_NEW:
7908 return cp_parser_new_expression (parser);
7910 case RID_DELETE:
7911 return cp_parser_delete_expression (parser);
7913 case RID_EXTENSION:
7915 /* The saved value of the PEDANTIC flag. */
7916 int saved_pedantic;
7917 tree expr;
7919 /* Save away the PEDANTIC flag. */
7920 cp_parser_extension_opt (parser, &saved_pedantic);
7921 /* Parse the cast-expression. */
7922 expr = cp_parser_simple_cast_expression (parser);
7923 /* Restore the PEDANTIC flag. */
7924 pedantic = saved_pedantic;
7926 return expr;
7929 case RID_REALPART:
7930 case RID_IMAGPART:
7932 tree expression;
7934 /* Consume the `__real__' or `__imag__' token. */
7935 cp_lexer_consume_token (parser->lexer);
7936 /* Parse the cast-expression. */
7937 expression = cp_parser_simple_cast_expression (parser);
7938 /* Create the complete representation. */
7939 return build_x_unary_op (token->location,
7940 (keyword == RID_REALPART
7941 ? REALPART_EXPR : IMAGPART_EXPR),
7942 expression,
7943 tf_warning_or_error);
7945 break;
7947 case RID_TRANSACTION_ATOMIC:
7948 case RID_TRANSACTION_RELAXED:
7949 return cp_parser_transaction_expression (parser, keyword);
7951 case RID_NOEXCEPT:
7953 tree expr;
7954 const char *saved_message;
7955 bool saved_integral_constant_expression_p;
7956 bool saved_non_integral_constant_expression_p;
7957 bool saved_greater_than_is_operator_p;
7959 cp_lexer_consume_token (parser->lexer);
7960 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7962 saved_message = parser->type_definition_forbidden_message;
7963 parser->type_definition_forbidden_message
7964 = G_("types may not be defined in %<noexcept%> expressions");
7966 saved_integral_constant_expression_p
7967 = parser->integral_constant_expression_p;
7968 saved_non_integral_constant_expression_p
7969 = parser->non_integral_constant_expression_p;
7970 parser->integral_constant_expression_p = false;
7972 saved_greater_than_is_operator_p
7973 = parser->greater_than_is_operator_p;
7974 parser->greater_than_is_operator_p = true;
7976 ++cp_unevaluated_operand;
7977 ++c_inhibit_evaluation_warnings;
7978 ++cp_noexcept_operand;
7979 expr = cp_parser_expression (parser);
7980 --cp_noexcept_operand;
7981 --c_inhibit_evaluation_warnings;
7982 --cp_unevaluated_operand;
7984 parser->greater_than_is_operator_p
7985 = saved_greater_than_is_operator_p;
7987 parser->integral_constant_expression_p
7988 = saved_integral_constant_expression_p;
7989 parser->non_integral_constant_expression_p
7990 = saved_non_integral_constant_expression_p;
7992 parser->type_definition_forbidden_message = saved_message;
7994 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7995 return finish_noexcept_expr (expr, tf_warning_or_error);
7998 default:
7999 break;
8003 /* Look for the `:: new' and `:: delete', which also signal the
8004 beginning of a new-expression, or delete-expression,
8005 respectively. If the next token is `::', then it might be one of
8006 these. */
8007 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
8009 enum rid keyword;
8011 /* See if the token after the `::' is one of the keywords in
8012 which we're interested. */
8013 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
8014 /* If it's `new', we have a new-expression. */
8015 if (keyword == RID_NEW)
8016 return cp_parser_new_expression (parser);
8017 /* Similarly, for `delete'. */
8018 else if (keyword == RID_DELETE)
8019 return cp_parser_delete_expression (parser);
8022 /* Look for a unary operator. */
8023 unary_operator = cp_parser_unary_operator (token);
8024 /* The `++' and `--' operators can be handled similarly, even though
8025 they are not technically unary-operators in the grammar. */
8026 if (unary_operator == ERROR_MARK)
8028 if (token->type == CPP_PLUS_PLUS)
8029 unary_operator = PREINCREMENT_EXPR;
8030 else if (token->type == CPP_MINUS_MINUS)
8031 unary_operator = PREDECREMENT_EXPR;
8032 /* Handle the GNU address-of-label extension. */
8033 else if (cp_parser_allow_gnu_extensions_p (parser)
8034 && token->type == CPP_AND_AND)
8036 tree identifier;
8037 tree expression;
8038 location_t start_loc = token->location;
8040 /* Consume the '&&' token. */
8041 cp_lexer_consume_token (parser->lexer);
8042 /* Look for the identifier. */
8043 location_t finish_loc
8044 = get_finish (cp_lexer_peek_token (parser->lexer)->location);
8045 identifier = cp_parser_identifier (parser);
8046 /* Construct a location of the form:
8047 &&label
8048 ^~~~~~~
8049 with caret==start at the "&&", finish at the end of the label. */
8050 location_t combined_loc
8051 = make_location (start_loc, start_loc, finish_loc);
8052 /* Create an expression representing the address. */
8053 expression = finish_label_address_expr (identifier, combined_loc);
8054 if (cp_parser_non_integral_constant_expression (parser,
8055 NIC_ADDR_LABEL))
8056 expression = error_mark_node;
8057 return expression;
8060 if (unary_operator != ERROR_MARK)
8062 cp_expr cast_expression;
8063 cp_expr expression = error_mark_node;
8064 non_integral_constant non_constant_p = NIC_NONE;
8065 location_t loc = token->location;
8066 tsubst_flags_t complain = complain_flags (decltype_p);
8068 /* Consume the operator token. */
8069 token = cp_lexer_consume_token (parser->lexer);
8070 enum cpp_ttype op_ttype = cp_lexer_peek_token (parser->lexer)->type;
8072 /* Parse the cast-expression. */
8073 cast_expression
8074 = cp_parser_cast_expression (parser,
8075 unary_operator == ADDR_EXPR,
8076 /*cast_p=*/false,
8077 /*decltype*/false,
8078 pidk);
8080 /* Make a location:
8081 OP_TOKEN CAST_EXPRESSION
8082 ^~~~~~~~~~~~~~~~~~~~~~~~~
8083 with start==caret at the operator token, and
8084 extending to the end of the cast_expression. */
8085 loc = make_location (loc, loc, cast_expression.get_finish ());
8087 /* Now, build an appropriate representation. */
8088 switch (unary_operator)
8090 case INDIRECT_REF:
8091 non_constant_p = NIC_STAR;
8092 expression = build_x_indirect_ref (loc, cast_expression,
8093 RO_UNARY_STAR,
8094 complain);
8095 /* TODO: build_x_indirect_ref does not always honor the
8096 location, so ensure it is set. */
8097 expression.set_location (loc);
8098 break;
8100 case ADDR_EXPR:
8101 non_constant_p = NIC_ADDR;
8102 /* Fall through. */
8103 case BIT_NOT_EXPR:
8104 expression = build_x_unary_op (loc, unary_operator,
8105 cast_expression,
8106 complain);
8107 /* TODO: build_x_unary_op does not always honor the location,
8108 so ensure it is set. */
8109 expression.set_location (loc);
8110 break;
8112 case PREINCREMENT_EXPR:
8113 case PREDECREMENT_EXPR:
8114 non_constant_p = unary_operator == PREINCREMENT_EXPR
8115 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
8116 /* Fall through. */
8117 case NEGATE_EXPR:
8118 /* Immediately fold negation of a constant, unless the constant is 0
8119 (since -0 == 0) or it would overflow. */
8120 if (unary_operator == NEGATE_EXPR && op_ttype == CPP_NUMBER
8121 && CONSTANT_CLASS_P (cast_expression)
8122 && !integer_zerop (cast_expression)
8123 && !TREE_OVERFLOW (cast_expression))
8125 tree folded = fold_build1 (unary_operator,
8126 TREE_TYPE (cast_expression),
8127 cast_expression);
8128 if (CONSTANT_CLASS_P (folded) && !TREE_OVERFLOW (folded))
8130 expression = cp_expr (folded, loc);
8131 break;
8134 /* Fall through. */
8135 case UNARY_PLUS_EXPR:
8136 case TRUTH_NOT_EXPR:
8137 expression = finish_unary_op_expr (loc, unary_operator,
8138 cast_expression, complain);
8139 break;
8141 default:
8142 gcc_unreachable ();
8145 if (non_constant_p != NIC_NONE
8146 && cp_parser_non_integral_constant_expression (parser,
8147 non_constant_p))
8148 expression = error_mark_node;
8150 return expression;
8153 return cp_parser_postfix_expression (parser, address_p, cast_p,
8154 /*member_access_only_p=*/false,
8155 decltype_p,
8156 pidk);
8159 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
8160 unary-operator, the corresponding tree code is returned. */
8162 static enum tree_code
8163 cp_parser_unary_operator (cp_token* token)
8165 switch (token->type)
8167 case CPP_MULT:
8168 return INDIRECT_REF;
8170 case CPP_AND:
8171 return ADDR_EXPR;
8173 case CPP_PLUS:
8174 return UNARY_PLUS_EXPR;
8176 case CPP_MINUS:
8177 return NEGATE_EXPR;
8179 case CPP_NOT:
8180 return TRUTH_NOT_EXPR;
8182 case CPP_COMPL:
8183 return BIT_NOT_EXPR;
8185 default:
8186 return ERROR_MARK;
8190 /* Parse a new-expression.
8192 new-expression:
8193 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
8194 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
8196 Returns a representation of the expression. */
8198 static tree
8199 cp_parser_new_expression (cp_parser* parser)
8201 bool global_scope_p;
8202 vec<tree, va_gc> *placement;
8203 tree type;
8204 vec<tree, va_gc> *initializer;
8205 tree nelts = NULL_TREE;
8206 tree ret;
8208 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
8210 /* Look for the optional `::' operator. */
8211 global_scope_p
8212 = (cp_parser_global_scope_opt (parser,
8213 /*current_scope_valid_p=*/false)
8214 != NULL_TREE);
8215 /* Look for the `new' operator. */
8216 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
8217 /* There's no easy way to tell a new-placement from the
8218 `( type-id )' construct. */
8219 cp_parser_parse_tentatively (parser);
8220 /* Look for a new-placement. */
8221 placement = cp_parser_new_placement (parser);
8222 /* If that didn't work out, there's no new-placement. */
8223 if (!cp_parser_parse_definitely (parser))
8225 if (placement != NULL)
8226 release_tree_vector (placement);
8227 placement = NULL;
8230 /* If the next token is a `(', then we have a parenthesized
8231 type-id. */
8232 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8234 cp_token *token;
8235 const char *saved_message = parser->type_definition_forbidden_message;
8237 /* Consume the `('. */
8238 cp_lexer_consume_token (parser->lexer);
8240 /* Parse the type-id. */
8241 parser->type_definition_forbidden_message
8242 = G_("types may not be defined in a new-expression");
8244 type_id_in_expr_sentinel s (parser);
8245 type = cp_parser_type_id (parser);
8247 parser->type_definition_forbidden_message = saved_message;
8249 /* Look for the closing `)'. */
8250 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8251 token = cp_lexer_peek_token (parser->lexer);
8252 /* There should not be a direct-new-declarator in this production,
8253 but GCC used to allowed this, so we check and emit a sensible error
8254 message for this case. */
8255 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8257 error_at (token->location,
8258 "array bound forbidden after parenthesized type-id");
8259 inform (token->location,
8260 "try removing the parentheses around the type-id");
8261 cp_parser_direct_new_declarator (parser);
8264 /* Otherwise, there must be a new-type-id. */
8265 else
8266 type = cp_parser_new_type_id (parser, &nelts);
8268 /* If the next token is a `(' or '{', then we have a new-initializer. */
8269 cp_token *token = cp_lexer_peek_token (parser->lexer);
8270 if (token->type == CPP_OPEN_PAREN
8271 || token->type == CPP_OPEN_BRACE)
8272 initializer = cp_parser_new_initializer (parser);
8273 else
8274 initializer = NULL;
8276 /* A new-expression may not appear in an integral constant
8277 expression. */
8278 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
8279 ret = error_mark_node;
8280 /* 5.3.4/2: "If the auto type-specifier appears in the type-specifier-seq
8281 of a new-type-id or type-id of a new-expression, the new-expression shall
8282 contain a new-initializer of the form ( assignment-expression )".
8283 Additionally, consistently with the spirit of DR 1467, we want to accept
8284 'new auto { 2 }' too. */
8285 else if ((ret = type_uses_auto (type))
8286 && !CLASS_PLACEHOLDER_TEMPLATE (ret)
8287 && (vec_safe_length (initializer) != 1
8288 || (BRACE_ENCLOSED_INITIALIZER_P ((*initializer)[0])
8289 && CONSTRUCTOR_NELTS ((*initializer)[0]) != 1)))
8291 error_at (token->location,
8292 "initialization of new-expression for type %<auto%> "
8293 "requires exactly one element");
8294 ret = error_mark_node;
8296 else
8298 /* Construct a location e.g.:
8299 ptr = new int[100]
8300 ^~~~~~~~~~~~
8301 with caret == start at the start of the "new" token, and the end
8302 at the end of the final token we consumed. */
8303 cp_token *end_tok = cp_lexer_previous_token (parser->lexer);
8304 location_t end_loc = get_finish (end_tok->location);
8305 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
8307 /* Create a representation of the new-expression. */
8308 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
8309 tf_warning_or_error);
8310 protected_set_expr_location (ret, combined_loc);
8313 if (placement != NULL)
8314 release_tree_vector (placement);
8315 if (initializer != NULL)
8316 release_tree_vector (initializer);
8318 return ret;
8321 /* Parse a new-placement.
8323 new-placement:
8324 ( expression-list )
8326 Returns the same representation as for an expression-list. */
8328 static vec<tree, va_gc> *
8329 cp_parser_new_placement (cp_parser* parser)
8331 vec<tree, va_gc> *expression_list;
8333 /* Parse the expression-list. */
8334 expression_list = (cp_parser_parenthesized_expression_list
8335 (parser, non_attr, /*cast_p=*/false,
8336 /*allow_expansion_p=*/true,
8337 /*non_constant_p=*/NULL));
8339 if (expression_list && expression_list->is_empty ())
8340 error ("expected expression-list or type-id");
8342 return expression_list;
8345 /* Parse a new-type-id.
8347 new-type-id:
8348 type-specifier-seq new-declarator [opt]
8350 Returns the TYPE allocated. If the new-type-id indicates an array
8351 type, *NELTS is set to the number of elements in the last array
8352 bound; the TYPE will not include the last array bound. */
8354 static tree
8355 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
8357 cp_decl_specifier_seq type_specifier_seq;
8358 cp_declarator *new_declarator;
8359 cp_declarator *declarator;
8360 cp_declarator *outer_declarator;
8361 const char *saved_message;
8363 /* The type-specifier sequence must not contain type definitions.
8364 (It cannot contain declarations of new types either, but if they
8365 are not definitions we will catch that because they are not
8366 complete.) */
8367 saved_message = parser->type_definition_forbidden_message;
8368 parser->type_definition_forbidden_message
8369 = G_("types may not be defined in a new-type-id");
8370 /* Parse the type-specifier-seq. */
8371 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
8372 /*is_trailing_return=*/false,
8373 &type_specifier_seq);
8374 /* Restore the old message. */
8375 parser->type_definition_forbidden_message = saved_message;
8377 if (type_specifier_seq.type == error_mark_node)
8378 return error_mark_node;
8380 /* Parse the new-declarator. */
8381 new_declarator = cp_parser_new_declarator_opt (parser);
8383 /* Determine the number of elements in the last array dimension, if
8384 any. */
8385 *nelts = NULL_TREE;
8386 /* Skip down to the last array dimension. */
8387 declarator = new_declarator;
8388 outer_declarator = NULL;
8389 while (declarator && (declarator->kind == cdk_pointer
8390 || declarator->kind == cdk_ptrmem))
8392 outer_declarator = declarator;
8393 declarator = declarator->declarator;
8395 while (declarator
8396 && declarator->kind == cdk_array
8397 && declarator->declarator
8398 && declarator->declarator->kind == cdk_array)
8400 outer_declarator = declarator;
8401 declarator = declarator->declarator;
8404 if (declarator && declarator->kind == cdk_array)
8406 *nelts = declarator->u.array.bounds;
8407 if (*nelts == error_mark_node)
8408 *nelts = integer_one_node;
8410 if (outer_declarator)
8411 outer_declarator->declarator = declarator->declarator;
8412 else
8413 new_declarator = NULL;
8416 return groktypename (&type_specifier_seq, new_declarator, false);
8419 /* Parse an (optional) new-declarator.
8421 new-declarator:
8422 ptr-operator new-declarator [opt]
8423 direct-new-declarator
8425 Returns the declarator. */
8427 static cp_declarator *
8428 cp_parser_new_declarator_opt (cp_parser* parser)
8430 enum tree_code code;
8431 tree type, std_attributes = NULL_TREE;
8432 cp_cv_quals cv_quals;
8434 /* We don't know if there's a ptr-operator next, or not. */
8435 cp_parser_parse_tentatively (parser);
8436 /* Look for a ptr-operator. */
8437 code = cp_parser_ptr_operator (parser, &type, &cv_quals, &std_attributes);
8438 /* If that worked, look for more new-declarators. */
8439 if (cp_parser_parse_definitely (parser))
8441 cp_declarator *declarator;
8443 /* Parse another optional declarator. */
8444 declarator = cp_parser_new_declarator_opt (parser);
8446 declarator = cp_parser_make_indirect_declarator
8447 (code, type, cv_quals, declarator, std_attributes);
8449 return declarator;
8452 /* If the next token is a `[', there is a direct-new-declarator. */
8453 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8454 return cp_parser_direct_new_declarator (parser);
8456 return NULL;
8459 /* Parse a direct-new-declarator.
8461 direct-new-declarator:
8462 [ expression ]
8463 direct-new-declarator [constant-expression]
8467 static cp_declarator *
8468 cp_parser_direct_new_declarator (cp_parser* parser)
8470 cp_declarator *declarator = NULL;
8472 while (true)
8474 tree expression;
8475 cp_token *token;
8477 /* Look for the opening `['. */
8478 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8480 token = cp_lexer_peek_token (parser->lexer);
8481 expression = cp_parser_expression (parser);
8482 /* The standard requires that the expression have integral
8483 type. DR 74 adds enumeration types. We believe that the
8484 real intent is that these expressions be handled like the
8485 expression in a `switch' condition, which also allows
8486 classes with a single conversion to integral or
8487 enumeration type. */
8488 if (!processing_template_decl)
8490 expression
8491 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
8492 expression,
8493 /*complain=*/true);
8494 if (!expression)
8496 error_at (token->location,
8497 "expression in new-declarator must have integral "
8498 "or enumeration type");
8499 expression = error_mark_node;
8503 /* Look for the closing `]'. */
8504 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8506 /* Add this bound to the declarator. */
8507 declarator = make_array_declarator (declarator, expression);
8509 /* If the next token is not a `[', then there are no more
8510 bounds. */
8511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
8512 break;
8515 return declarator;
8518 /* Parse a new-initializer.
8520 new-initializer:
8521 ( expression-list [opt] )
8522 braced-init-list
8524 Returns a representation of the expression-list. */
8526 static vec<tree, va_gc> *
8527 cp_parser_new_initializer (cp_parser* parser)
8529 vec<tree, va_gc> *expression_list;
8531 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8533 tree t;
8534 bool expr_non_constant_p;
8535 cp_lexer_set_source_position (parser->lexer);
8536 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8537 t = cp_parser_braced_list (parser, &expr_non_constant_p);
8538 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
8539 expression_list = make_tree_vector_single (t);
8541 else
8542 expression_list = (cp_parser_parenthesized_expression_list
8543 (parser, non_attr, /*cast_p=*/false,
8544 /*allow_expansion_p=*/true,
8545 /*non_constant_p=*/NULL));
8547 return expression_list;
8550 /* Parse a delete-expression.
8552 delete-expression:
8553 :: [opt] delete cast-expression
8554 :: [opt] delete [ ] cast-expression
8556 Returns a representation of the expression. */
8558 static tree
8559 cp_parser_delete_expression (cp_parser* parser)
8561 bool global_scope_p;
8562 bool array_p;
8563 tree expression;
8565 /* Look for the optional `::' operator. */
8566 global_scope_p
8567 = (cp_parser_global_scope_opt (parser,
8568 /*current_scope_valid_p=*/false)
8569 != NULL_TREE);
8570 /* Look for the `delete' keyword. */
8571 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
8572 /* See if the array syntax is in use. */
8573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
8575 /* Consume the `[' token. */
8576 cp_lexer_consume_token (parser->lexer);
8577 /* Look for the `]' token. */
8578 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8579 /* Remember that this is the `[]' construct. */
8580 array_p = true;
8582 else
8583 array_p = false;
8585 /* Parse the cast-expression. */
8586 expression = cp_parser_simple_cast_expression (parser);
8588 /* A delete-expression may not appear in an integral constant
8589 expression. */
8590 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
8591 return error_mark_node;
8593 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
8594 tf_warning_or_error);
8597 /* Returns 1 if TOKEN may start a cast-expression and isn't '++', '--',
8598 neither '[' in C++11; -1 if TOKEN is '++', '--', or '[' in C++11;
8599 0 otherwise. */
8601 static int
8602 cp_parser_tokens_start_cast_expression (cp_parser *parser)
8604 cp_token *token = cp_lexer_peek_token (parser->lexer);
8605 switch (token->type)
8607 case CPP_COMMA:
8608 case CPP_SEMICOLON:
8609 case CPP_QUERY:
8610 case CPP_COLON:
8611 case CPP_CLOSE_SQUARE:
8612 case CPP_CLOSE_PAREN:
8613 case CPP_CLOSE_BRACE:
8614 case CPP_OPEN_BRACE:
8615 case CPP_DOT:
8616 case CPP_DOT_STAR:
8617 case CPP_DEREF:
8618 case CPP_DEREF_STAR:
8619 case CPP_DIV:
8620 case CPP_MOD:
8621 case CPP_LSHIFT:
8622 case CPP_RSHIFT:
8623 case CPP_LESS:
8624 case CPP_GREATER:
8625 case CPP_LESS_EQ:
8626 case CPP_GREATER_EQ:
8627 case CPP_EQ_EQ:
8628 case CPP_NOT_EQ:
8629 case CPP_EQ:
8630 case CPP_MULT_EQ:
8631 case CPP_DIV_EQ:
8632 case CPP_MOD_EQ:
8633 case CPP_PLUS_EQ:
8634 case CPP_MINUS_EQ:
8635 case CPP_RSHIFT_EQ:
8636 case CPP_LSHIFT_EQ:
8637 case CPP_AND_EQ:
8638 case CPP_XOR_EQ:
8639 case CPP_OR_EQ:
8640 case CPP_XOR:
8641 case CPP_OR:
8642 case CPP_OR_OR:
8643 case CPP_EOF:
8644 case CPP_ELLIPSIS:
8645 return 0;
8647 case CPP_OPEN_PAREN:
8648 /* In ((type ()) () the last () isn't a valid cast-expression,
8649 so the whole must be parsed as postfix-expression. */
8650 return cp_lexer_peek_nth_token (parser->lexer, 2)->type
8651 != CPP_CLOSE_PAREN;
8653 case CPP_OPEN_SQUARE:
8654 /* '[' may start a primary-expression in obj-c++ and in C++11,
8655 as a lambda-expression, eg, '(void)[]{}'. */
8656 if (cxx_dialect >= cxx11)
8657 return -1;
8658 return c_dialect_objc ();
8660 case CPP_PLUS_PLUS:
8661 case CPP_MINUS_MINUS:
8662 /* '++' and '--' may or may not start a cast-expression:
8664 struct T { void operator++(int); };
8665 void f() { (T())++; }
8669 int a;
8670 (int)++a; */
8671 return -1;
8673 default:
8674 return 1;
8678 /* Try to find a legal C++-style cast to DST_TYPE for ORIG_EXPR, trying them
8679 in the order: const_cast, static_cast, reinterpret_cast.
8681 Don't suggest dynamic_cast.
8683 Return the first legal cast kind found, or NULL otherwise. */
8685 static const char *
8686 get_cast_suggestion (tree dst_type, tree orig_expr)
8688 tree trial;
8690 /* Reuse the parser logic by attempting to build the various kinds of
8691 cast, with "complain" disabled.
8692 Identify the first such cast that is valid. */
8694 /* Don't attempt to run such logic within template processing. */
8695 if (processing_template_decl)
8696 return NULL;
8698 /* First try const_cast. */
8699 trial = build_const_cast (dst_type, orig_expr, tf_none);
8700 if (trial != error_mark_node)
8701 return "const_cast";
8703 /* If that fails, try static_cast. */
8704 trial = build_static_cast (dst_type, orig_expr, tf_none);
8705 if (trial != error_mark_node)
8706 return "static_cast";
8708 /* Finally, try reinterpret_cast. */
8709 trial = build_reinterpret_cast (dst_type, orig_expr, tf_none);
8710 if (trial != error_mark_node)
8711 return "reinterpret_cast";
8713 /* No such cast possible. */
8714 return NULL;
8717 /* If -Wold-style-cast is enabled, add fix-its to RICHLOC,
8718 suggesting how to convert a C-style cast of the form:
8720 (DST_TYPE)ORIG_EXPR
8722 to a C++-style cast.
8724 The primary range of RICHLOC is asssumed to be that of the original
8725 expression. OPEN_PAREN_LOC and CLOSE_PAREN_LOC give the locations
8726 of the parens in the C-style cast. */
8728 static void
8729 maybe_add_cast_fixit (rich_location *rich_loc, location_t open_paren_loc,
8730 location_t close_paren_loc, tree orig_expr,
8731 tree dst_type)
8733 /* This function is non-trivial, so bail out now if the warning isn't
8734 going to be emitted. */
8735 if (!warn_old_style_cast)
8736 return;
8738 /* Try to find a legal C++ cast, trying them in order:
8739 const_cast, static_cast, reinterpret_cast. */
8740 const char *cast_suggestion = get_cast_suggestion (dst_type, orig_expr);
8741 if (!cast_suggestion)
8742 return;
8744 /* Replace the open paren with "CAST_SUGGESTION<". */
8745 pretty_printer pp;
8746 pp_printf (&pp, "%s<", cast_suggestion);
8747 rich_loc->add_fixit_replace (open_paren_loc, pp_formatted_text (&pp));
8749 /* Replace the close paren with "> (". */
8750 rich_loc->add_fixit_replace (close_paren_loc, "> (");
8752 /* Add a closing paren after the expr (the primary range of RICH_LOC). */
8753 rich_loc->add_fixit_insert_after (")");
8757 /* Parse a cast-expression.
8759 cast-expression:
8760 unary-expression
8761 ( type-id ) cast-expression
8763 ADDRESS_P is true iff the unary-expression is appearing as the
8764 operand of the `&' operator. CAST_P is true if this expression is
8765 the target of a cast.
8767 Returns a representation of the expression. */
8769 static cp_expr
8770 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
8771 bool decltype_p, cp_id_kind * pidk)
8773 /* If it's a `(', then we might be looking at a cast. */
8774 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8776 tree type = NULL_TREE;
8777 cp_expr expr (NULL_TREE);
8778 int cast_expression = 0;
8779 const char *saved_message;
8781 /* There's no way to know yet whether or not this is a cast.
8782 For example, `(int (3))' is a unary-expression, while `(int)
8783 3' is a cast. So, we resort to parsing tentatively. */
8784 cp_parser_parse_tentatively (parser);
8785 /* Types may not be defined in a cast. */
8786 saved_message = parser->type_definition_forbidden_message;
8787 parser->type_definition_forbidden_message
8788 = G_("types may not be defined in casts");
8789 /* Consume the `('. */
8790 cp_token *open_paren = cp_lexer_consume_token (parser->lexer);
8791 location_t open_paren_loc = open_paren->location;
8792 location_t close_paren_loc = UNKNOWN_LOCATION;
8794 /* A very tricky bit is that `(struct S) { 3 }' is a
8795 compound-literal (which we permit in C++ as an extension).
8796 But, that construct is not a cast-expression -- it is a
8797 postfix-expression. (The reason is that `(struct S) { 3 }.i'
8798 is legal; if the compound-literal were a cast-expression,
8799 you'd need an extra set of parentheses.) But, if we parse
8800 the type-id, and it happens to be a class-specifier, then we
8801 will commit to the parse at that point, because we cannot
8802 undo the action that is done when creating a new class. So,
8803 then we cannot back up and do a postfix-expression.
8805 Another tricky case is the following (c++/29234):
8807 struct S { void operator () (); };
8809 void foo ()
8811 ( S()() );
8814 As a type-id we parse the parenthesized S()() as a function
8815 returning a function, groktypename complains and we cannot
8816 back up in this case either.
8818 Therefore, we scan ahead to the closing `)', and check to see
8819 if the tokens after the `)' can start a cast-expression. Otherwise
8820 we are dealing with an unary-expression, a postfix-expression
8821 or something else.
8823 Yet another tricky case, in C++11, is the following (c++/54891):
8825 (void)[]{};
8827 The issue is that usually, besides the case of lambda-expressions,
8828 the parenthesized type-id cannot be followed by '[', and, eg, we
8829 want to parse '(C ())[2];' in parse/pr26997.C as unary-expression.
8830 Thus, if cp_parser_tokens_start_cast_expression returns -1, below
8831 we don't commit, we try a cast-expression, then an unary-expression.
8833 Save tokens so that we can put them back. */
8834 cp_lexer_save_tokens (parser->lexer);
8836 /* We may be looking at a cast-expression. */
8837 if (cp_parser_skip_to_closing_parenthesis (parser, false, false,
8838 /*consume_paren=*/true))
8839 cast_expression
8840 = cp_parser_tokens_start_cast_expression (parser);
8842 /* Roll back the tokens we skipped. */
8843 cp_lexer_rollback_tokens (parser->lexer);
8844 /* If we aren't looking at a cast-expression, simulate an error so
8845 that the call to cp_parser_error_occurred below returns true. */
8846 if (!cast_expression)
8847 cp_parser_simulate_error (parser);
8848 else
8850 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
8851 parser->in_type_id_in_expr_p = true;
8852 /* Look for the type-id. */
8853 type = cp_parser_type_id (parser);
8854 /* Look for the closing `)'. */
8855 cp_token *close_paren
8856 = cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8857 if (close_paren)
8858 close_paren_loc = close_paren->location;
8859 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
8862 /* Restore the saved message. */
8863 parser->type_definition_forbidden_message = saved_message;
8865 /* At this point this can only be either a cast or a
8866 parenthesized ctor such as `(T ())' that looks like a cast to
8867 function returning T. */
8868 if (!cp_parser_error_occurred (parser))
8870 /* Only commit if the cast-expression doesn't start with
8871 '++', '--', or '[' in C++11. */
8872 if (cast_expression > 0)
8873 cp_parser_commit_to_topmost_tentative_parse (parser);
8875 expr = cp_parser_cast_expression (parser,
8876 /*address_p=*/false,
8877 /*cast_p=*/true,
8878 /*decltype_p=*/false,
8879 pidk);
8881 if (cp_parser_parse_definitely (parser))
8883 /* Warn about old-style casts, if so requested. */
8884 if (warn_old_style_cast
8885 && !in_system_header_at (input_location)
8886 && !VOID_TYPE_P (type)
8887 && current_lang_name != lang_name_c)
8889 gcc_rich_location rich_loc (input_location);
8890 maybe_add_cast_fixit (&rich_loc, open_paren_loc, close_paren_loc,
8891 expr, type);
8892 warning_at_rich_loc (&rich_loc, OPT_Wold_style_cast,
8893 "use of old-style cast to %q#T", type);
8896 /* Only type conversions to integral or enumeration types
8897 can be used in constant-expressions. */
8898 if (!cast_valid_in_integral_constant_expression_p (type)
8899 && cp_parser_non_integral_constant_expression (parser,
8900 NIC_CAST))
8901 return error_mark_node;
8903 /* Perform the cast. */
8904 /* Make a location:
8905 (TYPE) EXPR
8906 ^~~~~~~~~~~
8907 with start==caret at the open paren, extending to the
8908 end of "expr". */
8909 location_t cast_loc = make_location (open_paren_loc,
8910 open_paren_loc,
8911 expr.get_finish ());
8912 expr = build_c_cast (cast_loc, type, expr);
8913 return expr;
8916 else
8917 cp_parser_abort_tentative_parse (parser);
8920 /* If we get here, then it's not a cast, so it must be a
8921 unary-expression. */
8922 return cp_parser_unary_expression (parser, pidk, address_p,
8923 cast_p, decltype_p);
8926 /* Parse a binary expression of the general form:
8928 pm-expression:
8929 cast-expression
8930 pm-expression .* cast-expression
8931 pm-expression ->* cast-expression
8933 multiplicative-expression:
8934 pm-expression
8935 multiplicative-expression * pm-expression
8936 multiplicative-expression / pm-expression
8937 multiplicative-expression % pm-expression
8939 additive-expression:
8940 multiplicative-expression
8941 additive-expression + multiplicative-expression
8942 additive-expression - multiplicative-expression
8944 shift-expression:
8945 additive-expression
8946 shift-expression << additive-expression
8947 shift-expression >> additive-expression
8949 relational-expression:
8950 shift-expression
8951 relational-expression < shift-expression
8952 relational-expression > shift-expression
8953 relational-expression <= shift-expression
8954 relational-expression >= shift-expression
8956 GNU Extension:
8958 relational-expression:
8959 relational-expression <? shift-expression
8960 relational-expression >? shift-expression
8962 equality-expression:
8963 relational-expression
8964 equality-expression == relational-expression
8965 equality-expression != relational-expression
8967 and-expression:
8968 equality-expression
8969 and-expression & equality-expression
8971 exclusive-or-expression:
8972 and-expression
8973 exclusive-or-expression ^ and-expression
8975 inclusive-or-expression:
8976 exclusive-or-expression
8977 inclusive-or-expression | exclusive-or-expression
8979 logical-and-expression:
8980 inclusive-or-expression
8981 logical-and-expression && inclusive-or-expression
8983 logical-or-expression:
8984 logical-and-expression
8985 logical-or-expression || logical-and-expression
8987 All these are implemented with a single function like:
8989 binary-expression:
8990 simple-cast-expression
8991 binary-expression <token> binary-expression
8993 CAST_P is true if this expression is the target of a cast.
8995 The binops_by_token map is used to get the tree codes for each <token> type.
8996 binary-expressions are associated according to a precedence table. */
8998 #define TOKEN_PRECEDENCE(token) \
8999 (((token->type == CPP_GREATER \
9000 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
9001 && !parser->greater_than_is_operator_p) \
9002 ? PREC_NOT_OPERATOR \
9003 : binops_by_token[token->type].prec)
9005 static cp_expr
9006 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9007 bool no_toplevel_fold_p,
9008 bool decltype_p,
9009 enum cp_parser_prec prec,
9010 cp_id_kind * pidk)
9012 cp_parser_expression_stack stack;
9013 cp_parser_expression_stack_entry *sp = &stack[0];
9014 cp_parser_expression_stack_entry current;
9015 cp_expr rhs;
9016 cp_token *token;
9017 enum tree_code rhs_type;
9018 enum cp_parser_prec new_prec, lookahead_prec;
9019 tree overload;
9021 /* Parse the first expression. */
9022 current.lhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9023 ? TRUTH_NOT_EXPR : ERROR_MARK);
9024 current.lhs = cp_parser_cast_expression (parser, /*address_p=*/false,
9025 cast_p, decltype_p, pidk);
9026 current.prec = prec;
9028 if (cp_parser_error_occurred (parser))
9029 return error_mark_node;
9031 for (;;)
9033 /* Get an operator token. */
9034 token = cp_lexer_peek_token (parser->lexer);
9036 if (warn_cxx11_compat
9037 && token->type == CPP_RSHIFT
9038 && !parser->greater_than_is_operator_p)
9040 if (warning_at (token->location, OPT_Wc__11_compat,
9041 "%<>>%> operator is treated"
9042 " as two right angle brackets in C++11"))
9043 inform (token->location,
9044 "suggest parentheses around %<>>%> expression");
9047 new_prec = TOKEN_PRECEDENCE (token);
9048 if (new_prec != PREC_NOT_OPERATOR
9049 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9050 /* This is a fold-expression; handle it later. */
9051 new_prec = PREC_NOT_OPERATOR;
9053 /* Popping an entry off the stack means we completed a subexpression:
9054 - either we found a token which is not an operator (`>' where it is not
9055 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
9056 will happen repeatedly;
9057 - or, we found an operator which has lower priority. This is the case
9058 where the recursive descent *ascends*, as in `3 * 4 + 5' after
9059 parsing `3 * 4'. */
9060 if (new_prec <= current.prec)
9062 if (sp == stack)
9063 break;
9064 else
9065 goto pop;
9068 get_rhs:
9069 current.tree_type = binops_by_token[token->type].tree_type;
9070 current.loc = token->location;
9072 /* We used the operator token. */
9073 cp_lexer_consume_token (parser->lexer);
9075 /* For "false && x" or "true || x", x will never be executed;
9076 disable warnings while evaluating it. */
9077 if (current.tree_type == TRUTH_ANDIF_EXPR)
9078 c_inhibit_evaluation_warnings +=
9079 cp_fully_fold (current.lhs) == truthvalue_false_node;
9080 else if (current.tree_type == TRUTH_ORIF_EXPR)
9081 c_inhibit_evaluation_warnings +=
9082 cp_fully_fold (current.lhs) == truthvalue_true_node;
9084 /* Extract another operand. It may be the RHS of this expression
9085 or the LHS of a new, higher priority expression. */
9086 rhs_type = (cp_lexer_next_token_is (parser->lexer, CPP_NOT)
9087 ? TRUTH_NOT_EXPR : ERROR_MARK);
9088 rhs = cp_parser_simple_cast_expression (parser);
9090 /* Get another operator token. Look up its precedence to avoid
9091 building a useless (immediately popped) stack entry for common
9092 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
9093 token = cp_lexer_peek_token (parser->lexer);
9094 lookahead_prec = TOKEN_PRECEDENCE (token);
9095 if (lookahead_prec != PREC_NOT_OPERATOR
9096 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9097 lookahead_prec = PREC_NOT_OPERATOR;
9098 if (lookahead_prec > new_prec)
9100 /* ... and prepare to parse the RHS of the new, higher priority
9101 expression. Since precedence levels on the stack are
9102 monotonically increasing, we do not have to care about
9103 stack overflows. */
9104 *sp = current;
9105 ++sp;
9106 current.lhs = rhs;
9107 current.lhs_type = rhs_type;
9108 current.prec = new_prec;
9109 new_prec = lookahead_prec;
9110 goto get_rhs;
9112 pop:
9113 lookahead_prec = new_prec;
9114 /* If the stack is not empty, we have parsed into LHS the right side
9115 (`4' in the example above) of an expression we had suspended.
9116 We can use the information on the stack to recover the LHS (`3')
9117 from the stack together with the tree code (`MULT_EXPR'), and
9118 the precedence of the higher level subexpression
9119 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
9120 which will be used to actually build the additive expression. */
9121 rhs = current.lhs;
9122 rhs_type = current.lhs_type;
9123 --sp;
9124 current = *sp;
9127 /* Undo the disabling of warnings done above. */
9128 if (current.tree_type == TRUTH_ANDIF_EXPR)
9129 c_inhibit_evaluation_warnings -=
9130 cp_fully_fold (current.lhs) == truthvalue_false_node;
9131 else if (current.tree_type == TRUTH_ORIF_EXPR)
9132 c_inhibit_evaluation_warnings -=
9133 cp_fully_fold (current.lhs) == truthvalue_true_node;
9135 if (warn_logical_not_paren
9136 && TREE_CODE_CLASS (current.tree_type) == tcc_comparison
9137 && current.lhs_type == TRUTH_NOT_EXPR
9138 /* Avoid warning for !!x == y. */
9139 && (TREE_CODE (current.lhs) != NE_EXPR
9140 || !integer_zerop (TREE_OPERAND (current.lhs, 1)))
9141 && (TREE_CODE (current.lhs) != TRUTH_NOT_EXPR
9142 || (TREE_CODE (TREE_OPERAND (current.lhs, 0)) != TRUTH_NOT_EXPR
9143 /* Avoid warning for !b == y where b is boolean. */
9144 && (TREE_TYPE (TREE_OPERAND (current.lhs, 0)) == NULL_TREE
9145 || (TREE_CODE (TREE_TYPE (TREE_OPERAND (current.lhs, 0)))
9146 != BOOLEAN_TYPE))))
9147 /* Avoid warning for !!b == y where b is boolean. */
9148 && (!DECL_P (current.lhs)
9149 || TREE_TYPE (current.lhs) == NULL_TREE
9150 || TREE_CODE (TREE_TYPE (current.lhs)) != BOOLEAN_TYPE))
9151 warn_logical_not_parentheses (current.loc, current.tree_type,
9152 current.lhs, maybe_constant_value (rhs));
9154 overload = NULL;
9156 location_t combined_loc = make_location (current.loc,
9157 current.lhs.get_start (),
9158 rhs.get_finish ());
9160 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
9161 ERROR_MARK for everything that is not a binary expression.
9162 This makes warn_about_parentheses miss some warnings that
9163 involve unary operators. For unary expressions we should
9164 pass the correct tree_code unless the unary expression was
9165 surrounded by parentheses.
9167 if (no_toplevel_fold_p
9168 && lookahead_prec <= current.prec
9169 && sp == stack)
9170 current.lhs = build2_loc (combined_loc,
9171 current.tree_type,
9172 TREE_CODE_CLASS (current.tree_type)
9173 == tcc_comparison
9174 ? boolean_type_node : TREE_TYPE (current.lhs),
9175 current.lhs, rhs);
9176 else
9178 current.lhs = build_x_binary_op (combined_loc, current.tree_type,
9179 current.lhs, current.lhs_type,
9180 rhs, rhs_type, &overload,
9181 complain_flags (decltype_p));
9182 /* TODO: build_x_binary_op doesn't always honor the location. */
9183 current.lhs.set_location (combined_loc);
9185 current.lhs_type = current.tree_type;
9187 /* If the binary operator required the use of an overloaded operator,
9188 then this expression cannot be an integral constant-expression.
9189 An overloaded operator can be used even if both operands are
9190 otherwise permissible in an integral constant-expression if at
9191 least one of the operands is of enumeration type. */
9193 if (overload
9194 && cp_parser_non_integral_constant_expression (parser,
9195 NIC_OVERLOADED))
9196 return error_mark_node;
9199 return current.lhs;
9202 static cp_expr
9203 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
9204 bool no_toplevel_fold_p,
9205 enum cp_parser_prec prec,
9206 cp_id_kind * pidk)
9208 return cp_parser_binary_expression (parser, cast_p, no_toplevel_fold_p,
9209 /*decltype*/false, prec, pidk);
9212 /* Parse the `? expression : assignment-expression' part of a
9213 conditional-expression. The LOGICAL_OR_EXPR is the
9214 logical-or-expression that started the conditional-expression.
9215 Returns a representation of the entire conditional-expression.
9217 This routine is used by cp_parser_assignment_expression.
9219 ? expression : assignment-expression
9221 GNU Extensions:
9223 ? : assignment-expression */
9225 static tree
9226 cp_parser_question_colon_clause (cp_parser* parser, cp_expr logical_or_expr)
9228 tree expr, folded_logical_or_expr = cp_fully_fold (logical_or_expr);
9229 cp_expr assignment_expr;
9230 struct cp_token *token;
9231 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9233 /* Consume the `?' token. */
9234 cp_lexer_consume_token (parser->lexer);
9235 token = cp_lexer_peek_token (parser->lexer);
9236 if (cp_parser_allow_gnu_extensions_p (parser)
9237 && token->type == CPP_COLON)
9239 pedwarn (token->location, OPT_Wpedantic,
9240 "ISO C++ does not allow ?: with omitted middle operand");
9241 /* Implicit true clause. */
9242 expr = NULL_TREE;
9243 c_inhibit_evaluation_warnings +=
9244 folded_logical_or_expr == truthvalue_true_node;
9245 warn_for_omitted_condop (token->location, logical_or_expr);
9247 else
9249 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9250 parser->colon_corrects_to_scope_p = false;
9251 /* Parse the expression. */
9252 c_inhibit_evaluation_warnings +=
9253 folded_logical_or_expr == truthvalue_false_node;
9254 expr = cp_parser_expression (parser);
9255 c_inhibit_evaluation_warnings +=
9256 ((folded_logical_or_expr == truthvalue_true_node)
9257 - (folded_logical_or_expr == truthvalue_false_node));
9258 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9261 /* The next token should be a `:'. */
9262 cp_parser_require (parser, CPP_COLON, RT_COLON);
9263 /* Parse the assignment-expression. */
9264 assignment_expr = cp_parser_assignment_expression (parser);
9265 c_inhibit_evaluation_warnings -=
9266 folded_logical_or_expr == truthvalue_true_node;
9268 /* Make a location:
9269 LOGICAL_OR_EXPR ? EXPR : ASSIGNMENT_EXPR
9270 ~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
9271 with the caret at the "?", ranging from the start of
9272 the logical_or_expr to the end of the assignment_expr. */
9273 loc = make_location (loc,
9274 logical_or_expr.get_start (),
9275 assignment_expr.get_finish ());
9277 /* Build the conditional-expression. */
9278 return build_x_conditional_expr (loc, logical_or_expr,
9279 expr,
9280 assignment_expr,
9281 tf_warning_or_error);
9284 /* Parse an assignment-expression.
9286 assignment-expression:
9287 conditional-expression
9288 logical-or-expression assignment-operator assignment_expression
9289 throw-expression
9291 CAST_P is true if this expression is the target of a cast.
9292 DECLTYPE_P is true if this expression is the operand of decltype.
9294 Returns a representation for the expression. */
9296 static cp_expr
9297 cp_parser_assignment_expression (cp_parser* parser, cp_id_kind * pidk,
9298 bool cast_p, bool decltype_p)
9300 cp_expr expr;
9302 /* If the next token is the `throw' keyword, then we're looking at
9303 a throw-expression. */
9304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
9305 expr = cp_parser_throw_expression (parser);
9306 /* Otherwise, it must be that we are looking at a
9307 logical-or-expression. */
9308 else
9310 /* Parse the binary expressions (logical-or-expression). */
9311 expr = cp_parser_binary_expression (parser, cast_p, false,
9312 decltype_p,
9313 PREC_NOT_OPERATOR, pidk);
9314 /* If the next token is a `?' then we're actually looking at a
9315 conditional-expression. */
9316 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
9317 return cp_parser_question_colon_clause (parser, expr);
9318 else
9320 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9322 /* If it's an assignment-operator, we're using the second
9323 production. */
9324 enum tree_code assignment_operator
9325 = cp_parser_assignment_operator_opt (parser);
9326 if (assignment_operator != ERROR_MARK)
9328 bool non_constant_p;
9330 /* Parse the right-hand side of the assignment. */
9331 cp_expr rhs = cp_parser_initializer_clause (parser,
9332 &non_constant_p);
9334 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
9335 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9337 /* An assignment may not appear in a
9338 constant-expression. */
9339 if (cp_parser_non_integral_constant_expression (parser,
9340 NIC_ASSIGNMENT))
9341 return error_mark_node;
9342 /* Build the assignment expression. Its default
9343 location:
9344 LHS = RHS
9345 ~~~~^~~~~
9346 is the location of the '=' token as the
9347 caret, ranging from the start of the lhs to the
9348 end of the rhs. */
9349 loc = make_location (loc,
9350 expr.get_start (),
9351 rhs.get_finish ());
9352 expr = build_x_modify_expr (loc, expr,
9353 assignment_operator,
9354 rhs,
9355 complain_flags (decltype_p));
9356 /* TODO: build_x_modify_expr doesn't honor the location,
9357 so we must set it here. */
9358 expr.set_location (loc);
9363 return expr;
9366 /* Parse an (optional) assignment-operator.
9368 assignment-operator: one of
9369 = *= /= %= += -= >>= <<= &= ^= |=
9371 GNU Extension:
9373 assignment-operator: one of
9374 <?= >?=
9376 If the next token is an assignment operator, the corresponding tree
9377 code is returned, and the token is consumed. For example, for
9378 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
9379 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
9380 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
9381 operator, ERROR_MARK is returned. */
9383 static enum tree_code
9384 cp_parser_assignment_operator_opt (cp_parser* parser)
9386 enum tree_code op;
9387 cp_token *token;
9389 /* Peek at the next token. */
9390 token = cp_lexer_peek_token (parser->lexer);
9392 switch (token->type)
9394 case CPP_EQ:
9395 op = NOP_EXPR;
9396 break;
9398 case CPP_MULT_EQ:
9399 op = MULT_EXPR;
9400 break;
9402 case CPP_DIV_EQ:
9403 op = TRUNC_DIV_EXPR;
9404 break;
9406 case CPP_MOD_EQ:
9407 op = TRUNC_MOD_EXPR;
9408 break;
9410 case CPP_PLUS_EQ:
9411 op = PLUS_EXPR;
9412 break;
9414 case CPP_MINUS_EQ:
9415 op = MINUS_EXPR;
9416 break;
9418 case CPP_RSHIFT_EQ:
9419 op = RSHIFT_EXPR;
9420 break;
9422 case CPP_LSHIFT_EQ:
9423 op = LSHIFT_EXPR;
9424 break;
9426 case CPP_AND_EQ:
9427 op = BIT_AND_EXPR;
9428 break;
9430 case CPP_XOR_EQ:
9431 op = BIT_XOR_EXPR;
9432 break;
9434 case CPP_OR_EQ:
9435 op = BIT_IOR_EXPR;
9436 break;
9438 default:
9439 /* Nothing else is an assignment operator. */
9440 op = ERROR_MARK;
9443 /* An operator followed by ... is a fold-expression, handled elsewhere. */
9444 if (op != ERROR_MARK
9445 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9446 op = ERROR_MARK;
9448 /* If it was an assignment operator, consume it. */
9449 if (op != ERROR_MARK)
9450 cp_lexer_consume_token (parser->lexer);
9452 return op;
9455 /* Parse an expression.
9457 expression:
9458 assignment-expression
9459 expression , assignment-expression
9461 CAST_P is true if this expression is the target of a cast.
9462 DECLTYPE_P is true if this expression is the immediate operand of decltype,
9463 except possibly parenthesized or on the RHS of a comma (N3276).
9465 Returns a representation of the expression. */
9467 static cp_expr
9468 cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
9469 bool cast_p, bool decltype_p)
9471 cp_expr expression = NULL_TREE;
9472 location_t loc = UNKNOWN_LOCATION;
9474 while (true)
9476 cp_expr assignment_expression;
9478 /* Parse the next assignment-expression. */
9479 assignment_expression
9480 = cp_parser_assignment_expression (parser, pidk, cast_p, decltype_p);
9482 /* We don't create a temporary for a call that is the immediate operand
9483 of decltype or on the RHS of a comma. But when we see a comma, we
9484 need to create a temporary for a call on the LHS. */
9485 if (decltype_p && !processing_template_decl
9486 && TREE_CODE (assignment_expression) == CALL_EXPR
9487 && CLASS_TYPE_P (TREE_TYPE (assignment_expression))
9488 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9489 assignment_expression
9490 = build_cplus_new (TREE_TYPE (assignment_expression),
9491 assignment_expression, tf_warning_or_error);
9493 /* If this is the first assignment-expression, we can just
9494 save it away. */
9495 if (!expression)
9496 expression = assignment_expression;
9497 else
9499 /* Create a location with caret at the comma, ranging
9500 from the start of the LHS to the end of the RHS. */
9501 loc = make_location (loc,
9502 expression.get_start (),
9503 assignment_expression.get_finish ());
9504 expression = build_x_compound_expr (loc, expression,
9505 assignment_expression,
9506 complain_flags (decltype_p));
9507 expression.set_location (loc);
9509 /* If the next token is not a comma, or we're in a fold-expression, then
9510 we are done with the expression. */
9511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
9512 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_ELLIPSIS))
9513 break;
9514 /* Consume the `,'. */
9515 loc = cp_lexer_peek_token (parser->lexer)->location;
9516 cp_lexer_consume_token (parser->lexer);
9517 /* A comma operator cannot appear in a constant-expression. */
9518 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
9519 expression = error_mark_node;
9522 return expression;
9525 /* Parse a constant-expression.
9527 constant-expression:
9528 conditional-expression
9530 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
9531 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
9532 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
9533 is false, NON_CONSTANT_P should be NULL. */
9535 static cp_expr
9536 cp_parser_constant_expression (cp_parser* parser,
9537 bool allow_non_constant_p,
9538 bool *non_constant_p)
9540 bool saved_integral_constant_expression_p;
9541 bool saved_allow_non_integral_constant_expression_p;
9542 bool saved_non_integral_constant_expression_p;
9543 cp_expr expression;
9545 /* It might seem that we could simply parse the
9546 conditional-expression, and then check to see if it were
9547 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
9548 one that the compiler can figure out is constant, possibly after
9549 doing some simplifications or optimizations. The standard has a
9550 precise definition of constant-expression, and we must honor
9551 that, even though it is somewhat more restrictive.
9553 For example:
9555 int i[(2, 3)];
9557 is not a legal declaration, because `(2, 3)' is not a
9558 constant-expression. The `,' operator is forbidden in a
9559 constant-expression. However, GCC's constant-folding machinery
9560 will fold this operation to an INTEGER_CST for `3'. */
9562 /* Save the old settings. */
9563 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
9564 saved_allow_non_integral_constant_expression_p
9565 = parser->allow_non_integral_constant_expression_p;
9566 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
9567 /* We are now parsing a constant-expression. */
9568 parser->integral_constant_expression_p = true;
9569 parser->allow_non_integral_constant_expression_p
9570 = (allow_non_constant_p || cxx_dialect >= cxx11);
9571 parser->non_integral_constant_expression_p = false;
9572 /* Although the grammar says "conditional-expression", we parse an
9573 "assignment-expression", which also permits "throw-expression"
9574 and the use of assignment operators. In the case that
9575 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
9576 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
9577 actually essential that we look for an assignment-expression.
9578 For example, cp_parser_initializer_clauses uses this function to
9579 determine whether a particular assignment-expression is in fact
9580 constant. */
9581 expression = cp_parser_assignment_expression (parser);
9582 /* Restore the old settings. */
9583 parser->integral_constant_expression_p
9584 = saved_integral_constant_expression_p;
9585 parser->allow_non_integral_constant_expression_p
9586 = saved_allow_non_integral_constant_expression_p;
9587 if (cxx_dialect >= cxx11)
9589 /* Require an rvalue constant expression here; that's what our
9590 callers expect. Reference constant expressions are handled
9591 separately in e.g. cp_parser_template_argument. */
9592 tree decay = expression;
9593 if (TREE_TYPE (expression)
9594 && TREE_CODE (TREE_TYPE (expression)) == ARRAY_TYPE)
9595 decay = build_address (expression);
9596 bool is_const = potential_rvalue_constant_expression (decay);
9597 parser->non_integral_constant_expression_p = !is_const;
9598 if (!is_const && !allow_non_constant_p)
9599 require_potential_rvalue_constant_expression (decay);
9601 if (allow_non_constant_p)
9602 *non_constant_p = parser->non_integral_constant_expression_p;
9603 parser->non_integral_constant_expression_p
9604 = saved_non_integral_constant_expression_p;
9606 return expression;
9609 /* Parse __builtin_offsetof.
9611 offsetof-expression:
9612 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
9614 offsetof-member-designator:
9615 id-expression
9616 | offsetof-member-designator "." id-expression
9617 | offsetof-member-designator "[" expression "]"
9618 | offsetof-member-designator "->" id-expression */
9620 static cp_expr
9621 cp_parser_builtin_offsetof (cp_parser *parser)
9623 int save_ice_p, save_non_ice_p;
9624 tree type;
9625 cp_expr expr;
9626 cp_id_kind dummy;
9627 cp_token *token;
9628 location_t finish_loc;
9630 /* We're about to accept non-integral-constant things, but will
9631 definitely yield an integral constant expression. Save and
9632 restore these values around our local parsing. */
9633 save_ice_p = parser->integral_constant_expression_p;
9634 save_non_ice_p = parser->non_integral_constant_expression_p;
9636 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
9638 /* Consume the "__builtin_offsetof" token. */
9639 cp_lexer_consume_token (parser->lexer);
9640 /* Consume the opening `('. */
9641 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9642 /* Parse the type-id. */
9643 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9644 type = cp_parser_type_id (parser);
9645 /* Look for the `,'. */
9646 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9647 token = cp_lexer_peek_token (parser->lexer);
9649 /* Build the (type *)null that begins the traditional offsetof macro. */
9650 tree object_ptr
9651 = build_static_cast (build_pointer_type (type), null_pointer_node,
9652 tf_warning_or_error);
9654 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
9655 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, object_ptr,
9656 true, &dummy, token->location);
9657 while (true)
9659 token = cp_lexer_peek_token (parser->lexer);
9660 switch (token->type)
9662 case CPP_OPEN_SQUARE:
9663 /* offsetof-member-designator "[" expression "]" */
9664 expr = cp_parser_postfix_open_square_expression (parser, expr,
9665 true, false);
9666 break;
9668 case CPP_DEREF:
9669 /* offsetof-member-designator "->" identifier */
9670 expr = grok_array_decl (token->location, expr,
9671 integer_zero_node, false);
9672 /* FALLTHRU */
9674 case CPP_DOT:
9675 /* offsetof-member-designator "." identifier */
9676 cp_lexer_consume_token (parser->lexer);
9677 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
9678 expr, true, &dummy,
9679 token->location);
9680 break;
9682 case CPP_CLOSE_PAREN:
9683 /* Consume the ")" token. */
9684 finish_loc = cp_lexer_peek_token (parser->lexer)->location;
9685 cp_lexer_consume_token (parser->lexer);
9686 goto success;
9688 default:
9689 /* Error. We know the following require will fail, but
9690 that gives the proper error message. */
9691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9692 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
9693 expr = error_mark_node;
9694 goto failure;
9698 success:
9699 /* Make a location of the form:
9700 __builtin_offsetof (struct s, f)
9701 ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
9702 with caret at the type-id, ranging from the start of the
9703 "_builtin_offsetof" token to the close paren. */
9704 loc = make_location (loc, start_loc, finish_loc);
9705 /* The result will be an INTEGER_CST, so we need to explicitly
9706 preserve the location. */
9707 expr = cp_expr (finish_offsetof (object_ptr, expr, loc), loc);
9709 failure:
9710 parser->integral_constant_expression_p = save_ice_p;
9711 parser->non_integral_constant_expression_p = save_non_ice_p;
9713 return expr;
9716 /* Parse a trait expression.
9718 Returns a representation of the expression, the underlying type
9719 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
9721 static tree
9722 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
9724 cp_trait_kind kind;
9725 tree type1, type2 = NULL_TREE;
9726 bool binary = false;
9727 bool variadic = false;
9729 switch (keyword)
9731 case RID_HAS_NOTHROW_ASSIGN:
9732 kind = CPTK_HAS_NOTHROW_ASSIGN;
9733 break;
9734 case RID_HAS_NOTHROW_CONSTRUCTOR:
9735 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
9736 break;
9737 case RID_HAS_NOTHROW_COPY:
9738 kind = CPTK_HAS_NOTHROW_COPY;
9739 break;
9740 case RID_HAS_TRIVIAL_ASSIGN:
9741 kind = CPTK_HAS_TRIVIAL_ASSIGN;
9742 break;
9743 case RID_HAS_TRIVIAL_CONSTRUCTOR:
9744 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
9745 break;
9746 case RID_HAS_TRIVIAL_COPY:
9747 kind = CPTK_HAS_TRIVIAL_COPY;
9748 break;
9749 case RID_HAS_TRIVIAL_DESTRUCTOR:
9750 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
9751 break;
9752 case RID_HAS_UNIQUE_OBJ_REPRESENTATIONS:
9753 kind = CPTK_HAS_UNIQUE_OBJ_REPRESENTATIONS;
9754 break;
9755 case RID_HAS_VIRTUAL_DESTRUCTOR:
9756 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
9757 break;
9758 case RID_IS_ABSTRACT:
9759 kind = CPTK_IS_ABSTRACT;
9760 break;
9761 case RID_IS_AGGREGATE:
9762 kind = CPTK_IS_AGGREGATE;
9763 break;
9764 case RID_IS_BASE_OF:
9765 kind = CPTK_IS_BASE_OF;
9766 binary = true;
9767 break;
9768 case RID_IS_CLASS:
9769 kind = CPTK_IS_CLASS;
9770 break;
9771 case RID_IS_EMPTY:
9772 kind = CPTK_IS_EMPTY;
9773 break;
9774 case RID_IS_ENUM:
9775 kind = CPTK_IS_ENUM;
9776 break;
9777 case RID_IS_FINAL:
9778 kind = CPTK_IS_FINAL;
9779 break;
9780 case RID_IS_LITERAL_TYPE:
9781 kind = CPTK_IS_LITERAL_TYPE;
9782 break;
9783 case RID_IS_POD:
9784 kind = CPTK_IS_POD;
9785 break;
9786 case RID_IS_POLYMORPHIC:
9787 kind = CPTK_IS_POLYMORPHIC;
9788 break;
9789 case RID_IS_SAME_AS:
9790 kind = CPTK_IS_SAME_AS;
9791 binary = true;
9792 break;
9793 case RID_IS_STD_LAYOUT:
9794 kind = CPTK_IS_STD_LAYOUT;
9795 break;
9796 case RID_IS_TRIVIAL:
9797 kind = CPTK_IS_TRIVIAL;
9798 break;
9799 case RID_IS_TRIVIALLY_ASSIGNABLE:
9800 kind = CPTK_IS_TRIVIALLY_ASSIGNABLE;
9801 binary = true;
9802 break;
9803 case RID_IS_TRIVIALLY_CONSTRUCTIBLE:
9804 kind = CPTK_IS_TRIVIALLY_CONSTRUCTIBLE;
9805 variadic = true;
9806 break;
9807 case RID_IS_TRIVIALLY_COPYABLE:
9808 kind = CPTK_IS_TRIVIALLY_COPYABLE;
9809 break;
9810 case RID_IS_UNION:
9811 kind = CPTK_IS_UNION;
9812 break;
9813 case RID_UNDERLYING_TYPE:
9814 kind = CPTK_UNDERLYING_TYPE;
9815 break;
9816 case RID_BASES:
9817 kind = CPTK_BASES;
9818 break;
9819 case RID_DIRECT_BASES:
9820 kind = CPTK_DIRECT_BASES;
9821 break;
9822 case RID_IS_ASSIGNABLE:
9823 kind = CPTK_IS_ASSIGNABLE;
9824 binary = true;
9825 break;
9826 case RID_IS_CONSTRUCTIBLE:
9827 kind = CPTK_IS_CONSTRUCTIBLE;
9828 variadic = true;
9829 break;
9830 default:
9831 gcc_unreachable ();
9834 /* Consume the token. */
9835 cp_lexer_consume_token (parser->lexer);
9837 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9840 type_id_in_expr_sentinel s (parser);
9841 type1 = cp_parser_type_id (parser);
9844 if (type1 == error_mark_node)
9845 return error_mark_node;
9847 if (binary)
9849 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
9852 type_id_in_expr_sentinel s (parser);
9853 type2 = cp_parser_type_id (parser);
9856 if (type2 == error_mark_node)
9857 return error_mark_node;
9859 else if (variadic)
9861 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9863 cp_lexer_consume_token (parser->lexer);
9864 tree elt = cp_parser_type_id (parser);
9865 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9867 cp_lexer_consume_token (parser->lexer);
9868 elt = make_pack_expansion (elt);
9870 if (elt == error_mark_node)
9871 return error_mark_node;
9872 type2 = tree_cons (NULL_TREE, elt, type2);
9876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9878 /* Complete the trait expression, which may mean either processing
9879 the trait expr now or saving it for template instantiation. */
9880 switch (kind)
9882 case CPTK_UNDERLYING_TYPE:
9883 return finish_underlying_type (type1);
9884 case CPTK_BASES:
9885 return finish_bases (type1, false);
9886 case CPTK_DIRECT_BASES:
9887 return finish_bases (type1, true);
9888 default:
9889 return finish_trait_expr (kind, type1, type2);
9893 /* Lambdas that appear in variable initializer or default argument scope
9894 get that in their mangling, so we need to record it. We might as well
9895 use the count for function and namespace scopes as well. */
9896 static GTY(()) tree lambda_scope;
9897 static GTY(()) int lambda_count;
9898 struct GTY(()) tree_int
9900 tree t;
9901 int i;
9903 static GTY(()) vec<tree_int, va_gc> *lambda_scope_stack;
9905 static void
9906 start_lambda_scope (tree decl)
9908 tree_int ti;
9909 gcc_assert (decl);
9910 /* Once we're inside a function, we ignore other scopes and just push
9911 the function again so that popping works properly. */
9912 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
9913 decl = current_function_decl;
9914 ti.t = lambda_scope;
9915 ti.i = lambda_count;
9916 vec_safe_push (lambda_scope_stack, ti);
9917 if (lambda_scope != decl)
9919 /* Don't reset the count if we're still in the same function. */
9920 lambda_scope = decl;
9921 lambda_count = 0;
9925 static void
9926 record_lambda_scope (tree lambda)
9928 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
9929 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
9932 static void
9933 finish_lambda_scope (void)
9935 tree_int *p = &lambda_scope_stack->last ();
9936 if (lambda_scope != p->t)
9938 lambda_scope = p->t;
9939 lambda_count = p->i;
9941 lambda_scope_stack->pop ();
9944 /* Parse a lambda expression.
9946 lambda-expression:
9947 lambda-introducer lambda-declarator [opt] compound-statement
9949 Returns a representation of the expression. */
9951 static cp_expr
9952 cp_parser_lambda_expression (cp_parser* parser)
9954 tree lambda_expr = build_lambda_expr ();
9955 tree type;
9956 bool ok = true;
9957 cp_token *token = cp_lexer_peek_token (parser->lexer);
9958 cp_token_position start = 0;
9960 LAMBDA_EXPR_LOCATION (lambda_expr) = token->location;
9962 if (cp_unevaluated_operand)
9964 if (!token->error_reported)
9966 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
9967 "lambda-expression in unevaluated context");
9968 token->error_reported = true;
9970 ok = false;
9972 else if (parser->in_template_argument_list_p)
9974 if (!token->error_reported)
9976 error_at (token->location, "lambda-expression in template-argument");
9977 token->error_reported = true;
9979 ok = false;
9982 /* We may be in the middle of deferred access check. Disable
9983 it now. */
9984 push_deferring_access_checks (dk_no_deferred);
9986 cp_parser_lambda_introducer (parser, lambda_expr);
9988 type = begin_lambda_type (lambda_expr);
9989 if (type == error_mark_node)
9990 return error_mark_node;
9992 record_lambda_scope (lambda_expr);
9994 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
9995 determine_visibility (TYPE_NAME (type));
9997 /* Now that we've started the type, add the capture fields for any
9998 explicit captures. */
9999 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10002 /* Inside the class, surrounding template-parameter-lists do not apply. */
10003 unsigned int saved_num_template_parameter_lists
10004 = parser->num_template_parameter_lists;
10005 unsigned char in_statement = parser->in_statement;
10006 bool in_switch_statement_p = parser->in_switch_statement_p;
10007 bool fully_implicit_function_template_p
10008 = parser->fully_implicit_function_template_p;
10009 tree implicit_template_parms = parser->implicit_template_parms;
10010 cp_binding_level* implicit_template_scope = parser->implicit_template_scope;
10011 bool auto_is_implicit_function_template_parm_p
10012 = parser->auto_is_implicit_function_template_parm_p;
10014 parser->num_template_parameter_lists = 0;
10015 parser->in_statement = 0;
10016 parser->in_switch_statement_p = false;
10017 parser->fully_implicit_function_template_p = false;
10018 parser->implicit_template_parms = 0;
10019 parser->implicit_template_scope = 0;
10020 parser->auto_is_implicit_function_template_parm_p = false;
10022 /* By virtue of defining a local class, a lambda expression has access to
10023 the private variables of enclosing classes. */
10025 ok &= cp_parser_lambda_declarator_opt (parser, lambda_expr);
10027 if (ok && cp_parser_error_occurred (parser))
10028 ok = false;
10030 if (ok)
10032 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
10033 && cp_parser_start_tentative_firewall (parser))
10034 start = token;
10035 cp_parser_lambda_body (parser, lambda_expr);
10037 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10039 if (cp_parser_skip_to_closing_brace (parser))
10040 cp_lexer_consume_token (parser->lexer);
10043 /* The capture list was built up in reverse order; fix that now. */
10044 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr)
10045 = nreverse (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
10047 if (ok)
10048 maybe_add_lambda_conv_op (type);
10050 type = finish_struct (type, /*attributes=*/NULL_TREE);
10052 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
10053 parser->in_statement = in_statement;
10054 parser->in_switch_statement_p = in_switch_statement_p;
10055 parser->fully_implicit_function_template_p
10056 = fully_implicit_function_template_p;
10057 parser->implicit_template_parms = implicit_template_parms;
10058 parser->implicit_template_scope = implicit_template_scope;
10059 parser->auto_is_implicit_function_template_parm_p
10060 = auto_is_implicit_function_template_parm_p;
10063 /* This field is only used during parsing of the lambda. */
10064 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
10066 /* This lambda shouldn't have any proxies left at this point. */
10067 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
10068 /* And now that we're done, push proxies for an enclosing lambda. */
10069 insert_pending_capture_proxies ();
10071 if (ok)
10072 lambda_expr = build_lambda_object (lambda_expr);
10073 else
10074 lambda_expr = error_mark_node;
10076 cp_parser_end_tentative_firewall (parser, start, lambda_expr);
10078 pop_deferring_access_checks ();
10080 return lambda_expr;
10083 /* Parse the beginning of a lambda expression.
10085 lambda-introducer:
10086 [ lambda-capture [opt] ]
10088 LAMBDA_EXPR is the current representation of the lambda expression. */
10090 static void
10091 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
10093 /* Need commas after the first capture. */
10094 bool first = true;
10096 /* Eat the leading `['. */
10097 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
10099 /* Record default capture mode. "[&" "[=" "[&," "[=," */
10100 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
10101 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
10102 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
10103 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10104 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
10106 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
10108 cp_lexer_consume_token (parser->lexer);
10109 first = false;
10112 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
10114 cp_token* capture_token;
10115 tree capture_id;
10116 tree capture_init_expr;
10117 cp_id_kind idk = CP_ID_KIND_NONE;
10118 bool explicit_init_p = false;
10120 enum capture_kind_type
10122 BY_COPY,
10123 BY_REFERENCE
10125 enum capture_kind_type capture_kind = BY_COPY;
10127 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
10129 error ("expected end of capture-list");
10130 return;
10133 if (first)
10134 first = false;
10135 else
10136 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10138 /* Possibly capture `this'. */
10139 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
10141 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10142 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
10143 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
10144 "with by-copy capture default");
10145 cp_lexer_consume_token (parser->lexer);
10146 add_capture (lambda_expr,
10147 /*id=*/this_identifier,
10148 /*initializer=*/finish_this_expr (),
10149 /*by_reference_p=*/true,
10150 explicit_init_p);
10151 continue;
10154 /* Possibly capture `*this'. */
10155 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
10156 && cp_lexer_nth_token_is_keyword (parser->lexer, 2, RID_THIS))
10158 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
10159 if (cxx_dialect < cxx1z)
10160 pedwarn (loc, 0, "%<*this%> capture only available with "
10161 "-std=c++1z or -std=gnu++1z");
10162 cp_lexer_consume_token (parser->lexer);
10163 cp_lexer_consume_token (parser->lexer);
10164 add_capture (lambda_expr,
10165 /*id=*/this_identifier,
10166 /*initializer=*/finish_this_expr (),
10167 /*by_reference_p=*/false,
10168 explicit_init_p);
10169 continue;
10172 /* Remember whether we want to capture as a reference or not. */
10173 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
10175 capture_kind = BY_REFERENCE;
10176 cp_lexer_consume_token (parser->lexer);
10179 /* Get the identifier. */
10180 capture_token = cp_lexer_peek_token (parser->lexer);
10181 capture_id = cp_parser_identifier (parser);
10183 if (capture_id == error_mark_node)
10184 /* Would be nice to have a cp_parser_skip_to_closing_x for general
10185 delimiters, but I modified this to stop on unnested ']' as well. It
10186 was already changed to stop on unnested '}', so the
10187 "closing_parenthesis" name is no more misleading with my change. */
10189 cp_parser_skip_to_closing_parenthesis (parser,
10190 /*recovering=*/true,
10191 /*or_comma=*/true,
10192 /*consume_paren=*/true);
10193 break;
10196 /* Find the initializer for this capture. */
10197 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ)
10198 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
10199 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10201 bool direct, non_constant;
10202 /* An explicit initializer exists. */
10203 if (cxx_dialect < cxx14)
10204 pedwarn (input_location, 0,
10205 "lambda capture initializers "
10206 "only available with -std=c++14 or -std=gnu++14");
10207 capture_init_expr = cp_parser_initializer (parser, &direct,
10208 &non_constant);
10209 explicit_init_p = true;
10210 if (capture_init_expr == NULL_TREE)
10212 error ("empty initializer for lambda init-capture");
10213 capture_init_expr = error_mark_node;
10216 else
10218 const char* error_msg;
10220 /* Turn the identifier into an id-expression. */
10221 capture_init_expr
10222 = cp_parser_lookup_name_simple (parser, capture_id,
10223 capture_token->location);
10225 if (capture_init_expr == error_mark_node)
10227 unqualified_name_lookup_error (capture_id);
10228 continue;
10230 else if (DECL_P (capture_init_expr)
10231 && (!VAR_P (capture_init_expr)
10232 && TREE_CODE (capture_init_expr) != PARM_DECL))
10234 error_at (capture_token->location,
10235 "capture of non-variable %qD ",
10236 capture_init_expr);
10237 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10238 "%q#D declared here", capture_init_expr);
10239 continue;
10241 if (VAR_P (capture_init_expr)
10242 && decl_storage_duration (capture_init_expr) != dk_auto)
10244 if (pedwarn (capture_token->location, 0, "capture of variable "
10245 "%qD with non-automatic storage duration",
10246 capture_init_expr))
10247 inform (DECL_SOURCE_LOCATION (capture_init_expr),
10248 "%q#D declared here", capture_init_expr);
10249 continue;
10252 capture_init_expr
10253 = finish_id_expression
10254 (capture_id,
10255 capture_init_expr,
10256 parser->scope,
10257 &idk,
10258 /*integral_constant_expression_p=*/false,
10259 /*allow_non_integral_constant_expression_p=*/false,
10260 /*non_integral_constant_expression_p=*/NULL,
10261 /*template_p=*/false,
10262 /*done=*/true,
10263 /*address_p=*/false,
10264 /*template_arg_p=*/false,
10265 &error_msg,
10266 capture_token->location);
10268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10270 cp_lexer_consume_token (parser->lexer);
10271 capture_init_expr = make_pack_expansion (capture_init_expr);
10273 else
10274 check_for_bare_parameter_packs (capture_init_expr);
10277 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
10278 && !explicit_init_p)
10280 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
10281 && capture_kind == BY_COPY)
10282 pedwarn (capture_token->location, 0, "explicit by-copy capture "
10283 "of %qD redundant with by-copy capture default",
10284 capture_id);
10285 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
10286 && capture_kind == BY_REFERENCE)
10287 pedwarn (capture_token->location, 0, "explicit by-reference "
10288 "capture of %qD redundant with by-reference capture "
10289 "default", capture_id);
10292 add_capture (lambda_expr,
10293 capture_id,
10294 capture_init_expr,
10295 /*by_reference_p=*/capture_kind == BY_REFERENCE,
10296 explicit_init_p);
10299 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10302 /* Parse the (optional) middle of a lambda expression.
10304 lambda-declarator:
10305 < template-parameter-list [opt] >
10306 ( parameter-declaration-clause [opt] )
10307 attribute-specifier [opt]
10308 decl-specifier-seq [opt]
10309 exception-specification [opt]
10310 lambda-return-type-clause [opt]
10312 LAMBDA_EXPR is the current representation of the lambda expression. */
10314 static bool
10315 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
10317 /* 5.1.1.4 of the standard says:
10318 If a lambda-expression does not include a lambda-declarator, it is as if
10319 the lambda-declarator were ().
10320 This means an empty parameter list, no attributes, and no exception
10321 specification. */
10322 tree param_list = void_list_node;
10323 tree attributes = NULL_TREE;
10324 tree exception_spec = NULL_TREE;
10325 tree template_param_list = NULL_TREE;
10326 tree tx_qual = NULL_TREE;
10327 cp_decl_specifier_seq lambda_specs;
10328 clear_decl_specs (&lambda_specs);
10330 /* The template-parameter-list is optional, but must begin with
10331 an opening angle if present. */
10332 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
10334 if (cxx_dialect < cxx14)
10335 pedwarn (parser->lexer->next_token->location, 0,
10336 "lambda templates are only available with "
10337 "-std=c++14 or -std=gnu++14");
10338 else
10339 pedwarn (parser->lexer->next_token->location, OPT_Wpedantic,
10340 "ISO C++ does not support lambda templates");
10342 cp_lexer_consume_token (parser->lexer);
10344 template_param_list = cp_parser_template_parameter_list (parser);
10346 cp_parser_skip_to_end_of_template_parameter_list (parser);
10348 /* We just processed one more parameter list. */
10349 ++parser->num_template_parameter_lists;
10352 /* The parameter-declaration-clause is optional (unless
10353 template-parameter-list was given), but must begin with an
10354 opening parenthesis if present. */
10355 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10357 cp_lexer_consume_token (parser->lexer);
10359 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
10361 /* Parse parameters. */
10362 param_list = cp_parser_parameter_declaration_clause (parser);
10364 /* Default arguments shall not be specified in the
10365 parameter-declaration-clause of a lambda-declarator. */
10366 if (cxx_dialect < cxx14)
10367 for (tree t = param_list; t; t = TREE_CHAIN (t))
10368 if (TREE_PURPOSE (t) && DECL_P (TREE_VALUE (t)))
10369 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
10370 "default argument specified for lambda parameter");
10372 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10374 attributes = cp_parser_attributes_opt (parser);
10376 /* In the decl-specifier-seq of the lambda-declarator, each
10377 decl-specifier shall either be mutable or constexpr. */
10378 int declares_class_or_enum;
10379 if (cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
10380 cp_parser_decl_specifier_seq (parser,
10381 CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR,
10382 &lambda_specs, &declares_class_or_enum);
10383 if (lambda_specs.storage_class == sc_mutable)
10385 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
10386 if (lambda_specs.conflicting_specifiers_p)
10387 error_at (lambda_specs.locations[ds_storage_class],
10388 "duplicate %<mutable%>");
10391 tx_qual = cp_parser_tx_qualifier_opt (parser);
10393 /* Parse optional exception specification. */
10394 exception_spec = cp_parser_exception_specification_opt (parser);
10396 /* Parse optional trailing return type. */
10397 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
10399 cp_lexer_consume_token (parser->lexer);
10400 LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10401 = cp_parser_trailing_type_id (parser);
10404 /* The function parameters must be in scope all the way until after the
10405 trailing-return-type in case of decltype. */
10406 pop_bindings_and_leave_scope ();
10408 else if (template_param_list != NULL_TREE) // generate diagnostic
10409 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10411 /* Create the function call operator.
10413 Messing with declarators like this is no uglier than building up the
10414 FUNCTION_DECL by hand, and this is less likely to get out of sync with
10415 other code. */
10417 cp_decl_specifier_seq return_type_specs;
10418 cp_declarator* declarator;
10419 tree fco;
10420 int quals;
10421 void *p;
10423 clear_decl_specs (&return_type_specs);
10424 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10425 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
10426 else
10427 /* Maybe we will deduce the return type later. */
10428 return_type_specs.type = make_auto ();
10430 if (lambda_specs.locations[ds_constexpr])
10432 if (cxx_dialect >= cxx1z)
10433 return_type_specs.locations[ds_constexpr]
10434 = lambda_specs.locations[ds_constexpr];
10435 else
10436 error_at (lambda_specs.locations[ds_constexpr], "%<constexpr%> "
10437 "lambda only available with -std=c++1z or -std=gnu++1z");
10440 p = obstack_alloc (&declarator_obstack, 0);
10442 declarator = make_id_declarator (NULL_TREE, cp_operator_id (CALL_EXPR),
10443 sfk_none);
10445 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
10446 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
10447 declarator = make_call_declarator (declarator, param_list, quals,
10448 VIRT_SPEC_UNSPECIFIED,
10449 REF_QUAL_NONE,
10450 tx_qual,
10451 exception_spec,
10452 /*late_return_type=*/NULL_TREE,
10453 /*requires_clause*/NULL_TREE);
10454 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
10456 fco = grokmethod (&return_type_specs,
10457 declarator,
10458 attributes);
10459 if (fco != error_mark_node)
10461 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
10462 DECL_ARTIFICIAL (fco) = 1;
10463 /* Give the object parameter a different name. */
10464 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
10465 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
10466 TYPE_HAS_LATE_RETURN_TYPE (TREE_TYPE (fco)) = 1;
10468 if (template_param_list)
10470 fco = finish_member_template_decl (fco);
10471 finish_template_decl (template_param_list);
10472 --parser->num_template_parameter_lists;
10474 else if (parser->fully_implicit_function_template_p)
10475 fco = finish_fully_implicit_template (parser, fco);
10477 finish_member_declaration (fco);
10479 obstack_free (&declarator_obstack, p);
10481 return (fco != error_mark_node);
10485 /* Parse the body of a lambda expression, which is simply
10487 compound-statement
10489 but which requires special handling.
10490 LAMBDA_EXPR is the current representation of the lambda expression. */
10492 static void
10493 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
10495 bool nested = (current_function_decl != NULL_TREE);
10496 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
10497 if (nested)
10498 push_function_context ();
10499 else
10500 /* Still increment function_depth so that we don't GC in the
10501 middle of an expression. */
10502 ++function_depth;
10503 vec<tree> omp_privatization_save;
10504 save_omp_privatization_clauses (omp_privatization_save);
10505 /* Clear this in case we're in the middle of a default argument. */
10506 parser->local_variables_forbidden_p = false;
10508 /* Finish the function call operator
10509 - class_specifier
10510 + late_parsing_for_member
10511 + function_definition_after_declarator
10512 + ctor_initializer_opt_and_function_body */
10514 tree fco = lambda_function (lambda_expr);
10515 tree body;
10516 bool done = false;
10517 tree compound_stmt;
10518 tree cap;
10520 /* Let the front end know that we are going to be defining this
10521 function. */
10522 start_preparsed_function (fco,
10523 NULL_TREE,
10524 SF_PRE_PARSED | SF_INCLASS_INLINE);
10526 start_lambda_scope (fco);
10527 body = begin_function_body ();
10529 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
10530 goto out;
10532 /* Push the proxies for any explicit captures. */
10533 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
10534 cap = TREE_CHAIN (cap))
10535 build_capture_proxy (TREE_PURPOSE (cap));
10537 compound_stmt = begin_compound_stmt (0);
10539 /* 5.1.1.4 of the standard says:
10540 If a lambda-expression does not include a trailing-return-type, it
10541 is as if the trailing-return-type denotes the following type:
10542 * if the compound-statement is of the form
10543 { return attribute-specifier [opt] expression ; }
10544 the type of the returned expression after lvalue-to-rvalue
10545 conversion (_conv.lval_ 4.1), array-to-pointer conversion
10546 (_conv.array_ 4.2), and function-to-pointer conversion
10547 (_conv.func_ 4.3);
10548 * otherwise, void. */
10550 /* In a lambda that has neither a lambda-return-type-clause
10551 nor a deducible form, errors should be reported for return statements
10552 in the body. Since we used void as the placeholder return type, parsing
10553 the body as usual will give such desired behavior. */
10554 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
10555 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
10556 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
10558 tree expr = NULL_TREE;
10559 cp_id_kind idk = CP_ID_KIND_NONE;
10561 /* Parse tentatively in case there's more after the initial return
10562 statement. */
10563 cp_parser_parse_tentatively (parser);
10565 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
10567 expr = cp_parser_expression (parser, &idk);
10569 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10572 if (cp_parser_parse_definitely (parser))
10574 if (!processing_template_decl)
10576 tree type = lambda_return_type (expr);
10577 apply_deduced_return_type (fco, type);
10578 if (type == error_mark_node)
10579 expr = error_mark_node;
10582 /* Will get error here if type not deduced yet. */
10583 finish_return_stmt (expr);
10585 done = true;
10589 if (!done)
10591 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10592 cp_parser_label_declaration (parser);
10593 cp_parser_statement_seq_opt (parser, NULL_TREE);
10594 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10597 finish_compound_stmt (compound_stmt);
10599 out:
10600 finish_function_body (body);
10601 finish_lambda_scope ();
10603 /* Finish the function and generate code for it if necessary. */
10604 tree fn = finish_function (/*inline*/2);
10606 /* Only expand if the call op is not a template. */
10607 if (!DECL_TEMPLATE_INFO (fco))
10608 expand_or_defer_fn (fn);
10611 restore_omp_privatization_clauses (omp_privatization_save);
10612 parser->local_variables_forbidden_p = local_variables_forbidden_p;
10613 if (nested)
10614 pop_function_context();
10615 else
10616 --function_depth;
10619 /* Statements [gram.stmt.stmt] */
10621 /* Parse a statement.
10623 statement:
10624 labeled-statement
10625 expression-statement
10626 compound-statement
10627 selection-statement
10628 iteration-statement
10629 jump-statement
10630 declaration-statement
10631 try-block
10633 C++11:
10635 statement:
10636 labeled-statement
10637 attribute-specifier-seq (opt) expression-statement
10638 attribute-specifier-seq (opt) compound-statement
10639 attribute-specifier-seq (opt) selection-statement
10640 attribute-specifier-seq (opt) iteration-statement
10641 attribute-specifier-seq (opt) jump-statement
10642 declaration-statement
10643 attribute-specifier-seq (opt) try-block
10645 init-statement:
10646 expression-statement
10647 simple-declaration
10649 TM Extension:
10651 statement:
10652 atomic-statement
10654 IN_COMPOUND is true when the statement is nested inside a
10655 cp_parser_compound_statement; this matters for certain pragmas.
10657 If IF_P is not NULL, *IF_P is set to indicate whether the statement
10658 is a (possibly labeled) if statement which is not enclosed in braces
10659 and has an else clause. This is used to implement -Wparentheses.
10661 CHAIN is a vector of if-else-if conditions. */
10663 static void
10664 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
10665 bool in_compound, bool *if_p, vec<tree> *chain,
10666 location_t *loc_after_labels)
10668 tree statement, std_attrs = NULL_TREE;
10669 cp_token *token;
10670 location_t statement_location, attrs_location;
10672 restart:
10673 if (if_p != NULL)
10674 *if_p = false;
10675 /* There is no statement yet. */
10676 statement = NULL_TREE;
10678 saved_token_sentinel saved_tokens (parser->lexer);
10679 attrs_location = cp_lexer_peek_token (parser->lexer)->location;
10680 if (c_dialect_objc ())
10681 /* In obj-c++, seeing '[[' might be the either the beginning of
10682 c++11 attributes, or a nested objc-message-expression. So
10683 let's parse the c++11 attributes tentatively. */
10684 cp_parser_parse_tentatively (parser);
10685 std_attrs = cp_parser_std_attribute_spec_seq (parser);
10686 if (c_dialect_objc ())
10688 if (!cp_parser_parse_definitely (parser))
10689 std_attrs = NULL_TREE;
10692 /* Peek at the next token. */
10693 token = cp_lexer_peek_token (parser->lexer);
10694 /* Remember the location of the first token in the statement. */
10695 statement_location = token->location;
10696 /* If this is a keyword, then that will often determine what kind of
10697 statement we have. */
10698 if (token->type == CPP_KEYWORD)
10700 enum rid keyword = token->keyword;
10702 switch (keyword)
10704 case RID_CASE:
10705 case RID_DEFAULT:
10706 /* Looks like a labeled-statement with a case label.
10707 Parse the label, and then use tail recursion to parse
10708 the statement. */
10709 cp_parser_label_for_labeled_statement (parser, std_attrs);
10710 in_compound = false;
10711 goto restart;
10713 case RID_IF:
10714 case RID_SWITCH:
10715 statement = cp_parser_selection_statement (parser, if_p, chain);
10716 break;
10718 case RID_WHILE:
10719 case RID_DO:
10720 case RID_FOR:
10721 statement = cp_parser_iteration_statement (parser, if_p, false);
10722 break;
10724 case RID_CILK_FOR:
10725 if (!flag_cilkplus)
10727 error_at (cp_lexer_peek_token (parser->lexer)->location,
10728 "-fcilkplus must be enabled to use %<_Cilk_for%>");
10729 cp_lexer_consume_token (parser->lexer);
10730 statement = error_mark_node;
10732 else
10733 statement = cp_parser_cilk_for (parser, integer_zero_node, if_p);
10734 break;
10736 case RID_BREAK:
10737 case RID_CONTINUE:
10738 case RID_RETURN:
10739 case RID_GOTO:
10740 statement = cp_parser_jump_statement (parser);
10741 break;
10743 case RID_CILK_SYNC:
10744 cp_lexer_consume_token (parser->lexer);
10745 if (flag_cilkplus)
10747 tree sync_expr = build_cilk_sync ();
10748 SET_EXPR_LOCATION (sync_expr,
10749 token->location);
10750 statement = finish_expr_stmt (sync_expr);
10752 else
10754 error_at (token->location, "-fcilkplus must be enabled to use"
10755 " %<_Cilk_sync%>");
10756 statement = error_mark_node;
10758 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10759 break;
10761 /* Objective-C++ exception-handling constructs. */
10762 case RID_AT_TRY:
10763 case RID_AT_CATCH:
10764 case RID_AT_FINALLY:
10765 case RID_AT_SYNCHRONIZED:
10766 case RID_AT_THROW:
10767 statement = cp_parser_objc_statement (parser);
10768 break;
10770 case RID_TRY:
10771 statement = cp_parser_try_block (parser);
10772 break;
10774 case RID_NAMESPACE:
10775 /* This must be a namespace alias definition. */
10776 cp_parser_declaration_statement (parser);
10777 return;
10779 case RID_TRANSACTION_ATOMIC:
10780 case RID_TRANSACTION_RELAXED:
10781 case RID_SYNCHRONIZED:
10782 case RID_ATOMIC_NOEXCEPT:
10783 case RID_ATOMIC_CANCEL:
10784 statement = cp_parser_transaction (parser, token);
10785 break;
10786 case RID_TRANSACTION_CANCEL:
10787 statement = cp_parser_transaction_cancel (parser);
10788 break;
10790 default:
10791 /* It might be a keyword like `int' that can start a
10792 declaration-statement. */
10793 break;
10796 else if (token->type == CPP_NAME)
10798 /* If the next token is a `:', then we are looking at a
10799 labeled-statement. */
10800 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10801 if (token->type == CPP_COLON)
10803 /* Looks like a labeled-statement with an ordinary label.
10804 Parse the label, and then use tail recursion to parse
10805 the statement. */
10807 cp_parser_label_for_labeled_statement (parser, std_attrs);
10808 in_compound = false;
10809 goto restart;
10812 /* Anything that starts with a `{' must be a compound-statement. */
10813 else if (token->type == CPP_OPEN_BRACE)
10814 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
10815 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
10816 a statement all its own. */
10817 else if (token->type == CPP_PRAGMA)
10819 /* Only certain OpenMP pragmas are attached to statements, and thus
10820 are considered statements themselves. All others are not. In
10821 the context of a compound, accept the pragma as a "statement" and
10822 return so that we can check for a close brace. Otherwise we
10823 require a real statement and must go back and read one. */
10824 if (in_compound)
10825 cp_parser_pragma (parser, pragma_compound, if_p);
10826 else if (!cp_parser_pragma (parser, pragma_stmt, if_p))
10827 goto restart;
10828 return;
10830 else if (token->type == CPP_EOF)
10832 cp_parser_error (parser, "expected statement");
10833 return;
10836 /* Everything else must be a declaration-statement or an
10837 expression-statement. Try for the declaration-statement
10838 first, unless we are looking at a `;', in which case we know that
10839 we have an expression-statement. */
10840 if (!statement)
10842 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
10844 if (std_attrs != NULL_TREE)
10846 /* Attributes should be parsed as part of the the
10847 declaration, so let's un-parse them. */
10848 saved_tokens.rollback();
10849 std_attrs = NULL_TREE;
10852 cp_parser_parse_tentatively (parser);
10853 /* Try to parse the declaration-statement. */
10854 cp_parser_declaration_statement (parser);
10855 /* If that worked, we're done. */
10856 if (cp_parser_parse_definitely (parser))
10857 return;
10859 /* All preceding labels have been parsed at this point. */
10860 if (loc_after_labels != NULL)
10861 *loc_after_labels = statement_location;
10863 /* Look for an expression-statement instead. */
10864 statement = cp_parser_expression_statement (parser, in_statement_expr);
10866 /* Handle [[fallthrough]];. */
10867 if (attribute_fallthrough_p (std_attrs))
10869 /* The next token after the fallthrough attribute is ';'. */
10870 if (statement == NULL_TREE)
10872 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
10873 statement = build_call_expr_internal_loc (statement_location,
10874 IFN_FALLTHROUGH,
10875 void_type_node, 0);
10876 finish_expr_stmt (statement);
10878 else
10879 warning_at (statement_location, OPT_Wattributes,
10880 "%<fallthrough%> attribute not followed by %<;%>");
10881 std_attrs = NULL_TREE;
10885 /* Set the line number for the statement. */
10886 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
10887 SET_EXPR_LOCATION (statement, statement_location);
10889 /* Allow "[[fallthrough]];", but warn otherwise. */
10890 if (std_attrs != NULL_TREE)
10891 warning_at (attrs_location,
10892 OPT_Wattributes,
10893 "attributes at the beginning of statement are ignored");
10896 /* Parse the label for a labeled-statement, i.e.
10898 identifier :
10899 case constant-expression :
10900 default :
10902 GNU Extension:
10903 case constant-expression ... constant-expression : statement
10905 When a label is parsed without errors, the label is added to the
10906 parse tree by the finish_* functions, so this function doesn't
10907 have to return the label. */
10909 static void
10910 cp_parser_label_for_labeled_statement (cp_parser* parser, tree attributes)
10912 cp_token *token;
10913 tree label = NULL_TREE;
10914 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
10916 /* The next token should be an identifier. */
10917 token = cp_lexer_peek_token (parser->lexer);
10918 if (token->type != CPP_NAME
10919 && token->type != CPP_KEYWORD)
10921 cp_parser_error (parser, "expected labeled-statement");
10922 return;
10925 /* Remember whether this case or a user-defined label is allowed to fall
10926 through to. */
10927 bool fallthrough_p = token->flags & PREV_FALLTHROUGH;
10929 parser->colon_corrects_to_scope_p = false;
10930 switch (token->keyword)
10932 case RID_CASE:
10934 tree expr, expr_hi;
10935 cp_token *ellipsis;
10937 /* Consume the `case' token. */
10938 cp_lexer_consume_token (parser->lexer);
10939 /* Parse the constant-expression. */
10940 expr = cp_parser_constant_expression (parser);
10941 if (check_for_bare_parameter_packs (expr))
10942 expr = error_mark_node;
10944 ellipsis = cp_lexer_peek_token (parser->lexer);
10945 if (ellipsis->type == CPP_ELLIPSIS)
10947 /* Consume the `...' token. */
10948 cp_lexer_consume_token (parser->lexer);
10949 expr_hi = cp_parser_constant_expression (parser);
10950 if (check_for_bare_parameter_packs (expr_hi))
10951 expr_hi = error_mark_node;
10953 /* We don't need to emit warnings here, as the common code
10954 will do this for us. */
10956 else
10957 expr_hi = NULL_TREE;
10959 if (parser->in_switch_statement_p)
10961 tree l = finish_case_label (token->location, expr, expr_hi);
10962 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10963 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10965 else
10966 error_at (token->location,
10967 "case label %qE not within a switch statement",
10968 expr);
10970 break;
10972 case RID_DEFAULT:
10973 /* Consume the `default' token. */
10974 cp_lexer_consume_token (parser->lexer);
10976 if (parser->in_switch_statement_p)
10978 tree l = finish_case_label (token->location, NULL_TREE, NULL_TREE);
10979 if (l && TREE_CODE (l) == CASE_LABEL_EXPR)
10980 FALLTHROUGH_LABEL_P (CASE_LABEL (l)) = fallthrough_p;
10982 else
10983 error_at (token->location, "case label not within a switch statement");
10984 break;
10986 default:
10987 /* Anything else must be an ordinary label. */
10988 label = finish_label_stmt (cp_parser_identifier (parser));
10989 if (label && TREE_CODE (label) == LABEL_DECL)
10990 FALLTHROUGH_LABEL_P (label) = fallthrough_p;
10991 break;
10994 /* Require the `:' token. */
10995 cp_parser_require (parser, CPP_COLON, RT_COLON);
10997 /* An ordinary label may optionally be followed by attributes.
10998 However, this is only permitted if the attributes are then
10999 followed by a semicolon. This is because, for backward
11000 compatibility, when parsing
11001 lab: __attribute__ ((unused)) int i;
11002 we want the attribute to attach to "i", not "lab". */
11003 if (label != NULL_TREE
11004 && cp_next_tokens_can_be_gnu_attribute_p (parser))
11006 tree attrs;
11007 cp_parser_parse_tentatively (parser);
11008 attrs = cp_parser_gnu_attributes_opt (parser);
11009 if (attrs == NULL_TREE
11010 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11011 cp_parser_abort_tentative_parse (parser);
11012 else if (!cp_parser_parse_definitely (parser))
11014 else
11015 attributes = chainon (attributes, attrs);
11018 if (attributes != NULL_TREE)
11019 cplus_decl_attributes (&label, attributes, 0);
11021 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
11024 /* Parse an expression-statement.
11026 expression-statement:
11027 expression [opt] ;
11029 Returns the new EXPR_STMT -- or NULL_TREE if the expression
11030 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
11031 indicates whether this expression-statement is part of an
11032 expression statement. */
11034 static tree
11035 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
11037 tree statement = NULL_TREE;
11038 cp_token *token = cp_lexer_peek_token (parser->lexer);
11039 location_t loc = token->location;
11041 /* There might be attribute fallthrough. */
11042 tree attr = cp_parser_gnu_attributes_opt (parser);
11044 /* If the next token is a ';', then there is no expression
11045 statement. */
11046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11048 statement = cp_parser_expression (parser);
11049 if (statement == error_mark_node
11050 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11052 cp_parser_skip_to_end_of_block_or_statement (parser);
11053 return error_mark_node;
11057 /* Handle [[fallthrough]];. */
11058 if (attribute_fallthrough_p (attr))
11060 /* The next token after the fallthrough attribute is ';'. */
11061 if (statement == NULL_TREE)
11062 /* Turn [[fallthrough]]; into FALLTHROUGH ();. */
11063 statement = build_call_expr_internal_loc (loc, IFN_FALLTHROUGH,
11064 void_type_node, 0);
11065 else
11066 warning_at (loc, OPT_Wattributes,
11067 "%<fallthrough%> attribute not followed by %<;%>");
11068 attr = NULL_TREE;
11071 /* Allow "[[fallthrough]];", but warn otherwise. */
11072 if (attr != NULL_TREE)
11073 warning_at (loc, OPT_Wattributes,
11074 "attributes at the beginning of statement are ignored");
11076 /* Give a helpful message for "A<T>::type t;" and the like. */
11077 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
11078 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
11080 if (TREE_CODE (statement) == SCOPE_REF)
11081 error_at (token->location, "need %<typename%> before %qE because "
11082 "%qT is a dependent scope",
11083 statement, TREE_OPERAND (statement, 0));
11084 else if (is_overloaded_fn (statement)
11085 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
11087 /* A::A a; */
11088 tree fn = get_first_fn (statement);
11089 error_at (token->location,
11090 "%<%T::%D%> names the constructor, not the type",
11091 DECL_CONTEXT (fn), DECL_NAME (fn));
11095 /* Consume the final `;'. */
11096 cp_parser_consume_semicolon_at_end_of_statement (parser);
11098 if (in_statement_expr
11099 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11100 /* This is the final expression statement of a statement
11101 expression. */
11102 statement = finish_stmt_expr_expr (statement, in_statement_expr);
11103 else if (statement)
11104 statement = finish_expr_stmt (statement);
11106 return statement;
11109 /* Parse a compound-statement.
11111 compound-statement:
11112 { statement-seq [opt] }
11114 GNU extension:
11116 compound-statement:
11117 { label-declaration-seq [opt] statement-seq [opt] }
11119 label-declaration-seq:
11120 label-declaration
11121 label-declaration-seq label-declaration
11123 Returns a tree representing the statement. */
11125 static tree
11126 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
11127 int bcs_flags, bool function_body)
11129 tree compound_stmt;
11131 /* Consume the `{'. */
11132 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
11133 return error_mark_node;
11134 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
11135 && !function_body && cxx_dialect < cxx14)
11136 pedwarn (input_location, OPT_Wpedantic,
11137 "compound-statement in constexpr function");
11138 /* Begin the compound-statement. */
11139 compound_stmt = begin_compound_stmt (bcs_flags);
11140 /* If the next keyword is `__label__' we have a label declaration. */
11141 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
11142 cp_parser_label_declaration (parser);
11143 /* Parse an (optional) statement-seq. */
11144 cp_parser_statement_seq_opt (parser, in_statement_expr);
11145 /* Finish the compound-statement. */
11146 finish_compound_stmt (compound_stmt);
11147 /* Consume the `}'. */
11148 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
11150 return compound_stmt;
11153 /* Parse an (optional) statement-seq.
11155 statement-seq:
11156 statement
11157 statement-seq [opt] statement */
11159 static void
11160 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
11162 /* Scan statements until there aren't any more. */
11163 while (true)
11165 cp_token *token = cp_lexer_peek_token (parser->lexer);
11167 /* If we are looking at a `}', then we have run out of
11168 statements; the same is true if we have reached the end
11169 of file, or have stumbled upon a stray '@end'. */
11170 if (token->type == CPP_CLOSE_BRACE
11171 || token->type == CPP_EOF
11172 || token->type == CPP_PRAGMA_EOL
11173 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
11174 break;
11176 /* If we are in a compound statement and find 'else' then
11177 something went wrong. */
11178 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
11180 if (parser->in_statement & IN_IF_STMT)
11181 break;
11182 else
11184 token = cp_lexer_consume_token (parser->lexer);
11185 error_at (token->location, "%<else%> without a previous %<if%>");
11189 /* Parse the statement. */
11190 cp_parser_statement (parser, in_statement_expr, true, NULL);
11194 /* Return true if we're looking at (init; cond), false otherwise. */
11196 static bool
11197 cp_parser_init_statement_p (cp_parser *parser)
11199 /* Save tokens so that we can put them back. */
11200 cp_lexer_save_tokens (parser->lexer);
11202 /* Look for ';' that is not nested in () or {}. */
11203 int ret = cp_parser_skip_to_closing_parenthesis_1 (parser,
11204 /*recovering=*/false,
11205 CPP_SEMICOLON,
11206 /*consume_paren=*/false);
11208 /* Roll back the tokens we skipped. */
11209 cp_lexer_rollback_tokens (parser->lexer);
11211 return ret == -1;
11214 /* Parse a selection-statement.
11216 selection-statement:
11217 if ( init-statement [opt] condition ) statement
11218 if ( init-statement [opt] condition ) statement else statement
11219 switch ( init-statement [opt] condition ) statement
11221 Returns the new IF_STMT or SWITCH_STMT.
11223 If IF_P is not NULL, *IF_P is set to indicate whether the statement
11224 is a (possibly labeled) if statement which is not enclosed in
11225 braces and has an else clause. This is used to implement
11226 -Wparentheses.
11228 CHAIN is a vector of if-else-if conditions. This is used to implement
11229 -Wduplicated-cond. */
11231 static tree
11232 cp_parser_selection_statement (cp_parser* parser, bool *if_p,
11233 vec<tree> *chain)
11235 cp_token *token;
11236 enum rid keyword;
11237 token_indent_info guard_tinfo;
11239 if (if_p != NULL)
11240 *if_p = false;
11242 /* Peek at the next token. */
11243 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
11244 guard_tinfo = get_token_indent_info (token);
11246 /* See what kind of keyword it is. */
11247 keyword = token->keyword;
11248 switch (keyword)
11250 case RID_IF:
11251 case RID_SWITCH:
11253 tree statement;
11254 tree condition;
11256 bool cx = false;
11257 if (keyword == RID_IF
11258 && cp_lexer_next_token_is_keyword (parser->lexer,
11259 RID_CONSTEXPR))
11261 cx = true;
11262 cp_token *tok = cp_lexer_consume_token (parser->lexer);
11263 if (cxx_dialect < cxx1z && !in_system_header_at (tok->location))
11264 pedwarn (tok->location, 0, "%<if constexpr%> only available "
11265 "with -std=c++1z or -std=gnu++1z");
11268 /* Look for the `('. */
11269 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11271 cp_parser_skip_to_end_of_statement (parser);
11272 return error_mark_node;
11275 /* Begin the selection-statement. */
11276 if (keyword == RID_IF)
11278 statement = begin_if_stmt ();
11279 IF_STMT_CONSTEXPR_P (statement) = cx;
11281 else
11282 statement = begin_switch_stmt ();
11284 /* Parse the optional init-statement. */
11285 if (cp_parser_init_statement_p (parser))
11287 tree decl;
11288 if (cxx_dialect < cxx1z)
11289 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
11290 "init-statement in selection statements only available "
11291 "with -std=c++1z or -std=gnu++1z");
11292 cp_parser_init_statement (parser, &decl);
11295 /* Parse the condition. */
11296 condition = cp_parser_condition (parser);
11297 /* Look for the `)'. */
11298 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11299 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11300 /*consume_paren=*/true);
11302 if (keyword == RID_IF)
11304 bool nested_if;
11305 unsigned char in_statement;
11307 /* Add the condition. */
11308 condition = finish_if_stmt_cond (condition, statement);
11310 if (warn_duplicated_cond)
11311 warn_duplicated_cond_add_or_warn (token->location, condition,
11312 &chain);
11314 /* Parse the then-clause. */
11315 in_statement = parser->in_statement;
11316 parser->in_statement |= IN_IF_STMT;
11318 /* Outside a template, the non-selected branch of a constexpr
11319 if is a 'discarded statement', i.e. unevaluated. */
11320 bool was_discarded = in_discarded_stmt;
11321 bool discard_then = (cx && !processing_template_decl
11322 && integer_zerop (condition));
11323 if (discard_then)
11325 in_discarded_stmt = true;
11326 ++c_inhibit_evaluation_warnings;
11329 cp_parser_implicitly_scoped_statement (parser, &nested_if,
11330 guard_tinfo);
11332 parser->in_statement = in_statement;
11334 finish_then_clause (statement);
11336 if (discard_then)
11338 THEN_CLAUSE (statement) = NULL_TREE;
11339 in_discarded_stmt = was_discarded;
11340 --c_inhibit_evaluation_warnings;
11343 /* If the next token is `else', parse the else-clause. */
11344 if (cp_lexer_next_token_is_keyword (parser->lexer,
11345 RID_ELSE))
11347 bool discard_else = (cx && !processing_template_decl
11348 && integer_nonzerop (condition));
11349 if (discard_else)
11351 in_discarded_stmt = true;
11352 ++c_inhibit_evaluation_warnings;
11355 guard_tinfo
11356 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
11357 /* Consume the `else' keyword. */
11358 cp_lexer_consume_token (parser->lexer);
11359 if (warn_duplicated_cond)
11361 if (cp_lexer_next_token_is_keyword (parser->lexer,
11362 RID_IF)
11363 && chain == NULL)
11365 /* We've got "if (COND) else if (COND2)". Start
11366 the condition chain and add COND as the first
11367 element. */
11368 chain = new vec<tree> ();
11369 if (!CONSTANT_CLASS_P (condition)
11370 && !TREE_SIDE_EFFECTS (condition))
11372 /* Wrap it in a NOP_EXPR so that we can set the
11373 location of the condition. */
11374 tree e = build1 (NOP_EXPR, TREE_TYPE (condition),
11375 condition);
11376 SET_EXPR_LOCATION (e, token->location);
11377 chain->safe_push (e);
11380 else if (!cp_lexer_next_token_is_keyword (parser->lexer,
11381 RID_IF))
11383 /* This is if-else without subsequent if. Zap the
11384 condition chain; we would have already warned at
11385 this point. */
11386 delete chain;
11387 chain = NULL;
11390 begin_else_clause (statement);
11391 /* Parse the else-clause. */
11392 cp_parser_implicitly_scoped_statement (parser, NULL,
11393 guard_tinfo, chain);
11395 finish_else_clause (statement);
11397 /* If we are currently parsing a then-clause, then
11398 IF_P will not be NULL. We set it to true to
11399 indicate that this if statement has an else clause.
11400 This may trigger the Wparentheses warning below
11401 when we get back up to the parent if statement. */
11402 if (if_p != NULL)
11403 *if_p = true;
11405 if (discard_else)
11407 ELSE_CLAUSE (statement) = NULL_TREE;
11408 in_discarded_stmt = was_discarded;
11409 --c_inhibit_evaluation_warnings;
11412 else
11414 /* This if statement does not have an else clause. If
11415 NESTED_IF is true, then the then-clause has an if
11416 statement which does have an else clause. We warn
11417 about the potential ambiguity. */
11418 if (nested_if)
11419 warning_at (EXPR_LOCATION (statement), OPT_Wdangling_else,
11420 "suggest explicit braces to avoid ambiguous"
11421 " %<else%>");
11422 if (warn_duplicated_cond)
11424 /* We don't need the condition chain anymore. */
11425 delete chain;
11426 chain = NULL;
11430 /* Now we're all done with the if-statement. */
11431 finish_if_stmt (statement);
11433 else
11435 bool in_switch_statement_p;
11436 unsigned char in_statement;
11438 /* Add the condition. */
11439 finish_switch_cond (condition, statement);
11441 /* Parse the body of the switch-statement. */
11442 in_switch_statement_p = parser->in_switch_statement_p;
11443 in_statement = parser->in_statement;
11444 parser->in_switch_statement_p = true;
11445 parser->in_statement |= IN_SWITCH_STMT;
11446 cp_parser_implicitly_scoped_statement (parser, if_p,
11447 guard_tinfo);
11448 parser->in_switch_statement_p = in_switch_statement_p;
11449 parser->in_statement = in_statement;
11451 /* Now we're all done with the switch-statement. */
11452 finish_switch_stmt (statement);
11455 return statement;
11457 break;
11459 default:
11460 cp_parser_error (parser, "expected selection-statement");
11461 return error_mark_node;
11465 /* Parse a condition.
11467 condition:
11468 expression
11469 type-specifier-seq declarator = initializer-clause
11470 type-specifier-seq declarator braced-init-list
11472 GNU Extension:
11474 condition:
11475 type-specifier-seq declarator asm-specification [opt]
11476 attributes [opt] = assignment-expression
11478 Returns the expression that should be tested. */
11480 static tree
11481 cp_parser_condition (cp_parser* parser)
11483 cp_decl_specifier_seq type_specifiers;
11484 const char *saved_message;
11485 int declares_class_or_enum;
11487 /* Try the declaration first. */
11488 cp_parser_parse_tentatively (parser);
11489 /* New types are not allowed in the type-specifier-seq for a
11490 condition. */
11491 saved_message = parser->type_definition_forbidden_message;
11492 parser->type_definition_forbidden_message
11493 = G_("types may not be defined in conditions");
11494 /* Parse the type-specifier-seq. */
11495 cp_parser_decl_specifier_seq (parser,
11496 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
11497 &type_specifiers,
11498 &declares_class_or_enum);
11499 /* Restore the saved message. */
11500 parser->type_definition_forbidden_message = saved_message;
11501 /* If all is well, we might be looking at a declaration. */
11502 if (!cp_parser_error_occurred (parser))
11504 tree decl;
11505 tree asm_specification;
11506 tree attributes;
11507 cp_declarator *declarator;
11508 tree initializer = NULL_TREE;
11510 /* Parse the declarator. */
11511 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11512 /*ctor_dtor_or_conv_p=*/NULL,
11513 /*parenthesized_p=*/NULL,
11514 /*member_p=*/false,
11515 /*friend_p=*/false);
11516 /* Parse the attributes. */
11517 attributes = cp_parser_attributes_opt (parser);
11518 /* Parse the asm-specification. */
11519 asm_specification = cp_parser_asm_specification_opt (parser);
11520 /* If the next token is not an `=' or '{', then we might still be
11521 looking at an expression. For example:
11523 if (A(a).x)
11525 looks like a decl-specifier-seq and a declarator -- but then
11526 there is no `=', so this is an expression. */
11527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11528 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11529 cp_parser_simulate_error (parser);
11531 /* If we did see an `=' or '{', then we are looking at a declaration
11532 for sure. */
11533 if (cp_parser_parse_definitely (parser))
11535 tree pushed_scope;
11536 bool non_constant_p;
11537 int flags = LOOKUP_ONLYCONVERTING;
11539 /* Create the declaration. */
11540 decl = start_decl (declarator, &type_specifiers,
11541 /*initialized_p=*/true,
11542 attributes, /*prefix_attributes=*/NULL_TREE,
11543 &pushed_scope);
11545 /* Parse the initializer. */
11546 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11548 initializer = cp_parser_braced_list (parser, &non_constant_p);
11549 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
11550 flags = 0;
11552 else
11554 /* Consume the `='. */
11555 cp_parser_require (parser, CPP_EQ, RT_EQ);
11556 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
11558 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
11559 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11561 /* Process the initializer. */
11562 cp_finish_decl (decl,
11563 initializer, !non_constant_p,
11564 asm_specification,
11565 flags);
11567 if (pushed_scope)
11568 pop_scope (pushed_scope);
11570 return convert_from_reference (decl);
11573 /* If we didn't even get past the declarator successfully, we are
11574 definitely not looking at a declaration. */
11575 else
11576 cp_parser_abort_tentative_parse (parser);
11578 /* Otherwise, we are looking at an expression. */
11579 return cp_parser_expression (parser);
11582 /* Parses a for-statement or range-for-statement until the closing ')',
11583 not included. */
11585 static tree
11586 cp_parser_for (cp_parser *parser, bool ivdep)
11588 tree init, scope, decl;
11589 bool is_range_for;
11591 /* Begin the for-statement. */
11592 scope = begin_for_scope (&init);
11594 /* Parse the initialization. */
11595 is_range_for = cp_parser_init_statement (parser, &decl);
11597 if (is_range_for)
11598 return cp_parser_range_for (parser, scope, init, decl, ivdep);
11599 else
11600 return cp_parser_c_for (parser, scope, init, ivdep);
11603 static tree
11604 cp_parser_c_for (cp_parser *parser, tree scope, tree init, bool ivdep)
11606 /* Normal for loop */
11607 tree condition = NULL_TREE;
11608 tree expression = NULL_TREE;
11609 tree stmt;
11611 stmt = begin_for_stmt (scope, init);
11612 /* The init-statement has already been parsed in
11613 cp_parser_init_statement, so no work is needed here. */
11614 finish_init_stmt (stmt);
11616 /* If there's a condition, process it. */
11617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11618 condition = cp_parser_condition (parser);
11619 else if (ivdep)
11621 cp_parser_error (parser, "missing loop condition in loop with "
11622 "%<GCC ivdep%> pragma");
11623 condition = error_mark_node;
11625 finish_for_cond (condition, stmt, ivdep);
11626 /* Look for the `;'. */
11627 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
11629 /* If there's an expression, process it. */
11630 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
11631 expression = cp_parser_expression (parser);
11632 finish_for_expr (expression, stmt);
11634 return stmt;
11637 /* Tries to parse a range-based for-statement:
11639 range-based-for:
11640 decl-specifier-seq declarator : expression
11642 The decl-specifier-seq declarator and the `:' are already parsed by
11643 cp_parser_init_statement. If processing_template_decl it returns a
11644 newly created RANGE_FOR_STMT; if not, it is converted to a
11645 regular FOR_STMT. */
11647 static tree
11648 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl,
11649 bool ivdep)
11651 tree stmt, range_expr;
11652 auto_vec <cxx_binding *, 16> bindings;
11653 auto_vec <tree, 16> names;
11654 tree decomp_first_name = NULL_TREE;
11655 unsigned int decomp_cnt = 0;
11657 /* Get the range declaration momentarily out of the way so that
11658 the range expression doesn't clash with it. */
11659 if (range_decl != error_mark_node)
11661 if (DECL_HAS_VALUE_EXPR_P (range_decl))
11663 tree v = DECL_VALUE_EXPR (range_decl);
11664 /* For decomposition declaration get all of the corresponding
11665 declarations out of the way. */
11666 if (TREE_CODE (v) == ARRAY_REF
11667 && VAR_P (TREE_OPERAND (v, 0))
11668 && DECL_DECOMPOSITION_P (TREE_OPERAND (v, 0)))
11670 tree d = range_decl;
11671 range_decl = TREE_OPERAND (v, 0);
11672 decomp_cnt = tree_to_uhwi (TREE_OPERAND (v, 1)) + 1;
11673 decomp_first_name = d;
11674 for (unsigned int i = 0; i < decomp_cnt; i++, d = DECL_CHAIN (d))
11676 tree name = DECL_NAME (d);
11677 names.safe_push (name);
11678 bindings.safe_push (IDENTIFIER_BINDING (name));
11679 IDENTIFIER_BINDING (name)
11680 = IDENTIFIER_BINDING (name)->previous;
11684 if (names.is_empty ())
11686 tree name = DECL_NAME (range_decl);
11687 names.safe_push (name);
11688 bindings.safe_push (IDENTIFIER_BINDING (name));
11689 IDENTIFIER_BINDING (name) = IDENTIFIER_BINDING (name)->previous;
11693 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11695 bool expr_non_constant_p;
11696 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
11698 else
11699 range_expr = cp_parser_expression (parser);
11701 /* Put the range declaration(s) back into scope. */
11702 for (unsigned int i = 0; i < names.length (); i++)
11704 cxx_binding *binding = bindings[i];
11705 binding->previous = IDENTIFIER_BINDING (names[i]);
11706 IDENTIFIER_BINDING (names[i]) = binding;
11709 /* If in template, STMT is converted to a normal for-statement
11710 at instantiation. If not, it is done just ahead. */
11711 if (processing_template_decl)
11713 if (check_for_bare_parameter_packs (range_expr))
11714 range_expr = error_mark_node;
11715 stmt = begin_range_for_stmt (scope, init);
11716 if (ivdep)
11717 RANGE_FOR_IVDEP (stmt) = 1;
11718 finish_range_for_decl (stmt, range_decl, range_expr);
11719 if (!type_dependent_expression_p (range_expr)
11720 /* do_auto_deduction doesn't mess with template init-lists. */
11721 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
11722 do_range_for_auto_deduction (range_decl, range_expr);
11724 else
11726 stmt = begin_for_stmt (scope, init);
11727 stmt = cp_convert_range_for (stmt, range_decl, range_expr,
11728 decomp_first_name, decomp_cnt, ivdep);
11730 return stmt;
11733 /* Subroutine of cp_convert_range_for: given the initializer expression,
11734 builds up the range temporary. */
11736 static tree
11737 build_range_temp (tree range_expr)
11739 tree range_type, range_temp;
11741 /* Find out the type deduced by the declaration
11742 `auto &&__range = range_expr'. */
11743 range_type = cp_build_reference_type (make_auto (), true);
11744 range_type = do_auto_deduction (range_type, range_expr,
11745 type_uses_auto (range_type));
11747 /* Create the __range variable. */
11748 range_temp = build_decl (input_location, VAR_DECL,
11749 get_identifier ("__for_range"), range_type);
11750 TREE_USED (range_temp) = 1;
11751 DECL_ARTIFICIAL (range_temp) = 1;
11753 return range_temp;
11756 /* Used by cp_parser_range_for in template context: we aren't going to
11757 do a full conversion yet, but we still need to resolve auto in the
11758 type of the for-range-declaration if present. This is basically
11759 a shortcut version of cp_convert_range_for. */
11761 static void
11762 do_range_for_auto_deduction (tree decl, tree range_expr)
11764 tree auto_node = type_uses_auto (TREE_TYPE (decl));
11765 if (auto_node)
11767 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
11768 range_temp = convert_from_reference (build_range_temp (range_expr));
11769 iter_type = (cp_parser_perform_range_for_lookup
11770 (range_temp, &begin_dummy, &end_dummy));
11771 if (iter_type)
11773 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE,
11774 iter_type);
11775 iter_decl = build_x_indirect_ref (input_location, iter_decl, RO_NULL,
11776 tf_warning_or_error);
11777 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
11778 iter_decl, auto_node);
11783 /* Converts a range-based for-statement into a normal
11784 for-statement, as per the definition.
11786 for (RANGE_DECL : RANGE_EXPR)
11787 BLOCK
11789 should be equivalent to:
11792 auto &&__range = RANGE_EXPR;
11793 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
11794 __begin != __end;
11795 ++__begin)
11797 RANGE_DECL = *__begin;
11798 BLOCK
11802 If RANGE_EXPR is an array:
11803 BEGIN_EXPR = __range
11804 END_EXPR = __range + ARRAY_SIZE(__range)
11805 Else if RANGE_EXPR has a member 'begin' or 'end':
11806 BEGIN_EXPR = __range.begin()
11807 END_EXPR = __range.end()
11808 Else:
11809 BEGIN_EXPR = begin(__range)
11810 END_EXPR = end(__range);
11812 If __range has a member 'begin' but not 'end', or vice versa, we must
11813 still use the second alternative (it will surely fail, however).
11814 When calling begin()/end() in the third alternative we must use
11815 argument dependent lookup, but always considering 'std' as an associated
11816 namespace. */
11818 tree
11819 cp_convert_range_for (tree statement, tree range_decl, tree range_expr,
11820 tree decomp_first_name, unsigned int decomp_cnt,
11821 bool ivdep)
11823 tree begin, end;
11824 tree iter_type, begin_expr, end_expr;
11825 tree condition, expression;
11827 if (range_decl == error_mark_node || range_expr == error_mark_node)
11828 /* If an error happened previously do nothing or else a lot of
11829 unhelpful errors would be issued. */
11830 begin_expr = end_expr = iter_type = error_mark_node;
11831 else
11833 tree range_temp;
11835 if (VAR_P (range_expr)
11836 && array_of_runtime_bound_p (TREE_TYPE (range_expr)))
11837 /* Can't bind a reference to an array of runtime bound. */
11838 range_temp = range_expr;
11839 else
11841 range_temp = build_range_temp (range_expr);
11842 pushdecl (range_temp);
11843 cp_finish_decl (range_temp, range_expr,
11844 /*is_constant_init*/false, NULL_TREE,
11845 LOOKUP_ONLYCONVERTING);
11846 range_temp = convert_from_reference (range_temp);
11848 iter_type = cp_parser_perform_range_for_lookup (range_temp,
11849 &begin_expr, &end_expr);
11852 /* The new for initialization statement. */
11853 begin = build_decl (input_location, VAR_DECL,
11854 get_identifier ("__for_begin"), iter_type);
11855 TREE_USED (begin) = 1;
11856 DECL_ARTIFICIAL (begin) = 1;
11857 pushdecl (begin);
11858 cp_finish_decl (begin, begin_expr,
11859 /*is_constant_init*/false, NULL_TREE,
11860 LOOKUP_ONLYCONVERTING);
11862 if (cxx_dialect >= cxx1z)
11863 iter_type = cv_unqualified (TREE_TYPE (end_expr));
11864 end = build_decl (input_location, VAR_DECL,
11865 get_identifier ("__for_end"), iter_type);
11866 TREE_USED (end) = 1;
11867 DECL_ARTIFICIAL (end) = 1;
11868 pushdecl (end);
11869 cp_finish_decl (end, end_expr,
11870 /*is_constant_init*/false, NULL_TREE,
11871 LOOKUP_ONLYCONVERTING);
11873 finish_init_stmt (statement);
11875 /* The new for condition. */
11876 condition = build_x_binary_op (input_location, NE_EXPR,
11877 begin, ERROR_MARK,
11878 end, ERROR_MARK,
11879 NULL, tf_warning_or_error);
11880 finish_for_cond (condition, statement, ivdep);
11882 /* The new increment expression. */
11883 expression = finish_unary_op_expr (input_location,
11884 PREINCREMENT_EXPR, begin,
11885 tf_warning_or_error);
11886 finish_for_expr (expression, statement);
11888 /* The declaration is initialized with *__begin inside the loop body. */
11889 cp_finish_decl (range_decl,
11890 build_x_indirect_ref (input_location, begin, RO_NULL,
11891 tf_warning_or_error),
11892 /*is_constant_init*/false, NULL_TREE,
11893 LOOKUP_ONLYCONVERTING);
11894 if (VAR_P (range_decl) && DECL_DECOMPOSITION_P (range_decl))
11895 cp_finish_decomp (range_decl, decomp_first_name, decomp_cnt);
11897 return statement;
11900 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
11901 We need to solve both at the same time because the method used
11902 depends on the existence of members begin or end.
11903 Returns the type deduced for the iterator expression. */
11905 static tree
11906 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
11908 if (error_operand_p (range))
11910 *begin = *end = error_mark_node;
11911 return error_mark_node;
11914 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
11916 error ("range-based %<for%> expression of type %qT "
11917 "has incomplete type", TREE_TYPE (range));
11918 *begin = *end = error_mark_node;
11919 return error_mark_node;
11921 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
11923 /* If RANGE is an array, we will use pointer arithmetic. */
11924 *begin = decay_conversion (range, tf_warning_or_error);
11925 *end = build_binary_op (input_location, PLUS_EXPR,
11926 range,
11927 array_type_nelts_top (TREE_TYPE (range)),
11929 return TREE_TYPE (*begin);
11931 else
11933 /* If it is not an array, we must do a bit of magic. */
11934 tree id_begin, id_end;
11935 tree member_begin, member_end;
11937 *begin = *end = error_mark_node;
11939 id_begin = get_identifier ("begin");
11940 id_end = get_identifier ("end");
11941 member_begin = lookup_member (TREE_TYPE (range), id_begin,
11942 /*protect=*/2, /*want_type=*/false,
11943 tf_warning_or_error);
11944 member_end = lookup_member (TREE_TYPE (range), id_end,
11945 /*protect=*/2, /*want_type=*/false,
11946 tf_warning_or_error);
11948 if (member_begin != NULL_TREE || member_end != NULL_TREE)
11950 /* Use the member functions. */
11951 if (member_begin != NULL_TREE)
11952 *begin = cp_parser_range_for_member_function (range, id_begin);
11953 else
11954 error ("range-based %<for%> expression of type %qT has an "
11955 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
11957 if (member_end != NULL_TREE)
11958 *end = cp_parser_range_for_member_function (range, id_end);
11959 else
11960 error ("range-based %<for%> expression of type %qT has a "
11961 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
11963 else
11965 /* Use global functions with ADL. */
11966 vec<tree, va_gc> *vec;
11967 vec = make_tree_vector ();
11969 vec_safe_push (vec, range);
11971 member_begin = perform_koenig_lookup (id_begin, vec,
11972 tf_warning_or_error);
11973 *begin = finish_call_expr (member_begin, &vec, false, true,
11974 tf_warning_or_error);
11975 member_end = perform_koenig_lookup (id_end, vec,
11976 tf_warning_or_error);
11977 *end = finish_call_expr (member_end, &vec, false, true,
11978 tf_warning_or_error);
11980 release_tree_vector (vec);
11983 /* Last common checks. */
11984 if (*begin == error_mark_node || *end == error_mark_node)
11986 /* If one of the expressions is an error do no more checks. */
11987 *begin = *end = error_mark_node;
11988 return error_mark_node;
11990 else if (type_dependent_expression_p (*begin)
11991 || type_dependent_expression_p (*end))
11992 /* Can happen, when, eg, in a template context, Koenig lookup
11993 can't resolve begin/end (c++/58503). */
11994 return NULL_TREE;
11995 else
11997 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
11998 /* The unqualified type of the __begin and __end temporaries should
11999 be the same, as required by the multiple auto declaration. */
12000 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
12002 if (cxx_dialect >= cxx1z
12003 && (build_x_binary_op (input_location, NE_EXPR,
12004 *begin, ERROR_MARK,
12005 *end, ERROR_MARK,
12006 NULL, tf_none)
12007 != error_mark_node))
12008 /* P0184R0 allows __begin and __end to have different types,
12009 but make sure they are comparable so we can give a better
12010 diagnostic. */;
12011 else
12012 error ("inconsistent begin/end types in range-based %<for%> "
12013 "statement: %qT and %qT",
12014 TREE_TYPE (*begin), TREE_TYPE (*end));
12016 return iter_type;
12021 /* Helper function for cp_parser_perform_range_for_lookup.
12022 Builds a tree for RANGE.IDENTIFIER(). */
12024 static tree
12025 cp_parser_range_for_member_function (tree range, tree identifier)
12027 tree member, res;
12028 vec<tree, va_gc> *vec;
12030 member = finish_class_member_access_expr (range, identifier,
12031 false, tf_warning_or_error);
12032 if (member == error_mark_node)
12033 return error_mark_node;
12035 vec = make_tree_vector ();
12036 res = finish_call_expr (member, &vec,
12037 /*disallow_virtual=*/false,
12038 /*koenig_p=*/false,
12039 tf_warning_or_error);
12040 release_tree_vector (vec);
12041 return res;
12044 /* Parse an iteration-statement.
12046 iteration-statement:
12047 while ( condition ) statement
12048 do statement while ( expression ) ;
12049 for ( init-statement condition [opt] ; expression [opt] )
12050 statement
12052 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
12054 static tree
12055 cp_parser_iteration_statement (cp_parser* parser, bool *if_p, bool ivdep)
12057 cp_token *token;
12058 enum rid keyword;
12059 tree statement;
12060 unsigned char in_statement;
12061 token_indent_info guard_tinfo;
12063 /* Peek at the next token. */
12064 token = cp_parser_require (parser, CPP_KEYWORD, RT_ITERATION);
12065 if (!token)
12066 return error_mark_node;
12068 guard_tinfo = get_token_indent_info (token);
12070 /* Remember whether or not we are already within an iteration
12071 statement. */
12072 in_statement = parser->in_statement;
12074 /* See what kind of keyword it is. */
12075 keyword = token->keyword;
12076 switch (keyword)
12078 case RID_WHILE:
12080 tree condition;
12082 /* Begin the while-statement. */
12083 statement = begin_while_stmt ();
12084 /* Look for the `('. */
12085 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12086 /* Parse the condition. */
12087 condition = cp_parser_condition (parser);
12088 finish_while_stmt_cond (condition, statement, ivdep);
12089 /* Look for the `)'. */
12090 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12091 /* Parse the dependent statement. */
12092 parser->in_statement = IN_ITERATION_STMT;
12093 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12094 parser->in_statement = in_statement;
12095 /* We're done with the while-statement. */
12096 finish_while_stmt (statement);
12098 break;
12100 case RID_DO:
12102 tree expression;
12104 /* Begin the do-statement. */
12105 statement = begin_do_stmt ();
12106 /* Parse the body of the do-statement. */
12107 parser->in_statement = IN_ITERATION_STMT;
12108 cp_parser_implicitly_scoped_statement (parser, NULL, guard_tinfo);
12109 parser->in_statement = in_statement;
12110 finish_do_body (statement);
12111 /* Look for the `while' keyword. */
12112 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
12113 /* Look for the `('. */
12114 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12115 /* Parse the expression. */
12116 expression = cp_parser_expression (parser);
12117 /* We're done with the do-statement. */
12118 finish_do_stmt (expression, statement, ivdep);
12119 /* Look for the `)'. */
12120 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12121 /* Look for the `;'. */
12122 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12124 break;
12126 case RID_FOR:
12128 /* Look for the `('. */
12129 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
12131 statement = cp_parser_for (parser, ivdep);
12133 /* Look for the `)'. */
12134 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
12136 /* Parse the body of the for-statement. */
12137 parser->in_statement = IN_ITERATION_STMT;
12138 cp_parser_already_scoped_statement (parser, if_p, guard_tinfo);
12139 parser->in_statement = in_statement;
12141 /* We're done with the for-statement. */
12142 finish_for_stmt (statement);
12144 break;
12146 default:
12147 cp_parser_error (parser, "expected iteration-statement");
12148 statement = error_mark_node;
12149 break;
12152 return statement;
12155 /* Parse a init-statement or the declarator of a range-based-for.
12156 Returns true if a range-based-for declaration is seen.
12158 init-statement:
12159 expression-statement
12160 simple-declaration */
12162 static bool
12163 cp_parser_init_statement (cp_parser* parser, tree *decl)
12165 /* If the next token is a `;', then we have an empty
12166 expression-statement. Grammatically, this is also a
12167 simple-declaration, but an invalid one, because it does not
12168 declare anything. Therefore, if we did not handle this case
12169 specially, we would issue an error message about an invalid
12170 declaration. */
12171 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12173 bool is_range_for = false;
12174 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
12176 /* A colon is used in range-based for. */
12177 parser->colon_corrects_to_scope_p = false;
12179 /* We're going to speculatively look for a declaration, falling back
12180 to an expression, if necessary. */
12181 cp_parser_parse_tentatively (parser);
12182 /* Parse the declaration. */
12183 cp_parser_simple_declaration (parser,
12184 /*function_definition_allowed_p=*/false,
12185 decl);
12186 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
12187 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12189 /* It is a range-for, consume the ':' */
12190 cp_lexer_consume_token (parser->lexer);
12191 is_range_for = true;
12192 if (cxx_dialect < cxx11)
12194 pedwarn (cp_lexer_peek_token (parser->lexer)->location, 0,
12195 "range-based %<for%> loops only available with "
12196 "-std=c++11 or -std=gnu++11");
12197 *decl = error_mark_node;
12200 else
12201 /* The ';' is not consumed yet because we told
12202 cp_parser_simple_declaration not to. */
12203 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12205 if (cp_parser_parse_definitely (parser))
12206 return is_range_for;
12207 /* If the tentative parse failed, then we shall need to look for an
12208 expression-statement. */
12210 /* If we are here, it is an expression-statement. */
12211 cp_parser_expression_statement (parser, NULL_TREE);
12212 return false;
12215 /* Parse a jump-statement.
12217 jump-statement:
12218 break ;
12219 continue ;
12220 return expression [opt] ;
12221 return braced-init-list ;
12222 goto identifier ;
12224 GNU extension:
12226 jump-statement:
12227 goto * expression ;
12229 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
12231 static tree
12232 cp_parser_jump_statement (cp_parser* parser)
12234 tree statement = error_mark_node;
12235 cp_token *token;
12236 enum rid keyword;
12237 unsigned char in_statement;
12239 /* Peek at the next token. */
12240 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
12241 if (!token)
12242 return error_mark_node;
12244 /* See what kind of keyword it is. */
12245 keyword = token->keyword;
12246 switch (keyword)
12248 case RID_BREAK:
12249 in_statement = parser->in_statement & ~IN_IF_STMT;
12250 switch (in_statement)
12252 case 0:
12253 error_at (token->location, "break statement not within loop or switch");
12254 break;
12255 default:
12256 gcc_assert ((in_statement & IN_SWITCH_STMT)
12257 || in_statement == IN_ITERATION_STMT);
12258 statement = finish_break_stmt ();
12259 if (in_statement == IN_ITERATION_STMT)
12260 break_maybe_infinite_loop ();
12261 break;
12262 case IN_OMP_BLOCK:
12263 error_at (token->location, "invalid exit from OpenMP structured block");
12264 break;
12265 case IN_OMP_FOR:
12266 error_at (token->location, "break statement used with OpenMP for loop");
12267 break;
12268 case IN_CILK_SIMD_FOR:
12269 error_at (token->location, "break statement used with Cilk Plus for loop");
12270 break;
12272 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12273 break;
12275 case RID_CONTINUE:
12276 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
12278 case 0:
12279 error_at (token->location, "continue statement not within a loop");
12280 break;
12281 case IN_CILK_SIMD_FOR:
12282 error_at (token->location,
12283 "continue statement within %<#pragma simd%> loop body");
12284 /* Fall through. */
12285 case IN_ITERATION_STMT:
12286 case IN_OMP_FOR:
12287 statement = finish_continue_stmt ();
12288 break;
12289 case IN_OMP_BLOCK:
12290 error_at (token->location, "invalid exit from OpenMP structured block");
12291 break;
12292 default:
12293 gcc_unreachable ();
12295 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12296 break;
12298 case RID_RETURN:
12300 tree expr;
12301 bool expr_non_constant_p;
12303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12305 cp_lexer_set_source_position (parser->lexer);
12306 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
12307 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
12309 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12310 expr = cp_parser_expression (parser);
12311 else
12312 /* If the next token is a `;', then there is no
12313 expression. */
12314 expr = NULL_TREE;
12315 /* Build the return-statement. */
12316 if (current_function_auto_return_pattern && in_discarded_stmt)
12317 /* Don't deduce from a discarded return statement. */;
12318 else
12319 statement = finish_return_stmt (expr);
12320 /* Look for the final `;'. */
12321 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12323 break;
12325 case RID_GOTO:
12326 if (parser->in_function_body
12327 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
12329 error ("%<goto%> in %<constexpr%> function");
12330 cp_function_chain->invalid_constexpr = true;
12333 /* Create the goto-statement. */
12334 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
12336 /* Issue a warning about this use of a GNU extension. */
12337 pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
12338 /* Consume the '*' token. */
12339 cp_lexer_consume_token (parser->lexer);
12340 /* Parse the dependent expression. */
12341 finish_goto_stmt (cp_parser_expression (parser));
12343 else
12344 finish_goto_stmt (cp_parser_identifier (parser));
12345 /* Look for the final `;'. */
12346 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
12347 break;
12349 default:
12350 cp_parser_error (parser, "expected jump-statement");
12351 break;
12354 return statement;
12357 /* Parse a declaration-statement.
12359 declaration-statement:
12360 block-declaration */
12362 static void
12363 cp_parser_declaration_statement (cp_parser* parser)
12365 void *p;
12367 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12368 p = obstack_alloc (&declarator_obstack, 0);
12370 /* Parse the block-declaration. */
12371 cp_parser_block_declaration (parser, /*statement_p=*/true);
12373 /* Free any declarators allocated. */
12374 obstack_free (&declarator_obstack, p);
12377 /* Some dependent statements (like `if (cond) statement'), are
12378 implicitly in their own scope. In other words, if the statement is
12379 a single statement (as opposed to a compound-statement), it is
12380 none-the-less treated as if it were enclosed in braces. Any
12381 declarations appearing in the dependent statement are out of scope
12382 after control passes that point. This function parses a statement,
12383 but ensures that is in its own scope, even if it is not a
12384 compound-statement.
12386 If IF_P is not NULL, *IF_P is set to indicate whether the statement
12387 is a (possibly labeled) if statement which is not enclosed in
12388 braces and has an else clause. This is used to implement
12389 -Wparentheses.
12391 CHAIN is a vector of if-else-if conditions. This is used to implement
12392 -Wduplicated-cond.
12394 Returns the new statement. */
12396 static tree
12397 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p,
12398 const token_indent_info &guard_tinfo,
12399 vec<tree> *chain)
12401 tree statement;
12402 location_t body_loc = cp_lexer_peek_token (parser->lexer)->location;
12403 location_t body_loc_after_labels = UNKNOWN_LOCATION;
12404 token_indent_info body_tinfo
12405 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12407 if (if_p != NULL)
12408 *if_p = false;
12410 /* Mark if () ; with a special NOP_EXPR. */
12411 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12413 cp_lexer_consume_token (parser->lexer);
12414 statement = add_stmt (build_empty_stmt (body_loc));
12416 if (guard_tinfo.keyword == RID_IF
12417 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
12418 warning_at (body_loc, OPT_Wempty_body,
12419 "suggest braces around empty body in an %<if%> statement");
12420 else if (guard_tinfo.keyword == RID_ELSE)
12421 warning_at (body_loc, OPT_Wempty_body,
12422 "suggest braces around empty body in an %<else%> statement");
12424 /* if a compound is opened, we simply parse the statement directly. */
12425 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12426 statement = cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
12427 /* If the token is not a `{', then we must take special action. */
12428 else
12430 /* Create a compound-statement. */
12431 statement = begin_compound_stmt (0);
12432 /* Parse the dependent-statement. */
12433 cp_parser_statement (parser, NULL_TREE, false, if_p, chain,
12434 &body_loc_after_labels);
12435 /* Finish the dummy compound-statement. */
12436 finish_compound_stmt (statement);
12439 token_indent_info next_tinfo
12440 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12441 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12443 if (body_loc_after_labels != UNKNOWN_LOCATION
12444 && next_tinfo.type != CPP_SEMICOLON)
12445 warn_for_multistatement_macros (body_loc_after_labels, next_tinfo.location,
12446 guard_tinfo.location, guard_tinfo.keyword);
12448 /* Return the statement. */
12449 return statement;
12452 /* For some dependent statements (like `while (cond) statement'), we
12453 have already created a scope. Therefore, even if the dependent
12454 statement is a compound-statement, we do not want to create another
12455 scope. */
12457 static void
12458 cp_parser_already_scoped_statement (cp_parser* parser, bool *if_p,
12459 const token_indent_info &guard_tinfo)
12461 /* If the token is a `{', then we must take special action. */
12462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12464 token_indent_info body_tinfo
12465 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12466 location_t loc_after_labels = UNKNOWN_LOCATION;
12468 cp_parser_statement (parser, NULL_TREE, false, if_p, NULL,
12469 &loc_after_labels);
12470 token_indent_info next_tinfo
12471 = get_token_indent_info (cp_lexer_peek_token (parser->lexer));
12472 warn_for_misleading_indentation (guard_tinfo, body_tinfo, next_tinfo);
12474 if (loc_after_labels != UNKNOWN_LOCATION
12475 && next_tinfo.type != CPP_SEMICOLON)
12476 warn_for_multistatement_macros (loc_after_labels, next_tinfo.location,
12477 guard_tinfo.location,
12478 guard_tinfo.keyword);
12480 else
12482 /* Avoid calling cp_parser_compound_statement, so that we
12483 don't create a new scope. Do everything else by hand. */
12484 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
12485 /* If the next keyword is `__label__' we have a label declaration. */
12486 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
12487 cp_parser_label_declaration (parser);
12488 /* Parse an (optional) statement-seq. */
12489 cp_parser_statement_seq_opt (parser, NULL_TREE);
12490 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
12494 /* Declarations [gram.dcl.dcl] */
12496 /* Parse an optional declaration-sequence.
12498 declaration-seq:
12499 declaration
12500 declaration-seq declaration */
12502 static void
12503 cp_parser_declaration_seq_opt (cp_parser* parser)
12505 while (true)
12507 cp_token *token;
12509 token = cp_lexer_peek_token (parser->lexer);
12511 if (token->type == CPP_CLOSE_BRACE
12512 || token->type == CPP_EOF
12513 || token->type == CPP_PRAGMA_EOL)
12514 break;
12516 if (token->type == CPP_SEMICOLON)
12518 /* A declaration consisting of a single semicolon is
12519 invalid. Allow it unless we're being pedantic. */
12520 cp_lexer_consume_token (parser->lexer);
12521 if (!in_system_header_at (input_location))
12522 pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
12523 continue;
12526 /* If we're entering or exiting a region that's implicitly
12527 extern "C", modify the lang context appropriately. */
12528 if (!parser->implicit_extern_c && token->implicit_extern_c)
12530 push_lang_context (lang_name_c);
12531 parser->implicit_extern_c = true;
12533 else if (parser->implicit_extern_c && !token->implicit_extern_c)
12535 pop_lang_context ();
12536 parser->implicit_extern_c = false;
12539 if (token->type == CPP_PRAGMA)
12541 /* A top-level declaration can consist solely of a #pragma.
12542 A nested declaration cannot, so this is done here and not
12543 in cp_parser_declaration. (A #pragma at block scope is
12544 handled in cp_parser_statement.) */
12545 cp_parser_pragma (parser, pragma_external, NULL);
12546 continue;
12549 /* Parse the declaration itself. */
12550 cp_parser_declaration (parser);
12554 /* Parse a declaration.
12556 declaration:
12557 block-declaration
12558 function-definition
12559 template-declaration
12560 explicit-instantiation
12561 explicit-specialization
12562 linkage-specification
12563 namespace-definition
12565 C++17:
12566 deduction-guide
12568 GNU extension:
12570 declaration:
12571 __extension__ declaration */
12573 static void
12574 cp_parser_declaration (cp_parser* parser)
12576 cp_token token1;
12577 cp_token token2;
12578 int saved_pedantic;
12579 void *p;
12580 tree attributes = NULL_TREE;
12582 /* Check for the `__extension__' keyword. */
12583 if (cp_parser_extension_opt (parser, &saved_pedantic))
12585 /* Parse the qualified declaration. */
12586 cp_parser_declaration (parser);
12587 /* Restore the PEDANTIC flag. */
12588 pedantic = saved_pedantic;
12590 return;
12593 /* Try to figure out what kind of declaration is present. */
12594 token1 = *cp_lexer_peek_token (parser->lexer);
12596 if (token1.type != CPP_EOF)
12597 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
12598 else
12600 token2.type = CPP_EOF;
12601 token2.keyword = RID_MAX;
12604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
12605 p = obstack_alloc (&declarator_obstack, 0);
12607 /* If the next token is `extern' and the following token is a string
12608 literal, then we have a linkage specification. */
12609 if (token1.keyword == RID_EXTERN
12610 && cp_parser_is_pure_string_literal (&token2))
12611 cp_parser_linkage_specification (parser);
12612 /* If the next token is `template', then we have either a template
12613 declaration, an explicit instantiation, or an explicit
12614 specialization. */
12615 else if (token1.keyword == RID_TEMPLATE)
12617 /* `template <>' indicates a template specialization. */
12618 if (token2.type == CPP_LESS
12619 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
12620 cp_parser_explicit_specialization (parser);
12621 /* `template <' indicates a template declaration. */
12622 else if (token2.type == CPP_LESS)
12623 cp_parser_template_declaration (parser, /*member_p=*/false);
12624 /* Anything else must be an explicit instantiation. */
12625 else
12626 cp_parser_explicit_instantiation (parser);
12628 /* If the next token is `export', then we have a template
12629 declaration. */
12630 else if (token1.keyword == RID_EXPORT)
12631 cp_parser_template_declaration (parser, /*member_p=*/false);
12632 /* If the next token is `extern', 'static' or 'inline' and the one
12633 after that is `template', we have a GNU extended explicit
12634 instantiation directive. */
12635 else if (cp_parser_allow_gnu_extensions_p (parser)
12636 && (token1.keyword == RID_EXTERN
12637 || token1.keyword == RID_STATIC
12638 || token1.keyword == RID_INLINE)
12639 && token2.keyword == RID_TEMPLATE)
12640 cp_parser_explicit_instantiation (parser);
12641 /* If the next token is `namespace', check for a named or unnamed
12642 namespace definition. */
12643 else if (token1.keyword == RID_NAMESPACE
12644 && (/* A named namespace definition. */
12645 (token2.type == CPP_NAME
12646 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
12647 != CPP_EQ))
12648 || (token2.type == CPP_OPEN_SQUARE
12649 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
12650 == CPP_OPEN_SQUARE)
12651 /* An unnamed namespace definition. */
12652 || token2.type == CPP_OPEN_BRACE
12653 || token2.keyword == RID_ATTRIBUTE))
12654 cp_parser_namespace_definition (parser);
12655 /* An inline (associated) namespace definition. */
12656 else if (token1.keyword == RID_INLINE
12657 && token2.keyword == RID_NAMESPACE)
12658 cp_parser_namespace_definition (parser);
12659 /* Objective-C++ declaration/definition. */
12660 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
12661 cp_parser_objc_declaration (parser, NULL_TREE);
12662 else if (c_dialect_objc ()
12663 && token1.keyword == RID_ATTRIBUTE
12664 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
12665 cp_parser_objc_declaration (parser, attributes);
12666 /* At this point we may have a template declared by a concept
12667 introduction. */
12668 else if (flag_concepts
12669 && cp_parser_template_declaration_after_export (parser,
12670 /*member_p=*/false))
12671 /* We did. */;
12672 else
12673 /* Try to parse a block-declaration, or a function-definition. */
12674 cp_parser_block_declaration (parser, /*statement_p=*/false);
12676 /* Free any declarators allocated. */
12677 obstack_free (&declarator_obstack, p);
12680 /* Parse a block-declaration.
12682 block-declaration:
12683 simple-declaration
12684 asm-definition
12685 namespace-alias-definition
12686 using-declaration
12687 using-directive
12689 GNU Extension:
12691 block-declaration:
12692 __extension__ block-declaration
12694 C++0x Extension:
12696 block-declaration:
12697 static_assert-declaration
12699 If STATEMENT_P is TRUE, then this block-declaration is occurring as
12700 part of a declaration-statement. */
12702 static void
12703 cp_parser_block_declaration (cp_parser *parser,
12704 bool statement_p)
12706 cp_token *token1;
12707 int saved_pedantic;
12709 /* Check for the `__extension__' keyword. */
12710 if (cp_parser_extension_opt (parser, &saved_pedantic))
12712 /* Parse the qualified declaration. */
12713 cp_parser_block_declaration (parser, statement_p);
12714 /* Restore the PEDANTIC flag. */
12715 pedantic = saved_pedantic;
12717 return;
12720 /* Peek at the next token to figure out which kind of declaration is
12721 present. */
12722 token1 = cp_lexer_peek_token (parser->lexer);
12724 /* If the next keyword is `asm', we have an asm-definition. */
12725 if (token1->keyword == RID_ASM)
12727 if (statement_p)
12728 cp_parser_commit_to_tentative_parse (parser);
12729 cp_parser_asm_definition (parser);
12731 /* If the next keyword is `namespace', we have a
12732 namespace-alias-definition. */
12733 else if (token1->keyword == RID_NAMESPACE)
12734 cp_parser_namespace_alias_definition (parser);
12735 /* If the next keyword is `using', we have a
12736 using-declaration, a using-directive, or an alias-declaration. */
12737 else if (token1->keyword == RID_USING)
12739 cp_token *token2;
12741 if (statement_p)
12742 cp_parser_commit_to_tentative_parse (parser);
12743 /* If the token after `using' is `namespace', then we have a
12744 using-directive. */
12745 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12746 if (token2->keyword == RID_NAMESPACE)
12747 cp_parser_using_directive (parser);
12748 /* If the second token after 'using' is '=', then we have an
12749 alias-declaration. */
12750 else if (cxx_dialect >= cxx11
12751 && token2->type == CPP_NAME
12752 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
12753 || (cp_nth_tokens_can_be_attribute_p (parser, 3))))
12754 cp_parser_alias_declaration (parser);
12755 /* Otherwise, it's a using-declaration. */
12756 else
12757 cp_parser_using_declaration (parser,
12758 /*access_declaration_p=*/false);
12760 /* If the next keyword is `__label__' we have a misplaced label
12761 declaration. */
12762 else if (token1->keyword == RID_LABEL)
12764 cp_lexer_consume_token (parser->lexer);
12765 error_at (token1->location, "%<__label__%> not at the beginning of a block");
12766 cp_parser_skip_to_end_of_statement (parser);
12767 /* If the next token is now a `;', consume it. */
12768 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12769 cp_lexer_consume_token (parser->lexer);
12771 /* If the next token is `static_assert' we have a static assertion. */
12772 else if (token1->keyword == RID_STATIC_ASSERT)
12773 cp_parser_static_assert (parser, /*member_p=*/false);
12774 /* Anything else must be a simple-declaration. */
12775 else
12776 cp_parser_simple_declaration (parser, !statement_p,
12777 /*maybe_range_for_decl*/NULL);
12780 /* Parse a simple-declaration.
12782 simple-declaration:
12783 decl-specifier-seq [opt] init-declarator-list [opt] ;
12784 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
12785 brace-or-equal-initializer ;
12787 init-declarator-list:
12788 init-declarator
12789 init-declarator-list , init-declarator
12791 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
12792 function-definition as a simple-declaration.
12794 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
12795 parsed declaration if it is an uninitialized single declarator not followed
12796 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
12797 if present, will not be consumed. */
12799 static void
12800 cp_parser_simple_declaration (cp_parser* parser,
12801 bool function_definition_allowed_p,
12802 tree *maybe_range_for_decl)
12804 cp_decl_specifier_seq decl_specifiers;
12805 int declares_class_or_enum;
12806 bool saw_declarator;
12807 location_t comma_loc = UNKNOWN_LOCATION;
12808 location_t init_loc = UNKNOWN_LOCATION;
12810 if (maybe_range_for_decl)
12811 *maybe_range_for_decl = NULL_TREE;
12813 /* Defer access checks until we know what is being declared; the
12814 checks for names appearing in the decl-specifier-seq should be
12815 done as if we were in the scope of the thing being declared. */
12816 push_deferring_access_checks (dk_deferred);
12818 /* Parse the decl-specifier-seq. We have to keep track of whether
12819 or not the decl-specifier-seq declares a named class or
12820 enumeration type, since that is the only case in which the
12821 init-declarator-list is allowed to be empty.
12823 [dcl.dcl]
12825 In a simple-declaration, the optional init-declarator-list can be
12826 omitted only when declaring a class or enumeration, that is when
12827 the decl-specifier-seq contains either a class-specifier, an
12828 elaborated-type-specifier, or an enum-specifier. */
12829 cp_parser_decl_specifier_seq (parser,
12830 CP_PARSER_FLAGS_OPTIONAL,
12831 &decl_specifiers,
12832 &declares_class_or_enum);
12833 /* We no longer need to defer access checks. */
12834 stop_deferring_access_checks ();
12836 /* In a block scope, a valid declaration must always have a
12837 decl-specifier-seq. By not trying to parse declarators, we can
12838 resolve the declaration/expression ambiguity more quickly. */
12839 if (!function_definition_allowed_p
12840 && !decl_specifiers.any_specifiers_p)
12842 cp_parser_error (parser, "expected declaration");
12843 goto done;
12846 /* If the next two tokens are both identifiers, the code is
12847 erroneous. The usual cause of this situation is code like:
12849 T t;
12851 where "T" should name a type -- but does not. */
12852 if (!decl_specifiers.any_type_specifiers_p
12853 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12855 /* If parsing tentatively, we should commit; we really are
12856 looking at a declaration. */
12857 cp_parser_commit_to_tentative_parse (parser);
12858 /* Give up. */
12859 goto done;
12862 /* If we have seen at least one decl-specifier, and the next token
12863 is not a parenthesis, then we must be looking at a declaration.
12864 (After "int (" we might be looking at a functional cast.) */
12865 if (decl_specifiers.any_specifiers_p
12866 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
12867 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
12868 && !cp_parser_error_occurred (parser))
12869 cp_parser_commit_to_tentative_parse (parser);
12871 /* Look for C++17 decomposition declaration. */
12872 for (size_t n = 1; ; n++)
12873 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_AND)
12874 || cp_lexer_nth_token_is (parser->lexer, n, CPP_AND_AND))
12875 continue;
12876 else if (cp_lexer_nth_token_is (parser->lexer, n, CPP_OPEN_SQUARE)
12877 && !cp_lexer_nth_token_is (parser->lexer, n + 1, CPP_OPEN_SQUARE)
12878 && decl_specifiers.any_specifiers_p)
12880 tree decl
12881 = cp_parser_decomposition_declaration (parser, &decl_specifiers,
12882 maybe_range_for_decl,
12883 &init_loc);
12885 /* The next token should be either a `,' or a `;'. */
12886 cp_token *token = cp_lexer_peek_token (parser->lexer);
12887 /* If it's a `;', we are done. */
12888 if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
12889 goto finish;
12890 /* Anything else is an error. */
12891 else
12893 /* If we have already issued an error message we don't need
12894 to issue another one. */
12895 if ((decl != error_mark_node
12896 && DECL_INITIAL (decl) != error_mark_node)
12897 || cp_parser_uncommitted_to_tentative_parse_p (parser))
12898 cp_parser_error (parser, "expected %<,%> or %<;%>");
12899 /* Skip tokens until we reach the end of the statement. */
12900 cp_parser_skip_to_end_of_statement (parser);
12901 /* If the next token is now a `;', consume it. */
12902 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12903 cp_lexer_consume_token (parser->lexer);
12904 goto done;
12907 else
12908 break;
12910 tree last_type;
12911 bool auto_specifier_p;
12912 /* NULL_TREE if both variable and function declaration are allowed,
12913 error_mark_node if function declaration are not allowed and
12914 a FUNCTION_DECL that should be diagnosed if it is followed by
12915 variable declarations. */
12916 tree auto_function_declaration;
12918 last_type = NULL_TREE;
12919 auto_specifier_p
12920 = decl_specifiers.type && type_uses_auto (decl_specifiers.type);
12921 auto_function_declaration = NULL_TREE;
12923 /* Keep going until we hit the `;' at the end of the simple
12924 declaration. */
12925 saw_declarator = false;
12926 while (cp_lexer_next_token_is_not (parser->lexer,
12927 CPP_SEMICOLON))
12929 cp_token *token;
12930 bool function_definition_p;
12931 tree decl;
12932 tree auto_result = NULL_TREE;
12934 if (saw_declarator)
12936 /* If we are processing next declarator, comma is expected */
12937 token = cp_lexer_peek_token (parser->lexer);
12938 gcc_assert (token->type == CPP_COMMA);
12939 cp_lexer_consume_token (parser->lexer);
12940 if (maybe_range_for_decl)
12942 *maybe_range_for_decl = error_mark_node;
12943 if (comma_loc == UNKNOWN_LOCATION)
12944 comma_loc = token->location;
12947 else
12948 saw_declarator = true;
12950 /* Parse the init-declarator. */
12951 decl = cp_parser_init_declarator (parser, &decl_specifiers,
12952 /*checks=*/NULL,
12953 function_definition_allowed_p,
12954 /*member_p=*/false,
12955 declares_class_or_enum,
12956 &function_definition_p,
12957 maybe_range_for_decl,
12958 &init_loc,
12959 &auto_result);
12960 /* If an error occurred while parsing tentatively, exit quickly.
12961 (That usually happens when in the body of a function; each
12962 statement is treated as a declaration-statement until proven
12963 otherwise.) */
12964 if (cp_parser_error_occurred (parser))
12965 goto done;
12967 if (auto_specifier_p && cxx_dialect >= cxx14)
12969 /* If the init-declarator-list contains more than one
12970 init-declarator, they shall all form declarations of
12971 variables. */
12972 if (auto_function_declaration == NULL_TREE)
12973 auto_function_declaration
12974 = TREE_CODE (decl) == FUNCTION_DECL ? decl : error_mark_node;
12975 else if (TREE_CODE (decl) == FUNCTION_DECL
12976 || auto_function_declaration != error_mark_node)
12978 error_at (decl_specifiers.locations[ds_type_spec],
12979 "non-variable %qD in declaration with more than one "
12980 "declarator with placeholder type",
12981 TREE_CODE (decl) == FUNCTION_DECL
12982 ? decl : auto_function_declaration);
12983 auto_function_declaration = error_mark_node;
12987 if (auto_result
12988 && (!processing_template_decl || !type_uses_auto (auto_result)))
12990 if (last_type
12991 && last_type != error_mark_node
12992 && !same_type_p (auto_result, last_type))
12994 /* If the list of declarators contains more than one declarator,
12995 the type of each declared variable is determined as described
12996 above. If the type deduced for the template parameter U is not
12997 the same in each deduction, the program is ill-formed. */
12998 error_at (decl_specifiers.locations[ds_type_spec],
12999 "inconsistent deduction for %qT: %qT and then %qT",
13000 decl_specifiers.type, last_type, auto_result);
13001 last_type = error_mark_node;
13003 else
13004 last_type = auto_result;
13007 /* Handle function definitions specially. */
13008 if (function_definition_p)
13010 /* If the next token is a `,', then we are probably
13011 processing something like:
13013 void f() {}, *p;
13015 which is erroneous. */
13016 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13018 cp_token *token = cp_lexer_peek_token (parser->lexer);
13019 error_at (token->location,
13020 "mixing"
13021 " declarations and function-definitions is forbidden");
13023 /* Otherwise, we're done with the list of declarators. */
13024 else
13026 pop_deferring_access_checks ();
13027 return;
13030 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
13031 *maybe_range_for_decl = decl;
13032 /* The next token should be either a `,' or a `;'. */
13033 token = cp_lexer_peek_token (parser->lexer);
13034 /* If it's a `,', there are more declarators to come. */
13035 if (token->type == CPP_COMMA)
13036 /* will be consumed next time around */;
13037 /* If it's a `;', we are done. */
13038 else if (token->type == CPP_SEMICOLON)
13039 break;
13040 else if (maybe_range_for_decl)
13042 if ((declares_class_or_enum & 2) && token->type == CPP_COLON)
13043 permerror (decl_specifiers.locations[ds_type_spec],
13044 "types may not be defined in a for-range-declaration");
13045 break;
13047 /* Anything else is an error. */
13048 else
13050 /* If we have already issued an error message we don't need
13051 to issue another one. */
13052 if ((decl != error_mark_node
13053 && DECL_INITIAL (decl) != error_mark_node)
13054 || cp_parser_uncommitted_to_tentative_parse_p (parser))
13055 cp_parser_error (parser, "expected %<,%> or %<;%>");
13056 /* Skip tokens until we reach the end of the statement. */
13057 cp_parser_skip_to_end_of_statement (parser);
13058 /* If the next token is now a `;', consume it. */
13059 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13060 cp_lexer_consume_token (parser->lexer);
13061 goto done;
13063 /* After the first time around, a function-definition is not
13064 allowed -- even if it was OK at first. For example:
13066 int i, f() {}
13068 is not valid. */
13069 function_definition_allowed_p = false;
13072 /* Issue an error message if no declarators are present, and the
13073 decl-specifier-seq does not itself declare a class or
13074 enumeration: [dcl.dcl]/3. */
13075 if (!saw_declarator)
13077 if (cp_parser_declares_only_class_p (parser))
13079 if (!declares_class_or_enum
13080 && decl_specifiers.type
13081 && OVERLOAD_TYPE_P (decl_specifiers.type))
13082 /* Ensure an error is issued anyway when finish_decltype_type,
13083 called via cp_parser_decl_specifier_seq, returns a class or
13084 an enumeration (c++/51786). */
13085 decl_specifiers.type = NULL_TREE;
13086 shadow_tag (&decl_specifiers);
13088 /* Perform any deferred access checks. */
13089 perform_deferred_access_checks (tf_warning_or_error);
13092 /* Consume the `;'. */
13093 finish:
13094 if (!maybe_range_for_decl)
13095 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13096 else if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13098 if (init_loc != UNKNOWN_LOCATION)
13099 error_at (init_loc, "initializer in range-based %<for%> loop");
13100 if (comma_loc != UNKNOWN_LOCATION)
13101 error_at (comma_loc,
13102 "multiple declarations in range-based %<for%> loop");
13105 done:
13106 pop_deferring_access_checks ();
13109 /* Helper of cp_parser_simple_declaration, parse a decomposition declaration.
13110 decl-specifier-seq ref-qualifier [opt] [ identifier-list ]
13111 initializer ; */
13113 static tree
13114 cp_parser_decomposition_declaration (cp_parser *parser,
13115 cp_decl_specifier_seq *decl_specifiers,
13116 tree *maybe_range_for_decl,
13117 location_t *init_loc)
13119 cp_ref_qualifier ref_qual = cp_parser_ref_qualifier_opt (parser);
13120 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
13121 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
13123 /* Parse the identifier-list. */
13124 auto_vec<cp_expr, 10> v;
13125 if (!cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13126 while (true)
13128 cp_expr e = cp_parser_identifier (parser);
13129 if (e.get_value () == error_mark_node)
13130 break;
13131 v.safe_push (e);
13132 if (!cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13133 break;
13134 cp_lexer_consume_token (parser->lexer);
13137 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
13138 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
13140 end_loc = UNKNOWN_LOCATION;
13141 cp_parser_skip_to_closing_parenthesis_1 (parser, true, CPP_CLOSE_SQUARE,
13142 false);
13143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
13144 cp_lexer_consume_token (parser->lexer);
13145 else
13147 cp_parser_skip_to_end_of_statement (parser);
13148 return error_mark_node;
13152 if (cxx_dialect < cxx1z)
13153 pedwarn (loc, 0, "structured bindings only available with "
13154 "-std=c++1z or -std=gnu++1z");
13156 tree pushed_scope;
13157 cp_declarator *declarator = make_declarator (cdk_decomp);
13158 loc = end_loc == UNKNOWN_LOCATION ? loc : make_location (loc, loc, end_loc);
13159 declarator->id_loc = loc;
13160 if (ref_qual != REF_QUAL_NONE)
13161 declarator = make_reference_declarator (TYPE_UNQUALIFIED, declarator,
13162 ref_qual == REF_QUAL_RVALUE,
13163 NULL_TREE);
13164 tree decl = start_decl (declarator, decl_specifiers, SD_INITIALIZED,
13165 NULL_TREE, decl_specifiers->attributes,
13166 &pushed_scope);
13167 tree orig_decl = decl;
13169 unsigned int i;
13170 cp_expr e;
13171 cp_decl_specifier_seq decl_specs;
13172 clear_decl_specs (&decl_specs);
13173 decl_specs.type = make_auto ();
13174 tree prev = decl;
13175 FOR_EACH_VEC_ELT (v, i, e)
13177 if (i == 0)
13178 declarator = make_id_declarator (NULL_TREE, e.get_value (), sfk_none);
13179 else
13180 declarator->u.id.unqualified_name = e.get_value ();
13181 declarator->id_loc = e.get_location ();
13182 tree elt_pushed_scope;
13183 tree decl2 = start_decl (declarator, &decl_specs, SD_INITIALIZED,
13184 NULL_TREE, NULL_TREE, &elt_pushed_scope);
13185 if (decl2 == error_mark_node)
13186 decl = error_mark_node;
13187 else if (decl != error_mark_node && DECL_CHAIN (decl2) != prev)
13189 /* Ensure we've diagnosed redeclaration if we aren't creating
13190 a new VAR_DECL. */
13191 gcc_assert (errorcount);
13192 decl = error_mark_node;
13194 else
13195 prev = decl2;
13196 if (elt_pushed_scope)
13197 pop_scope (elt_pushed_scope);
13200 if (v.is_empty ())
13202 error_at (loc, "empty structured binding declaration");
13203 decl = error_mark_node;
13206 if (maybe_range_for_decl == NULL
13207 || cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
13209 bool non_constant_p = false, is_direct_init = false;
13210 *init_loc = cp_lexer_peek_token (parser->lexer)->location;
13211 tree initializer = cp_parser_initializer (parser, &is_direct_init,
13212 &non_constant_p);
13213 if (initializer == NULL_TREE
13214 || (TREE_CODE (initializer) == TREE_LIST
13215 && TREE_CHAIN (initializer))
13216 || (TREE_CODE (initializer) == CONSTRUCTOR
13217 && CONSTRUCTOR_NELTS (initializer) != 1))
13219 error_at (loc, "invalid initializer for structured binding "
13220 "declaration");
13221 initializer = error_mark_node;
13224 if (decl != error_mark_node)
13226 cp_finish_decl (decl, initializer, non_constant_p, NULL_TREE,
13227 is_direct_init ? LOOKUP_NORMAL : LOOKUP_IMPLICIT);
13228 cp_finish_decomp (decl, prev, v.length ());
13231 else if (decl != error_mark_node)
13233 *maybe_range_for_decl = prev;
13234 /* Ensure DECL_VALUE_EXPR is created for all the decls but
13235 the underlying DECL. */
13236 cp_finish_decomp (decl, prev, v.length ());
13239 if (pushed_scope)
13240 pop_scope (pushed_scope);
13242 if (decl == error_mark_node && DECL_P (orig_decl))
13244 if (DECL_NAMESPACE_SCOPE_P (orig_decl))
13245 SET_DECL_ASSEMBLER_NAME (orig_decl, get_identifier ("<decomp>"));
13248 return decl;
13251 /* Parse a decl-specifier-seq.
13253 decl-specifier-seq:
13254 decl-specifier-seq [opt] decl-specifier
13255 decl-specifier attribute-specifier-seq [opt] (C++11)
13257 decl-specifier:
13258 storage-class-specifier
13259 type-specifier
13260 function-specifier
13261 friend
13262 typedef
13264 GNU Extension:
13266 decl-specifier:
13267 attributes
13269 Concepts Extension:
13271 decl-specifier:
13272 concept
13274 Set *DECL_SPECS to a representation of the decl-specifier-seq.
13276 The parser flags FLAGS is used to control type-specifier parsing.
13278 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
13279 flags:
13281 1: one of the decl-specifiers is an elaborated-type-specifier
13282 (i.e., a type declaration)
13283 2: one of the decl-specifiers is an enum-specifier or a
13284 class-specifier (i.e., a type definition)
13288 static void
13289 cp_parser_decl_specifier_seq (cp_parser* parser,
13290 cp_parser_flags flags,
13291 cp_decl_specifier_seq *decl_specs,
13292 int* declares_class_or_enum)
13294 bool constructor_possible_p = !parser->in_declarator_p;
13295 bool found_decl_spec = false;
13296 cp_token *start_token = NULL;
13297 cp_decl_spec ds;
13299 /* Clear DECL_SPECS. */
13300 clear_decl_specs (decl_specs);
13302 /* Assume no class or enumeration type is declared. */
13303 *declares_class_or_enum = 0;
13305 /* Keep reading specifiers until there are no more to read. */
13306 while (true)
13308 bool constructor_p;
13309 cp_token *token;
13310 ds = ds_last;
13312 /* Peek at the next token. */
13313 token = cp_lexer_peek_token (parser->lexer);
13315 /* Save the first token of the decl spec list for error
13316 reporting. */
13317 if (!start_token)
13318 start_token = token;
13319 /* Handle attributes. */
13320 if (cp_next_tokens_can_be_attribute_p (parser))
13322 /* Parse the attributes. */
13323 tree attrs = cp_parser_attributes_opt (parser);
13325 /* In a sequence of declaration specifiers, c++11 attributes
13326 appertain to the type that precede them. In that case
13327 [dcl.spec]/1 says:
13329 The attribute-specifier-seq affects the type only for
13330 the declaration it appears in, not other declarations
13331 involving the same type.
13333 But for now let's force the user to position the
13334 attribute either at the beginning of the declaration or
13335 after the declarator-id, which would clearly mean that it
13336 applies to the declarator. */
13337 if (cxx11_attribute_p (attrs))
13339 if (!found_decl_spec)
13340 /* The c++11 attribute is at the beginning of the
13341 declaration. It appertains to the entity being
13342 declared. */;
13343 else
13345 if (decl_specs->type && CLASS_TYPE_P (decl_specs->type))
13347 /* This is an attribute following a
13348 class-specifier. */
13349 if (decl_specs->type_definition_p)
13350 warn_misplaced_attr_for_class_type (token->location,
13351 decl_specs->type);
13352 attrs = NULL_TREE;
13354 else
13356 decl_specs->std_attributes
13357 = chainon (decl_specs->std_attributes,
13358 attrs);
13359 if (decl_specs->locations[ds_std_attribute] == 0)
13360 decl_specs->locations[ds_std_attribute] = token->location;
13362 continue;
13366 decl_specs->attributes
13367 = chainon (decl_specs->attributes,
13368 attrs);
13369 if (decl_specs->locations[ds_attribute] == 0)
13370 decl_specs->locations[ds_attribute] = token->location;
13371 continue;
13373 /* Assume we will find a decl-specifier keyword. */
13374 found_decl_spec = true;
13375 /* If the next token is an appropriate keyword, we can simply
13376 add it to the list. */
13377 switch (token->keyword)
13379 /* decl-specifier:
13380 friend
13381 constexpr */
13382 case RID_FRIEND:
13383 if (!at_class_scope_p ())
13385 gcc_rich_location richloc (token->location);
13386 richloc.add_fixit_remove ();
13387 error_at_rich_loc (&richloc, "%<friend%> used outside of class");
13388 cp_lexer_purge_token (parser->lexer);
13390 else
13392 ds = ds_friend;
13393 /* Consume the token. */
13394 cp_lexer_consume_token (parser->lexer);
13396 break;
13398 case RID_CONSTEXPR:
13399 ds = ds_constexpr;
13400 cp_lexer_consume_token (parser->lexer);
13401 break;
13403 case RID_CONCEPT:
13404 ds = ds_concept;
13405 cp_lexer_consume_token (parser->lexer);
13406 break;
13408 /* function-specifier:
13409 inline
13410 virtual
13411 explicit */
13412 case RID_INLINE:
13413 case RID_VIRTUAL:
13414 case RID_EXPLICIT:
13415 cp_parser_function_specifier_opt (parser, decl_specs);
13416 break;
13418 /* decl-specifier:
13419 typedef */
13420 case RID_TYPEDEF:
13421 ds = ds_typedef;
13422 /* Consume the token. */
13423 cp_lexer_consume_token (parser->lexer);
13424 /* A constructor declarator cannot appear in a typedef. */
13425 constructor_possible_p = false;
13426 /* The "typedef" keyword can only occur in a declaration; we
13427 may as well commit at this point. */
13428 cp_parser_commit_to_tentative_parse (parser);
13430 if (decl_specs->storage_class != sc_none)
13431 decl_specs->conflicting_specifiers_p = true;
13432 break;
13434 /* storage-class-specifier:
13435 auto
13436 register
13437 static
13438 extern
13439 mutable
13441 GNU Extension:
13442 thread */
13443 case RID_AUTO:
13444 if (cxx_dialect == cxx98)
13446 /* Consume the token. */
13447 cp_lexer_consume_token (parser->lexer);
13449 /* Complain about `auto' as a storage specifier, if
13450 we're complaining about C++0x compatibility. */
13451 gcc_rich_location richloc (token->location);
13452 richloc.add_fixit_remove ();
13453 warning_at_rich_loc (&richloc, OPT_Wc__11_compat,
13454 "%<auto%> changes meaning in C++11; "
13455 "please remove it");
13457 /* Set the storage class anyway. */
13458 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
13459 token);
13461 else
13462 /* C++0x auto type-specifier. */
13463 found_decl_spec = false;
13464 break;
13466 case RID_REGISTER:
13467 case RID_STATIC:
13468 case RID_EXTERN:
13469 case RID_MUTABLE:
13470 /* Consume the token. */
13471 cp_lexer_consume_token (parser->lexer);
13472 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
13473 token);
13474 break;
13475 case RID_THREAD:
13476 /* Consume the token. */
13477 ds = ds_thread;
13478 cp_lexer_consume_token (parser->lexer);
13479 break;
13481 default:
13482 /* We did not yet find a decl-specifier yet. */
13483 found_decl_spec = false;
13484 break;
13487 if (found_decl_spec
13488 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
13489 && token->keyword != RID_CONSTEXPR)
13490 error ("decl-specifier invalid in condition");
13492 if (found_decl_spec
13493 && (flags & CP_PARSER_FLAGS_ONLY_MUTABLE_OR_CONSTEXPR)
13494 && token->keyword != RID_MUTABLE
13495 && token->keyword != RID_CONSTEXPR)
13496 error_at (token->location, "%qD invalid in lambda",
13497 ridpointers[token->keyword]);
13499 if (ds != ds_last)
13500 set_and_check_decl_spec_loc (decl_specs, ds, token);
13502 /* Constructors are a special case. The `S' in `S()' is not a
13503 decl-specifier; it is the beginning of the declarator. */
13504 constructor_p
13505 = (!found_decl_spec
13506 && constructor_possible_p
13507 && (cp_parser_constructor_declarator_p
13508 (parser, decl_spec_seq_has_spec_p (decl_specs, ds_friend))));
13510 /* If we don't have a DECL_SPEC yet, then we must be looking at
13511 a type-specifier. */
13512 if (!found_decl_spec && !constructor_p)
13514 int decl_spec_declares_class_or_enum;
13515 bool is_cv_qualifier;
13516 tree type_spec;
13518 type_spec
13519 = cp_parser_type_specifier (parser, flags,
13520 decl_specs,
13521 /*is_declaration=*/true,
13522 &decl_spec_declares_class_or_enum,
13523 &is_cv_qualifier);
13524 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
13526 /* If this type-specifier referenced a user-defined type
13527 (a typedef, class-name, etc.), then we can't allow any
13528 more such type-specifiers henceforth.
13530 [dcl.spec]
13532 The longest sequence of decl-specifiers that could
13533 possibly be a type name is taken as the
13534 decl-specifier-seq of a declaration. The sequence shall
13535 be self-consistent as described below.
13537 [dcl.type]
13539 As a general rule, at most one type-specifier is allowed
13540 in the complete decl-specifier-seq of a declaration. The
13541 only exceptions are the following:
13543 -- const or volatile can be combined with any other
13544 type-specifier.
13546 -- signed or unsigned can be combined with char, long,
13547 short, or int.
13549 -- ..
13551 Example:
13553 typedef char* Pc;
13554 void g (const int Pc);
13556 Here, Pc is *not* part of the decl-specifier seq; it's
13557 the declarator. Therefore, once we see a type-specifier
13558 (other than a cv-qualifier), we forbid any additional
13559 user-defined types. We *do* still allow things like `int
13560 int' to be considered a decl-specifier-seq, and issue the
13561 error message later. */
13562 if (type_spec && !is_cv_qualifier)
13563 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13564 /* A constructor declarator cannot follow a type-specifier. */
13565 if (type_spec)
13567 constructor_possible_p = false;
13568 found_decl_spec = true;
13569 if (!is_cv_qualifier)
13570 decl_specs->any_type_specifiers_p = true;
13574 /* If we still do not have a DECL_SPEC, then there are no more
13575 decl-specifiers. */
13576 if (!found_decl_spec)
13577 break;
13579 decl_specs->any_specifiers_p = true;
13580 /* After we see one decl-specifier, further decl-specifiers are
13581 always optional. */
13582 flags |= CP_PARSER_FLAGS_OPTIONAL;
13585 /* Don't allow a friend specifier with a class definition. */
13586 if (decl_spec_seq_has_spec_p (decl_specs, ds_friend)
13587 && (*declares_class_or_enum & 2))
13588 error_at (decl_specs->locations[ds_friend],
13589 "class definition may not be declared a friend");
13592 /* Parse an (optional) storage-class-specifier.
13594 storage-class-specifier:
13595 auto
13596 register
13597 static
13598 extern
13599 mutable
13601 GNU Extension:
13603 storage-class-specifier:
13604 thread
13606 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
13608 static tree
13609 cp_parser_storage_class_specifier_opt (cp_parser* parser)
13611 switch (cp_lexer_peek_token (parser->lexer)->keyword)
13613 case RID_AUTO:
13614 if (cxx_dialect != cxx98)
13615 return NULL_TREE;
13616 /* Fall through for C++98. */
13617 gcc_fallthrough ();
13619 case RID_REGISTER:
13620 case RID_STATIC:
13621 case RID_EXTERN:
13622 case RID_MUTABLE:
13623 case RID_THREAD:
13624 /* Consume the token. */
13625 return cp_lexer_consume_token (parser->lexer)->u.value;
13627 default:
13628 return NULL_TREE;
13632 /* Parse an (optional) function-specifier.
13634 function-specifier:
13635 inline
13636 virtual
13637 explicit
13639 Returns an IDENTIFIER_NODE corresponding to the keyword used.
13640 Updates DECL_SPECS, if it is non-NULL. */
13642 static tree
13643 cp_parser_function_specifier_opt (cp_parser* parser,
13644 cp_decl_specifier_seq *decl_specs)
13646 cp_token *token = cp_lexer_peek_token (parser->lexer);
13647 switch (token->keyword)
13649 case RID_INLINE:
13650 set_and_check_decl_spec_loc (decl_specs, ds_inline, token);
13651 break;
13653 case RID_VIRTUAL:
13654 /* 14.5.2.3 [temp.mem]
13656 A member function template shall not be virtual. */
13657 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13658 && current_class_type)
13659 error_at (token->location, "templates may not be %<virtual%>");
13660 else
13661 set_and_check_decl_spec_loc (decl_specs, ds_virtual, token);
13662 break;
13664 case RID_EXPLICIT:
13665 set_and_check_decl_spec_loc (decl_specs, ds_explicit, token);
13666 break;
13668 default:
13669 return NULL_TREE;
13672 /* Consume the token. */
13673 return cp_lexer_consume_token (parser->lexer)->u.value;
13676 /* Parse a linkage-specification.
13678 linkage-specification:
13679 extern string-literal { declaration-seq [opt] }
13680 extern string-literal declaration */
13682 static void
13683 cp_parser_linkage_specification (cp_parser* parser)
13685 tree linkage;
13687 /* Look for the `extern' keyword. */
13688 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
13690 /* Look for the string-literal. */
13691 linkage = cp_parser_string_literal (parser, false, false);
13693 /* Transform the literal into an identifier. If the literal is a
13694 wide-character string, or contains embedded NULs, then we can't
13695 handle it as the user wants. */
13696 if (strlen (TREE_STRING_POINTER (linkage))
13697 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
13699 cp_parser_error (parser, "invalid linkage-specification");
13700 /* Assume C++ linkage. */
13701 linkage = lang_name_cplusplus;
13703 else
13704 linkage = get_identifier (TREE_STRING_POINTER (linkage));
13706 /* We're now using the new linkage. */
13707 push_lang_context (linkage);
13709 /* If the next token is a `{', then we're using the first
13710 production. */
13711 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13713 cp_ensure_no_omp_declare_simd (parser);
13714 cp_ensure_no_oacc_routine (parser);
13716 /* Consume the `{' token. */
13717 cp_lexer_consume_token (parser->lexer);
13718 /* Parse the declarations. */
13719 cp_parser_declaration_seq_opt (parser);
13720 /* Look for the closing `}'. */
13721 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13723 /* Otherwise, there's just one declaration. */
13724 else
13726 bool saved_in_unbraced_linkage_specification_p;
13728 saved_in_unbraced_linkage_specification_p
13729 = parser->in_unbraced_linkage_specification_p;
13730 parser->in_unbraced_linkage_specification_p = true;
13731 cp_parser_declaration (parser);
13732 parser->in_unbraced_linkage_specification_p
13733 = saved_in_unbraced_linkage_specification_p;
13736 /* We're done with the linkage-specification. */
13737 pop_lang_context ();
13740 /* Parse a static_assert-declaration.
13742 static_assert-declaration:
13743 static_assert ( constant-expression , string-literal ) ;
13744 static_assert ( constant-expression ) ; (C++1Z)
13746 If MEMBER_P, this static_assert is a class member. */
13748 static void
13749 cp_parser_static_assert(cp_parser *parser, bool member_p)
13751 tree condition;
13752 tree message;
13753 cp_token *token;
13754 location_t saved_loc;
13755 bool dummy;
13757 /* Peek at the `static_assert' token so we can keep track of exactly
13758 where the static assertion started. */
13759 token = cp_lexer_peek_token (parser->lexer);
13760 saved_loc = token->location;
13762 /* Look for the `static_assert' keyword. */
13763 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
13764 RT_STATIC_ASSERT))
13765 return;
13767 /* We know we are in a static assertion; commit to any tentative
13768 parse. */
13769 if (cp_parser_parsing_tentatively (parser))
13770 cp_parser_commit_to_tentative_parse (parser);
13772 /* Parse the `(' starting the static assertion condition. */
13773 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
13775 /* Parse the constant-expression. Allow a non-constant expression
13776 here in order to give better diagnostics in finish_static_assert. */
13777 condition =
13778 cp_parser_constant_expression (parser,
13779 /*allow_non_constant_p=*/true,
13780 /*non_constant_p=*/&dummy);
13782 if (cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13784 if (cxx_dialect < cxx1z)
13785 pedwarn (input_location, OPT_Wpedantic,
13786 "static_assert without a message "
13787 "only available with -std=c++1z or -std=gnu++1z");
13788 /* Eat the ')' */
13789 cp_lexer_consume_token (parser->lexer);
13790 message = build_string (1, "");
13791 TREE_TYPE (message) = char_array_type_node;
13792 fix_string_type (message);
13794 else
13796 /* Parse the separating `,'. */
13797 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
13799 /* Parse the string-literal message. */
13800 message = cp_parser_string_literal (parser,
13801 /*translate=*/false,
13802 /*wide_ok=*/true);
13804 /* A `)' completes the static assertion. */
13805 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13806 cp_parser_skip_to_closing_parenthesis (parser,
13807 /*recovering=*/true,
13808 /*or_comma=*/false,
13809 /*consume_paren=*/true);
13812 /* A semicolon terminates the declaration. */
13813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13815 /* Complete the static assertion, which may mean either processing
13816 the static assert now or saving it for template instantiation. */
13817 finish_static_assert (condition, message, saved_loc, member_p);
13820 /* Parse the expression in decltype ( expression ). */
13822 static tree
13823 cp_parser_decltype_expr (cp_parser *parser,
13824 bool &id_expression_or_member_access_p)
13826 cp_token *id_expr_start_token;
13827 tree expr;
13829 /* Since we're going to preserve any side-effects from this parse, set up a
13830 firewall to protect our callers from cp_parser_commit_to_tentative_parse
13831 in the expression. */
13832 tentative_firewall firewall (parser);
13834 /* First, try parsing an id-expression. */
13835 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
13836 cp_parser_parse_tentatively (parser);
13837 expr = cp_parser_id_expression (parser,
13838 /*template_keyword_p=*/false,
13839 /*check_dependency_p=*/true,
13840 /*template_p=*/NULL,
13841 /*declarator_p=*/false,
13842 /*optional_p=*/false);
13844 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
13846 bool non_integral_constant_expression_p = false;
13847 tree id_expression = expr;
13848 cp_id_kind idk;
13849 const char *error_msg;
13851 if (identifier_p (expr))
13852 /* Lookup the name we got back from the id-expression. */
13853 expr = cp_parser_lookup_name_simple (parser, expr,
13854 id_expr_start_token->location);
13856 if (expr
13857 && expr != error_mark_node
13858 && TREE_CODE (expr) != TYPE_DECL
13859 && (TREE_CODE (expr) != BIT_NOT_EXPR
13860 || !TYPE_P (TREE_OPERAND (expr, 0)))
13861 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13863 /* Complete lookup of the id-expression. */
13864 expr = (finish_id_expression
13865 (id_expression, expr, parser->scope, &idk,
13866 /*integral_constant_expression_p=*/false,
13867 /*allow_non_integral_constant_expression_p=*/true,
13868 &non_integral_constant_expression_p,
13869 /*template_p=*/false,
13870 /*done=*/true,
13871 /*address_p=*/false,
13872 /*template_arg_p=*/false,
13873 &error_msg,
13874 id_expr_start_token->location));
13876 if (expr == error_mark_node)
13877 /* We found an id-expression, but it was something that we
13878 should not have found. This is an error, not something
13879 we can recover from, so note that we found an
13880 id-expression and we'll recover as gracefully as
13881 possible. */
13882 id_expression_or_member_access_p = true;
13885 if (expr
13886 && expr != error_mark_node
13887 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13888 /* We have an id-expression. */
13889 id_expression_or_member_access_p = true;
13892 if (!id_expression_or_member_access_p)
13894 /* Abort the id-expression parse. */
13895 cp_parser_abort_tentative_parse (parser);
13897 /* Parsing tentatively, again. */
13898 cp_parser_parse_tentatively (parser);
13900 /* Parse a class member access. */
13901 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
13902 /*cast_p=*/false, /*decltype*/true,
13903 /*member_access_only_p=*/true, NULL);
13905 if (expr
13906 && expr != error_mark_node
13907 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
13908 /* We have an id-expression. */
13909 id_expression_or_member_access_p = true;
13912 if (id_expression_or_member_access_p)
13913 /* We have parsed the complete id-expression or member access. */
13914 cp_parser_parse_definitely (parser);
13915 else
13917 /* Abort our attempt to parse an id-expression or member access
13918 expression. */
13919 cp_parser_abort_tentative_parse (parser);
13921 /* Parse a full expression. */
13922 expr = cp_parser_expression (parser, /*pidk=*/NULL, /*cast_p=*/false,
13923 /*decltype_p=*/true);
13926 return expr;
13929 /* Parse a `decltype' type. Returns the type.
13931 simple-type-specifier:
13932 decltype ( expression )
13933 C++14 proposal:
13934 decltype ( auto ) */
13936 static tree
13937 cp_parser_decltype (cp_parser *parser)
13939 tree expr;
13940 bool id_expression_or_member_access_p = false;
13941 const char *saved_message;
13942 bool saved_integral_constant_expression_p;
13943 bool saved_non_integral_constant_expression_p;
13944 bool saved_greater_than_is_operator_p;
13945 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
13947 if (start_token->type == CPP_DECLTYPE)
13949 /* Already parsed. */
13950 cp_lexer_consume_token (parser->lexer);
13951 return saved_checks_value (start_token->u.tree_check_value);
13954 /* Look for the `decltype' token. */
13955 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
13956 return error_mark_node;
13958 /* Parse the opening `('. */
13959 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
13960 return error_mark_node;
13962 /* decltype (auto) */
13963 if (cxx_dialect >= cxx14
13964 && cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
13966 cp_lexer_consume_token (parser->lexer);
13967 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
13968 return error_mark_node;
13969 expr = make_decltype_auto ();
13970 AUTO_IS_DECLTYPE (expr) = true;
13971 goto rewrite;
13974 /* Types cannot be defined in a `decltype' expression. Save away the
13975 old message. */
13976 saved_message = parser->type_definition_forbidden_message;
13978 /* And create the new one. */
13979 parser->type_definition_forbidden_message
13980 = G_("types may not be defined in %<decltype%> expressions");
13982 /* The restrictions on constant-expressions do not apply inside
13983 decltype expressions. */
13984 saved_integral_constant_expression_p
13985 = parser->integral_constant_expression_p;
13986 saved_non_integral_constant_expression_p
13987 = parser->non_integral_constant_expression_p;
13988 parser->integral_constant_expression_p = false;
13990 /* Within a parenthesized expression, a `>' token is always
13991 the greater-than operator. */
13992 saved_greater_than_is_operator_p
13993 = parser->greater_than_is_operator_p;
13994 parser->greater_than_is_operator_p = true;
13996 /* Do not actually evaluate the expression. */
13997 ++cp_unevaluated_operand;
13999 /* Do not warn about problems with the expression. */
14000 ++c_inhibit_evaluation_warnings;
14002 expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
14004 /* Go back to evaluating expressions. */
14005 --cp_unevaluated_operand;
14006 --c_inhibit_evaluation_warnings;
14008 /* The `>' token might be the end of a template-id or
14009 template-parameter-list now. */
14010 parser->greater_than_is_operator_p
14011 = saved_greater_than_is_operator_p;
14013 /* Restore the old message and the integral constant expression
14014 flags. */
14015 parser->type_definition_forbidden_message = saved_message;
14016 parser->integral_constant_expression_p
14017 = saved_integral_constant_expression_p;
14018 parser->non_integral_constant_expression_p
14019 = saved_non_integral_constant_expression_p;
14021 /* Parse to the closing `)'. */
14022 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14024 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14025 /*consume_paren=*/true);
14026 return error_mark_node;
14029 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
14030 tf_warning_or_error);
14032 rewrite:
14033 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
14034 it again. */
14035 start_token->type = CPP_DECLTYPE;
14036 start_token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
14037 start_token->u.tree_check_value->value = expr;
14038 start_token->u.tree_check_value->checks = get_deferred_access_checks ();
14039 start_token->keyword = RID_MAX;
14040 cp_lexer_purge_tokens_after (parser->lexer, start_token);
14042 return expr;
14045 /* Special member functions [gram.special] */
14047 /* Parse a conversion-function-id.
14049 conversion-function-id:
14050 operator conversion-type-id
14052 Returns an IDENTIFIER_NODE representing the operator. */
14054 static tree
14055 cp_parser_conversion_function_id (cp_parser* parser)
14057 tree type;
14058 tree saved_scope;
14059 tree saved_qualifying_scope;
14060 tree saved_object_scope;
14061 tree pushed_scope = NULL_TREE;
14063 /* Look for the `operator' token. */
14064 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14065 return error_mark_node;
14066 /* When we parse the conversion-type-id, the current scope will be
14067 reset. However, we need that information in able to look up the
14068 conversion function later, so we save it here. */
14069 saved_scope = parser->scope;
14070 saved_qualifying_scope = parser->qualifying_scope;
14071 saved_object_scope = parser->object_scope;
14072 /* We must enter the scope of the class so that the names of
14073 entities declared within the class are available in the
14074 conversion-type-id. For example, consider:
14076 struct S {
14077 typedef int I;
14078 operator I();
14081 S::operator I() { ... }
14083 In order to see that `I' is a type-name in the definition, we
14084 must be in the scope of `S'. */
14085 if (saved_scope)
14086 pushed_scope = push_scope (saved_scope);
14087 /* Parse the conversion-type-id. */
14088 type = cp_parser_conversion_type_id (parser);
14089 /* Leave the scope of the class, if any. */
14090 if (pushed_scope)
14091 pop_scope (pushed_scope);
14092 /* Restore the saved scope. */
14093 parser->scope = saved_scope;
14094 parser->qualifying_scope = saved_qualifying_scope;
14095 parser->object_scope = saved_object_scope;
14096 /* If the TYPE is invalid, indicate failure. */
14097 if (type == error_mark_node)
14098 return error_mark_node;
14099 return make_conv_op_name (type);
14102 /* Parse a conversion-type-id:
14104 conversion-type-id:
14105 type-specifier-seq conversion-declarator [opt]
14107 Returns the TYPE specified. */
14109 static tree
14110 cp_parser_conversion_type_id (cp_parser* parser)
14112 tree attributes;
14113 cp_decl_specifier_seq type_specifiers;
14114 cp_declarator *declarator;
14115 tree type_specified;
14116 const char *saved_message;
14118 /* Parse the attributes. */
14119 attributes = cp_parser_attributes_opt (parser);
14121 saved_message = parser->type_definition_forbidden_message;
14122 parser->type_definition_forbidden_message
14123 = G_("types may not be defined in a conversion-type-id");
14125 /* Parse the type-specifiers. */
14126 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14127 /*is_trailing_return=*/false,
14128 &type_specifiers);
14130 parser->type_definition_forbidden_message = saved_message;
14132 /* If that didn't work, stop. */
14133 if (type_specifiers.type == error_mark_node)
14134 return error_mark_node;
14135 /* Parse the conversion-declarator. */
14136 declarator = cp_parser_conversion_declarator_opt (parser);
14138 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
14139 /*initialized=*/0, &attributes);
14140 if (attributes)
14141 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
14143 /* Don't give this error when parsing tentatively. This happens to
14144 work because we always parse this definitively once. */
14145 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
14146 && type_uses_auto (type_specified))
14148 if (cxx_dialect < cxx14)
14150 error ("invalid use of %<auto%> in conversion operator");
14151 return error_mark_node;
14153 else if (template_parm_scope_p ())
14154 warning (0, "use of %<auto%> in member template "
14155 "conversion operator can never be deduced");
14158 return type_specified;
14161 /* Parse an (optional) conversion-declarator.
14163 conversion-declarator:
14164 ptr-operator conversion-declarator [opt]
14168 static cp_declarator *
14169 cp_parser_conversion_declarator_opt (cp_parser* parser)
14171 enum tree_code code;
14172 tree class_type, std_attributes = NULL_TREE;
14173 cp_cv_quals cv_quals;
14175 /* We don't know if there's a ptr-operator next, or not. */
14176 cp_parser_parse_tentatively (parser);
14177 /* Try the ptr-operator. */
14178 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals,
14179 &std_attributes);
14180 /* If it worked, look for more conversion-declarators. */
14181 if (cp_parser_parse_definitely (parser))
14183 cp_declarator *declarator;
14185 /* Parse another optional declarator. */
14186 declarator = cp_parser_conversion_declarator_opt (parser);
14188 declarator = cp_parser_make_indirect_declarator
14189 (code, class_type, cv_quals, declarator, std_attributes);
14191 return declarator;
14194 return NULL;
14197 /* Parse an (optional) ctor-initializer.
14199 ctor-initializer:
14200 : mem-initializer-list
14202 Returns TRUE iff the ctor-initializer was actually present. */
14204 static bool
14205 cp_parser_ctor_initializer_opt (cp_parser* parser)
14207 /* If the next token is not a `:', then there is no
14208 ctor-initializer. */
14209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
14211 /* Do default initialization of any bases and members. */
14212 if (DECL_CONSTRUCTOR_P (current_function_decl))
14213 finish_mem_initializers (NULL_TREE);
14215 return false;
14218 /* Consume the `:' token. */
14219 cp_lexer_consume_token (parser->lexer);
14220 /* And the mem-initializer-list. */
14221 cp_parser_mem_initializer_list (parser);
14223 return true;
14226 /* Parse a mem-initializer-list.
14228 mem-initializer-list:
14229 mem-initializer ... [opt]
14230 mem-initializer ... [opt] , mem-initializer-list */
14232 static void
14233 cp_parser_mem_initializer_list (cp_parser* parser)
14235 tree mem_initializer_list = NULL_TREE;
14236 tree target_ctor = error_mark_node;
14237 cp_token *token = cp_lexer_peek_token (parser->lexer);
14239 /* Let the semantic analysis code know that we are starting the
14240 mem-initializer-list. */
14241 if (!DECL_CONSTRUCTOR_P (current_function_decl))
14242 error_at (token->location,
14243 "only constructors take member initializers");
14245 /* Loop through the list. */
14246 while (true)
14248 tree mem_initializer;
14250 token = cp_lexer_peek_token (parser->lexer);
14251 /* Parse the mem-initializer. */
14252 mem_initializer = cp_parser_mem_initializer (parser);
14253 /* If the next token is a `...', we're expanding member initializers. */
14254 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14256 /* Consume the `...'. */
14257 cp_lexer_consume_token (parser->lexer);
14259 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
14260 can be expanded but members cannot. */
14261 if (mem_initializer != error_mark_node
14262 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
14264 error_at (token->location,
14265 "cannot expand initializer for member %qD",
14266 TREE_PURPOSE (mem_initializer));
14267 mem_initializer = error_mark_node;
14270 /* Construct the pack expansion type. */
14271 if (mem_initializer != error_mark_node)
14272 mem_initializer = make_pack_expansion (mem_initializer);
14274 if (target_ctor != error_mark_node
14275 && mem_initializer != error_mark_node)
14277 error ("mem-initializer for %qD follows constructor delegation",
14278 TREE_PURPOSE (mem_initializer));
14279 mem_initializer = error_mark_node;
14281 /* Look for a target constructor. */
14282 if (mem_initializer != error_mark_node
14283 && CLASS_TYPE_P (TREE_PURPOSE (mem_initializer))
14284 && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
14286 maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
14287 if (mem_initializer_list)
14289 error ("constructor delegation follows mem-initializer for %qD",
14290 TREE_PURPOSE (mem_initializer_list));
14291 mem_initializer = error_mark_node;
14293 target_ctor = mem_initializer;
14295 /* Add it to the list, unless it was erroneous. */
14296 if (mem_initializer != error_mark_node)
14298 TREE_CHAIN (mem_initializer) = mem_initializer_list;
14299 mem_initializer_list = mem_initializer;
14301 /* If the next token is not a `,', we're done. */
14302 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14303 break;
14304 /* Consume the `,' token. */
14305 cp_lexer_consume_token (parser->lexer);
14308 /* Perform semantic analysis. */
14309 if (DECL_CONSTRUCTOR_P (current_function_decl))
14310 finish_mem_initializers (mem_initializer_list);
14313 /* Parse a mem-initializer.
14315 mem-initializer:
14316 mem-initializer-id ( expression-list [opt] )
14317 mem-initializer-id braced-init-list
14319 GNU extension:
14321 mem-initializer:
14322 ( expression-list [opt] )
14324 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
14325 class) or FIELD_DECL (for a non-static data member) to initialize;
14326 the TREE_VALUE is the expression-list. An empty initialization
14327 list is represented by void_list_node. */
14329 static tree
14330 cp_parser_mem_initializer (cp_parser* parser)
14332 tree mem_initializer_id;
14333 tree expression_list;
14334 tree member;
14335 cp_token *token = cp_lexer_peek_token (parser->lexer);
14337 /* Find out what is being initialized. */
14338 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14340 permerror (token->location,
14341 "anachronistic old-style base class initializer");
14342 mem_initializer_id = NULL_TREE;
14344 else
14346 mem_initializer_id = cp_parser_mem_initializer_id (parser);
14347 if (mem_initializer_id == error_mark_node)
14348 return mem_initializer_id;
14350 member = expand_member_init (mem_initializer_id);
14351 if (member && !DECL_P (member))
14352 in_base_initializer = 1;
14354 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14356 bool expr_non_constant_p;
14357 cp_lexer_set_source_position (parser->lexer);
14358 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
14359 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
14360 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
14361 expression_list = build_tree_list (NULL_TREE, expression_list);
14363 else
14365 vec<tree, va_gc> *vec;
14366 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
14367 /*cast_p=*/false,
14368 /*allow_expansion_p=*/true,
14369 /*non_constant_p=*/NULL);
14370 if (vec == NULL)
14371 return error_mark_node;
14372 expression_list = build_tree_list_vec (vec);
14373 release_tree_vector (vec);
14376 if (expression_list == error_mark_node)
14377 return error_mark_node;
14378 if (!expression_list)
14379 expression_list = void_type_node;
14381 in_base_initializer = 0;
14383 return member ? build_tree_list (member, expression_list) : error_mark_node;
14386 /* Parse a mem-initializer-id.
14388 mem-initializer-id:
14389 :: [opt] nested-name-specifier [opt] class-name
14390 decltype-specifier (C++11)
14391 identifier
14393 Returns a TYPE indicating the class to be initialized for the first
14394 production (and the second in C++11). Returns an IDENTIFIER_NODE
14395 indicating the data member to be initialized for the last production. */
14397 static tree
14398 cp_parser_mem_initializer_id (cp_parser* parser)
14400 bool global_scope_p;
14401 bool nested_name_specifier_p;
14402 bool template_p = false;
14403 tree id;
14405 cp_token *token = cp_lexer_peek_token (parser->lexer);
14407 /* `typename' is not allowed in this context ([temp.res]). */
14408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14410 error_at (token->location,
14411 "keyword %<typename%> not allowed in this context (a qualified "
14412 "member initializer is implicitly a type)");
14413 cp_lexer_consume_token (parser->lexer);
14415 /* Look for the optional `::' operator. */
14416 global_scope_p
14417 = (cp_parser_global_scope_opt (parser,
14418 /*current_scope_valid_p=*/false)
14419 != NULL_TREE);
14420 /* Look for the optional nested-name-specifier. The simplest way to
14421 implement:
14423 [temp.res]
14425 The keyword `typename' is not permitted in a base-specifier or
14426 mem-initializer; in these contexts a qualified name that
14427 depends on a template-parameter is implicitly assumed to be a
14428 type name.
14430 is to assume that we have seen the `typename' keyword at this
14431 point. */
14432 nested_name_specifier_p
14433 = (cp_parser_nested_name_specifier_opt (parser,
14434 /*typename_keyword_p=*/true,
14435 /*check_dependency_p=*/true,
14436 /*type_p=*/true,
14437 /*is_declaration=*/true)
14438 != NULL_TREE);
14439 if (nested_name_specifier_p)
14440 template_p = cp_parser_optional_template_keyword (parser);
14441 /* If there is a `::' operator or a nested-name-specifier, then we
14442 are definitely looking for a class-name. */
14443 if (global_scope_p || nested_name_specifier_p)
14444 return cp_parser_class_name (parser,
14445 /*typename_keyword_p=*/true,
14446 /*template_keyword_p=*/template_p,
14447 typename_type,
14448 /*check_dependency_p=*/true,
14449 /*class_head_p=*/false,
14450 /*is_declaration=*/true);
14451 /* Otherwise, we could also be looking for an ordinary identifier. */
14452 cp_parser_parse_tentatively (parser);
14453 if (cp_lexer_next_token_is_decltype (parser->lexer))
14454 /* Try a decltype-specifier. */
14455 id = cp_parser_decltype (parser);
14456 else
14457 /* Otherwise, try a class-name. */
14458 id = cp_parser_class_name (parser,
14459 /*typename_keyword_p=*/true,
14460 /*template_keyword_p=*/false,
14461 none_type,
14462 /*check_dependency_p=*/true,
14463 /*class_head_p=*/false,
14464 /*is_declaration=*/true);
14465 /* If we found one, we're done. */
14466 if (cp_parser_parse_definitely (parser))
14467 return id;
14468 /* Otherwise, look for an ordinary identifier. */
14469 return cp_parser_identifier (parser);
14472 /* Overloading [gram.over] */
14474 /* Parse an operator-function-id.
14476 operator-function-id:
14477 operator operator
14479 Returns an IDENTIFIER_NODE for the operator which is a
14480 human-readable spelling of the identifier, e.g., `operator +'. */
14482 static cp_expr
14483 cp_parser_operator_function_id (cp_parser* parser)
14485 /* Look for the `operator' keyword. */
14486 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
14487 return error_mark_node;
14488 /* And then the name of the operator itself. */
14489 return cp_parser_operator (parser);
14492 /* Return an identifier node for a user-defined literal operator.
14493 The suffix identifier is chained to the operator name identifier. */
14495 tree
14496 cp_literal_operator_id (const char* name)
14498 tree identifier;
14499 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
14500 + strlen (name) + 10);
14501 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
14502 identifier = get_identifier (buffer);
14504 return identifier;
14507 /* Parse an operator.
14509 operator:
14510 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
14511 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
14512 || ++ -- , ->* -> () []
14514 GNU Extensions:
14516 operator:
14517 <? >? <?= >?=
14519 Returns an IDENTIFIER_NODE for the operator which is a
14520 human-readable spelling of the identifier, e.g., `operator +'. */
14522 static cp_expr
14523 cp_parser_operator (cp_parser* parser)
14525 tree id = NULL_TREE;
14526 cp_token *token;
14527 bool utf8 = false;
14529 /* Peek at the next token. */
14530 token = cp_lexer_peek_token (parser->lexer);
14532 location_t start_loc = token->location;
14534 /* Figure out which operator we have. */
14535 switch (token->type)
14537 case CPP_KEYWORD:
14539 enum tree_code op;
14541 /* The keyword should be either `new' or `delete'. */
14542 if (token->keyword == RID_NEW)
14543 op = NEW_EXPR;
14544 else if (token->keyword == RID_DELETE)
14545 op = DELETE_EXPR;
14546 else
14547 break;
14549 /* Consume the `new' or `delete' token. */
14550 location_t end_loc = cp_lexer_consume_token (parser->lexer)->location;
14552 /* Peek at the next token. */
14553 token = cp_lexer_peek_token (parser->lexer);
14554 /* If it's a `[' token then this is the array variant of the
14555 operator. */
14556 if (token->type == CPP_OPEN_SQUARE)
14558 /* Consume the `[' token. */
14559 cp_lexer_consume_token (parser->lexer);
14560 /* Look for the `]' token. */
14561 if (cp_token *close_token
14562 = cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
14563 end_loc = close_token->location;
14564 id = cp_operator_id (op == NEW_EXPR
14565 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
14567 /* Otherwise, we have the non-array variant. */
14568 else
14569 id = cp_operator_id (op);
14571 location_t loc = make_location (start_loc, start_loc, end_loc);
14573 return cp_expr (id, loc);
14576 case CPP_PLUS:
14577 id = cp_operator_id (PLUS_EXPR);
14578 break;
14580 case CPP_MINUS:
14581 id = cp_operator_id (MINUS_EXPR);
14582 break;
14584 case CPP_MULT:
14585 id = cp_operator_id (MULT_EXPR);
14586 break;
14588 case CPP_DIV:
14589 id = cp_operator_id (TRUNC_DIV_EXPR);
14590 break;
14592 case CPP_MOD:
14593 id = cp_operator_id (TRUNC_MOD_EXPR);
14594 break;
14596 case CPP_XOR:
14597 id = cp_operator_id (BIT_XOR_EXPR);
14598 break;
14600 case CPP_AND:
14601 id = cp_operator_id (BIT_AND_EXPR);
14602 break;
14604 case CPP_OR:
14605 id = cp_operator_id (BIT_IOR_EXPR);
14606 break;
14608 case CPP_COMPL:
14609 id = cp_operator_id (BIT_NOT_EXPR);
14610 break;
14612 case CPP_NOT:
14613 id = cp_operator_id (TRUTH_NOT_EXPR);
14614 break;
14616 case CPP_EQ:
14617 id = cp_assignment_operator_id (NOP_EXPR);
14618 break;
14620 case CPP_LESS:
14621 id = cp_operator_id (LT_EXPR);
14622 break;
14624 case CPP_GREATER:
14625 id = cp_operator_id (GT_EXPR);
14626 break;
14628 case CPP_PLUS_EQ:
14629 id = cp_assignment_operator_id (PLUS_EXPR);
14630 break;
14632 case CPP_MINUS_EQ:
14633 id = cp_assignment_operator_id (MINUS_EXPR);
14634 break;
14636 case CPP_MULT_EQ:
14637 id = cp_assignment_operator_id (MULT_EXPR);
14638 break;
14640 case CPP_DIV_EQ:
14641 id = cp_assignment_operator_id (TRUNC_DIV_EXPR);
14642 break;
14644 case CPP_MOD_EQ:
14645 id = cp_assignment_operator_id (TRUNC_MOD_EXPR);
14646 break;
14648 case CPP_XOR_EQ:
14649 id = cp_assignment_operator_id (BIT_XOR_EXPR);
14650 break;
14652 case CPP_AND_EQ:
14653 id = cp_assignment_operator_id (BIT_AND_EXPR);
14654 break;
14656 case CPP_OR_EQ:
14657 id = cp_assignment_operator_id (BIT_IOR_EXPR);
14658 break;
14660 case CPP_LSHIFT:
14661 id = cp_operator_id (LSHIFT_EXPR);
14662 break;
14664 case CPP_RSHIFT:
14665 id = cp_operator_id (RSHIFT_EXPR);
14666 break;
14668 case CPP_LSHIFT_EQ:
14669 id = cp_assignment_operator_id (LSHIFT_EXPR);
14670 break;
14672 case CPP_RSHIFT_EQ:
14673 id = cp_assignment_operator_id (RSHIFT_EXPR);
14674 break;
14676 case CPP_EQ_EQ:
14677 id = cp_operator_id (EQ_EXPR);
14678 break;
14680 case CPP_NOT_EQ:
14681 id = cp_operator_id (NE_EXPR);
14682 break;
14684 case CPP_LESS_EQ:
14685 id = cp_operator_id (LE_EXPR);
14686 break;
14688 case CPP_GREATER_EQ:
14689 id = cp_operator_id (GE_EXPR);
14690 break;
14692 case CPP_AND_AND:
14693 id = cp_operator_id (TRUTH_ANDIF_EXPR);
14694 break;
14696 case CPP_OR_OR:
14697 id = cp_operator_id (TRUTH_ORIF_EXPR);
14698 break;
14700 case CPP_PLUS_PLUS:
14701 id = cp_operator_id (POSTINCREMENT_EXPR);
14702 break;
14704 case CPP_MINUS_MINUS:
14705 id = cp_operator_id (PREDECREMENT_EXPR);
14706 break;
14708 case CPP_COMMA:
14709 id = cp_operator_id (COMPOUND_EXPR);
14710 break;
14712 case CPP_DEREF_STAR:
14713 id = cp_operator_id (MEMBER_REF);
14714 break;
14716 case CPP_DEREF:
14717 id = cp_operator_id (COMPONENT_REF);
14718 break;
14720 case CPP_OPEN_PAREN:
14721 /* Consume the `('. */
14722 cp_lexer_consume_token (parser->lexer);
14723 /* Look for the matching `)'. */
14724 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14725 return cp_operator_id (CALL_EXPR);
14727 case CPP_OPEN_SQUARE:
14728 /* Consume the `['. */
14729 cp_lexer_consume_token (parser->lexer);
14730 /* Look for the matching `]'. */
14731 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
14732 return cp_operator_id (ARRAY_REF);
14734 case CPP_UTF8STRING:
14735 case CPP_UTF8STRING_USERDEF:
14736 utf8 = true;
14737 /* FALLTHRU */
14738 case CPP_STRING:
14739 case CPP_WSTRING:
14740 case CPP_STRING16:
14741 case CPP_STRING32:
14742 case CPP_STRING_USERDEF:
14743 case CPP_WSTRING_USERDEF:
14744 case CPP_STRING16_USERDEF:
14745 case CPP_STRING32_USERDEF:
14747 tree str, string_tree;
14748 int sz, len;
14750 if (cxx_dialect == cxx98)
14751 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
14753 /* Consume the string. */
14754 str = cp_parser_string_literal (parser, /*translate=*/true,
14755 /*wide_ok=*/true, /*lookup_udlit=*/false);
14756 if (str == error_mark_node)
14757 return error_mark_node;
14758 else if (TREE_CODE (str) == USERDEF_LITERAL)
14760 string_tree = USERDEF_LITERAL_VALUE (str);
14761 id = USERDEF_LITERAL_SUFFIX_ID (str);
14763 else
14765 string_tree = str;
14766 /* Look for the suffix identifier. */
14767 token = cp_lexer_peek_token (parser->lexer);
14768 if (token->type == CPP_NAME)
14769 id = cp_parser_identifier (parser);
14770 else if (token->type == CPP_KEYWORD)
14772 error ("unexpected keyword;"
14773 " remove space between quotes and suffix identifier");
14774 return error_mark_node;
14776 else
14778 error ("expected suffix identifier");
14779 return error_mark_node;
14782 sz = TREE_INT_CST_LOW (TYPE_SIZE_UNIT
14783 (TREE_TYPE (TREE_TYPE (string_tree))));
14784 len = TREE_STRING_LENGTH (string_tree) / sz - 1;
14785 if (len != 0)
14787 error ("expected empty string after %<operator%> keyword");
14788 return error_mark_node;
14790 if (utf8 || TYPE_MAIN_VARIANT (TREE_TYPE (TREE_TYPE (string_tree)))
14791 != char_type_node)
14793 error ("invalid encoding prefix in literal operator");
14794 return error_mark_node;
14796 if (id != error_mark_node)
14798 const char *name = IDENTIFIER_POINTER (id);
14799 id = cp_literal_operator_id (name);
14801 return id;
14804 default:
14805 /* Anything else is an error. */
14806 break;
14809 /* If we have selected an identifier, we need to consume the
14810 operator token. */
14811 if (id)
14812 cp_lexer_consume_token (parser->lexer);
14813 /* Otherwise, no valid operator name was present. */
14814 else
14816 cp_parser_error (parser, "expected operator");
14817 id = error_mark_node;
14820 return cp_expr (id, start_loc);
14823 /* Parse a template-declaration.
14825 template-declaration:
14826 export [opt] template < template-parameter-list > declaration
14828 If MEMBER_P is TRUE, this template-declaration occurs within a
14829 class-specifier.
14831 The grammar rule given by the standard isn't correct. What
14832 is really meant is:
14834 template-declaration:
14835 export [opt] template-parameter-list-seq
14836 decl-specifier-seq [opt] init-declarator [opt] ;
14837 export [opt] template-parameter-list-seq
14838 function-definition
14840 template-parameter-list-seq:
14841 template-parameter-list-seq [opt]
14842 template < template-parameter-list >
14844 Concept Extensions:
14846 template-parameter-list-seq:
14847 template < template-parameter-list > requires-clause [opt]
14849 requires-clause:
14850 requires logical-or-expression */
14852 static void
14853 cp_parser_template_declaration (cp_parser* parser, bool member_p)
14855 /* Check for `export'. */
14856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
14858 /* Consume the `export' token. */
14859 cp_lexer_consume_token (parser->lexer);
14860 /* Warn that we do not support `export'. */
14861 warning (0, "keyword %<export%> not implemented, and will be ignored");
14864 cp_parser_template_declaration_after_export (parser, member_p);
14867 /* Parse a template-parameter-list.
14869 template-parameter-list:
14870 template-parameter
14871 template-parameter-list , template-parameter
14873 Returns a TREE_LIST. Each node represents a template parameter.
14874 The nodes are connected via their TREE_CHAINs. */
14876 static tree
14877 cp_parser_template_parameter_list (cp_parser* parser)
14879 tree parameter_list = NULL_TREE;
14881 begin_template_parm_list ();
14883 /* The loop below parses the template parms. We first need to know
14884 the total number of template parms to be able to compute proper
14885 canonical types of each dependent type. So after the loop, when
14886 we know the total number of template parms,
14887 end_template_parm_list computes the proper canonical types and
14888 fixes up the dependent types accordingly. */
14889 while (true)
14891 tree parameter;
14892 bool is_non_type;
14893 bool is_parameter_pack;
14894 location_t parm_loc;
14896 /* Parse the template-parameter. */
14897 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
14898 parameter = cp_parser_template_parameter (parser,
14899 &is_non_type,
14900 &is_parameter_pack);
14901 /* Add it to the list. */
14902 if (parameter != error_mark_node)
14903 parameter_list = process_template_parm (parameter_list,
14904 parm_loc,
14905 parameter,
14906 is_non_type,
14907 is_parameter_pack);
14908 else
14910 tree err_parm = build_tree_list (parameter, parameter);
14911 parameter_list = chainon (parameter_list, err_parm);
14914 /* If the next token is not a `,', we're done. */
14915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14916 break;
14917 /* Otherwise, consume the `,' token. */
14918 cp_lexer_consume_token (parser->lexer);
14921 return end_template_parm_list (parameter_list);
14924 /* Parse a introduction-list.
14926 introduction-list:
14927 introduced-parameter
14928 introduction-list , introduced-parameter
14930 introduced-parameter:
14931 ...[opt] identifier
14933 Returns a TREE_VEC of WILDCARD_DECLs. If the parameter is a pack
14934 then the introduced parm will have WILDCARD_PACK_P set. In addition, the
14935 WILDCARD_DECL will also have DECL_NAME set and token location in
14936 DECL_SOURCE_LOCATION. */
14938 static tree
14939 cp_parser_introduction_list (cp_parser *parser)
14941 vec<tree, va_gc> *introduction_vec = make_tree_vector ();
14943 while (true)
14945 bool is_pack = cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS);
14946 if (is_pack)
14947 cp_lexer_consume_token (parser->lexer);
14949 /* Build placeholder. */
14950 tree parm = build_nt (WILDCARD_DECL);
14951 DECL_SOURCE_LOCATION (parm)
14952 = cp_lexer_peek_token (parser->lexer)->location;
14953 DECL_NAME (parm) = cp_parser_identifier (parser);
14954 WILDCARD_PACK_P (parm) = is_pack;
14955 vec_safe_push (introduction_vec, parm);
14957 /* If the next token is not a `,', we're done. */
14958 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14959 break;
14960 /* Otherwise, consume the `,' token. */
14961 cp_lexer_consume_token (parser->lexer);
14964 /* Convert the vec into a TREE_VEC. */
14965 tree introduction_list = make_tree_vec (introduction_vec->length ());
14966 unsigned int n;
14967 tree parm;
14968 FOR_EACH_VEC_ELT (*introduction_vec, n, parm)
14969 TREE_VEC_ELT (introduction_list, n) = parm;
14971 release_tree_vector (introduction_vec);
14972 return introduction_list;
14975 /* Given a declarator, get the declarator-id part, or NULL_TREE if this
14976 is an abstract declarator. */
14978 static inline cp_declarator*
14979 get_id_declarator (cp_declarator *declarator)
14981 cp_declarator *d = declarator;
14982 while (d && d->kind != cdk_id)
14983 d = d->declarator;
14984 return d;
14987 /* Get the unqualified-id from the DECLARATOR or NULL_TREE if this
14988 is an abstract declarator. */
14990 static inline tree
14991 get_unqualified_id (cp_declarator *declarator)
14993 declarator = get_id_declarator (declarator);
14994 if (declarator)
14995 return declarator->u.id.unqualified_name;
14996 else
14997 return NULL_TREE;
15000 /* Returns true if DECL represents a constrained-parameter. */
15002 static inline bool
15003 is_constrained_parameter (tree decl)
15005 return (decl
15006 && TREE_CODE (decl) == TYPE_DECL
15007 && CONSTRAINED_PARM_CONCEPT (decl)
15008 && DECL_P (CONSTRAINED_PARM_CONCEPT (decl)));
15011 /* Returns true if PARM declares a constrained-parameter. */
15013 static inline bool
15014 is_constrained_parameter (cp_parameter_declarator *parm)
15016 return is_constrained_parameter (parm->decl_specifiers.type);
15019 /* Check that the type parameter is only a declarator-id, and that its
15020 type is not cv-qualified. */
15022 bool
15023 cp_parser_check_constrained_type_parm (cp_parser *parser,
15024 cp_parameter_declarator *parm)
15026 if (!parm->declarator)
15027 return true;
15029 if (parm->declarator->kind != cdk_id)
15031 cp_parser_error (parser, "invalid constrained type parameter");
15032 return false;
15035 /* Don't allow cv-qualified type parameters. */
15036 if (decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_const)
15037 || decl_spec_seq_has_spec_p (&parm->decl_specifiers, ds_volatile))
15039 cp_parser_error (parser, "cv-qualified type parameter");
15040 return false;
15043 return true;
15046 /* Finish parsing/processing a template type parameter and checking
15047 various restrictions. */
15049 static inline tree
15050 cp_parser_constrained_type_template_parm (cp_parser *parser,
15051 tree id,
15052 cp_parameter_declarator* parmdecl)
15054 if (cp_parser_check_constrained_type_parm (parser, parmdecl))
15055 return finish_template_type_parm (class_type_node, id);
15056 else
15057 return error_mark_node;
15060 static tree
15061 finish_constrained_template_template_parm (tree proto, tree id)
15063 /* FIXME: This should probably be copied, and we may need to adjust
15064 the template parameter depths. */
15065 tree saved_parms = current_template_parms;
15066 begin_template_parm_list ();
15067 current_template_parms = DECL_TEMPLATE_PARMS (proto);
15068 end_template_parm_list ();
15070 tree parm = finish_template_template_parm (class_type_node, id);
15071 current_template_parms = saved_parms;
15073 return parm;
15076 /* Finish parsing/processing a template template parameter by borrowing
15077 the template parameter list from the prototype parameter. */
15079 static tree
15080 cp_parser_constrained_template_template_parm (cp_parser *parser,
15081 tree proto,
15082 tree id,
15083 cp_parameter_declarator *parmdecl)
15085 if (!cp_parser_check_constrained_type_parm (parser, parmdecl))
15086 return error_mark_node;
15087 return finish_constrained_template_template_parm (proto, id);
15090 /* Create a new non-type template parameter from the given PARM
15091 declarator. */
15093 static tree
15094 constrained_non_type_template_parm (bool *is_non_type,
15095 cp_parameter_declarator *parm)
15097 *is_non_type = true;
15098 cp_declarator *decl = parm->declarator;
15099 cp_decl_specifier_seq *specs = &parm->decl_specifiers;
15100 specs->type = TREE_TYPE (DECL_INITIAL (specs->type));
15101 return grokdeclarator (decl, specs, TPARM, 0, NULL);
15104 /* Build a constrained template parameter based on the PARMDECL
15105 declarator. The type of PARMDECL is the constrained type, which
15106 refers to the prototype template parameter that ultimately
15107 specifies the type of the declared parameter. */
15109 static tree
15110 finish_constrained_parameter (cp_parser *parser,
15111 cp_parameter_declarator *parmdecl,
15112 bool *is_non_type,
15113 bool *is_parameter_pack)
15115 tree decl = parmdecl->decl_specifiers.type;
15116 tree id = get_unqualified_id (parmdecl->declarator);
15117 tree def = parmdecl->default_argument;
15118 tree proto = DECL_INITIAL (decl);
15120 /* A template parameter constrained by a variadic concept shall also
15121 be declared as a template parameter pack. */
15122 bool is_variadic = template_parameter_pack_p (proto);
15123 if (is_variadic && !*is_parameter_pack)
15124 cp_parser_error (parser, "variadic constraint introduced without %<...%>");
15126 /* Build the parameter. Return an error if the declarator was invalid. */
15127 tree parm;
15128 if (TREE_CODE (proto) == TYPE_DECL)
15129 parm = cp_parser_constrained_type_template_parm (parser, id, parmdecl);
15130 else if (TREE_CODE (proto) == TEMPLATE_DECL)
15131 parm = cp_parser_constrained_template_template_parm (parser, proto, id,
15132 parmdecl);
15133 else
15134 parm = constrained_non_type_template_parm (is_non_type, parmdecl);
15135 if (parm == error_mark_node)
15136 return error_mark_node;
15138 /* Finish the parameter decl and create a node attaching the
15139 default argument and constraint. */
15140 parm = build_tree_list (def, parm);
15141 TEMPLATE_PARM_CONSTRAINTS (parm) = decl;
15143 return parm;
15146 /* Returns true if the parsed type actually represents the declaration
15147 of a type template-parameter. */
15149 static inline bool
15150 declares_constrained_type_template_parameter (tree type)
15152 return (is_constrained_parameter (type)
15153 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TYPE_PARM);
15157 /* Returns true if the parsed type actually represents the declaration of
15158 a template template-parameter. */
15160 static bool
15161 declares_constrained_template_template_parameter (tree type)
15163 return (is_constrained_parameter (type)
15164 && TREE_CODE (TREE_TYPE (type)) == TEMPLATE_TEMPLATE_PARM);
15167 /* Parse a default argument for a type template-parameter.
15168 Note that diagnostics are handled in cp_parser_template_parameter. */
15170 static tree
15171 cp_parser_default_type_template_argument (cp_parser *parser)
15173 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15175 /* Consume the `=' token. */
15176 cp_lexer_consume_token (parser->lexer);
15178 cp_token *token = cp_lexer_peek_token (parser->lexer);
15180 /* Parse the default-argument. */
15181 push_deferring_access_checks (dk_no_deferred);
15182 tree default_argument = cp_parser_type_id (parser);
15183 pop_deferring_access_checks ();
15185 if (flag_concepts && type_uses_auto (default_argument))
15187 error_at (token->location,
15188 "invalid use of %<auto%> in default template argument");
15189 return error_mark_node;
15192 return default_argument;
15195 /* Parse a default argument for a template template-parameter. */
15197 static tree
15198 cp_parser_default_template_template_argument (cp_parser *parser)
15200 gcc_assert (cp_lexer_next_token_is (parser->lexer, CPP_EQ));
15202 bool is_template;
15204 /* Consume the `='. */
15205 cp_lexer_consume_token (parser->lexer);
15206 /* Parse the id-expression. */
15207 push_deferring_access_checks (dk_no_deferred);
15208 /* save token before parsing the id-expression, for error
15209 reporting */
15210 const cp_token* token = cp_lexer_peek_token (parser->lexer);
15211 tree default_argument
15212 = cp_parser_id_expression (parser,
15213 /*template_keyword_p=*/false,
15214 /*check_dependency_p=*/true,
15215 /*template_p=*/&is_template,
15216 /*declarator_p=*/false,
15217 /*optional_p=*/false);
15218 if (TREE_CODE (default_argument) == TYPE_DECL)
15219 /* If the id-expression was a template-id that refers to
15220 a template-class, we already have the declaration here,
15221 so no further lookup is needed. */
15223 else
15224 /* Look up the name. */
15225 default_argument
15226 = cp_parser_lookup_name (parser, default_argument,
15227 none_type,
15228 /*is_template=*/is_template,
15229 /*is_namespace=*/false,
15230 /*check_dependency=*/true,
15231 /*ambiguous_decls=*/NULL,
15232 token->location);
15233 /* See if the default argument is valid. */
15234 default_argument = check_template_template_default_arg (default_argument);
15235 pop_deferring_access_checks ();
15236 return default_argument;
15239 /* Parse a template-parameter.
15241 template-parameter:
15242 type-parameter
15243 parameter-declaration
15245 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
15246 the parameter. The TREE_PURPOSE is the default value, if any.
15247 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
15248 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
15249 set to true iff this parameter is a parameter pack. */
15251 static tree
15252 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
15253 bool *is_parameter_pack)
15255 cp_token *token;
15256 cp_parameter_declarator *parameter_declarator;
15257 tree parm;
15259 /* Assume it is a type parameter or a template parameter. */
15260 *is_non_type = false;
15261 /* Assume it not a parameter pack. */
15262 *is_parameter_pack = false;
15263 /* Peek at the next token. */
15264 token = cp_lexer_peek_token (parser->lexer);
15265 /* If it is `template', we have a type-parameter. */
15266 if (token->keyword == RID_TEMPLATE)
15267 return cp_parser_type_parameter (parser, is_parameter_pack);
15268 /* If it is `class' or `typename' we do not know yet whether it is a
15269 type parameter or a non-type parameter. Consider:
15271 template <typename T, typename T::X X> ...
15275 template <class C, class D*> ...
15277 Here, the first parameter is a type parameter, and the second is
15278 a non-type parameter. We can tell by looking at the token after
15279 the identifier -- if it is a `,', `=', or `>' then we have a type
15280 parameter. */
15281 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
15283 /* Peek at the token after `class' or `typename'. */
15284 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15285 /* If it's an ellipsis, we have a template type parameter
15286 pack. */
15287 if (token->type == CPP_ELLIPSIS)
15288 return cp_parser_type_parameter (parser, is_parameter_pack);
15289 /* If it's an identifier, skip it. */
15290 if (token->type == CPP_NAME)
15291 token = cp_lexer_peek_nth_token (parser->lexer, 3);
15292 /* Now, see if the token looks like the end of a template
15293 parameter. */
15294 if (token->type == CPP_COMMA
15295 || token->type == CPP_EQ
15296 || token->type == CPP_GREATER)
15297 return cp_parser_type_parameter (parser, is_parameter_pack);
15300 /* Otherwise, it is a non-type parameter or a constrained parameter.
15302 [temp.param]
15304 When parsing a default template-argument for a non-type
15305 template-parameter, the first non-nested `>' is taken as the end
15306 of the template parameter-list rather than a greater-than
15307 operator. */
15308 parameter_declarator
15309 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
15310 /*parenthesized_p=*/NULL);
15312 if (!parameter_declarator)
15313 return error_mark_node;
15315 /* If the parameter declaration is marked as a parameter pack, set
15316 *IS_PARAMETER_PACK to notify the caller. */
15317 if (parameter_declarator->template_parameter_pack_p)
15318 *is_parameter_pack = true;
15320 if (parameter_declarator->default_argument)
15322 /* Can happen in some cases of erroneous input (c++/34892). */
15323 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15324 /* Consume the `...' for better error recovery. */
15325 cp_lexer_consume_token (parser->lexer);
15328 // The parameter may have been constrained.
15329 if (is_constrained_parameter (parameter_declarator))
15330 return finish_constrained_parameter (parser,
15331 parameter_declarator,
15332 is_non_type,
15333 is_parameter_pack);
15335 // Now we're sure that the parameter is a non-type parameter.
15336 *is_non_type = true;
15338 parm = grokdeclarator (parameter_declarator->declarator,
15339 &parameter_declarator->decl_specifiers,
15340 TPARM, /*initialized=*/0,
15341 /*attrlist=*/NULL);
15342 if (parm == error_mark_node)
15343 return error_mark_node;
15345 return build_tree_list (parameter_declarator->default_argument, parm);
15348 /* Parse a type-parameter.
15350 type-parameter:
15351 class identifier [opt]
15352 class identifier [opt] = type-id
15353 typename identifier [opt]
15354 typename identifier [opt] = type-id
15355 template < template-parameter-list > class identifier [opt]
15356 template < template-parameter-list > class identifier [opt]
15357 = id-expression
15359 GNU Extension (variadic templates):
15361 type-parameter:
15362 class ... identifier [opt]
15363 typename ... identifier [opt]
15365 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
15366 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
15367 the declaration of the parameter.
15369 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
15371 static tree
15372 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
15374 cp_token *token;
15375 tree parameter;
15377 /* Look for a keyword to tell us what kind of parameter this is. */
15378 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
15379 if (!token)
15380 return error_mark_node;
15382 switch (token->keyword)
15384 case RID_CLASS:
15385 case RID_TYPENAME:
15387 tree identifier;
15388 tree default_argument;
15390 /* If the next token is an ellipsis, we have a template
15391 argument pack. */
15392 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15394 /* Consume the `...' token. */
15395 cp_lexer_consume_token (parser->lexer);
15396 maybe_warn_variadic_templates ();
15398 *is_parameter_pack = true;
15401 /* If the next token is an identifier, then it names the
15402 parameter. */
15403 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15404 identifier = cp_parser_identifier (parser);
15405 else
15406 identifier = NULL_TREE;
15408 /* Create the parameter. */
15409 parameter = finish_template_type_parm (class_type_node, identifier);
15411 /* If the next token is an `=', we have a default argument. */
15412 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15414 default_argument
15415 = cp_parser_default_type_template_argument (parser);
15417 /* Template parameter packs cannot have default
15418 arguments. */
15419 if (*is_parameter_pack)
15421 if (identifier)
15422 error_at (token->location,
15423 "template parameter pack %qD cannot have a "
15424 "default argument", identifier);
15425 else
15426 error_at (token->location,
15427 "template parameter packs cannot have "
15428 "default arguments");
15429 default_argument = NULL_TREE;
15431 else if (check_for_bare_parameter_packs (default_argument))
15432 default_argument = error_mark_node;
15434 else
15435 default_argument = NULL_TREE;
15437 /* Create the combined representation of the parameter and the
15438 default argument. */
15439 parameter = build_tree_list (default_argument, parameter);
15441 break;
15443 case RID_TEMPLATE:
15445 tree identifier;
15446 tree default_argument;
15448 /* Look for the `<'. */
15449 cp_parser_require (parser, CPP_LESS, RT_LESS);
15450 /* Parse the template-parameter-list. */
15451 cp_parser_template_parameter_list (parser);
15452 /* Look for the `>'. */
15453 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
15455 // If template requirements are present, parse them.
15456 if (flag_concepts)
15458 tree reqs = get_shorthand_constraints (current_template_parms);
15459 if (tree r = cp_parser_requires_clause_opt (parser))
15460 reqs = conjoin_constraints (reqs, normalize_expression (r));
15461 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
15464 /* Look for the `class' or 'typename' keywords. */
15465 cp_parser_type_parameter_key (parser);
15466 /* If the next token is an ellipsis, we have a template
15467 argument pack. */
15468 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15470 /* Consume the `...' token. */
15471 cp_lexer_consume_token (parser->lexer);
15472 maybe_warn_variadic_templates ();
15474 *is_parameter_pack = true;
15476 /* If the next token is an `=', then there is a
15477 default-argument. If the next token is a `>', we are at
15478 the end of the parameter-list. If the next token is a `,',
15479 then we are at the end of this parameter. */
15480 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
15481 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
15482 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15484 identifier = cp_parser_identifier (parser);
15485 /* Treat invalid names as if the parameter were nameless. */
15486 if (identifier == error_mark_node)
15487 identifier = NULL_TREE;
15489 else
15490 identifier = NULL_TREE;
15492 /* Create the template parameter. */
15493 parameter = finish_template_template_parm (class_type_node,
15494 identifier);
15496 /* If the next token is an `=', then there is a
15497 default-argument. */
15498 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15500 default_argument
15501 = cp_parser_default_template_template_argument (parser);
15503 /* Template parameter packs cannot have default
15504 arguments. */
15505 if (*is_parameter_pack)
15507 if (identifier)
15508 error_at (token->location,
15509 "template parameter pack %qD cannot "
15510 "have a default argument",
15511 identifier);
15512 else
15513 error_at (token->location, "template parameter packs cannot "
15514 "have default arguments");
15515 default_argument = NULL_TREE;
15518 else
15519 default_argument = NULL_TREE;
15521 /* Create the combined representation of the parameter and the
15522 default argument. */
15523 parameter = build_tree_list (default_argument, parameter);
15525 break;
15527 default:
15528 gcc_unreachable ();
15529 break;
15532 return parameter;
15535 /* Parse a template-id.
15537 template-id:
15538 template-name < template-argument-list [opt] >
15540 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
15541 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
15542 returned. Otherwise, if the template-name names a function, or set
15543 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
15544 names a class, returns a TYPE_DECL for the specialization.
15546 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
15547 uninstantiated templates. */
15549 static tree
15550 cp_parser_template_id (cp_parser *parser,
15551 bool template_keyword_p,
15552 bool check_dependency_p,
15553 enum tag_types tag_type,
15554 bool is_declaration)
15556 tree templ;
15557 tree arguments;
15558 tree template_id;
15559 cp_token_position start_of_id = 0;
15560 cp_token *next_token = NULL, *next_token_2 = NULL;
15561 bool is_identifier;
15563 /* If the next token corresponds to a template-id, there is no need
15564 to reparse it. */
15565 cp_token *token = cp_lexer_peek_token (parser->lexer);
15566 if (token->type == CPP_TEMPLATE_ID)
15568 cp_lexer_consume_token (parser->lexer);
15569 return saved_checks_value (token->u.tree_check_value);
15572 /* Avoid performing name lookup if there is no possibility of
15573 finding a template-id. */
15574 if ((token->type != CPP_NAME && token->keyword != RID_OPERATOR)
15575 || (token->type == CPP_NAME
15576 && !cp_parser_nth_token_starts_template_argument_list_p
15577 (parser, 2)))
15579 cp_parser_error (parser, "expected template-id");
15580 return error_mark_node;
15583 /* Remember where the template-id starts. */
15584 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
15585 start_of_id = cp_lexer_token_position (parser->lexer, false);
15587 push_deferring_access_checks (dk_deferred);
15589 /* Parse the template-name. */
15590 is_identifier = false;
15591 templ = cp_parser_template_name (parser, template_keyword_p,
15592 check_dependency_p,
15593 is_declaration,
15594 tag_type,
15595 &is_identifier);
15596 if (templ == error_mark_node || is_identifier)
15598 pop_deferring_access_checks ();
15599 return templ;
15602 /* Since we're going to preserve any side-effects from this parse, set up a
15603 firewall to protect our callers from cp_parser_commit_to_tentative_parse
15604 in the template arguments. */
15605 tentative_firewall firewall (parser);
15607 /* If we find the sequence `[:' after a template-name, it's probably
15608 a digraph-typo for `< ::'. Substitute the tokens and check if we can
15609 parse correctly the argument list. */
15610 if (((next_token = cp_lexer_peek_token (parser->lexer))->type
15611 == CPP_OPEN_SQUARE)
15612 && next_token->flags & DIGRAPH
15613 && ((next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2))->type
15614 == CPP_COLON)
15615 && !(next_token_2->flags & PREV_WHITE))
15617 cp_parser_parse_tentatively (parser);
15618 /* Change `:' into `::'. */
15619 next_token_2->type = CPP_SCOPE;
15620 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
15621 CPP_LESS. */
15622 cp_lexer_consume_token (parser->lexer);
15624 /* Parse the arguments. */
15625 arguments = cp_parser_enclosed_template_argument_list (parser);
15626 if (!cp_parser_parse_definitely (parser))
15628 /* If we couldn't parse an argument list, then we revert our changes
15629 and return simply an error. Maybe this is not a template-id
15630 after all. */
15631 next_token_2->type = CPP_COLON;
15632 cp_parser_error (parser, "expected %<<%>");
15633 pop_deferring_access_checks ();
15634 return error_mark_node;
15636 /* Otherwise, emit an error about the invalid digraph, but continue
15637 parsing because we got our argument list. */
15638 if (permerror (next_token->location,
15639 "%<<::%> cannot begin a template-argument list"))
15641 static bool hint = false;
15642 inform (next_token->location,
15643 "%<<:%> is an alternate spelling for %<[%>."
15644 " Insert whitespace between %<<%> and %<::%>");
15645 if (!hint && !flag_permissive)
15647 inform (next_token->location, "(if you use %<-fpermissive%> "
15648 "or %<-std=c++11%>, or %<-std=gnu++11%> G++ will "
15649 "accept your code)");
15650 hint = true;
15654 else
15656 /* Look for the `<' that starts the template-argument-list. */
15657 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
15659 pop_deferring_access_checks ();
15660 return error_mark_node;
15662 /* Parse the arguments. */
15663 arguments = cp_parser_enclosed_template_argument_list (parser);
15666 /* Set the location to be of the form:
15667 template-name < template-argument-list [opt] >
15668 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15669 with caret == start at the start of the template-name,
15670 ranging until the closing '>'. */
15671 location_t finish_loc
15672 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
15673 location_t combined_loc
15674 = make_location (token->location, token->location, finish_loc);
15676 /* Build a representation of the specialization. */
15677 if (identifier_p (templ))
15678 template_id = build_min_nt_loc (combined_loc,
15679 TEMPLATE_ID_EXPR,
15680 templ, arguments);
15681 else if (DECL_TYPE_TEMPLATE_P (templ)
15682 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
15684 bool entering_scope;
15685 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
15686 template (rather than some instantiation thereof) only if
15687 is not nested within some other construct. For example, in
15688 "template <typename T> void f(T) { A<T>::", A<T> is just an
15689 instantiation of A. */
15690 entering_scope = (template_parm_scope_p ()
15691 && cp_lexer_next_token_is (parser->lexer,
15692 CPP_SCOPE));
15693 template_id
15694 = finish_template_type (templ, arguments, entering_scope);
15696 /* A template-like identifier may be a partial concept id. */
15697 else if (flag_concepts
15698 && (template_id = (cp_parser_maybe_partial_concept_id
15699 (parser, templ, arguments))))
15700 return template_id;
15701 else if (variable_template_p (templ))
15703 template_id = lookup_template_variable (templ, arguments);
15704 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15705 SET_EXPR_LOCATION (template_id, combined_loc);
15707 else
15709 /* If it's not a class-template or a template-template, it should be
15710 a function-template. */
15711 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
15712 || TREE_CODE (templ) == OVERLOAD
15713 || BASELINK_P (templ)));
15715 template_id = lookup_template_function (templ, arguments);
15716 if (TREE_CODE (template_id) == TEMPLATE_ID_EXPR)
15717 SET_EXPR_LOCATION (template_id, combined_loc);
15720 /* If parsing tentatively, replace the sequence of tokens that makes
15721 up the template-id with a CPP_TEMPLATE_ID token. That way,
15722 should we re-parse the token stream, we will not have to repeat
15723 the effort required to do the parse, nor will we issue duplicate
15724 error messages about problems during instantiation of the
15725 template. */
15726 if (start_of_id
15727 /* Don't do this if we had a parse error in a declarator; re-parsing
15728 might succeed if a name changes meaning (60361). */
15729 && !(cp_parser_error_occurred (parser)
15730 && cp_parser_parsing_tentatively (parser)
15731 && parser->in_declarator_p))
15733 /* Reset the contents of the START_OF_ID token. */
15734 token->type = CPP_TEMPLATE_ID;
15735 token->location = combined_loc;
15737 /* We must mark the lookup as kept, so we don't throw it away on
15738 the first parse. */
15739 if (is_overloaded_fn (template_id))
15740 lookup_keep (get_fns (template_id), true);
15742 /* Retrieve any deferred checks. Do not pop this access checks yet
15743 so the memory will not be reclaimed during token replacing below. */
15744 token->u.tree_check_value = ggc_cleared_alloc<struct tree_check> ();
15745 token->u.tree_check_value->value = template_id;
15746 token->u.tree_check_value->checks = get_deferred_access_checks ();
15747 token->keyword = RID_MAX;
15749 /* Purge all subsequent tokens. */
15750 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
15752 /* ??? Can we actually assume that, if template_id ==
15753 error_mark_node, we will have issued a diagnostic to the
15754 user, as opposed to simply marking the tentative parse as
15755 failed? */
15756 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
15757 error_at (token->location, "parse error in template argument list");
15760 pop_to_parent_deferring_access_checks ();
15761 return template_id;
15764 /* Parse a template-name.
15766 template-name:
15767 identifier
15769 The standard should actually say:
15771 template-name:
15772 identifier
15773 operator-function-id
15775 A defect report has been filed about this issue.
15777 A conversion-function-id cannot be a template name because they cannot
15778 be part of a template-id. In fact, looking at this code:
15780 a.operator K<int>()
15782 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
15783 It is impossible to call a templated conversion-function-id with an
15784 explicit argument list, since the only allowed template parameter is
15785 the type to which it is converting.
15787 If TEMPLATE_KEYWORD_P is true, then we have just seen the
15788 `template' keyword, in a construction like:
15790 T::template f<3>()
15792 In that case `f' is taken to be a template-name, even though there
15793 is no way of knowing for sure.
15795 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
15796 name refers to a set of overloaded functions, at least one of which
15797 is a template, or an IDENTIFIER_NODE with the name of the template,
15798 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
15799 names are looked up inside uninstantiated templates. */
15801 static tree
15802 cp_parser_template_name (cp_parser* parser,
15803 bool template_keyword_p,
15804 bool check_dependency_p,
15805 bool is_declaration,
15806 enum tag_types tag_type,
15807 bool *is_identifier)
15809 tree identifier;
15810 tree decl;
15811 cp_token *token = cp_lexer_peek_token (parser->lexer);
15813 /* If the next token is `operator', then we have either an
15814 operator-function-id or a conversion-function-id. */
15815 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
15817 /* We don't know whether we're looking at an
15818 operator-function-id or a conversion-function-id. */
15819 cp_parser_parse_tentatively (parser);
15820 /* Try an operator-function-id. */
15821 identifier = cp_parser_operator_function_id (parser);
15822 /* If that didn't work, try a conversion-function-id. */
15823 if (!cp_parser_parse_definitely (parser))
15825 cp_parser_error (parser, "expected template-name");
15826 return error_mark_node;
15829 /* Look for the identifier. */
15830 else
15831 identifier = cp_parser_identifier (parser);
15833 /* If we didn't find an identifier, we don't have a template-id. */
15834 if (identifier == error_mark_node)
15835 return error_mark_node;
15837 /* If the name immediately followed the `template' keyword, then it
15838 is a template-name. However, if the next token is not `<', then
15839 we do not treat it as a template-name, since it is not being used
15840 as part of a template-id. This enables us to handle constructs
15841 like:
15843 template <typename T> struct S { S(); };
15844 template <typename T> S<T>::S();
15846 correctly. We would treat `S' as a template -- if it were `S<T>'
15847 -- but we do not if there is no `<'. */
15849 if (processing_template_decl
15850 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
15852 /* In a declaration, in a dependent context, we pretend that the
15853 "template" keyword was present in order to improve error
15854 recovery. For example, given:
15856 template <typename T> void f(T::X<int>);
15858 we want to treat "X<int>" as a template-id. */
15859 if (is_declaration
15860 && !template_keyword_p
15861 && parser->scope && TYPE_P (parser->scope)
15862 && check_dependency_p
15863 && dependent_scope_p (parser->scope)
15864 /* Do not do this for dtors (or ctors), since they never
15865 need the template keyword before their name. */
15866 && !constructor_name_p (identifier, parser->scope))
15868 cp_token_position start = 0;
15870 /* Explain what went wrong. */
15871 error_at (token->location, "non-template %qD used as template",
15872 identifier);
15873 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
15874 parser->scope, identifier);
15875 /* If parsing tentatively, find the location of the "<" token. */
15876 if (cp_parser_simulate_error (parser))
15877 start = cp_lexer_token_position (parser->lexer, true);
15878 /* Parse the template arguments so that we can issue error
15879 messages about them. */
15880 cp_lexer_consume_token (parser->lexer);
15881 cp_parser_enclosed_template_argument_list (parser);
15882 /* Skip tokens until we find a good place from which to
15883 continue parsing. */
15884 cp_parser_skip_to_closing_parenthesis (parser,
15885 /*recovering=*/true,
15886 /*or_comma=*/true,
15887 /*consume_paren=*/false);
15888 /* If parsing tentatively, permanently remove the
15889 template argument list. That will prevent duplicate
15890 error messages from being issued about the missing
15891 "template" keyword. */
15892 if (start)
15893 cp_lexer_purge_tokens_after (parser->lexer, start);
15894 if (is_identifier)
15895 *is_identifier = true;
15896 parser->context->object_type = NULL_TREE;
15897 return identifier;
15900 /* If the "template" keyword is present, then there is generally
15901 no point in doing name-lookup, so we just return IDENTIFIER.
15902 But, if the qualifying scope is non-dependent then we can
15903 (and must) do name-lookup normally. */
15904 if (template_keyword_p)
15906 tree scope = (parser->scope ? parser->scope
15907 : parser->context->object_type);
15908 if (scope && TYPE_P (scope)
15909 && (!CLASS_TYPE_P (scope)
15910 || (check_dependency_p && dependent_type_p (scope))))
15912 /* We're optimizing away the call to cp_parser_lookup_name, but
15913 we still need to do this. */
15914 parser->context->object_type = NULL_TREE;
15915 return identifier;
15920 /* Look up the name. */
15921 decl = cp_parser_lookup_name (parser, identifier,
15922 tag_type,
15923 /*is_template=*/true,
15924 /*is_namespace=*/false,
15925 check_dependency_p,
15926 /*ambiguous_decls=*/NULL,
15927 token->location);
15929 decl = strip_using_decl (decl);
15931 /* If DECL is a template, then the name was a template-name. */
15932 if (TREE_CODE (decl) == TEMPLATE_DECL)
15934 if (TREE_DEPRECATED (decl)
15935 && deprecated_state != DEPRECATED_SUPPRESS)
15936 warn_deprecated_use (decl, NULL_TREE);
15938 else
15940 /* The standard does not explicitly indicate whether a name that
15941 names a set of overloaded declarations, some of which are
15942 templates, is a template-name. However, such a name should
15943 be a template-name; otherwise, there is no way to form a
15944 template-id for the overloaded templates. */
15945 bool found = false;
15947 for (lkp_iterator iter (MAYBE_BASELINK_FUNCTIONS (decl));
15948 !found && iter; ++iter)
15949 if (TREE_CODE (*iter) == TEMPLATE_DECL)
15950 found = true;
15952 if (!found)
15954 /* The name does not name a template. */
15955 cp_parser_error (parser, "expected template-name");
15956 return error_mark_node;
15960 /* If DECL is dependent, and refers to a function, then just return
15961 its name; we will look it up again during template instantiation. */
15962 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
15964 tree scope = ovl_scope (decl);
15965 if (TYPE_P (scope) && dependent_type_p (scope))
15966 return identifier;
15969 return decl;
15972 /* Parse a template-argument-list.
15974 template-argument-list:
15975 template-argument ... [opt]
15976 template-argument-list , template-argument ... [opt]
15978 Returns a TREE_VEC containing the arguments. */
15980 static tree
15981 cp_parser_template_argument_list (cp_parser* parser)
15983 tree fixed_args[10];
15984 unsigned n_args = 0;
15985 unsigned alloced = 10;
15986 tree *arg_ary = fixed_args;
15987 tree vec;
15988 bool saved_in_template_argument_list_p;
15989 bool saved_ice_p;
15990 bool saved_non_ice_p;
15992 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
15993 parser->in_template_argument_list_p = true;
15994 /* Even if the template-id appears in an integral
15995 constant-expression, the contents of the argument list do
15996 not. */
15997 saved_ice_p = parser->integral_constant_expression_p;
15998 parser->integral_constant_expression_p = false;
15999 saved_non_ice_p = parser->non_integral_constant_expression_p;
16000 parser->non_integral_constant_expression_p = false;
16002 /* Parse the arguments. */
16005 tree argument;
16007 if (n_args)
16008 /* Consume the comma. */
16009 cp_lexer_consume_token (parser->lexer);
16011 /* Parse the template-argument. */
16012 argument = cp_parser_template_argument (parser);
16014 /* If the next token is an ellipsis, we're expanding a template
16015 argument pack. */
16016 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16018 if (argument == error_mark_node)
16020 cp_token *token = cp_lexer_peek_token (parser->lexer);
16021 error_at (token->location,
16022 "expected parameter pack before %<...%>");
16024 /* Consume the `...' token. */
16025 cp_lexer_consume_token (parser->lexer);
16027 /* Make the argument into a TYPE_PACK_EXPANSION or
16028 EXPR_PACK_EXPANSION. */
16029 argument = make_pack_expansion (argument);
16032 if (n_args == alloced)
16034 alloced *= 2;
16036 if (arg_ary == fixed_args)
16038 arg_ary = XNEWVEC (tree, alloced);
16039 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
16041 else
16042 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
16044 arg_ary[n_args++] = argument;
16046 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16048 vec = make_tree_vec (n_args);
16050 while (n_args--)
16051 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
16053 if (arg_ary != fixed_args)
16054 free (arg_ary);
16055 parser->non_integral_constant_expression_p = saved_non_ice_p;
16056 parser->integral_constant_expression_p = saved_ice_p;
16057 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
16058 if (CHECKING_P)
16059 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
16060 return vec;
16063 /* Parse a template-argument.
16065 template-argument:
16066 assignment-expression
16067 type-id
16068 id-expression
16070 The representation is that of an assignment-expression, type-id, or
16071 id-expression -- except that the qualified id-expression is
16072 evaluated, so that the value returned is either a DECL or an
16073 OVERLOAD.
16075 Although the standard says "assignment-expression", it forbids
16076 throw-expressions or assignments in the template argument.
16077 Therefore, we use "conditional-expression" instead. */
16079 static tree
16080 cp_parser_template_argument (cp_parser* parser)
16082 tree argument;
16083 bool template_p;
16084 bool address_p;
16085 bool maybe_type_id = false;
16086 cp_token *token = NULL, *argument_start_token = NULL;
16087 location_t loc = 0;
16088 cp_id_kind idk;
16090 /* There's really no way to know what we're looking at, so we just
16091 try each alternative in order.
16093 [temp.arg]
16095 In a template-argument, an ambiguity between a type-id and an
16096 expression is resolved to a type-id, regardless of the form of
16097 the corresponding template-parameter.
16099 Therefore, we try a type-id first. */
16100 cp_parser_parse_tentatively (parser);
16101 argument = cp_parser_template_type_arg (parser);
16102 /* If there was no error parsing the type-id but the next token is a
16103 '>>', our behavior depends on which dialect of C++ we're
16104 parsing. In C++98, we probably found a typo for '> >'. But there
16105 are type-id which are also valid expressions. For instance:
16107 struct X { int operator >> (int); };
16108 template <int V> struct Foo {};
16109 Foo<X () >> 5> r;
16111 Here 'X()' is a valid type-id of a function type, but the user just
16112 wanted to write the expression "X() >> 5". Thus, we remember that we
16113 found a valid type-id, but we still try to parse the argument as an
16114 expression to see what happens.
16116 In C++0x, the '>>' will be considered two separate '>'
16117 tokens. */
16118 if (!cp_parser_error_occurred (parser)
16119 && cxx_dialect == cxx98
16120 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16122 maybe_type_id = true;
16123 cp_parser_abort_tentative_parse (parser);
16125 else
16127 /* If the next token isn't a `,' or a `>', then this argument wasn't
16128 really finished. This means that the argument is not a valid
16129 type-id. */
16130 if (!cp_parser_next_token_ends_template_argument_p (parser))
16131 cp_parser_error (parser, "expected template-argument");
16132 /* If that worked, we're done. */
16133 if (cp_parser_parse_definitely (parser))
16134 return argument;
16136 /* We're still not sure what the argument will be. */
16137 cp_parser_parse_tentatively (parser);
16138 /* Try a template. */
16139 argument_start_token = cp_lexer_peek_token (parser->lexer);
16140 argument = cp_parser_id_expression (parser,
16141 /*template_keyword_p=*/false,
16142 /*check_dependency_p=*/true,
16143 &template_p,
16144 /*declarator_p=*/false,
16145 /*optional_p=*/false);
16146 /* If the next token isn't a `,' or a `>', then this argument wasn't
16147 really finished. */
16148 if (!cp_parser_next_token_ends_template_argument_p (parser))
16149 cp_parser_error (parser, "expected template-argument");
16150 if (!cp_parser_error_occurred (parser))
16152 /* Figure out what is being referred to. If the id-expression
16153 was for a class template specialization, then we will have a
16154 TYPE_DECL at this point. There is no need to do name lookup
16155 at this point in that case. */
16156 if (TREE_CODE (argument) != TYPE_DECL)
16157 argument = cp_parser_lookup_name (parser, argument,
16158 none_type,
16159 /*is_template=*/template_p,
16160 /*is_namespace=*/false,
16161 /*check_dependency=*/true,
16162 /*ambiguous_decls=*/NULL,
16163 argument_start_token->location);
16164 /* Handle a constrained-type-specifier for a non-type template
16165 parameter. */
16166 if (tree decl = cp_parser_maybe_concept_name (parser, argument))
16167 argument = decl;
16168 else if (TREE_CODE (argument) != TEMPLATE_DECL
16169 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
16170 cp_parser_error (parser, "expected template-name");
16172 if (cp_parser_parse_definitely (parser))
16174 if (TREE_DEPRECATED (argument))
16175 warn_deprecated_use (argument, NULL_TREE);
16176 return argument;
16178 /* It must be a non-type argument. In C++17 any constant-expression is
16179 allowed. */
16180 if (cxx_dialect > cxx14)
16181 goto general_expr;
16183 /* Otherwise, the permitted cases are given in [temp.arg.nontype]:
16185 -- an integral constant-expression of integral or enumeration
16186 type; or
16188 -- the name of a non-type template-parameter; or
16190 -- the name of an object or function with external linkage...
16192 -- the address of an object or function with external linkage...
16194 -- a pointer to member... */
16195 /* Look for a non-type template parameter. */
16196 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16198 cp_parser_parse_tentatively (parser);
16199 argument = cp_parser_primary_expression (parser,
16200 /*address_p=*/false,
16201 /*cast_p=*/false,
16202 /*template_arg_p=*/true,
16203 &idk);
16204 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
16205 || !cp_parser_next_token_ends_template_argument_p (parser))
16206 cp_parser_simulate_error (parser);
16207 if (cp_parser_parse_definitely (parser))
16208 return argument;
16211 /* If the next token is "&", the argument must be the address of an
16212 object or function with external linkage. */
16213 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
16214 if (address_p)
16216 loc = cp_lexer_peek_token (parser->lexer)->location;
16217 cp_lexer_consume_token (parser->lexer);
16219 /* See if we might have an id-expression. */
16220 token = cp_lexer_peek_token (parser->lexer);
16221 if (token->type == CPP_NAME
16222 || token->keyword == RID_OPERATOR
16223 || token->type == CPP_SCOPE
16224 || token->type == CPP_TEMPLATE_ID
16225 || token->type == CPP_NESTED_NAME_SPECIFIER)
16227 cp_parser_parse_tentatively (parser);
16228 argument = cp_parser_primary_expression (parser,
16229 address_p,
16230 /*cast_p=*/false,
16231 /*template_arg_p=*/true,
16232 &idk);
16233 if (cp_parser_error_occurred (parser)
16234 || !cp_parser_next_token_ends_template_argument_p (parser))
16235 cp_parser_abort_tentative_parse (parser);
16236 else
16238 tree probe;
16240 if (INDIRECT_REF_P (argument))
16242 /* Strip the dereference temporarily. */
16243 gcc_assert (REFERENCE_REF_P (argument));
16244 argument = TREE_OPERAND (argument, 0);
16247 /* If we're in a template, we represent a qualified-id referring
16248 to a static data member as a SCOPE_REF even if the scope isn't
16249 dependent so that we can check access control later. */
16250 probe = argument;
16251 if (TREE_CODE (probe) == SCOPE_REF)
16252 probe = TREE_OPERAND (probe, 1);
16253 if (VAR_P (probe))
16255 /* A variable without external linkage might still be a
16256 valid constant-expression, so no error is issued here
16257 if the external-linkage check fails. */
16258 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
16259 cp_parser_simulate_error (parser);
16261 else if (is_overloaded_fn (argument))
16262 /* All overloaded functions are allowed; if the external
16263 linkage test does not pass, an error will be issued
16264 later. */
16266 else if (address_p
16267 && (TREE_CODE (argument) == OFFSET_REF
16268 || TREE_CODE (argument) == SCOPE_REF))
16269 /* A pointer-to-member. */
16271 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
16273 else
16274 cp_parser_simulate_error (parser);
16276 if (cp_parser_parse_definitely (parser))
16278 if (address_p)
16279 argument = build_x_unary_op (loc, ADDR_EXPR, argument,
16280 tf_warning_or_error);
16281 else
16282 argument = convert_from_reference (argument);
16283 return argument;
16287 /* If the argument started with "&", there are no other valid
16288 alternatives at this point. */
16289 if (address_p)
16291 cp_parser_error (parser, "invalid non-type template argument");
16292 return error_mark_node;
16295 general_expr:
16296 /* If the argument wasn't successfully parsed as a type-id followed
16297 by '>>', the argument can only be a constant expression now.
16298 Otherwise, we try parsing the constant-expression tentatively,
16299 because the argument could really be a type-id. */
16300 if (maybe_type_id)
16301 cp_parser_parse_tentatively (parser);
16303 if (cxx_dialect <= cxx14)
16304 argument = cp_parser_constant_expression (parser);
16305 else
16307 /* With C++17 generalized non-type template arguments we need to handle
16308 lvalue constant expressions, too. */
16309 argument = cp_parser_assignment_expression (parser);
16310 require_potential_constant_expression (argument);
16313 if (!maybe_type_id)
16314 return argument;
16315 if (!cp_parser_next_token_ends_template_argument_p (parser))
16316 cp_parser_error (parser, "expected template-argument");
16317 if (cp_parser_parse_definitely (parser))
16318 return argument;
16319 /* We did our best to parse the argument as a non type-id, but that
16320 was the only alternative that matched (albeit with a '>' after
16321 it). We can assume it's just a typo from the user, and a
16322 diagnostic will then be issued. */
16323 return cp_parser_template_type_arg (parser);
16326 /* Parse an explicit-instantiation.
16328 explicit-instantiation:
16329 template declaration
16331 Although the standard says `declaration', what it really means is:
16333 explicit-instantiation:
16334 template decl-specifier-seq [opt] declarator [opt] ;
16336 Things like `template int S<int>::i = 5, int S<double>::j;' are not
16337 supposed to be allowed. A defect report has been filed about this
16338 issue.
16340 GNU Extension:
16342 explicit-instantiation:
16343 storage-class-specifier template
16344 decl-specifier-seq [opt] declarator [opt] ;
16345 function-specifier template
16346 decl-specifier-seq [opt] declarator [opt] ; */
16348 static void
16349 cp_parser_explicit_instantiation (cp_parser* parser)
16351 int declares_class_or_enum;
16352 cp_decl_specifier_seq decl_specifiers;
16353 tree extension_specifier = NULL_TREE;
16355 timevar_push (TV_TEMPLATE_INST);
16357 /* Look for an (optional) storage-class-specifier or
16358 function-specifier. */
16359 if (cp_parser_allow_gnu_extensions_p (parser))
16361 extension_specifier
16362 = cp_parser_storage_class_specifier_opt (parser);
16363 if (!extension_specifier)
16364 extension_specifier
16365 = cp_parser_function_specifier_opt (parser,
16366 /*decl_specs=*/NULL);
16369 /* Look for the `template' keyword. */
16370 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16371 /* Let the front end know that we are processing an explicit
16372 instantiation. */
16373 begin_explicit_instantiation ();
16374 /* [temp.explicit] says that we are supposed to ignore access
16375 control while processing explicit instantiation directives. */
16376 push_deferring_access_checks (dk_no_check);
16377 /* Parse a decl-specifier-seq. */
16378 cp_parser_decl_specifier_seq (parser,
16379 CP_PARSER_FLAGS_OPTIONAL,
16380 &decl_specifiers,
16381 &declares_class_or_enum);
16382 /* If there was exactly one decl-specifier, and it declared a class,
16383 and there's no declarator, then we have an explicit type
16384 instantiation. */
16385 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
16387 tree type;
16389 type = check_tag_decl (&decl_specifiers,
16390 /*explicit_type_instantiation_p=*/true);
16391 /* Turn access control back on for names used during
16392 template instantiation. */
16393 pop_deferring_access_checks ();
16394 if (type)
16395 do_type_instantiation (type, extension_specifier,
16396 /*complain=*/tf_error);
16398 else
16400 cp_declarator *declarator;
16401 tree decl;
16403 /* Parse the declarator. */
16404 declarator
16405 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16406 /*ctor_dtor_or_conv_p=*/NULL,
16407 /*parenthesized_p=*/NULL,
16408 /*member_p=*/false,
16409 /*friend_p=*/false);
16410 if (declares_class_or_enum & 2)
16411 cp_parser_check_for_definition_in_return_type (declarator,
16412 decl_specifiers.type,
16413 decl_specifiers.locations[ds_type_spec]);
16414 if (declarator != cp_error_declarator)
16416 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_inline))
16417 permerror (decl_specifiers.locations[ds_inline],
16418 "explicit instantiation shall not use"
16419 " %<inline%> specifier");
16420 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_constexpr))
16421 permerror (decl_specifiers.locations[ds_constexpr],
16422 "explicit instantiation shall not use"
16423 " %<constexpr%> specifier");
16425 decl = grokdeclarator (declarator, &decl_specifiers,
16426 NORMAL, 0, &decl_specifiers.attributes);
16427 /* Turn access control back on for names used during
16428 template instantiation. */
16429 pop_deferring_access_checks ();
16430 /* Do the explicit instantiation. */
16431 do_decl_instantiation (decl, extension_specifier);
16433 else
16435 pop_deferring_access_checks ();
16436 /* Skip the body of the explicit instantiation. */
16437 cp_parser_skip_to_end_of_statement (parser);
16440 /* We're done with the instantiation. */
16441 end_explicit_instantiation ();
16443 cp_parser_consume_semicolon_at_end_of_statement (parser);
16445 timevar_pop (TV_TEMPLATE_INST);
16448 /* Parse an explicit-specialization.
16450 explicit-specialization:
16451 template < > declaration
16453 Although the standard says `declaration', what it really means is:
16455 explicit-specialization:
16456 template <> decl-specifier [opt] init-declarator [opt] ;
16457 template <> function-definition
16458 template <> explicit-specialization
16459 template <> template-declaration */
16461 static void
16462 cp_parser_explicit_specialization (cp_parser* parser)
16464 bool need_lang_pop;
16465 cp_token *token = cp_lexer_peek_token (parser->lexer);
16467 /* Look for the `template' keyword. */
16468 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
16469 /* Look for the `<'. */
16470 cp_parser_require (parser, CPP_LESS, RT_LESS);
16471 /* Look for the `>'. */
16472 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
16473 /* We have processed another parameter list. */
16474 ++parser->num_template_parameter_lists;
16475 /* [temp]
16477 A template ... explicit specialization ... shall not have C
16478 linkage. */
16479 if (current_lang_name == lang_name_c)
16481 error_at (token->location, "template specialization with C linkage");
16482 /* Give it C++ linkage to avoid confusing other parts of the
16483 front end. */
16484 push_lang_context (lang_name_cplusplus);
16485 need_lang_pop = true;
16487 else
16488 need_lang_pop = false;
16489 /* Let the front end know that we are beginning a specialization. */
16490 if (!begin_specialization ())
16492 end_specialization ();
16493 return;
16496 /* If the next keyword is `template', we need to figure out whether
16497 or not we're looking a template-declaration. */
16498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16500 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16501 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
16502 cp_parser_template_declaration_after_export (parser,
16503 /*member_p=*/false);
16504 else
16505 cp_parser_explicit_specialization (parser);
16507 else
16508 /* Parse the dependent declaration. */
16509 cp_parser_single_declaration (parser,
16510 /*checks=*/NULL,
16511 /*member_p=*/false,
16512 /*explicit_specialization_p=*/true,
16513 /*friend_p=*/NULL);
16514 /* We're done with the specialization. */
16515 end_specialization ();
16516 /* For the erroneous case of a template with C linkage, we pushed an
16517 implicit C++ linkage scope; exit that scope now. */
16518 if (need_lang_pop)
16519 pop_lang_context ();
16520 /* We're done with this parameter list. */
16521 --parser->num_template_parameter_lists;
16524 /* Parse a type-specifier.
16526 type-specifier:
16527 simple-type-specifier
16528 class-specifier
16529 enum-specifier
16530 elaborated-type-specifier
16531 cv-qualifier
16533 GNU Extension:
16535 type-specifier:
16536 __complex__
16538 Returns a representation of the type-specifier. For a
16539 class-specifier, enum-specifier, or elaborated-type-specifier, a
16540 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
16542 The parser flags FLAGS is used to control type-specifier parsing.
16544 If IS_DECLARATION is TRUE, then this type-specifier is appearing
16545 in a decl-specifier-seq.
16547 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
16548 class-specifier, enum-specifier, or elaborated-type-specifier, then
16549 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
16550 if a type is declared; 2 if it is defined. Otherwise, it is set to
16551 zero.
16553 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
16554 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
16555 is set to FALSE. */
16557 static tree
16558 cp_parser_type_specifier (cp_parser* parser,
16559 cp_parser_flags flags,
16560 cp_decl_specifier_seq *decl_specs,
16561 bool is_declaration,
16562 int* declares_class_or_enum,
16563 bool* is_cv_qualifier)
16565 tree type_spec = NULL_TREE;
16566 cp_token *token;
16567 enum rid keyword;
16568 cp_decl_spec ds = ds_last;
16570 /* Assume this type-specifier does not declare a new type. */
16571 if (declares_class_or_enum)
16572 *declares_class_or_enum = 0;
16573 /* And that it does not specify a cv-qualifier. */
16574 if (is_cv_qualifier)
16575 *is_cv_qualifier = false;
16576 /* Peek at the next token. */
16577 token = cp_lexer_peek_token (parser->lexer);
16579 /* If we're looking at a keyword, we can use that to guide the
16580 production we choose. */
16581 keyword = token->keyword;
16582 switch (keyword)
16584 case RID_ENUM:
16585 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16586 goto elaborated_type_specifier;
16588 /* Look for the enum-specifier. */
16589 type_spec = cp_parser_enum_specifier (parser);
16590 /* If that worked, we're done. */
16591 if (type_spec)
16593 if (declares_class_or_enum)
16594 *declares_class_or_enum = 2;
16595 if (decl_specs)
16596 cp_parser_set_decl_spec_type (decl_specs,
16597 type_spec,
16598 token,
16599 /*type_definition_p=*/true);
16600 return type_spec;
16602 else
16603 goto elaborated_type_specifier;
16605 /* Any of these indicate either a class-specifier, or an
16606 elaborated-type-specifier. */
16607 case RID_CLASS:
16608 case RID_STRUCT:
16609 case RID_UNION:
16610 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
16611 goto elaborated_type_specifier;
16613 /* Parse tentatively so that we can back up if we don't find a
16614 class-specifier. */
16615 cp_parser_parse_tentatively (parser);
16616 /* Look for the class-specifier. */
16617 type_spec = cp_parser_class_specifier (parser);
16618 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
16619 /* If that worked, we're done. */
16620 if (cp_parser_parse_definitely (parser))
16622 if (declares_class_or_enum)
16623 *declares_class_or_enum = 2;
16624 if (decl_specs)
16625 cp_parser_set_decl_spec_type (decl_specs,
16626 type_spec,
16627 token,
16628 /*type_definition_p=*/true);
16629 return type_spec;
16632 /* Fall through. */
16633 elaborated_type_specifier:
16634 /* We're declaring (not defining) a class or enum. */
16635 if (declares_class_or_enum)
16636 *declares_class_or_enum = 1;
16638 /* Fall through. */
16639 case RID_TYPENAME:
16640 /* Look for an elaborated-type-specifier. */
16641 type_spec
16642 = (cp_parser_elaborated_type_specifier
16643 (parser,
16644 decl_spec_seq_has_spec_p (decl_specs, ds_friend),
16645 is_declaration));
16646 if (decl_specs)
16647 cp_parser_set_decl_spec_type (decl_specs,
16648 type_spec,
16649 token,
16650 /*type_definition_p=*/false);
16651 return type_spec;
16653 case RID_CONST:
16654 ds = ds_const;
16655 if (is_cv_qualifier)
16656 *is_cv_qualifier = true;
16657 break;
16659 case RID_VOLATILE:
16660 ds = ds_volatile;
16661 if (is_cv_qualifier)
16662 *is_cv_qualifier = true;
16663 break;
16665 case RID_RESTRICT:
16666 ds = ds_restrict;
16667 if (is_cv_qualifier)
16668 *is_cv_qualifier = true;
16669 break;
16671 case RID_COMPLEX:
16672 /* The `__complex__' keyword is a GNU extension. */
16673 ds = ds_complex;
16674 break;
16676 default:
16677 break;
16680 /* Handle simple keywords. */
16681 if (ds != ds_last)
16683 if (decl_specs)
16685 set_and_check_decl_spec_loc (decl_specs, ds, token);
16686 decl_specs->any_specifiers_p = true;
16688 return cp_lexer_consume_token (parser->lexer)->u.value;
16691 /* If we do not already have a type-specifier, assume we are looking
16692 at a simple-type-specifier. */
16693 type_spec = cp_parser_simple_type_specifier (parser,
16694 decl_specs,
16695 flags);
16697 /* If we didn't find a type-specifier, and a type-specifier was not
16698 optional in this context, issue an error message. */
16699 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
16701 cp_parser_error (parser, "expected type specifier");
16702 return error_mark_node;
16705 return type_spec;
16708 /* Parse a simple-type-specifier.
16710 simple-type-specifier:
16711 :: [opt] nested-name-specifier [opt] type-name
16712 :: [opt] nested-name-specifier template template-id
16713 char
16714 wchar_t
16715 bool
16716 short
16718 long
16719 signed
16720 unsigned
16721 float
16722 double
16723 void
16725 C++11 Extension:
16727 simple-type-specifier:
16728 auto
16729 decltype ( expression )
16730 char16_t
16731 char32_t
16732 __underlying_type ( type-id )
16734 C++17 extension:
16736 nested-name-specifier(opt) template-name
16738 GNU Extension:
16740 simple-type-specifier:
16741 __int128
16742 __typeof__ unary-expression
16743 __typeof__ ( type-id )
16744 __typeof__ ( type-id ) { initializer-list , [opt] }
16746 Concepts Extension:
16748 simple-type-specifier:
16749 constrained-type-specifier
16751 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
16752 appropriately updated. */
16754 static tree
16755 cp_parser_simple_type_specifier (cp_parser* parser,
16756 cp_decl_specifier_seq *decl_specs,
16757 cp_parser_flags flags)
16759 tree type = NULL_TREE;
16760 cp_token *token;
16761 int idx;
16763 /* Peek at the next token. */
16764 token = cp_lexer_peek_token (parser->lexer);
16766 /* If we're looking at a keyword, things are easy. */
16767 switch (token->keyword)
16769 case RID_CHAR:
16770 if (decl_specs)
16771 decl_specs->explicit_char_p = true;
16772 type = char_type_node;
16773 break;
16774 case RID_CHAR16:
16775 type = char16_type_node;
16776 break;
16777 case RID_CHAR32:
16778 type = char32_type_node;
16779 break;
16780 case RID_WCHAR:
16781 type = wchar_type_node;
16782 break;
16783 case RID_BOOL:
16784 type = boolean_type_node;
16785 break;
16786 case RID_SHORT:
16787 set_and_check_decl_spec_loc (decl_specs, ds_short, token);
16788 type = short_integer_type_node;
16789 break;
16790 case RID_INT:
16791 if (decl_specs)
16792 decl_specs->explicit_int_p = true;
16793 type = integer_type_node;
16794 break;
16795 case RID_INT_N_0:
16796 case RID_INT_N_1:
16797 case RID_INT_N_2:
16798 case RID_INT_N_3:
16799 idx = token->keyword - RID_INT_N_0;
16800 if (! int_n_enabled_p [idx])
16801 break;
16802 if (decl_specs)
16804 decl_specs->explicit_intN_p = true;
16805 decl_specs->int_n_idx = idx;
16807 type = int_n_trees [idx].signed_type;
16808 break;
16809 case RID_LONG:
16810 if (decl_specs)
16811 set_and_check_decl_spec_loc (decl_specs, ds_long, token);
16812 type = long_integer_type_node;
16813 break;
16814 case RID_SIGNED:
16815 set_and_check_decl_spec_loc (decl_specs, ds_signed, token);
16816 type = integer_type_node;
16817 break;
16818 case RID_UNSIGNED:
16819 set_and_check_decl_spec_loc (decl_specs, ds_unsigned, token);
16820 type = unsigned_type_node;
16821 break;
16822 case RID_FLOAT:
16823 type = float_type_node;
16824 break;
16825 case RID_DOUBLE:
16826 type = double_type_node;
16827 break;
16828 case RID_VOID:
16829 type = void_type_node;
16830 break;
16832 case RID_AUTO:
16833 maybe_warn_cpp0x (CPP0X_AUTO);
16834 if (parser->auto_is_implicit_function_template_parm_p)
16836 /* The 'auto' might be the placeholder return type for a function decl
16837 with trailing return type. */
16838 bool have_trailing_return_fn_decl = false;
16840 cp_parser_parse_tentatively (parser);
16841 cp_lexer_consume_token (parser->lexer);
16842 while (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
16843 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
16844 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16845 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
16847 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16849 cp_lexer_consume_token (parser->lexer);
16850 cp_parser_skip_to_closing_parenthesis (parser,
16851 /*recovering*/false,
16852 /*or_comma*/false,
16853 /*consume_paren*/true);
16854 continue;
16857 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
16859 have_trailing_return_fn_decl = true;
16860 break;
16863 cp_lexer_consume_token (parser->lexer);
16865 cp_parser_abort_tentative_parse (parser);
16867 if (have_trailing_return_fn_decl)
16869 type = make_auto ();
16870 break;
16873 if (cxx_dialect >= cxx14)
16875 type = synthesize_implicit_template_parm (parser, NULL_TREE);
16876 type = TREE_TYPE (type);
16878 else
16879 type = error_mark_node;
16881 if (current_class_type && LAMBDA_TYPE_P (current_class_type))
16883 if (cxx_dialect < cxx14)
16884 error_at (token->location,
16885 "use of %<auto%> in lambda parameter declaration "
16886 "only available with "
16887 "-std=c++14 or -std=gnu++14");
16889 else if (cxx_dialect < cxx14)
16890 error_at (token->location,
16891 "use of %<auto%> in parameter declaration "
16892 "only available with "
16893 "-std=c++14 or -std=gnu++14");
16894 else if (!flag_concepts)
16895 pedwarn (token->location, OPT_Wpedantic,
16896 "ISO C++ forbids use of %<auto%> in parameter "
16897 "declaration");
16899 else
16900 type = make_auto ();
16901 break;
16903 case RID_DECLTYPE:
16904 /* Since DR 743, decltype can either be a simple-type-specifier by
16905 itself or begin a nested-name-specifier. Parsing it will replace
16906 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
16907 handling below decide what to do. */
16908 cp_parser_decltype (parser);
16909 cp_lexer_set_token_position (parser->lexer, token);
16910 break;
16912 case RID_TYPEOF:
16913 /* Consume the `typeof' token. */
16914 cp_lexer_consume_token (parser->lexer);
16915 /* Parse the operand to `typeof'. */
16916 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
16917 /* If it is not already a TYPE, take its type. */
16918 if (!TYPE_P (type))
16919 type = finish_typeof (type);
16921 if (decl_specs)
16922 cp_parser_set_decl_spec_type (decl_specs, type,
16923 token,
16924 /*type_definition_p=*/false);
16926 return type;
16928 case RID_UNDERLYING_TYPE:
16929 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
16930 if (decl_specs)
16931 cp_parser_set_decl_spec_type (decl_specs, type,
16932 token,
16933 /*type_definition_p=*/false);
16935 return type;
16937 case RID_BASES:
16938 case RID_DIRECT_BASES:
16939 type = cp_parser_trait_expr (parser, token->keyword);
16940 if (decl_specs)
16941 cp_parser_set_decl_spec_type (decl_specs, type,
16942 token,
16943 /*type_definition_p=*/false);
16944 return type;
16945 default:
16946 break;
16949 /* If token is an already-parsed decltype not followed by ::,
16950 it's a simple-type-specifier. */
16951 if (token->type == CPP_DECLTYPE
16952 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
16954 type = saved_checks_value (token->u.tree_check_value);
16955 if (decl_specs)
16957 cp_parser_set_decl_spec_type (decl_specs, type,
16958 token,
16959 /*type_definition_p=*/false);
16960 /* Remember that we are handling a decltype in order to
16961 implement the resolution of DR 1510 when the argument
16962 isn't instantiation dependent. */
16963 decl_specs->decltype_p = true;
16965 cp_lexer_consume_token (parser->lexer);
16966 return type;
16969 /* If the type-specifier was for a built-in type, we're done. */
16970 if (type)
16972 /* Record the type. */
16973 if (decl_specs
16974 && (token->keyword != RID_SIGNED
16975 && token->keyword != RID_UNSIGNED
16976 && token->keyword != RID_SHORT
16977 && token->keyword != RID_LONG))
16978 cp_parser_set_decl_spec_type (decl_specs,
16979 type,
16980 token,
16981 /*type_definition_p=*/false);
16982 if (decl_specs)
16983 decl_specs->any_specifiers_p = true;
16985 /* Consume the token. */
16986 cp_lexer_consume_token (parser->lexer);
16988 if (type == error_mark_node)
16989 return error_mark_node;
16991 /* There is no valid C++ program where a non-template type is
16992 followed by a "<". That usually indicates that the user thought
16993 that the type was a template. */
16994 cp_parser_check_for_invalid_template_id (parser, type, none_type,
16995 token->location);
16997 return TYPE_NAME (type);
17000 /* The type-specifier must be a user-defined type. */
17001 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
17003 bool qualified_p;
17004 bool global_p;
17006 /* Don't gobble tokens or issue error messages if this is an
17007 optional type-specifier. */
17008 if ((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17009 cp_parser_parse_tentatively (parser);
17011 token = cp_lexer_peek_token (parser->lexer);
17013 /* Look for the optional `::' operator. */
17014 global_p
17015 = (cp_parser_global_scope_opt (parser,
17016 /*current_scope_valid_p=*/false)
17017 != NULL_TREE);
17018 /* Look for the nested-name specifier. */
17019 qualified_p
17020 = (cp_parser_nested_name_specifier_opt (parser,
17021 /*typename_keyword_p=*/false,
17022 /*check_dependency_p=*/true,
17023 /*type_p=*/false,
17024 /*is_declaration=*/false)
17025 != NULL_TREE);
17026 /* If we have seen a nested-name-specifier, and the next token
17027 is `template', then we are using the template-id production. */
17028 if (parser->scope
17029 && cp_parser_optional_template_keyword (parser))
17031 /* Look for the template-id. */
17032 type = cp_parser_template_id (parser,
17033 /*template_keyword_p=*/true,
17034 /*check_dependency_p=*/true,
17035 none_type,
17036 /*is_declaration=*/false);
17037 /* If the template-id did not name a type, we are out of
17038 luck. */
17039 if (TREE_CODE (type) != TYPE_DECL)
17041 cp_parser_error (parser, "expected template-id for type");
17042 type = NULL_TREE;
17045 /* Otherwise, look for a type-name. */
17046 else
17047 type = cp_parser_type_name (parser);
17048 /* Keep track of all name-lookups performed in class scopes. */
17049 if (type
17050 && !global_p
17051 && !qualified_p
17052 && TREE_CODE (type) == TYPE_DECL
17053 && identifier_p (DECL_NAME (type)))
17054 maybe_note_name_used_in_class (DECL_NAME (type), type);
17055 /* If it didn't work out, we don't have a TYPE. */
17056 if (((flags & CP_PARSER_FLAGS_OPTIONAL) || cxx_dialect >= cxx1z)
17057 && !cp_parser_parse_definitely (parser))
17058 type = NULL_TREE;
17059 if (!type && cxx_dialect >= cxx1z)
17061 if (flags & CP_PARSER_FLAGS_OPTIONAL)
17062 cp_parser_parse_tentatively (parser);
17064 cp_parser_global_scope_opt (parser,
17065 /*current_scope_valid_p=*/false);
17066 cp_parser_nested_name_specifier_opt (parser,
17067 /*typename_keyword_p=*/false,
17068 /*check_dependency_p=*/true,
17069 /*type_p=*/false,
17070 /*is_declaration=*/false);
17071 tree name = cp_parser_identifier (parser);
17072 if (name && TREE_CODE (name) == IDENTIFIER_NODE
17073 && parser->scope != error_mark_node)
17075 tree tmpl = cp_parser_lookup_name (parser, name,
17076 none_type,
17077 /*is_template=*/false,
17078 /*is_namespace=*/false,
17079 /*check_dependency=*/true,
17080 /*ambiguous_decls=*/NULL,
17081 token->location);
17082 if (tmpl && tmpl != error_mark_node
17083 && (DECL_CLASS_TEMPLATE_P (tmpl)
17084 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
17085 type = make_template_placeholder (tmpl);
17086 else
17088 type = error_mark_node;
17089 if (!cp_parser_simulate_error (parser))
17090 cp_parser_name_lookup_error (parser, name, tmpl,
17091 NLE_TYPE, token->location);
17094 else
17095 type = error_mark_node;
17097 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
17098 && !cp_parser_parse_definitely (parser))
17099 type = NULL_TREE;
17101 if (type && decl_specs)
17102 cp_parser_set_decl_spec_type (decl_specs, type,
17103 token,
17104 /*type_definition_p=*/false);
17107 /* If we didn't get a type-name, issue an error message. */
17108 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
17110 cp_parser_error (parser, "expected type-name");
17111 return error_mark_node;
17114 if (type && type != error_mark_node)
17116 /* See if TYPE is an Objective-C type, and if so, parse and
17117 accept any protocol references following it. Do this before
17118 the cp_parser_check_for_invalid_template_id() call, because
17119 Objective-C types can be followed by '<...>' which would
17120 enclose protocol names rather than template arguments, and so
17121 everything is fine. */
17122 if (c_dialect_objc () && !parser->scope
17123 && (objc_is_id (type) || objc_is_class_name (type)))
17125 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17126 tree qual_type = objc_get_protocol_qualified_type (type, protos);
17128 /* Clobber the "unqualified" type previously entered into
17129 DECL_SPECS with the new, improved protocol-qualified version. */
17130 if (decl_specs)
17131 decl_specs->type = qual_type;
17133 return qual_type;
17136 /* There is no valid C++ program where a non-template type is
17137 followed by a "<". That usually indicates that the user
17138 thought that the type was a template. */
17139 cp_parser_check_for_invalid_template_id (parser, type,
17140 none_type,
17141 token->location);
17144 return type;
17147 /* Parse a type-name.
17149 type-name:
17150 class-name
17151 enum-name
17152 typedef-name
17153 simple-template-id [in c++0x]
17155 enum-name:
17156 identifier
17158 typedef-name:
17159 identifier
17161 Concepts:
17163 type-name:
17164 concept-name
17165 partial-concept-id
17167 concept-name:
17168 identifier
17170 Returns a TYPE_DECL for the type. */
17172 static tree
17173 cp_parser_type_name (cp_parser* parser)
17175 return cp_parser_type_name (parser, /*typename_keyword_p=*/false);
17178 /* See above. */
17179 static tree
17180 cp_parser_type_name (cp_parser* parser, bool typename_keyword_p)
17182 tree type_decl;
17184 /* We can't know yet whether it is a class-name or not. */
17185 cp_parser_parse_tentatively (parser);
17186 /* Try a class-name. */
17187 type_decl = cp_parser_class_name (parser,
17188 typename_keyword_p,
17189 /*template_keyword_p=*/false,
17190 none_type,
17191 /*check_dependency_p=*/true,
17192 /*class_head_p=*/false,
17193 /*is_declaration=*/false);
17194 /* If it's not a class-name, keep looking. */
17195 if (!cp_parser_parse_definitely (parser))
17197 if (cxx_dialect < cxx11)
17198 /* It must be a typedef-name or an enum-name. */
17199 return cp_parser_nonclass_name (parser);
17201 cp_parser_parse_tentatively (parser);
17202 /* It is either a simple-template-id representing an
17203 instantiation of an alias template... */
17204 type_decl = cp_parser_template_id (parser,
17205 /*template_keyword_p=*/false,
17206 /*check_dependency_p=*/true,
17207 none_type,
17208 /*is_declaration=*/false);
17209 /* Note that this must be an instantiation of an alias template
17210 because [temp.names]/6 says:
17212 A template-id that names an alias template specialization
17213 is a type-name.
17215 Whereas [temp.names]/7 says:
17217 A simple-template-id that names a class template
17218 specialization is a class-name.
17220 With concepts, this could also be a partial-concept-id that
17221 declares a non-type template parameter. */
17222 if (type_decl != NULL_TREE
17223 && TREE_CODE (type_decl) == TYPE_DECL
17224 && TYPE_DECL_ALIAS_P (type_decl))
17225 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
17226 else if (is_constrained_parameter (type_decl))
17227 /* Don't do anything. */ ;
17228 else
17229 cp_parser_simulate_error (parser);
17231 if (!cp_parser_parse_definitely (parser))
17232 /* ... Or a typedef-name or an enum-name. */
17233 return cp_parser_nonclass_name (parser);
17236 return type_decl;
17239 /* Check if DECL and ARGS can form a constrained-type-specifier.
17240 If ARGS is non-null, we try to form a concept check of the
17241 form DECL<?, ARGS> where ? is a wildcard that matches any
17242 kind of template argument. If ARGS is NULL, then we try to
17243 form a concept check of the form DECL<?>. */
17245 static tree
17246 cp_parser_maybe_constrained_type_specifier (cp_parser *parser,
17247 tree decl, tree args)
17249 gcc_assert (args ? TREE_CODE (args) == TREE_VEC : true);
17251 /* If we a constrained-type-specifier cannot be deduced. */
17252 if (parser->prevent_constrained_type_specifiers)
17253 return NULL_TREE;
17255 /* A constrained type specifier can only be found in an
17256 overload set or as a reference to a template declaration.
17258 FIXME: This might be masking a bug. It's possible that
17259 that the deduction below is causing template specializations
17260 to be formed with the wildcard as an argument. */
17261 if (TREE_CODE (decl) != OVERLOAD && TREE_CODE (decl) != TEMPLATE_DECL)
17262 return NULL_TREE;
17264 /* Try to build a call expression that evaluates the
17265 concept. This can fail if the overload set refers
17266 only to non-templates. */
17267 tree placeholder = build_nt (WILDCARD_DECL);
17268 tree check = build_concept_check (decl, placeholder, args);
17269 if (check == error_mark_node)
17270 return NULL_TREE;
17272 /* Deduce the checked constraint and the prototype parameter.
17274 FIXME: In certain cases, failure to deduce should be a
17275 diagnosable error. */
17276 tree conc;
17277 tree proto;
17278 if (!deduce_constrained_parameter (check, conc, proto))
17279 return NULL_TREE;
17281 /* In template parameter scope, this results in a constrained
17282 parameter. Return a descriptor of that parm. */
17283 if (processing_template_parmlist)
17284 return build_constrained_parameter (conc, proto, args);
17286 /* In a parameter-declaration-clause, constrained-type
17287 specifiers result in invented template parameters. */
17288 if (parser->auto_is_implicit_function_template_parm_p)
17290 tree x = build_constrained_parameter (conc, proto, args);
17291 return synthesize_implicit_template_parm (parser, x);
17293 else
17295 /* Otherwise, we're in a context where the constrained
17296 type name is deduced and the constraint applies
17297 after deduction. */
17298 return make_constrained_auto (conc, args);
17301 return NULL_TREE;
17304 /* If DECL refers to a concept, return a TYPE_DECL representing
17305 the result of using the constrained type specifier in the
17306 current context. DECL refers to a concept if
17308 - it is an overload set containing a function concept taking a single
17309 type argument, or
17311 - it is a variable concept taking a single type argument. */
17313 static tree
17314 cp_parser_maybe_concept_name (cp_parser* parser, tree decl)
17316 if (flag_concepts
17317 && (TREE_CODE (decl) == OVERLOAD
17318 || BASELINK_P (decl)
17319 || variable_concept_p (decl)))
17320 return cp_parser_maybe_constrained_type_specifier (parser, decl, NULL_TREE);
17321 else
17322 return NULL_TREE;
17325 /* Check if DECL and ARGS form a partial-concept-id. If so,
17326 assign ID to the resulting constrained placeholder.
17328 Returns true if the partial-concept-id designates a placeholder
17329 and false otherwise. Note that *id is set to NULL_TREE in
17330 this case. */
17332 static tree
17333 cp_parser_maybe_partial_concept_id (cp_parser *parser, tree decl, tree args)
17335 return cp_parser_maybe_constrained_type_specifier (parser, decl, args);
17338 /* Parse a non-class type-name, that is, either an enum-name, a typedef-name,
17339 or a concept-name.
17341 enum-name:
17342 identifier
17344 typedef-name:
17345 identifier
17347 concept-name:
17348 identifier
17350 Returns a TYPE_DECL for the type. */
17352 static tree
17353 cp_parser_nonclass_name (cp_parser* parser)
17355 tree type_decl;
17356 tree identifier;
17358 cp_token *token = cp_lexer_peek_token (parser->lexer);
17359 identifier = cp_parser_identifier (parser);
17360 if (identifier == error_mark_node)
17361 return error_mark_node;
17363 /* Look up the type-name. */
17364 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
17366 type_decl = strip_using_decl (type_decl);
17368 /* If we found an overload set, then it may refer to a concept-name. */
17369 if (tree decl = cp_parser_maybe_concept_name (parser, type_decl))
17370 type_decl = decl;
17372 if (TREE_CODE (type_decl) != TYPE_DECL
17373 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
17375 /* See if this is an Objective-C type. */
17376 tree protos = cp_parser_objc_protocol_refs_opt (parser);
17377 tree type = objc_get_protocol_qualified_type (identifier, protos);
17378 if (type)
17379 type_decl = TYPE_NAME (type);
17382 /* Issue an error if we did not find a type-name. */
17383 if (TREE_CODE (type_decl) != TYPE_DECL
17384 /* In Objective-C, we have the complication that class names are
17385 normally type names and start declarations (eg, the
17386 "NSObject" in "NSObject *object;"), but can be used in an
17387 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
17388 is an expression. So, a classname followed by a dot is not a
17389 valid type-name. */
17390 || (objc_is_class_name (TREE_TYPE (type_decl))
17391 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
17393 if (!cp_parser_simulate_error (parser))
17394 cp_parser_name_lookup_error (parser, identifier, type_decl,
17395 NLE_TYPE, token->location);
17396 return error_mark_node;
17398 /* Remember that the name was used in the definition of the
17399 current class so that we can check later to see if the
17400 meaning would have been different after the class was
17401 entirely defined. */
17402 else if (type_decl != error_mark_node
17403 && !parser->scope)
17404 maybe_note_name_used_in_class (identifier, type_decl);
17406 return type_decl;
17409 /* Parse an elaborated-type-specifier. Note that the grammar given
17410 here incorporates the resolution to DR68.
17412 elaborated-type-specifier:
17413 class-key :: [opt] nested-name-specifier [opt] identifier
17414 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
17415 enum-key :: [opt] nested-name-specifier [opt] identifier
17416 typename :: [opt] nested-name-specifier identifier
17417 typename :: [opt] nested-name-specifier template [opt]
17418 template-id
17420 GNU extension:
17422 elaborated-type-specifier:
17423 class-key attributes :: [opt] nested-name-specifier [opt] identifier
17424 class-key attributes :: [opt] nested-name-specifier [opt]
17425 template [opt] template-id
17426 enum attributes :: [opt] nested-name-specifier [opt] identifier
17428 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
17429 declared `friend'. If IS_DECLARATION is TRUE, then this
17430 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
17431 something is being declared.
17433 Returns the TYPE specified. */
17435 static tree
17436 cp_parser_elaborated_type_specifier (cp_parser* parser,
17437 bool is_friend,
17438 bool is_declaration)
17440 enum tag_types tag_type;
17441 tree identifier;
17442 tree type = NULL_TREE;
17443 tree attributes = NULL_TREE;
17444 tree globalscope;
17445 cp_token *token = NULL;
17447 /* See if we're looking at the `enum' keyword. */
17448 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
17450 /* Consume the `enum' token. */
17451 cp_lexer_consume_token (parser->lexer);
17452 /* Remember that it's an enumeration type. */
17453 tag_type = enum_type;
17454 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
17455 enums) is used here. */
17456 cp_token *token = cp_lexer_peek_token (parser->lexer);
17457 if (cp_parser_is_keyword (token, RID_CLASS)
17458 || cp_parser_is_keyword (token, RID_STRUCT))
17460 gcc_rich_location richloc (token->location);
17461 richloc.add_range (input_location, false);
17462 richloc.add_fixit_remove ();
17463 pedwarn_at_rich_loc (&richloc, 0, "elaborated-type-specifier for "
17464 "a scoped enum must not use the %qD keyword",
17465 token->u.value);
17466 /* Consume the `struct' or `class' and parse it anyway. */
17467 cp_lexer_consume_token (parser->lexer);
17469 /* Parse the attributes. */
17470 attributes = cp_parser_attributes_opt (parser);
17472 /* Or, it might be `typename'. */
17473 else if (cp_lexer_next_token_is_keyword (parser->lexer,
17474 RID_TYPENAME))
17476 /* Consume the `typename' token. */
17477 cp_lexer_consume_token (parser->lexer);
17478 /* Remember that it's a `typename' type. */
17479 tag_type = typename_type;
17481 /* Otherwise it must be a class-key. */
17482 else
17484 tag_type = cp_parser_class_key (parser);
17485 if (tag_type == none_type)
17486 return error_mark_node;
17487 /* Parse the attributes. */
17488 attributes = cp_parser_attributes_opt (parser);
17491 /* Look for the `::' operator. */
17492 globalscope = cp_parser_global_scope_opt (parser,
17493 /*current_scope_valid_p=*/false);
17494 /* Look for the nested-name-specifier. */
17495 tree nested_name_specifier;
17496 if (tag_type == typename_type && !globalscope)
17498 nested_name_specifier
17499 = cp_parser_nested_name_specifier (parser,
17500 /*typename_keyword_p=*/true,
17501 /*check_dependency_p=*/true,
17502 /*type_p=*/true,
17503 is_declaration);
17504 if (!nested_name_specifier)
17505 return error_mark_node;
17507 else
17508 /* Even though `typename' is not present, the proposed resolution
17509 to Core Issue 180 says that in `class A<T>::B', `B' should be
17510 considered a type-name, even if `A<T>' is dependent. */
17511 nested_name_specifier
17512 = cp_parser_nested_name_specifier_opt (parser,
17513 /*typename_keyword_p=*/true,
17514 /*check_dependency_p=*/true,
17515 /*type_p=*/true,
17516 is_declaration);
17517 /* For everything but enumeration types, consider a template-id.
17518 For an enumeration type, consider only a plain identifier. */
17519 if (tag_type != enum_type)
17521 bool template_p = false;
17522 tree decl;
17524 /* Allow the `template' keyword. */
17525 template_p = cp_parser_optional_template_keyword (parser);
17526 /* If we didn't see `template', we don't know if there's a
17527 template-id or not. */
17528 if (!template_p)
17529 cp_parser_parse_tentatively (parser);
17530 /* Parse the template-id. */
17531 token = cp_lexer_peek_token (parser->lexer);
17532 decl = cp_parser_template_id (parser, template_p,
17533 /*check_dependency_p=*/true,
17534 tag_type,
17535 is_declaration);
17536 /* If we didn't find a template-id, look for an ordinary
17537 identifier. */
17538 if (!template_p && !cp_parser_parse_definitely (parser))
17540 /* We can get here when cp_parser_template_id, called by
17541 cp_parser_class_name with tag_type == none_type, succeeds
17542 and caches a BASELINK. Then, when called again here,
17543 instead of failing and returning an error_mark_node
17544 returns it (see template/typename17.C in C++11).
17545 ??? Could we diagnose this earlier? */
17546 else if (tag_type == typename_type && BASELINK_P (decl))
17548 cp_parser_diagnose_invalid_type_name (parser, decl, token->location);
17549 type = error_mark_node;
17551 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
17552 in effect, then we must assume that, upon instantiation, the
17553 template will correspond to a class. */
17554 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17555 && tag_type == typename_type)
17556 type = make_typename_type (parser->scope, decl,
17557 typename_type,
17558 /*complain=*/tf_error);
17559 /* If the `typename' keyword is in effect and DECL is not a type
17560 decl, then type is non existent. */
17561 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
17563 else if (TREE_CODE (decl) == TYPE_DECL)
17565 type = check_elaborated_type_specifier (tag_type, decl,
17566 /*allow_template_p=*/true);
17568 /* If the next token is a semicolon, this must be a specialization,
17569 instantiation, or friend declaration. Check the scope while we
17570 still know whether or not we had a nested-name-specifier. */
17571 if (type != error_mark_node
17572 && !nested_name_specifier && !is_friend
17573 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17574 check_unqualified_spec_or_inst (type, token->location);
17576 else if (decl == error_mark_node)
17577 type = error_mark_node;
17580 if (!type)
17582 token = cp_lexer_peek_token (parser->lexer);
17583 identifier = cp_parser_identifier (parser);
17585 if (identifier == error_mark_node)
17587 parser->scope = NULL_TREE;
17588 return error_mark_node;
17591 /* For a `typename', we needn't call xref_tag. */
17592 if (tag_type == typename_type
17593 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
17594 return cp_parser_make_typename_type (parser, identifier,
17595 token->location);
17597 /* Template parameter lists apply only if we are not within a
17598 function parameter list. */
17599 bool template_parm_lists_apply
17600 = parser->num_template_parameter_lists;
17601 if (template_parm_lists_apply)
17602 for (cp_binding_level *s = current_binding_level;
17603 s && s->kind != sk_template_parms;
17604 s = s->level_chain)
17605 if (s->kind == sk_function_parms)
17606 template_parm_lists_apply = false;
17608 /* Look up a qualified name in the usual way. */
17609 if (parser->scope)
17611 tree decl;
17612 tree ambiguous_decls;
17614 decl = cp_parser_lookup_name (parser, identifier,
17615 tag_type,
17616 /*is_template=*/false,
17617 /*is_namespace=*/false,
17618 /*check_dependency=*/true,
17619 &ambiguous_decls,
17620 token->location);
17622 /* If the lookup was ambiguous, an error will already have been
17623 issued. */
17624 if (ambiguous_decls)
17625 return error_mark_node;
17627 /* If we are parsing friend declaration, DECL may be a
17628 TEMPLATE_DECL tree node here. However, we need to check
17629 whether this TEMPLATE_DECL results in valid code. Consider
17630 the following example:
17632 namespace N {
17633 template <class T> class C {};
17635 class X {
17636 template <class T> friend class N::C; // #1, valid code
17638 template <class T> class Y {
17639 friend class N::C; // #2, invalid code
17642 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
17643 name lookup of `N::C'. We see that friend declaration must
17644 be template for the code to be valid. Note that
17645 processing_template_decl does not work here since it is
17646 always 1 for the above two cases. */
17648 decl = (cp_parser_maybe_treat_template_as_class
17649 (decl, /*tag_name_p=*/is_friend
17650 && template_parm_lists_apply));
17652 if (TREE_CODE (decl) != TYPE_DECL)
17654 cp_parser_diagnose_invalid_type_name (parser,
17655 identifier,
17656 token->location);
17657 return error_mark_node;
17660 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
17662 bool allow_template = (template_parm_lists_apply
17663 || DECL_SELF_REFERENCE_P (decl));
17664 type = check_elaborated_type_specifier (tag_type, decl,
17665 allow_template);
17667 if (type == error_mark_node)
17668 return error_mark_node;
17671 /* Forward declarations of nested types, such as
17673 class C1::C2;
17674 class C1::C2::C3;
17676 are invalid unless all components preceding the final '::'
17677 are complete. If all enclosing types are complete, these
17678 declarations become merely pointless.
17680 Invalid forward declarations of nested types are errors
17681 caught elsewhere in parsing. Those that are pointless arrive
17682 here. */
17684 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17685 && !is_friend && !processing_explicit_instantiation)
17686 warning (0, "declaration %qD does not declare anything", decl);
17688 type = TREE_TYPE (decl);
17690 else
17692 /* An elaborated-type-specifier sometimes introduces a new type and
17693 sometimes names an existing type. Normally, the rule is that it
17694 introduces a new type only if there is not an existing type of
17695 the same name already in scope. For example, given:
17697 struct S {};
17698 void f() { struct S s; }
17700 the `struct S' in the body of `f' is the same `struct S' as in
17701 the global scope; the existing definition is used. However, if
17702 there were no global declaration, this would introduce a new
17703 local class named `S'.
17705 An exception to this rule applies to the following code:
17707 namespace N { struct S; }
17709 Here, the elaborated-type-specifier names a new type
17710 unconditionally; even if there is already an `S' in the
17711 containing scope this declaration names a new type.
17712 This exception only applies if the elaborated-type-specifier
17713 forms the complete declaration:
17715 [class.name]
17717 A declaration consisting solely of `class-key identifier ;' is
17718 either a redeclaration of the name in the current scope or a
17719 forward declaration of the identifier as a class name. It
17720 introduces the name into the current scope.
17722 We are in this situation precisely when the next token is a `;'.
17724 An exception to the exception is that a `friend' declaration does
17725 *not* name a new type; i.e., given:
17727 struct S { friend struct T; };
17729 `T' is not a new type in the scope of `S'.
17731 Also, `new struct S' or `sizeof (struct S)' never results in the
17732 definition of a new type; a new type can only be declared in a
17733 declaration context. */
17735 tag_scope ts;
17736 bool template_p;
17738 if (is_friend)
17739 /* Friends have special name lookup rules. */
17740 ts = ts_within_enclosing_non_class;
17741 else if (is_declaration
17742 && cp_lexer_next_token_is (parser->lexer,
17743 CPP_SEMICOLON))
17744 /* This is a `class-key identifier ;' */
17745 ts = ts_current;
17746 else
17747 ts = ts_global;
17749 template_p =
17750 (template_parm_lists_apply
17751 && (cp_parser_next_token_starts_class_definition_p (parser)
17752 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
17753 /* An unqualified name was used to reference this type, so
17754 there were no qualifying templates. */
17755 if (template_parm_lists_apply
17756 && !cp_parser_check_template_parameters (parser,
17757 /*num_templates=*/0,
17758 token->location,
17759 /*declarator=*/NULL))
17760 return error_mark_node;
17761 type = xref_tag (tag_type, identifier, ts, template_p);
17765 if (type == error_mark_node)
17766 return error_mark_node;
17768 /* Allow attributes on forward declarations of classes. */
17769 if (attributes)
17771 if (TREE_CODE (type) == TYPENAME_TYPE)
17772 warning (OPT_Wattributes,
17773 "attributes ignored on uninstantiated type");
17774 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
17775 && ! processing_explicit_instantiation)
17776 warning (OPT_Wattributes,
17777 "attributes ignored on template instantiation");
17778 else if (is_declaration && cp_parser_declares_only_class_p (parser))
17779 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
17780 else
17781 warning (OPT_Wattributes,
17782 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
17785 if (tag_type != enum_type)
17787 /* Indicate whether this class was declared as a `class' or as a
17788 `struct'. */
17789 if (CLASS_TYPE_P (type))
17790 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
17791 cp_parser_check_class_key (tag_type, type);
17794 /* A "<" cannot follow an elaborated type specifier. If that
17795 happens, the user was probably trying to form a template-id. */
17796 cp_parser_check_for_invalid_template_id (parser, type, tag_type,
17797 token->location);
17799 return type;
17802 /* Parse an enum-specifier.
17804 enum-specifier:
17805 enum-head { enumerator-list [opt] }
17806 enum-head { enumerator-list , } [C++0x]
17808 enum-head:
17809 enum-key identifier [opt] enum-base [opt]
17810 enum-key nested-name-specifier identifier enum-base [opt]
17812 enum-key:
17813 enum
17814 enum class [C++0x]
17815 enum struct [C++0x]
17817 enum-base: [C++0x]
17818 : type-specifier-seq
17820 opaque-enum-specifier:
17821 enum-key identifier enum-base [opt] ;
17823 GNU Extensions:
17824 enum-key attributes[opt] identifier [opt] enum-base [opt]
17825 { enumerator-list [opt] }attributes[opt]
17826 enum-key attributes[opt] identifier [opt] enum-base [opt]
17827 { enumerator-list, }attributes[opt] [C++0x]
17829 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
17830 if the token stream isn't an enum-specifier after all. */
17832 static tree
17833 cp_parser_enum_specifier (cp_parser* parser)
17835 tree identifier;
17836 tree type = NULL_TREE;
17837 tree prev_scope;
17838 tree nested_name_specifier = NULL_TREE;
17839 tree attributes;
17840 bool scoped_enum_p = false;
17841 bool has_underlying_type = false;
17842 bool nested_being_defined = false;
17843 bool new_value_list = false;
17844 bool is_new_type = false;
17845 bool is_unnamed = false;
17846 tree underlying_type = NULL_TREE;
17847 cp_token *type_start_token = NULL;
17848 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17850 parser->colon_corrects_to_scope_p = false;
17852 /* Parse tentatively so that we can back up if we don't find a
17853 enum-specifier. */
17854 cp_parser_parse_tentatively (parser);
17856 /* Caller guarantees that the current token is 'enum', an identifier
17857 possibly follows, and the token after that is an opening brace.
17858 If we don't have an identifier, fabricate an anonymous name for
17859 the enumeration being defined. */
17860 cp_lexer_consume_token (parser->lexer);
17862 /* Parse the "class" or "struct", which indicates a scoped
17863 enumeration type in C++0x. */
17864 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
17865 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
17867 if (cxx_dialect < cxx11)
17868 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17870 /* Consume the `struct' or `class' token. */
17871 cp_lexer_consume_token (parser->lexer);
17873 scoped_enum_p = true;
17876 attributes = cp_parser_attributes_opt (parser);
17878 /* Clear the qualification. */
17879 parser->scope = NULL_TREE;
17880 parser->qualifying_scope = NULL_TREE;
17881 parser->object_scope = NULL_TREE;
17883 /* Figure out in what scope the declaration is being placed. */
17884 prev_scope = current_scope ();
17886 type_start_token = cp_lexer_peek_token (parser->lexer);
17888 push_deferring_access_checks (dk_no_check);
17889 nested_name_specifier
17890 = cp_parser_nested_name_specifier_opt (parser,
17891 /*typename_keyword_p=*/true,
17892 /*check_dependency_p=*/false,
17893 /*type_p=*/false,
17894 /*is_declaration=*/false);
17896 if (nested_name_specifier)
17898 tree name;
17900 identifier = cp_parser_identifier (parser);
17901 name = cp_parser_lookup_name (parser, identifier,
17902 enum_type,
17903 /*is_template=*/false,
17904 /*is_namespace=*/false,
17905 /*check_dependency=*/true,
17906 /*ambiguous_decls=*/NULL,
17907 input_location);
17908 if (name && name != error_mark_node)
17910 type = TREE_TYPE (name);
17911 if (TREE_CODE (type) == TYPENAME_TYPE)
17913 /* Are template enums allowed in ISO? */
17914 if (template_parm_scope_p ())
17915 pedwarn (type_start_token->location, OPT_Wpedantic,
17916 "%qD is an enumeration template", name);
17917 /* ignore a typename reference, for it will be solved by name
17918 in start_enum. */
17919 type = NULL_TREE;
17922 else if (nested_name_specifier == error_mark_node)
17923 /* We already issued an error. */;
17924 else
17926 error_at (type_start_token->location,
17927 "%qD does not name an enumeration in %qT",
17928 identifier, nested_name_specifier);
17929 nested_name_specifier = error_mark_node;
17932 else
17934 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17935 identifier = cp_parser_identifier (parser);
17936 else
17938 identifier = make_anon_name ();
17939 is_unnamed = true;
17940 if (scoped_enum_p)
17941 error_at (type_start_token->location,
17942 "unnamed scoped enum is not allowed");
17945 pop_deferring_access_checks ();
17947 /* Check for the `:' that denotes a specified underlying type in C++0x.
17948 Note that a ':' could also indicate a bitfield width, however. */
17949 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17951 cp_decl_specifier_seq type_specifiers;
17953 /* Consume the `:'. */
17954 cp_lexer_consume_token (parser->lexer);
17956 /* Parse the type-specifier-seq. */
17957 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
17958 /*is_trailing_return=*/false,
17959 &type_specifiers);
17961 /* At this point this is surely not elaborated type specifier. */
17962 if (!cp_parser_parse_definitely (parser))
17963 return NULL_TREE;
17965 if (cxx_dialect < cxx11)
17966 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
17968 has_underlying_type = true;
17970 /* If that didn't work, stop. */
17971 if (type_specifiers.type != error_mark_node)
17973 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
17974 /*initialized=*/0, NULL);
17975 if (underlying_type == error_mark_node
17976 || check_for_bare_parameter_packs (underlying_type))
17977 underlying_type = NULL_TREE;
17981 /* Look for the `{' but don't consume it yet. */
17982 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17984 if (cxx_dialect < cxx11 || (!scoped_enum_p && !underlying_type))
17986 cp_parser_error (parser, "expected %<{%>");
17987 if (has_underlying_type)
17989 type = NULL_TREE;
17990 goto out;
17993 /* An opaque-enum-specifier must have a ';' here. */
17994 if ((scoped_enum_p || underlying_type)
17995 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17997 cp_parser_error (parser, "expected %<;%> or %<{%>");
17998 if (has_underlying_type)
18000 type = NULL_TREE;
18001 goto out;
18006 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
18007 return NULL_TREE;
18009 if (nested_name_specifier)
18011 if (CLASS_TYPE_P (nested_name_specifier))
18013 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
18014 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
18015 push_scope (nested_name_specifier);
18017 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18019 push_nested_namespace (nested_name_specifier);
18023 /* Issue an error message if type-definitions are forbidden here. */
18024 if (!cp_parser_check_type_definition (parser))
18025 type = error_mark_node;
18026 else
18027 /* Create the new type. We do this before consuming the opening
18028 brace so the enum will be recorded as being on the line of its
18029 tag (or the 'enum' keyword, if there is no tag). */
18030 type = start_enum (identifier, type, underlying_type,
18031 attributes, scoped_enum_p, &is_new_type);
18033 /* If the next token is not '{' it is an opaque-enum-specifier or an
18034 elaborated-type-specifier. */
18035 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18037 timevar_push (TV_PARSE_ENUM);
18038 if (nested_name_specifier
18039 && nested_name_specifier != error_mark_node)
18041 /* The following catches invalid code such as:
18042 enum class S<int>::E { A, B, C }; */
18043 if (!processing_specialization
18044 && CLASS_TYPE_P (nested_name_specifier)
18045 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
18046 error_at (type_start_token->location, "cannot add an enumerator "
18047 "list to a template instantiation");
18049 if (TREE_CODE (nested_name_specifier) == TYPENAME_TYPE)
18051 error_at (type_start_token->location,
18052 "%<%T::%E%> has not been declared",
18053 TYPE_CONTEXT (nested_name_specifier),
18054 nested_name_specifier);
18055 type = error_mark_node;
18057 else if (TREE_CODE (nested_name_specifier) != NAMESPACE_DECL
18058 && !CLASS_TYPE_P (nested_name_specifier))
18060 error_at (type_start_token->location, "nested name specifier "
18061 "%qT for enum declaration does not name a class "
18062 "or namespace", nested_name_specifier);
18063 type = error_mark_node;
18065 /* If that scope does not contain the scope in which the
18066 class was originally declared, the program is invalid. */
18067 else if (prev_scope && !is_ancestor (prev_scope,
18068 nested_name_specifier))
18070 if (at_namespace_scope_p ())
18071 error_at (type_start_token->location,
18072 "declaration of %qD in namespace %qD which does not "
18073 "enclose %qD",
18074 type, prev_scope, nested_name_specifier);
18075 else
18076 error_at (type_start_token->location,
18077 "declaration of %qD in %qD which does not "
18078 "enclose %qD",
18079 type, prev_scope, nested_name_specifier);
18080 type = error_mark_node;
18082 /* If that scope is the scope where the declaration is being placed
18083 the program is invalid. */
18084 else if (CLASS_TYPE_P (nested_name_specifier)
18085 && CLASS_TYPE_P (prev_scope)
18086 && same_type_p (nested_name_specifier, prev_scope))
18088 permerror (type_start_token->location,
18089 "extra qualification not allowed");
18090 nested_name_specifier = NULL_TREE;
18094 if (scoped_enum_p)
18095 begin_scope (sk_scoped_enum, type);
18097 /* Consume the opening brace. */
18098 cp_lexer_consume_token (parser->lexer);
18100 if (type == error_mark_node)
18101 ; /* Nothing to add */
18102 else if (OPAQUE_ENUM_P (type)
18103 || (cxx_dialect > cxx98 && processing_specialization))
18105 new_value_list = true;
18106 SET_OPAQUE_ENUM_P (type, false);
18107 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18109 else
18111 error_at (type_start_token->location,
18112 "multiple definition of %q#T", type);
18113 inform (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
18114 "previous definition here");
18115 type = error_mark_node;
18118 if (type == error_mark_node)
18119 cp_parser_skip_to_end_of_block_or_statement (parser);
18120 /* If the next token is not '}', then there are some enumerators. */
18121 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18123 if (is_unnamed && !scoped_enum_p)
18124 pedwarn (type_start_token->location, OPT_Wpedantic,
18125 "ISO C++ forbids empty unnamed enum");
18127 else
18128 cp_parser_enumerator_list (parser, type);
18130 /* Consume the final '}'. */
18131 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18133 if (scoped_enum_p)
18134 finish_scope ();
18135 timevar_pop (TV_PARSE_ENUM);
18137 else
18139 /* If a ';' follows, then it is an opaque-enum-specifier
18140 and additional restrictions apply. */
18141 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18143 if (is_unnamed)
18144 error_at (type_start_token->location,
18145 "opaque-enum-specifier without name");
18146 else if (nested_name_specifier)
18147 error_at (type_start_token->location,
18148 "opaque-enum-specifier must use a simple identifier");
18152 /* Look for trailing attributes to apply to this enumeration, and
18153 apply them if appropriate. */
18154 if (cp_parser_allow_gnu_extensions_p (parser))
18156 tree trailing_attr = cp_parser_gnu_attributes_opt (parser);
18157 cplus_decl_attributes (&type,
18158 trailing_attr,
18159 (int) ATTR_FLAG_TYPE_IN_PLACE);
18162 /* Finish up the enumeration. */
18163 if (type != error_mark_node)
18165 if (new_value_list)
18166 finish_enum_value_list (type);
18167 if (is_new_type)
18168 finish_enum (type);
18171 if (nested_name_specifier)
18173 if (CLASS_TYPE_P (nested_name_specifier))
18175 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
18176 pop_scope (nested_name_specifier);
18178 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
18180 pop_nested_namespace (nested_name_specifier);
18183 out:
18184 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18185 return type;
18188 /* Parse an enumerator-list. The enumerators all have the indicated
18189 TYPE.
18191 enumerator-list:
18192 enumerator-definition
18193 enumerator-list , enumerator-definition */
18195 static void
18196 cp_parser_enumerator_list (cp_parser* parser, tree type)
18198 while (true)
18200 /* Parse an enumerator-definition. */
18201 cp_parser_enumerator_definition (parser, type);
18203 /* If the next token is not a ',', we've reached the end of
18204 the list. */
18205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18206 break;
18207 /* Otherwise, consume the `,' and keep going. */
18208 cp_lexer_consume_token (parser->lexer);
18209 /* If the next token is a `}', there is a trailing comma. */
18210 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18212 if (cxx_dialect < cxx11 && !in_system_header_at (input_location))
18213 pedwarn (input_location, OPT_Wpedantic,
18214 "comma at end of enumerator list");
18215 break;
18220 /* Parse an enumerator-definition. The enumerator has the indicated
18221 TYPE.
18223 enumerator-definition:
18224 enumerator
18225 enumerator = constant-expression
18227 enumerator:
18228 identifier
18230 GNU Extensions:
18232 enumerator-definition:
18233 enumerator attributes [opt]
18234 enumerator attributes [opt] = constant-expression */
18236 static void
18237 cp_parser_enumerator_definition (cp_parser* parser, tree type)
18239 tree identifier;
18240 tree value;
18241 location_t loc;
18243 /* Save the input location because we are interested in the location
18244 of the identifier and not the location of the explicit value. */
18245 loc = cp_lexer_peek_token (parser->lexer)->location;
18247 /* Look for the identifier. */
18248 identifier = cp_parser_identifier (parser);
18249 if (identifier == error_mark_node)
18250 return;
18252 /* Parse any specified attributes. */
18253 tree attrs = cp_parser_attributes_opt (parser);
18255 /* If the next token is an '=', then there is an explicit value. */
18256 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18258 /* Consume the `=' token. */
18259 cp_lexer_consume_token (parser->lexer);
18260 /* Parse the value. */
18261 value = cp_parser_constant_expression (parser);
18263 else
18264 value = NULL_TREE;
18266 /* If we are processing a template, make sure the initializer of the
18267 enumerator doesn't contain any bare template parameter pack. */
18268 if (check_for_bare_parameter_packs (value))
18269 value = error_mark_node;
18271 /* Create the enumerator. */
18272 build_enumerator (identifier, value, type, attrs, loc);
18275 /* Parse a namespace-name.
18277 namespace-name:
18278 original-namespace-name
18279 namespace-alias
18281 Returns the NAMESPACE_DECL for the namespace. */
18283 static tree
18284 cp_parser_namespace_name (cp_parser* parser)
18286 tree identifier;
18287 tree namespace_decl;
18289 cp_token *token = cp_lexer_peek_token (parser->lexer);
18291 /* Get the name of the namespace. */
18292 identifier = cp_parser_identifier (parser);
18293 if (identifier == error_mark_node)
18294 return error_mark_node;
18296 /* Look up the identifier in the currently active scope. Look only
18297 for namespaces, due to:
18299 [basic.lookup.udir]
18301 When looking up a namespace-name in a using-directive or alias
18302 definition, only namespace names are considered.
18304 And:
18306 [basic.lookup.qual]
18308 During the lookup of a name preceding the :: scope resolution
18309 operator, object, function, and enumerator names are ignored.
18311 (Note that cp_parser_qualifying_entity only calls this
18312 function if the token after the name is the scope resolution
18313 operator.) */
18314 namespace_decl = cp_parser_lookup_name (parser, identifier,
18315 none_type,
18316 /*is_template=*/false,
18317 /*is_namespace=*/true,
18318 /*check_dependency=*/true,
18319 /*ambiguous_decls=*/NULL,
18320 token->location);
18321 /* If it's not a namespace, issue an error. */
18322 if (namespace_decl == error_mark_node
18323 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
18325 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
18326 error_at (token->location, "%qD is not a namespace-name", identifier);
18327 cp_parser_error (parser, "expected namespace-name");
18328 namespace_decl = error_mark_node;
18331 return namespace_decl;
18334 /* Parse a namespace-definition.
18336 namespace-definition:
18337 named-namespace-definition
18338 unnamed-namespace-definition
18340 named-namespace-definition:
18341 original-namespace-definition
18342 extension-namespace-definition
18344 original-namespace-definition:
18345 namespace identifier { namespace-body }
18347 extension-namespace-definition:
18348 namespace original-namespace-name { namespace-body }
18350 unnamed-namespace-definition:
18351 namespace { namespace-body } */
18353 static void
18354 cp_parser_namespace_definition (cp_parser* parser)
18356 tree identifier;
18357 int nested_definition_count = 0;
18359 cp_ensure_no_omp_declare_simd (parser);
18360 cp_ensure_no_oacc_routine (parser);
18362 bool is_inline = cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE);
18364 if (is_inline)
18366 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
18367 cp_lexer_consume_token (parser->lexer);
18370 /* Look for the `namespace' keyword. */
18371 cp_token* token
18372 = cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18374 /* Parse any specified attributes before the identifier. */
18375 tree attribs = cp_parser_attributes_opt (parser);
18377 for (;;)
18379 identifier = NULL_TREE;
18381 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18383 identifier = cp_parser_identifier (parser);
18385 /* Parse any attributes specified after the identifier. */
18386 attribs = chainon (attribs, cp_parser_attributes_opt (parser));
18389 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18390 break;
18392 if (!nested_definition_count && cxx_dialect < cxx1z)
18393 pedwarn (input_location, OPT_Wpedantic,
18394 "nested namespace definitions only available with "
18395 "-std=c++1z or -std=gnu++1z");
18397 /* Nested namespace names can create new namespaces (unlike
18398 other qualified-ids). */
18399 if (int count = identifier ? push_namespace (identifier) : 0)
18400 nested_definition_count += count;
18401 else
18402 cp_parser_error (parser, "nested namespace name required");
18403 cp_lexer_consume_token (parser->lexer);
18406 if (nested_definition_count && !identifier)
18407 cp_parser_error (parser, "namespace name required");
18409 if (nested_definition_count && attribs)
18410 error_at (token->location,
18411 "a nested namespace definition cannot have attributes");
18412 if (nested_definition_count && is_inline)
18413 error_at (token->location,
18414 "a nested namespace definition cannot be inline");
18416 /* Start the namespace. */
18417 nested_definition_count += push_namespace (identifier, is_inline);
18419 bool has_visibility = handle_namespace_attrs (current_namespace, attribs);
18421 warning (OPT_Wnamespaces, "namespace %qD entered", current_namespace);
18423 /* Look for the `{' to validate starting the namespace. */
18424 if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18426 /* Parse the body of the namespace. */
18427 cp_parser_namespace_body (parser);
18429 /* Look for the final `}'. */
18430 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18433 if (has_visibility)
18434 pop_visibility (1);
18436 /* Pop the nested namespace definitions. */
18437 while (nested_definition_count--)
18438 pop_namespace ();
18441 /* Parse a namespace-body.
18443 namespace-body:
18444 declaration-seq [opt] */
18446 static void
18447 cp_parser_namespace_body (cp_parser* parser)
18449 cp_parser_declaration_seq_opt (parser);
18452 /* Parse a namespace-alias-definition.
18454 namespace-alias-definition:
18455 namespace identifier = qualified-namespace-specifier ; */
18457 static void
18458 cp_parser_namespace_alias_definition (cp_parser* parser)
18460 tree identifier;
18461 tree namespace_specifier;
18463 cp_token *token = cp_lexer_peek_token (parser->lexer);
18465 /* Look for the `namespace' keyword. */
18466 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18467 /* Look for the identifier. */
18468 identifier = cp_parser_identifier (parser);
18469 if (identifier == error_mark_node)
18470 return;
18471 /* Look for the `=' token. */
18472 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
18473 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18475 error_at (token->location, "%<namespace%> definition is not allowed here");
18476 /* Skip the definition. */
18477 cp_lexer_consume_token (parser->lexer);
18478 if (cp_parser_skip_to_closing_brace (parser))
18479 cp_lexer_consume_token (parser->lexer);
18480 return;
18482 cp_parser_require (parser, CPP_EQ, RT_EQ);
18483 /* Look for the qualified-namespace-specifier. */
18484 namespace_specifier
18485 = cp_parser_qualified_namespace_specifier (parser);
18486 /* Look for the `;' token. */
18487 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18489 /* Register the alias in the symbol table. */
18490 do_namespace_alias (identifier, namespace_specifier);
18493 /* Parse a qualified-namespace-specifier.
18495 qualified-namespace-specifier:
18496 :: [opt] nested-name-specifier [opt] namespace-name
18498 Returns a NAMESPACE_DECL corresponding to the specified
18499 namespace. */
18501 static tree
18502 cp_parser_qualified_namespace_specifier (cp_parser* parser)
18504 /* Look for the optional `::'. */
18505 cp_parser_global_scope_opt (parser,
18506 /*current_scope_valid_p=*/false);
18508 /* Look for the optional nested-name-specifier. */
18509 cp_parser_nested_name_specifier_opt (parser,
18510 /*typename_keyword_p=*/false,
18511 /*check_dependency_p=*/true,
18512 /*type_p=*/false,
18513 /*is_declaration=*/true);
18515 return cp_parser_namespace_name (parser);
18518 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
18519 access declaration.
18521 using-declaration:
18522 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
18523 using :: unqualified-id ;
18525 access-declaration:
18526 qualified-id ;
18530 static bool
18531 cp_parser_using_declaration (cp_parser* parser,
18532 bool access_declaration_p)
18534 cp_token *token;
18535 bool typename_p = false;
18536 bool global_scope_p;
18537 tree decl;
18538 tree identifier;
18539 tree qscope;
18540 int oldcount = errorcount;
18541 cp_token *diag_token = NULL;
18543 if (access_declaration_p)
18545 diag_token = cp_lexer_peek_token (parser->lexer);
18546 cp_parser_parse_tentatively (parser);
18548 else
18550 /* Look for the `using' keyword. */
18551 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18553 again:
18554 /* Peek at the next token. */
18555 token = cp_lexer_peek_token (parser->lexer);
18556 /* See if it's `typename'. */
18557 if (token->keyword == RID_TYPENAME)
18559 /* Remember that we've seen it. */
18560 typename_p = true;
18561 /* Consume the `typename' token. */
18562 cp_lexer_consume_token (parser->lexer);
18566 /* Look for the optional global scope qualification. */
18567 global_scope_p
18568 = (cp_parser_global_scope_opt (parser,
18569 /*current_scope_valid_p=*/false)
18570 != NULL_TREE);
18572 /* If we saw `typename', or didn't see `::', then there must be a
18573 nested-name-specifier present. */
18574 if (typename_p || !global_scope_p)
18576 qscope = cp_parser_nested_name_specifier (parser, typename_p,
18577 /*check_dependency_p=*/true,
18578 /*type_p=*/false,
18579 /*is_declaration=*/true);
18580 if (!qscope && !cp_parser_uncommitted_to_tentative_parse_p (parser))
18582 cp_parser_skip_to_end_of_block_or_statement (parser);
18583 return false;
18586 /* Otherwise, we could be in either of the two productions. In that
18587 case, treat the nested-name-specifier as optional. */
18588 else
18589 qscope = cp_parser_nested_name_specifier_opt (parser,
18590 /*typename_keyword_p=*/false,
18591 /*check_dependency_p=*/true,
18592 /*type_p=*/false,
18593 /*is_declaration=*/true);
18594 if (!qscope)
18595 qscope = global_namespace;
18596 else if (UNSCOPED_ENUM_P (qscope))
18597 qscope = CP_TYPE_CONTEXT (qscope);
18599 if (access_declaration_p && cp_parser_error_occurred (parser))
18600 /* Something has already gone wrong; there's no need to parse
18601 further. Since an error has occurred, the return value of
18602 cp_parser_parse_definitely will be false, as required. */
18603 return cp_parser_parse_definitely (parser);
18605 token = cp_lexer_peek_token (parser->lexer);
18606 /* Parse the unqualified-id. */
18607 identifier = cp_parser_unqualified_id (parser,
18608 /*template_keyword_p=*/false,
18609 /*check_dependency_p=*/true,
18610 /*declarator_p=*/true,
18611 /*optional_p=*/false);
18613 if (access_declaration_p)
18615 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18616 cp_parser_simulate_error (parser);
18617 if (!cp_parser_parse_definitely (parser))
18618 return false;
18620 else if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18622 cp_token *ell = cp_lexer_consume_token (parser->lexer);
18623 if (cxx_dialect < cxx1z
18624 && !in_system_header_at (ell->location))
18625 pedwarn (ell->location, 0,
18626 "pack expansion in using-declaration only available "
18627 "with -std=c++1z or -std=gnu++1z");
18628 qscope = make_pack_expansion (qscope);
18631 /* The function we call to handle a using-declaration is different
18632 depending on what scope we are in. */
18633 if (qscope == error_mark_node || identifier == error_mark_node)
18635 else if (!identifier_p (identifier)
18636 && TREE_CODE (identifier) != BIT_NOT_EXPR)
18637 /* [namespace.udecl]
18639 A using declaration shall not name a template-id. */
18640 error_at (token->location,
18641 "a template-id may not appear in a using-declaration");
18642 else
18644 if (at_class_scope_p ())
18646 /* Create the USING_DECL. */
18647 decl = do_class_using_decl (qscope, identifier);
18649 if (decl && typename_p)
18650 USING_DECL_TYPENAME_P (decl) = 1;
18652 if (check_for_bare_parameter_packs (decl))
18654 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18655 return false;
18657 else
18658 /* Add it to the list of members in this class. */
18659 finish_member_declaration (decl);
18661 else
18663 decl = cp_parser_lookup_name_simple (parser,
18664 identifier,
18665 token->location);
18666 if (decl == error_mark_node)
18667 cp_parser_name_lookup_error (parser, identifier,
18668 decl, NLE_NULL,
18669 token->location);
18670 else if (check_for_bare_parameter_packs (decl))
18672 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18673 return false;
18675 else if (!at_namespace_scope_p ())
18676 finish_local_using_decl (decl, qscope, identifier);
18677 else
18678 finish_namespace_using_decl (decl, qscope, identifier);
18682 if (!access_declaration_p
18683 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18685 cp_token *comma = cp_lexer_consume_token (parser->lexer);
18686 if (cxx_dialect < cxx1z)
18687 pedwarn (comma->location, 0,
18688 "comma-separated list in using-declaration only available "
18689 "with -std=c++1z or -std=gnu++1z");
18690 goto again;
18693 /* Look for the final `;'. */
18694 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18696 if (access_declaration_p && errorcount == oldcount)
18697 warning_at (diag_token->location, OPT_Wdeprecated,
18698 "access declarations are deprecated "
18699 "in favour of using-declarations; "
18700 "suggestion: add the %<using%> keyword");
18702 return true;
18705 /* Parse an alias-declaration.
18707 alias-declaration:
18708 using identifier attribute-specifier-seq [opt] = type-id */
18710 static tree
18711 cp_parser_alias_declaration (cp_parser* parser)
18713 tree id, type, decl, pushed_scope = NULL_TREE, attributes;
18714 location_t id_location;
18715 cp_declarator *declarator;
18716 cp_decl_specifier_seq decl_specs;
18717 bool member_p;
18718 const char *saved_message = NULL;
18720 /* Look for the `using' keyword. */
18721 cp_token *using_token
18722 = cp_parser_require_keyword (parser, RID_USING, RT_USING);
18723 if (using_token == NULL)
18724 return error_mark_node;
18726 id_location = cp_lexer_peek_token (parser->lexer)->location;
18727 id = cp_parser_identifier (parser);
18728 if (id == error_mark_node)
18729 return error_mark_node;
18731 cp_token *attrs_token = cp_lexer_peek_token (parser->lexer);
18732 attributes = cp_parser_attributes_opt (parser);
18733 if (attributes == error_mark_node)
18734 return error_mark_node;
18736 cp_parser_require (parser, CPP_EQ, RT_EQ);
18738 if (cp_parser_error_occurred (parser))
18739 return error_mark_node;
18741 cp_parser_commit_to_tentative_parse (parser);
18743 /* Now we are going to parse the type-id of the declaration. */
18746 [dcl.type]/3 says:
18748 "A type-specifier-seq shall not define a class or enumeration
18749 unless it appears in the type-id of an alias-declaration (7.1.3) that
18750 is not the declaration of a template-declaration."
18752 In other words, if we currently are in an alias template, the
18753 type-id should not define a type.
18755 So let's set parser->type_definition_forbidden_message in that
18756 case; cp_parser_check_type_definition (called by
18757 cp_parser_class_specifier) will then emit an error if a type is
18758 defined in the type-id. */
18759 if (parser->num_template_parameter_lists)
18761 saved_message = parser->type_definition_forbidden_message;
18762 parser->type_definition_forbidden_message =
18763 G_("types may not be defined in alias template declarations");
18766 type = cp_parser_type_id (parser);
18768 /* Restore the error message if need be. */
18769 if (parser->num_template_parameter_lists)
18770 parser->type_definition_forbidden_message = saved_message;
18772 if (type == error_mark_node
18773 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
18775 cp_parser_skip_to_end_of_block_or_statement (parser);
18776 return error_mark_node;
18779 /* A typedef-name can also be introduced by an alias-declaration. The
18780 identifier following the using keyword becomes a typedef-name. It has
18781 the same semantics as if it were introduced by the typedef
18782 specifier. In particular, it does not define a new type and it shall
18783 not appear in the type-id. */
18785 clear_decl_specs (&decl_specs);
18786 decl_specs.type = type;
18787 if (attributes != NULL_TREE)
18789 decl_specs.attributes = attributes;
18790 set_and_check_decl_spec_loc (&decl_specs,
18791 ds_attribute,
18792 attrs_token);
18794 set_and_check_decl_spec_loc (&decl_specs,
18795 ds_typedef,
18796 using_token);
18797 set_and_check_decl_spec_loc (&decl_specs,
18798 ds_alias,
18799 using_token);
18801 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
18802 declarator->id_loc = id_location;
18804 member_p = at_class_scope_p ();
18805 if (member_p)
18806 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
18807 NULL_TREE, attributes);
18808 else
18809 decl = start_decl (declarator, &decl_specs, 0,
18810 attributes, NULL_TREE, &pushed_scope);
18811 if (decl == error_mark_node)
18812 return decl;
18814 // Attach constraints to the alias declaration.
18815 if (flag_concepts && current_template_parms)
18817 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
18818 tree constr = build_constraints (reqs, NULL_TREE);
18819 set_constraints (decl, constr);
18822 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
18824 if (pushed_scope)
18825 pop_scope (pushed_scope);
18827 /* If decl is a template, return its TEMPLATE_DECL so that it gets
18828 added into the symbol table; otherwise, return the TYPE_DECL. */
18829 if (DECL_LANG_SPECIFIC (decl)
18830 && DECL_TEMPLATE_INFO (decl)
18831 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
18833 decl = DECL_TI_TEMPLATE (decl);
18834 if (member_p)
18835 check_member_template (decl);
18838 return decl;
18841 /* Parse a using-directive.
18843 using-directive:
18844 using namespace :: [opt] nested-name-specifier [opt]
18845 namespace-name ; */
18847 static void
18848 cp_parser_using_directive (cp_parser* parser)
18850 tree namespace_decl;
18851 tree attribs;
18853 /* Look for the `using' keyword. */
18854 cp_parser_require_keyword (parser, RID_USING, RT_USING);
18855 /* And the `namespace' keyword. */
18856 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
18857 /* Look for the optional `::' operator. */
18858 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18859 /* And the optional nested-name-specifier. */
18860 cp_parser_nested_name_specifier_opt (parser,
18861 /*typename_keyword_p=*/false,
18862 /*check_dependency_p=*/true,
18863 /*type_p=*/false,
18864 /*is_declaration=*/true);
18865 /* Get the namespace being used. */
18866 namespace_decl = cp_parser_namespace_name (parser);
18867 /* And any specified attributes. */
18868 attribs = cp_parser_attributes_opt (parser);
18870 /* Update the symbol table. */
18871 if (namespace_bindings_p ())
18872 finish_namespace_using_directive (namespace_decl, attribs);
18873 else
18874 finish_local_using_directive (namespace_decl, attribs);
18876 /* Look for the final `;'. */
18877 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18880 /* Parse an asm-definition.
18882 asm-definition:
18883 asm ( string-literal ) ;
18885 GNU Extension:
18887 asm-definition:
18888 asm volatile [opt] ( string-literal ) ;
18889 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
18890 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18891 : asm-operand-list [opt] ) ;
18892 asm volatile [opt] ( string-literal : asm-operand-list [opt]
18893 : asm-operand-list [opt]
18894 : asm-clobber-list [opt] ) ;
18895 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
18896 : asm-clobber-list [opt]
18897 : asm-goto-list ) ; */
18899 static void
18900 cp_parser_asm_definition (cp_parser* parser)
18902 tree string;
18903 tree outputs = NULL_TREE;
18904 tree inputs = NULL_TREE;
18905 tree clobbers = NULL_TREE;
18906 tree labels = NULL_TREE;
18907 tree asm_stmt;
18908 bool volatile_p = false;
18909 bool extended_p = false;
18910 bool invalid_inputs_p = false;
18911 bool invalid_outputs_p = false;
18912 bool goto_p = false;
18913 required_token missing = RT_NONE;
18915 /* Look for the `asm' keyword. */
18916 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
18918 if (parser->in_function_body
18919 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
18921 error ("%<asm%> in %<constexpr%> function");
18922 cp_function_chain->invalid_constexpr = true;
18925 /* See if the next token is `volatile'. */
18926 if (cp_parser_allow_gnu_extensions_p (parser)
18927 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
18929 /* Remember that we saw the `volatile' keyword. */
18930 volatile_p = true;
18931 /* Consume the token. */
18932 cp_lexer_consume_token (parser->lexer);
18934 if (cp_parser_allow_gnu_extensions_p (parser)
18935 && parser->in_function_body
18936 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
18938 /* Remember that we saw the `goto' keyword. */
18939 goto_p = true;
18940 /* Consume the token. */
18941 cp_lexer_consume_token (parser->lexer);
18943 /* Look for the opening `('. */
18944 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
18945 return;
18946 /* Look for the string. */
18947 string = cp_parser_string_literal (parser, false, false);
18948 if (string == error_mark_node)
18950 cp_parser_skip_to_closing_parenthesis (parser, true, false,
18951 /*consume_paren=*/true);
18952 return;
18955 /* If we're allowing GNU extensions, check for the extended assembly
18956 syntax. Unfortunately, the `:' tokens need not be separated by
18957 a space in C, and so, for compatibility, we tolerate that here
18958 too. Doing that means that we have to treat the `::' operator as
18959 two `:' tokens. */
18960 if (cp_parser_allow_gnu_extensions_p (parser)
18961 && parser->in_function_body
18962 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
18963 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
18965 bool inputs_p = false;
18966 bool clobbers_p = false;
18967 bool labels_p = false;
18969 /* The extended syntax was used. */
18970 extended_p = true;
18972 /* Look for outputs. */
18973 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18975 /* Consume the `:'. */
18976 cp_lexer_consume_token (parser->lexer);
18977 /* Parse the output-operands. */
18978 if (cp_lexer_next_token_is_not (parser->lexer,
18979 CPP_COLON)
18980 && cp_lexer_next_token_is_not (parser->lexer,
18981 CPP_SCOPE)
18982 && cp_lexer_next_token_is_not (parser->lexer,
18983 CPP_CLOSE_PAREN)
18984 && !goto_p)
18986 outputs = cp_parser_asm_operand_list (parser);
18987 if (outputs == error_mark_node)
18988 invalid_outputs_p = true;
18991 /* If the next token is `::', there are no outputs, and the
18992 next token is the beginning of the inputs. */
18993 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
18994 /* The inputs are coming next. */
18995 inputs_p = true;
18997 /* Look for inputs. */
18998 if (inputs_p
18999 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19001 /* Consume the `:' or `::'. */
19002 cp_lexer_consume_token (parser->lexer);
19003 /* Parse the output-operands. */
19004 if (cp_lexer_next_token_is_not (parser->lexer,
19005 CPP_COLON)
19006 && cp_lexer_next_token_is_not (parser->lexer,
19007 CPP_SCOPE)
19008 && cp_lexer_next_token_is_not (parser->lexer,
19009 CPP_CLOSE_PAREN))
19011 inputs = cp_parser_asm_operand_list (parser);
19012 if (inputs == error_mark_node)
19013 invalid_inputs_p = true;
19016 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19017 /* The clobbers are coming next. */
19018 clobbers_p = true;
19020 /* Look for clobbers. */
19021 if (clobbers_p
19022 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19024 clobbers_p = true;
19025 /* Consume the `:' or `::'. */
19026 cp_lexer_consume_token (parser->lexer);
19027 /* Parse the clobbers. */
19028 if (cp_lexer_next_token_is_not (parser->lexer,
19029 CPP_COLON)
19030 && cp_lexer_next_token_is_not (parser->lexer,
19031 CPP_CLOSE_PAREN))
19032 clobbers = cp_parser_asm_clobber_list (parser);
19034 else if (goto_p
19035 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
19036 /* The labels are coming next. */
19037 labels_p = true;
19039 /* Look for labels. */
19040 if (labels_p
19041 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
19043 labels_p = true;
19044 /* Consume the `:' or `::'. */
19045 cp_lexer_consume_token (parser->lexer);
19046 /* Parse the labels. */
19047 labels = cp_parser_asm_label_list (parser);
19050 if (goto_p && !labels_p)
19051 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
19053 else if (goto_p)
19054 missing = RT_COLON_SCOPE;
19056 /* Look for the closing `)'. */
19057 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
19058 missing ? missing : RT_CLOSE_PAREN))
19059 cp_parser_skip_to_closing_parenthesis (parser, true, false,
19060 /*consume_paren=*/true);
19061 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19063 if (!invalid_inputs_p && !invalid_outputs_p)
19065 /* Create the ASM_EXPR. */
19066 if (parser->in_function_body)
19068 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
19069 inputs, clobbers, labels);
19070 /* If the extended syntax was not used, mark the ASM_EXPR. */
19071 if (!extended_p)
19073 tree temp = asm_stmt;
19074 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
19075 temp = TREE_OPERAND (temp, 0);
19077 ASM_INPUT_P (temp) = 1;
19080 else
19081 symtab->finalize_toplevel_asm (string);
19085 /* Given the type TYPE of a declaration with declarator DECLARATOR, return the
19086 type that comes from the decl-specifier-seq. */
19088 static tree
19089 strip_declarator_types (tree type, cp_declarator *declarator)
19091 for (cp_declarator *d = declarator; d;)
19092 switch (d->kind)
19094 case cdk_id:
19095 case cdk_decomp:
19096 case cdk_error:
19097 d = NULL;
19098 break;
19100 default:
19101 if (TYPE_PTRMEMFUNC_P (type))
19102 type = TYPE_PTRMEMFUNC_FN_TYPE (type);
19103 type = TREE_TYPE (type);
19104 d = d->declarator;
19105 break;
19108 return type;
19111 /* Declarators [gram.dcl.decl] */
19113 /* Parse an init-declarator.
19115 init-declarator:
19116 declarator initializer [opt]
19118 GNU Extension:
19120 init-declarator:
19121 declarator asm-specification [opt] attributes [opt] initializer [opt]
19123 function-definition:
19124 decl-specifier-seq [opt] declarator ctor-initializer [opt]
19125 function-body
19126 decl-specifier-seq [opt] declarator function-try-block
19128 GNU Extension:
19130 function-definition:
19131 __extension__ function-definition
19133 TM Extension:
19135 function-definition:
19136 decl-specifier-seq [opt] declarator function-transaction-block
19138 The DECL_SPECIFIERS apply to this declarator. Returns a
19139 representation of the entity declared. If MEMBER_P is TRUE, then
19140 this declarator appears in a class scope. The new DECL created by
19141 this declarator is returned.
19143 The CHECKS are access checks that should be performed once we know
19144 what entity is being declared (and, therefore, what classes have
19145 befriended it).
19147 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
19148 for a function-definition here as well. If the declarator is a
19149 declarator for a function-definition, *FUNCTION_DEFINITION_P will
19150 be TRUE upon return. By that point, the function-definition will
19151 have been completely parsed.
19153 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
19154 is FALSE.
19156 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
19157 parsed declaration if it is an uninitialized single declarator not followed
19158 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
19159 if present, will not be consumed. If returned, this declarator will be
19160 created with SD_INITIALIZED but will not call cp_finish_decl.
19162 If INIT_LOC is not NULL, and *INIT_LOC is equal to UNKNOWN_LOCATION,
19163 and there is an initializer, the pointed location_t is set to the
19164 location of the '=' or `(', or '{' in C++11 token introducing the
19165 initializer. */
19167 static tree
19168 cp_parser_init_declarator (cp_parser* parser,
19169 cp_decl_specifier_seq *decl_specifiers,
19170 vec<deferred_access_check, va_gc> *checks,
19171 bool function_definition_allowed_p,
19172 bool member_p,
19173 int declares_class_or_enum,
19174 bool* function_definition_p,
19175 tree* maybe_range_for_decl,
19176 location_t* init_loc,
19177 tree* auto_result)
19179 cp_token *token = NULL, *asm_spec_start_token = NULL,
19180 *attributes_start_token = NULL;
19181 cp_declarator *declarator;
19182 tree prefix_attributes;
19183 tree attributes = NULL;
19184 tree asm_specification;
19185 tree initializer;
19186 tree decl = NULL_TREE;
19187 tree scope;
19188 int is_initialized;
19189 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
19190 initialized with "= ..", CPP_OPEN_PAREN if initialized with
19191 "(...)". */
19192 enum cpp_ttype initialization_kind;
19193 bool is_direct_init = false;
19194 bool is_non_constant_init;
19195 int ctor_dtor_or_conv_p;
19196 bool friend_p = cp_parser_friend_p (decl_specifiers);
19197 tree pushed_scope = NULL_TREE;
19198 bool range_for_decl_p = false;
19199 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19200 location_t tmp_init_loc = UNKNOWN_LOCATION;
19202 /* Gather the attributes that were provided with the
19203 decl-specifiers. */
19204 prefix_attributes = decl_specifiers->attributes;
19206 /* Assume that this is not the declarator for a function
19207 definition. */
19208 if (function_definition_p)
19209 *function_definition_p = false;
19211 /* Default arguments are only permitted for function parameters. */
19212 if (decl_spec_seq_has_spec_p (decl_specifiers, ds_typedef))
19213 parser->default_arg_ok_p = false;
19215 /* Defer access checks while parsing the declarator; we cannot know
19216 what names are accessible until we know what is being
19217 declared. */
19218 resume_deferring_access_checks ();
19220 token = cp_lexer_peek_token (parser->lexer);
19222 /* Parse the declarator. */
19223 declarator
19224 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19225 &ctor_dtor_or_conv_p,
19226 /*parenthesized_p=*/NULL,
19227 member_p, friend_p);
19228 /* Gather up the deferred checks. */
19229 stop_deferring_access_checks ();
19231 parser->default_arg_ok_p = saved_default_arg_ok_p;
19233 /* If the DECLARATOR was erroneous, there's no need to go
19234 further. */
19235 if (declarator == cp_error_declarator)
19236 return error_mark_node;
19238 /* Check that the number of template-parameter-lists is OK. */
19239 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
19240 token->location))
19241 return error_mark_node;
19243 if (declares_class_or_enum & 2)
19244 cp_parser_check_for_definition_in_return_type (declarator,
19245 decl_specifiers->type,
19246 decl_specifiers->locations[ds_type_spec]);
19248 /* Figure out what scope the entity declared by the DECLARATOR is
19249 located in. `grokdeclarator' sometimes changes the scope, so
19250 we compute it now. */
19251 scope = get_scope_of_declarator (declarator);
19253 /* Perform any lookups in the declared type which were thought to be
19254 dependent, but are not in the scope of the declarator. */
19255 decl_specifiers->type
19256 = maybe_update_decl_type (decl_specifiers->type, scope);
19258 /* If we're allowing GNU extensions, look for an
19259 asm-specification. */
19260 if (cp_parser_allow_gnu_extensions_p (parser))
19262 /* Look for an asm-specification. */
19263 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
19264 asm_specification = cp_parser_asm_specification_opt (parser);
19266 else
19267 asm_specification = NULL_TREE;
19269 /* Look for attributes. */
19270 attributes_start_token = cp_lexer_peek_token (parser->lexer);
19271 attributes = cp_parser_attributes_opt (parser);
19273 /* Peek at the next token. */
19274 token = cp_lexer_peek_token (parser->lexer);
19276 bool bogus_implicit_tmpl = false;
19278 if (function_declarator_p (declarator))
19280 /* Handle C++17 deduction guides. */
19281 if (!decl_specifiers->type
19282 && ctor_dtor_or_conv_p <= 0
19283 && cxx_dialect >= cxx1z)
19285 cp_declarator *id = get_id_declarator (declarator);
19286 tree name = id->u.id.unqualified_name;
19287 parser->scope = id->u.id.qualifying_scope;
19288 tree tmpl = cp_parser_lookup_name_simple (parser, name, id->id_loc);
19289 if (tmpl
19290 && (DECL_CLASS_TEMPLATE_P (tmpl)
19291 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)))
19293 id->u.id.unqualified_name = dguide_name (tmpl);
19294 id->u.id.sfk = sfk_deduction_guide;
19295 ctor_dtor_or_conv_p = 1;
19299 /* Check to see if the token indicates the start of a
19300 function-definition. */
19301 if (cp_parser_token_starts_function_definition_p (token))
19303 if (!function_definition_allowed_p)
19305 /* If a function-definition should not appear here, issue an
19306 error message. */
19307 cp_parser_error (parser,
19308 "a function-definition is not allowed here");
19309 return error_mark_node;
19312 location_t func_brace_location
19313 = cp_lexer_peek_token (parser->lexer)->location;
19315 /* Neither attributes nor an asm-specification are allowed
19316 on a function-definition. */
19317 if (asm_specification)
19318 error_at (asm_spec_start_token->location,
19319 "an asm-specification is not allowed "
19320 "on a function-definition");
19321 if (attributes)
19322 error_at (attributes_start_token->location,
19323 "attributes are not allowed "
19324 "on a function-definition");
19325 /* This is a function-definition. */
19326 *function_definition_p = true;
19328 /* Parse the function definition. */
19329 if (member_p)
19330 decl = cp_parser_save_member_function_body (parser,
19331 decl_specifiers,
19332 declarator,
19333 prefix_attributes);
19334 else
19335 decl =
19336 (cp_parser_function_definition_from_specifiers_and_declarator
19337 (parser, decl_specifiers, prefix_attributes, declarator));
19339 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
19341 /* This is where the prologue starts... */
19342 DECL_STRUCT_FUNCTION (decl)->function_start_locus
19343 = func_brace_location;
19346 return decl;
19349 else if (parser->fully_implicit_function_template_p)
19351 /* A non-template declaration involving a function parameter list
19352 containing an implicit template parameter will be made into a
19353 template. If the resulting declaration is not going to be an
19354 actual function then finish the template scope here to prevent it.
19355 An error message will be issued once we have a decl to talk about.
19357 FIXME probably we should do type deduction rather than create an
19358 implicit template, but the standard currently doesn't allow it. */
19359 bogus_implicit_tmpl = true;
19360 finish_fully_implicit_template (parser, NULL_TREE);
19363 /* [dcl.dcl]
19365 Only in function declarations for constructors, destructors, type
19366 conversions, and deduction guides can the decl-specifier-seq be omitted.
19368 We explicitly postpone this check past the point where we handle
19369 function-definitions because we tolerate function-definitions
19370 that are missing their return types in some modes. */
19371 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
19373 cp_parser_error (parser,
19374 "expected constructor, destructor, or type conversion");
19375 return error_mark_node;
19378 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
19379 if (token->type == CPP_EQ
19380 || token->type == CPP_OPEN_PAREN
19381 || token->type == CPP_OPEN_BRACE)
19383 is_initialized = SD_INITIALIZED;
19384 initialization_kind = token->type;
19385 if (maybe_range_for_decl)
19386 *maybe_range_for_decl = error_mark_node;
19387 tmp_init_loc = token->location;
19388 if (init_loc && *init_loc == UNKNOWN_LOCATION)
19389 *init_loc = tmp_init_loc;
19391 if (token->type == CPP_EQ
19392 && function_declarator_p (declarator))
19394 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
19395 if (t2->keyword == RID_DEFAULT)
19396 is_initialized = SD_DEFAULTED;
19397 else if (t2->keyword == RID_DELETE)
19398 is_initialized = SD_DELETED;
19401 else
19403 /* If the init-declarator isn't initialized and isn't followed by a
19404 `,' or `;', it's not a valid init-declarator. */
19405 if (token->type != CPP_COMMA
19406 && token->type != CPP_SEMICOLON)
19408 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
19409 range_for_decl_p = true;
19410 else
19412 if (!maybe_range_for_decl)
19413 cp_parser_error (parser, "expected initializer");
19414 return error_mark_node;
19417 is_initialized = SD_UNINITIALIZED;
19418 initialization_kind = CPP_EOF;
19421 /* Because start_decl has side-effects, we should only call it if we
19422 know we're going ahead. By this point, we know that we cannot
19423 possibly be looking at any other construct. */
19424 cp_parser_commit_to_tentative_parse (parser);
19426 /* Enter the newly declared entry in the symbol table. If we're
19427 processing a declaration in a class-specifier, we wait until
19428 after processing the initializer. */
19429 if (!member_p)
19431 if (parser->in_unbraced_linkage_specification_p)
19432 decl_specifiers->storage_class = sc_extern;
19433 decl = start_decl (declarator, decl_specifiers,
19434 range_for_decl_p? SD_INITIALIZED : is_initialized,
19435 attributes, prefix_attributes, &pushed_scope);
19436 cp_finalize_omp_declare_simd (parser, decl);
19437 cp_finalize_oacc_routine (parser, decl, false);
19438 /* Adjust location of decl if declarator->id_loc is more appropriate:
19439 set, and decl wasn't merged with another decl, in which case its
19440 location would be different from input_location, and more accurate. */
19441 if (DECL_P (decl)
19442 && declarator->id_loc != UNKNOWN_LOCATION
19443 && DECL_SOURCE_LOCATION (decl) == input_location)
19444 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
19446 else if (scope)
19447 /* Enter the SCOPE. That way unqualified names appearing in the
19448 initializer will be looked up in SCOPE. */
19449 pushed_scope = push_scope (scope);
19451 /* Perform deferred access control checks, now that we know in which
19452 SCOPE the declared entity resides. */
19453 if (!member_p && decl)
19455 tree saved_current_function_decl = NULL_TREE;
19457 /* If the entity being declared is a function, pretend that we
19458 are in its scope. If it is a `friend', it may have access to
19459 things that would not otherwise be accessible. */
19460 if (TREE_CODE (decl) == FUNCTION_DECL)
19462 saved_current_function_decl = current_function_decl;
19463 current_function_decl = decl;
19466 /* Perform access checks for template parameters. */
19467 cp_parser_perform_template_parameter_access_checks (checks);
19469 /* Perform the access control checks for the declarator and the
19470 decl-specifiers. */
19471 perform_deferred_access_checks (tf_warning_or_error);
19473 /* Restore the saved value. */
19474 if (TREE_CODE (decl) == FUNCTION_DECL)
19475 current_function_decl = saved_current_function_decl;
19478 /* Parse the initializer. */
19479 initializer = NULL_TREE;
19480 is_direct_init = false;
19481 is_non_constant_init = true;
19482 if (is_initialized)
19484 if (function_declarator_p (declarator))
19486 if (initialization_kind == CPP_EQ)
19487 initializer = cp_parser_pure_specifier (parser);
19488 else
19490 /* If the declaration was erroneous, we don't really
19491 know what the user intended, so just silently
19492 consume the initializer. */
19493 if (decl != error_mark_node)
19494 error_at (tmp_init_loc, "initializer provided for function");
19495 cp_parser_skip_to_closing_parenthesis (parser,
19496 /*recovering=*/true,
19497 /*or_comma=*/false,
19498 /*consume_paren=*/true);
19501 else
19503 /* We want to record the extra mangling scope for in-class
19504 initializers of class members and initializers of static data
19505 member templates. The former involves deferring
19506 parsing of the initializer until end of class as with default
19507 arguments. So right here we only handle the latter. */
19508 if (!member_p && processing_template_decl)
19509 start_lambda_scope (decl);
19510 initializer = cp_parser_initializer (parser,
19511 &is_direct_init,
19512 &is_non_constant_init);
19513 if (!member_p && processing_template_decl)
19514 finish_lambda_scope ();
19515 if (initializer == error_mark_node)
19516 cp_parser_skip_to_end_of_statement (parser);
19520 /* The old parser allows attributes to appear after a parenthesized
19521 initializer. Mark Mitchell proposed removing this functionality
19522 on the GCC mailing lists on 2002-08-13. This parser accepts the
19523 attributes -- but ignores them. */
19524 if (cp_parser_allow_gnu_extensions_p (parser)
19525 && initialization_kind == CPP_OPEN_PAREN)
19526 if (cp_parser_attributes_opt (parser))
19527 warning (OPT_Wattributes,
19528 "attributes after parenthesized initializer ignored");
19530 /* And now complain about a non-function implicit template. */
19531 if (bogus_implicit_tmpl && decl != error_mark_node)
19532 error_at (DECL_SOURCE_LOCATION (decl),
19533 "non-function %qD declared as implicit template", decl);
19535 /* For an in-class declaration, use `grokfield' to create the
19536 declaration. */
19537 if (member_p)
19539 if (pushed_scope)
19541 pop_scope (pushed_scope);
19542 pushed_scope = NULL_TREE;
19544 decl = grokfield (declarator, decl_specifiers,
19545 initializer, !is_non_constant_init,
19546 /*asmspec=*/NULL_TREE,
19547 chainon (attributes, prefix_attributes));
19548 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
19549 cp_parser_save_default_args (parser, decl);
19550 cp_finalize_omp_declare_simd (parser, decl);
19551 cp_finalize_oacc_routine (parser, decl, false);
19554 /* Finish processing the declaration. But, skip member
19555 declarations. */
19556 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
19558 cp_finish_decl (decl,
19559 initializer, !is_non_constant_init,
19560 asm_specification,
19561 /* If the initializer is in parentheses, then this is
19562 a direct-initialization, which means that an
19563 `explicit' constructor is OK. Otherwise, an
19564 `explicit' constructor cannot be used. */
19565 ((is_direct_init || !is_initialized)
19566 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
19568 else if ((cxx_dialect != cxx98) && friend_p
19569 && decl && TREE_CODE (decl) == FUNCTION_DECL)
19570 /* Core issue #226 (C++0x only): A default template-argument
19571 shall not be specified in a friend class template
19572 declaration. */
19573 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/true,
19574 /*is_partial=*/false, /*is_friend_decl=*/1);
19576 if (!friend_p && pushed_scope)
19577 pop_scope (pushed_scope);
19579 if (function_declarator_p (declarator)
19580 && parser->fully_implicit_function_template_p)
19582 if (member_p)
19583 decl = finish_fully_implicit_template (parser, decl);
19584 else
19585 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
19588 if (auto_result && is_initialized && decl_specifiers->type
19589 && type_uses_auto (decl_specifiers->type))
19590 *auto_result = strip_declarator_types (TREE_TYPE (decl), declarator);
19592 return decl;
19595 /* Parse a declarator.
19597 declarator:
19598 direct-declarator
19599 ptr-operator declarator
19601 abstract-declarator:
19602 ptr-operator abstract-declarator [opt]
19603 direct-abstract-declarator
19605 GNU Extensions:
19607 declarator:
19608 attributes [opt] direct-declarator
19609 attributes [opt] ptr-operator declarator
19611 abstract-declarator:
19612 attributes [opt] ptr-operator abstract-declarator [opt]
19613 attributes [opt] direct-abstract-declarator
19615 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
19616 detect constructors, destructors, deduction guides, or conversion operators.
19617 It is set to -1 if the declarator is a name, and +1 if it is a
19618 function. Otherwise it is set to zero. Usually you just want to
19619 test for >0, but internally the negative value is used.
19621 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
19622 a decl-specifier-seq unless it declares a constructor, destructor,
19623 or conversion. It might seem that we could check this condition in
19624 semantic analysis, rather than parsing, but that makes it difficult
19625 to handle something like `f()'. We want to notice that there are
19626 no decl-specifiers, and therefore realize that this is an
19627 expression, not a declaration.)
19629 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
19630 the declarator is a direct-declarator of the form "(...)".
19632 MEMBER_P is true iff this declarator is a member-declarator.
19634 FRIEND_P is true iff this declarator is a friend. */
19636 static cp_declarator *
19637 cp_parser_declarator (cp_parser* parser,
19638 cp_parser_declarator_kind dcl_kind,
19639 int* ctor_dtor_or_conv_p,
19640 bool* parenthesized_p,
19641 bool member_p, bool friend_p)
19643 cp_declarator *declarator;
19644 enum tree_code code;
19645 cp_cv_quals cv_quals;
19646 tree class_type;
19647 tree gnu_attributes = NULL_TREE, std_attributes = NULL_TREE;
19649 /* Assume this is not a constructor, destructor, or type-conversion
19650 operator. */
19651 if (ctor_dtor_or_conv_p)
19652 *ctor_dtor_or_conv_p = 0;
19654 if (cp_parser_allow_gnu_extensions_p (parser))
19655 gnu_attributes = cp_parser_gnu_attributes_opt (parser);
19657 /* Check for the ptr-operator production. */
19658 cp_parser_parse_tentatively (parser);
19659 /* Parse the ptr-operator. */
19660 code = cp_parser_ptr_operator (parser,
19661 &class_type,
19662 &cv_quals,
19663 &std_attributes);
19665 /* If that worked, then we have a ptr-operator. */
19666 if (cp_parser_parse_definitely (parser))
19668 /* If a ptr-operator was found, then this declarator was not
19669 parenthesized. */
19670 if (parenthesized_p)
19671 *parenthesized_p = true;
19672 /* The dependent declarator is optional if we are parsing an
19673 abstract-declarator. */
19674 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19675 cp_parser_parse_tentatively (parser);
19677 /* Parse the dependent declarator. */
19678 declarator = cp_parser_declarator (parser, dcl_kind,
19679 /*ctor_dtor_or_conv_p=*/NULL,
19680 /*parenthesized_p=*/NULL,
19681 /*member_p=*/false,
19682 friend_p);
19684 /* If we are parsing an abstract-declarator, we must handle the
19685 case where the dependent declarator is absent. */
19686 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
19687 && !cp_parser_parse_definitely (parser))
19688 declarator = NULL;
19690 declarator = cp_parser_make_indirect_declarator
19691 (code, class_type, cv_quals, declarator, std_attributes);
19693 /* Everything else is a direct-declarator. */
19694 else
19696 if (parenthesized_p)
19697 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
19698 CPP_OPEN_PAREN);
19699 declarator = cp_parser_direct_declarator (parser, dcl_kind,
19700 ctor_dtor_or_conv_p,
19701 member_p, friend_p);
19704 if (gnu_attributes && declarator && declarator != cp_error_declarator)
19705 declarator->attributes = gnu_attributes;
19706 return declarator;
19709 /* Parse a direct-declarator or direct-abstract-declarator.
19711 direct-declarator:
19712 declarator-id
19713 direct-declarator ( parameter-declaration-clause )
19714 cv-qualifier-seq [opt]
19715 ref-qualifier [opt]
19716 exception-specification [opt]
19717 direct-declarator [ constant-expression [opt] ]
19718 ( declarator )
19720 direct-abstract-declarator:
19721 direct-abstract-declarator [opt]
19722 ( parameter-declaration-clause )
19723 cv-qualifier-seq [opt]
19724 ref-qualifier [opt]
19725 exception-specification [opt]
19726 direct-abstract-declarator [opt] [ constant-expression [opt] ]
19727 ( abstract-declarator )
19729 Returns a representation of the declarator. DCL_KIND is
19730 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
19731 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
19732 we are parsing a direct-declarator. It is
19733 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
19734 of ambiguity we prefer an abstract declarator, as per
19735 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P, MEMBER_P, and FRIEND_P are
19736 as for cp_parser_declarator. */
19738 static cp_declarator *
19739 cp_parser_direct_declarator (cp_parser* parser,
19740 cp_parser_declarator_kind dcl_kind,
19741 int* ctor_dtor_or_conv_p,
19742 bool member_p, bool friend_p)
19744 cp_token *token;
19745 cp_declarator *declarator = NULL;
19746 tree scope = NULL_TREE;
19747 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
19748 bool saved_in_declarator_p = parser->in_declarator_p;
19749 bool first = true;
19750 tree pushed_scope = NULL_TREE;
19752 while (true)
19754 /* Peek at the next token. */
19755 token = cp_lexer_peek_token (parser->lexer);
19756 if (token->type == CPP_OPEN_PAREN)
19758 /* This is either a parameter-declaration-clause, or a
19759 parenthesized declarator. When we know we are parsing a
19760 named declarator, it must be a parenthesized declarator
19761 if FIRST is true. For instance, `(int)' is a
19762 parameter-declaration-clause, with an omitted
19763 direct-abstract-declarator. But `((*))', is a
19764 parenthesized abstract declarator. Finally, when T is a
19765 template parameter `(T)' is a
19766 parameter-declaration-clause, and not a parenthesized
19767 named declarator.
19769 We first try and parse a parameter-declaration-clause,
19770 and then try a nested declarator (if FIRST is true).
19772 It is not an error for it not to be a
19773 parameter-declaration-clause, even when FIRST is
19774 false. Consider,
19776 int i (int);
19777 int i (3);
19779 The first is the declaration of a function while the
19780 second is the definition of a variable, including its
19781 initializer.
19783 Having seen only the parenthesis, we cannot know which of
19784 these two alternatives should be selected. Even more
19785 complex are examples like:
19787 int i (int (a));
19788 int i (int (3));
19790 The former is a function-declaration; the latter is a
19791 variable initialization.
19793 Thus again, we try a parameter-declaration-clause, and if
19794 that fails, we back out and return. */
19796 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19798 tree params;
19799 bool is_declarator = false;
19801 /* In a member-declarator, the only valid interpretation
19802 of a parenthesis is the start of a
19803 parameter-declaration-clause. (It is invalid to
19804 initialize a static data member with a parenthesized
19805 initializer; only the "=" form of initialization is
19806 permitted.) */
19807 if (!member_p)
19808 cp_parser_parse_tentatively (parser);
19810 /* Consume the `('. */
19811 cp_lexer_consume_token (parser->lexer);
19812 if (first)
19814 /* If this is going to be an abstract declarator, we're
19815 in a declarator and we can't have default args. */
19816 parser->default_arg_ok_p = false;
19817 parser->in_declarator_p = true;
19820 begin_scope (sk_function_parms, NULL_TREE);
19822 /* Parse the parameter-declaration-clause. */
19823 params = cp_parser_parameter_declaration_clause (parser);
19825 /* Consume the `)'. */
19826 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19828 /* If all went well, parse the cv-qualifier-seq,
19829 ref-qualifier and the exception-specification. */
19830 if (member_p || cp_parser_parse_definitely (parser))
19832 cp_cv_quals cv_quals;
19833 cp_virt_specifiers virt_specifiers;
19834 cp_ref_qualifier ref_qual;
19835 tree exception_specification;
19836 tree late_return;
19837 tree attrs;
19838 bool memfn = (member_p || (pushed_scope
19839 && CLASS_TYPE_P (pushed_scope)));
19841 is_declarator = true;
19843 if (ctor_dtor_or_conv_p)
19844 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
19845 first = false;
19847 /* Parse the cv-qualifier-seq. */
19848 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
19849 /* Parse the ref-qualifier. */
19850 ref_qual = cp_parser_ref_qualifier_opt (parser);
19851 /* Parse the tx-qualifier. */
19852 tree tx_qual = cp_parser_tx_qualifier_opt (parser);
19853 /* And the exception-specification. */
19854 exception_specification
19855 = cp_parser_exception_specification_opt (parser);
19857 attrs = cp_parser_std_attribute_spec_seq (parser);
19859 /* In here, we handle cases where attribute is used after
19860 the function declaration. For example:
19861 void func (int x) __attribute__((vector(..))); */
19862 tree gnu_attrs = NULL_TREE;
19863 if (flag_cilkplus
19864 && cp_next_tokens_can_be_gnu_attribute_p (parser))
19866 cp_parser_parse_tentatively (parser);
19867 tree attr = cp_parser_gnu_attributes_opt (parser);
19868 if (cp_lexer_next_token_is_not (parser->lexer,
19869 CPP_SEMICOLON)
19870 && cp_lexer_next_token_is_not (parser->lexer,
19871 CPP_OPEN_BRACE))
19872 cp_parser_abort_tentative_parse (parser);
19873 else if (!cp_parser_parse_definitely (parser))
19875 else
19876 gnu_attrs = attr;
19878 tree requires_clause = NULL_TREE;
19879 late_return = (cp_parser_late_return_type_opt
19880 (parser, declarator, requires_clause,
19881 memfn ? cv_quals : -1));
19883 /* Parse the virt-specifier-seq. */
19884 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
19886 /* Create the function-declarator. */
19887 declarator = make_call_declarator (declarator,
19888 params,
19889 cv_quals,
19890 virt_specifiers,
19891 ref_qual,
19892 tx_qual,
19893 exception_specification,
19894 late_return,
19895 requires_clause);
19896 declarator->std_attributes = attrs;
19897 declarator->attributes = gnu_attrs;
19898 /* Any subsequent parameter lists are to do with
19899 return type, so are not those of the declared
19900 function. */
19901 parser->default_arg_ok_p = false;
19904 /* Remove the function parms from scope. */
19905 pop_bindings_and_leave_scope ();
19907 if (is_declarator)
19908 /* Repeat the main loop. */
19909 continue;
19912 /* If this is the first, we can try a parenthesized
19913 declarator. */
19914 if (first)
19916 bool saved_in_type_id_in_expr_p;
19918 parser->default_arg_ok_p = saved_default_arg_ok_p;
19919 parser->in_declarator_p = saved_in_declarator_p;
19921 /* Consume the `('. */
19922 cp_lexer_consume_token (parser->lexer);
19923 /* Parse the nested declarator. */
19924 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19925 parser->in_type_id_in_expr_p = true;
19926 declarator
19927 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
19928 /*parenthesized_p=*/NULL,
19929 member_p, friend_p);
19930 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19931 first = false;
19932 /* Expect a `)'. */
19933 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
19934 declarator = cp_error_declarator;
19935 if (declarator == cp_error_declarator)
19936 break;
19938 goto handle_declarator;
19940 /* Otherwise, we must be done. */
19941 else
19942 break;
19944 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
19945 && token->type == CPP_OPEN_SQUARE
19946 && !cp_next_tokens_can_be_attribute_p (parser))
19948 /* Parse an array-declarator. */
19949 tree bounds, attrs;
19951 if (ctor_dtor_or_conv_p)
19952 *ctor_dtor_or_conv_p = 0;
19954 first = false;
19955 parser->default_arg_ok_p = false;
19956 parser->in_declarator_p = true;
19957 /* Consume the `['. */
19958 cp_lexer_consume_token (parser->lexer);
19959 /* Peek at the next token. */
19960 token = cp_lexer_peek_token (parser->lexer);
19961 /* If the next token is `]', then there is no
19962 constant-expression. */
19963 if (token->type != CPP_CLOSE_SQUARE)
19965 bool non_constant_p;
19966 bounds
19967 = cp_parser_constant_expression (parser,
19968 /*allow_non_constant=*/true,
19969 &non_constant_p);
19970 if (!non_constant_p)
19971 /* OK */;
19972 else if (error_operand_p (bounds))
19973 /* Already gave an error. */;
19974 else if (!parser->in_function_body
19975 || current_binding_level->kind == sk_function_parms)
19977 /* Normally, the array bound must be an integral constant
19978 expression. However, as an extension, we allow VLAs
19979 in function scopes as long as they aren't part of a
19980 parameter declaration. */
19981 cp_parser_error (parser,
19982 "array bound is not an integer constant");
19983 bounds = error_mark_node;
19985 else if (processing_template_decl
19986 && !type_dependent_expression_p (bounds))
19988 /* Remember this wasn't a constant-expression. */
19989 bounds = build_nop (TREE_TYPE (bounds), bounds);
19990 TREE_SIDE_EFFECTS (bounds) = 1;
19993 else
19994 bounds = NULL_TREE;
19995 /* Look for the closing `]'. */
19996 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
19998 declarator = cp_error_declarator;
19999 break;
20002 attrs = cp_parser_std_attribute_spec_seq (parser);
20003 declarator = make_array_declarator (declarator, bounds);
20004 declarator->std_attributes = attrs;
20006 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
20009 tree qualifying_scope;
20010 tree unqualified_name;
20011 tree attrs;
20012 special_function_kind sfk;
20013 bool abstract_ok;
20014 bool pack_expansion_p = false;
20015 cp_token *declarator_id_start_token;
20017 /* Parse a declarator-id */
20018 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
20019 if (abstract_ok)
20021 cp_parser_parse_tentatively (parser);
20023 /* If we see an ellipsis, we should be looking at a
20024 parameter pack. */
20025 if (token->type == CPP_ELLIPSIS)
20027 /* Consume the `...' */
20028 cp_lexer_consume_token (parser->lexer);
20030 pack_expansion_p = true;
20034 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
20035 unqualified_name
20036 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
20037 qualifying_scope = parser->scope;
20038 if (abstract_ok)
20040 bool okay = false;
20042 if (!unqualified_name && pack_expansion_p)
20044 /* Check whether an error occurred. */
20045 okay = !cp_parser_error_occurred (parser);
20047 /* We already consumed the ellipsis to mark a
20048 parameter pack, but we have no way to report it,
20049 so abort the tentative parse. We will be exiting
20050 immediately anyway. */
20051 cp_parser_abort_tentative_parse (parser);
20053 else
20054 okay = cp_parser_parse_definitely (parser);
20056 if (!okay)
20057 unqualified_name = error_mark_node;
20058 else if (unqualified_name
20059 && (qualifying_scope
20060 || (!identifier_p (unqualified_name))))
20062 cp_parser_error (parser, "expected unqualified-id");
20063 unqualified_name = error_mark_node;
20067 if (!unqualified_name)
20068 return NULL;
20069 if (unqualified_name == error_mark_node)
20071 declarator = cp_error_declarator;
20072 pack_expansion_p = false;
20073 declarator->parameter_pack_p = false;
20074 break;
20077 attrs = cp_parser_std_attribute_spec_seq (parser);
20079 if (qualifying_scope && at_namespace_scope_p ()
20080 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
20082 /* In the declaration of a member of a template class
20083 outside of the class itself, the SCOPE will sometimes
20084 be a TYPENAME_TYPE. For example, given:
20086 template <typename T>
20087 int S<T>::R::i = 3;
20089 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
20090 this context, we must resolve S<T>::R to an ordinary
20091 type, rather than a typename type.
20093 The reason we normally avoid resolving TYPENAME_TYPEs
20094 is that a specialization of `S' might render
20095 `S<T>::R' not a type. However, if `S' is
20096 specialized, then this `i' will not be used, so there
20097 is no harm in resolving the types here. */
20098 tree type;
20100 /* Resolve the TYPENAME_TYPE. */
20101 type = resolve_typename_type (qualifying_scope,
20102 /*only_current_p=*/false);
20103 /* If that failed, the declarator is invalid. */
20104 if (TREE_CODE (type) == TYPENAME_TYPE)
20106 if (typedef_variant_p (type))
20107 error_at (declarator_id_start_token->location,
20108 "cannot define member of dependent typedef "
20109 "%qT", type);
20110 else
20111 error_at (declarator_id_start_token->location,
20112 "%<%T::%E%> is not a type",
20113 TYPE_CONTEXT (qualifying_scope),
20114 TYPE_IDENTIFIER (qualifying_scope));
20116 qualifying_scope = type;
20119 sfk = sfk_none;
20121 if (unqualified_name)
20123 tree class_type;
20125 if (qualifying_scope
20126 && CLASS_TYPE_P (qualifying_scope))
20127 class_type = qualifying_scope;
20128 else
20129 class_type = current_class_type;
20131 if (TREE_CODE (unqualified_name) == TYPE_DECL)
20133 tree name_type = TREE_TYPE (unqualified_name);
20135 if (!class_type || !same_type_p (name_type, class_type))
20137 /* We do not attempt to print the declarator
20138 here because we do not have enough
20139 information about its original syntactic
20140 form. */
20141 cp_parser_error (parser, "invalid declarator");
20142 declarator = cp_error_declarator;
20143 break;
20145 else if (qualifying_scope
20146 && CLASSTYPE_USE_TEMPLATE (name_type))
20148 error_at (declarator_id_start_token->location,
20149 "invalid use of constructor as a template");
20150 inform (declarator_id_start_token->location,
20151 "use %<%T::%D%> instead of %<%T::%D%> to "
20152 "name the constructor in a qualified name",
20153 class_type,
20154 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
20155 class_type, name_type);
20156 declarator = cp_error_declarator;
20157 break;
20159 unqualified_name = constructor_name (class_type);
20162 if (class_type)
20164 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
20165 sfk = sfk_destructor;
20166 else if (identifier_p (unqualified_name)
20167 && IDENTIFIER_CONV_OP_P (unqualified_name))
20168 sfk = sfk_conversion;
20169 else if (/* There's no way to declare a constructor
20170 for an unnamed type, even if the type
20171 got a name for linkage purposes. */
20172 !TYPE_WAS_UNNAMED (class_type)
20173 /* Handle correctly (c++/19200):
20175 struct S {
20176 struct T{};
20177 friend void S(T);
20180 and also:
20182 namespace N {
20183 void S();
20186 struct S {
20187 friend void N::S();
20188 }; */
20189 && (!friend_p || class_type == qualifying_scope)
20190 && constructor_name_p (unqualified_name,
20191 class_type))
20192 sfk = sfk_constructor;
20193 else if (is_overloaded_fn (unqualified_name)
20194 && DECL_CONSTRUCTOR_P (get_first_fn
20195 (unqualified_name)))
20196 sfk = sfk_constructor;
20198 if (ctor_dtor_or_conv_p && sfk != sfk_none)
20199 *ctor_dtor_or_conv_p = -1;
20202 declarator = make_id_declarator (qualifying_scope,
20203 unqualified_name,
20204 sfk);
20205 declarator->std_attributes = attrs;
20206 declarator->id_loc = token->location;
20207 declarator->parameter_pack_p = pack_expansion_p;
20209 if (pack_expansion_p)
20210 maybe_warn_variadic_templates ();
20213 handle_declarator:;
20214 scope = get_scope_of_declarator (declarator);
20215 if (scope)
20217 /* Any names that appear after the declarator-id for a
20218 member are looked up in the containing scope. */
20219 if (at_function_scope_p ())
20221 /* But declarations with qualified-ids can't appear in a
20222 function. */
20223 cp_parser_error (parser, "qualified-id in declaration");
20224 declarator = cp_error_declarator;
20225 break;
20227 pushed_scope = push_scope (scope);
20229 parser->in_declarator_p = true;
20230 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
20231 || (declarator && declarator->kind == cdk_id))
20232 /* Default args are only allowed on function
20233 declarations. */
20234 parser->default_arg_ok_p = saved_default_arg_ok_p;
20235 else
20236 parser->default_arg_ok_p = false;
20238 first = false;
20240 /* We're done. */
20241 else
20242 break;
20245 /* For an abstract declarator, we might wind up with nothing at this
20246 point. That's an error; the declarator is not optional. */
20247 if (!declarator)
20248 cp_parser_error (parser, "expected declarator");
20250 /* If we entered a scope, we must exit it now. */
20251 if (pushed_scope)
20252 pop_scope (pushed_scope);
20254 parser->default_arg_ok_p = saved_default_arg_ok_p;
20255 parser->in_declarator_p = saved_in_declarator_p;
20257 return declarator;
20260 /* Parse a ptr-operator.
20262 ptr-operator:
20263 * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20264 * cv-qualifier-seq [opt]
20266 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
20267 nested-name-specifier * attribute-specifier-seq [opt] cv-qualifier-seq [opt] (C++11)
20269 GNU Extension:
20271 ptr-operator:
20272 & cv-qualifier-seq [opt]
20274 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
20275 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
20276 an rvalue reference. In the case of a pointer-to-member, *TYPE is
20277 filled in with the TYPE containing the member. *CV_QUALS is
20278 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
20279 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
20280 Note that the tree codes returned by this function have nothing
20281 to do with the types of trees that will be eventually be created
20282 to represent the pointer or reference type being parsed. They are
20283 just constants with suggestive names. */
20284 static enum tree_code
20285 cp_parser_ptr_operator (cp_parser* parser,
20286 tree* type,
20287 cp_cv_quals *cv_quals,
20288 tree *attributes)
20290 enum tree_code code = ERROR_MARK;
20291 cp_token *token;
20292 tree attrs = NULL_TREE;
20294 /* Assume that it's not a pointer-to-member. */
20295 *type = NULL_TREE;
20296 /* And that there are no cv-qualifiers. */
20297 *cv_quals = TYPE_UNQUALIFIED;
20299 /* Peek at the next token. */
20300 token = cp_lexer_peek_token (parser->lexer);
20302 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
20303 if (token->type == CPP_MULT)
20304 code = INDIRECT_REF;
20305 else if (token->type == CPP_AND)
20306 code = ADDR_EXPR;
20307 else if ((cxx_dialect != cxx98) &&
20308 token->type == CPP_AND_AND) /* C++0x only */
20309 code = NON_LVALUE_EXPR;
20311 if (code != ERROR_MARK)
20313 /* Consume the `*', `&' or `&&'. */
20314 cp_lexer_consume_token (parser->lexer);
20316 /* A `*' can be followed by a cv-qualifier-seq, and so can a
20317 `&', if we are allowing GNU extensions. (The only qualifier
20318 that can legally appear after `&' is `restrict', but that is
20319 enforced during semantic analysis. */
20320 if (code == INDIRECT_REF
20321 || cp_parser_allow_gnu_extensions_p (parser))
20322 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20324 attrs = cp_parser_std_attribute_spec_seq (parser);
20325 if (attributes != NULL)
20326 *attributes = attrs;
20328 else
20330 /* Try the pointer-to-member case. */
20331 cp_parser_parse_tentatively (parser);
20332 /* Look for the optional `::' operator. */
20333 cp_parser_global_scope_opt (parser,
20334 /*current_scope_valid_p=*/false);
20335 /* Look for the nested-name specifier. */
20336 token = cp_lexer_peek_token (parser->lexer);
20337 cp_parser_nested_name_specifier (parser,
20338 /*typename_keyword_p=*/false,
20339 /*check_dependency_p=*/true,
20340 /*type_p=*/false,
20341 /*is_declaration=*/false);
20342 /* If we found it, and the next token is a `*', then we are
20343 indeed looking at a pointer-to-member operator. */
20344 if (!cp_parser_error_occurred (parser)
20345 && cp_parser_require (parser, CPP_MULT, RT_MULT))
20347 /* Indicate that the `*' operator was used. */
20348 code = INDIRECT_REF;
20350 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
20351 error_at (token->location, "%qD is a namespace", parser->scope);
20352 else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
20353 error_at (token->location, "cannot form pointer to member of "
20354 "non-class %q#T", parser->scope);
20355 else
20357 /* The type of which the member is a member is given by the
20358 current SCOPE. */
20359 *type = parser->scope;
20360 /* The next name will not be qualified. */
20361 parser->scope = NULL_TREE;
20362 parser->qualifying_scope = NULL_TREE;
20363 parser->object_scope = NULL_TREE;
20364 /* Look for optional c++11 attributes. */
20365 attrs = cp_parser_std_attribute_spec_seq (parser);
20366 if (attributes != NULL)
20367 *attributes = attrs;
20368 /* Look for the optional cv-qualifier-seq. */
20369 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
20372 /* If that didn't work we don't have a ptr-operator. */
20373 if (!cp_parser_parse_definitely (parser))
20374 cp_parser_error (parser, "expected ptr-operator");
20377 return code;
20380 /* Parse an (optional) cv-qualifier-seq.
20382 cv-qualifier-seq:
20383 cv-qualifier cv-qualifier-seq [opt]
20385 cv-qualifier:
20386 const
20387 volatile
20389 GNU Extension:
20391 cv-qualifier:
20392 __restrict__
20394 Returns a bitmask representing the cv-qualifiers. */
20396 static cp_cv_quals
20397 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
20399 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
20401 while (true)
20403 cp_token *token;
20404 cp_cv_quals cv_qualifier;
20406 /* Peek at the next token. */
20407 token = cp_lexer_peek_token (parser->lexer);
20408 /* See if it's a cv-qualifier. */
20409 switch (token->keyword)
20411 case RID_CONST:
20412 cv_qualifier = TYPE_QUAL_CONST;
20413 break;
20415 case RID_VOLATILE:
20416 cv_qualifier = TYPE_QUAL_VOLATILE;
20417 break;
20419 case RID_RESTRICT:
20420 cv_qualifier = TYPE_QUAL_RESTRICT;
20421 break;
20423 default:
20424 cv_qualifier = TYPE_UNQUALIFIED;
20425 break;
20428 if (!cv_qualifier)
20429 break;
20431 if (cv_quals & cv_qualifier)
20433 gcc_rich_location richloc (token->location);
20434 richloc.add_fixit_remove ();
20435 error_at_rich_loc (&richloc, "duplicate cv-qualifier");
20436 cp_lexer_purge_token (parser->lexer);
20438 else
20440 cp_lexer_consume_token (parser->lexer);
20441 cv_quals |= cv_qualifier;
20445 return cv_quals;
20448 /* Parse an (optional) ref-qualifier
20450 ref-qualifier:
20454 Returns cp_ref_qualifier representing ref-qualifier. */
20456 static cp_ref_qualifier
20457 cp_parser_ref_qualifier_opt (cp_parser* parser)
20459 cp_ref_qualifier ref_qual = REF_QUAL_NONE;
20461 /* Don't try to parse bitwise '&' as a ref-qualifier (c++/57532). */
20462 if (cxx_dialect < cxx11 && cp_parser_parsing_tentatively (parser))
20463 return ref_qual;
20465 while (true)
20467 cp_ref_qualifier curr_ref_qual = REF_QUAL_NONE;
20468 cp_token *token = cp_lexer_peek_token (parser->lexer);
20470 switch (token->type)
20472 case CPP_AND:
20473 curr_ref_qual = REF_QUAL_LVALUE;
20474 break;
20476 case CPP_AND_AND:
20477 curr_ref_qual = REF_QUAL_RVALUE;
20478 break;
20480 default:
20481 curr_ref_qual = REF_QUAL_NONE;
20482 break;
20485 if (!curr_ref_qual)
20486 break;
20487 else if (ref_qual)
20489 error_at (token->location, "multiple ref-qualifiers");
20490 cp_lexer_purge_token (parser->lexer);
20492 else
20494 ref_qual = curr_ref_qual;
20495 cp_lexer_consume_token (parser->lexer);
20499 return ref_qual;
20502 /* Parse an optional tx-qualifier.
20504 tx-qualifier:
20505 transaction_safe
20506 transaction_safe_dynamic */
20508 static tree
20509 cp_parser_tx_qualifier_opt (cp_parser *parser)
20511 cp_token *token = cp_lexer_peek_token (parser->lexer);
20512 if (token->type == CPP_NAME)
20514 tree name = token->u.value;
20515 const char *p = IDENTIFIER_POINTER (name);
20516 const int len = strlen ("transaction_safe");
20517 if (!strncmp (p, "transaction_safe", len))
20519 p += len;
20520 if (*p == '\0'
20521 || !strcmp (p, "_dynamic"))
20523 cp_lexer_consume_token (parser->lexer);
20524 if (!flag_tm)
20526 error ("%qE requires %<-fgnu-tm%>", name);
20527 return NULL_TREE;
20529 else
20530 return name;
20534 return NULL_TREE;
20537 /* Parse an (optional) virt-specifier-seq.
20539 virt-specifier-seq:
20540 virt-specifier virt-specifier-seq [opt]
20542 virt-specifier:
20543 override
20544 final
20546 Returns a bitmask representing the virt-specifiers. */
20548 static cp_virt_specifiers
20549 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
20551 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
20553 while (true)
20555 cp_token *token;
20556 cp_virt_specifiers virt_specifier;
20558 /* Peek at the next token. */
20559 token = cp_lexer_peek_token (parser->lexer);
20560 /* See if it's a virt-specifier-qualifier. */
20561 if (token->type != CPP_NAME)
20562 break;
20563 if (id_equal (token->u.value, "override"))
20565 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20566 virt_specifier = VIRT_SPEC_OVERRIDE;
20568 else if (id_equal (token->u.value, "final"))
20570 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
20571 virt_specifier = VIRT_SPEC_FINAL;
20573 else if (id_equal (token->u.value, "__final"))
20575 virt_specifier = VIRT_SPEC_FINAL;
20577 else
20578 break;
20580 if (virt_specifiers & virt_specifier)
20582 gcc_rich_location richloc (token->location);
20583 richloc.add_fixit_remove ();
20584 error_at_rich_loc (&richloc, "duplicate virt-specifier");
20585 cp_lexer_purge_token (parser->lexer);
20587 else
20589 cp_lexer_consume_token (parser->lexer);
20590 virt_specifiers |= virt_specifier;
20593 return virt_specifiers;
20596 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
20597 is in scope even though it isn't real. */
20599 void
20600 inject_this_parameter (tree ctype, cp_cv_quals quals)
20602 tree this_parm;
20604 if (current_class_ptr)
20606 /* We don't clear this between NSDMIs. Is it already what we want? */
20607 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
20608 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
20609 && cp_type_quals (type) == quals)
20610 return;
20613 this_parm = build_this_parm (NULL_TREE, ctype, quals);
20614 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
20615 current_class_ptr = NULL_TREE;
20616 current_class_ref
20617 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
20618 current_class_ptr = this_parm;
20621 /* Return true iff our current scope is a non-static data member
20622 initializer. */
20624 bool
20625 parsing_nsdmi (void)
20627 /* We recognize NSDMI context by the context-less 'this' pointer set up
20628 by the function above. */
20629 if (current_class_ptr
20630 && TREE_CODE (current_class_ptr) == PARM_DECL
20631 && DECL_CONTEXT (current_class_ptr) == NULL_TREE)
20632 return true;
20633 return false;
20636 /* Return true iff our current scope is a default capturing generic lambda
20637 defined within a template. FIXME: This is part of a workaround (see
20638 semantics.c) to handle building lambda closure types correctly in templates
20639 which we ultimately want to defer to instantiation time. */
20641 bool
20642 parsing_default_capturing_generic_lambda_in_template (void)
20644 if (!processing_template_decl || !current_class_type)
20645 return false;
20647 tree lam = CLASSTYPE_LAMBDA_EXPR (current_class_type);
20648 if (!lam || LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lam) == CPLD_NONE)
20649 return false;
20651 tree callop = lambda_function (lam);
20652 if (!callop)
20653 return false;
20655 return (DECL_TEMPLATE_INFO (callop)
20656 && (DECL_TEMPLATE_RESULT (DECL_TI_TEMPLATE (callop)) == callop)
20657 && ((current_nonlambda_class_type ()
20658 && CLASSTYPE_TEMPLATE_INFO (current_nonlambda_class_type ()))
20659 || ((current_nonlambda_function ()
20660 && DECL_TEMPLATE_INFO (current_nonlambda_function ())))));
20663 /* Parse a late-specified return type, if any. This is not a separate
20664 non-terminal, but part of a function declarator, which looks like
20666 -> trailing-type-specifier-seq abstract-declarator(opt)
20668 Returns the type indicated by the type-id.
20670 In addition to this, parse any queued up #pragma omp declare simd
20671 clauses, Cilk Plus SIMD-enabled functions' vector attributes, and
20672 #pragma acc routine clauses.
20674 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
20675 function. */
20677 static tree
20678 cp_parser_late_return_type_opt (cp_parser* parser, cp_declarator *declarator,
20679 tree& requires_clause, cp_cv_quals quals)
20681 cp_token *token;
20682 tree type = NULL_TREE;
20683 bool declare_simd_p = (parser->omp_declare_simd
20684 && declarator
20685 && declarator->kind == cdk_id);
20687 bool cilk_simd_fn_vector_p = (parser->cilk_simd_fn_info
20688 && declarator && declarator->kind == cdk_id);
20690 bool oacc_routine_p = (parser->oacc_routine
20691 && declarator
20692 && declarator->kind == cdk_id);
20694 /* Peek at the next token. */
20695 token = cp_lexer_peek_token (parser->lexer);
20696 /* A late-specified return type is indicated by an initial '->'. */
20697 if (token->type != CPP_DEREF
20698 && token->keyword != RID_REQUIRES
20699 && !(token->type == CPP_NAME
20700 && token->u.value == ridpointers[RID_REQUIRES])
20701 && !(declare_simd_p || cilk_simd_fn_vector_p || oacc_routine_p))
20702 return NULL_TREE;
20704 tree save_ccp = current_class_ptr;
20705 tree save_ccr = current_class_ref;
20706 if (quals >= 0)
20708 /* DR 1207: 'this' is in scope in the trailing return type. */
20709 inject_this_parameter (current_class_type, quals);
20712 if (token->type == CPP_DEREF)
20714 /* Consume the ->. */
20715 cp_lexer_consume_token (parser->lexer);
20717 type = cp_parser_trailing_type_id (parser);
20720 /* Function declarations may be followed by a trailing
20721 requires-clause. */
20722 requires_clause = cp_parser_requires_clause_opt (parser);
20724 if (cilk_simd_fn_vector_p)
20725 declarator->attributes
20726 = cp_parser_late_parsing_cilk_simd_fn_info (parser,
20727 declarator->attributes);
20728 if (declare_simd_p)
20729 declarator->attributes
20730 = cp_parser_late_parsing_omp_declare_simd (parser,
20731 declarator->attributes);
20732 if (oacc_routine_p)
20733 declarator->attributes
20734 = cp_parser_late_parsing_oacc_routine (parser,
20735 declarator->attributes);
20737 if (quals >= 0)
20739 current_class_ptr = save_ccp;
20740 current_class_ref = save_ccr;
20743 return type;
20746 /* Parse a declarator-id.
20748 declarator-id:
20749 id-expression
20750 :: [opt] nested-name-specifier [opt] type-name
20752 In the `id-expression' case, the value returned is as for
20753 cp_parser_id_expression if the id-expression was an unqualified-id.
20754 If the id-expression was a qualified-id, then a SCOPE_REF is
20755 returned. The first operand is the scope (either a NAMESPACE_DECL
20756 or TREE_TYPE), but the second is still just a representation of an
20757 unqualified-id. */
20759 static tree
20760 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
20762 tree id;
20763 /* The expression must be an id-expression. Assume that qualified
20764 names are the names of types so that:
20766 template <class T>
20767 int S<T>::R::i = 3;
20769 will work; we must treat `S<T>::R' as the name of a type.
20770 Similarly, assume that qualified names are templates, where
20771 required, so that:
20773 template <class T>
20774 int S<T>::R<T>::i = 3;
20776 will work, too. */
20777 id = cp_parser_id_expression (parser,
20778 /*template_keyword_p=*/false,
20779 /*check_dependency_p=*/false,
20780 /*template_p=*/NULL,
20781 /*declarator_p=*/true,
20782 optional_p);
20783 if (id && BASELINK_P (id))
20784 id = BASELINK_FUNCTIONS (id);
20785 return id;
20788 /* Parse a type-id.
20790 type-id:
20791 type-specifier-seq abstract-declarator [opt]
20793 Returns the TYPE specified. */
20795 static tree
20796 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
20797 bool is_trailing_return)
20799 cp_decl_specifier_seq type_specifier_seq;
20800 cp_declarator *abstract_declarator;
20802 /* Parse the type-specifier-seq. */
20803 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
20804 is_trailing_return,
20805 &type_specifier_seq);
20806 if (is_template_arg && type_specifier_seq.type
20807 && TREE_CODE (type_specifier_seq.type) == TEMPLATE_TYPE_PARM
20808 && CLASS_PLACEHOLDER_TEMPLATE (type_specifier_seq.type))
20809 /* A bare template name as a template argument is a template template
20810 argument, not a placeholder, so fail parsing it as a type argument. */
20812 gcc_assert (cp_parser_uncommitted_to_tentative_parse_p (parser));
20813 cp_parser_simulate_error (parser);
20814 return error_mark_node;
20816 if (type_specifier_seq.type == error_mark_node)
20817 return error_mark_node;
20819 /* There might or might not be an abstract declarator. */
20820 cp_parser_parse_tentatively (parser);
20821 /* Look for the declarator. */
20822 abstract_declarator
20823 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
20824 /*parenthesized_p=*/NULL,
20825 /*member_p=*/false,
20826 /*friend_p=*/false);
20827 /* Check to see if there really was a declarator. */
20828 if (!cp_parser_parse_definitely (parser))
20829 abstract_declarator = NULL;
20831 if (type_specifier_seq.type
20832 /* The concepts TS allows 'auto' as a type-id. */
20833 && (!flag_concepts || parser->in_type_id_in_expr_p)
20834 /* None of the valid uses of 'auto' in C++14 involve the type-id
20835 nonterminal, but it is valid in a trailing-return-type. */
20836 && !(cxx_dialect >= cxx14 && is_trailing_return))
20837 if (tree auto_node = type_uses_auto (type_specifier_seq.type))
20839 /* A type-id with type 'auto' is only ok if the abstract declarator
20840 is a function declarator with a late-specified return type.
20842 A type-id with 'auto' is also valid in a trailing-return-type
20843 in a compound-requirement. */
20844 if (abstract_declarator
20845 && abstract_declarator->kind == cdk_function
20846 && abstract_declarator->u.function.late_return_type)
20847 /* OK */;
20848 else if (parser->in_result_type_constraint_p)
20849 /* OK */;
20850 else
20852 location_t loc = type_specifier_seq.locations[ds_type_spec];
20853 if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node))
20855 error_at (loc, "missing template arguments after %qT",
20856 auto_node);
20857 inform (DECL_SOURCE_LOCATION (tmpl), "%qD declared here",
20858 tmpl);
20860 else
20861 error_at (loc, "invalid use of %qT", auto_node);
20862 return error_mark_node;
20866 return groktypename (&type_specifier_seq, abstract_declarator,
20867 is_template_arg);
20870 static tree
20871 cp_parser_type_id (cp_parser *parser)
20873 return cp_parser_type_id_1 (parser, false, false);
20876 static tree
20877 cp_parser_template_type_arg (cp_parser *parser)
20879 tree r;
20880 const char *saved_message = parser->type_definition_forbidden_message;
20881 parser->type_definition_forbidden_message
20882 = G_("types may not be defined in template arguments");
20883 r = cp_parser_type_id_1 (parser, true, false);
20884 parser->type_definition_forbidden_message = saved_message;
20885 if (cxx_dialect >= cxx14 && !flag_concepts && type_uses_auto (r))
20887 error ("invalid use of %<auto%> in template argument");
20888 r = error_mark_node;
20890 return r;
20893 static tree
20894 cp_parser_trailing_type_id (cp_parser *parser)
20896 return cp_parser_type_id_1 (parser, false, true);
20899 /* Parse a type-specifier-seq.
20901 type-specifier-seq:
20902 type-specifier type-specifier-seq [opt]
20904 GNU extension:
20906 type-specifier-seq:
20907 attributes type-specifier-seq [opt]
20909 If IS_DECLARATION is true, we are at the start of a "condition" or
20910 exception-declaration, so we might be followed by a declarator-id.
20912 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
20913 i.e. we've just seen "->".
20915 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
20917 static void
20918 cp_parser_type_specifier_seq (cp_parser* parser,
20919 bool is_declaration,
20920 bool is_trailing_return,
20921 cp_decl_specifier_seq *type_specifier_seq)
20923 bool seen_type_specifier = false;
20924 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
20925 cp_token *start_token = NULL;
20927 /* Clear the TYPE_SPECIFIER_SEQ. */
20928 clear_decl_specs (type_specifier_seq);
20930 /* In the context of a trailing return type, enum E { } is an
20931 elaborated-type-specifier followed by a function-body, not an
20932 enum-specifier. */
20933 if (is_trailing_return)
20934 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
20936 /* Parse the type-specifiers and attributes. */
20937 while (true)
20939 tree type_specifier;
20940 bool is_cv_qualifier;
20942 /* Check for attributes first. */
20943 if (cp_next_tokens_can_be_attribute_p (parser))
20945 type_specifier_seq->attributes =
20946 chainon (type_specifier_seq->attributes,
20947 cp_parser_attributes_opt (parser));
20948 continue;
20951 /* record the token of the beginning of the type specifier seq,
20952 for error reporting purposes*/
20953 if (!start_token)
20954 start_token = cp_lexer_peek_token (parser->lexer);
20956 /* Look for the type-specifier. */
20957 type_specifier = cp_parser_type_specifier (parser,
20958 flags,
20959 type_specifier_seq,
20960 /*is_declaration=*/false,
20961 NULL,
20962 &is_cv_qualifier);
20963 if (!type_specifier)
20965 /* If the first type-specifier could not be found, this is not a
20966 type-specifier-seq at all. */
20967 if (!seen_type_specifier)
20969 /* Set in_declarator_p to avoid skipping to the semicolon. */
20970 int in_decl = parser->in_declarator_p;
20971 parser->in_declarator_p = true;
20973 if (cp_parser_uncommitted_to_tentative_parse_p (parser)
20974 || !cp_parser_parse_and_diagnose_invalid_type_name (parser))
20975 cp_parser_error (parser, "expected type-specifier");
20977 parser->in_declarator_p = in_decl;
20979 type_specifier_seq->type = error_mark_node;
20980 return;
20982 /* If subsequent type-specifiers could not be found, the
20983 type-specifier-seq is complete. */
20984 break;
20987 seen_type_specifier = true;
20988 /* The standard says that a condition can be:
20990 type-specifier-seq declarator = assignment-expression
20992 However, given:
20994 struct S {};
20995 if (int S = ...)
20997 we should treat the "S" as a declarator, not as a
20998 type-specifier. The standard doesn't say that explicitly for
20999 type-specifier-seq, but it does say that for
21000 decl-specifier-seq in an ordinary declaration. Perhaps it
21001 would be clearer just to allow a decl-specifier-seq here, and
21002 then add a semantic restriction that if any decl-specifiers
21003 that are not type-specifiers appear, the program is invalid. */
21004 if (is_declaration && !is_cv_qualifier)
21005 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
21009 /* Return whether the function currently being declared has an associated
21010 template parameter list. */
21012 static bool
21013 function_being_declared_is_template_p (cp_parser* parser)
21015 if (!current_template_parms || processing_template_parmlist)
21016 return false;
21018 if (parser->implicit_template_scope)
21019 return true;
21021 if (at_class_scope_p ()
21022 && TYPE_BEING_DEFINED (current_class_type))
21023 return parser->num_template_parameter_lists != 0;
21025 return ((int) parser->num_template_parameter_lists > template_class_depth
21026 (current_class_type));
21029 /* Parse a parameter-declaration-clause.
21031 parameter-declaration-clause:
21032 parameter-declaration-list [opt] ... [opt]
21033 parameter-declaration-list , ...
21035 Returns a representation for the parameter declarations. A return
21036 value of NULL indicates a parameter-declaration-clause consisting
21037 only of an ellipsis. */
21039 static tree
21040 cp_parser_parameter_declaration_clause (cp_parser* parser)
21042 tree parameters;
21043 cp_token *token;
21044 bool ellipsis_p;
21045 bool is_error;
21047 struct cleanup {
21048 cp_parser* parser;
21049 int auto_is_implicit_function_template_parm_p;
21050 ~cleanup() {
21051 parser->auto_is_implicit_function_template_parm_p
21052 = auto_is_implicit_function_template_parm_p;
21054 } cleanup = { parser, parser->auto_is_implicit_function_template_parm_p };
21056 (void) cleanup;
21058 if (!processing_specialization
21059 && !processing_template_parmlist
21060 && !processing_explicit_instantiation)
21061 if (!current_function_decl
21062 || (current_class_type && LAMBDA_TYPE_P (current_class_type)))
21063 parser->auto_is_implicit_function_template_parm_p = true;
21065 /* Peek at the next token. */
21066 token = cp_lexer_peek_token (parser->lexer);
21067 /* Check for trivial parameter-declaration-clauses. */
21068 if (token->type == CPP_ELLIPSIS)
21070 /* Consume the `...' token. */
21071 cp_lexer_consume_token (parser->lexer);
21072 return NULL_TREE;
21074 else if (token->type == CPP_CLOSE_PAREN)
21075 /* There are no parameters. */
21077 #ifndef NO_IMPLICIT_EXTERN_C
21078 if (in_system_header_at (input_location)
21079 && current_class_type == NULL
21080 && current_lang_name == lang_name_c)
21081 return NULL_TREE;
21082 else
21083 #endif
21084 return void_list_node;
21086 /* Check for `(void)', too, which is a special case. */
21087 else if (token->keyword == RID_VOID
21088 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
21089 == CPP_CLOSE_PAREN))
21091 /* Consume the `void' token. */
21092 cp_lexer_consume_token (parser->lexer);
21093 /* There are no parameters. */
21094 return void_list_node;
21097 /* Parse the parameter-declaration-list. */
21098 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
21099 /* If a parse error occurred while parsing the
21100 parameter-declaration-list, then the entire
21101 parameter-declaration-clause is erroneous. */
21102 if (is_error)
21103 return NULL;
21105 /* Peek at the next token. */
21106 token = cp_lexer_peek_token (parser->lexer);
21107 /* If it's a `,', the clause should terminate with an ellipsis. */
21108 if (token->type == CPP_COMMA)
21110 /* Consume the `,'. */
21111 cp_lexer_consume_token (parser->lexer);
21112 /* Expect an ellipsis. */
21113 ellipsis_p
21114 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
21116 /* It might also be `...' if the optional trailing `,' was
21117 omitted. */
21118 else if (token->type == CPP_ELLIPSIS)
21120 /* Consume the `...' token. */
21121 cp_lexer_consume_token (parser->lexer);
21122 /* And remember that we saw it. */
21123 ellipsis_p = true;
21125 else
21126 ellipsis_p = false;
21128 /* Finish the parameter list. */
21129 if (!ellipsis_p)
21130 parameters = chainon (parameters, void_list_node);
21132 return parameters;
21135 /* Parse a parameter-declaration-list.
21137 parameter-declaration-list:
21138 parameter-declaration
21139 parameter-declaration-list , parameter-declaration
21141 Returns a representation of the parameter-declaration-list, as for
21142 cp_parser_parameter_declaration_clause. However, the
21143 `void_list_node' is never appended to the list. Upon return,
21144 *IS_ERROR will be true iff an error occurred. */
21146 static tree
21147 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
21149 tree parameters = NULL_TREE;
21150 tree *tail = &parameters;
21151 bool saved_in_unbraced_linkage_specification_p;
21152 int index = 0;
21154 /* Assume all will go well. */
21155 *is_error = false;
21156 /* The special considerations that apply to a function within an
21157 unbraced linkage specifications do not apply to the parameters
21158 to the function. */
21159 saved_in_unbraced_linkage_specification_p
21160 = parser->in_unbraced_linkage_specification_p;
21161 parser->in_unbraced_linkage_specification_p = false;
21163 /* Look for more parameters. */
21164 while (true)
21166 cp_parameter_declarator *parameter;
21167 tree decl = error_mark_node;
21168 bool parenthesized_p = false;
21169 int template_parm_idx = (function_being_declared_is_template_p (parser)?
21170 TREE_VEC_LENGTH (INNERMOST_TEMPLATE_PARMS
21171 (current_template_parms)) : 0);
21173 /* Parse the parameter. */
21174 parameter
21175 = cp_parser_parameter_declaration (parser,
21176 /*template_parm_p=*/false,
21177 &parenthesized_p);
21179 /* We don't know yet if the enclosing context is deprecated, so wait
21180 and warn in grokparms if appropriate. */
21181 deprecated_state = DEPRECATED_SUPPRESS;
21183 if (parameter)
21185 /* If a function parameter pack was specified and an implicit template
21186 parameter was introduced during cp_parser_parameter_declaration,
21187 change any implicit parameters introduced into packs. */
21188 if (parser->implicit_template_parms
21189 && parameter->declarator
21190 && parameter->declarator->parameter_pack_p)
21192 int latest_template_parm_idx = TREE_VEC_LENGTH
21193 (INNERMOST_TEMPLATE_PARMS (current_template_parms));
21195 if (latest_template_parm_idx != template_parm_idx)
21196 parameter->decl_specifiers.type = convert_generic_types_to_packs
21197 (parameter->decl_specifiers.type,
21198 template_parm_idx, latest_template_parm_idx);
21201 decl = grokdeclarator (parameter->declarator,
21202 &parameter->decl_specifiers,
21203 PARM,
21204 parameter->default_argument != NULL_TREE,
21205 &parameter->decl_specifiers.attributes);
21208 deprecated_state = DEPRECATED_NORMAL;
21210 /* If a parse error occurred parsing the parameter declaration,
21211 then the entire parameter-declaration-list is erroneous. */
21212 if (decl == error_mark_node)
21214 *is_error = true;
21215 parameters = error_mark_node;
21216 break;
21219 if (parameter->decl_specifiers.attributes)
21220 cplus_decl_attributes (&decl,
21221 parameter->decl_specifiers.attributes,
21223 if (DECL_NAME (decl))
21224 decl = pushdecl (decl);
21226 if (decl != error_mark_node)
21228 retrofit_lang_decl (decl);
21229 DECL_PARM_INDEX (decl) = ++index;
21230 DECL_PARM_LEVEL (decl) = function_parm_depth ();
21233 /* Add the new parameter to the list. */
21234 *tail = build_tree_list (parameter->default_argument, decl);
21235 tail = &TREE_CHAIN (*tail);
21237 /* Peek at the next token. */
21238 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
21239 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
21240 /* These are for Objective-C++ */
21241 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21242 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21243 /* The parameter-declaration-list is complete. */
21244 break;
21245 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21247 cp_token *token;
21249 /* Peek at the next token. */
21250 token = cp_lexer_peek_nth_token (parser->lexer, 2);
21251 /* If it's an ellipsis, then the list is complete. */
21252 if (token->type == CPP_ELLIPSIS)
21253 break;
21254 /* Otherwise, there must be more parameters. Consume the
21255 `,'. */
21256 cp_lexer_consume_token (parser->lexer);
21257 /* When parsing something like:
21259 int i(float f, double d)
21261 we can tell after seeing the declaration for "f" that we
21262 are not looking at an initialization of a variable "i",
21263 but rather at the declaration of a function "i".
21265 Due to the fact that the parsing of template arguments
21266 (as specified to a template-id) requires backtracking we
21267 cannot use this technique when inside a template argument
21268 list. */
21269 if (!parser->in_template_argument_list_p
21270 && !parser->in_type_id_in_expr_p
21271 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21272 /* However, a parameter-declaration of the form
21273 "float(f)" (which is a valid declaration of a
21274 parameter "f") can also be interpreted as an
21275 expression (the conversion of "f" to "float"). */
21276 && !parenthesized_p)
21277 cp_parser_commit_to_tentative_parse (parser);
21279 else
21281 cp_parser_error (parser, "expected %<,%> or %<...%>");
21282 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
21283 cp_parser_skip_to_closing_parenthesis (parser,
21284 /*recovering=*/true,
21285 /*or_comma=*/false,
21286 /*consume_paren=*/false);
21287 break;
21291 parser->in_unbraced_linkage_specification_p
21292 = saved_in_unbraced_linkage_specification_p;
21294 /* Reset implicit_template_scope if we are about to leave the function
21295 parameter list that introduced it. Note that for out-of-line member
21296 definitions, there will be one or more class scopes before we get to
21297 the template parameter scope. */
21299 if (cp_binding_level *its = parser->implicit_template_scope)
21300 if (cp_binding_level *maybe_its = current_binding_level->level_chain)
21302 while (maybe_its->kind == sk_class)
21303 maybe_its = maybe_its->level_chain;
21304 if (maybe_its == its)
21306 parser->implicit_template_parms = 0;
21307 parser->implicit_template_scope = 0;
21311 return parameters;
21314 /* Parse a parameter declaration.
21316 parameter-declaration:
21317 decl-specifier-seq ... [opt] declarator
21318 decl-specifier-seq declarator = assignment-expression
21319 decl-specifier-seq ... [opt] abstract-declarator [opt]
21320 decl-specifier-seq abstract-declarator [opt] = assignment-expression
21322 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
21323 declares a template parameter. (In that case, a non-nested `>'
21324 token encountered during the parsing of the assignment-expression
21325 is not interpreted as a greater-than operator.)
21327 Returns a representation of the parameter, or NULL if an error
21328 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
21329 true iff the declarator is of the form "(p)". */
21331 static cp_parameter_declarator *
21332 cp_parser_parameter_declaration (cp_parser *parser,
21333 bool template_parm_p,
21334 bool *parenthesized_p)
21336 int declares_class_or_enum;
21337 cp_decl_specifier_seq decl_specifiers;
21338 cp_declarator *declarator;
21339 tree default_argument;
21340 cp_token *token = NULL, *declarator_token_start = NULL;
21341 const char *saved_message;
21342 bool template_parameter_pack_p = false;
21344 /* In a template parameter, `>' is not an operator.
21346 [temp.param]
21348 When parsing a default template-argument for a non-type
21349 template-parameter, the first non-nested `>' is taken as the end
21350 of the template parameter-list rather than a greater-than
21351 operator. */
21353 /* Type definitions may not appear in parameter types. */
21354 saved_message = parser->type_definition_forbidden_message;
21355 parser->type_definition_forbidden_message
21356 = G_("types may not be defined in parameter types");
21358 /* Parse the declaration-specifiers. */
21359 cp_parser_decl_specifier_seq (parser,
21360 CP_PARSER_FLAGS_NONE,
21361 &decl_specifiers,
21362 &declares_class_or_enum);
21364 /* Complain about missing 'typename' or other invalid type names. */
21365 if (!decl_specifiers.any_type_specifiers_p
21366 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21367 decl_specifiers.type = error_mark_node;
21369 /* If an error occurred, there's no reason to attempt to parse the
21370 rest of the declaration. */
21371 if (cp_parser_error_occurred (parser))
21373 parser->type_definition_forbidden_message = saved_message;
21374 return NULL;
21377 /* Peek at the next token. */
21378 token = cp_lexer_peek_token (parser->lexer);
21380 /* If the next token is a `)', `,', `=', `>', or `...', then there
21381 is no declarator. However, when variadic templates are enabled,
21382 there may be a declarator following `...'. */
21383 if (token->type == CPP_CLOSE_PAREN
21384 || token->type == CPP_COMMA
21385 || token->type == CPP_EQ
21386 || token->type == CPP_GREATER)
21388 declarator = NULL;
21389 if (parenthesized_p)
21390 *parenthesized_p = false;
21392 /* Otherwise, there should be a declarator. */
21393 else
21395 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
21396 parser->default_arg_ok_p = false;
21398 /* After seeing a decl-specifier-seq, if the next token is not a
21399 "(", there is no possibility that the code is a valid
21400 expression. Therefore, if parsing tentatively, we commit at
21401 this point. */
21402 if (!parser->in_template_argument_list_p
21403 /* In an expression context, having seen:
21405 (int((char ...
21407 we cannot be sure whether we are looking at a
21408 function-type (taking a "char" as a parameter) or a cast
21409 of some object of type "char" to "int". */
21410 && !parser->in_type_id_in_expr_p
21411 && cp_parser_uncommitted_to_tentative_parse_p (parser)
21412 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21413 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
21414 cp_parser_commit_to_tentative_parse (parser);
21415 /* Parse the declarator. */
21416 declarator_token_start = token;
21417 declarator = cp_parser_declarator (parser,
21418 CP_PARSER_DECLARATOR_EITHER,
21419 /*ctor_dtor_or_conv_p=*/NULL,
21420 parenthesized_p,
21421 /*member_p=*/false,
21422 /*friend_p=*/false);
21423 parser->default_arg_ok_p = saved_default_arg_ok_p;
21424 /* After the declarator, allow more attributes. */
21425 decl_specifiers.attributes
21426 = chainon (decl_specifiers.attributes,
21427 cp_parser_attributes_opt (parser));
21429 /* If the declarator is a template parameter pack, remember that and
21430 clear the flag in the declarator itself so we don't get errors
21431 from grokdeclarator. */
21432 if (template_parm_p && declarator && declarator->parameter_pack_p)
21434 declarator->parameter_pack_p = false;
21435 template_parameter_pack_p = true;
21439 /* If the next token is an ellipsis, and we have not seen a declarator
21440 name, and if either the type of the declarator contains parameter
21441 packs but it is not a TYPE_PACK_EXPANSION or is null (this happens
21442 for, eg, abbreviated integral type names), then we actually have a
21443 parameter pack expansion expression. Otherwise, leave the ellipsis
21444 for a C-style variadic function. */
21445 token = cp_lexer_peek_token (parser->lexer);
21446 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21448 tree type = decl_specifiers.type;
21450 if (type && DECL_P (type))
21451 type = TREE_TYPE (type);
21453 if (((type
21454 && TREE_CODE (type) != TYPE_PACK_EXPANSION
21455 && (template_parm_p || uses_parameter_packs (type)))
21456 || (!type && template_parm_p))
21457 && declarator_can_be_parameter_pack (declarator))
21459 /* Consume the `...'. */
21460 cp_lexer_consume_token (parser->lexer);
21461 maybe_warn_variadic_templates ();
21463 /* Build a pack expansion type */
21464 if (template_parm_p)
21465 template_parameter_pack_p = true;
21466 else if (declarator)
21467 declarator->parameter_pack_p = true;
21468 else
21469 decl_specifiers.type = make_pack_expansion (type);
21473 /* The restriction on defining new types applies only to the type
21474 of the parameter, not to the default argument. */
21475 parser->type_definition_forbidden_message = saved_message;
21477 /* If the next token is `=', then process a default argument. */
21478 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
21480 tree type = decl_specifiers.type;
21481 token = cp_lexer_peek_token (parser->lexer);
21482 /* If we are defining a class, then the tokens that make up the
21483 default argument must be saved and processed later. */
21484 if (!template_parm_p && at_class_scope_p ()
21485 && TYPE_BEING_DEFINED (current_class_type)
21486 && !LAMBDA_TYPE_P (current_class_type))
21487 default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
21489 // A constrained-type-specifier may declare a type template-parameter.
21490 else if (declares_constrained_type_template_parameter (type))
21491 default_argument
21492 = cp_parser_default_type_template_argument (parser);
21494 // A constrained-type-specifier may declare a template-template-parameter.
21495 else if (declares_constrained_template_template_parameter (type))
21496 default_argument
21497 = cp_parser_default_template_template_argument (parser);
21499 /* Outside of a class definition, we can just parse the
21500 assignment-expression. */
21501 else
21502 default_argument
21503 = cp_parser_default_argument (parser, template_parm_p);
21505 if (!parser->default_arg_ok_p)
21507 permerror (token->location,
21508 "default arguments are only "
21509 "permitted for function parameters");
21511 else if ((declarator && declarator->parameter_pack_p)
21512 || template_parameter_pack_p
21513 || (decl_specifiers.type
21514 && PACK_EXPANSION_P (decl_specifiers.type)))
21516 /* Find the name of the parameter pack. */
21517 cp_declarator *id_declarator = declarator;
21518 while (id_declarator && id_declarator->kind != cdk_id)
21519 id_declarator = id_declarator->declarator;
21521 if (id_declarator && id_declarator->kind == cdk_id)
21522 error_at (declarator_token_start->location,
21523 template_parm_p
21524 ? G_("template parameter pack %qD "
21525 "cannot have a default argument")
21526 : G_("parameter pack %qD cannot have "
21527 "a default argument"),
21528 id_declarator->u.id.unqualified_name);
21529 else
21530 error_at (declarator_token_start->location,
21531 template_parm_p
21532 ? G_("template parameter pack cannot have "
21533 "a default argument")
21534 : G_("parameter pack cannot have a "
21535 "default argument"));
21537 default_argument = NULL_TREE;
21540 else
21541 default_argument = NULL_TREE;
21543 return make_parameter_declarator (&decl_specifiers,
21544 declarator,
21545 default_argument,
21546 template_parameter_pack_p);
21549 /* Parse a default argument and return it.
21551 TEMPLATE_PARM_P is true if this is a default argument for a
21552 non-type template parameter. */
21553 static tree
21554 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
21556 tree default_argument = NULL_TREE;
21557 bool saved_greater_than_is_operator_p;
21558 bool saved_local_variables_forbidden_p;
21559 bool non_constant_p, is_direct_init;
21561 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
21562 set correctly. */
21563 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
21564 parser->greater_than_is_operator_p = !template_parm_p;
21565 /* Local variable names (and the `this' keyword) may not
21566 appear in a default argument. */
21567 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21568 parser->local_variables_forbidden_p = true;
21569 /* Parse the assignment-expression. */
21570 if (template_parm_p)
21571 push_deferring_access_checks (dk_no_deferred);
21572 tree saved_class_ptr = NULL_TREE;
21573 tree saved_class_ref = NULL_TREE;
21574 /* The "this" pointer is not valid in a default argument. */
21575 if (cfun)
21577 saved_class_ptr = current_class_ptr;
21578 cp_function_chain->x_current_class_ptr = NULL_TREE;
21579 saved_class_ref = current_class_ref;
21580 cp_function_chain->x_current_class_ref = NULL_TREE;
21582 default_argument
21583 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
21584 /* Restore the "this" pointer. */
21585 if (cfun)
21587 cp_function_chain->x_current_class_ptr = saved_class_ptr;
21588 cp_function_chain->x_current_class_ref = saved_class_ref;
21590 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
21591 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21592 if (template_parm_p)
21593 pop_deferring_access_checks ();
21594 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
21595 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21597 return default_argument;
21600 /* Parse a function-body.
21602 function-body:
21603 compound_statement */
21605 static void
21606 cp_parser_function_body (cp_parser *parser, bool in_function_try_block)
21608 cp_parser_compound_statement (parser, NULL, (in_function_try_block
21609 ? BCS_TRY_BLOCK : BCS_NORMAL),
21610 true);
21613 /* Parse a ctor-initializer-opt followed by a function-body. Return
21614 true if a ctor-initializer was present. When IN_FUNCTION_TRY_BLOCK
21615 is true we are parsing a function-try-block. */
21617 static bool
21618 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser,
21619 bool in_function_try_block)
21621 tree body, list;
21622 bool ctor_initializer_p;
21623 const bool check_body_p =
21624 DECL_CONSTRUCTOR_P (current_function_decl)
21625 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
21626 tree last = NULL;
21628 /* Begin the function body. */
21629 body = begin_function_body ();
21630 /* Parse the optional ctor-initializer. */
21631 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
21633 /* If we're parsing a constexpr constructor definition, we need
21634 to check that the constructor body is indeed empty. However,
21635 before we get to cp_parser_function_body lot of junk has been
21636 generated, so we can't just check that we have an empty block.
21637 Rather we take a snapshot of the outermost block, and check whether
21638 cp_parser_function_body changed its state. */
21639 if (check_body_p)
21641 list = cur_stmt_list;
21642 if (STATEMENT_LIST_TAIL (list))
21643 last = STATEMENT_LIST_TAIL (list)->stmt;
21645 /* Parse the function-body. */
21646 cp_parser_function_body (parser, in_function_try_block);
21647 if (check_body_p)
21648 check_constexpr_ctor_body (last, list, /*complain=*/true);
21649 /* Finish the function body. */
21650 finish_function_body (body);
21652 return ctor_initializer_p;
21655 /* Parse an initializer.
21657 initializer:
21658 = initializer-clause
21659 ( expression-list )
21661 Returns an expression representing the initializer. If no
21662 initializer is present, NULL_TREE is returned.
21664 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
21665 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
21666 set to TRUE if there is no initializer present. If there is an
21667 initializer, and it is not a constant-expression, *NON_CONSTANT_P
21668 is set to true; otherwise it is set to false. */
21670 static tree
21671 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
21672 bool* non_constant_p)
21674 cp_token *token;
21675 tree init;
21677 /* Peek at the next token. */
21678 token = cp_lexer_peek_token (parser->lexer);
21680 /* Let our caller know whether or not this initializer was
21681 parenthesized. */
21682 *is_direct_init = (token->type != CPP_EQ);
21683 /* Assume that the initializer is constant. */
21684 *non_constant_p = false;
21686 if (token->type == CPP_EQ)
21688 /* Consume the `='. */
21689 cp_lexer_consume_token (parser->lexer);
21690 /* Parse the initializer-clause. */
21691 init = cp_parser_initializer_clause (parser, non_constant_p);
21693 else if (token->type == CPP_OPEN_PAREN)
21695 vec<tree, va_gc> *vec;
21696 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21697 /*cast_p=*/false,
21698 /*allow_expansion_p=*/true,
21699 non_constant_p);
21700 if (vec == NULL)
21701 return error_mark_node;
21702 init = build_tree_list_vec (vec);
21703 release_tree_vector (vec);
21705 else if (token->type == CPP_OPEN_BRACE)
21707 cp_lexer_set_source_position (parser->lexer);
21708 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21709 init = cp_parser_braced_list (parser, non_constant_p);
21710 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
21712 else
21714 /* Anything else is an error. */
21715 cp_parser_error (parser, "expected initializer");
21716 init = error_mark_node;
21719 if (check_for_bare_parameter_packs (init))
21720 init = error_mark_node;
21722 return init;
21725 /* Parse an initializer-clause.
21727 initializer-clause:
21728 assignment-expression
21729 braced-init-list
21731 Returns an expression representing the initializer.
21733 If the `assignment-expression' production is used the value
21734 returned is simply a representation for the expression.
21736 Otherwise, calls cp_parser_braced_list. */
21738 static cp_expr
21739 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
21741 cp_expr initializer;
21743 /* Assume the expression is constant. */
21744 *non_constant_p = false;
21746 /* If it is not a `{', then we are looking at an
21747 assignment-expression. */
21748 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
21750 initializer
21751 = cp_parser_constant_expression (parser,
21752 /*allow_non_constant_p=*/true,
21753 non_constant_p);
21755 else
21756 initializer = cp_parser_braced_list (parser, non_constant_p);
21758 return initializer;
21761 /* Parse a brace-enclosed initializer list.
21763 braced-init-list:
21764 { initializer-list , [opt] }
21767 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
21768 the elements of the initializer-list (or NULL, if the last
21769 production is used). The TREE_TYPE for the CONSTRUCTOR will be
21770 NULL_TREE. There is no way to detect whether or not the optional
21771 trailing `,' was provided. NON_CONSTANT_P is as for
21772 cp_parser_initializer. */
21774 static cp_expr
21775 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
21777 tree initializer;
21778 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
21780 /* Consume the `{' token. */
21781 cp_lexer_consume_token (parser->lexer);
21782 /* Create a CONSTRUCTOR to represent the braced-initializer. */
21783 initializer = make_node (CONSTRUCTOR);
21784 /* If it's not a `}', then there is a non-trivial initializer. */
21785 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
21787 /* Parse the initializer list. */
21788 CONSTRUCTOR_ELTS (initializer)
21789 = cp_parser_initializer_list (parser, non_constant_p);
21790 /* A trailing `,' token is allowed. */
21791 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21792 cp_lexer_consume_token (parser->lexer);
21794 else
21795 *non_constant_p = false;
21796 /* Now, there should be a trailing `}'. */
21797 location_t finish_loc = cp_lexer_peek_token (parser->lexer)->location;
21798 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
21799 TREE_TYPE (initializer) = init_list_type_node;
21801 cp_expr result (initializer);
21802 /* Build a location of the form:
21803 { ... }
21804 ^~~~~~~
21805 with caret==start at the open brace, finish at the close brace. */
21806 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
21807 result.set_location (combined_loc);
21808 return result;
21811 /* Consume tokens up to, and including, the next non-nested closing `]'.
21812 Returns true iff we found a closing `]'. */
21814 static bool
21815 cp_parser_skip_to_closing_square_bracket (cp_parser *parser)
21817 unsigned square_depth = 0;
21819 while (true)
21821 cp_token * token = cp_lexer_peek_token (parser->lexer);
21823 switch (token->type)
21825 case CPP_EOF:
21826 case CPP_PRAGMA_EOL:
21827 /* If we've run out of tokens, then there is no closing `]'. */
21828 return false;
21830 case CPP_OPEN_SQUARE:
21831 ++square_depth;
21832 break;
21834 case CPP_CLOSE_SQUARE:
21835 if (!square_depth--)
21837 cp_lexer_consume_token (parser->lexer);
21838 return true;
21840 break;
21842 default:
21843 break;
21846 /* Consume the token. */
21847 cp_lexer_consume_token (parser->lexer);
21851 /* Return true if we are looking at an array-designator, false otherwise. */
21853 static bool
21854 cp_parser_array_designator_p (cp_parser *parser)
21856 /* Consume the `['. */
21857 cp_lexer_consume_token (parser->lexer);
21859 cp_lexer_save_tokens (parser->lexer);
21861 /* Skip tokens until the next token is a closing square bracket.
21862 If we find the closing `]', and the next token is a `=', then
21863 we are looking at an array designator. */
21864 bool array_designator_p
21865 = (cp_parser_skip_to_closing_square_bracket (parser)
21866 && cp_lexer_next_token_is (parser->lexer, CPP_EQ));
21868 /* Roll back the tokens we skipped. */
21869 cp_lexer_rollback_tokens (parser->lexer);
21871 return array_designator_p;
21874 /* Parse an initializer-list.
21876 initializer-list:
21877 initializer-clause ... [opt]
21878 initializer-list , initializer-clause ... [opt]
21880 GNU Extension:
21882 initializer-list:
21883 designation initializer-clause ...[opt]
21884 initializer-list , designation initializer-clause ...[opt]
21886 designation:
21887 . identifier =
21888 identifier :
21889 [ constant-expression ] =
21891 Returns a vec of constructor_elt. The VALUE of each elt is an expression
21892 for the initializer. If the INDEX of the elt is non-NULL, it is the
21893 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
21894 as for cp_parser_initializer. */
21896 static vec<constructor_elt, va_gc> *
21897 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
21899 vec<constructor_elt, va_gc> *v = NULL;
21901 /* Assume all of the expressions are constant. */
21902 *non_constant_p = false;
21904 /* Parse the rest of the list. */
21905 while (true)
21907 cp_token *token;
21908 tree designator;
21909 tree initializer;
21910 bool clause_non_constant_p;
21912 /* If the next token is an identifier and the following one is a
21913 colon, we are looking at the GNU designated-initializer
21914 syntax. */
21915 if (cp_parser_allow_gnu_extensions_p (parser)
21916 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
21917 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
21919 /* Warn the user that they are using an extension. */
21920 pedwarn (input_location, OPT_Wpedantic,
21921 "ISO C++ does not allow designated initializers");
21922 /* Consume the identifier. */
21923 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21924 /* Consume the `:'. */
21925 cp_lexer_consume_token (parser->lexer);
21927 /* Also handle the C99 syntax, '. id ='. */
21928 else if (cp_parser_allow_gnu_extensions_p (parser)
21929 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
21930 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
21931 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
21933 /* Warn the user that they are using an extension. */
21934 pedwarn (input_location, OPT_Wpedantic,
21935 "ISO C++ does not allow C99 designated initializers");
21936 /* Consume the `.'. */
21937 cp_lexer_consume_token (parser->lexer);
21938 /* Consume the identifier. */
21939 designator = cp_lexer_consume_token (parser->lexer)->u.value;
21940 /* Consume the `='. */
21941 cp_lexer_consume_token (parser->lexer);
21943 /* Also handle C99 array designators, '[ const ] ='. */
21944 else if (cp_parser_allow_gnu_extensions_p (parser)
21945 && !c_dialect_objc ()
21946 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
21948 /* In C++11, [ could start a lambda-introducer. */
21949 bool non_const = false;
21951 cp_parser_parse_tentatively (parser);
21953 if (!cp_parser_array_designator_p (parser))
21955 cp_parser_simulate_error (parser);
21956 designator = NULL_TREE;
21958 else
21960 designator = cp_parser_constant_expression (parser, true,
21961 &non_const);
21962 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21963 cp_parser_require (parser, CPP_EQ, RT_EQ);
21966 if (!cp_parser_parse_definitely (parser))
21967 designator = NULL_TREE;
21968 else if (non_const)
21969 require_potential_rvalue_constant_expression (designator);
21971 else
21972 designator = NULL_TREE;
21974 /* Parse the initializer. */
21975 initializer = cp_parser_initializer_clause (parser,
21976 &clause_non_constant_p);
21977 /* If any clause is non-constant, so is the entire initializer. */
21978 if (clause_non_constant_p)
21979 *non_constant_p = true;
21981 /* If we have an ellipsis, this is an initializer pack
21982 expansion. */
21983 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21985 /* Consume the `...'. */
21986 cp_lexer_consume_token (parser->lexer);
21988 /* Turn the initializer into an initializer expansion. */
21989 initializer = make_pack_expansion (initializer);
21992 /* Add it to the vector. */
21993 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
21995 /* If the next token is not a comma, we have reached the end of
21996 the list. */
21997 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21998 break;
22000 /* Peek at the next token. */
22001 token = cp_lexer_peek_nth_token (parser->lexer, 2);
22002 /* If the next token is a `}', then we're still done. An
22003 initializer-clause can have a trailing `,' after the
22004 initializer-list and before the closing `}'. */
22005 if (token->type == CPP_CLOSE_BRACE)
22006 break;
22008 /* Consume the `,' token. */
22009 cp_lexer_consume_token (parser->lexer);
22012 return v;
22015 /* Classes [gram.class] */
22017 /* Parse a class-name.
22019 class-name:
22020 identifier
22021 template-id
22023 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
22024 to indicate that names looked up in dependent types should be
22025 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
22026 keyword has been used to indicate that the name that appears next
22027 is a template. TAG_TYPE indicates the explicit tag given before
22028 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
22029 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
22030 is the class being defined in a class-head. If ENUM_OK is TRUE,
22031 enum-names are also accepted.
22033 Returns the TYPE_DECL representing the class. */
22035 static tree
22036 cp_parser_class_name (cp_parser *parser,
22037 bool typename_keyword_p,
22038 bool template_keyword_p,
22039 enum tag_types tag_type,
22040 bool check_dependency_p,
22041 bool class_head_p,
22042 bool is_declaration,
22043 bool enum_ok)
22045 tree decl;
22046 tree scope;
22047 bool typename_p;
22048 cp_token *token;
22049 tree identifier = NULL_TREE;
22051 /* All class-names start with an identifier. */
22052 token = cp_lexer_peek_token (parser->lexer);
22053 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
22055 cp_parser_error (parser, "expected class-name");
22056 return error_mark_node;
22059 /* PARSER->SCOPE can be cleared when parsing the template-arguments
22060 to a template-id, so we save it here. */
22061 scope = parser->scope;
22062 if (scope == error_mark_node)
22063 return error_mark_node;
22065 /* Any name names a type if we're following the `typename' keyword
22066 in a qualified name where the enclosing scope is type-dependent. */
22067 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
22068 && dependent_type_p (scope));
22069 /* Handle the common case (an identifier, but not a template-id)
22070 efficiently. */
22071 if (token->type == CPP_NAME
22072 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
22074 cp_token *identifier_token;
22075 bool ambiguous_p;
22077 /* Look for the identifier. */
22078 identifier_token = cp_lexer_peek_token (parser->lexer);
22079 ambiguous_p = identifier_token->error_reported;
22080 identifier = cp_parser_identifier (parser);
22081 /* If the next token isn't an identifier, we are certainly not
22082 looking at a class-name. */
22083 if (identifier == error_mark_node)
22084 decl = error_mark_node;
22085 /* If we know this is a type-name, there's no need to look it
22086 up. */
22087 else if (typename_p)
22088 decl = identifier;
22089 else
22091 tree ambiguous_decls;
22092 /* If we already know that this lookup is ambiguous, then
22093 we've already issued an error message; there's no reason
22094 to check again. */
22095 if (ambiguous_p)
22097 cp_parser_simulate_error (parser);
22098 return error_mark_node;
22100 /* If the next token is a `::', then the name must be a type
22101 name.
22103 [basic.lookup.qual]
22105 During the lookup for a name preceding the :: scope
22106 resolution operator, object, function, and enumerator
22107 names are ignored. */
22108 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22109 tag_type = scope_type;
22110 /* Look up the name. */
22111 decl = cp_parser_lookup_name (parser, identifier,
22112 tag_type,
22113 /*is_template=*/false,
22114 /*is_namespace=*/false,
22115 check_dependency_p,
22116 &ambiguous_decls,
22117 identifier_token->location);
22118 if (ambiguous_decls)
22120 if (cp_parser_parsing_tentatively (parser))
22121 cp_parser_simulate_error (parser);
22122 return error_mark_node;
22126 else
22128 /* Try a template-id. */
22129 decl = cp_parser_template_id (parser, template_keyword_p,
22130 check_dependency_p,
22131 tag_type,
22132 is_declaration);
22133 if (decl == error_mark_node)
22134 return error_mark_node;
22137 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
22139 /* If this is a typename, create a TYPENAME_TYPE. */
22140 if (typename_p && decl != error_mark_node)
22142 decl = make_typename_type (scope, decl, typename_type,
22143 /*complain=*/tf_error);
22144 if (decl != error_mark_node)
22145 decl = TYPE_NAME (decl);
22148 decl = strip_using_decl (decl);
22150 /* Check to see that it is really the name of a class. */
22151 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
22152 && identifier_p (TREE_OPERAND (decl, 0))
22153 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
22154 /* Situations like this:
22156 template <typename T> struct A {
22157 typename T::template X<int>::I i;
22160 are problematic. Is `T::template X<int>' a class-name? The
22161 standard does not seem to be definitive, but there is no other
22162 valid interpretation of the following `::'. Therefore, those
22163 names are considered class-names. */
22165 decl = make_typename_type (scope, decl, tag_type, tf_error);
22166 if (decl != error_mark_node)
22167 decl = TYPE_NAME (decl);
22169 else if (TREE_CODE (decl) != TYPE_DECL
22170 || TREE_TYPE (decl) == error_mark_node
22171 || !(MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
22172 || (enum_ok && TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE))
22173 /* In Objective-C 2.0, a classname followed by '.' starts a
22174 dot-syntax expression, and it's not a type-name. */
22175 || (c_dialect_objc ()
22176 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
22177 && objc_is_class_name (decl)))
22178 decl = error_mark_node;
22180 if (decl == error_mark_node)
22181 cp_parser_error (parser, "expected class-name");
22182 else if (identifier && !parser->scope)
22183 maybe_note_name_used_in_class (identifier, decl);
22185 return decl;
22188 /* Parse a class-specifier.
22190 class-specifier:
22191 class-head { member-specification [opt] }
22193 Returns the TREE_TYPE representing the class. */
22195 static tree
22196 cp_parser_class_specifier_1 (cp_parser* parser)
22198 tree type;
22199 tree attributes = NULL_TREE;
22200 bool nested_name_specifier_p;
22201 unsigned saved_num_template_parameter_lists;
22202 bool saved_in_function_body;
22203 unsigned char in_statement;
22204 bool in_switch_statement_p;
22205 bool saved_in_unbraced_linkage_specification_p;
22206 tree old_scope = NULL_TREE;
22207 tree scope = NULL_TREE;
22208 cp_token *closing_brace;
22210 push_deferring_access_checks (dk_no_deferred);
22212 /* Parse the class-head. */
22213 type = cp_parser_class_head (parser,
22214 &nested_name_specifier_p);
22215 /* If the class-head was a semantic disaster, skip the entire body
22216 of the class. */
22217 if (!type)
22219 cp_parser_skip_to_end_of_block_or_statement (parser);
22220 pop_deferring_access_checks ();
22221 return error_mark_node;
22224 /* Look for the `{'. */
22225 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
22227 pop_deferring_access_checks ();
22228 return error_mark_node;
22231 cp_ensure_no_omp_declare_simd (parser);
22232 cp_ensure_no_oacc_routine (parser);
22234 /* Issue an error message if type-definitions are forbidden here. */
22235 cp_parser_check_type_definition (parser);
22236 /* Remember that we are defining one more class. */
22237 ++parser->num_classes_being_defined;
22238 /* Inside the class, surrounding template-parameter-lists do not
22239 apply. */
22240 saved_num_template_parameter_lists
22241 = parser->num_template_parameter_lists;
22242 parser->num_template_parameter_lists = 0;
22243 /* We are not in a function body. */
22244 saved_in_function_body = parser->in_function_body;
22245 parser->in_function_body = false;
22246 /* Or in a loop. */
22247 in_statement = parser->in_statement;
22248 parser->in_statement = 0;
22249 /* Or in a switch. */
22250 in_switch_statement_p = parser->in_switch_statement_p;
22251 parser->in_switch_statement_p = false;
22252 /* We are not immediately inside an extern "lang" block. */
22253 saved_in_unbraced_linkage_specification_p
22254 = parser->in_unbraced_linkage_specification_p;
22255 parser->in_unbraced_linkage_specification_p = false;
22257 // Associate constraints with the type.
22258 if (flag_concepts)
22259 type = associate_classtype_constraints (type);
22261 /* Start the class. */
22262 if (nested_name_specifier_p)
22264 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
22265 old_scope = push_inner_scope (scope);
22267 type = begin_class_definition (type);
22269 if (type == error_mark_node)
22270 /* If the type is erroneous, skip the entire body of the class. */
22271 cp_parser_skip_to_closing_brace (parser);
22272 else
22273 /* Parse the member-specification. */
22274 cp_parser_member_specification_opt (parser);
22276 /* Look for the trailing `}'. */
22277 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
22278 /* Look for trailing attributes to apply to this class. */
22279 if (cp_parser_allow_gnu_extensions_p (parser))
22280 attributes = cp_parser_gnu_attributes_opt (parser);
22281 if (type != error_mark_node)
22282 type = finish_struct (type, attributes);
22283 if (nested_name_specifier_p)
22284 pop_inner_scope (old_scope, scope);
22286 /* We've finished a type definition. Check for the common syntax
22287 error of forgetting a semicolon after the definition. We need to
22288 be careful, as we can't just check for not-a-semicolon and be done
22289 with it; the user might have typed:
22291 class X { } c = ...;
22292 class X { } *p = ...;
22294 and so forth. Instead, enumerate all the possible tokens that
22295 might follow this production; if we don't see one of them, then
22296 complain and silently insert the semicolon. */
22298 cp_token *token = cp_lexer_peek_token (parser->lexer);
22299 bool want_semicolon = true;
22301 if (cp_next_tokens_can_be_std_attribute_p (parser))
22302 /* Don't try to parse c++11 attributes here. As per the
22303 grammar, that should be a task for
22304 cp_parser_decl_specifier_seq. */
22305 want_semicolon = false;
22307 switch (token->type)
22309 case CPP_NAME:
22310 case CPP_SEMICOLON:
22311 case CPP_MULT:
22312 case CPP_AND:
22313 case CPP_OPEN_PAREN:
22314 case CPP_CLOSE_PAREN:
22315 case CPP_COMMA:
22316 want_semicolon = false;
22317 break;
22319 /* While it's legal for type qualifiers and storage class
22320 specifiers to follow type definitions in the grammar, only
22321 compiler testsuites contain code like that. Assume that if
22322 we see such code, then what we're really seeing is a case
22323 like:
22325 class X { }
22326 const <type> var = ...;
22330 class Y { }
22331 static <type> func (...) ...
22333 i.e. the qualifier or specifier applies to the next
22334 declaration. To do so, however, we need to look ahead one
22335 more token to see if *that* token is a type specifier.
22337 This code could be improved to handle:
22339 class Z { }
22340 static const <type> var = ...; */
22341 case CPP_KEYWORD:
22342 if (keyword_is_decl_specifier (token->keyword))
22344 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
22346 /* Handling user-defined types here would be nice, but very
22347 tricky. */
22348 want_semicolon
22349 = (lookahead->type == CPP_KEYWORD
22350 && keyword_begins_type_specifier (lookahead->keyword));
22352 break;
22353 default:
22354 break;
22357 /* If we don't have a type, then something is very wrong and we
22358 shouldn't try to do anything clever. Likewise for not seeing the
22359 closing brace. */
22360 if (closing_brace && TYPE_P (type) && want_semicolon)
22362 /* Locate the closing brace. */
22363 cp_token_position prev
22364 = cp_lexer_previous_token_position (parser->lexer);
22365 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
22366 location_t loc = prev_token->location;
22368 /* We want to suggest insertion of a ';' immediately *after* the
22369 closing brace, so, if we can, offset the location by 1 column. */
22370 location_t next_loc = loc;
22371 if (!linemap_location_from_macro_expansion_p (line_table, loc))
22372 next_loc = linemap_position_for_loc_and_offset (line_table, loc, 1);
22374 rich_location richloc (line_table, next_loc);
22376 /* If we successfully offset the location, suggest the fix-it. */
22377 if (next_loc != loc)
22378 richloc.add_fixit_insert_before (next_loc, ";");
22380 if (CLASSTYPE_DECLARED_CLASS (type))
22381 error_at_rich_loc (&richloc,
22382 "expected %<;%> after class definition");
22383 else if (TREE_CODE (type) == RECORD_TYPE)
22384 error_at_rich_loc (&richloc,
22385 "expected %<;%> after struct definition");
22386 else if (TREE_CODE (type) == UNION_TYPE)
22387 error_at_rich_loc (&richloc,
22388 "expected %<;%> after union definition");
22389 else
22390 gcc_unreachable ();
22392 /* Unget one token and smash it to look as though we encountered
22393 a semicolon in the input stream. */
22394 cp_lexer_set_token_position (parser->lexer, prev);
22395 token = cp_lexer_peek_token (parser->lexer);
22396 token->type = CPP_SEMICOLON;
22397 token->keyword = RID_MAX;
22401 /* If this class is not itself within the scope of another class,
22402 then we need to parse the bodies of all of the queued function
22403 definitions. Note that the queued functions defined in a class
22404 are not always processed immediately following the
22405 class-specifier for that class. Consider:
22407 struct A {
22408 struct B { void f() { sizeof (A); } };
22411 If `f' were processed before the processing of `A' were
22412 completed, there would be no way to compute the size of `A'.
22413 Note that the nesting we are interested in here is lexical --
22414 not the semantic nesting given by TYPE_CONTEXT. In particular,
22415 for:
22417 struct A { struct B; };
22418 struct A::B { void f() { } };
22420 there is no need to delay the parsing of `A::B::f'. */
22421 if (--parser->num_classes_being_defined == 0)
22423 tree decl;
22424 tree class_type = NULL_TREE;
22425 tree pushed_scope = NULL_TREE;
22426 unsigned ix;
22427 cp_default_arg_entry *e;
22428 tree save_ccp, save_ccr;
22430 /* In a first pass, parse default arguments to the functions.
22431 Then, in a second pass, parse the bodies of the functions.
22432 This two-phased approach handles cases like:
22434 struct S {
22435 void f() { g(); }
22436 void g(int i = 3);
22440 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_default_args, ix, e)
22442 decl = e->decl;
22443 /* If there are default arguments that have not yet been processed,
22444 take care of them now. */
22445 if (class_type != e->class_type)
22447 if (pushed_scope)
22448 pop_scope (pushed_scope);
22449 class_type = e->class_type;
22450 pushed_scope = push_scope (class_type);
22452 /* Make sure that any template parameters are in scope. */
22453 maybe_begin_member_template_processing (decl);
22454 /* Parse the default argument expressions. */
22455 cp_parser_late_parsing_default_args (parser, decl);
22456 /* Remove any template parameters from the symbol table. */
22457 maybe_end_member_template_processing ();
22459 vec_safe_truncate (unparsed_funs_with_default_args, 0);
22460 /* Now parse any NSDMIs. */
22461 save_ccp = current_class_ptr;
22462 save_ccr = current_class_ref;
22463 FOR_EACH_VEC_SAFE_ELT (unparsed_nsdmis, ix, decl)
22465 if (class_type != DECL_CONTEXT (decl))
22467 if (pushed_scope)
22468 pop_scope (pushed_scope);
22469 class_type = DECL_CONTEXT (decl);
22470 pushed_scope = push_scope (class_type);
22472 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
22473 cp_parser_late_parsing_nsdmi (parser, decl);
22475 vec_safe_truncate (unparsed_nsdmis, 0);
22476 current_class_ptr = save_ccp;
22477 current_class_ref = save_ccr;
22478 if (pushed_scope)
22479 pop_scope (pushed_scope);
22481 /* Now do some post-NSDMI bookkeeping. */
22482 FOR_EACH_VEC_SAFE_ELT (unparsed_classes, ix, class_type)
22483 after_nsdmi_defaulted_late_checks (class_type);
22484 vec_safe_truncate (unparsed_classes, 0);
22485 after_nsdmi_defaulted_late_checks (type);
22487 /* Now parse the body of the functions. */
22488 if (flag_openmp)
22490 /* OpenMP UDRs need to be parsed before all other functions. */
22491 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22492 if (DECL_OMP_DECLARE_REDUCTION_P (decl))
22493 cp_parser_late_parsing_for_member (parser, decl);
22494 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22495 if (!DECL_OMP_DECLARE_REDUCTION_P (decl))
22496 cp_parser_late_parsing_for_member (parser, decl);
22498 else
22499 FOR_EACH_VEC_SAFE_ELT (unparsed_funs_with_definitions, ix, decl)
22500 cp_parser_late_parsing_for_member (parser, decl);
22501 vec_safe_truncate (unparsed_funs_with_definitions, 0);
22503 else
22504 vec_safe_push (unparsed_classes, type);
22506 /* Put back any saved access checks. */
22507 pop_deferring_access_checks ();
22509 /* Restore saved state. */
22510 parser->in_switch_statement_p = in_switch_statement_p;
22511 parser->in_statement = in_statement;
22512 parser->in_function_body = saved_in_function_body;
22513 parser->num_template_parameter_lists
22514 = saved_num_template_parameter_lists;
22515 parser->in_unbraced_linkage_specification_p
22516 = saved_in_unbraced_linkage_specification_p;
22518 return type;
22521 static tree
22522 cp_parser_class_specifier (cp_parser* parser)
22524 tree ret;
22525 timevar_push (TV_PARSE_STRUCT);
22526 ret = cp_parser_class_specifier_1 (parser);
22527 timevar_pop (TV_PARSE_STRUCT);
22528 return ret;
22531 /* Parse a class-head.
22533 class-head:
22534 class-key identifier [opt] base-clause [opt]
22535 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
22536 class-key nested-name-specifier [opt] template-id
22537 base-clause [opt]
22539 class-virt-specifier:
22540 final
22542 GNU Extensions:
22543 class-key attributes identifier [opt] base-clause [opt]
22544 class-key attributes nested-name-specifier identifier base-clause [opt]
22545 class-key attributes nested-name-specifier [opt] template-id
22546 base-clause [opt]
22548 Upon return BASES is initialized to the list of base classes (or
22549 NULL, if there are none) in the same form returned by
22550 cp_parser_base_clause.
22552 Returns the TYPE of the indicated class. Sets
22553 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
22554 involving a nested-name-specifier was used, and FALSE otherwise.
22556 Returns error_mark_node if this is not a class-head.
22558 Returns NULL_TREE if the class-head is syntactically valid, but
22559 semantically invalid in a way that means we should skip the entire
22560 body of the class. */
22562 static tree
22563 cp_parser_class_head (cp_parser* parser,
22564 bool* nested_name_specifier_p)
22566 tree nested_name_specifier;
22567 enum tag_types class_key;
22568 tree id = NULL_TREE;
22569 tree type = NULL_TREE;
22570 tree attributes;
22571 tree bases;
22572 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
22573 bool template_id_p = false;
22574 bool qualified_p = false;
22575 bool invalid_nested_name_p = false;
22576 bool invalid_explicit_specialization_p = false;
22577 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
22578 tree pushed_scope = NULL_TREE;
22579 unsigned num_templates;
22580 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
22581 /* Assume no nested-name-specifier will be present. */
22582 *nested_name_specifier_p = false;
22583 /* Assume no template parameter lists will be used in defining the
22584 type. */
22585 num_templates = 0;
22586 parser->colon_corrects_to_scope_p = false;
22588 /* Look for the class-key. */
22589 class_key = cp_parser_class_key (parser);
22590 if (class_key == none_type)
22591 return error_mark_node;
22593 location_t class_head_start_location = input_location;
22595 /* Parse the attributes. */
22596 attributes = cp_parser_attributes_opt (parser);
22598 /* If the next token is `::', that is invalid -- but sometimes
22599 people do try to write:
22601 struct ::S {};
22603 Handle this gracefully by accepting the extra qualifier, and then
22604 issuing an error about it later if this really is a
22605 class-head. If it turns out just to be an elaborated type
22606 specifier, remain silent. */
22607 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
22608 qualified_p = true;
22610 push_deferring_access_checks (dk_no_check);
22612 /* Determine the name of the class. Begin by looking for an
22613 optional nested-name-specifier. */
22614 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
22615 nested_name_specifier
22616 = cp_parser_nested_name_specifier_opt (parser,
22617 /*typename_keyword_p=*/false,
22618 /*check_dependency_p=*/false,
22619 /*type_p=*/true,
22620 /*is_declaration=*/false);
22621 /* If there was a nested-name-specifier, then there *must* be an
22622 identifier. */
22624 cp_token *bad_template_keyword = NULL;
22626 if (nested_name_specifier)
22628 type_start_token = cp_lexer_peek_token (parser->lexer);
22629 /* Although the grammar says `identifier', it really means
22630 `class-name' or `template-name'. You are only allowed to
22631 define a class that has already been declared with this
22632 syntax.
22634 The proposed resolution for Core Issue 180 says that wherever
22635 you see `class T::X' you should treat `X' as a type-name.
22637 It is OK to define an inaccessible class; for example:
22639 class A { class B; };
22640 class A::B {};
22642 We do not know if we will see a class-name, or a
22643 template-name. We look for a class-name first, in case the
22644 class-name is a template-id; if we looked for the
22645 template-name first we would stop after the template-name. */
22646 cp_parser_parse_tentatively (parser);
22647 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22648 bad_template_keyword = cp_lexer_consume_token (parser->lexer);
22649 type = cp_parser_class_name (parser,
22650 /*typename_keyword_p=*/false,
22651 /*template_keyword_p=*/false,
22652 class_type,
22653 /*check_dependency_p=*/false,
22654 /*class_head_p=*/true,
22655 /*is_declaration=*/false);
22656 /* If that didn't work, ignore the nested-name-specifier. */
22657 if (!cp_parser_parse_definitely (parser))
22659 invalid_nested_name_p = true;
22660 type_start_token = cp_lexer_peek_token (parser->lexer);
22661 id = cp_parser_identifier (parser);
22662 if (id == error_mark_node)
22663 id = NULL_TREE;
22665 /* If we could not find a corresponding TYPE, treat this
22666 declaration like an unqualified declaration. */
22667 if (type == error_mark_node)
22668 nested_name_specifier = NULL_TREE;
22669 /* Otherwise, count the number of templates used in TYPE and its
22670 containing scopes. */
22671 else
22673 tree scope;
22675 for (scope = TREE_TYPE (type);
22676 scope && TREE_CODE (scope) != NAMESPACE_DECL;
22677 scope = get_containing_scope (scope))
22678 if (TYPE_P (scope)
22679 && CLASS_TYPE_P (scope)
22680 && CLASSTYPE_TEMPLATE_INFO (scope)
22681 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
22682 && (!CLASSTYPE_TEMPLATE_SPECIALIZATION (scope)
22683 || uses_template_parms (CLASSTYPE_TI_ARGS (scope))))
22684 ++num_templates;
22687 /* Otherwise, the identifier is optional. */
22688 else
22690 /* We don't know whether what comes next is a template-id,
22691 an identifier, or nothing at all. */
22692 cp_parser_parse_tentatively (parser);
22693 /* Check for a template-id. */
22694 type_start_token = cp_lexer_peek_token (parser->lexer);
22695 id = cp_parser_template_id (parser,
22696 /*template_keyword_p=*/false,
22697 /*check_dependency_p=*/true,
22698 class_key,
22699 /*is_declaration=*/true);
22700 /* If that didn't work, it could still be an identifier. */
22701 if (!cp_parser_parse_definitely (parser))
22703 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22705 type_start_token = cp_lexer_peek_token (parser->lexer);
22706 id = cp_parser_identifier (parser);
22708 else
22709 id = NULL_TREE;
22711 else
22713 template_id_p = true;
22714 ++num_templates;
22718 pop_deferring_access_checks ();
22720 if (id)
22722 cp_parser_check_for_invalid_template_id (parser, id,
22723 class_key,
22724 type_start_token->location);
22726 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
22728 /* If it's not a `:' or a `{' then we can't really be looking at a
22729 class-head, since a class-head only appears as part of a
22730 class-specifier. We have to detect this situation before calling
22731 xref_tag, since that has irreversible side-effects. */
22732 if (!cp_parser_next_token_starts_class_definition_p (parser))
22734 cp_parser_error (parser, "expected %<{%> or %<:%>");
22735 type = error_mark_node;
22736 goto out;
22739 /* At this point, we're going ahead with the class-specifier, even
22740 if some other problem occurs. */
22741 cp_parser_commit_to_tentative_parse (parser);
22742 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
22744 cp_parser_error (parser,
22745 "cannot specify %<override%> for a class");
22746 type = error_mark_node;
22747 goto out;
22749 /* Issue the error about the overly-qualified name now. */
22750 if (qualified_p)
22752 cp_parser_error (parser,
22753 "global qualification of class name is invalid");
22754 type = error_mark_node;
22755 goto out;
22757 else if (invalid_nested_name_p)
22759 cp_parser_error (parser,
22760 "qualified name does not name a class");
22761 type = error_mark_node;
22762 goto out;
22764 else if (nested_name_specifier)
22766 tree scope;
22768 if (bad_template_keyword)
22769 /* [temp.names]: in a qualified-id formed by a class-head-name, the
22770 keyword template shall not appear at the top level. */
22771 pedwarn (bad_template_keyword->location, OPT_Wpedantic,
22772 "keyword %<template%> not allowed in class-head-name");
22774 /* Reject typedef-names in class heads. */
22775 if (!DECL_IMPLICIT_TYPEDEF_P (type))
22777 error_at (type_start_token->location,
22778 "invalid class name in declaration of %qD",
22779 type);
22780 type = NULL_TREE;
22781 goto done;
22784 /* Figure out in what scope the declaration is being placed. */
22785 scope = current_scope ();
22786 /* If that scope does not contain the scope in which the
22787 class was originally declared, the program is invalid. */
22788 if (scope && !is_ancestor (scope, nested_name_specifier))
22790 if (at_namespace_scope_p ())
22791 error_at (type_start_token->location,
22792 "declaration of %qD in namespace %qD which does not "
22793 "enclose %qD",
22794 type, scope, nested_name_specifier);
22795 else
22796 error_at (type_start_token->location,
22797 "declaration of %qD in %qD which does not enclose %qD",
22798 type, scope, nested_name_specifier);
22799 type = NULL_TREE;
22800 goto done;
22802 /* [dcl.meaning]
22804 A declarator-id shall not be qualified except for the
22805 definition of a ... nested class outside of its class
22806 ... [or] the definition or explicit instantiation of a
22807 class member of a namespace outside of its namespace. */
22808 if (scope == nested_name_specifier)
22810 permerror (nested_name_specifier_token_start->location,
22811 "extra qualification not allowed");
22812 nested_name_specifier = NULL_TREE;
22813 num_templates = 0;
22816 /* An explicit-specialization must be preceded by "template <>". If
22817 it is not, try to recover gracefully. */
22818 if (at_namespace_scope_p ()
22819 && parser->num_template_parameter_lists == 0
22820 && !processing_template_parmlist
22821 && template_id_p)
22823 /* Build a location of this form:
22824 struct typename <ARGS>
22825 ^~~~~~~~~~~~~~~~~~~~~~
22826 with caret==start at the start token, and
22827 finishing at the end of the type. */
22828 location_t reported_loc
22829 = make_location (class_head_start_location,
22830 class_head_start_location,
22831 get_finish (type_start_token->location));
22832 rich_location richloc (line_table, reported_loc);
22833 richloc.add_fixit_insert_before (class_head_start_location,
22834 "template <> ");
22835 error_at_rich_loc
22836 (&richloc,
22837 "an explicit specialization must be preceded by %<template <>%>");
22838 invalid_explicit_specialization_p = true;
22839 /* Take the same action that would have been taken by
22840 cp_parser_explicit_specialization. */
22841 ++parser->num_template_parameter_lists;
22842 begin_specialization ();
22844 /* There must be no "return" statements between this point and the
22845 end of this function; set "type "to the correct return value and
22846 use "goto done;" to return. */
22847 /* Make sure that the right number of template parameters were
22848 present. */
22849 if (!cp_parser_check_template_parameters (parser, num_templates,
22850 type_start_token->location,
22851 /*declarator=*/NULL))
22853 /* If something went wrong, there is no point in even trying to
22854 process the class-definition. */
22855 type = NULL_TREE;
22856 goto done;
22859 /* Look up the type. */
22860 if (template_id_p)
22862 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
22863 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
22864 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
22866 error_at (type_start_token->location,
22867 "function template %qD redeclared as a class template", id);
22868 type = error_mark_node;
22870 else
22872 type = TREE_TYPE (id);
22873 type = maybe_process_partial_specialization (type);
22875 /* Check the scope while we still know whether or not we had a
22876 nested-name-specifier. */
22877 if (type != error_mark_node)
22878 check_unqualified_spec_or_inst (type, type_start_token->location);
22880 if (nested_name_specifier)
22881 pushed_scope = push_scope (nested_name_specifier);
22883 else if (nested_name_specifier)
22885 tree class_type;
22887 /* Given:
22889 template <typename T> struct S { struct T };
22890 template <typename T> struct S<T>::T { };
22892 we will get a TYPENAME_TYPE when processing the definition of
22893 `S::T'. We need to resolve it to the actual type before we
22894 try to define it. */
22895 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
22897 class_type = resolve_typename_type (TREE_TYPE (type),
22898 /*only_current_p=*/false);
22899 if (TREE_CODE (class_type) != TYPENAME_TYPE)
22900 type = TYPE_NAME (class_type);
22901 else
22903 cp_parser_error (parser, "could not resolve typename type");
22904 type = error_mark_node;
22908 if (maybe_process_partial_specialization (TREE_TYPE (type))
22909 == error_mark_node)
22911 type = NULL_TREE;
22912 goto done;
22915 class_type = current_class_type;
22916 /* Enter the scope indicated by the nested-name-specifier. */
22917 pushed_scope = push_scope (nested_name_specifier);
22918 /* Get the canonical version of this type. */
22919 type = TYPE_MAIN_DECL (TREE_TYPE (type));
22920 /* Call push_template_decl if it seems like we should be defining a
22921 template either from the template headers or the type we're
22922 defining, so that we diagnose both extra and missing headers. */
22923 if ((PROCESSING_REAL_TEMPLATE_DECL_P ()
22924 || CLASSTYPE_TEMPLATE_INFO (TREE_TYPE (type)))
22925 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
22927 type = push_template_decl (type);
22928 if (type == error_mark_node)
22930 type = NULL_TREE;
22931 goto done;
22935 type = TREE_TYPE (type);
22936 *nested_name_specifier_p = true;
22938 else /* The name is not a nested name. */
22940 /* If the class was unnamed, create a dummy name. */
22941 if (!id)
22942 id = make_anon_name ();
22943 tag_scope tag_scope = (parser->in_type_id_in_expr_p
22944 ? ts_within_enclosing_non_class
22945 : ts_current);
22946 type = xref_tag (class_key, id, tag_scope,
22947 parser->num_template_parameter_lists);
22950 /* Indicate whether this class was declared as a `class' or as a
22951 `struct'. */
22952 if (TREE_CODE (type) == RECORD_TYPE)
22953 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
22954 cp_parser_check_class_key (class_key, type);
22956 /* If this type was already complete, and we see another definition,
22957 that's an error. */
22958 if (type != error_mark_node && COMPLETE_TYPE_P (type))
22960 error_at (type_start_token->location, "redefinition of %q#T",
22961 type);
22962 inform (location_of (type), "previous definition of %q#T",
22963 type);
22964 type = NULL_TREE;
22965 goto done;
22967 else if (type == error_mark_node)
22968 type = NULL_TREE;
22970 if (type)
22972 /* Apply attributes now, before any use of the class as a template
22973 argument in its base list. */
22974 cplus_decl_attributes (&type, attributes, (int)ATTR_FLAG_TYPE_IN_PLACE);
22975 fixup_attribute_variants (type);
22978 /* We will have entered the scope containing the class; the names of
22979 base classes should be looked up in that context. For example:
22981 struct A { struct B {}; struct C; };
22982 struct A::C : B {};
22984 is valid. */
22986 /* Get the list of base-classes, if there is one. */
22987 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
22989 /* PR59482: enter the class scope so that base-specifiers are looked
22990 up correctly. */
22991 if (type)
22992 pushclass (type);
22993 bases = cp_parser_base_clause (parser);
22994 /* PR59482: get out of the previously pushed class scope so that the
22995 subsequent pops pop the right thing. */
22996 if (type)
22997 popclass ();
22999 else
23000 bases = NULL_TREE;
23002 /* If we're really defining a class, process the base classes.
23003 If they're invalid, fail. */
23004 if (type && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23005 xref_basetypes (type, bases);
23007 done:
23008 /* Leave the scope given by the nested-name-specifier. We will
23009 enter the class scope itself while processing the members. */
23010 if (pushed_scope)
23011 pop_scope (pushed_scope);
23013 if (invalid_explicit_specialization_p)
23015 end_specialization ();
23016 --parser->num_template_parameter_lists;
23019 if (type)
23020 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
23021 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
23022 CLASSTYPE_FINAL (type) = 1;
23023 out:
23024 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23025 return type;
23028 /* Parse a class-key.
23030 class-key:
23031 class
23032 struct
23033 union
23035 Returns the kind of class-key specified, or none_type to indicate
23036 error. */
23038 static enum tag_types
23039 cp_parser_class_key (cp_parser* parser)
23041 cp_token *token;
23042 enum tag_types tag_type;
23044 /* Look for the class-key. */
23045 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
23046 if (!token)
23047 return none_type;
23049 /* Check to see if the TOKEN is a class-key. */
23050 tag_type = cp_parser_token_is_class_key (token);
23051 if (!tag_type)
23052 cp_parser_error (parser, "expected class-key");
23053 return tag_type;
23056 /* Parse a type-parameter-key.
23058 type-parameter-key:
23059 class
23060 typename
23063 static void
23064 cp_parser_type_parameter_key (cp_parser* parser)
23066 /* Look for the type-parameter-key. */
23067 enum tag_types tag_type = none_type;
23068 cp_token *token = cp_lexer_peek_token (parser->lexer);
23069 if ((tag_type = cp_parser_token_is_type_parameter_key (token)) != none_type)
23071 cp_lexer_consume_token (parser->lexer);
23072 if (pedantic && tag_type == typename_type && cxx_dialect < cxx1z)
23073 /* typename is not allowed in a template template parameter
23074 by the standard until C++1Z. */
23075 pedwarn (token->location, OPT_Wpedantic,
23076 "ISO C++ forbids typename key in template template parameter;"
23077 " use -std=c++1z or -std=gnu++1z");
23079 else
23080 cp_parser_error (parser, "expected %<class%> or %<typename%>");
23082 return;
23085 /* Parse an (optional) member-specification.
23087 member-specification:
23088 member-declaration member-specification [opt]
23089 access-specifier : member-specification [opt] */
23091 static void
23092 cp_parser_member_specification_opt (cp_parser* parser)
23094 while (true)
23096 cp_token *token;
23097 enum rid keyword;
23099 /* Peek at the next token. */
23100 token = cp_lexer_peek_token (parser->lexer);
23101 /* If it's a `}', or EOF then we've seen all the members. */
23102 if (token->type == CPP_CLOSE_BRACE
23103 || token->type == CPP_EOF
23104 || token->type == CPP_PRAGMA_EOL)
23105 break;
23107 /* See if this token is a keyword. */
23108 keyword = token->keyword;
23109 switch (keyword)
23111 case RID_PUBLIC:
23112 case RID_PROTECTED:
23113 case RID_PRIVATE:
23114 /* Consume the access-specifier. */
23115 cp_lexer_consume_token (parser->lexer);
23116 /* Remember which access-specifier is active. */
23117 current_access_specifier = token->u.value;
23118 /* Look for the `:'. */
23119 cp_parser_require (parser, CPP_COLON, RT_COLON);
23120 break;
23122 default:
23123 /* Accept #pragmas at class scope. */
23124 if (token->type == CPP_PRAGMA)
23126 cp_parser_pragma (parser, pragma_member, NULL);
23127 break;
23130 /* Otherwise, the next construction must be a
23131 member-declaration. */
23132 cp_parser_member_declaration (parser);
23137 /* Parse a member-declaration.
23139 member-declaration:
23140 decl-specifier-seq [opt] member-declarator-list [opt] ;
23141 function-definition ; [opt]
23142 :: [opt] nested-name-specifier template [opt] unqualified-id ;
23143 using-declaration
23144 template-declaration
23145 alias-declaration
23147 member-declarator-list:
23148 member-declarator
23149 member-declarator-list , member-declarator
23151 member-declarator:
23152 declarator pure-specifier [opt]
23153 declarator constant-initializer [opt]
23154 identifier [opt] : constant-expression
23156 GNU Extensions:
23158 member-declaration:
23159 __extension__ member-declaration
23161 member-declarator:
23162 declarator attributes [opt] pure-specifier [opt]
23163 declarator attributes [opt] constant-initializer [opt]
23164 identifier [opt] attributes [opt] : constant-expression
23166 C++0x Extensions:
23168 member-declaration:
23169 static_assert-declaration */
23171 static void
23172 cp_parser_member_declaration (cp_parser* parser)
23174 cp_decl_specifier_seq decl_specifiers;
23175 tree prefix_attributes;
23176 tree decl;
23177 int declares_class_or_enum;
23178 bool friend_p;
23179 cp_token *token = NULL;
23180 cp_token *decl_spec_token_start = NULL;
23181 cp_token *initializer_token_start = NULL;
23182 int saved_pedantic;
23183 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
23185 /* Check for the `__extension__' keyword. */
23186 if (cp_parser_extension_opt (parser, &saved_pedantic))
23188 /* Recurse. */
23189 cp_parser_member_declaration (parser);
23190 /* Restore the old value of the PEDANTIC flag. */
23191 pedantic = saved_pedantic;
23193 return;
23196 /* Check for a template-declaration. */
23197 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
23199 /* An explicit specialization here is an error condition, and we
23200 expect the specialization handler to detect and report this. */
23201 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
23202 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
23203 cp_parser_explicit_specialization (parser);
23204 else
23205 cp_parser_template_declaration (parser, /*member_p=*/true);
23207 return;
23209 /* Check for a template introduction. */
23210 else if (cp_parser_template_declaration_after_export (parser, true))
23211 return;
23213 /* Check for a using-declaration. */
23214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
23216 if (cxx_dialect < cxx11)
23218 /* Parse the using-declaration. */
23219 cp_parser_using_declaration (parser,
23220 /*access_declaration_p=*/false);
23221 return;
23223 else
23225 tree decl;
23226 bool alias_decl_expected;
23227 cp_parser_parse_tentatively (parser);
23228 decl = cp_parser_alias_declaration (parser);
23229 /* Note that if we actually see the '=' token after the
23230 identifier, cp_parser_alias_declaration commits the
23231 tentative parse. In that case, we really expect an
23232 alias-declaration. Otherwise, we expect a using
23233 declaration. */
23234 alias_decl_expected =
23235 !cp_parser_uncommitted_to_tentative_parse_p (parser);
23236 cp_parser_parse_definitely (parser);
23238 if (alias_decl_expected)
23239 finish_member_declaration (decl);
23240 else
23241 cp_parser_using_declaration (parser,
23242 /*access_declaration_p=*/false);
23243 return;
23247 /* Check for @defs. */
23248 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
23250 tree ivar, member;
23251 tree ivar_chains = cp_parser_objc_defs_expression (parser);
23252 ivar = ivar_chains;
23253 while (ivar)
23255 member = ivar;
23256 ivar = TREE_CHAIN (member);
23257 TREE_CHAIN (member) = NULL_TREE;
23258 finish_member_declaration (member);
23260 return;
23263 /* If the next token is `static_assert' we have a static assertion. */
23264 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
23266 cp_parser_static_assert (parser, /*member_p=*/true);
23267 return;
23270 parser->colon_corrects_to_scope_p = false;
23272 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
23273 goto out;
23275 /* Parse the decl-specifier-seq. */
23276 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
23277 cp_parser_decl_specifier_seq (parser,
23278 CP_PARSER_FLAGS_OPTIONAL,
23279 &decl_specifiers,
23280 &declares_class_or_enum);
23281 /* Check for an invalid type-name. */
23282 if (!decl_specifiers.any_type_specifiers_p
23283 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
23284 goto out;
23285 /* If there is no declarator, then the decl-specifier-seq should
23286 specify a type. */
23287 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23289 /* If there was no decl-specifier-seq, and the next token is a
23290 `;', then we have something like:
23292 struct S { ; };
23294 [class.mem]
23296 Each member-declaration shall declare at least one member
23297 name of the class. */
23298 if (!decl_specifiers.any_specifiers_p)
23300 cp_token *token = cp_lexer_peek_token (parser->lexer);
23301 if (!in_system_header_at (token->location))
23303 gcc_rich_location richloc (token->location);
23304 richloc.add_fixit_remove ();
23305 pedwarn_at_rich_loc (&richloc, OPT_Wpedantic, "extra %<;%>");
23308 else
23310 tree type;
23312 /* See if this declaration is a friend. */
23313 friend_p = cp_parser_friend_p (&decl_specifiers);
23314 /* If there were decl-specifiers, check to see if there was
23315 a class-declaration. */
23316 type = check_tag_decl (&decl_specifiers,
23317 /*explicit_type_instantiation_p=*/false);
23318 /* Nested classes have already been added to the class, but
23319 a `friend' needs to be explicitly registered. */
23320 if (friend_p)
23322 /* If the `friend' keyword was present, the friend must
23323 be introduced with a class-key. */
23324 if (!declares_class_or_enum && cxx_dialect < cxx11)
23325 pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
23326 "in C++03 a class-key must be used "
23327 "when declaring a friend");
23328 /* In this case:
23330 template <typename T> struct A {
23331 friend struct A<T>::B;
23334 A<T>::B will be represented by a TYPENAME_TYPE, and
23335 therefore not recognized by check_tag_decl. */
23336 if (!type)
23338 type = decl_specifiers.type;
23339 if (type && TREE_CODE (type) == TYPE_DECL)
23340 type = TREE_TYPE (type);
23342 if (!type || !TYPE_P (type))
23343 error_at (decl_spec_token_start->location,
23344 "friend declaration does not name a class or "
23345 "function");
23346 else
23347 make_friend_class (current_class_type, type,
23348 /*complain=*/true);
23350 /* If there is no TYPE, an error message will already have
23351 been issued. */
23352 else if (!type || type == error_mark_node)
23354 /* An anonymous aggregate has to be handled specially; such
23355 a declaration really declares a data member (with a
23356 particular type), as opposed to a nested class. */
23357 else if (ANON_AGGR_TYPE_P (type))
23359 /* C++11 9.5/6. */
23360 if (decl_specifiers.storage_class != sc_none)
23361 error_at (decl_spec_token_start->location,
23362 "a storage class on an anonymous aggregate "
23363 "in class scope is not allowed");
23365 /* Remove constructors and such from TYPE, now that we
23366 know it is an anonymous aggregate. */
23367 fixup_anonymous_aggr (type);
23368 /* And make the corresponding data member. */
23369 decl = build_decl (decl_spec_token_start->location,
23370 FIELD_DECL, NULL_TREE, type);
23371 /* Add it to the class. */
23372 finish_member_declaration (decl);
23374 else
23375 cp_parser_check_access_in_redeclaration
23376 (TYPE_NAME (type),
23377 decl_spec_token_start->location);
23380 else
23382 bool assume_semicolon = false;
23384 /* Clear attributes from the decl_specifiers but keep them
23385 around as prefix attributes that apply them to the entity
23386 being declared. */
23387 prefix_attributes = decl_specifiers.attributes;
23388 decl_specifiers.attributes = NULL_TREE;
23390 /* See if these declarations will be friends. */
23391 friend_p = cp_parser_friend_p (&decl_specifiers);
23393 /* Keep going until we hit the `;' at the end of the
23394 declaration. */
23395 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23397 tree attributes = NULL_TREE;
23398 tree first_attribute;
23400 /* Peek at the next token. */
23401 token = cp_lexer_peek_token (parser->lexer);
23403 /* Check for a bitfield declaration. */
23404 if (token->type == CPP_COLON
23405 || (token->type == CPP_NAME
23406 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
23407 == CPP_COLON))
23409 tree identifier;
23410 tree width;
23412 /* Get the name of the bitfield. Note that we cannot just
23413 check TOKEN here because it may have been invalidated by
23414 the call to cp_lexer_peek_nth_token above. */
23415 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
23416 identifier = cp_parser_identifier (parser);
23417 else
23418 identifier = NULL_TREE;
23420 /* Consume the `:' token. */
23421 cp_lexer_consume_token (parser->lexer);
23422 /* Get the width of the bitfield. */
23423 width
23424 = cp_parser_constant_expression (parser);
23426 /* Look for attributes that apply to the bitfield. */
23427 attributes = cp_parser_attributes_opt (parser);
23428 /* Remember which attributes are prefix attributes and
23429 which are not. */
23430 first_attribute = attributes;
23431 /* Combine the attributes. */
23432 attributes = chainon (prefix_attributes, attributes);
23434 /* Create the bitfield declaration. */
23435 decl = grokbitfield (identifier
23436 ? make_id_declarator (NULL_TREE,
23437 identifier,
23438 sfk_none)
23439 : NULL,
23440 &decl_specifiers,
23441 width,
23442 attributes);
23444 else
23446 cp_declarator *declarator;
23447 tree initializer;
23448 tree asm_specification;
23449 int ctor_dtor_or_conv_p;
23451 /* Parse the declarator. */
23452 declarator
23453 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23454 &ctor_dtor_or_conv_p,
23455 /*parenthesized_p=*/NULL,
23456 /*member_p=*/true,
23457 friend_p);
23459 /* If something went wrong parsing the declarator, make sure
23460 that we at least consume some tokens. */
23461 if (declarator == cp_error_declarator)
23463 /* Skip to the end of the statement. */
23464 cp_parser_skip_to_end_of_statement (parser);
23465 /* If the next token is not a semicolon, that is
23466 probably because we just skipped over the body of
23467 a function. So, we consume a semicolon if
23468 present, but do not issue an error message if it
23469 is not present. */
23470 if (cp_lexer_next_token_is (parser->lexer,
23471 CPP_SEMICOLON))
23472 cp_lexer_consume_token (parser->lexer);
23473 goto out;
23476 if (declares_class_or_enum & 2)
23477 cp_parser_check_for_definition_in_return_type
23478 (declarator, decl_specifiers.type,
23479 decl_specifiers.locations[ds_type_spec]);
23481 /* Look for an asm-specification. */
23482 asm_specification = cp_parser_asm_specification_opt (parser);
23483 /* Look for attributes that apply to the declaration. */
23484 attributes = cp_parser_attributes_opt (parser);
23485 /* Remember which attributes are prefix attributes and
23486 which are not. */
23487 first_attribute = attributes;
23488 /* Combine the attributes. */
23489 attributes = chainon (prefix_attributes, attributes);
23491 /* If it's an `=', then we have a constant-initializer or a
23492 pure-specifier. It is not correct to parse the
23493 initializer before registering the member declaration
23494 since the member declaration should be in scope while
23495 its initializer is processed. However, the rest of the
23496 front end does not yet provide an interface that allows
23497 us to handle this correctly. */
23498 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23500 /* In [class.mem]:
23502 A pure-specifier shall be used only in the declaration of
23503 a virtual function.
23505 A member-declarator can contain a constant-initializer
23506 only if it declares a static member of integral or
23507 enumeration type.
23509 Therefore, if the DECLARATOR is for a function, we look
23510 for a pure-specifier; otherwise, we look for a
23511 constant-initializer. When we call `grokfield', it will
23512 perform more stringent semantics checks. */
23513 initializer_token_start = cp_lexer_peek_token (parser->lexer);
23514 if (function_declarator_p (declarator)
23515 || (decl_specifiers.type
23516 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
23517 && declarator->kind == cdk_id
23518 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
23519 == FUNCTION_TYPE)))
23520 initializer = cp_parser_pure_specifier (parser);
23521 else if (decl_specifiers.storage_class != sc_static)
23522 initializer = cp_parser_save_nsdmi (parser);
23523 else if (cxx_dialect >= cxx11)
23525 bool nonconst;
23526 /* Don't require a constant rvalue in C++11, since we
23527 might want a reference constant. We'll enforce
23528 constancy later. */
23529 cp_lexer_consume_token (parser->lexer);
23530 /* Parse the initializer. */
23531 initializer = cp_parser_initializer_clause (parser,
23532 &nonconst);
23534 else
23535 /* Parse the initializer. */
23536 initializer = cp_parser_constant_initializer (parser);
23538 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
23539 && !function_declarator_p (declarator))
23541 bool x;
23542 if (decl_specifiers.storage_class != sc_static)
23543 initializer = cp_parser_save_nsdmi (parser);
23544 else
23545 initializer = cp_parser_initializer (parser, &x, &x);
23547 /* Otherwise, there is no initializer. */
23548 else
23549 initializer = NULL_TREE;
23551 /* See if we are probably looking at a function
23552 definition. We are certainly not looking at a
23553 member-declarator. Calling `grokfield' has
23554 side-effects, so we must not do it unless we are sure
23555 that we are looking at a member-declarator. */
23556 if (cp_parser_token_starts_function_definition_p
23557 (cp_lexer_peek_token (parser->lexer)))
23559 /* The grammar does not allow a pure-specifier to be
23560 used when a member function is defined. (It is
23561 possible that this fact is an oversight in the
23562 standard, since a pure function may be defined
23563 outside of the class-specifier. */
23564 if (initializer && initializer_token_start)
23565 error_at (initializer_token_start->location,
23566 "pure-specifier on function-definition");
23567 decl = cp_parser_save_member_function_body (parser,
23568 &decl_specifiers,
23569 declarator,
23570 attributes);
23571 if (parser->fully_implicit_function_template_p)
23572 decl = finish_fully_implicit_template (parser, decl);
23573 /* If the member was not a friend, declare it here. */
23574 if (!friend_p)
23575 finish_member_declaration (decl);
23576 /* Peek at the next token. */
23577 token = cp_lexer_peek_token (parser->lexer);
23578 /* If the next token is a semicolon, consume it. */
23579 if (token->type == CPP_SEMICOLON)
23581 location_t semicolon_loc
23582 = cp_lexer_consume_token (parser->lexer)->location;
23583 gcc_rich_location richloc (semicolon_loc);
23584 richloc.add_fixit_remove ();
23585 warning_at_rich_loc (&richloc, OPT_Wextra_semi,
23586 "extra %<;%> after in-class "
23587 "function definition");
23589 goto out;
23591 else
23592 if (declarator->kind == cdk_function)
23593 declarator->id_loc = token->location;
23594 /* Create the declaration. */
23595 decl = grokfield (declarator, &decl_specifiers,
23596 initializer, /*init_const_expr_p=*/true,
23597 asm_specification, attributes);
23598 if (parser->fully_implicit_function_template_p)
23600 if (friend_p)
23601 finish_fully_implicit_template (parser, 0);
23602 else
23603 decl = finish_fully_implicit_template (parser, decl);
23607 cp_finalize_omp_declare_simd (parser, decl);
23608 cp_finalize_oacc_routine (parser, decl, false);
23610 /* Reset PREFIX_ATTRIBUTES. */
23611 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23612 attributes = TREE_CHAIN (attributes);
23613 if (attributes)
23614 TREE_CHAIN (attributes) = NULL_TREE;
23616 /* If there is any qualification still in effect, clear it
23617 now; we will be starting fresh with the next declarator. */
23618 parser->scope = NULL_TREE;
23619 parser->qualifying_scope = NULL_TREE;
23620 parser->object_scope = NULL_TREE;
23621 /* If it's a `,', then there are more declarators. */
23622 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23624 cp_lexer_consume_token (parser->lexer);
23625 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23627 cp_token *token = cp_lexer_previous_token (parser->lexer);
23628 gcc_rich_location richloc (token->location);
23629 richloc.add_fixit_remove ();
23630 error_at_rich_loc (&richloc, "stray %<,%> at end of "
23631 "member declaration");
23634 /* If the next token isn't a `;', then we have a parse error. */
23635 else if (cp_lexer_next_token_is_not (parser->lexer,
23636 CPP_SEMICOLON))
23638 /* The next token might be a ways away from where the
23639 actual semicolon is missing. Find the previous token
23640 and use that for our error position. */
23641 cp_token *token = cp_lexer_previous_token (parser->lexer);
23642 gcc_rich_location richloc (token->location);
23643 richloc.add_fixit_insert_after (";");
23644 error_at_rich_loc (&richloc, "expected %<;%> at end of "
23645 "member declaration");
23647 /* Assume that the user meant to provide a semicolon. If
23648 we were to cp_parser_skip_to_end_of_statement, we might
23649 skip to a semicolon inside a member function definition
23650 and issue nonsensical error messages. */
23651 assume_semicolon = true;
23654 if (decl)
23656 /* Add DECL to the list of members. */
23657 if (!friend_p
23658 /* Explicitly include, eg, NSDMIs, for better error
23659 recovery (c++/58650). */
23660 || !DECL_DECLARES_FUNCTION_P (decl))
23661 finish_member_declaration (decl);
23663 if (TREE_CODE (decl) == FUNCTION_DECL)
23664 cp_parser_save_default_args (parser, decl);
23665 else if (TREE_CODE (decl) == FIELD_DECL
23666 && !DECL_C_BIT_FIELD (decl)
23667 && DECL_INITIAL (decl))
23668 /* Add DECL to the queue of NSDMI to be parsed later. */
23669 vec_safe_push (unparsed_nsdmis, decl);
23672 if (assume_semicolon)
23673 goto out;
23677 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
23678 out:
23679 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
23682 /* Parse a pure-specifier.
23684 pure-specifier:
23687 Returns INTEGER_ZERO_NODE if a pure specifier is found.
23688 Otherwise, ERROR_MARK_NODE is returned. */
23690 static tree
23691 cp_parser_pure_specifier (cp_parser* parser)
23693 cp_token *token;
23695 /* Look for the `=' token. */
23696 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23697 return error_mark_node;
23698 /* Look for the `0' token. */
23699 token = cp_lexer_peek_token (parser->lexer);
23701 if (token->type == CPP_EOF
23702 || token->type == CPP_PRAGMA_EOL)
23703 return error_mark_node;
23705 cp_lexer_consume_token (parser->lexer);
23707 /* Accept = default or = delete in c++0x mode. */
23708 if (token->keyword == RID_DEFAULT
23709 || token->keyword == RID_DELETE)
23711 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
23712 return token->u.value;
23715 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
23716 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
23718 cp_parser_error (parser,
23719 "invalid pure specifier (only %<= 0%> is allowed)");
23720 cp_parser_skip_to_end_of_statement (parser);
23721 return error_mark_node;
23723 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
23725 error_at (token->location, "templates may not be %<virtual%>");
23726 return error_mark_node;
23729 return integer_zero_node;
23732 /* Parse a constant-initializer.
23734 constant-initializer:
23735 = constant-expression
23737 Returns a representation of the constant-expression. */
23739 static tree
23740 cp_parser_constant_initializer (cp_parser* parser)
23742 /* Look for the `=' token. */
23743 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
23744 return error_mark_node;
23746 /* It is invalid to write:
23748 struct S { static const int i = { 7 }; };
23751 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23753 cp_parser_error (parser,
23754 "a brace-enclosed initializer is not allowed here");
23755 /* Consume the opening brace. */
23756 cp_lexer_consume_token (parser->lexer);
23757 /* Skip the initializer. */
23758 cp_parser_skip_to_closing_brace (parser);
23759 /* Look for the trailing `}'. */
23760 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
23762 return error_mark_node;
23765 return cp_parser_constant_expression (parser);
23768 /* Derived classes [gram.class.derived] */
23770 /* Parse a base-clause.
23772 base-clause:
23773 : base-specifier-list
23775 base-specifier-list:
23776 base-specifier ... [opt]
23777 base-specifier-list , base-specifier ... [opt]
23779 Returns a TREE_LIST representing the base-classes, in the order in
23780 which they were declared. The representation of each node is as
23781 described by cp_parser_base_specifier.
23783 In the case that no bases are specified, this function will return
23784 NULL_TREE, not ERROR_MARK_NODE. */
23786 static tree
23787 cp_parser_base_clause (cp_parser* parser)
23789 tree bases = NULL_TREE;
23791 /* Look for the `:' that begins the list. */
23792 cp_parser_require (parser, CPP_COLON, RT_COLON);
23794 /* Scan the base-specifier-list. */
23795 while (true)
23797 cp_token *token;
23798 tree base;
23799 bool pack_expansion_p = false;
23801 /* Look for the base-specifier. */
23802 base = cp_parser_base_specifier (parser);
23803 /* Look for the (optional) ellipsis. */
23804 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23806 /* Consume the `...'. */
23807 cp_lexer_consume_token (parser->lexer);
23809 pack_expansion_p = true;
23812 /* Add BASE to the front of the list. */
23813 if (base && base != error_mark_node)
23815 if (pack_expansion_p)
23816 /* Make this a pack expansion type. */
23817 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
23819 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
23821 TREE_CHAIN (base) = bases;
23822 bases = base;
23825 /* Peek at the next token. */
23826 token = cp_lexer_peek_token (parser->lexer);
23827 /* If it's not a comma, then the list is complete. */
23828 if (token->type != CPP_COMMA)
23829 break;
23830 /* Consume the `,'. */
23831 cp_lexer_consume_token (parser->lexer);
23834 /* PARSER->SCOPE may still be non-NULL at this point, if the last
23835 base class had a qualified name. However, the next name that
23836 appears is certainly not qualified. */
23837 parser->scope = NULL_TREE;
23838 parser->qualifying_scope = NULL_TREE;
23839 parser->object_scope = NULL_TREE;
23841 return nreverse (bases);
23844 /* Parse a base-specifier.
23846 base-specifier:
23847 :: [opt] nested-name-specifier [opt] class-name
23848 virtual access-specifier [opt] :: [opt] nested-name-specifier
23849 [opt] class-name
23850 access-specifier virtual [opt] :: [opt] nested-name-specifier
23851 [opt] class-name
23853 Returns a TREE_LIST. The TREE_PURPOSE will be one of
23854 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
23855 indicate the specifiers provided. The TREE_VALUE will be a TYPE
23856 (or the ERROR_MARK_NODE) indicating the type that was specified. */
23858 static tree
23859 cp_parser_base_specifier (cp_parser* parser)
23861 cp_token *token;
23862 bool done = false;
23863 bool virtual_p = false;
23864 bool duplicate_virtual_error_issued_p = false;
23865 bool duplicate_access_error_issued_p = false;
23866 bool class_scope_p, template_p;
23867 tree access = access_default_node;
23868 tree type;
23870 /* Process the optional `virtual' and `access-specifier'. */
23871 while (!done)
23873 /* Peek at the next token. */
23874 token = cp_lexer_peek_token (parser->lexer);
23875 /* Process `virtual'. */
23876 switch (token->keyword)
23878 case RID_VIRTUAL:
23879 /* If `virtual' appears more than once, issue an error. */
23880 if (virtual_p && !duplicate_virtual_error_issued_p)
23882 cp_parser_error (parser,
23883 "%<virtual%> specified more than once in base-specifier");
23884 duplicate_virtual_error_issued_p = true;
23887 virtual_p = true;
23889 /* Consume the `virtual' token. */
23890 cp_lexer_consume_token (parser->lexer);
23892 break;
23894 case RID_PUBLIC:
23895 case RID_PROTECTED:
23896 case RID_PRIVATE:
23897 /* If more than one access specifier appears, issue an
23898 error. */
23899 if (access != access_default_node
23900 && !duplicate_access_error_issued_p)
23902 cp_parser_error (parser,
23903 "more than one access specifier in base-specifier");
23904 duplicate_access_error_issued_p = true;
23907 access = ridpointers[(int) token->keyword];
23909 /* Consume the access-specifier. */
23910 cp_lexer_consume_token (parser->lexer);
23912 break;
23914 default:
23915 done = true;
23916 break;
23919 /* It is not uncommon to see programs mechanically, erroneously, use
23920 the 'typename' keyword to denote (dependent) qualified types
23921 as base classes. */
23922 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
23924 token = cp_lexer_peek_token (parser->lexer);
23925 if (!processing_template_decl)
23926 error_at (token->location,
23927 "keyword %<typename%> not allowed outside of templates");
23928 else
23929 error_at (token->location,
23930 "keyword %<typename%> not allowed in this context "
23931 "(the base class is implicitly a type)");
23932 cp_lexer_consume_token (parser->lexer);
23935 /* Look for the optional `::' operator. */
23936 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
23937 /* Look for the nested-name-specifier. The simplest way to
23938 implement:
23940 [temp.res]
23942 The keyword `typename' is not permitted in a base-specifier or
23943 mem-initializer; in these contexts a qualified name that
23944 depends on a template-parameter is implicitly assumed to be a
23945 type name.
23947 is to pretend that we have seen the `typename' keyword at this
23948 point. */
23949 cp_parser_nested_name_specifier_opt (parser,
23950 /*typename_keyword_p=*/true,
23951 /*check_dependency_p=*/true,
23952 /*type_p=*/true,
23953 /*is_declaration=*/true);
23954 /* If the base class is given by a qualified name, assume that names
23955 we see are type names or templates, as appropriate. */
23956 class_scope_p = (parser->scope && TYPE_P (parser->scope));
23957 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
23959 if (!parser->scope
23960 && cp_lexer_next_token_is_decltype (parser->lexer))
23961 /* DR 950 allows decltype as a base-specifier. */
23962 type = cp_parser_decltype (parser);
23963 else
23965 /* Otherwise, look for the class-name. */
23966 type = cp_parser_class_name (parser,
23967 class_scope_p,
23968 template_p,
23969 typename_type,
23970 /*check_dependency_p=*/true,
23971 /*class_head_p=*/false,
23972 /*is_declaration=*/true);
23973 type = TREE_TYPE (type);
23976 if (type == error_mark_node)
23977 return error_mark_node;
23979 return finish_base_specifier (type, access, virtual_p);
23982 /* Exception handling [gram.exception] */
23984 /* Parse an (optional) noexcept-specification.
23986 noexcept-specification:
23987 noexcept ( constant-expression ) [opt]
23989 If no noexcept-specification is present, returns NULL_TREE.
23990 Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
23991 expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
23992 there are no parentheses. CONSUMED_EXPR will be set accordingly.
23993 Otherwise, returns a noexcept specification unless RETURN_COND is true,
23994 in which case a boolean condition is returned instead. */
23996 static tree
23997 cp_parser_noexcept_specification_opt (cp_parser* parser,
23998 bool require_constexpr,
23999 bool* consumed_expr,
24000 bool return_cond)
24002 cp_token *token;
24003 const char *saved_message;
24005 /* Peek at the next token. */
24006 token = cp_lexer_peek_token (parser->lexer);
24008 /* Is it a noexcept-specification? */
24009 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
24011 tree expr;
24012 cp_lexer_consume_token (parser->lexer);
24014 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
24016 cp_lexer_consume_token (parser->lexer);
24018 if (require_constexpr)
24020 /* Types may not be defined in an exception-specification. */
24021 saved_message = parser->type_definition_forbidden_message;
24022 parser->type_definition_forbidden_message
24023 = G_("types may not be defined in an exception-specification");
24025 expr = cp_parser_constant_expression (parser);
24027 /* Restore the saved message. */
24028 parser->type_definition_forbidden_message = saved_message;
24030 else
24032 expr = cp_parser_expression (parser);
24033 *consumed_expr = true;
24036 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24038 else
24040 expr = boolean_true_node;
24041 if (!require_constexpr)
24042 *consumed_expr = false;
24045 /* We cannot build a noexcept-spec right away because this will check
24046 that expr is a constexpr. */
24047 if (!return_cond)
24048 return build_noexcept_spec (expr, tf_warning_or_error);
24049 else
24050 return expr;
24052 else
24053 return NULL_TREE;
24056 /* Parse an (optional) exception-specification.
24058 exception-specification:
24059 throw ( type-id-list [opt] )
24061 Returns a TREE_LIST representing the exception-specification. The
24062 TREE_VALUE of each node is a type. */
24064 static tree
24065 cp_parser_exception_specification_opt (cp_parser* parser)
24067 cp_token *token;
24068 tree type_id_list;
24069 const char *saved_message;
24071 /* Peek at the next token. */
24072 token = cp_lexer_peek_token (parser->lexer);
24074 /* Is it a noexcept-specification? */
24075 type_id_list = cp_parser_noexcept_specification_opt (parser, true, NULL,
24076 false);
24077 if (type_id_list != NULL_TREE)
24078 return type_id_list;
24080 /* If it's not `throw', then there's no exception-specification. */
24081 if (!cp_parser_is_keyword (token, RID_THROW))
24082 return NULL_TREE;
24084 location_t loc = token->location;
24086 /* Consume the `throw'. */
24087 cp_lexer_consume_token (parser->lexer);
24089 /* Look for the `('. */
24090 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24092 /* Peek at the next token. */
24093 token = cp_lexer_peek_token (parser->lexer);
24094 /* If it's not a `)', then there is a type-id-list. */
24095 if (token->type != CPP_CLOSE_PAREN)
24097 /* Types may not be defined in an exception-specification. */
24098 saved_message = parser->type_definition_forbidden_message;
24099 parser->type_definition_forbidden_message
24100 = G_("types may not be defined in an exception-specification");
24101 /* Parse the type-id-list. */
24102 type_id_list = cp_parser_type_id_list (parser);
24103 /* Restore the saved message. */
24104 parser->type_definition_forbidden_message = saved_message;
24106 if (cxx_dialect >= cxx1z)
24108 error_at (loc, "ISO C++1z does not allow dynamic exception "
24109 "specifications");
24110 type_id_list = NULL_TREE;
24112 else if (cxx_dialect >= cxx11 && !in_system_header_at (loc))
24113 warning_at (loc, OPT_Wdeprecated,
24114 "dynamic exception specifications are deprecated in "
24115 "C++11");
24117 /* In C++17, throw() is equivalent to noexcept (true). throw()
24118 is deprecated in C++11 and above as well, but is still widely used,
24119 so don't warn about it yet. */
24120 else if (cxx_dialect >= cxx1z)
24121 type_id_list = noexcept_true_spec;
24122 else
24123 type_id_list = empty_except_spec;
24125 /* Look for the `)'. */
24126 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24128 return type_id_list;
24131 /* Parse an (optional) type-id-list.
24133 type-id-list:
24134 type-id ... [opt]
24135 type-id-list , type-id ... [opt]
24137 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
24138 in the order that the types were presented. */
24140 static tree
24141 cp_parser_type_id_list (cp_parser* parser)
24143 tree types = NULL_TREE;
24145 while (true)
24147 cp_token *token;
24148 tree type;
24150 token = cp_lexer_peek_token (parser->lexer);
24152 /* Get the next type-id. */
24153 type = cp_parser_type_id (parser);
24154 /* Check for invalid 'auto'. */
24155 if (flag_concepts && type_uses_auto (type))
24157 error_at (token->location,
24158 "invalid use of %<auto%> in exception-specification");
24159 type = error_mark_node;
24161 /* Parse the optional ellipsis. */
24162 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24164 /* Consume the `...'. */
24165 cp_lexer_consume_token (parser->lexer);
24167 /* Turn the type into a pack expansion expression. */
24168 type = make_pack_expansion (type);
24170 /* Add it to the list. */
24171 types = add_exception_specifier (types, type, /*complain=*/1);
24172 /* Peek at the next token. */
24173 token = cp_lexer_peek_token (parser->lexer);
24174 /* If it is not a `,', we are done. */
24175 if (token->type != CPP_COMMA)
24176 break;
24177 /* Consume the `,'. */
24178 cp_lexer_consume_token (parser->lexer);
24181 return nreverse (types);
24184 /* Parse a try-block.
24186 try-block:
24187 try compound-statement handler-seq */
24189 static tree
24190 cp_parser_try_block (cp_parser* parser)
24192 tree try_block;
24194 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
24195 if (parser->in_function_body
24196 && DECL_DECLARED_CONSTEXPR_P (current_function_decl))
24197 error ("%<try%> in %<constexpr%> function");
24199 try_block = begin_try_block ();
24200 cp_parser_compound_statement (parser, NULL, BCS_TRY_BLOCK, false);
24201 finish_try_block (try_block);
24202 cp_parser_handler_seq (parser);
24203 finish_handler_sequence (try_block);
24205 return try_block;
24208 /* Parse a function-try-block.
24210 function-try-block:
24211 try ctor-initializer [opt] function-body handler-seq */
24213 static bool
24214 cp_parser_function_try_block (cp_parser* parser)
24216 tree compound_stmt;
24217 tree try_block;
24218 bool ctor_initializer_p;
24220 /* Look for the `try' keyword. */
24221 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
24222 return false;
24223 /* Let the rest of the front end know where we are. */
24224 try_block = begin_function_try_block (&compound_stmt);
24225 /* Parse the function-body. */
24226 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
24227 (parser, /*in_function_try_block=*/true);
24228 /* We're done with the `try' part. */
24229 finish_function_try_block (try_block);
24230 /* Parse the handlers. */
24231 cp_parser_handler_seq (parser);
24232 /* We're done with the handlers. */
24233 finish_function_handler_sequence (try_block, compound_stmt);
24235 return ctor_initializer_p;
24238 /* Parse a handler-seq.
24240 handler-seq:
24241 handler handler-seq [opt] */
24243 static void
24244 cp_parser_handler_seq (cp_parser* parser)
24246 while (true)
24248 cp_token *token;
24250 /* Parse the handler. */
24251 cp_parser_handler (parser);
24252 /* Peek at the next token. */
24253 token = cp_lexer_peek_token (parser->lexer);
24254 /* If it's not `catch' then there are no more handlers. */
24255 if (!cp_parser_is_keyword (token, RID_CATCH))
24256 break;
24260 /* Parse a handler.
24262 handler:
24263 catch ( exception-declaration ) compound-statement */
24265 static void
24266 cp_parser_handler (cp_parser* parser)
24268 tree handler;
24269 tree declaration;
24271 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
24272 handler = begin_handler ();
24273 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24274 declaration = cp_parser_exception_declaration (parser);
24275 finish_handler_parms (declaration, handler);
24276 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24277 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
24278 finish_handler (handler);
24281 /* Parse an exception-declaration.
24283 exception-declaration:
24284 type-specifier-seq declarator
24285 type-specifier-seq abstract-declarator
24286 type-specifier-seq
24289 Returns a VAR_DECL for the declaration, or NULL_TREE if the
24290 ellipsis variant is used. */
24292 static tree
24293 cp_parser_exception_declaration (cp_parser* parser)
24295 cp_decl_specifier_seq type_specifiers;
24296 cp_declarator *declarator;
24297 const char *saved_message;
24299 /* If it's an ellipsis, it's easy to handle. */
24300 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24302 /* Consume the `...' token. */
24303 cp_lexer_consume_token (parser->lexer);
24304 return NULL_TREE;
24307 /* Types may not be defined in exception-declarations. */
24308 saved_message = parser->type_definition_forbidden_message;
24309 parser->type_definition_forbidden_message
24310 = G_("types may not be defined in exception-declarations");
24312 /* Parse the type-specifier-seq. */
24313 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24314 /*is_trailing_return=*/false,
24315 &type_specifiers);
24316 /* If it's a `)', then there is no declarator. */
24317 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24318 declarator = NULL;
24319 else
24320 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
24321 /*ctor_dtor_or_conv_p=*/NULL,
24322 /*parenthesized_p=*/NULL,
24323 /*member_p=*/false,
24324 /*friend_p=*/false);
24326 /* Restore the saved message. */
24327 parser->type_definition_forbidden_message = saved_message;
24329 if (!type_specifiers.any_specifiers_p)
24330 return error_mark_node;
24332 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
24335 /* Parse a throw-expression.
24337 throw-expression:
24338 throw assignment-expression [opt]
24340 Returns a THROW_EXPR representing the throw-expression. */
24342 static tree
24343 cp_parser_throw_expression (cp_parser* parser)
24345 tree expression;
24346 cp_token* token;
24348 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
24349 token = cp_lexer_peek_token (parser->lexer);
24350 /* Figure out whether or not there is an assignment-expression
24351 following the "throw" keyword. */
24352 if (token->type == CPP_COMMA
24353 || token->type == CPP_SEMICOLON
24354 || token->type == CPP_CLOSE_PAREN
24355 || token->type == CPP_CLOSE_SQUARE
24356 || token->type == CPP_CLOSE_BRACE
24357 || token->type == CPP_COLON)
24358 expression = NULL_TREE;
24359 else
24360 expression = cp_parser_assignment_expression (parser);
24362 return build_throw (expression);
24365 /* GNU Extensions */
24367 /* Parse an (optional) asm-specification.
24369 asm-specification:
24370 asm ( string-literal )
24372 If the asm-specification is present, returns a STRING_CST
24373 corresponding to the string-literal. Otherwise, returns
24374 NULL_TREE. */
24376 static tree
24377 cp_parser_asm_specification_opt (cp_parser* parser)
24379 cp_token *token;
24380 tree asm_specification;
24382 /* Peek at the next token. */
24383 token = cp_lexer_peek_token (parser->lexer);
24384 /* If the next token isn't the `asm' keyword, then there's no
24385 asm-specification. */
24386 if (!cp_parser_is_keyword (token, RID_ASM))
24387 return NULL_TREE;
24389 /* Consume the `asm' token. */
24390 cp_lexer_consume_token (parser->lexer);
24391 /* Look for the `('. */
24392 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24394 /* Look for the string-literal. */
24395 asm_specification = cp_parser_string_literal (parser, false, false);
24397 /* Look for the `)'. */
24398 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24400 return asm_specification;
24403 /* Parse an asm-operand-list.
24405 asm-operand-list:
24406 asm-operand
24407 asm-operand-list , asm-operand
24409 asm-operand:
24410 string-literal ( expression )
24411 [ string-literal ] string-literal ( expression )
24413 Returns a TREE_LIST representing the operands. The TREE_VALUE of
24414 each node is the expression. The TREE_PURPOSE is itself a
24415 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
24416 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
24417 is a STRING_CST for the string literal before the parenthesis. Returns
24418 ERROR_MARK_NODE if any of the operands are invalid. */
24420 static tree
24421 cp_parser_asm_operand_list (cp_parser* parser)
24423 tree asm_operands = NULL_TREE;
24424 bool invalid_operands = false;
24426 while (true)
24428 tree string_literal;
24429 tree expression;
24430 tree name;
24432 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
24434 /* Consume the `[' token. */
24435 cp_lexer_consume_token (parser->lexer);
24436 /* Read the operand name. */
24437 name = cp_parser_identifier (parser);
24438 if (name != error_mark_node)
24439 name = build_string (IDENTIFIER_LENGTH (name),
24440 IDENTIFIER_POINTER (name));
24441 /* Look for the closing `]'. */
24442 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
24444 else
24445 name = NULL_TREE;
24446 /* Look for the string-literal. */
24447 string_literal = cp_parser_string_literal (parser, false, false);
24449 /* Look for the `('. */
24450 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24451 /* Parse the expression. */
24452 expression = cp_parser_expression (parser);
24453 /* Look for the `)'. */
24454 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24456 if (name == error_mark_node
24457 || string_literal == error_mark_node
24458 || expression == error_mark_node)
24459 invalid_operands = true;
24461 /* Add this operand to the list. */
24462 asm_operands = tree_cons (build_tree_list (name, string_literal),
24463 expression,
24464 asm_operands);
24465 /* If the next token is not a `,', there are no more
24466 operands. */
24467 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24468 break;
24469 /* Consume the `,'. */
24470 cp_lexer_consume_token (parser->lexer);
24473 return invalid_operands ? error_mark_node : nreverse (asm_operands);
24476 /* Parse an asm-clobber-list.
24478 asm-clobber-list:
24479 string-literal
24480 asm-clobber-list , string-literal
24482 Returns a TREE_LIST, indicating the clobbers in the order that they
24483 appeared. The TREE_VALUE of each node is a STRING_CST. */
24485 static tree
24486 cp_parser_asm_clobber_list (cp_parser* parser)
24488 tree clobbers = NULL_TREE;
24490 while (true)
24492 tree string_literal;
24494 /* Look for the string literal. */
24495 string_literal = cp_parser_string_literal (parser, false, false);
24496 /* Add it to the list. */
24497 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
24498 /* If the next token is not a `,', then the list is
24499 complete. */
24500 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24501 break;
24502 /* Consume the `,' token. */
24503 cp_lexer_consume_token (parser->lexer);
24506 return clobbers;
24509 /* Parse an asm-label-list.
24511 asm-label-list:
24512 identifier
24513 asm-label-list , identifier
24515 Returns a TREE_LIST, indicating the labels in the order that they
24516 appeared. The TREE_VALUE of each node is a label. */
24518 static tree
24519 cp_parser_asm_label_list (cp_parser* parser)
24521 tree labels = NULL_TREE;
24523 while (true)
24525 tree identifier, label, name;
24527 /* Look for the identifier. */
24528 identifier = cp_parser_identifier (parser);
24529 if (!error_operand_p (identifier))
24531 label = lookup_label (identifier);
24532 if (TREE_CODE (label) == LABEL_DECL)
24534 TREE_USED (label) = 1;
24535 check_goto (label);
24536 name = build_string (IDENTIFIER_LENGTH (identifier),
24537 IDENTIFIER_POINTER (identifier));
24538 labels = tree_cons (name, label, labels);
24541 /* If the next token is not a `,', then the list is
24542 complete. */
24543 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24544 break;
24545 /* Consume the `,' token. */
24546 cp_lexer_consume_token (parser->lexer);
24549 return nreverse (labels);
24552 /* Return TRUE iff the next tokens in the stream are possibly the
24553 beginning of a GNU extension attribute. */
24555 static bool
24556 cp_next_tokens_can_be_gnu_attribute_p (cp_parser *parser)
24558 return cp_nth_tokens_can_be_gnu_attribute_p (parser, 1);
24561 /* Return TRUE iff the next tokens in the stream are possibly the
24562 beginning of a standard C++-11 attribute specifier. */
24564 static bool
24565 cp_next_tokens_can_be_std_attribute_p (cp_parser *parser)
24567 return cp_nth_tokens_can_be_std_attribute_p (parser, 1);
24570 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24571 beginning of a standard C++-11 attribute specifier. */
24573 static bool
24574 cp_nth_tokens_can_be_std_attribute_p (cp_parser *parser, size_t n)
24576 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24578 return (cxx_dialect >= cxx11
24579 && ((token->type == CPP_KEYWORD && token->keyword == RID_ALIGNAS)
24580 || (token->type == CPP_OPEN_SQUARE
24581 && (token = cp_lexer_peek_nth_token (parser->lexer, n + 1))
24582 && token->type == CPP_OPEN_SQUARE)));
24585 /* Return TRUE iff the next Nth tokens in the stream are possibly the
24586 beginning of a GNU extension attribute. */
24588 static bool
24589 cp_nth_tokens_can_be_gnu_attribute_p (cp_parser *parser, size_t n)
24591 cp_token *token = cp_lexer_peek_nth_token (parser->lexer, n);
24593 return token->type == CPP_KEYWORD && token->keyword == RID_ATTRIBUTE;
24596 /* Return true iff the next tokens can be the beginning of either a
24597 GNU attribute list, or a standard C++11 attribute sequence. */
24599 static bool
24600 cp_next_tokens_can_be_attribute_p (cp_parser *parser)
24602 return (cp_next_tokens_can_be_gnu_attribute_p (parser)
24603 || cp_next_tokens_can_be_std_attribute_p (parser));
24606 /* Return true iff the next Nth tokens can be the beginning of either
24607 a GNU attribute list, or a standard C++11 attribute sequence. */
24609 static bool
24610 cp_nth_tokens_can_be_attribute_p (cp_parser *parser, size_t n)
24612 return (cp_nth_tokens_can_be_gnu_attribute_p (parser, n)
24613 || cp_nth_tokens_can_be_std_attribute_p (parser, n));
24616 /* Parse either a standard C++-11 attribute-specifier-seq, or a series
24617 of GNU attributes, or return NULL. */
24619 static tree
24620 cp_parser_attributes_opt (cp_parser *parser)
24622 if (cp_next_tokens_can_be_gnu_attribute_p (parser))
24623 return cp_parser_gnu_attributes_opt (parser);
24624 return cp_parser_std_attribute_spec_seq (parser);
24627 #define CILK_SIMD_FN_CLAUSE_MASK \
24628 ((OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_VECTORLENGTH) \
24629 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_LINEAR) \
24630 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_UNIFORM) \
24631 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_MASK) \
24632 | (OMP_CLAUSE_MASK_1 << PRAGMA_CILK_CLAUSE_NOMASK))
24634 /* Parses the Cilk Plus SIMD-enabled function's attribute. Syntax:
24635 vector [(<clauses>)] */
24637 static void
24638 cp_parser_cilk_simd_fn_vector_attrs (cp_parser *parser, cp_token *v_token)
24640 bool first_p = parser->cilk_simd_fn_info == NULL;
24641 cp_token *token = v_token;
24642 if (first_p)
24644 parser->cilk_simd_fn_info = XNEW (cp_omp_declare_simd_data);
24645 parser->cilk_simd_fn_info->error_seen = false;
24646 parser->cilk_simd_fn_info->fndecl_seen = false;
24647 parser->cilk_simd_fn_info->tokens = vNULL;
24648 parser->cilk_simd_fn_info->clauses = NULL_TREE;
24650 int paren_scope = 0;
24651 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24653 cp_lexer_consume_token (parser->lexer);
24654 v_token = cp_lexer_peek_token (parser->lexer);
24655 paren_scope++;
24657 while (paren_scope > 0)
24659 token = cp_lexer_peek_token (parser->lexer);
24660 if (token->type == CPP_OPEN_PAREN)
24661 paren_scope++;
24662 else if (token->type == CPP_CLOSE_PAREN)
24663 paren_scope--;
24664 /* Do not push the last ')' */
24665 if (!(token->type == CPP_CLOSE_PAREN && paren_scope == 0))
24666 cp_lexer_consume_token (parser->lexer);
24669 token->type = CPP_PRAGMA_EOL;
24670 parser->lexer->next_token = token;
24671 cp_lexer_consume_token (parser->lexer);
24673 struct cp_token_cache *cp
24674 = cp_token_cache_new (v_token, cp_lexer_peek_token (parser->lexer));
24675 parser->cilk_simd_fn_info->tokens.safe_push (cp);
24678 /* Parse an (optional) series of attributes.
24680 attributes:
24681 attributes attribute
24683 attribute:
24684 __attribute__ (( attribute-list [opt] ))
24686 The return value is as for cp_parser_gnu_attribute_list. */
24688 static tree
24689 cp_parser_gnu_attributes_opt (cp_parser* parser)
24691 tree attributes = NULL_TREE;
24693 while (true)
24695 cp_token *token;
24696 tree attribute_list;
24697 bool ok = true;
24699 /* Peek at the next token. */
24700 token = cp_lexer_peek_token (parser->lexer);
24701 /* If it's not `__attribute__', then we're done. */
24702 if (token->keyword != RID_ATTRIBUTE)
24703 break;
24705 /* Consume the `__attribute__' keyword. */
24706 cp_lexer_consume_token (parser->lexer);
24707 /* Look for the two `(' tokens. */
24708 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24709 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24711 /* Peek at the next token. */
24712 token = cp_lexer_peek_token (parser->lexer);
24713 if (token->type != CPP_CLOSE_PAREN)
24714 /* Parse the attribute-list. */
24715 attribute_list = cp_parser_gnu_attribute_list (parser);
24716 else
24717 /* If the next token is a `)', then there is no attribute
24718 list. */
24719 attribute_list = NULL;
24721 /* Look for the two `)' tokens. */
24722 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24723 ok = false;
24724 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24725 ok = false;
24726 if (!ok)
24727 cp_parser_skip_to_end_of_statement (parser);
24729 /* Add these new attributes to the list. */
24730 attributes = chainon (attributes, attribute_list);
24733 return attributes;
24736 /* Parse a GNU attribute-list.
24738 attribute-list:
24739 attribute
24740 attribute-list , attribute
24742 attribute:
24743 identifier
24744 identifier ( identifier )
24745 identifier ( identifier , expression-list )
24746 identifier ( expression-list )
24748 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
24749 to an attribute. The TREE_PURPOSE of each node is the identifier
24750 indicating which attribute is in use. The TREE_VALUE represents
24751 the arguments, if any. */
24753 static tree
24754 cp_parser_gnu_attribute_list (cp_parser* parser)
24756 tree attribute_list = NULL_TREE;
24757 bool save_translate_strings_p = parser->translate_strings_p;
24759 parser->translate_strings_p = false;
24760 while (true)
24762 cp_token *token;
24763 tree identifier;
24764 tree attribute;
24766 /* Look for the identifier. We also allow keywords here; for
24767 example `__attribute__ ((const))' is legal. */
24768 token = cp_lexer_peek_token (parser->lexer);
24769 if (token->type == CPP_NAME
24770 || token->type == CPP_KEYWORD)
24772 tree arguments = NULL_TREE;
24774 /* Consume the token, but save it since we need it for the
24775 SIMD enabled function parsing. */
24776 cp_token *id_token = cp_lexer_consume_token (parser->lexer);
24778 /* Save away the identifier that indicates which attribute
24779 this is. */
24780 identifier = (token->type == CPP_KEYWORD)
24781 /* For keywords, use the canonical spelling, not the
24782 parsed identifier. */
24783 ? ridpointers[(int) token->keyword]
24784 : id_token->u.value;
24786 attribute = build_tree_list (identifier, NULL_TREE);
24788 /* Peek at the next token. */
24789 token = cp_lexer_peek_token (parser->lexer);
24790 /* If it's an `(', then parse the attribute arguments. */
24791 if (token->type == CPP_OPEN_PAREN)
24793 vec<tree, va_gc> *vec;
24794 int attr_flag = (attribute_takes_identifier_p (identifier)
24795 ? id_attr : normal_attr);
24796 if (is_cilkplus_vector_p (identifier))
24798 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24799 continue;
24801 else
24802 vec = cp_parser_parenthesized_expression_list
24803 (parser, attr_flag, /*cast_p=*/false,
24804 /*allow_expansion_p=*/false,
24805 /*non_constant_p=*/NULL);
24806 if (vec == NULL)
24807 arguments = error_mark_node;
24808 else
24810 arguments = build_tree_list_vec (vec);
24811 release_tree_vector (vec);
24813 /* Save the arguments away. */
24814 TREE_VALUE (attribute) = arguments;
24816 else if (is_cilkplus_vector_p (identifier))
24818 cp_parser_cilk_simd_fn_vector_attrs (parser, id_token);
24819 continue;
24822 if (arguments != error_mark_node)
24824 /* Add this attribute to the list. */
24825 TREE_CHAIN (attribute) = attribute_list;
24826 attribute_list = attribute;
24829 token = cp_lexer_peek_token (parser->lexer);
24831 /* Now, look for more attributes. If the next token isn't a
24832 `,', we're done. */
24833 if (token->type != CPP_COMMA)
24834 break;
24836 /* Consume the comma and keep going. */
24837 cp_lexer_consume_token (parser->lexer);
24839 parser->translate_strings_p = save_translate_strings_p;
24841 /* We built up the list in reverse order. */
24842 return nreverse (attribute_list);
24845 /* Parse a standard C++11 attribute.
24847 The returned representation is a TREE_LIST which TREE_PURPOSE is
24848 the scoped name of the attribute, and the TREE_VALUE is its
24849 arguments list.
24851 Note that the scoped name of the attribute is itself a TREE_LIST
24852 which TREE_PURPOSE is the namespace of the attribute, and
24853 TREE_VALUE its name. This is unlike a GNU attribute -- as parsed
24854 by cp_parser_gnu_attribute_list -- that doesn't have any namespace
24855 and which TREE_PURPOSE is directly the attribute name.
24857 Clients of the attribute code should use get_attribute_namespace
24858 and get_attribute_name to get the actual namespace and name of
24859 attributes, regardless of their being GNU or C++11 attributes.
24861 attribute:
24862 attribute-token attribute-argument-clause [opt]
24864 attribute-token:
24865 identifier
24866 attribute-scoped-token
24868 attribute-scoped-token:
24869 attribute-namespace :: identifier
24871 attribute-namespace:
24872 identifier
24874 attribute-argument-clause:
24875 ( balanced-token-seq )
24877 balanced-token-seq:
24878 balanced-token [opt]
24879 balanced-token-seq balanced-token
24881 balanced-token:
24882 ( balanced-token-seq )
24883 [ balanced-token-seq ]
24884 { balanced-token-seq }. */
24886 static tree
24887 cp_parser_std_attribute (cp_parser *parser, tree attr_ns)
24889 tree attribute, attr_id = NULL_TREE, arguments;
24890 cp_token *token;
24892 /* First, parse name of the attribute, a.k.a attribute-token. */
24894 token = cp_lexer_peek_token (parser->lexer);
24895 if (token->type == CPP_NAME)
24896 attr_id = token->u.value;
24897 else if (token->type == CPP_KEYWORD)
24898 attr_id = ridpointers[(int) token->keyword];
24899 else if (token->flags & NAMED_OP)
24900 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24902 if (attr_id == NULL_TREE)
24903 return NULL_TREE;
24905 cp_lexer_consume_token (parser->lexer);
24907 token = cp_lexer_peek_token (parser->lexer);
24908 if (token->type == CPP_SCOPE)
24910 /* We are seeing a scoped attribute token. */
24912 cp_lexer_consume_token (parser->lexer);
24913 if (attr_ns)
24914 error_at (token->location, "attribute using prefix used together "
24915 "with scoped attribute token");
24916 attr_ns = attr_id;
24918 token = cp_lexer_consume_token (parser->lexer);
24919 if (token->type == CPP_NAME)
24920 attr_id = token->u.value;
24921 else if (token->type == CPP_KEYWORD)
24922 attr_id = ridpointers[(int) token->keyword];
24923 else if (token->flags & NAMED_OP)
24924 attr_id = get_identifier (cpp_type2name (token->type, token->flags));
24925 else
24927 error_at (token->location,
24928 "expected an identifier for the attribute name");
24929 return error_mark_node;
24931 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24932 NULL_TREE);
24933 token = cp_lexer_peek_token (parser->lexer);
24935 else if (attr_ns)
24936 attribute = build_tree_list (build_tree_list (attr_ns, attr_id),
24937 NULL_TREE);
24938 else
24940 attribute = build_tree_list (build_tree_list (NULL_TREE, attr_id),
24941 NULL_TREE);
24942 /* C++11 noreturn attribute is equivalent to GNU's. */
24943 if (is_attribute_p ("noreturn", attr_id))
24944 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24945 /* C++14 deprecated attribute is equivalent to GNU's. */
24946 else if (is_attribute_p ("deprecated", attr_id))
24947 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24948 /* C++17 fallthrough attribute is equivalent to GNU's. */
24949 else if (is_attribute_p ("fallthrough", attr_id))
24950 TREE_PURPOSE (TREE_PURPOSE (attribute)) = get_identifier ("gnu");
24951 /* Transactional Memory TS optimize_for_synchronized attribute is
24952 equivalent to GNU transaction_callable. */
24953 else if (is_attribute_p ("optimize_for_synchronized", attr_id))
24954 TREE_PURPOSE (attribute)
24955 = get_identifier ("transaction_callable");
24956 /* Transactional Memory attributes are GNU attributes. */
24957 else if (tm_attr_to_mask (attr_id))
24958 TREE_PURPOSE (attribute) = attr_id;
24961 /* Now parse the optional argument clause of the attribute. */
24963 if (token->type != CPP_OPEN_PAREN)
24964 return attribute;
24967 vec<tree, va_gc> *vec;
24968 int attr_flag = normal_attr;
24970 if (attr_ns == get_identifier ("gnu")
24971 && attribute_takes_identifier_p (attr_id))
24972 /* A GNU attribute that takes an identifier in parameter. */
24973 attr_flag = id_attr;
24975 vec = cp_parser_parenthesized_expression_list
24976 (parser, attr_flag, /*cast_p=*/false,
24977 /*allow_expansion_p=*/true,
24978 /*non_constant_p=*/NULL);
24979 if (vec == NULL)
24980 arguments = error_mark_node;
24981 else
24983 arguments = build_tree_list_vec (vec);
24984 release_tree_vector (vec);
24987 if (arguments == error_mark_node)
24988 attribute = error_mark_node;
24989 else
24990 TREE_VALUE (attribute) = arguments;
24993 return attribute;
24996 /* Check that the attribute ATTRIBUTE appears at most once in the
24997 attribute-list ATTRIBUTES. This is enforced for noreturn (7.6.3)
24998 and deprecated (7.6.5). Note that carries_dependency (7.6.4)
24999 isn't implemented yet in GCC. */
25001 static void
25002 cp_parser_check_std_attribute (tree attributes, tree attribute)
25004 if (attributes)
25006 tree name = get_attribute_name (attribute);
25007 if (is_attribute_p ("noreturn", name)
25008 && lookup_attribute ("noreturn", attributes))
25009 error ("attribute %<noreturn%> can appear at most once "
25010 "in an attribute-list");
25011 else if (is_attribute_p ("deprecated", name)
25012 && lookup_attribute ("deprecated", attributes))
25013 error ("attribute %<deprecated%> can appear at most once "
25014 "in an attribute-list");
25018 /* Parse a list of standard C++-11 attributes.
25020 attribute-list:
25021 attribute [opt]
25022 attribute-list , attribute[opt]
25023 attribute ...
25024 attribute-list , attribute ...
25027 static tree
25028 cp_parser_std_attribute_list (cp_parser *parser, tree attr_ns)
25030 tree attributes = NULL_TREE, attribute = NULL_TREE;
25031 cp_token *token = NULL;
25033 while (true)
25035 attribute = cp_parser_std_attribute (parser, attr_ns);
25036 if (attribute == error_mark_node)
25037 break;
25038 if (attribute != NULL_TREE)
25040 cp_parser_check_std_attribute (attributes, attribute);
25041 TREE_CHAIN (attribute) = attributes;
25042 attributes = attribute;
25044 token = cp_lexer_peek_token (parser->lexer);
25045 if (token->type == CPP_ELLIPSIS)
25047 cp_lexer_consume_token (parser->lexer);
25048 if (attribute == NULL_TREE)
25049 error_at (token->location,
25050 "expected attribute before %<...%>");
25051 else
25053 tree pack = make_pack_expansion (TREE_VALUE (attribute));
25054 if (pack == error_mark_node)
25055 return error_mark_node;
25056 TREE_VALUE (attribute) = pack;
25058 token = cp_lexer_peek_token (parser->lexer);
25060 if (token->type != CPP_COMMA)
25061 break;
25062 cp_lexer_consume_token (parser->lexer);
25064 attributes = nreverse (attributes);
25065 return attributes;
25068 /* Parse a standard C++-11 attribute specifier.
25070 attribute-specifier:
25071 [ [ attribute-using-prefix [opt] attribute-list ] ]
25072 alignment-specifier
25074 attribute-using-prefix:
25075 using attribute-namespace :
25077 alignment-specifier:
25078 alignas ( type-id ... [opt] )
25079 alignas ( alignment-expression ... [opt] ). */
25081 static tree
25082 cp_parser_std_attribute_spec (cp_parser *parser)
25084 tree attributes = NULL_TREE;
25085 cp_token *token = cp_lexer_peek_token (parser->lexer);
25087 if (token->type == CPP_OPEN_SQUARE
25088 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_SQUARE)
25090 tree attr_ns = NULL_TREE;
25092 cp_lexer_consume_token (parser->lexer);
25093 cp_lexer_consume_token (parser->lexer);
25095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
25097 token = cp_lexer_peek_nth_token (parser->lexer, 2);
25098 if (token->type == CPP_NAME)
25099 attr_ns = token->u.value;
25100 else if (token->type == CPP_KEYWORD)
25101 attr_ns = ridpointers[(int) token->keyword];
25102 else if (token->flags & NAMED_OP)
25103 attr_ns = get_identifier (cpp_type2name (token->type,
25104 token->flags));
25105 if (attr_ns
25106 && cp_lexer_nth_token_is (parser->lexer, 3, CPP_COLON))
25108 if (cxx_dialect < cxx1z
25109 && !in_system_header_at (input_location))
25110 pedwarn (input_location, 0,
25111 "attribute using prefix only available "
25112 "with -std=c++1z or -std=gnu++1z");
25114 cp_lexer_consume_token (parser->lexer);
25115 cp_lexer_consume_token (parser->lexer);
25116 cp_lexer_consume_token (parser->lexer);
25118 else
25119 attr_ns = NULL_TREE;
25122 attributes = cp_parser_std_attribute_list (parser, attr_ns);
25124 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE)
25125 || !cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
25126 cp_parser_skip_to_end_of_statement (parser);
25127 else
25128 /* Warn about parsing c++11 attribute in non-c++1 mode, only
25129 when we are sure that we have actually parsed them. */
25130 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25132 else
25134 tree alignas_expr;
25136 /* Look for an alignment-specifier. */
25138 token = cp_lexer_peek_token (parser->lexer);
25140 if (token->type != CPP_KEYWORD
25141 || token->keyword != RID_ALIGNAS)
25142 return NULL_TREE;
25144 cp_lexer_consume_token (parser->lexer);
25145 maybe_warn_cpp0x (CPP0X_ATTRIBUTES);
25147 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN) == NULL)
25149 cp_parser_error (parser, "expected %<(%>");
25150 return error_mark_node;
25153 cp_parser_parse_tentatively (parser);
25154 alignas_expr = cp_parser_type_id (parser);
25156 if (!cp_parser_parse_definitely (parser))
25158 alignas_expr = cp_parser_assignment_expression (parser);
25159 if (alignas_expr == error_mark_node)
25160 cp_parser_skip_to_end_of_statement (parser);
25161 if (alignas_expr == NULL_TREE
25162 || alignas_expr == error_mark_node)
25163 return alignas_expr;
25166 alignas_expr = cxx_alignas_expr (alignas_expr);
25167 alignas_expr = build_tree_list (NULL_TREE, alignas_expr);
25169 /* Handle alignas (pack...). */
25170 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
25172 cp_lexer_consume_token (parser->lexer);
25173 alignas_expr = make_pack_expansion (alignas_expr);
25176 /* Something went wrong, so don't build the attribute. */
25177 if (alignas_expr == error_mark_node)
25178 return error_mark_node;
25180 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) == NULL)
25182 cp_parser_error (parser, "expected %<)%>");
25183 return error_mark_node;
25186 /* Build the C++-11 representation of an 'aligned'
25187 attribute. */
25188 attributes =
25189 build_tree_list (build_tree_list (get_identifier ("gnu"),
25190 get_identifier ("aligned")),
25191 alignas_expr);
25194 return attributes;
25197 /* Parse a standard C++-11 attribute-specifier-seq.
25199 attribute-specifier-seq:
25200 attribute-specifier-seq [opt] attribute-specifier
25203 static tree
25204 cp_parser_std_attribute_spec_seq (cp_parser *parser)
25206 tree attr_specs = NULL_TREE;
25207 tree attr_last = NULL_TREE;
25209 while (true)
25211 tree attr_spec = cp_parser_std_attribute_spec (parser);
25212 if (attr_spec == NULL_TREE)
25213 break;
25214 if (attr_spec == error_mark_node)
25215 return error_mark_node;
25217 if (attr_last)
25218 TREE_CHAIN (attr_last) = attr_spec;
25219 else
25220 attr_specs = attr_last = attr_spec;
25221 attr_last = tree_last (attr_last);
25224 return attr_specs;
25227 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
25228 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
25229 current value of the PEDANTIC flag, regardless of whether or not
25230 the `__extension__' keyword is present. The caller is responsible
25231 for restoring the value of the PEDANTIC flag. */
25233 static bool
25234 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
25236 /* Save the old value of the PEDANTIC flag. */
25237 *saved_pedantic = pedantic;
25239 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
25241 /* Consume the `__extension__' token. */
25242 cp_lexer_consume_token (parser->lexer);
25243 /* We're not being pedantic while the `__extension__' keyword is
25244 in effect. */
25245 pedantic = 0;
25247 return true;
25250 return false;
25253 /* Parse a label declaration.
25255 label-declaration:
25256 __label__ label-declarator-seq ;
25258 label-declarator-seq:
25259 identifier , label-declarator-seq
25260 identifier */
25262 static void
25263 cp_parser_label_declaration (cp_parser* parser)
25265 /* Look for the `__label__' keyword. */
25266 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
25268 while (true)
25270 tree identifier;
25272 /* Look for an identifier. */
25273 identifier = cp_parser_identifier (parser);
25274 /* If we failed, stop. */
25275 if (identifier == error_mark_node)
25276 break;
25277 /* Declare it as a label. */
25278 finish_label_decl (identifier);
25279 /* If the next token is a `;', stop. */
25280 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25281 break;
25282 /* Look for the `,' separating the label declarations. */
25283 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
25286 /* Look for the final `;'. */
25287 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25290 // -------------------------------------------------------------------------- //
25291 // Requires Clause
25293 // Parse a requires clause.
25295 // requires-clause:
25296 // 'requires' logical-or-expression
25298 // The required logical-or-expression must be a constant expression. Note
25299 // that we don't check that the expression is constepxr here. We defer until
25300 // we analyze constraints and then, we only check atomic constraints.
25301 static tree
25302 cp_parser_requires_clause (cp_parser *parser)
25304 // Parse the requires clause so that it is not automatically folded.
25305 ++processing_template_decl;
25306 tree expr = cp_parser_binary_expression (parser, false, false,
25307 PREC_NOT_OPERATOR, NULL);
25308 if (check_for_bare_parameter_packs (expr))
25309 expr = error_mark_node;
25310 --processing_template_decl;
25311 return expr;
25314 // Optionally parse a requires clause:
25315 static tree
25316 cp_parser_requires_clause_opt (cp_parser *parser)
25318 cp_token *tok = cp_lexer_peek_token (parser->lexer);
25319 if (tok->keyword != RID_REQUIRES)
25321 if (!flag_concepts && tok->type == CPP_NAME
25322 && tok->u.value == ridpointers[RID_REQUIRES])
25324 error_at (cp_lexer_peek_token (parser->lexer)->location,
25325 "%<requires%> only available with -fconcepts");
25326 /* Parse and discard the requires-clause. */
25327 cp_lexer_consume_token (parser->lexer);
25328 cp_parser_requires_clause (parser);
25330 return NULL_TREE;
25332 cp_lexer_consume_token (parser->lexer);
25333 return cp_parser_requires_clause (parser);
25337 /*---------------------------------------------------------------------------
25338 Requires expressions
25339 ---------------------------------------------------------------------------*/
25341 /* Parse a requires expression
25343 requirement-expression:
25344 'requires' requirement-parameter-list [opt] requirement-body */
25345 static tree
25346 cp_parser_requires_expression (cp_parser *parser)
25348 gcc_assert (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES));
25349 location_t loc = cp_lexer_consume_token (parser->lexer)->location;
25351 /* A requires-expression shall appear only within a concept
25352 definition or a requires-clause.
25354 TODO: Implement this diagnostic correctly. */
25355 if (!processing_template_decl)
25357 error_at (loc, "a requires expression cannot appear outside a template");
25358 cp_parser_skip_to_end_of_statement (parser);
25359 return error_mark_node;
25362 tree parms, reqs;
25364 /* Local parameters are delared as variables within the scope
25365 of the expression. They are not visible past the end of
25366 the expression. Expressions within the requires-expression
25367 are unevaluated. */
25368 struct scope_sentinel
25370 scope_sentinel ()
25372 ++cp_unevaluated_operand;
25373 begin_scope (sk_block, NULL_TREE);
25376 ~scope_sentinel ()
25378 pop_bindings_and_leave_scope ();
25379 --cp_unevaluated_operand;
25381 } s;
25383 /* Parse the optional parameter list. */
25384 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25386 parms = cp_parser_requirement_parameter_list (parser);
25387 if (parms == error_mark_node)
25388 return error_mark_node;
25390 else
25391 parms = NULL_TREE;
25393 /* Parse the requirement body. */
25394 reqs = cp_parser_requirement_body (parser);
25395 if (reqs == error_mark_node)
25396 return error_mark_node;
25399 /* This needs to happen after pop_bindings_and_leave_scope, as it reverses
25400 the parm chain. */
25401 grokparms (parms, &parms);
25402 return finish_requires_expr (parms, reqs);
25405 /* Parse a parameterized requirement.
25407 requirement-parameter-list:
25408 '(' parameter-declaration-clause ')' */
25409 static tree
25410 cp_parser_requirement_parameter_list (cp_parser *parser)
25412 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25413 return error_mark_node;
25415 tree parms = cp_parser_parameter_declaration_clause (parser);
25417 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25418 return error_mark_node;
25420 return parms;
25423 /* Parse the body of a requirement.
25425 requirement-body:
25426 '{' requirement-list '}' */
25427 static tree
25428 cp_parser_requirement_body (cp_parser *parser)
25430 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25431 return error_mark_node;
25433 tree reqs = cp_parser_requirement_list (parser);
25435 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25436 return error_mark_node;
25438 return reqs;
25441 /* Parse a list of requirements.
25443 requirement-list:
25444 requirement
25445 requirement-list ';' requirement[opt] */
25446 static tree
25447 cp_parser_requirement_list (cp_parser *parser)
25449 tree result = NULL_TREE;
25450 while (true)
25452 tree req = cp_parser_requirement (parser);
25453 if (req == error_mark_node)
25454 return error_mark_node;
25456 result = tree_cons (NULL_TREE, req, result);
25458 /* If we see a semi-colon, consume it. */
25459 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25460 cp_lexer_consume_token (parser->lexer);
25462 /* Stop processing at the end of the list. */
25463 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25464 break;
25467 /* Reverse the order of requirements so they are analyzed in
25468 declaration order. */
25469 return nreverse (result);
25472 /* Parse a syntactic requirement or type requirement.
25474 requirement:
25475 simple-requirement
25476 compound-requirement
25477 type-requirement
25478 nested-requirement */
25479 static tree
25480 cp_parser_requirement (cp_parser *parser)
25482 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25483 return cp_parser_compound_requirement (parser);
25484 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
25485 return cp_parser_type_requirement (parser);
25486 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_REQUIRES))
25487 return cp_parser_nested_requirement (parser);
25488 else
25489 return cp_parser_simple_requirement (parser);
25492 /* Parse a simple requirement.
25494 simple-requirement:
25495 expression ';' */
25496 static tree
25497 cp_parser_simple_requirement (cp_parser *parser)
25499 tree expr = cp_parser_expression (parser, NULL, false, false);
25500 if (!expr || expr == error_mark_node)
25501 return error_mark_node;
25503 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25504 return error_mark_node;
25506 return finish_simple_requirement (expr);
25509 /* Parse a type requirement
25511 type-requirement
25512 nested-name-specifier [opt] required-type-name ';'
25514 required-type-name:
25515 type-name
25516 'template' [opt] simple-template-id */
25517 static tree
25518 cp_parser_type_requirement (cp_parser *parser)
25520 cp_lexer_consume_token (parser->lexer);
25522 // Save the scope before parsing name specifiers.
25523 tree saved_scope = parser->scope;
25524 tree saved_object_scope = parser->object_scope;
25525 tree saved_qualifying_scope = parser->qualifying_scope;
25526 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
25527 cp_parser_nested_name_specifier_opt (parser,
25528 /*typename_keyword_p=*/true,
25529 /*check_dependency_p=*/false,
25530 /*type_p=*/true,
25531 /*is_declaration=*/false);
25533 tree type;
25534 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
25536 cp_lexer_consume_token (parser->lexer);
25537 type = cp_parser_template_id (parser,
25538 /*template_keyword_p=*/true,
25539 /*check_dependency=*/false,
25540 /*tag_type=*/none_type,
25541 /*is_declaration=*/false);
25542 type = make_typename_type (parser->scope, type, typename_type,
25543 /*complain=*/tf_error);
25545 else
25546 type = cp_parser_type_name (parser, /*typename_keyword_p=*/true);
25548 if (TREE_CODE (type) == TYPE_DECL)
25549 type = TREE_TYPE (type);
25551 parser->scope = saved_scope;
25552 parser->object_scope = saved_object_scope;
25553 parser->qualifying_scope = saved_qualifying_scope;
25555 if (type == error_mark_node)
25556 cp_parser_skip_to_end_of_statement (parser);
25558 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25559 return error_mark_node;
25560 if (type == error_mark_node)
25561 return error_mark_node;
25563 return finish_type_requirement (type);
25566 /* Parse a compound requirement
25568 compound-requirement:
25569 '{' expression '}' 'noexcept' [opt] trailing-return-type [opt] ';' */
25570 static tree
25571 cp_parser_compound_requirement (cp_parser *parser)
25573 /* Parse an expression enclosed in '{ }'s. */
25574 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25575 return error_mark_node;
25577 tree expr = cp_parser_expression (parser, NULL, false, false);
25578 if (!expr || expr == error_mark_node)
25579 return error_mark_node;
25581 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
25582 return error_mark_node;
25584 /* Parse the optional noexcept. */
25585 bool noexcept_p = false;
25586 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_NOEXCEPT))
25588 cp_lexer_consume_token (parser->lexer);
25589 noexcept_p = true;
25592 /* Parse the optional trailing return type. */
25593 tree type = NULL_TREE;
25594 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
25596 cp_lexer_consume_token (parser->lexer);
25597 bool saved_result_type_constraint_p = parser->in_result_type_constraint_p;
25598 parser->in_result_type_constraint_p = true;
25599 type = cp_parser_trailing_type_id (parser);
25600 parser->in_result_type_constraint_p = saved_result_type_constraint_p;
25601 if (type == error_mark_node)
25602 return error_mark_node;
25605 return finish_compound_requirement (expr, type, noexcept_p);
25608 /* Parse a nested requirement. This is the same as a requires clause.
25610 nested-requirement:
25611 requires-clause */
25612 static tree
25613 cp_parser_nested_requirement (cp_parser *parser)
25615 cp_lexer_consume_token (parser->lexer);
25616 tree req = cp_parser_requires_clause (parser);
25617 if (req == error_mark_node)
25618 return error_mark_node;
25619 return finish_nested_requirement (req);
25622 /* Support Functions */
25624 /* Return the appropriate prefer_type argument for lookup_name_real based on
25625 tag_type and template_mem_access. */
25627 static inline int
25628 prefer_type_arg (tag_types tag_type, bool template_mem_access = false)
25630 /* DR 141: When looking in the current enclosing context for a template-name
25631 after -> or ., only consider class templates. */
25632 if (template_mem_access)
25633 return 2;
25634 switch (tag_type)
25636 case none_type: return 0; // No preference.
25637 case scope_type: return 1; // Type or namespace.
25638 default: return 2; // Type only.
25642 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
25643 NAME should have one of the representations used for an
25644 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
25645 is returned. If PARSER->SCOPE is a dependent type, then a
25646 SCOPE_REF is returned.
25648 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
25649 returned; the name was already resolved when the TEMPLATE_ID_EXPR
25650 was formed. Abstractly, such entities should not be passed to this
25651 function, because they do not need to be looked up, but it is
25652 simpler to check for this special case here, rather than at the
25653 call-sites.
25655 In cases not explicitly covered above, this function returns a
25656 DECL, OVERLOAD, or baselink representing the result of the lookup.
25657 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
25658 is returned.
25660 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
25661 (e.g., "struct") that was used. In that case bindings that do not
25662 refer to types are ignored.
25664 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
25665 ignored.
25667 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
25668 are ignored.
25670 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
25671 types.
25673 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
25674 TREE_LIST of candidates if name-lookup results in an ambiguity, and
25675 NULL_TREE otherwise. */
25677 static cp_expr
25678 cp_parser_lookup_name (cp_parser *parser, tree name,
25679 enum tag_types tag_type,
25680 bool is_template,
25681 bool is_namespace,
25682 bool check_dependency,
25683 tree *ambiguous_decls,
25684 location_t name_location)
25686 tree decl;
25687 tree object_type = parser->context->object_type;
25689 /* Assume that the lookup will be unambiguous. */
25690 if (ambiguous_decls)
25691 *ambiguous_decls = NULL_TREE;
25693 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
25694 no longer valid. Note that if we are parsing tentatively, and
25695 the parse fails, OBJECT_TYPE will be automatically restored. */
25696 parser->context->object_type = NULL_TREE;
25698 if (name == error_mark_node)
25699 return error_mark_node;
25701 /* A template-id has already been resolved; there is no lookup to
25702 do. */
25703 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
25704 return name;
25705 if (BASELINK_P (name))
25707 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
25708 == TEMPLATE_ID_EXPR);
25709 return name;
25712 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
25713 it should already have been checked to make sure that the name
25714 used matches the type being destroyed. */
25715 if (TREE_CODE (name) == BIT_NOT_EXPR)
25717 tree type;
25719 /* Figure out to which type this destructor applies. */
25720 if (parser->scope)
25721 type = parser->scope;
25722 else if (object_type)
25723 type = object_type;
25724 else
25725 type = current_class_type;
25726 /* If that's not a class type, there is no destructor. */
25727 if (!type || !CLASS_TYPE_P (type))
25728 return error_mark_node;
25730 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
25731 lazily_declare_fn (sfk_destructor, type);
25733 if (tree dtor = CLASSTYPE_DESTRUCTOR (type))
25734 return dtor;
25736 return error_mark_node;
25739 /* By this point, the NAME should be an ordinary identifier. If
25740 the id-expression was a qualified name, the qualifying scope is
25741 stored in PARSER->SCOPE at this point. */
25742 gcc_assert (identifier_p (name));
25744 /* Perform the lookup. */
25745 if (parser->scope)
25747 bool dependent_p;
25749 if (parser->scope == error_mark_node)
25750 return error_mark_node;
25752 /* If the SCOPE is dependent, the lookup must be deferred until
25753 the template is instantiated -- unless we are explicitly
25754 looking up names in uninstantiated templates. Even then, we
25755 cannot look up the name if the scope is not a class type; it
25756 might, for example, be a template type parameter. */
25757 dependent_p = (TYPE_P (parser->scope)
25758 && dependent_scope_p (parser->scope));
25759 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
25760 && dependent_p)
25761 /* Defer lookup. */
25762 decl = error_mark_node;
25763 else
25765 tree pushed_scope = NULL_TREE;
25767 /* If PARSER->SCOPE is a dependent type, then it must be a
25768 class type, and we must not be checking dependencies;
25769 otherwise, we would have processed this lookup above. So
25770 that PARSER->SCOPE is not considered a dependent base by
25771 lookup_member, we must enter the scope here. */
25772 if (dependent_p)
25773 pushed_scope = push_scope (parser->scope);
25775 /* If the PARSER->SCOPE is a template specialization, it
25776 may be instantiated during name lookup. In that case,
25777 errors may be issued. Even if we rollback the current
25778 tentative parse, those errors are valid. */
25779 decl = lookup_qualified_name (parser->scope, name,
25780 prefer_type_arg (tag_type),
25781 /*complain=*/true);
25783 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
25784 lookup result and the nested-name-specifier nominates a class C:
25785 * if the name specified after the nested-name-specifier, when
25786 looked up in C, is the injected-class-name of C (Clause 9), or
25787 * if the name specified after the nested-name-specifier is the
25788 same as the identifier or the simple-template-id's template-
25789 name in the last component of the nested-name-specifier,
25790 the name is instead considered to name the constructor of
25791 class C. [ Note: for example, the constructor is not an
25792 acceptable lookup result in an elaborated-type-specifier so
25793 the constructor would not be used in place of the
25794 injected-class-name. --end note ] Such a constructor name
25795 shall be used only in the declarator-id of a declaration that
25796 names a constructor or in a using-declaration. */
25797 if (tag_type == none_type
25798 && DECL_SELF_REFERENCE_P (decl)
25799 && same_type_p (DECL_CONTEXT (decl), parser->scope))
25800 decl = lookup_qualified_name (parser->scope, ctor_identifier,
25801 prefer_type_arg (tag_type),
25802 /*complain=*/true);
25804 /* If we have a single function from a using decl, pull it out. */
25805 if (TREE_CODE (decl) == OVERLOAD
25806 && !really_overloaded_fn (decl))
25807 decl = OVL_FUNCTION (decl);
25809 if (pushed_scope)
25810 pop_scope (pushed_scope);
25813 /* If the scope is a dependent type and either we deferred lookup or
25814 we did lookup but didn't find the name, rememeber the name. */
25815 if (decl == error_mark_node && TYPE_P (parser->scope)
25816 && dependent_type_p (parser->scope))
25818 if (tag_type)
25820 tree type;
25822 /* The resolution to Core Issue 180 says that `struct
25823 A::B' should be considered a type-name, even if `A'
25824 is dependent. */
25825 type = make_typename_type (parser->scope, name, tag_type,
25826 /*complain=*/tf_error);
25827 if (type != error_mark_node)
25828 decl = TYPE_NAME (type);
25830 else if (is_template
25831 && (cp_parser_next_token_ends_template_argument_p (parser)
25832 || cp_lexer_next_token_is (parser->lexer,
25833 CPP_CLOSE_PAREN)))
25834 decl = make_unbound_class_template (parser->scope,
25835 name, NULL_TREE,
25836 /*complain=*/tf_error);
25837 else
25838 decl = build_qualified_name (/*type=*/NULL_TREE,
25839 parser->scope, name,
25840 is_template);
25842 parser->qualifying_scope = parser->scope;
25843 parser->object_scope = NULL_TREE;
25845 else if (object_type)
25847 /* Look up the name in the scope of the OBJECT_TYPE, unless the
25848 OBJECT_TYPE is not a class. */
25849 if (CLASS_TYPE_P (object_type))
25850 /* If the OBJECT_TYPE is a template specialization, it may
25851 be instantiated during name lookup. In that case, errors
25852 may be issued. Even if we rollback the current tentative
25853 parse, those errors are valid. */
25854 decl = lookup_member (object_type,
25855 name,
25856 /*protect=*/0,
25857 prefer_type_arg (tag_type),
25858 tf_warning_or_error);
25859 else
25860 decl = NULL_TREE;
25862 if (!decl)
25863 /* Look it up in the enclosing context. DR 141: When looking for a
25864 template-name after -> or ., only consider class templates. */
25865 decl = lookup_name_real (name, prefer_type_arg (tag_type, is_template),
25866 /*nonclass=*/0,
25867 /*block_p=*/true, is_namespace, 0);
25868 if (object_type == unknown_type_node)
25869 /* The object is type-dependent, so we can't look anything up; we used
25870 this to get the DR 141 behavior. */
25871 object_type = NULL_TREE;
25872 parser->object_scope = object_type;
25873 parser->qualifying_scope = NULL_TREE;
25875 else
25877 decl = lookup_name_real (name, prefer_type_arg (tag_type),
25878 /*nonclass=*/0,
25879 /*block_p=*/true, is_namespace, 0);
25880 parser->qualifying_scope = NULL_TREE;
25881 parser->object_scope = NULL_TREE;
25884 /* If the lookup failed, let our caller know. */
25885 if (!decl || decl == error_mark_node)
25886 return error_mark_node;
25888 /* Pull out the template from an injected-class-name (or multiple). */
25889 if (is_template)
25890 decl = maybe_get_template_decl_from_type_decl (decl);
25892 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
25893 if (TREE_CODE (decl) == TREE_LIST)
25895 if (ambiguous_decls)
25896 *ambiguous_decls = decl;
25897 /* The error message we have to print is too complicated for
25898 cp_parser_error, so we incorporate its actions directly. */
25899 if (!cp_parser_simulate_error (parser))
25901 error_at (name_location, "reference to %qD is ambiguous",
25902 name);
25903 print_candidates (decl);
25905 return error_mark_node;
25908 gcc_assert (DECL_P (decl)
25909 || TREE_CODE (decl) == OVERLOAD
25910 || TREE_CODE (decl) == SCOPE_REF
25911 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
25912 || BASELINK_P (decl));
25914 /* If we have resolved the name of a member declaration, check to
25915 see if the declaration is accessible. When the name resolves to
25916 set of overloaded functions, accessibility is checked when
25917 overload resolution is done.
25919 During an explicit instantiation, access is not checked at all,
25920 as per [temp.explicit]. */
25921 if (DECL_P (decl))
25922 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
25924 maybe_record_typedef_use (decl);
25926 return cp_expr (decl, name_location);
25929 /* Like cp_parser_lookup_name, but for use in the typical case where
25930 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
25931 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
25933 static tree
25934 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
25936 return cp_parser_lookup_name (parser, name,
25937 none_type,
25938 /*is_template=*/false,
25939 /*is_namespace=*/false,
25940 /*check_dependency=*/true,
25941 /*ambiguous_decls=*/NULL,
25942 location);
25945 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
25946 the current context, return the TYPE_DECL. If TAG_NAME_P is
25947 true, the DECL indicates the class being defined in a class-head,
25948 or declared in an elaborated-type-specifier.
25950 Otherwise, return DECL. */
25952 static tree
25953 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
25955 /* If the TEMPLATE_DECL is being declared as part of a class-head,
25956 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
25958 struct A {
25959 template <typename T> struct B;
25962 template <typename T> struct A::B {};
25964 Similarly, in an elaborated-type-specifier:
25966 namespace N { struct X{}; }
25968 struct A {
25969 template <typename T> friend struct N::X;
25972 However, if the DECL refers to a class type, and we are in
25973 the scope of the class, then the name lookup automatically
25974 finds the TYPE_DECL created by build_self_reference rather
25975 than a TEMPLATE_DECL. For example, in:
25977 template <class T> struct S {
25978 S s;
25981 there is no need to handle such case. */
25983 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
25984 return DECL_TEMPLATE_RESULT (decl);
25986 return decl;
25989 /* If too many, or too few, template-parameter lists apply to the
25990 declarator, issue an error message. Returns TRUE if all went well,
25991 and FALSE otherwise. */
25993 static bool
25994 cp_parser_check_declarator_template_parameters (cp_parser* parser,
25995 cp_declarator *declarator,
25996 location_t declarator_location)
25998 switch (declarator->kind)
26000 case cdk_id:
26002 unsigned num_templates = 0;
26003 tree scope = declarator->u.id.qualifying_scope;
26005 if (scope)
26006 num_templates = num_template_headers_for_class (scope);
26007 else if (TREE_CODE (declarator->u.id.unqualified_name)
26008 == TEMPLATE_ID_EXPR)
26009 /* If the DECLARATOR has the form `X<y>' then it uses one
26010 additional level of template parameters. */
26011 ++num_templates;
26013 return cp_parser_check_template_parameters
26014 (parser, num_templates, declarator_location, declarator);
26017 case cdk_function:
26018 case cdk_array:
26019 case cdk_pointer:
26020 case cdk_reference:
26021 case cdk_ptrmem:
26022 return (cp_parser_check_declarator_template_parameters
26023 (parser, declarator->declarator, declarator_location));
26025 case cdk_decomp:
26026 case cdk_error:
26027 return true;
26029 default:
26030 gcc_unreachable ();
26032 return false;
26035 /* NUM_TEMPLATES were used in the current declaration. If that is
26036 invalid, return FALSE and issue an error messages. Otherwise,
26037 return TRUE. If DECLARATOR is non-NULL, then we are checking a
26038 declarator and we can print more accurate diagnostics. */
26040 static bool
26041 cp_parser_check_template_parameters (cp_parser* parser,
26042 unsigned num_templates,
26043 location_t location,
26044 cp_declarator *declarator)
26046 /* If there are the same number of template classes and parameter
26047 lists, that's OK. */
26048 if (parser->num_template_parameter_lists == num_templates)
26049 return true;
26050 /* If there are more, but only one more, then we are referring to a
26051 member template. That's OK too. */
26052 if (parser->num_template_parameter_lists == num_templates + 1)
26053 return true;
26054 /* If there are more template classes than parameter lists, we have
26055 something like:
26057 template <class T> void S<T>::R<T>::f (); */
26058 if (parser->num_template_parameter_lists < num_templates)
26060 if (declarator && !current_function_decl)
26061 error_at (location, "specializing member %<%T::%E%> "
26062 "requires %<template<>%> syntax",
26063 declarator->u.id.qualifying_scope,
26064 declarator->u.id.unqualified_name);
26065 else if (declarator)
26066 error_at (location, "invalid declaration of %<%T::%E%>",
26067 declarator->u.id.qualifying_scope,
26068 declarator->u.id.unqualified_name);
26069 else
26070 error_at (location, "too few template-parameter-lists");
26071 return false;
26073 /* Otherwise, there are too many template parameter lists. We have
26074 something like:
26076 template <class T> template <class U> void S::f(); */
26077 error_at (location, "too many template-parameter-lists");
26078 return false;
26081 /* Parse an optional `::' token indicating that the following name is
26082 from the global namespace. If so, PARSER->SCOPE is set to the
26083 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
26084 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
26085 Returns the new value of PARSER->SCOPE, if the `::' token is
26086 present, and NULL_TREE otherwise. */
26088 static tree
26089 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
26091 cp_token *token;
26093 /* Peek at the next token. */
26094 token = cp_lexer_peek_token (parser->lexer);
26095 /* If we're looking at a `::' token then we're starting from the
26096 global namespace, not our current location. */
26097 if (token->type == CPP_SCOPE)
26099 /* Consume the `::' token. */
26100 cp_lexer_consume_token (parser->lexer);
26101 /* Set the SCOPE so that we know where to start the lookup. */
26102 parser->scope = global_namespace;
26103 parser->qualifying_scope = global_namespace;
26104 parser->object_scope = NULL_TREE;
26106 return parser->scope;
26108 else if (!current_scope_valid_p)
26110 parser->scope = NULL_TREE;
26111 parser->qualifying_scope = NULL_TREE;
26112 parser->object_scope = NULL_TREE;
26115 return NULL_TREE;
26118 /* Returns TRUE if the upcoming token sequence is the start of a
26119 constructor declarator or C++17 deduction guide. If FRIEND_P is true, the
26120 declarator is preceded by the `friend' specifier. */
26122 static bool
26123 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
26125 bool constructor_p;
26126 bool outside_class_specifier_p;
26127 tree nested_name_specifier;
26128 cp_token *next_token;
26130 /* The common case is that this is not a constructor declarator, so
26131 try to avoid doing lots of work if at all possible. It's not
26132 valid declare a constructor at function scope. */
26133 if (parser->in_function_body)
26134 return false;
26135 /* And only certain tokens can begin a constructor declarator. */
26136 next_token = cp_lexer_peek_token (parser->lexer);
26137 if (next_token->type != CPP_NAME
26138 && next_token->type != CPP_SCOPE
26139 && next_token->type != CPP_NESTED_NAME_SPECIFIER
26140 && next_token->type != CPP_TEMPLATE_ID)
26141 return false;
26143 /* Parse tentatively; we are going to roll back all of the tokens
26144 consumed here. */
26145 cp_parser_parse_tentatively (parser);
26146 /* Assume that we are looking at a constructor declarator. */
26147 constructor_p = true;
26149 /* Look for the optional `::' operator. */
26150 cp_parser_global_scope_opt (parser,
26151 /*current_scope_valid_p=*/false);
26152 /* Look for the nested-name-specifier. */
26153 nested_name_specifier
26154 = (cp_parser_nested_name_specifier_opt (parser,
26155 /*typename_keyword_p=*/false,
26156 /*check_dependency_p=*/false,
26157 /*type_p=*/false,
26158 /*is_declaration=*/false));
26160 outside_class_specifier_p = (!at_class_scope_p ()
26161 || !TYPE_BEING_DEFINED (current_class_type)
26162 || friend_p);
26164 /* Outside of a class-specifier, there must be a
26165 nested-name-specifier. Except in C++17 mode, where we
26166 might be declaring a guiding declaration. */
26167 if (!nested_name_specifier && outside_class_specifier_p
26168 && cxx_dialect < cxx1z)
26169 constructor_p = false;
26170 else if (nested_name_specifier == error_mark_node)
26171 constructor_p = false;
26173 /* If we have a class scope, this is easy; DR 147 says that S::S always
26174 names the constructor, and no other qualified name could. */
26175 if (constructor_p && nested_name_specifier
26176 && CLASS_TYPE_P (nested_name_specifier))
26178 tree id = cp_parser_unqualified_id (parser,
26179 /*template_keyword_p=*/false,
26180 /*check_dependency_p=*/false,
26181 /*declarator_p=*/true,
26182 /*optional_p=*/false);
26183 if (is_overloaded_fn (id))
26184 id = DECL_NAME (get_first_fn (id));
26185 if (!constructor_name_p (id, nested_name_specifier))
26186 constructor_p = false;
26188 /* If we still think that this might be a constructor-declarator,
26189 look for a class-name. */
26190 else if (constructor_p)
26192 /* If we have:
26194 template <typename T> struct S {
26195 S();
26198 we must recognize that the nested `S' names a class. */
26199 if (cxx_dialect >= cxx1z)
26200 cp_parser_parse_tentatively (parser);
26202 tree type_decl;
26203 type_decl = cp_parser_class_name (parser,
26204 /*typename_keyword_p=*/false,
26205 /*template_keyword_p=*/false,
26206 none_type,
26207 /*check_dependency_p=*/false,
26208 /*class_head_p=*/false,
26209 /*is_declaration=*/false);
26211 if (cxx_dialect >= cxx1z
26212 && !cp_parser_parse_definitely (parser))
26214 type_decl = NULL_TREE;
26215 tree tmpl = cp_parser_template_name (parser,
26216 /*template_keyword*/false,
26217 /*check_dependency_p*/false,
26218 /*is_declaration*/false,
26219 none_type,
26220 /*is_identifier*/NULL);
26221 if (DECL_CLASS_TEMPLATE_P (tmpl)
26222 || DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl))
26223 /* It's a deduction guide, return true. */;
26224 else
26225 cp_parser_simulate_error (parser);
26228 /* If there was no class-name, then this is not a constructor.
26229 Otherwise, if we are in a class-specifier and we aren't
26230 handling a friend declaration, check that its type matches
26231 current_class_type (c++/38313). Note: error_mark_node
26232 is left alone for error recovery purposes. */
26233 constructor_p = (!cp_parser_error_occurred (parser)
26234 && (outside_class_specifier_p
26235 || type_decl == NULL_TREE
26236 || type_decl == error_mark_node
26237 || same_type_p (current_class_type,
26238 TREE_TYPE (type_decl))));
26240 /* If we're still considering a constructor, we have to see a `(',
26241 to begin the parameter-declaration-clause, followed by either a
26242 `)', an `...', or a decl-specifier. We need to check for a
26243 type-specifier to avoid being fooled into thinking that:
26245 S (f) (int);
26247 is a constructor. (It is actually a function named `f' that
26248 takes one parameter (of type `int') and returns a value of type
26249 `S'. */
26250 if (constructor_p
26251 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26252 constructor_p = false;
26254 if (constructor_p
26255 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
26256 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
26257 /* A parameter declaration begins with a decl-specifier,
26258 which is either the "attribute" keyword, a storage class
26259 specifier, or (usually) a type-specifier. */
26260 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
26262 tree type;
26263 tree pushed_scope = NULL_TREE;
26264 unsigned saved_num_template_parameter_lists;
26266 /* Names appearing in the type-specifier should be looked up
26267 in the scope of the class. */
26268 if (current_class_type)
26269 type = NULL_TREE;
26270 else if (type_decl)
26272 type = TREE_TYPE (type_decl);
26273 if (TREE_CODE (type) == TYPENAME_TYPE)
26275 type = resolve_typename_type (type,
26276 /*only_current_p=*/false);
26277 if (TREE_CODE (type) == TYPENAME_TYPE)
26279 cp_parser_abort_tentative_parse (parser);
26280 return false;
26283 pushed_scope = push_scope (type);
26286 /* Inside the constructor parameter list, surrounding
26287 template-parameter-lists do not apply. */
26288 saved_num_template_parameter_lists
26289 = parser->num_template_parameter_lists;
26290 parser->num_template_parameter_lists = 0;
26292 /* Look for the type-specifier. */
26293 cp_parser_type_specifier (parser,
26294 CP_PARSER_FLAGS_NONE,
26295 /*decl_specs=*/NULL,
26296 /*is_declarator=*/true,
26297 /*declares_class_or_enum=*/NULL,
26298 /*is_cv_qualifier=*/NULL);
26300 parser->num_template_parameter_lists
26301 = saved_num_template_parameter_lists;
26303 /* Leave the scope of the class. */
26304 if (pushed_scope)
26305 pop_scope (pushed_scope);
26307 constructor_p = !cp_parser_error_occurred (parser);
26311 /* We did not really want to consume any tokens. */
26312 cp_parser_abort_tentative_parse (parser);
26314 return constructor_p;
26317 /* Parse the definition of the function given by the DECL_SPECIFIERS,
26318 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
26319 they must be performed once we are in the scope of the function.
26321 Returns the function defined. */
26323 static tree
26324 cp_parser_function_definition_from_specifiers_and_declarator
26325 (cp_parser* parser,
26326 cp_decl_specifier_seq *decl_specifiers,
26327 tree attributes,
26328 const cp_declarator *declarator)
26330 tree fn;
26331 bool success_p;
26333 /* Begin the function-definition. */
26334 success_p = start_function (decl_specifiers, declarator, attributes);
26336 /* The things we're about to see are not directly qualified by any
26337 template headers we've seen thus far. */
26338 reset_specialization ();
26340 /* If there were names looked up in the decl-specifier-seq that we
26341 did not check, check them now. We must wait until we are in the
26342 scope of the function to perform the checks, since the function
26343 might be a friend. */
26344 perform_deferred_access_checks (tf_warning_or_error);
26346 if (success_p)
26348 cp_finalize_omp_declare_simd (parser, current_function_decl);
26349 parser->omp_declare_simd = NULL;
26350 cp_finalize_oacc_routine (parser, current_function_decl, true);
26351 parser->oacc_routine = NULL;
26354 if (!success_p)
26356 /* Skip the entire function. */
26357 cp_parser_skip_to_end_of_block_or_statement (parser);
26358 fn = error_mark_node;
26360 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
26362 /* Seen already, skip it. An error message has already been output. */
26363 cp_parser_skip_to_end_of_block_or_statement (parser);
26364 fn = current_function_decl;
26365 current_function_decl = NULL_TREE;
26366 /* If this is a function from a class, pop the nested class. */
26367 if (current_class_name)
26368 pop_nested_class ();
26370 else
26372 timevar_id_t tv;
26373 if (DECL_DECLARED_INLINE_P (current_function_decl))
26374 tv = TV_PARSE_INLINE;
26375 else
26376 tv = TV_PARSE_FUNC;
26377 timevar_push (tv);
26378 fn = cp_parser_function_definition_after_declarator (parser,
26379 /*inline_p=*/false);
26380 timevar_pop (tv);
26383 return fn;
26386 /* Parse the part of a function-definition that follows the
26387 declarator. INLINE_P is TRUE iff this function is an inline
26388 function defined within a class-specifier.
26390 Returns the function defined. */
26392 static tree
26393 cp_parser_function_definition_after_declarator (cp_parser* parser,
26394 bool inline_p)
26396 tree fn;
26397 bool ctor_initializer_p = false;
26398 bool saved_in_unbraced_linkage_specification_p;
26399 bool saved_in_function_body;
26400 unsigned saved_num_template_parameter_lists;
26401 cp_token *token;
26402 bool fully_implicit_function_template_p
26403 = parser->fully_implicit_function_template_p;
26404 parser->fully_implicit_function_template_p = false;
26405 tree implicit_template_parms
26406 = parser->implicit_template_parms;
26407 parser->implicit_template_parms = 0;
26408 cp_binding_level* implicit_template_scope
26409 = parser->implicit_template_scope;
26410 parser->implicit_template_scope = 0;
26412 saved_in_function_body = parser->in_function_body;
26413 parser->in_function_body = true;
26414 /* If the next token is `return', then the code may be trying to
26415 make use of the "named return value" extension that G++ used to
26416 support. */
26417 token = cp_lexer_peek_token (parser->lexer);
26418 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
26420 /* Consume the `return' keyword. */
26421 cp_lexer_consume_token (parser->lexer);
26422 /* Look for the identifier that indicates what value is to be
26423 returned. */
26424 cp_parser_identifier (parser);
26425 /* Issue an error message. */
26426 error_at (token->location,
26427 "named return values are no longer supported");
26428 /* Skip tokens until we reach the start of the function body. */
26429 while (true)
26431 cp_token *token = cp_lexer_peek_token (parser->lexer);
26432 if (token->type == CPP_OPEN_BRACE
26433 || token->type == CPP_EOF
26434 || token->type == CPP_PRAGMA_EOL)
26435 break;
26436 cp_lexer_consume_token (parser->lexer);
26439 /* The `extern' in `extern "C" void f () { ... }' does not apply to
26440 anything declared inside `f'. */
26441 saved_in_unbraced_linkage_specification_p
26442 = parser->in_unbraced_linkage_specification_p;
26443 parser->in_unbraced_linkage_specification_p = false;
26444 /* Inside the function, surrounding template-parameter-lists do not
26445 apply. */
26446 saved_num_template_parameter_lists
26447 = parser->num_template_parameter_lists;
26448 parser->num_template_parameter_lists = 0;
26450 start_lambda_scope (current_function_decl);
26452 /* If the next token is `try', `__transaction_atomic', or
26453 `__transaction_relaxed`, then we are looking at either function-try-block
26454 or function-transaction-block. Note that all of these include the
26455 function-body. */
26456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
26457 ctor_initializer_p = cp_parser_function_transaction (parser,
26458 RID_TRANSACTION_ATOMIC);
26459 else if (cp_lexer_next_token_is_keyword (parser->lexer,
26460 RID_TRANSACTION_RELAXED))
26461 ctor_initializer_p = cp_parser_function_transaction (parser,
26462 RID_TRANSACTION_RELAXED);
26463 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26464 ctor_initializer_p = cp_parser_function_try_block (parser);
26465 else
26466 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
26467 (parser, /*in_function_try_block=*/false);
26469 finish_lambda_scope ();
26471 /* Finish the function. */
26472 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
26473 (inline_p ? 2 : 0));
26474 /* Generate code for it, if necessary. */
26475 expand_or_defer_fn (fn);
26476 /* Restore the saved values. */
26477 parser->in_unbraced_linkage_specification_p
26478 = saved_in_unbraced_linkage_specification_p;
26479 parser->num_template_parameter_lists
26480 = saved_num_template_parameter_lists;
26481 parser->in_function_body = saved_in_function_body;
26483 parser->fully_implicit_function_template_p
26484 = fully_implicit_function_template_p;
26485 parser->implicit_template_parms
26486 = implicit_template_parms;
26487 parser->implicit_template_scope
26488 = implicit_template_scope;
26490 if (parser->fully_implicit_function_template_p)
26491 finish_fully_implicit_template (parser, /*member_decl_opt=*/0);
26493 return fn;
26496 /* Parse a template-declaration body (following argument list). */
26498 static void
26499 cp_parser_template_declaration_after_parameters (cp_parser* parser,
26500 tree parameter_list,
26501 bool member_p)
26503 tree decl = NULL_TREE;
26504 bool friend_p = false;
26506 /* We just processed one more parameter list. */
26507 ++parser->num_template_parameter_lists;
26509 /* Get the deferred access checks from the parameter list. These
26510 will be checked once we know what is being declared, as for a
26511 member template the checks must be performed in the scope of the
26512 class containing the member. */
26513 vec<deferred_access_check, va_gc> *checks = get_deferred_access_checks ();
26515 /* Tentatively parse for a new template parameter list, which can either be
26516 the template keyword or a template introduction. */
26517 if (cp_parser_template_declaration_after_export (parser, member_p))
26518 /* OK */;
26519 else if (cxx_dialect >= cxx11
26520 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
26521 decl = cp_parser_alias_declaration (parser);
26522 else
26524 /* There are no access checks when parsing a template, as we do not
26525 know if a specialization will be a friend. */
26526 push_deferring_access_checks (dk_no_check);
26527 cp_token *token = cp_lexer_peek_token (parser->lexer);
26528 decl = cp_parser_single_declaration (parser,
26529 checks,
26530 member_p,
26531 /*explicit_specialization_p=*/false,
26532 &friend_p);
26533 pop_deferring_access_checks ();
26535 /* If this is a member template declaration, let the front
26536 end know. */
26537 if (member_p && !friend_p && decl)
26539 if (TREE_CODE (decl) == TYPE_DECL)
26540 cp_parser_check_access_in_redeclaration (decl, token->location);
26542 decl = finish_member_template_decl (decl);
26544 else if (friend_p && decl
26545 && DECL_DECLARES_TYPE_P (decl))
26546 make_friend_class (current_class_type, TREE_TYPE (decl),
26547 /*complain=*/true);
26549 /* We are done with the current parameter list. */
26550 --parser->num_template_parameter_lists;
26552 pop_deferring_access_checks ();
26554 /* Finish up. */
26555 finish_template_decl (parameter_list);
26557 /* Check the template arguments for a literal operator template. */
26558 if (decl
26559 && DECL_DECLARES_FUNCTION_P (decl)
26560 && UDLIT_OPER_P (DECL_NAME (decl)))
26562 bool ok = true;
26563 if (parameter_list == NULL_TREE)
26564 ok = false;
26565 else
26567 int num_parms = TREE_VEC_LENGTH (parameter_list);
26568 if (num_parms == 1)
26570 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
26571 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26572 if (TREE_TYPE (parm) != char_type_node
26573 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26574 ok = false;
26576 else if (num_parms == 2 && cxx_dialect >= cxx14)
26578 tree parm_type = TREE_VEC_ELT (parameter_list, 0);
26579 tree type = INNERMOST_TEMPLATE_PARMS (parm_type);
26580 tree parm_list = TREE_VEC_ELT (parameter_list, 1);
26581 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
26582 if (parm == error_mark_node
26583 || TREE_TYPE (parm) != TREE_TYPE (type)
26584 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
26585 ok = false;
26587 else
26588 ok = false;
26590 if (!ok)
26592 if (cxx_dialect >= cxx14)
26593 error ("literal operator template %qD has invalid parameter list."
26594 " Expected non-type template argument pack <char...>"
26595 " or <typename CharT, CharT...>",
26596 decl);
26597 else
26598 error ("literal operator template %qD has invalid parameter list."
26599 " Expected non-type template argument pack <char...>",
26600 decl);
26604 /* Register member declarations. */
26605 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
26606 finish_member_declaration (decl);
26607 /* If DECL is a function template, we must return to parse it later.
26608 (Even though there is no definition, there might be default
26609 arguments that need handling.) */
26610 if (member_p && decl
26611 && DECL_DECLARES_FUNCTION_P (decl))
26612 vec_safe_push (unparsed_funs_with_definitions, decl);
26615 /* Parse a template introduction header for a template-declaration. Returns
26616 false if tentative parse fails. */
26618 static bool
26619 cp_parser_template_introduction (cp_parser* parser, bool member_p)
26621 cp_parser_parse_tentatively (parser);
26623 tree saved_scope = parser->scope;
26624 tree saved_object_scope = parser->object_scope;
26625 tree saved_qualifying_scope = parser->qualifying_scope;
26627 /* Look for the optional `::' operator. */
26628 cp_parser_global_scope_opt (parser,
26629 /*current_scope_valid_p=*/false);
26630 /* Look for the nested-name-specifier. */
26631 cp_parser_nested_name_specifier_opt (parser,
26632 /*typename_keyword_p=*/false,
26633 /*check_dependency_p=*/true,
26634 /*type_p=*/false,
26635 /*is_declaration=*/false);
26637 cp_token *token = cp_lexer_peek_token (parser->lexer);
26638 tree concept_name = cp_parser_identifier (parser);
26640 /* Look up the concept for which we will be matching
26641 template parameters. */
26642 tree tmpl_decl = cp_parser_lookup_name_simple (parser, concept_name,
26643 token->location);
26644 parser->scope = saved_scope;
26645 parser->object_scope = saved_object_scope;
26646 parser->qualifying_scope = saved_qualifying_scope;
26648 if (concept_name == error_mark_node)
26649 cp_parser_simulate_error (parser);
26651 /* Look for opening brace for introduction. */
26652 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
26654 if (!cp_parser_parse_definitely (parser))
26655 return false;
26657 push_deferring_access_checks (dk_deferred);
26659 /* Build vector of placeholder parameters and grab
26660 matching identifiers. */
26661 tree introduction_list = cp_parser_introduction_list (parser);
26663 /* The introduction-list shall not be empty. */
26664 int nargs = TREE_VEC_LENGTH (introduction_list);
26665 if (nargs == 0)
26667 error ("empty introduction-list");
26668 return true;
26671 /* Look for closing brace for introduction. */
26672 if (!cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE))
26673 return true;
26675 if (tmpl_decl == error_mark_node)
26677 cp_parser_name_lookup_error (parser, concept_name, tmpl_decl, NLE_NULL,
26678 token->location);
26679 return true;
26682 /* Build and associate the constraint. */
26683 tree parms = finish_template_introduction (tmpl_decl, introduction_list);
26684 if (parms && parms != error_mark_node)
26686 cp_parser_template_declaration_after_parameters (parser, parms,
26687 member_p);
26688 return true;
26691 error_at (token->location, "no matching concept for template-introduction");
26692 return true;
26695 /* Parse a normal template-declaration following the template keyword. */
26697 static void
26698 cp_parser_explicit_template_declaration (cp_parser* parser, bool member_p)
26700 tree parameter_list;
26701 bool need_lang_pop;
26702 location_t location = input_location;
26704 /* Look for the `<' token. */
26705 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
26706 return;
26707 if (at_class_scope_p () && current_function_decl)
26709 /* 14.5.2.2 [temp.mem]
26711 A local class shall not have member templates. */
26712 error_at (location,
26713 "invalid declaration of member template in local class");
26714 cp_parser_skip_to_end_of_block_or_statement (parser);
26715 return;
26717 /* [temp]
26719 A template ... shall not have C linkage. */
26720 if (current_lang_name == lang_name_c)
26722 error_at (location, "template with C linkage");
26723 /* Give it C++ linkage to avoid confusing other parts of the
26724 front end. */
26725 push_lang_context (lang_name_cplusplus);
26726 need_lang_pop = true;
26728 else
26729 need_lang_pop = false;
26731 /* We cannot perform access checks on the template parameter
26732 declarations until we know what is being declared, just as we
26733 cannot check the decl-specifier list. */
26734 push_deferring_access_checks (dk_deferred);
26736 /* If the next token is `>', then we have an invalid
26737 specialization. Rather than complain about an invalid template
26738 parameter, issue an error message here. */
26739 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
26741 cp_parser_error (parser, "invalid explicit specialization");
26742 begin_specialization ();
26743 parameter_list = NULL_TREE;
26745 else
26747 /* Parse the template parameters. */
26748 parameter_list = cp_parser_template_parameter_list (parser);
26751 /* Look for the `>'. */
26752 cp_parser_skip_to_end_of_template_parameter_list (parser);
26754 /* Manage template requirements */
26755 if (flag_concepts)
26757 tree reqs = get_shorthand_constraints (current_template_parms);
26758 if (tree r = cp_parser_requires_clause_opt (parser))
26759 reqs = conjoin_constraints (reqs, normalize_expression (r));
26760 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
26763 cp_parser_template_declaration_after_parameters (parser, parameter_list,
26764 member_p);
26766 /* For the erroneous case of a template with C linkage, we pushed an
26767 implicit C++ linkage scope; exit that scope now. */
26768 if (need_lang_pop)
26769 pop_lang_context ();
26772 /* Parse a template-declaration, assuming that the `export' (and
26773 `extern') keywords, if present, has already been scanned. MEMBER_P
26774 is as for cp_parser_template_declaration. */
26776 static bool
26777 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
26779 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
26781 cp_lexer_consume_token (parser->lexer);
26782 cp_parser_explicit_template_declaration (parser, member_p);
26783 return true;
26785 else if (flag_concepts)
26786 return cp_parser_template_introduction (parser, member_p);
26788 return false;
26791 /* Perform the deferred access checks from a template-parameter-list.
26792 CHECKS is a TREE_LIST of access checks, as returned by
26793 get_deferred_access_checks. */
26795 static void
26796 cp_parser_perform_template_parameter_access_checks (vec<deferred_access_check, va_gc> *checks)
26798 ++processing_template_parmlist;
26799 perform_access_checks (checks, tf_warning_or_error);
26800 --processing_template_parmlist;
26803 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
26804 `function-definition' sequence that follows a template header.
26805 If MEMBER_P is true, this declaration appears in a class scope.
26807 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
26808 *FRIEND_P is set to TRUE iff the declaration is a friend. */
26810 static tree
26811 cp_parser_single_declaration (cp_parser* parser,
26812 vec<deferred_access_check, va_gc> *checks,
26813 bool member_p,
26814 bool explicit_specialization_p,
26815 bool* friend_p)
26817 int declares_class_or_enum;
26818 tree decl = NULL_TREE;
26819 cp_decl_specifier_seq decl_specifiers;
26820 bool function_definition_p = false;
26821 cp_token *decl_spec_token_start;
26823 /* This function is only used when processing a template
26824 declaration. */
26825 gcc_assert (innermost_scope_kind () == sk_template_parms
26826 || innermost_scope_kind () == sk_template_spec);
26828 /* Defer access checks until we know what is being declared. */
26829 push_deferring_access_checks (dk_deferred);
26831 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
26832 alternative. */
26833 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
26834 cp_parser_decl_specifier_seq (parser,
26835 CP_PARSER_FLAGS_OPTIONAL,
26836 &decl_specifiers,
26837 &declares_class_or_enum);
26838 if (friend_p)
26839 *friend_p = cp_parser_friend_p (&decl_specifiers);
26841 /* There are no template typedefs. */
26842 if (decl_spec_seq_has_spec_p (&decl_specifiers, ds_typedef))
26844 error_at (decl_spec_token_start->location,
26845 "template declaration of %<typedef%>");
26846 decl = error_mark_node;
26849 /* Gather up the access checks that occurred the
26850 decl-specifier-seq. */
26851 stop_deferring_access_checks ();
26853 /* Check for the declaration of a template class. */
26854 if (declares_class_or_enum)
26856 if (cp_parser_declares_only_class_p (parser)
26857 || (declares_class_or_enum & 2))
26859 // If this is a declaration, but not a definition, associate
26860 // any constraints with the type declaration. Constraints
26861 // are associated with definitions in cp_parser_class_specifier.
26862 if (declares_class_or_enum == 1)
26863 associate_classtype_constraints (decl_specifiers.type);
26865 decl = shadow_tag (&decl_specifiers);
26867 /* In this case:
26869 struct C {
26870 friend template <typename T> struct A<T>::B;
26873 A<T>::B will be represented by a TYPENAME_TYPE, and
26874 therefore not recognized by shadow_tag. */
26875 if (friend_p && *friend_p
26876 && !decl
26877 && decl_specifiers.type
26878 && TYPE_P (decl_specifiers.type))
26879 decl = decl_specifiers.type;
26881 if (decl && decl != error_mark_node)
26882 decl = TYPE_NAME (decl);
26883 else
26884 decl = error_mark_node;
26886 /* Perform access checks for template parameters. */
26887 cp_parser_perform_template_parameter_access_checks (checks);
26889 /* Give a helpful diagnostic for
26890 template <class T> struct A { } a;
26891 if we aren't already recovering from an error. */
26892 if (!cp_parser_declares_only_class_p (parser)
26893 && !seen_error ())
26895 error_at (cp_lexer_peek_token (parser->lexer)->location,
26896 "a class template declaration must not declare "
26897 "anything else");
26898 cp_parser_skip_to_end_of_block_or_statement (parser);
26899 goto out;
26904 /* Complain about missing 'typename' or other invalid type names. */
26905 if (!decl_specifiers.any_type_specifiers_p
26906 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
26908 /* cp_parser_parse_and_diagnose_invalid_type_name calls
26909 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
26910 the rest of this declaration. */
26911 decl = error_mark_node;
26912 goto out;
26915 /* If it's not a template class, try for a template function. If
26916 the next token is a `;', then this declaration does not declare
26917 anything. But, if there were errors in the decl-specifiers, then
26918 the error might well have come from an attempted class-specifier.
26919 In that case, there's no need to warn about a missing declarator. */
26920 if (!decl
26921 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
26922 || decl_specifiers.type != error_mark_node))
26924 decl = cp_parser_init_declarator (parser,
26925 &decl_specifiers,
26926 checks,
26927 /*function_definition_allowed_p=*/true,
26928 member_p,
26929 declares_class_or_enum,
26930 &function_definition_p,
26931 NULL, NULL, NULL);
26933 /* 7.1.1-1 [dcl.stc]
26935 A storage-class-specifier shall not be specified in an explicit
26936 specialization... */
26937 if (decl
26938 && explicit_specialization_p
26939 && decl_specifiers.storage_class != sc_none)
26941 error_at (decl_spec_token_start->location,
26942 "explicit template specialization cannot have a storage class");
26943 decl = error_mark_node;
26946 if (decl && VAR_P (decl))
26947 check_template_variable (decl);
26950 /* Look for a trailing `;' after the declaration. */
26951 if (!function_definition_p
26952 && (decl == error_mark_node
26953 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
26954 cp_parser_skip_to_end_of_block_or_statement (parser);
26956 out:
26957 pop_deferring_access_checks ();
26959 /* Clear any current qualification; whatever comes next is the start
26960 of something new. */
26961 parser->scope = NULL_TREE;
26962 parser->qualifying_scope = NULL_TREE;
26963 parser->object_scope = NULL_TREE;
26965 return decl;
26968 /* Parse a cast-expression that is not the operand of a unary "&". */
26970 static cp_expr
26971 cp_parser_simple_cast_expression (cp_parser *parser)
26973 return cp_parser_cast_expression (parser, /*address_p=*/false,
26974 /*cast_p=*/false, /*decltype*/false, NULL);
26977 /* Parse a functional cast to TYPE. Returns an expression
26978 representing the cast. */
26980 static cp_expr
26981 cp_parser_functional_cast (cp_parser* parser, tree type)
26983 vec<tree, va_gc> *vec;
26984 tree expression_list;
26985 cp_expr cast;
26986 bool nonconst_p;
26988 location_t start_loc = input_location;
26990 if (!type)
26991 type = error_mark_node;
26993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26995 cp_lexer_set_source_position (parser->lexer);
26996 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
26997 expression_list = cp_parser_braced_list (parser, &nonconst_p);
26998 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
26999 if (TREE_CODE (type) == TYPE_DECL)
27000 type = TREE_TYPE (type);
27002 cast = finish_compound_literal (type, expression_list,
27003 tf_warning_or_error, fcl_functional);
27004 /* Create a location of the form:
27005 type_name{i, f}
27006 ^~~~~~~~~~~~~~~
27007 with caret == start at the start of the type name,
27008 finishing at the closing brace. */
27009 location_t finish_loc
27010 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27011 location_t combined_loc = make_location (start_loc, start_loc,
27012 finish_loc);
27013 cast.set_location (combined_loc);
27014 return cast;
27018 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
27019 /*cast_p=*/true,
27020 /*allow_expansion_p=*/true,
27021 /*non_constant_p=*/NULL);
27022 if (vec == NULL)
27023 expression_list = error_mark_node;
27024 else
27026 expression_list = build_tree_list_vec (vec);
27027 release_tree_vector (vec);
27030 cast = build_functional_cast (type, expression_list,
27031 tf_warning_or_error);
27032 /* [expr.const]/1: In an integral constant expression "only type
27033 conversions to integral or enumeration type can be used". */
27034 if (TREE_CODE (type) == TYPE_DECL)
27035 type = TREE_TYPE (type);
27036 if (cast != error_mark_node
27037 && !cast_valid_in_integral_constant_expression_p (type)
27038 && cp_parser_non_integral_constant_expression (parser,
27039 NIC_CONSTRUCTOR))
27040 return error_mark_node;
27042 /* Create a location of the form:
27043 float(i)
27044 ^~~~~~~~
27045 with caret == start at the start of the type name,
27046 finishing at the closing paren. */
27047 location_t finish_loc
27048 = get_finish (cp_lexer_previous_token (parser->lexer)->location);
27049 location_t combined_loc = make_location (start_loc, start_loc, finish_loc);
27050 cast.set_location (combined_loc);
27051 return cast;
27054 /* Save the tokens that make up the body of a member function defined
27055 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
27056 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
27057 specifiers applied to the declaration. Returns the FUNCTION_DECL
27058 for the member function. */
27060 static tree
27061 cp_parser_save_member_function_body (cp_parser* parser,
27062 cp_decl_specifier_seq *decl_specifiers,
27063 cp_declarator *declarator,
27064 tree attributes)
27066 cp_token *first;
27067 cp_token *last;
27068 tree fn;
27069 bool function_try_block = false;
27071 /* Create the FUNCTION_DECL. */
27072 fn = grokmethod (decl_specifiers, declarator, attributes);
27073 cp_finalize_omp_declare_simd (parser, fn);
27074 cp_finalize_oacc_routine (parser, fn, true);
27075 /* If something went badly wrong, bail out now. */
27076 if (fn == error_mark_node)
27078 /* If there's a function-body, skip it. */
27079 if (cp_parser_token_starts_function_definition_p
27080 (cp_lexer_peek_token (parser->lexer)))
27081 cp_parser_skip_to_end_of_block_or_statement (parser);
27082 return error_mark_node;
27085 /* Remember it, if there default args to post process. */
27086 cp_parser_save_default_args (parser, fn);
27088 /* Save away the tokens that make up the body of the
27089 function. */
27090 first = parser->lexer->next_token;
27092 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_RELAXED))
27093 cp_lexer_consume_token (parser->lexer);
27094 else if (cp_lexer_next_token_is_keyword (parser->lexer,
27095 RID_TRANSACTION_ATOMIC))
27097 cp_lexer_consume_token (parser->lexer);
27098 /* Match cp_parser_txn_attribute_opt [[ identifier ]]. */
27099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE)
27100 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_SQUARE)
27101 && (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME)
27102 || cp_lexer_nth_token_is (parser->lexer, 3, CPP_KEYWORD))
27103 && cp_lexer_nth_token_is (parser->lexer, 4, CPP_CLOSE_SQUARE)
27104 && cp_lexer_nth_token_is (parser->lexer, 5, CPP_CLOSE_SQUARE))
27106 cp_lexer_consume_token (parser->lexer);
27107 cp_lexer_consume_token (parser->lexer);
27108 cp_lexer_consume_token (parser->lexer);
27109 cp_lexer_consume_token (parser->lexer);
27110 cp_lexer_consume_token (parser->lexer);
27112 else
27113 while (cp_next_tokens_can_be_gnu_attribute_p (parser)
27114 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
27116 cp_lexer_consume_token (parser->lexer);
27117 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27118 break;
27122 /* Handle function try blocks. */
27123 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27125 cp_lexer_consume_token (parser->lexer);
27126 function_try_block = true;
27128 /* We can have braced-init-list mem-initializers before the fn body. */
27129 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
27131 cp_lexer_consume_token (parser->lexer);
27132 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
27134 /* cache_group will stop after an un-nested { } pair, too. */
27135 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
27136 break;
27138 /* variadic mem-inits have ... after the ')'. */
27139 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27140 cp_lexer_consume_token (parser->lexer);
27143 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27144 /* Handle function try blocks. */
27145 if (function_try_block)
27146 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
27147 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
27148 last = parser->lexer->next_token;
27150 /* Save away the inline definition; we will process it when the
27151 class is complete. */
27152 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
27153 DECL_PENDING_INLINE_P (fn) = 1;
27155 /* We need to know that this was defined in the class, so that
27156 friend templates are handled correctly. */
27157 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
27159 /* Add FN to the queue of functions to be parsed later. */
27160 vec_safe_push (unparsed_funs_with_definitions, fn);
27162 return fn;
27165 /* Save the tokens that make up the in-class initializer for a non-static
27166 data member. Returns a DEFAULT_ARG. */
27168 static tree
27169 cp_parser_save_nsdmi (cp_parser* parser)
27171 return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
27174 /* Parse a template-argument-list, as well as the trailing ">" (but
27175 not the opening "<"). See cp_parser_template_argument_list for the
27176 return value. */
27178 static tree
27179 cp_parser_enclosed_template_argument_list (cp_parser* parser)
27181 tree arguments;
27182 tree saved_scope;
27183 tree saved_qualifying_scope;
27184 tree saved_object_scope;
27185 bool saved_greater_than_is_operator_p;
27186 int saved_unevaluated_operand;
27187 int saved_inhibit_evaluation_warnings;
27189 /* [temp.names]
27191 When parsing a template-id, the first non-nested `>' is taken as
27192 the end of the template-argument-list rather than a greater-than
27193 operator. */
27194 saved_greater_than_is_operator_p
27195 = parser->greater_than_is_operator_p;
27196 parser->greater_than_is_operator_p = false;
27197 /* Parsing the argument list may modify SCOPE, so we save it
27198 here. */
27199 saved_scope = parser->scope;
27200 saved_qualifying_scope = parser->qualifying_scope;
27201 saved_object_scope = parser->object_scope;
27202 /* We need to evaluate the template arguments, even though this
27203 template-id may be nested within a "sizeof". */
27204 saved_unevaluated_operand = cp_unevaluated_operand;
27205 cp_unevaluated_operand = 0;
27206 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
27207 c_inhibit_evaluation_warnings = 0;
27208 /* Parse the template-argument-list itself. */
27209 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
27210 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27211 arguments = NULL_TREE;
27212 else
27213 arguments = cp_parser_template_argument_list (parser);
27214 /* Look for the `>' that ends the template-argument-list. If we find
27215 a '>>' instead, it's probably just a typo. */
27216 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
27218 if (cxx_dialect != cxx98)
27220 /* In C++0x, a `>>' in a template argument list or cast
27221 expression is considered to be two separate `>'
27222 tokens. So, change the current token to a `>', but don't
27223 consume it: it will be consumed later when the outer
27224 template argument list (or cast expression) is parsed.
27225 Note that this replacement of `>' for `>>' is necessary
27226 even if we are parsing tentatively: in the tentative
27227 case, after calling
27228 cp_parser_enclosed_template_argument_list we will always
27229 throw away all of the template arguments and the first
27230 closing `>', either because the template argument list
27231 was erroneous or because we are replacing those tokens
27232 with a CPP_TEMPLATE_ID token. The second `>' (which will
27233 not have been thrown away) is needed either to close an
27234 outer template argument list or to complete a new-style
27235 cast. */
27236 cp_token *token = cp_lexer_peek_token (parser->lexer);
27237 token->type = CPP_GREATER;
27239 else if (!saved_greater_than_is_operator_p)
27241 /* If we're in a nested template argument list, the '>>' has
27242 to be a typo for '> >'. We emit the error message, but we
27243 continue parsing and we push a '>' as next token, so that
27244 the argument list will be parsed correctly. Note that the
27245 global source location is still on the token before the
27246 '>>', so we need to say explicitly where we want it. */
27247 cp_token *token = cp_lexer_peek_token (parser->lexer);
27248 gcc_rich_location richloc (token->location);
27249 richloc.add_fixit_replace ("> >");
27250 error_at_rich_loc (&richloc, "%<>>%> should be %<> >%> "
27251 "within a nested template argument list");
27253 token->type = CPP_GREATER;
27255 else
27257 /* If this is not a nested template argument list, the '>>'
27258 is a typo for '>'. Emit an error message and continue.
27259 Same deal about the token location, but here we can get it
27260 right by consuming the '>>' before issuing the diagnostic. */
27261 cp_token *token = cp_lexer_consume_token (parser->lexer);
27262 error_at (token->location,
27263 "spurious %<>>%>, use %<>%> to terminate "
27264 "a template argument list");
27267 else
27268 cp_parser_skip_to_end_of_template_parameter_list (parser);
27269 /* The `>' token might be a greater-than operator again now. */
27270 parser->greater_than_is_operator_p
27271 = saved_greater_than_is_operator_p;
27272 /* Restore the SAVED_SCOPE. */
27273 parser->scope = saved_scope;
27274 parser->qualifying_scope = saved_qualifying_scope;
27275 parser->object_scope = saved_object_scope;
27276 cp_unevaluated_operand = saved_unevaluated_operand;
27277 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
27279 return arguments;
27282 /* MEMBER_FUNCTION is a member function, or a friend. If default
27283 arguments, or the body of the function have not yet been parsed,
27284 parse them now. */
27286 static void
27287 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
27289 timevar_push (TV_PARSE_INMETH);
27290 /* If this member is a template, get the underlying
27291 FUNCTION_DECL. */
27292 if (DECL_FUNCTION_TEMPLATE_P (member_function))
27293 member_function = DECL_TEMPLATE_RESULT (member_function);
27295 /* There should not be any class definitions in progress at this
27296 point; the bodies of members are only parsed outside of all class
27297 definitions. */
27298 gcc_assert (parser->num_classes_being_defined == 0);
27299 /* While we're parsing the member functions we might encounter more
27300 classes. We want to handle them right away, but we don't want
27301 them getting mixed up with functions that are currently in the
27302 queue. */
27303 push_unparsed_function_queues (parser);
27305 /* Make sure that any template parameters are in scope. */
27306 maybe_begin_member_template_processing (member_function);
27308 /* If the body of the function has not yet been parsed, parse it
27309 now. */
27310 if (DECL_PENDING_INLINE_P (member_function))
27312 tree function_scope;
27313 cp_token_cache *tokens;
27315 /* The function is no longer pending; we are processing it. */
27316 tokens = DECL_PENDING_INLINE_INFO (member_function);
27317 DECL_PENDING_INLINE_INFO (member_function) = NULL;
27318 DECL_PENDING_INLINE_P (member_function) = 0;
27320 /* If this is a local class, enter the scope of the containing
27321 function. */
27322 function_scope = current_function_decl;
27323 if (function_scope)
27324 push_function_context ();
27326 /* Push the body of the function onto the lexer stack. */
27327 cp_parser_push_lexer_for_tokens (parser, tokens);
27329 /* Let the front end know that we going to be defining this
27330 function. */
27331 start_preparsed_function (member_function, NULL_TREE,
27332 SF_PRE_PARSED | SF_INCLASS_INLINE);
27334 /* Don't do access checking if it is a templated function. */
27335 if (processing_template_decl)
27336 push_deferring_access_checks (dk_no_check);
27338 /* #pragma omp declare reduction needs special parsing. */
27339 if (DECL_OMP_DECLARE_REDUCTION_P (member_function))
27341 parser->lexer->in_pragma = true;
27342 cp_parser_omp_declare_reduction_exprs (member_function, parser);
27343 finish_function (/*inline*/2);
27344 cp_check_omp_declare_reduction (member_function);
27346 else
27347 /* Now, parse the body of the function. */
27348 cp_parser_function_definition_after_declarator (parser,
27349 /*inline_p=*/true);
27351 if (processing_template_decl)
27352 pop_deferring_access_checks ();
27354 /* Leave the scope of the containing function. */
27355 if (function_scope)
27356 pop_function_context ();
27357 cp_parser_pop_lexer (parser);
27360 /* Remove any template parameters from the symbol table. */
27361 maybe_end_member_template_processing ();
27363 /* Restore the queue. */
27364 pop_unparsed_function_queues (parser);
27365 timevar_pop (TV_PARSE_INMETH);
27368 /* If DECL contains any default args, remember it on the unparsed
27369 functions queue. */
27371 static void
27372 cp_parser_save_default_args (cp_parser* parser, tree decl)
27374 tree probe;
27376 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
27377 probe;
27378 probe = TREE_CHAIN (probe))
27379 if (TREE_PURPOSE (probe))
27381 cp_default_arg_entry entry = {current_class_type, decl};
27382 vec_safe_push (unparsed_funs_with_default_args, entry);
27383 break;
27387 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
27388 which is either a FIELD_DECL or PARM_DECL. Parse it and return
27389 the result. For a PARM_DECL, PARMTYPE is the corresponding type
27390 from the parameter-type-list. */
27392 static tree
27393 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
27394 tree default_arg, tree parmtype)
27396 cp_token_cache *tokens;
27397 tree parsed_arg;
27398 bool dummy;
27400 if (default_arg == error_mark_node)
27401 return error_mark_node;
27403 /* Push the saved tokens for the default argument onto the parser's
27404 lexer stack. */
27405 tokens = DEFARG_TOKENS (default_arg);
27406 cp_parser_push_lexer_for_tokens (parser, tokens);
27408 start_lambda_scope (decl);
27410 /* Parse the default argument. */
27411 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
27412 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
27413 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
27415 finish_lambda_scope ();
27417 if (parsed_arg == error_mark_node)
27418 cp_parser_skip_to_end_of_statement (parser);
27420 if (!processing_template_decl)
27422 /* In a non-template class, check conversions now. In a template,
27423 we'll wait and instantiate these as needed. */
27424 if (TREE_CODE (decl) == PARM_DECL)
27425 parsed_arg = check_default_argument (parmtype, parsed_arg,
27426 tf_warning_or_error);
27427 else if (maybe_reject_flexarray_init (decl, parsed_arg))
27428 parsed_arg = error_mark_node;
27429 else
27430 parsed_arg = digest_nsdmi_init (decl, parsed_arg);
27433 /* If the token stream has not been completely used up, then
27434 there was extra junk after the end of the default
27435 argument. */
27436 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
27438 if (TREE_CODE (decl) == PARM_DECL)
27439 cp_parser_error (parser, "expected %<,%>");
27440 else
27441 cp_parser_error (parser, "expected %<;%>");
27444 /* Revert to the main lexer. */
27445 cp_parser_pop_lexer (parser);
27447 return parsed_arg;
27450 /* FIELD is a non-static data member with an initializer which we saved for
27451 later; parse it now. */
27453 static void
27454 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
27456 tree def;
27458 maybe_begin_member_template_processing (field);
27460 push_unparsed_function_queues (parser);
27461 def = cp_parser_late_parse_one_default_arg (parser, field,
27462 DECL_INITIAL (field),
27463 NULL_TREE);
27464 pop_unparsed_function_queues (parser);
27466 maybe_end_member_template_processing ();
27468 DECL_INITIAL (field) = def;
27471 /* FN is a FUNCTION_DECL which may contains a parameter with an
27472 unparsed DEFAULT_ARG. Parse the default args now. This function
27473 assumes that the current scope is the scope in which the default
27474 argument should be processed. */
27476 static void
27477 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
27479 bool saved_local_variables_forbidden_p;
27480 tree parm, parmdecl;
27482 /* While we're parsing the default args, we might (due to the
27483 statement expression extension) encounter more classes. We want
27484 to handle them right away, but we don't want them getting mixed
27485 up with default args that are currently in the queue. */
27486 push_unparsed_function_queues (parser);
27488 /* Local variable names (and the `this' keyword) may not appear
27489 in a default argument. */
27490 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
27491 parser->local_variables_forbidden_p = true;
27493 push_defarg_context (fn);
27495 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
27496 parmdecl = DECL_ARGUMENTS (fn);
27497 parm && parm != void_list_node;
27498 parm = TREE_CHAIN (parm),
27499 parmdecl = DECL_CHAIN (parmdecl))
27501 tree default_arg = TREE_PURPOSE (parm);
27502 tree parsed_arg;
27503 vec<tree, va_gc> *insts;
27504 tree copy;
27505 unsigned ix;
27507 if (!default_arg)
27508 continue;
27510 if (TREE_CODE (default_arg) != DEFAULT_ARG)
27511 /* This can happen for a friend declaration for a function
27512 already declared with default arguments. */
27513 continue;
27515 parsed_arg
27516 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
27517 default_arg,
27518 TREE_VALUE (parm));
27519 if (parsed_arg == error_mark_node)
27521 continue;
27524 TREE_PURPOSE (parm) = parsed_arg;
27526 /* Update any instantiations we've already created. */
27527 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
27528 vec_safe_iterate (insts, ix, &copy); ix++)
27529 TREE_PURPOSE (copy) = parsed_arg;
27532 pop_defarg_context ();
27534 /* Make sure no default arg is missing. */
27535 check_default_args (fn);
27537 /* Restore the state of local_variables_forbidden_p. */
27538 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
27540 /* Restore the queue. */
27541 pop_unparsed_function_queues (parser);
27544 /* Subroutine of cp_parser_sizeof_operand, for handling C++11
27546 sizeof ... ( identifier )
27548 where the 'sizeof' token has already been consumed. */
27550 static tree
27551 cp_parser_sizeof_pack (cp_parser *parser)
27553 /* Consume the `...'. */
27554 cp_lexer_consume_token (parser->lexer);
27555 maybe_warn_variadic_templates ();
27557 bool paren = cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN);
27558 if (paren)
27559 cp_lexer_consume_token (parser->lexer);
27560 else
27561 permerror (cp_lexer_peek_token (parser->lexer)->location,
27562 "%<sizeof...%> argument must be surrounded by parentheses");
27564 cp_token *token = cp_lexer_peek_token (parser->lexer);
27565 tree name = cp_parser_identifier (parser);
27566 if (name == error_mark_node)
27567 return error_mark_node;
27568 /* The name is not qualified. */
27569 parser->scope = NULL_TREE;
27570 parser->qualifying_scope = NULL_TREE;
27571 parser->object_scope = NULL_TREE;
27572 tree expr = cp_parser_lookup_name_simple (parser, name, token->location);
27573 if (expr == error_mark_node)
27574 cp_parser_name_lookup_error (parser, name, expr, NLE_NULL,
27575 token->location);
27576 if (TREE_CODE (expr) == TYPE_DECL || TREE_CODE (expr) == TEMPLATE_DECL)
27577 expr = TREE_TYPE (expr);
27578 else if (TREE_CODE (expr) == CONST_DECL)
27579 expr = DECL_INITIAL (expr);
27580 expr = make_pack_expansion (expr);
27581 PACK_EXPANSION_SIZEOF_P (expr) = true;
27583 if (paren)
27584 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27586 return expr;
27589 /* Parse the operand of `sizeof' (or a similar operator). Returns
27590 either a TYPE or an expression, depending on the form of the
27591 input. The KEYWORD indicates which kind of expression we have
27592 encountered. */
27594 static tree
27595 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
27597 tree expr = NULL_TREE;
27598 const char *saved_message;
27599 char *tmp;
27600 bool saved_integral_constant_expression_p;
27601 bool saved_non_integral_constant_expression_p;
27603 /* If it's a `...', then we are computing the length of a parameter
27604 pack. */
27605 if (keyword == RID_SIZEOF
27606 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
27607 return cp_parser_sizeof_pack (parser);
27609 /* Types cannot be defined in a `sizeof' expression. Save away the
27610 old message. */
27611 saved_message = parser->type_definition_forbidden_message;
27612 /* And create the new one. */
27613 tmp = concat ("types may not be defined in %<",
27614 IDENTIFIER_POINTER (ridpointers[keyword]),
27615 "%> expressions", NULL);
27616 parser->type_definition_forbidden_message = tmp;
27618 /* The restrictions on constant-expressions do not apply inside
27619 sizeof expressions. */
27620 saved_integral_constant_expression_p
27621 = parser->integral_constant_expression_p;
27622 saved_non_integral_constant_expression_p
27623 = parser->non_integral_constant_expression_p;
27624 parser->integral_constant_expression_p = false;
27626 /* Do not actually evaluate the expression. */
27627 ++cp_unevaluated_operand;
27628 ++c_inhibit_evaluation_warnings;
27629 /* If it's a `(', then we might be looking at the type-id
27630 construction. */
27631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
27633 tree type = NULL_TREE;
27635 /* We can't be sure yet whether we're looking at a type-id or an
27636 expression. */
27637 cp_parser_parse_tentatively (parser);
27638 /* Note: as a GNU Extension, compound literals are considered
27639 postfix-expressions as they are in C99, so they are valid
27640 arguments to sizeof. See comment in cp_parser_cast_expression
27641 for details. */
27642 if (cp_parser_compound_literal_p (parser))
27643 cp_parser_simulate_error (parser);
27644 else
27646 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
27647 parser->in_type_id_in_expr_p = true;
27648 /* Look for the type-id. */
27649 type = cp_parser_type_id (parser);
27650 /* Look for the closing `)'. */
27651 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27652 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
27655 /* If all went well, then we're done. */
27656 if (cp_parser_parse_definitely (parser))
27658 cp_decl_specifier_seq decl_specs;
27660 /* Build a trivial decl-specifier-seq. */
27661 clear_decl_specs (&decl_specs);
27662 decl_specs.type = type;
27664 /* Call grokdeclarator to figure out what type this is. */
27665 expr = grokdeclarator (NULL,
27666 &decl_specs,
27667 TYPENAME,
27668 /*initialized=*/0,
27669 /*attrlist=*/NULL);
27673 /* If the type-id production did not work out, then we must be
27674 looking at the unary-expression production. */
27675 if (!expr)
27676 expr = cp_parser_unary_expression (parser);
27678 /* Go back to evaluating expressions. */
27679 --cp_unevaluated_operand;
27680 --c_inhibit_evaluation_warnings;
27682 /* Free the message we created. */
27683 free (tmp);
27684 /* And restore the old one. */
27685 parser->type_definition_forbidden_message = saved_message;
27686 parser->integral_constant_expression_p
27687 = saved_integral_constant_expression_p;
27688 parser->non_integral_constant_expression_p
27689 = saved_non_integral_constant_expression_p;
27691 return expr;
27694 /* If the current declaration has no declarator, return true. */
27696 static bool
27697 cp_parser_declares_only_class_p (cp_parser *parser)
27699 /* If the next token is a `;' or a `,' then there is no
27700 declarator. */
27701 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
27702 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
27705 /* Update the DECL_SPECS to reflect the storage class indicated by
27706 KEYWORD. */
27708 static void
27709 cp_parser_set_storage_class (cp_parser *parser,
27710 cp_decl_specifier_seq *decl_specs,
27711 enum rid keyword,
27712 cp_token *token)
27714 cp_storage_class storage_class;
27716 if (parser->in_unbraced_linkage_specification_p)
27718 error_at (token->location, "invalid use of %qD in linkage specification",
27719 ridpointers[keyword]);
27720 return;
27722 else if (decl_specs->storage_class != sc_none)
27724 decl_specs->conflicting_specifiers_p = true;
27725 return;
27728 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
27729 && decl_spec_seq_has_spec_p (decl_specs, ds_thread)
27730 && decl_specs->gnu_thread_keyword_p)
27732 pedwarn (decl_specs->locations[ds_thread], 0,
27733 "%<__thread%> before %qD", ridpointers[keyword]);
27736 switch (keyword)
27738 case RID_AUTO:
27739 storage_class = sc_auto;
27740 break;
27741 case RID_REGISTER:
27742 storage_class = sc_register;
27743 break;
27744 case RID_STATIC:
27745 storage_class = sc_static;
27746 break;
27747 case RID_EXTERN:
27748 storage_class = sc_extern;
27749 break;
27750 case RID_MUTABLE:
27751 storage_class = sc_mutable;
27752 break;
27753 default:
27754 gcc_unreachable ();
27756 decl_specs->storage_class = storage_class;
27757 set_and_check_decl_spec_loc (decl_specs, ds_storage_class, token);
27759 /* A storage class specifier cannot be applied alongside a typedef
27760 specifier. If there is a typedef specifier present then set
27761 conflicting_specifiers_p which will trigger an error later
27762 on in grokdeclarator. */
27763 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef))
27764 decl_specs->conflicting_specifiers_p = true;
27767 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
27768 is true, the type is a class or enum definition. */
27770 static void
27771 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
27772 tree type_spec,
27773 cp_token *token,
27774 bool type_definition_p)
27776 decl_specs->any_specifiers_p = true;
27778 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
27779 (with, for example, in "typedef int wchar_t;") we remember that
27780 this is what happened. In system headers, we ignore these
27781 declarations so that G++ can work with system headers that are not
27782 C++-safe. */
27783 if (decl_spec_seq_has_spec_p (decl_specs, ds_typedef)
27784 && !type_definition_p
27785 && (type_spec == boolean_type_node
27786 || type_spec == char16_type_node
27787 || type_spec == char32_type_node
27788 || type_spec == wchar_type_node)
27789 && (decl_specs->type
27790 || decl_spec_seq_has_spec_p (decl_specs, ds_long)
27791 || decl_spec_seq_has_spec_p (decl_specs, ds_short)
27792 || decl_spec_seq_has_spec_p (decl_specs, ds_unsigned)
27793 || decl_spec_seq_has_spec_p (decl_specs, ds_signed)))
27795 decl_specs->redefined_builtin_type = type_spec;
27796 set_and_check_decl_spec_loc (decl_specs,
27797 ds_redefined_builtin_type_spec,
27798 token);
27799 if (!decl_specs->type)
27801 decl_specs->type = type_spec;
27802 decl_specs->type_definition_p = false;
27803 set_and_check_decl_spec_loc (decl_specs,ds_type_spec, token);
27806 else if (decl_specs->type)
27807 decl_specs->multiple_types_p = true;
27808 else
27810 decl_specs->type = type_spec;
27811 decl_specs->type_definition_p = type_definition_p;
27812 decl_specs->redefined_builtin_type = NULL_TREE;
27813 set_and_check_decl_spec_loc (decl_specs, ds_type_spec, token);
27817 /* True iff TOKEN is the GNU keyword __thread. */
27819 static bool
27820 token_is__thread (cp_token *token)
27822 gcc_assert (token->keyword == RID_THREAD);
27823 return id_equal (token->u.value, "__thread");
27826 /* Set the location for a declarator specifier and check if it is
27827 duplicated.
27829 DECL_SPECS is the sequence of declarator specifiers onto which to
27830 set the location.
27832 DS is the single declarator specifier to set which location is to
27833 be set onto the existing sequence of declarators.
27835 LOCATION is the location for the declarator specifier to
27836 consider. */
27838 static void
27839 set_and_check_decl_spec_loc (cp_decl_specifier_seq *decl_specs,
27840 cp_decl_spec ds, cp_token *token)
27842 gcc_assert (ds < ds_last);
27844 if (decl_specs == NULL)
27845 return;
27847 source_location location = token->location;
27849 if (decl_specs->locations[ds] == 0)
27851 decl_specs->locations[ds] = location;
27852 if (ds == ds_thread)
27853 decl_specs->gnu_thread_keyword_p = token_is__thread (token);
27855 else
27857 if (ds == ds_long)
27859 if (decl_specs->locations[ds_long_long] != 0)
27860 error_at (location,
27861 "%<long long long%> is too long for GCC");
27862 else
27864 decl_specs->locations[ds_long_long] = location;
27865 pedwarn_cxx98 (location,
27866 OPT_Wlong_long,
27867 "ISO C++ 1998 does not support %<long long%>");
27870 else if (ds == ds_thread)
27872 bool gnu = token_is__thread (token);
27873 if (gnu != decl_specs->gnu_thread_keyword_p)
27874 error_at (location,
27875 "both %<__thread%> and %<thread_local%> specified");
27876 else
27878 gcc_rich_location richloc (location);
27879 richloc.add_fixit_remove ();
27880 error_at_rich_loc (&richloc, "duplicate %qD", token->u.value);
27883 else
27885 static const char *const decl_spec_names[] = {
27886 "signed",
27887 "unsigned",
27888 "short",
27889 "long",
27890 "const",
27891 "volatile",
27892 "restrict",
27893 "inline",
27894 "virtual",
27895 "explicit",
27896 "friend",
27897 "typedef",
27898 "using",
27899 "constexpr",
27900 "__complex"
27902 gcc_rich_location richloc (location);
27903 richloc.add_fixit_remove ();
27904 error_at_rich_loc (&richloc, "duplicate %qs", decl_spec_names[ds]);
27909 /* Return true iff the declarator specifier DS is present in the
27910 sequence of declarator specifiers DECL_SPECS. */
27912 bool
27913 decl_spec_seq_has_spec_p (const cp_decl_specifier_seq * decl_specs,
27914 cp_decl_spec ds)
27916 gcc_assert (ds < ds_last);
27918 if (decl_specs == NULL)
27919 return false;
27921 return decl_specs->locations[ds] != 0;
27924 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
27925 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
27927 static bool
27928 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
27930 return decl_spec_seq_has_spec_p (decl_specifiers, ds_friend);
27933 /* Issue an error message indicating that TOKEN_DESC was expected.
27934 If KEYWORD is true, it indicated this function is called by
27935 cp_parser_require_keword and the required token can only be
27936 a indicated keyword. */
27938 static void
27939 cp_parser_required_error (cp_parser *parser,
27940 required_token token_desc,
27941 bool keyword)
27943 switch (token_desc)
27945 case RT_NEW:
27946 cp_parser_error (parser, "expected %<new%>");
27947 return;
27948 case RT_DELETE:
27949 cp_parser_error (parser, "expected %<delete%>");
27950 return;
27951 case RT_RETURN:
27952 cp_parser_error (parser, "expected %<return%>");
27953 return;
27954 case RT_WHILE:
27955 cp_parser_error (parser, "expected %<while%>");
27956 return;
27957 case RT_EXTERN:
27958 cp_parser_error (parser, "expected %<extern%>");
27959 return;
27960 case RT_STATIC_ASSERT:
27961 cp_parser_error (parser, "expected %<static_assert%>");
27962 return;
27963 case RT_DECLTYPE:
27964 cp_parser_error (parser, "expected %<decltype%>");
27965 return;
27966 case RT_OPERATOR:
27967 cp_parser_error (parser, "expected %<operator%>");
27968 return;
27969 case RT_CLASS:
27970 cp_parser_error (parser, "expected %<class%>");
27971 return;
27972 case RT_TEMPLATE:
27973 cp_parser_error (parser, "expected %<template%>");
27974 return;
27975 case RT_NAMESPACE:
27976 cp_parser_error (parser, "expected %<namespace%>");
27977 return;
27978 case RT_USING:
27979 cp_parser_error (parser, "expected %<using%>");
27980 return;
27981 case RT_ASM:
27982 cp_parser_error (parser, "expected %<asm%>");
27983 return;
27984 case RT_TRY:
27985 cp_parser_error (parser, "expected %<try%>");
27986 return;
27987 case RT_CATCH:
27988 cp_parser_error (parser, "expected %<catch%>");
27989 return;
27990 case RT_THROW:
27991 cp_parser_error (parser, "expected %<throw%>");
27992 return;
27993 case RT_LABEL:
27994 cp_parser_error (parser, "expected %<__label__%>");
27995 return;
27996 case RT_AT_TRY:
27997 cp_parser_error (parser, "expected %<@try%>");
27998 return;
27999 case RT_AT_SYNCHRONIZED:
28000 cp_parser_error (parser, "expected %<@synchronized%>");
28001 return;
28002 case RT_AT_THROW:
28003 cp_parser_error (parser, "expected %<@throw%>");
28004 return;
28005 case RT_TRANSACTION_ATOMIC:
28006 cp_parser_error (parser, "expected %<__transaction_atomic%>");
28007 return;
28008 case RT_TRANSACTION_RELAXED:
28009 cp_parser_error (parser, "expected %<__transaction_relaxed%>");
28010 return;
28011 default:
28012 break;
28014 if (!keyword)
28016 switch (token_desc)
28018 case RT_SEMICOLON:
28019 cp_parser_error (parser, "expected %<;%>");
28020 return;
28021 case RT_OPEN_PAREN:
28022 cp_parser_error (parser, "expected %<(%>");
28023 return;
28024 case RT_CLOSE_BRACE:
28025 cp_parser_error (parser, "expected %<}%>");
28026 return;
28027 case RT_OPEN_BRACE:
28028 cp_parser_error (parser, "expected %<{%>");
28029 return;
28030 case RT_CLOSE_SQUARE:
28031 cp_parser_error (parser, "expected %<]%>");
28032 return;
28033 case RT_OPEN_SQUARE:
28034 cp_parser_error (parser, "expected %<[%>");
28035 return;
28036 case RT_COMMA:
28037 cp_parser_error (parser, "expected %<,%>");
28038 return;
28039 case RT_SCOPE:
28040 cp_parser_error (parser, "expected %<::%>");
28041 return;
28042 case RT_LESS:
28043 cp_parser_error (parser, "expected %<<%>");
28044 return;
28045 case RT_GREATER:
28046 cp_parser_error (parser, "expected %<>%>");
28047 return;
28048 case RT_EQ:
28049 cp_parser_error (parser, "expected %<=%>");
28050 return;
28051 case RT_ELLIPSIS:
28052 cp_parser_error (parser, "expected %<...%>");
28053 return;
28054 case RT_MULT:
28055 cp_parser_error (parser, "expected %<*%>");
28056 return;
28057 case RT_COMPL:
28058 cp_parser_error (parser, "expected %<~%>");
28059 return;
28060 case RT_COLON:
28061 cp_parser_error (parser, "expected %<:%>");
28062 return;
28063 case RT_COLON_SCOPE:
28064 cp_parser_error (parser, "expected %<:%> or %<::%>");
28065 return;
28066 case RT_CLOSE_PAREN:
28067 cp_parser_error (parser, "expected %<)%>");
28068 return;
28069 case RT_COMMA_CLOSE_PAREN:
28070 cp_parser_error (parser, "expected %<,%> or %<)%>");
28071 return;
28072 case RT_PRAGMA_EOL:
28073 cp_parser_error (parser, "expected end of line");
28074 return;
28075 case RT_NAME:
28076 cp_parser_error (parser, "expected identifier");
28077 return;
28078 case RT_SELECT:
28079 cp_parser_error (parser, "expected selection-statement");
28080 return;
28081 case RT_ITERATION:
28082 cp_parser_error (parser, "expected iteration-statement");
28083 return;
28084 case RT_JUMP:
28085 cp_parser_error (parser, "expected jump-statement");
28086 return;
28087 case RT_CLASS_KEY:
28088 cp_parser_error (parser, "expected class-key");
28089 return;
28090 case RT_CLASS_TYPENAME_TEMPLATE:
28091 cp_parser_error (parser,
28092 "expected %<class%>, %<typename%>, or %<template%>");
28093 return;
28094 default:
28095 gcc_unreachable ();
28098 else
28099 gcc_unreachable ();
28104 /* If the next token is of the indicated TYPE, consume it. Otherwise,
28105 issue an error message indicating that TOKEN_DESC was expected.
28107 Returns the token consumed, if the token had the appropriate type.
28108 Otherwise, returns NULL. */
28110 static cp_token *
28111 cp_parser_require (cp_parser* parser,
28112 enum cpp_ttype type,
28113 required_token token_desc)
28115 if (cp_lexer_next_token_is (parser->lexer, type))
28116 return cp_lexer_consume_token (parser->lexer);
28117 else
28119 /* Output the MESSAGE -- unless we're parsing tentatively. */
28120 if (!cp_parser_simulate_error (parser))
28121 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
28122 return NULL;
28126 /* An error message is produced if the next token is not '>'.
28127 All further tokens are skipped until the desired token is
28128 found or '{', '}', ';' or an unbalanced ')' or ']'. */
28130 static void
28131 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
28133 /* Current level of '< ... >'. */
28134 unsigned level = 0;
28135 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
28136 unsigned nesting_depth = 0;
28138 /* Are we ready, yet? If not, issue error message. */
28139 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
28140 return;
28142 /* Skip tokens until the desired token is found. */
28143 while (true)
28145 /* Peek at the next token. */
28146 switch (cp_lexer_peek_token (parser->lexer)->type)
28148 case CPP_LESS:
28149 if (!nesting_depth)
28150 ++level;
28151 break;
28153 case CPP_RSHIFT:
28154 if (cxx_dialect == cxx98)
28155 /* C++0x views the `>>' operator as two `>' tokens, but
28156 C++98 does not. */
28157 break;
28158 else if (!nesting_depth && level-- == 0)
28160 /* We've hit a `>>' where the first `>' closes the
28161 template argument list, and the second `>' is
28162 spurious. Just consume the `>>' and stop; we've
28163 already produced at least one error. */
28164 cp_lexer_consume_token (parser->lexer);
28165 return;
28167 /* Fall through for C++0x, so we handle the second `>' in
28168 the `>>'. */
28169 gcc_fallthrough ();
28171 case CPP_GREATER:
28172 if (!nesting_depth && level-- == 0)
28174 /* We've reached the token we want, consume it and stop. */
28175 cp_lexer_consume_token (parser->lexer);
28176 return;
28178 break;
28180 case CPP_OPEN_PAREN:
28181 case CPP_OPEN_SQUARE:
28182 ++nesting_depth;
28183 break;
28185 case CPP_CLOSE_PAREN:
28186 case CPP_CLOSE_SQUARE:
28187 if (nesting_depth-- == 0)
28188 return;
28189 break;
28191 case CPP_EOF:
28192 case CPP_PRAGMA_EOL:
28193 case CPP_SEMICOLON:
28194 case CPP_OPEN_BRACE:
28195 case CPP_CLOSE_BRACE:
28196 /* The '>' was probably forgotten, don't look further. */
28197 return;
28199 default:
28200 break;
28203 /* Consume this token. */
28204 cp_lexer_consume_token (parser->lexer);
28208 /* If the next token is the indicated keyword, consume it. Otherwise,
28209 issue an error message indicating that TOKEN_DESC was expected.
28211 Returns the token consumed, if the token had the appropriate type.
28212 Otherwise, returns NULL. */
28214 static cp_token *
28215 cp_parser_require_keyword (cp_parser* parser,
28216 enum rid keyword,
28217 required_token token_desc)
28219 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
28221 if (token && token->keyword != keyword)
28223 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
28224 return NULL;
28227 return token;
28230 /* Returns TRUE iff TOKEN is a token that can begin the body of a
28231 function-definition. */
28233 static bool
28234 cp_parser_token_starts_function_definition_p (cp_token* token)
28236 return (/* An ordinary function-body begins with an `{'. */
28237 token->type == CPP_OPEN_BRACE
28238 /* A ctor-initializer begins with a `:'. */
28239 || token->type == CPP_COLON
28240 /* A function-try-block begins with `try'. */
28241 || token->keyword == RID_TRY
28242 /* A function-transaction-block begins with `__transaction_atomic'
28243 or `__transaction_relaxed'. */
28244 || token->keyword == RID_TRANSACTION_ATOMIC
28245 || token->keyword == RID_TRANSACTION_RELAXED
28246 /* The named return value extension begins with `return'. */
28247 || token->keyword == RID_RETURN);
28250 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
28251 definition. */
28253 static bool
28254 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
28256 cp_token *token;
28258 token = cp_lexer_peek_token (parser->lexer);
28259 return (token->type == CPP_OPEN_BRACE
28260 || (token->type == CPP_COLON
28261 && !parser->colon_doesnt_start_class_def_p));
28264 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
28265 C++0x) ending a template-argument. */
28267 static bool
28268 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
28270 cp_token *token;
28272 token = cp_lexer_peek_token (parser->lexer);
28273 return (token->type == CPP_COMMA
28274 || token->type == CPP_GREATER
28275 || token->type == CPP_ELLIPSIS
28276 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
28279 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
28280 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
28282 static bool
28283 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
28284 size_t n)
28286 cp_token *token;
28288 token = cp_lexer_peek_nth_token (parser->lexer, n);
28289 if (token->type == CPP_LESS)
28290 return true;
28291 /* Check for the sequence `<::' in the original code. It would be lexed as
28292 `[:', where `[' is a digraph, and there is no whitespace before
28293 `:'. */
28294 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
28296 cp_token *token2;
28297 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
28298 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
28299 return true;
28301 return false;
28304 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
28305 or none_type otherwise. */
28307 static enum tag_types
28308 cp_parser_token_is_class_key (cp_token* token)
28310 switch (token->keyword)
28312 case RID_CLASS:
28313 return class_type;
28314 case RID_STRUCT:
28315 return record_type;
28316 case RID_UNION:
28317 return union_type;
28319 default:
28320 return none_type;
28324 /* Returns the kind of tag indicated by TOKEN, if it is a type-parameter-key,
28325 or none_type otherwise or if the token is null. */
28327 static enum tag_types
28328 cp_parser_token_is_type_parameter_key (cp_token* token)
28330 if (!token)
28331 return none_type;
28333 switch (token->keyword)
28335 case RID_CLASS:
28336 return class_type;
28337 case RID_TYPENAME:
28338 return typename_type;
28340 default:
28341 return none_type;
28345 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
28347 static void
28348 cp_parser_check_class_key (enum tag_types class_key, tree type)
28350 if (type == error_mark_node)
28351 return;
28352 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
28354 if (permerror (input_location, "%qs tag used in naming %q#T",
28355 class_key == union_type ? "union"
28356 : class_key == record_type ? "struct" : "class",
28357 type))
28358 inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
28359 "%q#T was previously declared here", type);
28363 /* Issue an error message if DECL is redeclared with different
28364 access than its original declaration [class.access.spec/3].
28365 This applies to nested classes, nested class templates and
28366 enumerations [class.mem/1]. */
28368 static void
28369 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
28371 if (!decl
28372 || (!CLASS_TYPE_P (TREE_TYPE (decl))
28373 && TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE))
28374 return;
28376 if ((TREE_PRIVATE (decl)
28377 != (current_access_specifier == access_private_node))
28378 || (TREE_PROTECTED (decl)
28379 != (current_access_specifier == access_protected_node)))
28380 error_at (location, "%qD redeclared with different access", decl);
28383 /* Look for the `template' keyword, as a syntactic disambiguator.
28384 Return TRUE iff it is present, in which case it will be
28385 consumed. */
28387 static bool
28388 cp_parser_optional_template_keyword (cp_parser *parser)
28390 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
28392 /* In C++98 the `template' keyword can only be used within templates;
28393 outside templates the parser can always figure out what is a
28394 template and what is not. In C++11, per the resolution of DR 468,
28395 `template' is allowed in cases where it is not strictly necessary. */
28396 if (!processing_template_decl
28397 && pedantic && cxx_dialect == cxx98)
28399 cp_token *token = cp_lexer_peek_token (parser->lexer);
28400 pedwarn (token->location, OPT_Wpedantic,
28401 "in C++98 %<template%> (as a disambiguator) is only "
28402 "allowed within templates");
28403 /* If this part of the token stream is rescanned, the same
28404 error message would be generated. So, we purge the token
28405 from the stream. */
28406 cp_lexer_purge_token (parser->lexer);
28407 return false;
28409 else
28411 /* Consume the `template' keyword. */
28412 cp_lexer_consume_token (parser->lexer);
28413 return true;
28416 return false;
28419 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
28420 set PARSER->SCOPE, and perform other related actions. */
28422 static void
28423 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
28425 struct tree_check *check_value;
28427 /* Get the stored value. */
28428 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
28429 /* Set the scope from the stored value. */
28430 parser->scope = saved_checks_value (check_value);
28431 parser->qualifying_scope = check_value->qualifying_scope;
28432 parser->object_scope = NULL_TREE;
28435 /* Consume tokens up through a non-nested END token. Returns TRUE if we
28436 encounter the end of a block before what we were looking for. */
28438 static bool
28439 cp_parser_cache_group (cp_parser *parser,
28440 enum cpp_ttype end,
28441 unsigned depth)
28443 while (true)
28445 cp_token *token = cp_lexer_peek_token (parser->lexer);
28447 /* Abort a parenthesized expression if we encounter a semicolon. */
28448 if ((end == CPP_CLOSE_PAREN || depth == 0)
28449 && token->type == CPP_SEMICOLON)
28450 return true;
28451 /* If we've reached the end of the file, stop. */
28452 if (token->type == CPP_EOF
28453 || (end != CPP_PRAGMA_EOL
28454 && token->type == CPP_PRAGMA_EOL))
28455 return true;
28456 if (token->type == CPP_CLOSE_BRACE && depth == 0)
28457 /* We've hit the end of an enclosing block, so there's been some
28458 kind of syntax error. */
28459 return true;
28461 /* Consume the token. */
28462 cp_lexer_consume_token (parser->lexer);
28463 /* See if it starts a new group. */
28464 if (token->type == CPP_OPEN_BRACE)
28466 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
28467 /* In theory this should probably check end == '}', but
28468 cp_parser_save_member_function_body needs it to exit
28469 after either '}' or ')' when called with ')'. */
28470 if (depth == 0)
28471 return false;
28473 else if (token->type == CPP_OPEN_PAREN)
28475 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
28476 if (depth == 0 && end == CPP_CLOSE_PAREN)
28477 return false;
28479 else if (token->type == CPP_PRAGMA)
28480 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
28481 else if (token->type == end)
28482 return false;
28486 /* Like above, for caching a default argument or NSDMI. Both of these are
28487 terminated by a non-nested comma, but it can be unclear whether or not a
28488 comma is nested in a template argument list unless we do more parsing.
28489 In order to handle this ambiguity, when we encounter a ',' after a '<'
28490 we try to parse what follows as a parameter-declaration-list (in the
28491 case of a default argument) or a member-declarator (in the case of an
28492 NSDMI). If that succeeds, then we stop caching. */
28494 static tree
28495 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
28497 unsigned depth = 0;
28498 int maybe_template_id = 0;
28499 cp_token *first_token;
28500 cp_token *token;
28501 tree default_argument;
28503 /* Add tokens until we have processed the entire default
28504 argument. We add the range [first_token, token). */
28505 first_token = cp_lexer_peek_token (parser->lexer);
28506 if (first_token->type == CPP_OPEN_BRACE)
28508 /* For list-initialization, this is straightforward. */
28509 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
28510 token = cp_lexer_peek_token (parser->lexer);
28512 else while (true)
28514 bool done = false;
28516 /* Peek at the next token. */
28517 token = cp_lexer_peek_token (parser->lexer);
28518 /* What we do depends on what token we have. */
28519 switch (token->type)
28521 /* In valid code, a default argument must be
28522 immediately followed by a `,' `)', or `...'. */
28523 case CPP_COMMA:
28524 if (depth == 0 && maybe_template_id)
28526 /* If we've seen a '<', we might be in a
28527 template-argument-list. Until Core issue 325 is
28528 resolved, we don't know how this situation ought
28529 to be handled, so try to DTRT. We check whether
28530 what comes after the comma is a valid parameter
28531 declaration list. If it is, then the comma ends
28532 the default argument; otherwise the default
28533 argument continues. */
28534 bool error = false;
28535 cp_token *peek;
28537 /* Set ITALP so cp_parser_parameter_declaration_list
28538 doesn't decide to commit to this parse. */
28539 bool saved_italp = parser->in_template_argument_list_p;
28540 parser->in_template_argument_list_p = true;
28542 cp_parser_parse_tentatively (parser);
28544 if (nsdmi)
28546 /* Parse declarators until we reach a non-comma or
28547 somthing that cannot be an initializer.
28548 Just checking whether we're looking at a single
28549 declarator is insufficient. Consider:
28550 int var = tuple<T,U>::x;
28551 The template parameter 'U' looks exactly like a
28552 declarator. */
28555 int ctor_dtor_or_conv_p;
28556 cp_lexer_consume_token (parser->lexer);
28557 cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
28558 &ctor_dtor_or_conv_p,
28559 /*parenthesized_p=*/NULL,
28560 /*member_p=*/true,
28561 /*friend_p=*/false);
28562 peek = cp_lexer_peek_token (parser->lexer);
28563 if (cp_parser_error_occurred (parser))
28564 break;
28566 while (peek->type == CPP_COMMA);
28567 /* If we met an '=' or ';' then the original comma
28568 was the end of the NSDMI. Otherwise assume
28569 we're still in the NSDMI. */
28570 error = (peek->type != CPP_EQ
28571 && peek->type != CPP_SEMICOLON);
28573 else
28575 cp_lexer_consume_token (parser->lexer);
28576 begin_scope (sk_function_parms, NULL_TREE);
28577 cp_parser_parameter_declaration_list (parser, &error);
28578 pop_bindings_and_leave_scope ();
28580 if (!cp_parser_error_occurred (parser) && !error)
28581 done = true;
28582 cp_parser_abort_tentative_parse (parser);
28584 parser->in_template_argument_list_p = saved_italp;
28585 break;
28587 /* FALLTHRU */
28588 case CPP_CLOSE_PAREN:
28589 case CPP_ELLIPSIS:
28590 /* If we run into a non-nested `;', `}', or `]',
28591 then the code is invalid -- but the default
28592 argument is certainly over. */
28593 case CPP_SEMICOLON:
28594 case CPP_CLOSE_BRACE:
28595 case CPP_CLOSE_SQUARE:
28596 if (depth == 0
28597 /* Handle correctly int n = sizeof ... ( p ); */
28598 && token->type != CPP_ELLIPSIS)
28599 done = true;
28600 /* Update DEPTH, if necessary. */
28601 else if (token->type == CPP_CLOSE_PAREN
28602 || token->type == CPP_CLOSE_BRACE
28603 || token->type == CPP_CLOSE_SQUARE)
28604 --depth;
28605 break;
28607 case CPP_OPEN_PAREN:
28608 case CPP_OPEN_SQUARE:
28609 case CPP_OPEN_BRACE:
28610 ++depth;
28611 break;
28613 case CPP_LESS:
28614 if (depth == 0)
28615 /* This might be the comparison operator, or it might
28616 start a template argument list. */
28617 ++maybe_template_id;
28618 break;
28620 case CPP_RSHIFT:
28621 if (cxx_dialect == cxx98)
28622 break;
28623 /* Fall through for C++0x, which treats the `>>'
28624 operator like two `>' tokens in certain
28625 cases. */
28626 gcc_fallthrough ();
28628 case CPP_GREATER:
28629 if (depth == 0)
28631 /* This might be an operator, or it might close a
28632 template argument list. But if a previous '<'
28633 started a template argument list, this will have
28634 closed it, so we can't be in one anymore. */
28635 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
28636 if (maybe_template_id < 0)
28637 maybe_template_id = 0;
28639 break;
28641 /* If we run out of tokens, issue an error message. */
28642 case CPP_EOF:
28643 case CPP_PRAGMA_EOL:
28644 error_at (token->location, "file ends in default argument");
28645 return error_mark_node;
28647 case CPP_NAME:
28648 case CPP_SCOPE:
28649 /* In these cases, we should look for template-ids.
28650 For example, if the default argument is
28651 `X<int, double>()', we need to do name lookup to
28652 figure out whether or not `X' is a template; if
28653 so, the `,' does not end the default argument.
28655 That is not yet done. */
28656 break;
28658 default:
28659 break;
28662 /* If we've reached the end, stop. */
28663 if (done)
28664 break;
28666 /* Add the token to the token block. */
28667 token = cp_lexer_consume_token (parser->lexer);
28670 /* Create a DEFAULT_ARG to represent the unparsed default
28671 argument. */
28672 default_argument = make_node (DEFAULT_ARG);
28673 DEFARG_TOKENS (default_argument)
28674 = cp_token_cache_new (first_token, token);
28675 DEFARG_INSTANTIATIONS (default_argument) = NULL;
28677 return default_argument;
28680 /* Begin parsing tentatively. We always save tokens while parsing
28681 tentatively so that if the tentative parsing fails we can restore the
28682 tokens. */
28684 static void
28685 cp_parser_parse_tentatively (cp_parser* parser)
28687 /* Enter a new parsing context. */
28688 parser->context = cp_parser_context_new (parser->context);
28689 /* Begin saving tokens. */
28690 cp_lexer_save_tokens (parser->lexer);
28691 /* In order to avoid repetitive access control error messages,
28692 access checks are queued up until we are no longer parsing
28693 tentatively. */
28694 push_deferring_access_checks (dk_deferred);
28697 /* Commit to the currently active tentative parse. */
28699 static void
28700 cp_parser_commit_to_tentative_parse (cp_parser* parser)
28702 cp_parser_context *context;
28703 cp_lexer *lexer;
28705 /* Mark all of the levels as committed. */
28706 lexer = parser->lexer;
28707 for (context = parser->context; context->next; context = context->next)
28709 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28710 break;
28711 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28712 while (!cp_lexer_saving_tokens (lexer))
28713 lexer = lexer->next;
28714 cp_lexer_commit_tokens (lexer);
28718 /* Commit to the topmost currently active tentative parse.
28720 Note that this function shouldn't be called when there are
28721 irreversible side-effects while in a tentative state. For
28722 example, we shouldn't create a permanent entry in the symbol
28723 table, or issue an error message that might not apply if the
28724 tentative parse is aborted. */
28726 static void
28727 cp_parser_commit_to_topmost_tentative_parse (cp_parser* parser)
28729 cp_parser_context *context = parser->context;
28730 cp_lexer *lexer = parser->lexer;
28732 if (context)
28734 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
28735 return;
28736 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
28738 while (!cp_lexer_saving_tokens (lexer))
28739 lexer = lexer->next;
28740 cp_lexer_commit_tokens (lexer);
28744 /* Abort the currently active tentative parse. All consumed tokens
28745 will be rolled back, and no diagnostics will be issued. */
28747 static void
28748 cp_parser_abort_tentative_parse (cp_parser* parser)
28750 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
28751 || errorcount > 0);
28752 cp_parser_simulate_error (parser);
28753 /* Now, pretend that we want to see if the construct was
28754 successfully parsed. */
28755 cp_parser_parse_definitely (parser);
28758 /* Stop parsing tentatively. If a parse error has occurred, restore the
28759 token stream. Otherwise, commit to the tokens we have consumed.
28760 Returns true if no error occurred; false otherwise. */
28762 static bool
28763 cp_parser_parse_definitely (cp_parser* parser)
28765 bool error_occurred;
28766 cp_parser_context *context;
28768 /* Remember whether or not an error occurred, since we are about to
28769 destroy that information. */
28770 error_occurred = cp_parser_error_occurred (parser);
28771 /* Remove the topmost context from the stack. */
28772 context = parser->context;
28773 parser->context = context->next;
28774 /* If no parse errors occurred, commit to the tentative parse. */
28775 if (!error_occurred)
28777 /* Commit to the tokens read tentatively, unless that was
28778 already done. */
28779 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
28780 cp_lexer_commit_tokens (parser->lexer);
28782 pop_to_parent_deferring_access_checks ();
28784 /* Otherwise, if errors occurred, roll back our state so that things
28785 are just as they were before we began the tentative parse. */
28786 else
28788 cp_lexer_rollback_tokens (parser->lexer);
28789 pop_deferring_access_checks ();
28791 /* Add the context to the front of the free list. */
28792 context->next = cp_parser_context_free_list;
28793 cp_parser_context_free_list = context;
28795 return !error_occurred;
28798 /* Returns true if we are parsing tentatively and are not committed to
28799 this tentative parse. */
28801 static bool
28802 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
28804 return (cp_parser_parsing_tentatively (parser)
28805 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
28808 /* Returns nonzero iff an error has occurred during the most recent
28809 tentative parse. */
28811 static bool
28812 cp_parser_error_occurred (cp_parser* parser)
28814 return (cp_parser_parsing_tentatively (parser)
28815 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
28818 /* Returns nonzero if GNU extensions are allowed. */
28820 static bool
28821 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
28823 return parser->allow_gnu_extensions_p;
28826 /* Objective-C++ Productions */
28829 /* Parse an Objective-C expression, which feeds into a primary-expression
28830 above.
28832 objc-expression:
28833 objc-message-expression
28834 objc-string-literal
28835 objc-encode-expression
28836 objc-protocol-expression
28837 objc-selector-expression
28839 Returns a tree representation of the expression. */
28841 static cp_expr
28842 cp_parser_objc_expression (cp_parser* parser)
28844 /* Try to figure out what kind of declaration is present. */
28845 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
28847 switch (kwd->type)
28849 case CPP_OPEN_SQUARE:
28850 return cp_parser_objc_message_expression (parser);
28852 case CPP_OBJC_STRING:
28853 kwd = cp_lexer_consume_token (parser->lexer);
28854 return objc_build_string_object (kwd->u.value);
28856 case CPP_KEYWORD:
28857 switch (kwd->keyword)
28859 case RID_AT_ENCODE:
28860 return cp_parser_objc_encode_expression (parser);
28862 case RID_AT_PROTOCOL:
28863 return cp_parser_objc_protocol_expression (parser);
28865 case RID_AT_SELECTOR:
28866 return cp_parser_objc_selector_expression (parser);
28868 default:
28869 break;
28871 default:
28872 error_at (kwd->location,
28873 "misplaced %<@%D%> Objective-C++ construct",
28874 kwd->u.value);
28875 cp_parser_skip_to_end_of_block_or_statement (parser);
28878 return error_mark_node;
28881 /* Parse an Objective-C message expression.
28883 objc-message-expression:
28884 [ objc-message-receiver objc-message-args ]
28886 Returns a representation of an Objective-C message. */
28888 static tree
28889 cp_parser_objc_message_expression (cp_parser* parser)
28891 tree receiver, messageargs;
28893 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
28894 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
28895 receiver = cp_parser_objc_message_receiver (parser);
28896 messageargs = cp_parser_objc_message_args (parser);
28897 location_t end_loc = cp_lexer_peek_token (parser->lexer)->location;
28898 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
28900 tree result = objc_build_message_expr (receiver, messageargs);
28902 /* Construct a location e.g.
28903 [self func1:5]
28904 ^~~~~~~~~~~~~~
28905 ranging from the '[' to the ']', with the caret at the start. */
28906 location_t combined_loc = make_location (start_loc, start_loc, end_loc);
28907 protected_set_expr_location (result, combined_loc);
28909 return result;
28912 /* Parse an objc-message-receiver.
28914 objc-message-receiver:
28915 expression
28916 simple-type-specifier
28918 Returns a representation of the type or expression. */
28920 static tree
28921 cp_parser_objc_message_receiver (cp_parser* parser)
28923 tree rcv;
28925 /* An Objective-C message receiver may be either (1) a type
28926 or (2) an expression. */
28927 cp_parser_parse_tentatively (parser);
28928 rcv = cp_parser_expression (parser);
28930 /* If that worked out, fine. */
28931 if (cp_parser_parse_definitely (parser))
28932 return rcv;
28934 cp_parser_parse_tentatively (parser);
28935 rcv = cp_parser_simple_type_specifier (parser,
28936 /*decl_specs=*/NULL,
28937 CP_PARSER_FLAGS_NONE);
28939 if (cp_parser_parse_definitely (parser))
28940 return objc_get_class_reference (rcv);
28942 cp_parser_error (parser, "objective-c++ message receiver expected");
28943 return error_mark_node;
28946 /* Parse the arguments and selectors comprising an Objective-C message.
28948 objc-message-args:
28949 objc-selector
28950 objc-selector-args
28951 objc-selector-args , objc-comma-args
28953 objc-selector-args:
28954 objc-selector [opt] : assignment-expression
28955 objc-selector-args objc-selector [opt] : assignment-expression
28957 objc-comma-args:
28958 assignment-expression
28959 objc-comma-args , assignment-expression
28961 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
28962 selector arguments and TREE_VALUE containing a list of comma
28963 arguments. */
28965 static tree
28966 cp_parser_objc_message_args (cp_parser* parser)
28968 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
28969 bool maybe_unary_selector_p = true;
28970 cp_token *token = cp_lexer_peek_token (parser->lexer);
28972 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
28974 tree selector = NULL_TREE, arg;
28976 if (token->type != CPP_COLON)
28977 selector = cp_parser_objc_selector (parser);
28979 /* Detect if we have a unary selector. */
28980 if (maybe_unary_selector_p
28981 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
28982 return build_tree_list (selector, NULL_TREE);
28984 maybe_unary_selector_p = false;
28985 cp_parser_require (parser, CPP_COLON, RT_COLON);
28986 arg = cp_parser_assignment_expression (parser);
28988 sel_args
28989 = chainon (sel_args,
28990 build_tree_list (selector, arg));
28992 token = cp_lexer_peek_token (parser->lexer);
28995 /* Handle non-selector arguments, if any. */
28996 while (token->type == CPP_COMMA)
28998 tree arg;
29000 cp_lexer_consume_token (parser->lexer);
29001 arg = cp_parser_assignment_expression (parser);
29003 addl_args
29004 = chainon (addl_args,
29005 build_tree_list (NULL_TREE, arg));
29007 token = cp_lexer_peek_token (parser->lexer);
29010 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
29012 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
29013 return build_tree_list (error_mark_node, error_mark_node);
29016 return build_tree_list (sel_args, addl_args);
29019 /* Parse an Objective-C encode expression.
29021 objc-encode-expression:
29022 @encode objc-typename
29024 Returns an encoded representation of the type argument. */
29026 static cp_expr
29027 cp_parser_objc_encode_expression (cp_parser* parser)
29029 tree type;
29030 cp_token *token;
29031 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29033 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
29034 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29035 token = cp_lexer_peek_token (parser->lexer);
29036 type = complete_type (cp_parser_type_id (parser));
29037 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29039 if (!type)
29041 error_at (token->location,
29042 "%<@encode%> must specify a type as an argument");
29043 return error_mark_node;
29046 /* This happens if we find @encode(T) (where T is a template
29047 typename or something dependent on a template typename) when
29048 parsing a template. In that case, we can't compile it
29049 immediately, but we rather create an AT_ENCODE_EXPR which will
29050 need to be instantiated when the template is used.
29052 if (dependent_type_p (type))
29054 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
29055 TREE_READONLY (value) = 1;
29056 return value;
29060 /* Build a location of the form:
29061 @encode(int)
29062 ^~~~~~~~~~~~
29063 with caret==start at the @ token, finishing at the close paren. */
29064 location_t combined_loc
29065 = make_location (start_loc, start_loc,
29066 cp_lexer_previous_token (parser->lexer)->location);
29068 return cp_expr (objc_build_encode_expr (type), combined_loc);
29071 /* Parse an Objective-C @defs expression. */
29073 static tree
29074 cp_parser_objc_defs_expression (cp_parser *parser)
29076 tree name;
29078 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
29079 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29080 name = cp_parser_identifier (parser);
29081 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29083 return objc_get_class_ivars (name);
29086 /* Parse an Objective-C protocol expression.
29088 objc-protocol-expression:
29089 @protocol ( identifier )
29091 Returns a representation of the protocol expression. */
29093 static tree
29094 cp_parser_objc_protocol_expression (cp_parser* parser)
29096 tree proto;
29097 location_t start_loc = cp_lexer_peek_token (parser->lexer)->location;
29099 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29100 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29101 proto = cp_parser_identifier (parser);
29102 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29104 /* Build a location of the form:
29105 @protocol(prot)
29106 ^~~~~~~~~~~~~~~
29107 with caret==start at the @ token, finishing at the close paren. */
29108 location_t combined_loc
29109 = make_location (start_loc, start_loc,
29110 cp_lexer_previous_token (parser->lexer)->location);
29111 tree result = objc_build_protocol_expr (proto);
29112 protected_set_expr_location (result, combined_loc);
29113 return result;
29116 /* Parse an Objective-C selector expression.
29118 objc-selector-expression:
29119 @selector ( objc-method-signature )
29121 objc-method-signature:
29122 objc-selector
29123 objc-selector-seq
29125 objc-selector-seq:
29126 objc-selector :
29127 objc-selector-seq objc-selector :
29129 Returns a representation of the method selector. */
29131 static tree
29132 cp_parser_objc_selector_expression (cp_parser* parser)
29134 tree sel_seq = NULL_TREE;
29135 bool maybe_unary_selector_p = true;
29136 cp_token *token;
29137 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
29139 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
29140 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
29141 token = cp_lexer_peek_token (parser->lexer);
29143 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
29144 || token->type == CPP_SCOPE)
29146 tree selector = NULL_TREE;
29148 if (token->type != CPP_COLON
29149 || token->type == CPP_SCOPE)
29150 selector = cp_parser_objc_selector (parser);
29152 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
29153 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
29155 /* Detect if we have a unary selector. */
29156 if (maybe_unary_selector_p)
29158 sel_seq = selector;
29159 goto finish_selector;
29161 else
29163 cp_parser_error (parser, "expected %<:%>");
29166 maybe_unary_selector_p = false;
29167 token = cp_lexer_consume_token (parser->lexer);
29169 if (token->type == CPP_SCOPE)
29171 sel_seq
29172 = chainon (sel_seq,
29173 build_tree_list (selector, NULL_TREE));
29174 sel_seq
29175 = chainon (sel_seq,
29176 build_tree_list (NULL_TREE, NULL_TREE));
29178 else
29179 sel_seq
29180 = chainon (sel_seq,
29181 build_tree_list (selector, NULL_TREE));
29183 token = cp_lexer_peek_token (parser->lexer);
29186 finish_selector:
29187 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29190 /* Build a location of the form:
29191 @selector(func)
29192 ^~~~~~~~~~~~~~~
29193 with caret==start at the @ token, finishing at the close paren. */
29194 location_t combined_loc
29195 = make_location (loc, loc,
29196 cp_lexer_previous_token (parser->lexer)->location);
29197 tree result = objc_build_selector_expr (combined_loc, sel_seq);
29198 /* TODO: objc_build_selector_expr doesn't always honor the location. */
29199 protected_set_expr_location (result, combined_loc);
29200 return result;
29203 /* Parse a list of identifiers.
29205 objc-identifier-list:
29206 identifier
29207 objc-identifier-list , identifier
29209 Returns a TREE_LIST of identifier nodes. */
29211 static tree
29212 cp_parser_objc_identifier_list (cp_parser* parser)
29214 tree identifier;
29215 tree list;
29216 cp_token *sep;
29218 identifier = cp_parser_identifier (parser);
29219 if (identifier == error_mark_node)
29220 return error_mark_node;
29222 list = build_tree_list (NULL_TREE, identifier);
29223 sep = cp_lexer_peek_token (parser->lexer);
29225 while (sep->type == CPP_COMMA)
29227 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29228 identifier = cp_parser_identifier (parser);
29229 if (identifier == error_mark_node)
29230 return list;
29232 list = chainon (list, build_tree_list (NULL_TREE,
29233 identifier));
29234 sep = cp_lexer_peek_token (parser->lexer);
29237 return list;
29240 /* Parse an Objective-C alias declaration.
29242 objc-alias-declaration:
29243 @compatibility_alias identifier identifier ;
29245 This function registers the alias mapping with the Objective-C front end.
29246 It returns nothing. */
29248 static void
29249 cp_parser_objc_alias_declaration (cp_parser* parser)
29251 tree alias, orig;
29253 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
29254 alias = cp_parser_identifier (parser);
29255 orig = cp_parser_identifier (parser);
29256 objc_declare_alias (alias, orig);
29257 cp_parser_consume_semicolon_at_end_of_statement (parser);
29260 /* Parse an Objective-C class forward-declaration.
29262 objc-class-declaration:
29263 @class objc-identifier-list ;
29265 The function registers the forward declarations with the Objective-C
29266 front end. It returns nothing. */
29268 static void
29269 cp_parser_objc_class_declaration (cp_parser* parser)
29271 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
29272 while (true)
29274 tree id;
29276 id = cp_parser_identifier (parser);
29277 if (id == error_mark_node)
29278 break;
29280 objc_declare_class (id);
29282 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29283 cp_lexer_consume_token (parser->lexer);
29284 else
29285 break;
29287 cp_parser_consume_semicolon_at_end_of_statement (parser);
29290 /* Parse a list of Objective-C protocol references.
29292 objc-protocol-refs-opt:
29293 objc-protocol-refs [opt]
29295 objc-protocol-refs:
29296 < objc-identifier-list >
29298 Returns a TREE_LIST of identifiers, if any. */
29300 static tree
29301 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
29303 tree protorefs = NULL_TREE;
29305 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
29307 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
29308 protorefs = cp_parser_objc_identifier_list (parser);
29309 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
29312 return protorefs;
29315 /* Parse a Objective-C visibility specification. */
29317 static void
29318 cp_parser_objc_visibility_spec (cp_parser* parser)
29320 cp_token *vis = cp_lexer_peek_token (parser->lexer);
29322 switch (vis->keyword)
29324 case RID_AT_PRIVATE:
29325 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
29326 break;
29327 case RID_AT_PROTECTED:
29328 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
29329 break;
29330 case RID_AT_PUBLIC:
29331 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
29332 break;
29333 case RID_AT_PACKAGE:
29334 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
29335 break;
29336 default:
29337 return;
29340 /* Eat '@private'/'@protected'/'@public'. */
29341 cp_lexer_consume_token (parser->lexer);
29344 /* Parse an Objective-C method type. Return 'true' if it is a class
29345 (+) method, and 'false' if it is an instance (-) method. */
29347 static inline bool
29348 cp_parser_objc_method_type (cp_parser* parser)
29350 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
29351 return true;
29352 else
29353 return false;
29356 /* Parse an Objective-C protocol qualifier. */
29358 static tree
29359 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
29361 tree quals = NULL_TREE, node;
29362 cp_token *token = cp_lexer_peek_token (parser->lexer);
29364 node = token->u.value;
29366 while (node && identifier_p (node)
29367 && (node == ridpointers [(int) RID_IN]
29368 || node == ridpointers [(int) RID_OUT]
29369 || node == ridpointers [(int) RID_INOUT]
29370 || node == ridpointers [(int) RID_BYCOPY]
29371 || node == ridpointers [(int) RID_BYREF]
29372 || node == ridpointers [(int) RID_ONEWAY]))
29374 quals = tree_cons (NULL_TREE, node, quals);
29375 cp_lexer_consume_token (parser->lexer);
29376 token = cp_lexer_peek_token (parser->lexer);
29377 node = token->u.value;
29380 return quals;
29383 /* Parse an Objective-C typename. */
29385 static tree
29386 cp_parser_objc_typename (cp_parser* parser)
29388 tree type_name = NULL_TREE;
29390 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
29392 tree proto_quals, cp_type = NULL_TREE;
29394 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
29395 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
29397 /* An ObjC type name may consist of just protocol qualifiers, in which
29398 case the type shall default to 'id'. */
29399 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
29401 cp_type = cp_parser_type_id (parser);
29403 /* If the type could not be parsed, an error has already
29404 been produced. For error recovery, behave as if it had
29405 not been specified, which will use the default type
29406 'id'. */
29407 if (cp_type == error_mark_node)
29409 cp_type = NULL_TREE;
29410 /* We need to skip to the closing parenthesis as
29411 cp_parser_type_id() does not seem to do it for
29412 us. */
29413 cp_parser_skip_to_closing_parenthesis (parser,
29414 /*recovering=*/true,
29415 /*or_comma=*/false,
29416 /*consume_paren=*/false);
29420 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
29421 type_name = build_tree_list (proto_quals, cp_type);
29424 return type_name;
29427 /* Check to see if TYPE refers to an Objective-C selector name. */
29429 static bool
29430 cp_parser_objc_selector_p (enum cpp_ttype type)
29432 return (type == CPP_NAME || type == CPP_KEYWORD
29433 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
29434 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
29435 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
29436 || type == CPP_XOR || type == CPP_XOR_EQ);
29439 /* Parse an Objective-C selector. */
29441 static tree
29442 cp_parser_objc_selector (cp_parser* parser)
29444 cp_token *token = cp_lexer_consume_token (parser->lexer);
29446 if (!cp_parser_objc_selector_p (token->type))
29448 error_at (token->location, "invalid Objective-C++ selector name");
29449 return error_mark_node;
29452 /* C++ operator names are allowed to appear in ObjC selectors. */
29453 switch (token->type)
29455 case CPP_AND_AND: return get_identifier ("and");
29456 case CPP_AND_EQ: return get_identifier ("and_eq");
29457 case CPP_AND: return get_identifier ("bitand");
29458 case CPP_OR: return get_identifier ("bitor");
29459 case CPP_COMPL: return get_identifier ("compl");
29460 case CPP_NOT: return get_identifier ("not");
29461 case CPP_NOT_EQ: return get_identifier ("not_eq");
29462 case CPP_OR_OR: return get_identifier ("or");
29463 case CPP_OR_EQ: return get_identifier ("or_eq");
29464 case CPP_XOR: return get_identifier ("xor");
29465 case CPP_XOR_EQ: return get_identifier ("xor_eq");
29466 default: return token->u.value;
29470 /* Parse an Objective-C params list. */
29472 static tree
29473 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
29475 tree params = NULL_TREE;
29476 bool maybe_unary_selector_p = true;
29477 cp_token *token = cp_lexer_peek_token (parser->lexer);
29479 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
29481 tree selector = NULL_TREE, type_name, identifier;
29482 tree parm_attr = NULL_TREE;
29484 if (token->keyword == RID_ATTRIBUTE)
29485 break;
29487 if (token->type != CPP_COLON)
29488 selector = cp_parser_objc_selector (parser);
29490 /* Detect if we have a unary selector. */
29491 if (maybe_unary_selector_p
29492 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
29494 params = selector; /* Might be followed by attributes. */
29495 break;
29498 maybe_unary_selector_p = false;
29499 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
29501 /* Something went quite wrong. There should be a colon
29502 here, but there is not. Stop parsing parameters. */
29503 break;
29505 type_name = cp_parser_objc_typename (parser);
29506 /* New ObjC allows attributes on parameters too. */
29507 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
29508 parm_attr = cp_parser_attributes_opt (parser);
29509 identifier = cp_parser_identifier (parser);
29511 params
29512 = chainon (params,
29513 objc_build_keyword_decl (selector,
29514 type_name,
29515 identifier,
29516 parm_attr));
29518 token = cp_lexer_peek_token (parser->lexer);
29521 if (params == NULL_TREE)
29523 cp_parser_error (parser, "objective-c++ method declaration is expected");
29524 return error_mark_node;
29527 /* We allow tail attributes for the method. */
29528 if (token->keyword == RID_ATTRIBUTE)
29530 *attributes = cp_parser_attributes_opt (parser);
29531 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29532 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29533 return params;
29534 cp_parser_error (parser,
29535 "method attributes must be specified at the end");
29536 return error_mark_node;
29539 if (params == NULL_TREE)
29541 cp_parser_error (parser, "objective-c++ method declaration is expected");
29542 return error_mark_node;
29544 return params;
29547 /* Parse the non-keyword Objective-C params. */
29549 static tree
29550 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
29551 tree* attributes)
29553 tree params = make_node (TREE_LIST);
29554 cp_token *token = cp_lexer_peek_token (parser->lexer);
29555 *ellipsisp = false; /* Initially, assume no ellipsis. */
29557 while (token->type == CPP_COMMA)
29559 cp_parameter_declarator *parmdecl;
29560 tree parm;
29562 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29563 token = cp_lexer_peek_token (parser->lexer);
29565 if (token->type == CPP_ELLIPSIS)
29567 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
29568 *ellipsisp = true;
29569 token = cp_lexer_peek_token (parser->lexer);
29570 break;
29573 /* TODO: parse attributes for tail parameters. */
29574 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
29575 parm = grokdeclarator (parmdecl->declarator,
29576 &parmdecl->decl_specifiers,
29577 PARM, /*initialized=*/0,
29578 /*attrlist=*/NULL);
29580 chainon (params, build_tree_list (NULL_TREE, parm));
29581 token = cp_lexer_peek_token (parser->lexer);
29584 /* We allow tail attributes for the method. */
29585 if (token->keyword == RID_ATTRIBUTE)
29587 if (*attributes == NULL_TREE)
29589 *attributes = cp_parser_attributes_opt (parser);
29590 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
29591 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
29592 return params;
29594 else
29595 /* We have an error, but parse the attributes, so that we can
29596 carry on. */
29597 *attributes = cp_parser_attributes_opt (parser);
29599 cp_parser_error (parser,
29600 "method attributes must be specified at the end");
29601 return error_mark_node;
29604 return params;
29607 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
29609 static void
29610 cp_parser_objc_interstitial_code (cp_parser* parser)
29612 cp_token *token = cp_lexer_peek_token (parser->lexer);
29614 /* If the next token is `extern' and the following token is a string
29615 literal, then we have a linkage specification. */
29616 if (token->keyword == RID_EXTERN
29617 && cp_parser_is_pure_string_literal
29618 (cp_lexer_peek_nth_token (parser->lexer, 2)))
29619 cp_parser_linkage_specification (parser);
29620 /* Handle #pragma, if any. */
29621 else if (token->type == CPP_PRAGMA)
29622 cp_parser_pragma (parser, pragma_objc_icode, NULL);
29623 /* Allow stray semicolons. */
29624 else if (token->type == CPP_SEMICOLON)
29625 cp_lexer_consume_token (parser->lexer);
29626 /* Mark methods as optional or required, when building protocols. */
29627 else if (token->keyword == RID_AT_OPTIONAL)
29629 cp_lexer_consume_token (parser->lexer);
29630 objc_set_method_opt (true);
29632 else if (token->keyword == RID_AT_REQUIRED)
29634 cp_lexer_consume_token (parser->lexer);
29635 objc_set_method_opt (false);
29637 else if (token->keyword == RID_NAMESPACE)
29638 cp_parser_namespace_definition (parser);
29639 /* Other stray characters must generate errors. */
29640 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
29642 cp_lexer_consume_token (parser->lexer);
29643 error ("stray %qs between Objective-C++ methods",
29644 token->type == CPP_OPEN_BRACE ? "{" : "}");
29646 /* Finally, try to parse a block-declaration, or a function-definition. */
29647 else
29648 cp_parser_block_declaration (parser, /*statement_p=*/false);
29651 /* Parse a method signature. */
29653 static tree
29654 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
29656 tree rettype, kwdparms, optparms;
29657 bool ellipsis = false;
29658 bool is_class_method;
29660 is_class_method = cp_parser_objc_method_type (parser);
29661 rettype = cp_parser_objc_typename (parser);
29662 *attributes = NULL_TREE;
29663 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
29664 if (kwdparms == error_mark_node)
29665 return error_mark_node;
29666 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
29667 if (optparms == error_mark_node)
29668 return error_mark_node;
29670 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
29673 static bool
29674 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
29676 tree tattr;
29677 cp_lexer_save_tokens (parser->lexer);
29678 tattr = cp_parser_attributes_opt (parser);
29679 gcc_assert (tattr) ;
29681 /* If the attributes are followed by a method introducer, this is not allowed.
29682 Dump the attributes and flag the situation. */
29683 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
29684 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
29685 return true;
29687 /* Otherwise, the attributes introduce some interstitial code, possibly so
29688 rewind to allow that check. */
29689 cp_lexer_rollback_tokens (parser->lexer);
29690 return false;
29693 /* Parse an Objective-C method prototype list. */
29695 static void
29696 cp_parser_objc_method_prototype_list (cp_parser* parser)
29698 cp_token *token = cp_lexer_peek_token (parser->lexer);
29700 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29702 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29704 tree attributes, sig;
29705 bool is_class_method;
29706 if (token->type == CPP_PLUS)
29707 is_class_method = true;
29708 else
29709 is_class_method = false;
29710 sig = cp_parser_objc_method_signature (parser, &attributes);
29711 if (sig == error_mark_node)
29713 cp_parser_skip_to_end_of_block_or_statement (parser);
29714 token = cp_lexer_peek_token (parser->lexer);
29715 continue;
29717 objc_add_method_declaration (is_class_method, sig, attributes);
29718 cp_parser_consume_semicolon_at_end_of_statement (parser);
29720 else if (token->keyword == RID_AT_PROPERTY)
29721 cp_parser_objc_at_property_declaration (parser);
29722 else if (token->keyword == RID_ATTRIBUTE
29723 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29724 warning_at (cp_lexer_peek_token (parser->lexer)->location,
29725 OPT_Wattributes,
29726 "prefix attributes are ignored for methods");
29727 else
29728 /* Allow for interspersed non-ObjC++ code. */
29729 cp_parser_objc_interstitial_code (parser);
29731 token = cp_lexer_peek_token (parser->lexer);
29734 if (token->type != CPP_EOF)
29735 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29736 else
29737 cp_parser_error (parser, "expected %<@end%>");
29739 objc_finish_interface ();
29742 /* Parse an Objective-C method definition list. */
29744 static void
29745 cp_parser_objc_method_definition_list (cp_parser* parser)
29747 cp_token *token = cp_lexer_peek_token (parser->lexer);
29749 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
29751 tree meth;
29753 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
29755 cp_token *ptk;
29756 tree sig, attribute;
29757 bool is_class_method;
29758 if (token->type == CPP_PLUS)
29759 is_class_method = true;
29760 else
29761 is_class_method = false;
29762 push_deferring_access_checks (dk_deferred);
29763 sig = cp_parser_objc_method_signature (parser, &attribute);
29764 if (sig == error_mark_node)
29766 cp_parser_skip_to_end_of_block_or_statement (parser);
29767 token = cp_lexer_peek_token (parser->lexer);
29768 continue;
29770 objc_start_method_definition (is_class_method, sig, attribute,
29771 NULL_TREE);
29773 /* For historical reasons, we accept an optional semicolon. */
29774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29775 cp_lexer_consume_token (parser->lexer);
29777 ptk = cp_lexer_peek_token (parser->lexer);
29778 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
29779 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
29781 perform_deferred_access_checks (tf_warning_or_error);
29782 stop_deferring_access_checks ();
29783 meth = cp_parser_function_definition_after_declarator (parser,
29784 false);
29785 pop_deferring_access_checks ();
29786 objc_finish_method_definition (meth);
29789 /* The following case will be removed once @synthesize is
29790 completely implemented. */
29791 else if (token->keyword == RID_AT_PROPERTY)
29792 cp_parser_objc_at_property_declaration (parser);
29793 else if (token->keyword == RID_AT_SYNTHESIZE)
29794 cp_parser_objc_at_synthesize_declaration (parser);
29795 else if (token->keyword == RID_AT_DYNAMIC)
29796 cp_parser_objc_at_dynamic_declaration (parser);
29797 else if (token->keyword == RID_ATTRIBUTE
29798 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
29799 warning_at (token->location, OPT_Wattributes,
29800 "prefix attributes are ignored for methods");
29801 else
29802 /* Allow for interspersed non-ObjC++ code. */
29803 cp_parser_objc_interstitial_code (parser);
29805 token = cp_lexer_peek_token (parser->lexer);
29808 if (token->type != CPP_EOF)
29809 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
29810 else
29811 cp_parser_error (parser, "expected %<@end%>");
29813 objc_finish_implementation ();
29816 /* Parse Objective-C ivars. */
29818 static void
29819 cp_parser_objc_class_ivars (cp_parser* parser)
29821 cp_token *token = cp_lexer_peek_token (parser->lexer);
29823 if (token->type != CPP_OPEN_BRACE)
29824 return; /* No ivars specified. */
29826 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
29827 token = cp_lexer_peek_token (parser->lexer);
29829 while (token->type != CPP_CLOSE_BRACE
29830 && token->keyword != RID_AT_END && token->type != CPP_EOF)
29832 cp_decl_specifier_seq declspecs;
29833 int decl_class_or_enum_p;
29834 tree prefix_attributes;
29836 cp_parser_objc_visibility_spec (parser);
29838 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
29839 break;
29841 cp_parser_decl_specifier_seq (parser,
29842 CP_PARSER_FLAGS_OPTIONAL,
29843 &declspecs,
29844 &decl_class_or_enum_p);
29846 /* auto, register, static, extern, mutable. */
29847 if (declspecs.storage_class != sc_none)
29849 cp_parser_error (parser, "invalid type for instance variable");
29850 declspecs.storage_class = sc_none;
29853 /* thread_local. */
29854 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
29856 cp_parser_error (parser, "invalid type for instance variable");
29857 declspecs.locations[ds_thread] = 0;
29860 /* typedef. */
29861 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
29863 cp_parser_error (parser, "invalid type for instance variable");
29864 declspecs.locations[ds_typedef] = 0;
29867 prefix_attributes = declspecs.attributes;
29868 declspecs.attributes = NULL_TREE;
29870 /* Keep going until we hit the `;' at the end of the
29871 declaration. */
29872 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
29874 tree width = NULL_TREE, attributes, first_attribute, decl;
29875 cp_declarator *declarator = NULL;
29876 int ctor_dtor_or_conv_p;
29878 /* Check for a (possibly unnamed) bitfield declaration. */
29879 token = cp_lexer_peek_token (parser->lexer);
29880 if (token->type == CPP_COLON)
29881 goto eat_colon;
29883 if (token->type == CPP_NAME
29884 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
29885 == CPP_COLON))
29887 /* Get the name of the bitfield. */
29888 declarator = make_id_declarator (NULL_TREE,
29889 cp_parser_identifier (parser),
29890 sfk_none);
29892 eat_colon:
29893 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
29894 /* Get the width of the bitfield. */
29895 width
29896 = cp_parser_constant_expression (parser);
29898 else
29900 /* Parse the declarator. */
29901 declarator
29902 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
29903 &ctor_dtor_or_conv_p,
29904 /*parenthesized_p=*/NULL,
29905 /*member_p=*/false,
29906 /*friend_p=*/false);
29909 /* Look for attributes that apply to the ivar. */
29910 attributes = cp_parser_attributes_opt (parser);
29911 /* Remember which attributes are prefix attributes and
29912 which are not. */
29913 first_attribute = attributes;
29914 /* Combine the attributes. */
29915 attributes = chainon (prefix_attributes, attributes);
29917 if (width)
29918 /* Create the bitfield declaration. */
29919 decl = grokbitfield (declarator, &declspecs,
29920 width,
29921 attributes);
29922 else
29923 decl = grokfield (declarator, &declspecs,
29924 NULL_TREE, /*init_const_expr_p=*/false,
29925 NULL_TREE, attributes);
29927 /* Add the instance variable. */
29928 if (decl != error_mark_node && decl != NULL_TREE)
29929 objc_add_instance_variable (decl);
29931 /* Reset PREFIX_ATTRIBUTES. */
29932 while (attributes && TREE_CHAIN (attributes) != first_attribute)
29933 attributes = TREE_CHAIN (attributes);
29934 if (attributes)
29935 TREE_CHAIN (attributes) = NULL_TREE;
29937 token = cp_lexer_peek_token (parser->lexer);
29939 if (token->type == CPP_COMMA)
29941 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
29942 continue;
29944 break;
29947 cp_parser_consume_semicolon_at_end_of_statement (parser);
29948 token = cp_lexer_peek_token (parser->lexer);
29951 if (token->keyword == RID_AT_END)
29952 cp_parser_error (parser, "expected %<}%>");
29954 /* Do not consume the RID_AT_END, so it will be read again as terminating
29955 the @interface of @implementation. */
29956 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
29957 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
29959 /* For historical reasons, we accept an optional semicolon. */
29960 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
29961 cp_lexer_consume_token (parser->lexer);
29964 /* Parse an Objective-C protocol declaration. */
29966 static void
29967 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
29969 tree proto, protorefs;
29970 cp_token *tok;
29972 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
29973 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
29975 tok = cp_lexer_peek_token (parser->lexer);
29976 error_at (tok->location, "identifier expected after %<@protocol%>");
29977 cp_parser_consume_semicolon_at_end_of_statement (parser);
29978 return;
29981 /* See if we have a forward declaration or a definition. */
29982 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
29984 /* Try a forward declaration first. */
29985 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
29987 while (true)
29989 tree id;
29991 id = cp_parser_identifier (parser);
29992 if (id == error_mark_node)
29993 break;
29995 objc_declare_protocol (id, attributes);
29997 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
29998 cp_lexer_consume_token (parser->lexer);
29999 else
30000 break;
30002 cp_parser_consume_semicolon_at_end_of_statement (parser);
30005 /* Ok, we got a full-fledged definition (or at least should). */
30006 else
30008 proto = cp_parser_identifier (parser);
30009 protorefs = cp_parser_objc_protocol_refs_opt (parser);
30010 objc_start_protocol (proto, protorefs, attributes);
30011 cp_parser_objc_method_prototype_list (parser);
30015 /* Parse an Objective-C superclass or category. */
30017 static void
30018 cp_parser_objc_superclass_or_category (cp_parser *parser,
30019 bool iface_p,
30020 tree *super,
30021 tree *categ, bool *is_class_extension)
30023 cp_token *next = cp_lexer_peek_token (parser->lexer);
30025 *super = *categ = NULL_TREE;
30026 *is_class_extension = false;
30027 if (next->type == CPP_COLON)
30029 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
30030 *super = cp_parser_identifier (parser);
30032 else if (next->type == CPP_OPEN_PAREN)
30034 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
30036 /* If there is no category name, and this is an @interface, we
30037 have a class extension. */
30038 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30040 *categ = NULL_TREE;
30041 *is_class_extension = true;
30043 else
30044 *categ = cp_parser_identifier (parser);
30046 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30050 /* Parse an Objective-C class interface. */
30052 static void
30053 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
30055 tree name, super, categ, protos;
30056 bool is_class_extension;
30058 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
30059 name = cp_parser_identifier (parser);
30060 if (name == error_mark_node)
30062 /* It's hard to recover because even if valid @interface stuff
30063 is to follow, we can't compile it (or validate it) if we
30064 don't even know which class it refers to. Let's assume this
30065 was a stray '@interface' token in the stream and skip it.
30067 return;
30069 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
30070 &is_class_extension);
30071 protos = cp_parser_objc_protocol_refs_opt (parser);
30073 /* We have either a class or a category on our hands. */
30074 if (categ || is_class_extension)
30075 objc_start_category_interface (name, categ, protos, attributes);
30076 else
30078 objc_start_class_interface (name, super, protos, attributes);
30079 /* Handle instance variable declarations, if any. */
30080 cp_parser_objc_class_ivars (parser);
30081 objc_continue_interface ();
30084 cp_parser_objc_method_prototype_list (parser);
30087 /* Parse an Objective-C class implementation. */
30089 static void
30090 cp_parser_objc_class_implementation (cp_parser* parser)
30092 tree name, super, categ;
30093 bool is_class_extension;
30095 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
30096 name = cp_parser_identifier (parser);
30097 if (name == error_mark_node)
30099 /* It's hard to recover because even if valid @implementation
30100 stuff is to follow, we can't compile it (or validate it) if
30101 we don't even know which class it refers to. Let's assume
30102 this was a stray '@implementation' token in the stream and
30103 skip it.
30105 return;
30107 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
30108 &is_class_extension);
30110 /* We have either a class or a category on our hands. */
30111 if (categ)
30112 objc_start_category_implementation (name, categ);
30113 else
30115 objc_start_class_implementation (name, super);
30116 /* Handle instance variable declarations, if any. */
30117 cp_parser_objc_class_ivars (parser);
30118 objc_continue_implementation ();
30121 cp_parser_objc_method_definition_list (parser);
30124 /* Consume the @end token and finish off the implementation. */
30126 static void
30127 cp_parser_objc_end_implementation (cp_parser* parser)
30129 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
30130 objc_finish_implementation ();
30133 /* Parse an Objective-C declaration. */
30135 static void
30136 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
30138 /* Try to figure out what kind of declaration is present. */
30139 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30141 if (attributes)
30142 switch (kwd->keyword)
30144 case RID_AT_ALIAS:
30145 case RID_AT_CLASS:
30146 case RID_AT_END:
30147 error_at (kwd->location, "attributes may not be specified before"
30148 " the %<@%D%> Objective-C++ keyword",
30149 kwd->u.value);
30150 attributes = NULL;
30151 break;
30152 case RID_AT_IMPLEMENTATION:
30153 warning_at (kwd->location, OPT_Wattributes,
30154 "prefix attributes are ignored before %<@%D%>",
30155 kwd->u.value);
30156 attributes = NULL;
30157 default:
30158 break;
30161 switch (kwd->keyword)
30163 case RID_AT_ALIAS:
30164 cp_parser_objc_alias_declaration (parser);
30165 break;
30166 case RID_AT_CLASS:
30167 cp_parser_objc_class_declaration (parser);
30168 break;
30169 case RID_AT_PROTOCOL:
30170 cp_parser_objc_protocol_declaration (parser, attributes);
30171 break;
30172 case RID_AT_INTERFACE:
30173 cp_parser_objc_class_interface (parser, attributes);
30174 break;
30175 case RID_AT_IMPLEMENTATION:
30176 cp_parser_objc_class_implementation (parser);
30177 break;
30178 case RID_AT_END:
30179 cp_parser_objc_end_implementation (parser);
30180 break;
30181 default:
30182 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30183 kwd->u.value);
30184 cp_parser_skip_to_end_of_block_or_statement (parser);
30188 /* Parse an Objective-C try-catch-finally statement.
30190 objc-try-catch-finally-stmt:
30191 @try compound-statement objc-catch-clause-seq [opt]
30192 objc-finally-clause [opt]
30194 objc-catch-clause-seq:
30195 objc-catch-clause objc-catch-clause-seq [opt]
30197 objc-catch-clause:
30198 @catch ( objc-exception-declaration ) compound-statement
30200 objc-finally-clause:
30201 @finally compound-statement
30203 objc-exception-declaration:
30204 parameter-declaration
30205 '...'
30207 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
30209 Returns NULL_TREE.
30211 PS: This function is identical to c_parser_objc_try_catch_finally_statement
30212 for C. Keep them in sync. */
30214 static tree
30215 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
30217 location_t location;
30218 tree stmt;
30220 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
30221 location = cp_lexer_peek_token (parser->lexer)->location;
30222 objc_maybe_warn_exceptions (location);
30223 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
30224 node, lest it get absorbed into the surrounding block. */
30225 stmt = push_stmt_list ();
30226 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30227 objc_begin_try_stmt (location, pop_stmt_list (stmt));
30229 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
30231 cp_parameter_declarator *parm;
30232 tree parameter_declaration = error_mark_node;
30233 bool seen_open_paren = false;
30235 cp_lexer_consume_token (parser->lexer);
30236 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
30237 seen_open_paren = true;
30238 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
30240 /* We have "@catch (...)" (where the '...' are literally
30241 what is in the code). Skip the '...'.
30242 parameter_declaration is set to NULL_TREE, and
30243 objc_being_catch_clauses() knows that that means
30244 '...'. */
30245 cp_lexer_consume_token (parser->lexer);
30246 parameter_declaration = NULL_TREE;
30248 else
30250 /* We have "@catch (NSException *exception)" or something
30251 like that. Parse the parameter declaration. */
30252 parm = cp_parser_parameter_declaration (parser, false, NULL);
30253 if (parm == NULL)
30254 parameter_declaration = error_mark_node;
30255 else
30256 parameter_declaration = grokdeclarator (parm->declarator,
30257 &parm->decl_specifiers,
30258 PARM, /*initialized=*/0,
30259 /*attrlist=*/NULL);
30261 if (seen_open_paren)
30262 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30263 else
30265 /* If there was no open parenthesis, we are recovering from
30266 an error, and we are trying to figure out what mistake
30267 the user has made. */
30269 /* If there is an immediate closing parenthesis, the user
30270 probably forgot the opening one (ie, they typed "@catch
30271 NSException *e)". Parse the closing parenthesis and keep
30272 going. */
30273 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
30274 cp_lexer_consume_token (parser->lexer);
30276 /* If these is no immediate closing parenthesis, the user
30277 probably doesn't know that parenthesis are required at
30278 all (ie, they typed "@catch NSException *e"). So, just
30279 forget about the closing parenthesis and keep going. */
30281 objc_begin_catch_clause (parameter_declaration);
30282 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30283 objc_finish_catch_clause ();
30285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
30287 cp_lexer_consume_token (parser->lexer);
30288 location = cp_lexer_peek_token (parser->lexer)->location;
30289 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
30290 node, lest it get absorbed into the surrounding block. */
30291 stmt = push_stmt_list ();
30292 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30293 objc_build_finally_clause (location, pop_stmt_list (stmt));
30296 return objc_finish_try_stmt ();
30299 /* Parse an Objective-C synchronized statement.
30301 objc-synchronized-stmt:
30302 @synchronized ( expression ) compound-statement
30304 Returns NULL_TREE. */
30306 static tree
30307 cp_parser_objc_synchronized_statement (cp_parser *parser)
30309 location_t location;
30310 tree lock, stmt;
30312 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
30314 location = cp_lexer_peek_token (parser->lexer)->location;
30315 objc_maybe_warn_exceptions (location);
30316 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
30317 lock = cp_parser_expression (parser);
30318 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
30320 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
30321 node, lest it get absorbed into the surrounding block. */
30322 stmt = push_stmt_list ();
30323 cp_parser_compound_statement (parser, NULL, BCS_NORMAL, false);
30325 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
30328 /* Parse an Objective-C throw statement.
30330 objc-throw-stmt:
30331 @throw assignment-expression [opt] ;
30333 Returns a constructed '@throw' statement. */
30335 static tree
30336 cp_parser_objc_throw_statement (cp_parser *parser)
30338 tree expr = NULL_TREE;
30339 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
30341 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
30343 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30344 expr = cp_parser_expression (parser);
30346 cp_parser_consume_semicolon_at_end_of_statement (parser);
30348 return objc_build_throw_stmt (loc, expr);
30351 /* Parse an Objective-C statement. */
30353 static tree
30354 cp_parser_objc_statement (cp_parser * parser)
30356 /* Try to figure out what kind of declaration is present. */
30357 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
30359 switch (kwd->keyword)
30361 case RID_AT_TRY:
30362 return cp_parser_objc_try_catch_finally_statement (parser);
30363 case RID_AT_SYNCHRONIZED:
30364 return cp_parser_objc_synchronized_statement (parser);
30365 case RID_AT_THROW:
30366 return cp_parser_objc_throw_statement (parser);
30367 default:
30368 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
30369 kwd->u.value);
30370 cp_parser_skip_to_end_of_block_or_statement (parser);
30373 return error_mark_node;
30376 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
30377 look ahead to see if an objc keyword follows the attributes. This
30378 is to detect the use of prefix attributes on ObjC @interface and
30379 @protocol. */
30381 static bool
30382 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
30384 cp_lexer_save_tokens (parser->lexer);
30385 *attrib = cp_parser_attributes_opt (parser);
30386 gcc_assert (*attrib);
30387 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
30389 cp_lexer_commit_tokens (parser->lexer);
30390 return true;
30392 cp_lexer_rollback_tokens (parser->lexer);
30393 return false;
30396 /* This routine is a minimal replacement for
30397 c_parser_struct_declaration () used when parsing the list of
30398 types/names or ObjC++ properties. For example, when parsing the
30399 code
30401 @property (readonly) int a, b, c;
30403 this function is responsible for parsing "int a, int b, int c" and
30404 returning the declarations as CHAIN of DECLs.
30406 TODO: Share this code with cp_parser_objc_class_ivars. It's very
30407 similar parsing. */
30408 static tree
30409 cp_parser_objc_struct_declaration (cp_parser *parser)
30411 tree decls = NULL_TREE;
30412 cp_decl_specifier_seq declspecs;
30413 int decl_class_or_enum_p;
30414 tree prefix_attributes;
30416 cp_parser_decl_specifier_seq (parser,
30417 CP_PARSER_FLAGS_NONE,
30418 &declspecs,
30419 &decl_class_or_enum_p);
30421 if (declspecs.type == error_mark_node)
30422 return error_mark_node;
30424 /* auto, register, static, extern, mutable. */
30425 if (declspecs.storage_class != sc_none)
30427 cp_parser_error (parser, "invalid type for property");
30428 declspecs.storage_class = sc_none;
30431 /* thread_local. */
30432 if (decl_spec_seq_has_spec_p (&declspecs, ds_thread))
30434 cp_parser_error (parser, "invalid type for property");
30435 declspecs.locations[ds_thread] = 0;
30438 /* typedef. */
30439 if (decl_spec_seq_has_spec_p (&declspecs, ds_typedef))
30441 cp_parser_error (parser, "invalid type for property");
30442 declspecs.locations[ds_typedef] = 0;
30445 prefix_attributes = declspecs.attributes;
30446 declspecs.attributes = NULL_TREE;
30448 /* Keep going until we hit the `;' at the end of the declaration. */
30449 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
30451 tree attributes, first_attribute, decl;
30452 cp_declarator *declarator;
30453 cp_token *token;
30455 /* Parse the declarator. */
30456 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
30457 NULL, NULL, false, false);
30459 /* Look for attributes that apply to the ivar. */
30460 attributes = cp_parser_attributes_opt (parser);
30461 /* Remember which attributes are prefix attributes and
30462 which are not. */
30463 first_attribute = attributes;
30464 /* Combine the attributes. */
30465 attributes = chainon (prefix_attributes, attributes);
30467 decl = grokfield (declarator, &declspecs,
30468 NULL_TREE, /*init_const_expr_p=*/false,
30469 NULL_TREE, attributes);
30471 if (decl == error_mark_node || decl == NULL_TREE)
30472 return error_mark_node;
30474 /* Reset PREFIX_ATTRIBUTES. */
30475 while (attributes && TREE_CHAIN (attributes) != first_attribute)
30476 attributes = TREE_CHAIN (attributes);
30477 if (attributes)
30478 TREE_CHAIN (attributes) = NULL_TREE;
30480 DECL_CHAIN (decl) = decls;
30481 decls = decl;
30483 token = cp_lexer_peek_token (parser->lexer);
30484 if (token->type == CPP_COMMA)
30486 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
30487 continue;
30489 else
30490 break;
30492 return decls;
30495 /* Parse an Objective-C @property declaration. The syntax is:
30497 objc-property-declaration:
30498 '@property' objc-property-attributes[opt] struct-declaration ;
30500 objc-property-attributes:
30501 '(' objc-property-attribute-list ')'
30503 objc-property-attribute-list:
30504 objc-property-attribute
30505 objc-property-attribute-list, objc-property-attribute
30507 objc-property-attribute
30508 'getter' = identifier
30509 'setter' = identifier
30510 'readonly'
30511 'readwrite'
30512 'assign'
30513 'retain'
30514 'copy'
30515 'nonatomic'
30517 For example:
30518 @property NSString *name;
30519 @property (readonly) id object;
30520 @property (retain, nonatomic, getter=getTheName) id name;
30521 @property int a, b, c;
30523 PS: This function is identical to
30524 c_parser_objc_at_property_declaration for C. Keep them in sync. */
30525 static void
30526 cp_parser_objc_at_property_declaration (cp_parser *parser)
30528 /* The following variables hold the attributes of the properties as
30529 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
30530 seen. When we see an attribute, we set them to 'true' (if they
30531 are boolean properties) or to the identifier (if they have an
30532 argument, ie, for getter and setter). Note that here we only
30533 parse the list of attributes, check the syntax and accumulate the
30534 attributes that we find. objc_add_property_declaration() will
30535 then process the information. */
30536 bool property_assign = false;
30537 bool property_copy = false;
30538 tree property_getter_ident = NULL_TREE;
30539 bool property_nonatomic = false;
30540 bool property_readonly = false;
30541 bool property_readwrite = false;
30542 bool property_retain = false;
30543 tree property_setter_ident = NULL_TREE;
30545 /* 'properties' is the list of properties that we read. Usually a
30546 single one, but maybe more (eg, in "@property int a, b, c;" there
30547 are three). */
30548 tree properties;
30549 location_t loc;
30551 loc = cp_lexer_peek_token (parser->lexer)->location;
30553 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
30555 /* Parse the optional attribute list... */
30556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
30558 /* Eat the '('. */
30559 cp_lexer_consume_token (parser->lexer);
30561 while (true)
30563 bool syntax_error = false;
30564 cp_token *token = cp_lexer_peek_token (parser->lexer);
30565 enum rid keyword;
30567 if (token->type != CPP_NAME)
30569 cp_parser_error (parser, "expected identifier");
30570 break;
30572 keyword = C_RID_CODE (token->u.value);
30573 cp_lexer_consume_token (parser->lexer);
30574 switch (keyword)
30576 case RID_ASSIGN: property_assign = true; break;
30577 case RID_COPY: property_copy = true; break;
30578 case RID_NONATOMIC: property_nonatomic = true; break;
30579 case RID_READONLY: property_readonly = true; break;
30580 case RID_READWRITE: property_readwrite = true; break;
30581 case RID_RETAIN: property_retain = true; break;
30583 case RID_GETTER:
30584 case RID_SETTER:
30585 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
30587 if (keyword == RID_GETTER)
30588 cp_parser_error (parser,
30589 "missing %<=%> (after %<getter%> attribute)");
30590 else
30591 cp_parser_error (parser,
30592 "missing %<=%> (after %<setter%> attribute)");
30593 syntax_error = true;
30594 break;
30596 cp_lexer_consume_token (parser->lexer); /* eat the = */
30597 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
30599 cp_parser_error (parser, "expected identifier");
30600 syntax_error = true;
30601 break;
30603 if (keyword == RID_SETTER)
30605 if (property_setter_ident != NULL_TREE)
30607 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
30608 cp_lexer_consume_token (parser->lexer);
30610 else
30611 property_setter_ident = cp_parser_objc_selector (parser);
30612 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
30613 cp_parser_error (parser, "setter name must terminate with %<:%>");
30614 else
30615 cp_lexer_consume_token (parser->lexer);
30617 else
30619 if (property_getter_ident != NULL_TREE)
30621 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
30622 cp_lexer_consume_token (parser->lexer);
30624 else
30625 property_getter_ident = cp_parser_objc_selector (parser);
30627 break;
30628 default:
30629 cp_parser_error (parser, "unknown property attribute");
30630 syntax_error = true;
30631 break;
30634 if (syntax_error)
30635 break;
30637 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30638 cp_lexer_consume_token (parser->lexer);
30639 else
30640 break;
30643 /* FIXME: "@property (setter, assign);" will generate a spurious
30644 "error: expected ‘)’ before ‘,’ token". This is because
30645 cp_parser_require, unlike the C counterpart, will produce an
30646 error even if we are in error recovery. */
30647 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
30649 cp_parser_skip_to_closing_parenthesis (parser,
30650 /*recovering=*/true,
30651 /*or_comma=*/false,
30652 /*consume_paren=*/true);
30656 /* ... and the property declaration(s). */
30657 properties = cp_parser_objc_struct_declaration (parser);
30659 if (properties == error_mark_node)
30661 cp_parser_skip_to_end_of_statement (parser);
30662 /* If the next token is now a `;', consume it. */
30663 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
30664 cp_lexer_consume_token (parser->lexer);
30665 return;
30668 if (properties == NULL_TREE)
30669 cp_parser_error (parser, "expected identifier");
30670 else
30672 /* Comma-separated properties are chained together in
30673 reverse order; add them one by one. */
30674 properties = nreverse (properties);
30676 for (; properties; properties = TREE_CHAIN (properties))
30677 objc_add_property_declaration (loc, copy_node (properties),
30678 property_readonly, property_readwrite,
30679 property_assign, property_retain,
30680 property_copy, property_nonatomic,
30681 property_getter_ident, property_setter_ident);
30684 cp_parser_consume_semicolon_at_end_of_statement (parser);
30687 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
30689 objc-synthesize-declaration:
30690 @synthesize objc-synthesize-identifier-list ;
30692 objc-synthesize-identifier-list:
30693 objc-synthesize-identifier
30694 objc-synthesize-identifier-list, objc-synthesize-identifier
30696 objc-synthesize-identifier
30697 identifier
30698 identifier = identifier
30700 For example:
30701 @synthesize MyProperty;
30702 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
30704 PS: This function is identical to c_parser_objc_at_synthesize_declaration
30705 for C. Keep them in sync.
30707 static void
30708 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
30710 tree list = NULL_TREE;
30711 location_t loc;
30712 loc = cp_lexer_peek_token (parser->lexer)->location;
30714 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
30715 while (true)
30717 tree property, ivar;
30718 property = cp_parser_identifier (parser);
30719 if (property == error_mark_node)
30721 cp_parser_consume_semicolon_at_end_of_statement (parser);
30722 return;
30724 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
30726 cp_lexer_consume_token (parser->lexer);
30727 ivar = cp_parser_identifier (parser);
30728 if (ivar == error_mark_node)
30730 cp_parser_consume_semicolon_at_end_of_statement (parser);
30731 return;
30734 else
30735 ivar = NULL_TREE;
30736 list = chainon (list, build_tree_list (ivar, property));
30737 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30738 cp_lexer_consume_token (parser->lexer);
30739 else
30740 break;
30742 cp_parser_consume_semicolon_at_end_of_statement (parser);
30743 objc_add_synthesize_declaration (loc, list);
30746 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
30748 objc-dynamic-declaration:
30749 @dynamic identifier-list ;
30751 For example:
30752 @dynamic MyProperty;
30753 @dynamic MyProperty, AnotherProperty;
30755 PS: This function is identical to c_parser_objc_at_dynamic_declaration
30756 for C. Keep them in sync.
30758 static void
30759 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
30761 tree list = NULL_TREE;
30762 location_t loc;
30763 loc = cp_lexer_peek_token (parser->lexer)->location;
30765 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
30766 while (true)
30768 tree property;
30769 property = cp_parser_identifier (parser);
30770 if (property == error_mark_node)
30772 cp_parser_consume_semicolon_at_end_of_statement (parser);
30773 return;
30775 list = chainon (list, build_tree_list (NULL, property));
30776 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
30777 cp_lexer_consume_token (parser->lexer);
30778 else
30779 break;
30781 cp_parser_consume_semicolon_at_end_of_statement (parser);
30782 objc_add_dynamic_declaration (loc, list);
30786 /* OpenMP 2.5 / 3.0 / 3.1 / 4.0 parsing routines. */
30788 /* Returns name of the next clause.
30789 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
30790 the token is not consumed. Otherwise appropriate pragma_omp_clause is
30791 returned and the token is consumed. */
30793 static pragma_omp_clause
30794 cp_parser_omp_clause_name (cp_parser *parser)
30796 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
30798 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
30799 result = PRAGMA_OACC_CLAUSE_AUTO;
30800 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
30801 result = PRAGMA_OMP_CLAUSE_IF;
30802 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
30803 result = PRAGMA_OMP_CLAUSE_DEFAULT;
30804 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE))
30805 result = PRAGMA_OACC_CLAUSE_DELETE;
30806 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
30807 result = PRAGMA_OMP_CLAUSE_PRIVATE;
30808 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
30809 result = PRAGMA_OMP_CLAUSE_FOR;
30810 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
30812 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
30813 const char *p = IDENTIFIER_POINTER (id);
30815 switch (p[0])
30817 case 'a':
30818 if (!strcmp ("aligned", p))
30819 result = PRAGMA_OMP_CLAUSE_ALIGNED;
30820 else if (!strcmp ("async", p))
30821 result = PRAGMA_OACC_CLAUSE_ASYNC;
30822 break;
30823 case 'c':
30824 if (!strcmp ("collapse", p))
30825 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
30826 else if (!strcmp ("copy", p))
30827 result = PRAGMA_OACC_CLAUSE_COPY;
30828 else if (!strcmp ("copyin", p))
30829 result = PRAGMA_OMP_CLAUSE_COPYIN;
30830 else if (!strcmp ("copyout", p))
30831 result = PRAGMA_OACC_CLAUSE_COPYOUT;
30832 else if (!strcmp ("copyprivate", p))
30833 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
30834 else if (!strcmp ("create", p))
30835 result = PRAGMA_OACC_CLAUSE_CREATE;
30836 break;
30837 case 'd':
30838 if (!strcmp ("defaultmap", p))
30839 result = PRAGMA_OMP_CLAUSE_DEFAULTMAP;
30840 else if (!strcmp ("depend", p))
30841 result = PRAGMA_OMP_CLAUSE_DEPEND;
30842 else if (!strcmp ("device", p))
30843 result = PRAGMA_OMP_CLAUSE_DEVICE;
30844 else if (!strcmp ("deviceptr", p))
30845 result = PRAGMA_OACC_CLAUSE_DEVICEPTR;
30846 else if (!strcmp ("device_resident", p))
30847 result = PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT;
30848 else if (!strcmp ("dist_schedule", p))
30849 result = PRAGMA_OMP_CLAUSE_DIST_SCHEDULE;
30850 break;
30851 case 'f':
30852 if (!strcmp ("final", p))
30853 result = PRAGMA_OMP_CLAUSE_FINAL;
30854 else if (!strcmp ("firstprivate", p))
30855 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
30856 else if (!strcmp ("from", p))
30857 result = PRAGMA_OMP_CLAUSE_FROM;
30858 break;
30859 case 'g':
30860 if (!strcmp ("gang", p))
30861 result = PRAGMA_OACC_CLAUSE_GANG;
30862 else if (!strcmp ("grainsize", p))
30863 result = PRAGMA_OMP_CLAUSE_GRAINSIZE;
30864 break;
30865 case 'h':
30866 if (!strcmp ("hint", p))
30867 result = PRAGMA_OMP_CLAUSE_HINT;
30868 else if (!strcmp ("host", p))
30869 result = PRAGMA_OACC_CLAUSE_HOST;
30870 break;
30871 case 'i':
30872 if (!strcmp ("inbranch", p))
30873 result = PRAGMA_OMP_CLAUSE_INBRANCH;
30874 else if (!strcmp ("independent", p))
30875 result = PRAGMA_OACC_CLAUSE_INDEPENDENT;
30876 else if (!strcmp ("is_device_ptr", p))
30877 result = PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR;
30878 break;
30879 case 'l':
30880 if (!strcmp ("lastprivate", p))
30881 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
30882 else if (!strcmp ("linear", p))
30883 result = PRAGMA_OMP_CLAUSE_LINEAR;
30884 else if (!strcmp ("link", p))
30885 result = PRAGMA_OMP_CLAUSE_LINK;
30886 break;
30887 case 'm':
30888 if (!strcmp ("map", p))
30889 result = PRAGMA_OMP_CLAUSE_MAP;
30890 else if (!strcmp ("mergeable", p))
30891 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
30892 else if (flag_cilkplus && !strcmp ("mask", p))
30893 result = PRAGMA_CILK_CLAUSE_MASK;
30894 break;
30895 case 'n':
30896 if (!strcmp ("nogroup", p))
30897 result = PRAGMA_OMP_CLAUSE_NOGROUP;
30898 else if (!strcmp ("notinbranch", p))
30899 result = PRAGMA_OMP_CLAUSE_NOTINBRANCH;
30900 else if (!strcmp ("nowait", p))
30901 result = PRAGMA_OMP_CLAUSE_NOWAIT;
30902 else if (flag_cilkplus && !strcmp ("nomask", p))
30903 result = PRAGMA_CILK_CLAUSE_NOMASK;
30904 else if (!strcmp ("num_gangs", p))
30905 result = PRAGMA_OACC_CLAUSE_NUM_GANGS;
30906 else if (!strcmp ("num_tasks", p))
30907 result = PRAGMA_OMP_CLAUSE_NUM_TASKS;
30908 else if (!strcmp ("num_teams", p))
30909 result = PRAGMA_OMP_CLAUSE_NUM_TEAMS;
30910 else if (!strcmp ("num_threads", p))
30911 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
30912 else if (!strcmp ("num_workers", p))
30913 result = PRAGMA_OACC_CLAUSE_NUM_WORKERS;
30914 break;
30915 case 'o':
30916 if (!strcmp ("ordered", p))
30917 result = PRAGMA_OMP_CLAUSE_ORDERED;
30918 break;
30919 case 'p':
30920 if (!strcmp ("parallel", p))
30921 result = PRAGMA_OMP_CLAUSE_PARALLEL;
30922 else if (!strcmp ("present", p))
30923 result = PRAGMA_OACC_CLAUSE_PRESENT;
30924 else if (!strcmp ("present_or_copy", p)
30925 || !strcmp ("pcopy", p))
30926 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY;
30927 else if (!strcmp ("present_or_copyin", p)
30928 || !strcmp ("pcopyin", p))
30929 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN;
30930 else if (!strcmp ("present_or_copyout", p)
30931 || !strcmp ("pcopyout", p))
30932 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT;
30933 else if (!strcmp ("present_or_create", p)
30934 || !strcmp ("pcreate", p))
30935 result = PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE;
30936 else if (!strcmp ("priority", p))
30937 result = PRAGMA_OMP_CLAUSE_PRIORITY;
30938 else if (!strcmp ("proc_bind", p))
30939 result = PRAGMA_OMP_CLAUSE_PROC_BIND;
30940 break;
30941 case 'r':
30942 if (!strcmp ("reduction", p))
30943 result = PRAGMA_OMP_CLAUSE_REDUCTION;
30944 break;
30945 case 's':
30946 if (!strcmp ("safelen", p))
30947 result = PRAGMA_OMP_CLAUSE_SAFELEN;
30948 else if (!strcmp ("schedule", p))
30949 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
30950 else if (!strcmp ("sections", p))
30951 result = PRAGMA_OMP_CLAUSE_SECTIONS;
30952 else if (!strcmp ("self", p))
30953 result = PRAGMA_OACC_CLAUSE_SELF;
30954 else if (!strcmp ("seq", p))
30955 result = PRAGMA_OACC_CLAUSE_SEQ;
30956 else if (!strcmp ("shared", p))
30957 result = PRAGMA_OMP_CLAUSE_SHARED;
30958 else if (!strcmp ("simd", p))
30959 result = PRAGMA_OMP_CLAUSE_SIMD;
30960 else if (!strcmp ("simdlen", p))
30961 result = PRAGMA_OMP_CLAUSE_SIMDLEN;
30962 break;
30963 case 't':
30964 if (!strcmp ("taskgroup", p))
30965 result = PRAGMA_OMP_CLAUSE_TASKGROUP;
30966 else if (!strcmp ("thread_limit", p))
30967 result = PRAGMA_OMP_CLAUSE_THREAD_LIMIT;
30968 else if (!strcmp ("threads", p))
30969 result = PRAGMA_OMP_CLAUSE_THREADS;
30970 else if (!strcmp ("tile", p))
30971 result = PRAGMA_OACC_CLAUSE_TILE;
30972 else if (!strcmp ("to", p))
30973 result = PRAGMA_OMP_CLAUSE_TO;
30974 break;
30975 case 'u':
30976 if (!strcmp ("uniform", p))
30977 result = PRAGMA_OMP_CLAUSE_UNIFORM;
30978 else if (!strcmp ("untied", p))
30979 result = PRAGMA_OMP_CLAUSE_UNTIED;
30980 else if (!strcmp ("use_device", p))
30981 result = PRAGMA_OACC_CLAUSE_USE_DEVICE;
30982 else if (!strcmp ("use_device_ptr", p))
30983 result = PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR;
30984 break;
30985 case 'v':
30986 if (!strcmp ("vector", p))
30987 result = PRAGMA_OACC_CLAUSE_VECTOR;
30988 else if (!strcmp ("vector_length", p))
30989 result = PRAGMA_OACC_CLAUSE_VECTOR_LENGTH;
30990 else if (flag_cilkplus && !strcmp ("vectorlength", p))
30991 result = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
30992 break;
30993 case 'w':
30994 if (!strcmp ("wait", p))
30995 result = PRAGMA_OACC_CLAUSE_WAIT;
30996 else if (!strcmp ("worker", p))
30997 result = PRAGMA_OACC_CLAUSE_WORKER;
30998 break;
31002 if (result != PRAGMA_OMP_CLAUSE_NONE)
31003 cp_lexer_consume_token (parser->lexer);
31005 return result;
31008 /* Validate that a clause of the given type does not already exist. */
31010 static void
31011 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
31012 const char *name, location_t location)
31014 tree c;
31016 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
31017 if (OMP_CLAUSE_CODE (c) == code)
31019 error_at (location, "too many %qs clauses", name);
31020 break;
31024 /* OpenMP 2.5:
31025 variable-list:
31026 identifier
31027 variable-list , identifier
31029 In addition, we match a closing parenthesis (or, if COLON is non-NULL,
31030 colon). An opening parenthesis will have been consumed by the caller.
31032 If KIND is nonzero, create the appropriate node and install the decl
31033 in OMP_CLAUSE_DECL and add the node to the head of the list.
31035 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
31036 return the list created.
31038 COLON can be NULL if only closing parenthesis should end the list,
31039 or pointer to bool which will receive false if the list is terminated
31040 by closing parenthesis or true if the list is terminated by colon. */
31042 static tree
31043 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
31044 tree list, bool *colon)
31046 cp_token *token;
31047 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
31048 if (colon)
31050 parser->colon_corrects_to_scope_p = false;
31051 *colon = false;
31053 while (1)
31055 tree name, decl;
31057 token = cp_lexer_peek_token (parser->lexer);
31058 if (kind != 0
31059 && current_class_ptr
31060 && cp_parser_is_keyword (token, RID_THIS))
31062 decl = finish_this_expr ();
31063 if (TREE_CODE (decl) == NON_LVALUE_EXPR
31064 || CONVERT_EXPR_P (decl))
31065 decl = TREE_OPERAND (decl, 0);
31066 cp_lexer_consume_token (parser->lexer);
31068 else
31070 name = cp_parser_id_expression (parser, /*template_p=*/false,
31071 /*check_dependency_p=*/true,
31072 /*template_p=*/NULL,
31073 /*declarator_p=*/false,
31074 /*optional_p=*/false);
31075 if (name == error_mark_node)
31076 goto skip_comma;
31078 decl = cp_parser_lookup_name_simple (parser, name, token->location);
31079 if (decl == error_mark_node)
31080 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
31081 token->location);
31083 if (decl == error_mark_node)
31085 else if (kind != 0)
31087 switch (kind)
31089 case OMP_CLAUSE__CACHE_:
31090 /* The OpenACC cache directive explicitly only allows "array
31091 elements or subarrays". */
31092 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_SQUARE)
31094 error_at (token->location, "expected %<[%>");
31095 decl = error_mark_node;
31096 break;
31098 /* FALLTHROUGH. */
31099 case OMP_CLAUSE_MAP:
31100 case OMP_CLAUSE_FROM:
31101 case OMP_CLAUSE_TO:
31102 while (cp_lexer_next_token_is (parser->lexer, CPP_DOT))
31104 location_t loc
31105 = cp_lexer_peek_token (parser->lexer)->location;
31106 cp_id_kind idk = CP_ID_KIND_NONE;
31107 cp_lexer_consume_token (parser->lexer);
31108 decl = convert_from_reference (decl);
31109 decl
31110 = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
31111 decl, false,
31112 &idk, loc);
31114 /* FALLTHROUGH. */
31115 case OMP_CLAUSE_DEPEND:
31116 case OMP_CLAUSE_REDUCTION:
31117 while (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
31119 tree low_bound = NULL_TREE, length = NULL_TREE;
31121 parser->colon_corrects_to_scope_p = false;
31122 cp_lexer_consume_token (parser->lexer);
31123 if (!cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31124 low_bound = cp_parser_expression (parser);
31125 if (!colon)
31126 parser->colon_corrects_to_scope_p
31127 = saved_colon_corrects_to_scope_p;
31128 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_SQUARE))
31129 length = integer_one_node;
31130 else
31132 /* Look for `:'. */
31133 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31134 goto skip_comma;
31135 if (!cp_lexer_next_token_is (parser->lexer,
31136 CPP_CLOSE_SQUARE))
31137 length = cp_parser_expression (parser);
31139 /* Look for the closing `]'. */
31140 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE,
31141 RT_CLOSE_SQUARE))
31142 goto skip_comma;
31144 decl = tree_cons (low_bound, length, decl);
31146 break;
31147 default:
31148 break;
31151 tree u = build_omp_clause (token->location, kind);
31152 OMP_CLAUSE_DECL (u) = decl;
31153 OMP_CLAUSE_CHAIN (u) = list;
31154 list = u;
31156 else
31157 list = tree_cons (decl, NULL_TREE, list);
31159 get_comma:
31160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
31161 break;
31162 cp_lexer_consume_token (parser->lexer);
31165 if (colon)
31166 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31168 if (colon != NULL && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
31170 *colon = true;
31171 cp_parser_require (parser, CPP_COLON, RT_COLON);
31172 return list;
31175 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31177 int ending;
31179 /* Try to resync to an unnested comma. Copied from
31180 cp_parser_parenthesized_expression_list. */
31181 skip_comma:
31182 if (colon)
31183 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
31184 ending = cp_parser_skip_to_closing_parenthesis (parser,
31185 /*recovering=*/true,
31186 /*or_comma=*/true,
31187 /*consume_paren=*/true);
31188 if (ending < 0)
31189 goto get_comma;
31192 return list;
31195 /* Similarly, but expect leading and trailing parenthesis. This is a very
31196 common case for omp clauses. */
31198 static tree
31199 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
31201 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31202 return cp_parser_omp_var_list_no_open (parser, kind, list, NULL);
31203 return list;
31206 /* OpenACC 2.0:
31207 copy ( variable-list )
31208 copyin ( variable-list )
31209 copyout ( variable-list )
31210 create ( variable-list )
31211 delete ( variable-list )
31212 present ( variable-list )
31213 present_or_copy ( variable-list )
31214 pcopy ( variable-list )
31215 present_or_copyin ( variable-list )
31216 pcopyin ( variable-list )
31217 present_or_copyout ( variable-list )
31218 pcopyout ( variable-list )
31219 present_or_create ( variable-list )
31220 pcreate ( variable-list ) */
31222 static tree
31223 cp_parser_oacc_data_clause (cp_parser *parser, pragma_omp_clause c_kind,
31224 tree list)
31226 enum gomp_map_kind kind;
31227 switch (c_kind)
31229 case PRAGMA_OACC_CLAUSE_COPY:
31230 kind = GOMP_MAP_FORCE_TOFROM;
31231 break;
31232 case PRAGMA_OACC_CLAUSE_COPYIN:
31233 kind = GOMP_MAP_FORCE_TO;
31234 break;
31235 case PRAGMA_OACC_CLAUSE_COPYOUT:
31236 kind = GOMP_MAP_FORCE_FROM;
31237 break;
31238 case PRAGMA_OACC_CLAUSE_CREATE:
31239 kind = GOMP_MAP_FORCE_ALLOC;
31240 break;
31241 case PRAGMA_OACC_CLAUSE_DELETE:
31242 kind = GOMP_MAP_DELETE;
31243 break;
31244 case PRAGMA_OACC_CLAUSE_DEVICE:
31245 kind = GOMP_MAP_FORCE_TO;
31246 break;
31247 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
31248 kind = GOMP_MAP_DEVICE_RESIDENT;
31249 break;
31250 case PRAGMA_OACC_CLAUSE_HOST:
31251 case PRAGMA_OACC_CLAUSE_SELF:
31252 kind = GOMP_MAP_FORCE_FROM;
31253 break;
31254 case PRAGMA_OACC_CLAUSE_LINK:
31255 kind = GOMP_MAP_LINK;
31256 break;
31257 case PRAGMA_OACC_CLAUSE_PRESENT:
31258 kind = GOMP_MAP_FORCE_PRESENT;
31259 break;
31260 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
31261 kind = GOMP_MAP_TOFROM;
31262 break;
31263 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
31264 kind = GOMP_MAP_TO;
31265 break;
31266 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
31267 kind = GOMP_MAP_FROM;
31268 break;
31269 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
31270 kind = GOMP_MAP_ALLOC;
31271 break;
31272 default:
31273 gcc_unreachable ();
31275 tree nl, c;
31276 nl = cp_parser_omp_var_list (parser, OMP_CLAUSE_MAP, list);
31278 for (c = nl; c != list; c = OMP_CLAUSE_CHAIN (c))
31279 OMP_CLAUSE_SET_MAP_KIND (c, kind);
31281 return nl;
31284 /* OpenACC 2.0:
31285 deviceptr ( variable-list ) */
31287 static tree
31288 cp_parser_oacc_data_clause_deviceptr (cp_parser *parser, tree list)
31290 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31291 tree vars, t;
31293 /* Can't use OMP_CLAUSE_MAP here (that is, can't use the generic
31294 cp_parser_oacc_data_clause), as for PRAGMA_OACC_CLAUSE_DEVICEPTR,
31295 variable-list must only allow for pointer variables. */
31296 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
31297 for (t = vars; t; t = TREE_CHAIN (t))
31299 tree v = TREE_PURPOSE (t);
31300 tree u = build_omp_clause (loc, OMP_CLAUSE_MAP);
31301 OMP_CLAUSE_SET_MAP_KIND (u, GOMP_MAP_FORCE_DEVICEPTR);
31302 OMP_CLAUSE_DECL (u) = v;
31303 OMP_CLAUSE_CHAIN (u) = list;
31304 list = u;
31307 return list;
31310 /* OpenACC 2.0:
31311 auto
31312 independent
31313 nohost
31314 seq */
31316 static tree
31317 cp_parser_oacc_simple_clause (cp_parser * /* parser */,
31318 enum omp_clause_code code,
31319 tree list, location_t location)
31321 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
31322 tree c = build_omp_clause (location, code);
31323 OMP_CLAUSE_CHAIN (c) = list;
31324 return c;
31327 /* OpenACC:
31328 num_gangs ( expression )
31329 num_workers ( expression )
31330 vector_length ( expression ) */
31332 static tree
31333 cp_parser_oacc_single_int_clause (cp_parser *parser, omp_clause_code code,
31334 const char *str, tree list)
31336 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
31338 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31339 return list;
31341 tree t = cp_parser_assignment_expression (parser, NULL, false, false);
31343 if (t == error_mark_node
31344 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31346 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31347 /*or_comma=*/false,
31348 /*consume_paren=*/true);
31349 return list;
31352 check_no_duplicate_clause (list, code, str, loc);
31354 tree c = build_omp_clause (loc, code);
31355 OMP_CLAUSE_OPERAND (c, 0) = t;
31356 OMP_CLAUSE_CHAIN (c) = list;
31357 return c;
31360 /* OpenACC:
31362 gang [( gang-arg-list )]
31363 worker [( [num:] int-expr )]
31364 vector [( [length:] int-expr )]
31366 where gang-arg is one of:
31368 [num:] int-expr
31369 static: size-expr
31371 and size-expr may be:
31374 int-expr
31377 static tree
31378 cp_parser_oacc_shape_clause (cp_parser *parser, omp_clause_code kind,
31379 const char *str, tree list)
31381 const char *id = "num";
31382 cp_lexer *lexer = parser->lexer;
31383 tree ops[2] = { NULL_TREE, NULL_TREE }, c;
31384 location_t loc = cp_lexer_peek_token (lexer)->location;
31386 if (kind == OMP_CLAUSE_VECTOR)
31387 id = "length";
31389 if (cp_lexer_next_token_is (lexer, CPP_OPEN_PAREN))
31391 cp_lexer_consume_token (lexer);
31395 cp_token *next = cp_lexer_peek_token (lexer);
31396 int idx = 0;
31398 /* Gang static argument. */
31399 if (kind == OMP_CLAUSE_GANG
31400 && cp_lexer_next_token_is_keyword (lexer, RID_STATIC))
31402 cp_lexer_consume_token (lexer);
31404 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
31405 goto cleanup_error;
31407 idx = 1;
31408 if (ops[idx] != NULL)
31410 cp_parser_error (parser, "too many %<static%> arguments");
31411 goto cleanup_error;
31414 /* Check for the '*' argument. */
31415 if (cp_lexer_next_token_is (lexer, CPP_MULT)
31416 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31417 || cp_lexer_nth_token_is (parser->lexer, 2,
31418 CPP_CLOSE_PAREN)))
31420 cp_lexer_consume_token (lexer);
31421 ops[idx] = integer_minus_one_node;
31423 if (cp_lexer_next_token_is (lexer, CPP_COMMA))
31425 cp_lexer_consume_token (lexer);
31426 continue;
31428 else break;
31431 /* Worker num: argument and vector length: arguments. */
31432 else if (cp_lexer_next_token_is (lexer, CPP_NAME)
31433 && id_equal (next->u.value, id)
31434 && cp_lexer_nth_token_is (lexer, 2, CPP_COLON))
31436 cp_lexer_consume_token (lexer); /* id */
31437 cp_lexer_consume_token (lexer); /* ':' */
31440 /* Now collect the actual argument. */
31441 if (ops[idx] != NULL_TREE)
31443 cp_parser_error (parser, "unexpected argument");
31444 goto cleanup_error;
31447 tree expr = cp_parser_assignment_expression (parser, NULL, false,
31448 false);
31449 if (expr == error_mark_node)
31450 goto cleanup_error;
31452 mark_exp_read (expr);
31453 ops[idx] = expr;
31455 if (kind == OMP_CLAUSE_GANG
31456 && cp_lexer_next_token_is (lexer, CPP_COMMA))
31458 cp_lexer_consume_token (lexer);
31459 continue;
31461 break;
31463 while (1);
31465 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31466 goto cleanup_error;
31469 check_no_duplicate_clause (list, kind, str, loc);
31471 c = build_omp_clause (loc, kind);
31473 if (ops[1])
31474 OMP_CLAUSE_OPERAND (c, 1) = ops[1];
31476 OMP_CLAUSE_OPERAND (c, 0) = ops[0];
31477 OMP_CLAUSE_CHAIN (c) = list;
31479 return c;
31481 cleanup_error:
31482 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
31483 return list;
31486 /* OpenACC 2.0:
31487 tile ( size-expr-list ) */
31489 static tree
31490 cp_parser_oacc_clause_tile (cp_parser *parser, location_t clause_loc, tree list)
31492 tree c, expr = error_mark_node;
31493 tree tile = NULL_TREE;
31495 /* Collapse and tile are mutually exclusive. (The spec doesn't say
31496 so, but the spec authors never considered such a case and have
31497 differing opinions on what it might mean, including 'not
31498 allowed'.) */
31499 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", clause_loc);
31500 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse",
31501 clause_loc);
31503 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31504 return list;
31508 if (tile && !cp_parser_require (parser, CPP_COMMA, RT_COMMA))
31509 return list;
31511 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT)
31512 && (cp_lexer_nth_token_is (parser->lexer, 2, CPP_COMMA)
31513 || cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN)))
31515 cp_lexer_consume_token (parser->lexer);
31516 expr = integer_zero_node;
31518 else
31519 expr = cp_parser_constant_expression (parser);
31521 tile = tree_cons (NULL_TREE, expr, tile);
31523 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN));
31525 /* Consume the trailing ')'. */
31526 cp_lexer_consume_token (parser->lexer);
31528 c = build_omp_clause (clause_loc, OMP_CLAUSE_TILE);
31529 tile = nreverse (tile);
31530 OMP_CLAUSE_TILE_LIST (c) = tile;
31531 OMP_CLAUSE_CHAIN (c) = list;
31532 return c;
31535 /* OpenACC 2.0
31536 Parse wait clause or directive parameters. */
31538 static tree
31539 cp_parser_oacc_wait_list (cp_parser *parser, location_t clause_loc, tree list)
31541 vec<tree, va_gc> *args;
31542 tree t, args_tree;
31544 args = cp_parser_parenthesized_expression_list (parser, non_attr,
31545 /*cast_p=*/false,
31546 /*allow_expansion_p=*/true,
31547 /*non_constant_p=*/NULL);
31549 if (args == NULL || args->length () == 0)
31551 cp_parser_error (parser, "expected integer expression before ')'");
31552 if (args != NULL)
31553 release_tree_vector (args);
31554 return list;
31557 args_tree = build_tree_list_vec (args);
31559 release_tree_vector (args);
31561 for (t = args_tree; t; t = TREE_CHAIN (t))
31563 tree targ = TREE_VALUE (t);
31565 if (targ != error_mark_node)
31567 if (!INTEGRAL_TYPE_P (TREE_TYPE (targ)))
31568 error ("%<wait%> expression must be integral");
31569 else
31571 tree c = build_omp_clause (clause_loc, OMP_CLAUSE_WAIT);
31573 mark_rvalue_use (targ);
31574 OMP_CLAUSE_DECL (c) = targ;
31575 OMP_CLAUSE_CHAIN (c) = list;
31576 list = c;
31581 return list;
31584 /* OpenACC:
31585 wait ( int-expr-list ) */
31587 static tree
31588 cp_parser_oacc_clause_wait (cp_parser *parser, tree list)
31590 location_t location = cp_lexer_peek_token (parser->lexer)->location;
31592 if (cp_lexer_peek_token (parser->lexer)->type != CPP_OPEN_PAREN)
31593 return list;
31595 list = cp_parser_oacc_wait_list (parser, location, list);
31597 return list;
31600 /* OpenMP 3.0:
31601 collapse ( constant-expression ) */
31603 static tree
31604 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
31606 tree c, num;
31607 location_t loc;
31608 HOST_WIDE_INT n;
31610 loc = cp_lexer_peek_token (parser->lexer)->location;
31611 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31612 return list;
31614 num = cp_parser_constant_expression (parser);
31616 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31617 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31618 /*or_comma=*/false,
31619 /*consume_paren=*/true);
31621 if (num == error_mark_node)
31622 return list;
31623 num = fold_non_dependent_expr (num);
31624 if (!tree_fits_shwi_p (num)
31625 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
31626 || (n = tree_to_shwi (num)) <= 0
31627 || (int) n != n)
31629 error_at (loc, "collapse argument needs positive constant integer expression");
31630 return list;
31633 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
31634 check_no_duplicate_clause (list, OMP_CLAUSE_TILE, "tile", location);
31635 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
31636 OMP_CLAUSE_CHAIN (c) = list;
31637 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
31639 return c;
31642 /* OpenMP 2.5:
31643 default ( none | shared )
31645 OpenACC:
31646 default ( none | present ) */
31648 static tree
31649 cp_parser_omp_clause_default (cp_parser *parser, tree list,
31650 location_t location, bool is_oacc)
31652 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
31653 tree c;
31655 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31656 return list;
31657 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31659 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31660 const char *p = IDENTIFIER_POINTER (id);
31662 switch (p[0])
31664 case 'n':
31665 if (strcmp ("none", p) != 0)
31666 goto invalid_kind;
31667 kind = OMP_CLAUSE_DEFAULT_NONE;
31668 break;
31670 case 'p':
31671 if (strcmp ("present", p) != 0 || !is_oacc)
31672 goto invalid_kind;
31673 kind = OMP_CLAUSE_DEFAULT_PRESENT;
31674 break;
31676 case 's':
31677 if (strcmp ("shared", p) != 0 || is_oacc)
31678 goto invalid_kind;
31679 kind = OMP_CLAUSE_DEFAULT_SHARED;
31680 break;
31682 default:
31683 goto invalid_kind;
31686 cp_lexer_consume_token (parser->lexer);
31688 else
31690 invalid_kind:
31691 if (is_oacc)
31692 cp_parser_error (parser, "expected %<none%> or %<present%>");
31693 else
31694 cp_parser_error (parser, "expected %<none%> or %<shared%>");
31697 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED
31698 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31699 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31700 /*or_comma=*/false,
31701 /*consume_paren=*/true);
31703 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
31704 return list;
31706 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
31707 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
31708 OMP_CLAUSE_CHAIN (c) = list;
31709 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
31711 return c;
31714 /* OpenMP 3.1:
31715 final ( expression ) */
31717 static tree
31718 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
31720 tree t, c;
31722 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31723 return list;
31725 t = cp_parser_condition (parser);
31727 if (t == error_mark_node
31728 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31729 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31730 /*or_comma=*/false,
31731 /*consume_paren=*/true);
31733 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
31735 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
31736 OMP_CLAUSE_FINAL_EXPR (c) = t;
31737 OMP_CLAUSE_CHAIN (c) = list;
31739 return c;
31742 /* OpenMP 2.5:
31743 if ( expression )
31745 OpenMP 4.5:
31746 if ( directive-name-modifier : expression )
31748 directive-name-modifier:
31749 parallel | task | taskloop | target data | target | target update
31750 | target enter data | target exit data */
31752 static tree
31753 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location,
31754 bool is_omp)
31756 tree t, c;
31757 enum tree_code if_modifier = ERROR_MARK;
31759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31760 return list;
31762 if (is_omp && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
31764 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
31765 const char *p = IDENTIFIER_POINTER (id);
31766 int n = 2;
31768 if (strcmp ("parallel", p) == 0)
31769 if_modifier = OMP_PARALLEL;
31770 else if (strcmp ("task", p) == 0)
31771 if_modifier = OMP_TASK;
31772 else if (strcmp ("taskloop", p) == 0)
31773 if_modifier = OMP_TASKLOOP;
31774 else if (strcmp ("target", p) == 0)
31776 if_modifier = OMP_TARGET;
31777 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_NAME))
31779 id = cp_lexer_peek_nth_token (parser->lexer, 2)->u.value;
31780 p = IDENTIFIER_POINTER (id);
31781 if (strcmp ("data", p) == 0)
31782 if_modifier = OMP_TARGET_DATA;
31783 else if (strcmp ("update", p) == 0)
31784 if_modifier = OMP_TARGET_UPDATE;
31785 else if (strcmp ("enter", p) == 0)
31786 if_modifier = OMP_TARGET_ENTER_DATA;
31787 else if (strcmp ("exit", p) == 0)
31788 if_modifier = OMP_TARGET_EXIT_DATA;
31789 if (if_modifier != OMP_TARGET)
31790 n = 3;
31791 else
31793 location_t loc
31794 = cp_lexer_peek_nth_token (parser->lexer, 2)->location;
31795 error_at (loc, "expected %<data%>, %<update%>, %<enter%> "
31796 "or %<exit%>");
31797 if_modifier = ERROR_MARK;
31799 if (if_modifier == OMP_TARGET_ENTER_DATA
31800 || if_modifier == OMP_TARGET_EXIT_DATA)
31802 if (cp_lexer_nth_token_is (parser->lexer, 3, CPP_NAME))
31804 id = cp_lexer_peek_nth_token (parser->lexer, 3)->u.value;
31805 p = IDENTIFIER_POINTER (id);
31806 if (strcmp ("data", p) == 0)
31807 n = 4;
31809 if (n != 4)
31811 location_t loc
31812 = cp_lexer_peek_nth_token (parser->lexer, 3)->location;
31813 error_at (loc, "expected %<data%>");
31814 if_modifier = ERROR_MARK;
31819 if (if_modifier != ERROR_MARK)
31821 if (cp_lexer_nth_token_is (parser->lexer, n, CPP_COLON))
31823 while (n-- > 0)
31824 cp_lexer_consume_token (parser->lexer);
31826 else
31828 if (n > 2)
31830 location_t loc
31831 = cp_lexer_peek_nth_token (parser->lexer, n)->location;
31832 error_at (loc, "expected %<:%>");
31834 if_modifier = ERROR_MARK;
31839 t = cp_parser_condition (parser);
31841 if (t == error_mark_node
31842 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31843 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31844 /*or_comma=*/false,
31845 /*consume_paren=*/true);
31847 for (c = list; c ; c = OMP_CLAUSE_CHAIN (c))
31848 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_IF)
31850 if (if_modifier != ERROR_MARK
31851 && OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31853 const char *p = NULL;
31854 switch (if_modifier)
31856 case OMP_PARALLEL: p = "parallel"; break;
31857 case OMP_TASK: p = "task"; break;
31858 case OMP_TASKLOOP: p = "taskloop"; break;
31859 case OMP_TARGET_DATA: p = "target data"; break;
31860 case OMP_TARGET: p = "target"; break;
31861 case OMP_TARGET_UPDATE: p = "target update"; break;
31862 case OMP_TARGET_ENTER_DATA: p = "enter data"; break;
31863 case OMP_TARGET_EXIT_DATA: p = "exit data"; break;
31864 default: gcc_unreachable ();
31866 error_at (location, "too many %<if%> clauses with %qs modifier",
31868 return list;
31870 else if (OMP_CLAUSE_IF_MODIFIER (c) == if_modifier)
31872 if (!is_omp)
31873 error_at (location, "too many %<if%> clauses");
31874 else
31875 error_at (location, "too many %<if%> clauses without modifier");
31876 return list;
31878 else if (if_modifier == ERROR_MARK
31879 || OMP_CLAUSE_IF_MODIFIER (c) == ERROR_MARK)
31881 error_at (location, "if any %<if%> clause has modifier, then all "
31882 "%<if%> clauses have to use modifier");
31883 return list;
31887 c = build_omp_clause (location, OMP_CLAUSE_IF);
31888 OMP_CLAUSE_IF_MODIFIER (c) = if_modifier;
31889 OMP_CLAUSE_IF_EXPR (c) = t;
31890 OMP_CLAUSE_CHAIN (c) = list;
31892 return c;
31895 /* OpenMP 3.1:
31896 mergeable */
31898 static tree
31899 cp_parser_omp_clause_mergeable (cp_parser * /*parser*/,
31900 tree list, location_t location)
31902 tree c;
31904 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
31905 location);
31907 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
31908 OMP_CLAUSE_CHAIN (c) = list;
31909 return c;
31912 /* OpenMP 2.5:
31913 nowait */
31915 static tree
31916 cp_parser_omp_clause_nowait (cp_parser * /*parser*/,
31917 tree list, location_t location)
31919 tree c;
31921 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
31923 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
31924 OMP_CLAUSE_CHAIN (c) = list;
31925 return c;
31928 /* OpenMP 2.5:
31929 num_threads ( expression ) */
31931 static tree
31932 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
31933 location_t location)
31935 tree t, c;
31937 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31938 return list;
31940 t = cp_parser_expression (parser);
31942 if (t == error_mark_node
31943 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31944 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31945 /*or_comma=*/false,
31946 /*consume_paren=*/true);
31948 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
31949 "num_threads", location);
31951 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
31952 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
31953 OMP_CLAUSE_CHAIN (c) = list;
31955 return c;
31958 /* OpenMP 4.5:
31959 num_tasks ( expression ) */
31961 static tree
31962 cp_parser_omp_clause_num_tasks (cp_parser *parser, tree list,
31963 location_t location)
31965 tree t, c;
31967 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31968 return list;
31970 t = cp_parser_expression (parser);
31972 if (t == error_mark_node
31973 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
31974 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
31975 /*or_comma=*/false,
31976 /*consume_paren=*/true);
31978 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TASKS,
31979 "num_tasks", location);
31981 c = build_omp_clause (location, OMP_CLAUSE_NUM_TASKS);
31982 OMP_CLAUSE_NUM_TASKS_EXPR (c) = t;
31983 OMP_CLAUSE_CHAIN (c) = list;
31985 return c;
31988 /* OpenMP 4.5:
31989 grainsize ( expression ) */
31991 static tree
31992 cp_parser_omp_clause_grainsize (cp_parser *parser, tree list,
31993 location_t location)
31995 tree t, c;
31997 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
31998 return list;
32000 t = cp_parser_expression (parser);
32002 if (t == error_mark_node
32003 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32004 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32005 /*or_comma=*/false,
32006 /*consume_paren=*/true);
32008 check_no_duplicate_clause (list, OMP_CLAUSE_GRAINSIZE,
32009 "grainsize", location);
32011 c = build_omp_clause (location, OMP_CLAUSE_GRAINSIZE);
32012 OMP_CLAUSE_GRAINSIZE_EXPR (c) = t;
32013 OMP_CLAUSE_CHAIN (c) = list;
32015 return c;
32018 /* OpenMP 4.5:
32019 priority ( expression ) */
32021 static tree
32022 cp_parser_omp_clause_priority (cp_parser *parser, tree list,
32023 location_t location)
32025 tree t, c;
32027 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32028 return list;
32030 t = cp_parser_expression (parser);
32032 if (t == error_mark_node
32033 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32034 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32035 /*or_comma=*/false,
32036 /*consume_paren=*/true);
32038 check_no_duplicate_clause (list, OMP_CLAUSE_PRIORITY,
32039 "priority", location);
32041 c = build_omp_clause (location, OMP_CLAUSE_PRIORITY);
32042 OMP_CLAUSE_PRIORITY_EXPR (c) = t;
32043 OMP_CLAUSE_CHAIN (c) = list;
32045 return c;
32048 /* OpenMP 4.5:
32049 hint ( expression ) */
32051 static tree
32052 cp_parser_omp_clause_hint (cp_parser *parser, tree list,
32053 location_t location)
32055 tree t, c;
32057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32058 return list;
32060 t = cp_parser_expression (parser);
32062 if (t == error_mark_node
32063 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32064 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32065 /*or_comma=*/false,
32066 /*consume_paren=*/true);
32068 check_no_duplicate_clause (list, OMP_CLAUSE_HINT, "hint", location);
32070 c = build_omp_clause (location, OMP_CLAUSE_HINT);
32071 OMP_CLAUSE_HINT_EXPR (c) = t;
32072 OMP_CLAUSE_CHAIN (c) = list;
32074 return c;
32077 /* OpenMP 4.5:
32078 defaultmap ( tofrom : scalar ) */
32080 static tree
32081 cp_parser_omp_clause_defaultmap (cp_parser *parser, tree list,
32082 location_t location)
32084 tree c, id;
32085 const char *p;
32087 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32088 return list;
32090 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32092 cp_parser_error (parser, "expected %<tofrom%>");
32093 goto out_err;
32095 id = cp_lexer_peek_token (parser->lexer)->u.value;
32096 p = IDENTIFIER_POINTER (id);
32097 if (strcmp (p, "tofrom") != 0)
32099 cp_parser_error (parser, "expected %<tofrom%>");
32100 goto out_err;
32102 cp_lexer_consume_token (parser->lexer);
32103 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32104 goto out_err;
32106 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32108 cp_parser_error (parser, "expected %<scalar%>");
32109 goto out_err;
32111 id = cp_lexer_peek_token (parser->lexer)->u.value;
32112 p = IDENTIFIER_POINTER (id);
32113 if (strcmp (p, "scalar") != 0)
32115 cp_parser_error (parser, "expected %<scalar%>");
32116 goto out_err;
32118 cp_lexer_consume_token (parser->lexer);
32119 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32120 goto out_err;
32122 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULTMAP, "defaultmap",
32123 location);
32125 c = build_omp_clause (location, OMP_CLAUSE_DEFAULTMAP);
32126 OMP_CLAUSE_CHAIN (c) = list;
32127 return c;
32129 out_err:
32130 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32131 /*or_comma=*/false,
32132 /*consume_paren=*/true);
32133 return list;
32136 /* OpenMP 2.5:
32137 ordered
32139 OpenMP 4.5:
32140 ordered ( constant-expression ) */
32142 static tree
32143 cp_parser_omp_clause_ordered (cp_parser *parser,
32144 tree list, location_t location)
32146 tree c, num = NULL_TREE;
32147 HOST_WIDE_INT n;
32149 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
32150 "ordered", location);
32152 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
32154 cp_lexer_consume_token (parser->lexer);
32156 num = cp_parser_constant_expression (parser);
32158 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32159 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32160 /*or_comma=*/false,
32161 /*consume_paren=*/true);
32163 if (num == error_mark_node)
32164 return list;
32165 num = fold_non_dependent_expr (num);
32166 if (!tree_fits_shwi_p (num)
32167 || !INTEGRAL_TYPE_P (TREE_TYPE (num))
32168 || (n = tree_to_shwi (num)) <= 0
32169 || (int) n != n)
32171 error_at (location,
32172 "ordered argument needs positive constant integer "
32173 "expression");
32174 return list;
32178 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
32179 OMP_CLAUSE_ORDERED_EXPR (c) = num;
32180 OMP_CLAUSE_CHAIN (c) = list;
32181 return c;
32184 /* OpenMP 2.5:
32185 reduction ( reduction-operator : variable-list )
32187 reduction-operator:
32188 One of: + * - & ^ | && ||
32190 OpenMP 3.1:
32192 reduction-operator:
32193 One of: + * - & ^ | && || min max
32195 OpenMP 4.0:
32197 reduction-operator:
32198 One of: + * - & ^ | && ||
32199 id-expression */
32201 static tree
32202 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
32204 enum tree_code code = ERROR_MARK;
32205 tree nlist, c, id = NULL_TREE;
32207 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32208 return list;
32210 switch (cp_lexer_peek_token (parser->lexer)->type)
32212 case CPP_PLUS: code = PLUS_EXPR; break;
32213 case CPP_MULT: code = MULT_EXPR; break;
32214 case CPP_MINUS: code = MINUS_EXPR; break;
32215 case CPP_AND: code = BIT_AND_EXPR; break;
32216 case CPP_XOR: code = BIT_XOR_EXPR; break;
32217 case CPP_OR: code = BIT_IOR_EXPR; break;
32218 case CPP_AND_AND: code = TRUTH_ANDIF_EXPR; break;
32219 case CPP_OR_OR: code = TRUTH_ORIF_EXPR; break;
32220 default: break;
32223 if (code != ERROR_MARK)
32224 cp_lexer_consume_token (parser->lexer);
32225 else
32227 bool saved_colon_corrects_to_scope_p;
32228 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
32229 parser->colon_corrects_to_scope_p = false;
32230 id = cp_parser_id_expression (parser, /*template_p=*/false,
32231 /*check_dependency_p=*/true,
32232 /*template_p=*/NULL,
32233 /*declarator_p=*/false,
32234 /*optional_p=*/false);
32235 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
32236 if (identifier_p (id))
32238 const char *p = IDENTIFIER_POINTER (id);
32240 if (strcmp (p, "min") == 0)
32241 code = MIN_EXPR;
32242 else if (strcmp (p, "max") == 0)
32243 code = MAX_EXPR;
32244 else if (id == cp_operator_id (PLUS_EXPR))
32245 code = PLUS_EXPR;
32246 else if (id == cp_operator_id (MULT_EXPR))
32247 code = MULT_EXPR;
32248 else if (id == cp_operator_id (MINUS_EXPR))
32249 code = MINUS_EXPR;
32250 else if (id == cp_operator_id (BIT_AND_EXPR))
32251 code = BIT_AND_EXPR;
32252 else if (id == cp_operator_id (BIT_IOR_EXPR))
32253 code = BIT_IOR_EXPR;
32254 else if (id == cp_operator_id (BIT_XOR_EXPR))
32255 code = BIT_XOR_EXPR;
32256 else if (id == cp_operator_id (TRUTH_ANDIF_EXPR))
32257 code = TRUTH_ANDIF_EXPR;
32258 else if (id == cp_operator_id (TRUTH_ORIF_EXPR))
32259 code = TRUTH_ORIF_EXPR;
32260 id = omp_reduction_id (code, id, NULL_TREE);
32261 tree scope = parser->scope;
32262 if (scope)
32263 id = build_qualified_name (NULL_TREE, scope, id, false);
32264 parser->scope = NULL_TREE;
32265 parser->qualifying_scope = NULL_TREE;
32266 parser->object_scope = NULL_TREE;
32268 else
32270 error ("invalid reduction-identifier");
32271 resync_fail:
32272 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32273 /*or_comma=*/false,
32274 /*consume_paren=*/true);
32275 return list;
32279 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32280 goto resync_fail;
32282 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list,
32283 NULL);
32284 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32286 OMP_CLAUSE_REDUCTION_CODE (c) = code;
32287 OMP_CLAUSE_REDUCTION_PLACEHOLDER (c) = id;
32290 return nlist;
32293 /* OpenMP 2.5:
32294 schedule ( schedule-kind )
32295 schedule ( schedule-kind , expression )
32297 schedule-kind:
32298 static | dynamic | guided | runtime | auto
32300 OpenMP 4.5:
32301 schedule ( schedule-modifier : schedule-kind )
32302 schedule ( schedule-modifier [ , schedule-modifier ] : schedule-kind , expression )
32304 schedule-modifier:
32305 simd
32306 monotonic
32307 nonmonotonic */
32309 static tree
32310 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
32312 tree c, t;
32313 int modifiers = 0, nmodifiers = 0;
32315 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32316 return list;
32318 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
32320 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32322 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32323 const char *p = IDENTIFIER_POINTER (id);
32324 if (strcmp ("simd", p) == 0)
32325 OMP_CLAUSE_SCHEDULE_SIMD (c) = 1;
32326 else if (strcmp ("monotonic", p) == 0)
32327 modifiers |= OMP_CLAUSE_SCHEDULE_MONOTONIC;
32328 else if (strcmp ("nonmonotonic", p) == 0)
32329 modifiers |= OMP_CLAUSE_SCHEDULE_NONMONOTONIC;
32330 else
32331 break;
32332 cp_lexer_consume_token (parser->lexer);
32333 if (nmodifiers++ == 0
32334 && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32335 cp_lexer_consume_token (parser->lexer);
32336 else
32338 cp_parser_require (parser, CPP_COLON, RT_COLON);
32339 break;
32343 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32345 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32346 const char *p = IDENTIFIER_POINTER (id);
32348 switch (p[0])
32350 case 'd':
32351 if (strcmp ("dynamic", p) != 0)
32352 goto invalid_kind;
32353 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
32354 break;
32356 case 'g':
32357 if (strcmp ("guided", p) != 0)
32358 goto invalid_kind;
32359 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
32360 break;
32362 case 'r':
32363 if (strcmp ("runtime", p) != 0)
32364 goto invalid_kind;
32365 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
32366 break;
32368 default:
32369 goto invalid_kind;
32372 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
32373 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
32374 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
32375 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
32376 else
32377 goto invalid_kind;
32378 cp_lexer_consume_token (parser->lexer);
32380 if ((modifiers & (OMP_CLAUSE_SCHEDULE_MONOTONIC
32381 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32382 == (OMP_CLAUSE_SCHEDULE_MONOTONIC
32383 | OMP_CLAUSE_SCHEDULE_NONMONOTONIC))
32385 error_at (location, "both %<monotonic%> and %<nonmonotonic%> modifiers "
32386 "specified");
32387 modifiers = 0;
32390 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
32392 cp_token *token;
32393 cp_lexer_consume_token (parser->lexer);
32395 token = cp_lexer_peek_token (parser->lexer);
32396 t = cp_parser_assignment_expression (parser);
32398 if (t == error_mark_node)
32399 goto resync_fail;
32400 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
32401 error_at (token->location, "schedule %<runtime%> does not take "
32402 "a %<chunk_size%> parameter");
32403 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
32404 error_at (token->location, "schedule %<auto%> does not take "
32405 "a %<chunk_size%> parameter");
32406 else
32407 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
32409 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32410 goto resync_fail;
32412 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
32413 goto resync_fail;
32415 OMP_CLAUSE_SCHEDULE_KIND (c)
32416 = (enum omp_clause_schedule_kind)
32417 (OMP_CLAUSE_SCHEDULE_KIND (c) | modifiers);
32419 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
32420 OMP_CLAUSE_CHAIN (c) = list;
32421 return c;
32423 invalid_kind:
32424 cp_parser_error (parser, "invalid schedule kind");
32425 resync_fail:
32426 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32427 /*or_comma=*/false,
32428 /*consume_paren=*/true);
32429 return list;
32432 /* OpenMP 3.0:
32433 untied */
32435 static tree
32436 cp_parser_omp_clause_untied (cp_parser * /*parser*/,
32437 tree list, location_t location)
32439 tree c;
32441 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
32443 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
32444 OMP_CLAUSE_CHAIN (c) = list;
32445 return c;
32448 /* OpenMP 4.0:
32449 inbranch
32450 notinbranch */
32452 static tree
32453 cp_parser_omp_clause_branch (cp_parser * /*parser*/, enum omp_clause_code code,
32454 tree list, location_t location)
32456 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32457 tree c = build_omp_clause (location, code);
32458 OMP_CLAUSE_CHAIN (c) = list;
32459 return c;
32462 /* OpenMP 4.0:
32463 parallel
32465 sections
32466 taskgroup */
32468 static tree
32469 cp_parser_omp_clause_cancelkind (cp_parser * /*parser*/,
32470 enum omp_clause_code code,
32471 tree list, location_t location)
32473 tree c = build_omp_clause (location, code);
32474 OMP_CLAUSE_CHAIN (c) = list;
32475 return c;
32478 /* OpenMP 4.5:
32479 nogroup */
32481 static tree
32482 cp_parser_omp_clause_nogroup (cp_parser * /*parser*/,
32483 tree list, location_t location)
32485 check_no_duplicate_clause (list, OMP_CLAUSE_NOGROUP, "nogroup", location);
32486 tree c = build_omp_clause (location, OMP_CLAUSE_NOGROUP);
32487 OMP_CLAUSE_CHAIN (c) = list;
32488 return c;
32491 /* OpenMP 4.5:
32492 simd
32493 threads */
32495 static tree
32496 cp_parser_omp_clause_orderedkind (cp_parser * /*parser*/,
32497 enum omp_clause_code code,
32498 tree list, location_t location)
32500 check_no_duplicate_clause (list, code, omp_clause_code_name[code], location);
32501 tree c = build_omp_clause (location, code);
32502 OMP_CLAUSE_CHAIN (c) = list;
32503 return c;
32506 /* OpenMP 4.0:
32507 num_teams ( expression ) */
32509 static tree
32510 cp_parser_omp_clause_num_teams (cp_parser *parser, tree list,
32511 location_t location)
32513 tree t, c;
32515 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32516 return list;
32518 t = cp_parser_expression (parser);
32520 if (t == error_mark_node
32521 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32522 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32523 /*or_comma=*/false,
32524 /*consume_paren=*/true);
32526 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_TEAMS,
32527 "num_teams", location);
32529 c = build_omp_clause (location, OMP_CLAUSE_NUM_TEAMS);
32530 OMP_CLAUSE_NUM_TEAMS_EXPR (c) = t;
32531 OMP_CLAUSE_CHAIN (c) = list;
32533 return c;
32536 /* OpenMP 4.0:
32537 thread_limit ( expression ) */
32539 static tree
32540 cp_parser_omp_clause_thread_limit (cp_parser *parser, tree list,
32541 location_t location)
32543 tree t, c;
32545 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32546 return list;
32548 t = cp_parser_expression (parser);
32550 if (t == error_mark_node
32551 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32552 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32553 /*or_comma=*/false,
32554 /*consume_paren=*/true);
32556 check_no_duplicate_clause (list, OMP_CLAUSE_THREAD_LIMIT,
32557 "thread_limit", location);
32559 c = build_omp_clause (location, OMP_CLAUSE_THREAD_LIMIT);
32560 OMP_CLAUSE_THREAD_LIMIT_EXPR (c) = t;
32561 OMP_CLAUSE_CHAIN (c) = list;
32563 return c;
32566 /* OpenMP 4.0:
32567 aligned ( variable-list )
32568 aligned ( variable-list : constant-expression ) */
32570 static tree
32571 cp_parser_omp_clause_aligned (cp_parser *parser, tree list)
32573 tree nlist, c, alignment = NULL_TREE;
32574 bool colon;
32576 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32577 return list;
32579 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_ALIGNED, list,
32580 &colon);
32582 if (colon)
32584 alignment = cp_parser_constant_expression (parser);
32586 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32587 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32588 /*or_comma=*/false,
32589 /*consume_paren=*/true);
32591 if (alignment == error_mark_node)
32592 alignment = NULL_TREE;
32595 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32596 OMP_CLAUSE_ALIGNED_ALIGNMENT (c) = alignment;
32598 return nlist;
32601 /* OpenMP 4.0:
32602 linear ( variable-list )
32603 linear ( variable-list : expression )
32605 OpenMP 4.5:
32606 linear ( modifier ( variable-list ) )
32607 linear ( modifier ( variable-list ) : expression ) */
32609 static tree
32610 cp_parser_omp_clause_linear (cp_parser *parser, tree list,
32611 bool is_cilk_simd_fn, bool declare_simd)
32613 tree nlist, c, step = integer_one_node;
32614 bool colon;
32615 enum omp_clause_linear_kind kind = OMP_CLAUSE_LINEAR_DEFAULT;
32617 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32618 return list;
32620 if (!is_cilk_simd_fn
32621 && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32623 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32624 const char *p = IDENTIFIER_POINTER (id);
32626 if (strcmp ("ref", p) == 0)
32627 kind = OMP_CLAUSE_LINEAR_REF;
32628 else if (strcmp ("val", p) == 0)
32629 kind = OMP_CLAUSE_LINEAR_VAL;
32630 else if (strcmp ("uval", p) == 0)
32631 kind = OMP_CLAUSE_LINEAR_UVAL;
32632 if (cp_lexer_nth_token_is (parser->lexer, 2, CPP_OPEN_PAREN))
32633 cp_lexer_consume_token (parser->lexer);
32634 else
32635 kind = OMP_CLAUSE_LINEAR_DEFAULT;
32638 if (kind == OMP_CLAUSE_LINEAR_DEFAULT)
32639 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_LINEAR, list,
32640 &colon);
32641 else
32643 nlist = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINEAR, list);
32644 colon = cp_lexer_next_token_is (parser->lexer, CPP_COLON);
32645 if (colon)
32646 cp_parser_require (parser, CPP_COLON, RT_COLON);
32647 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32648 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32649 /*or_comma=*/false,
32650 /*consume_paren=*/true);
32653 if (colon)
32655 step = NULL_TREE;
32656 if (declare_simd
32657 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32658 && cp_lexer_nth_token_is (parser->lexer, 2, CPP_CLOSE_PAREN))
32660 cp_token *token = cp_lexer_peek_token (parser->lexer);
32661 cp_parser_parse_tentatively (parser);
32662 step = cp_parser_id_expression (parser, /*template_p=*/false,
32663 /*check_dependency_p=*/true,
32664 /*template_p=*/NULL,
32665 /*declarator_p=*/false,
32666 /*optional_p=*/false);
32667 if (step != error_mark_node)
32668 step = cp_parser_lookup_name_simple (parser, step, token->location);
32669 if (step == error_mark_node)
32671 step = NULL_TREE;
32672 cp_parser_abort_tentative_parse (parser);
32674 else if (!cp_parser_parse_definitely (parser))
32675 step = NULL_TREE;
32677 if (!step)
32678 step = cp_parser_expression (parser);
32680 if (is_cilk_simd_fn && TREE_CODE (step) == PARM_DECL)
32682 sorry ("using parameters for %<linear%> step is not supported yet");
32683 step = integer_one_node;
32685 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32686 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32687 /*or_comma=*/false,
32688 /*consume_paren=*/true);
32690 if (step == error_mark_node)
32691 return list;
32694 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32696 OMP_CLAUSE_LINEAR_STEP (c) = step;
32697 OMP_CLAUSE_LINEAR_KIND (c) = kind;
32700 return nlist;
32703 /* OpenMP 4.0:
32704 safelen ( constant-expression ) */
32706 static tree
32707 cp_parser_omp_clause_safelen (cp_parser *parser, tree list,
32708 location_t location)
32710 tree t, c;
32712 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32713 return list;
32715 t = cp_parser_constant_expression (parser);
32717 if (t == error_mark_node
32718 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32719 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32720 /*or_comma=*/false,
32721 /*consume_paren=*/true);
32723 check_no_duplicate_clause (list, OMP_CLAUSE_SAFELEN, "safelen", location);
32725 c = build_omp_clause (location, OMP_CLAUSE_SAFELEN);
32726 OMP_CLAUSE_SAFELEN_EXPR (c) = t;
32727 OMP_CLAUSE_CHAIN (c) = list;
32729 return c;
32732 /* OpenMP 4.0:
32733 simdlen ( constant-expression ) */
32735 static tree
32736 cp_parser_omp_clause_simdlen (cp_parser *parser, tree list,
32737 location_t location)
32739 tree t, c;
32741 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32742 return list;
32744 t = cp_parser_constant_expression (parser);
32746 if (t == error_mark_node
32747 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32748 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32749 /*or_comma=*/false,
32750 /*consume_paren=*/true);
32752 check_no_duplicate_clause (list, OMP_CLAUSE_SIMDLEN, "simdlen", location);
32754 c = build_omp_clause (location, OMP_CLAUSE_SIMDLEN);
32755 OMP_CLAUSE_SIMDLEN_EXPR (c) = t;
32756 OMP_CLAUSE_CHAIN (c) = list;
32758 return c;
32761 /* OpenMP 4.5:
32762 vec:
32763 identifier [+/- integer]
32764 vec , identifier [+/- integer]
32767 static tree
32768 cp_parser_omp_clause_depend_sink (cp_parser *parser, location_t clause_loc,
32769 tree list)
32771 tree vec = NULL;
32773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
32775 cp_parser_error (parser, "expected identifier");
32776 return list;
32779 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32781 location_t id_loc = cp_lexer_peek_token (parser->lexer)->location;
32782 tree t, identifier = cp_parser_identifier (parser);
32783 tree addend = NULL;
32785 if (identifier == error_mark_node)
32786 t = error_mark_node;
32787 else
32789 t = cp_parser_lookup_name_simple
32790 (parser, identifier,
32791 cp_lexer_peek_token (parser->lexer)->location);
32792 if (t == error_mark_node)
32793 cp_parser_name_lookup_error (parser, identifier, t, NLE_NULL,
32794 id_loc);
32797 bool neg = false;
32798 if (cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
32799 neg = true;
32800 else if (!cp_lexer_next_token_is (parser->lexer, CPP_PLUS))
32802 addend = integer_zero_node;
32803 goto add_to_vector;
32805 cp_lexer_consume_token (parser->lexer);
32807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NUMBER))
32809 cp_parser_error (parser, "expected integer");
32810 return list;
32813 addend = cp_lexer_peek_token (parser->lexer)->u.value;
32814 if (TREE_CODE (addend) != INTEGER_CST)
32816 cp_parser_error (parser, "expected integer");
32817 return list;
32819 cp_lexer_consume_token (parser->lexer);
32821 add_to_vector:
32822 if (t != error_mark_node)
32824 vec = tree_cons (addend, t, vec);
32825 if (neg)
32826 OMP_CLAUSE_DEPEND_SINK_NEGATIVE (vec) = 1;
32829 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
32830 break;
32832 cp_lexer_consume_token (parser->lexer);
32835 if (cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN) && vec)
32837 tree u = build_omp_clause (clause_loc, OMP_CLAUSE_DEPEND);
32838 OMP_CLAUSE_DEPEND_KIND (u) = OMP_CLAUSE_DEPEND_SINK;
32839 OMP_CLAUSE_DECL (u) = nreverse (vec);
32840 OMP_CLAUSE_CHAIN (u) = list;
32841 return u;
32843 return list;
32846 /* OpenMP 4.0:
32847 depend ( depend-kind : variable-list )
32849 depend-kind:
32850 in | out | inout
32852 OpenMP 4.5:
32853 depend ( source )
32855 depend ( sink : vec ) */
32857 static tree
32858 cp_parser_omp_clause_depend (cp_parser *parser, tree list, location_t loc)
32860 tree nlist, c;
32861 enum omp_clause_depend_kind kind = OMP_CLAUSE_DEPEND_INOUT;
32863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32864 return list;
32866 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32868 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32869 const char *p = IDENTIFIER_POINTER (id);
32871 if (strcmp ("in", p) == 0)
32872 kind = OMP_CLAUSE_DEPEND_IN;
32873 else if (strcmp ("inout", p) == 0)
32874 kind = OMP_CLAUSE_DEPEND_INOUT;
32875 else if (strcmp ("out", p) == 0)
32876 kind = OMP_CLAUSE_DEPEND_OUT;
32877 else if (strcmp ("source", p) == 0)
32878 kind = OMP_CLAUSE_DEPEND_SOURCE;
32879 else if (strcmp ("sink", p) == 0)
32880 kind = OMP_CLAUSE_DEPEND_SINK;
32881 else
32882 goto invalid_kind;
32884 else
32885 goto invalid_kind;
32887 cp_lexer_consume_token (parser->lexer);
32889 if (kind == OMP_CLAUSE_DEPEND_SOURCE)
32891 c = build_omp_clause (loc, OMP_CLAUSE_DEPEND);
32892 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32893 OMP_CLAUSE_DECL (c) = NULL_TREE;
32894 OMP_CLAUSE_CHAIN (c) = list;
32895 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
32896 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32897 /*or_comma=*/false,
32898 /*consume_paren=*/true);
32899 return c;
32902 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
32903 goto resync_fail;
32905 if (kind == OMP_CLAUSE_DEPEND_SINK)
32906 nlist = cp_parser_omp_clause_depend_sink (parser, loc, list);
32907 else
32909 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_DEPEND,
32910 list, NULL);
32912 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
32913 OMP_CLAUSE_DEPEND_KIND (c) = kind;
32915 return nlist;
32917 invalid_kind:
32918 cp_parser_error (parser, "invalid depend kind");
32919 resync_fail:
32920 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32921 /*or_comma=*/false,
32922 /*consume_paren=*/true);
32923 return list;
32926 /* OpenMP 4.0:
32927 map ( map-kind : variable-list )
32928 map ( variable-list )
32930 map-kind:
32931 alloc | to | from | tofrom
32933 OpenMP 4.5:
32934 map-kind:
32935 alloc | to | from | tofrom | release | delete
32937 map ( always [,] map-kind: variable-list ) */
32939 static tree
32940 cp_parser_omp_clause_map (cp_parser *parser, tree list)
32942 tree nlist, c;
32943 enum gomp_map_kind kind = GOMP_MAP_TOFROM;
32944 bool always = false;
32946 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
32947 return list;
32949 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
32951 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32952 const char *p = IDENTIFIER_POINTER (id);
32954 if (strcmp ("always", p) == 0)
32956 int nth = 2;
32957 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COMMA)
32958 nth++;
32959 if ((cp_lexer_peek_nth_token (parser->lexer, nth)->type == CPP_NAME
32960 || (cp_lexer_peek_nth_token (parser->lexer, nth)->keyword
32961 == RID_DELETE))
32962 && (cp_lexer_peek_nth_token (parser->lexer, nth + 1)->type
32963 == CPP_COLON))
32965 always = true;
32966 cp_lexer_consume_token (parser->lexer);
32967 if (nth == 3)
32968 cp_lexer_consume_token (parser->lexer);
32973 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
32974 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
32976 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
32977 const char *p = IDENTIFIER_POINTER (id);
32979 if (strcmp ("alloc", p) == 0)
32980 kind = GOMP_MAP_ALLOC;
32981 else if (strcmp ("to", p) == 0)
32982 kind = always ? GOMP_MAP_ALWAYS_TO : GOMP_MAP_TO;
32983 else if (strcmp ("from", p) == 0)
32984 kind = always ? GOMP_MAP_ALWAYS_FROM : GOMP_MAP_FROM;
32985 else if (strcmp ("tofrom", p) == 0)
32986 kind = always ? GOMP_MAP_ALWAYS_TOFROM : GOMP_MAP_TOFROM;
32987 else if (strcmp ("release", p) == 0)
32988 kind = GOMP_MAP_RELEASE;
32989 else
32991 cp_parser_error (parser, "invalid map kind");
32992 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
32993 /*or_comma=*/false,
32994 /*consume_paren=*/true);
32995 return list;
32997 cp_lexer_consume_token (parser->lexer);
32998 cp_lexer_consume_token (parser->lexer);
33000 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DELETE)
33001 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
33003 kind = GOMP_MAP_DELETE;
33004 cp_lexer_consume_token (parser->lexer);
33005 cp_lexer_consume_token (parser->lexer);
33008 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_MAP, list,
33009 NULL);
33011 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
33012 OMP_CLAUSE_SET_MAP_KIND (c, kind);
33014 return nlist;
33017 /* OpenMP 4.0:
33018 device ( expression ) */
33020 static tree
33021 cp_parser_omp_clause_device (cp_parser *parser, tree list,
33022 location_t location)
33024 tree t, c;
33026 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33027 return list;
33029 t = cp_parser_expression (parser);
33031 if (t == error_mark_node
33032 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33033 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33034 /*or_comma=*/false,
33035 /*consume_paren=*/true);
33037 check_no_duplicate_clause (list, OMP_CLAUSE_DEVICE,
33038 "device", location);
33040 c = build_omp_clause (location, OMP_CLAUSE_DEVICE);
33041 OMP_CLAUSE_DEVICE_ID (c) = t;
33042 OMP_CLAUSE_CHAIN (c) = list;
33044 return c;
33047 /* OpenMP 4.0:
33048 dist_schedule ( static )
33049 dist_schedule ( static , expression ) */
33051 static tree
33052 cp_parser_omp_clause_dist_schedule (cp_parser *parser, tree list,
33053 location_t location)
33055 tree c, t;
33057 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33058 return list;
33060 c = build_omp_clause (location, OMP_CLAUSE_DIST_SCHEDULE);
33062 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
33063 goto invalid_kind;
33064 cp_lexer_consume_token (parser->lexer);
33066 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33068 cp_lexer_consume_token (parser->lexer);
33070 t = cp_parser_assignment_expression (parser);
33072 if (t == error_mark_node)
33073 goto resync_fail;
33074 OMP_CLAUSE_DIST_SCHEDULE_CHUNK_EXPR (c) = t;
33076 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33077 goto resync_fail;
33079 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33080 goto resync_fail;
33082 check_no_duplicate_clause (list, OMP_CLAUSE_DIST_SCHEDULE, "dist_schedule",
33083 location);
33084 OMP_CLAUSE_CHAIN (c) = list;
33085 return c;
33087 invalid_kind:
33088 cp_parser_error (parser, "invalid dist_schedule kind");
33089 resync_fail:
33090 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33091 /*or_comma=*/false,
33092 /*consume_paren=*/true);
33093 return list;
33096 /* OpenMP 4.0:
33097 proc_bind ( proc-bind-kind )
33099 proc-bind-kind:
33100 master | close | spread */
33102 static tree
33103 cp_parser_omp_clause_proc_bind (cp_parser *parser, tree list,
33104 location_t location)
33106 tree c;
33107 enum omp_clause_proc_bind_kind kind;
33109 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
33110 return list;
33112 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33114 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33115 const char *p = IDENTIFIER_POINTER (id);
33117 if (strcmp ("master", p) == 0)
33118 kind = OMP_CLAUSE_PROC_BIND_MASTER;
33119 else if (strcmp ("close", p) == 0)
33120 kind = OMP_CLAUSE_PROC_BIND_CLOSE;
33121 else if (strcmp ("spread", p) == 0)
33122 kind = OMP_CLAUSE_PROC_BIND_SPREAD;
33123 else
33124 goto invalid_kind;
33126 else
33127 goto invalid_kind;
33129 cp_lexer_consume_token (parser->lexer);
33130 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
33131 goto resync_fail;
33133 c = build_omp_clause (location, OMP_CLAUSE_PROC_BIND);
33134 check_no_duplicate_clause (list, OMP_CLAUSE_PROC_BIND, "proc_bind",
33135 location);
33136 OMP_CLAUSE_PROC_BIND_KIND (c) = kind;
33137 OMP_CLAUSE_CHAIN (c) = list;
33138 return c;
33140 invalid_kind:
33141 cp_parser_error (parser, "invalid depend kind");
33142 resync_fail:
33143 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33144 /*or_comma=*/false,
33145 /*consume_paren=*/true);
33146 return list;
33149 /* OpenACC:
33150 async [( int-expr )] */
33152 static tree
33153 cp_parser_oacc_clause_async (cp_parser *parser, tree list)
33155 tree c, t;
33156 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
33158 t = build_int_cst (integer_type_node, GOMP_ASYNC_NOVAL);
33160 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
33162 cp_lexer_consume_token (parser->lexer);
33164 t = cp_parser_expression (parser);
33165 if (t == error_mark_node
33166 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
33167 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
33168 /*or_comma=*/false,
33169 /*consume_paren=*/true);
33172 check_no_duplicate_clause (list, OMP_CLAUSE_ASYNC, "async", loc);
33174 c = build_omp_clause (loc, OMP_CLAUSE_ASYNC);
33175 OMP_CLAUSE_ASYNC_EXPR (c) = t;
33176 OMP_CLAUSE_CHAIN (c) = list;
33177 list = c;
33179 return list;
33182 /* Parse all OpenACC clauses. The set clauses allowed by the directive
33183 is a bitmask in MASK. Return the list of clauses found. */
33185 static tree
33186 cp_parser_oacc_all_clauses (cp_parser *parser, omp_clause_mask mask,
33187 const char *where, cp_token *pragma_tok,
33188 bool finish_p = true)
33190 tree clauses = NULL;
33191 bool first = true;
33193 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33195 location_t here;
33196 pragma_omp_clause c_kind;
33197 omp_clause_code code;
33198 const char *c_name;
33199 tree prev = clauses;
33201 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33202 cp_lexer_consume_token (parser->lexer);
33204 here = cp_lexer_peek_token (parser->lexer)->location;
33205 c_kind = cp_parser_omp_clause_name (parser);
33207 switch (c_kind)
33209 case PRAGMA_OACC_CLAUSE_ASYNC:
33210 clauses = cp_parser_oacc_clause_async (parser, clauses);
33211 c_name = "async";
33212 break;
33213 case PRAGMA_OACC_CLAUSE_AUTO:
33214 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_AUTO,
33215 clauses, here);
33216 c_name = "auto";
33217 break;
33218 case PRAGMA_OACC_CLAUSE_COLLAPSE:
33219 clauses = cp_parser_omp_clause_collapse (parser, clauses, here);
33220 c_name = "collapse";
33221 break;
33222 case PRAGMA_OACC_CLAUSE_COPY:
33223 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33224 c_name = "copy";
33225 break;
33226 case PRAGMA_OACC_CLAUSE_COPYIN:
33227 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33228 c_name = "copyin";
33229 break;
33230 case PRAGMA_OACC_CLAUSE_COPYOUT:
33231 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33232 c_name = "copyout";
33233 break;
33234 case PRAGMA_OACC_CLAUSE_CREATE:
33235 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33236 c_name = "create";
33237 break;
33238 case PRAGMA_OACC_CLAUSE_DELETE:
33239 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33240 c_name = "delete";
33241 break;
33242 case PRAGMA_OMP_CLAUSE_DEFAULT:
33243 clauses = cp_parser_omp_clause_default (parser, clauses, here, true);
33244 c_name = "default";
33245 break;
33246 case PRAGMA_OACC_CLAUSE_DEVICE:
33247 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33248 c_name = "device";
33249 break;
33250 case PRAGMA_OACC_CLAUSE_DEVICEPTR:
33251 clauses = cp_parser_oacc_data_clause_deviceptr (parser, clauses);
33252 c_name = "deviceptr";
33253 break;
33254 case PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT:
33255 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33256 c_name = "device_resident";
33257 break;
33258 case PRAGMA_OACC_CLAUSE_FIRSTPRIVATE:
33259 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33260 clauses);
33261 c_name = "firstprivate";
33262 break;
33263 case PRAGMA_OACC_CLAUSE_GANG:
33264 c_name = "gang";
33265 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_GANG,
33266 c_name, clauses);
33267 break;
33268 case PRAGMA_OACC_CLAUSE_HOST:
33269 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33270 c_name = "host";
33271 break;
33272 case PRAGMA_OACC_CLAUSE_IF:
33273 clauses = cp_parser_omp_clause_if (parser, clauses, here, false);
33274 c_name = "if";
33275 break;
33276 case PRAGMA_OACC_CLAUSE_INDEPENDENT:
33277 clauses = cp_parser_oacc_simple_clause (parser,
33278 OMP_CLAUSE_INDEPENDENT,
33279 clauses, here);
33280 c_name = "independent";
33281 break;
33282 case PRAGMA_OACC_CLAUSE_LINK:
33283 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33284 c_name = "link";
33285 break;
33286 case PRAGMA_OACC_CLAUSE_NUM_GANGS:
33287 code = OMP_CLAUSE_NUM_GANGS;
33288 c_name = "num_gangs";
33289 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33290 clauses);
33291 break;
33292 case PRAGMA_OACC_CLAUSE_NUM_WORKERS:
33293 c_name = "num_workers";
33294 code = OMP_CLAUSE_NUM_WORKERS;
33295 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33296 clauses);
33297 break;
33298 case PRAGMA_OACC_CLAUSE_PRESENT:
33299 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33300 c_name = "present";
33301 break;
33302 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY:
33303 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33304 c_name = "present_or_copy";
33305 break;
33306 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN:
33307 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33308 c_name = "present_or_copyin";
33309 break;
33310 case PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT:
33311 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33312 c_name = "present_or_copyout";
33313 break;
33314 case PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE:
33315 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33316 c_name = "present_or_create";
33317 break;
33318 case PRAGMA_OACC_CLAUSE_PRIVATE:
33319 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33320 clauses);
33321 c_name = "private";
33322 break;
33323 case PRAGMA_OACC_CLAUSE_REDUCTION:
33324 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33325 c_name = "reduction";
33326 break;
33327 case PRAGMA_OACC_CLAUSE_SELF:
33328 clauses = cp_parser_oacc_data_clause (parser, c_kind, clauses);
33329 c_name = "self";
33330 break;
33331 case PRAGMA_OACC_CLAUSE_SEQ:
33332 clauses = cp_parser_oacc_simple_clause (parser, OMP_CLAUSE_SEQ,
33333 clauses, here);
33334 c_name = "seq";
33335 break;
33336 case PRAGMA_OACC_CLAUSE_TILE:
33337 clauses = cp_parser_oacc_clause_tile (parser, here, clauses);
33338 c_name = "tile";
33339 break;
33340 case PRAGMA_OACC_CLAUSE_USE_DEVICE:
33341 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33342 clauses);
33343 c_name = "use_device";
33344 break;
33345 case PRAGMA_OACC_CLAUSE_VECTOR:
33346 c_name = "vector";
33347 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_VECTOR,
33348 c_name, clauses);
33349 break;
33350 case PRAGMA_OACC_CLAUSE_VECTOR_LENGTH:
33351 c_name = "vector_length";
33352 code = OMP_CLAUSE_VECTOR_LENGTH;
33353 clauses = cp_parser_oacc_single_int_clause (parser, code, c_name,
33354 clauses);
33355 break;
33356 case PRAGMA_OACC_CLAUSE_WAIT:
33357 clauses = cp_parser_oacc_clause_wait (parser, clauses);
33358 c_name = "wait";
33359 break;
33360 case PRAGMA_OACC_CLAUSE_WORKER:
33361 c_name = "worker";
33362 clauses = cp_parser_oacc_shape_clause (parser, OMP_CLAUSE_WORKER,
33363 c_name, clauses);
33364 break;
33365 default:
33366 cp_parser_error (parser, "expected %<#pragma acc%> clause");
33367 goto saw_error;
33370 first = false;
33372 if (((mask >> c_kind) & 1) == 0)
33374 /* Remove the invalid clause(s) from the list to avoid
33375 confusing the rest of the compiler. */
33376 clauses = prev;
33377 error_at (here, "%qs is not valid for %qs", c_name, where);
33381 saw_error:
33382 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33384 if (finish_p)
33385 return finish_omp_clauses (clauses, C_ORT_ACC);
33387 return clauses;
33390 /* Parse all OpenMP clauses. The set clauses allowed by the directive
33391 is a bitmask in MASK. Return the list of clauses found; the result
33392 of clause default goes in *pdefault. */
33394 static tree
33395 cp_parser_omp_all_clauses (cp_parser *parser, omp_clause_mask mask,
33396 const char *where, cp_token *pragma_tok,
33397 bool finish_p = true)
33399 tree clauses = NULL;
33400 bool first = true;
33401 cp_token *token = NULL;
33403 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
33405 pragma_omp_clause c_kind;
33406 const char *c_name;
33407 tree prev = clauses;
33409 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
33410 cp_lexer_consume_token (parser->lexer);
33412 token = cp_lexer_peek_token (parser->lexer);
33413 c_kind = cp_parser_omp_clause_name (parser);
33415 switch (c_kind)
33417 case PRAGMA_OMP_CLAUSE_COLLAPSE:
33418 clauses = cp_parser_omp_clause_collapse (parser, clauses,
33419 token->location);
33420 c_name = "collapse";
33421 break;
33422 case PRAGMA_OMP_CLAUSE_COPYIN:
33423 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
33424 c_name = "copyin";
33425 break;
33426 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
33427 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
33428 clauses);
33429 c_name = "copyprivate";
33430 break;
33431 case PRAGMA_OMP_CLAUSE_DEFAULT:
33432 clauses = cp_parser_omp_clause_default (parser, clauses,
33433 token->location, false);
33434 c_name = "default";
33435 break;
33436 case PRAGMA_OMP_CLAUSE_FINAL:
33437 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
33438 c_name = "final";
33439 break;
33440 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
33441 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
33442 clauses);
33443 c_name = "firstprivate";
33444 break;
33445 case PRAGMA_OMP_CLAUSE_GRAINSIZE:
33446 clauses = cp_parser_omp_clause_grainsize (parser, clauses,
33447 token->location);
33448 c_name = "grainsize";
33449 break;
33450 case PRAGMA_OMP_CLAUSE_HINT:
33451 clauses = cp_parser_omp_clause_hint (parser, clauses,
33452 token->location);
33453 c_name = "hint";
33454 break;
33455 case PRAGMA_OMP_CLAUSE_DEFAULTMAP:
33456 clauses = cp_parser_omp_clause_defaultmap (parser, clauses,
33457 token->location);
33458 c_name = "defaultmap";
33459 break;
33460 case PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR:
33461 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_USE_DEVICE_PTR,
33462 clauses);
33463 c_name = "use_device_ptr";
33464 break;
33465 case PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR:
33466 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_IS_DEVICE_PTR,
33467 clauses);
33468 c_name = "is_device_ptr";
33469 break;
33470 case PRAGMA_OMP_CLAUSE_IF:
33471 clauses = cp_parser_omp_clause_if (parser, clauses, token->location,
33472 true);
33473 c_name = "if";
33474 break;
33475 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
33476 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
33477 clauses);
33478 c_name = "lastprivate";
33479 break;
33480 case PRAGMA_OMP_CLAUSE_MERGEABLE:
33481 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
33482 token->location);
33483 c_name = "mergeable";
33484 break;
33485 case PRAGMA_OMP_CLAUSE_NOWAIT:
33486 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
33487 c_name = "nowait";
33488 break;
33489 case PRAGMA_OMP_CLAUSE_NUM_TASKS:
33490 clauses = cp_parser_omp_clause_num_tasks (parser, clauses,
33491 token->location);
33492 c_name = "num_tasks";
33493 break;
33494 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
33495 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
33496 token->location);
33497 c_name = "num_threads";
33498 break;
33499 case PRAGMA_OMP_CLAUSE_ORDERED:
33500 clauses = cp_parser_omp_clause_ordered (parser, clauses,
33501 token->location);
33502 c_name = "ordered";
33503 break;
33504 case PRAGMA_OMP_CLAUSE_PRIORITY:
33505 clauses = cp_parser_omp_clause_priority (parser, clauses,
33506 token->location);
33507 c_name = "priority";
33508 break;
33509 case PRAGMA_OMP_CLAUSE_PRIVATE:
33510 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
33511 clauses);
33512 c_name = "private";
33513 break;
33514 case PRAGMA_OMP_CLAUSE_REDUCTION:
33515 clauses = cp_parser_omp_clause_reduction (parser, clauses);
33516 c_name = "reduction";
33517 break;
33518 case PRAGMA_OMP_CLAUSE_SCHEDULE:
33519 clauses = cp_parser_omp_clause_schedule (parser, clauses,
33520 token->location);
33521 c_name = "schedule";
33522 break;
33523 case PRAGMA_OMP_CLAUSE_SHARED:
33524 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
33525 clauses);
33526 c_name = "shared";
33527 break;
33528 case PRAGMA_OMP_CLAUSE_UNTIED:
33529 clauses = cp_parser_omp_clause_untied (parser, clauses,
33530 token->location);
33531 c_name = "untied";
33532 break;
33533 case PRAGMA_OMP_CLAUSE_INBRANCH:
33534 case PRAGMA_CILK_CLAUSE_MASK:
33535 clauses = cp_parser_omp_clause_branch (parser, OMP_CLAUSE_INBRANCH,
33536 clauses, token->location);
33537 c_name = "inbranch";
33538 break;
33539 case PRAGMA_OMP_CLAUSE_NOTINBRANCH:
33540 case PRAGMA_CILK_CLAUSE_NOMASK:
33541 clauses = cp_parser_omp_clause_branch (parser,
33542 OMP_CLAUSE_NOTINBRANCH,
33543 clauses, token->location);
33544 c_name = "notinbranch";
33545 break;
33546 case PRAGMA_OMP_CLAUSE_PARALLEL:
33547 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_PARALLEL,
33548 clauses, token->location);
33549 c_name = "parallel";
33550 if (!first)
33552 clause_not_first:
33553 error_at (token->location, "%qs must be the first clause of %qs",
33554 c_name, where);
33555 clauses = prev;
33557 break;
33558 case PRAGMA_OMP_CLAUSE_FOR:
33559 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_FOR,
33560 clauses, token->location);
33561 c_name = "for";
33562 if (!first)
33563 goto clause_not_first;
33564 break;
33565 case PRAGMA_OMP_CLAUSE_SECTIONS:
33566 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_SECTIONS,
33567 clauses, token->location);
33568 c_name = "sections";
33569 if (!first)
33570 goto clause_not_first;
33571 break;
33572 case PRAGMA_OMP_CLAUSE_TASKGROUP:
33573 clauses = cp_parser_omp_clause_cancelkind (parser, OMP_CLAUSE_TASKGROUP,
33574 clauses, token->location);
33575 c_name = "taskgroup";
33576 if (!first)
33577 goto clause_not_first;
33578 break;
33579 case PRAGMA_OMP_CLAUSE_LINK:
33580 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LINK, clauses);
33581 c_name = "to";
33582 break;
33583 case PRAGMA_OMP_CLAUSE_TO:
33584 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK)) != 0)
33585 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
33586 clauses);
33587 else
33588 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO, clauses);
33589 c_name = "to";
33590 break;
33591 case PRAGMA_OMP_CLAUSE_FROM:
33592 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FROM, clauses);
33593 c_name = "from";
33594 break;
33595 case PRAGMA_OMP_CLAUSE_UNIFORM:
33596 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_UNIFORM,
33597 clauses);
33598 c_name = "uniform";
33599 break;
33600 case PRAGMA_OMP_CLAUSE_NUM_TEAMS:
33601 clauses = cp_parser_omp_clause_num_teams (parser, clauses,
33602 token->location);
33603 c_name = "num_teams";
33604 break;
33605 case PRAGMA_OMP_CLAUSE_THREAD_LIMIT:
33606 clauses = cp_parser_omp_clause_thread_limit (parser, clauses,
33607 token->location);
33608 c_name = "thread_limit";
33609 break;
33610 case PRAGMA_OMP_CLAUSE_ALIGNED:
33611 clauses = cp_parser_omp_clause_aligned (parser, clauses);
33612 c_name = "aligned";
33613 break;
33614 case PRAGMA_OMP_CLAUSE_LINEAR:
33616 bool cilk_simd_fn = false, declare_simd = false;
33617 if (((mask >> PRAGMA_CILK_CLAUSE_VECTORLENGTH) & 1) != 0)
33618 cilk_simd_fn = true;
33619 else if (((mask >> PRAGMA_OMP_CLAUSE_UNIFORM) & 1) != 0)
33620 declare_simd = true;
33621 clauses = cp_parser_omp_clause_linear (parser, clauses,
33622 cilk_simd_fn, declare_simd);
33624 c_name = "linear";
33625 break;
33626 case PRAGMA_OMP_CLAUSE_DEPEND:
33627 clauses = cp_parser_omp_clause_depend (parser, clauses,
33628 token->location);
33629 c_name = "depend";
33630 break;
33631 case PRAGMA_OMP_CLAUSE_MAP:
33632 clauses = cp_parser_omp_clause_map (parser, clauses);
33633 c_name = "map";
33634 break;
33635 case PRAGMA_OMP_CLAUSE_DEVICE:
33636 clauses = cp_parser_omp_clause_device (parser, clauses,
33637 token->location);
33638 c_name = "device";
33639 break;
33640 case PRAGMA_OMP_CLAUSE_DIST_SCHEDULE:
33641 clauses = cp_parser_omp_clause_dist_schedule (parser, clauses,
33642 token->location);
33643 c_name = "dist_schedule";
33644 break;
33645 case PRAGMA_OMP_CLAUSE_PROC_BIND:
33646 clauses = cp_parser_omp_clause_proc_bind (parser, clauses,
33647 token->location);
33648 c_name = "proc_bind";
33649 break;
33650 case PRAGMA_OMP_CLAUSE_SAFELEN:
33651 clauses = cp_parser_omp_clause_safelen (parser, clauses,
33652 token->location);
33653 c_name = "safelen";
33654 break;
33655 case PRAGMA_OMP_CLAUSE_SIMDLEN:
33656 clauses = cp_parser_omp_clause_simdlen (parser, clauses,
33657 token->location);
33658 c_name = "simdlen";
33659 break;
33660 case PRAGMA_OMP_CLAUSE_NOGROUP:
33661 clauses = cp_parser_omp_clause_nogroup (parser, clauses,
33662 token->location);
33663 c_name = "nogroup";
33664 break;
33665 case PRAGMA_OMP_CLAUSE_THREADS:
33666 clauses
33667 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_THREADS,
33668 clauses, token->location);
33669 c_name = "threads";
33670 break;
33671 case PRAGMA_OMP_CLAUSE_SIMD:
33672 clauses
33673 = cp_parser_omp_clause_orderedkind (parser, OMP_CLAUSE_SIMD,
33674 clauses, token->location);
33675 c_name = "simd";
33676 break;
33677 case PRAGMA_CILK_CLAUSE_VECTORLENGTH:
33678 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, true);
33679 c_name = "simdlen";
33680 break;
33681 default:
33682 cp_parser_error (parser, "expected %<#pragma omp%> clause");
33683 goto saw_error;
33686 first = false;
33688 if (((mask >> c_kind) & 1) == 0)
33690 /* Remove the invalid clause(s) from the list to avoid
33691 confusing the rest of the compiler. */
33692 clauses = prev;
33693 error_at (token->location, "%qs is not valid for %qs", c_name, where);
33696 saw_error:
33697 /* In Cilk Plus SIMD enabled functions there is no pragma_token, so
33698 no reason to skip to the end. */
33699 if (!(flag_cilkplus && pragma_tok == NULL))
33700 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
33701 if (finish_p)
33703 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM)) != 0)
33704 return finish_omp_clauses (clauses, C_ORT_OMP_DECLARE_SIMD);
33705 else
33706 return finish_omp_clauses (clauses, C_ORT_OMP);
33708 return clauses;
33711 /* OpenMP 2.5:
33712 structured-block:
33713 statement
33715 In practice, we're also interested in adding the statement to an
33716 outer node. So it is convenient if we work around the fact that
33717 cp_parser_statement calls add_stmt. */
33719 static unsigned
33720 cp_parser_begin_omp_structured_block (cp_parser *parser)
33722 unsigned save = parser->in_statement;
33724 /* Only move the values to IN_OMP_BLOCK if they weren't false.
33725 This preserves the "not within loop or switch" style error messages
33726 for nonsense cases like
33727 void foo() {
33728 #pragma omp single
33729 break;
33732 if (parser->in_statement)
33733 parser->in_statement = IN_OMP_BLOCK;
33735 return save;
33738 static void
33739 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
33741 parser->in_statement = save;
33744 static tree
33745 cp_parser_omp_structured_block (cp_parser *parser, bool *if_p)
33747 tree stmt = begin_omp_structured_block ();
33748 unsigned int save = cp_parser_begin_omp_structured_block (parser);
33750 cp_parser_statement (parser, NULL_TREE, false, if_p);
33752 cp_parser_end_omp_structured_block (parser, save);
33753 return finish_omp_structured_block (stmt);
33756 /* OpenMP 2.5:
33757 # pragma omp atomic new-line
33758 expression-stmt
33760 expression-stmt:
33761 x binop= expr | x++ | ++x | x-- | --x
33762 binop:
33763 +, *, -, /, &, ^, |, <<, >>
33765 where x is an lvalue expression with scalar type.
33767 OpenMP 3.1:
33768 # pragma omp atomic new-line
33769 update-stmt
33771 # pragma omp atomic read new-line
33772 read-stmt
33774 # pragma omp atomic write new-line
33775 write-stmt
33777 # pragma omp atomic update new-line
33778 update-stmt
33780 # pragma omp atomic capture new-line
33781 capture-stmt
33783 # pragma omp atomic capture new-line
33784 capture-block
33786 read-stmt:
33787 v = x
33788 write-stmt:
33789 x = expr
33790 update-stmt:
33791 expression-stmt | x = x binop expr
33792 capture-stmt:
33793 v = expression-stmt
33794 capture-block:
33795 { v = x; update-stmt; } | { update-stmt; v = x; }
33797 OpenMP 4.0:
33798 update-stmt:
33799 expression-stmt | x = x binop expr | x = expr binop x
33800 capture-stmt:
33801 v = update-stmt
33802 capture-block:
33803 { v = x; update-stmt; } | { update-stmt; v = x; } | { v = x; x = expr; }
33805 where x and v are lvalue expressions with scalar type. */
33807 static void
33808 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
33810 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
33811 tree rhs1 = NULL_TREE, orig_lhs;
33812 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
33813 bool structured_block = false;
33814 bool seq_cst = false;
33816 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33818 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33819 const char *p = IDENTIFIER_POINTER (id);
33821 if (!strcmp (p, "seq_cst"))
33823 seq_cst = true;
33824 cp_lexer_consume_token (parser->lexer);
33825 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33826 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33827 cp_lexer_consume_token (parser->lexer);
33830 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33832 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33833 const char *p = IDENTIFIER_POINTER (id);
33835 if (!strcmp (p, "read"))
33836 code = OMP_ATOMIC_READ;
33837 else if (!strcmp (p, "write"))
33838 code = NOP_EXPR;
33839 else if (!strcmp (p, "update"))
33840 code = OMP_ATOMIC;
33841 else if (!strcmp (p, "capture"))
33842 code = OMP_ATOMIC_CAPTURE_NEW;
33843 else
33844 p = NULL;
33845 if (p)
33846 cp_lexer_consume_token (parser->lexer);
33848 if (!seq_cst)
33850 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA)
33851 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
33852 cp_lexer_consume_token (parser->lexer);
33854 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
33856 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
33857 const char *p = IDENTIFIER_POINTER (id);
33859 if (!strcmp (p, "seq_cst"))
33861 seq_cst = true;
33862 cp_lexer_consume_token (parser->lexer);
33866 cp_parser_require_pragma_eol (parser, pragma_tok);
33868 switch (code)
33870 case OMP_ATOMIC_READ:
33871 case NOP_EXPR: /* atomic write */
33872 v = cp_parser_unary_expression (parser);
33873 if (v == error_mark_node)
33874 goto saw_error;
33875 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33876 goto saw_error;
33877 if (code == NOP_EXPR)
33878 lhs = cp_parser_expression (parser);
33879 else
33880 lhs = cp_parser_unary_expression (parser);
33881 if (lhs == error_mark_node)
33882 goto saw_error;
33883 if (code == NOP_EXPR)
33885 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
33886 opcode. */
33887 code = OMP_ATOMIC;
33888 rhs = lhs;
33889 lhs = v;
33890 v = NULL_TREE;
33892 goto done;
33893 case OMP_ATOMIC_CAPTURE_NEW:
33894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
33896 cp_lexer_consume_token (parser->lexer);
33897 structured_block = true;
33899 else
33901 v = cp_parser_unary_expression (parser);
33902 if (v == error_mark_node)
33903 goto saw_error;
33904 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
33905 goto saw_error;
33907 default:
33908 break;
33911 restart:
33912 lhs = cp_parser_unary_expression (parser);
33913 orig_lhs = lhs;
33914 switch (TREE_CODE (lhs))
33916 case ERROR_MARK:
33917 goto saw_error;
33919 case POSTINCREMENT_EXPR:
33920 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33921 code = OMP_ATOMIC_CAPTURE_OLD;
33922 /* FALLTHROUGH */
33923 case PREINCREMENT_EXPR:
33924 lhs = TREE_OPERAND (lhs, 0);
33925 opcode = PLUS_EXPR;
33926 rhs = integer_one_node;
33927 break;
33929 case POSTDECREMENT_EXPR:
33930 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
33931 code = OMP_ATOMIC_CAPTURE_OLD;
33932 /* FALLTHROUGH */
33933 case PREDECREMENT_EXPR:
33934 lhs = TREE_OPERAND (lhs, 0);
33935 opcode = MINUS_EXPR;
33936 rhs = integer_one_node;
33937 break;
33939 case COMPOUND_EXPR:
33940 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
33941 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
33942 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
33943 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
33944 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
33945 (TREE_OPERAND (lhs, 1), 0), 0)))
33946 == BOOLEAN_TYPE)
33947 /* Undo effects of boolean_increment for post {in,de}crement. */
33948 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
33949 /* FALLTHRU */
33950 case MODIFY_EXPR:
33951 if (TREE_CODE (lhs) == MODIFY_EXPR
33952 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
33954 /* Undo effects of boolean_increment. */
33955 if (integer_onep (TREE_OPERAND (lhs, 1)))
33957 /* This is pre or post increment. */
33958 rhs = TREE_OPERAND (lhs, 1);
33959 lhs = TREE_OPERAND (lhs, 0);
33960 opcode = NOP_EXPR;
33961 if (code == OMP_ATOMIC_CAPTURE_NEW
33962 && !structured_block
33963 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
33964 code = OMP_ATOMIC_CAPTURE_OLD;
33965 break;
33968 /* FALLTHRU */
33969 default:
33970 switch (cp_lexer_peek_token (parser->lexer)->type)
33972 case CPP_MULT_EQ:
33973 opcode = MULT_EXPR;
33974 break;
33975 case CPP_DIV_EQ:
33976 opcode = TRUNC_DIV_EXPR;
33977 break;
33978 case CPP_PLUS_EQ:
33979 opcode = PLUS_EXPR;
33980 break;
33981 case CPP_MINUS_EQ:
33982 opcode = MINUS_EXPR;
33983 break;
33984 case CPP_LSHIFT_EQ:
33985 opcode = LSHIFT_EXPR;
33986 break;
33987 case CPP_RSHIFT_EQ:
33988 opcode = RSHIFT_EXPR;
33989 break;
33990 case CPP_AND_EQ:
33991 opcode = BIT_AND_EXPR;
33992 break;
33993 case CPP_OR_EQ:
33994 opcode = BIT_IOR_EXPR;
33995 break;
33996 case CPP_XOR_EQ:
33997 opcode = BIT_XOR_EXPR;
33998 break;
33999 case CPP_EQ:
34000 enum cp_parser_prec oprec;
34001 cp_token *token;
34002 cp_lexer_consume_token (parser->lexer);
34003 cp_parser_parse_tentatively (parser);
34004 rhs1 = cp_parser_simple_cast_expression (parser);
34005 if (rhs1 == error_mark_node)
34007 cp_parser_abort_tentative_parse (parser);
34008 cp_parser_simple_cast_expression (parser);
34009 goto saw_error;
34011 token = cp_lexer_peek_token (parser->lexer);
34012 if (token->type != CPP_SEMICOLON && !cp_tree_equal (lhs, rhs1))
34014 cp_parser_abort_tentative_parse (parser);
34015 cp_parser_parse_tentatively (parser);
34016 rhs = cp_parser_binary_expression (parser, false, true,
34017 PREC_NOT_OPERATOR, NULL);
34018 if (rhs == error_mark_node)
34020 cp_parser_abort_tentative_parse (parser);
34021 cp_parser_binary_expression (parser, false, true,
34022 PREC_NOT_OPERATOR, NULL);
34023 goto saw_error;
34025 switch (TREE_CODE (rhs))
34027 case MULT_EXPR:
34028 case TRUNC_DIV_EXPR:
34029 case RDIV_EXPR:
34030 case PLUS_EXPR:
34031 case MINUS_EXPR:
34032 case LSHIFT_EXPR:
34033 case RSHIFT_EXPR:
34034 case BIT_AND_EXPR:
34035 case BIT_IOR_EXPR:
34036 case BIT_XOR_EXPR:
34037 if (cp_tree_equal (lhs, TREE_OPERAND (rhs, 1)))
34039 if (cp_parser_parse_definitely (parser))
34041 opcode = TREE_CODE (rhs);
34042 rhs1 = TREE_OPERAND (rhs, 0);
34043 rhs = TREE_OPERAND (rhs, 1);
34044 goto stmt_done;
34046 else
34047 goto saw_error;
34049 break;
34050 default:
34051 break;
34053 cp_parser_abort_tentative_parse (parser);
34054 if (structured_block && code == OMP_ATOMIC_CAPTURE_OLD)
34056 rhs = cp_parser_expression (parser);
34057 if (rhs == error_mark_node)
34058 goto saw_error;
34059 opcode = NOP_EXPR;
34060 rhs1 = NULL_TREE;
34061 goto stmt_done;
34063 cp_parser_error (parser,
34064 "invalid form of %<#pragma omp atomic%>");
34065 goto saw_error;
34067 if (!cp_parser_parse_definitely (parser))
34068 goto saw_error;
34069 switch (token->type)
34071 case CPP_SEMICOLON:
34072 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34074 code = OMP_ATOMIC_CAPTURE_OLD;
34075 v = lhs;
34076 lhs = NULL_TREE;
34077 lhs1 = rhs1;
34078 rhs1 = NULL_TREE;
34079 cp_lexer_consume_token (parser->lexer);
34080 goto restart;
34082 else if (structured_block)
34084 opcode = NOP_EXPR;
34085 rhs = rhs1;
34086 rhs1 = NULL_TREE;
34087 goto stmt_done;
34089 cp_parser_error (parser,
34090 "invalid form of %<#pragma omp atomic%>");
34091 goto saw_error;
34092 case CPP_MULT:
34093 opcode = MULT_EXPR;
34094 break;
34095 case CPP_DIV:
34096 opcode = TRUNC_DIV_EXPR;
34097 break;
34098 case CPP_PLUS:
34099 opcode = PLUS_EXPR;
34100 break;
34101 case CPP_MINUS:
34102 opcode = MINUS_EXPR;
34103 break;
34104 case CPP_LSHIFT:
34105 opcode = LSHIFT_EXPR;
34106 break;
34107 case CPP_RSHIFT:
34108 opcode = RSHIFT_EXPR;
34109 break;
34110 case CPP_AND:
34111 opcode = BIT_AND_EXPR;
34112 break;
34113 case CPP_OR:
34114 opcode = BIT_IOR_EXPR;
34115 break;
34116 case CPP_XOR:
34117 opcode = BIT_XOR_EXPR;
34118 break;
34119 default:
34120 cp_parser_error (parser,
34121 "invalid operator for %<#pragma omp atomic%>");
34122 goto saw_error;
34124 oprec = TOKEN_PRECEDENCE (token);
34125 gcc_assert (oprec != PREC_NOT_OPERATOR);
34126 if (commutative_tree_code (opcode))
34127 oprec = (enum cp_parser_prec) (oprec - 1);
34128 cp_lexer_consume_token (parser->lexer);
34129 rhs = cp_parser_binary_expression (parser, false, false,
34130 oprec, NULL);
34131 if (rhs == error_mark_node)
34132 goto saw_error;
34133 goto stmt_done;
34134 /* FALLTHROUGH */
34135 default:
34136 cp_parser_error (parser,
34137 "invalid operator for %<#pragma omp atomic%>");
34138 goto saw_error;
34140 cp_lexer_consume_token (parser->lexer);
34142 rhs = cp_parser_expression (parser);
34143 if (rhs == error_mark_node)
34144 goto saw_error;
34145 break;
34147 stmt_done:
34148 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
34150 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
34151 goto saw_error;
34152 v = cp_parser_unary_expression (parser);
34153 if (v == error_mark_node)
34154 goto saw_error;
34155 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
34156 goto saw_error;
34157 lhs1 = cp_parser_unary_expression (parser);
34158 if (lhs1 == error_mark_node)
34159 goto saw_error;
34161 if (structured_block)
34163 cp_parser_consume_semicolon_at_end_of_statement (parser);
34164 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
34166 done:
34167 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1, seq_cst);
34168 if (!structured_block)
34169 cp_parser_consume_semicolon_at_end_of_statement (parser);
34170 return;
34172 saw_error:
34173 cp_parser_skip_to_end_of_block_or_statement (parser);
34174 if (structured_block)
34176 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34177 cp_lexer_consume_token (parser->lexer);
34178 else if (code == OMP_ATOMIC_CAPTURE_NEW)
34180 cp_parser_skip_to_end_of_block_or_statement (parser);
34181 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34182 cp_lexer_consume_token (parser->lexer);
34188 /* OpenMP 2.5:
34189 # pragma omp barrier new-line */
34191 static void
34192 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
34194 cp_parser_require_pragma_eol (parser, pragma_tok);
34195 finish_omp_barrier ();
34198 /* OpenMP 2.5:
34199 # pragma omp critical [(name)] new-line
34200 structured-block
34202 OpenMP 4.5:
34203 # pragma omp critical [(name) [hint(expression)]] new-line
34204 structured-block */
34206 #define OMP_CRITICAL_CLAUSE_MASK \
34207 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_HINT) )
34209 static tree
34210 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
34212 tree stmt, name = NULL_TREE, clauses = NULL_TREE;
34214 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34216 cp_lexer_consume_token (parser->lexer);
34218 name = cp_parser_identifier (parser);
34220 if (name == error_mark_node
34221 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34222 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34223 /*or_comma=*/false,
34224 /*consume_paren=*/true);
34225 if (name == error_mark_node)
34226 name = NULL;
34228 clauses = cp_parser_omp_all_clauses (parser,
34229 OMP_CRITICAL_CLAUSE_MASK,
34230 "#pragma omp critical", pragma_tok);
34232 else
34233 cp_parser_require_pragma_eol (parser, pragma_tok);
34235 stmt = cp_parser_omp_structured_block (parser, if_p);
34236 return c_finish_omp_critical (input_location, stmt, name, clauses);
34239 /* OpenMP 2.5:
34240 # pragma omp flush flush-vars[opt] new-line
34242 flush-vars:
34243 ( variable-list ) */
34245 static void
34246 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
34248 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
34249 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
34250 cp_parser_require_pragma_eol (parser, pragma_tok);
34252 finish_omp_flush ();
34255 /* Helper function, to parse omp for increment expression. */
34257 static tree
34258 cp_parser_omp_for_cond (cp_parser *parser, tree decl, enum tree_code code)
34260 tree cond = cp_parser_binary_expression (parser, false, true,
34261 PREC_NOT_OPERATOR, NULL);
34262 if (cond == error_mark_node
34263 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34265 cp_parser_skip_to_end_of_statement (parser);
34266 return error_mark_node;
34269 switch (TREE_CODE (cond))
34271 case GT_EXPR:
34272 case GE_EXPR:
34273 case LT_EXPR:
34274 case LE_EXPR:
34275 break;
34276 case NE_EXPR:
34277 if (code == CILK_SIMD || code == CILK_FOR)
34278 break;
34279 /* Fall through: OpenMP disallows NE_EXPR. */
34280 gcc_fallthrough ();
34281 default:
34282 return error_mark_node;
34285 /* If decl is an iterator, preserve LHS and RHS of the relational
34286 expr until finish_omp_for. */
34287 if (decl
34288 && (type_dependent_expression_p (decl)
34289 || CLASS_TYPE_P (TREE_TYPE (decl))))
34290 return cond;
34292 return build_x_binary_op (EXPR_LOC_OR_LOC (cond, input_location),
34293 TREE_CODE (cond),
34294 TREE_OPERAND (cond, 0), ERROR_MARK,
34295 TREE_OPERAND (cond, 1), ERROR_MARK,
34296 /*overload=*/NULL, tf_warning_or_error);
34299 /* Helper function, to parse omp for increment expression. */
34301 static tree
34302 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
34304 cp_token *token = cp_lexer_peek_token (parser->lexer);
34305 enum tree_code op;
34306 tree lhs, rhs;
34307 cp_id_kind idk;
34308 bool decl_first;
34310 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34312 op = (token->type == CPP_PLUS_PLUS
34313 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
34314 cp_lexer_consume_token (parser->lexer);
34315 lhs = cp_parser_simple_cast_expression (parser);
34316 if (lhs != decl
34317 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34318 return error_mark_node;
34319 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34322 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
34323 if (lhs != decl
34324 && (!processing_template_decl || !cp_tree_equal (lhs, decl)))
34325 return error_mark_node;
34327 token = cp_lexer_peek_token (parser->lexer);
34328 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
34330 op = (token->type == CPP_PLUS_PLUS
34331 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
34332 cp_lexer_consume_token (parser->lexer);
34333 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
34336 op = cp_parser_assignment_operator_opt (parser);
34337 if (op == ERROR_MARK)
34338 return error_mark_node;
34340 if (op != NOP_EXPR)
34342 rhs = cp_parser_assignment_expression (parser);
34343 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
34344 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34347 lhs = cp_parser_binary_expression (parser, false, false,
34348 PREC_ADDITIVE_EXPRESSION, NULL);
34349 token = cp_lexer_peek_token (parser->lexer);
34350 decl_first = (lhs == decl
34351 || (processing_template_decl && cp_tree_equal (lhs, decl)));
34352 if (decl_first)
34353 lhs = NULL_TREE;
34354 if (token->type != CPP_PLUS
34355 && token->type != CPP_MINUS)
34356 return error_mark_node;
34360 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
34361 cp_lexer_consume_token (parser->lexer);
34362 rhs = cp_parser_binary_expression (parser, false, false,
34363 PREC_ADDITIVE_EXPRESSION, NULL);
34364 token = cp_lexer_peek_token (parser->lexer);
34365 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
34367 if (lhs == NULL_TREE)
34369 if (op == PLUS_EXPR)
34370 lhs = rhs;
34371 else
34372 lhs = build_x_unary_op (input_location, NEGATE_EXPR, rhs,
34373 tf_warning_or_error);
34375 else
34376 lhs = build_x_binary_op (input_location, op, lhs, ERROR_MARK, rhs,
34377 ERROR_MARK, NULL, tf_warning_or_error);
34380 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
34382 if (!decl_first)
34384 if ((rhs != decl
34385 && (!processing_template_decl || !cp_tree_equal (rhs, decl)))
34386 || op == MINUS_EXPR)
34387 return error_mark_node;
34388 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
34390 else
34391 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
34393 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
34396 /* Parse the initialization statement of either an OpenMP for loop or
34397 a Cilk Plus for loop.
34399 Return true if the resulting construct should have an
34400 OMP_CLAUSE_PRIVATE added to it. */
34402 static tree
34403 cp_parser_omp_for_loop_init (cp_parser *parser,
34404 enum tree_code code,
34405 tree &this_pre_body,
34406 vec<tree, va_gc> *for_block,
34407 tree &init,
34408 tree &orig_init,
34409 tree &decl,
34410 tree &real_decl)
34412 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34413 return NULL_TREE;
34415 tree add_private_clause = NULL_TREE;
34417 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
34419 init-expr:
34420 var = lb
34421 integer-type var = lb
34422 random-access-iterator-type var = lb
34423 pointer-type var = lb
34425 cp_decl_specifier_seq type_specifiers;
34427 /* First, try to parse as an initialized declaration. See
34428 cp_parser_condition, from whence the bulk of this is copied. */
34430 cp_parser_parse_tentatively (parser);
34431 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
34432 /*is_trailing_return=*/false,
34433 &type_specifiers);
34434 if (cp_parser_parse_definitely (parser))
34436 /* If parsing a type specifier seq succeeded, then this
34437 MUST be a initialized declaration. */
34438 tree asm_specification, attributes;
34439 cp_declarator *declarator;
34441 declarator = cp_parser_declarator (parser,
34442 CP_PARSER_DECLARATOR_NAMED,
34443 /*ctor_dtor_or_conv_p=*/NULL,
34444 /*parenthesized_p=*/NULL,
34445 /*member_p=*/false,
34446 /*friend_p=*/false);
34447 attributes = cp_parser_attributes_opt (parser);
34448 asm_specification = cp_parser_asm_specification_opt (parser);
34450 if (declarator == cp_error_declarator)
34451 cp_parser_skip_to_end_of_statement (parser);
34453 else
34455 tree pushed_scope, auto_node;
34457 decl = start_decl (declarator, &type_specifiers,
34458 SD_INITIALIZED, attributes,
34459 /*prefix_attributes=*/NULL_TREE,
34460 &pushed_scope);
34462 auto_node = type_uses_auto (TREE_TYPE (decl));
34463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
34465 if (cp_lexer_next_token_is (parser->lexer,
34466 CPP_OPEN_PAREN))
34468 if (code != CILK_SIMD && code != CILK_FOR)
34469 error ("parenthesized initialization is not allowed in "
34470 "OpenMP %<for%> loop");
34471 else
34472 error ("parenthesized initialization is "
34473 "not allowed in for-loop");
34475 else
34476 /* Trigger an error. */
34477 cp_parser_require (parser, CPP_EQ, RT_EQ);
34479 init = error_mark_node;
34480 cp_parser_skip_to_end_of_statement (parser);
34482 else if (CLASS_TYPE_P (TREE_TYPE (decl))
34483 || type_dependent_expression_p (decl)
34484 || auto_node)
34486 bool is_direct_init, is_non_constant_init;
34488 init = cp_parser_initializer (parser,
34489 &is_direct_init,
34490 &is_non_constant_init);
34492 if (auto_node)
34494 TREE_TYPE (decl)
34495 = do_auto_deduction (TREE_TYPE (decl), init,
34496 auto_node);
34498 if (!CLASS_TYPE_P (TREE_TYPE (decl))
34499 && !type_dependent_expression_p (decl))
34500 goto non_class;
34503 cp_finish_decl (decl, init, !is_non_constant_init,
34504 asm_specification,
34505 LOOKUP_ONLYCONVERTING);
34506 orig_init = init;
34507 if (CLASS_TYPE_P (TREE_TYPE (decl)))
34509 vec_safe_push (for_block, this_pre_body);
34510 init = NULL_TREE;
34512 else
34514 init = pop_stmt_list (this_pre_body);
34515 if (init && TREE_CODE (init) == STATEMENT_LIST)
34517 tree_stmt_iterator i = tsi_start (init);
34518 /* Move lambda DECL_EXPRs to FOR_BLOCK. */
34519 while (!tsi_end_p (i))
34521 tree t = tsi_stmt (i);
34522 if (TREE_CODE (t) == DECL_EXPR
34523 && TREE_CODE (DECL_EXPR_DECL (t)) == TYPE_DECL)
34525 tsi_delink (&i);
34526 vec_safe_push (for_block, t);
34527 continue;
34529 break;
34531 if (tsi_one_before_end_p (i))
34533 tree t = tsi_stmt (i);
34534 tsi_delink (&i);
34535 free_stmt_list (init);
34536 init = t;
34540 this_pre_body = NULL_TREE;
34542 else
34544 /* Consume '='. */
34545 cp_lexer_consume_token (parser->lexer);
34546 init = cp_parser_assignment_expression (parser);
34548 non_class:
34549 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
34550 init = error_mark_node;
34551 else
34552 cp_finish_decl (decl, NULL_TREE,
34553 /*init_const_expr_p=*/false,
34554 asm_specification,
34555 LOOKUP_ONLYCONVERTING);
34558 if (pushed_scope)
34559 pop_scope (pushed_scope);
34562 else
34564 cp_id_kind idk;
34565 /* If parsing a type specifier sequence failed, then
34566 this MUST be a simple expression. */
34567 if (code == CILK_FOR)
34568 error ("%<_Cilk_for%> allows expression instead of declaration only "
34569 "in C, not in C++");
34570 cp_parser_parse_tentatively (parser);
34571 decl = cp_parser_primary_expression (parser, false, false,
34572 false, &idk);
34573 cp_token *last_tok = cp_lexer_peek_token (parser->lexer);
34574 if (!cp_parser_error_occurred (parser)
34575 && decl
34576 && (TREE_CODE (decl) == COMPONENT_REF
34577 || (TREE_CODE (decl) == SCOPE_REF && TREE_TYPE (decl))))
34579 cp_parser_abort_tentative_parse (parser);
34580 cp_parser_parse_tentatively (parser);
34581 cp_token *token = cp_lexer_peek_token (parser->lexer);
34582 tree name = cp_parser_id_expression (parser, /*template_p=*/false,
34583 /*check_dependency_p=*/true,
34584 /*template_p=*/NULL,
34585 /*declarator_p=*/false,
34586 /*optional_p=*/false);
34587 if (name != error_mark_node
34588 && last_tok == cp_lexer_peek_token (parser->lexer))
34590 decl = cp_parser_lookup_name_simple (parser, name,
34591 token->location);
34592 if (TREE_CODE (decl) == FIELD_DECL)
34593 add_private_clause = omp_privatize_field (decl, false);
34595 cp_parser_abort_tentative_parse (parser);
34596 cp_parser_parse_tentatively (parser);
34597 decl = cp_parser_primary_expression (parser, false, false,
34598 false, &idk);
34600 if (!cp_parser_error_occurred (parser)
34601 && decl
34602 && DECL_P (decl)
34603 && CLASS_TYPE_P (TREE_TYPE (decl)))
34605 tree rhs;
34607 cp_parser_parse_definitely (parser);
34608 cp_parser_require (parser, CPP_EQ, RT_EQ);
34609 rhs = cp_parser_assignment_expression (parser);
34610 orig_init = rhs;
34611 finish_expr_stmt (build_x_modify_expr (EXPR_LOCATION (rhs),
34612 decl, NOP_EXPR,
34613 rhs,
34614 tf_warning_or_error));
34615 if (!add_private_clause)
34616 add_private_clause = decl;
34618 else
34620 decl = NULL;
34621 cp_parser_abort_tentative_parse (parser);
34622 init = cp_parser_expression (parser);
34623 if (init)
34625 if (TREE_CODE (init) == MODIFY_EXPR
34626 || TREE_CODE (init) == MODOP_EXPR)
34627 real_decl = TREE_OPERAND (init, 0);
34631 return add_private_clause;
34634 /* Parse the restricted form of the for statement allowed by OpenMP. */
34636 static tree
34637 cp_parser_omp_for_loop (cp_parser *parser, enum tree_code code, tree clauses,
34638 tree *cclauses, bool *if_p)
34640 tree init, orig_init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
34641 tree real_decl, initv, condv, incrv, declv;
34642 tree this_pre_body, cl, ordered_cl = NULL_TREE;
34643 location_t loc_first;
34644 bool collapse_err = false;
34645 int i, collapse = 1, ordered = 0, count, nbraces = 0;
34646 vec<tree, va_gc> *for_block = make_tree_vector ();
34647 auto_vec<tree, 4> orig_inits;
34648 bool tiling = false;
34650 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
34651 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
34652 collapse = tree_to_shwi (OMP_CLAUSE_COLLAPSE_EXPR (cl));
34653 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_TILE)
34655 tiling = true;
34656 collapse = list_length (OMP_CLAUSE_TILE_LIST (cl));
34658 else if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_ORDERED
34659 && OMP_CLAUSE_ORDERED_EXPR (cl))
34661 ordered_cl = cl;
34662 ordered = tree_to_shwi (OMP_CLAUSE_ORDERED_EXPR (cl));
34665 if (ordered && ordered < collapse)
34667 error_at (OMP_CLAUSE_LOCATION (ordered_cl),
34668 "%<ordered%> clause parameter is less than %<collapse%>");
34669 OMP_CLAUSE_ORDERED_EXPR (ordered_cl)
34670 = build_int_cst (NULL_TREE, collapse);
34671 ordered = collapse;
34673 if (ordered)
34675 for (tree *pc = &clauses; *pc; )
34676 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_LINEAR)
34678 error_at (OMP_CLAUSE_LOCATION (*pc),
34679 "%<linear%> clause may not be specified together "
34680 "with %<ordered%> clause with a parameter");
34681 *pc = OMP_CLAUSE_CHAIN (*pc);
34683 else
34684 pc = &OMP_CLAUSE_CHAIN (*pc);
34687 gcc_assert (tiling || (collapse >= 1 && ordered >= 0));
34688 count = ordered ? ordered : collapse;
34690 declv = make_tree_vec (count);
34691 initv = make_tree_vec (count);
34692 condv = make_tree_vec (count);
34693 incrv = make_tree_vec (count);
34695 loc_first = cp_lexer_peek_token (parser->lexer)->location;
34697 for (i = 0; i < count; i++)
34699 int bracecount = 0;
34700 tree add_private_clause = NULL_TREE;
34701 location_t loc;
34703 if (code != CILK_FOR
34704 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34706 if (!collapse_err)
34707 cp_parser_error (parser, "for statement expected");
34708 return NULL;
34710 if (code == CILK_FOR
34711 && !cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
34713 if (!collapse_err)
34714 cp_parser_error (parser, "_Cilk_for statement expected");
34715 return NULL;
34717 loc = cp_lexer_consume_token (parser->lexer)->location;
34719 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
34720 return NULL;
34722 init = orig_init = decl = real_decl = NULL;
34723 this_pre_body = push_stmt_list ();
34725 add_private_clause
34726 = cp_parser_omp_for_loop_init (parser, code,
34727 this_pre_body, for_block,
34728 init, orig_init, decl, real_decl);
34730 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34731 if (this_pre_body)
34733 this_pre_body = pop_stmt_list (this_pre_body);
34734 if (pre_body)
34736 tree t = pre_body;
34737 pre_body = push_stmt_list ();
34738 add_stmt (t);
34739 add_stmt (this_pre_body);
34740 pre_body = pop_stmt_list (pre_body);
34742 else
34743 pre_body = this_pre_body;
34746 if (decl)
34747 real_decl = decl;
34748 if (cclauses != NULL
34749 && cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL] != NULL
34750 && real_decl != NULL_TREE)
34752 tree *c;
34753 for (c = &cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL]; *c ; )
34754 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
34755 && OMP_CLAUSE_DECL (*c) == real_decl)
34757 error_at (loc, "iteration variable %qD"
34758 " should not be firstprivate", real_decl);
34759 *c = OMP_CLAUSE_CHAIN (*c);
34761 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
34762 && OMP_CLAUSE_DECL (*c) == real_decl)
34764 /* Move lastprivate (decl) clause to OMP_FOR_CLAUSES. */
34765 tree l = *c;
34766 *c = OMP_CLAUSE_CHAIN (*c);
34767 if (code == OMP_SIMD)
34769 OMP_CLAUSE_CHAIN (l) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
34770 cclauses[C_OMP_CLAUSE_SPLIT_FOR] = l;
34772 else
34774 OMP_CLAUSE_CHAIN (l) = clauses;
34775 clauses = l;
34777 add_private_clause = NULL_TREE;
34779 else
34781 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
34782 && OMP_CLAUSE_DECL (*c) == real_decl)
34783 add_private_clause = NULL_TREE;
34784 c = &OMP_CLAUSE_CHAIN (*c);
34788 if (add_private_clause)
34790 tree c;
34791 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
34793 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
34794 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
34795 && OMP_CLAUSE_DECL (c) == decl)
34796 break;
34797 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
34798 && OMP_CLAUSE_DECL (c) == decl)
34799 error_at (loc, "iteration variable %qD "
34800 "should not be firstprivate",
34801 decl);
34802 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
34803 && OMP_CLAUSE_DECL (c) == decl)
34804 error_at (loc, "iteration variable %qD should not be reduction",
34805 decl);
34807 if (c == NULL)
34809 if (code != OMP_SIMD)
34810 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
34811 else if (collapse == 1)
34812 c = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
34813 else
34814 c = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
34815 OMP_CLAUSE_DECL (c) = add_private_clause;
34816 c = finish_omp_clauses (c, C_ORT_OMP);
34817 if (c)
34819 OMP_CLAUSE_CHAIN (c) = clauses;
34820 clauses = c;
34821 /* For linear, signal that we need to fill up
34822 the so far unknown linear step. */
34823 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINEAR)
34824 OMP_CLAUSE_LINEAR_STEP (c) = NULL_TREE;
34829 cond = NULL;
34830 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
34831 cond = cp_parser_omp_for_cond (parser, decl, code);
34832 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
34834 incr = NULL;
34835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
34837 /* If decl is an iterator, preserve the operator on decl
34838 until finish_omp_for. */
34839 if (real_decl
34840 && ((processing_template_decl
34841 && (TREE_TYPE (real_decl) == NULL_TREE
34842 || !POINTER_TYPE_P (TREE_TYPE (real_decl))))
34843 || CLASS_TYPE_P (TREE_TYPE (real_decl))))
34844 incr = cp_parser_omp_for_incr (parser, real_decl);
34845 else
34846 incr = cp_parser_expression (parser);
34847 if (!EXPR_HAS_LOCATION (incr))
34848 protected_set_expr_location (incr, input_location);
34851 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
34852 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
34853 /*or_comma=*/false,
34854 /*consume_paren=*/true);
34856 TREE_VEC_ELT (declv, i) = decl;
34857 TREE_VEC_ELT (initv, i) = init;
34858 TREE_VEC_ELT (condv, i) = cond;
34859 TREE_VEC_ELT (incrv, i) = incr;
34860 if (orig_init)
34862 orig_inits.safe_grow_cleared (i + 1);
34863 orig_inits[i] = orig_init;
34866 if (i == count - 1)
34867 break;
34869 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
34870 in between the collapsed for loops to be still considered perfectly
34871 nested. Hopefully the final version clarifies this.
34872 For now handle (multiple) {'s and empty statements. */
34873 cp_parser_parse_tentatively (parser);
34874 for (;;)
34876 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
34877 break;
34878 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
34880 cp_lexer_consume_token (parser->lexer);
34881 bracecount++;
34883 else if (bracecount
34884 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34885 cp_lexer_consume_token (parser->lexer);
34886 else
34888 loc = cp_lexer_peek_token (parser->lexer)->location;
34889 error_at (loc, "not enough for loops to collapse");
34890 collapse_err = true;
34891 cp_parser_abort_tentative_parse (parser);
34892 declv = NULL_TREE;
34893 break;
34897 if (declv)
34899 cp_parser_parse_definitely (parser);
34900 nbraces += bracecount;
34904 if (nbraces)
34905 if_p = NULL;
34907 /* Note that we saved the original contents of this flag when we entered
34908 the structured block, and so we don't need to re-save it here. */
34909 if (code == CILK_SIMD || code == CILK_FOR)
34910 parser->in_statement = IN_CILK_SIMD_FOR;
34911 else
34912 parser->in_statement = IN_OMP_FOR;
34914 /* Note that the grammar doesn't call for a structured block here,
34915 though the loop as a whole is a structured block. */
34916 body = push_stmt_list ();
34917 cp_parser_statement (parser, NULL_TREE, false, if_p);
34918 body = pop_stmt_list (body);
34920 if (declv == NULL_TREE)
34921 ret = NULL_TREE;
34922 else
34923 ret = finish_omp_for (loc_first, code, declv, NULL, initv, condv, incrv,
34924 body, pre_body, &orig_inits, clauses);
34926 while (nbraces)
34928 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
34930 cp_lexer_consume_token (parser->lexer);
34931 nbraces--;
34933 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
34934 cp_lexer_consume_token (parser->lexer);
34935 else
34937 if (!collapse_err)
34939 error_at (cp_lexer_peek_token (parser->lexer)->location,
34940 "collapsed loops not perfectly nested");
34942 collapse_err = true;
34943 cp_parser_statement_seq_opt (parser, NULL);
34944 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
34945 break;
34949 while (!for_block->is_empty ())
34951 tree t = for_block->pop ();
34952 if (TREE_CODE (t) == STATEMENT_LIST)
34953 add_stmt (pop_stmt_list (t));
34954 else
34955 add_stmt (t);
34957 release_tree_vector (for_block);
34959 return ret;
34962 /* Helper function for OpenMP parsing, split clauses and call
34963 finish_omp_clauses on each of the set of clauses afterwards. */
34965 static void
34966 cp_omp_split_clauses (location_t loc, enum tree_code code,
34967 omp_clause_mask mask, tree clauses, tree *cclauses)
34969 int i;
34970 c_omp_split_clauses (loc, code, mask, clauses, cclauses);
34971 for (i = 0; i < C_OMP_CLAUSE_SPLIT_COUNT; i++)
34972 if (cclauses[i])
34973 cclauses[i] = finish_omp_clauses (cclauses[i], C_ORT_OMP);
34976 /* OpenMP 4.0:
34977 #pragma omp simd simd-clause[optseq] new-line
34978 for-loop */
34980 #define OMP_SIMD_CLAUSE_MASK \
34981 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SAFELEN) \
34982 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
34983 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
34984 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
34985 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
34986 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
34987 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
34988 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
34990 static tree
34991 cp_parser_omp_simd (cp_parser *parser, cp_token *pragma_tok,
34992 char *p_name, omp_clause_mask mask, tree *cclauses,
34993 bool *if_p)
34995 tree clauses, sb, ret;
34996 unsigned int save;
34997 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
34999 strcat (p_name, " simd");
35000 mask |= OMP_SIMD_CLAUSE_MASK;
35002 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35003 cclauses == NULL);
35004 if (cclauses)
35006 cp_omp_split_clauses (loc, OMP_SIMD, mask, clauses, cclauses);
35007 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SIMD];
35008 tree c = omp_find_clause (cclauses[C_OMP_CLAUSE_SPLIT_FOR],
35009 OMP_CLAUSE_ORDERED);
35010 if (c && OMP_CLAUSE_ORDERED_EXPR (c))
35012 error_at (OMP_CLAUSE_LOCATION (c),
35013 "%<ordered%> clause with parameter may not be specified "
35014 "on %qs construct", p_name);
35015 OMP_CLAUSE_ORDERED_EXPR (c) = NULL_TREE;
35019 sb = begin_omp_structured_block ();
35020 save = cp_parser_begin_omp_structured_block (parser);
35022 ret = cp_parser_omp_for_loop (parser, OMP_SIMD, clauses, cclauses, if_p);
35024 cp_parser_end_omp_structured_block (parser, save);
35025 add_stmt (finish_omp_structured_block (sb));
35027 return ret;
35030 /* OpenMP 2.5:
35031 #pragma omp for for-clause[optseq] new-line
35032 for-loop
35034 OpenMP 4.0:
35035 #pragma omp for simd for-simd-clause[optseq] new-line
35036 for-loop */
35038 #define OMP_FOR_CLAUSE_MASK \
35039 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35040 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35041 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35042 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
35043 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35044 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED) \
35045 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SCHEDULE) \
35046 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
35047 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35049 static tree
35050 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok,
35051 char *p_name, omp_clause_mask mask, tree *cclauses,
35052 bool *if_p)
35054 tree clauses, sb, ret;
35055 unsigned int save;
35056 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35058 strcat (p_name, " for");
35059 mask |= OMP_FOR_CLAUSE_MASK;
35060 /* parallel for{, simd} disallows nowait clause, but for
35061 target {teams distribute ,}parallel for{, simd} it should be accepted. */
35062 if (cclauses && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) == 0)
35063 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35064 /* Composite distribute parallel for{, simd} disallows ordered clause. */
35065 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35066 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ORDERED);
35068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35070 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35071 const char *p = IDENTIFIER_POINTER (id);
35073 if (strcmp (p, "simd") == 0)
35075 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35076 if (cclauses == NULL)
35077 cclauses = cclauses_buf;
35079 cp_lexer_consume_token (parser->lexer);
35080 if (!flag_openmp) /* flag_openmp_simd */
35081 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35082 cclauses, if_p);
35083 sb = begin_omp_structured_block ();
35084 save = cp_parser_begin_omp_structured_block (parser);
35085 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35086 cclauses, if_p);
35087 cp_parser_end_omp_structured_block (parser, save);
35088 tree body = finish_omp_structured_block (sb);
35089 if (ret == NULL)
35090 return ret;
35091 ret = make_node (OMP_FOR);
35092 TREE_TYPE (ret) = void_type_node;
35093 OMP_FOR_BODY (ret) = body;
35094 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35095 SET_EXPR_LOCATION (ret, loc);
35096 add_stmt (ret);
35097 return ret;
35100 if (!flag_openmp) /* flag_openmp_simd */
35102 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35103 return NULL_TREE;
35106 /* Composite distribute parallel for disallows linear clause. */
35107 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35108 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR);
35110 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35111 cclauses == NULL);
35112 if (cclauses)
35114 cp_omp_split_clauses (loc, OMP_FOR, mask, clauses, cclauses);
35115 clauses = cclauses[C_OMP_CLAUSE_SPLIT_FOR];
35118 sb = begin_omp_structured_block ();
35119 save = cp_parser_begin_omp_structured_block (parser);
35121 ret = cp_parser_omp_for_loop (parser, OMP_FOR, clauses, cclauses, if_p);
35123 cp_parser_end_omp_structured_block (parser, save);
35124 add_stmt (finish_omp_structured_block (sb));
35126 return ret;
35129 /* OpenMP 2.5:
35130 # pragma omp master new-line
35131 structured-block */
35133 static tree
35134 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35136 cp_parser_require_pragma_eol (parser, pragma_tok);
35137 return c_finish_omp_master (input_location,
35138 cp_parser_omp_structured_block (parser, if_p));
35141 /* OpenMP 2.5:
35142 # pragma omp ordered new-line
35143 structured-block
35145 OpenMP 4.5:
35146 # pragma omp ordered ordered-clauses new-line
35147 structured-block */
35149 #define OMP_ORDERED_CLAUSE_MASK \
35150 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREADS) \
35151 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMD))
35153 #define OMP_ORDERED_DEPEND_CLAUSE_MASK \
35154 (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND)
35156 static bool
35157 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok,
35158 enum pragma_context context, bool *if_p)
35160 location_t loc = pragma_tok->location;
35162 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35164 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35165 const char *p = IDENTIFIER_POINTER (id);
35167 if (strcmp (p, "depend") == 0)
35169 if (context == pragma_stmt)
35171 error_at (pragma_tok->location, "%<#pragma omp ordered%> with "
35172 "%<depend%> clause may only be used in compound "
35173 "statements");
35174 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35175 return false;
35177 tree clauses
35178 = cp_parser_omp_all_clauses (parser,
35179 OMP_ORDERED_DEPEND_CLAUSE_MASK,
35180 "#pragma omp ordered", pragma_tok);
35181 c_finish_omp_ordered (loc, clauses, NULL_TREE);
35182 return false;
35186 tree clauses
35187 = cp_parser_omp_all_clauses (parser, OMP_ORDERED_CLAUSE_MASK,
35188 "#pragma omp ordered", pragma_tok);
35189 c_finish_omp_ordered (loc, clauses,
35190 cp_parser_omp_structured_block (parser, if_p));
35191 return true;
35194 /* OpenMP 2.5:
35196 section-scope:
35197 { section-sequence }
35199 section-sequence:
35200 section-directive[opt] structured-block
35201 section-sequence section-directive structured-block */
35203 static tree
35204 cp_parser_omp_sections_scope (cp_parser *parser)
35206 tree stmt, substmt;
35207 bool error_suppress = false;
35208 cp_token *tok;
35210 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
35211 return NULL_TREE;
35213 stmt = push_stmt_list ();
35215 if (cp_parser_pragma_kind (cp_lexer_peek_token (parser->lexer))
35216 != PRAGMA_OMP_SECTION)
35218 substmt = cp_parser_omp_structured_block (parser, NULL);
35219 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35220 add_stmt (substmt);
35223 while (1)
35225 tok = cp_lexer_peek_token (parser->lexer);
35226 if (tok->type == CPP_CLOSE_BRACE)
35227 break;
35228 if (tok->type == CPP_EOF)
35229 break;
35231 if (cp_parser_pragma_kind (tok) == PRAGMA_OMP_SECTION)
35233 cp_lexer_consume_token (parser->lexer);
35234 cp_parser_require_pragma_eol (parser, tok);
35235 error_suppress = false;
35237 else if (!error_suppress)
35239 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
35240 error_suppress = true;
35243 substmt = cp_parser_omp_structured_block (parser, NULL);
35244 substmt = build1 (OMP_SECTION, void_type_node, substmt);
35245 add_stmt (substmt);
35247 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
35249 substmt = pop_stmt_list (stmt);
35251 stmt = make_node (OMP_SECTIONS);
35252 TREE_TYPE (stmt) = void_type_node;
35253 OMP_SECTIONS_BODY (stmt) = substmt;
35255 add_stmt (stmt);
35256 return stmt;
35259 /* OpenMP 2.5:
35260 # pragma omp sections sections-clause[optseq] newline
35261 sections-scope */
35263 #define OMP_SECTIONS_CLAUSE_MASK \
35264 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35265 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35266 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35267 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35268 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35270 static tree
35271 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok,
35272 char *p_name, omp_clause_mask mask, tree *cclauses)
35274 tree clauses, ret;
35275 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35277 strcat (p_name, " sections");
35278 mask |= OMP_SECTIONS_CLAUSE_MASK;
35279 if (cclauses)
35280 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT);
35282 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35283 cclauses == NULL);
35284 if (cclauses)
35286 cp_omp_split_clauses (loc, OMP_SECTIONS, mask, clauses, cclauses);
35287 clauses = cclauses[C_OMP_CLAUSE_SPLIT_SECTIONS];
35290 ret = cp_parser_omp_sections_scope (parser);
35291 if (ret)
35292 OMP_SECTIONS_CLAUSES (ret) = clauses;
35294 return ret;
35297 /* OpenMP 2.5:
35298 # pragma omp parallel parallel-clause[optseq] new-line
35299 structured-block
35300 # pragma omp parallel for parallel-for-clause[optseq] new-line
35301 structured-block
35302 # pragma omp parallel sections parallel-sections-clause[optseq] new-line
35303 structured-block
35305 OpenMP 4.0:
35306 # pragma omp parallel for simd parallel-for-simd-clause[optseq] new-line
35307 structured-block */
35309 #define OMP_PARALLEL_CLAUSE_MASK \
35310 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35311 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35312 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35313 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35314 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35315 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN) \
35316 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35317 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_THREADS) \
35318 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PROC_BIND))
35320 static tree
35321 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok,
35322 char *p_name, omp_clause_mask mask, tree *cclauses,
35323 bool *if_p)
35325 tree stmt, clauses, block;
35326 unsigned int save;
35327 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35329 strcat (p_name, " parallel");
35330 mask |= OMP_PARALLEL_CLAUSE_MASK;
35331 /* #pragma omp target parallel{, for, for simd} disallow copyin clause. */
35332 if ((mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP)) != 0
35333 && (mask & (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) == 0)
35334 mask &= ~(OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYIN);
35336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
35338 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35339 if (cclauses == NULL)
35340 cclauses = cclauses_buf;
35342 cp_lexer_consume_token (parser->lexer);
35343 if (!flag_openmp) /* flag_openmp_simd */
35344 return cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35345 if_p);
35346 block = begin_omp_parallel ();
35347 save = cp_parser_begin_omp_structured_block (parser);
35348 tree ret = cp_parser_omp_for (parser, pragma_tok, p_name, mask, cclauses,
35349 if_p);
35350 cp_parser_end_omp_structured_block (parser, save);
35351 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35352 block);
35353 if (ret == NULL_TREE)
35354 return ret;
35355 OMP_PARALLEL_COMBINED (stmt) = 1;
35356 return stmt;
35358 /* When combined with distribute, parallel has to be followed by for.
35359 #pragma omp target parallel is allowed though. */
35360 else if (cclauses
35361 && (mask & (OMP_CLAUSE_MASK_1
35362 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)) != 0)
35364 error_at (loc, "expected %<for%> after %qs", p_name);
35365 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35366 return NULL_TREE;
35368 else if (!flag_openmp) /* flag_openmp_simd */
35370 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35371 return NULL_TREE;
35373 else if (cclauses == NULL && cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35375 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35376 const char *p = IDENTIFIER_POINTER (id);
35377 if (strcmp (p, "sections") == 0)
35379 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35380 cclauses = cclauses_buf;
35382 cp_lexer_consume_token (parser->lexer);
35383 block = begin_omp_parallel ();
35384 save = cp_parser_begin_omp_structured_block (parser);
35385 cp_parser_omp_sections (parser, pragma_tok, p_name, mask, cclauses);
35386 cp_parser_end_omp_structured_block (parser, save);
35387 stmt = finish_omp_parallel (cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL],
35388 block);
35389 OMP_PARALLEL_COMBINED (stmt) = 1;
35390 return stmt;
35394 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35395 cclauses == NULL);
35396 if (cclauses)
35398 cp_omp_split_clauses (loc, OMP_PARALLEL, mask, clauses, cclauses);
35399 clauses = cclauses[C_OMP_CLAUSE_SPLIT_PARALLEL];
35402 block = begin_omp_parallel ();
35403 save = cp_parser_begin_omp_structured_block (parser);
35404 cp_parser_statement (parser, NULL_TREE, false, if_p);
35405 cp_parser_end_omp_structured_block (parser, save);
35406 stmt = finish_omp_parallel (clauses, block);
35407 return stmt;
35410 /* OpenMP 2.5:
35411 # pragma omp single single-clause[optseq] new-line
35412 structured-block */
35414 #define OMP_SINGLE_CLAUSE_MASK \
35415 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35416 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35417 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
35418 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35420 static tree
35421 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35423 tree stmt = make_node (OMP_SINGLE);
35424 TREE_TYPE (stmt) = void_type_node;
35426 OMP_SINGLE_CLAUSES (stmt)
35427 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
35428 "#pragma omp single", pragma_tok);
35429 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35431 return add_stmt (stmt);
35434 /* OpenMP 3.0:
35435 # pragma omp task task-clause[optseq] new-line
35436 structured-block */
35438 #define OMP_TASK_CLAUSE_MASK \
35439 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35440 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
35441 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
35442 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35443 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35444 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35445 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
35446 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
35447 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35448 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
35450 static tree
35451 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35453 tree clauses, block;
35454 unsigned int save;
35456 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
35457 "#pragma omp task", pragma_tok);
35458 block = begin_omp_task ();
35459 save = cp_parser_begin_omp_structured_block (parser);
35460 cp_parser_statement (parser, NULL_TREE, false, if_p);
35461 cp_parser_end_omp_structured_block (parser, save);
35462 return finish_omp_task (clauses, block);
35465 /* OpenMP 3.0:
35466 # pragma omp taskwait new-line */
35468 static void
35469 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
35471 cp_parser_require_pragma_eol (parser, pragma_tok);
35472 finish_omp_taskwait ();
35475 /* OpenMP 3.1:
35476 # pragma omp taskyield new-line */
35478 static void
35479 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
35481 cp_parser_require_pragma_eol (parser, pragma_tok);
35482 finish_omp_taskyield ();
35485 /* OpenMP 4.0:
35486 # pragma omp taskgroup new-line
35487 structured-block */
35489 static tree
35490 cp_parser_omp_taskgroup (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35492 cp_parser_require_pragma_eol (parser, pragma_tok);
35493 return c_finish_omp_taskgroup (input_location,
35494 cp_parser_omp_structured_block (parser,
35495 if_p));
35499 /* OpenMP 2.5:
35500 # pragma omp threadprivate (variable-list) */
35502 static void
35503 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
35505 tree vars;
35507 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
35508 cp_parser_require_pragma_eol (parser, pragma_tok);
35510 finish_omp_threadprivate (vars);
35513 /* OpenMP 4.0:
35514 # pragma omp cancel cancel-clause[optseq] new-line */
35516 #define OMP_CANCEL_CLAUSE_MASK \
35517 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35519 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35520 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP) \
35521 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF))
35523 static void
35524 cp_parser_omp_cancel (cp_parser *parser, cp_token *pragma_tok)
35526 tree clauses = cp_parser_omp_all_clauses (parser, OMP_CANCEL_CLAUSE_MASK,
35527 "#pragma omp cancel", pragma_tok);
35528 finish_omp_cancel (clauses);
35531 /* OpenMP 4.0:
35532 # pragma omp cancellation point cancelpt-clause[optseq] new-line */
35534 #define OMP_CANCELLATION_POINT_CLAUSE_MASK \
35535 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PARALLEL) \
35536 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FOR) \
35537 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SECTIONS) \
35538 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TASKGROUP))
35540 static void
35541 cp_parser_omp_cancellation_point (cp_parser *parser, cp_token *pragma_tok,
35542 enum pragma_context context)
35544 tree clauses;
35545 bool point_seen = false;
35547 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35549 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35550 const char *p = IDENTIFIER_POINTER (id);
35552 if (strcmp (p, "point") == 0)
35554 cp_lexer_consume_token (parser->lexer);
35555 point_seen = true;
35558 if (!point_seen)
35560 cp_parser_error (parser, "expected %<point%>");
35561 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35562 return;
35565 if (context != pragma_compound)
35567 if (context == pragma_stmt)
35568 error_at (pragma_tok->location,
35569 "%<#pragma %s%> may only be used in compound statements",
35570 "omp cancellation point");
35571 else
35572 cp_parser_error (parser, "expected declaration specifiers");
35573 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35574 return;
35577 clauses = cp_parser_omp_all_clauses (parser,
35578 OMP_CANCELLATION_POINT_CLAUSE_MASK,
35579 "#pragma omp cancellation point",
35580 pragma_tok);
35581 finish_omp_cancellation_point (clauses);
35584 /* OpenMP 4.0:
35585 #pragma omp distribute distribute-clause[optseq] new-line
35586 for-loop */
35588 #define OMP_DISTRIBUTE_CLAUSE_MASK \
35589 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35590 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35591 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
35592 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DIST_SCHEDULE)\
35593 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE))
35595 static tree
35596 cp_parser_omp_distribute (cp_parser *parser, cp_token *pragma_tok,
35597 char *p_name, omp_clause_mask mask, tree *cclauses,
35598 bool *if_p)
35600 tree clauses, sb, ret;
35601 unsigned int save;
35602 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35604 strcat (p_name, " distribute");
35605 mask |= OMP_DISTRIBUTE_CLAUSE_MASK;
35607 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35609 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35610 const char *p = IDENTIFIER_POINTER (id);
35611 bool simd = false;
35612 bool parallel = false;
35614 if (strcmp (p, "simd") == 0)
35615 simd = true;
35616 else
35617 parallel = strcmp (p, "parallel") == 0;
35618 if (parallel || simd)
35620 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35621 if (cclauses == NULL)
35622 cclauses = cclauses_buf;
35623 cp_lexer_consume_token (parser->lexer);
35624 if (!flag_openmp) /* flag_openmp_simd */
35626 if (simd)
35627 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35628 cclauses, if_p);
35629 else
35630 return cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35631 cclauses, if_p);
35633 sb = begin_omp_structured_block ();
35634 save = cp_parser_begin_omp_structured_block (parser);
35635 if (simd)
35636 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
35637 cclauses, if_p);
35638 else
35639 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask,
35640 cclauses, if_p);
35641 cp_parser_end_omp_structured_block (parser, save);
35642 tree body = finish_omp_structured_block (sb);
35643 if (ret == NULL)
35644 return ret;
35645 ret = make_node (OMP_DISTRIBUTE);
35646 TREE_TYPE (ret) = void_type_node;
35647 OMP_FOR_BODY (ret) = body;
35648 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35649 SET_EXPR_LOCATION (ret, loc);
35650 add_stmt (ret);
35651 return ret;
35654 if (!flag_openmp) /* flag_openmp_simd */
35656 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35657 return NULL_TREE;
35660 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35661 cclauses == NULL);
35662 if (cclauses)
35664 cp_omp_split_clauses (loc, OMP_DISTRIBUTE, mask, clauses, cclauses);
35665 clauses = cclauses[C_OMP_CLAUSE_SPLIT_DISTRIBUTE];
35668 sb = begin_omp_structured_block ();
35669 save = cp_parser_begin_omp_structured_block (parser);
35671 ret = cp_parser_omp_for_loop (parser, OMP_DISTRIBUTE, clauses, NULL, if_p);
35673 cp_parser_end_omp_structured_block (parser, save);
35674 add_stmt (finish_omp_structured_block (sb));
35676 return ret;
35679 /* OpenMP 4.0:
35680 # pragma omp teams teams-clause[optseq] new-line
35681 structured-block */
35683 #define OMP_TEAMS_CLAUSE_MASK \
35684 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
35685 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
35686 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
35687 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_REDUCTION) \
35688 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TEAMS) \
35689 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_THREAD_LIMIT) \
35690 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT))
35692 static tree
35693 cp_parser_omp_teams (cp_parser *parser, cp_token *pragma_tok,
35694 char *p_name, omp_clause_mask mask, tree *cclauses,
35695 bool *if_p)
35697 tree clauses, sb, ret;
35698 unsigned int save;
35699 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
35701 strcat (p_name, " teams");
35702 mask |= OMP_TEAMS_CLAUSE_MASK;
35704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35706 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35707 const char *p = IDENTIFIER_POINTER (id);
35708 if (strcmp (p, "distribute") == 0)
35710 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
35711 if (cclauses == NULL)
35712 cclauses = cclauses_buf;
35714 cp_lexer_consume_token (parser->lexer);
35715 if (!flag_openmp) /* flag_openmp_simd */
35716 return cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35717 cclauses, if_p);
35718 sb = begin_omp_structured_block ();
35719 save = cp_parser_begin_omp_structured_block (parser);
35720 ret = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask,
35721 cclauses, if_p);
35722 cp_parser_end_omp_structured_block (parser, save);
35723 tree body = finish_omp_structured_block (sb);
35724 if (ret == NULL)
35725 return ret;
35726 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35727 ret = make_node (OMP_TEAMS);
35728 TREE_TYPE (ret) = void_type_node;
35729 OMP_TEAMS_CLAUSES (ret) = clauses;
35730 OMP_TEAMS_BODY (ret) = body;
35731 OMP_TEAMS_COMBINED (ret) = 1;
35732 SET_EXPR_LOCATION (ret, loc);
35733 return add_stmt (ret);
35736 if (!flag_openmp) /* flag_openmp_simd */
35738 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35739 return NULL_TREE;
35742 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
35743 cclauses == NULL);
35744 if (cclauses)
35746 cp_omp_split_clauses (loc, OMP_TEAMS, mask, clauses, cclauses);
35747 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
35750 tree stmt = make_node (OMP_TEAMS);
35751 TREE_TYPE (stmt) = void_type_node;
35752 OMP_TEAMS_CLAUSES (stmt) = clauses;
35753 OMP_TEAMS_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35754 SET_EXPR_LOCATION (stmt, loc);
35756 return add_stmt (stmt);
35759 /* OpenMP 4.0:
35760 # pragma omp target data target-data-clause[optseq] new-line
35761 structured-block */
35763 #define OMP_TARGET_DATA_CLAUSE_MASK \
35764 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35767 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_USE_DEVICE_PTR))
35769 static tree
35770 cp_parser_omp_target_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
35772 tree clauses
35773 = cp_parser_omp_all_clauses (parser, OMP_TARGET_DATA_CLAUSE_MASK,
35774 "#pragma omp target data", pragma_tok);
35775 int map_seen = 0;
35776 for (tree *pc = &clauses; *pc;)
35778 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35779 switch (OMP_CLAUSE_MAP_KIND (*pc))
35781 case GOMP_MAP_TO:
35782 case GOMP_MAP_ALWAYS_TO:
35783 case GOMP_MAP_FROM:
35784 case GOMP_MAP_ALWAYS_FROM:
35785 case GOMP_MAP_TOFROM:
35786 case GOMP_MAP_ALWAYS_TOFROM:
35787 case GOMP_MAP_ALLOC:
35788 map_seen = 3;
35789 break;
35790 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35791 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35792 case GOMP_MAP_ALWAYS_POINTER:
35793 break;
35794 default:
35795 map_seen |= 1;
35796 error_at (OMP_CLAUSE_LOCATION (*pc),
35797 "%<#pragma omp target data%> with map-type other "
35798 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
35799 "on %<map%> clause");
35800 *pc = OMP_CLAUSE_CHAIN (*pc);
35801 continue;
35803 pc = &OMP_CLAUSE_CHAIN (*pc);
35806 if (map_seen != 3)
35808 if (map_seen == 0)
35809 error_at (pragma_tok->location,
35810 "%<#pragma omp target data%> must contain at least "
35811 "one %<map%> clause");
35812 return NULL_TREE;
35815 tree stmt = make_node (OMP_TARGET_DATA);
35816 TREE_TYPE (stmt) = void_type_node;
35817 OMP_TARGET_DATA_CLAUSES (stmt) = clauses;
35819 keep_next_level (true);
35820 OMP_TARGET_DATA_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
35822 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35823 return add_stmt (stmt);
35826 /* OpenMP 4.5:
35827 # pragma omp target enter data target-enter-data-clause[optseq] new-line
35828 structured-block */
35830 #define OMP_TARGET_ENTER_DATA_CLAUSE_MASK \
35831 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35832 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35833 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35834 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35835 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35837 static tree
35838 cp_parser_omp_target_enter_data (cp_parser *parser, cp_token *pragma_tok,
35839 enum pragma_context context)
35841 bool data_seen = false;
35842 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35844 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35845 const char *p = IDENTIFIER_POINTER (id);
35847 if (strcmp (p, "data") == 0)
35849 cp_lexer_consume_token (parser->lexer);
35850 data_seen = true;
35853 if (!data_seen)
35855 cp_parser_error (parser, "expected %<data%>");
35856 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35857 return NULL_TREE;
35860 if (context == pragma_stmt)
35862 error_at (pragma_tok->location,
35863 "%<#pragma %s%> may only be used in compound statements",
35864 "omp target enter data");
35865 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35866 return NULL_TREE;
35869 tree clauses
35870 = cp_parser_omp_all_clauses (parser, OMP_TARGET_ENTER_DATA_CLAUSE_MASK,
35871 "#pragma omp target enter data", pragma_tok);
35872 int map_seen = 0;
35873 for (tree *pc = &clauses; *pc;)
35875 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35876 switch (OMP_CLAUSE_MAP_KIND (*pc))
35878 case GOMP_MAP_TO:
35879 case GOMP_MAP_ALWAYS_TO:
35880 case GOMP_MAP_ALLOC:
35881 map_seen = 3;
35882 break;
35883 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35884 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35885 case GOMP_MAP_ALWAYS_POINTER:
35886 break;
35887 default:
35888 map_seen |= 1;
35889 error_at (OMP_CLAUSE_LOCATION (*pc),
35890 "%<#pragma omp target enter data%> with map-type other "
35891 "than %<to%> or %<alloc%> on %<map%> clause");
35892 *pc = OMP_CLAUSE_CHAIN (*pc);
35893 continue;
35895 pc = &OMP_CLAUSE_CHAIN (*pc);
35898 if (map_seen != 3)
35900 if (map_seen == 0)
35901 error_at (pragma_tok->location,
35902 "%<#pragma omp target enter data%> must contain at least "
35903 "one %<map%> clause");
35904 return NULL_TREE;
35907 tree stmt = make_node (OMP_TARGET_ENTER_DATA);
35908 TREE_TYPE (stmt) = void_type_node;
35909 OMP_TARGET_ENTER_DATA_CLAUSES (stmt) = clauses;
35910 SET_EXPR_LOCATION (stmt, pragma_tok->location);
35911 return add_stmt (stmt);
35914 /* OpenMP 4.5:
35915 # pragma omp target exit data target-enter-data-clause[optseq] new-line
35916 structured-block */
35918 #define OMP_TARGET_EXIT_DATA_CLAUSE_MASK \
35919 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
35920 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
35921 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
35922 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
35923 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
35925 static tree
35926 cp_parser_omp_target_exit_data (cp_parser *parser, cp_token *pragma_tok,
35927 enum pragma_context context)
35929 bool data_seen = false;
35930 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
35932 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
35933 const char *p = IDENTIFIER_POINTER (id);
35935 if (strcmp (p, "data") == 0)
35937 cp_lexer_consume_token (parser->lexer);
35938 data_seen = true;
35941 if (!data_seen)
35943 cp_parser_error (parser, "expected %<data%>");
35944 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35945 return NULL_TREE;
35948 if (context == pragma_stmt)
35950 error_at (pragma_tok->location,
35951 "%<#pragma %s%> may only be used in compound statements",
35952 "omp target exit data");
35953 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
35954 return NULL_TREE;
35957 tree clauses
35958 = cp_parser_omp_all_clauses (parser, OMP_TARGET_EXIT_DATA_CLAUSE_MASK,
35959 "#pragma omp target exit data", pragma_tok);
35960 int map_seen = 0;
35961 for (tree *pc = &clauses; *pc;)
35963 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
35964 switch (OMP_CLAUSE_MAP_KIND (*pc))
35966 case GOMP_MAP_FROM:
35967 case GOMP_MAP_ALWAYS_FROM:
35968 case GOMP_MAP_RELEASE:
35969 case GOMP_MAP_DELETE:
35970 map_seen = 3;
35971 break;
35972 case GOMP_MAP_FIRSTPRIVATE_POINTER:
35973 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
35974 case GOMP_MAP_ALWAYS_POINTER:
35975 break;
35976 default:
35977 map_seen |= 1;
35978 error_at (OMP_CLAUSE_LOCATION (*pc),
35979 "%<#pragma omp target exit data%> with map-type other "
35980 "than %<from%>, %<release%> or %<delete%> on %<map%>"
35981 " clause");
35982 *pc = OMP_CLAUSE_CHAIN (*pc);
35983 continue;
35985 pc = &OMP_CLAUSE_CHAIN (*pc);
35988 if (map_seen != 3)
35990 if (map_seen == 0)
35991 error_at (pragma_tok->location,
35992 "%<#pragma omp target exit data%> must contain at least "
35993 "one %<map%> clause");
35994 return NULL_TREE;
35997 tree stmt = make_node (OMP_TARGET_EXIT_DATA);
35998 TREE_TYPE (stmt) = void_type_node;
35999 OMP_TARGET_EXIT_DATA_CLAUSES (stmt) = clauses;
36000 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36001 return add_stmt (stmt);
36004 /* OpenMP 4.0:
36005 # pragma omp target update target-update-clause[optseq] new-line */
36007 #define OMP_TARGET_UPDATE_CLAUSE_MASK \
36008 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FROM) \
36009 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36010 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36011 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36012 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36013 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT))
36015 static bool
36016 cp_parser_omp_target_update (cp_parser *parser, cp_token *pragma_tok,
36017 enum pragma_context context)
36019 if (context == pragma_stmt)
36021 error_at (pragma_tok->location,
36022 "%<#pragma %s%> may only be used in compound statements",
36023 "omp target update");
36024 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36025 return false;
36028 tree clauses
36029 = cp_parser_omp_all_clauses (parser, OMP_TARGET_UPDATE_CLAUSE_MASK,
36030 "#pragma omp target update", pragma_tok);
36031 if (omp_find_clause (clauses, OMP_CLAUSE_TO) == NULL_TREE
36032 && omp_find_clause (clauses, OMP_CLAUSE_FROM) == NULL_TREE)
36034 error_at (pragma_tok->location,
36035 "%<#pragma omp target update%> must contain at least one "
36036 "%<from%> or %<to%> clauses");
36037 return false;
36040 tree stmt = make_node (OMP_TARGET_UPDATE);
36041 TREE_TYPE (stmt) = void_type_node;
36042 OMP_TARGET_UPDATE_CLAUSES (stmt) = clauses;
36043 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36044 add_stmt (stmt);
36045 return false;
36048 /* OpenMP 4.0:
36049 # pragma omp target target-clause[optseq] new-line
36050 structured-block */
36052 #define OMP_TARGET_CLAUSE_MASK \
36053 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEVICE) \
36054 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MAP) \
36055 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
36056 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEPEND) \
36057 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOWAIT) \
36058 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
36059 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
36060 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULTMAP) \
36061 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IS_DEVICE_PTR))
36063 static bool
36064 cp_parser_omp_target (cp_parser *parser, cp_token *pragma_tok,
36065 enum pragma_context context, bool *if_p)
36067 tree *pc = NULL, stmt;
36069 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36071 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
36072 const char *p = IDENTIFIER_POINTER (id);
36073 enum tree_code ccode = ERROR_MARK;
36075 if (strcmp (p, "teams") == 0)
36076 ccode = OMP_TEAMS;
36077 else if (strcmp (p, "parallel") == 0)
36078 ccode = OMP_PARALLEL;
36079 else if (strcmp (p, "simd") == 0)
36080 ccode = OMP_SIMD;
36081 if (ccode != ERROR_MARK)
36083 tree cclauses[C_OMP_CLAUSE_SPLIT_COUNT];
36084 char p_name[sizeof ("#pragma omp target teams distribute "
36085 "parallel for simd")];
36087 cp_lexer_consume_token (parser->lexer);
36088 strcpy (p_name, "#pragma omp target");
36089 if (!flag_openmp) /* flag_openmp_simd */
36091 tree stmt;
36092 switch (ccode)
36094 case OMP_TEAMS:
36095 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name,
36096 OMP_TARGET_CLAUSE_MASK,
36097 cclauses, if_p);
36098 break;
36099 case OMP_PARALLEL:
36100 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36101 OMP_TARGET_CLAUSE_MASK,
36102 cclauses, if_p);
36103 break;
36104 case OMP_SIMD:
36105 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name,
36106 OMP_TARGET_CLAUSE_MASK,
36107 cclauses, if_p);
36108 break;
36109 default:
36110 gcc_unreachable ();
36112 return stmt != NULL_TREE;
36114 keep_next_level (true);
36115 tree sb = begin_omp_structured_block (), ret;
36116 unsigned save = cp_parser_begin_omp_structured_block (parser);
36117 switch (ccode)
36119 case OMP_TEAMS:
36120 ret = cp_parser_omp_teams (parser, pragma_tok, p_name,
36121 OMP_TARGET_CLAUSE_MASK, cclauses,
36122 if_p);
36123 break;
36124 case OMP_PARALLEL:
36125 ret = cp_parser_omp_parallel (parser, pragma_tok, p_name,
36126 OMP_TARGET_CLAUSE_MASK, cclauses,
36127 if_p);
36128 break;
36129 case OMP_SIMD:
36130 ret = cp_parser_omp_simd (parser, pragma_tok, p_name,
36131 OMP_TARGET_CLAUSE_MASK, cclauses,
36132 if_p);
36133 break;
36134 default:
36135 gcc_unreachable ();
36137 cp_parser_end_omp_structured_block (parser, save);
36138 tree body = finish_omp_structured_block (sb);
36139 if (ret == NULL_TREE)
36140 return false;
36141 if (ccode == OMP_TEAMS && !processing_template_decl)
36143 /* For combined target teams, ensure the num_teams and
36144 thread_limit clause expressions are evaluated on the host,
36145 before entering the target construct. */
36146 tree c;
36147 for (c = cclauses[C_OMP_CLAUSE_SPLIT_TEAMS];
36148 c; c = OMP_CLAUSE_CHAIN (c))
36149 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_NUM_TEAMS
36150 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_THREAD_LIMIT)
36151 && TREE_CODE (OMP_CLAUSE_OPERAND (c, 0)) != INTEGER_CST)
36153 tree expr = OMP_CLAUSE_OPERAND (c, 0);
36154 expr = force_target_expr (TREE_TYPE (expr), expr, tf_none);
36155 if (expr == error_mark_node)
36156 continue;
36157 tree tmp = TARGET_EXPR_SLOT (expr);
36158 add_stmt (expr);
36159 OMP_CLAUSE_OPERAND (c, 0) = expr;
36160 tree tc = build_omp_clause (OMP_CLAUSE_LOCATION (c),
36161 OMP_CLAUSE_FIRSTPRIVATE);
36162 OMP_CLAUSE_DECL (tc) = tmp;
36163 OMP_CLAUSE_CHAIN (tc)
36164 = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36165 cclauses[C_OMP_CLAUSE_SPLIT_TARGET] = tc;
36168 tree stmt = make_node (OMP_TARGET);
36169 TREE_TYPE (stmt) = void_type_node;
36170 OMP_TARGET_CLAUSES (stmt) = cclauses[C_OMP_CLAUSE_SPLIT_TARGET];
36171 OMP_TARGET_BODY (stmt) = body;
36172 OMP_TARGET_COMBINED (stmt) = 1;
36173 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36174 add_stmt (stmt);
36175 pc = &OMP_TARGET_CLAUSES (stmt);
36176 goto check_clauses;
36178 else if (!flag_openmp) /* flag_openmp_simd */
36180 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36181 return false;
36183 else if (strcmp (p, "data") == 0)
36185 cp_lexer_consume_token (parser->lexer);
36186 cp_parser_omp_target_data (parser, pragma_tok, if_p);
36187 return true;
36189 else if (strcmp (p, "enter") == 0)
36191 cp_lexer_consume_token (parser->lexer);
36192 cp_parser_omp_target_enter_data (parser, pragma_tok, context);
36193 return false;
36195 else if (strcmp (p, "exit") == 0)
36197 cp_lexer_consume_token (parser->lexer);
36198 cp_parser_omp_target_exit_data (parser, pragma_tok, context);
36199 return false;
36201 else if (strcmp (p, "update") == 0)
36203 cp_lexer_consume_token (parser->lexer);
36204 return cp_parser_omp_target_update (parser, pragma_tok, context);
36207 if (!flag_openmp) /* flag_openmp_simd */
36209 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36210 return false;
36213 stmt = make_node (OMP_TARGET);
36214 TREE_TYPE (stmt) = void_type_node;
36216 OMP_TARGET_CLAUSES (stmt)
36217 = cp_parser_omp_all_clauses (parser, OMP_TARGET_CLAUSE_MASK,
36218 "#pragma omp target", pragma_tok);
36219 pc = &OMP_TARGET_CLAUSES (stmt);
36220 keep_next_level (true);
36221 OMP_TARGET_BODY (stmt) = cp_parser_omp_structured_block (parser, if_p);
36223 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36224 add_stmt (stmt);
36226 check_clauses:
36227 while (*pc)
36229 if (OMP_CLAUSE_CODE (*pc) == OMP_CLAUSE_MAP)
36230 switch (OMP_CLAUSE_MAP_KIND (*pc))
36232 case GOMP_MAP_TO:
36233 case GOMP_MAP_ALWAYS_TO:
36234 case GOMP_MAP_FROM:
36235 case GOMP_MAP_ALWAYS_FROM:
36236 case GOMP_MAP_TOFROM:
36237 case GOMP_MAP_ALWAYS_TOFROM:
36238 case GOMP_MAP_ALLOC:
36239 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36240 case GOMP_MAP_FIRSTPRIVATE_REFERENCE:
36241 case GOMP_MAP_ALWAYS_POINTER:
36242 break;
36243 default:
36244 error_at (OMP_CLAUSE_LOCATION (*pc),
36245 "%<#pragma omp target%> with map-type other "
36246 "than %<to%>, %<from%>, %<tofrom%> or %<alloc%> "
36247 "on %<map%> clause");
36248 *pc = OMP_CLAUSE_CHAIN (*pc);
36249 continue;
36251 pc = &OMP_CLAUSE_CHAIN (*pc);
36253 return true;
36256 /* OpenACC 2.0:
36257 # pragma acc cache (variable-list) new-line
36260 static tree
36261 cp_parser_oacc_cache (cp_parser *parser, cp_token *pragma_tok)
36263 tree stmt, clauses;
36265 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE__CACHE_, NULL_TREE);
36266 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36268 cp_parser_require_pragma_eol (parser, cp_lexer_peek_token (parser->lexer));
36270 stmt = make_node (OACC_CACHE);
36271 TREE_TYPE (stmt) = void_type_node;
36272 OACC_CACHE_CLAUSES (stmt) = clauses;
36273 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36274 add_stmt (stmt);
36276 return stmt;
36279 /* OpenACC 2.0:
36280 # pragma acc data oacc-data-clause[optseq] new-line
36281 structured-block */
36283 #define OACC_DATA_CLAUSE_MASK \
36284 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36285 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36286 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36287 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36288 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36289 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36290 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36291 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36292 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36293 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36294 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36296 static tree
36297 cp_parser_oacc_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36299 tree stmt, clauses, block;
36300 unsigned int save;
36302 clauses = cp_parser_oacc_all_clauses (parser, OACC_DATA_CLAUSE_MASK,
36303 "#pragma acc data", pragma_tok);
36305 block = begin_omp_parallel ();
36306 save = cp_parser_begin_omp_structured_block (parser);
36307 cp_parser_statement (parser, NULL_TREE, false, if_p);
36308 cp_parser_end_omp_structured_block (parser, save);
36309 stmt = finish_oacc_data (clauses, block);
36310 return stmt;
36313 /* OpenACC 2.0:
36314 # pragma acc host_data <clauses> new-line
36315 structured-block */
36317 #define OACC_HOST_DATA_CLAUSE_MASK \
36318 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_USE_DEVICE) )
36320 static tree
36321 cp_parser_oacc_host_data (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
36323 tree stmt, clauses, block;
36324 unsigned int save;
36326 clauses = cp_parser_oacc_all_clauses (parser, OACC_HOST_DATA_CLAUSE_MASK,
36327 "#pragma acc host_data", pragma_tok);
36329 block = begin_omp_parallel ();
36330 save = cp_parser_begin_omp_structured_block (parser);
36331 cp_parser_statement (parser, NULL_TREE, false, if_p);
36332 cp_parser_end_omp_structured_block (parser, save);
36333 stmt = finish_oacc_host_data (clauses, block);
36334 return stmt;
36337 /* OpenACC 2.0:
36338 # pragma acc declare oacc-data-clause[optseq] new-line
36341 #define OACC_DECLARE_CLAUSE_MASK \
36342 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36343 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36344 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36345 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36346 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36347 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE_RESIDENT) \
36348 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_LINK) \
36349 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36350 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36351 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36352 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36353 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE))
36355 static tree
36356 cp_parser_oacc_declare (cp_parser *parser, cp_token *pragma_tok)
36358 tree clauses, stmt;
36359 bool error = false;
36361 clauses = cp_parser_oacc_all_clauses (parser, OACC_DECLARE_CLAUSE_MASK,
36362 "#pragma acc declare", pragma_tok, true);
36365 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36367 error_at (pragma_tok->location,
36368 "no valid clauses specified in %<#pragma acc declare%>");
36369 return NULL_TREE;
36372 for (tree t = clauses; t; t = OMP_CLAUSE_CHAIN (t))
36374 location_t loc = OMP_CLAUSE_LOCATION (t);
36375 tree decl = OMP_CLAUSE_DECL (t);
36376 if (!DECL_P (decl))
36378 error_at (loc, "array section in %<#pragma acc declare%>");
36379 error = true;
36380 continue;
36382 gcc_assert (OMP_CLAUSE_CODE (t) == OMP_CLAUSE_MAP);
36383 switch (OMP_CLAUSE_MAP_KIND (t))
36385 case GOMP_MAP_FIRSTPRIVATE_POINTER:
36386 case GOMP_MAP_FORCE_ALLOC:
36387 case GOMP_MAP_FORCE_TO:
36388 case GOMP_MAP_FORCE_DEVICEPTR:
36389 case GOMP_MAP_DEVICE_RESIDENT:
36390 break;
36392 case GOMP_MAP_LINK:
36393 if (!global_bindings_p ()
36394 && (TREE_STATIC (decl)
36395 || !DECL_EXTERNAL (decl)))
36397 error_at (loc,
36398 "%qD must be a global variable in "
36399 "%<#pragma acc declare link%>",
36400 decl);
36401 error = true;
36402 continue;
36404 break;
36406 default:
36407 if (global_bindings_p ())
36409 error_at (loc, "invalid OpenACC clause at file scope");
36410 error = true;
36411 continue;
36413 if (DECL_EXTERNAL (decl))
36415 error_at (loc,
36416 "invalid use of %<extern%> variable %qD "
36417 "in %<#pragma acc declare%>", decl);
36418 error = true;
36419 continue;
36421 else if (TREE_PUBLIC (decl))
36423 error_at (loc,
36424 "invalid use of %<global%> variable %qD "
36425 "in %<#pragma acc declare%>", decl);
36426 error = true;
36427 continue;
36429 break;
36432 if (lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl))
36433 || lookup_attribute ("omp declare target link",
36434 DECL_ATTRIBUTES (decl)))
36436 error_at (loc, "variable %qD used more than once with "
36437 "%<#pragma acc declare%>", decl);
36438 error = true;
36439 continue;
36442 if (!error)
36444 tree id;
36446 if (OMP_CLAUSE_MAP_KIND (t) == GOMP_MAP_LINK)
36447 id = get_identifier ("omp declare target link");
36448 else
36449 id = get_identifier ("omp declare target");
36451 DECL_ATTRIBUTES (decl)
36452 = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
36453 if (global_bindings_p ())
36455 symtab_node *node = symtab_node::get (decl);
36456 if (node != NULL)
36458 node->offloadable = 1;
36459 if (ENABLE_OFFLOADING)
36461 g->have_offload = true;
36462 if (is_a <varpool_node *> (node))
36463 vec_safe_push (offload_vars, decl);
36470 if (error || global_bindings_p ())
36471 return NULL_TREE;
36473 stmt = make_node (OACC_DECLARE);
36474 TREE_TYPE (stmt) = void_type_node;
36475 OACC_DECLARE_CLAUSES (stmt) = clauses;
36476 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36478 add_stmt (stmt);
36480 return NULL_TREE;
36483 /* OpenACC 2.0:
36484 # pragma acc enter data oacc-enter-data-clause[optseq] new-line
36488 # pragma acc exit data oacc-exit-data-clause[optseq] new-line
36490 LOC is the location of the #pragma token.
36493 #define OACC_ENTER_DATA_CLAUSE_MASK \
36494 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36495 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36496 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36497 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36498 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36499 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36500 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36502 #define OACC_EXIT_DATA_CLAUSE_MASK \
36503 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36504 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36505 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DELETE) \
36507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36509 static tree
36510 cp_parser_oacc_enter_exit_data (cp_parser *parser, cp_token *pragma_tok,
36511 bool enter)
36513 location_t loc = pragma_tok->location;
36514 tree stmt, clauses;
36515 const char *p = "";
36517 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36518 p = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36520 if (strcmp (p, "data") != 0)
36522 error_at (loc, "expected %<data%> after %<#pragma acc %s%>",
36523 enter ? "enter" : "exit");
36524 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
36525 return NULL_TREE;
36528 cp_lexer_consume_token (parser->lexer);
36530 if (enter)
36531 clauses = cp_parser_oacc_all_clauses (parser, OACC_ENTER_DATA_CLAUSE_MASK,
36532 "#pragma acc enter data", pragma_tok);
36533 else
36534 clauses = cp_parser_oacc_all_clauses (parser, OACC_EXIT_DATA_CLAUSE_MASK,
36535 "#pragma acc exit data", pragma_tok);
36537 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36539 error_at (loc, "%<#pragma acc %s data%> has no data movement clause",
36540 enter ? "enter" : "exit");
36541 return NULL_TREE;
36544 stmt = enter ? make_node (OACC_ENTER_DATA) : make_node (OACC_EXIT_DATA);
36545 TREE_TYPE (stmt) = void_type_node;
36546 OMP_STANDALONE_CLAUSES (stmt) = clauses;
36547 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36548 add_stmt (stmt);
36549 return stmt;
36552 /* OpenACC 2.0:
36553 # pragma acc loop oacc-loop-clause[optseq] new-line
36554 structured-block */
36556 #define OACC_LOOP_CLAUSE_MASK \
36557 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COLLAPSE) \
36558 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36559 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36560 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
36561 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
36562 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
36563 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_AUTO) \
36564 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_INDEPENDENT) \
36565 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ) \
36566 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_TILE))
36568 static tree
36569 cp_parser_oacc_loop (cp_parser *parser, cp_token *pragma_tok, char *p_name,
36570 omp_clause_mask mask, tree *cclauses, bool *if_p)
36572 bool is_parallel = ((mask >> PRAGMA_OACC_CLAUSE_REDUCTION) & 1) == 1;
36574 strcat (p_name, " loop");
36575 mask |= OACC_LOOP_CLAUSE_MASK;
36577 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok,
36578 cclauses == NULL);
36579 if (cclauses)
36581 clauses = c_oacc_split_loop_clauses (clauses, cclauses, is_parallel);
36582 if (*cclauses)
36583 *cclauses = finish_omp_clauses (*cclauses, C_ORT_ACC);
36584 if (clauses)
36585 clauses = finish_omp_clauses (clauses, C_ORT_ACC);
36588 tree block = begin_omp_structured_block ();
36589 int save = cp_parser_begin_omp_structured_block (parser);
36590 tree stmt = cp_parser_omp_for_loop (parser, OACC_LOOP, clauses, NULL, if_p);
36591 cp_parser_end_omp_structured_block (parser, save);
36592 add_stmt (finish_omp_structured_block (block));
36594 return stmt;
36597 /* OpenACC 2.0:
36598 # pragma acc kernels oacc-kernels-clause[optseq] new-line
36599 structured-block
36603 # pragma acc parallel oacc-parallel-clause[optseq] new-line
36604 structured-block
36607 #define OACC_KERNELS_CLAUSE_MASK \
36608 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36609 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36610 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36611 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36612 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36613 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36614 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36615 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36616 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36617 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36618 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36619 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36620 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36621 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36622 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36623 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36624 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36626 #define OACC_PARALLEL_CLAUSE_MASK \
36627 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36628 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPY) \
36629 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYIN) \
36630 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_COPYOUT) \
36631 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_CREATE) \
36632 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEFAULT) \
36633 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICEPTR) \
36634 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_FIRSTPRIVATE) \
36635 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36636 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_GANGS) \
36637 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_NUM_WORKERS) \
36638 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT) \
36639 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPY) \
36640 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYIN) \
36641 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_COPYOUT) \
36642 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRESENT_OR_CREATE) \
36643 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_PRIVATE) \
36644 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_REDUCTION) \
36645 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR_LENGTH) \
36646 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT) )
36648 static tree
36649 cp_parser_oacc_kernels_parallel (cp_parser *parser, cp_token *pragma_tok,
36650 char *p_name, bool *if_p)
36652 omp_clause_mask mask;
36653 enum tree_code code;
36654 switch (cp_parser_pragma_kind (pragma_tok))
36656 case PRAGMA_OACC_KERNELS:
36657 strcat (p_name, " kernels");
36658 mask = OACC_KERNELS_CLAUSE_MASK;
36659 code = OACC_KERNELS;
36660 break;
36661 case PRAGMA_OACC_PARALLEL:
36662 strcat (p_name, " parallel");
36663 mask = OACC_PARALLEL_CLAUSE_MASK;
36664 code = OACC_PARALLEL;
36665 break;
36666 default:
36667 gcc_unreachable ();
36670 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36672 const char *p
36673 = IDENTIFIER_POINTER (cp_lexer_peek_token (parser->lexer)->u.value);
36674 if (strcmp (p, "loop") == 0)
36676 cp_lexer_consume_token (parser->lexer);
36677 tree block = begin_omp_parallel ();
36678 tree clauses;
36679 cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, &clauses,
36680 if_p);
36681 return finish_omp_construct (code, block, clauses);
36685 tree clauses = cp_parser_oacc_all_clauses (parser, mask, p_name, pragma_tok);
36687 tree block = begin_omp_parallel ();
36688 unsigned int save = cp_parser_begin_omp_structured_block (parser);
36689 cp_parser_statement (parser, NULL_TREE, false, if_p);
36690 cp_parser_end_omp_structured_block (parser, save);
36691 return finish_omp_construct (code, block, clauses);
36694 /* OpenACC 2.0:
36695 # pragma acc update oacc-update-clause[optseq] new-line
36698 #define OACC_UPDATE_CLAUSE_MASK \
36699 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC) \
36700 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_DEVICE) \
36701 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_HOST) \
36702 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_IF) \
36703 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SELF) \
36704 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WAIT))
36706 static tree
36707 cp_parser_oacc_update (cp_parser *parser, cp_token *pragma_tok)
36709 tree stmt, clauses;
36711 clauses = cp_parser_oacc_all_clauses (parser, OACC_UPDATE_CLAUSE_MASK,
36712 "#pragma acc update", pragma_tok);
36714 if (omp_find_clause (clauses, OMP_CLAUSE_MAP) == NULL_TREE)
36716 error_at (pragma_tok->location,
36717 "%<#pragma acc update%> must contain at least one "
36718 "%<device%> or %<host%> or %<self%> clause");
36719 return NULL_TREE;
36722 stmt = make_node (OACC_UPDATE);
36723 TREE_TYPE (stmt) = void_type_node;
36724 OACC_UPDATE_CLAUSES (stmt) = clauses;
36725 SET_EXPR_LOCATION (stmt, pragma_tok->location);
36726 add_stmt (stmt);
36727 return stmt;
36730 /* OpenACC 2.0:
36731 # pragma acc wait [(intseq)] oacc-wait-clause[optseq] new-line
36733 LOC is the location of the #pragma token.
36736 #define OACC_WAIT_CLAUSE_MASK \
36737 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_ASYNC))
36739 static tree
36740 cp_parser_oacc_wait (cp_parser *parser, cp_token *pragma_tok)
36742 tree clauses, list = NULL_TREE, stmt = NULL_TREE;
36743 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
36745 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
36746 list = cp_parser_oacc_wait_list (parser, loc, list);
36748 clauses = cp_parser_oacc_all_clauses (parser, OACC_WAIT_CLAUSE_MASK,
36749 "#pragma acc wait", pragma_tok);
36751 stmt = c_finish_oacc_wait (loc, list, clauses);
36752 stmt = finish_expr_stmt (stmt);
36754 return stmt;
36757 /* OpenMP 4.0:
36758 # pragma omp declare simd declare-simd-clauses[optseq] new-line */
36760 #define OMP_DECLARE_SIMD_CLAUSE_MASK \
36761 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SIMDLEN) \
36762 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINEAR) \
36763 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_ALIGNED) \
36764 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNIFORM) \
36765 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_INBRANCH) \
36766 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOTINBRANCH))
36768 static void
36769 cp_parser_omp_declare_simd (cp_parser *parser, cp_token *pragma_tok,
36770 enum pragma_context context)
36772 bool first_p = parser->omp_declare_simd == NULL;
36773 cp_omp_declare_simd_data data;
36774 if (first_p)
36776 data.error_seen = false;
36777 data.fndecl_seen = false;
36778 data.tokens = vNULL;
36779 data.clauses = NULL_TREE;
36780 /* It is safe to take the address of a local variable; it will only be
36781 used while this scope is live. */
36782 parser->omp_declare_simd = &data;
36785 /* Store away all pragma tokens. */
36786 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
36787 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
36788 cp_lexer_consume_token (parser->lexer);
36789 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
36790 parser->omp_declare_simd->error_seen = true;
36791 cp_parser_require_pragma_eol (parser, pragma_tok);
36792 struct cp_token_cache *cp
36793 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
36794 parser->omp_declare_simd->tokens.safe_push (cp);
36796 if (first_p)
36798 while (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
36799 cp_parser_pragma (parser, context, NULL);
36800 switch (context)
36802 case pragma_external:
36803 cp_parser_declaration (parser);
36804 break;
36805 case pragma_member:
36806 cp_parser_member_declaration (parser);
36807 break;
36808 case pragma_objc_icode:
36809 cp_parser_block_declaration (parser, /*statement_p=*/false);
36810 break;
36811 default:
36812 cp_parser_declaration_statement (parser);
36813 break;
36815 if (parser->omp_declare_simd
36816 && !parser->omp_declare_simd->error_seen
36817 && !parser->omp_declare_simd->fndecl_seen)
36818 error_at (pragma_tok->location,
36819 "%<#pragma omp declare simd%> not immediately followed by "
36820 "function declaration or definition");
36821 data.tokens.release ();
36822 parser->omp_declare_simd = NULL;
36826 /* Handles the delayed parsing of the Cilk Plus SIMD-enabled function.
36827 This function is modelled similar to the late parsing of omp declare
36828 simd. */
36830 static tree
36831 cp_parser_late_parsing_cilk_simd_fn_info (cp_parser *parser, tree attrs)
36833 struct cp_token_cache *ce;
36834 cp_omp_declare_simd_data *info = parser->cilk_simd_fn_info;
36835 int ii = 0;
36837 if (parser->omp_declare_simd != NULL
36838 || lookup_attribute ("simd", attrs))
36840 error ("%<#pragma omp declare simd%> or %<simd%> attribute cannot be "
36841 "used in the same function marked as a Cilk Plus SIMD-enabled "
36842 "function");
36843 parser->cilk_simd_fn_info->tokens.release ();
36844 XDELETE (parser->cilk_simd_fn_info);
36845 parser->cilk_simd_fn_info = NULL;
36846 return attrs;
36848 if (!info->error_seen && info->fndecl_seen)
36850 error ("vector attribute not immediately followed by a single function"
36851 " declaration or definition");
36852 info->error_seen = true;
36854 if (info->error_seen)
36855 return attrs;
36857 FOR_EACH_VEC_ELT (info->tokens, ii, ce)
36859 tree c, cl;
36861 cp_parser_push_lexer_for_tokens (parser, ce);
36862 parser->lexer->in_pragma = true;
36863 cl = cp_parser_omp_all_clauses (parser, CILK_SIMD_FN_CLAUSE_MASK,
36864 "SIMD-enabled functions attribute",
36865 NULL);
36866 cp_parser_pop_lexer (parser);
36867 if (cl)
36868 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36870 c = build_tree_list (get_identifier ("cilk simd function"), NULL_TREE);
36871 TREE_CHAIN (c) = attrs;
36872 attrs = c;
36874 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36875 TREE_CHAIN (c) = attrs;
36876 if (processing_template_decl)
36877 ATTR_IS_DEPENDENT (c) = 1;
36878 attrs = c;
36880 info->fndecl_seen = true;
36881 parser->cilk_simd_fn_info->tokens.release ();
36882 XDELETE (parser->cilk_simd_fn_info);
36883 parser->cilk_simd_fn_info = NULL;
36884 return attrs;
36887 /* Finalize #pragma omp declare simd clauses after direct declarator has
36888 been parsed, and put that into "omp declare simd" attribute. */
36890 static tree
36891 cp_parser_late_parsing_omp_declare_simd (cp_parser *parser, tree attrs)
36893 struct cp_token_cache *ce;
36894 cp_omp_declare_simd_data *data = parser->omp_declare_simd;
36895 int i;
36897 if (!data->error_seen && data->fndecl_seen)
36899 error ("%<#pragma omp declare simd%> not immediately followed by "
36900 "a single function declaration or definition");
36901 data->error_seen = true;
36903 if (data->error_seen)
36904 return attrs;
36906 FOR_EACH_VEC_ELT (data->tokens, i, ce)
36908 tree c, cl;
36910 cp_parser_push_lexer_for_tokens (parser, ce);
36911 parser->lexer->in_pragma = true;
36912 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
36913 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
36914 cp_lexer_consume_token (parser->lexer);
36915 cl = cp_parser_omp_all_clauses (parser, OMP_DECLARE_SIMD_CLAUSE_MASK,
36916 "#pragma omp declare simd", pragma_tok);
36917 cp_parser_pop_lexer (parser);
36918 if (cl)
36919 cl = tree_cons (NULL_TREE, cl, NULL_TREE);
36920 c = build_tree_list (get_identifier ("omp declare simd"), cl);
36921 TREE_CHAIN (c) = attrs;
36922 if (processing_template_decl)
36923 ATTR_IS_DEPENDENT (c) = 1;
36924 attrs = c;
36927 data->fndecl_seen = true;
36928 return attrs;
36932 /* OpenMP 4.0:
36933 # pragma omp declare target new-line
36934 declarations and definitions
36935 # pragma omp end declare target new-line
36937 OpenMP 4.5:
36938 # pragma omp declare target ( extended-list ) new-line
36940 # pragma omp declare target declare-target-clauses[seq] new-line */
36942 #define OMP_DECLARE_TARGET_CLAUSE_MASK \
36943 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_TO) \
36944 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LINK))
36946 static void
36947 cp_parser_omp_declare_target (cp_parser *parser, cp_token *pragma_tok)
36949 tree clauses = NULL_TREE;
36950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
36951 clauses
36952 = cp_parser_omp_all_clauses (parser, OMP_DECLARE_TARGET_CLAUSE_MASK,
36953 "#pragma omp declare target", pragma_tok);
36954 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
36956 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_TO_DECLARE,
36957 clauses);
36958 clauses = finish_omp_clauses (clauses, C_ORT_OMP);
36959 cp_parser_require_pragma_eol (parser, pragma_tok);
36961 else
36963 cp_parser_require_pragma_eol (parser, pragma_tok);
36964 scope_chain->omp_declare_target_attribute++;
36965 return;
36967 if (scope_chain->omp_declare_target_attribute)
36968 error_at (pragma_tok->location,
36969 "%<#pragma omp declare target%> with clauses in between "
36970 "%<#pragma omp declare target%> without clauses and "
36971 "%<#pragma omp end declare target%>");
36972 for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c))
36974 tree t = OMP_CLAUSE_DECL (c), id;
36975 tree at1 = lookup_attribute ("omp declare target", DECL_ATTRIBUTES (t));
36976 tree at2 = lookup_attribute ("omp declare target link",
36977 DECL_ATTRIBUTES (t));
36978 if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LINK)
36980 id = get_identifier ("omp declare target link");
36981 std::swap (at1, at2);
36983 else
36984 id = get_identifier ("omp declare target");
36985 if (at2)
36987 error_at (OMP_CLAUSE_LOCATION (c),
36988 "%qD specified both in declare target %<link%> and %<to%>"
36989 " clauses", t);
36990 continue;
36992 if (!at1)
36994 DECL_ATTRIBUTES (t) = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (t));
36995 if (TREE_CODE (t) != FUNCTION_DECL && !is_global_var (t))
36996 continue;
36998 symtab_node *node = symtab_node::get (t);
36999 if (node != NULL)
37001 node->offloadable = 1;
37002 if (ENABLE_OFFLOADING)
37004 g->have_offload = true;
37005 if (is_a <varpool_node *> (node))
37006 vec_safe_push (offload_vars, t);
37013 static void
37014 cp_parser_omp_end_declare_target (cp_parser *parser, cp_token *pragma_tok)
37016 const char *p = "";
37017 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37019 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37020 p = IDENTIFIER_POINTER (id);
37022 if (strcmp (p, "declare") == 0)
37024 cp_lexer_consume_token (parser->lexer);
37025 p = "";
37026 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37028 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37029 p = IDENTIFIER_POINTER (id);
37031 if (strcmp (p, "target") == 0)
37032 cp_lexer_consume_token (parser->lexer);
37033 else
37035 cp_parser_error (parser, "expected %<target%>");
37036 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37037 return;
37040 else
37042 cp_parser_error (parser, "expected %<declare%>");
37043 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37044 return;
37046 cp_parser_require_pragma_eol (parser, pragma_tok);
37047 if (!scope_chain->omp_declare_target_attribute)
37048 error_at (pragma_tok->location,
37049 "%<#pragma omp end declare target%> without corresponding "
37050 "%<#pragma omp declare target%>");
37051 else
37052 scope_chain->omp_declare_target_attribute--;
37055 /* Helper function of cp_parser_omp_declare_reduction. Parse the combiner
37056 expression and optional initializer clause of
37057 #pragma omp declare reduction. We store the expression(s) as
37058 either 3, 6 or 7 special statements inside of the artificial function's
37059 body. The first two statements are DECL_EXPRs for the artificial
37060 OMP_OUT resp. OMP_IN variables, followed by a statement with the combiner
37061 expression that uses those variables.
37062 If there was any INITIALIZER clause, this is followed by further statements,
37063 the fourth and fifth statements are DECL_EXPRs for the artificial
37064 OMP_PRIV resp. OMP_ORIG variables. If the INITIALIZER clause wasn't the
37065 constructor variant (first token after open paren is not omp_priv),
37066 then the sixth statement is a statement with the function call expression
37067 that uses the OMP_PRIV and optionally OMP_ORIG variable.
37068 Otherwise, the sixth statement is whatever statement cp_finish_decl emits
37069 to initialize the OMP_PRIV artificial variable and there is seventh
37070 statement, a DECL_EXPR of the OMP_PRIV statement again. */
37072 static bool
37073 cp_parser_omp_declare_reduction_exprs (tree fndecl, cp_parser *parser)
37075 tree type = TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fndecl)));
37076 gcc_assert (TREE_CODE (type) == REFERENCE_TYPE);
37077 type = TREE_TYPE (type);
37078 tree omp_out = build_lang_decl (VAR_DECL, get_identifier ("omp_out"), type);
37079 DECL_ARTIFICIAL (omp_out) = 1;
37080 pushdecl (omp_out);
37081 add_decl_expr (omp_out);
37082 tree omp_in = build_lang_decl (VAR_DECL, get_identifier ("omp_in"), type);
37083 DECL_ARTIFICIAL (omp_in) = 1;
37084 pushdecl (omp_in);
37085 add_decl_expr (omp_in);
37086 tree combiner;
37087 tree omp_priv = NULL_TREE, omp_orig = NULL_TREE, initializer = NULL_TREE;
37089 keep_next_level (true);
37090 tree block = begin_omp_structured_block ();
37091 combiner = cp_parser_expression (parser);
37092 finish_expr_stmt (combiner);
37093 block = finish_omp_structured_block (block);
37094 add_stmt (block);
37096 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37097 return false;
37099 const char *p = "";
37100 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37102 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37103 p = IDENTIFIER_POINTER (id);
37106 if (strcmp (p, "initializer") == 0)
37108 cp_lexer_consume_token (parser->lexer);
37109 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37110 return false;
37112 p = "";
37113 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37115 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37116 p = IDENTIFIER_POINTER (id);
37119 omp_priv = build_lang_decl (VAR_DECL, get_identifier ("omp_priv"), type);
37120 DECL_ARTIFICIAL (omp_priv) = 1;
37121 pushdecl (omp_priv);
37122 add_decl_expr (omp_priv);
37123 omp_orig = build_lang_decl (VAR_DECL, get_identifier ("omp_orig"), type);
37124 DECL_ARTIFICIAL (omp_orig) = 1;
37125 pushdecl (omp_orig);
37126 add_decl_expr (omp_orig);
37128 keep_next_level (true);
37129 block = begin_omp_structured_block ();
37131 bool ctor = false;
37132 if (strcmp (p, "omp_priv") == 0)
37134 bool is_direct_init, is_non_constant_init;
37135 ctor = true;
37136 cp_lexer_consume_token (parser->lexer);
37137 /* Reject initializer (omp_priv) and initializer (omp_priv ()). */
37138 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
37139 || (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37140 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
37141 == CPP_CLOSE_PAREN
37142 && cp_lexer_peek_nth_token (parser->lexer, 3)->type
37143 == CPP_CLOSE_PAREN))
37145 finish_omp_structured_block (block);
37146 error ("invalid initializer clause");
37147 return false;
37149 initializer = cp_parser_initializer (parser, &is_direct_init,
37150 &is_non_constant_init);
37151 cp_finish_decl (omp_priv, initializer, !is_non_constant_init,
37152 NULL_TREE, LOOKUP_ONLYCONVERTING);
37154 else
37156 cp_parser_parse_tentatively (parser);
37157 tree fn_name = cp_parser_id_expression (parser, /*template_p=*/false,
37158 /*check_dependency_p=*/true,
37159 /*template_p=*/NULL,
37160 /*declarator_p=*/false,
37161 /*optional_p=*/false);
37162 vec<tree, va_gc> *args;
37163 if (fn_name == error_mark_node
37164 || cp_parser_error_occurred (parser)
37165 || !cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
37166 || ((args = cp_parser_parenthesized_expression_list
37167 (parser, non_attr, /*cast_p=*/false,
37168 /*allow_expansion_p=*/true,
37169 /*non_constant_p=*/NULL)),
37170 cp_parser_error_occurred (parser)))
37172 finish_omp_structured_block (block);
37173 cp_parser_abort_tentative_parse (parser);
37174 cp_parser_error (parser, "expected id-expression (arguments)");
37175 return false;
37177 unsigned int i;
37178 tree arg;
37179 FOR_EACH_VEC_SAFE_ELT (args, i, arg)
37180 if (arg == omp_priv
37181 || (TREE_CODE (arg) == ADDR_EXPR
37182 && TREE_OPERAND (arg, 0) == omp_priv))
37183 break;
37184 cp_parser_abort_tentative_parse (parser);
37185 if (arg == NULL_TREE)
37186 error ("one of the initializer call arguments should be %<omp_priv%>"
37187 " or %<&omp_priv%>");
37188 initializer = cp_parser_postfix_expression (parser, false, false, false,
37189 false, NULL);
37190 finish_expr_stmt (initializer);
37193 block = finish_omp_structured_block (block);
37194 cp_walk_tree (&block, cp_remove_omp_priv_cleanup_stmt, omp_priv, NULL);
37195 add_stmt (block);
37197 if (ctor)
37198 add_decl_expr (omp_orig);
37200 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37201 return false;
37204 if (!cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL))
37205 cp_parser_required_error (parser, RT_PRAGMA_EOL, /*keyword=*/false);
37207 return true;
37210 /* OpenMP 4.0
37211 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37212 initializer-clause[opt] new-line
37214 initializer-clause:
37215 initializer (omp_priv initializer)
37216 initializer (function-name (argument-list)) */
37218 static void
37219 cp_parser_omp_declare_reduction (cp_parser *parser, cp_token *pragma_tok,
37220 enum pragma_context)
37222 auto_vec<tree> types;
37223 enum tree_code reduc_code = ERROR_MARK;
37224 tree reduc_id = NULL_TREE, orig_reduc_id = NULL_TREE, type;
37225 unsigned int i;
37226 cp_token *first_token;
37227 cp_token_cache *cp;
37228 int errs;
37229 void *p;
37231 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
37232 p = obstack_alloc (&declarator_obstack, 0);
37234 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
37235 goto fail;
37237 switch (cp_lexer_peek_token (parser->lexer)->type)
37239 case CPP_PLUS:
37240 reduc_code = PLUS_EXPR;
37241 break;
37242 case CPP_MULT:
37243 reduc_code = MULT_EXPR;
37244 break;
37245 case CPP_MINUS:
37246 reduc_code = MINUS_EXPR;
37247 break;
37248 case CPP_AND:
37249 reduc_code = BIT_AND_EXPR;
37250 break;
37251 case CPP_XOR:
37252 reduc_code = BIT_XOR_EXPR;
37253 break;
37254 case CPP_OR:
37255 reduc_code = BIT_IOR_EXPR;
37256 break;
37257 case CPP_AND_AND:
37258 reduc_code = TRUTH_ANDIF_EXPR;
37259 break;
37260 case CPP_OR_OR:
37261 reduc_code = TRUTH_ORIF_EXPR;
37262 break;
37263 case CPP_NAME:
37264 reduc_id = orig_reduc_id = cp_parser_identifier (parser);
37265 break;
37266 default:
37267 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
37268 "%<|%>, %<&&%>, %<||%> or identifier");
37269 goto fail;
37272 if (reduc_code != ERROR_MARK)
37273 cp_lexer_consume_token (parser->lexer);
37275 reduc_id = omp_reduction_id (reduc_code, reduc_id, NULL_TREE);
37276 if (reduc_id == error_mark_node)
37277 goto fail;
37279 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
37280 goto fail;
37282 /* Types may not be defined in declare reduction type list. */
37283 const char *saved_message;
37284 saved_message = parser->type_definition_forbidden_message;
37285 parser->type_definition_forbidden_message
37286 = G_("types may not be defined in declare reduction type list");
37287 bool saved_colon_corrects_to_scope_p;
37288 saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
37289 parser->colon_corrects_to_scope_p = false;
37290 bool saved_colon_doesnt_start_class_def_p;
37291 saved_colon_doesnt_start_class_def_p
37292 = parser->colon_doesnt_start_class_def_p;
37293 parser->colon_doesnt_start_class_def_p = true;
37295 while (true)
37297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37298 type = cp_parser_type_id (parser);
37299 if (type == error_mark_node)
37301 else if (ARITHMETIC_TYPE_P (type)
37302 && (orig_reduc_id == NULL_TREE
37303 || (TREE_CODE (type) != COMPLEX_TYPE
37304 && (id_equal (orig_reduc_id, "min")
37305 || id_equal (orig_reduc_id, "max")))))
37306 error_at (loc, "predeclared arithmetic type %qT in "
37307 "%<#pragma omp declare reduction%>", type);
37308 else if (TREE_CODE (type) == FUNCTION_TYPE
37309 || TREE_CODE (type) == METHOD_TYPE
37310 || TREE_CODE (type) == ARRAY_TYPE)
37311 error_at (loc, "function or array type %qT in "
37312 "%<#pragma omp declare reduction%>", type);
37313 else if (TREE_CODE (type) == REFERENCE_TYPE)
37314 error_at (loc, "reference type %qT in "
37315 "%<#pragma omp declare reduction%>", type);
37316 else if (TYPE_QUALS_NO_ADDR_SPACE (type))
37317 error_at (loc, "const, volatile or __restrict qualified type %qT in "
37318 "%<#pragma omp declare reduction%>", type);
37319 else
37320 types.safe_push (type);
37322 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
37323 cp_lexer_consume_token (parser->lexer);
37324 else
37325 break;
37328 /* Restore the saved message. */
37329 parser->type_definition_forbidden_message = saved_message;
37330 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
37331 parser->colon_doesnt_start_class_def_p
37332 = saved_colon_doesnt_start_class_def_p;
37334 if (!cp_parser_require (parser, CPP_COLON, RT_COLON)
37335 || types.is_empty ())
37337 fail:
37338 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37339 goto done;
37342 first_token = cp_lexer_peek_token (parser->lexer);
37343 cp = NULL;
37344 errs = errorcount;
37345 FOR_EACH_VEC_ELT (types, i, type)
37347 tree fntype
37348 = build_function_type_list (void_type_node,
37349 cp_build_reference_type (type, false),
37350 NULL_TREE);
37351 tree this_reduc_id = reduc_id;
37352 if (!dependent_type_p (type))
37353 this_reduc_id = omp_reduction_id (ERROR_MARK, reduc_id, type);
37354 tree fndecl = build_lang_decl (FUNCTION_DECL, this_reduc_id, fntype);
37355 DECL_SOURCE_LOCATION (fndecl) = pragma_tok->location;
37356 DECL_ARTIFICIAL (fndecl) = 1;
37357 DECL_EXTERNAL (fndecl) = 1;
37358 DECL_DECLARED_INLINE_P (fndecl) = 1;
37359 DECL_IGNORED_P (fndecl) = 1;
37360 DECL_OMP_DECLARE_REDUCTION_P (fndecl) = 1;
37361 SET_DECL_ASSEMBLER_NAME (fndecl, get_identifier ("<udr>"));
37362 DECL_ATTRIBUTES (fndecl)
37363 = tree_cons (get_identifier ("gnu_inline"), NULL_TREE,
37364 DECL_ATTRIBUTES (fndecl));
37365 if (processing_template_decl)
37366 fndecl = push_template_decl (fndecl);
37367 bool block_scope = false;
37368 tree block = NULL_TREE;
37369 if (current_function_decl)
37371 block_scope = true;
37372 DECL_CONTEXT (fndecl) = global_namespace;
37373 if (!processing_template_decl)
37374 pushdecl (fndecl);
37376 else if (current_class_type)
37378 if (cp == NULL)
37380 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37381 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37382 cp_lexer_consume_token (parser->lexer);
37383 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37384 goto fail;
37385 cp = cp_token_cache_new (first_token,
37386 cp_lexer_peek_nth_token (parser->lexer,
37387 2));
37389 DECL_STATIC_FUNCTION_P (fndecl) = 1;
37390 finish_member_declaration (fndecl);
37391 DECL_PENDING_INLINE_INFO (fndecl) = cp;
37392 DECL_PENDING_INLINE_P (fndecl) = 1;
37393 vec_safe_push (unparsed_funs_with_definitions, fndecl);
37394 continue;
37396 else
37398 DECL_CONTEXT (fndecl) = current_namespace;
37399 pushdecl (fndecl);
37401 if (!block_scope)
37402 start_preparsed_function (fndecl, NULL_TREE, SF_PRE_PARSED);
37403 else
37404 block = begin_omp_structured_block ();
37405 if (cp)
37407 cp_parser_push_lexer_for_tokens (parser, cp);
37408 parser->lexer->in_pragma = true;
37410 if (!cp_parser_omp_declare_reduction_exprs (fndecl, parser))
37412 if (!block_scope)
37413 finish_function (0);
37414 else
37415 DECL_CONTEXT (fndecl) = current_function_decl;
37416 if (cp)
37417 cp_parser_pop_lexer (parser);
37418 goto fail;
37420 if (cp)
37421 cp_parser_pop_lexer (parser);
37422 if (!block_scope)
37423 finish_function (0);
37424 else
37426 DECL_CONTEXT (fndecl) = current_function_decl;
37427 block = finish_omp_structured_block (block);
37428 if (TREE_CODE (block) == BIND_EXPR)
37429 DECL_SAVED_TREE (fndecl) = BIND_EXPR_BODY (block);
37430 else if (TREE_CODE (block) == STATEMENT_LIST)
37431 DECL_SAVED_TREE (fndecl) = block;
37432 if (processing_template_decl)
37433 add_decl_expr (fndecl);
37435 cp_check_omp_declare_reduction (fndecl);
37436 if (cp == NULL && types.length () > 1)
37437 cp = cp_token_cache_new (first_token,
37438 cp_lexer_peek_nth_token (parser->lexer, 2));
37439 if (errs != errorcount)
37440 break;
37443 cp_parser_require_pragma_eol (parser, pragma_tok);
37445 done:
37446 /* Free any declarators allocated. */
37447 obstack_free (&declarator_obstack, p);
37450 /* OpenMP 4.0
37451 #pragma omp declare simd declare-simd-clauses[optseq] new-line
37452 #pragma omp declare reduction (reduction-id : typename-list : expression) \
37453 initializer-clause[opt] new-line
37454 #pragma omp declare target new-line */
37456 static void
37457 cp_parser_omp_declare (cp_parser *parser, cp_token *pragma_tok,
37458 enum pragma_context context)
37460 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37462 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37463 const char *p = IDENTIFIER_POINTER (id);
37465 if (strcmp (p, "simd") == 0)
37467 cp_lexer_consume_token (parser->lexer);
37468 cp_parser_omp_declare_simd (parser, pragma_tok,
37469 context);
37470 return;
37472 cp_ensure_no_omp_declare_simd (parser);
37473 if (strcmp (p, "reduction") == 0)
37475 cp_lexer_consume_token (parser->lexer);
37476 cp_parser_omp_declare_reduction (parser, pragma_tok,
37477 context);
37478 return;
37480 if (!flag_openmp) /* flag_openmp_simd */
37482 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37483 return;
37485 if (strcmp (p, "target") == 0)
37487 cp_lexer_consume_token (parser->lexer);
37488 cp_parser_omp_declare_target (parser, pragma_tok);
37489 return;
37492 cp_parser_error (parser, "expected %<simd%> or %<reduction%> "
37493 "or %<target%>");
37494 cp_parser_require_pragma_eol (parser, pragma_tok);
37497 /* OpenMP 4.5:
37498 #pragma omp taskloop taskloop-clause[optseq] new-line
37499 for-loop
37501 #pragma omp taskloop simd taskloop-simd-clause[optseq] new-line
37502 for-loop */
37504 #define OMP_TASKLOOP_CLAUSE_MASK \
37505 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_SHARED) \
37506 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIVATE) \
37507 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
37508 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
37509 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_DEFAULT) \
37510 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_GRAINSIZE) \
37511 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NUM_TASKS) \
37512 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_COLLAPSE) \
37513 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_UNTIED) \
37514 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_IF) \
37515 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_FINAL) \
37516 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_MERGEABLE) \
37517 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_NOGROUP) \
37518 | (OMP_CLAUSE_MASK_1 << PRAGMA_OMP_CLAUSE_PRIORITY))
37520 static tree
37521 cp_parser_omp_taskloop (cp_parser *parser, cp_token *pragma_tok,
37522 char *p_name, omp_clause_mask mask, tree *cclauses,
37523 bool *if_p)
37525 tree clauses, sb, ret;
37526 unsigned int save;
37527 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
37529 strcat (p_name, " taskloop");
37530 mask |= OMP_TASKLOOP_CLAUSE_MASK;
37532 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
37534 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
37535 const char *p = IDENTIFIER_POINTER (id);
37537 if (strcmp (p, "simd") == 0)
37539 tree cclauses_buf[C_OMP_CLAUSE_SPLIT_COUNT];
37540 if (cclauses == NULL)
37541 cclauses = cclauses_buf;
37543 cp_lexer_consume_token (parser->lexer);
37544 if (!flag_openmp) /* flag_openmp_simd */
37545 return cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37546 cclauses, if_p);
37547 sb = begin_omp_structured_block ();
37548 save = cp_parser_begin_omp_structured_block (parser);
37549 ret = cp_parser_omp_simd (parser, pragma_tok, p_name, mask,
37550 cclauses, if_p);
37551 cp_parser_end_omp_structured_block (parser, save);
37552 tree body = finish_omp_structured_block (sb);
37553 if (ret == NULL)
37554 return ret;
37555 ret = make_node (OMP_TASKLOOP);
37556 TREE_TYPE (ret) = void_type_node;
37557 OMP_FOR_BODY (ret) = body;
37558 OMP_FOR_CLAUSES (ret) = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37559 SET_EXPR_LOCATION (ret, loc);
37560 add_stmt (ret);
37561 return ret;
37564 if (!flag_openmp) /* flag_openmp_simd */
37566 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37567 return NULL_TREE;
37570 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok,
37571 cclauses == NULL);
37572 if (cclauses)
37574 cp_omp_split_clauses (loc, OMP_TASKLOOP, mask, clauses, cclauses);
37575 clauses = cclauses[C_OMP_CLAUSE_SPLIT_TASKLOOP];
37578 sb = begin_omp_structured_block ();
37579 save = cp_parser_begin_omp_structured_block (parser);
37581 ret = cp_parser_omp_for_loop (parser, OMP_TASKLOOP, clauses, cclauses,
37582 if_p);
37584 cp_parser_end_omp_structured_block (parser, save);
37585 add_stmt (finish_omp_structured_block (sb));
37587 return ret;
37591 /* OpenACC 2.0:
37592 # pragma acc routine oacc-routine-clause[optseq] new-line
37593 function-definition
37595 # pragma acc routine ( name ) oacc-routine-clause[optseq] new-line
37598 #define OACC_ROUTINE_CLAUSE_MASK \
37599 ( (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_GANG) \
37600 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_WORKER) \
37601 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_VECTOR) \
37602 | (OMP_CLAUSE_MASK_1 << PRAGMA_OACC_CLAUSE_SEQ))
37605 /* Parse the OpenACC routine pragma. This has an optional '( name )'
37606 component, which must resolve to a declared namespace-scope
37607 function. The clauses are either processed directly (for a named
37608 function), or defered until the immediatley following declaration
37609 is parsed. */
37611 static void
37612 cp_parser_oacc_routine (cp_parser *parser, cp_token *pragma_tok,
37613 enum pragma_context context)
37615 gcc_checking_assert (context == pragma_external);
37616 /* The checking for "another pragma following this one" in the "no optional
37617 '( name )'" case makes sure that we dont re-enter. */
37618 gcc_checking_assert (parser->oacc_routine == NULL);
37620 cp_oacc_routine_data data;
37621 data.error_seen = false;
37622 data.fndecl_seen = false;
37623 data.tokens = vNULL;
37624 data.clauses = NULL_TREE;
37625 data.loc = pragma_tok->location;
37626 /* It is safe to take the address of a local variable; it will only be
37627 used while this scope is live. */
37628 parser->oacc_routine = &data;
37630 /* Look for optional '( name )'. */
37631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
37633 cp_lexer_consume_token (parser->lexer); /* '(' */
37635 /* We parse the name as an id-expression. If it resolves to
37636 anything other than a non-overloaded function at namespace
37637 scope, it's an error. */
37638 location_t name_loc = cp_lexer_peek_token (parser->lexer)->location;
37639 tree name = cp_parser_id_expression (parser,
37640 /*template_keyword_p=*/false,
37641 /*check_dependency_p=*/false,
37642 /*template_p=*/NULL,
37643 /*declarator_p=*/false,
37644 /*optional_p=*/false);
37645 tree decl = cp_parser_lookup_name_simple (parser, name, name_loc);
37646 if (name != error_mark_node && decl == error_mark_node)
37647 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL, name_loc);
37649 if (decl == error_mark_node
37650 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
37652 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
37653 parser->oacc_routine = NULL;
37654 return;
37657 data.clauses
37658 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37659 "#pragma acc routine",
37660 cp_lexer_peek_token (parser->lexer));
37662 if (decl && is_overloaded_fn (decl)
37663 && (TREE_CODE (decl) != FUNCTION_DECL
37664 || DECL_FUNCTION_TEMPLATE_P (decl)))
37666 error_at (name_loc,
37667 "%<#pragma acc routine%> names a set of overloads");
37668 parser->oacc_routine = NULL;
37669 return;
37672 /* Perhaps we should use the same rule as declarations in different
37673 namespaces? */
37674 if (!DECL_NAMESPACE_SCOPE_P (decl))
37676 error_at (name_loc,
37677 "%qD does not refer to a namespace scope function", decl);
37678 parser->oacc_routine = NULL;
37679 return;
37682 if (TREE_CODE (decl) != FUNCTION_DECL)
37684 error_at (name_loc, "%qD does not refer to a function", decl);
37685 parser->oacc_routine = NULL;
37686 return;
37689 cp_finalize_oacc_routine (parser, decl, false);
37690 parser->oacc_routine = NULL;
37692 else /* No optional '( name )'. */
37694 /* Store away all pragma tokens. */
37695 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
37696 && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
37697 cp_lexer_consume_token (parser->lexer);
37698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
37699 parser->oacc_routine->error_seen = true;
37700 cp_parser_require_pragma_eol (parser, pragma_tok);
37701 struct cp_token_cache *cp
37702 = cp_token_cache_new (pragma_tok, cp_lexer_peek_token (parser->lexer));
37703 parser->oacc_routine->tokens.safe_push (cp);
37705 /* Emit a helpful diagnostic if there's another pragma following this
37706 one. */
37707 if (cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA))
37709 cp_ensure_no_oacc_routine (parser);
37710 data.tokens.release ();
37711 /* ..., and then just keep going. */
37712 return;
37715 /* We only have to consider the pragma_external case here. */
37716 cp_parser_declaration (parser);
37717 if (parser->oacc_routine
37718 && !parser->oacc_routine->fndecl_seen)
37719 cp_ensure_no_oacc_routine (parser);
37720 else
37721 parser->oacc_routine = NULL;
37722 data.tokens.release ();
37726 /* Finalize #pragma acc routine clauses after direct declarator has
37727 been parsed. */
37729 static tree
37730 cp_parser_late_parsing_oacc_routine (cp_parser *parser, tree attrs)
37732 struct cp_token_cache *ce;
37733 cp_oacc_routine_data *data = parser->oacc_routine;
37735 if (!data->error_seen && data->fndecl_seen)
37737 error_at (data->loc,
37738 "%<#pragma acc routine%> not immediately followed by "
37739 "a single function declaration or definition");
37740 data->error_seen = true;
37742 if (data->error_seen)
37743 return attrs;
37745 gcc_checking_assert (data->tokens.length () == 1);
37746 ce = data->tokens[0];
37748 cp_parser_push_lexer_for_tokens (parser, ce);
37749 parser->lexer->in_pragma = true;
37750 gcc_assert (cp_lexer_peek_token (parser->lexer)->type == CPP_PRAGMA);
37752 cp_token *pragma_tok = cp_lexer_consume_token (parser->lexer);
37753 gcc_checking_assert (parser->oacc_routine->clauses == NULL_TREE);
37754 parser->oacc_routine->clauses
37755 = cp_parser_oacc_all_clauses (parser, OACC_ROUTINE_CLAUSE_MASK,
37756 "#pragma acc routine", pragma_tok);
37757 cp_parser_pop_lexer (parser);
37758 /* Later, cp_finalize_oacc_routine will process the clauses, and then set
37759 fndecl_seen. */
37761 return attrs;
37764 /* Apply any saved OpenACC routine clauses to a just-parsed
37765 declaration. */
37767 static void
37768 cp_finalize_oacc_routine (cp_parser *parser, tree fndecl, bool is_defn)
37770 if (__builtin_expect (parser->oacc_routine != NULL, 0))
37772 /* Keep going if we're in error reporting mode. */
37773 if (parser->oacc_routine->error_seen
37774 || fndecl == error_mark_node)
37775 return;
37777 if (parser->oacc_routine->fndecl_seen)
37779 error_at (parser->oacc_routine->loc,
37780 "%<#pragma acc routine%> not immediately followed by"
37781 " a single function declaration or definition");
37782 parser->oacc_routine = NULL;
37783 return;
37785 if (TREE_CODE (fndecl) != FUNCTION_DECL)
37787 cp_ensure_no_oacc_routine (parser);
37788 return;
37791 if (oacc_get_fn_attrib (fndecl))
37793 error_at (parser->oacc_routine->loc,
37794 "%<#pragma acc routine%> already applied to %qD", fndecl);
37795 parser->oacc_routine = NULL;
37796 return;
37799 if (TREE_USED (fndecl) || (!is_defn && DECL_SAVED_TREE (fndecl)))
37801 error_at (parser->oacc_routine->loc,
37802 TREE_USED (fndecl)
37803 ? G_("%<#pragma acc routine%> must be applied before use")
37804 : G_("%<#pragma acc routine%> must be applied before "
37805 "definition"));
37806 parser->oacc_routine = NULL;
37807 return;
37810 /* Process the routine's dimension clauses. */
37811 tree dims = oacc_build_routine_dims (parser->oacc_routine->clauses);
37812 oacc_replace_fn_attrib (fndecl, dims);
37814 /* Add an "omp declare target" attribute. */
37815 DECL_ATTRIBUTES (fndecl)
37816 = tree_cons (get_identifier ("omp declare target"),
37817 NULL_TREE, DECL_ATTRIBUTES (fndecl));
37819 /* Don't unset parser->oacc_routine here: we may still need it to
37820 diagnose wrong usage. But, remember that we've used this "#pragma acc
37821 routine". */
37822 parser->oacc_routine->fndecl_seen = true;
37826 /* Main entry point to OpenMP statement pragmas. */
37828 static void
37829 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
37831 tree stmt;
37832 char p_name[sizeof "#pragma omp teams distribute parallel for simd"];
37833 omp_clause_mask mask (0);
37835 switch (cp_parser_pragma_kind (pragma_tok))
37837 case PRAGMA_OACC_ATOMIC:
37838 cp_parser_omp_atomic (parser, pragma_tok);
37839 return;
37840 case PRAGMA_OACC_CACHE:
37841 stmt = cp_parser_oacc_cache (parser, pragma_tok);
37842 break;
37843 case PRAGMA_OACC_DATA:
37844 stmt = cp_parser_oacc_data (parser, pragma_tok, if_p);
37845 break;
37846 case PRAGMA_OACC_ENTER_DATA:
37847 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, true);
37848 break;
37849 case PRAGMA_OACC_EXIT_DATA:
37850 stmt = cp_parser_oacc_enter_exit_data (parser, pragma_tok, false);
37851 break;
37852 case PRAGMA_OACC_HOST_DATA:
37853 stmt = cp_parser_oacc_host_data (parser, pragma_tok, if_p);
37854 break;
37855 case PRAGMA_OACC_KERNELS:
37856 case PRAGMA_OACC_PARALLEL:
37857 strcpy (p_name, "#pragma acc");
37858 stmt = cp_parser_oacc_kernels_parallel (parser, pragma_tok, p_name,
37859 if_p);
37860 break;
37861 case PRAGMA_OACC_LOOP:
37862 strcpy (p_name, "#pragma acc");
37863 stmt = cp_parser_oacc_loop (parser, pragma_tok, p_name, mask, NULL,
37864 if_p);
37865 break;
37866 case PRAGMA_OACC_UPDATE:
37867 stmt = cp_parser_oacc_update (parser, pragma_tok);
37868 break;
37869 case PRAGMA_OACC_WAIT:
37870 stmt = cp_parser_oacc_wait (parser, pragma_tok);
37871 break;
37872 case PRAGMA_OMP_ATOMIC:
37873 cp_parser_omp_atomic (parser, pragma_tok);
37874 return;
37875 case PRAGMA_OMP_CRITICAL:
37876 stmt = cp_parser_omp_critical (parser, pragma_tok, if_p);
37877 break;
37878 case PRAGMA_OMP_DISTRIBUTE:
37879 strcpy (p_name, "#pragma omp");
37880 stmt = cp_parser_omp_distribute (parser, pragma_tok, p_name, mask, NULL,
37881 if_p);
37882 break;
37883 case PRAGMA_OMP_FOR:
37884 strcpy (p_name, "#pragma omp");
37885 stmt = cp_parser_omp_for (parser, pragma_tok, p_name, mask, NULL,
37886 if_p);
37887 break;
37888 case PRAGMA_OMP_MASTER:
37889 stmt = cp_parser_omp_master (parser, pragma_tok, if_p);
37890 break;
37891 case PRAGMA_OMP_PARALLEL:
37892 strcpy (p_name, "#pragma omp");
37893 stmt = cp_parser_omp_parallel (parser, pragma_tok, p_name, mask, NULL,
37894 if_p);
37895 break;
37896 case PRAGMA_OMP_SECTIONS:
37897 strcpy (p_name, "#pragma omp");
37898 stmt = cp_parser_omp_sections (parser, pragma_tok, p_name, mask, NULL);
37899 break;
37900 case PRAGMA_OMP_SIMD:
37901 strcpy (p_name, "#pragma omp");
37902 stmt = cp_parser_omp_simd (parser, pragma_tok, p_name, mask, NULL,
37903 if_p);
37904 break;
37905 case PRAGMA_OMP_SINGLE:
37906 stmt = cp_parser_omp_single (parser, pragma_tok, if_p);
37907 break;
37908 case PRAGMA_OMP_TASK:
37909 stmt = cp_parser_omp_task (parser, pragma_tok, if_p);
37910 break;
37911 case PRAGMA_OMP_TASKGROUP:
37912 stmt = cp_parser_omp_taskgroup (parser, pragma_tok, if_p);
37913 break;
37914 case PRAGMA_OMP_TASKLOOP:
37915 strcpy (p_name, "#pragma omp");
37916 stmt = cp_parser_omp_taskloop (parser, pragma_tok, p_name, mask, NULL,
37917 if_p);
37918 break;
37919 case PRAGMA_OMP_TEAMS:
37920 strcpy (p_name, "#pragma omp");
37921 stmt = cp_parser_omp_teams (parser, pragma_tok, p_name, mask, NULL,
37922 if_p);
37923 break;
37924 default:
37925 gcc_unreachable ();
37928 protected_set_expr_location (stmt, pragma_tok->location);
37931 /* Transactional Memory parsing routines. */
37933 /* Parse a transaction attribute.
37935 txn-attribute:
37936 attribute
37937 [ [ identifier ] ]
37939 We use this instead of cp_parser_attributes_opt for transactions to avoid
37940 the pedwarn in C++98 mode. */
37942 static tree
37943 cp_parser_txn_attribute_opt (cp_parser *parser)
37945 cp_token *token;
37946 tree attr_name, attr = NULL;
37948 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
37949 return cp_parser_attributes_opt (parser);
37951 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
37952 return NULL_TREE;
37953 cp_lexer_consume_token (parser->lexer);
37954 if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
37955 goto error1;
37957 token = cp_lexer_peek_token (parser->lexer);
37958 if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
37960 token = cp_lexer_consume_token (parser->lexer);
37962 attr_name = (token->type == CPP_KEYWORD
37963 /* For keywords, use the canonical spelling,
37964 not the parsed identifier. */
37965 ? ridpointers[(int) token->keyword]
37966 : token->u.value);
37967 attr = build_tree_list (attr_name, NULL_TREE);
37969 else
37970 cp_parser_error (parser, "expected identifier");
37972 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37973 error1:
37974 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
37975 return attr;
37978 /* Parse a __transaction_atomic or __transaction_relaxed statement.
37980 transaction-statement:
37981 __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
37982 compound-statement
37983 __transaction_relaxed txn-noexcept-spec[opt] compound-statement
37986 static tree
37987 cp_parser_transaction (cp_parser *parser, cp_token *token)
37989 unsigned char old_in = parser->in_transaction;
37990 unsigned char this_in = 1, new_in;
37991 enum rid keyword = token->keyword;
37992 tree stmt, attrs, noex;
37994 cp_lexer_consume_token (parser->lexer);
37996 if (keyword == RID_TRANSACTION_RELAXED
37997 || keyword == RID_SYNCHRONIZED)
37998 this_in |= TM_STMT_ATTR_RELAXED;
37999 else
38001 attrs = cp_parser_txn_attribute_opt (parser);
38002 if (attrs)
38003 this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38006 /* Parse a noexcept specification. */
38007 if (keyword == RID_ATOMIC_NOEXCEPT)
38008 noex = boolean_true_node;
38009 else if (keyword == RID_ATOMIC_CANCEL)
38011 /* cancel-and-throw is unimplemented. */
38012 sorry ("atomic_cancel");
38013 noex = NULL_TREE;
38015 else
38016 noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
38018 /* Keep track if we're in the lexical scope of an outer transaction. */
38019 new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
38021 stmt = begin_transaction_stmt (token->location, NULL, this_in);
38023 parser->in_transaction = new_in;
38024 cp_parser_compound_statement (parser, NULL, BCS_TRANSACTION, false);
38025 parser->in_transaction = old_in;
38027 finish_transaction_stmt (stmt, NULL, this_in, noex);
38029 return stmt;
38032 /* Parse a __transaction_atomic or __transaction_relaxed expression.
38034 transaction-expression:
38035 __transaction_atomic txn-noexcept-spec[opt] ( expression )
38036 __transaction_relaxed txn-noexcept-spec[opt] ( expression )
38039 static tree
38040 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
38042 unsigned char old_in = parser->in_transaction;
38043 unsigned char this_in = 1;
38044 cp_token *token;
38045 tree expr, noex;
38046 bool noex_expr;
38047 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38049 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38050 || keyword == RID_TRANSACTION_RELAXED);
38052 if (!flag_tm)
38053 error_at (loc,
38054 keyword == RID_TRANSACTION_RELAXED
38055 ? G_("%<__transaction_relaxed%> without transactional memory "
38056 "support enabled")
38057 : G_("%<__transaction_atomic%> without transactional memory "
38058 "support enabled"));
38060 token = cp_parser_require_keyword (parser, keyword,
38061 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38062 : RT_TRANSACTION_RELAXED));
38063 gcc_assert (token != NULL);
38065 if (keyword == RID_TRANSACTION_RELAXED)
38066 this_in |= TM_STMT_ATTR_RELAXED;
38068 /* Set this early. This might mean that we allow transaction_cancel in
38069 an expression that we find out later actually has to be a constexpr.
38070 However, we expect that cxx_constant_value will be able to deal with
38071 this; also, if the noexcept has no constexpr, then what we parse next
38072 really is a transaction's body. */
38073 parser->in_transaction = this_in;
38075 /* Parse a noexcept specification. */
38076 noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
38077 true);
38079 if (!noex || !noex_expr
38080 || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
38082 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
38084 expr = cp_parser_expression (parser);
38085 expr = finish_parenthesized_expr (expr);
38087 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
38089 else
38091 /* The only expression that is available got parsed for the noexcept
38092 already. noexcept is true then. */
38093 expr = noex;
38094 noex = boolean_true_node;
38097 expr = build_transaction_expr (token->location, expr, this_in, noex);
38098 parser->in_transaction = old_in;
38100 if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
38101 return error_mark_node;
38103 return (flag_tm ? expr : error_mark_node);
38106 /* Parse a function-transaction-block.
38108 function-transaction-block:
38109 __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
38110 function-body
38111 __transaction_atomic txn-attribute[opt] function-try-block
38112 __transaction_relaxed ctor-initializer[opt] function-body
38113 __transaction_relaxed function-try-block
38116 static bool
38117 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
38119 unsigned char old_in = parser->in_transaction;
38120 unsigned char new_in = 1;
38121 tree compound_stmt, stmt, attrs;
38122 bool ctor_initializer_p;
38123 cp_token *token;
38125 gcc_assert (keyword == RID_TRANSACTION_ATOMIC
38126 || keyword == RID_TRANSACTION_RELAXED);
38127 token = cp_parser_require_keyword (parser, keyword,
38128 (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
38129 : RT_TRANSACTION_RELAXED));
38130 gcc_assert (token != NULL);
38132 if (keyword == RID_TRANSACTION_RELAXED)
38133 new_in |= TM_STMT_ATTR_RELAXED;
38134 else
38136 attrs = cp_parser_txn_attribute_opt (parser);
38137 if (attrs)
38138 new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
38141 stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
38143 parser->in_transaction = new_in;
38145 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
38146 ctor_initializer_p = cp_parser_function_try_block (parser);
38147 else
38148 ctor_initializer_p = cp_parser_ctor_initializer_opt_and_function_body
38149 (parser, /*in_function_try_block=*/false);
38151 parser->in_transaction = old_in;
38153 finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
38155 return ctor_initializer_p;
38158 /* Parse a __transaction_cancel statement.
38160 cancel-statement:
38161 __transaction_cancel txn-attribute[opt] ;
38162 __transaction_cancel txn-attribute[opt] throw-expression ;
38164 ??? Cancel and throw is not yet implemented. */
38166 static tree
38167 cp_parser_transaction_cancel (cp_parser *parser)
38169 cp_token *token;
38170 bool is_outer = false;
38171 tree stmt, attrs;
38173 token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
38174 RT_TRANSACTION_CANCEL);
38175 gcc_assert (token != NULL);
38177 attrs = cp_parser_txn_attribute_opt (parser);
38178 if (attrs)
38179 is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
38181 /* ??? Parse cancel-and-throw here. */
38183 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
38185 if (!flag_tm)
38187 error_at (token->location, "%<__transaction_cancel%> without "
38188 "transactional memory support enabled");
38189 return error_mark_node;
38191 else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
38193 error_at (token->location, "%<__transaction_cancel%> within a "
38194 "%<__transaction_relaxed%>");
38195 return error_mark_node;
38197 else if (is_outer)
38199 if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
38200 && !is_tm_may_cancel_outer (current_function_decl))
38202 error_at (token->location, "outer %<__transaction_cancel%> not "
38203 "within outer %<__transaction_atomic%>");
38204 error_at (token->location,
38205 " or a %<transaction_may_cancel_outer%> function");
38206 return error_mark_node;
38209 else if (parser->in_transaction == 0)
38211 error_at (token->location, "%<__transaction_cancel%> not within "
38212 "%<__transaction_atomic%>");
38213 return error_mark_node;
38216 stmt = build_tm_abort_call (token->location, is_outer);
38217 add_stmt (stmt);
38219 return stmt;
38222 /* The parser. */
38224 static GTY (()) cp_parser *the_parser;
38227 /* Special handling for the first token or line in the file. The first
38228 thing in the file might be #pragma GCC pch_preprocess, which loads a
38229 PCH file, which is a GC collection point. So we need to handle this
38230 first pragma without benefit of an existing lexer structure.
38232 Always returns one token to the caller in *FIRST_TOKEN. This is
38233 either the true first token of the file, or the first token after
38234 the initial pragma. */
38236 static void
38237 cp_parser_initial_pragma (cp_token *first_token)
38239 tree name = NULL;
38241 cp_lexer_get_preprocessor_token (NULL, first_token);
38242 if (cp_parser_pragma_kind (first_token) != PRAGMA_GCC_PCH_PREPROCESS)
38243 return;
38245 cp_lexer_get_preprocessor_token (NULL, first_token);
38246 if (first_token->type == CPP_STRING)
38248 name = first_token->u.value;
38250 cp_lexer_get_preprocessor_token (NULL, first_token);
38251 if (first_token->type != CPP_PRAGMA_EOL)
38252 error_at (first_token->location,
38253 "junk at end of %<#pragma GCC pch_preprocess%>");
38255 else
38256 error_at (first_token->location, "expected string literal");
38258 /* Skip to the end of the pragma. */
38259 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
38260 cp_lexer_get_preprocessor_token (NULL, first_token);
38262 /* Now actually load the PCH file. */
38263 if (name)
38264 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
38266 /* Read one more token to return to our caller. We have to do this
38267 after reading the PCH file in, since its pointers have to be
38268 live. */
38269 cp_lexer_get_preprocessor_token (NULL, first_token);
38272 /* Parses the grainsize pragma for the _Cilk_for statement.
38273 Syntax:
38274 #pragma cilk grainsize = <VALUE>. */
38276 static void
38277 cp_parser_cilk_grainsize (cp_parser *parser, cp_token *pragma_tok, bool *if_p)
38279 if (cp_parser_require (parser, CPP_EQ, RT_EQ))
38281 tree exp = cp_parser_binary_expression (parser, false, false,
38282 PREC_NOT_OPERATOR, NULL);
38283 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38284 if (!exp || exp == error_mark_node)
38286 error_at (pragma_tok->location, "invalid grainsize for _Cilk_for");
38287 return;
38290 /* Make sure the next token is _Cilk_for, it is invalid otherwise. */
38291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CILK_FOR))
38292 cp_parser_cilk_for (parser, exp, if_p);
38293 else
38294 warning_at (cp_lexer_peek_token (parser->lexer)->location, 0,
38295 "%<#pragma cilk grainsize%> is not followed by "
38296 "%<_Cilk_for%>");
38297 return;
38299 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38302 /* Normal parsing of a pragma token. Here we can (and must) use the
38303 regular lexer. */
38305 static bool
38306 cp_parser_pragma (cp_parser *parser, enum pragma_context context, bool *if_p)
38308 cp_token *pragma_tok;
38309 unsigned int id;
38310 tree stmt;
38311 bool ret;
38313 pragma_tok = cp_lexer_consume_token (parser->lexer);
38314 gcc_assert (pragma_tok->type == CPP_PRAGMA);
38315 parser->lexer->in_pragma = true;
38317 id = cp_parser_pragma_kind (pragma_tok);
38318 if (id != PRAGMA_OMP_DECLARE && id != PRAGMA_OACC_ROUTINE)
38319 cp_ensure_no_omp_declare_simd (parser);
38320 switch (id)
38322 case PRAGMA_GCC_PCH_PREPROCESS:
38323 error_at (pragma_tok->location,
38324 "%<#pragma GCC pch_preprocess%> must be first");
38325 break;
38327 case PRAGMA_OMP_BARRIER:
38328 switch (context)
38330 case pragma_compound:
38331 cp_parser_omp_barrier (parser, pragma_tok);
38332 return false;
38333 case pragma_stmt:
38334 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38335 "used in compound statements", "omp barrier");
38336 break;
38337 default:
38338 goto bad_stmt;
38340 break;
38342 case PRAGMA_OMP_FLUSH:
38343 switch (context)
38345 case pragma_compound:
38346 cp_parser_omp_flush (parser, pragma_tok);
38347 return false;
38348 case pragma_stmt:
38349 error_at (pragma_tok->location, "%<#pragma %s%> may only be "
38350 "used in compound statements", "omp flush");
38351 break;
38352 default:
38353 goto bad_stmt;
38355 break;
38357 case PRAGMA_OMP_TASKWAIT:
38358 switch (context)
38360 case pragma_compound:
38361 cp_parser_omp_taskwait (parser, pragma_tok);
38362 return false;
38363 case pragma_stmt:
38364 error_at (pragma_tok->location,
38365 "%<#pragma %s%> may only be used in compound statements",
38366 "omp taskwait");
38367 break;
38368 default:
38369 goto bad_stmt;
38371 break;
38373 case PRAGMA_OMP_TASKYIELD:
38374 switch (context)
38376 case pragma_compound:
38377 cp_parser_omp_taskyield (parser, pragma_tok);
38378 return false;
38379 case pragma_stmt:
38380 error_at (pragma_tok->location,
38381 "%<#pragma %s%> may only be used in compound statements",
38382 "omp taskyield");
38383 break;
38384 default:
38385 goto bad_stmt;
38387 break;
38389 case PRAGMA_OMP_CANCEL:
38390 switch (context)
38392 case pragma_compound:
38393 cp_parser_omp_cancel (parser, pragma_tok);
38394 return false;
38395 case pragma_stmt:
38396 error_at (pragma_tok->location,
38397 "%<#pragma %s%> may only be used in compound statements",
38398 "omp cancel");
38399 break;
38400 default:
38401 goto bad_stmt;
38403 break;
38405 case PRAGMA_OMP_CANCELLATION_POINT:
38406 cp_parser_omp_cancellation_point (parser, pragma_tok, context);
38407 return false;
38409 case PRAGMA_OMP_THREADPRIVATE:
38410 cp_parser_omp_threadprivate (parser, pragma_tok);
38411 return false;
38413 case PRAGMA_OMP_DECLARE:
38414 cp_parser_omp_declare (parser, pragma_tok, context);
38415 return false;
38417 case PRAGMA_OACC_DECLARE:
38418 cp_parser_oacc_declare (parser, pragma_tok);
38419 return false;
38421 case PRAGMA_OACC_ENTER_DATA:
38422 if (context == pragma_stmt)
38424 error_at (pragma_tok->location,
38425 "%<#pragma %s%> may only be used in compound statements",
38426 "acc enter data");
38427 break;
38429 else if (context != pragma_compound)
38430 goto bad_stmt;
38431 cp_parser_omp_construct (parser, pragma_tok, if_p);
38432 return true;
38434 case PRAGMA_OACC_EXIT_DATA:
38435 if (context == pragma_stmt)
38437 error_at (pragma_tok->location,
38438 "%<#pragma %s%> may only be used in compound statements",
38439 "acc exit data");
38440 break;
38442 else if (context != pragma_compound)
38443 goto bad_stmt;
38444 cp_parser_omp_construct (parser, pragma_tok, if_p);
38445 return true;
38447 case PRAGMA_OACC_ROUTINE:
38448 if (context != pragma_external)
38450 error_at (pragma_tok->location,
38451 "%<#pragma acc routine%> must be at file scope");
38452 break;
38454 cp_parser_oacc_routine (parser, pragma_tok, context);
38455 return false;
38457 case PRAGMA_OACC_UPDATE:
38458 if (context == pragma_stmt)
38460 error_at (pragma_tok->location,
38461 "%<#pragma %s%> may only be used in compound statements",
38462 "acc update");
38463 break;
38465 else if (context != pragma_compound)
38466 goto bad_stmt;
38467 cp_parser_omp_construct (parser, pragma_tok, if_p);
38468 return true;
38470 case PRAGMA_OACC_WAIT:
38471 if (context == pragma_stmt)
38473 error_at (pragma_tok->location,
38474 "%<#pragma %s%> may only be used in compound statements",
38475 "acc wait");
38476 break;
38478 else if (context != pragma_compound)
38479 goto bad_stmt;
38480 cp_parser_omp_construct (parser, pragma_tok, if_p);
38481 return true;
38483 case PRAGMA_OACC_ATOMIC:
38484 case PRAGMA_OACC_CACHE:
38485 case PRAGMA_OACC_DATA:
38486 case PRAGMA_OACC_HOST_DATA:
38487 case PRAGMA_OACC_KERNELS:
38488 case PRAGMA_OACC_PARALLEL:
38489 case PRAGMA_OACC_LOOP:
38490 case PRAGMA_OMP_ATOMIC:
38491 case PRAGMA_OMP_CRITICAL:
38492 case PRAGMA_OMP_DISTRIBUTE:
38493 case PRAGMA_OMP_FOR:
38494 case PRAGMA_OMP_MASTER:
38495 case PRAGMA_OMP_PARALLEL:
38496 case PRAGMA_OMP_SECTIONS:
38497 case PRAGMA_OMP_SIMD:
38498 case PRAGMA_OMP_SINGLE:
38499 case PRAGMA_OMP_TASK:
38500 case PRAGMA_OMP_TASKGROUP:
38501 case PRAGMA_OMP_TASKLOOP:
38502 case PRAGMA_OMP_TEAMS:
38503 if (context != pragma_stmt && context != pragma_compound)
38504 goto bad_stmt;
38505 stmt = push_omp_privatization_clauses (false);
38506 cp_parser_omp_construct (parser, pragma_tok, if_p);
38507 pop_omp_privatization_clauses (stmt);
38508 return true;
38510 case PRAGMA_OMP_ORDERED:
38511 if (context != pragma_stmt && context != pragma_compound)
38512 goto bad_stmt;
38513 stmt = push_omp_privatization_clauses (false);
38514 ret = cp_parser_omp_ordered (parser, pragma_tok, context, if_p);
38515 pop_omp_privatization_clauses (stmt);
38516 return ret;
38518 case PRAGMA_OMP_TARGET:
38519 if (context != pragma_stmt && context != pragma_compound)
38520 goto bad_stmt;
38521 stmt = push_omp_privatization_clauses (false);
38522 ret = cp_parser_omp_target (parser, pragma_tok, context, if_p);
38523 pop_omp_privatization_clauses (stmt);
38524 return ret;
38526 case PRAGMA_OMP_END_DECLARE_TARGET:
38527 cp_parser_omp_end_declare_target (parser, pragma_tok);
38528 return false;
38530 case PRAGMA_OMP_SECTION:
38531 error_at (pragma_tok->location,
38532 "%<#pragma omp section%> may only be used in "
38533 "%<#pragma omp sections%> construct");
38534 break;
38536 case PRAGMA_IVDEP:
38538 if (context == pragma_external)
38540 error_at (pragma_tok->location,
38541 "%<#pragma GCC ivdep%> must be inside a function");
38542 break;
38544 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38545 cp_token *tok;
38546 tok = cp_lexer_peek_token (the_parser->lexer);
38547 if (tok->type != CPP_KEYWORD
38548 || (tok->keyword != RID_FOR && tok->keyword != RID_WHILE
38549 && tok->keyword != RID_DO))
38551 cp_parser_error (parser, "for, while or do statement expected");
38552 return false;
38554 cp_parser_iteration_statement (parser, if_p, true);
38555 return true;
38558 case PRAGMA_CILK_SIMD:
38559 if (context == pragma_external)
38561 error_at (pragma_tok->location,
38562 "%<#pragma simd%> must be inside a function");
38563 break;
38565 stmt = push_omp_privatization_clauses (false);
38566 cp_parser_cilk_simd (parser, pragma_tok, if_p);
38567 pop_omp_privatization_clauses (stmt);
38568 return true;
38570 case PRAGMA_CILK_GRAINSIZE:
38571 if (context == pragma_external)
38573 error_at (pragma_tok->location,
38574 "%<#pragma cilk grainsize%> must be inside a function");
38575 break;
38578 /* Ignore the pragma if Cilk Plus is not enabled. */
38579 if (flag_cilkplus)
38581 cp_parser_cilk_grainsize (parser, pragma_tok, if_p);
38582 return true;
38584 else
38586 error_at (pragma_tok->location, "-fcilkplus must be enabled to use "
38587 "%<#pragma cilk grainsize%>");
38588 break;
38591 default:
38592 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
38593 c_invoke_pragma_handler (id);
38594 break;
38596 bad_stmt:
38597 cp_parser_error (parser, "expected declaration specifiers");
38598 break;
38601 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
38602 return false;
38605 /* The interface the pragma parsers have to the lexer. */
38607 enum cpp_ttype
38608 pragma_lex (tree *value, location_t *loc)
38610 cp_token *tok = cp_lexer_peek_token (the_parser->lexer);
38611 enum cpp_ttype ret = tok->type;
38613 *value = tok->u.value;
38614 if (loc)
38615 *loc = tok->location;
38617 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
38618 ret = CPP_EOF;
38619 else if (ret == CPP_STRING)
38620 *value = cp_parser_string_literal (the_parser, false, false);
38621 else
38623 if (ret == CPP_KEYWORD)
38624 ret = CPP_NAME;
38625 cp_lexer_consume_token (the_parser->lexer);
38628 return ret;
38632 /* External interface. */
38634 /* Parse one entire translation unit. */
38636 void
38637 c_parse_file (void)
38639 static bool already_called = false;
38641 if (already_called)
38642 fatal_error (input_location,
38643 "inter-module optimizations not implemented for C++");
38644 already_called = true;
38646 the_parser = cp_parser_new ();
38647 push_deferring_access_checks (flag_access_control
38648 ? dk_no_deferred : dk_no_check);
38649 cp_parser_translation_unit (the_parser);
38650 the_parser = NULL;
38653 /* Parses the Cilk Plus #pragma simd and SIMD-enabled function attribute's
38654 vectorlength clause:
38655 Syntax:
38656 vectorlength ( constant-expression ) */
38658 static tree
38659 cp_parser_cilk_simd_vectorlength (cp_parser *parser, tree clauses,
38660 bool is_simd_fn)
38662 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38663 tree expr;
38664 /* The vectorlength clause in #pragma simd behaves exactly like OpenMP's
38665 safelen clause. Thus, vectorlength is represented as OMP 4.0
38666 safelen. For SIMD-enabled function it is represented by OMP 4.0
38667 simdlen. */
38668 if (!is_simd_fn)
38669 check_no_duplicate_clause (clauses, OMP_CLAUSE_SAFELEN, "vectorlength",
38670 loc);
38671 else
38672 check_no_duplicate_clause (clauses, OMP_CLAUSE_SIMDLEN, "vectorlength",
38673 loc);
38675 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38676 return error_mark_node;
38678 expr = cp_parser_constant_expression (parser);
38679 expr = maybe_constant_value (expr);
38681 /* If expr == error_mark_node, then don't emit any errors nor
38682 create a clause. if any of the above functions returns
38683 error mark node then they would have emitted an error message. */
38684 if (expr == error_mark_node)
38686 else if (!TREE_TYPE (expr)
38687 || !TREE_CONSTANT (expr)
38688 || !INTEGRAL_TYPE_P (TREE_TYPE (expr)))
38689 error_at (loc, "vectorlength must be an integer constant");
38690 else if (TREE_CONSTANT (expr)
38691 && !pow2p_hwi (TREE_INT_CST_LOW (expr)))
38692 error_at (loc, "vectorlength must be a power of 2");
38693 else
38695 tree c;
38696 if (!is_simd_fn)
38698 c = build_omp_clause (loc, OMP_CLAUSE_SAFELEN);
38699 OMP_CLAUSE_SAFELEN_EXPR (c) = expr;
38700 OMP_CLAUSE_CHAIN (c) = clauses;
38701 clauses = c;
38703 else
38705 c = build_omp_clause (loc, OMP_CLAUSE_SIMDLEN);
38706 OMP_CLAUSE_SIMDLEN_EXPR (c) = expr;
38707 OMP_CLAUSE_CHAIN (c) = clauses;
38708 clauses = c;
38712 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
38713 return error_mark_node;
38714 return clauses;
38717 /* Handles the Cilk Plus #pragma simd linear clause.
38718 Syntax:
38719 linear ( simd-linear-variable-list )
38721 simd-linear-variable-list:
38722 simd-linear-variable
38723 simd-linear-variable-list , simd-linear-variable
38725 simd-linear-variable:
38726 id-expression
38727 id-expression : simd-linear-step
38729 simd-linear-step:
38730 conditional-expression */
38732 static tree
38733 cp_parser_cilk_simd_linear (cp_parser *parser, tree clauses)
38735 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
38737 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
38738 return clauses;
38739 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38741 cp_parser_error (parser, "expected identifier");
38742 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38743 return error_mark_node;
38746 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
38747 parser->colon_corrects_to_scope_p = false;
38748 while (1)
38750 cp_token *token = cp_lexer_peek_token (parser->lexer);
38751 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
38753 cp_parser_error (parser, "expected variable-name");
38754 clauses = error_mark_node;
38755 break;
38758 tree var_name = cp_parser_id_expression (parser, false, true, NULL,
38759 false, false);
38760 tree decl = cp_parser_lookup_name_simple (parser, var_name,
38761 token->location);
38762 if (decl == error_mark_node)
38764 cp_parser_name_lookup_error (parser, var_name, decl, NLE_NULL,
38765 token->location);
38766 clauses = error_mark_node;
38768 else
38770 tree e = NULL_TREE;
38771 tree step_size = integer_one_node;
38773 /* If present, parse the linear step. Otherwise, assume the default
38774 value of 1. */
38775 if (cp_lexer_peek_token (parser->lexer)->type == CPP_COLON)
38777 cp_lexer_consume_token (parser->lexer);
38779 e = cp_parser_assignment_expression (parser);
38780 e = maybe_constant_value (e);
38782 if (e == error_mark_node)
38784 /* If an error has occurred, then the whole pragma is
38785 considered ill-formed. Thus, no reason to keep
38786 parsing. */
38787 clauses = error_mark_node;
38788 break;
38790 else if (type_dependent_expression_p (e)
38791 || value_dependent_expression_p (e)
38792 || (TREE_TYPE (e)
38793 && INTEGRAL_TYPE_P (TREE_TYPE (e))
38794 && (TREE_CONSTANT (e)
38795 || DECL_P (e))))
38796 step_size = e;
38797 else
38798 cp_parser_error (parser,
38799 "step size must be an integer constant "
38800 "expression or an integer variable");
38803 /* Use the OMP_CLAUSE_LINEAR, which has the same semantics. */
38804 tree l = build_omp_clause (loc, OMP_CLAUSE_LINEAR);
38805 OMP_CLAUSE_DECL (l) = decl;
38806 OMP_CLAUSE_LINEAR_STEP (l) = step_size;
38807 OMP_CLAUSE_CHAIN (l) = clauses;
38808 clauses = l;
38810 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
38811 cp_lexer_consume_token (parser->lexer);
38812 else if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
38813 break;
38814 else
38816 error_at (cp_lexer_peek_token (parser->lexer)->location,
38817 "expected %<,%> or %<)%> after %qE", decl);
38818 clauses = error_mark_node;
38819 break;
38822 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
38823 cp_parser_skip_to_closing_parenthesis (parser, false, false, true);
38824 return clauses;
38827 /* Returns the name of the next clause. If the clause is not
38828 recognized, then PRAGMA_CILK_CLAUSE_NONE is returned and the next
38829 token is not consumed. Otherwise, the appropriate enum from the
38830 pragma_simd_clause is returned and the token is consumed. */
38832 static pragma_omp_clause
38833 cp_parser_cilk_simd_clause_name (cp_parser *parser)
38835 pragma_omp_clause clause_type;
38836 cp_token *token = cp_lexer_peek_token (parser->lexer);
38838 if (token->keyword == RID_PRIVATE)
38839 clause_type = PRAGMA_CILK_CLAUSE_PRIVATE;
38840 else if (!token->u.value || token->type != CPP_NAME)
38841 return PRAGMA_CILK_CLAUSE_NONE;
38842 else if (id_equal (token->u.value, "vectorlength"))
38843 clause_type = PRAGMA_CILK_CLAUSE_VECTORLENGTH;
38844 else if (id_equal (token->u.value, "linear"))
38845 clause_type = PRAGMA_CILK_CLAUSE_LINEAR;
38846 else if (id_equal (token->u.value, "firstprivate"))
38847 clause_type = PRAGMA_CILK_CLAUSE_FIRSTPRIVATE;
38848 else if (id_equal (token->u.value, "lastprivate"))
38849 clause_type = PRAGMA_CILK_CLAUSE_LASTPRIVATE;
38850 else if (id_equal (token->u.value, "reduction"))
38851 clause_type = PRAGMA_CILK_CLAUSE_REDUCTION;
38852 else
38853 return PRAGMA_CILK_CLAUSE_NONE;
38855 cp_lexer_consume_token (parser->lexer);
38856 return clause_type;
38859 /* Parses all the #pragma simd clauses. Returns a list of clauses found. */
38861 static tree
38862 cp_parser_cilk_simd_all_clauses (cp_parser *parser, cp_token *pragma_token)
38864 tree clauses = NULL_TREE;
38866 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL)
38867 && clauses != error_mark_node)
38869 pragma_omp_clause c_kind;
38870 c_kind = cp_parser_cilk_simd_clause_name (parser);
38871 if (c_kind == PRAGMA_CILK_CLAUSE_VECTORLENGTH)
38872 clauses = cp_parser_cilk_simd_vectorlength (parser, clauses, false);
38873 else if (c_kind == PRAGMA_CILK_CLAUSE_LINEAR)
38874 clauses = cp_parser_cilk_simd_linear (parser, clauses);
38875 else if (c_kind == PRAGMA_CILK_CLAUSE_PRIVATE)
38876 /* Use the OpenMP 4.0 equivalent function. */
38877 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE, clauses);
38878 else if (c_kind == PRAGMA_CILK_CLAUSE_FIRSTPRIVATE)
38879 /* Use the OpenMP 4.0 equivalent function. */
38880 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
38881 clauses);
38882 else if (c_kind == PRAGMA_CILK_CLAUSE_LASTPRIVATE)
38883 /* Use the OMP 4.0 equivalent function. */
38884 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
38885 clauses);
38886 else if (c_kind == PRAGMA_CILK_CLAUSE_REDUCTION)
38887 /* Use the OMP 4.0 equivalent function. */
38888 clauses = cp_parser_omp_clause_reduction (parser, clauses);
38889 else
38891 clauses = error_mark_node;
38892 cp_parser_error (parser, "expected %<#pragma simd%> clause");
38893 break;
38897 cp_parser_skip_to_pragma_eol (parser, pragma_token);
38899 if (clauses == error_mark_node)
38900 return error_mark_node;
38901 else
38902 return finish_omp_clauses (clauses, C_ORT_CILK);
38905 /* Main entry-point for parsing Cilk Plus <#pragma simd> for loops. */
38907 static void
38908 cp_parser_cilk_simd (cp_parser *parser, cp_token *pragma_token, bool *if_p)
38910 tree clauses = cp_parser_cilk_simd_all_clauses (parser, pragma_token);
38912 if (clauses == error_mark_node)
38913 return;
38915 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_FOR))
38917 error_at (cp_lexer_peek_token (parser->lexer)->location,
38918 "for statement expected");
38919 return;
38922 tree sb = begin_omp_structured_block ();
38923 int save = cp_parser_begin_omp_structured_block (parser);
38924 tree ret = cp_parser_omp_for_loop (parser, CILK_SIMD, clauses, NULL, if_p);
38925 if (ret)
38926 cpp_validate_cilk_plus_loop (OMP_FOR_BODY (ret));
38927 cp_parser_end_omp_structured_block (parser, save);
38928 add_stmt (finish_omp_structured_block (sb));
38931 /* Main entry-point for parsing Cilk Plus _Cilk_for
38932 loops. The return value is error_mark_node
38933 when errors happen and CILK_FOR tree on success. */
38935 static tree
38936 cp_parser_cilk_for (cp_parser *parser, tree grain, bool *if_p)
38938 if (cp_lexer_next_token_is_not_keyword (parser->lexer, RID_CILK_FOR))
38939 gcc_unreachable ();
38941 tree sb = begin_omp_structured_block ();
38942 int save = cp_parser_begin_omp_structured_block (parser);
38944 tree clauses = build_omp_clause (EXPR_LOCATION (grain), OMP_CLAUSE_SCHEDULE);
38945 OMP_CLAUSE_SCHEDULE_KIND (clauses) = OMP_CLAUSE_SCHEDULE_CILKFOR;
38946 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (clauses) = grain;
38947 clauses = finish_omp_clauses (clauses, C_ORT_CILK);
38949 tree ret = cp_parser_omp_for_loop (parser, CILK_FOR, clauses, NULL, if_p);
38950 if (ret)
38951 cpp_validate_cilk_plus_loop (ret);
38952 else
38953 ret = error_mark_node;
38955 cp_parser_end_omp_structured_block (parser, save);
38956 add_stmt (finish_omp_structured_block (sb));
38957 return ret;
38960 /* Create an identifier for a generic parameter type (a synthesized
38961 template parameter implied by `auto' or a concept identifier). */
38963 static GTY(()) int generic_parm_count;
38964 static tree
38965 make_generic_type_name ()
38967 char buf[32];
38968 sprintf (buf, "auto:%d", ++generic_parm_count);
38969 return get_identifier (buf);
38972 /* Add an implicit template type parameter to the CURRENT_TEMPLATE_PARMS
38973 (creating a new template parameter list if necessary). Returns the newly
38974 created template type parm. */
38976 static tree
38977 synthesize_implicit_template_parm (cp_parser *parser, tree constr)
38979 gcc_assert (current_binding_level->kind == sk_function_parms);
38981 /* Before committing to modifying any scope, if we're in an
38982 implicit template scope, and we're trying to synthesize a
38983 constrained parameter, try to find a previous parameter with
38984 the same name. This is the same-type rule for abbreviated
38985 function templates.
38987 NOTE: We can generate implicit parameters when tentatively
38988 parsing a nested name specifier, only to reject that parse
38989 later. However, matching the same template-id as part of a
38990 direct-declarator should generate an identical template
38991 parameter, so this rule will merge them. */
38992 if (parser->implicit_template_scope && constr)
38994 tree t = parser->implicit_template_parms;
38995 while (t)
38997 if (equivalent_placeholder_constraints (TREE_TYPE (t), constr))
38999 tree d = TREE_VALUE (t);
39000 if (TREE_CODE (d) == PARM_DECL)
39001 /* Return the TEMPLATE_PARM_INDEX. */
39002 d = DECL_INITIAL (d);
39003 return d;
39005 t = TREE_CHAIN (t);
39009 /* We are either continuing a function template that already contains implicit
39010 template parameters, creating a new fully-implicit function template, or
39011 extending an existing explicit function template with implicit template
39012 parameters. */
39014 cp_binding_level *const entry_scope = current_binding_level;
39016 bool become_template = false;
39017 cp_binding_level *parent_scope = 0;
39019 if (parser->implicit_template_scope)
39021 gcc_assert (parser->implicit_template_parms);
39023 current_binding_level = parser->implicit_template_scope;
39025 else
39027 /* Roll back to the existing template parameter scope (in the case of
39028 extending an explicit function template) or introduce a new template
39029 parameter scope ahead of the function parameter scope (or class scope
39030 in the case of out-of-line member definitions). The function scope is
39031 added back after template parameter synthesis below. */
39033 cp_binding_level *scope = entry_scope;
39035 while (scope->kind == sk_function_parms)
39037 parent_scope = scope;
39038 scope = scope->level_chain;
39040 if (current_class_type && !LAMBDA_TYPE_P (current_class_type))
39042 /* If not defining a class, then any class scope is a scope level in
39043 an out-of-line member definition. In this case simply wind back
39044 beyond the first such scope to inject the template parameter list.
39045 Otherwise wind back to the class being defined. The latter can
39046 occur in class member friend declarations such as:
39048 class A {
39049 void foo (auto);
39051 class B {
39052 friend void A::foo (auto);
39055 The template parameter list synthesized for the friend declaration
39056 must be injected in the scope of 'B'. This can also occur in
39057 erroneous cases such as:
39059 struct A {
39060 struct B {
39061 void foo (auto);
39063 void B::foo (auto) {}
39066 Here the attempted definition of 'B::foo' within 'A' is ill-formed
39067 but, nevertheless, the template parameter list synthesized for the
39068 declarator should be injected into the scope of 'A' as if the
39069 ill-formed template was specified explicitly. */
39071 while (scope->kind == sk_class && !scope->defining_class_p)
39073 parent_scope = scope;
39074 scope = scope->level_chain;
39078 current_binding_level = scope;
39080 if (scope->kind != sk_template_parms
39081 || !function_being_declared_is_template_p (parser))
39083 /* Introduce a new template parameter list for implicit template
39084 parameters. */
39086 become_template = true;
39088 parser->implicit_template_scope
39089 = begin_scope (sk_template_parms, NULL);
39091 ++processing_template_decl;
39093 parser->fully_implicit_function_template_p = true;
39094 ++parser->num_template_parameter_lists;
39096 else
39098 /* Synthesize implicit template parameters at the end of the explicit
39099 template parameter list. */
39101 gcc_assert (current_template_parms);
39103 parser->implicit_template_scope = scope;
39105 tree v = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39106 parser->implicit_template_parms
39107 = TREE_VEC_ELT (v, TREE_VEC_LENGTH (v) - 1);
39111 /* Synthesize a new template parameter and track the current template
39112 parameter chain with implicit_template_parms. */
39114 tree proto = constr ? DECL_INITIAL (constr) : NULL_TREE;
39115 tree synth_id = make_generic_type_name ();
39116 tree synth_tmpl_parm;
39117 bool non_type = false;
39119 if (proto == NULL_TREE || TREE_CODE (proto) == TYPE_DECL)
39120 synth_tmpl_parm
39121 = finish_template_type_parm (class_type_node, synth_id);
39122 else if (TREE_CODE (proto) == TEMPLATE_DECL)
39123 synth_tmpl_parm
39124 = finish_constrained_template_template_parm (proto, synth_id);
39125 else
39127 synth_tmpl_parm = copy_decl (proto);
39128 DECL_NAME (synth_tmpl_parm) = synth_id;
39129 non_type = true;
39132 // Attach the constraint to the parm before processing.
39133 tree node = build_tree_list (NULL_TREE, synth_tmpl_parm);
39134 TREE_TYPE (node) = constr;
39135 tree new_parm
39136 = process_template_parm (parser->implicit_template_parms,
39137 input_location,
39138 node,
39139 /*non_type=*/non_type,
39140 /*param_pack=*/false);
39142 // Chain the new parameter to the list of implicit parameters.
39143 if (parser->implicit_template_parms)
39144 parser->implicit_template_parms
39145 = TREE_CHAIN (parser->implicit_template_parms);
39146 else
39147 parser->implicit_template_parms = new_parm;
39149 tree new_decl = get_local_decls ();
39150 if (non_type)
39151 /* Return the TEMPLATE_PARM_INDEX, not the PARM_DECL. */
39152 new_decl = DECL_INITIAL (new_decl);
39154 /* If creating a fully implicit function template, start the new implicit
39155 template parameter list with this synthesized type, otherwise grow the
39156 current template parameter list. */
39158 if (become_template)
39160 parent_scope->level_chain = current_binding_level;
39162 tree new_parms = make_tree_vec (1);
39163 TREE_VEC_ELT (new_parms, 0) = parser->implicit_template_parms;
39164 current_template_parms = tree_cons (size_int (processing_template_decl),
39165 new_parms, current_template_parms);
39167 else
39169 tree& new_parms = INNERMOST_TEMPLATE_PARMS (current_template_parms);
39170 int new_parm_idx = TREE_VEC_LENGTH (new_parms);
39171 new_parms = grow_tree_vec (new_parms, new_parm_idx + 1);
39172 TREE_VEC_ELT (new_parms, new_parm_idx) = parser->implicit_template_parms;
39175 // If the new parameter was constrained, we need to add that to the
39176 // constraints in the template parameter list.
39177 if (tree req = TEMPLATE_PARM_CONSTRAINTS (tree_last (new_parm)))
39179 tree reqs = TEMPLATE_PARMS_CONSTRAINTS (current_template_parms);
39180 reqs = conjoin_constraints (reqs, req);
39181 TEMPLATE_PARMS_CONSTRAINTS (current_template_parms) = reqs;
39184 current_binding_level = entry_scope;
39186 return new_decl;
39189 /* Finish the declaration of a fully implicit function template. Such a
39190 template has no explicit template parameter list so has not been through the
39191 normal template head and tail processing. synthesize_implicit_template_parm
39192 tries to do the head; this tries to do the tail. MEMBER_DECL_OPT should be
39193 provided if the declaration is a class member such that its template
39194 declaration can be completed. If MEMBER_DECL_OPT is provided the finished
39195 form is returned. Otherwise NULL_TREE is returned. */
39197 static tree
39198 finish_fully_implicit_template (cp_parser *parser, tree member_decl_opt)
39200 gcc_assert (parser->fully_implicit_function_template_p);
39202 if (member_decl_opt && member_decl_opt != error_mark_node
39203 && DECL_VIRTUAL_P (member_decl_opt))
39205 error_at (DECL_SOURCE_LOCATION (member_decl_opt),
39206 "implicit templates may not be %<virtual%>");
39207 DECL_VIRTUAL_P (member_decl_opt) = false;
39210 if (member_decl_opt)
39211 member_decl_opt = finish_member_template_decl (member_decl_opt);
39212 end_template_decl ();
39214 parser->fully_implicit_function_template_p = false;
39215 --parser->num_template_parameter_lists;
39217 return member_decl_opt;
39220 #include "gt-cp-parser.h"